Merge remote-tracking branch 'origin/develop-3.0-rk30' into develop-3.0
[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,set_w,set_h;
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     set_w = mf->width;
1815     set_h = mf->height;
1816
1817         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1818         {
1819         set_w = 176;
1820         set_h = 144;
1821         }
1822         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1823     {
1824         set_w = 320;
1825         set_h = 240;
1826     }
1827     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1828     {
1829         set_w = 352;
1830         set_h = 288;
1831     }
1832     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1833     {
1834         set_w = 640;
1835         set_h = 480;
1836     }
1837     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1838     {
1839         set_w = 800;
1840         set_h = 600;
1841     }
1842     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1843     {
1844         set_w = 1280;
1845         set_h = 1024;
1846     }
1847     else
1848     {
1849         set_w = SENSOR_INIT_WIDTH;
1850         set_h = SENSOR_INIT_HEIGHT;             
1851     }
1852
1853     mf->width = set_w;
1854     mf->height = set_h;
1855     mf->colorspace = fmt->colorspace;
1856     
1857     return ret;
1858 }
1859
1860  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1861 {
1862     struct i2c_client *client = v4l2_get_subdevdata(sd);
1863
1864     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1865         return -EINVAL;
1866
1867     if (id->match.addr != client->addr)
1868         return -ENODEV;
1869
1870     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV9650  identifier */
1871     id->revision = 0;
1872
1873     return 0;
1874 }
1875 #if CONFIG_SENSOR_Brightness
1876 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1877 {
1878     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1879
1880     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1881     {
1882         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1883         {
1884             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1885             {
1886                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1887                 return -EINVAL;
1888             }
1889             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1890             return 0;
1891         }
1892     }
1893         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1894     return -EINVAL;
1895 }
1896 #endif
1897 #if CONFIG_SENSOR_Effect
1898 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1899 {
1900     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1901
1902     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1903     {
1904         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1905         {
1906             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1907             {
1908                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1909                 return -EINVAL;
1910             }
1911             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1912             return 0;
1913         }
1914     }
1915         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1916     return -EINVAL;
1917 }
1918 #endif
1919 #if CONFIG_SENSOR_Exposure
1920 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1921 {
1922     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1923
1924     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1925     {
1926         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1927         {
1928             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1929             {
1930                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1931                 return -EINVAL;
1932             }
1933             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1934             return 0;
1935         }
1936     }
1937         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1938     return -EINVAL;
1939 }
1940 #endif
1941 #if CONFIG_SENSOR_Saturation
1942 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1943 {
1944     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1945
1946     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1947     {
1948         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1949         {
1950             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1951             {
1952                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1953                 return -EINVAL;
1954             }
1955             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1956             return 0;
1957         }
1958     }
1959     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1960     return -EINVAL;
1961 }
1962 #endif
1963 #if CONFIG_SENSOR_Contrast
1964 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1965 {
1966     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1967
1968     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1969     {
1970         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1971         {
1972             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1973             {
1974                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1975                 return -EINVAL;
1976             }
1977             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1978             return 0;
1979         }
1980     }
1981     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1982     return -EINVAL;
1983 }
1984 #endif
1985 #if CONFIG_SENSOR_Mirror
1986 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1987 {
1988     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1989
1990     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1991     {
1992         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1993         {
1994             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1995             {
1996                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1997                 return -EINVAL;
1998             }
1999             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2000             return 0;
2001         }
2002     }
2003     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2004     return -EINVAL;
2005 }
2006 #endif
2007 #if CONFIG_SENSOR_Flip
2008 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2009 {
2010     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2011
2012     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2013     {
2014         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2015         {
2016             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2017             {
2018                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2019                 return -EINVAL;
2020             }
2021             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2022             return 0;
2023         }
2024     }
2025     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2026     return -EINVAL;
2027 }
2028 #endif
2029 #if CONFIG_SENSOR_Scene
2030 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2031 {
2032     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2033
2034     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2035     {
2036         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2037         {
2038             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2039             {
2040                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2041                 return -EINVAL;
2042             }
2043             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2044             return 0;
2045         }
2046     }
2047     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2048     return -EINVAL;
2049 }
2050 #endif
2051 #if CONFIG_SENSOR_WhiteBalance
2052 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2053 {
2054     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2055
2056     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2057     {
2058         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2059         {
2060             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2061             {
2062                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2063                 return -EINVAL;
2064             }
2065             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2066             return 0;
2067         }
2068     }
2069         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2070     return -EINVAL;
2071 }
2072 #endif
2073 #if CONFIG_SENSOR_DigitalZoom
2074 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2075 {
2076     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2077     struct sensor *sensor = to_sensor(client);
2078         const struct v4l2_queryctrl *qctrl_info;
2079     int digitalzoom_cur, digitalzoom_total;
2080
2081         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2082         if (qctrl_info)
2083                 return -EINVAL;
2084
2085     digitalzoom_cur = sensor->info_priv.digitalzoom;
2086     digitalzoom_total = qctrl_info->maximum;
2087
2088     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2089     {
2090         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2091         return -EINVAL;
2092     }
2093
2094     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2095     {
2096         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2097         return -EINVAL;
2098     }
2099
2100     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2101     {
2102         *value = digitalzoom_total - digitalzoom_cur;
2103     }
2104
2105     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2106     {
2107         *value = 0 - digitalzoom_cur;
2108     }
2109
2110     digitalzoom_cur += *value;
2111
2112     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2113     {
2114         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2115         {
2116             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2117             return -EINVAL;
2118         }
2119         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2120         return 0;
2121     }
2122
2123     return -EINVAL;
2124 }
2125 #endif
2126 #if CONFIG_SENSOR_Flash
2127 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2128 {    
2129     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2130         if (value == 3) {       /* ddl@rock-chips.com: torch */
2131             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2132         } else {
2133             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2134         }
2135         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2136         return 0;
2137     }
2138     
2139         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2140     return -EINVAL;
2141 }
2142 #endif
2143
2144 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2145 {
2146     struct i2c_client *client = v4l2_get_subdevdata(sd);
2147     struct sensor *sensor = to_sensor(client);
2148     const struct v4l2_queryctrl *qctrl;
2149
2150     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2151
2152     if (!qctrl)
2153     {
2154         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2155         return -EINVAL;
2156     }
2157
2158     switch (ctrl->id)
2159     {
2160         case V4L2_CID_BRIGHTNESS:
2161             {
2162                 ctrl->value = sensor->info_priv.brightness;
2163                 break;
2164             }
2165         case V4L2_CID_SATURATION:
2166             {
2167                 ctrl->value = sensor->info_priv.saturation;
2168                 break;
2169             }
2170         case V4L2_CID_CONTRAST:
2171             {
2172                 ctrl->value = sensor->info_priv.contrast;
2173                 break;
2174             }
2175         case V4L2_CID_DO_WHITE_BALANCE:
2176             {
2177                 ctrl->value = sensor->info_priv.whiteBalance;
2178                 break;
2179             }
2180         case V4L2_CID_EXPOSURE:
2181             {
2182                 ctrl->value = sensor->info_priv.exposure;
2183                 break;
2184             }
2185         case V4L2_CID_HFLIP:
2186             {
2187                 ctrl->value = sensor->info_priv.mirror;
2188                 break;
2189             }
2190         case V4L2_CID_VFLIP:
2191             {
2192                 ctrl->value = sensor->info_priv.flip;
2193                 break;
2194             }
2195         default :
2196                 break;
2197     }
2198     return 0;
2199 }
2200
2201
2202
2203 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2204 {
2205     struct i2c_client *client = v4l2_get_subdevdata(sd);  
2206     const struct v4l2_queryctrl *qctrl;
2207     
2208     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2209
2210     if (!qctrl)
2211     {
2212         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2213         return -EINVAL;
2214     }
2215
2216     switch (ctrl->id)
2217     {
2218 #if CONFIG_SENSOR_Brightness
2219         case V4L2_CID_BRIGHTNESS:
2220             {
2221                 if (ctrl->value != sensor->info_priv.brightness)
2222                 {
2223                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2224                     {
2225                         return -EINVAL;
2226                     }
2227                     sensor->info_priv.brightness = ctrl->value;
2228                 }
2229                 break;
2230             }
2231 #endif
2232 #if CONFIG_SENSOR_Exposure
2233         case V4L2_CID_EXPOSURE:
2234             {
2235                 if (ctrl->value != sensor->info_priv.exposure)
2236                 {
2237                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2238                     {
2239                         return -EINVAL;
2240                     }
2241                     sensor->info_priv.exposure = ctrl->value;
2242                 }
2243                 break;
2244             }
2245 #endif
2246 #if CONFIG_SENSOR_Saturation
2247         case V4L2_CID_SATURATION:
2248             {
2249                 if (ctrl->value != sensor->info_priv.saturation)
2250                 {
2251                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2252                     {
2253                         return -EINVAL;
2254                     }
2255                     sensor->info_priv.saturation = ctrl->value;
2256                 }
2257                 break;
2258             }
2259 #endif
2260 #if CONFIG_SENSOR_Contrast
2261         case V4L2_CID_CONTRAST:
2262             {
2263                 if (ctrl->value != sensor->info_priv.contrast)
2264                 {
2265                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2266                     {
2267                         return -EINVAL;
2268                     }
2269                     sensor->info_priv.contrast = ctrl->value;
2270                 }
2271                 break;
2272             }
2273 #endif
2274 #if CONFIG_SENSOR_WhiteBalance
2275         case V4L2_CID_DO_WHITE_BALANCE:
2276             {
2277                 if (ctrl->value != sensor->info_priv.whiteBalance)
2278                 {
2279                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2280                     {
2281                         return -EINVAL;
2282                     }
2283                     sensor->info_priv.whiteBalance = ctrl->value;
2284                 }
2285                 break;
2286             }
2287 #endif
2288 #if CONFIG_SENSOR_Mirror
2289         case V4L2_CID_HFLIP:
2290             {
2291                 if (ctrl->value != sensor->info_priv.mirror)
2292                 {
2293                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2294                         return -EINVAL;
2295                     sensor->info_priv.mirror = ctrl->value;
2296                 }
2297                 break;
2298             }
2299 #endif
2300 #if CONFIG_SENSOR_Flip
2301         case V4L2_CID_VFLIP:
2302             {
2303                 if (ctrl->value != sensor->info_priv.flip)
2304                 {
2305                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2306                         return -EINVAL;
2307                     sensor->info_priv.flip = ctrl->value;
2308                 }
2309                 break;
2310             }
2311 #endif
2312         default:
2313             break;
2314     }
2315
2316     return 0;
2317 }
2318 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2319 {
2320     const struct v4l2_queryctrl *qctrl;
2321     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2322     struct sensor *sensor = to_sensor(client);
2323
2324     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2325
2326     if (!qctrl)
2327     {
2328         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2329         return -EINVAL;
2330     }
2331
2332     switch (ext_ctrl->id)
2333     {
2334         case V4L2_CID_SCENE:
2335             {
2336                 ext_ctrl->value = sensor->info_priv.scene;
2337                 break;
2338             }
2339         case V4L2_CID_EFFECT:
2340             {
2341                 ext_ctrl->value = sensor->info_priv.effect;
2342                 break;
2343             }
2344         case V4L2_CID_ZOOM_ABSOLUTE:
2345             {
2346                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2347                 break;
2348             }
2349         case V4L2_CID_ZOOM_RELATIVE:
2350             {
2351                 return -EINVAL;
2352             }
2353         case V4L2_CID_FOCUS_ABSOLUTE:
2354             {
2355                 ext_ctrl->value = sensor->info_priv.focus;
2356                 break;
2357             }
2358         case V4L2_CID_FOCUS_RELATIVE:
2359             {
2360                 return -EINVAL;
2361             }
2362         case V4L2_CID_FLASH:
2363             {
2364                 ext_ctrl->value = sensor->info_priv.flash;
2365                 break;
2366             }
2367         default :
2368             break;
2369     }
2370     return 0;
2371 }
2372 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2373 {
2374     const struct v4l2_queryctrl *qctrl;
2375     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2376     struct sensor *sensor = to_sensor(client);
2377     int val_offset;
2378
2379     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2380
2381     if (!qctrl)
2382     {
2383         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2384         return -EINVAL;
2385     }
2386
2387         val_offset = 0;
2388     switch (ext_ctrl->id)
2389     {
2390 #if CONFIG_SENSOR_Scene
2391         case V4L2_CID_SCENE:
2392             {
2393                 if (ext_ctrl->value != sensor->info_priv.scene)
2394                 {
2395                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2396                         return -EINVAL;
2397                     sensor->info_priv.scene = ext_ctrl->value;
2398                 }
2399                 break;
2400             }
2401 #endif
2402 #if CONFIG_SENSOR_Effect
2403         case V4L2_CID_EFFECT:
2404             {
2405                 if (ext_ctrl->value != sensor->info_priv.effect)
2406                 {
2407                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2408                         return -EINVAL;
2409                     sensor->info_priv.effect= ext_ctrl->value;
2410                 }
2411                 break;
2412             }
2413 #endif
2414 #if CONFIG_SENSOR_DigitalZoom
2415         case V4L2_CID_ZOOM_ABSOLUTE:
2416             {
2417                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2418                     return -EINVAL;
2419
2420                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2421                 {
2422                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2423
2424                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2425                         return -EINVAL;
2426                     sensor->info_priv.digitalzoom += val_offset;
2427
2428                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2429                 }
2430
2431                 break;
2432             }
2433         case V4L2_CID_ZOOM_RELATIVE:
2434             {
2435                 if (ext_ctrl->value)
2436                 {
2437                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2438                         return -EINVAL;
2439                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2440
2441                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2442                 }
2443                 break;
2444             }
2445 #endif
2446 #if CONFIG_SENSOR_Focus
2447         case V4L2_CID_FOCUS_ABSOLUTE:
2448             {
2449                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2450                     return -EINVAL;
2451
2452                 if (ext_ctrl->value != sensor->info_priv.focus)
2453                 {
2454                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2455
2456                     sensor->info_priv.focus += val_offset;
2457                 }
2458
2459                 break;
2460             }
2461         case V4L2_CID_FOCUS_RELATIVE:
2462             {
2463                 if (ext_ctrl->value)
2464                 {
2465                     sensor->info_priv.focus += ext_ctrl->value;
2466
2467                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2468                 }
2469                 break;
2470             }
2471 #endif
2472 #if CONFIG_SENSOR_Flash
2473         case V4L2_CID_FLASH:
2474             {
2475                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2476                     return -EINVAL;
2477                 sensor->info_priv.flash = ext_ctrl->value;
2478
2479                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2480                 break;
2481             }
2482 #endif
2483         default:
2484             break;
2485     }
2486
2487     return 0;
2488 }
2489
2490 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2491 {
2492     struct i2c_client *client = v4l2_get_subdevdata(sd);
2493     struct soc_camera_device *icd = client->dev.platform_data;
2494     int i, error_cnt=0, error_idx=-1;
2495
2496
2497     for (i=0; i<ext_ctrl->count; i++) {
2498         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2499             error_cnt++;
2500             error_idx = i;
2501         }
2502     }
2503
2504     if (error_cnt > 1)
2505         error_idx = ext_ctrl->count;
2506
2507     if (error_idx != -1) {
2508         ext_ctrl->error_idx = error_idx;
2509         return -EINVAL;
2510     } else {
2511         return 0;
2512     }
2513 }
2514
2515 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2516 {
2517     struct i2c_client *client = v4l2_get_subdevdata(sd);
2518     struct soc_camera_device *icd = client->dev.platform_data;
2519     int i, error_cnt=0, error_idx=-1;
2520
2521
2522     for (i=0; i<ext_ctrl->count; i++) {
2523         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2524             error_cnt++;
2525             error_idx = i;
2526         }
2527     }
2528
2529     if (error_cnt > 1)
2530         error_idx = ext_ctrl->count;
2531
2532     if (error_idx != -1) {
2533         ext_ctrl->error_idx = error_idx;
2534         return -EINVAL;
2535     } else {
2536         return 0;
2537     }
2538 }
2539
2540 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2541  * this wasn't our capture interface, so, we wait for the right one */
2542 static int sensor_video_probe(struct soc_camera_device *icd,
2543                                struct i2c_client *client)
2544 {
2545     char pid = 0;
2546     int ret;
2547     struct sensor *sensor = to_sensor(client);
2548
2549     /* We must have a parent by now. And it cannot be a wrong one.
2550      * So this entire test is completely redundant. */
2551     if (!icd->dev.parent ||
2552             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2553                 return -ENODEV;
2554
2555         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2556                 ret = -ENODEV;
2557                 goto sensor_video_probe_err;
2558         }
2559     /* soft reset */
2560     ret = sensor_write(client, 0x12, 0x80);
2561     if (ret != 0)
2562     {
2563         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2564         return -ENODEV;
2565     }
2566     mdelay(50);          //delay 5 microseconds
2567
2568     /* check if it is an sensor sensor */
2569     ret = sensor_read(client, 0x0a, &pid);
2570     if (ret != 0) {
2571         SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2572         ret = -ENODEV;
2573         goto sensor_video_probe_err;
2574     }
2575
2576     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2577     if (pid == SENSOR_ID) {
2578         sensor->model = SENSOR_V4L2_IDENT;
2579     } else {
2580         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2581         ret = -ENODEV;
2582         goto sensor_video_probe_err;
2583     }
2584
2585     return 0;
2586
2587 sensor_video_probe_err:
2588
2589     return ret;
2590 }
2591
2592 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2593 {
2594         struct i2c_client *client = v4l2_get_subdevdata(sd);
2595     struct soc_camera_device *icd = client->dev.platform_data;
2596     struct sensor *sensor = to_sensor(client);
2597     int ret = 0;
2598 #if CONFIG_SENSOR_Flash 
2599     int i;
2600 #endif
2601     
2602         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2603         switch (cmd)
2604         {
2605                 case RK29_CAM_SUBDEV_DEACTIVATE:
2606                 {
2607                         sensor_deactivate(client);
2608                         break;
2609                 }
2610
2611                 case RK29_CAM_SUBDEV_IOREQUEST:
2612                 {
2613                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2614             if (sensor->sensor_io_request != NULL) { 
2615                 sensor->sensor_gpio_res = NULL;
2616                 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
2617                     if (sensor->sensor_io_request->gpio_res[i].dev_name && 
2618                         (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
2619                         sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
2620                     }
2621                 }
2622                 if (sensor->sensor_gpio_res == NULL) {
2623                     SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
2624                     ret = -EINVAL;
2625                     goto sensor_ioctl_end;
2626                 }
2627             } else {
2628                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2629                 ret = -EINVAL;
2630                 goto sensor_ioctl_end;
2631             }
2632             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2633                for this project */
2634             #if CONFIG_SENSOR_Flash     
2635                 if (sensor->sensor_gpio_res) { 
2636                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2637                     for (i = 0; i < icd->ops->num_controls; i++) {
2638                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2639                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2640                                 }
2641                     }
2642                     sensor->info_priv.flash = 0xff;
2643                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2644                 }
2645                 }
2646             #endif
2647                         break;
2648                 }
2649                 default:
2650                 {
2651                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2652                         break;
2653                 }
2654         }
2655 sensor_ioctl_end:
2656         return ret;
2657
2658 }
2659 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2660                             enum v4l2_mbus_pixelcode *code)
2661 {
2662         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2663                 return -EINVAL;
2664
2665         *code = sensor_colour_fmts[index].code;
2666         return 0;
2667 }
2668 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2669         .init           = sensor_init,
2670         .g_ctrl         = sensor_g_control,
2671         .s_ctrl         = sensor_s_control,
2672         .g_ext_ctrls          = sensor_g_ext_controls,
2673         .s_ext_ctrls          = sensor_s_ext_controls,
2674         .g_chip_ident   = sensor_g_chip_ident,
2675         .ioctl = sensor_ioctl,
2676 };
2677
2678 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2679         .s_mbus_fmt     = sensor_s_fmt,
2680         .g_mbus_fmt     = sensor_g_fmt,
2681         .try_mbus_fmt   = sensor_try_fmt,
2682         .enum_mbus_fmt  = sensor_enum_fmt,
2683 };
2684
2685 static struct v4l2_subdev_ops sensor_subdev_ops = {
2686         .core   = &sensor_subdev_core_ops,
2687         .video = &sensor_subdev_video_ops,
2688 };
2689
2690 static int sensor_probe(struct i2c_client *client,
2691                          const struct i2c_device_id *did)
2692 {
2693     struct sensor *sensor;
2694     struct soc_camera_device *icd = client->dev.platform_data;
2695     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2696     struct soc_camera_link *icl;
2697     int ret;
2698
2699     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2700     if (!icd) {
2701         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2702         return -EINVAL;
2703     }
2704
2705     icl = to_soc_camera_link(icd);
2706     if (!icl) {
2707         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2708         return -EINVAL;
2709     }
2710
2711     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2712         dev_warn(&adapter->dev,
2713                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2714         return -EIO;
2715     }
2716
2717     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2718     if (!sensor)
2719         return -ENOMEM;
2720
2721     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2722
2723     /* Second stage probe - when a capture adapter is there */
2724     icd->ops            = &sensor_ops;
2725     sensor->info_priv.fmt = sensor_colour_fmts[0];
2726         #if CONFIG_SENSOR_I2C_NOSCHED
2727         atomic_set(&sensor->tasklock_cnt,0);
2728         #endif
2729
2730     ret = sensor_video_probe(icd, client);
2731     if (ret < 0) {
2732         icd->ops = NULL;
2733         i2c_set_clientdata(client, NULL);
2734         kfree(sensor);
2735                 sensor = NULL;
2736     }
2737     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2738     return ret;
2739 }
2740
2741 static int sensor_remove(struct i2c_client *client)
2742 {
2743     struct sensor *sensor = to_sensor(client);
2744     struct soc_camera_device *icd = client->dev.platform_data;
2745
2746     icd->ops = NULL;
2747     i2c_set_clientdata(client, NULL);
2748     client->driver = NULL;
2749     kfree(sensor);
2750         sensor = NULL;
2751     return 0;
2752 }
2753
2754 static const struct i2c_device_id sensor_id[] = {
2755         {SENSOR_NAME_STRING(), 0 },
2756         { }
2757 };
2758 MODULE_DEVICE_TABLE(i2c, sensor_id);
2759
2760 static struct i2c_driver sensor_i2c_driver = {
2761         .driver = {
2762                 .name = SENSOR_NAME_STRING(),
2763         },
2764         .probe          = sensor_probe,
2765         .remove         = sensor_remove,
2766         .id_table       = sensor_id,
2767 };
2768
2769 static int __init sensor_mod_init(void)
2770 {
2771     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2772     return i2c_add_driver(&sensor_i2c_driver);
2773 }
2774
2775 static void __exit sensor_mod_exit(void)
2776 {
2777     i2c_del_driver(&sensor_i2c_driver);
2778 }
2779
2780 device_initcall_sync(sensor_mod_init);
2781 module_exit(sensor_mod_exit);
2782
2783 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2784 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2785 MODULE_LICENSE("GPL");
2786
2787