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