V4L/DVB (13662): mt9t031: make the use of the soc-camera client API optional
[linux-2.6/libata-dev.git] / drivers / media / video / mt9t031.c
bloba9061bff79b2189b6e542beaab7417ff7ed35a3d
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>
21 * mt9t031 i2c address 0x5d
22 * The platform has to define i2c_board_info and link to it from
23 * struct soc_camera_link
26 /* mt9t031 selected register addresses */
27 #define MT9T031_CHIP_VERSION 0x00
28 #define MT9T031_ROW_START 0x01
29 #define MT9T031_COLUMN_START 0x02
30 #define MT9T031_WINDOW_HEIGHT 0x03
31 #define MT9T031_WINDOW_WIDTH 0x04
32 #define MT9T031_HORIZONTAL_BLANKING 0x05
33 #define MT9T031_VERTICAL_BLANKING 0x06
34 #define MT9T031_OUTPUT_CONTROL 0x07
35 #define MT9T031_SHUTTER_WIDTH_UPPER 0x08
36 #define MT9T031_SHUTTER_WIDTH 0x09
37 #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
38 #define MT9T031_FRAME_RESTART 0x0b
39 #define MT9T031_SHUTTER_DELAY 0x0c
40 #define MT9T031_RESET 0x0d
41 #define MT9T031_READ_MODE_1 0x1e
42 #define MT9T031_READ_MODE_2 0x20
43 #define MT9T031_READ_MODE_3 0x21
44 #define MT9T031_ROW_ADDRESS_MODE 0x22
45 #define MT9T031_COLUMN_ADDRESS_MODE 0x23
46 #define MT9T031_GLOBAL_GAIN 0x35
47 #define MT9T031_CHIP_ENABLE 0xF8
49 #define MT9T031_MAX_HEIGHT 1536
50 #define MT9T031_MAX_WIDTH 2048
51 #define MT9T031_MIN_HEIGHT 2
52 #define MT9T031_MIN_WIDTH 18
53 #define MT9T031_HORIZONTAL_BLANK 142
54 #define MT9T031_VERTICAL_BLANK 25
55 #define MT9T031_COLUMN_SKIP 32
56 #define MT9T031_ROW_SKIP 20
58 #define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \
59 SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \
60 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \
61 SOCAM_MASTER | SOCAM_DATAWIDTH_10)
63 struct mt9t031 {
64 struct v4l2_subdev subdev;
65 struct v4l2_rect rect; /* Sensor window */
66 int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
67 u16 xskip;
68 u16 yskip;
69 unsigned int gain;
70 unsigned short y_skip_top; /* Lines to skip at the top */
71 unsigned int exposure;
72 unsigned char autoexposure;
75 static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
77 return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
80 static int reg_read(struct i2c_client *client, const u8 reg)
82 s32 data = i2c_smbus_read_word_data(client, reg);
83 return data < 0 ? data : swab16(data);
86 static int reg_write(struct i2c_client *client, const u8 reg,
87 const u16 data)
89 return i2c_smbus_write_word_data(client, reg, swab16(data));
92 static int reg_set(struct i2c_client *client, const u8 reg,
93 const u16 data)
95 int ret;
97 ret = reg_read(client, reg);
98 if (ret < 0)
99 return ret;
100 return reg_write(client, reg, ret | data);
103 static int reg_clear(struct i2c_client *client, const u8 reg,
104 const u16 data)
106 int ret;
108 ret = reg_read(client, reg);
109 if (ret < 0)
110 return ret;
111 return reg_write(client, reg, ret & ~data);
114 static int set_shutter(struct i2c_client *client, const u32 data)
116 int ret;
118 ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
120 if (ret >= 0)
121 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
123 return ret;
126 static int get_shutter(struct i2c_client *client, u32 *data)
128 int ret;
130 ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
131 *data = ret << 16;
133 if (ret >= 0)
134 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
135 *data |= ret & 0xffff;
137 return ret < 0 ? ret : 0;
140 static int mt9t031_idle(struct i2c_client *client)
142 int ret;
144 /* Disable chip output, synchronous option update */
145 ret = reg_write(client, MT9T031_RESET, 1);
146 if (ret >= 0)
147 ret = reg_write(client, MT9T031_RESET, 0);
148 if (ret >= 0)
149 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
151 return ret >= 0 ? 0 : -EIO;
154 static int mt9t031_disable(struct i2c_client *client)
156 /* Disable the chip */
157 reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
159 return 0;
162 static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
164 struct i2c_client *client = sd->priv;
165 int ret;
167 if (enable)
168 /* Switch to master "normal" mode */
169 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
170 else
171 /* Stop sensor readout */
172 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
174 if (ret < 0)
175 return -EIO;
177 return 0;
180 static int mt9t031_set_bus_param(struct soc_camera_device *icd,
181 unsigned long flags)
183 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
185 /* The caller should have queried our parameters, check anyway */
186 if (flags & ~MT9T031_BUS_PARAM)
187 return -EINVAL;
189 if (flags & SOCAM_PCLK_SAMPLE_FALLING)
190 reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
191 else
192 reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
194 return 0;
197 static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
199 struct soc_camera_link *icl = to_soc_camera_link(icd);
201 return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
204 enum {
205 MT9T031_CTRL_VFLIP,
206 MT9T031_CTRL_HFLIP,
207 MT9T031_CTRL_GAIN,
208 MT9T031_CTRL_EXPOSURE,
209 MT9T031_CTRL_EXPOSURE_AUTO,
212 static const struct v4l2_queryctrl mt9t031_controls[] = {
213 [MT9T031_CTRL_VFLIP] = {
214 .id = V4L2_CID_VFLIP,
215 .type = V4L2_CTRL_TYPE_BOOLEAN,
216 .name = "Flip Vertically",
217 .minimum = 0,
218 .maximum = 1,
219 .step = 1,
220 .default_value = 0,
222 [MT9T031_CTRL_HFLIP] = {
223 .id = V4L2_CID_HFLIP,
224 .type = V4L2_CTRL_TYPE_BOOLEAN,
225 .name = "Flip Horizontally",
226 .minimum = 0,
227 .maximum = 1,
228 .step = 1,
229 .default_value = 0,
231 [MT9T031_CTRL_GAIN] = {
232 .id = V4L2_CID_GAIN,
233 .type = V4L2_CTRL_TYPE_INTEGER,
234 .name = "Gain",
235 .minimum = 0,
236 .maximum = 127,
237 .step = 1,
238 .default_value = 64,
239 .flags = V4L2_CTRL_FLAG_SLIDER,
241 [MT9T031_CTRL_EXPOSURE] = {
242 .id = V4L2_CID_EXPOSURE,
243 .type = V4L2_CTRL_TYPE_INTEGER,
244 .name = "Exposure",
245 .minimum = 1,
246 .maximum = 255,
247 .step = 1,
248 .default_value = 255,
249 .flags = V4L2_CTRL_FLAG_SLIDER,
251 [MT9T031_CTRL_EXPOSURE_AUTO] = {
252 .id = V4L2_CID_EXPOSURE_AUTO,
253 .type = V4L2_CTRL_TYPE_BOOLEAN,
254 .name = "Automatic Exposure",
255 .minimum = 0,
256 .maximum = 1,
257 .step = 1,
258 .default_value = 1,
262 static struct soc_camera_ops mt9t031_ops = {
263 .set_bus_param = mt9t031_set_bus_param,
264 .query_bus_param = mt9t031_query_bus_param,
265 .controls = mt9t031_controls,
266 .num_controls = ARRAY_SIZE(mt9t031_controls),
269 /* target must be _even_ */
270 static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
272 unsigned int skip;
274 if (*source < target + target / 2) {
275 *source = target;
276 return 1;
279 skip = min(max, *source + target / 2) / target;
280 if (skip > 8)
281 skip = 8;
282 *source = target * skip;
284 return skip;
287 /* rect is the sensor rectangle, the caller guarantees parameter validity */
288 static int mt9t031_set_params(struct i2c_client *client,
289 struct v4l2_rect *rect, u16 xskip, u16 yskip)
291 struct mt9t031 *mt9t031 = to_mt9t031(client);
292 int ret;
293 u16 xbin, ybin;
294 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
295 vblank = MT9T031_VERTICAL_BLANK;
297 xbin = min(xskip, (u16)3);
298 ybin = min(yskip, (u16)3);
301 * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
302 * There is always a valid suitably aligned value. The worst case is
303 * xbin = 3, width = 2048. Then we will start at 36, the last read out
304 * pixel will be 2083, which is < 2085 - first black pixel.
306 * MT9T031 datasheet imposes window left border alignment, depending on
307 * the selected xskip. Failing to conform to this requirement produces
308 * dark horizontal stripes in the image. However, even obeying to this
309 * requirement doesn't eliminate the stripes in all configurations. They
310 * appear "locally reproducibly," but can differ between tests under
311 * different lighting conditions.
313 switch (xbin) {
314 case 1:
315 rect->left &= ~1;
316 break;
317 case 2:
318 rect->left &= ~3;
319 break;
320 case 3:
321 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
322 (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
325 rect->top &= ~1;
327 dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
328 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
330 /* Disable register update, reconfigure atomically */
331 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
332 if (ret < 0)
333 return ret;
335 /* Blanking and start values - default... */
336 ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
337 if (ret >= 0)
338 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
340 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
341 /* Binning, skipping */
342 if (ret >= 0)
343 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
344 ((xbin - 1) << 4) | (xskip - 1));
345 if (ret >= 0)
346 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
347 ((ybin - 1) << 4) | (yskip - 1));
349 dev_dbg(&client->dev, "new physical left %u, top %u\n",
350 rect->left, rect->top);
353 * The caller provides a supported format, as guaranteed by
354 * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap()
356 if (ret >= 0)
357 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
358 if (ret >= 0)
359 ret = reg_write(client, MT9T031_ROW_START, rect->top);
360 if (ret >= 0)
361 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
362 if (ret >= 0)
363 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
364 rect->height + mt9t031->y_skip_top - 1);
365 if (ret >= 0 && mt9t031->autoexposure) {
366 unsigned int total_h = rect->height + mt9t031->y_skip_top + vblank;
367 ret = set_shutter(client, total_h);
368 if (ret >= 0) {
369 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
370 const struct v4l2_queryctrl *qctrl =
371 &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
372 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
373 (qctrl->maximum - qctrl->minimum)) /
374 shutter_max + qctrl->minimum;
378 /* Re-enable register update, commit all changes */
379 if (ret >= 0)
380 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
382 if (ret >= 0) {
383 mt9t031->rect = *rect;
384 mt9t031->xskip = xskip;
385 mt9t031->yskip = yskip;
388 return ret < 0 ? ret : 0;
391 static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
393 struct v4l2_rect rect = a->c;
394 struct i2c_client *client = sd->priv;
395 struct mt9t031 *mt9t031 = to_mt9t031(client);
397 rect.width = ALIGN(rect.width, 2);
398 rect.height = ALIGN(rect.height, 2);
400 soc_camera_limit_side(&rect.left, &rect.width,
401 MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
403 soc_camera_limit_side(&rect.top, &rect.height,
404 MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
406 return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
409 static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
411 struct i2c_client *client = sd->priv;
412 struct mt9t031 *mt9t031 = to_mt9t031(client);
414 a->c = mt9t031->rect;
415 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
417 return 0;
420 static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
422 a->bounds.left = MT9T031_COLUMN_SKIP;
423 a->bounds.top = MT9T031_ROW_SKIP;
424 a->bounds.width = MT9T031_MAX_WIDTH;
425 a->bounds.height = MT9T031_MAX_HEIGHT;
426 a->defrect = a->bounds;
427 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
428 a->pixelaspect.numerator = 1;
429 a->pixelaspect.denominator = 1;
431 return 0;
434 static int mt9t031_g_fmt(struct v4l2_subdev *sd,
435 struct v4l2_mbus_framefmt *mf)
437 struct i2c_client *client = sd->priv;
438 struct mt9t031 *mt9t031 = to_mt9t031(client);
440 mf->width = mt9t031->rect.width / mt9t031->xskip;
441 mf->height = mt9t031->rect.height / mt9t031->yskip;
442 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
443 mf->colorspace = V4L2_COLORSPACE_SRGB;
444 mf->field = V4L2_FIELD_NONE;
446 return 0;
449 static int mt9t031_s_fmt(struct v4l2_subdev *sd,
450 struct v4l2_mbus_framefmt *mf)
452 struct i2c_client *client = sd->priv;
453 struct mt9t031 *mt9t031 = to_mt9t031(client);
454 u16 xskip, yskip;
455 struct v4l2_rect rect = mt9t031->rect;
458 * try_fmt has put width and height within limits.
459 * S_FMT: use binning and skipping for scaling
461 xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
462 yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
464 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
465 mf->colorspace = V4L2_COLORSPACE_SRGB;
467 /* mt9t031_set_params() doesn't change width and height */
468 return mt9t031_set_params(client, &rect, xskip, yskip);
472 * If a user window larger than sensor window is requested, we'll increase the
473 * sensor window.
475 static int mt9t031_try_fmt(struct v4l2_subdev *sd,
476 struct v4l2_mbus_framefmt *mf)
478 v4l_bound_align_image(
479 &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
480 &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
482 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
483 mf->colorspace = V4L2_COLORSPACE_SRGB;
485 return 0;
488 static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
489 struct v4l2_dbg_chip_ident *id)
491 struct i2c_client *client = sd->priv;
492 struct mt9t031 *mt9t031 = to_mt9t031(client);
494 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
495 return -EINVAL;
497 if (id->match.addr != client->addr)
498 return -ENODEV;
500 id->ident = mt9t031->model;
501 id->revision = 0;
503 return 0;
506 #ifdef CONFIG_VIDEO_ADV_DEBUG
507 static int mt9t031_g_register(struct v4l2_subdev *sd,
508 struct v4l2_dbg_register *reg)
510 struct i2c_client *client = sd->priv;
512 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
513 return -EINVAL;
515 if (reg->match.addr != client->addr)
516 return -ENODEV;
518 reg->val = reg_read(client, reg->reg);
520 if (reg->val > 0xffff)
521 return -EIO;
523 return 0;
526 static int mt9t031_s_register(struct v4l2_subdev *sd,
527 struct v4l2_dbg_register *reg)
529 struct i2c_client *client = sd->priv;
531 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
532 return -EINVAL;
534 if (reg->match.addr != client->addr)
535 return -ENODEV;
537 if (reg_write(client, reg->reg, reg->val) < 0)
538 return -EIO;
540 return 0;
542 #endif
544 static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
546 struct i2c_client *client = sd->priv;
547 struct mt9t031 *mt9t031 = to_mt9t031(client);
548 int data;
550 switch (ctrl->id) {
551 case V4L2_CID_VFLIP:
552 data = reg_read(client, MT9T031_READ_MODE_2);
553 if (data < 0)
554 return -EIO;
555 ctrl->value = !!(data & 0x8000);
556 break;
557 case V4L2_CID_HFLIP:
558 data = reg_read(client, MT9T031_READ_MODE_2);
559 if (data < 0)
560 return -EIO;
561 ctrl->value = !!(data & 0x4000);
562 break;
563 case V4L2_CID_EXPOSURE_AUTO:
564 ctrl->value = mt9t031->autoexposure;
565 break;
566 case V4L2_CID_GAIN:
567 ctrl->value = mt9t031->gain;
568 break;
569 case V4L2_CID_EXPOSURE:
570 ctrl->value = mt9t031->exposure;
571 break;
573 return 0;
576 static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
578 struct i2c_client *client = sd->priv;
579 struct mt9t031 *mt9t031 = to_mt9t031(client);
580 const struct v4l2_queryctrl *qctrl;
581 int data;
583 switch (ctrl->id) {
584 case V4L2_CID_VFLIP:
585 if (ctrl->value)
586 data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
587 else
588 data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
589 if (data < 0)
590 return -EIO;
591 break;
592 case V4L2_CID_HFLIP:
593 if (ctrl->value)
594 data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
595 else
596 data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
597 if (data < 0)
598 return -EIO;
599 break;
600 case V4L2_CID_GAIN:
601 qctrl = &mt9t031_controls[MT9T031_CTRL_GAIN];
602 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
603 return -EINVAL;
604 /* See Datasheet Table 7, Gain settings. */
605 if (ctrl->value <= qctrl->default_value) {
606 /* Pack it into 0..1 step 0.125, register values 0..8 */
607 unsigned long range = qctrl->default_value - qctrl->minimum;
608 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
610 dev_dbg(&client->dev, "Setting gain %d\n", data);
611 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
612 if (data < 0)
613 return -EIO;
614 } else {
615 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
616 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
617 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
618 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
619 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
620 1015 + range / 2) / range + 9;
622 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
623 data = gain;
624 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
625 data = ((gain - 32) * 16 + 16) / 32 + 80;
626 else
627 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
628 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
630 dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
631 reg_read(client, MT9T031_GLOBAL_GAIN), data);
632 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
633 if (data < 0)
634 return -EIO;
637 /* Success */
638 mt9t031->gain = ctrl->value;
639 break;
640 case V4L2_CID_EXPOSURE:
641 qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
642 /* mt9t031 has maximum == default */
643 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
644 return -EINVAL;
645 else {
646 const unsigned long range = qctrl->maximum - qctrl->minimum;
647 const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
648 range / 2) / range + 1;
649 u32 old;
651 get_shutter(client, &old);
652 dev_dbg(&client->dev, "Set shutter from %u to %u\n",
653 old, shutter);
654 if (set_shutter(client, shutter) < 0)
655 return -EIO;
656 mt9t031->exposure = ctrl->value;
657 mt9t031->autoexposure = 0;
659 break;
660 case V4L2_CID_EXPOSURE_AUTO:
661 if (ctrl->value) {
662 const u16 vblank = MT9T031_VERTICAL_BLANK;
663 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
664 unsigned int total_h = mt9t031->rect.height +
665 mt9t031->y_skip_top + vblank;
667 if (set_shutter(client, total_h) < 0)
668 return -EIO;
669 qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
670 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
671 (qctrl->maximum - qctrl->minimum)) /
672 shutter_max + qctrl->minimum;
673 mt9t031->autoexposure = 1;
674 } else
675 mt9t031->autoexposure = 0;
676 break;
677 default:
678 return -EINVAL;
680 return 0;
684 * Interface active, can use i2c. If it fails, it can indeed mean, that
685 * this wasn't our capture interface, so, we wait for the right one
687 static int mt9t031_video_probe(struct i2c_client *client)
689 struct mt9t031 *mt9t031 = to_mt9t031(client);
690 s32 data;
691 int ret;
693 /* Enable the chip */
694 data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
695 dev_dbg(&client->dev, "write: %d\n", data);
697 /* Read out the chip version register */
698 data = reg_read(client, MT9T031_CHIP_VERSION);
700 switch (data) {
701 case 0x1621:
702 mt9t031->model = V4L2_IDENT_MT9T031;
703 break;
704 default:
705 dev_err(&client->dev,
706 "No MT9T031 chip detected, register read %x\n", data);
707 return -ENODEV;
710 dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
712 ret = mt9t031_idle(client);
713 if (ret < 0)
714 dev_err(&client->dev, "Failed to initialise the camera\n");
716 /* mt9t031_idle() has reset the chip to default. */
717 mt9t031->exposure = 255;
718 mt9t031->gain = 64;
720 return ret;
723 static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
725 struct i2c_client *client = sd->priv;
726 struct mt9t031 *mt9t031 = to_mt9t031(client);
728 *lines = mt9t031->y_skip_top;
730 return 0;
733 static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
734 .g_ctrl = mt9t031_g_ctrl,
735 .s_ctrl = mt9t031_s_ctrl,
736 .g_chip_ident = mt9t031_g_chip_ident,
737 #ifdef CONFIG_VIDEO_ADV_DEBUG
738 .g_register = mt9t031_g_register,
739 .s_register = mt9t031_s_register,
740 #endif
743 static int mt9t031_enum_fmt(struct v4l2_subdev *sd, int index,
744 enum v4l2_mbus_pixelcode *code)
746 if (index)
747 return -EINVAL;
749 *code = V4L2_MBUS_FMT_SBGGR10_1X10;
750 return 0;
753 static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
754 .s_stream = mt9t031_s_stream,
755 .s_mbus_fmt = mt9t031_s_fmt,
756 .g_mbus_fmt = mt9t031_g_fmt,
757 .try_mbus_fmt = mt9t031_try_fmt,
758 .s_crop = mt9t031_s_crop,
759 .g_crop = mt9t031_g_crop,
760 .cropcap = mt9t031_cropcap,
761 .enum_mbus_fmt = mt9t031_enum_fmt,
764 static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
765 .g_skip_top_lines = mt9t031_g_skip_top_lines,
768 static struct v4l2_subdev_ops mt9t031_subdev_ops = {
769 .core = &mt9t031_subdev_core_ops,
770 .video = &mt9t031_subdev_video_ops,
771 .sensor = &mt9t031_subdev_sensor_ops,
774 static int mt9t031_probe(struct i2c_client *client,
775 const struct i2c_device_id *did)
777 struct mt9t031 *mt9t031;
778 struct soc_camera_device *icd = client->dev.platform_data;
779 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
780 int ret;
782 if (icd) {
783 struct soc_camera_link *icl = to_soc_camera_link(icd);
784 if (!icl) {
785 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
786 return -EINVAL;
789 icd->ops = &mt9t031_ops;
792 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
793 dev_warn(&adapter->dev,
794 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
795 return -EIO;
798 mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
799 if (!mt9t031)
800 return -ENOMEM;
802 v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
804 mt9t031->y_skip_top = 0;
805 mt9t031->rect.left = MT9T031_COLUMN_SKIP;
806 mt9t031->rect.top = MT9T031_ROW_SKIP;
807 mt9t031->rect.width = MT9T031_MAX_WIDTH;
808 mt9t031->rect.height = MT9T031_MAX_HEIGHT;
811 * Simulated autoexposure. If enabled, we calculate shutter width
812 * ourselves in the driver based on vertical blanking and frame width
814 mt9t031->autoexposure = 1;
816 mt9t031->xskip = 1;
817 mt9t031->yskip = 1;
819 mt9t031_idle(client);
821 ret = mt9t031_video_probe(client);
823 mt9t031_disable(client);
825 if (ret) {
826 if (icd)
827 icd->ops = NULL;
828 i2c_set_clientdata(client, NULL);
829 kfree(mt9t031);
832 return ret;
835 static int mt9t031_remove(struct i2c_client *client)
837 struct mt9t031 *mt9t031 = to_mt9t031(client);
838 struct soc_camera_device *icd = client->dev.platform_data;
840 if (icd)
841 icd->ops = NULL;
842 i2c_set_clientdata(client, NULL);
843 client->driver = NULL;
844 kfree(mt9t031);
846 return 0;
849 static const struct i2c_device_id mt9t031_id[] = {
850 { "mt9t031", 0 },
853 MODULE_DEVICE_TABLE(i2c, mt9t031_id);
855 static struct i2c_driver mt9t031_i2c_driver = {
856 .driver = {
857 .name = "mt9t031",
859 .probe = mt9t031_probe,
860 .remove = mt9t031_remove,
861 .id_table = mt9t031_id,
864 static int __init mt9t031_mod_init(void)
866 return i2c_add_driver(&mt9t031_i2c_driver);
869 static void __exit mt9t031_mod_exit(void)
871 i2c_del_driver(&mt9t031_i2c_driver);
874 module_init(mt9t031_mod_init);
875 module_exit(mt9t031_mod_exit);
877 MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
878 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
879 MODULE_LICENSE("GPL v2");