xref: /linux/drivers/i2c/i2c-core-of-prober.c (revision cdd30ebb1b9f36159d66f088b61aee264e649d7a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Linux I2C core OF component prober code
4  *
5  * Copyright (C) 2024 Google LLC
6  */
7 
8 #include <linux/cleanup.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/dev_printk.h>
12 #include <linux/err.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/i2c-of-prober.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/slab.h>
20 #include <linux/stddef.h>
21 
22 /*
23  * Some devices, such as Google Hana Chromebooks, are produced by multiple
24  * vendors each using their preferred components. Such components are all
25  * in the device tree. Instead of having all of them enabled and having each
26  * driver separately try and probe its device while fighting over shared
27  * resources, they can be marked as "fail-needs-probe" and have a prober
28  * figure out which one is actually used beforehand.
29  *
30  * This prober assumes such drop-in parts are on the same I2C bus, have
31  * non-conflicting addresses, and can be directly probed by seeing which
32  * address responds.
33  *
34  * TODO:
35  * - Support I2C muxes
36  */
37 
i2c_of_probe_get_i2c_node(struct device * dev,const char * type)38 static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, const char *type)
39 {
40 	struct device_node *node __free(device_node) = of_find_node_by_name(NULL, type);
41 	if (!node) {
42 		dev_err(dev, "Could not find %s device node\n", type);
43 		return NULL;
44 	}
45 
46 	struct device_node *i2c_node __free(device_node) = of_get_parent(node);
47 	if (!of_node_name_eq(i2c_node, "i2c")) {
48 		dev_err(dev, "%s device isn't on I2C bus\n", type);
49 		return NULL;
50 	}
51 
52 	if (!of_device_is_available(i2c_node)) {
53 		dev_err(dev, "I2C controller not available\n");
54 		return NULL;
55 	}
56 
57 	return no_free_ptr(i2c_node);
58 }
59 
i2c_of_probe_enable_node(struct device * dev,struct device_node * node)60 static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node)
61 {
62 	int ret;
63 
64 	dev_dbg(dev, "Enabling %pOF\n", node);
65 
66 	struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL);
67 	if (!ocs)
68 		return -ENOMEM;
69 
70 	of_changeset_init(ocs);
71 	ret = of_changeset_update_prop_string(ocs, node, "status", "okay");
72 	if (ret)
73 		return ret;
74 
75 	ret = of_changeset_apply(ocs);
76 	if (ret) {
77 		/* ocs needs to be explicitly cleaned up before being freed. */
78 		of_changeset_destroy(ocs);
79 	} else {
80 		/*
81 		 * ocs is intentionally kept around as it needs to
82 		 * exist as long as the change is applied.
83 		 */
84 		void *ptr __always_unused = no_free_ptr(ocs);
85 	}
86 
87 	return ret;
88 }
89 
90 static const struct i2c_of_probe_ops i2c_of_probe_dummy_ops;
91 
92 /**
93  * i2c_of_probe_component() - probe for devices of "type" on the same i2c bus
94  * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages.
95  * @cfg: Pointer to the &struct i2c_of_probe_cfg containing callbacks and other options
96  *       for the prober.
97  * @ctx: Context data for callbacks.
98  *
99  * Probe for possible I2C components of the same "type" (&i2c_of_probe_cfg->type)
100  * on the same I2C bus that have their status marked as "fail-needs-probe".
101  *
102  * Assumes that across the entire device tree the only instances of nodes
103  * with "type" prefixed node names (not including the address portion) are
104  * the ones that need handling for second source components. In other words,
105  * if "type" is "touchscreen", then all device nodes named "touchscreen*"
106  * are the ones that need probing. There cannot be another "touchscreen*"
107  * node that is already enabled.
108  *
109  * Assumes that for each "type" of component, only one actually exists. In
110  * other words, only one matching and existing device will be enabled.
111  *
112  * Context: Process context only. Does non-atomic I2C transfers.
113  *          Should only be used from a driver probe function, as the function
114  *          can return -EPROBE_DEFER if the I2C adapter or other resources
115  *          are unavailable.
116  * Return: 0 on success or no-op, error code otherwise.
117  *         A no-op can happen when it seems like the device tree already
118  *         has components of the type to be probed already enabled. This
119  *         can happen when the device tree had not been updated to mark
120  *         the status of the to-be-probed components as "fail-needs-probe".
121  *         Or this function was already run with the same parameters and
122  *         succeeded in enabling a component. The latter could happen if
123  *         the user had multiple types of components to probe, and one of
124  *         them down the list caused a deferred probe. This is expected
125  *         behavior.
126  */
i2c_of_probe_component(struct device * dev,const struct i2c_of_probe_cfg * cfg,void * ctx)127 int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx)
128 {
129 	const struct i2c_of_probe_ops *ops;
130 	const char *type;
131 	struct i2c_adapter *i2c;
132 	int ret;
133 
134 	ops = cfg->ops ?: &i2c_of_probe_dummy_ops;
135 	type = cfg->type;
136 
137 	struct device_node *i2c_node __free(device_node) = i2c_of_probe_get_i2c_node(dev, type);
138 	if (!i2c_node)
139 		return -ENODEV;
140 
141 	/*
142 	 * If any devices of the given "type" are already enabled then this function is a no-op.
143 	 * Either the device tree hasn't been modified to work with this probe function, or the
144 	 * function had already run before and enabled some component.
145 	 */
146 	for_each_child_of_node_with_prefix(i2c_node, node, type)
147 		if (of_device_is_available(node))
148 			return 0;
149 
150 	i2c = of_get_i2c_adapter_by_node(i2c_node);
151 	if (!i2c)
152 		return dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n");
153 
154 	/* Grab and enable resources */
155 	ret = 0;
156 	if (ops->enable)
157 		ret = ops->enable(dev, i2c_node, ctx);
158 	if (ret)
159 		goto out_put_i2c_adapter;
160 
161 	for_each_child_of_node_with_prefix(i2c_node, node, type) {
162 		union i2c_smbus_data data;
163 		u32 addr;
164 
165 		if (of_property_read_u32(node, "reg", &addr))
166 			continue;
167 		if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0)
168 			continue;
169 
170 		/* Found a device that is responding */
171 		if (ops->cleanup_early)
172 			ops->cleanup_early(dev, ctx);
173 		ret = i2c_of_probe_enable_node(dev, node);
174 		break;
175 	}
176 
177 	if (ops->cleanup)
178 		ops->cleanup(dev, ctx);
179 out_put_i2c_adapter:
180 	i2c_put_adapter(i2c);
181 
182 	return ret;
183 }
184 EXPORT_SYMBOL_NS_GPL(i2c_of_probe_component, "I2C_OF_PROBER");
185 
i2c_of_probe_simple_get_supply(struct device * dev,struct device_node * node,struct i2c_of_probe_simple_ctx * ctx)186 static int i2c_of_probe_simple_get_supply(struct device *dev, struct device_node *node,
187 					  struct i2c_of_probe_simple_ctx *ctx)
188 {
189 	const char *supply_name;
190 	struct regulator *supply;
191 
192 	/*
193 	 * It's entirely possible for the component's device node to not have the
194 	 * regulator supplies. While it does not make sense from a hardware perspective,
195 	 * the supplies could be always on or otherwise not modeled in the device tree,
196 	 * but the device would still work.
197 	 */
198 	supply_name = ctx->opts->supply_name;
199 	if (!supply_name)
200 		return 0;
201 
202 	supply = of_regulator_get_optional(dev, node, supply_name);
203 	if (IS_ERR(supply)) {
204 		return dev_err_probe(dev, PTR_ERR(supply),
205 				     "Failed to get regulator supply \"%s\" from %pOF\n",
206 				     supply_name, node);
207 	}
208 
209 	ctx->supply = supply;
210 
211 	return 0;
212 }
213 
i2c_of_probe_simple_put_supply(struct i2c_of_probe_simple_ctx * ctx)214 static void i2c_of_probe_simple_put_supply(struct i2c_of_probe_simple_ctx *ctx)
215 {
216 	regulator_put(ctx->supply);
217 	ctx->supply = NULL;
218 }
219 
i2c_of_probe_simple_enable_regulator(struct device * dev,struct i2c_of_probe_simple_ctx * ctx)220 static int i2c_of_probe_simple_enable_regulator(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
221 {
222 	int ret;
223 
224 	if (!ctx->supply)
225 		return 0;
226 
227 	dev_dbg(dev, "Enabling regulator supply \"%s\"\n", ctx->opts->supply_name);
228 
229 	ret = regulator_enable(ctx->supply);
230 	if (ret)
231 		return ret;
232 
233 	if (ctx->opts->post_power_on_delay_ms)
234 		msleep(ctx->opts->post_power_on_delay_ms);
235 
236 	return 0;
237 }
238 
i2c_of_probe_simple_disable_regulator(struct device * dev,struct i2c_of_probe_simple_ctx * ctx)239 static void i2c_of_probe_simple_disable_regulator(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
240 {
241 	if (!ctx->supply)
242 		return;
243 
244 	dev_dbg(dev, "Disabling regulator supply \"%s\"\n", ctx->opts->supply_name);
245 
246 	regulator_disable(ctx->supply);
247 }
248 
i2c_of_probe_simple_get_gpiod(struct device * dev,struct device_node * node,struct i2c_of_probe_simple_ctx * ctx)249 static int i2c_of_probe_simple_get_gpiod(struct device *dev, struct device_node *node,
250 					 struct i2c_of_probe_simple_ctx *ctx)
251 {
252 	struct fwnode_handle *fwnode = of_fwnode_handle(node);
253 	struct gpio_desc *gpiod;
254 	const char *con_id;
255 
256 	/* NULL signals no GPIO needed */
257 	if (!ctx->opts->gpio_name)
258 		return 0;
259 
260 	/* An empty string signals an unnamed GPIO */
261 	if (!ctx->opts->gpio_name[0])
262 		con_id = NULL;
263 	else
264 		con_id = ctx->opts->gpio_name;
265 
266 	gpiod = fwnode_gpiod_get_index(fwnode, con_id, 0, GPIOD_ASIS, "i2c-of-prober");
267 	if (IS_ERR(gpiod))
268 		return PTR_ERR(gpiod);
269 
270 	ctx->gpiod = gpiod;
271 
272 	return 0;
273 }
274 
i2c_of_probe_simple_put_gpiod(struct i2c_of_probe_simple_ctx * ctx)275 static void i2c_of_probe_simple_put_gpiod(struct i2c_of_probe_simple_ctx *ctx)
276 {
277 	gpiod_put(ctx->gpiod);
278 	ctx->gpiod = NULL;
279 }
280 
i2c_of_probe_simple_set_gpio(struct device * dev,struct i2c_of_probe_simple_ctx * ctx)281 static int i2c_of_probe_simple_set_gpio(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
282 {
283 	int ret;
284 
285 	if (!ctx->gpiod)
286 		return 0;
287 
288 	dev_dbg(dev, "Configuring GPIO\n");
289 
290 	ret = gpiod_direction_output(ctx->gpiod, ctx->opts->gpio_assert_to_enable);
291 	if (ret)
292 		return ret;
293 
294 	if (ctx->opts->post_gpio_config_delay_ms)
295 		msleep(ctx->opts->post_gpio_config_delay_ms);
296 
297 	return 0;
298 }
299 
i2c_of_probe_simple_disable_gpio(struct device * dev,struct i2c_of_probe_simple_ctx * ctx)300 static void i2c_of_probe_simple_disable_gpio(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
301 {
302 	gpiod_set_value(ctx->gpiod, !ctx->opts->gpio_assert_to_enable);
303 }
304 
305 /**
306  * i2c_of_probe_simple_enable - Simple helper for I2C OF prober to get and enable resources
307  * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages
308  * @bus_node: Pointer to the &struct device_node of the I2C adapter.
309  * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context.
310  *
311  * If &i2c_of_probe_simple_opts->supply_name is given, request the named regulator supply.
312  * If &i2c_of_probe_simple_opts->gpio_name is given, request the named GPIO. Or if it is
313  * the empty string, request the unnamed GPIO.
314  * If a regulator supply was found, enable that regulator.
315  * If a GPIO line was found, configure the GPIO line to output and set value
316  * according to given options.
317  *
318  * Return: %0 on success or no-op, or a negative error number on failure.
319  */
i2c_of_probe_simple_enable(struct device * dev,struct device_node * bus_node,void * data)320 int i2c_of_probe_simple_enable(struct device *dev, struct device_node *bus_node, void *data)
321 {
322 	struct i2c_of_probe_simple_ctx *ctx = data;
323 	struct device_node *node;
324 	const char *compat;
325 	int ret;
326 
327 	dev_dbg(dev, "Requesting resources for components under I2C bus %pOF\n", bus_node);
328 
329 	if (!ctx || !ctx->opts)
330 		return -EINVAL;
331 
332 	compat = ctx->opts->res_node_compatible;
333 	if (!compat)
334 		return -EINVAL;
335 
336 	node = of_get_compatible_child(bus_node, compat);
337 	if (!node)
338 		return dev_err_probe(dev, -ENODEV, "No device compatible with \"%s\" found\n",
339 				     compat);
340 
341 	ret = i2c_of_probe_simple_get_supply(dev, node, ctx);
342 	if (ret)
343 		goto out_put_node;
344 
345 	ret = i2c_of_probe_simple_get_gpiod(dev, node, ctx);
346 	if (ret)
347 		goto out_put_supply;
348 
349 	ret = i2c_of_probe_simple_enable_regulator(dev, ctx);
350 	if (ret)
351 		goto out_put_gpiod;
352 
353 	ret = i2c_of_probe_simple_set_gpio(dev, ctx);
354 	if (ret)
355 		goto out_disable_regulator;
356 
357 	return 0;
358 
359 out_disable_regulator:
360 	i2c_of_probe_simple_disable_regulator(dev, ctx);
361 out_put_gpiod:
362 	i2c_of_probe_simple_put_gpiod(ctx);
363 out_put_supply:
364 	i2c_of_probe_simple_put_supply(ctx);
365 out_put_node:
366 	of_node_put(node);
367 	return ret;
368 }
369 EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, "I2C_OF_PROBER");
370 
371 /**
372  * i2c_of_probe_simple_cleanup_early - \
373  *	Simple helper for I2C OF prober to release GPIOs before component is enabled
374  * @dev: Pointer to the &struct device of the caller; unused.
375  * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context.
376  *
377  * GPIO descriptors are exclusive and have to be released before the
378  * actual driver probes so that the latter can acquire them.
379  */
i2c_of_probe_simple_cleanup_early(struct device * dev,void * data)380 void i2c_of_probe_simple_cleanup_early(struct device *dev, void *data)
381 {
382 	struct i2c_of_probe_simple_ctx *ctx = data;
383 
384 	i2c_of_probe_simple_put_gpiod(ctx);
385 }
386 EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup_early, "I2C_OF_PROBER");
387 
388 /**
389  * i2c_of_probe_simple_cleanup - Clean up and release resources for I2C OF prober simple helpers
390  * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages
391  * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context.
392  *
393  * * If a GPIO line was found and not yet released, set its value to the opposite of that
394  *   set in i2c_of_probe_simple_enable() and release it.
395  * * If a regulator supply was found, disable that regulator and release it.
396  */
i2c_of_probe_simple_cleanup(struct device * dev,void * data)397 void i2c_of_probe_simple_cleanup(struct device *dev, void *data)
398 {
399 	struct i2c_of_probe_simple_ctx *ctx = data;
400 
401 	/* GPIO operations here are no-ops if i2c_of_probe_simple_cleanup_early was called. */
402 	i2c_of_probe_simple_disable_gpio(dev, ctx);
403 	i2c_of_probe_simple_put_gpiod(ctx);
404 
405 	i2c_of_probe_simple_disable_regulator(dev, ctx);
406 	i2c_of_probe_simple_put_supply(ctx);
407 }
408 EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup, "I2C_OF_PROBER");
409 
410 struct i2c_of_probe_ops i2c_of_probe_simple_ops = {
411 	.enable = i2c_of_probe_simple_enable,
412 	.cleanup_early = i2c_of_probe_simple_cleanup_early,
413 	.cleanup = i2c_of_probe_simple_cleanup,
414 };
415 EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_ops, "I2C_OF_PROBER");
416