]>
Commit | Line | Data |
---|---|---|
f85ff305 SR |
1 | #include <linux/string.h> |
2 | #include <linux/kernel.h> | |
3 | #include <linux/of.h> | |
f898f8db | 4 | #include <linux/of_device.h> |
1f5c69aa MK |
5 | #include <linux/of_address.h> |
6 | #include <linux/of_iommu.h> | |
7 | #include <linux/dma-mapping.h> | |
f85ff305 SR |
8 | #include <linux/init.h> |
9 | #include <linux/module.h> | |
10 | #include <linux/mod_devicetable.h> | |
11 | #include <linux/slab.h> | |
72328883 | 12 | #include <linux/platform_device.h> |
f85ff305 SR |
13 | |
14 | #include <asm/errno.h> | |
ced4eec9 | 15 | #include "of_private.h" |
f85ff305 | 16 | |
f85ff305 | 17 | /** |
44504b2b | 18 | * of_match_device - Tell if a struct device matches an of_device_id list |
f85ff305 SR |
19 | * @ids: array of of device match structures to search in |
20 | * @dev: the of device structure to match against | |
21 | * | |
2dc11581 | 22 | * Used by a driver to check whether an platform_device present in the |
f85ff305 SR |
23 | * system is in its list of supported devices. |
24 | */ | |
25 | const struct of_device_id *of_match_device(const struct of_device_id *matches, | |
44504b2b | 26 | const struct device *dev) |
f85ff305 | 27 | { |
8cec0e7b | 28 | if ((!matches) || (!dev->of_node)) |
f85ff305 | 29 | return NULL; |
44504b2b | 30 | return of_match_node(matches, dev->of_node); |
f85ff305 SR |
31 | } |
32 | EXPORT_SYMBOL(of_match_device); | |
33 | ||
94a0cb1f | 34 | struct platform_device *of_dev_get(struct platform_device *dev) |
f85ff305 SR |
35 | { |
36 | struct device *tmp; | |
37 | ||
38 | if (!dev) | |
39 | return NULL; | |
40 | tmp = get_device(&dev->dev); | |
41 | if (tmp) | |
94a0cb1f | 42 | return to_platform_device(tmp); |
f85ff305 SR |
43 | else |
44 | return NULL; | |
45 | } | |
46 | EXPORT_SYMBOL(of_dev_get); | |
47 | ||
94a0cb1f | 48 | void of_dev_put(struct platform_device *dev) |
f85ff305 SR |
49 | { |
50 | if (dev) | |
51 | put_device(&dev->dev); | |
52 | } | |
53 | EXPORT_SYMBOL(of_dev_put); | |
54 | ||
7096d042 | 55 | int of_device_add(struct platform_device *ofdev) |
f85ff305 | 56 | { |
61c7a080 | 57 | BUG_ON(ofdev->dev.of_node == NULL); |
6098e2ee | 58 | |
eca39301 GL |
59 | /* name and id have to be set so that the platform bus doesn't get |
60 | * confused on matching */ | |
61 | ofdev->name = dev_name(&ofdev->dev); | |
6d7e3bf8 | 62 | ofdev->id = PLATFORM_DEVID_NONE; |
eca39301 | 63 | |
56f2de81 ZL |
64 | /* |
65 | * If this device has not binding numa node in devicetree, that is | |
66 | * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this | |
67 | * device is on the same node as the parent. | |
68 | */ | |
69 | set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node)); | |
6098e2ee JK |
70 | |
71 | return device_add(&ofdev->dev); | |
f85ff305 | 72 | } |
7096d042 | 73 | |
1f5c69aa MK |
74 | /** |
75 | * of_dma_configure - Setup DMA configuration | |
76 | * @dev: Device to apply DMA configuration | |
77 | * @np: Pointer to OF node having DMA configuration | |
78 | * | |
79 | * Try to get devices's DMA configuration from DT and update it | |
80 | * accordingly. | |
81 | * | |
82 | * If platform code needs to use its own special DMA configuration, it | |
83 | * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events | |
84 | * to fix up DMA configuration. | |
85 | */ | |
7b07cbef | 86 | int of_dma_configure(struct device *dev, struct device_node *np) |
1f5c69aa | 87 | { |
72328883 | 88 | u64 dma_addr, paddr, size = 0; |
1f5c69aa MK |
89 | int ret; |
90 | bool coherent; | |
91 | unsigned long offset; | |
53c92d79 | 92 | const struct iommu_ops *iommu; |
ee7b1f31 | 93 | u64 mask; |
1f5c69aa | 94 | |
1f5c69aa MK |
95 | ret = of_dma_get_range(np, &dma_addr, &paddr, &size); |
96 | if (ret < 0) { | |
72328883 RM |
97 | /* |
98 | * For legacy reasons, we have to assume some devices need | |
99 | * DMA configuration regardless of whether "dma-ranges" is | |
100 | * correctly specified or not. | |
101 | */ | |
d89e2378 | 102 | if (!dev->bus->force_dma) |
72328883 RM |
103 | return ret == -ENODEV ? 0 : ret; |
104 | ||
1f5c69aa | 105 | dma_addr = offset = 0; |
1f5c69aa MK |
106 | } else { |
107 | offset = PFN_DOWN(paddr - dma_addr); | |
0c79c81c MK |
108 | |
109 | /* | |
110 | * Add a work around to treat the size as mask + 1 in case | |
111 | * it is defined in DT as a mask. | |
112 | */ | |
113 | if (size & 1) { | |
114 | dev_warn(dev, "Invalid size 0x%llx for dma-range\n", | |
115 | size); | |
116 | size = size + 1; | |
117 | } | |
118 | ||
119 | if (!size) { | |
120 | dev_err(dev, "Adjusted size 0x%llx invalid\n", size); | |
7b07cbef | 121 | return -EINVAL; |
0c79c81c | 122 | } |
1f5c69aa MK |
123 | dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset); |
124 | } | |
125 | ||
72328883 RM |
126 | /* |
127 | * Set default coherent_dma_mask to 32 bit. Drivers are expected to | |
128 | * setup the correct supported mask. | |
129 | */ | |
130 | if (!dev->coherent_dma_mask) | |
131 | dev->coherent_dma_mask = DMA_BIT_MASK(32); | |
132 | /* | |
133 | * Set it to coherent_dma_mask by default if the architecture | |
134 | * code has not set it. | |
135 | */ | |
136 | if (!dev->dma_mask) | |
137 | dev->dma_mask = &dev->coherent_dma_mask; | |
138 | ||
139 | if (!size) | |
140 | size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1); | |
141 | ||
1f5c69aa MK |
142 | dev->dma_pfn_offset = offset; |
143 | ||
9a6d7298 MK |
144 | /* |
145 | * Limit coherent and dma mask based on size and default mask | |
146 | * set by the driver. | |
147 | */ | |
ee7b1f31 RM |
148 | mask = DMA_BIT_MASK(ilog2(dma_addr + size - 1) + 1); |
149 | dev->coherent_dma_mask &= mask; | |
150 | *dev->dma_mask &= mask; | |
9a6d7298 | 151 | |
1f5c69aa MK |
152 | coherent = of_dma_is_coherent(np); |
153 | dev_dbg(dev, "device is%sdma coherent\n", | |
154 | coherent ? " " : " not "); | |
155 | ||
156 | iommu = of_iommu_configure(dev, np); | |
a37b19a3 S |
157 | if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER) |
158 | return -EPROBE_DEFER; | |
7b07cbef | 159 | |
1f5c69aa MK |
160 | dev_dbg(dev, "device is%sbehind an iommu\n", |
161 | iommu ? " " : " not "); | |
162 | ||
163 | arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent); | |
7b07cbef LP |
164 | |
165 | return 0; | |
1f5c69aa MK |
166 | } |
167 | EXPORT_SYMBOL_GPL(of_dma_configure); | |
168 | ||
3f186677 LP |
169 | /** |
170 | * of_dma_deconfigure - Clean up DMA configuration | |
171 | * @dev: Device for which to clean up DMA configuration | |
172 | * | |
173 | * Clean up all configuration performed by of_dma_configure_ops() and free all | |
174 | * resources that have been allocated. | |
175 | */ | |
176 | void of_dma_deconfigure(struct device *dev) | |
177 | { | |
178 | arch_teardown_dma_ops(dev); | |
179 | } | |
180 | ||
7096d042 GL |
181 | int of_device_register(struct platform_device *pdev) |
182 | { | |
183 | device_initialize(&pdev->dev); | |
184 | return of_device_add(pdev); | |
185 | } | |
f85ff305 SR |
186 | EXPORT_SYMBOL(of_device_register); |
187 | ||
94a0cb1f | 188 | void of_device_unregister(struct platform_device *ofdev) |
f85ff305 | 189 | { |
f85ff305 SR |
190 | device_unregister(&ofdev->dev); |
191 | } | |
192 | EXPORT_SYMBOL(of_device_unregister); | |
09e67ca2 | 193 | |
3386e0fa JE |
194 | const void *of_device_get_match_data(const struct device *dev) |
195 | { | |
196 | const struct of_device_id *match; | |
197 | ||
198 | match = of_match_device(dev->driver->of_match_table, dev); | |
199 | if (!match) | |
200 | return NULL; | |
201 | ||
202 | return match->data; | |
203 | } | |
204 | EXPORT_SYMBOL(of_device_get_match_data); | |
205 | ||
0634c295 | 206 | static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len) |
09e67ca2 SR |
207 | { |
208 | const char *compat; | |
bc575064 RH |
209 | char *c; |
210 | struct property *p; | |
211 | ssize_t csize; | |
8c2a75e5 | 212 | ssize_t tsize; |
09e67ca2 | 213 | |
b9f73067 ZR |
214 | if ((!dev) || (!dev->of_node)) |
215 | return -ENODEV; | |
216 | ||
09e67ca2 | 217 | /* Name & Type */ |
34a1c1e8 GL |
218 | csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name, |
219 | dev->of_node->type); | |
8c2a75e5 | 220 | tsize = csize; |
bc575064 | 221 | len -= csize; |
8c2a75e5 BA |
222 | if (str) |
223 | str += csize; | |
bc575064 RH |
224 | |
225 | of_property_for_each_string(dev->of_node, "compatible", p, compat) { | |
8c2a75e5 BA |
226 | csize = strlen(compat) + 1; |
227 | tsize += csize; | |
228 | if (csize > len) | |
229 | continue; | |
bc575064 RH |
230 | |
231 | csize = snprintf(str, len, "C%s", compat); | |
232 | for (c = str; c; ) { | |
233 | c = strchr(c, ' '); | |
234 | if (c) | |
235 | *c++ = '_'; | |
236 | } | |
237 | len -= csize; | |
238 | str += csize; | |
09e67ca2 SR |
239 | } |
240 | ||
8c2a75e5 | 241 | return tsize; |
09e67ca2 | 242 | } |
dd27dcda | 243 | |
9c829c09 SB |
244 | int of_device_request_module(struct device *dev) |
245 | { | |
246 | char *str; | |
247 | ssize_t size; | |
248 | int ret; | |
249 | ||
250 | size = of_device_get_modalias(dev, NULL, 0); | |
251 | if (size < 0) | |
252 | return size; | |
253 | ||
254 | str = kmalloc(size + 1, GFP_KERNEL); | |
255 | if (!str) | |
256 | return -ENOMEM; | |
257 | ||
258 | of_device_get_modalias(dev, str, size); | |
259 | str[size] = '\0'; | |
260 | ret = request_module(str); | |
261 | kfree(str); | |
262 | ||
263 | return ret; | |
264 | } | |
265 | EXPORT_SYMBOL_GPL(of_device_request_module); | |
266 | ||
0634c295 RH |
267 | /** |
268 | * of_device_modalias - Fill buffer with newline terminated modalias string | |
269 | */ | |
270 | ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len) | |
271 | { | |
272 | ssize_t sl = of_device_get_modalias(dev, str, len - 2); | |
273 | if (sl < 0) | |
274 | return sl; | |
08ab58d9 BA |
275 | if (sl > len - 2) |
276 | return -ENOMEM; | |
0634c295 RH |
277 | |
278 | str[sl++] = '\n'; | |
279 | str[sl] = 0; | |
280 | return sl; | |
281 | } | |
282 | EXPORT_SYMBOL_GPL(of_device_modalias); | |
283 | ||
dd27dcda GL |
284 | /** |
285 | * of_device_uevent - Display OF related uevent information | |
286 | */ | |
07d57a32 | 287 | void of_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
dd27dcda GL |
288 | { |
289 | const char *compat; | |
ced4eec9 | 290 | struct alias_prop *app; |
bc575064 RH |
291 | struct property *p; |
292 | int seen = 0; | |
dd27dcda GL |
293 | |
294 | if ((!dev) || (!dev->of_node)) | |
07d57a32 | 295 | return; |
dd27dcda | 296 | |
07d57a32 | 297 | add_uevent_var(env, "OF_NAME=%s", dev->of_node->name); |
0d638a07 | 298 | add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node); |
07d57a32 GL |
299 | if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0) |
300 | add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type); | |
dd27dcda GL |
301 | |
302 | /* Since the compatible field can contain pretty much anything | |
303 | * it's not really legal to split it out with commas. We split it | |
304 | * up using a number of environment variables instead. */ | |
bc575064 | 305 | of_property_for_each_string(dev->of_node, "compatible", p, compat) { |
07d57a32 | 306 | add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat); |
dd27dcda GL |
307 | seen++; |
308 | } | |
07d57a32 | 309 | add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen); |
ced4eec9 SM |
310 | |
311 | seen = 0; | |
c05aba2b | 312 | mutex_lock(&of_mutex); |
ced4eec9 SM |
313 | list_for_each_entry(app, &aliases_lookup, link) { |
314 | if (dev->of_node == app->np) { | |
315 | add_uevent_var(env, "OF_ALIAS_%d=%s", seen, | |
316 | app->alias); | |
317 | seen++; | |
318 | } | |
319 | } | |
c05aba2b | 320 | mutex_unlock(&of_mutex); |
07d57a32 | 321 | } |
dd27dcda | 322 | |
07d57a32 GL |
323 | int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env) |
324 | { | |
325 | int sl; | |
326 | ||
327 | if ((!dev) || (!dev->of_node)) | |
328 | return -ENODEV; | |
dd27dcda | 329 | |
07d57a32 | 330 | /* Devicetree modalias is tricky, we add it in 2 steps */ |
dd27dcda GL |
331 | if (add_uevent_var(env, "MODALIAS=")) |
332 | return -ENOMEM; | |
333 | ||
34a1c1e8 | 334 | sl = of_device_get_modalias(dev, &env->buf[env->buflen-1], |
dd27dcda GL |
335 | sizeof(env->buf) - env->buflen); |
336 | if (sl >= (sizeof(env->buf) - env->buflen)) | |
337 | return -ENOMEM; | |
338 | env->buflen += sl; | |
339 | ||
340 | return 0; | |
341 | } | |
7a3b7cd3 | 342 | EXPORT_SYMBOL_GPL(of_device_uevent_modalias); |