1 // SPDX-License-Identifier: GPL-2.0-only
3 * Sony imx412 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-fwnode.h>
18 #include <media/v4l2-subdev.h>
21 #define IMX412_REG_MODE_SELECT 0x0100
22 #define IMX412_MODE_STANDBY 0x00
23 #define IMX412_MODE_STREAMING 0x01
26 #define IMX412_REG_LPFR 0x0340
29 #define IMX412_REG_ID 0x0016
30 #define IMX412_ID 0x577
32 /* Exposure control */
33 #define IMX412_REG_EXPOSURE_CIT 0x0202
34 #define IMX412_EXPOSURE_MIN 8
35 #define IMX412_EXPOSURE_OFFSET 22
36 #define IMX412_EXPOSURE_STEP 1
37 #define IMX412_EXPOSURE_DEFAULT 0x0648
39 /* Analog gain control */
40 #define IMX412_REG_AGAIN 0x0204
41 #define IMX412_AGAIN_MIN 0
42 #define IMX412_AGAIN_MAX 978
43 #define IMX412_AGAIN_STEP 1
44 #define IMX412_AGAIN_DEFAULT 0
46 /* Group hold register */
47 #define IMX412_REG_HOLD 0x0104
49 /* Input clock rate */
50 #define IMX412_INCLK_RATE 24000000
52 /* CSI2 HW configuration */
53 #define IMX412_LINK_FREQ 600000000
54 #define IMX412_NUM_DATA_LANES 4
56 #define IMX412_REG_MIN 0x00
57 #define IMX412_REG_MAX 0xffff
60 * struct imx412_reg - imx412 sensor register
61 * @address: Register address
62 * @val: Register value
70 * struct imx412_reg_list - imx412 sensor register list
71 * @num_of_regs: Number of registers in the list
72 * @regs: Pointer to register list
74 struct imx412_reg_list {
76 const struct imx412_reg *regs;
80 * struct imx412_mode - imx412 sensor mode structure
82 * @height: Frame height
84 * @hblank: Horizontal blanking in lines
85 * @vblank: Vertical blanking in lines
86 * @vblank_min: Minimum vertical blanking in lines
87 * @vblank_max: Maximum vertical blanking in lines
88 * @pclk: Sensor pixel clock
89 * @link_freq_idx: Link frequency index
90 * @reg_list: Register list for sensor mode
102 struct imx412_reg_list reg_list;
105 static const char * const imx412_supply_names[] = {
106 "dovdd", /* Digital I/O power */
107 "avdd", /* Analog power */
108 "dvdd", /* Digital core power */
112 * struct imx412 - imx412 sensor device structure
113 * @dev: Pointer to generic device
114 * @client: Pointer to i2c client
115 * @sd: V4L2 sub-device
116 * @pad: Media pad. Only one pad supported
117 * @reset_gpio: Sensor reset gpio
118 * @inclk: Sensor input clock
119 * @supplies: Regulator supplies
120 * @ctrl_handler: V4L2 control handler
121 * @link_freq_ctrl: Pointer to link frequency control
122 * @pclk_ctrl: Pointer to pixel clock control
123 * @hblank_ctrl: Pointer to horizontal blanking control
124 * @vblank_ctrl: Pointer to vertical blanking control
125 * @exp_ctrl: Pointer to exposure control
126 * @again_ctrl: Pointer to analog gain control
127 * @vblank: Vertical blanking in lines
128 * @cur_mode: Pointer to current selected sensor mode
129 * @mutex: Mutex for serializing sensor controls
133 struct i2c_client *client;
134 struct v4l2_subdev sd;
135 struct media_pad pad;
136 struct gpio_desc *reset_gpio;
138 struct regulator_bulk_data supplies[ARRAY_SIZE(imx412_supply_names)];
139 struct v4l2_ctrl_handler ctrl_handler;
140 struct v4l2_ctrl *link_freq_ctrl;
141 struct v4l2_ctrl *pclk_ctrl;
142 struct v4l2_ctrl *hblank_ctrl;
143 struct v4l2_ctrl *vblank_ctrl;
145 struct v4l2_ctrl *exp_ctrl;
146 struct v4l2_ctrl *again_ctrl;
149 const struct imx412_mode *cur_mode;
153 static const s64 link_freq[] = {
157 /* Sensor mode registers */
158 static const struct imx412_reg mode_4056x3040_regs[] = {
392 /* Supported sensor mode configurations */
393 static const struct imx412_mode supported_mode = {
402 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
404 .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
405 .regs = mode_4056x3040_regs,
410 * to_imx412() - imx412 V4L2 sub-device to imx412 device.
411 * @subdev: pointer to imx412 V4L2 sub-device
413 * Return: pointer to imx412 device
415 static inline struct imx412 *to_imx412(struct v4l2_subdev *subdev)
417 return container_of(subdev, struct imx412, sd);
421 * imx412_read_reg() - Read registers.
422 * @imx412: pointer to imx412 device
423 * @reg: register address
424 * @len: length of bytes to read. Max supported bytes is 4
425 * @val: pointer to register value to be filled.
427 * Return: 0 if successful, error code otherwise.
429 static int imx412_read_reg(struct imx412 *imx412, u16 reg, u32 len, u32 *val)
431 struct i2c_client *client = v4l2_get_subdevdata(&imx412->sd);
432 struct i2c_msg msgs[2] = {0};
433 u8 addr_buf[2] = {0};
434 u8 data_buf[4] = {0};
437 if (WARN_ON(len > 4))
440 put_unaligned_be16(reg, addr_buf);
442 /* Write register address */
443 msgs[0].addr = client->addr;
445 msgs[0].len = ARRAY_SIZE(addr_buf);
446 msgs[0].buf = addr_buf;
448 /* Read data from register */
449 msgs[1].addr = client->addr;
450 msgs[1].flags = I2C_M_RD;
452 msgs[1].buf = &data_buf[4 - len];
454 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
455 if (ret != ARRAY_SIZE(msgs))
458 *val = get_unaligned_be32(data_buf);
464 * imx412_write_reg() - Write register
465 * @imx412: pointer to imx412 device
466 * @reg: register address
467 * @len: length of bytes. Max supported bytes is 4
468 * @val: register value
470 * Return: 0 if successful, error code otherwise.
472 static int imx412_write_reg(struct imx412 *imx412, u16 reg, u32 len, u32 val)
474 struct i2c_client *client = v4l2_get_subdevdata(&imx412->sd);
477 if (WARN_ON(len > 4))
480 put_unaligned_be16(reg, buf);
481 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
482 if (i2c_master_send(client, buf, len + 2) != len + 2)
489 * imx412_write_regs() - Write a list of registers
490 * @imx412: pointer to imx412 device
491 * @regs: list of registers to be written
492 * @len: length of registers array
494 * Return: 0 if successful, error code otherwise.
496 static int imx412_write_regs(struct imx412 *imx412,
497 const struct imx412_reg *regs, u32 len)
502 for (i = 0; i < len; i++) {
503 ret = imx412_write_reg(imx412, regs[i].address, 1, regs[i].val);
512 * imx412_update_controls() - Update control ranges based on streaming mode
513 * @imx412: pointer to imx412 device
514 * @mode: pointer to imx412_mode sensor mode
516 * Return: 0 if successful, error code otherwise.
518 static int imx412_update_controls(struct imx412 *imx412,
519 const struct imx412_mode *mode)
523 ret = __v4l2_ctrl_s_ctrl(imx412->link_freq_ctrl, mode->link_freq_idx);
527 ret = __v4l2_ctrl_s_ctrl(imx412->hblank_ctrl, mode->hblank);
531 return __v4l2_ctrl_modify_range(imx412->vblank_ctrl, mode->vblank_min,
532 mode->vblank_max, 1, mode->vblank);
536 * imx412_update_exp_gain() - Set updated exposure and gain
537 * @imx412: pointer to imx412 device
538 * @exposure: updated exposure value
539 * @gain: updated analog gain value
541 * Return: 0 if successful, error code otherwise.
543 static int imx412_update_exp_gain(struct imx412 *imx412, u32 exposure, u32 gain)
548 lpfr = imx412->vblank + imx412->cur_mode->height;
550 dev_dbg(imx412->dev, "Set exp %u, analog gain %u, lpfr %u",
551 exposure, gain, lpfr);
553 ret = imx412_write_reg(imx412, IMX412_REG_HOLD, 1, 1);
557 ret = imx412_write_reg(imx412, IMX412_REG_LPFR, 2, lpfr);
559 goto error_release_group_hold;
561 ret = imx412_write_reg(imx412, IMX412_REG_EXPOSURE_CIT, 2, exposure);
563 goto error_release_group_hold;
565 ret = imx412_write_reg(imx412, IMX412_REG_AGAIN, 2, gain);
567 error_release_group_hold:
568 imx412_write_reg(imx412, IMX412_REG_HOLD, 1, 0);
574 * imx412_set_ctrl() - Set subdevice control
575 * @ctrl: pointer to v4l2_ctrl structure
577 * Supported controls:
579 * - cluster controls:
580 * - V4L2_CID_ANALOGUE_GAIN
581 * - V4L2_CID_EXPOSURE
583 * Return: 0 if successful, error code otherwise.
585 static int imx412_set_ctrl(struct v4l2_ctrl *ctrl)
587 struct imx412 *imx412 =
588 container_of(ctrl->handler, struct imx412, ctrl_handler);
594 case V4L2_CID_VBLANK:
595 imx412->vblank = imx412->vblank_ctrl->val;
597 dev_dbg(imx412->dev, "Received vblank %u, new lpfr %u",
599 imx412->vblank + imx412->cur_mode->height);
601 ret = __v4l2_ctrl_modify_range(imx412->exp_ctrl,
604 imx412->cur_mode->height -
605 IMX412_EXPOSURE_OFFSET,
606 1, IMX412_EXPOSURE_DEFAULT);
608 case V4L2_CID_EXPOSURE:
609 /* Set controls only if sensor is in power on state */
610 if (!pm_runtime_get_if_in_use(imx412->dev))
613 exposure = ctrl->val;
614 analog_gain = imx412->again_ctrl->val;
616 dev_dbg(imx412->dev, "Received exp %u, analog gain %u",
617 exposure, analog_gain);
619 ret = imx412_update_exp_gain(imx412, exposure, analog_gain);
621 pm_runtime_put(imx412->dev);
625 dev_err(imx412->dev, "Invalid control %d", ctrl->id);
632 /* V4l2 subdevice control ops*/
633 static const struct v4l2_ctrl_ops imx412_ctrl_ops = {
634 .s_ctrl = imx412_set_ctrl,
638 * imx412_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
639 * @sd: pointer to imx412 V4L2 sub-device structure
640 * @sd_state: V4L2 sub-device configuration
641 * @code: V4L2 sub-device code enumeration need to be filled
643 * Return: 0 if successful, error code otherwise.
645 static int imx412_enum_mbus_code(struct v4l2_subdev *sd,
646 struct v4l2_subdev_state *sd_state,
647 struct v4l2_subdev_mbus_code_enum *code)
652 code->code = supported_mode.code;
658 * imx412_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
659 * @sd: pointer to imx412 V4L2 sub-device structure
660 * @sd_state: V4L2 sub-device configuration
661 * @fsize: V4L2 sub-device size enumeration need to be filled
663 * Return: 0 if successful, error code otherwise.
665 static int imx412_enum_frame_size(struct v4l2_subdev *sd,
666 struct v4l2_subdev_state *sd_state,
667 struct v4l2_subdev_frame_size_enum *fsize)
669 if (fsize->index > 0)
672 if (fsize->code != supported_mode.code)
675 fsize->min_width = supported_mode.width;
676 fsize->max_width = fsize->min_width;
677 fsize->min_height = supported_mode.height;
678 fsize->max_height = fsize->min_height;
684 * imx412_fill_pad_format() - Fill subdevice pad format
685 * from selected sensor mode
686 * @imx412: pointer to imx412 device
687 * @mode: pointer to imx412_mode sensor mode
688 * @fmt: V4L2 sub-device format need to be filled
690 static void imx412_fill_pad_format(struct imx412 *imx412,
691 const struct imx412_mode *mode,
692 struct v4l2_subdev_format *fmt)
694 fmt->format.width = mode->width;
695 fmt->format.height = mode->height;
696 fmt->format.code = mode->code;
697 fmt->format.field = V4L2_FIELD_NONE;
698 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
699 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
700 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
701 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
705 * imx412_get_pad_format() - Get subdevice pad format
706 * @sd: pointer to imx412 V4L2 sub-device structure
707 * @sd_state: V4L2 sub-device configuration
708 * @fmt: V4L2 sub-device format need to be set
710 * Return: 0 if successful, error code otherwise.
712 static int imx412_get_pad_format(struct v4l2_subdev *sd,
713 struct v4l2_subdev_state *sd_state,
714 struct v4l2_subdev_format *fmt)
716 struct imx412 *imx412 = to_imx412(sd);
718 mutex_lock(&imx412->mutex);
720 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
721 struct v4l2_mbus_framefmt *framefmt;
723 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
724 fmt->format = *framefmt;
726 imx412_fill_pad_format(imx412, imx412->cur_mode, fmt);
729 mutex_unlock(&imx412->mutex);
735 * imx412_set_pad_format() - Set subdevice pad format
736 * @sd: pointer to imx412 V4L2 sub-device structure
737 * @sd_state: V4L2 sub-device configuration
738 * @fmt: V4L2 sub-device format need to be set
740 * Return: 0 if successful, error code otherwise.
742 static int imx412_set_pad_format(struct v4l2_subdev *sd,
743 struct v4l2_subdev_state *sd_state,
744 struct v4l2_subdev_format *fmt)
746 struct imx412 *imx412 = to_imx412(sd);
747 const struct imx412_mode *mode;
750 mutex_lock(&imx412->mutex);
752 mode = &supported_mode;
753 imx412_fill_pad_format(imx412, mode, fmt);
755 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
756 struct v4l2_mbus_framefmt *framefmt;
758 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
759 *framefmt = fmt->format;
761 ret = imx412_update_controls(imx412, mode);
763 imx412->cur_mode = mode;
766 mutex_unlock(&imx412->mutex);
772 * imx412_init_state() - Initialize sub-device state
773 * @sd: pointer to imx412 V4L2 sub-device structure
774 * @sd_state: V4L2 sub-device configuration
776 * Return: 0 if successful, error code otherwise.
778 static int imx412_init_state(struct v4l2_subdev *sd,
779 struct v4l2_subdev_state *sd_state)
781 struct imx412 *imx412 = to_imx412(sd);
782 struct v4l2_subdev_format fmt = { 0 };
784 fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
785 imx412_fill_pad_format(imx412, &supported_mode, &fmt);
787 return imx412_set_pad_format(sd, sd_state, &fmt);
791 * imx412_start_streaming() - Start sensor stream
792 * @imx412: pointer to imx412 device
794 * Return: 0 if successful, error code otherwise.
796 static int imx412_start_streaming(struct imx412 *imx412)
798 const struct imx412_reg_list *reg_list;
801 /* Write sensor mode registers */
802 reg_list = &imx412->cur_mode->reg_list;
803 ret = imx412_write_regs(imx412, reg_list->regs,
804 reg_list->num_of_regs);
806 dev_err(imx412->dev, "fail to write initial registers");
810 /* Setup handler will write actual exposure and gain */
811 ret = __v4l2_ctrl_handler_setup(imx412->sd.ctrl_handler);
813 dev_err(imx412->dev, "fail to setup handler");
817 /* Delay is required before streaming*/
818 usleep_range(7400, 8000);
820 /* Start streaming */
821 ret = imx412_write_reg(imx412, IMX412_REG_MODE_SELECT,
822 1, IMX412_MODE_STREAMING);
824 dev_err(imx412->dev, "fail to start streaming");
832 * imx412_stop_streaming() - Stop sensor stream
833 * @imx412: pointer to imx412 device
835 * Return: 0 if successful, error code otherwise.
837 static int imx412_stop_streaming(struct imx412 *imx412)
839 return imx412_write_reg(imx412, IMX412_REG_MODE_SELECT,
840 1, IMX412_MODE_STANDBY);
844 * imx412_set_stream() - Enable sensor streaming
845 * @sd: pointer to imx412 subdevice
846 * @enable: set to enable sensor streaming
848 * Return: 0 if successful, error code otherwise.
850 static int imx412_set_stream(struct v4l2_subdev *sd, int enable)
852 struct imx412 *imx412 = to_imx412(sd);
855 mutex_lock(&imx412->mutex);
858 ret = pm_runtime_resume_and_get(imx412->dev);
862 ret = imx412_start_streaming(imx412);
864 goto error_power_off;
866 imx412_stop_streaming(imx412);
867 pm_runtime_put(imx412->dev);
870 mutex_unlock(&imx412->mutex);
875 pm_runtime_put(imx412->dev);
877 mutex_unlock(&imx412->mutex);
883 * imx412_detect() - Detect imx412 sensor
884 * @imx412: pointer to imx412 device
886 * Return: 0 if successful, -EIO if sensor id does not match
888 static int imx412_detect(struct imx412 *imx412)
893 ret = imx412_read_reg(imx412, IMX412_REG_ID, 2, &val);
897 if (val != IMX412_ID) {
898 dev_err(imx412->dev, "chip id mismatch: %x!=%x",
907 * imx412_parse_hw_config() - Parse HW configuration and check if supported
908 * @imx412: pointer to imx412 device
910 * Return: 0 if successful, error code otherwise.
912 static int imx412_parse_hw_config(struct imx412 *imx412)
914 struct fwnode_handle *fwnode = dev_fwnode(imx412->dev);
915 struct v4l2_fwnode_endpoint bus_cfg = {
916 .bus_type = V4L2_MBUS_CSI2_DPHY
918 struct fwnode_handle *ep;
926 /* Request optional reset pin */
927 imx412->reset_gpio = devm_gpiod_get_optional(imx412->dev, "reset",
929 if (IS_ERR(imx412->reset_gpio)) {
930 dev_err(imx412->dev, "failed to get reset gpio %ld",
931 PTR_ERR(imx412->reset_gpio));
932 return PTR_ERR(imx412->reset_gpio);
935 /* Get sensor input clock */
936 imx412->inclk = devm_clk_get(imx412->dev, NULL);
937 if (IS_ERR(imx412->inclk)) {
938 dev_err(imx412->dev, "could not get inclk");
939 return PTR_ERR(imx412->inclk);
942 rate = clk_get_rate(imx412->inclk);
943 if (rate != IMX412_INCLK_RATE) {
944 dev_err(imx412->dev, "inclk frequency mismatch");
948 /* Get optional DT defined regulators */
949 for (i = 0; i < ARRAY_SIZE(imx412_supply_names); i++)
950 imx412->supplies[i].supply = imx412_supply_names[i];
952 ret = devm_regulator_bulk_get(imx412->dev,
953 ARRAY_SIZE(imx412_supply_names),
958 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
962 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
963 fwnode_handle_put(ep);
967 if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX412_NUM_DATA_LANES) {
969 "number of CSI2 data lanes %d is not supported",
970 bus_cfg.bus.mipi_csi2.num_data_lanes);
972 goto done_endpoint_free;
975 if (!bus_cfg.nr_of_link_frequencies) {
976 dev_err(imx412->dev, "no link frequencies defined");
978 goto done_endpoint_free;
981 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
982 if (bus_cfg.link_frequencies[i] == IMX412_LINK_FREQ)
983 goto done_endpoint_free;
988 v4l2_fwnode_endpoint_free(&bus_cfg);
993 /* V4l2 subdevice ops */
994 static const struct v4l2_subdev_video_ops imx412_video_ops = {
995 .s_stream = imx412_set_stream,
998 static const struct v4l2_subdev_pad_ops imx412_pad_ops = {
999 .enum_mbus_code = imx412_enum_mbus_code,
1000 .enum_frame_size = imx412_enum_frame_size,
1001 .get_fmt = imx412_get_pad_format,
1002 .set_fmt = imx412_set_pad_format,
1005 static const struct v4l2_subdev_ops imx412_subdev_ops = {
1006 .video = &imx412_video_ops,
1007 .pad = &imx412_pad_ops,
1010 static const struct v4l2_subdev_internal_ops imx412_internal_ops = {
1011 .init_state = imx412_init_state,
1015 * imx412_power_on() - Sensor power on sequence
1016 * @dev: pointer to i2c device
1018 * Return: 0 if successful, error code otherwise.
1020 static int imx412_power_on(struct device *dev)
1022 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1023 struct imx412 *imx412 = to_imx412(sd);
1026 ret = regulator_bulk_enable(ARRAY_SIZE(imx412_supply_names),
1029 dev_err(dev, "failed to enable regulators\n");
1033 gpiod_set_value_cansleep(imx412->reset_gpio, 0);
1035 ret = clk_prepare_enable(imx412->inclk);
1037 dev_err(imx412->dev, "fail to enable inclk");
1041 usleep_range(1000, 1200);
1046 gpiod_set_value_cansleep(imx412->reset_gpio, 1);
1047 regulator_bulk_disable(ARRAY_SIZE(imx412_supply_names),
1054 * imx412_power_off() - Sensor power off sequence
1055 * @dev: pointer to i2c device
1057 * Return: 0 if successful, error code otherwise.
1059 static int imx412_power_off(struct device *dev)
1061 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1062 struct imx412 *imx412 = to_imx412(sd);
1064 clk_disable_unprepare(imx412->inclk);
1066 gpiod_set_value_cansleep(imx412->reset_gpio, 1);
1068 regulator_bulk_disable(ARRAY_SIZE(imx412_supply_names),
1075 * imx412_init_controls() - Initialize sensor subdevice controls
1076 * @imx412: pointer to imx412 device
1078 * Return: 0 if successful, error code otherwise.
1080 static int imx412_init_controls(struct imx412 *imx412)
1082 struct v4l2_ctrl_handler *ctrl_hdlr = &imx412->ctrl_handler;
1083 const struct imx412_mode *mode = imx412->cur_mode;
1087 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
1091 /* Serialize controls with sensor device */
1092 ctrl_hdlr->lock = &imx412->mutex;
1094 /* Initialize exposure and gain */
1095 lpfr = mode->vblank + mode->height;
1096 imx412->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1099 IMX412_EXPOSURE_MIN,
1100 lpfr - IMX412_EXPOSURE_OFFSET,
1101 IMX412_EXPOSURE_STEP,
1102 IMX412_EXPOSURE_DEFAULT);
1104 imx412->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1106 V4L2_CID_ANALOGUE_GAIN,
1110 IMX412_AGAIN_DEFAULT);
1112 v4l2_ctrl_cluster(2, &imx412->exp_ctrl);
1114 imx412->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1121 /* Read only controls */
1122 imx412->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1124 V4L2_CID_PIXEL_RATE,
1125 mode->pclk, mode->pclk,
1128 imx412->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1131 ARRAY_SIZE(link_freq) -
1133 mode->link_freq_idx,
1135 if (imx412->link_freq_ctrl)
1136 imx412->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1138 imx412->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1144 if (imx412->hblank_ctrl)
1145 imx412->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1147 if (ctrl_hdlr->error) {
1148 dev_err(imx412->dev, "control init failed: %d",
1150 v4l2_ctrl_handler_free(ctrl_hdlr);
1151 return ctrl_hdlr->error;
1154 imx412->sd.ctrl_handler = ctrl_hdlr;
1160 * imx412_probe() - I2C client device binding
1161 * @client: pointer to i2c client device
1163 * Return: 0 if successful, error code otherwise.
1165 static int imx412_probe(struct i2c_client *client)
1167 struct imx412 *imx412;
1171 imx412 = devm_kzalloc(&client->dev, sizeof(*imx412), GFP_KERNEL);
1175 imx412->dev = &client->dev;
1176 name = device_get_match_data(&client->dev);
1180 /* Initialize subdev */
1181 v4l2_i2c_subdev_init(&imx412->sd, client, &imx412_subdev_ops);
1182 imx412->sd.internal_ops = &imx412_internal_ops;
1184 ret = imx412_parse_hw_config(imx412);
1186 dev_err(imx412->dev, "HW configuration is not supported");
1190 mutex_init(&imx412->mutex);
1192 ret = imx412_power_on(imx412->dev);
1194 dev_err(imx412->dev, "failed to power-on the sensor");
1195 goto error_mutex_destroy;
1198 /* Check module identity */
1199 ret = imx412_detect(imx412);
1201 dev_err(imx412->dev, "failed to find sensor: %d", ret);
1202 goto error_power_off;
1205 /* Set default mode to max resolution */
1206 imx412->cur_mode = &supported_mode;
1207 imx412->vblank = imx412->cur_mode->vblank;
1209 ret = imx412_init_controls(imx412);
1211 dev_err(imx412->dev, "failed to init controls: %d", ret);
1212 goto error_power_off;
1215 /* Initialize subdev */
1216 imx412->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1217 imx412->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1219 v4l2_i2c_subdev_set_name(&imx412->sd, client, name, NULL);
1221 /* Initialize source pad */
1222 imx412->pad.flags = MEDIA_PAD_FL_SOURCE;
1223 ret = media_entity_pads_init(&imx412->sd.entity, 1, &imx412->pad);
1225 dev_err(imx412->dev, "failed to init entity pads: %d", ret);
1226 goto error_handler_free;
1229 ret = v4l2_async_register_subdev_sensor(&imx412->sd);
1231 dev_err(imx412->dev,
1232 "failed to register async subdev: %d", ret);
1233 goto error_media_entity;
1236 pm_runtime_set_active(imx412->dev);
1237 pm_runtime_enable(imx412->dev);
1238 pm_runtime_idle(imx412->dev);
1243 media_entity_cleanup(&imx412->sd.entity);
1245 v4l2_ctrl_handler_free(imx412->sd.ctrl_handler);
1247 imx412_power_off(imx412->dev);
1248 error_mutex_destroy:
1249 mutex_destroy(&imx412->mutex);
1255 * imx412_remove() - I2C client device unbinding
1256 * @client: pointer to I2C client device
1258 * Return: 0 if successful, error code otherwise.
1260 static void imx412_remove(struct i2c_client *client)
1262 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1263 struct imx412 *imx412 = to_imx412(sd);
1265 v4l2_async_unregister_subdev(sd);
1266 media_entity_cleanup(&sd->entity);
1267 v4l2_ctrl_handler_free(sd->ctrl_handler);
1269 pm_runtime_disable(&client->dev);
1270 if (!pm_runtime_status_suspended(&client->dev))
1271 imx412_power_off(&client->dev);
1272 pm_runtime_set_suspended(&client->dev);
1274 mutex_destroy(&imx412->mutex);
1277 static const struct dev_pm_ops imx412_pm_ops = {
1278 SET_RUNTIME_PM_OPS(imx412_power_off, imx412_power_on, NULL)
1281 static const struct of_device_id imx412_of_match[] = {
1282 { .compatible = "sony,imx412", .data = "imx412" },
1283 { .compatible = "sony,imx577", .data = "imx577" },
1287 MODULE_DEVICE_TABLE(of, imx412_of_match);
1289 static struct i2c_driver imx412_driver = {
1290 .probe = imx412_probe,
1291 .remove = imx412_remove,
1294 .pm = &imx412_pm_ops,
1295 .of_match_table = imx412_of_match,
1299 module_i2c_driver(imx412_driver);
1301 MODULE_DESCRIPTION("Sony imx412 sensor driver");
1302 MODULE_LICENSE("GPL");