79df9996af404347d35b87b59fe300598f8a92e3
[openwrt/staging/xback.git] /
1 From 4afc11ae3b786ad8425435b70ab0285fbf951b05 Mon Sep 17 00:00:00 2001
2 From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
3 Date: Mon, 16 Jan 2023 15:44:45 +0100
4 Subject: [PATCH] media: i2c: imx290: Factor out DT parsing to separate
5 function
6
7 Upstream commit 6b69c52277ed
8
9 Make the probe() function more readable by factoring out the DT parsing
10 code to a separate function. No functional change intended.
11
12 Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
13 Reviewed-by: Alexander Stein <alexander.stein@ew.tq-group.com>
14 Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
15 Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
16 ---
17 drivers/media/i2c/imx290.c | 95 +++++++++++++++++++++-----------------
18 1 file changed, 52 insertions(+), 43 deletions(-)
19
20 --- a/drivers/media/i2c/imx290.c
21 +++ b/drivers/media/i2c/imx290.c
22 @@ -1142,111 +1142,124 @@ static s64 imx290_check_link_freqs(const
23 return 0;
24 }
25
26 -static int imx290_probe(struct i2c_client *client)
27 +static int imx290_parse_dt(struct imx290 *imx290)
28 {
29 - struct device *dev = &client->dev;
30 - struct fwnode_handle *endpoint;
31 /* Only CSI2 is supported for now: */
32 struct v4l2_fwnode_endpoint ep = {
33 .bus_type = V4L2_MBUS_CSI2_DPHY
34 };
35 - struct imx290 *imx290;
36 - u32 xclk_freq;
37 - s64 fq;
38 + struct fwnode_handle *endpoint;
39 int ret;
40 + s64 fq;
41
42 - imx290 = devm_kzalloc(dev, sizeof(*imx290), GFP_KERNEL);
43 - if (!imx290)
44 - return -ENOMEM;
45 -
46 - imx290->dev = dev;
47 - imx290->regmap = devm_regmap_init_i2c(client, &imx290_regmap_config);
48 - if (IS_ERR(imx290->regmap)) {
49 - dev_err(dev, "Unable to initialize I2C\n");
50 - return -ENODEV;
51 - }
52 -
53 - endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
54 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(imx290->dev), NULL);
55 if (!endpoint) {
56 - dev_err(dev, "Endpoint node not found\n");
57 + dev_err(imx290->dev, "Endpoint node not found\n");
58 return -EINVAL;
59 }
60
61 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep);
62 fwnode_handle_put(endpoint);
63 if (ret == -ENXIO) {
64 - dev_err(dev, "Unsupported bus type, should be CSI2\n");
65 - goto err_endpoint;
66 + dev_err(imx290->dev, "Unsupported bus type, should be CSI2\n");
67 + goto done;
68 } else if (ret) {
69 - dev_err(dev, "Parsing endpoint node failed\n");
70 - goto err_endpoint;
71 + dev_err(imx290->dev, "Parsing endpoint node failed\n");
72 + goto done;
73 }
74
75 /* Get number of data lanes */
76 imx290->nlanes = ep.bus.mipi_csi2.num_data_lanes;
77 if (imx290->nlanes != 2 && imx290->nlanes != 4) {
78 - dev_err(dev, "Invalid data lanes: %d\n", imx290->nlanes);
79 + dev_err(imx290->dev, "Invalid data lanes: %d\n", imx290->nlanes);
80 ret = -EINVAL;
81 - goto err_endpoint;
82 + goto done;
83 }
84
85 - dev_dbg(dev, "Using %u data lanes\n", imx290->nlanes);
86 + dev_dbg(imx290->dev, "Using %u data lanes\n", imx290->nlanes);
87
88 if (!ep.nr_of_link_frequencies) {
89 - dev_err(dev, "link-frequency property not found in DT\n");
90 + dev_err(imx290->dev, "link-frequency property not found in DT\n");
91 ret = -EINVAL;
92 - goto err_endpoint;
93 + goto done;
94 }
95
96 /* Check that link frequences for all the modes are in device tree */
97 fq = imx290_check_link_freqs(imx290, &ep);
98 if (fq) {
99 - dev_err(dev, "Link frequency of %lld is not supported\n", fq);
100 + dev_err(imx290->dev, "Link frequency of %lld is not supported\n",
101 + fq);
102 ret = -EINVAL;
103 - goto err_endpoint;
104 + goto done;
105 }
106
107 + ret = 0;
108 +
109 +done:
110 + v4l2_fwnode_endpoint_free(&ep);
111 + return ret;
112 +}
113 +
114 +static int imx290_probe(struct i2c_client *client)
115 +{
116 + struct device *dev = &client->dev;
117 + struct imx290 *imx290;
118 + u32 xclk_freq;
119 + int ret;
120 +
121 + imx290 = devm_kzalloc(dev, sizeof(*imx290), GFP_KERNEL);
122 + if (!imx290)
123 + return -ENOMEM;
124 +
125 + imx290->dev = dev;
126 + imx290->regmap = devm_regmap_init_i2c(client, &imx290_regmap_config);
127 + if (IS_ERR(imx290->regmap)) {
128 + dev_err(dev, "Unable to initialize I2C\n");
129 + return -ENODEV;
130 + }
131 +
132 + ret = imx290_parse_dt(imx290);
133 + if (ret)
134 + return ret;
135 +
136 /* get system clock (xclk) */
137 imx290->xclk = devm_clk_get(dev, "xclk");
138 if (IS_ERR(imx290->xclk)) {
139 dev_err(dev, "Could not get xclk");
140 - ret = PTR_ERR(imx290->xclk);
141 - goto err_endpoint;
142 + return PTR_ERR(imx290->xclk);
143 }
144
145 ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
146 &xclk_freq);
147 if (ret) {
148 dev_err(dev, "Could not get xclk frequency\n");
149 - goto err_endpoint;
150 + return ret;
151 }
152
153 /* external clock must be 37.125 MHz */
154 if (xclk_freq != 37125000) {
155 dev_err(dev, "External clock frequency %u is not supported\n",
156 xclk_freq);
157 - ret = -EINVAL;
158 - goto err_endpoint;
159 + return -EINVAL;
160 }
161
162 ret = clk_set_rate(imx290->xclk, xclk_freq);
163 if (ret) {
164 dev_err(dev, "Could not set xclk frequency\n");
165 - goto err_endpoint;
166 + return ret;
167 }
168
169 ret = imx290_get_regulators(dev, imx290);
170 if (ret < 0) {
171 dev_err(dev, "Cannot get regulators\n");
172 - goto err_endpoint;
173 + return ret;
174 }
175
176 imx290->rst_gpio = devm_gpiod_get_optional(dev, "reset",
177 GPIOD_OUT_HIGH);
178 if (IS_ERR(imx290->rst_gpio)) {
179 dev_err(dev, "Cannot get reset gpio\n");
180 - ret = PTR_ERR(imx290->rst_gpio);
181 - goto err_endpoint;
182 + return PTR_ERR(imx290->rst_gpio);
183 }
184
185 mutex_init(&imx290->lock);
186 @@ -1272,16 +1285,12 @@ static int imx290_probe(struct i2c_clien
187 pm_runtime_enable(dev);
188 pm_runtime_idle(dev);
189
190 - v4l2_fwnode_endpoint_free(&ep);
191 -
192 return 0;
193
194 err_subdev:
195 imx290_subdev_cleanup(imx290);
196 err_mutex:
197 mutex_destroy(&imx290->lock);
198 -err_endpoint:
199 - v4l2_fwnode_endpoint_free(&ep);
200
201 return ret;
202 }