]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/media/video/mt9m001.c
V4L/DVB (12535): soc-camera: remove .init() and .release() methods from struct soc_ca...
[mirror_ubuntu-zesty-kernel.git] / drivers / media / video / mt9m001.c
CommitLineData
f523dd0d
GL
1/*
2 * Driver for MT9M001 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/log2.h>
15
979ea1dd 16#include <media/v4l2-subdev.h>
f523dd0d
GL
17#include <media/v4l2-chip-ident.h>
18#include <media/soc_camera.h>
19
f523dd0d 20/* mt9m001 i2c address 0x5d
979ea1dd
GL
21 * The platform has to define ctruct i2c_board_info objects and link to them
22 * from struct soc_camera_link */
f523dd0d
GL
23
24/* mt9m001 selected register addresses */
25#define MT9M001_CHIP_VERSION 0x00
26#define MT9M001_ROW_START 0x01
27#define MT9M001_COLUMN_START 0x02
28#define MT9M001_WINDOW_HEIGHT 0x03
29#define MT9M001_WINDOW_WIDTH 0x04
30#define MT9M001_HORIZONTAL_BLANKING 0x05
31#define MT9M001_VERTICAL_BLANKING 0x06
32#define MT9M001_OUTPUT_CONTROL 0x07
33#define MT9M001_SHUTTER_WIDTH 0x09
34#define MT9M001_FRAME_RESTART 0x0b
35#define MT9M001_SHUTTER_DELAY 0x0c
36#define MT9M001_RESET 0x0d
37#define MT9M001_READ_OPTIONS1 0x1e
38#define MT9M001_READ_OPTIONS2 0x20
39#define MT9M001_GLOBAL_GAIN 0x35
40#define MT9M001_CHIP_ENABLE 0xF1
41
6a6c8786
GL
42#define MT9M001_MAX_WIDTH 1280
43#define MT9M001_MAX_HEIGHT 1024
44#define MT9M001_MIN_WIDTH 48
45#define MT9M001_MIN_HEIGHT 32
46#define MT9M001_COLUMN_SKIP 20
47#define MT9M001_ROW_SKIP 12
48
f523dd0d 49static const struct soc_camera_data_format mt9m001_colour_formats[] = {
bb55de3b
GL
50 /* Order important: first natively supported,
51 * second supported with a GPIO extender */
f523dd0d 52 {
bb55de3b
GL
53 .name = "Bayer (sRGB) 10 bit",
54 .depth = 10,
55 .fourcc = V4L2_PIX_FMT_SBGGR16,
56 .colorspace = V4L2_COLORSPACE_SRGB,
57 }, {
58 .name = "Bayer (sRGB) 8 bit",
59 .depth = 8,
f523dd0d
GL
60 .fourcc = V4L2_PIX_FMT_SBGGR8,
61 .colorspace = V4L2_COLORSPACE_SRGB,
62 }
63};
64
65static const struct soc_camera_data_format mt9m001_monochrome_formats[] = {
bb55de3b 66 /* Order important - see above */
f523dd0d
GL
67 {
68 .name = "Monochrome 10 bit",
69 .depth = 10,
70 .fourcc = V4L2_PIX_FMT_Y16,
71 }, {
72 .name = "Monochrome 8 bit",
73 .depth = 8,
74 .fourcc = V4L2_PIX_FMT_GREY,
75 },
76};
77
78struct mt9m001 {
979ea1dd 79 struct v4l2_subdev subdev;
6a6c8786
GL
80 struct v4l2_rect rect; /* Sensor window */
81 __u32 fourcc;
f523dd0d 82 int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
f523dd0d 83 unsigned char autoexposure;
f523dd0d
GL
84};
85
979ea1dd
GL
86static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
87{
88 return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
89}
90
9538e1c2 91static int reg_read(struct i2c_client *client, const u8 reg)
f523dd0d 92{
f523dd0d
GL
93 s32 data = i2c_smbus_read_word_data(client, reg);
94 return data < 0 ? data : swab16(data);
95}
96
9538e1c2 97static int reg_write(struct i2c_client *client, const u8 reg,
f523dd0d
GL
98 const u16 data)
99{
9538e1c2 100 return i2c_smbus_write_word_data(client, reg, swab16(data));
f523dd0d
GL
101}
102
9538e1c2 103static int reg_set(struct i2c_client *client, const u8 reg,
f523dd0d
GL
104 const u16 data)
105{
106 int ret;
107
9538e1c2 108 ret = reg_read(client, reg);
f523dd0d
GL
109 if (ret < 0)
110 return ret;
9538e1c2 111 return reg_write(client, reg, ret | data);
f523dd0d
GL
112}
113
9538e1c2 114static int reg_clear(struct i2c_client *client, const u8 reg,
f523dd0d
GL
115 const u16 data)
116{
117 int ret;
118
9538e1c2 119 ret = reg_read(client, reg);
f523dd0d
GL
120 if (ret < 0)
121 return ret;
9538e1c2 122 return reg_write(client, reg, ret & ~data);
f523dd0d
GL
123}
124
a4c56fd8 125static int mt9m001_init(struct i2c_client *client)
f523dd0d
GL
126{
127 int ret;
128
85f8be68 129 dev_dbg(&client->dev, "%s\n", __func__);
f523dd0d 130
979ea1dd
GL
131 /*
132 * We don't know, whether platform provides reset,
133 * issue a soft reset too
134 */
135 ret = reg_write(client, MT9M001_RESET, 1);
136 if (!ret)
137 ret = reg_write(client, MT9M001_RESET, 0);
81034663 138
11211641
GL
139 /* Disable chip, synchronous option update */
140 if (!ret)
9538e1c2 141 ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
f523dd0d 142
11211641 143 return ret;
f523dd0d
GL
144}
145
979ea1dd 146static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
f523dd0d 147{
979ea1dd 148 struct i2c_client *client = sd->priv;
9538e1c2 149
979ea1dd
GL
150 /* Switch to master "normal" mode or stop sensor readout */
151 if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0)
f523dd0d
GL
152 return -EIO;
153 return 0;
154}
155
ad5f2e85
GL
156static int mt9m001_set_bus_param(struct soc_camera_device *icd,
157 unsigned long flags)
f523dd0d 158{
40e2e092 159 struct soc_camera_link *icl = to_soc_camera_link(icd);
36034dc3 160 unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
f523dd0d 161
36034dc3
SH
162 /* Only one width bit may be set */
163 if (!is_power_of_2(width_flag))
164 return -EINVAL;
f523dd0d 165
36034dc3
SH
166 if (icl->set_bus_param)
167 return icl->set_bus_param(icl, width_flag);
f523dd0d 168
36034dc3
SH
169 /*
170 * Without board specific bus width settings we only support the
171 * sensors native bus width
172 */
173 if (width_flag == SOCAM_DATAWIDTH_10)
174 return 0;
f523dd0d 175
36034dc3 176 return -EINVAL;
ad5f2e85
GL
177}
178
179static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
180{
40e2e092 181 struct soc_camera_link *icl = to_soc_camera_link(icd);
bd73b36f 182 /* MT9M001 has all capture_format parameters fixed */
e951cbf2 183 unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING |
bd73b36f 184 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
2d9329f3 185 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER;
ad5f2e85 186
36034dc3
SH
187 if (icl->query_bus_param)
188 flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
189 else
190 flags |= SOCAM_DATAWIDTH_10;
ad5f2e85 191
bd73b36f 192 return soc_camera_apply_sensor_flags(icl, flags);
ad5f2e85
GL
193}
194
08590b96 195static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
ad5f2e85 196{
08590b96 197 struct i2c_client *client = sd->priv;
979ea1dd 198 struct mt9m001 *mt9m001 = to_mt9m001(client);
6a6c8786 199 struct v4l2_rect rect = a->c;
08590b96 200 struct soc_camera_device *icd = client->dev.platform_data;
ad5f2e85
GL
201 int ret;
202 const u16 hblank = 9, vblank = 25;
203
6a6c8786
GL
204 if (mt9m001->fourcc == V4L2_PIX_FMT_SBGGR8 ||
205 mt9m001->fourcc == V4L2_PIX_FMT_SBGGR16)
206 /*
207 * Bayer format - even number of rows for simplicity,
208 * but let the user play with the top row.
209 */
210 rect.height = ALIGN(rect.height, 2);
211
212 /* Datasheet requirement: see register description */
213 rect.width = ALIGN(rect.width, 2);
214 rect.left = ALIGN(rect.left, 2);
215
216 soc_camera_limit_side(&rect.left, &rect.width,
217 MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH);
218
219 soc_camera_limit_side(&rect.top, &rect.height,
220 MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT);
221
f523dd0d 222 /* Blanking and start values - default... */
9538e1c2 223 ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
11211641 224 if (!ret)
9538e1c2 225 ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
f523dd0d
GL
226
227 /* The caller provides a supported format, as verified per
d8fac217 228 * call to icd->try_fmt() */
11211641 229 if (!ret)
6a6c8786 230 ret = reg_write(client, MT9M001_COLUMN_START, rect.left);
11211641 231 if (!ret)
6a6c8786 232 ret = reg_write(client, MT9M001_ROW_START, rect.top);
11211641 233 if (!ret)
6a6c8786 234 ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1);
11211641 235 if (!ret)
9538e1c2 236 ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
6a6c8786 237 rect.height + icd->y_skip_top - 1);
11211641 238 if (!ret && mt9m001->autoexposure) {
9538e1c2 239 ret = reg_write(client, MT9M001_SHUTTER_WIDTH,
6a6c8786 240 rect.height + icd->y_skip_top + vblank);
11211641 241 if (!ret) {
f523dd0d
GL
242 const struct v4l2_queryctrl *qctrl =
243 soc_camera_find_qctrl(icd->ops,
244 V4L2_CID_EXPOSURE);
6a6c8786 245 icd->exposure = (524 + (rect.height + icd->y_skip_top +
f523dd0d
GL
246 vblank - 1) *
247 (qctrl->maximum - qctrl->minimum)) /
248 1048 + qctrl->minimum;
249 }
250 }
251
6a6c8786
GL
252 if (!ret)
253 mt9m001->rect = rect;
254
11211641 255 return ret;
f523dd0d
GL
256}
257
6a6c8786
GL
258static int mt9m001_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
259{
260 struct i2c_client *client = sd->priv;
261 struct mt9m001 *mt9m001 = to_mt9m001(client);
262
263 a->c = mt9m001->rect;
264 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
265
266 return 0;
267}
268
269static int mt9m001_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
270{
271 a->bounds.left = MT9M001_COLUMN_SKIP;
272 a->bounds.top = MT9M001_ROW_SKIP;
273 a->bounds.width = MT9M001_MAX_WIDTH;
274 a->bounds.height = MT9M001_MAX_HEIGHT;
275 a->defrect = a->bounds;
276 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
277 a->pixelaspect.numerator = 1;
278 a->pixelaspect.denominator = 1;
279
280 return 0;
281}
282
283static int mt9m001_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
284{
285 struct i2c_client *client = sd->priv;
286 struct mt9m001 *mt9m001 = to_mt9m001(client);
287 struct v4l2_pix_format *pix = &f->fmt.pix;
288
289 pix->width = mt9m001->rect.width;
290 pix->height = mt9m001->rect.height;
291 pix->pixelformat = mt9m001->fourcc;
292 pix->field = V4L2_FIELD_NONE;
293 pix->colorspace = V4L2_COLORSPACE_SRGB;
294
295 return 0;
296}
297
979ea1dd 298static int mt9m001_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
09e231b3 299{
979ea1dd 300 struct i2c_client *client = sd->priv;
6a6c8786
GL
301 struct mt9m001 *mt9m001 = to_mt9m001(client);
302 struct v4l2_pix_format *pix = &f->fmt.pix;
08590b96
GL
303 struct v4l2_crop a = {
304 .c = {
6a6c8786
GL
305 .left = mt9m001->rect.left,
306 .top = mt9m001->rect.top,
307 .width = pix->width,
308 .height = pix->height,
08590b96 309 },
09e231b3 310 };
6a6c8786 311 int ret;
09e231b3
GL
312
313 /* No support for scaling so far, just crop. TODO: use skipping */
6a6c8786
GL
314 ret = mt9m001_s_crop(sd, &a);
315 if (!ret) {
316 pix->width = mt9m001->rect.width;
317 pix->height = mt9m001->rect.height;
318 mt9m001->fourcc = pix->pixelformat;
319 }
320
321 return ret;
09e231b3
GL
322}
323
979ea1dd 324static int mt9m001_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
f523dd0d 325{
979ea1dd
GL
326 struct i2c_client *client = sd->priv;
327 struct soc_camera_device *icd = client->dev.platform_data;
64f5905e
GL
328 struct v4l2_pix_format *pix = &f->fmt.pix;
329
6a6c8786
GL
330 v4l_bound_align_image(&pix->width, MT9M001_MIN_WIDTH,
331 MT9M001_MAX_WIDTH, 1,
332 &pix->height, MT9M001_MIN_HEIGHT + icd->y_skip_top,
333 MT9M001_MAX_HEIGHT + icd->y_skip_top, 0, 0);
334
335 if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8 ||
336 pix->pixelformat == V4L2_PIX_FMT_SBGGR16)
337 pix->height = ALIGN(pix->height - 1, 2);
f523dd0d
GL
338
339 return 0;
340}
341
979ea1dd
GL
342static int mt9m001_g_chip_ident(struct v4l2_subdev *sd,
343 struct v4l2_dbg_chip_ident *id)
f523dd0d 344{
979ea1dd
GL
345 struct i2c_client *client = sd->priv;
346 struct mt9m001 *mt9m001 = to_mt9m001(client);
f523dd0d 347
aecde8b5 348 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
f523dd0d
GL
349 return -EINVAL;
350
40e2e092 351 if (id->match.addr != client->addr)
f523dd0d
GL
352 return -ENODEV;
353
354 id->ident = mt9m001->model;
355 id->revision = 0;
356
357 return 0;
358}
359
360#ifdef CONFIG_VIDEO_ADV_DEBUG
979ea1dd
GL
361static int mt9m001_g_register(struct v4l2_subdev *sd,
362 struct v4l2_dbg_register *reg)
f523dd0d 363{
979ea1dd 364 struct i2c_client *client = sd->priv;
f523dd0d 365
aecde8b5 366 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
f523dd0d
GL
367 return -EINVAL;
368
9538e1c2 369 if (reg->match.addr != client->addr)
f523dd0d
GL
370 return -ENODEV;
371
aecde8b5 372 reg->size = 2;
9538e1c2 373 reg->val = reg_read(client, reg->reg);
f523dd0d
GL
374
375 if (reg->val > 0xffff)
376 return -EIO;
377
378 return 0;
379}
380
979ea1dd
GL
381static int mt9m001_s_register(struct v4l2_subdev *sd,
382 struct v4l2_dbg_register *reg)
f523dd0d 383{
979ea1dd 384 struct i2c_client *client = sd->priv;
f523dd0d 385
aecde8b5 386 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
f523dd0d
GL
387 return -EINVAL;
388
9538e1c2 389 if (reg->match.addr != client->addr)
f523dd0d
GL
390 return -ENODEV;
391
9538e1c2 392 if (reg_write(client, reg->reg, reg->val) < 0)
f523dd0d
GL
393 return -EIO;
394
395 return 0;
396}
397#endif
398
4407a463 399static const struct v4l2_queryctrl mt9m001_controls[] = {
f523dd0d
GL
400 {
401 .id = V4L2_CID_VFLIP,
402 .type = V4L2_CTRL_TYPE_BOOLEAN,
403 .name = "Flip Vertically",
404 .minimum = 0,
405 .maximum = 1,
406 .step = 1,
407 .default_value = 0,
408 }, {
409 .id = V4L2_CID_GAIN,
410 .type = V4L2_CTRL_TYPE_INTEGER,
411 .name = "Gain",
412 .minimum = 0,
413 .maximum = 127,
414 .step = 1,
415 .default_value = 64,
416 .flags = V4L2_CTRL_FLAG_SLIDER,
417 }, {
418 .id = V4L2_CID_EXPOSURE,
419 .type = V4L2_CTRL_TYPE_INTEGER,
420 .name = "Exposure",
421 .minimum = 1,
422 .maximum = 255,
423 .step = 1,
424 .default_value = 255,
425 .flags = V4L2_CTRL_FLAG_SLIDER,
426 }, {
427 .id = V4L2_CID_EXPOSURE_AUTO,
428 .type = V4L2_CTRL_TYPE_BOOLEAN,
429 .name = "Automatic Exposure",
430 .minimum = 0,
431 .maximum = 1,
432 .step = 1,
433 .default_value = 1,
434 }
435};
436
f523dd0d 437static struct soc_camera_ops mt9m001_ops = {
ad5f2e85
GL
438 .set_bus_param = mt9m001_set_bus_param,
439 .query_bus_param = mt9m001_query_bus_param,
f523dd0d
GL
440 .controls = mt9m001_controls,
441 .num_controls = ARRAY_SIZE(mt9m001_controls),
f523dd0d
GL
442};
443
979ea1dd 444static int mt9m001_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
f523dd0d 445{
979ea1dd
GL
446 struct i2c_client *client = sd->priv;
447 struct mt9m001 *mt9m001 = to_mt9m001(client);
f523dd0d
GL
448 int data;
449
450 switch (ctrl->id) {
451 case V4L2_CID_VFLIP:
9538e1c2 452 data = reg_read(client, MT9M001_READ_OPTIONS2);
f523dd0d
GL
453 if (data < 0)
454 return -EIO;
455 ctrl->value = !!(data & 0x8000);
456 break;
457 case V4L2_CID_EXPOSURE_AUTO:
458 ctrl->value = mt9m001->autoexposure;
459 break;
460 }
461 return 0;
462}
463
979ea1dd 464static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
f523dd0d 465{
979ea1dd
GL
466 struct i2c_client *client = sd->priv;
467 struct mt9m001 *mt9m001 = to_mt9m001(client);
468 struct soc_camera_device *icd = client->dev.platform_data;
f523dd0d
GL
469 const struct v4l2_queryctrl *qctrl;
470 int data;
471
472 qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
473
474 if (!qctrl)
475 return -EINVAL;
476
477 switch (ctrl->id) {
478 case V4L2_CID_VFLIP:
479 if (ctrl->value)
9538e1c2 480 data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
f523dd0d 481 else
9538e1c2 482 data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
f523dd0d
GL
483 if (data < 0)
484 return -EIO;
485 break;
486 case V4L2_CID_GAIN:
487 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
488 return -EINVAL;
489 /* See Datasheet Table 7, Gain settings. */
490 if (ctrl->value <= qctrl->default_value) {
491 /* Pack it into 0..1 step 0.125, register values 0..8 */
492 unsigned long range = qctrl->default_value - qctrl->minimum;
493 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
494
85f8be68 495 dev_dbg(&client->dev, "Setting gain %d\n", data);
9538e1c2 496 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
f523dd0d
GL
497 if (data < 0)
498 return -EIO;
499 } else {
500 /* Pack it into 1.125..15 variable step, register values 9..67 */
501 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
502 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
503 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
504 111 + range / 2) / range + 9;
505
506 if (gain <= 32)
507 data = gain;
508 else if (gain <= 64)
509 data = ((gain - 32) * 16 + 16) / 32 + 80;
510 else
511 data = ((gain - 64) * 7 + 28) / 56 + 96;
512
85f8be68 513 dev_dbg(&client->dev, "Setting gain from %d to %d\n",
9538e1c2
GL
514 reg_read(client, MT9M001_GLOBAL_GAIN), data);
515 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
f523dd0d
GL
516 if (data < 0)
517 return -EIO;
518 }
519
520 /* Success */
521 icd->gain = ctrl->value;
522 break;
523 case V4L2_CID_EXPOSURE:
524 /* mt9m001 has maximum == default */
525 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
526 return -EINVAL;
527 else {
528 unsigned long range = qctrl->maximum - qctrl->minimum;
529 unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
530 range / 2) / range + 1;
531
85f8be68
GL
532 dev_dbg(&client->dev,
533 "Setting shutter width from %d to %lu\n",
534 reg_read(client, MT9M001_SHUTTER_WIDTH),
535 shutter);
9538e1c2 536 if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
f523dd0d
GL
537 return -EIO;
538 icd->exposure = ctrl->value;
539 mt9m001->autoexposure = 0;
540 }
541 break;
542 case V4L2_CID_EXPOSURE_AUTO:
543 if (ctrl->value) {
544 const u16 vblank = 25;
a0705b07 545 if (reg_write(client, MT9M001_SHUTTER_WIDTH,
6a6c8786 546 mt9m001->rect.height +
f523dd0d
GL
547 icd->y_skip_top + vblank) < 0)
548 return -EIO;
549 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
6a6c8786 550 icd->exposure = (524 + (mt9m001->rect.height +
a0705b07 551 icd->y_skip_top + vblank - 1) *
f523dd0d
GL
552 (qctrl->maximum - qctrl->minimum)) /
553 1048 + qctrl->minimum;
554 mt9m001->autoexposure = 1;
555 } else
556 mt9m001->autoexposure = 0;
557 break;
558 }
559 return 0;
560}
561
562/* Interface active, can use i2c. If it fails, it can indeed mean, that
563 * this wasn't our capture interface, so, we wait for the right one */
40e2e092
GL
564static int mt9m001_video_probe(struct soc_camera_device *icd,
565 struct i2c_client *client)
f523dd0d 566{
979ea1dd 567 struct mt9m001 *mt9m001 = to_mt9m001(client);
40e2e092 568 struct soc_camera_link *icl = to_soc_camera_link(icd);
f523dd0d 569 s32 data;
36034dc3 570 unsigned long flags;
a4c56fd8 571 int ret;
f523dd0d
GL
572
573 /* We must have a parent by now. And it cannot be a wrong one.
574 * So this entire test is completely redundant. */
575 if (!icd->dev.parent ||
576 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
577 return -ENODEV;
578
579 /* Enable the chip */
9538e1c2 580 data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
85f8be68 581 dev_dbg(&client->dev, "write: %d\n", data);
f523dd0d
GL
582
583 /* Read out the chip version register */
9538e1c2 584 data = reg_read(client, MT9M001_CHIP_VERSION);
f523dd0d
GL
585
586 /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
587 switch (data) {
588 case 0x8411:
589 case 0x8421:
590 mt9m001->model = V4L2_IDENT_MT9M001C12ST;
26f1b942 591 icd->formats = mt9m001_colour_formats;
f523dd0d
GL
592 break;
593 case 0x8431:
594 mt9m001->model = V4L2_IDENT_MT9M001C12STM;
26f1b942 595 icd->formats = mt9m001_monochrome_formats;
f523dd0d
GL
596 break;
597 default:
85f8be68 598 dev_err(&client->dev,
f523dd0d 599 "No MT9M001 chip detected, register read %x\n", data);
40e2e092 600 return -ENODEV;
f523dd0d
GL
601 }
602
36034dc3
SH
603 icd->num_formats = 0;
604
605 /*
606 * This is a 10bit sensor, so by default we only allow 10bit.
607 * The platform may support different bus widths due to
608 * different routing of the data lines.
609 */
610 if (icl->query_bus_param)
611 flags = icl->query_bus_param(icl);
612 else
613 flags = SOCAM_DATAWIDTH_10;
614
615 if (flags & SOCAM_DATAWIDTH_10)
616 icd->num_formats++;
617 else
618 icd->formats++;
619
620 if (flags & SOCAM_DATAWIDTH_8)
621 icd->num_formats++;
622
6a6c8786
GL
623 mt9m001->fourcc = icd->formats->fourcc;
624
85f8be68 625 dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
f523dd0d
GL
626 data == 0x8431 ? "C12STM" : "C12ST");
627
a4c56fd8
GL
628 ret = mt9m001_init(client);
629 if (ret < 0)
630 dev_err(&client->dev, "Failed to initialise the camera\n");
631
632 return ret;
f523dd0d
GL
633}
634
635static void mt9m001_video_remove(struct soc_camera_device *icd)
636{
40e2e092 637 struct soc_camera_link *icl = to_soc_camera_link(icd);
f523dd0d 638
6a6c8786 639 dev_dbg(&icd->dev, "Video removed: %p, %p\n",
a85bdace 640 icd->dev.parent, icd->vdev);
594bb46d
GL
641 if (icl->free_bus)
642 icl->free_bus(icl);
f523dd0d
GL
643}
644
979ea1dd
GL
645static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
646 .g_ctrl = mt9m001_g_ctrl,
647 .s_ctrl = mt9m001_s_ctrl,
648 .g_chip_ident = mt9m001_g_chip_ident,
649#ifdef CONFIG_VIDEO_ADV_DEBUG
650 .g_register = mt9m001_g_register,
651 .s_register = mt9m001_s_register,
652#endif
653};
654
655static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
656 .s_stream = mt9m001_s_stream,
657 .s_fmt = mt9m001_s_fmt,
6a6c8786 658 .g_fmt = mt9m001_g_fmt,
979ea1dd 659 .try_fmt = mt9m001_try_fmt,
08590b96 660 .s_crop = mt9m001_s_crop,
6a6c8786
GL
661 .g_crop = mt9m001_g_crop,
662 .cropcap = mt9m001_cropcap,
979ea1dd
GL
663};
664
665static struct v4l2_subdev_ops mt9m001_subdev_ops = {
666 .core = &mt9m001_subdev_core_ops,
667 .video = &mt9m001_subdev_video_ops,
668};
669
d2653e92
JD
670static int mt9m001_probe(struct i2c_client *client,
671 const struct i2c_device_id *did)
f523dd0d
GL
672{
673 struct mt9m001 *mt9m001;
40e2e092 674 struct soc_camera_device *icd = client->dev.platform_data;
f523dd0d 675 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
40e2e092 676 struct soc_camera_link *icl;
f523dd0d
GL
677 int ret;
678
40e2e092
GL
679 if (!icd) {
680 dev_err(&client->dev, "MT9M001: missing soc-camera data!\n");
681 return -EINVAL;
682 }
683
684 icl = to_soc_camera_link(icd);
f523dd0d
GL
685 if (!icl) {
686 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
687 return -EINVAL;
688 }
689
690 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
691 dev_warn(&adapter->dev,
692 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
693 return -EIO;
694 }
695
696 mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
697 if (!mt9m001)
698 return -ENOMEM;
699
979ea1dd 700 v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
f523dd0d
GL
701
702 /* Second stage probe - when a capture adapter is there */
a0705b07 703 icd->ops = &mt9m001_ops;
a0705b07 704 icd->y_skip_top = 1;
6a6c8786
GL
705
706 mt9m001->rect.left = MT9M001_COLUMN_SKIP;
707 mt9m001->rect.top = MT9M001_ROW_SKIP;
708 mt9m001->rect.width = MT9M001_MAX_WIDTH;
709 mt9m001->rect.height = MT9M001_MAX_HEIGHT;
710
f523dd0d
GL
711 /* Simulated autoexposure. If enabled, we calculate shutter width
712 * ourselves in the driver based on vertical blanking and frame width */
713 mt9m001->autoexposure = 1;
714
40e2e092
GL
715 ret = mt9m001_video_probe(icd, client);
716 if (ret) {
717 icd->ops = NULL;
718 i2c_set_clientdata(client, NULL);
719 kfree(mt9m001);
720 }
f523dd0d 721
f523dd0d
GL
722 return ret;
723}
724
725static int mt9m001_remove(struct i2c_client *client)
726{
979ea1dd 727 struct mt9m001 *mt9m001 = to_mt9m001(client);
40e2e092 728 struct soc_camera_device *icd = client->dev.platform_data;
f523dd0d 729
40e2e092
GL
730 icd->ops = NULL;
731 mt9m001_video_remove(icd);
732 i2c_set_clientdata(client, NULL);
733 client->driver = NULL;
f523dd0d
GL
734 kfree(mt9m001);
735
736 return 0;
737}
738
3760f736
JD
739static const struct i2c_device_id mt9m001_id[] = {
740 { "mt9m001", 0 },
741 { }
742};
743MODULE_DEVICE_TABLE(i2c, mt9m001_id);
744
f523dd0d
GL
745static struct i2c_driver mt9m001_i2c_driver = {
746 .driver = {
747 .name = "mt9m001",
748 },
749 .probe = mt9m001_probe,
750 .remove = mt9m001_remove,
3760f736 751 .id_table = mt9m001_id,
f523dd0d
GL
752};
753
754static int __init mt9m001_mod_init(void)
755{
756 return i2c_add_driver(&mt9m001_i2c_driver);
757}
758
759static void __exit mt9m001_mod_exit(void)
760{
761 i2c_del_driver(&mt9m001_i2c_driver);
762}
763
764module_init(mt9m001_mod_init);
765module_exit(mt9m001_mod_exit);
766
767MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
768MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
769MODULE_LICENSE("GPL");