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_OV2659
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV2659
48 #define SENSOR_ID 0x2656
49 #define SENSOR_MIN_WIDTH 800
50 #define SENSOR_MIN_HEIGHT 600
51 #define SENSOR_MAX_WIDTH 1600
52 #define SENSOR_MAX_HEIGHT 1200
53 #define SENSOR_INIT_WIDTH 800 /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT 600
55 #define SENSOR_INIT_WINSEQADR sensor_svga
56 #define SENSOR_INIT_PIXFMT V4L2_PIX_FMT_YUYV
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 1
68 #define CONFIG_SENSOR_Mirror 0
69 #define CONFIG_SENSOR_Flip 0
71 #define CONFIG_SENSOR_I2C_SPEED 350000 /* 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)
102 /* init 800*600 SVGA */
103 static struct reginfo sensor_init_data[] =
108 //{0x0100, 0x01}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array
274 static struct reginfo sensor_720p[]=
280 //{0x0100, 0x01 }, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array
443 static struct reginfo sensor_uxga[] =
494 static struct reginfo sensor_sxga[] =
499 static struct reginfo sensor_xga[] =
504 static struct reginfo sensor_svga[] =
506 {0x0100, 0x00}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
552 {0x0100, 0x01}, //software wake
557 static struct reginfo sensor_vga[] =
563 static struct reginfo sensor_cif[] =
569 static struct reginfo sensor_qvga[] =
575 static struct reginfo sensor_qcif[] =
581 static struct reginfo ov2655_qqvga[] =
625 static struct reginfo ov2655_Sharpness_auto[] =
630 static struct reginfo ov2655_Sharpness1[] =
636 static struct reginfo ov2655_Sharpness2[][3] =
643 static struct reginfo ov2655_Sharpness3[] =
649 static struct reginfo ov2655_Sharpness4[]=
656 static struct reginfo ov2655_Sharpness5[] =
664 static struct reginfo sensor_ClrFmt_YUYV[]=
670 static struct reginfo sensor_ClrFmt_UYVY[]=
676 #if CONFIG_SENSOR_WhiteBalance
677 static struct reginfo sensor_WhiteB_Auto[]=
679 {0x3406, 0x00}, //AWB auto, bit[1]:0,auto
682 /* Cloudy Colour Temperature : 6500K - 8000K */
683 static struct reginfo sensor_WhiteB_Cloudy[]=
694 /* ClearDay Colour Temperature : 5000K - 6500K */
695 static struct reginfo sensor_WhiteB_ClearDay[]=
707 /* Office Colour Temperature : 3500K - 5000K */
708 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
721 /* Home Colour Temperature : 2500K - 3500K */
722 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
734 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
735 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
739 #if CONFIG_SENSOR_Brightness
740 static struct reginfo sensor_Brightness0[]=
746 static struct reginfo sensor_Brightness1[]=
753 static struct reginfo sensor_Brightness2[]=
760 static struct reginfo sensor_Brightness3[]=
767 static struct reginfo sensor_Brightness4[]=
774 static struct reginfo sensor_Brightness5[]=
780 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
781 sensor_Brightness4, sensor_Brightness5,NULL,
786 #if CONFIG_SENSOR_Effect
787 static struct reginfo sensor_Effect_Normal[] =
793 static struct reginfo sensor_Effect_WandB[] =
799 static struct reginfo sensor_Effect_Sepia[] =
807 static struct reginfo sensor_Effect_Negative[] =
810 {0x507b, 0x40}, //bit[6] negative
813 static struct reginfo sensor_Effect_Bluish[] =
822 static struct reginfo sensor_Effect_Green[] =
830 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
831 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
834 #if CONFIG_SENSOR_Exposure
835 static struct reginfo sensor_Exposure0[]=
840 static struct reginfo sensor_Exposure1[]=
845 static struct reginfo sensor_Exposure2[]=
850 static struct reginfo sensor_Exposure3[]=
855 static struct reginfo sensor_Exposure4[]=
860 static struct reginfo sensor_Exposure5[]=
865 static struct reginfo sensor_Exposure6[]=
870 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
871 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
874 #if CONFIG_SENSOR_Saturation
875 static struct reginfo sensor_Saturation0[]=
880 static struct reginfo sensor_Saturation1[]=
885 static struct reginfo sensor_Saturation2[]=
889 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
892 #if CONFIG_SENSOR_Contrast
893 static struct reginfo sensor_Contrast0[]=
898 static struct reginfo sensor_Contrast1[]=
903 static struct reginfo sensor_Contrast2[]=
908 static struct reginfo sensor_Contrast3[]=
913 static struct reginfo sensor_Contrast4[]=
919 static struct reginfo sensor_Contrast5[]=
924 static struct reginfo sensor_Contrast6[]=
928 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
929 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
933 #if CONFIG_SENSOR_Mirror
934 static struct reginfo sensor_MirrorOn[]=
939 static struct reginfo sensor_MirrorOff[]=
943 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
945 #if CONFIG_SENSOR_Flip
946 static struct reginfo sensor_FlipOn[]=
951 static struct reginfo sensor_FlipOff[]=
955 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
958 #if CONFIG_SENSOR_Scene
959 static struct reginfo sensor_SceneAuto[] =
965 static struct reginfo sensor_SceneNight[] =
978 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
981 #if CONFIG_SENSOR_DigitalZoom
982 static struct reginfo sensor_Zoom0[] =
987 static struct reginfo sensor_Zoom1[] =
992 static struct reginfo sensor_Zoom2[] =
998 static struct reginfo sensor_Zoom3[] =
1002 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1004 static const struct v4l2_querymenu sensor_menus[] =
1006 #if CONFIG_SENSOR_WhiteBalance
1007 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1008 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1009 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1012 #if CONFIG_SENSOR_Effect
1013 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1014 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1015 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1018 #if CONFIG_SENSOR_Scene
1019 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1022 #if CONFIG_SENSOR_Flash
1023 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1024 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1028 static const struct v4l2_queryctrl sensor_controls[] =
1030 #if CONFIG_SENSOR_WhiteBalance
1032 .id = V4L2_CID_DO_WHITE_BALANCE,
1033 .type = V4L2_CTRL_TYPE_MENU,
1034 .name = "White Balance Control",
1042 #if CONFIG_SENSOR_Brightness
1044 .id = V4L2_CID_BRIGHTNESS,
1045 .type = V4L2_CTRL_TYPE_INTEGER,
1046 .name = "Brightness Control",
1054 #if CONFIG_SENSOR_Effect
1056 .id = V4L2_CID_EFFECT,
1057 .type = V4L2_CTRL_TYPE_MENU,
1058 .name = "Effect Control",
1066 #if CONFIG_SENSOR_Exposure
1068 .id = V4L2_CID_EXPOSURE,
1069 .type = V4L2_CTRL_TYPE_INTEGER,
1070 .name = "Exposure Control",
1078 #if CONFIG_SENSOR_Saturation
1080 .id = V4L2_CID_SATURATION,
1081 .type = V4L2_CTRL_TYPE_INTEGER,
1082 .name = "Saturation Control",
1090 #if CONFIG_SENSOR_Contrast
1092 .id = V4L2_CID_CONTRAST,
1093 .type = V4L2_CTRL_TYPE_INTEGER,
1094 .name = "Contrast Control",
1102 #if CONFIG_SENSOR_Mirror
1104 .id = V4L2_CID_HFLIP,
1105 .type = V4L2_CTRL_TYPE_BOOLEAN,
1106 .name = "Mirror Control",
1114 #if CONFIG_SENSOR_Flip
1116 .id = V4L2_CID_VFLIP,
1117 .type = V4L2_CTRL_TYPE_BOOLEAN,
1118 .name = "Flip Control",
1126 #if CONFIG_SENSOR_Scene
1128 .id = V4L2_CID_SCENE,
1129 .type = V4L2_CTRL_TYPE_MENU,
1130 .name = "Scene Control",
1138 #if CONFIG_SENSOR_DigitalZoom
1140 .id = V4L2_CID_ZOOM_RELATIVE,
1141 .type = V4L2_CTRL_TYPE_INTEGER,
1142 .name = "DigitalZoom Control",
1148 .id = V4L2_CID_ZOOM_ABSOLUTE,
1149 .type = V4L2_CTRL_TYPE_INTEGER,
1150 .name = "DigitalZoom Control",
1158 #if CONFIG_SENSOR_Focus
1160 .id = V4L2_CID_FOCUS_RELATIVE,
1161 .type = V4L2_CTRL_TYPE_INTEGER,
1162 .name = "Focus Control",
1168 .id = V4L2_CID_FOCUS_ABSOLUTE,
1169 .type = V4L2_CTRL_TYPE_INTEGER,
1170 .name = "Focus Control",
1174 .default_value = 125,
1178 #if CONFIG_SENSOR_Flash
1180 .id = V4L2_CID_FLASH,
1181 .type = V4L2_CTRL_TYPE_MENU,
1182 .name = "Flash Control",
1191 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1192 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1193 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1194 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1195 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1196 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1197 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1198 static int sensor_resume(struct soc_camera_device *icd);
1199 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1200 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1201 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1202 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1203 static int sensor_deactivate(struct i2c_client *client);
1205 static struct soc_camera_ops sensor_ops =
1207 .suspend = sensor_suspend,
1208 .resume = sensor_resume,
1209 .set_bus_param = sensor_set_bus_param,
1210 .query_bus_param = sensor_query_bus_param,
1211 .controls = sensor_controls,
1212 .menus = sensor_menus,
1213 .num_controls = ARRAY_SIZE(sensor_controls),
1214 .num_menus = ARRAY_SIZE(sensor_menus),
1217 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1218 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1219 .colorspace = _colorspace }
1221 #define JPG_FMT(_name, _depth, _fourcc) \
1222 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1224 static const struct soc_camera_data_format sensor_colour_formats[] = {
1225 JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1226 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1229 typedef struct sensor_info_priv_s
1243 unsigned char mirror; /* HFLIP */
1244 unsigned char flip; /* VFLIP */
1245 unsigned int winseqe_cur_addr;
1246 unsigned int pixfmt;
1247 unsigned int funmodule_state;
1248 } sensor_info_priv_t;
1252 struct v4l2_subdev subdev;
1253 struct i2c_client *client;
1254 sensor_info_priv_t info_priv;
1255 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1256 #if CONFIG_SENSOR_I2C_NOSCHED
1257 atomic_t tasklock_cnt;
1259 struct rk29camera_platform_data *sensor_io_request;
1260 struct rk29camera_gpio_res *sensor_gpio_res;
1264 static struct sensor* to_sensor(const struct i2c_client *client)
1266 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1269 static int sensor_task_lock(struct i2c_client *client, int lock)
1271 #if CONFIG_SENSOR_I2C_NOSCHED
1273 struct sensor *sensor = to_sensor(client);
1276 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1277 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1278 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1282 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1283 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1284 goto sensor_task_lock_err;
1289 atomic_add(1, &sensor->tasklock_cnt);
1291 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1292 atomic_sub(1, &sensor->tasklock_cnt);
1294 if (atomic_read(&sensor->tasklock_cnt) == 0)
1299 sensor_task_lock_err:
1307 /* sensor register write */
1308 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1312 struct i2c_msg msg[1];
1315 buf[1] = reg & 0xFF;
1318 msg->addr = client->addr;
1319 msg->flags = client->flags;
1321 msg->len = sizeof(buf);
1322 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1323 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1328 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1329 err = i2c_transfer(client->adapter, msg, 1);
1334 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1342 /* sensor register read */
1343 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1347 struct i2c_msg msg[2];
1350 buf[1] = reg & 0xFF;
1352 msg[0].addr = client->addr;
1353 msg[0].flags = client->flags;
1355 msg[0].len = sizeof(buf);
1356 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1357 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1359 msg[1].addr = client->addr;
1360 msg[1].flags = client->flags|I2C_M_RD;
1363 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1364 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1368 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1369 err = i2c_transfer(client->adapter, msg, 2);
1375 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1383 /* write a array of registers */
1384 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1388 #if CONFIG_SENSOR_I2C_RDWRCHK
1393 if (sensor_task_lock(client, 1) < 0)
1394 goto sensor_write_array_end;
1396 while (regarray[i].reg != 0)
1398 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1402 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1406 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1408 goto sensor_write_array_end;
1411 #if CONFIG_SENSOR_I2C_RDWRCHK
1412 sensor_read(client, regarray[i].reg, &valchk);
1413 if (valchk != regarray[i].val)
1414 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1420 sensor_write_array_end:
1421 sensor_task_lock(client,0);
1424 #if CONFIG_SENSOR_I2C_RDWRCHK
1425 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1433 while (regarray[i].reg != 0)
1435 sensor_read(client, regarray[i].reg, &valchk);
1436 if (valchk != regarray[i].val)
1437 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1444 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1446 struct soc_camera_link *icl = to_soc_camera_link(icd);
1449 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1452 case Sensor_PowerDown:
1454 if (icl->powerdown) {
1455 ret = icl->powerdown(icd->pdev, on);
1456 if (ret == RK29_CAM_IO_SUCCESS) {
1460 icl->reset(icd->pdev);
1462 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1464 goto sensor_power_end;
1471 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1472 struct sensor *sensor = to_sensor(client);
1474 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1475 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1481 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1488 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1490 struct i2c_client *client = sd->priv;
1491 struct soc_camera_device *icd = client->dev.platform_data;
1492 struct sensor *sensor = to_sensor(client);
1493 const struct v4l2_queryctrl *qctrl;
1497 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1499 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1501 goto sensor_INIT_ERR;
1505 if (sensor_task_lock(client,1)<0)
1506 goto sensor_INIT_ERR;
1508 ret = sensor_write(client, 0x0103, 0x01);
1511 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1513 goto sensor_INIT_ERR;
1515 mdelay(5); //delay 5 microseconds
1517 /* check if it is an sensor sensor */
1518 ret = sensor_read(client, 0x300a, &value);
1520 SENSOR_TR("read chip id high byte failed\n");
1522 goto sensor_INIT_ERR;
1525 pid |= (value << 8);
1527 ret = sensor_read(client, 0x300b, &value);
1529 SENSOR_TR("read chip id low byte failed\n");
1531 goto sensor_INIT_ERR;
1534 pid |= (value & 0xff);
1535 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1536 if (pid == SENSOR_ID) {
1537 sensor->model = SENSOR_V4L2_IDENT;
1539 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1541 goto sensor_INIT_ERR;
1544 ret = sensor_write_array(client, sensor_init_data);
1547 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1548 goto sensor_INIT_ERR;
1550 sensor_task_lock(client,0);
1551 //icd->user_width = SENSOR_INIT_WIDTH;
1552 //icd->user_height = SENSOR_INIT_HEIGHT;
1553 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1554 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
1556 /* sensor sensor information for initialization */
1557 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1559 sensor->info_priv.whiteBalance = qctrl->default_value;
1560 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1562 sensor->info_priv.brightness = qctrl->default_value;
1563 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1565 sensor->info_priv.effect = qctrl->default_value;
1566 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1568 sensor->info_priv.exposure = qctrl->default_value;
1570 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1572 sensor->info_priv.saturation = qctrl->default_value;
1573 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1575 sensor->info_priv.contrast = qctrl->default_value;
1576 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1578 sensor->info_priv.mirror = qctrl->default_value;
1579 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1581 sensor->info_priv.flip = qctrl->default_value;
1582 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1584 sensor->info_priv.scene = qctrl->default_value;
1585 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1587 sensor->info_priv.digitalzoom = qctrl->default_value;
1589 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1590 #if CONFIG_SENSOR_Focus
1592 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1594 sensor->info_priv.focus = qctrl->default_value;
1597 #if CONFIG_SENSOR_Flash
1598 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1600 sensor->info_priv.flash = qctrl->default_value;
1603 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);
1604 sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
1607 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1608 sensor_task_lock(client,0);
1609 sensor_deactivate(client);
1613 static int sensor_deactivate(struct i2c_client *client)
1615 struct soc_camera_device *icd = client->dev.platform_data;
1617 struct sensor *sensor = to_sensor(client);
1618 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1620 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1621 if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
1622 sensor_task_lock(client, 1);
1623 sensor_read(client,0x3000,®_val);
1624 sensor_write(client, 0x3000, reg_val&0xfc);
1625 sensor_write(client, 0x3001, 0x00);
1626 sensor_read(client,0x3002,®_val);
1627 sensor_write(client, 0x3002, reg_val&0x1f);
1628 sensor_task_lock(client, 0);
1630 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1633 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1634 icd->user_width = SENSOR_INIT_WIDTH;
1635 icd->user_height = SENSOR_INIT_HEIGHT;
1636 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1641 static struct reginfo sensor_power_down_sequence[]=
1645 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1648 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1650 if (pm_msg.event == PM_EVENT_SUSPEND) {
1651 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1652 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1654 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1657 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1659 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1664 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1670 static int sensor_resume(struct soc_camera_device *icd)
1674 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1676 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1680 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1686 static int sensor_set_bus_param(struct soc_camera_device *icd,
1687 unsigned long flags)
1693 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1695 struct soc_camera_link *icl = to_soc_camera_link(icd);
1696 unsigned long flags = SENSOR_BUS_PARAM;
1698 return soc_camera_apply_sensor_flags(icl, flags);
1701 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1703 struct i2c_client *client = sd->priv;
1704 struct soc_camera_device *icd = client->dev.platform_data;
1705 struct sensor *sensor = to_sensor(client);
1706 struct v4l2_pix_format *pix = &f->fmt.pix;
1708 pix->width = icd->user_width;
1709 pix->height = icd->user_height;
1710 pix->pixelformat = sensor->info_priv.pixfmt;
1711 pix->field = V4L2_FIELD_NONE;
1712 pix->colorspace = V4L2_COLORSPACE_JPEG;
1716 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
1720 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
1722 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
1724 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
1726 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
1728 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
1733 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1737 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
1741 if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
1743 } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
1748 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1751 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1753 struct i2c_client *client = sd->priv;
1754 struct sensor *sensor = to_sensor(client);
1755 struct v4l2_pix_format *pix = &f->fmt.pix;
1756 const struct v4l2_queryctrl *qctrl;
1757 struct soc_camera_device *icd = client->dev.platform_data;
1758 struct reginfo *winseqe_set_addr=NULL;
1759 int ret=0, set_w,set_h;
1761 if (sensor->info_priv.pixfmt != pix->pixelformat) {
1762 switch (pix->pixelformat)
1764 case V4L2_PIX_FMT_YUYV:
1766 winseqe_set_addr = sensor_ClrFmt_YUYV;
1769 case V4L2_PIX_FMT_UYVY:
1771 winseqe_set_addr = sensor_ClrFmt_UYVY;
1777 if (winseqe_set_addr != NULL) {
1778 sensor_write_array(client, winseqe_set_addr);
1779 sensor->info_priv.pixfmt = pix->pixelformat;
1781 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1783 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1788 set_h = pix->height;
1790 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1792 winseqe_set_addr = sensor_qcif;
1796 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1798 winseqe_set_addr = sensor_qvga;
1802 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1804 winseqe_set_addr = sensor_cif;
1808 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1810 winseqe_set_addr = sensor_vga;
1814 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1816 winseqe_set_addr = sensor_svga;
1820 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
1822 winseqe_set_addr = sensor_720p;
1826 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
1828 winseqe_set_addr = sensor_xga;
1832 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1834 winseqe_set_addr = sensor_sxga;
1838 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1840 winseqe_set_addr = sensor_uxga;
1846 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1847 set_w = SENSOR_INIT_WIDTH;
1848 set_h = SENSOR_INIT_HEIGHT;
1849 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
1852 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1853 #if CONFIG_SENSOR_Flash
1854 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
1855 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1856 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1857 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1859 } else { /* ddl@rock-chips.com : Video */
1860 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1861 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1862 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1866 ret |= sensor_write_array(client, winseqe_set_addr);
1868 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1869 #if CONFIG_SENSOR_Flash
1870 if (sensor_fmt_capturechk(sd,f) == true) {
1871 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1872 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1873 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1877 goto sensor_s_fmt_end;
1880 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1882 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
1883 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1884 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1885 if (sensor->info_priv.whiteBalance != 0) {
1886 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1887 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1889 sensor->info_priv.snap2preview = true;
1890 } else if (sensor_fmt_videochk(sd,f) == true) { /* ddl@rock-chips.com : Video */
1891 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1892 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1893 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1894 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1895 sensor->info_priv.video2preview = true;
1896 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1897 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1898 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1899 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1900 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1902 sensor->info_priv.video2preview = false;
1903 sensor->info_priv.snap2preview = false;
1906 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1910 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1914 pix->height = set_h;
1920 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1922 struct v4l2_pix_format *pix = &f->fmt.pix;
1923 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
1924 pix->pixelformat == V4L2_PIX_FMT_YUYV;
1927 * With Bayer format enforce even side lengths, but let the user play
1928 * with the starting pixel
1931 if (pix->height > SENSOR_MAX_HEIGHT)
1932 pix->height = SENSOR_MAX_HEIGHT;
1933 else if (pix->height < SENSOR_MIN_HEIGHT)
1934 pix->height = SENSOR_MIN_HEIGHT;
1936 pix->height = ALIGN(pix->height, 2);
1938 if (pix->width > SENSOR_MAX_WIDTH)
1939 pix->width = SENSOR_MAX_WIDTH;
1940 else if (pix->width < SENSOR_MIN_WIDTH)
1941 pix->width = SENSOR_MIN_WIDTH;
1943 pix->width = ALIGN(pix->width, 2);
1948 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1950 struct i2c_client *client = sd->priv;
1952 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1955 if (id->match.addr != client->addr)
1958 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
1963 #if CONFIG_SENSOR_Brightness
1964 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1966 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1968 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1970 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1972 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1974 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1977 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1981 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1985 #if CONFIG_SENSOR_Effect
1986 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1988 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1990 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1992 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1994 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1996 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1999 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2003 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2007 #if CONFIG_SENSOR_Exposure
2008 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2010 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2012 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2014 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2016 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2018 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2021 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2025 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2029 #if CONFIG_SENSOR_Saturation
2030 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2032 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2034 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2036 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2038 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2040 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2043 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2047 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2051 #if CONFIG_SENSOR_Contrast
2052 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2054 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2056 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2058 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2060 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2062 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2065 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2069 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2073 #if CONFIG_SENSOR_Mirror
2074 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2076 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2078 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2080 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2082 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2084 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2087 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2091 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2095 #if CONFIG_SENSOR_Flip
2096 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2098 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2100 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2102 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2104 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2106 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2109 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2113 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2117 #if CONFIG_SENSOR_Scene
2118 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2120 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2122 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2124 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2126 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2128 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2131 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2135 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2139 #if CONFIG_SENSOR_WhiteBalance
2140 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2142 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2144 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2146 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2148 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2150 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2153 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2157 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2161 #if CONFIG_SENSOR_DigitalZoom
2162 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2164 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2165 struct sensor *sensor = to_sensor(client);
2166 const struct v4l2_queryctrl *qctrl_info;
2167 int digitalzoom_cur, digitalzoom_total;
2169 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2173 digitalzoom_cur = sensor->info_priv.digitalzoom;
2174 digitalzoom_total = qctrl_info->maximum;
2176 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2178 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2182 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2184 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2188 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2190 *value = digitalzoom_total - digitalzoom_cur;
2193 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2195 *value = 0 - digitalzoom_cur;
2198 digitalzoom_cur += *value;
2200 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2202 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2204 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2207 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2214 #if CONFIG_SENSOR_Flash
2215 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2217 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2218 if (value == 3) { /* ddl@rock-chips.com: torch */
2219 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2221 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2223 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2227 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2232 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2234 struct i2c_client *client = sd->priv;
2235 struct sensor *sensor = to_sensor(client);
2236 const struct v4l2_queryctrl *qctrl;
2238 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2242 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2248 case V4L2_CID_BRIGHTNESS:
2250 ctrl->value = sensor->info_priv.brightness;
2253 case V4L2_CID_SATURATION:
2255 ctrl->value = sensor->info_priv.saturation;
2258 case V4L2_CID_CONTRAST:
2260 ctrl->value = sensor->info_priv.contrast;
2263 case V4L2_CID_DO_WHITE_BALANCE:
2265 ctrl->value = sensor->info_priv.whiteBalance;
2268 case V4L2_CID_EXPOSURE:
2270 ctrl->value = sensor->info_priv.exposure;
2273 case V4L2_CID_HFLIP:
2275 ctrl->value = sensor->info_priv.mirror;
2278 case V4L2_CID_VFLIP:
2280 ctrl->value = sensor->info_priv.flip;
2291 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2293 struct i2c_client *client = sd->priv;
2294 struct sensor *sensor = to_sensor(client);
2295 struct soc_camera_device *icd = client->dev.platform_data;
2296 const struct v4l2_queryctrl *qctrl;
2299 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2303 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2309 #if CONFIG_SENSOR_Brightness
2310 case V4L2_CID_BRIGHTNESS:
2312 if (ctrl->value != sensor->info_priv.brightness)
2314 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2318 sensor->info_priv.brightness = ctrl->value;
2323 #if CONFIG_SENSOR_Exposure
2324 case V4L2_CID_EXPOSURE:
2326 if (ctrl->value != sensor->info_priv.exposure)
2328 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2332 sensor->info_priv.exposure = ctrl->value;
2337 #if CONFIG_SENSOR_Saturation
2338 case V4L2_CID_SATURATION:
2340 if (ctrl->value != sensor->info_priv.saturation)
2342 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2346 sensor->info_priv.saturation = ctrl->value;
2351 #if CONFIG_SENSOR_Contrast
2352 case V4L2_CID_CONTRAST:
2354 if (ctrl->value != sensor->info_priv.contrast)
2356 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2360 sensor->info_priv.contrast = ctrl->value;
2365 #if CONFIG_SENSOR_WhiteBalance
2366 case V4L2_CID_DO_WHITE_BALANCE:
2368 if (ctrl->value != sensor->info_priv.whiteBalance)
2370 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2374 sensor->info_priv.whiteBalance = ctrl->value;
2379 #if CONFIG_SENSOR_Mirror
2380 case V4L2_CID_HFLIP:
2382 if (ctrl->value != sensor->info_priv.mirror)
2384 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2386 sensor->info_priv.mirror = ctrl->value;
2391 #if CONFIG_SENSOR_Flip
2392 case V4L2_CID_VFLIP:
2394 if (ctrl->value != sensor->info_priv.flip)
2396 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2398 sensor->info_priv.flip = ctrl->value;
2409 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2411 const struct v4l2_queryctrl *qctrl;
2412 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2413 struct sensor *sensor = to_sensor(client);
2415 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2419 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2423 switch (ext_ctrl->id)
2425 case V4L2_CID_SCENE:
2427 ext_ctrl->value = sensor->info_priv.scene;
2430 case V4L2_CID_EFFECT:
2432 ext_ctrl->value = sensor->info_priv.effect;
2435 case V4L2_CID_ZOOM_ABSOLUTE:
2437 ext_ctrl->value = sensor->info_priv.digitalzoom;
2440 case V4L2_CID_ZOOM_RELATIVE:
2444 case V4L2_CID_FOCUS_ABSOLUTE:
2446 ext_ctrl->value = sensor->info_priv.focus;
2449 case V4L2_CID_FOCUS_RELATIVE:
2453 case V4L2_CID_FLASH:
2455 ext_ctrl->value = sensor->info_priv.flash;
2463 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2465 const struct v4l2_queryctrl *qctrl;
2466 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2467 struct sensor *sensor = to_sensor(client);
2470 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2474 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2479 switch (ext_ctrl->id)
2481 #if CONFIG_SENSOR_Scene
2482 case V4L2_CID_SCENE:
2484 if (ext_ctrl->value != sensor->info_priv.scene)
2486 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2488 sensor->info_priv.scene = ext_ctrl->value;
2493 #if CONFIG_SENSOR_Effect
2494 case V4L2_CID_EFFECT:
2496 if (ext_ctrl->value != sensor->info_priv.effect)
2498 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2500 sensor->info_priv.effect= ext_ctrl->value;
2505 #if CONFIG_SENSOR_DigitalZoom
2506 case V4L2_CID_ZOOM_ABSOLUTE:
2508 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2511 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2513 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2515 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2517 sensor->info_priv.digitalzoom += val_offset;
2519 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2524 case V4L2_CID_ZOOM_RELATIVE:
2526 if (ext_ctrl->value)
2528 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2530 sensor->info_priv.digitalzoom += ext_ctrl->value;
2532 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2537 #if CONFIG_SENSOR_Focus
2538 case V4L2_CID_FOCUS_ABSOLUTE:
2540 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2543 if (ext_ctrl->value != sensor->info_priv.focus)
2545 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2547 sensor->info_priv.focus += val_offset;
2552 case V4L2_CID_FOCUS_RELATIVE:
2554 if (ext_ctrl->value)
2556 sensor->info_priv.focus += ext_ctrl->value;
2558 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2563 #if CONFIG_SENSOR_Flash
2564 case V4L2_CID_FLASH:
2566 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2568 sensor->info_priv.flash = ext_ctrl->value;
2570 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2581 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2583 struct i2c_client *client = sd->priv;
2584 struct soc_camera_device *icd = client->dev.platform_data;
2585 int i, error_cnt=0, error_idx=-1;
2588 for (i=0; i<ext_ctrl->count; i++) {
2589 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2596 error_idx = ext_ctrl->count;
2598 if (error_idx != -1) {
2599 ext_ctrl->error_idx = error_idx;
2606 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2608 struct i2c_client *client = sd->priv;
2609 struct soc_camera_device *icd = client->dev.platform_data;
2610 int i, error_cnt=0, error_idx=-1;
2613 for (i=0; i<ext_ctrl->count; i++) {
2614 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2621 error_idx = ext_ctrl->count;
2623 if (error_idx != -1) {
2624 ext_ctrl->error_idx = error_idx;
2631 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2632 * this wasn't our capture interface, so, we wait for the right one */
2633 static int sensor_video_probe(struct soc_camera_device *icd,
2634 struct i2c_client *client)
2638 struct sensor *sensor = to_sensor(client);
2640 /* We must have a parent by now. And it cannot be a wrong one.
2641 * So this entire test is completely redundant. */
2642 if (!icd->dev.parent ||
2643 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2646 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2648 goto sensor_video_probe_err;
2652 ret = sensor_write(client, 0x0103, 0x01);
2654 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2656 goto sensor_video_probe_err;
2658 mdelay(5); //delay 5 microseconds
2660 /* check if it is an sensor sensor */
2661 ret = sensor_read(client, 0x300a, &value);
2663 SENSOR_TR("read chip id high byte failed\n");
2665 goto sensor_video_probe_err;
2668 pid |= (value << 8);
2670 ret = sensor_read(client, 0x300b, &value);
2672 SENSOR_TR("read chip id low byte failed\n");
2674 goto sensor_video_probe_err;
2677 pid |= (value & 0xff);
2678 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2679 if (pid == SENSOR_ID) {
2680 sensor->model = SENSOR_V4L2_IDENT;
2682 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2684 goto sensor_video_probe_err;
2687 icd->formats = sensor_colour_formats;
2688 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
2692 sensor_video_probe_err:
2697 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2699 struct i2c_client *client = sd->priv;
2700 struct soc_camera_device *icd = client->dev.platform_data;
2701 struct sensor *sensor = to_sensor(client);
2704 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2707 case RK29_CAM_SUBDEV_DEACTIVATE:
2709 sensor_deactivate(client);
2713 case RK29_CAM_SUBDEV_IOREQUEST:
2715 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2716 if (sensor->sensor_io_request != NULL) {
2717 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2718 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2719 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2720 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2721 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2722 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2725 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2727 goto sensor_ioctl_end;
2729 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2731 #if CONFIG_SENSOR_Flash
2732 if (sensor->sensor_gpio_res) {
2733 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2734 for (i = 0; i < icd->ops->num_controls; i++) {
2735 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2736 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2739 sensor->info_priv.flash = 0xff;
2740 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2748 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2757 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2758 .init = sensor_init,
2759 .g_ctrl = sensor_g_control,
2760 .s_ctrl = sensor_s_control,
2761 .g_ext_ctrls = sensor_g_ext_controls,
2762 .s_ext_ctrls = sensor_s_ext_controls,
2763 .g_chip_ident = sensor_g_chip_ident,
2764 .ioctl = sensor_ioctl,
2767 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2768 .s_fmt = sensor_s_fmt,
2769 .g_fmt = sensor_g_fmt,
2770 .try_fmt = sensor_try_fmt,
2773 static struct v4l2_subdev_ops sensor_subdev_ops = {
2774 .core = &sensor_subdev_core_ops,
2775 .video = &sensor_subdev_video_ops,
2778 static int sensor_probe(struct i2c_client *client,
2779 const struct i2c_device_id *did)
2781 struct sensor *sensor;
2782 struct soc_camera_device *icd = client->dev.platform_data;
2783 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2784 struct soc_camera_link *icl;
2787 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2789 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2793 icl = to_soc_camera_link(icd);
2795 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2799 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2800 dev_warn(&adapter->dev,
2801 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2805 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2809 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2811 /* Second stage probe - when a capture adapter is there */
2812 icd->ops = &sensor_ops;
2813 icd->y_skip_top = 0;
2814 #if CONFIG_SENSOR_I2C_NOSCHED
2815 atomic_set(&sensor->tasklock_cnt,0);
2818 ret = sensor_video_probe(icd, client);
2821 i2c_set_clientdata(client, NULL);
2825 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2829 static int sensor_remove(struct i2c_client *client)
2831 struct sensor *sensor = to_sensor(client);
2832 struct soc_camera_device *icd = client->dev.platform_data;
2835 i2c_set_clientdata(client, NULL);
2836 client->driver = NULL;
2842 static const struct i2c_device_id sensor_id[] = {
2843 {SENSOR_NAME_STRING(), 0 },
2846 MODULE_DEVICE_TABLE(i2c, sensor_id);
2848 static struct i2c_driver sensor_i2c_driver = {
2850 .name = SENSOR_NAME_STRING(),
2852 .probe = sensor_probe,
2853 .remove = sensor_remove,
2854 .id_table = sensor_id,
2857 static int __init sensor_mod_init(void)
2859 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2860 return i2c_add_driver(&sensor_i2c_driver);
2863 static void __exit sensor_mod_exit(void)
2865 i2c_del_driver(&sensor_i2c_driver);
2868 device_initcall_sync(sensor_mod_init);
2869 module_exit(sensor_mod_exit);
2871 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2872 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2873 MODULE_LICENSE("GPL");