2 o* Driver for MT9M001 CMOS Image Sensor from Micron
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/miscdevice.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/soc_camera.h>
22 #include <mach/rk29_camera.h>
25 module_param(debug, int, S_IRUGO|S_IWUSR);
27 #define dprintk(level, fmt, arg...) do { \
29 printk(KERN_WARNING fmt , ## arg); } while (0)
31 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
32 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
42 #define MIN(x,y) ((x<y) ? x: y)
43 #define MAX(x,y) ((x>y) ? x: y)
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK29_CAM_SENSOR_NT99250
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_NT99250
48 #define SENSOR_ID 0x0105
49 #define SENSOR_MIN_WIDTH 176
50 #define SENSOR_MIN_HEIGHT 144
51 #define SENSOR_MAX_WIDTH 1600
52 #define SENSOR_MAX_HEIGHT 1200
53 #define SENSOR_INIT_WIDTH 800 /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT 600
55 #define SENSOR_INIT_WINSEQADR sensor_svga
56 #define SENSOR_INIT_PIXFMT V4L2_PIX_FMT_UYVY
58 #define CONFIG_SENSOR_WhiteBalance 0
59 #define CONFIG_SENSOR_Brightness 0
60 #define CONFIG_SENSOR_Contrast 0
61 #define CONFIG_SENSOR_Saturation 0
62 #define CONFIG_SENSOR_Effect 0
63 #define CONFIG_SENSOR_Scene 0
64 #define CONFIG_SENSOR_DigitalZoom 0
65 #define CONFIG_SENSOR_Focus 0
66 #define CONFIG_SENSOR_Exposure 0
67 #define CONFIG_SENSOR_Flash 0
68 #define CONFIG_SENSOR_Mirror 0
69 #define CONFIG_SENSOR_Flip 0
71 #define CONFIG_SENSOR_I2C_SPEED 250000 /* Hz */
72 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
73 #define CONFIG_SENSOR_I2C_NOSCHED 0
74 #define CONFIG_SENSOR_I2C_RDWRCHK 0
76 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
77 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
78 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
80 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
81 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
82 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
83 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
84 #define COLOR_TEMPERATURE_OFFICE_DN 3500
85 #define COLOR_TEMPERATURE_OFFICE_UP 5000
86 #define COLOR_TEMPERATURE_HOME_DN 2500
87 #define COLOR_TEMPERATURE_HOME_UP 3500
89 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
90 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
98 /* init 352X288 SVGA */
99 static struct reginfo sensor_init_data[] =
101 {0x3024,0x02}, //TG //0x02
102 {0x32F0,0x00},//0:UYVY 2:VYUY 1:YUYV 3:YVYU
224 {0x32f6,0x0c},//effect function
225 {0x3069, 0x00}, //Pix //01 :for M1002 00 :for other
226 {0x306d, 0x01}, //pclk //00 :for M1002 01 :for other
228 //==============================
230 //==============================
282 {0x32c2,0x5c}, //7.14fps @ 48M
286 {0x3290,0x01}, //awb init ++++
296 {0x32a8,0xa0}, //awb init ----
297 {0x3012,0x02}, //AE init +++
300 {0x3201,0x7f}, //AE init ---
303 {0x0, 0x0}, //end flag
308 static struct reginfo sensor_uxga[] =
310 //Output format & size
314 {0x3332, 0x80}, //0x20
315 {0x3320, 0x20}, //0x28
366 static struct reginfo sensor_sxga[] =
414 static struct reginfo sensor_svga[] =
444 {0x301f,0x20}, //pll 0x20 48Mhz 0x24 32Mhz
466 {0x32c2,0x5c}, //7.14fps @ 48M
468 //{0x32c2,0xd4}, //10fps @ 48M
472 //{0x3290,0x01}, //awb init ++++
482 {0x32a8,0xa0}, //awb init ----
483 //{0x3012,0x02}, //AE init +++
486 //{0x3201,0x7f}, //AE init ---
494 static struct reginfo sensor_vga[] =
525 {0x301f,0x20}, //pll 0x20 48Mhz 0x24 32Mhz
547 //{0x32c2,0x5c}, //7.14fps @ 48M
549 {0x32c2,0xd4}, //10fps @ 48M
553 //{0x3290,0x01}, //awb init ++++
563 {0x32a8,0xa0}, //awb init ----
564 //{0x3012,0x02}, //AE init +++
567 //{0x3201,0x7f}, //AE init ---
574 static struct reginfo sensor_cif[] =
580 static struct reginfo sensor_qvga[] =
586 static struct reginfo sensor_qcif[] =
592 static struct reginfo sensor_ClrFmt_YUYV[]=
594 {0x32f0, 0x00}, //0:UYVY 2:VYUY 1:YUYV 3:YVYU
598 static struct reginfo sensor_ClrFmt_UYVY[]=
600 {0x32F0, 0x00}, //0:UYVY 2:VYUY 1:YUYV 3:YVYU
604 #if CONFIG_SENSOR_WhiteBalance
605 static struct reginfo sensor_WhiteB_Auto[]=
607 {0x3201, 0x3f}, //AWB auto, bit[1]:0,auto
610 /* Cloudy Colour Temperature : 6500K - 8000K */
611 static struct reginfo sensor_WhiteB_Cloudy[]=
620 /* ClearDay Colour Temperature : 5000K - 6500K */
621 static struct reginfo sensor_WhiteB_ClearDay[]=
632 /* Office Colour Temperature : 3500K - 5000K */
633 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
644 /* Home Colour Temperature : 2500K - 3500K */
645 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
655 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
656 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
660 #if CONFIG_SENSOR_Brightness
661 static struct reginfo sensor_Brightness0[]=
669 static struct reginfo sensor_Brightness1[]=
677 static struct reginfo sensor_Brightness2[]=
685 static struct reginfo sensor_Brightness3[]=
693 static struct reginfo sensor_Brightness4[]=
701 static struct reginfo sensor_Brightness5[]=
708 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
709 sensor_Brightness4, sensor_Brightness5,NULL,
714 #if CONFIG_SENSOR_Effect
715 static struct reginfo sensor_Effect_Normal[] =
721 static struct reginfo sensor_Effect_WandB[] =
727 static struct reginfo sensor_Effect_Sepia[] =
734 static struct reginfo sensor_Effect_Negative[] =
741 static struct reginfo sensor_Effect_Bluish[] =
751 static struct reginfo sensor_Effect_Green[] =
760 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
761 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
764 #if CONFIG_SENSOR_Exposure
765 static struct reginfo sensor_Exposure0[]=
771 static struct reginfo sensor_Exposure1[]=
777 static struct reginfo sensor_Exposure2[]=
783 static struct reginfo sensor_Exposure3[]=
789 static struct reginfo sensor_Exposure4[]=
795 static struct reginfo sensor_Exposure5[]=
801 static struct reginfo sensor_Exposure6[]=
807 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
808 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
811 #if CONFIG_SENSOR_Saturation
812 static struct reginfo sensor_Saturation0[]=
817 static struct reginfo sensor_Saturation1[]=
822 static struct reginfo sensor_Saturation2[]=
826 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
829 #if CONFIG_SENSOR_Contrast
830 static struct reginfo sensor_Contrast0[]=
836 static struct reginfo sensor_Contrast1[]=
842 static struct reginfo sensor_Contrast2[]=
848 static struct reginfo sensor_Contrast3[]=
854 static struct reginfo sensor_Contrast4[]=
861 static struct reginfo sensor_Contrast5[]=
867 static struct reginfo sensor_Contrast6[]=
872 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
873 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
877 #if CONFIG_SENSOR_Mirror
878 static struct reginfo sensor_MirrorOn[]=
883 static struct reginfo sensor_MirrorOff[]=
887 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
889 #if CONFIG_SENSOR_Flip
890 static struct reginfo sensor_FlipOn[]=
895 static struct reginfo sensor_FlipOff[]=
899 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
902 #if CONFIG_SENSOR_Scene
903 static struct reginfo sensor_SceneAuto[] =
909 static struct reginfo sensor_SceneNight[] =
911 //30fps ~ 5fps night mode for 60/50Hz light environment, 24Mhz clock input,36Mzh pclk
915 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
918 #if CONFIG_SENSOR_DigitalZoom
919 static struct reginfo sensor_Zoom0[] =
924 static struct reginfo sensor_Zoom1[] =
929 static struct reginfo sensor_Zoom2[] =
935 static struct reginfo sensor_Zoom3[] =
939 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
941 static const struct v4l2_querymenu sensor_menus[] =
943 #if CONFIG_SENSOR_WhiteBalance
944 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
945 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
946 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
949 #if CONFIG_SENSOR_Effect
950 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
951 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
952 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
955 #if CONFIG_SENSOR_Scene
956 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
959 #if CONFIG_SENSOR_Flash
960 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
961 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
965 static const struct v4l2_queryctrl sensor_controls[] =
967 #if CONFIG_SENSOR_WhiteBalance
969 .id = V4L2_CID_DO_WHITE_BALANCE,
970 .type = V4L2_CTRL_TYPE_MENU,
971 .name = "White Balance Control",
979 #if CONFIG_SENSOR_Brightness
981 .id = V4L2_CID_BRIGHTNESS,
982 .type = V4L2_CTRL_TYPE_INTEGER,
983 .name = "Brightness Control",
991 #if CONFIG_SENSOR_Effect
993 .id = V4L2_CID_EFFECT,
994 .type = V4L2_CTRL_TYPE_MENU,
995 .name = "Effect Control",
1003 #if CONFIG_SENSOR_Exposure
1005 .id = V4L2_CID_EXPOSURE,
1006 .type = V4L2_CTRL_TYPE_INTEGER,
1007 .name = "Exposure Control",
1015 #if CONFIG_SENSOR_Saturation
1017 .id = V4L2_CID_SATURATION,
1018 .type = V4L2_CTRL_TYPE_INTEGER,
1019 .name = "Saturation Control",
1027 #if CONFIG_SENSOR_Contrast
1029 .id = V4L2_CID_CONTRAST,
1030 .type = V4L2_CTRL_TYPE_INTEGER,
1031 .name = "Contrast Control",
1039 #if CONFIG_SENSOR_Mirror
1041 .id = V4L2_CID_HFLIP,
1042 .type = V4L2_CTRL_TYPE_BOOLEAN,
1043 .name = "Mirror Control",
1051 #if CONFIG_SENSOR_Flip
1053 .id = V4L2_CID_VFLIP,
1054 .type = V4L2_CTRL_TYPE_BOOLEAN,
1055 .name = "Flip Control",
1063 #if CONFIG_SENSOR_Scene
1065 .id = V4L2_CID_SCENE,
1066 .type = V4L2_CTRL_TYPE_MENU,
1067 .name = "Scene Control",
1075 #if CONFIG_SENSOR_DigitalZoom
1077 .id = V4L2_CID_ZOOM_RELATIVE,
1078 .type = V4L2_CTRL_TYPE_INTEGER,
1079 .name = "DigitalZoom Control",
1085 .id = V4L2_CID_ZOOM_ABSOLUTE,
1086 .type = V4L2_CTRL_TYPE_INTEGER,
1087 .name = "DigitalZoom Control",
1095 #if CONFIG_SENSOR_Focus
1097 .id = V4L2_CID_FOCUS_RELATIVE,
1098 .type = V4L2_CTRL_TYPE_INTEGER,
1099 .name = "Focus Control",
1105 .id = V4L2_CID_FOCUS_ABSOLUTE,
1106 .type = V4L2_CTRL_TYPE_INTEGER,
1107 .name = "Focus Control",
1111 .default_value = 125,
1115 #if CONFIG_SENSOR_Flash
1117 .id = V4L2_CID_FLASH,
1118 .type = V4L2_CTRL_TYPE_MENU,
1119 .name = "Flash Control",
1128 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1129 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1130 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1131 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1132 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1133 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1134 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1135 static int sensor_resume(struct soc_camera_device *icd);
1136 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1137 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1138 #if CONFIG_SENSOR_Effect
1139 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1141 #if CONFIG_SENSOR_WhiteBalance
1142 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1144 static int sensor_deactivate(struct i2c_client *client);
1146 static struct soc_camera_ops sensor_ops =
1148 .suspend = sensor_suspend,
1149 .resume = sensor_resume,
1150 .set_bus_param = sensor_set_bus_param,
1151 .query_bus_param = sensor_query_bus_param,
1152 .controls = sensor_controls,
1153 .menus = sensor_menus,
1154 .num_controls = ARRAY_SIZE(sensor_controls),
1155 .num_menus = ARRAY_SIZE(sensor_menus),
1158 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1159 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1160 .colorspace = _colorspace }
1162 #define JPG_FMT(_name, _depth, _fourcc) \
1163 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1165 static const struct soc_camera_data_format sensor_colour_formats[] = {
1166 JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1167 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1170 typedef struct sensor_info_priv_s
1184 unsigned char mirror; /* HFLIP */
1185 unsigned char flip; /* VFLIP */
1186 unsigned int winseqe_cur_addr;
1187 unsigned int pixfmt;
1189 } sensor_info_priv_t;
1193 struct v4l2_subdev subdev;
1194 struct i2c_client *client;
1195 sensor_info_priv_t info_priv;
1196 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1197 #if CONFIG_SENSOR_I2C_NOSCHED
1198 atomic_t tasklock_cnt;
1200 struct rk29camera_platform_data *sensor_io_request;
1201 struct rk29camera_gpio_res *sensor_gpio_res;
1205 static struct sensor* to_sensor(const struct i2c_client *client)
1207 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1210 static int sensor_task_lock(struct i2c_client *client, int lock)
1212 #if CONFIG_SENSOR_I2C_NOSCHED
1214 struct sensor *sensor = to_sensor(client);
1217 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1218 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1219 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1223 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1224 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1225 goto sensor_task_lock_err;
1230 atomic_add(1, &sensor->tasklock_cnt);
1232 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1233 atomic_sub(1, &sensor->tasklock_cnt);
1235 if (atomic_read(&sensor->tasklock_cnt) == 0)
1240 sensor_task_lock_err:
1248 /* sensor register write */
1249 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1253 struct i2c_msg msg[1];
1256 buf[1] = reg & 0xFF;
1259 msg->addr = client->addr;
1260 msg->flags = client->flags;
1262 msg->len = sizeof(buf);
1263 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1264 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1269 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1270 err = i2c_transfer(client->adapter, msg, 1);
1275 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1283 /* sensor register read */
1284 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1288 struct i2c_msg msg[2];
1291 buf[1] = reg & 0xFF;
1293 msg[0].addr = client->addr;
1294 msg[0].flags = client->flags;
1296 msg[0].len = sizeof(buf);
1297 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1298 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1300 msg[1].addr = client->addr;
1301 msg[1].flags = client->flags|I2C_M_RD;
1304 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1305 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1309 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1310 err = i2c_transfer(client->adapter, msg, 2);
1316 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1324 /* write a array of registers */
1325 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1329 #if CONFIG_SENSOR_I2C_RDWRCHK
1334 if (sensor_task_lock(client, 1) < 0)
1335 goto sensor_write_array_end;
1337 while (regarray[i].reg != 0)
1339 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1343 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1347 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1349 goto sensor_write_array_end;
1352 #if CONFIG_SENSOR_I2C_RDWRCHK
1353 sensor_read(client, regarray[i].reg, &valchk);
1354 if (valchk != regarray[i].val)
1355 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1361 sensor_write_array_end:
1362 sensor_task_lock(client,0);
1365 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1373 while (regarray[i].reg != 0)
1375 sensor_read(client, regarray[i].reg, &valchk);
1376 if (valchk != regarray[i].val)
1377 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1383 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1385 struct soc_camera_link *icl = to_soc_camera_link(icd);
1388 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1391 case Sensor_PowerDown:
1393 if (icl->powerdown) {
1394 ret = icl->powerdown(icd->pdev, on);
1395 if (ret == RK29_CAM_IO_SUCCESS) {
1399 icl->reset(icd->pdev);
1401 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1403 goto sensor_power_end;
1410 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1411 struct sensor *sensor = to_sensor(client);
1413 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1414 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1420 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1427 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1429 struct i2c_client *client = sd->priv;
1430 struct soc_camera_device *icd = client->dev.platform_data;
1431 struct sensor *sensor = to_sensor(client);
1432 const struct v4l2_queryctrl *qctrl;
1436 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1438 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1440 goto sensor_INIT_ERR;
1444 sensor_task_lock(client,1);
1445 ret = sensor_write(client, 0x3021, 0x61);
1448 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1450 goto sensor_INIT_ERR;
1453 mdelay(5); //delay 5 microseconds
1454 /* check if it is an sensor sensor */
1455 ret = sensor_read(client, 0x307e, &value);
1457 SENSOR_TR("read chip id high byte failed\n");
1459 goto sensor_INIT_ERR;
1462 pid |= (value << 8);
1464 ret = sensor_read(client, 0x307f, &value);
1466 SENSOR_TR("read chip id low byte failed\n");
1468 goto sensor_INIT_ERR;
1471 pid |= (value & 0xff);
1472 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1474 if (pid == SENSOR_ID) {
1475 sensor->model = SENSOR_V4L2_IDENT;
1477 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1479 goto sensor_INIT_ERR;
1483 ret = sensor_write_array(client, sensor_init_data);
1486 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1487 goto sensor_INIT_ERR;
1489 sensor_task_lock(client,0);
1490 //icd->user_width = SENSOR_INIT_WIDTH;
1491 //icd->user_height = SENSOR_INIT_HEIGHT;
1492 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1493 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
1495 /* sensor sensor information for initialization */
1496 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1498 sensor->info_priv.whiteBalance = qctrl->default_value;
1499 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1501 sensor->info_priv.brightness = qctrl->default_value;
1502 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1504 sensor->info_priv.effect = qctrl->default_value;
1505 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1507 sensor->info_priv.exposure = qctrl->default_value;
1509 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1511 sensor->info_priv.saturation = qctrl->default_value;
1512 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1514 sensor->info_priv.contrast = qctrl->default_value;
1515 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1517 sensor->info_priv.mirror = qctrl->default_value;
1518 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1520 sensor->info_priv.flip = qctrl->default_value;
1521 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1523 sensor->info_priv.scene = qctrl->default_value;
1524 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1526 sensor->info_priv.digitalzoom = qctrl->default_value;
1528 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1529 #if CONFIG_SENSOR_Focus
1531 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1533 sensor->info_priv.focus = qctrl->default_value;
1536 #if CONFIG_SENSOR_Flash
1537 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1539 sensor->info_priv.flash = qctrl->default_value;
1542 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);
1546 sensor_task_lock(client,0);
1547 sensor_deactivate(client);
1551 static int sensor_deactivate(struct i2c_client *client)
1553 struct soc_camera_device *icd = client->dev.platform_data;
1556 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1558 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1559 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1561 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1562 icd->user_width = SENSOR_INIT_WIDTH;
1563 icd->user_height = SENSOR_INIT_HEIGHT;
1568 static struct reginfo sensor_power_down_sequence[]=
1576 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1579 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1581 if (pm_msg.event == PM_EVENT_SUSPEND) {
1582 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1583 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1585 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1588 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1590 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1595 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1601 static int sensor_resume(struct soc_camera_device *icd)
1605 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1607 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1611 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1617 static int sensor_set_bus_param(struct soc_camera_device *icd,
1618 unsigned long flags)
1624 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1626 struct soc_camera_link *icl = to_soc_camera_link(icd);
1627 unsigned long flags = SENSOR_BUS_PARAM;
1629 return soc_camera_apply_sensor_flags(icl, flags);
1632 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1634 struct i2c_client *client = sd->priv;
1635 struct soc_camera_device *icd = client->dev.platform_data;
1636 struct sensor *sensor = to_sensor(client);
1637 struct v4l2_pix_format *pix = &f->fmt.pix;
1639 pix->width = icd->user_width;
1640 pix->height = icd->user_height;
1641 pix->pixelformat = sensor->info_priv.pixfmt;
1642 pix->field = V4L2_FIELD_NONE;
1643 pix->colorspace = V4L2_COLORSPACE_JPEG;
1647 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
1651 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
1653 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
1655 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
1657 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
1659 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
1664 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1668 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
1672 if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
1674 } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
1679 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1682 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1684 struct i2c_client *client = sd->priv;
1685 struct sensor *sensor = to_sensor(client);
1686 struct v4l2_pix_format *pix = &f->fmt.pix;
1687 const struct v4l2_queryctrl *qctrl;
1688 struct soc_camera_device *icd = client->dev.platform_data;
1689 struct reginfo *winseqe_set_addr=NULL;
1691 int ret=0, set_w,set_h;
1693 u16 AE_reg, AGC_reg;
1694 u8 temp_reg12,temp_reg13;
1695 u16 shutter,reg_1, reg;
1697 //turn on scaler for preivew
1698 sensor_read(client ,0x3201, ®_1);
1699 sensor_write(client, 0x3201, (reg_1|0x40) );
1701 sensor_read(client ,0x32f1, ®);
1702 sensor_write(client, 0x32f1, (reg|0x10) );
1704 #if 0 //preview_fastmode
1705 // turn off AE for preview
1706 sensor_read(client ,0x3201, &AE_reg);
1707 sensor_write(client, 0x3201, (AE_reg|0x20) );
1708 // turn off AGC for preview
1709 sensor_read(client ,0x32bb, &AGC_reg);
1710 sensor_write(client, 0x32bb, (AGC_reg|0x01) );
1712 sensor_read(client, 0x3012, &temp_reg12);
1713 sensor_read(client, 0x3013, &temp_reg13);
1714 shutter = (temp_reg13 & 0x00FF) | (temp_reg12 << 8);
1716 if (sensor->info_priv.pixfmt != pix->pixelformat) {
1717 switch (pix->pixelformat)
1719 case V4L2_PIX_FMT_YUYV:
1721 winseqe_set_addr = sensor_ClrFmt_YUYV;
1724 case V4L2_PIX_FMT_UYVY:
1726 winseqe_set_addr = sensor_ClrFmt_UYVY;
1732 if (winseqe_set_addr != NULL) {
1733 sensor_write_array(client, winseqe_set_addr);
1734 sensor->info_priv.pixfmt = pix->pixelformat;
1736 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1738 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1743 set_h = pix->height;
1745 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1747 winseqe_set_addr = sensor_qcif;
1751 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1753 winseqe_set_addr = sensor_qvga;
1757 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1759 winseqe_set_addr = sensor_cif;
1763 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1765 winseqe_set_addr = sensor_vga;
1769 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1771 winseqe_set_addr = sensor_svga;
1775 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1777 winseqe_set_addr = sensor_sxga;
1781 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1783 winseqe_set_addr = sensor_uxga;
1789 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1790 set_w = SENSOR_INIT_WIDTH;
1791 set_h = SENSOR_INIT_HEIGHT;
1792 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
1795 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1796 #if CONFIG_SENSOR_Flash
1797 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
1798 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1799 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1800 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1802 } else { /* ddl@rock-chips.com : Video */
1803 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1804 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1805 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1809 ret |= sensor_write_array(client, winseqe_set_addr);
1811 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1812 #if CONFIG_SENSOR_Flash
1813 if (sensor_fmt_capturechk(sd,f) == true) {
1814 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1815 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1816 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1820 goto sensor_s_fmt_end;
1823 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1825 if ((set_w <= 640) && (set_h <= 480))
1832 sensor_read(client ,0x32f1, ®);
1833 sensor_write(client, 0x32f1, (reg&(~0x10)) );
1834 if ((set_w <= 1600) && (set_h <= 1200))
1836 //turn off scaler for UXGA capture
1837 sensor_read(client ,0x3201, ®_1);
1838 sensor_write(client, 0x3201, (reg_1&(~0x40)) );
1840 #if 0 //preview_fastmode
1842 sensor_read(client ,0x3201, &AE_reg);
1843 sensor_write(client, 0x3201, (AE_reg&(~0x20)) );
1845 sensor_read(client ,0x32bb, &AGC_reg);
1846 sensor_write(client, 0x32bb, (AGC_reg&(~0x01)) );
1847 if ((set_w <= 800) && (set_h <= 600))
1849 shutter = shutter*1984/2434;
1851 else if ((set_w <= 2434) && (set_h <= 1024))
1853 shutter = shutter*1984/2434;
1855 else if ((set_w <= 1600) && (set_h <= 1200))
1857 shutter = shutter*1984/2434;
1863 sensor_write(client, 0x3012, sizeof((shutter >> 8) & 0xff) );
1864 sensor_write(client, 0x3013, sizeof(shutter & 0xFF) );
1869 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1873 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1877 pix->height = set_h;
1883 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1885 struct v4l2_pix_format *pix = &f->fmt.pix;
1886 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
1887 pix->pixelformat == V4L2_PIX_FMT_YUYV;
1890 * With Bayer format enforce even side lengths, but let the user play
1891 * with the starting pixel
1894 if (pix->height > SENSOR_MAX_HEIGHT)
1895 pix->height = SENSOR_MAX_HEIGHT;
1896 else if (pix->height < SENSOR_MIN_HEIGHT)
1897 pix->height = SENSOR_MIN_HEIGHT;
1899 pix->height = ALIGN(pix->height, 2);
1901 if (pix->width > SENSOR_MAX_WIDTH)
1902 pix->width = SENSOR_MAX_WIDTH;
1903 else if (pix->width < SENSOR_MIN_WIDTH)
1904 pix->width = SENSOR_MIN_WIDTH;
1906 pix->width = ALIGN(pix->width, 2);
1907 /*not support 720p video*/
1908 if(pix->height == 720 && pix->width == 1280){
1915 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1917 struct i2c_client *client = sd->priv;
1919 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1922 if (id->match.addr != client->addr)
1925 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
1930 #if CONFIG_SENSOR_Brightness
1931 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1933 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1935 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1937 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1939 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1941 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1944 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1948 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1952 #if CONFIG_SENSOR_Effect
1953 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1955 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1957 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1959 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1961 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1963 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1966 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1970 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1974 #if CONFIG_SENSOR_Exposure
1975 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1977 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1979 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1981 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1983 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1985 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1988 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1992 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1996 #if CONFIG_SENSOR_Saturation
1997 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1999 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2001 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2003 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2005 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2007 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2010 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2014 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2018 #if CONFIG_SENSOR_Contrast
2019 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2021 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2023 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2025 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2027 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2029 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2032 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2036 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2040 #if CONFIG_SENSOR_Mirror
2041 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2043 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2045 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2047 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2049 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2051 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2054 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2058 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2062 #if CONFIG_SENSOR_Flip
2063 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2065 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2067 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2069 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2071 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2073 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2076 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2080 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2084 #if CONFIG_SENSOR_Scene
2085 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2087 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2089 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2091 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2093 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2095 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2098 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2102 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2106 #if CONFIG_SENSOR_WhiteBalance
2107 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2109 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2111 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2113 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2115 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2117 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2120 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2124 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2128 #if CONFIG_SENSOR_DigitalZoom
2129 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2131 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2132 struct sensor *sensor = to_sensor(client);
2133 const struct v4l2_queryctrl *qctrl_info;
2134 int digitalzoom_cur, digitalzoom_total;
2136 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2140 digitalzoom_cur = sensor->info_priv.digitalzoom;
2141 digitalzoom_total = qctrl_info->maximum;
2143 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2145 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2149 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2151 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2155 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2157 *value = digitalzoom_total - digitalzoom_cur;
2160 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2162 *value = 0 - digitalzoom_cur;
2165 digitalzoom_cur += *value;
2167 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2169 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2171 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2174 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2181 #if CONFIG_SENSOR_Flash
2182 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2184 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2185 if (value == 3) { /* ddl@rock-chips.com: torch */
2186 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2188 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2190 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2194 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2199 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2201 struct i2c_client *client = sd->priv;
2202 struct sensor *sensor = to_sensor(client);
2203 const struct v4l2_queryctrl *qctrl;
2205 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2209 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2215 case V4L2_CID_BRIGHTNESS:
2217 ctrl->value = sensor->info_priv.brightness;
2220 case V4L2_CID_SATURATION:
2222 ctrl->value = sensor->info_priv.saturation;
2225 case V4L2_CID_CONTRAST:
2227 ctrl->value = sensor->info_priv.contrast;
2230 case V4L2_CID_DO_WHITE_BALANCE:
2232 ctrl->value = sensor->info_priv.whiteBalance;
2235 case V4L2_CID_EXPOSURE:
2237 ctrl->value = sensor->info_priv.exposure;
2240 case V4L2_CID_HFLIP:
2242 ctrl->value = sensor->info_priv.mirror;
2245 case V4L2_CID_VFLIP:
2247 ctrl->value = sensor->info_priv.flip;
2258 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2260 struct i2c_client *client = sd->priv;
2261 struct sensor *sensor = to_sensor(client);
2262 struct soc_camera_device *icd = client->dev.platform_data;
2263 const struct v4l2_queryctrl *qctrl;
2266 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2270 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2276 #if CONFIG_SENSOR_Brightness
2277 case V4L2_CID_BRIGHTNESS:
2279 if (ctrl->value != sensor->info_priv.brightness)
2281 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2285 sensor->info_priv.brightness = ctrl->value;
2290 #if CONFIG_SENSOR_Exposure
2291 case V4L2_CID_EXPOSURE:
2293 if (ctrl->value != sensor->info_priv.exposure)
2295 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2299 sensor->info_priv.exposure = ctrl->value;
2304 #if CONFIG_SENSOR_Saturation
2305 case V4L2_CID_SATURATION:
2307 if (ctrl->value != sensor->info_priv.saturation)
2309 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2313 sensor->info_priv.saturation = ctrl->value;
2318 #if CONFIG_SENSOR_Contrast
2319 case V4L2_CID_CONTRAST:
2321 if (ctrl->value != sensor->info_priv.contrast)
2323 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2327 sensor->info_priv.contrast = ctrl->value;
2332 #if CONFIG_SENSOR_WhiteBalance
2333 case V4L2_CID_DO_WHITE_BALANCE:
2335 if (ctrl->value != sensor->info_priv.whiteBalance)
2337 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2341 sensor->info_priv.whiteBalance = ctrl->value;
2346 #if CONFIG_SENSOR_Mirror
2347 case V4L2_CID_HFLIP:
2349 if (ctrl->value != sensor->info_priv.mirror)
2351 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2353 sensor->info_priv.mirror = ctrl->value;
2358 #if CONFIG_SENSOR_Flip
2359 case V4L2_CID_VFLIP:
2361 if (ctrl->value != sensor->info_priv.flip)
2363 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2365 sensor->info_priv.flip = ctrl->value;
2376 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2378 const struct v4l2_queryctrl *qctrl;
2379 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2380 struct sensor *sensor = to_sensor(client);
2382 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2386 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2390 switch (ext_ctrl->id)
2392 case V4L2_CID_SCENE:
2394 ext_ctrl->value = sensor->info_priv.scene;
2397 case V4L2_CID_EFFECT:
2399 ext_ctrl->value = sensor->info_priv.effect;
2402 case V4L2_CID_ZOOM_ABSOLUTE:
2404 ext_ctrl->value = sensor->info_priv.digitalzoom;
2407 case V4L2_CID_ZOOM_RELATIVE:
2411 case V4L2_CID_FOCUS_ABSOLUTE:
2413 ext_ctrl->value = sensor->info_priv.focus;
2416 case V4L2_CID_FOCUS_RELATIVE:
2420 case V4L2_CID_FLASH:
2422 ext_ctrl->value = sensor->info_priv.flash;
2430 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2432 const struct v4l2_queryctrl *qctrl;
2433 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2434 struct sensor *sensor = to_sensor(client);
2437 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2441 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2446 switch (ext_ctrl->id)
2448 #if CONFIG_SENSOR_Scene
2449 case V4L2_CID_SCENE:
2451 if (ext_ctrl->value != sensor->info_priv.scene)
2453 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2455 sensor->info_priv.scene = ext_ctrl->value;
2460 #if CONFIG_SENSOR_Effect
2461 case V4L2_CID_EFFECT:
2463 if (ext_ctrl->value != sensor->info_priv.effect)
2465 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2467 sensor->info_priv.effect= ext_ctrl->value;
2472 #if CONFIG_SENSOR_DigitalZoom
2473 case V4L2_CID_ZOOM_ABSOLUTE:
2475 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2478 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2480 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2482 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2484 sensor->info_priv.digitalzoom += val_offset;
2486 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2491 case V4L2_CID_ZOOM_RELATIVE:
2493 if (ext_ctrl->value)
2495 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2497 sensor->info_priv.digitalzoom += ext_ctrl->value;
2499 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2504 #if CONFIG_SENSOR_Focus
2505 case V4L2_CID_FOCUS_ABSOLUTE:
2507 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2510 if (ext_ctrl->value != sensor->info_priv.focus)
2512 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2514 sensor->info_priv.focus += val_offset;
2519 case V4L2_CID_FOCUS_RELATIVE:
2521 if (ext_ctrl->value)
2523 sensor->info_priv.focus += ext_ctrl->value;
2525 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2530 #if CONFIG_SENSOR_Flash
2531 case V4L2_CID_FLASH:
2533 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2535 sensor->info_priv.flash = ext_ctrl->value;
2537 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2548 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2550 struct i2c_client *client = sd->priv;
2551 struct soc_camera_device *icd = client->dev.platform_data;
2552 int i, error_cnt=0, error_idx=-1;
2555 for (i=0; i<ext_ctrl->count; i++) {
2556 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2563 error_idx = ext_ctrl->count;
2565 if (error_idx != -1) {
2566 ext_ctrl->error_idx = error_idx;
2573 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2575 struct i2c_client *client = sd->priv;
2576 struct soc_camera_device *icd = client->dev.platform_data;
2577 int i, error_cnt=0, error_idx=-1;
2580 for (i=0; i<ext_ctrl->count; i++) {
2581 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2588 error_idx = ext_ctrl->count;
2590 if (error_idx != -1) {
2591 ext_ctrl->error_idx = error_idx;
2598 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2599 * this wasn't our capture interface, so, we wait for the right one */
2600 static int sensor_video_probe(struct soc_camera_device *icd,
2601 struct i2c_client *client)
2605 struct sensor *sensor = to_sensor(client);
2607 /* We must have a parent by now. And it cannot be a wrong one.
2608 * So this entire test is completely redundant. */
2609 if (!icd->dev.parent ||
2610 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2613 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2615 goto sensor_video_probe_err;
2619 ret = sensor_write(client, 0x3021, 0x61);
2622 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2625 mdelay(5); //delay 5 microseconds
2627 /* check if it is an sensor sensor */
2628 ret = sensor_read(client, 0x307e, &value);
2630 SENSOR_TR("read chip id high byte failed\n");
2632 goto sensor_video_probe_err;
2635 pid |= (value << 8);
2637 ret = sensor_read(client, 0x307f, &value);
2639 SENSOR_TR("read chip id low byte failed\n");
2641 goto sensor_video_probe_err;
2644 pid |= (value & 0xff);
2645 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2646 if (pid == SENSOR_ID) {
2647 sensor->model = SENSOR_V4L2_IDENT;
2649 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2651 goto sensor_video_probe_err;
2654 icd->formats = sensor_colour_formats;
2655 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
2659 sensor_video_probe_err:
2663 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2665 struct i2c_client *client = sd->priv;
2666 struct soc_camera_device *icd = client->dev.platform_data;
2667 struct sensor *sensor = to_sensor(client);
2669 #if CONFIG_SENSOR_Flash
2673 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2676 case RK29_CAM_SUBDEV_DEACTIVATE:
2678 sensor_deactivate(client);
2682 case RK29_CAM_SUBDEV_IOREQUEST:
2684 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2685 if (sensor->sensor_io_request != NULL) {
2686 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2687 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2688 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2689 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2690 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2691 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2694 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2696 goto sensor_ioctl_end;
2698 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2700 #if CONFIG_SENSOR_Flash
2701 if (sensor->sensor_gpio_res) {
2702 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2703 for (i = 0; i < icd->ops->num_controls; i++) {
2704 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2705 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2708 sensor->info_priv.flash = 0xff;
2709 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2717 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2725 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2726 .init = sensor_init,
2727 .g_ctrl = sensor_g_control,
2728 .s_ctrl = sensor_s_control,
2729 .g_ext_ctrls = sensor_g_ext_controls,
2730 .s_ext_ctrls = sensor_s_ext_controls,
2731 .g_chip_ident = sensor_g_chip_ident,
2732 .ioctl = sensor_ioctl,
2735 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2736 .s_fmt = sensor_s_fmt,
2737 .g_fmt = sensor_g_fmt,
2738 .try_fmt = sensor_try_fmt,
2741 static struct v4l2_subdev_ops sensor_subdev_ops = {
2742 .core = &sensor_subdev_core_ops,
2743 .video = &sensor_subdev_video_ops,
2746 static int sensor_probe(struct i2c_client *client,
2747 const struct i2c_device_id *did)
2749 struct sensor *sensor;
2750 struct soc_camera_device *icd = client->dev.platform_data;
2751 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2752 struct soc_camera_link *icl;
2755 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2757 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2761 icl = to_soc_camera_link(icd);
2763 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2767 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2768 dev_warn(&adapter->dev,
2769 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2773 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2777 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2779 /* Second stage probe - when a capture adapter is there */
2780 icd->ops = &sensor_ops;
2781 icd->y_skip_top = 0;
2782 #if CONFIG_SENSOR_I2C_NOSCHED
2783 atomic_set(&sensor->tasklock_cnt,0);
2786 ret = sensor_video_probe(icd, client);
2789 i2c_set_clientdata(client, NULL);
2793 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2797 static int sensor_remove(struct i2c_client *client)
2799 struct sensor *sensor = to_sensor(client);
2800 struct soc_camera_device *icd = client->dev.platform_data;
2803 i2c_set_clientdata(client, NULL);
2804 client->driver = NULL;
2810 static const struct i2c_device_id sensor_id[] = {
2811 {SENSOR_NAME_STRING(), 0 },
2814 MODULE_DEVICE_TABLE(i2c, sensor_id);
2816 static struct i2c_driver sensor_i2c_driver = {
2818 .name = SENSOR_NAME_STRING(),
2820 .probe = sensor_probe,
2821 .remove = sensor_remove,
2822 .id_table = sensor_id,
2825 static int __init sensor_mod_init(void)
2827 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2828 return i2c_add_driver(&sensor_i2c_driver);
2831 static void __exit sensor_mod_exit(void)
2833 i2c_del_driver(&sensor_i2c_driver);
2836 device_initcall_sync(sensor_mod_init);
2837 module_exit(sensor_mod_exit);
2839 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2840 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2841 MODULE_LICENSE("GPL");