OMAP3: PM: decouple PER and CORE context save and restore
[linux-ginger.git] / drivers / media / video / mt9m001.c
blob45388d2ce2fdb5688498188dde080df2a874e826
1 /*
2 * Driver for MT9M001 CMOS Image Sensor from Micron
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
16 #include <media/v4l2-subdev.h>
17 #include <media/v4l2-chip-ident.h>
18 #include <media/soc_camera.h>
20 /* mt9m001 i2c address 0x5d
21 * The platform has to define ctruct i2c_board_info objects and link to them
22 * from struct soc_camera_link */
24 /* mt9m001 selected register addresses */
25 #define MT9M001_CHIP_VERSION 0x00
26 #define MT9M001_ROW_START 0x01
27 #define MT9M001_COLUMN_START 0x02
28 #define MT9M001_WINDOW_HEIGHT 0x03
29 #define MT9M001_WINDOW_WIDTH 0x04
30 #define MT9M001_HORIZONTAL_BLANKING 0x05
31 #define MT9M001_VERTICAL_BLANKING 0x06
32 #define MT9M001_OUTPUT_CONTROL 0x07
33 #define MT9M001_SHUTTER_WIDTH 0x09
34 #define MT9M001_FRAME_RESTART 0x0b
35 #define MT9M001_SHUTTER_DELAY 0x0c
36 #define MT9M001_RESET 0x0d
37 #define MT9M001_READ_OPTIONS1 0x1e
38 #define MT9M001_READ_OPTIONS2 0x20
39 #define MT9M001_GLOBAL_GAIN 0x35
40 #define MT9M001_CHIP_ENABLE 0xF1
42 #define MT9M001_MAX_WIDTH 1280
43 #define MT9M001_MAX_HEIGHT 1024
44 #define MT9M001_MIN_WIDTH 48
45 #define MT9M001_MIN_HEIGHT 32
46 #define MT9M001_COLUMN_SKIP 20
47 #define MT9M001_ROW_SKIP 12
49 static const struct soc_camera_data_format mt9m001_colour_formats[] = {
50 /* Order important: first natively supported,
51 * second supported with a GPIO extender */
53 .name = "Bayer (sRGB) 10 bit",
54 .depth = 10,
55 .fourcc = V4L2_PIX_FMT_SBGGR16,
56 .colorspace = V4L2_COLORSPACE_SRGB,
57 }, {
58 .name = "Bayer (sRGB) 8 bit",
59 .depth = 8,
60 .fourcc = V4L2_PIX_FMT_SBGGR8,
61 .colorspace = V4L2_COLORSPACE_SRGB,
65 static const struct soc_camera_data_format mt9m001_monochrome_formats[] = {
66 /* Order important - see above */
68 .name = "Monochrome 10 bit",
69 .depth = 10,
70 .fourcc = V4L2_PIX_FMT_Y16,
71 }, {
72 .name = "Monochrome 8 bit",
73 .depth = 8,
74 .fourcc = V4L2_PIX_FMT_GREY,
78 struct mt9m001 {
79 struct v4l2_subdev subdev;
80 struct v4l2_rect rect; /* Sensor window */
81 __u32 fourcc;
82 int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
83 unsigned int gain;
84 unsigned int exposure;
85 unsigned char autoexposure;
88 static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
90 return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
93 static int reg_read(struct i2c_client *client, const u8 reg)
95 s32 data = i2c_smbus_read_word_data(client, reg);
96 return data < 0 ? data : swab16(data);
99 static int reg_write(struct i2c_client *client, const u8 reg,
100 const u16 data)
102 return i2c_smbus_write_word_data(client, reg, swab16(data));
105 static int reg_set(struct i2c_client *client, const u8 reg,
106 const u16 data)
108 int ret;
110 ret = reg_read(client, reg);
111 if (ret < 0)
112 return ret;
113 return reg_write(client, reg, ret | data);
116 static int reg_clear(struct i2c_client *client, const u8 reg,
117 const u16 data)
119 int ret;
121 ret = reg_read(client, reg);
122 if (ret < 0)
123 return ret;
124 return reg_write(client, reg, ret & ~data);
127 static int mt9m001_init(struct i2c_client *client)
129 int ret;
131 dev_dbg(&client->dev, "%s\n", __func__);
134 * We don't know, whether platform provides reset, issue a soft reset
135 * too. This returns all registers to their default values.
137 ret = reg_write(client, MT9M001_RESET, 1);
138 if (!ret)
139 ret = reg_write(client, MT9M001_RESET, 0);
141 /* Disable chip, synchronous option update */
142 if (!ret)
143 ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
145 return ret;
148 static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
150 struct i2c_client *client = sd->priv;
152 /* Switch to master "normal" mode or stop sensor readout */
153 if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0)
154 return -EIO;
155 return 0;
158 static int mt9m001_set_bus_param(struct soc_camera_device *icd,
159 unsigned long flags)
161 struct soc_camera_link *icl = to_soc_camera_link(icd);
162 unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
164 /* Only one width bit may be set */
165 if (!is_power_of_2(width_flag))
166 return -EINVAL;
168 if (icl->set_bus_param)
169 return icl->set_bus_param(icl, width_flag);
172 * Without board specific bus width settings we only support the
173 * sensors native bus width
175 if (width_flag == SOCAM_DATAWIDTH_10)
176 return 0;
178 return -EINVAL;
181 static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
183 struct soc_camera_link *icl = to_soc_camera_link(icd);
184 /* MT9M001 has all capture_format parameters fixed */
185 unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING |
186 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
187 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER;
189 if (icl->query_bus_param)
190 flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
191 else
192 flags |= SOCAM_DATAWIDTH_10;
194 return soc_camera_apply_sensor_flags(icl, flags);
197 static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
199 struct i2c_client *client = sd->priv;
200 struct mt9m001 *mt9m001 = to_mt9m001(client);
201 struct v4l2_rect rect = a->c;
202 struct soc_camera_device *icd = client->dev.platform_data;
203 int ret;
204 const u16 hblank = 9, vblank = 25;
205 unsigned int total_h;
207 if (mt9m001->fourcc == V4L2_PIX_FMT_SBGGR8 ||
208 mt9m001->fourcc == V4L2_PIX_FMT_SBGGR16)
210 * Bayer format - even number of rows for simplicity,
211 * but let the user play with the top row.
213 rect.height = ALIGN(rect.height, 2);
215 /* Datasheet requirement: see register description */
216 rect.width = ALIGN(rect.width, 2);
217 rect.left = ALIGN(rect.left, 2);
219 soc_camera_limit_side(&rect.left, &rect.width,
220 MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH);
222 soc_camera_limit_side(&rect.top, &rect.height,
223 MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT);
225 total_h = rect.height + icd->y_skip_top + vblank;
227 /* Blanking and start values - default... */
228 ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
229 if (!ret)
230 ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
232 /* The caller provides a supported format, as verified per
233 * call to icd->try_fmt() */
234 if (!ret)
235 ret = reg_write(client, MT9M001_COLUMN_START, rect.left);
236 if (!ret)
237 ret = reg_write(client, MT9M001_ROW_START, rect.top);
238 if (!ret)
239 ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1);
240 if (!ret)
241 ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
242 rect.height + icd->y_skip_top - 1);
243 if (!ret && mt9m001->autoexposure) {
244 ret = reg_write(client, MT9M001_SHUTTER_WIDTH, total_h);
245 if (!ret) {
246 const struct v4l2_queryctrl *qctrl =
247 soc_camera_find_qctrl(icd->ops,
248 V4L2_CID_EXPOSURE);
249 mt9m001->exposure = (524 + (total_h - 1) *
250 (qctrl->maximum - qctrl->minimum)) /
251 1048 + qctrl->minimum;
255 if (!ret)
256 mt9m001->rect = rect;
258 return ret;
261 static int mt9m001_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
263 struct i2c_client *client = sd->priv;
264 struct mt9m001 *mt9m001 = to_mt9m001(client);
266 a->c = mt9m001->rect;
267 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
269 return 0;
272 static int mt9m001_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
274 a->bounds.left = MT9M001_COLUMN_SKIP;
275 a->bounds.top = MT9M001_ROW_SKIP;
276 a->bounds.width = MT9M001_MAX_WIDTH;
277 a->bounds.height = MT9M001_MAX_HEIGHT;
278 a->defrect = a->bounds;
279 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
280 a->pixelaspect.numerator = 1;
281 a->pixelaspect.denominator = 1;
283 return 0;
286 static int mt9m001_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
288 struct i2c_client *client = sd->priv;
289 struct mt9m001 *mt9m001 = to_mt9m001(client);
290 struct v4l2_pix_format *pix = &f->fmt.pix;
292 pix->width = mt9m001->rect.width;
293 pix->height = mt9m001->rect.height;
294 pix->pixelformat = mt9m001->fourcc;
295 pix->field = V4L2_FIELD_NONE;
296 pix->colorspace = V4L2_COLORSPACE_SRGB;
298 return 0;
301 static int mt9m001_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
303 struct i2c_client *client = sd->priv;
304 struct mt9m001 *mt9m001 = to_mt9m001(client);
305 struct v4l2_pix_format *pix = &f->fmt.pix;
306 struct v4l2_crop a = {
307 .c = {
308 .left = mt9m001->rect.left,
309 .top = mt9m001->rect.top,
310 .width = pix->width,
311 .height = pix->height,
314 int ret;
316 /* No support for scaling so far, just crop. TODO: use skipping */
317 ret = mt9m001_s_crop(sd, &a);
318 if (!ret) {
319 pix->width = mt9m001->rect.width;
320 pix->height = mt9m001->rect.height;
321 mt9m001->fourcc = pix->pixelformat;
324 return ret;
327 static int mt9m001_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
329 struct i2c_client *client = sd->priv;
330 struct soc_camera_device *icd = client->dev.platform_data;
331 struct v4l2_pix_format *pix = &f->fmt.pix;
333 v4l_bound_align_image(&pix->width, MT9M001_MIN_WIDTH,
334 MT9M001_MAX_WIDTH, 1,
335 &pix->height, MT9M001_MIN_HEIGHT + icd->y_skip_top,
336 MT9M001_MAX_HEIGHT + icd->y_skip_top, 0, 0);
338 if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8 ||
339 pix->pixelformat == V4L2_PIX_FMT_SBGGR16)
340 pix->height = ALIGN(pix->height - 1, 2);
342 return 0;
345 static int mt9m001_g_chip_ident(struct v4l2_subdev *sd,
346 struct v4l2_dbg_chip_ident *id)
348 struct i2c_client *client = sd->priv;
349 struct mt9m001 *mt9m001 = to_mt9m001(client);
351 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
352 return -EINVAL;
354 if (id->match.addr != client->addr)
355 return -ENODEV;
357 id->ident = mt9m001->model;
358 id->revision = 0;
360 return 0;
363 #ifdef CONFIG_VIDEO_ADV_DEBUG
364 static int mt9m001_g_register(struct v4l2_subdev *sd,
365 struct v4l2_dbg_register *reg)
367 struct i2c_client *client = sd->priv;
369 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
370 return -EINVAL;
372 if (reg->match.addr != client->addr)
373 return -ENODEV;
375 reg->size = 2;
376 reg->val = reg_read(client, reg->reg);
378 if (reg->val > 0xffff)
379 return -EIO;
381 return 0;
384 static int mt9m001_s_register(struct v4l2_subdev *sd,
385 struct v4l2_dbg_register *reg)
387 struct i2c_client *client = sd->priv;
389 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
390 return -EINVAL;
392 if (reg->match.addr != client->addr)
393 return -ENODEV;
395 if (reg_write(client, reg->reg, reg->val) < 0)
396 return -EIO;
398 return 0;
400 #endif
402 static const struct v4l2_queryctrl mt9m001_controls[] = {
404 .id = V4L2_CID_VFLIP,
405 .type = V4L2_CTRL_TYPE_BOOLEAN,
406 .name = "Flip Vertically",
407 .minimum = 0,
408 .maximum = 1,
409 .step = 1,
410 .default_value = 0,
411 }, {
412 .id = V4L2_CID_GAIN,
413 .type = V4L2_CTRL_TYPE_INTEGER,
414 .name = "Gain",
415 .minimum = 0,
416 .maximum = 127,
417 .step = 1,
418 .default_value = 64,
419 .flags = V4L2_CTRL_FLAG_SLIDER,
420 }, {
421 .id = V4L2_CID_EXPOSURE,
422 .type = V4L2_CTRL_TYPE_INTEGER,
423 .name = "Exposure",
424 .minimum = 1,
425 .maximum = 255,
426 .step = 1,
427 .default_value = 255,
428 .flags = V4L2_CTRL_FLAG_SLIDER,
429 }, {
430 .id = V4L2_CID_EXPOSURE_AUTO,
431 .type = V4L2_CTRL_TYPE_BOOLEAN,
432 .name = "Automatic Exposure",
433 .minimum = 0,
434 .maximum = 1,
435 .step = 1,
436 .default_value = 1,
440 static struct soc_camera_ops mt9m001_ops = {
441 .set_bus_param = mt9m001_set_bus_param,
442 .query_bus_param = mt9m001_query_bus_param,
443 .controls = mt9m001_controls,
444 .num_controls = ARRAY_SIZE(mt9m001_controls),
447 static int mt9m001_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
449 struct i2c_client *client = sd->priv;
450 struct mt9m001 *mt9m001 = to_mt9m001(client);
451 int data;
453 switch (ctrl->id) {
454 case V4L2_CID_VFLIP:
455 data = reg_read(client, MT9M001_READ_OPTIONS2);
456 if (data < 0)
457 return -EIO;
458 ctrl->value = !!(data & 0x8000);
459 break;
460 case V4L2_CID_EXPOSURE_AUTO:
461 ctrl->value = mt9m001->autoexposure;
462 break;
463 case V4L2_CID_GAIN:
464 ctrl->value = mt9m001->gain;
465 break;
466 case V4L2_CID_EXPOSURE:
467 ctrl->value = mt9m001->exposure;
468 break;
470 return 0;
473 static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
475 struct i2c_client *client = sd->priv;
476 struct mt9m001 *mt9m001 = to_mt9m001(client);
477 struct soc_camera_device *icd = client->dev.platform_data;
478 const struct v4l2_queryctrl *qctrl;
479 int data;
481 qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
483 if (!qctrl)
484 return -EINVAL;
486 switch (ctrl->id) {
487 case V4L2_CID_VFLIP:
488 if (ctrl->value)
489 data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
490 else
491 data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
492 if (data < 0)
493 return -EIO;
494 break;
495 case V4L2_CID_GAIN:
496 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
497 return -EINVAL;
498 /* See Datasheet Table 7, Gain settings. */
499 if (ctrl->value <= qctrl->default_value) {
500 /* Pack it into 0..1 step 0.125, register values 0..8 */
501 unsigned long range = qctrl->default_value - qctrl->minimum;
502 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
504 dev_dbg(&client->dev, "Setting gain %d\n", data);
505 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
506 if (data < 0)
507 return -EIO;
508 } else {
509 /* Pack it into 1.125..15 variable step, register values 9..67 */
510 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
511 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
512 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
513 111 + range / 2) / range + 9;
515 if (gain <= 32)
516 data = gain;
517 else if (gain <= 64)
518 data = ((gain - 32) * 16 + 16) / 32 + 80;
519 else
520 data = ((gain - 64) * 7 + 28) / 56 + 96;
522 dev_dbg(&client->dev, "Setting gain from %d to %d\n",
523 reg_read(client, MT9M001_GLOBAL_GAIN), data);
524 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
525 if (data < 0)
526 return -EIO;
529 /* Success */
530 mt9m001->gain = ctrl->value;
531 break;
532 case V4L2_CID_EXPOSURE:
533 /* mt9m001 has maximum == default */
534 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
535 return -EINVAL;
536 else {
537 unsigned long range = qctrl->maximum - qctrl->minimum;
538 unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
539 range / 2) / range + 1;
541 dev_dbg(&client->dev,
542 "Setting shutter width from %d to %lu\n",
543 reg_read(client, MT9M001_SHUTTER_WIDTH),
544 shutter);
545 if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
546 return -EIO;
547 mt9m001->exposure = ctrl->value;
548 mt9m001->autoexposure = 0;
550 break;
551 case V4L2_CID_EXPOSURE_AUTO:
552 if (ctrl->value) {
553 const u16 vblank = 25;
554 unsigned int total_h = mt9m001->rect.height +
555 icd->y_skip_top + vblank;
556 if (reg_write(client, MT9M001_SHUTTER_WIDTH,
557 total_h) < 0)
558 return -EIO;
559 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
560 mt9m001->exposure = (524 + (total_h - 1) *
561 (qctrl->maximum - qctrl->minimum)) /
562 1048 + qctrl->minimum;
563 mt9m001->autoexposure = 1;
564 } else
565 mt9m001->autoexposure = 0;
566 break;
568 return 0;
571 /* Interface active, can use i2c. If it fails, it can indeed mean, that
572 * this wasn't our capture interface, so, we wait for the right one */
573 static int mt9m001_video_probe(struct soc_camera_device *icd,
574 struct i2c_client *client)
576 struct mt9m001 *mt9m001 = to_mt9m001(client);
577 struct soc_camera_link *icl = to_soc_camera_link(icd);
578 s32 data;
579 unsigned long flags;
580 int ret;
582 /* We must have a parent by now. And it cannot be a wrong one.
583 * So this entire test is completely redundant. */
584 if (!icd->dev.parent ||
585 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
586 return -ENODEV;
588 /* Enable the chip */
589 data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
590 dev_dbg(&client->dev, "write: %d\n", data);
592 /* Read out the chip version register */
593 data = reg_read(client, MT9M001_CHIP_VERSION);
595 /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
596 switch (data) {
597 case 0x8411:
598 case 0x8421:
599 mt9m001->model = V4L2_IDENT_MT9M001C12ST;
600 icd->formats = mt9m001_colour_formats;
601 break;
602 case 0x8431:
603 mt9m001->model = V4L2_IDENT_MT9M001C12STM;
604 icd->formats = mt9m001_monochrome_formats;
605 break;
606 default:
607 dev_err(&client->dev,
608 "No MT9M001 chip detected, register read %x\n", data);
609 return -ENODEV;
612 icd->num_formats = 0;
615 * This is a 10bit sensor, so by default we only allow 10bit.
616 * The platform may support different bus widths due to
617 * different routing of the data lines.
619 if (icl->query_bus_param)
620 flags = icl->query_bus_param(icl);
621 else
622 flags = SOCAM_DATAWIDTH_10;
624 if (flags & SOCAM_DATAWIDTH_10)
625 icd->num_formats++;
626 else
627 icd->formats++;
629 if (flags & SOCAM_DATAWIDTH_8)
630 icd->num_formats++;
632 mt9m001->fourcc = icd->formats->fourcc;
634 dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
635 data == 0x8431 ? "C12STM" : "C12ST");
637 ret = mt9m001_init(client);
638 if (ret < 0)
639 dev_err(&client->dev, "Failed to initialise the camera\n");
641 /* mt9m001_init() has reset the chip, returning registers to defaults */
642 mt9m001->gain = 64;
643 mt9m001->exposure = 255;
645 return ret;
648 static void mt9m001_video_remove(struct soc_camera_device *icd)
650 struct soc_camera_link *icl = to_soc_camera_link(icd);
652 dev_dbg(&icd->dev, "Video removed: %p, %p\n",
653 icd->dev.parent, icd->vdev);
654 if (icl->free_bus)
655 icl->free_bus(icl);
658 static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
659 .g_ctrl = mt9m001_g_ctrl,
660 .s_ctrl = mt9m001_s_ctrl,
661 .g_chip_ident = mt9m001_g_chip_ident,
662 #ifdef CONFIG_VIDEO_ADV_DEBUG
663 .g_register = mt9m001_g_register,
664 .s_register = mt9m001_s_register,
665 #endif
668 static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
669 .s_stream = mt9m001_s_stream,
670 .s_fmt = mt9m001_s_fmt,
671 .g_fmt = mt9m001_g_fmt,
672 .try_fmt = mt9m001_try_fmt,
673 .s_crop = mt9m001_s_crop,
674 .g_crop = mt9m001_g_crop,
675 .cropcap = mt9m001_cropcap,
678 static struct v4l2_subdev_ops mt9m001_subdev_ops = {
679 .core = &mt9m001_subdev_core_ops,
680 .video = &mt9m001_subdev_video_ops,
683 static int mt9m001_probe(struct i2c_client *client,
684 const struct i2c_device_id *did)
686 struct mt9m001 *mt9m001;
687 struct soc_camera_device *icd = client->dev.platform_data;
688 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
689 struct soc_camera_link *icl;
690 int ret;
692 if (!icd) {
693 dev_err(&client->dev, "MT9M001: missing soc-camera data!\n");
694 return -EINVAL;
697 icl = to_soc_camera_link(icd);
698 if (!icl) {
699 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
700 return -EINVAL;
703 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
704 dev_warn(&adapter->dev,
705 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
706 return -EIO;
709 mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
710 if (!mt9m001)
711 return -ENOMEM;
713 v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
715 /* Second stage probe - when a capture adapter is there */
716 icd->ops = &mt9m001_ops;
717 icd->y_skip_top = 0;
719 mt9m001->rect.left = MT9M001_COLUMN_SKIP;
720 mt9m001->rect.top = MT9M001_ROW_SKIP;
721 mt9m001->rect.width = MT9M001_MAX_WIDTH;
722 mt9m001->rect.height = MT9M001_MAX_HEIGHT;
724 /* Simulated autoexposure. If enabled, we calculate shutter width
725 * ourselves in the driver based on vertical blanking and frame width */
726 mt9m001->autoexposure = 1;
728 ret = mt9m001_video_probe(icd, client);
729 if (ret) {
730 icd->ops = NULL;
731 i2c_set_clientdata(client, NULL);
732 kfree(mt9m001);
735 return ret;
738 static int mt9m001_remove(struct i2c_client *client)
740 struct mt9m001 *mt9m001 = to_mt9m001(client);
741 struct soc_camera_device *icd = client->dev.platform_data;
743 icd->ops = NULL;
744 mt9m001_video_remove(icd);
745 i2c_set_clientdata(client, NULL);
746 client->driver = NULL;
747 kfree(mt9m001);
749 return 0;
752 static const struct i2c_device_id mt9m001_id[] = {
753 { "mt9m001", 0 },
756 MODULE_DEVICE_TABLE(i2c, mt9m001_id);
758 static struct i2c_driver mt9m001_i2c_driver = {
759 .driver = {
760 .name = "mt9m001",
762 .probe = mt9m001_probe,
763 .remove = mt9m001_remove,
764 .id_table = mt9m001_id,
767 static int __init mt9m001_mod_init(void)
769 return i2c_add_driver(&mt9m001_i2c_driver);
772 static void __exit mt9m001_mod_exit(void)
774 i2c_del_driver(&mt9m001_i2c_driver);
777 module_init(mt9m001_mod_init);
778 module_exit(mt9m001_mod_exit);
780 MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
781 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
782 MODULE_LICENSE("GPL");