1 // SPDX-License-Identifier: GPL-2.0-only
3 * OmniVision ov9282 Camera Sensor Driver
5 * Copyright (C) 2021 Intel Corporation
7 #include <asm/unaligned.h>
10 #include <linux/delay.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regulator/consumer.h>
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-event.h>
18 #include <media/v4l2-fwnode.h>
19 #include <media/v4l2-subdev.h>
22 #define OV9282_REG_MODE_SELECT 0x0100
23 #define OV9282_MODE_STANDBY 0x00
24 #define OV9282_MODE_STREAMING 0x01
26 #define OV9282_REG_PLL_CTRL_0D 0x030d
27 #define OV9282_PLL_CTRL_0D_RAW8 0x60
28 #define OV9282_PLL_CTRL_0D_RAW10 0x50
30 #define OV9282_REG_TIMING_HTS 0x380c
31 #define OV9282_TIMING_HTS_MAX 0x7fff
34 #define OV9282_REG_LPFR 0x380e
37 #define OV9282_REG_ID 0x300a
38 #define OV9282_ID 0x9281
40 /* Exposure control */
41 #define OV9282_REG_EXPOSURE 0x3500
42 #define OV9282_EXPOSURE_MIN 1
43 #define OV9282_EXPOSURE_OFFSET 12
44 #define OV9282_EXPOSURE_STEP 1
45 #define OV9282_EXPOSURE_DEFAULT 0x0282
47 /* Analog gain control */
48 #define OV9282_REG_AGAIN 0x3509
49 #define OV9282_AGAIN_MIN 0x10
50 #define OV9282_AGAIN_MAX 0xff
51 #define OV9282_AGAIN_STEP 1
52 #define OV9282_AGAIN_DEFAULT 0x10
54 /* Group hold register */
55 #define OV9282_REG_HOLD 0x3308
57 #define OV9282_REG_ANA_CORE_2 0x3662
58 #define OV9282_ANA_CORE2_RAW8 0x07
59 #define OV9282_ANA_CORE2_RAW10 0x05
61 #define OV9282_REG_TIMING_FORMAT_1 0x3820
62 #define OV9282_REG_TIMING_FORMAT_2 0x3821
63 #define OV9282_FLIP_BIT BIT(2)
65 #define OV9282_REG_MIPI_CTRL00 0x4800
66 #define OV9282_GATED_CLOCK BIT(5)
68 /* Input clock rate */
69 #define OV9282_INCLK_RATE 24000000
71 /* CSI2 HW configuration */
72 #define OV9282_LINK_FREQ 400000000
73 #define OV9282_NUM_DATA_LANES 2
76 #define OV9282_PIXEL_RATE_10BIT (OV9282_LINK_FREQ * 2 * \
77 OV9282_NUM_DATA_LANES / 10)
78 #define OV9282_PIXEL_RATE_8BIT (OV9282_LINK_FREQ * 2 * \
79 OV9282_NUM_DATA_LANES / 8)
82 * OV9282 native and active pixel array size.
83 * 8 dummy rows/columns on each edge of a 1280x800 active array
85 #define OV9282_NATIVE_WIDTH 1296U
86 #define OV9282_NATIVE_HEIGHT 816U
87 #define OV9282_PIXEL_ARRAY_LEFT 8U
88 #define OV9282_PIXEL_ARRAY_TOP 8U
89 #define OV9282_PIXEL_ARRAY_WIDTH 1280U
90 #define OV9282_PIXEL_ARRAY_HEIGHT 800U
92 #define OV9282_REG_MIN 0x00
93 #define OV9282_REG_MAX 0xfffff
95 static const char * const ov9282_supply_names[] = {
96 "avdd", /* Analog power */
97 "dovdd", /* Digital I/O power */
98 "dvdd", /* Digital core power */
101 #define OV9282_NUM_SUPPLIES ARRAY_SIZE(ov9282_supply_names)
104 * struct ov9282_reg - ov9282 sensor register
105 * @address: Register address
106 * @val: Register value
114 * struct ov9282_reg_list - ov9282 sensor register list
115 * @num_of_regs: Number of registers in the list
116 * @regs: Pointer to register list
118 struct ov9282_reg_list {
120 const struct ov9282_reg *regs;
124 * struct ov9282_mode - ov9282 sensor mode structure
125 * @width: Frame width
126 * @height: Frame height
127 * @hblank_min: Minimum horizontal blanking in lines for non-continuous[0] and
128 * continuous[1] clock modes
129 * @vblank: Vertical blanking in lines
130 * @vblank_min: Minimum vertical blanking in lines
131 * @vblank_max: Maximum vertical blanking in lines
132 * @link_freq_idx: Link frequency index
133 * @crop: on-sensor cropping for this mode
134 * @reg_list: Register list for sensor mode
144 struct v4l2_rect crop;
145 struct ov9282_reg_list reg_list;
149 * struct ov9282 - ov9282 sensor device structure
150 * @dev: Pointer to generic device
151 * @sd: V4L2 sub-device
152 * @pad: Media pad. Only one pad supported
153 * @reset_gpio: Sensor reset gpio
154 * @inclk: Sensor input clock
155 * @supplies: Regulator supplies for the sensor
156 * @ctrl_handler: V4L2 control handler
157 * @link_freq_ctrl: Pointer to link frequency control
158 * @hblank_ctrl: Pointer to horizontal blanking control
159 * @vblank_ctrl: Pointer to vertical blanking control
160 * @exp_ctrl: Pointer to exposure control
161 * @again_ctrl: Pointer to analog gain control
162 * @pixel_rate: Pointer to pixel rate control
163 * @vblank: Vertical blanking in lines
164 * @noncontinuous_clock: Selection of CSI2 noncontinuous clock mode
165 * @cur_mode: Pointer to current selected sensor mode
166 * @code: Mbus code currently selected
167 * @mutex: Mutex for serializing sensor controls
168 * @streaming: Flag indicating streaming state
172 struct v4l2_subdev sd;
173 struct media_pad pad;
174 struct gpio_desc *reset_gpio;
176 struct regulator_bulk_data supplies[OV9282_NUM_SUPPLIES];
177 struct v4l2_ctrl_handler ctrl_handler;
178 struct v4l2_ctrl *link_freq_ctrl;
179 struct v4l2_ctrl *hblank_ctrl;
180 struct v4l2_ctrl *vblank_ctrl;
182 struct v4l2_ctrl *exp_ctrl;
183 struct v4l2_ctrl *again_ctrl;
185 struct v4l2_ctrl *pixel_rate;
187 bool noncontinuous_clock;
188 const struct ov9282_mode *cur_mode;
194 static const s64 link_freq[] = {
201 * Note: Do NOT include a software reset (0x0103, 0x01) in any of these
202 * register arrays as some settings are written as part of ov9282_power_on,
203 * and the reset will clear them.
205 static const struct ov9282_reg common_regs[] = {
269 static struct ov9282_reg_list common_regs_list = {
270 .num_of_regs = ARRAY_SIZE(common_regs),
274 #define MODE_1280_800 0
275 #define MODE_1280_720 1
276 #define MODE_640_400 2
278 #define DEFAULT_MODE MODE_1280_720
280 /* Sensor mode registers */
281 static const struct ov9282_reg mode_1280x800_regs[] = {
312 static const struct ov9282_reg mode_1280x720_regs[] = {
343 static const struct ov9282_reg mode_640x400_regs[] = {
373 /* Supported sensor mode configurations */
374 static const struct ov9282_mode supported_modes[] = {
378 .hblank_min = { 250, 176 },
384 .left = OV9282_PIXEL_ARRAY_LEFT,
385 .top = OV9282_PIXEL_ARRAY_TOP,
390 .num_of_regs = ARRAY_SIZE(mode_1280x800_regs),
391 .regs = mode_1280x800_regs,
397 .hblank_min = { 250, 176 },
404 * Note that this mode takes the top 720 lines from the
405 * 800 of the sensor. It does not take a middle crop.
407 .left = OV9282_PIXEL_ARRAY_LEFT,
408 .top = OV9282_PIXEL_ARRAY_TOP,
413 .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
414 .regs = mode_1280x720_regs,
420 .hblank_min = { 890, 816 },
426 .left = OV9282_PIXEL_ARRAY_LEFT,
427 .top = OV9282_PIXEL_ARRAY_TOP,
432 .num_of_regs = ARRAY_SIZE(mode_640x400_regs),
433 .regs = mode_640x400_regs,
439 * to_ov9282() - ov9282 V4L2 sub-device to ov9282 device.
440 * @subdev: pointer to ov9282 V4L2 sub-device
442 * Return: pointer to ov9282 device
444 static inline struct ov9282 *to_ov9282(struct v4l2_subdev *subdev)
446 return container_of(subdev, struct ov9282, sd);
450 * ov9282_read_reg() - Read registers.
451 * @ov9282: pointer to ov9282 device
452 * @reg: register address
453 * @len: length of bytes to read. Max supported bytes is 4
454 * @val: pointer to register value to be filled.
456 * Return: 0 if successful, error code otherwise.
458 static int ov9282_read_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 *val)
460 struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
461 struct i2c_msg msgs[2] = {0};
462 u8 addr_buf[2] = {0};
463 u8 data_buf[4] = {0};
466 if (WARN_ON(len > 4))
469 put_unaligned_be16(reg, addr_buf);
471 /* Write register address */
472 msgs[0].addr = client->addr;
474 msgs[0].len = ARRAY_SIZE(addr_buf);
475 msgs[0].buf = addr_buf;
477 /* Read data from register */
478 msgs[1].addr = client->addr;
479 msgs[1].flags = I2C_M_RD;
481 msgs[1].buf = &data_buf[4 - len];
483 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
484 if (ret != ARRAY_SIZE(msgs))
487 *val = get_unaligned_be32(data_buf);
493 * ov9282_write_reg() - Write register
494 * @ov9282: pointer to ov9282 device
495 * @reg: register address
496 * @len: length of bytes. Max supported bytes is 4
497 * @val: register value
499 * Return: 0 if successful, error code otherwise.
501 static int ov9282_write_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 val)
503 struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
506 if (WARN_ON(len > 4))
509 put_unaligned_be16(reg, buf);
510 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
511 if (i2c_master_send(client, buf, len + 2) != len + 2)
518 * ov9282_write_regs() - Write a list of registers
519 * @ov9282: pointer to ov9282 device
520 * @regs: list of registers to be written
521 * @len: length of registers array
523 * Return: 0 if successful, error code otherwise.
525 static int ov9282_write_regs(struct ov9282 *ov9282,
526 const struct ov9282_reg *regs, u32 len)
531 for (i = 0; i < len; i++) {
532 ret = ov9282_write_reg(ov9282, regs[i].address, 1, regs[i].val);
541 * ov9282_update_controls() - Update control ranges based on streaming mode
542 * @ov9282: pointer to ov9282 device
543 * @mode: pointer to ov9282_mode sensor mode
544 * @fmt: pointer to the requested mode
546 * Return: 0 if successful, error code otherwise.
548 static int ov9282_update_controls(struct ov9282 *ov9282,
549 const struct ov9282_mode *mode,
550 const struct v4l2_subdev_format *fmt)
556 ret = __v4l2_ctrl_s_ctrl(ov9282->link_freq_ctrl, mode->link_freq_idx);
560 pixel_rate = (fmt->format.code == MEDIA_BUS_FMT_Y10_1X10) ?
561 OV9282_PIXEL_RATE_10BIT : OV9282_PIXEL_RATE_8BIT;
562 ret = __v4l2_ctrl_modify_range(ov9282->pixel_rate, pixel_rate,
563 pixel_rate, 1, pixel_rate);
567 hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1];
568 ret = __v4l2_ctrl_modify_range(ov9282->hblank_ctrl, hblank_min,
569 OV9282_TIMING_HTS_MAX - mode->width, 1,
574 return __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min,
575 mode->vblank_max, 1, mode->vblank);
579 * ov9282_update_exp_gain() - Set updated exposure and gain
580 * @ov9282: pointer to ov9282 device
581 * @exposure: updated exposure value
582 * @gain: updated analog gain value
584 * Return: 0 if successful, error code otherwise.
586 static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
590 dev_dbg(ov9282->dev, "Set exp %u, analog gain %u",
593 ret = ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 1);
597 ret = ov9282_write_reg(ov9282, OV9282_REG_EXPOSURE, 3, exposure << 4);
599 goto error_release_group_hold;
601 ret = ov9282_write_reg(ov9282, OV9282_REG_AGAIN, 1, gain);
603 error_release_group_hold:
604 ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 0);
609 static int ov9282_set_ctrl_hflip(struct ov9282 *ov9282, int value)
612 int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1,
618 current_val |= OV9282_FLIP_BIT;
620 current_val &= ~OV9282_FLIP_BIT;
622 return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1,
626 static int ov9282_set_ctrl_vflip(struct ov9282 *ov9282, int value)
629 int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1,
635 current_val |= OV9282_FLIP_BIT;
637 current_val &= ~OV9282_FLIP_BIT;
639 return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1,
644 * ov9282_set_ctrl() - Set subdevice control
645 * @ctrl: pointer to v4l2_ctrl structure
647 * Supported controls:
649 * - cluster controls:
650 * - V4L2_CID_ANALOGUE_GAIN
651 * - V4L2_CID_EXPOSURE
653 * Return: 0 if successful, error code otherwise.
655 static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
657 struct ov9282 *ov9282 =
658 container_of(ctrl->handler, struct ov9282, ctrl_handler);
665 case V4L2_CID_VBLANK:
666 ov9282->vblank = ov9282->vblank_ctrl->val;
668 dev_dbg(ov9282->dev, "Received vblank %u, new lpfr %u",
670 ov9282->vblank + ov9282->cur_mode->height);
672 ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl,
675 ov9282->cur_mode->height -
676 OV9282_EXPOSURE_OFFSET,
677 1, OV9282_EXPOSURE_DEFAULT);
681 /* Set controls only if sensor is in power on state */
682 if (!pm_runtime_get_if_in_use(ov9282->dev))
686 case V4L2_CID_EXPOSURE:
687 exposure = ctrl->val;
688 analog_gain = ov9282->again_ctrl->val;
690 dev_dbg(ov9282->dev, "Received exp %u, analog gain %u",
691 exposure, analog_gain);
693 ret = ov9282_update_exp_gain(ov9282, exposure, analog_gain);
695 case V4L2_CID_VBLANK:
696 lpfr = ov9282->vblank + ov9282->cur_mode->height;
697 ret = ov9282_write_reg(ov9282, OV9282_REG_LPFR, 2, lpfr);
700 ret = ov9282_set_ctrl_hflip(ov9282, ctrl->val);
703 ret = ov9282_set_ctrl_vflip(ov9282, ctrl->val);
705 case V4L2_CID_HBLANK:
706 ret = ov9282_write_reg(ov9282, OV9282_REG_TIMING_HTS, 2,
707 (ctrl->val + ov9282->cur_mode->width) >> 1);
710 dev_err(ov9282->dev, "Invalid control %d", ctrl->id);
714 pm_runtime_put(ov9282->dev);
719 /* V4l2 subdevice control ops*/
720 static const struct v4l2_ctrl_ops ov9282_ctrl_ops = {
721 .s_ctrl = ov9282_set_ctrl,
725 * ov9282_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
726 * @sd: pointer to ov9282 V4L2 sub-device structure
727 * @sd_state: V4L2 sub-device configuration
728 * @code: V4L2 sub-device code enumeration need to be filled
730 * Return: 0 if successful, error code otherwise.
732 static int ov9282_enum_mbus_code(struct v4l2_subdev *sd,
733 struct v4l2_subdev_state *sd_state,
734 struct v4l2_subdev_mbus_code_enum *code)
736 switch (code->index) {
738 code->code = MEDIA_BUS_FMT_Y10_1X10;
741 code->code = MEDIA_BUS_FMT_Y8_1X8;
751 * ov9282_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
752 * @sd: pointer to ov9282 V4L2 sub-device structure
753 * @sd_state: V4L2 sub-device configuration
754 * @fsize: V4L2 sub-device size enumeration need to be filled
756 * Return: 0 if successful, error code otherwise.
758 static int ov9282_enum_frame_size(struct v4l2_subdev *sd,
759 struct v4l2_subdev_state *sd_state,
760 struct v4l2_subdev_frame_size_enum *fsize)
762 if (fsize->index >= ARRAY_SIZE(supported_modes))
765 if (fsize->code != MEDIA_BUS_FMT_Y10_1X10 &&
766 fsize->code != MEDIA_BUS_FMT_Y8_1X8)
769 fsize->min_width = supported_modes[fsize->index].width;
770 fsize->max_width = fsize->min_width;
771 fsize->min_height = supported_modes[fsize->index].height;
772 fsize->max_height = fsize->min_height;
778 * ov9282_fill_pad_format() - Fill subdevice pad format
779 * from selected sensor mode
780 * @ov9282: pointer to ov9282 device
781 * @mode: pointer to ov9282_mode sensor mode
782 * @code: mbus code to be stored
783 * @fmt: V4L2 sub-device format need to be filled
785 static void ov9282_fill_pad_format(struct ov9282 *ov9282,
786 const struct ov9282_mode *mode,
788 struct v4l2_subdev_format *fmt)
790 fmt->format.width = mode->width;
791 fmt->format.height = mode->height;
792 fmt->format.code = code;
793 fmt->format.field = V4L2_FIELD_NONE;
794 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
795 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
796 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
797 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
801 * ov9282_get_pad_format() - Get subdevice pad format
802 * @sd: pointer to ov9282 V4L2 sub-device structure
803 * @sd_state: V4L2 sub-device configuration
804 * @fmt: V4L2 sub-device format need to be set
806 * Return: 0 if successful, error code otherwise.
808 static int ov9282_get_pad_format(struct v4l2_subdev *sd,
809 struct v4l2_subdev_state *sd_state,
810 struct v4l2_subdev_format *fmt)
812 struct ov9282 *ov9282 = to_ov9282(sd);
814 mutex_lock(&ov9282->mutex);
816 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
817 struct v4l2_mbus_framefmt *framefmt;
819 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
820 fmt->format = *framefmt;
822 ov9282_fill_pad_format(ov9282, ov9282->cur_mode, ov9282->code,
826 mutex_unlock(&ov9282->mutex);
832 * ov9282_set_pad_format() - Set subdevice pad format
833 * @sd: pointer to ov9282 V4L2 sub-device structure
834 * @sd_state: V4L2 sub-device configuration
835 * @fmt: V4L2 sub-device format need to be set
837 * Return: 0 if successful, error code otherwise.
839 static int ov9282_set_pad_format(struct v4l2_subdev *sd,
840 struct v4l2_subdev_state *sd_state,
841 struct v4l2_subdev_format *fmt)
843 struct ov9282 *ov9282 = to_ov9282(sd);
844 const struct ov9282_mode *mode;
848 mutex_lock(&ov9282->mutex);
850 mode = v4l2_find_nearest_size(supported_modes,
851 ARRAY_SIZE(supported_modes),
855 if (fmt->format.code == MEDIA_BUS_FMT_Y8_1X8)
856 code = MEDIA_BUS_FMT_Y8_1X8;
858 code = MEDIA_BUS_FMT_Y10_1X10;
860 ov9282_fill_pad_format(ov9282, mode, code, fmt);
862 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
863 struct v4l2_mbus_framefmt *framefmt;
865 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
866 *framefmt = fmt->format;
868 ret = ov9282_update_controls(ov9282, mode, fmt);
870 ov9282->cur_mode = mode;
875 mutex_unlock(&ov9282->mutex);
881 * ov9282_init_pad_cfg() - Initialize sub-device pad configuration
882 * @sd: pointer to ov9282 V4L2 sub-device structure
883 * @sd_state: V4L2 sub-device configuration
885 * Return: 0 if successful, error code otherwise.
887 static int ov9282_init_pad_cfg(struct v4l2_subdev *sd,
888 struct v4l2_subdev_state *sd_state)
890 struct ov9282 *ov9282 = to_ov9282(sd);
891 struct v4l2_subdev_format fmt = { 0 };
893 fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
894 ov9282_fill_pad_format(ov9282, &supported_modes[DEFAULT_MODE],
897 return ov9282_set_pad_format(sd, sd_state, &fmt);
900 static const struct v4l2_rect *
901 __ov9282_get_pad_crop(struct ov9282 *ov9282,
902 struct v4l2_subdev_state *sd_state,
903 unsigned int pad, enum v4l2_subdev_format_whence which)
906 case V4L2_SUBDEV_FORMAT_TRY:
907 return v4l2_subdev_get_try_crop(&ov9282->sd, sd_state, pad);
908 case V4L2_SUBDEV_FORMAT_ACTIVE:
909 return &ov9282->cur_mode->crop;
915 static int ov9282_get_selection(struct v4l2_subdev *sd,
916 struct v4l2_subdev_state *sd_state,
917 struct v4l2_subdev_selection *sel)
919 switch (sel->target) {
920 case V4L2_SEL_TGT_CROP: {
921 struct ov9282 *ov9282 = to_ov9282(sd);
923 mutex_lock(&ov9282->mutex);
924 sel->r = *__ov9282_get_pad_crop(ov9282, sd_state, sel->pad,
926 mutex_unlock(&ov9282->mutex);
931 case V4L2_SEL_TGT_NATIVE_SIZE:
934 sel->r.width = OV9282_NATIVE_WIDTH;
935 sel->r.height = OV9282_NATIVE_HEIGHT;
939 case V4L2_SEL_TGT_CROP_DEFAULT:
940 case V4L2_SEL_TGT_CROP_BOUNDS:
941 sel->r.top = OV9282_PIXEL_ARRAY_TOP;
942 sel->r.left = OV9282_PIXEL_ARRAY_LEFT;
943 sel->r.width = OV9282_PIXEL_ARRAY_WIDTH;
944 sel->r.height = OV9282_PIXEL_ARRAY_HEIGHT;
953 * ov9282_start_streaming() - Start sensor stream
954 * @ov9282: pointer to ov9282 device
956 * Return: 0 if successful, error code otherwise.
958 static int ov9282_start_streaming(struct ov9282 *ov9282)
960 const struct ov9282_reg bitdepth_regs[2][2] = {
962 {OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW10},
963 {OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW10},
965 {OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW8},
966 {OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW8},
969 const struct ov9282_reg_list *reg_list;
973 /* Write common registers */
974 ret = ov9282_write_regs(ov9282, common_regs_list.regs,
975 common_regs_list.num_of_regs);
977 dev_err(ov9282->dev, "fail to write common registers");
981 bitdepth_index = ov9282->code == MEDIA_BUS_FMT_Y10_1X10 ? 0 : 1;
982 ret = ov9282_write_regs(ov9282, bitdepth_regs[bitdepth_index], 2);
984 dev_err(ov9282->dev, "fail to write bitdepth regs");
988 /* Write sensor mode registers */
989 reg_list = &ov9282->cur_mode->reg_list;
990 ret = ov9282_write_regs(ov9282, reg_list->regs, reg_list->num_of_regs);
992 dev_err(ov9282->dev, "fail to write initial registers");
996 /* Setup handler will write actual exposure and gain */
997 ret = __v4l2_ctrl_handler_setup(ov9282->sd.ctrl_handler);
999 dev_err(ov9282->dev, "fail to setup handler");
1003 /* Start streaming */
1004 ret = ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
1005 1, OV9282_MODE_STREAMING);
1007 dev_err(ov9282->dev, "fail to start streaming");
1015 * ov9282_stop_streaming() - Stop sensor stream
1016 * @ov9282: pointer to ov9282 device
1018 * Return: 0 if successful, error code otherwise.
1020 static int ov9282_stop_streaming(struct ov9282 *ov9282)
1022 return ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
1023 1, OV9282_MODE_STANDBY);
1027 * ov9282_set_stream() - Enable sensor streaming
1028 * @sd: pointer to ov9282 subdevice
1029 * @enable: set to enable sensor streaming
1031 * Return: 0 if successful, error code otherwise.
1033 static int ov9282_set_stream(struct v4l2_subdev *sd, int enable)
1035 struct ov9282 *ov9282 = to_ov9282(sd);
1038 mutex_lock(&ov9282->mutex);
1040 if (ov9282->streaming == enable) {
1041 mutex_unlock(&ov9282->mutex);
1046 ret = pm_runtime_resume_and_get(ov9282->dev);
1050 ret = ov9282_start_streaming(ov9282);
1052 goto error_power_off;
1054 ov9282_stop_streaming(ov9282);
1055 pm_runtime_put(ov9282->dev);
1058 ov9282->streaming = enable;
1060 mutex_unlock(&ov9282->mutex);
1065 pm_runtime_put(ov9282->dev);
1067 mutex_unlock(&ov9282->mutex);
1073 * ov9282_detect() - Detect ov9282 sensor
1074 * @ov9282: pointer to ov9282 device
1076 * Return: 0 if successful, -EIO if sensor id does not match
1078 static int ov9282_detect(struct ov9282 *ov9282)
1083 ret = ov9282_read_reg(ov9282, OV9282_REG_ID + 1, 1, &val);
1086 ret = ov9282_read_reg(ov9282, OV9282_REG_ID, 1, &msb);
1091 if (val != OV9282_ID) {
1092 dev_err(ov9282->dev, "chip id mismatch: %x!=%x",
1100 static int ov9282_configure_regulators(struct ov9282 *ov9282)
1104 for (i = 0; i < OV9282_NUM_SUPPLIES; i++)
1105 ov9282->supplies[i].supply = ov9282_supply_names[i];
1107 return devm_regulator_bulk_get(ov9282->dev,
1108 OV9282_NUM_SUPPLIES,
1113 * ov9282_parse_hw_config() - Parse HW configuration and check if supported
1114 * @ov9282: pointer to ov9282 device
1116 * Return: 0 if successful, error code otherwise.
1118 static int ov9282_parse_hw_config(struct ov9282 *ov9282)
1120 struct fwnode_handle *fwnode = dev_fwnode(ov9282->dev);
1121 struct v4l2_fwnode_endpoint bus_cfg = {
1122 .bus_type = V4L2_MBUS_CSI2_DPHY
1124 struct fwnode_handle *ep;
1132 /* Request optional reset pin */
1133 ov9282->reset_gpio = devm_gpiod_get_optional(ov9282->dev, "reset",
1135 if (IS_ERR(ov9282->reset_gpio)) {
1136 dev_err(ov9282->dev, "failed to get reset gpio %ld",
1137 PTR_ERR(ov9282->reset_gpio));
1138 return PTR_ERR(ov9282->reset_gpio);
1141 /* Get sensor input clock */
1142 ov9282->inclk = devm_clk_get(ov9282->dev, NULL);
1143 if (IS_ERR(ov9282->inclk)) {
1144 dev_err(ov9282->dev, "could not get inclk");
1145 return PTR_ERR(ov9282->inclk);
1148 ret = ov9282_configure_regulators(ov9282);
1150 return dev_err_probe(ov9282->dev, ret,
1151 "Failed to get power regulators\n");
1153 rate = clk_get_rate(ov9282->inclk);
1154 if (rate != OV9282_INCLK_RATE) {
1155 dev_err(ov9282->dev, "inclk frequency mismatch");
1159 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1163 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1164 fwnode_handle_put(ep);
1168 ov9282->noncontinuous_clock =
1169 bus_cfg.bus.mipi_csi2.flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
1171 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV9282_NUM_DATA_LANES) {
1172 dev_err(ov9282->dev,
1173 "number of CSI2 data lanes %d is not supported",
1174 bus_cfg.bus.mipi_csi2.num_data_lanes);
1176 goto done_endpoint_free;
1179 if (!bus_cfg.nr_of_link_frequencies) {
1180 dev_err(ov9282->dev, "no link frequencies defined");
1182 goto done_endpoint_free;
1185 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
1186 if (bus_cfg.link_frequencies[i] == OV9282_LINK_FREQ)
1187 goto done_endpoint_free;
1192 v4l2_fwnode_endpoint_free(&bus_cfg);
1197 /* V4l2 subdevice ops */
1198 static const struct v4l2_subdev_core_ops ov9282_core_ops = {
1199 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1200 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1203 static const struct v4l2_subdev_video_ops ov9282_video_ops = {
1204 .s_stream = ov9282_set_stream,
1207 static const struct v4l2_subdev_pad_ops ov9282_pad_ops = {
1208 .init_cfg = ov9282_init_pad_cfg,
1209 .enum_mbus_code = ov9282_enum_mbus_code,
1210 .enum_frame_size = ov9282_enum_frame_size,
1211 .get_fmt = ov9282_get_pad_format,
1212 .set_fmt = ov9282_set_pad_format,
1213 .get_selection = ov9282_get_selection,
1216 static const struct v4l2_subdev_ops ov9282_subdev_ops = {
1217 .core = &ov9282_core_ops,
1218 .video = &ov9282_video_ops,
1219 .pad = &ov9282_pad_ops,
1223 * ov9282_power_on() - Sensor power on sequence
1224 * @dev: pointer to i2c device
1226 * Return: 0 if successful, error code otherwise.
1228 static int ov9282_power_on(struct device *dev)
1230 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1231 struct ov9282 *ov9282 = to_ov9282(sd);
1234 ret = regulator_bulk_enable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1236 dev_err(dev, "Failed to enable regulators\n");
1240 usleep_range(400, 600);
1242 gpiod_set_value_cansleep(ov9282->reset_gpio, 1);
1244 ret = clk_prepare_enable(ov9282->inclk);
1246 dev_err(ov9282->dev, "fail to enable inclk");
1250 usleep_range(400, 600);
1252 ret = ov9282_write_reg(ov9282, OV9282_REG_MIPI_CTRL00, 1,
1253 ov9282->noncontinuous_clock ?
1254 OV9282_GATED_CLOCK : 0);
1256 dev_err(ov9282->dev, "fail to write MIPI_CTRL00");
1263 clk_disable_unprepare(ov9282->inclk);
1265 gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
1267 regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1273 * ov9282_power_off() - Sensor power off sequence
1274 * @dev: pointer to i2c device
1276 * Return: 0 if successful, error code otherwise.
1278 static int ov9282_power_off(struct device *dev)
1280 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1281 struct ov9282 *ov9282 = to_ov9282(sd);
1283 gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
1285 clk_disable_unprepare(ov9282->inclk);
1287 regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1293 * ov9282_init_controls() - Initialize sensor subdevice controls
1294 * @ov9282: pointer to ov9282 device
1296 * Return: 0 if successful, error code otherwise.
1298 static int ov9282_init_controls(struct ov9282 *ov9282)
1300 struct v4l2_ctrl_handler *ctrl_hdlr = &ov9282->ctrl_handler;
1301 const struct ov9282_mode *mode = ov9282->cur_mode;
1302 struct v4l2_fwnode_device_properties props;
1307 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
1311 /* Serialize controls with sensor device */
1312 ctrl_hdlr->lock = &ov9282->mutex;
1314 /* Initialize exposure and gain */
1315 lpfr = mode->vblank + mode->height;
1316 ov9282->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1319 OV9282_EXPOSURE_MIN,
1320 lpfr - OV9282_EXPOSURE_OFFSET,
1321 OV9282_EXPOSURE_STEP,
1322 OV9282_EXPOSURE_DEFAULT);
1324 ov9282->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1326 V4L2_CID_ANALOGUE_GAIN,
1330 OV9282_AGAIN_DEFAULT);
1332 v4l2_ctrl_cluster(2, &ov9282->exp_ctrl);
1334 ov9282->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1341 v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_VFLIP,
1344 v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_HFLIP,
1347 /* Read only controls */
1348 ov9282->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
1349 V4L2_CID_PIXEL_RATE,
1350 OV9282_PIXEL_RATE_10BIT,
1351 OV9282_PIXEL_RATE_10BIT, 1,
1352 OV9282_PIXEL_RATE_10BIT);
1354 ov9282->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1357 ARRAY_SIZE(link_freq) -
1359 mode->link_freq_idx,
1361 if (ov9282->link_freq_ctrl)
1362 ov9282->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1364 hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1];
1365 ov9282->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1369 OV9282_TIMING_HTS_MAX - mode->width,
1372 ret = v4l2_fwnode_device_parse(ov9282->dev, &props);
1374 /* Failure sets ctrl_hdlr->error, which we check afterwards anyway */
1375 v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov9282_ctrl_ops,
1379 if (ctrl_hdlr->error || ret) {
1380 dev_err(ov9282->dev, "control init failed: %d",
1382 v4l2_ctrl_handler_free(ctrl_hdlr);
1383 return ctrl_hdlr->error;
1386 ov9282->sd.ctrl_handler = ctrl_hdlr;
1392 * ov9282_probe() - I2C client device binding
1393 * @client: pointer to i2c client device
1395 * Return: 0 if successful, error code otherwise.
1397 static int ov9282_probe(struct i2c_client *client)
1399 struct ov9282 *ov9282;
1402 ov9282 = devm_kzalloc(&client->dev, sizeof(*ov9282), GFP_KERNEL);
1406 ov9282->dev = &client->dev;
1408 /* Initialize subdev */
1409 v4l2_i2c_subdev_init(&ov9282->sd, client, &ov9282_subdev_ops);
1410 v4l2_i2c_subdev_set_name(&ov9282->sd, client,
1411 device_get_match_data(ov9282->dev), NULL);
1413 ret = ov9282_parse_hw_config(ov9282);
1415 dev_err(ov9282->dev, "HW configuration is not supported");
1419 mutex_init(&ov9282->mutex);
1421 ret = ov9282_power_on(ov9282->dev);
1423 dev_err(ov9282->dev, "failed to power-on the sensor");
1424 goto error_mutex_destroy;
1427 /* Check module identity */
1428 ret = ov9282_detect(ov9282);
1430 dev_err(ov9282->dev, "failed to find sensor: %d", ret);
1431 goto error_power_off;
1434 /* Set default mode to first mode */
1435 ov9282->cur_mode = &supported_modes[DEFAULT_MODE];
1436 ov9282->code = MEDIA_BUS_FMT_Y10_1X10;
1437 ov9282->vblank = ov9282->cur_mode->vblank;
1439 ret = ov9282_init_controls(ov9282);
1441 dev_err(ov9282->dev, "failed to init controls: %d", ret);
1442 goto error_power_off;
1445 /* Initialize subdev */
1446 ov9282->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1447 V4L2_SUBDEV_FL_HAS_EVENTS;
1448 ov9282->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1450 /* Initialize source pad */
1451 ov9282->pad.flags = MEDIA_PAD_FL_SOURCE;
1452 ret = media_entity_pads_init(&ov9282->sd.entity, 1, &ov9282->pad);
1454 dev_err(ov9282->dev, "failed to init entity pads: %d", ret);
1455 goto error_handler_free;
1458 ret = v4l2_async_register_subdev_sensor(&ov9282->sd);
1460 dev_err(ov9282->dev,
1461 "failed to register async subdev: %d", ret);
1462 goto error_media_entity;
1465 pm_runtime_set_active(ov9282->dev);
1466 pm_runtime_enable(ov9282->dev);
1467 pm_runtime_idle(ov9282->dev);
1472 media_entity_cleanup(&ov9282->sd.entity);
1474 v4l2_ctrl_handler_free(ov9282->sd.ctrl_handler);
1476 ov9282_power_off(ov9282->dev);
1477 error_mutex_destroy:
1478 mutex_destroy(&ov9282->mutex);
1484 * ov9282_remove() - I2C client device unbinding
1485 * @client: pointer to I2C client device
1487 * Return: 0 if successful, error code otherwise.
1489 static void ov9282_remove(struct i2c_client *client)
1491 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1492 struct ov9282 *ov9282 = to_ov9282(sd);
1494 v4l2_async_unregister_subdev(sd);
1495 media_entity_cleanup(&sd->entity);
1496 v4l2_ctrl_handler_free(sd->ctrl_handler);
1498 pm_runtime_disable(&client->dev);
1499 if (!pm_runtime_status_suspended(&client->dev))
1500 ov9282_power_off(&client->dev);
1501 pm_runtime_set_suspended(&client->dev);
1503 mutex_destroy(&ov9282->mutex);
1506 static const struct dev_pm_ops ov9282_pm_ops = {
1507 SET_RUNTIME_PM_OPS(ov9282_power_off, ov9282_power_on, NULL)
1510 static const struct of_device_id ov9282_of_match[] = {
1511 { .compatible = "ovti,ov9281", .data = "ov9281" },
1512 { .compatible = "ovti,ov9282", .data = "ov9282" },
1516 MODULE_DEVICE_TABLE(of, ov9282_of_match);
1518 static struct i2c_driver ov9282_driver = {
1519 .probe = ov9282_probe,
1520 .remove = ov9282_remove,
1523 .pm = &ov9282_pm_ops,
1524 .of_match_table = ov9282_of_match,
1528 module_i2c_driver(ov9282_driver);
1530 MODULE_DESCRIPTION("OmniVision ov9282 sensor driver");
1531 MODULE_LICENSE("GPL");