camera(v0.4.1): cif:v0.4.1 generic_sensor:v0.1.0 rk_camera:v0.1.0
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / gt2005_old.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 #ifdef 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     flash_off_timer.icd = icd;
2390         flash_off_timer.timer.function = flash_off_func;
2391     #endif
2392
2393     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);
2394     sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
2395     return 0;
2396 sensor_INIT_ERR:
2397     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2398         sensor_task_lock(client,0);
2399         sensor_deactivate(client);
2400     return ret;
2401 }
2402
2403 static int sensor_deactivate(struct i2c_client *client)
2404 {
2405         struct soc_camera_device *icd = client->dev.platform_data;
2406
2407     struct sensor *sensor = to_sensor(client);
2408         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2409
2410         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2411     if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2412     sensor_write(client, 0x30b0, 0x00);
2413         sensor_write(client, 0x30b1, 0x00);
2414     }
2415         sensor_ioctrl(icd, Sensor_PowerDown, 1);
2416     msleep(100); 
2417
2418         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2419         icd->user_width = SENSOR_INIT_WIDTH;
2420     icd->user_height = SENSOR_INIT_HEIGHT;
2421     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2422         
2423         return 0;
2424 }
2425
2426 static  struct reginfo sensor_power_down_sequence[]=
2427 {
2428     {0x30ab, 0x00},
2429     {0x30ad, 0x0a},
2430     {0x30ae,0x27},
2431     {0x363b,0x01},
2432     {0x00,0x00}
2433 };
2434 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2435 {
2436     int ret;
2437     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2438
2439     if (pm_msg.event == PM_EVENT_SUSPEND) {
2440         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2441         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2442         if (ret != 0) {
2443             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2444             return ret;
2445         } else {
2446             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2447             if (ret < 0) {
2448                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2449                 return -EINVAL;
2450             }
2451         }
2452     } else {
2453         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2454         return -EINVAL;
2455     }
2456     return 0;
2457 }
2458
2459 static int sensor_resume(struct soc_camera_device *icd)
2460 {
2461         int ret;
2462
2463     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2464     if (ret < 0) {
2465                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2466         return -EINVAL;
2467     }
2468
2469         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2470
2471     return 0;
2472
2473 }
2474
2475 static int sensor_set_bus_param(struct soc_camera_device *icd,
2476                                 unsigned long flags)
2477 {
2478
2479     return 0;
2480 }
2481
2482 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2483 {
2484     struct soc_camera_link *icl = to_soc_camera_link(icd);
2485     unsigned long flags = SENSOR_BUS_PARAM;
2486
2487     return soc_camera_apply_sensor_flags(icl, flags);
2488 }
2489
2490 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2491 {
2492     struct i2c_client *client = v4l2_get_subdevdata(sd);
2493     struct soc_camera_device *icd = client->dev.platform_data;
2494     struct sensor *sensor = to_sensor(client);
2495
2496     mf->width   = icd->user_width;
2497         mf->height      = icd->user_height;
2498         mf->code        = sensor->info_priv.fmt.code;
2499         mf->colorspace  = sensor->info_priv.fmt.colorspace;
2500         mf->field       = V4L2_FIELD_NONE;
2501
2502     return 0;
2503 }
2504 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2505 {
2506     bool ret = false;
2507
2508         if ((mf->width == 1024) && (mf->height == 768)) {
2509                 ret = true;
2510         } else if ((mf->width == 1280) && (mf->height == 1024)) {
2511                 ret = true;
2512         } else if ((mf->width == 1600) && (mf->height == 1200)) {
2513                 ret = true;
2514         } else if ((mf->width == 2048) && (mf->height == 1536)) {
2515                 ret = true;
2516         } else if ((mf->width == 2592) && (mf->height == 1944)) {
2517                 ret = true;
2518         }
2519
2520         if (ret == true)
2521                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2522         return ret;
2523 }
2524
2525 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2526 {
2527     bool ret = false;
2528
2529         if ((mf->width == 1280) && (mf->height == 720)) {
2530                 ret = true;
2531         } else if ((mf->width == 1920) && (mf->height == 1080)) {
2532                 ret = true;
2533         }
2534
2535         if (ret == true)
2536                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2537         return ret;
2538 }
2539 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2540 {
2541     struct i2c_client *client = v4l2_get_subdevdata(sd);
2542     const struct sensor_datafmt *fmt;
2543     struct sensor *sensor = to_sensor(client);
2544         const struct v4l2_queryctrl *qctrl;
2545         struct soc_camera_device *icd = client->dev.platform_data;
2546     struct reginfo *winseqe_set_addr=NULL;
2547     int ret=0, set_w,set_h;
2548
2549         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2550                                    ARRAY_SIZE(sensor_colour_fmts));
2551         if (!fmt) {
2552         ret = -EINVAL;
2553         goto sensor_s_fmt_end;
2554     }
2555
2556         if (sensor->info_priv.fmt.code != mf->code) {
2557                 switch (mf->code)
2558                 {
2559                         case V4L2_MBUS_FMT_YUYV8_2X8:
2560                         {
2561                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2562                                 break;
2563                         }
2564                         case V4L2_MBUS_FMT_UYVY8_2X8:
2565                         {
2566                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2567                                 break;
2568                         }
2569                         default:
2570                                 break;
2571                 }
2572                 if (winseqe_set_addr != NULL) {
2573             sensor_write_array(client, winseqe_set_addr);
2574                         sensor->info_priv.fmt.code = mf->code;
2575             sensor->info_priv.fmt.colorspace= mf->colorspace;            
2576                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2577                 } else {
2578                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2579                 }
2580         }
2581
2582     set_w = mf->width;
2583     set_h = mf->height;
2584
2585         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2586         {
2587                 winseqe_set_addr = sensor_qcif;
2588         set_w = 176;
2589         set_h = 144;
2590         }
2591         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2592     {
2593         winseqe_set_addr = sensor_qvga;
2594         set_w = 320;
2595         set_h = 240;
2596     }
2597     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2598     {
2599         winseqe_set_addr = sensor_cif;
2600         set_w = 352;
2601         set_h = 288;
2602     }
2603     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2604     {
2605         winseqe_set_addr = sensor_vga;
2606         set_w = 640;
2607         set_h = 480;
2608     }
2609     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2610     {
2611         winseqe_set_addr = sensor_svga;
2612         set_w = 800;
2613         set_h = 600;
2614     }
2615     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2616     {
2617         winseqe_set_addr = sensor_sxga;
2618         set_w = 1280;
2619         set_h = 1024;
2620     }
2621     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2622     {
2623         winseqe_set_addr = sensor_uxga;
2624         set_w = 1600;
2625         set_h = 1200;
2626     }
2627     else
2628     {
2629         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2630         set_w = SENSOR_INIT_WIDTH;
2631         set_h = SENSOR_INIT_HEIGHT;
2632                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2633     }
2634
2635     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
2636         #if CONFIG_SENSOR_Flash
2637         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
2638             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2639                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2640                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2641             }           
2642         } else {                                        /* ddl@rock-chips.com : Video */
2643             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2644                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2645                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2646             }
2647         }
2648         #endif
2649         ret |= sensor_write_array(client, winseqe_set_addr);
2650         if (ret != 0) {
2651             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2652             #if CONFIG_SENSOR_Flash
2653             if (sensor_fmt_capturechk(sd,mf) == true) {
2654                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2655                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2656                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2657                 }
2658             }
2659             #endif
2660             goto sensor_s_fmt_end;
2661         }
2662
2663         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
2664
2665                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
2666                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2667                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2668                         if (sensor->info_priv.whiteBalance != 0) {
2669                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2670                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2671                         }
2672                         sensor->info_priv.snap2preview = true;
2673                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
2674                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2675                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2676                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2677                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2678                         sensor->info_priv.video2preview = true;
2679                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2680                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2681                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2682                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2683                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2684             msleep(600);
2685                         sensor->info_priv.video2preview = false;
2686                         sensor->info_priv.snap2preview = false;
2687                 }
2688
2689         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2690     }
2691     else
2692     {
2693         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2694     }
2695
2696         mf->width = set_w;
2697     mf->height = set_h;
2698
2699 sensor_s_fmt_end:
2700     return ret;
2701 }
2702
2703 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2704 {
2705     struct i2c_client *client = v4l2_get_subdevdata(sd);
2706     struct sensor *sensor = to_sensor(client);
2707     const struct sensor_datafmt *fmt;
2708     int ret = 0,set_w,set_h;
2709    
2710         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2711                                    ARRAY_SIZE(sensor_colour_fmts));
2712         if (fmt == NULL) {
2713                 fmt = &sensor->info_priv.fmt;
2714         mf->code = fmt->code;
2715         } 
2716
2717     if (mf->height > SENSOR_MAX_HEIGHT)
2718         mf->height = SENSOR_MAX_HEIGHT;
2719     else if (mf->height < SENSOR_MIN_HEIGHT)
2720         mf->height = SENSOR_MIN_HEIGHT;
2721
2722     if (mf->width > SENSOR_MAX_WIDTH)
2723         mf->width = SENSOR_MAX_WIDTH;
2724     else if (mf->width < SENSOR_MIN_WIDTH)
2725         mf->width = SENSOR_MIN_WIDTH;
2726
2727     set_w = mf->width;
2728     set_h = mf->height;
2729
2730         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2731         {
2732         set_w = 176;
2733         set_h = 144;
2734         }
2735         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2736     {
2737         set_w = 320;
2738         set_h = 240;
2739     }
2740     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2741     {
2742         set_w = 352;
2743         set_h = 288;
2744     }
2745     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2746     {
2747         set_w = 640;
2748         set_h = 480;
2749     }
2750     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2751     {
2752         set_w = 800;
2753         set_h = 600;
2754     }
2755     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2756     {
2757         set_w = 1280;
2758         set_h = 1024;
2759     }
2760     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2761     {
2762         set_w = 1600;
2763         set_h = 1200;
2764     }
2765     else
2766     {
2767         set_w = SENSOR_INIT_WIDTH;
2768         set_h = SENSOR_INIT_HEIGHT;
2769     }
2770
2771         mf->width = set_w;
2772     mf->height = set_h;
2773     mf->colorspace = fmt->colorspace;
2774     
2775     return ret;
2776 }
2777
2778  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2779 {
2780     struct i2c_client *client = v4l2_get_subdevdata(sd);
2781
2782     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2783         return -EINVAL;
2784
2785     if (id->match.addr != client->addr)
2786         return -ENODEV;
2787
2788     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return gt2005  identifier */
2789     id->revision = 0;
2790
2791     return 0;
2792 }
2793 #if CONFIG_SENSOR_Brightness
2794 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2795 {
2796     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2797
2798     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2799     {
2800         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2801         {
2802             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2803             {
2804                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2805                 return -EINVAL;
2806             }
2807             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2808             return 0;
2809         }
2810     }
2811         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2812     return -EINVAL;
2813 }
2814 #endif
2815 #if CONFIG_SENSOR_Effect
2816 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2817 {
2818     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2819
2820     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2821     {
2822         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2823         {
2824             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2825             {
2826                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2827                 return -EINVAL;
2828             }
2829             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2830             return 0;
2831         }
2832     }
2833         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2834     return -EINVAL;
2835 }
2836 #endif
2837 #if CONFIG_SENSOR_Exposure
2838 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2839 {
2840     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2841
2842     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2843     {
2844         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2845         {
2846             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2847             {
2848                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2849                 return -EINVAL;
2850             }
2851             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2852             return 0;
2853         }
2854     }
2855         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2856     return -EINVAL;
2857 }
2858 #endif
2859 #if CONFIG_SENSOR_Saturation
2860 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2861 {
2862     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2863
2864     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2865     {
2866         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2867         {
2868             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2869             {
2870                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2871                 return -EINVAL;
2872             }
2873             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2874             return 0;
2875         }
2876     }
2877     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2878     return -EINVAL;
2879 }
2880 #endif
2881 #if CONFIG_SENSOR_Contrast
2882 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2883 {
2884     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2885
2886     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2887     {
2888         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2889         {
2890             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2891             {
2892                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2893                 return -EINVAL;
2894             }
2895             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2896             return 0;
2897         }
2898     }
2899     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2900     return -EINVAL;
2901 }
2902 #endif
2903 #if CONFIG_SENSOR_Mirror
2904 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2905 {
2906     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2907
2908     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2909     {
2910         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2911         {
2912             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2913             {
2914                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2915                 return -EINVAL;
2916             }
2917             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2918             return 0;
2919         }
2920     }
2921     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2922     return -EINVAL;
2923 }
2924 #endif
2925 #if CONFIG_SENSOR_Flip
2926 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2927 {
2928     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2929
2930     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2931     {
2932         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2933         {
2934             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2935             {
2936                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2937                 return -EINVAL;
2938             }
2939             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2940             return 0;
2941         }
2942     }
2943     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2944     return -EINVAL;
2945 }
2946 #endif
2947 #if CONFIG_SENSOR_Scene
2948 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2949 {
2950     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2951
2952     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2953     {
2954         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2955         {
2956             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2957             {
2958                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2959                 return -EINVAL;
2960             }
2961             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2962             return 0;
2963         }
2964     }
2965     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2966     return -EINVAL;
2967 }
2968 #endif
2969 #if CONFIG_SENSOR_WhiteBalance
2970 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2971 {
2972     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2973
2974     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2975     {
2976         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2977         {
2978             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2979             {
2980                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2981                 return -EINVAL;
2982             }
2983             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2984             return 0;
2985         }
2986     }
2987         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2988     return -EINVAL;
2989 }
2990 #endif
2991 #if CONFIG_SENSOR_DigitalZoom
2992 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2993 {
2994     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2995     struct sensor *sensor = to_sensor(client);
2996         const struct v4l2_queryctrl *qctrl_info;
2997     int digitalzoom_cur, digitalzoom_total;
2998
2999         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3000         if (qctrl_info)
3001                 return -EINVAL;
3002
3003     digitalzoom_cur = sensor->info_priv.digitalzoom;
3004     digitalzoom_total = qctrl_info->maximum;
3005
3006     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
3007     {
3008         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3009         return -EINVAL;
3010     }
3011
3012     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3013     {
3014         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3015         return -EINVAL;
3016     }
3017
3018     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
3019     {
3020         value = digitalzoom_total - digitalzoom_cur;
3021     }
3022
3023     if ((value < 0) && ((digitalzoom_cur + value) < 0))
3024     {
3025         value = 0 - digitalzoom_cur;
3026     }
3027
3028     digitalzoom_cur += value;
3029
3030     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3031     {
3032         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3033         {
3034             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3035             return -EINVAL;
3036         }
3037         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3038         return 0;
3039     }
3040
3041     return -EINVAL;
3042 }
3043 #endif
3044 #if CONFIG_SENSOR_Flash
3045 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3046 {    
3047     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3048         if (value == 3) {       /* ddl@rock-chips.com: torch */
3049             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
3050         } else {
3051             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3052         }
3053         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3054         return 0;
3055     }
3056     
3057         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3058     return -EINVAL;
3059 }
3060 #endif
3061
3062 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3063 {
3064     struct i2c_client *client = v4l2_get_subdevdata(sd);
3065     struct sensor *sensor = to_sensor(client);
3066     const struct v4l2_queryctrl *qctrl;
3067
3068     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3069
3070     if (!qctrl)
3071     {
3072         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3073         return -EINVAL;
3074     }
3075
3076     switch (ctrl->id)
3077     {
3078         case V4L2_CID_BRIGHTNESS:
3079             {
3080                 ctrl->value = sensor->info_priv.brightness;
3081                 break;
3082             }
3083         case V4L2_CID_SATURATION:
3084             {
3085                 ctrl->value = sensor->info_priv.saturation;
3086                 break;
3087             }
3088         case V4L2_CID_CONTRAST:
3089             {
3090                 ctrl->value = sensor->info_priv.contrast;
3091                 break;
3092             }
3093         case V4L2_CID_DO_WHITE_BALANCE:
3094             {
3095                 ctrl->value = sensor->info_priv.whiteBalance;
3096                 break;
3097             }
3098         case V4L2_CID_EXPOSURE:
3099             {
3100                 ctrl->value = sensor->info_priv.exposure;
3101                 break;
3102             }
3103         case V4L2_CID_HFLIP:
3104             {
3105                 ctrl->value = sensor->info_priv.mirror;
3106                 break;
3107             }
3108         case V4L2_CID_VFLIP:
3109             {
3110                 ctrl->value = sensor->info_priv.flip;
3111                 break;
3112             }
3113         default :
3114                 break;
3115     }
3116     return 0;
3117 }
3118
3119
3120
3121 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3122 {
3123     struct i2c_client *client = v4l2_get_subdevdata(sd);
3124     struct sensor *sensor = to_sensor(client);
3125     struct soc_camera_device *icd = client->dev.platform_data;
3126     const struct v4l2_queryctrl *qctrl;
3127
3128
3129     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3130
3131     if (!qctrl)
3132     {
3133         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3134         return -EINVAL;
3135     }
3136
3137     switch (ctrl->id)
3138     {
3139 #if CONFIG_SENSOR_Brightness
3140         case V4L2_CID_BRIGHTNESS:
3141             {
3142                 if (ctrl->value != sensor->info_priv.brightness)
3143                 {
3144                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3145                     {
3146                         return -EINVAL;
3147                     }
3148                     sensor->info_priv.brightness = ctrl->value;
3149                 }
3150                 break;
3151             }
3152 #endif
3153 #if CONFIG_SENSOR_Exposure
3154         case V4L2_CID_EXPOSURE:
3155             {
3156                 if (ctrl->value != sensor->info_priv.exposure)
3157                 {
3158                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3159                     {
3160                         return -EINVAL;
3161                     }
3162                     sensor->info_priv.exposure = ctrl->value;
3163                 }
3164                 break;
3165             }
3166 #endif
3167 #if CONFIG_SENSOR_Saturation
3168         case V4L2_CID_SATURATION:
3169             {
3170                 if (ctrl->value != sensor->info_priv.saturation)
3171                 {
3172                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3173                     {
3174                         return -EINVAL;
3175                     }
3176                     sensor->info_priv.saturation = ctrl->value;
3177                 }
3178                 break;
3179             }
3180 #endif
3181 #if CONFIG_SENSOR_Contrast
3182         case V4L2_CID_CONTRAST:
3183             {
3184                 if (ctrl->value != sensor->info_priv.contrast)
3185                 {
3186                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3187                     {
3188                         return -EINVAL;
3189                     }
3190                     sensor->info_priv.contrast = ctrl->value;
3191                 }
3192                 break;
3193             }
3194 #endif
3195 #if CONFIG_SENSOR_WhiteBalance
3196         case V4L2_CID_DO_WHITE_BALANCE:
3197             {
3198                 if (ctrl->value != sensor->info_priv.whiteBalance)
3199                 {
3200                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3201                     {
3202                         return -EINVAL;
3203                     }
3204                     sensor->info_priv.whiteBalance = ctrl->value;
3205                 }
3206                 break;
3207             }
3208 #endif
3209 #if CONFIG_SENSOR_Mirror
3210         case V4L2_CID_HFLIP:
3211             {
3212                 if (ctrl->value != sensor->info_priv.mirror)
3213                 {
3214                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3215                         return -EINVAL;
3216                     sensor->info_priv.mirror = ctrl->value;
3217                 }
3218                 break;
3219             }
3220 #endif
3221 #if CONFIG_SENSOR_Flip
3222         case V4L2_CID_VFLIP:
3223             {
3224                 if (ctrl->value != sensor->info_priv.flip)
3225                 {
3226                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3227                         return -EINVAL;
3228                     sensor->info_priv.flip = ctrl->value;
3229                 }
3230                 break;
3231             }
3232 #endif
3233         default:
3234             break;
3235     }
3236
3237     return 0;
3238 }
3239 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3240 {
3241     const struct v4l2_queryctrl *qctrl;
3242     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3243     struct sensor *sensor = to_sensor(client);
3244
3245     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3246
3247     if (!qctrl)
3248     {
3249         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3250         return -EINVAL;
3251     }
3252
3253     switch (ext_ctrl->id)
3254     {
3255         case V4L2_CID_SCENE:
3256             {
3257                 ext_ctrl->value = sensor->info_priv.scene;
3258                 break;
3259             }
3260         case V4L2_CID_EFFECT:
3261             {
3262                 ext_ctrl->value = sensor->info_priv.effect;
3263                 break;
3264             }
3265         case V4L2_CID_ZOOM_ABSOLUTE:
3266             {
3267                 ext_ctrl->value = sensor->info_priv.digitalzoom;
3268                 break;
3269             }
3270         case V4L2_CID_ZOOM_RELATIVE:
3271             {
3272                 return -EINVAL;
3273             }
3274         case V4L2_CID_FOCUS_ABSOLUTE:
3275             {
3276                 ext_ctrl->value = sensor->info_priv.focus;
3277                 break;
3278             }
3279         case V4L2_CID_FOCUS_RELATIVE:
3280             {
3281                 return -EINVAL;
3282             }
3283         case V4L2_CID_FLASH:
3284             {
3285                 ext_ctrl->value = sensor->info_priv.flash;
3286                 break;
3287             }
3288         default :
3289             break;
3290     }
3291     return 0;
3292 }
3293 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3294 {
3295     const struct v4l2_queryctrl *qctrl;
3296     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3297     struct sensor *sensor = to_sensor(client);
3298     int val_offset;
3299
3300     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3301
3302     if (!qctrl)
3303     {
3304         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3305         return -EINVAL;
3306     }
3307
3308         val_offset = 0;
3309     switch (ext_ctrl->id)
3310     {
3311 #if CONFIG_SENSOR_Scene
3312         case V4L2_CID_SCENE:
3313             {
3314                 if (ext_ctrl->value != sensor->info_priv.scene)
3315                 {
3316                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3317                         return -EINVAL;
3318                     sensor->info_priv.scene = ext_ctrl->value;
3319                 }
3320                 break;
3321             }
3322 #endif
3323 #if CONFIG_SENSOR_Effect
3324         case V4L2_CID_EFFECT:
3325             {
3326                 if (ext_ctrl->value != sensor->info_priv.effect)
3327                 {
3328                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3329                         return -EINVAL;
3330                     sensor->info_priv.effect= ext_ctrl->value;
3331                 }
3332                 break;
3333             }
3334 #endif
3335 #if CONFIG_SENSOR_DigitalZoom
3336         case V4L2_CID_ZOOM_ABSOLUTE:
3337             {
3338                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3339                     return -EINVAL;
3340
3341                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3342                 {
3343                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3344
3345                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3346                         return -EINVAL;
3347                     sensor->info_priv.digitalzoom += val_offset;
3348
3349                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
3350                 }
3351
3352                 break;
3353             }
3354         case V4L2_CID_ZOOM_RELATIVE:
3355             {
3356                 if (ext_ctrl->value)
3357                 {
3358                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3359                         return -EINVAL;
3360                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3361
3362                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3363                 }
3364                 break;
3365             }
3366 #endif
3367 #if CONFIG_SENSOR_Focus
3368         case V4L2_CID_FOCUS_ABSOLUTE:
3369             {
3370                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3371                     return -EINVAL;
3372
3373                 if (ext_ctrl->value != sensor->info_priv.focus)
3374                 {
3375                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
3376
3377                     sensor->info_priv.focus += val_offset;
3378                 }
3379
3380                 break;
3381             }
3382         case V4L2_CID_FOCUS_RELATIVE:
3383             {
3384                 if (ext_ctrl->value)
3385                 {
3386                     sensor->info_priv.focus += ext_ctrl->value;
3387
3388                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
3389                 }
3390                 break;
3391             }
3392 #endif
3393 #if CONFIG_SENSOR_Flash
3394         case V4L2_CID_FLASH:
3395             {
3396                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3397                     return -EINVAL;
3398                 sensor->info_priv.flash = ext_ctrl->value;
3399
3400                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3401                 break;
3402             }
3403 #endif
3404         default:
3405             break;
3406     }
3407
3408     return 0;
3409 }
3410
3411 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3412 {
3413     struct i2c_client *client = v4l2_get_subdevdata(sd);
3414     struct soc_camera_device *icd = client->dev.platform_data;
3415     int i, error_cnt=0, error_idx=-1;
3416
3417
3418     for (i=0; i<ext_ctrl->count; i++) {
3419         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3420             error_cnt++;
3421             error_idx = i;
3422         }
3423     }
3424
3425     if (error_cnt > 1)
3426         error_idx = ext_ctrl->count;
3427
3428     if (error_idx != -1) {
3429         ext_ctrl->error_idx = error_idx;
3430         return -EINVAL;
3431     } else {
3432         return 0;
3433     }
3434 }
3435
3436 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3437 {
3438     struct i2c_client *client = v4l2_get_subdevdata(sd);
3439     struct soc_camera_device *icd = client->dev.platform_data;
3440     int i, error_cnt=0, error_idx=-1;
3441
3442
3443     for (i=0; i<ext_ctrl->count; i++) {
3444         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3445             error_cnt++;
3446             error_idx = i;
3447         }
3448     }
3449
3450     if (error_cnt > 1)
3451         error_idx = ext_ctrl->count;
3452
3453     if (error_idx != -1) {
3454         ext_ctrl->error_idx = error_idx;
3455         return -EINVAL;
3456     } else {
3457         return 0;
3458     }
3459 }
3460
3461 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3462  * this wasn't our capture interface, so, we wait for the right one */
3463 static int sensor_video_probe(struct soc_camera_device *icd,
3464                                struct i2c_client *client)
3465 {
3466     char value;
3467     int ret,pid = 0;
3468     struct sensor *sensor = to_sensor(client);
3469
3470     /* We must have a parent by now. And it cannot be a wrong one.
3471      * So this entire test is completely redundant. */
3472     if (!icd->dev.parent ||
3473             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3474                 return -ENODEV;
3475
3476         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3477                 ret = -ENODEV;
3478                 goto sensor_video_probe_err;
3479         }
3480
3481     /* soft reset */
3482    /* ret = sensor_write(client, 0x3012, 0x80);
3483     if (ret != 0)
3484     {
3485         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3486         return -ENODEV;
3487     }
3488     mdelay(5);      */    //delay 5 microseconds
3489
3490     /* check if it is an sensor sensor */
3491     ret = sensor_read(client, 0x0000, &value);
3492     if (ret != 0) {
3493         SENSOR_TR("read chip id high byte failed\n");
3494         ret = -ENODEV;
3495         goto sensor_video_probe_err;
3496     }
3497
3498     pid |= (value << 8);
3499
3500     ret = sensor_read(client, 0x0001, &value);
3501     if (ret != 0) {
3502         SENSOR_TR("read chip id low byte failed\n");
3503         ret = -ENODEV;
3504         goto sensor_video_probe_err;
3505     }
3506
3507     pid |= (value & 0xff);
3508     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3509     if (pid == SENSOR_ID) {
3510         sensor->model = SENSOR_V4L2_IDENT;
3511     } else {
3512         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3513         ret = -ENODEV;
3514         goto sensor_video_probe_err;
3515     }
3516
3517     return 0;
3518
3519 sensor_video_probe_err:
3520
3521     return ret;
3522 }
3523 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3524 {
3525         struct i2c_client *client = v4l2_get_subdevdata(sd);
3526     struct soc_camera_device *icd = client->dev.platform_data;
3527     struct sensor *sensor = to_sensor(client);
3528 #if CONFIG_SENSOR_Flash
3529     int i;
3530 #endif
3531     int ret = 0;
3532     
3533         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3534         switch (cmd)
3535         {
3536                 case RK29_CAM_SUBDEV_DEACTIVATE:
3537                 {
3538                         sensor_deactivate(client);
3539                         break;
3540                 }
3541
3542                 case RK29_CAM_SUBDEV_IOREQUEST:
3543                 {
3544                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
3545             if (sensor->sensor_io_request != NULL) { 
3546                 sensor->sensor_gpio_res = NULL;
3547                 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
3548                     if (sensor->sensor_io_request->gpio_res[i].dev_name && 
3549                         (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
3550                         sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
3551                     }
3552                 }
3553                 if (sensor->sensor_gpio_res == NULL) {
3554                     SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
3555                     ret = -EINVAL;
3556                     goto sensor_ioctl_end;
3557                 }
3558             } else {
3559                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3560                 ret = -EINVAL;
3561                 goto sensor_ioctl_end;
3562             }
3563             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
3564                for this project */
3565             #if CONFIG_SENSOR_Flash     
3566                 if (sensor->sensor_gpio_res) {
3567                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3568                     for (i = 0; i < icd->ops->num_controls; i++) {
3569                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3570                                         //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));  
3571                               sensor_controls[i].id=0xffff;                             
3572                                 }
3573                     }
3574                     sensor->info_priv.flash = 0xff;
3575                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3576                 }else{ //two cameras are the same,need to deal diffrently ,zyc
3577                     for (i = 0; i < icd->ops->num_controls; i++) {
3578                            if(0xffff == icd->ops->controls[i].id){
3579                               sensor_controls[i].id=V4L2_CID_FLASH;
3580                            }               
3581                     }
3582                 }
3583                 }
3584             #endif
3585                         break;
3586                 }
3587                 default:
3588                 {
3589                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3590                         break;
3591                 }
3592         }
3593 sensor_ioctl_end:
3594         return ret;
3595
3596 }
3597 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3598                             enum v4l2_mbus_pixelcode *code)
3599 {
3600         if (index >= ARRAY_SIZE(sensor_colour_fmts))
3601                 return -EINVAL;
3602
3603         *code = sensor_colour_fmts[index].code;
3604         return 0;
3605 }
3606 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3607         .init           = sensor_init,
3608         .g_ctrl         = sensor_g_control,
3609         .s_ctrl         = sensor_s_control,
3610         .g_ext_ctrls          = sensor_g_ext_controls,
3611         .s_ext_ctrls          = sensor_s_ext_controls,
3612         .g_chip_ident   = sensor_g_chip_ident,
3613         .ioctl = sensor_ioctl,
3614 };
3615
3616 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3617         .s_mbus_fmt     = sensor_s_fmt,
3618         .g_mbus_fmt     = sensor_g_fmt,
3619         .try_mbus_fmt   = sensor_try_fmt,
3620         .enum_mbus_fmt  = sensor_enum_fmt,
3621 };
3622
3623 static struct v4l2_subdev_ops sensor_subdev_ops = {
3624         .core   = &sensor_subdev_core_ops,
3625         .video = &sensor_subdev_video_ops,
3626 };
3627
3628 static int sensor_probe(struct i2c_client *client,
3629                          const struct i2c_device_id *did)
3630 {
3631     struct sensor *sensor;
3632     struct soc_camera_device *icd = client->dev.platform_data;
3633     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3634     struct soc_camera_link *icl;
3635     int ret;
3636
3637     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3638     if (!icd) {
3639         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3640         return -EINVAL;
3641     }
3642
3643     icl = to_soc_camera_link(icd);
3644     if (!icl) {
3645         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3646         return -EINVAL;
3647     }
3648
3649     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3650         dev_warn(&adapter->dev,
3651                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3652         return -EIO;
3653     }
3654
3655     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3656     if (!sensor)
3657         return -ENOMEM;
3658
3659     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3660
3661     /* Second stage probe - when a capture adapter is there */
3662     icd->ops            = &sensor_ops;
3663
3664     sensor->info_priv.fmt = sensor_colour_fmts[0];
3665     
3666         #if CONFIG_SENSOR_I2C_NOSCHED
3667         atomic_set(&sensor->tasklock_cnt,0);
3668         #endif
3669
3670     ret = sensor_video_probe(icd, client);
3671     if (ret < 0) {
3672         icd->ops = NULL;
3673         i2c_set_clientdata(client, NULL);
3674         kfree(sensor);
3675                 sensor = NULL;
3676     }
3677         hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
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