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