1 // SPDX-License-Identifier: GPL-2.0-only
3 * Sony imx412 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 IMX412_REG_MODE_SELECT 0x0100
21 #define IMX412_MODE_STANDBY 0x00
22 #define IMX412_MODE_STREAMING 0x01
25 #define IMX412_REG_LPFR 0x0340
28 #define IMX412_REG_ID 0x0016
29 #define IMX412_ID 0x577
31 /* Exposure control */
32 #define IMX412_REG_EXPOSURE_CIT 0x0202
33 #define IMX412_EXPOSURE_MIN 8
34 #define IMX412_EXPOSURE_OFFSET 22
35 #define IMX412_EXPOSURE_STEP 1
36 #define IMX412_EXPOSURE_DEFAULT 0x0648
38 /* Analog gain control */
39 #define IMX412_REG_AGAIN 0x0204
40 #define IMX412_AGAIN_MIN 0
41 #define IMX412_AGAIN_MAX 978
42 #define IMX412_AGAIN_STEP 1
43 #define IMX412_AGAIN_DEFAULT 0
45 /* Group hold register */
46 #define IMX412_REG_HOLD 0x0104
48 /* Input clock rate */
49 #define IMX412_INCLK_RATE 24000000
51 /* CSI2 HW configuration */
52 #define IMX412_LINK_FREQ 600000000
53 #define IMX412_NUM_DATA_LANES 4
55 #define IMX412_REG_MIN 0x00
56 #define IMX412_REG_MAX 0xffff
59 * struct imx412_reg - imx412 sensor register
60 * @address: Register address
61 * @val: Register value
69 * struct imx412_reg_list - imx412 sensor register list
70 * @num_of_regs: Number of registers in the list
71 * @regs: Pointer to register list
73 struct imx412_reg_list
{
75 const struct imx412_reg
*regs
;
79 * struct imx412_mode - imx412 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 imx412_reg_list reg_list
;
105 * struct imx412 - imx412 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 imx412_mode
*cur_mode
;
146 static const s64 link_freq
[] = {
150 /* Sensor mode registers */
151 static const struct imx412_reg mode_4056x3040_regs
[] = {
385 /* Supported sensor mode configurations */
386 static const struct imx412_mode supported_mode
= {
395 .code
= MEDIA_BUS_FMT_SRGGB10_1X10
,
397 .num_of_regs
= ARRAY_SIZE(mode_4056x3040_regs
),
398 .regs
= mode_4056x3040_regs
,
403 * to_imx412() - imx412 V4L2 sub-device to imx412 device.
404 * @subdev: pointer to imx412 V4L2 sub-device
406 * Return: pointer to imx412 device
408 static inline struct imx412
*to_imx412(struct v4l2_subdev
*subdev
)
410 return container_of(subdev
, struct imx412
, sd
);
414 * imx412_read_reg() - Read registers.
415 * @imx412: pointer to imx412 device
416 * @reg: register address
417 * @len: length of bytes to read. Max supported bytes is 4
418 * @val: pointer to register value to be filled.
420 * Return: 0 if successful, error code otherwise.
422 static int imx412_read_reg(struct imx412
*imx412
, u16 reg
, u32 len
, u32
*val
)
424 struct i2c_client
*client
= v4l2_get_subdevdata(&imx412
->sd
);
425 struct i2c_msg msgs
[2] = {0};
426 u8 addr_buf
[2] = {0};
427 u8 data_buf
[4] = {0};
430 if (WARN_ON(len
> 4))
433 put_unaligned_be16(reg
, addr_buf
);
435 /* Write register address */
436 msgs
[0].addr
= client
->addr
;
438 msgs
[0].len
= ARRAY_SIZE(addr_buf
);
439 msgs
[0].buf
= addr_buf
;
441 /* Read data from register */
442 msgs
[1].addr
= client
->addr
;
443 msgs
[1].flags
= I2C_M_RD
;
445 msgs
[1].buf
= &data_buf
[4 - len
];
447 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
448 if (ret
!= ARRAY_SIZE(msgs
))
451 *val
= get_unaligned_be32(data_buf
);
457 * imx412_write_reg() - Write register
458 * @imx412: pointer to imx412 device
459 * @reg: register address
460 * @len: length of bytes. Max supported bytes is 4
461 * @val: register value
463 * Return: 0 if successful, error code otherwise.
465 static int imx412_write_reg(struct imx412
*imx412
, u16 reg
, u32 len
, u32 val
)
467 struct i2c_client
*client
= v4l2_get_subdevdata(&imx412
->sd
);
470 if (WARN_ON(len
> 4))
473 put_unaligned_be16(reg
, buf
);
474 put_unaligned_be32(val
<< (8 * (4 - len
)), buf
+ 2);
475 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
482 * imx412_write_regs() - Write a list of registers
483 * @imx412: pointer to imx412 device
484 * @regs: list of registers to be written
485 * @len: length of registers array
487 * Return: 0 if successful, error code otherwise.
489 static int imx412_write_regs(struct imx412
*imx412
,
490 const struct imx412_reg
*regs
, u32 len
)
495 for (i
= 0; i
< len
; i
++) {
496 ret
= imx412_write_reg(imx412
, regs
[i
].address
, 1, regs
[i
].val
);
505 * imx412_update_controls() - Update control ranges based on streaming mode
506 * @imx412: pointer to imx412 device
507 * @mode: pointer to imx412_mode sensor mode
509 * Return: 0 if successful, error code otherwise.
511 static int imx412_update_controls(struct imx412
*imx412
,
512 const struct imx412_mode
*mode
)
516 ret
= __v4l2_ctrl_s_ctrl(imx412
->link_freq_ctrl
, mode
->link_freq_idx
);
520 ret
= __v4l2_ctrl_s_ctrl(imx412
->hblank_ctrl
, mode
->hblank
);
524 return __v4l2_ctrl_modify_range(imx412
->vblank_ctrl
, mode
->vblank_min
,
525 mode
->vblank_max
, 1, mode
->vblank
);
529 * imx412_update_exp_gain() - Set updated exposure and gain
530 * @imx412: pointer to imx412 device
531 * @exposure: updated exposure value
532 * @gain: updated analog gain value
534 * Return: 0 if successful, error code otherwise.
536 static int imx412_update_exp_gain(struct imx412
*imx412
, u32 exposure
, u32 gain
)
541 lpfr
= imx412
->vblank
+ imx412
->cur_mode
->height
;
542 shutter
= lpfr
- exposure
;
544 dev_dbg(imx412
->dev
, "Set exp %u, analog gain %u, shutter %u, lpfr %u",
545 exposure
, gain
, shutter
, lpfr
);
547 ret
= imx412_write_reg(imx412
, IMX412_REG_HOLD
, 1, 1);
551 ret
= imx412_write_reg(imx412
, IMX412_REG_LPFR
, 2, lpfr
);
553 goto error_release_group_hold
;
555 ret
= imx412_write_reg(imx412
, IMX412_REG_EXPOSURE_CIT
, 2, shutter
);
557 goto error_release_group_hold
;
559 ret
= imx412_write_reg(imx412
, IMX412_REG_AGAIN
, 2, gain
);
561 error_release_group_hold
:
562 imx412_write_reg(imx412
, IMX412_REG_HOLD
, 1, 0);
568 * imx412_set_ctrl() - Set subdevice control
569 * @ctrl: pointer to v4l2_ctrl structure
571 * Supported controls:
573 * - cluster controls:
574 * - V4L2_CID_ANALOGUE_GAIN
575 * - V4L2_CID_EXPOSURE
577 * Return: 0 if successful, error code otherwise.
579 static int imx412_set_ctrl(struct v4l2_ctrl
*ctrl
)
581 struct imx412
*imx412
=
582 container_of(ctrl
->handler
, struct imx412
, ctrl_handler
);
588 case V4L2_CID_VBLANK
:
589 imx412
->vblank
= imx412
->vblank_ctrl
->val
;
591 dev_dbg(imx412
->dev
, "Received vblank %u, new lpfr %u",
593 imx412
->vblank
+ imx412
->cur_mode
->height
);
595 ret
= __v4l2_ctrl_modify_range(imx412
->exp_ctrl
,
598 imx412
->cur_mode
->height
-
599 IMX412_EXPOSURE_OFFSET
,
600 1, IMX412_EXPOSURE_DEFAULT
);
602 case V4L2_CID_EXPOSURE
:
603 /* Set controls only if sensor is in power on state */
604 if (!pm_runtime_get_if_in_use(imx412
->dev
))
607 exposure
= ctrl
->val
;
608 analog_gain
= imx412
->again_ctrl
->val
;
610 dev_dbg(imx412
->dev
, "Received exp %u, analog gain %u",
611 exposure
, analog_gain
);
613 ret
= imx412_update_exp_gain(imx412
, exposure
, analog_gain
);
615 pm_runtime_put(imx412
->dev
);
619 dev_err(imx412
->dev
, "Invalid control %d", ctrl
->id
);
626 /* V4l2 subdevice control ops*/
627 static const struct v4l2_ctrl_ops imx412_ctrl_ops
= {
628 .s_ctrl
= imx412_set_ctrl
,
632 * imx412_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
633 * @sd: pointer to imx412 V4L2 sub-device structure
634 * @sd_state: V4L2 sub-device configuration
635 * @code: V4L2 sub-device code enumeration need to be filled
637 * Return: 0 if successful, error code otherwise.
639 static int imx412_enum_mbus_code(struct v4l2_subdev
*sd
,
640 struct v4l2_subdev_state
*sd_state
,
641 struct v4l2_subdev_mbus_code_enum
*code
)
646 code
->code
= supported_mode
.code
;
652 * imx412_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
653 * @sd: pointer to imx412 V4L2 sub-device structure
654 * @sd_state: V4L2 sub-device configuration
655 * @fsize: V4L2 sub-device size enumeration need to be filled
657 * Return: 0 if successful, error code otherwise.
659 static int imx412_enum_frame_size(struct v4l2_subdev
*sd
,
660 struct v4l2_subdev_state
*sd_state
,
661 struct v4l2_subdev_frame_size_enum
*fsize
)
663 if (fsize
->index
> 0)
666 if (fsize
->code
!= supported_mode
.code
)
669 fsize
->min_width
= supported_mode
.width
;
670 fsize
->max_width
= fsize
->min_width
;
671 fsize
->min_height
= supported_mode
.height
;
672 fsize
->max_height
= fsize
->min_height
;
678 * imx412_fill_pad_format() - Fill subdevice pad format
679 * from selected sensor mode
680 * @imx412: pointer to imx412 device
681 * @mode: pointer to imx412_mode sensor mode
682 * @fmt: V4L2 sub-device format need to be filled
684 static void imx412_fill_pad_format(struct imx412
*imx412
,
685 const struct imx412_mode
*mode
,
686 struct v4l2_subdev_format
*fmt
)
688 fmt
->format
.width
= mode
->width
;
689 fmt
->format
.height
= mode
->height
;
690 fmt
->format
.code
= mode
->code
;
691 fmt
->format
.field
= V4L2_FIELD_NONE
;
692 fmt
->format
.colorspace
= V4L2_COLORSPACE_RAW
;
693 fmt
->format
.ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
694 fmt
->format
.quantization
= V4L2_QUANTIZATION_DEFAULT
;
695 fmt
->format
.xfer_func
= V4L2_XFER_FUNC_NONE
;
699 * imx412_get_pad_format() - Get subdevice pad format
700 * @sd: pointer to imx412 V4L2 sub-device structure
701 * @sd_state: V4L2 sub-device configuration
702 * @fmt: V4L2 sub-device format need to be set
704 * Return: 0 if successful, error code otherwise.
706 static int imx412_get_pad_format(struct v4l2_subdev
*sd
,
707 struct v4l2_subdev_state
*sd_state
,
708 struct v4l2_subdev_format
*fmt
)
710 struct imx412
*imx412
= to_imx412(sd
);
712 mutex_lock(&imx412
->mutex
);
714 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
715 struct v4l2_mbus_framefmt
*framefmt
;
717 framefmt
= v4l2_subdev_get_try_format(sd
, sd_state
, fmt
->pad
);
718 fmt
->format
= *framefmt
;
720 imx412_fill_pad_format(imx412
, imx412
->cur_mode
, fmt
);
723 mutex_unlock(&imx412
->mutex
);
729 * imx412_set_pad_format() - Set subdevice pad format
730 * @sd: pointer to imx412 V4L2 sub-device structure
731 * @sd_state: V4L2 sub-device configuration
732 * @fmt: V4L2 sub-device format need to be set
734 * Return: 0 if successful, error code otherwise.
736 static int imx412_set_pad_format(struct v4l2_subdev
*sd
,
737 struct v4l2_subdev_state
*sd_state
,
738 struct v4l2_subdev_format
*fmt
)
740 struct imx412
*imx412
= to_imx412(sd
);
741 const struct imx412_mode
*mode
;
744 mutex_lock(&imx412
->mutex
);
746 mode
= &supported_mode
;
747 imx412_fill_pad_format(imx412
, mode
, fmt
);
749 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
750 struct v4l2_mbus_framefmt
*framefmt
;
752 framefmt
= v4l2_subdev_get_try_format(sd
, sd_state
, fmt
->pad
);
753 *framefmt
= fmt
->format
;
755 ret
= imx412_update_controls(imx412
, mode
);
757 imx412
->cur_mode
= mode
;
760 mutex_unlock(&imx412
->mutex
);
766 * imx412_init_pad_cfg() - Initialize sub-device pad configuration
767 * @sd: pointer to imx412 V4L2 sub-device structure
768 * @sd_state: V4L2 sub-device configuration
770 * Return: 0 if successful, error code otherwise.
772 static int imx412_init_pad_cfg(struct v4l2_subdev
*sd
,
773 struct v4l2_subdev_state
*sd_state
)
775 struct imx412
*imx412
= to_imx412(sd
);
776 struct v4l2_subdev_format fmt
= { 0 };
778 fmt
.which
= sd_state
? V4L2_SUBDEV_FORMAT_TRY
: V4L2_SUBDEV_FORMAT_ACTIVE
;
779 imx412_fill_pad_format(imx412
, &supported_mode
, &fmt
);
781 return imx412_set_pad_format(sd
, sd_state
, &fmt
);
785 * imx412_start_streaming() - Start sensor stream
786 * @imx412: pointer to imx412 device
788 * Return: 0 if successful, error code otherwise.
790 static int imx412_start_streaming(struct imx412
*imx412
)
792 const struct imx412_reg_list
*reg_list
;
795 /* Write sensor mode registers */
796 reg_list
= &imx412
->cur_mode
->reg_list
;
797 ret
= imx412_write_regs(imx412
, reg_list
->regs
,
798 reg_list
->num_of_regs
);
800 dev_err(imx412
->dev
, "fail to write initial registers");
804 /* Setup handler will write actual exposure and gain */
805 ret
= __v4l2_ctrl_handler_setup(imx412
->sd
.ctrl_handler
);
807 dev_err(imx412
->dev
, "fail to setup handler");
811 /* Delay is required before streaming*/
812 usleep_range(7400, 8000);
814 /* Start streaming */
815 ret
= imx412_write_reg(imx412
, IMX412_REG_MODE_SELECT
,
816 1, IMX412_MODE_STREAMING
);
818 dev_err(imx412
->dev
, "fail to start streaming");
826 * imx412_stop_streaming() - Stop sensor stream
827 * @imx412: pointer to imx412 device
829 * Return: 0 if successful, error code otherwise.
831 static int imx412_stop_streaming(struct imx412
*imx412
)
833 return imx412_write_reg(imx412
, IMX412_REG_MODE_SELECT
,
834 1, IMX412_MODE_STANDBY
);
838 * imx412_set_stream() - Enable sensor streaming
839 * @sd: pointer to imx412 subdevice
840 * @enable: set to enable sensor streaming
842 * Return: 0 if successful, error code otherwise.
844 static int imx412_set_stream(struct v4l2_subdev
*sd
, int enable
)
846 struct imx412
*imx412
= to_imx412(sd
);
849 mutex_lock(&imx412
->mutex
);
851 if (imx412
->streaming
== enable
) {
852 mutex_unlock(&imx412
->mutex
);
857 ret
= pm_runtime_resume_and_get(imx412
->dev
);
861 ret
= imx412_start_streaming(imx412
);
863 goto error_power_off
;
865 imx412_stop_streaming(imx412
);
866 pm_runtime_put(imx412
->dev
);
869 imx412
->streaming
= enable
;
871 mutex_unlock(&imx412
->mutex
);
876 pm_runtime_put(imx412
->dev
);
878 mutex_unlock(&imx412
->mutex
);
884 * imx412_detect() - Detect imx412 sensor
885 * @imx412: pointer to imx412 device
887 * Return: 0 if successful, -EIO if sensor id does not match
889 static int imx412_detect(struct imx412
*imx412
)
894 ret
= imx412_read_reg(imx412
, IMX412_REG_ID
, 2, &val
);
898 if (val
!= IMX412_ID
) {
899 dev_err(imx412
->dev
, "chip id mismatch: %x!=%x",
908 * imx412_parse_hw_config() - Parse HW configuration and check if supported
909 * @imx412: pointer to imx412 device
911 * Return: 0 if successful, error code otherwise.
913 static int imx412_parse_hw_config(struct imx412
*imx412
)
915 struct fwnode_handle
*fwnode
= dev_fwnode(imx412
->dev
);
916 struct v4l2_fwnode_endpoint bus_cfg
= {
917 .bus_type
= V4L2_MBUS_CSI2_DPHY
919 struct fwnode_handle
*ep
;
927 /* Request optional reset pin */
928 imx412
->reset_gpio
= devm_gpiod_get_optional(imx412
->dev
, "reset",
930 if (IS_ERR(imx412
->reset_gpio
)) {
931 dev_err(imx412
->dev
, "failed to get reset gpio %ld",
932 PTR_ERR(imx412
->reset_gpio
));
933 return PTR_ERR(imx412
->reset_gpio
);
936 /* Get sensor input clock */
937 imx412
->inclk
= devm_clk_get(imx412
->dev
, NULL
);
938 if (IS_ERR(imx412
->inclk
)) {
939 dev_err(imx412
->dev
, "could not get inclk");
940 return PTR_ERR(imx412
->inclk
);
943 rate
= clk_get_rate(imx412
->inclk
);
944 if (rate
!= IMX412_INCLK_RATE
) {
945 dev_err(imx412
->dev
, "inclk frequency mismatch");
949 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
953 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
954 fwnode_handle_put(ep
);
958 if (bus_cfg
.bus
.mipi_csi2
.num_data_lanes
!= IMX412_NUM_DATA_LANES
) {
960 "number of CSI2 data lanes %d is not supported",
961 bus_cfg
.bus
.mipi_csi2
.num_data_lanes
);
963 goto done_endpoint_free
;
966 if (!bus_cfg
.nr_of_link_frequencies
) {
967 dev_err(imx412
->dev
, "no link frequencies defined");
969 goto done_endpoint_free
;
972 for (i
= 0; i
< bus_cfg
.nr_of_link_frequencies
; i
++)
973 if (bus_cfg
.link_frequencies
[i
] == IMX412_LINK_FREQ
)
974 goto done_endpoint_free
;
979 v4l2_fwnode_endpoint_free(&bus_cfg
);
984 /* V4l2 subdevice ops */
985 static const struct v4l2_subdev_video_ops imx412_video_ops
= {
986 .s_stream
= imx412_set_stream
,
989 static const struct v4l2_subdev_pad_ops imx412_pad_ops
= {
990 .init_cfg
= imx412_init_pad_cfg
,
991 .enum_mbus_code
= imx412_enum_mbus_code
,
992 .enum_frame_size
= imx412_enum_frame_size
,
993 .get_fmt
= imx412_get_pad_format
,
994 .set_fmt
= imx412_set_pad_format
,
997 static const struct v4l2_subdev_ops imx412_subdev_ops
= {
998 .video
= &imx412_video_ops
,
999 .pad
= &imx412_pad_ops
,
1003 * imx412_power_on() - Sensor power on sequence
1004 * @dev: pointer to i2c device
1006 * Return: 0 if successful, error code otherwise.
1008 static int imx412_power_on(struct device
*dev
)
1010 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1011 struct imx412
*imx412
= to_imx412(sd
);
1014 gpiod_set_value_cansleep(imx412
->reset_gpio
, 1);
1016 ret
= clk_prepare_enable(imx412
->inclk
);
1018 dev_err(imx412
->dev
, "fail to enable inclk");
1022 usleep_range(1000, 1200);
1027 gpiod_set_value_cansleep(imx412
->reset_gpio
, 0);
1033 * imx412_power_off() - Sensor power off sequence
1034 * @dev: pointer to i2c device
1036 * Return: 0 if successful, error code otherwise.
1038 static int imx412_power_off(struct device
*dev
)
1040 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1041 struct imx412
*imx412
= to_imx412(sd
);
1043 gpiod_set_value_cansleep(imx412
->reset_gpio
, 0);
1045 clk_disable_unprepare(imx412
->inclk
);
1051 * imx412_init_controls() - Initialize sensor subdevice controls
1052 * @imx412: pointer to imx412 device
1054 * Return: 0 if successful, error code otherwise.
1056 static int imx412_init_controls(struct imx412
*imx412
)
1058 struct v4l2_ctrl_handler
*ctrl_hdlr
= &imx412
->ctrl_handler
;
1059 const struct imx412_mode
*mode
= imx412
->cur_mode
;
1063 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 6);
1067 /* Serialize controls with sensor device */
1068 ctrl_hdlr
->lock
= &imx412
->mutex
;
1070 /* Initialize exposure and gain */
1071 lpfr
= mode
->vblank
+ mode
->height
;
1072 imx412
->exp_ctrl
= v4l2_ctrl_new_std(ctrl_hdlr
,
1075 IMX412_EXPOSURE_MIN
,
1076 lpfr
- IMX412_EXPOSURE_OFFSET
,
1077 IMX412_EXPOSURE_STEP
,
1078 IMX412_EXPOSURE_DEFAULT
);
1080 imx412
->again_ctrl
= v4l2_ctrl_new_std(ctrl_hdlr
,
1082 V4L2_CID_ANALOGUE_GAIN
,
1086 IMX412_AGAIN_DEFAULT
);
1088 v4l2_ctrl_cluster(2, &imx412
->exp_ctrl
);
1090 imx412
->vblank_ctrl
= v4l2_ctrl_new_std(ctrl_hdlr
,
1097 /* Read only controls */
1098 imx412
->pclk_ctrl
= v4l2_ctrl_new_std(ctrl_hdlr
,
1100 V4L2_CID_PIXEL_RATE
,
1101 mode
->pclk
, mode
->pclk
,
1104 imx412
->link_freq_ctrl
= v4l2_ctrl_new_int_menu(ctrl_hdlr
,
1107 ARRAY_SIZE(link_freq
) -
1109 mode
->link_freq_idx
,
1111 if (imx412
->link_freq_ctrl
)
1112 imx412
->link_freq_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1114 imx412
->hblank_ctrl
= v4l2_ctrl_new_std(ctrl_hdlr
,
1120 if (imx412
->hblank_ctrl
)
1121 imx412
->hblank_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1123 if (ctrl_hdlr
->error
) {
1124 dev_err(imx412
->dev
, "control init failed: %d",
1126 v4l2_ctrl_handler_free(ctrl_hdlr
);
1127 return ctrl_hdlr
->error
;
1130 imx412
->sd
.ctrl_handler
= ctrl_hdlr
;
1136 * imx412_probe() - I2C client device binding
1137 * @client: pointer to i2c client device
1139 * Return: 0 if successful, error code otherwise.
1141 static int imx412_probe(struct i2c_client
*client
)
1143 struct imx412
*imx412
;
1146 imx412
= devm_kzalloc(&client
->dev
, sizeof(*imx412
), GFP_KERNEL
);
1150 imx412
->dev
= &client
->dev
;
1152 /* Initialize subdev */
1153 v4l2_i2c_subdev_init(&imx412
->sd
, client
, &imx412_subdev_ops
);
1155 ret
= imx412_parse_hw_config(imx412
);
1157 dev_err(imx412
->dev
, "HW configuration is not supported");
1161 mutex_init(&imx412
->mutex
);
1163 ret
= imx412_power_on(imx412
->dev
);
1165 dev_err(imx412
->dev
, "failed to power-on the sensor");
1166 goto error_mutex_destroy
;
1169 /* Check module identity */
1170 ret
= imx412_detect(imx412
);
1172 dev_err(imx412
->dev
, "failed to find sensor: %d", ret
);
1173 goto error_power_off
;
1176 /* Set default mode to max resolution */
1177 imx412
->cur_mode
= &supported_mode
;
1178 imx412
->vblank
= imx412
->cur_mode
->vblank
;
1180 ret
= imx412_init_controls(imx412
);
1182 dev_err(imx412
->dev
, "failed to init controls: %d", ret
);
1183 goto error_power_off
;
1186 /* Initialize subdev */
1187 imx412
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1188 imx412
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1190 /* Initialize source pad */
1191 imx412
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1192 ret
= media_entity_pads_init(&imx412
->sd
.entity
, 1, &imx412
->pad
);
1194 dev_err(imx412
->dev
, "failed to init entity pads: %d", ret
);
1195 goto error_handler_free
;
1198 ret
= v4l2_async_register_subdev_sensor(&imx412
->sd
);
1200 dev_err(imx412
->dev
,
1201 "failed to register async subdev: %d", ret
);
1202 goto error_media_entity
;
1205 pm_runtime_set_active(imx412
->dev
);
1206 pm_runtime_enable(imx412
->dev
);
1207 pm_runtime_idle(imx412
->dev
);
1212 media_entity_cleanup(&imx412
->sd
.entity
);
1214 v4l2_ctrl_handler_free(imx412
->sd
.ctrl_handler
);
1216 imx412_power_off(imx412
->dev
);
1217 error_mutex_destroy
:
1218 mutex_destroy(&imx412
->mutex
);
1224 * imx412_remove() - I2C client device unbinding
1225 * @client: pointer to I2C client device
1227 * Return: 0 if successful, error code otherwise.
1229 static int imx412_remove(struct i2c_client
*client
)
1231 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1232 struct imx412
*imx412
= to_imx412(sd
);
1234 v4l2_async_unregister_subdev(sd
);
1235 media_entity_cleanup(&sd
->entity
);
1236 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1238 pm_runtime_disable(&client
->dev
);
1239 if (!pm_runtime_status_suspended(&client
->dev
))
1240 imx412_power_off(&client
->dev
);
1241 pm_runtime_set_suspended(&client
->dev
);
1243 mutex_destroy(&imx412
->mutex
);
1248 static const struct dev_pm_ops imx412_pm_ops
= {
1249 SET_RUNTIME_PM_OPS(imx412_power_off
, imx412_power_on
, NULL
)
1252 static const struct of_device_id imx412_of_match
[] = {
1253 { .compatible
= "sony,imx412" },
1257 MODULE_DEVICE_TABLE(of
, imx412_of_match
);
1259 static struct i2c_driver imx412_driver
= {
1260 .probe_new
= imx412_probe
,
1261 .remove
= imx412_remove
,
1264 .pm
= &imx412_pm_ops
,
1265 .of_match_table
= imx412_of_match
,
1269 module_i2c_driver(imx412_driver
);
1271 MODULE_DESCRIPTION("Sony imx412 sensor driver");
1272 MODULE_LICENSE("GPL");