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