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_MBUS_FMT_UYVY8_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_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)
92 #define SENSOR_AF_IS_ERR (0x00<<0)
93 #define SENSOR_AF_IS_OK (0x01<<0)
94 #define SENSOR_INIT_IS_ERR (0x00<<28)
95 #define SENSOR_INIT_IS_OK (0x01<<28)
104 /* init SVGA preview */
105 static struct reginfo sensor_init_data[] =
336 {0x11, 0x03}, //lxh,normal
378 {0x10, 0x7f},//lxh,3f
1144 /* 1600X1200 UXGA capture */
1145 static struct reginfo sensor_uxga[] =
1175 /* 1280X1024 SXGA */
1176 static struct reginfo sensor_sxga[] =
1180 static struct reginfo sensor_xga[] =
1185 /* 800X600 SVGA,30fps*/
1186 static struct reginfo sensor_svga[] =
1194 {0x03, 0x12}, //Function
1200 {0x03, 0x13}, //Function
1201 {0x80, 0xfd}, //Function
1228 static struct reginfo sensor_vga[] =
1234 static struct reginfo sensor_cif[] =
1238 static struct reginfo sensor_qvga[] =
1242 static struct reginfo sensor_qcif[] =
1246 static struct reginfo sensor_ClrFmt_YUYV[]=
1252 static struct reginfo sensor_ClrFmt_UYVY[]=
1258 #if CONFIG_SENSOR_WhiteBalance
1259 static struct reginfo sensor_WhiteB_Auto[]=
1273 /* Cloudy Colour Temperature : 6500K - 8000K */
1274 static struct reginfo sensor_WhiteB_Cloudy[]=
1288 /* ClearDay Colour Temperature : 5000K - 6500K */
1289 static struct reginfo sensor_WhiteB_ClearDay[]=
1303 /* Office Colour Temperature : 3500K - 5000K */
1304 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
1319 /* Home Colour Temperature : 2500K - 3500K */
1320 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
1334 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
1335 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
1339 #if CONFIG_SENSOR_Brightness
1340 static struct reginfo sensor_Brightness0[]=
1348 static struct reginfo sensor_Brightness1[]=
1356 static struct reginfo sensor_Brightness2[]=
1364 static struct reginfo sensor_Brightness3[]=
1372 static struct reginfo sensor_Brightness4[]=
1380 static struct reginfo sensor_Brightness5[]=
1387 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
1388 sensor_Brightness4, sensor_Brightness5,NULL,
1393 #if CONFIG_SENSOR_Effect
1394 static struct reginfo sensor_Effect_Normal[] =
1403 static struct reginfo sensor_Effect_WandB[] =
1415 static struct reginfo sensor_Effect_Sepia[] =
1427 static struct reginfo sensor_Effect_Negative[] =
1440 static struct reginfo sensor_Effect_Bluish[] =
1453 static struct reginfo sensor_Effect_Green[] =
1465 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
1466 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
1469 #if CONFIG_SENSOR_Exposure
1470 static struct reginfo sensor_Exposure0[]=
1476 static struct reginfo sensor_Exposure1[]=
1483 static struct reginfo sensor_Exposure2[]=
1489 static struct reginfo sensor_Exposure3[]=
1495 static struct reginfo sensor_Exposure4[]=
1502 static struct reginfo sensor_Exposure5[]=
1508 static struct reginfo sensor_Exposure6[]=
1515 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1516 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1519 #if CONFIG_SENSOR_Saturation
1520 static struct reginfo sensor_Saturation0[]=
1526 static struct reginfo sensor_Saturation1[]=
1532 static struct reginfo sensor_Saturation2[]=
1536 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1539 #if CONFIG_SENSOR_Contrast
1540 static struct reginfo sensor_Contrast0[]=
1547 static struct reginfo sensor_Contrast1[]=
1554 static struct reginfo sensor_Contrast2[]=
1561 static struct reginfo sensor_Contrast3[]=
1568 static struct reginfo sensor_Contrast4[]=
1576 static struct reginfo sensor_Contrast5[]=
1583 static struct reginfo sensor_Contrast6[]=
1589 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1590 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1594 #if CONFIG_SENSOR_Mirror
1595 static struct reginfo sensor_MirrorOn[]=
1601 static struct reginfo sensor_MirrorOff[]=
1606 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1608 #if CONFIG_SENSOR_Flip
1609 static struct reginfo sensor_FlipOn[]=
1615 static struct reginfo sensor_FlipOff[]=
1620 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1623 #if CONFIG_SENSOR_Scene
1624 static struct reginfo sensor_SceneAuto[] =
1630 static struct reginfo sensor_SceneNight[] =
1635 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1638 #if CONFIG_SENSOR_DigitalZoom
1639 static struct reginfo sensor_Zoom0[] =
1644 static struct reginfo sensor_Zoom1[] =
1649 static struct reginfo sensor_Zoom2[] =
1655 static struct reginfo sensor_Zoom3[] =
1659 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1661 static const struct v4l2_querymenu sensor_menus[] =
1663 #if CONFIG_SENSOR_WhiteBalance
1664 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1665 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1666 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1669 #if CONFIG_SENSOR_Effect
1670 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1671 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1672 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1675 #if CONFIG_SENSOR_Scene
1676 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1679 #if CONFIG_SENSOR_Flash
1680 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1681 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1685 static const struct v4l2_queryctrl sensor_controls[] =
1687 #if CONFIG_SENSOR_WhiteBalance
1689 .id = V4L2_CID_DO_WHITE_BALANCE,
1690 .type = V4L2_CTRL_TYPE_MENU,
1691 .name = "White Balance Control",
1699 #if CONFIG_SENSOR_Brightness
1701 .id = V4L2_CID_BRIGHTNESS,
1702 .type = V4L2_CTRL_TYPE_INTEGER,
1703 .name = "Brightness Control",
1711 #if CONFIG_SENSOR_Effect
1713 .id = V4L2_CID_EFFECT,
1714 .type = V4L2_CTRL_TYPE_MENU,
1715 .name = "Effect Control",
1723 #if CONFIG_SENSOR_Exposure
1725 .id = V4L2_CID_EXPOSURE,
1726 .type = V4L2_CTRL_TYPE_INTEGER,
1727 .name = "Exposure Control",
1735 #if CONFIG_SENSOR_Saturation
1737 .id = V4L2_CID_SATURATION,
1738 .type = V4L2_CTRL_TYPE_INTEGER,
1739 .name = "Saturation Control",
1747 #if CONFIG_SENSOR_Contrast
1749 .id = V4L2_CID_CONTRAST,
1750 .type = V4L2_CTRL_TYPE_INTEGER,
1751 .name = "Contrast Control",
1759 #if CONFIG_SENSOR_Mirror
1761 .id = V4L2_CID_HFLIP,
1762 .type = V4L2_CTRL_TYPE_BOOLEAN,
1763 .name = "Mirror Control",
1771 #if CONFIG_SENSOR_Flip
1773 .id = V4L2_CID_VFLIP,
1774 .type = V4L2_CTRL_TYPE_BOOLEAN,
1775 .name = "Flip Control",
1783 #if CONFIG_SENSOR_Scene
1785 .id = V4L2_CID_SCENE,
1786 .type = V4L2_CTRL_TYPE_MENU,
1787 .name = "Scene Control",
1795 #if CONFIG_SENSOR_DigitalZoom
1797 .id = V4L2_CID_ZOOM_RELATIVE,
1798 .type = V4L2_CTRL_TYPE_INTEGER,
1799 .name = "DigitalZoom Control",
1805 .id = V4L2_CID_ZOOM_ABSOLUTE,
1806 .type = V4L2_CTRL_TYPE_INTEGER,
1807 .name = "DigitalZoom Control",
1815 #if CONFIG_SENSOR_Focus
1817 .id = V4L2_CID_FOCUS_RELATIVE,
1818 .type = V4L2_CTRL_TYPE_INTEGER,
1819 .name = "Focus Control",
1825 .id = V4L2_CID_FOCUS_ABSOLUTE,
1826 .type = V4L2_CTRL_TYPE_INTEGER,
1827 .name = "Focus Control",
1831 .default_value = 125,
1835 #if CONFIG_SENSOR_Flash
1837 .id = V4L2_CID_FLASH,
1838 .type = V4L2_CTRL_TYPE_MENU,
1839 .name = "Flash Control",
1848 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1849 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1850 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1851 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1852 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1853 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1854 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1855 static int sensor_resume(struct soc_camera_device *icd);
1856 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1857 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1858 #if CONFIG_SENSOR_Effect
1859 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1861 #if CONFIG_SENSOR_WhiteBalance
1862 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1864 static int sensor_deactivate(struct i2c_client *client);
1866 static struct soc_camera_ops sensor_ops =
1868 .suspend = sensor_suspend,
1869 .resume = sensor_resume,
1870 .set_bus_param = sensor_set_bus_param,
1871 .query_bus_param = sensor_query_bus_param,
1872 .controls = sensor_controls,
1873 .menus = sensor_menus,
1874 .num_controls = ARRAY_SIZE(sensor_controls),
1875 .num_menus = ARRAY_SIZE(sensor_menus),
1878 /* only one fixed colorspace per pixelcode */
1879 struct sensor_datafmt {
1880 enum v4l2_mbus_pixelcode code;
1881 enum v4l2_colorspace colorspace;
1884 /* Find a data format by a pixel code in an array */
1885 static const struct sensor_datafmt *sensor_find_datafmt(
1886 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1890 for (i = 0; i < n; i++)
1891 if (fmt[i].code == code)
1897 static const struct sensor_datafmt sensor_colour_fmts[] = {
1898 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1899 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1902 typedef struct sensor_info_priv_s
1916 unsigned char mirror; /* HFLIP */
1917 unsigned char flip; /* VFLIP */
1918 unsigned int winseqe_cur_addr;
1919 struct sensor_datafmt fmt;
1920 unsigned int funmodule_state;
1922 } sensor_info_priv_t;
1926 struct v4l2_subdev subdev;
1927 struct i2c_client *client;
1928 sensor_info_priv_t info_priv;
1929 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1930 #if CONFIG_SENSOR_I2C_NOSCHED
1931 atomic_t tasklock_cnt;
1933 struct rk29camera_platform_data *sensor_io_request;
1934 struct rk29camera_gpio_res *sensor_gpio_res;
1937 static struct sensor* to_sensor(const struct i2c_client *client)
1939 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1942 static int sensor_task_lock(struct i2c_client *client, int lock)
1944 #if CONFIG_SENSOR_I2C_NOSCHED
1946 struct sensor *sensor = to_sensor(client);
1949 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1950 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1951 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1955 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1956 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1957 goto sensor_task_lock_err;
1962 atomic_add(1, &sensor->tasklock_cnt);
1964 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1965 atomic_sub(1, &sensor->tasklock_cnt);
1967 if (atomic_read(&sensor->tasklock_cnt) == 0)
1972 sensor_task_lock_err:
1979 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1983 struct i2c_msg msg[1];
1988 msg->addr = client->addr;
1989 msg->flags = client->flags;
1991 msg->len = sizeof(buf);
1992 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1993 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1998 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1999 err = i2c_transfer(client->adapter, msg, 1);
2004 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
2012 /* sensor register read */
2013 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
2017 struct i2c_msg msg[2];
2021 msg[0].addr = client->addr;
2022 msg[0].flags = client->flags;
2024 msg[0].len = sizeof(buf);
2025 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2026 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2028 msg[1].addr = client->addr;
2029 msg[1].flags = client->flags|I2C_M_RD;
2032 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
2033 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2037 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
2038 err = i2c_transfer(client->adapter, msg, 2);
2044 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
2053 /* write a array of registers */
2054 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
2058 #if CONFIG_SENSOR_I2C_RDWRCHK
2063 if (sensor_task_lock(client, 1) < 0)
2064 goto sensor_write_array_end;
2065 while (regarray[i].reg != END_REG)
2067 err = sensor_write(client, regarray[i].reg, regarray[i].val);
2071 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
2075 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
2077 goto sensor_write_array_end;
2080 #if CONFIG_SENSOR_I2C_RDWRCHK
2082 sensor_read(client, regarray[i].reg, &valchk);
2083 if (valchk != regarray[i].val)
2084 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2090 sensor_write_array_end:
2091 sensor_task_lock(client,0);
2094 #if CONFIG_SENSOR_I2C_RDWRCHK
2095 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
2103 while (regarray[i].reg != 0)
2105 sensor_read(client, regarray[i].reg, &valchk);
2106 if (valchk != regarray[i].val)
2107 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2114 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2116 struct soc_camera_link *icl = to_soc_camera_link(icd);
2119 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2122 case Sensor_PowerDown:
2124 if (icl->powerdown) {
2125 ret = icl->powerdown(icd->pdev, on);
2126 if (ret == RK29_CAM_IO_SUCCESS) {
2130 icl->reset(icd->pdev);
2132 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2134 goto sensor_power_end;
2141 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2142 struct sensor *sensor = to_sensor(client);
2144 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2145 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2151 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2158 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2160 struct i2c_client *client = sd->priv;
2161 struct soc_camera_device *icd = client->dev.platform_data;
2162 struct sensor *sensor = to_sensor(client);
2163 const struct v4l2_queryctrl *qctrl;
2164 const struct sensor_datafmt *fmt;
2168 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2170 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2172 goto sensor_INIT_ERR;
2176 if (sensor_task_lock(client,1)<0)
2177 goto sensor_INIT_ERR;
2179 ret = sensor_write(client, 0x01, 0xF9);
2182 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2184 goto sensor_INIT_ERR;
2187 mdelay(5); //delay 5 microseconds
2188 /* check if it is an sensor sensor */
2189 ret = sensor_read(client, 0x04, &value);
2191 SENSOR_TR("read chip id high byte failed\n");
2193 goto sensor_INIT_ERR;
2197 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), value);
2198 if (value == SENSOR_ID) {
2199 sensor->model = SENSOR_V4L2_IDENT;
2201 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), value);
2203 goto sensor_INIT_ERR;
2206 ret = sensor_write_array(client, sensor_init_data);
2209 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2210 goto sensor_INIT_ERR;
2212 sensor_task_lock(client,0);
2213 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
2214 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2216 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2218 goto sensor_INIT_ERR;
2220 sensor->info_priv.fmt = *fmt;
2222 /* sensor sensor information for initialization */
2223 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2225 sensor->info_priv.whiteBalance = qctrl->default_value;
2226 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2228 sensor->info_priv.brightness = qctrl->default_value;
2229 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2231 sensor->info_priv.effect = qctrl->default_value;
2232 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2234 sensor->info_priv.exposure = qctrl->default_value;
2236 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2238 sensor->info_priv.saturation = qctrl->default_value;
2239 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2241 sensor->info_priv.contrast = qctrl->default_value;
2242 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2244 sensor->info_priv.mirror = qctrl->default_value;
2245 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2247 sensor->info_priv.flip = qctrl->default_value;
2248 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2250 sensor->info_priv.scene = qctrl->default_value;
2251 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2253 sensor->info_priv.digitalzoom = qctrl->default_value;
2255 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2256 #if CONFIG_SENSOR_Focus
2258 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2260 sensor->info_priv.focus = qctrl->default_value;
2263 #if CONFIG_SENSOR_Flash
2264 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2266 sensor->info_priv.flash = qctrl->default_value;
2269 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);
2271 sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
2274 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2275 sensor_task_lock(client,0);
2276 sensor_deactivate(client);
2280 static int sensor_deactivate(struct i2c_client *client)
2282 struct soc_camera_device *icd = client->dev.platform_data;
2283 struct sensor *sensor = to_sensor(client);
2284 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2286 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2287 sensor_ioctrl(icd, Sensor_PowerDown, 1);
2289 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2290 icd->user_width = SENSOR_INIT_WIDTH;
2291 icd->user_height = SENSOR_INIT_HEIGHT;
2293 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2297 static struct reginfo sensor_power_down_sequence[]=
2301 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2304 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2306 if (pm_msg.event == PM_EVENT_SUSPEND) {
2307 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2308 ret = sensor_write_array(client, sensor_power_down_sequence) ;
2310 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2313 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2315 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2320 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2326 static int sensor_resume(struct soc_camera_device *icd)
2330 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2332 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2336 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2342 static int sensor_set_bus_param(struct soc_camera_device *icd,
2343 unsigned long flags)
2349 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2351 struct soc_camera_link *icl = to_soc_camera_link(icd);
2352 unsigned long flags = SENSOR_BUS_PARAM;
2354 return soc_camera_apply_sensor_flags(icl, flags);
2357 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2359 struct i2c_client *client = sd->priv;
2360 struct soc_camera_device *icd = client->dev.platform_data;
2361 struct sensor *sensor = to_sensor(client);
2363 mf->width = icd->user_width;
2364 mf->height = icd->user_height;
2365 mf->code = sensor->info_priv.fmt.code;
2366 mf->colorspace = sensor->info_priv.fmt.colorspace;
2367 mf->field = V4L2_FIELD_NONE;
2371 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2375 if ((mf->width == 1024) && (mf->height == 768)) {
2377 } else if ((mf->width == 1280) && (mf->height == 1024)) {
2379 } else if ((mf->width == 1600) && (mf->height == 1200)) {
2381 } else if ((mf->width == 2048) && (mf->height == 1536)) {
2383 } else if ((mf->width == 2592) && (mf->height == 1944)) {
2388 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2392 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2396 if ((mf->width == 1280) && (mf->height == 720)) {
2398 } else if ((mf->width == 1920) && (mf->height == 1080)) {
2403 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2406 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2408 struct i2c_client *client = sd->priv;
2409 struct sensor *sensor = to_sensor(client);
2410 const struct sensor_datafmt *fmt;
2411 const struct v4l2_queryctrl *qctrl;
2412 struct soc_camera_device *icd = client->dev.platform_data;
2413 struct reginfo *winseqe_set_addr=NULL;
2414 int ret=0, set_w,set_h;
2416 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2417 ARRAY_SIZE(sensor_colour_fmts));
2420 goto sensor_s_fmt_end;
2423 if (sensor->info_priv.fmt.code != mf->code) {
2426 case V4L2_MBUS_FMT_YUYV8_2X8:
2428 winseqe_set_addr = sensor_ClrFmt_YUYV;
2431 case V4L2_MBUS_FMT_UYVY8_2X8:
2433 winseqe_set_addr = sensor_ClrFmt_UYVY;
2439 if (winseqe_set_addr != NULL) {
2440 sensor_write_array(client, winseqe_set_addr);
2441 sensor->info_priv.fmt.code = mf->code;
2442 sensor->info_priv.fmt.colorspace= mf->colorspace;
2443 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2445 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2452 if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg != END_REG))
2454 winseqe_set_addr = sensor_qcif;
2458 else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg != END_REG))
2460 winseqe_set_addr = sensor_qvga;
2464 else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg != END_REG))
2466 winseqe_set_addr = sensor_cif;
2470 else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg != END_REG))
2472 winseqe_set_addr = sensor_vga;
2476 else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg != END_REG))
2478 winseqe_set_addr = sensor_svga;
2482 else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg != END_REG))
2484 winseqe_set_addr = sensor_xga;
2488 else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg != END_REG))
2490 winseqe_set_addr = sensor_sxga;
2494 else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg != END_REG))
2496 winseqe_set_addr = sensor_uxga;
2502 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
2503 set_w = SENSOR_INIT_WIDTH;
2504 set_h = SENSOR_INIT_HEIGHT;
2506 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2509 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
2510 #if CONFIG_SENSOR_Flash
2511 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2512 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2513 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2514 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2516 } else { /* ddl@rock-chips.com : Video */
2517 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2518 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2519 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2523 ret |= sensor_write_array(client, winseqe_set_addr);
2525 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2526 #if CONFIG_SENSOR_Flash
2527 if (sensor_fmt_capturechk(sd,f) == true) {
2528 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2529 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2530 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2534 goto sensor_s_fmt_end;
2537 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
2539 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
2540 #if CONFIG_SENSOR_Effect
2541 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2542 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2544 #if CONFIG_SENSOR_WhiteBalance
2545 if (sensor->info_priv.whiteBalance != 0) {
2546 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2547 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2550 sensor->info_priv.snap2preview = true;
2551 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
2552 #if CONFIG_SENSOR_Effect
2553 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2554 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2556 #if CONFIG_SENSOR_WhiteBalance
2557 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2558 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2560 sensor->info_priv.video2preview = true;
2561 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2562 #if CONFIG_SENSOR_Effect
2563 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2564 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2566 #if CONFIG_SENSOR_WhiteBalance
2567 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2568 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2570 sensor->info_priv.video2preview = false;
2571 sensor->info_priv.snap2preview = false;
2574 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2578 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2588 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2590 struct i2c_client *client = sd->priv;
2591 struct sensor *sensor = to_sensor(client);
2592 const struct sensor_datafmt *fmt;
2595 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2596 ARRAY_SIZE(sensor_colour_fmts));
2598 fmt = &sensor->info_priv.fmt;
2599 mf->code = fmt->code;
2602 if (mf->height > SENSOR_MAX_HEIGHT)
2603 mf->height = SENSOR_MAX_HEIGHT;
2604 else if (mf->height < SENSOR_MIN_HEIGHT)
2605 mf->height = SENSOR_MIN_HEIGHT;
2607 if (mf->width > SENSOR_MAX_WIDTH)
2608 mf->width = SENSOR_MAX_WIDTH;
2609 else if (mf->width < SENSOR_MIN_WIDTH)
2610 mf->width = SENSOR_MIN_WIDTH;
2612 mf->colorspace = fmt->colorspace;
2617 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2619 struct i2c_client *client = sd->priv;
2621 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2624 if (id->match.addr != client->addr)
2627 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
2632 #if CONFIG_SENSOR_Brightness
2633 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2635 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2637 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2639 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2641 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2643 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2646 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2650 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2654 #if CONFIG_SENSOR_Effect
2655 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2657 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2659 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2661 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2663 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2665 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2668 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2672 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2676 #if CONFIG_SENSOR_Exposure
2677 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2679 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2681 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2683 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2685 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2687 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2690 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2694 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2698 #if CONFIG_SENSOR_Saturation
2699 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2701 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2703 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2705 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2707 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2709 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2712 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2716 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2720 #if CONFIG_SENSOR_Contrast
2721 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2723 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2725 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2727 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2729 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2731 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2734 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2738 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2742 #if CONFIG_SENSOR_Mirror
2743 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2745 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2747 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2749 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2751 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2753 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2756 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2760 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2764 #if CONFIG_SENSOR_Flip
2765 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2767 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2769 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2771 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2773 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2775 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2778 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2782 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2786 #if CONFIG_SENSOR_Scene
2787 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2789 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2791 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2793 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2795 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2797 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2800 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2804 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2808 #if CONFIG_SENSOR_WhiteBalance
2809 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2811 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2813 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2815 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2817 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2819 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2822 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2826 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2830 #if CONFIG_SENSOR_DigitalZoom
2831 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2833 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2834 struct sensor *sensor = to_sensor(client);
2835 const struct v4l2_queryctrl *qctrl_info;
2836 int digitalzoom_cur, digitalzoom_total;
2838 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2842 digitalzoom_cur = sensor->info_priv.digitalzoom;
2843 digitalzoom_total = qctrl_info->maximum;
2845 if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
2847 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2851 if ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2853 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2857 if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
2859 *value = digitalzoom_total - digitalzoom_cur;
2862 if ((value < 0) && ((digitalzoom_cur + value) < 0))
2864 value = 0 - digitalzoom_cur;
2867 digitalzoom_cur += value;
2869 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2871 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2873 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2876 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2883 #if CONFIG_SENSOR_Flash
2884 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2886 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2887 if (value == 3) { /* ddl@rock-chips.com: torch */
2888 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2890 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2892 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2896 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2901 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2903 struct i2c_client *client = sd->priv;
2904 struct sensor *sensor = to_sensor(client);
2905 const struct v4l2_queryctrl *qctrl;
2907 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2911 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2917 case V4L2_CID_BRIGHTNESS:
2919 ctrl->value = sensor->info_priv.brightness;
2922 case V4L2_CID_SATURATION:
2924 ctrl->value = sensor->info_priv.saturation;
2927 case V4L2_CID_CONTRAST:
2929 ctrl->value = sensor->info_priv.contrast;
2932 case V4L2_CID_DO_WHITE_BALANCE:
2934 ctrl->value = sensor->info_priv.whiteBalance;
2937 case V4L2_CID_EXPOSURE:
2939 ctrl->value = sensor->info_priv.exposure;
2942 case V4L2_CID_HFLIP:
2944 ctrl->value = sensor->info_priv.mirror;
2947 case V4L2_CID_VFLIP:
2949 ctrl->value = sensor->info_priv.flip;
2960 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2962 struct i2c_client *client = sd->priv;
2963 struct sensor *sensor = to_sensor(client);
2964 struct soc_camera_device *icd = client->dev.platform_data;
2965 const struct v4l2_queryctrl *qctrl;
2968 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2972 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2978 #if CONFIG_SENSOR_Brightness
2979 case V4L2_CID_BRIGHTNESS:
2981 if (ctrl->value != sensor->info_priv.brightness)
2983 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2987 sensor->info_priv.brightness = ctrl->value;
2992 #if CONFIG_SENSOR_Exposure
2993 case V4L2_CID_EXPOSURE:
2995 if (ctrl->value != sensor->info_priv.exposure)
2997 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3001 sensor->info_priv.exposure = ctrl->value;
3006 #if CONFIG_SENSOR_Saturation
3007 case V4L2_CID_SATURATION:
3009 if (ctrl->value != sensor->info_priv.saturation)
3011 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3015 sensor->info_priv.saturation = ctrl->value;
3020 #if CONFIG_SENSOR_Contrast
3021 case V4L2_CID_CONTRAST:
3023 if (ctrl->value != sensor->info_priv.contrast)
3025 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3029 sensor->info_priv.contrast = ctrl->value;
3034 #if CONFIG_SENSOR_WhiteBalance
3035 case V4L2_CID_DO_WHITE_BALANCE:
3037 if (ctrl->value != sensor->info_priv.whiteBalance)
3039 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3043 sensor->info_priv.whiteBalance = ctrl->value;
3048 #if CONFIG_SENSOR_Mirror
3049 case V4L2_CID_HFLIP:
3051 if (ctrl->value != sensor->info_priv.mirror)
3053 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3055 sensor->info_priv.mirror = ctrl->value;
3060 #if CONFIG_SENSOR_Flip
3061 case V4L2_CID_VFLIP:
3063 if (ctrl->value != sensor->info_priv.flip)
3065 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3067 sensor->info_priv.flip = ctrl->value;
3078 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3080 const struct v4l2_queryctrl *qctrl;
3081 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3082 struct sensor *sensor = to_sensor(client);
3084 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3088 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3092 switch (ext_ctrl->id)
3094 case V4L2_CID_SCENE:
3096 ext_ctrl->value = sensor->info_priv.scene;
3099 case V4L2_CID_EFFECT:
3101 ext_ctrl->value = sensor->info_priv.effect;
3104 case V4L2_CID_ZOOM_ABSOLUTE:
3106 ext_ctrl->value = sensor->info_priv.digitalzoom;
3109 case V4L2_CID_ZOOM_RELATIVE:
3113 case V4L2_CID_FOCUS_ABSOLUTE:
3115 ext_ctrl->value = sensor->info_priv.focus;
3118 case V4L2_CID_FOCUS_RELATIVE:
3122 case V4L2_CID_FLASH:
3124 ext_ctrl->value = sensor->info_priv.flash;
3132 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3134 const struct v4l2_queryctrl *qctrl;
3135 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3136 struct sensor *sensor = to_sensor(client);
3139 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3143 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3148 switch (ext_ctrl->id)
3150 #if CONFIG_SENSOR_Scene
3151 case V4L2_CID_SCENE:
3153 if (ext_ctrl->value != sensor->info_priv.scene)
3155 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3157 sensor->info_priv.scene = ext_ctrl->value;
3162 #if CONFIG_SENSOR_Effect
3163 case V4L2_CID_EFFECT:
3165 if (ext_ctrl->value != sensor->info_priv.effect)
3167 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3169 sensor->info_priv.effect= ext_ctrl->value;
3174 #if CONFIG_SENSOR_DigitalZoom
3175 case V4L2_CID_ZOOM_ABSOLUTE:
3177 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3180 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3182 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3184 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3186 sensor->info_priv.digitalzoom += val_offset;
3188 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3193 case V4L2_CID_ZOOM_RELATIVE:
3195 if (ext_ctrl->value)
3197 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3199 sensor->info_priv.digitalzoom += ext_ctrl->value;
3201 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3206 #if CONFIG_SENSOR_Focus
3207 case V4L2_CID_FOCUS_ABSOLUTE:
3209 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3212 if (ext_ctrl->value != sensor->info_priv.focus)
3214 val_offset = ext_ctrl->value -sensor->info_priv.focus;
3216 sensor->info_priv.focus += val_offset;
3221 case V4L2_CID_FOCUS_RELATIVE:
3223 if (ext_ctrl->value)
3225 sensor->info_priv.focus += ext_ctrl->value;
3227 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
3232 #if CONFIG_SENSOR_Flash
3233 case V4L2_CID_FLASH:
3235 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3237 sensor->info_priv.flash = ext_ctrl->value;
3239 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3250 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3252 struct i2c_client *client = sd->priv;
3253 struct soc_camera_device *icd = client->dev.platform_data;
3254 int i, error_cnt=0, error_idx=-1;
3257 for (i=0; i<ext_ctrl->count; i++) {
3258 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3265 error_idx = ext_ctrl->count;
3267 if (error_idx != -1) {
3268 ext_ctrl->error_idx = error_idx;
3275 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3277 struct i2c_client *client = sd->priv;
3278 struct soc_camera_device *icd = client->dev.platform_data;
3279 int i, error_cnt=0, error_idx=-1;
3282 for (i=0; i<ext_ctrl->count; i++) {
3283 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3290 error_idx = ext_ctrl->count;
3292 if (error_idx != -1) {
3293 ext_ctrl->error_idx = error_idx;
3300 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3301 * this wasn't our capture interface, so, we wait for the right one */
3302 static int sensor_video_probe(struct soc_camera_device *icd,
3303 struct i2c_client *client)
3307 struct sensor *sensor = to_sensor(client);
3309 /* We must have a parent by now. And it cannot be a wrong one.
3310 * So this entire test is completely redundant. */
3311 if (!icd->dev.parent ||
3312 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3315 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3317 goto sensor_video_probe_err;
3321 ret = sensor_write(client, 0x01, 0xf9);
3324 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3327 mdelay(5); //delay 5 microseconds
3329 /* check if it is an sensor sensor */
3330 ret = sensor_read(client, 0x04, &value);
3332 SENSOR_TR("read chip id high byte failed\n");
3334 goto sensor_video_probe_err;
3337 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), value);
3338 if (value == SENSOR_ID) {
3339 sensor->model = SENSOR_V4L2_IDENT;
3341 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), value);
3343 goto sensor_video_probe_err;
3348 sensor_video_probe_err:
3352 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3354 struct i2c_client *client = sd->priv;
3355 struct soc_camera_device *icd = client->dev.platform_data;
3356 struct sensor *sensor = to_sensor(client);
3358 #if CONFIG_SENSOR_Flash
3362 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3365 case RK29_CAM_SUBDEV_DEACTIVATE:
3367 sensor_deactivate(client);
3371 case RK29_CAM_SUBDEV_IOREQUEST:
3373 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
3374 if (sensor->sensor_io_request != NULL) {
3375 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
3376 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
3377 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
3378 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
3379 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
3380 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
3383 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3385 goto sensor_ioctl_end;
3387 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
3389 #if CONFIG_SENSOR_Flash
3390 if (sensor->sensor_gpio_res) {
3391 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3392 for (i = 0; i < icd->ops->num_controls; i++) {
3393 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3394 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
3397 sensor->info_priv.flash = 0xff;
3398 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3406 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3414 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3415 enum v4l2_mbus_pixelcode *code)
3417 if (index >= ARRAY_SIZE(sensor_colour_fmts))
3420 *code = sensor_colour_fmts[index].code;
3423 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3424 .init = sensor_init,
3425 .g_ctrl = sensor_g_control,
3426 .s_ctrl = sensor_s_control,
3427 .g_ext_ctrls = sensor_g_ext_controls,
3428 .s_ext_ctrls = sensor_s_ext_controls,
3429 .g_chip_ident = sensor_g_chip_ident,
3430 .ioctl = sensor_ioctl,
3434 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3435 .s_mbus_fmt = sensor_s_fmt,
3436 .g_mbus_fmt = sensor_g_fmt,
3437 .try_mbus_fmt = sensor_try_fmt,
3438 .enum_mbus_fmt = sensor_enum_fmt,
3441 static struct v4l2_subdev_ops sensor_subdev_ops = {
3442 .core = &sensor_subdev_core_ops,
3443 .video = &sensor_subdev_video_ops,
3446 static int sensor_probe(struct i2c_client *client,
3447 const struct i2c_device_id *did)
3449 struct sensor *sensor;
3450 struct soc_camera_device *icd = client->dev.platform_data;
3451 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3452 struct soc_camera_link *icl;
3455 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3457 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3461 icl = to_soc_camera_link(icd);
3463 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3467 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3468 dev_warn(&adapter->dev,
3469 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3473 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3477 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3479 /* Second stage probe - when a capture adapter is there */
3480 icd->ops = &sensor_ops;
3481 sensor->info_priv.fmt = sensor_colour_fmts[0];
3482 #if CONFIG_SENSOR_I2C_NOSCHED
3483 atomic_set(&sensor->tasklock_cnt,0);
3486 ret = sensor_video_probe(icd, client);
3489 i2c_set_clientdata(client, NULL);
3493 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3497 static int sensor_remove(struct i2c_client *client)
3499 struct sensor *sensor = to_sensor(client);
3500 struct soc_camera_device *icd = client->dev.platform_data;
3503 i2c_set_clientdata(client, NULL);
3504 client->driver = NULL;
3510 static const struct i2c_device_id sensor_id[] = {
3511 {SENSOR_NAME_STRING(), 0 },
3514 MODULE_DEVICE_TABLE(i2c, sensor_id);
3516 static struct i2c_driver sensor_i2c_driver = {
3518 .name = SENSOR_NAME_STRING(),
3520 .probe = sensor_probe,
3521 .remove = sensor_remove,
3522 .id_table = sensor_id,
3525 static int __init sensor_mod_init(void)
3527 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3528 return i2c_add_driver(&sensor_i2c_driver);
3531 static void __exit sensor_mod_exit(void)
3533 i2c_del_driver(&sensor_i2c_driver);
3536 device_initcall_sync(sensor_mod_init);
3537 module_exit(sensor_mod_exit);
3539 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3540 MODULE_AUTHOR("lxh@wisky.com.cn");
3541 MODULE_LICENSE("GPL");