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