Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
5bf4fa7d WS |
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 | * | |
2f5a55c5 | 8 | * Copyright (C) 2013, 2018 Wolfram Sang <wsa@kernel.org> |
5bf4fa7d WS |
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> | |
a8023e66 | 18 | #include <linux/sysfs.h> |
5bf4fa7d WS |
19 | |
20 | #include "i2c-core.h" | |
21 | ||
da0086d0 BB |
22 | int of_i2c_get_board_info(struct device *dev, struct device_node *node, |
23 | struct i2c_board_info *info) | |
5bf4fa7d | 24 | { |
5bf4fa7d | 25 | u32 addr; |
a1671af2 | 26 | int ret; |
5bf4fa7d | 27 | |
da0086d0 | 28 | memset(info, 0, sizeof(*info)); |
5bf4fa7d | 29 | |
673aa1ed | 30 | if (of_alias_from_compatible(node, info->type, sizeof(info->type)) < 0) { |
da0086d0 BB |
31 | dev_err(dev, "of_i2c: modalias failure on %pOF\n", node); |
32 | return -EINVAL; | |
5bf4fa7d WS |
33 | } |
34 | ||
a1671af2 WS |
35 | ret = of_property_read_u32(node, "reg", &addr); |
36 | if (ret) { | |
da0086d0 BB |
37 | dev_err(dev, "of_i2c: invalid reg on %pOF\n", node); |
38 | return ret; | |
5bf4fa7d WS |
39 | } |
40 | ||
5bf4fa7d WS |
41 | if (addr & I2C_TEN_BIT_ADDRESS) { |
42 | addr &= ~I2C_TEN_BIT_ADDRESS; | |
da0086d0 | 43 | info->flags |= I2C_CLIENT_TEN; |
5bf4fa7d WS |
44 | } |
45 | ||
46 | if (addr & I2C_OWN_SLAVE_ADDRESS) { | |
47 | addr &= ~I2C_OWN_SLAVE_ADDRESS; | |
da0086d0 | 48 | info->flags |= I2C_CLIENT_SLAVE; |
5bf4fa7d WS |
49 | } |
50 | ||
da0086d0 | 51 | info->addr = addr; |
60774d2a | 52 | info->fwnode = of_fwnode_handle(node); |
5bf4fa7d WS |
53 | |
54 | if (of_property_read_bool(node, "host-notify")) | |
da0086d0 | 55 | info->flags |= I2C_CLIENT_HOST_NOTIFY; |
5bf4fa7d | 56 | |
89151f6b | 57 | if (of_property_read_bool(node, "wakeup-source")) |
da0086d0 BB |
58 | info->flags |= I2C_CLIENT_WAKE; |
59 | ||
60 | return 0; | |
61 | } | |
62 | EXPORT_SYMBOL_GPL(of_i2c_get_board_info); | |
63 | ||
64 | static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap, | |
65 | struct device_node *node) | |
66 | { | |
67 | struct i2c_client *client; | |
68 | struct i2c_board_info info; | |
69 | int ret; | |
70 | ||
71 | dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node); | |
72 | ||
73 | ret = of_i2c_get_board_info(&adap->dev, node, &info); | |
74 | if (ret) | |
75 | return ERR_PTR(ret); | |
5bf4fa7d | 76 | |
5f0155b4 WS |
77 | client = i2c_new_client_device(adap, &info); |
78 | if (IS_ERR(client)) | |
453a237c | 79 | dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node); |
5f0155b4 | 80 | |
c49b0e07 | 81 | return client; |
5bf4fa7d WS |
82 | } |
83 | ||
84 | void of_i2c_register_devices(struct i2c_adapter *adap) | |
85 | { | |
86 | struct device_node *bus, *node; | |
87 | struct i2c_client *client; | |
88 | ||
89 | /* Only register child devices if the adapter has a node pointer set */ | |
90 | if (!adap->dev.of_node) | |
91 | return; | |
92 | ||
93 | dev_dbg(&adap->dev, "of_i2c: walking child nodes\n"); | |
94 | ||
95 | bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus"); | |
96 | if (!bus) | |
97 | bus = of_node_get(adap->dev.of_node); | |
98 | ||
99 | for_each_available_child_of_node(bus, node) { | |
100 | if (of_node_test_and_set_flag(node, OF_POPULATED)) | |
101 | continue; | |
102 | ||
103 | client = of_i2c_register_device(adap, node); | |
104 | if (IS_ERR(client)) { | |
f1c87ceb | 105 | dev_err(&adap->dev, |
453a237c RH |
106 | "Failed to create I2C device for %pOF\n", |
107 | node); | |
5bf4fa7d WS |
108 | of_node_clear_flag(node, OF_POPULATED); |
109 | } | |
110 | } | |
111 | ||
112 | of_node_put(bus); | |
113 | } | |
114 | ||
5bf4fa7d WS |
115 | static const struct of_device_id* |
116 | i2c_of_match_device_sysfs(const struct of_device_id *matches, | |
117 | struct i2c_client *client) | |
118 | { | |
119 | const char *name; | |
120 | ||
121 | for (; matches->compatible[0]; matches++) { | |
122 | /* | |
123 | * Adding devices through the i2c sysfs interface provides us | |
124 | * a string to match which may be compatible with the device | |
125 | * tree compatible strings, however with no actual of_node the | |
126 | * of_match_device() will not match | |
127 | */ | |
128 | if (sysfs_streq(client->name, matches->compatible)) | |
129 | return matches; | |
130 | ||
131 | name = strchr(matches->compatible, ','); | |
132 | if (!name) | |
133 | name = matches->compatible; | |
134 | else | |
135 | name++; | |
136 | ||
137 | if (sysfs_streq(client->name, name)) | |
138 | return matches; | |
139 | } | |
140 | ||
141 | return NULL; | |
142 | } | |
143 | ||
144 | const struct of_device_id | |
145 | *i2c_of_match_device(const struct of_device_id *matches, | |
146 | struct i2c_client *client) | |
147 | { | |
148 | const struct of_device_id *match; | |
149 | ||
150 | if (!(client && matches)) | |
151 | return NULL; | |
152 | ||
153 | match = of_match_device(matches, &client->dev); | |
154 | if (match) | |
155 | return match; | |
156 | ||
157 | return i2c_of_match_device_sysfs(matches, client); | |
158 | } | |
5bf4fa7d WS |
159 | |
160 | #if IS_ENABLED(CONFIG_OF_DYNAMIC) | |
161 | static int of_i2c_notify(struct notifier_block *nb, unsigned long action, | |
162 | void *arg) | |
163 | { | |
164 | struct of_reconfig_data *rd = arg; | |
165 | struct i2c_adapter *adap; | |
166 | struct i2c_client *client; | |
167 | ||
168 | switch (of_reconfig_get_state_change(action, rd)) { | |
169 | case OF_RECONFIG_CHANGE_ADD: | |
170 | adap = of_find_i2c_adapter_by_node(rd->dn->parent); | |
171 | if (adap == NULL) | |
172 | return NOTIFY_OK; /* not for us */ | |
173 | ||
174 | if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { | |
175 | put_device(&adap->dev); | |
176 | return NOTIFY_OK; | |
177 | } | |
178 | ||
1a50d940 GU |
179 | /* |
180 | * Clear the flag before adding the device so that fw_devlink | |
181 | * doesn't skip adding consumers to this device. | |
182 | */ | |
183 | rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE; | |
5bf4fa7d | 184 | client = of_i2c_register_device(adap, rd->dn); |
5bf4fa7d | 185 | if (IS_ERR(client)) { |
453a237c RH |
186 | dev_err(&adap->dev, "failed to create client for '%pOF'\n", |
187 | rd->dn); | |
a4c2fec1 | 188 | put_device(&adap->dev); |
5bf4fa7d WS |
189 | of_node_clear_flag(rd->dn, OF_POPULATED); |
190 | return notifier_from_errno(PTR_ERR(client)); | |
191 | } | |
a4c2fec1 | 192 | put_device(&adap->dev); |
5bf4fa7d WS |
193 | break; |
194 | case OF_RECONFIG_CHANGE_REMOVE: | |
195 | /* already depopulated? */ | |
196 | if (!of_node_check_flag(rd->dn, OF_POPULATED)) | |
197 | return NOTIFY_OK; | |
198 | ||
199 | /* find our device by node */ | |
200 | client = of_find_i2c_device_by_node(rd->dn); | |
201 | if (client == NULL) | |
202 | return NOTIFY_OK; /* no? not meant for us */ | |
203 | ||
204 | /* unregister takes one ref away */ | |
205 | i2c_unregister_device(client); | |
206 | ||
207 | /* and put the reference of the find */ | |
208 | put_device(&client->dev); | |
209 | break; | |
210 | } | |
211 | ||
212 | return NOTIFY_OK; | |
213 | } | |
214 | ||
215 | struct notifier_block i2c_of_notifier = { | |
216 | .notifier_call = of_i2c_notify, | |
217 | }; | |
218 | #endif /* CONFIG_OF_DYNAMIC */ |