OMAP3: PM: decouple PER and CORE context save and restore
[linux-ginger.git] / drivers / media / video / mt9m111.c
blob90da699601ea334d056008d4334c828dac3695a0
1 /*
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.
9 */
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 {
146 HIGHPOWER = 0,
147 LOWPOWER,
150 struct mt9m111 {
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;
155 u32 pixfmt;
156 unsigned int gain;
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)
176 int ret;
177 u16 page;
178 static int lastpage = -1; /* PageMap cache value */
180 page = (reg >> 8);
181 if (page == lastpage)
182 return 0;
183 if (page > 2)
184 return -EINVAL;
186 ret = i2c_smbus_write_word_data(client, MT9M111_PAGE_MAP, swab16(page));
187 if (!ret)
188 lastpage = page;
189 return ret;
192 static int mt9m111_reg_read(struct i2c_client *client, const u16 reg)
194 int ret;
196 ret = reg_page_map_set(client, reg);
197 if (!ret)
198 ret = swab16(i2c_smbus_read_word_data(client, reg & 0xff));
200 dev_dbg(&client->dev, "read reg.%03x -> %04x\n", reg, ret);
201 return ret;
204 static int mt9m111_reg_write(struct i2c_client *client, const u16 reg,
205 const u16 data)
207 int ret;
209 ret = reg_page_map_set(client, reg);
210 if (!ret)
211 ret = i2c_smbus_write_word_data(client, reg & 0xff,
212 swab16(data));
213 dev_dbg(&client->dev, "write reg.%03x = %04x -> %d\n", reg, data, ret);
214 return ret;
217 static int mt9m111_reg_set(struct i2c_client *client, const u16 reg,
218 const u16 data)
220 int ret;
222 ret = mt9m111_reg_read(client, reg);
223 if (ret >= 0)
224 ret = mt9m111_reg_write(client, reg, ret | data);
225 return ret;
228 static int mt9m111_reg_clear(struct i2c_client *client, const u16 reg,
229 const u16 data)
231 int ret;
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);
249 else
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)
263 is_raw_format = 1;
264 else
265 is_raw_format = 0;
267 ret = reg_write(COLUMN_START, rect->left);
268 if (!ret)
269 ret = reg_write(ROW_START, rect->top);
271 if (is_raw_format) {
272 if (!ret)
273 ret = reg_write(WINDOW_WIDTH, width);
274 if (!ret)
275 ret = reg_write(WINDOW_HEIGHT, height);
276 } else {
277 if (!ret)
278 ret = reg_write(REDUCER_XZOOM_B, MT9M111_MAX_WIDTH);
279 if (!ret)
280 ret = reg_write(REDUCER_YZOOM_B, MT9M111_MAX_HEIGHT);
281 if (!ret)
282 ret = reg_write(REDUCER_XSIZE_B, width);
283 if (!ret)
284 ret = reg_write(REDUCER_YSIZE_B, height);
285 if (!ret)
286 ret = reg_write(REDUCER_XZOOM_A, MT9M111_MAX_WIDTH);
287 if (!ret)
288 ret = reg_write(REDUCER_YZOOM_A, MT9M111_MAX_HEIGHT);
289 if (!ret)
290 ret = reg_write(REDUCER_XSIZE_A, width);
291 if (!ret)
292 ret = reg_write(REDUCER_YSIZE_A, height);
295 return ret;
298 static int mt9m111_setup_pixfmt(struct i2c_client *client, u16 outfmt)
300 int ret;
302 ret = reg_write(OUTPUT_FORMAT_CTRL2_A, outfmt);
303 if (!ret)
304 ret = reg_write(OUTPUT_FORMAT_CTRL2_B, outfmt);
305 return ret;
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);
321 int val = 0;
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);
335 int val = 0;
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);
349 int val = 0;
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);
362 int ret;
364 ret = reg_set(RESET, MT9M111_RESET_CHIP_ENABLE);
365 if (!ret)
366 mt9m111->powered = 1;
367 return ret;
370 static int mt9m111_reset(struct i2c_client *client)
372 int ret;
374 ret = reg_set(RESET, MT9M111_RESET_RESET_MODE);
375 if (!ret)
376 ret = reg_set(RESET, MT9M111_RESET_RESET_SOC);
377 if (!ret)
378 ret = reg_clear(RESET, MT9M111_RESET_RESET_MODE
379 | MT9M111_RESET_RESET_SOC);
381 return ret;
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)
396 return 0;
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);
427 int ret;
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);
433 if (!ret)
434 mt9m111->rect = rect;
435 return ret;
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;
446 return 0;
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;
460 return 0;
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;
475 return 0;
478 static int mt9m111_set_pixfmt(struct i2c_client *client, u32 pixfmt)
480 struct mt9m111 *mt9m111 = to_mt9m111(client);
481 int ret;
483 switch (pixfmt) {
484 case V4L2_PIX_FMT_SBGGR8:
485 ret = mt9m111_setfmt_bayer8(client);
486 break;
487 case V4L2_PIX_FMT_SBGGR16:
488 ret = mt9m111_setfmt_bayer10(client);
489 break;
490 case V4L2_PIX_FMT_RGB555:
491 ret = mt9m111_setfmt_rgb555(client);
492 break;
493 case V4L2_PIX_FMT_RGB565:
494 ret = mt9m111_setfmt_rgb565(client);
495 break;
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);
500 break;
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);
505 break;
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);
510 break;
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);
515 break;
516 default:
517 dev_err(&client->dev, "Pixel format not handled : %x\n",
518 pixfmt);
519 ret = -EINVAL;
522 if (!ret)
523 mt9m111->pixfmt = pixfmt;
525 return ret;
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,
536 .width = pix->width,
537 .height = pix->height,
539 int ret;
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);
546 if (!ret)
547 ret = mt9m111_set_pixfmt(client, pix->pixelformat);
548 if (!ret)
549 mt9m111->rect = rect;
550 return ret;
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)
567 pix->height = 2;
568 else if (bayer)
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)
574 pix->width = 2;
575 else if (bayer)
576 pix->width = ALIGN(pix->width, 2);
578 return 0;
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)
588 return -EINVAL;
590 if (id->match.addr != client->addr)
591 return -ENODEV;
593 id->ident = mt9m111->model;
594 id->revision = 0;
596 return 0;
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;
604 int val;
606 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
607 return -EINVAL;
608 if (reg->match.addr != client->addr)
609 return -ENODEV;
611 val = mt9m111_reg_read(client, reg->reg);
612 reg->size = 2;
613 reg->val = (u64)val;
615 if (reg->val > 0xffff)
616 return -EIO;
618 return 0;
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)
627 return -EINVAL;
629 if (reg->match.addr != client->addr)
630 return -ENODEV;
632 if (mt9m111_reg_write(client, reg->reg, reg->val) < 0)
633 return -EIO;
635 return 0;
637 #endif
639 static const struct v4l2_queryctrl mt9m111_controls[] = {
641 .id = V4L2_CID_VFLIP,
642 .type = V4L2_CTRL_TYPE_BOOLEAN,
643 .name = "Flip Verticaly",
644 .minimum = 0,
645 .maximum = 1,
646 .step = 1,
647 .default_value = 0,
648 }, {
649 .id = V4L2_CID_HFLIP,
650 .type = V4L2_CTRL_TYPE_BOOLEAN,
651 .name = "Flip Horizontaly",
652 .minimum = 0,
653 .maximum = 1,
654 .step = 1,
655 .default_value = 0,
656 }, { /* gain = 1/32*val (=>gain=1 if val==32) */
657 .id = V4L2_CID_GAIN,
658 .type = V4L2_CTRL_TYPE_INTEGER,
659 .name = "Gain",
660 .minimum = 0,
661 .maximum = 63 * 2 * 2,
662 .step = 1,
663 .default_value = 32,
664 .flags = V4L2_CTRL_FLAG_SLIDER,
665 }, {
666 .id = V4L2_CID_EXPOSURE_AUTO,
667 .type = V4L2_CTRL_TYPE_BOOLEAN,
668 .name = "Auto Exposure",
669 .minimum = 0,
670 .maximum = 1,
671 .step = 1,
672 .default_value = 1,
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);
691 int ret;
693 if (mt9m111->context == HIGHPOWER) {
694 if (flip)
695 ret = reg_set(READ_MODE_B, mask);
696 else
697 ret = reg_clear(READ_MODE_B, mask);
698 } else {
699 if (flip)
700 ret = reg_set(READ_MODE_A, mask);
701 else
702 ret = reg_clear(READ_MODE_A, mask);
705 return ret;
708 static int mt9m111_get_global_gain(struct i2c_client *client)
710 int data;
712 data = reg_read(GLOBAL_GAIN);
713 if (data >= 0)
714 return (data & 0x2f) * (1 << ((data >> 10) & 1)) *
715 (1 << ((data >> 9) & 1));
716 return data;
719 static int mt9m111_set_global_gain(struct i2c_client *client, int gain)
721 struct mt9m111 *mt9m111 = to_mt9m111(client);
722 u16 val;
724 if (gain > 63 * 2 * 2)
725 return -EINVAL;
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);
732 else
733 val = gain;
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);
741 int ret;
743 if (on)
744 ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
745 else
746 ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
748 if (!ret)
749 mt9m111->autoexposure = on;
751 return ret;
754 static int mt9m111_set_autowhitebalance(struct i2c_client *client, int on)
756 struct mt9m111 *mt9m111 = to_mt9m111(client);
757 int ret;
759 if (on)
760 ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
761 else
762 ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
764 if (!ret)
765 mt9m111->autowhitebalance = on;
767 return ret;
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);
774 int data;
776 switch (ctrl->id) {
777 case V4L2_CID_VFLIP:
778 if (mt9m111->context == HIGHPOWER)
779 data = reg_read(READ_MODE_B);
780 else
781 data = reg_read(READ_MODE_A);
783 if (data < 0)
784 return -EIO;
785 ctrl->value = !!(data & MT9M111_RMB_MIRROR_ROWS);
786 break;
787 case V4L2_CID_HFLIP:
788 if (mt9m111->context == HIGHPOWER)
789 data = reg_read(READ_MODE_B);
790 else
791 data = reg_read(READ_MODE_A);
793 if (data < 0)
794 return -EIO;
795 ctrl->value = !!(data & MT9M111_RMB_MIRROR_COLS);
796 break;
797 case V4L2_CID_GAIN:
798 data = mt9m111_get_global_gain(client);
799 if (data < 0)
800 return data;
801 ctrl->value = data;
802 break;
803 case V4L2_CID_EXPOSURE_AUTO:
804 ctrl->value = mt9m111->autoexposure;
805 break;
806 case V4L2_CID_AUTO_WHITE_BALANCE:
807 ctrl->value = mt9m111->autowhitebalance;
808 break;
810 return 0;
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;
818 int ret;
820 qctrl = soc_camera_find_qctrl(&mt9m111_ops, ctrl->id);
821 if (!qctrl)
822 return -EINVAL;
824 switch (ctrl->id) {
825 case V4L2_CID_VFLIP:
826 mt9m111->vflip = ctrl->value;
827 ret = mt9m111_set_flip(client, ctrl->value,
828 MT9M111_RMB_MIRROR_ROWS);
829 break;
830 case V4L2_CID_HFLIP:
831 mt9m111->hflip = ctrl->value;
832 ret = mt9m111_set_flip(client, ctrl->value,
833 MT9M111_RMB_MIRROR_COLS);
834 break;
835 case V4L2_CID_GAIN:
836 ret = mt9m111_set_global_gain(client, ctrl->value);
837 break;
838 case V4L2_CID_EXPOSURE_AUTO:
839 ret = mt9m111_set_autoexposure(client, ctrl->value);
840 break;
841 case V4L2_CID_AUTO_WHITE_BALANCE:
842 ret = mt9m111_set_autowhitebalance(client, ctrl->value);
843 break;
844 default:
845 ret = -EINVAL;
848 return ret;
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);
858 return 0;
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);
873 return 0;
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);
880 int ret = 0;
882 if (mt9m111->powered) {
883 ret = mt9m111_enable(client);
884 if (!ret)
885 ret = mt9m111_reset(client);
886 if (!ret)
887 ret = mt9m111_restore_state(client);
889 return ret;
892 static int mt9m111_init(struct i2c_client *client)
894 struct mt9m111 *mt9m111 = to_mt9m111(client);
895 int ret;
897 mt9m111->context = HIGHPOWER;
898 ret = mt9m111_enable(client);
899 if (!ret)
900 ret = mt9m111_reset(client);
901 if (!ret)
902 ret = mt9m111_set_context(client, mt9m111->context);
903 if (!ret)
904 ret = mt9m111_set_autoexposure(client, mt9m111->autoexposure);
905 if (ret)
906 dev_err(&client->dev, "mt9m11x init failed: %d\n", ret);
907 return 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);
918 s32 data;
919 int ret;
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)
927 return -ENODEV;
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);
936 if (ret)
937 goto ei2c;
939 data = reg_read(CHIP_VERSION);
941 switch (data) {
942 case 0x143a: /* MT9M111 */
943 mt9m111->model = V4L2_IDENT_MT9M111;
944 break;
945 case 0x148c: /* MT9M112 */
946 mt9m111->model = V4L2_IDENT_MT9M112;
947 break;
948 default:
949 ret = -ENODEV;
950 dev_err(&client->dev,
951 "No MT9M11x chip detected, register read %x\n", data);
952 goto ei2c;
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);
960 ei2c:
961 return ret;
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,
971 #endif
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;
995 int ret;
997 if (!icd) {
998 dev_err(&client->dev, "MT9M11x: missing soc-camera data!\n");
999 return -EINVAL;
1002 icl = to_soc_camera_link(icd);
1003 if (!icl) {
1004 dev_err(&client->dev, "MT9M11x driver needs platform data\n");
1005 return -EINVAL;
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");
1011 return -EIO;
1014 mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL);
1015 if (!mt9m111)
1016 return -ENOMEM;
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);
1030 if (ret) {
1031 icd->ops = NULL;
1032 i2c_set_clientdata(client, NULL);
1033 kfree(mt9m111);
1036 return ret;
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;
1044 icd->ops = NULL;
1045 i2c_set_clientdata(client, NULL);
1046 client->driver = NULL;
1047 kfree(mt9m111);
1049 return 0;
1052 static const struct i2c_device_id mt9m111_id[] = {
1053 { "mt9m111", 0 },
1056 MODULE_DEVICE_TABLE(i2c, mt9m111_id);
1058 static struct i2c_driver mt9m111_i2c_driver = {
1059 .driver = {
1060 .name = "mt9m111",
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");