5a45a36de5f6cbafafbfedb3de18ca4f484fea01
[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_PIX_FMT_YUYV
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 = 1,
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 = 1,
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 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1202 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1203 static int sensor_deactivate(struct i2c_client *client);
1204
1205 static struct soc_camera_ops sensor_ops =
1206 {
1207     .suspend                     = sensor_suspend,
1208     .resume                       = sensor_resume,
1209     .set_bus_param              = sensor_set_bus_param,
1210     .query_bus_param    = sensor_query_bus_param,
1211     .controls           = sensor_controls,
1212     .menus                         = sensor_menus,
1213     .num_controls               = ARRAY_SIZE(sensor_controls),
1214     .num_menus          = ARRAY_SIZE(sensor_menus),
1215 };
1216
1217 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1218         { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1219         .colorspace = _colorspace }
1220
1221 #define JPG_FMT(_name, _depth, _fourcc) \
1222         COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1223
1224 static const struct soc_camera_data_format sensor_colour_formats[] = {
1225         JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1226         JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1227 };
1228
1229 typedef struct sensor_info_priv_s
1230 {
1231     int whiteBalance;
1232     int brightness;
1233     int contrast;
1234     int saturation;
1235     int effect;
1236     int scene;
1237     int digitalzoom;
1238     int focus;
1239     int flash;
1240     int exposure;
1241         bool snap2preview;
1242         bool video2preview;
1243     unsigned char mirror;                                        /* HFLIP */
1244     unsigned char flip;                                          /* VFLIP */
1245     unsigned int winseqe_cur_addr;
1246         unsigned int pixfmt;
1247     unsigned int funmodule_state;
1248 } sensor_info_priv_t;
1249
1250 struct sensor
1251 {
1252     struct v4l2_subdev subdev;
1253     struct i2c_client *client;
1254     sensor_info_priv_t info_priv;
1255     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1256 #if CONFIG_SENSOR_I2C_NOSCHED
1257         atomic_t tasklock_cnt;
1258 #endif
1259         struct rk29camera_platform_data *sensor_io_request;
1260     struct rk29camera_gpio_res *sensor_gpio_res;
1261 };
1262
1263
1264 static struct sensor* to_sensor(const struct i2c_client *client)
1265 {
1266     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1267 }
1268
1269 static int sensor_task_lock(struct i2c_client *client, int lock)
1270 {
1271 #if CONFIG_SENSOR_I2C_NOSCHED
1272         int cnt = 3;
1273     struct sensor *sensor = to_sensor(client);
1274
1275         if (lock) {
1276                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1277                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1278                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1279                                 msleep(35);
1280                                 cnt--;
1281                         }
1282                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1283                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1284                                 goto sensor_task_lock_err;
1285                         }
1286                         preempt_disable();
1287                 }
1288
1289                 atomic_add(1, &sensor->tasklock_cnt);
1290         } else {
1291                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1292                         atomic_sub(1, &sensor->tasklock_cnt);
1293
1294                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1295                                 preempt_enable();
1296                 }
1297         }
1298         return 0;
1299 sensor_task_lock_err:
1300         return -1;  
1301 #else
1302     return 0;
1303 #endif
1304
1305 }
1306
1307 /* sensor register write */
1308 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1309 {
1310     int err,cnt;
1311     u8 buf[3];
1312     struct i2c_msg msg[1];
1313
1314     buf[0] = reg >> 8;
1315     buf[1] = reg & 0xFF;
1316     buf[2] = val;
1317
1318     msg->addr = client->addr;
1319     msg->flags = client->flags;
1320     msg->buf = buf;
1321     msg->len = sizeof(buf);
1322     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1323     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1324
1325     cnt = 3;
1326     err = -EAGAIN;
1327
1328     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1329         err = i2c_transfer(client->adapter, msg, 1);
1330
1331         if (err >= 0) {
1332             return 0;
1333         } else {
1334                 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1335                 udelay(10);
1336         }
1337     }
1338
1339     return err;
1340 }
1341
1342 /* sensor register read */
1343 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1344 {
1345     int err,cnt;
1346     u8 buf[2];
1347     struct i2c_msg msg[2];
1348
1349     buf[0] = reg >> 8;
1350     buf[1] = reg & 0xFF;
1351
1352     msg[0].addr = client->addr;
1353     msg[0].flags = client->flags;
1354     msg[0].buf = buf;
1355     msg[0].len = sizeof(buf);
1356     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1357     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1358
1359     msg[1].addr = client->addr;
1360     msg[1].flags = client->flags|I2C_M_RD;
1361     msg[1].buf = buf;
1362     msg[1].len = 1;
1363     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1364     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1365
1366     cnt = 3;
1367     err = -EAGAIN;
1368     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1369         err = i2c_transfer(client->adapter, msg, 2);
1370
1371         if (err >= 0) {
1372             *val = buf[0];
1373             return 0;
1374         } else {
1375                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1376             udelay(10);
1377         }
1378     }
1379
1380     return err;
1381 }
1382
1383 /* write a array of registers  */
1384 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1385 {
1386     int err = 0, cnt;
1387     int i = 0;
1388 #if CONFIG_SENSOR_I2C_RDWRCHK    
1389         char valchk;
1390 #endif
1391
1392         cnt = 0;
1393         if (sensor_task_lock(client, 1) < 0)
1394                 goto sensor_write_array_end;
1395
1396     while (regarray[i].reg != 0)
1397     {
1398         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1399         if (err < 0)
1400         {
1401             if (cnt-- > 0) {
1402                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1403                                 i = 0;
1404                                 continue;
1405             } else {
1406                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1407                 err = -EPERM;
1408                                 goto sensor_write_array_end;
1409             }
1410         } else {
1411         #if CONFIG_SENSOR_I2C_RDWRCHK
1412                         sensor_read(client, regarray[i].reg, &valchk);
1413                         if (valchk != regarray[i].val)
1414                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1415                 #endif
1416         }
1417         i++;
1418     }
1419
1420 sensor_write_array_end:
1421         sensor_task_lock(client,0);
1422         return err;
1423 }
1424 #if CONFIG_SENSOR_I2C_RDWRCHK
1425 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1426 {
1427     int cnt;
1428     int i = 0;
1429         char valchk;
1430
1431         cnt = 0;
1432         valchk = 0;
1433     while (regarray[i].reg != 0)
1434     {
1435                 sensor_read(client, regarray[i].reg, &valchk);
1436                 if (valchk != regarray[i].val)
1437                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1438
1439         i++;
1440     }
1441     return 0;
1442 }
1443 #endif
1444 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1445 {
1446         struct soc_camera_link *icl = to_soc_camera_link(icd);
1447         int ret = 0;
1448
1449     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1450         switch (cmd)
1451         {
1452                 case Sensor_PowerDown:
1453                 {
1454                         if (icl->powerdown) {
1455                                 ret = icl->powerdown(icd->pdev, on);
1456                                 if (ret == RK29_CAM_IO_SUCCESS) {
1457                                         if (on == 0) {
1458                                                 mdelay(2);
1459                                                 if (icl->reset)
1460                                                         icl->reset(icd->pdev);
1461                                         }
1462                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1463                                         ret = -ENODEV;
1464                                         goto sensor_power_end;
1465                                 }
1466                         }
1467                         break;
1468                 }
1469                 case Sensor_Flash:
1470                 {
1471                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1472                 struct sensor *sensor = to_sensor(client);
1473
1474                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1475                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1476                         }
1477             break;
1478                 }
1479                 default:
1480                 {
1481                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1482                         break;
1483                 }
1484         }
1485 sensor_power_end:
1486         return ret;
1487 }
1488 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1489 {
1490     struct i2c_client *client = sd->priv;
1491     struct soc_camera_device *icd = client->dev.platform_data;
1492     struct sensor *sensor = to_sensor(client);
1493         const struct v4l2_queryctrl *qctrl;
1494     char value;
1495     int ret,pid = 0;
1496
1497     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1498
1499         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1500                 ret = -ENODEV;
1501                 goto sensor_INIT_ERR;
1502         }
1503
1504     /* soft reset */
1505         if (sensor_task_lock(client,1)<0)
1506                 goto sensor_INIT_ERR;
1507
1508     ret = sensor_write(client, 0x0103, 0x01);
1509     if (ret != 0)
1510     {
1511         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1512         ret = -ENODEV;
1513                 goto sensor_INIT_ERR;
1514     }
1515     mdelay(5);  //delay 5 microseconds
1516
1517         /* check if it is an sensor sensor */
1518     ret = sensor_read(client, 0x300a, &value);
1519     if (ret != 0) {
1520         SENSOR_TR("read chip id high byte failed\n");
1521         ret = -ENODEV;
1522         goto sensor_INIT_ERR;
1523     }
1524
1525     pid |= (value << 8);
1526
1527     ret = sensor_read(client, 0x300b, &value);
1528     if (ret != 0) {
1529         SENSOR_TR("read chip id low byte failed\n");
1530         ret = -ENODEV;
1531         goto sensor_INIT_ERR;
1532     }
1533
1534     pid |= (value & 0xff);
1535     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1536     if (pid == SENSOR_ID) {
1537         sensor->model = SENSOR_V4L2_IDENT;
1538     } else {
1539         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1540         ret = -ENODEV;
1541         goto sensor_INIT_ERR;
1542     }
1543
1544     ret = sensor_write_array(client, sensor_init_data);
1545     if (ret != 0)
1546     {
1547         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1548         goto sensor_INIT_ERR;
1549     }
1550         sensor_task_lock(client,0);
1551     //icd->user_width = SENSOR_INIT_WIDTH;
1552     //icd->user_height = SENSOR_INIT_HEIGHT;
1553     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
1554         sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
1555
1556     /* sensor sensor information for initialization  */
1557         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1558         if (qctrl)
1559         sensor->info_priv.whiteBalance = qctrl->default_value;
1560         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1561         if (qctrl)
1562         sensor->info_priv.brightness = qctrl->default_value;
1563         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1564         if (qctrl)
1565         sensor->info_priv.effect = qctrl->default_value;
1566         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1567         if (qctrl)
1568         sensor->info_priv.exposure = qctrl->default_value;
1569
1570         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1571         if (qctrl)
1572         sensor->info_priv.saturation = qctrl->default_value;
1573         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1574         if (qctrl)
1575         sensor->info_priv.contrast = qctrl->default_value;
1576         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1577         if (qctrl)
1578         sensor->info_priv.mirror = qctrl->default_value;
1579         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1580         if (qctrl)
1581         sensor->info_priv.flip = qctrl->default_value;
1582         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1583         if (qctrl)
1584         sensor->info_priv.scene = qctrl->default_value;
1585         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1586         if (qctrl)
1587         sensor->info_priv.digitalzoom = qctrl->default_value;
1588
1589     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
1590         #if CONFIG_SENSOR_Focus
1591     sensor_set_focus();
1592     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1593         if (qctrl)
1594         sensor->info_priv.focus = qctrl->default_value;
1595         #endif
1596
1597         #if CONFIG_SENSOR_Flash 
1598         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1599         if (qctrl)
1600         sensor->info_priv.flash = qctrl->default_value;
1601     #endif
1602
1603     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);
1604     sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
1605     return 0;
1606 sensor_INIT_ERR:
1607     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1608         sensor_task_lock(client,0);
1609         sensor_deactivate(client);
1610     return ret;
1611 }
1612
1613 static int sensor_deactivate(struct i2c_client *client)
1614 {
1615         struct soc_camera_device *icd = client->dev.platform_data;
1616         u8 reg_val;
1617     struct sensor *sensor = to_sensor(client);
1618         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1619     
1620         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1621     if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
1622         sensor_task_lock(client, 1);
1623         sensor_read(client,0x3000,&reg_val);
1624         sensor_write(client, 0x3000, reg_val&0xfc);
1625         sensor_write(client, 0x3001, 0x00);
1626         sensor_read(client,0x3002,&reg_val);
1627         sensor_write(client, 0x3002, reg_val&0x1f);
1628         sensor_task_lock(client, 0);        
1629     }
1630     sensor_ioctrl(icd, Sensor_PowerDown, 1); 
1631     msleep(100); 
1632
1633         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1634         icd->user_width = SENSOR_INIT_WIDTH;
1635     icd->user_height = SENSOR_INIT_HEIGHT;
1636     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1637         
1638         return 0;
1639 }
1640
1641 static  struct reginfo sensor_power_down_sequence[]=
1642 {
1643     {0x00,0x00}
1644 };
1645 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1646 {
1647     int ret;
1648     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1649
1650     if (pm_msg.event == PM_EVENT_SUSPEND) {
1651         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1652         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1653         if (ret != 0) {
1654             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1655             return ret;
1656         } else {
1657             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1658             if (ret < 0) {
1659                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1660                 return -EINVAL;
1661             }
1662         }
1663     } else {
1664         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1665         return -EINVAL;
1666     }
1667     return 0;
1668 }
1669
1670 static int sensor_resume(struct soc_camera_device *icd)
1671 {
1672         int ret;
1673
1674     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1675     if (ret < 0) {
1676                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1677         return -EINVAL;
1678     }
1679
1680         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1681
1682     return 0;
1683
1684 }
1685
1686 static int sensor_set_bus_param(struct soc_camera_device *icd,
1687                                 unsigned long flags)
1688 {
1689
1690     return 0;
1691 }
1692
1693 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1694 {
1695     struct soc_camera_link *icl = to_soc_camera_link(icd);
1696     unsigned long flags = SENSOR_BUS_PARAM;
1697
1698     return soc_camera_apply_sensor_flags(icl, flags);
1699 }
1700
1701 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1702 {
1703     struct i2c_client *client = sd->priv;
1704     struct soc_camera_device *icd = client->dev.platform_data;
1705     struct sensor *sensor = to_sensor(client);
1706     struct v4l2_pix_format *pix = &f->fmt.pix;
1707
1708     pix->width          = icd->user_width;
1709     pix->height         = icd->user_height;
1710     pix->pixelformat    = sensor->info_priv.pixfmt;
1711     pix->field          = V4L2_FIELD_NONE;
1712     pix->colorspace             = V4L2_COLORSPACE_JPEG;
1713
1714     return 0;
1715 }
1716 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
1717 {
1718     bool ret = false;
1719
1720         if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
1721                 ret = true;
1722         } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
1723                 ret = true;
1724         } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
1725                 ret = true;
1726         } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
1727                 ret = true;
1728         } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
1729                 ret = true;
1730         }
1731
1732         if (ret == true)
1733                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1734         return ret;
1735 }
1736
1737 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
1738 {
1739     bool ret = false;
1740
1741         if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
1742                 ret = true;
1743         } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
1744                 ret = true;
1745         }
1746
1747         if (ret == true)
1748                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1749         return ret;
1750 }
1751 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1752 {
1753     struct i2c_client *client = sd->priv;
1754     struct sensor *sensor = to_sensor(client);
1755     struct v4l2_pix_format *pix = &f->fmt.pix;
1756         const struct v4l2_queryctrl *qctrl;
1757         struct soc_camera_device *icd = client->dev.platform_data;
1758     struct reginfo *winseqe_set_addr=NULL;
1759     int ret=0, set_w,set_h;
1760
1761         if (sensor->info_priv.pixfmt != pix->pixelformat) {
1762                 switch (pix->pixelformat)
1763                 {
1764                         case V4L2_PIX_FMT_YUYV:
1765                         {
1766                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
1767                                 break;
1768                         }
1769                         case V4L2_PIX_FMT_UYVY:
1770                         {
1771                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
1772                                 break;
1773                         }
1774                         default:
1775                                 break;
1776                 }
1777                 if (winseqe_set_addr != NULL) {
1778             sensor_write_array(client, winseqe_set_addr);
1779                         sensor->info_priv.pixfmt = pix->pixelformat;
1780
1781                         SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1782                 } else {
1783                         SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1784                 }
1785         }
1786
1787     set_w = pix->width;
1788     set_h = pix->height;
1789
1790         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1791         {
1792                 winseqe_set_addr = sensor_qcif;
1793         set_w = 176;
1794         set_h = 144;
1795         }
1796         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1797     {
1798         winseqe_set_addr = sensor_qvga;
1799         set_w = 320;
1800         set_h = 240;
1801     }
1802     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1803     {
1804         winseqe_set_addr = sensor_cif;
1805         set_w = 352;
1806         set_h = 288;
1807     }
1808     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1809     {
1810         winseqe_set_addr = sensor_vga;
1811         set_w = 640;
1812         set_h = 480;
1813     }
1814     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1815     {
1816         winseqe_set_addr = sensor_svga;
1817         set_w = 800;
1818         set_h = 600;
1819     }
1820     else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
1821     {
1822         winseqe_set_addr = sensor_720p;
1823         set_w = 1280;
1824         set_h = 720;
1825     }
1826         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
1827     {
1828         winseqe_set_addr = sensor_xga;
1829         set_w = 1024;
1830         set_h = 768;
1831     }
1832     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1833     {
1834         winseqe_set_addr = sensor_sxga;
1835         set_w = 1280;
1836         set_h = 1024;
1837     }
1838     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1839     {
1840         winseqe_set_addr = sensor_uxga;
1841         set_w = 1600;
1842         set_h = 1200;
1843     }
1844     else
1845     {
1846         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
1847         set_w = SENSOR_INIT_WIDTH;
1848         set_h = SENSOR_INIT_HEIGHT;
1849                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
1850     }
1851
1852     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
1853         #if CONFIG_SENSOR_Flash
1854         if (sensor_fmt_capturechk(sd,f) == true) {      /* ddl@rock-chips.com : Capture */
1855             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1856                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1857                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1858             }           
1859         } else {                                        /* ddl@rock-chips.com : Video */
1860             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1861                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1862                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1863             }
1864         }
1865         #endif
1866         ret |= sensor_write_array(client, winseqe_set_addr);
1867         if (ret != 0) {
1868             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1869             #if CONFIG_SENSOR_Flash
1870             if (sensor_fmt_capturechk(sd,f) == true) {
1871                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1872                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1873                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1874                 }
1875             }
1876             #endif
1877             goto sensor_s_fmt_end;
1878         }
1879
1880         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
1881
1882                 if (sensor_fmt_capturechk(sd,f) == true) {                                  /* ddl@rock-chips.com : Capture */
1883                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1884                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1885                         if (sensor->info_priv.whiteBalance != 0) {
1886                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1887                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1888                         }
1889                         sensor->info_priv.snap2preview = true;
1890                 } else if (sensor_fmt_videochk(sd,f) == true) {                 /* ddl@rock-chips.com : Video */
1891                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1892                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1893                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1894                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1895                         sensor->info_priv.video2preview = true;
1896                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1897                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1898                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1899                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1900                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1901             msleep(600);
1902                         sensor->info_priv.video2preview = false;
1903                         sensor->info_priv.snap2preview = false;
1904                 }
1905
1906         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1907     }
1908     else
1909     {
1910         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1911     }
1912
1913         pix->width = set_w;
1914     pix->height = set_h;
1915
1916 sensor_s_fmt_end:
1917     return ret;
1918 }
1919
1920 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1921 {
1922     struct v4l2_pix_format *pix = &f->fmt.pix;
1923     bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
1924         pix->pixelformat == V4L2_PIX_FMT_YUYV;
1925
1926     /*
1927     * With Bayer format enforce even side lengths, but let the user play
1928     * with the starting pixel
1929     */
1930
1931     if (pix->height > SENSOR_MAX_HEIGHT)
1932         pix->height = SENSOR_MAX_HEIGHT;
1933     else if (pix->height < SENSOR_MIN_HEIGHT)
1934         pix->height = SENSOR_MIN_HEIGHT;
1935     else if (bayer)
1936         pix->height = ALIGN(pix->height, 2);
1937
1938     if (pix->width > SENSOR_MAX_WIDTH)
1939         pix->width = SENSOR_MAX_WIDTH;
1940     else if (pix->width < SENSOR_MIN_WIDTH)
1941         pix->width = SENSOR_MIN_WIDTH;
1942     else if (bayer)
1943         pix->width = ALIGN(pix->width, 2);
1944
1945     return 0;
1946 }
1947
1948  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1949 {
1950     struct i2c_client *client = sd->priv;
1951
1952     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1953         return -EINVAL;
1954
1955     if (id->match.addr != client->addr)
1956         return -ENODEV;
1957
1958     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
1959     id->revision = 0;
1960
1961     return 0;
1962 }
1963 #if CONFIG_SENSOR_Brightness
1964 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1965 {
1966     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1967
1968     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1969     {
1970         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1971         {
1972             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1973             {
1974                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1975                 return -EINVAL;
1976             }
1977             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1978             return 0;
1979         }
1980     }
1981         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1982     return -EINVAL;
1983 }
1984 #endif
1985 #if CONFIG_SENSOR_Effect
1986 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1987 {
1988     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1989
1990     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1991     {
1992         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1993         {
1994             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1995             {
1996                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1997                 return -EINVAL;
1998             }
1999             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2000             return 0;
2001         }
2002     }
2003         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2004     return -EINVAL;
2005 }
2006 #endif
2007 #if CONFIG_SENSOR_Exposure
2008 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2009 {
2010     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2011
2012     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2013     {
2014         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2015         {
2016             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2017             {
2018                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2019                 return -EINVAL;
2020             }
2021             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2022             return 0;
2023         }
2024     }
2025         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2026     return -EINVAL;
2027 }
2028 #endif
2029 #if CONFIG_SENSOR_Saturation
2030 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2031 {
2032     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2033
2034     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2035     {
2036         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2037         {
2038             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2039             {
2040                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2041                 return -EINVAL;
2042             }
2043             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2044             return 0;
2045         }
2046     }
2047     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2048     return -EINVAL;
2049 }
2050 #endif
2051 #if CONFIG_SENSOR_Contrast
2052 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2053 {
2054     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2055
2056     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2057     {
2058         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2059         {
2060             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2061             {
2062                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2063                 return -EINVAL;
2064             }
2065             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2066             return 0;
2067         }
2068     }
2069     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2070     return -EINVAL;
2071 }
2072 #endif
2073 #if CONFIG_SENSOR_Mirror
2074 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2075 {
2076     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2077
2078     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2079     {
2080         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2081         {
2082             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2083             {
2084                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2085                 return -EINVAL;
2086             }
2087             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2088             return 0;
2089         }
2090     }
2091     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2092     return -EINVAL;
2093 }
2094 #endif
2095 #if CONFIG_SENSOR_Flip
2096 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2097 {
2098     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2099
2100     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2101     {
2102         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2103         {
2104             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2105             {
2106                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2107                 return -EINVAL;
2108             }
2109             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2110             return 0;
2111         }
2112     }
2113     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2114     return -EINVAL;
2115 }
2116 #endif
2117 #if CONFIG_SENSOR_Scene
2118 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2119 {
2120     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2121
2122     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2123     {
2124         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2125         {
2126             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2127             {
2128                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2129                 return -EINVAL;
2130             }
2131             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2132             return 0;
2133         }
2134     }
2135     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2136     return -EINVAL;
2137 }
2138 #endif
2139 #if CONFIG_SENSOR_WhiteBalance
2140 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2141 {
2142     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2143
2144     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2145     {
2146         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2147         {
2148             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2149             {
2150                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2151                 return -EINVAL;
2152             }
2153             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2154             return 0;
2155         }
2156     }
2157         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2158     return -EINVAL;
2159 }
2160 #endif
2161 #if CONFIG_SENSOR_DigitalZoom
2162 static int sensor_set_digitalzoom(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     struct sensor *sensor = to_sensor(client);
2166         const struct v4l2_queryctrl *qctrl_info;
2167     int digitalzoom_cur, digitalzoom_total;
2168
2169         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2170         if (qctrl_info)
2171                 return -EINVAL;
2172
2173     digitalzoom_cur = sensor->info_priv.digitalzoom;
2174     digitalzoom_total = qctrl_info->maximum;
2175
2176     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2177     {
2178         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2179         return -EINVAL;
2180     }
2181
2182     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2183     {
2184         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2185         return -EINVAL;
2186     }
2187
2188     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2189     {
2190         *value = digitalzoom_total - digitalzoom_cur;
2191     }
2192
2193     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2194     {
2195         *value = 0 - digitalzoom_cur;
2196     }
2197
2198     digitalzoom_cur += *value;
2199
2200     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2201     {
2202         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2203         {
2204             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2205             return -EINVAL;
2206         }
2207         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2208         return 0;
2209     }
2210
2211     return -EINVAL;
2212 }
2213 #endif
2214 #if CONFIG_SENSOR_Flash
2215 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2216 {    
2217     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2218         if (value == 3) {       /* ddl@rock-chips.com: torch */
2219             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2220         } else {
2221             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2222         }
2223         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2224         return 0;
2225     }
2226     
2227         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2228     return -EINVAL;
2229 }
2230 #endif
2231
2232 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2233 {
2234     struct i2c_client *client = sd->priv;
2235     struct sensor *sensor = to_sensor(client);
2236     const struct v4l2_queryctrl *qctrl;
2237
2238     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2239
2240     if (!qctrl)
2241     {
2242         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2243         return -EINVAL;
2244     }
2245
2246     switch (ctrl->id)
2247     {
2248         case V4L2_CID_BRIGHTNESS:
2249             {
2250                 ctrl->value = sensor->info_priv.brightness;
2251                 break;
2252             }
2253         case V4L2_CID_SATURATION:
2254             {
2255                 ctrl->value = sensor->info_priv.saturation;
2256                 break;
2257             }
2258         case V4L2_CID_CONTRAST:
2259             {
2260                 ctrl->value = sensor->info_priv.contrast;
2261                 break;
2262             }
2263         case V4L2_CID_DO_WHITE_BALANCE:
2264             {
2265                 ctrl->value = sensor->info_priv.whiteBalance;
2266                 break;
2267             }
2268         case V4L2_CID_EXPOSURE:
2269             {
2270                 ctrl->value = sensor->info_priv.exposure;
2271                 break;
2272             }
2273         case V4L2_CID_HFLIP:
2274             {
2275                 ctrl->value = sensor->info_priv.mirror;
2276                 break;
2277             }
2278         case V4L2_CID_VFLIP:
2279             {
2280                 ctrl->value = sensor->info_priv.flip;
2281                 break;
2282             }
2283         default :
2284                 break;
2285     }
2286     return 0;
2287 }
2288
2289
2290
2291 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2292 {
2293     struct i2c_client *client = sd->priv;
2294     struct sensor *sensor = to_sensor(client);
2295     struct soc_camera_device *icd = client->dev.platform_data;
2296     const struct v4l2_queryctrl *qctrl;
2297
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 #if CONFIG_SENSOR_Brightness
2310         case V4L2_CID_BRIGHTNESS:
2311             {
2312                 if (ctrl->value != sensor->info_priv.brightness)
2313                 {
2314                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2315                     {
2316                         return -EINVAL;
2317                     }
2318                     sensor->info_priv.brightness = ctrl->value;
2319                 }
2320                 break;
2321             }
2322 #endif
2323 #if CONFIG_SENSOR_Exposure
2324         case V4L2_CID_EXPOSURE:
2325             {
2326                 if (ctrl->value != sensor->info_priv.exposure)
2327                 {
2328                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2329                     {
2330                         return -EINVAL;
2331                     }
2332                     sensor->info_priv.exposure = ctrl->value;
2333                 }
2334                 break;
2335             }
2336 #endif
2337 #if CONFIG_SENSOR_Saturation
2338         case V4L2_CID_SATURATION:
2339             {
2340                 if (ctrl->value != sensor->info_priv.saturation)
2341                 {
2342                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2343                     {
2344                         return -EINVAL;
2345                     }
2346                     sensor->info_priv.saturation = ctrl->value;
2347                 }
2348                 break;
2349             }
2350 #endif
2351 #if CONFIG_SENSOR_Contrast
2352         case V4L2_CID_CONTRAST:
2353             {
2354                 if (ctrl->value != sensor->info_priv.contrast)
2355                 {
2356                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2357                     {
2358                         return -EINVAL;
2359                     }
2360                     sensor->info_priv.contrast = ctrl->value;
2361                 }
2362                 break;
2363             }
2364 #endif
2365 #if CONFIG_SENSOR_WhiteBalance
2366         case V4L2_CID_DO_WHITE_BALANCE:
2367             {
2368                 if (ctrl->value != sensor->info_priv.whiteBalance)
2369                 {
2370                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2371                     {
2372                         return -EINVAL;
2373                     }
2374                     sensor->info_priv.whiteBalance = ctrl->value;
2375                 }
2376                 break;
2377             }
2378 #endif
2379 #if CONFIG_SENSOR_Mirror
2380         case V4L2_CID_HFLIP:
2381             {
2382                 if (ctrl->value != sensor->info_priv.mirror)
2383                 {
2384                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2385                         return -EINVAL;
2386                     sensor->info_priv.mirror = ctrl->value;
2387                 }
2388                 break;
2389             }
2390 #endif
2391 #if CONFIG_SENSOR_Flip
2392         case V4L2_CID_VFLIP:
2393             {
2394                 if (ctrl->value != sensor->info_priv.flip)
2395                 {
2396                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2397                         return -EINVAL;
2398                     sensor->info_priv.flip = ctrl->value;
2399                 }
2400                 break;
2401             }
2402 #endif
2403         default:
2404             break;
2405     }
2406
2407     return 0;
2408 }
2409 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2410 {
2411     const struct v4l2_queryctrl *qctrl;
2412     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2413     struct sensor *sensor = to_sensor(client);
2414
2415     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2416
2417     if (!qctrl)
2418     {
2419         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2420         return -EINVAL;
2421     }
2422
2423     switch (ext_ctrl->id)
2424     {
2425         case V4L2_CID_SCENE:
2426             {
2427                 ext_ctrl->value = sensor->info_priv.scene;
2428                 break;
2429             }
2430         case V4L2_CID_EFFECT:
2431             {
2432                 ext_ctrl->value = sensor->info_priv.effect;
2433                 break;
2434             }
2435         case V4L2_CID_ZOOM_ABSOLUTE:
2436             {
2437                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2438                 break;
2439             }
2440         case V4L2_CID_ZOOM_RELATIVE:
2441             {
2442                 return -EINVAL;
2443             }
2444         case V4L2_CID_FOCUS_ABSOLUTE:
2445             {
2446                 ext_ctrl->value = sensor->info_priv.focus;
2447                 break;
2448             }
2449         case V4L2_CID_FOCUS_RELATIVE:
2450             {
2451                 return -EINVAL;
2452             }
2453         case V4L2_CID_FLASH:
2454             {
2455                 ext_ctrl->value = sensor->info_priv.flash;
2456                 break;
2457             }
2458         default :
2459             break;
2460     }
2461     return 0;
2462 }
2463 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2464 {
2465     const struct v4l2_queryctrl *qctrl;
2466     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2467     struct sensor *sensor = to_sensor(client);
2468     int val_offset;
2469
2470     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2471     
2472     if (!qctrl)
2473     {
2474         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2475         return -EINVAL;
2476     }
2477
2478         val_offset = 0;
2479     switch (ext_ctrl->id)
2480     {
2481 #if CONFIG_SENSOR_Scene
2482         case V4L2_CID_SCENE:
2483             {
2484                 if (ext_ctrl->value != sensor->info_priv.scene)
2485                 {
2486                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2487                         return -EINVAL;
2488                     sensor->info_priv.scene = ext_ctrl->value;
2489                 }
2490                 break;
2491             }
2492 #endif
2493 #if CONFIG_SENSOR_Effect
2494         case V4L2_CID_EFFECT:
2495             {
2496                 if (ext_ctrl->value != sensor->info_priv.effect)
2497                 {
2498                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2499                         return -EINVAL;
2500                     sensor->info_priv.effect= ext_ctrl->value;
2501                 }
2502                 break;
2503             }
2504 #endif
2505 #if CONFIG_SENSOR_DigitalZoom
2506         case V4L2_CID_ZOOM_ABSOLUTE:
2507             {
2508                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2509                     return -EINVAL;
2510
2511                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2512                 {
2513                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2514
2515                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2516                         return -EINVAL;
2517                     sensor->info_priv.digitalzoom += val_offset;
2518
2519                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2520                 }
2521
2522                 break;
2523             }
2524         case V4L2_CID_ZOOM_RELATIVE:
2525             {
2526                 if (ext_ctrl->value)
2527                 {
2528                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2529                         return -EINVAL;
2530                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2531
2532                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2533                 }
2534                 break;
2535             }
2536 #endif
2537 #if CONFIG_SENSOR_Focus
2538         case V4L2_CID_FOCUS_ABSOLUTE:
2539             {
2540                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2541                     return -EINVAL;
2542
2543                 if (ext_ctrl->value != sensor->info_priv.focus)
2544                 {
2545                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2546
2547                     sensor->info_priv.focus += val_offset;
2548                 }
2549
2550                 break;
2551             }
2552         case V4L2_CID_FOCUS_RELATIVE:
2553             {
2554                 if (ext_ctrl->value)
2555                 {
2556                     sensor->info_priv.focus += ext_ctrl->value;
2557
2558                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2559                 }
2560                 break;
2561             }
2562 #endif
2563 #if CONFIG_SENSOR_Flash
2564         case V4L2_CID_FLASH:
2565             {
2566                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2567                     return -EINVAL;
2568                 sensor->info_priv.flash = ext_ctrl->value;
2569
2570                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2571                 break;
2572             }
2573 #endif
2574         default:
2575             break;
2576     }
2577
2578     return 0;
2579 }
2580
2581 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2582 {
2583     struct i2c_client *client = sd->priv;
2584     struct soc_camera_device *icd = client->dev.platform_data;
2585     int i, error_cnt=0, error_idx=-1;
2586
2587
2588     for (i=0; i<ext_ctrl->count; i++) {
2589         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2590             error_cnt++;
2591             error_idx = i;
2592         }
2593     }
2594
2595     if (error_cnt > 1)
2596         error_idx = ext_ctrl->count;
2597
2598     if (error_idx != -1) {
2599         ext_ctrl->error_idx = error_idx;
2600         return -EINVAL;
2601     } else {
2602         return 0;
2603     }
2604 }
2605
2606 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2607 {
2608     struct i2c_client *client = sd->priv;
2609     struct soc_camera_device *icd = client->dev.platform_data;
2610     int i, error_cnt=0, error_idx=-1;
2611
2612
2613     for (i=0; i<ext_ctrl->count; i++) {
2614         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2615             error_cnt++;
2616             error_idx = i;
2617         }
2618     }
2619
2620     if (error_cnt > 1)
2621         error_idx = ext_ctrl->count;
2622
2623     if (error_idx != -1) {
2624         ext_ctrl->error_idx = error_idx;
2625         return -EINVAL;
2626     } else {
2627         return 0;
2628     }
2629 }
2630
2631 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2632  * this wasn't our capture interface, so, we wait for the right one */
2633 static int sensor_video_probe(struct soc_camera_device *icd,
2634                                struct i2c_client *client)
2635 {
2636     char value;
2637     int ret,pid = 0;
2638     struct sensor *sensor = to_sensor(client);
2639
2640     /* We must have a parent by now. And it cannot be a wrong one.
2641      * So this entire test is completely redundant. */
2642     if (!icd->dev.parent ||
2643             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2644                 return -ENODEV;
2645
2646         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2647                 ret = -ENODEV;
2648                 goto sensor_video_probe_err;
2649         }
2650
2651     /* soft reset */
2652     ret = sensor_write(client, 0x0103, 0x01);
2653     if (ret != 0) {
2654         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2655         ret = -ENODEV;
2656                 goto sensor_video_probe_err;
2657         }
2658     mdelay(5);          //delay 5 microseconds
2659
2660     /* check if it is an sensor sensor */
2661     ret = sensor_read(client, 0x300a, &value);
2662     if (ret != 0) {
2663         SENSOR_TR("read chip id high byte failed\n");
2664         ret = -ENODEV;
2665         goto sensor_video_probe_err;
2666     }
2667
2668     pid |= (value << 8);
2669
2670     ret = sensor_read(client, 0x300b, &value);
2671     if (ret != 0) {
2672         SENSOR_TR("read chip id low byte failed\n");
2673         ret = -ENODEV;
2674         goto sensor_video_probe_err;
2675     }
2676
2677     pid |= (value & 0xff);
2678     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2679     if (pid == SENSOR_ID) {
2680         sensor->model = SENSOR_V4L2_IDENT;
2681     } else {
2682         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2683         ret = -ENODEV;
2684         goto sensor_video_probe_err;
2685     }
2686
2687     icd->formats = sensor_colour_formats;
2688     icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
2689
2690     return 0;
2691
2692 sensor_video_probe_err:
2693
2694     return ret;
2695 }
2696
2697 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2698 {
2699         struct i2c_client *client = sd->priv;
2700     struct soc_camera_device *icd = client->dev.platform_data;
2701     struct sensor *sensor = to_sensor(client);
2702     int ret = 0,i;
2703     
2704         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2705         switch (cmd)
2706         {
2707                 case RK29_CAM_SUBDEV_DEACTIVATE:
2708                 {
2709                         sensor_deactivate(client);
2710                         break;
2711                 }
2712
2713                 case RK29_CAM_SUBDEV_IOREQUEST:
2714                 {
2715                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2716             if (sensor->sensor_io_request != NULL) { 
2717                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
2718                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2719                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2720                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
2721                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2722                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2723                 }
2724             } else {
2725                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2726                 ret = -EINVAL;
2727                 goto sensor_ioctl_end;
2728             }
2729             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2730                for this project */
2731             #if CONFIG_SENSOR_Flash     
2732                 if (sensor->sensor_gpio_res) { 
2733                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2734                     for (i = 0; i < icd->ops->num_controls; i++) {
2735                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2736                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2737                                 }
2738                     }
2739                     sensor->info_priv.flash = 0xff;
2740                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2741                 }
2742                 }
2743             #endif
2744                         break;
2745                 }
2746                 default:
2747                 {
2748                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2749                         break;
2750                 }
2751         }
2752 sensor_ioctl_end:
2753         return ret;
2754
2755 }
2756
2757 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2758         .init           = sensor_init,
2759         .g_ctrl         = sensor_g_control,
2760         .s_ctrl         = sensor_s_control,
2761         .g_ext_ctrls          = sensor_g_ext_controls,
2762         .s_ext_ctrls          = sensor_s_ext_controls,
2763         .g_chip_ident   = sensor_g_chip_ident,
2764         .ioctl = sensor_ioctl,
2765 };
2766
2767 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2768         .s_fmt          = sensor_s_fmt,
2769         .g_fmt          = sensor_g_fmt,
2770         .try_fmt        = sensor_try_fmt,
2771 };
2772
2773 static struct v4l2_subdev_ops sensor_subdev_ops = {
2774         .core   = &sensor_subdev_core_ops,
2775         .video = &sensor_subdev_video_ops,
2776 };
2777
2778 static int sensor_probe(struct i2c_client *client,
2779                          const struct i2c_device_id *did)
2780 {
2781     struct sensor *sensor;
2782     struct soc_camera_device *icd = client->dev.platform_data;
2783     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2784     struct soc_camera_link *icl;
2785     int ret;
2786
2787     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2788     if (!icd) {
2789         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2790         return -EINVAL;
2791     }
2792
2793     icl = to_soc_camera_link(icd);
2794     if (!icl) {
2795         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2796         return -EINVAL;
2797     }
2798
2799     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2800         dev_warn(&adapter->dev,
2801                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2802         return -EIO;
2803     }
2804
2805     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2806     if (!sensor)
2807         return -ENOMEM;
2808
2809     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2810
2811     /* Second stage probe - when a capture adapter is there */
2812     icd->ops            = &sensor_ops;
2813     icd->y_skip_top             = 0;
2814         #if CONFIG_SENSOR_I2C_NOSCHED
2815         atomic_set(&sensor->tasklock_cnt,0);
2816         #endif
2817
2818     ret = sensor_video_probe(icd, client);
2819     if (ret < 0) {
2820         icd->ops = NULL;
2821         i2c_set_clientdata(client, NULL);
2822         kfree(sensor);
2823                 sensor = NULL;
2824     }
2825     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2826     return ret;
2827 }
2828
2829 static int sensor_remove(struct i2c_client *client)
2830 {
2831     struct sensor *sensor = to_sensor(client);
2832     struct soc_camera_device *icd = client->dev.platform_data;
2833
2834     icd->ops = NULL;
2835     i2c_set_clientdata(client, NULL);
2836     client->driver = NULL;
2837     kfree(sensor);
2838         sensor = NULL;
2839     return 0;
2840 }
2841
2842 static const struct i2c_device_id sensor_id[] = {
2843         {SENSOR_NAME_STRING(), 0 },
2844         { }
2845 };
2846 MODULE_DEVICE_TABLE(i2c, sensor_id);
2847
2848 static struct i2c_driver sensor_i2c_driver = {
2849         .driver = {
2850                 .name = SENSOR_NAME_STRING(),
2851         },
2852         .probe          = sensor_probe,
2853         .remove         = sensor_remove,
2854         .id_table       = sensor_id,
2855 };
2856
2857 static int __init sensor_mod_init(void)
2858 {
2859     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2860     return i2c_add_driver(&sensor_i2c_driver);
2861 }
2862
2863 static void __exit sensor_mod_exit(void)
2864 {
2865     i2c_del_driver(&sensor_i2c_driver);
2866 }
2867
2868 device_initcall_sync(sensor_mod_init);
2869 module_exit(sensor_mod_exit);
2870
2871 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2872 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2873 MODULE_LICENSE("GPL");
2874
2875