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