2 * Driver for MT9M111/MT9M112 CMOS Image Sensor from Micron
4 * Copyright (C) 2008, Robert Jarzmik <robert.jarzmik@free.fr>
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.
10 #include <linux/videodev2.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13 #include <linux/log2.h>
14 #include <linux/gpio.h>
15 #include <linux/delay.h>
17 #include <media/v4l2-common.h>
18 #include <media/v4l2-chip-ident.h>
19 #include <media/soc_camera.h>
22 * mt9m111 and mt9m112 i2c address is 0x5d or 0x48 (depending on SAddr pin)
23 * The platform has to define i2c_board_info and call i2c_register_board_info()
26 /* mt9m111: Sensor register addresses */
27 #define MT9M111_CHIP_VERSION 0x000
28 #define MT9M111_ROW_START 0x001
29 #define MT9M111_COLUMN_START 0x002
30 #define MT9M111_WINDOW_HEIGHT 0x003
31 #define MT9M111_WINDOW_WIDTH 0x004
32 #define MT9M111_HORIZONTAL_BLANKING_B 0x005
33 #define MT9M111_VERTICAL_BLANKING_B 0x006
34 #define MT9M111_HORIZONTAL_BLANKING_A 0x007
35 #define MT9M111_VERTICAL_BLANKING_A 0x008
36 #define MT9M111_SHUTTER_WIDTH 0x009
37 #define MT9M111_ROW_SPEED 0x00a
38 #define MT9M111_EXTRA_DELAY 0x00b
39 #define MT9M111_SHUTTER_DELAY 0x00c
40 #define MT9M111_RESET 0x00d
41 #define MT9M111_READ_MODE_B 0x020
42 #define MT9M111_READ_MODE_A 0x021
43 #define MT9M111_FLASH_CONTROL 0x023
44 #define MT9M111_GREEN1_GAIN 0x02b
45 #define MT9M111_BLUE_GAIN 0x02c
46 #define MT9M111_RED_GAIN 0x02d
47 #define MT9M111_GREEN2_GAIN 0x02e
48 #define MT9M111_GLOBAL_GAIN 0x02f
49 #define MT9M111_CONTEXT_CONTROL 0x0c8
50 #define MT9M111_PAGE_MAP 0x0f0
51 #define MT9M111_BYTE_WISE_ADDR 0x0f1
53 #define MT9M111_RESET_SYNC_CHANGES (1 << 15)
54 #define MT9M111_RESET_RESTART_BAD_FRAME (1 << 9)
55 #define MT9M111_RESET_SHOW_BAD_FRAMES (1 << 8)
56 #define MT9M111_RESET_RESET_SOC (1 << 5)
57 #define MT9M111_RESET_OUTPUT_DISABLE (1 << 4)
58 #define MT9M111_RESET_CHIP_ENABLE (1 << 3)
59 #define MT9M111_RESET_ANALOG_STANDBY (1 << 2)
60 #define MT9M111_RESET_RESTART_FRAME (1 << 1)
61 #define MT9M111_RESET_RESET_MODE (1 << 0)
63 #define MT9M111_RMB_MIRROR_COLS (1 << 1)
64 #define MT9M111_RMB_MIRROR_ROWS (1 << 0)
65 #define MT9M111_CTXT_CTRL_RESTART (1 << 15)
66 #define MT9M111_CTXT_CTRL_DEFECTCOR_B (1 << 12)
67 #define MT9M111_CTXT_CTRL_RESIZE_B (1 << 10)
68 #define MT9M111_CTXT_CTRL_CTRL2_B (1 << 9)
69 #define MT9M111_CTXT_CTRL_GAMMA_B (1 << 8)
70 #define MT9M111_CTXT_CTRL_XENON_EN (1 << 7)
71 #define MT9M111_CTXT_CTRL_READ_MODE_B (1 << 3)
72 #define MT9M111_CTXT_CTRL_LED_FLASH_EN (1 << 2)
73 #define MT9M111_CTXT_CTRL_VBLANK_SEL_B (1 << 1)
74 #define MT9M111_CTXT_CTRL_HBLANK_SEL_B (1 << 0)
76 * mt9m111: Colorpipe register addresses (0x100..0x1ff)
78 #define MT9M111_OPER_MODE_CTRL 0x106
79 #define MT9M111_OUTPUT_FORMAT_CTRL 0x108
80 #define MT9M111_REDUCER_XZOOM_B 0x1a0
81 #define MT9M111_REDUCER_XSIZE_B 0x1a1
82 #define MT9M111_REDUCER_YZOOM_B 0x1a3
83 #define MT9M111_REDUCER_YSIZE_B 0x1a4
84 #define MT9M111_REDUCER_XZOOM_A 0x1a6
85 #define MT9M111_REDUCER_XSIZE_A 0x1a7
86 #define MT9M111_REDUCER_YZOOM_A 0x1a9
87 #define MT9M111_REDUCER_YSIZE_A 0x1aa
89 #define MT9M111_OUTPUT_FORMAT_CTRL2_A 0x13a
90 #define MT9M111_OUTPUT_FORMAT_CTRL2_B 0x19b
92 #define MT9M111_OPMODE_AUTOEXPO_EN (1 << 14)
93 #define MT9M111_OPMODE_AUTOWHITEBAL_EN (1 << 1)
95 #define MT9M111_OUTFMT_PROCESSED_BAYER (1 << 14)
96 #define MT9M111_OUTFMT_BYPASS_IFP (1 << 10)
97 #define MT9M111_OUTFMT_INV_PIX_CLOCK (1 << 9)
98 #define MT9M111_OUTFMT_RGB (1 << 8)
99 #define MT9M111_OUTFMT_RGB565 (0x0 << 6)
100 #define MT9M111_OUTFMT_RGB555 (0x1 << 6)
101 #define MT9M111_OUTFMT_RGB444x (0x2 << 6)
102 #define MT9M111_OUTFMT_RGBx444 (0x3 << 6)
103 #define MT9M111_OUTFMT_TST_RAMP_OFF (0x0 << 4)
104 #define MT9M111_OUTFMT_TST_RAMP_COL (0x1 << 4)
105 #define MT9M111_OUTFMT_TST_RAMP_ROW (0x2 << 4)
106 #define MT9M111_OUTFMT_TST_RAMP_FRAME (0x3 << 4)
107 #define MT9M111_OUTFMT_SHIFT_3_UP (1 << 3)
108 #define MT9M111_OUTFMT_AVG_CHROMA (1 << 2)
109 #define MT9M111_OUTFMT_SWAP_YCbCr_C_Y (1 << 1)
110 #define MT9M111_OUTFMT_SWAP_RGB_EVEN (1 << 1)
111 #define MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr (1 << 0)
113 * mt9m111: Camera control register addresses (0x200..0x2ff not implemented)
116 #define reg_read(reg) mt9m111_reg_read(client, MT9M111_##reg)
117 #define reg_write(reg, val) mt9m111_reg_write(client, MT9M111_##reg, (val))
118 #define reg_set(reg, val) mt9m111_reg_set(client, MT9M111_##reg, (val))
119 #define reg_clear(reg, val) mt9m111_reg_clear(client, MT9M111_##reg, (val))
121 #define MT9M111_MIN_DARK_ROWS 8
122 #define MT9M111_MIN_DARK_COLS 24
123 #define MT9M111_MAX_HEIGHT 1024
124 #define MT9M111_MAX_WIDTH 1280
126 /* MT9M111 has only one fixed colorspace per pixelcode */
127 struct mt9m111_datafmt {
128 enum v4l2_mbus_pixelcode code;
129 enum v4l2_colorspace colorspace;
132 /* Find a data format by a pixel code in an array */
133 static const struct mt9m111_datafmt *mt9m111_find_datafmt(
134 enum v4l2_mbus_pixelcode code, const struct mt9m111_datafmt *fmt,
138 for (i = 0; i < n; i++)
139 if (fmt[i].code == code)
145 static const struct mt9m111_datafmt mt9m111_colour_fmts[] = {
146 {V4L2_MBUS_FMT_YUYV8_2X8_LE, V4L2_COLORSPACE_JPEG},
147 {V4L2_MBUS_FMT_YVYU8_2X8_LE, V4L2_COLORSPACE_JPEG},
148 {V4L2_MBUS_FMT_YUYV8_2X8_BE, V4L2_COLORSPACE_JPEG},
149 {V4L2_MBUS_FMT_YVYU8_2X8_BE, V4L2_COLORSPACE_JPEG},
150 {V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE, V4L2_COLORSPACE_SRGB},
151 {V4L2_MBUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB},
152 {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
153 {V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE, V4L2_COLORSPACE_SRGB},
156 enum mt9m111_context {
162 struct v4l2_subdev subdev;
163 int model; /* V4L2_IDENT_MT9M11x* codes from v4l2-chip-ident.h */
164 enum mt9m111_context context;
165 struct v4l2_rect rect;
166 const struct mt9m111_datafmt *fmt;
168 unsigned char autoexposure;
169 unsigned char datawidth;
170 unsigned int powered:1;
171 unsigned int hflip:1;
172 unsigned int vflip:1;
173 unsigned int swap_rgb_even_odd:1;
174 unsigned int swap_rgb_red_blue:1;
175 unsigned int swap_yuv_y_chromas:1;
176 unsigned int swap_yuv_cb_cr:1;
177 unsigned int autowhitebalance:1;
180 static struct mt9m111 *to_mt9m111(const struct i2c_client *client)
182 return container_of(i2c_get_clientdata(client), struct mt9m111, subdev);
185 static int reg_page_map_set(struct i2c_client *client, const u16 reg)
189 static int lastpage = -1; /* PageMap cache value */
192 if (page == lastpage)
197 ret = i2c_smbus_write_word_data(client, MT9M111_PAGE_MAP, swab16(page));
203 static int mt9m111_reg_read(struct i2c_client *client, const u16 reg)
207 ret = reg_page_map_set(client, reg);
209 ret = swab16(i2c_smbus_read_word_data(client, reg & 0xff));
211 dev_dbg(&client->dev, "read reg.%03x -> %04x\n", reg, ret);
215 static int mt9m111_reg_write(struct i2c_client *client, const u16 reg,
220 ret = reg_page_map_set(client, reg);
222 ret = i2c_smbus_write_word_data(client, reg & 0xff,
224 dev_dbg(&client->dev, "write reg.%03x = %04x -> %d\n", reg, data, ret);
228 static int mt9m111_reg_set(struct i2c_client *client, const u16 reg,
233 ret = mt9m111_reg_read(client, reg);
235 ret = mt9m111_reg_write(client, reg, ret | data);
239 static int mt9m111_reg_clear(struct i2c_client *client, const u16 reg,
244 ret = mt9m111_reg_read(client, reg);
245 return mt9m111_reg_write(client, reg, ret & ~data);
248 static int mt9m111_set_context(struct i2c_client *client,
249 enum mt9m111_context ctxt)
251 int valB = MT9M111_CTXT_CTRL_RESTART | MT9M111_CTXT_CTRL_DEFECTCOR_B
252 | MT9M111_CTXT_CTRL_RESIZE_B | MT9M111_CTXT_CTRL_CTRL2_B
253 | MT9M111_CTXT_CTRL_GAMMA_B | MT9M111_CTXT_CTRL_READ_MODE_B
254 | MT9M111_CTXT_CTRL_VBLANK_SEL_B
255 | MT9M111_CTXT_CTRL_HBLANK_SEL_B;
256 int valA = MT9M111_CTXT_CTRL_RESTART;
258 if (ctxt == HIGHPOWER)
259 return reg_write(CONTEXT_CONTROL, valB);
261 return reg_write(CONTEXT_CONTROL, valA);
264 static int mt9m111_setup_rect(struct i2c_client *client,
265 struct v4l2_rect *rect)
267 struct mt9m111 *mt9m111 = to_mt9m111(client);
268 int ret, is_raw_format;
269 int width = rect->width;
270 int height = rect->height;
272 if (mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
273 mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE)
278 ret = reg_write(COLUMN_START, rect->left);
280 ret = reg_write(ROW_START, rect->top);
284 ret = reg_write(WINDOW_WIDTH, width);
286 ret = reg_write(WINDOW_HEIGHT, height);
289 ret = reg_write(REDUCER_XZOOM_B, MT9M111_MAX_WIDTH);
291 ret = reg_write(REDUCER_YZOOM_B, MT9M111_MAX_HEIGHT);
293 ret = reg_write(REDUCER_XSIZE_B, width);
295 ret = reg_write(REDUCER_YSIZE_B, height);
297 ret = reg_write(REDUCER_XZOOM_A, MT9M111_MAX_WIDTH);
299 ret = reg_write(REDUCER_YZOOM_A, MT9M111_MAX_HEIGHT);
301 ret = reg_write(REDUCER_XSIZE_A, width);
303 ret = reg_write(REDUCER_YSIZE_A, height);
309 static int mt9m111_setup_pixfmt(struct i2c_client *client, u16 outfmt)
313 ret = reg_write(OUTPUT_FORMAT_CTRL2_A, outfmt);
315 ret = reg_write(OUTPUT_FORMAT_CTRL2_B, outfmt);
319 static int mt9m111_setfmt_bayer8(struct i2c_client *client)
321 return mt9m111_setup_pixfmt(client, MT9M111_OUTFMT_PROCESSED_BAYER |
325 static int mt9m111_setfmt_bayer10(struct i2c_client *client)
327 return mt9m111_setup_pixfmt(client, MT9M111_OUTFMT_BYPASS_IFP);
330 static int mt9m111_setfmt_rgb565(struct i2c_client *client)
332 struct mt9m111 *mt9m111 = to_mt9m111(client);
335 if (mt9m111->swap_rgb_red_blue)
336 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
337 if (mt9m111->swap_rgb_even_odd)
338 val |= MT9M111_OUTFMT_SWAP_RGB_EVEN;
339 val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565;
341 return mt9m111_setup_pixfmt(client, val);
344 static int mt9m111_setfmt_rgb555(struct i2c_client *client)
346 struct mt9m111 *mt9m111 = to_mt9m111(client);
349 if (mt9m111->swap_rgb_red_blue)
350 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
351 if (mt9m111->swap_rgb_even_odd)
352 val |= MT9M111_OUTFMT_SWAP_RGB_EVEN;
353 val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB555;
355 return mt9m111_setup_pixfmt(client, val);
358 static int mt9m111_setfmt_yuv(struct i2c_client *client)
360 struct mt9m111 *mt9m111 = to_mt9m111(client);
363 if (mt9m111->swap_yuv_cb_cr)
364 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
365 if (mt9m111->swap_yuv_y_chromas)
366 val |= MT9M111_OUTFMT_SWAP_YCbCr_C_Y;
368 return mt9m111_setup_pixfmt(client, val);
371 static int mt9m111_enable(struct i2c_client *client)
373 struct mt9m111 *mt9m111 = to_mt9m111(client);
376 ret = reg_set(RESET, MT9M111_RESET_CHIP_ENABLE);
378 mt9m111->powered = 1;
382 static int mt9m111_reset(struct i2c_client *client)
386 ret = reg_set(RESET, MT9M111_RESET_RESET_MODE);
388 ret = reg_set(RESET, MT9M111_RESET_RESET_SOC);
390 ret = reg_clear(RESET, MT9M111_RESET_RESET_MODE
391 | MT9M111_RESET_RESET_SOC);
396 static unsigned long mt9m111_query_bus_param(struct soc_camera_device *icd)
398 struct soc_camera_link *icl = to_soc_camera_link(icd);
399 unsigned long flags = SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |
400 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
401 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8;
403 return soc_camera_apply_sensor_flags(icl, flags);
406 static int mt9m111_set_bus_param(struct soc_camera_device *icd, unsigned long f)
411 static int mt9m111_make_rect(struct i2c_client *client,
412 struct v4l2_rect *rect)
414 struct mt9m111 *mt9m111 = to_mt9m111(client);
416 if (mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
417 mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE) {
418 /* Bayer format - even size lengths */
419 rect->width = ALIGN(rect->width, 2);
420 rect->height = ALIGN(rect->height, 2);
421 /* Let the user play with the starting pixel */
424 /* FIXME: the datasheet doesn't specify minimum sizes */
425 soc_camera_limit_side(&rect->left, &rect->width,
426 MT9M111_MIN_DARK_COLS, 2, MT9M111_MAX_WIDTH);
428 soc_camera_limit_side(&rect->top, &rect->height,
429 MT9M111_MIN_DARK_ROWS, 2, MT9M111_MAX_HEIGHT);
431 return mt9m111_setup_rect(client, rect);
434 static int mt9m111_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
436 struct v4l2_rect rect = a->c;
437 struct i2c_client *client = sd->priv;
438 struct mt9m111 *mt9m111 = to_mt9m111(client);
441 dev_dbg(&client->dev, "%s left=%d, top=%d, width=%d, height=%d\n",
442 __func__, rect.left, rect.top, rect.width, rect.height);
444 ret = mt9m111_make_rect(client, &rect);
446 mt9m111->rect = rect;
450 static int mt9m111_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
452 struct i2c_client *client = sd->priv;
453 struct mt9m111 *mt9m111 = to_mt9m111(client);
455 a->c = mt9m111->rect;
456 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
461 static int mt9m111_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
463 a->bounds.left = MT9M111_MIN_DARK_COLS;
464 a->bounds.top = MT9M111_MIN_DARK_ROWS;
465 a->bounds.width = MT9M111_MAX_WIDTH;
466 a->bounds.height = MT9M111_MAX_HEIGHT;
467 a->defrect = a->bounds;
468 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
469 a->pixelaspect.numerator = 1;
470 a->pixelaspect.denominator = 1;
475 static int mt9m111_g_fmt(struct v4l2_subdev *sd,
476 struct v4l2_mbus_framefmt *mf)
478 struct i2c_client *client = sd->priv;
479 struct mt9m111 *mt9m111 = to_mt9m111(client);
481 mf->width = mt9m111->rect.width;
482 mf->height = mt9m111->rect.height;
483 mf->code = mt9m111->fmt->code;
484 mf->field = V4L2_FIELD_NONE;
489 static int mt9m111_set_pixfmt(struct i2c_client *client,
490 enum v4l2_mbus_pixelcode code)
492 struct mt9m111 *mt9m111 = to_mt9m111(client);
496 case V4L2_MBUS_FMT_SBGGR8_1X8:
497 ret = mt9m111_setfmt_bayer8(client);
499 case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE:
500 ret = mt9m111_setfmt_bayer10(client);
502 case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
503 ret = mt9m111_setfmt_rgb555(client);
505 case V4L2_MBUS_FMT_RGB565_2X8_LE:
506 ret = mt9m111_setfmt_rgb565(client);
508 case V4L2_MBUS_FMT_YUYV8_2X8_BE:
509 mt9m111->swap_yuv_y_chromas = 0;
510 mt9m111->swap_yuv_cb_cr = 0;
511 ret = mt9m111_setfmt_yuv(client);
513 case V4L2_MBUS_FMT_YVYU8_2X8_BE:
514 mt9m111->swap_yuv_y_chromas = 0;
515 mt9m111->swap_yuv_cb_cr = 1;
516 ret = mt9m111_setfmt_yuv(client);
518 case V4L2_MBUS_FMT_YUYV8_2X8_LE:
519 mt9m111->swap_yuv_y_chromas = 1;
520 mt9m111->swap_yuv_cb_cr = 0;
521 ret = mt9m111_setfmt_yuv(client);
523 case V4L2_MBUS_FMT_YVYU8_2X8_LE:
524 mt9m111->swap_yuv_y_chromas = 1;
525 mt9m111->swap_yuv_cb_cr = 1;
526 ret = mt9m111_setfmt_yuv(client);
529 dev_err(&client->dev, "Pixel format not handled : %x\n",
537 static int mt9m111_s_fmt(struct v4l2_subdev *sd,
538 struct v4l2_mbus_framefmt *mf)
540 struct i2c_client *client = sd->priv;
541 const struct mt9m111_datafmt *fmt;
542 struct mt9m111 *mt9m111 = to_mt9m111(client);
543 struct v4l2_rect rect = {
544 .left = mt9m111->rect.left,
545 .top = mt9m111->rect.top,
547 .height = mf->height,
551 fmt = mt9m111_find_datafmt(mf->code, mt9m111_colour_fmts,
552 ARRAY_SIZE(mt9m111_colour_fmts));
556 dev_dbg(&client->dev,
557 "%s code=%x left=%d, top=%d, width=%d, height=%d\n", __func__,
558 mf->code, rect.left, rect.top, rect.width, rect.height);
560 ret = mt9m111_make_rect(client, &rect);
562 ret = mt9m111_set_pixfmt(client, mf->code);
564 mt9m111->rect = rect;
566 mf->colorspace = fmt->colorspace;
572 static int mt9m111_try_fmt(struct v4l2_subdev *sd,
573 struct v4l2_mbus_framefmt *mf)
575 struct i2c_client *client = sd->priv;
576 struct mt9m111 *mt9m111 = to_mt9m111(client);
577 const struct mt9m111_datafmt *fmt;
578 bool bayer = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
579 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE;
581 fmt = mt9m111_find_datafmt(mf->code, mt9m111_colour_fmts,
582 ARRAY_SIZE(mt9m111_colour_fmts));
585 mf->code = fmt->code;
589 * With Bayer format enforce even side lengths, but let the user play
590 * with the starting pixel
593 if (mf->height > MT9M111_MAX_HEIGHT)
594 mf->height = MT9M111_MAX_HEIGHT;
595 else if (mf->height < 2)
598 mf->height = ALIGN(mf->height, 2);
600 if (mf->width > MT9M111_MAX_WIDTH)
601 mf->width = MT9M111_MAX_WIDTH;
602 else if (mf->width < 2)
605 mf->width = ALIGN(mf->width, 2);
607 mf->colorspace = fmt->colorspace;
612 static int mt9m111_g_chip_ident(struct v4l2_subdev *sd,
613 struct v4l2_dbg_chip_ident *id)
615 struct i2c_client *client = sd->priv;
616 struct mt9m111 *mt9m111 = to_mt9m111(client);
618 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
621 if (id->match.addr != client->addr)
624 id->ident = mt9m111->model;
630 #ifdef CONFIG_VIDEO_ADV_DEBUG
631 static int mt9m111_g_register(struct v4l2_subdev *sd,
632 struct v4l2_dbg_register *reg)
634 struct i2c_client *client = sd->priv;
637 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
639 if (reg->match.addr != client->addr)
642 val = mt9m111_reg_read(client, reg->reg);
646 if (reg->val > 0xffff)
652 static int mt9m111_s_register(struct v4l2_subdev *sd,
653 struct v4l2_dbg_register *reg)
655 struct i2c_client *client = sd->priv;
657 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
660 if (reg->match.addr != client->addr)
663 if (mt9m111_reg_write(client, reg->reg, reg->val) < 0)
670 static const struct v4l2_queryctrl mt9m111_controls[] = {
672 .id = V4L2_CID_VFLIP,
673 .type = V4L2_CTRL_TYPE_BOOLEAN,
674 .name = "Flip Verticaly",
680 .id = V4L2_CID_HFLIP,
681 .type = V4L2_CTRL_TYPE_BOOLEAN,
682 .name = "Flip Horizontaly",
687 }, { /* gain = 1/32*val (=>gain=1 if val==32) */
689 .type = V4L2_CTRL_TYPE_INTEGER,
692 .maximum = 63 * 2 * 2,
695 .flags = V4L2_CTRL_FLAG_SLIDER,
697 .id = V4L2_CID_EXPOSURE_AUTO,
698 .type = V4L2_CTRL_TYPE_BOOLEAN,
699 .name = "Auto Exposure",
707 static int mt9m111_resume(struct soc_camera_device *icd);
708 static int mt9m111_suspend(struct soc_camera_device *icd, pm_message_t state);
710 static struct soc_camera_ops mt9m111_ops = {
711 .suspend = mt9m111_suspend,
712 .resume = mt9m111_resume,
713 .query_bus_param = mt9m111_query_bus_param,
714 .set_bus_param = mt9m111_set_bus_param,
715 .controls = mt9m111_controls,
716 .num_controls = ARRAY_SIZE(mt9m111_controls),
719 static int mt9m111_set_flip(struct i2c_client *client, int flip, int mask)
721 struct mt9m111 *mt9m111 = to_mt9m111(client);
724 if (mt9m111->context == HIGHPOWER) {
726 ret = reg_set(READ_MODE_B, mask);
728 ret = reg_clear(READ_MODE_B, mask);
731 ret = reg_set(READ_MODE_A, mask);
733 ret = reg_clear(READ_MODE_A, mask);
739 static int mt9m111_get_global_gain(struct i2c_client *client)
743 data = reg_read(GLOBAL_GAIN);
745 return (data & 0x2f) * (1 << ((data >> 10) & 1)) *
746 (1 << ((data >> 9) & 1));
750 static int mt9m111_set_global_gain(struct i2c_client *client, int gain)
752 struct mt9m111 *mt9m111 = to_mt9m111(client);
755 if (gain > 63 * 2 * 2)
758 mt9m111->gain = gain;
759 if ((gain >= 64 * 2) && (gain < 63 * 2 * 2))
760 val = (1 << 10) | (1 << 9) | (gain / 4);
761 else if ((gain >= 64) && (gain < 64 * 2))
762 val = (1 << 9) | (gain / 2);
766 return reg_write(GLOBAL_GAIN, val);
769 static int mt9m111_set_autoexposure(struct i2c_client *client, int on)
771 struct mt9m111 *mt9m111 = to_mt9m111(client);
775 ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
777 ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
780 mt9m111->autoexposure = on;
785 static int mt9m111_set_autowhitebalance(struct i2c_client *client, int on)
787 struct mt9m111 *mt9m111 = to_mt9m111(client);
791 ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
793 ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
796 mt9m111->autowhitebalance = on;
801 static int mt9m111_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
803 struct i2c_client *client = sd->priv;
804 struct mt9m111 *mt9m111 = to_mt9m111(client);
809 if (mt9m111->context == HIGHPOWER)
810 data = reg_read(READ_MODE_B);
812 data = reg_read(READ_MODE_A);
816 ctrl->value = !!(data & MT9M111_RMB_MIRROR_ROWS);
819 if (mt9m111->context == HIGHPOWER)
820 data = reg_read(READ_MODE_B);
822 data = reg_read(READ_MODE_A);
826 ctrl->value = !!(data & MT9M111_RMB_MIRROR_COLS);
829 data = mt9m111_get_global_gain(client);
834 case V4L2_CID_EXPOSURE_AUTO:
835 ctrl->value = mt9m111->autoexposure;
837 case V4L2_CID_AUTO_WHITE_BALANCE:
838 ctrl->value = mt9m111->autowhitebalance;
844 static int mt9m111_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
846 struct i2c_client *client = sd->priv;
847 struct mt9m111 *mt9m111 = to_mt9m111(client);
848 const struct v4l2_queryctrl *qctrl;
851 qctrl = soc_camera_find_qctrl(&mt9m111_ops, ctrl->id);
857 mt9m111->vflip = ctrl->value;
858 ret = mt9m111_set_flip(client, ctrl->value,
859 MT9M111_RMB_MIRROR_ROWS);
862 mt9m111->hflip = ctrl->value;
863 ret = mt9m111_set_flip(client, ctrl->value,
864 MT9M111_RMB_MIRROR_COLS);
867 ret = mt9m111_set_global_gain(client, ctrl->value);
869 case V4L2_CID_EXPOSURE_AUTO:
870 ret = mt9m111_set_autoexposure(client, ctrl->value);
872 case V4L2_CID_AUTO_WHITE_BALANCE:
873 ret = mt9m111_set_autowhitebalance(client, ctrl->value);
882 static int mt9m111_suspend(struct soc_camera_device *icd, pm_message_t state)
884 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
885 struct mt9m111 *mt9m111 = to_mt9m111(client);
887 mt9m111->gain = mt9m111_get_global_gain(client);
892 static int mt9m111_restore_state(struct i2c_client *client)
894 struct mt9m111 *mt9m111 = to_mt9m111(client);
896 mt9m111_set_context(client, mt9m111->context);
897 mt9m111_set_pixfmt(client, mt9m111->fmt->code);
898 mt9m111_setup_rect(client, &mt9m111->rect);
899 mt9m111_set_flip(client, mt9m111->hflip, MT9M111_RMB_MIRROR_COLS);
900 mt9m111_set_flip(client, mt9m111->vflip, MT9M111_RMB_MIRROR_ROWS);
901 mt9m111_set_global_gain(client, mt9m111->gain);
902 mt9m111_set_autoexposure(client, mt9m111->autoexposure);
903 mt9m111_set_autowhitebalance(client, mt9m111->autowhitebalance);
907 static int mt9m111_resume(struct soc_camera_device *icd)
909 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
910 struct mt9m111 *mt9m111 = to_mt9m111(client);
913 if (mt9m111->powered) {
914 ret = mt9m111_enable(client);
916 ret = mt9m111_reset(client);
918 ret = mt9m111_restore_state(client);
923 static int mt9m111_init(struct i2c_client *client)
925 struct mt9m111 *mt9m111 = to_mt9m111(client);
928 mt9m111->context = HIGHPOWER;
929 ret = mt9m111_enable(client);
931 ret = mt9m111_reset(client);
933 ret = mt9m111_set_context(client, mt9m111->context);
935 ret = mt9m111_set_autoexposure(client, mt9m111->autoexposure);
937 dev_err(&client->dev, "mt9m11x init failed: %d\n", ret);
942 * Interface active, can use i2c. If it fails, it can indeed mean, that
943 * this wasn't our capture interface, so, we wait for the right one
945 static int mt9m111_video_probe(struct soc_camera_device *icd,
946 struct i2c_client *client)
948 struct mt9m111 *mt9m111 = to_mt9m111(client);
953 * We must have a parent by now. And it cannot be a wrong one.
954 * So this entire test is completely redundant.
956 if (!icd->dev.parent ||
957 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
960 mt9m111->autoexposure = 1;
961 mt9m111->autowhitebalance = 1;
963 mt9m111->swap_rgb_even_odd = 1;
964 mt9m111->swap_rgb_red_blue = 1;
966 ret = mt9m111_init(client);
970 data = reg_read(CHIP_VERSION);
973 case 0x143a: /* MT9M111 */
974 mt9m111->model = V4L2_IDENT_MT9M111;
976 case 0x148c: /* MT9M112 */
977 mt9m111->model = V4L2_IDENT_MT9M112;
981 dev_err(&client->dev,
982 "No MT9M11x chip detected, register read %x\n", data);
986 dev_info(&client->dev, "Detected a MT9M11x chip ID %x\n", data);
992 static struct v4l2_subdev_core_ops mt9m111_subdev_core_ops = {
993 .g_ctrl = mt9m111_g_ctrl,
994 .s_ctrl = mt9m111_s_ctrl,
995 .g_chip_ident = mt9m111_g_chip_ident,
996 #ifdef CONFIG_VIDEO_ADV_DEBUG
997 .g_register = mt9m111_g_register,
998 .s_register = mt9m111_s_register,
1002 static int mt9m111_enum_fmt(struct v4l2_subdev *sd, int index,
1003 enum v4l2_mbus_pixelcode *code)
1005 if ((unsigned int)index >= ARRAY_SIZE(mt9m111_colour_fmts))
1008 *code = mt9m111_colour_fmts[index].code;
1012 static struct v4l2_subdev_video_ops mt9m111_subdev_video_ops = {
1013 .s_mbus_fmt = mt9m111_s_fmt,
1014 .g_mbus_fmt = mt9m111_g_fmt,
1015 .try_mbus_fmt = mt9m111_try_fmt,
1016 .s_crop = mt9m111_s_crop,
1017 .g_crop = mt9m111_g_crop,
1018 .cropcap = mt9m111_cropcap,
1019 .enum_mbus_fmt = mt9m111_enum_fmt,
1022 static struct v4l2_subdev_ops mt9m111_subdev_ops = {
1023 .core = &mt9m111_subdev_core_ops,
1024 .video = &mt9m111_subdev_video_ops,
1027 static int mt9m111_probe(struct i2c_client *client,
1028 const struct i2c_device_id *did)
1030 struct mt9m111 *mt9m111;
1031 struct soc_camera_device *icd = client->dev.platform_data;
1032 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
1033 struct soc_camera_link *icl;
1037 dev_err(&client->dev, "MT9M11x: missing soc-camera data!\n");
1041 icl = to_soc_camera_link(icd);
1043 dev_err(&client->dev, "MT9M11x driver needs platform data\n");
1047 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
1048 dev_warn(&adapter->dev,
1049 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
1053 mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL);
1057 v4l2_i2c_subdev_init(&mt9m111->subdev, client, &mt9m111_subdev_ops);
1059 /* Second stage probe - when a capture adapter is there */
1060 icd->ops = &mt9m111_ops;
1062 mt9m111->rect.left = MT9M111_MIN_DARK_COLS;
1063 mt9m111->rect.top = MT9M111_MIN_DARK_ROWS;
1064 mt9m111->rect.width = MT9M111_MAX_WIDTH;
1065 mt9m111->rect.height = MT9M111_MAX_HEIGHT;
1066 mt9m111->fmt = &mt9m111_colour_fmts[0];
1068 ret = mt9m111_video_probe(icd, client);
1071 i2c_set_clientdata(client, NULL);
1078 static int mt9m111_remove(struct i2c_client *client)
1080 struct mt9m111 *mt9m111 = to_mt9m111(client);
1081 struct soc_camera_device *icd = client->dev.platform_data;
1084 i2c_set_clientdata(client, NULL);
1085 client->driver = NULL;
1091 static const struct i2c_device_id mt9m111_id[] = {
1095 MODULE_DEVICE_TABLE(i2c, mt9m111_id);
1097 static struct i2c_driver mt9m111_i2c_driver = {
1101 .probe = mt9m111_probe,
1102 .remove = mt9m111_remove,
1103 .id_table = mt9m111_id,
1106 static int __init mt9m111_mod_init(void)
1108 return i2c_add_driver(&mt9m111_i2c_driver);
1111 static void __exit mt9m111_mod_exit(void)
1113 i2c_del_driver(&mt9m111_i2c_driver);
1116 module_init(mt9m111_mod_init);
1117 module_exit(mt9m111_mod_exit);
1119 MODULE_DESCRIPTION("Micron MT9M111/MT9M112 Camera driver");
1120 MODULE_AUTHOR("Robert Jarzmik");
1121 MODULE_LICENSE("GPL");