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