1 From bb459b1433e79fa048f6a904e90393f9f26ea52a Mon Sep 17 00:00:00 2001
2 From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
3 Date: Mon, 16 Jan 2023 15:44:39 +0100
4 Subject: [PATCH] media: i2c: imx290: Factor out subdev init and
7 Upstream commit dfb704da8300.
9 The probe() function is large. Make it more readable by factoring the
10 subdev initialization code out. While at it, rename the error labels as
11 the "free_" prefix isn't accurate.
13 No functional change intended.
15 Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
16 Reviewed-by: Alexander Stein <alexander.stein@ew.tq-group.com>
17 Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
18 Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
20 drivers/media/i2c/imx290.c | 108 +++++++++++++++++++++----------------
21 1 file changed, 62 insertions(+), 46 deletions(-)
23 --- a/drivers/media/i2c/imx290.c
24 +++ b/drivers/media/i2c/imx290.c
25 @@ -1015,6 +1015,47 @@ static const struct media_entity_operati
26 .link_validate = v4l2_subdev_link_validate,
29 +static int imx290_subdev_init(struct imx290 *imx290)
31 + struct i2c_client *client = to_i2c_client(imx290->dev);
35 + * Initialize the frame format. In particular, imx290->current_mode
36 + * and imx290->bpp are set to defaults: imx290_calc_pixel_rate() call
37 + * below relies on these fields.
39 + imx290_entity_init_cfg(&imx290->sd, NULL);
41 + ret = imx290_ctrl_init(imx290);
43 + dev_err(imx290->dev, "Control initialization error %d\n", ret);
47 + v4l2_i2c_subdev_init(&imx290->sd, client, &imx290_subdev_ops);
48 + imx290->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
49 + imx290->sd.dev = imx290->dev;
50 + imx290->sd.entity.ops = &imx290_subdev_entity_ops;
51 + imx290->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
53 + imx290->pad.flags = MEDIA_PAD_FL_SOURCE;
54 + ret = media_entity_pads_init(&imx290->sd.entity, 1, &imx290->pad);
56 + dev_err(imx290->dev, "Could not register media entity\n");
57 + v4l2_ctrl_handler_free(&imx290->ctrls);
64 +static void imx290_subdev_cleanup(struct imx290 *imx290)
66 + media_entity_cleanup(&imx290->sd.entity);
67 + v4l2_ctrl_handler_free(&imx290->ctrls);
70 /* ----------------------------------------------------------------------------
73 @@ -1147,10 +1188,10 @@ static int imx290_probe(struct i2c_clien
74 fwnode_handle_put(endpoint);
76 dev_err(dev, "Unsupported bus type, should be CSI2\n");
80 dev_err(dev, "Parsing endpoint node failed\n");
85 /* Get number of data lanes */
86 @@ -1158,7 +1199,7 @@ static int imx290_probe(struct i2c_clien
87 if (imx290->nlanes != 2 && imx290->nlanes != 4) {
88 dev_err(dev, "Invalid data lanes: %d\n", imx290->nlanes);
94 dev_dbg(dev, "Using %u data lanes\n", imx290->nlanes);
95 @@ -1166,7 +1207,7 @@ static int imx290_probe(struct i2c_clien
96 if (!ep.nr_of_link_frequencies) {
97 dev_err(dev, "link-frequency property not found in DT\n");
103 /* Check that link frequences for all the modes are in device tree */
104 @@ -1174,7 +1215,7 @@ static int imx290_probe(struct i2c_clien
106 dev_err(dev, "Link frequency of %lld is not supported\n", fq);
112 /* get system clock (xclk) */
113 @@ -1182,14 +1223,14 @@ static int imx290_probe(struct i2c_clien
114 if (IS_ERR(imx290->xclk)) {
115 dev_err(dev, "Could not get xclk");
116 ret = PTR_ERR(imx290->xclk);
121 ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
124 dev_err(dev, "Could not get xclk frequency\n");
129 /* external clock must be 37.125 MHz */
130 @@ -1197,19 +1238,19 @@ static int imx290_probe(struct i2c_clien
131 dev_err(dev, "External clock frequency %u is not supported\n",
138 ret = clk_set_rate(imx290->xclk, xclk_freq);
140 dev_err(dev, "Could not set xclk frequency\n");
145 ret = imx290_get_regulators(dev, imx290);
147 dev_err(dev, "Cannot get regulators\n");
152 imx290->rst_gpio = devm_gpiod_get_optional(dev, "reset",
153 @@ -1217,48 +1258,26 @@ static int imx290_probe(struct i2c_clien
154 if (IS_ERR(imx290->rst_gpio)) {
155 dev_err(dev, "Cannot get reset gpio\n");
156 ret = PTR_ERR(imx290->rst_gpio);
161 mutex_init(&imx290->lock);
164 - * Initialize the frame format. In particular, imx290->current_mode
165 - * and imx290->bpp are set to defaults: imx290_calc_pixel_rate() call
166 - * below relies on these fields.
168 - imx290_entity_init_cfg(&imx290->sd, NULL);
170 - ret = imx290_ctrl_init(imx290);
172 - dev_err(dev, "Control initialization error %d\n", ret);
176 - v4l2_i2c_subdev_init(&imx290->sd, client, &imx290_subdev_ops);
177 - imx290->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
178 - imx290->sd.dev = &client->dev;
179 - imx290->sd.entity.ops = &imx290_subdev_entity_ops;
180 - imx290->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
182 - imx290->pad.flags = MEDIA_PAD_FL_SOURCE;
183 - ret = media_entity_pads_init(&imx290->sd.entity, 1, &imx290->pad);
185 - dev_err(dev, "Could not register media entity\n");
188 + ret = imx290_subdev_init(imx290);
192 ret = v4l2_async_register_subdev(&imx290->sd);
194 dev_err(dev, "Could not register v4l2 device\n");
199 /* Power on the device to match runtime PM state below */
200 ret = imx290_power_on(dev);
202 dev_err(dev, "Could not power on the device\n");
207 pm_runtime_set_active(dev);
208 @@ -1269,13 +1288,11 @@ static int imx290_probe(struct i2c_clien
213 - media_entity_cleanup(&imx290->sd.entity);
215 - v4l2_ctrl_handler_free(&imx290->ctrls);
218 + imx290_subdev_cleanup(imx290);
220 mutex_destroy(&imx290->lock);
223 v4l2_fwnode_endpoint_free(&ep);
226 @@ -1287,8 +1304,7 @@ static void imx290_remove(struct i2c_cli
227 struct imx290 *imx290 = to_imx290(sd);
229 v4l2_async_unregister_subdev(sd);
230 - media_entity_cleanup(&sd->entity);
231 - v4l2_ctrl_handler_free(sd->ctrl_handler);
232 + imx290_subdev_cleanup(imx290);
234 mutex_destroy(&imx290->lock);