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