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