2 * Driver for MT9P031 CMOS Image Sensor from Aptina
4 * Copyright (C) 2011, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
5 * Copyright (C) 2011, Javier Martin <javier.martin@vista-silicon.com>
6 * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
8 * Based on the MT9V032 driver and Bastian Hecht's code.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/gpio.h>
19 #include <linux/i2c.h>
20 #include <linux/log2.h>
21 #include <linux/module.h>
22 #include <linux/of_gpio.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
28 #include <media/mt9p031.h>
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-of.h>
32 #include <media/v4l2-subdev.h>
34 #include "aptina-pll.h"
36 #define MT9P031_PIXEL_ARRAY_WIDTH 2752
37 #define MT9P031_PIXEL_ARRAY_HEIGHT 2004
39 #define MT9P031_CHIP_VERSION 0x00
40 #define MT9P031_CHIP_VERSION_VALUE 0x1801
41 #define MT9P031_ROW_START 0x01
42 #define MT9P031_ROW_START_MIN 0
43 #define MT9P031_ROW_START_MAX 2004
44 #define MT9P031_ROW_START_DEF 54
45 #define MT9P031_COLUMN_START 0x02
46 #define MT9P031_COLUMN_START_MIN 0
47 #define MT9P031_COLUMN_START_MAX 2750
48 #define MT9P031_COLUMN_START_DEF 16
49 #define MT9P031_WINDOW_HEIGHT 0x03
50 #define MT9P031_WINDOW_HEIGHT_MIN 2
51 #define MT9P031_WINDOW_HEIGHT_MAX 2006
52 #define MT9P031_WINDOW_HEIGHT_DEF 1944
53 #define MT9P031_WINDOW_WIDTH 0x04
54 #define MT9P031_WINDOW_WIDTH_MIN 2
55 #define MT9P031_WINDOW_WIDTH_MAX 2752
56 #define MT9P031_WINDOW_WIDTH_DEF 2592
57 #define MT9P031_HORIZONTAL_BLANK 0x05
58 #define MT9P031_HORIZONTAL_BLANK_MIN 0
59 #define MT9P031_HORIZONTAL_BLANK_MAX 4095
60 #define MT9P031_VERTICAL_BLANK 0x06
61 #define MT9P031_VERTICAL_BLANK_MIN 1
62 #define MT9P031_VERTICAL_BLANK_MAX 4096
63 #define MT9P031_VERTICAL_BLANK_DEF 26
64 #define MT9P031_OUTPUT_CONTROL 0x07
65 #define MT9P031_OUTPUT_CONTROL_CEN 2
66 #define MT9P031_OUTPUT_CONTROL_SYN 1
67 #define MT9P031_OUTPUT_CONTROL_DEF 0x1f82
68 #define MT9P031_SHUTTER_WIDTH_UPPER 0x08
69 #define MT9P031_SHUTTER_WIDTH_LOWER 0x09
70 #define MT9P031_SHUTTER_WIDTH_MIN 1
71 #define MT9P031_SHUTTER_WIDTH_MAX 1048575
72 #define MT9P031_SHUTTER_WIDTH_DEF 1943
73 #define MT9P031_PLL_CONTROL 0x10
74 #define MT9P031_PLL_CONTROL_PWROFF 0x0050
75 #define MT9P031_PLL_CONTROL_PWRON 0x0051
76 #define MT9P031_PLL_CONTROL_USEPLL 0x0052
77 #define MT9P031_PLL_CONFIG_1 0x11
78 #define MT9P031_PLL_CONFIG_2 0x12
79 #define MT9P031_PIXEL_CLOCK_CONTROL 0x0a
80 #define MT9P031_FRAME_RESTART 0x0b
81 #define MT9P031_SHUTTER_DELAY 0x0c
82 #define MT9P031_RST 0x0d
83 #define MT9P031_RST_ENABLE 1
84 #define MT9P031_RST_DISABLE 0
85 #define MT9P031_READ_MODE_1 0x1e
86 #define MT9P031_READ_MODE_2 0x20
87 #define MT9P031_READ_MODE_2_ROW_MIR (1 << 15)
88 #define MT9P031_READ_MODE_2_COL_MIR (1 << 14)
89 #define MT9P031_READ_MODE_2_ROW_BLC (1 << 6)
90 #define MT9P031_ROW_ADDRESS_MODE 0x22
91 #define MT9P031_COLUMN_ADDRESS_MODE 0x23
92 #define MT9P031_GLOBAL_GAIN 0x35
93 #define MT9P031_GLOBAL_GAIN_MIN 8
94 #define MT9P031_GLOBAL_GAIN_MAX 1024
95 #define MT9P031_GLOBAL_GAIN_DEF 8
96 #define MT9P031_GLOBAL_GAIN_MULT (1 << 6)
97 #define MT9P031_ROW_BLACK_TARGET 0x49
98 #define MT9P031_ROW_BLACK_DEF_OFFSET 0x4b
99 #define MT9P031_GREEN1_OFFSET 0x60
100 #define MT9P031_GREEN2_OFFSET 0x61
101 #define MT9P031_BLACK_LEVEL_CALIBRATION 0x62
102 #define MT9P031_BLC_MANUAL_BLC (1 << 0)
103 #define MT9P031_RED_OFFSET 0x63
104 #define MT9P031_BLUE_OFFSET 0x64
105 #define MT9P031_TEST_PATTERN 0xa0
106 #define MT9P031_TEST_PATTERN_SHIFT 3
107 #define MT9P031_TEST_PATTERN_ENABLE (1 << 0)
108 #define MT9P031_TEST_PATTERN_DISABLE (0 << 0)
109 #define MT9P031_TEST_PATTERN_GREEN 0xa1
110 #define MT9P031_TEST_PATTERN_RED 0xa2
111 #define MT9P031_TEST_PATTERN_BLUE 0xa3
115 MT9P031_MODEL_MONOCHROME,
119 struct v4l2_subdev subdev;
120 struct media_pad pad;
121 struct v4l2_rect crop; /* Sensor window */
122 struct v4l2_mbus_framefmt format;
123 struct mt9p031_platform_data *pdata;
124 struct mutex power_lock; /* lock to protect power_count */
128 struct regulator_bulk_data regulators[3];
130 enum mt9p031_model model;
131 struct aptina_pll pll;
134 struct v4l2_ctrl_handler ctrls;
135 struct v4l2_ctrl *blc_auto;
136 struct v4l2_ctrl *blc_offset;
138 /* Registers cache */
143 static struct mt9p031 *to_mt9p031(struct v4l2_subdev *sd)
145 return container_of(sd, struct mt9p031, subdev);
148 static int mt9p031_read(struct i2c_client *client, u8 reg)
150 return i2c_smbus_read_word_swapped(client, reg);
153 static int mt9p031_write(struct i2c_client *client, u8 reg, u16 data)
155 return i2c_smbus_write_word_swapped(client, reg, data);
158 static int mt9p031_set_output_control(struct mt9p031 *mt9p031, u16 clear,
161 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
162 u16 value = (mt9p031->output_control & ~clear) | set;
165 ret = mt9p031_write(client, MT9P031_OUTPUT_CONTROL, value);
169 mt9p031->output_control = value;
173 static int mt9p031_set_mode2(struct mt9p031 *mt9p031, u16 clear, u16 set)
175 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
176 u16 value = (mt9p031->mode2 & ~clear) | set;
179 ret = mt9p031_write(client, MT9P031_READ_MODE_2, value);
183 mt9p031->mode2 = value;
187 static int mt9p031_reset(struct mt9p031 *mt9p031)
189 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
192 /* Disable chip output, synchronous option update */
193 ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_ENABLE);
196 ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_DISABLE);
200 return mt9p031_set_output_control(mt9p031, MT9P031_OUTPUT_CONTROL_CEN,
204 static int mt9p031_clk_setup(struct mt9p031 *mt9p031)
206 static const struct aptina_pll_limits limits = {
207 .ext_clock_min = 6000000,
208 .ext_clock_max = 27000000,
209 .int_clock_min = 2000000,
210 .int_clock_max = 13500000,
211 .out_clock_min = 180000000,
212 .out_clock_max = 360000000,
213 .pix_clock_max = 96000000,
222 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
223 struct mt9p031_platform_data *pdata = mt9p031->pdata;
225 mt9p031->clk = devm_clk_get(&client->dev, NULL);
226 if (IS_ERR(mt9p031->clk))
227 return PTR_ERR(mt9p031->clk);
229 clk_set_rate(mt9p031->clk, pdata->ext_freq);
231 mt9p031->pll.ext_clock = pdata->ext_freq;
232 mt9p031->pll.pix_clock = pdata->target_freq;
234 return aptina_pll_calculate(&client->dev, &limits, &mt9p031->pll);
237 static int mt9p031_pll_enable(struct mt9p031 *mt9p031)
239 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
242 ret = mt9p031_write(client, MT9P031_PLL_CONTROL,
243 MT9P031_PLL_CONTROL_PWRON);
247 ret = mt9p031_write(client, MT9P031_PLL_CONFIG_1,
248 (mt9p031->pll.m << 8) | (mt9p031->pll.n - 1));
252 ret = mt9p031_write(client, MT9P031_PLL_CONFIG_2, mt9p031->pll.p1 - 1);
256 usleep_range(1000, 2000);
257 ret = mt9p031_write(client, MT9P031_PLL_CONTROL,
258 MT9P031_PLL_CONTROL_PWRON |
259 MT9P031_PLL_CONTROL_USEPLL);
263 static inline int mt9p031_pll_disable(struct mt9p031 *mt9p031)
265 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
267 return mt9p031_write(client, MT9P031_PLL_CONTROL,
268 MT9P031_PLL_CONTROL_PWROFF);
271 static int mt9p031_power_on(struct mt9p031 *mt9p031)
275 /* Ensure RESET_BAR is low */
276 if (gpio_is_valid(mt9p031->reset)) {
277 gpio_set_value(mt9p031->reset, 0);
278 usleep_range(1000, 2000);
281 /* Bring up the supplies */
282 ret = regulator_bulk_enable(ARRAY_SIZE(mt9p031->regulators),
283 mt9p031->regulators);
289 clk_prepare_enable(mt9p031->clk);
291 /* Now RESET_BAR must be high */
292 if (gpio_is_valid(mt9p031->reset)) {
293 gpio_set_value(mt9p031->reset, 1);
294 usleep_range(1000, 2000);
300 static void mt9p031_power_off(struct mt9p031 *mt9p031)
302 if (gpio_is_valid(mt9p031->reset)) {
303 gpio_set_value(mt9p031->reset, 0);
304 usleep_range(1000, 2000);
307 regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators),
308 mt9p031->regulators);
311 clk_disable_unprepare(mt9p031->clk);
314 static int __mt9p031_set_power(struct mt9p031 *mt9p031, bool on)
316 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
320 mt9p031_power_off(mt9p031);
324 ret = mt9p031_power_on(mt9p031);
328 ret = mt9p031_reset(mt9p031);
330 dev_err(&client->dev, "Failed to reset the camera\n");
334 return v4l2_ctrl_handler_setup(&mt9p031->ctrls);
337 /* -----------------------------------------------------------------------------
338 * V4L2 subdev video operations
341 static int mt9p031_set_params(struct mt9p031 *mt9p031)
343 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
344 struct v4l2_mbus_framefmt *format = &mt9p031->format;
345 const struct v4l2_rect *crop = &mt9p031->crop;
354 /* Windows position and size.
356 * TODO: Make sure the start coordinates and window size match the
357 * skipping, binning and mirroring (see description of registers 2 and 4
358 * in table 13, and Binning section on page 41).
360 ret = mt9p031_write(client, MT9P031_COLUMN_START, crop->left);
363 ret = mt9p031_write(client, MT9P031_ROW_START, crop->top);
366 ret = mt9p031_write(client, MT9P031_WINDOW_WIDTH, crop->width - 1);
369 ret = mt9p031_write(client, MT9P031_WINDOW_HEIGHT, crop->height - 1);
373 /* Row and column binning and skipping. Use the maximum binning value
374 * compatible with the skipping settings.
376 xskip = DIV_ROUND_CLOSEST(crop->width, format->width);
377 yskip = DIV_ROUND_CLOSEST(crop->height, format->height);
378 xbin = 1 << (ffs(xskip) - 1);
379 ybin = 1 << (ffs(yskip) - 1);
381 ret = mt9p031_write(client, MT9P031_COLUMN_ADDRESS_MODE,
382 ((xbin - 1) << 4) | (xskip - 1));
385 ret = mt9p031_write(client, MT9P031_ROW_ADDRESS_MODE,
386 ((ybin - 1) << 4) | (yskip - 1));
390 /* Blanking - use minimum value for horizontal blanking and default
391 * value for vertical blanking.
393 hblank = 346 * ybin + 64 + (80 >> min_t(unsigned int, xbin, 3));
394 vblank = MT9P031_VERTICAL_BLANK_DEF;
396 ret = mt9p031_write(client, MT9P031_HORIZONTAL_BLANK, hblank - 1);
399 ret = mt9p031_write(client, MT9P031_VERTICAL_BLANK, vblank - 1);
406 static int mt9p031_s_stream(struct v4l2_subdev *subdev, int enable)
408 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
412 /* Stop sensor readout */
413 ret = mt9p031_set_output_control(mt9p031,
414 MT9P031_OUTPUT_CONTROL_CEN, 0);
418 return mt9p031_pll_disable(mt9p031);
421 ret = mt9p031_set_params(mt9p031);
425 /* Switch to master "normal" mode */
426 ret = mt9p031_set_output_control(mt9p031, 0,
427 MT9P031_OUTPUT_CONTROL_CEN);
431 return mt9p031_pll_enable(mt9p031);
434 static int mt9p031_enum_mbus_code(struct v4l2_subdev *subdev,
435 struct v4l2_subdev_fh *fh,
436 struct v4l2_subdev_mbus_code_enum *code)
438 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
440 if (code->pad || code->index)
443 code->code = mt9p031->format.code;
447 static int mt9p031_enum_frame_size(struct v4l2_subdev *subdev,
448 struct v4l2_subdev_fh *fh,
449 struct v4l2_subdev_frame_size_enum *fse)
451 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
453 if (fse->index >= 8 || fse->code != mt9p031->format.code)
456 fse->min_width = MT9P031_WINDOW_WIDTH_DEF
457 / min_t(unsigned int, 7, fse->index + 1);
458 fse->max_width = fse->min_width;
459 fse->min_height = MT9P031_WINDOW_HEIGHT_DEF / (fse->index + 1);
460 fse->max_height = fse->min_height;
465 static struct v4l2_mbus_framefmt *
466 __mt9p031_get_pad_format(struct mt9p031 *mt9p031, struct v4l2_subdev_fh *fh,
467 unsigned int pad, u32 which)
470 case V4L2_SUBDEV_FORMAT_TRY:
471 return v4l2_subdev_get_try_format(fh, pad);
472 case V4L2_SUBDEV_FORMAT_ACTIVE:
473 return &mt9p031->format;
479 static struct v4l2_rect *
480 __mt9p031_get_pad_crop(struct mt9p031 *mt9p031, struct v4l2_subdev_fh *fh,
481 unsigned int pad, u32 which)
484 case V4L2_SUBDEV_FORMAT_TRY:
485 return v4l2_subdev_get_try_crop(fh, pad);
486 case V4L2_SUBDEV_FORMAT_ACTIVE:
487 return &mt9p031->crop;
493 static int mt9p031_get_format(struct v4l2_subdev *subdev,
494 struct v4l2_subdev_fh *fh,
495 struct v4l2_subdev_format *fmt)
497 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
499 fmt->format = *__mt9p031_get_pad_format(mt9p031, fh, fmt->pad,
504 static int mt9p031_set_format(struct v4l2_subdev *subdev,
505 struct v4l2_subdev_fh *fh,
506 struct v4l2_subdev_format *format)
508 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
509 struct v4l2_mbus_framefmt *__format;
510 struct v4l2_rect *__crop;
516 __crop = __mt9p031_get_pad_crop(mt9p031, fh, format->pad,
519 /* Clamp the width and height to avoid dividing by zero. */
520 width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
521 max(__crop->width / 7, MT9P031_WINDOW_WIDTH_MIN),
523 height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
524 max(__crop->height / 8, MT9P031_WINDOW_HEIGHT_MIN),
527 hratio = DIV_ROUND_CLOSEST(__crop->width, width);
528 vratio = DIV_ROUND_CLOSEST(__crop->height, height);
530 __format = __mt9p031_get_pad_format(mt9p031, fh, format->pad,
532 __format->width = __crop->width / hratio;
533 __format->height = __crop->height / vratio;
535 format->format = *__format;
540 static int mt9p031_get_crop(struct v4l2_subdev *subdev,
541 struct v4l2_subdev_fh *fh,
542 struct v4l2_subdev_crop *crop)
544 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
546 crop->rect = *__mt9p031_get_pad_crop(mt9p031, fh, crop->pad,
551 static int mt9p031_set_crop(struct v4l2_subdev *subdev,
552 struct v4l2_subdev_fh *fh,
553 struct v4l2_subdev_crop *crop)
555 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
556 struct v4l2_mbus_framefmt *__format;
557 struct v4l2_rect *__crop;
558 struct v4l2_rect rect;
560 /* Clamp the crop rectangle boundaries and align them to a multiple of 2
561 * pixels to ensure a GRBG Bayer pattern.
563 rect.left = clamp(ALIGN(crop->rect.left, 2), MT9P031_COLUMN_START_MIN,
564 MT9P031_COLUMN_START_MAX);
565 rect.top = clamp(ALIGN(crop->rect.top, 2), MT9P031_ROW_START_MIN,
566 MT9P031_ROW_START_MAX);
567 rect.width = clamp(ALIGN(crop->rect.width, 2),
568 MT9P031_WINDOW_WIDTH_MIN,
569 MT9P031_WINDOW_WIDTH_MAX);
570 rect.height = clamp(ALIGN(crop->rect.height, 2),
571 MT9P031_WINDOW_HEIGHT_MIN,
572 MT9P031_WINDOW_HEIGHT_MAX);
574 rect.width = min(rect.width, MT9P031_PIXEL_ARRAY_WIDTH - rect.left);
575 rect.height = min(rect.height, MT9P031_PIXEL_ARRAY_HEIGHT - rect.top);
577 __crop = __mt9p031_get_pad_crop(mt9p031, fh, crop->pad, crop->which);
579 if (rect.width != __crop->width || rect.height != __crop->height) {
580 /* Reset the output image size if the crop rectangle size has
583 __format = __mt9p031_get_pad_format(mt9p031, fh, crop->pad,
585 __format->width = rect.width;
586 __format->height = rect.height;
595 /* -----------------------------------------------------------------------------
596 * V4L2 subdev control operations
599 #define V4L2_CID_BLC_AUTO (V4L2_CID_USER_BASE | 0x1002)
600 #define V4L2_CID_BLC_TARGET_LEVEL (V4L2_CID_USER_BASE | 0x1003)
601 #define V4L2_CID_BLC_ANALOG_OFFSET (V4L2_CID_USER_BASE | 0x1004)
602 #define V4L2_CID_BLC_DIGITAL_OFFSET (V4L2_CID_USER_BASE | 0x1005)
604 static int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl)
606 struct mt9p031 *mt9p031 =
607 container_of(ctrl->handler, struct mt9p031, ctrls);
608 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
613 case V4L2_CID_EXPOSURE:
614 ret = mt9p031_write(client, MT9P031_SHUTTER_WIDTH_UPPER,
615 (ctrl->val >> 16) & 0xffff);
619 return mt9p031_write(client, MT9P031_SHUTTER_WIDTH_LOWER,
623 /* Gain is controlled by 2 analog stages and a digital stage.
624 * Valid values for the 3 stages are
627 * ------------------------------------------
628 * First analog stage x1 x2 1
629 * Second analog stage x1 x4 0.125
630 * Digital stage x1 x16 0.125
632 * To minimize noise, the gain stages should be used in the
633 * second analog stage, first analog stage, digital stage order.
634 * Gain from a previous stage should be pushed to its maximum
635 * value before the next stage is used.
637 if (ctrl->val <= 32) {
639 } else if (ctrl->val <= 64) {
641 data = (1 << 6) | (ctrl->val >> 1);
644 data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;
647 return mt9p031_write(client, MT9P031_GLOBAL_GAIN, data);
651 return mt9p031_set_mode2(mt9p031,
652 0, MT9P031_READ_MODE_2_COL_MIR);
654 return mt9p031_set_mode2(mt9p031,
655 MT9P031_READ_MODE_2_COL_MIR, 0);
659 return mt9p031_set_mode2(mt9p031,
660 0, MT9P031_READ_MODE_2_ROW_MIR);
662 return mt9p031_set_mode2(mt9p031,
663 MT9P031_READ_MODE_2_ROW_MIR, 0);
665 case V4L2_CID_TEST_PATTERN:
667 /* Restore the black level compensation settings. */
668 if (mt9p031->blc_auto->cur.val != 0) {
669 ret = mt9p031_s_ctrl(mt9p031->blc_auto);
673 if (mt9p031->blc_offset->cur.val != 0) {
674 ret = mt9p031_s_ctrl(mt9p031->blc_offset);
678 return mt9p031_write(client, MT9P031_TEST_PATTERN,
679 MT9P031_TEST_PATTERN_DISABLE);
682 ret = mt9p031_write(client, MT9P031_TEST_PATTERN_GREEN, 0x05a0);
685 ret = mt9p031_write(client, MT9P031_TEST_PATTERN_RED, 0x0a50);
688 ret = mt9p031_write(client, MT9P031_TEST_PATTERN_BLUE, 0x0aa0);
692 /* Disable digital black level compensation when using a test
695 ret = mt9p031_set_mode2(mt9p031, MT9P031_READ_MODE_2_ROW_BLC,
700 ret = mt9p031_write(client, MT9P031_ROW_BLACK_DEF_OFFSET, 0);
704 return mt9p031_write(client, MT9P031_TEST_PATTERN,
705 ((ctrl->val - 1) << MT9P031_TEST_PATTERN_SHIFT)
706 | MT9P031_TEST_PATTERN_ENABLE);
708 case V4L2_CID_BLC_AUTO:
709 ret = mt9p031_set_mode2(mt9p031,
710 ctrl->val ? 0 : MT9P031_READ_MODE_2_ROW_BLC,
711 ctrl->val ? MT9P031_READ_MODE_2_ROW_BLC : 0);
715 return mt9p031_write(client, MT9P031_BLACK_LEVEL_CALIBRATION,
716 ctrl->val ? 0 : MT9P031_BLC_MANUAL_BLC);
718 case V4L2_CID_BLC_TARGET_LEVEL:
719 return mt9p031_write(client, MT9P031_ROW_BLACK_TARGET,
722 case V4L2_CID_BLC_ANALOG_OFFSET:
723 data = ctrl->val & ((1 << 9) - 1);
725 ret = mt9p031_write(client, MT9P031_GREEN1_OFFSET, data);
728 ret = mt9p031_write(client, MT9P031_GREEN2_OFFSET, data);
731 ret = mt9p031_write(client, MT9P031_RED_OFFSET, data);
734 return mt9p031_write(client, MT9P031_BLUE_OFFSET, data);
736 case V4L2_CID_BLC_DIGITAL_OFFSET:
737 return mt9p031_write(client, MT9P031_ROW_BLACK_DEF_OFFSET,
738 ctrl->val & ((1 << 12) - 1));
744 static struct v4l2_ctrl_ops mt9p031_ctrl_ops = {
745 .s_ctrl = mt9p031_s_ctrl,
748 static const char * const mt9p031_test_pattern_menu[] = {
751 "Horizontal Gradient",
754 "Classic Test Pattern",
756 "Monochrome Horizontal Bars",
757 "Monochrome Vertical Bars",
758 "Vertical Color Bars",
761 static const struct v4l2_ctrl_config mt9p031_ctrls[] = {
763 .ops = &mt9p031_ctrl_ops,
764 .id = V4L2_CID_BLC_AUTO,
765 .type = V4L2_CTRL_TYPE_BOOLEAN,
773 .ops = &mt9p031_ctrl_ops,
774 .id = V4L2_CID_BLC_TARGET_LEVEL,
775 .type = V4L2_CTRL_TYPE_INTEGER,
776 .name = "BLC Target Level",
783 .ops = &mt9p031_ctrl_ops,
784 .id = V4L2_CID_BLC_ANALOG_OFFSET,
785 .type = V4L2_CTRL_TYPE_INTEGER,
786 .name = "BLC Analog Offset",
793 .ops = &mt9p031_ctrl_ops,
794 .id = V4L2_CID_BLC_DIGITAL_OFFSET,
795 .type = V4L2_CTRL_TYPE_INTEGER,
796 .name = "BLC Digital Offset",
805 /* -----------------------------------------------------------------------------
806 * V4L2 subdev core operations
809 static int mt9p031_set_power(struct v4l2_subdev *subdev, int on)
811 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
814 mutex_lock(&mt9p031->power_lock);
816 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
817 * update the power state.
819 if (mt9p031->power_count == !on) {
820 ret = __mt9p031_set_power(mt9p031, !!on);
825 /* Update the power count. */
826 mt9p031->power_count += on ? 1 : -1;
827 WARN_ON(mt9p031->power_count < 0);
830 mutex_unlock(&mt9p031->power_lock);
834 /* -----------------------------------------------------------------------------
835 * V4L2 subdev internal operations
838 static int mt9p031_registered(struct v4l2_subdev *subdev)
840 struct i2c_client *client = v4l2_get_subdevdata(subdev);
841 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
845 ret = mt9p031_power_on(mt9p031);
847 dev_err(&client->dev, "MT9P031 power up failed\n");
851 /* Read out the chip version register */
852 data = mt9p031_read(client, MT9P031_CHIP_VERSION);
853 mt9p031_power_off(mt9p031);
855 if (data != MT9P031_CHIP_VERSION_VALUE) {
856 dev_err(&client->dev, "MT9P031 not detected, wrong version "
861 dev_info(&client->dev, "MT9P031 detected at address 0x%02x\n",
867 static int mt9p031_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
869 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
870 struct v4l2_mbus_framefmt *format;
871 struct v4l2_rect *crop;
873 crop = v4l2_subdev_get_try_crop(fh, 0);
874 crop->left = MT9P031_COLUMN_START_DEF;
875 crop->top = MT9P031_ROW_START_DEF;
876 crop->width = MT9P031_WINDOW_WIDTH_DEF;
877 crop->height = MT9P031_WINDOW_HEIGHT_DEF;
879 format = v4l2_subdev_get_try_format(fh, 0);
881 if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
882 format->code = V4L2_MBUS_FMT_Y12_1X12;
884 format->code = V4L2_MBUS_FMT_SGRBG12_1X12;
886 format->width = MT9P031_WINDOW_WIDTH_DEF;
887 format->height = MT9P031_WINDOW_HEIGHT_DEF;
888 format->field = V4L2_FIELD_NONE;
889 format->colorspace = V4L2_COLORSPACE_SRGB;
891 return mt9p031_set_power(subdev, 1);
894 static int mt9p031_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
896 return mt9p031_set_power(subdev, 0);
899 static struct v4l2_subdev_core_ops mt9p031_subdev_core_ops = {
900 .s_power = mt9p031_set_power,
903 static struct v4l2_subdev_video_ops mt9p031_subdev_video_ops = {
904 .s_stream = mt9p031_s_stream,
907 static struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops = {
908 .enum_mbus_code = mt9p031_enum_mbus_code,
909 .enum_frame_size = mt9p031_enum_frame_size,
910 .get_fmt = mt9p031_get_format,
911 .set_fmt = mt9p031_set_format,
912 .get_crop = mt9p031_get_crop,
913 .set_crop = mt9p031_set_crop,
916 static struct v4l2_subdev_ops mt9p031_subdev_ops = {
917 .core = &mt9p031_subdev_core_ops,
918 .video = &mt9p031_subdev_video_ops,
919 .pad = &mt9p031_subdev_pad_ops,
922 static const struct v4l2_subdev_internal_ops mt9p031_subdev_internal_ops = {
923 .registered = mt9p031_registered,
924 .open = mt9p031_open,
925 .close = mt9p031_close,
928 /* -----------------------------------------------------------------------------
929 * Driver initialization and probing
932 static struct mt9p031_platform_data *
933 mt9p031_get_pdata(struct i2c_client *client)
935 struct mt9p031_platform_data *pdata;
936 struct device_node *np;
938 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
939 return client->dev.platform_data;
941 np = v4l2_of_get_next_endpoint(client->dev.of_node, NULL);
945 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
949 pdata->reset = of_get_named_gpio(client->dev.of_node, "reset-gpios", 0);
950 of_property_read_u32(np, "input-clock-frequency", &pdata->ext_freq);
951 of_property_read_u32(np, "pixel-clock-frequency", &pdata->target_freq);
958 static int mt9p031_probe(struct i2c_client *client,
959 const struct i2c_device_id *did)
961 struct mt9p031_platform_data *pdata = mt9p031_get_pdata(client);
962 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
963 struct mt9p031 *mt9p031;
968 dev_err(&client->dev, "No platform data\n");
972 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
973 dev_warn(&client->dev,
974 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
978 mt9p031 = devm_kzalloc(&client->dev, sizeof(*mt9p031), GFP_KERNEL);
982 mt9p031->pdata = pdata;
983 mt9p031->output_control = MT9P031_OUTPUT_CONTROL_DEF;
984 mt9p031->mode2 = MT9P031_READ_MODE_2_ROW_BLC;
985 mt9p031->model = did->driver_data;
988 mt9p031->regulators[0].supply = "vdd";
989 mt9p031->regulators[1].supply = "vdd_io";
990 mt9p031->regulators[2].supply = "vaa";
992 ret = devm_regulator_bulk_get(&client->dev, 3, mt9p031->regulators);
994 dev_err(&client->dev, "Unable to get regulators\n");
998 v4l2_ctrl_handler_init(&mt9p031->ctrls, ARRAY_SIZE(mt9p031_ctrls) + 6);
1000 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1001 V4L2_CID_EXPOSURE, MT9P031_SHUTTER_WIDTH_MIN,
1002 MT9P031_SHUTTER_WIDTH_MAX, 1,
1003 MT9P031_SHUTTER_WIDTH_DEF);
1004 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1005 V4L2_CID_GAIN, MT9P031_GLOBAL_GAIN_MIN,
1006 MT9P031_GLOBAL_GAIN_MAX, 1, MT9P031_GLOBAL_GAIN_DEF);
1007 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1008 V4L2_CID_HFLIP, 0, 1, 1, 0);
1009 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1010 V4L2_CID_VFLIP, 0, 1, 1, 0);
1011 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1012 V4L2_CID_PIXEL_RATE, pdata->target_freq,
1013 pdata->target_freq, 1, pdata->target_freq);
1014 v4l2_ctrl_new_std_menu_items(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1015 V4L2_CID_TEST_PATTERN,
1016 ARRAY_SIZE(mt9p031_test_pattern_menu) - 1, 0,
1017 0, mt9p031_test_pattern_menu);
1019 for (i = 0; i < ARRAY_SIZE(mt9p031_ctrls); ++i)
1020 v4l2_ctrl_new_custom(&mt9p031->ctrls, &mt9p031_ctrls[i], NULL);
1022 mt9p031->subdev.ctrl_handler = &mt9p031->ctrls;
1024 if (mt9p031->ctrls.error) {
1025 printk(KERN_INFO "%s: control initialization error %d\n",
1026 __func__, mt9p031->ctrls.error);
1027 ret = mt9p031->ctrls.error;
1031 mt9p031->blc_auto = v4l2_ctrl_find(&mt9p031->ctrls, V4L2_CID_BLC_AUTO);
1032 mt9p031->blc_offset = v4l2_ctrl_find(&mt9p031->ctrls,
1033 V4L2_CID_BLC_DIGITAL_OFFSET);
1035 mutex_init(&mt9p031->power_lock);
1036 v4l2_i2c_subdev_init(&mt9p031->subdev, client, &mt9p031_subdev_ops);
1037 mt9p031->subdev.internal_ops = &mt9p031_subdev_internal_ops;
1039 mt9p031->pad.flags = MEDIA_PAD_FL_SOURCE;
1040 ret = media_entity_init(&mt9p031->subdev.entity, 1, &mt9p031->pad, 0);
1044 mt9p031->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1046 mt9p031->crop.width = MT9P031_WINDOW_WIDTH_DEF;
1047 mt9p031->crop.height = MT9P031_WINDOW_HEIGHT_DEF;
1048 mt9p031->crop.left = MT9P031_COLUMN_START_DEF;
1049 mt9p031->crop.top = MT9P031_ROW_START_DEF;
1051 if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
1052 mt9p031->format.code = V4L2_MBUS_FMT_Y12_1X12;
1054 mt9p031->format.code = V4L2_MBUS_FMT_SGRBG12_1X12;
1056 mt9p031->format.width = MT9P031_WINDOW_WIDTH_DEF;
1057 mt9p031->format.height = MT9P031_WINDOW_HEIGHT_DEF;
1058 mt9p031->format.field = V4L2_FIELD_NONE;
1059 mt9p031->format.colorspace = V4L2_COLORSPACE_SRGB;
1061 if (gpio_is_valid(pdata->reset)) {
1062 ret = devm_gpio_request_one(&client->dev, pdata->reset,
1063 GPIOF_OUT_INIT_LOW, "mt9p031_rst");
1067 mt9p031->reset = pdata->reset;
1070 ret = mt9p031_clk_setup(mt9p031);
1074 v4l2_ctrl_handler_free(&mt9p031->ctrls);
1075 media_entity_cleanup(&mt9p031->subdev.entity);
1081 static int mt9p031_remove(struct i2c_client *client)
1083 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1084 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
1086 v4l2_ctrl_handler_free(&mt9p031->ctrls);
1087 v4l2_device_unregister_subdev(subdev);
1088 media_entity_cleanup(&subdev->entity);
1093 static const struct i2c_device_id mt9p031_id[] = {
1094 { "mt9p031", MT9P031_MODEL_COLOR },
1095 { "mt9p031m", MT9P031_MODEL_MONOCHROME },
1098 MODULE_DEVICE_TABLE(i2c, mt9p031_id);
1100 #if IS_ENABLED(CONFIG_OF)
1101 static const struct of_device_id mt9p031_of_match[] = {
1102 { .compatible = "aptina,mt9p031", },
1103 { .compatible = "aptina,mt9p031m", },
1106 MODULE_DEVICE_TABLE(of, mt9p031_of_match);
1109 static struct i2c_driver mt9p031_i2c_driver = {
1111 .of_match_table = of_match_ptr(mt9p031_of_match),
1114 .probe = mt9p031_probe,
1115 .remove = mt9p031_remove,
1116 .id_table = mt9p031_id,
1119 module_i2c_driver(mt9p031_i2c_driver);
1121 MODULE_DESCRIPTION("Aptina MT9P031 Camera driver");
1122 MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
1123 MODULE_LICENSE("GPL v2");