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