2 o* 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 <mach/rk29_camera.h>
25 module_param(debug, int, S_IRUGO|S_IWUSR);
27 #define dprintk(level, fmt, arg...) do { \
29 printk(KERN_WARNING fmt , ## arg); } while (0)
31 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
32 #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_HI253
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_HI253
48 #define SENSOR_ID 0x92
49 #define SENSOR_MIN_WIDTH 176
50 #define SENSOR_MIN_HEIGHT 144
51 #define SENSOR_MAX_WIDTH 1600
52 #define SENSOR_MAX_HEIGHT 1200
53 #define SENSOR_INIT_WIDTH 1600 /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT 1200
55 #define SENSOR_INIT_WINSEQADR sensor_uxga
56 #define SENSOR_INIT_PIXFMT V4L2_PIX_FMT_UYVY
58 #define CONFIG_SENSOR_WhiteBalance 1
59 #define CONFIG_SENSOR_Brightness 0
60 #define CONFIG_SENSOR_Contrast 0
61 #define CONFIG_SENSOR_Saturation 0
62 #define CONFIG_SENSOR_Effect 1
63 #define CONFIG_SENSOR_Scene 1
64 #define CONFIG_SENSOR_DigitalZoom 0
65 #define CONFIG_SENSOR_Focus 0
66 #define CONFIG_SENSOR_Exposure 0
67 #define CONFIG_SENSOR_Flash 0
68 #define CONFIG_SENSOR_Mirror 0
69 #define CONFIG_SENSOR_Flip 0
71 #define CONFIG_SENSOR_I2C_SPEED 100000 /* Hz */
72 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
73 #define CONFIG_SENSOR_I2C_NOSCHED 0
74 #define CONFIG_SENSOR_I2C_RDWRCHK 0
76 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING|\
77 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
78 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
80 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
81 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
82 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
83 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
84 #define COLOR_TEMPERATURE_OFFICE_DN 3500
85 #define COLOR_TEMPERATURE_OFFICE_UP 5000
86 #define COLOR_TEMPERATURE_HOME_DN 2500
87 #define COLOR_TEMPERATURE_HOME_UP 3500
89 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
90 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
101 /* init SVGA preview */
102 static struct reginfo sensor_init_data[] =
333 {0x11, 0x03}, //lxh,normal
375 {0x10, 0x7f},//lxh,3f
1141 /* 1600X1200 UXGA capture */
1142 static struct reginfo sensor_uxga[] =
1172 /* 1280X1024 SXGA */
1173 static struct reginfo sensor_sxga[] =
1177 static struct reginfo sensor_xga[] =
1182 /* 800X600 SVGA,30fps*/
1183 static struct reginfo sensor_svga[] =
1191 {0x03, 0x12}, //Function
1197 {0x03, 0x13}, //Function
1198 {0x80, 0xfd}, //Function
1225 static struct reginfo sensor_vga[] =
1231 static struct reginfo sensor_cif[] =
1235 static struct reginfo sensor_qvga[] =
1239 static struct reginfo sensor_qcif[] =
1243 static struct reginfo sensor_ClrFmt_YUYV[]=
1249 static struct reginfo sensor_ClrFmt_UYVY[]=
1255 #if CONFIG_SENSOR_WhiteBalance
1256 static struct reginfo sensor_WhiteB_Auto[]=
1270 /* Cloudy Colour Temperature : 6500K - 8000K */
1271 static struct reginfo sensor_WhiteB_Cloudy[]=
1285 /* ClearDay Colour Temperature : 5000K - 6500K */
1286 static struct reginfo sensor_WhiteB_ClearDay[]=
1300 /* Office Colour Temperature : 3500K - 5000K */
1301 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
1316 /* Home Colour Temperature : 2500K - 3500K */
1317 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
1331 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
1332 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
1336 #if CONFIG_SENSOR_Brightness
1337 static struct reginfo sensor_Brightness0[]=
1345 static struct reginfo sensor_Brightness1[]=
1353 static struct reginfo sensor_Brightness2[]=
1361 static struct reginfo sensor_Brightness3[]=
1369 static struct reginfo sensor_Brightness4[]=
1377 static struct reginfo sensor_Brightness5[]=
1384 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
1385 sensor_Brightness4, sensor_Brightness5,NULL,
1390 #if CONFIG_SENSOR_Effect
1391 static struct reginfo sensor_Effect_Normal[] =
1400 static struct reginfo sensor_Effect_WandB[] =
1412 static struct reginfo sensor_Effect_Sepia[] =
1424 static struct reginfo sensor_Effect_Negative[] =
1437 static struct reginfo sensor_Effect_Bluish[] =
1450 static struct reginfo sensor_Effect_Green[] =
1462 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
1463 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
1466 #if CONFIG_SENSOR_Exposure
1467 static struct reginfo sensor_Exposure0[]=
1473 static struct reginfo sensor_Exposure1[]=
1480 static struct reginfo sensor_Exposure2[]=
1486 static struct reginfo sensor_Exposure3[]=
1492 static struct reginfo sensor_Exposure4[]=
1499 static struct reginfo sensor_Exposure5[]=
1505 static struct reginfo sensor_Exposure6[]=
1512 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1513 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1516 #if CONFIG_SENSOR_Saturation
1517 static struct reginfo sensor_Saturation0[]=
1523 static struct reginfo sensor_Saturation1[]=
1529 static struct reginfo sensor_Saturation2[]=
1533 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1536 #if CONFIG_SENSOR_Contrast
1537 static struct reginfo sensor_Contrast0[]=
1544 static struct reginfo sensor_Contrast1[]=
1551 static struct reginfo sensor_Contrast2[]=
1558 static struct reginfo sensor_Contrast3[]=
1565 static struct reginfo sensor_Contrast4[]=
1573 static struct reginfo sensor_Contrast5[]=
1580 static struct reginfo sensor_Contrast6[]=
1586 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1587 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1591 #if CONFIG_SENSOR_Mirror
1592 static struct reginfo sensor_MirrorOn[]=
1598 static struct reginfo sensor_MirrorOff[]=
1603 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1605 #if CONFIG_SENSOR_Flip
1606 static struct reginfo sensor_FlipOn[]=
1612 static struct reginfo sensor_FlipOff[]=
1617 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1620 #if CONFIG_SENSOR_Scene
1621 static struct reginfo sensor_SceneAuto[] =
1627 static struct reginfo sensor_SceneNight[] =
1632 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1635 #if CONFIG_SENSOR_DigitalZoom
1636 static struct reginfo sensor_Zoom0[] =
1641 static struct reginfo sensor_Zoom1[] =
1646 static struct reginfo sensor_Zoom2[] =
1652 static struct reginfo sensor_Zoom3[] =
1656 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1658 static const struct v4l2_querymenu sensor_menus[] =
1660 #if CONFIG_SENSOR_WhiteBalance
1661 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1662 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1663 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1666 #if CONFIG_SENSOR_Effect
1667 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1668 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1669 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1672 #if CONFIG_SENSOR_Scene
1673 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1676 #if CONFIG_SENSOR_Flash
1677 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1678 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1682 static const struct v4l2_queryctrl sensor_controls[] =
1684 #if CONFIG_SENSOR_WhiteBalance
1686 .id = V4L2_CID_DO_WHITE_BALANCE,
1687 .type = V4L2_CTRL_TYPE_MENU,
1688 .name = "White Balance Control",
1696 #if CONFIG_SENSOR_Brightness
1698 .id = V4L2_CID_BRIGHTNESS,
1699 .type = V4L2_CTRL_TYPE_INTEGER,
1700 .name = "Brightness Control",
1708 #if CONFIG_SENSOR_Effect
1710 .id = V4L2_CID_EFFECT,
1711 .type = V4L2_CTRL_TYPE_MENU,
1712 .name = "Effect Control",
1720 #if CONFIG_SENSOR_Exposure
1722 .id = V4L2_CID_EXPOSURE,
1723 .type = V4L2_CTRL_TYPE_INTEGER,
1724 .name = "Exposure Control",
1732 #if CONFIG_SENSOR_Saturation
1734 .id = V4L2_CID_SATURATION,
1735 .type = V4L2_CTRL_TYPE_INTEGER,
1736 .name = "Saturation Control",
1744 #if CONFIG_SENSOR_Contrast
1746 .id = V4L2_CID_CONTRAST,
1747 .type = V4L2_CTRL_TYPE_INTEGER,
1748 .name = "Contrast Control",
1756 #if CONFIG_SENSOR_Mirror
1758 .id = V4L2_CID_HFLIP,
1759 .type = V4L2_CTRL_TYPE_BOOLEAN,
1760 .name = "Mirror Control",
1768 #if CONFIG_SENSOR_Flip
1770 .id = V4L2_CID_VFLIP,
1771 .type = V4L2_CTRL_TYPE_BOOLEAN,
1772 .name = "Flip Control",
1780 #if CONFIG_SENSOR_Scene
1782 .id = V4L2_CID_SCENE,
1783 .type = V4L2_CTRL_TYPE_MENU,
1784 .name = "Scene Control",
1792 #if CONFIG_SENSOR_DigitalZoom
1794 .id = V4L2_CID_ZOOM_RELATIVE,
1795 .type = V4L2_CTRL_TYPE_INTEGER,
1796 .name = "DigitalZoom Control",
1802 .id = V4L2_CID_ZOOM_ABSOLUTE,
1803 .type = V4L2_CTRL_TYPE_INTEGER,
1804 .name = "DigitalZoom Control",
1812 #if CONFIG_SENSOR_Focus
1814 .id = V4L2_CID_FOCUS_RELATIVE,
1815 .type = V4L2_CTRL_TYPE_INTEGER,
1816 .name = "Focus Control",
1822 .id = V4L2_CID_FOCUS_ABSOLUTE,
1823 .type = V4L2_CTRL_TYPE_INTEGER,
1824 .name = "Focus Control",
1828 .default_value = 125,
1832 #if CONFIG_SENSOR_Flash
1834 .id = V4L2_CID_FLASH,
1835 .type = V4L2_CTRL_TYPE_MENU,
1836 .name = "Flash Control",
1845 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1846 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1847 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1848 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1849 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1850 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1851 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1852 static int sensor_resume(struct soc_camera_device *icd);
1853 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1854 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1855 #if CONFIG_SENSOR_Effect
1856 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1858 #if CONFIG_SENSOR_WhiteBalance
1859 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1861 static int sensor_deactivate(struct i2c_client *client);
1863 static struct soc_camera_ops sensor_ops =
1865 .suspend = sensor_suspend,
1866 .resume = sensor_resume,
1867 .set_bus_param = sensor_set_bus_param,
1868 .query_bus_param = sensor_query_bus_param,
1869 .controls = sensor_controls,
1870 .menus = sensor_menus,
1871 .num_controls = ARRAY_SIZE(sensor_controls),
1872 .num_menus = ARRAY_SIZE(sensor_menus),
1875 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1876 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1877 .colorspace = _colorspace }
1879 #define JPG_FMT(_name, _depth, _fourcc) \
1880 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1882 static const struct soc_camera_data_format sensor_colour_formats[] = {
1883 JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1884 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1887 typedef struct sensor_info_priv_s
1901 unsigned char mirror; /* HFLIP */
1902 unsigned char flip; /* VFLIP */
1903 unsigned int winseqe_cur_addr;
1904 unsigned int pixfmt;
1906 } sensor_info_priv_t;
1910 struct v4l2_subdev subdev;
1911 struct i2c_client *client;
1912 sensor_info_priv_t info_priv;
1913 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1914 #if CONFIG_SENSOR_I2C_NOSCHED
1915 atomic_t tasklock_cnt;
1917 struct rk29camera_platform_data *sensor_io_request;
1918 struct rk29camera_gpio_res *sensor_gpio_res;
1921 static struct sensor* to_sensor(const struct i2c_client *client)
1923 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1926 static int sensor_task_lock(struct i2c_client *client, int lock)
1928 #if CONFIG_SENSOR_I2C_NOSCHED
1930 struct sensor *sensor = to_sensor(client);
1933 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1934 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1935 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1939 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1940 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1941 goto sensor_task_lock_err;
1946 atomic_add(1, &sensor->tasklock_cnt);
1948 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1949 atomic_sub(1, &sensor->tasklock_cnt);
1951 if (atomic_read(&sensor->tasklock_cnt) == 0)
1956 sensor_task_lock_err:
1963 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1967 struct i2c_msg msg[1];
1972 msg->addr = client->addr;
1973 msg->flags = client->flags;
1975 msg->len = sizeof(buf);
1976 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1977 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1982 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1983 err = i2c_transfer(client->adapter, msg, 1);
1988 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1996 /* sensor register read */
1997 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
2001 struct i2c_msg msg[2];
2005 msg[0].addr = client->addr;
2006 msg[0].flags = client->flags;
2008 msg[0].len = sizeof(buf);
2009 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2010 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2012 msg[1].addr = client->addr;
2013 msg[1].flags = client->flags|I2C_M_RD;
2016 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2017 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2021 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
2022 err = i2c_transfer(client->adapter, msg, 2);
2028 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
2037 /* write a array of registers */
2038 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
2042 #if CONFIG_SENSOR_I2C_RDWRCHK
2047 if (sensor_task_lock(client, 1) < 0)
2048 goto sensor_write_array_end;
2049 while (regarray[i].reg != END_REG)
2051 err = sensor_write(client, regarray[i].reg, regarray[i].val);
2055 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
2059 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
2061 goto sensor_write_array_end;
2064 #if CONFIG_SENSOR_I2C_RDWRCHK
2066 sensor_read(client, regarray[i].reg, &valchk);
2067 if (valchk != regarray[i].val)
2068 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2074 sensor_write_array_end:
2075 sensor_task_lock(client,0);
2078 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
2086 while (regarray[i].reg != 0)
2088 sensor_read(client, regarray[i].reg, &valchk);
2089 if (valchk != regarray[i].val)
2090 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2096 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2098 struct soc_camera_link *icl = to_soc_camera_link(icd);
2101 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2104 case Sensor_PowerDown:
2106 if (icl->powerdown) {
2107 ret = icl->powerdown(icd->pdev, on);
2108 if (ret == RK29_CAM_IO_SUCCESS) {
2112 icl->reset(icd->pdev);
2114 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2116 goto sensor_power_end;
2123 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2124 struct sensor *sensor = to_sensor(client);
2126 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2127 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2133 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2140 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2142 struct i2c_client *client = sd->priv;
2143 struct soc_camera_device *icd = client->dev.platform_data;
2144 struct sensor *sensor = to_sensor(client);
2145 const struct v4l2_queryctrl *qctrl;
2149 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2151 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2153 goto sensor_INIT_ERR;
2157 if (sensor_task_lock(client,1)<0)
2158 goto sensor_INIT_ERR;
2160 ret = sensor_write(client, 0x01, 0xF9);
2163 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2165 goto sensor_INIT_ERR;
2168 mdelay(5); //delay 5 microseconds
2169 /* check if it is an sensor sensor */
2170 ret = sensor_read(client, 0x04, &value);
2172 SENSOR_TR("read chip id high byte failed\n");
2174 goto sensor_INIT_ERR;
2178 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), value);
2179 if (value == SENSOR_ID) {
2180 sensor->model = SENSOR_V4L2_IDENT;
2182 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), value);
2184 goto sensor_INIT_ERR;
2187 ret = sensor_write_array(client, sensor_init_data);
2190 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2191 goto sensor_INIT_ERR;
2193 sensor_task_lock(client,0);
2194 //icd->user_width = SENSOR_INIT_WIDTH;
2195 //icd->user_height = SENSOR_INIT_HEIGHT;
2196 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
2197 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
2199 /* sensor sensor information for initialization */
2200 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2202 sensor->info_priv.whiteBalance = qctrl->default_value;
2203 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2205 sensor->info_priv.brightness = qctrl->default_value;
2206 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2208 sensor->info_priv.effect = qctrl->default_value;
2209 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2211 sensor->info_priv.exposure = qctrl->default_value;
2213 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2215 sensor->info_priv.saturation = qctrl->default_value;
2216 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2218 sensor->info_priv.contrast = qctrl->default_value;
2219 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2221 sensor->info_priv.mirror = qctrl->default_value;
2222 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2224 sensor->info_priv.flip = qctrl->default_value;
2225 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2227 sensor->info_priv.scene = qctrl->default_value;
2228 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2230 sensor->info_priv.digitalzoom = qctrl->default_value;
2232 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2233 #if CONFIG_SENSOR_Focus
2235 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2237 sensor->info_priv.focus = qctrl->default_value;
2240 #if CONFIG_SENSOR_Flash
2241 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2243 sensor->info_priv.flash = qctrl->default_value;
2246 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);
2250 sensor_task_lock(client,0);
2251 sensor_deactivate(client);
2255 static int sensor_deactivate(struct i2c_client *client)
2257 struct soc_camera_device *icd = client->dev.platform_data;
2259 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2261 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2262 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2264 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2265 icd->user_width = SENSOR_INIT_WIDTH;
2266 icd->user_height = SENSOR_INIT_HEIGHT;
2271 static struct reginfo sensor_power_down_sequence[]=
2275 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2278 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2280 if (pm_msg.event == PM_EVENT_SUSPEND) {
2281 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2282 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2284 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2287 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2289 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2294 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2300 static int sensor_resume(struct soc_camera_device *icd)
2304 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2306 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2310 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2316 static int sensor_set_bus_param(struct soc_camera_device *icd,
2317 unsigned long flags)
2323 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2325 struct soc_camera_link *icl = to_soc_camera_link(icd);
2326 unsigned long flags = SENSOR_BUS_PARAM;
2328 return soc_camera_apply_sensor_flags(icl, flags);
2331 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2333 struct i2c_client *client = sd->priv;
2334 struct soc_camera_device *icd = client->dev.platform_data;
2335 struct sensor *sensor = to_sensor(client);
2336 struct v4l2_pix_format *pix = &f->fmt.pix;
2338 pix->width = icd->user_width;
2339 pix->height = icd->user_height;
2340 pix->pixelformat = sensor->info_priv.pixfmt;
2341 pix->field = V4L2_FIELD_NONE;
2342 pix->colorspace = V4L2_COLORSPACE_JPEG;
2346 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
2350 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
2352 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
2354 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
2356 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
2358 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
2363 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2367 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
2371 if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
2373 } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
2378 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2381 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2383 struct i2c_client *client = sd->priv;
2384 struct sensor *sensor = to_sensor(client);
2385 struct v4l2_pix_format *pix = &f->fmt.pix;
2386 const struct v4l2_queryctrl *qctrl;
2387 struct soc_camera_device *icd = client->dev.platform_data;
2388 struct reginfo *winseqe_set_addr=NULL;
2389 int ret=0, set_w,set_h;
2391 if (sensor->info_priv.pixfmt != pix->pixelformat) {
2392 switch (pix->pixelformat)
2394 case V4L2_PIX_FMT_YUYV:
2396 winseqe_set_addr = sensor_ClrFmt_YUYV;
2399 case V4L2_PIX_FMT_UYVY:
2401 winseqe_set_addr = sensor_ClrFmt_UYVY;
2407 if (winseqe_set_addr != NULL) {
2408 sensor_write_array(client, winseqe_set_addr);
2409 sensor->info_priv.pixfmt = pix->pixelformat;
2411 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2413 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2418 set_h = pix->height;
2420 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg != END_REG))
2422 winseqe_set_addr = sensor_qcif;
2426 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg != END_REG))
2428 winseqe_set_addr = sensor_qvga;
2432 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg != END_REG))
2434 winseqe_set_addr = sensor_cif;
2438 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg != END_REG))
2440 winseqe_set_addr = sensor_vga;
2444 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg != END_REG))
2446 winseqe_set_addr = sensor_svga;
2450 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg != END_REG))
2452 winseqe_set_addr = sensor_xga;
2456 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg != END_REG))
2458 winseqe_set_addr = sensor_sxga;
2462 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg != END_REG))
2464 winseqe_set_addr = sensor_uxga;
2470 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2471 set_w = SENSOR_INIT_WIDTH;
2472 set_h = SENSOR_INIT_HEIGHT;
2474 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
2477 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
2478 #if CONFIG_SENSOR_Flash
2479 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
2480 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2481 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2482 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2484 } else { /* ddl@rock-chips.com : Video */
2485 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2486 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2487 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2491 ret |= sensor_write_array(client, winseqe_set_addr);
2493 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2494 #if CONFIG_SENSOR_Flash
2495 if (sensor_fmt_capturechk(sd,f) == true) {
2496 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2497 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2498 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2502 goto sensor_s_fmt_end;
2505 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
2507 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
2508 #if CONFIG_SENSOR_Effect
2509 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2510 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2512 #if CONFIG_SENSOR_WhiteBalance
2513 if (sensor->info_priv.whiteBalance != 0) {
2514 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2515 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2518 sensor->info_priv.snap2preview = true;
2519 } else if (sensor_fmt_videochk(sd,f) == true) { /* ddl@rock-chips.com : Video */
2520 #if CONFIG_SENSOR_Effect
2521 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2522 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2524 #if CONFIG_SENSOR_WhiteBalance
2525 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2526 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2528 sensor->info_priv.video2preview = true;
2529 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2530 #if CONFIG_SENSOR_Effect
2531 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2532 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2534 #if CONFIG_SENSOR_WhiteBalance
2535 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2536 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2538 sensor->info_priv.video2preview = false;
2539 sensor->info_priv.snap2preview = false;
2542 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2546 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2550 pix->height = set_h;
2556 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2558 struct v4l2_pix_format *pix = &f->fmt.pix;
2559 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
2560 pix->pixelformat == V4L2_PIX_FMT_YUYV;
2563 * With Bayer format enforce even side lengths, but let the user play
2564 * with the starting pixel
2567 if (pix->height > SENSOR_MAX_HEIGHT)
2568 pix->height = SENSOR_MAX_HEIGHT;
2569 else if (pix->height < SENSOR_MIN_HEIGHT)
2570 pix->height = SENSOR_MIN_HEIGHT;
2572 pix->height = ALIGN(pix->height, 2);
2574 if (pix->width > SENSOR_MAX_WIDTH)
2575 pix->width = SENSOR_MAX_WIDTH;
2576 else if (pix->width < SENSOR_MIN_WIDTH)
2577 pix->width = SENSOR_MIN_WIDTH;
2579 pix->width = ALIGN(pix->width, 2);
2584 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2586 struct i2c_client *client = sd->priv;
2588 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2591 if (id->match.addr != client->addr)
2594 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2599 #if CONFIG_SENSOR_Brightness
2600 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2602 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2604 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2606 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2608 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2610 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2613 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2617 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2621 #if CONFIG_SENSOR_Effect
2622 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2624 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2626 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2628 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2630 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2632 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2635 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2639 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2643 #if CONFIG_SENSOR_Exposure
2644 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2646 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2648 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2650 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2652 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2654 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2657 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2661 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2665 #if CONFIG_SENSOR_Saturation
2666 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2668 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2670 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2672 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2674 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2676 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2679 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2683 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2687 #if CONFIG_SENSOR_Contrast
2688 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2690 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2692 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2694 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2696 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2698 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2701 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2705 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2709 #if CONFIG_SENSOR_Mirror
2710 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2712 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2714 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2716 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2718 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2720 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2723 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2727 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2731 #if CONFIG_SENSOR_Flip
2732 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2734 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2736 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2738 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2740 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2742 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2745 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2749 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2753 #if CONFIG_SENSOR_Scene
2754 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2756 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2758 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2760 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2762 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2764 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2767 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2771 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2775 #if CONFIG_SENSOR_WhiteBalance
2776 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2778 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2780 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2782 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2784 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2786 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2789 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2793 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2797 #if CONFIG_SENSOR_DigitalZoom
2798 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2800 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2801 struct sensor *sensor = to_sensor(client);
2802 const struct v4l2_queryctrl *qctrl_info;
2803 int digitalzoom_cur, digitalzoom_total;
2805 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2809 digitalzoom_cur = sensor->info_priv.digitalzoom;
2810 digitalzoom_total = qctrl_info->maximum;
2812 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2814 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2818 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2820 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2824 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2826 *value = digitalzoom_total - digitalzoom_cur;
2829 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2831 *value = 0 - digitalzoom_cur;
2834 digitalzoom_cur += *value;
2836 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2838 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2840 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2843 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2850 #if CONFIG_SENSOR_Flash
2851 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2853 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2854 if (value == 3) { /* ddl@rock-chips.com: torch */
2855 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2857 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2859 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2863 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2868 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2870 struct i2c_client *client = sd->priv;
2871 struct sensor *sensor = to_sensor(client);
2872 const struct v4l2_queryctrl *qctrl;
2874 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2878 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2884 case V4L2_CID_BRIGHTNESS:
2886 ctrl->value = sensor->info_priv.brightness;
2889 case V4L2_CID_SATURATION:
2891 ctrl->value = sensor->info_priv.saturation;
2894 case V4L2_CID_CONTRAST:
2896 ctrl->value = sensor->info_priv.contrast;
2899 case V4L2_CID_DO_WHITE_BALANCE:
2901 ctrl->value = sensor->info_priv.whiteBalance;
2904 case V4L2_CID_EXPOSURE:
2906 ctrl->value = sensor->info_priv.exposure;
2909 case V4L2_CID_HFLIP:
2911 ctrl->value = sensor->info_priv.mirror;
2914 case V4L2_CID_VFLIP:
2916 ctrl->value = sensor->info_priv.flip;
2927 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2929 struct i2c_client *client = sd->priv;
2930 struct sensor *sensor = to_sensor(client);
2931 struct soc_camera_device *icd = client->dev.platform_data;
2932 const struct v4l2_queryctrl *qctrl;
2935 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2939 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2945 #if CONFIG_SENSOR_Brightness
2946 case V4L2_CID_BRIGHTNESS:
2948 if (ctrl->value != sensor->info_priv.brightness)
2950 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2954 sensor->info_priv.brightness = ctrl->value;
2959 #if CONFIG_SENSOR_Exposure
2960 case V4L2_CID_EXPOSURE:
2962 if (ctrl->value != sensor->info_priv.exposure)
2964 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2968 sensor->info_priv.exposure = ctrl->value;
2973 #if CONFIG_SENSOR_Saturation
2974 case V4L2_CID_SATURATION:
2976 if (ctrl->value != sensor->info_priv.saturation)
2978 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2982 sensor->info_priv.saturation = ctrl->value;
2987 #if CONFIG_SENSOR_Contrast
2988 case V4L2_CID_CONTRAST:
2990 if (ctrl->value != sensor->info_priv.contrast)
2992 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2996 sensor->info_priv.contrast = ctrl->value;
3001 #if CONFIG_SENSOR_WhiteBalance
3002 case V4L2_CID_DO_WHITE_BALANCE:
3004 if (ctrl->value != sensor->info_priv.whiteBalance)
3006 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3010 sensor->info_priv.whiteBalance = ctrl->value;
3015 #if CONFIG_SENSOR_Mirror
3016 case V4L2_CID_HFLIP:
3018 if (ctrl->value != sensor->info_priv.mirror)
3020 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3022 sensor->info_priv.mirror = ctrl->value;
3027 #if CONFIG_SENSOR_Flip
3028 case V4L2_CID_VFLIP:
3030 if (ctrl->value != sensor->info_priv.flip)
3032 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3034 sensor->info_priv.flip = ctrl->value;
3045 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3047 const struct v4l2_queryctrl *qctrl;
3048 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3049 struct sensor *sensor = to_sensor(client);
3051 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3055 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3059 switch (ext_ctrl->id)
3061 case V4L2_CID_SCENE:
3063 ext_ctrl->value = sensor->info_priv.scene;
3066 case V4L2_CID_EFFECT:
3068 ext_ctrl->value = sensor->info_priv.effect;
3071 case V4L2_CID_ZOOM_ABSOLUTE:
3073 ext_ctrl->value = sensor->info_priv.digitalzoom;
3076 case V4L2_CID_ZOOM_RELATIVE:
3080 case V4L2_CID_FOCUS_ABSOLUTE:
3082 ext_ctrl->value = sensor->info_priv.focus;
3085 case V4L2_CID_FOCUS_RELATIVE:
3089 case V4L2_CID_FLASH:
3091 ext_ctrl->value = sensor->info_priv.flash;
3099 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3101 const struct v4l2_queryctrl *qctrl;
3102 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3103 struct sensor *sensor = to_sensor(client);
3106 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3110 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3115 switch (ext_ctrl->id)
3117 #if CONFIG_SENSOR_Scene
3118 case V4L2_CID_SCENE:
3120 if (ext_ctrl->value != sensor->info_priv.scene)
3122 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3124 sensor->info_priv.scene = ext_ctrl->value;
3129 #if CONFIG_SENSOR_Effect
3130 case V4L2_CID_EFFECT:
3132 if (ext_ctrl->value != sensor->info_priv.effect)
3134 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3136 sensor->info_priv.effect= ext_ctrl->value;
3141 #if CONFIG_SENSOR_DigitalZoom
3142 case V4L2_CID_ZOOM_ABSOLUTE:
3144 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3147 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3149 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3151 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3153 sensor->info_priv.digitalzoom += val_offset;
3155 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3160 case V4L2_CID_ZOOM_RELATIVE:
3162 if (ext_ctrl->value)
3164 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3166 sensor->info_priv.digitalzoom += ext_ctrl->value;
3168 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3173 #if CONFIG_SENSOR_Focus
3174 case V4L2_CID_FOCUS_ABSOLUTE:
3176 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3179 if (ext_ctrl->value != sensor->info_priv.focus)
3181 val_offset = ext_ctrl->value -sensor->info_priv.focus;
3183 sensor->info_priv.focus += val_offset;
3188 case V4L2_CID_FOCUS_RELATIVE:
3190 if (ext_ctrl->value)
3192 sensor->info_priv.focus += ext_ctrl->value;
3194 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
3199 #if CONFIG_SENSOR_Flash
3200 case V4L2_CID_FLASH:
3202 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3204 sensor->info_priv.flash = ext_ctrl->value;
3206 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3217 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3219 struct i2c_client *client = sd->priv;
3220 struct soc_camera_device *icd = client->dev.platform_data;
3221 int i, error_cnt=0, error_idx=-1;
3224 for (i=0; i<ext_ctrl->count; i++) {
3225 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3232 error_idx = ext_ctrl->count;
3234 if (error_idx != -1) {
3235 ext_ctrl->error_idx = error_idx;
3242 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3244 struct i2c_client *client = sd->priv;
3245 struct soc_camera_device *icd = client->dev.platform_data;
3246 int i, error_cnt=0, error_idx=-1;
3249 for (i=0; i<ext_ctrl->count; i++) {
3250 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3257 error_idx = ext_ctrl->count;
3259 if (error_idx != -1) {
3260 ext_ctrl->error_idx = error_idx;
3267 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3268 * this wasn't our capture interface, so, we wait for the right one */
3269 static int sensor_video_probe(struct soc_camera_device *icd,
3270 struct i2c_client *client)
3274 struct sensor *sensor = to_sensor(client);
3276 /* We must have a parent by now. And it cannot be a wrong one.
3277 * So this entire test is completely redundant. */
3278 if (!icd->dev.parent ||
3279 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3282 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3284 goto sensor_video_probe_err;
3288 ret = sensor_write(client, 0x01, 0xf9);
3291 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3294 mdelay(5); //delay 5 microseconds
3296 /* check if it is an sensor sensor */
3297 ret = sensor_read(client, 0x04, &value);
3299 SENSOR_TR("read chip id high byte failed\n");
3301 goto sensor_video_probe_err;
3304 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), value);
3305 if (value == SENSOR_ID) {
3306 sensor->model = SENSOR_V4L2_IDENT;
3308 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), value);
3310 goto sensor_video_probe_err;
3314 icd->formats = sensor_colour_formats;
3315 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
3319 sensor_video_probe_err:
3323 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3325 struct i2c_client *client = sd->priv;
3326 struct soc_camera_device *icd = client->dev.platform_data;
3327 struct sensor *sensor = to_sensor(client);
3329 #if CONFIG_SENSOR_Flash
3333 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3336 case RK29_CAM_SUBDEV_DEACTIVATE:
3338 sensor_deactivate(client);
3342 case RK29_CAM_SUBDEV_IOREQUEST:
3344 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3345 if (sensor->sensor_io_request != NULL) {
3346 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
3347 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
3348 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
3349 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
3350 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
3351 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
3354 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3356 goto sensor_ioctl_end;
3358 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3360 #if CONFIG_SENSOR_Flash
3361 if (sensor->sensor_gpio_res) {
3362 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3363 for (i = 0; i < icd->ops->num_controls; i++) {
3364 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3365 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3368 sensor->info_priv.flash = 0xff;
3369 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3377 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3385 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3386 .init = sensor_init,
3387 .g_ctrl = sensor_g_control,
3388 .s_ctrl = sensor_s_control,
3389 .g_ext_ctrls = sensor_g_ext_controls,
3390 .s_ext_ctrls = sensor_s_ext_controls,
3391 .g_chip_ident = sensor_g_chip_ident,
3392 .ioctl = sensor_ioctl,
3395 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3396 .s_fmt = sensor_s_fmt,
3397 .g_fmt = sensor_g_fmt,
3398 .try_fmt = sensor_try_fmt,
3401 static struct v4l2_subdev_ops sensor_subdev_ops = {
3402 .core = &sensor_subdev_core_ops,
3403 .video = &sensor_subdev_video_ops,
3406 static int sensor_probe(struct i2c_client *client,
3407 const struct i2c_device_id *did)
3409 struct sensor *sensor;
3410 struct soc_camera_device *icd = client->dev.platform_data;
3411 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3412 struct soc_camera_link *icl;
3415 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3417 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3421 icl = to_soc_camera_link(icd);
3423 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3427 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3428 dev_warn(&adapter->dev,
3429 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3433 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3437 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3439 /* Second stage probe - when a capture adapter is there */
3440 icd->ops = &sensor_ops;
3441 icd->y_skip_top = 0;
3442 #if CONFIG_SENSOR_I2C_NOSCHED
3443 atomic_set(&sensor->tasklock_cnt,0);
3446 ret = sensor_video_probe(icd, client);
3449 i2c_set_clientdata(client, NULL);
3453 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3457 static int sensor_remove(struct i2c_client *client)
3459 struct sensor *sensor = to_sensor(client);
3460 struct soc_camera_device *icd = client->dev.platform_data;
3463 i2c_set_clientdata(client, NULL);
3464 client->driver = NULL;
3470 static const struct i2c_device_id sensor_id[] = {
3471 {SENSOR_NAME_STRING(), 0 },
3474 MODULE_DEVICE_TABLE(i2c, sensor_id);
3476 static struct i2c_driver sensor_i2c_driver = {
3478 .name = SENSOR_NAME_STRING(),
3480 .probe = sensor_probe,
3481 .remove = sensor_remove,
3482 .id_table = sensor_id,
3485 static int __init sensor_mod_init(void)
3487 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3488 return i2c_add_driver(&sensor_i2c_driver);
3491 static void __exit sensor_mod_exit(void)
3493 i2c_del_driver(&sensor_i2c_driver);
3496 device_initcall_sync(sensor_mod_init);
3497 module_exit(sensor_mod_exit);
3499 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3500 MODULE_AUTHOR("lxh@wisky.com.cn");
3501 MODULE_LICENSE("GPL");