3 o* Driver for MT9M001 CMOS Image Sensor from Micron
5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/videodev2.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/log2.h>
16 #include <linux/platform_device.h>
17 #include <linux/delay.h>
18 #include <linux/circ_buf.h>
19 #include <linux/miscdevice.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-chip-ident.h>
22 #include <media/soc_camera.h>
23 #include <mach/rk29_camera.h>
26 module_param(debug, int, S_IRUGO|S_IWUSR);
28 #define dprintk(level, fmt, arg...) do { \
30 printk(KERN_WARNING fmt , ## arg); } while (0)
32 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
33 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
36 #define _CONS(a,b) a##b
37 #define CONS(a,b) _CONS(a,b)
40 #define _STR(x) __STR(x)
41 #define STR(x) _STR(x)
43 #define MIN(x,y) ((x<y) ? x: y)
44 #define MAX(x,y) ((x>y) ? x: y)
46 /* Sensor Driver Configuration */
47 #define SENSOR_NAME RK29_CAM_SENSOR_GC0308
48 #define SENSOR_V4L2_IDENT V4L2_IDENT_GC0308
49 #define SENSOR_ID 0x9b
50 #define SENSOR_MIN_WIDTH 640//176
51 #define SENSOR_MIN_HEIGHT 480//144
52 #define SENSOR_MAX_WIDTH 640
53 #define SENSOR_MAX_HEIGHT 480
54 #define SENSOR_INIT_WIDTH 640 /* Sensor pixel size for sensor_init_data array */
55 #define SENSOR_INIT_HEIGHT 480
56 #define SENSOR_INIT_WINSEQADR sensor_vga
57 #define SENSOR_INIT_PIXFMT V4L2_PIX_FMT_YUYV
59 #define CONFIG_SENSOR_WhiteBalance 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 250000 /* Hz */
73 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
74 #define CONFIG_SENSOR_I2C_NOSCHED 0
75 #define CONFIG_SENSOR_I2C_RDWRCHK 0
77 #define SENSOR_BUS_PARAM (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
78 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
79 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8 |SOCAM_MCLK_24MHZ)
81 #define COLOR_TEMPERATURE_CLOUDY_DN 6500
82 #define COLOR_TEMPERATURE_CLOUDY_UP 8000
83 #define COLOR_TEMPERATURE_CLEARDAY_DN 5000
84 #define COLOR_TEMPERATURE_CLEARDAY_UP 6500
85 #define COLOR_TEMPERATURE_OFFICE_DN 3500
86 #define COLOR_TEMPERATURE_OFFICE_UP 5000
87 #define COLOR_TEMPERATURE_HOME_DN 2500
88 #define COLOR_TEMPERATURE_HOME_UP 3500
90 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
91 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
99 /* init 640X480 VGA */
100 static struct reginfo sensor_init_data[] =
105 {0xfe , 0x00}, // set page0
107 {0xd2 , 0x10}, // close AEC
108 {0x22 , 0x55}, // close AWB
116 {0x22 , 0x57}, // Open AWB
123 {0xe2 , 0x00}, //anti-flicker step [11:8]
124 {0xe3 , 0x64}, //anti-flicker step [7:0]
126 {0xe4 , 0x02}, //exp level 1 16.67fps
128 {0xe6 , 0x03}, //exp level 2 12.5fps
130 {0xe8 , 0x04}, //exp level 3 8.33fps
132 {0xea , 0x09}, //exp level 4 4.00fps
172 {0x26 , 0x02}, //0x03 20101016 zhj
292 //---------------------------------------------------------------
293 {0xfe , 0x01},// set page1
345 {0xfe , 0x00}, // set page0
347 //-----------Update the registers 2010/07/06-------------//
349 {0xfe , 0x00}, // set page0
351 {0x11 , 0x0d}, // fd,modified by mormo 2010/07/06
352 {0x1a , 0x2a}, // 1e,modified by mormo 2010/07/06
354 {0x1c , 0x49}, // c1,modified by mormo 2010/07/06
355 {0x1d , 0x9a}, // 08,modified by mormo 2010/07/06
356 {0x1e , 0x61}, // 60,modified by mormo 2010/07/06
360 {0x50 , 0x14}, // 10,modified by mormo 2010/07/06
375 {0xb1 , 0x40}, // YCPT
380 {0xd0 , 0xcb}, // AECT c9,modifed by mormo 2010/07/06
381 {0xd3 , 0x48}, // 80,modified by mormor 2010/07/06
388 {0xfe , 0x01},// set page1
412 //-----------Update the registers end---------//
415 {0xfe , 0x00}, // set page0
419 //-----------GAMMA Select(3)---------------//
438 /*GC0308_GAMMA_Select,
439 1: //smallest gamma curve
515 5: //largest gamma curve
533 //-----------GAMMA Select End--------------//
538 //-------------H_V_Switch(4)---------------//
539 {0x14 , 0x12}, //0x10
552 4: // IMAGE_HV_MIRROR
555 //-------------H_V_Select End--------------//
560 static struct reginfo sensor_sxga[] =
566 static struct reginfo sensor_svga[] =
572 static struct reginfo sensor_vga[] =
584 static struct reginfo sensor_cif[] =
590 static struct reginfo sensor_qvga[] =
596 static struct reginfo sensor_qcif[] =
601 static struct reginfo sensor_ClrFmt_YUYV[]=
606 static struct reginfo sensor_ClrFmt_UYVY[]=
612 #if CONFIG_SENSOR_WhiteBalance
613 static struct reginfo sensor_WhiteB_Auto[]=
621 /* Cloudy Colour Temperature : 6500K - 8000K */
622 static struct reginfo sensor_WhiteB_Cloudy[]=
624 {0x22, 0x55}, // Disable AWB
630 /* ClearDay Colour Temperature : 5000K - 6500K */
631 static struct reginfo sensor_WhiteB_ClearDay[]=
634 {0x22, 0x55}, // Disable AWB
640 /* Office Colour Temperature : 3500K - 5000K */
641 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
644 {0x22, 0x55}, // Disable AWB
651 /* Home Colour Temperature : 2500K - 3500K */
652 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
655 {0x22, 0x55}, // Disable AWB
661 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
662 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
666 #if CONFIG_SENSOR_Brightness
667 static struct reginfo sensor_Brightness0[]=
674 static struct reginfo sensor_Brightness1[]=
681 static struct reginfo sensor_Brightness2[]=
688 static struct reginfo sensor_Brightness3[]=
695 static struct reginfo sensor_Brightness4[]=
702 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[] =
730 static struct reginfo sensor_Effect_WandB[] =
744 static struct reginfo sensor_Effect_Sepia[] =
758 static struct reginfo sensor_Effect_Negative[] =
772 static struct reginfo sensor_Effect_Bluish[] =
787 static struct reginfo sensor_Effect_Green[] =
801 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
802 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
805 #if CONFIG_SENSOR_Exposure
806 static struct reginfo sensor_Exposure0[]=
812 static struct reginfo sensor_Exposure1[]=
818 static struct reginfo sensor_Exposure2[]=
824 static struct reginfo sensor_Exposure3[]=
830 static struct reginfo sensor_Exposure4[]=
836 static struct reginfo sensor_Exposure5[]=
842 static struct reginfo sensor_Exposure6[]=
848 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
849 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
852 #if CONFIG_SENSOR_Saturation
853 static struct reginfo sensor_Saturation0[]=
858 static struct reginfo sensor_Saturation1[]=
863 static struct reginfo sensor_Saturation2[]=
867 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
870 #if CONFIG_SENSOR_Contrast
871 static struct reginfo sensor_Contrast0[]=
877 static struct reginfo sensor_Contrast1[]=
883 static struct reginfo sensor_Contrast2[]=
889 static struct reginfo sensor_Contrast3[]=
895 static struct reginfo sensor_Contrast4[]=
902 static struct reginfo sensor_Contrast5[]=
908 static struct reginfo sensor_Contrast6[]=
913 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
914 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
918 #if CONFIG_SENSOR_Mirror
919 static struct reginfo sensor_MirrorOn[]=
924 static struct reginfo sensor_MirrorOff[]=
928 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
930 #if CONFIG_SENSOR_Flip
931 static struct reginfo sensor_FlipOn[]=
936 static struct reginfo sensor_FlipOff[]=
940 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
943 #if CONFIG_SENSOR_Scene
944 static struct reginfo sensor_SceneAuto[] =
950 static struct reginfo sensor_SceneNight[] =
955 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
958 #if CONFIG_SENSOR_DigitalZoom
959 static struct reginfo sensor_Zoom0[] =
964 static struct reginfo sensor_Zoom1[] =
969 static struct reginfo sensor_Zoom2[] =
975 static struct reginfo sensor_Zoom3[] =
979 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
981 static const struct v4l2_querymenu sensor_menus[] =
983 #if CONFIG_SENSOR_WhiteBalance
984 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
985 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
986 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
989 #if CONFIG_SENSOR_Effect
990 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
991 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
992 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
995 #if CONFIG_SENSOR_Scene
996 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
999 #if CONFIG_SENSOR_Flash
1000 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1001 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1005 static const struct v4l2_queryctrl sensor_controls[] =
1007 #if CONFIG_SENSOR_WhiteBalance
1009 .id = V4L2_CID_DO_WHITE_BALANCE,
1010 .type = V4L2_CTRL_TYPE_MENU,
1011 .name = "White Balance Control",
1019 #if CONFIG_SENSOR_Brightness
1021 .id = V4L2_CID_BRIGHTNESS,
1022 .type = V4L2_CTRL_TYPE_INTEGER,
1023 .name = "Brightness Control",
1031 #if CONFIG_SENSOR_Effect
1033 .id = V4L2_CID_EFFECT,
1034 .type = V4L2_CTRL_TYPE_MENU,
1035 .name = "Effect Control",
1043 #if CONFIG_SENSOR_Exposure
1045 .id = V4L2_CID_EXPOSURE,
1046 .type = V4L2_CTRL_TYPE_INTEGER,
1047 .name = "Exposure Control",
1055 #if CONFIG_SENSOR_Saturation
1057 .id = V4L2_CID_SATURATION,
1058 .type = V4L2_CTRL_TYPE_INTEGER,
1059 .name = "Saturation Control",
1067 #if CONFIG_SENSOR_Contrast
1069 .id = V4L2_CID_CONTRAST,
1070 .type = V4L2_CTRL_TYPE_INTEGER,
1071 .name = "Contrast Control",
1079 #if CONFIG_SENSOR_Mirror
1081 .id = V4L2_CID_HFLIP,
1082 .type = V4L2_CTRL_TYPE_BOOLEAN,
1083 .name = "Mirror Control",
1091 #if CONFIG_SENSOR_Flip
1093 .id = V4L2_CID_VFLIP,
1094 .type = V4L2_CTRL_TYPE_BOOLEAN,
1095 .name = "Flip Control",
1103 #if CONFIG_SENSOR_Scene
1105 .id = V4L2_CID_SCENE,
1106 .type = V4L2_CTRL_TYPE_MENU,
1107 .name = "Scene Control",
1115 #if CONFIG_SENSOR_DigitalZoom
1117 .id = V4L2_CID_ZOOM_RELATIVE,
1118 .type = V4L2_CTRL_TYPE_INTEGER,
1119 .name = "DigitalZoom Control",
1125 .id = V4L2_CID_ZOOM_ABSOLUTE,
1126 .type = V4L2_CTRL_TYPE_INTEGER,
1127 .name = "DigitalZoom Control",
1135 #if CONFIG_SENSOR_Focus
1137 .id = V4L2_CID_FOCUS_RELATIVE,
1138 .type = V4L2_CTRL_TYPE_INTEGER,
1139 .name = "Focus Control",
1145 .id = V4L2_CID_FOCUS_ABSOLUTE,
1146 .type = V4L2_CTRL_TYPE_INTEGER,
1147 .name = "Focus Control",
1151 .default_value = 125,
1155 #if CONFIG_SENSOR_Flash
1157 .id = V4L2_CID_FLASH,
1158 .type = V4L2_CTRL_TYPE_MENU,
1159 .name = "Flash Control",
1168 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1169 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1170 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1171 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1172 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1173 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1174 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1175 static int sensor_resume(struct soc_camera_device *icd);
1176 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1177 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1178 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1179 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1180 static int sensor_deactivate(struct i2c_client *client);
1182 static struct soc_camera_ops sensor_ops =
1184 .suspend = sensor_suspend,
1185 .resume = sensor_resume,
1186 .set_bus_param = sensor_set_bus_param,
1187 .query_bus_param = sensor_query_bus_param,
1188 .controls = sensor_controls,
1189 .menus = sensor_menus,
1190 .num_controls = ARRAY_SIZE(sensor_controls),
1191 .num_menus = ARRAY_SIZE(sensor_menus),
1194 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1195 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1196 .colorspace = _colorspace }
1198 #define JPG_FMT(_name, _depth, _fourcc) \
1199 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1201 static const struct soc_camera_data_format sensor_colour_formats[] = {
1202 JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1203 //JPG_FMT(SENSOR_NAME_STRING(UYVY), 16, V4L2_PIX_FMT_UYVY),
1206 typedef struct sensor_info_priv_s
1220 unsigned char mirror; /* HFLIP */
1221 unsigned char flip; /* VFLIP */
1222 unsigned int winseqe_cur_addr;
1223 unsigned int pixfmt;
1225 } sensor_info_priv_t;
1229 struct v4l2_subdev subdev;
1230 struct i2c_client *client;
1231 sensor_info_priv_t info_priv;
1232 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1233 #if CONFIG_SENSOR_I2C_NOSCHED
1234 atomic_t tasklock_cnt;
1236 struct rk29camera_platform_data *sensor_io_request;
1237 struct rk29camera_gpio_res *sensor_gpio_res;
1240 static struct sensor* to_sensor(const struct i2c_client *client)
1242 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1245 static int sensor_task_lock(struct i2c_client *client, int lock)
1247 #if CONFIG_SENSOR_I2C_NOSCHED
1249 struct sensor *sensor = to_sensor(client);
1252 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1253 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1254 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1258 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1259 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1260 goto sensor_task_lock_err;
1265 atomic_add(1, &sensor->tasklock_cnt);
1267 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1268 atomic_sub(1, &sensor->tasklock_cnt);
1270 if (atomic_read(&sensor->tasklock_cnt) == 0)
1275 sensor_task_lock_err:
1283 /* sensor register write */
1284 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1288 struct i2c_msg msg[1];
1290 buf[0] = reg & 0xFF;
1293 msg->addr = client->addr;
1294 msg->flags = client->flags;
1296 msg->len = sizeof(buf);
1297 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1298 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1303 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1304 err = i2c_transfer(client->adapter, msg, 1);
1309 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1317 /* sensor register read */
1318 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1323 struct i2c_msg msg[2];
1325 //buf[0] = reg >> 8;
1327 buf[1] = reg & 0xFF;
1329 msg[0].addr = client->addr;
1330 msg[0].flags = client->flags;
1332 msg[0].len = sizeof(buf);
1333 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1334 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1336 msg[1].addr = client->addr;
1337 msg[1].flags = client->flags|I2C_M_RD;
1340 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1341 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1345 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1346 err = i2c_transfer(client->adapter, msg, 2);
1352 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1360 /* write a array of registers */
1362 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1367 //for(i=0; i < sizeof(sensor_init_data) / 2;i++)
1368 while((regarray[i].reg != 0) || (regarray[i].val != 0))
1370 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1373 SENSOR_TR("%s..write failed current i = %d\n", SENSOR_NAME_STRING(),i);
1382 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1387 while (regarray[i].reg != 0)
1389 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1392 SENSOR_TR("%s..write failed current i = %d\n", SENSOR_NAME_STRING(),i);
1395 err = sensor_read(client, regarray[i].reg, &val_read);
1396 SENSOR_TR("%s..reg[0x%x]=0x%x,0x%x\n", SENSOR_NAME_STRING(),regarray[i].reg, val_read, regarray[i].val);
1404 static int sensor_check_array(struct i2c_client *client, struct reginfo *regarray)
1411 SENSOR_DG("%s >>>>>>>>>>>>>>>>>>>>>>\n",__FUNCTION__);
1412 for(i=0;i<sizeof(sensor_init_data) / 2;i++)
1414 ret = sensor_read(client,regarray[i].reg,&value);
1417 SENSOR_TR("read value failed\n");
1420 if(regarray[i].val != value)
1422 SENSOR_DG("%s reg[0x%x] check err,writte :0x%x read:0x%x\n",__FUNCTION__,regarray[i].reg,regarray[i].val,value);
1430 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1432 struct soc_camera_link *icl = to_soc_camera_link(icd);
1435 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1438 case Sensor_PowerDown:
1440 if (icl->powerdown) {
1441 ret = icl->powerdown(icd->pdev, on);
1442 if (ret == RK29_CAM_IO_SUCCESS) {
1446 icl->reset(icd->pdev);
1448 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1450 goto sensor_power_end;
1457 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1458 struct sensor *sensor = to_sensor(client);
1460 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1461 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1467 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1475 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1477 struct i2c_client *client = sd->priv;
1478 struct soc_camera_device *icd = client->dev.platform_data;
1479 struct sensor *sensor = to_sensor(client);
1480 const struct v4l2_queryctrl *qctrl;
1483 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1485 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1487 goto sensor_INIT_ERR;
1491 if (sensor_task_lock(client,1)<0)
1492 goto sensor_INIT_ERR;
1493 /* ret = sensor_write(client, 0x12, 0x80);
1496 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1498 goto sensor_INIT_ERR;
1501 mdelay(5); */ //delay 5 microseconds
1503 ret = sensor_write_array(client, sensor_init_data);
1506 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1507 goto sensor_INIT_ERR;
1510 sensor_task_lock(client,0);
1511 //icd->user_width = SENSOR_INIT_WIDTH;
1512 //icd->user_height = SENSOR_INIT_HEIGHT;
1513 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1514 sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
1516 /* sensor sensor information for initialization */
1517 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1519 sensor->info_priv.whiteBalance = qctrl->default_value;
1520 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1522 sensor->info_priv.brightness = qctrl->default_value;
1523 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1525 sensor->info_priv.effect = qctrl->default_value;
1526 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1528 sensor->info_priv.exposure = qctrl->default_value;
1530 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1532 sensor->info_priv.saturation = qctrl->default_value;
1533 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1535 sensor->info_priv.contrast = qctrl->default_value;
1536 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1538 sensor->info_priv.mirror = qctrl->default_value;
1539 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1541 sensor->info_priv.flip = qctrl->default_value;
1542 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1544 sensor->info_priv.scene = qctrl->default_value;
1545 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1547 sensor->info_priv.digitalzoom = qctrl->default_value;
1549 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1550 #if CONFIG_SENSOR_Focus
1552 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1554 sensor->info_priv.focus = qctrl->default_value;
1557 #if CONFIG_SENSOR_Flash
1558 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1560 sensor->info_priv.flash = qctrl->default_value;
1563 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);
1567 sensor_task_lock(client,0);
1568 sensor_deactivate(client);
1572 static int sensor_deactivate(struct i2c_client *client)
1574 struct soc_camera_device *icd = client->dev.platform_data;
1577 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1579 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1580 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1582 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1583 icd->user_width = SENSOR_INIT_WIDTH;
1584 icd->user_height = SENSOR_INIT_HEIGHT;
1588 static struct reginfo sensor_power_down_sequence[]=
1592 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1595 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1597 if (pm_msg.event == PM_EVENT_SUSPEND) {
1598 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1599 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1601 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1604 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1606 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1611 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1617 static int sensor_resume(struct soc_camera_device *icd)
1621 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1623 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1627 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1633 static int sensor_set_bus_param(struct soc_camera_device *icd,
1634 unsigned long flags)
1640 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1642 struct soc_camera_link *icl = to_soc_camera_link(icd);
1643 unsigned long flags = SENSOR_BUS_PARAM;
1645 return soc_camera_apply_sensor_flags(icl, flags);
1648 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1650 struct i2c_client *client = sd->priv;
1651 struct soc_camera_device *icd = client->dev.platform_data;
1652 struct sensor *sensor = to_sensor(client);
1653 struct v4l2_pix_format *pix = &f->fmt.pix;
1655 pix->width = icd->user_width;
1656 pix->height = icd->user_height;
1657 pix->pixelformat = sensor->info_priv.pixfmt;
1658 pix->field = V4L2_FIELD_NONE;
1659 pix->colorspace = V4L2_COLORSPACE_JPEG;
1663 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
1667 if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
1669 } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
1671 } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
1673 } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
1675 } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
1680 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1684 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
1688 if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
1690 } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
1695 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
1698 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1700 struct i2c_client *client = sd->priv;
1701 struct sensor *sensor = to_sensor(client);
1702 struct v4l2_pix_format *pix = &f->fmt.pix;
1703 struct reginfo *winseqe_set_addr=NULL;
1704 int ret=0, set_w,set_h;
1706 if (sensor->info_priv.pixfmt != pix->pixelformat) {
1707 switch (pix->pixelformat)
1709 case V4L2_PIX_FMT_YUYV:
1711 winseqe_set_addr = sensor_ClrFmt_YUYV;
1714 case V4L2_PIX_FMT_UYVY:
1716 winseqe_set_addr = sensor_ClrFmt_UYVY;
1722 if (winseqe_set_addr != NULL) {
1723 sensor_write_array(client, winseqe_set_addr);
1724 sensor->info_priv.pixfmt = pix->pixelformat;
1726 SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1728 SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
1733 set_h = pix->height;
1735 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1737 winseqe_set_addr = sensor_qcif;
1741 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1743 winseqe_set_addr = sensor_qvga;
1747 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1749 winseqe_set_addr = sensor_cif;
1753 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1755 winseqe_set_addr = sensor_vga;
1759 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1761 winseqe_set_addr = sensor_svga;
1765 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1767 winseqe_set_addr = sensor_sxga;
1773 winseqe_set_addr = SENSOR_INIT_WINSEQADR; /* ddl@rock-chips.com : Sensor output smallest size if isn't support app */
1774 set_w = SENSOR_INIT_WIDTH;
1775 set_h = SENSOR_INIT_HEIGHT;
1776 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
1779 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1780 #if CONFIG_SENSOR_Flash
1781 if (sensor_fmt_capturechk(sd,f) == true) { /* ddl@rock-chips.com : Capture */
1782 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1783 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1784 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1786 } else { /* ddl@rock-chips.com : Video */
1787 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1788 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1789 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1793 ret |= sensor_write_array(client, winseqe_set_addr);
1795 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1796 #if CONFIG_SENSOR_Flash
1797 if (sensor_fmt_capturechk(sd,f) == true) {
1798 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1799 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1800 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1804 goto sensor_s_fmt_end;
1807 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1810 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1814 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1818 pix->height = set_h;
1824 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
1826 struct v4l2_pix_format *pix = &f->fmt.pix;
1827 bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
1828 pix->pixelformat == V4L2_PIX_FMT_YUYV;
1831 * With Bayer format enforce even side lengths, but let the user play
1832 * with the starting pixel
1835 if (pix->height > SENSOR_MAX_HEIGHT)
1836 pix->height = SENSOR_MAX_HEIGHT;
1837 else if (pix->height < SENSOR_MIN_HEIGHT)
1838 pix->height = SENSOR_MIN_HEIGHT;
1840 pix->height = ALIGN(pix->height, 2);
1842 if (pix->width > SENSOR_MAX_WIDTH)
1843 pix->width = SENSOR_MAX_WIDTH;
1844 else if (pix->width < SENSOR_MIN_WIDTH)
1845 pix->width = SENSOR_MIN_WIDTH;
1847 pix->width = ALIGN(pix->width, 2);
1852 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1854 struct i2c_client *client = sd->priv;
1856 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1859 if (id->match.addr != client->addr)
1862 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV9650 identifier */
1867 #if CONFIG_SENSOR_Brightness
1868 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1870 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1872 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1874 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1876 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1878 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1881 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1885 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1889 #if CONFIG_SENSOR_Effect
1890 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1892 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1894 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1896 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1898 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1900 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1903 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1907 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1911 #if CONFIG_SENSOR_Exposure
1912 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1914 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1916 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1918 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1920 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1922 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1925 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1929 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1933 #if CONFIG_SENSOR_Saturation
1934 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1936 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1938 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1940 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1942 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1944 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1947 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1951 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1955 #if CONFIG_SENSOR_Contrast
1956 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1958 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1960 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1962 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1964 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1966 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1969 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1973 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1977 #if CONFIG_SENSOR_Mirror
1978 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1980 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1982 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1984 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1986 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1988 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1991 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1995 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1999 #if CONFIG_SENSOR_Flip
2000 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2002 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2004 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2006 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2008 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2010 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2013 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2017 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2021 #if CONFIG_SENSOR_Scene
2022 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2024 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2026 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2028 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2030 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2032 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2035 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2039 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2043 #if CONFIG_SENSOR_WhiteBalance
2044 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2046 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2048 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2050 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2052 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2054 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2057 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2061 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2065 #if CONFIG_SENSOR_DigitalZoom
2066 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2068 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2069 struct sensor *sensor = to_sensor(client);
2070 const struct v4l2_queryctrl *qctrl_info;
2071 int digitalzoom_cur, digitalzoom_total;
2073 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2077 digitalzoom_cur = sensor->info_priv.digitalzoom;
2078 digitalzoom_total = qctrl_info->maximum;
2080 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2082 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2086 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2088 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2092 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2094 *value = digitalzoom_total - digitalzoom_cur;
2097 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2099 *value = 0 - digitalzoom_cur;
2102 digitalzoom_cur += *value;
2104 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2106 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2108 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2111 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2118 #if CONFIG_SENSOR_Flash
2119 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2121 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2122 if (value == 3) { /* ddl@rock-chips.com: torch */
2123 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2125 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2127 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2131 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2136 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2138 struct i2c_client *client = sd->priv;
2139 struct sensor *sensor = to_sensor(client);
2140 const struct v4l2_queryctrl *qctrl;
2142 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2146 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2152 case V4L2_CID_BRIGHTNESS:
2154 ctrl->value = sensor->info_priv.brightness;
2157 case V4L2_CID_SATURATION:
2159 ctrl->value = sensor->info_priv.saturation;
2162 case V4L2_CID_CONTRAST:
2164 ctrl->value = sensor->info_priv.contrast;
2167 case V4L2_CID_DO_WHITE_BALANCE:
2169 ctrl->value = sensor->info_priv.whiteBalance;
2172 case V4L2_CID_EXPOSURE:
2174 ctrl->value = sensor->info_priv.exposure;
2177 case V4L2_CID_HFLIP:
2179 ctrl->value = sensor->info_priv.mirror;
2182 case V4L2_CID_VFLIP:
2184 ctrl->value = sensor->info_priv.flip;
2195 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2197 struct i2c_client *client = sd->priv;
2198 struct sensor *sensor = to_sensor(client);
2199 struct soc_camera_device *icd = client->dev.platform_data;
2200 const struct v4l2_queryctrl *qctrl;
2203 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2207 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2213 #if CONFIG_SENSOR_Brightness
2214 case V4L2_CID_BRIGHTNESS:
2216 if (ctrl->value != sensor->info_priv.brightness)
2218 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2222 sensor->info_priv.brightness = ctrl->value;
2227 #if CONFIG_SENSOR_Exposure
2228 case V4L2_CID_EXPOSURE:
2230 if (ctrl->value != sensor->info_priv.exposure)
2232 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2236 sensor->info_priv.exposure = ctrl->value;
2241 #if CONFIG_SENSOR_Saturation
2242 case V4L2_CID_SATURATION:
2244 if (ctrl->value != sensor->info_priv.saturation)
2246 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2250 sensor->info_priv.saturation = ctrl->value;
2255 #if CONFIG_SENSOR_Contrast
2256 case V4L2_CID_CONTRAST:
2258 if (ctrl->value != sensor->info_priv.contrast)
2260 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2264 sensor->info_priv.contrast = ctrl->value;
2269 #if CONFIG_SENSOR_WhiteBalance
2270 case V4L2_CID_DO_WHITE_BALANCE:
2272 if (ctrl->value != sensor->info_priv.whiteBalance)
2274 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2278 sensor->info_priv.whiteBalance = ctrl->value;
2283 #if CONFIG_SENSOR_Mirror
2284 case V4L2_CID_HFLIP:
2286 if (ctrl->value != sensor->info_priv.mirror)
2288 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2290 sensor->info_priv.mirror = ctrl->value;
2295 #if CONFIG_SENSOR_Flip
2296 case V4L2_CID_VFLIP:
2298 if (ctrl->value != sensor->info_priv.flip)
2300 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2302 sensor->info_priv.flip = ctrl->value;
2313 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2315 const struct v4l2_queryctrl *qctrl;
2316 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2317 struct sensor *sensor = to_sensor(client);
2319 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2323 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2327 switch (ext_ctrl->id)
2329 case V4L2_CID_SCENE:
2331 ext_ctrl->value = sensor->info_priv.scene;
2334 case V4L2_CID_EFFECT:
2336 ext_ctrl->value = sensor->info_priv.effect;
2339 case V4L2_CID_ZOOM_ABSOLUTE:
2341 ext_ctrl->value = sensor->info_priv.digitalzoom;
2344 case V4L2_CID_ZOOM_RELATIVE:
2348 case V4L2_CID_FOCUS_ABSOLUTE:
2350 ext_ctrl->value = sensor->info_priv.focus;
2353 case V4L2_CID_FOCUS_RELATIVE:
2357 case V4L2_CID_FLASH:
2359 ext_ctrl->value = sensor->info_priv.flash;
2367 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2369 const struct v4l2_queryctrl *qctrl;
2370 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2371 struct sensor *sensor = to_sensor(client);
2374 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2378 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2383 switch (ext_ctrl->id)
2385 #if CONFIG_SENSOR_Scene
2386 case V4L2_CID_SCENE:
2388 if (ext_ctrl->value != sensor->info_priv.scene)
2390 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2392 sensor->info_priv.scene = ext_ctrl->value;
2397 #if CONFIG_SENSOR_Effect
2398 case V4L2_CID_EFFECT:
2400 if (ext_ctrl->value != sensor->info_priv.effect)
2402 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2404 sensor->info_priv.effect= ext_ctrl->value;
2409 #if CONFIG_SENSOR_DigitalZoom
2410 case V4L2_CID_ZOOM_ABSOLUTE:
2412 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2415 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2417 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2419 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2421 sensor->info_priv.digitalzoom += val_offset;
2423 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2428 case V4L2_CID_ZOOM_RELATIVE:
2430 if (ext_ctrl->value)
2432 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2434 sensor->info_priv.digitalzoom += ext_ctrl->value;
2436 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2441 #if CONFIG_SENSOR_Focus
2442 case V4L2_CID_FOCUS_ABSOLUTE:
2444 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2447 if (ext_ctrl->value != sensor->info_priv.focus)
2449 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2451 sensor->info_priv.focus += val_offset;
2456 case V4L2_CID_FOCUS_RELATIVE:
2458 if (ext_ctrl->value)
2460 sensor->info_priv.focus += ext_ctrl->value;
2462 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2467 #if CONFIG_SENSOR_Flash
2468 case V4L2_CID_FLASH:
2470 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2472 sensor->info_priv.flash = ext_ctrl->value;
2474 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2485 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2487 struct i2c_client *client = sd->priv;
2488 struct soc_camera_device *icd = client->dev.platform_data;
2489 int i, error_cnt=0, error_idx=-1;
2492 for (i=0; i<ext_ctrl->count; i++) {
2493 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2500 error_idx = ext_ctrl->count;
2502 if (error_idx != -1) {
2503 ext_ctrl->error_idx = error_idx;
2510 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2512 struct i2c_client *client = sd->priv;
2513 struct soc_camera_device *icd = client->dev.platform_data;
2514 int i, error_cnt=0, error_idx=-1;
2517 for (i=0; i<ext_ctrl->count; i++) {
2518 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2525 error_idx = ext_ctrl->count;
2527 if (error_idx != -1) {
2528 ext_ctrl->error_idx = error_idx;
2535 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2536 * this wasn't our capture interface, so, we wait for the right one */
2537 static int sensor_video_probe(struct soc_camera_device *icd,
2538 struct i2c_client *client)
2542 struct sensor *sensor = to_sensor(client);
2544 /* We must have a parent by now. And it cannot be a wrong one.
2545 * So this entire test is completely redundant. */
2546 if (!icd->dev.parent ||
2547 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2550 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2552 goto sensor_video_probe_err;
2556 /* ret = sensor_write(client, 0x12, 0x80);
2559 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2562 mdelay(50); *///delay 5 microseconds
2564 /* check if it is an sensor sensor */
2565 ret = sensor_read(client, 0x00, &pid);
2567 SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2569 goto sensor_video_probe_err;
2572 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2573 if (pid == SENSOR_ID) {
2574 sensor->model = SENSOR_V4L2_IDENT;
2576 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2578 goto sensor_video_probe_err;
2581 icd->formats = sensor_colour_formats;
2582 icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
2586 sensor_video_probe_err:
2591 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2593 struct i2c_client *client = sd->priv;
2594 struct soc_camera_device *icd = client->dev.platform_data;
2595 struct sensor *sensor = to_sensor(client);
2597 #if CONFIG_SENSOR_Flash
2601 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2604 case RK29_CAM_SUBDEV_DEACTIVATE:
2606 sensor_deactivate(client);
2610 case RK29_CAM_SUBDEV_IOREQUEST:
2612 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2613 if (sensor->sensor_io_request != NULL) {
2614 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2615 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2616 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2617 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2618 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2619 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2622 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2624 goto sensor_ioctl_end;
2626 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2628 #if CONFIG_SENSOR_Flash
2629 if (sensor->sensor_gpio_res) {
2630 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2631 for (i = 0; i < icd->ops->num_controls; i++) {
2632 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2633 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2636 sensor->info_priv.flash = 0xff;
2637 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2645 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2653 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2654 .init = sensor_init,
2655 .g_ctrl = sensor_g_control,
2656 .s_ctrl = sensor_s_control,
2657 .g_ext_ctrls = sensor_g_ext_controls,
2658 .s_ext_ctrls = sensor_s_ext_controls,
2659 .g_chip_ident = sensor_g_chip_ident,
2660 .ioctl = sensor_ioctl,
2663 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2664 .s_fmt = sensor_s_fmt,
2665 .g_fmt = sensor_g_fmt,
2666 .try_fmt = sensor_try_fmt,
2669 static struct v4l2_subdev_ops sensor_subdev_ops = {
2670 .core = &sensor_subdev_core_ops,
2671 .video = &sensor_subdev_video_ops,
2674 static int sensor_probe(struct i2c_client *client,
2675 const struct i2c_device_id *did)
2677 struct sensor *sensor;
2678 struct soc_camera_device *icd = client->dev.platform_data;
2679 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2680 struct soc_camera_link *icl;
2683 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2685 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2689 icl = to_soc_camera_link(icd);
2691 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2695 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2696 dev_warn(&adapter->dev,
2697 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2701 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2705 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2707 /* Second stage probe - when a capture adapter is there */
2708 icd->ops = &sensor_ops;
2709 icd->y_skip_top = 0;
2710 #if CONFIG_SENSOR_I2C_NOSCHED
2711 atomic_set(&sensor->tasklock_cnt,0);
2714 ret = sensor_video_probe(icd, client);
2717 i2c_set_clientdata(client, NULL);
2721 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2725 static int sensor_remove(struct i2c_client *client)
2727 struct sensor *sensor = to_sensor(client);
2728 struct soc_camera_device *icd = client->dev.platform_data;
2731 i2c_set_clientdata(client, NULL);
2732 client->driver = NULL;
2738 static const struct i2c_device_id sensor_id[] = {
2739 {SENSOR_NAME_STRING(), 0 },
2742 MODULE_DEVICE_TABLE(i2c, sensor_id);
2744 static struct i2c_driver sensor_i2c_driver = {
2746 .name = SENSOR_NAME_STRING(),
2748 .probe = sensor_probe,
2749 .remove = sensor_remove,
2750 .id_table = sensor_id,
2753 static int __init sensor_mod_init(void)
2755 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2756 return i2c_add_driver(&sensor_i2c_driver);
2759 static void __exit sensor_mod_exit(void)
2761 i2c_del_driver(&sensor_i2c_driver);
2764 device_initcall_sync(sensor_mod_init);
2765 module_exit(sensor_mod_exit);
2767 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2768 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2769 MODULE_LICENSE("GPL");