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