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