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