camera driver v0.2.1 for RK30
[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 <mach/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 RK_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 static s32 sensor_init_width = 0;
87 static s32 sensor_init_height = 0;
88 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);
89 static enum v4l2_mbus_pixelcode sensor_init_pixelcode = V4L2_MBUS_FMT_YUYV8_2X8;
90 static struct reginfo* sensor_init_data_p = NULL;
91 static struct reginfo* sensor_init_winseq_p = NULL;
92 static struct reginfo* sensor_init_winseq_board = NULL;
93 /* init 800*600 SVGA */
94 static struct reginfo sensor_init_data[] =
95 {
96 #if 1
97     {0xff,0x01},
98     {0x12,0x80},
99     {0xff,0x00},
100     {0x2c,0xff},
101     {0x2e,0xdf},
102     {0xff,0x01},
103
104     {0x03,0x4f},
105     {0x0f,0x4b},
106
107
108     {0x3c,0x32},
109     {0x11,0x00},
110     {0x09,0x02},
111     {0x04,0xF8},//b7,b6 directs
112     {0x13,0xe5},
113     {0x14,0x48},
114     {0x2c,0x0c},
115     {0x33,0x78},
116     {0x3a,0x33},
117     {0x3b,0xfB},
118     {0x3e,0x00},
119     {0x43,0x11},
120     {0x16,0x10},
121     {0x39,0x02},
122     {0x35,0x88},
123     {0x22,0x09},
124     {0x37,0x40},
125     {0x23,0x00},
126     {0x34,0xa0},
127     {0x36,0x1a},
128     {0x06,0x02},
129     {0x07,0xc0},
130     {0x0d,0xb7},
131     {0x0e,0x01},
132     {0x4c,0x00},
133     {0x4a,0x81},
134     {0x21,0x99},
135     
136     //{0x24,0x58},
137     //{0x25,0x50},
138     //{0x26,0x92},
139     
140     {0x24, 0x70},
141     {0x25, 0x60},
142     {0x26, 0xa4},    
143     
144     {0x5c,0x00},
145     {0x63,0x00},
146     {0x46,0x3f},
147     {0x0c,0x3c},
148     {0x61,0x70},
149     {0x62,0x80},
150     {0x7c,0x05},
151     {0x20,0x80},
152     {0x28,0x30},
153     {0x6c,0x00},
154     {0x6d,0x80},
155     {0x6e,0x00},
156     {0x70,0x02},
157     {0x71,0x94},
158     {0x73,0xc1},
159     {0x3d,0x34},
160     {0x5a,0x57},
161     {0x4f,0xbb},
162     {0x50,0x9c},
163     {0xff,0x00},
164     {0xe5,0x7f},
165     {0xf9,0xc0},
166     {0x41,0x24},
167     {0xe0,0x14},
168     {0x76,0xff},
169     {0x33,0xa0},
170     {0x42,0x20},
171     {0x43,0x18},
172     {0x4c,0x00},
173     {0x87,0xd0},
174     {0x88,0x3f},
175     {0xd7,0x03},
176     {0xd9,0x10},
177     {0xd3,0x82},
178     {0xc8,0x08},
179     {0xc9,0x80},
180     {0x7c,0x00},
181     {0x7d,0x00},//0x00//0x07
182     {0x7c,0x03},
183     {0x7d,0x48},//0x48//0x40
184     {0x7d,0x48},//0x48//0x40
185     {0x7c,0x08},
186     {0x7d,0x20},
187     {0x7d,0x10},//0x10
188     {0x7d,0x0e},//0x0e
189
190     {0x92,0x00},
191     {0x93,0x06},
192     {0x93,0xc8},//e3
193     {0x93,0x05},
194     {0x93,0x05},
195     {0x93,0x00},
196     {0x93,0x04},
197     {0x93,0x00},
198     {0x93,0x00},
199     {0x93,0x00},
200     {0x93,0x00},
201     {0x93,0x00},
202     {0x93,0x00},
203     {0x93,0x00},
204     {0x96,0x00},
205     {0x97,0x08},
206     {0x97,0x19},
207     {0x97,0x02},
208     {0x97,0x0c},
209     {0x97,0x24},
210     {0x97,0x30},
211     {0x97,0x28},
212     {0x97,0x26},
213     {0x97,0x02},
214     {0x97,0x98},
215     {0x97,0x80},
216     {0x97,0x00},
217     {0x97,0x00},
218     {0xc3,0xef},//ed
219     {0xa4,0x00},
220     {0xa8,0x00},
221
222     {0xbf, 0x00},
223     {0xba, 0xdc},
224     {0xbb, 0x08},
225     {0xb6, 0x20},
226     {0xb8, 0x30},
227     {0xb7, 0x20},
228     {0xb9, 0x30},
229     {0xb3, 0xb4},
230     {0xb4, 0xca},
231     {0xb5, 0x34},
232     {0xb0, 0x46},
233     {0xb1, 0x46},
234     {0xb2, 0x06},
235     {0xc7, 0x00},
236     {0xc6, 0x51},
237     {0xc5, 0x11},
238     {0xc4, 0x9c},
239 ////
240     {0xc0,0xc8},
241     {0xc1,0x96},
242     {0x86,0x3d},
243     {0x50,0x92},
244     {0x51,0x90},
245     {0x52,0x2c},
246     {0x53,0x00},
247     {0x54,0x00},
248     {0x55,0x88},
249     {0x57,0x00},
250     {0x5a,0x50},
251     {0x5b,0x3c},
252     {0x5c,0x00},
253     {0xc3,0xed},
254     {0x7f,0x00},
255     {0xda,0x01},
256     {0xe5,0x1f},
257     {0xe1,0x67},
258     {0xe0,0x00},
259     {0xdd,0xff},
260     {0x05,0x00},
261
262 #endif
263 #if 1
264     {0xff, 0x01},
265     {0x5d, 0x00},
266     {0x5e, 0x3c},
267     {0x5f, 0x28},
268     {0x60, 0x55},
269
270
271     {0xff, 0x00},
272     {0xc3, 0xef},
273     {0xa6, 0x00},
274     {0xa7, 0x0f},
275     {0xa7, 0x4e},
276     {0xa7, 0x7a},
277     {0xa7, 0x33},
278     {0xa7, 0x00},
279     {0xa7, 0x23},
280     {0xa7, 0x27},
281     {0xa7, 0x3a},
282     {0xa7, 0x70},
283     {0xa7, 0x33},
284     {0xa7, 0x00},//L
285     {0xa7, 0x23},
286     {0xa7, 0x20},
287     {0xa7, 0x0c},
288     {0xa7, 0x66},
289     {0xa7, 0x33},
290     {0xa7, 0x00},
291     {0xa7, 0x23},
292     {0xc3, 0xef},
293 #endif
294
295
296 #if 1
297     {0xff,0x00},
298     {0x92,0x00},
299     {0x93,0x06},
300     {0x93,0xc1},//e
301     {0x93,0x02},
302     {0x93,0x02},
303     {0x93,0x00},
304     {0x93,0x04},
305 #endif
306
307     {0x03, 0x0f},
308     {0xe0, 0x04},
309     {0xc0, 0xc8},
310     {0xc1, 0x96},
311     {0x86, 0x3d},
312     {0x50, 0x89},
313     {0x51, 0x90},
314     {0x52, 0x2c},
315     {0x53, 0x00},
316     {0x54, 0x00},
317     {0x55, 0x88},
318     {0x57, 0x00},
319     {0x5a, 0xa0},
320     {0x5b, 0x78},
321     {0x5c, 0x00},
322     {0xd3, 0x04},
323     {0xe0, 0x00},
324
325   {0x0, 0x0}   //end flag
326
327 };
328
329 /* 1600X1200 UXGA */
330 static struct reginfo sensor_uxga[] =
331 {
332     {0xff, 0x00},
333     {0xe0, 0x04},
334     {0xc0, 0xc8},
335     {0xc1, 0x96},
336     {0x86, 0x3d},
337     {0x50, 0x00},
338     {0x51, 0x90},
339     {0x52, 0x2c},
340     {0x53, 0x00},
341     {0x54, 0x00},
342     {0x55, 0x88},
343     {0x57, 0x00},
344     {0x5a, 0x90},
345     {0x5b, 0x2c},
346     {0x5c, 0x05},
347     {0xd3, 0x82},
348     {0xe0, 0x00},
349   {0x0, 0x0}   //end flag
350 };
351
352 /* 1280X1024 SXGA */
353 static struct reginfo sensor_sxga[] =
354 {
355     {0xff, 0x00},
356     {0xe0, 0x04},
357     {0xc0, 0xc8},
358     {0xc1, 0x96},
359     {0x86, 0x3d},
360     {0x50, 0x00},
361     {0x51, 0x90},
362     {0x52, 0x2c},
363     {0x53, 0x00},
364     {0x54, 0x00},
365     {0x55, 0x88},
366     {0x57, 0x00},
367     {0x5a, 0x40},
368     {0x5b, 0x00},
369     {0x5c, 0x05},
370     {0xd3, 0x82},
371     {0xe0, 0x00},
372   {0x0, 0x0}   //end flag
373 };
374
375
376 static struct reginfo sensor_xga[] =
377 {
378     {0xff, 0x00},
379     {0xe0, 0x04},
380     {0xc0, 0xc8},
381     {0xc1, 0x96},
382     {0x86, 0x3d},
383     {0x50, 0x00},
384     {0x51, 0x90},
385     {0x52, 0x2c},
386     {0x53, 0x00},
387     {0x54, 0x00},
388     {0x55, 0x88},
389     {0x57, 0x00},
390     {0x5a, 0x40},
391     {0x5b, 0x00},
392     {0x5c, 0x05},
393     {0xd3, 0x82},
394     {0xe0, 0x00},
395   {0x0, 0x0}   //end flag
396
397
398 };
399
400
401 /* 800X600 SVGA*/
402 static struct reginfo sensor_svga[] =
403 {
404   {0x0, 0x0}   //end flag
405 };
406
407 /* 640X480 VGA */
408 static struct reginfo sensor_vga[] =
409 {
410    {0x0, 0x0}   //end flag
411  };
412
413 /* 352X288 CIF */
414 static struct reginfo sensor_cif[] =
415 {
416   {0x0, 0x0}   //end flag
417 };
418
419 /* 320*240 QVGA */
420 static  struct reginfo sensor_qvga[] =
421 {
422   {0x0, 0x0}   //end flag
423 };
424
425 /* 176X144 QCIF*/
426 static struct reginfo sensor_qcif[] =
427 {
428   {0x0, 0x0}   //end flag
429 };
430 #if 0
431 /* 160X120 QQVGA*/
432 static struct reginfo ov2655_qqvga[] =
433 {
434
435     {0x300E, 0x34},
436     {0x3011, 0x01},
437     {0x3012, 0x10},
438     {0x302a, 0x02},
439     {0x302b, 0xE6},
440     {0x306f, 0x14},
441     {0x3362, 0x90},
442
443     {0x3070, 0x5d},
444     {0x3072, 0x5d},
445     {0x301c, 0x07},
446     {0x301d, 0x07},
447
448     {0x3020, 0x01},
449     {0x3021, 0x18},
450     {0x3022, 0x00},
451     {0x3023, 0x06},
452     {0x3024, 0x06},
453     {0x3025, 0x58},
454     {0x3026, 0x02},
455     {0x3027, 0x61},
456     {0x3088, 0x00},
457     {0x3089, 0xa0},
458     {0x308a, 0x00},
459     {0x308b, 0x78},
460     {0x3316, 0x64},
461     {0x3317, 0x25},
462     {0x3318, 0x80},
463     {0x3319, 0x08},
464     {0x331a, 0x0a},
465     {0x331b, 0x07},
466     {0x331c, 0x80},
467     {0x331d, 0x38},
468     {0x3100, 0x00},
469     {0x3302, 0x11},
470
471     {0x0, 0x0},
472 };
473
474
475
476 static  struct reginfo ov2655_Sharpness_auto[] =
477 {
478     {0x3306, 0x00},
479 };
480
481 static  struct reginfo ov2655_Sharpness1[] =
482 {
483     {0x3306, 0x08},
484     {0x3371, 0x00},
485 };
486
487 static  struct reginfo ov2655_Sharpness2[][3] =
488 {
489     //Sharpness 2
490     {0x3306, 0x08},
491     {0x3371, 0x01},
492 };
493
494 static  struct reginfo ov2655_Sharpness3[] =
495 {
496     //default
497     {0x3306, 0x08},
498     {0x332d, 0x02},
499 };
500 static  struct reginfo ov2655_Sharpness4[]=
501 {
502     //Sharpness 4
503     {0x3306, 0x08},
504     {0x332d, 0x03},
505 };
506
507 static  struct reginfo ov2655_Sharpness5[] =
508 {
509     //Sharpness 5
510     {0x3306, 0x08},
511     {0x332d, 0x04},
512 };
513 #endif
514
515 static  struct reginfo sensor_ClrFmt_YUYV[]=
516 {
517     //{0x4300, 0x30},
518     {0x00, 0x00}
519 };
520
521 static  struct reginfo sensor_ClrFmt_UYVY[]=
522 {
523     //{0x4300, 0x32},
524     {0x00, 0x00}
525 };
526
527 #if CONFIG_SENSOR_WhiteBalance
528 static  struct reginfo sensor_WhiteB_Auto[]=
529 {
530     {0x3406, 0x00},  //AWB auto, bit[1]:0,auto
531     {0x0000, 0x00}
532 };
533 /* Cloudy Colour Temperature : 6500K - 8000K  */
534 static  struct reginfo sensor_WhiteB_Cloudy[]=
535 {
536     {0x3406, 0x01},
537     {0x3400, 0x07},
538     {0x3401, 0x08},
539     {0x3402, 0x04},
540     {0x3403, 0x00},
541     {0x3404, 0x05},
542     {0x3405, 0x00},
543     {0x0000, 0x00}
544 };
545 /* ClearDay Colour Temperature : 5000K - 6500K  */
546 static  struct reginfo sensor_WhiteB_ClearDay[]=
547 {
548     //Sunny
549     {0x3406, 0x01},
550     {0x3400, 0x07},
551     {0x3401, 0x02},
552     {0x3402, 0x04},
553     {0x3403, 0x00},
554     {0x3404, 0x05},
555     {0x3405, 0x15},
556     {0x0000, 0x00}
557 };
558 /* Office Colour Temperature : 3500K - 5000K  */
559 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
560 {
561     //Office
562     {0x3406, 0x01},
563     {0x3400, 0x06},
564     {0x3401, 0x2a},
565     {0x3402, 0x04},
566     {0x3403, 0x00},
567     {0x3404, 0x07},
568     {0x3405, 0x24},
569     {0x0000, 0x00}
570
571 };
572 /* Home Colour Temperature : 2500K - 3500K  */
573 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
574 {
575     //Home
576     {0x3406, 0x01},
577     {0x3400, 0x04},
578     {0x3401, 0x58},
579     {0x3402, 0x04},
580     {0x3403, 0x00},
581     {0x3404, 0x07},
582     {0x3405, 0x24},
583     {0x0000, 0x00}
584 };
585 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
586     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
587 };
588 #endif
589
590 #if CONFIG_SENSOR_Brightness
591 static  struct reginfo sensor_Brightness0[]=
592 {
593     // Brightness -2
594     {0xff, 0x01},
595     {0x24, 0x34},
596     {0x25, 0x22},
597     {0x26, 0x70},
598   {0x0, 0x0}   //end flag
599 };
600
601 static  struct reginfo sensor_Brightness1[]=
602 {
603     // Brightness -1
604
605     {0xff, 0x01},
606     {0x24, 0x58},
607     {0x25, 0x50},
608     {0x26, 0x92},
609   {0x0, 0x0}   //end flag
610 };
611
612 static  struct reginfo sensor_Brightness2[]=
613 {
614     //  Brightness 0
615
616         {0xff, 0x01},
617         {0x24, 0xa8},
618         {0x25, 0x90},
619         {0x26, 0xd6},
620   {0x0, 0x0}   //end flag
621 };
622
623 static  struct reginfo sensor_Brightness3[]=
624 {
625     // Brightness +1
626
627     {0xff, 0x01},
628     {0x24, 0x48},
629     {0x25, 0x40},
630     {0x26, 0x81},
631   {0x0, 0x0}   //end flag
632 };
633
634 static  struct reginfo sensor_Brightness4[]=
635 {
636     //  Brightness +2
637
638     {0xff, 0x01},
639     {0x24, 0x58},
640     {0x25, 0x50},
641     {0x26, 0x92},
642   {0x0, 0x0}   //end flag
643 };
644
645 static  struct reginfo sensor_Brightness5[]=
646 {
647     //  Brightness +3
648     {0xff, 0x01},
649     {0x24, 0x70},
650     {0x25, 0x60},
651     {0x26, 0xa4},
652   {0x0, 0x0}   //end flag
653 };
654 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
655     sensor_Brightness4, sensor_Brightness5,NULL,
656 };
657
658 #endif
659
660 #if CONFIG_SENSOR_Effect
661 static  struct reginfo sensor_Effect_Normal[] =
662 {
663         {0xff,0x00},
664         {0x7c,0x00},
665         {0x7d,0x00},
666         {0x7c,0x05},
667         {0x7d,0x80},
668         {0x7d,0x80},
669   {0x0, 0x0}   //end flag
670 };
671
672 static  struct reginfo sensor_Effect_WandB[] =
673 {
674     {0xff,0x00},
675     {0x7c,0x00},
676     {0x7d,0x18},
677     {0x7c,0x05},
678     {0x7d,0x80},
679     {0x7d,0x80},
680   {0x0, 0x0}   //end flag
681 };
682
683 static  struct reginfo sensor_Effect_Sepia[] =
684 {
685     {0xff,0x00},
686     {0x7c,0x00},
687     {0x7d,0x18},
688     {0x7c,0x05},
689     {0x7d,0x40},
690     {0x7d,0xc0},
691   {0x0, 0x0}   //end flag
692 };
693
694 static  struct reginfo sensor_Effect_Negative[] =
695 {
696     {0xff,0x00},
697     {0x7c,0x00},
698     {0x7d,0x40},
699     {0x7c,0x05},
700     {0x7d,0x80},
701     {0x7d,0x80},
702   {0x0, 0x0}   //end flag
703 };
704 static  struct reginfo sensor_Effect_Bluish[] =
705 {
706     {0Xff, 0X00},
707     {0X7c, 0X00},
708     {0X7d, 0X18},
709     {0X7c, 0X05},
710     {0X7d, 0Xa0},
711     {0X7d, 0X40},
712   {0x0, 0x0}   //end flag
713 };
714
715 static  struct reginfo sensor_Effect_Green[] =
716 {
717     {0Xff, 0X00},
718     {0X7c, 0X00},
719     {0X7d, 0X18},
720     {0X7c, 0X05},
721     {0X7d, 0X40},
722     {0X7d, 0X40},
723   {0x0, 0x0}   //end flag
724 };
725
726 static  struct reginfo sensor_Effect_Exp_Windows_Half[] =
727 {
728         {0xff, 0x01},
729         {0x5d, 0x00},
730         {0x5e, 0x3c},
731         {0x5f, 0x28},
732         {0x60, 0x55},
733   {0x0, 0x0}   //end flag
734 };
735 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
736     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
737 };
738 #endif
739 #if CONFIG_SENSOR_Exposure
740 static  struct reginfo sensor_Exposure0[]=
741 {
742         {0x0000, 0x00}
743 };
744
745 static  struct reginfo sensor_Exposure1[]=
746 {
747     {0x0000, 0x00}
748 };
749
750 static  struct reginfo sensor_Exposure2[]=
751 {
752     {0x0000, 0x00}
753 };
754
755 static  struct reginfo sensor_Exposure3[]=
756 {
757     {0x0000, 0x00}
758 };
759
760 static  struct reginfo sensor_Exposure4[]=
761 {
762     {0x0000, 0x00}
763 };
764
765 static  struct reginfo sensor_Exposure5[]=
766 {
767     {0x0000, 0x00}
768 };
769
770 static  struct reginfo sensor_Exposure6[]=
771 {
772     {0x0000, 0x00}
773 };
774
775 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
776     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
777 };
778 #endif
779 #if CONFIG_SENSOR_Saturation
780 static  struct reginfo sensor_Saturation0[]=
781 {
782         {0xff, 0x00},
783         {0x90, 0x00},
784         {0x91, 0x0e},
785         {0x91, 0x1a},
786         {0x91, 0x31},
787         {0x91, 0x5a},
788         {0x91, 0x69},
789         {0x91, 0x75},
790         {0x91, 0x7e},
791         {0x91, 0x88},
792         {0x91, 0x8f},
793         {0x91, 0x96},
794         {0x91, 0xa3},
795         {0x91, 0xaf},
796         {0x91, 0xc4},
797         {0x91, 0xd7},
798         {0x91, 0xe8},
799         {0x91, 0x20},
800   {0x0, 0x0}   //end flag
801 };
802
803 static  struct reginfo sensor_Saturation1[]=
804 {
805     {0xff, 0x00},
806     {0x90, 0x00},
807     {0x91, 0x03},
808     {0x91, 0x0a},
809     {0x91, 0x1a},
810     {0x91, 0x3f},
811     {0x91, 0x4e},
812     {0x91, 0x5b},
813     {0x91, 0x68},
814     {0x91, 0x75},
815     {0x91, 0x7f},
816     {0x91, 0x89},
817     {0x91, 0x9a},
818     {0x91, 0xa6},
819     {0x91, 0xbd},
820     {0x91, 0xd3},
821     {0x91, 0xe5},
822     {0x91, 0x24},
823   {0x0, 0x0}   //end flag
824 };
825
826 static  struct reginfo sensor_Saturation2[]=
827 {
828     {0xff, 0x00},
829     {0x90, 0x00},
830     {0x91, 0x04},
831     {0x91, 0x07},
832     {0x91, 0x10},
833     {0x91, 0x28},
834     {0x91, 0x36},
835     {0x91, 0x44},
836     {0x91, 0x52},
837     {0x91, 0x60},
838     {0x91, 0x6c},
839     {0x91, 0x78},
840     {0x91, 0x8c},
841     {0x91, 0x9e},
842     {0x91, 0xbb},
843     {0x91, 0xd3},
844     {0x91, 0xe5},
845     {0x91, 0x24},
846   {0x0, 0x0}   //end flag       
847 };
848 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
849
850
851 #endif
852 #if CONFIG_SENSOR_Contrast
853 static  struct reginfo sensor_Contrast0[]=
854 {
855     {0xff, 0x00},
856     {0x7c, 0x00},
857     {0x7d, 0x04},
858     {0x7c, 0x07},
859     {0x7d, 0x20},
860     {0x7d, 0x10},
861     {0x7d, 0x4a},
862     {0x7d, 0x06},
863   {0x0, 0x0}   //end flag
864
865 };
866
867 static  struct reginfo sensor_Contrast1[]=
868 {
869     {0xff, 0x00},
870     {0x7c, 0x00},
871     {0x7d, 0x04},
872     {0x7c, 0x07},
873     {0x7d, 0x20},
874     {0x7d, 0x14},
875     {0x7d, 0x40},
876     {0x7d, 0x06},
877   {0x0, 0x0}   //end flag
878 };
879
880 static  struct reginfo sensor_Contrast2[]=
881 {
882     {0xff, 0x00},
883     {0x7c, 0x00},
884     {0x7d, 0x04},
885     {0x7c, 0x07},
886     {0x7d, 0x20},
887     {0x7d, 0x18},
888     {0x7d, 0x34},
889     {0x7d, 0x06},
890   {0x0, 0x0}   //end flag
891 };
892
893 static  struct reginfo sensor_Contrast3[]=
894 {
895     {0xff, 0x00},
896     {0x7c, 0x00},
897     {0x7d, 0x04},
898     {0x7c, 0x07},
899     {0x7d, 0x20},
900     {0x7d, 0x1c},
901     {0x7d, 0x2a},
902     {0x7d, 0x06},
903   {0x0, 0x0}   //end flag
904 };
905
906 static  struct reginfo sensor_Contrast4[]=
907 {
908     {0xff,0x00},
909     {0x7c,0x00},
910     {0x7d,0x04},
911     {0x7c,0x07},
912     {0x7d,0x20},
913     {0x7d,0x24},
914     {0x7d,0x16},
915     {0x7d,0x06},
916   {0x0, 0x0}   //end flag
917 };
918
919
920 static  struct reginfo sensor_Contrast5[]=
921 {
922     {0xff, 0x00},
923     {0x7c, 0x00},
924     {0x7d, 0x04},
925     {0x7c, 0x07},
926     {0x7d, 0x20},
927     {0x7d, 0x20},
928     {0x7d, 0x20},
929     {0x7d, 0x06},
930   {0x0, 0x0}   //end flag
931 };
932
933 static  struct reginfo sensor_Contrast6[]=
934 {
935     {0xff, 0x00},
936     {0x7c, 0x00},
937     {0x7d, 0x04},
938     {0x7c, 0x07},
939     {0x7d, 0x20},
940     {0x7d, 0x24},
941     {0x7d, 0x16},
942     {0x7d, 0x06},
943   {0x0, 0x0}   //end flag
944 };
945
946
947 static  struct reginfo sensor_Contrast7[]=
948 {
949     {0xff, 0x00},
950     {0x7c, 0x00},
951     {0x7d, 0x04},
952     {0x7c, 0x07},
953     {0x7d, 0x20},
954     {0x7d, 0x28},
955     {0x7d, 0x0c},
956     {0x7d, 0x06},
957   {0x0, 0x0}   //end flag
958 };
959
960 static  struct reginfo sensor_Contrast8[]=
961 {
962     {0xff, 0x00},
963     {0x7c, 0x00},
964     {0x7d, 0x04},
965     {0x7c, 0x07},
966     {0x7d, 0x20},
967     {0x7d, 0x2c},
968     {0x7d, 0x02},
969     {0x7d, 0x06},
970   {0x0, 0x0}   //end flag
971 };
972
973 static  struct reginfo sensor_Contrast9[]=
974 {
975     {0xff, 0x00},
976     {0x7c, 0x00},
977     {0x7d, 0x04},
978     {0x7c, 0x07},
979     {0x7d, 0x20},
980     {0x7d, 0x30},
981     {0x7d, 0x08},
982     {0x7d, 0x0e},
983   {0x0, 0x0}   //end flag
984 };
985
986
987
988 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
989     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
990 };
991
992 #endif
993 #if CONFIG_SENSOR_Mirror
994 static  struct reginfo sensor_MirrorOn[]=
995 {
996     {0x0000, 0x00}
997 };
998
999 static  struct reginfo sensor_MirrorOff[]=
1000 {
1001     {0x0000, 0x00}
1002 };
1003 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1004 #endif
1005 #if CONFIG_SENSOR_Flip
1006 static  struct reginfo sensor_FlipOn[]=
1007 {
1008     {0x0000, 0x00}
1009 };
1010
1011 static  struct reginfo sensor_FlipOff[]=
1012 {
1013     {0x0000, 0x00}
1014 };
1015 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1016
1017 #endif
1018 #if CONFIG_SENSOR_Scene
1019 static  struct reginfo sensor_SceneAuto[] =
1020 {
1021     {0x3a00, 0x78},
1022     {0x0000, 0x00}
1023 };
1024
1025 static  struct reginfo sensor_SceneNight[] =
1026 {
1027     {0x3003, 0x80},
1028         {0x3004, 0x20},
1029         {0x3005, 0x18},
1030         {0x3006, 0x0d},
1031         {0x3a00, 0x7c},
1032         {0x3a02 ,0x07},
1033         {0x3a03 ,0x38},
1034         {0x3a14 ,0x07},
1035         {0x3a15 ,0x38},
1036     {0x0000, 0x00}
1037 };
1038 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1039
1040 #endif
1041 #if CONFIG_SENSOR_DigitalZoom
1042 static struct reginfo sensor_Zoom0[] =
1043 {
1044     {0x0, 0x0},
1045 };
1046
1047 static struct reginfo sensor_Zoom1[] =
1048 {
1049      {0x0, 0x0},
1050 };
1051
1052 static struct reginfo sensor_Zoom2[] =
1053 {
1054     {0x0, 0x0},
1055 };
1056
1057
1058 static struct reginfo sensor_Zoom3[] =
1059 {
1060     {0x0, 0x0},
1061 };
1062 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1063 #endif
1064 static const struct v4l2_querymenu sensor_menus[] =
1065 {
1066         #if CONFIG_SENSOR_WhiteBalance
1067     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1068     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1069     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1070     #endif
1071
1072         #if CONFIG_SENSOR_Effect
1073     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1074     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1075     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
1076     #endif
1077
1078         #if CONFIG_SENSOR_Scene
1079     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1080     #endif
1081
1082         #if CONFIG_SENSOR_Flash
1083     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1084     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1085     #endif
1086 };
1087
1088 static const struct v4l2_queryctrl sensor_controls[] =
1089 {
1090         #if CONFIG_SENSOR_WhiteBalance
1091     {
1092         .id             = V4L2_CID_DO_WHITE_BALANCE,
1093         .type           = V4L2_CTRL_TYPE_MENU,
1094         .name           = "White Balance Control",
1095         .minimum        = 0,
1096         .maximum        = 4,
1097         .step           = 1,
1098         .default_value = 0,
1099     },
1100     #endif
1101
1102         #if CONFIG_SENSOR_Brightness
1103         {
1104         .id             = V4L2_CID_BRIGHTNESS,
1105         .type           = V4L2_CTRL_TYPE_INTEGER,
1106         .name           = "Brightness Control",
1107         .minimum        = -3,
1108         .maximum        = 2,
1109         .step           = 1,
1110         .default_value = 0,
1111     },
1112     #endif
1113
1114         #if CONFIG_SENSOR_Effect
1115         {
1116         .id             = V4L2_CID_EFFECT,
1117         .type           = V4L2_CTRL_TYPE_MENU,
1118         .name           = "Effect Control",
1119         .minimum        = 0,
1120         .maximum        = 5,
1121         .step           = 1,
1122         .default_value = 0,
1123     },
1124         #endif
1125
1126         #if CONFIG_SENSOR_Exposure
1127         {
1128         .id             = V4L2_CID_EXPOSURE,
1129         .type           = V4L2_CTRL_TYPE_INTEGER,
1130         .name           = "Exposure Control",
1131         .minimum        = 0,
1132         .maximum        = 6,
1133         .step           = 1,
1134         .default_value = 0,
1135     },
1136         #endif
1137
1138         #if CONFIG_SENSOR_Saturation
1139         {
1140         .id             = V4L2_CID_SATURATION,
1141         .type           = V4L2_CTRL_TYPE_INTEGER,
1142         .name           = "Saturation Control",
1143         .minimum        = 0,
1144         .maximum        = 2,
1145         .step           = 1,
1146         .default_value = 0,
1147     },
1148     #endif
1149
1150         #if CONFIG_SENSOR_Contrast
1151         {
1152         .id             = V4L2_CID_CONTRAST,
1153         .type           = V4L2_CTRL_TYPE_INTEGER,
1154         .name           = "Contrast Control",
1155         .minimum        = -3,
1156         .maximum        = 3,
1157         .step           = 1,
1158         .default_value = 0,
1159     },
1160         #endif
1161
1162         #if CONFIG_SENSOR_Mirror
1163         {
1164         .id             = V4L2_CID_HFLIP,
1165         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1166         .name           = "Mirror Control",
1167         .minimum        = 0,
1168         .maximum        = 1,
1169         .step           = 1,
1170         .default_value = 1,
1171     },
1172     #endif
1173
1174         #if CONFIG_SENSOR_Flip
1175         {
1176         .id             = V4L2_CID_VFLIP,
1177         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1178         .name           = "Flip Control",
1179         .minimum        = 0,
1180         .maximum        = 1,
1181         .step           = 1,
1182         .default_value = 1,
1183     },
1184     #endif
1185
1186         #if CONFIG_SENSOR_Scene
1187     {
1188         .id             = V4L2_CID_SCENE,
1189         .type           = V4L2_CTRL_TYPE_MENU,
1190         .name           = "Scene Control",
1191         .minimum        = 0,
1192         .maximum        = 1,
1193         .step           = 1,
1194         .default_value = 0,
1195     },
1196     #endif
1197
1198         #if CONFIG_SENSOR_DigitalZoom
1199     {
1200         .id             = V4L2_CID_ZOOM_RELATIVE,
1201         .type           = V4L2_CTRL_TYPE_INTEGER,
1202         .name           = "DigitalZoom Control",
1203         .minimum        = -1,
1204         .maximum        = 1,
1205         .step           = 1,
1206         .default_value = 0,
1207     }, {
1208         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1209         .type           = V4L2_CTRL_TYPE_INTEGER,
1210         .name           = "DigitalZoom Control",
1211         .minimum        = 0,
1212         .maximum        = 3,
1213         .step           = 1,
1214         .default_value = 0,
1215     },
1216     #endif
1217
1218         #if CONFIG_SENSOR_Focus
1219         {
1220         .id             = V4L2_CID_FOCUS_RELATIVE,
1221         .type           = V4L2_CTRL_TYPE_INTEGER,
1222         .name           = "Focus Control",
1223         .minimum        = -1,
1224         .maximum        = 1,
1225         .step           = 1,
1226         .default_value = 0,
1227     }, {
1228         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1229         .type           = V4L2_CTRL_TYPE_INTEGER,
1230         .name           = "Focus Control",
1231         .minimum        = 0,
1232         .maximum        = 255,
1233         .step           = 1,
1234         .default_value = 125,
1235     },
1236     #endif
1237
1238         #if CONFIG_SENSOR_Flash
1239         {
1240         .id             = V4L2_CID_FLASH,
1241         .type           = V4L2_CTRL_TYPE_MENU,
1242         .name           = "Flash Control",
1243         .minimum        = 0,
1244         .maximum        = 3,
1245         .step           = 1,
1246         .default_value = 0,
1247     },
1248         #endif
1249 };
1250
1251 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1252 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1253 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1254 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1255 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1256 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1257 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1258 static int sensor_resume(struct soc_camera_device *icd);
1259 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1260 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1261 #if CONFIG_SENSOR_Effect
1262 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1263 #endif
1264 #if CONFIG_SENSOR_WhiteBalance
1265 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1266 #endif
1267 static int sensor_deactivate(struct i2c_client *client);
1268
1269 static struct soc_camera_ops sensor_ops =
1270 {
1271     .suspend                     = sensor_suspend,
1272     .resume                       = sensor_resume,
1273     .set_bus_param              = sensor_set_bus_param,
1274     .query_bus_param    = sensor_query_bus_param,
1275     .controls           = sensor_controls,
1276     .menus                         = sensor_menus,
1277     .num_controls               = ARRAY_SIZE(sensor_controls),
1278     .num_menus          = ARRAY_SIZE(sensor_menus),
1279 };
1280
1281 /* only one fixed colorspace per pixelcode */
1282 struct sensor_datafmt {
1283         enum v4l2_mbus_pixelcode code;
1284         enum v4l2_colorspace colorspace;
1285 };
1286
1287 /* Find a data format by a pixel code in an array */
1288 static const struct sensor_datafmt *sensor_find_datafmt(
1289         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1290         int n)
1291 {
1292         int i;
1293         for (i = 0; i < n; i++)
1294                 if (fmt[i].code == code)
1295                         return fmt + i;
1296
1297         return NULL;
1298 }
1299
1300 static const struct sensor_datafmt sensor_colour_fmts[] = {
1301     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1302     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1303 };
1304
1305 typedef struct sensor_info_priv_s
1306 {
1307     int whiteBalance;
1308     int brightness;
1309     int contrast;
1310     int saturation;
1311     int effect;
1312     int scene;
1313     int digitalzoom;
1314     int focus;
1315     int flash;
1316     int exposure;
1317         bool snap2preview;
1318         bool video2preview;
1319     unsigned char mirror;                                        /* HFLIP */
1320     unsigned char flip;                                          /* VFLIP */
1321     unsigned int winseqe_cur_addr;
1322     struct sensor_datafmt fmt;
1323
1324 } sensor_info_priv_t;
1325
1326 struct sensor
1327 {
1328     struct v4l2_subdev subdev;
1329     struct i2c_client *client;
1330     sensor_info_priv_t info_priv;
1331     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1332 #if CONFIG_SENSOR_I2C_NOSCHED
1333         atomic_t tasklock_cnt;
1334 #endif
1335         struct rkcamera_platform_data *sensor_io_request;
1336     struct rkcamera_gpio_res *sensor_gpio_res;
1337 };
1338
1339 static struct sensor* to_sensor(const struct i2c_client *client)
1340 {
1341     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1342 }
1343
1344 static int sensor_task_lock(struct i2c_client *client, int lock)
1345 {
1346 #if CONFIG_SENSOR_I2C_NOSCHED
1347         int cnt = 3;
1348     struct sensor *sensor = to_sensor(client);
1349
1350         if (lock) {
1351                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1352                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1353                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1354                                 msleep(35);
1355                                 cnt--;
1356                         }
1357                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1358                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1359                                 goto sensor_task_lock_err;
1360                         }
1361                         preempt_disable();
1362                 }
1363
1364                 atomic_add(1, &sensor->tasklock_cnt);
1365         } else {
1366                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1367                         atomic_sub(1, &sensor->tasklock_cnt);
1368
1369                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1370                                 preempt_enable();
1371                 }
1372         }
1373         return 0;
1374 sensor_task_lock_err:
1375         return -1;  
1376 #else
1377     return 0;
1378 #endif
1379
1380 }
1381 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1382 {
1383     int err,cnt;
1384     u8 buf[2];
1385     struct i2c_msg msg[1];
1386
1387     buf[0] = reg & 0xFF;
1388     buf[1] = val;
1389
1390     msg->addr = client->addr;
1391     msg->flags = client->flags;
1392     msg->buf = buf;
1393     msg->len = sizeof(buf);
1394     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
1395     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1396
1397     cnt = 3;
1398     err = -EAGAIN;
1399
1400     while ((cnt-->0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1401         err = i2c_transfer(client->adapter, msg, 1);
1402
1403         if (err >= 0) {
1404             return 0;
1405         } else {
1406             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1407             udelay(10);
1408         }
1409     }
1410
1411     return err;
1412 }
1413
1414 /* sensor register read */
1415 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1416 {
1417     int err,cnt;
1418     u8 buf[1];
1419     struct i2c_msg msg[2];
1420
1421     buf[0] = reg ;//>> 8;
1422    // buf[1] = 0;
1423
1424     msg[0].addr = client->addr;
1425     msg[0].flags = client->flags;
1426     msg[0].buf = buf;
1427     msg[0].len = sizeof(buf);
1428     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1429     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 */
1430
1431     msg[1].addr = client->addr;
1432     msg[1].flags = client->flags|I2C_M_RD;
1433     msg[1].buf = buf;
1434     msg[1].len = 1;
1435     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1436     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1437
1438     cnt = 3;
1439     err = -EAGAIN;
1440     while ((cnt-->0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1441         err = i2c_transfer(client->adapter, msg, 2);
1442
1443         if (err >= 0) {
1444             *val = buf[0];
1445             return 0;
1446         } else {
1447                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1448             udelay(10);
1449         }
1450     }
1451
1452     return err;
1453 }
1454
1455 /* write a array of registers  */
1456 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1457 {
1458     int err = 0, cnt;
1459     int i = 0;
1460 #if CONFIG_SENSOR_I2C_RDWRCHK    
1461         char valchk;
1462 #endif
1463
1464         cnt = 0;
1465         if (sensor_task_lock(client, 1) < 0)
1466                 goto sensor_write_array_end;
1467     while (regarray[i].reg != 0)
1468     {
1469         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1470         if (err < 0)
1471         {
1472             if (cnt-- > 0) {
1473                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1474                                 i = 0;
1475                                 continue;
1476             } else {
1477                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1478                 err = -EPERM;
1479                                 goto sensor_write_array_end;
1480             }
1481         } else {
1482         #if CONFIG_SENSOR_I2C_RDWRCHK
1483                         sensor_read(client, regarray[i].reg, &valchk);
1484                         if (valchk != regarray[i].val)
1485                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1486                 #endif
1487         }
1488         i++;
1489     }
1490
1491 sensor_write_array_end:
1492         sensor_task_lock(client,0);
1493         return err;
1494 }
1495 #if CONFIG_SENSOR_I2C_RDWRCHK
1496 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1497 {
1498     int cnt;
1499     int i = 0;
1500         char valchk;
1501
1502         cnt = 0;
1503         valchk = 0;
1504     while (regarray[i].reg != 0)
1505     {
1506                 sensor_read(client, regarray[i].reg, &valchk);
1507                 if (valchk != regarray[i].val)
1508                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1509
1510         i++;
1511     }
1512     return 0;
1513 }
1514 #endif
1515 static int sensor_ioctrl(struct soc_camera_device *icd,enum rksensor_power_cmd cmd, int on)
1516 {
1517         struct soc_camera_link *icl = to_soc_camera_link(icd);
1518         int ret = 0;
1519
1520     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1521
1522         switch (cmd)
1523         {
1524                 case Sensor_PowerDown:
1525                 {
1526                         if (icl->powerdown) {
1527                                 ret = icl->powerdown(icd->pdev, on);
1528                                 if (ret == RK_CAM_IO_SUCCESS) {
1529                                         if (on == 0) {
1530                                                 mdelay(2);
1531                                                 if (icl->reset)
1532                                                         icl->reset(icd->pdev);
1533                                         }
1534                                 } else if (ret == RK_CAM_EIO_REQUESTFAIL) {
1535                                         ret = -ENODEV;
1536                                         goto sensor_power_end;
1537                                 }
1538                         }
1539                         break;
1540                 }
1541                 case Sensor_Flash:
1542                 {
1543                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1544                 struct sensor *sensor = to_sensor(client);
1545
1546                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1547                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1548                         }
1549             break;
1550                 }
1551                 default:
1552                 {
1553                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1554                         break;
1555                 }
1556         }
1557 sensor_power_end:
1558         return ret;
1559 }
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 rkcamera_platform_data* tmp_plat_data =(struct rkcamera_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) { 
1575                 for(i = 0;i < RK_CAM_NUM_PER_HOST;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  && (i < RK_CAM_NUM_PER_HOST) && 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 RK_CAM_SUBDEV_DEACTIVATE:
2839                 {
2840                         sensor_deactivate(client);
2841                         break;
2842                 }
2843
2844                 case RK_CAM_SUBDEV_IOREQUEST:
2845                 {
2846                         sensor->sensor_io_request = (struct rkcamera_platform_data*)arg;           
2847             if (sensor->sensor_io_request != NULL) { 
2848                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
2849                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2850                     sensor->sensor_gpio_res = (struct rkcamera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2851                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
2852                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2853                     sensor->sensor_gpio_res = (struct rkcamera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2854                 }
2855             } else {
2856                 SENSOR_TR("%s %s RK_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2857                 ret = -EINVAL;
2858                 goto sensor_ioctl_end;
2859             }
2860             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2861                for this project */
2862             #if CONFIG_SENSOR_Flash     
2863                 if (sensor->sensor_gpio_res) { 
2864                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2865                     for (i = 0; i < icd->ops->num_controls; i++) {
2866                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2867                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2868                                 }
2869                     }
2870                     sensor->info_priv.flash = 0xff;
2871                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2872                 }
2873                 }
2874             #endif
2875                         break;
2876                 }
2877                 default:
2878                 {
2879                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2880                         break;
2881                 }
2882         }
2883
2884 sensor_ioctl_end:
2885         return ret;
2886
2887 }
2888 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2889                             enum v4l2_mbus_pixelcode *code)
2890 {
2891         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2892                 return -EINVAL;
2893
2894         *code = sensor_colour_fmts[index].code;
2895         return 0;
2896 }
2897 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2898         .init           = sensor_init,
2899         .g_ctrl         = sensor_g_control,
2900         .s_ctrl         = sensor_s_control,
2901         .g_ext_ctrls          = sensor_g_ext_controls,
2902         .s_ext_ctrls          = sensor_s_ext_controls,
2903         .g_chip_ident   = sensor_g_chip_ident,
2904         .ioctl = sensor_ioctl,
2905 };
2906
2907 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2908         .s_mbus_fmt     = sensor_s_fmt,
2909         .g_mbus_fmt     = sensor_g_fmt,
2910         .try_mbus_fmt   = sensor_try_fmt,
2911         .enum_mbus_fmt  = sensor_enum_fmt,
2912 };
2913
2914 static struct v4l2_subdev_ops sensor_subdev_ops = {
2915         .core   = &sensor_subdev_core_ops,
2916         .video = &sensor_subdev_video_ops,
2917 };
2918
2919 static int sensor_probe(struct i2c_client *client,
2920                          const struct i2c_device_id *did)
2921 {
2922     struct sensor *sensor;
2923     struct soc_camera_device *icd = client->dev.platform_data;
2924     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2925     struct soc_camera_link *icl;
2926     int ret;
2927
2928     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2929     if (!icd) {
2930         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2931         return -EINVAL;
2932     }
2933
2934     icl = to_soc_camera_link(icd);
2935     if (!icl) {
2936         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2937         return -EINVAL;
2938     }
2939
2940     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2941         dev_warn(&adapter->dev,
2942                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2943         return -EIO;
2944     }
2945
2946     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2947     if (!sensor)
2948         return -ENOMEM;
2949
2950     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2951
2952     /* Second stage probe - when a capture adapter is there */
2953     icd->ops            = &sensor_ops;
2954     sensor->info_priv.fmt = sensor_colour_fmts[0];
2955         #if CONFIG_SENSOR_I2C_NOSCHED
2956         atomic_set(&sensor->tasklock_cnt,0);
2957         #endif
2958
2959     ret = sensor_video_probe(icd, client);
2960     if (ret < 0) {
2961         icd->ops = NULL;
2962         i2c_set_clientdata(client, NULL);
2963         kfree(sensor);
2964                 sensor = NULL;
2965     }
2966     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2967     return ret;
2968 }
2969
2970 static int sensor_remove(struct i2c_client *client)
2971 {
2972     struct sensor *sensor = to_sensor(client);
2973     struct soc_camera_device *icd = client->dev.platform_data;
2974
2975     icd->ops = NULL;
2976     i2c_set_clientdata(client, NULL);
2977     client->driver = NULL;
2978     kfree(sensor);
2979         sensor = NULL;
2980     return 0;
2981 }
2982
2983 static const struct i2c_device_id sensor_id[] = {
2984         {SENSOR_NAME_STRING(), 0 },
2985         { }
2986 };
2987 MODULE_DEVICE_TABLE(i2c, sensor_id);
2988
2989 static struct i2c_driver sensor_i2c_driver = {
2990         .driver = {
2991                 .name = SENSOR_NAME_STRING(),
2992         },
2993         .probe          = sensor_probe,
2994         .remove         = sensor_remove,
2995         .id_table       = sensor_id,
2996 };
2997
2998 static int __init sensor_mod_init(void)
2999 {
3000     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3001     return i2c_add_driver(&sensor_i2c_driver);
3002 }
3003
3004 static void __exit sensor_mod_exit(void)
3005 {
3006     i2c_del_driver(&sensor_i2c_driver);
3007 }
3008
3009 device_initcall_sync(sensor_mod_init);
3010 module_exit(sensor_mod_exit);
3011
3012 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3013 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3014 MODULE_LICENSE("GPL");
3015
3016
3017