1 From a04966a5f739570c32ac6bcc44c68643bf836780 Mon Sep 17 00:00:00 2001
2 From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
3 Date: Sun, 16 Oct 2022 09:15:13 +0300
4 Subject: [PATCH] media: i2c: imx290: Simplify error handling when
7 Upstream commit e611f3dac54c.
9 Error handling for register writes requires checking the error status of
10 every single write. This makes the code complex, or incorrect when the
11 checks are omitted. Simplify this by passing a pointer to an error code
12 to the imx290_write_reg() function, which allows writing multiple
13 registers in a row and only checking for errors at the end.
15 While at it, rename imx290_write_reg() to imx290_write() as there's
16 nothing else than registers to write, and rename imx290_read_reg()
19 Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
20 Reviewed-by: Alexander Stein <alexander.stein@ew.tq-group.com>
21 Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
23 drivers/media/i2c/imx290.c | 86 ++++++++++++++------------------------
24 1 file changed, 32 insertions(+), 54 deletions(-)
26 --- a/drivers/media/i2c/imx290.c
27 +++ b/drivers/media/i2c/imx290.c
28 @@ -367,7 +367,7 @@ static inline struct imx290 *to_imx290(s
29 return container_of(_sd, struct imx290, sd);
32 -static int __always_unused imx290_read_reg(struct imx290 *imx290, u32 addr, u32 *value)
33 +static int __always_unused imx290_read(struct imx290 *imx290, u32 addr, u32 *value)
35 u8 data[3] = { 0, 0, 0 };
37 @@ -385,17 +385,23 @@ static int __always_unused imx290_read_r
41 -static int imx290_write_reg(struct imx290 *imx290, u32 addr, u32 value)
42 +static int imx290_write(struct imx290 *imx290, u32 addr, u32 value, int *err)
44 u8 data[3] = { value & 0xff, (value >> 8) & 0xff, value >> 16 };
50 ret = regmap_raw_write(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
51 data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
54 dev_err(imx290->dev, "%u-bit write to 0x%04x failed: %d\n",
55 ((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
56 addr & IMX290_REG_ADDR_MASK, ret);
63 @@ -408,7 +414,7 @@ static int imx290_set_register_array(str
66 for (i = 0; i < num_settings; ++i, ++settings) {
67 - ret = imx290_write_reg(imx290, settings->reg, settings->val);
68 + ret = imx290_write(imx290, settings->reg, settings->val, NULL);
72 @@ -419,29 +425,16 @@ static int imx290_set_register_array(str
76 -static int imx290_set_gain(struct imx290 *imx290, u32 value)
80 - ret = imx290_write_reg(imx290, IMX290_GAIN, value);
82 - dev_err(imx290->dev, "Unable to write gain\n");
88 static int imx290_stop_streaming(struct imx290 *imx290)
93 - ret = imx290_write_reg(imx290, IMX290_STANDBY, 0x01);
96 + imx290_write(imx290, IMX290_STANDBY, 0x01, &ret);
100 - return imx290_write_reg(imx290, IMX290_XMSTA, 0x01);
101 + return imx290_write(imx290, IMX290_XMSTA, 0x01, &ret);
104 static int imx290_set_ctrl(struct v4l2_ctrl *ctrl)
105 @@ -456,25 +449,25 @@ static int imx290_set_ctrl(struct v4l2_c
109 - ret = imx290_set_gain(imx290, ctrl->val);
110 + ret = imx290_write(imx290, IMX290_GAIN, ctrl->val, NULL);
112 case V4L2_CID_TEST_PATTERN:
114 - imx290_write_reg(imx290, IMX290_BLKLEVEL, 0);
115 + imx290_write(imx290, IMX290_BLKLEVEL, 0, &ret);
116 usleep_range(10000, 11000);
117 - imx290_write_reg(imx290, IMX290_PGCTRL,
118 - (u8)(IMX290_PGCTRL_REGEN |
119 - IMX290_PGCTRL_THRU |
120 - IMX290_PGCTRL_MODE(ctrl->val)));
121 + imx290_write(imx290, IMX290_PGCTRL,
122 + (u8)(IMX290_PGCTRL_REGEN |
123 + IMX290_PGCTRL_THRU |
124 + IMX290_PGCTRL_MODE(ctrl->val)), &ret);
126 - imx290_write_reg(imx290, IMX290_PGCTRL, 0x00);
127 + imx290_write(imx290, IMX290_PGCTRL, 0x00, &ret);
128 usleep_range(10000, 11000);
129 if (imx290->bpp == 10)
130 - imx290_write_reg(imx290, IMX290_BLKLEVEL,
132 + imx290_write(imx290, IMX290_BLKLEVEL, 0x3c,
134 else /* 12 bits per pixel */
135 - imx290_write_reg(imx290, IMX290_BLKLEVEL,
137 + imx290_write(imx290, IMX290_BLKLEVEL, 0xf0,
142 @@ -695,7 +688,8 @@ static int imx290_start_streaming(struct
146 - ret = imx290_write_reg(imx290, IMX290_HMAX, imx290->current_mode->hmax);
147 + ret = imx290_write(imx290, IMX290_HMAX, imx290->current_mode->hmax,
152 @@ -706,14 +700,12 @@ static int imx290_start_streaming(struct
156 - ret = imx290_write_reg(imx290, IMX290_STANDBY, 0x00);
159 + imx290_write(imx290, IMX290_STANDBY, 0x00, &ret);
163 /* Start streaming */
164 - return imx290_write_reg(imx290, IMX290_XMSTA, 0x00);
165 + return imx290_write(imx290, IMX290_XMSTA, 0x00, &ret);
168 static int imx290_set_stream(struct v4l2_subdev *sd, int enable)
169 @@ -772,27 +764,13 @@ static int imx290_set_data_lanes(struct
170 * validated in probe itself
172 dev_err(imx290->dev, "Lane configuration not supported\n");
177 - ret = imx290_write_reg(imx290, IMX290_PHY_LANE_NUM, laneval);
179 - dev_err(imx290->dev, "Error setting Physical Lane number register\n");
183 - ret = imx290_write_reg(imx290, IMX290_CSI_LANE_MODE, laneval);
185 - dev_err(imx290->dev, "Error setting CSI Lane mode register\n");
190 - ret = imx290_write_reg(imx290, IMX290_FR_FDG_SEL, frsel);
192 - dev_err(imx290->dev, "Error setting FR/FDG SEL register\n");
193 + imx290_write(imx290, IMX290_PHY_LANE_NUM, laneval, &ret);
194 + imx290_write(imx290, IMX290_CSI_LANE_MODE, laneval, &ret);
195 + imx290_write(imx290, IMX290_FR_FDG_SEL, frsel, &ret);