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