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