perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / i2c / soc_camera / mt9m001.c
blob1bfb0d53059ee803c4499d6bfea58cf652550586
1 /*
2 * Driver for MT9M001 CMOS Image Sensor from Micron
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
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 */
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/module.h>
17 #include <media/soc_camera.h>
18 #include <media/drv-intf/soc_mediabus.h>
19 #include <media/v4l2-clk.h>
20 #include <media/v4l2-subdev.h>
21 #include <media/v4l2-ctrls.h>
24 * mt9m001 i2c address 0x5d
25 * The platform has to define struct i2c_board_info objects and link to them
26 * from struct soc_camera_host_desc
29 /* mt9m001 selected register addresses */
30 #define MT9M001_CHIP_VERSION 0x00
31 #define MT9M001_ROW_START 0x01
32 #define MT9M001_COLUMN_START 0x02
33 #define MT9M001_WINDOW_HEIGHT 0x03
34 #define MT9M001_WINDOW_WIDTH 0x04
35 #define MT9M001_HORIZONTAL_BLANKING 0x05
36 #define MT9M001_VERTICAL_BLANKING 0x06
37 #define MT9M001_OUTPUT_CONTROL 0x07
38 #define MT9M001_SHUTTER_WIDTH 0x09
39 #define MT9M001_FRAME_RESTART 0x0b
40 #define MT9M001_SHUTTER_DELAY 0x0c
41 #define MT9M001_RESET 0x0d
42 #define MT9M001_READ_OPTIONS1 0x1e
43 #define MT9M001_READ_OPTIONS2 0x20
44 #define MT9M001_GLOBAL_GAIN 0x35
45 #define MT9M001_CHIP_ENABLE 0xF1
47 #define MT9M001_MAX_WIDTH 1280
48 #define MT9M001_MAX_HEIGHT 1024
49 #define MT9M001_MIN_WIDTH 48
50 #define MT9M001_MIN_HEIGHT 32
51 #define MT9M001_COLUMN_SKIP 20
52 #define MT9M001_ROW_SKIP 12
54 /* MT9M001 has only one fixed colorspace per pixelcode */
55 struct mt9m001_datafmt {
56 u32 code;
57 enum v4l2_colorspace colorspace;
60 /* Find a data format by a pixel code in an array */
61 static const struct mt9m001_datafmt *mt9m001_find_datafmt(
62 u32 code, const struct mt9m001_datafmt *fmt,
63 int n)
65 int i;
66 for (i = 0; i < n; i++)
67 if (fmt[i].code == code)
68 return fmt + i;
70 return NULL;
73 static const struct mt9m001_datafmt mt9m001_colour_fmts[] = {
75 * Order important: first natively supported,
76 * second supported with a GPIO extender
78 {MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
79 {MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
82 static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = {
83 /* Order important - see above */
84 {MEDIA_BUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
85 {MEDIA_BUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
88 struct mt9m001 {
89 struct v4l2_subdev subdev;
90 struct v4l2_ctrl_handler hdl;
91 struct {
92 /* exposure/auto-exposure cluster */
93 struct v4l2_ctrl *autoexposure;
94 struct v4l2_ctrl *exposure;
96 struct v4l2_rect rect; /* Sensor window */
97 struct v4l2_clk *clk;
98 const struct mt9m001_datafmt *fmt;
99 const struct mt9m001_datafmt *fmts;
100 int num_fmts;
101 unsigned int total_h;
102 unsigned short y_skip_top; /* Lines to skip at the top */
105 static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
107 return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
110 static int reg_read(struct i2c_client *client, const u8 reg)
112 return i2c_smbus_read_word_swapped(client, reg);
115 static int reg_write(struct i2c_client *client, const u8 reg,
116 const u16 data)
118 return i2c_smbus_write_word_swapped(client, reg, data);
121 static int reg_set(struct i2c_client *client, const u8 reg,
122 const u16 data)
124 int ret;
126 ret = reg_read(client, reg);
127 if (ret < 0)
128 return ret;
129 return reg_write(client, reg, ret | data);
132 static int reg_clear(struct i2c_client *client, const u8 reg,
133 const u16 data)
135 int ret;
137 ret = reg_read(client, reg);
138 if (ret < 0)
139 return ret;
140 return reg_write(client, reg, ret & ~data);
143 static int mt9m001_init(struct i2c_client *client)
145 int ret;
147 dev_dbg(&client->dev, "%s\n", __func__);
150 * We don't know, whether platform provides reset, issue a soft reset
151 * too. This returns all registers to their default values.
153 ret = reg_write(client, MT9M001_RESET, 1);
154 if (!ret)
155 ret = reg_write(client, MT9M001_RESET, 0);
157 /* Disable chip, synchronous option update */
158 if (!ret)
159 ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
161 return ret;
164 static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
166 struct i2c_client *client = v4l2_get_subdevdata(sd);
168 /* Switch to master "normal" mode or stop sensor readout */
169 if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0)
170 return -EIO;
171 return 0;
174 static int mt9m001_set_selection(struct v4l2_subdev *sd,
175 struct v4l2_subdev_pad_config *cfg,
176 struct v4l2_subdev_selection *sel)
178 struct i2c_client *client = v4l2_get_subdevdata(sd);
179 struct mt9m001 *mt9m001 = to_mt9m001(client);
180 struct v4l2_rect rect = sel->r;
181 const u16 hblank = 9, vblank = 25;
182 int ret;
184 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
185 sel->target != V4L2_SEL_TGT_CROP)
186 return -EINVAL;
188 if (mt9m001->fmts == mt9m001_colour_fmts)
190 * Bayer format - even number of rows for simplicity,
191 * but let the user play with the top row.
193 rect.height = ALIGN(rect.height, 2);
195 /* Datasheet requirement: see register description */
196 rect.width = ALIGN(rect.width, 2);
197 rect.left = ALIGN(rect.left, 2);
199 soc_camera_limit_side(&rect.left, &rect.width,
200 MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH);
202 soc_camera_limit_side(&rect.top, &rect.height,
203 MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT);
205 mt9m001->total_h = rect.height + mt9m001->y_skip_top + vblank;
207 /* Blanking and start values - default... */
208 ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
209 if (!ret)
210 ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
213 * The caller provides a supported format, as verified per
214 * call to .set_fmt(FORMAT_TRY).
216 if (!ret)
217 ret = reg_write(client, MT9M001_COLUMN_START, rect.left);
218 if (!ret)
219 ret = reg_write(client, MT9M001_ROW_START, rect.top);
220 if (!ret)
221 ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1);
222 if (!ret)
223 ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
224 rect.height + mt9m001->y_skip_top - 1);
225 if (!ret && v4l2_ctrl_g_ctrl(mt9m001->autoexposure) == V4L2_EXPOSURE_AUTO)
226 ret = reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h);
228 if (!ret)
229 mt9m001->rect = rect;
231 return ret;
234 static int mt9m001_get_selection(struct v4l2_subdev *sd,
235 struct v4l2_subdev_pad_config *cfg,
236 struct v4l2_subdev_selection *sel)
238 struct i2c_client *client = v4l2_get_subdevdata(sd);
239 struct mt9m001 *mt9m001 = to_mt9m001(client);
241 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
242 return -EINVAL;
244 switch (sel->target) {
245 case V4L2_SEL_TGT_CROP_BOUNDS:
246 case V4L2_SEL_TGT_CROP_DEFAULT:
247 sel->r.left = MT9M001_COLUMN_SKIP;
248 sel->r.top = MT9M001_ROW_SKIP;
249 sel->r.width = MT9M001_MAX_WIDTH;
250 sel->r.height = MT9M001_MAX_HEIGHT;
251 return 0;
252 case V4L2_SEL_TGT_CROP:
253 sel->r = mt9m001->rect;
254 return 0;
255 default:
256 return -EINVAL;
260 static int mt9m001_get_fmt(struct v4l2_subdev *sd,
261 struct v4l2_subdev_pad_config *cfg,
262 struct v4l2_subdev_format *format)
264 struct i2c_client *client = v4l2_get_subdevdata(sd);
265 struct mt9m001 *mt9m001 = to_mt9m001(client);
266 struct v4l2_mbus_framefmt *mf = &format->format;
268 if (format->pad)
269 return -EINVAL;
271 mf->width = mt9m001->rect.width;
272 mf->height = mt9m001->rect.height;
273 mf->code = mt9m001->fmt->code;
274 mf->colorspace = mt9m001->fmt->colorspace;
275 mf->field = V4L2_FIELD_NONE;
277 return 0;
280 static int mt9m001_s_fmt(struct v4l2_subdev *sd,
281 const struct mt9m001_datafmt *fmt,
282 struct v4l2_mbus_framefmt *mf)
284 struct i2c_client *client = v4l2_get_subdevdata(sd);
285 struct mt9m001 *mt9m001 = to_mt9m001(client);
286 struct v4l2_subdev_selection sel = {
287 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
288 .target = V4L2_SEL_TGT_CROP,
289 .r.left = mt9m001->rect.left,
290 .r.top = mt9m001->rect.top,
291 .r.width = mf->width,
292 .r.height = mf->height,
294 int ret;
296 /* No support for scaling so far, just crop. TODO: use skipping */
297 ret = mt9m001_set_selection(sd, NULL, &sel);
298 if (!ret) {
299 mf->width = mt9m001->rect.width;
300 mf->height = mt9m001->rect.height;
301 mt9m001->fmt = fmt;
302 mf->colorspace = fmt->colorspace;
305 return ret;
308 static int mt9m001_set_fmt(struct v4l2_subdev *sd,
309 struct v4l2_subdev_pad_config *cfg,
310 struct v4l2_subdev_format *format)
312 struct v4l2_mbus_framefmt *mf = &format->format;
313 struct i2c_client *client = v4l2_get_subdevdata(sd);
314 struct mt9m001 *mt9m001 = to_mt9m001(client);
315 const struct mt9m001_datafmt *fmt;
317 if (format->pad)
318 return -EINVAL;
320 v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH,
321 MT9M001_MAX_WIDTH, 1,
322 &mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top,
323 MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0);
325 if (mt9m001->fmts == mt9m001_colour_fmts)
326 mf->height = ALIGN(mf->height - 1, 2);
328 fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts,
329 mt9m001->num_fmts);
330 if (!fmt) {
331 fmt = mt9m001->fmt;
332 mf->code = fmt->code;
335 mf->colorspace = fmt->colorspace;
337 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
338 return mt9m001_s_fmt(sd, fmt, mf);
339 cfg->try_fmt = *mf;
340 return 0;
343 #ifdef CONFIG_VIDEO_ADV_DEBUG
344 static int mt9m001_g_register(struct v4l2_subdev *sd,
345 struct v4l2_dbg_register *reg)
347 struct i2c_client *client = v4l2_get_subdevdata(sd);
349 if (reg->reg > 0xff)
350 return -EINVAL;
352 reg->size = 2;
353 reg->val = reg_read(client, reg->reg);
355 if (reg->val > 0xffff)
356 return -EIO;
358 return 0;
361 static int mt9m001_s_register(struct v4l2_subdev *sd,
362 const struct v4l2_dbg_register *reg)
364 struct i2c_client *client = v4l2_get_subdevdata(sd);
366 if (reg->reg > 0xff)
367 return -EINVAL;
369 if (reg_write(client, reg->reg, reg->val) < 0)
370 return -EIO;
372 return 0;
374 #endif
376 static int mt9m001_s_power(struct v4l2_subdev *sd, int on)
378 struct i2c_client *client = v4l2_get_subdevdata(sd);
379 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
380 struct mt9m001 *mt9m001 = to_mt9m001(client);
382 return soc_camera_set_power(&client->dev, ssdd, mt9m001->clk, on);
385 static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
387 struct mt9m001 *mt9m001 = container_of(ctrl->handler,
388 struct mt9m001, hdl);
389 s32 min, max;
391 switch (ctrl->id) {
392 case V4L2_CID_EXPOSURE_AUTO:
393 min = mt9m001->exposure->minimum;
394 max = mt9m001->exposure->maximum;
395 mt9m001->exposure->val =
396 (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
397 break;
399 return 0;
402 static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl)
404 struct mt9m001 *mt9m001 = container_of(ctrl->handler,
405 struct mt9m001, hdl);
406 struct v4l2_subdev *sd = &mt9m001->subdev;
407 struct i2c_client *client = v4l2_get_subdevdata(sd);
408 struct v4l2_ctrl *exp = mt9m001->exposure;
409 int data;
411 switch (ctrl->id) {
412 case V4L2_CID_VFLIP:
413 if (ctrl->val)
414 data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
415 else
416 data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
417 if (data < 0)
418 return -EIO;
419 return 0;
421 case V4L2_CID_GAIN:
422 /* See Datasheet Table 7, Gain settings. */
423 if (ctrl->val <= ctrl->default_value) {
424 /* Pack it into 0..1 step 0.125, register values 0..8 */
425 unsigned long range = ctrl->default_value - ctrl->minimum;
426 data = ((ctrl->val - (s32)ctrl->minimum) * 8 + range / 2) / range;
428 dev_dbg(&client->dev, "Setting gain %d\n", data);
429 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
430 if (data < 0)
431 return -EIO;
432 } else {
433 /* Pack it into 1.125..15 variable step, register values 9..67 */
434 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
435 unsigned long range = ctrl->maximum - ctrl->default_value - 1;
436 unsigned long gain = ((ctrl->val - (s32)ctrl->default_value - 1) *
437 111 + range / 2) / range + 9;
439 if (gain <= 32)
440 data = gain;
441 else if (gain <= 64)
442 data = ((gain - 32) * 16 + 16) / 32 + 80;
443 else
444 data = ((gain - 64) * 7 + 28) / 56 + 96;
446 dev_dbg(&client->dev, "Setting gain from %d to %d\n",
447 reg_read(client, MT9M001_GLOBAL_GAIN), data);
448 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
449 if (data < 0)
450 return -EIO;
452 return 0;
454 case V4L2_CID_EXPOSURE_AUTO:
455 if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
456 unsigned long range = exp->maximum - exp->minimum;
457 unsigned long shutter = ((exp->val - (s32)exp->minimum) * 1048 +
458 range / 2) / range + 1;
460 dev_dbg(&client->dev,
461 "Setting shutter width from %d to %lu\n",
462 reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
463 if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
464 return -EIO;
465 } else {
466 const u16 vblank = 25;
468 mt9m001->total_h = mt9m001->rect.height +
469 mt9m001->y_skip_top + vblank;
470 if (reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h) < 0)
471 return -EIO;
473 return 0;
475 return -EINVAL;
479 * Interface active, can use i2c. If it fails, it can indeed mean, that
480 * this wasn't our capture interface, so, we wait for the right one
482 static int mt9m001_video_probe(struct soc_camera_subdev_desc *ssdd,
483 struct i2c_client *client)
485 struct mt9m001 *mt9m001 = to_mt9m001(client);
486 s32 data;
487 unsigned long flags;
488 int ret;
490 ret = mt9m001_s_power(&mt9m001->subdev, 1);
491 if (ret < 0)
492 return ret;
494 /* Enable the chip */
495 data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
496 dev_dbg(&client->dev, "write: %d\n", data);
498 /* Read out the chip version register */
499 data = reg_read(client, MT9M001_CHIP_VERSION);
501 /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
502 switch (data) {
503 case 0x8411:
504 case 0x8421:
505 mt9m001->fmts = mt9m001_colour_fmts;
506 break;
507 case 0x8431:
508 mt9m001->fmts = mt9m001_monochrome_fmts;
509 break;
510 default:
511 dev_err(&client->dev,
512 "No MT9M001 chip detected, register read %x\n", data);
513 ret = -ENODEV;
514 goto done;
517 mt9m001->num_fmts = 0;
520 * This is a 10bit sensor, so by default we only allow 10bit.
521 * The platform may support different bus widths due to
522 * different routing of the data lines.
524 if (ssdd->query_bus_param)
525 flags = ssdd->query_bus_param(ssdd);
526 else
527 flags = SOCAM_DATAWIDTH_10;
529 if (flags & SOCAM_DATAWIDTH_10)
530 mt9m001->num_fmts++;
531 else
532 mt9m001->fmts++;
534 if (flags & SOCAM_DATAWIDTH_8)
535 mt9m001->num_fmts++;
537 mt9m001->fmt = &mt9m001->fmts[0];
539 dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
540 data == 0x8431 ? "C12STM" : "C12ST");
542 ret = mt9m001_init(client);
543 if (ret < 0) {
544 dev_err(&client->dev, "Failed to initialise the camera\n");
545 goto done;
548 /* mt9m001_init() has reset the chip, returning registers to defaults */
549 ret = v4l2_ctrl_handler_setup(&mt9m001->hdl);
551 done:
552 mt9m001_s_power(&mt9m001->subdev, 0);
553 return ret;
556 static void mt9m001_video_remove(struct soc_camera_subdev_desc *ssdd)
558 if (ssdd->free_bus)
559 ssdd->free_bus(ssdd);
562 static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
564 struct i2c_client *client = v4l2_get_subdevdata(sd);
565 struct mt9m001 *mt9m001 = to_mt9m001(client);
567 *lines = mt9m001->y_skip_top;
569 return 0;
572 static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = {
573 .g_volatile_ctrl = mt9m001_g_volatile_ctrl,
574 .s_ctrl = mt9m001_s_ctrl,
577 static const struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
578 #ifdef CONFIG_VIDEO_ADV_DEBUG
579 .g_register = mt9m001_g_register,
580 .s_register = mt9m001_s_register,
581 #endif
582 .s_power = mt9m001_s_power,
585 static int mt9m001_enum_mbus_code(struct v4l2_subdev *sd,
586 struct v4l2_subdev_pad_config *cfg,
587 struct v4l2_subdev_mbus_code_enum *code)
589 struct i2c_client *client = v4l2_get_subdevdata(sd);
590 struct mt9m001 *mt9m001 = to_mt9m001(client);
592 if (code->pad || code->index >= mt9m001->num_fmts)
593 return -EINVAL;
595 code->code = mt9m001->fmts[code->index].code;
596 return 0;
599 static int mt9m001_g_mbus_config(struct v4l2_subdev *sd,
600 struct v4l2_mbus_config *cfg)
602 struct i2c_client *client = v4l2_get_subdevdata(sd);
603 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
605 /* MT9M001 has all capture_format parameters fixed */
606 cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
607 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
608 V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER;
609 cfg->type = V4L2_MBUS_PARALLEL;
610 cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
612 return 0;
615 static int mt9m001_s_mbus_config(struct v4l2_subdev *sd,
616 const struct v4l2_mbus_config *cfg)
618 const struct i2c_client *client = v4l2_get_subdevdata(sd);
619 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
620 struct mt9m001 *mt9m001 = to_mt9m001(client);
621 unsigned int bps = soc_mbus_get_fmtdesc(mt9m001->fmt->code)->bits_per_sample;
623 if (ssdd->set_bus_param)
624 return ssdd->set_bus_param(ssdd, 1 << (bps - 1));
627 * Without board specific bus width settings we only support the
628 * sensors native bus width
630 return bps == 10 ? 0 : -EINVAL;
633 static const struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
634 .s_stream = mt9m001_s_stream,
635 .g_mbus_config = mt9m001_g_mbus_config,
636 .s_mbus_config = mt9m001_s_mbus_config,
639 static const struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
640 .g_skip_top_lines = mt9m001_g_skip_top_lines,
643 static const struct v4l2_subdev_pad_ops mt9m001_subdev_pad_ops = {
644 .enum_mbus_code = mt9m001_enum_mbus_code,
645 .get_selection = mt9m001_get_selection,
646 .set_selection = mt9m001_set_selection,
647 .get_fmt = mt9m001_get_fmt,
648 .set_fmt = mt9m001_set_fmt,
651 static const struct v4l2_subdev_ops mt9m001_subdev_ops = {
652 .core = &mt9m001_subdev_core_ops,
653 .video = &mt9m001_subdev_video_ops,
654 .sensor = &mt9m001_subdev_sensor_ops,
655 .pad = &mt9m001_subdev_pad_ops,
658 static int mt9m001_probe(struct i2c_client *client,
659 const struct i2c_device_id *did)
661 struct mt9m001 *mt9m001;
662 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
663 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
664 int ret;
666 if (!ssdd) {
667 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
668 return -EINVAL;
671 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
672 dev_warn(&adapter->dev,
673 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
674 return -EIO;
677 mt9m001 = devm_kzalloc(&client->dev, sizeof(struct mt9m001), GFP_KERNEL);
678 if (!mt9m001)
679 return -ENOMEM;
681 v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
682 v4l2_ctrl_handler_init(&mt9m001->hdl, 4);
683 v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
684 V4L2_CID_VFLIP, 0, 1, 1, 0);
685 v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
686 V4L2_CID_GAIN, 0, 127, 1, 64);
687 mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
688 V4L2_CID_EXPOSURE, 1, 255, 1, 255);
690 * Simulated autoexposure. If enabled, we calculate shutter width
691 * ourselves in the driver based on vertical blanking and frame width
693 mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl,
694 &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
695 V4L2_EXPOSURE_AUTO);
696 mt9m001->subdev.ctrl_handler = &mt9m001->hdl;
697 if (mt9m001->hdl.error)
698 return mt9m001->hdl.error;
700 v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
701 V4L2_EXPOSURE_MANUAL, true);
703 /* Second stage probe - when a capture adapter is there */
704 mt9m001->y_skip_top = 0;
705 mt9m001->rect.left = MT9M001_COLUMN_SKIP;
706 mt9m001->rect.top = MT9M001_ROW_SKIP;
707 mt9m001->rect.width = MT9M001_MAX_WIDTH;
708 mt9m001->rect.height = MT9M001_MAX_HEIGHT;
710 mt9m001->clk = v4l2_clk_get(&client->dev, "mclk");
711 if (IS_ERR(mt9m001->clk)) {
712 ret = PTR_ERR(mt9m001->clk);
713 goto eclkget;
716 ret = mt9m001_video_probe(ssdd, client);
717 if (ret) {
718 v4l2_clk_put(mt9m001->clk);
719 eclkget:
720 v4l2_ctrl_handler_free(&mt9m001->hdl);
723 return ret;
726 static int mt9m001_remove(struct i2c_client *client)
728 struct mt9m001 *mt9m001 = to_mt9m001(client);
729 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
731 v4l2_clk_put(mt9m001->clk);
732 v4l2_device_unregister_subdev(&mt9m001->subdev);
733 v4l2_ctrl_handler_free(&mt9m001->hdl);
734 mt9m001_video_remove(ssdd);
736 return 0;
739 static const struct i2c_device_id mt9m001_id[] = {
740 { "mt9m001", 0 },
743 MODULE_DEVICE_TABLE(i2c, mt9m001_id);
745 static struct i2c_driver mt9m001_i2c_driver = {
746 .driver = {
747 .name = "mt9m001",
749 .probe = mt9m001_probe,
750 .remove = mt9m001_remove,
751 .id_table = mt9m001_id,
754 module_i2c_driver(mt9m001_i2c_driver);
756 MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
757 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
758 MODULE_LICENSE("GPL");