1 // SPDX-License-Identifier: GPL-2.0
3 * V4L2 sensor driver for Aptina MT9V111 image sensor
4 * Copyright (C) 2018 Jacopo Mondi <jacopo@jmondi.org>
6 * Based on mt9v032 driver
7 * Copyright (C) 2010, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
10 * Based on mt9v011 driver
11 * Copyright (c) 2009 Mauro Carvalho Chehab <mchehab@kernel.org>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <linux/videodev2.h>
21 #include <linux/v4l2-mediabus.h>
22 #include <linux/module.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-fwnode.h>
27 #include <media/v4l2-image-sizes.h>
28 #include <media/v4l2-subdev.h>
31 * MT9V111 is a 1/4-Inch CMOS digital image sensor with an integrated
32 * Image Flow Processing (IFP) engine and a sensor core loosely based on
35 * The IFP can produce several output image formats from the sensor core
36 * output. This driver currently supports only YUYV format permutations.
38 * The driver allows manual frame rate control through s_frame_interval subdev
39 * operation or V4L2_CID_V/HBLANK controls, but it is known that the
40 * auto-exposure algorithm might modify the programmed frame rate. While the
41 * driver initially programs the sensor with auto-exposure and
42 * auto-white-balancing enabled, it is possible to disable them and more
43 * precisely control the frame rate.
45 * While it seems possible to instruct the auto-exposure control algorithm to
46 * respect a programmed frame rate when adjusting the pixel integration time,
47 * registers controlling this feature are not documented in the public
48 * available sensor manual used to develop this driver (09005aef80e90084,
49 * MT9V111_1.fm - Rev. G 1/05 EN).
52 #define MT9V111_CHIP_ID_HIGH 0x82
53 #define MT9V111_CHIP_ID_LOW 0x3a
55 #define MT9V111_R01_ADDR_SPACE 0x01
56 #define MT9V111_R01_IFP 0x01
57 #define MT9V111_R01_CORE 0x04
59 #define MT9V111_IFP_R06_OPMODE_CTRL 0x06
60 #define MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN BIT(1)
61 #define MT9V111_IFP_R06_OPMODE_CTRL_AE_EN BIT(14)
62 #define MT9V111_IFP_R07_IFP_RESET 0x07
63 #define MT9V111_IFP_R07_IFP_RESET_MASK BIT(0)
64 #define MT9V111_IFP_R08_OUTFMT_CTRL 0x08
65 #define MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER BIT(11)
66 #define MT9V111_IFP_R08_OUTFMT_CTRL_PCLK BIT(5)
67 #define MT9V111_IFP_R3A_OUTFMT_CTRL2 0x3a
68 #define MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR BIT(0)
69 #define MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC BIT(1)
70 #define MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_MASK GENMASK(2, 0)
71 #define MT9V111_IFP_RA5_HPAN 0xa5
72 #define MT9V111_IFP_RA6_HZOOM 0xa6
73 #define MT9V111_IFP_RA7_HOUT 0xa7
74 #define MT9V111_IFP_RA8_VPAN 0xa8
75 #define MT9V111_IFP_RA9_VZOOM 0xa9
76 #define MT9V111_IFP_RAA_VOUT 0xaa
77 #define MT9V111_IFP_DECIMATION_MASK GENMASK(9, 0)
78 #define MT9V111_IFP_DECIMATION_FREEZE BIT(15)
80 #define MT9V111_CORE_R03_WIN_HEIGHT 0x03
81 #define MT9V111_CORE_R03_WIN_V_OFFS 2
82 #define MT9V111_CORE_R04_WIN_WIDTH 0x04
83 #define MT9V111_CORE_R04_WIN_H_OFFS 114
84 #define MT9V111_CORE_R05_HBLANK 0x05
85 #define MT9V111_CORE_R05_MIN_HBLANK 0x09
86 #define MT9V111_CORE_R05_MAX_HBLANK GENMASK(9, 0)
87 #define MT9V111_CORE_R05_DEF_HBLANK 0x26
88 #define MT9V111_CORE_R06_VBLANK 0x06
89 #define MT9V111_CORE_R06_MIN_VBLANK 0x03
90 #define MT9V111_CORE_R06_MAX_VBLANK GENMASK(11, 0)
91 #define MT9V111_CORE_R06_DEF_VBLANK 0x04
92 #define MT9V111_CORE_R07_OUT_CTRL 0x07
93 #define MT9V111_CORE_R07_OUT_CTRL_SAMPLE BIT(4)
94 #define MT9V111_CORE_R09_PIXEL_INT 0x09
95 #define MT9V111_CORE_R09_PIXEL_INT_MASK GENMASK(11, 0)
96 #define MT9V111_CORE_R0D_CORE_RESET 0x0d
97 #define MT9V111_CORE_R0D_CORE_RESET_MASK BIT(0)
98 #define MT9V111_CORE_RFF_CHIP_VER 0xff
100 #define MT9V111_PIXEL_ARRAY_WIDTH 640
101 #define MT9V111_PIXEL_ARRAY_HEIGHT 480
103 #define MT9V111_MAX_CLKIN 27000000
105 /* The default sensor configuration at startup time. */
106 static struct v4l2_mbus_framefmt mt9v111_def_fmt
= {
109 .code
= MEDIA_BUS_FMT_UYVY8_2X8
,
110 .field
= V4L2_FIELD_NONE
,
111 .colorspace
= V4L2_COLORSPACE_SRGB
,
112 .ycbcr_enc
= V4L2_YCBCR_ENC_601
,
113 .quantization
= V4L2_QUANTIZATION_LIM_RANGE
,
114 .xfer_func
= V4L2_XFER_FUNC_SRGB
,
119 struct i2c_client
*client
;
123 struct v4l2_subdev sd
;
124 #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
125 struct media_pad pad
;
128 struct v4l2_ctrl
*auto_awb
;
129 struct v4l2_ctrl
*auto_exp
;
130 struct v4l2_ctrl
*hblank
;
131 struct v4l2_ctrl
*vblank
;
132 struct v4l2_ctrl_handler ctrls
;
134 /* Output image format and sizes. */
135 struct v4l2_mbus_framefmt fmt
;
138 /* Protects power up/down sequences. */
139 struct mutex pwr_mutex
;
142 /* Protects stream on/off sequences. */
143 struct mutex stream_mutex
;
146 /* Flags to mark HW settings as not yet applied. */
149 /* Clock provider and system clock frequency. */
153 struct gpio_desc
*oe
;
154 struct gpio_desc
*standby
;
155 struct gpio_desc
*reset
;
158 #define sd_to_mt9v111(__sd) container_of((__sd), struct mt9v111_dev, sd)
161 * mt9v111_mbus_fmt - List all media bus formats supported by the driver.
163 * Only list the media bus code here. The image sizes are freely configurable
164 * in the pixel array sizes range.
166 * The desired frame interval, in the supported frame interval range, is
167 * obtained by configuring blanking as the sensor does not have a PLL but
168 * only a fixed clock divider that generates the output pixel clock.
170 static struct mt9v111_mbus_fmt
{
172 } mt9v111_formats
[] = {
174 .code
= MEDIA_BUS_FMT_UYVY8_2X8
,
177 .code
= MEDIA_BUS_FMT_YUYV8_2X8
,
180 .code
= MEDIA_BUS_FMT_VYUY8_2X8
,
183 .code
= MEDIA_BUS_FMT_YVYU8_2X8
,
187 static u32 mt9v111_frame_intervals
[] = {5, 10, 15, 20, 30};
190 * mt9v111_frame_sizes - List sensor's supported resolutions.
192 * Resolution generated through decimation in the IFP block from the
193 * full VGA pixel array.
195 static struct v4l2_rect mt9v111_frame_sizes
[] = {
218 /* --- Device I/O access --- */
220 static int __mt9v111_read(struct i2c_client
*c
, u8 reg
, u16
*val
)
222 struct i2c_msg msg
[2];
226 msg
[0].addr
= c
->addr
;
231 msg
[1].addr
= c
->addr
;
232 msg
[1].flags
= I2C_M_RD
;
234 msg
[1].buf
= (char *)&buf
;
236 ret
= i2c_transfer(c
->adapter
, msg
, 2);
238 dev_err(&c
->dev
, "i2c read transfer error: %d\n", ret
);
242 *val
= be16_to_cpu(buf
);
244 dev_dbg(&c
->dev
, "%s: %x=%x\n", __func__
, reg
, *val
);
249 static int __mt9v111_write(struct i2c_client
*c
, u8 reg
, u16 val
)
262 msg
.buf
= (char *)buf
;
264 dev_dbg(&c
->dev
, "%s: %x = %x%x\n", __func__
, reg
, buf
[1], buf
[2]);
266 ret
= i2c_transfer(c
->adapter
, &msg
, 1);
268 dev_err(&c
->dev
, "i2c write transfer error: %d\n", ret
);
275 static int __mt9v111_addr_space_select(struct i2c_client
*c
, u16 addr_space
)
277 struct v4l2_subdev
*sd
= i2c_get_clientdata(c
);
278 struct mt9v111_dev
*mt9v111
= sd_to_mt9v111(sd
);
282 if (mt9v111
->addr_space
== addr_space
)
285 ret
= __mt9v111_write(c
, MT9V111_R01_ADDR_SPACE
, addr_space
);
289 /* Verify address space has been updated */
290 ret
= __mt9v111_read(c
, MT9V111_R01_ADDR_SPACE
, &val
);
294 if (val
!= addr_space
)
297 mt9v111
->addr_space
= addr_space
;
302 static int mt9v111_read(struct i2c_client
*c
, u8 addr_space
, u8 reg
, u16
*val
)
306 /* Select register address space first. */
307 ret
= __mt9v111_addr_space_select(c
, addr_space
);
311 ret
= __mt9v111_read(c
, reg
, val
);
318 static int mt9v111_write(struct i2c_client
*c
, u8 addr_space
, u8 reg
, u16 val
)
322 /* Select register address space first. */
323 ret
= __mt9v111_addr_space_select(c
, addr_space
);
327 ret
= __mt9v111_write(c
, reg
, val
);
334 static int mt9v111_update(struct i2c_client
*c
, u8 addr_space
, u8 reg
,
340 /* Select register address space first. */
341 ret
= __mt9v111_addr_space_select(c
, addr_space
);
345 /* Read the current register value, then update it. */
346 ret
= __mt9v111_read(c
, reg
, ¤t_val
);
350 current_val
&= ~mask
;
351 current_val
|= (val
& mask
);
352 ret
= __mt9v111_write(c
, reg
, current_val
);
359 /* --- Sensor HW operations --- */
361 static int __mt9v111_power_on(struct v4l2_subdev
*sd
)
363 struct mt9v111_dev
*mt9v111
= sd_to_mt9v111(sd
);
366 ret
= clk_prepare_enable(mt9v111
->clk
);
370 clk_set_rate(mt9v111
->clk
, mt9v111
->sysclk
);
372 gpiod_set_value(mt9v111
->standby
, 0);
373 usleep_range(500, 1000);
375 gpiod_set_value(mt9v111
->oe
, 1);
376 usleep_range(500, 1000);
381 static int __mt9v111_power_off(struct v4l2_subdev
*sd
)
383 struct mt9v111_dev
*mt9v111
= sd_to_mt9v111(sd
);
385 gpiod_set_value(mt9v111
->oe
, 0);
386 usleep_range(500, 1000);
388 gpiod_set_value(mt9v111
->standby
, 1);
389 usleep_range(500, 1000);
391 clk_disable_unprepare(mt9v111
->clk
);
396 static int __mt9v111_hw_reset(struct mt9v111_dev
*mt9v111
)
401 gpiod_set_value(mt9v111
->reset
, 1);
402 usleep_range(500, 1000);
404 gpiod_set_value(mt9v111
->reset
, 0);
405 usleep_range(500, 1000);
410 static int __mt9v111_sw_reset(struct mt9v111_dev
*mt9v111
)
412 struct i2c_client
*c
= mt9v111
->client
;
415 /* Software reset core and IFP blocks. */
417 ret
= mt9v111_update(c
, MT9V111_R01_CORE
,
418 MT9V111_CORE_R0D_CORE_RESET
,
419 MT9V111_CORE_R0D_CORE_RESET_MASK
, 1);
422 usleep_range(500, 1000);
424 ret
= mt9v111_update(c
, MT9V111_R01_CORE
,
425 MT9V111_CORE_R0D_CORE_RESET
,
426 MT9V111_CORE_R0D_CORE_RESET_MASK
, 0);
429 usleep_range(500, 1000);
431 ret
= mt9v111_update(c
, MT9V111_R01_IFP
,
432 MT9V111_IFP_R07_IFP_RESET
,
433 MT9V111_IFP_R07_IFP_RESET_MASK
, 1);
436 usleep_range(500, 1000);
438 ret
= mt9v111_update(c
, MT9V111_R01_IFP
,
439 MT9V111_IFP_R07_IFP_RESET
,
440 MT9V111_IFP_R07_IFP_RESET_MASK
, 0);
443 usleep_range(500, 1000);
448 static int mt9v111_calc_frame_rate(struct mt9v111_dev
*mt9v111
,
449 struct v4l2_fract
*tpf
)
451 unsigned int fps
= tpf
->numerator
?
452 tpf
->denominator
/ tpf
->numerator
:
454 unsigned int best_diff
;
455 unsigned int frm_cols
;
456 unsigned int row_pclk
;
457 unsigned int best_fps
;
466 /* Approximate to the closest supported frame interval. */
468 for (i
= 0, idx
= 0; i
< ARRAY_SIZE(mt9v111_frame_intervals
); i
++) {
469 diff
= abs(fps
- mt9v111_frame_intervals
[i
]);
470 if (diff
< best_diff
) {
475 fps
= mt9v111_frame_intervals
[idx
];
478 * The sensor does not provide a PLL circuitry and pixel clock is
479 * generated dividing the master clock source by two.
481 * Trow = (W + Hblank + 114) * 2 * (1 / SYSCLK)
482 * TFrame = Trow * (H + Vblank + 2)
484 * FPS = (SYSCLK / 2) / (Trow * (H + Vblank + 2))
486 * This boils down to tune H and V blanks to best approximate the
489 * Test all available H/V blank values, until we reach the
490 * desired frame rate.
492 best_fps
= vb
= hb
= 0;
493 pclk
= DIV_ROUND_CLOSEST(mt9v111
->sysclk
, 2);
494 row_pclk
= MT9V111_PIXEL_ARRAY_WIDTH
+ 7 + MT9V111_CORE_R04_WIN_H_OFFS
;
495 frm_cols
= MT9V111_PIXEL_ARRAY_HEIGHT
+ 7 + MT9V111_CORE_R03_WIN_V_OFFS
;
498 for (vb
= MT9V111_CORE_R06_MIN_VBLANK
;
499 vb
< MT9V111_CORE_R06_MAX_VBLANK
; vb
++) {
500 for (hb
= MT9V111_CORE_R05_MIN_HBLANK
;
501 hb
< MT9V111_CORE_R05_MAX_HBLANK
; hb
+= 10) {
502 unsigned int t_frame
= (row_pclk
+ hb
) *
504 unsigned int t_fps
= DIV_ROUND_CLOSEST(pclk
, t_frame
);
506 diff
= abs(fps
- t_fps
);
507 if (diff
< best_diff
) {
520 ret
= v4l2_ctrl_s_ctrl_int64(mt9v111
->hblank
, hb
);
524 ret
= v4l2_ctrl_s_ctrl_int64(mt9v111
->vblank
, vb
);
529 tpf
->denominator
= best_fps
;
534 static int mt9v111_hw_config(struct mt9v111_dev
*mt9v111
)
536 struct i2c_client
*c
= mt9v111
->client
;
540 /* Force device reset. */
541 ret
= __mt9v111_hw_reset(mt9v111
);
543 ret
= __mt9v111_sw_reset(mt9v111
);
547 /* Configure internal clock sample rate. */
548 ret
= mt9v111
->sysclk
< DIV_ROUND_CLOSEST(MT9V111_MAX_CLKIN
, 2) ?
549 mt9v111_update(c
, MT9V111_R01_CORE
,
550 MT9V111_CORE_R07_OUT_CTRL
,
551 MT9V111_CORE_R07_OUT_CTRL_SAMPLE
, 1) :
552 mt9v111_update(c
, MT9V111_R01_CORE
,
553 MT9V111_CORE_R07_OUT_CTRL
,
554 MT9V111_CORE_R07_OUT_CTRL_SAMPLE
, 0);
559 * Configure output image format components ordering.
561 * TODO: IFP block can also output several RGB permutations, we only
562 * support YUYV permutations at the moment.
564 switch (mt9v111
->fmt
.code
) {
565 case MEDIA_BUS_FMT_YUYV8_2X8
:
566 outfmtctrl2
= MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC
;
568 case MEDIA_BUS_FMT_VYUY8_2X8
:
569 outfmtctrl2
= MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR
;
571 case MEDIA_BUS_FMT_YVYU8_2X8
:
572 outfmtctrl2
= MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC
|
573 MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR
;
575 case MEDIA_BUS_FMT_UYVY8_2X8
:
581 ret
= mt9v111_update(c
, MT9V111_R01_IFP
, MT9V111_IFP_R3A_OUTFMT_CTRL2
,
582 MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_MASK
,
588 * Do not change default sensor's core configuration:
589 * output the whole 640x480 pixel array, skip 18 columns and 6 rows.
591 * Instead, control the output image size through IFP block.
593 * TODO: No zoom&pan support. Currently we control the output image
594 * size only through decimation, with no zoom support.
596 ret
= mt9v111_write(c
, MT9V111_R01_IFP
, MT9V111_IFP_RA5_HPAN
,
597 MT9V111_IFP_DECIMATION_FREEZE
);
601 ret
= mt9v111_write(c
, MT9V111_R01_IFP
, MT9V111_IFP_RA8_VPAN
,
602 MT9V111_IFP_DECIMATION_FREEZE
);
606 ret
= mt9v111_write(c
, MT9V111_R01_IFP
, MT9V111_IFP_RA6_HZOOM
,
607 MT9V111_IFP_DECIMATION_FREEZE
|
608 MT9V111_PIXEL_ARRAY_WIDTH
);
612 ret
= mt9v111_write(c
, MT9V111_R01_IFP
, MT9V111_IFP_RA9_VZOOM
,
613 MT9V111_IFP_DECIMATION_FREEZE
|
614 MT9V111_PIXEL_ARRAY_HEIGHT
);
618 ret
= mt9v111_write(c
, MT9V111_R01_IFP
, MT9V111_IFP_RA7_HOUT
,
619 MT9V111_IFP_DECIMATION_FREEZE
|
624 ret
= mt9v111_write(c
, MT9V111_R01_IFP
, MT9V111_IFP_RAA_VOUT
,
625 mt9v111
->fmt
.height
);
629 /* Apply controls to set auto exp, auto awb and timings */
630 ret
= v4l2_ctrl_handler_setup(&mt9v111
->ctrls
);
635 * Set pixel integration time to the whole frame time.
636 * This value controls the the shutter delay when running with AE
637 * disabled. If longer than frame time, it affects the output
640 return mt9v111_write(c
, MT9V111_R01_CORE
, MT9V111_CORE_R09_PIXEL_INT
,
641 MT9V111_PIXEL_ARRAY_HEIGHT
);
644 /* --- V4L2 subdev operations --- */
646 static int mt9v111_s_power(struct v4l2_subdev
*sd
, int on
)
648 struct mt9v111_dev
*mt9v111
= sd_to_mt9v111(sd
);
652 mutex_lock(&mt9v111
->pwr_mutex
);
655 * Make sure we're transitioning from 0 to 1, or viceversa,
656 * before actually changing the power state.
658 pwr_count
= mt9v111
->pwr_count
;
659 pwr_count
+= on
? 1 : -1;
660 if (pwr_count
== !!on
) {
661 ret
= on
? __mt9v111_power_on(sd
) :
662 __mt9v111_power_off(sd
);
664 /* All went well, updated power counter. */
665 mt9v111
->pwr_count
= pwr_count
;
667 mutex_unlock(&mt9v111
->pwr_mutex
);
673 * Update power counter to keep track of how many nested calls we
676 WARN_ON(pwr_count
< 0 || pwr_count
> 1);
677 mt9v111
->pwr_count
= pwr_count
;
679 mutex_unlock(&mt9v111
->pwr_mutex
);
684 static int mt9v111_s_stream(struct v4l2_subdev
*subdev
, int enable
)
686 struct mt9v111_dev
*mt9v111
= sd_to_mt9v111(subdev
);
689 mutex_lock(&mt9v111
->stream_mutex
);
691 if (mt9v111
->streaming
== enable
) {
692 mutex_unlock(&mt9v111
->stream_mutex
);
696 ret
= mt9v111_s_power(subdev
, enable
);
700 if (enable
&& mt9v111
->pending
) {
701 ret
= mt9v111_hw_config(mt9v111
);
706 * No need to update control here as far as only H/VBLANK are
707 * supported and immediately programmed to registers in .s_ctrl
710 mt9v111
->pending
= false;
713 mt9v111
->streaming
= enable
? true : false;
714 mutex_unlock(&mt9v111
->stream_mutex
);
719 mutex_unlock(&mt9v111
->stream_mutex
);
724 static int mt9v111_s_frame_interval(struct v4l2_subdev
*sd
,
725 struct v4l2_subdev_frame_interval
*ival
)
727 struct mt9v111_dev
*mt9v111
= sd_to_mt9v111(sd
);
728 struct v4l2_fract
*tpf
= &ival
->interval
;
729 unsigned int fps
= tpf
->numerator
?
730 tpf
->denominator
/ tpf
->numerator
:
732 unsigned int max_fps
;
737 mutex_lock(&mt9v111
->stream_mutex
);
739 if (mt9v111
->streaming
) {
740 mutex_unlock(&mt9v111
->stream_mutex
);
744 if (mt9v111
->fps
== fps
) {
745 mutex_unlock(&mt9v111
->stream_mutex
);
749 /* Make sure frame rate/image sizes constraints are respected. */
750 if (mt9v111
->fmt
.width
< QVGA_WIDTH
&&
751 mt9v111
->fmt
.height
< QVGA_HEIGHT
)
753 else if (mt9v111
->fmt
.width
< CIF_WIDTH
&&
754 mt9v111
->fmt
.height
< CIF_HEIGHT
)
757 max_fps
= mt9v111
->sysclk
<
758 DIV_ROUND_CLOSEST(MT9V111_MAX_CLKIN
, 2) ? 15 :
762 mutex_unlock(&mt9v111
->stream_mutex
);
766 mt9v111_calc_frame_rate(mt9v111
, tpf
);
769 mt9v111
->pending
= true;
771 mutex_unlock(&mt9v111
->stream_mutex
);
776 static int mt9v111_g_frame_interval(struct v4l2_subdev
*sd
,
777 struct v4l2_subdev_frame_interval
*ival
)
779 struct mt9v111_dev
*mt9v111
= sd_to_mt9v111(sd
);
780 struct v4l2_fract
*tpf
= &ival
->interval
;
782 mutex_lock(&mt9v111
->stream_mutex
);
785 tpf
->denominator
= mt9v111
->fps
;
787 mutex_unlock(&mt9v111
->stream_mutex
);
792 static struct v4l2_mbus_framefmt
*__mt9v111_get_pad_format(
793 struct mt9v111_dev
*mt9v111
,
794 struct v4l2_subdev_pad_config
*cfg
,
796 enum v4l2_subdev_format_whence which
)
799 case V4L2_SUBDEV_FORMAT_TRY
:
800 #if IS_ENABLED(CONFIG_VIDEO_V4L2_SUBDEV_API)
801 return v4l2_subdev_get_try_format(&mt9v111
->sd
, cfg
, pad
);
803 return &cfg
->try_fmt
;
805 case V4L2_SUBDEV_FORMAT_ACTIVE
:
806 return &mt9v111
->fmt
;
812 static int mt9v111_enum_mbus_code(struct v4l2_subdev
*subdev
,
813 struct v4l2_subdev_pad_config
*cfg
,
814 struct v4l2_subdev_mbus_code_enum
*code
)
816 if (code
->pad
|| code
->index
> ARRAY_SIZE(mt9v111_formats
) - 1)
819 code
->code
= mt9v111_formats
[code
->index
].code
;
824 static int mt9v111_enum_frame_interval(struct v4l2_subdev
*sd
,
825 struct v4l2_subdev_pad_config
*cfg
,
826 struct v4l2_subdev_frame_interval_enum
*fie
)
830 if (fie
->pad
|| fie
->index
>= ARRAY_SIZE(mt9v111_frame_intervals
))
833 for (i
= 0; i
< ARRAY_SIZE(mt9v111_frame_sizes
); i
++)
834 if (fie
->width
== mt9v111_frame_sizes
[i
].width
&&
835 fie
->height
== mt9v111_frame_sizes
[i
].height
)
838 if (i
== ARRAY_SIZE(mt9v111_frame_sizes
))
841 fie
->interval
.numerator
= 1;
842 fie
->interval
.denominator
= mt9v111_frame_intervals
[fie
->index
];
847 static int mt9v111_enum_frame_size(struct v4l2_subdev
*subdev
,
848 struct v4l2_subdev_pad_config
*cfg
,
849 struct v4l2_subdev_frame_size_enum
*fse
)
851 if (fse
->pad
|| fse
->index
>= ARRAY_SIZE(mt9v111_frame_sizes
))
854 fse
->min_width
= mt9v111_frame_sizes
[fse
->index
].width
;
855 fse
->max_width
= mt9v111_frame_sizes
[fse
->index
].width
;
856 fse
->min_height
= mt9v111_frame_sizes
[fse
->index
].height
;
857 fse
->max_height
= mt9v111_frame_sizes
[fse
->index
].height
;
862 static int mt9v111_get_format(struct v4l2_subdev
*subdev
,
863 struct v4l2_subdev_pad_config
*cfg
,
864 struct v4l2_subdev_format
*format
)
866 struct mt9v111_dev
*mt9v111
= sd_to_mt9v111(subdev
);
871 mutex_lock(&mt9v111
->stream_mutex
);
872 format
->format
= *__mt9v111_get_pad_format(mt9v111
, cfg
, format
->pad
,
874 mutex_unlock(&mt9v111
->stream_mutex
);
879 static int mt9v111_set_format(struct v4l2_subdev
*subdev
,
880 struct v4l2_subdev_pad_config
*cfg
,
881 struct v4l2_subdev_format
*format
)
883 struct mt9v111_dev
*mt9v111
= sd_to_mt9v111(subdev
);
884 struct v4l2_mbus_framefmt new_fmt
;
885 struct v4l2_mbus_framefmt
*__fmt
;
886 unsigned int best_fit
= ~0L;
887 unsigned int idx
= 0;
890 mutex_lock(&mt9v111
->stream_mutex
);
891 if (mt9v111
->streaming
) {
892 mutex_unlock(&mt9v111
->stream_mutex
);
897 mutex_unlock(&mt9v111
->stream_mutex
);
901 /* Update mbus format code and sizes. */
902 for (i
= 0; i
< ARRAY_SIZE(mt9v111_formats
); i
++) {
903 if (format
->format
.code
== mt9v111_formats
[i
].code
) {
904 new_fmt
.code
= mt9v111_formats
[i
].code
;
908 if (i
== ARRAY_SIZE(mt9v111_formats
))
909 new_fmt
.code
= mt9v111_formats
[0].code
;
911 for (i
= 0; i
< ARRAY_SIZE(mt9v111_frame_sizes
); i
++) {
912 unsigned int fit
= abs(mt9v111_frame_sizes
[i
].width
-
913 format
->format
.width
) +
914 abs(mt9v111_frame_sizes
[i
].height
-
915 format
->format
.height
);
916 if (fit
< best_fit
) {
924 new_fmt
.width
= mt9v111_frame_sizes
[idx
].width
;
925 new_fmt
.height
= mt9v111_frame_sizes
[idx
].height
;
927 /* Update the device (or pad) format if it has changed. */
928 __fmt
= __mt9v111_get_pad_format(mt9v111
, cfg
, format
->pad
,
931 /* Format hasn't changed, stop here. */
932 if (__fmt
->code
== new_fmt
.code
&&
933 __fmt
->width
== new_fmt
.width
&&
934 __fmt
->height
== new_fmt
.height
)
937 /* Update the format and sizes, then mark changes as pending. */
938 __fmt
->code
= new_fmt
.code
;
939 __fmt
->width
= new_fmt
.width
;
940 __fmt
->height
= new_fmt
.height
;
942 if (format
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
943 mt9v111
->pending
= true;
945 dev_dbg(mt9v111
->dev
, "%s: mbus_code: %x - (%ux%u)\n",
946 __func__
, __fmt
->code
, __fmt
->width
, __fmt
->height
);
949 format
->format
= *__fmt
;
951 mutex_unlock(&mt9v111
->stream_mutex
);
956 static int mt9v111_init_cfg(struct v4l2_subdev
*subdev
,
957 struct v4l2_subdev_pad_config
*cfg
)
959 cfg
->try_fmt
= mt9v111_def_fmt
;
964 static const struct v4l2_subdev_core_ops mt9v111_core_ops
= {
965 .s_power
= mt9v111_s_power
,
968 static const struct v4l2_subdev_video_ops mt9v111_video_ops
= {
969 .s_stream
= mt9v111_s_stream
,
970 .s_frame_interval
= mt9v111_s_frame_interval
,
971 .g_frame_interval
= mt9v111_g_frame_interval
,
974 static const struct v4l2_subdev_pad_ops mt9v111_pad_ops
= {
975 .init_cfg
= mt9v111_init_cfg
,
976 .enum_mbus_code
= mt9v111_enum_mbus_code
,
977 .enum_frame_size
= mt9v111_enum_frame_size
,
978 .enum_frame_interval
= mt9v111_enum_frame_interval
,
979 .get_fmt
= mt9v111_get_format
,
980 .set_fmt
= mt9v111_set_format
,
983 static const struct v4l2_subdev_ops mt9v111_ops
= {
984 .core
= &mt9v111_core_ops
,
985 .video
= &mt9v111_video_ops
,
986 .pad
= &mt9v111_pad_ops
,
989 #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
990 static const struct media_entity_operations mt9v111_subdev_entity_ops
= {
991 .link_validate
= v4l2_subdev_link_validate
,
995 /* --- V4L2 ctrl --- */
996 static int mt9v111_s_ctrl(struct v4l2_ctrl
*ctrl
)
998 struct mt9v111_dev
*mt9v111
= container_of(ctrl
->handler
,
1003 mutex_lock(&mt9v111
->pwr_mutex
);
1005 * If sensor is powered down, just cache new control values,
1006 * no actual register access.
1008 if (!mt9v111
->pwr_count
) {
1009 mt9v111
->pending
= true;
1010 mutex_unlock(&mt9v111
->pwr_mutex
);
1013 mutex_unlock(&mt9v111
->pwr_mutex
);
1016 * Flickering control gets disabled if both auto exp and auto awb
1017 * are disabled too. If any of the two is enabled, enable it.
1019 * Disabling flickering when ae and awb are off allows a more precise
1020 * control of the programmed frame rate.
1022 if (mt9v111
->auto_exp
->is_new
|| mt9v111
->auto_awb
->is_new
) {
1023 if (mt9v111
->auto_exp
->val
== V4L2_EXPOSURE_MANUAL
&&
1024 mt9v111
->auto_awb
->val
== V4L2_WHITE_BALANCE_MANUAL
)
1025 ret
= mt9v111_update(mt9v111
->client
, MT9V111_R01_IFP
,
1026 MT9V111_IFP_R08_OUTFMT_CTRL
,
1027 MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER
,
1030 ret
= mt9v111_update(mt9v111
->client
, MT9V111_R01_IFP
,
1031 MT9V111_IFP_R08_OUTFMT_CTRL
,
1032 MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER
,
1040 case V4L2_CID_AUTO_WHITE_BALANCE
:
1041 ret
= mt9v111_update(mt9v111
->client
, MT9V111_R01_IFP
,
1042 MT9V111_IFP_R06_OPMODE_CTRL
,
1043 MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN
,
1044 ctrl
->val
== V4L2_WHITE_BALANCE_AUTO
?
1045 MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN
: 0);
1047 case V4L2_CID_EXPOSURE_AUTO
:
1048 ret
= mt9v111_update(mt9v111
->client
, MT9V111_R01_IFP
,
1049 MT9V111_IFP_R06_OPMODE_CTRL
,
1050 MT9V111_IFP_R06_OPMODE_CTRL_AE_EN
,
1051 ctrl
->val
== V4L2_EXPOSURE_AUTO
?
1052 MT9V111_IFP_R06_OPMODE_CTRL_AE_EN
: 0);
1054 case V4L2_CID_HBLANK
:
1055 ret
= mt9v111_update(mt9v111
->client
, MT9V111_R01_CORE
,
1056 MT9V111_CORE_R05_HBLANK
,
1057 MT9V111_CORE_R05_MAX_HBLANK
,
1058 mt9v111
->hblank
->val
);
1060 case V4L2_CID_VBLANK
:
1061 ret
= mt9v111_update(mt9v111
->client
, MT9V111_R01_CORE
,
1062 MT9V111_CORE_R06_VBLANK
,
1063 MT9V111_CORE_R06_MAX_VBLANK
,
1064 mt9v111
->vblank
->val
);
1071 static const struct v4l2_ctrl_ops mt9v111_ctrl_ops
= {
1072 .s_ctrl
= mt9v111_s_ctrl
,
1075 static int mt9v111_chip_probe(struct mt9v111_dev
*mt9v111
)
1080 ret
= __mt9v111_power_on(&mt9v111
->sd
);
1084 ret
= mt9v111_read(mt9v111
->client
, MT9V111_R01_CORE
,
1085 MT9V111_CORE_RFF_CHIP_VER
, &val
);
1089 if ((val
>> 8) != MT9V111_CHIP_ID_HIGH
&&
1090 (val
& 0xff) != MT9V111_CHIP_ID_LOW
) {
1091 dev_err(mt9v111
->dev
,
1092 "Unable to identify MT9V111 chip: 0x%2x%2x\n",
1093 val
>> 8, val
& 0xff);
1098 dev_dbg(mt9v111
->dev
, "Chip identified: 0x%2x%2x\n",
1099 val
>> 8, val
& 0xff);
1102 __mt9v111_power_off(&mt9v111
->sd
);
1107 static int mt9v111_probe(struct i2c_client
*client
)
1109 struct mt9v111_dev
*mt9v111
;
1110 struct v4l2_fract tpf
;
1113 mt9v111
= devm_kzalloc(&client
->dev
, sizeof(*mt9v111
), GFP_KERNEL
);
1117 mt9v111
->dev
= &client
->dev
;
1118 mt9v111
->client
= client
;
1120 mt9v111
->clk
= devm_clk_get(&client
->dev
, NULL
);
1121 if (IS_ERR(mt9v111
->clk
))
1122 return PTR_ERR(mt9v111
->clk
);
1124 mt9v111
->sysclk
= clk_get_rate(mt9v111
->clk
);
1125 if (mt9v111
->sysclk
> MT9V111_MAX_CLKIN
)
1128 mt9v111
->oe
= devm_gpiod_get_optional(&client
->dev
, "enable",
1130 if (IS_ERR(mt9v111
->oe
)) {
1131 dev_err(&client
->dev
, "Unable to get GPIO \"enable\": %ld\n",
1132 PTR_ERR(mt9v111
->oe
));
1133 return PTR_ERR(mt9v111
->oe
);
1136 mt9v111
->standby
= devm_gpiod_get_optional(&client
->dev
, "standby",
1138 if (IS_ERR(mt9v111
->standby
)) {
1139 dev_err(&client
->dev
, "Unable to get GPIO \"standby\": %ld\n",
1140 PTR_ERR(mt9v111
->standby
));
1141 return PTR_ERR(mt9v111
->standby
);
1144 mt9v111
->reset
= devm_gpiod_get_optional(&client
->dev
, "reset",
1146 if (IS_ERR(mt9v111
->reset
)) {
1147 dev_err(&client
->dev
, "Unable to get GPIO \"reset\": %ld\n",
1148 PTR_ERR(mt9v111
->reset
));
1149 return PTR_ERR(mt9v111
->reset
);
1152 mutex_init(&mt9v111
->pwr_mutex
);
1153 mutex_init(&mt9v111
->stream_mutex
);
1155 v4l2_ctrl_handler_init(&mt9v111
->ctrls
, 5);
1157 mt9v111
->auto_awb
= v4l2_ctrl_new_std(&mt9v111
->ctrls
,
1159 V4L2_CID_AUTO_WHITE_BALANCE
,
1161 V4L2_WHITE_BALANCE_AUTO
);
1162 mt9v111
->auto_exp
= v4l2_ctrl_new_std_menu(&mt9v111
->ctrls
,
1164 V4L2_CID_EXPOSURE_AUTO
,
1165 V4L2_EXPOSURE_MANUAL
,
1166 0, V4L2_EXPOSURE_AUTO
);
1167 mt9v111
->hblank
= v4l2_ctrl_new_std(&mt9v111
->ctrls
, &mt9v111_ctrl_ops
,
1169 MT9V111_CORE_R05_MIN_HBLANK
,
1170 MT9V111_CORE_R05_MAX_HBLANK
, 1,
1171 MT9V111_CORE_R05_DEF_HBLANK
);
1172 mt9v111
->vblank
= v4l2_ctrl_new_std(&mt9v111
->ctrls
, &mt9v111_ctrl_ops
,
1174 MT9V111_CORE_R06_MIN_VBLANK
,
1175 MT9V111_CORE_R06_MAX_VBLANK
, 1,
1176 MT9V111_CORE_R06_DEF_VBLANK
);
1178 /* PIXEL_RATE is fixed: just expose it to user space. */
1179 v4l2_ctrl_new_std(&mt9v111
->ctrls
, &mt9v111_ctrl_ops
,
1180 V4L2_CID_PIXEL_RATE
, 0,
1181 DIV_ROUND_CLOSEST(mt9v111
->sysclk
, 2), 1,
1182 DIV_ROUND_CLOSEST(mt9v111
->sysclk
, 2));
1184 if (mt9v111
->ctrls
.error
) {
1185 ret
= mt9v111
->ctrls
.error
;
1186 goto error_free_ctrls
;
1188 mt9v111
->sd
.ctrl_handler
= &mt9v111
->ctrls
;
1190 /* Start with default configuration: 640x480 UYVY. */
1191 mt9v111
->fmt
= mt9v111_def_fmt
;
1193 /* Re-calculate blankings for 640x480@15fps. */
1196 tpf
.denominator
= mt9v111
->fps
;
1197 mt9v111_calc_frame_rate(mt9v111
, &tpf
);
1199 mt9v111
->pwr_count
= 0;
1200 mt9v111
->addr_space
= MT9V111_R01_IFP
;
1201 mt9v111
->pending
= true;
1203 v4l2_i2c_subdev_init(&mt9v111
->sd
, client
, &mt9v111_ops
);
1205 #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
1206 mt9v111
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1207 mt9v111
->sd
.entity
.ops
= &mt9v111_subdev_entity_ops
;
1208 mt9v111
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1210 mt9v111
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1211 ret
= media_entity_pads_init(&mt9v111
->sd
.entity
, 1, &mt9v111
->pad
);
1213 goto error_free_entity
;
1216 ret
= mt9v111_chip_probe(mt9v111
);
1218 goto error_free_entity
;
1220 ret
= v4l2_async_register_subdev(&mt9v111
->sd
);
1222 goto error_free_entity
;
1227 #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
1228 media_entity_cleanup(&mt9v111
->sd
.entity
);
1232 v4l2_ctrl_handler_free(&mt9v111
->ctrls
);
1234 mutex_destroy(&mt9v111
->pwr_mutex
);
1235 mutex_destroy(&mt9v111
->stream_mutex
);
1240 static int mt9v111_remove(struct i2c_client
*client
)
1242 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1243 struct mt9v111_dev
*mt9v111
= sd_to_mt9v111(sd
);
1245 v4l2_async_unregister_subdev(sd
);
1247 #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
1248 media_entity_cleanup(&sd
->entity
);
1251 v4l2_ctrl_handler_free(&mt9v111
->ctrls
);
1253 mutex_destroy(&mt9v111
->pwr_mutex
);
1254 mutex_destroy(&mt9v111
->stream_mutex
);
1256 devm_gpiod_put(mt9v111
->dev
, mt9v111
->oe
);
1257 devm_gpiod_put(mt9v111
->dev
, mt9v111
->standby
);
1258 devm_gpiod_put(mt9v111
->dev
, mt9v111
->reset
);
1260 devm_clk_put(mt9v111
->dev
, mt9v111
->clk
);
1265 static const struct of_device_id mt9v111_of_match
[] = {
1266 { .compatible
= "aptina,mt9v111", },
1270 static struct i2c_driver mt9v111_driver
= {
1273 .of_match_table
= mt9v111_of_match
,
1275 .probe_new
= mt9v111_probe
,
1276 .remove
= mt9v111_remove
,
1279 module_i2c_driver(mt9v111_driver
);
1281 MODULE_DESCRIPTION("V4L2 sensor driver for Aptina MT9V111");
1282 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
1283 MODULE_LICENSE("GPL v2");