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