1 // SPDX-License-Identifier: GPL-2.0-only
3 * OmniVision ov9282 Camera Sensor Driver
5 * Copyright (C) 2021 Intel Corporation
7 #include <linux/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 25
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
171 struct v4l2_subdev sd;
172 struct media_pad pad;
173 struct gpio_desc *reset_gpio;
175 struct regulator_bulk_data supplies[OV9282_NUM_SUPPLIES];
176 struct v4l2_ctrl_handler ctrl_handler;
177 struct v4l2_ctrl *link_freq_ctrl;
178 struct v4l2_ctrl *hblank_ctrl;
179 struct v4l2_ctrl *vblank_ctrl;
181 struct v4l2_ctrl *exp_ctrl;
182 struct v4l2_ctrl *again_ctrl;
184 struct v4l2_ctrl *pixel_rate;
186 bool noncontinuous_clock;
187 const struct ov9282_mode *cur_mode;
192 static const s64 link_freq[] = {
199 * Note: Do NOT include a software reset (0x0103, 0x01) in any of these
200 * register arrays as some settings are written as part of ov9282_power_on,
201 * and the reset will clear them.
203 static const struct ov9282_reg common_regs[] = {
267 static struct ov9282_reg_list common_regs_list = {
268 .num_of_regs = ARRAY_SIZE(common_regs),
272 #define MODE_1280_800 0
273 #define MODE_1280_720 1
274 #define MODE_640_400 2
276 #define DEFAULT_MODE MODE_1280_720
278 /* Sensor mode registers */
279 static const struct ov9282_reg mode_1280x800_regs[] = {
310 static const struct ov9282_reg mode_1280x720_regs[] = {
341 static const struct ov9282_reg mode_640x400_regs[] = {
371 /* Supported sensor mode configurations */
372 static const struct ov9282_mode supported_modes[] = {
376 .hblank_min = { 250, 176 },
382 .left = OV9282_PIXEL_ARRAY_LEFT,
383 .top = OV9282_PIXEL_ARRAY_TOP,
388 .num_of_regs = ARRAY_SIZE(mode_1280x800_regs),
389 .regs = mode_1280x800_regs,
395 .hblank_min = { 250, 176 },
402 * Note that this mode takes the top 720 lines from the
403 * 800 of the sensor. It does not take a middle crop.
405 .left = OV9282_PIXEL_ARRAY_LEFT,
406 .top = OV9282_PIXEL_ARRAY_TOP,
411 .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
412 .regs = mode_1280x720_regs,
418 .hblank_min = { 890, 816 },
424 .left = OV9282_PIXEL_ARRAY_LEFT,
425 .top = OV9282_PIXEL_ARRAY_TOP,
430 .num_of_regs = ARRAY_SIZE(mode_640x400_regs),
431 .regs = mode_640x400_regs,
437 * to_ov9282() - ov9282 V4L2 sub-device to ov9282 device.
438 * @subdev: pointer to ov9282 V4L2 sub-device
440 * Return: pointer to ov9282 device
442 static inline struct ov9282 *to_ov9282(struct v4l2_subdev *subdev)
444 return container_of(subdev, struct ov9282, sd);
448 * ov9282_read_reg() - Read registers.
449 * @ov9282: pointer to ov9282 device
450 * @reg: register address
451 * @len: length of bytes to read. Max supported bytes is 4
452 * @val: pointer to register value to be filled.
454 * Return: 0 if successful, error code otherwise.
456 static int ov9282_read_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 *val)
458 struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
459 struct i2c_msg msgs[2] = {0};
460 u8 addr_buf[2] = {0};
461 u8 data_buf[4] = {0};
464 if (WARN_ON(len > 4))
467 put_unaligned_be16(reg, addr_buf);
469 /* Write register address */
470 msgs[0].addr = client->addr;
472 msgs[0].len = ARRAY_SIZE(addr_buf);
473 msgs[0].buf = addr_buf;
475 /* Read data from register */
476 msgs[1].addr = client->addr;
477 msgs[1].flags = I2C_M_RD;
479 msgs[1].buf = &data_buf[4 - len];
481 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
482 if (ret != ARRAY_SIZE(msgs))
485 *val = get_unaligned_be32(data_buf);
491 * ov9282_write_reg() - Write register
492 * @ov9282: pointer to ov9282 device
493 * @reg: register address
494 * @len: length of bytes. Max supported bytes is 4
495 * @val: register value
497 * Return: 0 if successful, error code otherwise.
499 static int ov9282_write_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 val)
501 struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
504 if (WARN_ON(len > 4))
507 put_unaligned_be16(reg, buf);
508 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
509 if (i2c_master_send(client, buf, len + 2) != len + 2)
516 * ov9282_write_regs() - Write a list of registers
517 * @ov9282: pointer to ov9282 device
518 * @regs: list of registers to be written
519 * @len: length of registers array
521 * Return: 0 if successful, error code otherwise.
523 static int ov9282_write_regs(struct ov9282 *ov9282,
524 const struct ov9282_reg *regs, u32 len)
529 for (i = 0; i < len; i++) {
530 ret = ov9282_write_reg(ov9282, regs[i].address, 1, regs[i].val);
539 * ov9282_update_controls() - Update control ranges based on streaming mode
540 * @ov9282: pointer to ov9282 device
541 * @mode: pointer to ov9282_mode sensor mode
542 * @fmt: pointer to the requested mode
544 * Return: 0 if successful, error code otherwise.
546 static int ov9282_update_controls(struct ov9282 *ov9282,
547 const struct ov9282_mode *mode,
548 const struct v4l2_subdev_format *fmt)
554 ret = __v4l2_ctrl_s_ctrl(ov9282->link_freq_ctrl, mode->link_freq_idx);
558 pixel_rate = (fmt->format.code == MEDIA_BUS_FMT_Y10_1X10) ?
559 OV9282_PIXEL_RATE_10BIT : OV9282_PIXEL_RATE_8BIT;
560 ret = __v4l2_ctrl_modify_range(ov9282->pixel_rate, pixel_rate,
561 pixel_rate, 1, pixel_rate);
565 hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1];
566 ret = __v4l2_ctrl_modify_range(ov9282->hblank_ctrl, hblank_min,
567 OV9282_TIMING_HTS_MAX - mode->width, 1,
572 return __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min,
573 mode->vblank_max, 1, mode->vblank);
577 * ov9282_update_exp_gain() - Set updated exposure and gain
578 * @ov9282: pointer to ov9282 device
579 * @exposure: updated exposure value
580 * @gain: updated analog gain value
582 * Return: 0 if successful, error code otherwise.
584 static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
588 dev_dbg(ov9282->dev, "Set exp %u, analog gain %u",
591 ret = ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 1);
595 ret = ov9282_write_reg(ov9282, OV9282_REG_EXPOSURE, 3, exposure << 4);
597 goto error_release_group_hold;
599 ret = ov9282_write_reg(ov9282, OV9282_REG_AGAIN, 1, gain);
601 error_release_group_hold:
602 ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 0);
607 static int ov9282_set_ctrl_hflip(struct ov9282 *ov9282, int value)
610 int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1,
616 current_val |= OV9282_FLIP_BIT;
618 current_val &= ~OV9282_FLIP_BIT;
620 return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1,
624 static int ov9282_set_ctrl_vflip(struct ov9282 *ov9282, int value)
627 int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1,
633 current_val |= OV9282_FLIP_BIT;
635 current_val &= ~OV9282_FLIP_BIT;
637 return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1,
642 * ov9282_set_ctrl() - Set subdevice control
643 * @ctrl: pointer to v4l2_ctrl structure
645 * Supported controls:
647 * - cluster controls:
648 * - V4L2_CID_ANALOGUE_GAIN
649 * - V4L2_CID_EXPOSURE
651 * Return: 0 if successful, error code otherwise.
653 static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
655 struct ov9282 *ov9282 =
656 container_of(ctrl->handler, struct ov9282, ctrl_handler);
663 case V4L2_CID_VBLANK:
664 ov9282->vblank = ov9282->vblank_ctrl->val;
666 dev_dbg(ov9282->dev, "Received vblank %u, new lpfr %u",
668 ov9282->vblank + ov9282->cur_mode->height);
670 ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl,
673 ov9282->cur_mode->height -
674 OV9282_EXPOSURE_OFFSET,
675 1, OV9282_EXPOSURE_DEFAULT);
679 /* Set controls only if sensor is in power on state */
680 if (!pm_runtime_get_if_in_use(ov9282->dev))
684 case V4L2_CID_EXPOSURE:
685 exposure = ctrl->val;
686 analog_gain = ov9282->again_ctrl->val;
688 dev_dbg(ov9282->dev, "Received exp %u, analog gain %u",
689 exposure, analog_gain);
691 ret = ov9282_update_exp_gain(ov9282, exposure, analog_gain);
693 case V4L2_CID_VBLANK:
694 lpfr = ov9282->vblank + ov9282->cur_mode->height;
695 ret = ov9282_write_reg(ov9282, OV9282_REG_LPFR, 2, lpfr);
698 ret = ov9282_set_ctrl_hflip(ov9282, ctrl->val);
701 ret = ov9282_set_ctrl_vflip(ov9282, ctrl->val);
703 case V4L2_CID_HBLANK:
704 ret = ov9282_write_reg(ov9282, OV9282_REG_TIMING_HTS, 2,
705 (ctrl->val + ov9282->cur_mode->width) >> 1);
708 dev_err(ov9282->dev, "Invalid control %d", ctrl->id);
712 pm_runtime_put(ov9282->dev);
717 /* V4l2 subdevice control ops*/
718 static const struct v4l2_ctrl_ops ov9282_ctrl_ops = {
719 .s_ctrl = ov9282_set_ctrl,
723 * ov9282_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
724 * @sd: pointer to ov9282 V4L2 sub-device structure
725 * @sd_state: V4L2 sub-device configuration
726 * @code: V4L2 sub-device code enumeration need to be filled
728 * Return: 0 if successful, error code otherwise.
730 static int ov9282_enum_mbus_code(struct v4l2_subdev *sd,
731 struct v4l2_subdev_state *sd_state,
732 struct v4l2_subdev_mbus_code_enum *code)
734 switch (code->index) {
736 code->code = MEDIA_BUS_FMT_Y10_1X10;
739 code->code = MEDIA_BUS_FMT_Y8_1X8;
749 * ov9282_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
750 * @sd: pointer to ov9282 V4L2 sub-device structure
751 * @sd_state: V4L2 sub-device configuration
752 * @fsize: V4L2 sub-device size enumeration need to be filled
754 * Return: 0 if successful, error code otherwise.
756 static int ov9282_enum_frame_size(struct v4l2_subdev *sd,
757 struct v4l2_subdev_state *sd_state,
758 struct v4l2_subdev_frame_size_enum *fsize)
760 if (fsize->index >= ARRAY_SIZE(supported_modes))
763 if (fsize->code != MEDIA_BUS_FMT_Y10_1X10 &&
764 fsize->code != MEDIA_BUS_FMT_Y8_1X8)
767 fsize->min_width = supported_modes[fsize->index].width;
768 fsize->max_width = fsize->min_width;
769 fsize->min_height = supported_modes[fsize->index].height;
770 fsize->max_height = fsize->min_height;
776 * ov9282_fill_pad_format() - Fill subdevice pad format
777 * from selected sensor mode
778 * @ov9282: pointer to ov9282 device
779 * @mode: pointer to ov9282_mode sensor mode
780 * @code: mbus code to be stored
781 * @fmt: V4L2 sub-device format need to be filled
783 static void ov9282_fill_pad_format(struct ov9282 *ov9282,
784 const struct ov9282_mode *mode,
786 struct v4l2_subdev_format *fmt)
788 fmt->format.width = mode->width;
789 fmt->format.height = mode->height;
790 fmt->format.code = code;
791 fmt->format.field = V4L2_FIELD_NONE;
792 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
793 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
794 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
795 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
799 * ov9282_get_pad_format() - Get subdevice pad format
800 * @sd: pointer to ov9282 V4L2 sub-device structure
801 * @sd_state: V4L2 sub-device configuration
802 * @fmt: V4L2 sub-device format need to be set
804 * Return: 0 if successful, error code otherwise.
806 static int ov9282_get_pad_format(struct v4l2_subdev *sd,
807 struct v4l2_subdev_state *sd_state,
808 struct v4l2_subdev_format *fmt)
810 struct ov9282 *ov9282 = to_ov9282(sd);
812 mutex_lock(&ov9282->mutex);
814 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
815 struct v4l2_mbus_framefmt *framefmt;
817 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
818 fmt->format = *framefmt;
820 ov9282_fill_pad_format(ov9282, ov9282->cur_mode, ov9282->code,
824 mutex_unlock(&ov9282->mutex);
830 * ov9282_set_pad_format() - Set subdevice pad format
831 * @sd: pointer to ov9282 V4L2 sub-device structure
832 * @sd_state: V4L2 sub-device configuration
833 * @fmt: V4L2 sub-device format need to be set
835 * Return: 0 if successful, error code otherwise.
837 static int ov9282_set_pad_format(struct v4l2_subdev *sd,
838 struct v4l2_subdev_state *sd_state,
839 struct v4l2_subdev_format *fmt)
841 struct ov9282 *ov9282 = to_ov9282(sd);
842 const struct ov9282_mode *mode;
846 mutex_lock(&ov9282->mutex);
848 mode = v4l2_find_nearest_size(supported_modes,
849 ARRAY_SIZE(supported_modes),
853 if (fmt->format.code == MEDIA_BUS_FMT_Y8_1X8)
854 code = MEDIA_BUS_FMT_Y8_1X8;
856 code = MEDIA_BUS_FMT_Y10_1X10;
858 ov9282_fill_pad_format(ov9282, mode, code, fmt);
860 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
861 struct v4l2_mbus_framefmt *framefmt;
863 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
864 *framefmt = fmt->format;
866 ret = ov9282_update_controls(ov9282, mode, fmt);
868 ov9282->cur_mode = mode;
873 mutex_unlock(&ov9282->mutex);
879 * ov9282_init_state() - Initialize sub-device state
880 * @sd: pointer to ov9282 V4L2 sub-device structure
881 * @sd_state: V4L2 sub-device configuration
883 * Return: 0 if successful, error code otherwise.
885 static int ov9282_init_state(struct v4l2_subdev *sd,
886 struct v4l2_subdev_state *sd_state)
888 struct ov9282 *ov9282 = to_ov9282(sd);
889 struct v4l2_subdev_format fmt = { 0 };
891 fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
892 ov9282_fill_pad_format(ov9282, &supported_modes[DEFAULT_MODE],
895 return ov9282_set_pad_format(sd, sd_state, &fmt);
898 static const struct v4l2_rect *
899 __ov9282_get_pad_crop(struct ov9282 *ov9282,
900 struct v4l2_subdev_state *sd_state,
901 unsigned int pad, enum v4l2_subdev_format_whence which)
904 case V4L2_SUBDEV_FORMAT_TRY:
905 return v4l2_subdev_state_get_crop(sd_state, pad);
906 case V4L2_SUBDEV_FORMAT_ACTIVE:
907 return &ov9282->cur_mode->crop;
913 static int ov9282_get_selection(struct v4l2_subdev *sd,
914 struct v4l2_subdev_state *sd_state,
915 struct v4l2_subdev_selection *sel)
917 switch (sel->target) {
918 case V4L2_SEL_TGT_CROP: {
919 struct ov9282 *ov9282 = to_ov9282(sd);
921 mutex_lock(&ov9282->mutex);
922 sel->r = *__ov9282_get_pad_crop(ov9282, sd_state, sel->pad,
924 mutex_unlock(&ov9282->mutex);
929 case V4L2_SEL_TGT_NATIVE_SIZE:
932 sel->r.width = OV9282_NATIVE_WIDTH;
933 sel->r.height = OV9282_NATIVE_HEIGHT;
937 case V4L2_SEL_TGT_CROP_DEFAULT:
938 case V4L2_SEL_TGT_CROP_BOUNDS:
939 sel->r.top = OV9282_PIXEL_ARRAY_TOP;
940 sel->r.left = OV9282_PIXEL_ARRAY_LEFT;
941 sel->r.width = OV9282_PIXEL_ARRAY_WIDTH;
942 sel->r.height = OV9282_PIXEL_ARRAY_HEIGHT;
951 * ov9282_start_streaming() - Start sensor stream
952 * @ov9282: pointer to ov9282 device
954 * Return: 0 if successful, error code otherwise.
956 static int ov9282_start_streaming(struct ov9282 *ov9282)
958 const struct ov9282_reg bitdepth_regs[2][2] = {
960 {OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW10},
961 {OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW10},
963 {OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW8},
964 {OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW8},
967 const struct ov9282_reg_list *reg_list;
971 /* Write common registers */
972 ret = ov9282_write_regs(ov9282, common_regs_list.regs,
973 common_regs_list.num_of_regs);
975 dev_err(ov9282->dev, "fail to write common registers");
979 bitdepth_index = ov9282->code == MEDIA_BUS_FMT_Y10_1X10 ? 0 : 1;
980 ret = ov9282_write_regs(ov9282, bitdepth_regs[bitdepth_index], 2);
982 dev_err(ov9282->dev, "fail to write bitdepth regs");
986 /* Write sensor mode registers */
987 reg_list = &ov9282->cur_mode->reg_list;
988 ret = ov9282_write_regs(ov9282, reg_list->regs, reg_list->num_of_regs);
990 dev_err(ov9282->dev, "fail to write initial registers");
994 /* Setup handler will write actual exposure and gain */
995 ret = __v4l2_ctrl_handler_setup(ov9282->sd.ctrl_handler);
997 dev_err(ov9282->dev, "fail to setup handler");
1001 /* Start streaming */
1002 ret = ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
1003 1, OV9282_MODE_STREAMING);
1005 dev_err(ov9282->dev, "fail to start streaming");
1013 * ov9282_stop_streaming() - Stop sensor stream
1014 * @ov9282: pointer to ov9282 device
1016 * Return: 0 if successful, error code otherwise.
1018 static int ov9282_stop_streaming(struct ov9282 *ov9282)
1020 return ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
1021 1, OV9282_MODE_STANDBY);
1025 * ov9282_set_stream() - Enable sensor streaming
1026 * @sd: pointer to ov9282 subdevice
1027 * @enable: set to enable sensor streaming
1029 * Return: 0 if successful, error code otherwise.
1031 static int ov9282_set_stream(struct v4l2_subdev *sd, int enable)
1033 struct ov9282 *ov9282 = to_ov9282(sd);
1036 mutex_lock(&ov9282->mutex);
1039 ret = pm_runtime_resume_and_get(ov9282->dev);
1043 ret = ov9282_start_streaming(ov9282);
1045 goto error_power_off;
1047 ov9282_stop_streaming(ov9282);
1048 pm_runtime_put(ov9282->dev);
1051 mutex_unlock(&ov9282->mutex);
1056 pm_runtime_put(ov9282->dev);
1058 mutex_unlock(&ov9282->mutex);
1064 * ov9282_detect() - Detect ov9282 sensor
1065 * @ov9282: pointer to ov9282 device
1067 * Return: 0 if successful, -EIO if sensor id does not match
1069 static int ov9282_detect(struct ov9282 *ov9282)
1074 ret = ov9282_read_reg(ov9282, OV9282_REG_ID, 2, &val);
1078 if (val != OV9282_ID) {
1079 dev_err(ov9282->dev, "chip id mismatch: %x!=%x",
1087 static int ov9282_configure_regulators(struct ov9282 *ov9282)
1091 for (i = 0; i < OV9282_NUM_SUPPLIES; i++)
1092 ov9282->supplies[i].supply = ov9282_supply_names[i];
1094 return devm_regulator_bulk_get(ov9282->dev,
1095 OV9282_NUM_SUPPLIES,
1100 * ov9282_parse_hw_config() - Parse HW configuration and check if supported
1101 * @ov9282: pointer to ov9282 device
1103 * Return: 0 if successful, error code otherwise.
1105 static int ov9282_parse_hw_config(struct ov9282 *ov9282)
1107 struct fwnode_handle *fwnode = dev_fwnode(ov9282->dev);
1108 struct v4l2_fwnode_endpoint bus_cfg = {
1109 .bus_type = V4L2_MBUS_CSI2_DPHY
1111 struct fwnode_handle *ep;
1119 /* Request optional reset pin */
1120 ov9282->reset_gpio = devm_gpiod_get_optional(ov9282->dev, "reset",
1122 if (IS_ERR(ov9282->reset_gpio)) {
1123 dev_err(ov9282->dev, "failed to get reset gpio %ld",
1124 PTR_ERR(ov9282->reset_gpio));
1125 return PTR_ERR(ov9282->reset_gpio);
1128 /* Get sensor input clock */
1129 ov9282->inclk = devm_clk_get(ov9282->dev, NULL);
1130 if (IS_ERR(ov9282->inclk)) {
1131 dev_err(ov9282->dev, "could not get inclk");
1132 return PTR_ERR(ov9282->inclk);
1135 ret = ov9282_configure_regulators(ov9282);
1137 return dev_err_probe(ov9282->dev, ret,
1138 "Failed to get power regulators\n");
1140 rate = clk_get_rate(ov9282->inclk);
1141 if (rate != OV9282_INCLK_RATE) {
1142 dev_err(ov9282->dev, "inclk frequency mismatch");
1146 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1150 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1151 fwnode_handle_put(ep);
1155 ov9282->noncontinuous_clock =
1156 bus_cfg.bus.mipi_csi2.flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
1158 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV9282_NUM_DATA_LANES) {
1159 dev_err(ov9282->dev,
1160 "number of CSI2 data lanes %d is not supported",
1161 bus_cfg.bus.mipi_csi2.num_data_lanes);
1163 goto done_endpoint_free;
1166 if (!bus_cfg.nr_of_link_frequencies) {
1167 dev_err(ov9282->dev, "no link frequencies defined");
1169 goto done_endpoint_free;
1172 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
1173 if (bus_cfg.link_frequencies[i] == OV9282_LINK_FREQ)
1174 goto done_endpoint_free;
1179 v4l2_fwnode_endpoint_free(&bus_cfg);
1184 /* V4l2 subdevice ops */
1185 static const struct v4l2_subdev_core_ops ov9282_core_ops = {
1186 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1187 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1190 static const struct v4l2_subdev_video_ops ov9282_video_ops = {
1191 .s_stream = ov9282_set_stream,
1194 static const struct v4l2_subdev_pad_ops ov9282_pad_ops = {
1195 .enum_mbus_code = ov9282_enum_mbus_code,
1196 .enum_frame_size = ov9282_enum_frame_size,
1197 .get_fmt = ov9282_get_pad_format,
1198 .set_fmt = ov9282_set_pad_format,
1199 .get_selection = ov9282_get_selection,
1202 static const struct v4l2_subdev_ops ov9282_subdev_ops = {
1203 .core = &ov9282_core_ops,
1204 .video = &ov9282_video_ops,
1205 .pad = &ov9282_pad_ops,
1208 static const struct v4l2_subdev_internal_ops ov9282_internal_ops = {
1209 .init_state = ov9282_init_state,
1213 * ov9282_power_on() - Sensor power on sequence
1214 * @dev: pointer to i2c device
1216 * Return: 0 if successful, error code otherwise.
1218 static int ov9282_power_on(struct device *dev)
1220 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1221 struct ov9282 *ov9282 = to_ov9282(sd);
1224 ret = regulator_bulk_enable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1226 dev_err(dev, "Failed to enable regulators\n");
1230 usleep_range(400, 600);
1232 gpiod_set_value_cansleep(ov9282->reset_gpio, 1);
1234 ret = clk_prepare_enable(ov9282->inclk);
1236 dev_err(ov9282->dev, "fail to enable inclk");
1240 usleep_range(400, 600);
1242 ret = ov9282_write_reg(ov9282, OV9282_REG_MIPI_CTRL00, 1,
1243 ov9282->noncontinuous_clock ?
1244 OV9282_GATED_CLOCK : 0);
1246 dev_err(ov9282->dev, "fail to write MIPI_CTRL00");
1253 clk_disable_unprepare(ov9282->inclk);
1255 gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
1257 regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1263 * ov9282_power_off() - Sensor power off sequence
1264 * @dev: pointer to i2c device
1266 * Return: 0 if successful, error code otherwise.
1268 static int ov9282_power_off(struct device *dev)
1270 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1271 struct ov9282 *ov9282 = to_ov9282(sd);
1273 gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
1275 clk_disable_unprepare(ov9282->inclk);
1277 regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1283 * ov9282_init_controls() - Initialize sensor subdevice controls
1284 * @ov9282: pointer to ov9282 device
1286 * Return: 0 if successful, error code otherwise.
1288 static int ov9282_init_controls(struct ov9282 *ov9282)
1290 struct v4l2_ctrl_handler *ctrl_hdlr = &ov9282->ctrl_handler;
1291 const struct ov9282_mode *mode = ov9282->cur_mode;
1292 struct v4l2_fwnode_device_properties props;
1297 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
1301 /* Serialize controls with sensor device */
1302 ctrl_hdlr->lock = &ov9282->mutex;
1304 /* Initialize exposure and gain */
1305 lpfr = mode->vblank + mode->height;
1306 ov9282->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1309 OV9282_EXPOSURE_MIN,
1310 lpfr - OV9282_EXPOSURE_OFFSET,
1311 OV9282_EXPOSURE_STEP,
1312 OV9282_EXPOSURE_DEFAULT);
1314 ov9282->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1316 V4L2_CID_ANALOGUE_GAIN,
1320 OV9282_AGAIN_DEFAULT);
1322 v4l2_ctrl_cluster(2, &ov9282->exp_ctrl);
1324 ov9282->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1331 v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_VFLIP,
1334 v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_HFLIP,
1337 /* Read only controls */
1338 ov9282->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
1339 V4L2_CID_PIXEL_RATE,
1340 OV9282_PIXEL_RATE_10BIT,
1341 OV9282_PIXEL_RATE_10BIT, 1,
1342 OV9282_PIXEL_RATE_10BIT);
1344 ov9282->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1347 ARRAY_SIZE(link_freq) -
1349 mode->link_freq_idx,
1351 if (ov9282->link_freq_ctrl)
1352 ov9282->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1354 hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1];
1355 ov9282->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1359 OV9282_TIMING_HTS_MAX - mode->width,
1362 ret = v4l2_fwnode_device_parse(ov9282->dev, &props);
1364 /* Failure sets ctrl_hdlr->error, which we check afterwards anyway */
1365 v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov9282_ctrl_ops,
1369 if (ctrl_hdlr->error || ret) {
1370 dev_err(ov9282->dev, "control init failed: %d",
1372 v4l2_ctrl_handler_free(ctrl_hdlr);
1373 return ctrl_hdlr->error;
1376 ov9282->sd.ctrl_handler = ctrl_hdlr;
1382 * ov9282_probe() - I2C client device binding
1383 * @client: pointer to i2c client device
1385 * Return: 0 if successful, error code otherwise.
1387 static int ov9282_probe(struct i2c_client *client)
1389 struct ov9282 *ov9282;
1392 ov9282 = devm_kzalloc(&client->dev, sizeof(*ov9282), GFP_KERNEL);
1396 ov9282->dev = &client->dev;
1398 /* Initialize subdev */
1399 v4l2_i2c_subdev_init(&ov9282->sd, client, &ov9282_subdev_ops);
1400 ov9282->sd.internal_ops = &ov9282_internal_ops;
1401 v4l2_i2c_subdev_set_name(&ov9282->sd, client,
1402 device_get_match_data(ov9282->dev), NULL);
1404 ret = ov9282_parse_hw_config(ov9282);
1406 dev_err(ov9282->dev, "HW configuration is not supported");
1410 mutex_init(&ov9282->mutex);
1412 ret = ov9282_power_on(ov9282->dev);
1414 dev_err(ov9282->dev, "failed to power-on the sensor");
1415 goto error_mutex_destroy;
1418 /* Check module identity */
1419 ret = ov9282_detect(ov9282);
1421 dev_err(ov9282->dev, "failed to find sensor: %d", ret);
1422 goto error_power_off;
1425 /* Set default mode to first mode */
1426 ov9282->cur_mode = &supported_modes[DEFAULT_MODE];
1427 ov9282->code = MEDIA_BUS_FMT_Y10_1X10;
1428 ov9282->vblank = ov9282->cur_mode->vblank;
1430 ret = ov9282_init_controls(ov9282);
1432 dev_err(ov9282->dev, "failed to init controls: %d", ret);
1433 goto error_power_off;
1436 /* Initialize subdev */
1437 ov9282->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1438 V4L2_SUBDEV_FL_HAS_EVENTS;
1439 ov9282->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1441 /* Initialize source pad */
1442 ov9282->pad.flags = MEDIA_PAD_FL_SOURCE;
1443 ret = media_entity_pads_init(&ov9282->sd.entity, 1, &ov9282->pad);
1445 dev_err(ov9282->dev, "failed to init entity pads: %d", ret);
1446 goto error_handler_free;
1449 ret = v4l2_async_register_subdev_sensor(&ov9282->sd);
1451 dev_err(ov9282->dev,
1452 "failed to register async subdev: %d", ret);
1453 goto error_media_entity;
1456 pm_runtime_set_active(ov9282->dev);
1457 pm_runtime_enable(ov9282->dev);
1458 pm_runtime_idle(ov9282->dev);
1463 media_entity_cleanup(&ov9282->sd.entity);
1465 v4l2_ctrl_handler_free(ov9282->sd.ctrl_handler);
1467 ov9282_power_off(ov9282->dev);
1468 error_mutex_destroy:
1469 mutex_destroy(&ov9282->mutex);
1475 * ov9282_remove() - I2C client device unbinding
1476 * @client: pointer to I2C client device
1478 * Return: 0 if successful, error code otherwise.
1480 static void ov9282_remove(struct i2c_client *client)
1482 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1483 struct ov9282 *ov9282 = to_ov9282(sd);
1485 v4l2_async_unregister_subdev(sd);
1486 media_entity_cleanup(&sd->entity);
1487 v4l2_ctrl_handler_free(sd->ctrl_handler);
1489 pm_runtime_disable(&client->dev);
1490 if (!pm_runtime_status_suspended(&client->dev))
1491 ov9282_power_off(&client->dev);
1492 pm_runtime_set_suspended(&client->dev);
1494 mutex_destroy(&ov9282->mutex);
1497 static const struct dev_pm_ops ov9282_pm_ops = {
1498 SET_RUNTIME_PM_OPS(ov9282_power_off, ov9282_power_on, NULL)
1501 static const struct of_device_id ov9282_of_match[] = {
1502 { .compatible = "ovti,ov9281", .data = "ov9281" },
1503 { .compatible = "ovti,ov9282", .data = "ov9282" },
1507 MODULE_DEVICE_TABLE(of, ov9282_of_match);
1509 static struct i2c_driver ov9282_driver = {
1510 .probe = ov9282_probe,
1511 .remove = ov9282_remove,
1514 .pm = &ov9282_pm_ops,
1515 .of_match_table = ov9282_of_match,
1519 module_i2c_driver(ov9282_driver);
1521 MODULE_DESCRIPTION("OmniVision ov9282 sensor driver");
1522 MODULE_LICENSE("GPL");