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