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