V4L/DVB (12535): soc-camera: remove .init() and .release() methods from struct soc_ca...
[linux-2.6/mini2440.git] / drivers / media / video / mt9t031.c
blob9a648968938287eb5183881183cdb5d49a622b48
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-subdev.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 and link to it from
22 * struct soc_camera_link */
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 18
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 v4l2_subdev subdev;
72 struct v4l2_rect rect; /* Sensor window */
73 int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
74 u16 xskip;
75 u16 yskip;
76 unsigned char autoexposure;
79 static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
81 return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
84 static int reg_read(struct i2c_client *client, const u8 reg)
86 s32 data = i2c_smbus_read_word_data(client, reg);
87 return data < 0 ? data : swab16(data);
90 static int reg_write(struct i2c_client *client, const u8 reg,
91 const u16 data)
93 return i2c_smbus_write_word_data(client, reg, swab16(data));
96 static int reg_set(struct i2c_client *client, const u8 reg,
97 const u16 data)
99 int ret;
101 ret = reg_read(client, reg);
102 if (ret < 0)
103 return ret;
104 return reg_write(client, reg, ret | data);
107 static int reg_clear(struct i2c_client *client, const u8 reg,
108 const u16 data)
110 int ret;
112 ret = reg_read(client, reg);
113 if (ret < 0)
114 return ret;
115 return reg_write(client, reg, ret & ~data);
118 static int set_shutter(struct i2c_client *client, const u32 data)
120 int ret;
122 ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
124 if (ret >= 0)
125 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
127 return ret;
130 static int get_shutter(struct i2c_client *client, u32 *data)
132 int ret;
134 ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
135 *data = ret << 16;
137 if (ret >= 0)
138 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
139 *data |= ret & 0xffff;
141 return ret < 0 ? ret : 0;
144 static int mt9t031_idle(struct i2c_client *client)
146 int ret;
148 /* Disable chip output, synchronous option update */
149 ret = reg_write(client, MT9T031_RESET, 1);
150 if (ret >= 0)
151 ret = reg_write(client, MT9T031_RESET, 0);
152 if (ret >= 0)
153 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
155 return ret >= 0 ? 0 : -EIO;
158 static int mt9t031_disable(struct i2c_client *client)
160 /* Disable the chip */
161 reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
163 return 0;
166 static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
168 struct i2c_client *client = sd->priv;
169 int ret;
171 if (enable)
172 /* Switch to master "normal" mode */
173 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
174 else
175 /* Stop sensor readout */
176 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
178 if (ret < 0)
179 return -EIO;
181 return 0;
184 static int mt9t031_set_bus_param(struct soc_camera_device *icd,
185 unsigned long flags)
187 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
189 /* The caller should have queried our parameters, check anyway */
190 if (flags & ~MT9T031_BUS_PARAM)
191 return -EINVAL;
193 if (flags & SOCAM_PCLK_SAMPLE_FALLING)
194 reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
195 else
196 reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
198 return 0;
201 static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
203 struct soc_camera_link *icl = to_soc_camera_link(icd);
205 return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
208 /* target must be _even_ */
209 static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
211 unsigned int skip;
213 if (*source < target + target / 2) {
214 *source = target;
215 return 1;
218 skip = min(max, *source + target / 2) / target;
219 if (skip > 8)
220 skip = 8;
221 *source = target * skip;
223 return skip;
226 /* rect is the sensor rectangle, the caller guarantees parameter validity */
227 static int mt9t031_set_params(struct soc_camera_device *icd,
228 struct v4l2_rect *rect, u16 xskip, u16 yskip)
230 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
231 struct mt9t031 *mt9t031 = to_mt9t031(client);
232 int ret;
233 u16 xbin, ybin;
234 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
235 vblank = MT9T031_VERTICAL_BLANK;
237 xbin = min(xskip, (u16)3);
238 ybin = min(yskip, (u16)3);
241 * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
242 * There is always a valid suitably aligned value. The worst case is
243 * xbin = 3, width = 2048. Then we will start at 36, the last read out
244 * pixel will be 2083, which is < 2085 - first black pixel.
246 * MT9T031 datasheet imposes window left border alignment, depending on
247 * the selected xskip. Failing to conform to this requirement produces
248 * dark horizontal stripes in the image. However, even obeying to this
249 * requirement doesn't eliminate the stripes in all configurations. They
250 * appear "locally reproducibly," but can differ between tests under
251 * different lighting conditions.
253 switch (xbin) {
254 case 1:
255 rect->left &= ~1;
256 break;
257 case 2:
258 rect->left &= ~3;
259 break;
260 case 3:
261 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
262 (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
265 rect->top &= ~1;
267 dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
268 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
270 /* Disable register update, reconfigure atomically */
271 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
272 if (ret < 0)
273 return ret;
275 /* Blanking and start values - default... */
276 ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
277 if (ret >= 0)
278 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
280 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
281 /* Binning, skipping */
282 if (ret >= 0)
283 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
284 ((xbin - 1) << 4) | (xskip - 1));
285 if (ret >= 0)
286 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
287 ((ybin - 1) << 4) | (yskip - 1));
289 dev_dbg(&client->dev, "new physical left %u, top %u\n",
290 rect->left, rect->top);
292 /* The caller provides a supported format, as guaranteed by
293 * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap() */
294 if (ret >= 0)
295 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
296 if (ret >= 0)
297 ret = reg_write(client, MT9T031_ROW_START, rect->top);
298 if (ret >= 0)
299 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
300 if (ret >= 0)
301 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
302 rect->height + icd->y_skip_top - 1);
303 if (ret >= 0 && mt9t031->autoexposure) {
304 ret = set_shutter(client,
305 rect->height + icd->y_skip_top + vblank);
306 if (ret >= 0) {
307 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
308 const struct v4l2_queryctrl *qctrl =
309 soc_camera_find_qctrl(icd->ops,
310 V4L2_CID_EXPOSURE);
311 icd->exposure = (shutter_max / 2 + (rect->height +
312 icd->y_skip_top + vblank - 1) *
313 (qctrl->maximum - qctrl->minimum)) /
314 shutter_max + qctrl->minimum;
318 /* Re-enable register update, commit all changes */
319 if (ret >= 0)
320 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
322 if (ret >= 0) {
323 mt9t031->rect = *rect;
324 mt9t031->xskip = xskip;
325 mt9t031->yskip = yskip;
328 return ret < 0 ? ret : 0;
331 static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
333 struct v4l2_rect rect = a->c;
334 struct i2c_client *client = sd->priv;
335 struct mt9t031 *mt9t031 = to_mt9t031(client);
336 struct soc_camera_device *icd = client->dev.platform_data;
338 rect.width = ALIGN(rect.width, 2);
339 rect.height = ALIGN(rect.height, 2);
341 soc_camera_limit_side(&rect.left, &rect.width,
342 MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
344 soc_camera_limit_side(&rect.top, &rect.height,
345 MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
347 return mt9t031_set_params(icd, &rect, mt9t031->xskip, mt9t031->yskip);
350 static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
352 struct i2c_client *client = sd->priv;
353 struct mt9t031 *mt9t031 = to_mt9t031(client);
355 a->c = mt9t031->rect;
356 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
358 return 0;
361 static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
363 a->bounds.left = MT9T031_COLUMN_SKIP;
364 a->bounds.top = MT9T031_ROW_SKIP;
365 a->bounds.width = MT9T031_MAX_WIDTH;
366 a->bounds.height = MT9T031_MAX_HEIGHT;
367 a->defrect = a->bounds;
368 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
369 a->pixelaspect.numerator = 1;
370 a->pixelaspect.denominator = 1;
372 return 0;
375 static int mt9t031_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
377 struct i2c_client *client = sd->priv;
378 struct mt9t031 *mt9t031 = to_mt9t031(client);
379 struct v4l2_pix_format *pix = &f->fmt.pix;
381 pix->width = mt9t031->rect.width / mt9t031->xskip;
382 pix->height = mt9t031->rect.height / mt9t031->yskip;
383 pix->pixelformat = V4L2_PIX_FMT_SGRBG10;
384 pix->field = V4L2_FIELD_NONE;
385 pix->colorspace = V4L2_COLORSPACE_SRGB;
387 return 0;
390 static int mt9t031_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
392 struct i2c_client *client = sd->priv;
393 struct mt9t031 *mt9t031 = to_mt9t031(client);
394 struct soc_camera_device *icd = client->dev.platform_data;
395 struct v4l2_pix_format *pix = &f->fmt.pix;
396 u16 xskip, yskip;
397 struct v4l2_rect rect = mt9t031->rect;
400 * try_fmt has put width and height within limits.
401 * S_FMT: use binning and skipping for scaling
403 xskip = mt9t031_skip(&rect.width, pix->width, MT9T031_MAX_WIDTH);
404 yskip = mt9t031_skip(&rect.height, pix->height, MT9T031_MAX_HEIGHT);
406 /* mt9t031_set_params() doesn't change width and height */
407 return mt9t031_set_params(icd, &rect, xskip, yskip);
411 * If a user window larger than sensor window is requested, we'll increase the
412 * sensor window.
414 static int mt9t031_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
416 struct v4l2_pix_format *pix = &f->fmt.pix;
418 v4l_bound_align_image(
419 &pix->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
420 &pix->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
422 return 0;
425 static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
426 struct v4l2_dbg_chip_ident *id)
428 struct i2c_client *client = sd->priv;
429 struct mt9t031 *mt9t031 = to_mt9t031(client);
431 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
432 return -EINVAL;
434 if (id->match.addr != client->addr)
435 return -ENODEV;
437 id->ident = mt9t031->model;
438 id->revision = 0;
440 return 0;
443 #ifdef CONFIG_VIDEO_ADV_DEBUG
444 static int mt9t031_g_register(struct v4l2_subdev *sd,
445 struct v4l2_dbg_register *reg)
447 struct i2c_client *client = sd->priv;
449 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
450 return -EINVAL;
452 if (reg->match.addr != client->addr)
453 return -ENODEV;
455 reg->val = reg_read(client, reg->reg);
457 if (reg->val > 0xffff)
458 return -EIO;
460 return 0;
463 static int mt9t031_s_register(struct v4l2_subdev *sd,
464 struct v4l2_dbg_register *reg)
466 struct i2c_client *client = sd->priv;
468 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
469 return -EINVAL;
471 if (reg->match.addr != client->addr)
472 return -ENODEV;
474 if (reg_write(client, reg->reg, reg->val) < 0)
475 return -EIO;
477 return 0;
479 #endif
481 static const struct v4l2_queryctrl mt9t031_controls[] = {
483 .id = V4L2_CID_VFLIP,
484 .type = V4L2_CTRL_TYPE_BOOLEAN,
485 .name = "Flip Vertically",
486 .minimum = 0,
487 .maximum = 1,
488 .step = 1,
489 .default_value = 0,
490 }, {
491 .id = V4L2_CID_HFLIP,
492 .type = V4L2_CTRL_TYPE_BOOLEAN,
493 .name = "Flip Horizontally",
494 .minimum = 0,
495 .maximum = 1,
496 .step = 1,
497 .default_value = 0,
498 }, {
499 .id = V4L2_CID_GAIN,
500 .type = V4L2_CTRL_TYPE_INTEGER,
501 .name = "Gain",
502 .minimum = 0,
503 .maximum = 127,
504 .step = 1,
505 .default_value = 64,
506 .flags = V4L2_CTRL_FLAG_SLIDER,
507 }, {
508 .id = V4L2_CID_EXPOSURE,
509 .type = V4L2_CTRL_TYPE_INTEGER,
510 .name = "Exposure",
511 .minimum = 1,
512 .maximum = 255,
513 .step = 1,
514 .default_value = 255,
515 .flags = V4L2_CTRL_FLAG_SLIDER,
516 }, {
517 .id = V4L2_CID_EXPOSURE_AUTO,
518 .type = V4L2_CTRL_TYPE_BOOLEAN,
519 .name = "Automatic Exposure",
520 .minimum = 0,
521 .maximum = 1,
522 .step = 1,
523 .default_value = 1,
527 static struct soc_camera_ops mt9t031_ops = {
528 .set_bus_param = mt9t031_set_bus_param,
529 .query_bus_param = mt9t031_query_bus_param,
530 .controls = mt9t031_controls,
531 .num_controls = ARRAY_SIZE(mt9t031_controls),
534 static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
536 struct i2c_client *client = sd->priv;
537 struct mt9t031 *mt9t031 = to_mt9t031(client);
538 int data;
540 switch (ctrl->id) {
541 case V4L2_CID_VFLIP:
542 data = reg_read(client, MT9T031_READ_MODE_2);
543 if (data < 0)
544 return -EIO;
545 ctrl->value = !!(data & 0x8000);
546 break;
547 case V4L2_CID_HFLIP:
548 data = reg_read(client, MT9T031_READ_MODE_2);
549 if (data < 0)
550 return -EIO;
551 ctrl->value = !!(data & 0x4000);
552 break;
553 case V4L2_CID_EXPOSURE_AUTO:
554 ctrl->value = mt9t031->autoexposure;
555 break;
557 return 0;
560 static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
562 struct i2c_client *client = sd->priv;
563 struct mt9t031 *mt9t031 = to_mt9t031(client);
564 struct soc_camera_device *icd = client->dev.platform_data;
565 const struct v4l2_queryctrl *qctrl;
566 int data;
568 qctrl = soc_camera_find_qctrl(&mt9t031_ops, ctrl->id);
570 if (!qctrl)
571 return -EINVAL;
573 switch (ctrl->id) {
574 case V4L2_CID_VFLIP:
575 if (ctrl->value)
576 data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
577 else
578 data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
579 if (data < 0)
580 return -EIO;
581 break;
582 case V4L2_CID_HFLIP:
583 if (ctrl->value)
584 data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
585 else
586 data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
587 if (data < 0)
588 return -EIO;
589 break;
590 case V4L2_CID_GAIN:
591 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
592 return -EINVAL;
593 /* See Datasheet Table 7, Gain settings. */
594 if (ctrl->value <= qctrl->default_value) {
595 /* Pack it into 0..1 step 0.125, register values 0..8 */
596 unsigned long range = qctrl->default_value - qctrl->minimum;
597 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
599 dev_dbg(&client->dev, "Setting gain %d\n", data);
600 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
601 if (data < 0)
602 return -EIO;
603 } else {
604 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
605 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
606 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
607 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
608 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
609 1015 + range / 2) / range + 9;
611 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
612 data = gain;
613 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
614 data = ((gain - 32) * 16 + 16) / 32 + 80;
615 else
616 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
617 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
619 dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
620 reg_read(client, MT9T031_GLOBAL_GAIN), data);
621 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
622 if (data < 0)
623 return -EIO;
626 /* Success */
627 icd->gain = ctrl->value;
628 break;
629 case V4L2_CID_EXPOSURE:
630 /* mt9t031 has maximum == default */
631 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
632 return -EINVAL;
633 else {
634 const unsigned long range = qctrl->maximum - qctrl->minimum;
635 const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
636 range / 2) / range + 1;
637 u32 old;
639 get_shutter(client, &old);
640 dev_dbg(&client->dev, "Set shutter from %u to %u\n",
641 old, shutter);
642 if (set_shutter(client, shutter) < 0)
643 return -EIO;
644 icd->exposure = ctrl->value;
645 mt9t031->autoexposure = 0;
647 break;
648 case V4L2_CID_EXPOSURE_AUTO:
649 if (ctrl->value) {
650 const u16 vblank = MT9T031_VERTICAL_BLANK;
651 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
652 if (set_shutter(client, mt9t031->rect.height +
653 icd->y_skip_top + vblank) < 0)
654 return -EIO;
655 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
656 icd->exposure = (shutter_max / 2 +
657 (mt9t031->rect.height +
658 icd->y_skip_top + vblank - 1) *
659 (qctrl->maximum - qctrl->minimum)) /
660 shutter_max + qctrl->minimum;
661 mt9t031->autoexposure = 1;
662 } else
663 mt9t031->autoexposure = 0;
664 break;
666 return 0;
669 /* Interface active, can use i2c. If it fails, it can indeed mean, that
670 * this wasn't our capture interface, so, we wait for the right one */
671 static int mt9t031_video_probe(struct i2c_client *client)
673 struct soc_camera_device *icd = client->dev.platform_data;
674 struct mt9t031 *mt9t031 = to_mt9t031(client);
675 s32 data;
676 int ret;
678 /* Enable the chip */
679 data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
680 dev_dbg(&client->dev, "write: %d\n", data);
682 /* Read out the chip version register */
683 data = reg_read(client, MT9T031_CHIP_VERSION);
685 switch (data) {
686 case 0x1621:
687 mt9t031->model = V4L2_IDENT_MT9T031;
688 icd->formats = mt9t031_colour_formats;
689 icd->num_formats = ARRAY_SIZE(mt9t031_colour_formats);
690 break;
691 default:
692 dev_err(&client->dev,
693 "No MT9T031 chip detected, register read %x\n", data);
694 return -ENODEV;
697 dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
699 ret = mt9t031_idle(client);
700 if (ret < 0)
701 dev_err(&client->dev, "Failed to initialise the camera\n");
703 return ret;
706 static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
707 .g_ctrl = mt9t031_g_ctrl,
708 .s_ctrl = mt9t031_s_ctrl,
709 .g_chip_ident = mt9t031_g_chip_ident,
710 #ifdef CONFIG_VIDEO_ADV_DEBUG
711 .g_register = mt9t031_g_register,
712 .s_register = mt9t031_s_register,
713 #endif
716 static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
717 .s_stream = mt9t031_s_stream,
718 .s_fmt = mt9t031_s_fmt,
719 .g_fmt = mt9t031_g_fmt,
720 .try_fmt = mt9t031_try_fmt,
721 .s_crop = mt9t031_s_crop,
722 .g_crop = mt9t031_g_crop,
723 .cropcap = mt9t031_cropcap,
726 static struct v4l2_subdev_ops mt9t031_subdev_ops = {
727 .core = &mt9t031_subdev_core_ops,
728 .video = &mt9t031_subdev_video_ops,
731 static int mt9t031_probe(struct i2c_client *client,
732 const struct i2c_device_id *did)
734 struct mt9t031 *mt9t031;
735 struct soc_camera_device *icd = client->dev.platform_data;
736 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
737 struct soc_camera_link *icl;
738 int ret;
740 if (!icd) {
741 dev_err(&client->dev, "MT9T031: missing soc-camera data!\n");
742 return -EINVAL;
745 icl = to_soc_camera_link(icd);
746 if (!icl) {
747 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
748 return -EINVAL;
751 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
752 dev_warn(&adapter->dev,
753 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
754 return -EIO;
757 mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
758 if (!mt9t031)
759 return -ENOMEM;
761 v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
763 /* Second stage probe - when a capture adapter is there */
764 icd->ops = &mt9t031_ops;
765 icd->y_skip_top = 0;
767 mt9t031->rect.left = MT9T031_COLUMN_SKIP;
768 mt9t031->rect.top = MT9T031_ROW_SKIP;
769 mt9t031->rect.width = MT9T031_MAX_WIDTH;
770 mt9t031->rect.height = MT9T031_MAX_HEIGHT;
772 /* Simulated autoexposure. If enabled, we calculate shutter width
773 * ourselves in the driver based on vertical blanking and frame width */
774 mt9t031->autoexposure = 1;
776 mt9t031->xskip = 1;
777 mt9t031->yskip = 1;
779 mt9t031_idle(client);
781 ret = mt9t031_video_probe(client);
783 mt9t031_disable(client);
785 if (ret) {
786 icd->ops = NULL;
787 i2c_set_clientdata(client, NULL);
788 kfree(mt9t031);
791 return ret;
794 static int mt9t031_remove(struct i2c_client *client)
796 struct mt9t031 *mt9t031 = to_mt9t031(client);
797 struct soc_camera_device *icd = client->dev.platform_data;
799 icd->ops = NULL;
800 i2c_set_clientdata(client, NULL);
801 client->driver = NULL;
802 kfree(mt9t031);
804 return 0;
807 static const struct i2c_device_id mt9t031_id[] = {
808 { "mt9t031", 0 },
811 MODULE_DEVICE_TABLE(i2c, mt9t031_id);
813 static struct i2c_driver mt9t031_i2c_driver = {
814 .driver = {
815 .name = "mt9t031",
817 .probe = mt9t031_probe,
818 .remove = mt9t031_remove,
819 .id_table = mt9t031_id,
822 static int __init mt9t031_mod_init(void)
824 return i2c_add_driver(&mt9t031_i2c_driver);
827 static void __exit mt9t031_mod_exit(void)
829 i2c_del_driver(&mt9t031_i2c_driver);
832 module_init(mt9t031_mod_init);
833 module_exit(mt9t031_mod_exit);
835 MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
836 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
837 MODULE_LICENSE("GPL v2");