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