camera: all sensor driver close debug trace
[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(1, 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                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
1966     }
1967
1968     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
1969         #if CONFIG_SENSOR_Flash
1970         if (sensor_fmt_capturechk(sd,f) == true) {      /* ddl@rock-chips.com : Capture */
1971             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1972                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1973                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1974             }           
1975         } else {                                        /* ddl@rock-chips.com : Video */
1976             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1977                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1978                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1979             }
1980         }
1981         #endif
1982         ret |= sensor_write_array(client, winseqe_set_addr);
1983         if (ret != 0) {
1984             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1985             #if CONFIG_SENSOR_Flash
1986             if (sensor_fmt_capturechk(sd,f) == true) {
1987                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1988                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1989                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1990                 }
1991             }
1992             #endif
1993             goto sensor_s_fmt_end;
1994         }
1995
1996         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
1997
1998
1999         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2000     }
2001     else
2002     {
2003         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2004     }
2005
2006         pix->width = set_w;
2007     pix->height = set_h;
2008
2009 sensor_s_fmt_end:
2010     return ret;
2011 }
2012
2013 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2014 {
2015     struct v4l2_pix_format *pix = &f->fmt.pix;
2016     bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
2017         pix->pixelformat == V4L2_PIX_FMT_YUYV;
2018
2019     /*
2020     * With Bayer format enforce even side lengths, but let the user play
2021     * with the starting pixel
2022     */
2023
2024     if (pix->height > SENSOR_MAX_HEIGHT)
2025         pix->height = SENSOR_MAX_HEIGHT;
2026     else if (pix->height < SENSOR_MIN_HEIGHT)
2027         pix->height = SENSOR_MIN_HEIGHT;
2028     else if (bayer)
2029         pix->height = ALIGN(pix->height, 2);
2030
2031     if (pix->width > SENSOR_MAX_WIDTH)
2032         pix->width = SENSOR_MAX_WIDTH;
2033     else if (pix->width < SENSOR_MIN_WIDTH)
2034         pix->width = SENSOR_MIN_WIDTH;
2035     else if (bayer)
2036         pix->width = ALIGN(pix->width, 2);
2037
2038     return 0;
2039 }
2040
2041  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2042 {
2043     struct i2c_client *client = sd->priv;
2044
2045     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2046         return -EINVAL;
2047
2048     if (id->match.addr != client->addr)
2049         return -ENODEV;
2050
2051     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV9650  identifier */
2052     id->revision = 0;
2053
2054     return 0;
2055 }
2056 #if CONFIG_SENSOR_Brightness
2057 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2058 {
2059     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2060
2061     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2062     {
2063         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2064         {
2065             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2066             {
2067                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2068                 return -EINVAL;
2069             }
2070             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2071             return 0;
2072         }
2073     }
2074         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2075     return -EINVAL;
2076 }
2077 #endif
2078 #if CONFIG_SENSOR_Effect
2079 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2080 {
2081     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2082
2083     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2084     {
2085         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2086         {
2087             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2088             {
2089                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2090                 return -EINVAL;
2091             }
2092             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2093             return 0;
2094         }
2095     }
2096         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2097     return -EINVAL;
2098 }
2099 #endif
2100 #if CONFIG_SENSOR_Exposure
2101 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2102 {
2103     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2104
2105     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2106     {
2107         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2108         {
2109             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2110             {
2111                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2112                 return -EINVAL;
2113             }
2114             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2115             return 0;
2116         }
2117     }
2118         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2119     return -EINVAL;
2120 }
2121 #endif
2122 #if CONFIG_SENSOR_Saturation
2123 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2124 {
2125     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2126
2127     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2128     {
2129         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2130         {
2131             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2132             {
2133                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2134                 return -EINVAL;
2135             }
2136             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2137             return 0;
2138         }
2139     }
2140     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2141     return -EINVAL;
2142 }
2143 #endif
2144 #if CONFIG_SENSOR_Contrast
2145 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2146 {
2147     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2148
2149     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2150     {
2151         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2152         {
2153             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2154             {
2155                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2156                 return -EINVAL;
2157             }
2158             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2159             return 0;
2160         }
2161     }
2162     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2163     return -EINVAL;
2164 }
2165 #endif
2166 #if CONFIG_SENSOR_Mirror
2167 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2168 {
2169     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2170
2171     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2172     {
2173         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2174         {
2175             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2176             {
2177                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2178                 return -EINVAL;
2179             }
2180             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2181             return 0;
2182         }
2183     }
2184     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2185     return -EINVAL;
2186 }
2187 #endif
2188 #if CONFIG_SENSOR_Flip
2189 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2190 {
2191     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2192
2193     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2194     {
2195         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2196         {
2197             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2198             {
2199                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2200                 return -EINVAL;
2201             }
2202             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2203             return 0;
2204         }
2205     }
2206     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2207     return -EINVAL;
2208 }
2209 #endif
2210 #if CONFIG_SENSOR_Scene
2211 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2212 {
2213     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2214
2215     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2216     {
2217         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2218         {
2219             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2220             {
2221                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2222                 return -EINVAL;
2223             }
2224             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2225             return 0;
2226         }
2227     }
2228     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2229     return -EINVAL;
2230 }
2231 #endif
2232 #if CONFIG_SENSOR_WhiteBalance
2233 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2234 {
2235     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2236
2237     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2238     {
2239         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2240         {
2241             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2242             {
2243                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2244                 return -EINVAL;
2245             }
2246             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2247             return 0;
2248         }
2249     }
2250         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2251     return -EINVAL;
2252 }
2253 #endif
2254 #if CONFIG_SENSOR_DigitalZoom
2255 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2256 {
2257     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2258     struct sensor *sensor = to_sensor(client);
2259         const struct v4l2_queryctrl *qctrl_info;
2260     int digitalzoom_cur, digitalzoom_total;
2261
2262         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2263         if (qctrl_info)
2264                 return -EINVAL;
2265
2266     digitalzoom_cur = sensor->info_priv.digitalzoom;
2267     digitalzoom_total = qctrl_info->maximum;
2268
2269     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2270     {
2271         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2272         return -EINVAL;
2273     }
2274
2275     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2276     {
2277         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2278         return -EINVAL;
2279     }
2280
2281     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2282     {
2283         *value = digitalzoom_total - digitalzoom_cur;
2284     }
2285
2286     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2287     {
2288         *value = 0 - digitalzoom_cur;
2289     }
2290
2291     digitalzoom_cur += *value;
2292
2293     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2294     {
2295         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2296         {
2297             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2298             return -EINVAL;
2299         }
2300         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2301         return 0;
2302     }
2303
2304     return -EINVAL;
2305 }
2306 #endif
2307 #if CONFIG_SENSOR_Flash
2308 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2309 {    
2310     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2311         if (value == 3) {       /* ddl@rock-chips.com: torch */
2312             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2313         } else {
2314             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2315         }
2316         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2317         return 0;
2318     }
2319     
2320         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2321     return -EINVAL;
2322 }
2323 #endif
2324
2325 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2326 {
2327     struct i2c_client *client = sd->priv;
2328     struct sensor *sensor = to_sensor(client);
2329     const struct v4l2_queryctrl *qctrl;
2330
2331     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2332
2333     if (!qctrl)
2334     {
2335         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2336         return -EINVAL;
2337     }
2338
2339     switch (ctrl->id)
2340     {
2341         case V4L2_CID_BRIGHTNESS:
2342             {
2343                 ctrl->value = sensor->info_priv.brightness;
2344                 break;
2345             }
2346         case V4L2_CID_SATURATION:
2347             {
2348                 ctrl->value = sensor->info_priv.saturation;
2349                 break;
2350             }
2351         case V4L2_CID_CONTRAST:
2352             {
2353                 ctrl->value = sensor->info_priv.contrast;
2354                 break;
2355             }
2356         case V4L2_CID_DO_WHITE_BALANCE:
2357             {
2358                 ctrl->value = sensor->info_priv.whiteBalance;
2359                 break;
2360             }
2361         case V4L2_CID_EXPOSURE:
2362             {
2363                 ctrl->value = sensor->info_priv.exposure;
2364                 break;
2365             }
2366         case V4L2_CID_HFLIP:
2367             {
2368                 ctrl->value = sensor->info_priv.mirror;
2369                 break;
2370             }
2371         case V4L2_CID_VFLIP:
2372             {
2373                 ctrl->value = sensor->info_priv.flip;
2374                 break;
2375             }
2376         default :
2377                 break;
2378     }
2379     return 0;
2380 }
2381
2382
2383
2384 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2385 {
2386     struct i2c_client *client = sd->priv;
2387     struct sensor *sensor = to_sensor(client);
2388     struct soc_camera_device *icd = client->dev.platform_data;
2389     const struct v4l2_queryctrl *qctrl;
2390
2391
2392     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2393
2394     if (!qctrl)
2395     {
2396         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2397         return -EINVAL;
2398     }
2399
2400     switch (ctrl->id)
2401     {
2402 #if CONFIG_SENSOR_Brightness
2403         case V4L2_CID_BRIGHTNESS:
2404             {
2405                 if (ctrl->value != sensor->info_priv.brightness)
2406                 {
2407                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2408                     {
2409                         return -EINVAL;
2410                     }
2411                     sensor->info_priv.brightness = ctrl->value;
2412                 }
2413                 break;
2414             }
2415 #endif
2416 #if CONFIG_SENSOR_Exposure
2417         case V4L2_CID_EXPOSURE:
2418             {
2419                 if (ctrl->value != sensor->info_priv.exposure)
2420                 {
2421                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2422                     {
2423                         return -EINVAL;
2424                     }
2425                     sensor->info_priv.exposure = ctrl->value;
2426                 }
2427                 break;
2428             }
2429 #endif
2430 #if CONFIG_SENSOR_Saturation
2431         case V4L2_CID_SATURATION:
2432             {
2433                 if (ctrl->value != sensor->info_priv.saturation)
2434                 {
2435                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2436                     {
2437                         return -EINVAL;
2438                     }
2439                     sensor->info_priv.saturation = ctrl->value;
2440                 }
2441                 break;
2442             }
2443 #endif
2444 #if CONFIG_SENSOR_Contrast
2445         case V4L2_CID_CONTRAST:
2446             {
2447                 if (ctrl->value != sensor->info_priv.contrast)
2448                 {
2449                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2450                     {
2451                         return -EINVAL;
2452                     }
2453                     sensor->info_priv.contrast = ctrl->value;
2454                 }
2455                 break;
2456             }
2457 #endif
2458 #if CONFIG_SENSOR_WhiteBalance
2459         case V4L2_CID_DO_WHITE_BALANCE:
2460             {
2461                 if (ctrl->value != sensor->info_priv.whiteBalance)
2462                 {
2463                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2464                     {
2465                         return -EINVAL;
2466                     }
2467                     sensor->info_priv.whiteBalance = ctrl->value;
2468                 }
2469                 break;
2470             }
2471 #endif
2472 #if CONFIG_SENSOR_Mirror
2473         case V4L2_CID_HFLIP:
2474             {
2475                 if (ctrl->value != sensor->info_priv.mirror)
2476                 {
2477                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2478                         return -EINVAL;
2479                     sensor->info_priv.mirror = ctrl->value;
2480                 }
2481                 break;
2482             }
2483 #endif
2484 #if CONFIG_SENSOR_Flip
2485         case V4L2_CID_VFLIP:
2486             {
2487                 if (ctrl->value != sensor->info_priv.flip)
2488                 {
2489                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2490                         return -EINVAL;
2491                     sensor->info_priv.flip = ctrl->value;
2492                 }
2493                 break;
2494             }
2495 #endif
2496         default:
2497             break;
2498     }
2499
2500     return 0;
2501 }
2502 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2503 {
2504     const struct v4l2_queryctrl *qctrl;
2505     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2506     struct sensor *sensor = to_sensor(client);
2507
2508     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2509
2510     if (!qctrl)
2511     {
2512         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2513         return -EINVAL;
2514     }
2515
2516     switch (ext_ctrl->id)
2517     {
2518         case V4L2_CID_SCENE:
2519             {
2520                 ext_ctrl->value = sensor->info_priv.scene;
2521                 break;
2522             }
2523         case V4L2_CID_EFFECT:
2524             {
2525                 ext_ctrl->value = sensor->info_priv.effect;
2526                 break;
2527             }
2528         case V4L2_CID_ZOOM_ABSOLUTE:
2529             {
2530                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2531                 break;
2532             }
2533         case V4L2_CID_ZOOM_RELATIVE:
2534             {
2535                 return -EINVAL;
2536             }
2537         case V4L2_CID_FOCUS_ABSOLUTE:
2538             {
2539                 ext_ctrl->value = sensor->info_priv.focus;
2540                 break;
2541             }
2542         case V4L2_CID_FOCUS_RELATIVE:
2543             {
2544                 return -EINVAL;
2545             }
2546         case V4L2_CID_FLASH:
2547             {
2548                 ext_ctrl->value = sensor->info_priv.flash;
2549                 break;
2550             }
2551         default :
2552             break;
2553     }
2554     return 0;
2555 }
2556 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2557 {
2558     const struct v4l2_queryctrl *qctrl;
2559     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2560     struct sensor *sensor = to_sensor(client);
2561     int val_offset;
2562
2563     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2564
2565     if (!qctrl)
2566     {
2567         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2568         return -EINVAL;
2569     }
2570
2571         val_offset = 0;
2572     switch (ext_ctrl->id)
2573     {
2574 #if CONFIG_SENSOR_Scene
2575         case V4L2_CID_SCENE:
2576             {
2577                 if (ext_ctrl->value != sensor->info_priv.scene)
2578                 {
2579                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2580                         return -EINVAL;
2581                     sensor->info_priv.scene = ext_ctrl->value;
2582                 }
2583                 break;
2584             }
2585 #endif
2586 #if CONFIG_SENSOR_Effect
2587         case V4L2_CID_EFFECT:
2588             {
2589                 if (ext_ctrl->value != sensor->info_priv.effect)
2590                 {
2591                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2592                         return -EINVAL;
2593                     sensor->info_priv.effect= ext_ctrl->value;
2594                 }
2595                 break;
2596             }
2597 #endif
2598 #if CONFIG_SENSOR_DigitalZoom
2599         case V4L2_CID_ZOOM_ABSOLUTE:
2600             {
2601                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2602                     return -EINVAL;
2603
2604                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2605                 {
2606                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2607
2608                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2609                         return -EINVAL;
2610                     sensor->info_priv.digitalzoom += val_offset;
2611
2612                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2613                 }
2614
2615                 break;
2616             }
2617         case V4L2_CID_ZOOM_RELATIVE:
2618             {
2619                 if (ext_ctrl->value)
2620                 {
2621                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2622                         return -EINVAL;
2623                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2624
2625                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2626                 }
2627                 break;
2628             }
2629 #endif
2630 #if CONFIG_SENSOR_Focus
2631         case V4L2_CID_FOCUS_ABSOLUTE:
2632             {
2633                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2634                     return -EINVAL;
2635
2636                 if (ext_ctrl->value != sensor->info_priv.focus)
2637                 {
2638                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2639
2640                     sensor->info_priv.focus += val_offset;
2641                 }
2642
2643                 break;
2644             }
2645         case V4L2_CID_FOCUS_RELATIVE:
2646             {
2647                 if (ext_ctrl->value)
2648                 {
2649                     sensor->info_priv.focus += ext_ctrl->value;
2650
2651                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2652                 }
2653                 break;
2654             }
2655 #endif
2656 #if CONFIG_SENSOR_Flash
2657         case V4L2_CID_FLASH:
2658             {
2659                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2660                     return -EINVAL;
2661                 sensor->info_priv.flash = ext_ctrl->value;
2662
2663                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2664                 break;
2665             }
2666 #endif
2667         default:
2668             break;
2669     }
2670
2671     return 0;
2672 }
2673
2674 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2675 {
2676     struct i2c_client *client = sd->priv;
2677     struct soc_camera_device *icd = client->dev.platform_data;
2678     int i, error_cnt=0, error_idx=-1;
2679
2680
2681     for (i=0; i<ext_ctrl->count; i++) {
2682         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2683             error_cnt++;
2684             error_idx = i;
2685         }
2686     }
2687
2688     if (error_cnt > 1)
2689         error_idx = ext_ctrl->count;
2690
2691     if (error_idx != -1) {
2692         ext_ctrl->error_idx = error_idx;
2693         return -EINVAL;
2694     } else {
2695         return 0;
2696     }
2697 }
2698
2699 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2700 {
2701     struct i2c_client *client = sd->priv;
2702     struct soc_camera_device *icd = client->dev.platform_data;
2703     int i, error_cnt=0, error_idx=-1;
2704
2705
2706     for (i=0; i<ext_ctrl->count; i++) {
2707         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2708             error_cnt++;
2709             error_idx = i;
2710         }
2711     }
2712
2713     if (error_cnt > 1)
2714         error_idx = ext_ctrl->count;
2715
2716     if (error_idx != -1) {
2717         ext_ctrl->error_idx = error_idx;
2718         return -EINVAL;
2719     } else {
2720         return 0;
2721     }
2722 }
2723
2724 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2725  * this wasn't our capture interface, so, we wait for the right one */
2726 static int sensor_video_probe(struct soc_camera_device *icd,
2727                                struct i2c_client *client)
2728 {
2729     char pid = 0;
2730     int ret;
2731     struct sensor *sensor = to_sensor(client);
2732
2733     /* We must have a parent by now. And it cannot be a wrong one.
2734      * So this entire test is completely redundant. */
2735     if (!icd->dev.parent ||
2736             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2737                 return -ENODEV;
2738
2739         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2740                 ret = -ENODEV;
2741                 goto sensor_video_probe_err;
2742         }
2743
2744     /* soft reset */
2745    /* ret = sensor_write(client, 0x12, 0x80);
2746     if (ret != 0)
2747     {
2748         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2749         return -ENODEV;
2750     }
2751     mdelay(50);          *///delay 5 microseconds
2752
2753     /* check if it is an sensor sensor */
2754     ret = sensor_read(client, 0x00, &pid);
2755     if (ret != 0) {
2756         SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2757         ret = -ENODEV;
2758         goto sensor_video_probe_err;
2759     }
2760
2761     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2762     if (pid == SENSOR_ID) {
2763         sensor->model = SENSOR_V4L2_IDENT;
2764     } else {
2765         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2766         ret = -ENODEV;
2767         goto sensor_video_probe_err;
2768     }
2769
2770     icd->formats = sensor_colour_formats;
2771     icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
2772
2773     return 0;
2774
2775 sensor_video_probe_err:
2776
2777     return ret;
2778 }
2779
2780 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2781 {
2782         struct i2c_client *client = sd->priv;
2783     struct soc_camera_device *icd = client->dev.platform_data;
2784     struct sensor *sensor = to_sensor(client);
2785     int ret = 0;
2786 #if CONFIG_SENSOR_Flash 
2787     int i;
2788 #endif
2789     
2790         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2791         switch (cmd)
2792         {
2793                 case RK29_CAM_SUBDEV_DEACTIVATE:
2794                 {
2795                         sensor_deactivate(client);
2796                         break;
2797                 }
2798
2799                 case RK29_CAM_SUBDEV_IOREQUEST:
2800                 {
2801                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2802             if (sensor->sensor_io_request != NULL) { 
2803                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
2804                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2805                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2806                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
2807                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2808                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2809                 }
2810             } else {
2811                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2812                 ret = -EINVAL;
2813                 goto sensor_ioctl_end;
2814             }
2815             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2816                for this project */
2817             #if CONFIG_SENSOR_Flash     
2818                 if (sensor->sensor_gpio_res) { 
2819                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2820                     for (i = 0; i < icd->ops->num_controls; i++) {
2821                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2822                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2823                                 }
2824                     }
2825                     sensor->info_priv.flash = 0xff;
2826                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2827                 }
2828                 }
2829             #endif
2830                         break;
2831                 }
2832                 default:
2833                 {
2834                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2835                         break;
2836                 }
2837         }
2838 sensor_ioctl_end:
2839         return ret;
2840
2841 }
2842 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2843         .init           = sensor_init,
2844         .g_ctrl         = sensor_g_control,
2845         .s_ctrl         = sensor_s_control,
2846         .g_ext_ctrls          = sensor_g_ext_controls,
2847         .s_ext_ctrls          = sensor_s_ext_controls,
2848         .g_chip_ident   = sensor_g_chip_ident,
2849         .ioctl = sensor_ioctl,
2850 };
2851
2852 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2853         .s_fmt          = sensor_s_fmt,
2854         .g_fmt          = sensor_g_fmt,
2855         .try_fmt        = sensor_try_fmt,
2856 };
2857
2858 static struct v4l2_subdev_ops sensor_subdev_ops = {
2859         .core   = &sensor_subdev_core_ops,
2860         .video = &sensor_subdev_video_ops,
2861 };
2862
2863 static int sensor_probe(struct i2c_client *client,
2864                          const struct i2c_device_id *did)
2865 {
2866     struct sensor *sensor;
2867     struct soc_camera_device *icd = client->dev.platform_data;
2868     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2869     struct soc_camera_link *icl;
2870     int ret;
2871
2872     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2873     if (!icd) {
2874         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2875         return -EINVAL;
2876     }
2877
2878     icl = to_soc_camera_link(icd);
2879     if (!icl) {
2880         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2881         return -EINVAL;
2882     }
2883
2884     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2885         dev_warn(&adapter->dev,
2886                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2887         return -EIO;
2888     }
2889
2890     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2891     if (!sensor)
2892         return -ENOMEM;
2893
2894     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2895
2896     /* Second stage probe - when a capture adapter is there */
2897     icd->ops            = &sensor_ops;
2898     icd->y_skip_top             = 0;
2899         #if CONFIG_SENSOR_I2C_NOSCHED
2900         atomic_set(&sensor->tasklock_cnt,0);
2901         #endif
2902
2903     ret = sensor_video_probe(icd, client);
2904     if (ret < 0) {
2905         icd->ops = NULL;
2906         i2c_set_clientdata(client, NULL);
2907         kfree(sensor);
2908                 sensor = NULL;
2909     }
2910     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2911     return ret;
2912 }
2913
2914 static int sensor_remove(struct i2c_client *client)
2915 {
2916     struct sensor *sensor = to_sensor(client);
2917     struct soc_camera_device *icd = client->dev.platform_data;
2918
2919     icd->ops = NULL;
2920     i2c_set_clientdata(client, NULL);
2921     client->driver = NULL;
2922     kfree(sensor);
2923         sensor = NULL;
2924     return 0;
2925 }
2926
2927 static const struct i2c_device_id sensor_id[] = {
2928         {SENSOR_NAME_STRING(), 0 },
2929         { }
2930 };
2931 MODULE_DEVICE_TABLE(i2c, sensor_id);
2932
2933 static struct i2c_driver sensor_i2c_driver = {
2934         .driver = {
2935                 .name = SENSOR_NAME_STRING(),
2936         },
2937         .probe          = sensor_probe,
2938         .remove         = sensor_remove,
2939         .id_table       = sensor_id,
2940 };
2941
2942 static int __init sensor_mod_init(void)
2943 {
2944     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2945     return i2c_add_driver(&sensor_i2c_driver);
2946 }
2947
2948 static void __exit sensor_mod_exit(void)
2949 {
2950     i2c_del_driver(&sensor_i2c_driver);
2951 }
2952
2953 device_initcall_sync(sensor_mod_init);
2954 module_exit(sensor_mod_exit);
2955
2956 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2957 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2958 MODULE_LICENSE("GPL");
2959
2960