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>
23 #include <mach/gpio.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(0, 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 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 0
68 #define CONFIG_SENSOR_Mirror 0
69 #define CONFIG_SENSOR_Flip 0
71 #define CONFIG_SENSOR_I2C_SPEED 250000 /* 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_FALLING|\
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)
97 /* init 800*600 SVGA */
98 static struct reginfo sensor_init_data[] =
103 //{0x0100, 0x01}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array
269 static struct reginfo sensor_720p[]=
275 //{0x0100, 0x01 }, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array
438 static struct reginfo sensor_uxga[] =
489 static struct reginfo sensor_sxga[] =
495 static struct reginfo sensor_svga[] =
497 {0x0100, 0x00}, //software sleep : Sensor vsync singal may not output if haven't sleep the sensor when transfer the array,
543 {0x0100, 0x01}, //software wake
548 static struct reginfo sensor_vga[] =
554 static struct reginfo sensor_cif[] =
560 static struct reginfo sensor_qvga[] =
566 static struct reginfo sensor_qcif[] =
572 static struct reginfo ov2655_qqvga[] =
616 static struct reginfo ov2655_Sharpness_auto[] =
621 static struct reginfo ov2655_Sharpness1[] =
627 static struct reginfo ov2655_Sharpness2[][3] =
634 static struct reginfo ov2655_Sharpness3[] =
640 static struct reginfo ov2655_Sharpness4[]=
647 static struct reginfo ov2655_Sharpness5[] =
655 static struct reginfo sensor_ClrFmt_YUYV[]=
661 static struct reginfo sensor_ClrFmt_UYVY[]=
667 #if CONFIG_SENSOR_WhiteBalance
668 static struct reginfo sensor_WhiteB_Auto[]=
670 {0x3406, 0x00}, //AWB auto, bit[1]:0,auto
673 /* Cloudy Colour Temperature : 6500K - 8000K */
674 static struct reginfo sensor_WhiteB_Cloudy[]=
685 /* ClearDay Colour Temperature : 5000K - 6500K */
686 static struct reginfo sensor_WhiteB_ClearDay[]=
698 /* Office Colour Temperature : 3500K - 5000K */
699 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
712 /* Home Colour Temperature : 2500K - 3500K */
713 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
725 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
726 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
730 #if CONFIG_SENSOR_Brightness
731 static struct reginfo sensor_Brightness0[]=
737 static struct reginfo sensor_Brightness1[]=
744 static struct reginfo sensor_Brightness2[]=
751 static struct reginfo sensor_Brightness3[]=
758 static struct reginfo sensor_Brightness4[]=
765 static struct reginfo sensor_Brightness5[]=
771 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
772 sensor_Brightness4, sensor_Brightness5,NULL,
777 #if CONFIG_SENSOR_Effect
778 static struct reginfo sensor_Effect_Normal[] =
784 static struct reginfo sensor_Effect_WandB[] =
790 static struct reginfo sensor_Effect_Sepia[] =
798 static struct reginfo sensor_Effect_Negative[] =
801 {0x507b, 0x40}, //bit[6] negative
804 static struct reginfo sensor_Effect_Bluish[] =
813 static struct reginfo sensor_Effect_Green[] =
821 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
822 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
825 #if CONFIG_SENSOR_Exposure
826 static struct reginfo sensor_Exposure0[]=
831 static struct reginfo sensor_Exposure1[]=
836 static struct reginfo sensor_Exposure2[]=
841 static struct reginfo sensor_Exposure3[]=
846 static struct reginfo sensor_Exposure4[]=
851 static struct reginfo sensor_Exposure5[]=
856 static struct reginfo sensor_Exposure6[]=
861 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
862 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
865 #if CONFIG_SENSOR_Saturation
866 static struct reginfo sensor_Saturation0[]=
871 static struct reginfo sensor_Saturation1[]=
876 static struct reginfo sensor_Saturation2[]=
880 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
883 #if CONFIG_SENSOR_Contrast
884 static struct reginfo sensor_Contrast0[]=
889 static struct reginfo sensor_Contrast1[]=
894 static struct reginfo sensor_Contrast2[]=
899 static struct reginfo sensor_Contrast3[]=
904 static struct reginfo sensor_Contrast4[]=
910 static struct reginfo sensor_Contrast5[]=
915 static struct reginfo sensor_Contrast6[]=
919 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
920 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
924 #if CONFIG_SENSOR_Mirror
925 static struct reginfo sensor_MirrorOn[]=
930 static struct reginfo sensor_MirrorOff[]=
934 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
936 #if CONFIG_SENSOR_Flip
937 static struct reginfo sensor_FlipOn[]=
942 static struct reginfo sensor_FlipOff[]=
946 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
949 #if CONFIG_SENSOR_Scene
950 static struct reginfo sensor_SceneAuto[] =
956 static struct reginfo sensor_SceneNight[] =
969 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
972 #if CONFIG_SENSOR_DigitalZoom
973 static struct reginfo sensor_Zoom0[] =
978 static struct reginfo sensor_Zoom1[] =
983 static struct reginfo sensor_Zoom2[] =
989 static struct reginfo sensor_Zoom3[] =
993 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
995 static const struct v4l2_querymenu sensor_menus[] =
997 #if CONFIG_SENSOR_WhiteBalance
998 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
999 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1000 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1003 #if CONFIG_SENSOR_Effect
1004 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1005 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1006 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1009 #if CONFIG_SENSOR_Scene
1010 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1013 #if CONFIG_SENSOR_Flash
1014 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1015 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1019 static const struct v4l2_queryctrl sensor_controls[] =
1021 #if CONFIG_SENSOR_WhiteBalance
1023 .id = V4L2_CID_DO_WHITE_BALANCE,
1024 .type = V4L2_CTRL_TYPE_MENU,
1025 .name = "White Balance Control",
1033 #if CONFIG_SENSOR_Brightness
1035 .id = V4L2_CID_BRIGHTNESS,
1036 .type = V4L2_CTRL_TYPE_INTEGER,
1037 .name = "Brightness Control",
1045 #if CONFIG_SENSOR_Effect
1047 .id = V4L2_CID_EFFECT,
1048 .type = V4L2_CTRL_TYPE_MENU,
1049 .name = "Effect Control",
1057 #if CONFIG_SENSOR_Exposure
1059 .id = V4L2_CID_EXPOSURE,
1060 .type = V4L2_CTRL_TYPE_INTEGER,
1061 .name = "Exposure Control",
1069 #if CONFIG_SENSOR_Saturation
1071 .id = V4L2_CID_SATURATION,
1072 .type = V4L2_CTRL_TYPE_INTEGER,
1073 .name = "Saturation Control",
1081 #if CONFIG_SENSOR_Contrast
1083 .id = V4L2_CID_CONTRAST,
1084 .type = V4L2_CTRL_TYPE_INTEGER,
1085 .name = "Contrast Control",
1093 #if CONFIG_SENSOR_Mirror
1095 .id = V4L2_CID_HFLIP,
1096 .type = V4L2_CTRL_TYPE_BOOLEAN,
1097 .name = "Mirror Control",
1105 #if CONFIG_SENSOR_Flip
1107 .id = V4L2_CID_VFLIP,
1108 .type = V4L2_CTRL_TYPE_BOOLEAN,
1109 .name = "Flip Control",
1117 #if CONFIG_SENSOR_Scene
1119 .id = V4L2_CID_SCENE,
1120 .type = V4L2_CTRL_TYPE_MENU,
1121 .name = "Scene Control",
1129 #if CONFIG_SENSOR_DigitalZoom
1131 .id = V4L2_CID_ZOOM_RELATIVE,
1132 .type = V4L2_CTRL_TYPE_INTEGER,
1133 .name = "DigitalZoom Control",
1139 .id = V4L2_CID_ZOOM_ABSOLUTE,
1140 .type = V4L2_CTRL_TYPE_INTEGER,
1141 .name = "DigitalZoom Control",
1149 #if CONFIG_SENSOR_Focus
1151 .id = V4L2_CID_FOCUS_RELATIVE,
1152 .type = V4L2_CTRL_TYPE_INTEGER,
1153 .name = "Focus Control",
1159 .id = V4L2_CID_FOCUS_ABSOLUTE,
1160 .type = V4L2_CTRL_TYPE_INTEGER,
1161 .name = "Focus Control",
1165 .default_value = 125,
1169 #if CONFIG_SENSOR_Flash
1171 .id = V4L2_CID_FLASH,
1172 .type = V4L2_CTRL_TYPE_MENU,
1173 .name = "Flash Control",
1182 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1183 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1184 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1185 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1186 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1187 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1188 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1189 static int sensor_resume(struct soc_camera_device *icd);
1190 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1191 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1193 static struct soc_camera_ops sensor_ops =
1195 .suspend = sensor_suspend,
1196 .resume = sensor_resume,
1197 .set_bus_param = sensor_set_bus_param,
1198 .query_bus_param = sensor_query_bus_param,
1199 .controls = sensor_controls,
1200 .menus = sensor_menus,
1201 .num_controls = ARRAY_SIZE(sensor_controls),
1202 .num_menus = ARRAY_SIZE(sensor_menus),
1205 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1206 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1207 .colorspace = _colorspace }
1209 #define JPG_FMT(_name, _depth, _fourcc) \
1210 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1212 static const struct soc_camera_data_format sensor_colour_formats[] = {
1213 JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1214 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1217 typedef struct sensor_info_priv_s
1229 unsigned char mirror; /* HFLIP */
1230 unsigned char flip; /* VFLIP */
1231 unsigned int winseqe_cur_addr;
1232 unsigned int pixfmt;
1234 } sensor_info_priv_t;
1238 struct v4l2_subdev subdev;
1239 struct i2c_client *client;
1240 sensor_info_priv_t info_priv;
1241 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1242 #if CONFIG_SENSOR_I2C_NOSCHED
1243 atomic_t tasklock_cnt;
1245 struct rk29camera_platform_data *sensor_io_request;
1248 static int sensor_deactivate(struct i2c_client *client);
1250 static struct sensor* to_sensor(const struct i2c_client *client)
1252 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1255 static int sensor_task_lock(struct i2c_client *client, int lock)
1257 #if CONFIG_SENSOR_I2C_NOSCHED
1259 struct sensor *sensor = to_sensor(client);
1262 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1263 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1264 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1268 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1269 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1270 goto sensor_task_lock_err;
1275 atomic_add(1, &sensor->tasklock_cnt);
1277 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1278 atomic_sub(1, &sensor->tasklock_cnt);
1280 if (atomic_read(&sensor->tasklock_cnt) == 0)
1286 sensor_task_lock_err:
1290 /* sensor register write */
1291 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1295 struct i2c_msg msg[1];
1298 buf[1] = reg & 0xFF;
1301 msg->addr = client->addr;
1302 msg->flags = client->flags;
1304 msg->len = sizeof(buf);
1305 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1306 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1311 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1312 err = i2c_transfer(client->adapter, msg, 1);
1317 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1325 /* sensor register read */
1326 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1330 struct i2c_msg msg[2];
1333 buf[1] = reg & 0xFF;
1335 msg[0].addr = client->addr;
1336 msg[0].flags = client->flags;
1338 msg[0].len = sizeof(buf);
1339 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1340 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1342 msg[1].addr = client->addr;
1343 msg[1].flags = client->flags|I2C_M_RD;
1346 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1347 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1351 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1352 err = i2c_transfer(client->adapter, msg, 2);
1358 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1366 /* write a array of registers */
1367 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1374 if (sensor_task_lock(client, 1) < 0)
1375 goto sensor_write_array_end;
1377 while (regarray[i].reg != 0)
1379 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1383 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1387 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1389 goto sensor_write_array_end;
1392 #if CONFIG_SENSOR_I2C_RDWRCHK
1393 sensor_read(client, regarray[i].reg, &valchk);
1394 if (valchk != regarray[i].val)
1395 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1401 sensor_write_array_end:
1402 sensor_task_lock(client,0);
1405 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1413 while (regarray[i].reg != 0)
1415 sensor_read(client, regarray[i].reg, &valchk);
1416 if (valchk != regarray[i].val)
1417 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1423 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1425 struct soc_camera_link *icl = to_soc_camera_link(icd);
1431 case Sensor_PowerDown:
1433 if (icl->powerdown) {
1434 ret = icl->powerdown(icd->pdev, on);
1435 if (ret == RK29_CAM_IO_SUCCESS) {
1439 icl->reset(icd->pdev);
1441 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1443 goto sensor_power_end;
1450 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1451 struct sensor *sensor = to_sensor(client);
1453 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1454 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1459 SENSOR_TR("%s power cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
1466 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1468 struct i2c_client *client = sd->priv;
1469 struct soc_camera_device *icd = client->dev.platform_data;
1470 struct sensor *sensor = to_sensor(client);
1471 const struct v4l2_queryctrl *qctrl;
1475 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1477 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1479 goto sensor_INIT_ERR;
1483 if (sensor_task_lock(client,1)<0)
1484 goto sensor_INIT_ERR;
1486 ret = sensor_write(client, 0x0103, 0x01);
1489 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1491 goto sensor_INIT_ERR;
1493 mdelay(5); //delay 5 microseconds
1495 /* check if it is an sensor sensor */
1496 ret = sensor_read(client, 0x300a, &value);
1498 SENSOR_TR("read chip id high byte failed\n");
1500 goto sensor_INIT_ERR;
1503 pid |= (value << 8);
1505 ret = sensor_read(client, 0x300b, &value);
1507 SENSOR_TR("read chip id low byte failed\n");
1509 goto sensor_INIT_ERR;
1512 pid |= (value & 0xff);
1513 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1514 if (pid == SENSOR_ID) {
1515 sensor->model = SENSOR_V4L2_IDENT;
1517 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1519 goto sensor_INIT_ERR;
1522 ret = sensor_write_array(client, sensor_init_data);
1525 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1526 goto sensor_INIT_ERR;
1528 sensor_task_lock(client,0);
1529 //icd->user_width = SENSOR_INIT_WIDTH;
1530 //icd->user_height = SENSOR_INIT_HEIGHT;
1531 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1532 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
1534 /* sensor sensor information for initialization */
1535 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1537 sensor->info_priv.whiteBalance = qctrl->default_value;
1538 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1540 sensor->info_priv.brightness = qctrl->default_value;
1541 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1543 sensor->info_priv.effect = qctrl->default_value;
1544 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1546 sensor->info_priv.exposure = qctrl->default_value;
1548 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1550 sensor->info_priv.saturation = qctrl->default_value;
1551 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1553 sensor->info_priv.contrast = qctrl->default_value;
1554 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1556 sensor->info_priv.mirror = qctrl->default_value;
1557 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1559 sensor->info_priv.flip = qctrl->default_value;
1560 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1562 sensor->info_priv.scene = qctrl->default_value;
1563 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1565 sensor->info_priv.digitalzoom = qctrl->default_value;
1567 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1568 #if CONFIG_SENSOR_Focus
1570 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1572 sensor->info_priv.focus = qctrl->default_value;
1575 #if CONFIG_SENSOR_Flash
1577 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1579 sensor->info_priv.flash = qctrl->default_value;
1582 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);
1586 sensor_task_lock(client,0);
1587 sensor_deactivate(client);
1591 static int sensor_deactivate(struct i2c_client *client)
1593 struct soc_camera_device *icd = client->dev.platform_data;
1596 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1598 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1599 sensor_task_lock(client, 1);
1600 sensor_read(client,0x3000,®_val);
1601 sensor_write(client, 0x3000, reg_val&0xfc);
1602 sensor_write(client, 0x3001, 0x00);
1603 sensor_read(client,0x3002,®_val);
1604 sensor_write(client, 0x3002, reg_val&0x1f);
1605 sensor_task_lock(client, 0);
1606 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1608 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1609 icd->user_width = SENSOR_INIT_WIDTH;
1610 icd->user_height = SENSOR_INIT_HEIGHT;
1615 static struct reginfo sensor_power_down_sequence[]=
1619 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1622 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1624 if (pm_msg.event == PM_EVENT_SUSPEND) {
1625 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1626 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1628 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1631 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1633 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1638 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1644 static int sensor_resume(struct soc_camera_device *icd)
1648 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1650 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1654 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1660 static int sensor_set_bus_param(struct soc_camera_device *icd,
1661 unsigned long flags)
1667 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1669 struct soc_camera_link *icl = to_soc_camera_link(icd);
1670 unsigned long flags = SENSOR_BUS_PARAM;
1672 return soc_camera_apply_sensor_flags(icl, flags);
1675 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1677 struct i2c_client *client = sd->priv;
1678 struct soc_camera_device *icd = client->dev.platform_data;
1679 struct sensor *sensor = to_sensor(client);
1680 struct v4l2_pix_format *pix = &f->fmt.pix;
1682 pix->width = icd->user_width;
1683 pix->height = icd->user_height;
1684 pix->pixelformat = sensor->info_priv.pixfmt;
1685 pix->field = V4L2_FIELD_NONE;
1686 pix->colorspace = V4L2_COLORSPACE_JPEG;
1690 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1692 struct i2c_client *client = sd->priv;
1693 struct sensor *sensor = to_sensor(client);
1694 struct v4l2_pix_format *pix = &f->fmt.pix;
1695 struct reginfo *winseqe_set_addr=NULL;
1696 int ret=0, set_w,set_h;
1698 gpio_direction_output(RK29_PIN6_PB7,1);
1699 gpio_set_value(RK29_PIN6_PB7, 1);
1701 if (sensor->info_priv.pixfmt != pix->pixelformat) {
1702 switch (pix->pixelformat)
1704 case V4L2_PIX_FMT_YUYV:
1706 winseqe_set_addr = sensor_ClrFmt_YUYV;
1709 case V4L2_PIX_FMT_UYVY:
1711 winseqe_set_addr = sensor_ClrFmt_UYVY;
1717 if (winseqe_set_addr != NULL) {
1718 sensor_write_array(client, winseqe_set_addr);
1719 sensor->info_priv.pixfmt = pix->pixelformat;
1721 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1723 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1728 set_h = pix->height;
1730 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1732 winseqe_set_addr = sensor_qcif;
1736 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1738 winseqe_set_addr = sensor_qvga;
1742 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1744 winseqe_set_addr = sensor_cif;
1748 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1750 winseqe_set_addr = sensor_vga;
1754 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1756 winseqe_set_addr = sensor_svga;
1760 else if (((set_w <= 1280) && (set_h <= 720)) && sensor_720p[0].reg)
1762 winseqe_set_addr = sensor_720p;
1766 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1768 winseqe_set_addr = sensor_sxga;
1772 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1774 winseqe_set_addr = sensor_uxga;
1780 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1781 set_w = SENSOR_INIT_WIDTH;
1782 set_h = SENSOR_INIT_HEIGHT;
1784 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
1787 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr)
1789 ret |= sensor_write_array(client, winseqe_set_addr);
1791 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1792 goto sensor_s_fmt_end;
1795 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1798 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1802 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1806 pix->height = set_h;
1809 gpio_direction_output(RK29_PIN6_PB7,0);
1810 gpio_set_value(RK29_PIN6_PB7, 0);
1814 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1816 struct v4l2_pix_format *pix = &f->fmt.pix;
1817 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
1818 pix->pixelformat == V4L2_PIX_FMT_YUYV;
1821 * With Bayer format enforce even side lengths, but let the user play
1822 * with the starting pixel
1825 if (pix->height > SENSOR_MAX_HEIGHT)
1826 pix->height = SENSOR_MAX_HEIGHT;
1827 else if (pix->height < SENSOR_MIN_HEIGHT)
1828 pix->height = SENSOR_MIN_HEIGHT;
1830 pix->height = ALIGN(pix->height, 2);
1832 if (pix->width > SENSOR_MAX_WIDTH)
1833 pix->width = SENSOR_MAX_WIDTH;
1834 else if (pix->width < SENSOR_MIN_WIDTH)
1835 pix->width = SENSOR_MIN_WIDTH;
1837 pix->width = ALIGN(pix->width, 2);
1842 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1844 struct i2c_client *client = sd->priv;
1846 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1849 if (id->match.addr != client->addr)
1852 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
1857 #if CONFIG_SENSOR_Brightness
1858 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1860 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1862 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1864 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1866 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1868 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1871 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1875 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1879 #if CONFIG_SENSOR_Effect
1880 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1882 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1884 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1886 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1888 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1890 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1893 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1897 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1901 #if CONFIG_SENSOR_Exposure
1902 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1904 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1906 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1908 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1910 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1912 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1915 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1919 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1923 #if CONFIG_SENSOR_Saturation
1924 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1926 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1928 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1930 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1932 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1934 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1937 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1941 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1945 #if CONFIG_SENSOR_Contrast
1946 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1948 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1950 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1952 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1954 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1956 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1959 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1963 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1967 #if CONFIG_SENSOR_Mirror
1968 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1970 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1972 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1974 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1976 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1978 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1981 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1985 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1989 #if CONFIG_SENSOR_Flip
1990 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1992 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1994 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1996 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1998 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2000 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2003 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2007 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2011 #if CONFIG_SENSOR_Scene
2012 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2014 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2016 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2018 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2020 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2022 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2025 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2029 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2033 #if CONFIG_SENSOR_WhiteBalance
2034 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2036 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2038 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2040 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2042 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2044 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2047 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2051 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2055 #if CONFIG_SENSOR_DigitalZoom
2056 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2058 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2059 struct sensor *sensor = to_sensor(client);
2060 const struct v4l2_queryctrl *qctrl_info;
2061 int digitalzoom_cur, digitalzoom_total;
2063 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2067 digitalzoom_cur = sensor->info_priv.digitalzoom;
2068 digitalzoom_total = qctrl_info->maximum;
2070 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2072 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2076 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2078 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2082 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2084 *value = digitalzoom_total - digitalzoom_cur;
2087 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2089 *value = 0 - digitalzoom_cur;
2092 digitalzoom_cur += *value;
2094 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2096 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2098 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2101 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2108 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2110 struct i2c_client *client = sd->priv;
2111 struct sensor *sensor = to_sensor(client);
2112 const struct v4l2_queryctrl *qctrl;
2114 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2118 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2124 case V4L2_CID_BRIGHTNESS:
2126 ctrl->value = sensor->info_priv.brightness;
2129 case V4L2_CID_SATURATION:
2131 ctrl->value = sensor->info_priv.saturation;
2134 case V4L2_CID_CONTRAST:
2136 ctrl->value = sensor->info_priv.contrast;
2139 case V4L2_CID_DO_WHITE_BALANCE:
2141 ctrl->value = sensor->info_priv.whiteBalance;
2144 case V4L2_CID_EXPOSURE:
2146 ctrl->value = sensor->info_priv.exposure;
2149 case V4L2_CID_HFLIP:
2151 ctrl->value = sensor->info_priv.mirror;
2154 case V4L2_CID_VFLIP:
2156 ctrl->value = sensor->info_priv.flip;
2167 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2169 struct i2c_client *client = sd->priv;
2170 struct sensor *sensor = to_sensor(client);
2171 struct soc_camera_device *icd = client->dev.platform_data;
2172 const struct v4l2_queryctrl *qctrl;
2175 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2179 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2185 #if CONFIG_SENSOR_Brightness
2186 case V4L2_CID_BRIGHTNESS:
2188 if (ctrl->value != sensor->info_priv.brightness)
2190 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2194 sensor->info_priv.brightness = ctrl->value;
2199 #if CONFIG_SENSOR_Exposure
2200 case V4L2_CID_EXPOSURE:
2202 if (ctrl->value != sensor->info_priv.exposure)
2204 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2208 sensor->info_priv.exposure = ctrl->value;
2213 #if CONFIG_SENSOR_Saturation
2214 case V4L2_CID_SATURATION:
2216 if (ctrl->value != sensor->info_priv.saturation)
2218 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2222 sensor->info_priv.saturation = ctrl->value;
2227 #if CONFIG_SENSOR_Contrast
2228 case V4L2_CID_CONTRAST:
2230 if (ctrl->value != sensor->info_priv.contrast)
2232 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2236 sensor->info_priv.contrast = ctrl->value;
2241 #if CONFIG_SENSOR_WhiteBalance
2242 case V4L2_CID_DO_WHITE_BALANCE:
2244 if (ctrl->value != sensor->info_priv.whiteBalance)
2246 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2250 sensor->info_priv.whiteBalance = ctrl->value;
2255 #if CONFIG_SENSOR_Mirror
2256 case V4L2_CID_HFLIP:
2258 if (ctrl->value != sensor->info_priv.mirror)
2260 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2262 sensor->info_priv.mirror = ctrl->value;
2267 #if CONFIG_SENSOR_Flip
2268 case V4L2_CID_VFLIP:
2270 if (ctrl->value != sensor->info_priv.flip)
2272 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2274 sensor->info_priv.flip = ctrl->value;
2285 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2287 const struct v4l2_queryctrl *qctrl;
2288 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2289 struct sensor *sensor = to_sensor(client);
2291 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2295 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2299 switch (ext_ctrl->id)
2301 case V4L2_CID_SCENE:
2303 ext_ctrl->value = sensor->info_priv.scene;
2306 case V4L2_CID_EFFECT:
2308 ext_ctrl->value = sensor->info_priv.effect;
2311 case V4L2_CID_ZOOM_ABSOLUTE:
2313 ext_ctrl->value = sensor->info_priv.digitalzoom;
2316 case V4L2_CID_ZOOM_RELATIVE:
2320 case V4L2_CID_FOCUS_ABSOLUTE:
2322 ext_ctrl->value = sensor->info_priv.focus;
2325 case V4L2_CID_FOCUS_RELATIVE:
2329 case V4L2_CID_FLASH:
2331 ext_ctrl->value = sensor->info_priv.flash;
2339 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2341 const struct v4l2_queryctrl *qctrl;
2342 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2343 struct sensor *sensor = to_sensor(client);
2346 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2350 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2355 switch (ext_ctrl->id)
2357 #if CONFIG_SENSOR_Scene
2358 case V4L2_CID_SCENE:
2360 if (ext_ctrl->value != sensor->info_priv.scene)
2362 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2364 sensor->info_priv.scene = ext_ctrl->value;
2369 #if CONFIG_SENSOR_Effect
2370 case V4L2_CID_EFFECT:
2372 if (ext_ctrl->value != sensor->info_priv.effect)
2374 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2376 sensor->info_priv.effect= ext_ctrl->value;
2381 #if CONFIG_SENSOR_DigitalZoom
2382 case V4L2_CID_ZOOM_ABSOLUTE:
2384 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2387 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2389 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2391 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2393 sensor->info_priv.digitalzoom += val_offset;
2395 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2400 case V4L2_CID_ZOOM_RELATIVE:
2402 if (ext_ctrl->value)
2404 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2406 sensor->info_priv.digitalzoom += ext_ctrl->value;
2408 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2413 #if CONFIG_SENSOR_Focus
2414 case V4L2_CID_FOCUS_ABSOLUTE:
2416 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2419 if (ext_ctrl->value != sensor->info_priv.focus)
2421 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2423 sensor->info_priv.focus += val_offset;
2428 case V4L2_CID_FOCUS_RELATIVE:
2430 if (ext_ctrl->value)
2432 sensor->info_priv.focus += ext_ctrl->value;
2434 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2439 #if CONFIG_SENSOR_Flash
2440 case V4L2_CID_FLASH:
2442 sensor->info_priv.flash = ext_ctrl->value;
2444 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2455 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2457 struct i2c_client *client = sd->priv;
2458 struct soc_camera_device *icd = client->dev.platform_data;
2459 int i, error_cnt=0, error_idx=-1;
2462 for (i=0; i<ext_ctrl->count; i++) {
2463 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2470 error_idx = ext_ctrl->count;
2472 if (error_idx != -1) {
2473 ext_ctrl->error_idx = error_idx;
2480 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2482 struct i2c_client *client = sd->priv;
2483 struct soc_camera_device *icd = client->dev.platform_data;
2484 int i, error_cnt=0, error_idx=-1;
2487 for (i=0; i<ext_ctrl->count; i++) {
2488 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2495 error_idx = ext_ctrl->count;
2497 if (error_idx != -1) {
2498 ext_ctrl->error_idx = error_idx;
2505 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2506 * this wasn't our capture interface, so, we wait for the right one */
2507 static int sensor_video_probe(struct soc_camera_device *icd,
2508 struct i2c_client *client)
2512 struct sensor *sensor = to_sensor(client);
2514 /* We must have a parent by now. And it cannot be a wrong one.
2515 * So this entire test is completely redundant. */
2516 if (!icd->dev.parent ||
2517 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2520 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2522 goto sensor_video_probe_err;
2526 ret = sensor_write(client, 0x0103, 0x01);
2528 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2530 goto sensor_video_probe_err;
2532 mdelay(5); //delay 5 microseconds
2534 /* check if it is an sensor sensor */
2535 ret = sensor_read(client, 0x300a, &value);
2537 SENSOR_TR("read chip id high byte failed\n");
2539 goto sensor_video_probe_err;
2542 pid |= (value << 8);
2544 ret = sensor_read(client, 0x300b, &value);
2546 SENSOR_TR("read chip id low byte failed\n");
2548 goto sensor_video_probe_err;
2551 pid |= (value & 0xff);
2552 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2553 if (pid == SENSOR_ID) {
2554 sensor->model = SENSOR_V4L2_IDENT;
2556 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2558 goto sensor_video_probe_err;
2561 icd->formats = sensor_colour_formats;
2562 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
2566 sensor_video_probe_err:
2570 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2572 struct i2c_client *client = sd->priv;
2573 struct sensor *sensor = to_sensor(client);
2575 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2578 case RK29_CAM_SUBDEV_DEACTIVATE:
2580 sensor_deactivate(client);
2584 case RK29_CAM_SUBDEV_IOREQUEST:
2586 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2591 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2600 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2601 .init = sensor_init,
2602 .g_ctrl = sensor_g_control,
2603 .s_ctrl = sensor_s_control,
2604 .g_ext_ctrls = sensor_g_ext_controls,
2605 .s_ext_ctrls = sensor_s_ext_controls,
2606 .g_chip_ident = sensor_g_chip_ident,
2607 .ioctl = sensor_ioctl,
2610 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2611 .s_fmt = sensor_s_fmt,
2612 .g_fmt = sensor_g_fmt,
2613 .try_fmt = sensor_try_fmt,
2616 static struct v4l2_subdev_ops sensor_subdev_ops = {
2617 .core = &sensor_subdev_core_ops,
2618 .video = &sensor_subdev_video_ops,
2621 static int sensor_probe(struct i2c_client *client,
2622 const struct i2c_device_id *did)
2624 struct sensor *sensor;
2625 struct soc_camera_device *icd = client->dev.platform_data;
2626 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2627 struct soc_camera_link *icl;
2630 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2632 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2636 icl = to_soc_camera_link(icd);
2638 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2642 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2643 dev_warn(&adapter->dev,
2644 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2648 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2652 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2654 /* Second stage probe - when a capture adapter is there */
2655 icd->ops = &sensor_ops;
2656 icd->y_skip_top = 0;
2657 #if CONFIG_SENSOR_I2C_NOSCHED
2658 atomic_set(&sensor->tasklock_cnt,0);
2661 ret = sensor_video_probe(icd, client);
2664 i2c_set_clientdata(client, NULL);
2667 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2671 static int sensor_remove(struct i2c_client *client)
2673 struct sensor *sensor = to_sensor(client);
2674 struct soc_camera_device *icd = client->dev.platform_data;
2677 i2c_set_clientdata(client, NULL);
2678 client->driver = NULL;
2684 static const struct i2c_device_id sensor_id[] = {
2685 {SENSOR_NAME_STRING(), 0 },
2688 MODULE_DEVICE_TABLE(i2c, sensor_id);
2690 static struct i2c_driver sensor_i2c_driver = {
2692 .name = SENSOR_NAME_STRING(),
2694 .probe = sensor_probe,
2695 .remove = sensor_remove,
2696 .id_table = sensor_id,
2699 static int __init sensor_mod_init(void)
2701 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2702 return i2c_add_driver(&sensor_i2c_driver);
2705 static void __exit sensor_mod_exit(void)
2707 i2c_del_driver(&sensor_i2c_driver);
2710 device_initcall_sync(sensor_mod_init);
2711 module_exit(sensor_mod_exit);
2713 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2714 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2715 MODULE_LICENSE("GPL");