]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/mt9v022.c
V4L/DVB (13659): soc-camera: convert to the new mediabus API
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / mt9v022.c
CommitLineData
7397bfbe
GL
1/*
2 * Driver for MT9V022 CMOS Image Sensor from Micron
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5 *
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
11#include <linux/videodev2.h>
12#include <linux/slab.h>
13#include <linux/i2c.h>
14#include <linux/delay.h>
15#include <linux/log2.h>
16
979ea1dd 17#include <media/v4l2-subdev.h>
7397bfbe
GL
18#include <media/v4l2-chip-ident.h>
19#include <media/soc_camera.h>
20
5d28d525
GL
21/*
22 * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
979ea1dd 23 * The platform has to define ctruct i2c_board_info objects and link to them
5d28d525
GL
24 * from struct soc_camera_link
25 */
7397bfbe
GL
26
27static char *sensor_type;
28module_param(sensor_type, charp, S_IRUGO);
61a2d07d 29MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
7397bfbe
GL
30
31/* mt9v022 selected register addresses */
32#define MT9V022_CHIP_VERSION 0x00
33#define MT9V022_COLUMN_START 0x01
34#define MT9V022_ROW_START 0x02
35#define MT9V022_WINDOW_HEIGHT 0x03
36#define MT9V022_WINDOW_WIDTH 0x04
37#define MT9V022_HORIZONTAL_BLANKING 0x05
38#define MT9V022_VERTICAL_BLANKING 0x06
39#define MT9V022_CHIP_CONTROL 0x07
40#define MT9V022_SHUTTER_WIDTH1 0x08
41#define MT9V022_SHUTTER_WIDTH2 0x09
42#define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
43#define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
44#define MT9V022_RESET 0x0c
45#define MT9V022_READ_MODE 0x0d
46#define MT9V022_MONITOR_MODE 0x0e
47#define MT9V022_PIXEL_OPERATION_MODE 0x0f
48#define MT9V022_LED_OUT_CONTROL 0x1b
49#define MT9V022_ADC_MODE_CONTROL 0x1c
96c75399 50#define MT9V022_ANALOG_GAIN 0x35
7397bfbe
GL
51#define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
52#define MT9V022_PIXCLK_FV_LV 0x74
53#define MT9V022_DIGITAL_TEST_PATTERN 0x7f
54#define MT9V022_AEC_AGC_ENABLE 0xAF
55#define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
56
57/* Progressive scan, master, defaults */
58#define MT9V022_CHIP_CONTROL_DEFAULT 0x188
59
6a6c8786
GL
60#define MT9V022_MAX_WIDTH 752
61#define MT9V022_MAX_HEIGHT 480
62#define MT9V022_MIN_WIDTH 48
63#define MT9V022_MIN_HEIGHT 32
64#define MT9V022_COLUMN_SKIP 1
65#define MT9V022_ROW_SKIP 4
66
760697be
GL
67/* MT9V022 has only one fixed colorspace per pixelcode */
68struct mt9v022_datafmt {
69 enum v4l2_mbus_pixelcode code;
70 enum v4l2_colorspace colorspace;
71};
72
73/* Find a data format by a pixel code in an array */
74static const struct mt9v022_datafmt *mt9v022_find_datafmt(
75 enum v4l2_mbus_pixelcode code, const struct mt9v022_datafmt *fmt,
76 int n)
77{
78 int i;
79 for (i = 0; i < n; i++)
80 if (fmt[i].code == code)
81 return fmt + i;
82
83 return NULL;
84}
85
86static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
5d28d525
GL
87 /*
88 * Order important: first natively supported,
89 * second supported with a GPIO extender
90 */
760697be
GL
91 {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
92 {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
bb55de3b
GL
93};
94
760697be 95static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
bb55de3b 96 /* Order important - see above */
760697be
GL
97 {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
98 {V4L2_MBUS_FMT_GREY8_1X8, V4L2_COLORSPACE_JPEG},
7397bfbe
GL
99};
100
101struct mt9v022 {
979ea1dd 102 struct v4l2_subdev subdev;
6a6c8786 103 struct v4l2_rect rect; /* Sensor window */
760697be
GL
104 const struct mt9v022_datafmt *fmt;
105 const struct mt9v022_datafmt *fmts;
106 int num_fmts;
7fb0fd05 107 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
7397bfbe 108 u16 chip_control;
32536108 109 unsigned short y_skip_top; /* Lines to skip at the top */
7397bfbe
GL
110};
111
979ea1dd
GL
112static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
113{
114 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
115}
116
9538e1c2 117static int reg_read(struct i2c_client *client, const u8 reg)
7397bfbe 118{
7397bfbe
GL
119 s32 data = i2c_smbus_read_word_data(client, reg);
120 return data < 0 ? data : swab16(data);
121}
122
9538e1c2 123static int reg_write(struct i2c_client *client, const u8 reg,
7397bfbe
GL
124 const u16 data)
125{
9538e1c2 126 return i2c_smbus_write_word_data(client, reg, swab16(data));
7397bfbe
GL
127}
128
9538e1c2 129static int reg_set(struct i2c_client *client, const u8 reg,
7397bfbe
GL
130 const u16 data)
131{
132 int ret;
133
9538e1c2 134 ret = reg_read(client, reg);
7397bfbe
GL
135 if (ret < 0)
136 return ret;
9538e1c2 137 return reg_write(client, reg, ret | data);
7397bfbe
GL
138}
139
9538e1c2 140static int reg_clear(struct i2c_client *client, const u8 reg,
7397bfbe
GL
141 const u16 data)
142{
143 int ret;
144
9538e1c2 145 ret = reg_read(client, reg);
7397bfbe
GL
146 if (ret < 0)
147 return ret;
9538e1c2 148 return reg_write(client, reg, ret & ~data);
7397bfbe
GL
149}
150
a4c56fd8 151static int mt9v022_init(struct i2c_client *client)
7397bfbe 152{
979ea1dd 153 struct mt9v022 *mt9v022 = to_mt9v022(client);
7397bfbe
GL
154 int ret;
155
5d28d525
GL
156 /*
157 * Almost the default mode: master, parallel, simultaneous, and an
7397bfbe 158 * undocumented bit 0x200, which is present in table 7, but not in 8,
5d28d525
GL
159 * plus snapshot mode to disable scan for now
160 */
7397bfbe 161 mt9v022->chip_control |= 0x10;
9538e1c2 162 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
11211641 163 if (!ret)
9538e1c2 164 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
7397bfbe
GL
165
166 /* All defaults */
11211641 167 if (!ret)
7397bfbe 168 /* AEC, AGC on */
9538e1c2 169 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
96c75399
GL
170 if (!ret)
171 ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
172 if (!ret)
173 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
11211641 174 if (!ret)
9538e1c2 175 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
11211641 176 if (!ret)
7397bfbe 177 /* default - auto */
9538e1c2 178 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
11211641 179 if (!ret)
9538e1c2 180 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
7397bfbe 181
11211641 182 return ret;
7397bfbe
GL
183}
184
979ea1dd 185static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
7397bfbe 186{
979ea1dd
GL
187 struct i2c_client *client = sd->priv;
188 struct mt9v022 *mt9v022 = to_mt9v022(client);
81034663 189
979ea1dd
GL
190 if (enable)
191 /* Switch to master "normal" mode */
192 mt9v022->chip_control &= ~0x10;
193 else
194 /* Switch to snapshot mode */
195 mt9v022->chip_control |= 0x10;
7397bfbe 196
979ea1dd 197 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
7397bfbe
GL
198 return -EIO;
199 return 0;
200}
201
ad5f2e85
GL
202static int mt9v022_set_bus_param(struct soc_camera_device *icd,
203 unsigned long flags)
7397bfbe 204{
40e2e092 205 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
979ea1dd 206 struct mt9v022 *mt9v022 = to_mt9v022(client);
40e2e092 207 struct soc_camera_link *icl = to_soc_camera_link(icd);
ad5f2e85 208 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
7397bfbe 209 int ret;
ad5f2e85 210 u16 pixclk = 0;
7397bfbe
GL
211
212 /* Only one width bit may be set */
213 if (!is_power_of_2(width_flag))
214 return -EINVAL;
215
e958e27a
SH
216 if (icl->set_bus_param) {
217 ret = icl->set_bus_param(icl, width_flag);
218 if (ret)
ad5f2e85 219 return ret;
e958e27a
SH
220 } else {
221 /*
222 * Without board specific bus width settings we only support the
223 * sensors native bus width
224 */
225 if (width_flag != SOCAM_DATAWIDTH_10)
226 return -EINVAL;
ad5f2e85
GL
227 }
228
bd73b36f
GL
229 flags = soc_camera_apply_sensor_flags(icl, flags);
230
ad5f2e85
GL
231 if (flags & SOCAM_PCLK_SAMPLE_RISING)
232 pixclk |= 0x10;
233
234 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
235 pixclk |= 0x1;
236
237 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
238 pixclk |= 0x2;
239
9538e1c2 240 ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
ad5f2e85
GL
241 if (ret < 0)
242 return ret;
243
244 if (!(flags & SOCAM_MASTER))
245 mt9v022->chip_control &= ~0x8;
246
9538e1c2 247 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
ad5f2e85
GL
248 if (ret < 0)
249 return ret;
250
85f8be68 251 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
ad5f2e85
GL
252 pixclk, mt9v022->chip_control);
253
254 return 0;
255}
256
257static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
258{
40e2e092 259 struct soc_camera_link *icl = to_soc_camera_link(icd);
e958e27a 260 unsigned int width_flag;
ad5f2e85 261
e958e27a
SH
262 if (icl->query_bus_param)
263 width_flag = icl->query_bus_param(icl) &
264 SOCAM_DATAWIDTH_MASK;
265 else
266 width_flag = SOCAM_DATAWIDTH_10;
ad5f2e85
GL
267
268 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
269 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
270 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
2d9329f3 271 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE |
ad5f2e85
GL
272 width_flag;
273}
274
08590b96 275static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
ad5f2e85 276{
08590b96 277 struct i2c_client *client = sd->priv;
6a6c8786
GL
278 struct mt9v022 *mt9v022 = to_mt9v022(client);
279 struct v4l2_rect rect = a->c;
ad5f2e85
GL
280 int ret;
281
6a6c8786 282 /* Bayer format - even size lengths */
760697be 283 if (mt9v022->fmts == mt9v022_colour_fmts) {
6a6c8786
GL
284 rect.width = ALIGN(rect.width, 2);
285 rect.height = ALIGN(rect.height, 2);
286 /* Let the user play with the starting pixel */
287 }
288
289 soc_camera_limit_side(&rect.left, &rect.width,
290 MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
291
292 soc_camera_limit_side(&rect.top, &rect.height,
293 MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
294
7397bfbe 295 /* Like in example app. Contradicts the datasheet though */
9538e1c2 296 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
7397bfbe
GL
297 if (ret >= 0) {
298 if (ret & 1) /* Autoexposure */
9538e1c2 299 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
32536108 300 rect.height + mt9v022->y_skip_top + 43);
7397bfbe 301 else
9538e1c2 302 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
32536108 303 rect.height + mt9v022->y_skip_top + 43);
7397bfbe
GL
304 }
305 /* Setup frame format: defaults apart from width and height */
11211641 306 if (!ret)
6a6c8786 307 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
11211641 308 if (!ret)
6a6c8786 309 ret = reg_write(client, MT9V022_ROW_START, rect.top);
11211641 310 if (!ret)
5d28d525
GL
311 /*
312 * Default 94, Phytec driver says:
313 * "width + horizontal blank >= 660"
314 */
9538e1c2 315 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
6a6c8786
GL
316 rect.width > 660 - 43 ? 43 :
317 660 - rect.width);
11211641 318 if (!ret)
9538e1c2 319 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
11211641 320 if (!ret)
6a6c8786 321 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
11211641 322 if (!ret)
9538e1c2 323 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
32536108 324 rect.height + mt9v022->y_skip_top);
7397bfbe
GL
325
326 if (ret < 0)
327 return ret;
328
6a6c8786
GL
329 dev_dbg(&client->dev, "Frame %ux%u pixel\n", rect.width, rect.height);
330
331 mt9v022->rect = rect;
332
333 return 0;
334}
335
336static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
337{
338 struct i2c_client *client = sd->priv;
339 struct mt9v022 *mt9v022 = to_mt9v022(client);
340
341 a->c = mt9v022->rect;
342 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
343
344 return 0;
345}
346
347static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
348{
349 a->bounds.left = MT9V022_COLUMN_SKIP;
350 a->bounds.top = MT9V022_ROW_SKIP;
351 a->bounds.width = MT9V022_MAX_WIDTH;
352 a->bounds.height = MT9V022_MAX_HEIGHT;
353 a->defrect = a->bounds;
354 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
355 a->pixelaspect.numerator = 1;
356 a->pixelaspect.denominator = 1;
357
358 return 0;
359}
360
760697be
GL
361static int mt9v022_g_fmt(struct v4l2_subdev *sd,
362 struct v4l2_mbus_framefmt *mf)
6a6c8786
GL
363{
364 struct i2c_client *client = sd->priv;
365 struct mt9v022 *mt9v022 = to_mt9v022(client);
6a6c8786 366
760697be
GL
367 mf->width = mt9v022->rect.width;
368 mf->height = mt9v022->rect.height;
369 mf->code = mt9v022->fmt->code;
370 mf->colorspace = mt9v022->fmt->colorspace;
371 mf->field = V4L2_FIELD_NONE;
7397bfbe 372
7397bfbe
GL
373 return 0;
374}
375
760697be
GL
376static int mt9v022_s_fmt(struct v4l2_subdev *sd,
377 struct v4l2_mbus_framefmt *mf)
09e231b3 378{
979ea1dd
GL
379 struct i2c_client *client = sd->priv;
380 struct mt9v022 *mt9v022 = to_mt9v022(client);
08590b96
GL
381 struct v4l2_crop a = {
382 .c = {
6a6c8786
GL
383 .left = mt9v022->rect.left,
384 .top = mt9v022->rect.top,
760697be
GL
385 .width = mf->width,
386 .height = mf->height,
08590b96 387 },
09e231b3 388 };
6a6c8786 389 int ret;
09e231b3 390
5d28d525
GL
391 /*
392 * The caller provides a supported format, as verified per call to
393 * icd->try_fmt(), datawidth is from our supported format list
394 */
760697be
GL
395 switch (mf->code) {
396 case V4L2_MBUS_FMT_GREY8_1X8:
397 case V4L2_MBUS_FMT_Y10_1X10:
09e231b3
GL
398 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
399 return -EINVAL;
400 break;
760697be
GL
401 case V4L2_MBUS_FMT_SBGGR8_1X8:
402 case V4L2_MBUS_FMT_SBGGR10_1X10:
09e231b3
GL
403 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
404 return -EINVAL;
405 break;
406 case 0:
407 /* No format change, only geometry */
408 break;
409 default:
410 return -EINVAL;
411 }
412
413 /* No support for scaling on this camera, just crop. */
6a6c8786
GL
414 ret = mt9v022_s_crop(sd, &a);
415 if (!ret) {
760697be
GL
416 mf->width = mt9v022->rect.width;
417 mf->height = mt9v022->rect.height;
418 mt9v022->fmt = mt9v022_find_datafmt(mf->code,
419 mt9v022->fmts, mt9v022->num_fmts);
420 mf->colorspace = mt9v022->fmt->colorspace;
6a6c8786
GL
421 }
422
423 return ret;
09e231b3
GL
424}
425
760697be
GL
426static int mt9v022_try_fmt(struct v4l2_subdev *sd,
427 struct v4l2_mbus_framefmt *mf)
7397bfbe 428{
979ea1dd 429 struct i2c_client *client = sd->priv;
32536108 430 struct mt9v022 *mt9v022 = to_mt9v022(client);
760697be
GL
431 const struct mt9v022_datafmt *fmt;
432 int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
433 mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
64f5905e 434
760697be 435 v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
6a6c8786 436 MT9V022_MAX_WIDTH, align,
760697be 437 &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
32536108 438 MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
7397bfbe 439
760697be
GL
440 fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
441 mt9v022->num_fmts);
442 if (!fmt) {
443 fmt = mt9v022->fmt;
444 mf->code = fmt->code;
445 }
446
447 mf->colorspace = fmt->colorspace;
448
7397bfbe
GL
449 return 0;
450}
451
979ea1dd
GL
452static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
453 struct v4l2_dbg_chip_ident *id)
7397bfbe 454{
979ea1dd
GL
455 struct i2c_client *client = sd->priv;
456 struct mt9v022 *mt9v022 = to_mt9v022(client);
7397bfbe 457
aecde8b5 458 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
7397bfbe
GL
459 return -EINVAL;
460
40e2e092 461 if (id->match.addr != client->addr)
7397bfbe
GL
462 return -ENODEV;
463
464 id->ident = mt9v022->model;
465 id->revision = 0;
466
467 return 0;
468}
469
470#ifdef CONFIG_VIDEO_ADV_DEBUG
979ea1dd
GL
471static int mt9v022_g_register(struct v4l2_subdev *sd,
472 struct v4l2_dbg_register *reg)
7397bfbe 473{
979ea1dd 474 struct i2c_client *client = sd->priv;
7397bfbe 475
aecde8b5 476 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
7397bfbe
GL
477 return -EINVAL;
478
9538e1c2 479 if (reg->match.addr != client->addr)
7397bfbe
GL
480 return -ENODEV;
481
aecde8b5 482 reg->size = 2;
9538e1c2 483 reg->val = reg_read(client, reg->reg);
7397bfbe
GL
484
485 if (reg->val > 0xffff)
486 return -EIO;
487
488 return 0;
489}
490
979ea1dd
GL
491static int mt9v022_s_register(struct v4l2_subdev *sd,
492 struct v4l2_dbg_register *reg)
7397bfbe 493{
979ea1dd 494 struct i2c_client *client = sd->priv;
7397bfbe 495
aecde8b5 496 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
7397bfbe
GL
497 return -EINVAL;
498
9538e1c2 499 if (reg->match.addr != client->addr)
7397bfbe
GL
500 return -ENODEV;
501
9538e1c2 502 if (reg_write(client, reg->reg, reg->val) < 0)
7397bfbe
GL
503 return -EIO;
504
505 return 0;
506}
507#endif
508
4407a463 509static const struct v4l2_queryctrl mt9v022_controls[] = {
7397bfbe
GL
510 {
511 .id = V4L2_CID_VFLIP,
512 .type = V4L2_CTRL_TYPE_BOOLEAN,
513 .name = "Flip Vertically",
514 .minimum = 0,
515 .maximum = 1,
516 .step = 1,
517 .default_value = 0,
518 }, {
519 .id = V4L2_CID_HFLIP,
520 .type = V4L2_CTRL_TYPE_BOOLEAN,
521 .name = "Flip Horizontally",
522 .minimum = 0,
523 .maximum = 1,
524 .step = 1,
525 .default_value = 0,
526 }, {
527 .id = V4L2_CID_GAIN,
528 .type = V4L2_CTRL_TYPE_INTEGER,
529 .name = "Analog Gain",
530 .minimum = 64,
531 .maximum = 127,
532 .step = 1,
533 .default_value = 64,
534 .flags = V4L2_CTRL_FLAG_SLIDER,
535 }, {
536 .id = V4L2_CID_EXPOSURE,
537 .type = V4L2_CTRL_TYPE_INTEGER,
538 .name = "Exposure",
539 .minimum = 1,
540 .maximum = 255,
541 .step = 1,
542 .default_value = 255,
543 .flags = V4L2_CTRL_FLAG_SLIDER,
544 }, {
545 .id = V4L2_CID_AUTOGAIN,
546 .type = V4L2_CTRL_TYPE_BOOLEAN,
547 .name = "Automatic Gain",
548 .minimum = 0,
549 .maximum = 1,
550 .step = 1,
551 .default_value = 1,
552 }, {
553 .id = V4L2_CID_EXPOSURE_AUTO,
554 .type = V4L2_CTRL_TYPE_BOOLEAN,
555 .name = "Automatic Exposure",
556 .minimum = 0,
557 .maximum = 1,
558 .step = 1,
559 .default_value = 1,
560 }
561};
562
7397bfbe 563static struct soc_camera_ops mt9v022_ops = {
ad5f2e85
GL
564 .set_bus_param = mt9v022_set_bus_param,
565 .query_bus_param = mt9v022_query_bus_param,
7397bfbe
GL
566 .controls = mt9v022_controls,
567 .num_controls = ARRAY_SIZE(mt9v022_controls),
7397bfbe
GL
568};
569
979ea1dd 570static int mt9v022_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
7397bfbe 571{
979ea1dd 572 struct i2c_client *client = sd->priv;
96c75399
GL
573 const struct v4l2_queryctrl *qctrl;
574 unsigned long range;
7397bfbe
GL
575 int data;
576
96c75399
GL
577 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
578
7397bfbe
GL
579 switch (ctrl->id) {
580 case V4L2_CID_VFLIP:
9538e1c2 581 data = reg_read(client, MT9V022_READ_MODE);
7397bfbe
GL
582 if (data < 0)
583 return -EIO;
584 ctrl->value = !!(data & 0x10);
585 break;
586 case V4L2_CID_HFLIP:
9538e1c2 587 data = reg_read(client, MT9V022_READ_MODE);
7397bfbe
GL
588 if (data < 0)
589 return -EIO;
590 ctrl->value = !!(data & 0x20);
591 break;
592 case V4L2_CID_EXPOSURE_AUTO:
9538e1c2 593 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
7397bfbe
GL
594 if (data < 0)
595 return -EIO;
596 ctrl->value = !!(data & 0x1);
597 break;
598 case V4L2_CID_AUTOGAIN:
9538e1c2 599 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
7397bfbe
GL
600 if (data < 0)
601 return -EIO;
602 ctrl->value = !!(data & 0x2);
96c75399
GL
603 break;
604 case V4L2_CID_GAIN:
605 data = reg_read(client, MT9V022_ANALOG_GAIN);
606 if (data < 0)
607 return -EIO;
608
609 range = qctrl->maximum - qctrl->minimum;
610 ctrl->value = ((data - 16) * range + 24) / 48 + qctrl->minimum;
611
612 break;
613 case V4L2_CID_EXPOSURE:
614 data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
615 if (data < 0)
616 return -EIO;
617
618 range = qctrl->maximum - qctrl->minimum;
619 ctrl->value = ((data - 1) * range + 239) / 479 + qctrl->minimum;
620
7397bfbe
GL
621 break;
622 }
623 return 0;
624}
625
979ea1dd 626static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
7397bfbe
GL
627{
628 int data;
979ea1dd 629 struct i2c_client *client = sd->priv;
7397bfbe
GL
630 const struct v4l2_queryctrl *qctrl;
631
632 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
7397bfbe
GL
633 if (!qctrl)
634 return -EINVAL;
635
636 switch (ctrl->id) {
637 case V4L2_CID_VFLIP:
638 if (ctrl->value)
9538e1c2 639 data = reg_set(client, MT9V022_READ_MODE, 0x10);
7397bfbe 640 else
9538e1c2 641 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
7397bfbe
GL
642 if (data < 0)
643 return -EIO;
644 break;
645 case V4L2_CID_HFLIP:
646 if (ctrl->value)
9538e1c2 647 data = reg_set(client, MT9V022_READ_MODE, 0x20);
7397bfbe 648 else
9538e1c2 649 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
7397bfbe
GL
650 if (data < 0)
651 return -EIO;
652 break;
653 case V4L2_CID_GAIN:
654 /* mt9v022 has minimum == default */
655 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
656 return -EINVAL;
657 else {
658 unsigned long range = qctrl->maximum - qctrl->minimum;
96c75399 659 /* Valid values 16 to 64, 32 to 64 must be even. */
7397bfbe 660 unsigned long gain = ((ctrl->value - qctrl->minimum) *
96c75399 661 48 + range / 2) / range + 16;
7397bfbe
GL
662 if (gain >= 32)
663 gain &= ~1;
5d28d525
GL
664 /*
665 * The user wants to set gain manually, hope, she
666 * knows, what she's doing... Switch AGC off.
667 */
7397bfbe 668
9538e1c2 669 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
7397bfbe
GL
670 return -EIO;
671
96c75399
GL
672 dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
673 reg_read(client, MT9V022_ANALOG_GAIN), gain);
9538e1c2 674 if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
7397bfbe 675 return -EIO;
7397bfbe
GL
676 }
677 break;
678 case V4L2_CID_EXPOSURE:
679 /* mt9v022 has maximum == default */
680 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
681 return -EINVAL;
682 else {
683 unsigned long range = qctrl->maximum - qctrl->minimum;
684 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
685 479 + range / 2) / range + 1;
5d28d525
GL
686 /*
687 * The user wants to set shutter width manually, hope,
688 * she knows, what she's doing... Switch AEC off.
689 */
7397bfbe 690
9538e1c2 691 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
7397bfbe
GL
692 return -EIO;
693
85f8be68 694 dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
9538e1c2 695 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
7397bfbe 696 shutter);
9538e1c2 697 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
7397bfbe
GL
698 shutter) < 0)
699 return -EIO;
7397bfbe
GL
700 }
701 break;
702 case V4L2_CID_AUTOGAIN:
703 if (ctrl->value)
9538e1c2 704 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2);
7397bfbe 705 else
9538e1c2 706 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2);
7397bfbe
GL
707 if (data < 0)
708 return -EIO;
709 break;
710 case V4L2_CID_EXPOSURE_AUTO:
711 if (ctrl->value)
9538e1c2 712 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
7397bfbe 713 else
9538e1c2 714 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
7397bfbe
GL
715 if (data < 0)
716 return -EIO;
717 break;
718 }
719 return 0;
720}
721
5d28d525
GL
722/*
723 * Interface active, can use i2c. If it fails, it can indeed mean, that
724 * this wasn't our capture interface, so, we wait for the right one
725 */
40e2e092
GL
726static int mt9v022_video_probe(struct soc_camera_device *icd,
727 struct i2c_client *client)
7397bfbe 728{
979ea1dd 729 struct mt9v022 *mt9v022 = to_mt9v022(client);
40e2e092 730 struct soc_camera_link *icl = to_soc_camera_link(icd);
7397bfbe
GL
731 s32 data;
732 int ret;
e958e27a 733 unsigned long flags;
7397bfbe
GL
734
735 if (!icd->dev.parent ||
736 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
737 return -ENODEV;
738
739 /* Read out the chip version register */
9538e1c2 740 data = reg_read(client, MT9V022_CHIP_VERSION);
7397bfbe
GL
741
742 /* must be 0x1311 or 0x1313 */
743 if (data != 0x1311 && data != 0x1313) {
744 ret = -ENODEV;
85f8be68 745 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
7397bfbe
GL
746 data);
747 goto ei2c;
748 }
749
750 /* Soft reset */
9538e1c2 751 ret = reg_write(client, MT9V022_RESET, 1);
7397bfbe
GL
752 if (ret < 0)
753 goto ei2c;
754 /* 15 clock cycles */
755 udelay(200);
9538e1c2 756 if (reg_read(client, MT9V022_RESET)) {
85f8be68 757 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
40e2e092
GL
758 if (ret > 0)
759 ret = -EIO;
7397bfbe
GL
760 goto ei2c;
761 }
762
763 /* Set monochrome or colour sensor type */
764 if (sensor_type && (!strcmp("colour", sensor_type) ||
765 !strcmp("color", sensor_type))) {
9538e1c2 766 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
7397bfbe 767 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
760697be 768 mt9v022->fmts = mt9v022_colour_fmts;
7397bfbe 769 } else {
9538e1c2 770 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
7397bfbe 771 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
760697be 772 mt9v022->fmts = mt9v022_monochrome_fmts;
7397bfbe
GL
773 }
774
e958e27a 775 if (ret < 0)
40e2e092 776 goto ei2c;
e958e27a 777
760697be 778 mt9v022->num_fmts = 0;
e958e27a
SH
779
780 /*
781 * This is a 10bit sensor, so by default we only allow 10bit.
782 * The platform may support different bus widths due to
783 * different routing of the data lines.
784 */
785 if (icl->query_bus_param)
786 flags = icl->query_bus_param(icl);
787 else
788 flags = SOCAM_DATAWIDTH_10;
789
790 if (flags & SOCAM_DATAWIDTH_10)
760697be 791 mt9v022->num_fmts++;
e958e27a 792 else
760697be 793 mt9v022->fmts++;
e958e27a
SH
794
795 if (flags & SOCAM_DATAWIDTH_8)
760697be 796 mt9v022->num_fmts++;
e958e27a 797
760697be 798 mt9v022->fmt = &mt9v022->fmts[0];
6a6c8786 799
85f8be68 800 dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
7397bfbe
GL
801 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
802 "monochrome" : "colour");
803
a4c56fd8
GL
804 ret = mt9v022_init(client);
805 if (ret < 0)
806 dev_err(&client->dev, "Failed to initialise the camera\n");
807
7397bfbe
GL
808ei2c:
809 return ret;
810}
811
812static void mt9v022_video_remove(struct soc_camera_device *icd)
813{
40e2e092 814 struct soc_camera_link *icl = to_soc_camera_link(icd);
7397bfbe 815
6a6c8786 816 dev_dbg(&icd->dev, "Video removed: %p, %p\n",
a85bdace 817 icd->dev.parent, icd->vdev);
594bb46d
GL
818 if (icl->free_bus)
819 icl->free_bus(icl);
7397bfbe
GL
820}
821
32536108
GL
822static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
823{
824 struct i2c_client *client = sd->priv;
825 struct mt9v022 *mt9v022 = to_mt9v022(client);
826
827 *lines = mt9v022->y_skip_top;
828
829 return 0;
830}
831
979ea1dd
GL
832static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
833 .g_ctrl = mt9v022_g_ctrl,
834 .s_ctrl = mt9v022_s_ctrl,
835 .g_chip_ident = mt9v022_g_chip_ident,
836#ifdef CONFIG_VIDEO_ADV_DEBUG
837 .g_register = mt9v022_g_register,
838 .s_register = mt9v022_s_register,
839#endif
840};
841
760697be
GL
842static int mt9v022_enum_fmt(struct v4l2_subdev *sd, int index,
843 enum v4l2_mbus_pixelcode *code)
844{
845 struct i2c_client *client = sd->priv;
846 struct mt9v022 *mt9v022 = to_mt9v022(client);
847
848 if ((unsigned int)index >= mt9v022->num_fmts)
849 return -EINVAL;
850
851 *code = mt9v022->fmts[index].code;
852 return 0;
853}
854
979ea1dd
GL
855static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
856 .s_stream = mt9v022_s_stream,
760697be
GL
857 .s_mbus_fmt = mt9v022_s_fmt,
858 .g_mbus_fmt = mt9v022_g_fmt,
859 .try_mbus_fmt = mt9v022_try_fmt,
08590b96 860 .s_crop = mt9v022_s_crop,
6a6c8786
GL
861 .g_crop = mt9v022_g_crop,
862 .cropcap = mt9v022_cropcap,
760697be 863 .enum_mbus_fmt = mt9v022_enum_fmt,
979ea1dd
GL
864};
865
32536108
GL
866static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
867 .g_skip_top_lines = mt9v022_g_skip_top_lines,
868};
869
979ea1dd
GL
870static struct v4l2_subdev_ops mt9v022_subdev_ops = {
871 .core = &mt9v022_subdev_core_ops,
872 .video = &mt9v022_subdev_video_ops,
32536108 873 .sensor = &mt9v022_subdev_sensor_ops,
979ea1dd
GL
874};
875
d2653e92
JD
876static int mt9v022_probe(struct i2c_client *client,
877 const struct i2c_device_id *did)
7397bfbe
GL
878{
879 struct mt9v022 *mt9v022;
40e2e092 880 struct soc_camera_device *icd = client->dev.platform_data;
7397bfbe 881 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
40e2e092 882 struct soc_camera_link *icl;
7397bfbe
GL
883 int ret;
884
40e2e092
GL
885 if (!icd) {
886 dev_err(&client->dev, "MT9V022: missing soc-camera data!\n");
887 return -EINVAL;
888 }
889
890 icl = to_soc_camera_link(icd);
7397bfbe
GL
891 if (!icl) {
892 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
893 return -EINVAL;
894 }
895
896 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
897 dev_warn(&adapter->dev,
898 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
899 return -EIO;
900 }
901
902 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
903 if (!mt9v022)
904 return -ENOMEM;
905
979ea1dd
GL
906 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
907
7397bfbe 908 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
7397bfbe 909
a0705b07 910 icd->ops = &mt9v022_ops;
96c75399
GL
911 /*
912 * MT9V022 _really_ corrupts the first read out line.
913 * TODO: verify on i.MX31
914 */
32536108 915 mt9v022->y_skip_top = 1;
6a6c8786
GL
916 mt9v022->rect.left = MT9V022_COLUMN_SKIP;
917 mt9v022->rect.top = MT9V022_ROW_SKIP;
918 mt9v022->rect.width = MT9V022_MAX_WIDTH;
919 mt9v022->rect.height = MT9V022_MAX_HEIGHT;
920
40e2e092
GL
921 ret = mt9v022_video_probe(icd, client);
922 if (ret) {
923 icd->ops = NULL;
924 i2c_set_clientdata(client, NULL);
925 kfree(mt9v022);
926 }
7397bfbe 927
7397bfbe
GL
928 return ret;
929}
930
931static int mt9v022_remove(struct i2c_client *client)
932{
979ea1dd 933 struct mt9v022 *mt9v022 = to_mt9v022(client);
40e2e092 934 struct soc_camera_device *icd = client->dev.platform_data;
7397bfbe 935
40e2e092
GL
936 icd->ops = NULL;
937 mt9v022_video_remove(icd);
938 i2c_set_clientdata(client, NULL);
939 client->driver = NULL;
7397bfbe
GL
940 kfree(mt9v022);
941
942 return 0;
943}
3760f736
JD
944static const struct i2c_device_id mt9v022_id[] = {
945 { "mt9v022", 0 },
946 { }
947};
948MODULE_DEVICE_TABLE(i2c, mt9v022_id);
949
7397bfbe
GL
950static struct i2c_driver mt9v022_i2c_driver = {
951 .driver = {
952 .name = "mt9v022",
953 },
954 .probe = mt9v022_probe,
955 .remove = mt9v022_remove,
3760f736 956 .id_table = mt9v022_id,
7397bfbe
GL
957};
958
959static int __init mt9v022_mod_init(void)
960{
961 return i2c_add_driver(&mt9v022_i2c_driver);
962}
963
964static void __exit mt9v022_mod_exit(void)
965{
966 i2c_del_driver(&mt9v022_i2c_driver);
967}
968
969module_init(mt9v022_mod_init);
970module_exit(mt9v022_mod_exit);
971
972MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
973MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
974MODULE_LICENSE("GPL");