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