03d19f88929f40de48bccb94fbcde3a71b144484
[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_MBUS_FMT_YUYV8_2X8
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, 0x17},       //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, 0x20},// {0x7a, 0x24}, 
185         {0x7b, 0x16},// {0x7b, 0x04}, 
186         {0x7c, 0x23},// {0x7c, 0x07}, 
187         {0x7d, 0x3c},// {0x7d, 0x12}, 
188         {0x7e, 0x5c},// {0x7e, 0x2f}, 
189         {0x7f, 0x69},// {0x7f, 0x3f}, 
190         {0x80, 0x75},// {0x80, 0x4d}, 
191         {0x81, 0x7e},// {0x81, 0x5a}, 
192         {0x82, 0x88},// {0x82, 0x69}, 
193         {0x83, 0x8f},// {0x83, 0x74}, 
194         {0x84, 0x96},// {0x84, 0x7f}, 
195         {0x85, 0xa3},// {0x85, 0x91}, 
196         {0x86, 0xaf},// {0x86, 0x9e}, 
197         {0x87, 0xc4},// {0x87, 0xbb}, 
198         {0x88, 0xd7},// {0x88, 0xd2}, 
199         {0x89, 0xe8},// {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, 0x04},//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 /* only one fixed colorspace per pixelcode */
1107 struct sensor_datafmt {
1108         enum v4l2_mbus_pixelcode code;
1109         enum v4l2_colorspace colorspace;
1110 };
1111
1112 /* Find a data format by a pixel code in an array */
1113 static const struct sensor_datafmt *sensor_find_datafmt(
1114         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1115         int n)
1116 {
1117         int i;
1118         for (i = 0; i < n; i++)
1119                 if (fmt[i].code == code)
1120                         return fmt + i;
1121
1122         return NULL;
1123 }
1124
1125 static const struct sensor_datafmt sensor_colour_fmts[] = {
1126     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1127     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1128 };
1129
1130 typedef struct sensor_info_priv_s
1131 {
1132     int whiteBalance;
1133     int brightness;
1134     int contrast;
1135     int saturation;
1136     int effect;
1137     int scene;
1138     int digitalzoom;
1139     int focus;
1140     int flash;
1141     int exposure;
1142         bool snap2preview;
1143         bool video2preview;
1144     unsigned char mirror;                                        /* HFLIP */
1145     unsigned char flip;                                          /* VFLIP */
1146     unsigned int winseqe_cur_addr;
1147         struct sensor_datafmt fmt;
1148
1149 } sensor_info_priv_t;
1150
1151 struct sensor
1152 {
1153     struct v4l2_subdev subdev;
1154     struct i2c_client *client;
1155     sensor_info_priv_t info_priv;
1156     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1157 #if CONFIG_SENSOR_I2C_NOSCHED
1158         atomic_t tasklock_cnt;
1159 #endif
1160         struct rk29camera_platform_data *sensor_io_request;
1161     struct rk29camera_gpio_res *sensor_gpio_res;
1162 };
1163
1164 static struct sensor* to_sensor(const struct i2c_client *client)
1165 {
1166     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1167 }
1168
1169 static int sensor_task_lock(struct i2c_client *client, int lock)
1170 {
1171 #if CONFIG_SENSOR_I2C_NOSCHED
1172         int cnt = 3;
1173     struct sensor *sensor = to_sensor(client);
1174
1175         if (lock) {
1176                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1177                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1178                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1179                                 msleep(35);
1180                                 cnt--;
1181                         }
1182                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1183                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1184                                 goto sensor_task_lock_err;
1185                         }
1186                         preempt_disable();
1187                 }
1188
1189                 atomic_add(1, &sensor->tasklock_cnt);
1190         } else {
1191                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1192                         atomic_sub(1, &sensor->tasklock_cnt);
1193
1194                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1195                                 preempt_enable();
1196                 }
1197         }
1198         return 0;
1199 sensor_task_lock_err:
1200         return -1;  
1201 #else
1202     return 0;
1203 #endif
1204
1205 }
1206
1207 /* sensor register write */
1208 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1209 {
1210     int err,cnt;
1211     u8 buf[2];
1212     struct i2c_msg msg[1];
1213
1214     buf[0] = reg & 0xFF;
1215     buf[1] = val;
1216
1217     msg->addr = client->addr;
1218     msg->flags = client->flags;
1219     msg->buf = buf;
1220     msg->len = sizeof(buf);
1221     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
1222     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1223
1224     cnt = 3;
1225     err = -EAGAIN;
1226
1227     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1228         err = i2c_transfer(client->adapter, msg, 1);
1229                                 udelay(50);
1230         if (err >= 0) {
1231             return 0;
1232         } else {
1233                 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1234                 udelay(10);
1235         }
1236     }
1237
1238     return err;
1239 }
1240
1241 /* sensor register read */
1242 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1243 {
1244     int err,cnt;
1245     u8 buf[1];
1246     struct i2c_msg msg[2];
1247    
1248     buf[0] = reg;
1249
1250     msg[0].addr = client->addr;
1251     msg[0].flags = client->flags;
1252     msg[0].buf = buf;
1253     msg[0].len = sizeof(buf);
1254     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1255     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1256
1257     msg[1].addr = client->addr;
1258     msg[1].flags = client->flags|I2C_M_RD;
1259     msg[1].buf = buf;
1260     msg[1].len = 1;
1261     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1262     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1263
1264     cnt = 1;
1265     err = -EAGAIN;
1266     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1267         err = i2c_transfer(client->adapter, msg, 2);
1268
1269         if (err >= 0) {
1270             *val = buf[0];
1271             return 0;
1272         } else {
1273                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1274             udelay(10);
1275         }
1276     }
1277
1278     return err;
1279 }
1280
1281 /* write a array of registers  */
1282 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1283 {
1284     int err = 0, cnt;
1285     int i = 0;
1286 #if CONFIG_SENSOR_I2C_RDWRCHK    
1287         char valchk;
1288 #endif
1289
1290         cnt = 0;
1291         if (sensor_task_lock(client, 1) < 0)
1292                 goto sensor_write_array_end;
1293
1294     while (regarray[i].reg != 0)
1295     {
1296         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1297         if (err < 0)
1298         {
1299             if (cnt-- > 0) {
1300                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1301                                 i = 0;
1302                                 continue;
1303             } else {
1304                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1305                 err = -EPERM;
1306                                 goto sensor_write_array_end;
1307             }
1308         } else {
1309         #if CONFIG_SENSOR_I2C_RDWRCHK
1310                         sensor_read(client, regarray[i].reg, &valchk);
1311                         if (valchk != regarray[i].val)
1312                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1313                 #endif
1314         }
1315         i++;
1316     }
1317
1318 sensor_write_array_end:
1319         sensor_task_lock(client,0);
1320         return err;
1321 }
1322 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1323 {
1324     int cnt;
1325     int i = 0;
1326         char valchk;
1327
1328         cnt = 0;
1329         valchk = 0;
1330     while (regarray[i].reg != 0)
1331     {
1332                 sensor_read(client, regarray[i].reg, &valchk);
1333                 if (valchk != regarray[i].val)
1334                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1335
1336         i++;
1337     }
1338     return 0;
1339 }
1340 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1341 {
1342         struct soc_camera_link *icl = to_soc_camera_link(icd);
1343         int ret = 0;
1344
1345     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1346         switch (cmd)
1347         {
1348                 case Sensor_PowerDown:
1349                 {
1350                         if (icl->powerdown) {
1351                                 if (on == 0)
1352                                         mdelay(1);
1353                                 ret = icl->powerdown(icd->pdev, on);
1354                                 if (ret == RK29_CAM_IO_SUCCESS) {
1355                                         if (on == 0) {
1356                                                 mdelay(20);
1357                                                 if (icl->reset)
1358                                                         icl->reset(icd->pdev);
1359                                         }
1360                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1361                                         ret = -ENODEV;
1362                                         goto sensor_power_end;
1363                                 }
1364                         }
1365                         break;
1366                 }
1367                 case Sensor_Flash:
1368                 {
1369                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1370                 struct sensor *sensor = to_sensor(client);
1371
1372                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1373                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1374                         }
1375             break;
1376                 }
1377                 default:
1378                 {
1379                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1380                         break;
1381                 }
1382         }
1383 sensor_power_end:
1384         return ret;
1385 }
1386 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1387 {
1388     struct i2c_client *client = v4l2_get_subdevdata(sd);
1389     struct soc_camera_device *icd = client->dev.platform_data;
1390     struct sensor *sensor = to_sensor(client);
1391         const struct v4l2_queryctrl *qctrl;
1392     const struct sensor_datafmt *fmt;
1393     int ret;
1394
1395     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1396
1397         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1398                 ret = -ENODEV;
1399                 goto sensor_INIT_ERR;
1400         }
1401
1402     /* soft reset */
1403         if (sensor_task_lock(client,1)<0)
1404                 goto sensor_INIT_ERR;
1405     ret = sensor_write(client, 0x12, 0x80);
1406     if (ret != 0)
1407     {
1408         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1409         ret = -ENODEV;
1410                 goto sensor_INIT_ERR;
1411     }
1412
1413     mdelay(5);  //delay 5 microseconds
1414
1415     ret = sensor_write_array(client, sensor_init_data);
1416     if (ret != 0)
1417     {
1418         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1419         goto sensor_INIT_ERR;
1420     }
1421         sensor_task_lock(client,0);
1422     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
1423     fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1424     if (!fmt) {
1425         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1426         ret = -EINVAL;
1427         goto sensor_INIT_ERR;
1428     }
1429         sensor->info_priv.fmt = *fmt;
1430
1431     /* sensor sensor information for initialization  */
1432         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1433         if (qctrl)
1434         sensor->info_priv.whiteBalance = qctrl->default_value;
1435         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1436         if (qctrl)
1437         sensor->info_priv.brightness = qctrl->default_value;
1438         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1439         if (qctrl)
1440         sensor->info_priv.effect = qctrl->default_value;
1441         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1442         if (qctrl)
1443         sensor->info_priv.exposure = qctrl->default_value;
1444
1445         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1446         if (qctrl)
1447         sensor->info_priv.saturation = qctrl->default_value;
1448         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1449         if (qctrl)
1450         sensor->info_priv.contrast = qctrl->default_value;
1451         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1452         if (qctrl)
1453         sensor->info_priv.mirror = qctrl->default_value;
1454         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1455         if (qctrl)
1456         sensor->info_priv.flip = qctrl->default_value;
1457         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1458         if (qctrl)
1459         sensor->info_priv.scene = qctrl->default_value;
1460         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1461         if (qctrl)
1462         sensor->info_priv.digitalzoom = qctrl->default_value;
1463
1464     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
1465         #if CONFIG_SENSOR_Focus
1466     sensor_set_focus();
1467     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1468         if (qctrl)
1469         sensor->info_priv.focus = qctrl->default_value;
1470         #endif
1471
1472         #if CONFIG_SENSOR_Flash 
1473         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1474         if (qctrl)
1475         sensor->info_priv.flash = qctrl->default_value;
1476     #endif
1477
1478     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);
1479
1480     return 0;
1481 sensor_INIT_ERR:
1482         sensor_task_lock(client,0);
1483         sensor_deactivate(client);
1484     return ret;
1485 }
1486
1487 static int sensor_deactivate(struct i2c_client *client)
1488 {
1489         struct soc_camera_device *icd = client->dev.platform_data;
1490         //u8 reg_val;
1491
1492         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1493
1494         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1495         sensor_ioctrl(icd, Sensor_PowerDown, 1);
1496
1497         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1498         icd->user_width = SENSOR_INIT_WIDTH;
1499     icd->user_height = SENSOR_INIT_HEIGHT;
1500         msleep(100);
1501         return 0;
1502 }
1503 static  struct reginfo sensor_power_down_sequence[]=
1504 {
1505     {0x00,0x00}
1506 };
1507 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1508 {
1509     int ret;
1510     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1511
1512     if (pm_msg.event == PM_EVENT_SUSPEND) {
1513         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1514         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1515         if (ret != 0) {
1516             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1517             return ret;
1518         } else {
1519             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1520             if (ret < 0) {
1521                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1522                 return -EINVAL;
1523             }
1524         }
1525     } else {
1526         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1527         return -EINVAL;
1528     }
1529     return 0;
1530 }
1531
1532 static int sensor_resume(struct soc_camera_device *icd)
1533 {
1534         int ret;
1535
1536     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1537     if (ret < 0) {
1538                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1539         return -EINVAL;
1540     }
1541
1542         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1543
1544     return 0;
1545
1546 }
1547
1548 static int sensor_set_bus_param(struct soc_camera_device *icd,
1549                                 unsigned long flags)
1550 {
1551
1552     return 0;
1553 }
1554
1555 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1556 {
1557     struct soc_camera_link *icl = to_soc_camera_link(icd);
1558     unsigned long flags = SENSOR_BUS_PARAM;
1559
1560     return soc_camera_apply_sensor_flags(icl, flags);
1561 }
1562
1563 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1564 {
1565     struct i2c_client *client = v4l2_get_subdevdata(sd);
1566     struct soc_camera_device *icd = client->dev.platform_data;
1567     struct sensor *sensor = to_sensor(client);
1568
1569     mf->width   = icd->user_width;
1570         mf->height      = icd->user_height;
1571         mf->code        = sensor->info_priv.fmt.code;
1572         mf->colorspace  = sensor->info_priv.fmt.colorspace;
1573         mf->field       = V4L2_FIELD_NONE;
1574
1575     return 0;
1576 }
1577 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1578 {
1579     bool ret = false;
1580
1581         if ((mf->width == 1024) && (mf->height == 768)) {
1582                 ret = true;
1583         } else if ((mf->width == 1280) && (mf->height == 1024)) {
1584                 ret = true;
1585         } else if ((mf->width == 1600) && (mf->height == 1200)) {
1586                 ret = true;
1587         } else if ((mf->width == 2048) && (mf->height == 1536)) {
1588                 ret = true;
1589         } else if ((mf->width == 2592) && (mf->height == 1944)) {
1590                 ret = true;
1591         }
1592
1593         if (ret == true)
1594                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1595         return ret;
1596 }
1597
1598 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1599 {
1600     bool ret = false;
1601
1602         if ((mf->width == 1280) && (mf->height == 720)) {
1603                 ret = true;
1604         } else if ((mf->width == 1920) && (mf->height == 1080)) {
1605                 ret = true;
1606         }
1607
1608         if (ret == true)
1609                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1610         return ret;
1611 }
1612 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1613 {
1614     struct i2c_client *client = v4l2_get_subdevdata(sd);
1615     struct sensor *sensor = to_sensor(client);
1616     const struct sensor_datafmt *fmt;
1617         const struct v4l2_queryctrl *qctrl;
1618         struct soc_camera_device *icd = client->dev.platform_data;
1619     struct reginfo *winseqe_set_addr=NULL;
1620         char readval;
1621     int ret=0, set_w,set_h;
1622     
1623         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1624                                    ARRAY_SIZE(sensor_colour_fmts));
1625         if (!fmt) {
1626         ret = -EINVAL;
1627         goto sensor_s_fmt_end;
1628     }
1629
1630         if (sensor->info_priv.fmt.code != mf->code) {
1631                 switch (mf->code)
1632                 {
1633                         case V4L2_MBUS_FMT_YUYV8_2X8:
1634                         {
1635                                 //winseqe_set_addr = sensor_ClrFmt_YUYV;
1636                                 sensor_read(client, 0x3a, &readval);
1637                                 sensor_write(client,0x3a, readval&0xf7);
1638                                 sensor_read(client,0x3d,&readval);
1639                                 sensor_write(client,0x3d,readval&0xfe);
1640                                 break;
1641                         }
1642                         case V4L2_MBUS_FMT_UYVY8_2X8:
1643                         {
1644                                 //winseqe_set_addr = sensor_ClrFmt_UYVY;
1645                                 sensor_read(client, 0x3a, &readval);
1646                                 sensor_write(client,0x3a, readval|0x08);
1647                                 sensor_read(client,0x3d,&readval);
1648                                 sensor_write(client,0x3d,readval&0xfe);
1649                                 break;
1650                         }
1651                         default:
1652                                 break;
1653                 }
1654                 if (winseqe_set_addr != NULL) {
1655             sensor_write_array(client, winseqe_set_addr);
1656                         sensor->info_priv.fmt.code = mf->code;
1657             sensor->info_priv.fmt.colorspace= mf->colorspace;            
1658                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1659                 } else {
1660                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1661                 }
1662         }
1663
1664     set_w = mf->width;
1665     set_h = mf->height;
1666
1667         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1668         {
1669                 winseqe_set_addr = sensor_qcif;
1670         set_w = 176;
1671         set_h = 144;
1672         }
1673         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1674     {
1675         winseqe_set_addr = sensor_qvga;
1676         set_w = 320;
1677         set_h = 240;
1678     }
1679     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1680     {
1681         winseqe_set_addr = sensor_cif;
1682         set_w = 352;
1683         set_h = 288;
1684     }
1685     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1686     {
1687         winseqe_set_addr = sensor_vga;
1688         set_w = 640;
1689         set_h = 480;
1690     }
1691     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1692     {
1693         winseqe_set_addr = sensor_svga;
1694         set_w = 800;
1695         set_h = 600;
1696     }
1697     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1698     {
1699                 winseqe_set_addr = sensor_vga; //sensor_sxga;
1700         set_w = 1280;
1701         set_h = 1024;
1702     }
1703     else
1704     {
1705         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
1706         set_w = SENSOR_INIT_WIDTH;
1707         set_h = SENSOR_INIT_HEIGHT;             
1708                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1709     }
1710
1711     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
1712         #if CONFIG_SENSOR_Flash
1713         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
1714             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1715                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1716                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1717             }           
1718         } else {                                        /* ddl@rock-chips.com : Video */
1719             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1720                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1721                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1722             }
1723         }
1724         #endif
1725         ret |= sensor_write_array(client, winseqe_set_addr);
1726         if (ret != 0) {
1727             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1728             #if CONFIG_SENSOR_Flash
1729             if (sensor_fmt_capturechk(sd,mf) == true) {
1730                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1731                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1732                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1733                 }
1734             }
1735             #endif
1736             goto sensor_s_fmt_end;
1737         }
1738
1739         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
1740
1741                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
1742         #if CONFIG_SENSOR_Effect
1743                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1744                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1745         #endif
1746         #if CONFIG_SENSOR_WhiteBalance
1747                         if (sensor->info_priv.whiteBalance != 0) {
1748                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1749                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1750                         }
1751         #endif
1752                         sensor->info_priv.snap2preview = true;
1753                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
1754                 #if CONFIG_SENSOR_Effect
1755                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1756                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1757         #endif
1758         #if CONFIG_SENSOR_WhiteBalance
1759                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1760                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1761         #endif
1762                         sensor->info_priv.video2preview = true;
1763                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1764                 #if CONFIG_SENSOR_Effect
1765                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1766                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1767         #endif
1768         #if CONFIG_SENSOR_WhiteBalance
1769                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1770                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1771         #endif
1772                         sensor->info_priv.video2preview = false;
1773                         sensor->info_priv.snap2preview = false;
1774                 }
1775
1776         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1777     }
1778     else
1779     {
1780         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1781     }
1782
1783         mf->width = set_w;
1784     mf->height = set_h;
1785
1786 sensor_s_fmt_end:
1787     return ret;
1788 }
1789
1790 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1791 {
1792     struct i2c_client *client = v4l2_get_subdevdata(sd);
1793     struct sensor *sensor = to_sensor(client);
1794     const struct sensor_datafmt *fmt;
1795     int ret = 0;
1796    
1797         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1798                                    ARRAY_SIZE(sensor_colour_fmts));
1799         if (fmt == NULL) {
1800                 fmt = &sensor->info_priv.fmt;
1801         mf->code = fmt->code;
1802         } 
1803
1804     if (mf->height > SENSOR_MAX_HEIGHT)
1805         mf->height = SENSOR_MAX_HEIGHT;
1806     else if (mf->height < SENSOR_MIN_HEIGHT)
1807         mf->height = SENSOR_MIN_HEIGHT;
1808
1809     if (mf->width > SENSOR_MAX_WIDTH)
1810         mf->width = SENSOR_MAX_WIDTH;
1811     else if (mf->width < SENSOR_MIN_WIDTH)
1812         mf->width = SENSOR_MIN_WIDTH;
1813
1814     mf->colorspace = fmt->colorspace;
1815     
1816     return ret;
1817 }
1818
1819  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1820 {
1821     struct i2c_client *client = v4l2_get_subdevdata(sd);
1822
1823     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1824         return -EINVAL;
1825
1826     if (id->match.addr != client->addr)
1827         return -ENODEV;
1828
1829     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV9650  identifier */
1830     id->revision = 0;
1831
1832     return 0;
1833 }
1834 #if CONFIG_SENSOR_Brightness
1835 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1836 {
1837     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1838
1839     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1840     {
1841         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1842         {
1843             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1844             {
1845                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1846                 return -EINVAL;
1847             }
1848             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1849             return 0;
1850         }
1851     }
1852         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1853     return -EINVAL;
1854 }
1855 #endif
1856 #if CONFIG_SENSOR_Effect
1857 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1858 {
1859     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1860
1861     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1862     {
1863         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1864         {
1865             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1866             {
1867                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1868                 return -EINVAL;
1869             }
1870             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1871             return 0;
1872         }
1873     }
1874         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1875     return -EINVAL;
1876 }
1877 #endif
1878 #if CONFIG_SENSOR_Exposure
1879 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1880 {
1881     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1882
1883     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1884     {
1885         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1886         {
1887             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1888             {
1889                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1890                 return -EINVAL;
1891             }
1892             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1893             return 0;
1894         }
1895     }
1896         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1897     return -EINVAL;
1898 }
1899 #endif
1900 #if CONFIG_SENSOR_Saturation
1901 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1902 {
1903     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1904
1905     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1906     {
1907         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1908         {
1909             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1910             {
1911                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1912                 return -EINVAL;
1913             }
1914             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1915             return 0;
1916         }
1917     }
1918     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1919     return -EINVAL;
1920 }
1921 #endif
1922 #if CONFIG_SENSOR_Contrast
1923 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1924 {
1925     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1926
1927     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1928     {
1929         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1930         {
1931             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1932             {
1933                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1934                 return -EINVAL;
1935             }
1936             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1937             return 0;
1938         }
1939     }
1940     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1941     return -EINVAL;
1942 }
1943 #endif
1944 #if CONFIG_SENSOR_Mirror
1945 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1946 {
1947     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1948
1949     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1950     {
1951         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1952         {
1953             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1954             {
1955                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1956                 return -EINVAL;
1957             }
1958             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1959             return 0;
1960         }
1961     }
1962     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1963     return -EINVAL;
1964 }
1965 #endif
1966 #if CONFIG_SENSOR_Flip
1967 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1968 {
1969     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1970
1971     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1972     {
1973         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1974         {
1975             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1976             {
1977                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1978                 return -EINVAL;
1979             }
1980             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1981             return 0;
1982         }
1983     }
1984     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1985     return -EINVAL;
1986 }
1987 #endif
1988 #if CONFIG_SENSOR_Scene
1989 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1990 {
1991     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1992
1993     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1994     {
1995         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1996         {
1997             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1998             {
1999                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2000                 return -EINVAL;
2001             }
2002             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2003             return 0;
2004         }
2005     }
2006     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2007     return -EINVAL;
2008 }
2009 #endif
2010 #if CONFIG_SENSOR_WhiteBalance
2011 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2012 {
2013     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2014
2015     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2016     {
2017         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2018         {
2019             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2020             {
2021                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2022                 return -EINVAL;
2023             }
2024             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2025             return 0;
2026         }
2027     }
2028         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2029     return -EINVAL;
2030 }
2031 #endif
2032 #if CONFIG_SENSOR_DigitalZoom
2033 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2034 {
2035     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2036     struct sensor *sensor = to_sensor(client);
2037         const struct v4l2_queryctrl *qctrl_info;
2038     int digitalzoom_cur, digitalzoom_total;
2039
2040         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2041         if (qctrl_info)
2042                 return -EINVAL;
2043
2044     digitalzoom_cur = sensor->info_priv.digitalzoom;
2045     digitalzoom_total = qctrl_info->maximum;
2046
2047     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2048     {
2049         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2050         return -EINVAL;
2051     }
2052
2053     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2054     {
2055         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2056         return -EINVAL;
2057     }
2058
2059     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2060     {
2061         *value = digitalzoom_total - digitalzoom_cur;
2062     }
2063
2064     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2065     {
2066         *value = 0 - digitalzoom_cur;
2067     }
2068
2069     digitalzoom_cur += *value;
2070
2071     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2072     {
2073         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2074         {
2075             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2076             return -EINVAL;
2077         }
2078         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2079         return 0;
2080     }
2081
2082     return -EINVAL;
2083 }
2084 #endif
2085 #if CONFIG_SENSOR_Flash
2086 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2087 {    
2088     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2089         if (value == 3) {       /* ddl@rock-chips.com: torch */
2090             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2091         } else {
2092             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2093         }
2094         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2095         return 0;
2096     }
2097     
2098         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2099     return -EINVAL;
2100 }
2101 #endif
2102
2103 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2104 {
2105     struct i2c_client *client = v4l2_get_subdevdata(sd);
2106     struct sensor *sensor = to_sensor(client);
2107     const struct v4l2_queryctrl *qctrl;
2108
2109     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2110
2111     if (!qctrl)
2112     {
2113         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2114         return -EINVAL;
2115     }
2116
2117     switch (ctrl->id)
2118     {
2119         case V4L2_CID_BRIGHTNESS:
2120             {
2121                 ctrl->value = sensor->info_priv.brightness;
2122                 break;
2123             }
2124         case V4L2_CID_SATURATION:
2125             {
2126                 ctrl->value = sensor->info_priv.saturation;
2127                 break;
2128             }
2129         case V4L2_CID_CONTRAST:
2130             {
2131                 ctrl->value = sensor->info_priv.contrast;
2132                 break;
2133             }
2134         case V4L2_CID_DO_WHITE_BALANCE:
2135             {
2136                 ctrl->value = sensor->info_priv.whiteBalance;
2137                 break;
2138             }
2139         case V4L2_CID_EXPOSURE:
2140             {
2141                 ctrl->value = sensor->info_priv.exposure;
2142                 break;
2143             }
2144         case V4L2_CID_HFLIP:
2145             {
2146                 ctrl->value = sensor->info_priv.mirror;
2147                 break;
2148             }
2149         case V4L2_CID_VFLIP:
2150             {
2151                 ctrl->value = sensor->info_priv.flip;
2152                 break;
2153             }
2154         default :
2155                 break;
2156     }
2157     return 0;
2158 }
2159
2160
2161
2162 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2163 {
2164     struct i2c_client *client = v4l2_get_subdevdata(sd);  
2165     const struct v4l2_queryctrl *qctrl;
2166     
2167     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2168
2169     if (!qctrl)
2170     {
2171         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2172         return -EINVAL;
2173     }
2174
2175     switch (ctrl->id)
2176     {
2177 #if CONFIG_SENSOR_Brightness
2178         case V4L2_CID_BRIGHTNESS:
2179             {
2180                 if (ctrl->value != sensor->info_priv.brightness)
2181                 {
2182                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2183                     {
2184                         return -EINVAL;
2185                     }
2186                     sensor->info_priv.brightness = ctrl->value;
2187                 }
2188                 break;
2189             }
2190 #endif
2191 #if CONFIG_SENSOR_Exposure
2192         case V4L2_CID_EXPOSURE:
2193             {
2194                 if (ctrl->value != sensor->info_priv.exposure)
2195                 {
2196                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2197                     {
2198                         return -EINVAL;
2199                     }
2200                     sensor->info_priv.exposure = ctrl->value;
2201                 }
2202                 break;
2203             }
2204 #endif
2205 #if CONFIG_SENSOR_Saturation
2206         case V4L2_CID_SATURATION:
2207             {
2208                 if (ctrl->value != sensor->info_priv.saturation)
2209                 {
2210                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2211                     {
2212                         return -EINVAL;
2213                     }
2214                     sensor->info_priv.saturation = ctrl->value;
2215                 }
2216                 break;
2217             }
2218 #endif
2219 #if CONFIG_SENSOR_Contrast
2220         case V4L2_CID_CONTRAST:
2221             {
2222                 if (ctrl->value != sensor->info_priv.contrast)
2223                 {
2224                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2225                     {
2226                         return -EINVAL;
2227                     }
2228                     sensor->info_priv.contrast = ctrl->value;
2229                 }
2230                 break;
2231             }
2232 #endif
2233 #if CONFIG_SENSOR_WhiteBalance
2234         case V4L2_CID_DO_WHITE_BALANCE:
2235             {
2236                 if (ctrl->value != sensor->info_priv.whiteBalance)
2237                 {
2238                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2239                     {
2240                         return -EINVAL;
2241                     }
2242                     sensor->info_priv.whiteBalance = ctrl->value;
2243                 }
2244                 break;
2245             }
2246 #endif
2247 #if CONFIG_SENSOR_Mirror
2248         case V4L2_CID_HFLIP:
2249             {
2250                 if (ctrl->value != sensor->info_priv.mirror)
2251                 {
2252                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2253                         return -EINVAL;
2254                     sensor->info_priv.mirror = ctrl->value;
2255                 }
2256                 break;
2257             }
2258 #endif
2259 #if CONFIG_SENSOR_Flip
2260         case V4L2_CID_VFLIP:
2261             {
2262                 if (ctrl->value != sensor->info_priv.flip)
2263                 {
2264                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2265                         return -EINVAL;
2266                     sensor->info_priv.flip = ctrl->value;
2267                 }
2268                 break;
2269             }
2270 #endif
2271         default:
2272             break;
2273     }
2274
2275     return 0;
2276 }
2277 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2278 {
2279     const struct v4l2_queryctrl *qctrl;
2280     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2281     struct sensor *sensor = to_sensor(client);
2282
2283     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2284
2285     if (!qctrl)
2286     {
2287         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2288         return -EINVAL;
2289     }
2290
2291     switch (ext_ctrl->id)
2292     {
2293         case V4L2_CID_SCENE:
2294             {
2295                 ext_ctrl->value = sensor->info_priv.scene;
2296                 break;
2297             }
2298         case V4L2_CID_EFFECT:
2299             {
2300                 ext_ctrl->value = sensor->info_priv.effect;
2301                 break;
2302             }
2303         case V4L2_CID_ZOOM_ABSOLUTE:
2304             {
2305                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2306                 break;
2307             }
2308         case V4L2_CID_ZOOM_RELATIVE:
2309             {
2310                 return -EINVAL;
2311             }
2312         case V4L2_CID_FOCUS_ABSOLUTE:
2313             {
2314                 ext_ctrl->value = sensor->info_priv.focus;
2315                 break;
2316             }
2317         case V4L2_CID_FOCUS_RELATIVE:
2318             {
2319                 return -EINVAL;
2320             }
2321         case V4L2_CID_FLASH:
2322             {
2323                 ext_ctrl->value = sensor->info_priv.flash;
2324                 break;
2325             }
2326         default :
2327             break;
2328     }
2329     return 0;
2330 }
2331 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2332 {
2333     const struct v4l2_queryctrl *qctrl;
2334     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2335     struct sensor *sensor = to_sensor(client);
2336     int val_offset;
2337
2338     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2339
2340     if (!qctrl)
2341     {
2342         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2343         return -EINVAL;
2344     }
2345
2346         val_offset = 0;
2347     switch (ext_ctrl->id)
2348     {
2349 #if CONFIG_SENSOR_Scene
2350         case V4L2_CID_SCENE:
2351             {
2352                 if (ext_ctrl->value != sensor->info_priv.scene)
2353                 {
2354                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2355                         return -EINVAL;
2356                     sensor->info_priv.scene = ext_ctrl->value;
2357                 }
2358                 break;
2359             }
2360 #endif
2361 #if CONFIG_SENSOR_Effect
2362         case V4L2_CID_EFFECT:
2363             {
2364                 if (ext_ctrl->value != sensor->info_priv.effect)
2365                 {
2366                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2367                         return -EINVAL;
2368                     sensor->info_priv.effect= ext_ctrl->value;
2369                 }
2370                 break;
2371             }
2372 #endif
2373 #if CONFIG_SENSOR_DigitalZoom
2374         case V4L2_CID_ZOOM_ABSOLUTE:
2375             {
2376                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2377                     return -EINVAL;
2378
2379                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2380                 {
2381                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2382
2383                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2384                         return -EINVAL;
2385                     sensor->info_priv.digitalzoom += val_offset;
2386
2387                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2388                 }
2389
2390                 break;
2391             }
2392         case V4L2_CID_ZOOM_RELATIVE:
2393             {
2394                 if (ext_ctrl->value)
2395                 {
2396                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2397                         return -EINVAL;
2398                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2399
2400                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2401                 }
2402                 break;
2403             }
2404 #endif
2405 #if CONFIG_SENSOR_Focus
2406         case V4L2_CID_FOCUS_ABSOLUTE:
2407             {
2408                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2409                     return -EINVAL;
2410
2411                 if (ext_ctrl->value != sensor->info_priv.focus)
2412                 {
2413                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2414
2415                     sensor->info_priv.focus += val_offset;
2416                 }
2417
2418                 break;
2419             }
2420         case V4L2_CID_FOCUS_RELATIVE:
2421             {
2422                 if (ext_ctrl->value)
2423                 {
2424                     sensor->info_priv.focus += ext_ctrl->value;
2425
2426                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2427                 }
2428                 break;
2429             }
2430 #endif
2431 #if CONFIG_SENSOR_Flash
2432         case V4L2_CID_FLASH:
2433             {
2434                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2435                     return -EINVAL;
2436                 sensor->info_priv.flash = ext_ctrl->value;
2437
2438                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2439                 break;
2440             }
2441 #endif
2442         default:
2443             break;
2444     }
2445
2446     return 0;
2447 }
2448
2449 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2450 {
2451     struct i2c_client *client = v4l2_get_subdevdata(sd);
2452     struct soc_camera_device *icd = client->dev.platform_data;
2453     int i, error_cnt=0, error_idx=-1;
2454
2455
2456     for (i=0; i<ext_ctrl->count; i++) {
2457         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2458             error_cnt++;
2459             error_idx = i;
2460         }
2461     }
2462
2463     if (error_cnt > 1)
2464         error_idx = ext_ctrl->count;
2465
2466     if (error_idx != -1) {
2467         ext_ctrl->error_idx = error_idx;
2468         return -EINVAL;
2469     } else {
2470         return 0;
2471     }
2472 }
2473
2474 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2475 {
2476     struct i2c_client *client = v4l2_get_subdevdata(sd);
2477     struct soc_camera_device *icd = client->dev.platform_data;
2478     int i, error_cnt=0, error_idx=-1;
2479
2480
2481     for (i=0; i<ext_ctrl->count; i++) {
2482         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2483             error_cnt++;
2484             error_idx = i;
2485         }
2486     }
2487
2488     if (error_cnt > 1)
2489         error_idx = ext_ctrl->count;
2490
2491     if (error_idx != -1) {
2492         ext_ctrl->error_idx = error_idx;
2493         return -EINVAL;
2494     } else {
2495         return 0;
2496     }
2497 }
2498
2499 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2500  * this wasn't our capture interface, so, we wait for the right one */
2501 static int sensor_video_probe(struct soc_camera_device *icd,
2502                                struct i2c_client *client)
2503 {
2504     char pid = 0;
2505     int ret;
2506     struct sensor *sensor = to_sensor(client);
2507
2508     /* We must have a parent by now. And it cannot be a wrong one.
2509      * So this entire test is completely redundant. */
2510     if (!icd->dev.parent ||
2511             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2512                 return -ENODEV;
2513
2514         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2515                 ret = -ENODEV;
2516                 goto sensor_video_probe_err;
2517         }
2518     /* soft reset */
2519     ret = sensor_write(client, 0x12, 0x80);
2520     if (ret != 0)
2521     {
2522         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2523         return -ENODEV;
2524     }
2525     mdelay(50);          //delay 5 microseconds
2526
2527     /* check if it is an sensor sensor */
2528     ret = sensor_read(client, 0x0a, &pid);
2529     if (ret != 0) {
2530         SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2531         ret = -ENODEV;
2532         goto sensor_video_probe_err;
2533     }
2534
2535     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2536     if (pid == SENSOR_ID) {
2537         sensor->model = SENSOR_V4L2_IDENT;
2538     } else {
2539         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2540         ret = -ENODEV;
2541         goto sensor_video_probe_err;
2542     }
2543
2544     return 0;
2545
2546 sensor_video_probe_err:
2547
2548     return ret;
2549 }
2550
2551 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2552 {
2553         struct i2c_client *client = v4l2_get_subdevdata(sd);
2554     struct soc_camera_device *icd = client->dev.platform_data;
2555     struct sensor *sensor = to_sensor(client);
2556     int ret = 0;
2557 #if CONFIG_SENSOR_Flash 
2558     int i;
2559 #endif
2560     
2561         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2562         switch (cmd)
2563         {
2564                 case RK29_CAM_SUBDEV_DEACTIVATE:
2565                 {
2566                         sensor_deactivate(client);
2567                         break;
2568                 }
2569
2570                 case RK29_CAM_SUBDEV_IOREQUEST:
2571                 {
2572                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2573             if (sensor->sensor_io_request != NULL) { 
2574                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
2575                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2576                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2577                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
2578                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2579                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2580                 }
2581             } else {
2582                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2583                 ret = -EINVAL;
2584                 goto sensor_ioctl_end;
2585             }
2586             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2587                for this project */
2588             #if CONFIG_SENSOR_Flash     
2589                 if (sensor->sensor_gpio_res) { 
2590                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2591                     for (i = 0; i < icd->ops->num_controls; i++) {
2592                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2593                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2594                                 }
2595                     }
2596                     sensor->info_priv.flash = 0xff;
2597                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2598                 }
2599                 }
2600             #endif
2601                         break;
2602                 }
2603                 default:
2604                 {
2605                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2606                         break;
2607                 }
2608         }
2609 sensor_ioctl_end:
2610         return ret;
2611
2612 }
2613 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2614                             enum v4l2_mbus_pixelcode *code)
2615 {
2616         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2617                 return -EINVAL;
2618
2619         *code = sensor_colour_fmts[index].code;
2620         return 0;
2621 }
2622 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2623         .init           = sensor_init,
2624         .g_ctrl         = sensor_g_control,
2625         .s_ctrl         = sensor_s_control,
2626         .g_ext_ctrls          = sensor_g_ext_controls,
2627         .s_ext_ctrls          = sensor_s_ext_controls,
2628         .g_chip_ident   = sensor_g_chip_ident,
2629         .ioctl = sensor_ioctl,
2630 };
2631
2632 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2633         .s_mbus_fmt     = sensor_s_fmt,
2634         .g_mbus_fmt     = sensor_g_fmt,
2635         .try_mbus_fmt   = sensor_try_fmt,
2636         .enum_mbus_fmt  = sensor_enum_fmt,
2637 };
2638
2639 static struct v4l2_subdev_ops sensor_subdev_ops = {
2640         .core   = &sensor_subdev_core_ops,
2641         .video = &sensor_subdev_video_ops,
2642 };
2643
2644 static int sensor_probe(struct i2c_client *client,
2645                          const struct i2c_device_id *did)
2646 {
2647     struct sensor *sensor;
2648     struct soc_camera_device *icd = client->dev.platform_data;
2649     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2650     struct soc_camera_link *icl;
2651     int ret;
2652
2653     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2654     if (!icd) {
2655         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2656         return -EINVAL;
2657     }
2658
2659     icl = to_soc_camera_link(icd);
2660     if (!icl) {
2661         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2662         return -EINVAL;
2663     }
2664
2665     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2666         dev_warn(&adapter->dev,
2667                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2668         return -EIO;
2669     }
2670
2671     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2672     if (!sensor)
2673         return -ENOMEM;
2674
2675     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2676
2677     /* Second stage probe - when a capture adapter is there */
2678     icd->ops            = &sensor_ops;
2679     sensor->info_priv.fmt = sensor_colour_fmts[0];
2680         #if CONFIG_SENSOR_I2C_NOSCHED
2681         atomic_set(&sensor->tasklock_cnt,0);
2682         #endif
2683
2684     ret = sensor_video_probe(icd, client);
2685     if (ret < 0) {
2686         icd->ops = NULL;
2687         i2c_set_clientdata(client, NULL);
2688         kfree(sensor);
2689                 sensor = NULL;
2690     }
2691     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2692     return ret;
2693 }
2694
2695 static int sensor_remove(struct i2c_client *client)
2696 {
2697     struct sensor *sensor = to_sensor(client);
2698     struct soc_camera_device *icd = client->dev.platform_data;
2699
2700     icd->ops = NULL;
2701     i2c_set_clientdata(client, NULL);
2702     client->driver = NULL;
2703     kfree(sensor);
2704         sensor = NULL;
2705     return 0;
2706 }
2707
2708 static const struct i2c_device_id sensor_id[] = {
2709         {SENSOR_NAME_STRING(), 0 },
2710         { }
2711 };
2712 MODULE_DEVICE_TABLE(i2c, sensor_id);
2713
2714 static struct i2c_driver sensor_i2c_driver = {
2715         .driver = {
2716                 .name = SENSOR_NAME_STRING(),
2717         },
2718         .probe          = sensor_probe,
2719         .remove         = sensor_remove,
2720         .id_table       = sensor_id,
2721 };
2722
2723 static int __init sensor_mod_init(void)
2724 {
2725     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2726     return i2c_add_driver(&sensor_i2c_driver);
2727 }
2728
2729 static void __exit sensor_mod_exit(void)
2730 {
2731     i2c_del_driver(&sensor_i2c_driver);
2732 }
2733
2734 device_initcall_sync(sensor_mod_init);
2735 module_exit(sensor_mod_exit);
2736
2737 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2738 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2739 MODULE_LICENSE("GPL");
2740
2741