1 From e97a08baa18802bb3704e1dfda87193417d5fa58 Mon Sep 17 00:00:00 2001
2 From: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
3 Date: Thu, 22 Dec 2022 13:59:33 +0000
4 Subject: [PATCH] media/i2c: Add a driver for the Sony IMX708 image
7 The imx708 is a 12MP MIPI sensor with a 16:9 aspect ratio, here using
8 two CSI-2 lanes. It is a "quad Bayer" sensor with all 3 modes offering
11 12MP: 4608x2592 up to 14.35fps (full FoV)
12 1080p: 2304x1296 up to 56.02fps (full FoV)
13 720p: 1536x864 up to 120.12fps (cropped)
15 This imx708 sensor driver is based heavily on the imx477 driver and
16 has been tested on the Raspberry Pi platform using libcamera.
18 Signed-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
19 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
21 drivers/media/i2c/Kconfig | 13 +
22 drivers/media/i2c/Makefile | 1 +
23 drivers/media/i2c/imx708.c | 1984 ++++++++++++++++++++++++++++++++++++
24 3 files changed, 1998 insertions(+)
25 create mode 100644 drivers/media/i2c/imx708.c
27 --- a/drivers/media/i2c/Kconfig
28 +++ b/drivers/media/i2c/Kconfig
29 @@ -261,6 +261,19 @@ config VIDEO_IMX519
30 To compile this driver as a module, choose M here: the
31 module will be called IMX519.
34 + tristate "Sony IMX708 sensor support"
35 + depends on I2C && VIDEO_DEV
36 + select MEDIA_CONTROLLER
37 + select VIDEO_V4L2_SUBDEV_API
40 + This is a Video4Linux2 sensor driver for the Sony
43 + To compile this driver as a module, choose M here: the
44 + module will be called imx708.
46 config VIDEO_MAX9271_LIB
49 --- a/drivers/media/i2c/Makefile
50 +++ b/drivers/media/i2c/Makefile
51 @@ -53,6 +53,7 @@ obj-$(CONFIG_VIDEO_IMX355) += imx355.o
52 obj-$(CONFIG_VIDEO_IMX412) += imx412.o
53 obj-$(CONFIG_VIDEO_IMX477) += imx477.o
54 obj-$(CONFIG_VIDEO_IMX519) += imx519.o
55 +obj-$(CONFIG_VIDEO_IMX708) += imx708.o
56 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
57 obj-$(CONFIG_VIDEO_IRS1125) += irs1125.o
58 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
60 +++ b/drivers/media/i2c/imx708.c
62 +// SPDX-License-Identifier: GPL-2.0
64 + * A V4L2 driver for Sony IMX708 cameras.
65 + * Copyright (C) 2022, Raspberry Pi Ltd
67 + * Based on Sony imx477 camera driver
68 + * Copyright (C) 2020 Raspberry Pi Ltd
70 +#include <asm/unaligned.h>
71 +#include <linux/clk.h>
72 +#include <linux/delay.h>
73 +#include <linux/gpio/consumer.h>
74 +#include <linux/i2c.h>
75 +#include <linux/module.h>
76 +#include <linux/pm_runtime.h>
77 +#include <linux/regulator/consumer.h>
78 +#include <media/v4l2-ctrls.h>
79 +#include <media/v4l2-device.h>
80 +#include <media/v4l2-event.h>
81 +#include <media/v4l2-fwnode.h>
82 +#include <media/v4l2-mediabus.h>
84 +#define IMX708_REG_VALUE_08BIT 1
85 +#define IMX708_REG_VALUE_16BIT 2
88 +#define IMX708_REG_CHIP_ID 0x0016
89 +#define IMX708_CHIP_ID 0x0708
91 +#define IMX708_REG_MODE_SELECT 0x0100
92 +#define IMX708_MODE_STANDBY 0x00
93 +#define IMX708_MODE_STREAMING 0x01
95 +#define IMX708_REG_ORIENTATION 0x101
97 +#define IMX708_XCLK_FREQ 24000000
99 +#define IMX708_DEFAULT_LINK_FREQ 450000000
101 +/* Default initial pixel rate, will get updated for each mode. */
102 +#define IMX708_INITIAL_PIXEL_RATE 590000000
104 +/* V_TIMING internal */
105 +#define IMX708_REG_FRAME_LENGTH 0x0340
106 +#define IMX708_FRAME_LENGTH_MAX 0xffff
108 +/* Exposure control */
109 +#define IMX708_REG_EXPOSURE 0x0202
110 +#define IMX708_EXPOSURE_OFFSET 48
111 +#define IMX708_EXPOSURE_DEFAULT 0x640
112 +#define IMX708_EXPOSURE_STEP 1
113 +#define IMX708_EXPOSURE_MIN 1
114 +#define IMX708_EXPOSURE_MAX (IMX708_FRAME_LENGTH_MAX - \
115 + IMX708_EXPOSURE_OFFSET)
117 +/* Analog gain control */
118 +#define IMX708_REG_ANALOG_GAIN 0x0204
119 +#define IMX708_ANA_GAIN_MIN 112
120 +#define IMX708_ANA_GAIN_MAX 960
121 +#define IMX708_ANA_GAIN_STEP 1
122 +#define IMX708_ANA_GAIN_DEFAULT IMX708_ANA_GAIN_MIN
124 +/* Digital gain control */
125 +#define IMX708_REG_DIGITAL_GAIN 0x020e
126 +#define IMX708_DGTL_GAIN_MIN 0x0100
127 +#define IMX708_DGTL_GAIN_MAX 0xffff
128 +#define IMX708_DGTL_GAIN_DEFAULT 0x0100
129 +#define IMX708_DGTL_GAIN_STEP 1
131 +/* Colour balance controls */
132 +#define IMX708_REG_COLOUR_BALANCE_RED 0x0b90
133 +#define IMX708_REG_COLOUR_BALANCE_BLUE 0x0b92
134 +#define IMX708_COLOUR_BALANCE_MIN 0x01
135 +#define IMX708_COLOUR_BALANCE_MAX 0xffff
136 +#define IMX708_COLOUR_BALANCE_STEP 0x01
137 +#define IMX708_COLOUR_BALANCE_DEFAULT 0x100
139 +/* Test Pattern Control */
140 +#define IMX708_REG_TEST_PATTERN 0x0600
141 +#define IMX708_TEST_PATTERN_DISABLE 0
142 +#define IMX708_TEST_PATTERN_SOLID_COLOR 1
143 +#define IMX708_TEST_PATTERN_COLOR_BARS 2
144 +#define IMX708_TEST_PATTERN_GREY_COLOR 3
145 +#define IMX708_TEST_PATTERN_PN9 4
147 +/* Test pattern colour components */
148 +#define IMX708_REG_TEST_PATTERN_R 0x0602
149 +#define IMX708_REG_TEST_PATTERN_GR 0x0604
150 +#define IMX708_REG_TEST_PATTERN_B 0x0606
151 +#define IMX708_REG_TEST_PATTERN_GB 0x0608
152 +#define IMX708_TEST_PATTERN_COLOUR_MIN 0
153 +#define IMX708_TEST_PATTERN_COLOUR_MAX 0x0fff
154 +#define IMX708_TEST_PATTERN_COLOUR_STEP 1
156 +#define IMX708_REG_BASE_SPC_GAINS_L 0x7b10
157 +#define IMX708_REG_BASE_SPC_GAINS_R 0x7c00
159 +/* HDR exposure ratio (long:med == med:short) */
160 +#define IMX708_HDR_EXPOSURE_RATIO 4
161 +#define IMX708_REG_MID_EXPOSURE 0x3116
162 +#define IMX708_REG_SHT_EXPOSURE 0x0224
163 +#define IMX708_REG_MID_ANALOG_GAIN 0x3118
164 +#define IMX708_REG_SHT_ANALOG_GAIN 0x0216
167 + * Metadata buffer holds a variety of data, all sent with the same VC/DT (0x12).
168 + * It comprises two scanlines (of up to 5760 bytes each, for 4608 pixels)
169 + * of embedded data, one line of PDAF data, and two lines of AE-HIST data
170 + * (AE histograms are valid for HDR mode and empty in non-HDR modes).
172 +#define IMX708_EMBEDDED_LINE_WIDTH (5 * 5760)
173 +#define IMX708_NUM_EMBEDDED_LINES 1
181 +/* IMX708 native and active pixel array size. */
182 +#define IMX708_NATIVE_WIDTH 4640U
183 +#define IMX708_NATIVE_HEIGHT 2658U
184 +#define IMX708_PIXEL_ARRAY_LEFT 16U
185 +#define IMX708_PIXEL_ARRAY_TOP 24U
186 +#define IMX708_PIXEL_ARRAY_WIDTH 4608U
187 +#define IMX708_PIXEL_ARRAY_HEIGHT 2592U
194 +struct imx708_reg_list {
195 + unsigned int num_of_regs;
196 + const struct imx708_reg *regs;
199 +/* Mode : resolution and related config&values */
200 +struct imx708_mode {
202 + unsigned int width;
205 + unsigned int height;
207 + /* H-timing in pixels */
208 + unsigned int line_length_pix;
210 + /* Analog crop rectangle. */
211 + struct v4l2_rect crop;
213 + /* Highest possible framerate. */
214 + unsigned int vblank_min;
216 + /* Default framerate. */
217 + unsigned int vblank_default;
219 + /* Default register values */
220 + struct imx708_reg_list reg_list;
222 + /* Not all modes have the same pixel rate. */
225 + /* Not all modes have the same minimum exposure. */
226 + u32 exposure_lines_min;
228 + /* Not all modes have the same exposure lines step. */
229 + u32 exposure_lines_step;
231 + /* HDR flag, currently not used at runtime */
235 +/* Default PDAF pixel correction gains */
236 +static const u8 pdaf_gains[2][9] = {
237 + { 0x4c, 0x4c, 0x4c, 0x46, 0x3e, 0x38, 0x35, 0x35, 0x35 },
238 + { 0x35, 0x35, 0x35, 0x38, 0x3e, 0x46, 0x4c, 0x4c, 0x4c }
241 +static const struct imx708_reg mode_common_regs[] = {
293 +static const struct imx708_reg mode_4608x2592_regs[] = {
391 +static const struct imx708_reg mode_2x2binned_regs[] = {
487 +static const struct imx708_reg mode_2x2binned_720p_regs[] = {
583 +static const struct imx708_reg mode_hdr_regs[] = {
597 + {0x0222, IMX708_HDR_EXPOSURE_RATIO},
681 +/* Mode configs. Keep separate lists for when HDR is enabled or not. */
682 +static const struct imx708_mode supported_modes_10bit_no_hdr[] = {
684 + /* Full resolution. */
687 + .line_length_pix = 0x3d20,
689 + .left = IMX708_PIXEL_ARRAY_LEFT,
690 + .top = IMX708_PIXEL_ARRAY_TOP,
695 + .vblank_default = 58,
697 + .num_of_regs = ARRAY_SIZE(mode_4608x2592_regs),
698 + .regs = mode_4608x2592_regs,
700 + .pixel_rate = 595200000,
701 + .exposure_lines_min = 8,
702 + .exposure_lines_step = 1,
706 + /* regular 2x2 binned. */
709 + .line_length_pix = 0x1e90,
711 + .left = IMX708_PIXEL_ARRAY_LEFT,
712 + .top = IMX708_PIXEL_ARRAY_TOP,
717 + .vblank_default = 1198,
719 + .num_of_regs = ARRAY_SIZE(mode_2x2binned_regs),
720 + .regs = mode_2x2binned_regs,
722 + .pixel_rate = 585600000,
723 + .exposure_lines_min = 4,
724 + .exposure_lines_step = 2,
728 + /* 2x2 binned and cropped for 720p. */
731 + .line_length_pix = 0x1460,
733 + .left = IMX708_PIXEL_ARRAY_LEFT,
734 + .top = IMX708_PIXEL_ARRAY_TOP,
739 + .vblank_default = 2755,
741 + .num_of_regs = ARRAY_SIZE(mode_2x2binned_720p_regs),
742 + .regs = mode_2x2binned_720p_regs,
744 + .pixel_rate = 566400000,
745 + .exposure_lines_min = 4,
746 + .exposure_lines_step = 2,
751 +static const struct imx708_mode supported_modes_10bit_hdr[] = {
753 + /* There's only one HDR mode, which is 2x2 downscaled */
756 + .line_length_pix = 0x1460,
758 + .left = IMX708_PIXEL_ARRAY_LEFT,
759 + .top = IMX708_PIXEL_ARRAY_TOP,
763 + .vblank_min = 3673,
764 + .vblank_default = 3673,
766 + .num_of_regs = ARRAY_SIZE(mode_hdr_regs),
767 + .regs = mode_hdr_regs,
769 + .pixel_rate = 777600000,
770 + .exposure_lines_min = 8 * IMX708_HDR_EXPOSURE_RATIO * IMX708_HDR_EXPOSURE_RATIO,
771 + .exposure_lines_step = 2 * IMX708_HDR_EXPOSURE_RATIO * IMX708_HDR_EXPOSURE_RATIO,
777 + * The supported formats.
778 + * This table MUST contain 4 entries per format, to cover the various flip
779 + * combinations in the order
785 +static const u32 codes[] = {
786 + /* 10-bit modes. */
787 + MEDIA_BUS_FMT_SRGGB10_1X10,
788 + MEDIA_BUS_FMT_SGRBG10_1X10,
789 + MEDIA_BUS_FMT_SGBRG10_1X10,
790 + MEDIA_BUS_FMT_SBGGR10_1X10,
793 +static const char * const imx708_test_pattern_menu[] = {
801 +static const int imx708_test_pattern_val[] = {
802 + IMX708_TEST_PATTERN_DISABLE,
803 + IMX708_TEST_PATTERN_COLOR_BARS,
804 + IMX708_TEST_PATTERN_SOLID_COLOR,
805 + IMX708_TEST_PATTERN_GREY_COLOR,
806 + IMX708_TEST_PATTERN_PN9,
809 +/* regulator supplies */
810 +static const char * const imx708_supply_name[] = {
811 + /* Supplies can be enabled in any order */
812 + "VANA1", /* Analog1 (2.8V) supply */
813 + "VANA2", /* Analog2 (1.8V) supply */
814 + "VDIG", /* Digital Core (1.1V) supply */
815 + "VDDL", /* IF (1.8V) supply */
818 +#define IMX708_NUM_SUPPLIES ARRAY_SIZE(imx708_supply_name)
821 + * Initialisation delay between XCLR low->high and the moment when the sensor
822 + * can start capture (i.e. can leave software standby), given by T7 in the
823 + * datasheet is 8ms. This does include I2C setup time as well.
825 + * Note, that delay between XCLR low->high and reading the CCI ID register (T6
826 + * in the datasheet) is much smaller - 600us.
828 +#define IMX708_XCLR_MIN_DELAY_US 8000
829 +#define IMX708_XCLR_DELAY_RANGE_US 1000
832 + struct v4l2_subdev sd;
833 + struct media_pad pad[NUM_PADS];
835 + struct v4l2_mbus_framefmt fmt;
840 + struct gpio_desc *reset_gpio;
841 + struct regulator_bulk_data supplies[IMX708_NUM_SUPPLIES];
843 + struct v4l2_ctrl_handler ctrl_handler;
844 + /* V4L2 Controls */
845 + struct v4l2_ctrl *pixel_rate;
846 + struct v4l2_ctrl *exposure;
847 + struct v4l2_ctrl *vflip;
848 + struct v4l2_ctrl *hflip;
849 + struct v4l2_ctrl *vblank;
850 + struct v4l2_ctrl *hblank;
851 + struct v4l2_ctrl *red_balance;
852 + struct v4l2_ctrl *blue_balance;
853 + struct v4l2_ctrl *notify_gains;
854 + struct v4l2_ctrl *hdr_mode;
857 + const struct imx708_mode *mode;
860 + * Mutex for serialized access:
861 + * Protect sensor module set pad format and start/stop streaming safely.
863 + struct mutex mutex;
865 + /* Streaming on/off */
868 + /* Rewrite common registers on stream on? */
869 + bool common_regs_written;
872 +static inline struct imx708 *to_imx708(struct v4l2_subdev *_sd)
874 + return container_of(_sd, struct imx708, sd);
877 +static inline void get_mode_table(unsigned int code,
878 + const struct imx708_mode **mode_list,
879 + unsigned int *num_modes,
884 + case MEDIA_BUS_FMT_SRGGB10_1X10:
885 + case MEDIA_BUS_FMT_SGRBG10_1X10:
886 + case MEDIA_BUS_FMT_SGBRG10_1X10:
887 + case MEDIA_BUS_FMT_SBGGR10_1X10:
889 + *mode_list = supported_modes_10bit_hdr;
890 + *num_modes = ARRAY_SIZE(supported_modes_10bit_hdr);
892 + *mode_list = supported_modes_10bit_no_hdr;
893 + *num_modes = ARRAY_SIZE(supported_modes_10bit_no_hdr);
902 +/* Read registers up to 2 at a time */
903 +static int imx708_read_reg(struct imx708 *imx708, u16 reg, u32 len, u32 *val)
905 + struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
906 + struct i2c_msg msgs[2];
907 + u8 addr_buf[2] = { reg >> 8, reg & 0xff };
908 + u8 data_buf[4] = { 0, };
914 + /* Write register address */
915 + msgs[0].addr = client->addr;
917 + msgs[0].len = ARRAY_SIZE(addr_buf);
918 + msgs[0].buf = addr_buf;
920 + /* Read data from register */
921 + msgs[1].addr = client->addr;
922 + msgs[1].flags = I2C_M_RD;
924 + msgs[1].buf = &data_buf[4 - len];
926 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
927 + if (ret != ARRAY_SIZE(msgs))
930 + *val = get_unaligned_be32(data_buf);
935 +/* Write registers up to 2 at a time */
936 +static int imx708_write_reg(struct imx708 *imx708, u16 reg, u32 len, u32 val)
938 + struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
944 + put_unaligned_be16(reg, buf);
945 + put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
946 + if (i2c_master_send(client, buf, len + 2) != len + 2)
952 +/* Write a list of registers */
953 +static int imx708_write_regs(struct imx708 *imx708,
954 + const struct imx708_reg *regs, u32 len)
956 + struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
960 + for (i = 0; i < len; i++) {
961 + ret = imx708_write_reg(imx708, regs[i].address, 1, regs[i].val);
963 + dev_err_ratelimited(&client->dev,
964 + "Failed to write reg 0x%4.4x. error = %d\n",
965 + regs[i].address, ret);
974 +/* Get bayer order based on flip setting. */
975 +static u32 imx708_get_format_code(struct imx708 *imx708)
979 + lockdep_assert_held(&imx708->mutex);
981 + i = (imx708->vflip->val ? 2 : 0) |
982 + (imx708->hflip->val ? 1 : 0);
987 +static void imx708_set_default_format(struct imx708 *imx708)
989 + struct v4l2_mbus_framefmt *fmt = &imx708->fmt;
991 + /* Set default mode to max resolution */
992 + imx708->mode = &supported_modes_10bit_no_hdr[0];
994 + /* fmt->code not set as it will always be computed based on flips */
995 + fmt->colorspace = V4L2_COLORSPACE_RAW;
996 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
997 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1000 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1001 + fmt->width = imx708->mode->width;
1002 + fmt->height = imx708->mode->height;
1003 + fmt->field = V4L2_FIELD_NONE;
1006 +static int imx708_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1008 + struct imx708 *imx708 = to_imx708(sd);
1009 + struct v4l2_mbus_framefmt *try_fmt_img =
1010 + v4l2_subdev_get_try_format(sd, fh->state, IMAGE_PAD);
1011 + struct v4l2_mbus_framefmt *try_fmt_meta =
1012 + v4l2_subdev_get_try_format(sd, fh->state, METADATA_PAD);
1013 + struct v4l2_rect *try_crop;
1015 + mutex_lock(&imx708->mutex);
1017 + /* Initialize try_fmt for the image pad */
1018 + if (imx708->hdr_mode->val) {
1019 + try_fmt_img->width = supported_modes_10bit_hdr[0].width;
1020 + try_fmt_img->height = supported_modes_10bit_hdr[0].height;
1022 + try_fmt_img->width = supported_modes_10bit_no_hdr[0].width;
1023 + try_fmt_img->height = supported_modes_10bit_no_hdr[0].height;
1025 + try_fmt_img->code = imx708_get_format_code(imx708);
1026 + try_fmt_img->field = V4L2_FIELD_NONE;
1028 + /* Initialize try_fmt for the embedded metadata pad */
1029 + try_fmt_meta->width = IMX708_EMBEDDED_LINE_WIDTH;
1030 + try_fmt_meta->height = IMX708_NUM_EMBEDDED_LINES;
1031 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
1032 + try_fmt_meta->field = V4L2_FIELD_NONE;
1034 + /* Initialize try_crop */
1035 + try_crop = v4l2_subdev_get_try_crop(sd, fh->state, IMAGE_PAD);
1036 + try_crop->left = IMX708_PIXEL_ARRAY_LEFT;
1037 + try_crop->top = IMX708_PIXEL_ARRAY_TOP;
1038 + try_crop->width = IMX708_PIXEL_ARRAY_WIDTH;
1039 + try_crop->height = IMX708_PIXEL_ARRAY_HEIGHT;
1041 + mutex_unlock(&imx708->mutex);
1046 +static int imx708_set_exposure(struct imx708 *imx708, unsigned int val)
1050 + val = max(val, imx708->mode->exposure_lines_min);
1051 + val -= val % imx708->mode->exposure_lines_step;
1054 + * In HDR mode this will set the longest exposure. The sensor
1055 + * will automatically divide the medium and short ones by 4,16.
1057 + ret = imx708_write_reg(imx708, IMX708_REG_EXPOSURE,
1058 + IMX708_REG_VALUE_16BIT, val);
1063 +static void imx708_adjust_exposure_range(struct imx708 *imx708,
1064 + struct v4l2_ctrl *ctrl)
1066 + int exposure_max, exposure_def;
1068 + /* Honour the VBLANK limits when setting exposure. */
1069 + exposure_max = imx708->mode->height + imx708->vblank->val -
1070 + IMX708_EXPOSURE_OFFSET;
1071 + exposure_def = min(exposure_max, imx708->exposure->val);
1072 + __v4l2_ctrl_modify_range(imx708->exposure, imx708->exposure->minimum,
1073 + exposure_max, imx708->exposure->step,
1077 +static int imx708_set_analogue_gain(struct imx708 *imx708, unsigned int val)
1082 + * In HDR mode this will set the gain for the longest exposure,
1083 + * and by default the sensor uses the same gain for all of them.
1085 + ret = imx708_write_reg(imx708, IMX708_REG_ANALOG_GAIN,
1086 + IMX708_REG_VALUE_16BIT, val);
1091 +static void imx708_set_framing_limits(struct imx708 *imx708)
1093 + unsigned int hblank;
1094 + const struct imx708_mode *mode = imx708->mode;
1096 + __v4l2_ctrl_modify_range(imx708->pixel_rate,
1097 + mode->pixel_rate, mode->pixel_rate,
1098 + 1, mode->pixel_rate);
1100 + /* Update limits and set FPS to default */
1101 + __v4l2_ctrl_modify_range(imx708->vblank, mode->vblank_min,
1102 + IMX708_FRAME_LENGTH_MAX - mode->height,
1103 + 1, mode->vblank_default);
1106 + * Currently PPL is fixed to the mode specified value, so hblank
1107 + * depends on mode->width only, and is not changeable in any
1108 + * way other than changing the mode.
1110 + hblank = mode->line_length_pix - mode->width;
1111 + __v4l2_ctrl_modify_range(imx708->hblank, hblank, hblank, 1, hblank);
1114 +static int imx708_set_ctrl(struct v4l2_ctrl *ctrl)
1116 + struct imx708 *imx708 =
1117 + container_of(ctrl->handler, struct imx708, ctrl_handler);
1118 + struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1119 + const struct imx708_mode *mode_list;
1120 + unsigned int code, num_modes;
1124 + * The VBLANK control may change the limits of usable exposure, so check
1125 + * and adjust if necessary.
1127 + if (ctrl->id == V4L2_CID_VBLANK)
1128 + imx708_adjust_exposure_range(imx708, ctrl);
1131 + * Applying V4L2 control value only happens
1132 + * when power is up for streaming
1134 + if (pm_runtime_get_if_in_use(&client->dev) == 0)
1137 + switch (ctrl->id) {
1138 + case V4L2_CID_ANALOGUE_GAIN:
1139 + imx708_set_analogue_gain(imx708, ctrl->val);
1141 + case V4L2_CID_EXPOSURE:
1142 + ret = imx708_set_exposure(imx708, ctrl->val);
1144 + case V4L2_CID_DIGITAL_GAIN:
1145 + ret = imx708_write_reg(imx708, IMX708_REG_DIGITAL_GAIN,
1146 + IMX708_REG_VALUE_16BIT, ctrl->val);
1148 + case V4L2_CID_TEST_PATTERN:
1149 + ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN,
1150 + IMX708_REG_VALUE_16BIT,
1151 + imx708_test_pattern_val[ctrl->val]);
1153 + case V4L2_CID_TEST_PATTERN_RED:
1154 + ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_R,
1155 + IMX708_REG_VALUE_16BIT, ctrl->val);
1157 + case V4L2_CID_TEST_PATTERN_GREENR:
1158 + ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_GR,
1159 + IMX708_REG_VALUE_16BIT, ctrl->val);
1161 + case V4L2_CID_TEST_PATTERN_BLUE:
1162 + ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_B,
1163 + IMX708_REG_VALUE_16BIT, ctrl->val);
1165 + case V4L2_CID_TEST_PATTERN_GREENB:
1166 + ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_GB,
1167 + IMX708_REG_VALUE_16BIT, ctrl->val);
1169 + case V4L2_CID_HFLIP:
1170 + case V4L2_CID_VFLIP:
1171 + ret = imx708_write_reg(imx708, IMX708_REG_ORIENTATION, 1,
1172 + imx708->hflip->val |
1173 + imx708->vflip->val << 1);
1175 + case V4L2_CID_VBLANK:
1176 + ret = imx708_write_reg(imx708, IMX708_REG_FRAME_LENGTH,
1177 + IMX708_REG_VALUE_16BIT,
1178 + imx708->mode->height + ctrl->val);
1180 + case V4L2_CID_NOTIFY_GAINS:
1181 + ret = imx708_write_reg(imx708, IMX708_REG_COLOUR_BALANCE_BLUE,
1182 + IMX708_REG_VALUE_16BIT,
1183 + imx708->notify_gains->p_new.p_u32[0]);
1186 + ret = imx708_write_reg(imx708, IMX708_REG_COLOUR_BALANCE_RED,
1187 + IMX708_REG_VALUE_16BIT,
1188 + imx708->notify_gains->p_new.p_u32[3]);
1190 + case V4L2_CID_WIDE_DYNAMIC_RANGE:
1191 + code = imx708_get_format_code(imx708);
1192 + get_mode_table(code, &mode_list, &num_modes, ctrl->val);
1193 + imx708->mode = v4l2_find_nearest_size(mode_list,
1196 + imx708->mode->width,
1197 + imx708->mode->height);
1198 + imx708_set_framing_limits(imx708);
1201 + dev_info(&client->dev,
1202 + "ctrl(id:0x%x,val:0x%x) is not handled\n",
1203 + ctrl->id, ctrl->val);
1208 + pm_runtime_put(&client->dev);
1213 +static const struct v4l2_ctrl_ops imx708_ctrl_ops = {
1214 + .s_ctrl = imx708_set_ctrl,
1217 +static int imx708_enum_mbus_code(struct v4l2_subdev *sd,
1218 + struct v4l2_subdev_state *sd_state,
1219 + struct v4l2_subdev_mbus_code_enum *code)
1221 + struct imx708 *imx708 = to_imx708(sd);
1223 + if (code->pad >= NUM_PADS)
1226 + if (code->pad == IMAGE_PAD) {
1227 + if (code->index >= (ARRAY_SIZE(codes) / 4))
1230 + code->code = imx708_get_format_code(imx708);
1232 + if (code->index > 0)
1235 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1241 +static int imx708_enum_frame_size(struct v4l2_subdev *sd,
1242 + struct v4l2_subdev_state *sd_state,
1243 + struct v4l2_subdev_frame_size_enum *fse)
1245 + struct imx708 *imx708 = to_imx708(sd);
1247 + if (fse->pad >= NUM_PADS)
1250 + if (fse->pad == IMAGE_PAD) {
1251 + const struct imx708_mode *mode_list;
1252 + unsigned int num_modes;
1254 + get_mode_table(fse->code, &mode_list, &num_modes,
1255 + imx708->hdr_mode->val);
1257 + if (fse->index >= num_modes)
1260 + if (fse->code != imx708_get_format_code(imx708))
1263 + fse->min_width = mode_list[fse->index].width;
1264 + fse->max_width = fse->min_width;
1265 + fse->min_height = mode_list[fse->index].height;
1266 + fse->max_height = fse->min_height;
1268 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1271 + fse->min_width = IMX708_EMBEDDED_LINE_WIDTH;
1272 + fse->max_width = fse->min_width;
1273 + fse->min_height = IMX708_NUM_EMBEDDED_LINES;
1274 + fse->max_height = fse->min_height;
1280 +static void imx708_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1282 + fmt->colorspace = V4L2_COLORSPACE_RAW;
1283 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1284 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1287 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1290 +static void imx708_update_image_pad_format(struct imx708 *imx708,
1291 + const struct imx708_mode *mode,
1292 + struct v4l2_subdev_format *fmt)
1294 + fmt->format.width = mode->width;
1295 + fmt->format.height = mode->height;
1296 + fmt->format.field = V4L2_FIELD_NONE;
1297 + imx708_reset_colorspace(&fmt->format);
1300 +static void imx708_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1302 + fmt->format.width = IMX708_EMBEDDED_LINE_WIDTH;
1303 + fmt->format.height = IMX708_NUM_EMBEDDED_LINES;
1304 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
1305 + fmt->format.field = V4L2_FIELD_NONE;
1308 +static int imx708_get_pad_format(struct v4l2_subdev *sd,
1309 + struct v4l2_subdev_state *sd_state,
1310 + struct v4l2_subdev_format *fmt)
1312 + struct imx708 *imx708 = to_imx708(sd);
1314 + if (fmt->pad >= NUM_PADS)
1317 + mutex_lock(&imx708->mutex);
1319 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1320 + struct v4l2_mbus_framefmt *try_fmt =
1321 + v4l2_subdev_get_try_format(&imx708->sd, sd_state,
1323 + /* update the code which could change due to vflip or hflip */
1324 + try_fmt->code = fmt->pad == IMAGE_PAD ?
1325 + imx708_get_format_code(imx708) :
1326 + MEDIA_BUS_FMT_SENSOR_DATA;
1327 + fmt->format = *try_fmt;
1329 + if (fmt->pad == IMAGE_PAD) {
1330 + imx708_update_image_pad_format(imx708, imx708->mode,
1332 + fmt->format.code = imx708_get_format_code(imx708);
1334 + imx708_update_metadata_pad_format(fmt);
1338 + mutex_unlock(&imx708->mutex);
1342 +static int imx708_set_pad_format(struct v4l2_subdev *sd,
1343 + struct v4l2_subdev_state *sd_state,
1344 + struct v4l2_subdev_format *fmt)
1346 + struct v4l2_mbus_framefmt *framefmt;
1347 + const struct imx708_mode *mode;
1348 + struct imx708 *imx708 = to_imx708(sd);
1350 + if (fmt->pad >= NUM_PADS)
1353 + mutex_lock(&imx708->mutex);
1355 + if (fmt->pad == IMAGE_PAD) {
1356 + const struct imx708_mode *mode_list;
1357 + unsigned int num_modes;
1359 + /* Bayer order varies with flips */
1360 + fmt->format.code = imx708_get_format_code(imx708);
1362 + get_mode_table(fmt->format.code, &mode_list, &num_modes,
1363 + imx708->hdr_mode->val);
1365 + mode = v4l2_find_nearest_size(mode_list,
1368 + fmt->format.width,
1369 + fmt->format.height);
1370 + imx708_update_image_pad_format(imx708, mode, fmt);
1371 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1372 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1374 + *framefmt = fmt->format;
1376 + imx708->mode = mode;
1377 + imx708_set_framing_limits(imx708);
1380 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1381 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1383 + *framefmt = fmt->format;
1385 + /* Only one embedded data mode is supported */
1386 + imx708_update_metadata_pad_format(fmt);
1390 + mutex_unlock(&imx708->mutex);
1395 +static const struct v4l2_rect *
1396 +__imx708_get_pad_crop(struct imx708 *imx708, struct v4l2_subdev_state *sd_state,
1397 + unsigned int pad, enum v4l2_subdev_format_whence which)
1400 + case V4L2_SUBDEV_FORMAT_TRY:
1401 + return v4l2_subdev_get_try_crop(&imx708->sd, sd_state, pad);
1402 + case V4L2_SUBDEV_FORMAT_ACTIVE:
1403 + return &imx708->mode->crop;
1409 +static int imx708_get_selection(struct v4l2_subdev *sd,
1410 + struct v4l2_subdev_state *sd_state,
1411 + struct v4l2_subdev_selection *sel)
1413 + switch (sel->target) {
1414 + case V4L2_SEL_TGT_CROP: {
1415 + struct imx708 *imx708 = to_imx708(sd);
1417 + mutex_lock(&imx708->mutex);
1418 + sel->r = *__imx708_get_pad_crop(imx708, sd_state, sel->pad,
1420 + mutex_unlock(&imx708->mutex);
1425 + case V4L2_SEL_TGT_NATIVE_SIZE:
1428 + sel->r.width = IMX708_NATIVE_WIDTH;
1429 + sel->r.height = IMX708_NATIVE_HEIGHT;
1433 + case V4L2_SEL_TGT_CROP_DEFAULT:
1434 + case V4L2_SEL_TGT_CROP_BOUNDS:
1435 + sel->r.left = IMX708_PIXEL_ARRAY_LEFT;
1436 + sel->r.top = IMX708_PIXEL_ARRAY_TOP;
1437 + sel->r.width = IMX708_PIXEL_ARRAY_WIDTH;
1438 + sel->r.height = IMX708_PIXEL_ARRAY_HEIGHT;
1446 +/* Start streaming */
1447 +static int imx708_start_streaming(struct imx708 *imx708)
1449 + struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1450 + const struct imx708_reg_list *reg_list;
1454 + if (!imx708->common_regs_written) {
1455 + ret = imx708_write_regs(imx708, mode_common_regs,
1456 + ARRAY_SIZE(mode_common_regs));
1458 + dev_err(&client->dev, "%s failed to set common settings\n",
1463 + ret = imx708_read_reg(imx708, IMX708_REG_BASE_SPC_GAINS_L,
1464 + IMX708_REG_VALUE_08BIT, &val);
1465 + if (ret == 0 && val == 0x40) {
1466 + for (i = 0; i < 54 && ret == 0; i++) {
1467 + ret = imx708_write_reg(imx708,
1468 + IMX708_REG_BASE_SPC_GAINS_L + i,
1469 + IMX708_REG_VALUE_08BIT,
1470 + pdaf_gains[0][i % 9]);
1472 + for (i = 0; i < 54 && ret == 0; i++) {
1473 + ret = imx708_write_reg(imx708,
1474 + IMX708_REG_BASE_SPC_GAINS_R + i,
1475 + IMX708_REG_VALUE_08BIT,
1476 + pdaf_gains[1][i % 9]);
1480 + dev_err(&client->dev, "%s failed to set PDAF gains\n",
1485 + imx708->common_regs_written = true;
1488 + /* Apply default values of current mode */
1489 + reg_list = &imx708->mode->reg_list;
1490 + ret = imx708_write_regs(imx708, reg_list->regs, reg_list->num_of_regs);
1492 + dev_err(&client->dev, "%s failed to set mode\n", __func__);
1496 + /* Apply customized values from user */
1497 + ret = __v4l2_ctrl_handler_setup(imx708->sd.ctrl_handler);
1501 + /* set stream on register */
1502 + return imx708_write_reg(imx708, IMX708_REG_MODE_SELECT,
1503 + IMX708_REG_VALUE_08BIT, IMX708_MODE_STREAMING);
1506 +/* Stop streaming */
1507 +static void imx708_stop_streaming(struct imx708 *imx708)
1509 + struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1512 + /* set stream off register */
1513 + ret = imx708_write_reg(imx708, IMX708_REG_MODE_SELECT,
1514 + IMX708_REG_VALUE_08BIT, IMX708_MODE_STANDBY);
1516 + dev_err(&client->dev, "%s failed to set stream\n", __func__);
1519 +static int imx708_set_stream(struct v4l2_subdev *sd, int enable)
1521 + struct imx708 *imx708 = to_imx708(sd);
1522 + struct i2c_client *client = v4l2_get_subdevdata(sd);
1525 + mutex_lock(&imx708->mutex);
1526 + if (imx708->streaming == enable) {
1527 + mutex_unlock(&imx708->mutex);
1532 + ret = pm_runtime_get_sync(&client->dev);
1534 + pm_runtime_put_noidle(&client->dev);
1539 + * Apply default & customized values
1540 + * and then start streaming.
1542 + ret = imx708_start_streaming(imx708);
1546 + imx708_stop_streaming(imx708);
1547 + pm_runtime_put(&client->dev);
1550 + imx708->streaming = enable;
1552 + /* vflip/hflip and hdr mode cannot change during streaming */
1553 + __v4l2_ctrl_grab(imx708->vflip, enable);
1554 + __v4l2_ctrl_grab(imx708->hflip, enable);
1555 + __v4l2_ctrl_grab(imx708->hdr_mode, enable);
1557 + mutex_unlock(&imx708->mutex);
1562 + pm_runtime_put(&client->dev);
1564 + mutex_unlock(&imx708->mutex);
1569 +/* Power/clock management functions */
1570 +static int imx708_power_on(struct device *dev)
1572 + struct i2c_client *client = to_i2c_client(dev);
1573 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1574 + struct imx708 *imx708 = to_imx708(sd);
1577 + ret = regulator_bulk_enable(IMX708_NUM_SUPPLIES,
1578 + imx708->supplies);
1580 + dev_err(&client->dev, "%s: failed to enable regulators\n",
1585 + ret = clk_prepare_enable(imx708->xclk);
1587 + dev_err(&client->dev, "%s: failed to enable clock\n",
1592 + gpiod_set_value_cansleep(imx708->reset_gpio, 1);
1593 + usleep_range(IMX708_XCLR_MIN_DELAY_US,
1594 + IMX708_XCLR_MIN_DELAY_US + IMX708_XCLR_DELAY_RANGE_US);
1599 + regulator_bulk_disable(IMX708_NUM_SUPPLIES, imx708->supplies);
1603 +static int imx708_power_off(struct device *dev)
1605 + struct i2c_client *client = to_i2c_client(dev);
1606 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1607 + struct imx708 *imx708 = to_imx708(sd);
1609 + gpiod_set_value_cansleep(imx708->reset_gpio, 0);
1610 + regulator_bulk_disable(IMX708_NUM_SUPPLIES, imx708->supplies);
1611 + clk_disable_unprepare(imx708->xclk);
1613 + /* Force reprogramming of the common registers when powered up again. */
1614 + imx708->common_regs_written = false;
1619 +static int __maybe_unused imx708_suspend(struct device *dev)
1621 + struct i2c_client *client = to_i2c_client(dev);
1622 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1623 + struct imx708 *imx708 = to_imx708(sd);
1625 + if (imx708->streaming)
1626 + imx708_stop_streaming(imx708);
1631 +static int __maybe_unused imx708_resume(struct device *dev)
1633 + struct i2c_client *client = to_i2c_client(dev);
1634 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1635 + struct imx708 *imx708 = to_imx708(sd);
1638 + if (imx708->streaming) {
1639 + ret = imx708_start_streaming(imx708);
1647 + imx708_stop_streaming(imx708);
1648 + imx708->streaming = 0;
1652 +static int imx708_get_regulators(struct imx708 *imx708)
1654 + struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1657 + for (i = 0; i < IMX708_NUM_SUPPLIES; i++)
1658 + imx708->supplies[i].supply = imx708_supply_name[i];
1660 + return devm_regulator_bulk_get(&client->dev,
1661 + IMX708_NUM_SUPPLIES,
1662 + imx708->supplies);
1665 +/* Verify chip ID */
1666 +static int imx708_identify_module(struct imx708 *imx708)
1668 + struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1672 + ret = imx708_read_reg(imx708, IMX708_REG_CHIP_ID,
1673 + IMX708_REG_VALUE_16BIT, &val);
1675 + dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
1676 + IMX708_CHIP_ID, ret);
1680 + if (val != IMX708_CHIP_ID) {
1681 + dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1682 + IMX708_CHIP_ID, val);
1686 + ret = imx708_read_reg(imx708, 0x0000, IMX708_REG_VALUE_16BIT, &val);
1688 + dev_info(&client->dev, "camera module ID 0x%04x\n", val);
1689 + snprintf(imx708->sd.name, sizeof(imx708->sd.name), "imx708%s%s",
1690 + val & 0x02 ? "_wide" : "",
1691 + val & 0x80 ? "_noir" : "");
1697 +static const struct v4l2_subdev_core_ops imx708_core_ops = {
1698 + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1699 + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1702 +static const struct v4l2_subdev_video_ops imx708_video_ops = {
1703 + .s_stream = imx708_set_stream,
1706 +static const struct v4l2_subdev_pad_ops imx708_pad_ops = {
1707 + .enum_mbus_code = imx708_enum_mbus_code,
1708 + .get_fmt = imx708_get_pad_format,
1709 + .set_fmt = imx708_set_pad_format,
1710 + .get_selection = imx708_get_selection,
1711 + .enum_frame_size = imx708_enum_frame_size,
1714 +static const struct v4l2_subdev_ops imx708_subdev_ops = {
1715 + .core = &imx708_core_ops,
1716 + .video = &imx708_video_ops,
1717 + .pad = &imx708_pad_ops,
1720 +static const struct v4l2_subdev_internal_ops imx708_internal_ops = {
1721 + .open = imx708_open,
1724 +static const struct v4l2_ctrl_config imx708_notify_gains_ctrl = {
1725 + .ops = &imx708_ctrl_ops,
1726 + .id = V4L2_CID_NOTIFY_GAINS,
1727 + .type = V4L2_CTRL_TYPE_U32,
1728 + .min = IMX708_COLOUR_BALANCE_MIN,
1729 + .max = IMX708_COLOUR_BALANCE_MAX,
1730 + .step = IMX708_COLOUR_BALANCE_STEP,
1731 + .def = IMX708_COLOUR_BALANCE_DEFAULT,
1733 + .elem_size = sizeof(u32),
1736 +/* Initialize control handlers */
1737 +static int imx708_init_controls(struct imx708 *imx708)
1739 + struct v4l2_ctrl_handler *ctrl_hdlr;
1740 + struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd);
1741 + struct v4l2_fwnode_device_properties props;
1745 + ctrl_hdlr = &imx708->ctrl_handler;
1746 + ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
1750 + mutex_init(&imx708->mutex);
1751 + ctrl_hdlr->lock = &imx708->mutex;
1753 + /* By default, PIXEL_RATE is read only */
1754 + imx708->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1755 + V4L2_CID_PIXEL_RATE,
1756 + IMX708_INITIAL_PIXEL_RATE,
1757 + IMX708_INITIAL_PIXEL_RATE, 1,
1758 + IMX708_INITIAL_PIXEL_RATE);
1761 + * Create the controls here, but mode specific limits are setup
1762 + * in the imx708_set_framing_limits() call below.
1764 + imx708->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1765 + V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
1766 + imx708->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1767 + V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
1769 + imx708->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1770 + V4L2_CID_EXPOSURE,
1771 + IMX708_EXPOSURE_MIN,
1772 + IMX708_EXPOSURE_MAX,
1773 + IMX708_EXPOSURE_STEP,
1774 + IMX708_EXPOSURE_DEFAULT);
1776 + v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1777 + IMX708_ANA_GAIN_MIN, IMX708_ANA_GAIN_MAX,
1778 + IMX708_ANA_GAIN_STEP, IMX708_ANA_GAIN_DEFAULT);
1780 + v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1781 + IMX708_DGTL_GAIN_MIN, IMX708_DGTL_GAIN_MAX,
1782 + IMX708_DGTL_GAIN_STEP, IMX708_DGTL_GAIN_DEFAULT);
1784 + imx708->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1785 + V4L2_CID_HFLIP, 0, 1, 1, 0);
1787 + imx708->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1788 + V4L2_CID_VFLIP, 0, 1, 1, 0);
1790 + v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx708_ctrl_ops,
1791 + V4L2_CID_TEST_PATTERN,
1792 + ARRAY_SIZE(imx708_test_pattern_menu) - 1,
1793 + 0, 0, imx708_test_pattern_menu);
1794 + for (i = 0; i < 4; i++) {
1796 + * The assumption is that
1797 + * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
1798 + * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
1799 + * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
1801 + v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1802 + V4L2_CID_TEST_PATTERN_RED + i,
1803 + IMX708_TEST_PATTERN_COLOUR_MIN,
1804 + IMX708_TEST_PATTERN_COLOUR_MAX,
1805 + IMX708_TEST_PATTERN_COLOUR_STEP,
1806 + IMX708_TEST_PATTERN_COLOUR_MAX);
1807 + /* The "Solid color" pattern is white by default */
1810 + imx708->notify_gains = v4l2_ctrl_new_custom(ctrl_hdlr,
1811 + &imx708_notify_gains_ctrl, NULL);
1813 + imx708->hdr_mode = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops,
1814 + V4L2_CID_WIDE_DYNAMIC_RANGE,
1817 + ret = v4l2_fwnode_device_parse(&client->dev, &props);
1821 + v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx708_ctrl_ops, &props);
1823 + if (ctrl_hdlr->error) {
1824 + ret = ctrl_hdlr->error;
1825 + dev_err(&client->dev, "%s control init failed (%d)\n",
1830 + imx708->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1831 + imx708->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1832 + imx708->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1833 + imx708->hdr_mode->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1835 + imx708->sd.ctrl_handler = ctrl_hdlr;
1837 + /* Setup exposure and frame/line length limits. */
1838 + imx708_set_framing_limits(imx708);
1843 + v4l2_ctrl_handler_free(ctrl_hdlr);
1844 + mutex_destroy(&imx708->mutex);
1849 +static void imx708_free_controls(struct imx708 *imx708)
1851 + v4l2_ctrl_handler_free(imx708->sd.ctrl_handler);
1852 + mutex_destroy(&imx708->mutex);
1855 +static int imx708_check_hwcfg(struct device *dev)
1857 + struct fwnode_handle *endpoint;
1858 + struct v4l2_fwnode_endpoint ep_cfg = {
1859 + .bus_type = V4L2_MBUS_CSI2_DPHY
1861 + int ret = -EINVAL;
1863 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1865 + dev_err(dev, "endpoint node not found\n");
1869 + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
1870 + dev_err(dev, "could not parse endpoint\n");
1874 + /* Check the number of MIPI CSI2 data lanes */
1875 + if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
1876 + dev_err(dev, "only 2 data lanes are currently supported\n");
1880 + /* Check the link frequency set in device tree */
1881 + if (!ep_cfg.nr_of_link_frequencies) {
1882 + dev_err(dev, "link-frequency property not found in DT\n");
1886 + if (ep_cfg.nr_of_link_frequencies != 1 ||
1887 + ep_cfg.link_frequencies[0] != IMX708_DEFAULT_LINK_FREQ) {
1888 + dev_err(dev, "Link frequency not supported: %lld\n",
1889 + ep_cfg.link_frequencies[0]);
1896 + v4l2_fwnode_endpoint_free(&ep_cfg);
1897 + fwnode_handle_put(endpoint);
1902 +static int imx708_probe(struct i2c_client *client)
1904 + struct device *dev = &client->dev;
1905 + struct imx708 *imx708;
1908 + imx708 = devm_kzalloc(&client->dev, sizeof(*imx708), GFP_KERNEL);
1912 + v4l2_i2c_subdev_init(&imx708->sd, client, &imx708_subdev_ops);
1914 + /* Check the hardware configuration in device tree */
1915 + if (imx708_check_hwcfg(dev))
1918 + /* Get system clock (xclk) */
1919 + imx708->xclk = devm_clk_get(dev, NULL);
1920 + if (IS_ERR(imx708->xclk)) {
1921 + dev_err(dev, "failed to get xclk\n");
1922 + return PTR_ERR(imx708->xclk);
1925 + imx708->xclk_freq = clk_get_rate(imx708->xclk);
1926 + if (imx708->xclk_freq != IMX708_XCLK_FREQ) {
1927 + dev_err(dev, "xclk frequency not supported: %d Hz\n",
1928 + imx708->xclk_freq);
1932 + ret = imx708_get_regulators(imx708);
1934 + dev_err(dev, "failed to get regulators\n");
1938 + /* Request optional enable pin */
1939 + imx708->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1943 + * The sensor must be powered for imx708_identify_module()
1944 + * to be able to read the CHIP_ID register
1946 + ret = imx708_power_on(dev);
1950 + ret = imx708_identify_module(imx708);
1952 + goto error_power_off;
1954 + /* Initialize default format */
1955 + imx708_set_default_format(imx708);
1957 + /* Enable runtime PM and turn off the device */
1958 + pm_runtime_set_active(dev);
1959 + pm_runtime_enable(dev);
1960 + pm_runtime_idle(dev);
1962 + /* This needs the pm runtime to be registered. */
1963 + ret = imx708_init_controls(imx708);
1965 + goto error_power_off;
1967 + /* Initialize subdev */
1968 + imx708->sd.internal_ops = &imx708_internal_ops;
1969 + imx708->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1970 + V4L2_SUBDEV_FL_HAS_EVENTS;
1971 + imx708->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1973 + /* Initialize source pads */
1974 + imx708->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
1975 + imx708->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
1977 + ret = media_entity_pads_init(&imx708->sd.entity, NUM_PADS, imx708->pad);
1979 + dev_err(dev, "failed to init entity pads: %d\n", ret);
1980 + goto error_handler_free;
1983 + ret = v4l2_async_register_subdev_sensor(&imx708->sd);
1985 + dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
1986 + goto error_media_entity;
1991 +error_media_entity:
1992 + media_entity_cleanup(&imx708->sd.entity);
1994 +error_handler_free:
1995 + imx708_free_controls(imx708);
1998 + pm_runtime_disable(&client->dev);
1999 + pm_runtime_set_suspended(&client->dev);
2000 + imx708_power_off(&client->dev);
2005 +static void imx708_remove(struct i2c_client *client)
2007 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2008 + struct imx708 *imx708 = to_imx708(sd);
2010 + v4l2_async_unregister_subdev(sd);
2011 + media_entity_cleanup(&sd->entity);
2012 + imx708_free_controls(imx708);
2014 + pm_runtime_disable(&client->dev);
2015 + if (!pm_runtime_status_suspended(&client->dev))
2016 + imx708_power_off(&client->dev);
2017 + pm_runtime_set_suspended(&client->dev);
2020 +static const struct of_device_id imx708_dt_ids[] = {
2021 + { .compatible = "sony,imx708" },
2022 + { /* sentinel */ }
2024 +MODULE_DEVICE_TABLE(of, imx708_dt_ids);
2026 +static const struct dev_pm_ops imx708_pm_ops = {
2027 + SET_SYSTEM_SLEEP_PM_OPS(imx708_suspend, imx708_resume)
2028 + SET_RUNTIME_PM_OPS(imx708_power_off, imx708_power_on, NULL)
2031 +static struct i2c_driver imx708_i2c_driver = {
2034 + .of_match_table = imx708_dt_ids,
2035 + .pm = &imx708_pm_ops,
2037 + .probe_new = imx708_probe,
2038 + .remove = imx708_remove,
2041 +module_i2c_driver(imx708_i2c_driver);
2043 +MODULE_AUTHOR("David Plowman <david.plowman@raspberrypi.com>");
2044 +MODULE_DESCRIPTION("Sony IMX708 sensor driver");
2045 +MODULE_LICENSE("GPL v2");