fdd85f0cc59f471977df3eebae3d059c15e14c81
[openwrt/staging/ansuel.git] /
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
5 cleanup to functions
6
7 Upstream commit dfb704da8300.
8
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.
12
13 No functional change intended.
14
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>
19 ---
20 drivers/media/i2c/imx290.c | 108 +++++++++++++++++++++----------------
21 1 file changed, 62 insertions(+), 46 deletions(-)
22
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,
27 };
28
29 +static int imx290_subdev_init(struct imx290 *imx290)
30 +{
31 + struct i2c_client *client = to_i2c_client(imx290->dev);
32 + int ret;
33 +
34 + /*
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.
38 + */
39 + imx290_entity_init_cfg(&imx290->sd, NULL);
40 +
41 + ret = imx290_ctrl_init(imx290);
42 + if (ret < 0) {
43 + dev_err(imx290->dev, "Control initialization error %d\n", ret);
44 + return ret;
45 + }
46 +
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;
52 +
53 + imx290->pad.flags = MEDIA_PAD_FL_SOURCE;
54 + ret = media_entity_pads_init(&imx290->sd.entity, 1, &imx290->pad);
55 + if (ret < 0) {
56 + dev_err(imx290->dev, "Could not register media entity\n");
57 + v4l2_ctrl_handler_free(&imx290->ctrls);
58 + return ret;
59 + }
60 +
61 + return 0;
62 +}
63 +
64 +static void imx290_subdev_cleanup(struct imx290 *imx290)
65 +{
66 + media_entity_cleanup(&imx290->sd.entity);
67 + v4l2_ctrl_handler_free(&imx290->ctrls);
68 +}
69 +
70 /* ----------------------------------------------------------------------------
71 * Power management
72 */
73 @@ -1147,10 +1188,10 @@ static int imx290_probe(struct i2c_clien
74 fwnode_handle_put(endpoint);
75 if (ret == -ENXIO) {
76 dev_err(dev, "Unsupported bus type, should be CSI2\n");
77 - goto free_err;
78 + goto err_endpoint;
79 } else if (ret) {
80 dev_err(dev, "Parsing endpoint node failed\n");
81 - goto free_err;
82 + goto err_endpoint;
83 }
84
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);
89 ret = -EINVAL;
90 - goto free_err;
91 + goto err_endpoint;
92 }
93
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");
98 ret = -EINVAL;
99 - goto free_err;
100 + goto err_endpoint;
101 }
102
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
105 if (fq) {
106 dev_err(dev, "Link frequency of %lld is not supported\n", fq);
107 ret = -EINVAL;
108 - goto free_err;
109 + goto err_endpoint;
110 }
111
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);
117 - goto free_err;
118 + goto err_endpoint;
119 }
120
121 ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
122 &xclk_freq);
123 if (ret) {
124 dev_err(dev, "Could not get xclk frequency\n");
125 - goto free_err;
126 + goto err_endpoint;
127 }
128
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",
132 xclk_freq);
133 ret = -EINVAL;
134 - goto free_err;
135 + goto err_endpoint;
136 }
137
138 ret = clk_set_rate(imx290->xclk, xclk_freq);
139 if (ret) {
140 dev_err(dev, "Could not set xclk frequency\n");
141 - goto free_err;
142 + goto err_endpoint;
143 }
144
145 ret = imx290_get_regulators(dev, imx290);
146 if (ret < 0) {
147 dev_err(dev, "Cannot get regulators\n");
148 - goto free_err;
149 + goto err_endpoint;
150 }
151
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);
157 - goto free_err;
158 + goto err_endpoint;
159 }
160
161 mutex_init(&imx290->lock);
162
163 - /*
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.
167 - */
168 - imx290_entity_init_cfg(&imx290->sd, NULL);
169 -
170 - ret = imx290_ctrl_init(imx290);
171 - if (ret < 0) {
172 - dev_err(dev, "Control initialization error %d\n", ret);
173 - goto free_mutex;
174 - }
175 -
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;
181 -
182 - imx290->pad.flags = MEDIA_PAD_FL_SOURCE;
183 - ret = media_entity_pads_init(&imx290->sd.entity, 1, &imx290->pad);
184 - if (ret < 0) {
185 - dev_err(dev, "Could not register media entity\n");
186 - goto free_ctrl;
187 - }
188 + ret = imx290_subdev_init(imx290);
189 + if (ret)
190 + goto err_mutex;
191
192 ret = v4l2_async_register_subdev(&imx290->sd);
193 if (ret < 0) {
194 dev_err(dev, "Could not register v4l2 device\n");
195 - goto free_entity;
196 + goto err_subdev;
197 }
198
199 /* Power on the device to match runtime PM state below */
200 ret = imx290_power_on(dev);
201 if (ret < 0) {
202 dev_err(dev, "Could not power on the device\n");
203 - goto free_entity;
204 + goto err_subdev;
205 }
206
207 pm_runtime_set_active(dev);
208 @@ -1269,13 +1288,11 @@ static int imx290_probe(struct i2c_clien
209
210 return 0;
211
212 -free_entity:
213 - media_entity_cleanup(&imx290->sd.entity);
214 -free_ctrl:
215 - v4l2_ctrl_handler_free(&imx290->ctrls);
216 -free_mutex:
217 +err_subdev:
218 + imx290_subdev_cleanup(imx290);
219 +err_mutex:
220 mutex_destroy(&imx290->lock);
221 -free_err:
222 +err_endpoint:
223 v4l2_fwnode_endpoint_free(&ep);
224
225 return ret;
226 @@ -1287,8 +1304,7 @@ static void imx290_remove(struct i2c_cli
227 struct imx290 *imx290 = to_imx290(sd);
228
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);
233
234 mutex_destroy(&imx290->lock);
235