b1ea3f0142af212925e58b5fea88634df714978a
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / gc0309.c
1 /*
2 o* Driver for MT9M001 CMOS Image Sensor from Micron
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/miscdevice.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/soc_camera.h>
22 #include <mach/rk29_camera.h>
23
24 static int debug;
25 module_param(debug, int, S_IRUGO|S_IWUSR);
26
27 #define dprintk(level, fmt, arg...) do {                        \
28         if (debug >= level)                                     \
29         printk(KERN_WARNING fmt , ## arg); } while (0)
30
31 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
32 #define SENSOR_DG(format, ...) dprintk(0, format, ## __VA_ARGS__)
33
34
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
37
38 #define __STR(x) #x
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
41
42 #define MIN(x,y)   ((x<y) ? x: y)
43 #define MAX(x,y)    ((x>y) ? x: y)
44
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK29_CAM_SENSOR_GC0309
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_GC0309
48 #define SENSOR_ID 0xa0
49 #define SENSOR_MIN_WIDTH    176
50 #define SENSOR_MIN_HEIGHT   144
51 #define SENSOR_MAX_WIDTH    640
52 #define SENSOR_MAX_HEIGHT   480
53 #define SENSOR_INIT_WIDTH       640             /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT  480
55 #define SENSOR_INIT_WINSEQADR sensor_vga
56 #define SENSOR_INIT_PIXFMT V4L2_PIX_FMT_UYVY
57
58 #define CONFIG_SENSOR_WhiteBalance      1
59 #define CONFIG_SENSOR_Brightness        0
60 #define CONFIG_SENSOR_Contrast      0
61 #define CONFIG_SENSOR_Saturation    0
62 #define CONFIG_SENSOR_Effect        1
63 #define CONFIG_SENSOR_Scene         1
64 #define CONFIG_SENSOR_DigitalZoom   0
65 #define CONFIG_SENSOR_Focus         0
66 #define CONFIG_SENSOR_Exposure      0
67 #define CONFIG_SENSOR_Flash         0
68 #define CONFIG_SENSOR_Mirror        0
69 #define CONFIG_SENSOR_Flip          0
70
71 #define CONFIG_SENSOR_I2C_SPEED     100000       /* Hz */
72 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
73 #define CONFIG_SENSOR_I2C_NOSCHED   0
74 #define CONFIG_SENSOR_I2C_RDWRCHK   0
75
76 #define SENSOR_BUS_PARAM  (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING|\
77                           SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |\
78                           SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ)
79
80 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
81 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
82 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
83 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
84 #define COLOR_TEMPERATURE_OFFICE_DN     3500
85 #define COLOR_TEMPERATURE_OFFICE_UP     5000
86 #define COLOR_TEMPERATURE_HOME_DN       2500
87 #define COLOR_TEMPERATURE_HOME_UP       3500
88
89 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
90 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
91
92
93 struct reginfo
94 {
95     u8 reg;
96     u8 val;
97 };
98
99 /* init SVGA preview */
100 static struct reginfo sensor_init_data[] =
101 {
102           /*init registers code.*/
103
104 {0xfe,0x80},   // soft reset    
105                 
106 //      GC0309_SET_PAGE0;       // set page0
107         
108 {0x1a,0x16},    
109 {0xd2,0x10},   // close AEC
110 {0x22,0x55},   // close AWB
111
112 {0x5a,0x56}, 
113 {0x5b,0x40},
114 {0x5c,0x4a},                    
115
116 {0x22,0x57}, 
117                 
118 {0x01,0xfa}, 
119 {0x02,0x70}, 
120 {0x0f,0x01}, 
121
122 {0xe2,0x00}, 
123 {0xe3,0x64}, 
124
125 {0x03,0x01}, 
126 {0x04,0x2c}, 
127
128         /*
129 {0x01,0x6a}, 
130 {0x02,0x25}, 
131 {0x0f,0x00},
132
133 {0xe2,0x00}, 
134 {0xe3,0x4b}, 
135                 
136 {0xe4,0x02}, 
137 {0xe5,0x0d}, 
138 {0xe6,0x02}, 
139 {0xe7,0x0d}, 
140 {0xe8,0x02}, 
141 {0xe9,0x0d}, 
142 {0xea,0x05}, 
143 {0xeb,0xdc}, 
144         */
145
146 {0x05,0x00},
147 {0x06,0x00},
148 {0x07,0x00}, 
149 {0x08,0x00}, 
150 {0x09,0x01}, 
151 {0x0a,0xe8}, 
152 {0x0b,0x02}, 
153 {0x0c,0x88}, 
154 {0x0d,0x02}, 
155 {0x0e,0x02}, 
156 {0x10,0x22}, 
157 {0x11,0x0d}, 
158 {0x12,0x2a}, 
159 {0x13,0x00}, 
160 //{0x14,0x10},  
161 {0x15,0x0a}, 
162 {0x16,0x05}, 
163 {0x17,0x01}, 
164
165 {0x1b,0x03}, 
166 {0x1c,0xc1}, 
167 {0x1d,0x08}, 
168 {0x1e,0x20},
169 {0x1f,0x16}, 
170
171 {0x20,0xff}, 
172 {0x21,0xf8}, 
173 {0x24,0xa2}, 
174 {0x25,0x0f},
175         //output sync_mode
176 {0x26,0x03}, 
177 {0x2f,0x01}, 
178         /////////////////////////////////////////////////////////////////////
179         /////////////////////////// grab_t ////////////////////////////////
180         /////////////////////////////////////////////////////////////////////
181 {0x30,0xf7}, 
182 {0x31,0x40},
183 {0x32,0x00}, 
184 {0x39,0x04}, 
185 {0x3a,0x20}, 
186 {0x3b,0x20}, 
187 {0x3c,0x02}, 
188 {0x3d,0x02}, 
189 {0x3e,0x02},
190 {0x3f,0x02}, 
191         
192         //gain
193 {0x50,0x24}, 
194         
195 {0x53,0x82}, 
196 {0x54,0x80}, 
197 {0x55,0x80}, 
198 {0x56,0x82}, 
199         
200         /////////////////////////////////////////////////////////////////////
201         /////////////////////////// LSC_t  ////////////////////////////////
202         /////////////////////////////////////////////////////////////////////
203 {0x8b,0x20}, 
204 {0x8c,0x20}, 
205 {0x8d,0x20}, 
206 {0x8e,0x10}, 
207 {0x8f,0x10}, 
208 {0x90,0x10}, 
209 {0x91,0x3c}, 
210 {0x92,0x50}, 
211 {0x5d,0x12}, 
212 {0x5e,0x1a}, 
213 {0x5f,0x24}, 
214         /////////////////////////////////////////////////////////////////////
215         /////////////////////////// DNDD_t  ///////////////////////////////
216         /////////////////////////////////////////////////////////////////////
217 {0x60,0x07}, 
218 {0x61,0x0e}, 
219 {0x62,0x0c}, 
220 {0x64,0x03}, 
221 {0x66,0xe8}, 
222 {0x67,0x86}, 
223 {0x68,0xa2}, 
224         
225         /////////////////////////////////////////////////////////////////////
226         /////////////////////////// asde_t ///////////////////////////////
227         /////////////////////////////////////////////////////////////////////
228 {0x69,0x20}, 
229 {0x6a,0x0f}, 
230 {0x6b,0x00}, 
231 {0x6c,0x53}, 
232 {0x6d,0x83}, 
233 {0x6e,0xac}, 
234 {0x6f,0xac}, 
235 {0x70,0x15}, 
236 {0x71,0x33}, 
237         /////////////////////////////////////////////////////////////////////
238         /////////////////////////// eeintp_t///////////////////////////////
239 };
240
241
242 /* 640X480 VGA */
243 static struct reginfo sensor_vga[] =
244 {
245 {0x45 , 0x0f}, //output enable
246  {0x0,0x0}
247 };
248
249 /* 352X288 CIF */
250 static struct reginfo sensor_cif[] =
251 {};
252
253 /* 320*240 QVGA */
254 static  struct reginfo sensor_qvga[] =
255 {};
256
257 /* 176X144 QCIF*/
258 static struct reginfo sensor_qcif[] =
259 {};
260
261
262 static  struct reginfo sensor_ClrFmt_YUYV[]=
263 {
264
265     {0x00, 0x00}
266 };
267
268 static  struct reginfo sensor_ClrFmt_UYVY[]=
269 {
270
271     {0x00, 0x00}
272 };
273
274 #if CONFIG_SENSOR_WhiteBalance
275 static  struct reginfo sensor_WhiteB_Auto[]=
276 {
277
278     {0x00, 0x00}
279 };
280 /* Cloudy Colour Temperature : 6500K - 8000K  */
281 static  struct reginfo sensor_WhiteB_Cloudy[]=
282 {
283
284     {0x00, 0x00}
285 };
286 /* ClearDay Colour Temperature : 5000K - 6500K  */
287 static  struct reginfo sensor_WhiteB_ClearDay[]=
288 {
289     //Sunny
290
291     {0x00, 0x00}
292 };
293 /* Office Colour Temperature : 3500K - 5000K  */
294 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
295 {
296     //Office
297   
298     {0x00, 0x00}
299
300 };
301 /* Home Colour Temperature : 2500K - 3500K  */
302 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
303 {
304     //Home
305
306     {0x00, 0x00}
307 };
308 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
309     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
310 };
311 #endif
312
313 #if CONFIG_SENSOR_Brightness
314 static  struct reginfo sensor_Brightness0[]=
315 {
316     // Brightness -2
317  
318     {0x00, 0x00}
319 };
320
321 static  struct reginfo sensor_Brightness1[]=
322 {
323     // Brightness -1
324
325     {0x00, 0x00}
326 };
327
328 static  struct reginfo sensor_Brightness2[]=
329 {
330     //  Brightness 0
331
332     {0x00, 0x00}
333 };
334
335 static  struct reginfo sensor_Brightness3[]=
336 {
337     // Brightness +1
338
339     {0x00, 0x00}
340 };
341
342 static  struct reginfo sensor_Brightness4[]=
343 {
344     //  Brightness +2
345
346     {0x00, 0x00}
347 };
348
349 static  struct reginfo sensor_Brightness5[]=
350 {
351     //  Brightness +3
352
353     {0x00, 0x00}
354 };
355 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
356     sensor_Brightness4, sensor_Brightness5,NULL,
357 };
358
359 #endif
360
361 #if CONFIG_SENSOR_Effect
362 static  struct reginfo sensor_Effect_Normal[] =
363 {
364
365     {0x00, 0x00}
366 };
367
368 static  struct reginfo sensor_Effect_WandB[] =
369 {
370
371     {0x00, 0x00}
372 };
373
374 static  struct reginfo sensor_Effect_Sepia[] =
375 {
376
377     {0x00, 0x00}
378 };
379
380 static  struct reginfo sensor_Effect_Negative[] =
381 {
382     //Negative
383
384     {0x00, 0x00}
385 };
386 static  struct reginfo sensor_Effect_Bluish[] =
387 {
388     // Bluish
389
390     {0x00, 0x00}
391 };
392
393 static  struct reginfo sensor_Effect_Green[] =
394 {
395     //  Greenish
396
397     {0x00, 0x00}
398 };
399 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
400     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
401 };
402 #endif
403 #if CONFIG_SENSOR_Exposure
404 static  struct reginfo sensor_Exposure0[]=
405 {
406     //-3
407
408 };
409
410 static  struct reginfo sensor_Exposure1[]=
411 {
412     //-2
413
414     {0x00, 0x00}
415 };
416
417 static  struct reginfo sensor_Exposure2[]=
418 {
419     //-0.3EV
420
421     {0x00, 0x00}
422 };
423
424 static  struct reginfo sensor_Exposure3[]=
425 {
426     //default
427
428     {0x00, 0x00}
429 };
430
431 static  struct reginfo sensor_Exposure4[]=
432 {
433     // 1
434
435     {0x00, 0x00}
436 };
437
438 static  struct reginfo sensor_Exposure5[]=
439 {
440     // 2
441
442     {0x00, 0x00}
443 };
444
445 static  struct reginfo sensor_Exposure6[]=
446 {
447     // 3
448
449     {0x00, 0x00}
450 };
451
452 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
453     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
454 };
455 #endif
456 #if CONFIG_SENSOR_Saturation
457 static  struct reginfo sensor_Saturation0[]=
458 {
459
460     {0x00, 0x00}
461 };
462
463 static  struct reginfo sensor_Saturation1[]=
464 {
465
466     {0x00, 0x00}
467 };
468
469 static  struct reginfo sensor_Saturation2[]=
470 {
471
472     {0x00, 0x00}
473 };
474 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
475
476 #endif
477 #if CONFIG_SENSOR_Contrast
478 static  struct reginfo sensor_Contrast0[]=
479 {
480     //Contrast -3
481   
482     {0x00, 0x00}
483 };
484
485 static  struct reginfo sensor_Contrast1[]=
486 {
487     //Contrast -2
488
489     {0x00, 0x00}
490 };
491
492 static  struct reginfo sensor_Contrast2[]=
493 {
494     // Contrast -1
495
496     {0x00, 0x00}
497 };
498
499 static  struct reginfo sensor_Contrast3[]=
500 {
501     //Contrast 0
502
503     {0x00, 0x00}
504 };
505
506 static  struct reginfo sensor_Contrast4[]=
507 {
508     //Contrast +1
509
510     {0x00, 0x00}
511 };
512
513
514 static  struct reginfo sensor_Contrast5[]=
515 {
516     //Contrast +2
517
518     {0x00, 0x00}
519 };
520
521 static  struct reginfo sensor_Contrast6[]=
522 {
523     //Contrast +3
524
525     {0x00, 0x00}
526 };
527 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
528     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
529 };
530
531 #endif
532 #if CONFIG_SENSOR_Mirror
533 static  struct reginfo sensor_MirrorOn[]=
534 {
535
536     {0x00, 0x00}
537 };
538
539 static  struct reginfo sensor_MirrorOff[]=
540 {
541
542     {0x00, 0x00}
543 };
544 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
545 #endif
546 #if CONFIG_SENSOR_Flip
547 static  struct reginfo sensor_FlipOn[]=
548 {
549
550     {0x00, 0x00}
551 };
552
553 static  struct reginfo sensor_FlipOff[]=
554 {
555
556     {0x00, 0x00}
557 };
558 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
559
560 #endif
561 #if CONFIG_SENSOR_Scene
562 static  struct reginfo sensor_SceneAuto[] =
563 {
564 #if 0                           /* ddl@rock-chips.com : */
565     {0x3014, 0x04},
566     {0x3015, 0x00},
567     {0x302e, 0x00},
568     {0x302d, 0x00},
569     {0x00, 0x00}
570 #else
571
572     {0x00, 0x00}
573 #endif
574 };
575
576 static  struct reginfo sensor_SceneNight[] =
577 {
578 #if 1
579     //30fps ~ 5fps night mode for 60/50Hz light environment, 24Mhz clock input,36Mzh pclk
580
581     {0x00, 0x00}
582 #else
583     //15fps ~ 5fps night mode for 60/50Hz light environment, 24Mhz clock input,18Mhz pclk
584     {0x300e, 0x34},
585     {0x3011, 0x01},
586     {0x302c, 0x00},
587     {0x3071, 0x00},
588     {0x3070, 0x5d},
589     {0x301c, 0x05},
590     {0x3073, 0x00},
591     {0x3072, 0x4d},
592     {0x301d, 0x07},
593     {0x3014, 0x0c},
594     {0x3015, 0x50},
595     {0x302e, 0x00},
596     {0x302d, 0x00},
597 #endif
598 };
599 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
600
601 #endif
602 #if CONFIG_SENSOR_DigitalZoom
603 static struct reginfo sensor_Zoom0[] =
604 {
605     {0x0, 0x0},
606 };
607
608 static struct reginfo sensor_Zoom1[] =
609 {
610      {0x0, 0x0},
611 };
612
613 static struct reginfo sensor_Zoom2[] =
614 {
615     {0x0, 0x0},
616 };
617
618
619 static struct reginfo sensor_Zoom3[] =
620 {
621     {0x0, 0x0},
622 };
623 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
624 #endif
625 static const struct v4l2_querymenu sensor_menus[] =
626 {
627         #if CONFIG_SENSOR_WhiteBalance
628     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
629     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
630     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
631     #endif
632
633         #if CONFIG_SENSOR_Effect
634     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
635     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
636     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
637     #endif
638
639         #if CONFIG_SENSOR_Scene
640     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
641     #endif
642
643         #if CONFIG_SENSOR_Flash
644     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
645     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
646     #endif
647 };
648
649 static const struct v4l2_queryctrl sensor_controls[] =
650 {
651         #if CONFIG_SENSOR_WhiteBalance
652     {
653         .id             = V4L2_CID_DO_WHITE_BALANCE,
654         .type           = V4L2_CTRL_TYPE_MENU,
655         .name           = "White Balance Control",
656         .minimum        = 0,
657         .maximum        = 4,
658         .step           = 1,
659         .default_value = 0,
660     },
661     #endif
662
663         #if CONFIG_SENSOR_Brightness
664         {
665         .id             = V4L2_CID_BRIGHTNESS,
666         .type           = V4L2_CTRL_TYPE_INTEGER,
667         .name           = "Brightness Control",
668         .minimum        = -3,
669         .maximum        = 2,
670         .step           = 1,
671         .default_value = 0,
672     },
673     #endif
674
675         #if CONFIG_SENSOR_Effect
676         {
677         .id             = V4L2_CID_EFFECT,
678         .type           = V4L2_CTRL_TYPE_MENU,
679         .name           = "Effect Control",
680         .minimum        = 0,
681         .maximum        = 5,
682         .step           = 1,
683         .default_value = 0,
684     },
685         #endif
686
687         #if CONFIG_SENSOR_Exposure
688         {
689         .id             = V4L2_CID_EXPOSURE,
690         .type           = V4L2_CTRL_TYPE_INTEGER,
691         .name           = "Exposure Control",
692         .minimum        = 0,
693         .maximum        = 6,
694         .step           = 1,
695         .default_value = 0,
696     },
697         #endif
698
699         #if CONFIG_SENSOR_Saturation
700         {
701         .id             = V4L2_CID_SATURATION,
702         .type           = V4L2_CTRL_TYPE_INTEGER,
703         .name           = "Saturation Control",
704         .minimum        = 0,
705         .maximum        = 2,
706         .step           = 1,
707         .default_value = 0,
708     },
709     #endif
710
711         #if CONFIG_SENSOR_Contrast
712         {
713         .id             = V4L2_CID_CONTRAST,
714         .type           = V4L2_CTRL_TYPE_INTEGER,
715         .name           = "Contrast Control",
716         .minimum        = -3,
717         .maximum        = 3,
718         .step           = 1,
719         .default_value = 0,
720     },
721         #endif
722
723         #if CONFIG_SENSOR_Mirror
724         {
725         .id             = V4L2_CID_HFLIP,
726         .type           = V4L2_CTRL_TYPE_BOOLEAN,
727         .name           = "Mirror Control",
728         .minimum        = 0,
729         .maximum        = 1,
730         .step           = 1,
731         .default_value = 1,
732     },
733     #endif
734
735         #if CONFIG_SENSOR_Flip
736         {
737         .id             = V4L2_CID_VFLIP,
738         .type           = V4L2_CTRL_TYPE_BOOLEAN,
739         .name           = "Flip Control",
740         .minimum        = 0,
741         .maximum        = 1,
742         .step           = 1,
743         .default_value = 1,
744     },
745     #endif
746
747         #if CONFIG_SENSOR_Scene
748     {
749         .id             = V4L2_CID_SCENE,
750         .type           = V4L2_CTRL_TYPE_MENU,
751         .name           = "Scene Control",
752         .minimum        = 0,
753         .maximum        = 1,
754         .step           = 1,
755         .default_value = 0,
756     },
757     #endif
758
759         #if CONFIG_SENSOR_DigitalZoom
760     {
761         .id             = V4L2_CID_ZOOM_RELATIVE,
762         .type           = V4L2_CTRL_TYPE_INTEGER,
763         .name           = "DigitalZoom Control",
764         .minimum        = -1,
765         .maximum        = 1,
766         .step           = 1,
767         .default_value = 0,
768     }, {
769         .id             = V4L2_CID_ZOOM_ABSOLUTE,
770         .type           = V4L2_CTRL_TYPE_INTEGER,
771         .name           = "DigitalZoom Control",
772         .minimum        = 0,
773         .maximum        = 3,
774         .step           = 1,
775         .default_value = 0,
776     },
777     #endif
778
779         #if CONFIG_SENSOR_Focus
780         {
781         .id             = V4L2_CID_FOCUS_RELATIVE,
782         .type           = V4L2_CTRL_TYPE_INTEGER,
783         .name           = "Focus Control",
784         .minimum        = -1,
785         .maximum        = 1,
786         .step           = 1,
787         .default_value = 0,
788     }, {
789         .id             = V4L2_CID_FOCUS_ABSOLUTE,
790         .type           = V4L2_CTRL_TYPE_INTEGER,
791         .name           = "Focus Control",
792         .minimum        = 0,
793         .maximum        = 255,
794         .step           = 1,
795         .default_value = 125,
796     },
797     #endif
798
799         #if CONFIG_SENSOR_Flash
800         {
801         .id             = V4L2_CID_FLASH,
802         .type           = V4L2_CTRL_TYPE_MENU,
803         .name           = "Flash Control",
804         .minimum        = 0,
805         .maximum        = 3,
806         .step           = 1,
807         .default_value = 0,
808     },
809         #endif
810 };
811
812 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
813 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
814 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
815 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
816 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
817 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
818 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
819 static int sensor_resume(struct soc_camera_device *icd);
820 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
821 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
822 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
823 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
824 static int sensor_deactivate(struct i2c_client *client);
825
826 static struct soc_camera_ops sensor_ops =
827 {
828     .suspend                     = sensor_suspend,
829     .resume                       = sensor_resume,
830     .set_bus_param              = sensor_set_bus_param,
831     .query_bus_param    = sensor_query_bus_param,
832     .controls           = sensor_controls,
833     .menus                         = sensor_menus,
834     .num_controls               = ARRAY_SIZE(sensor_controls),
835     .num_menus          = ARRAY_SIZE(sensor_menus),
836 };
837
838 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
839         { .name = _name, .depth = _depth, .fourcc = _fourcc, \
840         .colorspace = _colorspace }
841
842 #define JPG_FMT(_name, _depth, _fourcc) \
843         COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
844
845 static const struct soc_camera_data_format sensor_colour_formats[] = {
846         JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
847         JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
848 };
849
850 typedef struct sensor_info_priv_s
851 {
852     int whiteBalance;
853     int brightness;
854     int contrast;
855     int saturation;
856     int effect;
857     int scene;
858     int digitalzoom;
859     int focus;
860     int flash;
861     int exposure;
862         bool snap2preview;
863         bool video2preview;
864     unsigned char mirror;                                        /* HFLIP */
865     unsigned char flip;                                          /* VFLIP */
866     unsigned int winseqe_cur_addr;
867         unsigned int pixfmt;
868
869 } sensor_info_priv_t;
870
871 struct sensor
872 {
873     struct v4l2_subdev subdev;
874     struct i2c_client *client;
875     sensor_info_priv_t info_priv;
876     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
877 #if CONFIG_SENSOR_I2C_NOSCHED
878         atomic_t tasklock_cnt;
879 #endif
880         struct rk29camera_platform_data *sensor_io_request;
881     struct rk29camera_gpio_res *sensor_gpio_res;
882 };
883
884 static struct sensor* to_sensor(const struct i2c_client *client)
885 {
886     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
887 }
888
889 static int sensor_task_lock(struct i2c_client *client, int lock)
890 {
891 #if CONFIG_SENSOR_I2C_NOSCHED
892         int cnt = 3;
893     struct sensor *sensor = to_sensor(client);
894
895         if (lock) {
896                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
897                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
898                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
899                                 msleep(35);
900                                 cnt--;
901                         }
902                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
903                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
904                                 goto sensor_task_lock_err;
905                         }
906                         preempt_disable();
907                 }
908
909                 atomic_add(1, &sensor->tasklock_cnt);
910         } else {
911                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
912                         atomic_sub(1, &sensor->tasklock_cnt);
913
914                         if (atomic_read(&sensor->tasklock_cnt) == 0)
915                                 preempt_enable();
916                 }
917         }
918 #endif
919         return 0;
920 sensor_task_lock_err:
921         return -1;
922 }
923
924 #if 0
925 /* sensor register */
926 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
927 {
928         int ret = 0;
929
930         ret = i2c_master_reg8_recv(client, reg, val, 1,  CONFIG_SENSOR_I2C_SPEED);
931
932         return (ret > 0)? 0 : ret;
933 }
934
935 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
936 {
937         int ret = 0;
938         
939         ret = i2c_master_reg8_send(client, reg, &val, 1, CONFIG_SENSOR_I2C_SPEED);
940
941         return (ret > 0)? 0 : ret;
942 }
943 #else
944 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
945 {
946     int err,cnt;
947     u8 buf[2];
948     struct i2c_msg msg[1];
949
950     buf[0] = reg;
951     buf[1] = val;
952
953         if (reg == 0xfe)
954                 mdelay(10);
955         
956     msg->addr = client->addr;
957     msg->flags = client->flags;
958     msg->buf = buf;
959     msg->len = sizeof(buf);
960     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
961     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
962
963     cnt = 3;
964     err = -EAGAIN;
965
966     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
967         err = i2c_transfer(client->adapter, msg, 1);
968
969         if (err >= 0) {
970             return 0;
971         } else {
972             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
973             udelay(10);
974         }
975     }
976
977     return err;
978 }
979
980 /* sensor register read */
981 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
982 {
983     int err,cnt;
984     u8 buf[1];
985     struct i2c_msg msg[2];
986
987     buf[0] = reg ;
988
989     msg[0].addr = client->addr;
990     msg[0].flags = client->flags;
991     msg[0].buf = buf;
992     msg[0].len = sizeof(buf);
993     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
994     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
995
996     msg[1].addr = client->addr;
997     msg[1].flags = client->flags|I2C_M_RD;
998     msg[1].buf = buf;
999     msg[1].len = 1;
1000     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1001     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1002
1003     cnt = 3;
1004     err = -EAGAIN;
1005     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1006         err = i2c_transfer(client->adapter, msg, 2);
1007
1008         if (err >= 0) {
1009             *val = buf[0];
1010             return 0;
1011         } else {
1012                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1013             udelay(10);
1014         }
1015     }
1016
1017     return err;
1018 }
1019
1020 #endif
1021
1022 /* write a array of registers  */
1023 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1024 {
1025     int err = 0, cnt;
1026     int i = 0;
1027         int j = 0;
1028         char valchk;
1029
1030         cnt = 0;
1031         if (sensor_task_lock(client, 1) < 0)
1032                 goto sensor_write_array_end;
1033     while (regarray[i].reg != 0)
1034     {
1035         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1036         if (err < 0)
1037         {
1038             if (cnt-- > 0) {
1039                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1040                                 i = 0;
1041                                 continue;
1042             } else {
1043                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1044                 err = -EPERM;
1045                                 goto sensor_write_array_end;
1046             }
1047         } else {
1048         #if CONFIG_SENSOR_I2C_RDWRCHK
1049                         //mdelay(5);
1050                         sensor_read(client, regarray[i].reg, &valchk);
1051                         if (valchk != regarray[i].val)
1052                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1053                 #endif
1054         }
1055         i++;
1056     }
1057
1058 sensor_write_array_end:
1059         sensor_task_lock(client,0);
1060         return err;
1061 }
1062 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1063 {
1064     int cnt;
1065     int i = 0;
1066         char valchk;
1067
1068         cnt = 0;
1069         valchk = 0;
1070     while (regarray[i].reg != 0)
1071     {
1072                 sensor_read(client, regarray[i].reg, &valchk);
1073                 if (valchk != regarray[i].val)
1074                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1075
1076         i++;
1077     }
1078     return 0;
1079 }
1080 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1081 {
1082         struct soc_camera_link *icl = to_soc_camera_link(icd);
1083         int ret = 0;
1084
1085     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1086         switch (cmd)
1087         {
1088                 case Sensor_PowerDown:
1089                 {
1090                         if (icl->powerdown) {
1091                                 ret = icl->powerdown(icd->pdev, on);
1092                                 if (ret == RK29_CAM_IO_SUCCESS) {
1093                                         if (on == 0) {
1094                                                 mdelay(2);
1095                                                 if (icl->reset)
1096                                                         icl->reset(icd->pdev);
1097                                         }
1098                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1099                                         ret = -ENODEV;
1100                                         goto sensor_power_end;
1101                                 }
1102                         }
1103                         break;
1104                 }
1105                 case Sensor_Flash:
1106                 {
1107                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1108                 struct sensor *sensor = to_sensor(client);
1109
1110                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1111                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1112                         }
1113             break;
1114                 }
1115                 default:
1116                 {
1117                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1118                         break;
1119                 }
1120         }
1121 sensor_power_end:
1122         return ret;
1123 }
1124 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1125 {
1126     struct i2c_client *client = sd->priv;
1127     struct soc_camera_device *icd = client->dev.platform_data;
1128     struct sensor *sensor = to_sensor(client);
1129         const struct v4l2_queryctrl *qctrl;
1130     char value;
1131     int ret;
1132
1133     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1134
1135         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1136                 ret = -ENODEV;
1137                 goto sensor_INIT_ERR;
1138         }
1139
1140     /* soft reset */
1141         if (sensor_task_lock(client,1)<0)
1142                 goto sensor_INIT_ERR;
1143     ret = sensor_write(client, 0xfe, 0x80);
1144     if (ret != 0)
1145     {
1146         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1147         ret = -ENODEV;
1148                 goto sensor_INIT_ERR;
1149     }
1150
1151     mdelay(5);  //delay 5 microseconds
1152         /* check if it is an sensor sensor */
1153     ret = sensor_read(client, 0x00, &value);
1154     if (ret != 0) {
1155         SENSOR_TR("read chip id high byte failed\n");
1156         ret = -ENODEV;
1157         goto sensor_INIT_ERR;
1158     }
1159         
1160     if (value == SENSOR_ID) {
1161         sensor->model = SENSOR_V4L2_IDENT;
1162     } else {
1163         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), value);
1164         ret = -ENODEV;
1165         goto sensor_INIT_ERR;
1166     }
1167
1168     ret = sensor_write_array(client, sensor_init_data);
1169     if (ret != 0)
1170     {
1171         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1172         goto sensor_INIT_ERR;
1173     }
1174         sensor_task_lock(client,0);
1175     //icd->user_width = SENSOR_INIT_WIDTH;
1176     //icd->user_height = SENSOR_INIT_HEIGHT;
1177     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
1178         sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
1179
1180     /* sensor sensor information for initialization  */
1181         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1182         if (qctrl)
1183         sensor->info_priv.whiteBalance = qctrl->default_value;
1184         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1185         if (qctrl)
1186         sensor->info_priv.brightness = qctrl->default_value;
1187         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1188         if (qctrl)
1189         sensor->info_priv.effect = qctrl->default_value;
1190         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1191         if (qctrl)
1192         sensor->info_priv.exposure = qctrl->default_value;
1193
1194         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1195         if (qctrl)
1196         sensor->info_priv.saturation = qctrl->default_value;
1197         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1198         if (qctrl)
1199         sensor->info_priv.contrast = qctrl->default_value;
1200         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1201         if (qctrl)
1202         sensor->info_priv.mirror = qctrl->default_value;
1203         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1204         if (qctrl)
1205         sensor->info_priv.flip = qctrl->default_value;
1206         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1207         if (qctrl)
1208         sensor->info_priv.scene = qctrl->default_value;
1209         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1210         if (qctrl)
1211         sensor->info_priv.digitalzoom = qctrl->default_value;
1212
1213     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
1214         #if CONFIG_SENSOR_Focus
1215     sensor_set_focus();
1216     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1217         if (qctrl)
1218         sensor->info_priv.focus = qctrl->default_value;
1219         #endif
1220
1221         #if CONFIG_SENSOR_Flash 
1222         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1223         if (qctrl)
1224         sensor->info_priv.flash = qctrl->default_value;
1225     #endif
1226
1227     SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),((val == 0)?__FUNCTION__:"sensor_reinit"),icd->user_width,icd->user_height);
1228
1229     return 0;
1230 sensor_INIT_ERR:
1231         sensor_task_lock(client,0);
1232         sensor_deactivate(client);
1233     return ret;
1234 }
1235
1236 static int sensor_deactivate(struct i2c_client *client)
1237 {
1238         struct soc_camera_device *icd = client->dev.platform_data;
1239
1240         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1241
1242         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1243         sensor_ioctrl(icd, Sensor_PowerDown, 1);
1244
1245         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1246         icd->user_width = SENSOR_INIT_WIDTH;
1247     icd->user_height = SENSOR_INIT_HEIGHT;
1248         msleep(100);
1249         return 0;
1250 }
1251
1252 static  struct reginfo sensor_power_down_sequence[]=
1253 {
1254     {0x00,0x00}
1255 };
1256 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1257 {
1258     int ret;
1259     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1260
1261     if (pm_msg.event == PM_EVENT_SUSPEND) {
1262         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1263         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1264         if (ret != 0) {
1265             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1266             return ret;
1267         } else {
1268             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1269             if (ret < 0) {
1270                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1271                 return -EINVAL;
1272             }
1273         }
1274     } else {
1275         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1276         return -EINVAL;
1277     }
1278     return 0;
1279 }
1280
1281 static int sensor_resume(struct soc_camera_device *icd)
1282 {
1283         int ret;
1284
1285     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1286     if (ret < 0) {
1287                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1288         return -EINVAL;
1289     }
1290
1291         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1292
1293     return 0;
1294
1295 }
1296
1297 static int sensor_set_bus_param(struct soc_camera_device *icd,
1298                                 unsigned long flags)
1299 {
1300
1301     return 0;
1302 }
1303
1304 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1305 {
1306     struct soc_camera_link *icl = to_soc_camera_link(icd);
1307     unsigned long flags = SENSOR_BUS_PARAM;
1308
1309     return soc_camera_apply_sensor_flags(icl, flags);
1310 }
1311
1312 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1313 {
1314     struct i2c_client *client = sd->priv;
1315     struct soc_camera_device *icd = client->dev.platform_data;
1316     struct sensor *sensor = to_sensor(client);
1317     struct v4l2_pix_format *pix = &f->fmt.pix;
1318
1319     pix->width          = icd->user_width;
1320     pix->height         = icd->user_height;
1321     pix->pixelformat    = sensor->info_priv.pixfmt;
1322     pix->field          = V4L2_FIELD_NONE;
1323     pix->colorspace             = V4L2_COLORSPACE_JPEG;
1324
1325     return 0;
1326 }
1327 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
1328 {
1329     bool ret = false;
1330
1331         if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
1332                 ret = true;
1333         } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
1334                 ret = true;
1335         } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
1336                 ret = true;
1337         } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
1338                 ret = true;
1339         } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
1340                 ret = true;
1341         }
1342
1343         if (ret == true)
1344                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1345         return ret;
1346 }
1347
1348 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
1349 {
1350     bool ret = false;
1351
1352         if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
1353                 ret = true;
1354         } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
1355                 ret = true;
1356         }
1357
1358         if (ret == true)
1359                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1360         return ret;
1361 }
1362 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1363 {
1364     struct i2c_client *client = sd->priv;
1365     struct sensor *sensor = to_sensor(client);
1366     struct v4l2_pix_format *pix = &f->fmt.pix;
1367         const struct v4l2_queryctrl *qctrl;
1368         struct soc_camera_device *icd = client->dev.platform_data;
1369     struct reginfo *winseqe_set_addr=NULL;
1370     int ret=0, set_w,set_h;
1371
1372         SENSOR_TR("sensor_s_fmt\n");
1373         if (sensor->info_priv.pixfmt != pix->pixelformat) {
1374                 switch (pix->pixelformat)
1375                 {
1376                         case V4L2_PIX_FMT_YUYV:
1377                         {
1378                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
1379                                 break;
1380                         }
1381                         case V4L2_PIX_FMT_UYVY:
1382                         {
1383                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
1384                                 break;
1385                         }
1386                         default:
1387                                 break;
1388                 }
1389                 if (winseqe_set_addr != NULL) {
1390             sensor_write_array(client, winseqe_set_addr);
1391                         sensor->info_priv.pixfmt = pix->pixelformat;
1392
1393                         SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1394                 } else {
1395                         SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1396                 }
1397         }
1398
1399     set_w = pix->width;
1400     set_h = pix->height;
1401
1402         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1403         {
1404                 winseqe_set_addr = sensor_qcif;
1405         set_w = 176;
1406         set_h = 144;
1407         }
1408         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1409     {
1410         winseqe_set_addr = sensor_qvga;
1411         set_w = 320;
1412         set_h = 240;
1413     }
1414     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1415     {
1416         winseqe_set_addr = sensor_cif;
1417         set_w = 352;
1418         set_h = 288;
1419     }
1420     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1421     {
1422         winseqe_set_addr = sensor_vga;
1423         set_w = 640;
1424         set_h = 480;
1425     }
1426     
1427     else
1428     {
1429         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
1430         set_w = SENSOR_INIT_WIDTH;
1431         set_h = SENSOR_INIT_HEIGHT;
1432                 ret = -1;
1433                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
1434     }
1435
1436     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
1437         #if CONFIG_SENSOR_Flash
1438         if (sensor_fmt_capturechk(sd,f) == true) {      /* ddl@rock-chips.com : Capture */
1439             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1440                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1441                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1442             }           
1443         } else {                                        /* ddl@rock-chips.com : Video */
1444             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1445                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1446                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1447             }
1448         }
1449         #endif
1450         ret |= sensor_write_array(client, winseqe_set_addr);
1451         if (ret != 0) {
1452             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1453             #if CONFIG_SENSOR_Flash
1454             if (sensor_fmt_capturechk(sd,f) == true) {
1455                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1456                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1457                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1458                 }
1459             }
1460             #endif
1461             goto sensor_s_fmt_end;
1462         }
1463
1464         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
1465
1466                 if (sensor_fmt_capturechk(sd,f) == true) {                                  /* ddl@rock-chips.com : Capture */
1467                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1468                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1469                         if (sensor->info_priv.whiteBalance != 0) {
1470                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1471                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1472                         }
1473                         sensor->info_priv.snap2preview = true;
1474                 } else if (sensor_fmt_videochk(sd,f) == true) {                 /* ddl@rock-chips.com : Video */
1475                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1476                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1477                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1478                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1479                         sensor->info_priv.video2preview = true;
1480                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1481                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1482                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1483                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1484                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1485                         sensor->info_priv.video2preview = false;
1486                         sensor->info_priv.snap2preview = false;
1487                 }
1488         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1489     }
1490     else
1491     {
1492         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1493     }
1494
1495         pix->width = set_w;
1496     pix->height = set_h;
1497
1498 sensor_s_fmt_end:
1499     return ret;
1500 }
1501
1502 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1503 {
1504     struct v4l2_pix_format *pix = &f->fmt.pix;
1505     bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
1506         pix->pixelformat == V4L2_PIX_FMT_YUYV;
1507
1508     /*
1509     * With Bayer format enforce even side lengths, but let the user play
1510     * with the starting pixel
1511     */
1512
1513     if (pix->height > SENSOR_MAX_HEIGHT)
1514         pix->height = SENSOR_MAX_HEIGHT;
1515     else if (pix->height < SENSOR_MIN_HEIGHT)
1516         pix->height = SENSOR_MIN_HEIGHT;
1517     else if (bayer)
1518         pix->height = ALIGN(pix->height, 2);
1519
1520     if (pix->width > SENSOR_MAX_WIDTH)
1521         pix->width = SENSOR_MAX_WIDTH;
1522     else if (pix->width < SENSOR_MIN_WIDTH)
1523         pix->width = SENSOR_MIN_WIDTH;
1524     else if (bayer)
1525         pix->width = ALIGN(pix->width, 2);
1526
1527     return 0;
1528 }
1529
1530  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1531 {
1532     struct i2c_client *client = sd->priv;
1533
1534     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1535         return -EINVAL;
1536
1537     if (id->match.addr != client->addr)
1538         return -ENODEV;
1539
1540     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
1541     id->revision = 0;
1542
1543     return 0;
1544 }
1545 #if CONFIG_SENSOR_Brightness
1546 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1547 {
1548     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1549
1550     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1551     {
1552         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1553         {
1554             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1555             {
1556                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1557                 return -EINVAL;
1558             }
1559             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1560             return 0;
1561         }
1562     }
1563         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1564     return -EINVAL;
1565 }
1566 #endif
1567 #if CONFIG_SENSOR_Effect
1568 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1569 {
1570     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1571
1572     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1573     {
1574         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1575         {
1576             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1577             {
1578                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1579                 return -EINVAL;
1580             }
1581             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1582             return 0;
1583         }
1584     }
1585         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1586     return -EINVAL;
1587 }
1588 #endif
1589 #if CONFIG_SENSOR_Exposure
1590 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1591 {
1592     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1593
1594     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1595     {
1596         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1597         {
1598             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1599             {
1600                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1601                 return -EINVAL;
1602             }
1603             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1604             return 0;
1605         }
1606     }
1607         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1608     return -EINVAL;
1609 }
1610 #endif
1611 #if CONFIG_SENSOR_Saturation
1612 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1613 {
1614     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1615
1616     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1617     {
1618         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1619         {
1620             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1621             {
1622                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1623                 return -EINVAL;
1624             }
1625             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1626             return 0;
1627         }
1628     }
1629     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1630     return -EINVAL;
1631 }
1632 #endif
1633 #if CONFIG_SENSOR_Contrast
1634 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1635 {
1636     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1637
1638     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1639     {
1640         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1641         {
1642             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1643             {
1644                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1645                 return -EINVAL;
1646             }
1647             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1648             return 0;
1649         }
1650     }
1651     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1652     return -EINVAL;
1653 }
1654 #endif
1655 #if CONFIG_SENSOR_Mirror
1656 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1657 {
1658     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1659
1660     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1661     {
1662         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1663         {
1664             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1665             {
1666                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1667                 return -EINVAL;
1668             }
1669             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1670             return 0;
1671         }
1672     }
1673     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1674     return -EINVAL;
1675 }
1676 #endif
1677 #if CONFIG_SENSOR_Flip
1678 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1679 {
1680     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1681
1682     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1683     {
1684         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1685         {
1686             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1687             {
1688                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1689                 return -EINVAL;
1690             }
1691             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1692             return 0;
1693         }
1694     }
1695     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1696     return -EINVAL;
1697 }
1698 #endif
1699 #if CONFIG_SENSOR_Scene
1700 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1701 {
1702     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1703
1704     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1705     {
1706         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1707         {
1708             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1709             {
1710                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1711                 return -EINVAL;
1712             }
1713             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1714             return 0;
1715         }
1716     }
1717     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1718     return -EINVAL;
1719 }
1720 #endif
1721 #if CONFIG_SENSOR_WhiteBalance
1722 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1723 {
1724     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1725
1726     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1727     {
1728         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
1729         {
1730             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
1731             {
1732                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1733                 return -EINVAL;
1734             }
1735             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1736             return 0;
1737         }
1738     }
1739         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1740     return -EINVAL;
1741 }
1742 #endif
1743 #if CONFIG_SENSOR_DigitalZoom
1744 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
1745 {
1746     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1747     struct sensor *sensor = to_sensor(client);
1748         const struct v4l2_queryctrl *qctrl_info;
1749     int digitalzoom_cur, digitalzoom_total;
1750
1751         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1752         if (qctrl_info)
1753                 return -EINVAL;
1754
1755     digitalzoom_cur = sensor->info_priv.digitalzoom;
1756     digitalzoom_total = qctrl_info->maximum;
1757
1758     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
1759     {
1760         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1761         return -EINVAL;
1762     }
1763
1764     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
1765     {
1766         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1767         return -EINVAL;
1768     }
1769
1770     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
1771     {
1772         *value = digitalzoom_total - digitalzoom_cur;
1773     }
1774
1775     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
1776     {
1777         *value = 0 - digitalzoom_cur;
1778     }
1779
1780     digitalzoom_cur += *value;
1781
1782     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
1783     {
1784         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
1785         {
1786             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1787             return -EINVAL;
1788         }
1789         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
1790         return 0;
1791     }
1792
1793     return -EINVAL;
1794 }
1795 #endif
1796 #if CONFIG_SENSOR_Flash
1797 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1798 {    
1799     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
1800         if (value == 3) {       /* ddl@rock-chips.com: torch */
1801             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
1802         } else {
1803             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1804         }
1805         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1806         return 0;
1807     }
1808     
1809         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1810     return -EINVAL;
1811 }
1812 #endif
1813
1814 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1815 {
1816     struct i2c_client *client = sd->priv;
1817     struct sensor *sensor = to_sensor(client);
1818     const struct v4l2_queryctrl *qctrl;
1819
1820     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
1821
1822     if (!qctrl)
1823     {
1824         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
1825         return -EINVAL;
1826     }
1827
1828     switch (ctrl->id)
1829     {
1830         case V4L2_CID_BRIGHTNESS:
1831             {
1832                 ctrl->value = sensor->info_priv.brightness;
1833                 break;
1834             }
1835         case V4L2_CID_SATURATION:
1836             {
1837                 ctrl->value = sensor->info_priv.saturation;
1838                 break;
1839             }
1840         case V4L2_CID_CONTRAST:
1841             {
1842                 ctrl->value = sensor->info_priv.contrast;
1843                 break;
1844             }
1845         case V4L2_CID_DO_WHITE_BALANCE:
1846             {
1847                 ctrl->value = sensor->info_priv.whiteBalance;
1848                 break;
1849             }
1850         case V4L2_CID_EXPOSURE:
1851             {
1852                 ctrl->value = sensor->info_priv.exposure;
1853                 break;
1854             }
1855         case V4L2_CID_HFLIP:
1856             {
1857                 ctrl->value = sensor->info_priv.mirror;
1858                 break;
1859             }
1860         case V4L2_CID_VFLIP:
1861             {
1862                 ctrl->value = sensor->info_priv.flip;
1863                 break;
1864             }
1865         default :
1866                 break;
1867     }
1868     return 0;
1869 }
1870
1871
1872
1873 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1874 {
1875     struct i2c_client *client = sd->priv;
1876     struct sensor *sensor = to_sensor(client);
1877     struct soc_camera_device *icd = client->dev.platform_data;
1878     const struct v4l2_queryctrl *qctrl;
1879
1880
1881     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
1882
1883     if (!qctrl)
1884     {
1885         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
1886         return -EINVAL;
1887     }
1888
1889     switch (ctrl->id)
1890     {
1891 #if CONFIG_SENSOR_Brightness
1892         case V4L2_CID_BRIGHTNESS:
1893             {
1894                 if (ctrl->value != sensor->info_priv.brightness)
1895                 {
1896                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
1897                     {
1898                         return -EINVAL;
1899                     }
1900                     sensor->info_priv.brightness = ctrl->value;
1901                 }
1902                 break;
1903             }
1904 #endif
1905 #if CONFIG_SENSOR_Exposure
1906         case V4L2_CID_EXPOSURE:
1907             {
1908                 if (ctrl->value != sensor->info_priv.exposure)
1909                 {
1910                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
1911                     {
1912                         return -EINVAL;
1913                     }
1914                     sensor->info_priv.exposure = ctrl->value;
1915                 }
1916                 break;
1917             }
1918 #endif
1919 #if CONFIG_SENSOR_Saturation
1920         case V4L2_CID_SATURATION:
1921             {
1922                 if (ctrl->value != sensor->info_priv.saturation)
1923                 {
1924                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
1925                     {
1926                         return -EINVAL;
1927                     }
1928                     sensor->info_priv.saturation = ctrl->value;
1929                 }
1930                 break;
1931             }
1932 #endif
1933 #if CONFIG_SENSOR_Contrast
1934         case V4L2_CID_CONTRAST:
1935             {
1936                 if (ctrl->value != sensor->info_priv.contrast)
1937                 {
1938                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
1939                     {
1940                         return -EINVAL;
1941                     }
1942                     sensor->info_priv.contrast = ctrl->value;
1943                 }
1944                 break;
1945             }
1946 #endif
1947 #if CONFIG_SENSOR_WhiteBalance
1948         case V4L2_CID_DO_WHITE_BALANCE:
1949             {
1950                 if (ctrl->value != sensor->info_priv.whiteBalance)
1951                 {
1952                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
1953                     {
1954                         return -EINVAL;
1955                     }
1956                     sensor->info_priv.whiteBalance = ctrl->value;
1957                 }
1958                 break;
1959             }
1960 #endif
1961 #if CONFIG_SENSOR_Mirror
1962         case V4L2_CID_HFLIP:
1963             {
1964                 if (ctrl->value != sensor->info_priv.mirror)
1965                 {
1966                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
1967                         return -EINVAL;
1968                     sensor->info_priv.mirror = ctrl->value;
1969                 }
1970                 break;
1971             }
1972 #endif
1973 #if CONFIG_SENSOR_Flip
1974         case V4L2_CID_VFLIP:
1975             {
1976                 if (ctrl->value != sensor->info_priv.flip)
1977                 {
1978                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
1979                         return -EINVAL;
1980                     sensor->info_priv.flip = ctrl->value;
1981                 }
1982                 break;
1983             }
1984 #endif
1985         default:
1986             break;
1987     }
1988
1989     return 0;
1990 }
1991 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
1992 {
1993     const struct v4l2_queryctrl *qctrl;
1994     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1995     struct sensor *sensor = to_sensor(client);
1996
1997     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
1998
1999     if (!qctrl)
2000     {
2001         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2002         return -EINVAL;
2003     }
2004
2005     switch (ext_ctrl->id)
2006     {
2007         case V4L2_CID_SCENE:
2008             {
2009                 ext_ctrl->value = sensor->info_priv.scene;
2010                 break;
2011             }
2012         case V4L2_CID_EFFECT:
2013             {
2014                 ext_ctrl->value = sensor->info_priv.effect;
2015                 break;
2016             }
2017         case V4L2_CID_ZOOM_ABSOLUTE:
2018             {
2019                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2020                 break;
2021             }
2022         case V4L2_CID_ZOOM_RELATIVE:
2023             {
2024                 return -EINVAL;
2025             }
2026         case V4L2_CID_FOCUS_ABSOLUTE:
2027             {
2028                 ext_ctrl->value = sensor->info_priv.focus;
2029                 break;
2030             }
2031         case V4L2_CID_FOCUS_RELATIVE:
2032             {
2033                 return -EINVAL;
2034             }
2035         case V4L2_CID_FLASH:
2036             {
2037                 ext_ctrl->value = sensor->info_priv.flash;
2038                 break;
2039             }
2040         default :
2041             break;
2042     }
2043     return 0;
2044 }
2045 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2046 {
2047     const struct v4l2_queryctrl *qctrl;
2048     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2049     struct sensor *sensor = to_sensor(client);
2050     int val_offset;
2051
2052     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2053
2054     if (!qctrl)
2055     {
2056         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2057         return -EINVAL;
2058     }
2059
2060         val_offset = 0;
2061     switch (ext_ctrl->id)
2062     {
2063 #if CONFIG_SENSOR_Scene
2064         case V4L2_CID_SCENE:
2065             {
2066                 if (ext_ctrl->value != sensor->info_priv.scene)
2067                 {
2068                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2069                         return -EINVAL;
2070                     sensor->info_priv.scene = ext_ctrl->value;
2071                 }
2072                 break;
2073             }
2074 #endif
2075 #if CONFIG_SENSOR_Effect
2076         case V4L2_CID_EFFECT:
2077             {
2078                 if (ext_ctrl->value != sensor->info_priv.effect)
2079                 {
2080                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2081                         return -EINVAL;
2082                     sensor->info_priv.effect= ext_ctrl->value;
2083                 }
2084                 break;
2085             }
2086 #endif
2087 #if CONFIG_SENSOR_DigitalZoom
2088         case V4L2_CID_ZOOM_ABSOLUTE:
2089             {
2090                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2091                     return -EINVAL;
2092
2093                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2094                 {
2095                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2096
2097                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2098                         return -EINVAL;
2099                     sensor->info_priv.digitalzoom += val_offset;
2100
2101                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2102                 }
2103
2104                 break;
2105             }
2106         case V4L2_CID_ZOOM_RELATIVE:
2107             {
2108                 if (ext_ctrl->value)
2109                 {
2110                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2111                         return -EINVAL;
2112                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2113
2114                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2115                 }
2116                 break;
2117             }
2118 #endif
2119 #if CONFIG_SENSOR_Focus
2120         case V4L2_CID_FOCUS_ABSOLUTE:
2121             {
2122                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2123                     return -EINVAL;
2124
2125                 if (ext_ctrl->value != sensor->info_priv.focus)
2126                 {
2127                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2128
2129                     sensor->info_priv.focus += val_offset;
2130                 }
2131
2132                 break;
2133             }
2134         case V4L2_CID_FOCUS_RELATIVE:
2135             {
2136                 if (ext_ctrl->value)
2137                 {
2138                     sensor->info_priv.focus += ext_ctrl->value;
2139
2140                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2141                 }
2142                 break;
2143             }
2144 #endif
2145 #if CONFIG_SENSOR_Flash
2146         case V4L2_CID_FLASH:
2147             {
2148                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2149                     return -EINVAL;
2150                 sensor->info_priv.flash = ext_ctrl->value;
2151
2152                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2153                 break;
2154             }
2155 #endif
2156         default:
2157             break;
2158     }
2159
2160     return 0;
2161 }
2162
2163 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2164 {
2165     struct i2c_client *client = sd->priv;
2166     struct soc_camera_device *icd = client->dev.platform_data;
2167     int i, error_cnt=0, error_idx=-1;
2168
2169
2170     for (i=0; i<ext_ctrl->count; i++) {
2171         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2172             error_cnt++;
2173             error_idx = i;
2174         }
2175     }
2176
2177     if (error_cnt > 1)
2178         error_idx = ext_ctrl->count;
2179
2180     if (error_idx != -1) {
2181         ext_ctrl->error_idx = error_idx;
2182         return -EINVAL;
2183     } else {
2184         return 0;
2185     }
2186 }
2187
2188 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2189 {
2190     struct i2c_client *client = sd->priv;
2191     struct soc_camera_device *icd = client->dev.platform_data;
2192     int i, error_cnt=0, error_idx=-1;
2193
2194
2195     for (i=0; i<ext_ctrl->count; i++) {
2196         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2197             error_cnt++;
2198             error_idx = i;
2199         }
2200     }
2201
2202     if (error_cnt > 1)
2203         error_idx = ext_ctrl->count;
2204
2205     if (error_idx != -1) {
2206         ext_ctrl->error_idx = error_idx;
2207         return -EINVAL;
2208     } else {
2209         return 0;
2210     }
2211 }
2212
2213 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2214  * this wasn't our capture interface, so, we wait for the right one */
2215 static int sensor_video_probe(struct soc_camera_device *icd,
2216                                struct i2c_client *client)
2217 {
2218     char value;
2219     int ret;
2220     struct sensor *sensor = to_sensor(client);
2221
2222     /* We must have a parent by now. And it cannot be a wrong one.
2223      * So this entire test is completely redundant. */
2224     if (!icd->dev.parent ||
2225             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2226                 return -ENODEV;
2227
2228         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2229                 ret = -ENODEV;
2230                 goto sensor_video_probe_err;
2231         }
2232
2233     /* soft reset */
2234     ret = sensor_write(client, 0xfe, 0x80);
2235     if (ret != 0)
2236     {
2237         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2238         return -ENODEV;
2239     }
2240     mdelay(5);          //delay 5 microseconds
2241
2242     /* check if it is an sensor sensor */
2243     ret = sensor_read(client, 0x00, &value);
2244     if (ret != 0) {
2245         SENSOR_TR("read chip id high byte failed\n");
2246         ret = -ENODEV;
2247         goto sensor_video_probe_err;
2248     }
2249
2250     if (value == SENSOR_ID) {
2251         sensor->model = SENSOR_V4L2_IDENT;
2252                 SENSOR_TR("chip id:0x%x\n",value);
2253     } else {
2254         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), value);
2255         ret = -ENODEV;
2256         goto sensor_video_probe_err;
2257     }
2258
2259     icd->formats = sensor_colour_formats;
2260     icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
2261
2262     return 0;
2263
2264 sensor_video_probe_err:
2265
2266     return ret;
2267 }
2268 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2269 {
2270         struct i2c_client *client = sd->priv;
2271     struct soc_camera_device *icd = client->dev.platform_data;
2272     struct sensor *sensor = to_sensor(client);
2273     int ret = 0;
2274 #if CONFIG_SENSOR_Flash 
2275     int i;
2276 #endif
2277     
2278         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2279         switch (cmd)
2280         {
2281                 case RK29_CAM_SUBDEV_DEACTIVATE:
2282                 {
2283                         sensor_deactivate(client);
2284                         break;
2285                 }
2286
2287                 case RK29_CAM_SUBDEV_IOREQUEST:
2288                 {
2289                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2290             if (sensor->sensor_io_request != NULL) { 
2291                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
2292                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2293                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2294                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
2295                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2296                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2297                 }
2298             } else {
2299                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2300                 ret = -EINVAL;
2301                 goto sensor_ioctl_end;
2302             }
2303             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2304                for this project */
2305             #if CONFIG_SENSOR_Flash     
2306                 if (sensor->sensor_gpio_res) { 
2307                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2308                     for (i = 0; i < icd->ops->num_controls; i++) {
2309                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2310                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2311                                 }
2312                     }
2313                     sensor->info_priv.flash = 0xff;
2314                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2315                 }
2316                 }
2317             #endif
2318                         break;
2319                 }
2320                 default:
2321                 {
2322                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2323                         break;
2324                 }
2325         }
2326 sensor_ioctl_end:
2327         return ret;
2328
2329 }
2330 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2331         .init           = sensor_init,
2332         .g_ctrl         = sensor_g_control,
2333         .s_ctrl         = sensor_s_control,
2334         .g_ext_ctrls          = sensor_g_ext_controls,
2335         .s_ext_ctrls          = sensor_s_ext_controls,
2336         .g_chip_ident   = sensor_g_chip_ident,
2337         .ioctl = sensor_ioctl,
2338 };
2339
2340 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2341         .s_fmt          = sensor_s_fmt,
2342         .g_fmt          = sensor_g_fmt,
2343         .try_fmt        = sensor_try_fmt,
2344 };
2345
2346 static struct v4l2_subdev_ops sensor_subdev_ops = {
2347         .core   = &sensor_subdev_core_ops,
2348         .video = &sensor_subdev_video_ops,
2349 };
2350
2351 static int sensor_probe(struct i2c_client *client,
2352                          const struct i2c_device_id *did)
2353 {
2354     struct sensor *sensor;
2355     struct soc_camera_device *icd = client->dev.platform_data;
2356     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2357     struct soc_camera_link *icl;
2358     int ret;
2359
2360     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2361     if (!icd) {
2362         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2363         return -EINVAL;
2364     }
2365
2366     icl = to_soc_camera_link(icd);
2367     if (!icl) {
2368         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2369         return -EINVAL;
2370     }
2371
2372     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2373         dev_warn(&adapter->dev,
2374                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2375         return -EIO;
2376     }
2377
2378     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2379     if (!sensor)
2380         return -ENOMEM;
2381
2382     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2383
2384     /* Second stage probe - when a capture adapter is there */
2385     icd->ops            = &sensor_ops;
2386     icd->y_skip_top             = 0;
2387         #if CONFIG_SENSOR_I2C_NOSCHED
2388         atomic_set(&sensor->tasklock_cnt,0);
2389         #endif
2390
2391     ret = sensor_video_probe(icd, client);
2392     if (ret < 0) {
2393         icd->ops = NULL;
2394         i2c_set_clientdata(client, NULL);
2395         kfree(sensor);
2396                 sensor = NULL;
2397     }
2398     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2399     return ret;
2400 }
2401
2402 static int sensor_remove(struct i2c_client *client)
2403 {
2404     struct sensor *sensor = to_sensor(client);
2405     struct soc_camera_device *icd = client->dev.platform_data;
2406
2407     icd->ops = NULL;
2408     i2c_set_clientdata(client, NULL);
2409     client->driver = NULL;
2410     kfree(sensor);
2411         sensor = NULL;
2412     return 0;
2413 }
2414
2415 static const struct i2c_device_id sensor_id[] = {
2416         {SENSOR_NAME_STRING(), 0 },
2417         { }
2418 };
2419 MODULE_DEVICE_TABLE(i2c, sensor_id);
2420
2421 static struct i2c_driver sensor_i2c_driver = {
2422         .driver = {
2423                 .name = SENSOR_NAME_STRING(),
2424         },
2425         .probe          = sensor_probe,
2426         .remove         = sensor_remove,
2427         .id_table       = sensor_id,
2428 };
2429
2430 static int __init sensor_mod_init(void)
2431 {
2432     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2433     return i2c_add_driver(&sensor_i2c_driver);
2434 }
2435
2436 static void __exit sensor_mod_exit(void)
2437 {
2438     i2c_del_driver(&sensor_i2c_driver);
2439 }
2440
2441 device_initcall_sync(sensor_mod_init);
2442 module_exit(sensor_mod_exit);
2443
2444 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2445 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2446 MODULE_LICENSE("GPL");
2447
2448
2449