• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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  *
8  * Copyright (C) 2013, 2018 Wolfram Sang <wsa@kernel.org>
9  */
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>
18 #include <linux/sysfs.h>
19 #include <trace/hooks/i2c.h>
20 
21 #include "i2c-core.h"
22 
of_i2c_get_board_info(struct device * dev,struct device_node * node,struct i2c_board_info * info)23 int of_i2c_get_board_info(struct device *dev, struct device_node *node,
24 			  struct i2c_board_info *info)
25 {
26 	u32 addr;
27 	int ret;
28 
29 	memset(info, 0, sizeof(*info));
30 
31 	if (of_modalias_node(node, info->type, sizeof(info->type)) < 0) {
32 		dev_err(dev, "of_i2c: modalias failure on %pOF\n", node);
33 		return -EINVAL;
34 	}
35 
36 	trace_android_vh_of_i2c_get_board_info(node, &(info->dev_name));
37 
38 	ret = of_property_read_u32(node, "reg", &addr);
39 	if (ret) {
40 		dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
41 		return ret;
42 	}
43 
44 	if (addr & I2C_TEN_BIT_ADDRESS) {
45 		addr &= ~I2C_TEN_BIT_ADDRESS;
46 		info->flags |= I2C_CLIENT_TEN;
47 	}
48 
49 	if (addr & I2C_OWN_SLAVE_ADDRESS) {
50 		addr &= ~I2C_OWN_SLAVE_ADDRESS;
51 		info->flags |= I2C_CLIENT_SLAVE;
52 	}
53 
54 	info->addr = addr;
55 	info->of_node = node;
56 	info->fwnode = of_fwnode_handle(node);
57 
58 	if (of_property_read_bool(node, "host-notify"))
59 		info->flags |= I2C_CLIENT_HOST_NOTIFY;
60 
61 	if (of_get_property(node, "wakeup-source", NULL))
62 		info->flags |= I2C_CLIENT_WAKE;
63 
64 	return 0;
65 }
66 EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
67 
of_i2c_register_device(struct i2c_adapter * adap,struct device_node * node)68 static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
69 						 struct device_node *node)
70 {
71 	struct i2c_client *client;
72 	struct i2c_board_info info;
73 	int ret;
74 
75 	dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
76 
77 	ret = of_i2c_get_board_info(&adap->dev, node, &info);
78 	if (ret)
79 		return ERR_PTR(ret);
80 
81 	client = i2c_new_client_device(adap, &info);
82 	if (IS_ERR(client))
83 		dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
84 
85 	return client;
86 }
87 
of_i2c_register_devices(struct i2c_adapter * adap)88 void 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)) {
109 			dev_err(&adap->dev,
110 				 "Failed to create I2C device for %pOF\n",
111 				 node);
112 			of_node_clear_flag(node, OF_POPULATED);
113 		}
114 	}
115 
116 	of_node_put(bus);
117 }
118 
of_dev_or_parent_node_match(struct device * dev,const void * data)119 static int of_dev_or_parent_node_match(struct device *dev, const void *data)
120 {
121 	if (dev->of_node == data)
122 		return 1;
123 
124 	if (dev->parent)
125 		return dev->parent->of_node == data;
126 
127 	return 0;
128 }
129 
130 /* must call put_device() when done with returned i2c_client device */
of_find_i2c_device_by_node(struct device_node * node)131 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
132 {
133 	struct device *dev;
134 	struct i2c_client *client;
135 
136 	dev = bus_find_device_by_of_node(&i2c_bus_type, node);
137 	if (!dev)
138 		return NULL;
139 
140 	client = i2c_verify_client(dev);
141 	if (!client)
142 		put_device(dev);
143 
144 	return client;
145 }
146 EXPORT_SYMBOL(of_find_i2c_device_by_node);
147 
148 /* must call put_device() when done with returned i2c_adapter device */
of_find_i2c_adapter_by_node(struct device_node * node)149 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
150 {
151 	struct device *dev;
152 	struct i2c_adapter *adapter;
153 
154 	dev = bus_find_device(&i2c_bus_type, NULL, node,
155 			      of_dev_or_parent_node_match);
156 	if (!dev)
157 		return NULL;
158 
159 	adapter = i2c_verify_adapter(dev);
160 	if (!adapter)
161 		put_device(dev);
162 
163 	return adapter;
164 }
165 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
166 
167 /* must call i2c_put_adapter() when done with returned i2c_adapter device */
of_get_i2c_adapter_by_node(struct device_node * node)168 struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
169 {
170 	struct i2c_adapter *adapter;
171 
172 	adapter = of_find_i2c_adapter_by_node(node);
173 	if (!adapter)
174 		return NULL;
175 
176 	if (!try_module_get(adapter->owner)) {
177 		put_device(&adapter->dev);
178 		adapter = NULL;
179 	}
180 
181 	return adapter;
182 }
183 EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
184 
185 static const struct of_device_id*
i2c_of_match_device_sysfs(const struct of_device_id * matches,struct i2c_client * client)186 i2c_of_match_device_sysfs(const struct of_device_id *matches,
187 				  struct i2c_client *client)
188 {
189 	const char *name;
190 
191 	for (; matches->compatible[0]; matches++) {
192 		/*
193 		 * Adding devices through the i2c sysfs interface provides us
194 		 * a string to match which may be compatible with the device
195 		 * tree compatible strings, however with no actual of_node the
196 		 * of_match_device() will not match
197 		 */
198 		if (sysfs_streq(client->name, matches->compatible))
199 			return matches;
200 
201 		name = strchr(matches->compatible, ',');
202 		if (!name)
203 			name = matches->compatible;
204 		else
205 			name++;
206 
207 		if (sysfs_streq(client->name, name))
208 			return matches;
209 	}
210 
211 	return NULL;
212 }
213 
214 const struct of_device_id
i2c_of_match_device(const struct of_device_id * matches,struct i2c_client * client)215 *i2c_of_match_device(const struct of_device_id *matches,
216 		     struct i2c_client *client)
217 {
218 	const struct of_device_id *match;
219 
220 	if (!(client && matches))
221 		return NULL;
222 
223 	match = of_match_device(matches, &client->dev);
224 	if (match)
225 		return match;
226 
227 	return i2c_of_match_device_sysfs(matches, client);
228 }
229 EXPORT_SYMBOL_GPL(i2c_of_match_device);
230 
231 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
of_i2c_notify(struct notifier_block * nb,unsigned long action,void * arg)232 static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
233 			 void *arg)
234 {
235 	struct of_reconfig_data *rd = arg;
236 	struct i2c_adapter *adap;
237 	struct i2c_client *client;
238 
239 	switch (of_reconfig_get_state_change(action, rd)) {
240 	case OF_RECONFIG_CHANGE_ADD:
241 		adap = of_find_i2c_adapter_by_node(rd->dn->parent);
242 		if (adap == NULL)
243 			return NOTIFY_OK;	/* not for us */
244 
245 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
246 			put_device(&adap->dev);
247 			return NOTIFY_OK;
248 		}
249 
250 		client = of_i2c_register_device(adap, rd->dn);
251 		if (IS_ERR(client)) {
252 			dev_err(&adap->dev, "failed to create client for '%pOF'\n",
253 				 rd->dn);
254 			put_device(&adap->dev);
255 			of_node_clear_flag(rd->dn, OF_POPULATED);
256 			return notifier_from_errno(PTR_ERR(client));
257 		}
258 		put_device(&adap->dev);
259 		break;
260 	case OF_RECONFIG_CHANGE_REMOVE:
261 		/* already depopulated? */
262 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
263 			return NOTIFY_OK;
264 
265 		/* find our device by node */
266 		client = of_find_i2c_device_by_node(rd->dn);
267 		if (client == NULL)
268 			return NOTIFY_OK;	/* no? not meant for us */
269 
270 		/* unregister takes one ref away */
271 		i2c_unregister_device(client);
272 
273 		/* and put the reference of the find */
274 		put_device(&client->dev);
275 		break;
276 	}
277 
278 	return NOTIFY_OK;
279 }
280 
281 struct notifier_block i2c_of_notifier = {
282 	.notifier_call = of_i2c_notify,
283 };
284 #endif /* CONFIG_OF_DYNAMIC */
285