camera: all sensor driver(ov3640/ov9650 exception) can be compiled success in rk30...
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / gt2005.c
1 /*
2 o* Driver for MT9M001 CMOS Image Sensor from Micron
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/miscdevice.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/soc_camera.h>
22 #include <plat/rk_camera.h>
23
24 static int debug;
25 module_param(debug, int, S_IRUGO|S_IWUSR);
26
27 #define dprintk(level, fmt, arg...) do {                        \
28         if (debug >= level)                                     \
29         printk(KERN_WARNING fmt , ## arg); } while (0)
30
31 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
32 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
33
34
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
37
38 #define __STR(x) #x
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
41
42 #define MIN(x,y)   ((x<y) ? x: y)
43 #define MAX(x,y)    ((x>y) ? x: y)
44
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK29_CAM_SENSOR_GT2005
47 #define SENSOR_V4L2_IDENT  V4L2_IDENT_GT2005
48 #define SENSOR_ID 0x5138
49 #define SENSOR_MIN_WIDTH    640
50 #define SENSOR_MIN_HEIGHT   480
51 #define SENSOR_MAX_WIDTH    1600
52 #define SENSOR_MAX_HEIGHT   1200
53 #define SENSOR_INIT_WIDTH       640                     /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT  480
55 #define SENSOR_INIT_WINSEQADR sensor_vga
56 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_YUYV8_2X8
57
58 #define CONFIG_SENSOR_WhiteBalance      1
59 #define CONFIG_SENSOR_Brightness        0
60 #define CONFIG_SENSOR_Contrast      0
61 #define CONFIG_SENSOR_Saturation    0
62 #define CONFIG_SENSOR_Effect        1
63 #define CONFIG_SENSOR_Scene         1
64 #define CONFIG_SENSOR_DigitalZoom   0
65 #define CONFIG_SENSOR_Focus         0
66 #define CONFIG_SENSOR_Exposure      0
67 #define CONFIG_SENSOR_Flash         1
68 #define CONFIG_SENSOR_Mirror        0
69 #define CONFIG_SENSOR_Flip          0
70
71 #define CONFIG_SENSOR_I2C_SPEED     250000       /* Hz */
72 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
73 #define CONFIG_SENSOR_I2C_NOSCHED   0
74 #define CONFIG_SENSOR_I2C_RDWRCHK   0
75
76 #define SENSOR_BUS_PARAM  (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
77                           SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |\
78                           SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ)
79
80 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
81 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
82 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
83 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
84 #define COLOR_TEMPERATURE_OFFICE_DN     3500
85 #define COLOR_TEMPERATURE_OFFICE_UP     5000
86 #define COLOR_TEMPERATURE_HOME_DN       2500
87 #define COLOR_TEMPERATURE_HOME_UP       3500
88
89 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
90 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
91
92 #define SENSOR_AF_IS_ERR    (0x00<<0)
93 #define SENSOR_AF_IS_OK         (0x01<<0)
94 #define SENSOR_INIT_IS_ERR   (0x00<<28)
95 #define SENSOR_INIT_IS_OK    (0x01<<28)
96
97 struct reginfo
98 {
99     u16 reg;
100     u8 val;
101 };
102
103 /* init 352X288 SVGA */
104 static struct reginfo sensor_init_data[] =
105 {
106     {0x0101 , 0x00},
107         {0x0103 , 0x00}, 
108         {0x0105 , 0x00},
109         {0x0106 , 0xF0},
110         {0x0107 , 0x00},
111         {0x0108 , 0x1C},
112         {0x0109 , 0x01},
113         {0x010A , 0x00},
114         {0x010B , 0x00},
115         {0x010C , 0x00},
116         {0x010D , 0x08},
117         {0x010E , 0x00},
118         {0x010F , 0x08},
119         {0x0110 , 0x06},
120         {0x0111 , 0x40},
121         {0x0112 , 0x04},
122         {0x0113 , 0xB0},
123 {0x0114 , 0x00},
124         {0x0115 , 0x00},
125         {0x0116 , 0x02},
126         {0x0117 , 0x00},
127 {0x0118 , 0x67},
128         {0x0119 , 0x02},
129         {0x011A , 0x04},
130         {0x011B , 0x01},
131 {0x011C , 0x01},
132 {0x011D , 0x02},
133 {0x011E , 0x00},
134 {0x011F , 0x00},
135 {0x0120 , 0x1C},
136 {0x0121 , 0x00},
137 {0x0122 , 0x04},
138 {0x0123 , 0x00},
139 {0x0124 , 0x00},
140 {0x0125 , 0x00},
141 {0x0126 , 0x00},
142 {0x0127 , 0x00},
143 {0x0128 , 0x00},
144 {0x0200 , 0x00},
145 {0x0201 , 0x00},
146 {0x0202 , 0x40},
147 {0x0203 , 0x00},
148 {0x0204 , 0x03},
149 {0x0205 , 0x1F},
150 {0x0206 , 0x0B},
151 {0x0207 , 0x20},
152 {0x0208 , 0x00},
153 {0x0209 , 0x2A},
154 {0x020A , 0x01},
155 {0x020B , 0x48},
156 {0x020C , 0x64},
157 {0x020D , 0xC8},
158 {0x020E , 0xBC},
159 {0x020F , 0x08},
160 {0x0210 , 0xD6},
161 {0x0211 , 0x00},
162 {0x0212 , 0x20},
163 {0x0213 , 0x81},
164 {0x0214 , 0x15},
165 {0x0215 , 0x00},
166 {0x0216 , 0x00},
167 {0x0217 , 0x00},
168 {0x0218 , 0x46},
169 {0x0219 , 0x30},
170 {0x021A , 0x03},
171 {0x021B , 0x28},
172 {0x021C , 0x02},
173 {0x021D , 0x60},
174 {0x021E , 0x00},
175 {0x021F , 0x00},
176 {0x0220 , 0x08},
177 {0x0221 , 0x08},
178 {0x0222 , 0x04},
179 {0x0223 , 0x00},
180 {0x0224 , 0x1F},
181 {0x0225 , 0x1E},
182 {0x0226 , 0x18},
183 {0x0227 , 0x1D},
184 {0x0228 , 0x1F},
185 {0x0229 , 0x1F},
186 {0x022A , 0x01},
187 {0x022B , 0x04},
188 {0x022C , 0x05},
189 {0x022D , 0x05},
190 {0x022E , 0x04},
191 {0x022F , 0x03},
192 {0x0230 , 0x02},
193 {0x0231 , 0x1F},
194 {0x0232 , 0x1A},
195 {0x0233 , 0x19},
196 {0x0234 , 0x19},
197 {0x0235 , 0x1B},
198 {0x0236 , 0x1F},
199 {0x0237 , 0x04},
200 {0x0238 , 0xEE},
201 {0x0239 , 0xFF},
202 {0x023A , 0x00},
203 {0x023B , 0x00},
204 {0x023C , 0x00},
205 {0x023D , 0x00},
206 {0x023E , 0x00},
207 {0x023F , 0x00},
208 {0x0240 , 0x00},
209 {0x0241 , 0x00},
210 {0x0242 , 0x00},
211 {0x0243 , 0x21},
212 {0x0244 , 0x42},
213 {0x0245 , 0x53},
214 {0x0246 , 0x54},
215 {0x0247 , 0x54},
216 {0x0248 , 0x54},
217 {0x0249 , 0x33},
218 {0x024A , 0x11},
219 {0x024B , 0x00},
220 {0x024C , 0x00},
221 {0x024D , 0xFF},
222 {0x024E , 0xEE},
223 {0x024F , 0xDD},
224 {0x0250 , 0x00},
225 {0x0251 , 0x00},
226 {0x0252 , 0x00},
227 {0x0253 , 0x00},
228 {0x0254 , 0x00},
229 {0x0255 , 0x00},
230 {0x0256 , 0x00},
231 {0x0257 , 0x00},
232 {0x0258 , 0x00},
233 {0x0259 , 0x00},
234 {0x025A , 0x00},
235 {0x025B , 0x00},
236 {0x025C , 0x00},
237 {0x025D , 0x00},
238 {0x025E , 0x00},
239 {0x025F , 0x00},
240 {0x0260 , 0x00},
241 {0x0261 , 0x00},
242 {0x0262 , 0x00},
243 {0x0263 , 0x00},
244 {0x0264 , 0x00},
245 {0x0265 , 0x00},
246 {0x0266 , 0x00},
247 {0x0267 , 0x00},
248 {0x0268 , 0x8F},
249 {0x0269 , 0xA3},
250 {0x026A , 0xB4},
251 {0x026B , 0x90},
252 {0x026C , 0x00},
253 {0x026D , 0xD0},
254 {0x026E , 0x60},
255 {0x026F , 0xA0},
256 {0x0270 , 0x40},
257 {0x0300 , 0x81},
258 {0x0301 , 0x80},
259 {0x0302 , 0x22},
260 {0x0303 , 0x06},
261 {0x0304 , 0x03},
262 {0x0305 , 0x83},
263 {0x0306 , 0x00},
264 {0x0307 , 0x22},
265 {0x0308 , 0x00},
266 {0x0309 , 0x55},
267 {0x030A , 0x55},
268 {0x030B , 0x55},
269 {0x030C , 0x54},
270 {0x030D , 0x1F},
271 {0x030E , 0x13},
272 {0x030F , 0x10},
273 {0x0310 , 0x04},
274 {0x0311 , 0xFF},
275 {0x0312 , 0x08},
276 {0x0313 , 0x28},
277 {0x0314 , 0x66},
278 {0x0315 , 0x16},
279 {0x0316 , 0x26},
280 {0x0317 , 0x02},
281 {0x0318 , 0x08},
282 {0x0319 , 0x0C},
283 {0x031A , 0x81},
284 {0x031B , 0x00},
285 {0x031C , 0x3D},
286 {0x031D , 0x00},
287 {0x031E , 0xF9},
288 {0x031F , 0x00},
289 {0x0320 , 0x24},
290 {0x0321 , 0x14},
291 {0x0322 , 0x1A},
292 {0x0323 , 0x24},
293 {0x0324 , 0x08},
294 {0x0325 , 0xF0},
295 {0x0326 , 0x30}, 
296 {0x0327 , 0x17},
297 {0x0328 , 0x11},
298 {0x0329 , 0x22},
299 {0x032A , 0x2F},
300 {0x032B , 0x21},
301 {0x032C , 0xDA},
302 {0x032D , 0x10},
303 {0x032E , 0xEA},
304 {0x032F , 0x18},
305 {0x0330 , 0x29},
306 {0x0331 , 0x25},
307 {0x0332 , 0x12},
308 {0x0333 , 0x0F},
309 {0x0334 , 0xE0},
310 {0x0335 , 0x13},
311 {0x0336 , 0xFF},
312 {0x0337 , 0x20},
313 {0x0338 , 0x46},
314 {0x0339 , 0x04},
315 {0x033A , 0x04},
316 {0x033B , 0xFF},
317 {0x033C , 0x01},
318 {0x033D , 0x00},
319 {0x033E , 0x03},
320 {0x033F , 0x28},
321 {0x0340 , 0x02},
322 {0x0341 , 0x60},
323 {0x0342 , 0xAC},
324 {0x0343 , 0x97},
325 {0x0344 , 0x7F},
326 {0x0400 , 0xE8},
327 {0x0401 , 0x40},
328 {0x0402 , 0x00},
329 {0x0403 , 0x00},
330 {0x0404 , 0xF8},
331 {0x0405 , 0x03},
332 {0x0406 , 0x03},
333 {0x0407 , 0x85},
334 {0x0408 , 0x44},
335 {0x0409 , 0x1F},
336 {0x040A , 0x40},
337 {0x040B , 0x33},
338 {0x040C , 0xA0},
339 {0x040D , 0x00},
340 {0x040E , 0x00},
341 {0x040F , 0x00},
342 {0x0410 , 0x0D},
343 {0x0411 , 0x0D},
344 {0x0412 , 0x0C},
345 {0x0413 , 0x04},
346 {0x0414 , 0x00},
347 {0x0415 , 0x00},
348 {0x0416 , 0x07},
349 {0x0417 , 0x09},
350 {0x0418 , 0x16},
351 {0x0419 , 0x14},
352 {0x041A , 0x11},
353 {0x041B , 0x14},
354 {0x041C , 0x07},
355 {0x041D , 0x07},
356 {0x041E , 0x06},
357 {0x041F , 0x02},
358 {0x0420 , 0x42},
359 {0x0421 , 0x42},
360 {0x0422 , 0x47},
361 {0x0423 , 0x39},
362 {0x0424 , 0x3E},
363 {0x0425 , 0x4D},
364 {0x0426 , 0x46},
365 {0x0427 , 0x3A},
366 {0x0428 , 0x21},
367 {0x0429 , 0x21},
368 {0x042A , 0x26},
369 {0x042B , 0x1C},
370 {0x042C , 0x25},
371 {0x042D , 0x25},
372 {0x042E , 0x28},
373 {0x042F , 0x20},
374 {0x0430 , 0x3E},
375 {0x0431 , 0x3E},
376 {0x0432 , 0x33},
377 {0x0433 , 0x2E},
378 {0x0434 , 0x54},
379 {0x0435 , 0x53},
380 {0x0436 , 0x3C},
381 {0x0437 , 0x51},
382 {0x0438 , 0x2B},
383 {0x0439 , 0x2B},
384 {0x043A , 0x38},
385 {0x043B , 0x22},
386 {0x043C , 0x3B},
387 {0x043D , 0x3B},
388 {0x043E , 0x31},
389 {0x043F , 0x37},
390 {0x0440 , 0x00},
391 {0x0441 , 0x4B},
392 {0x0442 , 0x00},
393 {0x0443 , 0x00},
394 {0x0444 , 0x31},
395 {0x0445 , 0x00},
396 {0x0446 , 0x00},
397 {0x0447 , 0x00},
398 {0x0448 , 0x00},
399 {0x0449 , 0x00},
400 {0x044A , 0x00},
401 {0x044D , 0xE0},
402 {0x044E , 0x05},
403 {0x044F , 0x07},
404 {0x0450 , 0x00},
405 {0x0451 , 0x00},
406 {0x0452 , 0x00},
407 {0x0453 , 0x00},
408 {0x0454 , 0x00},
409 {0x0455 , 0x00},
410 {0x0456 , 0x00},
411 {0x0457 , 0x00},
412 {0x0458 , 0x00},
413 {0x0459 , 0x00},
414 {0x045A , 0x00},
415 {0x045B , 0x00},
416 {0x045C , 0x00},
417 {0x045D , 0x00},
418 {0x045E , 0x00},
419 {0x045F , 0x00},
420 {0x0460 , 0x80},
421 {0x0461 , 0x10},
422 {0x0462 , 0x10},
423 {0x0463 , 0x10},
424 {0x0464 , 0x08},
425 {0x0465 , 0x08},
426 {0x0466 , 0x11},
427 {0x0467 , 0x09},
428 {0x0468 , 0x23},
429 {0x0469 , 0x2A},
430 {0x046A , 0x2A},
431 {0x046B , 0x47},
432 {0x046C , 0x52},
433 {0x046D , 0x42},
434 {0x046E , 0x36},
435 {0x046F , 0x46},
436 {0x0470 , 0x3A},
437 {0x0471 , 0x32},
438 {0x0472 , 0x32},
439 {0x0473 , 0x38},
440 {0x0474 , 0x3D},
441 {0x0475 , 0x2F},
442 {0x0476 , 0x29},
443 {0x0477 , 0x48},
444 {0x0600 , 0x00},
445 {0x0601 , 0x24},
446 {0x0602 , 0x45},
447 {0x0603 , 0x0E},
448 {0x0604 , 0x14},
449 {0x0605 , 0x2F},
450 {0x0606 , 0x01},
451 {0x0607 , 0x0E},
452 {0x0608 , 0x0E},
453 {0x0609 , 0x37},
454 {0x060A , 0x18},
455 {0x060B , 0xA0},
456 {0x060C , 0x20},
457 {0x060D , 0x07},
458 {0x060E , 0x47},
459 {0x060F , 0x90},
460 {0x0610 , 0x06},
461 {0x0611 , 0x0C},
462 {0x0612 , 0x28},
463 {0x0613 , 0x13},
464 {0x0614 , 0x0B},
465 {0x0615 , 0x10},
466 {0x0616 , 0x14},
467 {0x0617 , 0x19},
468 {0x0618 , 0x52},
469 {0x0619 , 0xA0},
470 {0x061A , 0x11},
471 {0x061B , 0x33},
472 {0x061C , 0x56},
473 {0x061D , 0x20},
474 {0x061E , 0x28},
475 {0x061F , 0x2B},
476 {0x0620 , 0x22},
477 {0x0621 , 0x11},
478 {0x0622 , 0x75},
479 {0x0623 , 0x49},
480 {0x0624 , 0x6E},
481 {0x0625 , 0x80},
482 {0x0626 , 0x02},
483 {0x0627 , 0x0C},
484 {0x0628 , 0x51},
485 {0x0629 , 0x25},
486 {0x062A , 0x01},
487 {0x062B , 0x3D},
488 {0x062C , 0x04},
489 {0x062D , 0x01},
490 {0x062E , 0x0C},
491 {0x062F , 0x2C},
492 {0x0630 , 0x0D},
493 {0x0631 , 0x14},
494 {0x0632 , 0x12},
495 {0x0633 , 0x34},
496 {0x0634 , 0x00},
497 {0x0635 , 0x00},
498 {0x0636 , 0x00},
499 {0x0637 , 0xB1},
500 {0x0638 , 0x22},
501 {0x0639 , 0x32},
502 {0x063A , 0x0E},
503 {0x063B , 0x18},
504 {0x063C , 0x88},
505 {0x0640 , 0xB2},
506 {0x0641 , 0xC0},
507 {0x0642 , 0x01},
508 {0x0643 , 0x26},
509 {0x0644 , 0x13},
510 {0x0645 , 0x88},
511 {0x0646 , 0x64},
512 {0x0647 , 0x00},
513 {0x0681 , 0x1B},
514 {0x0682 , 0xA0},
515 {0x0683 , 0x28},
516 {0x0684 , 0x00},
517 {0x0685 , 0xB0},
518 {0x0686 , 0x6F},
519 {0x0687 , 0x33},
520 {0x0688 , 0x1F},
521 {0x0689 , 0x44},
522 {0x068A , 0xA8},
523 {0x068B , 0x44},
524 {0x068C , 0x08},
525 {0x068D , 0x08},
526 {0x068E , 0x00},
527 {0x068F , 0x00},
528 {0x0690 , 0x01},
529 {0x0691 , 0x00},
530 {0x0692 , 0x01},
531 {0x0693 , 0x00},
532 {0x0694 , 0x00},
533 {0x0695 , 0x00},
534 {0x0696 , 0x00},
535 {0x0697 , 0x00},
536 {0x0698 , 0x2A},
537 {0x0699 , 0x80},
538 {0x069A , 0x1F},
539 {0x069B , 0x00},
540 {0x069C , 0x02},
541 {0x069D , 0xF5},
542 {0x069E , 0x03},
543 {0x069F , 0x6D},
544 {0x06A0 , 0x0C},
545 {0x06A1 , 0xB8},
546 {0x06A2 , 0x0D},
547 {0x06A3 , 0x74},
548 {0x06A4 , 0x00},
549 {0x06A5 , 0x2F},
550 {0x06A6 , 0x00},
551 {0x06A7 , 0x2F},
552 {0x0F00 , 0x00},
553 {0x0F01 , 0x00},
554 {0x0100 , 0x01},
555 {0x0102 , 0x02},
556 {0x0104 , 0x03},
557 {0x0101 , 0x02},
558
559 ///////////////////////////
560 {0x020B , 0x48},
561 {0x020C , 0x64},
562 {0x040A , 0x40},
563 {0x040B , 0x33},
564 {0x0109 , 0x00},
565 {0x010A , 0x04},
566 {0x010B , 0x03},
567 #if 0
568 {0x0110 , 0x02},
569 {0x0111 , 0x80},
570 {0x0112 , 0x01},
571 {0x0113 , 0xe0},
572 #else
573 {0x0110, 0x03},
574 {0x0111, 0x20},
575 {0x0112, 0x02},
576 {0x0113, 0x58},
577
578 #endif
579 {0x0116 , 0x02},
580 {0x0118 , 0x40},
581 {0x0119 , 0x01},
582 {0x011a , 0x04},
583 {0x011B , 0x00},
584 {0x0313 , 0x35},
585 {0x0314 , 0x36},
586 {0x0315 , 0x16},
587
588 };
589         
590
591
592 /* 1600X1200 UXGA */
593 static struct reginfo sensor_uxga[] = 
594 {
595     {0x010c , 0x00},
596         {0x010d , 0x08},
597         {0x010e , 0x00},
598         {0x010f , 0x08},
599         {0x010a , 0x00},
600         {0x0110 , 0x06},
601         {0x0111 , 0x40},
602         {0x0112 , 0x04},
603         {0x0113 , 0xb0},
604         {0x0, 0x0},
605 };
606
607         
608
609 /* 1280X1024 SXGA */
610 static struct reginfo sensor_sxga[] =
611 {
612     {0x010c , 0x00},
613         {0x010d , 0xa8},
614         {0x010e , 0x00},
615         {0x010f , 0x60},
616         {0x010a , 0x00},
617         {0x0110 , 0x05},
618         {0x0111 , 0x00},
619         {0x0112 , 0x04},
620         {0x0113 , 0x00}, 
621         {0x00, 0x00},
622 };
623
624 /* 800X600 SVGA*/
625 static struct reginfo sensor_svga[] =
626 {       
627 #if 0   
628         {0x0101, 0x00},
629         {0x0103, 0x00},
630         {0x0105, 0x00},
631         {0x0106, 0xF0},
632         {0x0107, 0x00},
633         {0x0108, 0x1C},
634         {0x0109, 0x01},
635         {0x010A, 0x00},
636         {0x010B, 0x00},
637         {0x010C, 0x00},
638         {0x010D, 0x08},
639         {0x010E, 0x00},
640         {0x010F, 0x08},
641         {0x0110, 0x06},
642         {0x0111, 0x40},
643         {0x0112, 0x04},
644         {0x0113, 0xB0},
645         {0x0114, 0x04},
646         {0x0115, 0x00},
647         {0x0116, 0x02},
648         {0x0117, 0x00},
649         {0x0118, 0x40},
650         {0x0119, 0x02},
651         {0x011A, 0x04},
652         {0x011B, 0x01},
653         {0x011C, 0x00},
654         {0x011D, 0x01},
655         {0x011E, 0x36},
656         {0x011F, 0x00},
657         {0x0120, 0x1C},
658         {0x0121, 0x00},
659         {0x0122, 0x04},
660         {0x0123, 0x00},
661         {0x0124, 0x00},
662         {0x0125, 0x00},
663         {0x0126, 0x00},
664         {0x0127, 0x00},
665         {0x0128, 0x00},
666         {0x0200, 0x1f},
667         {0x0201, 0x0c},
668         {0x0202, 0x38},
669         {0x0203, 0x00},
670         {0x0204, 0x03},
671         {0x0205, 0x1F},
672         {0x0206, 0x0B},
673         {0x0207, 0x20},
674         {0x0208, 0x00},
675         {0x0209, 0x2A},
676         {0x020A, 0x01},
677         {0x020B, 0x28},
678         {0x020C, 0x44},
679         {0x020D, 0xC8},
680         {0x020E, 0xBC},
681         {0x020F, 0x08},
682         {0x0210, 0xD6},
683         {0x0211, 0x00},
684         {0x0212, 0x20},
685         {0x0213, 0x81},
686         {0x0214, 0x15},
687         {0x0215, 0x00},
688         {0x0216, 0x00},
689         {0x0217, 0x00},
690         {0x0218, 0x46},
691         {0x0219, 0x30},
692         {0x021A, 0x03},
693         {0x021B, 0x28},
694         {0x021C, 0x02},
695         {0x021D, 0x60},
696         {0x021E, 0x00},
697         {0x021F, 0x00},
698         {0x0220, 0x08},
699         {0x0221, 0x08},
700         {0x0222, 0x04},
701         {0x0223, 0x00},
702         {0x0224, 0x1F},
703         {0x0225, 0x1E},
704         {0x0226, 0x18},
705         {0x0227, 0x1D},
706         {0x0228, 0x1F},
707         {0x0229, 0x1F},
708         {0x022A, 0x01},
709         {0x022B, 0x04},
710         {0x022C, 0x05},
711         {0x022D, 0x05},
712         {0x022E, 0x04},
713         {0x022F, 0x03},
714         {0x0230, 0x02},
715         {0x0231, 0x1F},
716         {0x0232, 0x1A},
717         {0x0233, 0x19},
718         {0x0234, 0x19},
719         {0x0235, 0x1B},
720         {0x0236, 0x1F},
721         {0x0237, 0x04},
722         {0x0238, 0xEE},
723         {0x0239, 0xFF},
724         {0x023A, 0x00},
725         {0x023B, 0x00},
726         {0x023C, 0x00},
727         {0x023D, 0x00},
728         {0x023E, 0x00},
729         {0x023F, 0x00},
730         {0x0240, 0x00},
731         {0x0241, 0x00},
732         {0x0242, 0x00},
733         {0x0243, 0x21},
734         {0x0244, 0x42},
735         {0x0245, 0x53},
736         {0x0246, 0x54},
737         {0x0247, 0x54},
738         {0x0248, 0x54},
739         {0x0249, 0x33},
740         {0x024A, 0x11},
741         {0x024B, 0x00},
742         {0x024C, 0x00},
743         {0x024D, 0xFF},
744         {0x024E, 0xEE},
745         {0x024F, 0xDD},
746         {0x0250, 0x00},
747         {0x0251, 0x00},
748         {0x0252, 0x00},
749         {0x0253, 0x00},
750         {0x0254, 0x00},
751         {0x0255, 0x00},
752         {0x0256, 0x00},
753         {0x0257, 0x00},
754         {0x0258, 0x00},
755         {0x0259, 0x00},
756         {0x025A, 0x00},
757         {0x025B, 0x00},
758         {0x025C, 0x00},
759         {0x025D, 0x00},
760         {0x025E, 0x00},
761         {0x025F, 0x00},
762         {0x0260, 0x00},
763         {0x0261, 0x00},
764         {0x0262, 0x00},
765         {0x0263, 0x00},
766         {0x0264, 0x00},
767         {0x0265, 0x00},
768         {0x0266, 0x00},
769         {0x0267, 0x00},
770         {0x0268, 0x8F},
771         {0x0269, 0xA3},
772         {0x026A, 0xB4},
773         {0x026B, 0x90},
774         {0x026C, 0x00},
775         {0x026D, 0xD0},
776         {0x026E, 0x60},
777         {0x026F, 0xA0},
778         {0x0270, 0x40},
779         {0x0300, 0x81},
780         {0x0301, 0x80},
781         {0x0302, 0x22},
782         {0x0303, 0x06},
783         {0x0304, 0x03},
784         {0x0305, 0x83},
785         {0x0306, 0x00},
786         {0x0307, 0x22},
787         {0x0308, 0x00},
788         {0x0309, 0x55},
789         {0x030A, 0x55},
790         {0x030B, 0x55},
791         {0x030C, 0x54},
792         {0x030D, 0x1F},
793         {0x030E, 0x0A},
794         {0x030F, 0x10},
795         {0x0310, 0x04},
796         {0x0311, 0xFF},
797         {0x0312, 0x08},
798         {0x0313, 0x35},
799         {0x0314, 0x36},
800         {0x0315, 0x15},
801         {0x0316, 0x26},
802         {0x0317, 0x02},
803         {0x0318, 0x08},
804         {0x0319, 0x0C},
805         {0x031A, 0x81},
806         {0x031B, 0x00},
807         {0x031C, 0x3D},
808         {0x031D, 0x00},
809         {0x031E, 0xF9},
810         {0x031F, 0x00},
811         {0x0320, 0x24},
812         {0x0321, 0x14},
813         {0x0322, 0x1A},
814         {0x0323, 0x24},
815         {0x0324, 0x08},
816         {0x0325, 0xF0},
817         {0x0326, 0x30},
818         {0x0327, 0x17},
819         {0x0328, 0x11},
820         {0x0329, 0x22},
821         {0x032A, 0x2F},
822         {0x032B, 0x21},
823         {0x032C, 0xDA},
824         {0x032D, 0x10},
825         {0x032E, 0xEA},
826         {0x032F, 0x18},
827         {0x0330, 0x29},
828         {0x0331, 0x25},
829         {0x0332, 0x12},
830         {0x0333, 0x0F},
831         {0x0334, 0xE0},
832         {0x0335, 0x13},
833         {0x0336, 0xFF},
834         {0x0337, 0x20},
835         {0x0338, 0x46},
836         {0x0339, 0x04},
837         {0x033A, 0x04},
838         {0x033B, 0xFF},
839         {0x033C, 0x01},
840         {0x033D, 0x00},
841         {0x033E, 0x03},
842         {0x033F, 0x28},
843         {0x0340, 0x02},
844         {0x0341, 0x60},
845         {0x0342, 0xAC},
846         {0x0343, 0x97},
847         {0x0344, 0x7F},
848         {0x0400, 0xE8},
849         {0x0401, 0x40},
850         {0x0402, 0x00},
851         {0x0403, 0x00},
852         {0x0404, 0xF8},
853         {0x0405, 0x03},
854         {0x0406, 0x03},
855         {0x0407, 0x85},
856         {0x0408, 0x44},
857         {0x0409, 0x1F},
858         {0x040A, 0x40},
859         {0x040B, 0x33},
860         {0x040C, 0xA0},
861         {0x040D, 0x00},
862         {0x040E, 0x00},
863         {0x040F, 0x00},
864         {0x0410, 0x0D},
865         {0x0411, 0x0D},
866         {0x0412, 0x0C},
867         {0x0413, 0x04},
868         {0x0414, 0x00},
869         {0x0415, 0x00},
870         {0x0416, 0x07},
871         {0x0417, 0x09},
872         {0x0418, 0x16},
873         {0x0419, 0x14},
874         {0x041A, 0x11},
875         {0x041B, 0x14},
876         {0x041C, 0x07},
877         {0x041D, 0x07},
878         {0x041E, 0x06},
879         {0x041F, 0x02},
880         {0x0420, 0x42},
881         {0x0421, 0x42},
882         {0x0422, 0x47},
883         {0x0423, 0x39},
884         {0x0424, 0x3E},
885         {0x0425, 0x4D},
886         {0x0426, 0x46},
887         {0x0427, 0x3A},
888         {0x0428, 0x21},
889         {0x0429, 0x21},
890         {0x042A, 0x26},
891         {0x042B, 0x1C},
892         {0x042C, 0x25},
893         {0x042D, 0x25},
894         {0x042E, 0x28},
895         {0x042F, 0x20},
896         {0x0430, 0x3E},
897         {0x0431, 0x3E},
898         {0x0432, 0x33},
899         {0x0433, 0x2E},
900         {0x0434, 0x54},
901         {0x0435, 0x53},
902         {0x0436, 0x3C},
903         {0x0437, 0x51},
904         {0x0438, 0x2B},
905         {0x0439, 0x2B},
906         {0x043A, 0x38},
907         {0x043B, 0x22},
908         {0x043C, 0x3B},
909         {0x043D, 0x3B},
910         {0x043E, 0x31},
911         {0x043F, 0x37},
912         {0x0440, 0x00},
913         {0x0441, 0x4B},
914         {0x0442, 0x00},
915         {0x0443, 0x00},
916         {0x0444, 0x31},
917         {0x0445, 0x00},
918         {0x0446, 0x00},
919         {0x0447, 0x00},
920         {0x0448, 0x00},
921         {0x0449, 0x00},
922         {0x044A, 0x00},
923         {0x044D, 0xE0},
924         {0x044E, 0x05},
925         {0x044F, 0x07},
926         {0x0450, 0x00},
927         {0x0451, 0x00},
928         {0x0452, 0x00},
929         {0x0453, 0x00},
930         {0x0454, 0x00},
931         {0x0455, 0x00},
932         {0x0456, 0x00},
933         {0x0457, 0x00},
934         {0x0458, 0x00},
935         {0x0459, 0x00},
936         {0x045A, 0x00},
937         {0x045B, 0x00},
938         {0x045C, 0x00},
939         {0x045D, 0x00},
940         {0x045E, 0x00},
941         {0x045F, 0x00},
942         {0x0460, 0x80},
943         {0x0461, 0x10},
944         {0x0462, 0x10},
945         {0x0463, 0x10},
946         {0x0464, 0x08},
947         {0x0465, 0x08},
948         {0x0466, 0x11},
949         {0x0467, 0x09},
950         {0x0468, 0x23},
951         {0x0469, 0x2A},
952         {0x046A, 0x2A},
953         {0x046B, 0x47},
954         {0x046C, 0x52},
955         {0x046D, 0x42},
956         {0x046E, 0x36},
957         {0x046F, 0x46},
958         {0x0470, 0x3A},
959         {0x0471, 0x32},
960         {0x0472, 0x32},
961         {0x0473, 0x38},
962         {0x0474, 0x3D},
963         {0x0475, 0x2F},
964         {0x0476, 0x29},
965         {0x0477, 0x48},
966         {0x0600, 0x00},
967         {0x0601, 0x24},
968         {0x0602, 0x45},
969         {0x0603, 0x0E},
970         {0x0604, 0x14},
971         {0x0605, 0x2F},
972         {0x0606, 0x01},
973         {0x0607, 0x0E},
974         {0x0608, 0x0E},
975         {0x0609, 0x37},
976         {0x060A, 0x18},
977         {0x060B, 0xA0},
978         {0x060C, 0x20},
979         {0x060D, 0x07},
980         {0x060E, 0x47},
981         {0x060F, 0x90},
982         {0x0610, 0x06},
983         {0x0611, 0x0C},
984         {0x0612, 0x28},
985         {0x0613, 0x13},
986         {0x0614, 0x0B},
987         {0x0615, 0x10},
988         {0x0616, 0x14},
989         {0x0617, 0x19},
990         {0x0618, 0x52},
991         {0x0619, 0xA0},
992         {0x061A, 0x11},
993         {0x061B, 0x33},
994         {0x061C, 0x56},
995         {0x061D, 0x20},
996         {0x061E, 0x28},
997         {0x061F, 0x2B},
998         {0x0620, 0x22},
999         {0x0621, 0x11},
1000         {0x0622, 0x75},
1001         {0x0623, 0x49},
1002         {0x0624, 0x6E},
1003         {0x0625, 0x80},
1004         {0x0626, 0x02},
1005         {0x0627, 0x0C},
1006         {0x0628, 0x51},
1007         {0x0629, 0x25},
1008         {0x062A, 0x01},
1009         {0x062B, 0x3D},
1010         {0x062C, 0x04},
1011         {0x062D, 0x01},
1012         {0x062E, 0x0C},
1013         {0x062F, 0x2C},
1014         {0x0630, 0x0D},
1015         {0x0631, 0x14},
1016         {0x0632, 0x12},
1017         {0x0633, 0x34},
1018         {0x0634, 0x00},
1019         {0x0635, 0x00},
1020         {0x0636, 0x00},
1021         {0x0637, 0xB1},
1022         {0x0638, 0x22},
1023         {0x0639, 0x32},
1024         {0x063A, 0x0E},
1025         {0x063B, 0x18},
1026         {0x063C, 0x88},
1027         {0x0640, 0xB2},
1028         {0x0641, 0xC0},
1029         {0x0642, 0x01},
1030         {0x0643, 0x26},
1031         {0x0644, 0x13},
1032         {0x0645, 0x88},
1033         {0x0646, 0x64},
1034         {0x0647, 0x00},
1035         {0x0681, 0x1B},
1036         {0x0682, 0xA0},
1037         {0x0683, 0x28},
1038         {0x0684, 0x00},
1039         {0x0685, 0xB0},
1040         {0x0686, 0x6F},
1041         {0x0687, 0x33},
1042         {0x0688, 0x1F},
1043         {0x0689, 0x44},
1044         {0x068A, 0xA8},
1045         {0x068B, 0x44},
1046         {0x068C, 0x08},
1047         {0x068D, 0x08},
1048         {0x068E, 0x00},
1049         {0x068F, 0x00},
1050         {0x0690, 0x01},
1051         {0x0691, 0x00},
1052         {0x0692, 0x01},
1053         {0x0693, 0x00},
1054         {0x0694, 0x00},
1055         {0x0695, 0x00},
1056         {0x0696, 0x00},
1057         {0x0697, 0x00},
1058         {0x0698, 0x2A},
1059         {0x0699, 0x80},
1060         {0x069A, 0x1F},
1061         {0x069B, 0x00},
1062         {0x069C, 0x02},
1063         {0x069D, 0xF5},
1064         {0x069E, 0x03},
1065         {0x069F, 0x6D},
1066         {0x06A0, 0x0C},
1067         {0x06A1, 0xB8},
1068         {0x06A2, 0x0D},
1069         {0x06A3, 0x74},
1070         {0x06A4, 0x00},
1071         {0x06A5, 0x2F},
1072         {0x06A6, 0x00},
1073         {0x06A7, 0x2F},
1074         {0x0F00, 0x00},
1075         {0x0F01, 0x00},
1076         {0x0100, 0x01},
1077         {0x0102, 0x02},
1078         {0x0104, 0x03},
1079 #endif
1080         {0x020B, 0x48},
1081         {0x020C, 0x64},
1082         {0x040A, 0x40},
1083         {0x040B, 0x33},
1084         {0x010c , 0x00},
1085         {0x010d , 0x08},
1086         {0x010e , 0x00},
1087         {0x010f , 0x08},
1088         {0x010a , 0x00},
1089         {0x0109, 0x00},
1090         {0x010A, 0x04},
1091         {0x010B, 0x03},
1092         {0x0110, 0x03},
1093         {0x0111, 0x20},
1094         {0x0112, 0x02},
1095         {0x0113, 0x58},
1096         {0x0116, 0x02},
1097         {0x0118, 0x40},
1098         {0x0119, 0x02},
1099         {0x011a, 0x04},
1100         {0x011B, 0x01},
1101         {0x0, 0x0},
1102 };
1103         
1104         
1105
1106 /* 640X480 VGA */
1107 static struct reginfo sensor_vga[] =
1108 {
1109    {0x020B , 0x48},
1110    {0x020C , 0x64},
1111    {0x040A , 0x40},
1112    {0x040B , 0x33},
1113    {0x0109 , 0x00},
1114    {0x010A , 0x04},
1115    {0x010B , 0x03},
1116    {0x010c , 0x00},
1117    {0x010d , 0xa8},
1118    {0x010e , 0x00},
1119    {0x010f , 0x60},
1120    {0x010a , 0x04},
1121    #if 1
1122    {0x0110 , 0x02},
1123    {0x0111 , 0x80},
1124    {0x0112 , 0x01},
1125    {0x0113 , 0xe0},
1126    #else
1127    {0x0110, 0x03},
1128    {0x0111, 0x20},
1129    {0x0112, 0x02},
1130    {0x0113, 0x58},
1131    #endif
1132    {0x0116 , 0x02},
1133    {0x0118 , 0x40},
1134    {0x0119 , 0x01},
1135    {0x011a , 0x04},
1136    {0x011B , 0x00},
1137    {0x0313 , 0x35},
1138    {0x0314 , 0x36},
1139    {0x0315 , 0x16}, 
1140    {0x0, 0x0},
1141 };
1142
1143 /* 352X288 CIF */
1144 static struct reginfo sensor_cif[] =
1145 {
1146     {0x0, 0x0},
1147 };
1148
1149 /* 320*240 QVGA */
1150 static  struct reginfo sensor_qvga[] =
1151 {
1152     
1153
1154     {0x0, 0x0},
1155 };
1156
1157 /* 176X144 QCIF*/
1158 static struct reginfo sensor_qcif[] =
1159 {
1160         
1161     {0x0, 0x0},
1162 };
1163 #if 0
1164 /* 160X120 QQVGA*/
1165 static struct reginfo gt2005_qqvga[] =
1166 {
1167
1168     {0x300E, 0x34},
1169     {0x3011, 0x01},
1170     {0x3012, 0x10},
1171     {0x302a, 0x02},
1172     {0x302b, 0xE6},
1173     {0x306f, 0x14},
1174     {0x3362, 0x90},
1175
1176     {0x3070, 0x5d},
1177     {0x3072, 0x5d},
1178     {0x301c, 0x07},
1179     {0x301d, 0x07},
1180
1181     {0x3020, 0x01},
1182     {0x3021, 0x18},
1183     {0x3022, 0x00},
1184     {0x3023, 0x06},
1185     {0x3024, 0x06},
1186     {0x3025, 0x58},
1187     {0x3026, 0x02},
1188     {0x3027, 0x61},
1189     {0x3088, 0x00},
1190     {0x3089, 0xa0},
1191     {0x308a, 0x00},
1192     {0x308b, 0x78},
1193     {0x3316, 0x64},
1194     {0x3317, 0x25},
1195     {0x3318, 0x80},
1196     {0x3319, 0x08},
1197     {0x331a, 0x0a},
1198     {0x331b, 0x07},
1199     {0x331c, 0x80},
1200     {0x331d, 0x38},
1201     {0x3100, 0x00},
1202     {0x3302, 0x11},
1203
1204     {0x0, 0x0},
1205 };
1206
1207
1208
1209 static  struct reginfo gt2005_Sharpness_auto[] =
1210 {
1211     {0x3306, 0x00},
1212 };
1213
1214 static  struct reginfo gt2005_Sharpness1[] =
1215 {
1216     {0x3306, 0x08},
1217     {0x3371, 0x00},
1218 };
1219
1220 static  struct reginfo gt2005_Sharpness2[][3] =
1221 {
1222     //Sharpness 2
1223     {0x3306, 0x08},
1224     {0x3371, 0x01},
1225 };
1226
1227 static  struct reginfo gt2005_Sharpness3[] =
1228 {
1229     //default
1230     {0x3306, 0x08},
1231     {0x332d, 0x02},
1232 };
1233 static  struct reginfo gt2005_Sharpness4[]=
1234 {
1235     //Sharpness 4
1236     {0x3306, 0x08},
1237     {0x332d, 0x03},
1238 };
1239
1240 static  struct reginfo gt2005_Sharpness5[] =
1241 {
1242     //Sharpness 5
1243     {0x3306, 0x08},
1244     {0x332d, 0x04},
1245 };
1246 #endif
1247
1248 static  struct reginfo sensor_ClrFmt_YUYV[]=
1249 {
1250     //{0x3400, 0x00},
1251     {0x0000, 0x00}
1252 };
1253
1254 static  struct reginfo sensor_ClrFmt_UYVY[]=
1255 {
1256     //{0x3400, 0x02},
1257     {0x0000, 0x00}
1258 };
1259
1260 #if CONFIG_SENSOR_WhiteBalance
1261 static  struct reginfo sensor_WhiteB_Auto[]=
1262 {
1263     {0x3306, 0x00},  //AWB auto, bit[1]:0,auto
1264     {0x0000, 0x00}
1265 };
1266 /* Cloudy Colour Temperature : 6500K - 8000K  */
1267 static  struct reginfo sensor_WhiteB_Cloudy[]=
1268 {
1269     {0x3306, 0x82},
1270     {0x3337, 0x68},
1271     {0x3338, 0x40},
1272     {0x3339, 0x4e},
1273     {0x0000, 0x00}
1274 };
1275 /* ClearDay Colour Temperature : 5000K - 6500K  */
1276 static  struct reginfo sensor_WhiteB_ClearDay[]=
1277 {
1278     //Sunny
1279     {0x3306, 0x02}, //AWB off
1280     {0x3337, 0x5e},
1281     {0x3338, 0x40},
1282     {0x3339, 0x46},
1283     {0x0000, 0x00}
1284 };
1285 /* Office Colour Temperature : 3500K - 5000K  */
1286 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
1287 {
1288     //Office
1289     {0x3306, 0x02},
1290     {0x3337, 0x52},
1291     {0x3338, 0x40},
1292     {0x3339, 0x58},
1293     {0x0000, 0x00}
1294
1295 };
1296 /* Home Colour Temperature : 2500K - 3500K  */
1297 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
1298 {
1299     //Home
1300     {0x3306, 0x02},
1301     {0x3337, 0x44},
1302     {0x3338, 0x40},
1303     {0x3339, 0x70},
1304     {0x0000, 0x00}
1305 };
1306 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
1307     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
1308 };
1309 #endif
1310
1311 #if CONFIG_SENSOR_Brightness
1312 static  struct reginfo sensor_Brightness0[]=
1313 {
1314     // Brightness -2
1315     {0x3301, 0xff},//bit[7]:1, enable SDE
1316     {0x3391, 0x04},
1317     {0x3390, 0x49},
1318     {0x339a, 0x20},
1319     {0x0000, 0x00}
1320 };
1321
1322 static  struct reginfo sensor_Brightness1[]=
1323 {
1324     // Brightness -1
1325     {0x3301, 0xff},//bit[7]:1, enable SDE
1326     {0x3391, 0x04},
1327     {0x3390, 0x49},
1328     {0x339a, 0x10},
1329     {0x0000, 0x00}
1330 };
1331
1332 static  struct reginfo sensor_Brightness2[]=
1333 {
1334     //  Brightness 0
1335     {0x3301, 0xff},//bit[7]:1, enable SDE
1336     {0x3391, 0x00},
1337     {0x3390, 0x41},
1338     {0x339a, 0x00},
1339     {0x0000, 0x00}
1340 };
1341
1342 static  struct reginfo sensor_Brightness3[]=
1343 {
1344     // Brightness +1
1345     {0x3301, 0xff},//bit[7]:1, enable SDE
1346     {0x3391, 0x04},
1347     {0x3390, 0x41},
1348     {0x339a, 0x10},
1349     {0x0000, 0x00}
1350 };
1351
1352 static  struct reginfo sensor_Brightness4[]=
1353 {
1354     //  Brightness +2
1355     {0x3301, 0xff},//bit[7]:1, enable SDE
1356     {0x3391, 0x04},
1357     {0x3390, 0x41},
1358     {0x339a, 0x20},
1359     {0x0000, 0x00}
1360 };
1361
1362 static  struct reginfo sensor_Brightness5[]=
1363 {
1364     //  Brightness +3
1365     {0x3301, 0xff},//bit[7]:1, enable SDE
1366     {0x3391, 0x04}, //bit[2] enable
1367     {0x3390, 0x41}, //bit[3] sign of brightness
1368     {0x339a, 0x30},
1369     {0x0000, 0x00}
1370 };
1371 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
1372     sensor_Brightness4, sensor_Brightness5,NULL,
1373 };
1374
1375 #endif
1376
1377 #if CONFIG_SENSOR_Effect
1378 static  struct reginfo sensor_Effect_Normal[] =
1379 {
1380     {0x3391, 0x00},
1381     {0x0000, 0x00}
1382 };
1383
1384 static  struct reginfo sensor_Effect_WandB[] =
1385 {
1386     {0x3391, 0x20},
1387     {0x0000, 0x00}
1388 };
1389
1390 static  struct reginfo sensor_Effect_Sepia[] =
1391 {
1392     {0x3391, 0x18},
1393     {0x3396, 0x40},
1394     {0x3397, 0xa6},
1395     {0x0000, 0x00}
1396 };
1397
1398 static  struct reginfo sensor_Effect_Negative[] =
1399 {
1400     //Negative
1401     {0x3391, 0x40}, //bit[6] negative
1402     {0x0000, 0x00}
1403 };
1404 static  struct reginfo sensor_Effect_Bluish[] =
1405 {
1406     // Bluish
1407     {0x3391, 0x18},
1408     {0x3396, 0xa0},
1409     {0x3397, 0x40},
1410     {0x0000, 0x00}
1411 };
1412
1413 static  struct reginfo sensor_Effect_Green[] =
1414 {
1415     //  Greenish
1416     {0x3391, 0x18},
1417     {0x3396, 0x60},
1418     {0x3397, 0x60},
1419     {0x0000, 0x00}
1420 };
1421 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
1422     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
1423 };
1424 #endif
1425 #if CONFIG_SENSOR_Exposure
1426 static  struct reginfo sensor_Exposure0[]=
1427 {
1428     //-3
1429     {0x3047, 0x05},
1430     {0x3018, 0x40},
1431     {0x3019, 0x30},
1432     {0x301a, 0x71},
1433     {0x0000, 0x00}
1434 };
1435
1436 static  struct reginfo sensor_Exposure1[]=
1437 {
1438     //-2
1439     {0x3047, 0x05},
1440     {0x3018, 0x5a},
1441     {0x3019, 0x4a},
1442     {0x301a, 0xc2},
1443     {0x0000, 0x00}
1444 };
1445
1446 static  struct reginfo sensor_Exposure2[]=
1447 {
1448     //-0.3EV
1449     {0x3047, 0x05},
1450     {0x3018, 0x6a},
1451     {0x3019, 0x5a},
1452     {0x301a, 0xd4},
1453     {0x0000, 0x00}
1454 };
1455
1456 static  struct reginfo sensor_Exposure3[]=
1457 {
1458     //default
1459     {0x3047, 0x05},
1460     {0x3018, 0x78},
1461     {0x3019, 0x68},
1462     {0x301a, 0xd4},
1463     {0x0000, 0x00}
1464 };
1465
1466 static  struct reginfo sensor_Exposure4[]=
1467 {
1468     // 1
1469     {0x3047, 0x05},
1470     {0x3018, 0x88},
1471     {0x3019, 0x78},
1472     {0x301a, 0xd5},
1473     {0x0000, 0x00}
1474 };
1475
1476 static  struct reginfo sensor_Exposure5[]=
1477 {
1478     // 2
1479     {0x3047, 0x05},
1480     {0x3018, 0xa8},
1481     {0x3019, 0x98},
1482     {0x301a, 0xe6},
1483     {0x0000, 0x00}
1484 };
1485
1486 static  struct reginfo sensor_Exposure6[]=
1487 {
1488     // 3
1489     {0x3047, 0x05},
1490     {0x3018, 0xc8},
1491     {0x3019, 0xb8},
1492     {0x301a, 0xf7},
1493     {0x0000, 0x00}
1494 };
1495
1496 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1497     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1498 };
1499 #endif
1500 #if CONFIG_SENSOR_Saturation
1501 static  struct reginfo sensor_Saturation0[]=
1502 {
1503     {0x3301, 0xff},//bit[7]:1, enable SDE
1504     {0x3391, 0x02},
1505     {0x3394, 0x40},
1506     {0x3395, 0x40},
1507     {0x0000, 0x00}
1508 };
1509
1510 static  struct reginfo sensor_Saturation1[]=
1511 {
1512     {0x3301, 0xff},//bit[7]:1, enable SDE
1513     {0x3391, 0x02},
1514     {0x3394, 0x50},
1515     {0x3395, 0x50},
1516     {0x0000, 0x00}
1517 };
1518
1519 static  struct reginfo sensor_Saturation2[]=
1520 {
1521     {0x3301, 0xff},//bit[7]:1, enable SDE
1522     {0x3391, 0x02}, //enable color saturation
1523     {0x3394, 0x70},
1524     {0x3395, 0x70},
1525     {0x0000, 0x00}
1526 };
1527 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1528
1529 #endif
1530 #if CONFIG_SENSOR_Contrast
1531 static  struct reginfo sensor_Contrast0[]=
1532 {
1533     //Contrast -3
1534     {0x3301, 0xff},//bit[7]:1, enable SDE
1535     {0x3391, 0x04},
1536     {0x3390, 0x45},
1537     {0x3398, 0x18},
1538     {0x3399, 0x18},
1539     {0x0000, 0x00}
1540 };
1541
1542 static  struct reginfo sensor_Contrast1[]=
1543 {
1544     //Contrast -2
1545     {0x3301, 0xff},//bit[7]:1, enable SDE
1546     {0x3391, 0x04},
1547     {0x3390, 0x45},
1548     {0x3398, 0x18},
1549     {0x3399, 0x18},
1550     {0x0000, 0x00}
1551 };
1552
1553 static  struct reginfo sensor_Contrast2[]=
1554 {
1555     // Contrast -1
1556     {0x3301, 0xff},//bit[7]:1, enable SDE
1557     {0x3391, 0x04},
1558     {0x3390, 0x45},
1559     {0x3398, 0x1c},
1560     {0x3399, 0x1c},
1561     {0x0000, 0x00}
1562 };
1563
1564 static  struct reginfo sensor_Contrast3[]=
1565 {
1566     //Contrast 0
1567     {0x3301, 0xff},//bit[7]:1, enable SDE
1568     {0x3391, 0x00},
1569     {0x3390, 0x41},
1570     {0x3398, 0x20},
1571     {0x3399, 0x20},
1572     {0x0000, 0x00}
1573 };
1574
1575 static  struct reginfo sensor_Contrast4[]=
1576 {
1577     //Contrast +1
1578     {0x3301, 0xff},//bit[7]:1, enable SDE
1579     {0x3391, 0x04},
1580     {0x3390, 0x45},
1581     {0x3398, 0x24},
1582     {0x3399, 0x24},
1583     {0x0000, 0x00}
1584 };
1585
1586
1587 static  struct reginfo sensor_Contrast5[]=
1588 {
1589     //Contrast +2
1590     {0x3301, 0xff},//bit[7]:1, enable SDE
1591     {0x3391, 0x04},
1592     {0x3390, 0x45},
1593     {0x3398, 0x28},
1594     {0x3399, 0x28},
1595     {0x0000, 0x00}
1596 };
1597
1598 static  struct reginfo sensor_Contrast6[]=
1599 {
1600     //Contrast +3
1601     {0x3301, 0xff},//bit[7]:1, enable SDE
1602     {0x3391, 0x04}, //bit[2] enable contrast/brightness
1603     {0x3390, 0x45}, //bit[2] Yoffset sign
1604     {0x3398, 0x2c},
1605     {0x3399, 0x2c},
1606     {0x0000, 0x00}
1607 };
1608 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1609     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1610 };
1611
1612 #endif
1613 #if CONFIG_SENSOR_Mirror
1614 static  struct reginfo sensor_MirrorOn[]=
1615 {
1616     {0x3069, 0x84},
1617     {0x307c, 0x13},
1618     {0x3087, 0x02},
1619     {0x0000, 0x00}
1620 };
1621
1622 static  struct reginfo sensor_MirrorOff[]=
1623 {
1624     {0x3069, 0x84},
1625     {0x307c, 0x10},
1626     {0x3087, 0x02},
1627     {0x0000, 0x00}
1628 };
1629 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1630 #endif
1631 #if CONFIG_SENSOR_Flip
1632 static  struct reginfo sensor_FlipOn[]=
1633 {
1634     {0x300e, 0x34},
1635     {0x300f, 0xa6},
1636     {0x3010, 0x81},
1637     {0x3082, 0x01},
1638     {0x30f4, 0x01},
1639     {0x3090, 0x3b},
1640     {0x3091, 0xc0},
1641     {0x30ac, 0x42},
1642     {0x0000, 0x00}
1643 };
1644
1645 static  struct reginfo sensor_FlipOff[]=
1646 {
1647     {0x300e, 0x34},
1648     {0x300f, 0xa6},
1649     {0x3010, 0x81},
1650     {0x3082, 0x01},
1651     {0x30f4, 0x01},
1652     {0x3090, 0x33},
1653     {0x3091, 0xc0},
1654     {0x30ac, 0x42},
1655     {0x0000, 0x00}
1656 };
1657 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1658
1659 #endif
1660 #if CONFIG_SENSOR_Scene
1661 static  struct reginfo sensor_SceneAuto[] =
1662 {
1663 #if 0                           /* ddl@rock-chips.com : */
1664     {0x3014, 0x04},
1665     {0x3015, 0x00},
1666     {0x302e, 0x00},
1667     {0x302d, 0x00},
1668     {0x0000, 0x00}
1669 #else
1670     {0x3014, 0x84},
1671     {0x3015, 0x02},
1672     {0x302e, 0x00},
1673     {0x302d, 0x00},
1674     {0x0000, 0x00}
1675 #endif
1676 };
1677
1678 static  struct reginfo sensor_SceneNight[] =
1679 {
1680 #if 1
1681     //30fps ~ 5fps night mode for 60/50Hz light environment, 24Mhz clock input,36Mzh pclk
1682     {0x300e, 0x34},
1683     {0x3011, 0x00},
1684     {0x302c, 0x00},
1685     {0x3071, 0x00},
1686     {0x3070, 0xb9},
1687     {0x301c, 0x02},
1688     {0x3073, 0x00},
1689     {0x3072, 0x9a},
1690     {0x301d, 0x03},
1691     {0x3014, 0x0c},
1692     {0x3015, 0x50},//add 5 dummy frame
1693     {0x302e, 0x00},
1694     {0x302d, 0x00},
1695     {0x0000, 0x00}
1696 #else
1697     //15fps ~ 5fps night mode for 60/50Hz light environment, 24Mhz clock input,18Mhz pclk
1698     {0x300e, 0x34},
1699     {0x3011, 0x01},
1700     {0x302c, 0x00},
1701     {0x3071, 0x00},
1702     {0x3070, 0x5d},
1703     {0x301c, 0x05},
1704     {0x3073, 0x00},
1705     {0x3072, 0x4d},
1706     {0x301d, 0x07},
1707     {0x3014, 0x0c},
1708     {0x3015, 0x50},
1709     {0x302e, 0x00},
1710     {0x302d, 0x00},
1711 #endif
1712 };
1713 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1714
1715 #endif
1716 #if CONFIG_SENSOR_DigitalZoom
1717 static struct reginfo sensor_Zoom0[] =
1718 {
1719     {0x0, 0x0},
1720 };
1721
1722 static struct reginfo sensor_Zoom1[] =
1723 {
1724      {0x0, 0x0},
1725 };
1726
1727 static struct reginfo sensor_Zoom2[] =
1728 {
1729     {0x0, 0x0},
1730 };
1731
1732
1733 static struct reginfo sensor_Zoom3[] =
1734 {
1735     {0x0, 0x0},
1736 };
1737 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1738 #endif
1739 static const struct v4l2_querymenu sensor_menus[] =
1740 {
1741         #if CONFIG_SENSOR_WhiteBalance
1742     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1743     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1744     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1745     #endif
1746
1747         #if CONFIG_SENSOR_Effect
1748     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1749     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1750     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
1751     #endif
1752
1753         #if CONFIG_SENSOR_Scene
1754     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1755     #endif
1756
1757         #if CONFIG_SENSOR_Flash
1758     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1759     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1760     #endif
1761 };
1762
1763 static const struct v4l2_queryctrl sensor_controls[] =
1764 {
1765         #if CONFIG_SENSOR_WhiteBalance
1766     {
1767         .id             = V4L2_CID_DO_WHITE_BALANCE,
1768         .type           = V4L2_CTRL_TYPE_MENU,
1769         .name           = "White Balance Control",
1770         .minimum        = 0,
1771         .maximum        = 4,
1772         .step           = 1,
1773         .default_value = 0,
1774     },
1775     #endif
1776
1777         #if CONFIG_SENSOR_Brightness
1778         {
1779         .id             = V4L2_CID_BRIGHTNESS,
1780         .type           = V4L2_CTRL_TYPE_INTEGER,
1781         .name           = "Brightness Control",
1782         .minimum        = -3,
1783         .maximum        = 2,
1784         .step           = 1,
1785         .default_value = 0,
1786     },
1787     #endif
1788
1789         #if CONFIG_SENSOR_Effect
1790         {
1791         .id             = V4L2_CID_EFFECT,
1792         .type           = V4L2_CTRL_TYPE_MENU,
1793         .name           = "Effect Control",
1794         .minimum        = 0,
1795         .maximum        = 5,
1796         .step           = 1,
1797         .default_value = 0,
1798     },
1799         #endif
1800
1801         #if CONFIG_SENSOR_Exposure
1802         {
1803         .id             = V4L2_CID_EXPOSURE,
1804         .type           = V4L2_CTRL_TYPE_INTEGER,
1805         .name           = "Exposure Control",
1806         .minimum        = 0,
1807         .maximum        = 6,
1808         .step           = 1,
1809         .default_value = 0,
1810     },
1811         #endif
1812
1813         #if CONFIG_SENSOR_Saturation
1814         {
1815         .id             = V4L2_CID_SATURATION,
1816         .type           = V4L2_CTRL_TYPE_INTEGER,
1817         .name           = "Saturation Control",
1818         .minimum        = 0,
1819         .maximum        = 2,
1820         .step           = 1,
1821         .default_value = 0,
1822     },
1823     #endif
1824
1825         #if CONFIG_SENSOR_Contrast
1826         {
1827         .id             = V4L2_CID_CONTRAST,
1828         .type           = V4L2_CTRL_TYPE_INTEGER,
1829         .name           = "Contrast Control",
1830         .minimum        = -3,
1831         .maximum        = 3,
1832         .step           = 1,
1833         .default_value = 0,
1834     },
1835         #endif
1836
1837         #if CONFIG_SENSOR_Mirror
1838         {
1839         .id             = V4L2_CID_HFLIP,
1840         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1841         .name           = "Mirror Control",
1842         .minimum        = 0,
1843         .maximum        = 1,
1844         .step           = 1,
1845         .default_value = 1,
1846     },
1847     #endif
1848
1849         #if CONFIG_SENSOR_Flip
1850         {
1851         .id             = V4L2_CID_VFLIP,
1852         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1853         .name           = "Flip Control",
1854         .minimum        = 0,
1855         .maximum        = 1,
1856         .step           = 1,
1857         .default_value = 1,
1858     },
1859     #endif
1860
1861         #if CONFIG_SENSOR_Scene
1862     {
1863         .id             = V4L2_CID_SCENE,
1864         .type           = V4L2_CTRL_TYPE_MENU,
1865         .name           = "Scene Control",
1866         .minimum        = 0,
1867         .maximum        = 1,
1868         .step           = 1,
1869         .default_value = 0,
1870     },
1871     #endif
1872
1873         #if CONFIG_SENSOR_DigitalZoom
1874     {
1875         .id             = V4L2_CID_ZOOM_RELATIVE,
1876         .type           = V4L2_CTRL_TYPE_INTEGER,
1877         .name           = "DigitalZoom Control",
1878         .minimum        = -1,
1879         .maximum        = 1,
1880         .step           = 1,
1881         .default_value = 0,
1882     }, {
1883         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1884         .type           = V4L2_CTRL_TYPE_INTEGER,
1885         .name           = "DigitalZoom Control",
1886         .minimum        = 0,
1887         .maximum        = 3,
1888         .step           = 1,
1889         .default_value = 0,
1890     },
1891     #endif
1892
1893         #if CONFIG_SENSOR_Focus
1894         {
1895         .id             = V4L2_CID_FOCUS_RELATIVE,
1896         .type           = V4L2_CTRL_TYPE_INTEGER,
1897         .name           = "Focus Control",
1898         .minimum        = -1,
1899         .maximum        = 1,
1900         .step           = 1,
1901         .default_value = 0,
1902     }, {
1903         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1904         .type           = V4L2_CTRL_TYPE_INTEGER,
1905         .name           = "Focus Control",
1906         .minimum        = 0,
1907         .maximum        = 255,
1908         .step           = 1,
1909         .default_value = 125,
1910     },
1911     #endif
1912
1913         #if CONFIG_SENSOR_Flash
1914         {
1915         .id             = V4L2_CID_FLASH,
1916         .type           = V4L2_CTRL_TYPE_MENU,
1917         .name           = "Flash Control",
1918         .minimum        = 0,
1919         .maximum        = 3,
1920         .step           = 1,
1921         .default_value = 0,
1922     },
1923         #endif
1924 };
1925
1926 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1927 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1928 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1929 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1930 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1931 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1932 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1933 static int sensor_resume(struct soc_camera_device *icd);
1934 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1935 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1936 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1937 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1938 static int sensor_deactivate(struct i2c_client *client);
1939
1940 static struct soc_camera_ops sensor_ops =
1941 {
1942     .suspend                     = sensor_suspend,
1943     .resume                       = sensor_resume,
1944     .set_bus_param              = sensor_set_bus_param,
1945     .query_bus_param    = sensor_query_bus_param,
1946     .controls           = sensor_controls,
1947     .menus                         = sensor_menus,
1948     .num_controls               = ARRAY_SIZE(sensor_controls),
1949     .num_menus          = ARRAY_SIZE(sensor_menus),
1950 };
1951
1952 /* only one fixed colorspace per pixelcode */
1953 struct sensor_datafmt {
1954         enum v4l2_mbus_pixelcode code;
1955         enum v4l2_colorspace colorspace;
1956 };
1957
1958 /* Find a data format by a pixel code in an array */
1959 static const struct sensor_datafmt *sensor_find_datafmt(
1960         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1961         int n)
1962 {
1963         int i;
1964         for (i = 0; i < n; i++)
1965                 if (fmt[i].code == code)
1966                         return fmt + i;
1967
1968         return NULL;
1969 }
1970
1971 static const struct sensor_datafmt sensor_colour_fmts[] = {
1972     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1973     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1974 };
1975
1976 typedef struct sensor_info_priv_s
1977 {
1978     int whiteBalance;
1979     int brightness;
1980     int contrast;
1981     int saturation;
1982     int effect;
1983     int scene;
1984     int digitalzoom;
1985     int focus;
1986     int flash;
1987     int exposure;
1988         bool snap2preview;
1989         bool video2preview;
1990     unsigned char mirror;                                        /* HFLIP */
1991     unsigned char flip;                                          /* VFLIP */
1992     unsigned int winseqe_cur_addr;
1993     struct sensor_datafmt fmt;
1994     unsigned int funmodule_state;
1995 } sensor_info_priv_t;
1996
1997 struct sensor
1998 {
1999     struct v4l2_subdev subdev;
2000     struct i2c_client *client;
2001     sensor_info_priv_t info_priv;
2002     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
2003 #if CONFIG_SENSOR_I2C_NOSCHED
2004         atomic_t tasklock_cnt;
2005 #endif
2006         struct rk29camera_platform_data *sensor_io_request;
2007     struct rk29camera_gpio_res *sensor_gpio_res;
2008 };
2009
2010 static struct sensor* to_sensor(const struct i2c_client *client)
2011 {
2012     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
2013 }
2014
2015 static int sensor_task_lock(struct i2c_client *client, int lock)
2016 {
2017 #if CONFIG_SENSOR_I2C_NOSCHED
2018         int cnt = 3;
2019     struct sensor *sensor = to_sensor(client);
2020
2021         if (lock) {
2022                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
2023                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
2024                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
2025                                 msleep(35);
2026                                 cnt--;
2027                         }
2028                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
2029                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
2030                                 goto sensor_task_lock_err;
2031                         }
2032                         preempt_disable();
2033                 }
2034
2035                 atomic_add(1, &sensor->tasklock_cnt);
2036         } else {
2037                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
2038                         atomic_sub(1, &sensor->tasklock_cnt);
2039
2040                         if (atomic_read(&sensor->tasklock_cnt) == 0)
2041                                 preempt_enable();
2042                 }
2043         }
2044         return 0;
2045 sensor_task_lock_err:
2046         return -1; 
2047 #else
2048     return 0;
2049 #endif
2050
2051 }
2052
2053 /* sensor register write */
2054 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
2055 {
2056     int err,cnt;
2057     u8 buf[3];
2058     struct i2c_msg msg[1];
2059
2060     buf[0] = reg >> 8;
2061     buf[1] = reg & 0xFF;
2062     buf[2] = val;
2063
2064     msg->addr = client->addr;
2065     msg->flags = client->flags;
2066     msg->buf = buf;
2067     msg->len = sizeof(buf);
2068     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
2069     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
2070
2071     cnt = 3;
2072     err = -EAGAIN;
2073
2074     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
2075         err = i2c_transfer(client->adapter, msg, 1);
2076
2077         if (err >= 0) {
2078             return 0;
2079         } else {
2080                 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
2081             udelay(10);
2082         }
2083     }
2084
2085     return err;
2086 }
2087
2088 /* sensor register read */
2089 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
2090 {
2091     int err,cnt;
2092     u8 buf[2];
2093     struct i2c_msg msg[2];
2094
2095     buf[0] = reg >> 8;
2096     buf[1] = reg & 0xFF;
2097
2098     msg[0].addr = client->addr;
2099     msg[0].flags = client->flags;
2100     msg[0].buf = buf;
2101     msg[0].len = sizeof(buf);
2102     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
2103     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2104
2105     msg[1].addr = client->addr;
2106     msg[1].flags = client->flags|I2C_M_RD;
2107     msg[1].buf = buf;
2108     msg[1].len = 1;
2109     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
2110     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2111
2112     cnt = 3;
2113     err = -EAGAIN;
2114     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
2115         err = i2c_transfer(client->adapter, msg, 2);
2116
2117         if (err >= 0) {
2118             *val = buf[0];
2119             return 0;
2120         } else {
2121                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
2122             udelay(10);
2123         }
2124     }
2125
2126     return err;
2127 }
2128
2129 /* write a array of registers  */
2130 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
2131 {
2132     int err = 0, cnt;
2133     int i = 0;
2134 #if CONFIG_SENSOR_I2C_RDWRCHK    
2135         char valchk;
2136 #endif
2137
2138         cnt = 0;
2139         if (sensor_task_lock(client, 1) < 0)
2140                 goto sensor_write_array_end;
2141
2142     while (regarray[i].reg != 0)
2143     {
2144         err = sensor_write(client, regarray[i].reg, regarray[i].val);
2145         if (err < 0)
2146         {
2147             if (cnt-- > 0) {
2148                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
2149                                 i = 0;
2150                                 continue;
2151             } else {
2152                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
2153                 err = -EPERM;
2154                                 goto sensor_write_array_end;
2155             }
2156         } else {
2157         #if CONFIG_SENSOR_I2C_RDWRCHK
2158                         sensor_read(client, regarray[i].reg, &valchk);
2159                         if (valchk != regarray[i].val)
2160                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2161                 #endif
2162         }
2163         i++;
2164     }
2165
2166 sensor_write_array_end:
2167         sensor_task_lock(client,0);
2168         return err;
2169 }
2170
2171 #if CONFIG_SENSOR_I2C_RDWRCHK
2172 static int sensor_check_array(struct i2c_client *client, struct reginfo *regarray)
2173 {
2174   int ret;
2175   int i = 0,j=0;  
2176   u8 value;
2177   
2178   SENSOR_DG("%s >>>>>>>>>>>>>>>>>>>>>>\n",__FUNCTION__);
2179   while(regarray[i].reg != 0)
2180   {
2181      ret = sensor_read(client,regarray[i].reg,&value);
2182          if(ret !=0)
2183          {
2184           SENSOR_TR("read value failed\n");
2185
2186          }
2187          if(regarray[i].val != value)
2188          {
2189           SENSOR_DG("%s reg[0x%x] check err,writte :0x%x  read:0x%x\n",__FUNCTION__,regarray[i].reg,regarray[i].val,value);
2190          }
2191          else
2192           j++;
2193          
2194          i++;
2195   }
2196   if(i==j)
2197          SENSOR_DG("%s check success\n",__FUNCTION__);
2198         
2199   return 0;
2200 }
2201 #endif
2202 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2203 {
2204         struct soc_camera_link *icl = to_soc_camera_link(icd);
2205         int ret = 0;
2206
2207     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2208         switch (cmd)
2209         {
2210                 case Sensor_PowerDown:
2211                 {
2212                         if (icl->powerdown) {
2213                                 ret = icl->powerdown(icd->pdev, on);
2214                                 if (ret == RK29_CAM_IO_SUCCESS) {
2215                                         if (on == 0) {
2216                                                 mdelay(2);
2217                                                 if (icl->reset)
2218                                                         icl->reset(icd->pdev);
2219                                         }
2220                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2221                                         ret = -ENODEV;
2222                                         goto sensor_power_end;
2223                                 }
2224                         }
2225                         break;
2226                 }
2227                 case Sensor_Flash:
2228                 {
2229                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2230                 struct sensor *sensor = to_sensor(client);
2231
2232                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2233                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2234                         }
2235             break;
2236                 }
2237                 default:
2238                 {
2239                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2240                         break;
2241                 }
2242         }
2243 sensor_power_end:
2244         return ret;
2245 }
2246
2247 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2248 {
2249     struct i2c_client *client = v4l2_get_subdevdata(sd);
2250     struct soc_camera_device *icd = client->dev.platform_data;
2251     struct sensor *sensor = to_sensor(client);
2252         const struct v4l2_queryctrl *qctrl;
2253     const struct sensor_datafmt *fmt;
2254     char value;
2255     int ret,pid = 0;
2256
2257     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2258
2259         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2260                 ret = -ENODEV;
2261                 goto sensor_INIT_ERR;
2262         }
2263
2264     /* soft reset */
2265         if (sensor_task_lock(client,1)<0)
2266                 goto sensor_INIT_ERR;
2267    /* ret = sensor_write(client, 0x3012, 0x80);
2268     if (ret != 0)
2269     {
2270         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2271         ret = -ENODEV;
2272                 goto sensor_INIT_ERR;
2273     }
2274
2275     mdelay(5); */ //delay 5 microseconds
2276         /* check if it is an sensor sensor */
2277     ret = sensor_read(client, 0x0000, &value);
2278     if (ret != 0) {
2279         SENSOR_TR("read chip id high byte failed\n");
2280         ret = -ENODEV;
2281         goto sensor_INIT_ERR;
2282     }
2283
2284     pid |= (value << 8);
2285
2286     ret = sensor_read(client, 0x0001, &value);
2287     if (ret != 0) {
2288         SENSOR_TR("read chip id low byte failed\n");
2289         ret = -ENODEV;
2290         goto sensor_INIT_ERR;
2291     }
2292
2293     pid |= (value & 0xff);
2294     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2295     if (pid == SENSOR_ID) {
2296         sensor->model = SENSOR_V4L2_IDENT;
2297     } else {
2298         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2299         ret = -ENODEV;
2300         goto sensor_INIT_ERR;
2301     }
2302
2303     ret = sensor_write_array(client, sensor_init_data);
2304     if (ret != 0)
2305     {
2306         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2307         goto sensor_INIT_ERR;
2308     }
2309         sensor_task_lock(client,0);
2310     
2311     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
2312     fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2313     if (!fmt) {
2314         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2315         ret = -EINVAL;
2316         goto sensor_INIT_ERR;
2317     }
2318         sensor->info_priv.fmt = *fmt;
2319
2320     /* sensor sensor information for initialization  */
2321         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2322         if (qctrl)
2323         sensor->info_priv.whiteBalance = qctrl->default_value;
2324         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2325         if (qctrl)
2326         sensor->info_priv.brightness = qctrl->default_value;
2327         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2328         if (qctrl)
2329         sensor->info_priv.effect = qctrl->default_value;
2330         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2331         if (qctrl)
2332         sensor->info_priv.exposure = qctrl->default_value;
2333
2334         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2335         if (qctrl)
2336         sensor->info_priv.saturation = qctrl->default_value;
2337         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2338         if (qctrl)
2339         sensor->info_priv.contrast = qctrl->default_value;
2340         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2341         if (qctrl)
2342         sensor->info_priv.mirror = qctrl->default_value;
2343         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2344         if (qctrl)
2345         sensor->info_priv.flip = qctrl->default_value;
2346         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2347         if (qctrl)
2348         sensor->info_priv.scene = qctrl->default_value;
2349         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2350         if (qctrl)
2351         sensor->info_priv.digitalzoom = qctrl->default_value;
2352
2353     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
2354         #if CONFIG_SENSOR_Focus
2355     sensor_set_focus();
2356     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2357         if (qctrl)
2358         sensor->info_priv.focus = qctrl->default_value;
2359         #endif
2360
2361         #if CONFIG_SENSOR_Flash 
2362         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2363         if (qctrl)
2364         sensor->info_priv.flash = qctrl->default_value;
2365     #endif
2366
2367     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);
2368     sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
2369     return 0;
2370 sensor_INIT_ERR:
2371     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2372         sensor_task_lock(client,0);
2373         sensor_deactivate(client);
2374     return ret;
2375 }
2376
2377 static int sensor_deactivate(struct i2c_client *client)
2378 {
2379         struct soc_camera_device *icd = client->dev.platform_data;
2380
2381     struct sensor *sensor = to_sensor(client);
2382         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2383
2384         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2385     if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2386     sensor_write(client, 0x30b0, 0x00);
2387         sensor_write(client, 0x30b1, 0x00);
2388     }
2389         sensor_ioctrl(icd, Sensor_PowerDown, 1);
2390     msleep(100); 
2391
2392         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2393         icd->user_width = SENSOR_INIT_WIDTH;
2394     icd->user_height = SENSOR_INIT_HEIGHT;
2395     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2396         
2397         return 0;
2398 }
2399
2400 static  struct reginfo sensor_power_down_sequence[]=
2401 {
2402     {0x30ab, 0x00},
2403     {0x30ad, 0x0a},
2404     {0x30ae,0x27},
2405     {0x363b,0x01},
2406     {0x00,0x00}
2407 };
2408 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2409 {
2410     int ret;
2411     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2412
2413     if (pm_msg.event == PM_EVENT_SUSPEND) {
2414         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2415         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2416         if (ret != 0) {
2417             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2418             return ret;
2419         } else {
2420             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2421             if (ret < 0) {
2422                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2423                 return -EINVAL;
2424             }
2425         }
2426     } else {
2427         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2428         return -EINVAL;
2429     }
2430     return 0;
2431 }
2432
2433 static int sensor_resume(struct soc_camera_device *icd)
2434 {
2435         int ret;
2436
2437     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2438     if (ret < 0) {
2439                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2440         return -EINVAL;
2441     }
2442
2443         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2444
2445     return 0;
2446
2447 }
2448
2449 static int sensor_set_bus_param(struct soc_camera_device *icd,
2450                                 unsigned long flags)
2451 {
2452
2453     return 0;
2454 }
2455
2456 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2457 {
2458     struct soc_camera_link *icl = to_soc_camera_link(icd);
2459     unsigned long flags = SENSOR_BUS_PARAM;
2460
2461     return soc_camera_apply_sensor_flags(icl, flags);
2462 }
2463
2464 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2465 {
2466     struct i2c_client *client = v4l2_get_subdevdata(sd);
2467     struct soc_camera_device *icd = client->dev.platform_data;
2468     struct sensor *sensor = to_sensor(client);
2469
2470     mf->width   = icd->user_width;
2471         mf->height      = icd->user_height;
2472         mf->code        = sensor->info_priv.fmt.code;
2473         mf->colorspace  = sensor->info_priv.fmt.colorspace;
2474         mf->field       = V4L2_FIELD_NONE;
2475
2476     return 0;
2477 }
2478 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2479 {
2480     bool ret = false;
2481
2482         if ((mf->width == 1024) && (mf->height == 768)) {
2483                 ret = true;
2484         } else if ((mf->width == 1280) && (mf->height == 1024)) {
2485                 ret = true;
2486         } else if ((mf->width == 1600) && (mf->height == 1200)) {
2487                 ret = true;
2488         } else if ((mf->width == 2048) && (mf->height == 1536)) {
2489                 ret = true;
2490         } else if ((mf->width == 2592) && (mf->height == 1944)) {
2491                 ret = true;
2492         }
2493
2494         if (ret == true)
2495                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2496         return ret;
2497 }
2498
2499 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2500 {
2501     bool ret = false;
2502
2503         if ((mf->width == 1280) && (mf->height == 720)) {
2504                 ret = true;
2505         } else if ((mf->width == 1920) && (mf->height == 1080)) {
2506                 ret = true;
2507         }
2508
2509         if (ret == true)
2510                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2511         return ret;
2512 }
2513 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2514 {
2515     struct i2c_client *client = v4l2_get_subdevdata(sd);
2516     const struct sensor_datafmt *fmt;
2517     struct sensor *sensor = to_sensor(client);
2518         const struct v4l2_queryctrl *qctrl;
2519         struct soc_camera_device *icd = client->dev.platform_data;
2520     struct reginfo *winseqe_set_addr=NULL;
2521     int ret=0, set_w,set_h;
2522
2523         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2524                                    ARRAY_SIZE(sensor_colour_fmts));
2525         if (!fmt) {
2526         ret = -EINVAL;
2527         goto sensor_s_fmt_end;
2528     }
2529
2530         if (sensor->info_priv.fmt.code != mf->code) {
2531                 switch (mf->code)
2532                 {
2533                         case V4L2_MBUS_FMT_YUYV8_2X8:
2534                         {
2535                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2536                                 break;
2537                         }
2538                         case V4L2_MBUS_FMT_UYVY8_2X8:
2539                         {
2540                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2541                                 break;
2542                         }
2543                         default:
2544                                 break;
2545                 }
2546                 if (winseqe_set_addr != NULL) {
2547             sensor_write_array(client, winseqe_set_addr);
2548                         sensor->info_priv.fmt.code = mf->code;
2549             sensor->info_priv.fmt.colorspace= mf->colorspace;            
2550                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2551                 } else {
2552                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2553                 }
2554         }
2555
2556     set_w = mf->width;
2557     set_h = mf->height;
2558
2559         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2560         {
2561                 winseqe_set_addr = sensor_qcif;
2562         set_w = 176;
2563         set_h = 144;
2564         }
2565         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2566     {
2567         winseqe_set_addr = sensor_qvga;
2568         set_w = 320;
2569         set_h = 240;
2570     }
2571     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2572     {
2573         winseqe_set_addr = sensor_cif;
2574         set_w = 352;
2575         set_h = 288;
2576     }
2577     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2578     {
2579         winseqe_set_addr = sensor_vga;
2580         set_w = 640;
2581         set_h = 480;
2582     }
2583     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2584     {
2585         winseqe_set_addr = sensor_svga;
2586         set_w = 800;
2587         set_h = 600;
2588     }
2589     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2590     {
2591         winseqe_set_addr = sensor_sxga;
2592         set_w = 1280;
2593         set_h = 1024;
2594     }
2595     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2596     {
2597         winseqe_set_addr = sensor_uxga;
2598         set_w = 1600;
2599         set_h = 1200;
2600     }
2601     else
2602     {
2603         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2604         set_w = SENSOR_INIT_WIDTH;
2605         set_h = SENSOR_INIT_HEIGHT;
2606                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2607     }
2608
2609     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
2610         #if CONFIG_SENSOR_Flash
2611         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
2612             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2613                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2614                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2615             }           
2616         } else {                                        /* ddl@rock-chips.com : Video */
2617             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2618                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2619                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2620             }
2621         }
2622         #endif
2623         ret |= sensor_write_array(client, winseqe_set_addr);
2624         if (ret != 0) {
2625             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2626             #if CONFIG_SENSOR_Flash
2627             if (sensor_fmt_capturechk(sd,mf) == true) {
2628                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2629                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2630                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2631                 }
2632             }
2633             #endif
2634             goto sensor_s_fmt_end;
2635         }
2636
2637         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
2638
2639                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
2640                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2641                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2642                         if (sensor->info_priv.whiteBalance != 0) {
2643                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2644                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2645                         }
2646                         sensor->info_priv.snap2preview = true;
2647                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
2648                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2649                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2650                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2651                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2652                         sensor->info_priv.video2preview = true;
2653                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2654                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2655                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2656                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2657                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2658             msleep(600);
2659                         sensor->info_priv.video2preview = false;
2660                         sensor->info_priv.snap2preview = false;
2661                 }
2662
2663         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2664     }
2665     else
2666     {
2667         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2668     }
2669
2670         mf->width = set_w;
2671     mf->height = set_h;
2672
2673 sensor_s_fmt_end:
2674     return ret;
2675 }
2676
2677 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2678 {
2679     struct i2c_client *client = v4l2_get_subdevdata(sd);
2680     struct sensor *sensor = to_sensor(client);
2681     const struct sensor_datafmt *fmt;
2682     int ret = 0,set_w,set_h;
2683    
2684         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2685                                    ARRAY_SIZE(sensor_colour_fmts));
2686         if (fmt == NULL) {
2687                 fmt = &sensor->info_priv.fmt;
2688         mf->code = fmt->code;
2689         } 
2690
2691     if (mf->height > SENSOR_MAX_HEIGHT)
2692         mf->height = SENSOR_MAX_HEIGHT;
2693     else if (mf->height < SENSOR_MIN_HEIGHT)
2694         mf->height = SENSOR_MIN_HEIGHT;
2695
2696     if (mf->width > SENSOR_MAX_WIDTH)
2697         mf->width = SENSOR_MAX_WIDTH;
2698     else if (mf->width < SENSOR_MIN_WIDTH)
2699         mf->width = SENSOR_MIN_WIDTH;
2700
2701     set_w = mf->width;
2702     set_h = mf->height;
2703
2704         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2705         {
2706         set_w = 176;
2707         set_h = 144;
2708         }
2709         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2710     {
2711         set_w = 320;
2712         set_h = 240;
2713     }
2714     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2715     {
2716         set_w = 352;
2717         set_h = 288;
2718     }
2719     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2720     {
2721         set_w = 640;
2722         set_h = 480;
2723     }
2724     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2725     {
2726         set_w = 800;
2727         set_h = 600;
2728     }
2729     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2730     {
2731         set_w = 1280;
2732         set_h = 1024;
2733     }
2734     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2735     {
2736         set_w = 1600;
2737         set_h = 1200;
2738     }
2739     else
2740     {
2741         set_w = SENSOR_INIT_WIDTH;
2742         set_h = SENSOR_INIT_HEIGHT;
2743     }
2744
2745         mf->width = set_w;
2746     mf->height = set_h;
2747     mf->colorspace = fmt->colorspace;
2748     
2749     return ret;
2750 }
2751
2752  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2753 {
2754     struct i2c_client *client = v4l2_get_subdevdata(sd);
2755
2756     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2757         return -EINVAL;
2758
2759     if (id->match.addr != client->addr)
2760         return -ENODEV;
2761
2762     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return gt2005  identifier */
2763     id->revision = 0;
2764
2765     return 0;
2766 }
2767 #if CONFIG_SENSOR_Brightness
2768 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2769 {
2770     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2771
2772     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2773     {
2774         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2775         {
2776             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2777             {
2778                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2779                 return -EINVAL;
2780             }
2781             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2782             return 0;
2783         }
2784     }
2785         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2786     return -EINVAL;
2787 }
2788 #endif
2789 #if CONFIG_SENSOR_Effect
2790 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2791 {
2792     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2793
2794     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2795     {
2796         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2797         {
2798             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2799             {
2800                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2801                 return -EINVAL;
2802             }
2803             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2804             return 0;
2805         }
2806     }
2807         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2808     return -EINVAL;
2809 }
2810 #endif
2811 #if CONFIG_SENSOR_Exposure
2812 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2813 {
2814     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2815
2816     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2817     {
2818         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2819         {
2820             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2821             {
2822                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2823                 return -EINVAL;
2824             }
2825             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2826             return 0;
2827         }
2828     }
2829         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2830     return -EINVAL;
2831 }
2832 #endif
2833 #if CONFIG_SENSOR_Saturation
2834 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2835 {
2836     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2837
2838     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2839     {
2840         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2841         {
2842             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2843             {
2844                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2845                 return -EINVAL;
2846             }
2847             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2848             return 0;
2849         }
2850     }
2851     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2852     return -EINVAL;
2853 }
2854 #endif
2855 #if CONFIG_SENSOR_Contrast
2856 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2857 {
2858     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2859
2860     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2861     {
2862         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2863         {
2864             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2865             {
2866                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2867                 return -EINVAL;
2868             }
2869             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2870             return 0;
2871         }
2872     }
2873     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2874     return -EINVAL;
2875 }
2876 #endif
2877 #if CONFIG_SENSOR_Mirror
2878 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2879 {
2880     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2881
2882     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2883     {
2884         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2885         {
2886             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2887             {
2888                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2889                 return -EINVAL;
2890             }
2891             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2892             return 0;
2893         }
2894     }
2895     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2896     return -EINVAL;
2897 }
2898 #endif
2899 #if CONFIG_SENSOR_Flip
2900 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2901 {
2902     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2903
2904     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2905     {
2906         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2907         {
2908             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2909             {
2910                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2911                 return -EINVAL;
2912             }
2913             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2914             return 0;
2915         }
2916     }
2917     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2918     return -EINVAL;
2919 }
2920 #endif
2921 #if CONFIG_SENSOR_Scene
2922 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2923 {
2924     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2925
2926     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2927     {
2928         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2929         {
2930             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2931             {
2932                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2933                 return -EINVAL;
2934             }
2935             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2936             return 0;
2937         }
2938     }
2939     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2940     return -EINVAL;
2941 }
2942 #endif
2943 #if CONFIG_SENSOR_WhiteBalance
2944 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2945 {
2946     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2947
2948     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2949     {
2950         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2951         {
2952             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2953             {
2954                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2955                 return -EINVAL;
2956             }
2957             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2958             return 0;
2959         }
2960     }
2961         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2962     return -EINVAL;
2963 }
2964 #endif
2965 #if CONFIG_SENSOR_DigitalZoom
2966 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2967 {
2968     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2969     struct sensor *sensor = to_sensor(client);
2970         const struct v4l2_queryctrl *qctrl_info;
2971     int digitalzoom_cur, digitalzoom_total;
2972
2973         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2974         if (qctrl_info)
2975                 return -EINVAL;
2976
2977     digitalzoom_cur = sensor->info_priv.digitalzoom;
2978     digitalzoom_total = qctrl_info->maximum;
2979
2980     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
2981     {
2982         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2983         return -EINVAL;
2984     }
2985
2986     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2987     {
2988         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2989         return -EINVAL;
2990     }
2991
2992     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
2993     {
2994         value = digitalzoom_total - digitalzoom_cur;
2995     }
2996
2997     if ((value < 0) && ((digitalzoom_cur + value) < 0))
2998     {
2999         value = 0 - digitalzoom_cur;
3000     }
3001
3002     digitalzoom_cur += value;
3003
3004     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3005     {
3006         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3007         {
3008             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3009             return -EINVAL;
3010         }
3011         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3012         return 0;
3013     }
3014
3015     return -EINVAL;
3016 }
3017 #endif
3018 #if CONFIG_SENSOR_Flash
3019 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3020 {    
3021     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3022         if (value == 3) {       /* ddl@rock-chips.com: torch */
3023             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
3024         } else {
3025             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3026         }
3027         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3028         return 0;
3029     }
3030     
3031         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3032     return -EINVAL;
3033 }
3034 #endif
3035
3036 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3037 {
3038     struct i2c_client *client = v4l2_get_subdevdata(sd);
3039     struct sensor *sensor = to_sensor(client);
3040     const struct v4l2_queryctrl *qctrl;
3041
3042     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3043
3044     if (!qctrl)
3045     {
3046         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3047         return -EINVAL;
3048     }
3049
3050     switch (ctrl->id)
3051     {
3052         case V4L2_CID_BRIGHTNESS:
3053             {
3054                 ctrl->value = sensor->info_priv.brightness;
3055                 break;
3056             }
3057         case V4L2_CID_SATURATION:
3058             {
3059                 ctrl->value = sensor->info_priv.saturation;
3060                 break;
3061             }
3062         case V4L2_CID_CONTRAST:
3063             {
3064                 ctrl->value = sensor->info_priv.contrast;
3065                 break;
3066             }
3067         case V4L2_CID_DO_WHITE_BALANCE:
3068             {
3069                 ctrl->value = sensor->info_priv.whiteBalance;
3070                 break;
3071             }
3072         case V4L2_CID_EXPOSURE:
3073             {
3074                 ctrl->value = sensor->info_priv.exposure;
3075                 break;
3076             }
3077         case V4L2_CID_HFLIP:
3078             {
3079                 ctrl->value = sensor->info_priv.mirror;
3080                 break;
3081             }
3082         case V4L2_CID_VFLIP:
3083             {
3084                 ctrl->value = sensor->info_priv.flip;
3085                 break;
3086             }
3087         default :
3088                 break;
3089     }
3090     return 0;
3091 }
3092
3093
3094
3095 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3096 {
3097     struct i2c_client *client = v4l2_get_subdevdata(sd);
3098     struct sensor *sensor = to_sensor(client);
3099     struct soc_camera_device *icd = client->dev.platform_data;
3100     const struct v4l2_queryctrl *qctrl;
3101
3102
3103     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3104
3105     if (!qctrl)
3106     {
3107         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3108         return -EINVAL;
3109     }
3110
3111     switch (ctrl->id)
3112     {
3113 #if CONFIG_SENSOR_Brightness
3114         case V4L2_CID_BRIGHTNESS:
3115             {
3116                 if (ctrl->value != sensor->info_priv.brightness)
3117                 {
3118                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3119                     {
3120                         return -EINVAL;
3121                     }
3122                     sensor->info_priv.brightness = ctrl->value;
3123                 }
3124                 break;
3125             }
3126 #endif
3127 #if CONFIG_SENSOR_Exposure
3128         case V4L2_CID_EXPOSURE:
3129             {
3130                 if (ctrl->value != sensor->info_priv.exposure)
3131                 {
3132                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3133                     {
3134                         return -EINVAL;
3135                     }
3136                     sensor->info_priv.exposure = ctrl->value;
3137                 }
3138                 break;
3139             }
3140 #endif
3141 #if CONFIG_SENSOR_Saturation
3142         case V4L2_CID_SATURATION:
3143             {
3144                 if (ctrl->value != sensor->info_priv.saturation)
3145                 {
3146                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3147                     {
3148                         return -EINVAL;
3149                     }
3150                     sensor->info_priv.saturation = ctrl->value;
3151                 }
3152                 break;
3153             }
3154 #endif
3155 #if CONFIG_SENSOR_Contrast
3156         case V4L2_CID_CONTRAST:
3157             {
3158                 if (ctrl->value != sensor->info_priv.contrast)
3159                 {
3160                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3161                     {
3162                         return -EINVAL;
3163                     }
3164                     sensor->info_priv.contrast = ctrl->value;
3165                 }
3166                 break;
3167             }
3168 #endif
3169 #if CONFIG_SENSOR_WhiteBalance
3170         case V4L2_CID_DO_WHITE_BALANCE:
3171             {
3172                 if (ctrl->value != sensor->info_priv.whiteBalance)
3173                 {
3174                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3175                     {
3176                         return -EINVAL;
3177                     }
3178                     sensor->info_priv.whiteBalance = ctrl->value;
3179                 }
3180                 break;
3181             }
3182 #endif
3183 #if CONFIG_SENSOR_Mirror
3184         case V4L2_CID_HFLIP:
3185             {
3186                 if (ctrl->value != sensor->info_priv.mirror)
3187                 {
3188                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3189                         return -EINVAL;
3190                     sensor->info_priv.mirror = ctrl->value;
3191                 }
3192                 break;
3193             }
3194 #endif
3195 #if CONFIG_SENSOR_Flip
3196         case V4L2_CID_VFLIP:
3197             {
3198                 if (ctrl->value != sensor->info_priv.flip)
3199                 {
3200                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3201                         return -EINVAL;
3202                     sensor->info_priv.flip = ctrl->value;
3203                 }
3204                 break;
3205             }
3206 #endif
3207         default:
3208             break;
3209     }
3210
3211     return 0;
3212 }
3213 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3214 {
3215     const struct v4l2_queryctrl *qctrl;
3216     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3217     struct sensor *sensor = to_sensor(client);
3218
3219     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3220
3221     if (!qctrl)
3222     {
3223         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3224         return -EINVAL;
3225     }
3226
3227     switch (ext_ctrl->id)
3228     {
3229         case V4L2_CID_SCENE:
3230             {
3231                 ext_ctrl->value = sensor->info_priv.scene;
3232                 break;
3233             }
3234         case V4L2_CID_EFFECT:
3235             {
3236                 ext_ctrl->value = sensor->info_priv.effect;
3237                 break;
3238             }
3239         case V4L2_CID_ZOOM_ABSOLUTE:
3240             {
3241                 ext_ctrl->value = sensor->info_priv.digitalzoom;
3242                 break;
3243             }
3244         case V4L2_CID_ZOOM_RELATIVE:
3245             {
3246                 return -EINVAL;
3247             }
3248         case V4L2_CID_FOCUS_ABSOLUTE:
3249             {
3250                 ext_ctrl->value = sensor->info_priv.focus;
3251                 break;
3252             }
3253         case V4L2_CID_FOCUS_RELATIVE:
3254             {
3255                 return -EINVAL;
3256             }
3257         case V4L2_CID_FLASH:
3258             {
3259                 ext_ctrl->value = sensor->info_priv.flash;
3260                 break;
3261             }
3262         default :
3263             break;
3264     }
3265     return 0;
3266 }
3267 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3268 {
3269     const struct v4l2_queryctrl *qctrl;
3270     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3271     struct sensor *sensor = to_sensor(client);
3272     int val_offset;
3273
3274     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3275
3276     if (!qctrl)
3277     {
3278         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3279         return -EINVAL;
3280     }
3281
3282         val_offset = 0;
3283     switch (ext_ctrl->id)
3284     {
3285 #if CONFIG_SENSOR_Scene
3286         case V4L2_CID_SCENE:
3287             {
3288                 if (ext_ctrl->value != sensor->info_priv.scene)
3289                 {
3290                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3291                         return -EINVAL;
3292                     sensor->info_priv.scene = ext_ctrl->value;
3293                 }
3294                 break;
3295             }
3296 #endif
3297 #if CONFIG_SENSOR_Effect
3298         case V4L2_CID_EFFECT:
3299             {
3300                 if (ext_ctrl->value != sensor->info_priv.effect)
3301                 {
3302                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3303                         return -EINVAL;
3304                     sensor->info_priv.effect= ext_ctrl->value;
3305                 }
3306                 break;
3307             }
3308 #endif
3309 #if CONFIG_SENSOR_DigitalZoom
3310         case V4L2_CID_ZOOM_ABSOLUTE:
3311             {
3312                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3313                     return -EINVAL;
3314
3315                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3316                 {
3317                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3318
3319                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3320                         return -EINVAL;
3321                     sensor->info_priv.digitalzoom += val_offset;
3322
3323                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
3324                 }
3325
3326                 break;
3327             }
3328         case V4L2_CID_ZOOM_RELATIVE:
3329             {
3330                 if (ext_ctrl->value)
3331                 {
3332                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3333                         return -EINVAL;
3334                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3335
3336                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3337                 }
3338                 break;
3339             }
3340 #endif
3341 #if CONFIG_SENSOR_Focus
3342         case V4L2_CID_FOCUS_ABSOLUTE:
3343             {
3344                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3345                     return -EINVAL;
3346
3347                 if (ext_ctrl->value != sensor->info_priv.focus)
3348                 {
3349                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
3350
3351                     sensor->info_priv.focus += val_offset;
3352                 }
3353
3354                 break;
3355             }
3356         case V4L2_CID_FOCUS_RELATIVE:
3357             {
3358                 if (ext_ctrl->value)
3359                 {
3360                     sensor->info_priv.focus += ext_ctrl->value;
3361
3362                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
3363                 }
3364                 break;
3365             }
3366 #endif
3367 #if CONFIG_SENSOR_Flash
3368         case V4L2_CID_FLASH:
3369             {
3370                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3371                     return -EINVAL;
3372                 sensor->info_priv.flash = ext_ctrl->value;
3373
3374                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3375                 break;
3376             }
3377 #endif
3378         default:
3379             break;
3380     }
3381
3382     return 0;
3383 }
3384
3385 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3386 {
3387     struct i2c_client *client = v4l2_get_subdevdata(sd);
3388     struct soc_camera_device *icd = client->dev.platform_data;
3389     int i, error_cnt=0, error_idx=-1;
3390
3391
3392     for (i=0; i<ext_ctrl->count; i++) {
3393         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3394             error_cnt++;
3395             error_idx = i;
3396         }
3397     }
3398
3399     if (error_cnt > 1)
3400         error_idx = ext_ctrl->count;
3401
3402     if (error_idx != -1) {
3403         ext_ctrl->error_idx = error_idx;
3404         return -EINVAL;
3405     } else {
3406         return 0;
3407     }
3408 }
3409
3410 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3411 {
3412     struct i2c_client *client = v4l2_get_subdevdata(sd);
3413     struct soc_camera_device *icd = client->dev.platform_data;
3414     int i, error_cnt=0, error_idx=-1;
3415
3416
3417     for (i=0; i<ext_ctrl->count; i++) {
3418         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3419             error_cnt++;
3420             error_idx = i;
3421         }
3422     }
3423
3424     if (error_cnt > 1)
3425         error_idx = ext_ctrl->count;
3426
3427     if (error_idx != -1) {
3428         ext_ctrl->error_idx = error_idx;
3429         return -EINVAL;
3430     } else {
3431         return 0;
3432     }
3433 }
3434
3435 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3436  * this wasn't our capture interface, so, we wait for the right one */
3437 static int sensor_video_probe(struct soc_camera_device *icd,
3438                                struct i2c_client *client)
3439 {
3440     char value;
3441     int ret,pid = 0;
3442     struct sensor *sensor = to_sensor(client);
3443
3444     /* We must have a parent by now. And it cannot be a wrong one.
3445      * So this entire test is completely redundant. */
3446     if (!icd->dev.parent ||
3447             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3448                 return -ENODEV;
3449
3450         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3451                 ret = -ENODEV;
3452                 goto sensor_video_probe_err;
3453         }
3454
3455     /* soft reset */
3456    /* ret = sensor_write(client, 0x3012, 0x80);
3457     if (ret != 0)
3458     {
3459         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3460         return -ENODEV;
3461     }
3462     mdelay(5);      */    //delay 5 microseconds
3463
3464     /* check if it is an sensor sensor */
3465     ret = sensor_read(client, 0x0000, &value);
3466     if (ret != 0) {
3467         SENSOR_TR("read chip id high byte failed\n");
3468         ret = -ENODEV;
3469         goto sensor_video_probe_err;
3470     }
3471
3472     pid |= (value << 8);
3473
3474     ret = sensor_read(client, 0x0001, &value);
3475     if (ret != 0) {
3476         SENSOR_TR("read chip id low byte failed\n");
3477         ret = -ENODEV;
3478         goto sensor_video_probe_err;
3479     }
3480
3481     pid |= (value & 0xff);
3482     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3483     if (pid == SENSOR_ID) {
3484         sensor->model = SENSOR_V4L2_IDENT;
3485     } else {
3486         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3487         ret = -ENODEV;
3488         goto sensor_video_probe_err;
3489     }
3490
3491     return 0;
3492
3493 sensor_video_probe_err:
3494
3495     return ret;
3496 }
3497 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3498 {
3499         struct i2c_client *client = v4l2_get_subdevdata(sd);
3500     struct soc_camera_device *icd = client->dev.platform_data;
3501     struct sensor *sensor = to_sensor(client);
3502 #if CONFIG_SENSOR_Flash
3503     int i;
3504 #endif
3505     int ret = 0;
3506     
3507         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3508         switch (cmd)
3509         {
3510                 case RK29_CAM_SUBDEV_DEACTIVATE:
3511                 {
3512                         sensor_deactivate(client);
3513                         break;
3514                 }
3515
3516                 case RK29_CAM_SUBDEV_IOREQUEST:
3517                 {
3518                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
3519             if (sensor->sensor_io_request != NULL) { 
3520                 sensor->sensor_gpio_res = NULL;
3521                 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
3522                     if (sensor->sensor_io_request->gpio_res[i].dev_name && 
3523                         (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
3524                         sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
3525                     }
3526                 }
3527                 if (sensor->sensor_gpio_res == NULL) {
3528                     SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
3529                     ret = -EINVAL;
3530                     goto sensor_ioctl_end;
3531                 }
3532             } else {
3533                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3534                 ret = -EINVAL;
3535                 goto sensor_ioctl_end;
3536             }
3537             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
3538                for this project */
3539             #if CONFIG_SENSOR_Flash     
3540                 if (sensor->sensor_gpio_res) {
3541                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3542                     for (i = 0; i < icd->ops->num_controls; i++) {
3543                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3544                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
3545                                 }
3546                     }
3547                     sensor->info_priv.flash = 0xff;
3548                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3549                 }
3550                 }
3551             #endif
3552                         break;
3553                 }
3554                 default:
3555                 {
3556                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3557                         break;
3558                 }
3559         }
3560 sensor_ioctl_end:
3561         return ret;
3562
3563 }
3564 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3565                             enum v4l2_mbus_pixelcode *code)
3566 {
3567         if (index >= ARRAY_SIZE(sensor_colour_fmts))
3568                 return -EINVAL;
3569
3570         *code = sensor_colour_fmts[index].code;
3571         return 0;
3572 }
3573 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3574         .init           = sensor_init,
3575         .g_ctrl         = sensor_g_control,
3576         .s_ctrl         = sensor_s_control,
3577         .g_ext_ctrls          = sensor_g_ext_controls,
3578         .s_ext_ctrls          = sensor_s_ext_controls,
3579         .g_chip_ident   = sensor_g_chip_ident,
3580         .ioctl = sensor_ioctl,
3581 };
3582
3583 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3584         .s_mbus_fmt     = sensor_s_fmt,
3585         .g_mbus_fmt     = sensor_g_fmt,
3586         .try_mbus_fmt   = sensor_try_fmt,
3587         .enum_mbus_fmt  = sensor_enum_fmt,
3588 };
3589
3590 static struct v4l2_subdev_ops sensor_subdev_ops = {
3591         .core   = &sensor_subdev_core_ops,
3592         .video = &sensor_subdev_video_ops,
3593 };
3594
3595 static int sensor_probe(struct i2c_client *client,
3596                          const struct i2c_device_id *did)
3597 {
3598     struct sensor *sensor;
3599     struct soc_camera_device *icd = client->dev.platform_data;
3600     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3601     struct soc_camera_link *icl;
3602     int ret;
3603
3604     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3605     if (!icd) {
3606         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3607         return -EINVAL;
3608     }
3609
3610     icl = to_soc_camera_link(icd);
3611     if (!icl) {
3612         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3613         return -EINVAL;
3614     }
3615
3616     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3617         dev_warn(&adapter->dev,
3618                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3619         return -EIO;
3620     }
3621
3622     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3623     if (!sensor)
3624         return -ENOMEM;
3625
3626     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3627
3628     /* Second stage probe - when a capture adapter is there */
3629     icd->ops            = &sensor_ops;
3630
3631     sensor->info_priv.fmt = sensor_colour_fmts[0];
3632     
3633         #if CONFIG_SENSOR_I2C_NOSCHED
3634         atomic_set(&sensor->tasklock_cnt,0);
3635         #endif
3636
3637     ret = sensor_video_probe(icd, client);
3638     if (ret < 0) {
3639         icd->ops = NULL;
3640         i2c_set_clientdata(client, NULL);
3641         kfree(sensor);
3642                 sensor = NULL;
3643     }
3644     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3645     return ret;
3646 }
3647
3648 static int sensor_remove(struct i2c_client *client)
3649 {
3650     struct sensor *sensor = to_sensor(client);
3651     struct soc_camera_device *icd = client->dev.platform_data;
3652
3653     icd->ops = NULL;
3654     i2c_set_clientdata(client, NULL);
3655     client->driver = NULL;
3656     kfree(sensor);
3657
3658     return 0;
3659 }
3660
3661 static const struct i2c_device_id sensor_id[] = {
3662         {SENSOR_NAME_STRING(), 0 },
3663         { }
3664 };
3665 MODULE_DEVICE_TABLE(i2c, sensor_id);
3666
3667 static struct i2c_driver sensor_i2c_driver = {
3668         .driver = {
3669                 .name = SENSOR_NAME_STRING(),
3670         },
3671         .probe          = sensor_probe,
3672         .remove         = sensor_remove,
3673         .id_table       = sensor_id,
3674 };
3675
3676 static int __init sensor_mod_init(void)
3677 {
3678     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3679     return i2c_add_driver(&sensor_i2c_driver);
3680 }
3681
3682 static void __exit sensor_mod_exit(void)
3683 {
3684     i2c_del_driver(&sensor_i2c_driver);
3685 }
3686
3687 device_initcall_sync(sensor_mod_init);
3688 module_exit(sensor_mod_exit);
3689
3690 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3691 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3692 MODULE_LICENSE("GPL");
3693