1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux I2C core OF support code
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>
17 #include <linux/of_device.h>
18 #include <linux/sysfs.h>
22 int of_i2c_get_board_info(struct device *dev, struct device_node *node,
23 struct i2c_board_info *info)
28 memset(info, 0, sizeof(*info));
30 if (of_modalias_node(node, info->type, sizeof(info->type)) < 0) {
31 dev_err(dev, "of_i2c: modalias failure on %pOF\n", node);
35 ret = of_property_read_u32(node, "reg", &addr);
37 dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
41 if (addr & I2C_TEN_BIT_ADDRESS) {
42 addr &= ~I2C_TEN_BIT_ADDRESS;
43 info->flags |= I2C_CLIENT_TEN;
46 if (addr & I2C_OWN_SLAVE_ADDRESS) {
47 addr &= ~I2C_OWN_SLAVE_ADDRESS;
48 info->flags |= I2C_CLIENT_SLAVE;
54 if (of_property_read_bool(node, "host-notify"))
55 info->flags |= I2C_CLIENT_HOST_NOTIFY;
57 if (of_get_property(node, "wakeup-source", NULL))
58 info->flags |= I2C_CLIENT_WAKE;
62 EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
64 static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
65 struct device_node *node)
67 struct i2c_client *client;
68 struct i2c_board_info info;
71 dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
73 ret = of_i2c_get_board_info(&adap->dev, node, &info);
77 client = i2c_new_device(adap, &info);
79 dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
80 return ERR_PTR(-EINVAL);
85 void of_i2c_register_devices(struct i2c_adapter *adap)
87 struct device_node *bus, *node;
88 struct i2c_client *client;
90 /* Only register child devices if the adapter has a node pointer set */
91 if (!adap->dev.of_node)
94 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
96 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
98 bus = of_node_get(adap->dev.of_node);
100 for_each_available_child_of_node(bus, node) {
101 if (of_node_test_and_set_flag(node, OF_POPULATED))
104 client = of_i2c_register_device(adap, node);
105 if (IS_ERR(client)) {
107 "Failed to create I2C device for %pOF\n",
109 of_node_clear_flag(node, OF_POPULATED);
116 static int of_dev_node_match(struct device *dev, const void *data)
118 return dev->of_node == data;
121 static int of_dev_or_parent_node_match(struct device *dev, const void *data)
123 if (dev->of_node == data)
127 return dev->parent->of_node == data;
132 /* must call put_device() when done with returned i2c_client device */
133 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
136 struct i2c_client *client;
138 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
142 client = i2c_verify_client(dev);
148 EXPORT_SYMBOL(of_find_i2c_device_by_node);
150 /* must call put_device() when done with returned i2c_adapter device */
151 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
154 struct i2c_adapter *adapter;
156 dev = bus_find_device(&i2c_bus_type, NULL, node,
157 of_dev_or_parent_node_match);
161 adapter = i2c_verify_adapter(dev);
167 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
169 /* must call i2c_put_adapter() when done with returned i2c_adapter device */
170 struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
172 struct i2c_adapter *adapter;
174 adapter = of_find_i2c_adapter_by_node(node);
178 if (!try_module_get(adapter->owner)) {
179 put_device(&adapter->dev);
185 EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
187 static const struct of_device_id*
188 i2c_of_match_device_sysfs(const struct of_device_id *matches,
189 struct i2c_client *client)
193 for (; matches->compatible[0]; matches++) {
195 * Adding devices through the i2c sysfs interface provides us
196 * a string to match which may be compatible with the device
197 * tree compatible strings, however with no actual of_node the
198 * of_match_device() will not match
200 if (sysfs_streq(client->name, matches->compatible))
203 name = strchr(matches->compatible, ',');
205 name = matches->compatible;
209 if (sysfs_streq(client->name, name))
216 const struct of_device_id
217 *i2c_of_match_device(const struct of_device_id *matches,
218 struct i2c_client *client)
220 const struct of_device_id *match;
222 if (!(client && matches))
225 match = of_match_device(matches, &client->dev);
229 return i2c_of_match_device_sysfs(matches, client);
231 EXPORT_SYMBOL_GPL(i2c_of_match_device);
233 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
234 static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
237 struct of_reconfig_data *rd = arg;
238 struct i2c_adapter *adap;
239 struct i2c_client *client;
241 switch (of_reconfig_get_state_change(action, rd)) {
242 case OF_RECONFIG_CHANGE_ADD:
243 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
245 return NOTIFY_OK; /* not for us */
247 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
248 put_device(&adap->dev);
252 client = of_i2c_register_device(adap, rd->dn);
253 put_device(&adap->dev);
255 if (IS_ERR(client)) {
256 dev_err(&adap->dev, "failed to create client for '%pOF'\n",
258 of_node_clear_flag(rd->dn, OF_POPULATED);
259 return notifier_from_errno(PTR_ERR(client));
262 case OF_RECONFIG_CHANGE_REMOVE:
263 /* already depopulated? */
264 if (!of_node_check_flag(rd->dn, OF_POPULATED))
267 /* find our device by node */
268 client = of_find_i2c_device_by_node(rd->dn);
270 return NOTIFY_OK; /* no? not meant for us */
272 /* unregister takes one ref away */
273 i2c_unregister_device(client);
275 /* and put the reference of the find */
276 put_device(&client->dev);
283 struct notifier_block i2c_of_notifier = {
284 .notifier_call = of_i2c_notify,
286 #endif /* CONFIG_OF_DYNAMIC */