fix race condition in cor_announce_send_stop
[cor.git] / drivers / i2c / i2c-core-of.c
blobe4d296b40baae48019425bb1a8999c7bf7727833
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Linux I2C core OF support code
5 * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de>
6 * based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
8 * Copyright (C) 2013, 2018 Wolfram Sang <wsa@the-dreams.de>
9 */
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>
20 #include "i2c-core.h"
22 int of_i2c_get_board_info(struct device *dev, struct device_node *node,
23 struct i2c_board_info *info)
25 u32 addr;
26 int ret;
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);
32 return -EINVAL;
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;
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;
51 info->addr = addr;
52 info->of_node = node;
53 info->fwnode = of_fwnode_handle(node);
55 if (of_property_read_bool(node, "host-notify"))
56 info->flags |= I2C_CLIENT_HOST_NOTIFY;
58 if (of_get_property(node, "wakeup-source", NULL))
59 info->flags |= I2C_CLIENT_WAKE;
61 return 0;
63 EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
65 static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
66 struct device_node *node)
68 struct i2c_client *client;
69 struct i2c_board_info info;
70 int ret;
72 dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
74 ret = of_i2c_get_board_info(&adap->dev, node, &info);
75 if (ret)
76 return ERR_PTR(ret);
78 client = i2c_new_device(adap, &info);
79 if (!client) {
80 dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
81 return ERR_PTR(-EINVAL);
83 return client;
86 void of_i2c_register_devices(struct i2c_adapter *adap)
88 struct device_node *bus, *node;
89 struct i2c_client *client;
91 /* Only register child devices if the adapter has a node pointer set */
92 if (!adap->dev.of_node)
93 return;
95 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
97 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
98 if (!bus)
99 bus = of_node_get(adap->dev.of_node);
101 for_each_available_child_of_node(bus, node) {
102 if (of_node_test_and_set_flag(node, OF_POPULATED))
103 continue;
105 client = of_i2c_register_device(adap, node);
106 if (IS_ERR(client)) {
107 dev_err(&adap->dev,
108 "Failed to create I2C device for %pOF\n",
109 node);
110 of_node_clear_flag(node, OF_POPULATED);
114 of_node_put(bus);
117 static int of_dev_or_parent_node_match(struct device *dev, const void *data)
119 if (dev->of_node == data)
120 return 1;
122 if (dev->parent)
123 return dev->parent->of_node == data;
125 return 0;
128 /* must call put_device() when done with returned i2c_client device */
129 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
131 struct device *dev;
132 struct i2c_client *client;
134 dev = bus_find_device_by_of_node(&i2c_bus_type, node);
135 if (!dev)
136 return NULL;
138 client = i2c_verify_client(dev);
139 if (!client)
140 put_device(dev);
142 return client;
144 EXPORT_SYMBOL(of_find_i2c_device_by_node);
146 /* must call put_device() when done with returned i2c_adapter device */
147 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
149 struct device *dev;
150 struct i2c_adapter *adapter;
152 dev = bus_find_device(&i2c_bus_type, NULL, node,
153 of_dev_or_parent_node_match);
154 if (!dev)
155 return NULL;
157 adapter = i2c_verify_adapter(dev);
158 if (!adapter)
159 put_device(dev);
161 return adapter;
163 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
165 /* must call i2c_put_adapter() when done with returned i2c_adapter device */
166 struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
168 struct i2c_adapter *adapter;
170 adapter = of_find_i2c_adapter_by_node(node);
171 if (!adapter)
172 return NULL;
174 if (!try_module_get(adapter->owner)) {
175 put_device(&adapter->dev);
176 adapter = NULL;
179 return adapter;
181 EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
183 static const struct of_device_id*
184 i2c_of_match_device_sysfs(const struct of_device_id *matches,
185 struct i2c_client *client)
187 const char *name;
189 for (; matches->compatible[0]; matches++) {
191 * Adding devices through the i2c sysfs interface provides us
192 * a string to match which may be compatible with the device
193 * tree compatible strings, however with no actual of_node the
194 * of_match_device() will not match
196 if (sysfs_streq(client->name, matches->compatible))
197 return matches;
199 name = strchr(matches->compatible, ',');
200 if (!name)
201 name = matches->compatible;
202 else
203 name++;
205 if (sysfs_streq(client->name, name))
206 return matches;
209 return NULL;
212 const struct of_device_id
213 *i2c_of_match_device(const struct of_device_id *matches,
214 struct i2c_client *client)
216 const struct of_device_id *match;
218 if (!(client && matches))
219 return NULL;
221 match = of_match_device(matches, &client->dev);
222 if (match)
223 return match;
225 return i2c_of_match_device_sysfs(matches, client);
227 EXPORT_SYMBOL_GPL(i2c_of_match_device);
229 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
230 static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
231 void *arg)
233 struct of_reconfig_data *rd = arg;
234 struct i2c_adapter *adap;
235 struct i2c_client *client;
237 switch (of_reconfig_get_state_change(action, rd)) {
238 case OF_RECONFIG_CHANGE_ADD:
239 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
240 if (adap == NULL)
241 return NOTIFY_OK; /* not for us */
243 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
244 put_device(&adap->dev);
245 return NOTIFY_OK;
248 client = of_i2c_register_device(adap, rd->dn);
249 if (IS_ERR(client)) {
250 dev_err(&adap->dev, "failed to create client for '%pOF'\n",
251 rd->dn);
252 put_device(&adap->dev);
253 of_node_clear_flag(rd->dn, OF_POPULATED);
254 return notifier_from_errno(PTR_ERR(client));
256 put_device(&adap->dev);
257 break;
258 case OF_RECONFIG_CHANGE_REMOVE:
259 /* already depopulated? */
260 if (!of_node_check_flag(rd->dn, OF_POPULATED))
261 return NOTIFY_OK;
263 /* find our device by node */
264 client = of_find_i2c_device_by_node(rd->dn);
265 if (client == NULL)
266 return NOTIFY_OK; /* no? not meant for us */
268 /* unregister takes one ref away */
269 i2c_unregister_device(client);
271 /* and put the reference of the find */
272 put_device(&client->dev);
273 break;
276 return NOTIFY_OK;
279 struct notifier_block i2c_of_notifier = {
280 .notifier_call = of_i2c_notify,
282 #endif /* CONFIG_OF_DYNAMIC */