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(0, format, ## __VA_ARGS__)
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
42 #define MIN(x,y) ((x<y) ? x: y)
43 #define MAX(x,y) ((x>y) ? x: y)
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK29_CAM_SENSOR_GC0309
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_GC0309
48 #define SENSOR_ID 0xa0
49 #define SENSOR_MIN_WIDTH 176
50 #define SENSOR_MIN_HEIGHT 144
51 #define SENSOR_MAX_WIDTH 640
52 #define SENSOR_MAX_HEIGHT 480
53 #define SENSOR_INIT_WIDTH 640 /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT 480
55 #define SENSOR_INIT_WINSEQADR sensor_vga
56 //#define SENSOR_INIT_PIXFMT V4L2_PIX_FMT_UYVY
57 #define SENSOR_INIT_PIXFMT V4L2_PIX_FMT_YVYU
59 #define CONFIG_SENSOR_WhiteBalance 1
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 1
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 100000 /* 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_HIGH |\
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)
100 /* init SVGA preview */
101 static struct reginfo sensor_init_data[] =
103 /*init registers code.*/
105 {0xfe,0x80}, // soft reset
107 // GC0309_SET_PAGE0; // set page0
110 {0xd2,0x10}, // close AEC
111 {0x22,0x55}, // close AWB
179 /////////////////////////////////////////////////////////////////////
180 /////////////////////////// grab_t ////////////////////////////////
181 /////////////////////////////////////////////////////////////////////
201 /////////////////////////////////////////////////////////////////////
202 /////////////////////////// LSC_t ////////////////////////////////
203 /////////////////////////////////////////////////////////////////////
215 /////////////////////////////////////////////////////////////////////
216 /////////////////////////// DNDD_t ///////////////////////////////
217 /////////////////////////////////////////////////////////////////////
226 /////////////////////////////////////////////////////////////////////
227 /////////////////////////// asde_t ///////////////////////////////
228 /////////////////////////////////////////////////////////////////////
238 /////////////////////////////////////////////////////////////////////
239 /////////////////////////// eeintp_t///////////////////////////////
241 {0xfe,0x80}, /*sofware reset*/
242 {0xfe, 0x00}, /*set page0*/
245 {0xd2,0x10}, /*close AEC*/
246 {0x22,0x55}, /*close AWB*/
359 //for high resolution in light scene
487 /* Open AEC at last*/
509 /*direction: normal*/
517 static struct reginfo sensor_vga[] =
519 {0x45 , 0x0f}, //output enable
524 static struct reginfo sensor_cif[] =
528 static struct reginfo sensor_qvga[] =
532 static struct reginfo sensor_qcif[] =
536 static struct reginfo sensor_ClrFmt_YUYV[]=
542 static struct reginfo sensor_ClrFmt_UYVY[]=
548 #if CONFIG_SENSOR_WhiteBalance
549 static struct reginfo sensor_WhiteB_Auto[]=
551 {0x5a,0x56}, //for AWB can adjust back
554 {0x22,0x57}, // Enable AWB
557 /* Cloudy Colour Temperature : 6500K - 8000K */
558 static struct reginfo sensor_WhiteB_Cloudy[]=
560 {0x22,0x55}, // Disable AWB
561 {0x5a,0x8c}, //WB_manual_gain
566 /* ClearDay Colour Temperature : 5000K - 6500K */
567 static struct reginfo sensor_WhiteB_ClearDay[]=
576 /* Office Colour Temperature : 3500K - 5000K */
577 static struct reginfo sensor_WhiteB_Incandescent[]=
587 /* Home Colour Temperature : 2500K - 3500K */
588 static struct reginfo sensor_WhiteB_Fluorescent[]=
597 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_Incandescent,sensor_WhiteB_Fluorescent,
598 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
602 #if CONFIG_SENSOR_Brightness
603 static struct reginfo sensor_Brightness0[]=
610 static struct reginfo sensor_Brightness1[]=
617 static struct reginfo sensor_Brightness2[]=
624 static struct reginfo sensor_Brightness3[]=
631 static struct reginfo sensor_Brightness4[]=
638 static struct reginfo sensor_Brightness5[]=
644 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
645 sensor_Brightness4, sensor_Brightness5,NULL,
650 #if CONFIG_SENSOR_Effect
651 static struct reginfo sensor_Effect_Normal[] =
667 static struct reginfo sensor_Effect_WandB[] =
684 static struct reginfo sensor_Effect_Sepia[] =
699 static struct reginfo sensor_Effect_Negative[] =
714 static struct reginfo sensor_Effect_Bluish[] =
730 static struct reginfo sensor_Effect_Green[] =
747 static struct reginfo sensor_Effect_Grayscale[]=
762 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
763 sensor_Effect_Bluish, sensor_Effect_Green,sensor_Effect_Grayscale,NULL,
766 #if CONFIG_SENSOR_Exposure
767 static struct reginfo sensor_Exposure0[]=
773 static struct reginfo sensor_Exposure1[]=
780 static struct reginfo sensor_Exposure2[]=
787 static struct reginfo sensor_Exposure3[]=
794 static struct reginfo sensor_Exposure4[]=
801 static struct reginfo sensor_Exposure5[]=
808 static struct reginfo sensor_Exposure6[]=
815 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
816 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
819 #if CONFIG_SENSOR_Saturation
820 static struct reginfo sensor_Saturation0[]=
826 static struct reginfo sensor_Saturation1[]=
832 static struct reginfo sensor_Saturation2[]=
837 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
840 #if CONFIG_SENSOR_Contrast
841 static struct reginfo sensor_Contrast0[]=
848 static struct reginfo sensor_Contrast1[]=
855 static struct reginfo sensor_Contrast2[]=
862 static struct reginfo sensor_Contrast3[]=
869 static struct reginfo sensor_Contrast4[]=
877 static struct reginfo sensor_Contrast5[]=
884 static struct reginfo sensor_Contrast6[]=
890 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
891 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
895 #if CONFIG_SENSOR_Mirror
896 static struct reginfo sensor_MirrorOn[]=
902 static struct reginfo sensor_MirrorOff[]=
907 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
909 #if CONFIG_SENSOR_Flip
910 static struct reginfo sensor_FlipOn[]=
916 static struct reginfo sensor_FlipOff[]=
921 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
924 #if CONFIG_SENSOR_Scene
925 static struct reginfo sensor_SceneAuto[] =
927 #if 0 /* ddl@rock-chips.com : */
935 {0x20 ,0x7f}, // close cc
944 static struct reginfo sensor_SceneNight[] =
947 //30fps ~ 5fps night mode for 60/50Hz light environment, 24Mhz clock input,36Mzh pclk
949 {0x20 ,0x5f}, // close cc
956 //15fps ~ 5fps night mode for 60/50Hz light environment, 24Mhz clock input,18Mhz pclk
972 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
975 #if CONFIG_SENSOR_DigitalZoom
976 static struct reginfo sensor_Zoom0[] =
981 static struct reginfo sensor_Zoom1[] =
986 static struct reginfo sensor_Zoom2[] =
992 static struct reginfo sensor_Zoom3[] =
996 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
998 static const struct v4l2_querymenu sensor_menus[] =
1000 #if CONFIG_SENSOR_WhiteBalance
1001 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
1002 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
1003 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
1006 #if CONFIG_SENSOR_Effect
1007 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
1008 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
1009 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1010 { .id = V4L2_CID_EFFECT, .index = 6, .name = "grayscale", .reserved = 0,},
1013 #if CONFIG_SENSOR_Scene
1014 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1017 #if CONFIG_SENSOR_Flash
1018 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1019 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1023 static const struct v4l2_queryctrl sensor_controls[] =
1025 #if CONFIG_SENSOR_WhiteBalance
1027 .id = V4L2_CID_DO_WHITE_BALANCE,
1028 .type = V4L2_CTRL_TYPE_MENU,
1029 .name = "White Balance Control",
1037 #if CONFIG_SENSOR_Brightness
1039 .id = V4L2_CID_BRIGHTNESS,
1040 .type = V4L2_CTRL_TYPE_INTEGER,
1041 .name = "Brightness Control",
1049 #if CONFIG_SENSOR_Effect
1051 .id = V4L2_CID_EFFECT,
1052 .type = V4L2_CTRL_TYPE_MENU,
1053 .name = "Effect Control",
1061 #if CONFIG_SENSOR_Exposure
1063 .id = V4L2_CID_EXPOSURE,
1064 .type = V4L2_CTRL_TYPE_INTEGER,
1065 .name = "Exposure Control",
1073 #if CONFIG_SENSOR_Saturation
1075 .id = V4L2_CID_SATURATION,
1076 .type = V4L2_CTRL_TYPE_INTEGER,
1077 .name = "Saturation Control",
1085 #if CONFIG_SENSOR_Contrast
1087 .id = V4L2_CID_CONTRAST,
1088 .type = V4L2_CTRL_TYPE_INTEGER,
1089 .name = "Contrast Control",
1097 #if CONFIG_SENSOR_Mirror
1099 .id = V4L2_CID_HFLIP,
1100 .type = V4L2_CTRL_TYPE_BOOLEAN,
1101 .name = "Mirror Control",
1109 #if CONFIG_SENSOR_Flip
1111 .id = V4L2_CID_VFLIP,
1112 .type = V4L2_CTRL_TYPE_BOOLEAN,
1113 .name = "Flip Control",
1121 #if CONFIG_SENSOR_Scene
1123 .id = V4L2_CID_SCENE,
1124 .type = V4L2_CTRL_TYPE_MENU,
1125 .name = "Scene Control",
1133 #if CONFIG_SENSOR_DigitalZoom
1135 .id = V4L2_CID_ZOOM_RELATIVE,
1136 .type = V4L2_CTRL_TYPE_INTEGER,
1137 .name = "DigitalZoom Control",
1143 .id = V4L2_CID_ZOOM_ABSOLUTE,
1144 .type = V4L2_CTRL_TYPE_INTEGER,
1145 .name = "DigitalZoom Control",
1153 #if CONFIG_SENSOR_Focus
1155 .id = V4L2_CID_FOCUS_RELATIVE,
1156 .type = V4L2_CTRL_TYPE_INTEGER,
1157 .name = "Focus Control",
1163 .id = V4L2_CID_FOCUS_ABSOLUTE,
1164 .type = V4L2_CTRL_TYPE_INTEGER,
1165 .name = "Focus Control",
1169 .default_value = 125,
1173 #if CONFIG_SENSOR_Flash
1175 .id = V4L2_CID_FLASH,
1176 .type = V4L2_CTRL_TYPE_MENU,
1177 .name = "Flash Control",
1186 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1187 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1188 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1189 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1190 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1191 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1192 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1193 static int sensor_resume(struct soc_camera_device *icd);
1194 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1195 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1196 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1197 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1198 static int sensor_deactivate(struct i2c_client *client);
1200 static struct soc_camera_ops sensor_ops =
1202 .suspend = sensor_suspend,
1203 .resume = sensor_resume,
1204 .set_bus_param = sensor_set_bus_param,
1205 .query_bus_param = sensor_query_bus_param,
1206 .controls = sensor_controls,
1207 .menus = sensor_menus,
1208 .num_controls = ARRAY_SIZE(sensor_controls),
1209 .num_menus = ARRAY_SIZE(sensor_menus),
1212 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1213 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1214 .colorspace = _colorspace }
1216 #define JPG_FMT(_name, _depth, _fourcc) \
1217 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1219 static const struct soc_camera_data_format sensor_colour_formats[] = {
1220 JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1221 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1224 typedef struct sensor_info_priv_s
1238 unsigned char mirror; /* HFLIP */
1239 unsigned char flip; /* VFLIP */
1240 unsigned int winseqe_cur_addr;
1241 unsigned int pixfmt;
1243 } sensor_info_priv_t;
1247 struct v4l2_subdev subdev;
1248 struct i2c_client *client;
1249 sensor_info_priv_t info_priv;
1250 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1251 #if CONFIG_SENSOR_I2C_NOSCHED
1252 atomic_t tasklock_cnt;
1254 struct rk29camera_platform_data *sensor_io_request;
1255 struct rk29camera_gpio_res *sensor_gpio_res;
1258 static struct sensor* to_sensor(const struct i2c_client *client)
1260 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1263 static int sensor_task_lock(struct i2c_client *client, int lock)
1265 #if CONFIG_SENSOR_I2C_NOSCHED
1267 struct sensor *sensor = to_sensor(client);
1270 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1271 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1272 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1276 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1277 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1278 goto sensor_task_lock_err;
1283 atomic_add(1, &sensor->tasklock_cnt);
1285 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1286 atomic_sub(1, &sensor->tasklock_cnt);
1288 if (atomic_read(&sensor->tasklock_cnt) == 0)
1294 sensor_task_lock_err:
1299 /* sensor register */
1300 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1304 ret = i2c_master_reg8_recv(client, reg, val, 1, CONFIG_SENSOR_I2C_SPEED);
1306 return (ret > 0)? 0 : ret;
1309 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1313 ret = i2c_master_reg8_send(client, reg, &val, 1, CONFIG_SENSOR_I2C_SPEED);
1315 return (ret > 0)? 0 : ret;
1318 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1322 struct i2c_msg msg[1];
1330 msg->addr = client->addr;
1331 msg->flags = client->flags;
1333 msg->len = sizeof(buf);
1334 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1335 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1340 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1341 err = i2c_transfer(client->adapter, msg, 1);
1346 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1354 /* sensor register read */
1355 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1359 struct i2c_msg msg[2];
1363 msg[0].addr = client->addr;
1364 msg[0].flags = client->flags;
1366 msg[0].len = sizeof(buf);
1367 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1368 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1370 msg[1].addr = client->addr;
1371 msg[1].flags = client->flags|I2C_M_RD;
1374 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1375 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1379 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1380 err = i2c_transfer(client->adapter, msg, 2);
1386 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1396 /* write a array of registers */
1397 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1405 if (sensor_task_lock(client, 1) < 0)
1406 goto sensor_write_array_end;
1407 while (regarray[i].reg != 0)
1409 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1413 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1417 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1419 goto sensor_write_array_end;
1422 #if CONFIG_SENSOR_I2C_RDWRCHK
1424 sensor_read(client, regarray[i].reg, &valchk);
1425 if (valchk != regarray[i].val)
1426 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1432 sensor_write_array_end:
1433 sensor_task_lock(client,0);
1436 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1444 while (regarray[i].reg != 0)
1446 sensor_read(client, regarray[i].reg, &valchk);
1447 if (valchk != regarray[i].val)
1448 SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1454 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1456 struct soc_camera_link *icl = to_soc_camera_link(icd);
1459 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1462 case Sensor_PowerDown:
1464 if (icl->powerdown) {
1465 ret = icl->powerdown(icd->pdev, on);
1466 if (ret == RK29_CAM_IO_SUCCESS) {
1470 icl->reset(icd->pdev);
1472 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1474 goto sensor_power_end;
1481 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1482 struct sensor *sensor = to_sensor(client);
1484 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1485 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1491 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1498 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1500 struct i2c_client *client = sd->priv;
1501 struct soc_camera_device *icd = client->dev.platform_data;
1502 struct sensor *sensor = to_sensor(client);
1503 const struct v4l2_queryctrl *qctrl;
1507 SENSOR_DG("\n-------------%s..%s.. %d,val = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__,val);
1509 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1510 SENSOR_DG("\n-------------%s..%s.. %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1512 goto sensor_INIT_ERR;
1516 if (sensor_task_lock(client,1)<0)
1517 goto sensor_INIT_ERR;
1518 ret = sensor_write(client, 0xfe, 0x80);
1521 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1523 goto sensor_INIT_ERR;
1525 SENSOR_DG("--------------------%s , %s, %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1526 mdelay(5); //delay 5 microseconds
1527 /* check if it is an sensor sensor */
1528 ret = sensor_read(client, 0x00, &value);
1530 SENSOR_TR("read chip id high byte failed\n");
1532 goto sensor_INIT_ERR;
1534 SENSOR_DG("--------------------%s , %s, %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1535 if (value == SENSOR_ID) {
1536 sensor->model = SENSOR_V4L2_IDENT;
1538 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), value);
1540 goto sensor_INIT_ERR;
1542 SENSOR_DG("--------------------%s , %s, %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1543 ret = sensor_write_array(client, sensor_init_data);
1546 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1547 goto sensor_INIT_ERR;
1549 sensor_task_lock(client,0);
1550 //icd->user_width = SENSOR_INIT_WIDTH;
1551 //icd->user_height = SENSOR_INIT_HEIGHT;
1552 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1553 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
1554 SENSOR_DG("--------------------%s , %s, %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1555 /* sensor sensor information for initialization */
1556 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1558 sensor->info_priv.whiteBalance = qctrl->default_value;
1559 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1561 sensor->info_priv.brightness = qctrl->default_value;
1562 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1564 sensor->info_priv.effect = qctrl->default_value;
1565 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1567 sensor->info_priv.exposure = qctrl->default_value;
1569 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1571 sensor->info_priv.saturation = qctrl->default_value;
1572 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1574 sensor->info_priv.contrast = qctrl->default_value;
1575 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1577 sensor->info_priv.mirror = qctrl->default_value;
1578 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1580 sensor->info_priv.flip = qctrl->default_value;
1581 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1583 sensor->info_priv.scene = qctrl->default_value;
1584 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1586 sensor->info_priv.digitalzoom = qctrl->default_value;
1588 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1589 #if CONFIG_SENSOR_Focus
1590 //sensor_set_focus();
1591 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1593 sensor->info_priv.focus = qctrl->default_value;
1595 SENSOR_DG("--------------------%s , %s, %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1596 #if CONFIG_SENSOR_Flash
1597 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1599 sensor->info_priv.flash = qctrl->default_value;
1602 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);
1606 sensor_task_lock(client,0);
1607 sensor_deactivate(client);
1611 static int sensor_deactivate(struct i2c_client *client)
1613 struct soc_camera_device *icd = client->dev.platform_data;
1614 SENSOR_DG("\n----------------------------%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1616 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1617 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1619 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1620 icd->user_width = SENSOR_INIT_WIDTH;
1621 icd->user_height = SENSOR_INIT_HEIGHT;
1626 static struct reginfo sensor_power_down_sequence[]=
1630 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1633 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1635 if (pm_msg.event == PM_EVENT_SUSPEND) {
1636 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1637 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1639 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1642 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1644 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1649 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1655 static int sensor_resume(struct soc_camera_device *icd)
1659 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1661 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1665 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1671 static int sensor_set_bus_param(struct soc_camera_device *icd,
1672 unsigned long flags)
1678 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1680 struct soc_camera_link *icl = to_soc_camera_link(icd);
1681 unsigned long flags = SENSOR_BUS_PARAM;
1683 return soc_camera_apply_sensor_flags(icl, flags);
1686 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1688 struct i2c_client *client = sd->priv;
1689 struct soc_camera_device *icd = client->dev.platform_data;
1690 struct sensor *sensor = to_sensor(client);
1691 struct v4l2_pix_format *pix = &f->fmt.pix;
1693 pix->width = icd->user_width;
1694 pix->height = icd->user_height;
1695 pix->pixelformat = sensor->info_priv.pixfmt;
1696 pix->field = V4L2_FIELD_NONE;
1697 pix->colorspace = V4L2_COLORSPACE_JPEG;
1701 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
1705 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
1707 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
1709 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
1711 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
1713 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
1718 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1722 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
1726 if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
1728 } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
1733 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1736 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1738 struct i2c_client *client = sd->priv;
1739 struct sensor *sensor = to_sensor(client);
1740 struct v4l2_pix_format *pix = &f->fmt.pix;
1741 const struct v4l2_queryctrl *qctrl;
1742 struct soc_camera_device *icd = client->dev.platform_data;
1743 struct reginfo *winseqe_set_addr=NULL;
1744 int ret=0, set_w,set_h;
1746 SENSOR_TR("------------------------------%s, %s ,%d,\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1747 if (sensor->info_priv.pixfmt != pix->pixelformat) {
1748 SENSOR_TR("------------------------------%s, %s ,%d,\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1749 switch (pix->pixelformat)
1751 SENSOR_TR("------------------------------%s, %s ,%d,\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1752 case V4L2_PIX_FMT_YUYV:
1754 SENSOR_TR("------------------------------%s, %s ,%d,\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1755 winseqe_set_addr = sensor_ClrFmt_YUYV;
1758 case V4L2_PIX_FMT_UYVY:
1760 SENSOR_TR("------------------------------%s, %s ,%d,\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1761 winseqe_set_addr = sensor_ClrFmt_UYVY;
1767 if (winseqe_set_addr != NULL) {
1768 sensor_write_array(client, winseqe_set_addr);
1769 sensor->info_priv.pixfmt = pix->pixelformat;
1771 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1773 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1778 set_h = pix->height;
1779 SENSOR_TR("------------------------------%s, %s ,%d,\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1780 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1782 winseqe_set_addr = sensor_qcif;
1786 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1788 winseqe_set_addr = sensor_qvga;
1792 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1794 winseqe_set_addr = sensor_cif;
1798 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1800 winseqe_set_addr = sensor_vga;
1807 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1808 set_w = SENSOR_INIT_WIDTH;
1809 set_h = SENSOR_INIT_HEIGHT;
1810 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
1812 SENSOR_TR("------------------------------%s, %s ,%d,\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
1813 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1814 ret |= sensor_write_array(client, winseqe_set_addr);
1816 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1817 goto sensor_s_fmt_end;
1820 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1822 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
1823 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1824 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1825 if (sensor->info_priv.whiteBalance != 0) {
1826 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1827 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1829 sensor->info_priv.snap2preview = true;
1830 } else if (sensor_fmt_videochk(sd,f) == true) { /* ddl@rock-chips.com : Video */
1831 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1832 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1833 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1834 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1835 sensor->info_priv.video2preview = true;
1836 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1837 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1838 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1839 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1840 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1841 sensor->info_priv.video2preview = false;
1842 sensor->info_priv.snap2preview = false;
1844 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1848 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1852 pix->height = set_h;
1858 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1860 struct v4l2_pix_format *pix = &f->fmt.pix;
1861 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
1862 pix->pixelformat == V4L2_PIX_FMT_YUYV;
1865 * With Bayer format enforce even side lengths, but let the user play
1866 * with the starting pixel
1869 if (pix->height > SENSOR_MAX_HEIGHT)
1870 pix->height = SENSOR_MAX_HEIGHT;
1871 else if (pix->height < SENSOR_MIN_HEIGHT)
1872 pix->height = SENSOR_MIN_HEIGHT;
1874 pix->height = ALIGN(pix->height, 2);
1876 if (pix->width > SENSOR_MAX_WIDTH)
1877 pix->width = SENSOR_MAX_WIDTH;
1878 else if (pix->width < SENSOR_MIN_WIDTH)
1879 pix->width = SENSOR_MIN_WIDTH;
1881 pix->width = ALIGN(pix->width, 2);
1886 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1888 struct i2c_client *client = sd->priv;
1890 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1893 if (id->match.addr != client->addr)
1896 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
1901 #if CONFIG_SENSOR_Brightness
1902 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1904 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1906 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1908 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1910 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1912 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1915 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1919 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1923 #if CONFIG_SENSOR_Effect
1924 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1926 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1928 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1930 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1932 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1934 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1937 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1941 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1945 #if CONFIG_SENSOR_Exposure
1946 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1948 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1950 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1952 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1954 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1956 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1959 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1963 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1967 #if CONFIG_SENSOR_Saturation
1968 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1970 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1972 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1974 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1976 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1978 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1981 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1985 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1989 #if CONFIG_SENSOR_Contrast
1990 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1992 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1994 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1996 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1998 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2000 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2003 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2007 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2011 #if CONFIG_SENSOR_Mirror
2012 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2014 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2016 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2018 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2020 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2022 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2025 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2029 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2033 #if CONFIG_SENSOR_Flip
2034 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2036 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2038 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2040 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2042 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2044 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2047 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2051 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2055 #if CONFIG_SENSOR_Scene
2056 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2058 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2060 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2062 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2064 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2066 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2069 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2073 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2077 #if CONFIG_SENSOR_WhiteBalance
2078 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2080 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2082 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2084 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2086 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2088 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2091 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2095 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2099 #if CONFIG_SENSOR_DigitalZoom
2100 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2102 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2103 struct sensor *sensor = to_sensor(client);
2104 const struct v4l2_queryctrl *qctrl_info;
2105 int digitalzoom_cur, digitalzoom_total;
2107 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2111 digitalzoom_cur = sensor->info_priv.digitalzoom;
2112 digitalzoom_total = qctrl_info->maximum;
2114 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2116 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2120 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2122 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2126 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2128 *value = digitalzoom_total - digitalzoom_cur;
2131 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2133 *value = 0 - digitalzoom_cur;
2136 digitalzoom_cur += *value;
2138 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2140 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2142 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2145 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2152 #if CONFIG_SENSOR_Flash
2153 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2155 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2156 if (value == 3) { /* ddl@rock-chips.com: torch */
2157 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2159 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2161 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2165 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2170 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2172 struct i2c_client *client = sd->priv;
2173 struct sensor *sensor = to_sensor(client);
2174 const struct v4l2_queryctrl *qctrl;
2176 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2180 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2186 case V4L2_CID_BRIGHTNESS:
2188 ctrl->value = sensor->info_priv.brightness;
2191 case V4L2_CID_SATURATION:
2193 ctrl->value = sensor->info_priv.saturation;
2196 case V4L2_CID_CONTRAST:
2198 ctrl->value = sensor->info_priv.contrast;
2201 case V4L2_CID_DO_WHITE_BALANCE:
2203 ctrl->value = sensor->info_priv.whiteBalance;
2206 case V4L2_CID_EXPOSURE:
2208 ctrl->value = sensor->info_priv.exposure;
2211 case V4L2_CID_HFLIP:
2213 ctrl->value = sensor->info_priv.mirror;
2216 case V4L2_CID_VFLIP:
2218 ctrl->value = sensor->info_priv.flip;
2229 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2231 struct i2c_client *client = sd->priv;
2232 struct sensor *sensor = to_sensor(client);
2233 struct soc_camera_device *icd = client->dev.platform_data;
2234 const struct v4l2_queryctrl *qctrl;
2237 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2241 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2247 #if CONFIG_SENSOR_Brightness
2248 case V4L2_CID_BRIGHTNESS:
2250 if (ctrl->value != sensor->info_priv.brightness)
2252 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2256 sensor->info_priv.brightness = ctrl->value;
2261 #if CONFIG_SENSOR_Exposure
2262 case V4L2_CID_EXPOSURE:
2264 if (ctrl->value != sensor->info_priv.exposure)
2266 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2270 sensor->info_priv.exposure = ctrl->value;
2275 #if CONFIG_SENSOR_Saturation
2276 case V4L2_CID_SATURATION:
2278 if (ctrl->value != sensor->info_priv.saturation)
2280 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2284 sensor->info_priv.saturation = ctrl->value;
2289 #if CONFIG_SENSOR_Contrast
2290 case V4L2_CID_CONTRAST:
2292 if (ctrl->value != sensor->info_priv.contrast)
2294 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2298 sensor->info_priv.contrast = ctrl->value;
2303 #if CONFIG_SENSOR_WhiteBalance
2304 case V4L2_CID_DO_WHITE_BALANCE:
2306 if (ctrl->value != sensor->info_priv.whiteBalance)
2308 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2312 sensor->info_priv.whiteBalance = ctrl->value;
2317 #if CONFIG_SENSOR_Mirror
2318 case V4L2_CID_HFLIP:
2320 if (ctrl->value != sensor->info_priv.mirror)
2322 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2324 sensor->info_priv.mirror = ctrl->value;
2329 #if CONFIG_SENSOR_Flip
2330 case V4L2_CID_VFLIP:
2332 if (ctrl->value != sensor->info_priv.flip)
2334 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2336 sensor->info_priv.flip = ctrl->value;
2347 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2349 const struct v4l2_queryctrl *qctrl;
2350 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2351 struct sensor *sensor = to_sensor(client);
2353 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2357 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2361 switch (ext_ctrl->id)
2363 case V4L2_CID_SCENE:
2365 ext_ctrl->value = sensor->info_priv.scene;
2368 case V4L2_CID_EFFECT:
2370 ext_ctrl->value = sensor->info_priv.effect;
2373 case V4L2_CID_ZOOM_ABSOLUTE:
2375 ext_ctrl->value = sensor->info_priv.digitalzoom;
2378 case V4L2_CID_ZOOM_RELATIVE:
2382 case V4L2_CID_FOCUS_ABSOLUTE:
2384 ext_ctrl->value = sensor->info_priv.focus;
2387 case V4L2_CID_FOCUS_RELATIVE:
2391 case V4L2_CID_FLASH:
2393 ext_ctrl->value = sensor->info_priv.flash;
2401 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2403 const struct v4l2_queryctrl *qctrl;
2404 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2405 struct sensor *sensor = to_sensor(client);
2408 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2412 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2417 switch (ext_ctrl->id)
2419 #if CONFIG_SENSOR_Scene
2420 case V4L2_CID_SCENE:
2422 if (ext_ctrl->value != sensor->info_priv.scene)
2424 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2426 sensor->info_priv.scene = ext_ctrl->value;
2431 #if CONFIG_SENSOR_Effect
2432 case V4L2_CID_EFFECT:
2434 if (ext_ctrl->value != sensor->info_priv.effect)
2436 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2438 sensor->info_priv.effect= ext_ctrl->value;
2443 #if CONFIG_SENSOR_DigitalZoom
2444 case V4L2_CID_ZOOM_ABSOLUTE:
2446 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2449 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2451 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2453 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2455 sensor->info_priv.digitalzoom += val_offset;
2457 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2462 case V4L2_CID_ZOOM_RELATIVE:
2464 if (ext_ctrl->value)
2466 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2468 sensor->info_priv.digitalzoom += ext_ctrl->value;
2470 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2475 #if CONFIG_SENSOR_Focus
2476 case V4L2_CID_FOCUS_ABSOLUTE:
2478 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2481 if (ext_ctrl->value != sensor->info_priv.focus)
2483 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2485 sensor->info_priv.focus += val_offset;
2490 case V4L2_CID_FOCUS_RELATIVE:
2492 if (ext_ctrl->value)
2494 sensor->info_priv.focus += ext_ctrl->value;
2496 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2501 #if CONFIG_SENSOR_Flash
2502 case V4L2_CID_FLASH:
2504 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2506 sensor->info_priv.flash = ext_ctrl->value;
2508 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2519 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2521 struct i2c_client *client = sd->priv;
2522 struct soc_camera_device *icd = client->dev.platform_data;
2523 int i, error_cnt=0, error_idx=-1;
2526 for (i=0; i<ext_ctrl->count; i++) {
2527 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2534 error_idx = ext_ctrl->count;
2536 if (error_idx != -1) {
2537 ext_ctrl->error_idx = error_idx;
2544 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2546 struct i2c_client *client = sd->priv;
2547 struct soc_camera_device *icd = client->dev.platform_data;
2548 int i, error_cnt=0, error_idx=-1;
2551 for (i=0; i<ext_ctrl->count; i++) {
2552 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2559 error_idx = ext_ctrl->count;
2561 if (error_idx != -1) {
2562 ext_ctrl->error_idx = error_idx;
2569 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2570 * this wasn't our capture interface, so, we wait for the right one */
2571 static int sensor_video_probe(struct soc_camera_device *icd,
2572 struct i2c_client *client)
2576 struct sensor *sensor = to_sensor(client);
2578 SENSOR_DG("--------------------%s , %s, %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
2580 /* We must have a parent by now. And it cannot be a wrong one.
2581 * So this entire test is completely redundant. */
2582 if (!icd->dev.parent ||
2583 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2586 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2588 goto sensor_video_probe_err;
2590 SENSOR_DG("--------------------%s , %s, %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
2592 ret = sensor_write(client, 0xfe, 0x80);
2595 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2598 mdelay(5); //delay 5 microseconds
2599 SENSOR_DG("--------------------%s , %s, %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
2600 /* check if it is an sensor sensor */
2601 ret = sensor_read(client, 0x00, &value);
2603 SENSOR_TR("read chip id high byte failed\n");
2605 goto sensor_video_probe_err;
2607 SENSOR_DG("--------------------%s , %s, %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
2608 if (value == SENSOR_ID) {
2609 sensor->model = SENSOR_V4L2_IDENT;
2610 SENSOR_TR("chip id:0x%x\n",value);
2612 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), value);
2614 goto sensor_video_probe_err;
2617 icd->formats = sensor_colour_formats;
2618 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
2619 SENSOR_DG("--------------------%s , %s, %d\n",SENSOR_NAME_STRING(),__FUNCTION__,__LINE__);
2622 sensor_video_probe_err:
2626 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2628 struct i2c_client *client = sd->priv;
2629 struct soc_camera_device *icd = client->dev.platform_data;
2630 struct sensor *sensor = to_sensor(client);
2632 #if CONFIG_SENSOR_Flash
2636 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2639 case RK29_CAM_SUBDEV_DEACTIVATE:
2641 sensor_deactivate(client);
2645 case RK29_CAM_SUBDEV_IOREQUEST:
2647 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2648 if (sensor->sensor_io_request != NULL) {
2649 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2650 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2651 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2652 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2653 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2654 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2657 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2659 goto sensor_ioctl_end;
2661 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2663 #if CONFIG_SENSOR_Flash
2664 if (sensor->sensor_gpio_res) {
2665 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2666 for (i = 0; i < icd->ops->num_controls; i++) {
2667 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2668 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2671 sensor->info_priv.flash = 0xff;
2672 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2680 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2688 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2689 .init = sensor_init,
2690 .g_ctrl = sensor_g_control,
2691 .s_ctrl = sensor_s_control,
2692 .g_ext_ctrls = sensor_g_ext_controls,
2693 .s_ext_ctrls = sensor_s_ext_controls,
2694 .g_chip_ident = sensor_g_chip_ident,
2695 .ioctl = sensor_ioctl,
2698 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2699 .s_fmt = sensor_s_fmt,
2700 .g_fmt = sensor_g_fmt,
2701 .try_fmt = sensor_try_fmt,
2704 static struct v4l2_subdev_ops sensor_subdev_ops = {
2705 .core = &sensor_subdev_core_ops,
2706 .video = &sensor_subdev_video_ops,
2709 static int sensor_probe(struct i2c_client *client,
2710 const struct i2c_device_id *did)
2712 struct sensor *sensor;
2713 struct soc_camera_device *icd = client->dev.platform_data;
2714 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2715 struct soc_camera_link *icl;
2718 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2720 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2724 icl = to_soc_camera_link(icd);
2726 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2730 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2731 dev_warn(&adapter->dev,
2732 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2736 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2740 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2742 /* Second stage probe - when a capture adapter is there */
2743 icd->ops = &sensor_ops;
2744 icd->y_skip_top = 0;
2745 #if CONFIG_SENSOR_I2C_NOSCHED
2746 atomic_set(&sensor->tasklock_cnt,0);
2749 ret = sensor_video_probe(icd, client);
2752 i2c_set_clientdata(client, NULL);
2756 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2760 static int sensor_remove(struct i2c_client *client)
2762 struct sensor *sensor = to_sensor(client);
2763 struct soc_camera_device *icd = client->dev.platform_data;
2766 i2c_set_clientdata(client, NULL);
2767 client->driver = NULL;
2773 static const struct i2c_device_id sensor_id[] = {
2774 {SENSOR_NAME_STRING(), 0 },
2777 MODULE_DEVICE_TABLE(i2c, sensor_id);
2779 static struct i2c_driver sensor_i2c_driver = {
2781 .name = SENSOR_NAME_STRING(),
2783 .probe = sensor_probe,
2784 .remove = sensor_remove,
2785 .id_table = sensor_id,
2788 static int __init sensor_mod_init(void)
2790 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2791 return i2c_add_driver(&sensor_i2c_driver);
2794 static void __exit sensor_mod_exit(void)
2796 i2c_del_driver(&sensor_i2c_driver);
2799 device_initcall_sync(sensor_mod_init);
2800 module_exit(sensor_mod_exit);
2802 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2803 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2804 MODULE_LICENSE("GPL");