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