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(1, 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_MBUS_FMT_YUYV8_2X8
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[] =
147 {0x1e, 0x17}, //0x07//0x27
184 {0x7a, 0x20},// {0x7a, 0x24},
185 {0x7b, 0x16},// {0x7b, 0x04},
186 {0x7c, 0x23},// {0x7c, 0x07},
187 {0x7d, 0x3c},// {0x7d, 0x12},
188 {0x7e, 0x5c},// {0x7e, 0x2f},
189 {0x7f, 0x69},// {0x7f, 0x3f},
190 {0x80, 0x75},// {0x80, 0x4d},
191 {0x81, 0x7e},// {0x81, 0x5a},
192 {0x82, 0x88},// {0x82, 0x69},
193 {0x83, 0x8f},// {0x83, 0x74},
194 {0x84, 0x96},// {0x84, 0x7f},
195 {0x85, 0xa3},// {0x85, 0x91},
196 {0x86, 0xaf},// {0x86, 0x9e},
197 {0x87, 0xc4},// {0x87, 0xbb},
198 {0x88, 0xd7},// {0x88, 0xd2},
199 {0x89, 0xe8},// {0x89, 0xe5},
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 /* only one fixed colorspace per pixelcode */
1107 struct sensor_datafmt {
1108 enum v4l2_mbus_pixelcode code;
1109 enum v4l2_colorspace colorspace;
1112 /* Find a data format by a pixel code in an array */
1113 static const struct sensor_datafmt *sensor_find_datafmt(
1114 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1118 for (i = 0; i < n; i++)
1119 if (fmt[i].code == code)
1125 static const struct sensor_datafmt sensor_colour_fmts[] = {
1126 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1127 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1130 typedef struct sensor_info_priv_s
1144 unsigned char mirror; /* HFLIP */
1145 unsigned char flip; /* VFLIP */
1146 unsigned int winseqe_cur_addr;
1147 struct sensor_datafmt fmt;
1149 } sensor_info_priv_t;
1153 struct v4l2_subdev subdev;
1154 struct i2c_client *client;
1155 sensor_info_priv_t info_priv;
1156 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1157 #if CONFIG_SENSOR_I2C_NOSCHED
1158 atomic_t tasklock_cnt;
1160 struct rk29camera_platform_data *sensor_io_request;
1161 struct rk29camera_gpio_res *sensor_gpio_res;
1164 static struct sensor* to_sensor(const struct i2c_client *client)
1166 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1169 static int sensor_task_lock(struct i2c_client *client, int lock)
1171 #if CONFIG_SENSOR_I2C_NOSCHED
1173 struct sensor *sensor = to_sensor(client);
1176 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1177 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1178 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1182 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1183 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1184 goto sensor_task_lock_err;
1189 atomic_add(1, &sensor->tasklock_cnt);
1191 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1192 atomic_sub(1, &sensor->tasklock_cnt);
1194 if (atomic_read(&sensor->tasklock_cnt) == 0)
1199 sensor_task_lock_err:
1207 /* sensor register write */
1208 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1212 struct i2c_msg msg[1];
1214 buf[0] = reg & 0xFF;
1217 msg->addr = client->addr;
1218 msg->flags = client->flags;
1220 msg->len = sizeof(buf);
1221 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1222 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1227 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1228 err = i2c_transfer(client->adapter, msg, 1);
1233 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1241 /* sensor register read */
1242 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1246 struct i2c_msg msg[2];
1250 msg[0].addr = client->addr;
1251 msg[0].flags = client->flags;
1253 msg[0].len = sizeof(buf);
1254 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1255 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1257 msg[1].addr = client->addr;
1258 msg[1].flags = client->flags|I2C_M_RD;
1261 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1262 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1266 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1267 err = i2c_transfer(client->adapter, msg, 2);
1273 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1281 /* write a array of registers */
1282 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1286 #if CONFIG_SENSOR_I2C_RDWRCHK
1291 if (sensor_task_lock(client, 1) < 0)
1292 goto sensor_write_array_end;
1294 while (regarray[i].reg != 0)
1296 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1300 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1304 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1306 goto sensor_write_array_end;
1309 #if CONFIG_SENSOR_I2C_RDWRCHK
1310 sensor_read(client, regarray[i].reg, &valchk);
1311 if (valchk != regarray[i].val)
1312 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1318 sensor_write_array_end:
1319 sensor_task_lock(client,0);
1322 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1330 while (regarray[i].reg != 0)
1332 sensor_read(client, regarray[i].reg, &valchk);
1333 if (valchk != regarray[i].val)
1334 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1340 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1342 struct soc_camera_link *icl = to_soc_camera_link(icd);
1345 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1348 case Sensor_PowerDown:
1350 if (icl->powerdown) {
1353 ret = icl->powerdown(icd->pdev, on);
1354 if (ret == RK29_CAM_IO_SUCCESS) {
1358 icl->reset(icd->pdev);
1360 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1362 goto sensor_power_end;
1369 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1370 struct sensor *sensor = to_sensor(client);
1372 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1373 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1379 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1386 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1388 struct i2c_client *client = v4l2_get_subdevdata(sd);
1389 struct soc_camera_device *icd = client->dev.platform_data;
1390 struct sensor *sensor = to_sensor(client);
1391 const struct v4l2_queryctrl *qctrl;
1392 const struct sensor_datafmt *fmt;
1395 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1397 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1399 goto sensor_INIT_ERR;
1403 if (sensor_task_lock(client,1)<0)
1404 goto sensor_INIT_ERR;
1405 ret = sensor_write(client, 0x12, 0x80);
1408 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1410 goto sensor_INIT_ERR;
1413 mdelay(5); //delay 5 microseconds
1415 ret = sensor_write_array(client, sensor_init_data);
1418 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1419 goto sensor_INIT_ERR;
1421 sensor_task_lock(client,0);
1422 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1423 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1425 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1427 goto sensor_INIT_ERR;
1429 sensor->info_priv.fmt = *fmt;
1431 /* sensor sensor information for initialization */
1432 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1434 sensor->info_priv.whiteBalance = qctrl->default_value;
1435 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1437 sensor->info_priv.brightness = qctrl->default_value;
1438 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1440 sensor->info_priv.effect = qctrl->default_value;
1441 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1443 sensor->info_priv.exposure = qctrl->default_value;
1445 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1447 sensor->info_priv.saturation = qctrl->default_value;
1448 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1450 sensor->info_priv.contrast = qctrl->default_value;
1451 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1453 sensor->info_priv.mirror = qctrl->default_value;
1454 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1456 sensor->info_priv.flip = qctrl->default_value;
1457 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1459 sensor->info_priv.scene = qctrl->default_value;
1460 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1462 sensor->info_priv.digitalzoom = qctrl->default_value;
1464 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1465 #if CONFIG_SENSOR_Focus
1467 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1469 sensor->info_priv.focus = qctrl->default_value;
1472 #if CONFIG_SENSOR_Flash
1473 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1475 sensor->info_priv.flash = qctrl->default_value;
1478 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);
1482 sensor_task_lock(client,0);
1483 sensor_deactivate(client);
1487 static int sensor_deactivate(struct i2c_client *client)
1489 struct soc_camera_device *icd = client->dev.platform_data;
1492 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1494 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1495 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1497 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1498 icd->user_width = SENSOR_INIT_WIDTH;
1499 icd->user_height = SENSOR_INIT_HEIGHT;
1503 static struct reginfo sensor_power_down_sequence[]=
1507 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1510 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1512 if (pm_msg.event == PM_EVENT_SUSPEND) {
1513 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1514 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1516 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1519 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1521 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1526 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1532 static int sensor_resume(struct soc_camera_device *icd)
1536 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1538 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1542 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1548 static int sensor_set_bus_param(struct soc_camera_device *icd,
1549 unsigned long flags)
1555 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1557 struct soc_camera_link *icl = to_soc_camera_link(icd);
1558 unsigned long flags = SENSOR_BUS_PARAM;
1560 return soc_camera_apply_sensor_flags(icl, flags);
1563 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1565 struct i2c_client *client = v4l2_get_subdevdata(sd);
1566 struct soc_camera_device *icd = client->dev.platform_data;
1567 struct sensor *sensor = to_sensor(client);
1569 mf->width = icd->user_width;
1570 mf->height = icd->user_height;
1571 mf->code = sensor->info_priv.fmt.code;
1572 mf->colorspace = sensor->info_priv.fmt.colorspace;
1573 mf->field = V4L2_FIELD_NONE;
1577 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1581 if ((mf->width == 1024) && (mf->height == 768)) {
1583 } else if ((mf->width == 1280) && (mf->height == 1024)) {
1585 } else if ((mf->width == 1600) && (mf->height == 1200)) {
1587 } else if ((mf->width == 2048) && (mf->height == 1536)) {
1589 } else if ((mf->width == 2592) && (mf->height == 1944)) {
1594 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1598 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1602 if ((mf->width == 1280) && (mf->height == 720)) {
1604 } else if ((mf->width == 1920) && (mf->height == 1080)) {
1609 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1612 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1614 struct i2c_client *client = v4l2_get_subdevdata(sd);
1615 struct sensor *sensor = to_sensor(client);
1616 const struct sensor_datafmt *fmt;
1617 const struct v4l2_queryctrl *qctrl;
1618 struct soc_camera_device *icd = client->dev.platform_data;
1619 struct reginfo *winseqe_set_addr=NULL;
1621 int ret=0, set_w,set_h;
1623 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1624 ARRAY_SIZE(sensor_colour_fmts));
1627 goto sensor_s_fmt_end;
1630 if (sensor->info_priv.fmt.code != mf->code) {
1633 case V4L2_MBUS_FMT_YUYV8_2X8:
1635 //winseqe_set_addr = sensor_ClrFmt_YUYV;
1636 sensor_read(client, 0x3a, &readval);
1637 sensor_write(client,0x3a, readval&0xf7);
1638 sensor_read(client,0x3d,&readval);
1639 sensor_write(client,0x3d,readval&0xfe);
1642 case V4L2_MBUS_FMT_UYVY8_2X8:
1644 //winseqe_set_addr = sensor_ClrFmt_UYVY;
1645 sensor_read(client, 0x3a, &readval);
1646 sensor_write(client,0x3a, readval|0x08);
1647 sensor_read(client,0x3d,&readval);
1648 sensor_write(client,0x3d,readval&0xfe);
1654 if (winseqe_set_addr != NULL) {
1655 sensor_write_array(client, winseqe_set_addr);
1656 sensor->info_priv.fmt.code = mf->code;
1657 sensor->info_priv.fmt.colorspace= mf->colorspace;
1658 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1660 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1667 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1669 winseqe_set_addr = sensor_qcif;
1673 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1675 winseqe_set_addr = sensor_qvga;
1679 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1681 winseqe_set_addr = sensor_cif;
1685 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1687 winseqe_set_addr = sensor_vga;
1691 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1693 winseqe_set_addr = sensor_svga;
1697 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1699 winseqe_set_addr = sensor_vga; //sensor_sxga;
1705 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1706 set_w = SENSOR_INIT_WIDTH;
1707 set_h = SENSOR_INIT_HEIGHT;
1708 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1711 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1712 #if CONFIG_SENSOR_Flash
1713 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
1714 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1715 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1716 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1718 } else { /* ddl@rock-chips.com : Video */
1719 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1720 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1721 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1725 ret |= sensor_write_array(client, winseqe_set_addr);
1727 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1728 #if CONFIG_SENSOR_Flash
1729 if (sensor_fmt_capturechk(sd,mf) == true) {
1730 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1731 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1732 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1736 goto sensor_s_fmt_end;
1739 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1741 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
1742 #if CONFIG_SENSOR_Effect
1743 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1744 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1746 #if CONFIG_SENSOR_WhiteBalance
1747 if (sensor->info_priv.whiteBalance != 0) {
1748 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1749 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1752 sensor->info_priv.snap2preview = true;
1753 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
1754 #if CONFIG_SENSOR_Effect
1755 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1756 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1758 #if CONFIG_SENSOR_WhiteBalance
1759 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1760 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1762 sensor->info_priv.video2preview = true;
1763 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1764 #if CONFIG_SENSOR_Effect
1765 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1766 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1768 #if CONFIG_SENSOR_WhiteBalance
1769 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1770 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1772 sensor->info_priv.video2preview = false;
1773 sensor->info_priv.snap2preview = false;
1776 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1780 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1790 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1792 struct i2c_client *client = v4l2_get_subdevdata(sd);
1793 struct sensor *sensor = to_sensor(client);
1794 const struct sensor_datafmt *fmt;
1797 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1798 ARRAY_SIZE(sensor_colour_fmts));
1800 fmt = &sensor->info_priv.fmt;
1801 mf->code = fmt->code;
1804 if (mf->height > SENSOR_MAX_HEIGHT)
1805 mf->height = SENSOR_MAX_HEIGHT;
1806 else if (mf->height < SENSOR_MIN_HEIGHT)
1807 mf->height = SENSOR_MIN_HEIGHT;
1809 if (mf->width > SENSOR_MAX_WIDTH)
1810 mf->width = SENSOR_MAX_WIDTH;
1811 else if (mf->width < SENSOR_MIN_WIDTH)
1812 mf->width = SENSOR_MIN_WIDTH;
1814 mf->colorspace = fmt->colorspace;
1819 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1821 struct i2c_client *client = v4l2_get_subdevdata(sd);
1823 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1826 if (id->match.addr != client->addr)
1829 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV9650 identifier */
1834 #if CONFIG_SENSOR_Brightness
1835 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1837 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1839 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1841 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1843 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1845 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1848 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1852 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1856 #if CONFIG_SENSOR_Effect
1857 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1859 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1861 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1863 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1865 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1867 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1870 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1874 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1878 #if CONFIG_SENSOR_Exposure
1879 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1881 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1883 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1885 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1887 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1889 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1892 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1896 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1900 #if CONFIG_SENSOR_Saturation
1901 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1903 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1905 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1907 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1909 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1911 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1914 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1918 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1922 #if CONFIG_SENSOR_Contrast
1923 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1925 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1927 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1929 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1931 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1933 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1936 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1940 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1944 #if CONFIG_SENSOR_Mirror
1945 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1947 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1949 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1951 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1953 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1955 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1958 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1962 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1966 #if CONFIG_SENSOR_Flip
1967 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1969 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1971 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1973 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1975 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1977 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1980 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1984 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1988 #if CONFIG_SENSOR_Scene
1989 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1991 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1993 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1995 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1997 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1999 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2002 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2006 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2010 #if CONFIG_SENSOR_WhiteBalance
2011 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2013 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2015 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2017 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2019 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2021 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2024 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2028 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2032 #if CONFIG_SENSOR_DigitalZoom
2033 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2035 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2036 struct sensor *sensor = to_sensor(client);
2037 const struct v4l2_queryctrl *qctrl_info;
2038 int digitalzoom_cur, digitalzoom_total;
2040 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2044 digitalzoom_cur = sensor->info_priv.digitalzoom;
2045 digitalzoom_total = qctrl_info->maximum;
2047 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2049 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2053 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2055 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2059 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2061 *value = digitalzoom_total - digitalzoom_cur;
2064 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2066 *value = 0 - digitalzoom_cur;
2069 digitalzoom_cur += *value;
2071 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2073 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2075 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2078 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2085 #if CONFIG_SENSOR_Flash
2086 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2088 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2089 if (value == 3) { /* ddl@rock-chips.com: torch */
2090 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2092 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2094 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2098 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2103 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2105 struct i2c_client *client = v4l2_get_subdevdata(sd);
2106 struct sensor *sensor = to_sensor(client);
2107 const struct v4l2_queryctrl *qctrl;
2109 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2113 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2119 case V4L2_CID_BRIGHTNESS:
2121 ctrl->value = sensor->info_priv.brightness;
2124 case V4L2_CID_SATURATION:
2126 ctrl->value = sensor->info_priv.saturation;
2129 case V4L2_CID_CONTRAST:
2131 ctrl->value = sensor->info_priv.contrast;
2134 case V4L2_CID_DO_WHITE_BALANCE:
2136 ctrl->value = sensor->info_priv.whiteBalance;
2139 case V4L2_CID_EXPOSURE:
2141 ctrl->value = sensor->info_priv.exposure;
2144 case V4L2_CID_HFLIP:
2146 ctrl->value = sensor->info_priv.mirror;
2149 case V4L2_CID_VFLIP:
2151 ctrl->value = sensor->info_priv.flip;
2162 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2164 struct i2c_client *client = v4l2_get_subdevdata(sd);
2165 const struct v4l2_queryctrl *qctrl;
2167 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2171 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2177 #if CONFIG_SENSOR_Brightness
2178 case V4L2_CID_BRIGHTNESS:
2180 if (ctrl->value != sensor->info_priv.brightness)
2182 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2186 sensor->info_priv.brightness = ctrl->value;
2191 #if CONFIG_SENSOR_Exposure
2192 case V4L2_CID_EXPOSURE:
2194 if (ctrl->value != sensor->info_priv.exposure)
2196 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2200 sensor->info_priv.exposure = ctrl->value;
2205 #if CONFIG_SENSOR_Saturation
2206 case V4L2_CID_SATURATION:
2208 if (ctrl->value != sensor->info_priv.saturation)
2210 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2214 sensor->info_priv.saturation = ctrl->value;
2219 #if CONFIG_SENSOR_Contrast
2220 case V4L2_CID_CONTRAST:
2222 if (ctrl->value != sensor->info_priv.contrast)
2224 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2228 sensor->info_priv.contrast = ctrl->value;
2233 #if CONFIG_SENSOR_WhiteBalance
2234 case V4L2_CID_DO_WHITE_BALANCE:
2236 if (ctrl->value != sensor->info_priv.whiteBalance)
2238 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2242 sensor->info_priv.whiteBalance = ctrl->value;
2247 #if CONFIG_SENSOR_Mirror
2248 case V4L2_CID_HFLIP:
2250 if (ctrl->value != sensor->info_priv.mirror)
2252 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2254 sensor->info_priv.mirror = ctrl->value;
2259 #if CONFIG_SENSOR_Flip
2260 case V4L2_CID_VFLIP:
2262 if (ctrl->value != sensor->info_priv.flip)
2264 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2266 sensor->info_priv.flip = ctrl->value;
2277 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2279 const struct v4l2_queryctrl *qctrl;
2280 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2281 struct sensor *sensor = to_sensor(client);
2283 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2287 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2291 switch (ext_ctrl->id)
2293 case V4L2_CID_SCENE:
2295 ext_ctrl->value = sensor->info_priv.scene;
2298 case V4L2_CID_EFFECT:
2300 ext_ctrl->value = sensor->info_priv.effect;
2303 case V4L2_CID_ZOOM_ABSOLUTE:
2305 ext_ctrl->value = sensor->info_priv.digitalzoom;
2308 case V4L2_CID_ZOOM_RELATIVE:
2312 case V4L2_CID_FOCUS_ABSOLUTE:
2314 ext_ctrl->value = sensor->info_priv.focus;
2317 case V4L2_CID_FOCUS_RELATIVE:
2321 case V4L2_CID_FLASH:
2323 ext_ctrl->value = sensor->info_priv.flash;
2331 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2333 const struct v4l2_queryctrl *qctrl;
2334 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2335 struct sensor *sensor = to_sensor(client);
2338 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2342 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2347 switch (ext_ctrl->id)
2349 #if CONFIG_SENSOR_Scene
2350 case V4L2_CID_SCENE:
2352 if (ext_ctrl->value != sensor->info_priv.scene)
2354 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2356 sensor->info_priv.scene = ext_ctrl->value;
2361 #if CONFIG_SENSOR_Effect
2362 case V4L2_CID_EFFECT:
2364 if (ext_ctrl->value != sensor->info_priv.effect)
2366 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2368 sensor->info_priv.effect= ext_ctrl->value;
2373 #if CONFIG_SENSOR_DigitalZoom
2374 case V4L2_CID_ZOOM_ABSOLUTE:
2376 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2379 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2381 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2383 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2385 sensor->info_priv.digitalzoom += val_offset;
2387 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2392 case V4L2_CID_ZOOM_RELATIVE:
2394 if (ext_ctrl->value)
2396 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2398 sensor->info_priv.digitalzoom += ext_ctrl->value;
2400 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2405 #if CONFIG_SENSOR_Focus
2406 case V4L2_CID_FOCUS_ABSOLUTE:
2408 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2411 if (ext_ctrl->value != sensor->info_priv.focus)
2413 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2415 sensor->info_priv.focus += val_offset;
2420 case V4L2_CID_FOCUS_RELATIVE:
2422 if (ext_ctrl->value)
2424 sensor->info_priv.focus += ext_ctrl->value;
2426 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2431 #if CONFIG_SENSOR_Flash
2432 case V4L2_CID_FLASH:
2434 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2436 sensor->info_priv.flash = ext_ctrl->value;
2438 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2449 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2451 struct i2c_client *client = v4l2_get_subdevdata(sd);
2452 struct soc_camera_device *icd = client->dev.platform_data;
2453 int i, error_cnt=0, error_idx=-1;
2456 for (i=0; i<ext_ctrl->count; i++) {
2457 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2464 error_idx = ext_ctrl->count;
2466 if (error_idx != -1) {
2467 ext_ctrl->error_idx = error_idx;
2474 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2476 struct i2c_client *client = v4l2_get_subdevdata(sd);
2477 struct soc_camera_device *icd = client->dev.platform_data;
2478 int i, error_cnt=0, error_idx=-1;
2481 for (i=0; i<ext_ctrl->count; i++) {
2482 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2489 error_idx = ext_ctrl->count;
2491 if (error_idx != -1) {
2492 ext_ctrl->error_idx = error_idx;
2499 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2500 * this wasn't our capture interface, so, we wait for the right one */
2501 static int sensor_video_probe(struct soc_camera_device *icd,
2502 struct i2c_client *client)
2506 struct sensor *sensor = to_sensor(client);
2508 /* We must have a parent by now. And it cannot be a wrong one.
2509 * So this entire test is completely redundant. */
2510 if (!icd->dev.parent ||
2511 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2514 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2516 goto sensor_video_probe_err;
2519 ret = sensor_write(client, 0x12, 0x80);
2522 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2525 mdelay(50); //delay 5 microseconds
2527 /* check if it is an sensor sensor */
2528 ret = sensor_read(client, 0x0a, &pid);
2530 SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2532 goto sensor_video_probe_err;
2535 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2536 if (pid == SENSOR_ID) {
2537 sensor->model = SENSOR_V4L2_IDENT;
2539 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2541 goto sensor_video_probe_err;
2546 sensor_video_probe_err:
2551 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2553 struct i2c_client *client = v4l2_get_subdevdata(sd);
2554 struct soc_camera_device *icd = client->dev.platform_data;
2555 struct sensor *sensor = to_sensor(client);
2557 #if CONFIG_SENSOR_Flash
2561 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2564 case RK29_CAM_SUBDEV_DEACTIVATE:
2566 sensor_deactivate(client);
2570 case RK29_CAM_SUBDEV_IOREQUEST:
2572 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2573 if (sensor->sensor_io_request != NULL) {
2574 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2575 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2576 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2577 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2578 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2579 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2582 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2584 goto sensor_ioctl_end;
2586 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2588 #if CONFIG_SENSOR_Flash
2589 if (sensor->sensor_gpio_res) {
2590 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2591 for (i = 0; i < icd->ops->num_controls; i++) {
2592 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2593 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2596 sensor->info_priv.flash = 0xff;
2597 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2605 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2613 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2614 enum v4l2_mbus_pixelcode *code)
2616 if (index >= ARRAY_SIZE(sensor_colour_fmts))
2619 *code = sensor_colour_fmts[index].code;
2622 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2623 .init = sensor_init,
2624 .g_ctrl = sensor_g_control,
2625 .s_ctrl = sensor_s_control,
2626 .g_ext_ctrls = sensor_g_ext_controls,
2627 .s_ext_ctrls = sensor_s_ext_controls,
2628 .g_chip_ident = sensor_g_chip_ident,
2629 .ioctl = sensor_ioctl,
2632 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2633 .s_mbus_fmt = sensor_s_fmt,
2634 .g_mbus_fmt = sensor_g_fmt,
2635 .try_mbus_fmt = sensor_try_fmt,
2636 .enum_mbus_fmt = sensor_enum_fmt,
2639 static struct v4l2_subdev_ops sensor_subdev_ops = {
2640 .core = &sensor_subdev_core_ops,
2641 .video = &sensor_subdev_video_ops,
2644 static int sensor_probe(struct i2c_client *client,
2645 const struct i2c_device_id *did)
2647 struct sensor *sensor;
2648 struct soc_camera_device *icd = client->dev.platform_data;
2649 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2650 struct soc_camera_link *icl;
2653 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2655 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2659 icl = to_soc_camera_link(icd);
2661 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2665 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2666 dev_warn(&adapter->dev,
2667 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2671 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2675 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2677 /* Second stage probe - when a capture adapter is there */
2678 icd->ops = &sensor_ops;
2679 sensor->info_priv.fmt = sensor_colour_fmts[0];
2680 #if CONFIG_SENSOR_I2C_NOSCHED
2681 atomic_set(&sensor->tasklock_cnt,0);
2684 ret = sensor_video_probe(icd, client);
2687 i2c_set_clientdata(client, NULL);
2691 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2695 static int sensor_remove(struct i2c_client *client)
2697 struct sensor *sensor = to_sensor(client);
2698 struct soc_camera_device *icd = client->dev.platform_data;
2701 i2c_set_clientdata(client, NULL);
2702 client->driver = NULL;
2708 static const struct i2c_device_id sensor_id[] = {
2709 {SENSOR_NAME_STRING(), 0 },
2712 MODULE_DEVICE_TABLE(i2c, sensor_id);
2714 static struct i2c_driver sensor_i2c_driver = {
2716 .name = SENSOR_NAME_STRING(),
2718 .probe = sensor_probe,
2719 .remove = sensor_remove,
2720 .id_table = sensor_id,
2723 static int __init sensor_mod_init(void)
2725 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2726 return i2c_add_driver(&sensor_i2c_driver);
2729 static void __exit sensor_mod_exit(void)
2731 i2c_del_driver(&sensor_i2c_driver);
2734 device_initcall_sync(sensor_mod_init);
2735 module_exit(sensor_mod_exit);
2737 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2738 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2739 MODULE_LICENSE("GPL");