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