Merge remote-tracking branch 'origin/develop-3.0-rk30' into develop-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / ov2640_rk.c
1 #include <linux/videodev2.h>\r
2 #include <linux/slab.h>
3 #include <linux/i2c.h>
4 #include <linux/log2.h>
5 #include <linux/platform_device.h>
6 #include <linux/delay.h>
7 #include <linux/circ_buf.h>
8 #include <linux/miscdevice.h>
9 #include <media/v4l2-common.h>
10 #include <media/v4l2-chip-ident.h>
11 #include <media/soc_camera.h>
12 #include <mach/rk29_camera.h>
13
14 static int debug;
15 module_param(debug, int, S_IRUGO|S_IWUSR);
16
17 #define dprintk(level, fmt, arg...) do {                        \
18         if (debug >= level)                                     \
19         printk(KERN_WARNING fmt , ## arg); } while (0)
20
21 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
22 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
23
24
25 #define _CONS(a,b) a##b
26 #define CONS(a,b) _CONS(a,b)
27
28 #define __STR(x) #x
29 #define _STR(x) __STR(x)
30 #define STR(x) _STR(x)
31
32 #define MIN(x,y)   ((x<y) ? x: y)
33 #define MAX(x,y)    ((x>y) ? x: y)
34
35 /* Sensor Driver Configuration */
36 #define SENSOR_NAME RK29_CAM_SENSOR_OV2640
37 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV2640
38 #define SENSOR_ID 0x2642
39 #define SENSOR_ID1 0x2641
40 #define SENSOR_MIN_WIDTH    640
41 #define SENSOR_MIN_HEIGHT   480
42 #define SENSOR_MAX_WIDTH    1600
43 #define SENSOR_MAX_HEIGHT   1200
44 #define SENSOR_INIT_WIDTH       640                     /* Sensor pixel size for sensor_init_data array */
45 #define SENSOR_INIT_HEIGHT  480
46 #define SENSOR_INIT_WINSEQADR sensor_vga
47 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_YUYV8_2X8
48
49 #define CONFIG_SENSOR_WhiteBalance      0
50 #define CONFIG_SENSOR_Brightness        1
51 #define CONFIG_SENSOR_Contrast      0
52 #define CONFIG_SENSOR_Saturation    1
53 #define CONFIG_SENSOR_Effect        1
54 #define CONFIG_SENSOR_Scene         0
55 #define CONFIG_SENSOR_DigitalZoom   0
56 #define CONFIG_SENSOR_Focus         0
57 #define CONFIG_SENSOR_Exposure      0
58 #define CONFIG_SENSOR_Flash         0
59 #define CONFIG_SENSOR_Mirror        0
60 #define CONFIG_SENSOR_Flip          0
61
62 #define CONFIG_SENSOR_I2C_SPEED     250000       /* Hz */
63 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
64 #define CONFIG_SENSOR_I2C_NOSCHED   0
65 #define CONFIG_SENSOR_I2C_RDWRCHK   0
66
67 #define SENSOR_BUS_PARAM  (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
68                           SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
69                           SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ)
70
71 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
72 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
73 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
74 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
75 #define COLOR_TEMPERATURE_OFFICE_DN     3500
76 #define COLOR_TEMPERATURE_OFFICE_UP     5000
77 #define COLOR_TEMPERATURE_HOME_DN       2500
78 #define COLOR_TEMPERATURE_HOME_UP       3500
79
80 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
81 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
82
83 struct reginfo
84 {
85     u8 reg;
86     u8 val;
87 };
88
89 /* init 800*600 SVGA */
90 static struct reginfo sensor_init_data[] =
91 {
92 #if 1
93     {0xff,0x01},
94     {0x12,0x80},
95     {0xff,0x00},
96     {0x2c,0xff},
97     {0x2e,0xdf},
98     {0xff,0x01},
99
100     {0x03,0x4f},
101     {0x0f,0x4b},
102
103
104     {0x3c,0x32},
105     {0x11,0x00},
106     {0x09,0x02},
107     {0x04,0xF8},//b7,b6 directs
108     {0x13,0xe5},
109     {0x14,0x48},
110     {0x2c,0x0c},
111     {0x33,0x78},
112     {0x3a,0x33},
113     {0x3b,0xfB},
114     {0x3e,0x00},
115     {0x43,0x11},
116     {0x16,0x10},
117     {0x39,0x02},
118     {0x35,0x88},
119     {0x22,0x09},
120     {0x37,0x40},
121     {0x23,0x00},
122     {0x34,0xa0},
123     {0x36,0x1a},
124     {0x06,0x02},
125     {0x07,0xc0},
126     {0x0d,0xb7},
127     {0x0e,0x01},
128     {0x4c,0x00},
129     {0x4a,0x81},
130     {0x21,0x99},
131     
132     //{0x24,0x58},
133     //{0x25,0x50},
134     //{0x26,0x92},
135     
136     {0x24, 0x70},
137     {0x25, 0x60},
138     {0x26, 0xa4},    
139     
140     {0x5c,0x00},
141     {0x63,0x00},
142     {0x46,0x3f},
143     {0x0c,0x3c},
144     {0x61,0x70},
145     {0x62,0x80},
146     {0x7c,0x05},
147     {0x20,0x80},
148     {0x28,0x30},
149     {0x6c,0x00},
150     {0x6d,0x80},
151     {0x6e,0x00},
152     {0x70,0x02},
153     {0x71,0x94},
154     {0x73,0xc1},
155     {0x3d,0x34},
156     {0x5a,0x57},
157     {0x4f,0xbb},
158     {0x50,0x9c},
159     {0xff,0x00},
160     {0xe5,0x7f},
161     {0xf9,0xc0},
162     {0x41,0x24},
163     {0xe0,0x14},
164     {0x76,0xff},
165     {0x33,0xa0},
166     {0x42,0x20},
167     {0x43,0x18},
168     {0x4c,0x00},
169     {0x87,0xd0},
170     {0x88,0x3f},
171     {0xd7,0x03},
172     {0xd9,0x10},
173     {0xd3,0x82},
174     {0xc8,0x08},
175     {0xc9,0x80},
176     {0x7c,0x00},
177     {0x7d,0x00},//0x00//0x07
178     {0x7c,0x03},
179     {0x7d,0x48},//0x48//0x40
180     {0x7d,0x48},//0x48//0x40
181     {0x7c,0x08},
182     {0x7d,0x20},
183     {0x7d,0x10},//0x10
184     {0x7d,0x0e},//0x0e
185
186     {0x92,0x00},
187     {0x93,0x06},
188     {0x93,0xc8},//e3
189     {0x93,0x05},
190     {0x93,0x05},
191     {0x93,0x00},
192     {0x93,0x04},
193     {0x93,0x00},
194     {0x93,0x00},
195     {0x93,0x00},
196     {0x93,0x00},
197     {0x93,0x00},
198     {0x93,0x00},
199     {0x93,0x00},
200     {0x96,0x00},
201     {0x97,0x08},
202     {0x97,0x19},
203     {0x97,0x02},
204     {0x97,0x0c},
205     {0x97,0x24},
206     {0x97,0x30},
207     {0x97,0x28},
208     {0x97,0x26},
209     {0x97,0x02},
210     {0x97,0x98},
211     {0x97,0x80},
212     {0x97,0x00},
213     {0x97,0x00},
214     {0xc3,0xef},//ed
215     {0xa4,0x00},
216     {0xa8,0x00},
217
218     {0xbf, 0x00},
219     {0xba, 0xdc},
220     {0xbb, 0x08},
221     {0xb6, 0x20},
222     {0xb8, 0x30},
223     {0xb7, 0x20},
224     {0xb9, 0x30},
225     {0xb3, 0xb4},
226     {0xb4, 0xca},
227     {0xb5, 0x34},
228     {0xb0, 0x46},
229     {0xb1, 0x46},
230     {0xb2, 0x06},
231     {0xc7, 0x00},
232     {0xc6, 0x51},
233     {0xc5, 0x11},
234     {0xc4, 0x9c},
235 ////
236     {0xc0,0xc8},
237     {0xc1,0x96},
238     {0x86,0x3d},
239     {0x50,0x92},
240     {0x51,0x90},
241     {0x52,0x2c},
242     {0x53,0x00},
243     {0x54,0x00},
244     {0x55,0x88},
245     {0x57,0x00},
246     {0x5a,0x50},
247     {0x5b,0x3c},
248     {0x5c,0x00},
249     {0xc3,0xed},
250     {0x7f,0x00},
251     {0xda,0x01},
252     {0xe5,0x1f},
253     {0xe1,0x67},
254     {0xe0,0x00},
255     {0xdd,0xff},
256     {0x05,0x00},
257
258 #endif
259 #if 1
260     {0xff, 0x01},
261     {0x5d, 0x00},
262     {0x5e, 0x3c},
263     {0x5f, 0x28},
264     {0x60, 0x55},
265
266
267     {0xff, 0x00},
268     {0xc3, 0xef},
269     {0xa6, 0x00},
270     {0xa7, 0x0f},
271     {0xa7, 0x4e},
272     {0xa7, 0x7a},
273     {0xa7, 0x33},
274     {0xa7, 0x00},
275     {0xa7, 0x23},
276     {0xa7, 0x27},
277     {0xa7, 0x3a},
278     {0xa7, 0x70},
279     {0xa7, 0x33},
280     {0xa7, 0x00},//L
281     {0xa7, 0x23},
282     {0xa7, 0x20},
283     {0xa7, 0x0c},
284     {0xa7, 0x66},
285     {0xa7, 0x33},
286     {0xa7, 0x00},
287     {0xa7, 0x23},
288     {0xc3, 0xef},
289 #endif
290
291
292 #if 1
293     {0xff,0x00},
294     {0x92,0x00},
295     {0x93,0x06},
296     {0x93,0xc1},//e
297     {0x93,0x02},
298     {0x93,0x02},
299     {0x93,0x00},
300     {0x93,0x04},
301 #endif
302
303     {0x03, 0x0f},
304     {0xe0, 0x04},
305     {0xc0, 0xc8},
306     {0xc1, 0x96},
307     {0x86, 0x3d},
308     {0x50, 0x89},
309     {0x51, 0x90},
310     {0x52, 0x2c},
311     {0x53, 0x00},
312     {0x54, 0x00},
313     {0x55, 0x88},
314     {0x57, 0x00},
315     {0x5a, 0xa0},
316     {0x5b, 0x78},
317     {0x5c, 0x00},
318     {0xd3, 0x04},
319     {0xe0, 0x00},
320
321   {0x0, 0x0}   //end flag
322
323 };
324
325 /* 1600X1200 UXGA */
326 static struct reginfo sensor_uxga[] =
327 {
328     {0xff, 0x00},
329     {0xe0, 0x04},
330     {0xc0, 0xc8},
331     {0xc1, 0x96},
332     {0x86, 0x3d},
333     {0x50, 0x00},
334     {0x51, 0x90},
335     {0x52, 0x2c},
336     {0x53, 0x00},
337     {0x54, 0x00},
338     {0x55, 0x88},
339     {0x57, 0x00},
340     {0x5a, 0x90},
341     {0x5b, 0x2c},
342     {0x5c, 0x05},
343     {0xd3, 0x82},
344     {0xe0, 0x00},
345   {0x0, 0x0}   //end flag
346 };
347
348 /* 1280X1024 SXGA */
349 static struct reginfo sensor_sxga[] =
350 {
351     {0xff, 0x00},
352     {0xe0, 0x04},
353     {0xc0, 0xc8},
354     {0xc1, 0x96},
355     {0x86, 0x3d},
356     {0x50, 0x00},
357     {0x51, 0x90},
358     {0x52, 0x2c},
359     {0x53, 0x00},
360     {0x54, 0x00},
361     {0x55, 0x88},
362     {0x57, 0x00},
363     {0x5a, 0x40},
364     {0x5b, 0x00},
365     {0x5c, 0x05},
366     {0xd3, 0x82},
367     {0xe0, 0x00},
368   {0x0, 0x0}   //end flag
369 };
370
371
372 static struct reginfo sensor_xga[] =
373 {
374     {0xff, 0x00},
375     {0xe0, 0x04},
376     {0xc0, 0xc8},
377     {0xc1, 0x96},
378     {0x86, 0x3d},
379     {0x50, 0x00},
380     {0x51, 0x90},
381     {0x52, 0x2c},
382     {0x53, 0x00},
383     {0x54, 0x00},
384     {0x55, 0x88},
385     {0x57, 0x00},
386     {0x5a, 0x40},
387     {0x5b, 0x00},
388     {0x5c, 0x05},
389     {0xd3, 0x82},
390     {0xe0, 0x00},
391   {0x0, 0x0}   //end flag
392
393
394 };
395
396
397 /* 800X600 SVGA*/
398 static struct reginfo sensor_svga[] =
399 {
400   {0x0, 0x0}   //end flag
401 };
402
403 /* 640X480 VGA */
404 static struct reginfo sensor_vga[] =
405 {
406    {0x0, 0x0}   //end flag
407  };
408
409 /* 352X288 CIF */
410 static struct reginfo sensor_cif[] =
411 {
412   {0x0, 0x0}   //end flag
413 };
414
415 /* 320*240 QVGA */
416 static  struct reginfo sensor_qvga[] =
417 {
418   {0x0, 0x0}   //end flag
419 };
420
421 /* 176X144 QCIF*/
422 static struct reginfo sensor_qcif[] =
423 {
424   {0x0, 0x0}   //end flag
425 };
426 #if 0
427 /* 160X120 QQVGA*/
428 static struct reginfo ov2655_qqvga[] =
429 {
430
431     {0x300E, 0x34},
432     {0x3011, 0x01},
433     {0x3012, 0x10},
434     {0x302a, 0x02},
435     {0x302b, 0xE6},
436     {0x306f, 0x14},
437     {0x3362, 0x90},
438
439     {0x3070, 0x5d},
440     {0x3072, 0x5d},
441     {0x301c, 0x07},
442     {0x301d, 0x07},
443
444     {0x3020, 0x01},
445     {0x3021, 0x18},
446     {0x3022, 0x00},
447     {0x3023, 0x06},
448     {0x3024, 0x06},
449     {0x3025, 0x58},
450     {0x3026, 0x02},
451     {0x3027, 0x61},
452     {0x3088, 0x00},
453     {0x3089, 0xa0},
454     {0x308a, 0x00},
455     {0x308b, 0x78},
456     {0x3316, 0x64},
457     {0x3317, 0x25},
458     {0x3318, 0x80},
459     {0x3319, 0x08},
460     {0x331a, 0x0a},
461     {0x331b, 0x07},
462     {0x331c, 0x80},
463     {0x331d, 0x38},
464     {0x3100, 0x00},
465     {0x3302, 0x11},
466
467     {0x0, 0x0},
468 };
469
470
471
472 static  struct reginfo ov2655_Sharpness_auto[] =
473 {
474     {0x3306, 0x00},
475 };
476
477 static  struct reginfo ov2655_Sharpness1[] =
478 {
479     {0x3306, 0x08},
480     {0x3371, 0x00},
481 };
482
483 static  struct reginfo ov2655_Sharpness2[][3] =
484 {
485     //Sharpness 2
486     {0x3306, 0x08},
487     {0x3371, 0x01},
488 };
489
490 static  struct reginfo ov2655_Sharpness3[] =
491 {
492     //default
493     {0x3306, 0x08},
494     {0x332d, 0x02},
495 };
496 static  struct reginfo ov2655_Sharpness4[]=
497 {
498     //Sharpness 4
499     {0x3306, 0x08},
500     {0x332d, 0x03},
501 };
502
503 static  struct reginfo ov2655_Sharpness5[] =
504 {
505     //Sharpness 5
506     {0x3306, 0x08},
507     {0x332d, 0x04},
508 };
509 #endif
510
511 static  struct reginfo sensor_ClrFmt_YUYV[]=
512 {
513     //{0x4300, 0x30},
514     {0x00, 0x00}
515 };
516
517 static  struct reginfo sensor_ClrFmt_UYVY[]=
518 {
519     //{0x4300, 0x32},
520     {0x00, 0x00}
521 };
522
523 #if CONFIG_SENSOR_WhiteBalance
524 static  struct reginfo sensor_WhiteB_Auto[]=
525 {
526     {0x3406, 0x00},  //AWB auto, bit[1]:0,auto
527     {0x0000, 0x00}
528 };
529 /* Cloudy Colour Temperature : 6500K - 8000K  */
530 static  struct reginfo sensor_WhiteB_Cloudy[]=
531 {
532     {0x3406, 0x01},
533     {0x3400, 0x07},
534     {0x3401, 0x08},
535     {0x3402, 0x04},
536     {0x3403, 0x00},
537     {0x3404, 0x05},
538     {0x3405, 0x00},
539     {0x0000, 0x00}
540 };
541 /* ClearDay Colour Temperature : 5000K - 6500K  */
542 static  struct reginfo sensor_WhiteB_ClearDay[]=
543 {
544     //Sunny
545     {0x3406, 0x01},
546     {0x3400, 0x07},
547     {0x3401, 0x02},
548     {0x3402, 0x04},
549     {0x3403, 0x00},
550     {0x3404, 0x05},
551     {0x3405, 0x15},
552     {0x0000, 0x00}
553 };
554 /* Office Colour Temperature : 3500K - 5000K  */
555 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
556 {
557     //Office
558     {0x3406, 0x01},
559     {0x3400, 0x06},
560     {0x3401, 0x2a},
561     {0x3402, 0x04},
562     {0x3403, 0x00},
563     {0x3404, 0x07},
564     {0x3405, 0x24},
565     {0x0000, 0x00}
566
567 };
568 /* Home Colour Temperature : 2500K - 3500K  */
569 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
570 {
571     //Home
572     {0x3406, 0x01},
573     {0x3400, 0x04},
574     {0x3401, 0x58},
575     {0x3402, 0x04},
576     {0x3403, 0x00},
577     {0x3404, 0x07},
578     {0x3405, 0x24},
579     {0x0000, 0x00}
580 };
581 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
582     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
583 };
584 #endif
585
586 #if CONFIG_SENSOR_Brightness
587 static  struct reginfo sensor_Brightness0[]=
588 {
589     // Brightness -2
590     {0xff, 0x01},
591     {0x24, 0x34},
592     {0x25, 0x22},
593     {0x26, 0x70},
594   {0x0, 0x0}   //end flag
595 };
596
597 static  struct reginfo sensor_Brightness1[]=
598 {
599     // Brightness -1
600
601     {0xff, 0x01},
602     {0x24, 0x58},
603     {0x25, 0x50},
604     {0x26, 0x92},
605   {0x0, 0x0}   //end flag
606 };
607
608 static  struct reginfo sensor_Brightness2[]=
609 {
610     //  Brightness 0
611
612         {0xff, 0x01},
613         {0x24, 0xa8},
614         {0x25, 0x90},
615         {0x26, 0xd6},
616   {0x0, 0x0}   //end flag
617 };
618
619 static  struct reginfo sensor_Brightness3[]=
620 {
621     // Brightness +1
622
623     {0xff, 0x01},
624     {0x24, 0x48},
625     {0x25, 0x40},
626     {0x26, 0x81},
627   {0x0, 0x0}   //end flag
628 };
629
630 static  struct reginfo sensor_Brightness4[]=
631 {
632     //  Brightness +2
633
634     {0xff, 0x01},
635     {0x24, 0x58},
636     {0x25, 0x50},
637     {0x26, 0x92},
638   {0x0, 0x0}   //end flag
639 };
640
641 static  struct reginfo sensor_Brightness5[]=
642 {
643     //  Brightness +3
644     {0xff, 0x01},
645     {0x24, 0x70},
646     {0x25, 0x60},
647     {0x26, 0xa4},
648   {0x0, 0x0}   //end flag
649 };
650 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
651     sensor_Brightness4, sensor_Brightness5,NULL,
652 };
653
654 #endif
655
656 #if CONFIG_SENSOR_Effect
657 static  struct reginfo sensor_Effect_Normal[] =
658 {
659         {0xff,0x00},
660         {0x7c,0x00},
661         {0x7d,0x00},
662         {0x7c,0x05},
663         {0x7d,0x80},
664         {0x7d,0x80},
665   {0x0, 0x0}   //end flag
666 };
667
668 static  struct reginfo sensor_Effect_WandB[] =
669 {
670     {0xff,0x00},
671     {0x7c,0x00},
672     {0x7d,0x18},
673     {0x7c,0x05},
674     {0x7d,0x80},
675     {0x7d,0x80},
676   {0x0, 0x0}   //end flag
677 };
678
679 static  struct reginfo sensor_Effect_Sepia[] =
680 {
681     {0xff,0x00},
682     {0x7c,0x00},
683     {0x7d,0x18},
684     {0x7c,0x05},
685     {0x7d,0x40},
686     {0x7d,0xc0},
687   {0x0, 0x0}   //end flag
688 };
689
690 static  struct reginfo sensor_Effect_Negative[] =
691 {
692     {0xff,0x00},
693     {0x7c,0x00},
694     {0x7d,0x40},
695     {0x7c,0x05},
696     {0x7d,0x80},
697     {0x7d,0x80},
698   {0x0, 0x0}   //end flag
699 };
700 static  struct reginfo sensor_Effect_Bluish[] =
701 {
702     {0Xff, 0X00},
703     {0X7c, 0X00},
704     {0X7d, 0X18},
705     {0X7c, 0X05},
706     {0X7d, 0Xa0},
707     {0X7d, 0X40},
708   {0x0, 0x0}   //end flag
709 };
710
711 static  struct reginfo sensor_Effect_Green[] =
712 {
713     {0Xff, 0X00},
714     {0X7c, 0X00},
715     {0X7d, 0X18},
716     {0X7c, 0X05},
717     {0X7d, 0X40},
718     {0X7d, 0X40},
719   {0x0, 0x0}   //end flag
720 };
721
722 static  struct reginfo sensor_Effect_Exp_Windows_Half[] =
723 {
724         {0xff, 0x01},
725         {0x5d, 0x00},
726         {0x5e, 0x3c},
727         {0x5f, 0x28},
728         {0x60, 0x55},
729   {0x0, 0x0}   //end flag
730 };
731 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
732     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
733 };
734 #endif
735 #if CONFIG_SENSOR_Exposure
736 static  struct reginfo sensor_Exposure0[]=
737 {
738         {0x0000, 0x00}
739 };
740
741 static  struct reginfo sensor_Exposure1[]=
742 {
743     {0x0000, 0x00}
744 };
745
746 static  struct reginfo sensor_Exposure2[]=
747 {
748     {0x0000, 0x00}
749 };
750
751 static  struct reginfo sensor_Exposure3[]=
752 {
753     {0x0000, 0x00}
754 };
755
756 static  struct reginfo sensor_Exposure4[]=
757 {
758     {0x0000, 0x00}
759 };
760
761 static  struct reginfo sensor_Exposure5[]=
762 {
763     {0x0000, 0x00}
764 };
765
766 static  struct reginfo sensor_Exposure6[]=
767 {
768     {0x0000, 0x00}
769 };
770
771 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
772     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
773 };
774 #endif
775 #if CONFIG_SENSOR_Saturation
776 static  struct reginfo sensor_Saturation0[]=
777 {
778         {0xff, 0x00},
779         {0x90, 0x00},
780         {0x91, 0x0e},
781         {0x91, 0x1a},
782         {0x91, 0x31},
783         {0x91, 0x5a},
784         {0x91, 0x69},
785         {0x91, 0x75},
786         {0x91, 0x7e},
787         {0x91, 0x88},
788         {0x91, 0x8f},
789         {0x91, 0x96},
790         {0x91, 0xa3},
791         {0x91, 0xaf},
792         {0x91, 0xc4},
793         {0x91, 0xd7},
794         {0x91, 0xe8},
795         {0x91, 0x20},
796   {0x0, 0x0}   //end flag
797 };
798
799 static  struct reginfo sensor_Saturation1[]=
800 {
801     {0xff, 0x00},
802     {0x90, 0x00},
803     {0x91, 0x03},
804     {0x91, 0x0a},
805     {0x91, 0x1a},
806     {0x91, 0x3f},
807     {0x91, 0x4e},
808     {0x91, 0x5b},
809     {0x91, 0x68},
810     {0x91, 0x75},
811     {0x91, 0x7f},
812     {0x91, 0x89},
813     {0x91, 0x9a},
814     {0x91, 0xa6},
815     {0x91, 0xbd},
816     {0x91, 0xd3},
817     {0x91, 0xe5},
818     {0x91, 0x24},
819   {0x0, 0x0}   //end flag
820 };
821
822 static  struct reginfo sensor_Saturation2[]=
823 {
824     {0xff, 0x00},
825     {0x90, 0x00},
826     {0x91, 0x04},
827     {0x91, 0x07},
828     {0x91, 0x10},
829     {0x91, 0x28},
830     {0x91, 0x36},
831     {0x91, 0x44},
832     {0x91, 0x52},
833     {0x91, 0x60},
834     {0x91, 0x6c},
835     {0x91, 0x78},
836     {0x91, 0x8c},
837     {0x91, 0x9e},
838     {0x91, 0xbb},
839     {0x91, 0xd3},
840     {0x91, 0xe5},
841     {0x91, 0x24},
842   {0x0, 0x0}   //end flag       
843 };
844 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
845
846
847 #endif
848 #if CONFIG_SENSOR_Contrast
849 static  struct reginfo sensor_Contrast0[]=
850 {
851     {0xff, 0x00},
852     {0x7c, 0x00},
853     {0x7d, 0x04},
854     {0x7c, 0x07},
855     {0x7d, 0x20},
856     {0x7d, 0x10},
857     {0x7d, 0x4a},
858     {0x7d, 0x06},
859   {0x0, 0x0}   //end flag
860
861 };
862
863 static  struct reginfo sensor_Contrast1[]=
864 {
865     {0xff, 0x00},
866     {0x7c, 0x00},
867     {0x7d, 0x04},
868     {0x7c, 0x07},
869     {0x7d, 0x20},
870     {0x7d, 0x14},
871     {0x7d, 0x40},
872     {0x7d, 0x06},
873   {0x0, 0x0}   //end flag
874 };
875
876 static  struct reginfo sensor_Contrast2[]=
877 {
878     {0xff, 0x00},
879     {0x7c, 0x00},
880     {0x7d, 0x04},
881     {0x7c, 0x07},
882     {0x7d, 0x20},
883     {0x7d, 0x18},
884     {0x7d, 0x34},
885     {0x7d, 0x06},
886   {0x0, 0x0}   //end flag
887 };
888
889 static  struct reginfo sensor_Contrast3[]=
890 {
891     {0xff, 0x00},
892     {0x7c, 0x00},
893     {0x7d, 0x04},
894     {0x7c, 0x07},
895     {0x7d, 0x20},
896     {0x7d, 0x1c},
897     {0x7d, 0x2a},
898     {0x7d, 0x06},
899   {0x0, 0x0}   //end flag
900 };
901
902 static  struct reginfo sensor_Contrast4[]=
903 {
904     {0xff,0x00},
905     {0x7c,0x00},
906     {0x7d,0x04},
907     {0x7c,0x07},
908     {0x7d,0x20},
909     {0x7d,0x24},
910     {0x7d,0x16},
911     {0x7d,0x06},
912   {0x0, 0x0}   //end flag
913 };
914
915
916 static  struct reginfo sensor_Contrast5[]=
917 {
918     {0xff, 0x00},
919     {0x7c, 0x00},
920     {0x7d, 0x04},
921     {0x7c, 0x07},
922     {0x7d, 0x20},
923     {0x7d, 0x20},
924     {0x7d, 0x20},
925     {0x7d, 0x06},
926   {0x0, 0x0}   //end flag
927 };
928
929 static  struct reginfo sensor_Contrast6[]=
930 {
931     {0xff, 0x00},
932     {0x7c, 0x00},
933     {0x7d, 0x04},
934     {0x7c, 0x07},
935     {0x7d, 0x20},
936     {0x7d, 0x24},
937     {0x7d, 0x16},
938     {0x7d, 0x06},
939   {0x0, 0x0}   //end flag
940 };
941
942
943 static  struct reginfo sensor_Contrast7[]=
944 {
945     {0xff, 0x00},
946     {0x7c, 0x00},
947     {0x7d, 0x04},
948     {0x7c, 0x07},
949     {0x7d, 0x20},
950     {0x7d, 0x28},
951     {0x7d, 0x0c},
952     {0x7d, 0x06},
953   {0x0, 0x0}   //end flag
954 };
955
956 static  struct reginfo sensor_Contrast8[]=
957 {
958     {0xff, 0x00},
959     {0x7c, 0x00},
960     {0x7d, 0x04},
961     {0x7c, 0x07},
962     {0x7d, 0x20},
963     {0x7d, 0x2c},
964     {0x7d, 0x02},
965     {0x7d, 0x06},
966   {0x0, 0x0}   //end flag
967 };
968
969 static  struct reginfo sensor_Contrast9[]=
970 {
971     {0xff, 0x00},
972     {0x7c, 0x00},
973     {0x7d, 0x04},
974     {0x7c, 0x07},
975     {0x7d, 0x20},
976     {0x7d, 0x30},
977     {0x7d, 0x08},
978     {0x7d, 0x0e},
979   {0x0, 0x0}   //end flag
980 };
981
982
983
984 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
985     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
986 };
987
988 #endif
989 #if CONFIG_SENSOR_Mirror
990 static  struct reginfo sensor_MirrorOn[]=
991 {
992     {0x0000, 0x00}
993 };
994
995 static  struct reginfo sensor_MirrorOff[]=
996 {
997     {0x0000, 0x00}
998 };
999 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1000 #endif
1001 #if CONFIG_SENSOR_Flip
1002 static  struct reginfo sensor_FlipOn[]=
1003 {
1004     {0x0000, 0x00}
1005 };
1006
1007 static  struct reginfo sensor_FlipOff[]=
1008 {
1009     {0x0000, 0x00}
1010 };
1011 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1012
1013 #endif
1014 #if CONFIG_SENSOR_Scene
1015 static  struct reginfo sensor_SceneAuto[] =
1016 {
1017     {0x3a00, 0x78},
1018     {0x0000, 0x00}
1019 };
1020
1021 static  struct reginfo sensor_SceneNight[] =
1022 {
1023     {0x3003, 0x80},
1024         {0x3004, 0x20},
1025         {0x3005, 0x18},
1026         {0x3006, 0x0d},
1027         {0x3a00, 0x7c},
1028         {0x3a02 ,0x07},
1029         {0x3a03 ,0x38},
1030         {0x3a14 ,0x07},
1031         {0x3a15 ,0x38},
1032     {0x0000, 0x00}
1033 };
1034 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1035
1036 #endif
1037 #if CONFIG_SENSOR_DigitalZoom
1038 static struct reginfo sensor_Zoom0[] =
1039 {
1040     {0x0, 0x0},
1041 };
1042
1043 static struct reginfo sensor_Zoom1[] =
1044 {
1045      {0x0, 0x0},
1046 };
1047
1048 static struct reginfo sensor_Zoom2[] =
1049 {
1050     {0x0, 0x0},
1051 };
1052
1053
1054 static struct reginfo sensor_Zoom3[] =
1055 {
1056     {0x0, 0x0},
1057 };
1058 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1059 #endif
1060 static const struct v4l2_querymenu sensor_menus[] =
1061 {
1062         #if CONFIG_SENSOR_WhiteBalance
1063     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1064     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1065     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1066     #endif
1067
1068         #if CONFIG_SENSOR_Effect
1069     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1070     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1071     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
1072     #endif
1073
1074         #if CONFIG_SENSOR_Scene
1075     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1076     #endif
1077
1078         #if CONFIG_SENSOR_Flash
1079     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1080     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1081     #endif
1082 };
1083
1084 static const struct v4l2_queryctrl sensor_controls[] =
1085 {
1086         #if CONFIG_SENSOR_WhiteBalance
1087     {
1088         .id             = V4L2_CID_DO_WHITE_BALANCE,
1089         .type           = V4L2_CTRL_TYPE_MENU,
1090         .name           = "White Balance Control",
1091         .minimum        = 0,
1092         .maximum        = 4,
1093         .step           = 1,
1094         .default_value = 0,
1095     },
1096     #endif
1097
1098         #if CONFIG_SENSOR_Brightness
1099         {
1100         .id             = V4L2_CID_BRIGHTNESS,
1101         .type           = V4L2_CTRL_TYPE_INTEGER,
1102         .name           = "Brightness Control",
1103         .minimum        = -3,
1104         .maximum        = 2,
1105         .step           = 1,
1106         .default_value = 0,
1107     },
1108     #endif
1109
1110         #if CONFIG_SENSOR_Effect
1111         {
1112         .id             = V4L2_CID_EFFECT,
1113         .type           = V4L2_CTRL_TYPE_MENU,
1114         .name           = "Effect Control",
1115         .minimum        = 0,
1116         .maximum        = 5,
1117         .step           = 1,
1118         .default_value = 0,
1119     },
1120         #endif
1121
1122         #if CONFIG_SENSOR_Exposure
1123         {
1124         .id             = V4L2_CID_EXPOSURE,
1125         .type           = V4L2_CTRL_TYPE_INTEGER,
1126         .name           = "Exposure Control",
1127         .minimum        = 0,
1128         .maximum        = 6,
1129         .step           = 1,
1130         .default_value = 0,
1131     },
1132         #endif
1133
1134         #if CONFIG_SENSOR_Saturation
1135         {
1136         .id             = V4L2_CID_SATURATION,
1137         .type           = V4L2_CTRL_TYPE_INTEGER,
1138         .name           = "Saturation Control",
1139         .minimum        = 0,
1140         .maximum        = 2,
1141         .step           = 1,
1142         .default_value = 0,
1143     },
1144     #endif
1145
1146         #if CONFIG_SENSOR_Contrast
1147         {
1148         .id             = V4L2_CID_CONTRAST,
1149         .type           = V4L2_CTRL_TYPE_INTEGER,
1150         .name           = "Contrast Control",
1151         .minimum        = -3,
1152         .maximum        = 3,
1153         .step           = 1,
1154         .default_value = 0,
1155     },
1156         #endif
1157
1158         #if CONFIG_SENSOR_Mirror
1159         {
1160         .id             = V4L2_CID_HFLIP,
1161         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1162         .name           = "Mirror Control",
1163         .minimum        = 0,
1164         .maximum        = 1,
1165         .step           = 1,
1166         .default_value = 1,
1167     },
1168     #endif
1169
1170         #if CONFIG_SENSOR_Flip
1171         {
1172         .id             = V4L2_CID_VFLIP,
1173         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1174         .name           = "Flip Control",
1175         .minimum        = 0,
1176         .maximum        = 1,
1177         .step           = 1,
1178         .default_value = 1,
1179     },
1180     #endif
1181
1182         #if CONFIG_SENSOR_Scene
1183     {
1184         .id             = V4L2_CID_SCENE,
1185         .type           = V4L2_CTRL_TYPE_MENU,
1186         .name           = "Scene Control",
1187         .minimum        = 0,
1188         .maximum        = 1,
1189         .step           = 1,
1190         .default_value = 0,
1191     },
1192     #endif
1193
1194         #if CONFIG_SENSOR_DigitalZoom
1195     {
1196         .id             = V4L2_CID_ZOOM_RELATIVE,
1197         .type           = V4L2_CTRL_TYPE_INTEGER,
1198         .name           = "DigitalZoom Control",
1199         .minimum        = -1,
1200         .maximum        = 1,
1201         .step           = 1,
1202         .default_value = 0,
1203     }, {
1204         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1205         .type           = V4L2_CTRL_TYPE_INTEGER,
1206         .name           = "DigitalZoom Control",
1207         .minimum        = 0,
1208         .maximum        = 3,
1209         .step           = 1,
1210         .default_value = 0,
1211     },
1212     #endif
1213
1214         #if CONFIG_SENSOR_Focus
1215         {
1216         .id             = V4L2_CID_FOCUS_RELATIVE,
1217         .type           = V4L2_CTRL_TYPE_INTEGER,
1218         .name           = "Focus Control",
1219         .minimum        = -1,
1220         .maximum        = 1,
1221         .step           = 1,
1222         .default_value = 0,
1223     }, {
1224         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1225         .type           = V4L2_CTRL_TYPE_INTEGER,
1226         .name           = "Focus Control",
1227         .minimum        = 0,
1228         .maximum        = 255,
1229         .step           = 1,
1230         .default_value = 125,
1231     },
1232     #endif
1233
1234         #if CONFIG_SENSOR_Flash
1235         {
1236         .id             = V4L2_CID_FLASH,
1237         .type           = V4L2_CTRL_TYPE_MENU,
1238         .name           = "Flash Control",
1239         .minimum        = 0,
1240         .maximum        = 3,
1241         .step           = 1,
1242         .default_value = 0,
1243     },
1244         #endif
1245 };
1246
1247 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1248 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1249 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1250 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1251 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1252 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1253 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1254 static int sensor_resume(struct soc_camera_device *icd);
1255 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1256 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1257 #if CONFIG_SENSOR_Effect
1258 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1259 #endif
1260 #if CONFIG_SENSOR_WhiteBalance
1261 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1262 #endif
1263 static int sensor_deactivate(struct i2c_client *client);
1264
1265 static struct soc_camera_ops sensor_ops =
1266 {
1267     .suspend                     = sensor_suspend,
1268     .resume                       = sensor_resume,
1269     .set_bus_param              = sensor_set_bus_param,
1270     .query_bus_param    = sensor_query_bus_param,
1271     .controls           = sensor_controls,
1272     .menus                         = sensor_menus,
1273     .num_controls               = ARRAY_SIZE(sensor_controls),
1274     .num_menus          = ARRAY_SIZE(sensor_menus),
1275 };
1276
1277 /* only one fixed colorspace per pixelcode */
1278 struct sensor_datafmt {
1279         enum v4l2_mbus_pixelcode code;
1280         enum v4l2_colorspace colorspace;
1281 };
1282
1283 /* Find a data format by a pixel code in an array */
1284 static const struct sensor_datafmt *sensor_find_datafmt(
1285         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1286         int n)
1287 {
1288         int i;
1289         for (i = 0; i < n; i++)
1290                 if (fmt[i].code == code)
1291                         return fmt + i;
1292
1293         return NULL;
1294 }
1295
1296 static const struct sensor_datafmt sensor_colour_fmts[] = {
1297     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1298     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1299 };
1300
1301 typedef struct sensor_info_priv_s
1302 {
1303     int whiteBalance;
1304     int brightness;
1305     int contrast;
1306     int saturation;
1307     int effect;
1308     int scene;
1309     int digitalzoom;
1310     int focus;
1311     int flash;
1312     int exposure;
1313         bool snap2preview;
1314         bool video2preview;
1315     unsigned char mirror;                                        /* HFLIP */
1316     unsigned char flip;                                          /* VFLIP */
1317     unsigned int winseqe_cur_addr;
1318     struct sensor_datafmt fmt;
1319
1320 } sensor_info_priv_t;
1321
1322 struct sensor
1323 {
1324     struct v4l2_subdev subdev;
1325     struct i2c_client *client;
1326     sensor_info_priv_t info_priv;
1327     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1328 #if CONFIG_SENSOR_I2C_NOSCHED
1329         atomic_t tasklock_cnt;
1330 #endif
1331         struct rk29camera_platform_data *sensor_io_request;
1332     struct rk29camera_gpio_res *sensor_gpio_res;
1333 };
1334
1335 static struct sensor* to_sensor(const struct i2c_client *client)
1336 {
1337     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1338 }
1339
1340 static int sensor_task_lock(struct i2c_client *client, int lock)
1341 {
1342 #if CONFIG_SENSOR_I2C_NOSCHED
1343         int cnt = 3;
1344     struct sensor *sensor = to_sensor(client);
1345
1346         if (lock) {
1347                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1348                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1349                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1350                                 msleep(35);
1351                                 cnt--;
1352                         }
1353                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1354                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1355                                 goto sensor_task_lock_err;
1356                         }
1357                         preempt_disable();
1358                 }
1359
1360                 atomic_add(1, &sensor->tasklock_cnt);
1361         } else {
1362                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1363                         atomic_sub(1, &sensor->tasklock_cnt);
1364
1365                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1366                                 preempt_enable();
1367                 }
1368         }
1369         return 0;
1370 sensor_task_lock_err:
1371         return -1;  
1372 #else
1373     return 0;
1374 #endif
1375
1376 }
1377 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1378 {
1379     int err,cnt;
1380     u8 buf[2];
1381     struct i2c_msg msg[1];
1382
1383     buf[0] = reg & 0xFF;
1384     buf[1] = val;
1385
1386     msg->addr = client->addr;
1387     msg->flags = client->flags;
1388     msg->buf = buf;
1389     msg->len = sizeof(buf);
1390     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
1391     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1392
1393     cnt = 3;
1394     err = -EAGAIN;
1395
1396     while ((cnt-->0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1397         err = i2c_transfer(client->adapter, msg, 1);
1398
1399         if (err >= 0) {
1400             return 0;
1401         } else {
1402             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1403             udelay(10);
1404         }
1405     }
1406
1407     return err;
1408 }
1409
1410 /* sensor register read */
1411 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1412 {
1413     int err,cnt;
1414     u8 buf[1];
1415     struct i2c_msg msg[2];
1416
1417     buf[0] = reg ;//>> 8;
1418    // buf[1] = 0;
1419
1420     msg[0].addr = client->addr;
1421     msg[0].flags = client->flags;
1422     msg[0].buf = buf;
1423     msg[0].len = sizeof(buf);
1424     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1425     msg[0].read_type = 2;//0x55;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1426
1427     msg[1].addr = client->addr;
1428     msg[1].flags = client->flags|I2C_M_RD;
1429     msg[1].buf = buf;
1430     msg[1].len = 1;
1431     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1432     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1433
1434     cnt = 3;
1435     err = -EAGAIN;
1436     while ((cnt-->0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1437         err = i2c_transfer(client->adapter, msg, 2);
1438
1439         if (err >= 0) {
1440             *val = buf[0];
1441             return 0;
1442         } else {
1443                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1444             udelay(10);
1445         }
1446     }
1447
1448     return err;
1449 }
1450
1451 /* write a array of registers  */
1452 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1453 {
1454     int err = 0, cnt;
1455     int i = 0;
1456 #if CONFIG_SENSOR_I2C_RDWRCHK    
1457         char valchk;
1458 #endif
1459
1460         cnt = 0;
1461         if (sensor_task_lock(client, 1) < 0)
1462                 goto sensor_write_array_end;
1463     while (regarray[i].reg != 0)
1464     {
1465         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1466         if (err < 0)
1467         {
1468             if (cnt-- > 0) {
1469                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1470                                 i = 0;
1471                                 continue;
1472             } else {
1473                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1474                 err = -EPERM;
1475                                 goto sensor_write_array_end;
1476             }
1477         } else {
1478         #if CONFIG_SENSOR_I2C_RDWRCHK
1479                         sensor_read(client, regarray[i].reg, &valchk);
1480                         if (valchk != regarray[i].val)
1481                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1482                 #endif
1483         }
1484         i++;
1485     }
1486
1487 sensor_write_array_end:
1488         sensor_task_lock(client,0);
1489         return err;
1490 }
1491 #if CONFIG_SENSOR_I2C_RDWRCHK
1492 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1493 {
1494     int cnt;
1495     int i = 0;
1496         char valchk;
1497
1498         cnt = 0;
1499         valchk = 0;
1500     while (regarray[i].reg != 0)
1501     {
1502                 sensor_read(client, regarray[i].reg, &valchk);
1503                 if (valchk != regarray[i].val)
1504                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1505
1506         i++;
1507     }
1508     return 0;
1509 }
1510 #endif
1511 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1512 {
1513         struct soc_camera_link *icl = to_soc_camera_link(icd);
1514         int ret = 0;
1515
1516     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1517
1518         switch (cmd)
1519         {
1520                 case Sensor_PowerDown:
1521                 {
1522                         if (icl->powerdown) {
1523                                 ret = icl->powerdown(icd->pdev, on);
1524                                 if (ret == RK29_CAM_IO_SUCCESS) {
1525                                         if (on == 0) {
1526                                                 mdelay(2);
1527                                                 if (icl->reset)
1528                                                         icl->reset(icd->pdev);
1529                                         }
1530                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1531                                         ret = -ENODEV;
1532                                         goto sensor_power_end;
1533                                 }
1534                         }
1535                         break;
1536                 }
1537                 case Sensor_Flash:
1538                 {
1539                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1540                 struct sensor *sensor = to_sensor(client);
1541
1542                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1543                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1544                         }
1545             break;
1546                 }
1547                 default:
1548                 {
1549                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1550                         break;
1551                 }
1552         }
1553 sensor_power_end:
1554         return ret;
1555 }
1556 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1557 {
1558     struct i2c_client *client = v4l2_get_subdevdata(sd);\r
1559     struct soc_camera_device *icd = client->dev.platform_data;
1560     struct sensor *sensor = to_sensor(client);
1561         const struct v4l2_queryctrl *qctrl;
1562     const struct sensor_datafmt *fmt;
1563     char value;
1564     int ret,pid = 0;
1565
1566     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1567
1568         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1569                 ret = -ENODEV;
1570                 goto sensor_INIT_ERR;
1571         }
1572
1573     /* soft reset */
1574         if (sensor_task_lock(client,1)<0)
1575                 goto sensor_INIT_ERR;
1576     ret = sensor_write(client, 0xff, 1);
1577     ret |= sensor_write(client, 0x12, 0x80);
1578     if (ret != 0)
1579     {
1580         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1581         ret = -ENODEV;
1582                 goto sensor_INIT_ERR;
1583     }
1584
1585     mdelay(5);  //delay 5 microseconds
1586         /* check if it is an sensor sensor */
1587     ret = sensor_write(client, 0xff, 1);
1588     ret |= sensor_read(client, 0x0a, &value);
1589     if (ret != 0) {
1590         SENSOR_TR("read chip id high byte failed\n");
1591         ret = -ENODEV;
1592         goto sensor_INIT_ERR;
1593     }
1594
1595     pid = value << 8;
1596     ret = sensor_read(client, 0x0b, &value);
1597     if (ret != 0) {
1598         SENSOR_TR("read chip id low byte failed\n");
1599         ret = -ENODEV;
1600         goto sensor_INIT_ERR;
1601     }
1602
1603     pid |= (value & 0xff);
1604
1605     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1606     if ((pid == SENSOR_ID)||(pid == SENSOR_ID1)) {
1607         sensor->model = SENSOR_V4L2_IDENT;
1608     } else {
1609         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1610         ret = -ENODEV;
1611         goto sensor_INIT_ERR;
1612     }
1613
1614     ret = sensor_write_array(client, sensor_init_data);
1615     if (ret != 0)
1616     {
1617         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1618         goto sensor_INIT_ERR;
1619     }
1620         sensor_task_lock(client,0);
1621     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
1622     fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1623     if (!fmt) {
1624         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1625         ret = -EINVAL;
1626         goto sensor_INIT_ERR;
1627     }
1628         sensor->info_priv.fmt = *fmt;
1629
1630     /* sensor sensor information for initialization  */
1631         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1632         if (qctrl)
1633         sensor->info_priv.whiteBalance = qctrl->default_value;
1634         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1635         if (qctrl)
1636         sensor->info_priv.brightness = qctrl->default_value;
1637         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1638         if (qctrl)
1639         sensor->info_priv.effect = qctrl->default_value;
1640         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1641         if (qctrl)
1642         sensor->info_priv.exposure = qctrl->default_value;
1643
1644         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1645         if (qctrl)
1646         sensor->info_priv.saturation = qctrl->default_value;
1647         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1648         if (qctrl)
1649         sensor->info_priv.contrast = qctrl->default_value;
1650         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1651         if (qctrl)
1652         sensor->info_priv.mirror = qctrl->default_value;
1653         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1654         if (qctrl)
1655         sensor->info_priv.flip = qctrl->default_value;
1656         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1657         if (qctrl)
1658         sensor->info_priv.scene = qctrl->default_value;
1659         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1660         if (qctrl)
1661         sensor->info_priv.digitalzoom = qctrl->default_value;
1662
1663     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
1664         #if CONFIG_SENSOR_Focus
1665     sensor_set_focus();
1666     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1667         if (qctrl)
1668         sensor->info_priv.focus = qctrl->default_value;
1669         #endif
1670
1671         #if CONFIG_SENSOR_Flash
1672         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1673         if (qctrl)
1674         sensor->info_priv.flash = qctrl->default_value;
1675     #endif
1676
1677     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);
1678
1679     return 0;
1680 sensor_INIT_ERR:
1681         sensor_task_lock(client,0);
1682         sensor_deactivate(client);
1683     return ret;
1684 }
1685
1686 static int sensor_deactivate(struct i2c_client *client)
1687 {
1688         struct soc_camera_device *icd = client->dev.platform_data;
1689
1690         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1691
1692         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1693 #if 0   
1694         sensor_task_lock(client, 1);
1695     sensor_write(client, 0x3000, reg_val&0xfc);
1696         sensor_write(client, 0x3001, 0x00);
1697         sensor_task_lock(client, 0);
1698 #endif
1699         sensor_ioctrl(icd, Sensor_PowerDown, 1);
1700         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1701         icd->user_width = SENSOR_INIT_WIDTH;
1702     icd->user_height = SENSOR_INIT_HEIGHT;
1703         msleep(100);
1704         return 0;
1705 }
1706
1707 static  struct reginfo sensor_power_down_sequence[]=
1708 {
1709     {0x00,0x00}
1710 };
1711 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1712 {
1713     int ret;
1714     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1715
1716     if (pm_msg.event == PM_EVENT_SUSPEND) {
1717         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1718         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1719         if (ret != 0) {
1720             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1721             return ret;
1722         } else {
1723             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1724             if (ret < 0) {
1725                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1726                 return -EINVAL;
1727             }
1728         }
1729     } else {
1730         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1731         return -EINVAL;
1732     }
1733     return 0;
1734 }
1735
1736 static int sensor_resume(struct soc_camera_device *icd)
1737 {
1738         int ret;
1739
1740     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1741     if (ret < 0) {
1742                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1743         return -EINVAL;
1744     }
1745
1746         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1747
1748     return 0;
1749
1750 }
1751
1752 static int sensor_set_bus_param(struct soc_camera_device *icd,
1753                                 unsigned long flags)
1754 {
1755
1756     return 0;
1757 }
1758
1759 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1760 {
1761     struct soc_camera_link *icl = to_soc_camera_link(icd);
1762     unsigned long flags = SENSOR_BUS_PARAM;
1763
1764     return soc_camera_apply_sensor_flags(icl, flags);
1765 }
1766
1767 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1768 {
1769     struct i2c_client *client = v4l2_get_subdevdata(sd);\r
1770     struct soc_camera_device *icd = client->dev.platform_data;
1771     struct sensor *sensor = to_sensor(client);
1772
1773     mf->width   = icd->user_width;
1774         mf->height      = icd->user_height;
1775         mf->code        = sensor->info_priv.fmt.code;
1776         mf->colorspace  = sensor->info_priv.fmt.colorspace;
1777         mf->field       = V4L2_FIELD_NONE;
1778
1779     return 0;
1780 }
1781 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1782 {
1783     bool ret = false;
1784
1785         if ((mf->width == 1024) && (mf->height == 768)) {
1786                 ret = true;
1787         } else if ((mf->width == 1280) && (mf->height == 1024)) {
1788                 ret = true;
1789         } else if ((mf->width == 1600) && (mf->height == 1200)) {
1790                 ret = true;
1791         } else if ((mf->width == 2048) && (mf->height == 1536)) {
1792                 ret = true;
1793         } else if ((mf->width == 2592) && (mf->height == 1944)) {
1794                 ret = true;
1795         }
1796
1797         if (ret == true)
1798                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1799         return ret;
1800 }
1801
1802 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1803 {
1804     bool ret = false;
1805
1806         if ((mf->width == 1280) && (mf->height == 720)) {
1807                 ret = true;
1808         } else if ((mf->width == 1920) && (mf->height == 1080)) {
1809                 ret = true;
1810         }
1811
1812         if (ret == true)
1813                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1814         return ret;
1815 }
1816 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1817 {
1818     struct i2c_client *client = v4l2_get_subdevdata(sd);\r
1819     struct sensor *sensor = to_sensor(client);
1820     const struct sensor_datafmt *fmt;
1821         const struct v4l2_queryctrl *qctrl;
1822         struct soc_camera_device *icd = client->dev.platform_data;
1823     struct reginfo *winseqe_set_addr=NULL;
1824     int ret=0, set_w,set_h;
1825
1826         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1827                                    ARRAY_SIZE(sensor_colour_fmts));
1828         if (!fmt) {
1829         ret = -EINVAL;
1830         goto sensor_s_fmt_end;
1831     }
1832
1833         if (sensor->info_priv.fmt.code != mf->code) {
1834                 switch (mf->code)
1835                 {
1836                         case V4L2_MBUS_FMT_YUYV8_2X8:
1837                         {
1838                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
1839                                 break;
1840                         }
1841                         case V4L2_MBUS_FMT_UYVY8_2X8:
1842                         {
1843                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
1844                                 break;
1845                         }
1846                         default:
1847                                 break;
1848                 }
1849                 if (winseqe_set_addr != NULL) {
1850             sensor_write_array(client, winseqe_set_addr);
1851                         sensor->info_priv.fmt.code = mf->code;
1852             sensor->info_priv.fmt.colorspace= mf->colorspace;            
1853                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1854                 } else {
1855                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1856                 }
1857         }
1858
1859     set_w = mf->width;
1860     set_h = mf->height;
1861
1862         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1863         {
1864                 winseqe_set_addr = sensor_qcif;
1865         set_w = 176;
1866         set_h = 144;
1867         }
1868         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1869     {
1870         winseqe_set_addr = sensor_qvga;
1871         set_w = 320;
1872         set_h = 240;
1873     }
1874     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1875     {
1876         winseqe_set_addr = sensor_cif;
1877         set_w = 352;
1878         set_h = 288;
1879     }
1880     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1881     {
1882         winseqe_set_addr = sensor_vga;
1883         set_w = 640;
1884         set_h = 480;
1885     }
1886     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1887     {
1888         winseqe_set_addr = sensor_svga;
1889         set_w = 800;
1890         set_h = 600;
1891     }
1892         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
1893     {
1894         winseqe_set_addr = sensor_xga;
1895         set_w = 1024;
1896         set_h = 768;
1897     }
1898     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1899     {
1900         winseqe_set_addr = sensor_sxga;
1901         set_w = 1280;
1902         set_h = 1024;
1903     }
1904     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1905     {
1906         winseqe_set_addr = sensor_uxga;
1907         set_w = 1600;
1908         set_h = 1200;
1909     }
1910     else
1911     {
1912         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
1913         set_w = SENSOR_INIT_WIDTH;
1914         set_h = SENSOR_INIT_HEIGHT;
1915                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1916     }
1917
1918     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
1919         #if CONFIG_SENSOR_Flash
1920         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
1921             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1922                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1923                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1924             }           
1925         } else {                                        /* ddl@rock-chips.com : Video */
1926             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1927                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1928                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1929             }
1930         }
1931         #endif        
1932         ret |= sensor_write_array(client, winseqe_set_addr);
1933         if (ret != 0) {
1934             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1935             #if CONFIG_SENSOR_Flash
1936             if (sensor_fmt_capturechk(sd,mf) == true) {
1937                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1938                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1939                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1940                 }
1941             }
1942             #endif
1943             goto sensor_s_fmt_end;
1944         }
1945
1946         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
1947
1948                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
1949         #if CONFIG_SENSOR_Effect
1950                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1951                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1952         #endif
1953         #if CONFIG_SENSOR_WhiteBalance
1954                         if (sensor->info_priv.whiteBalance != 0) {
1955                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1956                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1957                         }
1958         #endif
1959                         sensor->info_priv.snap2preview = true;
1960                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
1961                 #if CONFIG_SENSOR_Effect
1962                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1963                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1964         #endif
1965         #if CONFIG_SENSOR_WhiteBalance
1966                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1967                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1968         #endif
1969                         sensor->info_priv.video2preview = true;
1970                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1971                 #if CONFIG_SENSOR_Effect
1972                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1973                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1974         #endif
1975         #if CONFIG_SENSOR_WhiteBalance    
1976                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1977                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1978         #endif    
1979                         sensor->info_priv.video2preview = false;            
1980                         sensor->info_priv.snap2preview = false;
1981                 }
1982         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1983     }
1984     else
1985     {
1986         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1987     }
1988
1989         mf->width = set_w;
1990     mf->height = set_h;
1991
1992 sensor_s_fmt_end:
1993     return ret;
1994 }
1995
1996 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1997 {
1998     struct i2c_client *client = v4l2_get_subdevdata(sd);\r
1999     struct sensor *sensor = to_sensor(client);
2000     const struct sensor_datafmt *fmt;
2001     int ret = 0,set_w,set_h;\r
2002    
2003         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2004                                    ARRAY_SIZE(sensor_colour_fmts));
2005         if (fmt == NULL) {
2006                 fmt = &sensor->info_priv.fmt;
2007         mf->code = fmt->code;
2008         } 
2009
2010     if (mf->height > SENSOR_MAX_HEIGHT)
2011         mf->height = SENSOR_MAX_HEIGHT;
2012     else if (mf->height < SENSOR_MIN_HEIGHT)
2013         mf->height = SENSOR_MIN_HEIGHT;
2014
2015     if (mf->width > SENSOR_MAX_WIDTH)
2016         mf->width = SENSOR_MAX_WIDTH;
2017     else if (mf->width < SENSOR_MIN_WIDTH)
2018         mf->width = SENSOR_MIN_WIDTH;
2019 \r
2020     set_w = mf->width;
2021     set_h = mf->height;
2022
2023         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2024         {
2025         set_w = 176;\r
2026         set_h = 144;
2027         }
2028         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2029     {
2030         set_w = 320;\r
2031         set_h = 240;
2032     }
2033     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2034     {
2035         set_w = 352;\r
2036         set_h = 288;
2037     }
2038     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2039     {
2040         set_w = 640;\r
2041         set_h = 480;
2042     }
2043     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2044     {
2045         set_w = 800;\r
2046         set_h = 600;
2047     }
2048         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
2049     {
2050         set_w = 1024;\r
2051         set_h = 768;
2052     }
2053     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2054     {
2055         set_w = 1280;\r
2056         set_h = 1024;
2057     }
2058     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2059     {
2060         set_w = 1600;\r
2061         set_h = 1200;
2062     }
2063     else
2064     {
2065         set_w = SENSOR_INIT_WIDTH;\r
2066         set_h = SENSOR_INIT_HEIGHT;
2067     }\r
2068
2069     mf->width = set_w;
2070     mf->height = set_h;\r
2071 \r
2072     mf->colorspace = fmt->colorspace;
2073     
2074     return ret;
2075 }
2076  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2077 {
2078     struct i2c_client *client = v4l2_get_subdevdata(sd);\r
2079
2080     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2081         return -EINVAL;
2082
2083     if (id->match.addr != client->addr)
2084         return -ENODEV;
2085
2086     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2087     id->revision = 0;
2088
2089     return 0;
2090 }
2091 #if CONFIG_SENSOR_Brightness
2092 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2093 {
2094     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2095
2096     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2097     {
2098         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2099         {
2100             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2101             {
2102                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2103                 return -EINVAL;
2104             }
2105             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2106             return 0;
2107         }
2108     }
2109         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2110     return -EINVAL;
2111 }
2112 #endif
2113 #if CONFIG_SENSOR_Effect
2114 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2115 {
2116     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2117
2118     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2119     {
2120         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2121         {
2122             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2123             {
2124                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2125                 return -EINVAL;
2126             }
2127             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2128             return 0;
2129         }
2130     }
2131         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2132     return -EINVAL;
2133 }
2134 #endif
2135 #if CONFIG_SENSOR_Exposure
2136 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2137 {
2138     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2139
2140     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2141     {
2142         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2143         {
2144             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2145             {
2146                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2147                 return -EINVAL;
2148             }
2149             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2150             return 0;
2151         }
2152     }
2153         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2154     return -EINVAL;
2155 }
2156 #endif
2157 #if CONFIG_SENSOR_Saturation
2158 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2159 {
2160     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2161
2162     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2163     {
2164         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2165         {
2166             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2167             {
2168                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2169                 return -EINVAL;
2170             }
2171             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2172             return 0;
2173         }
2174     }
2175     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2176     return -EINVAL;
2177 }
2178 #endif
2179 #if CONFIG_SENSOR_Contrast
2180 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2181 {
2182     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2183
2184     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2185     {
2186         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2187         {
2188             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2189             {
2190                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2191                 return -EINVAL;
2192             }
2193             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2194             return 0;
2195         }
2196     }
2197     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2198     return -EINVAL;
2199 }
2200 #endif
2201 #if CONFIG_SENSOR_Mirror
2202 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2203 {
2204     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2205
2206     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2207     {
2208         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2209         {
2210             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2211             {
2212                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2213                 return -EINVAL;
2214             }
2215             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2216             return 0;
2217         }
2218     }
2219     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2220     return -EINVAL;
2221 }
2222 #endif
2223 #if CONFIG_SENSOR_Flip
2224 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2225 {
2226     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2227
2228     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2229     {
2230         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2231         {
2232             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2233             {
2234                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2235                 return -EINVAL;
2236             }
2237             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2238             return 0;
2239         }
2240     }
2241     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2242     return -EINVAL;
2243 }
2244 #endif
2245 #if CONFIG_SENSOR_Scene
2246 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2247 {
2248     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2249
2250     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2251     {
2252         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2253         {
2254             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2255             {
2256                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2257                 return -EINVAL;
2258             }
2259             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2260             return 0;
2261         }
2262     }
2263     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2264     return -EINVAL;
2265 }
2266 #endif
2267 #if CONFIG_SENSOR_WhiteBalance
2268 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2269 {
2270     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2271
2272     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2273     {
2274         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2275         {
2276             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2277             {
2278                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2279                 return -EINVAL;
2280             }
2281             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2282             return 0;
2283         }
2284     }
2285         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2286     return -EINVAL;
2287 }
2288 #endif
2289 #if CONFIG_SENSOR_DigitalZoom
2290 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2291 {
2292     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2293     struct sensor *sensor = to_sensor(client);
2294         const struct v4l2_queryctrl *qctrl_info;
2295     int digitalzoom_cur, digitalzoom_total;
2296
2297         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2298         if (qctrl_info)
2299                 return -EINVAL;
2300
2301     digitalzoom_cur = sensor->info_priv.digitalzoom;
2302     digitalzoom_total = qctrl_info->maximum;
2303
2304     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2305     {
2306         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2307         return -EINVAL;
2308     }
2309
2310     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2311     {
2312         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2313         return -EINVAL;
2314     }
2315
2316     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2317     {
2318         *value = digitalzoom_total - digitalzoom_cur;
2319     }
2320
2321     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2322     {
2323         *value = 0 - digitalzoom_cur;
2324     }
2325
2326     digitalzoom_cur += *value;
2327
2328     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2329     {
2330         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2331         {
2332             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2333             return -EINVAL;
2334         }
2335         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2336         return 0;
2337     }
2338
2339     return -EINVAL;
2340 }
2341 #endif
2342 #if CONFIG_SENSOR_Flash
2343 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2344 {    
2345     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2346         if (value == 3) {       /* ddl@rock-chips.com: torch */
2347             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2348         } else {
2349             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2350         }
2351         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2352         return 0;
2353     }
2354     
2355         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2356     return -EINVAL;
2357 }
2358 #endif
2359
2360 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2361 {
2362     struct i2c_client *client = v4l2_get_subdevdata(sd);\r
2363     struct sensor *sensor = to_sensor(client);
2364     const struct v4l2_queryctrl *qctrl;
2365
2366     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2367
2368     if (!qctrl)
2369     {
2370         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2371         return -EINVAL;
2372     }
2373
2374     switch (ctrl->id)
2375     {
2376         case V4L2_CID_BRIGHTNESS:
2377             {
2378                 ctrl->value = sensor->info_priv.brightness;
2379                 break;
2380             }
2381         case V4L2_CID_SATURATION:
2382             {
2383                 ctrl->value = sensor->info_priv.saturation;
2384                 break;
2385             }
2386         case V4L2_CID_CONTRAST:
2387             {
2388                 ctrl->value = sensor->info_priv.contrast;
2389                 break;
2390             }
2391         case V4L2_CID_DO_WHITE_BALANCE:
2392             {
2393                 ctrl->value = sensor->info_priv.whiteBalance;
2394                 break;
2395             }
2396         case V4L2_CID_EXPOSURE:
2397             {
2398                 ctrl->value = sensor->info_priv.exposure;
2399                 break;
2400             }
2401         case V4L2_CID_HFLIP:
2402             {
2403                 ctrl->value = sensor->info_priv.mirror;
2404                 break;
2405             }
2406         case V4L2_CID_VFLIP:
2407             {
2408                 ctrl->value = sensor->info_priv.flip;
2409                 break;
2410             }
2411         default :
2412                 break;
2413     }
2414     return 0;
2415 }
2416
2417
2418
2419 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2420 {
2421     struct i2c_client *client = v4l2_get_subdevdata(sd);\r
2422     struct sensor *sensor = to_sensor(client);
2423     struct soc_camera_device *icd = client->dev.platform_data;
2424     const struct v4l2_queryctrl *qctrl;
2425
2426
2427     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2428
2429     if (!qctrl)
2430     {
2431         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2432         return -EINVAL;
2433     }
2434
2435     switch (ctrl->id)
2436     {
2437 #if CONFIG_SENSOR_Brightness
2438         case V4L2_CID_BRIGHTNESS:
2439             {
2440                 if (ctrl->value != sensor->info_priv.brightness)
2441                 {
2442                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2443                     {
2444                         return -EINVAL;
2445                     }
2446                     sensor->info_priv.brightness = ctrl->value;
2447                 }
2448                 break;
2449             }
2450 #endif
2451 #if CONFIG_SENSOR_Exposure
2452         case V4L2_CID_EXPOSURE:
2453             {
2454                 if (ctrl->value != sensor->info_priv.exposure)
2455                 {
2456                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2457                     {
2458                         return -EINVAL;
2459                     }
2460                     sensor->info_priv.exposure = ctrl->value;
2461                 }
2462                 break;
2463             }
2464 #endif
2465 #if CONFIG_SENSOR_Saturation
2466         case V4L2_CID_SATURATION:
2467             {
2468                 if (ctrl->value != sensor->info_priv.saturation)
2469                 {
2470                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2471                     {
2472                         return -EINVAL;
2473                     }
2474                     sensor->info_priv.saturation = ctrl->value;
2475                 }
2476                 break;
2477             }
2478 #endif
2479 #if CONFIG_SENSOR_Contrast
2480         case V4L2_CID_CONTRAST:
2481             {
2482                 if (ctrl->value != sensor->info_priv.contrast)
2483                 {
2484                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2485                     {
2486                         return -EINVAL;
2487                     }
2488                     sensor->info_priv.contrast = ctrl->value;
2489                 }
2490                 break;
2491             }
2492 #endif
2493 #if CONFIG_SENSOR_WhiteBalance
2494         case V4L2_CID_DO_WHITE_BALANCE:
2495             {
2496                 if (ctrl->value != sensor->info_priv.whiteBalance)
2497                 {
2498                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2499                     {
2500                         return -EINVAL;
2501                     }
2502                     sensor->info_priv.whiteBalance = ctrl->value;
2503                 }
2504                 break;
2505             }
2506 #endif
2507 #if CONFIG_SENSOR_Mirror
2508         case V4L2_CID_HFLIP:
2509             {
2510                 if (ctrl->value != sensor->info_priv.mirror)
2511                 {
2512                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2513                         return -EINVAL;
2514                     sensor->info_priv.mirror = ctrl->value;
2515                 }
2516                 break;
2517             }
2518 #endif
2519 #if CONFIG_SENSOR_Flip
2520         case V4L2_CID_VFLIP:
2521             {
2522                 if (ctrl->value != sensor->info_priv.flip)
2523                 {
2524                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2525                         return -EINVAL;
2526                     sensor->info_priv.flip = ctrl->value;
2527                 }
2528                 break;
2529             }
2530 #endif
2531         default:
2532             break;
2533     }
2534
2535     return 0;
2536 }
2537 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2538 {
2539     const struct v4l2_queryctrl *qctrl;
2540     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2541     struct sensor *sensor = to_sensor(client);
2542
2543     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2544
2545     if (!qctrl)
2546     {
2547         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2548         return -EINVAL;
2549     }
2550
2551     switch (ext_ctrl->id)
2552     {
2553         case V4L2_CID_SCENE:
2554             {
2555                 ext_ctrl->value = sensor->info_priv.scene;
2556                 break;
2557             }
2558         case V4L2_CID_EFFECT:
2559             {
2560                 ext_ctrl->value = sensor->info_priv.effect;
2561                 break;
2562             }
2563         case V4L2_CID_ZOOM_ABSOLUTE:
2564             {
2565                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2566                 break;
2567             }
2568         case V4L2_CID_ZOOM_RELATIVE:
2569             {
2570                 return -EINVAL;
2571             }
2572         case V4L2_CID_FOCUS_ABSOLUTE:
2573             {
2574                 ext_ctrl->value = sensor->info_priv.focus;
2575                 break;
2576             }
2577         case V4L2_CID_FOCUS_RELATIVE:
2578             {
2579                 return -EINVAL;
2580             }
2581         case V4L2_CID_FLASH:
2582             {
2583                 ext_ctrl->value = sensor->info_priv.flash;
2584                 break;
2585             }
2586         default :
2587             break;
2588     }
2589     return 0;
2590 }
2591 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2592 {
2593     const struct v4l2_queryctrl *qctrl;
2594     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2595     struct sensor *sensor = to_sensor(client);
2596     int val_offset;
2597
2598     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2599
2600     if (!qctrl)
2601     {
2602         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2603         return -EINVAL;
2604     }
2605
2606         val_offset = 0;
2607     switch (ext_ctrl->id)
2608     {
2609 #if CONFIG_SENSOR_Scene
2610         case V4L2_CID_SCENE:
2611             {
2612                 if (ext_ctrl->value != sensor->info_priv.scene)
2613                 {
2614                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2615                         return -EINVAL;
2616                     sensor->info_priv.scene = ext_ctrl->value;
2617                 }
2618                 break;
2619             }
2620 #endif
2621 #if CONFIG_SENSOR_Effect
2622         case V4L2_CID_EFFECT:
2623             {
2624                 if (ext_ctrl->value != sensor->info_priv.effect)
2625                 {
2626                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2627                         return -EINVAL;
2628                     sensor->info_priv.effect= ext_ctrl->value;
2629                 }
2630                 break;
2631             }
2632 #endif
2633 #if CONFIG_SENSOR_DigitalZoom
2634         case V4L2_CID_ZOOM_ABSOLUTE:
2635             {
2636                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2637                     return -EINVAL;
2638
2639                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2640                 {
2641                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2642
2643                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2644                         return -EINVAL;
2645                     sensor->info_priv.digitalzoom += val_offset;
2646
2647                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2648                 }
2649
2650                 break;
2651             }
2652         case V4L2_CID_ZOOM_RELATIVE:
2653             {
2654                 if (ext_ctrl->value)
2655                 {
2656                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2657                         return -EINVAL;
2658                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2659
2660                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2661                 }
2662                 break;
2663             }
2664 #endif
2665 #if CONFIG_SENSOR_Focus
2666         case V4L2_CID_FOCUS_ABSOLUTE:
2667             {
2668                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2669                     return -EINVAL;
2670
2671                 if (ext_ctrl->value != sensor->info_priv.focus)
2672                 {
2673                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2674
2675                     sensor->info_priv.focus += val_offset;
2676                 }
2677
2678                 break;
2679             }
2680         case V4L2_CID_FOCUS_RELATIVE:
2681             {
2682                 if (ext_ctrl->value)
2683                 {
2684                     sensor->info_priv.focus += ext_ctrl->value;
2685
2686                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2687                 }
2688                 break;
2689             }
2690 #endif
2691 #if CONFIG_SENSOR_Flash
2692         case V4L2_CID_FLASH:
2693             {
2694                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2695                     return -EINVAL;
2696                 sensor->info_priv.flash = ext_ctrl->value;
2697
2698                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2699                 break;
2700             }
2701 #endif
2702         default:
2703             break;
2704     }
2705
2706     return 0;
2707 }
2708
2709 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2710 {
2711     struct i2c_client *client = v4l2_get_subdevdata(sd);\r
2712     struct soc_camera_device *icd = client->dev.platform_data;
2713     int i, error_cnt=0, error_idx=-1;
2714
2715
2716     for (i=0; i<ext_ctrl->count; i++) {
2717         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2718             error_cnt++;
2719             error_idx = i;
2720         }
2721     }
2722
2723     if (error_cnt > 1)
2724         error_idx = ext_ctrl->count;
2725
2726     if (error_idx != -1) {
2727         ext_ctrl->error_idx = error_idx;
2728         return -EINVAL;
2729     } else {
2730         return 0;
2731     }
2732 }
2733
2734 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2735 {
2736     struct i2c_client *client = v4l2_get_subdevdata(sd);\r
2737     struct soc_camera_device *icd = client->dev.platform_data;
2738     int i, error_cnt=0, error_idx=-1;
2739
2740
2741     for (i=0; i<ext_ctrl->count; i++) {
2742         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2743             error_cnt++;
2744             error_idx = i;
2745         }
2746     }
2747
2748     if (error_cnt > 1)
2749         error_idx = ext_ctrl->count;
2750
2751     if (error_idx != -1) {
2752         ext_ctrl->error_idx = error_idx;
2753         return -EINVAL;
2754     } else {
2755         return 0;
2756     }
2757 }
2758
2759 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2760  * this wasn't our capture interface, so, we wait for the right one */
2761 static int sensor_video_probe(struct soc_camera_device *icd,
2762                                struct i2c_client *client)
2763 {
2764     char value;
2765     int ret,pid = 0;
2766     struct sensor *sensor = to_sensor(client);
2767
2768     /* We must have a parent by now. And it cannot be a wrong one.
2769      * So this entire test is completely redundant. */
2770     if (!icd->dev.parent ||
2771             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2772                 return -ENODEV;
2773
2774         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2775                 ret = -ENODEV;
2776                 goto sensor_video_probe_err;
2777         }
2778
2779     /* soft reset */
2780     ret = sensor_write(client, 0xff, 0x1);
2781     if (ret != 0) {
2782         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2783         ret = -ENODEV;
2784                 goto sensor_video_probe_err;
2785     }
2786     mdelay(5);          //delay 5 microseconds
2787
2788     /* check if it is an sensor sensor */
2789     ret = sensor_read(client, 0x0a, &value);
2790     if (ret != 0) {
2791         SENSOR_TR("read chip id high byte failed\n");
2792         ret = -ENODEV;
2793         goto sensor_video_probe_err;
2794     }
2795     pid = value << 8;
2796
2797     ret = sensor_read(client, 0x0b, &value);
2798     if (ret != 0) {
2799         SENSOR_TR("read chip id low byte failed\n");
2800         ret = -ENODEV;
2801         goto sensor_video_probe_err;
2802     }
2803
2804     pid |= (value & 0xff);
2805     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2806
2807     if ((pid == SENSOR_ID)||(pid == SENSOR_ID1)) {
2808         sensor->model = SENSOR_V4L2_IDENT;
2809     } else {
2810         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2811         ret = -ENODEV;
2812         goto sensor_video_probe_err;
2813     }
2814
2815     return 0;
2816
2817 sensor_video_probe_err:
2818
2819     return ret;
2820 }
2821
2822 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2823 {
2824         struct i2c_client *client = v4l2_get_subdevdata(sd);\r
2825     struct soc_camera_device *icd = client->dev.platform_data;  
2826     struct sensor *sensor = to_sensor(client);
2827     int ret = 0;
2828     
2829         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2830         switch (cmd)
2831         {
2832                 case RK29_CAM_SUBDEV_DEACTIVATE:
2833                 {
2834                         sensor_deactivate(client);
2835                         break;
2836                 }
2837
2838                 case RK29_CAM_SUBDEV_IOREQUEST:
2839                 {
2840                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           \r
2841             if (sensor->sensor_io_request != NULL) { 
2842                 sensor->sensor_gpio_res = NULL;
2843                 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
2844                     if (sensor->sensor_io_request->gpio_res[i].dev_name && 
2845                         (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
2846                         sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
2847                     }
2848                 }
2849                 if (sensor->sensor_gpio_res == NULL) {
2850                     SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
2851                     ret = -EINVAL;
2852                     goto sensor_ioctl_end;
2853                 }
2854             } else {
2855                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2856                 ret = -EINVAL;
2857                 goto sensor_ioctl_end;
2858             }\r
2859             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2860                for this project */
2861             #if CONFIG_SENSOR_Flash     
2862                 if (sensor->sensor_gpio_res) { 
2863                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2864                     for (i = 0; i < icd->ops->num_controls; i++) {
2865                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2866                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2867                                 }
2868                     }
2869                     sensor->info_priv.flash = 0xff;
2870                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2871                 }
2872                 }
2873             #endif
2874                         break;
2875                 }
2876                 default:
2877                 {
2878                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2879                         break;
2880                 }
2881         }
2882
2883 sensor_ioctl_end:
2884         return ret;
2885
2886 }
2887 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2888                             enum v4l2_mbus_pixelcode *code)
2889 {
2890         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2891                 return -EINVAL;
2892
2893         *code = sensor_colour_fmts[index].code;
2894         return 0;
2895 }
2896 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2897         .init           = sensor_init,
2898         .g_ctrl         = sensor_g_control,
2899         .s_ctrl         = sensor_s_control,
2900         .g_ext_ctrls          = sensor_g_ext_controls,
2901         .s_ext_ctrls          = sensor_s_ext_controls,
2902         .g_chip_ident   = sensor_g_chip_ident,
2903         .ioctl = sensor_ioctl,
2904 };
2905
2906 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2907         .s_mbus_fmt     = sensor_s_fmt,
2908         .g_mbus_fmt     = sensor_g_fmt,
2909         .try_mbus_fmt   = sensor_try_fmt,
2910         .enum_mbus_fmt  = sensor_enum_fmt,
2911 };
2912
2913 static struct v4l2_subdev_ops sensor_subdev_ops = {
2914         .core   = &sensor_subdev_core_ops,
2915         .video = &sensor_subdev_video_ops,
2916 };
2917
2918 static int sensor_probe(struct i2c_client *client,
2919                          const struct i2c_device_id *did)
2920 {
2921     struct sensor *sensor;
2922     struct soc_camera_device *icd = client->dev.platform_data;
2923     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2924     struct soc_camera_link *icl;
2925     int ret;
2926
2927     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2928     if (!icd) {
2929         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2930         return -EINVAL;
2931     }
2932
2933     icl = to_soc_camera_link(icd);
2934     if (!icl) {
2935         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2936         return -EINVAL;
2937     }
2938
2939     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2940         dev_warn(&adapter->dev,
2941                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2942         return -EIO;
2943     }
2944
2945     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2946     if (!sensor)
2947         return -ENOMEM;
2948
2949     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2950
2951     /* Second stage probe - when a capture adapter is there */
2952     icd->ops            = &sensor_ops;
2953     sensor->info_priv.fmt = sensor_colour_fmts[0];
2954         #if CONFIG_SENSOR_I2C_NOSCHED
2955         atomic_set(&sensor->tasklock_cnt,0);
2956         #endif
2957
2958     ret = sensor_video_probe(icd, client);
2959     if (ret < 0) {
2960         icd->ops = NULL;
2961         i2c_set_clientdata(client, NULL);
2962         kfree(sensor);
2963                 sensor = NULL;
2964     }
2965     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2966     return ret;
2967 }
2968
2969 static int sensor_remove(struct i2c_client *client)
2970 {
2971     struct sensor *sensor = to_sensor(client);
2972     struct soc_camera_device *icd = client->dev.platform_data;
2973
2974     icd->ops = NULL;
2975     i2c_set_clientdata(client, NULL);
2976     client->driver = NULL;
2977     kfree(sensor);
2978         sensor = NULL;
2979     return 0;
2980 }
2981
2982 static const struct i2c_device_id sensor_id[] = {
2983         {SENSOR_NAME_STRING(), 0 },
2984         { }
2985 };
2986 MODULE_DEVICE_TABLE(i2c, sensor_id);
2987
2988 static struct i2c_driver sensor_i2c_driver = {
2989         .driver = {
2990                 .name = SENSOR_NAME_STRING(),
2991         },
2992         .probe          = sensor_probe,
2993         .remove         = sensor_remove,
2994         .id_table       = sensor_id,
2995 };
2996
2997 static int __init sensor_mod_init(void)
2998 {
2999     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3000     return i2c_add_driver(&sensor_i2c_driver);
3001 }
3002
3003 static void __exit sensor_mod_exit(void)
3004 {
3005     i2c_del_driver(&sensor_i2c_driver);
3006 }
3007
3008 device_initcall_sync(sensor_mod_init);
3009 module_exit(sensor_mod_exit);
3010
3011 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3012 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3013 MODULE_LICENSE("GPL");\r