fb:support 180 degree rotate
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / hi253.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 #include <mach/rk29_camera.h>
23
24 static int debug;
25 module_param(debug, int, S_IRUGO|S_IWUSR);
26
27 #define dprintk(level, fmt, arg...) do {                        \
28         if (debug >= level)                                     \
29         printk(KERN_WARNING fmt , ## arg); } while (0)
30
31 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
32 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
33
34
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
37
38 #define __STR(x) #x
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
41
42 #define MIN(x,y)   ((x<y) ? x: y)
43 #define MAX(x,y)    ((x>y) ? x: y)
44
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK29_CAM_SENSOR_HI253
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_HI253
48 #define SENSOR_ID 0x92
49 #define SENSOR_MIN_WIDTH    176
50 #define SENSOR_MIN_HEIGHT   144
51 #define SENSOR_MAX_WIDTH    1600
52 #define SENSOR_MAX_HEIGHT   1200
53 #define SENSOR_INIT_WIDTH       1600                    /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT  1200
55 #define SENSOR_INIT_WINSEQADR sensor_uxga
56 #define SENSOR_INIT_PIXFMT V4L2_PIX_FMT_UYVY
57
58 #define CONFIG_SENSOR_WhiteBalance      1
59 #define CONFIG_SENSOR_Brightness        0
60 #define CONFIG_SENSOR_Contrast      0
61 #define CONFIG_SENSOR_Saturation    0
62 #define CONFIG_SENSOR_Effect        1
63 #define CONFIG_SENSOR_Scene         1
64 #define CONFIG_SENSOR_DigitalZoom   0
65 #define CONFIG_SENSOR_Focus         0
66 #define CONFIG_SENSOR_Exposure      0
67 #define CONFIG_SENSOR_Flash         0
68 #define CONFIG_SENSOR_Mirror        0
69 #define CONFIG_SENSOR_Flip          0
70
71 #define CONFIG_SENSOR_I2C_SPEED     100000       /* Hz */
72 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
73 #define CONFIG_SENSOR_I2C_NOSCHED   0
74 #define CONFIG_SENSOR_I2C_RDWRCHK   0
75
76 #define SENSOR_BUS_PARAM  (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING|\
77                           SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
78                           SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ)
79
80 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
81 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
82 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
83 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
84 #define COLOR_TEMPERATURE_OFFICE_DN     3500
85 #define COLOR_TEMPERATURE_OFFICE_UP     5000
86 #define COLOR_TEMPERATURE_HOME_DN       2500
87 #define COLOR_TEMPERATURE_HOME_UP       3500
88
89 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
90 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
91
92
93 #define END_REG 0xff
94
95 struct reginfo
96 {
97     u8 reg;
98     u8 val;
99 };
100
101 /* init SVGA preview */
102 static struct reginfo sensor_init_data[] =
103 {
104 //      {0x01, 0xf9}, 
105         {0x08, 0x0f}, 
106         {0x01, 0xf8}, 
107
108         {0x03, 0x00}, 
109         {0x03, 0x00},
110         {0x03, 0x00},
111         {0x03, 0x00},
112         {0x03, 0x00},
113         {0x03, 0x00},
114         {0x03, 0x00},
115         {0x03, 0x00},
116         {0x03, 0x00},
117         {0x03, 0x00}, 
118
119         {0x0e, 0x00}, 
120
121         {0x03, 0x00}, 
122         {0x03, 0x00},
123         {0x03, 0x00},
124         {0x03, 0x00},
125         {0x03, 0x00},
126         {0x03, 0x00},
127         {0x03, 0x00},
128         {0x03, 0x00},
129         {0x03, 0x00},
130         {0x03, 0x00}, 
131
132         {0x0e, 0x00}, 
133         {0x01, 0xf1}, 
134         {0x08, 0x00}, 
135         {0x01, 0xf3},
136         {0x01, 0xf1},
137
138         {0x03, 0x20}, 
139         {0x10, 0x0c}, 
140         {0x03, 0x22}, 
141         {0x10, 0x69}, 
142         
143         //Page 00
144         {0x03, 0x00}, 
145         {0x10, 0x00}, //lxh
146         {0x11, 0x90}, 
147         {0x12, 0x04}, 
148         {0x0b, 0xaa}, 
149         {0x0c, 0xaa}, 
150         {0x0d, 0xaa}, 
151         {0x20, 0x00}, 
152         {0x21, 0x0a}, //lxh
153         {0x22, 0x00}, 
154         {0x23, 0x0a}, //lxh
155         {0x24, 0x04}, 
156         {0x25, 0xb0}, 
157         {0x26, 0x06}, 
158         {0x27, 0x40}, 
159
160         {0x40, 0x01}, 
161         {0x41, 0x98},//lxh 
162         {0x42, 0x00}, 
163         {0x43, 0x14},//lxh 
164
165         {0x45, 0x04}, 
166         {0x46, 0x18}, 
167         {0x47, 0xd8}, 
168
169         {0xe1, 0x0f},
170
171         //BLC
172         {0x80, 0x2e}, 
173         {0x81, 0x7e},
174         {0x82, 0x90},
175         {0x83, 0x00},
176         {0x84, 0x0c},
177         {0x85, 0x00},
178         {0x90, 0x0c}, 
179         {0x91, 0x0c}, 
180         {0x92, 0x78}, 
181         {0x93, 0x70}, 
182         {0x94, 0x75}, 
183         {0x95, 0x70}, 
184         {0x96, 0xdc},
185         {0x97, 0xfe},
186         {0x98, 0x20},
187
188         //OutDoor BLC
189         {0x99,0x42}, 
190         {0x9a,0x42}, 
191         {0x9b,0x42}, 
192         {0x9c,0x42}, 
193
194         //Dark BLC
195         {0xa0, 0x00},
196         {0xa2, 0x00},
197         {0xa4, 0x00},
198         {0xa6, 0x00},
199
200         //Normal BLC
201         {0xa8, 0x43},
202         {0xaa, 0x43},
203         {0xac, 0x43},
204         {0xae, 0x43},
205
206         //Page 02
207         {0x03, 0x02}, 
208         {0x12, 0x03},
209         {0x13, 0x03},
210         {0x16, 0x00},
211         {0x17, 0x8C},
212         {0x18, 0x4c}, 
213         {0x19, 0x00}, 
214         {0x1a, 0x39},
215         {0x1c, 0x09},
216         {0x1d, 0x40},
217         {0x1e, 0x30},
218         {0x1f, 0x10},
219         {0x20, 0x77},
220         {0x21, 0xde}, 
221         {0x22, 0xa7},
222         {0x23, 0x30},
223         {0x27, 0x3c},
224         {0x2b, 0x80},
225         {0x2e, 0x00},
226         {0x2f, 0x00},
227         {0x30, 0x05},
228         {0x50, 0x20},
229         {0x52, 0x01},
230         {0x53, 0xc1},
231         {0x55, 0x1c},
232         {0x56, 0x11},
233         {0x5d, 0xA2},
234         {0x5e, 0x5a},
235         {0x60, 0x87},
236         {0x61, 0x99},
237         {0x62, 0x88},
238         {0x63, 0x97},
239         {0x64, 0x88},
240         {0x65, 0x97},
241         {0x67, 0x0c},
242         {0x68, 0x0c},
243         {0x69, 0x0c},
244         {0x72, 0x89},
245         {0x73, 0x96}, 
246         {0x74, 0x89},
247         {0x75, 0x96}, 
248         {0x76, 0x89},
249         {0x77, 0x96}, 
250         {0x7C, 0x85},
251         {0x7d, 0xaf},
252         {0x80, 0x01},
253         {0x81, 0x7f}, 
254         {0x82, 0x13}, 
255         {0x83, 0x24}, 
256         {0x84, 0x7d},
257         {0x85, 0x81},
258         {0x86, 0x7d},
259         {0x87, 0x81},
260         {0x92, 0x48}, 
261         {0x93, 0x54}, 
262         {0x94, 0x7d},
263         {0x95, 0x81},
264         {0x96, 0x7d},
265         {0x97, 0x81},
266         {0xa0, 0x02},
267         {0xa1, 0x7b},
268         {0xa2, 0x02},
269         {0xa3, 0x7b},
270         {0xa4, 0x7b},
271         {0xa5, 0x02},
272         {0xa6, 0x7b},
273         {0xa7, 0x02},
274         {0xa8, 0x85},
275         {0xa9, 0x8c},
276         {0xaa, 0x85},
277         {0xab, 0x8c},
278         {0xac, 0x10},
279         {0xad, 0x16},
280         {0xae, 0x10},
281         {0xaf, 0x16},
282         {0xb0, 0x99},
283         {0xb1, 0xa3},
284         {0xb2, 0xa4},
285         {0xb3, 0xae},
286         {0xb4, 0x9b},
287         {0xb5, 0xa2},
288         {0xb6, 0xa6},
289         {0xb7, 0xac},
290         {0xb8, 0x9b},
291         {0xb9, 0x9f},
292         {0xba, 0xa6},
293         {0xbb, 0xaa},
294         {0xbc, 0x9b},
295         {0xbd, 0x9f},
296         {0xbe, 0xa6},
297         {0xbf, 0xaa},
298         {0xc4, 0x2c},
299         {0xc5, 0x43},
300         {0xc6, 0x63},
301         {0xc7, 0x79},
302         {0xc8, 0x2d},
303         {0xc9, 0x42},
304         {0xca, 0x2d},
305         {0xcb, 0x42},
306         {0xcc, 0x64},
307         {0xcd, 0x78},
308         {0xce, 0x64},
309         {0xcf, 0x78},
310         {0xd0, 0x0a},
311         {0xd1, 0x09},
312         {0xd4, 0x0c},
313         {0xd5, 0x0c},
314         {0xd6, 0xd8},
315         {0xd7, 0xd0},//lxh
316         {0xe0, 0xc4},
317         {0xe1, 0xc4},
318         {0xe2, 0xc4},
319         {0xe3, 0xc4},
320         {0xe4, 0x00},
321         {0xe8, 0x80}, 
322         {0xe9, 0x40},
323         {0xea, 0x7f}, 
324         {0xf0, 0x01}, 
325         {0xf1, 0x01}, 
326         {0xf2, 0x01}, 
327         {0xf3, 0x01}, 
328         {0xf4, 0x01}, 
329
330         //PAGE10
331         {0x03, 0x10},
332         {0x10, 0x01}, //lxh
333     {0x11, 0x03}, //lxh,normal  
334         {0x12, 0x00},
335         {0x13, 0x00},
336         {0x20, 0x00}, 
337
338         {0x40, 0x80},
339         {0x41, 0x00},
340         {0x48, 0x88},// 84
341         {0x50, 0x90},
342         {0x30, 0x00},
343         {0x31, 0x00},
344         {0x32, 0x00},
345         {0x33, 0x00},
346
347         {0x34, 0x30},
348         {0x35, 0x00},
349         {0x36, 0x00},
350         {0x38, 0x00},
351         {0x3e, 0x58},
352         {0x3f, 0x00},
353
354         //Saturation
355         {0x60, 0x6f},
356         {0x61, 0x95},// 74
357         {0x62, 0x95},// 76
358         {0x63, 0x30},
359         {0x64, 0x41},
360
361         {0x66, 0x33},
362         {0x67, 0x00},
363
364         {0x6a, 0x90}, 
365         {0x6b, 0x80}, 
366         {0x6c, 0x80}, 
367         {0x6d, 0xa0}, 
368
369         {0x76, 0x01}, 
370         {0x74, 0x66},
371         {0x79, 0x06},
372         
373         //Page 11
374         {0x03, 0x11}, 
375         {0x10, 0x7f},//lxh,3f 
376         {0x11, 0x40},
377         {0x12, 0xba},
378         {0x13, 0xcb},
379         {0x26, 0x20}, 
380         {0x27, 0x22}, 
381         {0x28, 0x0f}, 
382         {0x29, 0x10}, 
383         {0x2b, 0x30}, 
384         {0x2c, 0x32}, 
385
386         //Out2 D-LPF th
387         {0x30, 0x70}, 
388         {0x31, 0x10}, 
389         {0x32, 0x65}, 
390         {0x33, 0x09}, 
391         {0x34, 0x06}, 
392         {0x35, 0x04}, 
393
394         //Out1 D-LPF th
395         {0x36, 0x70}, 
396         {0x37, 0x18}, 
397         {0x38, 0x65}, 
398         {0x39, 0x09}, 
399         {0x3a, 0x06}, 
400         {0x3b, 0x04}, 
401
402         //Indoor D-LPF th
403         {0x3c, 0x80}, 
404         {0x3d, 0x18}, 
405         {0x3e, 0x80}, 
406         {0x3f, 0x0c}, 
407         {0x40, 0x09}, 
408         {0x41, 0x06}, 
409
410         {0x42, 0x80}, 
411         {0x43, 0x18}, 
412         {0x44, 0x80}, 
413         {0x45, 0x12}, 
414         {0x46, 0x10}, 
415         {0x47, 0x10}, 
416         {0x48, 0x90}, 
417         {0x49, 0x40}, 
418         {0x4a, 0x80}, 
419         {0x4b, 0x13}, 
420         {0x4c, 0x10}, 
421         {0x4d, 0x11}, 
422         {0x4e, 0x80}, 
423         {0x4f, 0x30}, 
424         {0x50, 0x80}, 
425         {0x51, 0x13}, 
426         {0x52, 0x10}, 
427         {0x53, 0x13}, 
428         {0x54, 0x11},
429         {0x55, 0x17},
430         {0x56, 0x20},
431         {0x57, 0x20},
432         {0x58, 0x20},
433         {0x59, 0x30},
434         {0x5a, 0x18},
435         {0x5b, 0x00},
436         {0x5c, 0x00},
437         {0x60, 0x3f},
438         {0x62, 0x50},
439         {0x70, 0x06},
440         
441         //Page 12
442         {0x03, 0x12}, 
443         {0x20, 0x0f},
444         {0x21, 0x0f},
445         {0x25, 0x30},
446         {0x28, 0x00}, 
447         {0x29, 0x00}, 
448         {0x2a, 0x00},
449         {0x30, 0x50},
450         {0x31, 0x18}, 
451         {0x32, 0x32}, 
452         {0x33, 0x40}, 
453         {0x34, 0x50}, 
454         {0x35, 0x70}, 
455         {0x36, 0xa0}, 
456
457         //Out2 th
458         {0x40, 0xa0}, 
459         {0x41, 0x40}, 
460         {0x42, 0xa0}, 
461         {0x43, 0x90}, 
462         {0x44, 0x90}, 
463         {0x45, 0x80}, 
464
465         //Out1 th
466         {0x46, 0xb0}, 
467         {0x47, 0x55}, 
468         {0x48, 0xa0}, 
469         {0x49, 0x90}, 
470         {0x4a, 0x90}, 
471         {0x4b, 0x80}, 
472
473         //In door th
474         {0x4c, 0xb0},
475         {0x4d, 0x40},
476         {0x4e, 0x90},
477         {0x4f, 0x90},
478         {0x50, 0xe6},
479         {0x51, 0x80},
480
481         //Dark1 th
482         {0x52, 0xb0},
483         {0x53, 0x60},
484         {0x54, 0xc0},
485         {0x55, 0xc0},
486         {0x56, 0xc0},
487         {0x57, 0x80},
488
489         //Dark2 th
490         {0x58, 0x90},
491         {0x59, 0x40},
492         {0x5a, 0xd0},
493         {0x5b, 0xd0},
494         {0x5c, 0xe0},
495         {0x5d, 0x80},
496
497         //Dark3 th
498         {0x5e, 0x88},
499         {0x5f, 0x40},
500         {0x60, 0xe0},
501         {0x61, 0xe6},
502         {0x62, 0xe6},
503         {0x63, 0x80},
504
505         {0x70, 0x15},
506         {0x71, 0x01},
507
508         {0x72, 0x18},
509         {0x73, 0x01},
510
511         {0x74, 0x25},
512         {0x75, 0x15},
513         {0x80, 0x30},
514         {0x81, 0x50},
515         {0x82, 0x80},
516         {0x85, 0x1a},
517         {0x88, 0x00},
518         {0x89, 0x00},
519         {0x90, 0x5d},
520
521         {0xc5, 0x30},
522         {0xc6, 0x2a},
523
524         {0xD0, 0x0c},
525         {0xD1, 0x80},
526         {0xD2, 0x67},
527         {0xD3, 0x00},
528         {0xD4, 0x00},
529         {0xD5, 0x02},
530         {0xD6, 0xff},
531         {0xD7, 0x18},
532         {0x3b, 0x06},
533         {0x3c, 0x06},
534
535         {0xc5, 0x30},
536         {0xc6, 0x2a},
537
538         //Page 13
539         {0x03, 0x13},
540         {0x10, 0xcb},
541         {0x11, 0x7b},
542         {0x12, 0x07},
543         {0x14, 0x00},
544
545         {0x20, 0x15},
546         {0x21, 0x13},
547         {0x22, 0x33},
548         {0x23, 0x04},
549         {0x24, 0x09},
550         {0x25, 0x08},
551         {0x26, 0x18},
552         {0x27, 0x30},
553         {0x29, 0x12},
554         {0x2a, 0x50},
555
556         //Low clip th
557         {0x2b, 0x06},
558         {0x2c, 0x06},
559         {0x25, 0x08},
560         {0x2d, 0x0c},
561         {0x2e, 0x12},
562         {0x2f, 0x12},
563
564         //Out2 Edge
565         {0x50, 0x10},
566         {0x51, 0x14},
567         {0x52, 0x10},
568         {0x53, 0x0c},
569         {0x54, 0x0f},
570         {0x55, 0x0c},
571
572         //Out1 Edge
573         {0x56, 0x10},
574         {0x57, 0x13},
575         {0x58, 0x10},
576         {0x59, 0x0c},
577         {0x5a, 0x0f},
578         {0x5b, 0x0c},
579
580         //Indoor Edge
581         {0x5c, 0x0a},
582         {0x5d, 0x0b},
583         {0x5e, 0x0a},
584         {0x5f, 0x08},
585         {0x60, 0x09},
586         {0x61, 0x08},
587
588         //Dark1 Edge
589         {0x62, 0x08},
590         {0x63, 0x08},
591         {0x64, 0x08},
592         {0x65, 0x06},
593         {0x66, 0x06},
594         {0x67, 0x06},
595
596         //Dark2 Edge
597         {0x68, 0x07},
598         {0x69, 0x07},
599         {0x6a, 0x07},
600         {0x6b, 0x05},
601         {0x6c, 0x05},
602         {0x6d, 0x05},
603
604         //Dark3 Edge
605         {0x6e, 0x07},
606         {0x6f, 0x07},
607         {0x70, 0x07},
608         {0x71, 0x05},
609         {0x72, 0x05},
610         {0x73, 0x05},
611
612         //2DY
613         {0x80, 0xfd},
614         {0x81, 0x1f},
615         {0x82, 0x05},
616         {0x83, 0x01},
617
618         {0x90, 0x15},
619         {0x91, 0x15},
620         {0x92, 0x33},
621         {0x93, 0x30},
622         {0x94, 0x03},
623         {0x95, 0x14},
624         {0x97, 0x30},
625         {0x99, 0x30},
626
627         {0xa0, 0x04},
628         {0xa1, 0x05},
629         {0xa2, 0x04},
630         {0xa3, 0x05},
631         {0xa4, 0x07},
632         {0xa5, 0x08},
633         {0xa6, 0x07},
634         {0xa7, 0x08},
635         {0xa8, 0x07},
636         {0xa9, 0x08},
637         {0xaa, 0x07},
638         {0xab, 0x08}, 
639
640         //Out2 
641         {0xb0, 0x22},
642         {0xb1, 0x2a},
643         {0xb2, 0x28},
644         {0xb3, 0x22},
645         {0xb4, 0x2a},
646         {0xb5, 0x28},
647
648         //Out1 
649         {0xb6, 0x22},
650         {0xb7, 0x2a},
651         {0xb8, 0x28},
652         {0xb9, 0x22},
653         {0xba, 0x2a},
654         {0xbb, 0x28},
655
656         {0xbc, 0x17},
657         {0xbd, 0x17},
658         {0xbe, 0x17},
659         {0xbf, 0x17},
660         {0xc0, 0x17},
661         {0xc1, 0x17},
662
663         //Dark1
664         {0xc2, 0x1e},
665         {0xc3, 0x12},
666         {0xc4, 0x10},
667         {0xc5, 0x1e},
668         {0xc6, 0x12},
669         {0xc7, 0x10},
670
671         //Dark2
672         {0xc8, 0x18},
673         {0xc9, 0x05},
674         {0xca, 0x05},
675         {0xcb, 0x18},
676         {0xcc, 0x05},
677         {0xcd, 0x05},
678
679         //Dark3 
680         {0xce, 0x18},
681         {0xcf, 0x05},
682         {0xd0, 0x05},
683         {0xd1, 0x18},
684         {0xd2, 0x05},
685         {0xd3, 0x05},
686         
687         //Page 14
688         {0x03, 0x14},
689         {0x10, 0x11},
690         {0x20, 0x40},
691         {0x21, 0x80},
692         {0x23, 0x80},
693         {0x22, 0x80},
694         {0x23, 0x80},
695         {0x24, 0x80},
696
697         {0x30, 0xc8},
698         {0x31, 0x2b},
699         {0x32, 0x00},
700         {0x33, 0x00},
701         {0x34, 0x90},
702
703         {0x40, 0x42},
704         {0x50, 0x2d},
705         {0x60, 0x28},
706         {0x70, 0x2d},
707
708         //Page 15
709         {0x03, 0x15}, 
710         {0x10, 0x0f}, 
711         {0x14, 0x52}, 
712         {0x15, 0x42}, 
713         {0x16, 0x32}, 
714         {0x17, 0x2f}, 
715
716         //CMC
717         {0x30, 0x8f}, 
718         {0x31, 0x59}, 
719         {0x32, 0x0a}, 
720         {0x33, 0x15}, 
721         {0x34, 0x5b}, 
722         {0x35, 0x06}, 
723         {0x36, 0x07}, 
724         {0x37, 0x40}, 
725         {0x38, 0x86}, 
726
727         //CMC OFS
728         {0x40, 0x95}, 
729         {0x41, 0x1f}, 
730         {0x42, 0x8a}, 
731         {0x43, 0x86}, 
732         {0x44, 0x0a}, 
733         {0x45, 0x84}, 
734         {0x46, 0x87}, 
735         {0x47, 0x9b}, 
736         {0x48, 0x23}, 
737
738         //CMC POFS
739         {0x50, 0x8c}, 
740         {0x51, 0x0c}, 
741         {0x52, 0x00}, 
742         {0x53, 0x07}, 
743         {0x54, 0x17}, 
744         {0x55, 0x9d}, 
745         {0x56, 0x00}, 
746         {0x57, 0x0b}, 
747         {0x58, 0x89}, 
748
749         {0x80, 0x03},
750         {0x85, 0x40},
751         {0x87, 0x02},
752         {0x88, 0x00},
753         {0x89, 0x00},
754         {0x8a, 0x00},
755
756         {0x03, 0x16}, 
757         {0x10, 0x31}, 
758         {0x18, 0x37},
759         {0x19, 0x36},
760         {0x1a, 0x0e},
761         {0x1b, 0x01},
762         {0x1c, 0xdc},
763         {0x1d, 0xfe},
764 /* original
765         {0x30, 0x00}, 
766         {0x31, 0x06}, 
767         {0x32, 0x1d}, 
768         {0x33, 0x33}, 
769         {0x34, 0x53}, 
770         {0x35, 0x6c}, 
771         {0x36, 0x81}, 
772         {0x37, 0x94}, 
773         {0x38, 0xa4}, 
774         {0x39, 0xb3}, 
775         {0x3a, 0xc0}, 
776         {0x3b, 0xcb}, 
777         {0x3c, 0xd5}, 
778         {0x3d, 0xde}, 
779         {0x3e, 0xe6}, 
780         {0x3f, 0xee}, 
781         {0x40, 0xf5}, 
782         {0x41, 0xfc}, 
783         {0x42, 0xff}, 
784
785         {0x50, 0x00}, 
786         {0x51, 0x03}, 
787         {0x52, 0x19}, 
788         {0x53, 0x34}, 
789         {0x54, 0x58}, 
790         {0x55, 0x75}, 
791         {0x56, 0x8d}, 
792         {0x57, 0xa1}, 
793         {0x58, 0xb2}, 
794         {0x59, 0xbe}, 
795         {0x5a, 0xc9}, 
796         {0x5b, 0xd2}, 
797         {0x5c, 0xdb}, 
798         {0x5d, 0xe3}, 
799         {0x5e, 0xeb}, 
800         {0x5f, 0xf0}, 
801         {0x60, 0xf5}, 
802         {0x61, 0xf7}, 
803         {0x62, 0xf8}, 
804
805         {0x70, 0x00}, 
806         {0x71, 0x08}, 
807         {0x72, 0x17}, 
808         {0x73, 0x2f}, 
809         {0x74, 0x53}, 
810         {0x75, 0x6c}, 
811         {0x76, 0x81}, 
812         {0x77, 0x94}, 
813         {0x78, 0xa4}, 
814         {0x79, 0xb3}, 
815         {0x7a, 0xc0}, 
816         {0x7b, 0xcb}, 
817         {0x7c, 0xd5}, 
818         {0x7d, 0xde}, 
819         {0x7e, 0xe6}, 
820         {0x7f, 0xee}, 
821         {0x80, 0xf4}, 
822         {0x81, 0xfa}, 
823         {0x82, 0xff}, 
824 */    
825         {0x30, 0x00}, 
826         {0x31, 0x08}, 
827         {0x32, 0x1f}, 
828         {0x33, 0x35}, 
829         {0x34, 0x55}, 
830         {0x35, 0x6e}, 
831         {0x36, 0x83}, 
832         {0x37, 0x96}, 
833         {0x38, 0xa6}, 
834         {0x39, 0xb5}, 
835         {0x3a, 0xc2}, 
836         {0x3b, 0xcd}, 
837         {0x3c, 0xd7}, 
838         {0x3d, 0xe0}, 
839         {0x3e, 0xe8}, 
840         {0x3f, 0xf0}, 
841         {0x40, 0xf7}, 
842         {0x41, 0xfe}, 
843         {0x42, 0xff}, 
844
845         {0x50, 0x00}, 
846         {0x51, 0x05}, 
847         {0x52, 0x1b}, 
848         {0x53, 0x36}, 
849         {0x54, 0x5a}, 
850         {0x55, 0x77}, 
851         {0x56, 0x8f}, 
852         {0x57, 0xa3}, 
853         {0x58, 0xb4}, 
854         {0x59, 0xc0}, 
855         {0x5a, 0xcb}, 
856         {0x5b, 0xd4}, 
857         {0x5c, 0xde}, 
858         {0x5d, 0xe5}, 
859         {0x5e, 0xed}, 
860         {0x5f, 0xf2}, 
861         {0x60, 0xf7}, 
862         {0x61, 0xf9}, 
863         {0x62, 0xfa}, 
864
865         {0x70, 0x00}, 
866         {0x71, 0x0a}, 
867         {0x72, 0x19}, 
868         {0x73, 0x31}, 
869         {0x74, 0x55}, 
870         {0x75, 0x6e}, 
871         {0x76, 0x83}, 
872         {0x77, 0x96}, 
873         {0x78, 0xa6}, 
874         {0x79, 0xb5}, 
875         {0x7a, 0xc2}, 
876         {0x7b, 0xcd}, 
877         {0x7c, 0xd7}, 
878         {0x7d, 0xe0}, 
879         {0x7e, 0xe8}, 
880         {0x7f, 0xf0}, 
881         {0x80, 0xf6}, 
882         {0x81, 0xfc}, 
883         {0x82, 0xff}, 
884
885         
886         {0x03, 0x17}, 
887         {0xc4, 0x6e}, 
888         {0xc5, 0x5c}, 
889
890         {0x03, 0x20}, 
891         {0x10, 0x1c},
892         {0x18, 0x38},
893         {0x20, 0x01}, 
894         {0x21, 0x30},
895         {0x22, 0x10},
896         {0x23, 0x00},
897         {0x24, 0x04},
898
899         {0x28, 0xff},
900         {0x29, 0xad},
901
902         {0x2a, 0xf0},
903         {0x2b, 0x34},
904         {0x30, 0x78},
905         {0x2c, 0xc3},
906         {0x2d, 0x5f},
907         {0x2e, 0x33},
908         //{0x30, 0xf8},
909         {0x32, 0x03},
910         {0x33, 0x2e},
911         {0x34, 0x30},
912         {0x35, 0xd4},
913         {0x36, 0xfe},
914         {0x37, 0x32},
915         {0x38, 0x04},
916         {0x47, 0xf0},
917
918         //Y_Frame TH
919         {0x50, 0x45},
920         {0x51, 0x88},
921
922         {0x56, 0x10},
923         {0x57, 0xb7},
924         {0x58, 0x14},
925         {0x59, 0x88},
926         {0x5a, 0x04},
927
928         {0x60, 0x55}, 
929         {0x61, 0x55}, 
930         {0x62, 0x6a}, 
931         {0x63, 0xa9}, 
932         {0x64, 0x6a}, 
933         {0x65, 0xa9}, 
934         {0x66, 0x6a}, 
935         {0x67, 0xa9}, 
936         {0x68, 0x6b}, 
937         {0x69, 0xe9}, 
938         {0x6a, 0x6a}, 
939         {0x6b, 0xa9}, 
940         {0x6c, 0x6a}, 
941         {0x6d, 0xa9}, 
942         {0x6e, 0x55}, 
943         {0x6f, 0x55}, 
944         {0x70, 0x42}, 
945         {0x71, 0xBb},
946
947         // haunting control
948         {0x76, 0x21},
949         {0x77, 0x02},
950         {0x78, 0x22},
951         {0x79, 0x2a},
952
953         {0x78, 0x24},
954         {0x79, 0x23},
955         {0x7a, 0x23},
956         {0x7b, 0x22},
957         {0x7d, 0x23},
958         {0x83, 0x01},
959         {0x84, 0x5f},
960         {0x85, 0x6c},
961         {0x86, 0x02},
962         {0x87, 0x00},
963         {0x88, 0x05},
964         {0x89, 0x7c},
965         {0x8a, 0x00},
966         {0x8B, 0x75},
967         {0x8C, 0x00},
968         {0x8D, 0x61},
969         {0x8E, 0x00},
970
971         {0x98, 0xdc},
972         {0x99, 0x45},
973         {0x9a, 0x0d},
974         {0x9b, 0xde},
975         {0x9c, 0x08},
976         {0x9d, 0x0a},
977         {0x9e, 0x01},
978     {0x10, 0x9c},
979     {0x18, 0x30},
980     {0x90, 0x0c},
981     {0x91, 0x0c},
982     {0x92, 0xd8},   
983     {0x93, 0xd0},  
984     
985         {0x9f, 0x26}, 
986         {0xa0, 0x03},
987         {0xa1, 0xa9},
988         {0xa2, 0x80},
989         {0xb0, 0x1d},
990         {0xb1, 0x1a},
991         {0xb2, 0x60},
992         {0xb3, 0x1a},
993         {0xb4, 0x1a},
994         {0xb5, 0x44},
995         {0xb6, 0x2f},
996         {0xb7, 0x28},
997         {0xb8, 0x25},
998         {0xb9, 0x22},
999         {0xba, 0x21},
1000         {0xbb, 0x20},
1001         {0xbc, 0x1f},
1002         {0xbd, 0x1f},
1003         {0xc0, 0x30},
1004         {0xc1, 0x20},
1005         {0xc2, 0x20},
1006         {0xc3, 0x20},
1007         {0xc4, 0x08},
1008         {0xc8, 0x60},
1009         {0xc9, 0x40},
1010         
1011         //Page 22
1012         {0x03, 0x22},
1013         {0x10, 0x69},//lxh
1014         {0x11, 0x2c},
1015         {0x19, 0x01},
1016         {0x20, 0x30},
1017         {0x21, 0x80},
1018         {0x23, 0x08},
1019         {0x24, 0x01},
1020
1021         {0x30, 0x80},
1022         {0x31, 0x80},
1023         {0x38, 0x11},
1024         {0x39, 0x34},
1025         {0x40, 0xf7},
1026
1027         {0x41, 0x77},
1028         {0x42, 0x55},
1029         {0x43, 0xf0},
1030         {0x44, 0x43}, 
1031         {0x45, 0x33},
1032         {0x46, 0x00}, 
1033
1034         {0x47, 0x94},
1035
1036         {0x50, 0xb2},
1037         {0x51, 0x81},
1038         {0x52, 0x98},
1039
1040         {0x80, 0x3d},//lxh 
1041         {0x81, 0x20}, 
1042         {0x82, 0x32},//lxh 
1043
1044         {0x83, 0x50}, 
1045         {0x84, 0x20}, 
1046         {0x85, 0x50}, 
1047         {0x86, 0x20}, 
1048
1049         {0x87, 0x54}, 
1050         {0x88, 0x20}, 
1051         {0x89, 0x45}, 
1052         {0x8a, 0x2a}, 
1053
1054         {0x8b, 0x46}, 
1055         {0x8c, 0x3f}, 
1056         {0x8d, 0x34}, 
1057         {0x8e, 0x2c}, 
1058
1059         {0x8f, 0x60}, 
1060         {0x90, 0x5f}, 
1061         {0x91, 0x5c}, 
1062         {0x92, 0x4C}, 
1063         {0x93, 0x41}, 
1064         {0x94, 0x3b}, 
1065         {0x95, 0x36}, 
1066         {0x96, 0x30}, 
1067         {0x97, 0x27}, 
1068         {0x98, 0x20}, 
1069         {0x99, 0x1C}, 
1070         {0x9a, 0x19}, 
1071
1072         {0x9b, 0x88}, 
1073         {0x9c, 0x88}, 
1074         {0x9d, 0x48}, 
1075         {0x9e, 0x38}, 
1076         {0x9f, 0x30}, 
1077
1078         {0xa0, 0x74}, 
1079         {0xa1, 0x35}, 
1080         {0xa2, 0xaf}, 
1081         {0xa3, 0xf7}, 
1082
1083         {0xa4, 0x10}, 
1084         {0xa5, 0x50}, 
1085         {0xa6, 0xc4}, 
1086
1087         {0xad, 0x40},
1088         {0xae, 0x4a},
1089
1090         {0xaf, 0x2a},
1091         {0xb0, 0x29},
1092
1093         {0xb1, 0x20},
1094         {0xb4, 0xff},
1095         {0xb8, 0x6b},
1096         {0xb9, 0x00},
1097
1098         {0x03, 0x24}, 
1099         {0x10, 0x01}, 
1100         {0x18, 0x06},
1101         {0x30, 0x06},
1102         {0x31, 0x90},
1103         {0x32, 0x25},
1104         {0x33, 0xa2},
1105         {0x34, 0x26},
1106         {0x35, 0x58},
1107         {0x36, 0x60},
1108         {0x37, 0x00},
1109         {0x38, 0x50},
1110         {0x39, 0x00},
1111
1112         {0x03, 0x20}, 
1113         {0x10, 0x9c}, 
1114         {0x03, 0x22}, 
1115         {0x10, 0xe9}, 
1116         
1117         //Page 00
1118         {0x03, 0x00}, 
1119         {0x0e, 0x03}, 
1120         {0x0e, 0x73}, 
1121
1122         {0x03, 0x00}, 
1123         {0x03, 0x00},
1124         {0x03, 0x00},
1125         {0x03, 0x00},
1126         {0x03, 0x00},
1127         {0x03, 0x00},
1128         {0x03, 0x00},
1129         {0x03, 0x00},
1130         {0x03, 0x00},
1131         {0x03, 0x00}, 
1132
1133         {0x03, 0x00}, 
1134         {0x01, 0xf8},
1135
1136         {END_REG, END_REG},
1137
1138 };
1139
1140
1141 /* 1600X1200 UXGA capture */
1142 static struct reginfo sensor_uxga[] =
1143 {
1144         {0x03, 0x00},
1145         //{0x11, 0xa0}, 
1146
1147         {0x20, 0x00}, 
1148         {0x21, 0x0a}, 
1149         {0x22, 0x00}, 
1150         {0x23, 0x0a}, 
1151
1152         {0x03, 0x10},
1153         {0x3f, 0x00},
1154
1155         //Page12
1156         {0x03, 0x12},
1157         {0x20, 0x0f},
1158         {0x21, 0x0f},
1159         {0x90, 0x5d},
1160
1161         //Page13
1162         {0x03, 0x13},
1163         {0x80, 0xfd},
1164
1165         // 1600*1200    
1166         {0x03,0x00},
1167         {0x10,0x00},
1168         
1169         {END_REG, END_REG},
1170 };
1171
1172 /* 1280X1024 SXGA */
1173 static struct reginfo sensor_sxga[] =
1174 {
1175         {END_REG, END_REG},
1176 };
1177 static struct reginfo sensor_xga[] =
1178 {
1179         {0x01, 0xf8},
1180         {END_REG, END_REG},
1181 };
1182 /* 800X600 SVGA,30fps*/
1183 static struct reginfo sensor_svga[] =
1184 {
1185 #if 1
1186         {0x03, 0x10},
1187         {0x3f, 0x00},
1188         
1189
1190         //Page12
1191         {0x03, 0x12}, //Function
1192         {0x20, 0x0f},
1193         {0x21, 0x0f},
1194         {0x90, 0x5d},  
1195
1196         //Page13
1197         {0x03, 0x13}, //Function
1198         {0x80, 0xfd}, //Function
1199
1200         // 800*600      
1201         {0x03,0x00},
1202         {0x10,0x91},//11
1203         {0x20, 0x00},
1204         {0x21, 0x04},
1205         {0x22, 0x00},
1206         {0x23, 0x07},
1207         {0x24, 0x04},
1208         {0x25, 0xb0},
1209         {0x26, 0x06},
1210         {0x27, 0x40},
1211         
1212         {0x03, 0x20},
1213         {0x8b, 0x1d},
1214         {0x8c, 0x4c},
1215         {0x8d, 0x18},
1216         {0x8e, 0x6a},
1217         
1218         {0x03, 0x20},
1219         {0x03, 0x20}, 
1220 #endif
1221         {END_REG, END_REG},
1222 };
1223
1224 /* 640X480 VGA */
1225 static struct reginfo sensor_vga[] =
1226 {
1227         {END_REG, END_REG},
1228 };
1229
1230 /* 352X288 CIF */
1231 static struct reginfo sensor_cif[] =
1232 {};
1233
1234 /* 320*240 QVGA */
1235 static  struct reginfo sensor_qvga[] =
1236 {};
1237
1238 /* 176X144 QCIF*/
1239 static struct reginfo sensor_qcif[] =
1240 {};
1241
1242
1243 static  struct reginfo sensor_ClrFmt_YUYV[]=
1244 {
1245
1246     {0x00, 0x00}
1247 };
1248
1249 static  struct reginfo sensor_ClrFmt_UYVY[]=
1250 {
1251
1252     {0x00, 0x00}
1253 };
1254
1255 #if CONFIG_SENSOR_WhiteBalance
1256 static  struct reginfo sensor_WhiteB_Auto[]=
1257 {
1258         {0x03, 0x22},
1259         {0x10, 0x69},
1260         {0x80, 0x3d},
1261         {0x81, 0x20},
1262         {0x82, 0x32},
1263         {0x83, 0x50},
1264         {0x84, 0x20},
1265         {0x85, 0x50},
1266         {0x86, 0x20},
1267         {0x10, 0xe9},   
1268         {END_REG, END_REG},
1269 };
1270 /* Cloudy Colour Temperature : 6500K - 8000K  */
1271 static  struct reginfo sensor_WhiteB_Cloudy[]=
1272 {
1273         {0x03, 0x22},
1274         {0x10, 0x69},
1275         {0x80, 0x49},
1276         {0x81, 0x20},
1277         {0x82, 0x24},
1278         {0x83, 0x50},
1279         {0x84, 0x45},
1280         {0x85, 0x24},
1281         {0x86, 0x1e},
1282         {0x10, 0xe9},   
1283         {END_REG, END_REG},
1284 };
1285 /* ClearDay Colour Temperature : 5000K - 6500K  */
1286 static  struct reginfo sensor_WhiteB_ClearDay[]=
1287 {
1288     //Sunny
1289         {0x03, 0x22},
1290         {0x10, 0x69},
1291         {0x80, 0x45},
1292         {0x81, 0x20},
1293         {0x82, 0x27},
1294         {0x83, 0x44},
1295         {0x84, 0x3f},
1296         {0x85, 0x29},
1297         {0x86, 0x23},
1298         {END_REG, END_REG},
1299 };
1300 /* Office Colour Temperature : 3500K - 5000K  */
1301 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
1302 {
1303     //incandescense
1304         {0x03, 0x22},
1305         {0x10, 0x69},
1306         {0x80, 0x33},
1307         {0x81, 0x20},
1308         {0x82, 0x3d},
1309         {0x83, 0x2e},
1310         {0x84, 0x24},
1311         {0x85, 0x43},
1312         {0x86, 0x3d},
1313         {END_REG, END_REG},
1314
1315 };
1316 /* Home Colour Temperature : 2500K - 3500K  */
1317 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
1318 {
1319     //Home
1320         {0x03, 0x22},
1321         {0x10, 0x69},
1322         {0x80, 0x45},
1323         {0x81, 0x20},
1324         {0x82, 0x2f},
1325         {0x83, 0x38},
1326         {0x84, 0x32},
1327         {0x85, 0x39},
1328         {0x86, 0x33},   
1329         {END_REG, END_REG},
1330 };
1331 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
1332     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
1333 };
1334 #endif
1335
1336 #if CONFIG_SENSOR_Brightness
1337 static  struct reginfo sensor_Brightness0[]=
1338 {
1339     // Brightness -2
1340     {0x03, 0x10},
1341     {0x40, 0xa0},
1342         {END_REG, END_REG},
1343 };
1344
1345 static  struct reginfo sensor_Brightness1[]=
1346 {
1347     // Brightness -1
1348         {0x03, 0x10},
1349     {0x40, 0x90},
1350         {END_REG, END_REG},
1351 };
1352
1353 static  struct reginfo sensor_Brightness2[]=
1354 {
1355     //  Brightness 0
1356         {0x03, 0x10},
1357     {0x40, 0x00},
1358         {END_REG, END_REG},
1359 };
1360
1361 static  struct reginfo sensor_Brightness3[]=
1362 {
1363     // Brightness +1
1364         {0x03, 0x10},
1365     {0x40, 0x10},
1366         {END_REG, END_REG},
1367 };
1368
1369 static  struct reginfo sensor_Brightness4[]=
1370 {
1371     //  Brightness +2
1372         {0x03, 0x10},
1373     {0x40, 0x20},
1374         {END_REG, END_REG},
1375 };
1376
1377 static  struct reginfo sensor_Brightness5[]=
1378 {
1379     //  Brightness +3
1380     {0x03, 0x10},
1381     {0x40, 0x30},
1382         {END_REG, END_REG},
1383 };
1384 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
1385     sensor_Brightness4, sensor_Brightness5,NULL,
1386 };
1387
1388 #endif
1389
1390 #if CONFIG_SENSOR_Effect
1391 static  struct reginfo sensor_Effect_Normal[] =
1392 {
1393         {0x03, 0x10},
1394         {0x11, 0x03},
1395         {0x12, 0x00},
1396         {0x13, 0x00},   
1397         {END_REG, END_REG},
1398 };
1399
1400 static  struct reginfo sensor_Effect_WandB[] =
1401 {
1402         {0x03, 0x10},
1403         {0x11, 0x03},
1404         {0x12, 0x03},
1405         {0x13, 0x02},
1406         {0x44, 0x80},
1407         {0x45, 0x80},
1408         {0x47, 0x7f},   
1409         {END_REG, END_REG},
1410 };
1411
1412 static  struct reginfo sensor_Effect_Sepia[] =
1413 {
1414         {0x03, 0x10},
1415         {0x11, 0x03},
1416         {0x12, 0x23},
1417         {0x13, 0x00},
1418         {0x44, 0x70},
1419         {0x45, 0x98},
1420         {0x47, 0x7f},   
1421         {END_REG, END_REG},
1422 };
1423
1424 static  struct reginfo sensor_Effect_Negative[] =
1425 {
1426     //Negative
1427         {0x03, 0x10},
1428         {0x11, 0x03},
1429         {0x12, 0x08},
1430         {0x13, 0x02},
1431         {0x14, 0x00},   
1432         {0x44, 0x80},
1433         {0x45, 0x80},
1434         {0x47, 0x7f},   
1435         {END_REG, END_REG},
1436 };
1437 static  struct reginfo sensor_Effect_Bluish[] =
1438 {
1439     // Bluish
1440         {0x03, 0x10},
1441         {0x11, 0x03},
1442         {0x12, 0x03},
1443         {0x13, 0x02},
1444         {0x44, 0xb0},
1445         {0x45, 0x40},
1446         {0x47, 0x7f},   
1447         {END_REG, END_REG},
1448 };
1449
1450 static  struct reginfo sensor_Effect_Green[] =
1451 {
1452     //  Greenish
1453     {0x03, 0x10},
1454         {0x11, 0x03},
1455         {0x12, 0x03},
1456         {0x13, 0x02},
1457         {0x44, 0x30},
1458         {0x45, 0x50},
1459         {0x47, 0x7f},   
1460         {END_REG, END_REG},
1461 };
1462 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
1463     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
1464 };
1465 #endif
1466 #if CONFIG_SENSOR_Exposure
1467 static  struct reginfo sensor_Exposure0[]=
1468 {
1469     //-3
1470
1471 };
1472
1473 static  struct reginfo sensor_Exposure1[]=
1474 {
1475     //-2
1476
1477         {END_REG, END_REG},
1478 };
1479
1480 static  struct reginfo sensor_Exposure2[]=
1481 {
1482     //-0.3EV
1483         {END_REG, END_REG},
1484 };
1485
1486 static  struct reginfo sensor_Exposure3[]=
1487 {
1488     //default
1489         {END_REG, END_REG},
1490 };
1491
1492 static  struct reginfo sensor_Exposure4[]=
1493 {
1494     // 1
1495
1496         {END_REG, END_REG},
1497 };
1498
1499 static  struct reginfo sensor_Exposure5[]=
1500 {
1501     // 2
1502         {END_REG, END_REG},
1503 };
1504
1505 static  struct reginfo sensor_Exposure6[]=
1506 {
1507     // 3
1508
1509         {END_REG, END_REG},
1510 };
1511
1512 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1513     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1514 };
1515 #endif
1516 #if CONFIG_SENSOR_Saturation
1517 static  struct reginfo sensor_Saturation0[]=
1518 {
1519
1520         {END_REG, END_REG},
1521 };
1522
1523 static  struct reginfo sensor_Saturation1[]=
1524 {
1525
1526         {END_REG, END_REG},
1527 };
1528
1529 static  struct reginfo sensor_Saturation2[]=
1530 {
1531         {END_REG, END_REG},
1532 };
1533 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1534
1535 #endif
1536 #if CONFIG_SENSOR_Contrast
1537 static  struct reginfo sensor_Contrast0[]=
1538 {
1539     //Contrast -3
1540   
1541         {END_REG, END_REG},
1542 };
1543
1544 static  struct reginfo sensor_Contrast1[]=
1545 {
1546     //Contrast -2
1547
1548         {END_REG, END_REG},
1549 };
1550
1551 static  struct reginfo sensor_Contrast2[]=
1552 {
1553     // Contrast -1
1554
1555         {END_REG, END_REG},
1556 };
1557
1558 static  struct reginfo sensor_Contrast3[]=
1559 {
1560     //Contrast 0
1561
1562         {END_REG, END_REG},
1563 };
1564
1565 static  struct reginfo sensor_Contrast4[]=
1566 {
1567     //Contrast +1
1568
1569         {END_REG, END_REG},
1570 };
1571
1572
1573 static  struct reginfo sensor_Contrast5[]=
1574 {
1575     //Contrast +2
1576
1577         {END_REG, END_REG},
1578 };
1579
1580 static  struct reginfo sensor_Contrast6[]=
1581 {
1582     //Contrast +3
1583
1584         {END_REG, END_REG},
1585 };
1586 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1587     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1588 };
1589
1590 #endif
1591 #if CONFIG_SENSOR_Mirror
1592 static  struct reginfo sensor_MirrorOn[]=
1593 {
1594
1595         {END_REG, END_REG},
1596 };
1597
1598 static  struct reginfo sensor_MirrorOff[]=
1599 {
1600
1601         {END_REG, END_REG},
1602 };
1603 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1604 #endif
1605 #if CONFIG_SENSOR_Flip
1606 static  struct reginfo sensor_FlipOn[]=
1607 {
1608
1609         {END_REG, END_REG},
1610 };
1611
1612 static  struct reginfo sensor_FlipOff[]=
1613 {
1614
1615         {END_REG, END_REG},
1616 };
1617 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1618
1619 #endif
1620 #if CONFIG_SENSOR_Scene
1621 static  struct reginfo sensor_SceneAuto[] =
1622 {
1623
1624         {END_REG, END_REG},     
1625 };
1626
1627 static  struct reginfo sensor_SceneNight[] =
1628 {
1629
1630         {END_REG, END_REG},
1631 };
1632 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1633
1634 #endif
1635 #if CONFIG_SENSOR_DigitalZoom
1636 static struct reginfo sensor_Zoom0[] =
1637 {
1638         {END_REG, END_REG},
1639 };
1640
1641 static struct reginfo sensor_Zoom1[] =
1642 {
1643         {END_REG, END_REG},
1644 };
1645
1646 static struct reginfo sensor_Zoom2[] =
1647 {
1648         {END_REG, END_REG},
1649 };
1650
1651
1652 static struct reginfo sensor_Zoom3[] =
1653 {
1654         {END_REG, END_REG},
1655 };
1656 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1657 #endif
1658 static const struct v4l2_querymenu sensor_menus[] =
1659 {
1660         #if CONFIG_SENSOR_WhiteBalance
1661     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1662     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1663     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1664     #endif
1665
1666         #if CONFIG_SENSOR_Effect
1667     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1668     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1669     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
1670     #endif
1671
1672         #if CONFIG_SENSOR_Scene
1673     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1674     #endif
1675
1676         #if CONFIG_SENSOR_Flash
1677     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1678     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1679     #endif
1680 };
1681
1682 static const struct v4l2_queryctrl sensor_controls[] =
1683 {
1684         #if CONFIG_SENSOR_WhiteBalance
1685     {
1686         .id             = V4L2_CID_DO_WHITE_BALANCE,
1687         .type           = V4L2_CTRL_TYPE_MENU,
1688         .name           = "White Balance Control",
1689         .minimum        = 0,
1690         .maximum        = 4,
1691         .step           = 1,
1692         .default_value = 0,
1693     },
1694     #endif
1695
1696         #if CONFIG_SENSOR_Brightness
1697         {
1698         .id             = V4L2_CID_BRIGHTNESS,
1699         .type           = V4L2_CTRL_TYPE_INTEGER,
1700         .name           = "Brightness Control",
1701         .minimum        = -3,
1702         .maximum        = 2,
1703         .step           = 1,
1704         .default_value = 0,
1705     },
1706     #endif
1707
1708         #if CONFIG_SENSOR_Effect
1709         {
1710         .id             = V4L2_CID_EFFECT,
1711         .type           = V4L2_CTRL_TYPE_MENU,
1712         .name           = "Effect Control",
1713         .minimum        = 0,
1714         .maximum        = 5,
1715         .step           = 1,
1716         .default_value = 0,
1717     },
1718         #endif
1719
1720         #if CONFIG_SENSOR_Exposure
1721         {
1722         .id             = V4L2_CID_EXPOSURE,
1723         .type           = V4L2_CTRL_TYPE_INTEGER,
1724         .name           = "Exposure Control",
1725         .minimum        = 0,
1726         .maximum        = 6,
1727         .step           = 1,
1728         .default_value = 0,
1729     },
1730         #endif
1731
1732         #if CONFIG_SENSOR_Saturation
1733         {
1734         .id             = V4L2_CID_SATURATION,
1735         .type           = V4L2_CTRL_TYPE_INTEGER,
1736         .name           = "Saturation Control",
1737         .minimum        = 0,
1738         .maximum        = 2,
1739         .step           = 1,
1740         .default_value = 0,
1741     },
1742     #endif
1743
1744         #if CONFIG_SENSOR_Contrast
1745         {
1746         .id             = V4L2_CID_CONTRAST,
1747         .type           = V4L2_CTRL_TYPE_INTEGER,
1748         .name           = "Contrast Control",
1749         .minimum        = -3,
1750         .maximum        = 3,
1751         .step           = 1,
1752         .default_value = 0,
1753     },
1754         #endif
1755
1756         #if CONFIG_SENSOR_Mirror
1757         {
1758         .id             = V4L2_CID_HFLIP,
1759         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1760         .name           = "Mirror Control",
1761         .minimum        = 0,
1762         .maximum        = 1,
1763         .step           = 1,
1764         .default_value = 1,
1765     },
1766     #endif
1767
1768         #if CONFIG_SENSOR_Flip
1769         {
1770         .id             = V4L2_CID_VFLIP,
1771         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1772         .name           = "Flip Control",
1773         .minimum        = 0,
1774         .maximum        = 1,
1775         .step           = 1,
1776         .default_value = 1,
1777     },
1778     #endif
1779
1780         #if CONFIG_SENSOR_Scene
1781     {
1782         .id             = V4L2_CID_SCENE,
1783         .type           = V4L2_CTRL_TYPE_MENU,
1784         .name           = "Scene Control",
1785         .minimum        = 0,
1786         .maximum        = 1,
1787         .step           = 1,
1788         .default_value = 0,
1789     },
1790     #endif
1791
1792         #if CONFIG_SENSOR_DigitalZoom
1793     {
1794         .id             = V4L2_CID_ZOOM_RELATIVE,
1795         .type           = V4L2_CTRL_TYPE_INTEGER,
1796         .name           = "DigitalZoom Control",
1797         .minimum        = -1,
1798         .maximum        = 1,
1799         .step           = 1,
1800         .default_value = 0,
1801     }, {
1802         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1803         .type           = V4L2_CTRL_TYPE_INTEGER,
1804         .name           = "DigitalZoom Control",
1805         .minimum        = 0,
1806         .maximum        = 3,
1807         .step           = 1,
1808         .default_value = 0,
1809     },
1810     #endif
1811
1812         #if CONFIG_SENSOR_Focus
1813         {
1814         .id             = V4L2_CID_FOCUS_RELATIVE,
1815         .type           = V4L2_CTRL_TYPE_INTEGER,
1816         .name           = "Focus Control",
1817         .minimum        = -1,
1818         .maximum        = 1,
1819         .step           = 1,
1820         .default_value = 0,
1821     }, {
1822         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1823         .type           = V4L2_CTRL_TYPE_INTEGER,
1824         .name           = "Focus Control",
1825         .minimum        = 0,
1826         .maximum        = 255,
1827         .step           = 1,
1828         .default_value = 125,
1829     },
1830     #endif
1831
1832         #if CONFIG_SENSOR_Flash
1833         {
1834         .id             = V4L2_CID_FLASH,
1835         .type           = V4L2_CTRL_TYPE_MENU,
1836         .name           = "Flash Control",
1837         .minimum        = 0,
1838         .maximum        = 3,
1839         .step           = 1,
1840         .default_value = 0,
1841     },
1842         #endif
1843 };
1844
1845 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1846 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1847 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1848 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1849 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1850 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1851 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1852 static int sensor_resume(struct soc_camera_device *icd);
1853 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1854 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1855 #if CONFIG_SENSOR_Effect
1856 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1857 #endif
1858 #if CONFIG_SENSOR_WhiteBalance
1859 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1860 #endif
1861 static int sensor_deactivate(struct i2c_client *client);
1862
1863 static struct soc_camera_ops sensor_ops =
1864 {
1865     .suspend                     = sensor_suspend,
1866     .resume                       = sensor_resume,
1867     .set_bus_param              = sensor_set_bus_param,
1868     .query_bus_param    = sensor_query_bus_param,
1869     .controls           = sensor_controls,
1870     .menus                         = sensor_menus,
1871     .num_controls               = ARRAY_SIZE(sensor_controls),
1872     .num_menus          = ARRAY_SIZE(sensor_menus),
1873 };
1874
1875 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1876         { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1877         .colorspace = _colorspace }
1878
1879 #define JPG_FMT(_name, _depth, _fourcc) \
1880         COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1881
1882 static const struct soc_camera_data_format sensor_colour_formats[] = {
1883         JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1884         JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1885 };
1886
1887 typedef struct sensor_info_priv_s
1888 {
1889     int whiteBalance;
1890     int brightness;
1891     int contrast;
1892     int saturation;
1893     int effect;
1894     int scene;
1895     int digitalzoom;
1896     int focus;
1897     int flash;
1898     int exposure;
1899         bool snap2preview;
1900         bool video2preview;
1901     unsigned char mirror;                                        /* HFLIP */
1902     unsigned char flip;                                          /* VFLIP */
1903     unsigned int winseqe_cur_addr;
1904         unsigned int pixfmt;
1905
1906 } sensor_info_priv_t;
1907
1908 struct sensor
1909 {
1910     struct v4l2_subdev subdev;
1911     struct i2c_client *client;
1912     sensor_info_priv_t info_priv;
1913     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1914 #if CONFIG_SENSOR_I2C_NOSCHED
1915         atomic_t tasklock_cnt;
1916 #endif
1917         struct rk29camera_platform_data *sensor_io_request;
1918     struct rk29camera_gpio_res *sensor_gpio_res;
1919 };
1920
1921 static struct sensor* to_sensor(const struct i2c_client *client)
1922 {
1923     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1924 }
1925
1926 static int sensor_task_lock(struct i2c_client *client, int lock)
1927 {
1928 #if CONFIG_SENSOR_I2C_NOSCHED
1929         int cnt = 3;
1930     struct sensor *sensor = to_sensor(client);
1931
1932         if (lock) {
1933                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1934                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1935                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1936                                 msleep(35);
1937                                 cnt--;
1938                         }
1939                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1940                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1941                                 goto sensor_task_lock_err;
1942                         }
1943                         preempt_disable();
1944                 }
1945
1946                 atomic_add(1, &sensor->tasklock_cnt);
1947         } else {
1948                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1949                         atomic_sub(1, &sensor->tasklock_cnt);
1950
1951                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1952                                 preempt_enable();
1953                 }
1954         }
1955         return 0;
1956 sensor_task_lock_err:
1957         return -1;  
1958 #else
1959     return 0;
1960 #endif
1961 }
1962
1963 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1964 {
1965     int err,cnt;
1966     u8 buf[2];
1967     struct i2c_msg msg[1];
1968
1969     buf[0] = reg;
1970     buf[1] = val;
1971         
1972     msg->addr = client->addr;
1973     msg->flags = client->flags;
1974     msg->buf = buf;
1975     msg->len = sizeof(buf);
1976     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1977     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1978
1979     cnt = 3;
1980     err = -EAGAIN;
1981
1982     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1983         err = i2c_transfer(client->adapter, msg, 1);
1984
1985         if (err >= 0) {
1986             return 0;
1987         } else {
1988                 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1989             udelay(10);
1990         }
1991     }
1992
1993     return err;
1994 }
1995
1996 /* sensor register read */
1997 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1998 {
1999     int err,cnt;
2000     u8 buf[1];
2001     struct i2c_msg msg[2];
2002
2003     buf[0] = reg ;
2004
2005     msg[0].addr = client->addr;
2006     msg[0].flags = client->flags;
2007     msg[0].buf = buf;
2008     msg[0].len = sizeof(buf);
2009     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
2010     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2011
2012     msg[1].addr = client->addr;
2013     msg[1].flags = client->flags|I2C_M_RD;
2014     msg[1].buf = buf;
2015     msg[1].len = 1;
2016     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
2017     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2018
2019     cnt = 3;
2020     err = -EAGAIN;
2021     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
2022         err = i2c_transfer(client->adapter, msg, 2);
2023
2024         if (err >= 0) {
2025             *val = buf[0];
2026             return 0;
2027         } else {
2028                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
2029             udelay(10);
2030         }
2031     }
2032
2033     return err;
2034 }
2035
2036
2037 /* write a array of registers  */
2038 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
2039 {
2040     int err = 0, cnt;
2041     int i = 0;
2042 #if CONFIG_SENSOR_I2C_RDWRCHK    
2043         char valchk;
2044 #endif
2045
2046         cnt = 0;
2047         if (sensor_task_lock(client, 1) < 0)
2048                 goto sensor_write_array_end;
2049     while (regarray[i].reg != END_REG)
2050     {
2051         err = sensor_write(client, regarray[i].reg, regarray[i].val);
2052         if (err < 0)
2053         {
2054             if (cnt-- > 0) {
2055                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
2056                                 i = 0;
2057                                 continue;
2058             } else {
2059                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
2060                 err = -EPERM;
2061                                 goto sensor_write_array_end;
2062             }
2063         } else {
2064         #if CONFIG_SENSOR_I2C_RDWRCHK
2065                         //mdelay(5);
2066                         sensor_read(client, regarray[i].reg, &valchk);
2067                         if (valchk != regarray[i].val)
2068                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2069                 #endif
2070         }
2071         i++;
2072     }
2073
2074 sensor_write_array_end:
2075         sensor_task_lock(client,0);
2076         return err;
2077 }
2078 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
2079 {
2080     int cnt;
2081     int i = 0;
2082         char valchk;
2083
2084         cnt = 0;
2085         valchk = 0;
2086     while (regarray[i].reg != 0)
2087     {
2088                 sensor_read(client, regarray[i].reg, &valchk);
2089                 if (valchk != regarray[i].val)
2090                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2091
2092         i++;
2093     }
2094     return 0;
2095 }
2096 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2097 {
2098         struct soc_camera_link *icl = to_soc_camera_link(icd);
2099         int ret = 0;
2100
2101     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2102         switch (cmd)
2103         {
2104                 case Sensor_PowerDown:
2105                 {
2106                         if (icl->powerdown) {
2107                                 ret = icl->powerdown(icd->pdev, on);
2108                                 if (ret == RK29_CAM_IO_SUCCESS) {
2109                                         if (on == 0) {
2110                                                 mdelay(2);
2111                                                 if (icl->reset)
2112                                                         icl->reset(icd->pdev);
2113                                         }
2114                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2115                                         ret = -ENODEV;
2116                                         goto sensor_power_end;
2117                                 }
2118                         }
2119                         break;
2120                 }
2121                 case Sensor_Flash:
2122                 {
2123                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2124                 struct sensor *sensor = to_sensor(client);
2125
2126                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2127                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2128                         }
2129             break;
2130                 }
2131                 default:
2132                 {
2133                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2134                         break;
2135                 }
2136         }
2137 sensor_power_end:
2138         return ret;
2139 }
2140 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2141 {
2142     struct i2c_client *client = sd->priv;
2143     struct soc_camera_device *icd = client->dev.platform_data;
2144     struct sensor *sensor = to_sensor(client);
2145         const struct v4l2_queryctrl *qctrl;
2146     char value;
2147     int ret;
2148
2149     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2150
2151         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2152                 ret = -ENODEV;
2153                 goto sensor_INIT_ERR;
2154         }
2155
2156     /* soft reset */
2157         if (sensor_task_lock(client,1)<0)
2158                 goto sensor_INIT_ERR;
2159 #if 1   
2160     ret = sensor_write(client, 0x01, 0xF9);
2161     if (ret != 0)
2162     {
2163         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2164         ret = -ENODEV;
2165                 goto sensor_INIT_ERR;
2166     }
2167
2168     mdelay(5);  //delay 5 microseconds
2169         /* check if it is an sensor sensor */
2170     ret = sensor_read(client, 0x04, &value);
2171     if (ret != 0) {
2172         SENSOR_TR("read chip id high byte failed\n");
2173         ret = -ENODEV;
2174         goto sensor_INIT_ERR;
2175     }
2176
2177    
2178     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), value);
2179     if (value == SENSOR_ID) {
2180         sensor->model = SENSOR_V4L2_IDENT;
2181     } else {
2182         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), value);
2183         ret = -ENODEV;
2184         goto sensor_INIT_ERR;
2185     }
2186 #endif
2187     ret = sensor_write_array(client, sensor_init_data);
2188     if (ret != 0)
2189     {
2190         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2191         goto sensor_INIT_ERR;
2192     }
2193         sensor_task_lock(client,0);
2194     //icd->user_width = SENSOR_INIT_WIDTH;
2195     //icd->user_height = SENSOR_INIT_HEIGHT;
2196     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
2197         sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
2198
2199     /* sensor sensor information for initialization  */
2200         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2201         if (qctrl)
2202         sensor->info_priv.whiteBalance = qctrl->default_value;
2203         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2204         if (qctrl)
2205         sensor->info_priv.brightness = qctrl->default_value;
2206         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2207         if (qctrl)
2208         sensor->info_priv.effect = qctrl->default_value;
2209         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2210         if (qctrl)
2211         sensor->info_priv.exposure = qctrl->default_value;
2212
2213         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2214         if (qctrl)
2215         sensor->info_priv.saturation = qctrl->default_value;
2216         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2217         if (qctrl)
2218         sensor->info_priv.contrast = qctrl->default_value;
2219         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2220         if (qctrl)
2221         sensor->info_priv.mirror = qctrl->default_value;
2222         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2223         if (qctrl)
2224         sensor->info_priv.flip = qctrl->default_value;
2225         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2226         if (qctrl)
2227         sensor->info_priv.scene = qctrl->default_value;
2228         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2229         if (qctrl)
2230         sensor->info_priv.digitalzoom = qctrl->default_value;
2231
2232     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
2233         #if CONFIG_SENSOR_Focus
2234     sensor_set_focus();
2235     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2236         if (qctrl)
2237         sensor->info_priv.focus = qctrl->default_value;
2238         #endif
2239
2240         #if CONFIG_SENSOR_Flash
2241         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2242         if (qctrl)
2243         sensor->info_priv.flash = qctrl->default_value;
2244     #endif
2245
2246     SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),((val == 0)?__FUNCTION__:"sensor_reinit"),icd->user_width,icd->user_height);
2247
2248     return 0;
2249 sensor_INIT_ERR:
2250         sensor_task_lock(client,0);
2251         sensor_deactivate(client);
2252     return ret;
2253 }
2254
2255 static int sensor_deactivate(struct i2c_client *client)
2256 {
2257         struct soc_camera_device *icd = client->dev.platform_data;
2258
2259         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2260
2261         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2262         sensor_ioctrl(icd, Sensor_PowerDown, 1);
2263
2264         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2265         icd->user_width = SENSOR_INIT_WIDTH;
2266     icd->user_height = SENSOR_INIT_HEIGHT;
2267         msleep(100);
2268         return 0;
2269 }
2270
2271 static  struct reginfo sensor_power_down_sequence[]=
2272 {
2273     {0x00,0x00}
2274 };
2275 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2276 {
2277     int ret;
2278     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2279
2280     if (pm_msg.event == PM_EVENT_SUSPEND) {
2281         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2282         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2283         if (ret != 0) {
2284             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2285             return ret;
2286         } else {
2287             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2288             if (ret < 0) {
2289                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2290                 return -EINVAL;
2291             }
2292         }
2293     } else {
2294         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2295         return -EINVAL;
2296     }
2297     return 0;
2298 }
2299
2300 static int sensor_resume(struct soc_camera_device *icd)
2301 {
2302         int ret;
2303
2304     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2305     if (ret < 0) {
2306                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2307         return -EINVAL;
2308     }
2309
2310         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2311
2312     return 0;
2313
2314 }
2315
2316 static int sensor_set_bus_param(struct soc_camera_device *icd,
2317                                 unsigned long flags)
2318 {
2319
2320     return 0;
2321 }
2322
2323 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2324 {
2325     struct soc_camera_link *icl = to_soc_camera_link(icd);
2326     unsigned long flags = SENSOR_BUS_PARAM;
2327
2328     return soc_camera_apply_sensor_flags(icl, flags);
2329 }
2330
2331 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2332 {
2333     struct i2c_client *client = sd->priv;
2334     struct soc_camera_device *icd = client->dev.platform_data;
2335     struct sensor *sensor = to_sensor(client);
2336     struct v4l2_pix_format *pix = &f->fmt.pix;
2337
2338     pix->width          = icd->user_width;
2339     pix->height         = icd->user_height;
2340     pix->pixelformat    = sensor->info_priv.pixfmt;
2341     pix->field          = V4L2_FIELD_NONE;
2342     pix->colorspace             = V4L2_COLORSPACE_JPEG;
2343
2344     return 0;
2345 }
2346 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
2347 {
2348     bool ret = false;
2349
2350         if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
2351                 ret = true;
2352         } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
2353                 ret = true;
2354         } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
2355                 ret = true;
2356         } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
2357                 ret = true;
2358         } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
2359                 ret = true;
2360         }
2361
2362         if (ret == true)
2363                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2364         return ret;
2365 }
2366
2367 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
2368 {
2369     bool ret = false;
2370
2371         if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
2372                 ret = true;
2373         } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
2374                 ret = true;
2375         }
2376
2377         if (ret == true)
2378                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2379         return ret;
2380 }
2381 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2382 {
2383     struct i2c_client *client = sd->priv;
2384     struct sensor *sensor = to_sensor(client);
2385     struct v4l2_pix_format *pix = &f->fmt.pix;
2386         const struct v4l2_queryctrl *qctrl;
2387         struct soc_camera_device *icd = client->dev.platform_data;
2388     struct reginfo *winseqe_set_addr=NULL;
2389     int ret=0, set_w,set_h;
2390
2391         if (sensor->info_priv.pixfmt != pix->pixelformat) {
2392                 switch (pix->pixelformat)
2393                 {
2394                         case V4L2_PIX_FMT_YUYV:
2395                         {
2396                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2397                                 break;
2398                         }
2399                         case V4L2_PIX_FMT_UYVY:
2400                         {
2401                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2402                                 break;
2403                         }
2404                         default:
2405                                 break;
2406                 }
2407                 if (winseqe_set_addr != NULL) {
2408             sensor_write_array(client, winseqe_set_addr);
2409                         sensor->info_priv.pixfmt = pix->pixelformat;
2410
2411                         SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2412                 } else {
2413                         SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2414                 }
2415         }
2416
2417     set_w = pix->width;
2418     set_h = pix->height;
2419
2420         if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg != END_REG))
2421         {
2422                 winseqe_set_addr = sensor_qcif;
2423         set_w = 176;
2424         set_h = 144;
2425         }
2426         else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg != END_REG))
2427     {
2428         winseqe_set_addr = sensor_qvga;
2429         set_w = 320;
2430         set_h = 240;
2431     }
2432     else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg != END_REG))
2433     {
2434         winseqe_set_addr = sensor_cif;
2435         set_w = 352;
2436         set_h = 288;
2437     }
2438     else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg != END_REG))
2439     {
2440         winseqe_set_addr = sensor_vga;
2441         set_w = 640;
2442         set_h = 480;
2443     }
2444     else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg != END_REG))
2445     {
2446         winseqe_set_addr = sensor_svga;
2447         set_w = 800;
2448         set_h = 600;
2449     }
2450         else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg != END_REG))
2451     {
2452         winseqe_set_addr = sensor_xga;
2453         set_w = 1024;
2454         set_h = 768;
2455     }
2456     else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg != END_REG))
2457     {
2458         winseqe_set_addr = sensor_sxga;
2459         set_w = 1280;
2460         set_h = 1024;
2461     }
2462     else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg != END_REG))
2463     {
2464         winseqe_set_addr = sensor_uxga;
2465         set_w = 1600;
2466         set_h = 1200;
2467     }
2468     else
2469     {
2470         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2471         set_w = SENSOR_INIT_WIDTH;
2472         set_h = SENSOR_INIT_HEIGHT;
2473
2474                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
2475     }
2476
2477     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
2478         #if CONFIG_SENSOR_Flash
2479         if (sensor_fmt_capturechk(sd,f) == true) {      /* ddl@rock-chips.com : Capture */
2480             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2481                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2482                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2483             }           
2484         } else {                                        /* ddl@rock-chips.com : Video */
2485             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2486                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2487                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2488             }
2489         }
2490         #endif
2491         ret |= sensor_write_array(client, winseqe_set_addr);
2492         if (ret != 0) {
2493             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2494             #if CONFIG_SENSOR_Flash
2495             if (sensor_fmt_capturechk(sd,f) == true) {
2496                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2497                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2498                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2499                 }
2500             }
2501             #endif
2502             goto sensor_s_fmt_end;
2503         }
2504
2505         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
2506
2507                 if (sensor_fmt_capturechk(sd,f) == true) {                                  /* ddl@rock-chips.com : Capture */
2508         #if CONFIG_SENSOR_Effect
2509                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2510                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2511         #endif
2512         #if CONFIG_SENSOR_WhiteBalance
2513                         if (sensor->info_priv.whiteBalance != 0) {
2514                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2515                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2516                         }
2517         #endif
2518                         sensor->info_priv.snap2preview = true;
2519                 } else if (sensor_fmt_videochk(sd,f) == true) {                 /* ddl@rock-chips.com : Video */
2520                 #if CONFIG_SENSOR_Effect
2521                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2522                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2523         #endif
2524         #if CONFIG_SENSOR_WhiteBalance
2525                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2526                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2527         #endif
2528                         sensor->info_priv.video2preview = true;
2529                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2530                 #if CONFIG_SENSOR_Effect
2531                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2532                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2533         #endif
2534         #if CONFIG_SENSOR_WhiteBalance
2535                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2536                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2537         #endif
2538                         sensor->info_priv.video2preview = false;
2539                         sensor->info_priv.snap2preview = false;
2540                 }
2541
2542         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2543     }
2544     else
2545     {
2546         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2547     }
2548
2549         pix->width = set_w;
2550     pix->height = set_h;
2551
2552 sensor_s_fmt_end:
2553     return ret;
2554 }
2555
2556 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2557 {
2558     struct v4l2_pix_format *pix = &f->fmt.pix;
2559     bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
2560         pix->pixelformat == V4L2_PIX_FMT_YUYV;
2561
2562     /*
2563     * With Bayer format enforce even side lengths, but let the user play
2564     * with the starting pixel
2565     */
2566
2567     if (pix->height > SENSOR_MAX_HEIGHT)
2568         pix->height = SENSOR_MAX_HEIGHT;
2569     else if (pix->height < SENSOR_MIN_HEIGHT)
2570         pix->height = SENSOR_MIN_HEIGHT;
2571     else if (bayer)
2572         pix->height = ALIGN(pix->height, 2);
2573
2574     if (pix->width > SENSOR_MAX_WIDTH)
2575         pix->width = SENSOR_MAX_WIDTH;
2576     else if (pix->width < SENSOR_MIN_WIDTH)
2577         pix->width = SENSOR_MIN_WIDTH;
2578     else if (bayer)
2579         pix->width = ALIGN(pix->width, 2);
2580
2581     return 0;
2582 }
2583
2584  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2585 {
2586     struct i2c_client *client = sd->priv;
2587
2588     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2589         return -EINVAL;
2590
2591     if (id->match.addr != client->addr)
2592         return -ENODEV;
2593
2594     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2595     id->revision = 0;
2596
2597     return 0;
2598 }
2599 #if CONFIG_SENSOR_Brightness
2600 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2601 {
2602     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2603
2604     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2605     {
2606         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2607         {
2608             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2609             {
2610                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2611                 return -EINVAL;
2612             }
2613             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2614             return 0;
2615         }
2616     }
2617         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2618     return -EINVAL;
2619 }
2620 #endif
2621 #if CONFIG_SENSOR_Effect
2622 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2623 {
2624     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2625
2626     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2627     {
2628         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2629         {
2630             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2631             {
2632                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2633                 return -EINVAL;
2634             }
2635             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2636             return 0;
2637         }
2638     }
2639         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2640     return -EINVAL;
2641 }
2642 #endif
2643 #if CONFIG_SENSOR_Exposure
2644 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2645 {
2646     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2647
2648     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2649     {
2650         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2651         {
2652             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2653             {
2654                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2655                 return -EINVAL;
2656             }
2657             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2658             return 0;
2659         }
2660     }
2661         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2662     return -EINVAL;
2663 }
2664 #endif
2665 #if CONFIG_SENSOR_Saturation
2666 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2667 {
2668     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2669
2670     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2671     {
2672         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2673         {
2674             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2675             {
2676                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2677                 return -EINVAL;
2678             }
2679             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2680             return 0;
2681         }
2682     }
2683     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2684     return -EINVAL;
2685 }
2686 #endif
2687 #if CONFIG_SENSOR_Contrast
2688 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2689 {
2690     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2691
2692     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2693     {
2694         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2695         {
2696             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2697             {
2698                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2699                 return -EINVAL;
2700             }
2701             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2702             return 0;
2703         }
2704     }
2705     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2706     return -EINVAL;
2707 }
2708 #endif
2709 #if CONFIG_SENSOR_Mirror
2710 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2711 {
2712     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2713
2714     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2715     {
2716         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2717         {
2718             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2719             {
2720                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2721                 return -EINVAL;
2722             }
2723             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2724             return 0;
2725         }
2726     }
2727     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2728     return -EINVAL;
2729 }
2730 #endif
2731 #if CONFIG_SENSOR_Flip
2732 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2733 {
2734     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2735
2736     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2737     {
2738         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2739         {
2740             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2741             {
2742                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2743                 return -EINVAL;
2744             }
2745             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2746             return 0;
2747         }
2748     }
2749     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2750     return -EINVAL;
2751 }
2752 #endif
2753 #if CONFIG_SENSOR_Scene
2754 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2755 {
2756     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2757
2758     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2759     {
2760         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2761         {
2762             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2763             {
2764                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2765                 return -EINVAL;
2766             }
2767             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2768             return 0;
2769         }
2770     }
2771     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2772     return -EINVAL;
2773 }
2774 #endif
2775 #if CONFIG_SENSOR_WhiteBalance
2776 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2777 {
2778     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2779
2780     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2781     {
2782         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2783         {
2784             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2785             {
2786                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2787                 return -EINVAL;
2788             }
2789             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2790             return 0;
2791         }
2792     }
2793         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2794     return -EINVAL;
2795 }
2796 #endif
2797 #if CONFIG_SENSOR_DigitalZoom
2798 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2799 {
2800     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2801     struct sensor *sensor = to_sensor(client);
2802         const struct v4l2_queryctrl *qctrl_info;
2803     int digitalzoom_cur, digitalzoom_total;
2804
2805         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2806         if (qctrl_info)
2807                 return -EINVAL;
2808
2809     digitalzoom_cur = sensor->info_priv.digitalzoom;
2810     digitalzoom_total = qctrl_info->maximum;
2811
2812     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2813     {
2814         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2815         return -EINVAL;
2816     }
2817
2818     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2819     {
2820         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2821         return -EINVAL;
2822     }
2823
2824     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2825     {
2826         *value = digitalzoom_total - digitalzoom_cur;
2827     }
2828
2829     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2830     {
2831         *value = 0 - digitalzoom_cur;
2832     }
2833
2834     digitalzoom_cur += *value;
2835
2836     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2837     {
2838         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2839         {
2840             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2841             return -EINVAL;
2842         }
2843         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2844         return 0;
2845     }
2846
2847     return -EINVAL;
2848 }
2849 #endif
2850 #if CONFIG_SENSOR_Flash
2851 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2852 {    
2853     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2854         if (value == 3) {       /* ddl@rock-chips.com: torch */
2855             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2856         } else {
2857             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2858         }
2859         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2860         return 0;
2861     }
2862     
2863         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2864     return -EINVAL;
2865 }
2866 #endif
2867
2868 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2869 {
2870     struct i2c_client *client = sd->priv;
2871     struct sensor *sensor = to_sensor(client);
2872     const struct v4l2_queryctrl *qctrl;
2873
2874     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2875
2876     if (!qctrl)
2877     {
2878         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2879         return -EINVAL;
2880     }
2881
2882     switch (ctrl->id)
2883     {
2884         case V4L2_CID_BRIGHTNESS:
2885             {
2886                 ctrl->value = sensor->info_priv.brightness;
2887                 break;
2888             }
2889         case V4L2_CID_SATURATION:
2890             {
2891                 ctrl->value = sensor->info_priv.saturation;
2892                 break;
2893             }
2894         case V4L2_CID_CONTRAST:
2895             {
2896                 ctrl->value = sensor->info_priv.contrast;
2897                 break;
2898             }
2899         case V4L2_CID_DO_WHITE_BALANCE:
2900             {
2901                 ctrl->value = sensor->info_priv.whiteBalance;
2902                 break;
2903             }
2904         case V4L2_CID_EXPOSURE:
2905             {
2906                 ctrl->value = sensor->info_priv.exposure;
2907                 break;
2908             }
2909         case V4L2_CID_HFLIP:
2910             {
2911                 ctrl->value = sensor->info_priv.mirror;
2912                 break;
2913             }
2914         case V4L2_CID_VFLIP:
2915             {
2916                 ctrl->value = sensor->info_priv.flip;
2917                 break;
2918             }
2919         default :
2920                 break;
2921     }
2922     return 0;
2923 }
2924
2925
2926
2927 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2928 {
2929     struct i2c_client *client = sd->priv;
2930     struct sensor *sensor = to_sensor(client);
2931     struct soc_camera_device *icd = client->dev.platform_data;
2932     const struct v4l2_queryctrl *qctrl;
2933
2934
2935     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2936
2937     if (!qctrl)
2938     {
2939         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2940         return -EINVAL;
2941     }
2942
2943     switch (ctrl->id)
2944     {
2945 #if CONFIG_SENSOR_Brightness
2946         case V4L2_CID_BRIGHTNESS:
2947             {
2948                 if (ctrl->value != sensor->info_priv.brightness)
2949                 {
2950                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2951                     {
2952                         return -EINVAL;
2953                     }
2954                     sensor->info_priv.brightness = ctrl->value;
2955                 }
2956                 break;
2957             }
2958 #endif
2959 #if CONFIG_SENSOR_Exposure
2960         case V4L2_CID_EXPOSURE:
2961             {
2962                 if (ctrl->value != sensor->info_priv.exposure)
2963                 {
2964                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2965                     {
2966                         return -EINVAL;
2967                     }
2968                     sensor->info_priv.exposure = ctrl->value;
2969                 }
2970                 break;
2971             }
2972 #endif
2973 #if CONFIG_SENSOR_Saturation
2974         case V4L2_CID_SATURATION:
2975             {
2976                 if (ctrl->value != sensor->info_priv.saturation)
2977                 {
2978                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2979                     {
2980                         return -EINVAL;
2981                     }
2982                     sensor->info_priv.saturation = ctrl->value;
2983                 }
2984                 break;
2985             }
2986 #endif
2987 #if CONFIG_SENSOR_Contrast
2988         case V4L2_CID_CONTRAST:
2989             {
2990                 if (ctrl->value != sensor->info_priv.contrast)
2991                 {
2992                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2993                     {
2994                         return -EINVAL;
2995                     }
2996                     sensor->info_priv.contrast = ctrl->value;
2997                 }
2998                 break;
2999             }
3000 #endif
3001 #if CONFIG_SENSOR_WhiteBalance
3002         case V4L2_CID_DO_WHITE_BALANCE:
3003             {
3004                 if (ctrl->value != sensor->info_priv.whiteBalance)
3005                 {
3006                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3007                     {
3008                         return -EINVAL;
3009                     }
3010                     sensor->info_priv.whiteBalance = ctrl->value;
3011                 }
3012                 break;
3013             }
3014 #endif
3015 #if CONFIG_SENSOR_Mirror
3016         case V4L2_CID_HFLIP:
3017             {
3018                 if (ctrl->value != sensor->info_priv.mirror)
3019                 {
3020                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3021                         return -EINVAL;
3022                     sensor->info_priv.mirror = ctrl->value;
3023                 }
3024                 break;
3025             }
3026 #endif
3027 #if CONFIG_SENSOR_Flip
3028         case V4L2_CID_VFLIP:
3029             {
3030                 if (ctrl->value != sensor->info_priv.flip)
3031                 {
3032                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3033                         return -EINVAL;
3034                     sensor->info_priv.flip = ctrl->value;
3035                 }
3036                 break;
3037             }
3038 #endif
3039         default:
3040             break;
3041     }
3042
3043     return 0;
3044 }
3045 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3046 {
3047     const struct v4l2_queryctrl *qctrl;
3048     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3049     struct sensor *sensor = to_sensor(client);
3050
3051     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3052
3053     if (!qctrl)
3054     {
3055         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3056         return -EINVAL;
3057     }
3058
3059     switch (ext_ctrl->id)
3060     {
3061         case V4L2_CID_SCENE:
3062             {
3063                 ext_ctrl->value = sensor->info_priv.scene;
3064                 break;
3065             }
3066         case V4L2_CID_EFFECT:
3067             {
3068                 ext_ctrl->value = sensor->info_priv.effect;
3069                 break;
3070             }
3071         case V4L2_CID_ZOOM_ABSOLUTE:
3072             {
3073                 ext_ctrl->value = sensor->info_priv.digitalzoom;
3074                 break;
3075             }
3076         case V4L2_CID_ZOOM_RELATIVE:
3077             {
3078                 return -EINVAL;
3079             }
3080         case V4L2_CID_FOCUS_ABSOLUTE:
3081             {
3082                 ext_ctrl->value = sensor->info_priv.focus;
3083                 break;
3084             }
3085         case V4L2_CID_FOCUS_RELATIVE:
3086             {
3087                 return -EINVAL;
3088             }
3089         case V4L2_CID_FLASH:
3090             {
3091                 ext_ctrl->value = sensor->info_priv.flash;
3092                 break;
3093             }
3094         default :
3095             break;
3096     }
3097     return 0;
3098 }
3099 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3100 {
3101     const struct v4l2_queryctrl *qctrl;
3102     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3103     struct sensor *sensor = to_sensor(client);
3104     int val_offset;
3105
3106     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3107
3108     if (!qctrl)
3109     {
3110         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3111         return -EINVAL;
3112     }
3113
3114         val_offset = 0;
3115     switch (ext_ctrl->id)
3116     {
3117 #if CONFIG_SENSOR_Scene
3118         case V4L2_CID_SCENE:
3119             {
3120                 if (ext_ctrl->value != sensor->info_priv.scene)
3121                 {
3122                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3123                         return -EINVAL;
3124                     sensor->info_priv.scene = ext_ctrl->value;
3125                 }
3126                 break;
3127             }
3128 #endif
3129 #if CONFIG_SENSOR_Effect
3130         case V4L2_CID_EFFECT:
3131             {
3132                 if (ext_ctrl->value != sensor->info_priv.effect)
3133                 {
3134                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3135                         return -EINVAL;
3136                     sensor->info_priv.effect= ext_ctrl->value;
3137                 }
3138                 break;
3139             }
3140 #endif
3141 #if CONFIG_SENSOR_DigitalZoom
3142         case V4L2_CID_ZOOM_ABSOLUTE:
3143             {
3144                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3145                     return -EINVAL;
3146
3147                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3148                 {
3149                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3150
3151                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3152                         return -EINVAL;
3153                     sensor->info_priv.digitalzoom += val_offset;
3154
3155                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
3156                 }
3157
3158                 break;
3159             }
3160         case V4L2_CID_ZOOM_RELATIVE:
3161             {
3162                 if (ext_ctrl->value)
3163                 {
3164                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3165                         return -EINVAL;
3166                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3167
3168                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3169                 }
3170                 break;
3171             }
3172 #endif
3173 #if CONFIG_SENSOR_Focus
3174         case V4L2_CID_FOCUS_ABSOLUTE:
3175             {
3176                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3177                     return -EINVAL;
3178
3179                 if (ext_ctrl->value != sensor->info_priv.focus)
3180                 {
3181                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
3182
3183                     sensor->info_priv.focus += val_offset;
3184                 }
3185
3186                 break;
3187             }
3188         case V4L2_CID_FOCUS_RELATIVE:
3189             {
3190                 if (ext_ctrl->value)
3191                 {
3192                     sensor->info_priv.focus += ext_ctrl->value;
3193
3194                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
3195                 }
3196                 break;
3197             }
3198 #endif
3199 #if CONFIG_SENSOR_Flash
3200         case V4L2_CID_FLASH:
3201             {
3202                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3203                     return -EINVAL;
3204                 sensor->info_priv.flash = ext_ctrl->value;
3205
3206                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3207                 break;
3208             }
3209 #endif
3210         default:
3211             break;
3212     }
3213
3214     return 0;
3215 }
3216
3217 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3218 {
3219     struct i2c_client *client = sd->priv;
3220     struct soc_camera_device *icd = client->dev.platform_data;
3221     int i, error_cnt=0, error_idx=-1;
3222
3223
3224     for (i=0; i<ext_ctrl->count; i++) {
3225         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3226             error_cnt++;
3227             error_idx = i;
3228         }
3229     }
3230
3231     if (error_cnt > 1)
3232         error_idx = ext_ctrl->count;
3233
3234     if (error_idx != -1) {
3235         ext_ctrl->error_idx = error_idx;
3236         return -EINVAL;
3237     } else {
3238         return 0;
3239     }
3240 }
3241
3242 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3243 {
3244     struct i2c_client *client = sd->priv;
3245     struct soc_camera_device *icd = client->dev.platform_data;
3246     int i, error_cnt=0, error_idx=-1;
3247
3248
3249     for (i=0; i<ext_ctrl->count; i++) {
3250         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3251             error_cnt++;
3252             error_idx = i;
3253         }
3254     }
3255
3256     if (error_cnt > 1)
3257         error_idx = ext_ctrl->count;
3258
3259     if (error_idx != -1) {
3260         ext_ctrl->error_idx = error_idx;
3261         return -EINVAL;
3262     } else {
3263         return 0;
3264     }
3265 }
3266
3267 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3268  * this wasn't our capture interface, so, we wait for the right one */
3269 static int sensor_video_probe(struct soc_camera_device *icd,
3270                                struct i2c_client *client)
3271 {
3272     char value;
3273     int ret;
3274     struct sensor *sensor = to_sensor(client);
3275
3276     /* We must have a parent by now. And it cannot be a wrong one.
3277      * So this entire test is completely redundant. */
3278     if (!icd->dev.parent ||
3279             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3280                 return -ENODEV;
3281
3282         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3283                 ret = -ENODEV;
3284                 goto sensor_video_probe_err;
3285         }
3286
3287     /* soft reset */
3288     ret = sensor_write(client, 0x01, 0xf9);
3289     if (ret != 0)
3290     {
3291         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3292         return -ENODEV;
3293     }
3294     mdelay(5);          //delay 5 microseconds
3295
3296     /* check if it is an sensor sensor */
3297     ret = sensor_read(client, 0x04, &value);
3298     if (ret != 0) {
3299         SENSOR_TR("read chip id high byte failed\n");
3300         ret = -ENODEV;
3301         goto sensor_video_probe_err;
3302     }
3303
3304     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), value);
3305     if (value == SENSOR_ID) {
3306         sensor->model = SENSOR_V4L2_IDENT;
3307     } else {
3308         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), value);
3309         ret = -ENODEV;
3310         goto sensor_video_probe_err;
3311     }
3312
3313                 
3314     icd->formats = sensor_colour_formats;
3315     icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
3316
3317     return 0;
3318
3319 sensor_video_probe_err:
3320
3321     return ret;
3322 }
3323 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3324 {
3325         struct i2c_client *client = sd->priv;
3326     struct soc_camera_device *icd = client->dev.platform_data;
3327     struct sensor *sensor = to_sensor(client);
3328     int ret = 0;
3329 #if CONFIG_SENSOR_Flash 
3330     int i;
3331 #endif
3332     
3333         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3334         switch (cmd)
3335         {
3336                 case RK29_CAM_SUBDEV_DEACTIVATE:
3337                 {
3338                         sensor_deactivate(client);
3339                         break;
3340                 }
3341
3342                 case RK29_CAM_SUBDEV_IOREQUEST:
3343                 {
3344                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
3345             if (sensor->sensor_io_request != NULL) { 
3346                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
3347                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
3348                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
3349                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
3350                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
3351                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
3352                 }
3353             } else {
3354                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3355                 ret = -EINVAL;
3356                 goto sensor_ioctl_end;
3357             }
3358             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
3359                for this project */
3360             #if CONFIG_SENSOR_Flash     
3361                 if (sensor->sensor_gpio_res) { 
3362                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3363                     for (i = 0; i < icd->ops->num_controls; i++) {
3364                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3365                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
3366                                 }
3367                     }
3368                     sensor->info_priv.flash = 0xff;
3369                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3370                 }
3371                 }
3372             #endif
3373                         break;
3374                 }
3375                 default:
3376                 {
3377                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3378                         break;
3379                 }
3380         }
3381 sensor_ioctl_end:
3382         return ret;
3383
3384 }
3385 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3386         .init           = sensor_init,
3387         .g_ctrl         = sensor_g_control,
3388         .s_ctrl         = sensor_s_control,
3389         .g_ext_ctrls          = sensor_g_ext_controls,
3390         .s_ext_ctrls          = sensor_s_ext_controls,
3391         .g_chip_ident   = sensor_g_chip_ident,
3392         .ioctl = sensor_ioctl,
3393 };
3394
3395 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3396         .s_fmt          = sensor_s_fmt,
3397         .g_fmt          = sensor_g_fmt,
3398         .try_fmt        = sensor_try_fmt,
3399 };
3400
3401 static struct v4l2_subdev_ops sensor_subdev_ops = {
3402         .core   = &sensor_subdev_core_ops,
3403         .video = &sensor_subdev_video_ops,
3404 };
3405
3406 static int sensor_probe(struct i2c_client *client,
3407                          const struct i2c_device_id *did)
3408 {
3409     struct sensor *sensor;
3410     struct soc_camera_device *icd = client->dev.platform_data;
3411     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3412     struct soc_camera_link *icl;
3413     int ret;
3414
3415     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3416     if (!icd) {
3417         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3418         return -EINVAL;
3419     }
3420
3421     icl = to_soc_camera_link(icd);
3422     if (!icl) {
3423         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3424         return -EINVAL;
3425     }
3426
3427     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3428         dev_warn(&adapter->dev,
3429                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3430         return -EIO;
3431     }
3432
3433     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3434     if (!sensor)
3435         return -ENOMEM;
3436
3437     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3438
3439     /* Second stage probe - when a capture adapter is there */
3440     icd->ops            = &sensor_ops;
3441     icd->y_skip_top             = 0;
3442         #if CONFIG_SENSOR_I2C_NOSCHED
3443         atomic_set(&sensor->tasklock_cnt,0);
3444         #endif
3445
3446     ret = sensor_video_probe(icd, client);
3447     if (ret < 0) {
3448         icd->ops = NULL;
3449         i2c_set_clientdata(client, NULL);
3450         kfree(sensor);
3451                 sensor = NULL;
3452     }
3453     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3454     return ret;
3455 }
3456
3457 static int sensor_remove(struct i2c_client *client)
3458 {
3459     struct sensor *sensor = to_sensor(client);
3460     struct soc_camera_device *icd = client->dev.platform_data;
3461
3462     icd->ops = NULL;
3463     i2c_set_clientdata(client, NULL);
3464     client->driver = NULL;
3465     kfree(sensor);
3466         sensor = NULL;
3467     return 0;
3468 }
3469
3470 static const struct i2c_device_id sensor_id[] = {
3471         {SENSOR_NAME_STRING(), 0 },
3472         { }
3473 };
3474 MODULE_DEVICE_TABLE(i2c, sensor_id);
3475
3476 static struct i2c_driver sensor_i2c_driver = {
3477         .driver = {
3478                 .name = SENSOR_NAME_STRING(),
3479         },
3480         .probe          = sensor_probe,
3481         .remove         = sensor_remove,
3482         .id_table       = sensor_id,
3483 };
3484
3485 static int __init sensor_mod_init(void)
3486 {
3487     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3488     return i2c_add_driver(&sensor_i2c_driver);
3489 }
3490
3491 static void __exit sensor_mod_exit(void)
3492 {
3493     i2c_del_driver(&sensor_i2c_driver);
3494 }
3495
3496 device_initcall_sync(sensor_mod_init);
3497 module_exit(sensor_mod_exit);
3498
3499 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3500 MODULE_AUTHOR("lxh@wisky.com.cn");
3501 MODULE_LICENSE("GPL");
3502
3503
3504