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 #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 v4l2_subdev subdev
;
152 int model
; /* V4L2_IDENT_MT9M11x* codes from v4l2-chip-ident.h */
153 enum mt9m111_context context
;
154 struct v4l2_rect rect
;
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;
166 unsigned int autowhitebalance
:1;
169 static struct mt9m111
*to_mt9m111(const struct i2c_client
*client
)
171 return container_of(i2c_get_clientdata(client
), struct mt9m111
, subdev
);
174 static int reg_page_map_set(struct i2c_client
*client
, const u16 reg
)
178 static int lastpage
= -1; /* PageMap cache value */
181 if (page
== lastpage
)
186 ret
= i2c_smbus_write_word_data(client
, MT9M111_PAGE_MAP
, swab16(page
));
192 static int mt9m111_reg_read(struct i2c_client
*client
, const u16 reg
)
196 ret
= reg_page_map_set(client
, reg
);
198 ret
= swab16(i2c_smbus_read_word_data(client
, reg
& 0xff));
200 dev_dbg(&client
->dev
, "read reg.%03x -> %04x\n", reg
, ret
);
204 static int mt9m111_reg_write(struct i2c_client
*client
, const u16 reg
,
209 ret
= reg_page_map_set(client
, reg
);
211 ret
= i2c_smbus_write_word_data(client
, reg
& 0xff,
213 dev_dbg(&client
->dev
, "write reg.%03x = %04x -> %d\n", reg
, data
, ret
);
217 static int mt9m111_reg_set(struct i2c_client
*client
, const u16 reg
,
222 ret
= mt9m111_reg_read(client
, reg
);
224 ret
= mt9m111_reg_write(client
, reg
, ret
| data
);
228 static int mt9m111_reg_clear(struct i2c_client
*client
, const u16 reg
,
233 ret
= mt9m111_reg_read(client
, reg
);
234 return mt9m111_reg_write(client
, reg
, ret
& ~data
);
237 static int mt9m111_set_context(struct i2c_client
*client
,
238 enum mt9m111_context ctxt
)
240 int valB
= MT9M111_CTXT_CTRL_RESTART
| MT9M111_CTXT_CTRL_DEFECTCOR_B
241 | MT9M111_CTXT_CTRL_RESIZE_B
| MT9M111_CTXT_CTRL_CTRL2_B
242 | MT9M111_CTXT_CTRL_GAMMA_B
| MT9M111_CTXT_CTRL_READ_MODE_B
243 | MT9M111_CTXT_CTRL_VBLANK_SEL_B
244 | MT9M111_CTXT_CTRL_HBLANK_SEL_B
;
245 int valA
= MT9M111_CTXT_CTRL_RESTART
;
247 if (ctxt
== HIGHPOWER
)
248 return reg_write(CONTEXT_CONTROL
, valB
);
250 return reg_write(CONTEXT_CONTROL
, valA
);
253 static int mt9m111_setup_rect(struct i2c_client
*client
,
254 struct v4l2_rect
*rect
)
256 struct mt9m111
*mt9m111
= to_mt9m111(client
);
257 int ret
, is_raw_format
;
258 int width
= rect
->width
;
259 int height
= rect
->height
;
261 if (mt9m111
->pixfmt
== V4L2_PIX_FMT_SBGGR8
||
262 mt9m111
->pixfmt
== V4L2_PIX_FMT_SBGGR16
)
267 ret
= reg_write(COLUMN_START
, rect
->left
);
269 ret
= reg_write(ROW_START
, rect
->top
);
273 ret
= reg_write(WINDOW_WIDTH
, width
);
275 ret
= reg_write(WINDOW_HEIGHT
, height
);
278 ret
= reg_write(REDUCER_XZOOM_B
, MT9M111_MAX_WIDTH
);
280 ret
= reg_write(REDUCER_YZOOM_B
, MT9M111_MAX_HEIGHT
);
282 ret
= reg_write(REDUCER_XSIZE_B
, width
);
284 ret
= reg_write(REDUCER_YSIZE_B
, height
);
286 ret
= reg_write(REDUCER_XZOOM_A
, MT9M111_MAX_WIDTH
);
288 ret
= reg_write(REDUCER_YZOOM_A
, MT9M111_MAX_HEIGHT
);
290 ret
= reg_write(REDUCER_XSIZE_A
, width
);
292 ret
= reg_write(REDUCER_YSIZE_A
, height
);
298 static int mt9m111_setup_pixfmt(struct i2c_client
*client
, u16 outfmt
)
302 ret
= reg_write(OUTPUT_FORMAT_CTRL2_A
, outfmt
);
304 ret
= reg_write(OUTPUT_FORMAT_CTRL2_B
, outfmt
);
308 static int mt9m111_setfmt_bayer8(struct i2c_client
*client
)
310 return mt9m111_setup_pixfmt(client
, MT9M111_OUTFMT_PROCESSED_BAYER
);
313 static int mt9m111_setfmt_bayer10(struct i2c_client
*client
)
315 return mt9m111_setup_pixfmt(client
, MT9M111_OUTFMT_BYPASS_IFP
);
318 static int mt9m111_setfmt_rgb565(struct i2c_client
*client
)
320 struct mt9m111
*mt9m111
= to_mt9m111(client
);
323 if (mt9m111
->swap_rgb_red_blue
)
324 val
|= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr
;
325 if (mt9m111
->swap_rgb_even_odd
)
326 val
|= MT9M111_OUTFMT_SWAP_RGB_EVEN
;
327 val
|= MT9M111_OUTFMT_RGB
| MT9M111_OUTFMT_RGB565
;
329 return mt9m111_setup_pixfmt(client
, val
);
332 static int mt9m111_setfmt_rgb555(struct i2c_client
*client
)
334 struct mt9m111
*mt9m111
= to_mt9m111(client
);
337 if (mt9m111
->swap_rgb_red_blue
)
338 val
|= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr
;
339 if (mt9m111
->swap_rgb_even_odd
)
340 val
|= MT9M111_OUTFMT_SWAP_RGB_EVEN
;
341 val
|= MT9M111_OUTFMT_RGB
| MT9M111_OUTFMT_RGB555
;
343 return mt9m111_setup_pixfmt(client
, val
);
346 static int mt9m111_setfmt_yuv(struct i2c_client
*client
)
348 struct mt9m111
*mt9m111
= to_mt9m111(client
);
351 if (mt9m111
->swap_yuv_cb_cr
)
352 val
|= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr
;
353 if (mt9m111
->swap_yuv_y_chromas
)
354 val
|= MT9M111_OUTFMT_SWAP_YCbCr_C_Y
;
356 return mt9m111_setup_pixfmt(client
, val
);
359 static int mt9m111_enable(struct i2c_client
*client
)
361 struct mt9m111
*mt9m111
= to_mt9m111(client
);
364 ret
= reg_set(RESET
, MT9M111_RESET_CHIP_ENABLE
);
366 mt9m111
->powered
= 1;
370 static int mt9m111_reset(struct i2c_client
*client
)
374 ret
= reg_set(RESET
, MT9M111_RESET_RESET_MODE
);
376 ret
= reg_set(RESET
, MT9M111_RESET_RESET_SOC
);
378 ret
= reg_clear(RESET
, MT9M111_RESET_RESET_MODE
379 | MT9M111_RESET_RESET_SOC
);
384 static unsigned long mt9m111_query_bus_param(struct soc_camera_device
*icd
)
386 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
387 unsigned long flags
= SOCAM_MASTER
| SOCAM_PCLK_SAMPLE_RISING
|
388 SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_HIGH
|
389 SOCAM_DATA_ACTIVE_HIGH
| SOCAM_DATAWIDTH_8
;
391 return soc_camera_apply_sensor_flags(icl
, flags
);
394 static int mt9m111_set_bus_param(struct soc_camera_device
*icd
, unsigned long f
)
399 static int mt9m111_make_rect(struct i2c_client
*client
,
400 struct v4l2_rect
*rect
)
402 struct mt9m111
*mt9m111
= to_mt9m111(client
);
404 if (mt9m111
->pixfmt
== V4L2_PIX_FMT_SBGGR8
||
405 mt9m111
->pixfmt
== V4L2_PIX_FMT_SBGGR16
) {
406 /* Bayer format - even size lengths */
407 rect
->width
= ALIGN(rect
->width
, 2);
408 rect
->height
= ALIGN(rect
->height
, 2);
409 /* Let the user play with the starting pixel */
412 /* FIXME: the datasheet doesn't specify minimum sizes */
413 soc_camera_limit_side(&rect
->left
, &rect
->width
,
414 MT9M111_MIN_DARK_COLS
, 2, MT9M111_MAX_WIDTH
);
416 soc_camera_limit_side(&rect
->top
, &rect
->height
,
417 MT9M111_MIN_DARK_ROWS
, 2, MT9M111_MAX_HEIGHT
);
419 return mt9m111_setup_rect(client
, rect
);
422 static int mt9m111_s_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
424 struct v4l2_rect rect
= a
->c
;
425 struct i2c_client
*client
= sd
->priv
;
426 struct mt9m111
*mt9m111
= to_mt9m111(client
);
429 dev_dbg(&client
->dev
, "%s left=%d, top=%d, width=%d, height=%d\n",
430 __func__
, rect
.left
, rect
.top
, rect
.width
, rect
.height
);
432 ret
= mt9m111_make_rect(client
, &rect
);
434 mt9m111
->rect
= rect
;
438 static int mt9m111_g_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
440 struct i2c_client
*client
= sd
->priv
;
441 struct mt9m111
*mt9m111
= to_mt9m111(client
);
443 a
->c
= mt9m111
->rect
;
444 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
449 static int mt9m111_cropcap(struct v4l2_subdev
*sd
, struct v4l2_cropcap
*a
)
451 a
->bounds
.left
= MT9M111_MIN_DARK_COLS
;
452 a
->bounds
.top
= MT9M111_MIN_DARK_ROWS
;
453 a
->bounds
.width
= MT9M111_MAX_WIDTH
;
454 a
->bounds
.height
= MT9M111_MAX_HEIGHT
;
455 a
->defrect
= a
->bounds
;
456 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
457 a
->pixelaspect
.numerator
= 1;
458 a
->pixelaspect
.denominator
= 1;
463 static int mt9m111_g_fmt(struct v4l2_subdev
*sd
, struct v4l2_format
*f
)
465 struct i2c_client
*client
= sd
->priv
;
466 struct mt9m111
*mt9m111
= to_mt9m111(client
);
467 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
469 pix
->width
= mt9m111
->rect
.width
;
470 pix
->height
= mt9m111
->rect
.height
;
471 pix
->pixelformat
= mt9m111
->pixfmt
;
472 pix
->field
= V4L2_FIELD_NONE
;
473 pix
->colorspace
= V4L2_COLORSPACE_SRGB
;
478 static int mt9m111_set_pixfmt(struct i2c_client
*client
, u32 pixfmt
)
480 struct mt9m111
*mt9m111
= to_mt9m111(client
);
484 case V4L2_PIX_FMT_SBGGR8
:
485 ret
= mt9m111_setfmt_bayer8(client
);
487 case V4L2_PIX_FMT_SBGGR16
:
488 ret
= mt9m111_setfmt_bayer10(client
);
490 case V4L2_PIX_FMT_RGB555
:
491 ret
= mt9m111_setfmt_rgb555(client
);
493 case V4L2_PIX_FMT_RGB565
:
494 ret
= mt9m111_setfmt_rgb565(client
);
496 case V4L2_PIX_FMT_UYVY
:
497 mt9m111
->swap_yuv_y_chromas
= 0;
498 mt9m111
->swap_yuv_cb_cr
= 0;
499 ret
= mt9m111_setfmt_yuv(client
);
501 case V4L2_PIX_FMT_VYUY
:
502 mt9m111
->swap_yuv_y_chromas
= 0;
503 mt9m111
->swap_yuv_cb_cr
= 1;
504 ret
= mt9m111_setfmt_yuv(client
);
506 case V4L2_PIX_FMT_YUYV
:
507 mt9m111
->swap_yuv_y_chromas
= 1;
508 mt9m111
->swap_yuv_cb_cr
= 0;
509 ret
= mt9m111_setfmt_yuv(client
);
511 case V4L2_PIX_FMT_YVYU
:
512 mt9m111
->swap_yuv_y_chromas
= 1;
513 mt9m111
->swap_yuv_cb_cr
= 1;
514 ret
= mt9m111_setfmt_yuv(client
);
517 dev_err(&client
->dev
, "Pixel format not handled : %x\n",
523 mt9m111
->pixfmt
= pixfmt
;
528 static int mt9m111_s_fmt(struct v4l2_subdev
*sd
, struct v4l2_format
*f
)
530 struct i2c_client
*client
= sd
->priv
;
531 struct mt9m111
*mt9m111
= to_mt9m111(client
);
532 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
533 struct v4l2_rect rect
= {
534 .left
= mt9m111
->rect
.left
,
535 .top
= mt9m111
->rect
.top
,
537 .height
= pix
->height
,
541 dev_dbg(&client
->dev
,
542 "%s fmt=%x left=%d, top=%d, width=%d, height=%d\n", __func__
,
543 pix
->pixelformat
, rect
.left
, rect
.top
, rect
.width
, rect
.height
);
545 ret
= mt9m111_make_rect(client
, &rect
);
547 ret
= mt9m111_set_pixfmt(client
, pix
->pixelformat
);
549 mt9m111
->rect
= rect
;
553 static int mt9m111_try_fmt(struct v4l2_subdev
*sd
, struct v4l2_format
*f
)
555 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
556 bool bayer
= pix
->pixelformat
== V4L2_PIX_FMT_SBGGR8
||
557 pix
->pixelformat
== V4L2_PIX_FMT_SBGGR16
;
560 * With Bayer format enforce even side lengths, but let the user play
561 * with the starting pixel
564 if (pix
->height
> MT9M111_MAX_HEIGHT
)
565 pix
->height
= MT9M111_MAX_HEIGHT
;
566 else if (pix
->height
< 2)
569 pix
->height
= ALIGN(pix
->height
, 2);
571 if (pix
->width
> MT9M111_MAX_WIDTH
)
572 pix
->width
= MT9M111_MAX_WIDTH
;
573 else if (pix
->width
< 2)
576 pix
->width
= ALIGN(pix
->width
, 2);
581 static int mt9m111_g_chip_ident(struct v4l2_subdev
*sd
,
582 struct v4l2_dbg_chip_ident
*id
)
584 struct i2c_client
*client
= sd
->priv
;
585 struct mt9m111
*mt9m111
= to_mt9m111(client
);
587 if (id
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
590 if (id
->match
.addr
!= client
->addr
)
593 id
->ident
= mt9m111
->model
;
599 #ifdef CONFIG_VIDEO_ADV_DEBUG
600 static int mt9m111_g_register(struct v4l2_subdev
*sd
,
601 struct v4l2_dbg_register
*reg
)
603 struct i2c_client
*client
= sd
->priv
;
606 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0x2ff)
608 if (reg
->match
.addr
!= client
->addr
)
611 val
= mt9m111_reg_read(client
, reg
->reg
);
615 if (reg
->val
> 0xffff)
621 static int mt9m111_s_register(struct v4l2_subdev
*sd
,
622 struct v4l2_dbg_register
*reg
)
624 struct i2c_client
*client
= sd
->priv
;
626 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
|| reg
->reg
> 0x2ff)
629 if (reg
->match
.addr
!= client
->addr
)
632 if (mt9m111_reg_write(client
, reg
->reg
, reg
->val
) < 0)
639 static const struct v4l2_queryctrl mt9m111_controls
[] = {
641 .id
= V4L2_CID_VFLIP
,
642 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
643 .name
= "Flip Verticaly",
649 .id
= V4L2_CID_HFLIP
,
650 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
651 .name
= "Flip Horizontaly",
656 }, { /* gain = 1/32*val (=>gain=1 if val==32) */
658 .type
= V4L2_CTRL_TYPE_INTEGER
,
661 .maximum
= 63 * 2 * 2,
664 .flags
= V4L2_CTRL_FLAG_SLIDER
,
666 .id
= V4L2_CID_EXPOSURE_AUTO
,
667 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
668 .name
= "Auto Exposure",
676 static int mt9m111_resume(struct soc_camera_device
*icd
);
677 static int mt9m111_suspend(struct soc_camera_device
*icd
, pm_message_t state
);
679 static struct soc_camera_ops mt9m111_ops
= {
680 .suspend
= mt9m111_suspend
,
681 .resume
= mt9m111_resume
,
682 .query_bus_param
= mt9m111_query_bus_param
,
683 .set_bus_param
= mt9m111_set_bus_param
,
684 .controls
= mt9m111_controls
,
685 .num_controls
= ARRAY_SIZE(mt9m111_controls
),
688 static int mt9m111_set_flip(struct i2c_client
*client
, int flip
, int mask
)
690 struct mt9m111
*mt9m111
= to_mt9m111(client
);
693 if (mt9m111
->context
== HIGHPOWER
) {
695 ret
= reg_set(READ_MODE_B
, mask
);
697 ret
= reg_clear(READ_MODE_B
, mask
);
700 ret
= reg_set(READ_MODE_A
, mask
);
702 ret
= reg_clear(READ_MODE_A
, mask
);
708 static int mt9m111_get_global_gain(struct i2c_client
*client
)
712 data
= reg_read(GLOBAL_GAIN
);
714 return (data
& 0x2f) * (1 << ((data
>> 10) & 1)) *
715 (1 << ((data
>> 9) & 1));
719 static int mt9m111_set_global_gain(struct i2c_client
*client
, int gain
)
721 struct mt9m111
*mt9m111
= to_mt9m111(client
);
724 if (gain
> 63 * 2 * 2)
727 mt9m111
->gain
= gain
;
728 if ((gain
>= 64 * 2) && (gain
< 63 * 2 * 2))
729 val
= (1 << 10) | (1 << 9) | (gain
/ 4);
730 else if ((gain
>= 64) && (gain
< 64 * 2))
731 val
= (1 << 9) | (gain
/ 2);
735 return reg_write(GLOBAL_GAIN
, val
);
738 static int mt9m111_set_autoexposure(struct i2c_client
*client
, int on
)
740 struct mt9m111
*mt9m111
= to_mt9m111(client
);
744 ret
= reg_set(OPER_MODE_CTRL
, MT9M111_OPMODE_AUTOEXPO_EN
);
746 ret
= reg_clear(OPER_MODE_CTRL
, MT9M111_OPMODE_AUTOEXPO_EN
);
749 mt9m111
->autoexposure
= on
;
754 static int mt9m111_set_autowhitebalance(struct i2c_client
*client
, int on
)
756 struct mt9m111
*mt9m111
= to_mt9m111(client
);
760 ret
= reg_set(OPER_MODE_CTRL
, MT9M111_OPMODE_AUTOWHITEBAL_EN
);
762 ret
= reg_clear(OPER_MODE_CTRL
, MT9M111_OPMODE_AUTOWHITEBAL_EN
);
765 mt9m111
->autowhitebalance
= on
;
770 static int mt9m111_g_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
772 struct i2c_client
*client
= sd
->priv
;
773 struct mt9m111
*mt9m111
= to_mt9m111(client
);
778 if (mt9m111
->context
== HIGHPOWER
)
779 data
= reg_read(READ_MODE_B
);
781 data
= reg_read(READ_MODE_A
);
785 ctrl
->value
= !!(data
& MT9M111_RMB_MIRROR_ROWS
);
788 if (mt9m111
->context
== HIGHPOWER
)
789 data
= reg_read(READ_MODE_B
);
791 data
= reg_read(READ_MODE_A
);
795 ctrl
->value
= !!(data
& MT9M111_RMB_MIRROR_COLS
);
798 data
= mt9m111_get_global_gain(client
);
803 case V4L2_CID_EXPOSURE_AUTO
:
804 ctrl
->value
= mt9m111
->autoexposure
;
806 case V4L2_CID_AUTO_WHITE_BALANCE
:
807 ctrl
->value
= mt9m111
->autowhitebalance
;
813 static int mt9m111_s_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
815 struct i2c_client
*client
= sd
->priv
;
816 struct mt9m111
*mt9m111
= to_mt9m111(client
);
817 const struct v4l2_queryctrl
*qctrl
;
820 qctrl
= soc_camera_find_qctrl(&mt9m111_ops
, ctrl
->id
);
826 mt9m111
->vflip
= ctrl
->value
;
827 ret
= mt9m111_set_flip(client
, ctrl
->value
,
828 MT9M111_RMB_MIRROR_ROWS
);
831 mt9m111
->hflip
= ctrl
->value
;
832 ret
= mt9m111_set_flip(client
, ctrl
->value
,
833 MT9M111_RMB_MIRROR_COLS
);
836 ret
= mt9m111_set_global_gain(client
, ctrl
->value
);
838 case V4L2_CID_EXPOSURE_AUTO
:
839 ret
= mt9m111_set_autoexposure(client
, ctrl
->value
);
841 case V4L2_CID_AUTO_WHITE_BALANCE
:
842 ret
= mt9m111_set_autowhitebalance(client
, ctrl
->value
);
851 static int mt9m111_suspend(struct soc_camera_device
*icd
, pm_message_t state
)
853 struct i2c_client
*client
= to_i2c_client(to_soc_camera_control(icd
));
854 struct mt9m111
*mt9m111
= to_mt9m111(client
);
856 mt9m111
->gain
= mt9m111_get_global_gain(client
);
861 static int mt9m111_restore_state(struct i2c_client
*client
)
863 struct mt9m111
*mt9m111
= to_mt9m111(client
);
865 mt9m111_set_context(client
, mt9m111
->context
);
866 mt9m111_set_pixfmt(client
, mt9m111
->pixfmt
);
867 mt9m111_setup_rect(client
, &mt9m111
->rect
);
868 mt9m111_set_flip(client
, mt9m111
->hflip
, MT9M111_RMB_MIRROR_COLS
);
869 mt9m111_set_flip(client
, mt9m111
->vflip
, MT9M111_RMB_MIRROR_ROWS
);
870 mt9m111_set_global_gain(client
, mt9m111
->gain
);
871 mt9m111_set_autoexposure(client
, mt9m111
->autoexposure
);
872 mt9m111_set_autowhitebalance(client
, mt9m111
->autowhitebalance
);
876 static int mt9m111_resume(struct soc_camera_device
*icd
)
878 struct i2c_client
*client
= to_i2c_client(to_soc_camera_control(icd
));
879 struct mt9m111
*mt9m111
= to_mt9m111(client
);
882 if (mt9m111
->powered
) {
883 ret
= mt9m111_enable(client
);
885 ret
= mt9m111_reset(client
);
887 ret
= mt9m111_restore_state(client
);
892 static int mt9m111_init(struct i2c_client
*client
)
894 struct mt9m111
*mt9m111
= to_mt9m111(client
);
897 mt9m111
->context
= HIGHPOWER
;
898 ret
= mt9m111_enable(client
);
900 ret
= mt9m111_reset(client
);
902 ret
= mt9m111_set_context(client
, mt9m111
->context
);
904 ret
= mt9m111_set_autoexposure(client
, mt9m111
->autoexposure
);
906 dev_err(&client
->dev
, "mt9m11x init failed: %d\n", ret
);
911 * Interface active, can use i2c. If it fails, it can indeed mean, that
912 * this wasn't our capture interface, so, we wait for the right one
914 static int mt9m111_video_probe(struct soc_camera_device
*icd
,
915 struct i2c_client
*client
)
917 struct mt9m111
*mt9m111
= to_mt9m111(client
);
922 * We must have a parent by now. And it cannot be a wrong one.
923 * So this entire test is completely redundant.
925 if (!icd
->dev
.parent
||
926 to_soc_camera_host(icd
->dev
.parent
)->nr
!= icd
->iface
)
929 mt9m111
->autoexposure
= 1;
930 mt9m111
->autowhitebalance
= 1;
932 mt9m111
->swap_rgb_even_odd
= 1;
933 mt9m111
->swap_rgb_red_blue
= 1;
935 ret
= mt9m111_init(client
);
939 data
= reg_read(CHIP_VERSION
);
942 case 0x143a: /* MT9M111 */
943 mt9m111
->model
= V4L2_IDENT_MT9M111
;
945 case 0x148c: /* MT9M112 */
946 mt9m111
->model
= V4L2_IDENT_MT9M112
;
950 dev_err(&client
->dev
,
951 "No MT9M11x chip detected, register read %x\n", data
);
955 icd
->formats
= mt9m111_colour_formats
;
956 icd
->num_formats
= ARRAY_SIZE(mt9m111_colour_formats
);
958 dev_info(&client
->dev
, "Detected a MT9M11x chip ID %x\n", data
);
964 static struct v4l2_subdev_core_ops mt9m111_subdev_core_ops
= {
965 .g_ctrl
= mt9m111_g_ctrl
,
966 .s_ctrl
= mt9m111_s_ctrl
,
967 .g_chip_ident
= mt9m111_g_chip_ident
,
968 #ifdef CONFIG_VIDEO_ADV_DEBUG
969 .g_register
= mt9m111_g_register
,
970 .s_register
= mt9m111_s_register
,
974 static struct v4l2_subdev_video_ops mt9m111_subdev_video_ops
= {
975 .s_fmt
= mt9m111_s_fmt
,
976 .g_fmt
= mt9m111_g_fmt
,
977 .try_fmt
= mt9m111_try_fmt
,
978 .s_crop
= mt9m111_s_crop
,
979 .g_crop
= mt9m111_g_crop
,
980 .cropcap
= mt9m111_cropcap
,
983 static struct v4l2_subdev_ops mt9m111_subdev_ops
= {
984 .core
= &mt9m111_subdev_core_ops
,
985 .video
= &mt9m111_subdev_video_ops
,
988 static int mt9m111_probe(struct i2c_client
*client
,
989 const struct i2c_device_id
*did
)
991 struct mt9m111
*mt9m111
;
992 struct soc_camera_device
*icd
= client
->dev
.platform_data
;
993 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
994 struct soc_camera_link
*icl
;
998 dev_err(&client
->dev
, "MT9M11x: missing soc-camera data!\n");
1002 icl
= to_soc_camera_link(icd
);
1004 dev_err(&client
->dev
, "MT9M11x driver needs platform data\n");
1008 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
)) {
1009 dev_warn(&adapter
->dev
,
1010 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
1014 mt9m111
= kzalloc(sizeof(struct mt9m111
), GFP_KERNEL
);
1018 v4l2_i2c_subdev_init(&mt9m111
->subdev
, client
, &mt9m111_subdev_ops
);
1020 /* Second stage probe - when a capture adapter is there */
1021 icd
->ops
= &mt9m111_ops
;
1022 icd
->y_skip_top
= 0;
1024 mt9m111
->rect
.left
= MT9M111_MIN_DARK_COLS
;
1025 mt9m111
->rect
.top
= MT9M111_MIN_DARK_ROWS
;
1026 mt9m111
->rect
.width
= MT9M111_MAX_WIDTH
;
1027 mt9m111
->rect
.height
= MT9M111_MAX_HEIGHT
;
1029 ret
= mt9m111_video_probe(icd
, client
);
1032 i2c_set_clientdata(client
, NULL
);
1039 static int mt9m111_remove(struct i2c_client
*client
)
1041 struct mt9m111
*mt9m111
= to_mt9m111(client
);
1042 struct soc_camera_device
*icd
= client
->dev
.platform_data
;
1045 i2c_set_clientdata(client
, NULL
);
1046 client
->driver
= NULL
;
1052 static const struct i2c_device_id mt9m111_id
[] = {
1056 MODULE_DEVICE_TABLE(i2c
, mt9m111_id
);
1058 static struct i2c_driver mt9m111_i2c_driver
= {
1062 .probe
= mt9m111_probe
,
1063 .remove
= mt9m111_remove
,
1064 .id_table
= mt9m111_id
,
1067 static int __init
mt9m111_mod_init(void)
1069 return i2c_add_driver(&mt9m111_i2c_driver
);
1072 static void __exit
mt9m111_mod_exit(void)
1074 i2c_del_driver(&mt9m111_i2c_driver
);
1077 module_init(mt9m111_mod_init
);
1078 module_exit(mt9m111_mod_exit
);
1080 MODULE_DESCRIPTION("Micron MT9M111/MT9M112 Camera driver");
1081 MODULE_AUTHOR("Robert Jarzmik");
1082 MODULE_LICENSE("GPL");