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