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