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