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