camera rk30:update to v0.x.10,wake up vb when cif have't receive data etc.
[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 #if 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         hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2293     flash_off_timer.icd = icd;
2294         flash_off_timer.timer.function = flash_off_func;
2295     #endif
2296
2297     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);
2298
2299     sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
2300     return 0;
2301 sensor_INIT_ERR:
2302     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2303         sensor_task_lock(client,0);
2304         sensor_deactivate(client);
2305     return ret;
2306 }
2307
2308 static int sensor_deactivate(struct i2c_client *client)
2309 {
2310         struct soc_camera_device *icd = client->dev.platform_data;
2311     struct sensor *sensor = to_sensor(client);
2312         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2313
2314         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2315         sensor_ioctrl(icd, Sensor_PowerDown, 1);
2316
2317         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2318         icd->user_width = SENSOR_INIT_WIDTH;
2319     icd->user_height = SENSOR_INIT_HEIGHT;
2320         msleep(100);
2321     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2322         return 0;
2323 }
2324
2325 static  struct reginfo sensor_power_down_sequence[]=
2326 {
2327     {0x00,0x00}
2328 };
2329 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2330 {
2331     int ret;
2332     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2333
2334     if (pm_msg.event == PM_EVENT_SUSPEND) {
2335         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2336         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2337         if (ret != 0) {
2338             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2339             return ret;
2340         } else {
2341             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2342             if (ret < 0) {
2343                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2344                 return -EINVAL;
2345             }
2346         }
2347     } else {
2348         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2349         return -EINVAL;
2350     }
2351     return 0;
2352 }
2353
2354 static int sensor_resume(struct soc_camera_device *icd)
2355 {
2356         int ret;
2357
2358     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2359     if (ret < 0) {
2360                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2361         return -EINVAL;
2362     }
2363
2364         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2365
2366     return 0;
2367
2368 }
2369
2370 static int sensor_set_bus_param(struct soc_camera_device *icd,
2371                                 unsigned long flags)
2372 {
2373
2374     return 0;
2375 }
2376
2377 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2378 {
2379     struct soc_camera_link *icl = to_soc_camera_link(icd);
2380     unsigned long flags = SENSOR_BUS_PARAM;
2381
2382     return soc_camera_apply_sensor_flags(icl, flags);
2383 }
2384
2385 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2386 {
2387     struct i2c_client *client = v4l2_get_subdevdata(sd);
2388     struct soc_camera_device *icd = client->dev.platform_data;
2389     struct sensor *sensor = to_sensor(client);
2390
2391     mf->width   = icd->user_width;
2392         mf->height      = icd->user_height;
2393         mf->code        = sensor->info_priv.fmt.code;
2394         mf->colorspace  = sensor->info_priv.fmt.colorspace;
2395         mf->field       = V4L2_FIELD_NONE;
2396
2397     return 0;
2398 }
2399 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2400 {
2401     bool ret = false;
2402
2403         if ((mf->width == 1024) && (mf->height == 768)) {
2404                 ret = true;
2405         } else if ((mf->width == 1280) && (mf->height == 1024)) {
2406                 ret = true;
2407         } else if ((mf->width == 1600) && (mf->height == 1200)) {
2408                 ret = true;
2409         } else if ((mf->width == 2048) && (mf->height == 1536)) {
2410                 ret = true;
2411         } else if ((mf->width == 2592) && (mf->height == 1944)) {
2412                 ret = true;
2413         }
2414
2415         if (ret == true)
2416                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2417         return ret;
2418 }
2419
2420 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2421 {
2422     bool ret = false;
2423
2424         if ((mf->width == 1280) && (mf->height == 720)) {
2425                 ret = true;
2426         } else if ((mf->width == 1920) && (mf->height == 1080)) {
2427                 ret = true;
2428         }
2429
2430         if (ret == true)
2431                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2432         return ret;
2433 }
2434 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2435 {
2436     struct i2c_client *client = v4l2_get_subdevdata(sd);
2437     struct sensor *sensor = to_sensor(client);
2438     const struct sensor_datafmt *fmt;
2439         const struct v4l2_queryctrl *qctrl;
2440         struct soc_camera_device *icd = client->dev.platform_data;
2441     struct reginfo *winseqe_set_addr=NULL;
2442     int ret=0, set_w,set_h;
2443
2444         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2445                                    ARRAY_SIZE(sensor_colour_fmts));
2446         if (!fmt) {
2447         ret = -EINVAL;
2448         goto sensor_s_fmt_end;
2449     }
2450
2451         if (sensor->info_priv.fmt.code != mf->code) {
2452                 switch (mf->code)
2453                 {
2454                         case V4L2_MBUS_FMT_YUYV8_2X8:
2455                         {
2456                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2457                                 break;
2458                         }
2459                         case V4L2_MBUS_FMT_UYVY8_2X8:
2460                         {
2461                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2462                                 break;
2463                         }
2464                         default:
2465                                 break;
2466                 }
2467                 if (winseqe_set_addr != NULL) {
2468             sensor_write_array(client, winseqe_set_addr);
2469                         sensor->info_priv.fmt.code = mf->code;
2470             sensor->info_priv.fmt.colorspace= mf->colorspace;            
2471                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2472                 } else {
2473                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2474                 }
2475         }
2476
2477     set_w = mf->width;
2478     set_h = mf->height;
2479
2480         if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg != END_REG))
2481         {
2482                 winseqe_set_addr = sensor_qcif;
2483         set_w = 176;
2484         set_h = 144;
2485         }
2486         else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg != END_REG))
2487     {
2488         winseqe_set_addr = sensor_qvga;
2489         set_w = 320;
2490         set_h = 240;
2491     }
2492     else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg != END_REG))
2493     {
2494         winseqe_set_addr = sensor_cif;
2495         set_w = 352;
2496         set_h = 288;
2497     }
2498     else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg != END_REG))
2499     {
2500         winseqe_set_addr = sensor_vga;
2501         set_w = 640;
2502         set_h = 480;
2503     }
2504     else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg != END_REG))
2505     {
2506         winseqe_set_addr = sensor_svga;
2507         set_w = 800;
2508         set_h = 600;
2509     }
2510         else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg != END_REG))
2511     {
2512         winseqe_set_addr = sensor_xga;
2513         set_w = 1024;
2514         set_h = 768;
2515     }
2516     else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg != END_REG))
2517     {
2518         winseqe_set_addr = sensor_sxga;
2519         set_w = 1280;
2520         set_h = 1024;
2521     }
2522     else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg != END_REG))
2523     {
2524         winseqe_set_addr = sensor_uxga;
2525         set_w = 1600;
2526         set_h = 1200;
2527     }
2528     else
2529     {
2530         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2531         set_w = SENSOR_INIT_WIDTH;
2532         set_h = SENSOR_INIT_HEIGHT;
2533
2534                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2535     }
2536
2537     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
2538         #if CONFIG_SENSOR_Flash
2539         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
2540             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2541                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2542                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2543             }           
2544         } else {                                        /* ddl@rock-chips.com : Video */
2545             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2546                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2547                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2548             }
2549         }
2550         #endif
2551         ret |= sensor_write_array(client, winseqe_set_addr);
2552         if (ret != 0) {
2553             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2554             #if CONFIG_SENSOR_Flash
2555             if (sensor_fmt_capturechk(sd,f) == true) {
2556                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2557                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2558                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2559                 }
2560             }
2561             #endif
2562             goto sensor_s_fmt_end;
2563         }
2564
2565         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
2566
2567                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
2568         #if CONFIG_SENSOR_Effect
2569                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2570                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2571         #endif
2572         #if CONFIG_SENSOR_WhiteBalance
2573                         if (sensor->info_priv.whiteBalance != 0) {
2574                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2575                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2576                         }
2577         #endif
2578                         sensor->info_priv.snap2preview = true;
2579                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
2580                 #if CONFIG_SENSOR_Effect
2581                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2582                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2583         #endif
2584         #if CONFIG_SENSOR_WhiteBalance
2585                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2586                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2587         #endif
2588                         sensor->info_priv.video2preview = true;
2589                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2590                 #if CONFIG_SENSOR_Effect
2591                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2592                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2593         #endif
2594         #if CONFIG_SENSOR_WhiteBalance
2595                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2596                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2597         #endif
2598                         sensor->info_priv.video2preview = false;
2599                         sensor->info_priv.snap2preview = false;
2600                 }
2601
2602         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2603     }
2604     else
2605     {
2606         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2607     }
2608
2609         mf->width = set_w;
2610     mf->height = set_h;
2611
2612 sensor_s_fmt_end:
2613     return ret;
2614 }
2615
2616 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2617 {
2618     struct i2c_client *client = v4l2_get_subdevdata(sd);
2619     struct sensor *sensor = to_sensor(client);
2620     const struct sensor_datafmt *fmt;
2621     int ret = 0;
2622    
2623         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2624                                    ARRAY_SIZE(sensor_colour_fmts));
2625         if (fmt == NULL) {
2626                 fmt = &sensor->info_priv.fmt;
2627         mf->code = fmt->code;
2628         } 
2629
2630     if (mf->height > SENSOR_MAX_HEIGHT)
2631         mf->height = SENSOR_MAX_HEIGHT;
2632     else if (mf->height < SENSOR_MIN_HEIGHT)
2633         mf->height = SENSOR_MIN_HEIGHT;
2634
2635     if (mf->width > SENSOR_MAX_WIDTH)
2636         mf->width = SENSOR_MAX_WIDTH;
2637     else if (mf->width < SENSOR_MIN_WIDTH)
2638         mf->width = SENSOR_MIN_WIDTH;
2639
2640     mf->colorspace = fmt->colorspace;
2641     
2642     return ret;
2643 }
2644
2645  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2646 {
2647     struct i2c_client *client = v4l2_get_subdevdata(sd);
2648
2649     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2650         return -EINVAL;
2651
2652     if (id->match.addr != client->addr)
2653         return -ENODEV;
2654
2655     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2656     id->revision = 0;
2657
2658     return 0;
2659 }
2660 #if CONFIG_SENSOR_Brightness
2661 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2662 {
2663     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2664
2665     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2666     {
2667         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2668         {
2669             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2670             {
2671                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2672                 return -EINVAL;
2673             }
2674             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2675             return 0;
2676         }
2677     }
2678         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2679     return -EINVAL;
2680 }
2681 #endif
2682 #if CONFIG_SENSOR_Effect
2683 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2684 {
2685     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2686
2687     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2688     {
2689         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2690         {
2691             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2692             {
2693                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2694                 return -EINVAL;
2695             }
2696             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2697             return 0;
2698         }
2699     }
2700         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2701     return -EINVAL;
2702 }
2703 #endif
2704 #if CONFIG_SENSOR_Exposure
2705 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2706 {
2707     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2708
2709     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2710     {
2711         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2712         {
2713             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2714             {
2715                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2716                 return -EINVAL;
2717             }
2718             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2719             return 0;
2720         }
2721     }
2722         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2723     return -EINVAL;
2724 }
2725 #endif
2726 #if CONFIG_SENSOR_Saturation
2727 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2728 {
2729     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2730
2731     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2732     {
2733         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2734         {
2735             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2736             {
2737                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2738                 return -EINVAL;
2739             }
2740             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2741             return 0;
2742         }
2743     }
2744     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2745     return -EINVAL;
2746 }
2747 #endif
2748 #if CONFIG_SENSOR_Contrast
2749 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2750 {
2751     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2752
2753     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2754     {
2755         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2756         {
2757             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2758             {
2759                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2760                 return -EINVAL;
2761             }
2762             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2763             return 0;
2764         }
2765     }
2766     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2767     return -EINVAL;
2768 }
2769 #endif
2770 #if CONFIG_SENSOR_Mirror
2771 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2772 {
2773     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2774
2775     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2776     {
2777         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2778         {
2779             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2780             {
2781                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2782                 return -EINVAL;
2783             }
2784             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2785             return 0;
2786         }
2787     }
2788     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2789     return -EINVAL;
2790 }
2791 #endif
2792 #if CONFIG_SENSOR_Flip
2793 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2794 {
2795     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2796
2797     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2798     {
2799         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2800         {
2801             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2802             {
2803                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2804                 return -EINVAL;
2805             }
2806             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2807             return 0;
2808         }
2809     }
2810     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2811     return -EINVAL;
2812 }
2813 #endif
2814 #if CONFIG_SENSOR_Scene
2815 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2816 {
2817     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2818
2819     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2820     {
2821         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2822         {
2823             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2824             {
2825                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2826                 return -EINVAL;
2827             }
2828             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2829             return 0;
2830         }
2831     }
2832     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2833     return -EINVAL;
2834 }
2835 #endif
2836 #if CONFIG_SENSOR_WhiteBalance
2837 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2838 {
2839     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2840
2841     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2842     {
2843         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2844         {
2845             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2846             {
2847                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2848                 return -EINVAL;
2849             }
2850             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2851             return 0;
2852         }
2853     }
2854         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2855     return -EINVAL;
2856 }
2857 #endif
2858 #if CONFIG_SENSOR_DigitalZoom
2859 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2860 {
2861     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2862     struct sensor *sensor = to_sensor(client);
2863         const struct v4l2_queryctrl *qctrl_info;
2864     int digitalzoom_cur, digitalzoom_total;
2865
2866         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2867         if (qctrl_info)
2868                 return -EINVAL;
2869
2870     digitalzoom_cur = sensor->info_priv.digitalzoom;
2871     digitalzoom_total = qctrl_info->maximum;
2872
2873     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
2874     {
2875         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2876         return -EINVAL;
2877     }
2878
2879     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2880     {
2881         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2882         return -EINVAL;
2883     }
2884
2885     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
2886     {
2887         *value = digitalzoom_total - digitalzoom_cur;
2888     }
2889
2890     if ((value < 0) && ((digitalzoom_cur + value) < 0))
2891     {
2892         value = 0 - digitalzoom_cur;
2893     }
2894
2895     digitalzoom_cur += value;
2896
2897     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2898     {
2899         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2900         {
2901             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2902             return -EINVAL;
2903         }
2904         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2905         return 0;
2906     }
2907
2908     return -EINVAL;
2909 }
2910 #endif
2911 #if CONFIG_SENSOR_Flash
2912 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2913 {    
2914     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2915         if (value == 3) {       /* ddl@rock-chips.com: torch */
2916             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2917         } else {
2918             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2919         }
2920         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2921         return 0;
2922     }
2923     
2924         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2925     return -EINVAL;
2926 }
2927 #endif
2928
2929 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2930 {
2931     struct i2c_client *client = v4l2_get_subdevdata(sd);
2932     struct sensor *sensor = to_sensor(client);
2933     const struct v4l2_queryctrl *qctrl;
2934
2935     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2936
2937     if (!qctrl)
2938     {
2939         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2940         return -EINVAL;
2941     }
2942
2943     switch (ctrl->id)
2944     {
2945         case V4L2_CID_BRIGHTNESS:
2946             {
2947                 ctrl->value = sensor->info_priv.brightness;
2948                 break;
2949             }
2950         case V4L2_CID_SATURATION:
2951             {
2952                 ctrl->value = sensor->info_priv.saturation;
2953                 break;
2954             }
2955         case V4L2_CID_CONTRAST:
2956             {
2957                 ctrl->value = sensor->info_priv.contrast;
2958                 break;
2959             }
2960         case V4L2_CID_DO_WHITE_BALANCE:
2961             {
2962                 ctrl->value = sensor->info_priv.whiteBalance;
2963                 break;
2964             }
2965         case V4L2_CID_EXPOSURE:
2966             {
2967                 ctrl->value = sensor->info_priv.exposure;
2968                 break;
2969             }
2970         case V4L2_CID_HFLIP:
2971             {
2972                 ctrl->value = sensor->info_priv.mirror;
2973                 break;
2974             }
2975         case V4L2_CID_VFLIP:
2976             {
2977                 ctrl->value = sensor->info_priv.flip;
2978                 break;
2979             }
2980         default :
2981                 break;
2982     }
2983     return 0;
2984 }
2985
2986
2987
2988 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2989 {
2990     struct i2c_client *client = v4l2_get_subdevdata(sd);
2991     struct sensor *sensor = to_sensor(client);
2992     struct soc_camera_device *icd = client->dev.platform_data;
2993     const struct v4l2_queryctrl *qctrl;
2994
2995
2996     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2997
2998     if (!qctrl)
2999     {
3000         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3001         return -EINVAL;
3002     }
3003
3004     switch (ctrl->id)
3005     {
3006 #if CONFIG_SENSOR_Brightness
3007         case V4L2_CID_BRIGHTNESS:
3008             {
3009                 if (ctrl->value != sensor->info_priv.brightness)
3010                 {
3011                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3012                     {
3013                         return -EINVAL;
3014                     }
3015                     sensor->info_priv.brightness = ctrl->value;
3016                 }
3017                 break;
3018             }
3019 #endif
3020 #if CONFIG_SENSOR_Exposure
3021         case V4L2_CID_EXPOSURE:
3022             {
3023                 if (ctrl->value != sensor->info_priv.exposure)
3024                 {
3025                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3026                     {
3027                         return -EINVAL;
3028                     }
3029                     sensor->info_priv.exposure = ctrl->value;
3030                 }
3031                 break;
3032             }
3033 #endif
3034 #if CONFIG_SENSOR_Saturation
3035         case V4L2_CID_SATURATION:
3036             {
3037                 if (ctrl->value != sensor->info_priv.saturation)
3038                 {
3039                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3040                     {
3041                         return -EINVAL;
3042                     }
3043                     sensor->info_priv.saturation = ctrl->value;
3044                 }
3045                 break;
3046             }
3047 #endif
3048 #if CONFIG_SENSOR_Contrast
3049         case V4L2_CID_CONTRAST:
3050             {
3051                 if (ctrl->value != sensor->info_priv.contrast)
3052                 {
3053                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3054                     {
3055                         return -EINVAL;
3056                     }
3057                     sensor->info_priv.contrast = ctrl->value;
3058                 }
3059                 break;
3060             }
3061 #endif
3062 #if CONFIG_SENSOR_WhiteBalance
3063         case V4L2_CID_DO_WHITE_BALANCE:
3064             {
3065                 if (ctrl->value != sensor->info_priv.whiteBalance)
3066                 {
3067                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3068                     {
3069                         return -EINVAL;
3070                     }
3071                     sensor->info_priv.whiteBalance = ctrl->value;
3072                 }
3073                 break;
3074             }
3075 #endif
3076 #if CONFIG_SENSOR_Mirror
3077         case V4L2_CID_HFLIP:
3078             {
3079                 if (ctrl->value != sensor->info_priv.mirror)
3080                 {
3081                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3082                         return -EINVAL;
3083                     sensor->info_priv.mirror = ctrl->value;
3084                 }
3085                 break;
3086             }
3087 #endif
3088 #if CONFIG_SENSOR_Flip
3089         case V4L2_CID_VFLIP:
3090             {
3091                 if (ctrl->value != sensor->info_priv.flip)
3092                 {
3093                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3094                         return -EINVAL;
3095                     sensor->info_priv.flip = ctrl->value;
3096                 }
3097                 break;
3098             }
3099 #endif
3100         default:
3101             break;
3102     }
3103
3104     return 0;
3105 }
3106 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3107 {
3108     const struct v4l2_queryctrl *qctrl;
3109     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3110     struct sensor *sensor = to_sensor(client);
3111
3112     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3113
3114     if (!qctrl)
3115     {
3116         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3117         return -EINVAL;
3118     }
3119
3120     switch (ext_ctrl->id)
3121     {
3122         case V4L2_CID_SCENE:
3123             {
3124                 ext_ctrl->value = sensor->info_priv.scene;
3125                 break;
3126             }
3127         case V4L2_CID_EFFECT:
3128             {
3129                 ext_ctrl->value = sensor->info_priv.effect;
3130                 break;
3131             }
3132         case V4L2_CID_ZOOM_ABSOLUTE:
3133             {
3134                 ext_ctrl->value = sensor->info_priv.digitalzoom;
3135                 break;
3136             }
3137         case V4L2_CID_ZOOM_RELATIVE:
3138             {
3139                 return -EINVAL;
3140             }
3141         case V4L2_CID_FOCUS_ABSOLUTE:
3142             {
3143                 ext_ctrl->value = sensor->info_priv.focus;
3144                 break;
3145             }
3146         case V4L2_CID_FOCUS_RELATIVE:
3147             {
3148                 return -EINVAL;
3149             }
3150         case V4L2_CID_FLASH:
3151             {
3152                 ext_ctrl->value = sensor->info_priv.flash;
3153                 break;
3154             }
3155         default :
3156             break;
3157     }
3158     return 0;
3159 }
3160 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3161 {
3162     const struct v4l2_queryctrl *qctrl;
3163     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3164     struct sensor *sensor = to_sensor(client);
3165     int val_offset;
3166
3167     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3168
3169     if (!qctrl)
3170     {
3171         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3172         return -EINVAL;
3173     }
3174
3175         val_offset = 0;
3176     switch (ext_ctrl->id)
3177     {
3178 #if CONFIG_SENSOR_Scene
3179         case V4L2_CID_SCENE:
3180             {
3181                 if (ext_ctrl->value != sensor->info_priv.scene)
3182                 {
3183                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3184                         return -EINVAL;
3185                     sensor->info_priv.scene = ext_ctrl->value;
3186                 }
3187                 break;
3188             }
3189 #endif
3190 #if CONFIG_SENSOR_Effect
3191         case V4L2_CID_EFFECT:
3192             {
3193                 if (ext_ctrl->value != sensor->info_priv.effect)
3194                 {
3195                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3196                         return -EINVAL;
3197                     sensor->info_priv.effect= ext_ctrl->value;
3198                 }
3199                 break;
3200             }
3201 #endif
3202 #if CONFIG_SENSOR_DigitalZoom
3203         case V4L2_CID_ZOOM_ABSOLUTE:
3204             {
3205                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3206                     return -EINVAL;
3207
3208                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3209                 {
3210                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3211
3212                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3213                         return -EINVAL;
3214                     sensor->info_priv.digitalzoom += val_offset;
3215
3216                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
3217                 }
3218
3219                 break;
3220             }
3221         case V4L2_CID_ZOOM_RELATIVE:
3222             {
3223                 if (ext_ctrl->value)
3224                 {
3225                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3226                         return -EINVAL;
3227                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3228
3229                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3230                 }
3231                 break;
3232             }
3233 #endif
3234 #if CONFIG_SENSOR_Focus
3235         case V4L2_CID_FOCUS_ABSOLUTE:
3236             {
3237                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3238                     return -EINVAL;
3239
3240                 if (ext_ctrl->value != sensor->info_priv.focus)
3241                 {
3242                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
3243
3244                     sensor->info_priv.focus += val_offset;
3245                 }
3246
3247                 break;
3248             }
3249         case V4L2_CID_FOCUS_RELATIVE:
3250             {
3251                 if (ext_ctrl->value)
3252                 {
3253                     sensor->info_priv.focus += ext_ctrl->value;
3254
3255                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
3256                 }
3257                 break;
3258             }
3259 #endif
3260 #if CONFIG_SENSOR_Flash
3261         case V4L2_CID_FLASH:
3262             {
3263                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3264                     return -EINVAL;
3265                 sensor->info_priv.flash = ext_ctrl->value;
3266
3267                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3268                 break;
3269             }
3270 #endif
3271         default:
3272             break;
3273     }
3274
3275     return 0;
3276 }
3277
3278 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3279 {
3280     struct i2c_client *client = v4l2_get_subdevdata(sd);
3281     struct soc_camera_device *icd = client->dev.platform_data;
3282     int i, error_cnt=0, error_idx=-1;
3283
3284
3285     for (i=0; i<ext_ctrl->count; i++) {
3286         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3287             error_cnt++;
3288             error_idx = i;
3289         }
3290     }
3291
3292     if (error_cnt > 1)
3293         error_idx = ext_ctrl->count;
3294
3295     if (error_idx != -1) {
3296         ext_ctrl->error_idx = error_idx;
3297         return -EINVAL;
3298     } else {
3299         return 0;
3300     }
3301 }
3302
3303 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3304 {
3305     struct i2c_client *client = v4l2_get_subdevdata(sd);
3306     struct soc_camera_device *icd = client->dev.platform_data;
3307     int i, error_cnt=0, error_idx=-1;
3308
3309
3310     for (i=0; i<ext_ctrl->count; i++) {
3311         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3312             error_cnt++;
3313             error_idx = i;
3314         }
3315     }
3316
3317     if (error_cnt > 1)
3318         error_idx = ext_ctrl->count;
3319
3320     if (error_idx != -1) {
3321         ext_ctrl->error_idx = error_idx;
3322         return -EINVAL;
3323     } else {
3324         return 0;
3325     }
3326 }
3327
3328 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3329  * this wasn't our capture interface, so, we wait for the right one */
3330 static int sensor_video_probe(struct soc_camera_device *icd,
3331                                struct i2c_client *client)
3332 {
3333     char value;
3334     int ret;
3335     struct sensor *sensor = to_sensor(client);
3336
3337     /* We must have a parent by now. And it cannot be a wrong one.
3338      * So this entire test is completely redundant. */
3339     if (!icd->dev.parent ||
3340             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3341                 return -ENODEV;
3342
3343         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3344                 ret = -ENODEV;
3345                 goto sensor_video_probe_err;
3346         }
3347
3348     /* soft reset */
3349     ret = sensor_write(client, 0x01, 0xf9);
3350     if (ret != 0)
3351     {
3352         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3353         return -ENODEV;
3354     }
3355     mdelay(5);          //delay 5 microseconds
3356
3357     /* check if it is an sensor sensor */
3358     ret = sensor_read(client, 0x04, &value);
3359     if (ret != 0) {
3360         SENSOR_TR("read chip id high byte failed\n");
3361         ret = -ENODEV;
3362         goto sensor_video_probe_err;
3363     }
3364
3365     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), value);
3366     if (value == SENSOR_ID) {
3367         sensor->model = SENSOR_V4L2_IDENT;
3368     } else {
3369         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), value);
3370         ret = -ENODEV;
3371         goto sensor_video_probe_err;
3372     }
3373         
3374     return 0;
3375
3376 sensor_video_probe_err:
3377
3378     return ret;
3379 }
3380 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3381 {
3382         struct i2c_client *client = v4l2_get_subdevdata(sd);
3383     struct soc_camera_device *icd = client->dev.platform_data;
3384     struct sensor *sensor = to_sensor(client);
3385     int ret = 0;
3386 #if CONFIG_SENSOR_Flash 
3387     int i;
3388 #endif
3389     
3390         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3391         switch (cmd)
3392         {
3393                 case RK29_CAM_SUBDEV_DEACTIVATE:
3394                 {
3395                         sensor_deactivate(client);
3396                         break;
3397                 }
3398
3399                 case RK29_CAM_SUBDEV_IOREQUEST:
3400                 {
3401                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
3402             if (sensor->sensor_io_request != NULL) { 
3403                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
3404                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
3405                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
3406                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
3407                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
3408                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
3409                 }
3410             } else {
3411                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3412                 ret = -EINVAL;
3413                 goto sensor_ioctl_end;
3414             }
3415             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
3416                for this project */
3417             #if CONFIG_SENSOR_Flash     
3418                 if (sensor->sensor_gpio_res) { 
3419                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3420                     for (i = 0; i < icd->ops->num_controls; i++) {
3421                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3422                                         //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));  
3423                               sensor_controls[i].id=0xffff;                             
3424                                 }
3425                     }
3426                     sensor->info_priv.flash = 0xff;
3427                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3428                 }else{ //two cameras are the same,need to deal diffrently ,zyc
3429                     for (i = 0; i < icd->ops->num_controls; i++) {
3430                            if(0xffff == icd->ops->controls[i].id){
3431                               sensor_controls[i].id=V4L2_CID_FLASH;
3432                            }               
3433                     }
3434                 }
3435                 }
3436             #endif
3437                         break;
3438                 }
3439                 default:
3440                 {
3441                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3442                         break;
3443                 }
3444         }
3445 sensor_ioctl_end:
3446         return ret;
3447
3448 }
3449 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3450                             enum v4l2_mbus_pixelcode *code)
3451 {
3452         if (index >= ARRAY_SIZE(sensor_colour_fmts))
3453                 return -EINVAL;
3454
3455         *code = sensor_colour_fmts[index].code;
3456         return 0;
3457 }
3458 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3459         .init           = sensor_init,
3460         .g_ctrl         = sensor_g_control,
3461         .s_ctrl         = sensor_s_control,
3462         .g_ext_ctrls          = sensor_g_ext_controls,
3463         .s_ext_ctrls          = sensor_s_ext_controls,
3464         .g_chip_ident   = sensor_g_chip_ident,
3465         .ioctl = sensor_ioctl,
3466 };
3467
3468
3469 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3470         .s_mbus_fmt     = sensor_s_fmt,
3471         .g_mbus_fmt     = sensor_g_fmt,
3472         .try_mbus_fmt   = sensor_try_fmt,
3473         .enum_mbus_fmt  = sensor_enum_fmt,
3474 };
3475
3476 static struct v4l2_subdev_ops sensor_subdev_ops = {
3477         .core   = &sensor_subdev_core_ops,
3478         .video = &sensor_subdev_video_ops,
3479 };
3480
3481 static int sensor_probe(struct i2c_client *client,
3482                          const struct i2c_device_id *did)
3483 {
3484     struct sensor *sensor;
3485     struct soc_camera_device *icd = client->dev.platform_data;
3486     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3487     struct soc_camera_link *icl;
3488     int ret;
3489
3490     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3491     if (!icd) {
3492         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3493         return -EINVAL;
3494     }
3495
3496     icl = to_soc_camera_link(icd);
3497     if (!icl) {
3498         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3499         return -EINVAL;
3500     }
3501
3502     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3503         dev_warn(&adapter->dev,
3504                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3505         return -EIO;
3506     }
3507
3508     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3509     if (!sensor)
3510         return -ENOMEM;
3511
3512     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3513
3514     /* Second stage probe - when a capture adapter is there */
3515     icd->ops            = &sensor_ops;
3516     sensor->info_priv.fmt = sensor_colour_fmts[0];
3517         #if CONFIG_SENSOR_I2C_NOSCHED
3518         atomic_set(&sensor->tasklock_cnt,0);
3519         #endif
3520
3521     ret = sensor_video_probe(icd, client);
3522     if (ret < 0) {
3523         icd->ops = NULL;
3524         i2c_set_clientdata(client, NULL);
3525         kfree(sensor);
3526                 sensor = NULL;
3527     }
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