blob: 241382f5ba21069fd63dead570d3710f233a16e1 [file] [log] [blame]
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -03001/*
2 * mt9v011 -Micron 1/4-Inch VGA Digital Image Sensor
3 *
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
6 */
7
8#include <linux/i2c.h>
9#include <linux/videodev2.h>
10#include <linux/delay.h>
11#include <media/v4l2-device.h>
12#include "mt9v011.h"
13#include <media/v4l2-i2c-drv.h>
14#include <media/v4l2-chip-ident.h>
15
16MODULE_DESCRIPTION("Micron mt9v011 sensor driver");
17MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
18MODULE_LICENSE("GPL");
19
20
21static int debug;
22module_param(debug, int, 0);
23MODULE_PARM_DESC(debug, "Debug level (0-2)");
24
25/* supported controls */
26static struct v4l2_queryctrl mt9v011_qctrl[] = {
27 {
28 .id = V4L2_CID_GAIN,
29 .type = V4L2_CTRL_TYPE_INTEGER,
30 .name = "Gain",
31 .minimum = 0,
32 .maximum = (1 << 10) - 1,
33 .step = 1,
34 .default_value = 0x0020,
35 .flags = 0,
36 }, {
37 .id = V4L2_CID_RED_BALANCE,
38 .type = V4L2_CTRL_TYPE_INTEGER,
39 .name = "Red Balance",
40 .minimum = -1 << 9,
41 .maximum = (1 << 9) - 1,
42 .step = 1,
43 .default_value = 0,
44 .flags = 0,
45 }, {
46 .id = V4L2_CID_BLUE_BALANCE,
47 .type = V4L2_CTRL_TYPE_INTEGER,
48 .name = "Blue Balance",
49 .minimum = -1 << 9,
50 .maximum = (1 << 9) - 1,
51 .step = 1,
52 .default_value = 0,
53 .flags = 0,
54 },
55};
56
57struct mt9v011 {
58 struct v4l2_subdev sd;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -030059 unsigned width, height;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030060
61 u16 global_gain, red_bal, blue_bal;
62};
63
64static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd)
65{
66 return container_of(sd, struct mt9v011, sd);
67}
68
69static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr)
70{
71 struct i2c_client *c = v4l2_get_subdevdata(sd);
72 __be16 buffer;
73 int rc, val;
74
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -030075 rc = i2c_master_send(c, &addr, 1);
76 if (rc != 1)
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030077 v4l2_dbg(0, debug, sd,
78 "i2c i/o error: rc == %d (should be 1)\n", rc);
79
80 msleep(10);
81
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -030082 rc = i2c_master_recv(c, (char *)&buffer, 2);
83 if (rc != 2)
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030084 v4l2_dbg(0, debug, sd,
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -030085 "i2c i/o error: rc == %d (should be 2)\n", rc);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030086
87 val = be16_to_cpu(buffer);
88
89 v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val);
90
91 return val;
92}
93
94static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr,
95 u16 value)
96{
97 struct i2c_client *c = v4l2_get_subdevdata(sd);
98 unsigned char buffer[3];
99 int rc;
100
101 buffer[0] = addr;
102 buffer[1] = value >> 8;
103 buffer[2] = value & 0xff;
104
105 v4l2_dbg(2, debug, sd,
106 "mt9v011: writing 0x%02x 0x%04x\n", buffer[0], value);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300107 rc = i2c_master_send(c, buffer, 3);
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -0300108 if (rc != 3)
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300109 v4l2_dbg(0, debug, sd,
110 "i2c i/o error: rc == %d (should be 3)\n", rc);
111}
112
113
114struct i2c_reg_value {
115 unsigned char reg;
116 u16 value;
117};
118
119/*
120 * Values used at the original driver
121 * Some values are marked as Reserved at the datasheet
122 */
123static const struct i2c_reg_value mt9v011_init_default[] = {
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300124 { R0D_MT9V011_RESET, 0x0001 },
125 { R0D_MT9V011_RESET, 0x0000 },
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300126
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300127 { R0C_MT9V011_SHUTTER_DELAY, 0x0000 },
Mauro Carvalho Chehab6934e6f2009-07-04 08:15:11 -0300128 { R09_MT9V011_SHUTTER_WIDTH, 0x1fc },
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300129
Mauro Carvalho Chehab6934e6f2009-07-04 08:15:11 -0300130 { R0A_MT9V011_CLK_SPEED, 0x0000 },
131 { R1E_MT9V011_DIGITAL_ZOOM, 0x0000 },
132 { R20_MT9V011_READ_MODE, 0x1000 },
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300133
134 { R07_MT9V011_OUT_CTRL, 0x000a }, /* chip enable */
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300135};
136
137static void set_balance(struct v4l2_subdev *sd)
138{
139 struct mt9v011 *core = to_mt9v011(sd);
140 u16 green1_gain, green2_gain, blue_gain, red_gain;
141
142 green1_gain = core->global_gain;
143 green2_gain = core->global_gain;
144
145 blue_gain = core->global_gain +
146 core->global_gain * core->blue_bal / (1 << 9);
147
148 red_gain = core->global_gain +
149 core->global_gain * core->blue_bal / (1 << 9);
150
151 mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green1_gain);
152 mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green1_gain);
153 mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain);
154 mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain);
155}
156
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300157static void set_res(struct v4l2_subdev *sd)
158{
159 struct mt9v011 *core = to_mt9v011(sd);
160 unsigned vstart, hstart;
161
162 /*
163 * The mt9v011 doesn't have scaling. So, in order to select the desired
164 * resolution, we're cropping at the middle of the sensor.
165 * hblank and vblank should be adjusted, in order to warrant that
166 * we'll preserve the line timings for 30 fps, no matter what resolution
167 * is selected.
Mauro Carvalho Chehab6934e6f2009-07-04 08:15:11 -0300168 * NOTE: datasheet says that width (and height) should be filled with
169 * width-1. However, this doesn't work, since one pixel per line will
170 * be missing.
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300171 */
172
173 hstart = 14 + (640 - core->width) / 2;
174 mt9v011_write(sd, R02_MT9V011_COLSTART, hstart);
175 mt9v011_write(sd, R04_MT9V011_WIDTH, core->width);
176 mt9v011_write(sd, R05_MT9V011_HBLANK, 771 - core->width);
177
178 vstart = 8 + (640 - core->height) / 2;
179 mt9v011_write(sd, R01_MT9V011_ROWSTART, vstart);
180 mt9v011_write(sd, R03_MT9V011_HEIGHT, core->height);
181 mt9v011_write(sd, R06_MT9V011_VBLANK, 508 - core->height);
182};
183
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300184static int mt9v011_reset(struct v4l2_subdev *sd, u32 val)
185{
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300186 int i;
187
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300188 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);
191
192 set_balance(sd);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300193 set_res(sd);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300194
195 return 0;
196};
197
198static int mt9v011_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
199{
200 struct mt9v011 *core = to_mt9v011(sd);
201
202 v4l2_dbg(1, debug, sd, "g_ctrl called\n");
203
204 switch (ctrl->id) {
205 case V4L2_CID_GAIN:
206 ctrl->value = core->global_gain;
207 return 0;
208 case V4L2_CID_RED_BALANCE:
209 ctrl->value = core->red_bal;
210 return 0;
211 case V4L2_CID_BLUE_BALANCE:
212 ctrl->value = core->blue_bal;
213 return 0;
214 }
215 return -EINVAL;
216}
217
Mauro Carvalho Chehab98737402009-07-13 01:03:37 -0300218static int mt9v011_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
219{
220 int i;
221
222 v4l2_dbg(1, debug, sd, "queryctrl called\n");
223
224 for (i = 0; i < ARRAY_SIZE(mt9v011_qctrl); i++)
225 if (qc->id && qc->id == mt9v011_qctrl[i].id) {
226 memcpy(qc, &(mt9v011_qctrl[i]),
227 sizeof(*qc));
228 return 0;
229 }
230
231 return -EINVAL;
232}
233
234
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300235static int mt9v011_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
236{
237 struct mt9v011 *core = to_mt9v011(sd);
238 u8 i, n;
239 n = ARRAY_SIZE(mt9v011_qctrl);
240
241 for (i = 0; i < n; i++) {
242 if (ctrl->id != mt9v011_qctrl[i].id)
243 continue;
244 if (ctrl->value < mt9v011_qctrl[i].minimum ||
245 ctrl->value > mt9v011_qctrl[i].maximum)
246 return -ERANGE;
247 v4l2_dbg(1, debug, sd, "s_ctrl: id=%d, value=%d\n",
248 ctrl->id, ctrl->value);
249 break;
250 }
251
252 switch (ctrl->id) {
253 case V4L2_CID_GAIN:
254 core->global_gain = ctrl->value;
255 break;
256 case V4L2_CID_RED_BALANCE:
257 core->red_bal = ctrl->value;
258 break;
259 case V4L2_CID_BLUE_BALANCE:
260 core->blue_bal = ctrl->value;
261 break;
262 default:
263 return -EINVAL;
264 }
265
266 set_balance(sd);
267
268 return 0;
269}
270
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300271static int mt9v011_enum_fmt(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
272{
273 if (fmt->index > 0)
274 return -EINVAL;
275
276 fmt->flags = 0;
277 strcpy(fmt->description, "8 bpp Bayer GRGR..BGBG");
278 fmt->pixelformat = V4L2_PIX_FMT_SGRBG8;
279
280 return 0;
281}
282
283static int mt9v011_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
284{
285 struct v4l2_pix_format *pix = &fmt->fmt.pix;
286
287 if (pix->pixelformat != V4L2_PIX_FMT_SGRBG8)
288 return -EINVAL;
289
290 v4l_bound_align_image(&pix->width, 48, 639, 1,
291 &pix->height, 32, 480, 1, 0);
292
293 return 0;
294}
295
296static int mt9v011_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
297{
298 struct v4l2_pix_format *pix = &fmt->fmt.pix;
299 struct mt9v011 *core = to_mt9v011(sd);
300 int rc;
301
302 rc = mt9v011_try_fmt(sd, fmt);
303 if (rc < 0)
304 return -EINVAL;
305
306 core->width = pix->width;
307 core->height = pix->height;
308
309 set_res(sd);
310
311 return 0;
312}
313
314
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300315#ifdef CONFIG_VIDEO_ADV_DEBUG
316static int mt9v011_g_register(struct v4l2_subdev *sd,
317 struct v4l2_dbg_register *reg)
318{
319 struct i2c_client *client = v4l2_get_subdevdata(sd);
320
321 if (!v4l2_chip_match_i2c_client(client, &reg->match))
322 return -EINVAL;
323 if (!capable(CAP_SYS_ADMIN))
324 return -EPERM;
325
326 reg->val = mt9v011_read(sd, reg->reg & 0xff);
327 reg->size = 2;
328
329 return 0;
330}
331
332static int mt9v011_s_register(struct v4l2_subdev *sd,
333 struct v4l2_dbg_register *reg)
334{
335 struct i2c_client *client = v4l2_get_subdevdata(sd);
336
337 if (!v4l2_chip_match_i2c_client(client, &reg->match))
338 return -EINVAL;
339 if (!capable(CAP_SYS_ADMIN))
340 return -EPERM;
341
342 mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff);
343
344 return 0;
345}
346#endif
347
348static int mt9v011_g_chip_ident(struct v4l2_subdev *sd,
349 struct v4l2_dbg_chip_ident *chip)
350{
351 struct i2c_client *client = v4l2_get_subdevdata(sd);
352
353 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_MT9V011,
354 MT9V011_VERSION);
355}
356
357static const struct v4l2_subdev_core_ops mt9v011_core_ops = {
Mauro Carvalho Chehab98737402009-07-13 01:03:37 -0300358 .queryctrl = mt9v011_queryctrl,
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300359 .g_ctrl = mt9v011_g_ctrl,
360 .s_ctrl = mt9v011_s_ctrl,
361 .reset = mt9v011_reset,
362 .g_chip_ident = mt9v011_g_chip_ident,
363#ifdef CONFIG_VIDEO_ADV_DEBUG
364 .g_register = mt9v011_g_register,
365 .s_register = mt9v011_s_register,
366#endif
367};
368
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300369static const struct v4l2_subdev_video_ops mt9v011_video_ops = {
370 .enum_fmt = mt9v011_enum_fmt,
371 .try_fmt = mt9v011_try_fmt,
372 .s_fmt = mt9v011_s_fmt,
373};
374
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300375static const struct v4l2_subdev_ops mt9v011_ops = {
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300376 .core = &mt9v011_core_ops,
377 .video = &mt9v011_video_ops,
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300378};
379
380
381/****************************************************************************
382 I2C Client & Driver
383 ****************************************************************************/
384
385static int mt9v011_probe(struct i2c_client *c,
386 const struct i2c_device_id *id)
387{
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300388 u16 version;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300389 struct mt9v011 *core;
390 struct v4l2_subdev *sd;
391
392 /* Check if the adapter supports the needed features */
393 if (!i2c_check_functionality(c->adapter,
394 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
395 return -EIO;
396
397 core = kzalloc(sizeof(struct mt9v011), GFP_KERNEL);
398 if (!core)
399 return -ENOMEM;
400
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300401 sd = &core->sd;
402 v4l2_i2c_subdev_init(sd, c, &mt9v011_ops);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300403
404 /* Check if the sensor is really a MT9V011 */
405 version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION);
406 if (version != MT9V011_VERSION) {
407 v4l2_info(sd, "*** unknown micron chip detected (0x%04x.\n",
408 version);
409 kfree(core);
410 return -EINVAL;
411 }
412
413 core->global_gain = 0x0024;
414 core->width = 640;
415 core->height = 480;
416
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300417 v4l_info(c, "chip found @ 0x%02x (%s)\n",
418 c->addr << 1, c->adapter->name);
419
420 return 0;
421}
422
423static int mt9v011_remove(struct i2c_client *c)
424{
425 struct v4l2_subdev *sd = i2c_get_clientdata(c);
426
427 v4l2_dbg(1, debug, sd,
428 "mt9v011.c: removing mt9v011 adapter on address 0x%x\n",
429 c->addr << 1);
430
431 v4l2_device_unregister_subdev(sd);
432 kfree(to_mt9v011(sd));
433 return 0;
434}
435
436/* ----------------------------------------------------------------------- */
437
438static const struct i2c_device_id mt9v011_id[] = {
439 { "mt9v011", 0 },
440 { }
441};
442MODULE_DEVICE_TABLE(i2c, mt9v011_id);
443
444static struct v4l2_i2c_driver_data v4l2_i2c_data = {
445 .name = "mt9v011",
446 .probe = mt9v011_probe,
447 .remove = mt9v011_remove,
448 .id_table = mt9v011_id,
449};