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