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