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