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;
1690 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
1693 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1694 #if CONFIG_SENSOR_Flash
1695 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
1696 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1697 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1698 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1700 } else { /* ddl@rock-chips.com : Video */
1701 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1702 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1703 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1707 ret |= sensor_write_array(client, winseqe_set_addr);
1709 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1710 #if CONFIG_SENSOR_Flash
1711 if (sensor_fmt_capturechk(sd,f) == true) {
1712 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1713 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1714 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1718 goto sensor_s_fmt_end;
1721 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1723 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
1724 #if CONFIG_SENSOR_Effect
1725 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1726 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1728 #if CONFIG_SENSOR_WhiteBalance
1729 if (sensor->info_priv.whiteBalance != 0) {
1730 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1731 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1734 sensor->info_priv.snap2preview = true;
1735 } else if (sensor_fmt_videochk(sd,f) == true) { /* ddl@rock-chips.com : Video */
1736 #if CONFIG_SENSOR_Effect
1737 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1738 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1740 #if CONFIG_SENSOR_WhiteBalance
1741 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1742 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1744 sensor->info_priv.video2preview = true;
1745 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1746 #if CONFIG_SENSOR_Effect
1747 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1748 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1750 #if CONFIG_SENSOR_WhiteBalance
1751 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1752 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1754 sensor->info_priv.video2preview = false;
1755 sensor->info_priv.snap2preview = false;
1758 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1762 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1766 pix->height = set_h;
1772 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1774 struct v4l2_pix_format *pix = &f->fmt.pix;
1775 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
1776 pix->pixelformat == V4L2_PIX_FMT_YUYV;
1779 * With Bayer format enforce even side lengths, but let the user play
1780 * with the starting pixel
1783 if (pix->height > SENSOR_MAX_HEIGHT)
1784 pix->height = SENSOR_MAX_HEIGHT;
1785 else if (pix->height < SENSOR_MIN_HEIGHT)
1786 pix->height = SENSOR_MIN_HEIGHT;
1788 pix->height = ALIGN(pix->height, 2);
1790 if (pix->width > SENSOR_MAX_WIDTH)
1791 pix->width = SENSOR_MAX_WIDTH;
1792 else if (pix->width < SENSOR_MIN_WIDTH)
1793 pix->width = SENSOR_MIN_WIDTH;
1795 pix->width = ALIGN(pix->width, 2);
1800 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1802 struct i2c_client *client = sd->priv;
1804 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1807 if (id->match.addr != client->addr)
1810 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV9650 identifier */
1815 #if CONFIG_SENSOR_Brightness
1816 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1818 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1820 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1822 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1824 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1826 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1829 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1833 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1837 #if CONFIG_SENSOR_Effect
1838 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1840 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1842 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1844 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1846 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1848 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1851 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1855 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1859 #if CONFIG_SENSOR_Exposure
1860 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1862 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1864 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1866 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1868 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1870 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1873 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1877 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1881 #if CONFIG_SENSOR_Saturation
1882 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1884 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1886 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1888 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1890 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1892 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1895 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1899 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1903 #if CONFIG_SENSOR_Contrast
1904 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1906 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1908 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1910 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1912 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1914 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1917 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1921 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1925 #if CONFIG_SENSOR_Mirror
1926 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1928 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1930 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1932 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1934 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1936 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1939 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1943 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1947 #if CONFIG_SENSOR_Flip
1948 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1950 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1952 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1954 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1956 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1958 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1961 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1965 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1969 #if CONFIG_SENSOR_Scene
1970 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1972 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1974 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1976 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1978 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1980 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1983 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1987 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1991 #if CONFIG_SENSOR_WhiteBalance
1992 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1994 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1996 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1998 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2000 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2002 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2005 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2009 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2013 #if CONFIG_SENSOR_DigitalZoom
2014 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2016 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2017 struct sensor *sensor = to_sensor(client);
2018 const struct v4l2_queryctrl *qctrl_info;
2019 int digitalzoom_cur, digitalzoom_total;
2021 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2025 digitalzoom_cur = sensor->info_priv.digitalzoom;
2026 digitalzoom_total = qctrl_info->maximum;
2028 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2030 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2034 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2036 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2040 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2042 *value = digitalzoom_total - digitalzoom_cur;
2045 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2047 *value = 0 - digitalzoom_cur;
2050 digitalzoom_cur += *value;
2052 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2054 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2056 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2059 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2066 #if CONFIG_SENSOR_Flash
2067 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2069 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2070 if (value == 3) { /* ddl@rock-chips.com: torch */
2071 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2073 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2075 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2079 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2084 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2086 struct i2c_client *client = sd->priv;
2087 struct sensor *sensor = to_sensor(client);
2088 const struct v4l2_queryctrl *qctrl;
2090 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2094 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2100 case V4L2_CID_BRIGHTNESS:
2102 ctrl->value = sensor->info_priv.brightness;
2105 case V4L2_CID_SATURATION:
2107 ctrl->value = sensor->info_priv.saturation;
2110 case V4L2_CID_CONTRAST:
2112 ctrl->value = sensor->info_priv.contrast;
2115 case V4L2_CID_DO_WHITE_BALANCE:
2117 ctrl->value = sensor->info_priv.whiteBalance;
2120 case V4L2_CID_EXPOSURE:
2122 ctrl->value = sensor->info_priv.exposure;
2125 case V4L2_CID_HFLIP:
2127 ctrl->value = sensor->info_priv.mirror;
2130 case V4L2_CID_VFLIP:
2132 ctrl->value = sensor->info_priv.flip;
2143 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2145 struct i2c_client *client = sd->priv;
2146 struct sensor *sensor = to_sensor(client);
2147 struct soc_camera_device *icd = client->dev.platform_data;
2148 const struct v4l2_queryctrl *qctrl;
2151 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2155 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2161 #if CONFIG_SENSOR_Brightness
2162 case V4L2_CID_BRIGHTNESS:
2164 if (ctrl->value != sensor->info_priv.brightness)
2166 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2170 sensor->info_priv.brightness = ctrl->value;
2175 #if CONFIG_SENSOR_Exposure
2176 case V4L2_CID_EXPOSURE:
2178 if (ctrl->value != sensor->info_priv.exposure)
2180 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2184 sensor->info_priv.exposure = ctrl->value;
2189 #if CONFIG_SENSOR_Saturation
2190 case V4L2_CID_SATURATION:
2192 if (ctrl->value != sensor->info_priv.saturation)
2194 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2198 sensor->info_priv.saturation = ctrl->value;
2203 #if CONFIG_SENSOR_Contrast
2204 case V4L2_CID_CONTRAST:
2206 if (ctrl->value != sensor->info_priv.contrast)
2208 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2212 sensor->info_priv.contrast = ctrl->value;
2217 #if CONFIG_SENSOR_WhiteBalance
2218 case V4L2_CID_DO_WHITE_BALANCE:
2220 if (ctrl->value != sensor->info_priv.whiteBalance)
2222 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2226 sensor->info_priv.whiteBalance = ctrl->value;
2231 #if CONFIG_SENSOR_Mirror
2232 case V4L2_CID_HFLIP:
2234 if (ctrl->value != sensor->info_priv.mirror)
2236 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2238 sensor->info_priv.mirror = ctrl->value;
2243 #if CONFIG_SENSOR_Flip
2244 case V4L2_CID_VFLIP:
2246 if (ctrl->value != sensor->info_priv.flip)
2248 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2250 sensor->info_priv.flip = ctrl->value;
2261 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2263 const struct v4l2_queryctrl *qctrl;
2264 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2265 struct sensor *sensor = to_sensor(client);
2267 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2271 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2275 switch (ext_ctrl->id)
2277 case V4L2_CID_SCENE:
2279 ext_ctrl->value = sensor->info_priv.scene;
2282 case V4L2_CID_EFFECT:
2284 ext_ctrl->value = sensor->info_priv.effect;
2287 case V4L2_CID_ZOOM_ABSOLUTE:
2289 ext_ctrl->value = sensor->info_priv.digitalzoom;
2292 case V4L2_CID_ZOOM_RELATIVE:
2296 case V4L2_CID_FOCUS_ABSOLUTE:
2298 ext_ctrl->value = sensor->info_priv.focus;
2301 case V4L2_CID_FOCUS_RELATIVE:
2305 case V4L2_CID_FLASH:
2307 ext_ctrl->value = sensor->info_priv.flash;
2315 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2317 const struct v4l2_queryctrl *qctrl;
2318 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2319 struct sensor *sensor = to_sensor(client);
2322 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2326 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2331 switch (ext_ctrl->id)
2333 #if CONFIG_SENSOR_Scene
2334 case V4L2_CID_SCENE:
2336 if (ext_ctrl->value != sensor->info_priv.scene)
2338 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2340 sensor->info_priv.scene = ext_ctrl->value;
2345 #if CONFIG_SENSOR_Effect
2346 case V4L2_CID_EFFECT:
2348 if (ext_ctrl->value != sensor->info_priv.effect)
2350 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2352 sensor->info_priv.effect= ext_ctrl->value;
2357 #if CONFIG_SENSOR_DigitalZoom
2358 case V4L2_CID_ZOOM_ABSOLUTE:
2360 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2363 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2365 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2367 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2369 sensor->info_priv.digitalzoom += val_offset;
2371 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2376 case V4L2_CID_ZOOM_RELATIVE:
2378 if (ext_ctrl->value)
2380 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2382 sensor->info_priv.digitalzoom += ext_ctrl->value;
2384 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2389 #if CONFIG_SENSOR_Focus
2390 case V4L2_CID_FOCUS_ABSOLUTE:
2392 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2395 if (ext_ctrl->value != sensor->info_priv.focus)
2397 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2399 sensor->info_priv.focus += val_offset;
2404 case V4L2_CID_FOCUS_RELATIVE:
2406 if (ext_ctrl->value)
2408 sensor->info_priv.focus += ext_ctrl->value;
2410 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2415 #if CONFIG_SENSOR_Flash
2416 case V4L2_CID_FLASH:
2418 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2420 sensor->info_priv.flash = ext_ctrl->value;
2422 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2433 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2435 struct i2c_client *client = sd->priv;
2436 struct soc_camera_device *icd = client->dev.platform_data;
2437 int i, error_cnt=0, error_idx=-1;
2440 for (i=0; i<ext_ctrl->count; i++) {
2441 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2448 error_idx = ext_ctrl->count;
2450 if (error_idx != -1) {
2451 ext_ctrl->error_idx = error_idx;
2458 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2460 struct i2c_client *client = sd->priv;
2461 struct soc_camera_device *icd = client->dev.platform_data;
2462 int i, error_cnt=0, error_idx=-1;
2465 for (i=0; i<ext_ctrl->count; i++) {
2466 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2473 error_idx = ext_ctrl->count;
2475 if (error_idx != -1) {
2476 ext_ctrl->error_idx = error_idx;
2483 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2484 * this wasn't our capture interface, so, we wait for the right one */
2485 static int sensor_video_probe(struct soc_camera_device *icd,
2486 struct i2c_client *client)
2490 struct sensor *sensor = to_sensor(client);
2492 /* We must have a parent by now. And it cannot be a wrong one.
2493 * So this entire test is completely redundant. */
2494 if (!icd->dev.parent ||
2495 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2498 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2500 goto sensor_video_probe_err;
2503 ret = sensor_write(client, 0x12, 0x80);
2506 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2509 mdelay(50); //delay 5 microseconds
2511 /* check if it is an sensor sensor */
2512 ret = sensor_read(client, 0x0a, &pid);
2514 SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2516 goto sensor_video_probe_err;
2519 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2520 if (pid == SENSOR_ID) {
2521 sensor->model = SENSOR_V4L2_IDENT;
2523 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2525 goto sensor_video_probe_err;
2528 icd->formats = sensor_colour_formats;
2529 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
2533 sensor_video_probe_err:
2538 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2540 struct i2c_client *client = sd->priv;
2541 struct soc_camera_device *icd = client->dev.platform_data;
2542 struct sensor *sensor = to_sensor(client);
2544 #if CONFIG_SENSOR_Flash
2548 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2551 case RK29_CAM_SUBDEV_DEACTIVATE:
2553 sensor_deactivate(client);
2557 case RK29_CAM_SUBDEV_IOREQUEST:
2559 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2560 if (sensor->sensor_io_request != NULL) {
2561 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2562 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2563 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2564 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2565 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2566 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2569 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2571 goto sensor_ioctl_end;
2573 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2575 #if CONFIG_SENSOR_Flash
2576 if (sensor->sensor_gpio_res) {
2577 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2578 for (i = 0; i < icd->ops->num_controls; i++) {
2579 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2580 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2583 sensor->info_priv.flash = 0xff;
2584 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2592 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);
2668 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2672 static int sensor_remove(struct i2c_client *client)
2674 struct sensor *sensor = to_sensor(client);
2675 struct soc_camera_device *icd = client->dev.platform_data;
2678 i2c_set_clientdata(client, NULL);
2679 client->driver = NULL;
2685 static const struct i2c_device_id sensor_id[] = {
2686 {SENSOR_NAME_STRING(), 0 },
2689 MODULE_DEVICE_TABLE(i2c, sensor_id);
2691 static struct i2c_driver sensor_i2c_driver = {
2693 .name = SENSOR_NAME_STRING(),
2695 .probe = sensor_probe,
2696 .remove = sensor_remove,
2697 .id_table = sensor_id,
2700 static int __init sensor_mod_init(void)
2702 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2703 return i2c_add_driver(&sensor_i2c_driver);
2706 static void __exit sensor_mod_exit(void)
2708 i2c_del_driver(&sensor_i2c_driver);
2711 device_initcall_sync(sensor_mod_init);
2712 module_exit(sensor_mod_exit);
2714 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2715 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2716 MODULE_LICENSE("GPL");