1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for MT9P031 CMOS Image Sensor from Aptina
5 * Copyright (C) 2011, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
6 * Copyright (C) 2011, Javier Martin <javier.martin@vista-silicon.com>
7 * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
9 * Based on the MT9V032 driver and Bastian Hecht's code.
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/i2c.h>
17 #include <linux/log2.h>
18 #include <linux/module.h>
20 #include <linux/of_graph.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <linux/videodev2.h>
26 #include <media/i2c/mt9p031.h>
27 #include <media/v4l2-async.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-subdev.h>
32 #include "aptina-pll.h"
34 #define MT9P031_PIXEL_ARRAY_WIDTH 2752
35 #define MT9P031_PIXEL_ARRAY_HEIGHT 2004
37 #define MT9P031_CHIP_VERSION 0x00
38 #define MT9P031_CHIP_VERSION_VALUE 0x1801
39 #define MT9P031_ROW_START 0x01
40 #define MT9P031_ROW_START_MIN 0
41 #define MT9P031_ROW_START_MAX 2004
42 #define MT9P031_ROW_START_DEF 54
43 #define MT9P031_COLUMN_START 0x02
44 #define MT9P031_COLUMN_START_MIN 0
45 #define MT9P031_COLUMN_START_MAX 2750
46 #define MT9P031_COLUMN_START_DEF 16
47 #define MT9P031_WINDOW_HEIGHT 0x03
48 #define MT9P031_WINDOW_HEIGHT_MIN 2
49 #define MT9P031_WINDOW_HEIGHT_MAX 2006
50 #define MT9P031_WINDOW_HEIGHT_DEF 1944
51 #define MT9P031_WINDOW_WIDTH 0x04
52 #define MT9P031_WINDOW_WIDTH_MIN 2
53 #define MT9P031_WINDOW_WIDTH_MAX 2752
54 #define MT9P031_WINDOW_WIDTH_DEF 2592
55 #define MT9P031_HORIZONTAL_BLANK 0x05
56 #define MT9P031_HORIZONTAL_BLANK_MIN 0
57 #define MT9P031_HORIZONTAL_BLANK_MAX 4095
58 #define MT9P031_VERTICAL_BLANK 0x06
59 #define MT9P031_VERTICAL_BLANK_MIN 1
60 #define MT9P031_VERTICAL_BLANK_MAX 4096
61 #define MT9P031_VERTICAL_BLANK_DEF 26
62 #define MT9P031_OUTPUT_CONTROL 0x07
63 #define MT9P031_OUTPUT_CONTROL_CEN 2
64 #define MT9P031_OUTPUT_CONTROL_SYN 1
65 #define MT9P031_OUTPUT_CONTROL_DEF 0x1f82
66 #define MT9P031_SHUTTER_WIDTH_UPPER 0x08
67 #define MT9P031_SHUTTER_WIDTH_LOWER 0x09
68 #define MT9P031_SHUTTER_WIDTH_MIN 1
69 #define MT9P031_SHUTTER_WIDTH_MAX 1048575
70 #define MT9P031_SHUTTER_WIDTH_DEF 1943
71 #define MT9P031_PLL_CONTROL 0x10
72 #define MT9P031_PLL_CONTROL_PWROFF 0x0050
73 #define MT9P031_PLL_CONTROL_PWRON 0x0051
74 #define MT9P031_PLL_CONTROL_USEPLL 0x0052
75 #define MT9P031_PLL_CONFIG_1 0x11
76 #define MT9P031_PLL_CONFIG_2 0x12
77 #define MT9P031_PIXEL_CLOCK_CONTROL 0x0a
78 #define MT9P031_PIXEL_CLOCK_INVERT (1 << 15)
79 #define MT9P031_PIXEL_CLOCK_SHIFT(n) ((n) << 8)
80 #define MT9P031_PIXEL_CLOCK_DIVIDE(n) ((n) << 0)
81 #define MT9P031_FRAME_RESTART 0x0b
82 #define MT9P031_SHUTTER_DELAY 0x0c
83 #define MT9P031_RST 0x0d
84 #define MT9P031_RST_ENABLE 1
85 #define MT9P031_RST_DISABLE 0
86 #define MT9P031_READ_MODE_1 0x1e
87 #define MT9P031_READ_MODE_2 0x20
88 #define MT9P031_READ_MODE_2_ROW_MIR (1 << 15)
89 #define MT9P031_READ_MODE_2_COL_MIR (1 << 14)
90 #define MT9P031_READ_MODE_2_ROW_BLC (1 << 6)
91 #define MT9P031_ROW_ADDRESS_MODE 0x22
92 #define MT9P031_COLUMN_ADDRESS_MODE 0x23
93 #define MT9P031_GLOBAL_GAIN 0x35
94 #define MT9P031_GLOBAL_GAIN_MIN 8
95 #define MT9P031_GLOBAL_GAIN_MAX 1024
96 #define MT9P031_GLOBAL_GAIN_DEF 8
97 #define MT9P031_GLOBAL_GAIN_MULT (1 << 6)
98 #define MT9P031_ROW_BLACK_TARGET 0x49
99 #define MT9P031_ROW_BLACK_DEF_OFFSET 0x4b
100 #define MT9P031_GREEN1_OFFSET 0x60
101 #define MT9P031_GREEN2_OFFSET 0x61
102 #define MT9P031_BLACK_LEVEL_CALIBRATION 0x62
103 #define MT9P031_BLC_MANUAL_BLC (1 << 0)
104 #define MT9P031_RED_OFFSET 0x63
105 #define MT9P031_BLUE_OFFSET 0x64
106 #define MT9P031_TEST_PATTERN 0xa0
107 #define MT9P031_TEST_PATTERN_SHIFT 3
108 #define MT9P031_TEST_PATTERN_ENABLE (1 << 0)
109 #define MT9P031_TEST_PATTERN_DISABLE (0 << 0)
110 #define MT9P031_TEST_PATTERN_GREEN 0xa1
111 #define MT9P031_TEST_PATTERN_RED 0xa2
112 #define MT9P031_TEST_PATTERN_BLUE 0xa3
116 MT9P031_MODEL_MONOCHROME,
120 struct v4l2_subdev subdev;
121 struct media_pad pad;
122 struct v4l2_rect crop; /* Sensor window */
123 struct v4l2_mbus_framefmt format;
124 struct mt9p031_platform_data *pdata;
125 struct mutex power_lock; /* lock to protect power_count */
129 struct regulator_bulk_data regulators[3];
131 enum mt9p031_model model;
132 struct aptina_pll pll;
133 unsigned int clk_div;
135 struct gpio_desc *reset;
137 struct v4l2_ctrl_handler ctrls;
138 struct v4l2_ctrl *blc_auto;
139 struct v4l2_ctrl *blc_offset;
141 /* Registers cache */
146 static struct mt9p031 *to_mt9p031(struct v4l2_subdev *sd)
148 return container_of(sd, struct mt9p031, subdev);
151 static int mt9p031_read(struct i2c_client *client, u8 reg)
153 return i2c_smbus_read_word_swapped(client, reg);
156 static int mt9p031_write(struct i2c_client *client, u8 reg, u16 data)
158 return i2c_smbus_write_word_swapped(client, reg, data);
161 static int mt9p031_set_output_control(struct mt9p031 *mt9p031, u16 clear,
164 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
165 u16 value = (mt9p031->output_control & ~clear) | set;
168 ret = mt9p031_write(client, MT9P031_OUTPUT_CONTROL, value);
172 mt9p031->output_control = value;
176 static int mt9p031_set_mode2(struct mt9p031 *mt9p031, u16 clear, u16 set)
178 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
179 u16 value = (mt9p031->mode2 & ~clear) | set;
182 ret = mt9p031_write(client, MT9P031_READ_MODE_2, value);
186 mt9p031->mode2 = value;
190 static int mt9p031_reset(struct mt9p031 *mt9p031)
192 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
195 /* Disable chip output, synchronous option update */
196 ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_ENABLE);
199 ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_DISABLE);
203 ret = mt9p031_write(client, MT9P031_PIXEL_CLOCK_CONTROL,
204 MT9P031_PIXEL_CLOCK_DIVIDE(mt9p031->clk_div));
208 return mt9p031_set_output_control(mt9p031, MT9P031_OUTPUT_CONTROL_CEN,
212 static int mt9p031_clk_setup(struct mt9p031 *mt9p031)
214 static const struct aptina_pll_limits limits = {
215 .ext_clock_min = 6000000,
216 .ext_clock_max = 27000000,
217 .int_clock_min = 2000000,
218 .int_clock_max = 13500000,
219 .out_clock_min = 180000000,
220 .out_clock_max = 360000000,
221 .pix_clock_max = 96000000,
230 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
231 struct mt9p031_platform_data *pdata = mt9p031->pdata;
234 mt9p031->clk = devm_clk_get(&client->dev, NULL);
235 if (IS_ERR(mt9p031->clk))
236 return PTR_ERR(mt9p031->clk);
238 ret = clk_set_rate(mt9p031->clk, pdata->ext_freq);
242 /* If the external clock frequency is out of bounds for the PLL use the
243 * pixel clock divider only and disable the PLL.
245 if (pdata->ext_freq > limits.ext_clock_max) {
248 div = DIV_ROUND_UP(pdata->ext_freq, pdata->target_freq);
249 div = roundup_pow_of_two(div) / 2;
251 mt9p031->clk_div = min_t(unsigned int, div, 64);
252 mt9p031->use_pll = false;
257 mt9p031->pll.ext_clock = pdata->ext_freq;
258 mt9p031->pll.pix_clock = pdata->target_freq;
259 mt9p031->use_pll = true;
261 return aptina_pll_calculate(&client->dev, &limits, &mt9p031->pll);
264 static int mt9p031_pll_enable(struct mt9p031 *mt9p031)
266 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
269 if (!mt9p031->use_pll)
272 ret = mt9p031_write(client, MT9P031_PLL_CONTROL,
273 MT9P031_PLL_CONTROL_PWRON);
277 ret = mt9p031_write(client, MT9P031_PLL_CONFIG_1,
278 (mt9p031->pll.m << 8) | (mt9p031->pll.n - 1));
282 ret = mt9p031_write(client, MT9P031_PLL_CONFIG_2, mt9p031->pll.p1 - 1);
286 usleep_range(1000, 2000);
287 ret = mt9p031_write(client, MT9P031_PLL_CONTROL,
288 MT9P031_PLL_CONTROL_PWRON |
289 MT9P031_PLL_CONTROL_USEPLL);
293 static inline int mt9p031_pll_disable(struct mt9p031 *mt9p031)
295 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
297 if (!mt9p031->use_pll)
300 return mt9p031_write(client, MT9P031_PLL_CONTROL,
301 MT9P031_PLL_CONTROL_PWROFF);
304 static int mt9p031_power_on(struct mt9p031 *mt9p031)
308 /* Ensure RESET_BAR is active */
309 if (mt9p031->reset) {
310 gpiod_set_value(mt9p031->reset, 1);
311 usleep_range(1000, 2000);
314 /* Bring up the supplies */
315 ret = regulator_bulk_enable(ARRAY_SIZE(mt9p031->regulators),
316 mt9p031->regulators);
322 ret = clk_prepare_enable(mt9p031->clk);
324 regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators),
325 mt9p031->regulators);
330 /* Now RESET_BAR must be high */
331 if (mt9p031->reset) {
332 gpiod_set_value(mt9p031->reset, 0);
333 usleep_range(1000, 2000);
339 static void mt9p031_power_off(struct mt9p031 *mt9p031)
341 if (mt9p031->reset) {
342 gpiod_set_value(mt9p031->reset, 1);
343 usleep_range(1000, 2000);
346 regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators),
347 mt9p031->regulators);
349 clk_disable_unprepare(mt9p031->clk);
352 static int __mt9p031_set_power(struct mt9p031 *mt9p031, bool on)
354 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
358 mt9p031_power_off(mt9p031);
362 ret = mt9p031_power_on(mt9p031);
366 ret = mt9p031_reset(mt9p031);
368 dev_err(&client->dev, "Failed to reset the camera\n");
372 return v4l2_ctrl_handler_setup(&mt9p031->ctrls);
375 /* -----------------------------------------------------------------------------
376 * V4L2 subdev video operations
379 static int mt9p031_set_params(struct mt9p031 *mt9p031)
381 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
382 struct v4l2_mbus_framefmt *format = &mt9p031->format;
383 const struct v4l2_rect *crop = &mt9p031->crop;
392 /* Windows position and size.
394 * TODO: Make sure the start coordinates and window size match the
395 * skipping, binning and mirroring (see description of registers 2 and 4
396 * in table 13, and Binning section on page 41).
398 ret = mt9p031_write(client, MT9P031_COLUMN_START, crop->left);
401 ret = mt9p031_write(client, MT9P031_ROW_START, crop->top);
404 ret = mt9p031_write(client, MT9P031_WINDOW_WIDTH, crop->width - 1);
407 ret = mt9p031_write(client, MT9P031_WINDOW_HEIGHT, crop->height - 1);
411 /* Row and column binning and skipping. Use the maximum binning value
412 * compatible with the skipping settings.
414 xskip = DIV_ROUND_CLOSEST(crop->width, format->width);
415 yskip = DIV_ROUND_CLOSEST(crop->height, format->height);
416 xbin = 1 << (ffs(xskip) - 1);
417 ybin = 1 << (ffs(yskip) - 1);
419 ret = mt9p031_write(client, MT9P031_COLUMN_ADDRESS_MODE,
420 ((xbin - 1) << 4) | (xskip - 1));
423 ret = mt9p031_write(client, MT9P031_ROW_ADDRESS_MODE,
424 ((ybin - 1) << 4) | (yskip - 1));
428 /* Blanking - use minimum value for horizontal blanking and default
429 * value for vertical blanking.
431 hblank = 346 * ybin + 64 + (80 >> min_t(unsigned int, xbin, 3));
432 vblank = MT9P031_VERTICAL_BLANK_DEF;
434 ret = mt9p031_write(client, MT9P031_HORIZONTAL_BLANK, hblank - 1);
437 ret = mt9p031_write(client, MT9P031_VERTICAL_BLANK, vblank - 1);
444 static int mt9p031_s_stream(struct v4l2_subdev *subdev, int enable)
446 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
450 /* Stop sensor readout */
451 ret = mt9p031_set_output_control(mt9p031,
452 MT9P031_OUTPUT_CONTROL_CEN, 0);
456 return mt9p031_pll_disable(mt9p031);
459 ret = mt9p031_set_params(mt9p031);
463 /* Switch to master "normal" mode */
464 ret = mt9p031_set_output_control(mt9p031, 0,
465 MT9P031_OUTPUT_CONTROL_CEN);
469 return mt9p031_pll_enable(mt9p031);
472 static int mt9p031_enum_mbus_code(struct v4l2_subdev *subdev,
473 struct v4l2_subdev_state *sd_state,
474 struct v4l2_subdev_mbus_code_enum *code)
476 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
478 if (code->pad || code->index)
481 code->code = mt9p031->format.code;
485 static int mt9p031_enum_frame_size(struct v4l2_subdev *subdev,
486 struct v4l2_subdev_state *sd_state,
487 struct v4l2_subdev_frame_size_enum *fse)
489 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
491 if (fse->index >= 8 || fse->code != mt9p031->format.code)
494 fse->min_width = MT9P031_WINDOW_WIDTH_DEF
495 / min_t(unsigned int, 7, fse->index + 1);
496 fse->max_width = fse->min_width;
497 fse->min_height = MT9P031_WINDOW_HEIGHT_DEF / (fse->index + 1);
498 fse->max_height = fse->min_height;
503 static struct v4l2_mbus_framefmt *
504 __mt9p031_get_pad_format(struct mt9p031 *mt9p031,
505 struct v4l2_subdev_state *sd_state,
506 unsigned int pad, u32 which)
509 case V4L2_SUBDEV_FORMAT_TRY:
510 return v4l2_subdev_get_try_format(&mt9p031->subdev, sd_state,
512 case V4L2_SUBDEV_FORMAT_ACTIVE:
513 return &mt9p031->format;
519 static struct v4l2_rect *
520 __mt9p031_get_pad_crop(struct mt9p031 *mt9p031,
521 struct v4l2_subdev_state *sd_state,
522 unsigned int pad, u32 which)
525 case V4L2_SUBDEV_FORMAT_TRY:
526 return v4l2_subdev_get_try_crop(&mt9p031->subdev, sd_state,
528 case V4L2_SUBDEV_FORMAT_ACTIVE:
529 return &mt9p031->crop;
535 static int mt9p031_get_format(struct v4l2_subdev *subdev,
536 struct v4l2_subdev_state *sd_state,
537 struct v4l2_subdev_format *fmt)
539 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
541 fmt->format = *__mt9p031_get_pad_format(mt9p031, sd_state, fmt->pad,
546 static int mt9p031_set_format(struct v4l2_subdev *subdev,
547 struct v4l2_subdev_state *sd_state,
548 struct v4l2_subdev_format *format)
550 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
551 struct v4l2_mbus_framefmt *__format;
552 struct v4l2_rect *__crop;
558 __crop = __mt9p031_get_pad_crop(mt9p031, sd_state, format->pad,
561 /* Clamp the width and height to avoid dividing by zero. */
562 width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
563 max_t(unsigned int, __crop->width / 7,
564 MT9P031_WINDOW_WIDTH_MIN),
566 height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
567 max_t(unsigned int, __crop->height / 8,
568 MT9P031_WINDOW_HEIGHT_MIN),
571 hratio = DIV_ROUND_CLOSEST(__crop->width, width);
572 vratio = DIV_ROUND_CLOSEST(__crop->height, height);
574 __format = __mt9p031_get_pad_format(mt9p031, sd_state, format->pad,
576 __format->width = __crop->width / hratio;
577 __format->height = __crop->height / vratio;
579 format->format = *__format;
584 static int mt9p031_get_selection(struct v4l2_subdev *subdev,
585 struct v4l2_subdev_state *sd_state,
586 struct v4l2_subdev_selection *sel)
588 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
590 if (sel->target != V4L2_SEL_TGT_CROP)
593 sel->r = *__mt9p031_get_pad_crop(mt9p031, sd_state, sel->pad,
598 static int mt9p031_set_selection(struct v4l2_subdev *subdev,
599 struct v4l2_subdev_state *sd_state,
600 struct v4l2_subdev_selection *sel)
602 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
603 struct v4l2_mbus_framefmt *__format;
604 struct v4l2_rect *__crop;
605 struct v4l2_rect rect;
607 if (sel->target != V4L2_SEL_TGT_CROP)
610 /* Clamp the crop rectangle boundaries and align them to a multiple of 2
611 * pixels to ensure a GRBG Bayer pattern.
613 rect.left = clamp(ALIGN(sel->r.left, 2), MT9P031_COLUMN_START_MIN,
614 MT9P031_COLUMN_START_MAX);
615 rect.top = clamp(ALIGN(sel->r.top, 2), MT9P031_ROW_START_MIN,
616 MT9P031_ROW_START_MAX);
617 rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
618 MT9P031_WINDOW_WIDTH_MIN,
619 MT9P031_WINDOW_WIDTH_MAX);
620 rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
621 MT9P031_WINDOW_HEIGHT_MIN,
622 MT9P031_WINDOW_HEIGHT_MAX);
624 rect.width = min_t(unsigned int, rect.width,
625 MT9P031_PIXEL_ARRAY_WIDTH - rect.left);
626 rect.height = min_t(unsigned int, rect.height,
627 MT9P031_PIXEL_ARRAY_HEIGHT - rect.top);
629 __crop = __mt9p031_get_pad_crop(mt9p031, sd_state, sel->pad,
632 if (rect.width != __crop->width || rect.height != __crop->height) {
633 /* Reset the output image size if the crop rectangle size has
636 __format = __mt9p031_get_pad_format(mt9p031, sd_state,
639 __format->width = rect.width;
640 __format->height = rect.height;
649 /* -----------------------------------------------------------------------------
650 * V4L2 subdev control operations
653 #define V4L2_CID_BLC_AUTO (V4L2_CID_USER_BASE | 0x1002)
654 #define V4L2_CID_BLC_TARGET_LEVEL (V4L2_CID_USER_BASE | 0x1003)
655 #define V4L2_CID_BLC_ANALOG_OFFSET (V4L2_CID_USER_BASE | 0x1004)
656 #define V4L2_CID_BLC_DIGITAL_OFFSET (V4L2_CID_USER_BASE | 0x1005)
658 static int mt9p031_restore_blc(struct mt9p031 *mt9p031)
660 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
663 if (mt9p031->blc_auto->cur.val != 0) {
664 ret = mt9p031_set_mode2(mt9p031, 0,
665 MT9P031_READ_MODE_2_ROW_BLC);
670 if (mt9p031->blc_offset->cur.val != 0) {
671 ret = mt9p031_write(client, MT9P031_ROW_BLACK_TARGET,
672 mt9p031->blc_offset->cur.val);
680 static int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl)
682 struct mt9p031 *mt9p031 =
683 container_of(ctrl->handler, struct mt9p031, ctrls);
684 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
688 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
692 case V4L2_CID_EXPOSURE:
693 ret = mt9p031_write(client, MT9P031_SHUTTER_WIDTH_UPPER,
694 (ctrl->val >> 16) & 0xffff);
698 return mt9p031_write(client, MT9P031_SHUTTER_WIDTH_LOWER,
702 /* Gain is controlled by 2 analog stages and a digital stage.
703 * Valid values for the 3 stages are
706 * ------------------------------------------
707 * First analog stage x1 x2 1
708 * Second analog stage x1 x4 0.125
709 * Digital stage x1 x16 0.125
711 * To minimize noise, the gain stages should be used in the
712 * second analog stage, first analog stage, digital stage order.
713 * Gain from a previous stage should be pushed to its maximum
714 * value before the next stage is used.
716 if (ctrl->val <= 32) {
718 } else if (ctrl->val <= 64) {
720 data = (1 << 6) | (ctrl->val >> 1);
723 data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;
726 return mt9p031_write(client, MT9P031_GLOBAL_GAIN, data);
730 return mt9p031_set_mode2(mt9p031,
731 0, MT9P031_READ_MODE_2_COL_MIR);
733 return mt9p031_set_mode2(mt9p031,
734 MT9P031_READ_MODE_2_COL_MIR, 0);
738 return mt9p031_set_mode2(mt9p031,
739 0, MT9P031_READ_MODE_2_ROW_MIR);
741 return mt9p031_set_mode2(mt9p031,
742 MT9P031_READ_MODE_2_ROW_MIR, 0);
744 case V4L2_CID_TEST_PATTERN:
745 /* The digital side of the Black Level Calibration function must
746 * be disabled when generating a test pattern to avoid artifacts
747 * in the image. Activate (deactivate) the BLC-related controls
748 * when the test pattern is enabled (disabled).
750 v4l2_ctrl_activate(mt9p031->blc_auto, ctrl->val == 0);
751 v4l2_ctrl_activate(mt9p031->blc_offset, ctrl->val == 0);
754 /* Restore the BLC settings. */
755 ret = mt9p031_restore_blc(mt9p031);
759 return mt9p031_write(client, MT9P031_TEST_PATTERN,
760 MT9P031_TEST_PATTERN_DISABLE);
763 ret = mt9p031_write(client, MT9P031_TEST_PATTERN_GREEN, 0x05a0);
766 ret = mt9p031_write(client, MT9P031_TEST_PATTERN_RED, 0x0a50);
769 ret = mt9p031_write(client, MT9P031_TEST_PATTERN_BLUE, 0x0aa0);
773 /* Disable digital BLC when generating a test pattern. */
774 ret = mt9p031_set_mode2(mt9p031, MT9P031_READ_MODE_2_ROW_BLC,
779 ret = mt9p031_write(client, MT9P031_ROW_BLACK_DEF_OFFSET, 0);
783 return mt9p031_write(client, MT9P031_TEST_PATTERN,
784 ((ctrl->val - 1) << MT9P031_TEST_PATTERN_SHIFT)
785 | MT9P031_TEST_PATTERN_ENABLE);
787 case V4L2_CID_BLC_AUTO:
788 ret = mt9p031_set_mode2(mt9p031,
789 ctrl->val ? 0 : MT9P031_READ_MODE_2_ROW_BLC,
790 ctrl->val ? MT9P031_READ_MODE_2_ROW_BLC : 0);
794 return mt9p031_write(client, MT9P031_BLACK_LEVEL_CALIBRATION,
795 ctrl->val ? 0 : MT9P031_BLC_MANUAL_BLC);
797 case V4L2_CID_BLC_TARGET_LEVEL:
798 return mt9p031_write(client, MT9P031_ROW_BLACK_TARGET,
801 case V4L2_CID_BLC_ANALOG_OFFSET:
802 data = ctrl->val & ((1 << 9) - 1);
804 ret = mt9p031_write(client, MT9P031_GREEN1_OFFSET, data);
807 ret = mt9p031_write(client, MT9P031_GREEN2_OFFSET, data);
810 ret = mt9p031_write(client, MT9P031_RED_OFFSET, data);
813 return mt9p031_write(client, MT9P031_BLUE_OFFSET, data);
815 case V4L2_CID_BLC_DIGITAL_OFFSET:
816 return mt9p031_write(client, MT9P031_ROW_BLACK_DEF_OFFSET,
817 ctrl->val & ((1 << 12) - 1));
823 static const struct v4l2_ctrl_ops mt9p031_ctrl_ops = {
824 .s_ctrl = mt9p031_s_ctrl,
827 static const char * const mt9p031_test_pattern_menu[] = {
830 "Horizontal Gradient",
833 "Classic Test Pattern",
835 "Monochrome Horizontal Bars",
836 "Monochrome Vertical Bars",
837 "Vertical Color Bars",
840 static const struct v4l2_ctrl_config mt9p031_ctrls[] = {
842 .ops = &mt9p031_ctrl_ops,
843 .id = V4L2_CID_BLC_AUTO,
844 .type = V4L2_CTRL_TYPE_BOOLEAN,
852 .ops = &mt9p031_ctrl_ops,
853 .id = V4L2_CID_BLC_TARGET_LEVEL,
854 .type = V4L2_CTRL_TYPE_INTEGER,
855 .name = "BLC Target Level",
862 .ops = &mt9p031_ctrl_ops,
863 .id = V4L2_CID_BLC_ANALOG_OFFSET,
864 .type = V4L2_CTRL_TYPE_INTEGER,
865 .name = "BLC Analog Offset",
872 .ops = &mt9p031_ctrl_ops,
873 .id = V4L2_CID_BLC_DIGITAL_OFFSET,
874 .type = V4L2_CTRL_TYPE_INTEGER,
875 .name = "BLC Digital Offset",
884 /* -----------------------------------------------------------------------------
885 * V4L2 subdev core operations
888 static int mt9p031_set_power(struct v4l2_subdev *subdev, int on)
890 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
893 mutex_lock(&mt9p031->power_lock);
895 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
896 * update the power state.
898 if (mt9p031->power_count == !on) {
899 ret = __mt9p031_set_power(mt9p031, !!on);
904 /* Update the power count. */
905 mt9p031->power_count += on ? 1 : -1;
906 WARN_ON(mt9p031->power_count < 0);
909 mutex_unlock(&mt9p031->power_lock);
913 /* -----------------------------------------------------------------------------
914 * V4L2 subdev internal operations
917 static int mt9p031_registered(struct v4l2_subdev *subdev)
919 struct i2c_client *client = v4l2_get_subdevdata(subdev);
920 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
924 ret = mt9p031_power_on(mt9p031);
926 dev_err(&client->dev, "MT9P031 power up failed\n");
930 /* Read out the chip version register */
931 data = mt9p031_read(client, MT9P031_CHIP_VERSION);
932 mt9p031_power_off(mt9p031);
934 if (data != MT9P031_CHIP_VERSION_VALUE) {
935 dev_err(&client->dev, "MT9P031 not detected, wrong version "
940 dev_info(&client->dev, "MT9P031 detected at address 0x%02x\n",
946 static int mt9p031_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
948 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
949 struct v4l2_mbus_framefmt *format;
950 struct v4l2_rect *crop;
952 crop = v4l2_subdev_get_try_crop(subdev, fh->state, 0);
953 crop->left = MT9P031_COLUMN_START_DEF;
954 crop->top = MT9P031_ROW_START_DEF;
955 crop->width = MT9P031_WINDOW_WIDTH_DEF;
956 crop->height = MT9P031_WINDOW_HEIGHT_DEF;
958 format = v4l2_subdev_get_try_format(subdev, fh->state, 0);
960 if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
961 format->code = MEDIA_BUS_FMT_Y12_1X12;
963 format->code = MEDIA_BUS_FMT_SGRBG12_1X12;
965 format->width = MT9P031_WINDOW_WIDTH_DEF;
966 format->height = MT9P031_WINDOW_HEIGHT_DEF;
967 format->field = V4L2_FIELD_NONE;
968 format->colorspace = V4L2_COLORSPACE_SRGB;
970 return mt9p031_set_power(subdev, 1);
973 static int mt9p031_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
975 return mt9p031_set_power(subdev, 0);
978 static const struct v4l2_subdev_core_ops mt9p031_subdev_core_ops = {
979 .s_power = mt9p031_set_power,
982 static const struct v4l2_subdev_video_ops mt9p031_subdev_video_ops = {
983 .s_stream = mt9p031_s_stream,
986 static const struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops = {
987 .enum_mbus_code = mt9p031_enum_mbus_code,
988 .enum_frame_size = mt9p031_enum_frame_size,
989 .get_fmt = mt9p031_get_format,
990 .set_fmt = mt9p031_set_format,
991 .get_selection = mt9p031_get_selection,
992 .set_selection = mt9p031_set_selection,
995 static const struct v4l2_subdev_ops mt9p031_subdev_ops = {
996 .core = &mt9p031_subdev_core_ops,
997 .video = &mt9p031_subdev_video_ops,
998 .pad = &mt9p031_subdev_pad_ops,
1001 static const struct v4l2_subdev_internal_ops mt9p031_subdev_internal_ops = {
1002 .registered = mt9p031_registered,
1003 .open = mt9p031_open,
1004 .close = mt9p031_close,
1007 /* -----------------------------------------------------------------------------
1008 * Driver initialization and probing
1011 static struct mt9p031_platform_data *
1012 mt9p031_get_pdata(struct i2c_client *client)
1014 struct mt9p031_platform_data *pdata;
1015 struct device_node *np;
1017 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
1018 return client->dev.platform_data;
1020 np = of_graph_get_next_endpoint(client->dev.of_node, NULL);
1024 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1028 of_property_read_u32(np, "input-clock-frequency", &pdata->ext_freq);
1029 of_property_read_u32(np, "pixel-clock-frequency", &pdata->target_freq);
1036 static int mt9p031_probe(struct i2c_client *client,
1037 const struct i2c_device_id *did)
1039 struct mt9p031_platform_data *pdata = mt9p031_get_pdata(client);
1040 struct i2c_adapter *adapter = client->adapter;
1041 struct mt9p031 *mt9p031;
1045 if (pdata == NULL) {
1046 dev_err(&client->dev, "No platform data\n");
1050 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
1051 dev_warn(&client->dev,
1052 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
1056 mt9p031 = devm_kzalloc(&client->dev, sizeof(*mt9p031), GFP_KERNEL);
1057 if (mt9p031 == NULL)
1060 mt9p031->pdata = pdata;
1061 mt9p031->output_control = MT9P031_OUTPUT_CONTROL_DEF;
1062 mt9p031->mode2 = MT9P031_READ_MODE_2_ROW_BLC;
1063 mt9p031->model = did->driver_data;
1065 mt9p031->regulators[0].supply = "vdd";
1066 mt9p031->regulators[1].supply = "vdd_io";
1067 mt9p031->regulators[2].supply = "vaa";
1069 ret = devm_regulator_bulk_get(&client->dev, 3, mt9p031->regulators);
1071 dev_err(&client->dev, "Unable to get regulators\n");
1075 mutex_init(&mt9p031->power_lock);
1077 v4l2_ctrl_handler_init(&mt9p031->ctrls, ARRAY_SIZE(mt9p031_ctrls) + 6);
1079 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1080 V4L2_CID_EXPOSURE, MT9P031_SHUTTER_WIDTH_MIN,
1081 MT9P031_SHUTTER_WIDTH_MAX, 1,
1082 MT9P031_SHUTTER_WIDTH_DEF);
1083 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1084 V4L2_CID_GAIN, MT9P031_GLOBAL_GAIN_MIN,
1085 MT9P031_GLOBAL_GAIN_MAX, 1, MT9P031_GLOBAL_GAIN_DEF);
1086 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1087 V4L2_CID_HFLIP, 0, 1, 1, 0);
1088 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1089 V4L2_CID_VFLIP, 0, 1, 1, 0);
1090 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1091 V4L2_CID_PIXEL_RATE, pdata->target_freq,
1092 pdata->target_freq, 1, pdata->target_freq);
1093 v4l2_ctrl_new_std_menu_items(&mt9p031->ctrls, &mt9p031_ctrl_ops,
1094 V4L2_CID_TEST_PATTERN,
1095 ARRAY_SIZE(mt9p031_test_pattern_menu) - 1, 0,
1096 0, mt9p031_test_pattern_menu);
1098 for (i = 0; i < ARRAY_SIZE(mt9p031_ctrls); ++i)
1099 v4l2_ctrl_new_custom(&mt9p031->ctrls, &mt9p031_ctrls[i], NULL);
1101 mt9p031->subdev.ctrl_handler = &mt9p031->ctrls;
1103 if (mt9p031->ctrls.error) {
1104 printk(KERN_INFO "%s: control initialization error %d\n",
1105 __func__, mt9p031->ctrls.error);
1106 ret = mt9p031->ctrls.error;
1110 mt9p031->blc_auto = v4l2_ctrl_find(&mt9p031->ctrls, V4L2_CID_BLC_AUTO);
1111 mt9p031->blc_offset = v4l2_ctrl_find(&mt9p031->ctrls,
1112 V4L2_CID_BLC_DIGITAL_OFFSET);
1114 v4l2_i2c_subdev_init(&mt9p031->subdev, client, &mt9p031_subdev_ops);
1115 mt9p031->subdev.internal_ops = &mt9p031_subdev_internal_ops;
1117 mt9p031->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1118 mt9p031->pad.flags = MEDIA_PAD_FL_SOURCE;
1119 ret = media_entity_pads_init(&mt9p031->subdev.entity, 1, &mt9p031->pad);
1123 mt9p031->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1125 mt9p031->crop.width = MT9P031_WINDOW_WIDTH_DEF;
1126 mt9p031->crop.height = MT9P031_WINDOW_HEIGHT_DEF;
1127 mt9p031->crop.left = MT9P031_COLUMN_START_DEF;
1128 mt9p031->crop.top = MT9P031_ROW_START_DEF;
1130 if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
1131 mt9p031->format.code = MEDIA_BUS_FMT_Y12_1X12;
1133 mt9p031->format.code = MEDIA_BUS_FMT_SGRBG12_1X12;
1135 mt9p031->format.width = MT9P031_WINDOW_WIDTH_DEF;
1136 mt9p031->format.height = MT9P031_WINDOW_HEIGHT_DEF;
1137 mt9p031->format.field = V4L2_FIELD_NONE;
1138 mt9p031->format.colorspace = V4L2_COLORSPACE_SRGB;
1140 mt9p031->reset = devm_gpiod_get_optional(&client->dev, "reset",
1143 ret = mt9p031_clk_setup(mt9p031);
1147 ret = v4l2_async_register_subdev(&mt9p031->subdev);
1151 v4l2_ctrl_handler_free(&mt9p031->ctrls);
1152 media_entity_cleanup(&mt9p031->subdev.entity);
1153 mutex_destroy(&mt9p031->power_lock);
1159 static int mt9p031_remove(struct i2c_client *client)
1161 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1162 struct mt9p031 *mt9p031 = to_mt9p031(subdev);
1164 v4l2_ctrl_handler_free(&mt9p031->ctrls);
1165 v4l2_async_unregister_subdev(subdev);
1166 media_entity_cleanup(&subdev->entity);
1167 mutex_destroy(&mt9p031->power_lock);
1172 static const struct i2c_device_id mt9p031_id[] = {
1173 { "mt9p031", MT9P031_MODEL_COLOR },
1174 { "mt9p031m", MT9P031_MODEL_MONOCHROME },
1177 MODULE_DEVICE_TABLE(i2c, mt9p031_id);
1179 #if IS_ENABLED(CONFIG_OF)
1180 static const struct of_device_id mt9p031_of_match[] = {
1181 { .compatible = "aptina,mt9p031", },
1182 { .compatible = "aptina,mt9p031m", },
1185 MODULE_DEVICE_TABLE(of, mt9p031_of_match);
1188 static struct i2c_driver mt9p031_i2c_driver = {
1190 .of_match_table = of_match_ptr(mt9p031_of_match),
1193 .probe = mt9p031_probe,
1194 .remove = mt9p031_remove,
1195 .id_table = mt9p031_id,
1198 module_i2c_driver(mt9p031_i2c_driver);
1200 MODULE_DESCRIPTION("Aptina MT9P031 Camera driver");
1201 MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
1202 MODULE_LICENSE("GPL v2");