blob: bbfff3f61b1fdb0422a9fda2059e24260e4b3aaf [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 *
7 * Copyright (C) 2013 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
25static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
26 struct device_node *node)
27{
Wolfram Sangc49b0e02018-01-18 13:11:31 +010028 struct i2c_client *client;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020029 struct i2c_board_info info = {};
30 struct dev_archdata dev_ad = {};
31 const __be32 *addr_be;
32 u32 addr;
33 int len;
34
Rob Herring453a2372017-07-18 16:43:06 -050035 dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020036
37 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
Rob Herring453a2372017-07-18 16:43:06 -050038 dev_err(&adap->dev, "of_i2c: modalias failure on %pOF\n",
39 node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020040 return ERR_PTR(-EINVAL);
41 }
42
43 addr_be = of_get_property(node, "reg", &len);
44 if (!addr_be || (len < sizeof(*addr_be))) {
Rob Herring453a2372017-07-18 16:43:06 -050045 dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020046 return ERR_PTR(-EINVAL);
47 }
48
49 addr = be32_to_cpup(addr_be);
50 if (addr & I2C_TEN_BIT_ADDRESS) {
51 addr &= ~I2C_TEN_BIT_ADDRESS;
52 info.flags |= I2C_CLIENT_TEN;
53 }
54
55 if (addr & I2C_OWN_SLAVE_ADDRESS) {
56 addr &= ~I2C_OWN_SLAVE_ADDRESS;
57 info.flags |= I2C_CLIENT_SLAVE;
58 }
59
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020060 info.addr = addr;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020061 info.archdata = &dev_ad;
Wolfram Sange6db2d32018-01-18 13:11:30 +010062 info.of_node = of_node_get(node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020063
64 if (of_property_read_bool(node, "host-notify"))
65 info.flags |= I2C_CLIENT_HOST_NOTIFY;
66
67 if (of_get_property(node, "wakeup-source", NULL))
68 info.flags |= I2C_CLIENT_WAKE;
69
Wolfram Sangc49b0e02018-01-18 13:11:31 +010070 client = i2c_new_device(adap, &info);
71 if (!client) {
Rob Herring453a2372017-07-18 16:43:06 -050072 dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020073 of_node_put(node);
74 return ERR_PTR(-EINVAL);
75 }
Wolfram Sangc49b0e02018-01-18 13:11:31 +010076 return client;
Wolfram Sang5bf4fa72017-05-23 11:50:58 +020077}
78
79void of_i2c_register_devices(struct i2c_adapter *adap)
80{
81 struct device_node *bus, *node;
82 struct i2c_client *client;
83
84 /* Only register child devices if the adapter has a node pointer set */
85 if (!adap->dev.of_node)
86 return;
87
88 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
89
90 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
91 if (!bus)
92 bus = of_node_get(adap->dev.of_node);
93
94 for_each_available_child_of_node(bus, node) {
95 if (of_node_test_and_set_flag(node, OF_POPULATED))
96 continue;
97
98 client = of_i2c_register_device(adap, node);
99 if (IS_ERR(client)) {
Wolfram Sangf1c87ce2018-01-18 13:11:29 +0100100 dev_err(&adap->dev,
Rob Herring453a2372017-07-18 16:43:06 -0500101 "Failed to create I2C device for %pOF\n",
102 node);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200103 of_node_clear_flag(node, OF_POPULATED);
104 }
105 }
106
107 of_node_put(bus);
108}
109
110static int of_dev_node_match(struct device *dev, void *data)
111{
112 return dev->of_node == data;
113}
114
115/* must call put_device() when done with returned i2c_client device */
116struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
117{
118 struct device *dev;
119 struct i2c_client *client;
120
121 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
122 if (!dev)
123 return NULL;
124
125 client = i2c_verify_client(dev);
126 if (!client)
127 put_device(dev);
128
129 return client;
130}
131EXPORT_SYMBOL(of_find_i2c_device_by_node);
132
133/* must call put_device() when done with returned i2c_adapter device */
134struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
135{
136 struct device *dev;
137 struct i2c_adapter *adapter;
138
139 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
140 if (!dev)
141 return NULL;
142
143 adapter = i2c_verify_adapter(dev);
144 if (!adapter)
145 put_device(dev);
146
147 return adapter;
148}
149EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
150
151/* must call i2c_put_adapter() when done with returned i2c_adapter device */
152struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
153{
154 struct i2c_adapter *adapter;
155
156 adapter = of_find_i2c_adapter_by_node(node);
157 if (!adapter)
158 return NULL;
159
160 if (!try_module_get(adapter->owner)) {
161 put_device(&adapter->dev);
162 adapter = NULL;
163 }
164
165 return adapter;
166}
167EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
168
169static const struct of_device_id*
170i2c_of_match_device_sysfs(const struct of_device_id *matches,
171 struct i2c_client *client)
172{
173 const char *name;
174
175 for (; matches->compatible[0]; matches++) {
176 /*
177 * Adding devices through the i2c sysfs interface provides us
178 * a string to match which may be compatible with the device
179 * tree compatible strings, however with no actual of_node the
180 * of_match_device() will not match
181 */
182 if (sysfs_streq(client->name, matches->compatible))
183 return matches;
184
185 name = strchr(matches->compatible, ',');
186 if (!name)
187 name = matches->compatible;
188 else
189 name++;
190
191 if (sysfs_streq(client->name, name))
192 return matches;
193 }
194
195 return NULL;
196}
197
198const struct of_device_id
199*i2c_of_match_device(const struct of_device_id *matches,
200 struct i2c_client *client)
201{
202 const struct of_device_id *match;
203
204 if (!(client && matches))
205 return NULL;
206
207 match = of_match_device(matches, &client->dev);
208 if (match)
209 return match;
210
211 return i2c_of_match_device_sysfs(matches, client);
212}
213EXPORT_SYMBOL_GPL(i2c_of_match_device);
214
215#if IS_ENABLED(CONFIG_OF_DYNAMIC)
216static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
217 void *arg)
218{
219 struct of_reconfig_data *rd = arg;
220 struct i2c_adapter *adap;
221 struct i2c_client *client;
222
223 switch (of_reconfig_get_state_change(action, rd)) {
224 case OF_RECONFIG_CHANGE_ADD:
225 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
226 if (adap == NULL)
227 return NOTIFY_OK; /* not for us */
228
229 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
230 put_device(&adap->dev);
231 return NOTIFY_OK;
232 }
233
234 client = of_i2c_register_device(adap, rd->dn);
235 put_device(&adap->dev);
236
237 if (IS_ERR(client)) {
Rob Herring453a2372017-07-18 16:43:06 -0500238 dev_err(&adap->dev, "failed to create client for '%pOF'\n",
239 rd->dn);
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200240 of_node_clear_flag(rd->dn, OF_POPULATED);
241 return notifier_from_errno(PTR_ERR(client));
242 }
243 break;
244 case OF_RECONFIG_CHANGE_REMOVE:
245 /* already depopulated? */
246 if (!of_node_check_flag(rd->dn, OF_POPULATED))
247 return NOTIFY_OK;
248
249 /* find our device by node */
250 client = of_find_i2c_device_by_node(rd->dn);
251 if (client == NULL)
252 return NOTIFY_OK; /* no? not meant for us */
253
254 /* unregister takes one ref away */
255 i2c_unregister_device(client);
256
257 /* and put the reference of the find */
258 put_device(&client->dev);
259 break;
260 }
261
262 return NOTIFY_OK;
263}
264
265struct notifier_block i2c_of_notifier = {
266 .notifier_call = of_i2c_notify,
267};
268#endif /* CONFIG_OF_DYNAMIC */