blob: 209ff97261a95814bf585e04bf957c160512f9c8 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030010#include <linux/videodev2.h>
11#include <linux/delay.h>
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -030012#include <asm/div64.h>
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030013#include <media/v4l2-device.h>
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030014#include <media/v4l2-chip-ident.h>
Hans Verkuil6ce58be2010-09-15 15:09:38 -030015#include "mt9v011.h"
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030016
17MODULE_DESCRIPTION("Micron mt9v011 sensor driver");
18MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
19MODULE_LICENSE("GPL");
20
21
22static int debug;
23module_param(debug, int, 0);
24MODULE_PARM_DESC(debug, "Debug level (0-2)");
25
26/* supported controls */
27static struct v4l2_queryctrl mt9v011_qctrl[] = {
28 {
29 .id = V4L2_CID_GAIN,
30 .type = V4L2_CTRL_TYPE_INTEGER,
31 .name = "Gain",
32 .minimum = 0,
33 .maximum = (1 << 10) - 1,
34 .step = 1,
35 .default_value = 0x0020,
36 .flags = 0,
37 }, {
38 .id = V4L2_CID_RED_BALANCE,
39 .type = V4L2_CTRL_TYPE_INTEGER,
40 .name = "Red Balance",
41 .minimum = -1 << 9,
42 .maximum = (1 << 9) - 1,
43 .step = 1,
44 .default_value = 0,
45 .flags = 0,
46 }, {
47 .id = V4L2_CID_BLUE_BALANCE,
48 .type = V4L2_CTRL_TYPE_INTEGER,
49 .name = "Blue Balance",
50 .minimum = -1 << 9,
51 .maximum = (1 << 9) - 1,
52 .step = 1,
53 .default_value = 0,
54 .flags = 0,
Mauro Carvalho Chehab2526ea62009-08-07 01:09:54 -030055 }, {
56 .id = V4L2_CID_HFLIP,
57 .type = V4L2_CTRL_TYPE_BOOLEAN,
58 .name = "Mirror",
59 .minimum = 0,
60 .maximum = 1,
61 .step = 1,
62 .default_value = 0,
63 .flags = 0,
64 }, {
65 .id = V4L2_CID_VFLIP,
66 .type = V4L2_CTRL_TYPE_BOOLEAN,
67 .name = "Vflip",
68 .minimum = 0,
69 .maximum = 1,
70 .step = 1,
71 .default_value = 0,
72 .flags = 0,
73 }, {
74 }
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030075};
76
77struct mt9v011 {
78 struct v4l2_subdev sd;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -030079 unsigned width, height;
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -030080 unsigned xtal;
Mauro Carvalho Chehab2526ea62009-08-07 01:09:54 -030081 unsigned hflip:1;
82 unsigned vflip:1;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030083
84 u16 global_gain, red_bal, blue_bal;
85};
86
87static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd)
88{
89 return container_of(sd, struct mt9v011, sd);
90}
91
92static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr)
93{
94 struct i2c_client *c = v4l2_get_subdevdata(sd);
95 __be16 buffer;
96 int rc, val;
97
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -030098 rc = i2c_master_send(c, &addr, 1);
99 if (rc != 1)
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300100 v4l2_dbg(0, debug, sd,
101 "i2c i/o error: rc == %d (should be 1)\n", rc);
102
103 msleep(10);
104
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -0300105 rc = i2c_master_recv(c, (char *)&buffer, 2);
106 if (rc != 2)
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300107 v4l2_dbg(0, debug, sd,
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -0300108 "i2c i/o error: rc == %d (should be 2)\n", rc);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300109
110 val = be16_to_cpu(buffer);
111
112 v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val);
113
114 return val;
115}
116
117static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr,
118 u16 value)
119{
120 struct i2c_client *c = v4l2_get_subdevdata(sd);
121 unsigned char buffer[3];
122 int rc;
123
124 buffer[0] = addr;
125 buffer[1] = value >> 8;
126 buffer[2] = value & 0xff;
127
128 v4l2_dbg(2, debug, sd,
129 "mt9v011: writing 0x%02x 0x%04x\n", buffer[0], value);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300130 rc = i2c_master_send(c, buffer, 3);
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -0300131 if (rc != 3)
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300132 v4l2_dbg(0, debug, sd,
133 "i2c i/o error: rc == %d (should be 3)\n", rc);
134}
135
136
137struct i2c_reg_value {
138 unsigned char reg;
139 u16 value;
140};
141
142/*
143 * Values used at the original driver
144 * Some values are marked as Reserved at the datasheet
145 */
146static const struct i2c_reg_value mt9v011_init_default[] = {
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300147 { R0D_MT9V011_RESET, 0x0001 },
148 { R0D_MT9V011_RESET, 0x0000 },
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300149
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300150 { R0C_MT9V011_SHUTTER_DELAY, 0x0000 },
Mauro Carvalho Chehab6934e6f2009-07-04 08:15:11 -0300151 { R09_MT9V011_SHUTTER_WIDTH, 0x1fc },
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300152
Mauro Carvalho Chehab6934e6f2009-07-04 08:15:11 -0300153 { R0A_MT9V011_CLK_SPEED, 0x0000 },
154 { R1E_MT9V011_DIGITAL_ZOOM, 0x0000 },
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300155
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -0300156 { R07_MT9V011_OUT_CTRL, 0x0002 }, /* chip enable */
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300157};
158
159static void set_balance(struct v4l2_subdev *sd)
160{
161 struct mt9v011 *core = to_mt9v011(sd);
162 u16 green1_gain, green2_gain, blue_gain, red_gain;
163
164 green1_gain = core->global_gain;
165 green2_gain = core->global_gain;
166
167 blue_gain = core->global_gain +
168 core->global_gain * core->blue_bal / (1 << 9);
169
170 red_gain = core->global_gain +
171 core->global_gain * core->blue_bal / (1 << 9);
172
173 mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green1_gain);
174 mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green1_gain);
175 mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain);
176 mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain);
177}
178
Mauro Carvalho Chehab83053f72009-08-06 21:03:35 -0300179static void calc_fps(struct v4l2_subdev *sd, u32 *numerator, u32 *denominator)
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -0300180{
181 struct mt9v011 *core = to_mt9v011(sd);
182 unsigned height, width, hblank, vblank, speed;
183 unsigned row_time, t_time;
184 u64 frames_per_ms;
185 unsigned tmp;
186
187 height = mt9v011_read(sd, R03_MT9V011_HEIGHT);
188 width = mt9v011_read(sd, R04_MT9V011_WIDTH);
189 hblank = mt9v011_read(sd, R05_MT9V011_HBLANK);
190 vblank = mt9v011_read(sd, R06_MT9V011_VBLANK);
191 speed = mt9v011_read(sd, R0A_MT9V011_CLK_SPEED);
192
193 row_time = (width + 113 + hblank) * (speed + 2);
194 t_time = row_time * (height + vblank + 1);
195
196 frames_per_ms = core->xtal * 1000l;
197 do_div(frames_per_ms, t_time);
198 tmp = frames_per_ms;
199
200 v4l2_dbg(1, debug, sd, "Programmed to %u.%03u fps (%d pixel clcks)\n",
201 tmp / 1000, tmp % 1000, t_time);
Mauro Carvalho Chehab83053f72009-08-06 21:03:35 -0300202
203 if (numerator && denominator) {
204 *numerator = 1000;
205 *denominator = (u32)frames_per_ms;
206 }
207}
208
209static u16 calc_speed(struct v4l2_subdev *sd, u32 numerator, u32 denominator)
210{
211 struct mt9v011 *core = to_mt9v011(sd);
212 unsigned height, width, hblank, vblank;
213 unsigned row_time, line_time;
214 u64 t_time, speed;
215
216 /* Avoid bogus calculus */
217 if (!numerator || !denominator)
218 return 0;
219
220 height = mt9v011_read(sd, R03_MT9V011_HEIGHT);
221 width = mt9v011_read(sd, R04_MT9V011_WIDTH);
222 hblank = mt9v011_read(sd, R05_MT9V011_HBLANK);
223 vblank = mt9v011_read(sd, R06_MT9V011_VBLANK);
224
225 row_time = width + 113 + hblank;
226 line_time = height + vblank + 1;
227
228 t_time = core->xtal * ((u64)numerator);
229 /* round to the closest value */
230 t_time += denominator / 2;
231 do_div(t_time, denominator);
232
233 speed = t_time;
234 do_div(speed, row_time * line_time);
235
236 /* Avoid having a negative value for speed */
237 if (speed < 2)
238 speed = 0;
239 else
240 speed -= 2;
241
242 /* Avoid speed overflow */
243 if (speed > 15)
244 return 15;
245
246 return (u16)speed;
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -0300247}
248
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300249static void set_res(struct v4l2_subdev *sd)
250{
251 struct mt9v011 *core = to_mt9v011(sd);
252 unsigned vstart, hstart;
253
254 /*
255 * The mt9v011 doesn't have scaling. So, in order to select the desired
256 * resolution, we're cropping at the middle of the sensor.
257 * hblank and vblank should be adjusted, in order to warrant that
258 * we'll preserve the line timings for 30 fps, no matter what resolution
259 * is selected.
Mauro Carvalho Chehab6934e6f2009-07-04 08:15:11 -0300260 * NOTE: datasheet says that width (and height) should be filled with
261 * width-1. However, this doesn't work, since one pixel per line will
262 * be missing.
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300263 */
264
265 hstart = 14 + (640 - core->width) / 2;
266 mt9v011_write(sd, R02_MT9V011_COLSTART, hstart);
267 mt9v011_write(sd, R04_MT9V011_WIDTH, core->width);
268 mt9v011_write(sd, R05_MT9V011_HBLANK, 771 - core->width);
269
Mauro Carvalho Chehabc1806042009-07-14 02:39:19 -0300270 vstart = 8 + (480 - core->height) / 2;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300271 mt9v011_write(sd, R01_MT9V011_ROWSTART, vstart);
272 mt9v011_write(sd, R03_MT9V011_HEIGHT, core->height);
273 mt9v011_write(sd, R06_MT9V011_VBLANK, 508 - core->height);
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -0300274
Mauro Carvalho Chehab83053f72009-08-06 21:03:35 -0300275 calc_fps(sd, NULL, NULL);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300276};
277
Mauro Carvalho Chehab2526ea62009-08-07 01:09:54 -0300278static void set_read_mode(struct v4l2_subdev *sd)
279{
280 struct mt9v011 *core = to_mt9v011(sd);
281 unsigned mode = 0x1000;
282
283 if (core->hflip)
284 mode |= 0x4000;
285
286 if (core->vflip)
287 mode |= 0x8000;
288
289 mt9v011_write(sd, R20_MT9V011_READ_MODE, mode);
290}
291
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300292static int mt9v011_reset(struct v4l2_subdev *sd, u32 val)
293{
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300294 int i;
295
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300296 for (i = 0; i < ARRAY_SIZE(mt9v011_init_default); i++)
297 mt9v011_write(sd, mt9v011_init_default[i].reg,
298 mt9v011_init_default[i].value);
299
300 set_balance(sd);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300301 set_res(sd);
Mauro Carvalho Chehab2526ea62009-08-07 01:09:54 -0300302 set_read_mode(sd);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300303
304 return 0;
305};
306
307static int mt9v011_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
308{
309 struct mt9v011 *core = to_mt9v011(sd);
310
311 v4l2_dbg(1, debug, sd, "g_ctrl called\n");
312
313 switch (ctrl->id) {
314 case V4L2_CID_GAIN:
315 ctrl->value = core->global_gain;
316 return 0;
317 case V4L2_CID_RED_BALANCE:
318 ctrl->value = core->red_bal;
319 return 0;
320 case V4L2_CID_BLUE_BALANCE:
321 ctrl->value = core->blue_bal;
322 return 0;
Mauro Carvalho Chehab2526ea62009-08-07 01:09:54 -0300323 case V4L2_CID_HFLIP:
324 ctrl->value = core->hflip ? 1 : 0;
325 return 0;
326 case V4L2_CID_VFLIP:
327 ctrl->value = core->vflip ? 1 : 0;
328 return 0;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300329 }
330 return -EINVAL;
331}
332
Mauro Carvalho Chehab98737402009-07-13 01:03:37 -0300333static int mt9v011_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
334{
335 int i;
336
337 v4l2_dbg(1, debug, sd, "queryctrl called\n");
338
339 for (i = 0; i < ARRAY_SIZE(mt9v011_qctrl); i++)
340 if (qc->id && qc->id == mt9v011_qctrl[i].id) {
341 memcpy(qc, &(mt9v011_qctrl[i]),
342 sizeof(*qc));
343 return 0;
344 }
345
346 return -EINVAL;
347}
348
349
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300350static int mt9v011_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
351{
352 struct mt9v011 *core = to_mt9v011(sd);
353 u8 i, n;
354 n = ARRAY_SIZE(mt9v011_qctrl);
355
356 for (i = 0; i < n; i++) {
357 if (ctrl->id != mt9v011_qctrl[i].id)
358 continue;
359 if (ctrl->value < mt9v011_qctrl[i].minimum ||
360 ctrl->value > mt9v011_qctrl[i].maximum)
361 return -ERANGE;
362 v4l2_dbg(1, debug, sd, "s_ctrl: id=%d, value=%d\n",
363 ctrl->id, ctrl->value);
364 break;
365 }
366
367 switch (ctrl->id) {
368 case V4L2_CID_GAIN:
369 core->global_gain = ctrl->value;
370 break;
371 case V4L2_CID_RED_BALANCE:
372 core->red_bal = ctrl->value;
373 break;
374 case V4L2_CID_BLUE_BALANCE:
375 core->blue_bal = ctrl->value;
376 break;
Mauro Carvalho Chehab2526ea62009-08-07 01:09:54 -0300377 case V4L2_CID_HFLIP:
378 core->hflip = ctrl->value;
379 set_read_mode(sd);
380 return 0;
381 case V4L2_CID_VFLIP:
382 core->vflip = ctrl->value;
383 set_read_mode(sd);
384 return 0;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300385 default:
386 return -EINVAL;
387 }
388
389 set_balance(sd);
390
391 return 0;
392}
393
Hans Verkuilea01b112010-05-09 10:19:25 -0300394static int mt9v011_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
395 enum v4l2_mbus_pixelcode *code)
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300396{
Hans Verkuilea01b112010-05-09 10:19:25 -0300397 if (index > 0)
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300398 return -EINVAL;
399
Hans Verkuilea01b112010-05-09 10:19:25 -0300400 *code = V4L2_MBUS_FMT_SGRBG8_1X8;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300401 return 0;
402}
403
Hans Verkuilea01b112010-05-09 10:19:25 -0300404static int mt9v011_try_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt)
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300405{
Hans Verkuilea01b112010-05-09 10:19:25 -0300406 if (fmt->code != V4L2_MBUS_FMT_SGRBG8_1X8)
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300407 return -EINVAL;
408
Hans Verkuilea01b112010-05-09 10:19:25 -0300409 v4l_bound_align_image(&fmt->width, 48, 639, 1,
410 &fmt->height, 32, 480, 1, 0);
411 fmt->field = V4L2_FIELD_NONE;
412 fmt->colorspace = V4L2_COLORSPACE_SRGB;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300413
414 return 0;
415}
416
Mauro Carvalho Chehab83053f72009-08-06 21:03:35 -0300417static int mt9v011_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
418{
419 struct v4l2_captureparm *cp = &parms->parm.capture;
420
421 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
422 return -EINVAL;
423
424 memset(cp, 0, sizeof(struct v4l2_captureparm));
425 cp->capability = V4L2_CAP_TIMEPERFRAME;
426 calc_fps(sd,
427 &cp->timeperframe.numerator,
428 &cp->timeperframe.denominator);
429
430 return 0;
431}
432
433static int mt9v011_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
434{
435 struct v4l2_captureparm *cp = &parms->parm.capture;
436 struct v4l2_fract *tpf = &cp->timeperframe;
437 u16 speed;
438
439 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
440 return -EINVAL;
441 if (cp->extendedmode != 0)
442 return -EINVAL;
443
444 speed = calc_speed(sd, tpf->numerator, tpf->denominator);
445
446 mt9v011_write(sd, R0A_MT9V011_CLK_SPEED, speed);
447 v4l2_dbg(1, debug, sd, "Setting speed to %d\n", speed);
448
449 /* Recalculate and update fps info */
450 calc_fps(sd, &tpf->numerator, &tpf->denominator);
451
452 return 0;
453}
454
Hans Verkuilea01b112010-05-09 10:19:25 -0300455static int mt9v011_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt)
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300456{
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300457 struct mt9v011 *core = to_mt9v011(sd);
458 int rc;
459
Hans Verkuilea01b112010-05-09 10:19:25 -0300460 rc = mt9v011_try_mbus_fmt(sd, fmt);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300461 if (rc < 0)
462 return -EINVAL;
463
Hans Verkuilea01b112010-05-09 10:19:25 -0300464 core->width = fmt->width;
465 core->height = fmt->height;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300466
467 set_res(sd);
468
469 return 0;
470}
471
Mauro Carvalho Chehab2ea472f2009-07-14 03:14:21 -0300472static int mt9v011_s_config(struct v4l2_subdev *sd, int dumb, void *data)
473{
474 struct mt9v011 *core = to_mt9v011(sd);
475 unsigned *xtal = data;
476
477 v4l2_dbg(1, debug, sd, "s_config called\n");
478
479 if (xtal) {
480 core->xtal = *xtal;
481 v4l2_dbg(1, debug, sd, "xtal set to %d.%03d MHz\n",
482 *xtal / 1000000, (*xtal / 1000) % 1000);
483 }
484
485 return 0;
486}
487
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300488
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300489#ifdef CONFIG_VIDEO_ADV_DEBUG
490static int mt9v011_g_register(struct v4l2_subdev *sd,
491 struct v4l2_dbg_register *reg)
492{
493 struct i2c_client *client = v4l2_get_subdevdata(sd);
494
495 if (!v4l2_chip_match_i2c_client(client, &reg->match))
496 return -EINVAL;
497 if (!capable(CAP_SYS_ADMIN))
498 return -EPERM;
499
500 reg->val = mt9v011_read(sd, reg->reg & 0xff);
501 reg->size = 2;
502
503 return 0;
504}
505
506static int mt9v011_s_register(struct v4l2_subdev *sd,
507 struct v4l2_dbg_register *reg)
508{
509 struct i2c_client *client = v4l2_get_subdevdata(sd);
510
511 if (!v4l2_chip_match_i2c_client(client, &reg->match))
512 return -EINVAL;
513 if (!capable(CAP_SYS_ADMIN))
514 return -EPERM;
515
516 mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff);
517
518 return 0;
519}
520#endif
521
522static int mt9v011_g_chip_ident(struct v4l2_subdev *sd,
523 struct v4l2_dbg_chip_ident *chip)
524{
Mauro Carvalho Chehab296544e2009-07-27 08:24:29 -0300525 u16 version;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300526 struct i2c_client *client = v4l2_get_subdevdata(sd);
527
Mauro Carvalho Chehab296544e2009-07-27 08:24:29 -0300528 version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION);
529
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300530 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_MT9V011,
Mauro Carvalho Chehab296544e2009-07-27 08:24:29 -0300531 version);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300532}
533
534static const struct v4l2_subdev_core_ops mt9v011_core_ops = {
Mauro Carvalho Chehab98737402009-07-13 01:03:37 -0300535 .queryctrl = mt9v011_queryctrl,
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300536 .g_ctrl = mt9v011_g_ctrl,
537 .s_ctrl = mt9v011_s_ctrl,
538 .reset = mt9v011_reset,
Mauro Carvalho Chehab2ea472f2009-07-14 03:14:21 -0300539 .s_config = mt9v011_s_config,
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300540 .g_chip_ident = mt9v011_g_chip_ident,
541#ifdef CONFIG_VIDEO_ADV_DEBUG
542 .g_register = mt9v011_g_register,
543 .s_register = mt9v011_s_register,
544#endif
545};
546
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300547static const struct v4l2_subdev_video_ops mt9v011_video_ops = {
Hans Verkuilea01b112010-05-09 10:19:25 -0300548 .enum_mbus_fmt = mt9v011_enum_mbus_fmt,
549 .try_mbus_fmt = mt9v011_try_mbus_fmt,
550 .s_mbus_fmt = mt9v011_s_mbus_fmt,
Mauro Carvalho Chehab83053f72009-08-06 21:03:35 -0300551 .g_parm = mt9v011_g_parm,
552 .s_parm = mt9v011_s_parm,
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300553};
554
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300555static const struct v4l2_subdev_ops mt9v011_ops = {
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300556 .core = &mt9v011_core_ops,
557 .video = &mt9v011_video_ops,
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300558};
559
560
561/****************************************************************************
562 I2C Client & Driver
563 ****************************************************************************/
564
565static int mt9v011_probe(struct i2c_client *c,
566 const struct i2c_device_id *id)
567{
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300568 u16 version;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300569 struct mt9v011 *core;
570 struct v4l2_subdev *sd;
571
572 /* Check if the adapter supports the needed features */
573 if (!i2c_check_functionality(c->adapter,
574 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
575 return -EIO;
576
577 core = kzalloc(sizeof(struct mt9v011), GFP_KERNEL);
578 if (!core)
579 return -ENOMEM;
580
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300581 sd = &core->sd;
582 v4l2_i2c_subdev_init(sd, c, &mt9v011_ops);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300583
584 /* Check if the sensor is really a MT9V011 */
585 version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION);
Mauro Carvalho Chehab296544e2009-07-27 08:24:29 -0300586 if ((version != MT9V011_VERSION) &&
587 (version != MT9V011_REV_B_VERSION)) {
588 v4l2_info(sd, "*** unknown micron chip detected (0x%04x).\n",
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300589 version);
590 kfree(core);
591 return -EINVAL;
592 }
593
594 core->global_gain = 0x0024;
595 core->width = 640;
596 core->height = 480;
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -0300597 core->xtal = 27000000; /* Hz */
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300598
Mauro Carvalho Chehab296544e2009-07-27 08:24:29 -0300599 v4l_info(c, "chip found @ 0x%02x (%s - chip version 0x%04x)\n",
600 c->addr << 1, c->adapter->name, version);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300601
602 return 0;
603}
604
605static int mt9v011_remove(struct i2c_client *c)
606{
607 struct v4l2_subdev *sd = i2c_get_clientdata(c);
608
609 v4l2_dbg(1, debug, sd,
610 "mt9v011.c: removing mt9v011 adapter on address 0x%x\n",
611 c->addr << 1);
612
613 v4l2_device_unregister_subdev(sd);
614 kfree(to_mt9v011(sd));
615 return 0;
616}
617
618/* ----------------------------------------------------------------------- */
619
620static const struct i2c_device_id mt9v011_id[] = {
621 { "mt9v011", 0 },
622 { }
623};
624MODULE_DEVICE_TABLE(i2c, mt9v011_id);
625
Hans Verkuil6ce58be2010-09-15 15:09:38 -0300626static struct i2c_driver mt9v011_driver = {
627 .driver = {
628 .owner = THIS_MODULE,
629 .name = "mt9v011",
630 },
631 .probe = mt9v011_probe,
632 .remove = mt9v011_remove,
633 .id_table = mt9v011_id,
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300634};
Hans Verkuil6ce58be2010-09-15 15:09:38 -0300635
636static __init int init_mt9v011(void)
637{
638 return i2c_add_driver(&mt9v011_driver);
639}
640
641static __exit void exit_mt9v011(void)
642{
643 i2c_del_driver(&mt9v011_driver);
644}
645
646module_init(init_mt9v011);
647module_exit(exit_mt9v011);