1/*
2 * Linux I2C core OF support code
3 *
4 * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de>
5 * based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
6 *
7 * Copyright (C) 2013, 2018 Wolfram Sang <wsa@the-dreams.de>
8 *
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)
12 * any later version.
13 */
14
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>
20#include <linux/of.h>
21#include <linux/of_device.h>
22
23#include "i2c-core.h"
24
25int of_i2c_get_board_info(struct device *dev, struct device_node *node,
26 struct i2c_board_info *info)
27{
28 u32 addr;
29 int ret;
30
31 memset(info, 0, sizeof(*info));
32
33 if (of_modalias_node(node, info->type, sizeof(info->type)) < 0) {
34 dev_err(dev, "of_i2c: modalias failure on %pOF\n", node);
35 return -EINVAL;
36 }
37
38 ret = of_property_read_u32(node, "reg", &addr);
39 if (ret) {
40 dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
41 return ret;
42 }
43
44 if (addr & I2C_TEN_BIT_ADDRESS) {
45 addr &= ~I2C_TEN_BIT_ADDRESS;
46 info->flags |= I2C_CLIENT_TEN;
47 }
48
49 if (addr & I2C_OWN_SLAVE_ADDRESS) {
50 addr &= ~I2C_OWN_SLAVE_ADDRESS;
51 info->flags |= I2C_CLIENT_SLAVE;
52 }
53
54 info->addr = addr;
55 info->of_node = node;
56
57 if (of_property_read_bool(node, "host-notify"))
58 info->flags |= I2C_CLIENT_HOST_NOTIFY;
59
60 if (of_get_property(node, "wakeup-source", NULL))
61 info->flags |= I2C_CLIENT_WAKE;
62
63 return 0;
64}
65EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
66
67static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
68 struct device_node *node)
69{
70 struct i2c_client *client;
71 struct i2c_board_info info;
72 int ret;
73
74 dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
75
76 ret = of_i2c_get_board_info(&adap->dev, node, &info);
77 if (ret)
78 return ERR_PTR(ret);
79
80 client = i2c_new_device(adap, &info);
81 if (!client) {
82 dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
83 return ERR_PTR(-EINVAL);
84 }
85 return client;
86}
87
88void of_i2c_register_devices(struct i2c_adapter *adap)
89{
90 struct device_node *bus, *node;
91 struct i2c_client *client;
92
93 /* Only register child devices if the adapter has a node pointer set */
94 if (!adap->dev.of_node)
95 return;
96
97 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
98
99 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
100 if (!bus)
101 bus = of_node_get(adap->dev.of_node);
102
103 for_each_available_child_of_node(bus, node) {
104 if (of_node_test_and_set_flag(node, OF_POPULATED))
105 continue;
106
107 client = of_i2c_register_device(adap, node);
108 if (IS_ERR(client)) {
109 dev_err(&adap->dev,
110 "Failed to create I2C device for %pOF\n",
111 node);
112 of_node_clear_flag(node, OF_POPULATED);
113 }
114 }
115
116 of_node_put(bus);
117}
118
119static int of_dev_node_match(struct device *dev, void *data)
120{
121 return dev->of_node == data;
122}
123
124static int of_dev_or_parent_node_match(struct device *dev, void *data)
125{
126 if (dev->of_node == data)
127 return 1;
128
129 if (dev->parent)
130 return dev->parent->of_node == data;
131
132 return 0;
133}
134
135/* must call put_device() when done with returned i2c_client device */
136struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
137{
138 struct device *dev;
139 struct i2c_client *client;
140
141 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
142 if (!dev)
143 return NULL;
144
145 client = i2c_verify_client(dev);
146 if (!client)
147 put_device(dev);
148
149 return client;
150}
151EXPORT_SYMBOL(of_find_i2c_device_by_node);
152
153/* must call put_device() when done with returned i2c_adapter device */
154struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
155{
156 struct device *dev;
157 struct i2c_adapter *adapter;
158
159 dev = bus_find_device(&i2c_bus_type, NULL, node,
160 of_dev_or_parent_node_match);
161 if (!dev)
162 return NULL;
163
164 adapter = i2c_verify_adapter(dev);
165 if (!adapter)
166 put_device(dev);
167
168 return adapter;
169}
170EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
171
172/* must call i2c_put_adapter() when done with returned i2c_adapter device */
173struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
174{
175 struct i2c_adapter *adapter;
176
177 adapter = of_find_i2c_adapter_by_node(node);
178 if (!adapter)
179 return NULL;
180
181 if (!try_module_get(adapter->owner)) {
182 put_device(&adapter->dev);
183 adapter = NULL;
184 }
185
186 return adapter;
187}
188EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
189
190static const struct of_device_id*
191i2c_of_match_device_sysfs(const struct of_device_id *matches,
192 struct i2c_client *client)
193{
194 const char *name;
195
196 for (; matches->compatible[0]; matches++) {
197 /*
198 * Adding devices through the i2c sysfs interface provides us
199 * a string to match which may be compatible with the device
200 * tree compatible strings, however with no actual of_node the
201 * of_match_device() will not match
202 */
203 if (sysfs_streq(client->name, matches->compatible))
204 return matches;
205
206 name = strchr(matches->compatible, ',');
207 if (!name)
208 name = matches->compatible;
209 else
210 name++;
211
212 if (sysfs_streq(client->name, name))
213 return matches;
214 }
215
216 return NULL;
217}
218
219const struct of_device_id
220*i2c_of_match_device(const struct of_device_id *matches,
221 struct i2c_client *client)
222{
223 const struct of_device_id *match;
224
225 if (!(client && matches))
226 return NULL;
227
228 match = of_match_device(matches, &client->dev);
229 if (match)
230 return match;
231
232 return i2c_of_match_device_sysfs(matches, client);
233}
234EXPORT_SYMBOL_GPL(i2c_of_match_device);
235
236#if IS_ENABLED(CONFIG_OF_DYNAMIC)
237static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
238 void *arg)
239{
240 struct of_reconfig_data *rd = arg;
241 struct i2c_adapter *adap;
242 struct i2c_client *client;
243
244 switch (of_reconfig_get_state_change(action, rd)) {
245 case OF_RECONFIG_CHANGE_ADD:
246 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
247 if (adap == NULL)
248 return NOTIFY_OK; /* not for us */
249
250 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
251 put_device(&adap->dev);
252 return NOTIFY_OK;
253 }
254
255 client = of_i2c_register_device(adap, rd->dn);
256 put_device(&adap->dev);
257
258 if (IS_ERR(client)) {
259 dev_err(&adap->dev, "failed to create client for '%pOF'\n",
260 rd->dn);
261 of_node_clear_flag(rd->dn, OF_POPULATED);
262 return notifier_from_errno(PTR_ERR(client));
263 }
264 break;
265 case OF_RECONFIG_CHANGE_REMOVE:
266 /* already depopulated? */
267 if (!of_node_check_flag(rd->dn, OF_POPULATED))
268 return NOTIFY_OK;
269
270 /* find our device by node */
271 client = of_find_i2c_device_by_node(rd->dn);
272 if (client == NULL)
273 return NOTIFY_OK; /* no? not meant for us */
274
275 /* unregister takes one ref away */
276 i2c_unregister_device(client);
277
278 /* and put the reference of the find */
279 put_device(&client->dev);
280 break;
281 }
282
283 return NOTIFY_OK;
284}
285
286struct notifier_block i2c_of_notifier = {
287 .notifier_call = of_i2c_notify,
288};
289#endif /* CONFIG_OF_DYNAMIC */
290