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