camera driver v0.2.1 for RK30
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / ov2659.c
1 /*
2 o* Driver for MT9M001 CMOS Image Sensor from Micron
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/miscdevice.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/soc_camera.h>
22 #include <mach/rk_camera.h>
23 #include <linux/vmalloc.h>
24 static int debug;
25 module_param(debug, int, S_IRUGO|S_IWUSR);
26
27 #define dprintk(level, fmt, arg...) do {                        \
28         if (debug >= level)                                     \
29         printk(KERN_WARNING fmt , ## arg); } while (0)
30
31 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
32 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
33
34
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
37
38 #define __STR(x) #x
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
41
42 #define MIN(x,y)   ((x<y) ? x: y)
43 #define MAX(x,y)    ((x>y) ? x: y)
44
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK_CAM_SENSOR_OV2659
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV2659
48 #define SENSOR_ID 0x2656
49 #define SENSOR_MIN_WIDTH    800
50 #define SENSOR_MIN_HEIGHT   600
51 #define SENSOR_MAX_WIDTH    1600
52 #define SENSOR_MAX_HEIGHT   1200
53 #define SENSOR_INIT_WIDTH       sensor_init_width                       /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT  sensor_init_height
55 #define SENSOR_INIT_WINSEQADR  sensor_init_winseq_p
56 #define SENSOR_INIT_PIXFMT sensor_init_pixelcode
57 #define SENSOR_BUS_PARAM  sensor_init_busparam
58
59 #define CONFIG_SENSOR_WhiteBalance      1
60 #define CONFIG_SENSOR_Brightness        0
61 #define CONFIG_SENSOR_Contrast      0
62 #define CONFIG_SENSOR_Saturation    0
63 #define CONFIG_SENSOR_Effect        1
64 #define CONFIG_SENSOR_Scene         1
65 #define CONFIG_SENSOR_DigitalZoom   0
66 #define CONFIG_SENSOR_Focus         0
67 #define CONFIG_SENSOR_Exposure      0
68 #define CONFIG_SENSOR_Flash         1
69 #define CONFIG_SENSOR_Mirror        0 
70 #define CONFIG_SENSOR_Flip          0
71
72 #define CONFIG_SENSOR_I2C_SPEED     100000       /* Hz */
73 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
74 #define CONFIG_SENSOR_I2C_NOSCHED   0
75 #define CONFIG_SENSOR_I2C_RDWRCHK   0
76
77 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
78 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
79 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
80 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
81 #define COLOR_TEMPERATURE_OFFICE_DN     3500
82 #define COLOR_TEMPERATURE_OFFICE_UP     5000
83 #define COLOR_TEMPERATURE_HOME_DN       2500
84 #define COLOR_TEMPERATURE_HOME_UP       3500
85
86 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
87 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
88
89 #define SENSOR_AF_IS_ERR    (0x00<<0)
90 #define SENSOR_AF_IS_OK         (0x01<<0)
91 #define SENSOR_INIT_IS_ERR   (0x00<<28)
92 #define SENSOR_INIT_IS_OK    (0x01<<28)
93 struct reginfo
94 {
95         u16 reg;
96         u8 val;
97 };
98
99 static s32 sensor_init_width = 0;
100 static s32 sensor_init_height = 0;
101 static unsigned long sensor_init_busparam = (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING|SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ);
102 static enum v4l2_mbus_pixelcode sensor_init_pixelcode = V4L2_MBUS_FMT_YUYV8_2X8;
103 static struct reginfo* sensor_init_data_p = NULL;
104 static struct reginfo* sensor_init_winseq_p = NULL;
105 static struct reginfo* sensor_init_winseq_board = NULL;
106 /* init 800*600 SVGA */
107 static struct reginfo sensor_init_data[] =
108 {
109         {0x3000, 0x0f},
110         {0x3001, 0xff},
111         {0x3002, 0xff},
112         //{0x0100, 0x01},       //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array
113         {0x3633, 0x3d},
114         {0x3620, 0x02},
115         {0x3631, 0x11},
116         {0x3612, 0x04},
117         {0x3630, 0x20},
118         {0x4702, 0x02},
119         {0x370c, 0x34},
120         {0x3004, 0x10},
121         {0x3005, 0x18},
122         {0x3800, 0x00},
123         {0x3801, 0x00},
124         {0x3802, 0x00},
125         {0x3803, 0x00},
126         {0x3804, 0x06},
127         {0x3805, 0x5f},
128         {0x3806, 0x04},
129         {0x3807, 0xb7},
130         {0x3808, 0x03},
131         {0x3809, 0x20},
132         {0x380a, 0x02},
133         {0x380b, 0x58},
134         {0x380c, 0x05},
135         {0x380d, 0x14},
136         {0x380e, 0x02},
137         {0x380f, 0x68},
138         {0x3811, 0x08},
139         {0x3813, 0x02},
140         {0x3814, 0x31},
141         {0x3815, 0x31},
142         {0x3a02, 0x02},
143         {0x3a03, 0x68},
144         {0x3a08, 0x00},
145         {0x3a09, 0x5c},
146         {0x3a0a, 0x00},
147         {0x3a0b, 0x4d},
148         {0x3a0d, 0x08},
149         {0x3a0e, 0x06},
150         {0x3a14, 0x02},
151         {0x3a15, 0x28},
152                 {0x4708, 0x01},
153         {0x3623, 0x00},
154         {0x3634, 0x76},
155         {0x3701, 0x44},
156         {0x3702, 0x18},
157         {0x3703, 0x24},
158         {0x3704, 0x24},
159         {0x3705, 0x0c},
160         {0x3820, 0x81},
161         {0x3821, 0x01},
162         {0x370a, 0x52},
163         {0x4608, 0x00},
164         {0x4609, 0x80},
165         {0x4300, 0x32},
166         {0x5086, 0x02},
167         {0x5000, 0xfb},
168         {0x5001, 0x1f},
169         {0x5002, 0x00},
170         {0x5025, 0x0e},
171         {0x5026, 0x18},
172         {0x5027, 0x34},
173         {0x5028, 0x4c},
174         {0x5029, 0x62},
175         {0x502a, 0x74},
176         {0x502b, 0x85},
177         {0x502c, 0x92},
178         {0x502d, 0x9e},
179         {0x502e, 0xb2},
180         {0x502f, 0xc0},
181         {0x5030, 0xcc},
182         {0x5031, 0xe0},
183         {0x5032, 0xee},
184         {0x5033, 0xf6},
185         {0x5034, 0x11},
186         {0x5070, 0x1c},
187         {0x5071, 0x5b},
188         {0x5072, 0x05},
189         {0x5073, 0x20},
190         {0x5074, 0x94},
191         {0x5075, 0xb4},
192         {0x5076, 0xb4},
193         {0x5077, 0xaf},
194         {0x5078, 0x05},
195         {0x5079, 0x98},
196         {0x507a, 0x21},
197         {0x5035, 0x6a},
198         {0x5036, 0x11},
199         {0x5037, 0x92},
200         {0x5038, 0x21},
201
202         {0x5039, 0xe1},
203         {0x503a, 0x01},
204         {0x503c, 0x05},
205         {0x503d, 0x08},
206         {0x503e, 0x08},
207         {0x503f, 0x64},
208         {0x5040, 0x58},
209         {0x5041, 0x2a},
210         {0x5042, 0xc5},
211         {0x5043, 0x2e},
212         {0x5044, 0x3a},
213         {0x5045, 0x3c},
214         {0x5046, 0x44},
215         {0x5047, 0xf8},
216         {0x5048, 0x08},
217         {0x5049, 0x70},
218         {0x504a, 0xf0},
219         {0x504b, 0xf0},
220         {0x500c, 0x03},
221         {0x500d, 0x20},
222         {0x500e, 0x02},
223         {0x500f, 0x5c},
224         {0x5010, 0x48},
225         {0x5011, 0x00},
226         {0x5012, 0x66},
227         {0x5013, 0x03},
228         {0x5014, 0x30},
229         {0x5015, 0x02},
230         {0x5016, 0x7c},
231         {0x5017, 0x40},
232         {0x5018, 0x00},
233         {0x5019, 0x66},
234         {0x501a, 0x03},
235         {0x501b, 0x10},
236         {0x501c, 0x02},
237         {0x501d, 0x7c},
238         {0x501e, 0x3a},
239         {0x501f, 0x00},
240         {0x5020, 0x66},
241         {0x506e, 0x44},
242         {0x5064, 0x08},
243         {0x5065, 0x10},
244         {0x5066, 0x12},
245         {0x5067, 0x02},
246         {0x506c, 0x08},
247         {0x506d, 0x10},
248         {0x506f, 0xa6},
249         {0x5068, 0x08},
250
251
252         {0x5069, 0x10},
253         {0x506a, 0x04},
254         {0x506b, 0x12},
255         {0x507e, 0x40},
256         {0x507f, 0x20},
257         {0x507b, 0x02},
258         {0x507a, 0x01},
259         {0x5084, 0x0c},
260         {0x5085, 0x3e},
261         {0x5005, 0x80},
262         {0x3a0f, 0x30},
263         {0x3a10, 0x28},
264         {0x3a1b, 0x32},
265         {0x3a1e, 0x26},
266         {0x3a11, 0x60},
267         {0x3a1f, 0x14},
268         {0x5060, 0x69},
269         {0x5061, 0x7d},
270         {0x5062, 0x7d},
271         {0x5063, 0x69},
272         {0x3004, 0x20},
273                 {0x0100, 0x01},
274         {0x0000, 0x00}
275 };
276
277 /* 1280x720 */
278 static struct reginfo sensor_720p[]=
279 {
280         {0x0103, 0x01   },
281         {0x3000, 0x0f   },
282         {0x3001, 0xff   },
283         {0x3002, 0xff   },
284         //{0x0100, 0x01 },  //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array
285         {0x3633, 0x3d   },
286         {0x3620, 0x02   },
287         {0x3631, 0x11   },
288         {0x3612, 0x04   },
289         {0x3630, 0x20   },
290         {0x4702, 0x02   },
291         {0x370c, 0x34   },
292         {0x3004, 0x10   },
293         {0x3005, 0x24   },
294         {0x3800, 0x00   },
295         {0x3801, 0xa0   },
296         {0x3802, 0x00   },
297         {0x3803, 0xf0   },
298         {0x3804, 0x05   },
299         {0x3805, 0xbf   },
300         {0x3806, 0x03   },
301         {0x3807, 0xcb   },
302         {0x3808, 0x05   },
303         {0x3809, 0x00   },
304         {0x380a, 0x02   },
305         {0x380b, 0xd0   },
306         {0x380c, 0x06   },
307         {0x380d, 0x4c   },
308         {0x380e, 0x02   },
309         {0x380f, 0xe8   },
310         {0x3811, 0x10   },
311         {0x3813, 0x06   },
312         {0x3814, 0x11   },
313         {0x3815, 0x11   },
314         {0x3a02, 0x02   },
315         {0x3a03, 0xe8   },
316         {0x3a08, 0x00   },
317         {0x3a09, 0x6f   },
318         {0x3a0a, 0x00   },
319         {0x3a0b, 0x5d   },
320         {0x3a0d, 0x08   },
321         {0x3a0e, 0x06   },
322         {0x3a14, 0x02   },
323         {0x3a15, 0x9a   },
324             {0x4708, 0x01  },
325         {0x3623, 0x02   },
326         {0x3634, 0x44   },
327         {0x3701, 0x41   },
328         {0x3702, 0x30   },
329         {0x3703, 0x48   },
330         {0x3704, 0x48   },
331         {0x3705, 0x18   },
332         {0x3820, 0x80   },
333         {0x3821, 0x00   },
334         {0x370a, 0x12   },
335         {0x4608, 0x00   },
336         {0x4609, 0x80   },
337         {0x4300, 0x32   },
338         {0x5086, 0x02   },
339         {0x5000, 0xfb   },
340         {0x5001, 0x1f   },
341         {0x5002, 0x00   },
342         {0x5025, 0x0e   },
343         {0x5026, 0x18   },
344         {0x5027, 0x34   },
345         {0x5028, 0x4c   },
346         {0x5029, 0x62   },
347         {0x502a, 0x74   },
348         {0x502b, 0x85   },
349         {0x502c, 0x92   },
350         {0x502d, 0x9e   },
351         {0x502e, 0xb2   },
352         {0x502f, 0xc0   },
353         {0x5030, 0xcc   },
354         {0x5031, 0xe0   },
355         {0x5032, 0xee   },
356         {0x5033, 0xf6   },
357         {0x5034, 0x11   },
358         {0x5070, 0x1c   },
359         {0x5071, 0x5b   },
360         {0x5072, 0x05   },
361         {0x5073, 0x20   },
362         {0x5074, 0x94   },
363         {0x5075, 0xb4   },
364         {0x5076, 0xb4   },
365         {0x5077, 0xaf   },
366         {0x5078, 0x05   },
367         {0x5079, 0x98   },
368         {0x507a, 0x21   },
369         {0x5035, 0x6a   },
370         {0x5036, 0x11   },
371         {0x5037, 0x92   },
372         {0x5038, 0x21   },
373         {0x5039, 0xe1   },
374         {0x503a, 0x01   },
375         {0x503c, 0x05   },
376         {0x503d, 0x08   },
377         {0x503e, 0x08   },
378         {0x503f, 0x64   },
379         {0x5040, 0x58   },
380         {0x5041, 0x2a   },
381         {0x5042, 0xc5   },
382         {0x5043, 0x2e   },
383         {0x5044, 0x3a   },
384         {0x5045, 0x3c   },
385         {0x5046, 0x44   },
386         {0x5047, 0xf8   },
387         {0x5048, 0x08   },
388         {0x5049, 0x70   },
389         {0x504a, 0xf0   },
390         {0x504b, 0xf0   },
391         {0x500c, 0x03   },
392         {0x500d, 0x20   },
393         {0x500e, 0x02   },
394         {0x500f, 0x5c   },
395         {0x5010, 0x48   },
396         {0x5011, 0x00   },
397         {0x5012, 0x66   },
398         {0x5013, 0x03   },
399         {0x5014, 0x30   },
400         {0x5015, 0x02   },
401         {0x5016, 0x7c   },
402         {0x5017, 0x40   },
403         {0x5018, 0x00   },
404         {0x5019, 0x66   },
405         {0x501a, 0x03   },
406         {0x501b, 0x10   },
407         {0x501c, 0x02   },
408         {0x501d, 0x7c   },
409         {0x501e, 0x3a   },
410         {0x501f, 0x00   },
411         {0x5020, 0x66   },
412         {0x506e, 0x44   },
413         {0x5064, 0x08   },
414         {0x5065, 0x10   },
415         {0x5066, 0x12   },
416         {0x5067, 0x02   },
417         {0x506c, 0x08   },
418         {0x506d, 0x10   },
419         {0x506f, 0xa6   },
420         {0x5068, 0x08   },
421         {0x5069, 0x10   },
422         {0x506a, 0x04   },
423         {0x506b, 0x12   },
424         {0x507e, 0x40   },
425         {0x507f, 0x20   },
426         {0x507b, 0x02   },
427         {0x507a, 0x01   },
428         {0x5084, 0x0c   },
429         {0x5085, 0x3e   },
430         {0x5005, 0x80   },
431         {0x3a0f, 0x30   },
432         {0x3a10, 0x28   },
433         {0x3a1b, 0x32   },
434         {0x3a1e, 0x26   },
435         {0x3a11, 0x60   },
436         {0x3a1f, 0x14   },
437         {0x5060, 0x69   },
438         {0x5061, 0x7d   },
439         {0x5062, 0x7d   },
440         {0x5063, 0x69   },
441         {0x0100, 0x01   },
442         {0x0000 ,0x00}
443
444 };
445
446 /* 1600X1200 UXGA */
447 static struct reginfo sensor_uxga[] =
448 {
449     {0x3800, 0x00},
450         {0x3801, 0x00},
451         {0x3802, 0x00},
452         {0x3803, 0x00},
453         {0x3804, 0x06},
454         {0x3805, 0x5f},
455         {0x3806, 0x04},
456         {0x3807, 0xbb},
457         {0x3808, 0x06},
458         {0x3809, 0x40},
459         {0x380a, 0x04},
460         {0x380b, 0xb0},
461         {0x380c, 0x07},
462         {0x380d, 0x9f},
463         {0x380e, 0x04},
464         {0x380f, 0xd0},
465         {0x3811, 0x10},
466         {0x3813, 0x06},
467         {0x3814, 0x11},
468         {0x3815, 0x11},
469         {0x3a02, 0x04},
470         {0x3a03, 0xd0},
471         {0x3a08, 0x00},
472         {0x3a09, 0xb8},
473         {0x3a0a, 0x00},
474         {0x3a0b, 0x9a},
475         {0x3a0d, 0x08},
476         {0x3a0e, 0x06},
477         {0x3a14, 0x04},
478         {0x3a15, 0x50},
479         {0x3623, 0x00},
480         {0x3634, 0x44},
481         {0x3701, 0x44},
482         {0x3702, 0x30},
483         {0x3703, 0x48},
484         {0x3704, 0x48},
485         {0x3705, 0x18},
486         {0x3820, 0x80},
487         {0x3821, 0x00},
488         {0x370a, 0x12},
489         {0x4608, 0x00},
490         {0x4609, 0x80},
491         {0x5002, 0x00},
492         {0x3005, 0x24},
493         {0x3004, 0x20},
494         {0x0000, 0x00}
495 };
496
497 /* 1280X1024 SXGA */
498 static struct reginfo sensor_sxga[] =
499 {
500         {0x0, 0x0}
501 };
502 /* 1024X768 SXGA */
503 static struct reginfo sensor_xga[] =
504 {
505         {0x0, 0x0}
506 };
507 /* 800X600 SVGA*/
508 static struct reginfo sensor_svga[] =
509 {
510                 {0x0100, 0x00},    //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
511         {0x3800, 0x00},
512         {0x3801, 0x00},
513         {0x3802, 0x00},
514         {0x3803, 0x00},
515         {0x3804, 0x06},
516         {0x3805, 0x5f},
517         {0x3806, 0x04},
518         {0x3807, 0xb7},
519         {0x3808, 0x03},
520         {0x3809, 0x20},
521         {0x380a, 0x02},
522         {0x380b, 0x58},
523         {0x380c, 0x05},
524         {0x380d, 0x14},
525         {0x380e, 0x02},
526         {0x380f, 0x68},
527         {0x3811, 0x08},
528         {0x3813, 0x02},
529         {0x3814, 0x31},
530         {0x3815, 0x31},
531         {0x3a02, 0x02},
532         {0x3a03, 0x68},
533         {0x3a08, 0x00},
534         {0x3a09, 0x5c},
535         {0x3a0a, 0x00},
536         {0x3a0b, 0x4d},
537         {0x3a0d, 0x08},
538         {0x3a0e, 0x06},
539         {0x3a14, 0x02},
540         {0x3a15, 0x28},
541         {0x3623, 0x00},
542         {0x3634, 0x76},
543         {0x3701, 0x44},
544         {0x3702, 0x18},
545         {0x3703, 0x24},
546         {0x3704, 0x24},
547         {0x3705, 0x0c},
548         {0x3820, 0x81},
549         {0x3821, 0x01},
550         {0x370a, 0x52},
551         {0x4608, 0x00},
552         {0x4609, 0x80},
553         {0x5002, 0x10},
554         {0x3005, 0x18},
555         {0x3004, 0x20},
556                 {0x0100, 0x01},         //software wake
557         {0x0000, 0x00}
558 };
559
560 /* 640X480 VGA */
561 static struct reginfo sensor_vga[] =
562 {
563         {0x0, 0x0}
564 };
565
566 /* 352X288 CIF */
567 static struct reginfo sensor_cif[] =
568 {
569         {0x0, 0x0}
570 };
571
572 /* 320*240 QVGA */
573 static  struct reginfo sensor_qvga[] =
574 {
575         {0x0, 0x0}
576 };
577
578 /* 176X144 QCIF*/
579 static struct reginfo sensor_qcif[] =
580 {
581         {0x0, 0x0}
582 };
583 #if 0
584 /* 160X120 QQVGA*/
585 static struct reginfo ov2655_qqvga[] =
586 {
587
588     {0x300E, 0x34},
589     {0x3011, 0x01},
590     {0x3012, 0x10},
591     {0x302a, 0x02},
592     {0x302b, 0xE6},
593     {0x306f, 0x14},
594     {0x3362, 0x90},
595
596     {0x3070, 0x5d},
597     {0x3072, 0x5d},
598     {0x301c, 0x07},
599     {0x301d, 0x07},
600
601     {0x3020, 0x01},
602     {0x3021, 0x18},
603     {0x3022, 0x00},
604     {0x3023, 0x06},
605     {0x3024, 0x06},
606     {0x3025, 0x58},
607     {0x3026, 0x02},
608     {0x3027, 0x61},
609     {0x3088, 0x00},
610     {0x3089, 0xa0},
611     {0x308a, 0x00},
612     {0x308b, 0x78},
613     {0x3316, 0x64},
614     {0x3317, 0x25},
615     {0x3318, 0x80},
616     {0x3319, 0x08},
617     {0x331a, 0x0a},
618     {0x331b, 0x07},
619     {0x331c, 0x80},
620     {0x331d, 0x38},
621     {0x3100, 0x00},
622     {0x3302, 0x11},
623
624     {0x0, 0x0},
625 };
626
627
628
629 static  struct reginfo ov2655_Sharpness_auto[] =
630 {
631     {0x3306, 0x00},
632 };
633
634 static  struct reginfo ov2655_Sharpness1[] =
635 {
636     {0x3306, 0x08},
637     {0x3371, 0x00},
638 };
639
640 static  struct reginfo ov2655_Sharpness2[][3] =
641 {
642     //Sharpness 2
643     {0x3306, 0x08},
644     {0x3371, 0x01},
645 };
646
647 static  struct reginfo ov2655_Sharpness3[] =
648 {
649     //default
650     {0x3306, 0x08},
651     {0x332d, 0x02},
652 };
653 static  struct reginfo ov2655_Sharpness4[]=
654 {
655     //Sharpness 4
656     {0x3306, 0x08},
657     {0x332d, 0x03},
658 };
659
660 static  struct reginfo ov2655_Sharpness5[] =
661 {
662     //Sharpness 5
663     {0x3306, 0x08},
664     {0x332d, 0x04},
665 };
666 #endif
667
668 static  struct reginfo sensor_ClrFmt_YUYV[]=
669 {
670     {0x4300, 0x30},
671     {0x0000, 0x00}
672 };
673
674 static  struct reginfo sensor_ClrFmt_UYVY[]=
675 {
676     {0x4300, 0x32},
677     {0x0000, 0x00}
678 };
679
680 #if CONFIG_SENSOR_WhiteBalance
681 static  struct reginfo sensor_WhiteB_Auto[]=
682 {
683     {0x3406, 0x00},  //AWB auto, bit[1]:0,auto
684     {0x0000, 0x00}
685 };
686 /* Cloudy Colour Temperature : 6500K - 8000K  */
687 static  struct reginfo sensor_WhiteB_Cloudy[]=
688 {
689     {0x3406, 0x01},
690     {0x3400, 0x07},
691     {0x3401, 0x08},
692     {0x3402, 0x04},
693     {0x3403, 0x00},
694     {0x3404, 0x05},
695     {0x3405, 0x00},
696     {0x0000, 0x00}
697 };
698 /* ClearDay Colour Temperature : 5000K - 6500K  */
699 static  struct reginfo sensor_WhiteB_ClearDay[]=
700 {
701     //Sunny
702     {0x3406, 0x01},
703     {0x3400, 0x07},
704     {0x3401, 0x02},
705     {0x3402, 0x04},
706     {0x3403, 0x00},
707     {0x3404, 0x05},
708     {0x3405, 0x15},
709     {0x0000, 0x00}
710 };
711 /* Office Colour Temperature : 3500K - 5000K  */
712 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
713 {
714     //Office
715     {0x3406, 0x01},
716     {0x3400, 0x06},
717     {0x3401, 0x2a},
718     {0x3402, 0x04},
719     {0x3403, 0x00},
720     {0x3404, 0x07},
721     {0x3405, 0x24},
722     {0x0000, 0x00}
723
724 };
725 /* Home Colour Temperature : 2500K - 3500K  */
726 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
727 {
728     //Home
729     {0x3406, 0x01},
730     {0x3400, 0x04},
731     {0x3401, 0x58},
732     {0x3402, 0x04},
733     {0x3403, 0x00},
734     {0x3404, 0x07},
735     {0x3405, 0x24},
736     {0x0000, 0x00}
737 };
738 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
739     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
740 };
741 #endif
742
743 #if CONFIG_SENSOR_Brightness
744 static  struct reginfo sensor_Brightness0[]=
745 {
746     // Brightness -2
747     {0x0000, 0x00}
748 };
749
750 static  struct reginfo sensor_Brightness1[]=
751 {
752     // Brightness -1
753
754     {0x0000, 0x00}
755 };
756
757 static  struct reginfo sensor_Brightness2[]=
758 {
759     //  Brightness 0
760
761     {0x0000, 0x00}
762 };
763
764 static  struct reginfo sensor_Brightness3[]=
765 {
766     // Brightness +1
767
768     {0x0000, 0x00}
769 };
770
771 static  struct reginfo sensor_Brightness4[]=
772 {
773     //  Brightness +2
774
775     {0x0000, 0x00}
776 };
777
778 static  struct reginfo sensor_Brightness5[]=
779 {
780     //  Brightness +3
781
782     {0x0000, 0x00}
783 };
784 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
785     sensor_Brightness4, sensor_Brightness5,NULL,
786 };
787
788 #endif
789
790 #if CONFIG_SENSOR_Effect
791 static  struct reginfo sensor_Effect_Normal[] =
792 {
793     {0x507b, 0x00},
794     {0x0000, 0x00}
795 };
796
797 static  struct reginfo sensor_Effect_WandB[] =
798 {
799     {0x507b, 0x20},
800     {0x0000, 0x00}
801 };
802
803 static  struct reginfo sensor_Effect_Sepia[] =
804 {
805     {0x507b, 0x18},
806     {0x507e, 0x40},
807     {0x507f, 0xa0},
808     {0x0000, 0x00}
809 };
810
811 static  struct reginfo sensor_Effect_Negative[] =
812 {
813     //Negative
814     {0x507b, 0x40}, //bit[6] negative
815     {0x0000, 0x00}
816 };
817 static  struct reginfo sensor_Effect_Bluish[] =
818 {
819     // Bluish
820     {0x507b, 0x18},
821     {0x507e, 0xa0},
822     {0x507f, 0x40},
823     {0x0000, 0x00}
824 };
825
826 static  struct reginfo sensor_Effect_Green[] =
827 {
828     //  Greenish
829     {0x507b, 0x18},
830     {0x507e, 0x60},
831     {0x507f, 0x60},
832     {0x0000, 0x00}
833 };
834 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
835     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
836 };
837 #endif
838 #if CONFIG_SENSOR_Exposure
839 static  struct reginfo sensor_Exposure0[]=
840 {
841         {0x0000, 0x00}
842 };
843
844 static  struct reginfo sensor_Exposure1[]=
845 {
846     {0x0000, 0x00}
847 };
848
849 static  struct reginfo sensor_Exposure2[]=
850 {
851     {0x0000, 0x00}
852 };
853
854 static  struct reginfo sensor_Exposure3[]=
855 {
856     {0x0000, 0x00}
857 };
858
859 static  struct reginfo sensor_Exposure4[]=
860 {
861     {0x0000, 0x00}
862 };
863
864 static  struct reginfo sensor_Exposure5[]=
865 {
866     {0x0000, 0x00}
867 };
868
869 static  struct reginfo sensor_Exposure6[]=
870 {
871     {0x0000, 0x00}
872 };
873
874 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
875     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
876 };
877 #endif
878 #if CONFIG_SENSOR_Saturation
879 static  struct reginfo sensor_Saturation0[]=
880 {
881     {0x0000, 0x00}
882 };
883
884 static  struct reginfo sensor_Saturation1[]=
885 {
886     {0x0000, 0x00}
887 };
888
889 static  struct reginfo sensor_Saturation2[]=
890 {
891     {0x0000, 0x00}
892 };
893 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
894
895 #endif
896 #if CONFIG_SENSOR_Contrast
897 static  struct reginfo sensor_Contrast0[]=
898 {
899     {0x0000, 0x00}
900 };
901
902 static  struct reginfo sensor_Contrast1[]=
903 {
904     {0x0000, 0x00}
905 };
906
907 static  struct reginfo sensor_Contrast2[]=
908 {
909     {0x0000, 0x00}
910 };
911
912 static  struct reginfo sensor_Contrast3[]=
913 {
914     {0x0000, 0x00}
915 };
916
917 static  struct reginfo sensor_Contrast4[]=
918 {
919     {0x0000, 0x00}
920 };
921
922
923 static  struct reginfo sensor_Contrast5[]=
924 {
925     {0x0000, 0x00}
926 };
927
928 static  struct reginfo sensor_Contrast6[]=
929 {
930     {0x0000, 0x00}
931 };
932 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
933     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
934 };
935
936 #endif
937 #if CONFIG_SENSOR_Mirror
938 static  struct reginfo sensor_MirrorOn[]=
939 {
940     {0x0000, 0x00}
941 };
942
943 static  struct reginfo sensor_MirrorOff[]=
944 {
945     {0x0000, 0x00}
946 };
947 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
948 #endif
949 #if CONFIG_SENSOR_Flip
950 static  struct reginfo sensor_FlipOn[]=
951 {
952     {0x0000, 0x00}
953 };
954
955 static  struct reginfo sensor_FlipOff[]=
956 {
957     {0x0000, 0x00}
958 };
959 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
960
961 #endif
962 #if CONFIG_SENSOR_Scene
963 static  struct reginfo sensor_SceneAuto[] =
964 {
965     {0x3a00, 0x78},
966     {0x0000, 0x00}
967 };
968
969 static  struct reginfo sensor_SceneNight[] =
970 {
971     {0x3003, 0x80},
972         {0x3004, 0x20},
973         {0x3005, 0x18},
974         {0x3006, 0x0d},
975         {0x3a00, 0x7c},
976         {0x3a02 ,0x07},
977         {0x3a03 ,0x38},
978         {0x3a14 ,0x07},
979         {0x3a15 ,0x38},
980     {0x0000, 0x00}
981 };
982 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
983
984 #endif
985 #if CONFIG_SENSOR_DigitalZoom
986 static struct reginfo sensor_Zoom0[] =
987 {
988     {0x0, 0x0},
989 };
990
991 static struct reginfo sensor_Zoom1[] =
992 {
993      {0x0, 0x0},
994 };
995
996 static struct reginfo sensor_Zoom2[] =
997 {
998     {0x0, 0x0},
999 };
1000
1001
1002 static struct reginfo sensor_Zoom3[] =
1003 {
1004     {0x0, 0x0},
1005 };
1006 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1007 #endif
1008 static const struct v4l2_querymenu sensor_menus[] =
1009 {
1010         #if CONFIG_SENSOR_WhiteBalance
1011     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1012     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1013     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1014     #endif
1015
1016         #if CONFIG_SENSOR_Effect
1017     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1018     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1019     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
1020     #endif
1021
1022         #if CONFIG_SENSOR_Scene
1023     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1024     #endif
1025
1026         #if CONFIG_SENSOR_Flash
1027     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1028     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1029     #endif
1030 };
1031
1032 static const struct v4l2_queryctrl sensor_controls[] =
1033 {
1034         #if CONFIG_SENSOR_WhiteBalance
1035     {
1036         .id             = V4L2_CID_DO_WHITE_BALANCE,
1037         .type           = V4L2_CTRL_TYPE_MENU,
1038         .name           = "White Balance Control",
1039         .minimum        = 0,
1040         .maximum        = 4,
1041         .step           = 1,
1042         .default_value = 0,
1043     },
1044     #endif
1045
1046         #if CONFIG_SENSOR_Brightness
1047         {
1048         .id             = V4L2_CID_BRIGHTNESS,
1049         .type           = V4L2_CTRL_TYPE_INTEGER,
1050         .name           = "Brightness Control",
1051         .minimum        = -3,
1052         .maximum        = 2,
1053         .step           = 1,
1054         .default_value = 0,
1055     },
1056     #endif
1057
1058         #if CONFIG_SENSOR_Effect
1059         {
1060         .id             = V4L2_CID_EFFECT,
1061         .type           = V4L2_CTRL_TYPE_MENU,
1062         .name           = "Effect Control",
1063         .minimum        = 0,
1064         .maximum        = 5,
1065         .step           = 1,
1066         .default_value = 0,
1067     },
1068         #endif
1069
1070         #if CONFIG_SENSOR_Exposure
1071         {
1072         .id             = V4L2_CID_EXPOSURE,
1073         .type           = V4L2_CTRL_TYPE_INTEGER,
1074         .name           = "Exposure Control",
1075         .minimum        = 0,
1076         .maximum        = 6,
1077         .step           = 1,
1078         .default_value = 0,
1079     },
1080         #endif
1081
1082         #if CONFIG_SENSOR_Saturation
1083         {
1084         .id             = V4L2_CID_SATURATION,
1085         .type           = V4L2_CTRL_TYPE_INTEGER,
1086         .name           = "Saturation Control",
1087         .minimum        = 0,
1088         .maximum        = 2,
1089         .step           = 1,
1090         .default_value = 0,
1091     },
1092     #endif
1093
1094         #if CONFIG_SENSOR_Contrast
1095         {
1096         .id             = V4L2_CID_CONTRAST,
1097         .type           = V4L2_CTRL_TYPE_INTEGER,
1098         .name           = "Contrast Control",
1099         .minimum        = -3,
1100         .maximum        = 3,
1101         .step           = 1,
1102         .default_value = 0,
1103     },
1104         #endif
1105
1106         #if CONFIG_SENSOR_Mirror
1107         {
1108         .id             = V4L2_CID_HFLIP,
1109         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1110         .name           = "Mirror Control",
1111         .minimum        = 0,
1112         .maximum        = 1,
1113         .step           = 1,
1114         .default_value = 0,
1115     },
1116     #endif
1117
1118         #if CONFIG_SENSOR_Flip
1119         {
1120         .id             = V4L2_CID_VFLIP,
1121         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1122         .name           = "Flip Control",
1123         .minimum        = 0,
1124         .maximum        = 1,
1125         .step           = 1,
1126         .default_value = 0,
1127     },
1128     #endif
1129
1130         #if CONFIG_SENSOR_Scene
1131     {
1132         .id             = V4L2_CID_SCENE,
1133         .type           = V4L2_CTRL_TYPE_MENU,
1134         .name           = "Scene Control",
1135         .minimum        = 0,
1136         .maximum        = 1,
1137         .step           = 1,
1138         .default_value = 0,
1139     },
1140     #endif
1141
1142         #if CONFIG_SENSOR_DigitalZoom
1143     {
1144         .id             = V4L2_CID_ZOOM_RELATIVE,
1145         .type           = V4L2_CTRL_TYPE_INTEGER,
1146         .name           = "DigitalZoom Control",
1147         .minimum        = -1,
1148         .maximum        = 1,
1149         .step           = 1,
1150         .default_value = 0,
1151     }, {
1152         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1153         .type           = V4L2_CTRL_TYPE_INTEGER,
1154         .name           = "DigitalZoom Control",
1155         .minimum        = 0,
1156         .maximum        = 3,
1157         .step           = 1,
1158         .default_value = 0,
1159     },
1160     #endif
1161
1162         #if CONFIG_SENSOR_Focus
1163         {
1164         .id             = V4L2_CID_FOCUS_RELATIVE,
1165         .type           = V4L2_CTRL_TYPE_INTEGER,
1166         .name           = "Focus Control",
1167         .minimum        = -1,
1168         .maximum        = 1,
1169         .step           = 1,
1170         .default_value = 0,
1171     }, {
1172         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1173         .type           = V4L2_CTRL_TYPE_INTEGER,
1174         .name           = "Focus Control",
1175         .minimum        = 0,
1176         .maximum        = 255,
1177         .step           = 1,
1178         .default_value = 125,
1179     },
1180     #endif
1181
1182         #if CONFIG_SENSOR_Flash
1183         {
1184         .id             = V4L2_CID_FLASH,
1185         .type           = V4L2_CTRL_TYPE_MENU,
1186         .name           = "Flash Control",
1187         .minimum        = 0,
1188         .maximum        = 3,
1189         .step           = 1,
1190         .default_value = 0,
1191     },
1192         #endif
1193 };
1194
1195 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1196 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1197 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1198 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1199 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1200 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1201 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1202 static int sensor_resume(struct soc_camera_device *icd);
1203 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1204 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1205 #if CONFIG_SENSOR_Effect
1206 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1207 #endif
1208 #if CONFIG_SENSOR_WhiteBalance
1209 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1210 #endif
1211 static int sensor_deactivate(struct i2c_client *client);
1212
1213 static struct soc_camera_ops sensor_ops =
1214 {
1215     .suspend                     = sensor_suspend,
1216     .resume                       = sensor_resume,
1217     .set_bus_param              = sensor_set_bus_param,
1218     .query_bus_param    = sensor_query_bus_param,
1219     .controls           = sensor_controls,
1220     .menus                         = sensor_menus,
1221     .num_controls               = ARRAY_SIZE(sensor_controls),
1222     .num_menus          = ARRAY_SIZE(sensor_menus),
1223 };
1224
1225 /* only one fixed colorspace per pixelcode */
1226 struct sensor_datafmt {
1227         enum v4l2_mbus_pixelcode code;
1228         enum v4l2_colorspace colorspace;
1229 };
1230
1231 /* Find a data format by a pixel code in an array */
1232 static const struct sensor_datafmt *sensor_find_datafmt(
1233         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1234         int n)
1235 {
1236         int i;
1237         for (i = 0; i < n; i++)
1238                 if (fmt[i].code == code)
1239                         return fmt + i;
1240
1241         return NULL;
1242 }
1243
1244 static const struct sensor_datafmt sensor_colour_fmts[] = {
1245     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1246     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1247 };
1248
1249 typedef struct sensor_info_priv_s
1250 {
1251     int whiteBalance;
1252     int brightness;
1253     int contrast;
1254     int saturation;
1255     int effect;
1256     int scene;
1257     int digitalzoom;
1258     int focus;
1259     int flash;
1260     int exposure;
1261         bool snap2preview;
1262         bool video2preview;
1263     unsigned char mirror;                                        /* HFLIP */
1264     unsigned char flip;                                          /* VFLIP */
1265     unsigned int winseqe_cur_addr;
1266     struct sensor_datafmt fmt;
1267     unsigned int funmodule_state;
1268 } sensor_info_priv_t;
1269
1270 struct sensor
1271 {
1272     struct v4l2_subdev subdev;
1273     struct i2c_client *client;
1274     sensor_info_priv_t info_priv;
1275     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1276 #if CONFIG_SENSOR_I2C_NOSCHED
1277         atomic_t tasklock_cnt;
1278 #endif
1279         struct rkcamera_platform_data *sensor_io_request;
1280     struct rkcamera_gpio_res *sensor_gpio_res;
1281 };
1282
1283
1284 static struct sensor* to_sensor(const struct i2c_client *client)
1285 {
1286     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1287 }
1288
1289 static int sensor_task_lock(struct i2c_client *client, int lock)
1290 {
1291 #if CONFIG_SENSOR_I2C_NOSCHED
1292         int cnt = 3;
1293     struct sensor *sensor = to_sensor(client);
1294
1295         if (lock) {
1296                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1297                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1298                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1299                                 msleep(35);
1300                                 cnt--;
1301                         }
1302                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1303                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1304                                 goto sensor_task_lock_err;
1305                         }
1306                         preempt_disable();
1307                 }
1308
1309                 atomic_add(1, &sensor->tasklock_cnt);
1310         } else {
1311                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1312                         atomic_sub(1, &sensor->tasklock_cnt);
1313
1314                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1315                                 preempt_enable();
1316                 }
1317         }
1318         return 0;
1319 sensor_task_lock_err:
1320         return -1;  
1321 #else
1322     return 0;
1323 #endif
1324
1325 }
1326
1327 /* sensor register write */
1328 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1329 {
1330     int err,cnt;
1331     u8 buf[3];
1332     struct i2c_msg msg[1];
1333
1334     buf[0] = reg >> 8;
1335     buf[1] = reg & 0xFF;
1336     buf[2] = val;
1337
1338     msg->addr = client->addr;
1339     msg->flags = client->flags;
1340     msg->buf = buf;
1341     msg->len = sizeof(buf);
1342     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1343     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1344
1345     cnt = 3;
1346     err = -EAGAIN;
1347
1348     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1349         err = i2c_transfer(client->adapter, msg, 1);
1350
1351         if (err >= 0) {
1352             return 0;
1353         } else {
1354                 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1355                 udelay(10);
1356         }
1357     }
1358
1359     return err;
1360 }
1361
1362 /* sensor register read */
1363 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1364 {
1365     int err,cnt;
1366     u8 buf[2];
1367     struct i2c_msg msg[2];
1368
1369     buf[0] = reg >> 8;
1370     buf[1] = reg & 0xFF;
1371
1372     msg[0].addr = client->addr;
1373     msg[0].flags = client->flags;
1374     msg[0].buf = buf;
1375     msg[0].len = sizeof(buf);
1376     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1377     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1378
1379     msg[1].addr = client->addr;
1380     msg[1].flags = client->flags|I2C_M_RD;
1381     msg[1].buf = buf;
1382     msg[1].len = 1;
1383     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1384     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1385
1386     cnt = 3;
1387     err = -EAGAIN;
1388     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1389         err = i2c_transfer(client->adapter, msg, 2);
1390
1391         if (err >= 0) {
1392             *val = buf[0];
1393             return 0;
1394         } else {
1395                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1396             udelay(10);
1397         }
1398     }
1399
1400     return err;
1401 }
1402
1403 /* write a array of registers  */
1404 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1405 {
1406     int err = 0, cnt;
1407     int i = 0;
1408 #if CONFIG_SENSOR_I2C_RDWRCHK    
1409         char valchk;
1410 #endif
1411
1412         cnt = 0;
1413         if (sensor_task_lock(client, 1) < 0)
1414                 goto sensor_write_array_end;
1415
1416     while (regarray[i].reg != 0)
1417     {
1418         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1419         if (err < 0)
1420         {
1421             if (cnt-- > 0) {
1422                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1423                                 i = 0;
1424                                 continue;
1425             } else {
1426                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1427                 err = -EPERM;
1428                                 goto sensor_write_array_end;
1429             }
1430         } else {
1431         #if CONFIG_SENSOR_I2C_RDWRCHK
1432                         sensor_read(client, regarray[i].reg, &valchk);
1433                         if (valchk != regarray[i].val)
1434                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1435                 #endif
1436         }
1437         i++;
1438     }
1439
1440 sensor_write_array_end:
1441         sensor_task_lock(client,0);
1442         return err;
1443 }
1444 #if CONFIG_SENSOR_I2C_RDWRCHK
1445 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1446 {
1447     int cnt;
1448     int i = 0;
1449         char valchk;
1450
1451         cnt = 0;
1452         valchk = 0;
1453     while (regarray[i].reg != 0)
1454     {
1455                 sensor_read(client, regarray[i].reg, &valchk);
1456                 if (valchk != regarray[i].val)
1457                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1458
1459         i++;
1460     }
1461     return 0;
1462 }
1463 #endif
1464 static int sensor_ioctrl(struct soc_camera_device *icd,enum rksensor_power_cmd cmd, int on)
1465 {
1466         struct soc_camera_link *icl = to_soc_camera_link(icd);
1467         int ret = 0;
1468         return 0;
1469     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1470         switch (cmd)
1471         {
1472                 case Sensor_PowerDown:
1473                 {
1474                         if (icl->powerdown) {
1475                                 ret = icl->powerdown(icd->pdev, on);
1476                                 if (ret == RK_CAM_IO_SUCCESS) {
1477                                         if (on == 0) {
1478                                                 mdelay(2);
1479                                                 if (icl->reset)
1480                                                         icl->reset(icd->pdev);
1481                                         }
1482                                 } else if (ret == RK_CAM_EIO_REQUESTFAIL) {
1483                                         ret = -ENODEV;
1484                                         goto sensor_power_end;
1485                                 }
1486                         }
1487                         break;
1488                 }
1489                 case Sensor_Flash:
1490                 {
1491                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1492                 struct sensor *sensor = to_sensor(client);
1493
1494                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1495                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1496                         }
1497             break;
1498                 }
1499                 default:
1500                 {
1501                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1502                         break;
1503                 }
1504         }
1505 sensor_power_end:
1506         return ret;
1507 }
1508 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1509 {
1510     struct i2c_client *client = v4l2_get_subdevdata(sd);
1511     struct soc_camera_device *icd = client->dev.platform_data;
1512     struct sensor *sensor = to_sensor(client);
1513         const struct v4l2_queryctrl *qctrl;
1514     const struct sensor_datafmt *fmt;
1515     char value;
1516     int ret,pid = 0,i = 0,j=0;
1517     struct rkcamera_platform_data* tmp_plat_data =(struct rkcamera_platform_data*)val;
1518     sensor_init_data_p = sensor_init_data;
1519         sensor_init_winseq_p = sensor_svga;
1520         sensor_init_width = 800;
1521         sensor_init_height = 600;
1522         if (tmp_plat_data != NULL) { 
1523                 for(i = 0;i < RK_CAM_NUM_PER_HOST;i++){
1524                         if ((tmp_plat_data->sensor_init_data[i])&& tmp_plat_data->info[i].dev_name &&
1525                                 (strcmp(tmp_plat_data->info[i].dev_name, dev_name(icd->pdev)) == 0))
1526                                         break;
1527                         }
1528                 }
1529         if(tmp_plat_data  && (i < RK_CAM_NUM_PER_HOST) && tmp_plat_data->sensor_init_data[i]){
1530         //user has defined the init data
1531                 //init reg
1532                 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data && (sizeof(struct reginfo) != sizeof(struct reginfo_t))){
1533                         for(j = 0;j< sizeof(sensor_init_data)/sizeof(struct reginfo);j++){
1534                                 sensor_init_data[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].reg;
1535                                 sensor_init_data[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].val;
1536                                 }
1537                         sensor_init_data_p = sensor_init_data;
1538                         }
1539                 else if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data){
1540                         sensor_init_data_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data);
1541                         }
1542                 //init winseq
1543                 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq && (sizeof(struct reginfo) != sizeof(struct reginfo_t))){
1544                         int tmp_winseq_size = tmp_plat_data->sensor_init_data[i]->rk_sensor_winseq_size;
1545                         if(sensor_init_winseq_board)
1546                                 {
1547                                 vfree(sensor_init_winseq_board);
1548                                 sensor_init_winseq_board = NULL;
1549                                 }
1550                         sensor_init_winseq_board = (struct reginfo*)vmalloc(tmp_winseq_size);
1551                         if(!sensor_init_winseq_board)
1552                                 SENSOR_TR("%s :vmalloc erro !",__FUNCTION__);
1553                         for(j = 0;j< tmp_winseq_size;j++){
1554                                 sensor_init_winseq_board[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].reg;
1555                                 sensor_init_winseq_board[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].val;
1556                                 }
1557                         sensor_init_winseq_p = sensor_init_winseq_board;
1558                         }
1559                 else if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq){
1560                         sensor_init_winseq_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq);
1561                         }
1562                 //init width,height,bus,pixelcode
1563                 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width != INVALID_VALUE)
1564                         sensor_init_width = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width;
1565                 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height != INVALID_VALUE)
1566                         sensor_init_height = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height;
1567                 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param != INVALID_VALUE)
1568                         sensor_init_busparam = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param;
1569                 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode != INVALID_VALUE)
1570                         sensor_init_pixelcode = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode;
1571         }
1572     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1573
1574         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1575                 ret = -ENODEV;
1576                 goto sensor_INIT_ERR;
1577         }
1578
1579     /* soft reset */
1580         if (sensor_task_lock(client,1)<0)
1581                 goto sensor_INIT_ERR;
1582
1583     ret = sensor_write(client, 0x0103, 0x01);
1584     if (ret != 0)
1585     {
1586         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1587         ret = -ENODEV;
1588                 goto sensor_INIT_ERR;
1589     }
1590     mdelay(5);  //delay 5 microseconds
1591
1592         /* check if it is an sensor sensor */
1593     ret = sensor_read(client, 0x300a, &value);
1594     if (ret != 0) {
1595         SENSOR_TR("read chip id high byte failed\n");
1596         ret = -ENODEV;
1597         goto sensor_INIT_ERR;
1598     }
1599
1600     pid |= (value << 8);
1601
1602     ret = sensor_read(client, 0x300b, &value);
1603     if (ret != 0) {
1604         SENSOR_TR("read chip id low byte failed\n");
1605         ret = -ENODEV;
1606         goto sensor_INIT_ERR;
1607     }
1608
1609     pid |= (value & 0xff);
1610     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1611     if (pid == SENSOR_ID) {
1612         sensor->model = SENSOR_V4L2_IDENT;
1613     } else {
1614         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1615         ret = -ENODEV;
1616         goto sensor_INIT_ERR;
1617     }
1618
1619     ret = sensor_write_array(client, sensor_init_data_p);
1620     if (ret != 0)
1621     {
1622         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1623         goto sensor_INIT_ERR;
1624     }
1625         sensor_task_lock(client,0);
1626     
1627     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
1628     fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1629     if (!fmt) {
1630         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1631         ret = -EINVAL;
1632         goto sensor_INIT_ERR;
1633     }
1634         sensor->info_priv.fmt = *fmt;
1635
1636     /* sensor sensor information for initialization  */
1637         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1638         if (qctrl)
1639         sensor->info_priv.whiteBalance = qctrl->default_value;
1640         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1641         if (qctrl)
1642         sensor->info_priv.brightness = qctrl->default_value;
1643         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1644         if (qctrl)
1645         sensor->info_priv.effect = qctrl->default_value;
1646         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1647         if (qctrl)
1648         sensor->info_priv.exposure = qctrl->default_value;
1649
1650         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1651         if (qctrl)
1652         sensor->info_priv.saturation = qctrl->default_value;
1653         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1654         if (qctrl)
1655         sensor->info_priv.contrast = qctrl->default_value;
1656         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1657         if (qctrl)
1658         sensor->info_priv.mirror = qctrl->default_value;
1659         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1660         if (qctrl)
1661         sensor->info_priv.flip = qctrl->default_value;
1662         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1663         if (qctrl)
1664         sensor->info_priv.scene = qctrl->default_value;
1665         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1666         if (qctrl)
1667         sensor->info_priv.digitalzoom = qctrl->default_value;
1668
1669     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
1670         #if CONFIG_SENSOR_Focus
1671     sensor_set_focus();
1672     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1673         if (qctrl)
1674         sensor->info_priv.focus = qctrl->default_value;
1675         #endif
1676
1677         #if CONFIG_SENSOR_Flash 
1678         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1679         if (qctrl)
1680         sensor->info_priv.flash = qctrl->default_value;
1681     #endif
1682
1683     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);
1684     sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
1685     return 0;
1686 sensor_INIT_ERR:
1687     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1688         sensor_task_lock(client,0);
1689         sensor_deactivate(client);
1690     return ret;
1691 }
1692
1693 static int sensor_deactivate(struct i2c_client *client)
1694 {
1695         struct soc_camera_device *icd = client->dev.platform_data;
1696         u8 reg_val;
1697     struct sensor *sensor = to_sensor(client);
1698         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1699     
1700         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1701     if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
1702         sensor_task_lock(client, 1);
1703         sensor_read(client,0x3000,&reg_val);
1704         sensor_write(client, 0x3000, reg_val&0xfc);
1705         sensor_write(client, 0x3001, 0x00);
1706         sensor_read(client,0x3002,&reg_val);
1707         sensor_write(client, 0x3002, reg_val&0x1f);
1708         sensor_task_lock(client, 0);        
1709     }
1710     sensor_ioctrl(icd, Sensor_PowerDown, 1); 
1711     msleep(100); 
1712
1713         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1714         icd->user_width = SENSOR_INIT_WIDTH;
1715     icd->user_height = SENSOR_INIT_HEIGHT;
1716     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1717         
1718         return 0;
1719 }
1720
1721 static  struct reginfo sensor_power_down_sequence[]=
1722 {
1723     {0x00,0x00}
1724 };
1725 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1726 {
1727     int ret;
1728     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1729
1730     if (pm_msg.event == PM_EVENT_SUSPEND) {
1731         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1732         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1733         if (ret != 0) {
1734             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1735             return ret;
1736         } else {
1737             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1738             if (ret < 0) {
1739                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1740                 return -EINVAL;
1741             }
1742         }
1743     } else {
1744         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1745         return -EINVAL;
1746     }
1747     return 0;
1748 }
1749
1750 static int sensor_resume(struct soc_camera_device *icd)
1751 {
1752         int ret;
1753
1754     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1755     if (ret < 0) {
1756                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1757         return -EINVAL;
1758     }
1759
1760         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1761
1762     return 0;
1763
1764 }
1765
1766 static int sensor_set_bus_param(struct soc_camera_device *icd,
1767                                 unsigned long flags)
1768 {
1769
1770     return 0;
1771 }
1772
1773 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1774 {
1775     struct soc_camera_link *icl = to_soc_camera_link(icd);
1776     unsigned long flags = SENSOR_BUS_PARAM;
1777
1778     return soc_camera_apply_sensor_flags(icl, flags);
1779 }
1780
1781 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1782 {
1783     struct i2c_client *client = v4l2_get_subdevdata(sd);
1784     struct soc_camera_device *icd = client->dev.platform_data;
1785     struct sensor *sensor = to_sensor(client);
1786
1787     mf->width   = icd->user_width;
1788         mf->height      = icd->user_height;
1789         mf->code        = sensor->info_priv.fmt.code;
1790         mf->colorspace  = sensor->info_priv.fmt.colorspace;
1791         mf->field       = V4L2_FIELD_NONE;
1792
1793     return 0;
1794 }
1795 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1796 {
1797     bool ret = false;
1798
1799         if ((mf->width == 1024) && (mf->height == 768)) {
1800                 ret = true;
1801         } else if ((mf->width == 1280) && (mf->height == 1024)) {
1802                 ret = true;
1803         } else if ((mf->width == 1600) && (mf->height == 1200)) {
1804                 ret = true;
1805         } else if ((mf->width == 2048) && (mf->height == 1536)) {
1806                 ret = true;
1807         } else if ((mf->width == 2592) && (mf->height == 1944)) {
1808                 ret = true;
1809         }
1810
1811         if (ret == true)
1812                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1813         return ret;
1814 }
1815
1816 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1817 {
1818     bool ret = false;
1819
1820         if ((mf->width == 1280) && (mf->height == 720)) {
1821                 ret = true;
1822         } else if ((mf->width == 1920) && (mf->height == 1080)) {
1823                 ret = true;
1824         }
1825
1826         if (ret == true)
1827                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1828         return ret;
1829 }
1830 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1831 {
1832     struct i2c_client *client = v4l2_get_subdevdata(sd);
1833     const struct sensor_datafmt *fmt;
1834     struct sensor *sensor = to_sensor(client);
1835         const struct v4l2_queryctrl *qctrl;
1836         struct soc_camera_device *icd = client->dev.platform_data;
1837     struct reginfo *winseqe_set_addr=NULL;
1838     int ret=0, set_w,set_h;
1839
1840         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1841                                    ARRAY_SIZE(sensor_colour_fmts));
1842         if (!fmt) {
1843         ret = -EINVAL;
1844         goto sensor_s_fmt_end;
1845     }
1846
1847         if (sensor->info_priv.fmt.code != mf->code) {
1848                 switch (mf->code)
1849                 {
1850                         case V4L2_MBUS_FMT_YUYV8_2X8:
1851                         {
1852                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
1853                                 break;
1854                         }
1855                         case V4L2_MBUS_FMT_UYVY8_2X8:
1856                         {
1857                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
1858                                 break;
1859                         }
1860                         default:
1861                                 break;
1862                 }
1863                 if (winseqe_set_addr != NULL) {
1864             sensor_write_array(client, winseqe_set_addr);
1865                         sensor->info_priv.fmt.code = mf->code;
1866             sensor->info_priv.fmt.colorspace= mf->colorspace;            
1867                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1868                 } else {
1869                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1870                 }
1871         }
1872
1873     set_w = mf->width;
1874     set_h = mf->height;
1875
1876         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1877         {
1878                 winseqe_set_addr = sensor_qcif;
1879         set_w = 176;
1880         set_h = 144;
1881         }
1882         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1883     {
1884         winseqe_set_addr = sensor_qvga;
1885         set_w = 320;
1886         set_h = 240;
1887     }
1888     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1889     {
1890         winseqe_set_addr = sensor_cif;
1891         set_w = 352;
1892         set_h = 288;
1893     }
1894     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1895     {
1896         winseqe_set_addr = sensor_vga;
1897         set_w = 640;
1898         set_h = 480;
1899     }
1900     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1901     {
1902         winseqe_set_addr = sensor_svga;
1903         set_w = 800;
1904         set_h = 600;
1905     }
1906     else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
1907     {
1908         winseqe_set_addr = sensor_720p;
1909         set_w = 1280;
1910         set_h = 720;
1911     }
1912         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
1913     {
1914         winseqe_set_addr = sensor_xga;
1915         set_w = 1024;
1916         set_h = 768;
1917     }
1918     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1919     {
1920         winseqe_set_addr = sensor_sxga;
1921         set_w = 1280;
1922         set_h = 1024;
1923     }
1924     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1925     {
1926         winseqe_set_addr = sensor_uxga;
1927         set_w = 1600;
1928         set_h = 1200;
1929     }
1930     else
1931     {
1932         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
1933         set_w = SENSOR_INIT_WIDTH;
1934         set_h = SENSOR_INIT_HEIGHT;
1935                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1936     }
1937
1938     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
1939         #if CONFIG_SENSOR_Flash
1940         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
1941             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1942                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1943                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1944             }           
1945         } else {                                        /* ddl@rock-chips.com : Video */
1946             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1947                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1948                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1949             }
1950         }
1951         #endif
1952         ret |= sensor_write_array(client, winseqe_set_addr);
1953         if (ret != 0) {
1954             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1955             #if CONFIG_SENSOR_Flash
1956             if (sensor_fmt_capturechk(sd,mf) == true) {
1957                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1958                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1959                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1960                 }
1961             }
1962             #endif
1963             goto sensor_s_fmt_end;
1964         }
1965
1966         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
1967
1968                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
1969                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1970                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1971                         if (sensor->info_priv.whiteBalance != 0) {
1972                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1973                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1974                         }
1975                         sensor->info_priv.snap2preview = true;
1976                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
1977                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1978                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1979                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1980                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1981                         sensor->info_priv.video2preview = true;
1982                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1983                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1984                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1985                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1986                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1987             msleep(600);
1988                         sensor->info_priv.video2preview = false;
1989                         sensor->info_priv.snap2preview = false;
1990                 }
1991
1992         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1993     }
1994     else
1995     {
1996         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1997     }
1998
1999         mf->width = set_w;
2000     mf->height = set_h;
2001
2002 sensor_s_fmt_end:
2003     return ret;
2004 }
2005
2006 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2007 {
2008     struct i2c_client *client = v4l2_get_subdevdata(sd);
2009     struct sensor *sensor = to_sensor(client);
2010     const struct sensor_datafmt *fmt;
2011     int ret = 0;
2012    
2013         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2014                                    ARRAY_SIZE(sensor_colour_fmts));
2015         if (fmt == NULL) {
2016                 fmt = &sensor->info_priv.fmt;
2017         mf->code = fmt->code;
2018         } 
2019
2020     if (mf->height > SENSOR_MAX_HEIGHT)
2021         mf->height = SENSOR_MAX_HEIGHT;
2022     else if (mf->height < SENSOR_MIN_HEIGHT)
2023         mf->height = SENSOR_MIN_HEIGHT;
2024
2025     if (mf->width > SENSOR_MAX_WIDTH)
2026         mf->width = SENSOR_MAX_WIDTH;
2027     else if (mf->width < SENSOR_MIN_WIDTH)
2028         mf->width = SENSOR_MIN_WIDTH;
2029
2030     mf->colorspace = fmt->colorspace;
2031     
2032     return ret;
2033 }
2034
2035  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2036 {
2037     struct i2c_client *client = v4l2_get_subdevdata(sd);
2038
2039     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2040         return -EINVAL;
2041
2042     if (id->match.addr != client->addr)
2043         return -ENODEV;
2044
2045     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2046     id->revision = 0;
2047
2048     return 0;
2049 }
2050 #if CONFIG_SENSOR_Brightness
2051 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2052 {
2053     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2054
2055     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2056     {
2057         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2058         {
2059             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2060             {
2061                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2062                 return -EINVAL;
2063             }
2064             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2065             return 0;
2066         }
2067     }
2068         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2069     return -EINVAL;
2070 }
2071 #endif
2072 #if CONFIG_SENSOR_Effect
2073 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2074 {
2075     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2076
2077     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2078     {
2079         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2080         {
2081             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2082             {
2083                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2084                 return -EINVAL;
2085             }
2086             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2087             return 0;
2088         }
2089     }
2090         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2091     return -EINVAL;
2092 }
2093 #endif
2094 #if CONFIG_SENSOR_Exposure
2095 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2096 {
2097     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2098
2099     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2100     {
2101         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2102         {
2103             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2104             {
2105                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2106                 return -EINVAL;
2107             }
2108             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2109             return 0;
2110         }
2111     }
2112         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2113     return -EINVAL;
2114 }
2115 #endif
2116 #if CONFIG_SENSOR_Saturation
2117 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2118 {
2119     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2120
2121     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2122     {
2123         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2124         {
2125             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2126             {
2127                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2128                 return -EINVAL;
2129             }
2130             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2131             return 0;
2132         }
2133     }
2134     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2135     return -EINVAL;
2136 }
2137 #endif
2138 #if CONFIG_SENSOR_Contrast
2139 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2140 {
2141     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2142
2143     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2144     {
2145         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2146         {
2147             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2148             {
2149                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2150                 return -EINVAL;
2151             }
2152             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2153             return 0;
2154         }
2155     }
2156     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2157     return -EINVAL;
2158 }
2159 #endif
2160 #if CONFIG_SENSOR_Mirror
2161 static int sensor_mirror(struct i2c_client *client, int on)
2162 {
2163     char val;
2164     int err = 0;    
2165
2166     if (on) {
2167         err = sensor_read(client, 0x3821, &val);
2168         if (err == 0) {
2169             val |= 0x06;
2170             err = sensor_write(client, 0x3821, val);
2171         }
2172     } else {
2173         err = sensor_read(client, 0x3821, &val);
2174         if (err == 0) {
2175             val &= 0xf9;
2176             err = sensor_write(client, 0x3821, val);
2177         }
2178     }
2179
2180     return err;    
2181 }
2182 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2183 {
2184     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2185
2186     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2187     {
2188         if (sensor_mirror(client,value) != 0)
2189             SENSOR_TR("%s(%d): sensor_mirror failed, value:0x%x",__FUNCTION__, __LINE__,value);
2190         
2191         SENSOR_DG("%s(%d): sensor_mirror success, value:0x%x",__FUNCTION__, __LINE__,value);
2192         return 0;
2193     }
2194     SENSOR_TR("\n %s..%s value = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2195     return -EINVAL;
2196 }
2197 #endif
2198 #if CONFIG_SENSOR_Flip
2199 static int sensor_flip(struct i2c_client *client, int on)
2200 {
2201     char val;
2202     int err = 0;    
2203
2204     if (on) {
2205         err = sensor_read(client, 0x3820, &val);
2206         if (err == 0) {
2207             val |= 0x06;
2208             err = sensor_write(client, 0x3820, val);
2209         }
2210     } else {
2211         err = sensor_read(client, 0x3820, &val);
2212         if (err == 0) {
2213             val &= 0xf9;
2214             err = sensor_write(client, 0x3820, val);
2215         }
2216     }
2217
2218     return err;    
2219 }
2220 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2221 {
2222     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2223
2224     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2225     {
2226         if (sensor_flip(client,value) != 0)
2227             SENSOR_TR("%s(%d): sensor_flip failed, value:0x%x",__FUNCTION__, __LINE__,value);
2228         
2229         SENSOR_DG("%s(%d): sensor_flip success, value:0x%x",__FUNCTION__, __LINE__,value);
2230         return 0;
2231     }
2232     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2233     return -EINVAL;
2234 }
2235 #endif
2236 #if CONFIG_SENSOR_Scene
2237 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2238 {
2239     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2240
2241     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2242     {
2243         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2244         {
2245             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2246             {
2247                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2248                 return -EINVAL;
2249             }
2250             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2251             return 0;
2252         }
2253     }
2254     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2255     return -EINVAL;
2256 }
2257 #endif
2258 #if CONFIG_SENSOR_WhiteBalance
2259 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2260 {
2261     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2262
2263     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2264     {
2265         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2266         {
2267             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2268             {
2269                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2270                 return -EINVAL;
2271             }
2272             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2273             return 0;
2274         }
2275     }
2276         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2277     return -EINVAL;
2278 }
2279 #endif
2280 #if CONFIG_SENSOR_DigitalZoom
2281 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2282 {
2283     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2284     struct sensor *sensor = to_sensor(client);
2285         const struct v4l2_queryctrl *qctrl_info;
2286     int digitalzoom_cur, digitalzoom_total;
2287
2288         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2289         if (qctrl_info)
2290                 return -EINVAL;
2291
2292     digitalzoom_cur = sensor->info_priv.digitalzoom;
2293     digitalzoom_total = qctrl_info->maximum;
2294
2295     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
2296     {
2297         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2298         return -EINVAL;
2299     }
2300
2301     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2302     {
2303         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2304         return -EINVAL;
2305     }
2306
2307     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
2308     {
2309         value = digitalzoom_total - digitalzoom_cur;
2310     }
2311
2312     if ((value < 0) && ((digitalzoom_cur + value) < 0))
2313     {
2314         value = 0 - digitalzoom_cur;
2315     }
2316
2317     digitalzoom_cur += value;
2318
2319     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2320     {
2321         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2322         {
2323             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2324             return -EINVAL;
2325         }
2326         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2327         return 0;
2328     }
2329
2330     return -EINVAL;
2331 }
2332 #endif
2333 #if CONFIG_SENSOR_Flash
2334 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2335 {    
2336     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2337         if (value == 3) {       /* ddl@rock-chips.com: torch */
2338             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2339         } else {
2340             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2341         }
2342         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2343         return 0;
2344     }
2345     
2346         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2347     return -EINVAL;
2348 }
2349 #endif
2350
2351 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2352 {
2353     struct i2c_client *client = v4l2_get_subdevdata(sd);
2354     struct sensor *sensor = to_sensor(client);
2355     const struct v4l2_queryctrl *qctrl;
2356
2357     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2358
2359     if (!qctrl)
2360     {
2361         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2362         return -EINVAL;
2363     }
2364
2365     switch (ctrl->id)
2366     {
2367         case V4L2_CID_BRIGHTNESS:
2368             {
2369                 ctrl->value = sensor->info_priv.brightness;
2370                 break;
2371             }
2372         case V4L2_CID_SATURATION:
2373             {
2374                 ctrl->value = sensor->info_priv.saturation;
2375                 break;
2376             }
2377         case V4L2_CID_CONTRAST:
2378             {
2379                 ctrl->value = sensor->info_priv.contrast;
2380                 break;
2381             }
2382         case V4L2_CID_DO_WHITE_BALANCE:
2383             {
2384                 ctrl->value = sensor->info_priv.whiteBalance;
2385                 break;
2386             }
2387         case V4L2_CID_EXPOSURE:
2388             {
2389                 ctrl->value = sensor->info_priv.exposure;
2390                 break;
2391             }
2392         case V4L2_CID_HFLIP:
2393             {
2394                 ctrl->value = sensor->info_priv.mirror;
2395                 break;
2396             }
2397         case V4L2_CID_VFLIP:
2398             {
2399                 ctrl->value = sensor->info_priv.flip;
2400                 break;
2401             }
2402         default :
2403                 break;
2404     }
2405     return 0;
2406 }
2407
2408
2409
2410 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2411 {
2412     struct i2c_client *client = v4l2_get_subdevdata(sd);
2413     struct sensor *sensor = to_sensor(client);
2414     struct soc_camera_device *icd = client->dev.platform_data;
2415     const struct v4l2_queryctrl *qctrl;
2416
2417
2418     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2419
2420     if (!qctrl)
2421     {
2422         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2423         return -EINVAL;
2424     }
2425
2426     switch (ctrl->id)
2427     {
2428 #if CONFIG_SENSOR_Brightness
2429         case V4L2_CID_BRIGHTNESS:
2430             {
2431                 if (ctrl->value != sensor->info_priv.brightness)
2432                 {
2433                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2434                     {
2435                         return -EINVAL;
2436                     }
2437                     sensor->info_priv.brightness = ctrl->value;
2438                 }
2439                 break;
2440             }
2441 #endif
2442 #if CONFIG_SENSOR_Exposure
2443         case V4L2_CID_EXPOSURE:
2444             {
2445                 if (ctrl->value != sensor->info_priv.exposure)
2446                 {
2447                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2448                     {
2449                         return -EINVAL;
2450                     }
2451                     sensor->info_priv.exposure = ctrl->value;
2452                 }
2453                 break;
2454             }
2455 #endif
2456 #if CONFIG_SENSOR_Saturation
2457         case V4L2_CID_SATURATION:
2458             {
2459                 if (ctrl->value != sensor->info_priv.saturation)
2460                 {
2461                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2462                     {
2463                         return -EINVAL;
2464                     }
2465                     sensor->info_priv.saturation = ctrl->value;
2466                 }
2467                 break;
2468             }
2469 #endif
2470 #if CONFIG_SENSOR_Contrast
2471         case V4L2_CID_CONTRAST:
2472             {
2473                 if (ctrl->value != sensor->info_priv.contrast)
2474                 {
2475                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2476                     {
2477                         return -EINVAL;
2478                     }
2479                     sensor->info_priv.contrast = ctrl->value;
2480                 }
2481                 break;
2482             }
2483 #endif
2484 #if CONFIG_SENSOR_WhiteBalance
2485         case V4L2_CID_DO_WHITE_BALANCE:
2486             {
2487                 if (ctrl->value != sensor->info_priv.whiteBalance)
2488                 {
2489                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2490                     {
2491                         return -EINVAL;
2492                     }
2493                     sensor->info_priv.whiteBalance = ctrl->value;
2494                 }
2495                 break;
2496             }
2497 #endif
2498 #if CONFIG_SENSOR_Mirror
2499         case V4L2_CID_HFLIP:
2500             {
2501                 if (ctrl->value != sensor->info_priv.mirror)
2502                 {
2503                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2504                         return -EINVAL;
2505                     sensor->info_priv.mirror = ctrl->value;
2506                 }
2507                 break;
2508             }
2509 #endif
2510 #if CONFIG_SENSOR_Flip
2511         case V4L2_CID_VFLIP:
2512             {
2513                 if (ctrl->value != sensor->info_priv.flip)
2514                 {
2515                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2516                         return -EINVAL;
2517                     sensor->info_priv.flip = ctrl->value;
2518                 }
2519                 break;
2520             }
2521 #endif
2522         default:
2523             break;
2524     }
2525
2526     return 0;
2527 }
2528 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2529 {
2530     const struct v4l2_queryctrl *qctrl;
2531     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2532     struct sensor *sensor = to_sensor(client);
2533
2534     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2535
2536     if (!qctrl)
2537     {
2538         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2539         return -EINVAL;
2540     }
2541
2542     switch (ext_ctrl->id)
2543     {
2544         case V4L2_CID_SCENE:
2545             {
2546                 ext_ctrl->value = sensor->info_priv.scene;
2547                 break;
2548             }
2549         case V4L2_CID_EFFECT:
2550             {
2551                 ext_ctrl->value = sensor->info_priv.effect;
2552                 break;
2553             }
2554         case V4L2_CID_ZOOM_ABSOLUTE:
2555             {
2556                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2557                 break;
2558             }
2559         case V4L2_CID_ZOOM_RELATIVE:
2560             {
2561                 return -EINVAL;
2562             }
2563         case V4L2_CID_FOCUS_ABSOLUTE:
2564             {
2565                 ext_ctrl->value = sensor->info_priv.focus;
2566                 break;
2567             }
2568         case V4L2_CID_FOCUS_RELATIVE:
2569             {
2570                 return -EINVAL;
2571             }
2572         case V4L2_CID_FLASH:
2573             {
2574                 ext_ctrl->value = sensor->info_priv.flash;
2575                 break;
2576             }
2577         default :
2578             break;
2579     }
2580     return 0;
2581 }
2582 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2583 {
2584     const struct v4l2_queryctrl *qctrl;
2585     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2586     struct sensor *sensor = to_sensor(client);
2587     int val_offset;
2588
2589     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2590     
2591     if (!qctrl)
2592     {
2593         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2594         return -EINVAL;
2595     }
2596
2597         val_offset = 0;
2598     switch (ext_ctrl->id)
2599     {
2600 #if CONFIG_SENSOR_Scene
2601         case V4L2_CID_SCENE:
2602             {
2603                 if (ext_ctrl->value != sensor->info_priv.scene)
2604                 {
2605                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2606                         return -EINVAL;
2607                     sensor->info_priv.scene = ext_ctrl->value;
2608                 }
2609                 break;
2610             }
2611 #endif
2612 #if CONFIG_SENSOR_Effect
2613         case V4L2_CID_EFFECT:
2614             {
2615                 if (ext_ctrl->value != sensor->info_priv.effect)
2616                 {
2617                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2618                         return -EINVAL;
2619                     sensor->info_priv.effect= ext_ctrl->value;
2620                 }
2621                 break;
2622             }
2623 #endif
2624 #if CONFIG_SENSOR_DigitalZoom
2625         case V4L2_CID_ZOOM_ABSOLUTE:
2626             {
2627                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2628                     return -EINVAL;
2629
2630                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2631                 {
2632                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2633
2634                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2635                         return -EINVAL;
2636                     sensor->info_priv.digitalzoom += val_offset;
2637
2638                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2639                 }
2640
2641                 break;
2642             }
2643         case V4L2_CID_ZOOM_RELATIVE:
2644             {
2645                 if (ext_ctrl->value)
2646                 {
2647                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2648                         return -EINVAL;
2649                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2650
2651                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2652                 }
2653                 break;
2654             }
2655 #endif
2656 #if CONFIG_SENSOR_Focus
2657         case V4L2_CID_FOCUS_ABSOLUTE:
2658             {
2659                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2660                     return -EINVAL;
2661
2662                 if (ext_ctrl->value != sensor->info_priv.focus)
2663                 {
2664                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2665
2666                     sensor->info_priv.focus += val_offset;
2667                 }
2668
2669                 break;
2670             }
2671         case V4L2_CID_FOCUS_RELATIVE:
2672             {
2673                 if (ext_ctrl->value)
2674                 {
2675                     sensor->info_priv.focus += ext_ctrl->value;
2676
2677                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2678                 }
2679                 break;
2680             }
2681 #endif
2682 #if CONFIG_SENSOR_Flash
2683         case V4L2_CID_FLASH:
2684             {
2685                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2686                     return -EINVAL;
2687                 sensor->info_priv.flash = ext_ctrl->value;
2688
2689                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2690                 break;
2691             }
2692 #endif
2693         default:
2694             break;
2695     }
2696
2697     return 0;
2698 }
2699
2700 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2701 {
2702     struct i2c_client *client = v4l2_get_subdevdata(sd);
2703     struct soc_camera_device *icd = client->dev.platform_data;
2704     int i, error_cnt=0, error_idx=-1;
2705
2706
2707     for (i=0; i<ext_ctrl->count; i++) {
2708         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2709             error_cnt++;
2710             error_idx = i;
2711         }
2712     }
2713
2714     if (error_cnt > 1)
2715         error_idx = ext_ctrl->count;
2716
2717     if (error_idx != -1) {
2718         ext_ctrl->error_idx = error_idx;
2719         return -EINVAL;
2720     } else {
2721         return 0;
2722     }
2723 }
2724
2725 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2726 {
2727     struct i2c_client *client = v4l2_get_subdevdata(sd);
2728     struct soc_camera_device *icd = client->dev.platform_data;
2729     int i, error_cnt=0, error_idx=-1;
2730
2731
2732     for (i=0; i<ext_ctrl->count; i++) {
2733         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2734             error_cnt++;
2735             error_idx = i;
2736         }
2737     }
2738
2739     if (error_cnt > 1)
2740         error_idx = ext_ctrl->count;
2741
2742     if (error_idx != -1) {
2743         ext_ctrl->error_idx = error_idx;
2744         return -EINVAL;
2745     } else {
2746         return 0;
2747     }
2748 }
2749
2750 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2751  * this wasn't our capture interface, so, we wait for the right one */
2752 static int sensor_video_probe(struct soc_camera_device *icd,
2753                                struct i2c_client *client)
2754 {
2755     char value;
2756     int ret,pid = 0,i=0;
2757     struct sensor *sensor = to_sensor(client);
2758
2759     /* We must have a parent by now. And it cannot be a wrong one.
2760      * So this entire test is completely redundant. */
2761     if (!icd->dev.parent ||
2762             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2763                 return -ENODEV;
2764
2765         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2766                 ret = -ENODEV;
2767                 printk("sensor ioctrl powdown erro!!!");
2768                 goto sensor_video_probe_err;
2769         }
2770     /* soft reset */
2771     ret = sensor_write(client, 0x0103, 0x01);
2772     if (ret != 0) {
2773         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2774         ret = -ENODEV;
2775                 goto sensor_video_probe_err;
2776         }
2777     mdelay(5);          //delay 5 microseconds
2778
2779     /* check if it is an sensor sensor */
2780     ret = sensor_read(client, 0x300a, &value);
2781     if (ret != 0) {
2782         SENSOR_TR("read chip id high byte failed\n");
2783         ret = -ENODEV;
2784         goto sensor_video_probe_err;
2785     }
2786
2787     pid |= (value << 8);
2788
2789     ret = sensor_read(client, 0x300b, &value);
2790     if (ret != 0) {
2791         SENSOR_TR("read chip id low byte failed\n");
2792         ret = -ENODEV;
2793         goto sensor_video_probe_err;
2794     }
2795     pid |= (value & 0xff);
2796     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2797     if (pid == SENSOR_ID) {
2798         sensor->model = SENSOR_V4L2_IDENT;
2799     } else {
2800         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2801         ret = -ENODEV;
2802         goto sensor_video_probe_err;
2803     }
2804         
2805     return 0;
2806
2807 sensor_video_probe_err:
2808         printk("the erro value = %d\n",i);
2809
2810     return ret;
2811 }
2812
2813 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2814 {
2815         struct i2c_client *client = v4l2_get_subdevdata(sd);
2816     struct soc_camera_device *icd = client->dev.platform_data;
2817     struct sensor *sensor = to_sensor(client);
2818     int ret = 0;
2819 #if CONFIG_SENSOR_Flash 
2820     int i;
2821 #endif
2822     
2823         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2824         switch (cmd)
2825         {
2826                 case RK_CAM_SUBDEV_DEACTIVATE:
2827                 {
2828                         sensor_deactivate(client);
2829                         break;
2830                 }
2831
2832                 case RK_CAM_SUBDEV_IOREQUEST:
2833                 {
2834                         sensor->sensor_io_request = (struct rkcamera_platform_data*)arg;           
2835             if (sensor->sensor_io_request != NULL) { 
2836                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
2837                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2838                     sensor->sensor_gpio_res = (struct rkcamera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2839                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
2840                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2841                     sensor->sensor_gpio_res = (struct rkcamera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2842                 }
2843             } else {
2844                 SENSOR_TR("%s %s RK_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2845                 ret = -EINVAL;
2846                 goto sensor_ioctl_end;
2847             }
2848             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2849                for this project */
2850             #if CONFIG_SENSOR_Flash     
2851                 if (sensor->sensor_gpio_res) { 
2852                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2853                     for (i = 0; i < icd->ops->num_controls; i++) {
2854                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2855                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2856                                 }
2857                     }
2858                     sensor->info_priv.flash = 0xff;
2859                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2860                 }
2861                 }
2862             #endif
2863                         break;
2864                 }
2865                 default:
2866                 {
2867                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2868                         break;
2869                 }
2870         }
2871 sensor_ioctl_end:
2872         return ret;
2873
2874 }
2875 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2876                             enum v4l2_mbus_pixelcode *code)
2877 {
2878         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2879                 return -EINVAL;
2880
2881         *code = sensor_colour_fmts[index].code;
2882         return 0;
2883 }
2884 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2885         .init           = sensor_init,
2886         .g_ctrl         = sensor_g_control,
2887         .s_ctrl         = sensor_s_control,
2888         .g_ext_ctrls          = sensor_g_ext_controls,
2889         .s_ext_ctrls          = sensor_s_ext_controls,
2890         .g_chip_ident   = sensor_g_chip_ident,
2891         .ioctl = sensor_ioctl,  
2892 };
2893
2894 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2895         .s_mbus_fmt     = sensor_s_fmt,
2896         .g_mbus_fmt     = sensor_g_fmt,
2897         .try_mbus_fmt   = sensor_try_fmt,
2898         .enum_mbus_fmt  = sensor_enum_fmt,
2899 };
2900 static struct v4l2_subdev_ops sensor_subdev_ops = {
2901         .core   = &sensor_subdev_core_ops,
2902         .video = &sensor_subdev_video_ops,
2903 };
2904
2905 static int sensor_probe(struct i2c_client *client,
2906                          const struct i2c_device_id *did)
2907 {
2908     struct sensor *sensor;
2909     struct soc_camera_device *icd = client->dev.platform_data;
2910     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2911     struct soc_camera_link *icl;
2912     int ret;
2913
2914     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2915     if (!icd) {
2916         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2917         return -EINVAL;
2918     }
2919
2920     icl = to_soc_camera_link(icd);
2921     if (!icl) {
2922         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2923         return -EINVAL;
2924     }
2925
2926     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2927         dev_warn(&adapter->dev,
2928                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2929         return -EIO;
2930     }
2931
2932     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2933     if (!sensor)
2934         return -ENOMEM;
2935
2936     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2937
2938     /* Second stage probe - when a capture adapter is there */
2939     icd->ops            = &sensor_ops;
2940
2941     sensor->info_priv.fmt = sensor_colour_fmts[0];
2942     
2943         #if CONFIG_SENSOR_I2C_NOSCHED
2944         atomic_set(&sensor->tasklock_cnt,0);
2945         #endif
2946
2947     ret = sensor_video_probe(icd, client);
2948     if (ret < 0) {
2949         icd->ops = NULL;
2950         i2c_set_clientdata(client, NULL);
2951         kfree(sensor);
2952                 sensor = NULL;
2953     }
2954     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2955     return ret;
2956 }
2957
2958 static int sensor_remove(struct i2c_client *client)
2959 {
2960     struct sensor *sensor = to_sensor(client);
2961     struct soc_camera_device *icd = client->dev.platform_data;
2962
2963     icd->ops = NULL;
2964     i2c_set_clientdata(client, NULL);
2965     client->driver = NULL;
2966     kfree(sensor);
2967         sensor = NULL;
2968         if(sensor_init_winseq_board)
2969                 {
2970                 vfree(sensor_init_winseq_board);
2971                 sensor_init_winseq_board = NULL;
2972                 }
2973     return 0;
2974 }
2975
2976 static const struct i2c_device_id sensor_id[] = {
2977         {SENSOR_NAME_STRING(), 0 },
2978         { }
2979 };
2980 MODULE_DEVICE_TABLE(i2c, sensor_id);
2981
2982 static struct i2c_driver sensor_i2c_driver = {
2983         .driver = {
2984                 .name = SENSOR_NAME_STRING(),
2985         },
2986         .probe          = sensor_probe,
2987         .remove         = sensor_remove,
2988         .id_table       = sensor_id,
2989 };
2990
2991 static int __init sensor_mod_init(void)
2992 {
2993     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2994     return i2c_add_driver(&sensor_i2c_driver);
2995 }
2996
2997 static void __exit sensor_mod_exit(void)
2998 {
2999     i2c_del_driver(&sensor_i2c_driver);
3000 }
3001
3002 device_initcall_sync(sensor_mod_init);
3003 module_exit(sensor_mod_exit);
3004
3005 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3006 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3007 MODULE_LICENSE("GPL");
3008
3009