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