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