2 * mt9v011 -Micron 1/4-Inch VGA Digital Image Sensor
4 * Copyright (c) 2009 Mauro Carvalho Chehab (mchehab@redhat.com)
5 * This code is placed under the terms of the GNU General Public License v2
9 #include <linux/videodev2.h>
10 #include <linux/delay.h>
11 #include <media/v4l2-device.h>
13 #include <media/v4l2-i2c-drv.h>
14 #include <media/v4l2-chip-ident.h>
16 MODULE_DESCRIPTION("Micron mt9v011 sensor driver");
17 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
18 MODULE_LICENSE("GPL");
22 module_param(debug, int, 0);
23 MODULE_PARM_DESC(debug, "Debug level (0-2)");
25 /* supported controls */
26 static struct v4l2_queryctrl mt9v011_qctrl[] = {
29 .type = V4L2_CTRL_TYPE_INTEGER,
32 .maximum = (1 << 10) - 1,
34 .default_value = 0x0020,
37 .id = V4L2_CID_RED_BALANCE,
38 .type = V4L2_CTRL_TYPE_INTEGER,
39 .name = "Red Balance",
41 .maximum = (1 << 9) - 1,
46 .id = V4L2_CID_BLUE_BALANCE,
47 .type = V4L2_CTRL_TYPE_INTEGER,
48 .name = "Blue Balance",
50 .maximum = (1 << 9) - 1,
58 struct v4l2_subdev sd;
60 u16 global_gain, red_bal, blue_bal;
63 static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd)
65 return container_of(sd, struct mt9v011, sd);
68 static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr)
70 struct i2c_client *c = v4l2_get_subdevdata(sd);
74 rc = i2c_master_send(c, &addr, 1);
76 v4l2_dbg(0, debug, sd,
77 "i2c i/o error: rc == %d (should be 1)\n", rc);
81 rc = i2c_master_recv(c, (char *)&buffer, 2);
83 v4l2_dbg(0, debug, sd,
84 "i2c i/o error: rc == %d (should be 2)\n", rc);
86 val = be16_to_cpu(buffer);
88 v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val);
93 static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr,
96 struct i2c_client *c = v4l2_get_subdevdata(sd);
97 unsigned char buffer[3];
101 buffer[1] = value >> 8;
102 buffer[2] = value & 0xff;
104 v4l2_dbg(2, debug, sd,
105 "mt9v011: writing 0x%02x 0x%04x\n", buffer[0], value);
106 rc = i2c_master_send(c, &buffer, 3);
108 v4l2_dbg(0, debug, sd,
109 "i2c i/o error: rc == %d (should be 3)\n", rc);
113 struct i2c_reg_value {
119 * Values used at the original driver
120 * Some values are marked as Reserved at the datasheet
122 static const struct i2c_reg_value mt9v011_init_default[] = {
123 { R0D_MT9V011_RESET, 0x0001 },
124 { R0D_MT9V011_RESET, 0x0000 },
126 { R01_MT9V011_ROWSTART, 0x0008 },
127 { R02_MT9V011_COLSTART, 0x0014 },
128 { R03_MT9V011_HEIGHT, 0x01e0 },
129 { R04_MT9V011_WIDTH, 0x0280 },
130 { R05_MT9V011_HBLANK, 0x007b },
131 { R06_MT9V011_VBLANK, 0x001c },
132 { R09_MT9V011_SHUTTER_WIDTH, 0x0418 },
133 { R0A_MT9V011_CLK_SPEED, 0x0000 },
134 { R0C_MT9V011_SHUTTER_DELAY, 0x0000 },
135 { R1E_MT9V011_DIGITAL_ZOOM, 0x0000 },
136 { R20_MT9V011_READ_MODE, 0x1100 },
139 * Those registers are not docummented at the datasheet.
140 * However, the original driver initializes them
147 { 0x58, 0x0038 }, /* Datasheet default 0x0078 */
148 { 0x59, 0x0723 }, /* Datasheet default 0x0703 */
149 { 0x62, 0x041a }, /* Datasheet default 0x0418 */
151 { R07_MT9V011_OUT_CTRL, 0x000a }, /* chip enable */
154 static void set_balance(struct v4l2_subdev *sd)
156 struct mt9v011 *core = to_mt9v011(sd);
157 u16 green1_gain, green2_gain, blue_gain, red_gain;
159 green1_gain = core->global_gain;
160 green2_gain = core->global_gain;
162 blue_gain = core->global_gain +
163 core->global_gain * core->blue_bal / (1 << 9);
165 red_gain = core->global_gain +
166 core->global_gain * core->blue_bal / (1 << 9);
168 mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green1_gain);
169 mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green1_gain);
170 mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain);
171 mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain);
174 static int mt9v011_reset(struct v4l2_subdev *sd, u32 val)
179 version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION);
181 if (version != MT9V011_VERSION) {
182 v4l2_info(sd, "*** unknown micron chip detected (0x%04x.\n",
188 for (i = 0; i < ARRAY_SIZE(mt9v011_init_default); i++)
189 mt9v011_write(sd, mt9v011_init_default[i].reg,
190 mt9v011_init_default[i].value);
197 static int mt9v011_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
199 struct mt9v011 *core = to_mt9v011(sd);
201 v4l2_dbg(1, debug, sd, "g_ctrl called\n");
205 ctrl->value = core->global_gain;
207 case V4L2_CID_RED_BALANCE:
208 ctrl->value = core->red_bal;
210 case V4L2_CID_BLUE_BALANCE:
211 ctrl->value = core->blue_bal;
217 static int mt9v011_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
219 struct mt9v011 *core = to_mt9v011(sd);
221 n = ARRAY_SIZE(mt9v011_qctrl);
223 for (i = 0; i < n; i++) {
224 if (ctrl->id != mt9v011_qctrl[i].id)
226 if (ctrl->value < mt9v011_qctrl[i].minimum ||
227 ctrl->value > mt9v011_qctrl[i].maximum)
229 v4l2_dbg(1, debug, sd, "s_ctrl: id=%d, value=%d\n",
230 ctrl->id, ctrl->value);
236 core->global_gain = ctrl->value;
238 case V4L2_CID_RED_BALANCE:
239 core->red_bal = ctrl->value;
241 case V4L2_CID_BLUE_BALANCE:
242 core->blue_bal = ctrl->value;
253 #ifdef CONFIG_VIDEO_ADV_DEBUG
254 static int mt9v011_g_register(struct v4l2_subdev *sd,
255 struct v4l2_dbg_register *reg)
257 struct i2c_client *client = v4l2_get_subdevdata(sd);
259 if (!v4l2_chip_match_i2c_client(client, ®->match))
261 if (!capable(CAP_SYS_ADMIN))
264 reg->val = mt9v011_read(sd, reg->reg & 0xff);
270 static int mt9v011_s_register(struct v4l2_subdev *sd,
271 struct v4l2_dbg_register *reg)
273 struct i2c_client *client = v4l2_get_subdevdata(sd);
275 if (!v4l2_chip_match_i2c_client(client, ®->match))
277 if (!capable(CAP_SYS_ADMIN))
280 mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff);
286 static int mt9v011_g_chip_ident(struct v4l2_subdev *sd,
287 struct v4l2_dbg_chip_ident *chip)
289 struct i2c_client *client = v4l2_get_subdevdata(sd);
291 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_MT9V011,
295 static const struct v4l2_subdev_core_ops mt9v011_core_ops = {
296 .g_ctrl = mt9v011_g_ctrl,
297 .s_ctrl = mt9v011_s_ctrl,
298 .reset = mt9v011_reset,
299 .g_chip_ident = mt9v011_g_chip_ident,
300 #ifdef CONFIG_VIDEO_ADV_DEBUG
301 .g_register = mt9v011_g_register,
302 .s_register = mt9v011_s_register,
306 static const struct v4l2_subdev_ops mt9v011_ops = {
307 .core = &mt9v011_core_ops,
311 /****************************************************************************
313 ****************************************************************************/
315 static int mt9v011_probe(struct i2c_client *c,
316 const struct i2c_device_id *id)
318 struct mt9v011 *core;
319 struct v4l2_subdev *sd;
321 /* Check if the adapter supports the needed features */
322 if (!i2c_check_functionality(c->adapter,
323 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
326 core = kzalloc(sizeof(struct mt9v011), GFP_KERNEL);
330 core->global_gain = 0x0024;
333 v4l2_i2c_subdev_init(sd, c, &mt9v011_ops);
334 v4l_info(c, "chip found @ 0x%02x (%s)\n",
335 c->addr << 1, c->adapter->name);
340 static int mt9v011_remove(struct i2c_client *c)
342 struct v4l2_subdev *sd = i2c_get_clientdata(c);
344 v4l2_dbg(1, debug, sd,
345 "mt9v011.c: removing mt9v011 adapter on address 0x%x\n",
348 v4l2_device_unregister_subdev(sd);
349 kfree(to_mt9v011(sd));
353 /* ----------------------------------------------------------------------- */
355 static const struct i2c_device_id mt9v011_id[] = {
359 MODULE_DEVICE_TABLE(i2c, mt9v011_id);
361 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
363 .probe = mt9v011_probe,
364 .remove = mt9v011_remove,
365 .id_table = mt9v011_id,