2 * Linux I2C core OF support code
4 * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de>
5 * based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
7 * Copyright (C) 2013, 2018 Wolfram Sang <wsa@the-dreams.de>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
15 #include <dt-bindings/i2c/i2c.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/i2c.h>
19 #include <linux/module.h>
21 #include <linux/of_device.h>
25 static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
26 struct device_node *node)
28 struct i2c_client *client;
29 struct i2c_board_info info = {};
30 struct dev_archdata dev_ad = {};
34 dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
36 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
37 dev_err(&adap->dev, "of_i2c: modalias failure on %pOF\n",
39 return ERR_PTR(-EINVAL);
42 ret = of_property_read_u32(node, "reg", &addr);
44 dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", node);
48 if (addr & I2C_TEN_BIT_ADDRESS) {
49 addr &= ~I2C_TEN_BIT_ADDRESS;
50 info.flags |= I2C_CLIENT_TEN;
53 if (addr & I2C_OWN_SLAVE_ADDRESS) {
54 addr &= ~I2C_OWN_SLAVE_ADDRESS;
55 info.flags |= I2C_CLIENT_SLAVE;
59 info.archdata = &dev_ad;
60 info.of_node = of_node_get(node);
62 if (of_property_read_bool(node, "host-notify"))
63 info.flags |= I2C_CLIENT_HOST_NOTIFY;
65 if (of_get_property(node, "wakeup-source", NULL))
66 info.flags |= I2C_CLIENT_WAKE;
68 client = i2c_new_device(adap, &info);
70 dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
72 return ERR_PTR(-EINVAL);
77 void of_i2c_register_devices(struct i2c_adapter *adap)
79 struct device_node *bus, *node;
80 struct i2c_client *client;
82 /* Only register child devices if the adapter has a node pointer set */
83 if (!adap->dev.of_node)
86 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
88 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
90 bus = of_node_get(adap->dev.of_node);
92 for_each_available_child_of_node(bus, node) {
93 if (of_node_test_and_set_flag(node, OF_POPULATED))
96 client = of_i2c_register_device(adap, node);
99 "Failed to create I2C device for %pOF\n",
101 of_node_clear_flag(node, OF_POPULATED);
108 static int of_dev_node_match(struct device *dev, void *data)
110 return dev->of_node == data;
113 /* must call put_device() when done with returned i2c_client device */
114 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
117 struct i2c_client *client;
119 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
123 client = i2c_verify_client(dev);
129 EXPORT_SYMBOL(of_find_i2c_device_by_node);
131 /* must call put_device() when done with returned i2c_adapter device */
132 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
135 struct i2c_adapter *adapter;
137 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
141 adapter = i2c_verify_adapter(dev);
147 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
149 /* must call i2c_put_adapter() when done with returned i2c_adapter device */
150 struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
152 struct i2c_adapter *adapter;
154 adapter = of_find_i2c_adapter_by_node(node);
158 if (!try_module_get(adapter->owner)) {
159 put_device(&adapter->dev);
165 EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
167 static const struct of_device_id*
168 i2c_of_match_device_sysfs(const struct of_device_id *matches,
169 struct i2c_client *client)
173 for (; matches->compatible[0]; matches++) {
175 * Adding devices through the i2c sysfs interface provides us
176 * a string to match which may be compatible with the device
177 * tree compatible strings, however with no actual of_node the
178 * of_match_device() will not match
180 if (sysfs_streq(client->name, matches->compatible))
183 name = strchr(matches->compatible, ',');
185 name = matches->compatible;
189 if (sysfs_streq(client->name, name))
196 const struct of_device_id
197 *i2c_of_match_device(const struct of_device_id *matches,
198 struct i2c_client *client)
200 const struct of_device_id *match;
202 if (!(client && matches))
205 match = of_match_device(matches, &client->dev);
209 return i2c_of_match_device_sysfs(matches, client);
211 EXPORT_SYMBOL_GPL(i2c_of_match_device);
213 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
214 static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
217 struct of_reconfig_data *rd = arg;
218 struct i2c_adapter *adap;
219 struct i2c_client *client;
221 switch (of_reconfig_get_state_change(action, rd)) {
222 case OF_RECONFIG_CHANGE_ADD:
223 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
225 return NOTIFY_OK; /* not for us */
227 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
228 put_device(&adap->dev);
232 client = of_i2c_register_device(adap, rd->dn);
233 put_device(&adap->dev);
235 if (IS_ERR(client)) {
236 dev_err(&adap->dev, "failed to create client for '%pOF'\n",
238 of_node_clear_flag(rd->dn, OF_POPULATED);
239 return notifier_from_errno(PTR_ERR(client));
242 case OF_RECONFIG_CHANGE_REMOVE:
243 /* already depopulated? */
244 if (!of_node_check_flag(rd->dn, OF_POPULATED))
247 /* find our device by node */
248 client = of_find_i2c_device_by_node(rd->dn);
250 return NOTIFY_OK; /* no? not meant for us */
252 /* unregister takes one ref away */
253 i2c_unregister_device(client);
255 /* and put the reference of the find */
256 put_device(&client->dev);
263 struct notifier_block i2c_of_notifier = {
264 .notifier_call = of_i2c_notify,
266 #endif /* CONFIG_OF_DYNAMIC */