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_MBUS_FMT_YUYV8_2X8
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)
93 #define SENSOR_AF_IS_ERR (0x00<<0)
94 #define SENSOR_AF_IS_OK (0x01<<0)
95 #define SENSOR_INIT_IS_ERR (0x00<<28)
96 #define SENSOR_INIT_IS_OK (0x01<<28)
104 /* init 640X480 VGA */
105 static struct reginfo sensor_init_data[] =
110 {0xfe , 0x00}, // set page0
112 {0xd2 , 0x10}, // close AEC
113 {0x22 , 0x55}, // close AWB
121 {0x22 , 0x57}, // Open AWB
128 {0xe2 , 0x00}, //anti-flicker step [11:8]
129 {0xe3 , 0x64}, //anti-flicker step [7:0]
131 {0xe4 , 0x02}, //exp level 1 16.67fps
133 {0xe6 , 0x03}, //exp level 2 12.5fps
135 {0xe8 , 0x04}, //exp level 3 8.33fps
137 {0xea , 0x09}, //exp level 4 4.00fps
177 {0x26 , 0x02}, //0x03 20101016 zhj
297 //---------------------------------------------------------------
298 {0xfe , 0x01},// set page1
350 {0xfe , 0x00}, // set page0
352 //-----------Update the registers 2010/07/06-------------//
354 {0xfe , 0x00}, // set page0
356 {0x11 , 0x0d}, // fd,modified by mormo 2010/07/06
357 {0x1a , 0x2a}, // 1e,modified by mormo 2010/07/06
359 {0x1c , 0x49}, // c1,modified by mormo 2010/07/06
360 {0x1d , 0x9a}, // 08,modified by mormo 2010/07/06
361 {0x1e , 0x61}, // 60,modified by mormo 2010/07/06
365 {0x50 , 0x14}, // 10,modified by mormo 2010/07/06
380 {0xb1 , 0x40}, // YCPT
385 {0xd0 , 0xcb}, // AECT c9,modifed by mormo 2010/07/06
386 {0xd3 , 0x48}, // 80,modified by mormor 2010/07/06
393 {0xfe , 0x01},// set page1
417 //-----------Update the registers end---------//
420 {0xfe , 0x00}, // set page0
424 //-----------GAMMA Select(3)---------------//
443 /*GC0308_GAMMA_Select,
444 1: //smallest gamma curve
520 5: //largest gamma curve
538 //-----------GAMMA Select End--------------//
543 //-------------H_V_Switch(4)---------------//
544 {0x14 , 0x12}, //0x10
557 4: // IMAGE_HV_MIRROR
560 //-------------H_V_Select End--------------//
565 static struct reginfo sensor_sxga[] =
571 static struct reginfo sensor_svga[] =
577 static struct reginfo sensor_vga[] =
589 static struct reginfo sensor_cif[] =
595 static struct reginfo sensor_qvga[] =
601 static struct reginfo sensor_qcif[] =
606 static struct reginfo sensor_ClrFmt_YUYV[]=
611 static struct reginfo sensor_ClrFmt_UYVY[]=
617 #if CONFIG_SENSOR_WhiteBalance
618 static struct reginfo sensor_WhiteB_Auto[]=
626 /* Cloudy Colour Temperature : 6500K - 8000K */
627 static struct reginfo sensor_WhiteB_Cloudy[]=
629 {0x22, 0x55}, // Disable AWB
635 /* ClearDay Colour Temperature : 5000K - 6500K */
636 static struct reginfo sensor_WhiteB_ClearDay[]=
639 {0x22, 0x55}, // Disable AWB
645 /* Office Colour Temperature : 3500K - 5000K */
646 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
649 {0x22, 0x55}, // Disable AWB
656 /* Home Colour Temperature : 2500K - 3500K */
657 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
660 {0x22, 0x55}, // Disable AWB
666 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
667 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
671 #if CONFIG_SENSOR_Brightness
672 static struct reginfo sensor_Brightness0[]=
679 static struct reginfo sensor_Brightness1[]=
686 static struct reginfo sensor_Brightness2[]=
693 static struct reginfo sensor_Brightness3[]=
700 static struct reginfo sensor_Brightness4[]=
707 static struct reginfo sensor_Brightness5[]=
713 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
714 sensor_Brightness4, sensor_Brightness5,NULL,
719 #if CONFIG_SENSOR_Effect
720 static struct reginfo sensor_Effect_Normal[] =
735 static struct reginfo sensor_Effect_WandB[] =
749 static struct reginfo sensor_Effect_Sepia[] =
763 static struct reginfo sensor_Effect_Negative[] =
777 static struct reginfo sensor_Effect_Bluish[] =
792 static struct reginfo sensor_Effect_Green[] =
806 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
807 sensor_Effect_Bluish, sensor_Effect_Green,NULL,
810 #if CONFIG_SENSOR_Exposure
811 static struct reginfo sensor_Exposure0[]=
817 static struct reginfo sensor_Exposure1[]=
823 static struct reginfo sensor_Exposure2[]=
829 static struct reginfo sensor_Exposure3[]=
835 static struct reginfo sensor_Exposure4[]=
841 static struct reginfo sensor_Exposure5[]=
847 static struct reginfo sensor_Exposure6[]=
853 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
854 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
857 #if CONFIG_SENSOR_Saturation
858 static struct reginfo sensor_Saturation0[]=
863 static struct reginfo sensor_Saturation1[]=
868 static struct reginfo sensor_Saturation2[]=
872 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
875 #if CONFIG_SENSOR_Contrast
876 static struct reginfo sensor_Contrast0[]=
882 static struct reginfo sensor_Contrast1[]=
888 static struct reginfo sensor_Contrast2[]=
894 static struct reginfo sensor_Contrast3[]=
900 static struct reginfo sensor_Contrast4[]=
907 static struct reginfo sensor_Contrast5[]=
913 static struct reginfo sensor_Contrast6[]=
918 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
919 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
923 #if CONFIG_SENSOR_Mirror
924 static struct reginfo sensor_MirrorOn[]=
929 static struct reginfo sensor_MirrorOff[]=
933 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
935 #if CONFIG_SENSOR_Flip
936 static struct reginfo sensor_FlipOn[]=
941 static struct reginfo sensor_FlipOff[]=
945 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
948 #if CONFIG_SENSOR_Scene
949 static struct reginfo sensor_SceneAuto[] =
955 static struct reginfo sensor_SceneNight[] =
960 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
963 #if CONFIG_SENSOR_DigitalZoom
964 static struct reginfo sensor_Zoom0[] =
969 static struct reginfo sensor_Zoom1[] =
974 static struct reginfo sensor_Zoom2[] =
980 static struct reginfo sensor_Zoom3[] =
984 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
986 static const struct v4l2_querymenu sensor_menus[] =
988 #if CONFIG_SENSOR_WhiteBalance
989 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 0, .name = "auto", .reserved = 0, }, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 1, .name = "incandescent", .reserved = 0,},
990 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 2, .name = "fluorescent", .reserved = 0,}, { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3, .name = "daylight", .reserved = 0,},
991 { .id = V4L2_CID_DO_WHITE_BALANCE, .index = 4, .name = "cloudy-daylight", .reserved = 0,},
994 #if CONFIG_SENSOR_Effect
995 { .id = V4L2_CID_EFFECT, .index = 0, .name = "none", .reserved = 0, }, { .id = V4L2_CID_EFFECT, .index = 1, .name = "mono", .reserved = 0,},
996 { .id = V4L2_CID_EFFECT, .index = 2, .name = "negative", .reserved = 0,}, { .id = V4L2_CID_EFFECT, .index = 3, .name = "sepia", .reserved = 0,},
997 { .id = V4L2_CID_EFFECT, .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT, .index = 5, .name = "aqua", .reserved = 0,},
1000 #if CONFIG_SENSOR_Scene
1001 { .id = V4L2_CID_SCENE, .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE, .index = 1, .name = "night", .reserved = 0,},
1004 #if CONFIG_SENSOR_Flash
1005 { .id = V4L2_CID_FLASH, .index = 0, .name = "off", .reserved = 0, }, { .id = V4L2_CID_FLASH, .index = 1, .name = "auto", .reserved = 0,},
1006 { .id = V4L2_CID_FLASH, .index = 2, .name = "on", .reserved = 0,}, { .id = V4L2_CID_FLASH, .index = 3, .name = "torch", .reserved = 0,},
1010 static const struct v4l2_queryctrl sensor_controls[] =
1012 #if CONFIG_SENSOR_WhiteBalance
1014 .id = V4L2_CID_DO_WHITE_BALANCE,
1015 .type = V4L2_CTRL_TYPE_MENU,
1016 .name = "White Balance Control",
1024 #if CONFIG_SENSOR_Brightness
1026 .id = V4L2_CID_BRIGHTNESS,
1027 .type = V4L2_CTRL_TYPE_INTEGER,
1028 .name = "Brightness Control",
1036 #if CONFIG_SENSOR_Effect
1038 .id = V4L2_CID_EFFECT,
1039 .type = V4L2_CTRL_TYPE_MENU,
1040 .name = "Effect Control",
1048 #if CONFIG_SENSOR_Exposure
1050 .id = V4L2_CID_EXPOSURE,
1051 .type = V4L2_CTRL_TYPE_INTEGER,
1052 .name = "Exposure Control",
1060 #if CONFIG_SENSOR_Saturation
1062 .id = V4L2_CID_SATURATION,
1063 .type = V4L2_CTRL_TYPE_INTEGER,
1064 .name = "Saturation Control",
1072 #if CONFIG_SENSOR_Contrast
1074 .id = V4L2_CID_CONTRAST,
1075 .type = V4L2_CTRL_TYPE_INTEGER,
1076 .name = "Contrast Control",
1084 #if CONFIG_SENSOR_Mirror
1086 .id = V4L2_CID_HFLIP,
1087 .type = V4L2_CTRL_TYPE_BOOLEAN,
1088 .name = "Mirror Control",
1096 #if CONFIG_SENSOR_Flip
1098 .id = V4L2_CID_VFLIP,
1099 .type = V4L2_CTRL_TYPE_BOOLEAN,
1100 .name = "Flip Control",
1108 #if CONFIG_SENSOR_Scene
1110 .id = V4L2_CID_SCENE,
1111 .type = V4L2_CTRL_TYPE_MENU,
1112 .name = "Scene Control",
1120 #if CONFIG_SENSOR_DigitalZoom
1122 .id = V4L2_CID_ZOOM_RELATIVE,
1123 .type = V4L2_CTRL_TYPE_INTEGER,
1124 .name = "DigitalZoom Control",
1130 .id = V4L2_CID_ZOOM_ABSOLUTE,
1131 .type = V4L2_CTRL_TYPE_INTEGER,
1132 .name = "DigitalZoom Control",
1140 #if CONFIG_SENSOR_Focus
1142 .id = V4L2_CID_FOCUS_RELATIVE,
1143 .type = V4L2_CTRL_TYPE_INTEGER,
1144 .name = "Focus Control",
1150 .id = V4L2_CID_FOCUS_ABSOLUTE,
1151 .type = V4L2_CTRL_TYPE_INTEGER,
1152 .name = "Focus Control",
1156 .default_value = 125,
1160 #if CONFIG_SENSOR_Flash
1162 .id = V4L2_CID_FLASH,
1163 .type = V4L2_CTRL_TYPE_MENU,
1164 .name = "Flash Control",
1173 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1174 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1175 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1176 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1177 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1178 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl);
1179 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1180 static int sensor_resume(struct soc_camera_device *icd);
1181 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1182 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1183 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1184 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1185 static int sensor_deactivate(struct i2c_client *client);
1187 static struct soc_camera_ops sensor_ops =
1189 .suspend = sensor_suspend,
1190 .resume = sensor_resume,
1191 .set_bus_param = sensor_set_bus_param,
1192 .query_bus_param = sensor_query_bus_param,
1193 .controls = sensor_controls,
1194 .menus = sensor_menus,
1195 .num_controls = ARRAY_SIZE(sensor_controls),
1196 .num_menus = ARRAY_SIZE(sensor_menus),
1199 /* only one fixed colorspace per pixelcode */
1200 struct sensor_datafmt {
1201 enum v4l2_mbus_pixelcode code;
1202 enum v4l2_colorspace colorspace;
1205 /* Find a data format by a pixel code in an array */
1206 static const struct sensor_datafmt *sensor_find_datafmt(
1207 enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1211 for (i = 0; i < n; i++)
1212 if (fmt[i].code == code)
1218 static const struct sensor_datafmt sensor_colour_fmts[] = {
1219 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}
1222 typedef struct sensor_info_priv_s
1236 unsigned char mirror; /* HFLIP */
1237 unsigned char flip; /* VFLIP */
1238 unsigned int winseqe_cur_addr;
1239 struct sensor_datafmt fmt;
1240 unsigned int funmodule_state;
1241 } sensor_info_priv_t;
1245 struct v4l2_subdev subdev;
1246 struct i2c_client *client;
1247 sensor_info_priv_t info_priv;
1248 int model; /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1249 #if CONFIG_SENSOR_I2C_NOSCHED
1250 atomic_t tasklock_cnt;
1252 struct rk29camera_platform_data *sensor_io_request;
1253 struct rk29camera_gpio_res *sensor_gpio_res;
1256 static struct sensor* to_sensor(const struct i2c_client *client)
1258 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1261 static int sensor_task_lock(struct i2c_client *client, int lock)
1263 #if CONFIG_SENSOR_I2C_NOSCHED
1265 struct sensor *sensor = to_sensor(client);
1268 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1269 while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1270 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1274 if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1275 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1276 goto sensor_task_lock_err;
1281 atomic_add(1, &sensor->tasklock_cnt);
1283 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1284 atomic_sub(1, &sensor->tasklock_cnt);
1286 if (atomic_read(&sensor->tasklock_cnt) == 0)
1291 sensor_task_lock_err:
1299 /* sensor register write */
1300 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1304 struct i2c_msg msg[1];
1306 buf[0] = reg & 0xFF;
1309 msg->addr = client->addr;
1310 msg->flags = client->flags;
1312 msg->len = sizeof(buf);
1313 msg->scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1314 msg->read_type = 0; /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1319 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1320 err = i2c_transfer(client->adapter, msg, 1);
1325 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1333 /* sensor register read */
1334 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1339 struct i2c_msg msg[2];
1341 //buf[0] = reg >> 8;
1343 buf[1] = reg & 0xFF;
1345 msg[0].addr = client->addr;
1346 msg[0].flags = client->flags;
1348 msg[0].len = sizeof(buf);
1349 msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1350 msg[0].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1352 msg[1].addr = client->addr;
1353 msg[1].flags = client->flags|I2C_M_RD;
1356 msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED; /* ddl@rock-chips.com : 100kHz */
1357 msg[1].read_type = 2; /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1361 while ((cnt-- > 0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
1362 err = i2c_transfer(client->adapter, msg, 2);
1368 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1376 /* write a array of registers */
1378 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1383 //for(i=0; i < sizeof(sensor_init_data) / 2;i++)
1384 while((regarray[i].reg != 0) || (regarray[i].val != 0))
1386 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1389 SENSOR_TR("%s..write failed current i = %d\n", SENSOR_NAME_STRING(),i);
1398 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1403 while (regarray[i].reg != 0)
1405 err = sensor_write(client, regarray[i].reg, regarray[i].val);
1408 SENSOR_TR("%s..write failed current i = %d\n", SENSOR_NAME_STRING(),i);
1411 err = sensor_read(client, regarray[i].reg, &val_read);
1412 SENSOR_TR("%s..reg[0x%x]=0x%x,0x%x\n", SENSOR_NAME_STRING(),regarray[i].reg, val_read, regarray[i].val);
1419 #if CONFIG_SENSOR_I2C_RDWRCHK
1420 static int sensor_check_array(struct i2c_client *client, struct reginfo *regarray)
1427 SENSOR_DG("%s >>>>>>>>>>>>>>>>>>>>>>\n",__FUNCTION__);
1428 for(i=0;i<sizeof(sensor_init_data) / 2;i++)
1430 ret = sensor_read(client,regarray[i].reg,&value);
1433 SENSOR_TR("read value failed\n");
1436 if(regarray[i].val != value)
1438 SENSOR_DG("%s reg[0x%x] check err,writte :0x%x read:0x%x\n",__FUNCTION__,regarray[i].reg,regarray[i].val,value);
1447 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1449 struct soc_camera_link *icl = to_soc_camera_link(icd);
1452 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1455 case Sensor_PowerDown:
1457 if (icl->powerdown) {
1458 ret = icl->powerdown(icd->pdev, on);
1459 if (ret == RK29_CAM_IO_SUCCESS) {
1463 icl->reset(icd->pdev);
1465 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1467 goto sensor_power_end;
1474 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1475 struct sensor *sensor = to_sensor(client);
1477 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1478 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1484 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1492 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1494 struct i2c_client *client = v4l2_get_subdevdata(sd);
1495 struct soc_camera_device *icd = client->dev.platform_data;
1496 struct sensor *sensor = to_sensor(client);
1497 const struct v4l2_queryctrl *qctrl;
1498 const struct sensor_datafmt *fmt;
1501 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1503 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1505 goto sensor_INIT_ERR;
1509 if (sensor_task_lock(client,1)<0)
1510 goto sensor_INIT_ERR;
1511 /* ret = sensor_write(client, 0x12, 0x80);
1514 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1516 goto sensor_INIT_ERR;
1519 mdelay(5); */ //delay 5 microseconds
1521 ret = sensor_write_array(client, sensor_init_data);
1524 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1525 goto sensor_INIT_ERR;
1528 sensor_task_lock(client,0);
1530 sensor->info_priv.winseqe_cur_addr = (int)SENSOR_INIT_WINSEQADR;
1531 fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1533 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1535 goto sensor_INIT_ERR;
1537 sensor->info_priv.fmt = *fmt;
1539 /* sensor sensor information for initialization */
1540 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1542 sensor->info_priv.whiteBalance = qctrl->default_value;
1543 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1545 sensor->info_priv.brightness = qctrl->default_value;
1546 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1548 sensor->info_priv.effect = qctrl->default_value;
1549 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1551 sensor->info_priv.exposure = qctrl->default_value;
1553 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1555 sensor->info_priv.saturation = qctrl->default_value;
1556 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1558 sensor->info_priv.contrast = qctrl->default_value;
1559 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1561 sensor->info_priv.mirror = qctrl->default_value;
1562 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1564 sensor->info_priv.flip = qctrl->default_value;
1565 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1567 sensor->info_priv.scene = qctrl->default_value;
1568 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1570 sensor->info_priv.digitalzoom = qctrl->default_value;
1572 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
1573 #if CONFIG_SENSOR_Focus
1575 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1577 sensor->info_priv.focus = qctrl->default_value;
1580 #if CONFIG_SENSOR_Flash
1581 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1583 sensor->info_priv.flash = qctrl->default_value;
1586 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);
1587 sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
1590 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1591 sensor_task_lock(client,0);
1592 sensor_deactivate(client);
1596 static int sensor_deactivate(struct i2c_client *client)
1598 struct soc_camera_device *icd = client->dev.platform_data;
1600 struct sensor *sensor = to_sensor(client);
1601 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1603 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1604 sensor_ioctrl(icd, Sensor_PowerDown, 1);
1607 /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1608 icd->user_width = SENSOR_INIT_WIDTH;
1609 icd->user_height = SENSOR_INIT_HEIGHT;
1610 sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1614 static struct reginfo sensor_power_down_sequence[]=
1618 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1621 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1623 if (pm_msg.event == PM_EVENT_SUSPEND) {
1624 SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1625 ret = sensor_write_array(client, sensor_power_down_sequence) ;
1627 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1630 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1632 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1637 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1643 static int sensor_resume(struct soc_camera_device *icd)
1647 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1649 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1653 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1659 static int sensor_set_bus_param(struct soc_camera_device *icd,
1660 unsigned long flags)
1666 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1668 struct soc_camera_link *icl = to_soc_camera_link(icd);
1669 unsigned long flags = SENSOR_BUS_PARAM;
1671 return soc_camera_apply_sensor_flags(icl, flags);
1674 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1676 struct i2c_client *client = v4l2_get_subdevdata(sd);
1677 struct soc_camera_device *icd = client->dev.platform_data;
1678 struct sensor *sensor = to_sensor(client);
1680 mf->width = icd->user_width;
1681 mf->height = icd->user_height;
1682 mf->code = sensor->info_priv.fmt.code;
1683 mf->colorspace = sensor->info_priv.fmt.colorspace;
1684 mf->field = V4L2_FIELD_NONE;
1688 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1692 if ((mf->width == 1024) && (mf->height == 768)) {
1694 } else if ((mf->width == 1280) && (mf->height == 1024)) {
1696 } else if ((mf->width == 1600) && (mf->height == 1200)) {
1698 } else if ((mf->width == 2048) && (mf->height == 1536)) {
1700 } else if ((mf->width == 2592) && (mf->height == 1944)) {
1705 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1709 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1713 if ((mf->width == 1280) && (mf->height == 720)) {
1715 } else if ((mf->width == 1920) && (mf->height == 1080)) {
1720 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1723 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1725 struct i2c_client *client = v4l2_get_subdevdata(sd);
1726 const struct sensor_datafmt *fmt;
1727 struct sensor *sensor = to_sensor(client);
1728 const struct v4l2_queryctrl *qctrl;
1729 struct soc_camera_device *icd = client->dev.platform_data;
1730 struct reginfo *winseqe_set_addr=NULL;
1731 int ret=0, set_w,set_h;
1733 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1734 ARRAY_SIZE(sensor_colour_fmts));
1737 goto sensor_s_fmt_end;
1740 if (sensor->info_priv.fmt.code != mf->code) {
1743 case V4L2_MBUS_FMT_YUYV8_2X8:
1745 winseqe_set_addr = sensor_ClrFmt_YUYV;
1748 case V4L2_MBUS_FMT_UYVY8_2X8:
1750 winseqe_set_addr = sensor_ClrFmt_UYVY;
1756 if (winseqe_set_addr != NULL) {
1757 sensor_write_array(client, winseqe_set_addr);
1758 sensor->info_priv.fmt.code = mf->code;
1759 sensor->info_priv.fmt.colorspace= mf->colorspace;
1760 SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1762 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1769 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1771 winseqe_set_addr = sensor_qcif;
1775 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1777 winseqe_set_addr = sensor_qvga;
1781 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1783 winseqe_set_addr = sensor_cif;
1787 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1789 winseqe_set_addr = sensor_vga;
1793 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1795 winseqe_set_addr = sensor_svga;
1799 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1801 winseqe_set_addr = sensor_sxga;
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__,mf->width,mf->height);
1813 if ((int)winseqe_set_addr != sensor->info_priv.winseqe_cur_addr) {
1814 #if CONFIG_SENSOR_Flash
1815 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
1816 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1817 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1818 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1820 } else { /* ddl@rock-chips.com : Video */
1821 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1822 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1823 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1827 ret |= sensor_write_array(client, winseqe_set_addr);
1829 SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1830 #if CONFIG_SENSOR_Flash
1831 if (sensor_fmt_capturechk(sd,mf) == true) {
1832 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1833 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1834 SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1838 goto sensor_s_fmt_end;
1841 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
1843 if (sensor_fmt_capturechk(sd,mf) == true) { /* ddl@rock-chips.com : Capture */
1844 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1845 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1846 if (sensor->info_priv.whiteBalance != 0) {
1847 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1848 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1850 sensor->info_priv.snap2preview = true;
1851 } else if (sensor_fmt_videochk(sd,mf) == true) { /* ddl@rock-chips.com : Video */
1852 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1853 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1854 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1855 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1856 sensor->info_priv.video2preview = true;
1857 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1858 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1859 sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1860 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1861 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1863 sensor->info_priv.video2preview = false;
1864 sensor->info_priv.snap2preview = false;
1867 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1871 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1881 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1883 struct i2c_client *client = v4l2_get_subdevdata(sd);
1884 struct sensor *sensor = to_sensor(client);
1885 const struct sensor_datafmt *fmt;
1888 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1889 ARRAY_SIZE(sensor_colour_fmts));
1891 fmt = &sensor->info_priv.fmt;
1892 mf->code = fmt->code;
1895 if (mf->height > SENSOR_MAX_HEIGHT)
1896 mf->height = SENSOR_MAX_HEIGHT;
1897 else if (mf->height < SENSOR_MIN_HEIGHT)
1898 mf->height = SENSOR_MIN_HEIGHT;
1900 if (mf->width > SENSOR_MAX_WIDTH)
1901 mf->width = SENSOR_MAX_WIDTH;
1902 else if (mf->width < SENSOR_MIN_WIDTH)
1903 mf->width = SENSOR_MIN_WIDTH;
1905 mf->colorspace = fmt->colorspace;
1910 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1912 struct i2c_client *client = v4l2_get_subdevdata(sd);
1914 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1917 if (id->match.addr != client->addr)
1920 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV9650 identifier */
1925 #if CONFIG_SENSOR_Brightness
1926 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1928 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1930 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1932 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1934 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1936 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1939 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1943 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1947 #if CONFIG_SENSOR_Effect
1948 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1950 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1952 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1954 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1956 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1958 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1961 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1965 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1969 #if CONFIG_SENSOR_Exposure
1970 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1972 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1974 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1976 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1978 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1980 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1983 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1987 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1991 #if CONFIG_SENSOR_Saturation
1992 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1994 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1996 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1998 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2000 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2002 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2005 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2009 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2013 #if CONFIG_SENSOR_Contrast
2014 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2016 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2018 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2020 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2022 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2024 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2027 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2031 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2035 #if CONFIG_SENSOR_Mirror
2036 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2038 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2040 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2042 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2044 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2046 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2049 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2053 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2057 #if CONFIG_SENSOR_Flip
2058 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2060 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2062 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2064 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2066 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2068 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2071 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2075 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2079 #if CONFIG_SENSOR_Scene
2080 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2082 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2084 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2086 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2088 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2090 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2093 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2097 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2101 #if CONFIG_SENSOR_WhiteBalance
2102 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2104 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2106 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2108 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2110 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2112 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2115 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2119 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2123 #if CONFIG_SENSOR_DigitalZoom
2124 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2126 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2127 struct sensor *sensor = to_sensor(client);
2128 const struct v4l2_queryctrl *qctrl_info;
2129 int digitalzoom_cur, digitalzoom_total;
2131 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2135 digitalzoom_cur = sensor->info_priv.digitalzoom;
2136 digitalzoom_total = qctrl_info->maximum;
2138 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2140 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2144 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2146 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2150 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2152 *value = digitalzoom_total - digitalzoom_cur;
2155 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2157 *value = 0 - digitalzoom_cur;
2160 digitalzoom_cur += *value;
2162 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2164 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2166 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2169 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2176 #if CONFIG_SENSOR_Flash
2177 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2179 if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2180 if (value == 3) { /* ddl@rock-chips.com: torch */
2181 sensor_ioctrl(icd, Sensor_Flash, Flash_Torch); /* Flash On */
2183 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2185 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2189 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2194 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2196 struct i2c_client *client = v4l2_get_subdevdata(sd);
2197 struct sensor *sensor = to_sensor(client);
2198 const struct v4l2_queryctrl *qctrl;
2200 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2204 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2210 case V4L2_CID_BRIGHTNESS:
2212 ctrl->value = sensor->info_priv.brightness;
2215 case V4L2_CID_SATURATION:
2217 ctrl->value = sensor->info_priv.saturation;
2220 case V4L2_CID_CONTRAST:
2222 ctrl->value = sensor->info_priv.contrast;
2225 case V4L2_CID_DO_WHITE_BALANCE:
2227 ctrl->value = sensor->info_priv.whiteBalance;
2230 case V4L2_CID_EXPOSURE:
2232 ctrl->value = sensor->info_priv.exposure;
2235 case V4L2_CID_HFLIP:
2237 ctrl->value = sensor->info_priv.mirror;
2240 case V4L2_CID_VFLIP:
2242 ctrl->value = sensor->info_priv.flip;
2253 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2255 struct i2c_client *client = v4l2_get_subdevdata(sd);
2256 struct sensor *sensor = to_sensor(client);
2257 struct soc_camera_device *icd = client->dev.platform_data;
2258 const struct v4l2_queryctrl *qctrl;
2261 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2265 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2271 #if CONFIG_SENSOR_Brightness
2272 case V4L2_CID_BRIGHTNESS:
2274 if (ctrl->value != sensor->info_priv.brightness)
2276 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2280 sensor->info_priv.brightness = ctrl->value;
2285 #if CONFIG_SENSOR_Exposure
2286 case V4L2_CID_EXPOSURE:
2288 if (ctrl->value != sensor->info_priv.exposure)
2290 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2294 sensor->info_priv.exposure = ctrl->value;
2299 #if CONFIG_SENSOR_Saturation
2300 case V4L2_CID_SATURATION:
2302 if (ctrl->value != sensor->info_priv.saturation)
2304 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2308 sensor->info_priv.saturation = ctrl->value;
2313 #if CONFIG_SENSOR_Contrast
2314 case V4L2_CID_CONTRAST:
2316 if (ctrl->value != sensor->info_priv.contrast)
2318 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2322 sensor->info_priv.contrast = ctrl->value;
2327 #if CONFIG_SENSOR_WhiteBalance
2328 case V4L2_CID_DO_WHITE_BALANCE:
2330 if (ctrl->value != sensor->info_priv.whiteBalance)
2332 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2336 sensor->info_priv.whiteBalance = ctrl->value;
2341 #if CONFIG_SENSOR_Mirror
2342 case V4L2_CID_HFLIP:
2344 if (ctrl->value != sensor->info_priv.mirror)
2346 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2348 sensor->info_priv.mirror = ctrl->value;
2353 #if CONFIG_SENSOR_Flip
2354 case V4L2_CID_VFLIP:
2356 if (ctrl->value != sensor->info_priv.flip)
2358 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2360 sensor->info_priv.flip = ctrl->value;
2371 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2373 const struct v4l2_queryctrl *qctrl;
2374 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2375 struct sensor *sensor = to_sensor(client);
2377 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2381 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2385 switch (ext_ctrl->id)
2387 case V4L2_CID_SCENE:
2389 ext_ctrl->value = sensor->info_priv.scene;
2392 case V4L2_CID_EFFECT:
2394 ext_ctrl->value = sensor->info_priv.effect;
2397 case V4L2_CID_ZOOM_ABSOLUTE:
2399 ext_ctrl->value = sensor->info_priv.digitalzoom;
2402 case V4L2_CID_ZOOM_RELATIVE:
2406 case V4L2_CID_FOCUS_ABSOLUTE:
2408 ext_ctrl->value = sensor->info_priv.focus;
2411 case V4L2_CID_FOCUS_RELATIVE:
2415 case V4L2_CID_FLASH:
2417 ext_ctrl->value = sensor->info_priv.flash;
2425 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2427 const struct v4l2_queryctrl *qctrl;
2428 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2429 struct sensor *sensor = to_sensor(client);
2432 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2436 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2441 switch (ext_ctrl->id)
2443 #if CONFIG_SENSOR_Scene
2444 case V4L2_CID_SCENE:
2446 if (ext_ctrl->value != sensor->info_priv.scene)
2448 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2450 sensor->info_priv.scene = ext_ctrl->value;
2455 #if CONFIG_SENSOR_Effect
2456 case V4L2_CID_EFFECT:
2458 if (ext_ctrl->value != sensor->info_priv.effect)
2460 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2462 sensor->info_priv.effect= ext_ctrl->value;
2467 #if CONFIG_SENSOR_DigitalZoom
2468 case V4L2_CID_ZOOM_ABSOLUTE:
2470 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2473 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2475 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2477 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2479 sensor->info_priv.digitalzoom += val_offset;
2481 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2486 case V4L2_CID_ZOOM_RELATIVE:
2488 if (ext_ctrl->value)
2490 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2492 sensor->info_priv.digitalzoom += ext_ctrl->value;
2494 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2499 #if CONFIG_SENSOR_Focus
2500 case V4L2_CID_FOCUS_ABSOLUTE:
2502 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2505 if (ext_ctrl->value != sensor->info_priv.focus)
2507 val_offset = ext_ctrl->value -sensor->info_priv.focus;
2509 sensor->info_priv.focus += val_offset;
2514 case V4L2_CID_FOCUS_RELATIVE:
2516 if (ext_ctrl->value)
2518 sensor->info_priv.focus += ext_ctrl->value;
2520 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2525 #if CONFIG_SENSOR_Flash
2526 case V4L2_CID_FLASH:
2528 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2530 sensor->info_priv.flash = ext_ctrl->value;
2532 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2543 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2545 struct i2c_client *client = v4l2_get_subdevdata(sd);
2546 struct soc_camera_device *icd = client->dev.platform_data;
2547 int i, error_cnt=0, error_idx=-1;
2550 for (i=0; i<ext_ctrl->count; i++) {
2551 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2558 error_idx = ext_ctrl->count;
2560 if (error_idx != -1) {
2561 ext_ctrl->error_idx = error_idx;
2568 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2570 struct i2c_client *client = v4l2_get_subdevdata(sd);
2571 struct soc_camera_device *icd = client->dev.platform_data;
2572 int i, error_cnt=0, error_idx=-1;
2575 for (i=0; i<ext_ctrl->count; i++) {
2576 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2583 error_idx = ext_ctrl->count;
2585 if (error_idx != -1) {
2586 ext_ctrl->error_idx = error_idx;
2593 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2594 * this wasn't our capture interface, so, we wait for the right one */
2595 static int sensor_video_probe(struct soc_camera_device *icd,
2596 struct i2c_client *client)
2600 struct sensor *sensor = to_sensor(client);
2602 /* We must have a parent by now. And it cannot be a wrong one.
2603 * So this entire test is completely redundant. */
2604 if (!icd->dev.parent ||
2605 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2608 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2610 goto sensor_video_probe_err;
2614 /* ret = sensor_write(client, 0x12, 0x80);
2617 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2620 mdelay(50); *///delay 5 microseconds
2622 /* check if it is an sensor sensor */
2623 ret = sensor_read(client, 0x00, &pid);
2625 SENSOR_TR("%s read chip id high byte failed\n",SENSOR_NAME_STRING());
2627 goto sensor_video_probe_err;
2630 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2631 if (pid == SENSOR_ID) {
2632 sensor->model = SENSOR_V4L2_IDENT;
2634 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2636 goto sensor_video_probe_err;
2641 sensor_video_probe_err:
2646 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2648 struct i2c_client *client = v4l2_get_subdevdata(sd);
2649 struct soc_camera_device *icd = client->dev.platform_data;
2650 struct sensor *sensor = to_sensor(client);
2652 #if CONFIG_SENSOR_Flash
2656 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2659 case RK29_CAM_SUBDEV_DEACTIVATE:
2661 sensor_deactivate(client);
2665 case RK29_CAM_SUBDEV_IOREQUEST:
2667 sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;
2668 if (sensor->sensor_io_request != NULL) {
2669 if (sensor->sensor_io_request->gpio_res[0].dev_name &&
2670 (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2671 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2672 } else if (sensor->sensor_io_request->gpio_res[1].dev_name &&
2673 (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2674 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2677 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2679 goto sensor_ioctl_end;
2681 /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control
2683 #if CONFIG_SENSOR_Flash
2684 if (sensor->sensor_gpio_res) {
2685 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2686 for (i = 0; i < icd->ops->num_controls; i++) {
2687 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2688 memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));
2691 sensor->info_priv.flash = 0xff;
2692 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2700 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2708 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2709 enum v4l2_mbus_pixelcode *code)
2711 if (index >= ARRAY_SIZE(sensor_colour_fmts))
2714 *code = sensor_colour_fmts[index].code;
2717 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2718 .init = sensor_init,
2719 .g_ctrl = sensor_g_control,
2720 .s_ctrl = sensor_s_control,
2721 .g_ext_ctrls = sensor_g_ext_controls,
2722 .s_ext_ctrls = sensor_s_ext_controls,
2723 .g_chip_ident = sensor_g_chip_ident,
2724 .ioctl = sensor_ioctl,
2727 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2728 .s_mbus_fmt = sensor_s_fmt,
2729 .g_mbus_fmt = sensor_g_fmt,
2730 .try_mbus_fmt = sensor_try_fmt,
2731 .enum_mbus_fmt = sensor_enum_fmt,
2734 static struct v4l2_subdev_ops sensor_subdev_ops = {
2735 .core = &sensor_subdev_core_ops,
2736 .video = &sensor_subdev_video_ops,
2739 static int sensor_probe(struct i2c_client *client,
2740 const struct i2c_device_id *did)
2742 struct sensor *sensor;
2743 struct soc_camera_device *icd = client->dev.platform_data;
2744 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2745 struct soc_camera_link *icl;
2748 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2750 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2754 icl = to_soc_camera_link(icd);
2756 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2760 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2761 dev_warn(&adapter->dev,
2762 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2766 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2770 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2772 /* Second stage probe - when a capture adapter is there */
2773 icd->ops = &sensor_ops;
2775 sensor->info_priv.fmt = sensor_colour_fmts[0];
2777 #if CONFIG_SENSOR_I2C_NOSCHED
2778 atomic_set(&sensor->tasklock_cnt,0);
2781 ret = sensor_video_probe(icd, client);
2784 i2c_set_clientdata(client, NULL);
2788 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2792 static int sensor_remove(struct i2c_client *client)
2794 struct sensor *sensor = to_sensor(client);
2795 struct soc_camera_device *icd = client->dev.platform_data;
2798 i2c_set_clientdata(client, NULL);
2799 client->driver = NULL;
2805 static const struct i2c_device_id sensor_id[] = {
2806 {SENSOR_NAME_STRING(), 0 },
2809 MODULE_DEVICE_TABLE(i2c, sensor_id);
2811 static struct i2c_driver sensor_i2c_driver = {
2813 .name = SENSOR_NAME_STRING(),
2815 .probe = sensor_probe,
2816 .remove = sensor_remove,
2817 .id_table = sensor_id,
2820 static int __init sensor_mod_init(void)
2822 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2823 return i2c_add_driver(&sensor_i2c_driver);
2826 static void __exit sensor_mod_exit(void)
2828 i2c_del_driver(&sensor_i2c_driver);
2831 device_initcall_sync(sensor_mod_init);
2832 module_exit(sensor_mod_exit);
2834 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2835 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2836 MODULE_LICENSE("GPL");