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