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