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