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