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