2 * Driver for MT9M111 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 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)
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(icd, MT9M111_##reg)
117 #define reg_write(reg, val) mt9m111_reg_write(icd, MT9M111_##reg, (val))
118 #define reg_set(reg, val) mt9m111_reg_set(icd, MT9M111_##reg, (val))
119 #define reg_clear(reg, val) mt9m111_reg_clear(icd, 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 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
127 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
128 .colorspace = _colorspace }
129 #define RGB_FMT(_name, _depth, _fourcc) \
130 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_SRGB)
131 #define JPG_FMT(_name, _depth, _fourcc) \
132 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
134 static const struct soc_camera_data_format mt9m111_colour_formats[] = {
135 JPG_FMT("CbYCrY 16 bit", 16, V4L2_PIX_FMT_UYVY),
136 JPG_FMT("CrYCbY 16 bit", 16, V4L2_PIX_FMT_VYUY),
137 JPG_FMT("YCbYCr 16 bit", 16, V4L2_PIX_FMT_YUYV),
138 JPG_FMT("YCrYCb 16 bit", 16, V4L2_PIX_FMT_YVYU),
139 RGB_FMT("RGB 565", 16, V4L2_PIX_FMT_RGB565),
140 RGB_FMT("RGB 555", 16, V4L2_PIX_FMT_RGB555),
141 RGB_FMT("Bayer (sRGB) 10 bit", 10, V4L2_PIX_FMT_SBGGR16),
142 RGB_FMT("Bayer (sRGB) 8 bit", 8, V4L2_PIX_FMT_SBGGR8),
145 enum mt9m111_context {
151 struct i2c_client *client;
152 struct soc_camera_device icd;
153 int model; /* V4L2_IDENT_MT9M111* codes from v4l2-chip-ident.h */
154 enum mt9m111_context context;
155 unsigned int left, top, width, height;
157 unsigned char autoexposure;
158 unsigned char datawidth;
159 unsigned int powered:1;
160 unsigned int hflip:1;
161 unsigned int vflip:1;
162 unsigned int swap_rgb_even_odd:1;
163 unsigned int swap_rgb_red_blue:1;
164 unsigned int swap_yuv_y_chromas:1;
165 unsigned int swap_yuv_cb_cr:1;
168 static int reg_page_map_set(struct i2c_client *client, const u16 reg)
172 static int lastpage = -1; /* PageMap cache value */
175 if (page == lastpage)
180 ret = i2c_smbus_write_word_data(client, MT9M111_PAGE_MAP, swab16(page));
186 static int mt9m111_reg_read(struct soc_camera_device *icd, const u16 reg)
188 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
189 struct i2c_client *client = mt9m111->client;
192 ret = reg_page_map_set(client, reg);
194 ret = swab16(i2c_smbus_read_word_data(client, (reg & 0xff)));
196 dev_dbg(&icd->dev, "read reg.%03x -> %04x\n", reg, ret);
200 static int mt9m111_reg_write(struct soc_camera_device *icd, const u16 reg,
203 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
204 struct i2c_client *client = mt9m111->client;
207 ret = reg_page_map_set(client, reg);
209 ret = i2c_smbus_write_word_data(mt9m111->client, (reg & 0xff),
211 dev_dbg(&icd->dev, "write reg.%03x = %04x -> %d\n", reg, data, ret);
215 static int mt9m111_reg_set(struct soc_camera_device *icd, const u16 reg,
220 ret = mt9m111_reg_read(icd, reg);
222 ret = mt9m111_reg_write(icd, reg, ret | data);
226 static int mt9m111_reg_clear(struct soc_camera_device *icd, const u16 reg,
231 ret = mt9m111_reg_read(icd, reg);
232 return mt9m111_reg_write(icd, reg, ret & ~data);
235 static int mt9m111_set_context(struct soc_camera_device *icd,
236 enum mt9m111_context ctxt)
238 int valB = MT9M111_CTXT_CTRL_RESTART | MT9M111_CTXT_CTRL_DEFECTCOR_B
239 | MT9M111_CTXT_CTRL_RESIZE_B | MT9M111_CTXT_CTRL_CTRL2_B
240 | MT9M111_CTXT_CTRL_GAMMA_B | MT9M111_CTXT_CTRL_READ_MODE_B
241 | MT9M111_CTXT_CTRL_VBLANK_SEL_B
242 | MT9M111_CTXT_CTRL_HBLANK_SEL_B;
243 int valA = MT9M111_CTXT_CTRL_RESTART;
245 if (ctxt == HIGHPOWER)
246 return reg_write(CONTEXT_CONTROL, valB);
248 return reg_write(CONTEXT_CONTROL, valA);
251 static int mt9m111_setup_rect(struct soc_camera_device *icd)
253 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
254 int ret, is_raw_format;
255 int width = mt9m111->width;
256 int height = mt9m111->height;
258 if ((mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR8)
259 || (mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR16))
264 ret = reg_write(COLUMN_START, mt9m111->left);
266 ret = reg_write(ROW_START, mt9m111->top);
270 ret = reg_write(WINDOW_WIDTH, width);
272 ret = reg_write(WINDOW_HEIGHT, height);
275 ret = reg_write(REDUCER_XZOOM_B, MT9M111_MAX_WIDTH);
277 ret = reg_write(REDUCER_YZOOM_B, MT9M111_MAX_HEIGHT);
279 ret = reg_write(REDUCER_XSIZE_B, width);
281 ret = reg_write(REDUCER_YSIZE_B, height);
283 ret = reg_write(REDUCER_XZOOM_A, MT9M111_MAX_WIDTH);
285 ret = reg_write(REDUCER_YZOOM_A, MT9M111_MAX_HEIGHT);
287 ret = reg_write(REDUCER_XSIZE_A, width);
289 ret = reg_write(REDUCER_YSIZE_A, height);
295 static int mt9m111_setup_pixfmt(struct soc_camera_device *icd, u16 outfmt)
299 ret = reg_write(OUTPUT_FORMAT_CTRL2_A, outfmt);
301 ret = reg_write(OUTPUT_FORMAT_CTRL2_B, outfmt);
305 static int mt9m111_setfmt_bayer8(struct soc_camera_device *icd)
307 return mt9m111_setup_pixfmt(icd, MT9M111_OUTFMT_PROCESSED_BAYER);
310 static int mt9m111_setfmt_bayer10(struct soc_camera_device *icd)
312 return mt9m111_setup_pixfmt(icd, MT9M111_OUTFMT_BYPASS_IFP);
315 static int mt9m111_setfmt_rgb565(struct soc_camera_device *icd)
317 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
320 if (mt9m111->swap_rgb_red_blue)
321 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
322 if (mt9m111->swap_rgb_even_odd)
323 val |= MT9M111_OUTFMT_SWAP_RGB_EVEN;
324 val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565;
326 return mt9m111_setup_pixfmt(icd, val);
329 static int mt9m111_setfmt_rgb555(struct soc_camera_device *icd)
331 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
334 if (mt9m111->swap_rgb_red_blue)
335 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
336 if (mt9m111->swap_rgb_even_odd)
337 val |= MT9M111_OUTFMT_SWAP_RGB_EVEN;
338 val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB555;
340 return mt9m111_setup_pixfmt(icd, val);
343 static int mt9m111_setfmt_yuv(struct soc_camera_device *icd)
345 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
348 if (mt9m111->swap_yuv_cb_cr)
349 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
350 if (mt9m111->swap_yuv_y_chromas)
351 val |= MT9M111_OUTFMT_SWAP_YCbCr_C_Y;
353 return mt9m111_setup_pixfmt(icd, val);
356 static int mt9m111_enable(struct soc_camera_device *icd)
358 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
359 struct soc_camera_link *icl = mt9m111->client->dev.platform_data;
363 ret = icl->power(&mt9m111->client->dev, 1);
365 dev_err(icd->vdev->parent,
366 "Platform failed to power-on the camera.\n");
371 ret = reg_set(RESET, MT9M111_RESET_CHIP_ENABLE);
373 mt9m111->powered = 1;
377 static int mt9m111_disable(struct soc_camera_device *icd)
379 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
380 struct soc_camera_link *icl = mt9m111->client->dev.platform_data;
383 ret = reg_clear(RESET, MT9M111_RESET_CHIP_ENABLE);
385 mt9m111->powered = 0;
388 icl->power(&mt9m111->client->dev, 0);
393 static int mt9m111_reset(struct soc_camera_device *icd)
397 ret = reg_set(RESET, MT9M111_RESET_RESET_MODE);
399 ret = reg_set(RESET, MT9M111_RESET_RESET_SOC);
401 ret = reg_clear(RESET, MT9M111_RESET_RESET_MODE
402 | MT9M111_RESET_RESET_SOC);
406 static int mt9m111_start_capture(struct soc_camera_device *icd)
411 static int mt9m111_stop_capture(struct soc_camera_device *icd)
416 static unsigned long mt9m111_query_bus_param(struct soc_camera_device *icd)
418 return SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |
419 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
423 static int mt9m111_set_bus_param(struct soc_camera_device *icd, unsigned long f)
428 static int mt9m111_set_pixfmt(struct soc_camera_device *icd, u32 pixfmt)
430 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
434 case V4L2_PIX_FMT_SBGGR8:
435 ret = mt9m111_setfmt_bayer8(icd);
437 case V4L2_PIX_FMT_SBGGR16:
438 ret = mt9m111_setfmt_bayer10(icd);
440 case V4L2_PIX_FMT_RGB555:
441 ret = mt9m111_setfmt_rgb555(icd);
443 case V4L2_PIX_FMT_RGB565:
444 ret = mt9m111_setfmt_rgb565(icd);
446 case V4L2_PIX_FMT_UYVY:
447 mt9m111->swap_yuv_y_chromas = 0;
448 mt9m111->swap_yuv_cb_cr = 0;
449 ret = mt9m111_setfmt_yuv(icd);
451 case V4L2_PIX_FMT_VYUY:
452 mt9m111->swap_yuv_y_chromas = 0;
453 mt9m111->swap_yuv_cb_cr = 1;
454 ret = mt9m111_setfmt_yuv(icd);
456 case V4L2_PIX_FMT_YUYV:
457 mt9m111->swap_yuv_y_chromas = 1;
458 mt9m111->swap_yuv_cb_cr = 0;
459 ret = mt9m111_setfmt_yuv(icd);
461 case V4L2_PIX_FMT_YVYU:
462 mt9m111->swap_yuv_y_chromas = 1;
463 mt9m111->swap_yuv_cb_cr = 1;
464 ret = mt9m111_setfmt_yuv(icd);
467 dev_err(&icd->dev, "Pixel format not handled : %x\n", pixfmt);
472 mt9m111->pixfmt = pixfmt;
477 static int mt9m111_set_fmt(struct soc_camera_device *icd,
478 __u32 pixfmt, struct v4l2_rect *rect)
480 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
483 mt9m111->left = rect->left;
484 mt9m111->top = rect->top;
485 mt9m111->width = rect->width;
486 mt9m111->height = rect->height;
488 dev_dbg(&icd->dev, "%s fmt=%x left=%d, top=%d, width=%d, height=%d\n",
489 __func__, pixfmt, mt9m111->left, mt9m111->top, mt9m111->width,
492 ret = mt9m111_setup_rect(icd);
494 ret = mt9m111_set_pixfmt(icd, pixfmt);
498 static int mt9m111_try_fmt(struct soc_camera_device *icd,
499 struct v4l2_format *f)
501 if (f->fmt.pix.height > MT9M111_MAX_HEIGHT)
502 f->fmt.pix.height = MT9M111_MAX_HEIGHT;
503 if (f->fmt.pix.width > MT9M111_MAX_WIDTH)
504 f->fmt.pix.width = MT9M111_MAX_WIDTH;
509 static int mt9m111_get_chip_id(struct soc_camera_device *icd,
510 struct v4l2_chip_ident *id)
512 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
514 if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR)
517 if (id->match_chip != mt9m111->client->addr)
520 id->ident = mt9m111->model;
526 #ifdef CONFIG_VIDEO_ADV_DEBUG
527 static int mt9m111_get_register(struct soc_camera_device *icd,
528 struct v4l2_register *reg)
532 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
534 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
536 if (reg->match_chip != mt9m111->client->addr)
539 val = mt9m111_reg_read(icd, reg->reg);
542 if (reg->val > 0xffff)
548 static int mt9m111_set_register(struct soc_camera_device *icd,
549 struct v4l2_register *reg)
551 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
553 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
556 if (reg->match_chip != mt9m111->client->addr)
559 if (mt9m111_reg_write(icd, reg->reg, reg->val) < 0)
566 static const struct v4l2_queryctrl mt9m111_controls[] = {
568 .id = V4L2_CID_VFLIP,
569 .type = V4L2_CTRL_TYPE_BOOLEAN,
570 .name = "Flip Verticaly",
576 .id = V4L2_CID_HFLIP,
577 .type = V4L2_CTRL_TYPE_BOOLEAN,
578 .name = "Flip Horizontaly",
583 }, { /* gain = 1/32*val (=>gain=1 if val==32) */
585 .type = V4L2_CTRL_TYPE_INTEGER,
588 .maximum = 63 * 2 * 2,
591 .flags = V4L2_CTRL_FLAG_SLIDER,
593 .id = V4L2_CID_EXPOSURE_AUTO,
594 .type = V4L2_CTRL_TYPE_BOOLEAN,
595 .name = "Auto Exposure",
603 static int mt9m111_video_probe(struct soc_camera_device *);
604 static void mt9m111_video_remove(struct soc_camera_device *);
605 static int mt9m111_get_control(struct soc_camera_device *,
606 struct v4l2_control *);
607 static int mt9m111_set_control(struct soc_camera_device *,
608 struct v4l2_control *);
609 static int mt9m111_resume(struct soc_camera_device *icd);
610 static int mt9m111_init(struct soc_camera_device *icd);
611 static int mt9m111_release(struct soc_camera_device *icd);
613 static struct soc_camera_ops mt9m111_ops = {
614 .owner = THIS_MODULE,
615 .probe = mt9m111_video_probe,
616 .remove = mt9m111_video_remove,
617 .init = mt9m111_init,
618 .resume = mt9m111_resume,
619 .release = mt9m111_release,
620 .start_capture = mt9m111_start_capture,
621 .stop_capture = mt9m111_stop_capture,
622 .set_fmt = mt9m111_set_fmt,
623 .try_fmt = mt9m111_try_fmt,
624 .query_bus_param = mt9m111_query_bus_param,
625 .set_bus_param = mt9m111_set_bus_param,
626 .controls = mt9m111_controls,
627 .num_controls = ARRAY_SIZE(mt9m111_controls),
628 .get_control = mt9m111_get_control,
629 .set_control = mt9m111_set_control,
630 .get_chip_id = mt9m111_get_chip_id,
631 #ifdef CONFIG_VIDEO_ADV_DEBUG
632 .get_register = mt9m111_get_register,
633 .set_register = mt9m111_set_register,
637 static int mt9m111_set_flip(struct soc_camera_device *icd, int flip, int mask)
639 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
642 if (mt9m111->context == HIGHPOWER) {
644 ret = reg_set(READ_MODE_B, mask);
646 ret = reg_clear(READ_MODE_B, mask);
649 ret = reg_set(READ_MODE_A, mask);
651 ret = reg_clear(READ_MODE_A, mask);
657 static int mt9m111_get_global_gain(struct soc_camera_device *icd)
661 data = reg_read(GLOBAL_GAIN);
663 return (data & 0x2f) * (1 << ((data >> 10) & 1)) *
664 (1 << ((data >> 9) & 1));
668 static int mt9m111_set_global_gain(struct soc_camera_device *icd, int gain)
672 if (gain > 63 * 2 * 2)
676 if ((gain >= 64 * 2) && (gain < 63 * 2 * 2))
677 val = (1 << 10) | (1 << 9) | (gain / 4);
678 else if ((gain >= 64) && (gain < 64 * 2))
679 val = (1 << 9) | (gain / 2);
683 return reg_write(GLOBAL_GAIN, val);
686 static int mt9m111_set_autoexposure(struct soc_camera_device *icd, int on)
688 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
692 ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
694 ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
697 mt9m111->autoexposure = on;
701 static int mt9m111_get_control(struct soc_camera_device *icd,
702 struct v4l2_control *ctrl)
704 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
709 if (mt9m111->context == HIGHPOWER)
710 data = reg_read(READ_MODE_B);
712 data = reg_read(READ_MODE_A);
716 ctrl->value = !!(data & MT9M111_RMB_MIRROR_ROWS);
719 if (mt9m111->context == HIGHPOWER)
720 data = reg_read(READ_MODE_B);
722 data = reg_read(READ_MODE_A);
726 ctrl->value = !!(data & MT9M111_RMB_MIRROR_COLS);
729 data = mt9m111_get_global_gain(icd);
734 case V4L2_CID_EXPOSURE_AUTO:
735 ctrl->value = mt9m111->autoexposure;
741 static int mt9m111_set_control(struct soc_camera_device *icd,
742 struct v4l2_control *ctrl)
744 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
745 const struct v4l2_queryctrl *qctrl;
748 qctrl = soc_camera_find_qctrl(&mt9m111_ops, ctrl->id);
755 mt9m111->vflip = ctrl->value;
756 ret = mt9m111_set_flip(icd, ctrl->value,
757 MT9M111_RMB_MIRROR_ROWS);
760 mt9m111->hflip = ctrl->value;
761 ret = mt9m111_set_flip(icd, ctrl->value,
762 MT9M111_RMB_MIRROR_COLS);
765 ret = mt9m111_set_global_gain(icd, ctrl->value);
767 case V4L2_CID_EXPOSURE_AUTO:
768 ret = mt9m111_set_autoexposure(icd, ctrl->value);
777 static int mt9m111_restore_state(struct soc_camera_device *icd)
779 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
781 mt9m111_set_context(icd, mt9m111->context);
782 mt9m111_set_pixfmt(icd, mt9m111->pixfmt);
783 mt9m111_setup_rect(icd);
784 mt9m111_set_flip(icd, mt9m111->hflip, MT9M111_RMB_MIRROR_COLS);
785 mt9m111_set_flip(icd, mt9m111->vflip, MT9M111_RMB_MIRROR_ROWS);
786 mt9m111_set_global_gain(icd, icd->gain);
787 mt9m111_set_autoexposure(icd, mt9m111->autoexposure);
791 static int mt9m111_resume(struct soc_camera_device *icd)
793 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
796 if (mt9m111->powered) {
797 ret = mt9m111_enable(icd);
799 ret = mt9m111_reset(icd);
801 ret = mt9m111_restore_state(icd);
806 static int mt9m111_init(struct soc_camera_device *icd)
808 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
811 mt9m111->context = HIGHPOWER;
812 ret = mt9m111_enable(icd);
814 ret = mt9m111_reset(icd);
816 ret = mt9m111_set_context(icd, mt9m111->context);
818 ret = mt9m111_set_autoexposure(icd, mt9m111->autoexposure);
820 dev_err(&icd->dev, "mt9m111 init failed: %d\n", ret);
824 static int mt9m111_release(struct soc_camera_device *icd)
828 ret = mt9m111_disable(icd);
830 dev_err(&icd->dev, "mt9m111 release failed: %d\n", ret);
836 * Interface active, can use i2c. If it fails, it can indeed mean, that
837 * this wasn't our capture interface, so, we wait for the right one
839 static int mt9m111_video_probe(struct soc_camera_device *icd)
841 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
846 * We must have a parent by now. And it cannot be a wrong one.
847 * So this entire test is completely redundant.
849 if (!icd->dev.parent ||
850 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
853 ret = mt9m111_enable(icd);
856 ret = mt9m111_reset(icd);
860 data = reg_read(CHIP_VERSION);
864 mt9m111->model = V4L2_IDENT_MT9M111;
865 icd->formats = mt9m111_colour_formats;
866 icd->num_formats = ARRAY_SIZE(mt9m111_colour_formats);
871 "No MT9M111 chip detected, register read %x\n", data);
875 dev_info(&icd->dev, "Detected a MT9M111 chip ID 0x143a\n");
877 ret = soc_camera_video_start(icd);
881 mt9m111->autoexposure = 1;
883 mt9m111->swap_rgb_even_odd = 1;
884 mt9m111->swap_rgb_red_blue = 1;
892 static void mt9m111_video_remove(struct soc_camera_device *icd)
894 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
896 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m111->client->addr,
897 mt9m111->icd.dev.parent, mt9m111->icd.vdev);
898 soc_camera_video_stop(&mt9m111->icd);
901 static int mt9m111_probe(struct i2c_client *client,
902 const struct i2c_device_id *did)
904 struct mt9m111 *mt9m111;
905 struct soc_camera_device *icd;
906 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
907 struct soc_camera_link *icl = client->dev.platform_data;
911 dev_err(&client->dev, "MT9M111 driver needs platform data\n");
915 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
916 dev_warn(&adapter->dev,
917 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
921 mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL);
925 mt9m111->client = client;
926 i2c_set_clientdata(client, mt9m111);
928 /* Second stage probe - when a capture adapter is there */
930 icd->ops = &mt9m111_ops;
931 icd->control = &client->dev;
932 icd->x_min = MT9M111_MIN_DARK_COLS;
933 icd->y_min = MT9M111_MIN_DARK_ROWS;
934 icd->x_current = icd->x_min;
935 icd->y_current = icd->y_min;
936 icd->width_min = MT9M111_MIN_DARK_ROWS;
937 icd->width_max = MT9M111_MAX_WIDTH;
938 icd->height_min = MT9M111_MIN_DARK_COLS;
939 icd->height_max = MT9M111_MAX_HEIGHT;
941 icd->iface = icl->bus_id;
943 ret = soc_camera_device_register(icd);
953 static int mt9m111_remove(struct i2c_client *client)
955 struct mt9m111 *mt9m111 = i2c_get_clientdata(client);
956 soc_camera_device_unregister(&mt9m111->icd);
962 static const struct i2c_device_id mt9m111_id[] = {
966 MODULE_DEVICE_TABLE(i2c, mt9m111_id);
968 static struct i2c_driver mt9m111_i2c_driver = {
972 .probe = mt9m111_probe,
973 .remove = mt9m111_remove,
974 .id_table = mt9m111_id,
977 static int __init mt9m111_mod_init(void)
979 return i2c_add_driver(&mt9m111_i2c_driver);
982 static void __exit mt9m111_mod_exit(void)
984 i2c_del_driver(&mt9m111_i2c_driver);
987 module_init(mt9m111_mod_init);
988 module_exit(mt9m111_mod_exit);
990 MODULE_DESCRIPTION("Micron MT9M111 Camera driver");
991 MODULE_AUTHOR("Robert Jarzmik");
992 MODULE_LICENSE("GPL");