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