xref: /linux/drivers/i2c/i2c-core-of.c (revision 74ce1896c6c65b2f8cccbf59162d542988835835)
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 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 
25 static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
26 						 struct device_node *node)
27 {
28 	struct i2c_client *result;
29 	struct i2c_board_info info = {};
30 	struct dev_archdata dev_ad = {};
31 	const __be32 *addr_be;
32 	u32 addr;
33 	int len;
34 
35 	dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
36 
37 	if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
38 		dev_err(&adap->dev, "of_i2c: modalias failure on %pOF\n",
39 			node);
40 		return ERR_PTR(-EINVAL);
41 	}
42 
43 	addr_be = of_get_property(node, "reg", &len);
44 	if (!addr_be || (len < sizeof(*addr_be))) {
45 		dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", node);
46 		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 
60 	if (i2c_check_addr_validity(addr, info.flags)) {
61 		dev_err(&adap->dev, "of_i2c: invalid addr=%x on %pOF\n",
62 			addr, node);
63 		return ERR_PTR(-EINVAL);
64 	}
65 
66 	info.addr = addr;
67 	info.of_node = of_node_get(node);
68 	info.archdata = &dev_ad;
69 
70 	if (of_property_read_bool(node, "host-notify"))
71 		info.flags |= I2C_CLIENT_HOST_NOTIFY;
72 
73 	if (of_get_property(node, "wakeup-source", NULL))
74 		info.flags |= I2C_CLIENT_WAKE;
75 
76 	result = i2c_new_device(adap, &info);
77 	if (result == NULL) {
78 		dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
79 		of_node_put(node);
80 		return ERR_PTR(-EINVAL);
81 	}
82 	return result;
83 }
84 
85 void 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)) {
106 			dev_warn(&adap->dev,
107 				 "Failed to create I2C device for %pOF\n",
108 				 node);
109 			of_node_clear_flag(node, OF_POPULATED);
110 		}
111 	}
112 
113 	of_node_put(bus);
114 }
115 
116 static int of_dev_node_match(struct device *dev, void *data)
117 {
118 	return dev->of_node == data;
119 }
120 
121 /* must call put_device() when done with returned i2c_client device */
122 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
123 {
124 	struct device *dev;
125 	struct i2c_client *client;
126 
127 	dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
128 	if (!dev)
129 		return NULL;
130 
131 	client = i2c_verify_client(dev);
132 	if (!client)
133 		put_device(dev);
134 
135 	return client;
136 }
137 EXPORT_SYMBOL(of_find_i2c_device_by_node);
138 
139 /* must call put_device() when done with returned i2c_adapter device */
140 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
141 {
142 	struct device *dev;
143 	struct i2c_adapter *adapter;
144 
145 	dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
146 	if (!dev)
147 		return NULL;
148 
149 	adapter = i2c_verify_adapter(dev);
150 	if (!adapter)
151 		put_device(dev);
152 
153 	return adapter;
154 }
155 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
156 
157 /* must call i2c_put_adapter() when done with returned i2c_adapter device */
158 struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
159 {
160 	struct i2c_adapter *adapter;
161 
162 	adapter = of_find_i2c_adapter_by_node(node);
163 	if (!adapter)
164 		return NULL;
165 
166 	if (!try_module_get(adapter->owner)) {
167 		put_device(&adapter->dev);
168 		adapter = NULL;
169 	}
170 
171 	return adapter;
172 }
173 EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
174 
175 static const struct of_device_id*
176 i2c_of_match_device_sysfs(const struct of_device_id *matches,
177 				  struct i2c_client *client)
178 {
179 	const char *name;
180 
181 	for (; matches->compatible[0]; matches++) {
182 		/*
183 		 * Adding devices through the i2c sysfs interface provides us
184 		 * a string to match which may be compatible with the device
185 		 * tree compatible strings, however with no actual of_node the
186 		 * of_match_device() will not match
187 		 */
188 		if (sysfs_streq(client->name, matches->compatible))
189 			return matches;
190 
191 		name = strchr(matches->compatible, ',');
192 		if (!name)
193 			name = matches->compatible;
194 		else
195 			name++;
196 
197 		if (sysfs_streq(client->name, name))
198 			return matches;
199 	}
200 
201 	return NULL;
202 }
203 
204 const struct of_device_id
205 *i2c_of_match_device(const struct of_device_id *matches,
206 		     struct i2c_client *client)
207 {
208 	const struct of_device_id *match;
209 
210 	if (!(client && matches))
211 		return NULL;
212 
213 	match = of_match_device(matches, &client->dev);
214 	if (match)
215 		return match;
216 
217 	return i2c_of_match_device_sysfs(matches, client);
218 }
219 EXPORT_SYMBOL_GPL(i2c_of_match_device);
220 
221 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
222 static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
223 			 void *arg)
224 {
225 	struct of_reconfig_data *rd = arg;
226 	struct i2c_adapter *adap;
227 	struct i2c_client *client;
228 
229 	switch (of_reconfig_get_state_change(action, rd)) {
230 	case OF_RECONFIG_CHANGE_ADD:
231 		adap = of_find_i2c_adapter_by_node(rd->dn->parent);
232 		if (adap == NULL)
233 			return NOTIFY_OK;	/* not for us */
234 
235 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
236 			put_device(&adap->dev);
237 			return NOTIFY_OK;
238 		}
239 
240 		client = of_i2c_register_device(adap, rd->dn);
241 		put_device(&adap->dev);
242 
243 		if (IS_ERR(client)) {
244 			dev_err(&adap->dev, "failed to create client for '%pOF'\n",
245 				 rd->dn);
246 			of_node_clear_flag(rd->dn, OF_POPULATED);
247 			return notifier_from_errno(PTR_ERR(client));
248 		}
249 		break;
250 	case OF_RECONFIG_CHANGE_REMOVE:
251 		/* already depopulated? */
252 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
253 			return NOTIFY_OK;
254 
255 		/* find our device by node */
256 		client = of_find_i2c_device_by_node(rd->dn);
257 		if (client == NULL)
258 			return NOTIFY_OK;	/* no? not meant for us */
259 
260 		/* unregister takes one ref away */
261 		i2c_unregister_device(client);
262 
263 		/* and put the reference of the find */
264 		put_device(&client->dev);
265 		break;
266 	}
267 
268 	return NOTIFY_OK;
269 }
270 
271 struct notifier_block i2c_of_notifier = {
272 	.notifier_call = of_i2c_notify,
273 };
274 #endif /* CONFIG_OF_DYNAMIC */
275