camera: sensor driver support 1M capture
[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 /* 1024X768 XGA */
647 static struct reginfo sensor_xga[] =
648 {
649         {0x3800 ,0x1 },
650         {0x3801 ,0x8A},
651         {0x3802 ,0x0 },
652         {0x3803 ,0xA },
653         {0x3804 ,0xA },
654         {0x3805 ,0x20},
655         {0x3806 ,0x7 },
656         {0x3807 ,0x98},
657         {0x3808 ,0x4 },
658         {0x3809 ,0x0 },
659         {0x380a ,0x3 },
660         {0x380b ,0x0 },
661         {0x380c ,0xc },
662         {0x380d ,0x80},
663         {0x380e ,0x7 },
664         {0x380f ,0xd0},
665         {0x5001 ,0x7f},
666         {0x5680 ,0x0 },
667         {0x5681 ,0x0 },
668         {0x5682 ,0xA },
669         {0x5683 ,0x20},
670         {0x5684 ,0x0 },
671         {0x5685 ,0x0 },
672         {0x5686 ,0x7 },
673         {0x5687 ,0x98},
674         {SEQUENCE_END, 0x00}
675 };
676 /* 800X600 SVGA*/
677 static struct reginfo sensor_svga[] =
678 {
679         {0x3820 ,0x41},
680         {0x3821 ,0x7 },
681         {0x3814 ,0x31},
682         {0x3815 ,0x31},
683         {0x3803 ,0x4 },
684         {0x3807 ,0x9b},
685         {0x3808 ,0x2 },
686         {0x3809 ,0x80},
687         {0x380a ,0x1 },
688         {0x380b ,0xe0},
689         {0x380c ,0x7 },
690         {0x380d ,0x68},
691         {0x380e ,0x3 },
692         {0x380f ,0xd8},
693         {0x3813 ,0x6 },
694         {0x3618 ,0x0 },
695         {0x3612 ,0x49},
696         {0x3708 ,0x62},
697         {0x3709 ,0x52},
698         {0x370c ,0x3 },
699         {0x3a02 ,0x3 },
700         {0x3a03 ,0xd8},
701         {0x3a0e ,0x3 },
702         {0x3a0d ,0x4 },
703         {0x3a14 ,0x3 },
704         {0x3a15 ,0xd8},
705         {0x4004 ,0x2 },
706         {0x5000 ,0xa7},
707         {0x5181 ,0xf2},
708         {0x5182 ,0x0 },
709         {0x5197 ,0x1 },
710         {0x519e ,0x38},
711         {0x3035 ,0x21},
712         {0x5000 ,0xa7},
713         {0x5001 ,0xa3},
714         {0x3035 ,0x21},
715         {0x4713 ,0x3 },
716         {0x3036 ,0x46},
717         {0x4407 ,0x4 },
718         {0x460b ,0x35},
719         {0x460c ,0x22},
720         {0x3824 ,0x2 },
721         {SEQUENCE_END, 0x00}
722 };
723
724 /* 640X480 VGA */
725 static struct reginfo sensor_vga[] =
726 {
727
728         {SEQUENCE_END, 0x00}
729 };
730 /* 352X288 CIF */
731 static struct reginfo sensor_cif[] =
732 {
733
734         {SEQUENCE_END, 0x00}
735 };
736
737 /* 320*240 QVGA */
738 static  struct reginfo sensor_qvga[] =
739 {
740
741         {SEQUENCE_END, 0x00}
742 };
743
744 /* 176X144 QCIF*/
745 static struct reginfo sensor_qcif[] =
746 {
747
748         {SEQUENCE_END, 0x00}
749 };
750
751 static  struct reginfo sensor_ClrFmt_YUYV[]=
752 {
753         {0x4300,0x30},
754         {SEQUENCE_END, 0x00}
755 };
756
757 static  struct reginfo sensor_ClrFmt_UYVY[]=
758 {
759         {0x4300,0x32},
760         {SEQUENCE_END, 0x00}
761 };
762
763
764 #if CONFIG_SENSOR_WhiteBalance
765 static  struct reginfo sensor_WhiteB_Auto[]=
766 {
767         {0x3406 ,0x00},
768         {0x5183 ,0x94},
769         {0x5191 ,0xff},
770         {0x5192 ,0x00},
771         {SEQUENCE_END, 0x00}
772 };
773 /* Cloudy Colour Temperature : 6500K - 8000K  */
774 static  struct reginfo sensor_WhiteB_Cloudy[]=
775 {
776         {0x3406 ,0x1 },
777         {0x3400 ,0x6 },
778         {0x3401 ,0x48},
779         {0x3402 ,0x4 },
780         {0x3403 ,0x0 },
781         {0x3404 ,0x4 },
782         {0x3405 ,0xd3 },
783         {SEQUENCE_END, 0x00}
784 };
785 /* ClearDay Colour Temperature : 5000K - 6500K  */
786 static  struct reginfo sensor_WhiteB_ClearDay[]=
787 {
788         {0x3406 ,0x1 },
789         {0x3400 ,0x6 },
790         {0x3401 ,0x1c},
791         {0x3402 ,0x4 },
792         {0x3403 ,0x0 },
793         {0x3404 ,0x4 },
794         {0x3405 ,0xf3},
795         {SEQUENCE_END, 0x00}
796 };
797 /* Office Colour Temperature : 3500K - 5000K  */
798 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
799 {
800         {0x3406 ,0x1 },
801         {0x3400 ,0x5 },
802         {0x3401 ,0x48},
803         {0x3402 ,0x4 },
804         {0x3403 ,0x0 },
805         {0x3404 ,0x7 },
806         {0x3405 ,0xcf},
807         {SEQUENCE_END, 0x00}
808 };
809 /* Home Colour Temperature : 2500K - 3500K  */
810 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
811 {
812         {0x3406 ,0x1 },
813         {0x3400 ,0x4 },
814         {0x3401 ,0x10},
815         {0x3402 ,0x4 },
816         {0x3403 ,0x0 },
817         {0x3404 ,0x8 },
818         {0x3405 ,0xb6},
819         {SEQUENCE_END, 0x00}
820 };
821 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
822     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
823 };
824 #endif
825
826 #if CONFIG_SENSOR_Brightness
827 static  struct reginfo sensor_Brightness0[]=
828 {
829         {SEQUENCE_END, 0x00}
830 };
831 static  struct reginfo sensor_Brightness1[]=
832 {
833         {SEQUENCE_END, 0x00}
834 };
835
836 static  struct reginfo sensor_Brightness2[]=
837 {
838         {SEQUENCE_END, 0x00}
839 };
840 static  struct reginfo sensor_Brightness3[]=
841 {
842         {SEQUENCE_END, 0x00}
843 };
844 static  struct reginfo sensor_Brightness4[]=
845 {
846         {SEQUENCE_END, 0x00}
847 };
848
849 static  struct reginfo sensor_Brightness5[]=
850 {
851         {SEQUENCE_END, 0x00}
852 };
853 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
854     sensor_Brightness4, sensor_Brightness5,NULL,
855 };
856
857 #endif
858
859 #if CONFIG_SENSOR_Effect
860 static  struct reginfo sensor_Effect_Normal[] =
861 {
862     {0x5001, 0x7f},
863         {0x5580, 0x00},
864         {SEQUENCE_END, 0x00}
865 };
866 static  struct reginfo sensor_Effect_WandB[] =
867 {
868     {0x5001, 0xff},
869         {0x5580, 0x18},
870         {0x5585, 0x80},
871         {0x5586, 0x80},
872         {SEQUENCE_END, 0x00}
873 };
874 static  struct reginfo sensor_Effect_Sepia[] =
875 {
876     {0x5001, 0xff},
877         {0x5580, 0x18},
878         {0x5585, 0x40},
879         {0x5586, 0xa0},
880         {SEQUENCE_END, 0x00}
881 };
882
883 static  struct reginfo sensor_Effect_Negative[] =
884 {
885     //Negative
886     {0x5001, 0xff},
887         {0x5580, 0x40},
888         {SEQUENCE_END, 0x00}
889 };static  struct reginfo sensor_Effect_Bluish[] =
890 {
891     // Bluish
892     {0x5001, 0xff},
893         {0x5580, 0x18},
894         {0x5585, 0xa0},
895         {0x5586, 0x40},
896         {SEQUENCE_END, 0x00}
897 };
898
899 static  struct reginfo sensor_Effect_Green[] =
900 {
901     //  Greenish
902     {0x5001, 0xff},
903         {0x5580, 0x18},
904         {0x5585, 0x60},
905         {0x5586, 0x60},
906         {SEQUENCE_END, 0x00}
907 };
908 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
909     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
910 };
911 #endif
912 #if CONFIG_SENSOR_Exposure
913 static  struct reginfo sensor_Exposure0[]=
914 {
915         {SEQUENCE_END, 0x00}
916 };
917 static  struct reginfo sensor_Exposure1[]=
918 {
919         {SEQUENCE_END, 0x00}
920 };
921 static  struct reginfo sensor_Exposure2[]=
922 {
923         {SEQUENCE_END, 0x00}
924 };
925
926 static  struct reginfo sensor_Exposure3[]=
927 {
928         {SEQUENCE_END, 0x00}
929 };
930 static  struct reginfo sensor_Exposure4[]=
931 {
932         {SEQUENCE_END, 0x00}
933 };
934 static  struct reginfo sensor_Exposure5[]=
935 {
936         {SEQUENCE_END, 0x00}
937 };
938 static  struct reginfo sensor_Exposure6[]=
939 {
940         {SEQUENCE_END, 0x00}
941 };
942 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
943     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
944 };
945 #endif
946 #if CONFIG_SENSOR_Saturation
947 static  struct reginfo sensor_Saturation0[]=
948 {
949         {SEQUENCE_END, 0x00}
950 };
951
952 static  struct reginfo sensor_Saturation1[]=
953 {
954         {SEQUENCE_END, 0x00}
955 };
956
957 static  struct reginfo sensor_Saturation2[]=
958 {
959         {SEQUENCE_END, 0x00}
960 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
961
962 #endif
963 #if CONFIG_SENSOR_Contrast
964 static  struct reginfo sensor_Contrast0[]=
965 {
966         {SEQUENCE_END, 0x00}
967 };
968
969 static  struct reginfo sensor_Contrast1[]=
970 {
971         {SEQUENCE_END, 0x00}
972 };
973 static  struct reginfo sensor_Contrast2[]=
974 {
975         {SEQUENCE_END, 0x00}
976 };
977
978 static  struct reginfo sensor_Contrast3[]=
979 {
980         {SEQUENCE_END, 0x00}
981 };
982
983 static  struct reginfo sensor_Contrast4[]=
984 {
985         {SEQUENCE_END, 0x00}
986 };
987
988
989 static  struct reginfo sensor_Contrast5[]=
990 {
991         {SEQUENCE_END, 0x00}
992 };
993
994 static  struct reginfo sensor_Contrast6[]=
995 {
996         {SEQUENCE_END, 0x00}
997 };
998 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
999     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1000 };
1001
1002 #endif
1003 #if CONFIG_SENSOR_Mirror
1004 static  struct reginfo sensor_MirrorOn[]=
1005 {
1006         {SEQUENCE_END, 0x00}
1007 };
1008 static  struct reginfo sensor_MirrorOff[]=
1009 {
1010         {SEQUENCE_END, 0x00}
1011 };
1012 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1013 #endif
1014 #if CONFIG_SENSOR_Flip
1015 static  struct reginfo sensor_FlipOn[]=
1016 {
1017         {SEQUENCE_END, 0x00}
1018 };
1019
1020 static  struct reginfo sensor_FlipOff[]=
1021 {
1022         {SEQUENCE_END, 0x00}
1023 };
1024 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1025
1026 #endif
1027 #if CONFIG_SENSOR_Scene
1028 static  struct reginfo sensor_SceneAuto[] =
1029 {
1030         {0x3a00 , 0x78},
1031         {SEQUENCE_END, 0x00}
1032 };
1033 static  struct reginfo sensor_SceneNight[] =
1034 {
1035     //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1036         {0x3034 ,0x1a},
1037         {0x3035 ,0x21},
1038         {0x3036 ,0x46},
1039         {0x3037 ,0x13},
1040         {0x3038 ,0x00},
1041         {0x3039 ,0x00},
1042         {0x3a00 ,0x7c},
1043         {0x3a08 ,0x01},
1044         {0x3a09 ,0x27},
1045         {0x3a0a ,0x00},
1046         {0x3a0b ,0xf6},
1047         {0x3a0d ,0x04},
1048         {0x3a0e ,0x04},
1049         {0x3a02 ,0x0b},
1050         {0x3a03 ,0x88},
1051         {0x3a14 ,0x0b},
1052         {0x3a15 ,0x88},
1053         {SEQUENCE_END, 0x00}
1054 };
1055 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1056
1057 #endif
1058 #if CONFIG_SENSOR_DigitalZoom
1059 static struct reginfo sensor_Zoom0[] =
1060 {
1061         {SEQUENCE_END, 0x00}
1062 };
1063 static struct reginfo sensor_Zoom1[] =
1064 {
1065         {SEQUENCE_END, 0x00}
1066 };
1067
1068 static struct reginfo sensor_Zoom2[] =
1069 {
1070         {SEQUENCE_END, 0x00}
1071 };
1072
1073
1074 static struct reginfo sensor_Zoom3[] =
1075 {
1076         {SEQUENCE_END, 0x00}
1077 };
1078 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1079 #endif
1080 static const struct v4l2_querymenu sensor_menus[] =
1081 {
1082         #if CONFIG_SENSOR_WhiteBalance
1083     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1084     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1085     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1086     #endif
1087
1088         #if CONFIG_SENSOR_Effect
1089     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1090     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1091     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
1092     #endif
1093
1094         #if CONFIG_SENSOR_Scene
1095     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1096     #endif
1097
1098         #if CONFIG_SENSOR_Flash
1099     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1100     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1101     #endif
1102 };
1103
1104 static const struct v4l2_queryctrl sensor_controls[] =
1105 {
1106         #if CONFIG_SENSOR_WhiteBalance
1107     {
1108         .id             = V4L2_CID_DO_WHITE_BALANCE,
1109         .type           = V4L2_CTRL_TYPE_MENU,
1110         .name           = "White Balance Control",
1111         .minimum        = 0,
1112         .maximum        = 4,
1113         .step           = 1,
1114         .default_value = 0,
1115     },
1116     #endif
1117
1118         #if CONFIG_SENSOR_Brightness
1119         {
1120         .id             = V4L2_CID_BRIGHTNESS,
1121         .type           = V4L2_CTRL_TYPE_INTEGER,
1122         .name           = "Brightness Control",
1123         .minimum        = -3,
1124         .maximum        = 2,
1125         .step           = 1,
1126         .default_value = 0,
1127     },
1128     #endif
1129
1130         #if CONFIG_SENSOR_Effect
1131         {
1132         .id             = V4L2_CID_EFFECT,
1133         .type           = V4L2_CTRL_TYPE_MENU,
1134         .name           = "Effect Control",
1135         .minimum        = 0,
1136         .maximum        = 5,
1137         .step           = 1,
1138         .default_value = 0,
1139     },
1140         #endif
1141
1142         #if CONFIG_SENSOR_Exposure
1143         {
1144         .id             = V4L2_CID_EXPOSURE,
1145         .type           = V4L2_CTRL_TYPE_INTEGER,
1146         .name           = "Exposure Control",
1147         .minimum        = 0,
1148         .maximum        = 6,
1149         .step           = 1,
1150         .default_value = 0,
1151     },
1152         #endif
1153
1154         #if CONFIG_SENSOR_Saturation
1155         {
1156         .id             = V4L2_CID_SATURATION,
1157         .type           = V4L2_CTRL_TYPE_INTEGER,
1158         .name           = "Saturation Control",
1159         .minimum        = 0,
1160         .maximum        = 2,
1161         .step           = 1,
1162         .default_value = 0,
1163     },
1164     #endif
1165
1166         #if CONFIG_SENSOR_Contrast
1167         {
1168         .id             = V4L2_CID_CONTRAST,
1169         .type           = V4L2_CTRL_TYPE_INTEGER,
1170         .name           = "Contrast Control",
1171         .minimum        = -3,
1172         .maximum        = 3,
1173         .step           = 1,
1174         .default_value = 0,
1175     },
1176         #endif
1177
1178         #if CONFIG_SENSOR_Mirror
1179         {
1180         .id             = V4L2_CID_HFLIP,
1181         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1182         .name           = "Mirror Control",
1183         .minimum        = 0,
1184         .maximum        = 1,
1185         .step           = 1,
1186         .default_value = 1,
1187     },
1188     #endif
1189
1190         #if CONFIG_SENSOR_Flip
1191         {
1192         .id             = V4L2_CID_VFLIP,
1193         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1194         .name           = "Flip Control",
1195         .minimum        = 0,
1196         .maximum        = 1,
1197         .step           = 1,
1198         .default_value = 1,
1199     },
1200     #endif
1201
1202         #if CONFIG_SENSOR_Scene
1203     {
1204         .id             = V4L2_CID_SCENE,
1205         .type           = V4L2_CTRL_TYPE_MENU,
1206         .name           = "Scene Control",
1207         .minimum        = 0,
1208         .maximum        = 1,
1209         .step           = 1,
1210         .default_value = 0,
1211     },
1212     #endif
1213
1214         #if CONFIG_SENSOR_DigitalZoom
1215     {
1216         .id             = V4L2_CID_ZOOM_RELATIVE,
1217         .type           = V4L2_CTRL_TYPE_INTEGER,
1218         .name           = "DigitalZoom Control",
1219         .minimum        = -1,
1220         .maximum        = 1,
1221         .step           = 1,
1222         .default_value = 0,
1223     }, {
1224         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1225         .type           = V4L2_CTRL_TYPE_INTEGER,
1226         .name           = "DigitalZoom Control",
1227         .minimum        = 0,
1228         .maximum        = 3,
1229         .step           = 1,
1230         .default_value = 0,
1231     },
1232     #endif
1233
1234         #if CONFIG_SENSOR_Focus
1235         {
1236         .id             = V4L2_CID_FOCUS_RELATIVE,
1237         .type           = V4L2_CTRL_TYPE_INTEGER,
1238         .name           = "Focus Control",
1239         .minimum        = -1,
1240         .maximum        = 1,
1241         .step           = 1,
1242         .default_value = 0,
1243     }, {
1244         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1245         .type           = V4L2_CTRL_TYPE_INTEGER,
1246         .name           = "Focus Control",
1247         .minimum        = 0,
1248         .maximum        = 255,
1249         .step           = 1,
1250         .default_value = 125,
1251     },
1252         {
1253         .id             = V4L2_CID_FOCUS_AUTO,
1254         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1255         .name           = "Focus Control",
1256         .minimum        = 0,
1257         .maximum        = 1,
1258         .step           = 1,
1259         .default_value = 0,
1260     },{
1261         .id             = V4L2_CID_FOCUS_CONTINUOUS,
1262         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1263         .name           = "Focus Control",
1264         .minimum        = 0,
1265         .maximum        = 1,
1266         .step           = 1,
1267         .default_value = 0,
1268     },
1269     #endif
1270
1271         #if CONFIG_SENSOR_Flash
1272         {
1273         .id             = V4L2_CID_FLASH,
1274         .type           = V4L2_CTRL_TYPE_MENU,
1275         .name           = "Flash Control",
1276         .minimum        = 0,
1277         .maximum        = 3,
1278         .step           = 1,
1279         .default_value = 0,
1280     },
1281         #endif
1282 };
1283
1284 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1285 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1286 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1287 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1288 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1289 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1290 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1291 static int sensor_resume(struct soc_camera_device *icd);
1292 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1293 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1294 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1295 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1296 static int sensor_deactivate(struct i2c_client *client);
1297
1298 static struct soc_camera_ops sensor_ops =
1299 {
1300     .suspend                     = sensor_suspend,
1301     .resume                       = sensor_resume,
1302     .set_bus_param              = sensor_set_bus_param,
1303     .query_bus_param    = sensor_query_bus_param,
1304     .controls           = sensor_controls,
1305     .menus                         = sensor_menus,
1306     .num_controls               = ARRAY_SIZE(sensor_controls),
1307     .num_menus          = ARRAY_SIZE(sensor_menus),
1308 };
1309
1310 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1311         { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1312         .colorspace = _colorspace }
1313
1314 #define JPG_FMT(_name, _depth, _fourcc) \
1315         COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1316
1317 static const struct soc_camera_data_format sensor_colour_formats[] = {
1318         JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1319         JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1320 };
1321 enum sensor_work_state
1322 {
1323         sensor_work_ready = 0,
1324         sensor_working,
1325 };
1326 struct sensor_work
1327 {
1328         struct i2c_client *client;
1329         struct delayed_work dwork;
1330         enum sensor_work_state state;
1331 };
1332
1333 typedef struct sensor_info_priv_s
1334 {
1335     int whiteBalance;
1336     int brightness;
1337     int contrast;
1338     int saturation;
1339     int effect;
1340     int scene;
1341     int digitalzoom;
1342     int focus;
1343         int auto_focus;
1344         int affm_reinit;
1345     int flash;
1346     int exposure;
1347         bool snap2preview;
1348         bool video2preview;
1349     unsigned char mirror;                                        /* HFLIP */
1350     unsigned char flip;                                          /* VFLIP */
1351     struct reginfo *winseqe_cur_addr;
1352         unsigned int pixfmt;
1353         unsigned int enable;
1354         unsigned int funmodule_state;
1355 } sensor_info_priv_t;
1356
1357
1358
1359 struct sensor_parameter
1360 {
1361         unsigned short int preview_maxlines;
1362         unsigned short int preview_exposure;
1363         unsigned short int preview_line_width;
1364         unsigned short int preview_gain;
1365
1366         unsigned short int capture_framerate;
1367         unsigned short int preview_framerate;
1368         char awb[6];
1369 };
1370
1371 struct sensor
1372 {
1373     struct v4l2_subdev subdev;
1374     struct i2c_client *client;
1375     sensor_info_priv_t info_priv;
1376         struct sensor_parameter parameter;
1377         struct workqueue_struct *sensor_wq;
1378         struct sensor_work sensor_wk;
1379         struct mutex wq_lock;
1380     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1381 #if CONFIG_SENSOR_I2C_NOSCHED
1382         atomic_t tasklock_cnt;
1383 #endif
1384         struct rk29camera_platform_data *sensor_io_request;
1385 };
1386
1387 static struct sensor* to_sensor(const struct i2c_client *client)
1388 {
1389     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1390 }
1391
1392 static int sensor_task_lock(struct i2c_client *client, int lock)
1393 {
1394 #if CONFIG_SENSOR_I2C_NOSCHED
1395         int cnt = 3;
1396     struct sensor *sensor = to_sensor(client);
1397
1398         if (lock) {
1399                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1400                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1401                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1402                                 msleep(35);
1403                                 cnt--;
1404                         }
1405                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1406                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1407                                 goto sensor_task_lock_err;
1408                         }
1409                         preempt_disable();
1410                 }
1411
1412                 atomic_add(1, &sensor->tasklock_cnt);
1413         } else {
1414                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1415                         atomic_sub(1, &sensor->tasklock_cnt);
1416
1417                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1418                                 preempt_enable();
1419                 }
1420         }
1421 #endif
1422         return 0;
1423 sensor_task_lock_err:
1424         return -1;
1425 }
1426
1427 /* sensor register write */
1428 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1429 {
1430     int err,cnt;
1431     u8 buf[3];
1432     struct i2c_msg msg[1];
1433
1434     buf[0] = reg >> 8;
1435     buf[1] = reg & 0xFF;
1436     buf[2] = val;
1437
1438     msg->addr = client->addr;
1439     msg->flags = client->flags;
1440     msg->buf = buf;
1441     msg->len = sizeof(buf);
1442     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1443     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1444
1445     cnt = 3;
1446     err = -EAGAIN;
1447
1448     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1449         err = i2c_transfer(client->adapter, msg, 1);
1450
1451         if (err >= 0) {
1452             return 0;
1453         } else {
1454             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1455             udelay(10);
1456         }
1457     }
1458
1459     return err;
1460 }
1461
1462 /* sensor register read */
1463 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1464 {
1465     int err,cnt;
1466     u8 buf[2];
1467     struct i2c_msg msg[2];
1468
1469     buf[0] = reg >> 8;
1470     buf[1] = reg & 0xFF;
1471
1472     msg[0].addr = client->addr;
1473     msg[0].flags = client->flags;
1474     msg[0].buf = buf;
1475     msg[0].len = sizeof(buf);
1476     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1477     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1478
1479     msg[1].addr = client->addr;
1480     msg[1].flags = client->flags|I2C_M_RD;
1481     msg[1].buf = buf;
1482     msg[1].len = 1;
1483     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1484     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1485
1486     cnt = 3;
1487     err = -EAGAIN;
1488     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1489         err = i2c_transfer(client->adapter, msg, 2);
1490
1491         if (err >= 0) {
1492             *val = buf[0];
1493             return 0;
1494         } else {
1495                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1496             udelay(10);
1497         }
1498     }
1499
1500     return err;
1501 }
1502
1503 /* write a array of registers  */
1504 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1505 {
1506     int err = 0, cnt;
1507     int i = 0;
1508 #if CONFIG_SENSOR_Focus
1509         struct sensor *sensor = to_sensor(client);
1510 #endif
1511 #if CONFIG_SENSOR_I2C_RDWRCHK
1512         char valchk;
1513 #endif
1514
1515         cnt = 0;
1516         if (sensor_task_lock(client, 1) < 0)
1517                 goto sensor_write_array_end;
1518     while (regarray[i].reg != SEQUENCE_END)
1519     {
1520         #if CONFIG_SENSOR_Focus
1521         if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1522                         SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1523                         err = -EINVAL;
1524                         goto sensor_write_array_end;
1525         }
1526                 #endif
1527
1528         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1529         if (err < 0)
1530         {
1531             if (cnt-- > 0) {
1532                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1533                                 i = 0;
1534                                 continue;
1535             } else {
1536                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1537                 err = -EPERM;
1538                                 goto sensor_write_array_end;
1539             }
1540         } else {
1541         #if CONFIG_SENSOR_I2C_RDWRCHK
1542                         sensor_read(client, regarray[i].reg, &valchk);
1543                         if (valchk != regarray[i].val)
1544                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1545                 #endif
1546         }
1547
1548         i++;
1549     }
1550
1551         #if CONFIG_SENSOR_Focus
1552         if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1553                 || (regarray == sensor_init_data)) {
1554                 sensor->info_priv.affm_reinit = 1;
1555         }
1556         #endif
1557
1558 sensor_write_array_end:
1559         sensor_task_lock(client,0);
1560     return err;
1561 }
1562 #if CONFIG_SENSOR_I2C_RDWRCHK
1563 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1564 {
1565     int cnt;
1566     int i = 0;
1567         char valchk;
1568
1569         cnt = 0;
1570         valchk = 0;
1571     while (regarray[i].reg != 0)
1572     {
1573                 sensor_read(client, regarray[i].reg, &valchk);
1574                 if (valchk != regarray[i].val)
1575                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1576
1577         i++;
1578     }
1579     return 0;
1580 }
1581 #endif
1582 #if CONFIG_SENSOR_Focus
1583 struct af_cmdinfo
1584 {
1585         char cmd_tag;
1586         char cmd_para[4];
1587         char validate_bit;
1588 };
1589 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1590 {
1591         int i;
1592         char read_tag=0xff,cnt;
1593
1594         if (cmdinfo) {
1595                 if (cmdinfo->validate_bit & 0x80) {
1596                         if (sensor_write(client, CMD_TAG_Reg, cmdinfo->cmd_tag)) {
1597                                 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1598                                 goto sensor_af_cmdset_err;
1599                         }
1600                         SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1601                 }
1602                 for (i=0; i<4; i++) {
1603                         if (cmdinfo->validate_bit & (1<<i)) {
1604                                 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1605                                         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]);
1606                                         goto sensor_af_cmdset_err;
1607                                 }
1608                                 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]);
1609                         }
1610                 }
1611         } else {
1612                 if (sensor_write(client, CMD_TAG_Reg, 0xff)) {
1613                         SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1614                         goto sensor_af_cmdset_err;
1615                 }
1616                 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1617         }
1618
1619         if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1620                 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1621                 goto sensor_af_cmdset_err;
1622         }
1623
1624         cnt = 0;
1625         do
1626         {
1627                 msleep(5);
1628                 if (sensor_read(client,CMD_TAG_Reg,&read_tag)){
1629                    SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1630                    break;
1631                 }
1632     } while((read_tag != 0x00)&& (cnt++<100));
1633
1634         SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1635         return 0;
1636 sensor_af_cmdset_err:
1637         return -1;
1638 }
1639
1640 static int sensor_af_idlechk(struct i2c_client *client)
1641 {
1642         int ret = 0;
1643         char state,cnt;
1644
1645         cnt = 0;
1646         do
1647         {
1648                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1649                 if (ret != 0){
1650                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1651                    ret = -1;
1652                    goto sensor_af_idlechk_end;
1653                 }
1654
1655                 if (state != S_IDLE) {
1656                         sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
1657                         msleep(1);
1658                         cnt++;
1659                 }
1660     } while((state != S_IDLE)&& (cnt<100));
1661
1662         ret = (state == S_IDLE) ? 0 : -1;
1663
1664 sensor_af_idlechk_end:
1665         return ret;
1666 }
1667
1668 static int sensor_af_single(struct i2c_client *client)
1669 {
1670         int ret = 0;
1671         char state,cnt;
1672
1673         if (sensor_af_idlechk(client))
1674                 goto sensor_af_single_end;
1675
1676         if (sensor_af_cmdset(client, SingleFocus_Cmd, NULL)) {
1677                 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1678                 ret = -1;
1679                 goto sensor_af_single_end;
1680         }
1681
1682         cnt = 0;
1683     do
1684     {
1685         if (cnt != 0) {
1686                         msleep(1);
1687         }
1688         cnt++;
1689                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1690                 if (ret != 0){
1691                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1692                    ret = -1;
1693                    goto sensor_af_single_end;
1694                 }
1695     }while((state == S_FOCUSING) && (cnt<100));
1696
1697         if (state != S_FOCUSED) {
1698         SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1699                 ret = -1;
1700                 goto sensor_af_single_end;
1701     }
1702
1703         sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
1704 sensor_af_single_end:
1705         return ret;
1706 }
1707
1708 static int sensor_af_const(struct i2c_client *client)
1709 {
1710         int ret = 0;
1711
1712         if (sensor_af_idlechk(client))
1713                 goto sensor_af_const_end;
1714
1715         if (sensor_af_cmdset(client, ConstFocus_Cmd, NULL)) {
1716                 SENSOR_TR("%s const focus mode set error!\n",SENSOR_NAME_STRING());
1717                 ret = -1;
1718                 goto sensor_af_const_end;
1719         }
1720 sensor_af_const_end:
1721         return ret;
1722 }
1723 static int sensor_af_pause2capture(struct i2c_client *client)
1724 {
1725         int ret = 0;
1726         char state,cnt;
1727
1728         if (sensor_af_cmdset(client, PauseFocus_Cmd, NULL)) {
1729                 SENSOR_TR("%s pause focus mode set error!\n",SENSOR_NAME_STRING());
1730                 ret = -1;
1731                 goto sensor_af_pause_end;
1732         }
1733
1734         cnt = 0;
1735     do
1736     {
1737         if (cnt != 0) {
1738                         msleep(1);
1739         }
1740         cnt++;
1741                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1742                 if (ret != 0){
1743                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1744                    ret = -1;
1745                    goto sensor_af_pause_end;
1746                 }
1747     }while((state != S_CAPTURE) && (cnt<100));
1748
1749         if (state != S_CAPTURE) {
1750         SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1751                 ret = -1;
1752                 goto sensor_af_pause_end;
1753     }
1754 sensor_af_pause_end:
1755         return ret;
1756 }
1757 static int sensor_af_zoneupdate(struct i2c_client *client)
1758 {
1759         int ret = 0;
1760
1761         if (sensor_af_idlechk(client))
1762                 goto sensor_af_zoneupdate_end;
1763
1764         if (sensor_af_cmdset(client, UpdateZone_Cmd, NULL)) {
1765                 SENSOR_TR("%s update zone fail!\n",SENSOR_NAME_STRING());
1766                 ret = -1;
1767                 goto sensor_af_zoneupdate_end;
1768         }
1769
1770 sensor_af_zoneupdate_end:
1771         return ret;
1772 }
1773 static int sensor_af_init(struct i2c_client *client)
1774 {
1775         int ret = 0;
1776         char state,cnt;
1777
1778         ret = sensor_write_array(client, sensor_af_firmware);
1779     if (ret != 0) {
1780        SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1781        ret = -1;
1782            goto sensor_af_init_end;
1783     }
1784
1785         cnt = 0;
1786     do
1787     {
1788         if (cnt != 0) {
1789                         msleep(1);
1790         }
1791         cnt++;
1792                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1793                 if (ret != 0){
1794                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1795                    ret = -1;
1796                    goto sensor_af_init_end;
1797                 }
1798     }while((state == S_STARTUP) && (cnt<100));
1799
1800     if (state != S_IDLE) {
1801         SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1802                 ret = -1;
1803                 goto sensor_af_init_end;
1804     }
1805
1806 sensor_af_init_end:
1807         SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1808         return ret;
1809 }
1810
1811 static int sensor_af_wq_function(struct i2c_client *client)
1812 {
1813         struct sensor *sensor = to_sensor(client);
1814         struct af_cmdinfo cmdinfo;
1815         int ret=0, focus_pos = 0xfe;
1816
1817         SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1818
1819         mutex_lock(&sensor->wq_lock);
1820         if (sensor_af_init(client)) {
1821                 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1822                 ret = -1;
1823         } else {
1824                 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1825
1826                 switch (sensor->info_priv.auto_focus)
1827                 {
1828                         case SENSOR_AF_MODE_INFINITY:
1829                         {
1830                                 focus_pos = 0x00;
1831                         }
1832                         case SENSOR_AF_MODE_MACRO:
1833                         {
1834                                 if (focus_pos != 0x00)
1835                                         focus_pos = 0xff;
1836
1837                                 sensor_af_idlechk(client);
1838                                 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
1839                                 cmdinfo.cmd_para[0] = focus_pos;
1840                                 cmdinfo.validate_bit = 0x81;
1841                                 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
1842                                 break;
1843                         }
1844                         case SENSOR_AF_MODE_AUTO:
1845                         {
1846                                 ret = sensor_af_single(client);
1847                                 break;
1848                         }
1849                         case SENSOR_AF_MODE_CONTINUOUS:
1850                         {
1851                                 ret = sensor_af_const(client);
1852                                 break;
1853                         }
1854                         case SENSOR_AF_MODE_CLOSE:
1855                         {
1856                                 ret = 0;
1857                                 break;
1858                         }
1859                         default:
1860                                 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1861                 }
1862
1863                 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);
1864         }
1865
1866 sensor_af_wq_function_end:
1867         sensor->sensor_wk.state = sensor_work_ready;
1868         mutex_unlock(&sensor->wq_lock);
1869         return ret;
1870 }
1871 static void sensor_af_workqueue(struct work_struct *work)
1872 {
1873         struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
1874         struct i2c_client *client = sensor_work->client;
1875         struct sensor *sensor = to_sensor(client);
1876
1877         if (sensor_af_wq_function(client) < 0) {
1878                 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
1879         }
1880 }
1881 #endif
1882 static int sensor_parameter_record(struct i2c_client *client)
1883 {
1884         u8 ret_l,ret_m,ret_h;
1885         u8 tp_l,tp_m,tp_h;
1886         struct sensor *sensor = to_sensor(client);
1887
1888         sensor_write(client,0x3503,0x07);       //stop AE/AG
1889         sensor_write(client,0x3406,0x01);   //stop AWB
1890
1891         sensor_read(client,0x3500,&ret_h);
1892         sensor_read(client,0x3501, &ret_m);
1893         sensor_read(client,0x3502, &ret_l);
1894         tp_l = ret_l;
1895         tp_m = ret_m;
1896         tp_h = ret_h;
1897         SENSOR_DG(" %s Read 0x3500 = 0x%02x  0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
1898         sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
1899         sensor_read(client,0x350c, &ret_h);
1900         sensor_read(client,0x350d, &ret_l);
1901         sensor->parameter.preview_line_width = ret_h & 0xff;
1902         sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
1903         //Read back AGC Gain for preview
1904         sensor_read(client,0x350b, &tp_l);
1905         sensor->parameter.preview_gain = tp_l;
1906
1907         sensor->parameter.capture_framerate = 900;
1908         sensor->parameter.preview_framerate = 1500;
1909
1910         sensor_read(client,0x3400,&sensor->parameter.awb[0]);           //record awb value
1911         sensor_read(client,0x3401,&sensor->parameter.awb[1]);
1912         sensor_read(client,0x3402,&sensor->parameter.awb[2]);
1913         sensor_read(client,0x3403,&sensor->parameter.awb[3]);
1914         sensor_read(client,0x3404,&sensor->parameter.awb[4]);
1915         sensor_read(client,0x3405,&sensor->parameter.awb[5]);
1916
1917         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);
1918         return 0;
1919 }
1920 static int sensor_ae_transfer(struct i2c_client *client)
1921 {
1922         u8  ExposureLow;
1923         u8  ExposureMid;
1924         u8  ExposureHigh;
1925         u16 ulCapture_Exposure;
1926         u32 ulCapture_Exposure_Gain;
1927         u16  iCapture_Gain;
1928         u8   Lines_10ms;
1929         bool m_60Hz = 0;
1930         u8  reg_l = 0,reg_h =0;
1931         u16 Preview_Maxlines;
1932         u8  Gain;
1933         u32  Capture_MaxLines;
1934         struct sensor *sensor = to_sensor(client);
1935
1936         Preview_Maxlines = sensor->parameter.preview_line_width;
1937         Gain = sensor->parameter.preview_gain;
1938         sensor_read(client,0x350c, &reg_h);
1939         sensor_read(client,0x350d, &reg_l);
1940         Capture_MaxLines = reg_h & 0xff;
1941         Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
1942
1943         if(m_60Hz== 1) {
1944                 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
1945         } else {
1946                 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
1947         }
1948
1949         if(Preview_Maxlines == 0)
1950                 Preview_Maxlines = 1;
1951
1952         ulCapture_Exposure =
1953                 (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
1954         iCapture_Gain = (Gain & 0x0f) + 16;
1955         if (Gain & 0x10) {
1956                 iCapture_Gain = iCapture_Gain << 1;
1957         }
1958         if (Gain & 0x20) {
1959                 iCapture_Gain = iCapture_Gain << 1;
1960         }
1961         if (Gain & 0x40) {
1962                 iCapture_Gain = iCapture_Gain << 1;
1963         }
1964         if (Gain & 0x80) {
1965                 iCapture_Gain = iCapture_Gain << 1;
1966         }
1967         ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5);   //0ld value 2.5, ½â¾ö¹ýÁÁ
1968         if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
1969                 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
1970                 if (ulCapture_Exposure > Lines_10ms)
1971                 {
1972                         //ulCapture_Exposure *= 1.7;
1973                         ulCapture_Exposure /= Lines_10ms;
1974                         ulCapture_Exposure *= Lines_10ms;
1975                 }
1976         } else {
1977                 ulCapture_Exposure = Capture_MaxLines;
1978                 //ulCapture_Exposure_Gain *= 1.5;
1979         }
1980         if(ulCapture_Exposure == 0)
1981                 ulCapture_Exposure = 1;
1982         iCapture_Gain = (ulCapture_Exposure_Gain*2/ulCapture_Exposure + 1)/2;
1983         ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
1984         ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
1985         ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
1986
1987         Gain = 0;
1988         if (iCapture_Gain > 31) {
1989                 Gain |= 0x10;
1990                 iCapture_Gain = iCapture_Gain >> 1;
1991         }
1992         if (iCapture_Gain > 31) {
1993                 Gain |= 0x20;
1994                 iCapture_Gain = iCapture_Gain >> 1;
1995         }
1996         if (iCapture_Gain > 31) {
1997                 Gain |= 0x40;
1998                 iCapture_Gain = iCapture_Gain >> 1;
1999         }
2000         if (iCapture_Gain > 31) {
2001                 Gain |= 0x80;
2002                 iCapture_Gain = iCapture_Gain >> 1;
2003         }
2004         if (iCapture_Gain > 16)
2005                 Gain |= ((iCapture_Gain -16) & 0x0f);
2006         if(Gain == 0x10)
2007                 Gain = 0x11;
2008         // write the gain and exposure to 0x350* registers
2009         //m_iWrite0x350b=Gain;
2010         sensor_write(client,0x350b, Gain);
2011         //m_iWrite0x3502=ExposureLow;
2012         sensor_write(client,0x3502, ExposureLow);
2013         //m_iWrite0x3501=ExposureMid;
2014         sensor_write(client,0x3501, ExposureMid);
2015         //m_iWrite0x3500=ExposureHigh;
2016         sensor_write(client,0x3500, ExposureHigh);
2017         // SendToFile("Gain = 0x%x\r\n", Gain);
2018         // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2019         // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2020         // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2021         //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2022         //camera_timed_wait(200);
2023         //linzhk camera_timed_wait(500);
2024
2025         sensor_write(client,0x3400,sensor->parameter.awb[0]);           // resume awb value
2026         sensor_write(client,0x3401,sensor->parameter.awb[1]);
2027         sensor_write(client,0x3402,sensor->parameter.awb[2]);
2028         sensor_write(client,0x3403,sensor->parameter.awb[3]);
2029         sensor_write(client,0x3404,sensor->parameter.awb[4]);
2030         sensor_write(client,0x3405,sensor->parameter.awb[5]);
2031
2032         SENSOR_DG(" %s Write 0x350b = 0x%02x  0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2033         mdelay(100);
2034         return 0;
2035 }
2036 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2037 {
2038         struct soc_camera_link *icl = to_soc_camera_link(icd);
2039         int ret = 0;
2040
2041
2042         switch (cmd)
2043         {
2044                 case Sensor_PowerDown:
2045                 {
2046                         if (icl->powerdown) {
2047                                 ret = icl->powerdown(icd->pdev, on);
2048                                 if (ret == RK29_CAM_IO_SUCCESS) {
2049                                         if (on == 0) {
2050                                                 mdelay(2);
2051                                                 if (icl->reset)
2052                                                         icl->reset(icd->pdev);
2053                                         }
2054                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2055                                         ret = -ENODEV;
2056                                         goto sensor_power_end;
2057                                 }
2058                         }
2059                         break;
2060                 }
2061                 case Sensor_Flash:
2062                 {
2063                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2064                 struct sensor *sensor = to_sensor(client);
2065
2066                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2067                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2068                         }
2069                         break;
2070                 }
2071                 default:
2072                 {
2073                         SENSOR_TR("%s power cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2074                         break;
2075                 }
2076         }
2077
2078 sensor_power_end:
2079         return ret;
2080 }
2081 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2082 {
2083     struct i2c_client *client = sd->priv;
2084     struct soc_camera_device *icd = client->dev.platform_data;
2085     struct sensor *sensor = to_sensor(client);
2086         const struct v4l2_queryctrl *qctrl;
2087     char value;
2088     int ret,pid = 0;
2089
2090     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2091
2092         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2093                 ret = -ENODEV;
2094                 goto sensor_INIT_ERR;
2095         }
2096
2097     /* soft reset */
2098         if (sensor_task_lock(client,1)<0)
2099                 goto sensor_INIT_ERR;
2100     ret = sensor_write(client, 0x3008, 0x80);
2101     if (ret != 0) {
2102         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2103         ret = -ENODEV;
2104                 goto sensor_INIT_ERR;
2105     }
2106
2107     mdelay(5);  //delay 5 microseconds
2108         /* check if it is an sensor sensor */
2109     ret = sensor_read(client, 0x300a, &value);
2110     if (ret != 0) {
2111         SENSOR_TR("read chip id high byte failed\n");
2112         ret = -ENODEV;
2113         goto sensor_INIT_ERR;
2114     }
2115
2116     pid |= (value << 8);
2117
2118     ret = sensor_read(client, 0x300b, &value);
2119     if (ret != 0) {
2120         SENSOR_TR("read chip id low byte failed\n");
2121         ret = -ENODEV;
2122         goto sensor_INIT_ERR;
2123     }
2124
2125     pid |= (value & 0xff);
2126     SENSOR_DG("\n %s  pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2127
2128     if (pid == SENSOR_ID) {
2129         sensor->model = SENSOR_V4L2_IDENT;
2130     } else {
2131         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2132         ret = -ENODEV;
2133         goto sensor_INIT_ERR;
2134     }
2135
2136     ret = sensor_write_array(client, sensor_init_data);
2137     if (ret != 0)
2138     {
2139         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2140         goto sensor_INIT_ERR;
2141     }
2142         sensor_task_lock(client,0);
2143     //icd->user_width = SENSOR_INIT_WIDTH;
2144     //icd->user_height = SENSOR_INIT_HEIGHT;
2145     sensor->info_priv.winseqe_cur_addr  = SENSOR_INIT_WINSEQADR;
2146         sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
2147
2148     /* sensor sensor information for initialization  */
2149         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2150         if (qctrl)
2151         sensor->info_priv.whiteBalance = qctrl->default_value;
2152         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2153         if (qctrl)
2154         sensor->info_priv.brightness = qctrl->default_value;
2155         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2156         if (qctrl)
2157         sensor->info_priv.effect = qctrl->default_value;
2158         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2159         if (qctrl)
2160         sensor->info_priv.exposure = qctrl->default_value;
2161
2162         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2163         if (qctrl)
2164         sensor->info_priv.saturation = qctrl->default_value;
2165         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2166         if (qctrl)
2167         sensor->info_priv.contrast = qctrl->default_value;
2168         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2169         if (qctrl)
2170         sensor->info_priv.mirror = qctrl->default_value;
2171         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2172         if (qctrl)
2173         sensor->info_priv.flip = qctrl->default_value;
2174         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2175         if (qctrl)
2176         sensor->info_priv.scene = qctrl->default_value;
2177         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2178         if (qctrl)
2179         sensor->info_priv.digitalzoom = qctrl->default_value;
2180
2181     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
2182         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2183         if (qctrl)
2184         sensor->info_priv.focus = qctrl->default_value;
2185
2186         #if CONFIG_SENSOR_Flash
2187         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2188         if (qctrl)
2189         sensor->info_priv.flash = qctrl->default_value;
2190     #endif
2191     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);
2192
2193     return 0;
2194 sensor_INIT_ERR:
2195         sensor_task_lock(client,0);
2196         sensor_deactivate(client);
2197     return ret;
2198 }
2199 static int sensor_deactivate(struct i2c_client *client)
2200 {
2201         struct soc_camera_device *icd = client->dev.platform_data;
2202
2203         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2204
2205         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2206         sensor_task_lock(client, 1);
2207     sensor_write(client, 0x3017, 0x00);  // FREX,VSYNC,HREF,PCLK,D9-D6
2208         sensor_write(client, 0x3018, 0x03);  // D5-D0
2209         sensor_write(client,0x3019,0x00);    // STROBE,SDA
2210         sensor_task_lock(client, 0);
2211         sensor_ioctrl(icd, Sensor_PowerDown, 1);
2212         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2213         icd->user_width = SENSOR_INIT_WIDTH;
2214     icd->user_height = SENSOR_INIT_HEIGHT;
2215         msleep(100);
2216         return 0;
2217 }
2218 static  struct reginfo sensor_power_down_sequence[]=
2219 {
2220     {0x00,0x00}
2221 };
2222 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2223 {
2224     int ret;
2225     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2226
2227     if (pm_msg.event == PM_EVENT_SUSPEND) {
2228         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2229         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2230         if (ret != 0) {
2231             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2232             return ret;
2233         } else {
2234             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2235             if (ret < 0) {
2236                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2237                 return -EINVAL;
2238             }
2239         }
2240     } else {
2241         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2242         return -EINVAL;
2243     }
2244
2245     return 0;
2246 }
2247
2248 static int sensor_resume(struct soc_camera_device *icd)
2249 {
2250         int ret;
2251
2252     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2253     if (ret < 0) {
2254                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2255         return -EINVAL;
2256     }
2257
2258         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2259         return 0;
2260 }
2261
2262 static int sensor_set_bus_param(struct soc_camera_device *icd,
2263                                 unsigned long flags)
2264 {
2265
2266     return 0;
2267 }
2268
2269 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2270 {
2271     struct soc_camera_link *icl = to_soc_camera_link(icd);
2272     unsigned long flags = SENSOR_BUS_PARAM;
2273
2274     return soc_camera_apply_sensor_flags(icl, flags);
2275 }
2276
2277 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2278 {
2279     struct i2c_client *client = sd->priv;
2280     struct soc_camera_device *icd = client->dev.platform_data;
2281     struct sensor *sensor = to_sensor(client);
2282     struct v4l2_pix_format *pix = &f->fmt.pix;
2283
2284     pix->width          = icd->user_width;
2285     pix->height         = icd->user_height;
2286     pix->pixelformat    = sensor->info_priv.pixfmt;
2287     pix->field          = V4L2_FIELD_NONE;
2288     pix->colorspace             = V4L2_COLORSPACE_JPEG;
2289
2290     return 0;
2291 }
2292 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
2293 {
2294     bool ret = false;
2295
2296         if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
2297                 ret = true;
2298         } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
2299                 ret = true;
2300         } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
2301                 ret = true;
2302         } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
2303                 ret = true;
2304         } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
2305                 ret = true;
2306         }
2307
2308         if (ret == true)
2309                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2310         return ret;
2311 }
2312 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
2313 {
2314     bool ret = false;
2315
2316         if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
2317                 ret = true;
2318         } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
2319                 ret = true;
2320         }
2321
2322         if (ret == true)
2323                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2324         return ret;
2325 }
2326 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2327 {
2328     struct i2c_client *client = sd->priv;
2329     struct sensor *sensor = to_sensor(client);
2330     struct v4l2_pix_format *pix = &f->fmt.pix;
2331         const struct v4l2_queryctrl *qctrl;
2332         struct soc_camera_device *icd = client->dev.platform_data;
2333     struct reginfo *winseqe_set_addr=NULL;
2334     int ret = 0, set_w,set_h;
2335
2336         if (sensor->info_priv.pixfmt != pix->pixelformat) {
2337                 switch (pix->pixelformat)
2338                 {
2339                         case V4L2_PIX_FMT_YUYV:
2340                         {
2341                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2342                                 break;
2343                         }
2344                         case V4L2_PIX_FMT_UYVY:
2345                         {
2346                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2347                                 break;
2348                         }
2349                         default:
2350                                 break;
2351                 }
2352                 if (winseqe_set_addr != NULL) {
2353             sensor_write_array(client, winseqe_set_addr);
2354                         sensor->info_priv.pixfmt = pix->pixelformat;
2355
2356                         SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2357                 } else {
2358                         SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2359                 }
2360         }
2361
2362     set_w = pix->width;
2363     set_h = pix->height;
2364
2365         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2366         {
2367                 winseqe_set_addr = sensor_qcif;
2368         set_w = 176;
2369         set_h = 144;
2370         }
2371         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2372     {
2373         winseqe_set_addr = sensor_qvga;
2374         set_w = 320;
2375         set_h = 240;
2376     }
2377     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2378     {
2379         winseqe_set_addr = sensor_cif;
2380         set_w = 352;
2381         set_h = 288;
2382     }
2383     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2384     {
2385         winseqe_set_addr = sensor_vga;
2386         set_w = 640;
2387         set_h = 480;
2388     }
2389     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2390     {
2391         winseqe_set_addr = sensor_svga;
2392         set_w = 800;
2393         set_h = 600;
2394     }
2395         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
2396     {
2397         winseqe_set_addr = sensor_xga;
2398         set_w = 1024;
2399         set_h = 768;
2400     }
2401         else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
2402     {
2403         winseqe_set_addr = sensor_720p;
2404         set_w = 1280;
2405         set_h = 720;
2406     }
2407     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2408     {
2409         winseqe_set_addr = sensor_sxga;
2410         set_w = 1280;
2411         set_h = 1024;
2412     }
2413     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2414     {
2415         winseqe_set_addr = sensor_uxga;
2416         set_w = 1600;
2417         set_h = 1200;
2418     }
2419     else if (((set_w <= 1920) && (set_h <= 1080)) && sensor_1080p[0].reg)
2420     {
2421         winseqe_set_addr = sensor_1080p;
2422         set_w = 1920;
2423         set_h = 1080;
2424     }
2425         else if (((set_w <= 2048) && (set_h <= 1536)) && sensor_qxga[0].reg)
2426     {
2427         winseqe_set_addr = sensor_qxga;
2428         set_w = 2048;
2429         set_h = 1536;
2430     }
2431         else if (((set_w <= 2592) && (set_h <= 1944)) && sensor_qsxga[0].reg)
2432     {
2433         winseqe_set_addr = sensor_qsxga;
2434         set_w = 2592;
2435         set_h = 1944;
2436     }
2437     else
2438     {
2439         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2440         set_w = SENSOR_INIT_WIDTH;
2441         set_h = SENSOR_INIT_HEIGHT;
2442                 ret = -1;
2443                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
2444     }
2445
2446     if (winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr)
2447     {
2448                 if (sensor_fmt_capturechk(sd,f) == true) {                                      /* ddl@rock-chips.com : Capture */
2449                         sensor_parameter_record(client);
2450                 #if CONFIG_SENSOR_Focus
2451                         sensor_af_idlechk(client);
2452                         if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2453                                 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2454                 #endif
2455                 }
2456
2457                 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2458                         if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2459                                 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2460                                 ret |= sensor_write_array(client,sensor_init_data);
2461                                 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2462                         }
2463                 }
2464
2465         ret |= sensor_write_array(client, winseqe_set_addr);
2466         if (ret != 0) {
2467             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2468             goto sensor_s_fmt_end;
2469         }
2470
2471         sensor->info_priv.winseqe_cur_addr  = winseqe_set_addr;
2472
2473                 if (sensor_fmt_capturechk(sd,f) == true) {                                  /* ddl@rock-chips.com : Capture */
2474                         sensor_ae_transfer(client);
2475                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2476                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2477                         if (sensor->info_priv.whiteBalance != 0) {
2478                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2479                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2480                         }
2481                         sensor->info_priv.snap2preview = true;
2482                 } else if (sensor_fmt_videochk(sd,f) == true) {                 /* ddl@rock-chips.com : Video */
2483                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2484                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2485
2486                         sensor->info_priv.video2preview = true;
2487                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2488                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2489                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2490                         if (sensor->info_priv.snap2preview == true) {
2491                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2492                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2493                         }
2494                         sensor->info_priv.video2preview = false;
2495                         sensor->info_priv.snap2preview = false;
2496                 }
2497         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2498     }
2499     else
2500     {
2501         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2502     }
2503
2504         pix->width = set_w;
2505         pix->height = set_h;
2506 sensor_s_fmt_end:
2507     return ret;
2508 }
2509
2510 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2511 {
2512     struct v4l2_pix_format *pix = &f->fmt.pix;
2513     bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
2514         pix->pixelformat == V4L2_PIX_FMT_YUYV;
2515
2516     /*
2517     * With Bayer format enforce even side lengths, but let the user play
2518     * with the starting pixel
2519     */
2520
2521     if (pix->height > SENSOR_MAX_HEIGHT)
2522         pix->height = SENSOR_MAX_HEIGHT;
2523     else if (pix->height < SENSOR_MIN_HEIGHT)
2524         pix->height = SENSOR_MIN_HEIGHT;
2525     else if (bayer)
2526         pix->height = ALIGN(pix->height, 2);
2527
2528     if (pix->width > SENSOR_MAX_WIDTH)
2529         pix->width = SENSOR_MAX_WIDTH;
2530     else if (pix->width < SENSOR_MIN_WIDTH)
2531         pix->width = SENSOR_MIN_WIDTH;
2532     else if (bayer)
2533         pix->width = ALIGN(pix->width, 2);
2534
2535     return 0;
2536 }
2537
2538  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2539 {
2540     struct i2c_client *client = sd->priv;
2541
2542     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2543         return -EINVAL;
2544
2545     if (id->match.addr != client->addr)
2546         return -ENODEV;
2547
2548     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2549     id->revision = 0;
2550
2551     return 0;
2552 }
2553 #if CONFIG_SENSOR_Brightness
2554 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2555 {
2556     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2557
2558     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2559     {
2560         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2561         {
2562             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2563             {
2564                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2565                 return -EINVAL;
2566             }
2567             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2568             return 0;
2569         }
2570     }
2571         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2572     return -EINVAL;
2573 }
2574 #endif
2575 #if CONFIG_SENSOR_Effect
2576 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2577 {
2578     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2579
2580     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2581     {
2582         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2583         {
2584             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2585             {
2586                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2587                 return -EINVAL;
2588             }
2589             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2590             return 0;
2591         }
2592     }
2593         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2594     return -EINVAL;
2595 }
2596 #endif
2597 #if CONFIG_SENSOR_Exposure
2598 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2599 {
2600     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2601
2602     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2603     {
2604         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2605         {
2606             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2607             {
2608                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2609                 return -EINVAL;
2610             }
2611             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2612             return 0;
2613         }
2614     }
2615         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2616     return -EINVAL;
2617 }
2618 #endif
2619 #if CONFIG_SENSOR_Saturation
2620 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2621 {
2622     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2623
2624     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2625     {
2626         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2627         {
2628             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2629             {
2630                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2631                 return -EINVAL;
2632             }
2633             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2634             return 0;
2635         }
2636     }
2637     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2638     return -EINVAL;
2639 }
2640 #endif
2641 #if CONFIG_SENSOR_Contrast
2642 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2643 {
2644     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2645
2646     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2647     {
2648         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2649         {
2650             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2651             {
2652                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2653                 return -EINVAL;
2654             }
2655             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2656             return 0;
2657         }
2658     }
2659     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2660     return -EINVAL;
2661 }
2662 #endif
2663 #if CONFIG_SENSOR_Mirror
2664 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2665 {
2666     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2667
2668     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2669     {
2670         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2671         {
2672             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2673             {
2674                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2675                 return -EINVAL;
2676             }
2677             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2678             return 0;
2679         }
2680     }
2681     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2682     return -EINVAL;
2683 }
2684 #endif
2685 #if CONFIG_SENSOR_Flip
2686 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2687 {
2688     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2689
2690     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2691     {
2692         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2693         {
2694             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2695             {
2696                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2697                 return -EINVAL;
2698             }
2699             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2700             return 0;
2701         }
2702     }
2703     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2704     return -EINVAL;
2705 }
2706 #endif
2707 #if CONFIG_SENSOR_Scene
2708 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2709 {
2710     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2711
2712     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2713     {
2714         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2715         {
2716             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2717             {
2718                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2719                 return -EINVAL;
2720             }
2721             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2722             return 0;
2723         }
2724     }
2725     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2726     return -EINVAL;
2727 }
2728 #endif
2729 #if CONFIG_SENSOR_WhiteBalance
2730 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2731 {
2732     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2733
2734     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2735     {
2736         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2737         {
2738             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2739             {
2740                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2741                 return -EINVAL;
2742             }
2743             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2744             return 0;
2745         }
2746     }
2747         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2748     return -EINVAL;
2749 }
2750 #endif
2751 #if CONFIG_SENSOR_DigitalZoom
2752 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2753 {
2754     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2755     struct sensor *sensor = to_sensor(client);
2756         const struct v4l2_queryctrl *qctrl_info;
2757     int digitalzoom_cur, digitalzoom_total;
2758
2759         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2760         if (qctrl_info)
2761                 return -EINVAL;
2762
2763     digitalzoom_cur = sensor->info_priv.digitalzoom;
2764     digitalzoom_total = qctrl_info->maximum;
2765
2766     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2767     {
2768         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2769         return -EINVAL;
2770     }
2771
2772     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2773     {
2774         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2775         return -EINVAL;
2776     }
2777
2778     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2779     {
2780         *value = digitalzoom_total - digitalzoom_cur;
2781     }
2782
2783     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2784     {
2785         *value = 0 - digitalzoom_cur;
2786     }
2787
2788     digitalzoom_cur += *value;
2789
2790     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2791     {
2792         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2793         {
2794             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2795             return -EINVAL;
2796         }
2797         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2798         return 0;
2799     }
2800
2801     return -EINVAL;
2802 }
2803 #endif
2804 #if CONFIG_SENSOR_Focus
2805 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2806 {
2807         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2808     struct sensor *sensor = to_sensor(client);
2809         const struct v4l2_queryctrl *qctrl_info;
2810         struct af_cmdinfo cmdinfo;
2811         int ret = 0;
2812
2813         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2814         if (!qctrl_info)
2815                 return -EINVAL;
2816
2817         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2818                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2819
2820                         if (sensor_af_idlechk(client))
2821                                 goto sensor_set_focus_absolute_end;
2822
2823                         cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2824                         cmdinfo.cmd_para[0] = value;
2825                         cmdinfo.validate_bit = 0x81;
2826                         ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
2827                         //ret |= sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
2828                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2829                 } else {
2830                         ret = -EINVAL;
2831                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2832                 }
2833         } else {
2834                 ret = -EACCES;
2835                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2836                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2837         }
2838
2839 sensor_set_focus_absolute_end:
2840         return ret;
2841 }
2842 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2843 {
2844         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2845         struct sensor *sensor = to_sensor(client);
2846         const struct v4l2_queryctrl *qctrl_info;
2847         struct af_cmdinfo cmdinfo;
2848         int ret = 0;
2849
2850         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
2851         if (!qctrl_info)
2852                 return -EINVAL;
2853
2854         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2855                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2856
2857                         if (sensor_af_idlechk(client))
2858                                 goto sensor_set_focus_relative_end;
2859
2860                         if (value > 0) {
2861                                 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2862                         } else if (value < 0) {
2863                                 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2864                         }
2865                         cmdinfo.validate_bit = 0x80;
2866                         ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
2867
2868                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2869                 } else {
2870                         ret = -EINVAL;
2871                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2872                 }
2873         } else {
2874                 ret = -EACCES;
2875                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2876                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2877         }
2878 sensor_set_focus_relative_end:
2879         return ret;
2880 }
2881
2882 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2883 {
2884         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2885         struct sensor *sensor = to_sensor(client);
2886         int ret = 0;
2887
2888         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)  && (sensor->info_priv.affm_reinit == 0)) {
2889                 switch (value)
2890                 {
2891                         case SENSOR_AF_MODE_AUTO:
2892                         {
2893                                 ret = sensor_af_single(client);
2894                                 break;
2895                         }
2896
2897                         case SENSOR_AF_MODE_MACRO:
2898                         {
2899                                 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
2900                                 break;
2901                         }
2902
2903                         case SENSOR_AF_MODE_INFINITY:
2904                         {
2905                                 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
2906                                 break;
2907                         }
2908
2909                         case SENSOR_AF_MODE_CONTINUOUS:
2910                         {
2911                                 ret = sensor_af_const(client);
2912                                 break;
2913                         }
2914                         default:
2915                                 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2916                                 break;
2917
2918                 }
2919
2920                 SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2921         } else {
2922                 ret = -EACCES;
2923                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2924                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2925         }
2926
2927         return ret;
2928 }
2929 #endif
2930 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2931 {
2932     struct i2c_client *client = sd->priv;
2933     struct sensor *sensor = to_sensor(client);
2934     const struct v4l2_queryctrl *qctrl;
2935
2936     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2937
2938     if (!qctrl)
2939     {
2940         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2941         return -EINVAL;
2942     }
2943
2944     switch (ctrl->id)
2945     {
2946         case V4L2_CID_BRIGHTNESS:
2947             {
2948                 ctrl->value = sensor->info_priv.brightness;
2949                 break;
2950             }
2951         case V4L2_CID_SATURATION:
2952             {
2953                 ctrl->value = sensor->info_priv.saturation;
2954                 break;
2955             }
2956         case V4L2_CID_CONTRAST:
2957             {
2958                 ctrl->value = sensor->info_priv.contrast;
2959                 break;
2960             }
2961         case V4L2_CID_DO_WHITE_BALANCE:
2962             {
2963                 ctrl->value = sensor->info_priv.whiteBalance;
2964                 break;
2965             }
2966         case V4L2_CID_EXPOSURE:
2967             {
2968                 ctrl->value = sensor->info_priv.exposure;
2969                 break;
2970             }
2971         case V4L2_CID_HFLIP:
2972             {
2973                 ctrl->value = sensor->info_priv.mirror;
2974                 break;
2975             }
2976         case V4L2_CID_VFLIP:
2977             {
2978                 ctrl->value = sensor->info_priv.flip;
2979                 break;
2980             }
2981         default :
2982                 break;
2983     }
2984     return 0;
2985 }
2986
2987
2988
2989 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2990 {
2991     struct i2c_client *client = sd->priv;
2992     struct sensor *sensor = to_sensor(client);
2993     struct soc_camera_device *icd = client->dev.platform_data;
2994     const struct v4l2_queryctrl *qctrl;
2995
2996
2997     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2998
2999     if (!qctrl)
3000     {
3001         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3002         return -EINVAL;
3003     }
3004
3005     switch (ctrl->id)
3006     {
3007 #if CONFIG_SENSOR_Brightness
3008         case V4L2_CID_BRIGHTNESS:
3009             {
3010                 if (ctrl->value != sensor->info_priv.brightness)
3011                 {
3012                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3013                     {
3014                         return -EINVAL;
3015                     }
3016                     sensor->info_priv.brightness = ctrl->value;
3017                 }
3018                 break;
3019             }
3020 #endif
3021 #if CONFIG_SENSOR_Exposure
3022         case V4L2_CID_EXPOSURE:
3023             {
3024                 if (ctrl->value != sensor->info_priv.exposure)
3025                 {
3026                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3027                     {
3028                         return -EINVAL;
3029                     }
3030                     sensor->info_priv.exposure = ctrl->value;
3031                 }
3032                 break;
3033             }
3034 #endif
3035 #if CONFIG_SENSOR_Saturation
3036         case V4L2_CID_SATURATION:
3037             {
3038                 if (ctrl->value != sensor->info_priv.saturation)
3039                 {
3040                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3041                     {
3042                         return -EINVAL;
3043                     }
3044                     sensor->info_priv.saturation = ctrl->value;
3045                 }
3046                 break;
3047             }
3048 #endif
3049 #if CONFIG_SENSOR_Contrast
3050         case V4L2_CID_CONTRAST:
3051             {
3052                 if (ctrl->value != sensor->info_priv.contrast)
3053                 {
3054                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3055                     {
3056                         return -EINVAL;
3057                     }
3058                     sensor->info_priv.contrast = ctrl->value;
3059                 }
3060                 break;
3061             }
3062 #endif
3063 #if CONFIG_SENSOR_WhiteBalance
3064         case V4L2_CID_DO_WHITE_BALANCE:
3065             {
3066                 if (ctrl->value != sensor->info_priv.whiteBalance)
3067                 {
3068                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3069                     {
3070                         return -EINVAL;
3071                     }
3072                     sensor->info_priv.whiteBalance = ctrl->value;
3073                 }
3074                 break;
3075             }
3076 #endif
3077 #if CONFIG_SENSOR_Mirror
3078         case V4L2_CID_HFLIP:
3079             {
3080                 if (ctrl->value != sensor->info_priv.mirror)
3081                 {
3082                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3083                         return -EINVAL;
3084                     sensor->info_priv.mirror = ctrl->value;
3085                 }
3086                 break;
3087             }
3088 #endif
3089 #if CONFIG_SENSOR_Flip
3090         case V4L2_CID_VFLIP:
3091             {
3092                 if (ctrl->value != sensor->info_priv.flip)
3093                 {
3094                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3095                         return -EINVAL;
3096                     sensor->info_priv.flip = ctrl->value;
3097                 }
3098                 break;
3099             }
3100 #endif
3101         default:
3102             break;
3103     }
3104
3105     return 0;
3106 }
3107 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3108 {
3109     const struct v4l2_queryctrl *qctrl;
3110     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3111     struct sensor *sensor = to_sensor(client);
3112
3113     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3114
3115     if (!qctrl)
3116     {
3117         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3118         return -EINVAL;
3119     }
3120
3121     switch (ext_ctrl->id)
3122     {
3123         case V4L2_CID_SCENE:
3124             {
3125                 ext_ctrl->value = sensor->info_priv.scene;
3126                 break;
3127             }
3128         case V4L2_CID_EFFECT:
3129             {
3130                 ext_ctrl->value = sensor->info_priv.effect;
3131                 break;
3132             }
3133         case V4L2_CID_ZOOM_ABSOLUTE:
3134             {
3135                 ext_ctrl->value = sensor->info_priv.digitalzoom;
3136                 break;
3137             }
3138         case V4L2_CID_ZOOM_RELATIVE:
3139             {
3140                 return -EINVAL;
3141             }
3142         case V4L2_CID_FOCUS_ABSOLUTE:
3143             {
3144                 return -EINVAL;
3145             }
3146         case V4L2_CID_FOCUS_RELATIVE:
3147             {
3148                 return -EINVAL;
3149             }
3150         case V4L2_CID_FLASH:
3151             {
3152                 ext_ctrl->value = sensor->info_priv.flash;
3153                 break;
3154             }
3155         default :
3156             break;
3157     }
3158     return 0;
3159 }
3160 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3161 {
3162     const struct v4l2_queryctrl *qctrl;
3163     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3164     struct sensor *sensor = to_sensor(client);
3165     int val_offset;
3166
3167     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3168
3169     if (!qctrl)
3170     {
3171         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3172         return -EINVAL;
3173     }
3174
3175         val_offset = 0;
3176     switch (ext_ctrl->id)
3177     {
3178 #if CONFIG_SENSOR_Scene
3179         case V4L2_CID_SCENE:
3180             {
3181                 if (ext_ctrl->value != sensor->info_priv.scene)
3182                 {
3183                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3184                         return -EINVAL;
3185                     sensor->info_priv.scene = ext_ctrl->value;
3186                 }
3187                 break;
3188             }
3189 #endif
3190 #if CONFIG_SENSOR_Effect
3191         case V4L2_CID_EFFECT:
3192             {
3193                 if (ext_ctrl->value != sensor->info_priv.effect)
3194                 {
3195                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3196                         return -EINVAL;
3197                     sensor->info_priv.effect= ext_ctrl->value;
3198                 }
3199                 break;
3200             }
3201 #endif
3202 #if CONFIG_SENSOR_DigitalZoom
3203         case V4L2_CID_ZOOM_ABSOLUTE:
3204             {
3205                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3206                     return -EINVAL;
3207
3208                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3209                 {
3210                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3211
3212                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3213                         return -EINVAL;
3214                     sensor->info_priv.digitalzoom += val_offset;
3215
3216                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
3217                 }
3218
3219                 break;
3220             }
3221         case V4L2_CID_ZOOM_RELATIVE:
3222             {
3223                 if (ext_ctrl->value)
3224                 {
3225                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3226                         return -EINVAL;
3227                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3228
3229                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3230                 }
3231                 break;
3232             }
3233 #endif
3234 #if CONFIG_SENSOR_Focus
3235         case V4L2_CID_FOCUS_ABSOLUTE:
3236             {
3237                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3238                     return -EINVAL;
3239
3240                                 if (sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value) == 0) {
3241                                         if (ext_ctrl->value == qctrl->minimum) {
3242                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
3243                                         } else if (ext_ctrl->value == qctrl->maximum) {
3244                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
3245                                         } else {
3246                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
3247                                         }
3248                                 }
3249
3250                 break;
3251             }
3252         case V4L2_CID_FOCUS_RELATIVE:
3253             {
3254                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3255                     return -EINVAL;
3256
3257                 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3258                 break;
3259             }
3260                 case V4L2_CID_FOCUS_AUTO:
3261                         {
3262                                 if (ext_ctrl->value == 1) {
3263                                         if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
3264                                                 return -EINVAL;
3265                                         sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3266                                 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3267                                         if (ext_ctrl->value == 0)
3268                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3269                                 }
3270                                 break;
3271                         }
3272                 case V4L2_CID_FOCUS_CONTINUOUS:
3273                         {
3274                                 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3275                                         if (ext_ctrl->value == 1) {
3276                                                 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3277                                                         return -EINVAL;
3278                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3279                                         }
3280                                 } else {
3281                                         if (ext_ctrl->value == 0)
3282                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3283                                 }
3284                                 break;
3285                         }
3286 #endif
3287 #if CONFIG_SENSOR_Flash
3288         case V4L2_CID_FLASH:
3289             {
3290                 sensor->info_priv.flash = ext_ctrl->value;
3291
3292                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3293                 break;
3294             }
3295 #endif
3296         default:
3297             break;
3298     }
3299
3300     return 0;
3301 }
3302
3303 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3304 {
3305     struct i2c_client *client = sd->priv;
3306     struct soc_camera_device *icd = client->dev.platform_data;
3307     int i, error_cnt=0, error_idx=-1;
3308
3309
3310     for (i=0; i<ext_ctrl->count; i++) {
3311         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3312             error_cnt++;
3313             error_idx = i;
3314         }
3315     }
3316
3317     if (error_cnt > 1)
3318         error_idx = ext_ctrl->count;
3319
3320     if (error_idx != -1) {
3321         ext_ctrl->error_idx = error_idx;
3322         return -EINVAL;
3323     } else {
3324         return 0;
3325     }
3326 }
3327
3328 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3329 {
3330     struct i2c_client *client = sd->priv;
3331     struct soc_camera_device *icd = client->dev.platform_data;
3332     int i, error_cnt=0, error_idx=-1;
3333
3334     for (i=0; i<ext_ctrl->count; i++) {
3335         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3336             error_cnt++;
3337             error_idx = i;
3338         }
3339     }
3340
3341     if (error_cnt > 1)
3342         error_idx = ext_ctrl->count;
3343
3344     if (error_idx != -1) {
3345         ext_ctrl->error_idx = error_idx;
3346         return -EINVAL;
3347     } else {
3348         return 0;
3349     }
3350 }
3351
3352 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3353 {
3354         struct i2c_client *client = sd->priv;
3355     struct sensor *sensor = to_sensor(client);
3356         #if CONFIG_SENSOR_Focus
3357         struct soc_camera_device *icd = client->dev.platform_data;
3358         struct v4l2_format fmt;
3359         #endif
3360
3361         if (enable == 1) {
3362                 sensor->info_priv.enable = 1;
3363                 #if CONFIG_SENSOR_Focus
3364                 fmt.fmt.pix.width = icd->user_width;
3365                 fmt.fmt.pix.height = icd->user_height;
3366                 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3367                 if (sensor_fmt_capturechk(sd, &fmt) == false) {
3368                         if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3369                                 if (sensor->sensor_wq != NULL) {
3370                                         mutex_lock(&sensor->wq_lock);
3371                                         if (sensor->sensor_wk.state == sensor_working) {
3372                                                 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
3373                                                 mutex_unlock(&sensor->wq_lock);
3374                                                 goto sensor_s_stream_end;
3375                                         }
3376                                         sensor->sensor_wk.state = sensor_working;
3377                                         mutex_unlock(&sensor->wq_lock);
3378                                         sensor->sensor_wk.client = client;
3379                                         INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
3380                                         queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
3381                                 }
3382                                 sensor->info_priv.affm_reinit = 0;
3383                         }
3384                 }
3385                 #endif
3386         } else if (enable == 0) {
3387                 sensor->info_priv.enable = 0;
3388                 #if CONFIG_SENSOR_Focus
3389                 flush_work(&(sensor->sensor_wk.dwork.work));
3390                 mutex_lock(&sensor->wq_lock);
3391                 sensor->sensor_wk.state = sensor_work_ready;
3392                 mutex_unlock(&sensor->wq_lock);
3393                 #endif
3394         }
3395
3396 sensor_s_stream_end:
3397         return 0;
3398 }
3399
3400 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3401  * this wasn't our capture interface, so, we wait for the right one */
3402 static int sensor_video_probe(struct soc_camera_device *icd,
3403                                struct i2c_client *client)
3404 {
3405     char value;
3406     int ret,pid = 0;
3407     struct sensor *sensor = to_sensor(client);
3408
3409     /* We must have a parent by now. And it cannot be a wrong one.
3410      * So this entire test is completely redundant. */
3411     if (!icd->dev.parent ||
3412             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3413                 return -ENODEV;
3414
3415         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3416                 ret = -ENODEV;
3417                 goto sensor_video_probe_err;
3418         }
3419     /* soft reset */
3420     ret = sensor_write(client, 0x3012, 0x80);
3421     if (ret != 0) {
3422         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3423         ret = -ENODEV;
3424                 goto sensor_video_probe_err;
3425     }
3426     mdelay(5);          //delay 5 microseconds
3427
3428     /* check if it is an sensor sensor */
3429     ret = sensor_read(client, 0x300a, &value);
3430     if (ret != 0) {
3431         SENSOR_TR("read chip id high byte failed\n");
3432         ret = -ENODEV;
3433         goto sensor_video_probe_err;
3434     }
3435
3436     pid |= (value << 8);
3437
3438     ret = sensor_read(client, 0x300b, &value);
3439     if (ret != 0) {
3440         SENSOR_TR("read chip id low byte failed\n");
3441         ret = -ENODEV;
3442         goto sensor_video_probe_err;
3443     }
3444
3445     pid |= (value & 0xff);
3446     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3447     if (pid == SENSOR_ID) {
3448         sensor->model = SENSOR_V4L2_IDENT;
3449     } else {
3450         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3451         ret = -ENODEV;
3452         goto sensor_video_probe_err;
3453     }
3454
3455     icd->formats = sensor_colour_formats;
3456     icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
3457
3458     return 0;
3459
3460 sensor_video_probe_err:
3461     return ret;
3462 }
3463 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3464 {
3465         struct i2c_client *client = sd->priv;
3466     struct sensor *sensor = to_sensor(client);
3467
3468         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3469         switch (cmd)
3470         {
3471                 case RK29_CAM_SUBDEV_DEACTIVATE:
3472                 {
3473                         sensor_deactivate(client);
3474                         break;
3475                 }
3476                 case RK29_CAM_SUBDEV_IOREQUEST:
3477                 {
3478                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3479                         break;
3480                 }
3481                 default:
3482                 {
3483                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3484                         break;
3485                 }
3486         }
3487
3488         return 0;
3489
3490 }
3491
3492 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3493         .init           = sensor_init,
3494         .g_ctrl         = sensor_g_control,
3495         .s_ctrl         = sensor_s_control,
3496         .g_ext_ctrls          = sensor_g_ext_controls,
3497         .s_ext_ctrls          = sensor_s_ext_controls,
3498         .g_chip_ident   = sensor_g_chip_ident,
3499         .ioctl = sensor_ioctl,
3500 };
3501
3502 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3503         .s_fmt          = sensor_s_fmt,
3504         .g_fmt          = sensor_g_fmt,
3505         .try_fmt        = sensor_try_fmt,
3506         .s_stream   = sensor_s_stream,
3507 };
3508
3509 static struct v4l2_subdev_ops sensor_subdev_ops = {
3510         .core   = &sensor_subdev_core_ops,
3511         .video = &sensor_subdev_video_ops,
3512 };
3513
3514 static int sensor_probe(struct i2c_client *client,
3515                          const struct i2c_device_id *did)
3516 {
3517     struct sensor *sensor;
3518     struct soc_camera_device *icd = client->dev.platform_data;
3519     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3520     struct soc_camera_link *icl;
3521     int ret;
3522
3523     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3524     if (!icd) {
3525         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3526         return -EINVAL;
3527     }
3528
3529     icl = to_soc_camera_link(icd);
3530     if (!icl) {
3531         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3532         return -EINVAL;
3533     }
3534
3535     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3536         dev_warn(&adapter->dev,
3537                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3538         return -EIO;
3539     }
3540
3541     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3542     if (!sensor)
3543         return -ENOMEM;
3544
3545     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3546
3547     /* Second stage probe - when a capture adapter is there */
3548     icd->ops            = &sensor_ops;
3549     icd->y_skip_top             = 0;
3550         #if CONFIG_SENSOR_I2C_NOSCHED
3551         atomic_set(&sensor->tasklock_cnt,0);
3552         #endif
3553
3554     ret = sensor_video_probe(icd, client);
3555     if (ret) {
3556         icd->ops = NULL;
3557         i2c_set_clientdata(client, NULL);
3558         kfree(sensor);
3559                 sensor = NULL;
3560     } else {
3561                 #if CONFIG_SENSOR_Focus
3562                 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
3563                 if (sensor->sensor_wq == NULL)
3564                         SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
3565                 mutex_init(&sensor->wq_lock);
3566                 sensor->sensor_wk.state = sensor_work_ready;
3567                 #endif
3568     }
3569
3570     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3571     return ret;
3572 }
3573
3574 static int sensor_remove(struct i2c_client *client)
3575 {
3576     struct sensor *sensor = to_sensor(client);
3577     struct soc_camera_device *icd = client->dev.platform_data;
3578
3579         #if CONFIG_SENSOR_Focus
3580         if (sensor->sensor_wq) {
3581                 destroy_workqueue(sensor->sensor_wq);
3582                 sensor->sensor_wq = NULL;
3583         }
3584         #endif
3585
3586     icd->ops = NULL;
3587     i2c_set_clientdata(client, NULL);
3588     client->driver = NULL;
3589     kfree(sensor);
3590         sensor = NULL;
3591     return 0;
3592 }
3593
3594 static const struct i2c_device_id sensor_id[] = {
3595         {SENSOR_NAME_STRING(), 0 },
3596         { }
3597 };
3598 MODULE_DEVICE_TABLE(i2c, sensor_id);
3599
3600 static struct i2c_driver sensor_i2c_driver = {
3601         .driver = {
3602                 .name = SENSOR_NAME_STRING(),
3603         },
3604         .probe          = sensor_probe,
3605         .remove         = sensor_remove,
3606         .id_table       = sensor_id,
3607 };
3608
3609 static int __init sensor_mod_init(void)
3610 {
3611     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3612     return i2c_add_driver(&sensor_i2c_driver);
3613 }
3614
3615 static void __exit sensor_mod_exit(void)
3616 {
3617     i2c_del_driver(&sensor_i2c_driver);
3618 }
3619
3620 device_initcall_sync(sensor_mod_init);
3621 module_exit(sensor_mod_exit);
3622
3623 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3624 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3625 MODULE_LICENSE("GPL");
3626
3627