blob: d1c48dec7118e798d15db3ed653e5c2c83e828e5 [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>
Wolfram Sanga8023e62019-06-19 19:02:45 +020018#include <linux/sysfs.h>
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020019
20#include "i2c-core.h"
21
Boris Brezillonda0086d2018-03-25 14:49:03 +020022int of_i2c_get_board_info(struct device *dev, struct device_node *node,
23 struct i2c_board_info *info)
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020024{
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020025 u32 addr;
Wolfram Sanga1671af2018-01-18 13:11:33 +010026 int ret;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020027
Boris Brezillonda0086d2018-03-25 14:49:03 +020028 memset(info, 0, sizeof(*info));
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020029
Boris Brezillonda0086d2018-03-25 14:49:03 +020030 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;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020033 }
34
Wolfram Sanga1671af2018-01-18 13:11:33 +010035 ret = of_property_read_u32(node, "reg", &addr);
36 if (ret) {
Boris Brezillonda0086d2018-03-25 14:49:03 +020037 dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
38 return ret;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020039 }
40
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020041 if (addr & I2C_TEN_BIT_ADDRESS) {
42 addr &= ~I2C_TEN_BIT_ADDRESS;
Boris Brezillonda0086d2018-03-25 14:49:03 +020043 info->flags |= I2C_CLIENT_TEN;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020044 }
45
46 if (addr & I2C_OWN_SLAVE_ADDRESS) {
47 addr &= ~I2C_OWN_SLAVE_ADDRESS;
Boris Brezillonda0086d2018-03-25 14:49:03 +020048 info->flags |= I2C_CLIENT_SLAVE;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020049 }
50
Boris Brezillonda0086d2018-03-25 14:49:03 +020051 info->addr = addr;
52 info->of_node = node;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020053
54 if (of_property_read_bool(node, "host-notify"))
Boris Brezillonda0086d2018-03-25 14:49:03 +020055 info->flags |= I2C_CLIENT_HOST_NOTIFY;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020056
57 if (of_get_property(node, "wakeup-source", NULL))
Boris Brezillonda0086d2018-03-25 14:49:03 +020058 info->flags |= I2C_CLIENT_WAKE;
59
60 return 0;
61}
62EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
63
64static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
65 struct device_node *node)
66{
67 struct i2c_client *client;
68 struct i2c_board_info info;
69 int ret;
70
71 dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
72
73 ret = of_i2c_get_board_info(&adap->dev, node, &info);
74 if (ret)
75 return ERR_PTR(ret);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020076
Wolfram Sangc49b0e02018-01-18 13:11:31 +010077 client = i2c_new_device(adap, &info);
78 if (!client) {
Rob Herring453a2372017-07-18 16:43:06 -050079 dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020080 return ERR_PTR(-EINVAL);
81 }
Wolfram Sangc49b0e02018-01-18 13:11:31 +010082 return client;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020083}
84
85void of_i2c_register_devices(struct i2c_adapter *adap)
86{
87 struct device_node *bus, *node;
88 struct i2c_client *client;
89
90 /* Only register child devices if the adapter has a node pointer set */
91 if (!adap->dev.of_node)
92 return;
93
94 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
95
96 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
97 if (!bus)
98 bus = of_node_get(adap->dev.of_node);
99
100 for_each_available_child_of_node(bus, node) {
101 if (of_node_test_and_set_flag(node, OF_POPULATED))
102 continue;
103
104 client = of_i2c_register_device(adap, node);
105 if (IS_ERR(client)) {
Wolfram Sangf1c87ce2018-01-18 13:11:29 +0100106 dev_err(&adap->dev,
Rob Herring453a2372017-07-18 16:43:06 -0500107 "Failed to create I2C device for %pOF\n",
108 node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200109 of_node_clear_flag(node, OF_POPULATED);
110 }
111 }
112
113 of_node_put(bus);
114}
115
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +0100116static int of_dev_node_match(struct device *dev, const void *data)
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200117{
118 return dev->of_node == data;
119}
120
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +0100121static int of_dev_or_parent_node_match(struct device *dev, const void *data)
Thierry Redinge814e682019-01-25 14:11:42 +0100122{
123 if (dev->of_node == data)
124 return 1;
125
126 if (dev->parent)
127 return dev->parent->of_node == data;
128
129 return 0;
130}
131
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200132/* must call put_device() when done with returned i2c_client device */
133struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
134{
135 struct device *dev;
136 struct i2c_client *client;
137
138 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
139 if (!dev)
140 return NULL;
141
142 client = i2c_verify_client(dev);
143 if (!client)
144 put_device(dev);
145
146 return client;
147}
148EXPORT_SYMBOL(of_find_i2c_device_by_node);
149
150/* must call put_device() when done with returned i2c_adapter device */
151struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
152{
153 struct device *dev;
154 struct i2c_adapter *adapter;
155
Thierry Redinge814e682019-01-25 14:11:42 +0100156 dev = bus_find_device(&i2c_bus_type, NULL, node,
157 of_dev_or_parent_node_match);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200158 if (!dev)
159 return NULL;
160
161 adapter = i2c_verify_adapter(dev);
162 if (!adapter)
163 put_device(dev);
164
165 return adapter;
166}
167EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
168
169/* must call i2c_put_adapter() when done with returned i2c_adapter device */
170struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
171{
172 struct i2c_adapter *adapter;
173
174 adapter = of_find_i2c_adapter_by_node(node);
175 if (!adapter)
176 return NULL;
177
178 if (!try_module_get(adapter->owner)) {
179 put_device(&adapter->dev);
180 adapter = NULL;
181 }
182
183 return adapter;
184}
185EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
186
187static const struct of_device_id*
188i2c_of_match_device_sysfs(const struct of_device_id *matches,
189 struct i2c_client *client)
190{
191 const char *name;
192
193 for (; matches->compatible[0]; matches++) {
194 /*
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
199 */
200 if (sysfs_streq(client->name, matches->compatible))
201 return matches;
202
203 name = strchr(matches->compatible, ',');
204 if (!name)
205 name = matches->compatible;
206 else
207 name++;
208
209 if (sysfs_streq(client->name, name))
210 return matches;
211 }
212
213 return NULL;
214}
215
216const struct of_device_id
217*i2c_of_match_device(const struct of_device_id *matches,
218 struct i2c_client *client)
219{
220 const struct of_device_id *match;
221
222 if (!(client && matches))
223 return NULL;
224
225 match = of_match_device(matches, &client->dev);
226 if (match)
227 return match;
228
229 return i2c_of_match_device_sysfs(matches, client);
230}
231EXPORT_SYMBOL_GPL(i2c_of_match_device);
232
233#if IS_ENABLED(CONFIG_OF_DYNAMIC)
234static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
235 void *arg)
236{
237 struct of_reconfig_data *rd = arg;
238 struct i2c_adapter *adap;
239 struct i2c_client *client;
240
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);
244 if (adap == NULL)
245 return NOTIFY_OK; /* not for us */
246
247 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
248 put_device(&adap->dev);
249 return NOTIFY_OK;
250 }
251
252 client = of_i2c_register_device(adap, rd->dn);
253 put_device(&adap->dev);
254
255 if (IS_ERR(client)) {
Rob Herring453a2372017-07-18 16:43:06 -0500256 dev_err(&adap->dev, "failed to create client for '%pOF'\n",
257 rd->dn);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200258 of_node_clear_flag(rd->dn, OF_POPULATED);
259 return notifier_from_errno(PTR_ERR(client));
260 }
261 break;
262 case OF_RECONFIG_CHANGE_REMOVE:
263 /* already depopulated? */
264 if (!of_node_check_flag(rd->dn, OF_POPULATED))
265 return NOTIFY_OK;
266
267 /* find our device by node */
268 client = of_find_i2c_device_by_node(rd->dn);
269 if (client == NULL)
270 return NOTIFY_OK; /* no? not meant for us */
271
272 /* unregister takes one ref away */
273 i2c_unregister_device(client);
274
275 /* and put the reference of the find */
276 put_device(&client->dev);
277 break;
278 }
279
280 return NOTIFY_OK;
281}
282
283struct notifier_block i2c_of_notifier = {
284 .notifier_call = of_i2c_notify,
285};
286#endif /* CONFIG_OF_DYNAMIC */