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