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