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