V4L/DVB (11024): soc-camera: separate S_FMT and S_CROP operations
[linux-2.6/mini2440.git] / drivers / media / video / mt9t031.c
blob677be18df9b346db8b3e82133c508a312a1abac0
1 /*
2 * Driver for MT9T031 CMOS Image Sensor from Micron
4 * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.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-common.h>
17 #include <media/v4l2-chip-ident.h>
18 #include <media/soc_camera.h>
20 /* mt9t031 i2c address 0x5d
21 * The platform has to define i2c_board_info
22 * and call i2c_register_board_info() */
24 /* mt9t031 selected register addresses */
25 #define MT9T031_CHIP_VERSION 0x00
26 #define MT9T031_ROW_START 0x01
27 #define MT9T031_COLUMN_START 0x02
28 #define MT9T031_WINDOW_HEIGHT 0x03
29 #define MT9T031_WINDOW_WIDTH 0x04
30 #define MT9T031_HORIZONTAL_BLANKING 0x05
31 #define MT9T031_VERTICAL_BLANKING 0x06
32 #define MT9T031_OUTPUT_CONTROL 0x07
33 #define MT9T031_SHUTTER_WIDTH_UPPER 0x08
34 #define MT9T031_SHUTTER_WIDTH 0x09
35 #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
36 #define MT9T031_FRAME_RESTART 0x0b
37 #define MT9T031_SHUTTER_DELAY 0x0c
38 #define MT9T031_RESET 0x0d
39 #define MT9T031_READ_MODE_1 0x1e
40 #define MT9T031_READ_MODE_2 0x20
41 #define MT9T031_READ_MODE_3 0x21
42 #define MT9T031_ROW_ADDRESS_MODE 0x22
43 #define MT9T031_COLUMN_ADDRESS_MODE 0x23
44 #define MT9T031_GLOBAL_GAIN 0x35
45 #define MT9T031_CHIP_ENABLE 0xF8
47 #define MT9T031_MAX_HEIGHT 1536
48 #define MT9T031_MAX_WIDTH 2048
49 #define MT9T031_MIN_HEIGHT 2
50 #define MT9T031_MIN_WIDTH 2
51 #define MT9T031_HORIZONTAL_BLANK 142
52 #define MT9T031_VERTICAL_BLANK 25
53 #define MT9T031_COLUMN_SKIP 32
54 #define MT9T031_ROW_SKIP 20
56 #define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \
57 SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \
58 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \
59 SOCAM_MASTER | SOCAM_DATAWIDTH_10)
61 static const struct soc_camera_data_format mt9t031_colour_formats[] = {
63 .name = "Bayer (sRGB) 10 bit",
64 .depth = 10,
65 .fourcc = V4L2_PIX_FMT_SGRBG10,
66 .colorspace = V4L2_COLORSPACE_SRGB,
70 struct mt9t031 {
71 struct i2c_client *client;
72 struct soc_camera_device icd;
73 int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
74 unsigned char autoexposure;
75 u16 xskip;
76 u16 yskip;
79 static int reg_read(struct soc_camera_device *icd, const u8 reg)
81 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
82 struct i2c_client *client = mt9t031->client;
83 s32 data = i2c_smbus_read_word_data(client, reg);
84 return data < 0 ? data : swab16(data);
87 static int reg_write(struct soc_camera_device *icd, const u8 reg,
88 const u16 data)
90 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
91 return i2c_smbus_write_word_data(mt9t031->client, reg, swab16(data));
94 static int reg_set(struct soc_camera_device *icd, const u8 reg,
95 const u16 data)
97 int ret;
99 ret = reg_read(icd, reg);
100 if (ret < 0)
101 return ret;
102 return reg_write(icd, reg, ret | data);
105 static int reg_clear(struct soc_camera_device *icd, const u8 reg,
106 const u16 data)
108 int ret;
110 ret = reg_read(icd, reg);
111 if (ret < 0)
112 return ret;
113 return reg_write(icd, reg, ret & ~data);
116 static int set_shutter(struct soc_camera_device *icd, const u32 data)
118 int ret;
120 ret = reg_write(icd, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
122 if (ret >= 0)
123 ret = reg_write(icd, MT9T031_SHUTTER_WIDTH, data & 0xffff);
125 return ret;
128 static int get_shutter(struct soc_camera_device *icd, u32 *data)
130 int ret;
132 ret = reg_read(icd, MT9T031_SHUTTER_WIDTH_UPPER);
133 *data = ret << 16;
135 if (ret >= 0)
136 ret = reg_read(icd, MT9T031_SHUTTER_WIDTH);
137 *data |= ret & 0xffff;
139 return ret < 0 ? ret : 0;
142 static int mt9t031_init(struct soc_camera_device *icd)
144 int ret;
146 /* Disable chip output, synchronous option update */
147 dev_dbg(icd->vdev->parent, "%s\n", __func__);
149 ret = reg_write(icd, MT9T031_RESET, 1);
150 if (ret >= 0)
151 ret = reg_write(icd, MT9T031_RESET, 0);
152 if (ret >= 0)
153 ret = reg_clear(icd, MT9T031_OUTPUT_CONTROL, 2);
155 return ret >= 0 ? 0 : -EIO;
158 static int mt9t031_release(struct soc_camera_device *icd)
160 /* Disable the chip */
161 reg_clear(icd, MT9T031_OUTPUT_CONTROL, 2);
162 return 0;
165 static int mt9t031_start_capture(struct soc_camera_device *icd)
167 /* Switch to master "normal" mode */
168 if (reg_set(icd, MT9T031_OUTPUT_CONTROL, 2) < 0)
169 return -EIO;
170 return 0;
173 static int mt9t031_stop_capture(struct soc_camera_device *icd)
175 /* Stop sensor readout */
176 if (reg_clear(icd, MT9T031_OUTPUT_CONTROL, 2) < 0)
177 return -EIO;
178 return 0;
181 static int mt9t031_set_bus_param(struct soc_camera_device *icd,
182 unsigned long flags)
184 /* The caller should have queried our parameters, check anyway */
185 if (flags & ~MT9T031_BUS_PARAM)
186 return -EINVAL;
188 if (flags & SOCAM_PCLK_SAMPLE_FALLING)
189 reg_set(icd, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
190 else
191 reg_clear(icd, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
193 return 0;
196 static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
198 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
199 struct soc_camera_link *icl = mt9t031->client->dev.platform_data;
201 return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
204 /* Round up minima and round down maxima */
205 static void recalculate_limits(struct soc_camera_device *icd,
206 u16 xskip, u16 yskip)
208 icd->x_min = (MT9T031_COLUMN_SKIP + xskip - 1) / xskip;
209 icd->y_min = (MT9T031_ROW_SKIP + yskip - 1) / yskip;
210 icd->width_min = (MT9T031_MIN_WIDTH + xskip - 1) / xskip;
211 icd->height_min = (MT9T031_MIN_HEIGHT + yskip - 1) / yskip;
212 icd->width_max = MT9T031_MAX_WIDTH / xskip;
213 icd->height_max = MT9T031_MAX_HEIGHT / yskip;
216 static int mt9t031_set_params(struct soc_camera_device *icd,
217 struct v4l2_rect *rect, u16 xskip, u16 yskip)
219 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
220 int ret;
221 u16 xbin, ybin, width, height, left, top;
222 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
223 vblank = MT9T031_VERTICAL_BLANK;
225 /* Make sure we don't exceed sensor limits */
226 if (rect->left + rect->width > icd->width_max)
227 rect->left = (icd->width_max - rect->width) / 2 + icd->x_min;
229 if (rect->top + rect->height > icd->height_max)
230 rect->top = (icd->height_max - rect->height) / 2 + icd->y_min;
232 width = rect->width * xskip;
233 height = rect->height * yskip;
234 left = rect->left * xskip;
235 top = rect->top * yskip;
237 xbin = min(xskip, (u16)3);
238 ybin = min(yskip, (u16)3);
240 dev_dbg(&icd->dev, "xskip %u, width %u/%u, yskip %u, height %u/%u\n",
241 xskip, width, rect->width, yskip, height, rect->height);
243 /* Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper */
244 switch (xbin) {
245 case 2:
246 left = (left + 3) & ~3;
247 break;
248 case 3:
249 left = roundup(left, 6);
252 switch (ybin) {
253 case 2:
254 top = (top + 3) & ~3;
255 break;
256 case 3:
257 top = roundup(top, 6);
260 /* Disable register update, reconfigure atomically */
261 ret = reg_set(icd, MT9T031_OUTPUT_CONTROL, 1);
262 if (ret < 0)
263 return ret;
265 /* Blanking and start values - default... */
266 ret = reg_write(icd, MT9T031_HORIZONTAL_BLANKING, hblank);
267 if (ret >= 0)
268 ret = reg_write(icd, MT9T031_VERTICAL_BLANKING, vblank);
270 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
271 /* Binning, skipping */
272 if (ret >= 0)
273 ret = reg_write(icd, MT9T031_COLUMN_ADDRESS_MODE,
274 ((xbin - 1) << 4) | (xskip - 1));
275 if (ret >= 0)
276 ret = reg_write(icd, MT9T031_ROW_ADDRESS_MODE,
277 ((ybin - 1) << 4) | (yskip - 1));
279 dev_dbg(&icd->dev, "new physical left %u, top %u\n", left, top);
281 /* The caller provides a supported format, as guaranteed by
282 * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap() */
283 if (ret >= 0)
284 ret = reg_write(icd, MT9T031_COLUMN_START, left);
285 if (ret >= 0)
286 ret = reg_write(icd, MT9T031_ROW_START, top);
287 if (ret >= 0)
288 ret = reg_write(icd, MT9T031_WINDOW_WIDTH, width - 1);
289 if (ret >= 0)
290 ret = reg_write(icd, MT9T031_WINDOW_HEIGHT,
291 height + icd->y_skip_top - 1);
292 if (ret >= 0 && mt9t031->autoexposure) {
293 ret = set_shutter(icd, height + icd->y_skip_top + vblank);
294 if (ret >= 0) {
295 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
296 const struct v4l2_queryctrl *qctrl =
297 soc_camera_find_qctrl(icd->ops,
298 V4L2_CID_EXPOSURE);
299 icd->exposure = (shutter_max / 2 + (height +
300 icd->y_skip_top + vblank - 1) *
301 (qctrl->maximum - qctrl->minimum)) /
302 shutter_max + qctrl->minimum;
306 /* Re-enable register update, commit all changes */
307 if (ret >= 0)
308 ret = reg_clear(icd, MT9T031_OUTPUT_CONTROL, 1);
310 return ret < 0 ? ret : 0;
313 static int mt9t031_set_crop(struct soc_camera_device *icd,
314 struct v4l2_rect *rect)
316 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
318 /* CROP - no change in scaling, or in limits */
319 return mt9t031_set_params(icd, rect, mt9t031->xskip, mt9t031->yskip);
322 static int mt9t031_set_fmt(struct soc_camera_device *icd,
323 struct v4l2_format *f)
325 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
326 int ret;
327 u16 xskip, yskip;
328 struct v4l2_rect rect = {
329 .left = icd->x_current,
330 .top = icd->y_current,
331 .width = f->fmt.pix.width,
332 .height = f->fmt.pix.height,
336 * try_fmt has put rectangle within limits.
337 * S_FMT - use binning and skipping for scaling, recalculate
338 * limits, used for cropping
340 /* Is this more optimal than just a division? */
341 for (xskip = 8; xskip > 1; xskip--)
342 if (rect.width * xskip <= MT9T031_MAX_WIDTH)
343 break;
345 for (yskip = 8; yskip > 1; yskip--)
346 if (rect.height * yskip <= MT9T031_MAX_HEIGHT)
347 break;
349 recalculate_limits(icd, xskip, yskip);
351 ret = mt9t031_set_params(icd, &rect, xskip, yskip);
352 if (!ret) {
353 mt9t031->xskip = xskip;
354 mt9t031->yskip = yskip;
357 return ret;
360 static int mt9t031_try_fmt(struct soc_camera_device *icd,
361 struct v4l2_format *f)
363 struct v4l2_pix_format *pix = &f->fmt.pix;
365 if (pix->height < MT9T031_MIN_HEIGHT)
366 pix->height = MT9T031_MIN_HEIGHT;
367 if (pix->height > MT9T031_MAX_HEIGHT)
368 pix->height = MT9T031_MAX_HEIGHT;
369 if (pix->width < MT9T031_MIN_WIDTH)
370 pix->width = MT9T031_MIN_WIDTH;
371 if (pix->width > MT9T031_MAX_WIDTH)
372 pix->width = MT9T031_MAX_WIDTH;
374 pix->width &= ~0x01; /* has to be even */
375 pix->height &= ~0x01; /* has to be even */
377 return 0;
380 static int mt9t031_get_chip_id(struct soc_camera_device *icd,
381 struct v4l2_dbg_chip_ident *id)
383 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
385 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
386 return -EINVAL;
388 if (id->match.addr != mt9t031->client->addr)
389 return -ENODEV;
391 id->ident = mt9t031->model;
392 id->revision = 0;
394 return 0;
397 #ifdef CONFIG_VIDEO_ADV_DEBUG
398 static int mt9t031_get_register(struct soc_camera_device *icd,
399 struct v4l2_dbg_register *reg)
401 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
403 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
404 return -EINVAL;
406 if (reg->match.addr != mt9t031->client->addr)
407 return -ENODEV;
409 reg->val = reg_read(icd, reg->reg);
411 if (reg->val > 0xffff)
412 return -EIO;
414 return 0;
417 static int mt9t031_set_register(struct soc_camera_device *icd,
418 struct v4l2_dbg_register *reg)
420 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
422 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
423 return -EINVAL;
425 if (reg->match.addr != mt9t031->client->addr)
426 return -ENODEV;
428 if (reg_write(icd, reg->reg, reg->val) < 0)
429 return -EIO;
431 return 0;
433 #endif
435 static const struct v4l2_queryctrl mt9t031_controls[] = {
437 .id = V4L2_CID_VFLIP,
438 .type = V4L2_CTRL_TYPE_BOOLEAN,
439 .name = "Flip Vertically",
440 .minimum = 0,
441 .maximum = 1,
442 .step = 1,
443 .default_value = 0,
444 }, {
445 .id = V4L2_CID_HFLIP,
446 .type = V4L2_CTRL_TYPE_BOOLEAN,
447 .name = "Flip Horizontally",
448 .minimum = 0,
449 .maximum = 1,
450 .step = 1,
451 .default_value = 0,
452 }, {
453 .id = V4L2_CID_GAIN,
454 .type = V4L2_CTRL_TYPE_INTEGER,
455 .name = "Gain",
456 .minimum = 0,
457 .maximum = 127,
458 .step = 1,
459 .default_value = 64,
460 .flags = V4L2_CTRL_FLAG_SLIDER,
461 }, {
462 .id = V4L2_CID_EXPOSURE,
463 .type = V4L2_CTRL_TYPE_INTEGER,
464 .name = "Exposure",
465 .minimum = 1,
466 .maximum = 255,
467 .step = 1,
468 .default_value = 255,
469 .flags = V4L2_CTRL_FLAG_SLIDER,
470 }, {
471 .id = V4L2_CID_EXPOSURE_AUTO,
472 .type = V4L2_CTRL_TYPE_BOOLEAN,
473 .name = "Automatic Exposure",
474 .minimum = 0,
475 .maximum = 1,
476 .step = 1,
477 .default_value = 1,
481 static int mt9t031_video_probe(struct soc_camera_device *);
482 static void mt9t031_video_remove(struct soc_camera_device *);
483 static int mt9t031_get_control(struct soc_camera_device *, struct v4l2_control *);
484 static int mt9t031_set_control(struct soc_camera_device *, struct v4l2_control *);
486 static struct soc_camera_ops mt9t031_ops = {
487 .owner = THIS_MODULE,
488 .probe = mt9t031_video_probe,
489 .remove = mt9t031_video_remove,
490 .init = mt9t031_init,
491 .release = mt9t031_release,
492 .start_capture = mt9t031_start_capture,
493 .stop_capture = mt9t031_stop_capture,
494 .set_crop = mt9t031_set_crop,
495 .set_fmt = mt9t031_set_fmt,
496 .try_fmt = mt9t031_try_fmt,
497 .set_bus_param = mt9t031_set_bus_param,
498 .query_bus_param = mt9t031_query_bus_param,
499 .controls = mt9t031_controls,
500 .num_controls = ARRAY_SIZE(mt9t031_controls),
501 .get_control = mt9t031_get_control,
502 .set_control = mt9t031_set_control,
503 .get_chip_id = mt9t031_get_chip_id,
504 #ifdef CONFIG_VIDEO_ADV_DEBUG
505 .get_register = mt9t031_get_register,
506 .set_register = mt9t031_set_register,
507 #endif
510 static int mt9t031_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
512 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
513 int data;
515 switch (ctrl->id) {
516 case V4L2_CID_VFLIP:
517 data = reg_read(icd, MT9T031_READ_MODE_2);
518 if (data < 0)
519 return -EIO;
520 ctrl->value = !!(data & 0x8000);
521 break;
522 case V4L2_CID_HFLIP:
523 data = reg_read(icd, MT9T031_READ_MODE_2);
524 if (data < 0)
525 return -EIO;
526 ctrl->value = !!(data & 0x4000);
527 break;
528 case V4L2_CID_EXPOSURE_AUTO:
529 ctrl->value = mt9t031->autoexposure;
530 break;
532 return 0;
535 static int mt9t031_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
537 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
538 const struct v4l2_queryctrl *qctrl;
539 int data;
541 qctrl = soc_camera_find_qctrl(&mt9t031_ops, ctrl->id);
543 if (!qctrl)
544 return -EINVAL;
546 switch (ctrl->id) {
547 case V4L2_CID_VFLIP:
548 if (ctrl->value)
549 data = reg_set(icd, MT9T031_READ_MODE_2, 0x8000);
550 else
551 data = reg_clear(icd, MT9T031_READ_MODE_2, 0x8000);
552 if (data < 0)
553 return -EIO;
554 break;
555 case V4L2_CID_HFLIP:
556 if (ctrl->value)
557 data = reg_set(icd, MT9T031_READ_MODE_2, 0x4000);
558 else
559 data = reg_clear(icd, MT9T031_READ_MODE_2, 0x4000);
560 if (data < 0)
561 return -EIO;
562 break;
563 case V4L2_CID_GAIN:
564 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
565 return -EINVAL;
566 /* See Datasheet Table 7, Gain settings. */
567 if (ctrl->value <= qctrl->default_value) {
568 /* Pack it into 0..1 step 0.125, register values 0..8 */
569 unsigned long range = qctrl->default_value - qctrl->minimum;
570 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
572 dev_dbg(&icd->dev, "Setting gain %d\n", data);
573 data = reg_write(icd, MT9T031_GLOBAL_GAIN, data);
574 if (data < 0)
575 return -EIO;
576 } else {
577 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
578 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
579 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
580 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
581 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
582 1015 + range / 2) / range + 9;
584 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
585 data = gain;
586 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
587 data = ((gain - 32) * 16 + 16) / 32 + 80;
588 else
589 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
590 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
592 dev_dbg(&icd->dev, "Setting gain from 0x%x to 0x%x\n",
593 reg_read(icd, MT9T031_GLOBAL_GAIN), data);
594 data = reg_write(icd, MT9T031_GLOBAL_GAIN, data);
595 if (data < 0)
596 return -EIO;
599 /* Success */
600 icd->gain = ctrl->value;
601 break;
602 case V4L2_CID_EXPOSURE:
603 /* mt9t031 has maximum == default */
604 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
605 return -EINVAL;
606 else {
607 const unsigned long range = qctrl->maximum - qctrl->minimum;
608 const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
609 range / 2) / range + 1;
610 u32 old;
612 get_shutter(icd, &old);
613 dev_dbg(&icd->dev, "Setting shutter width from %u to %u\n",
614 old, shutter);
615 if (set_shutter(icd, shutter) < 0)
616 return -EIO;
617 icd->exposure = ctrl->value;
618 mt9t031->autoexposure = 0;
620 break;
621 case V4L2_CID_EXPOSURE_AUTO:
622 if (ctrl->value) {
623 const u16 vblank = MT9T031_VERTICAL_BLANK;
624 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
625 if (set_shutter(icd, icd->height +
626 icd->y_skip_top + vblank) < 0)
627 return -EIO;
628 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
629 icd->exposure = (shutter_max / 2 + (icd->height +
630 icd->y_skip_top + vblank - 1) *
631 (qctrl->maximum - qctrl->minimum)) /
632 shutter_max + qctrl->minimum;
633 mt9t031->autoexposure = 1;
634 } else
635 mt9t031->autoexposure = 0;
636 break;
638 return 0;
641 /* Interface active, can use i2c. If it fails, it can indeed mean, that
642 * this wasn't our capture interface, so, we wait for the right one */
643 static int mt9t031_video_probe(struct soc_camera_device *icd)
645 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
646 s32 data;
647 int ret;
649 /* We must have a parent by now. And it cannot be a wrong one.
650 * So this entire test is completely redundant. */
651 if (!icd->dev.parent ||
652 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
653 return -ENODEV;
655 /* Enable the chip */
656 data = reg_write(icd, MT9T031_CHIP_ENABLE, 1);
657 dev_dbg(&icd->dev, "write: %d\n", data);
659 /* Read out the chip version register */
660 data = reg_read(icd, MT9T031_CHIP_VERSION);
662 switch (data) {
663 case 0x1621:
664 mt9t031->model = V4L2_IDENT_MT9T031;
665 icd->formats = mt9t031_colour_formats;
666 icd->num_formats = ARRAY_SIZE(mt9t031_colour_formats);
667 break;
668 default:
669 ret = -ENODEV;
670 dev_err(&icd->dev,
671 "No MT9T031 chip detected, register read %x\n", data);
672 goto ei2c;
675 dev_info(&icd->dev, "Detected a MT9T031 chip ID %x\n", data);
677 /* Now that we know the model, we can start video */
678 ret = soc_camera_video_start(icd);
679 if (ret)
680 goto evstart;
682 return 0;
684 evstart:
685 ei2c:
686 return ret;
689 static void mt9t031_video_remove(struct soc_camera_device *icd)
691 struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
693 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9t031->client->addr,
694 icd->dev.parent, icd->vdev);
695 soc_camera_video_stop(icd);
698 static int mt9t031_probe(struct i2c_client *client,
699 const struct i2c_device_id *did)
701 struct mt9t031 *mt9t031;
702 struct soc_camera_device *icd;
703 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
704 struct soc_camera_link *icl = client->dev.platform_data;
705 int ret;
707 if (!icl) {
708 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
709 return -EINVAL;
712 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
713 dev_warn(&adapter->dev,
714 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
715 return -EIO;
718 mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
719 if (!mt9t031)
720 return -ENOMEM;
722 mt9t031->client = client;
723 i2c_set_clientdata(client, mt9t031);
725 /* Second stage probe - when a capture adapter is there */
726 icd = &mt9t031->icd;
727 icd->ops = &mt9t031_ops;
728 icd->control = &client->dev;
729 icd->x_min = MT9T031_COLUMN_SKIP;
730 icd->y_min = MT9T031_ROW_SKIP;
731 icd->x_current = icd->x_min;
732 icd->y_current = icd->y_min;
733 icd->width_min = MT9T031_MIN_WIDTH;
734 icd->width_max = MT9T031_MAX_WIDTH;
735 icd->height_min = MT9T031_MIN_HEIGHT;
736 icd->height_max = MT9T031_MAX_HEIGHT;
737 icd->y_skip_top = 0;
738 icd->iface = icl->bus_id;
739 /* Simulated autoexposure. If enabled, we calculate shutter width
740 * ourselves in the driver based on vertical blanking and frame width */
741 mt9t031->autoexposure = 1;
743 mt9t031->xskip = 1;
744 mt9t031->yskip = 1;
746 ret = soc_camera_device_register(icd);
747 if (ret)
748 goto eisdr;
750 return 0;
752 eisdr:
753 i2c_set_clientdata(client, NULL);
754 kfree(mt9t031);
755 return ret;
758 static int mt9t031_remove(struct i2c_client *client)
760 struct mt9t031 *mt9t031 = i2c_get_clientdata(client);
762 soc_camera_device_unregister(&mt9t031->icd);
763 i2c_set_clientdata(client, NULL);
764 kfree(mt9t031);
766 return 0;
769 static const struct i2c_device_id mt9t031_id[] = {
770 { "mt9t031", 0 },
773 MODULE_DEVICE_TABLE(i2c, mt9t031_id);
775 static struct i2c_driver mt9t031_i2c_driver = {
776 .driver = {
777 .name = "mt9t031",
779 .probe = mt9t031_probe,
780 .remove = mt9t031_remove,
781 .id_table = mt9t031_id,
784 static int __init mt9t031_mod_init(void)
786 return i2c_add_driver(&mt9t031_i2c_driver);
789 static void __exit mt9t031_mod_exit(void)
791 i2c_del_driver(&mt9t031_i2c_driver);
794 module_init(mt9t031_mod_init);
795 module_exit(mt9t031_mod_exit);
797 MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
798 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
799 MODULE_LICENSE("GPL v2");