File Index
Files and directories
Sign in
FILE

i2c-core-of.c

File preview with syntax highlighting.

linux-6.19.14/drivers/i2c/i2c-core-of.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Linux I2C core OF support code
  4 *
  5 * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de>
  6 * based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
  7 *
  8 * Copyright (C) 2013, 2018 Wolfram Sang <wsa@kernel.org>
  9 */
 10
 11#include <dt-bindings/i2c/i2c.h>
 12#include <linux/device.h>
 13#include <linux/err.h>
 14#include <linux/i2c.h>
 15#include <linux/module.h>
 16#include <linux/of.h>
 17#include <linux/of_device.h>
 18#include <linux/sysfs.h>
 19
 20#include "i2c-core.h"
 21
 22int of_i2c_get_board_info(struct device *dev, struct device_node *node,
 23			  struct i2c_board_info *info)
 24{
 25	u32 addr;
 26	int ret;
 27
 28	memset(info, 0, sizeof(*info));
 29
 30	if (of_alias_from_compatible(node, info->type, sizeof(info->type)) < 0) {
 31		dev_err(dev, "of_i2c: modalias failure on %pOF\n", node);
 32		return -EINVAL;
 33	}
 34
 35	ret = of_property_read_u32(node, "reg", &addr);
 36	if (ret) {
 37		dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
 38		return ret;
 39	}
 40
 41	if (addr & I2C_TEN_BIT_ADDRESS) {
 42		addr &= ~I2C_TEN_BIT_ADDRESS;
 43		info->flags |= I2C_CLIENT_TEN;
 44	}
 45
 46	if (addr & I2C_OWN_SLAVE_ADDRESS) {
 47		addr &= ~I2C_OWN_SLAVE_ADDRESS;
 48		info->flags |= I2C_CLIENT_SLAVE;
 49	}
 50
 51	info->addr = addr;
 52	info->fwnode = of_fwnode_handle(node);
 53
 54	if (of_property_read_bool(node, "host-notify"))
 55		info->flags |= I2C_CLIENT_HOST_NOTIFY;
 56
 57	if (of_property_read_bool(node, "wakeup-source"))
 58		info->flags |= I2C_CLIENT_WAKE;
 59
 60	return 0;
 61}
 62EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
 63
 64static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
 65						 struct device_node *node)
 66{
 67	struct i2c_client *client;
 68	struct i2c_board_info info;
 69	int ret;
 70
 71	dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
 72
 73	ret = of_i2c_get_board_info(&adap->dev, node, &info);
 74	if (ret)
 75		return ERR_PTR(ret);
 76
 77	client = i2c_new_client_device(adap, &info);
 78	if (IS_ERR(client))
 79		dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
 80
 81	return client;
 82}
 83
 84void of_i2c_register_devices(struct i2c_adapter *adap)
 85{
 86	struct device_node *bus, *node;
 87	struct i2c_client *client;
 88
 89	/* Only register child devices if the adapter has a node pointer set */
 90	if (!adap->dev.of_node)
 91		return;
 92
 93	dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
 94
 95	bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
 96	if (!bus)
 97		bus = of_node_get(adap->dev.of_node);
 98
 99	for_each_available_child_of_node(bus, node) {
100		if (of_node_test_and_set_flag(node, OF_POPULATED))
101			continue;
102
103		client = of_i2c_register_device(adap, node);
104		if (IS_ERR(client)) {
105			dev_err(&adap->dev,
106				 "Failed to create I2C device for %pOF\n",
107				 node);
108			of_node_clear_flag(node, OF_POPULATED);
109		}
110	}
111
112	of_node_put(bus);
113}
114
115static const struct of_device_id*
116i2c_of_match_device_sysfs(const struct of_device_id *matches,
117				  struct i2c_client *client)
118{
119	const char *name;
120
121	for (; matches->compatible[0]; matches++) {
122		/*
123		 * Adding devices through the i2c sysfs interface provides us
124		 * a string to match which may be compatible with the device
125		 * tree compatible strings, however with no actual of_node the
126		 * of_match_device() will not match
127		 */
128		if (sysfs_streq(client->name, matches->compatible))
129			return matches;
130
131		name = strchr(matches->compatible, ',');
132		if (!name)
133			name = matches->compatible;
134		else
135			name++;
136
137		if (sysfs_streq(client->name, name))
138			return matches;
139	}
140
141	return NULL;
142}
143
144const struct of_device_id
145*i2c_of_match_device(const struct of_device_id *matches,
146		     struct i2c_client *client)
147{
148	const struct of_device_id *match;
149
150	if (!(client && matches))
151		return NULL;
152
153	match = of_match_device(matches, &client->dev);
154	if (match)
155		return match;
156
157	return i2c_of_match_device_sysfs(matches, client);
158}
159
160#if IS_ENABLED(CONFIG_OF_DYNAMIC)
161static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
162			 void *arg)
163{
164	struct of_reconfig_data *rd = arg;
165	struct i2c_adapter *adap;
166	struct i2c_client *client;
167
168	switch (of_reconfig_get_state_change(action, rd)) {
169	case OF_RECONFIG_CHANGE_ADD:
170		adap = of_find_i2c_adapter_by_node(rd->dn->parent);
171		if (adap == NULL)
172			return NOTIFY_OK;	/* not for us */
173
174		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
175			put_device(&adap->dev);
176			return NOTIFY_OK;
177		}
178
179		/*
180		 * Clear the flag before adding the device so that fw_devlink
181		 * doesn't skip adding consumers to this device.
182		 */
183		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
184		client = of_i2c_register_device(adap, rd->dn);
185		if (IS_ERR(client)) {
186			dev_err(&adap->dev, "failed to create client for '%pOF'\n",
187				 rd->dn);
188			put_device(&adap->dev);
189			of_node_clear_flag(rd->dn, OF_POPULATED);
190			return notifier_from_errno(PTR_ERR(client));
191		}
192		put_device(&adap->dev);
193		break;
194	case OF_RECONFIG_CHANGE_REMOVE:
195		/* already depopulated? */
196		if (!of_node_check_flag(rd->dn, OF_POPULATED))
197			return NOTIFY_OK;
198
199		/* find our device by node */
200		client = of_find_i2c_device_by_node(rd->dn);
201		if (client == NULL)
202			return NOTIFY_OK;	/* no? not meant for us */
203
204		/* unregister takes one ref away */
205		i2c_unregister_device(client);
206
207		/* and put the reference of the find */
208		put_device(&client->dev);
209		break;
210	}
211
212	return NOTIFY_OK;
213}
214
215struct notifier_block i2c_of_notifier = {
216	.notifier_call = of_i2c_notify,
217};
218#endif /* CONFIG_OF_DYNAMIC */
~/ Read-only file preview.