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