3 o* Driver for MT9M001 CMOS Image Sensor from Micron
5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/videodev2.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/log2.h>
16 #include <linux/platform_device.h>
17 #include <linux/delay.h>
18 #include <linux/circ_buf.h>
19 #include <linux/miscdevice.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-chip-ident.h>
22 #include <media/soc_camera.h>
23 #include <mach/rk29_camera.h>
26 module_param(debug, int, S_IRUGO|S_IWUSR);
28 #define dprintk(level, fmt, arg...) do { \
30 printk(KERN_WARNING fmt , ## arg); } while (0)
32 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
33 #define SENSOR_DG(format, ...) dprintk(0, format, ## __VA_ARGS__)
36 #define _CONS(a,b) a##b
37 #define CONS(a,b) _CONS(a,b)
40 #define _STR(x) __STR(x)
41 #define STR(x) _STR(x)
43 #define MIN(x,y) ((x<y) ? x: y)
44 #define MAX(x,y) ((x>y) ? x: y)
46 /* Sensor Driver Configuration */
47 #define SENSOR_NAME RK29_CAM_SENSOR_OV7675
48 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV7675
49 #define SENSOR_ID 0x76
50 #define SENSOR_MIN_WIDTH 640//176
51 #define SENSOR_MIN_HEIGHT 480//144
52 #define SENSOR_MAX_WIDTH 640
53 #define SENSOR_MAX_HEIGHT 480
54 #define SENSOR_INIT_WIDTH 640 /* Sensor pixel size for sensor_init_data array */
55 #define SENSOR_INIT_HEIGHT 480
56 #define SENSOR_INIT_WINSEQADR sensor_vga
57 #define SENSOR_INIT_PIXFMT V4L2_PIX_FMT_YUYV
59 #define CONFIG_SENSOR_WhiteBalance 0
60 #define CONFIG_SENSOR_Brightness 0
61 #define CONFIG_SENSOR_Contrast 0
62 #define CONFIG_SENSOR_Saturation 0
63 #define CONFIG_SENSOR_Effect 1
64 #define CONFIG_SENSOR_Scene 0
65 #define CONFIG_SENSOR_DigitalZoom 0
66 #define CONFIG_SENSOR_Focus 0
67 #define CONFIG_SENSOR_Exposure 0
68 #define CONFIG_SENSOR_Flash 0
69 #define CONFIG_SENSOR_Mirror 0
70 #define CONFIG_SENSOR_Flip 0
72 #define CONFIG_SENSOR_I2C_SPEED 250000 /* Hz */
73 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
74 #define CONFIG_SENSOR_I2C_NOSCHED 0
75 #define CONFIG_SENSOR_I2C_RDWRCHK 0
77 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
78 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
79 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
81 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
82 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
83 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
84 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
85 #define COLOR_TEMPERATURE_OFFICE_DN 3500
86 #define COLOR_TEMPERATURE_OFFICE_UP 5000
87 #define COLOR_TEMPERATURE_HOME_DN 2500
88 #define COLOR_TEMPERATURE_HOME_UP 3500
90 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
91 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
99 /* init 640X480 VGA */
100 static struct reginfo sensor_init_data[] =
276 static struct reginfo sensor_sxga[] =
282 static struct reginfo sensor_svga[] =
288 static struct reginfo sensor_vga[] =
300 static struct reginfo sensor_cif[] =
306 static struct reginfo sensor_qvga[] =
312 static struct reginfo sensor_qcif[] =
317 static struct reginfo sensor_ClrFmt_YUYV[]=
322 static struct reginfo sensor_ClrFmt_UYVY[]=
328 #if CONFIG_SENSOR_WhiteBalance
329 static struct reginfo sensor_WhiteB_Auto[]=
331 {0x84, 0x6C}, //Contrast 4
355 {0x0f, 0x4a}, //Saturation 3
367 {0x4f, 0x2e}, //Brightness 3
381 /* Cloudy Colour Temperature : 6500K - 8000K */
382 static struct reginfo sensor_WhiteB_Cloudy[]=
384 {0x7C,0x04}, //Contrast 5
418 {0x4f,0x3a}, //Saturation 4
424 {0x4f, 0x2e}, //Brightness 3
437 /* ClearDay Colour Temperature : 5000K - 6500K */
438 static struct reginfo sensor_WhiteB_ClearDay[]=
441 {0x7C,0x04}, //Contrast 5
475 {0x4f,0x3a}, //Saturation 4
481 {0x4f, 0x2e}, //Brightness 3
494 /* Office Colour Temperature : 3500K - 5000K */
495 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
498 {0x84, 0x6C}, //Contrast 4
522 {0x0f, 0x4a}, //Saturation 3
534 {0x4f, 0x2e}, //Brightness 3
550 /* Home Colour Temperature : 2500K - 3500K */
551 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
554 {0x84, 0x6C}, //Contrast 4
578 {0x0f, 0x4a}, //Saturation 3
590 {0x4f, 0x2e}, //Brightness 3
603 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
604 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
608 #if CONFIG_SENSOR_Brightness
609 static struct reginfo sensor_Brightness0[]=
612 {0x3301, 0xff},//bit[7]:1, enable SDE
619 static struct reginfo sensor_Brightness1[]=
622 {0x3301, 0xff},//bit[7]:1, enable SDE
629 static struct reginfo sensor_Brightness2[]=
632 {0x3301, 0xff},//bit[7]:1, enable SDE
639 static struct reginfo sensor_Brightness3[]=
642 {0x3301, 0xff},//bit[7]:1, enable SDE
649 static struct reginfo sensor_Brightness4[]=
652 {0x3301, 0xff},//bit[7]:1, enable SDE
659 static struct reginfo sensor_Brightness5[]=
662 {0x3301, 0xff},//bit[7]:1, enable SDE
663 {0x3391, 0x04}, //bit[2] enable
664 {0x3390, 0x41}, //bit[3] sign of brightness
668 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
669 sensor_Brightness4, sensor_Brightness5,NULL,
674 #if CONFIG_SENSOR_Effect
675 static struct reginfo sensor_Effect_Normal[] =
683 static struct reginfo sensor_Effect_WandB[] =
691 static struct reginfo sensor_Effect_Sepia[] =
699 static struct reginfo sensor_Effect_Negative[] =
707 static struct reginfo sensor_Effect_Bluish[] =
716 static struct reginfo sensor_Effect_Green[] =
724 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
725 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
728 #if CONFIG_SENSOR_Exposure
729 static struct reginfo sensor_Exposure0[]=
734 static struct reginfo sensor_Exposure1[]=
739 static struct reginfo sensor_Exposure2[]=
744 static struct reginfo sensor_Exposure3[]=
749 static struct reginfo sensor_Exposure4[]=
754 static struct reginfo sensor_Exposure5[]=
759 static struct reginfo sensor_Exposure6[]=
764 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
765 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
768 #if CONFIG_SENSOR_Saturation
769 static struct reginfo sensor_Saturation0[]=
774 static struct reginfo sensor_Saturation1[]=
779 static struct reginfo sensor_Saturation2[]=
783 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
786 #if CONFIG_SENSOR_Contrast
787 static struct reginfo sensor_Contrast0[]=
792 static struct reginfo sensor_Contrast1[]=
797 static struct reginfo sensor_Contrast2[]=
802 static struct reginfo sensor_Contrast3[]=
807 static struct reginfo sensor_Contrast4[]=
813 static struct reginfo sensor_Contrast5[]=
818 static struct reginfo sensor_Contrast6[]=
822 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
823 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
827 #if CONFIG_SENSOR_Mirror
828 static struct reginfo sensor_MirrorOn[]=
833 static struct reginfo sensor_MirrorOff[]=
837 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
839 #if CONFIG_SENSOR_Flip
840 static struct reginfo sensor_FlipOn[]=
845 static struct reginfo sensor_FlipOff[]=
849 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
852 #if CONFIG_SENSOR_Scene
853 static struct reginfo sensor_SceneAuto[] =
858 static struct reginfo sensor_SceneNight[] =
862 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
865 #if CONFIG_SENSOR_DigitalZoom
866 static struct reginfo sensor_Zoom0[] =
871 static struct reginfo sensor_Zoom1[] =
876 static struct reginfo sensor_Zoom2[] =
882 static struct reginfo sensor_Zoom3[] =
886 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
888 static const struct v4l2_querymenu sensor_menus[] =
890 #if CONFIG_SENSOR_WhiteBalance
891 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
892 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
893 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
896 #if CONFIG_SENSOR_Effect
897 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
898 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
899 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
902 #if CONFIG_SENSOR_Scene
903 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
906 #if CONFIG_SENSOR_Flash
907 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
908 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
912 static const struct v4l2_queryctrl sensor_controls[] =
914 #if CONFIG_SENSOR_WhiteBalance
916 .id = V4L2_CID_DO_WHITE_BALANCE,
917 .type = V4L2_CTRL_TYPE_MENU,
918 .name = "White Balance Control",
926 #if CONFIG_SENSOR_Brightness
928 .id = V4L2_CID_BRIGHTNESS,
929 .type = V4L2_CTRL_TYPE_INTEGER,
930 .name = "Brightness Control",
938 #if CONFIG_SENSOR_Effect
940 .id = V4L2_CID_EFFECT,
941 .type = V4L2_CTRL_TYPE_MENU,
942 .name = "Effect Control",
950 #if CONFIG_SENSOR_Exposure
952 .id = V4L2_CID_EXPOSURE,
953 .type = V4L2_CTRL_TYPE_INTEGER,
954 .name = "Exposure Control",
962 #if CONFIG_SENSOR_Saturation
964 .id = V4L2_CID_SATURATION,
965 .type = V4L2_CTRL_TYPE_INTEGER,
966 .name = "Saturation Control",
974 #if CONFIG_SENSOR_Contrast
976 .id = V4L2_CID_CONTRAST,
977 .type = V4L2_CTRL_TYPE_INTEGER,
978 .name = "Contrast Control",
986 #if CONFIG_SENSOR_Mirror
988 .id = V4L2_CID_HFLIP,
989 .type = V4L2_CTRL_TYPE_BOOLEAN,
990 .name = "Mirror Control",
998 #if CONFIG_SENSOR_Flip
1000 .id = V4L2_CID_VFLIP,
1001 .type = V4L2_CTRL_TYPE_BOOLEAN,
1002 .name = "Flip Control",
1010 #if CONFIG_SENSOR_Scene
1012 .id = V4L2_CID_SCENE,
1013 .type = V4L2_CTRL_TYPE_MENU,
1014 .name = "Scene Control",
1022 #if CONFIG_SENSOR_DigitalZoom
1024 .id = V4L2_CID_ZOOM_RELATIVE,
1025 .type = V4L2_CTRL_TYPE_INTEGER,
1026 .name = "DigitalZoom Control",
1032 .id = V4L2_CID_ZOOM_ABSOLUTE,
1033 .type = V4L2_CTRL_TYPE_INTEGER,
1034 .name = "DigitalZoom Control",
1042 #if CONFIG_SENSOR_Focus
1044 .id = V4L2_CID_FOCUS_RELATIVE,
1045 .type = V4L2_CTRL_TYPE_INTEGER,
1046 .name = "Focus Control",
1052 .id = V4L2_CID_FOCUS_ABSOLUTE,
1053 .type = V4L2_CTRL_TYPE_INTEGER,
1054 .name = "Focus Control",
1058 .default_value = 125,
1062 #if CONFIG_SENSOR_Flash
1064 .id = V4L2_CID_FLASH,
1065 .type = V4L2_CTRL_TYPE_MENU,
1066 .name = "Flash Control",
1075 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1076 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1077 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1078 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1079 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1080 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1081 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1082 static int sensor_resume(struct soc_camera_device *icd);
1083 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1084 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1085 #if CONFIG_SENSOR_Effect
1086 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1088 #if CONFIG_SENSOR_WhiteBalance
1089 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1091 static int sensor_deactivate(struct i2c_client *client);
1094 static struct soc_camera_ops sensor_ops =
1096 .suspend = sensor_suspend,
1097 .resume = sensor_resume,
1098 .set_bus_param = sensor_set_bus_param,
1099 .query_bus_param = sensor_query_bus_param,
1100 .controls = sensor_controls,
1101 .menus = sensor_menus,
1102 .num_controls = ARRAY_SIZE(sensor_controls),
1103 .num_menus = ARRAY_SIZE(sensor_menus),
1106 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1107 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1108 .colorspace = _colorspace }
1110 #define JPG_FMT(_name, _depth, _fourcc) \
1111 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1113 static const struct soc_camera_data_format sensor_colour_formats[] = {
1114 JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1115 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1118 typedef struct sensor_info_priv_s
1132 unsigned char mirror; /* HFLIP */
1133 unsigned char flip; /* VFLIP */
1134 unsigned int winseqe_cur_addr;
1135 unsigned int pixfmt;
1137 } sensor_info_priv_t;
1141 struct v4l2_subdev subdev;
1142 struct i2c_client *client;
1143 sensor_info_priv_t info_priv;
1144 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1145 #if CONFIG_SENSOR_I2C_NOSCHED
1146 atomic_t tasklock_cnt;
1148 struct rk29camera_platform_data *sensor_io_request;
1149 struct rk29camera_gpio_res *sensor_gpio_res;
1152 static struct sensor* to_sensor(const struct i2c_client *client)
1154 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1157 static int sensor_task_lock(struct i2c_client *client, int lock)
1159 #if CONFIG_SENSOR_I2C_NOSCHED
1161 struct sensor *sensor = to_sensor(client);
1164 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1165 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1166 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1170 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1171 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1172 goto sensor_task_lock_err;
1177 atomic_add(1, &sensor->tasklock_cnt);
1179 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1180 atomic_sub(1, &sensor->tasklock_cnt);
1182 if (atomic_read(&sensor->tasklock_cnt) == 0)
1187 sensor_task_lock_err:
1195 /* sensor register write */
1196 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1200 struct i2c_msg msg[1];
1202 buf[0] = reg & 0xFF;
1205 msg->addr = client->addr;
1206 msg->flags = client->flags;
1208 msg->len = sizeof(buf);
1209 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1210 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1215 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1216 err = i2c_transfer(client->adapter, msg, 1);
1221 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1229 /* sensor register read */
1230 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1235 struct i2c_msg msg[2];
1237 //buf[0] = reg >> 8;
1239 buf[1] = reg & 0xFF;
1241 msg[0].addr = client->addr;
1242 msg[0].flags = client->flags;
1244 msg[0].len = sizeof(buf);
1245 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1246 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1248 msg[1].addr = client->addr;
1249 msg[1].flags = client->flags|I2C_M_RD;
1252 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1253 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1257 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1258 err = i2c_transfer(client->adapter, msg, 2);
1264 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1272 /* write a array of registers */
1274 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1278 #if CONFIG_SENSOR_I2C_RDWRCHK
1283 if (sensor_task_lock(client, 1) < 0)
1284 goto sensor_write_array_end;
1286 while (regarray[i].reg != 0)
1288 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1292 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1296 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1298 goto sensor_write_array_end;
1301 #if CONFIG_SENSOR_I2C_RDWRCHK
1302 sensor_read(client, regarray[i].reg, &valchk);
1303 if (valchk != regarray[i].val)
1304 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1310 sensor_write_array_end:
1311 sensor_task_lock(client,0);
1315 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1320 while (regarray[i].reg != 0)
1322 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1325 SENSOR_TR("%s..write failed current i = %d\n", SENSOR_NAME_STRING(),i);
1328 err = sensor_read(client, regarray[i].reg, &val_read);
1329 SENSOR_TR("%s..reg[0x%x]=0x%x,0x%x\n", SENSOR_NAME_STRING(),regarray[i].reg, val_read, regarray[i].val);
1335 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1337 struct soc_camera_link *icl = to_soc_camera_link(icd);
1340 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1343 case Sensor_PowerDown:
1345 if (icl->powerdown) {
1346 ret = icl->powerdown(icd->pdev, on);
1347 if (ret == RK29_CAM_IO_SUCCESS) {
1351 icl->reset(icd->pdev);
1353 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1355 goto sensor_power_end;
1362 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1363 struct sensor *sensor = to_sensor(client);
1365 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1366 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1372 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1379 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1381 struct i2c_client *client = sd->priv;
1382 struct soc_camera_device *icd = client->dev.platform_data;
1383 struct sensor *sensor = to_sensor(client);
1384 const struct v4l2_queryctrl *qctrl;
1387 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1389 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1391 goto sensor_INIT_ERR;
1395 if (sensor_task_lock(client,1)<0)
1396 goto sensor_INIT_ERR;
1397 ret = sensor_write(client, 0x12, 0x80);
1400 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1402 goto sensor_INIT_ERR;
1405 mdelay(5); //delay 5 microseconds
1407 ret = sensor_write_array(client, sensor_init_data);
1410 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1411 goto sensor_INIT_ERR;
1413 sensor_task_lock(client,0);
1414 //icd->user_width = SENSOR_INIT_WIDTH;
1415 //icd->user_height = SENSOR_INIT_HEIGHT;
1416 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1417 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
1419 /* sensor sensor information for initialization */
1420 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1422 sensor->info_priv.whiteBalance = qctrl->default_value;
1423 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1425 sensor->info_priv.brightness = qctrl->default_value;
1426 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1428 sensor->info_priv.effect = qctrl->default_value;
1429 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1431 sensor->info_priv.exposure = qctrl->default_value;
1433 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1435 sensor->info_priv.saturation = qctrl->default_value;
1436 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1438 sensor->info_priv.contrast = qctrl->default_value;
1439 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1441 sensor->info_priv.mirror = qctrl->default_value;
1442 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1444 sensor->info_priv.flip = qctrl->default_value;
1445 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1447 sensor->info_priv.scene = qctrl->default_value;
1448 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1450 sensor->info_priv.digitalzoom = qctrl->default_value;
1452 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1453 #if CONFIG_SENSOR_Focus
1455 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1457 sensor->info_priv.focus = qctrl->default_value;
1460 #if CONFIG_SENSOR_Flash
1461 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1463 sensor->info_priv.flash = qctrl->default_value;
1466 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);
1470 sensor_task_lock(client,0);
1471 sensor_deactivate(client);
1475 static int sensor_deactivate(struct i2c_client *client)
1477 struct soc_camera_device *icd = client->dev.platform_data;
1480 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1482 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1483 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1485 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1486 icd->user_width = SENSOR_INIT_WIDTH;
1487 icd->user_height = SENSOR_INIT_HEIGHT;
1491 static struct reginfo sensor_power_down_sequence[]=
1495 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1498 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1500 if (pm_msg.event == PM_EVENT_SUSPEND) {
1501 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1502 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1504 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1507 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1509 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1514 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1520 static int sensor_resume(struct soc_camera_device *icd)
1524 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1526 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1530 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1536 static int sensor_set_bus_param(struct soc_camera_device *icd,
1537 unsigned long flags)
1543 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1545 struct soc_camera_link *icl = to_soc_camera_link(icd);
1546 unsigned long flags = SENSOR_BUS_PARAM;
1548 return soc_camera_apply_sensor_flags(icl, flags);
1551 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1553 struct i2c_client *client = sd->priv;
1554 struct soc_camera_device *icd = client->dev.platform_data;
1555 struct sensor *sensor = to_sensor(client);
1556 struct v4l2_pix_format *pix = &f->fmt.pix;
1558 pix->width = icd->user_width;
1559 pix->height = icd->user_height;
1560 pix->pixelformat = sensor->info_priv.pixfmt;
1561 pix->field = V4L2_FIELD_NONE;
1562 pix->colorspace = V4L2_COLORSPACE_JPEG;
1566 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
1570 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
1572 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
1574 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
1576 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
1578 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
1583 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1587 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
1591 if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
1593 } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
1598 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1601 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1603 struct i2c_client *client = sd->priv;
1604 struct sensor *sensor = to_sensor(client);
1605 struct v4l2_pix_format *pix = &f->fmt.pix;
1606 const struct v4l2_queryctrl *qctrl;
1607 struct soc_camera_device *icd = client->dev.platform_data;
1608 struct reginfo *winseqe_set_addr=NULL;
1610 int ret=0, set_w,set_h;
1612 if (sensor->info_priv.pixfmt != pix->pixelformat) {
1613 switch (pix->pixelformat)
1615 case V4L2_PIX_FMT_YUYV:
1617 //winseqe_set_addr = sensor_ClrFmt_YUYV;
1618 sensor_read(client, 0x3a, &readval);
1619 sensor_write(client,0x3a, readval&0xf7);
1620 sensor_read(client,0x3d,&readval);
1621 sensor_write(client,0x3d,readval&0xfe);
1624 case V4L2_PIX_FMT_UYVY:
1626 //winseqe_set_addr = sensor_ClrFmt_UYVY;
1627 sensor_read(client, 0x3a, &readval);
1628 sensor_write(client,0x3a, readval|0x08);
1629 sensor_read(client,0x3d,&readval);
1630 sensor_write(client,0x3d,readval&0xfe);
1636 if (winseqe_set_addr != NULL) {
1637 sensor_write_array(client, winseqe_set_addr);
1638 sensor->info_priv.pixfmt = pix->pixelformat;
1640 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1642 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1647 set_h = pix->height;
1649 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1651 winseqe_set_addr = sensor_qcif;
1655 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1657 winseqe_set_addr = sensor_qvga;
1661 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1663 winseqe_set_addr = sensor_cif;
1667 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1669 winseqe_set_addr = sensor_vga;
1673 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1675 winseqe_set_addr = sensor_svga;
1679 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1681 winseqe_set_addr = sensor_sxga;
1687 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1688 set_w = SENSOR_INIT_WIDTH;
1689 set_h = SENSOR_INIT_HEIGHT;
1691 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
1694 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1695 #if CONFIG_SENSOR_Flash
1696 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
1697 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1698 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1699 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1701 } else { /* ddl@rock-chips.com : Video */
1702 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1703 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1704 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1708 ret |= sensor_write_array(client, winseqe_set_addr);
1710 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1711 #if CONFIG_SENSOR_Flash
1712 if (sensor_fmt_capturechk(sd,f) == true) {
1713 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1714 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1715 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1719 goto sensor_s_fmt_end;
1722 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1724 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
1725 #if CONFIG_SENSOR_Effect
1726 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1727 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1729 #if CONFIG_SENSOR_WhiteBalance
1730 if (sensor->info_priv.whiteBalance != 0) {
1731 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1732 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1735 sensor->info_priv.snap2preview = true;
1736 } else if (sensor_fmt_videochk(sd,f) == true) { /* ddl@rock-chips.com : Video */
1737 #if CONFIG_SENSOR_Effect
1738 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1739 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1741 #if CONFIG_SENSOR_WhiteBalance
1742 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1743 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1745 sensor->info_priv.video2preview = true;
1746 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1747 #if CONFIG_SENSOR_Effect
1748 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1749 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1751 #if CONFIG_SENSOR_WhiteBalance
1752 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1753 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1755 sensor->info_priv.video2preview = false;
1756 sensor->info_priv.snap2preview = false;
1759 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1763 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1767 pix->height = set_h;
1773 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1775 struct v4l2_pix_format *pix = &f->fmt.pix;
1776 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
1777 pix->pixelformat == V4L2_PIX_FMT_YUYV;
1780 * With Bayer format enforce even side lengths, but let the user play
1781 * with the starting pixel
1784 if (pix->height > SENSOR_MAX_HEIGHT)
1785 pix->height = SENSOR_MAX_HEIGHT;
1786 else if (pix->height < SENSOR_MIN_HEIGHT)
1787 pix->height = SENSOR_MIN_HEIGHT;
1789 pix->height = ALIGN(pix->height, 2);
1791 if (pix->width > SENSOR_MAX_WIDTH)
1792 pix->width = SENSOR_MAX_WIDTH;
1793 else if (pix->width < SENSOR_MIN_WIDTH)
1794 pix->width = SENSOR_MIN_WIDTH;
1796 pix->width = ALIGN(pix->width, 2);
1801 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1803 struct i2c_client *client = sd->priv;
1805 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1808 if (id->match.addr != client->addr)
1811 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV9650 identifier */
1816 #if CONFIG_SENSOR_Brightness
1817 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1819 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1821 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1823 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1825 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1827 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1830 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1834 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1838 #if CONFIG_SENSOR_Effect
1839 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1841 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1843 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1845 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1847 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1849 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1852 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1856 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1860 #if CONFIG_SENSOR_Exposure
1861 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1863 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1865 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1867 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1869 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1871 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1874 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1878 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1882 #if CONFIG_SENSOR_Saturation
1883 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1885 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1887 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1889 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1891 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1893 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1896 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1900 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1904 #if CONFIG_SENSOR_Contrast
1905 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1907 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1909 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1911 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1913 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1915 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1918 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1922 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1926 #if CONFIG_SENSOR_Mirror
1927 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1929 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1931 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1933 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1935 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1937 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1940 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1944 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1948 #if CONFIG_SENSOR_Flip
1949 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1951 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1953 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1955 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1957 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1959 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1962 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1966 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1970 #if CONFIG_SENSOR_Scene
1971 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1973 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1975 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1977 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1979 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1981 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1984 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1988 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1992 #if CONFIG_SENSOR_WhiteBalance
1993 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1995 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1997 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1999 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2001 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2003 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2006 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2010 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2014 #if CONFIG_SENSOR_DigitalZoom
2015 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2017 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2018 struct sensor *sensor = to_sensor(client);
2019 const struct v4l2_queryctrl *qctrl_info;
2020 int digitalzoom_cur, digitalzoom_total;
2022 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2026 digitalzoom_cur = sensor->info_priv.digitalzoom;
2027 digitalzoom_total = qctrl_info->maximum;
2029 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2031 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2035 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2037 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2041 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2043 *value = digitalzoom_total - digitalzoom_cur;
2046 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2048 *value = 0 - digitalzoom_cur;
2051 digitalzoom_cur += *value;
2053 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2055 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2057 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2060 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2067 #if CONFIG_SENSOR_Flash
2068 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2070 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2071 if (value == 3) { /* ddl@rock-chips.com: torch */
2072 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2074 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2076 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2080 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2085 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2087 struct i2c_client *client = sd->priv;
2088 struct sensor *sensor = to_sensor(client);
2089 const struct v4l2_queryctrl *qctrl;
2091 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2095 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2101 case V4L2_CID_BRIGHTNESS:
2103 ctrl->value = sensor->info_priv.brightness;
2106 case V4L2_CID_SATURATION:
2108 ctrl->value = sensor->info_priv.saturation;
2111 case V4L2_CID_CONTRAST:
2113 ctrl->value = sensor->info_priv.contrast;
2116 case V4L2_CID_DO_WHITE_BALANCE:
2118 ctrl->value = sensor->info_priv.whiteBalance;
2121 case V4L2_CID_EXPOSURE:
2123 ctrl->value = sensor->info_priv.exposure;
2126 case V4L2_CID_HFLIP:
2128 ctrl->value = sensor->info_priv.mirror;
2131 case V4L2_CID_VFLIP:
2133 ctrl->value = sensor->info_priv.flip;
2144 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2146 struct i2c_client *client = sd->priv;
2147 struct sensor *sensor = to_sensor(client);
2148 struct soc_camera_device *icd = client->dev.platform_data;
2149 const struct v4l2_queryctrl *qctrl;
2152 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2156 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2162 #if CONFIG_SENSOR_Brightness
2163 case V4L2_CID_BRIGHTNESS:
2165 if (ctrl->value != sensor->info_priv.brightness)
2167 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2171 sensor->info_priv.brightness = ctrl->value;
2176 #if CONFIG_SENSOR_Exposure
2177 case V4L2_CID_EXPOSURE:
2179 if (ctrl->value != sensor->info_priv.exposure)
2181 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2185 sensor->info_priv.exposure = ctrl->value;
2190 #if CONFIG_SENSOR_Saturation
2191 case V4L2_CID_SATURATION:
2193 if (ctrl->value != sensor->info_priv.saturation)
2195 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2199 sensor->info_priv.saturation = ctrl->value;
2204 #if CONFIG_SENSOR_Contrast
2205 case V4L2_CID_CONTRAST:
2207 if (ctrl->value != sensor->info_priv.contrast)
2209 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2213 sensor->info_priv.contrast = ctrl->value;
2218 #if CONFIG_SENSOR_WhiteBalance
2219 case V4L2_CID_DO_WHITE_BALANCE:
2221 if (ctrl->value != sensor->info_priv.whiteBalance)
2223 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2227 sensor->info_priv.whiteBalance = ctrl->value;
2232 #if CONFIG_SENSOR_Mirror
2233 case V4L2_CID_HFLIP:
2235 if (ctrl->value != sensor->info_priv.mirror)
2237 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2239 sensor->info_priv.mirror = ctrl->value;
2244 #if CONFIG_SENSOR_Flip
2245 case V4L2_CID_VFLIP:
2247 if (ctrl->value != sensor->info_priv.flip)
2249 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2251 sensor->info_priv.flip = ctrl->value;
2262 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2264 const struct v4l2_queryctrl *qctrl;
2265 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2266 struct sensor *sensor = to_sensor(client);
2268 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2272 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2276 switch (ext_ctrl->id)
2278 case V4L2_CID_SCENE:
2280 ext_ctrl->value = sensor->info_priv.scene;
2283 case V4L2_CID_EFFECT:
2285 ext_ctrl->value = sensor->info_priv.effect;
2288 case V4L2_CID_ZOOM_ABSOLUTE:
2290 ext_ctrl->value = sensor->info_priv.digitalzoom;
2293 case V4L2_CID_ZOOM_RELATIVE:
2297 case V4L2_CID_FOCUS_ABSOLUTE:
2299 ext_ctrl->value = sensor->info_priv.focus;
2302 case V4L2_CID_FOCUS_RELATIVE:
2306 case V4L2_CID_FLASH:
2308 ext_ctrl->value = sensor->info_priv.flash;
2316 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2318 const struct v4l2_queryctrl *qctrl;
2319 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2320 struct sensor *sensor = to_sensor(client);
2323 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2327 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2332 switch (ext_ctrl->id)
2334 #if CONFIG_SENSOR_Scene
2335 case V4L2_CID_SCENE:
2337 if (ext_ctrl->value != sensor->info_priv.scene)
2339 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2341 sensor->info_priv.scene = ext_ctrl->value;
2346 #if CONFIG_SENSOR_Effect
2347 case V4L2_CID_EFFECT:
2349 if (ext_ctrl->value != sensor->info_priv.effect)
2351 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2353 sensor->info_priv.effect= ext_ctrl->value;
2358 #if CONFIG_SENSOR_DigitalZoom
2359 case V4L2_CID_ZOOM_ABSOLUTE:
2361 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2364 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2366 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2368 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2370 sensor->info_priv.digitalzoom += val_offset;
2372 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2377 case V4L2_CID_ZOOM_RELATIVE:
2379 if (ext_ctrl->value)
2381 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2383 sensor->info_priv.digitalzoom += ext_ctrl->value;
2385 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2390 #if CONFIG_SENSOR_Focus
2391 case V4L2_CID_FOCUS_ABSOLUTE:
2393 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2396 if (ext_ctrl->value != sensor->info_priv.focus)
2398 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2400 sensor->info_priv.focus += val_offset;
2405 case V4L2_CID_FOCUS_RELATIVE:
2407 if (ext_ctrl->value)
2409 sensor->info_priv.focus += ext_ctrl->value;
2411 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2416 #if CONFIG_SENSOR_Flash
2417 case V4L2_CID_FLASH:
2419 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2421 sensor->info_priv.flash = ext_ctrl->value;
2423 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2434 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2436 struct i2c_client *client = sd->priv;
2437 struct soc_camera_device *icd = client->dev.platform_data;
2438 int i, error_cnt=0, error_idx=-1;
2441 for (i=0; i<ext_ctrl->count; i++) {
2442 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2449 error_idx = ext_ctrl->count;
2451 if (error_idx != -1) {
2452 ext_ctrl->error_idx = error_idx;
2459 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2461 struct i2c_client *client = sd->priv;
2462 struct soc_camera_device *icd = client->dev.platform_data;
2463 int i, error_cnt=0, error_idx=-1;
2466 for (i=0; i<ext_ctrl->count; i++) {
2467 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2474 error_idx = ext_ctrl->count;
2476 if (error_idx != -1) {
2477 ext_ctrl->error_idx = error_idx;
2484 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2485 * this wasn't our capture interface, so, we wait for the right one */
2486 static int sensor_video_probe(struct soc_camera_device *icd,
2487 struct i2c_client *client)
2491 struct sensor *sensor = to_sensor(client);
2493 /* We must have a parent by now. And it cannot be a wrong one.
2494 * So this entire test is completely redundant. */
2495 if (!icd->dev.parent ||
2496 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2499 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2501 goto sensor_video_probe_err;
2504 ret = sensor_write(client, 0x12, 0x80);
2507 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2510 mdelay(50); //delay 5 microseconds
2512 /* check if it is an sensor sensor */
2513 ret = sensor_read(client, 0x0a, &pid);
2515 SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2517 goto sensor_video_probe_err;
2520 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2521 if (pid == SENSOR_ID) {
2522 sensor->model = SENSOR_V4L2_IDENT;
2524 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2526 goto sensor_video_probe_err;
2529 icd->formats = sensor_colour_formats;
2530 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
2534 sensor_video_probe_err:
2539 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2541 struct i2c_client *client = sd->priv;
2542 struct soc_camera_device *icd = client->dev.platform_data;
2543 struct sensor *sensor = to_sensor(client);
2545 #if CONFIG_SENSOR_Flash
2549 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2552 case RK29_CAM_SUBDEV_DEACTIVATE:
2554 sensor_deactivate(client);
2558 case RK29_CAM_SUBDEV_IOREQUEST:
2560 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2561 if (sensor->sensor_io_request != NULL) {
2562 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2563 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2564 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2565 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2566 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2567 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2570 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2572 goto sensor_ioctl_end;
2574 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2576 #if CONFIG_SENSOR_Flash
2577 if (sensor->sensor_gpio_res) {
2578 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2579 for (i = 0; i < icd->ops->num_controls; i++) {
2580 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2581 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2584 sensor->info_priv.flash = 0xff;
2585 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2593 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2601 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2602 .init = sensor_init,
2603 .g_ctrl = sensor_g_control,
2604 .s_ctrl = sensor_s_control,
2605 .g_ext_ctrls = sensor_g_ext_controls,
2606 .s_ext_ctrls = sensor_s_ext_controls,
2607 .g_chip_ident = sensor_g_chip_ident,
2608 .ioctl = sensor_ioctl,
2611 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2612 .s_fmt = sensor_s_fmt,
2613 .g_fmt = sensor_g_fmt,
2614 .try_fmt = sensor_try_fmt,
2617 static struct v4l2_subdev_ops sensor_subdev_ops = {
2618 .core = &sensor_subdev_core_ops,
2619 .video = &sensor_subdev_video_ops,
2622 static int sensor_probe(struct i2c_client *client,
2623 const struct i2c_device_id *did)
2625 struct sensor *sensor;
2626 struct soc_camera_device *icd = client->dev.platform_data;
2627 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2628 struct soc_camera_link *icl;
2631 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2633 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2637 icl = to_soc_camera_link(icd);
2639 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2643 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2644 dev_warn(&adapter->dev,
2645 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2649 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2653 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2655 /* Second stage probe - when a capture adapter is there */
2656 icd->ops = &sensor_ops;
2657 icd->y_skip_top = 0;
2658 #if CONFIG_SENSOR_I2C_NOSCHED
2659 atomic_set(&sensor->tasklock_cnt,0);
2662 ret = sensor_video_probe(icd, client);
2665 i2c_set_clientdata(client, NULL);
2669 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2673 static int sensor_remove(struct i2c_client *client)
2675 struct sensor *sensor = to_sensor(client);
2676 struct soc_camera_device *icd = client->dev.platform_data;
2679 i2c_set_clientdata(client, NULL);
2680 client->driver = NULL;
2686 static const struct i2c_device_id sensor_id[] = {
2687 {SENSOR_NAME_STRING(), 0 },
2690 MODULE_DEVICE_TABLE(i2c, sensor_id);
2692 static struct i2c_driver sensor_i2c_driver = {
2694 .name = SENSOR_NAME_STRING(),
2696 .probe = sensor_probe,
2697 .remove = sensor_remove,
2698 .id_table = sensor_id,
2701 static int __init sensor_mod_init(void)
2703 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2704 return i2c_add_driver(&sensor_i2c_driver);
2707 static void __exit sensor_mod_exit(void)
2709 i2c_del_driver(&sensor_i2c_driver);
2712 device_initcall_sync(sensor_mod_init);
2713 module_exit(sensor_mod_exit);
2715 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2716 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2717 MODULE_LICENSE("GPL");