camera driver v0.2.1 for RK30
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / ov7675.c
1
2 /*
3 o* Driver for MT9M001 CMOS Image Sensor from Micron
4  *
5  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/videodev2.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/log2.h>
16 #include <linux/platform_device.h>
17 #include <linux/delay.h>
18 #include <linux/circ_buf.h>
19 #include <linux/miscdevice.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-chip-ident.h>
22 #include <media/soc_camera.h>
23 #include <mach/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 RK_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 static s32 sensor_init_width = 0;
96 static s32 sensor_init_height = 0;
97 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);
98 static enum v4l2_mbus_pixelcode sensor_init_pixelcode = V4L2_MBUS_FMT_YUYV8_2X8;
99 static struct reginfo* sensor_init_data_p = NULL;
100 static struct reginfo* sensor_init_winseq_p = NULL;
101 static struct reginfo* sensor_init_winseq_board = NULL;
102 /* init 800*600 SVGA */
103 static struct reginfo sensor_init_data[] =
104 {
105         {0x12, 0x80},
106         {0x11, 0x80},
107         {0x2a, 0x00},
108         {0x2b, 0x00},
109         {0x92, 0x66},
110         {0x93, 0x00},
111         {0x3a, 0x0c},
112         {0x3D, 0xC0},
113         {0x12, 0x00},
114         {0x15, 0x00},
115         {0xc1, 0x7f},
116         {0x17, 0x13},
117         {0x18, 0x01},
118         {0x32, 0xbF},
119         {0x19, 0x03},
120         {0x1a, 0x7b},
121         {0x03, 0x0a},
122         {0x0c, 0x00},
123         {0x3e, 0x00},
124         {0x70, 0x3a},
125         {0x71, 0x35},
126         {0x72, 0x11},
127         {0x73, 0xf0},
128         {0xa2, 0x02},
129         {0x13, 0xe0},
130         //{0x00, 0x00},
131         {0x10, 0x00},
132         {0x14, 0x28},
133         {0xa5, 0x06},
134         {0xab, 0x07},
135         {0x24, 0x58},
136         {0x25, 0x48},
137         {0x26, 0x93},
138         {0x9f, 0x78},
139         {0xa0, 0x68},
140         {0xa1, 0x03},
141         {0xa6, 0xD8},
142         {0xa7, 0xD8},
143         {0xa8, 0xf0},
144         {0xa9, 0x90},
145         {0xaa, 0x14},
146         {0x13, 0xe5},
147         {0x0e, 0x61},
148         {0x0f, 0x4b},
149         {0x16, 0x02},
150         {0x1e, 0x17},       //0x07//0x27
151         {0x21, 0x02},
152         {0x22, 0x91},
153         {0x29, 0x07},
154         {0x33, 0x0b},
155         {0x35, 0x0b},
156         {0x37, 0x1d},
157         {0x38, 0x71},
158         {0x39, 0x2a},
159         {0x3c, 0x78},
160         {0x4d, 0x40},
161         {0x4e, 0x20},
162         {0x69, 0x00},
163         {0x6b, 0x0a},
164         {0x74, 0x10},
165         {0x8d, 0x4f},
166         {0x8e, 0x00},
167         {0x8f, 0x00},
168         {0x90, 0x00},
169         {0x91, 0x00},
170         {0x96, 0x00},
171         {0x9a, 0x80},
172         {0xb0, 0x84},
173         {0xb1, 0x0c},
174         {0xb2, 0x0e},
175         {0xb3, 0x82},
176         {0xb8, 0x0a},
177         {0xbb, 0xa1},
178         {0x0d, 0x60},
179         {0x42, 0x80},
180         {0x62, 0x00},
181         {0x63, 0x00},
182         {0x64, 0x10},
183         {0x65, 0x07},
184         {0x66, 0x05},
185         {0x94, 0x10},
186         {0x95, 0x12},
187         {0x7a, 0x20},// {0x7a, 0x24}, 
188         {0x7b, 0x16},// {0x7b, 0x04}, 
189         {0x7c, 0x23},// {0x7c, 0x07}, 
190         {0x7d, 0x3c},// {0x7d, 0x12}, 
191         {0x7e, 0x5c},// {0x7e, 0x2f}, 
192         {0x7f, 0x69},// {0x7f, 0x3f}, 
193         {0x80, 0x75},// {0x80, 0x4d}, 
194         {0x81, 0x7e},// {0x81, 0x5a}, 
195         {0x82, 0x88},// {0x82, 0x69}, 
196         {0x83, 0x8f},// {0x83, 0x74}, 
197         {0x84, 0x96},// {0x84, 0x7f}, 
198         {0x85, 0xa3},// {0x85, 0x91}, 
199         {0x86, 0xaf},// {0x86, 0x9e}, 
200         {0x87, 0xc4},// {0x87, 0xbb}, 
201         {0x88, 0xd7},// {0x88, 0xd2}, 
202         {0x89, 0xe8},// {0x89, 0xe5}, 
203         {0x43, 0x0a},
204         {0x44, 0xf0},
205         {0x45, 0x34},
206         {0x46, 0x58},
207         {0x47, 0x28},
208         {0x48, 0x3a},
209         {0x59, 0x88},
210         {0x5a, 0x88},
211         {0x5b, 0xc2},
212         {0x5c, 0x60},
213         {0x5d, 0x58},
214         {0x5e, 0x10},
215         {0x6c, 0x0a},
216         {0x6d, 0x55},
217         {0x6e, 0x11},
218         {0x6f, 0x9e},
219         {0x6a, 0x40},
220         {0x01, 0x56},
221         {0x02, 0x44},
222         {0x13, 0xe7},
223         {0x4f, 0x95},
224         {0x50, 0x99},
225         {0x51, 0x04},
226         {0x52, 0x1a},
227         {0x53, 0x7f},
228         {0x54, 0x99},
229         {0x58, 0x1a},
230         {0x3f, 0x02},
231         {0x75, 0x63},
232         {0x76, 0xe1},
233         {0x4c, 0x00},
234         {0x77, 0x04},//0x01
235         {0x4b, 0x09},
236         {0xc9, 0x60},
237         {0x41, 0x38},
238         {0x56, 0x40},
239         {0x34, 0x11},
240         {0x3b, 0xaa},
241         {0xa4, 0x88},
242         {0x96, 0x00},
243         {0x97, 0x30},
244         {0x98, 0x20},
245         {0x99, 0x30},
246         {0x9a, 0x84},
247         {0x9b, 0x29},
248         {0x9c, 0x03},
249         {0x9d, 0x99},
250         {0x9e, 0x99},
251         {0x78, 0x04},
252         {0x79, 0x01},
253         {0xc8, 0xf0},
254         {0x79, 0x0f},
255         {0xc8, 0x00},
256         {0x79, 0x10},
257         {0xc8, 0x7e},
258         {0x79, 0x0a},
259         {0xc8, 0x80},
260         {0x79, 0x0b},
261         {0xc8, 0x01},
262         {0x79, 0x0c},
263         {0xc8, 0x0f},
264         {0x79, 0x0d},
265         {0xc8, 0x20},
266         {0x79, 0x09},
267         {0xc8, 0x80},
268         {0x79, 0x02},
269         {0xc8, 0xc0},
270         {0x79, 0x03},
271         {0xc8, 0x40},
272         {0x79, 0x05},
273         {0xc8, 0x30},
274         {0x79, 0x26},
275         {0x00, 0x00}
276 };
277
278 /* 1280X1024 SXGA */
279 static struct reginfo sensor_sxga[] =
280 {
281         {0x00,0x00}
282 };
283
284 /* 800X600 SVGA*/
285 static struct reginfo sensor_svga[] =
286 {
287     {0x0, 0x0},
288 };
289
290 /* 640X480 VGA */
291 static struct reginfo sensor_vga[] =
292 {
293     {0x17, 0x13},
294     {0x18, 0x01},
295     {0x32, 0xbf},
296     {0x19, 0x03},
297     {0x1a, 0x7b},
298     {0x03, 0x0a},
299         {0x00,0x00}
300 };
301
302 /* 352X288 CIF */
303 static struct reginfo sensor_cif[] =
304 {
305     {0x00,0x00}
306 };
307
308 /* 320*240 QVGA */
309 static  struct reginfo sensor_qvga[] =
310 {
311     {0x00,0x00}
312 };
313
314 /* 176X144 QCIF*/
315 static struct reginfo sensor_qcif[] =
316 {
317     {0x00,0x00}
318 };
319
320 static  struct reginfo sensor_ClrFmt_YUYV[]=
321 {
322     {0x00, 0x00}
323 };
324
325 static  struct reginfo sensor_ClrFmt_UYVY[]=
326 {
327     {0x00, 0x00}
328 };
329
330
331 #if CONFIG_SENSOR_WhiteBalance
332 static  struct reginfo sensor_WhiteB_Auto[]=
333 {
334         {0x84, 0x6C},           //Contrast 4
335         {0x85, 0x78},
336         {0x86, 0x8C},
337         {0x87, 0x9E},
338         {0x88, 0xBB},
339         {0x89, 0xD2},
340         {0x8A, 0xE6},
341         {0x6C, 0x40},
342         {0x6D, 0x30},
343         {0x6E, 0x48},
344         {0x6F, 0x60},
345         {0x70, 0x70},
346         {0x71, 0x70},
347         {0x72, 0x70},
348         {0x73, 0x70},
349         {0x74, 0x60},
350         {0x75, 0x60},
351         {0x76, 0x50},
352         {0x77, 0x48},
353         {0x78, 0x3A},
354         {0x79, 0x2E},
355         {0x7A, 0x28},
356         {0x7B, 0x22},
357
358         {0x0f, 0x4a},           //Saturation 3
359         {0x27, 0x80},
360         {0x28, 0x80},
361         {0x2c, 0x80},
362         {0x62, 0x60},
363         {0x63, 0xe0},
364         {0x64, 0x04},
365         {0x65, 0x00},
366         {0x66, 0x01},
367         {0x24, 0x70},
368         {0x25, 0x64},
369
370         {0x4f, 0x2e},           //Brightness 3
371         {0x50, 0x31},
372         {0x51, 0x02},
373         {0x52, 0x0e},
374         {0x53, 0x1e},
375         {0x54, 0x2d},
376
377         {0x11, 0x80},
378         {0x14, 0x2a},
379         {0x13, 0xe7},
380         {0x66, 0x05},
381
382     {0x00, 0x00}
383 };
384 /* Cloudy Colour Temperature : 6500K - 8000K  */
385 static  struct reginfo sensor_WhiteB_Cloudy[]=
386 {
387         {0x7C,0x04},            //Contrast 5
388         {0x7D,0x09},
389         {0x7E,0x13},
390         {0x7F,0x29},
391         {0x80,0x35},
392         {0x81,0x41},
393         {0x82,0x4D},
394         {0x83,0x59},
395         {0x84,0x64},
396         {0x85,0x6F},
397         {0x86,0x85},
398         {0x87,0x97},
399         {0x88,0xB7},
400         {0x89,0xCF},
401         {0x8A,0xE3},
402         {0x6C,0x40},
403         {0x6D,0x50},
404         {0x6E,0x50},
405         {0x6F,0x58},
406         {0x70,0x60},
407         {0x71,0x60},
408         {0x72,0x60},
409         {0x73,0x60},
410         {0x74,0x58},
411         {0x75,0x58},
412         {0x76,0x58},
413         {0x77,0x48},
414         {0x78,0x40},
415         {0x79,0x30},
416         {0x7A,0x28},
417         {0x7B,0x26},
418
419
420
421         {0x4f,0x3a},            //Saturation 4
422         {0x50,0x3d},
423         {0x51,0x03},
424         {0x52,0x12},
425         {0x53,0x26},
426         {0x54,0x38},
427         {0x4f, 0x2e},           //Brightness 3
428         {0x50, 0x31},
429         {0x51, 0x02},
430         {0x52, 0x0e},
431         {0x53, 0x1e},
432         {0x54, 0x2d},
433
434         {0x11,0x80},
435         {0x14,0x0a},
436         {0x13,0xc7},
437         {0x66,0x05},
438     {0x00, 0x00}
439 };
440 /* ClearDay Colour Temperature : 5000K - 6500K  */
441 static  struct reginfo sensor_WhiteB_ClearDay[]=
442 {
443     //Sunny
444         {0x7C,0x04},            //Contrast 5
445         {0x7D,0x09},
446         {0x7E,0x13},
447         {0x7F,0x29},
448         {0x80,0x35},
449         {0x81,0x41},
450         {0x82,0x4D},
451         {0x83,0x59},
452         {0x84,0x64},
453         {0x85,0x6F},
454         {0x86,0x85},
455         {0x87,0x97},
456         {0x88,0xB7},
457         {0x89,0xCF},
458         {0x8A,0xE3},
459         {0x6C,0x40},
460         {0x6D,0x50},
461         {0x6E,0x50},
462         {0x6F,0x58},
463         {0x70,0x60},
464         {0x71,0x60},
465         {0x72,0x60},
466         {0x73,0x60},
467         {0x74,0x58},
468         {0x75,0x58},
469         {0x76,0x58},
470         {0x77,0x48},
471         {0x78,0x40},
472         {0x79,0x30},
473         {0x7A,0x28},
474         {0x7B,0x26},
475
476
477
478         {0x4f,0x3a},            //Saturation 4
479         {0x50,0x3d},
480         {0x51,0x03},
481         {0x52,0x12},
482         {0x53,0x26},
483         {0x54,0x38},
484         {0x4f, 0x2e},           //Brightness 3
485         {0x50, 0x31},
486         {0x51, 0x02},
487         {0x52, 0x0e},
488         {0x53, 0x1e},
489         {0x54, 0x2d},
490
491         {0x11,0x80},
492         {0x14,0x0a},
493         {0x13,0xc7},
494         {0x66,0x05},
495     {0x00, 0x00}
496 };
497 /* Office Colour Temperature : 3500K - 5000K  */
498 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
499 {
500     //Office
501         {0x84, 0x6C},           //Contrast 4
502         {0x85, 0x78},
503         {0x86, 0x8C},
504         {0x87, 0x9E},
505         {0x88, 0xBB},
506         {0x89, 0xD2},
507         {0x8A, 0xE6},
508         {0x6C, 0x40},
509         {0x6D, 0x30},
510         {0x6E, 0x48},
511         {0x6F, 0x60},
512         {0x70, 0x70},
513         {0x71, 0x70},
514         {0x72, 0x70},
515         {0x73, 0x70},
516         {0x74, 0x60},
517         {0x75, 0x60},
518         {0x76, 0x50},
519         {0x77, 0x48},
520         {0x78, 0x3A},
521         {0x79, 0x2E},
522         {0x7A, 0x28},
523         {0x7B, 0x22},
524
525         {0x0f, 0x4a},           //Saturation 3
526         {0x27, 0x80},
527         {0x28, 0x80},
528         {0x2c, 0x80},
529         {0x62, 0x60},
530         {0x63, 0xe0},
531         {0x64, 0x04},
532         {0x65, 0x00},
533         {0x66, 0x01},
534         {0x24, 0x70},
535         {0x25, 0x64},
536
537         {0x4f, 0x2e},           //Brightness 3
538         {0x50, 0x31},
539         {0x51, 0x02},
540         {0x52, 0x0e},
541         {0x53, 0x1e},
542         {0x54, 0x2d},
543
544         {0x11,0x80},
545         {0x14,0x2a},
546         {0x13,0xe7},
547         {0x66,0x05},
548
549     {0x00, 0x00}
550
551
552 };
553 /* Home Colour Temperature : 2500K - 3500K  */
554 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
555 {
556     //Home
557         {0x84, 0x6C},           //Contrast 4
558         {0x85, 0x78},
559         {0x86, 0x8C},
560         {0x87, 0x9E},
561         {0x88, 0xBB},
562         {0x89, 0xD2},
563         {0x8A, 0xE6},
564         {0x6C, 0x40},
565         {0x6D, 0x30},
566         {0x6E, 0x48},
567         {0x6F, 0x60},
568         {0x70, 0x70},
569         {0x71, 0x70},
570         {0x72, 0x70},
571         {0x73, 0x70},
572         {0x74, 0x60},
573         {0x75, 0x60},
574         {0x76, 0x50},
575         {0x77, 0x48},
576         {0x78, 0x3A},
577         {0x79, 0x2E},
578         {0x7A, 0x28},
579         {0x7B, 0x22},
580
581         {0x0f, 0x4a},           //Saturation 3
582         {0x27, 0x80},
583         {0x28, 0x80},
584         {0x2c, 0x80},
585         {0x62, 0x60},
586         {0x63, 0xe0},
587         {0x64, 0x04},
588         {0x65, 0x00},
589         {0x66, 0x01},
590         {0x24, 0x70},
591         {0x25, 0x64},
592
593         {0x4f, 0x2e},           //Brightness 3
594         {0x50, 0x31},
595         {0x51, 0x02},
596         {0x52, 0x0e},
597         {0x53, 0x1e},
598         {0x54, 0x2d},
599
600         {0x11, 0x80},
601         {0x14, 0x2a},
602         {0x13, 0xe7},
603         {0x66, 0x05},
604     {0x00, 0x00}
605 };
606 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
607     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
608 };
609 #endif
610
611 #if CONFIG_SENSOR_Brightness
612 static  struct reginfo sensor_Brightness0[]=
613 {
614     // Brightness -2
615     {0x3301, 0xff},//bit[7]:1, enable SDE
616     {0x3391, 0x04},
617     {0x3390, 0x49},
618     {0x339a, 0x20},
619     {0x0000, 0x00}
620 };
621
622 static  struct reginfo sensor_Brightness1[]=
623 {
624     // Brightness -1
625     {0x3301, 0xff},//bit[7]:1, enable SDE
626     {0x3391, 0x04},
627     {0x3390, 0x49},
628     {0x339a, 0x10},
629     {0x0000, 0x00}
630 };
631
632 static  struct reginfo sensor_Brightness2[]=
633 {
634     //  Brightness 0
635     {0x3301, 0xff},//bit[7]:1, enable SDE
636     {0x3391, 0x00},
637     {0x3390, 0x41},
638     {0x339a, 0x00},
639     {0x0000, 0x00}
640 };
641
642 static  struct reginfo sensor_Brightness3[]=
643 {
644     // Brightness +1
645     {0x3301, 0xff},//bit[7]:1, enable SDE
646     {0x3391, 0x04},
647     {0x3390, 0x41},
648     {0x339a, 0x10},
649     {0x0000, 0x00}
650 };
651
652 static  struct reginfo sensor_Brightness4[]=
653 {
654     //  Brightness +2
655     {0x3301, 0xff},//bit[7]:1, enable SDE
656     {0x3391, 0x04},
657     {0x3390, 0x41},
658     {0x339a, 0x20},
659     {0x0000, 0x00}
660 };
661
662 static  struct reginfo sensor_Brightness5[]=
663 {
664     //  Brightness +3
665     {0x3301, 0xff},//bit[7]:1, enable SDE
666     {0x3391, 0x04}, //bit[2] enable
667     {0x3390, 0x41}, //bit[3] sign of brightness
668     {0x339a, 0x30},
669     {0x0000, 0x00}
670 };
671 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
672     sensor_Brightness4, sensor_Brightness5,NULL,
673 };
674
675 #endif
676
677 #if CONFIG_SENSOR_Effect
678 static  struct reginfo sensor_Effect_Normal[] =
679 {
680         {0x3a,0x0d},
681         {0x67,0x80},
682         {0x68,0x80},
683     {0x00, 0x00}
684 };
685
686 static  struct reginfo sensor_Effect_WandB[] =
687 {
688         {0x3a,0x1d},
689         {0x67,0x80},
690         {0x68,0x80},
691     {0x00, 0x00}
692 };
693
694 static  struct reginfo sensor_Effect_Sepia[] =
695 {
696         {0x3a,0x1d},
697         {0x67,0x40},
698         {0x68,0xa0},
699     {0x00, 0x00}
700 };
701
702 static  struct reginfo sensor_Effect_Negative[] =
703 {
704     //Negative
705         {0x3a,0x2d},
706         {0x67,0x80},
707         {0x68,0x80},
708     {0x00, 0x00}
709 };
710 static  struct reginfo sensor_Effect_Bluish[] =
711 {
712     // Bluish
713         {0x3a,0x1d},
714         {0x67,0xc0},
715         {0x68,0x80},
716     {0x00, 0x00}
717 };
718
719 static  struct reginfo sensor_Effect_Green[] =
720 {
721     //  Greenish
722         {0x3a,0x1d},
723         {0x67,0x40},
724         {0x68,0x40},
725     {0x00, 0x00}
726 };
727 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
728     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
729 };
730 #endif
731 #if CONFIG_SENSOR_Exposure
732 static  struct reginfo sensor_Exposure0[]=
733 {
734     {0x00, 0x00}
735 };
736
737 static  struct reginfo sensor_Exposure1[]=
738 {
739     {0x00, 0x00}
740 };
741
742 static  struct reginfo sensor_Exposure2[]=
743 {
744     {0x00, 0x00}
745 };
746
747 static  struct reginfo sensor_Exposure3[]=
748 {
749     {0x00, 0x00}
750 };
751
752 static  struct reginfo sensor_Exposure4[]=
753 {
754     {0x00, 0x00}
755 };
756
757 static  struct reginfo sensor_Exposure5[]=
758 {
759     {0x00, 0x00}
760 };
761
762 static  struct reginfo sensor_Exposure6[]=
763 {
764     {0x00, 0x00}
765 };
766
767 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
768     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
769 };
770 #endif
771 #if CONFIG_SENSOR_Saturation
772 static  struct reginfo sensor_Saturation0[]=
773 {
774     {0x00, 0x00}
775 };
776
777 static  struct reginfo sensor_Saturation1[]=
778 {
779     {0x00, 0x00}
780 };
781
782 static  struct reginfo sensor_Saturation2[]=
783 {
784     {0x00, 0x00}
785 };
786 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
787
788 #endif
789 #if CONFIG_SENSOR_Contrast
790 static  struct reginfo sensor_Contrast0[]=
791 {
792     {0x00, 0x00}
793 };
794
795 static  struct reginfo sensor_Contrast1[]=
796 {
797     {0x00, 0x00}
798 };
799
800 static  struct reginfo sensor_Contrast2[]=
801 {
802     {0x00, 0x00}
803 };
804
805 static  struct reginfo sensor_Contrast3[]=
806 {
807     {0x00, 0x00}
808 };
809
810 static  struct reginfo sensor_Contrast4[]=
811 {
812     {0x00, 0x00}
813 };
814
815
816 static  struct reginfo sensor_Contrast5[]=
817 {
818     {0x00, 0x00}
819 };
820
821 static  struct reginfo sensor_Contrast6[]=
822 {
823     {0x00, 0x00}
824 };
825 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
826     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
827 };
828
829 #endif
830 #if CONFIG_SENSOR_Mirror
831 static  struct reginfo sensor_MirrorOn[]=
832 {
833     {0x00, 0x00}
834 };
835
836 static  struct reginfo sensor_MirrorOff[]=
837 {
838     {0x00, 0x00}
839 };
840 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
841 #endif
842 #if CONFIG_SENSOR_Flip
843 static  struct reginfo sensor_FlipOn[]=
844 {
845     {0x00, 0x00}
846 };
847
848 static  struct reginfo sensor_FlipOff[]=
849 {
850     {0x00, 0x00}
851 };
852 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
853
854 #endif
855 #if CONFIG_SENSOR_Scene
856 static  struct reginfo sensor_SceneAuto[] =
857 {
858 {0x00, 0x00}
859 };
860
861 static  struct reginfo sensor_SceneNight[] =
862 {
863 {0x00, 0x00}
864 };
865 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
866
867 #endif
868 #if CONFIG_SENSOR_DigitalZoom
869 static struct reginfo sensor_Zoom0[] =
870 {
871     {0x0, 0x0},
872 };
873
874 static struct reginfo sensor_Zoom1[] =
875 {
876      {0x0, 0x0},
877 };
878
879 static struct reginfo sensor_Zoom2[] =
880 {
881     {0x0, 0x0},
882 };
883
884
885 static struct reginfo sensor_Zoom3[] =
886 {
887     {0x0, 0x0},
888 };
889 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
890 #endif
891 static const struct v4l2_querymenu sensor_menus[] =
892 {
893         #if CONFIG_SENSOR_WhiteBalance
894     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
895     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
896     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
897     #endif
898
899         #if CONFIG_SENSOR_Effect
900     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
901     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
902     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
903     #endif
904
905         #if CONFIG_SENSOR_Scene
906     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
907     #endif
908
909         #if CONFIG_SENSOR_Flash
910     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
911     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
912     #endif
913 };
914
915 static const struct v4l2_queryctrl sensor_controls[] =
916 {
917         #if CONFIG_SENSOR_WhiteBalance
918     {
919         .id             = V4L2_CID_DO_WHITE_BALANCE,
920         .type           = V4L2_CTRL_TYPE_MENU,
921         .name           = "White Balance Control",
922         .minimum        = 0,
923         .maximum        = 4,
924         .step           = 1,
925         .default_value = 0,
926     },
927     #endif
928
929         #if CONFIG_SENSOR_Brightness
930         {
931         .id             = V4L2_CID_BRIGHTNESS,
932         .type           = V4L2_CTRL_TYPE_INTEGER,
933         .name           = "Brightness Control",
934         .minimum        = -3,
935         .maximum        = 2,
936         .step           = 1,
937         .default_value = 0,
938     },
939     #endif
940
941         #if CONFIG_SENSOR_Effect
942         {
943         .id             = V4L2_CID_EFFECT,
944         .type           = V4L2_CTRL_TYPE_MENU,
945         .name           = "Effect Control",
946         .minimum        = 0,
947         .maximum        = 5,
948         .step           = 1,
949         .default_value = 0,
950     },
951         #endif
952
953         #if CONFIG_SENSOR_Exposure
954         {
955         .id             = V4L2_CID_EXPOSURE,
956         .type           = V4L2_CTRL_TYPE_INTEGER,
957         .name           = "Exposure Control",
958         .minimum        = 0,
959         .maximum        = 6,
960         .step           = 1,
961         .default_value = 0,
962     },
963         #endif
964
965         #if CONFIG_SENSOR_Saturation
966         {
967         .id             = V4L2_CID_SATURATION,
968         .type           = V4L2_CTRL_TYPE_INTEGER,
969         .name           = "Saturation Control",
970         .minimum        = 0,
971         .maximum        = 2,
972         .step           = 1,
973         .default_value = 0,
974     },
975     #endif
976
977         #if CONFIG_SENSOR_Contrast
978         {
979         .id             = V4L2_CID_CONTRAST,
980         .type           = V4L2_CTRL_TYPE_INTEGER,
981         .name           = "Contrast Control",
982         .minimum        = -3,
983         .maximum        = 3,
984         .step           = 1,
985         .default_value = 0,
986     },
987         #endif
988
989         #if CONFIG_SENSOR_Mirror
990         {
991         .id             = V4L2_CID_HFLIP,
992         .type           = V4L2_CTRL_TYPE_BOOLEAN,
993         .name           = "Mirror Control",
994         .minimum        = 0,
995         .maximum        = 1,
996         .step           = 1,
997         .default_value = 1,
998     },
999     #endif
1000
1001         #if CONFIG_SENSOR_Flip
1002         {
1003         .id             = V4L2_CID_VFLIP,
1004         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1005         .name           = "Flip Control",
1006         .minimum        = 0,
1007         .maximum        = 1,
1008         .step           = 1,
1009         .default_value = 1,
1010     },
1011     #endif
1012
1013         #if CONFIG_SENSOR_Scene
1014     {
1015         .id             = V4L2_CID_SCENE,
1016         .type           = V4L2_CTRL_TYPE_MENU,
1017         .name           = "Scene Control",
1018         .minimum        = 0,
1019         .maximum        = 1,
1020         .step           = 1,
1021         .default_value = 0,
1022     },
1023     #endif
1024
1025         #if CONFIG_SENSOR_DigitalZoom
1026     {
1027         .id             = V4L2_CID_ZOOM_RELATIVE,
1028         .type           = V4L2_CTRL_TYPE_INTEGER,
1029         .name           = "DigitalZoom Control",
1030         .minimum        = -1,
1031         .maximum        = 1,
1032         .step           = 1,
1033         .default_value = 0,
1034     }, {
1035         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1036         .type           = V4L2_CTRL_TYPE_INTEGER,
1037         .name           = "DigitalZoom Control",
1038         .minimum        = 0,
1039         .maximum        = 3,
1040         .step           = 1,
1041         .default_value = 0,
1042     },
1043     #endif
1044
1045         #if CONFIG_SENSOR_Focus
1046         {
1047         .id             = V4L2_CID_FOCUS_RELATIVE,
1048         .type           = V4L2_CTRL_TYPE_INTEGER,
1049         .name           = "Focus Control",
1050         .minimum        = -1,
1051         .maximum        = 1,
1052         .step           = 1,
1053         .default_value = 0,
1054     }, {
1055         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1056         .type           = V4L2_CTRL_TYPE_INTEGER,
1057         .name           = "Focus Control",
1058         .minimum        = 0,
1059         .maximum        = 255,
1060         .step           = 1,
1061         .default_value = 125,
1062     },
1063     #endif
1064
1065         #if CONFIG_SENSOR_Flash
1066         {
1067         .id             = V4L2_CID_FLASH,
1068         .type           = V4L2_CTRL_TYPE_MENU,
1069         .name           = "Flash Control",
1070         .minimum        = 0,
1071         .maximum        = 3,
1072         .step           = 1,
1073         .default_value = 0,
1074     },
1075         #endif
1076 };
1077
1078 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1079 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1080 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1081 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1082 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1083 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1084 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1085 static int sensor_resume(struct soc_camera_device *icd);
1086 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1087 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1088 #if CONFIG_SENSOR_Effect
1089 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1090 #endif
1091 #if CONFIG_SENSOR_WhiteBalance
1092 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1093 #endif
1094 static int sensor_deactivate(struct i2c_client *client);
1095
1096
1097 static struct soc_camera_ops sensor_ops =
1098 {
1099     .suspend                     = sensor_suspend,
1100     .resume                       = sensor_resume,
1101     .set_bus_param              = sensor_set_bus_param,
1102     .query_bus_param    = sensor_query_bus_param,
1103     .controls           = sensor_controls,
1104     .menus                         = sensor_menus,
1105     .num_controls               = ARRAY_SIZE(sensor_controls),
1106     .num_menus          = ARRAY_SIZE(sensor_menus),
1107 };
1108
1109 /* only one fixed colorspace per pixelcode */
1110 struct sensor_datafmt {
1111         enum v4l2_mbus_pixelcode code;
1112         enum v4l2_colorspace colorspace;
1113 };
1114
1115 /* Find a data format by a pixel code in an array */
1116 static const struct sensor_datafmt *sensor_find_datafmt(
1117         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1118         int n)
1119 {
1120         int i;
1121         for (i = 0; i < n; i++)
1122                 if (fmt[i].code == code)
1123                         return fmt + i;
1124
1125         return NULL;
1126 }
1127
1128 static const struct sensor_datafmt sensor_colour_fmts[] = {
1129     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1130     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1131 };
1132
1133 typedef struct sensor_info_priv_s
1134 {
1135     int whiteBalance;
1136     int brightness;
1137     int contrast;
1138     int saturation;
1139     int effect;
1140     int scene;
1141     int digitalzoom;
1142     int focus;
1143     int flash;
1144     int exposure;
1145         bool snap2preview;
1146         bool video2preview;
1147     unsigned char mirror;                                        /* HFLIP */
1148     unsigned char flip;                                          /* VFLIP */
1149     unsigned int winseqe_cur_addr;
1150         struct sensor_datafmt fmt;
1151
1152 } sensor_info_priv_t;
1153
1154 struct sensor
1155 {
1156     struct v4l2_subdev subdev;
1157     struct i2c_client *client;
1158     sensor_info_priv_t info_priv;
1159     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1160 #if CONFIG_SENSOR_I2C_NOSCHED
1161         atomic_t tasklock_cnt;
1162 #endif
1163         struct rkcamera_platform_data *sensor_io_request;
1164     struct rkcamera_gpio_res *sensor_gpio_res;
1165 };
1166
1167 static struct sensor* to_sensor(const struct i2c_client *client)
1168 {
1169     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1170 }
1171
1172 static int sensor_task_lock(struct i2c_client *client, int lock)
1173 {
1174 #if CONFIG_SENSOR_I2C_NOSCHED
1175         int cnt = 3;
1176     struct sensor *sensor = to_sensor(client);
1177
1178         if (lock) {
1179                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1180                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1181                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1182                                 msleep(35);
1183                                 cnt--;
1184                         }
1185                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1186                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1187                                 goto sensor_task_lock_err;
1188                         }
1189                         preempt_disable();
1190                 }
1191
1192                 atomic_add(1, &sensor->tasklock_cnt);
1193         } else {
1194                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1195                         atomic_sub(1, &sensor->tasklock_cnt);
1196
1197                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1198                                 preempt_enable();
1199                 }
1200         }
1201         return 0;
1202 sensor_task_lock_err:
1203         return -1;  
1204 #else
1205     return 0;
1206 #endif
1207
1208 }
1209
1210 /* sensor register write */
1211 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1212 {
1213     int err,cnt;
1214     u8 buf[2];
1215     struct i2c_msg msg[1];
1216
1217     buf[0] = reg & 0xFF;
1218     buf[1] = val;
1219
1220     msg->addr = client->addr;
1221     msg->flags = client->flags;
1222     msg->buf = buf;
1223     msg->len = sizeof(buf);
1224     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
1225     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1226
1227     cnt = 3;
1228     err = -EAGAIN;
1229
1230     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1231         err = i2c_transfer(client->adapter, msg, 1);
1232                                 udelay(50);
1233         if (err >= 0) {
1234             return 0;
1235         } else {
1236                 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1237                 udelay(10);
1238         }
1239     }
1240
1241     return err;
1242 }
1243
1244 /* sensor register read */
1245 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1246 {
1247     int err,cnt;
1248     u8 buf[1];
1249     struct i2c_msg msg[2];
1250    
1251     buf[0] = reg;
1252
1253     msg[0].addr = client->addr;
1254     msg[0].flags = client->flags;
1255     msg[0].buf = buf;
1256     msg[0].len = sizeof(buf);
1257     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1258     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1259
1260     msg[1].addr = client->addr;
1261     msg[1].flags = client->flags|I2C_M_RD;
1262     msg[1].buf = buf;
1263     msg[1].len = 1;
1264     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1265     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1266
1267     cnt = 1;
1268     err = -EAGAIN;
1269     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1270         err = i2c_transfer(client->adapter, msg, 2);
1271
1272         if (err >= 0) {
1273             *val = buf[0];
1274             return 0;
1275         } else {
1276                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1277             udelay(10);
1278         }
1279     }
1280
1281     return err;
1282 }
1283
1284 /* write a array of registers  */
1285 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1286 {
1287     int err = 0, cnt;
1288     int i = 0;
1289 #if CONFIG_SENSOR_I2C_RDWRCHK    
1290         char valchk;
1291 #endif
1292
1293         cnt = 0;
1294         if (sensor_task_lock(client, 1) < 0)
1295                 goto sensor_write_array_end;
1296
1297     while (regarray[i].reg != 0)
1298     {
1299         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1300         if (err < 0)
1301         {
1302             if (cnt-- > 0) {
1303                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1304                                 i = 0;
1305                                 continue;
1306             } else {
1307                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1308                 err = -EPERM;
1309                                 goto sensor_write_array_end;
1310             }
1311         } else {
1312         #if CONFIG_SENSOR_I2C_RDWRCHK
1313                         sensor_read(client, regarray[i].reg, &valchk);
1314                         if (valchk != regarray[i].val)
1315                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1316                 #endif
1317         }
1318         i++;
1319     }
1320
1321 sensor_write_array_end:
1322         sensor_task_lock(client,0);
1323         return err;
1324 }
1325 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1326 {
1327     int cnt;
1328     int i = 0;
1329         char valchk;
1330
1331         cnt = 0;
1332         valchk = 0;
1333     while (regarray[i].reg != 0)
1334     {
1335                 sensor_read(client, regarray[i].reg, &valchk);
1336                 if (valchk != regarray[i].val)
1337                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1338
1339         i++;
1340     }
1341     return 0;
1342 }
1343 static int sensor_ioctrl(struct soc_camera_device *icd,enum rksensor_power_cmd cmd, int on)
1344 {
1345         struct soc_camera_link *icl = to_soc_camera_link(icd);
1346         int ret = 0;
1347
1348     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1349         switch (cmd)
1350         {
1351                 case Sensor_PowerDown:
1352                 {
1353                         if (icl->powerdown) {
1354                                 if (on == 0)
1355                                         mdelay(1);
1356                                 ret = icl->powerdown(icd->pdev, on);
1357                                 if (ret == RK_CAM_IO_SUCCESS) {
1358                                         if (on == 0) {
1359                                                 mdelay(20);
1360                                                 if (icl->reset)
1361                                                         icl->reset(icd->pdev);
1362                                         }
1363                                 } else if (ret == RK_CAM_EIO_REQUESTFAIL) {
1364                                         ret = -ENODEV;
1365                                         goto sensor_power_end;
1366                                 }
1367                         }
1368                         break;
1369                 }
1370                 case Sensor_Flash:
1371                 {
1372                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1373                 struct sensor *sensor = to_sensor(client);
1374
1375                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1376                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1377                         }
1378             break;
1379                 }
1380                 default:
1381                 {
1382                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1383                         break;
1384                 }
1385         }
1386 sensor_power_end:
1387         return ret;
1388 }
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 rkcamera_platform_data* tmp_plat_data =(struct rkcamera_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) { 
1403                 for(i = 0;i < RK_CAM_NUM_PER_HOST;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  && (i < RK_CAM_NUM_PER_HOST) && 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 RK_CAM_SUBDEV_DEACTIVATE:
2623                 {
2624                         sensor_deactivate(client);
2625                         break;
2626                 }
2627
2628                 case RK_CAM_SUBDEV_IOREQUEST:
2629                 {
2630                         sensor->sensor_io_request = (struct rkcamera_platform_data*)arg;           
2631             if (sensor->sensor_io_request != NULL) { 
2632                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
2633                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2634                     sensor->sensor_gpio_res = (struct rkcamera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2635                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
2636                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2637                     sensor->sensor_gpio_res = (struct rkcamera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2638                 }
2639             } else {
2640                 SENSOR_TR("%s %s RK_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2641                 ret = -EINVAL;
2642                 goto sensor_ioctl_end;
2643             }
2644             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2645                for this project */
2646             #if CONFIG_SENSOR_Flash     
2647                 if (sensor->sensor_gpio_res) { 
2648                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2649                     for (i = 0; i < icd->ops->num_controls; i++) {
2650                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2651                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2652                                 }
2653                     }
2654                     sensor->info_priv.flash = 0xff;
2655                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2656                 }
2657                 }
2658             #endif
2659                         break;
2660                 }
2661                 default:
2662                 {
2663                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2664                         break;
2665                 }
2666         }
2667 sensor_ioctl_end:
2668         return ret;
2669
2670 }
2671 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2672                             enum v4l2_mbus_pixelcode *code)
2673 {
2674         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2675                 return -EINVAL;
2676
2677         *code = sensor_colour_fmts[index].code;
2678         return 0;
2679 }
2680 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2681         .init           = sensor_init,
2682         .g_ctrl         = sensor_g_control,
2683         .s_ctrl         = sensor_s_control,
2684         .g_ext_ctrls          = sensor_g_ext_controls,
2685         .s_ext_ctrls          = sensor_s_ext_controls,
2686         .g_chip_ident   = sensor_g_chip_ident,
2687         .ioctl = sensor_ioctl,
2688 };
2689
2690 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2691         .s_mbus_fmt     = sensor_s_fmt,
2692         .g_mbus_fmt     = sensor_g_fmt,
2693         .try_mbus_fmt   = sensor_try_fmt,
2694         .enum_mbus_fmt  = sensor_enum_fmt,
2695 };
2696
2697 static struct v4l2_subdev_ops sensor_subdev_ops = {
2698         .core   = &sensor_subdev_core_ops,
2699         .video = &sensor_subdev_video_ops,
2700 };
2701
2702 static int sensor_probe(struct i2c_client *client,
2703                          const struct i2c_device_id *did)
2704 {
2705     struct sensor *sensor;
2706     struct soc_camera_device *icd = client->dev.platform_data;
2707     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2708     struct soc_camera_link *icl;
2709     int ret;
2710
2711     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2712     if (!icd) {
2713         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2714         return -EINVAL;
2715     }
2716
2717     icl = to_soc_camera_link(icd);
2718     if (!icl) {
2719         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2720         return -EINVAL;
2721     }
2722
2723     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2724         dev_warn(&adapter->dev,
2725                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2726         return -EIO;
2727     }
2728
2729     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2730     if (!sensor)
2731         return -ENOMEM;
2732
2733     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2734
2735     /* Second stage probe - when a capture adapter is there */
2736     icd->ops            = &sensor_ops;
2737     sensor->info_priv.fmt = sensor_colour_fmts[0];
2738         #if CONFIG_SENSOR_I2C_NOSCHED
2739         atomic_set(&sensor->tasklock_cnt,0);
2740         #endif
2741
2742     ret = sensor_video_probe(icd, client);
2743     if (ret < 0) {
2744         icd->ops = NULL;
2745         i2c_set_clientdata(client, NULL);
2746         kfree(sensor);
2747                 sensor = NULL;
2748     }
2749     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2750     return ret;
2751 }
2752
2753 static int sensor_remove(struct i2c_client *client)
2754 {
2755     struct sensor *sensor = to_sensor(client);
2756     struct soc_camera_device *icd = client->dev.platform_data;
2757
2758     icd->ops = NULL;
2759     i2c_set_clientdata(client, NULL);
2760     client->driver = NULL;
2761     kfree(sensor);
2762         sensor = NULL;
2763     return 0;
2764 }
2765
2766 static const struct i2c_device_id sensor_id[] = {
2767         {SENSOR_NAME_STRING(), 0 },
2768         { }
2769 };
2770 MODULE_DEVICE_TABLE(i2c, sensor_id);
2771
2772 static struct i2c_driver sensor_i2c_driver = {
2773         .driver = {
2774                 .name = SENSOR_NAME_STRING(),
2775         },
2776         .probe          = sensor_probe,
2777         .remove         = sensor_remove,
2778         .id_table       = sensor_id,
2779 };
2780
2781 static int __init sensor_mod_init(void)
2782 {
2783     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2784     return i2c_add_driver(&sensor_i2c_driver);
2785 }
2786
2787 static void __exit sensor_mod_exit(void)
2788 {
2789     i2c_del_driver(&sensor_i2c_driver);
2790 }
2791
2792 device_initcall_sync(sensor_mod_init);
2793 module_exit(sensor_mod_exit);
2794
2795 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2796 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2797 MODULE_LICENSE("GPL");
2798
2799