summaryrefslogtreecommitdiff
path: root/drivers/i2c/i2c-core-of-prober.c
blob: 76df75c51caee911b090d1234fd0779ee59b7595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Linux I2C core OF component prober code
 *
 * Copyright (C) 2024 Google LLC
 */

#include <linux/cleanup.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/dev_printk.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/i2c-of-prober.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/stddef.h>

/*
 * Some devices, such as Google Hana Chromebooks, are produced by multiple
 * vendors each using their preferred components. Such components are all
 * in the device tree. Instead of having all of them enabled and having each
 * driver separately try and probe its device while fighting over shared
 * resources, they can be marked as "fail-needs-probe" and have a prober
 * figure out which one is actually used beforehand.
 *
 * This prober assumes such drop-in parts are on the same I2C bus, have
 * non-conflicting addresses, and can be directly probed by seeing which
 * address responds.
 *
 * TODO:
 * - Support handling common GPIOs.
 * - Support I2C muxes
 */

static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, const char *type)
{
	struct device_node *node __free(device_node) = of_find_node_by_name(NULL, type);
	if (!node) {
		dev_err(dev, "Could not find %s device node\n", type);
		return NULL;
	}

	struct device_node *i2c_node __free(device_node) = of_get_parent(node);
	if (!of_node_name_eq(i2c_node, "i2c")) {
		dev_err(dev, "%s device isn't on I2C bus\n", type);
		return NULL;
	}

	if (!of_device_is_available(i2c_node)) {
		dev_err(dev, "I2C controller not available\n");
		return NULL;
	}

	return no_free_ptr(i2c_node);
}

static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node)
{
	int ret;

	dev_dbg(dev, "Enabling %pOF\n", node);

	struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL);
	if (!ocs)
		return -ENOMEM;

	of_changeset_init(ocs);
	ret = of_changeset_update_prop_string(ocs, node, "status", "okay");
	if (ret)
		return ret;

	ret = of_changeset_apply(ocs);
	if (ret) {
		/* ocs needs to be explicitly cleaned up before being freed. */
		of_changeset_destroy(ocs);
	} else {
		/*
		 * ocs is intentionally kept around as it needs to
		 * exist as long as the change is applied.
		 */
		void *ptr __always_unused = no_free_ptr(ocs);
	}

	return ret;
}

static const struct i2c_of_probe_ops i2c_of_probe_dummy_ops;

/**
 * i2c_of_probe_component() - probe for devices of "type" on the same i2c bus
 * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages.
 * @cfg: Pointer to the &struct i2c_of_probe_cfg containing callbacks and other options
 *       for the prober.
 * @ctx: Context data for callbacks.
 *
 * Probe for possible I2C components of the same "type" (&i2c_of_probe_cfg->type)
 * on the same I2C bus that have their status marked as "fail-needs-probe".
 *
 * Assumes that across the entire device tree the only instances of nodes
 * with "type" prefixed node names (not including the address portion) are
 * the ones that need handling for second source components. In other words,
 * if "type" is "touchscreen", then all device nodes named "touchscreen*"
 * are the ones that need probing. There cannot be another "touchscreen*"
 * node that is already enabled.
 *
 * Assumes that for each "type" of component, only one actually exists. In
 * other words, only one matching and existing device will be enabled.
 *
 * Context: Process context only. Does non-atomic I2C transfers.
 *          Should only be used from a driver probe function, as the function
 *          can return -EPROBE_DEFER if the I2C adapter or other resources
 *          are unavailable.
 * Return: 0 on success or no-op, error code otherwise.
 *         A no-op can happen when it seems like the device tree already
 *         has components of the type to be probed already enabled. This
 *         can happen when the device tree had not been updated to mark
 *         the status of the to-be-probed components as "fail-needs-probe".
 *         Or this function was already run with the same parameters and
 *         succeeded in enabling a component. The latter could happen if
 *         the user had multiple types of components to probe, and one of
 *         them down the list caused a deferred probe. This is expected
 *         behavior.
 */
int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx)
{
	const struct i2c_of_probe_ops *ops;
	const char *type;
	struct i2c_adapter *i2c;
	int ret;

	ops = cfg->ops ?: &i2c_of_probe_dummy_ops;
	type = cfg->type;

	struct device_node *i2c_node __free(device_node) = i2c_of_probe_get_i2c_node(dev, type);
	if (!i2c_node)
		return -ENODEV;

	/*
	 * If any devices of the given "type" are already enabled then this function is a no-op.
	 * Either the device tree hasn't been modified to work with this probe function, or the
	 * function had already run before and enabled some component.
	 */
	for_each_child_of_node_with_prefix(i2c_node, node, type)
		if (of_device_is_available(node))
			return 0;

	i2c = of_get_i2c_adapter_by_node(i2c_node);
	if (!i2c)
		return dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n");

	/* Grab and enable resources */
	ret = 0;
	if (ops->enable)
		ret = ops->enable(dev, i2c_node, ctx);
	if (ret)
		goto out_put_i2c_adapter;

	for_each_child_of_node_with_prefix(i2c_node, node, type) {
		union i2c_smbus_data data;
		u32 addr;

		if (of_property_read_u32(node, "reg", &addr))
			continue;
		if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0)
			continue;

		/* Found a device that is responding */
		if (ops->cleanup_early)
			ops->cleanup_early(dev, ctx);
		ret = i2c_of_probe_enable_node(dev, node);
		break;
	}

	if (ops->cleanup)
		ops->cleanup(dev, ctx);
out_put_i2c_adapter:
	i2c_put_adapter(i2c);

	return ret;
}
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_component, I2C_OF_PROBER);

static int i2c_of_probe_simple_get_supply(struct device *dev, struct device_node *node,
					  struct i2c_of_probe_simple_ctx *ctx)
{
	const char *supply_name;
	struct regulator *supply;

	/*
	 * It's entirely possible for the component's device node to not have the
	 * regulator supplies. While it does not make sense from a hardware perspective,
	 * the supplies could be always on or otherwise not modeled in the device tree,
	 * but the device would still work.
	 */
	supply_name = ctx->opts->supply_name;
	if (!supply_name)
		return 0;

	supply = of_regulator_get_optional(dev, node, supply_name);
	if (IS_ERR(supply)) {
		return dev_err_probe(dev, PTR_ERR(supply),
				     "Failed to get regulator supply \"%s\" from %pOF\n",
				     supply_name, node);
	}

	ctx->supply = supply;

	return 0;
}

static void i2c_of_probe_simple_put_supply(struct i2c_of_probe_simple_ctx *ctx)
{
	regulator_put(ctx->supply);
	ctx->supply = NULL;
}

static int i2c_of_probe_simple_enable_regulator(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
{
	int ret;

	if (!ctx->supply)
		return 0;

	dev_dbg(dev, "Enabling regulator supply \"%s\"\n", ctx->opts->supply_name);

	ret = regulator_enable(ctx->supply);
	if (ret)
		return ret;

	if (ctx->opts->post_power_on_delay_ms)
		msleep(ctx->opts->post_power_on_delay_ms);

	return 0;
}

static void i2c_of_probe_simple_disable_regulator(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
{
	if (!ctx->supply)
		return;

	dev_dbg(dev, "Disabling regulator supply \"%s\"\n", ctx->opts->supply_name);

	regulator_disable(ctx->supply);
}

/**
 * i2c_of_probe_simple_enable - Simple helper for I2C OF prober to get and enable resources
 * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages
 * @bus_node: Pointer to the &struct device_node of the I2C adapter.
 * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context.
 *
 * If &i2c_of_probe_simple_opts->supply_name is given, request the named regulator supply.
 * If a regulator supply was found, enable that regulator.
 *
 * Return: %0 on success or no-op, or a negative error number on failure.
 */
int i2c_of_probe_simple_enable(struct device *dev, struct device_node *bus_node, void *data)
{
	struct i2c_of_probe_simple_ctx *ctx = data;
	struct device_node *node;
	const char *compat;
	int ret;

	dev_dbg(dev, "Requesting resources for components under I2C bus %pOF\n", bus_node);

	if (!ctx || !ctx->opts)
		return -EINVAL;

	compat = ctx->opts->res_node_compatible;
	if (!compat)
		return -EINVAL;

	node = of_get_compatible_child(bus_node, compat);
	if (!node)
		return dev_err_probe(dev, -ENODEV, "No device compatible with \"%s\" found\n",
				     compat);

	ret = i2c_of_probe_simple_get_supply(dev, node, ctx);
	if (ret)
		goto out_put_node;

	ret = i2c_of_probe_simple_enable_regulator(dev, ctx);
	if (ret)
		goto out_put_supply;

	return 0;

out_put_supply:
	i2c_of_probe_simple_put_supply(ctx);
out_put_node:
	of_node_put(node);
	return ret;
}
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, I2C_OF_PROBER);

/**
 * i2c_of_probe_simple_cleanup - Clean up and release resources for I2C OF prober simple helpers
 * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages
 * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context.
 *
 * * If a regulator supply was found, disable that regulator and release it.
 */
void i2c_of_probe_simple_cleanup(struct device *dev, void *data)
{
	struct i2c_of_probe_simple_ctx *ctx = data;

	i2c_of_probe_simple_disable_regulator(dev, ctx);
	i2c_of_probe_simple_put_supply(ctx);
}
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup, I2C_OF_PROBER);

struct i2c_of_probe_ops i2c_of_probe_simple_ops = {
	.enable = i2c_of_probe_simple_enable,
	.cleanup = i2c_of_probe_simple_cleanup,
};
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_ops, I2C_OF_PROBER);