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