2 * Driver for MT9M001 CMOS Image Sensor from Micron
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/miscdevice.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/soc_camera.h>
22 #include <plat/rk_camera.h>
26 module_param(debug, int, S_IRUGO|S_IWUSR);
28 #define dprintk(level, fmt, arg...) do { \
30 printk(KERN_WARNING fmt , ## arg); } while (0)
32 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
33 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
42 #define MIN(x,y) ((x<y) ? x: y)
43 #define MAX(x,y) ((x>y) ? x: y)
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK29_CAM_SENSOR_OV3640
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV3640
48 #define SENSOR_ID 0x364c
49 #define SENSOR_MIN_WIDTH 176
50 #define SENSOR_MIN_HEIGHT 144
51 #define SENSOR_MAX_WIDTH 2048
52 #define SENSOR_MAX_HEIGHT 1536
53 #define SENSOR_INIT_WIDTH 640 /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT 480
55 #define SENSOR_INIT_WINSEQADR sensor_svga
56 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_YUYV8_2X8
58 #define CONFIG_SENSOR_WhiteBalance 1
59 #define CONFIG_SENSOR_Brightness 0
60 #define CONFIG_SENSOR_Contrast 0
61 #define CONFIG_SENSOR_Saturation 0
62 #define CONFIG_SENSOR_Effect 1
63 #define CONFIG_SENSOR_Scene 1
64 #define CONFIG_SENSOR_DigitalZoom 0
65 #define CONFIG_SENSOR_Exposure 0
66 #define CONFIG_SENSOR_Flash 0
67 #define CONFIG_SENSOR_Mirror 0
68 #define CONFIG_SENSOR_Flip 0
69 #ifdef CONFIG_OV3640_AUTOFOCUS
70 #define CONFIG_SENSOR_Focus 1
71 #include "ov3640_af_firmware.c"
73 #define CONFIG_SENSOR_Focus 0
76 #define CONFIG_SENSOR_I2C_SPEED 100000 /* Hz */
77 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
78 #define CONFIG_SENSOR_I2C_NOSCHED 0
79 #define CONFIG_SENSOR_I2C_RDWRCHK 0
81 #define CONFIG_SENSOR_WRITE_REGS 1
82 #define WRITE_REGS_NUM 3
84 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
85 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
86 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
88 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
89 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
90 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
91 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
92 #define COLOR_TEMPERATURE_OFFICE_DN 3500
93 #define COLOR_TEMPERATURE_OFFICE_UP 5000
94 #define COLOR_TEMPERATURE_HOME_DN 2500
95 #define COLOR_TEMPERATURE_HOME_UP 3500
97 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
98 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
100 #define SENSOR_AF_IS_ERR (0x00<<0)
101 #define SENSOR_AF_IS_OK (0x01<<0)
102 #define SENSOR_INIT_IS_ERR (0x00<<28)
103 #define SENSOR_INIT_IS_OK (0x01<<28)
105 #if CONFIG_SENSOR_Focus
106 /*#define SENSOR_AF_MODE_INFINITY 0
107 #define SENSOR_AF_MODE_MACRO 1
108 #define SENSOR_AF_MODE_FIXED 2
109 #define SENSOR_AF_MODE_AUTO 3
110 #define SENSOR_AF_MODE_CONTINUOUS 4
111 #define SENSOR_AF_MODE_CLOSE 5*/
112 #define SENSOR_AF_MODE_AUTO 0
113 #define SENSOR_AF_MODE_CLOSE 1
117 #if CONFIG_SENSOR_Focus
118 /* ov3640 VCM Command and Status Registers */
119 #define CMD_MAIN_Reg 0x3F00
120 #define CMD_ACK_Reg 0x3F01
121 #define CMD_PARA0_Reg 0x3F05
122 #define CMD_PARA1_Reg 0x3F04
123 #define CMD_PARA2_Reg 0x3F03
124 #define CMD_PARA3_Reg 0x3F02
125 #define STA_ZONE_Reg 0x3F06
126 #define STA_FOCUS_Reg 0x3F07
128 /* ov3640 VCM Command */
129 #define OverlayEn_Cmd 0x01
130 #define OverlayDis_Cmd 0x02
131 #define SingleFocus_Cmd 0x03
132 #define ConstFocus_Cmd 0x04
133 #define StepMode_Cmd 0x05
134 #define PauseFocus_Cmd 0x06
135 #define ReturnIdle_Cmd 0x08
136 #define SetZone_Cmd 0x10
137 #define UpdateZone_Cmd 0x12
138 #define SetMotor_Cmd 0x20
140 /* ov3640 Focus State */
141 #define S_FIRWRE 0xFF
142 #define S_STARTUP 0xFA
144 #define S_DRVICERR 0xEE
146 #define S_FOCUSING 0x01
147 #define S_FOCUSED 0x02
148 #define S_CAPTURE 0x12
151 /* ovxxxx Zone State */
152 #define Zone_Is_Focused(a, zone_val) (zone_val&(1<<(a-3)))
153 #define Zone_Get_ID(zone_val) (zone_val&0x03)
155 #define Zone_CenterMode 0x01
156 #define Zone_5xMode 0x02
157 #define Zone_5PlusMode 0x03
158 #define Zone_4fMode 0x04
160 #define ZoneSel_Auto 0x0b
161 #define ZoneSel_SemiAuto 0x0c
162 #define ZoneSel_Manual 0x0d
163 #define ZoneSel_Rotate 0x0e
165 /* ovxxxx Step Focus Commands */
166 #define StepFocus_Near_Tag 0x01
167 #define StepFocus_Far_Tag 0x02
168 #define StepFocus_Furthest_Tag 0x03
169 #define StepFocus_Nearest_Tag 0x04
170 #define StepFocus_Spec_Tag 0x10
173 //flash off in fixed time to prevent from too hot , zyc
175 struct soc_camera_device *icd;
176 struct hrtimer timer;
178 #if CONFIG_SENSOR_Flash
179 static enum hrtimer_restart flash_off_func(struct hrtimer *timer);
181 static struct flash_timer flash_off_timer;
182 //for user defined if user want to customize the series , zyc
183 #ifdef CONFIG_OV5640_USER_DEFINED_SERIES
184 #include "ov5640_user_series.c"
186 /* init VGA 640*480 */
187 static struct reginfo sensor_init_data[] =
199 {0x30b2, 0x18}, // by FAE
212 {0x30a9, 0xbd},//disable internal DVDD, by FAE.
265 {0x3404, 0x00},//YUV, sequence
342 #ifdef CONFIG_RK29_NEWTON
343 {0x307c, 0x10}, // flip && mirror ,for newton
347 //{0x307c, 0x13}, // flip && mirror
348 {0x307c, 0x10}, // flip && mirror ,by FAE.
379 {0x3089, 0x88},// 0x80, by FAE.
381 {0x308b, 0xe4},// 0xe0, by FAE.
382 {0x304c, 0x84}, //0x83, by FAE.
388 static struct reginfo sensor_qxga_preview[] =
430 //[Sensor.YUV.2048x1536] XGA->QXGA
468 static struct reginfo sensor_qxga_capture[] = {
510 static struct reginfo *sensor_qxga[2] = {
516 static struct reginfo sensor_720p[] = {
522 static struct reginfo sensor_uxga_preview[] =
558 {0x304c, 0x81},//56Mhz PCLK output
562 static struct reginfo sensor_uxga_capture[] =
576 {0x304c, 0x81},//56Mhz PCLK output
580 static struct reginfo *sensor_uxga[2] = {
586 static struct reginfo sensor_sxga_preview[] =
622 {0x304c, 0x81},//56Mhz PCLK output
626 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
628 static struct reginfo sensor_1080p[]=
633 static struct reginfo sensor_sxga_capture[] =
647 {0x304c, 0x81},//56Mhz PCLK output
651 static struct reginfo *sensor_sxga[2] = {
656 /* 2592X1944 QSXGA */
657 static struct reginfo sensor_qsxga[] =
662 static struct reginfo sensor_xga_preview[] =
694 {0x304c, 0x82},//28Mhz PCLK output
699 static struct reginfo sensor_xga_capture[] =
713 {0x304c, 0x82},//28Mhz PCLK output
717 static struct reginfo *sensor_xga[2] = {
723 static struct reginfo sensor_svga_preview[] =
755 {0x304c, 0x83},//28Mhz PCLK output
759 static struct reginfo sensor_svga_capture[] =
773 {0x304c, 0x82},//28Mhz PCLK output
777 static struct reginfo * sensor_svga[2] = {
783 static struct reginfo sensor_vga_preview[] =
812 {0x3089, 0x88},// 0x80, by FAE.
814 {0x308b, 0xe4},// 0xe0, by FAE.
815 {0x304c, 0x84}, //0x83, by FAE.
833 static struct reginfo sensor_vga_capture[] =
847 {0x304c, 0x82},//14Mhz PCLK output 84
851 static struct reginfo *sensor_vga[2] = {
857 static struct reginfo sensor_cif_preview[] =
909 static struct reginfo sensor_cif_capture[] =
923 {0x304c, 0x84},//14Mhz PCLK output
927 static struct reginfo *sensor_cif[2] = {
933 static struct reginfo sensor_qvga_preview[] =
965 {0x304c, 0x89},//14Mhz PCLK output
969 static struct reginfo sensor_qvga_capture[] =
983 {0x304c, 0x84},//14Mhz PCLK output
987 static struct reginfo *sensor_qvga[2] = {
993 static struct reginfo sensor_qcif_preview[] =
1025 {0x304c, 0x82},//14Mhz PCLK output 89
1029 static struct reginfo sensor_qcif_capture[] =
1043 {0x304c, 0x84},//14Mhz PCLK output
1047 static struct reginfo *sensor_qcif[2] = {
1048 sensor_qcif_preview,
1049 sensor_qcif_capture,
1055 static struct reginfo ov3640_Sharpness_auto[] =
1060 static struct reginfo ov3640_Sharpness1[] =
1066 static struct reginfo ov3640_Sharpness2[][3] =
1073 static struct reginfo ov3640_Sharpness3[] =
1079 static struct reginfo ov3640_Sharpness4[]=
1086 static struct reginfo ov3640_Sharpness5[] =
1094 static struct reginfo sensor_ClrFmt_YUYV[]=
1100 static struct reginfo sensor_ClrFmt_UYVY[]=
1106 #if CONFIG_SENSOR_WhiteBalance
1107 static struct reginfo sensor_WhiteB_Auto[]=
1109 {0x332b, 0x00},//AWB auto, bit[3]:0,auto
1113 /* Cloudy Colour Temperature : 6500K - 8000K */
1114 static struct reginfo sensor_WhiteB_Cloudy[]=
1123 /* ClearDay Colour Temperature : 5000K - 6500K */
1124 static struct reginfo sensor_WhiteB_ClearDay[]=
1127 {0x332b, 0x08}, //AWB off
1134 /* Office Colour Temperature : 3500K - 5000K */
1135 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
1145 /* Home Colour Temperature : 2500K - 3500K */
1146 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
1156 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
1157 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
1161 #if CONFIG_SENSOR_Brightness
1162 static struct reginfo sensor_Brightness0[]=
1173 static struct reginfo sensor_Brightness1[]=
1184 static struct reginfo sensor_Brightness2[]=
1195 static struct reginfo sensor_Brightness3[]=
1206 static struct reginfo sensor_Brightness4[]=
1217 static struct reginfo sensor_Brightness5[]=
1228 static struct reginfo sensor_Brightness6[]=
1232 {0x3355, 0x04}, //bit[2] enable
1233 {0x3354, 0x01}, //bit[3] sign of brightness
1239 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
1240 sensor_Brightness4, sensor_Brightness5,sensor_Brightness6,NULL,
1245 #if CONFIG_SENSOR_Effect
1246 static struct reginfo sensor_Effect_Normal[] =
1255 static struct reginfo sensor_Effect_WandB[] =
1259 {0x3355, 0x18}, //bit[4]fix u enable, bit[3]fix v enable
1266 static struct reginfo sensor_Effect_Sepia[] =
1277 static struct reginfo sensor_Effect_Negative[] =
1281 {0x3355, 0x40}, //bit[6] negative
1285 static struct reginfo sensor_Effect_Bluish[] =
1296 static struct reginfo sensor_Effect_Green[] =
1307 static struct reginfo sensor_Effect_Red[] =
1317 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
1318 sensor_Effect_Bluish, sensor_Effect_Green,sensor_Effect_Red,NULL,
1321 #if CONFIG_SENSOR_Exposure
1322 static struct reginfo sensor_Exposure0[]=
1333 static struct reginfo sensor_Exposure1[]=
1344 static struct reginfo sensor_Exposure2[]=
1355 static struct reginfo sensor_Exposure3[]=
1366 static struct reginfo sensor_Exposure4[]=
1377 static struct reginfo sensor_Exposure5[]=
1388 static struct reginfo sensor_Exposure6[]=
1399 static struct reginfo sensor_Exposure7[]=
1410 static struct reginfo sensor_Exposure8[]=
1421 static struct reginfo sensor_Exposure9[]=
1432 static struct reginfo sensor_Exposure10[]=
1443 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1444 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,sensor_Exposure7,sensor_Exposure8,sensor_Exposure9,
1445 sensor_Exposure10,NULL,
1448 #if CONFIG_SENSOR_Saturation
1449 static struct reginfo sensor_Saturation0[]=
1460 static struct reginfo sensor_Saturation1[]=
1471 static struct reginfo sensor_Saturation2[]=
1482 static struct reginfo sensor_Saturation3[]=
1493 static struct reginfo sensor_Saturation4[]=
1496 {0x3302, 0xef}, //bit[7]:1, enable SDE
1497 {0x3355, 0x02}, //enable color saturation
1504 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2,
1505 sensor_Saturation3, sensor_Saturation4, NULL,};
1508 #if CONFIG_SENSOR_Contrast
1509 static struct reginfo sensor_Contrast0[]=
1521 static struct reginfo sensor_Contrast1[]=
1533 static struct reginfo sensor_Contrast2[]=
1545 static struct reginfo sensor_Contrast3[]=
1557 static struct reginfo sensor_Contrast4[]=
1570 static struct reginfo sensor_Contrast5[]=
1582 static struct reginfo sensor_Contrast6[]=
1586 {0x3355, 0x04}, //bit[2] enable contrast/brightness
1587 {0x3354, 0x01}, //bit[2] Yoffset sign
1593 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1594 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1598 #if CONFIG_SENSOR_Mirror
1599 static struct reginfo sensor_MirrorOn[]=
1601 {0x307c, 0x12}, //mirror
1608 static struct reginfo sensor_MirrorOff[]=
1610 {0x307c, 0x10},// no mirror/flip
1617 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1619 #if CONFIG_SENSOR_Flip
1620 static struct reginfo sensor_FlipOn[]=
1622 {0x307c, 0x11}, //flip
1629 static struct reginfo sensor_FlipOff[]=
1631 {0x307c, 0x10}, // no mirror/flip
1637 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1640 #if CONFIG_SENSOR_Scene
1641 static struct reginfo sensor_SceneAuto[] =
1643 {0x3014, 0x84},//bit[3]=0 disable auto night mode
1644 {0x3015, 0x12},// or 0x02
1645 {0x302d, 0x00},//clear dummy frame
1646 {0x302e, 0x00},//clear dummy frame
1651 static struct reginfo sensor_SceneNight[] =
1653 {0x3014, 0x8c}, //bit[3] =1 enable auto night mode ¸ù¾Ý¹âÏßÇ¿Èõ×Ô¶¯½µÖ¡
1654 {0x3015, 0x22},//bit[6:4] ÉèÖò»Í¬,½µµÄÖ¡Âʲ»Ò»Ñù.¿ÉÒÔ¿´datasheet
1656 //000 no dummy frame
1663 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1666 #if CONFIG_SENSOR_DigitalZoom
1667 static struct reginfo sensor_Zoom0[] =
1672 static struct reginfo sensor_Zoom1[] =
1677 static struct reginfo sensor_Zoom2[] =
1683 static struct reginfo sensor_Zoom3[] =
1687 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1689 static struct v4l2_querymenu sensor_menus[] =
1691 #if CONFIG_SENSOR_WhiteBalance
1692 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1693 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1694 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1697 #if CONFIG_SENSOR_Effect
1698 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1699 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1700 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1703 #if CONFIG_SENSOR_Scene
1704 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1707 #if CONFIG_SENSOR_Flash
1708 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1709 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1713 static struct v4l2_queryctrl sensor_controls[] =
1715 #if CONFIG_SENSOR_WhiteBalance
1717 .id = V4L2_CID_DO_WHITE_BALANCE,
1718 .type = V4L2_CTRL_TYPE_MENU,
1719 .name = "White Balance Control",
1727 #if CONFIG_SENSOR_Brightness
1729 .id = V4L2_CID_BRIGHTNESS,
1730 .type = V4L2_CTRL_TYPE_INTEGER,
1731 .name = "Brightness Control",
1739 #if CONFIG_SENSOR_Effect
1741 .id = V4L2_CID_EFFECT,
1742 .type = V4L2_CTRL_TYPE_MENU,
1743 .name = "Effect Control",
1751 #if CONFIG_SENSOR_Exposure
1753 .id = V4L2_CID_EXPOSURE,
1754 .type = V4L2_CTRL_TYPE_INTEGER,
1755 .name = "Exposure Control",
1763 #if CONFIG_SENSOR_Saturation
1765 .id = V4L2_CID_SATURATION,
1766 .type = V4L2_CTRL_TYPE_INTEGER,
1767 .name = "Saturation Control",
1775 #if CONFIG_SENSOR_Contrast
1777 .id = V4L2_CID_CONTRAST,
1778 .type = V4L2_CTRL_TYPE_INTEGER,
1779 .name = "Contrast Control",
1787 #if CONFIG_SENSOR_Mirror
1789 .id = V4L2_CID_HFLIP,
1790 .type = V4L2_CTRL_TYPE_BOOLEAN,
1791 .name = "Mirror Control",
1799 #if CONFIG_SENSOR_Flip
1801 .id = V4L2_CID_VFLIP,
1802 .type = V4L2_CTRL_TYPE_BOOLEAN,
1803 .name = "Flip Control",
1811 #if CONFIG_SENSOR_Scene
1813 .id = V4L2_CID_SCENE,
1814 .type = V4L2_CTRL_TYPE_MENU,
1815 .name = "Scene Control",
1823 #if CONFIG_SENSOR_DigitalZoom
1825 .id = V4L2_CID_ZOOM_RELATIVE,
1826 .type = V4L2_CTRL_TYPE_INTEGER,
1827 .name = "DigitalZoom Control",
1833 .id = V4L2_CID_ZOOM_ABSOLUTE,
1834 .type = V4L2_CTRL_TYPE_INTEGER,
1835 .name = "DigitalZoom Control",
1843 #if CONFIG_SENSOR_Focus
1845 .id = V4L2_CID_FOCUS_RELATIVE,
1846 .type = V4L2_CTRL_TYPE_INTEGER,
1847 .name = "Focus Control",
1853 .id = V4L2_CID_FOCUS_ABSOLUTE,
1854 .type = V4L2_CTRL_TYPE_INTEGER,
1855 .name = "Focus Control",
1859 .default_value = 125,
1862 .id = V4L2_CID_FOCUS_AUTO,
1863 .type = V4L2_CTRL_TYPE_BOOLEAN,
1864 .name = "Focus Control",
1870 #if 0 //IF CONTINOUS IS SUPPORT, SET TO 1
1872 .id = V4L2_CID_FOCUS_CONTINUOUS,
1873 .type = V4L2_CTRL_TYPE_BOOLEAN,
1874 .name = "Focus Control",
1883 #if CONFIG_SENSOR_Flash
1885 .id = V4L2_CID_FLASH,
1886 .type = V4L2_CTRL_TYPE_MENU,
1887 .name = "Flash Control",
1896 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1897 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1898 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1899 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1900 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1901 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1902 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1903 static int sensor_resume(struct soc_camera_device *icd);
1904 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1905 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1906 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1907 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1908 static int sensor_deactivate(struct i2c_client *client);
1909 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1910 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1912 static struct soc_camera_ops sensor_ops =
1914 .suspend = sensor_suspend,
1915 .resume = sensor_resume,
1916 .set_bus_param = sensor_set_bus_param,
1917 .query_bus_param = sensor_query_bus_param,
1918 .controls = sensor_controls,
1919 .menus = sensor_menus,
1920 .num_controls = ARRAY_SIZE(sensor_controls),
1921 .num_menus = ARRAY_SIZE(sensor_menus),
1924 /* only one fixed colorspace per pixelcode */
1925 struct sensor_datafmt {
1926 enum v4l2_mbus_pixelcode code;
1927 enum v4l2_colorspace colorspace;
1930 /* Find a data format by a pixel code in an array */
1931 static const struct sensor_datafmt *sensor_find_datafmt(
1932 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1936 for (i = 0; i < n; i++)
1937 if (fmt[i].code == code)
1943 static const struct sensor_datafmt sensor_colour_fmts[] = {
1944 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG} ,
1945 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG}
1951 WqCmd_af_special_pos,
1955 WqCmd_af_return_idle,
1957 enum sensor_wq_result
1965 struct i2c_client *client;
1966 struct delayed_work dwork;
1967 enum sensor_wq_cmd cmd;
1968 wait_queue_head_t done;
1969 enum sensor_wq_result result;
1974 typedef struct sensor_info_priv_s
1990 unsigned char mirror; /* HFLIP */
1991 unsigned char flip; /* VFLIP */
1992 struct reginfo *winseqe_cur_addr;
1993 struct sensor_datafmt fmt;
1994 unsigned int enable;
1995 unsigned int funmodule_state;
1996 } sensor_info_priv_t;
2000 struct sensor_parameter
2002 unsigned short int preview_maxlines;
2003 unsigned short int preview_exposure;
2004 unsigned short int preview_line_width;
2005 unsigned short int preview_gain;
2007 unsigned short int capture_framerate;
2008 unsigned short int preview_framerate;
2014 struct v4l2_subdev subdev;
2015 struct i2c_client *client;
2016 sensor_info_priv_t info_priv;
2017 struct sensor_parameter parameter;
2018 struct workqueue_struct *sensor_wq;
2019 struct mutex wq_lock;
2020 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
2021 #if CONFIG_SENSOR_I2C_NOSCHED
2022 atomic_t tasklock_cnt;
2024 struct rk29camera_platform_data *sensor_io_request;
2025 struct rk29camera_gpio_res *sensor_gpio_res;
2028 static struct sensor* to_sensor(const struct i2c_client *client)
2030 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
2033 static int sensor_task_lock(struct i2c_client *client, int lock)
2035 #if CONFIG_SENSOR_I2C_NOSCHED
2037 struct sensor *sensor = to_sensor(client);
2040 if (atomic_read(&sensor->tasklock_cnt) == 0) {
2041 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
2042 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
2046 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
2047 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
2048 goto sensor_task_lock_err;
2053 atomic_add(1, &sensor->tasklock_cnt);
2055 if (atomic_read(&sensor->tasklock_cnt) > 0) {
2056 atomic_sub(1, &sensor->tasklock_cnt);
2058 if (atomic_read(&sensor->tasklock_cnt) == 0)
2063 sensor_task_lock_err:
2071 #if CONFIG_SENSOR_WRITE_REGS
2072 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
2075 struct i2c_msg msg[1];
2078 msg->addr = client->addr;
2079 msg->flags = client->flags;
2080 msg->buf = reg_info;
2081 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2082 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
2088 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
2089 err = i2c_transfer(client->adapter, msg, 1);
2093 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
2103 /* sensor register write */
2104 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
2108 struct i2c_msg msg[1];
2111 buf[1] = reg & 0xFF;
2114 msg->addr = client->addr;
2115 msg->flags = client->flags;
2117 msg->len = sizeof(buf);
2118 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2119 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
2124 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
2125 err = i2c_transfer(client->adapter, msg, 1);
2130 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
2138 /* sensor register read */
2139 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
2143 struct i2c_msg msg[2];
2146 buf[1] = reg & 0xFF;
2148 msg[0].addr = client->addr;
2149 msg[0].flags = client->flags;
2151 msg[0].len = sizeof(buf);
2152 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2153 msg[0].read_type = 2; /* fpga i2c:2==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2155 msg[1].addr = client->addr;
2156 msg[1].flags = client->flags|I2C_M_RD;
2159 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2160 msg[1].read_type = 2; /* fpga i2c:2==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2164 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
2165 err = i2c_transfer(client->adapter, msg, 2);
2171 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
2179 /* write a array of registers */
2180 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
2184 #if CONFIG_SENSOR_Focus
2185 struct sensor *sensor = to_sensor(client);
2187 #if CONFIG_SENSOR_I2C_RDWRCHK
2190 #if CONFIG_SENSOR_WRITE_REGS
2197 if (sensor_task_lock(client, 1) < 0)
2198 goto sensor_write_array_end;
2199 while (regarray[i].reg != SEQUENCE_END) {
2200 #if CONFIG_SENSOR_Focus
2201 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
2202 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
2204 goto sensor_write_array_end;
2208 #if CONFIG_SENSOR_WRITE_REGS
2212 reg_length = 0x0001;
2214 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
2217 if(reg_num >= WRITE_REGS_NUM)
2224 size_num = reg_num + 1;
2226 ptemp = phead = (u8*)kmalloc((size_num+10)*sizeof(u8),GFP_KERNEL);
2228 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
2230 err = sensor_write(client, regarray[i].reg, regarray[i].val);
2232 *phead = regarray[j].reg >> 8;
2233 *(ptemp+1) = regarray[j].reg & 0xFF;
2235 for( ; reg_num > 0; reg_num --, j++) {
2236 *ptemp ++ = regarray[j].val;
2240 err = sensor_write_regs(client, ptemp,size_num);
2244 err = sensor_write(client, regarray[i].reg, regarray[i].val);
2247 err = sensor_write(client, regarray[i].reg, regarray[i].val);
2252 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
2256 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
2259 goto sensor_write_array_end;
2262 #if CONFIG_SENSOR_I2C_RDWRCHK
2263 sensor_read(client, regarray[i].reg, &valchk);
2264 if (valchk != regarray[i].val)
2265 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2271 #if CONFIG_SENSOR_Focus
2272 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
2273 || (regarray == sensor_init_data)) {
2274 sensor->info_priv.affm_reinit = 1;
2278 sensor_write_array_end:
2279 sensor_task_lock(client,0);
2282 #if CONFIG_SENSOR_I2C_RDWRCHK
2283 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
2291 while (regarray[i].reg != 0)
2293 sensor_read(client, regarray[i].reg, &valchk);
2294 if (valchk != regarray[i].val)
2295 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2302 #if CONFIG_SENSOR_Focus
2309 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
2312 char read_tag=0xff,cnt;
2315 if (cmdinfo->validate_bit & 0x80) {
2316 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
2317 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
2318 goto sensor_af_cmdset_err;
2320 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
2322 for (i=0; i<4; i++) {
2323 if (cmdinfo->validate_bit & (1<<i)) {
2324 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
2325 SENSOR_TR("%s write CMD_PARA_Reg(main:0x%x para%d:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,i,cmdinfo->cmd_para[i]);
2326 goto sensor_af_cmdset_err;
2328 SENSOR_DG("%s write CMD_PARA_Reg(main:0x%x para%d:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,i,cmdinfo->cmd_para[i]);
2332 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
2333 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
2334 goto sensor_af_cmdset_err;
2336 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
2339 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
2340 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
2341 goto sensor_af_cmdset_err;
2348 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
2349 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
2352 } while((read_tag != 0x00)&& (cnt++<100));
2354 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
2356 sensor_af_cmdset_err:
2360 static int sensor_af_idlechk(struct i2c_client *client)
2364 struct af_cmdinfo cmdinfo;
2366 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
2368 cmdinfo.cmd_tag = 0x01;
2369 cmdinfo.validate_bit = 0x80;
2370 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
2372 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2374 goto sensor_af_idlechk_end;
2379 ret = sensor_read(client, CMD_ACK_Reg, &state);
2381 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2383 goto sensor_af_idlechk_end;
2385 }while(0x00 != state);
2388 sensor_af_idlechk_end:
2392 static int sensor_af_single(struct i2c_client *client)
2396 struct af_cmdinfo cmdinfo;
2398 cmdinfo.cmd_tag = 0x01;
2399 cmdinfo.validate_bit = 0x80;
2400 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
2402 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
2404 goto sensor_af_single_end;
2414 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2416 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2418 goto sensor_af_single_end;
2420 }while((state == S_FOCUSING) && (cnt<100));
2422 if (state != S_FOCUSED) {
2423 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
2425 goto sensor_af_single_end;
2428 //sensor_af_cmdset(client, ReturnIdle_Cmd, NULL); // by duanyp. fix af blur when taking pictures
2429 sensor_af_single_end:
2433 static int sensor_af_const(struct i2c_client *client)
2442 static int sensor_af_init(struct i2c_client *client)
2447 ret = sensor_write_array(client, sensor_af_firmware);
2449 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
2451 goto sensor_af_init_end;
2461 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2463 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2465 goto sensor_af_init_end;
2467 }while((state == S_STARTUP) && (cnt<100));
2469 if (state != S_IDLE) {
2470 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
2472 goto sensor_af_init_end;
2476 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
2480 static int sensor_af_downfirmware(struct i2c_client *client)
2482 struct sensor *sensor = to_sensor(client);
2484 struct soc_camera_device *icd = client->dev.platform_data;
2485 struct v4l2_mbus_framefmt mf;
2487 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
2489 if (sensor_af_init(client)) {
2490 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
2493 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
2495 mf.width = icd->user_width;
2496 mf.height = icd->user_height;
2497 mf.code = sensor->info_priv.fmt.code;
2498 mf.colorspace = sensor->info_priv.fmt.colorspace;
2499 mf.field = V4L2_FIELD_NONE;
2500 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
2501 ret = sensor_af_const(client);
2503 switch (sensor->info_priv.auto_focus)
2505 case SENSOR_AF_MODE_AUTO:
2507 ret = sensor_af_single(client);
2510 case SENSOR_AF_MODE_CLOSE:
2517 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
2518 goto sensor_af_downfirmware_end;
2522 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
2525 sensor_af_downfirmware_end:
2529 static void sensor_af_workqueue(struct work_struct *work)
2531 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
2532 struct i2c_client *client = sensor_work->client;
2533 struct sensor *sensor = to_sensor(client);
2534 struct af_cmdinfo cmdinfo;
2536 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
2538 mutex_lock(&sensor->wq_lock);
2540 switch (sensor_work->cmd)
2544 if (sensor_af_downfirmware(client) < 0) {
2545 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2549 case WqCmd_af_single:
2551 if (sensor_af_single(client) < 0) {
2552 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2553 sensor_work->result = WqRet_fail;
2555 sensor_work->result = WqRet_success;
2559 case WqCmd_af_special_pos:
2561 sensor_af_idlechk(client);
2563 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2564 cmdinfo.cmd_para[0] = sensor_work->var;
2565 cmdinfo.validate_bit = 0x81;
2566 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2567 sensor_work->result = WqRet_fail;
2569 sensor_work->result = WqRet_success;
2572 case WqCmd_af_near_pos:
2574 sensor_af_idlechk(client);
2575 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2576 cmdinfo.validate_bit = 0x80;
2577 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2578 sensor_work->result = WqRet_fail;
2580 sensor_work->result = WqRet_success;
2583 case WqCmd_af_far_pos:
2585 sensor_af_idlechk(client);
2586 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2587 cmdinfo.validate_bit = 0x80;
2588 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2589 sensor_work->result = WqRet_fail;
2591 sensor_work->result = WqRet_success;
2594 case WqCmd_af_continues:
2596 if (sensor_af_const(client) < 0)
2597 sensor_work->result = WqRet_fail;
2599 sensor_work->result = WqRet_success;
2602 case WqCmd_af_return_idle:
2604 if (sensor_af_idlechk(client) < 0)
2605 sensor_work->result = WqRet_fail;
2607 sensor_work->result = WqRet_success;
2611 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
2615 if (sensor_work->wait == false) {
2616 kfree((void*)sensor_work);
2618 wake_up(&sensor_work->done);
2620 mutex_unlock(&sensor->wq_lock);
2624 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait)
2626 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2627 struct sensor *sensor = to_sensor(client);
2628 struct sensor_work *wk;
2631 if (sensor->sensor_wq == NULL) {
2633 goto sensor_af_workqueue_set_end;
2636 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) != SENSOR_AF_IS_OK) {
2637 if (cmd != WqCmd_af_init) {
2638 SENSOR_TR("%s %s cmd(%d) ingore,because af module isn't ready!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2640 goto sensor_af_workqueue_set_end;
2644 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
2646 wk->client = client;
2647 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
2649 wk->result = WqRet_inval;
2652 init_waitqueue_head(&wk->done);
2654 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
2656 /* ddl@rock-chips.com:
2657 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
2658 * As a result any other ioctl calls will proceed very, very slowly since each call
2659 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
2660 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
2663 mutex_unlock(&icd->video_lock);
2664 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(2500)) == 0) {
2665 SENSOR_TR("%s %s cmd(%d) is timeout!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2669 mutex_lock(&icd->video_lock);
2673 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2676 sensor_af_workqueue_set_end:
2680 static int sensor_parameter_record(struct i2c_client *client)
2682 u8 ret_l,ret_m,ret_h;
2684 struct sensor *sensor = to_sensor(client);
2686 sensor_write(client,0x3503,0x07); //stop AE/AG
2687 sensor_write(client,0x3406,0x01); //stop AWB
2689 sensor_read(client,0x3500,&ret_h);
2690 sensor_read(client,0x3501, &ret_m);
2691 sensor_read(client,0x3502, &ret_l);
2695 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
2696 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
2697 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
2699 sensor_read(client,0x350c, &ret_h);
2700 sensor_read(client,0x350d, &ret_l);
2701 sensor->parameter.preview_line_width = ret_h & 0xff;
2702 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
2703 //Read back AGC Gain for preview
2704 sensor_read(client,0x350a, &ret_h);
2705 sensor_read(client,0x350b, &tp_l);
2706 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
2708 sensor_read(client,0x380e, &ret_h);
2709 sensor->parameter.preview_maxlines = ret_h;
2710 sensor->parameter.preview_maxlines <<= 8;
2711 sensor_read(client,0x380f, &tp_l);
2712 sensor->parameter.preview_maxlines += tp_l;
2714 sensor->parameter.capture_framerate = 375;
2715 sensor->parameter.preview_framerate = 1500;
2717 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
2718 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2719 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2720 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2721 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2722 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2724 SENSOR_DG(" %s Read 0x350c = 0x%02x 0x350d = 0x%02x 0x350b=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_l, sensor->parameter.preview_gain);
2727 static int sensor_ae_transfer(struct i2c_client *client)
2732 u16 ulCapture_Exposure;
2733 u32 ulCapture_Exposure_Gain;
2737 u8 reg_l = 0,reg_h =0;
2738 u16 Preview_Maxlines;
2740 u32 Capture_MaxLines;
2741 struct sensor *sensor = to_sensor(client);
2743 //Preview_Maxlines = sensor->parameter.preview_line_width;
2744 Preview_Maxlines = sensor->parameter.preview_maxlines;
2745 Gain = sensor->parameter.preview_gain;
2747 sensor_read(client,0x350c, ®_h);
2748 sensor_read(client,0x350d, ®_l);
2749 Capture_MaxLines = reg_h & 0xff;
2750 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2753 sensor_read(client,0x380e, ®_h);
2754 Capture_MaxLines = reg_h;
2755 Capture_MaxLines <<= 8;
2756 sensor_read(client,0x380f, ®_l);
2757 Capture_MaxLines += reg_l;
2760 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2762 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2764 Lines_10ms = Lines_10ms & 0xffff;
2766 if(Preview_Maxlines == 0)
2767 Preview_Maxlines = 1;
2769 //ulCapture_Exposure =
2770 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2772 ulCapture_Exposure =
2773 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2774 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2776 iCapture_Gain = (Gain & 0x0f) + 16;
2778 iCapture_Gain = iCapture_Gain << 1;
2781 iCapture_Gain = iCapture_Gain << 1;
2784 iCapture_Gain = iCapture_Gain << 1;
2787 iCapture_Gain = iCapture_Gain << 1;
2790 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
2791 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2793 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2794 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2795 if (ulCapture_Exposure > Lines_10ms)
2797 //ulCapture_Exposure *= 1.7;
2798 ulCapture_Exposure /= Lines_10ms;
2799 ulCapture_Exposure *= Lines_10ms;
2802 ulCapture_Exposure = Capture_MaxLines;
2803 //ulCapture_Exposure_Gain *= 1.5;
2806 if(ulCapture_Exposure == 0)
2807 ulCapture_Exposure = 1;
2809 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2810 iCapture_Gain = iCapture_Gain & 0xffff;
2812 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2813 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2814 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2818 if (iCapture_Gain > 31) {
2820 iCapture_Gain = iCapture_Gain >> 1;
2822 if (iCapture_Gain > 31) {
2824 iCapture_Gain = iCapture_Gain >> 1;
2826 if (iCapture_Gain > 31) {
2828 iCapture_Gain = iCapture_Gain >> 1;
2830 if (iCapture_Gain > 31) {
2832 iCapture_Gain = iCapture_Gain >> 1;
2834 if (iCapture_Gain > 16)
2835 Gain |= ((iCapture_Gain -16) & 0x0f);
2838 // write the gain and exposure to 0x350* registers
2839 //m_iWrite0x350b=Gain;
2840 sensor_write(client,0x350b, Gain);
2841 Gain = (Gain >> 8) & 0x01;
2842 sensor_write(client,0x350a, Gain);
2843 //m_iWrite0x3502=ExposureLow;
2844 sensor_write(client,0x3502, ExposureLow);
2845 //m_iWrite0x3501=ExposureMid;
2846 sensor_write(client,0x3501, ExposureMid);
2847 //m_iWrite0x3500=ExposureHigh;
2848 sensor_write(client,0x3500, ExposureHigh);
2849 // SendToFile("Gain = 0x%x\r\n", Gain);
2850 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2851 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2852 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2853 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2854 //camera_timed_wait(200);
2855 //linzhk camera_timed_wait(500);
2857 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2858 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2859 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2860 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2861 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2862 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2864 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2868 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2870 struct soc_camera_link *icl = to_soc_camera_link(icd);
2873 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2877 case Sensor_PowerDown:
2879 if (icl->powerdown) {
2880 ret = icl->powerdown(icd->pdev, on);
2881 if (ret == RK29_CAM_IO_SUCCESS) {
2885 icl->reset(icd->pdev);
2887 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2889 goto sensor_power_end;
2896 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2897 struct sensor *sensor = to_sensor(client);
2899 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2900 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2902 //flash off after 2 secs
2903 hrtimer_cancel(&(flash_off_timer.timer));
2904 hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
2911 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2919 #if CONFIG_SENSOR_Flash
2920 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
2921 struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
2922 sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
2923 SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
2928 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2930 struct i2c_client *client = v4l2_get_subdevdata(sd);
2931 struct soc_camera_device *icd = client->dev.platform_data;
2932 struct sensor *sensor = to_sensor(client);
2933 const struct v4l2_queryctrl *qctrl;
2934 const struct sensor_datafmt *fmt;
2938 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2940 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2942 goto sensor_INIT_ERR;
2944 /* ddl@rock-chips.com : ov3640_powerOn have hardware reset */
2947 if (sensor_task_lock(client,1)<0)
2948 goto sensor_INIT_ERR;
2949 ret = sensor_write(client, 0x3012, 0x80);
2952 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2954 goto sensor_INIT_ERR;
2957 mdelay(5); //delay 5 microseconds
2959 /* check if it is an sensor sensor */
2960 ret = sensor_read(client, 0x300a, &value);
2962 SENSOR_TR("read chip id high byte failed\n");
2964 goto sensor_INIT_ERR;
2967 pid |= (value << 8);
2969 ret = sensor_read(client, 0x300b, &value);
2971 SENSOR_TR("read chip id low byte failed\n");
2973 goto sensor_INIT_ERR;
2976 pid |= (value & 0xff);
2977 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2978 if (pid == SENSOR_ID) {
2979 sensor->model = SENSOR_V4L2_IDENT;
2981 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2983 goto sensor_INIT_ERR;
2986 ret = sensor_write_array(client, sensor_init_data);
2989 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2990 goto sensor_INIT_ERR;
2992 sensor_task_lock(client,0);
2994 sensor->info_priv.winseqe_cur_addr = (struct reginfo *)SENSOR_INIT_WINSEQADR;
2995 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2997 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2999 goto sensor_INIT_ERR;
3001 sensor->info_priv.fmt = *fmt;
3003 /* sensor sensor information for initialization */
3004 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3006 sensor->info_priv.whiteBalance = qctrl->default_value;
3007 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
3009 sensor->info_priv.brightness = qctrl->default_value;
3010 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3012 sensor->info_priv.effect = qctrl->default_value;
3013 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
3015 sensor->info_priv.exposure = qctrl->default_value;
3017 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
3019 sensor->info_priv.saturation = qctrl->default_value;
3020 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
3022 sensor->info_priv.contrast = qctrl->default_value;
3023 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
3025 sensor->info_priv.mirror = qctrl->default_value;
3026 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
3028 sensor->info_priv.flip = qctrl->default_value;
3029 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
3031 sensor->info_priv.scene = qctrl->default_value;
3032 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3034 sensor->info_priv.digitalzoom = qctrl->default_value;
3036 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
3037 #if CONFIG_SENSOR_Focus
3038 // sensor_set_focus();
3039 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);//?,JYK
3041 sensor->info_priv.focus = qctrl->default_value;
3044 #if CONFIG_SENSOR_Flash
3045 //sensor_set_flash();
3046 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
3048 sensor->info_priv.flash = qctrl->default_value;
3049 flash_off_timer.icd = icd;
3050 flash_off_timer.timer.function = flash_off_func;
3053 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);
3055 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
3058 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
3059 sensor_task_lock(client,0);
3060 sensor_deactivate(client);
3063 static int sensor_deactivate(struct i2c_client *client)
3065 struct soc_camera_device *icd = client->dev.platform_data;
3066 struct sensor *sensor = to_sensor(client);
3068 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
3070 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
3071 sensor_ioctrl(icd, Sensor_PowerDown, 1);
3073 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
3074 icd->user_width = SENSOR_INIT_WIDTH;
3075 icd->user_height = SENSOR_INIT_HEIGHT;
3077 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
3080 static struct reginfo sensor_power_down_sequence[]=
3089 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
3092 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3094 if (pm_msg.event == PM_EVENT_SUSPEND) {
3095 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
3096 ret = sensor_write_array(client, sensor_power_down_sequence) ;
3098 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
3101 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
3103 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
3108 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
3115 static int sensor_resume(struct soc_camera_device *icd)
3119 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
3121 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
3125 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
3129 static int sensor_set_bus_param(struct soc_camera_device *icd,
3130 unsigned long flags)
3136 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
3138 struct soc_camera_link *icl = to_soc_camera_link(icd);
3139 unsigned long flags = SENSOR_BUS_PARAM;
3141 return soc_camera_apply_sensor_flags(icl, flags);
3144 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3146 struct i2c_client *client = v4l2_get_subdevdata(sd);
3147 struct soc_camera_device *icd = client->dev.platform_data;
3148 struct sensor *sensor = to_sensor(client);
3150 mf->width = icd->user_width;
3151 mf->height = icd->user_height;
3152 mf->code = sensor->info_priv.fmt.code;
3153 mf->colorspace = sensor->info_priv.fmt.colorspace;
3154 mf->field = V4L2_FIELD_NONE;
3158 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3162 if ((mf->width == 1024) && (mf->height == 768)) {
3164 } else if ((mf->width == 1280) && (mf->height == 1024)) {
3166 } else if ((mf->width == 1600) && (mf->height == 1200)) {
3168 } else if ((mf->width == 2048) && (mf->height == 1536)) {
3170 } else if ((mf->width == 2592) && (mf->height == 1944)) {
3175 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
3179 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3183 if ((mf->width == 1280) && (mf->height == 720)) {
3185 } else if ((mf->width == 1920) && (mf->height == 1080)) {
3190 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
3193 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3195 struct i2c_client *client = v4l2_get_subdevdata(sd);
3196 const struct sensor_datafmt *fmt;
3197 struct sensor *sensor = to_sensor(client);
3198 const struct v4l2_queryctrl *qctrl;
3199 struct soc_camera_device *icd = client->dev.platform_data;
3200 struct reginfo *winseqe_set_addr=NULL;
3201 int ret = 0, set_w,set_h;
3204 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
3205 ARRAY_SIZE(sensor_colour_fmts));
3208 goto sensor_s_fmt_end;
3211 if (sensor->info_priv.fmt.code != mf->code) {
3214 case V4L2_MBUS_FMT_YUYV8_2X8:
3216 winseqe_set_addr = sensor_ClrFmt_YUYV;
3219 case V4L2_MBUS_FMT_UYVY8_2X8:
3221 winseqe_set_addr = sensor_ClrFmt_UYVY;
3227 if (winseqe_set_addr != NULL) {
3228 sensor_write_array(client, winseqe_set_addr);
3229 sensor->info_priv.fmt.code = mf->code;
3230 sensor->info_priv.fmt.colorspace= mf->colorspace;
3231 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
3233 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
3239 isCapture = sensor_fmt_capturechk(sd, mf);
3241 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[isCapture][0].reg)
3243 winseqe_set_addr = sensor_qcif[isCapture];
3247 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[isCapture][0].reg)
3249 winseqe_set_addr = sensor_qvga[isCapture];
3253 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[isCapture][0].reg)
3255 winseqe_set_addr = sensor_cif[isCapture];
3259 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[isCapture][0].reg)
3261 winseqe_set_addr = sensor_vga[isCapture];
3265 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[isCapture][0].reg)
3267 winseqe_set_addr = sensor_svga[isCapture];
3271 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_sxga[isCapture][0].reg)
3273 winseqe_set_addr = sensor_xga[isCapture];
3277 else if (((set_w <= 1280) && (set_h <= 960)) && sensor_sxga[isCapture][0].reg)
3279 winseqe_set_addr = sensor_sxga[isCapture];
3283 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[isCapture][0].reg)
3285 winseqe_set_addr = sensor_uxga[isCapture];
3289 else if (((set_w <= 2048) && (set_h <= 1536)) && sensor_uxga[isCapture][0].reg)
3291 winseqe_set_addr = sensor_qxga[isCapture];
3297 winseqe_set_addr = (struct reginfo *)SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
3298 set_w = SENSOR_INIT_WIDTH;
3299 set_h = SENSOR_INIT_HEIGHT;
3300 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
3303 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
3305 //srt --if capture,then should write sensor_qxga[1] first
3306 if((winseqe_set_addr != sensor_qxga[isCapture]) && isCapture) {
3307 SENSOR_DG("%s write sensor_qxga[1]\n", SENSOR_NAME_STRING());
3308 ret = sensor_write_array(client, sensor_qxga[isCapture]);
3310 SENSOR_TR("%s write sensor_qxga[1] failed\n", SENSOR_NAME_STRING());
3314 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
3315 sensor_parameter_record(client);
3316 /*#if CONFIG_SENSOR_Focus
3317 sensor_af_idlechk(client);
3318 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
3319 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
3321 #if CONFIG_SENSOR_Flash
3322 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
3323 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
3324 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
3327 }else { /* ddl@rock-chips.com : Video */
3328 #if CONFIG_SENSOR_Flash
3329 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
3330 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3331 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
3335 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
3336 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
3337 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
3338 ret |= sensor_write_array(client,sensor_init_data);
3339 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
3342 ret |= sensor_write_array(client, winseqe_set_addr);
3344 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
3345 #if CONFIG_SENSOR_Flash
3346 if (sensor_fmt_capturechk(sd,mf) == true) {
3347 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
3348 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3349 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
3353 goto sensor_s_fmt_end;
3355 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
3357 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
3358 sensor_ae_transfer(client);
3359 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3360 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3361 if (sensor->info_priv.whiteBalance != 0) {
3362 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3363 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3365 sensor->info_priv.snap2preview = true;
3366 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
3367 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3368 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3370 sensor->info_priv.video2preview = true;
3371 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
3372 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3373 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3374 if (sensor->info_priv.snap2preview == true) {
3375 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3376 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3378 #if CONFIG_SENSOR_Focus
3379 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
3380 sensor_af_workqueue_set(icd,WqCmd_af_return_idle,0,true);
3388 sensor->info_priv.video2preview = false;
3389 sensor->info_priv.snap2preview = false;
3391 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
3395 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
3403 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3405 struct i2c_client *client = v4l2_get_subdevdata(sd);
3406 struct sensor *sensor = to_sensor(client);
3407 const struct sensor_datafmt *fmt;
3408 int ret = 0,set_w,set_h;
3410 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
3411 ARRAY_SIZE(sensor_colour_fmts));
3413 fmt = &sensor->info_priv.fmt;
3414 mf->code = fmt->code;
3417 if (mf->height > SENSOR_MAX_HEIGHT)
3418 mf->height = SENSOR_MAX_HEIGHT;
3419 else if (mf->height < SENSOR_MIN_HEIGHT)
3420 mf->height = SENSOR_MIN_HEIGHT;
3422 if (mf->width > SENSOR_MAX_WIDTH)
3423 mf->width = SENSOR_MAX_WIDTH;
3424 else if (mf->width < SENSOR_MIN_WIDTH)
3425 mf->width = SENSOR_MIN_WIDTH;
3429 isCapture = sensor_fmt_capturechk(sd, mf);
3430 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[isCapture][0].reg!=SEQUENCE_END))
3435 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[isCapture][0].reg!=SEQUENCE_END))
3440 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[isCapture][0].reg!=SEQUENCE_END))
3445 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[isCapture][0].reg!=SEQUENCE_END))
3450 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[isCapture][0].reg!=SEQUENCE_END))
3455 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[isCapture][0].reg!=SEQUENCE_END))
3460 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
3465 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[isCapture][0].reg!=SEQUENCE_END))
3470 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[isCapture][0].reg!=SEQUENCE_END))
3475 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
3480 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[isCapture][0].reg!=SEQUENCE_END))
3485 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
3492 set_w = SENSOR_INIT_WIDTH;
3493 set_h = SENSOR_INIT_HEIGHT;
3499 mf->colorspace = fmt->colorspace;
3504 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
3506 struct i2c_client *client = v4l2_get_subdevdata(sd);
3508 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
3511 if (id->match.addr != client->addr)
3514 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV3640 identifier */
3519 #if CONFIG_SENSOR_Brightness
3520 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3522 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3524 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3526 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
3528 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
3530 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3533 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3537 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3541 #if CONFIG_SENSOR_Effect
3542 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3544 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3546 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3548 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
3550 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
3552 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3555 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3559 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3563 #if CONFIG_SENSOR_Exposure
3564 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3566 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3568 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3570 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
3572 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3574 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3577 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3581 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3585 #if CONFIG_SENSOR_Saturation
3586 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3588 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3590 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3592 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3594 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3596 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3599 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3603 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3607 #if CONFIG_SENSOR_Contrast
3608 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3610 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3612 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3614 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3616 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3618 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3621 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3625 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3629 #if CONFIG_SENSOR_Mirror
3630 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3632 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3634 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3636 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3638 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3640 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3643 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3647 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3651 #if CONFIG_SENSOR_Flip
3652 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3654 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3656 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3658 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3660 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3662 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3665 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3669 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3673 #if CONFIG_SENSOR_Scene
3674 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3676 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3678 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3680 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3682 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3684 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3687 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3691 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3695 #if CONFIG_SENSOR_WhiteBalance
3696 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3698 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3700 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3702 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3704 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3706 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3709 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3713 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3717 #if CONFIG_SENSOR_DigitalZoom
3718 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3720 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3721 struct sensor *sensor = to_sensor(client);
3722 const struct v4l2_queryctrl *qctrl_info;
3723 int digitalzoom_cur, digitalzoom_total;
3725 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3729 digitalzoom_cur = sensor->info_priv.digitalzoom;
3730 digitalzoom_total = qctrl_info->maximum;
3732 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3734 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3738 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3740 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3744 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3746 *value = digitalzoom_total - digitalzoom_cur;
3749 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3751 *value = 0 - digitalzoom_cur;
3754 digitalzoom_cur += *value;
3756 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3758 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3760 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3763 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3771 #if CONFIG_SENSOR_Focus
3773 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3775 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3776 struct sensor *sensor = to_sensor(client);
3777 const struct v4l2_queryctrl *qctrl_info;
3780 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3784 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3785 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3786 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true);
3787 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3790 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3794 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3795 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3801 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3803 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3804 struct sensor *sensor = to_sensor(client);
3805 const struct v4l2_queryctrl *qctrl_info;
3808 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3812 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3813 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3815 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true);
3817 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true);
3819 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3822 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3826 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3827 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3832 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3834 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3835 struct sensor *sensor = to_sensor(client);
3838 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3841 case SENSOR_AF_MODE_AUTO:
3843 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true);
3847 case SENSOR_AF_MODE_MACRO:
3849 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3853 case SENSOR_AF_MODE_INFINITY:
3855 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3859 case SENSOR_AF_MODE_CONTINUOUS:
3861 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true);
3865 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3870 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3873 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3874 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3880 #if CONFIG_SENSOR_Flash
3881 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3883 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3884 if (value == 3) { /* ddl@rock-chips.com: torch */
3885 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3887 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3889 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3893 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3897 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3899 struct i2c_client *client = v4l2_get_subdevdata(sd);
3900 struct sensor *sensor = to_sensor(client);
3901 const struct v4l2_queryctrl *qctrl;
3903 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3907 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3913 case V4L2_CID_BRIGHTNESS:
3915 ctrl->value = sensor->info_priv.brightness;
3918 case V4L2_CID_SATURATION:
3920 ctrl->value = sensor->info_priv.saturation;
3923 case V4L2_CID_CONTRAST:
3925 ctrl->value = sensor->info_priv.contrast;
3928 case V4L2_CID_DO_WHITE_BALANCE:
3930 ctrl->value = sensor->info_priv.whiteBalance;
3933 case V4L2_CID_EXPOSURE:
3935 ctrl->value = sensor->info_priv.exposure;
3938 case V4L2_CID_HFLIP:
3940 ctrl->value = sensor->info_priv.mirror;
3943 case V4L2_CID_VFLIP:
3945 ctrl->value = sensor->info_priv.flip;
3956 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3958 struct i2c_client *client = v4l2_get_subdevdata(sd);
3959 struct sensor *sensor = to_sensor(client);
3960 struct soc_camera_device *icd = client->dev.platform_data;
3961 const struct v4l2_queryctrl *qctrl;
3964 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3968 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3974 #if CONFIG_SENSOR_Brightness
3975 case V4L2_CID_BRIGHTNESS:
3977 if (ctrl->value != sensor->info_priv.brightness)
3979 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3983 sensor->info_priv.brightness = ctrl->value;
3988 #if CONFIG_SENSOR_Exposure
3989 case V4L2_CID_EXPOSURE:
3991 if (ctrl->value != sensor->info_priv.exposure)
3993 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3997 sensor->info_priv.exposure = ctrl->value;
4002 #if CONFIG_SENSOR_Saturation
4003 case V4L2_CID_SATURATION:
4005 if (ctrl->value != sensor->info_priv.saturation)
4007 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
4011 sensor->info_priv.saturation = ctrl->value;
4016 #if CONFIG_SENSOR_Contrast
4017 case V4L2_CID_CONTRAST:
4019 if (ctrl->value != sensor->info_priv.contrast)
4021 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
4025 sensor->info_priv.contrast = ctrl->value;
4030 #if CONFIG_SENSOR_WhiteBalance
4031 case V4L2_CID_DO_WHITE_BALANCE:
4033 if (ctrl->value != sensor->info_priv.whiteBalance)
4035 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
4039 sensor->info_priv.whiteBalance = ctrl->value;
4044 #if CONFIG_SENSOR_Mirror
4045 case V4L2_CID_HFLIP:
4047 if (ctrl->value != sensor->info_priv.mirror)
4049 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
4051 sensor->info_priv.mirror = ctrl->value;
4056 #if CONFIG_SENSOR_Flip
4057 case V4L2_CID_VFLIP:
4059 if (ctrl->value != sensor->info_priv.flip)
4061 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
4063 sensor->info_priv.flip = ctrl->value;
4074 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
4076 const struct v4l2_queryctrl *qctrl;
4077 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4078 struct sensor *sensor = to_sensor(client);
4080 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
4084 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
4088 switch (ext_ctrl->id)
4090 case V4L2_CID_SCENE:
4092 ext_ctrl->value = sensor->info_priv.scene;
4095 case V4L2_CID_EFFECT:
4097 ext_ctrl->value = sensor->info_priv.effect;
4100 case V4L2_CID_ZOOM_ABSOLUTE:
4102 ext_ctrl->value = sensor->info_priv.digitalzoom;
4105 case V4L2_CID_ZOOM_RELATIVE:
4109 case V4L2_CID_FOCUS_ABSOLUTE:
4113 case V4L2_CID_FOCUS_RELATIVE:
4117 case V4L2_CID_FLASH:
4119 ext_ctrl->value = sensor->info_priv.flash;
4127 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
4129 const struct v4l2_queryctrl *qctrl;
4130 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4131 struct sensor *sensor = to_sensor(client);
4134 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
4138 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
4143 switch (ext_ctrl->id)
4145 #if CONFIG_SENSOR_Scene
4146 case V4L2_CID_SCENE:
4148 if (ext_ctrl->value != sensor->info_priv.scene)
4150 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
4152 sensor->info_priv.scene = ext_ctrl->value;
4157 #if CONFIG_SENSOR_Effect
4158 case V4L2_CID_EFFECT:
4160 if (ext_ctrl->value != sensor->info_priv.effect)
4162 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
4164 sensor->info_priv.effect= ext_ctrl->value;
4169 #if CONFIG_SENSOR_DigitalZoom
4170 case V4L2_CID_ZOOM_ABSOLUTE:
4172 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
4175 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
4177 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
4179 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
4181 sensor->info_priv.digitalzoom += val_offset;
4183 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
4188 case V4L2_CID_ZOOM_RELATIVE:
4190 if (ext_ctrl->value)
4192 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
4194 sensor->info_priv.digitalzoom += ext_ctrl->value;
4196 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
4201 #if CONFIG_SENSOR_Focus
4202 case V4L2_CID_FOCUS_ABSOLUTE:
4204 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
4209 case V4L2_CID_FOCUS_RELATIVE:
4211 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
4214 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
4217 case V4L2_CID_FOCUS_AUTO:
4219 if (ext_ctrl->value == 1) {
4220 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
4221 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
4222 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
4226 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
4227 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
4228 if (ext_ctrl->value == 0)
4229 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
4233 case V4L2_CID_FOCUS_CONTINUOUS:
4235 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
4236 if (ext_ctrl->value == 1) {
4237 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
4239 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
4242 if (ext_ctrl->value == 0)
4243 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
4248 #if CONFIG_SENSOR_Flash
4249 case V4L2_CID_FLASH:
4251 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
4253 sensor->info_priv.flash = ext_ctrl->value;
4255 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
4266 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
4268 struct i2c_client *client = v4l2_get_subdevdata(sd);
4269 struct soc_camera_device *icd = client->dev.platform_data;
4270 int i, error_cnt=0, error_idx=-1;
4273 for (i=0; i<ext_ctrl->count; i++) {
4274 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
4281 error_idx = ext_ctrl->count;
4283 if (error_idx != -1) {
4284 ext_ctrl->error_idx = error_idx;
4291 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
4293 struct i2c_client *client = v4l2_get_subdevdata(sd);
4294 struct soc_camera_device *icd = client->dev.platform_data;
4295 int i, error_cnt=0, error_idx=-1;
4298 for (i=0; i<ext_ctrl->count; i++) {
4299 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
4306 error_idx = ext_ctrl->count;
4308 if (error_idx != -1) {
4309 ext_ctrl->error_idx = error_idx;
4316 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
4318 struct i2c_client *client = v4l2_get_subdevdata(sd);
4319 struct sensor *sensor = to_sensor(client);
4320 #if CONFIG_SENSOR_Focus
4321 struct soc_camera_device *icd = client->dev.platform_data;
4322 struct v4l2_mbus_framefmt mf;
4326 sensor->info_priv.enable = 1;
4327 #if CONFIG_SENSOR_Focus
4328 mf.width = icd->user_width;
4329 mf.height = icd->user_height;
4330 mf.code = sensor->info_priv.fmt.code;
4331 mf.colorspace = sensor->info_priv.fmt.colorspace;
4332 mf.field = V4L2_FIELD_NONE;
4333 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
4334 if (sensor_fmt_capturechk(sd, &mf) == false) {
4335 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
4336 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false);
4337 sensor->info_priv.affm_reinit = 0;
4341 } else if (enable == 0) {
4342 sensor->info_priv.enable = 0;
4343 #if CONFIG_SENSOR_Focus
4344 flush_workqueue(sensor->sensor_wq);
4350 /* Interface active, can use i2c. If it fails, it can indeed mean, that
4351 * this wasn't our capture interface, so, we wait for the right one */
4352 static int sensor_video_probe(struct soc_camera_device *icd,
4353 struct i2c_client *client)
4357 struct sensor *sensor = to_sensor(client);
4359 /* We must have a parent by now. And it cannot be a wrong one.
4360 * So this entire test is completely redundant. */
4361 if (!icd->dev.parent ||
4362 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
4365 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
4367 goto sensor_video_probe_err;
4370 ret = sensor_write(client, 0x3012, 0x80);
4372 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
4374 goto sensor_video_probe_err;
4376 mdelay(5); //delay 5 microseconds
4378 /* check if it is an sensor sensor */
4379 ret = sensor_read(client, 0x300a, &value);
4381 SENSOR_TR("read chip id high byte failed\n");
4383 goto sensor_video_probe_err;
4386 pid |= (value << 8);
4388 ret = sensor_read(client, 0x300b, &value);
4390 SENSOR_TR("read chip id low byte failed\n");
4392 goto sensor_video_probe_err;
4395 pid |= (value & 0xff);
4396 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
4397 if (pid == SENSOR_ID) {
4398 sensor->model = SENSOR_V4L2_IDENT;
4400 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
4402 goto sensor_video_probe_err;
4406 sensor_video_probe_err:
4409 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
4411 struct i2c_client *client = v4l2_get_subdevdata(sd);
4412 struct soc_camera_device *icd = client->dev.platform_data;
4413 struct sensor *sensor = to_sensor(client);
4416 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4419 case RK29_CAM_SUBDEV_DEACTIVATE:
4421 sensor_deactivate(client);
4424 case RK29_CAM_SUBDEV_IOREQUEST:
4426 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
4427 if (sensor->sensor_io_request != NULL) {
4428 sensor->sensor_gpio_res = NULL;
4429 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
4430 if (sensor->sensor_io_request->gpio_res[i].dev_name &&
4431 (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
4432 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
4435 if (sensor->sensor_gpio_res == NULL) {
4436 SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
4438 goto sensor_ioctl_end;
4441 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
4443 goto sensor_ioctl_end;
4445 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
4447 #if CONFIG_SENSOR_Flash
4448 if (sensor->sensor_gpio_res) {
4449 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
4450 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
4451 for (i = 0; i < icd->ops->num_controls; i++) {
4452 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
4453 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
4454 sensor_controls[i].id=0xffff;
4457 sensor->info_priv.flash = 0xff;
4458 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
4459 }else{ //two cameras are the same,need to deal diffrently ,zyc
4460 for (i = 0; i < icd->ops->num_controls; i++) {
4461 if(0xffff == icd->ops->controls[i].id){
4462 sensor_controls[i].id=V4L2_CID_FLASH;
4472 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4481 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
4482 enum v4l2_mbus_pixelcode *code)
4484 if (index >= ARRAY_SIZE(sensor_colour_fmts))
4487 *code = sensor_colour_fmts[index].code;
4490 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
4491 .init = sensor_init,
4492 .g_ctrl = sensor_g_control,
4493 .s_ctrl = sensor_s_control,
4494 .g_ext_ctrls = sensor_g_ext_controls,
4495 .s_ext_ctrls = sensor_s_ext_controls,
4496 .g_chip_ident = sensor_g_chip_ident,
4497 .ioctl = sensor_ioctl,
4500 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
4501 .s_mbus_fmt = sensor_s_fmt,
4502 .g_mbus_fmt = sensor_g_fmt,
4503 .try_mbus_fmt = sensor_try_fmt,
4504 .enum_mbus_fmt = sensor_enum_fmt,
4505 .s_stream = sensor_s_stream,
4508 static struct v4l2_subdev_ops sensor_subdev_ops = {
4509 .core = &sensor_subdev_core_ops,
4510 .video = &sensor_subdev_video_ops,
4513 static int sensor_probe(struct i2c_client *client,
4514 const struct i2c_device_id *did)
4516 struct sensor *sensor;
4517 struct soc_camera_device *icd = client->dev.platform_data;
4518 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
4519 struct soc_camera_link *icl;
4522 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
4524 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
4528 icl = to_soc_camera_link(icd);
4530 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
4534 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
4535 dev_warn(&adapter->dev,
4536 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
4540 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
4544 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
4546 /* Second stage probe - when a capture adapter is there */
4547 icd->ops = &sensor_ops;
4548 sensor->info_priv.fmt = sensor_colour_fmts[0];
4549 #if CONFIG_SENSOR_I2C_NOSCHED
4550 atomic_set(&sensor->tasklock_cnt,0);
4553 ret = sensor_video_probe(icd, client);
4556 i2c_set_clientdata(client, NULL);
4560 #if CONFIG_SENSOR_Focus
4561 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
4562 if (sensor->sensor_wq == NULL)
4563 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
4564 mutex_init(&sensor->wq_lock);
4567 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4568 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
4572 static int sensor_remove(struct i2c_client *client)
4574 struct sensor *sensor = to_sensor(client);
4575 struct soc_camera_device *icd = client->dev.platform_data;
4577 #if CONFIG_SENSOR_Focus
4578 if (sensor->sensor_wq) {
4579 destroy_workqueue(sensor->sensor_wq);
4580 sensor->sensor_wq = NULL;
4585 i2c_set_clientdata(client, NULL);
4586 client->driver = NULL;
4592 static const struct i2c_device_id sensor_id[] = {
4593 {SENSOR_NAME_STRING(), 0 },
4596 MODULE_DEVICE_TABLE(i2c, sensor_id);
4598 static struct i2c_driver sensor_i2c_driver = {
4600 .name = SENSOR_NAME_STRING(),
4602 .probe = sensor_probe,
4603 .remove = sensor_remove,
4604 .id_table = sensor_id,
4607 static int __init sensor_mod_init(void)
4609 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4610 return i2c_add_driver(&sensor_i2c_driver);
4613 static void __exit sensor_mod_exit(void)
4615 i2c_del_driver(&sensor_i2c_driver);
4618 device_initcall_sync(sensor_mod_init);
4619 module_exit(sensor_mod_exit);
4621 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4622 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4623 MODULE_LICENSE("GPL");