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