blob: 6cb7ad608bcd53d9c966752a342562208aa7617e [file] [log] [blame]
Wolfram Sang5bf4fa72017-05-23 11:50:58 +02001/*
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 *
Wolfram Sanga1671af2018-01-18 13:11:33 +01007 * Copyright (C) 2013, 2018 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang5bf4fa72017-05-23 11:50:58 +02008 *
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
Boris Brezillonda0086d2018-03-25 14:49:03 +020025int of_i2c_get_board_info(struct device *dev, struct device_node *node,
26 struct i2c_board_info *info)
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020027{
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020028 u32 addr;
Wolfram Sanga1671af2018-01-18 13:11:33 +010029 int ret;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020030
Boris Brezillonda0086d2018-03-25 14:49:03 +020031 memset(info, 0, sizeof(*info));
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020032
Boris Brezillonda0086d2018-03-25 14:49:03 +020033 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;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020036 }
37
Wolfram Sanga1671af2018-01-18 13:11:33 +010038 ret = of_property_read_u32(node, "reg", &addr);
39 if (ret) {
Boris Brezillonda0086d2018-03-25 14:49:03 +020040 dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
41 return ret;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020042 }
43
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020044 if (addr & I2C_TEN_BIT_ADDRESS) {
45 addr &= ~I2C_TEN_BIT_ADDRESS;
Boris Brezillonda0086d2018-03-25 14:49:03 +020046 info->flags |= I2C_CLIENT_TEN;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020047 }
48
49 if (addr & I2C_OWN_SLAVE_ADDRESS) {
50 addr &= ~I2C_OWN_SLAVE_ADDRESS;
Boris Brezillonda0086d2018-03-25 14:49:03 +020051 info->flags |= I2C_CLIENT_SLAVE;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020052 }
53
Boris Brezillonda0086d2018-03-25 14:49:03 +020054 info->addr = addr;
55 info->of_node = node;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020056
57 if (of_property_read_bool(node, "host-notify"))
Boris Brezillonda0086d2018-03-25 14:49:03 +020058 info->flags |= I2C_CLIENT_HOST_NOTIFY;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020059
60 if (of_get_property(node, "wakeup-source", NULL))
Boris Brezillonda0086d2018-03-25 14:49:03 +020061 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);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020079
Wolfram Sangc49b0e02018-01-18 13:11:31 +010080 client = i2c_new_device(adap, &info);
81 if (!client) {
Rob Herring453a2372017-07-18 16:43:06 -050082 dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020083 return ERR_PTR(-EINVAL);
84 }
Wolfram Sangc49b0e02018-01-18 13:11:31 +010085 return client;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020086}
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)) {
Wolfram Sangf1c87ce2018-01-18 13:11:29 +0100109 dev_err(&adap->dev,
Rob Herring453a2372017-07-18 16:43:06 -0500110 "Failed to create I2C device for %pOF\n",
111 node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200112 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
124/* must call put_device() when done with returned i2c_client device */
125struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
126{
127 struct device *dev;
128 struct i2c_client *client;
129
130 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
131 if (!dev)
132 return NULL;
133
134 client = i2c_verify_client(dev);
135 if (!client)
136 put_device(dev);
137
138 return client;
139}
140EXPORT_SYMBOL(of_find_i2c_device_by_node);
141
142/* must call put_device() when done with returned i2c_adapter device */
143struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
144{
145 struct device *dev;
146 struct i2c_adapter *adapter;
147
148 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
149 if (!dev)
150 return NULL;
151
152 adapter = i2c_verify_adapter(dev);
153 if (!adapter)
154 put_device(dev);
155
156 return adapter;
157}
158EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
159
160/* must call i2c_put_adapter() when done with returned i2c_adapter device */
161struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
162{
163 struct i2c_adapter *adapter;
164
165 adapter = of_find_i2c_adapter_by_node(node);
166 if (!adapter)
167 return NULL;
168
169 if (!try_module_get(adapter->owner)) {
170 put_device(&adapter->dev);
171 adapter = NULL;
172 }
173
174 return adapter;
175}
176EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
177
178static const struct of_device_id*
179i2c_of_match_device_sysfs(const struct of_device_id *matches,
180 struct i2c_client *client)
181{
182 const char *name;
183
184 for (; matches->compatible[0]; matches++) {
185 /*
186 * Adding devices through the i2c sysfs interface provides us
187 * a string to match which may be compatible with the device
188 * tree compatible strings, however with no actual of_node the
189 * of_match_device() will not match
190 */
191 if (sysfs_streq(client->name, matches->compatible))
192 return matches;
193
194 name = strchr(matches->compatible, ',');
195 if (!name)
196 name = matches->compatible;
197 else
198 name++;
199
200 if (sysfs_streq(client->name, name))
201 return matches;
202 }
203
204 return NULL;
205}
206
207const struct of_device_id
208*i2c_of_match_device(const struct of_device_id *matches,
209 struct i2c_client *client)
210{
211 const struct of_device_id *match;
212
213 if (!(client && matches))
214 return NULL;
215
216 match = of_match_device(matches, &client->dev);
217 if (match)
218 return match;
219
220 return i2c_of_match_device_sysfs(matches, client);
221}
222EXPORT_SYMBOL_GPL(i2c_of_match_device);
223
224#if IS_ENABLED(CONFIG_OF_DYNAMIC)
225static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
226 void *arg)
227{
228 struct of_reconfig_data *rd = arg;
229 struct i2c_adapter *adap;
230 struct i2c_client *client;
231
232 switch (of_reconfig_get_state_change(action, rd)) {
233 case OF_RECONFIG_CHANGE_ADD:
234 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
235 if (adap == NULL)
236 return NOTIFY_OK; /* not for us */
237
238 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
239 put_device(&adap->dev);
240 return NOTIFY_OK;
241 }
242
243 client = of_i2c_register_device(adap, rd->dn);
244 put_device(&adap->dev);
245
246 if (IS_ERR(client)) {
Rob Herring453a2372017-07-18 16:43:06 -0500247 dev_err(&adap->dev, "failed to create client for '%pOF'\n",
248 rd->dn);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200249 of_node_clear_flag(rd->dn, OF_POPULATED);
250 return notifier_from_errno(PTR_ERR(client));
251 }
252 break;
253 case OF_RECONFIG_CHANGE_REMOVE:
254 /* already depopulated? */
255 if (!of_node_check_flag(rd->dn, OF_POPULATED))
256 return NOTIFY_OK;
257
258 /* find our device by node */
259 client = of_find_i2c_device_by_node(rd->dn);
260 if (client == NULL)
261 return NOTIFY_OK; /* no? not meant for us */
262
263 /* unregister takes one ref away */
264 i2c_unregister_device(client);
265
266 /* and put the reference of the find */
267 put_device(&client->dev);
268 break;
269 }
270
271 return NOTIFY_OK;
272}
273
274struct notifier_block i2c_of_notifier = {
275 .notifier_call = of_i2c_notify,
276};
277#endif /* CONFIG_OF_DYNAMIC */