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