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