Merge remote-tracking branch 'origin/develop-3.0-rk30' into develop-3.0
[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,set_w,set_h;
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     set_w = mf->width;
1837     set_h = mf->height;
1838
1839         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1840         {
1841         set_w = 176;
1842         set_h = 144;
1843         }
1844         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1845     {
1846         set_w = 320;
1847         set_h = 240;
1848     }
1849     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1850     {
1851         set_w = 352;
1852         set_h = 288;
1853     }
1854     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1855     {
1856         set_w = 640;
1857         set_h = 480;
1858     }
1859     else
1860     {
1861         set_w = SENSOR_INIT_WIDTH;
1862         set_h = SENSOR_INIT_HEIGHT;             
1863     }
1864     mf->width = set_w;
1865     mf->height = set_h;  
1866     mf->colorspace = fmt->colorspace;
1867     
1868     return ret;
1869 }
1870  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1871 {
1872     struct i2c_client *client = v4l2_get_subdevdata(sd);
1873
1874     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1875         return -EINVAL;
1876
1877     if (id->match.addr != client->addr)
1878         return -ENODEV;
1879
1880     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV9650  identifier */
1881     id->revision = 0;
1882
1883     return 0;
1884 }
1885 #if CONFIG_SENSOR_Brightness
1886 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1887 {
1888     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1889
1890     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1891     {
1892         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1893         {
1894             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1895             {
1896                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1897                 return -EINVAL;
1898             }
1899             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1900             return 0;
1901         }
1902     }
1903         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1904     return -EINVAL;
1905 }
1906 #endif
1907 #if CONFIG_SENSOR_Effect
1908 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1909 {
1910     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1911         int i;
1912     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1913     {
1914         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1915         {
1916                 #if 0
1917             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1918             {
1919                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1920                 return -EINVAL;
1921             }
1922                         #else
1923                         printk("value %d qctrl->minimum %d ",value, qctrl->minimum);
1924                         printk("sizeof(sensor_EffectSeqe) %d\n",sizeof(sensor_EffectSeqe));
1925                         printk("sizeof(sensor_EffectSeqe[%d]) %d\n",value - qctrl->minimum,sizeof(sensor_EffectSeqe[value - qctrl->minimum]));
1926                         for(i = 0; i < 4; i++)  {
1927                                 printk("sensor_EffectSeqe[%d][%d].reg 0x%x sensor_EffectSeqe[%d][%d].val 0x%x \n",
1928                                         value - qctrl->minimum, i, sensor_EffectSeqe[value - qctrl->minimum][i].reg,value - qctrl->minimum, i,sensor_EffectSeqe[value - qctrl->minimum][i].val);
1929                                 sensor_write(client, sensor_EffectSeqe[value - qctrl->minimum][i].reg, sensor_EffectSeqe[value - qctrl->minimum][i].val);
1930                                 
1931                         }
1932                                 
1933                         #endif
1934             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1935             return 0;
1936         }
1937     }
1938         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1939     return -EINVAL;
1940 }
1941 #endif
1942 #if CONFIG_SENSOR_Exposure
1943 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1944 {
1945     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1946
1947     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1948     {
1949         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1950         {
1951             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1952             {
1953                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1954                 return -EINVAL;
1955             }
1956             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1957             return 0;
1958         }
1959     }
1960         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1961     return -EINVAL;
1962 }
1963 #endif
1964 #if CONFIG_SENSOR_Saturation
1965 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1966 {
1967     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1968
1969     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1970     {
1971         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1972         {
1973             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1974             {
1975                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1976                 return -EINVAL;
1977             }
1978             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1979             return 0;
1980         }
1981     }
1982     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1983     return -EINVAL;
1984 }
1985 #endif
1986 #if CONFIG_SENSOR_Contrast
1987 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1988 {
1989     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1990
1991     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1992     {
1993         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1994         {
1995             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1996             {
1997                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1998                 return -EINVAL;
1999             }
2000             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2001             return 0;
2002         }
2003     }
2004     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2005     return -EINVAL;
2006 }
2007 #endif
2008 #if CONFIG_SENSOR_Mirror
2009 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2010 {
2011     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2012
2013     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2014     {
2015         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2016         {
2017             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2018             {
2019                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2020                 return -EINVAL;
2021             }
2022             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2023             return 0;
2024         }
2025     }
2026     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2027     return -EINVAL;
2028 }
2029 #endif
2030 #if CONFIG_SENSOR_Flip
2031 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2032 {
2033     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2034
2035     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2036     {
2037         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2038         {
2039             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2040             {
2041                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2042                 return -EINVAL;
2043             }
2044             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2045             return 0;
2046         }
2047     }
2048     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2049     return -EINVAL;
2050 }
2051 #endif
2052 #if CONFIG_SENSOR_Scene
2053 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2054 {
2055     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2056
2057     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2058     {
2059         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2060         {
2061             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2062             {
2063                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2064                 return -EINVAL;
2065             }
2066             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2067             return 0;
2068         }
2069     }
2070     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2071     return -EINVAL;
2072 }
2073 #endif
2074 #if CONFIG_SENSOR_WhiteBalance
2075 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2076 {
2077     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2078
2079     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2080     {
2081         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2082         {
2083             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2084             {
2085                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2086                 return -EINVAL;
2087             }
2088             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2089             return 0;
2090         }
2091     }
2092         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2093     return -EINVAL;
2094 }
2095 #endif
2096 #if CONFIG_SENSOR_DigitalZoom
2097 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2098 {
2099     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2100     struct sensor *sensor = to_sensor(client);
2101         const struct v4l2_queryctrl *qctrl_info;
2102     int digitalzoom_cur, digitalzoom_total;
2103
2104         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2105         if (qctrl_info)
2106                 return -EINVAL;
2107
2108     digitalzoom_cur = sensor->info_priv.digitalzoom;
2109     digitalzoom_total = qctrl_info->maximum;
2110
2111     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
2112     {
2113         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2114         return -EINVAL;
2115     }
2116
2117     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2118     {
2119         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2120         return -EINVAL;
2121     }
2122
2123     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
2124     {
2125         value = digitalzoom_total - digitalzoom_cur;
2126     }
2127
2128     if ((value < 0) && ((digitalzoom_cur + value) < 0))
2129     {
2130         value = 0 - digitalzoom_cur;
2131     }
2132
2133     digitalzoom_cur += value;
2134
2135     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2136     {
2137         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2138         {
2139             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2140             return -EINVAL;
2141         }
2142         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2143         return 0;
2144     }
2145
2146     return -EINVAL;
2147 }
2148 #endif
2149 #if CONFIG_SENSOR_Flash
2150 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2151 {    
2152     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2153         if (value == 3) {       /* ddl@rock-chips.com: torch */
2154             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2155         } else {
2156             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2157         }
2158         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2159         return 0;
2160     }
2161     
2162         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2163     return -EINVAL;
2164 }
2165 #endif
2166
2167 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2168 {
2169     struct i2c_client *client = v4l2_get_subdevdata(sd);
2170     struct sensor *sensor = to_sensor(client);
2171     const struct v4l2_queryctrl *qctrl;
2172
2173     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2174
2175     if (!qctrl)
2176     {
2177         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2178         return -EINVAL;
2179     }
2180
2181     switch (ctrl->id)
2182     {
2183         case V4L2_CID_BRIGHTNESS:
2184             {
2185                 ctrl->value = sensor->info_priv.brightness;
2186                 break;
2187             }
2188         case V4L2_CID_SATURATION:
2189             {
2190                 ctrl->value = sensor->info_priv.saturation;
2191                 break;
2192             }
2193         case V4L2_CID_CONTRAST:
2194             {
2195                 ctrl->value = sensor->info_priv.contrast;
2196                 break;
2197             }
2198         case V4L2_CID_DO_WHITE_BALANCE:
2199             {
2200                 ctrl->value = sensor->info_priv.whiteBalance;
2201                 break;
2202             }
2203         case V4L2_CID_EXPOSURE:
2204             {
2205                 ctrl->value = sensor->info_priv.exposure;
2206                 break;
2207             }
2208         case V4L2_CID_HFLIP:
2209             {
2210                 ctrl->value = sensor->info_priv.mirror;
2211                 break;
2212             }
2213         case V4L2_CID_VFLIP:
2214             {
2215                 ctrl->value = sensor->info_priv.flip;
2216                 break;
2217             }
2218         default :
2219                 break;
2220     }
2221     return 0;
2222 }
2223
2224
2225
2226 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2227 {
2228     struct i2c_client *client = v4l2_get_subdevdata(sd);
2229     struct sensor *sensor = to_sensor(client);
2230     struct soc_camera_device *icd = client->dev.platform_data;
2231     const struct v4l2_queryctrl *qctrl;
2232
2233
2234     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2235
2236     if (!qctrl)
2237     {
2238         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2239         return -EINVAL;
2240     }
2241
2242     switch (ctrl->id)
2243     {
2244 #if CONFIG_SENSOR_Brightness
2245         case V4L2_CID_BRIGHTNESS:
2246             {
2247                 if (ctrl->value != sensor->info_priv.brightness)
2248                 {
2249                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2250                     {
2251                         return -EINVAL;
2252                     }
2253                     sensor->info_priv.brightness = ctrl->value;
2254                 }
2255                 break;
2256             }
2257 #endif
2258 #if CONFIG_SENSOR_Exposure
2259         case V4L2_CID_EXPOSURE:
2260             {
2261                 if (ctrl->value != sensor->info_priv.exposure)
2262                 {
2263                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2264                     {
2265                         return -EINVAL;
2266                     }
2267                     sensor->info_priv.exposure = ctrl->value;
2268                 }
2269                 break;
2270             }
2271 #endif
2272 #if CONFIG_SENSOR_Saturation
2273         case V4L2_CID_SATURATION:
2274             {
2275                 if (ctrl->value != sensor->info_priv.saturation)
2276                 {
2277                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2278                     {
2279                         return -EINVAL;
2280                     }
2281                     sensor->info_priv.saturation = ctrl->value;
2282                 }
2283                 break;
2284             }
2285 #endif
2286 #if CONFIG_SENSOR_Contrast
2287         case V4L2_CID_CONTRAST:
2288             {
2289                 if (ctrl->value != sensor->info_priv.contrast)
2290                 {
2291                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2292                     {
2293                         return -EINVAL;
2294                     }
2295                     sensor->info_priv.contrast = ctrl->value;
2296                 }
2297                 break;
2298             }
2299 #endif
2300 #if CONFIG_SENSOR_WhiteBalance
2301         case V4L2_CID_DO_WHITE_BALANCE:
2302             {
2303                 if (ctrl->value != sensor->info_priv.whiteBalance)
2304                 {
2305                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2306                     {
2307                         return -EINVAL;
2308                     }
2309                     sensor->info_priv.whiteBalance = ctrl->value;
2310                 }
2311                 break;
2312             }
2313 #endif
2314 #if CONFIG_SENSOR_Mirror
2315         case V4L2_CID_HFLIP:
2316             {
2317                 if (ctrl->value != sensor->info_priv.mirror)
2318                 {
2319                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2320                         return -EINVAL;
2321                     sensor->info_priv.mirror = ctrl->value;
2322                 }
2323                 break;
2324             }
2325 #endif
2326 #if CONFIG_SENSOR_Flip
2327         case V4L2_CID_VFLIP:
2328             {
2329                 if (ctrl->value != sensor->info_priv.flip)
2330                 {
2331                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2332                         return -EINVAL;
2333                     sensor->info_priv.flip = ctrl->value;
2334                 }
2335                 break;
2336             }
2337 #endif
2338         default:
2339             break;
2340     }
2341
2342     return 0;
2343 }
2344 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2345 {
2346     const struct v4l2_queryctrl *qctrl;
2347     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2348     struct sensor *sensor = to_sensor(client);
2349
2350     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2351
2352     if (!qctrl)
2353     {
2354         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2355         return -EINVAL;
2356     }
2357
2358     switch (ext_ctrl->id)
2359     {
2360         case V4L2_CID_SCENE:
2361             {
2362                 ext_ctrl->value = sensor->info_priv.scene;
2363                 break;
2364             }
2365         case V4L2_CID_EFFECT:
2366             {
2367                 ext_ctrl->value = sensor->info_priv.effect;
2368                 break;
2369             }
2370         case V4L2_CID_ZOOM_ABSOLUTE:
2371             {
2372                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2373                 break;
2374             }
2375         case V4L2_CID_ZOOM_RELATIVE:
2376             {
2377                 return -EINVAL;
2378             }
2379         case V4L2_CID_FOCUS_ABSOLUTE:
2380             {
2381                 ext_ctrl->value = sensor->info_priv.focus;
2382                 break;
2383             }
2384         case V4L2_CID_FOCUS_RELATIVE:
2385             {
2386                 return -EINVAL;
2387             }
2388         case V4L2_CID_FLASH:
2389             {
2390                 ext_ctrl->value = sensor->info_priv.flash;
2391                 break;
2392             }
2393         default :
2394             break;
2395     }
2396     return 0;
2397 }
2398 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2399 {
2400     const struct v4l2_queryctrl *qctrl;
2401     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2402     struct sensor *sensor = to_sensor(client);
2403     int val_offset;
2404
2405     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2406
2407     if (!qctrl)
2408     {
2409         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2410         return -EINVAL;
2411     }
2412
2413         val_offset = 0;
2414     switch (ext_ctrl->id)
2415     {
2416 #if CONFIG_SENSOR_Scene
2417         case V4L2_CID_SCENE:
2418             {
2419                 if (ext_ctrl->value != sensor->info_priv.scene)
2420                 {
2421                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2422                         return -EINVAL;
2423                     sensor->info_priv.scene = ext_ctrl->value;
2424                 }
2425                 break;
2426             }
2427 #endif
2428 #if CONFIG_SENSOR_Effect
2429         case V4L2_CID_EFFECT:
2430             {
2431                 if (ext_ctrl->value != sensor->info_priv.effect)
2432                 {
2433                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2434                         return -EINVAL;
2435                     sensor->info_priv.effect= ext_ctrl->value;
2436                 }
2437                 break;
2438             }
2439 #endif
2440 #if CONFIG_SENSOR_DigitalZoom
2441         case V4L2_CID_ZOOM_ABSOLUTE:
2442             {
2443                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2444                     return -EINVAL;
2445
2446                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2447                 {
2448                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2449
2450                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2451                         return -EINVAL;
2452                     sensor->info_priv.digitalzoom += val_offset;
2453
2454                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2455                 }
2456
2457                 break;
2458             }
2459         case V4L2_CID_ZOOM_RELATIVE:
2460             {
2461                 if (ext_ctrl->value)
2462                 {
2463                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2464                         return -EINVAL;
2465                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2466
2467                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2468                 }
2469                 break;
2470             }
2471 #endif
2472 #if CONFIG_SENSOR_Focus
2473         case V4L2_CID_FOCUS_ABSOLUTE:
2474             {
2475                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2476                     return -EINVAL;
2477
2478                 if (ext_ctrl->value != sensor->info_priv.focus)
2479                 {
2480                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2481
2482                     sensor->info_priv.focus += val_offset;
2483                 }
2484
2485                 break;
2486             }
2487         case V4L2_CID_FOCUS_RELATIVE:
2488             {
2489                 if (ext_ctrl->value)
2490                 {
2491                     sensor->info_priv.focus += ext_ctrl->value;
2492
2493                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2494                 }
2495                 break;
2496             }
2497 #endif
2498 #if CONFIG_SENSOR_Flash
2499         case V4L2_CID_FLASH:
2500             {
2501                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2502                     return -EINVAL;
2503                 sensor->info_priv.flash = ext_ctrl->value;
2504
2505                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2506                 break;
2507             }
2508 #endif
2509         default:
2510             break;
2511     }
2512
2513     return 0;
2514 }
2515
2516 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2517 {
2518     struct i2c_client *client = v4l2_get_subdevdata(sd);
2519     struct soc_camera_device *icd = client->dev.platform_data;
2520     int i, error_cnt=0, error_idx=-1;
2521
2522
2523     for (i=0; i<ext_ctrl->count; i++) {
2524         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2525             error_cnt++;
2526             error_idx = i;
2527         }
2528     }
2529
2530     if (error_cnt > 1)
2531         error_idx = ext_ctrl->count;
2532
2533     if (error_idx != -1) {
2534         ext_ctrl->error_idx = error_idx;
2535         return -EINVAL;
2536     } else {
2537         return 0;
2538     }
2539 }
2540
2541 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2542 {
2543     struct i2c_client *client = v4l2_get_subdevdata(sd);
2544     struct soc_camera_device *icd = client->dev.platform_data;
2545     int i, error_cnt=0, error_idx=-1;
2546
2547
2548     for (i=0; i<ext_ctrl->count; i++) {
2549         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2550             error_cnt++;
2551             error_idx = i;
2552         }
2553     }
2554
2555     if (error_cnt > 1)
2556         error_idx = ext_ctrl->count;
2557
2558     if (error_idx != -1) {
2559         ext_ctrl->error_idx = error_idx;
2560         return -EINVAL;
2561     } else {
2562         return 0;
2563     }
2564 }
2565
2566 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2567  * this wasn't our capture interface, so, we wait for the right one */
2568 static int sensor_video_probe(struct soc_camera_device *icd,
2569                                struct i2c_client *client)
2570 {
2571     char pid = 0;
2572     int ret;
2573     struct sensor *sensor = to_sensor(client);
2574
2575     /* We must have a parent by now. And it cannot be a wrong one.
2576      * So this entire test is completely redundant. */
2577     if (!icd->dev.parent ||
2578             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2579                 return -ENODEV;
2580
2581         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2582                 ret = -ENODEV;
2583                 goto sensor_video_probe_err;
2584         }
2585
2586     /* soft reset */
2587 //    ret = sensor_write(client, 0x12, 0x80);
2588 //    if (ret != 0)
2589 //    {
2590 //        SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2591 //        return -ENODEV;
2592 //    }
2593     mdelay(50);          //delay 5 microseconds
2594
2595     /* check if it is an sensor sensor */
2596     ret = sensor_read(client, 0x01, &pid);
2597         if (ret != 0) {
2598         SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2599         ret = -ENODEV;
2600         goto sensor_video_probe_err;
2601     }
2602
2603     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2604     if (pid == SENSOR_ID) {
2605         sensor->model = SENSOR_V4L2_IDENT;
2606     } else {
2607         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2608         ret = -ENODEV;
2609         goto sensor_video_probe_err;
2610     }
2611
2612     return 0;
2613
2614 sensor_video_probe_err:
2615
2616     return ret;
2617 }
2618
2619 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2620 {
2621         struct i2c_client *client = v4l2_get_subdevdata(sd);
2622     struct soc_camera_device *icd = client->dev.platform_data;
2623     struct sensor *sensor = to_sensor(client);
2624     int ret = 0;
2625 #if CONFIG_SENSOR_Flash     
2626     int i;
2627 #endif
2628     
2629         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2630         switch (cmd)
2631         {
2632                 case RK29_CAM_SUBDEV_DEACTIVATE:
2633                 {
2634                         sensor_deactivate(client);
2635                         break;
2636                 }
2637
2638                 case RK29_CAM_SUBDEV_IOREQUEST:
2639                 {
2640                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2641             if (sensor->sensor_io_request != NULL) { 
2642                 sensor->sensor_gpio_res = NULL;
2643                 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
2644                     if (sensor->sensor_io_request->gpio_res[i].dev_name && 
2645                         (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
2646                         sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
2647                     }
2648                 }
2649                 if (sensor->sensor_gpio_res == NULL) {
2650                     SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
2651                     ret = -EINVAL;
2652                     goto sensor_ioctl_end;
2653                 }
2654             } else {
2655                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2656                 ret = -EINVAL;
2657                 goto sensor_ioctl_end;
2658             }
2659             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2660                for this project */
2661             #if CONFIG_SENSOR_Flash     
2662                 if (sensor->sensor_gpio_res) {
2663                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2664                     for (i = 0; i < icd->ops->num_controls; i++) {
2665                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2666                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2667                                 }
2668                     }
2669                     sensor->info_priv.flash = 0xff;
2670                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2671                 }
2672                 }
2673             #endif
2674                         break;
2675                 }
2676                 default:
2677                 {
2678                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2679                         break;
2680                 }
2681         }
2682 sensor_ioctl_end:
2683         return ret;
2684
2685 }
2686 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2687                             enum v4l2_mbus_pixelcode *code)
2688 {
2689         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2690                 return -EINVAL;
2691
2692         *code = sensor_colour_fmts[index].code;
2693         return 0;
2694 }
2695 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2696         .init           = sensor_init,
2697         .g_ctrl         = sensor_g_control,
2698         .s_ctrl         = sensor_s_control,
2699         .g_ext_ctrls          = sensor_g_ext_controls,
2700         .s_ext_ctrls          = sensor_s_ext_controls,
2701         .g_chip_ident   = sensor_g_chip_ident,
2702         .ioctl = sensor_ioctl,
2703 };
2704
2705 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2706         .s_mbus_fmt     = sensor_s_fmt,
2707         .g_mbus_fmt     = sensor_g_fmt,
2708         .try_mbus_fmt   = sensor_try_fmt,
2709         .enum_mbus_fmt  = sensor_enum_fmt,
2710 };
2711
2712 static struct v4l2_subdev_ops sensor_subdev_ops = {
2713         .core   = &sensor_subdev_core_ops,
2714         .video = &sensor_subdev_video_ops,
2715 };
2716
2717 static int sensor_probe(struct i2c_client *client,
2718                          const struct i2c_device_id *did)
2719 {
2720     struct sensor *sensor;
2721     struct soc_camera_device *icd = client->dev.platform_data;
2722     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2723     struct soc_camera_link *icl;
2724     int ret;
2725
2726     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2727     if (!icd) {
2728         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2729         return -EINVAL;
2730     }
2731
2732     icl = to_soc_camera_link(icd);
2733     if (!icl) {
2734         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2735         return -EINVAL;
2736     }
2737
2738     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2739         dev_warn(&adapter->dev,
2740                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2741         return -EIO;
2742     }
2743
2744     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2745     if (!sensor)
2746         return -ENOMEM;
2747
2748     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2749
2750     /* Second stage probe - when a capture adapter is there */
2751     icd->ops            = &sensor_ops;
2752     sensor->info_priv.fmt = sensor_colour_fmts[0];
2753         #if CONFIG_SENSOR_I2C_NOSCHED
2754         atomic_set(&sensor->tasklock_cnt,0);
2755         #endif
2756
2757     ret = sensor_video_probe(icd, client);
2758     if (ret < 0) {
2759         icd->ops = NULL;
2760         i2c_set_clientdata(client, NULL);
2761         kfree(sensor);
2762                 sensor = NULL;
2763     }
2764     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2765     return ret;
2766 }
2767
2768 static int sensor_remove(struct i2c_client *client)
2769 {
2770     struct sensor *sensor = to_sensor(client);
2771     struct soc_camera_device *icd = client->dev.platform_data;
2772
2773     icd->ops = NULL;
2774     i2c_set_clientdata(client, NULL);
2775     client->driver = NULL;
2776     kfree(sensor);
2777         sensor = NULL;
2778     return 0;
2779 }
2780
2781 static const struct i2c_device_id sensor_id[] = {
2782         {SENSOR_NAME_STRING(), 0 },
2783         { }
2784 };
2785 MODULE_DEVICE_TABLE(i2c, sensor_id);
2786
2787 static struct i2c_driver sensor_i2c_driver = {
2788         .driver = {
2789                 .name = SENSOR_NAME_STRING(),
2790         },
2791         .probe          = sensor_probe,
2792         .remove         = sensor_remove,
2793         .id_table       = sensor_id,
2794 };
2795
2796 static int __init sensor_mod_init(void)
2797 {
2798     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2799     return i2c_add_driver(&sensor_i2c_driver);
2800 }
2801
2802 static void __exit sensor_mod_exit(void)
2803 {
2804     i2c_del_driver(&sensor_i2c_driver);
2805 }
2806
2807 device_initcall_sync(sensor_mod_init);
2808 module_exit(sensor_mod_exit);
2809
2810 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2811 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2812 MODULE_LICENSE("GPL");
2813
2814