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