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>
15 #include <media/v4l2-ctrls.h>
16 #include <media/v4l2-fwnode.h>
17 #include <media/v4l2-subdev.h>
20 #define OV9282_REG_MODE_SELECT 0x0100
21 #define OV9282_MODE_STANDBY 0x00
22 #define OV9282_MODE_STREAMING 0x01
25 #define OV9282_REG_LPFR 0x380e
28 #define OV9282_REG_ID 0x300a
29 #define OV9282_ID 0x9281
31 /* Exposure control */
32 #define OV9282_REG_EXPOSURE 0x3500
33 #define OV9282_EXPOSURE_MIN 1
34 #define OV9282_EXPOSURE_OFFSET 12
35 #define OV9282_EXPOSURE_STEP 1
36 #define OV9282_EXPOSURE_DEFAULT 0x0282
38 /* Analog gain control */
39 #define OV9282_REG_AGAIN 0x3509
40 #define OV9282_AGAIN_MIN 0x10
41 #define OV9282_AGAIN_MAX 0xff
42 #define OV9282_AGAIN_STEP 1
43 #define OV9282_AGAIN_DEFAULT 0x10
45 /* Group hold register */
46 #define OV9282_REG_HOLD 0x3308
48 /* Input clock rate */
49 #define OV9282_INCLK_RATE 24000000
51 /* CSI2 HW configuration */
52 #define OV9282_LINK_FREQ 400000000
53 #define OV9282_NUM_DATA_LANES 2
55 #define OV9282_REG_MIN 0x00
56 #define OV9282_REG_MAX 0xfffff
59 * struct ov9282_reg - ov9282 sensor register
60 * @address: Register address
61 * @val: Register value
69 * struct ov9282_reg_list - ov9282 sensor register list
70 * @num_of_regs: Number of registers in the list
71 * @regs: Pointer to register list
73 struct ov9282_reg_list {
75 const struct ov9282_reg *regs;
79 * struct ov9282_mode - ov9282 sensor mode structure
81 * @height: Frame height
83 * @hblank: Horizontal blanking in lines
84 * @vblank: Vertical blanking in lines
85 * @vblank_min: Minimum vertical blanking in lines
86 * @vblank_max: Maximum vertical blanking in lines
87 * @pclk: Sensor pixel clock
88 * @link_freq_idx: Link frequency index
89 * @reg_list: Register list for sensor mode
101 struct ov9282_reg_list reg_list;
105 * struct ov9282 - ov9282 sensor device structure
106 * @dev: Pointer to generic device
107 * @client: Pointer to i2c client
108 * @sd: V4L2 sub-device
109 * @pad: Media pad. Only one pad supported
110 * @reset_gpio: Sensor reset gpio
111 * @inclk: Sensor input clock
112 * @ctrl_handler: V4L2 control handler
113 * @link_freq_ctrl: Pointer to link frequency control
114 * @pclk_ctrl: Pointer to pixel clock control
115 * @hblank_ctrl: Pointer to horizontal blanking control
116 * @vblank_ctrl: Pointer to vertical blanking control
117 * @exp_ctrl: Pointer to exposure control
118 * @again_ctrl: Pointer to analog gain control
119 * @vblank: Vertical blanking in lines
120 * @cur_mode: Pointer to current selected sensor mode
121 * @mutex: Mutex for serializing sensor controls
122 * @streaming: Flag indicating streaming state
126 struct i2c_client *client;
127 struct v4l2_subdev sd;
128 struct media_pad pad;
129 struct gpio_desc *reset_gpio;
131 struct v4l2_ctrl_handler ctrl_handler;
132 struct v4l2_ctrl *link_freq_ctrl;
133 struct v4l2_ctrl *pclk_ctrl;
134 struct v4l2_ctrl *hblank_ctrl;
135 struct v4l2_ctrl *vblank_ctrl;
137 struct v4l2_ctrl *exp_ctrl;
138 struct v4l2_ctrl *again_ctrl;
141 const struct ov9282_mode *cur_mode;
146 static const s64 link_freq[] = {
150 /* Sensor mode registers */
151 static const struct ov9282_reg mode_1280x720_regs[] = {
253 /* Supported sensor mode configurations */
254 static const struct ov9282_mode supported_mode = {
263 .code = MEDIA_BUS_FMT_Y10_1X10,
265 .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
266 .regs = mode_1280x720_regs,
271 * to_ov9282() - ov9282 V4L2 sub-device to ov9282 device.
272 * @subdev: pointer to ov9282 V4L2 sub-device
274 * Return: pointer to ov9282 device
276 static inline struct ov9282 *to_ov9282(struct v4l2_subdev *subdev)
278 return container_of(subdev, struct ov9282, sd);
282 * ov9282_read_reg() - Read registers.
283 * @ov9282: pointer to ov9282 device
284 * @reg: register address
285 * @len: length of bytes to read. Max supported bytes is 4
286 * @val: pointer to register value to be filled.
288 * Return: 0 if successful, error code otherwise.
290 static int ov9282_read_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 *val)
292 struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
293 struct i2c_msg msgs[2] = {0};
294 u8 addr_buf[2] = {0};
295 u8 data_buf[4] = {0};
298 if (WARN_ON(len > 4))
301 put_unaligned_be16(reg, addr_buf);
303 /* Write register address */
304 msgs[0].addr = client->addr;
306 msgs[0].len = ARRAY_SIZE(addr_buf);
307 msgs[0].buf = addr_buf;
309 /* Read data from register */
310 msgs[1].addr = client->addr;
311 msgs[1].flags = I2C_M_RD;
313 msgs[1].buf = &data_buf[4 - len];
315 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
316 if (ret != ARRAY_SIZE(msgs))
319 *val = get_unaligned_be32(data_buf);
325 * ov9282_write_reg() - Write register
326 * @ov9282: pointer to ov9282 device
327 * @reg: register address
328 * @len: length of bytes. Max supported bytes is 4
329 * @val: register value
331 * Return: 0 if successful, error code otherwise.
333 static int ov9282_write_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 val)
335 struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
338 if (WARN_ON(len > 4))
341 put_unaligned_be16(reg, buf);
342 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
343 if (i2c_master_send(client, buf, len + 2) != len + 2)
350 * ov9282_write_regs() - Write a list of registers
351 * @ov9282: pointer to ov9282 device
352 * @regs: list of registers to be written
353 * @len: length of registers array
355 * Return: 0 if successful, error code otherwise.
357 static int ov9282_write_regs(struct ov9282 *ov9282,
358 const struct ov9282_reg *regs, u32 len)
363 for (i = 0; i < len; i++) {
364 ret = ov9282_write_reg(ov9282, regs[i].address, 1, regs[i].val);
373 * ov9282_update_controls() - Update control ranges based on streaming mode
374 * @ov9282: pointer to ov9282 device
375 * @mode: pointer to ov9282_mode sensor mode
377 * Return: 0 if successful, error code otherwise.
379 static int ov9282_update_controls(struct ov9282 *ov9282,
380 const struct ov9282_mode *mode)
384 ret = __v4l2_ctrl_s_ctrl(ov9282->link_freq_ctrl, mode->link_freq_idx);
388 ret = __v4l2_ctrl_s_ctrl(ov9282->hblank_ctrl, mode->hblank);
392 return __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min,
393 mode->vblank_max, 1, mode->vblank);
397 * ov9282_update_exp_gain() - Set updated exposure and gain
398 * @ov9282: pointer to ov9282 device
399 * @exposure: updated exposure value
400 * @gain: updated analog gain value
402 * Return: 0 if successful, error code otherwise.
404 static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
409 lpfr = ov9282->vblank + ov9282->cur_mode->height;
411 dev_dbg(ov9282->dev, "Set exp %u, analog gain %u, lpfr %u",
412 exposure, gain, lpfr);
414 ret = ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 1);
418 ret = ov9282_write_reg(ov9282, OV9282_REG_LPFR, 2, lpfr);
420 goto error_release_group_hold;
422 ret = ov9282_write_reg(ov9282, OV9282_REG_EXPOSURE, 3, exposure << 4);
424 goto error_release_group_hold;
426 ret = ov9282_write_reg(ov9282, OV9282_REG_AGAIN, 1, gain);
428 error_release_group_hold:
429 ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 0);
435 * ov9282_set_ctrl() - Set subdevice control
436 * @ctrl: pointer to v4l2_ctrl structure
438 * Supported controls:
440 * - cluster controls:
441 * - V4L2_CID_ANALOGUE_GAIN
442 * - V4L2_CID_EXPOSURE
444 * Return: 0 if successful, error code otherwise.
446 static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
448 struct ov9282 *ov9282 =
449 container_of(ctrl->handler, struct ov9282, ctrl_handler);
455 case V4L2_CID_VBLANK:
456 ov9282->vblank = ov9282->vblank_ctrl->val;
458 dev_dbg(ov9282->dev, "Received vblank %u, new lpfr %u",
460 ov9282->vblank + ov9282->cur_mode->height);
462 ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl,
465 ov9282->cur_mode->height -
466 OV9282_EXPOSURE_OFFSET,
467 1, OV9282_EXPOSURE_DEFAULT);
469 case V4L2_CID_EXPOSURE:
470 /* Set controls only if sensor is in power on state */
471 if (!pm_runtime_get_if_in_use(ov9282->dev))
474 exposure = ctrl->val;
475 analog_gain = ov9282->again_ctrl->val;
477 dev_dbg(ov9282->dev, "Received exp %u, analog gain %u",
478 exposure, analog_gain);
480 ret = ov9282_update_exp_gain(ov9282, exposure, analog_gain);
482 pm_runtime_put(ov9282->dev);
486 dev_err(ov9282->dev, "Invalid control %d", ctrl->id);
493 /* V4l2 subdevice control ops*/
494 static const struct v4l2_ctrl_ops ov9282_ctrl_ops = {
495 .s_ctrl = ov9282_set_ctrl,
499 * ov9282_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
500 * @sd: pointer to ov9282 V4L2 sub-device structure
501 * @sd_state: V4L2 sub-device configuration
502 * @code: V4L2 sub-device code enumeration need to be filled
504 * Return: 0 if successful, error code otherwise.
506 static int ov9282_enum_mbus_code(struct v4l2_subdev *sd,
507 struct v4l2_subdev_state *sd_state,
508 struct v4l2_subdev_mbus_code_enum *code)
513 code->code = supported_mode.code;
519 * ov9282_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
520 * @sd: pointer to ov9282 V4L2 sub-device structure
521 * @sd_state: V4L2 sub-device configuration
522 * @fsize: V4L2 sub-device size enumeration need to be filled
524 * Return: 0 if successful, error code otherwise.
526 static int ov9282_enum_frame_size(struct v4l2_subdev *sd,
527 struct v4l2_subdev_state *sd_state,
528 struct v4l2_subdev_frame_size_enum *fsize)
530 if (fsize->index > 0)
533 if (fsize->code != supported_mode.code)
536 fsize->min_width = supported_mode.width;
537 fsize->max_width = fsize->min_width;
538 fsize->min_height = supported_mode.height;
539 fsize->max_height = fsize->min_height;
545 * ov9282_fill_pad_format() - Fill subdevice pad format
546 * from selected sensor mode
547 * @ov9282: pointer to ov9282 device
548 * @mode: pointer to ov9282_mode sensor mode
549 * @fmt: V4L2 sub-device format need to be filled
551 static void ov9282_fill_pad_format(struct ov9282 *ov9282,
552 const struct ov9282_mode *mode,
553 struct v4l2_subdev_format *fmt)
555 fmt->format.width = mode->width;
556 fmt->format.height = mode->height;
557 fmt->format.code = mode->code;
558 fmt->format.field = V4L2_FIELD_NONE;
559 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
560 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
561 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
562 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
566 * ov9282_get_pad_format() - Get subdevice pad format
567 * @sd: pointer to ov9282 V4L2 sub-device structure
568 * @sd_state: V4L2 sub-device configuration
569 * @fmt: V4L2 sub-device format need to be set
571 * Return: 0 if successful, error code otherwise.
573 static int ov9282_get_pad_format(struct v4l2_subdev *sd,
574 struct v4l2_subdev_state *sd_state,
575 struct v4l2_subdev_format *fmt)
577 struct ov9282 *ov9282 = to_ov9282(sd);
579 mutex_lock(&ov9282->mutex);
581 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
582 struct v4l2_mbus_framefmt *framefmt;
584 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
585 fmt->format = *framefmt;
587 ov9282_fill_pad_format(ov9282, ov9282->cur_mode, fmt);
590 mutex_unlock(&ov9282->mutex);
596 * ov9282_set_pad_format() - Set subdevice pad format
597 * @sd: pointer to ov9282 V4L2 sub-device structure
598 * @sd_state: V4L2 sub-device configuration
599 * @fmt: V4L2 sub-device format need to be set
601 * Return: 0 if successful, error code otherwise.
603 static int ov9282_set_pad_format(struct v4l2_subdev *sd,
604 struct v4l2_subdev_state *sd_state,
605 struct v4l2_subdev_format *fmt)
607 struct ov9282 *ov9282 = to_ov9282(sd);
608 const struct ov9282_mode *mode;
611 mutex_lock(&ov9282->mutex);
613 mode = &supported_mode;
614 ov9282_fill_pad_format(ov9282, mode, fmt);
616 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
617 struct v4l2_mbus_framefmt *framefmt;
619 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
620 *framefmt = fmt->format;
622 ret = ov9282_update_controls(ov9282, mode);
624 ov9282->cur_mode = mode;
627 mutex_unlock(&ov9282->mutex);
633 * ov9282_init_pad_cfg() - Initialize sub-device pad configuration
634 * @sd: pointer to ov9282 V4L2 sub-device structure
635 * @sd_state: V4L2 sub-device configuration
637 * Return: 0 if successful, error code otherwise.
639 static int ov9282_init_pad_cfg(struct v4l2_subdev *sd,
640 struct v4l2_subdev_state *sd_state)
642 struct ov9282 *ov9282 = to_ov9282(sd);
643 struct v4l2_subdev_format fmt = { 0 };
645 fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
646 ov9282_fill_pad_format(ov9282, &supported_mode, &fmt);
648 return ov9282_set_pad_format(sd, sd_state, &fmt);
652 * ov9282_start_streaming() - Start sensor stream
653 * @ov9282: pointer to ov9282 device
655 * Return: 0 if successful, error code otherwise.
657 static int ov9282_start_streaming(struct ov9282 *ov9282)
659 const struct ov9282_reg_list *reg_list;
662 /* Write sensor mode registers */
663 reg_list = &ov9282->cur_mode->reg_list;
664 ret = ov9282_write_regs(ov9282, reg_list->regs, reg_list->num_of_regs);
666 dev_err(ov9282->dev, "fail to write initial registers");
670 /* Setup handler will write actual exposure and gain */
671 ret = __v4l2_ctrl_handler_setup(ov9282->sd.ctrl_handler);
673 dev_err(ov9282->dev, "fail to setup handler");
677 /* Start streaming */
678 ret = ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
679 1, OV9282_MODE_STREAMING);
681 dev_err(ov9282->dev, "fail to start streaming");
689 * ov9282_stop_streaming() - Stop sensor stream
690 * @ov9282: pointer to ov9282 device
692 * Return: 0 if successful, error code otherwise.
694 static int ov9282_stop_streaming(struct ov9282 *ov9282)
696 return ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
697 1, OV9282_MODE_STANDBY);
701 * ov9282_set_stream() - Enable sensor streaming
702 * @sd: pointer to ov9282 subdevice
703 * @enable: set to enable sensor streaming
705 * Return: 0 if successful, error code otherwise.
707 static int ov9282_set_stream(struct v4l2_subdev *sd, int enable)
709 struct ov9282 *ov9282 = to_ov9282(sd);
712 mutex_lock(&ov9282->mutex);
714 if (ov9282->streaming == enable) {
715 mutex_unlock(&ov9282->mutex);
720 ret = pm_runtime_resume_and_get(ov9282->dev);
724 ret = ov9282_start_streaming(ov9282);
726 goto error_power_off;
728 ov9282_stop_streaming(ov9282);
729 pm_runtime_put(ov9282->dev);
732 ov9282->streaming = enable;
734 mutex_unlock(&ov9282->mutex);
739 pm_runtime_put(ov9282->dev);
741 mutex_unlock(&ov9282->mutex);
747 * ov9282_detect() - Detect ov9282 sensor
748 * @ov9282: pointer to ov9282 device
750 * Return: 0 if successful, -EIO if sensor id does not match
752 static int ov9282_detect(struct ov9282 *ov9282)
757 ret = ov9282_read_reg(ov9282, OV9282_REG_ID, 2, &val);
761 if (val != OV9282_ID) {
762 dev_err(ov9282->dev, "chip id mismatch: %x!=%x",
771 * ov9282_parse_hw_config() - Parse HW configuration and check if supported
772 * @ov9282: pointer to ov9282 device
774 * Return: 0 if successful, error code otherwise.
776 static int ov9282_parse_hw_config(struct ov9282 *ov9282)
778 struct fwnode_handle *fwnode = dev_fwnode(ov9282->dev);
779 struct v4l2_fwnode_endpoint bus_cfg = {
780 .bus_type = V4L2_MBUS_CSI2_DPHY
782 struct fwnode_handle *ep;
790 /* Request optional reset pin */
791 ov9282->reset_gpio = devm_gpiod_get_optional(ov9282->dev, "reset",
793 if (IS_ERR(ov9282->reset_gpio)) {
794 dev_err(ov9282->dev, "failed to get reset gpio %ld",
795 PTR_ERR(ov9282->reset_gpio));
796 return PTR_ERR(ov9282->reset_gpio);
799 /* Get sensor input clock */
800 ov9282->inclk = devm_clk_get(ov9282->dev, NULL);
801 if (IS_ERR(ov9282->inclk)) {
802 dev_err(ov9282->dev, "could not get inclk");
803 return PTR_ERR(ov9282->inclk);
806 rate = clk_get_rate(ov9282->inclk);
807 if (rate != OV9282_INCLK_RATE) {
808 dev_err(ov9282->dev, "inclk frequency mismatch");
812 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
816 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
817 fwnode_handle_put(ep);
821 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV9282_NUM_DATA_LANES) {
823 "number of CSI2 data lanes %d is not supported",
824 bus_cfg.bus.mipi_csi2.num_data_lanes);
826 goto done_endpoint_free;
829 if (!bus_cfg.nr_of_link_frequencies) {
830 dev_err(ov9282->dev, "no link frequencies defined");
832 goto done_endpoint_free;
835 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
836 if (bus_cfg.link_frequencies[i] == OV9282_LINK_FREQ)
837 goto done_endpoint_free;
842 v4l2_fwnode_endpoint_free(&bus_cfg);
847 /* V4l2 subdevice ops */
848 static const struct v4l2_subdev_video_ops ov9282_video_ops = {
849 .s_stream = ov9282_set_stream,
852 static const struct v4l2_subdev_pad_ops ov9282_pad_ops = {
853 .init_cfg = ov9282_init_pad_cfg,
854 .enum_mbus_code = ov9282_enum_mbus_code,
855 .enum_frame_size = ov9282_enum_frame_size,
856 .get_fmt = ov9282_get_pad_format,
857 .set_fmt = ov9282_set_pad_format,
860 static const struct v4l2_subdev_ops ov9282_subdev_ops = {
861 .video = &ov9282_video_ops,
862 .pad = &ov9282_pad_ops,
866 * ov9282_power_on() - Sensor power on sequence
867 * @dev: pointer to i2c device
869 * Return: 0 if successful, error code otherwise.
871 static int ov9282_power_on(struct device *dev)
873 struct v4l2_subdev *sd = dev_get_drvdata(dev);
874 struct ov9282 *ov9282 = to_ov9282(sd);
877 usleep_range(400, 600);
879 gpiod_set_value_cansleep(ov9282->reset_gpio, 1);
881 ret = clk_prepare_enable(ov9282->inclk);
883 dev_err(ov9282->dev, "fail to enable inclk");
887 usleep_range(400, 600);
892 gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
898 * ov9282_power_off() - Sensor power off sequence
899 * @dev: pointer to i2c device
901 * Return: 0 if successful, error code otherwise.
903 static int ov9282_power_off(struct device *dev)
905 struct v4l2_subdev *sd = dev_get_drvdata(dev);
906 struct ov9282 *ov9282 = to_ov9282(sd);
908 gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
910 clk_disable_unprepare(ov9282->inclk);
916 * ov9282_init_controls() - Initialize sensor subdevice controls
917 * @ov9282: pointer to ov9282 device
919 * Return: 0 if successful, error code otherwise.
921 static int ov9282_init_controls(struct ov9282 *ov9282)
923 struct v4l2_ctrl_handler *ctrl_hdlr = &ov9282->ctrl_handler;
924 const struct ov9282_mode *mode = ov9282->cur_mode;
928 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
932 /* Serialize controls with sensor device */
933 ctrl_hdlr->lock = &ov9282->mutex;
935 /* Initialize exposure and gain */
936 lpfr = mode->vblank + mode->height;
937 ov9282->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
941 lpfr - OV9282_EXPOSURE_OFFSET,
942 OV9282_EXPOSURE_STEP,
943 OV9282_EXPOSURE_DEFAULT);
945 ov9282->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
947 V4L2_CID_ANALOGUE_GAIN,
951 OV9282_AGAIN_DEFAULT);
953 v4l2_ctrl_cluster(2, &ov9282->exp_ctrl);
955 ov9282->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
962 /* Read only controls */
963 ov9282->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
966 mode->pclk, mode->pclk,
969 ov9282->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
972 ARRAY_SIZE(link_freq) -
976 if (ov9282->link_freq_ctrl)
977 ov9282->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
979 ov9282->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
985 if (ov9282->hblank_ctrl)
986 ov9282->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
988 if (ctrl_hdlr->error) {
989 dev_err(ov9282->dev, "control init failed: %d",
991 v4l2_ctrl_handler_free(ctrl_hdlr);
992 return ctrl_hdlr->error;
995 ov9282->sd.ctrl_handler = ctrl_hdlr;
1001 * ov9282_probe() - I2C client device binding
1002 * @client: pointer to i2c client device
1004 * Return: 0 if successful, error code otherwise.
1006 static int ov9282_probe(struct i2c_client *client)
1008 struct ov9282 *ov9282;
1011 ov9282 = devm_kzalloc(&client->dev, sizeof(*ov9282), GFP_KERNEL);
1015 ov9282->dev = &client->dev;
1017 /* Initialize subdev */
1018 v4l2_i2c_subdev_init(&ov9282->sd, client, &ov9282_subdev_ops);
1020 ret = ov9282_parse_hw_config(ov9282);
1022 dev_err(ov9282->dev, "HW configuration is not supported");
1026 mutex_init(&ov9282->mutex);
1028 ret = ov9282_power_on(ov9282->dev);
1030 dev_err(ov9282->dev, "failed to power-on the sensor");
1031 goto error_mutex_destroy;
1034 /* Check module identity */
1035 ret = ov9282_detect(ov9282);
1037 dev_err(ov9282->dev, "failed to find sensor: %d", ret);
1038 goto error_power_off;
1041 /* Set default mode to max resolution */
1042 ov9282->cur_mode = &supported_mode;
1043 ov9282->vblank = ov9282->cur_mode->vblank;
1045 ret = ov9282_init_controls(ov9282);
1047 dev_err(ov9282->dev, "failed to init controls: %d", ret);
1048 goto error_power_off;
1051 /* Initialize subdev */
1052 ov9282->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1053 ov9282->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1055 /* Initialize source pad */
1056 ov9282->pad.flags = MEDIA_PAD_FL_SOURCE;
1057 ret = media_entity_pads_init(&ov9282->sd.entity, 1, &ov9282->pad);
1059 dev_err(ov9282->dev, "failed to init entity pads: %d", ret);
1060 goto error_handler_free;
1063 ret = v4l2_async_register_subdev_sensor(&ov9282->sd);
1065 dev_err(ov9282->dev,
1066 "failed to register async subdev: %d", ret);
1067 goto error_media_entity;
1070 pm_runtime_set_active(ov9282->dev);
1071 pm_runtime_enable(ov9282->dev);
1072 pm_runtime_idle(ov9282->dev);
1077 media_entity_cleanup(&ov9282->sd.entity);
1079 v4l2_ctrl_handler_free(ov9282->sd.ctrl_handler);
1081 ov9282_power_off(ov9282->dev);
1082 error_mutex_destroy:
1083 mutex_destroy(&ov9282->mutex);
1089 * ov9282_remove() - I2C client device unbinding
1090 * @client: pointer to I2C client device
1092 * Return: 0 if successful, error code otherwise.
1094 static void ov9282_remove(struct i2c_client *client)
1096 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1097 struct ov9282 *ov9282 = to_ov9282(sd);
1099 v4l2_async_unregister_subdev(sd);
1100 media_entity_cleanup(&sd->entity);
1101 v4l2_ctrl_handler_free(sd->ctrl_handler);
1103 pm_runtime_disable(&client->dev);
1104 if (!pm_runtime_status_suspended(&client->dev))
1105 ov9282_power_off(&client->dev);
1106 pm_runtime_set_suspended(&client->dev);
1108 mutex_destroy(&ov9282->mutex);
1111 static const struct dev_pm_ops ov9282_pm_ops = {
1112 SET_RUNTIME_PM_OPS(ov9282_power_off, ov9282_power_on, NULL)
1115 static const struct of_device_id ov9282_of_match[] = {
1116 { .compatible = "ovti,ov9282" },
1120 MODULE_DEVICE_TABLE(of, ov9282_of_match);
1122 static struct i2c_driver ov9282_driver = {
1123 .probe_new = ov9282_probe,
1124 .remove = ov9282_remove,
1127 .pm = &ov9282_pm_ops,
1128 .of_match_table = ov9282_of_match,
1132 module_i2c_driver(ov9282_driver);
1134 MODULE_DESCRIPTION("OmniVision ov9282 sensor driver");
1135 MODULE_LICENSE("GPL");