Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / media / video / mt9t031.c
blobe6e0238eca16a4bb5b1f6ddfabe2e71a323bec88
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/device.h>
12 #include <linux/i2c.h>
13 #include <linux/log2.h>
14 #include <linux/pm.h>
15 #include <linux/slab.h>
16 #include <linux/videodev2.h>
17 #include <linux/module.h>
19 #include <media/soc_camera.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/v4l2-subdev.h>
24 * mt9t031 i2c address 0x5d
25 * The platform has to define i2c_board_info and link to it from
26 * struct soc_camera_link
29 /* mt9t031 selected register addresses */
30 #define MT9T031_CHIP_VERSION 0x00
31 #define MT9T031_ROW_START 0x01
32 #define MT9T031_COLUMN_START 0x02
33 #define MT9T031_WINDOW_HEIGHT 0x03
34 #define MT9T031_WINDOW_WIDTH 0x04
35 #define MT9T031_HORIZONTAL_BLANKING 0x05
36 #define MT9T031_VERTICAL_BLANKING 0x06
37 #define MT9T031_OUTPUT_CONTROL 0x07
38 #define MT9T031_SHUTTER_WIDTH_UPPER 0x08
39 #define MT9T031_SHUTTER_WIDTH 0x09
40 #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
41 #define MT9T031_FRAME_RESTART 0x0b
42 #define MT9T031_SHUTTER_DELAY 0x0c
43 #define MT9T031_RESET 0x0d
44 #define MT9T031_READ_MODE_1 0x1e
45 #define MT9T031_READ_MODE_2 0x20
46 #define MT9T031_READ_MODE_3 0x21
47 #define MT9T031_ROW_ADDRESS_MODE 0x22
48 #define MT9T031_COLUMN_ADDRESS_MODE 0x23
49 #define MT9T031_GLOBAL_GAIN 0x35
50 #define MT9T031_CHIP_ENABLE 0xF8
52 #define MT9T031_MAX_HEIGHT 1536
53 #define MT9T031_MAX_WIDTH 2048
54 #define MT9T031_MIN_HEIGHT 2
55 #define MT9T031_MIN_WIDTH 18
56 #define MT9T031_HORIZONTAL_BLANK 142
57 #define MT9T031_VERTICAL_BLANK 25
58 #define MT9T031_COLUMN_SKIP 32
59 #define MT9T031_ROW_SKIP 20
61 #define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \
62 SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \
63 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \
64 SOCAM_MASTER | SOCAM_DATAWIDTH_10)
66 struct mt9t031 {
67 struct v4l2_subdev subdev;
68 struct v4l2_rect rect; /* Sensor window */
69 int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
70 u16 xskip;
71 u16 yskip;
72 unsigned int gain;
73 unsigned short y_skip_top; /* Lines to skip at the top */
74 unsigned int exposure;
75 unsigned char autoexposure;
78 static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
80 return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
83 static int reg_read(struct i2c_client *client, const u8 reg)
85 s32 data = i2c_smbus_read_word_data(client, reg);
86 return data < 0 ? data : swab16(data);
89 static int reg_write(struct i2c_client *client, const u8 reg,
90 const u16 data)
92 return i2c_smbus_write_word_data(client, reg, swab16(data));
95 static int reg_set(struct i2c_client *client, const u8 reg,
96 const u16 data)
98 int ret;
100 ret = reg_read(client, reg);
101 if (ret < 0)
102 return ret;
103 return reg_write(client, reg, ret | data);
106 static int reg_clear(struct i2c_client *client, const u8 reg,
107 const u16 data)
109 int ret;
111 ret = reg_read(client, reg);
112 if (ret < 0)
113 return ret;
114 return reg_write(client, reg, ret & ~data);
117 static int set_shutter(struct i2c_client *client, const u32 data)
119 int ret;
121 ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
123 if (ret >= 0)
124 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
126 return ret;
129 static int get_shutter(struct i2c_client *client, u32 *data)
131 int ret;
133 ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
134 *data = ret << 16;
136 if (ret >= 0)
137 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
138 *data |= ret & 0xffff;
140 return ret < 0 ? ret : 0;
143 static int mt9t031_idle(struct i2c_client *client)
145 int ret;
147 /* Disable chip output, synchronous option update */
148 ret = reg_write(client, MT9T031_RESET, 1);
149 if (ret >= 0)
150 ret = reg_write(client, MT9T031_RESET, 0);
151 if (ret >= 0)
152 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
154 return ret >= 0 ? 0 : -EIO;
157 static int mt9t031_disable(struct i2c_client *client)
159 /* Disable the chip */
160 reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
162 return 0;
165 static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
167 struct i2c_client *client = v4l2_get_subdevdata(sd);
168 int ret;
170 if (enable)
171 /* Switch to master "normal" mode */
172 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
173 else
174 /* Stop sensor readout */
175 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
177 if (ret < 0)
178 return -EIO;
180 return 0;
183 static int mt9t031_set_bus_param(struct soc_camera_device *icd,
184 unsigned long flags)
186 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
188 /* The caller should have queried our parameters, check anyway */
189 if (flags & ~MT9T031_BUS_PARAM)
190 return -EINVAL;
192 if (flags & SOCAM_PCLK_SAMPLE_FALLING)
193 reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
194 else
195 reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
197 return 0;
200 static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
202 struct soc_camera_link *icl = to_soc_camera_link(icd);
204 return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
207 enum {
208 MT9T031_CTRL_VFLIP,
209 MT9T031_CTRL_HFLIP,
210 MT9T031_CTRL_GAIN,
211 MT9T031_CTRL_EXPOSURE,
212 MT9T031_CTRL_EXPOSURE_AUTO,
215 static const struct v4l2_queryctrl mt9t031_controls[] = {
216 [MT9T031_CTRL_VFLIP] = {
217 .id = V4L2_CID_VFLIP,
218 .type = V4L2_CTRL_TYPE_BOOLEAN,
219 .name = "Flip Vertically",
220 .minimum = 0,
221 .maximum = 1,
222 .step = 1,
223 .default_value = 0,
225 [MT9T031_CTRL_HFLIP] = {
226 .id = V4L2_CID_HFLIP,
227 .type = V4L2_CTRL_TYPE_BOOLEAN,
228 .name = "Flip Horizontally",
229 .minimum = 0,
230 .maximum = 1,
231 .step = 1,
232 .default_value = 0,
234 [MT9T031_CTRL_GAIN] = {
235 .id = V4L2_CID_GAIN,
236 .type = V4L2_CTRL_TYPE_INTEGER,
237 .name = "Gain",
238 .minimum = 0,
239 .maximum = 127,
240 .step = 1,
241 .default_value = 64,
242 .flags = V4L2_CTRL_FLAG_SLIDER,
244 [MT9T031_CTRL_EXPOSURE] = {
245 .id = V4L2_CID_EXPOSURE,
246 .type = V4L2_CTRL_TYPE_INTEGER,
247 .name = "Exposure",
248 .minimum = 1,
249 .maximum = 255,
250 .step = 1,
251 .default_value = 255,
252 .flags = V4L2_CTRL_FLAG_SLIDER,
254 [MT9T031_CTRL_EXPOSURE_AUTO] = {
255 .id = V4L2_CID_EXPOSURE_AUTO,
256 .type = V4L2_CTRL_TYPE_BOOLEAN,
257 .name = "Automatic Exposure",
258 .minimum = 0,
259 .maximum = 1,
260 .step = 1,
261 .default_value = 1,
265 static struct soc_camera_ops mt9t031_ops = {
266 .set_bus_param = mt9t031_set_bus_param,
267 .query_bus_param = mt9t031_query_bus_param,
268 .controls = mt9t031_controls,
269 .num_controls = ARRAY_SIZE(mt9t031_controls),
272 /* target must be _even_ */
273 static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
275 unsigned int skip;
277 if (*source < target + target / 2) {
278 *source = target;
279 return 1;
282 skip = min(max, *source + target / 2) / target;
283 if (skip > 8)
284 skip = 8;
285 *source = target * skip;
287 return skip;
290 /* rect is the sensor rectangle, the caller guarantees parameter validity */
291 static int mt9t031_set_params(struct i2c_client *client,
292 struct v4l2_rect *rect, u16 xskip, u16 yskip)
294 struct mt9t031 *mt9t031 = to_mt9t031(client);
295 int ret;
296 u16 xbin, ybin;
297 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
298 vblank = MT9T031_VERTICAL_BLANK;
300 xbin = min(xskip, (u16)3);
301 ybin = min(yskip, (u16)3);
304 * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
305 * There is always a valid suitably aligned value. The worst case is
306 * xbin = 3, width = 2048. Then we will start at 36, the last read out
307 * pixel will be 2083, which is < 2085 - first black pixel.
309 * MT9T031 datasheet imposes window left border alignment, depending on
310 * the selected xskip. Failing to conform to this requirement produces
311 * dark horizontal stripes in the image. However, even obeying to this
312 * requirement doesn't eliminate the stripes in all configurations. They
313 * appear "locally reproducibly," but can differ between tests under
314 * different lighting conditions.
316 switch (xbin) {
317 case 1:
318 rect->left &= ~1;
319 break;
320 case 2:
321 rect->left &= ~3;
322 break;
323 case 3:
324 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
325 (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
328 rect->top &= ~1;
330 dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
331 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
333 /* Disable register update, reconfigure atomically */
334 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
335 if (ret < 0)
336 return ret;
338 /* Blanking and start values - default... */
339 ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
340 if (ret >= 0)
341 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
343 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
344 /* Binning, skipping */
345 if (ret >= 0)
346 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
347 ((xbin - 1) << 4) | (xskip - 1));
348 if (ret >= 0)
349 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
350 ((ybin - 1) << 4) | (yskip - 1));
352 dev_dbg(&client->dev, "new physical left %u, top %u\n",
353 rect->left, rect->top);
356 * The caller provides a supported format, as guaranteed by
357 * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap()
359 if (ret >= 0)
360 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
361 if (ret >= 0)
362 ret = reg_write(client, MT9T031_ROW_START, rect->top);
363 if (ret >= 0)
364 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
365 if (ret >= 0)
366 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
367 rect->height + mt9t031->y_skip_top - 1);
368 if (ret >= 0 && mt9t031->autoexposure) {
369 unsigned int total_h = rect->height + mt9t031->y_skip_top + vblank;
370 ret = set_shutter(client, total_h);
371 if (ret >= 0) {
372 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
373 const struct v4l2_queryctrl *qctrl =
374 &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
375 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
376 (qctrl->maximum - qctrl->minimum)) /
377 shutter_max + qctrl->minimum;
381 /* Re-enable register update, commit all changes */
382 if (ret >= 0)
383 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
385 if (ret >= 0) {
386 mt9t031->rect = *rect;
387 mt9t031->xskip = xskip;
388 mt9t031->yskip = yskip;
391 return ret < 0 ? ret : 0;
394 static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
396 struct v4l2_rect rect = a->c;
397 struct i2c_client *client = v4l2_get_subdevdata(sd);
398 struct mt9t031 *mt9t031 = to_mt9t031(client);
400 rect.width = ALIGN(rect.width, 2);
401 rect.height = ALIGN(rect.height, 2);
403 soc_camera_limit_side(&rect.left, &rect.width,
404 MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
406 soc_camera_limit_side(&rect.top, &rect.height,
407 MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
409 return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
412 static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
414 struct i2c_client *client = v4l2_get_subdevdata(sd);
415 struct mt9t031 *mt9t031 = to_mt9t031(client);
417 a->c = mt9t031->rect;
418 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
420 return 0;
423 static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
425 a->bounds.left = MT9T031_COLUMN_SKIP;
426 a->bounds.top = MT9T031_ROW_SKIP;
427 a->bounds.width = MT9T031_MAX_WIDTH;
428 a->bounds.height = MT9T031_MAX_HEIGHT;
429 a->defrect = a->bounds;
430 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
431 a->pixelaspect.numerator = 1;
432 a->pixelaspect.denominator = 1;
434 return 0;
437 static int mt9t031_g_fmt(struct v4l2_subdev *sd,
438 struct v4l2_mbus_framefmt *mf)
440 struct i2c_client *client = v4l2_get_subdevdata(sd);
441 struct mt9t031 *mt9t031 = to_mt9t031(client);
443 mf->width = mt9t031->rect.width / mt9t031->xskip;
444 mf->height = mt9t031->rect.height / mt9t031->yskip;
445 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
446 mf->colorspace = V4L2_COLORSPACE_SRGB;
447 mf->field = V4L2_FIELD_NONE;
449 return 0;
452 static int mt9t031_s_fmt(struct v4l2_subdev *sd,
453 struct v4l2_mbus_framefmt *mf)
455 struct i2c_client *client = v4l2_get_subdevdata(sd);
456 struct mt9t031 *mt9t031 = to_mt9t031(client);
457 u16 xskip, yskip;
458 struct v4l2_rect rect = mt9t031->rect;
461 * try_fmt has put width and height within limits.
462 * S_FMT: use binning and skipping for scaling
464 xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
465 yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
467 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
468 mf->colorspace = V4L2_COLORSPACE_SRGB;
470 /* mt9t031_set_params() doesn't change width and height */
471 return mt9t031_set_params(client, &rect, xskip, yskip);
475 * If a user window larger than sensor window is requested, we'll increase the
476 * sensor window.
478 static int mt9t031_try_fmt(struct v4l2_subdev *sd,
479 struct v4l2_mbus_framefmt *mf)
481 v4l_bound_align_image(
482 &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
483 &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
485 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
486 mf->colorspace = V4L2_COLORSPACE_SRGB;
488 return 0;
491 static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
492 struct v4l2_dbg_chip_ident *id)
494 struct i2c_client *client = v4l2_get_subdevdata(sd);
495 struct mt9t031 *mt9t031 = to_mt9t031(client);
497 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
498 return -EINVAL;
500 if (id->match.addr != client->addr)
501 return -ENODEV;
503 id->ident = mt9t031->model;
504 id->revision = 0;
506 return 0;
509 #ifdef CONFIG_VIDEO_ADV_DEBUG
510 static int mt9t031_g_register(struct v4l2_subdev *sd,
511 struct v4l2_dbg_register *reg)
513 struct i2c_client *client = v4l2_get_subdevdata(sd);
515 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
516 return -EINVAL;
518 if (reg->match.addr != client->addr)
519 return -ENODEV;
521 reg->val = reg_read(client, reg->reg);
523 if (reg->val > 0xffff)
524 return -EIO;
526 return 0;
529 static int mt9t031_s_register(struct v4l2_subdev *sd,
530 struct v4l2_dbg_register *reg)
532 struct i2c_client *client = v4l2_get_subdevdata(sd);
534 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
535 return -EINVAL;
537 if (reg->match.addr != client->addr)
538 return -ENODEV;
540 if (reg_write(client, reg->reg, reg->val) < 0)
541 return -EIO;
543 return 0;
545 #endif
547 static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
549 struct i2c_client *client = v4l2_get_subdevdata(sd);
550 struct mt9t031 *mt9t031 = to_mt9t031(client);
551 int data;
553 switch (ctrl->id) {
554 case V4L2_CID_VFLIP:
555 data = reg_read(client, MT9T031_READ_MODE_2);
556 if (data < 0)
557 return -EIO;
558 ctrl->value = !!(data & 0x8000);
559 break;
560 case V4L2_CID_HFLIP:
561 data = reg_read(client, MT9T031_READ_MODE_2);
562 if (data < 0)
563 return -EIO;
564 ctrl->value = !!(data & 0x4000);
565 break;
566 case V4L2_CID_EXPOSURE_AUTO:
567 ctrl->value = mt9t031->autoexposure;
568 break;
569 case V4L2_CID_GAIN:
570 ctrl->value = mt9t031->gain;
571 break;
572 case V4L2_CID_EXPOSURE:
573 ctrl->value = mt9t031->exposure;
574 break;
576 return 0;
579 static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
581 struct i2c_client *client = v4l2_get_subdevdata(sd);
582 struct mt9t031 *mt9t031 = to_mt9t031(client);
583 const struct v4l2_queryctrl *qctrl;
584 int data;
586 switch (ctrl->id) {
587 case V4L2_CID_VFLIP:
588 if (ctrl->value)
589 data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
590 else
591 data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
592 if (data < 0)
593 return -EIO;
594 break;
595 case V4L2_CID_HFLIP:
596 if (ctrl->value)
597 data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
598 else
599 data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
600 if (data < 0)
601 return -EIO;
602 break;
603 case V4L2_CID_GAIN:
604 qctrl = &mt9t031_controls[MT9T031_CTRL_GAIN];
605 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
606 return -EINVAL;
607 /* See Datasheet Table 7, Gain settings. */
608 if (ctrl->value <= qctrl->default_value) {
609 /* Pack it into 0..1 step 0.125, register values 0..8 */
610 unsigned long range = qctrl->default_value - qctrl->minimum;
611 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
613 dev_dbg(&client->dev, "Setting gain %d\n", data);
614 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
615 if (data < 0)
616 return -EIO;
617 } else {
618 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
619 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
620 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
621 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
622 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
623 1015 + range / 2) / range + 9;
625 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
626 data = gain;
627 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
628 data = ((gain - 32) * 16 + 16) / 32 + 80;
629 else
630 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
631 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
633 dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
634 reg_read(client, MT9T031_GLOBAL_GAIN), data);
635 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
636 if (data < 0)
637 return -EIO;
640 /* Success */
641 mt9t031->gain = ctrl->value;
642 break;
643 case V4L2_CID_EXPOSURE:
644 qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
645 /* mt9t031 has maximum == default */
646 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
647 return -EINVAL;
648 else {
649 const unsigned long range = qctrl->maximum - qctrl->minimum;
650 const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
651 range / 2) / range + 1;
652 u32 old;
654 get_shutter(client, &old);
655 dev_dbg(&client->dev, "Set shutter from %u to %u\n",
656 old, shutter);
657 if (set_shutter(client, shutter) < 0)
658 return -EIO;
659 mt9t031->exposure = ctrl->value;
660 mt9t031->autoexposure = 0;
662 break;
663 case V4L2_CID_EXPOSURE_AUTO:
664 if (ctrl->value) {
665 const u16 vblank = MT9T031_VERTICAL_BLANK;
666 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
667 unsigned int total_h = mt9t031->rect.height +
668 mt9t031->y_skip_top + vblank;
670 if (set_shutter(client, total_h) < 0)
671 return -EIO;
672 qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
673 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
674 (qctrl->maximum - qctrl->minimum)) /
675 shutter_max + qctrl->minimum;
676 mt9t031->autoexposure = 1;
677 } else
678 mt9t031->autoexposure = 0;
679 break;
680 default:
681 return -EINVAL;
683 return 0;
687 * Power Management:
688 * This function does nothing for now but must be present for pm to work
690 static int mt9t031_runtime_suspend(struct device *dev)
692 return 0;
696 * Power Management:
697 * COLUMN_ADDRESS_MODE and ROW_ADDRESS_MODE are not rewritten if unchanged
698 * they are however changed at reset if the platform hook is present
699 * thus we rewrite them with the values stored by the driver
701 static int mt9t031_runtime_resume(struct device *dev)
703 struct video_device *vdev = to_video_device(dev);
704 struct soc_camera_device *icd = dev_get_drvdata(vdev->parent);
705 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
706 struct i2c_client *client = v4l2_get_subdevdata(sd);
707 struct mt9t031 *mt9t031 = to_mt9t031(client);
709 int ret;
710 u16 xbin, ybin;
712 xbin = min(mt9t031->xskip, (u16)3);
713 ybin = min(mt9t031->yskip, (u16)3);
715 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
716 ((xbin - 1) << 4) | (mt9t031->xskip - 1));
717 if (ret < 0)
718 return ret;
720 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
721 ((ybin - 1) << 4) | (mt9t031->yskip - 1));
722 if (ret < 0)
723 return ret;
725 return 0;
728 static struct dev_pm_ops mt9t031_dev_pm_ops = {
729 .runtime_suspend = mt9t031_runtime_suspend,
730 .runtime_resume = mt9t031_runtime_resume,
733 static struct device_type mt9t031_dev_type = {
734 .name = "MT9T031",
735 .pm = &mt9t031_dev_pm_ops,
739 * Interface active, can use i2c. If it fails, it can indeed mean, that
740 * this wasn't our capture interface, so, we wait for the right one
742 static int mt9t031_video_probe(struct i2c_client *client)
744 struct mt9t031 *mt9t031 = to_mt9t031(client);
745 struct video_device *vdev = soc_camera_i2c_to_vdev(client);
746 s32 data;
747 int ret;
749 /* Enable the chip */
750 data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
751 dev_dbg(&client->dev, "write: %d\n", data);
753 /* Read out the chip version register */
754 data = reg_read(client, MT9T031_CHIP_VERSION);
756 switch (data) {
757 case 0x1621:
758 mt9t031->model = V4L2_IDENT_MT9T031;
759 break;
760 default:
761 dev_err(&client->dev,
762 "No MT9T031 chip detected, register read %x\n", data);
763 return -ENODEV;
766 dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
768 ret = mt9t031_idle(client);
769 if (ret < 0)
770 dev_err(&client->dev, "Failed to initialise the camera\n");
771 else
772 vdev->dev.type = &mt9t031_dev_type;
774 /* mt9t031_idle() has reset the chip to default. */
775 mt9t031->exposure = 255;
776 mt9t031->gain = 64;
778 return ret;
781 static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
783 struct i2c_client *client = v4l2_get_subdevdata(sd);
784 struct mt9t031 *mt9t031 = to_mt9t031(client);
786 *lines = mt9t031->y_skip_top;
788 return 0;
791 static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
792 .g_ctrl = mt9t031_g_ctrl,
793 .s_ctrl = mt9t031_s_ctrl,
794 .g_chip_ident = mt9t031_g_chip_ident,
795 #ifdef CONFIG_VIDEO_ADV_DEBUG
796 .g_register = mt9t031_g_register,
797 .s_register = mt9t031_s_register,
798 #endif
801 static int mt9t031_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
802 enum v4l2_mbus_pixelcode *code)
804 if (index)
805 return -EINVAL;
807 *code = V4L2_MBUS_FMT_SBGGR10_1X10;
808 return 0;
811 static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
812 .s_stream = mt9t031_s_stream,
813 .s_mbus_fmt = mt9t031_s_fmt,
814 .g_mbus_fmt = mt9t031_g_fmt,
815 .try_mbus_fmt = mt9t031_try_fmt,
816 .s_crop = mt9t031_s_crop,
817 .g_crop = mt9t031_g_crop,
818 .cropcap = mt9t031_cropcap,
819 .enum_mbus_fmt = mt9t031_enum_fmt,
822 static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
823 .g_skip_top_lines = mt9t031_g_skip_top_lines,
826 static struct v4l2_subdev_ops mt9t031_subdev_ops = {
827 .core = &mt9t031_subdev_core_ops,
828 .video = &mt9t031_subdev_video_ops,
829 .sensor = &mt9t031_subdev_sensor_ops,
832 static int mt9t031_probe(struct i2c_client *client,
833 const struct i2c_device_id *did)
835 struct mt9t031 *mt9t031;
836 struct soc_camera_device *icd = client->dev.platform_data;
837 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
838 int ret;
840 if (icd) {
841 struct soc_camera_link *icl = to_soc_camera_link(icd);
842 if (!icl) {
843 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
844 return -EINVAL;
847 icd->ops = &mt9t031_ops;
850 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
851 dev_warn(&adapter->dev,
852 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
853 return -EIO;
856 mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
857 if (!mt9t031)
858 return -ENOMEM;
860 v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
862 mt9t031->y_skip_top = 0;
863 mt9t031->rect.left = MT9T031_COLUMN_SKIP;
864 mt9t031->rect.top = MT9T031_ROW_SKIP;
865 mt9t031->rect.width = MT9T031_MAX_WIDTH;
866 mt9t031->rect.height = MT9T031_MAX_HEIGHT;
869 * Simulated autoexposure. If enabled, we calculate shutter width
870 * ourselves in the driver based on vertical blanking and frame width
872 mt9t031->autoexposure = 1;
874 mt9t031->xskip = 1;
875 mt9t031->yskip = 1;
877 mt9t031_idle(client);
879 ret = mt9t031_video_probe(client);
881 mt9t031_disable(client);
883 if (ret) {
884 if (icd)
885 icd->ops = NULL;
886 kfree(mt9t031);
889 return ret;
892 static int mt9t031_remove(struct i2c_client *client)
894 struct mt9t031 *mt9t031 = to_mt9t031(client);
895 struct soc_camera_device *icd = client->dev.platform_data;
897 if (icd)
898 icd->ops = NULL;
899 kfree(mt9t031);
901 return 0;
904 static const struct i2c_device_id mt9t031_id[] = {
905 { "mt9t031", 0 },
908 MODULE_DEVICE_TABLE(i2c, mt9t031_id);
910 static struct i2c_driver mt9t031_i2c_driver = {
911 .driver = {
912 .name = "mt9t031",
914 .probe = mt9t031_probe,
915 .remove = mt9t031_remove,
916 .id_table = mt9t031_id,
919 static int __init mt9t031_mod_init(void)
921 return i2c_add_driver(&mt9t031_i2c_driver);
924 static void __exit mt9t031_mod_exit(void)
926 i2c_del_driver(&mt9t031_i2c_driver);
929 module_init(mt9t031_mod_init);
930 module_exit(mt9t031_mod_exit);
932 MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
933 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
934 MODULE_LICENSE("GPL v2");