powerpc: use consistent types in mktree
[zen-stable.git] / drivers / media / video / mt9m111.c
blobfc5e2de037663abcc3fd84e9c5d32bca71183ef9
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 i2c_client *client;
152 struct soc_camera_device icd;
153 int model; /* V4L2_IDENT_MT9M11x* codes from v4l2-chip-ident.h */
154 enum mt9m111_context context;
155 struct v4l2_rect rect;
156 u32 pixfmt;
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 int reg_page_map_set(struct i2c_client *client, const u16 reg)
171 int ret;
172 u16 page;
173 static int lastpage = -1; /* PageMap cache value */
175 page = (reg >> 8);
176 if (page == lastpage)
177 return 0;
178 if (page > 2)
179 return -EINVAL;
181 ret = i2c_smbus_write_word_data(client, MT9M111_PAGE_MAP, swab16(page));
182 if (!ret)
183 lastpage = page;
184 return ret;
187 static int mt9m111_reg_read(struct i2c_client *client, const u16 reg)
189 int ret;
191 ret = reg_page_map_set(client, reg);
192 if (!ret)
193 ret = swab16(i2c_smbus_read_word_data(client, (reg & 0xff)));
195 dev_dbg(&client->dev, "read reg.%03x -> %04x\n", reg, ret);
196 return ret;
199 static int mt9m111_reg_write(struct i2c_client *client, const u16 reg,
200 const u16 data)
202 int ret;
204 ret = reg_page_map_set(client, reg);
205 if (!ret)
206 ret = i2c_smbus_write_word_data(client, (reg & 0xff),
207 swab16(data));
208 dev_dbg(&client->dev, "write reg.%03x = %04x -> %d\n", reg, data, ret);
209 return ret;
212 static int mt9m111_reg_set(struct i2c_client *client, const u16 reg,
213 const u16 data)
215 int ret;
217 ret = mt9m111_reg_read(client, reg);
218 if (ret >= 0)
219 ret = mt9m111_reg_write(client, reg, ret | data);
220 return ret;
223 static int mt9m111_reg_clear(struct i2c_client *client, const u16 reg,
224 const u16 data)
226 int ret;
228 ret = mt9m111_reg_read(client, reg);
229 return mt9m111_reg_write(client, reg, ret & ~data);
232 static int mt9m111_set_context(struct soc_camera_device *icd,
233 enum mt9m111_context ctxt)
235 struct i2c_client *client = to_i2c_client(icd->control);
236 int valB = MT9M111_CTXT_CTRL_RESTART | MT9M111_CTXT_CTRL_DEFECTCOR_B
237 | MT9M111_CTXT_CTRL_RESIZE_B | MT9M111_CTXT_CTRL_CTRL2_B
238 | MT9M111_CTXT_CTRL_GAMMA_B | MT9M111_CTXT_CTRL_READ_MODE_B
239 | MT9M111_CTXT_CTRL_VBLANK_SEL_B
240 | MT9M111_CTXT_CTRL_HBLANK_SEL_B;
241 int valA = MT9M111_CTXT_CTRL_RESTART;
243 if (ctxt == HIGHPOWER)
244 return reg_write(CONTEXT_CONTROL, valB);
245 else
246 return reg_write(CONTEXT_CONTROL, valA);
249 static int mt9m111_setup_rect(struct soc_camera_device *icd,
250 struct v4l2_rect *rect)
252 struct i2c_client *client = to_i2c_client(icd->control);
253 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
254 int ret, is_raw_format;
255 int width = rect->width;
256 int height = rect->height;
258 if ((mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR8)
259 || (mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR16))
260 is_raw_format = 1;
261 else
262 is_raw_format = 0;
264 ret = reg_write(COLUMN_START, rect->left);
265 if (!ret)
266 ret = reg_write(ROW_START, rect->top);
268 if (is_raw_format) {
269 if (!ret)
270 ret = reg_write(WINDOW_WIDTH, width);
271 if (!ret)
272 ret = reg_write(WINDOW_HEIGHT, height);
273 } else {
274 if (!ret)
275 ret = reg_write(REDUCER_XZOOM_B, MT9M111_MAX_WIDTH);
276 if (!ret)
277 ret = reg_write(REDUCER_YZOOM_B, MT9M111_MAX_HEIGHT);
278 if (!ret)
279 ret = reg_write(REDUCER_XSIZE_B, width);
280 if (!ret)
281 ret = reg_write(REDUCER_YSIZE_B, height);
282 if (!ret)
283 ret = reg_write(REDUCER_XZOOM_A, MT9M111_MAX_WIDTH);
284 if (!ret)
285 ret = reg_write(REDUCER_YZOOM_A, MT9M111_MAX_HEIGHT);
286 if (!ret)
287 ret = reg_write(REDUCER_XSIZE_A, width);
288 if (!ret)
289 ret = reg_write(REDUCER_YSIZE_A, height);
292 return ret;
295 static int mt9m111_setup_pixfmt(struct soc_camera_device *icd, u16 outfmt)
297 struct i2c_client *client = to_i2c_client(icd->control);
298 int ret;
300 ret = reg_write(OUTPUT_FORMAT_CTRL2_A, outfmt);
301 if (!ret)
302 ret = reg_write(OUTPUT_FORMAT_CTRL2_B, outfmt);
303 return ret;
306 static int mt9m111_setfmt_bayer8(struct soc_camera_device *icd)
308 return mt9m111_setup_pixfmt(icd, MT9M111_OUTFMT_PROCESSED_BAYER);
311 static int mt9m111_setfmt_bayer10(struct soc_camera_device *icd)
313 return mt9m111_setup_pixfmt(icd, MT9M111_OUTFMT_BYPASS_IFP);
316 static int mt9m111_setfmt_rgb565(struct soc_camera_device *icd)
318 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
319 int val = 0;
321 if (mt9m111->swap_rgb_red_blue)
322 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
323 if (mt9m111->swap_rgb_even_odd)
324 val |= MT9M111_OUTFMT_SWAP_RGB_EVEN;
325 val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565;
327 return mt9m111_setup_pixfmt(icd, val);
330 static int mt9m111_setfmt_rgb555(struct soc_camera_device *icd)
332 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
333 int val = 0;
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_RGB555;
341 return mt9m111_setup_pixfmt(icd, val);
344 static int mt9m111_setfmt_yuv(struct soc_camera_device *icd)
346 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
347 int val = 0;
349 if (mt9m111->swap_yuv_cb_cr)
350 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
351 if (mt9m111->swap_yuv_y_chromas)
352 val |= MT9M111_OUTFMT_SWAP_YCbCr_C_Y;
354 return mt9m111_setup_pixfmt(icd, val);
357 static int mt9m111_enable(struct soc_camera_device *icd)
359 struct i2c_client *client = to_i2c_client(icd->control);
360 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
361 struct soc_camera_link *icl = client->dev.platform_data;
362 int ret;
364 if (icl->power) {
365 ret = icl->power(&client->dev, 1);
366 if (ret < 0) {
367 dev_err(icd->vdev->parent,
368 "Platform failed to power-on the camera.\n");
369 return ret;
373 ret = reg_set(RESET, MT9M111_RESET_CHIP_ENABLE);
374 if (!ret)
375 mt9m111->powered = 1;
376 return ret;
379 static int mt9m111_disable(struct soc_camera_device *icd)
381 struct i2c_client *client = to_i2c_client(icd->control);
382 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
383 struct soc_camera_link *icl = client->dev.platform_data;
384 int ret;
386 ret = reg_clear(RESET, MT9M111_RESET_CHIP_ENABLE);
387 if (!ret)
388 mt9m111->powered = 0;
390 if (icl->power)
391 icl->power(&client->dev, 0);
393 return ret;
396 static int mt9m111_reset(struct soc_camera_device *icd)
398 struct i2c_client *client = to_i2c_client(icd->control);
399 struct soc_camera_link *icl = client->dev.platform_data;
400 int ret;
402 ret = reg_set(RESET, MT9M111_RESET_RESET_MODE);
403 if (!ret)
404 ret = reg_set(RESET, MT9M111_RESET_RESET_SOC);
405 if (!ret)
406 ret = reg_clear(RESET, MT9M111_RESET_RESET_MODE
407 | MT9M111_RESET_RESET_SOC);
409 if (icl->reset)
410 icl->reset(&client->dev);
412 return ret;
415 static int mt9m111_start_capture(struct soc_camera_device *icd)
417 return 0;
420 static int mt9m111_stop_capture(struct soc_camera_device *icd)
422 return 0;
425 static unsigned long mt9m111_query_bus_param(struct soc_camera_device *icd)
427 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
428 struct soc_camera_link *icl = mt9m111->client->dev.platform_data;
429 unsigned long flags = SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |
430 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
431 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8;
433 return soc_camera_apply_sensor_flags(icl, flags);
436 static int mt9m111_set_bus_param(struct soc_camera_device *icd, unsigned long f)
438 return 0;
441 static int mt9m111_set_crop(struct soc_camera_device *icd,
442 struct v4l2_rect *rect)
444 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
445 int ret;
447 dev_dbg(&icd->dev, "%s left=%d, top=%d, width=%d, height=%d\n",
448 __func__, rect->left, rect->top, rect->width,
449 rect->height);
451 ret = mt9m111_setup_rect(icd, rect);
452 if (!ret)
453 mt9m111->rect = *rect;
454 return ret;
457 static int mt9m111_set_pixfmt(struct soc_camera_device *icd, u32 pixfmt)
459 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
460 int ret;
462 switch (pixfmt) {
463 case V4L2_PIX_FMT_SBGGR8:
464 ret = mt9m111_setfmt_bayer8(icd);
465 break;
466 case V4L2_PIX_FMT_SBGGR16:
467 ret = mt9m111_setfmt_bayer10(icd);
468 break;
469 case V4L2_PIX_FMT_RGB555:
470 ret = mt9m111_setfmt_rgb555(icd);
471 break;
472 case V4L2_PIX_FMT_RGB565:
473 ret = mt9m111_setfmt_rgb565(icd);
474 break;
475 case V4L2_PIX_FMT_UYVY:
476 mt9m111->swap_yuv_y_chromas = 0;
477 mt9m111->swap_yuv_cb_cr = 0;
478 ret = mt9m111_setfmt_yuv(icd);
479 break;
480 case V4L2_PIX_FMT_VYUY:
481 mt9m111->swap_yuv_y_chromas = 0;
482 mt9m111->swap_yuv_cb_cr = 1;
483 ret = mt9m111_setfmt_yuv(icd);
484 break;
485 case V4L2_PIX_FMT_YUYV:
486 mt9m111->swap_yuv_y_chromas = 1;
487 mt9m111->swap_yuv_cb_cr = 0;
488 ret = mt9m111_setfmt_yuv(icd);
489 break;
490 case V4L2_PIX_FMT_YVYU:
491 mt9m111->swap_yuv_y_chromas = 1;
492 mt9m111->swap_yuv_cb_cr = 1;
493 ret = mt9m111_setfmt_yuv(icd);
494 break;
495 default:
496 dev_err(&icd->dev, "Pixel format not handled : %x\n", pixfmt);
497 ret = -EINVAL;
500 if (!ret)
501 mt9m111->pixfmt = pixfmt;
503 return ret;
506 static int mt9m111_set_fmt(struct soc_camera_device *icd,
507 struct v4l2_format *f)
509 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
510 struct v4l2_pix_format *pix = &f->fmt.pix;
511 struct v4l2_rect rect = {
512 .left = mt9m111->rect.left,
513 .top = mt9m111->rect.top,
514 .width = pix->width,
515 .height = pix->height,
517 int ret;
519 dev_dbg(&icd->dev, "%s fmt=%x left=%d, top=%d, width=%d, height=%d\n",
520 __func__, pix->pixelformat, rect.left, rect.top, rect.width,
521 rect.height);
523 ret = mt9m111_setup_rect(icd, &rect);
524 if (!ret)
525 ret = mt9m111_set_pixfmt(icd, pix->pixelformat);
526 if (!ret)
527 mt9m111->rect = rect;
528 return ret;
531 static int mt9m111_try_fmt(struct soc_camera_device *icd,
532 struct v4l2_format *f)
534 struct v4l2_pix_format *pix = &f->fmt.pix;
536 if (pix->height > MT9M111_MAX_HEIGHT)
537 pix->height = MT9M111_MAX_HEIGHT;
538 if (pix->width > MT9M111_MAX_WIDTH)
539 pix->width = MT9M111_MAX_WIDTH;
541 return 0;
544 static int mt9m111_get_chip_id(struct soc_camera_device *icd,
545 struct v4l2_dbg_chip_ident *id)
547 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
549 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
550 return -EINVAL;
552 if (id->match.addr != mt9m111->client->addr)
553 return -ENODEV;
555 id->ident = mt9m111->model;
556 id->revision = 0;
558 return 0;
561 #ifdef CONFIG_VIDEO_ADV_DEBUG
562 static int mt9m111_get_register(struct soc_camera_device *icd,
563 struct v4l2_dbg_register *reg)
565 int val;
566 struct i2c_client *client = to_i2c_client(icd->control);
568 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
569 return -EINVAL;
570 if (reg->match.addr != client->addr)
571 return -ENODEV;
573 val = mt9m111_reg_read(client, reg->reg);
574 reg->size = 2;
575 reg->val = (u64)val;
577 if (reg->val > 0xffff)
578 return -EIO;
580 return 0;
583 static int mt9m111_set_register(struct soc_camera_device *icd,
584 struct v4l2_dbg_register *reg)
586 struct i2c_client *client = to_i2c_client(icd->control);
588 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
589 return -EINVAL;
591 if (reg->match.addr != client->addr)
592 return -ENODEV;
594 if (mt9m111_reg_write(client, reg->reg, reg->val) < 0)
595 return -EIO;
597 return 0;
599 #endif
601 static const struct v4l2_queryctrl mt9m111_controls[] = {
603 .id = V4L2_CID_VFLIP,
604 .type = V4L2_CTRL_TYPE_BOOLEAN,
605 .name = "Flip Verticaly",
606 .minimum = 0,
607 .maximum = 1,
608 .step = 1,
609 .default_value = 0,
610 }, {
611 .id = V4L2_CID_HFLIP,
612 .type = V4L2_CTRL_TYPE_BOOLEAN,
613 .name = "Flip Horizontaly",
614 .minimum = 0,
615 .maximum = 1,
616 .step = 1,
617 .default_value = 0,
618 }, { /* gain = 1/32*val (=>gain=1 if val==32) */
619 .id = V4L2_CID_GAIN,
620 .type = V4L2_CTRL_TYPE_INTEGER,
621 .name = "Gain",
622 .minimum = 0,
623 .maximum = 63 * 2 * 2,
624 .step = 1,
625 .default_value = 32,
626 .flags = V4L2_CTRL_FLAG_SLIDER,
627 }, {
628 .id = V4L2_CID_EXPOSURE_AUTO,
629 .type = V4L2_CTRL_TYPE_BOOLEAN,
630 .name = "Auto Exposure",
631 .minimum = 0,
632 .maximum = 1,
633 .step = 1,
634 .default_value = 1,
638 static int mt9m111_video_probe(struct soc_camera_device *);
639 static void mt9m111_video_remove(struct soc_camera_device *);
640 static int mt9m111_get_control(struct soc_camera_device *,
641 struct v4l2_control *);
642 static int mt9m111_set_control(struct soc_camera_device *,
643 struct v4l2_control *);
644 static int mt9m111_resume(struct soc_camera_device *icd);
645 static int mt9m111_init(struct soc_camera_device *icd);
646 static int mt9m111_release(struct soc_camera_device *icd);
648 static struct soc_camera_ops mt9m111_ops = {
649 .owner = THIS_MODULE,
650 .probe = mt9m111_video_probe,
651 .remove = mt9m111_video_remove,
652 .init = mt9m111_init,
653 .resume = mt9m111_resume,
654 .release = mt9m111_release,
655 .start_capture = mt9m111_start_capture,
656 .stop_capture = mt9m111_stop_capture,
657 .set_crop = mt9m111_set_crop,
658 .set_fmt = mt9m111_set_fmt,
659 .try_fmt = mt9m111_try_fmt,
660 .query_bus_param = mt9m111_query_bus_param,
661 .set_bus_param = mt9m111_set_bus_param,
662 .controls = mt9m111_controls,
663 .num_controls = ARRAY_SIZE(mt9m111_controls),
664 .get_control = mt9m111_get_control,
665 .set_control = mt9m111_set_control,
666 .get_chip_id = mt9m111_get_chip_id,
667 #ifdef CONFIG_VIDEO_ADV_DEBUG
668 .get_register = mt9m111_get_register,
669 .set_register = mt9m111_set_register,
670 #endif
673 static int mt9m111_set_flip(struct soc_camera_device *icd, int flip, int mask)
675 struct i2c_client *client = to_i2c_client(icd->control);
676 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
677 int ret;
679 if (mt9m111->context == HIGHPOWER) {
680 if (flip)
681 ret = reg_set(READ_MODE_B, mask);
682 else
683 ret = reg_clear(READ_MODE_B, mask);
684 } else {
685 if (flip)
686 ret = reg_set(READ_MODE_A, mask);
687 else
688 ret = reg_clear(READ_MODE_A, mask);
691 return ret;
694 static int mt9m111_get_global_gain(struct soc_camera_device *icd)
696 struct i2c_client *client = to_i2c_client(icd->control);
697 int data;
699 data = reg_read(GLOBAL_GAIN);
700 if (data >= 0)
701 return (data & 0x2f) * (1 << ((data >> 10) & 1)) *
702 (1 << ((data >> 9) & 1));
703 return data;
706 static int mt9m111_set_global_gain(struct soc_camera_device *icd, int gain)
708 struct i2c_client *client = to_i2c_client(icd->control);
709 u16 val;
711 if (gain > 63 * 2 * 2)
712 return -EINVAL;
714 icd->gain = gain;
715 if ((gain >= 64 * 2) && (gain < 63 * 2 * 2))
716 val = (1 << 10) | (1 << 9) | (gain / 4);
717 else if ((gain >= 64) && (gain < 64 * 2))
718 val = (1 << 9) | (gain / 2);
719 else
720 val = gain;
722 return reg_write(GLOBAL_GAIN, val);
725 static int mt9m111_set_autoexposure(struct soc_camera_device *icd, int on)
727 struct i2c_client *client = to_i2c_client(icd->control);
728 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
729 int ret;
731 if (on)
732 ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
733 else
734 ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
736 if (!ret)
737 mt9m111->autoexposure = on;
739 return ret;
742 static int mt9m111_set_autowhitebalance(struct soc_camera_device *icd, int on)
744 struct i2c_client *client = to_i2c_client(icd->control);
745 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
746 int ret;
748 if (on)
749 ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
750 else
751 ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
753 if (!ret)
754 mt9m111->autowhitebalance = on;
756 return ret;
759 static int mt9m111_get_control(struct soc_camera_device *icd,
760 struct v4l2_control *ctrl)
762 struct i2c_client *client = to_i2c_client(icd->control);
763 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
764 int data;
766 switch (ctrl->id) {
767 case V4L2_CID_VFLIP:
768 if (mt9m111->context == HIGHPOWER)
769 data = reg_read(READ_MODE_B);
770 else
771 data = reg_read(READ_MODE_A);
773 if (data < 0)
774 return -EIO;
775 ctrl->value = !!(data & MT9M111_RMB_MIRROR_ROWS);
776 break;
777 case V4L2_CID_HFLIP:
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_COLS);
786 break;
787 case V4L2_CID_GAIN:
788 data = mt9m111_get_global_gain(icd);
789 if (data < 0)
790 return data;
791 ctrl->value = data;
792 break;
793 case V4L2_CID_EXPOSURE_AUTO:
794 ctrl->value = mt9m111->autoexposure;
795 break;
796 case V4L2_CID_AUTO_WHITE_BALANCE:
797 ctrl->value = mt9m111->autowhitebalance;
798 break;
800 return 0;
803 static int mt9m111_set_control(struct soc_camera_device *icd,
804 struct v4l2_control *ctrl)
806 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
807 const struct v4l2_queryctrl *qctrl;
808 int ret;
810 qctrl = soc_camera_find_qctrl(&mt9m111_ops, ctrl->id);
812 if (!qctrl)
813 return -EINVAL;
815 switch (ctrl->id) {
816 case V4L2_CID_VFLIP:
817 mt9m111->vflip = ctrl->value;
818 ret = mt9m111_set_flip(icd, ctrl->value,
819 MT9M111_RMB_MIRROR_ROWS);
820 break;
821 case V4L2_CID_HFLIP:
822 mt9m111->hflip = ctrl->value;
823 ret = mt9m111_set_flip(icd, ctrl->value,
824 MT9M111_RMB_MIRROR_COLS);
825 break;
826 case V4L2_CID_GAIN:
827 ret = mt9m111_set_global_gain(icd, ctrl->value);
828 break;
829 case V4L2_CID_EXPOSURE_AUTO:
830 ret = mt9m111_set_autoexposure(icd, ctrl->value);
831 break;
832 case V4L2_CID_AUTO_WHITE_BALANCE:
833 ret = mt9m111_set_autowhitebalance(icd, ctrl->value);
834 break;
835 default:
836 ret = -EINVAL;
839 return ret;
842 static int mt9m111_restore_state(struct soc_camera_device *icd)
844 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
846 mt9m111_set_context(icd, mt9m111->context);
847 mt9m111_set_pixfmt(icd, mt9m111->pixfmt);
848 mt9m111_setup_rect(icd, &mt9m111->rect);
849 mt9m111_set_flip(icd, mt9m111->hflip, MT9M111_RMB_MIRROR_COLS);
850 mt9m111_set_flip(icd, mt9m111->vflip, MT9M111_RMB_MIRROR_ROWS);
851 mt9m111_set_global_gain(icd, icd->gain);
852 mt9m111_set_autoexposure(icd, mt9m111->autoexposure);
853 mt9m111_set_autowhitebalance(icd, mt9m111->autowhitebalance);
854 return 0;
857 static int mt9m111_resume(struct soc_camera_device *icd)
859 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
860 int ret = 0;
862 if (mt9m111->powered) {
863 ret = mt9m111_enable(icd);
864 if (!ret)
865 ret = mt9m111_reset(icd);
866 if (!ret)
867 ret = mt9m111_restore_state(icd);
869 return ret;
872 static int mt9m111_init(struct soc_camera_device *icd)
874 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
875 int ret;
877 mt9m111->context = HIGHPOWER;
878 ret = mt9m111_enable(icd);
879 if (!ret)
880 ret = mt9m111_reset(icd);
881 if (!ret)
882 ret = mt9m111_set_context(icd, mt9m111->context);
883 if (!ret)
884 ret = mt9m111_set_autoexposure(icd, mt9m111->autoexposure);
885 if (ret)
886 dev_err(&icd->dev, "mt9m11x init failed: %d\n", ret);
887 return ret;
890 static int mt9m111_release(struct soc_camera_device *icd)
892 int ret;
894 ret = mt9m111_disable(icd);
895 if (ret < 0)
896 dev_err(&icd->dev, "mt9m11x release failed: %d\n", ret);
898 return ret;
902 * Interface active, can use i2c. If it fails, it can indeed mean, that
903 * this wasn't our capture interface, so, we wait for the right one
905 static int mt9m111_video_probe(struct soc_camera_device *icd)
907 struct i2c_client *client = to_i2c_client(icd->control);
908 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
909 s32 data;
910 int ret;
913 * We must have a parent by now. And it cannot be a wrong one.
914 * So this entire test is completely redundant.
916 if (!icd->dev.parent ||
917 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
918 return -ENODEV;
920 ret = mt9m111_enable(icd);
921 if (ret)
922 goto ei2c;
923 ret = mt9m111_reset(icd);
924 if (ret)
925 goto ei2c;
927 data = reg_read(CHIP_VERSION);
929 switch (data) {
930 case 0x143a: /* MT9M111 */
931 mt9m111->model = V4L2_IDENT_MT9M111;
932 break;
933 case 0x148c: /* MT9M112 */
934 mt9m111->model = V4L2_IDENT_MT9M112;
935 break;
936 default:
937 ret = -ENODEV;
938 dev_err(&icd->dev,
939 "No MT9M11x chip detected, register read %x\n", data);
940 goto ei2c;
943 icd->formats = mt9m111_colour_formats;
944 icd->num_formats = ARRAY_SIZE(mt9m111_colour_formats);
946 dev_info(&icd->dev, "Detected a MT9M11x chip ID %x\n", data);
948 ret = soc_camera_video_start(icd);
949 if (ret)
950 goto eisis;
952 mt9m111->autoexposure = 1;
953 mt9m111->autowhitebalance = 1;
955 mt9m111->swap_rgb_even_odd = 1;
956 mt9m111->swap_rgb_red_blue = 1;
958 return 0;
959 eisis:
960 ei2c:
961 return ret;
964 static void mt9m111_video_remove(struct soc_camera_device *icd)
966 struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);
968 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m111->client->addr,
969 mt9m111->icd.dev.parent, mt9m111->icd.vdev);
970 soc_camera_video_stop(&mt9m111->icd);
973 static int mt9m111_probe(struct i2c_client *client,
974 const struct i2c_device_id *did)
976 struct mt9m111 *mt9m111;
977 struct soc_camera_device *icd;
978 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
979 struct soc_camera_link *icl = client->dev.platform_data;
980 int ret;
982 if (!icl) {
983 dev_err(&client->dev, "MT9M11x driver needs platform data\n");
984 return -EINVAL;
987 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
988 dev_warn(&adapter->dev,
989 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
990 return -EIO;
993 mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL);
994 if (!mt9m111)
995 return -ENOMEM;
997 mt9m111->client = client;
998 i2c_set_clientdata(client, mt9m111);
1000 /* Second stage probe - when a capture adapter is there */
1001 icd = &mt9m111->icd;
1002 icd->ops = &mt9m111_ops;
1003 icd->control = &client->dev;
1004 icd->x_min = MT9M111_MIN_DARK_COLS;
1005 icd->y_min = MT9M111_MIN_DARK_ROWS;
1006 icd->x_current = icd->x_min;
1007 icd->y_current = icd->y_min;
1008 icd->width_min = MT9M111_MIN_DARK_ROWS;
1009 icd->width_max = MT9M111_MAX_WIDTH;
1010 icd->height_min = MT9M111_MIN_DARK_COLS;
1011 icd->height_max = MT9M111_MAX_HEIGHT;
1012 icd->y_skip_top = 0;
1013 icd->iface = icl->bus_id;
1015 ret = soc_camera_device_register(icd);
1016 if (ret)
1017 goto eisdr;
1018 return 0;
1020 eisdr:
1021 kfree(mt9m111);
1022 return ret;
1025 static int mt9m111_remove(struct i2c_client *client)
1027 struct mt9m111 *mt9m111 = i2c_get_clientdata(client);
1028 soc_camera_device_unregister(&mt9m111->icd);
1029 kfree(mt9m111);
1031 return 0;
1034 static const struct i2c_device_id mt9m111_id[] = {
1035 { "mt9m111", 0 },
1038 MODULE_DEVICE_TABLE(i2c, mt9m111_id);
1040 static struct i2c_driver mt9m111_i2c_driver = {
1041 .driver = {
1042 .name = "mt9m111",
1044 .probe = mt9m111_probe,
1045 .remove = mt9m111_remove,
1046 .id_table = mt9m111_id,
1049 static int __init mt9m111_mod_init(void)
1051 return i2c_add_driver(&mt9m111_i2c_driver);
1054 static void __exit mt9m111_mod_exit(void)
1056 i2c_del_driver(&mt9m111_i2c_driver);
1059 module_init(mt9m111_mod_init);
1060 module_exit(mt9m111_mod_exit);
1062 MODULE_DESCRIPTION("Micron MT9M111/MT9M112 Camera driver");
1063 MODULE_AUTHOR("Robert Jarzmik");
1064 MODULE_LICENSE("GPL");