]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/media/video/mt9m001.c
V4L/DVB (11031): pcm990 baseboard: add camera bus width switch setting
[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 |
2d9329f3 279 SOCAM_DATA_ACTIVE_HIGH | 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
09e231b3
GL
287static int mt9m001_set_crop(struct soc_camera_device *icd,
288 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
09e231b3
GL
327static int mt9m001_set_fmt(struct soc_camera_device *icd,
328 struct v4l2_format *f)
329{
330 struct v4l2_rect rect = {
331 .left = icd->x_current,
332 .top = icd->y_current,
333 .width = f->fmt.pix.width,
334 .height = f->fmt.pix.height,
335 };
336
337 /* No support for scaling so far, just crop. TODO: use skipping */
338 return mt9m001_set_crop(icd, &rect);
339}
340
d8fac217
GL
341static int mt9m001_try_fmt(struct soc_camera_device *icd,
342 struct v4l2_format *f)
f523dd0d 343{
64f5905e
GL
344 struct v4l2_pix_format *pix = &f->fmt.pix;
345
346 if (pix->height < 32 + icd->y_skip_top)
347 pix->height = 32 + icd->y_skip_top;
348 if (pix->height > 1024 + icd->y_skip_top)
349 pix->height = 1024 + icd->y_skip_top;
350 if (pix->width < 48)
351 pix->width = 48;
352 if (pix->width > 1280)
353 pix->width = 1280;
354 pix->width &= ~0x01; /* has to be even, unsure why was ~3 */
f523dd0d
GL
355
356 return 0;
357}
358
359static int mt9m001_get_chip_id(struct soc_camera_device *icd,
aecde8b5 360 struct v4l2_dbg_chip_ident *id)
f523dd0d
GL
361{
362 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
363
aecde8b5 364 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
f523dd0d
GL
365 return -EINVAL;
366
aecde8b5 367 if (id->match.addr != mt9m001->client->addr)
f523dd0d
GL
368 return -ENODEV;
369
370 id->ident = mt9m001->model;
371 id->revision = 0;
372
373 return 0;
374}
375
376#ifdef CONFIG_VIDEO_ADV_DEBUG
377static int mt9m001_get_register(struct soc_camera_device *icd,
aecde8b5 378 struct v4l2_dbg_register *reg)
f523dd0d
GL
379{
380 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
381
aecde8b5 382 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
f523dd0d
GL
383 return -EINVAL;
384
aecde8b5 385 if (reg->match.addr != mt9m001->client->addr)
f523dd0d
GL
386 return -ENODEV;
387
aecde8b5 388 reg->size = 2;
f523dd0d
GL
389 reg->val = reg_read(icd, reg->reg);
390
391 if (reg->val > 0xffff)
392 return -EIO;
393
394 return 0;
395}
396
397static int mt9m001_set_register(struct soc_camera_device *icd,
aecde8b5 398 struct v4l2_dbg_register *reg)
f523dd0d
GL
399{
400 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
401
aecde8b5 402 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
f523dd0d
GL
403 return -EINVAL;
404
aecde8b5 405 if (reg->match.addr != mt9m001->client->addr)
f523dd0d
GL
406 return -ENODEV;
407
408 if (reg_write(icd, reg->reg, reg->val) < 0)
409 return -EIO;
410
411 return 0;
412}
413#endif
414
4407a463 415static const struct v4l2_queryctrl mt9m001_controls[] = {
f523dd0d
GL
416 {
417 .id = V4L2_CID_VFLIP,
418 .type = V4L2_CTRL_TYPE_BOOLEAN,
419 .name = "Flip Vertically",
420 .minimum = 0,
421 .maximum = 1,
422 .step = 1,
423 .default_value = 0,
424 }, {
425 .id = V4L2_CID_GAIN,
426 .type = V4L2_CTRL_TYPE_INTEGER,
427 .name = "Gain",
428 .minimum = 0,
429 .maximum = 127,
430 .step = 1,
431 .default_value = 64,
432 .flags = V4L2_CTRL_FLAG_SLIDER,
433 }, {
434 .id = V4L2_CID_EXPOSURE,
435 .type = V4L2_CTRL_TYPE_INTEGER,
436 .name = "Exposure",
437 .minimum = 1,
438 .maximum = 255,
439 .step = 1,
440 .default_value = 255,
441 .flags = V4L2_CTRL_FLAG_SLIDER,
442 }, {
443 .id = V4L2_CID_EXPOSURE_AUTO,
444 .type = V4L2_CTRL_TYPE_BOOLEAN,
445 .name = "Automatic Exposure",
446 .minimum = 0,
447 .maximum = 1,
448 .step = 1,
449 .default_value = 1,
450 }
451};
452
26f1b942
GL
453static int mt9m001_video_probe(struct soc_camera_device *);
454static void mt9m001_video_remove(struct soc_camera_device *);
455static int mt9m001_get_control(struct soc_camera_device *, struct v4l2_control *);
456static int mt9m001_set_control(struct soc_camera_device *, struct v4l2_control *);
f523dd0d
GL
457
458static struct soc_camera_ops mt9m001_ops = {
459 .owner = THIS_MODULE,
26f1b942
GL
460 .probe = mt9m001_video_probe,
461 .remove = mt9m001_video_remove,
f523dd0d
GL
462 .init = mt9m001_init,
463 .release = mt9m001_release,
464 .start_capture = mt9m001_start_capture,
465 .stop_capture = mt9m001_stop_capture,
09e231b3 466 .set_crop = mt9m001_set_crop,
d8fac217
GL
467 .set_fmt = mt9m001_set_fmt,
468 .try_fmt = mt9m001_try_fmt,
ad5f2e85
GL
469 .set_bus_param = mt9m001_set_bus_param,
470 .query_bus_param = mt9m001_query_bus_param,
f523dd0d
GL
471 .controls = mt9m001_controls,
472 .num_controls = ARRAY_SIZE(mt9m001_controls),
473 .get_control = mt9m001_get_control,
474 .set_control = mt9m001_set_control,
475 .get_chip_id = mt9m001_get_chip_id,
476#ifdef CONFIG_VIDEO_ADV_DEBUG
477 .get_register = mt9m001_get_register,
478 .set_register = mt9m001_set_register,
479#endif
480};
481
482static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
483{
484 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
485 int data;
486
487 switch (ctrl->id) {
488 case V4L2_CID_VFLIP:
489 data = reg_read(icd, MT9M001_READ_OPTIONS2);
490 if (data < 0)
491 return -EIO;
492 ctrl->value = !!(data & 0x8000);
493 break;
494 case V4L2_CID_EXPOSURE_AUTO:
495 ctrl->value = mt9m001->autoexposure;
496 break;
497 }
498 return 0;
499}
500
501static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
502{
503 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
504 const struct v4l2_queryctrl *qctrl;
505 int data;
506
507 qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
508
509 if (!qctrl)
510 return -EINVAL;
511
512 switch (ctrl->id) {
513 case V4L2_CID_VFLIP:
514 if (ctrl->value)
515 data = reg_set(icd, MT9M001_READ_OPTIONS2, 0x8000);
516 else
517 data = reg_clear(icd, MT9M001_READ_OPTIONS2, 0x8000);
518 if (data < 0)
519 return -EIO;
520 break;
521 case V4L2_CID_GAIN:
522 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
523 return -EINVAL;
524 /* See Datasheet Table 7, Gain settings. */
525 if (ctrl->value <= qctrl->default_value) {
526 /* Pack it into 0..1 step 0.125, register values 0..8 */
527 unsigned long range = qctrl->default_value - qctrl->minimum;
528 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
529
530 dev_dbg(&icd->dev, "Setting gain %d\n", data);
531 data = reg_write(icd, MT9M001_GLOBAL_GAIN, data);
532 if (data < 0)
533 return -EIO;
534 } else {
535 /* Pack it into 1.125..15 variable step, register values 9..67 */
536 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
537 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
538 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
539 111 + range / 2) / range + 9;
540
541 if (gain <= 32)
542 data = gain;
543 else if (gain <= 64)
544 data = ((gain - 32) * 16 + 16) / 32 + 80;
545 else
546 data = ((gain - 64) * 7 + 28) / 56 + 96;
547
548 dev_dbg(&icd->dev, "Setting gain from %d to %d\n",
549 reg_read(icd, MT9M001_GLOBAL_GAIN), data);
550 data = reg_write(icd, MT9M001_GLOBAL_GAIN, data);
551 if (data < 0)
552 return -EIO;
553 }
554
555 /* Success */
556 icd->gain = ctrl->value;
557 break;
558 case V4L2_CID_EXPOSURE:
559 /* mt9m001 has maximum == default */
560 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
561 return -EINVAL;
562 else {
563 unsigned long range = qctrl->maximum - qctrl->minimum;
564 unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
565 range / 2) / range + 1;
566
567 dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n",
568 reg_read(icd, MT9M001_SHUTTER_WIDTH), shutter);
569 if (reg_write(icd, MT9M001_SHUTTER_WIDTH, shutter) < 0)
570 return -EIO;
571 icd->exposure = ctrl->value;
572 mt9m001->autoexposure = 0;
573 }
574 break;
575 case V4L2_CID_EXPOSURE_AUTO:
576 if (ctrl->value) {
577 const u16 vblank = 25;
578 if (reg_write(icd, MT9M001_SHUTTER_WIDTH, icd->height +
579 icd->y_skip_top + vblank) < 0)
580 return -EIO;
581 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
582 icd->exposure = (524 + (icd->height + icd->y_skip_top + vblank - 1) *
583 (qctrl->maximum - qctrl->minimum)) /
584 1048 + qctrl->minimum;
585 mt9m001->autoexposure = 1;
586 } else
587 mt9m001->autoexposure = 0;
588 break;
589 }
590 return 0;
591}
592
593/* Interface active, can use i2c. If it fails, it can indeed mean, that
594 * this wasn't our capture interface, so, we wait for the right one */
595static int mt9m001_video_probe(struct soc_camera_device *icd)
596{
597 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
297a7ef7 598 struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
f523dd0d
GL
599 s32 data;
600 int ret;
601
602 /* We must have a parent by now. And it cannot be a wrong one.
603 * So this entire test is completely redundant. */
604 if (!icd->dev.parent ||
605 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
606 return -ENODEV;
607
608 /* Enable the chip */
a85bdace 609 data = reg_write(icd, MT9M001_CHIP_ENABLE, 1);
f523dd0d
GL
610 dev_dbg(&icd->dev, "write: %d\n", data);
611
612 /* Read out the chip version register */
613 data = reg_read(icd, MT9M001_CHIP_VERSION);
614
615 /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
616 switch (data) {
617 case 0x8411:
618 case 0x8421:
619 mt9m001->model = V4L2_IDENT_MT9M001C12ST;
26f1b942 620 icd->formats = mt9m001_colour_formats;
297a7ef7 621 if (gpio_is_valid(icl->gpio))
26f1b942 622 icd->num_formats = ARRAY_SIZE(mt9m001_colour_formats);
bb55de3b 623 else
26f1b942 624 icd->num_formats = 1;
f523dd0d
GL
625 break;
626 case 0x8431:
627 mt9m001->model = V4L2_IDENT_MT9M001C12STM;
26f1b942 628 icd->formats = mt9m001_monochrome_formats;
297a7ef7 629 if (gpio_is_valid(icl->gpio))
26f1b942 630 icd->num_formats = ARRAY_SIZE(mt9m001_monochrome_formats);
f523dd0d 631 else
26f1b942 632 icd->num_formats = 1;
f523dd0d
GL
633 break;
634 default:
635 ret = -ENODEV;
636 dev_err(&icd->dev,
637 "No MT9M001 chip detected, register read %x\n", data);
638 goto ei2c;
639 }
640
641 dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
642 data == 0x8431 ? "C12STM" : "C12ST");
643
644 /* Now that we know the model, we can start video */
645 ret = soc_camera_video_start(icd);
646 if (ret)
647 goto eisis;
648
649 return 0;
650
651eisis:
652ei2c:
653 return ret;
654}
655
656static void mt9m001_video_remove(struct soc_camera_device *icd)
657{
658 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
659
660 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m001->client->addr,
a85bdace
GL
661 icd->dev.parent, icd->vdev);
662 soc_camera_video_stop(icd);
f523dd0d
GL
663}
664
d2653e92
JD
665static int mt9m001_probe(struct i2c_client *client,
666 const struct i2c_device_id *did)
f523dd0d
GL
667{
668 struct mt9m001 *mt9m001;
669 struct soc_camera_device *icd;
670 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
671 struct soc_camera_link *icl = client->dev.platform_data;
672 int ret;
673
674 if (!icl) {
675 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
676 return -EINVAL;
677 }
678
679 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
680 dev_warn(&adapter->dev,
681 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
682 return -EIO;
683 }
684
685 mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
686 if (!mt9m001)
687 return -ENOMEM;
688
689 mt9m001->client = client;
690 i2c_set_clientdata(client, mt9m001);
691
692 /* Second stage probe - when a capture adapter is there */
693 icd = &mt9m001->icd;
f523dd0d
GL
694 icd->ops = &mt9m001_ops;
695 icd->control = &client->dev;
696 icd->x_min = 20;
697 icd->y_min = 12;
698 icd->x_current = 20;
699 icd->y_current = 12;
700 icd->width_min = 48;
701 icd->width_max = 1280;
702 icd->height_min = 32;
703 icd->height_max = 1024;
704 icd->y_skip_top = 1;
705 icd->iface = icl->bus_id;
706 /* Default datawidth - this is the only width this camera (normally)
707 * supports. It is only with extra logic that it can support
708 * other widths. Therefore it seems to be a sensible default. */
709 mt9m001->datawidth = 10;
710 /* Simulated autoexposure. If enabled, we calculate shutter width
711 * ourselves in the driver based on vertical blanking and frame width */
712 mt9m001->autoexposure = 1;
713
714 ret = bus_switch_request(mt9m001, icl);
715 if (ret)
716 goto eswinit;
717
718 ret = soc_camera_device_register(icd);
719 if (ret)
720 goto eisdr;
721
722 return 0;
723
724eisdr:
725 bus_switch_release(mt9m001);
726eswinit:
727 kfree(mt9m001);
728 return ret;
729}
730
731static int mt9m001_remove(struct i2c_client *client)
732{
733 struct mt9m001 *mt9m001 = i2c_get_clientdata(client);
734
735 soc_camera_device_unregister(&mt9m001->icd);
736 bus_switch_release(mt9m001);
737 kfree(mt9m001);
738
739 return 0;
740}
741
3760f736
JD
742static const struct i2c_device_id mt9m001_id[] = {
743 { "mt9m001", 0 },
744 { }
745};
746MODULE_DEVICE_TABLE(i2c, mt9m001_id);
747
f523dd0d
GL
748static struct i2c_driver mt9m001_i2c_driver = {
749 .driver = {
750 .name = "mt9m001",
751 },
752 .probe = mt9m001_probe,
753 .remove = mt9m001_remove,
3760f736 754 .id_table = mt9m001_id,
f523dd0d
GL
755};
756
757static int __init mt9m001_mod_init(void)
758{
759 return i2c_add_driver(&mt9m001_i2c_driver);
760}
761
762static void __exit mt9m001_mod_exit(void)
763{
764 i2c_del_driver(&mt9m001_i2c_driver);
765}
766
767module_init(mt9m001_mod_init);
768module_exit(mt9m001_mod_exit);
769
770MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
771MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
772MODULE_LICENSE("GPL");