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