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