1 // SPDX-License-Identifier: GPL-2.0-only
3 * Sony imx334 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>
15 #include <media/v4l2-ctrls.h>
16 #include <media/v4l2-fwnode.h>
17 #include <media/v4l2-subdev.h>
20 #define IMX334_REG_MODE_SELECT 0x3000
21 #define IMX334_MODE_STANDBY 0x01
22 #define IMX334_MODE_STREAMING 0x00
25 #define IMX334_REG_LPFR 0x3030
28 #define IMX334_REG_ID 0x3044
29 #define IMX334_ID 0x1e
31 /* Exposure control */
32 #define IMX334_REG_SHUTTER 0x3058
33 #define IMX334_EXPOSURE_MIN 1
34 #define IMX334_EXPOSURE_OFFSET 5
35 #define IMX334_EXPOSURE_STEP 1
36 #define IMX334_EXPOSURE_DEFAULT 0x0648
38 /* Analog gain control */
39 #define IMX334_REG_AGAIN 0x30e8
40 #define IMX334_AGAIN_MIN 0
41 #define IMX334_AGAIN_MAX 240
42 #define IMX334_AGAIN_STEP 1
43 #define IMX334_AGAIN_DEFAULT 0
45 /* Group hold register */
46 #define IMX334_REG_HOLD 0x3001
48 /* Input clock rate */
49 #define IMX334_INCLK_RATE 24000000
51 /* CSI2 HW configuration */
52 #define IMX334_LINK_FREQ_891M 891000000
53 #define IMX334_LINK_FREQ_445M 445500000
54 #define IMX334_NUM_DATA_LANES 4
56 #define IMX334_REG_MIN 0x00
57 #define IMX334_REG_MAX 0xfffff
59 /* Test Pattern Control */
60 #define IMX334_REG_TP 0x329e
61 #define IMX334_TP_COLOR_HBARS 0xA
62 #define IMX334_TP_COLOR_VBARS 0xB
64 #define IMX334_TPG_EN_DOUT 0x329c
65 #define IMX334_TP_ENABLE 0x1
66 #define IMX334_TP_DISABLE 0x0
68 #define IMX334_TPG_COLORW 0x32a0
69 #define IMX334_TPG_COLORW_120P 0x13
71 #define IMX334_TP_CLK_EN 0x3148
72 #define IMX334_TP_CLK_EN_VAL 0x10
73 #define IMX334_TP_CLK_DIS_VAL 0x0
75 #define IMX334_DIG_CLP_MODE 0x3280
78 * struct imx334_reg - imx334 sensor register
79 * @address: Register address
80 * @val: Register value
88 * struct imx334_reg_list - imx334 sensor register list
89 * @num_of_regs: Number of registers in the list
90 * @regs: Pointer to register list
92 struct imx334_reg_list
{
94 const struct imx334_reg
*regs
;
98 * struct imx334_mode - imx334 sensor mode structure
100 * @height: Frame height
101 * @hblank: Horizontal blanking in lines
102 * @vblank: Vertical blanking in lines
103 * @vblank_min: Minimal vertical blanking in lines
104 * @vblank_max: Maximum vertical blanking in lines
105 * @pclk: Sensor pixel clock
106 * @link_freq_idx: Link frequency index
107 * @reg_list: Register list for sensor mode
118 struct imx334_reg_list reg_list
;
122 * struct imx334 - imx334 sensor device structure
123 * @dev: Pointer to generic device
124 * @client: Pointer to i2c client
125 * @sd: V4L2 sub-device
126 * @pad: Media pad. Only one pad supported
127 * @reset_gpio: Sensor reset gpio
128 * @inclk: Sensor input clock
129 * @ctrl_handler: V4L2 control handler
130 * @link_freq_ctrl: Pointer to link frequency control
131 * @pclk_ctrl: Pointer to pixel clock control
132 * @hblank_ctrl: Pointer to horizontal blanking control
133 * @vblank_ctrl: Pointer to vertical blanking control
134 * @exp_ctrl: Pointer to exposure control
135 * @again_ctrl: Pointer to analog gain control
136 * @vblank: Vertical blanking in lines
137 * @cur_mode: Pointer to current selected sensor mode
138 * @mutex: Mutex for serializing sensor controls
139 * @link_freq_bitmap: Menu bitmap for link_freq_ctrl
140 * @cur_code: current selected format code
144 struct i2c_client
*client
;
145 struct v4l2_subdev sd
;
146 struct media_pad pad
;
147 struct gpio_desc
*reset_gpio
;
149 struct v4l2_ctrl_handler ctrl_handler
;
150 struct v4l2_ctrl
*link_freq_ctrl
;
151 struct v4l2_ctrl
*pclk_ctrl
;
152 struct v4l2_ctrl
*hblank_ctrl
;
153 struct v4l2_ctrl
*vblank_ctrl
;
155 struct v4l2_ctrl
*exp_ctrl
;
156 struct v4l2_ctrl
*again_ctrl
;
159 const struct imx334_mode
*cur_mode
;
161 unsigned long link_freq_bitmap
;
165 static const s64 link_freq
[] = {
166 IMX334_LINK_FREQ_891M
,
167 IMX334_LINK_FREQ_445M
,
170 /* Sensor mode registers for 1920x1080@30fps */
171 static const struct imx334_reg mode_1920x1080_regs
[] = {
333 /* Sensor mode registers for 3840x2160@30fps */
334 static const struct imx334_reg mode_3840x2160_regs
[] = {
449 static const char * const imx334_test_pattern_menu
[] = {
451 "Vertical Color Bars",
452 "Horizontal Color Bars",
455 static const int imx334_test_pattern_val
[] = {
457 IMX334_TP_COLOR_HBARS
,
458 IMX334_TP_COLOR_VBARS
,
461 static const struct imx334_reg raw10_framefmt_regs
[] = {
468 static const struct imx334_reg raw12_framefmt_regs
[] = {
475 static const u32 imx334_mbus_codes
[] = {
476 MEDIA_BUS_FMT_SRGGB12_1X12
,
477 MEDIA_BUS_FMT_SRGGB10_1X10
,
480 /* Supported sensor mode configurations */
481 static const struct imx334_mode supported_modes
[] = {
488 .vblank_max
= 132840,
492 .num_of_regs
= ARRAY_SIZE(mode_3840x2160_regs
),
493 .regs
= mode_3840x2160_regs
,
501 .vblank_max
= 132840,
505 .num_of_regs
= ARRAY_SIZE(mode_1920x1080_regs
),
506 .regs
= mode_1920x1080_regs
,
512 * to_imx334() - imv334 V4L2 sub-device to imx334 device.
513 * @subdev: pointer to imx334 V4L2 sub-device
515 * Return: pointer to imx334 device
517 static inline struct imx334
*to_imx334(struct v4l2_subdev
*subdev
)
519 return container_of(subdev
, struct imx334
, sd
);
523 * imx334_read_reg() - Read registers.
524 * @imx334: pointer to imx334 device
525 * @reg: register address
526 * @len: length of bytes to read. Max supported bytes is 4
527 * @val: pointer to register value to be filled.
529 * Big endian register addresses with little endian values.
531 * Return: 0 if successful, error code otherwise.
533 static int imx334_read_reg(struct imx334
*imx334
, u16 reg
, u32 len
, u32
*val
)
535 struct i2c_client
*client
= v4l2_get_subdevdata(&imx334
->sd
);
536 struct i2c_msg msgs
[2] = {0};
537 u8 addr_buf
[2] = {0};
538 u8 data_buf
[4] = {0};
541 if (WARN_ON(len
> 4))
544 put_unaligned_be16(reg
, addr_buf
);
546 /* Write register address */
547 msgs
[0].addr
= client
->addr
;
549 msgs
[0].len
= ARRAY_SIZE(addr_buf
);
550 msgs
[0].buf
= addr_buf
;
552 /* Read data from register */
553 msgs
[1].addr
= client
->addr
;
554 msgs
[1].flags
= I2C_M_RD
;
556 msgs
[1].buf
= data_buf
;
558 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
559 if (ret
!= ARRAY_SIZE(msgs
))
562 *val
= get_unaligned_le32(data_buf
);
568 * imx334_write_reg() - Write register
569 * @imx334: pointer to imx334 device
570 * @reg: register address
571 * @len: length of bytes. Max supported bytes is 4
572 * @val: register value
574 * Big endian register addresses with little endian values.
576 * Return: 0 if successful, error code otherwise.
578 static int imx334_write_reg(struct imx334
*imx334
, u16 reg
, u32 len
, u32 val
)
580 struct i2c_client
*client
= v4l2_get_subdevdata(&imx334
->sd
);
583 if (WARN_ON(len
> 4))
586 put_unaligned_be16(reg
, buf
);
587 put_unaligned_le32(val
, buf
+ 2);
588 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
595 * imx334_write_regs() - Write a list of registers
596 * @imx334: pointer to imx334 device
597 * @regs: list of registers to be written
598 * @len: length of registers array
600 * Return: 0 if successful, error code otherwise.
602 static int imx334_write_regs(struct imx334
*imx334
,
603 const struct imx334_reg
*regs
, u32 len
)
608 for (i
= 0; i
< len
; i
++) {
609 ret
= imx334_write_reg(imx334
, regs
[i
].address
, 1, regs
[i
].val
);
618 * imx334_update_controls() - Update control ranges based on streaming mode
619 * @imx334: pointer to imx334 device
620 * @mode: pointer to imx334_mode sensor mode
622 * Return: 0 if successful, error code otherwise.
624 static int imx334_update_controls(struct imx334
*imx334
,
625 const struct imx334_mode
*mode
)
629 ret
= __v4l2_ctrl_s_ctrl(imx334
->link_freq_ctrl
, mode
->link_freq_idx
);
633 ret
= __v4l2_ctrl_modify_range(imx334
->pclk_ctrl
, mode
->pclk
,
634 mode
->pclk
, 1, mode
->pclk
);
638 ret
= __v4l2_ctrl_modify_range(imx334
->hblank_ctrl
, mode
->hblank
,
639 mode
->hblank
, 1, mode
->hblank
);
643 ret
= __v4l2_ctrl_modify_range(imx334
->vblank_ctrl
, mode
->vblank_min
,
644 mode
->vblank_max
, 1, mode
->vblank
);
648 return __v4l2_ctrl_s_ctrl(imx334
->vblank_ctrl
, mode
->vblank
);
652 * imx334_update_exp_gain() - Set updated exposure and gain
653 * @imx334: pointer to imx334 device
654 * @exposure: updated exposure value
655 * @gain: updated analog gain value
657 * Return: 0 if successful, error code otherwise.
659 static int imx334_update_exp_gain(struct imx334
*imx334
, u32 exposure
, u32 gain
)
664 lpfr
= imx334
->vblank
+ imx334
->cur_mode
->height
;
665 shutter
= lpfr
- exposure
;
667 dev_dbg(imx334
->dev
, "Set long exp %u analog gain %u sh0 %u lpfr %u",
668 exposure
, gain
, shutter
, lpfr
);
670 ret
= imx334_write_reg(imx334
, IMX334_REG_HOLD
, 1, 1);
674 ret
= imx334_write_reg(imx334
, IMX334_REG_LPFR
, 3, lpfr
);
676 goto error_release_group_hold
;
678 ret
= imx334_write_reg(imx334
, IMX334_REG_SHUTTER
, 3, shutter
);
680 goto error_release_group_hold
;
682 ret
= imx334_write_reg(imx334
, IMX334_REG_AGAIN
, 1, gain
);
684 error_release_group_hold
:
685 imx334_write_reg(imx334
, IMX334_REG_HOLD
, 1, 0);
691 * imx334_set_ctrl() - Set subdevice control
692 * @ctrl: pointer to v4l2_ctrl structure
694 * Supported controls:
696 * - cluster controls:
697 * - V4L2_CID_ANALOGUE_GAIN
698 * - V4L2_CID_EXPOSURE
700 * Return: 0 if successful, error code otherwise.
702 static int imx334_set_ctrl(struct v4l2_ctrl
*ctrl
)
704 struct imx334
*imx334
=
705 container_of(ctrl
->handler
, struct imx334
, ctrl_handler
);
711 case V4L2_CID_VBLANK
:
712 imx334
->vblank
= imx334
->vblank_ctrl
->val
;
714 dev_dbg(imx334
->dev
, "Received vblank %u, new lpfr %u",
716 imx334
->vblank
+ imx334
->cur_mode
->height
);
718 ret
= __v4l2_ctrl_modify_range(imx334
->exp_ctrl
,
721 imx334
->cur_mode
->height
-
722 IMX334_EXPOSURE_OFFSET
,
723 1, IMX334_EXPOSURE_DEFAULT
);
725 case V4L2_CID_EXPOSURE
:
727 /* Set controls only if sensor is in power on state */
728 if (!pm_runtime_get_if_in_use(imx334
->dev
))
731 exposure
= ctrl
->val
;
732 analog_gain
= imx334
->again_ctrl
->val
;
734 dev_dbg(imx334
->dev
, "Received exp %u analog gain %u",
735 exposure
, analog_gain
);
737 ret
= imx334_update_exp_gain(imx334
, exposure
, analog_gain
);
739 pm_runtime_put(imx334
->dev
);
742 case V4L2_CID_PIXEL_RATE
:
743 case V4L2_CID_LINK_FREQ
:
744 case V4L2_CID_HBLANK
:
747 case V4L2_CID_TEST_PATTERN
:
749 imx334_write_reg(imx334
, IMX334_TP_CLK_EN
, 1,
750 IMX334_TP_CLK_EN_VAL
);
751 imx334_write_reg(imx334
, IMX334_DIG_CLP_MODE
, 1, 0x0);
752 imx334_write_reg(imx334
, IMX334_TPG_COLORW
, 1,
753 IMX334_TPG_COLORW_120P
);
754 imx334_write_reg(imx334
, IMX334_REG_TP
, 1,
755 imx334_test_pattern_val
[ctrl
->val
]);
756 imx334_write_reg(imx334
, IMX334_TPG_EN_DOUT
, 1,
759 imx334_write_reg(imx334
, IMX334_DIG_CLP_MODE
, 1, 0x1);
760 imx334_write_reg(imx334
, IMX334_TP_CLK_EN
, 1,
761 IMX334_TP_CLK_DIS_VAL
);
762 imx334_write_reg(imx334
, IMX334_TPG_EN_DOUT
, 1,
768 dev_err(imx334
->dev
, "Invalid control %d", ctrl
->id
);
775 /* V4l2 subdevice control ops*/
776 static const struct v4l2_ctrl_ops imx334_ctrl_ops
= {
777 .s_ctrl
= imx334_set_ctrl
,
780 static int imx334_get_format_code(struct imx334
*imx334
, u32 code
)
784 for (i
= 0; i
< ARRAY_SIZE(imx334_mbus_codes
); i
++) {
785 if (imx334_mbus_codes
[i
] == code
)
786 return imx334_mbus_codes
[i
];
789 return imx334_mbus_codes
[0];
793 * imx334_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
794 * @sd: pointer to imx334 V4L2 sub-device structure
795 * @sd_state: V4L2 sub-device state
796 * @code: V4L2 sub-device code enumeration need to be filled
798 * Return: 0 if successful, error code otherwise.
800 static int imx334_enum_mbus_code(struct v4l2_subdev
*sd
,
801 struct v4l2_subdev_state
*sd_state
,
802 struct v4l2_subdev_mbus_code_enum
*code
)
804 if (code
->index
>= ARRAY_SIZE(imx334_mbus_codes
))
807 code
->code
= imx334_mbus_codes
[code
->index
];
813 * imx334_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
814 * @sd: pointer to imx334 V4L2 sub-device structure
815 * @sd_state: V4L2 sub-device state
816 * @fsize: V4L2 sub-device size enumeration need to be filled
818 * Return: 0 if successful, error code otherwise.
820 static int imx334_enum_frame_size(struct v4l2_subdev
*sd
,
821 struct v4l2_subdev_state
*sd_state
,
822 struct v4l2_subdev_frame_size_enum
*fsize
)
824 struct imx334
*imx334
= to_imx334(sd
);
827 if (fsize
->index
>= ARRAY_SIZE(supported_modes
))
830 code
= imx334_get_format_code(imx334
, fsize
->code
);
832 if (fsize
->code
!= code
)
835 fsize
->min_width
= supported_modes
[fsize
->index
].width
;
836 fsize
->max_width
= fsize
->min_width
;
837 fsize
->min_height
= supported_modes
[fsize
->index
].height
;
838 fsize
->max_height
= fsize
->min_height
;
844 * imx334_fill_pad_format() - Fill subdevice pad format
845 * from selected sensor mode
846 * @imx334: pointer to imx334 device
847 * @mode: pointer to imx334_mode sensor mode
848 * @fmt: V4L2 sub-device format need to be filled
850 static void imx334_fill_pad_format(struct imx334
*imx334
,
851 const struct imx334_mode
*mode
,
852 struct v4l2_subdev_format
*fmt
)
854 fmt
->format
.width
= mode
->width
;
855 fmt
->format
.height
= mode
->height
;
856 fmt
->format
.field
= V4L2_FIELD_NONE
;
857 fmt
->format
.colorspace
= V4L2_COLORSPACE_RAW
;
858 fmt
->format
.ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
859 fmt
->format
.quantization
= V4L2_QUANTIZATION_DEFAULT
;
860 fmt
->format
.xfer_func
= V4L2_XFER_FUNC_NONE
;
864 * imx334_get_pad_format() - Get subdevice pad format
865 * @sd: pointer to imx334 V4L2 sub-device structure
866 * @sd_state: V4L2 sub-device state
867 * @fmt: V4L2 sub-device format need to be set
869 * Return: 0 if successful, error code otherwise.
871 static int imx334_get_pad_format(struct v4l2_subdev
*sd
,
872 struct v4l2_subdev_state
*sd_state
,
873 struct v4l2_subdev_format
*fmt
)
875 struct imx334
*imx334
= to_imx334(sd
);
877 mutex_lock(&imx334
->mutex
);
879 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
880 struct v4l2_mbus_framefmt
*framefmt
;
882 framefmt
= v4l2_subdev_state_get_format(sd_state
, fmt
->pad
);
883 fmt
->format
= *framefmt
;
885 fmt
->format
.code
= imx334
->cur_code
;
886 imx334_fill_pad_format(imx334
, imx334
->cur_mode
, fmt
);
889 mutex_unlock(&imx334
->mutex
);
895 * imx334_set_pad_format() - Set subdevice pad format
896 * @sd: pointer to imx334 V4L2 sub-device structure
897 * @sd_state: V4L2 sub-device state
898 * @fmt: V4L2 sub-device format need to be set
900 * Return: 0 if successful, error code otherwise.
902 static int imx334_set_pad_format(struct v4l2_subdev
*sd
,
903 struct v4l2_subdev_state
*sd_state
,
904 struct v4l2_subdev_format
*fmt
)
906 struct imx334
*imx334
= to_imx334(sd
);
907 const struct imx334_mode
*mode
;
910 mutex_lock(&imx334
->mutex
);
912 mode
= v4l2_find_nearest_size(supported_modes
,
913 ARRAY_SIZE(supported_modes
),
915 fmt
->format
.width
, fmt
->format
.height
);
917 imx334_fill_pad_format(imx334
, mode
, fmt
);
918 fmt
->format
.code
= imx334_get_format_code(imx334
, fmt
->format
.code
);
920 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
921 struct v4l2_mbus_framefmt
*framefmt
;
923 framefmt
= v4l2_subdev_state_get_format(sd_state
, fmt
->pad
);
924 *framefmt
= fmt
->format
;
925 } else if (imx334
->cur_mode
!= mode
|| imx334
->cur_code
!= fmt
->format
.code
) {
926 imx334
->cur_code
= fmt
->format
.code
;
927 ret
= imx334_update_controls(imx334
, mode
);
929 imx334
->cur_mode
= mode
;
932 mutex_unlock(&imx334
->mutex
);
938 * imx334_init_state() - Initialize sub-device state
939 * @sd: pointer to imx334 V4L2 sub-device structure
940 * @sd_state: V4L2 sub-device state
942 * Return: 0 if successful, error code otherwise.
944 static int imx334_init_state(struct v4l2_subdev
*sd
,
945 struct v4l2_subdev_state
*sd_state
)
947 struct imx334
*imx334
= to_imx334(sd
);
948 struct v4l2_subdev_format fmt
= { 0 };
950 fmt
.which
= sd_state
? V4L2_SUBDEV_FORMAT_TRY
: V4L2_SUBDEV_FORMAT_ACTIVE
;
952 mutex_lock(&imx334
->mutex
);
954 imx334_fill_pad_format(imx334
, imx334
->cur_mode
, &fmt
);
956 __v4l2_ctrl_modify_range(imx334
->link_freq_ctrl
, 0,
957 __fls(imx334
->link_freq_bitmap
),
958 ~(imx334
->link_freq_bitmap
),
959 __ffs(imx334
->link_freq_bitmap
));
961 mutex_unlock(&imx334
->mutex
);
963 return imx334_set_pad_format(sd
, sd_state
, &fmt
);
966 static int imx334_set_framefmt(struct imx334
*imx334
)
968 switch (imx334
->cur_code
) {
969 case MEDIA_BUS_FMT_SRGGB10_1X10
:
970 return imx334_write_regs(imx334
, raw10_framefmt_regs
,
971 ARRAY_SIZE(raw10_framefmt_regs
));
973 case MEDIA_BUS_FMT_SRGGB12_1X12
:
974 return imx334_write_regs(imx334
, raw12_framefmt_regs
,
975 ARRAY_SIZE(raw12_framefmt_regs
));
982 * imx334_start_streaming() - Start sensor stream
983 * @imx334: pointer to imx334 device
985 * Return: 0 if successful, error code otherwise.
987 static int imx334_start_streaming(struct imx334
*imx334
)
989 const struct imx334_reg_list
*reg_list
;
992 /* Write sensor mode registers */
993 reg_list
= &imx334
->cur_mode
->reg_list
;
994 ret
= imx334_write_regs(imx334
, reg_list
->regs
,
995 reg_list
->num_of_regs
);
997 dev_err(imx334
->dev
, "fail to write initial registers");
1001 ret
= imx334_set_framefmt(imx334
);
1003 dev_err(imx334
->dev
, "%s failed to set frame format: %d\n",
1008 /* Setup handler will write actual exposure and gain */
1009 ret
= __v4l2_ctrl_handler_setup(imx334
->sd
.ctrl_handler
);
1011 dev_err(imx334
->dev
, "fail to setup handler");
1015 /* Start streaming */
1016 ret
= imx334_write_reg(imx334
, IMX334_REG_MODE_SELECT
,
1017 1, IMX334_MODE_STREAMING
);
1019 dev_err(imx334
->dev
, "fail to start streaming");
1027 * imx334_stop_streaming() - Stop sensor stream
1028 * @imx334: pointer to imx334 device
1030 * Return: 0 if successful, error code otherwise.
1032 static int imx334_stop_streaming(struct imx334
*imx334
)
1034 return imx334_write_reg(imx334
, IMX334_REG_MODE_SELECT
,
1035 1, IMX334_MODE_STANDBY
);
1039 * imx334_set_stream() - Enable sensor streaming
1040 * @sd: pointer to imx334 subdevice
1041 * @enable: set to enable sensor streaming
1043 * Return: 0 if successful, error code otherwise.
1045 static int imx334_set_stream(struct v4l2_subdev
*sd
, int enable
)
1047 struct imx334
*imx334
= to_imx334(sd
);
1050 mutex_lock(&imx334
->mutex
);
1053 ret
= pm_runtime_resume_and_get(imx334
->dev
);
1057 ret
= imx334_start_streaming(imx334
);
1059 goto error_power_off
;
1061 imx334_stop_streaming(imx334
);
1062 pm_runtime_put(imx334
->dev
);
1065 mutex_unlock(&imx334
->mutex
);
1070 pm_runtime_put(imx334
->dev
);
1072 mutex_unlock(&imx334
->mutex
);
1078 * imx334_detect() - Detect imx334 sensor
1079 * @imx334: pointer to imx334 device
1081 * Return: 0 if successful, -EIO if sensor id does not match
1083 static int imx334_detect(struct imx334
*imx334
)
1088 ret
= imx334_read_reg(imx334
, IMX334_REG_ID
, 2, &val
);
1092 if (val
!= IMX334_ID
) {
1093 dev_err(imx334
->dev
, "chip id mismatch: %x!=%x",
1102 * imx334_parse_hw_config() - Parse HW configuration and check if supported
1103 * @imx334: pointer to imx334 device
1105 * Return: 0 if successful, error code otherwise.
1107 static int imx334_parse_hw_config(struct imx334
*imx334
)
1109 struct fwnode_handle
*fwnode
= dev_fwnode(imx334
->dev
);
1110 struct v4l2_fwnode_endpoint bus_cfg
= {
1111 .bus_type
= V4L2_MBUS_CSI2_DPHY
1113 struct fwnode_handle
*ep
;
1120 /* Request optional reset pin */
1121 imx334
->reset_gpio
= devm_gpiod_get_optional(imx334
->dev
, "reset",
1123 if (IS_ERR(imx334
->reset_gpio
)) {
1124 dev_err(imx334
->dev
, "failed to get reset gpio %ld",
1125 PTR_ERR(imx334
->reset_gpio
));
1126 return PTR_ERR(imx334
->reset_gpio
);
1129 /* Get sensor input clock */
1130 imx334
->inclk
= devm_clk_get(imx334
->dev
, NULL
);
1131 if (IS_ERR(imx334
->inclk
)) {
1132 dev_err(imx334
->dev
, "could not get inclk");
1133 return PTR_ERR(imx334
->inclk
);
1136 rate
= clk_get_rate(imx334
->inclk
);
1137 if (rate
!= IMX334_INCLK_RATE
) {
1138 dev_err(imx334
->dev
, "inclk frequency mismatch");
1142 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
1146 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
1147 fwnode_handle_put(ep
);
1151 if (bus_cfg
.bus
.mipi_csi2
.num_data_lanes
!= IMX334_NUM_DATA_LANES
) {
1152 dev_err(imx334
->dev
,
1153 "number of CSI2 data lanes %d is not supported",
1154 bus_cfg
.bus
.mipi_csi2
.num_data_lanes
);
1156 goto done_endpoint_free
;
1159 ret
= v4l2_link_freq_to_bitmap(imx334
->dev
, bus_cfg
.link_frequencies
,
1160 bus_cfg
.nr_of_link_frequencies
,
1161 link_freq
, ARRAY_SIZE(link_freq
),
1162 &imx334
->link_freq_bitmap
);
1165 v4l2_fwnode_endpoint_free(&bus_cfg
);
1170 /* V4l2 subdevice ops */
1171 static const struct v4l2_subdev_video_ops imx334_video_ops
= {
1172 .s_stream
= imx334_set_stream
,
1175 static const struct v4l2_subdev_pad_ops imx334_pad_ops
= {
1176 .enum_mbus_code
= imx334_enum_mbus_code
,
1177 .enum_frame_size
= imx334_enum_frame_size
,
1178 .get_fmt
= imx334_get_pad_format
,
1179 .set_fmt
= imx334_set_pad_format
,
1182 static const struct v4l2_subdev_ops imx334_subdev_ops
= {
1183 .video
= &imx334_video_ops
,
1184 .pad
= &imx334_pad_ops
,
1187 static const struct v4l2_subdev_internal_ops imx334_internal_ops
= {
1188 .init_state
= imx334_init_state
,
1192 * imx334_power_on() - Sensor power on sequence
1193 * @dev: pointer to i2c device
1195 * Return: 0 if successful, error code otherwise.
1197 static int imx334_power_on(struct device
*dev
)
1199 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1200 struct imx334
*imx334
= to_imx334(sd
);
1203 gpiod_set_value_cansleep(imx334
->reset_gpio
, 1);
1205 ret
= clk_prepare_enable(imx334
->inclk
);
1207 dev_err(imx334
->dev
, "fail to enable inclk");
1211 usleep_range(18000, 20000);
1216 gpiod_set_value_cansleep(imx334
->reset_gpio
, 0);
1222 * imx334_power_off() - Sensor power off sequence
1223 * @dev: pointer to i2c device
1225 * Return: 0 if successful, error code otherwise.
1227 static int imx334_power_off(struct device
*dev
)
1229 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1230 struct imx334
*imx334
= to_imx334(sd
);
1232 gpiod_set_value_cansleep(imx334
->reset_gpio
, 0);
1234 clk_disable_unprepare(imx334
->inclk
);
1240 * imx334_init_controls() - Initialize sensor subdevice controls
1241 * @imx334: pointer to imx334 device
1243 * Return: 0 if successful, error code otherwise.
1245 static int imx334_init_controls(struct imx334
*imx334
)
1247 struct v4l2_ctrl_handler
*ctrl_hdlr
= &imx334
->ctrl_handler
;
1248 const struct imx334_mode
*mode
= imx334
->cur_mode
;
1252 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 7);
1256 /* Serialize controls with sensor device */
1257 ctrl_hdlr
->lock
= &imx334
->mutex
;
1259 /* Initialize exposure and gain */
1260 lpfr
= mode
->vblank
+ mode
->height
;
1261 imx334
->exp_ctrl
= v4l2_ctrl_new_std(ctrl_hdlr
,
1264 IMX334_EXPOSURE_MIN
,
1265 lpfr
- IMX334_EXPOSURE_OFFSET
,
1266 IMX334_EXPOSURE_STEP
,
1267 IMX334_EXPOSURE_DEFAULT
);
1269 imx334
->again_ctrl
= v4l2_ctrl_new_std(ctrl_hdlr
,
1271 V4L2_CID_ANALOGUE_GAIN
,
1275 IMX334_AGAIN_DEFAULT
);
1277 v4l2_ctrl_cluster(2, &imx334
->exp_ctrl
);
1279 imx334
->vblank_ctrl
= v4l2_ctrl_new_std(ctrl_hdlr
,
1286 /* Read only controls */
1287 imx334
->pclk_ctrl
= v4l2_ctrl_new_std(ctrl_hdlr
,
1289 V4L2_CID_PIXEL_RATE
,
1290 mode
->pclk
, mode
->pclk
,
1293 imx334
->link_freq_ctrl
= v4l2_ctrl_new_int_menu(ctrl_hdlr
,
1296 __fls(imx334
->link_freq_bitmap
),
1297 __ffs(imx334
->link_freq_bitmap
),
1300 if (imx334
->link_freq_ctrl
)
1301 imx334
->link_freq_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1303 imx334
->hblank_ctrl
= v4l2_ctrl_new_std(ctrl_hdlr
,
1309 if (imx334
->hblank_ctrl
)
1310 imx334
->hblank_ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1312 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &imx334_ctrl_ops
,
1313 V4L2_CID_TEST_PATTERN
,
1314 ARRAY_SIZE(imx334_test_pattern_menu
) - 1,
1315 0, 0, imx334_test_pattern_menu
);
1317 if (ctrl_hdlr
->error
) {
1318 dev_err(imx334
->dev
, "control init failed: %d",
1320 v4l2_ctrl_handler_free(ctrl_hdlr
);
1321 return ctrl_hdlr
->error
;
1324 imx334
->sd
.ctrl_handler
= ctrl_hdlr
;
1330 * imx334_probe() - I2C client device binding
1331 * @client: pointer to i2c client device
1333 * Return: 0 if successful, error code otherwise.
1335 static int imx334_probe(struct i2c_client
*client
)
1337 struct imx334
*imx334
;
1340 imx334
= devm_kzalloc(&client
->dev
, sizeof(*imx334
), GFP_KERNEL
);
1344 imx334
->dev
= &client
->dev
;
1346 /* Initialize subdev */
1347 v4l2_i2c_subdev_init(&imx334
->sd
, client
, &imx334_subdev_ops
);
1348 imx334
->sd
.internal_ops
= &imx334_internal_ops
;
1350 ret
= imx334_parse_hw_config(imx334
);
1352 dev_err(imx334
->dev
, "HW configuration is not supported");
1356 mutex_init(&imx334
->mutex
);
1358 ret
= imx334_power_on(imx334
->dev
);
1360 dev_err(imx334
->dev
, "failed to power-on the sensor");
1361 goto error_mutex_destroy
;
1364 /* Check module identity */
1365 ret
= imx334_detect(imx334
);
1367 dev_err(imx334
->dev
, "failed to find sensor: %d", ret
);
1368 goto error_power_off
;
1371 /* Set default mode to max resolution */
1372 imx334
->cur_mode
= &supported_modes
[__ffs(imx334
->link_freq_bitmap
)];
1373 imx334
->cur_code
= imx334_mbus_codes
[0];
1374 imx334
->vblank
= imx334
->cur_mode
->vblank
;
1376 ret
= imx334_init_controls(imx334
);
1378 dev_err(imx334
->dev
, "failed to init controls: %d", ret
);
1379 goto error_power_off
;
1382 /* Initialize subdev */
1383 imx334
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1384 imx334
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1386 /* Initialize source pad */
1387 imx334
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1388 ret
= media_entity_pads_init(&imx334
->sd
.entity
, 1, &imx334
->pad
);
1390 dev_err(imx334
->dev
, "failed to init entity pads: %d", ret
);
1391 goto error_handler_free
;
1394 ret
= v4l2_async_register_subdev_sensor(&imx334
->sd
);
1396 dev_err(imx334
->dev
,
1397 "failed to register async subdev: %d", ret
);
1398 goto error_media_entity
;
1401 pm_runtime_set_active(imx334
->dev
);
1402 pm_runtime_enable(imx334
->dev
);
1403 pm_runtime_idle(imx334
->dev
);
1408 media_entity_cleanup(&imx334
->sd
.entity
);
1410 v4l2_ctrl_handler_free(imx334
->sd
.ctrl_handler
);
1412 imx334_power_off(imx334
->dev
);
1413 error_mutex_destroy
:
1414 mutex_destroy(&imx334
->mutex
);
1420 * imx334_remove() - I2C client device unbinding
1421 * @client: pointer to I2C client device
1423 * Return: 0 if successful, error code otherwise.
1425 static void imx334_remove(struct i2c_client
*client
)
1427 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1428 struct imx334
*imx334
= to_imx334(sd
);
1430 v4l2_async_unregister_subdev(sd
);
1431 media_entity_cleanup(&sd
->entity
);
1432 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1434 pm_runtime_disable(&client
->dev
);
1435 pm_runtime_suspended(&client
->dev
);
1437 mutex_destroy(&imx334
->mutex
);
1440 static const struct dev_pm_ops imx334_pm_ops
= {
1441 SET_RUNTIME_PM_OPS(imx334_power_off
, imx334_power_on
, NULL
)
1444 static const struct of_device_id imx334_of_match
[] = {
1445 { .compatible
= "sony,imx334" },
1449 MODULE_DEVICE_TABLE(of
, imx334_of_match
);
1451 static struct i2c_driver imx334_driver
= {
1452 .probe
= imx334_probe
,
1453 .remove
= imx334_remove
,
1456 .pm
= &imx334_pm_ops
,
1457 .of_match_table
= imx334_of_match
,
1461 module_i2c_driver(imx334_driver
);
1463 MODULE_DESCRIPTION("Sony imx334 sensor driver");
1464 MODULE_LICENSE("GPL");