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