blob: 406e5f695a7ecb6b832ae29c3b30258afaef8fe6 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Wolfram Sang5bf4fa72017-05-23 11:50:58 +02002/*
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 *
Wolfram Sanga1671af2018-01-18 13:11:33 +01008 * Copyright (C) 2013, 2018 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang5bf4fa72017-05-23 11:50:58 +02009 */
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
19#include "i2c-core.h"
20
Boris Brezillonda0086d2018-03-25 14:49:03 +020021int of_i2c_get_board_info(struct device *dev, struct device_node *node,
22 struct i2c_board_info *info)
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020023{
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020024 u32 addr;
Wolfram Sanga1671af2018-01-18 13:11:33 +010025 int ret;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020026
Boris Brezillonda0086d2018-03-25 14:49:03 +020027 memset(info, 0, sizeof(*info));
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020028
Boris Brezillonda0086d2018-03-25 14:49:03 +020029 if (of_modalias_node(node, info->type, sizeof(info->type)) < 0) {
30 dev_err(dev, "of_i2c: modalias failure on %pOF\n", node);
31 return -EINVAL;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020032 }
33
Wolfram Sanga1671af2018-01-18 13:11:33 +010034 ret = of_property_read_u32(node, "reg", &addr);
35 if (ret) {
Boris Brezillonda0086d2018-03-25 14:49:03 +020036 dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
37 return ret;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020038 }
39
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020040 if (addr & I2C_TEN_BIT_ADDRESS) {
41 addr &= ~I2C_TEN_BIT_ADDRESS;
Boris Brezillonda0086d2018-03-25 14:49:03 +020042 info->flags |= I2C_CLIENT_TEN;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020043 }
44
45 if (addr & I2C_OWN_SLAVE_ADDRESS) {
46 addr &= ~I2C_OWN_SLAVE_ADDRESS;
Boris Brezillonda0086d2018-03-25 14:49:03 +020047 info->flags |= I2C_CLIENT_SLAVE;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020048 }
49
Boris Brezillonda0086d2018-03-25 14:49:03 +020050 info->addr = addr;
51 info->of_node = node;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020052
53 if (of_property_read_bool(node, "host-notify"))
Boris Brezillonda0086d2018-03-25 14:49:03 +020054 info->flags |= I2C_CLIENT_HOST_NOTIFY;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020055
56 if (of_get_property(node, "wakeup-source", NULL))
Boris Brezillonda0086d2018-03-25 14:49:03 +020057 info->flags |= I2C_CLIENT_WAKE;
58
59 return 0;
60}
61EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
62
63static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
64 struct device_node *node)
65{
66 struct i2c_client *client;
67 struct i2c_board_info info;
68 int ret;
69
70 dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
71
72 ret = of_i2c_get_board_info(&adap->dev, node, &info);
73 if (ret)
74 return ERR_PTR(ret);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020075
Wolfram Sangc49b0e02018-01-18 13:11:31 +010076 client = i2c_new_device(adap, &info);
77 if (!client) {
Rob Herring453a2372017-07-18 16:43:06 -050078 dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020079 return ERR_PTR(-EINVAL);
80 }
Wolfram Sangc49b0e02018-01-18 13:11:31 +010081 return client;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020082}
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)) {
Wolfram Sangf1c87ce2018-01-18 13:11:29 +0100105 dev_err(&adap->dev,
Rob Herring453a2372017-07-18 16:43:06 -0500106 "Failed to create I2C device for %pOF\n",
107 node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200108 of_node_clear_flag(node, OF_POPULATED);
109 }
110 }
111
112 of_node_put(bus);
113}
114
115static int of_dev_node_match(struct device *dev, void *data)
116{
117 return dev->of_node == data;
118}
119
Thierry Redinge814e682019-01-25 14:11:42 +0100120static int of_dev_or_parent_node_match(struct device *dev, void *data)
121{
122 if (dev->of_node == data)
123 return 1;
124
125 if (dev->parent)
126 return dev->parent->of_node == data;
127
128 return 0;
129}
130
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200131/* must call put_device() when done with returned i2c_client device */
132struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
133{
134 struct device *dev;
135 struct i2c_client *client;
136
137 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
138 if (!dev)
139 return NULL;
140
141 client = i2c_verify_client(dev);
142 if (!client)
143 put_device(dev);
144
145 return client;
146}
147EXPORT_SYMBOL(of_find_i2c_device_by_node);
148
149/* must call put_device() when done with returned i2c_adapter device */
150struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
151{
152 struct device *dev;
153 struct i2c_adapter *adapter;
154
Thierry Redinge814e682019-01-25 14:11:42 +0100155 dev = bus_find_device(&i2c_bus_type, NULL, node,
156 of_dev_or_parent_node_match);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200157 if (!dev)
158 return NULL;
159
160 adapter = i2c_verify_adapter(dev);
161 if (!adapter)
162 put_device(dev);
163
164 return adapter;
165}
166EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
167
168/* must call i2c_put_adapter() when done with returned i2c_adapter device */
169struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
170{
171 struct i2c_adapter *adapter;
172
173 adapter = of_find_i2c_adapter_by_node(node);
174 if (!adapter)
175 return NULL;
176
177 if (!try_module_get(adapter->owner)) {
178 put_device(&adapter->dev);
179 adapter = NULL;
180 }
181
182 return adapter;
183}
184EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
185
186static const struct of_device_id*
187i2c_of_match_device_sysfs(const struct of_device_id *matches,
188 struct i2c_client *client)
189{
190 const char *name;
191
192 for (; matches->compatible[0]; matches++) {
193 /*
194 * Adding devices through the i2c sysfs interface provides us
195 * a string to match which may be compatible with the device
196 * tree compatible strings, however with no actual of_node the
197 * of_match_device() will not match
198 */
199 if (sysfs_streq(client->name, matches->compatible))
200 return matches;
201
202 name = strchr(matches->compatible, ',');
203 if (!name)
204 name = matches->compatible;
205 else
206 name++;
207
208 if (sysfs_streq(client->name, name))
209 return matches;
210 }
211
212 return NULL;
213}
214
215const struct of_device_id
216*i2c_of_match_device(const struct of_device_id *matches,
217 struct i2c_client *client)
218{
219 const struct of_device_id *match;
220
221 if (!(client && matches))
222 return NULL;
223
224 match = of_match_device(matches, &client->dev);
225 if (match)
226 return match;
227
228 return i2c_of_match_device_sysfs(matches, client);
229}
230EXPORT_SYMBOL_GPL(i2c_of_match_device);
231
232#if IS_ENABLED(CONFIG_OF_DYNAMIC)
233static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
234 void *arg)
235{
236 struct of_reconfig_data *rd = arg;
237 struct i2c_adapter *adap;
238 struct i2c_client *client;
239
240 switch (of_reconfig_get_state_change(action, rd)) {
241 case OF_RECONFIG_CHANGE_ADD:
242 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
243 if (adap == NULL)
244 return NOTIFY_OK; /* not for us */
245
246 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
247 put_device(&adap->dev);
248 return NOTIFY_OK;
249 }
250
251 client = of_i2c_register_device(adap, rd->dn);
252 put_device(&adap->dev);
253
254 if (IS_ERR(client)) {
Rob Herring453a2372017-07-18 16:43:06 -0500255 dev_err(&adap->dev, "failed to create client for '%pOF'\n",
256 rd->dn);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200257 of_node_clear_flag(rd->dn, OF_POPULATED);
258 return notifier_from_errno(PTR_ERR(client));
259 }
260 break;
261 case OF_RECONFIG_CHANGE_REMOVE:
262 /* already depopulated? */
263 if (!of_node_check_flag(rd->dn, OF_POPULATED))
264 return NOTIFY_OK;
265
266 /* find our device by node */
267 client = of_find_i2c_device_by_node(rd->dn);
268 if (client == NULL)
269 return NOTIFY_OK; /* no? not meant for us */
270
271 /* unregister takes one ref away */
272 i2c_unregister_device(client);
273
274 /* and put the reference of the find */
275 put_device(&client->dev);
276 break;
277 }
278
279 return NOTIFY_OK;
280}
281
282struct notifier_block i2c_of_notifier = {
283 .notifier_call = of_i2c_notify,
284};
285#endif /* CONFIG_OF_DYNAMIC */