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 static enum hrtimer_restart flash_off_func(struct hrtimer *timer);
180 static struct flash_timer flash_off_timer;
181 //for user defined if user want to customize the series , zyc
182 #ifdef CONFIG_OV5640_USER_DEFINED_SERIES
183 #include "ov5640_user_series.c"
185 /* init VGA 640*480 */
186 static struct reginfo sensor_init_data[] =
198 {0x30b2, 0x18}, // by FAE
211 {0x30a9, 0xbd},//disable internal DVDD, by FAE.
264 {0x3404, 0x00},//YUV, sequence
341 #ifdef CONFIG_RK29_NEWTON
342 {0x307c, 0x10}, // flip && mirror ,for newton
346 //{0x307c, 0x13}, // flip && mirror
347 {0x307c, 0x10}, // flip && mirror ,by FAE.
378 {0x3089, 0x88},// 0x80, by FAE.
380 {0x308b, 0xe4},// 0xe0, by FAE.
381 {0x304c, 0x84}, //0x83, by FAE.
387 static struct reginfo sensor_qxga_preview[] =
429 //[Sensor.YUV.2048x1536] XGA->QXGA
467 static struct reginfo sensor_qxga_capture[] = {
509 static struct reginfo *sensor_qxga[2] = {
515 static struct reginfo sensor_720p[] = {
521 static struct reginfo sensor_uxga_preview[] =
557 {0x304c, 0x81},//56Mhz PCLK output
561 static struct reginfo sensor_uxga_capture[] =
575 {0x304c, 0x81},//56Mhz PCLK output
579 static struct reginfo *sensor_uxga[2] = {
585 static struct reginfo sensor_sxga_preview[] =
621 {0x304c, 0x81},//56Mhz PCLK output
625 /* 1080p, 0x15fps, 0xyuv @1920x1080 */
627 static struct reginfo sensor_1080p[]=
632 static struct reginfo sensor_sxga_capture[] =
646 {0x304c, 0x81},//56Mhz PCLK output
650 static struct reginfo *sensor_sxga[2] = {
655 /* 2592X1944 QSXGA */
656 static struct reginfo sensor_qsxga[] =
661 static struct reginfo sensor_xga_preview[] =
693 {0x304c, 0x82},//28Mhz PCLK output
698 static struct reginfo sensor_xga_capture[] =
712 {0x304c, 0x82},//28Mhz PCLK output
716 static struct reginfo *sensor_xga[2] = {
722 static struct reginfo sensor_svga_preview[] =
754 {0x304c, 0x83},//28Mhz PCLK output
758 static struct reginfo sensor_svga_capture[] =
772 {0x304c, 0x82},//28Mhz PCLK output
776 static struct reginfo * sensor_svga[2] = {
782 static struct reginfo sensor_vga_preview[] =
811 {0x3089, 0x88},// 0x80, by FAE.
813 {0x308b, 0xe4},// 0xe0, by FAE.
814 {0x304c, 0x84}, //0x83, by FAE.
832 static struct reginfo sensor_vga_capture[] =
846 {0x304c, 0x82},//14Mhz PCLK output 84
850 static struct reginfo *sensor_vga[2] = {
856 static struct reginfo sensor_cif_preview[] =
908 static struct reginfo sensor_cif_capture[] =
922 {0x304c, 0x84},//14Mhz PCLK output
926 static struct reginfo *sensor_cif[2] = {
932 static struct reginfo sensor_qvga_preview[] =
964 {0x304c, 0x89},//14Mhz PCLK output
968 static struct reginfo sensor_qvga_capture[] =
982 {0x304c, 0x84},//14Mhz PCLK output
986 static struct reginfo *sensor_qvga[2] = {
992 static struct reginfo sensor_qcif_preview[] =
1024 {0x304c, 0x82},//14Mhz PCLK output 89
1028 static struct reginfo sensor_qcif_capture[] =
1042 {0x304c, 0x84},//14Mhz PCLK output
1046 static struct reginfo *sensor_qcif[2] = {
1047 sensor_qcif_preview,
1048 sensor_qcif_capture,
1054 static struct reginfo ov3640_Sharpness_auto[] =
1059 static struct reginfo ov3640_Sharpness1[] =
1065 static struct reginfo ov3640_Sharpness2[][3] =
1072 static struct reginfo ov3640_Sharpness3[] =
1078 static struct reginfo ov3640_Sharpness4[]=
1085 static struct reginfo ov3640_Sharpness5[] =
1093 static struct reginfo sensor_ClrFmt_YUYV[]=
1099 static struct reginfo sensor_ClrFmt_UYVY[]=
1105 #if CONFIG_SENSOR_WhiteBalance
1106 static struct reginfo sensor_WhiteB_Auto[]=
1108 {0x332b, 0x00},//AWB auto, bit[3]:0,auto
1112 /* Cloudy Colour Temperature : 6500K - 8000K */
1113 static struct reginfo sensor_WhiteB_Cloudy[]=
1122 /* ClearDay Colour Temperature : 5000K - 6500K */
1123 static struct reginfo sensor_WhiteB_ClearDay[]=
1126 {0x332b, 0x08}, //AWB off
1133 /* Office Colour Temperature : 3500K - 5000K */
1134 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
1144 /* Home Colour Temperature : 2500K - 3500K */
1145 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
1155 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
1156 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
1160 #if CONFIG_SENSOR_Brightness
1161 static struct reginfo sensor_Brightness0[]=
1172 static struct reginfo sensor_Brightness1[]=
1183 static struct reginfo sensor_Brightness2[]=
1194 static struct reginfo sensor_Brightness3[]=
1205 static struct reginfo sensor_Brightness4[]=
1216 static struct reginfo sensor_Brightness5[]=
1227 static struct reginfo sensor_Brightness6[]=
1231 {0x3355, 0x04}, //bit[2] enable
1232 {0x3354, 0x01}, //bit[3] sign of brightness
1238 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
1239 sensor_Brightness4, sensor_Brightness5,sensor_Brightness6,NULL,
1244 #if CONFIG_SENSOR_Effect
1245 static struct reginfo sensor_Effect_Normal[] =
1254 static struct reginfo sensor_Effect_WandB[] =
1258 {0x3355, 0x18}, //bit[4]fix u enable, bit[3]fix v enable
1265 static struct reginfo sensor_Effect_Sepia[] =
1276 static struct reginfo sensor_Effect_Negative[] =
1280 {0x3355, 0x40}, //bit[6] negative
1284 static struct reginfo sensor_Effect_Bluish[] =
1295 static struct reginfo sensor_Effect_Green[] =
1306 static struct reginfo sensor_Effect_Red[] =
1316 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
1317 sensor_Effect_Bluish, sensor_Effect_Green,sensor_Effect_Red,NULL,
1320 #if CONFIG_SENSOR_Exposure
1321 static struct reginfo sensor_Exposure0[]=
1332 static struct reginfo sensor_Exposure1[]=
1343 static struct reginfo sensor_Exposure2[]=
1354 static struct reginfo sensor_Exposure3[]=
1365 static struct reginfo sensor_Exposure4[]=
1376 static struct reginfo sensor_Exposure5[]=
1387 static struct reginfo sensor_Exposure6[]=
1398 static struct reginfo sensor_Exposure7[]=
1409 static struct reginfo sensor_Exposure8[]=
1420 static struct reginfo sensor_Exposure9[]=
1431 static struct reginfo sensor_Exposure10[]=
1442 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1443 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,sensor_Exposure7,sensor_Exposure8,sensor_Exposure9,
1444 sensor_Exposure10,NULL,
1447 #if CONFIG_SENSOR_Saturation
1448 static struct reginfo sensor_Saturation0[]=
1459 static struct reginfo sensor_Saturation1[]=
1470 static struct reginfo sensor_Saturation2[]=
1481 static struct reginfo sensor_Saturation3[]=
1492 static struct reginfo sensor_Saturation4[]=
1495 {0x3302, 0xef}, //bit[7]:1, enable SDE
1496 {0x3355, 0x02}, //enable color saturation
1503 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2,
1504 sensor_Saturation3, sensor_Saturation4, NULL,};
1507 #if CONFIG_SENSOR_Contrast
1508 static struct reginfo sensor_Contrast0[]=
1520 static struct reginfo sensor_Contrast1[]=
1532 static struct reginfo sensor_Contrast2[]=
1544 static struct reginfo sensor_Contrast3[]=
1556 static struct reginfo sensor_Contrast4[]=
1569 static struct reginfo sensor_Contrast5[]=
1581 static struct reginfo sensor_Contrast6[]=
1585 {0x3355, 0x04}, //bit[2] enable contrast/brightness
1586 {0x3354, 0x01}, //bit[2] Yoffset sign
1592 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1593 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1597 #if CONFIG_SENSOR_Mirror
1598 static struct reginfo sensor_MirrorOn[]=
1600 {0x307c, 0x12}, //mirror
1607 static struct reginfo sensor_MirrorOff[]=
1609 {0x307c, 0x10},// no mirror/flip
1616 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1618 #if CONFIG_SENSOR_Flip
1619 static struct reginfo sensor_FlipOn[]=
1621 {0x307c, 0x11}, //flip
1628 static struct reginfo sensor_FlipOff[]=
1630 {0x307c, 0x10}, // no mirror/flip
1636 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1639 #if CONFIG_SENSOR_Scene
1640 static struct reginfo sensor_SceneAuto[] =
1642 {0x3014, 0x84},//bit[3]=0 disable auto night mode
1643 {0x3015, 0x12},// or 0x02
1644 {0x302d, 0x00},//clear dummy frame
1645 {0x302e, 0x00},//clear dummy frame
1650 static struct reginfo sensor_SceneNight[] =
1652 {0x3014, 0x8c}, //bit[3] =1 enable auto night mode ¸ù¾Ý¹âÏßÇ¿Èõ×Ô¶¯½µÖ¡
1653 {0x3015, 0x22},//bit[6:4] ÉèÖò»Í¬,½µµÄÖ¡Âʲ»Ò»Ñù.¿ÉÒÔ¿´datasheet
1655 //000 no dummy frame
1662 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1665 #if CONFIG_SENSOR_DigitalZoom
1666 static struct reginfo sensor_Zoom0[] =
1671 static struct reginfo sensor_Zoom1[] =
1676 static struct reginfo sensor_Zoom2[] =
1682 static struct reginfo sensor_Zoom3[] =
1686 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1688 static const struct v4l2_querymenu sensor_menus[] =
1690 #if CONFIG_SENSOR_WhiteBalance
1691 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1692 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1693 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1696 #if CONFIG_SENSOR_Effect
1697 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1698 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1699 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1702 #if CONFIG_SENSOR_Scene
1703 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1706 #if CONFIG_SENSOR_Flash
1707 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1708 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1712 static struct v4l2_queryctrl sensor_controls[] =
1714 #if CONFIG_SENSOR_WhiteBalance
1716 .id = V4L2_CID_DO_WHITE_BALANCE,
1717 .type = V4L2_CTRL_TYPE_MENU,
1718 .name = "White Balance Control",
1726 #if CONFIG_SENSOR_Brightness
1728 .id = V4L2_CID_BRIGHTNESS,
1729 .type = V4L2_CTRL_TYPE_INTEGER,
1730 .name = "Brightness Control",
1738 #if CONFIG_SENSOR_Effect
1740 .id = V4L2_CID_EFFECT,
1741 .type = V4L2_CTRL_TYPE_MENU,
1742 .name = "Effect Control",
1750 #if CONFIG_SENSOR_Exposure
1752 .id = V4L2_CID_EXPOSURE,
1753 .type = V4L2_CTRL_TYPE_INTEGER,
1754 .name = "Exposure Control",
1762 #if CONFIG_SENSOR_Saturation
1764 .id = V4L2_CID_SATURATION,
1765 .type = V4L2_CTRL_TYPE_INTEGER,
1766 .name = "Saturation Control",
1774 #if CONFIG_SENSOR_Contrast
1776 .id = V4L2_CID_CONTRAST,
1777 .type = V4L2_CTRL_TYPE_INTEGER,
1778 .name = "Contrast Control",
1786 #if CONFIG_SENSOR_Mirror
1788 .id = V4L2_CID_HFLIP,
1789 .type = V4L2_CTRL_TYPE_BOOLEAN,
1790 .name = "Mirror Control",
1798 #if CONFIG_SENSOR_Flip
1800 .id = V4L2_CID_VFLIP,
1801 .type = V4L2_CTRL_TYPE_BOOLEAN,
1802 .name = "Flip Control",
1810 #if CONFIG_SENSOR_Scene
1812 .id = V4L2_CID_SCENE,
1813 .type = V4L2_CTRL_TYPE_MENU,
1814 .name = "Scene Control",
1822 #if CONFIG_SENSOR_DigitalZoom
1824 .id = V4L2_CID_ZOOM_RELATIVE,
1825 .type = V4L2_CTRL_TYPE_INTEGER,
1826 .name = "DigitalZoom Control",
1832 .id = V4L2_CID_ZOOM_ABSOLUTE,
1833 .type = V4L2_CTRL_TYPE_INTEGER,
1834 .name = "DigitalZoom Control",
1842 #if CONFIG_SENSOR_Focus
1844 .id = V4L2_CID_FOCUS_RELATIVE,
1845 .type = V4L2_CTRL_TYPE_INTEGER,
1846 .name = "Focus Control",
1852 .id = V4L2_CID_FOCUS_ABSOLUTE,
1853 .type = V4L2_CTRL_TYPE_INTEGER,
1854 .name = "Focus Control",
1858 .default_value = 125,
1861 .id = V4L2_CID_FOCUS_AUTO,
1862 .type = V4L2_CTRL_TYPE_BOOLEAN,
1863 .name = "Focus Control",
1869 #if 0 //IF CONTINOUS IS SUPPORT, SET TO 1
1871 .id = V4L2_CID_FOCUS_CONTINUOUS,
1872 .type = V4L2_CTRL_TYPE_BOOLEAN,
1873 .name = "Focus Control",
1882 #if CONFIG_SENSOR_Flash
1884 .id = V4L2_CID_FLASH,
1885 .type = V4L2_CTRL_TYPE_MENU,
1886 .name = "Flash Control",
1895 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1896 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1897 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1898 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1899 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1900 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1901 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1902 static int sensor_resume(struct soc_camera_device *icd);
1903 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1904 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1905 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1906 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1907 static int sensor_deactivate(struct i2c_client *client);
1908 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1909 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf);
1911 static struct soc_camera_ops sensor_ops =
1913 .suspend = sensor_suspend,
1914 .resume = sensor_resume,
1915 .set_bus_param = sensor_set_bus_param,
1916 .query_bus_param = sensor_query_bus_param,
1917 .controls = sensor_controls,
1918 .menus = sensor_menus,
1919 .num_controls = ARRAY_SIZE(sensor_controls),
1920 .num_menus = ARRAY_SIZE(sensor_menus),
1923 /* only one fixed colorspace per pixelcode */
1924 struct sensor_datafmt {
1925 enum v4l2_mbus_pixelcode code;
1926 enum v4l2_colorspace colorspace;
1929 /* Find a data format by a pixel code in an array */
1930 static const struct sensor_datafmt *sensor_find_datafmt(
1931 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1935 for (i = 0; i < n; i++)
1936 if (fmt[i].code == code)
1942 static const struct sensor_datafmt sensor_colour_fmts[] = {
1943 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG} ,
1944 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG}
1950 WqCmd_af_special_pos,
1954 WqCmd_af_return_idle,
1956 enum sensor_wq_result
1964 struct i2c_client *client;
1965 struct delayed_work dwork;
1966 enum sensor_wq_cmd cmd;
1967 wait_queue_head_t done;
1968 enum sensor_wq_result result;
1973 typedef struct sensor_info_priv_s
1989 unsigned char mirror; /* HFLIP */
1990 unsigned char flip; /* VFLIP */
1991 struct reginfo *winseqe_cur_addr;
1992 struct sensor_datafmt fmt;
1993 unsigned int enable;
1994 unsigned int funmodule_state;
1995 } sensor_info_priv_t;
1999 struct sensor_parameter
2001 unsigned short int preview_maxlines;
2002 unsigned short int preview_exposure;
2003 unsigned short int preview_line_width;
2004 unsigned short int preview_gain;
2006 unsigned short int capture_framerate;
2007 unsigned short int preview_framerate;
2013 struct v4l2_subdev subdev;
2014 struct i2c_client *client;
2015 sensor_info_priv_t info_priv;
2016 struct sensor_parameter parameter;
2017 struct workqueue_struct *sensor_wq;
2018 struct mutex wq_lock;
2019 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
2020 #if CONFIG_SENSOR_I2C_NOSCHED
2021 atomic_t tasklock_cnt;
2023 struct rk29camera_platform_data *sensor_io_request;
2024 struct rk29camera_gpio_res *sensor_gpio_res;
2027 static struct sensor* to_sensor(const struct i2c_client *client)
2029 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
2032 static int sensor_task_lock(struct i2c_client *client, int lock)
2034 #if CONFIG_SENSOR_I2C_NOSCHED
2036 struct sensor *sensor = to_sensor(client);
2039 if (atomic_read(&sensor->tasklock_cnt) == 0) {
2040 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
2041 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
2045 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
2046 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
2047 goto sensor_task_lock_err;
2052 atomic_add(1, &sensor->tasklock_cnt);
2054 if (atomic_read(&sensor->tasklock_cnt) > 0) {
2055 atomic_sub(1, &sensor->tasklock_cnt);
2057 if (atomic_read(&sensor->tasklock_cnt) == 0)
2062 sensor_task_lock_err:
2070 #if CONFIG_SENSOR_WRITE_REGS
2071 static int sensor_write_regs(struct i2c_client *client, u8 *reg_info, int num)
2074 struct i2c_msg msg[1];
2077 msg->addr = client->addr;
2078 msg->flags = client->flags;
2079 msg->buf = reg_info;
2080 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2081 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
2087 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
2088 err = i2c_transfer(client->adapter, msg, 1);
2092 SENSOR_TR("\n %s write reg failed, try to write again!\n", SENSOR_NAME_STRING());
2102 /* sensor register write */
2103 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
2107 struct i2c_msg msg[1];
2110 buf[1] = reg & 0xFF;
2113 msg->addr = client->addr;
2114 msg->flags = client->flags;
2116 msg->len = sizeof(buf);
2117 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2118 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
2123 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
2124 err = i2c_transfer(client->adapter, msg, 1);
2129 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
2137 /* sensor register read */
2138 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
2142 struct i2c_msg msg[2];
2145 buf[1] = reg & 0xFF;
2147 msg[0].addr = client->addr;
2148 msg[0].flags = client->flags;
2150 msg[0].len = sizeof(buf);
2151 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2152 msg[0].read_type = 2; /* fpga i2c:2==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2154 msg[1].addr = client->addr;
2155 msg[1].flags = client->flags|I2C_M_RD;
2158 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2159 msg[1].read_type = 2; /* fpga i2c:2==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2163 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
2164 err = i2c_transfer(client->adapter, msg, 2);
2170 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
2178 /* write a array of registers */
2179 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
2183 #if CONFIG_SENSOR_Focus
2184 struct sensor *sensor = to_sensor(client);
2186 #if CONFIG_SENSOR_I2C_RDWRCHK
2189 #if CONFIG_SENSOR_WRITE_REGS
2196 if (sensor_task_lock(client, 1) < 0)
2197 goto sensor_write_array_end;
2198 while (regarray[i].reg != SEQUENCE_END) {
2199 #if CONFIG_SENSOR_Focus
2200 if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
2201 SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
2203 goto sensor_write_array_end;
2207 #if CONFIG_SENSOR_WRITE_REGS
2211 reg_length = 0x0001;
2213 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {
2216 if(reg_num >= WRITE_REGS_NUM)
2223 size_num = reg_num + 1;
2225 ptemp = phead = (u8*)kmalloc((size_num+10)*sizeof(u8),GFP_KERNEL);
2227 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
2229 err = sensor_write(client, regarray[i].reg, regarray[i].val);
2231 *phead = regarray[j].reg >> 8;
2232 *(ptemp+1) = regarray[j].reg & 0xFF;
2234 for( ; reg_num > 0; reg_num --, j++) {
2235 *ptemp ++ = regarray[j].val;
2239 err = sensor_write_regs(client, ptemp,size_num);
2243 err = sensor_write(client, regarray[i].reg, regarray[i].val);
2246 err = sensor_write(client, regarray[i].reg, regarray[i].val);
2251 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
2255 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
2258 goto sensor_write_array_end;
2261 #if CONFIG_SENSOR_I2C_RDWRCHK
2262 sensor_read(client, regarray[i].reg, &valchk);
2263 if (valchk != regarray[i].val)
2264 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2270 #if CONFIG_SENSOR_Focus
2271 if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
2272 || (regarray == sensor_init_data)) {
2273 sensor->info_priv.affm_reinit = 1;
2277 sensor_write_array_end:
2278 sensor_task_lock(client,0);
2281 #if CONFIG_SENSOR_I2C_RDWRCHK
2282 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
2290 while (regarray[i].reg != 0)
2292 sensor_read(client, regarray[i].reg, &valchk);
2293 if (valchk != regarray[i].val)
2294 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2301 #if CONFIG_SENSOR_Focus
2308 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
2311 char read_tag=0xff,cnt;
2314 if (cmdinfo->validate_bit & 0x80) {
2315 if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
2316 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
2317 goto sensor_af_cmdset_err;
2319 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
2321 for (i=0; i<4; i++) {
2322 if (cmdinfo->validate_bit & (1<<i)) {
2323 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
2324 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]);
2325 goto sensor_af_cmdset_err;
2327 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]);
2331 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
2332 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
2333 goto sensor_af_cmdset_err;
2335 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
2338 if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
2339 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
2340 goto sensor_af_cmdset_err;
2347 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
2348 SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
2351 } while((read_tag != 0x00)&& (cnt++<100));
2353 SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
2355 sensor_af_cmdset_err:
2359 static int sensor_af_idlechk(struct i2c_client *client)
2363 struct af_cmdinfo cmdinfo;
2365 SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
2367 cmdinfo.cmd_tag = 0x01;
2368 cmdinfo.validate_bit = 0x80;
2369 ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
2371 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2373 goto sensor_af_idlechk_end;
2378 ret = sensor_read(client, CMD_ACK_Reg, &state);
2380 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2382 goto sensor_af_idlechk_end;
2384 }while(0x00 != state);
2387 sensor_af_idlechk_end:
2391 static int sensor_af_single(struct i2c_client *client)
2395 struct af_cmdinfo cmdinfo;
2397 cmdinfo.cmd_tag = 0x01;
2398 cmdinfo.validate_bit = 0x80;
2399 ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
2401 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
2403 goto sensor_af_single_end;
2413 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2415 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2417 goto sensor_af_single_end;
2419 }while((state == S_FOCUSING) && (cnt<100));
2421 if (state != S_FOCUSED) {
2422 SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
2424 goto sensor_af_single_end;
2427 //sensor_af_cmdset(client, ReturnIdle_Cmd, NULL); // by duanyp. fix af blur when taking pictures
2428 sensor_af_single_end:
2432 static int sensor_af_const(struct i2c_client *client)
2441 static int sensor_af_init(struct i2c_client *client)
2446 ret = sensor_write_array(client, sensor_af_firmware);
2448 SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
2450 goto sensor_af_init_end;
2460 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2462 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2464 goto sensor_af_init_end;
2466 }while((state == S_STARTUP) && (cnt<100));
2468 if (state != S_IDLE) {
2469 SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
2471 goto sensor_af_init_end;
2475 SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
2479 static int sensor_af_downfirmware(struct i2c_client *client)
2481 struct sensor *sensor = to_sensor(client);
2483 struct soc_camera_device *icd = client->dev.platform_data;
2484 struct v4l2_mbus_framefmt mf;
2486 SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
2488 if (sensor_af_init(client)) {
2489 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
2492 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
2494 mf.width = icd->user_width;
2495 mf.height = icd->user_height;
2496 mf.code = sensor->info_priv.fmt.code;
2497 mf.colorspace = sensor->info_priv.fmt.colorspace;
2498 mf.field = V4L2_FIELD_NONE;
2499 if (sensor_fmt_videochk(NULL, &mf) == true) { /* ddl@rock-chips.com: focus mode fix const auto focus in video */
2500 ret = sensor_af_const(client);
2502 switch (sensor->info_priv.auto_focus)
2504 case SENSOR_AF_MODE_AUTO:
2506 ret = sensor_af_single(client);
2509 case SENSOR_AF_MODE_CLOSE:
2516 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
2517 goto sensor_af_downfirmware_end;
2521 SENSOR_DG("%s sensor_af_downfirmware set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
2524 sensor_af_downfirmware_end:
2528 static void sensor_af_workqueue(struct work_struct *work)
2530 struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
2531 struct i2c_client *client = sensor_work->client;
2532 struct sensor *sensor = to_sensor(client);
2533 struct af_cmdinfo cmdinfo;
2535 SENSOR_DG("%s %s Enter, cmd:0x%x \n",SENSOR_NAME_STRING(), __FUNCTION__,sensor_work->cmd);
2537 mutex_lock(&sensor->wq_lock);
2539 switch (sensor_work->cmd)
2543 if (sensor_af_downfirmware(client) < 0) {
2544 SENSOR_TR("%s Sensor_af_init is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2548 case WqCmd_af_single:
2550 if (sensor_af_single(client) < 0) {
2551 SENSOR_TR("%s Sensor_af_single is failed in sensor_af_workqueue!\n",SENSOR_NAME_STRING());
2552 sensor_work->result = WqRet_fail;
2554 sensor_work->result = WqRet_success;
2558 case WqCmd_af_special_pos:
2560 sensor_af_idlechk(client);
2562 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2563 cmdinfo.cmd_para[0] = sensor_work->var;
2564 cmdinfo.validate_bit = 0x81;
2565 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2566 sensor_work->result = WqRet_fail;
2568 sensor_work->result = WqRet_success;
2571 case WqCmd_af_near_pos:
2573 sensor_af_idlechk(client);
2574 cmdinfo.cmd_tag = StepFocus_Near_Tag;
2575 cmdinfo.validate_bit = 0x80;
2576 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2577 sensor_work->result = WqRet_fail;
2579 sensor_work->result = WqRet_success;
2582 case WqCmd_af_far_pos:
2584 sensor_af_idlechk(client);
2585 cmdinfo.cmd_tag = StepFocus_Far_Tag;
2586 cmdinfo.validate_bit = 0x80;
2587 if (sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo) < 0)
2588 sensor_work->result = WqRet_fail;
2590 sensor_work->result = WqRet_success;
2593 case WqCmd_af_continues:
2595 if (sensor_af_const(client) < 0)
2596 sensor_work->result = WqRet_fail;
2598 sensor_work->result = WqRet_success;
2601 case WqCmd_af_return_idle:
2603 if (sensor_af_idlechk(client) < 0)
2604 sensor_work->result = WqRet_fail;
2606 sensor_work->result = WqRet_success;
2610 SENSOR_TR("Unknow command(%d) in %s af workqueue!",sensor_work->cmd,SENSOR_NAME_STRING());
2614 if (sensor_work->wait == false) {
2615 kfree((void*)sensor_work);
2617 wake_up(&sensor_work->done);
2619 mutex_unlock(&sensor->wq_lock);
2623 static int sensor_af_workqueue_set(struct soc_camera_device *icd, enum sensor_wq_cmd cmd, int var, bool wait)
2625 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2626 struct sensor *sensor = to_sensor(client);
2627 struct sensor_work *wk;
2630 if (sensor->sensor_wq == NULL) {
2632 goto sensor_af_workqueue_set_end;
2635 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) != SENSOR_AF_IS_OK) {
2636 if (cmd != WqCmd_af_init) {
2637 SENSOR_TR("%s %s cmd(%d) ingore,because af module isn't ready!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2639 goto sensor_af_workqueue_set_end;
2643 wk = kzalloc(sizeof(struct sensor_work), GFP_KERNEL);
2645 wk->client = client;
2646 INIT_WORK(&(wk->dwork.work), sensor_af_workqueue);
2648 wk->result = WqRet_inval;
2651 init_waitqueue_head(&wk->done);
2653 queue_delayed_work(sensor->sensor_wq,&(wk->dwork),0);
2655 /* ddl@rock-chips.com:
2656 * video_lock is been locked in v4l2_ioctl function, but auto focus may slow,
2657 * As a result any other ioctl calls will proceed very, very slowly since each call
2658 * will have to wait for the AF to finish. Camera preview is pause,because VIDIOC_QBUF
2659 * and VIDIOC_DQBUF is sched. so unlock video_lock here.
2662 mutex_unlock(&icd->video_lock);
2663 if (wait_event_timeout(wk->done, (wk->result != WqRet_inval), msecs_to_jiffies(2500)) == 0) {
2664 SENSOR_TR("%s %s cmd(%d) is timeout!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2668 mutex_lock(&icd->video_lock);
2672 SENSOR_TR("%s %s cmd(%d) ingore,because struct sensor_work malloc failed!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2675 sensor_af_workqueue_set_end:
2679 static int sensor_parameter_record(struct i2c_client *client)
2681 u8 ret_l,ret_m,ret_h;
2683 struct sensor *sensor = to_sensor(client);
2685 sensor_write(client,0x3503,0x07); //stop AE/AG
2686 sensor_write(client,0x3406,0x01); //stop AWB
2688 sensor_read(client,0x3500,&ret_h);
2689 sensor_read(client,0x3501, &ret_m);
2690 sensor_read(client,0x3502, &ret_l);
2694 SENSOR_DG(" %s Read 0x3500 = 0x%02x 0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
2695 //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
2696 sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
2698 sensor_read(client,0x350c, &ret_h);
2699 sensor_read(client,0x350d, &ret_l);
2700 sensor->parameter.preview_line_width = ret_h & 0xff;
2701 sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
2702 //Read back AGC Gain for preview
2703 sensor_read(client,0x350a, &ret_h);
2704 sensor_read(client,0x350b, &tp_l);
2705 sensor->parameter.preview_gain = ((ret_h & 0x01) << 8) + tp_l;
2707 sensor_read(client,0x380e, &ret_h);
2708 sensor->parameter.preview_maxlines = ret_h;
2709 sensor->parameter.preview_maxlines <<= 8;
2710 sensor_read(client,0x380f, &tp_l);
2711 sensor->parameter.preview_maxlines += tp_l;
2713 sensor->parameter.capture_framerate = 375;
2714 sensor->parameter.preview_framerate = 1500;
2716 sensor_read(client,0x3400,&sensor->parameter.awb[0]); //record awb value
2717 sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2718 sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2719 sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2720 sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2721 sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2723 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);
2726 static int sensor_ae_transfer(struct i2c_client *client)
2731 u16 ulCapture_Exposure;
2732 u32 ulCapture_Exposure_Gain;
2736 u8 reg_l = 0,reg_h =0;
2737 u16 Preview_Maxlines;
2739 u32 Capture_MaxLines;
2740 struct sensor *sensor = to_sensor(client);
2742 //Preview_Maxlines = sensor->parameter.preview_line_width;
2743 Preview_Maxlines = sensor->parameter.preview_maxlines;
2744 Gain = sensor->parameter.preview_gain;
2746 sensor_read(client,0x350c, ®_h);
2747 sensor_read(client,0x350d, ®_l);
2748 Capture_MaxLines = reg_h & 0xff;
2749 Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2752 sensor_read(client,0x380e, ®_h);
2753 Capture_MaxLines = reg_h;
2754 Capture_MaxLines <<= 8;
2755 sensor_read(client,0x380f, ®_l);
2756 Capture_MaxLines += reg_l;
2759 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2761 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2763 Lines_10ms = Lines_10ms & 0xffff;
2765 if(Preview_Maxlines == 0)
2766 Preview_Maxlines = 1;
2768 //ulCapture_Exposure =
2769 // (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2771 ulCapture_Exposure =
2772 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2773 ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2775 iCapture_Gain = (Gain & 0x0f) + 16;
2777 iCapture_Gain = iCapture_Gain << 1;
2780 iCapture_Gain = iCapture_Gain << 1;
2783 iCapture_Gain = iCapture_Gain << 1;
2786 iCapture_Gain = iCapture_Gain << 1;
2789 //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5); //0ld value 2.5, ½â¾ö¹ýÁÁ
2790 ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2792 if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2793 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2794 if (ulCapture_Exposure > Lines_10ms)
2796 //ulCapture_Exposure *= 1.7;
2797 ulCapture_Exposure /= Lines_10ms;
2798 ulCapture_Exposure *= Lines_10ms;
2801 ulCapture_Exposure = Capture_MaxLines;
2802 //ulCapture_Exposure_Gain *= 1.5;
2805 if(ulCapture_Exposure == 0)
2806 ulCapture_Exposure = 1;
2808 iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2809 iCapture_Gain = iCapture_Gain & 0xffff;
2811 ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2812 ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2813 ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2817 if (iCapture_Gain > 31) {
2819 iCapture_Gain = iCapture_Gain >> 1;
2821 if (iCapture_Gain > 31) {
2823 iCapture_Gain = iCapture_Gain >> 1;
2825 if (iCapture_Gain > 31) {
2827 iCapture_Gain = iCapture_Gain >> 1;
2829 if (iCapture_Gain > 31) {
2831 iCapture_Gain = iCapture_Gain >> 1;
2833 if (iCapture_Gain > 16)
2834 Gain |= ((iCapture_Gain -16) & 0x0f);
2837 // write the gain and exposure to 0x350* registers
2838 //m_iWrite0x350b=Gain;
2839 sensor_write(client,0x350b, Gain);
2840 Gain = (Gain >> 8) & 0x01;
2841 sensor_write(client,0x350a, Gain);
2842 //m_iWrite0x3502=ExposureLow;
2843 sensor_write(client,0x3502, ExposureLow);
2844 //m_iWrite0x3501=ExposureMid;
2845 sensor_write(client,0x3501, ExposureMid);
2846 //m_iWrite0x3500=ExposureHigh;
2847 sensor_write(client,0x3500, ExposureHigh);
2848 // SendToFile("Gain = 0x%x\r\n", Gain);
2849 // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2850 // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2851 // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2852 //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2853 //camera_timed_wait(200);
2854 //linzhk camera_timed_wait(500);
2856 sensor_write(client,0x3400,sensor->parameter.awb[0]); // resume awb value
2857 sensor_write(client,0x3401,sensor->parameter.awb[1]);
2858 sensor_write(client,0x3402,sensor->parameter.awb[2]);
2859 sensor_write(client,0x3403,sensor->parameter.awb[3]);
2860 sensor_write(client,0x3404,sensor->parameter.awb[4]);
2861 sensor_write(client,0x3405,sensor->parameter.awb[5]);
2863 SENSOR_DG(" %s Write 0x350b = 0x%02x 0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2867 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2869 struct soc_camera_link *icl = to_soc_camera_link(icd);
2872 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2876 case Sensor_PowerDown:
2878 if (icl->powerdown) {
2879 ret = icl->powerdown(icd->pdev, on);
2880 if (ret == RK29_CAM_IO_SUCCESS) {
2884 icl->reset(icd->pdev);
2886 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2888 goto sensor_power_end;
2895 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2896 struct sensor *sensor = to_sensor(client);
2898 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2899 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2901 //flash off after 2 secs
2902 hrtimer_cancel(&(flash_off_timer.timer));
2903 hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
2910 SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2919 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
2920 struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
2921 sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
2922 SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
2927 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2929 struct i2c_client *client = v4l2_get_subdevdata(sd);
2930 struct soc_camera_device *icd = client->dev.platform_data;
2931 struct sensor *sensor = to_sensor(client);
2932 const struct v4l2_queryctrl *qctrl;
2933 const struct sensor_datafmt *fmt;
2937 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2939 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2941 goto sensor_INIT_ERR;
2943 /* ddl@rock-chips.com : ov3640_powerOn have hardware reset */
2946 if (sensor_task_lock(client,1)<0)
2947 goto sensor_INIT_ERR;
2948 ret = sensor_write(client, 0x3012, 0x80);
2951 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2953 goto sensor_INIT_ERR;
2956 mdelay(5); //delay 5 microseconds
2958 /* check if it is an sensor sensor */
2959 ret = sensor_read(client, 0x300a, &value);
2961 SENSOR_TR("read chip id high byte failed\n");
2963 goto sensor_INIT_ERR;
2966 pid |= (value << 8);
2968 ret = sensor_read(client, 0x300b, &value);
2970 SENSOR_TR("read chip id low byte failed\n");
2972 goto sensor_INIT_ERR;
2975 pid |= (value & 0xff);
2976 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2977 if (pid == SENSOR_ID) {
2978 sensor->model = SENSOR_V4L2_IDENT;
2980 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2982 goto sensor_INIT_ERR;
2985 ret = sensor_write_array(client, sensor_init_data);
2988 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2989 goto sensor_INIT_ERR;
2991 sensor_task_lock(client,0);
2993 sensor->info_priv.winseqe_cur_addr = SENSOR_INIT_WINSEQADR;
2994 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2996 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2998 goto sensor_INIT_ERR;
3000 sensor->info_priv.fmt = *fmt;
3002 /* sensor sensor information for initialization */
3003 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3005 sensor->info_priv.whiteBalance = qctrl->default_value;
3006 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
3008 sensor->info_priv.brightness = qctrl->default_value;
3009 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3011 sensor->info_priv.effect = qctrl->default_value;
3012 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
3014 sensor->info_priv.exposure = qctrl->default_value;
3016 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
3018 sensor->info_priv.saturation = qctrl->default_value;
3019 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
3021 sensor->info_priv.contrast = qctrl->default_value;
3022 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
3024 sensor->info_priv.mirror = qctrl->default_value;
3025 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
3027 sensor->info_priv.flip = qctrl->default_value;
3028 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
3030 sensor->info_priv.scene = qctrl->default_value;
3031 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3033 sensor->info_priv.digitalzoom = qctrl->default_value;
3035 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
3036 #if CONFIG_SENSOR_Focus
3037 // sensor_set_focus();
3038 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);//?,JYK
3040 sensor->info_priv.focus = qctrl->default_value;
3043 #if CONFIG_SENSOR_Flash
3044 //sensor_set_flash();
3045 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
3047 sensor->info_priv.flash = qctrl->default_value;
3048 flash_off_timer.icd = icd;
3049 flash_off_timer.timer.function = flash_off_func;
3052 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);
3054 sensor->info_priv.funmodule_state = SENSOR_INIT_IS_OK;
3057 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
3058 sensor_task_lock(client,0);
3059 sensor_deactivate(client);
3062 static int sensor_deactivate(struct i2c_client *client)
3064 struct soc_camera_device *icd = client->dev.platform_data;
3065 struct sensor *sensor = to_sensor(client);
3067 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
3069 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
3070 sensor_ioctrl(icd, Sensor_PowerDown, 1);
3072 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
3073 icd->user_width = SENSOR_INIT_WIDTH;
3074 icd->user_height = SENSOR_INIT_HEIGHT;
3076 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
3079 static struct reginfo sensor_power_down_sequence[]=
3088 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
3091 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3093 if (pm_msg.event == PM_EVENT_SUSPEND) {
3094 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
3095 ret = sensor_write_array(client, sensor_power_down_sequence) ;
3097 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
3100 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
3102 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
3107 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
3114 static int sensor_resume(struct soc_camera_device *icd)
3118 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
3120 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
3124 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
3128 static int sensor_set_bus_param(struct soc_camera_device *icd,
3129 unsigned long flags)
3135 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
3137 struct soc_camera_link *icl = to_soc_camera_link(icd);
3138 unsigned long flags = SENSOR_BUS_PARAM;
3140 return soc_camera_apply_sensor_flags(icl, flags);
3143 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3145 struct i2c_client *client = v4l2_get_subdevdata(sd);
3146 struct soc_camera_device *icd = client->dev.platform_data;
3147 struct sensor *sensor = to_sensor(client);
3149 mf->width = icd->user_width;
3150 mf->height = icd->user_height;
3151 mf->code = sensor->info_priv.fmt.code;
3152 mf->colorspace = sensor->info_priv.fmt.colorspace;
3153 mf->field = V4L2_FIELD_NONE;
3157 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3161 if ((mf->width == 1024) && (mf->height == 768)) {
3163 } else if ((mf->width == 1280) && (mf->height == 1024)) {
3165 } else if ((mf->width == 1600) && (mf->height == 1200)) {
3167 } else if ((mf->width == 2048) && (mf->height == 1536)) {
3169 } else if ((mf->width == 2592) && (mf->height == 1944)) {
3174 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
3178 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3182 if ((mf->width == 1280) && (mf->height == 720)) {
3184 } else if ((mf->width == 1920) && (mf->height == 1080)) {
3189 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
3192 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3194 struct i2c_client *client = v4l2_get_subdevdata(sd);
3195 const struct sensor_datafmt *fmt;
3196 struct sensor *sensor = to_sensor(client);
3197 const struct v4l2_queryctrl *qctrl;
3198 struct soc_camera_device *icd = client->dev.platform_data;
3199 struct reginfo *winseqe_set_addr=NULL;
3200 int ret = 0, set_w,set_h;
3203 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
3204 ARRAY_SIZE(sensor_colour_fmts));
3207 goto sensor_s_fmt_end;
3210 if (sensor->info_priv.fmt.code != mf->code) {
3213 case V4L2_MBUS_FMT_YUYV8_2X8:
3215 winseqe_set_addr = sensor_ClrFmt_YUYV;
3218 case V4L2_MBUS_FMT_UYVY8_2X8:
3220 winseqe_set_addr = sensor_ClrFmt_UYVY;
3226 if (winseqe_set_addr != NULL) {
3227 sensor_write_array(client, winseqe_set_addr);
3228 sensor->info_priv.fmt.code = mf->code;
3229 sensor->info_priv.fmt.colorspace= mf->colorspace;
3230 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
3232 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
3238 isCapture = sensor_fmt_capturechk(sd, mf);
3240 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[isCapture][0].reg)
3242 winseqe_set_addr = sensor_qcif[isCapture];
3246 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[isCapture][0].reg)
3248 winseqe_set_addr = sensor_qvga[isCapture];
3252 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[isCapture][0].reg)
3254 winseqe_set_addr = sensor_cif[isCapture];
3258 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[isCapture][0].reg)
3260 winseqe_set_addr = sensor_vga[isCapture];
3264 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[isCapture][0].reg)
3266 winseqe_set_addr = sensor_svga[isCapture];
3270 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_sxga[isCapture][0].reg)
3272 winseqe_set_addr = sensor_xga[isCapture];
3276 else if (((set_w <= 1280) && (set_h <= 960)) && sensor_sxga[isCapture][0].reg)
3278 winseqe_set_addr = sensor_sxga[isCapture];
3282 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[isCapture][0].reg)
3284 winseqe_set_addr = sensor_uxga[isCapture];
3288 else if (((set_w <= 2048) && (set_h <= 1536)) && sensor_uxga[isCapture][0].reg)
3290 winseqe_set_addr = sensor_qxga[isCapture];
3296 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
3297 set_w = SENSOR_INIT_WIDTH;
3298 set_h = SENSOR_INIT_HEIGHT;
3299 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
3302 if (winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
3304 //srt --if capture,then should write sensor_qxga[1] first
3305 if((winseqe_set_addr != sensor_qxga[isCapture]) && isCapture) {
3306 SENSOR_DG("%s write sensor_qxga[1]\n", SENSOR_NAME_STRING());
3307 ret = sensor_write_array(client, sensor_qxga[isCapture]);
3309 SENSOR_TR("%s write sensor_qxga[1] failed\n", SENSOR_NAME_STRING());
3313 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
3314 sensor_parameter_record(client);
3315 /*#if CONFIG_SENSOR_Focus
3316 sensor_af_idlechk(client);
3317 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
3318 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
3320 #if CONFIG_SENSOR_Flash
3321 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
3322 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
3323 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
3326 }else { /* ddl@rock-chips.com : Video */
3327 #if CONFIG_SENSOR_Flash
3328 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
3329 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3330 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
3334 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
3335 if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
3336 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
3337 ret |= sensor_write_array(client,sensor_init_data);
3338 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
3341 ret |= sensor_write_array(client, winseqe_set_addr);
3343 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
3344 #if CONFIG_SENSOR_Flash
3345 if (sensor_fmt_capturechk(sd,mf) == true) {
3346 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
3347 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3348 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
3352 goto sensor_s_fmt_end;
3354 sensor->info_priv.winseqe_cur_addr = winseqe_set_addr;
3356 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
3357 sensor_ae_transfer(client);
3358 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3359 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3360 if (sensor->info_priv.whiteBalance != 0) {
3361 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3362 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3364 sensor->info_priv.snap2preview = true;
3365 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
3366 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3367 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3369 sensor->info_priv.video2preview = true;
3370 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
3371 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3372 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3373 if (sensor->info_priv.snap2preview == true) {
3374 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3375 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3377 #if CONFIG_SENSOR_Focus
3378 if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_AUTO) {
3379 sensor_af_workqueue_set(icd,WqCmd_af_return_idle,0,true);
3387 sensor->info_priv.video2preview = false;
3388 sensor->info_priv.snap2preview = false;
3390 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
3394 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
3402 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3404 struct i2c_client *client = v4l2_get_subdevdata(sd);
3405 struct sensor *sensor = to_sensor(client);
3406 const struct sensor_datafmt *fmt;
3407 int ret = 0,set_w,set_h;
3409 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
3410 ARRAY_SIZE(sensor_colour_fmts));
3412 fmt = &sensor->info_priv.fmt;
3413 mf->code = fmt->code;
3416 if (mf->height > SENSOR_MAX_HEIGHT)
3417 mf->height = SENSOR_MAX_HEIGHT;
3418 else if (mf->height < SENSOR_MIN_HEIGHT)
3419 mf->height = SENSOR_MIN_HEIGHT;
3421 if (mf->width > SENSOR_MAX_WIDTH)
3422 mf->width = SENSOR_MAX_WIDTH;
3423 else if (mf->width < SENSOR_MIN_WIDTH)
3424 mf->width = SENSOR_MIN_WIDTH;
3428 isCapture = sensor_fmt_capturechk(sd, mf);
3429 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[isCapture][0].reg!=SEQUENCE_END))
3434 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[isCapture][0].reg!=SEQUENCE_END))
3439 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[isCapture][0].reg!=SEQUENCE_END))
3444 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[isCapture][0].reg!=SEQUENCE_END))
3449 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[isCapture][0].reg!=SEQUENCE_END))
3454 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[isCapture][0].reg!=SEQUENCE_END))
3459 else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
3464 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[isCapture][0].reg!=SEQUENCE_END))
3469 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[isCapture][0].reg!=SEQUENCE_END))
3474 else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
3479 else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[isCapture][0].reg!=SEQUENCE_END))
3484 else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
3491 set_w = SENSOR_INIT_WIDTH;
3492 set_h = SENSOR_INIT_HEIGHT;
3498 mf->colorspace = fmt->colorspace;
3503 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
3505 struct i2c_client *client = v4l2_get_subdevdata(sd);
3507 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
3510 if (id->match.addr != client->addr)
3513 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV3640 identifier */
3518 #if CONFIG_SENSOR_Brightness
3519 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3521 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3523 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3525 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
3527 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
3529 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3532 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3536 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3540 #if CONFIG_SENSOR_Effect
3541 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3543 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3545 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3547 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
3549 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
3551 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3554 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3558 SENSOR_TR("\n%s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3562 #if CONFIG_SENSOR_Exposure
3563 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3565 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3567 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3569 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
3571 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3573 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3576 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3580 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3584 #if CONFIG_SENSOR_Saturation
3585 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3587 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3589 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3591 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3593 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3595 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3598 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3602 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3606 #if CONFIG_SENSOR_Contrast
3607 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3609 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3611 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3613 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3615 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3617 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3620 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3624 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3628 #if CONFIG_SENSOR_Mirror
3629 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3631 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3633 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3635 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3637 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3639 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3642 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3646 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3650 #if CONFIG_SENSOR_Flip
3651 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3653 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3655 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3657 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3659 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3661 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3664 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3668 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3672 #if CONFIG_SENSOR_Scene
3673 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3675 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3677 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3679 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3681 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3683 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3686 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3690 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3694 #if CONFIG_SENSOR_WhiteBalance
3695 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3697 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3699 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3701 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3703 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3705 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3708 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3712 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3716 #if CONFIG_SENSOR_DigitalZoom
3717 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3719 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3720 struct sensor *sensor = to_sensor(client);
3721 const struct v4l2_queryctrl *qctrl_info;
3722 int digitalzoom_cur, digitalzoom_total;
3724 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3728 digitalzoom_cur = sensor->info_priv.digitalzoom;
3729 digitalzoom_total = qctrl_info->maximum;
3731 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3733 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3737 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3739 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3743 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3745 *value = digitalzoom_total - digitalzoom_cur;
3748 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3750 *value = 0 - digitalzoom_cur;
3753 digitalzoom_cur += *value;
3755 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3757 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3759 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3762 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3770 #if CONFIG_SENSOR_Focus
3771 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3773 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3774 struct sensor *sensor = to_sensor(client);
3775 const struct v4l2_queryctrl *qctrl_info;
3778 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3782 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3783 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3784 ret = sensor_af_workqueue_set(icd, WqCmd_af_special_pos, value, true);
3785 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3788 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3792 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3793 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3798 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3800 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3801 struct sensor *sensor = to_sensor(client);
3802 const struct v4l2_queryctrl *qctrl_info;
3805 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3809 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3810 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3812 ret = sensor_af_workqueue_set(icd, WqCmd_af_near_pos, 0, true);
3814 ret = sensor_af_workqueue_set(icd, WqCmd_af_far_pos, 0, true);
3816 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3819 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3823 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3824 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3829 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3831 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3832 struct sensor *sensor = to_sensor(client);
3835 if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3838 case SENSOR_AF_MODE_AUTO:
3840 ret = sensor_af_workqueue_set(icd, WqCmd_af_single, 0, true);
3844 /*case SENSOR_AF_MODE_MACRO:
3846 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3850 case SENSOR_AF_MODE_INFINITY:
3852 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3856 case SENSOR_AF_MODE_CONTINUOUS:
3858 ret = sensor_af_workqueue_set(icd, WqCmd_af_continues, 0, true);
3862 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3867 SENSOR_DG("%s..%s : %d ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3870 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3871 sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3877 #if CONFIG_SENSOR_Flash
3878 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3880 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3881 if (value == 3) { /* ddl@rock-chips.com: torch */
3882 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
3884 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3886 SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3890 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3894 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3896 struct i2c_client *client = v4l2_get_subdevdata(sd);
3897 struct sensor *sensor = to_sensor(client);
3898 const struct v4l2_queryctrl *qctrl;
3900 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3904 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3910 case V4L2_CID_BRIGHTNESS:
3912 ctrl->value = sensor->info_priv.brightness;
3915 case V4L2_CID_SATURATION:
3917 ctrl->value = sensor->info_priv.saturation;
3920 case V4L2_CID_CONTRAST:
3922 ctrl->value = sensor->info_priv.contrast;
3925 case V4L2_CID_DO_WHITE_BALANCE:
3927 ctrl->value = sensor->info_priv.whiteBalance;
3930 case V4L2_CID_EXPOSURE:
3932 ctrl->value = sensor->info_priv.exposure;
3935 case V4L2_CID_HFLIP:
3937 ctrl->value = sensor->info_priv.mirror;
3940 case V4L2_CID_VFLIP:
3942 ctrl->value = sensor->info_priv.flip;
3953 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3955 struct i2c_client *client = v4l2_get_subdevdata(sd);
3956 struct sensor *sensor = to_sensor(client);
3957 struct soc_camera_device *icd = client->dev.platform_data;
3958 const struct v4l2_queryctrl *qctrl;
3961 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3965 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3971 #if CONFIG_SENSOR_Brightness
3972 case V4L2_CID_BRIGHTNESS:
3974 if (ctrl->value != sensor->info_priv.brightness)
3976 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3980 sensor->info_priv.brightness = ctrl->value;
3985 #if CONFIG_SENSOR_Exposure
3986 case V4L2_CID_EXPOSURE:
3988 if (ctrl->value != sensor->info_priv.exposure)
3990 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3994 sensor->info_priv.exposure = ctrl->value;
3999 #if CONFIG_SENSOR_Saturation
4000 case V4L2_CID_SATURATION:
4002 if (ctrl->value != sensor->info_priv.saturation)
4004 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
4008 sensor->info_priv.saturation = ctrl->value;
4013 #if CONFIG_SENSOR_Contrast
4014 case V4L2_CID_CONTRAST:
4016 if (ctrl->value != sensor->info_priv.contrast)
4018 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
4022 sensor->info_priv.contrast = ctrl->value;
4027 #if CONFIG_SENSOR_WhiteBalance
4028 case V4L2_CID_DO_WHITE_BALANCE:
4030 if (ctrl->value != sensor->info_priv.whiteBalance)
4032 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
4036 sensor->info_priv.whiteBalance = ctrl->value;
4041 #if CONFIG_SENSOR_Mirror
4042 case V4L2_CID_HFLIP:
4044 if (ctrl->value != sensor->info_priv.mirror)
4046 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
4048 sensor->info_priv.mirror = ctrl->value;
4053 #if CONFIG_SENSOR_Flip
4054 case V4L2_CID_VFLIP:
4056 if (ctrl->value != sensor->info_priv.flip)
4058 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
4060 sensor->info_priv.flip = ctrl->value;
4071 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
4073 const struct v4l2_queryctrl *qctrl;
4074 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4075 struct sensor *sensor = to_sensor(client);
4077 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
4081 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
4085 switch (ext_ctrl->id)
4087 case V4L2_CID_SCENE:
4089 ext_ctrl->value = sensor->info_priv.scene;
4092 case V4L2_CID_EFFECT:
4094 ext_ctrl->value = sensor->info_priv.effect;
4097 case V4L2_CID_ZOOM_ABSOLUTE:
4099 ext_ctrl->value = sensor->info_priv.digitalzoom;
4102 case V4L2_CID_ZOOM_RELATIVE:
4106 case V4L2_CID_FOCUS_ABSOLUTE:
4110 case V4L2_CID_FOCUS_RELATIVE:
4114 case V4L2_CID_FLASH:
4116 ext_ctrl->value = sensor->info_priv.flash;
4124 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
4126 const struct v4l2_queryctrl *qctrl;
4127 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
4128 struct sensor *sensor = to_sensor(client);
4131 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
4135 SENSOR_TR("\n %s ioctrl id = 0x%x is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
4140 switch (ext_ctrl->id)
4142 #if CONFIG_SENSOR_Scene
4143 case V4L2_CID_SCENE:
4145 if (ext_ctrl->value != sensor->info_priv.scene)
4147 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
4149 sensor->info_priv.scene = ext_ctrl->value;
4154 #if CONFIG_SENSOR_Effect
4155 case V4L2_CID_EFFECT:
4157 if (ext_ctrl->value != sensor->info_priv.effect)
4159 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
4161 sensor->info_priv.effect= ext_ctrl->value;
4166 #if CONFIG_SENSOR_DigitalZoom
4167 case V4L2_CID_ZOOM_ABSOLUTE:
4169 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
4172 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
4174 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
4176 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
4178 sensor->info_priv.digitalzoom += val_offset;
4180 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
4185 case V4L2_CID_ZOOM_RELATIVE:
4187 if (ext_ctrl->value)
4189 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
4191 sensor->info_priv.digitalzoom += ext_ctrl->value;
4193 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
4198 #if CONFIG_SENSOR_Focus
4199 case V4L2_CID_FOCUS_ABSOLUTE:
4201 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
4206 case V4L2_CID_FOCUS_RELATIVE:
4208 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
4211 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
4214 case V4L2_CID_FOCUS_AUTO:
4216 if (ext_ctrl->value == 1) {
4217 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0) {
4218 if(0 == (sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)) {
4219 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
4223 sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
4224 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
4225 if (ext_ctrl->value == 0)
4226 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
4230 case V4L2_CID_FOCUS_CONTINUOUS:
4232 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
4233 if (ext_ctrl->value == 1) {
4234 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
4236 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
4239 if (ext_ctrl->value == 0)
4240 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
4245 #if CONFIG_SENSOR_Flash
4246 case V4L2_CID_FLASH:
4248 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
4250 sensor->info_priv.flash = ext_ctrl->value;
4252 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
4263 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
4265 struct i2c_client *client = v4l2_get_subdevdata(sd);
4266 struct soc_camera_device *icd = client->dev.platform_data;
4267 int i, error_cnt=0, error_idx=-1;
4270 for (i=0; i<ext_ctrl->count; i++) {
4271 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
4278 error_idx = ext_ctrl->count;
4280 if (error_idx != -1) {
4281 ext_ctrl->error_idx = error_idx;
4288 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
4290 struct i2c_client *client = v4l2_get_subdevdata(sd);
4291 struct soc_camera_device *icd = client->dev.platform_data;
4292 int i, error_cnt=0, error_idx=-1;
4295 for (i=0; i<ext_ctrl->count; i++) {
4296 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
4303 error_idx = ext_ctrl->count;
4305 if (error_idx != -1) {
4306 ext_ctrl->error_idx = error_idx;
4313 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
4315 struct i2c_client *client = v4l2_get_subdevdata(sd);
4316 struct sensor *sensor = to_sensor(client);
4317 #if CONFIG_SENSOR_Focus
4318 struct soc_camera_device *icd = client->dev.platform_data;
4319 struct v4l2_mbus_framefmt mf;
4323 sensor->info_priv.enable = 1;
4324 #if CONFIG_SENSOR_Focus
4325 mf.width = icd->user_width;
4326 mf.height = icd->user_height;
4327 mf.code = sensor->info_priv.fmt.code;
4328 mf.colorspace = sensor->info_priv.fmt.colorspace;
4329 mf.field = V4L2_FIELD_NONE;
4330 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
4331 if (sensor_fmt_capturechk(sd, &mf) == false) {
4332 if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
4333 sensor_af_workqueue_set(icd, WqCmd_af_init, 0, false);
4334 sensor->info_priv.affm_reinit = 0;
4338 } else if (enable == 0) {
4339 sensor->info_priv.enable = 0;
4340 #if CONFIG_SENSOR_Focus
4341 flush_workqueue(sensor->sensor_wq);
4347 /* Interface active, can use i2c. If it fails, it can indeed mean, that
4348 * this wasn't our capture interface, so, we wait for the right one */
4349 static int sensor_video_probe(struct soc_camera_device *icd,
4350 struct i2c_client *client)
4354 struct sensor *sensor = to_sensor(client);
4356 /* We must have a parent by now. And it cannot be a wrong one.
4357 * So this entire test is completely redundant. */
4358 if (!icd->dev.parent ||
4359 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
4362 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
4364 goto sensor_video_probe_err;
4367 ret = sensor_write(client, 0x3012, 0x80);
4369 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
4371 goto sensor_video_probe_err;
4373 mdelay(5); //delay 5 microseconds
4375 /* check if it is an sensor sensor */
4376 ret = sensor_read(client, 0x300a, &value);
4378 SENSOR_TR("read chip id high byte failed\n");
4380 goto sensor_video_probe_err;
4383 pid |= (value << 8);
4385 ret = sensor_read(client, 0x300b, &value);
4387 SENSOR_TR("read chip id low byte failed\n");
4389 goto sensor_video_probe_err;
4392 pid |= (value & 0xff);
4393 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
4394 if (pid == SENSOR_ID) {
4395 sensor->model = SENSOR_V4L2_IDENT;
4397 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
4399 goto sensor_video_probe_err;
4403 sensor_video_probe_err:
4406 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
4408 struct i2c_client *client = v4l2_get_subdevdata(sd);
4409 struct soc_camera_device *icd = client->dev.platform_data;
4410 struct sensor *sensor = to_sensor(client);
4413 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4416 case RK29_CAM_SUBDEV_DEACTIVATE:
4418 sensor_deactivate(client);
4421 case RK29_CAM_SUBDEV_IOREQUEST:
4423 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
4424 if (sensor->sensor_io_request != NULL) {
4425 sensor->sensor_gpio_res = NULL;
4426 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
4427 if (sensor->sensor_io_request->gpio_res[i].dev_name &&
4428 (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
4429 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
4432 if (sensor->sensor_gpio_res == NULL) {
4433 SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
4435 goto sensor_ioctl_end;
4438 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
4440 goto sensor_ioctl_end;
4442 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
4444 #if CONFIG_SENSOR_Flash
4445 if (sensor->sensor_gpio_res) {
4446 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
4447 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
4448 for (i = 0; i < icd->ops->num_controls; i++) {
4449 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
4450 //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
4451 sensor_controls[i].id=0xffff;
4454 sensor->info_priv.flash = 0xff;
4455 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
4456 }else{ //two cameras are the same,need to deal diffrently ,zyc
4457 for (i = 0; i < icd->ops->num_controls; i++) {
4458 if(0xffff == icd->ops->controls[i].id){
4459 sensor_controls[i].id=V4L2_CID_FLASH;
4469 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4478 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
4479 enum v4l2_mbus_pixelcode *code)
4481 if (index >= ARRAY_SIZE(sensor_colour_fmts))
4484 *code = sensor_colour_fmts[index].code;
4487 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
4488 .init = sensor_init,
4489 .g_ctrl = sensor_g_control,
4490 .s_ctrl = sensor_s_control,
4491 .g_ext_ctrls = sensor_g_ext_controls,
4492 .s_ext_ctrls = sensor_s_ext_controls,
4493 .g_chip_ident = sensor_g_chip_ident,
4494 .ioctl = sensor_ioctl,
4497 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
4498 .s_mbus_fmt = sensor_s_fmt,
4499 .g_mbus_fmt = sensor_g_fmt,
4500 .try_mbus_fmt = sensor_try_fmt,
4501 .enum_mbus_fmt = sensor_enum_fmt,
4502 .s_stream = sensor_s_stream,
4505 static struct v4l2_subdev_ops sensor_subdev_ops = {
4506 .core = &sensor_subdev_core_ops,
4507 .video = &sensor_subdev_video_ops,
4510 static int sensor_probe(struct i2c_client *client,
4511 const struct i2c_device_id *did)
4513 struct sensor *sensor;
4514 struct soc_camera_device *icd = client->dev.platform_data;
4515 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
4516 struct soc_camera_link *icl;
4519 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
4521 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
4525 icl = to_soc_camera_link(icd);
4527 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
4531 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
4532 dev_warn(&adapter->dev,
4533 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
4537 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
4541 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
4543 /* Second stage probe - when a capture adapter is there */
4544 icd->ops = &sensor_ops;
4545 sensor->info_priv.fmt = sensor_colour_fmts[0];
4546 #if CONFIG_SENSOR_I2C_NOSCHED
4547 atomic_set(&sensor->tasklock_cnt,0);
4550 ret = sensor_video_probe(icd, client);
4553 i2c_set_clientdata(client, NULL);
4557 #if CONFIG_SENSOR_Focus
4558 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING(_af_workqueue));
4559 if (sensor->sensor_wq == NULL)
4560 SENSOR_TR("%s create fail!", SENSOR_NAME_STRING(_af_workqueue));
4561 mutex_init(&sensor->wq_lock);
4564 hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4565 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
4569 static int sensor_remove(struct i2c_client *client)
4571 struct sensor *sensor = to_sensor(client);
4572 struct soc_camera_device *icd = client->dev.platform_data;
4574 #if CONFIG_SENSOR_Focus
4575 if (sensor->sensor_wq) {
4576 destroy_workqueue(sensor->sensor_wq);
4577 sensor->sensor_wq = NULL;
4582 i2c_set_clientdata(client, NULL);
4583 client->driver = NULL;
4589 static const struct i2c_device_id sensor_id[] = {
4590 {SENSOR_NAME_STRING(), 0 },
4593 MODULE_DEVICE_TABLE(i2c, sensor_id);
4595 static struct i2c_driver sensor_i2c_driver = {
4597 .name = SENSOR_NAME_STRING(),
4599 .probe = sensor_probe,
4600 .remove = sensor_remove,
4601 .id_table = sensor_id,
4604 static int __init sensor_mod_init(void)
4606 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4607 return i2c_add_driver(&sensor_i2c_driver);
4610 static void __exit sensor_mod_exit(void)
4612 i2c_del_driver(&sensor_i2c_driver);
4615 device_initcall_sync(sensor_mod_init);
4616 module_exit(sensor_mod_exit);
4618 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4619 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4620 MODULE_LICENSE("GPL");