camera rk30:update to v0.x.10,wake up vb when cif have't receive data etc.
[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 #if 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 const 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         hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1585     flash_off_timer.icd = icd;
1586         flash_off_timer.timer.function = flash_off_func;
1587     #endif
1588
1589     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);
1590     sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
1591     return 0;
1592 sensor_INIT_ERR:
1593     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1594     sensor_task_lock(client,0);
1595         sensor_deactivate(client);
1596     return ret;
1597 }
1598
1599 static int sensor_deactivate(struct i2c_client *client)
1600 {
1601         struct soc_camera_device *icd = client->dev.platform_data;
1602     struct sensor *sensor = to_sensor(client);
1603
1604         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1605
1606         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1607         sensor_ioctrl(icd, Sensor_PowerDown, 1);
1608
1609         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1610         icd->user_width = SENSOR_INIT_WIDTH;
1611     icd->user_height = SENSOR_INIT_HEIGHT;
1612         msleep(100);
1613     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1614         return 0;
1615 }
1616 static  struct reginfo sensor_power_down_sequence[]=
1617 {
1618     {0x00,0x00}
1619 };
1620 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1621 {
1622     int ret;
1623     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1624
1625
1626     if (pm_msg.event == PM_EVENT_SUSPEND) {
1627         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1628         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1629         if (ret != 0) {
1630             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1631             return ret;
1632         } else {
1633             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1634             if (ret < 0) {
1635                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1636                 return -EINVAL;
1637             }
1638         }
1639     } else {
1640         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1641         return -EINVAL;
1642     }
1643     return 0;
1644 }
1645
1646 static int sensor_resume(struct soc_camera_device *icd)
1647 {
1648         int ret;
1649
1650     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1651     if (ret < 0) {
1652                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1653         return -EINVAL;
1654     }
1655
1656         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1657
1658     return 0;
1659
1660 }
1661
1662 static int sensor_set_bus_param(struct soc_camera_device *icd,
1663                                 unsigned long flags)
1664 {
1665
1666     return 0;
1667 }
1668
1669 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1670 {
1671     struct soc_camera_link *icl = to_soc_camera_link(icd);
1672     unsigned long flags = SENSOR_BUS_PARAM;
1673
1674     return soc_camera_apply_sensor_flags(icl, flags);
1675 }
1676
1677 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1678 {
1679     struct i2c_client *client = v4l2_get_subdevdata(sd);
1680     struct soc_camera_device *icd = client->dev.platform_data;
1681     struct sensor *sensor = to_sensor(client);
1682
1683     mf->width   = icd->user_width;
1684         mf->height      = icd->user_height;
1685         mf->code        = sensor->info_priv.fmt.code;
1686         mf->colorspace  = sensor->info_priv.fmt.colorspace;
1687         mf->field       = V4L2_FIELD_NONE;
1688
1689     return 0;
1690 }
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 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1727 {
1728     struct i2c_client *client = v4l2_get_subdevdata(sd);
1729     struct sensor *sensor = to_sensor(client);
1730     const struct sensor_datafmt *fmt;
1731     struct reginfo *winseqe_set_addr=NULL;
1732         char readval;
1733     int ret=0, set_w,set_h,i;
1734
1735         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1736                                    ARRAY_SIZE(sensor_colour_fmts));
1737         if (!fmt) {
1738         ret = -EINVAL;
1739         goto sensor_s_fmt_end;
1740     }
1741
1742         if (sensor->info_priv.fmt.code != mf->code) {
1743                 switch (mf->code)
1744                 {
1745                         case V4L2_MBUS_FMT_YUYV8_2X8:
1746                         {
1747                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
1748                                 sensor_read(client, 0x3a, &readval);
1749                                 sensor_write(client,0x3a, readval&0xf7);
1750                                 sensor_read(client,0x3d,&readval);
1751                                 sensor_write(client,0x3d,readval&0xfe);
1752                                 break;
1753                         }
1754                         case V4L2_MBUS_FMT_UYVY8_2X8:
1755                         {
1756                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
1757                                 sensor_read(client, 0x3a, &readval);
1758                                 sensor_write(client,0x3a, readval|0x08);
1759                                 sensor_read(client,0x3d,&readval);
1760                                 sensor_write(client,0x3d,readval&0xfe);
1761                                 break;
1762                         }
1763                         default:
1764                                 break;
1765                 }
1766                 if (winseqe_set_addr != NULL) {
1767             sensor_write_array(client, winseqe_set_addr);
1768                         sensor->info_priv.fmt.code = mf->code;
1769             sensor->info_priv.fmt.colorspace= mf->colorspace;            
1770                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1771                 } else {
1772                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1773                 }
1774         }
1775
1776     set_w = mf->width;
1777     set_h = mf->height;
1778
1779         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1780         {
1781                 winseqe_set_addr = sensor_qcif;
1782         set_w = 176;
1783         set_h = 144;
1784         }
1785         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1786     {
1787         winseqe_set_addr = sensor_qvga;
1788         set_w = 320;
1789         set_h = 240;
1790     }
1791     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1792     {
1793         winseqe_set_addr = sensor_cif;
1794         set_w = 352;
1795         set_h = 288;
1796     }
1797     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1798     {
1799         winseqe_set_addr = sensor_vga;
1800         set_w = 640;
1801         set_h = 480;
1802     }
1803     else
1804     {
1805         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
1806         set_w = SENSOR_INIT_WIDTH;
1807         set_h = SENSOR_INIT_HEIGHT;             
1808                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1809     }
1810
1811     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr)
1812     {
1813     printk("sizeof(winseqe_set_addr) %d\n",sizeof(winseqe_set_addr));
1814         for(i = 0; i < sizeof(winseqe_set_addr)/2;i++){
1815                 ret |= sensor_write(client, winseqe_set_addr[i].reg, winseqe_set_addr[i].val);
1816                         //ret |= sensor_write_array(client, winseqe_set_addr);
1817                 if (ret != 0) {
1818                   SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1819                 goto sensor_s_fmt_end;
1820                 }
1821                         }
1822         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
1823
1824
1825         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1826     }
1827     else
1828     {
1829         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1830     }
1831
1832         mf->width = set_w;
1833     mf->height = set_h;
1834
1835 sensor_s_fmt_end:
1836     return ret;
1837 }
1838
1839 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1840 {
1841     struct i2c_client *client = v4l2_get_subdevdata(sd);
1842     struct sensor *sensor = to_sensor(client);
1843     const struct sensor_datafmt *fmt;
1844     int ret = 0,set_w,set_h;
1845    
1846         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1847                                    ARRAY_SIZE(sensor_colour_fmts));
1848         if (fmt == NULL) {
1849                 fmt = &sensor->info_priv.fmt;
1850         mf->code = fmt->code;
1851         } 
1852
1853     if (mf->height > SENSOR_MAX_HEIGHT)
1854         mf->height = SENSOR_MAX_HEIGHT;
1855     else if (mf->height < SENSOR_MIN_HEIGHT)
1856         mf->height = SENSOR_MIN_HEIGHT;
1857
1858     if (mf->width > SENSOR_MAX_WIDTH)
1859         mf->width = SENSOR_MAX_WIDTH;
1860     else if (mf->width < SENSOR_MIN_WIDTH)
1861         mf->width = SENSOR_MIN_WIDTH;
1862     
1863     set_w = mf->width;
1864     set_h = mf->height;
1865
1866         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1867         {
1868         set_w = 176;
1869         set_h = 144;
1870         }
1871         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1872     {
1873         set_w = 320;
1874         set_h = 240;
1875     }
1876     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1877     {
1878         set_w = 352;
1879         set_h = 288;
1880     }
1881     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1882     {
1883         set_w = 640;
1884         set_h = 480;
1885     }
1886     else
1887     {
1888         set_w = SENSOR_INIT_WIDTH;
1889         set_h = SENSOR_INIT_HEIGHT;             
1890     }
1891     mf->width = set_w;
1892     mf->height = set_h;  
1893     mf->colorspace = fmt->colorspace;
1894     
1895     return ret;
1896 }
1897  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1898 {
1899     struct i2c_client *client = v4l2_get_subdevdata(sd);
1900
1901     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1902         return -EINVAL;
1903
1904     if (id->match.addr != client->addr)
1905         return -ENODEV;
1906
1907     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV9650  identifier */
1908     id->revision = 0;
1909
1910     return 0;
1911 }
1912 #if CONFIG_SENSOR_Brightness
1913 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1914 {
1915     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1916
1917     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1918     {
1919         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1920         {
1921             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1922             {
1923                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1924                 return -EINVAL;
1925             }
1926             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1927             return 0;
1928         }
1929     }
1930         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1931     return -EINVAL;
1932 }
1933 #endif
1934 #if CONFIG_SENSOR_Effect
1935 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1936 {
1937     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1938         int i;
1939     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1940     {
1941         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1942         {
1943                 #if 0
1944             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1945             {
1946                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1947                 return -EINVAL;
1948             }
1949                         #else
1950                         printk("value %d qctrl->minimum %d ",value, qctrl->minimum);
1951                         printk("sizeof(sensor_EffectSeqe) %d\n",sizeof(sensor_EffectSeqe));
1952                         printk("sizeof(sensor_EffectSeqe[%d]) %d\n",value - qctrl->minimum,sizeof(sensor_EffectSeqe[value - qctrl->minimum]));
1953                         for(i = 0; i < 4; i++)  {
1954                                 printk("sensor_EffectSeqe[%d][%d].reg 0x%x sensor_EffectSeqe[%d][%d].val 0x%x \n",
1955                                         value - qctrl->minimum, i, sensor_EffectSeqe[value - qctrl->minimum][i].reg,value - qctrl->minimum, i,sensor_EffectSeqe[value - qctrl->minimum][i].val);
1956                                 sensor_write(client, sensor_EffectSeqe[value - qctrl->minimum][i].reg, sensor_EffectSeqe[value - qctrl->minimum][i].val);
1957                                 
1958                         }
1959                                 
1960                         #endif
1961             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1962             return 0;
1963         }
1964     }
1965         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1966     return -EINVAL;
1967 }
1968 #endif
1969 #if CONFIG_SENSOR_Exposure
1970 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1971 {
1972     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1973
1974     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1975     {
1976         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1977         {
1978             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1979             {
1980                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1981                 return -EINVAL;
1982             }
1983             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1984             return 0;
1985         }
1986     }
1987         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1988     return -EINVAL;
1989 }
1990 #endif
1991 #if CONFIG_SENSOR_Saturation
1992 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1993 {
1994     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1995
1996     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1997     {
1998         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1999         {
2000             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2001             {
2002                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2003                 return -EINVAL;
2004             }
2005             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2006             return 0;
2007         }
2008     }
2009     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2010     return -EINVAL;
2011 }
2012 #endif
2013 #if CONFIG_SENSOR_Contrast
2014 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2015 {
2016     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2017
2018     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2019     {
2020         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2021         {
2022             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2023             {
2024                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2025                 return -EINVAL;
2026             }
2027             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2028             return 0;
2029         }
2030     }
2031     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2032     return -EINVAL;
2033 }
2034 #endif
2035 #if CONFIG_SENSOR_Mirror
2036 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2037 {
2038     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2039
2040     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2041     {
2042         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2043         {
2044             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2045             {
2046                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2047                 return -EINVAL;
2048             }
2049             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2050             return 0;
2051         }
2052     }
2053     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2054     return -EINVAL;
2055 }
2056 #endif
2057 #if CONFIG_SENSOR_Flip
2058 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2059 {
2060     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2061
2062     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2063     {
2064         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2065         {
2066             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2067             {
2068                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2069                 return -EINVAL;
2070             }
2071             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2072             return 0;
2073         }
2074     }
2075     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2076     return -EINVAL;
2077 }
2078 #endif
2079 #if CONFIG_SENSOR_Scene
2080 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2081 {
2082     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2083
2084     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2085     {
2086         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2087         {
2088             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2089             {
2090                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2091                 return -EINVAL;
2092             }
2093             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2094             return 0;
2095         }
2096     }
2097     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2098     return -EINVAL;
2099 }
2100 #endif
2101 #if CONFIG_SENSOR_WhiteBalance
2102 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2103 {
2104     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2105
2106     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2107     {
2108         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2109         {
2110             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2111             {
2112                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2113                 return -EINVAL;
2114             }
2115             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2116             return 0;
2117         }
2118     }
2119         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2120     return -EINVAL;
2121 }
2122 #endif
2123 #if CONFIG_SENSOR_DigitalZoom
2124 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2125 {
2126     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2127     struct sensor *sensor = to_sensor(client);
2128         const struct v4l2_queryctrl *qctrl_info;
2129     int digitalzoom_cur, digitalzoom_total;
2130
2131         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2132         if (qctrl_info)
2133                 return -EINVAL;
2134
2135     digitalzoom_cur = sensor->info_priv.digitalzoom;
2136     digitalzoom_total = qctrl_info->maximum;
2137
2138     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
2139     {
2140         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2141         return -EINVAL;
2142     }
2143
2144     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2145     {
2146         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2147         return -EINVAL;
2148     }
2149
2150     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
2151     {
2152         value = digitalzoom_total - digitalzoom_cur;
2153     }
2154
2155     if ((value < 0) && ((digitalzoom_cur + value) < 0))
2156     {
2157         value = 0 - digitalzoom_cur;
2158     }
2159
2160     digitalzoom_cur += value;
2161
2162     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2163     {
2164         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2165         {
2166             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2167             return -EINVAL;
2168         }
2169         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2170         return 0;
2171     }
2172
2173     return -EINVAL;
2174 }
2175 #endif
2176 #if CONFIG_SENSOR_Flash
2177 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2178 {    
2179     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2180         if (value == 3) {       /* ddl@rock-chips.com: torch */
2181             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2182         } else {
2183             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2184         }
2185         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2186         return 0;
2187     }
2188     
2189         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2190     return -EINVAL;
2191 }
2192 #endif
2193
2194 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2195 {
2196     struct i2c_client *client = v4l2_get_subdevdata(sd);
2197     struct sensor *sensor = to_sensor(client);
2198     const struct v4l2_queryctrl *qctrl;
2199
2200     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2201
2202     if (!qctrl)
2203     {
2204         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2205         return -EINVAL;
2206     }
2207
2208     switch (ctrl->id)
2209     {
2210         case V4L2_CID_BRIGHTNESS:
2211             {
2212                 ctrl->value = sensor->info_priv.brightness;
2213                 break;
2214             }
2215         case V4L2_CID_SATURATION:
2216             {
2217                 ctrl->value = sensor->info_priv.saturation;
2218                 break;
2219             }
2220         case V4L2_CID_CONTRAST:
2221             {
2222                 ctrl->value = sensor->info_priv.contrast;
2223                 break;
2224             }
2225         case V4L2_CID_DO_WHITE_BALANCE:
2226             {
2227                 ctrl->value = sensor->info_priv.whiteBalance;
2228                 break;
2229             }
2230         case V4L2_CID_EXPOSURE:
2231             {
2232                 ctrl->value = sensor->info_priv.exposure;
2233                 break;
2234             }
2235         case V4L2_CID_HFLIP:
2236             {
2237                 ctrl->value = sensor->info_priv.mirror;
2238                 break;
2239             }
2240         case V4L2_CID_VFLIP:
2241             {
2242                 ctrl->value = sensor->info_priv.flip;
2243                 break;
2244             }
2245         default :
2246                 break;
2247     }
2248     return 0;
2249 }
2250
2251
2252
2253 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2254 {
2255     struct i2c_client *client = v4l2_get_subdevdata(sd);
2256     struct sensor *sensor = to_sensor(client);
2257     struct soc_camera_device *icd = client->dev.platform_data;
2258     const struct v4l2_queryctrl *qctrl;
2259
2260
2261     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2262
2263     if (!qctrl)
2264     {
2265         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2266         return -EINVAL;
2267     }
2268
2269     switch (ctrl->id)
2270     {
2271 #if CONFIG_SENSOR_Brightness
2272         case V4L2_CID_BRIGHTNESS:
2273             {
2274                 if (ctrl->value != sensor->info_priv.brightness)
2275                 {
2276                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2277                     {
2278                         return -EINVAL;
2279                     }
2280                     sensor->info_priv.brightness = ctrl->value;
2281                 }
2282                 break;
2283             }
2284 #endif
2285 #if CONFIG_SENSOR_Exposure
2286         case V4L2_CID_EXPOSURE:
2287             {
2288                 if (ctrl->value != sensor->info_priv.exposure)
2289                 {
2290                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2291                     {
2292                         return -EINVAL;
2293                     }
2294                     sensor->info_priv.exposure = ctrl->value;
2295                 }
2296                 break;
2297             }
2298 #endif
2299 #if CONFIG_SENSOR_Saturation
2300         case V4L2_CID_SATURATION:
2301             {
2302                 if (ctrl->value != sensor->info_priv.saturation)
2303                 {
2304                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2305                     {
2306                         return -EINVAL;
2307                     }
2308                     sensor->info_priv.saturation = ctrl->value;
2309                 }
2310                 break;
2311             }
2312 #endif
2313 #if CONFIG_SENSOR_Contrast
2314         case V4L2_CID_CONTRAST:
2315             {
2316                 if (ctrl->value != sensor->info_priv.contrast)
2317                 {
2318                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2319                     {
2320                         return -EINVAL;
2321                     }
2322                     sensor->info_priv.contrast = ctrl->value;
2323                 }
2324                 break;
2325             }
2326 #endif
2327 #if CONFIG_SENSOR_WhiteBalance
2328         case V4L2_CID_DO_WHITE_BALANCE:
2329             {
2330                 if (ctrl->value != sensor->info_priv.whiteBalance)
2331                 {
2332                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2333                     {
2334                         return -EINVAL;
2335                     }
2336                     sensor->info_priv.whiteBalance = ctrl->value;
2337                 }
2338                 break;
2339             }
2340 #endif
2341 #if CONFIG_SENSOR_Mirror
2342         case V4L2_CID_HFLIP:
2343             {
2344                 if (ctrl->value != sensor->info_priv.mirror)
2345                 {
2346                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2347                         return -EINVAL;
2348                     sensor->info_priv.mirror = ctrl->value;
2349                 }
2350                 break;
2351             }
2352 #endif
2353 #if CONFIG_SENSOR_Flip
2354         case V4L2_CID_VFLIP:
2355             {
2356                 if (ctrl->value != sensor->info_priv.flip)
2357                 {
2358                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2359                         return -EINVAL;
2360                     sensor->info_priv.flip = ctrl->value;
2361                 }
2362                 break;
2363             }
2364 #endif
2365         default:
2366             break;
2367     }
2368
2369     return 0;
2370 }
2371 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2372 {
2373     const struct v4l2_queryctrl *qctrl;
2374     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2375     struct sensor *sensor = to_sensor(client);
2376
2377     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2378
2379     if (!qctrl)
2380     {
2381         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2382         return -EINVAL;
2383     }
2384
2385     switch (ext_ctrl->id)
2386     {
2387         case V4L2_CID_SCENE:
2388             {
2389                 ext_ctrl->value = sensor->info_priv.scene;
2390                 break;
2391             }
2392         case V4L2_CID_EFFECT:
2393             {
2394                 ext_ctrl->value = sensor->info_priv.effect;
2395                 break;
2396             }
2397         case V4L2_CID_ZOOM_ABSOLUTE:
2398             {
2399                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2400                 break;
2401             }
2402         case V4L2_CID_ZOOM_RELATIVE:
2403             {
2404                 return -EINVAL;
2405             }
2406         case V4L2_CID_FOCUS_ABSOLUTE:
2407             {
2408                 ext_ctrl->value = sensor->info_priv.focus;
2409                 break;
2410             }
2411         case V4L2_CID_FOCUS_RELATIVE:
2412             {
2413                 return -EINVAL;
2414             }
2415         case V4L2_CID_FLASH:
2416             {
2417                 ext_ctrl->value = sensor->info_priv.flash;
2418                 break;
2419             }
2420         default :
2421             break;
2422     }
2423     return 0;
2424 }
2425 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2426 {
2427     const struct v4l2_queryctrl *qctrl;
2428     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2429     struct sensor *sensor = to_sensor(client);
2430     int val_offset;
2431
2432     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2433
2434     if (!qctrl)
2435     {
2436         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2437         return -EINVAL;
2438     }
2439
2440         val_offset = 0;
2441     switch (ext_ctrl->id)
2442     {
2443 #if CONFIG_SENSOR_Scene
2444         case V4L2_CID_SCENE:
2445             {
2446                 if (ext_ctrl->value != sensor->info_priv.scene)
2447                 {
2448                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2449                         return -EINVAL;
2450                     sensor->info_priv.scene = ext_ctrl->value;
2451                 }
2452                 break;
2453             }
2454 #endif
2455 #if CONFIG_SENSOR_Effect
2456         case V4L2_CID_EFFECT:
2457             {
2458                 if (ext_ctrl->value != sensor->info_priv.effect)
2459                 {
2460                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2461                         return -EINVAL;
2462                     sensor->info_priv.effect= ext_ctrl->value;
2463                 }
2464                 break;
2465             }
2466 #endif
2467 #if CONFIG_SENSOR_DigitalZoom
2468         case V4L2_CID_ZOOM_ABSOLUTE:
2469             {
2470                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2471                     return -EINVAL;
2472
2473                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2474                 {
2475                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2476
2477                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2478                         return -EINVAL;
2479                     sensor->info_priv.digitalzoom += val_offset;
2480
2481                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2482                 }
2483
2484                 break;
2485             }
2486         case V4L2_CID_ZOOM_RELATIVE:
2487             {
2488                 if (ext_ctrl->value)
2489                 {
2490                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2491                         return -EINVAL;
2492                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2493
2494                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2495                 }
2496                 break;
2497             }
2498 #endif
2499 #if CONFIG_SENSOR_Focus
2500         case V4L2_CID_FOCUS_ABSOLUTE:
2501             {
2502                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2503                     return -EINVAL;
2504
2505                 if (ext_ctrl->value != sensor->info_priv.focus)
2506                 {
2507                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2508
2509                     sensor->info_priv.focus += val_offset;
2510                 }
2511
2512                 break;
2513             }
2514         case V4L2_CID_FOCUS_RELATIVE:
2515             {
2516                 if (ext_ctrl->value)
2517                 {
2518                     sensor->info_priv.focus += ext_ctrl->value;
2519
2520                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2521                 }
2522                 break;
2523             }
2524 #endif
2525 #if CONFIG_SENSOR_Flash
2526         case V4L2_CID_FLASH:
2527             {
2528                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2529                     return -EINVAL;
2530                 sensor->info_priv.flash = ext_ctrl->value;
2531
2532                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2533                 break;
2534             }
2535 #endif
2536         default:
2537             break;
2538     }
2539
2540     return 0;
2541 }
2542
2543 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2544 {
2545     struct i2c_client *client = v4l2_get_subdevdata(sd);
2546     struct soc_camera_device *icd = client->dev.platform_data;
2547     int i, error_cnt=0, error_idx=-1;
2548
2549
2550     for (i=0; i<ext_ctrl->count; i++) {
2551         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2552             error_cnt++;
2553             error_idx = i;
2554         }
2555     }
2556
2557     if (error_cnt > 1)
2558         error_idx = ext_ctrl->count;
2559
2560     if (error_idx != -1) {
2561         ext_ctrl->error_idx = error_idx;
2562         return -EINVAL;
2563     } else {
2564         return 0;
2565     }
2566 }
2567
2568 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2569 {
2570     struct i2c_client *client = v4l2_get_subdevdata(sd);
2571     struct soc_camera_device *icd = client->dev.platform_data;
2572     int i, error_cnt=0, error_idx=-1;
2573
2574
2575     for (i=0; i<ext_ctrl->count; i++) {
2576         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2577             error_cnt++;
2578             error_idx = i;
2579         }
2580     }
2581
2582     if (error_cnt > 1)
2583         error_idx = ext_ctrl->count;
2584
2585     if (error_idx != -1) {
2586         ext_ctrl->error_idx = error_idx;
2587         return -EINVAL;
2588     } else {
2589         return 0;
2590     }
2591 }
2592
2593 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2594  * this wasn't our capture interface, so, we wait for the right one */
2595 static int sensor_video_probe(struct soc_camera_device *icd,
2596                                struct i2c_client *client)
2597 {
2598     char pid = 0;
2599     int ret;
2600     struct sensor *sensor = to_sensor(client);
2601
2602     /* We must have a parent by now. And it cannot be a wrong one.
2603      * So this entire test is completely redundant. */
2604     if (!icd->dev.parent ||
2605             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2606                 return -ENODEV;
2607
2608         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2609                 ret = -ENODEV;
2610                 goto sensor_video_probe_err;
2611         }
2612
2613     /* soft reset */
2614 //    ret = sensor_write(client, 0x12, 0x80);
2615 //    if (ret != 0)
2616 //    {
2617 //        SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2618 //        return -ENODEV;
2619 //    }
2620     mdelay(50);          //delay 5 microseconds
2621
2622     /* check if it is an sensor sensor */
2623     ret = sensor_read(client, 0x01, &pid);
2624         if (ret != 0) {
2625         SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2626         ret = -ENODEV;
2627         goto sensor_video_probe_err;
2628     }
2629
2630     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2631     if (pid == SENSOR_ID) {
2632         sensor->model = SENSOR_V4L2_IDENT;
2633     } else {
2634         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2635         ret = -ENODEV;
2636         goto sensor_video_probe_err;
2637     }
2638
2639     return 0;
2640
2641 sensor_video_probe_err:
2642
2643     return ret;
2644 }
2645
2646 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2647 {
2648         struct i2c_client *client = v4l2_get_subdevdata(sd);
2649     struct soc_camera_device *icd = client->dev.platform_data;
2650     struct sensor *sensor = to_sensor(client);
2651     int ret = 0;
2652 #if CONFIG_SENSOR_Flash     
2653     int i;
2654 #endif
2655     
2656         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2657         switch (cmd)
2658         {
2659                 case RK29_CAM_SUBDEV_DEACTIVATE:
2660                 {
2661                         sensor_deactivate(client);
2662                         break;
2663                 }
2664
2665                 case RK29_CAM_SUBDEV_IOREQUEST:
2666                 {
2667                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2668             if (sensor->sensor_io_request != NULL) { 
2669                 sensor->sensor_gpio_res = NULL;
2670                 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
2671                     if (sensor->sensor_io_request->gpio_res[i].dev_name && 
2672                         (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
2673                         sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
2674                     }
2675                 }
2676                 if (sensor->sensor_gpio_res == NULL) {
2677                     SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
2678                     ret = -EINVAL;
2679                     goto sensor_ioctl_end;
2680                 }
2681             } else {
2682                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2683                 ret = -EINVAL;
2684                 goto sensor_ioctl_end;
2685             }
2686             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2687                for this project */
2688             #if CONFIG_SENSOR_Flash     
2689                 if (sensor->sensor_gpio_res) {
2690                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2691                     for (i = 0; i < icd->ops->num_controls; i++) {
2692                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2693                                         //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));  
2694                               sensor_controls[i].id=0xffff;                             
2695                                 }
2696                     }
2697                     sensor->info_priv.flash = 0xff;
2698                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2699                 }else{ //two cameras are the same,need to deal diffrently ,zyc
2700                     for (i = 0; i < icd->ops->num_controls; i++) {
2701                            if(0xffff == icd->ops->controls[i].id){
2702                               sensor_controls[i].id=V4L2_CID_FLASH;
2703                            }               
2704                     }
2705                 }
2706                 }
2707             #endif
2708                         break;
2709                 }
2710                 default:
2711                 {
2712                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2713                         break;
2714                 }
2715         }
2716 sensor_ioctl_end:
2717         return ret;
2718
2719 }
2720 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2721                             enum v4l2_mbus_pixelcode *code)
2722 {
2723         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2724                 return -EINVAL;
2725
2726         *code = sensor_colour_fmts[index].code;
2727         return 0;
2728 }
2729 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2730         .init           = sensor_init,
2731         .g_ctrl         = sensor_g_control,
2732         .s_ctrl         = sensor_s_control,
2733         .g_ext_ctrls          = sensor_g_ext_controls,
2734         .s_ext_ctrls          = sensor_s_ext_controls,
2735         .g_chip_ident   = sensor_g_chip_ident,
2736         .ioctl = sensor_ioctl,
2737 };
2738
2739 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2740         .s_mbus_fmt     = sensor_s_fmt,
2741         .g_mbus_fmt     = sensor_g_fmt,
2742         .try_mbus_fmt   = sensor_try_fmt,
2743         .enum_mbus_fmt  = sensor_enum_fmt,
2744 };
2745
2746 static struct v4l2_subdev_ops sensor_subdev_ops = {
2747         .core   = &sensor_subdev_core_ops,
2748         .video = &sensor_subdev_video_ops,
2749 };
2750
2751 static int sensor_probe(struct i2c_client *client,
2752                          const struct i2c_device_id *did)
2753 {
2754     struct sensor *sensor;
2755     struct soc_camera_device *icd = client->dev.platform_data;
2756     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2757     struct soc_camera_link *icl;
2758     int ret;
2759
2760     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2761     if (!icd) {
2762         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2763         return -EINVAL;
2764     }
2765
2766     icl = to_soc_camera_link(icd);
2767     if (!icl) {
2768         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2769         return -EINVAL;
2770     }
2771
2772     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2773         dev_warn(&adapter->dev,
2774                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2775         return -EIO;
2776     }
2777
2778     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2779     if (!sensor)
2780         return -ENOMEM;
2781
2782     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2783
2784     /* Second stage probe - when a capture adapter is there */
2785     icd->ops            = &sensor_ops;
2786     sensor->info_priv.fmt = sensor_colour_fmts[0];
2787         #if CONFIG_SENSOR_I2C_NOSCHED
2788         atomic_set(&sensor->tasklock_cnt,0);
2789         #endif
2790
2791     ret = sensor_video_probe(icd, client);
2792     if (ret < 0) {
2793         icd->ops = NULL;
2794         i2c_set_clientdata(client, NULL);
2795         kfree(sensor);
2796                 sensor = NULL;
2797     }
2798     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2799     return ret;
2800 }
2801
2802 static int sensor_remove(struct i2c_client *client)
2803 {
2804     struct sensor *sensor = to_sensor(client);
2805     struct soc_camera_device *icd = client->dev.platform_data;
2806
2807     icd->ops = NULL;
2808     i2c_set_clientdata(client, NULL);
2809     client->driver = NULL;
2810     kfree(sensor);
2811         sensor = NULL;
2812     return 0;
2813 }
2814
2815 static const struct i2c_device_id sensor_id[] = {
2816         {SENSOR_NAME_STRING(), 0 },
2817         { }
2818 };
2819 MODULE_DEVICE_TABLE(i2c, sensor_id);
2820
2821 static struct i2c_driver sensor_i2c_driver = {
2822         .driver = {
2823                 .name = SENSOR_NAME_STRING(),
2824         },
2825         .probe          = sensor_probe,
2826         .remove         = sensor_remove,
2827         .id_table       = sensor_id,
2828 };
2829
2830 static int __init sensor_mod_init(void)
2831 {
2832     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2833     return i2c_add_driver(&sensor_i2c_driver);
2834 }
2835
2836 static void __exit sensor_mod_exit(void)
2837 {
2838     i2c_del_driver(&sensor_i2c_driver);
2839 }
2840
2841 device_initcall_sync(sensor_mod_init);
2842 module_exit(sensor_mod_exit);
2843
2844 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2845 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2846 MODULE_LICENSE("GPL");
2847
2848