]>
Commit | Line | Data |
---|---|---|
989d42e8 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * platform.c - platform 'pseudo' bus for legacy devices | |
4 | * | |
5 | * Copyright (c) 2002-3 Patrick Mochel | |
6 | * Copyright (c) 2002-3 Open Source Development Labs | |
7 | * | |
fe34c89d | 8 | * Please see Documentation/driver-api/driver-model/platform.rst for more |
1da177e4 LT |
9 | * information. |
10 | */ | |
11 | ||
daa41226 | 12 | #include <linux/string.h> |
d052d1be | 13 | #include <linux/platform_device.h> |
05212157 | 14 | #include <linux/of_device.h> |
9ec36caf | 15 | #include <linux/of_irq.h> |
1da177e4 LT |
16 | #include <linux/module.h> |
17 | #include <linux/init.h> | |
18 | #include <linux/dma-mapping.h> | |
57c8a661 | 19 | #include <linux/memblock.h> |
1da177e4 | 20 | #include <linux/err.h> |
4e57b681 | 21 | #include <linux/slab.h> |
9d730229 | 22 | #include <linux/pm_runtime.h> |
f48c767c | 23 | #include <linux/pm_domain.h> |
689ae231 | 24 | #include <linux/idr.h> |
91e56878 | 25 | #include <linux/acpi.h> |
86be408b | 26 | #include <linux/clk/clk-conf.h> |
3d713e0e | 27 | #include <linux/limits.h> |
00bbc1d8 | 28 | #include <linux/property.h> |
967d3010 | 29 | #include <linux/kmemleak.h> |
39cc539f | 30 | #include <linux/types.h> |
1da177e4 | 31 | |
a1bdc7aa | 32 | #include "base.h" |
bed2b42d | 33 | #include "power/power.h" |
a1bdc7aa | 34 | |
689ae231 JD |
35 | /* For automatically allocated device IDs */ |
36 | static DEFINE_IDA(platform_devid_ida); | |
37 | ||
1da177e4 | 38 | struct device platform_bus = { |
1e0b2cf9 | 39 | .init_name = "platform", |
1da177e4 | 40 | }; |
a96b2042 | 41 | EXPORT_SYMBOL_GPL(platform_bus); |
1da177e4 LT |
42 | |
43 | /** | |
4a3ad20c GKH |
44 | * platform_get_resource - get a resource for a device |
45 | * @dev: platform device | |
46 | * @type: resource type | |
47 | * @num: resource index | |
1da177e4 | 48 | */ |
4a3ad20c GKH |
49 | struct resource *platform_get_resource(struct platform_device *dev, |
50 | unsigned int type, unsigned int num) | |
1da177e4 | 51 | { |
39cc539f | 52 | u32 i; |
1da177e4 LT |
53 | |
54 | for (i = 0; i < dev->num_resources; i++) { | |
55 | struct resource *r = &dev->resource[i]; | |
56 | ||
c9f66169 MD |
57 | if (type == resource_type(r) && num-- == 0) |
58 | return r; | |
1da177e4 LT |
59 | } |
60 | return NULL; | |
61 | } | |
a96b2042 | 62 | EXPORT_SYMBOL_GPL(platform_get_resource); |
1da177e4 | 63 | |
bb6243b4 | 64 | #ifdef CONFIG_HAS_IOMEM |
890cc39a DZ |
65 | /** |
66 | * devm_platform_get_and_ioremap_resource - call devm_ioremap_resource() for a | |
67 | * platform device and get resource | |
68 | * | |
69 | * @pdev: platform device to use both for memory resource lookup as well as | |
70 | * resource management | |
71 | * @index: resource index | |
72 | * @res: optional output parameter to store a pointer to the obtained resource. | |
73 | */ | |
74 | void __iomem * | |
75 | devm_platform_get_and_ioremap_resource(struct platform_device *pdev, | |
76 | unsigned int index, struct resource **res) | |
77 | { | |
78 | struct resource *r; | |
79 | ||
80 | r = platform_get_resource(pdev, IORESOURCE_MEM, index); | |
81 | if (res) | |
82 | *res = r; | |
83 | return devm_ioremap_resource(&pdev->dev, r); | |
84 | } | |
85 | EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource); | |
86 | ||
7945f929 BG |
87 | /** |
88 | * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform | |
89 | * device | |
90 | * | |
91 | * @pdev: platform device to use both for memory resource lookup as well as | |
7067c96e | 92 | * resource management |
7945f929 BG |
93 | * @index: resource index |
94 | */ | |
95 | void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev, | |
96 | unsigned int index) | |
97 | { | |
fd78901c | 98 | return devm_platform_get_and_ioremap_resource(pdev, index, NULL); |
7945f929 BG |
99 | } |
100 | EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource); | |
bb6243b4 BG |
101 | |
102 | /** | |
103 | * devm_platform_ioremap_resource_wc - write-combined variant of | |
104 | * devm_platform_ioremap_resource() | |
105 | * | |
106 | * @pdev: platform device to use both for memory resource lookup as well as | |
107 | * resource management | |
108 | * @index: resource index | |
109 | */ | |
110 | void __iomem *devm_platform_ioremap_resource_wc(struct platform_device *pdev, | |
111 | unsigned int index) | |
112 | { | |
113 | struct resource *res; | |
114 | ||
115 | res = platform_get_resource(pdev, IORESOURCE_MEM, index); | |
116 | return devm_ioremap_resource_wc(&pdev->dev, res); | |
117 | } | |
c9c8641d BG |
118 | |
119 | /** | |
120 | * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for | |
121 | * a platform device, retrieve the | |
122 | * resource by name | |
123 | * | |
124 | * @pdev: platform device to use both for memory resource lookup as well as | |
125 | * resource management | |
126 | * @name: name of the resource | |
127 | */ | |
128 | void __iomem * | |
129 | devm_platform_ioremap_resource_byname(struct platform_device *pdev, | |
130 | const char *name) | |
131 | { | |
132 | struct resource *res; | |
133 | ||
134 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name); | |
135 | return devm_ioremap_resource(&pdev->dev, res); | |
136 | } | |
137 | EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname); | |
837ccda3 | 138 | #endif /* CONFIG_HAS_IOMEM */ |
7945f929 | 139 | |
ec4e2906 UKK |
140 | /** |
141 | * platform_get_irq_optional - get an optional IRQ for a device | |
142 | * @dev: platform device | |
143 | * @num: IRQ number index | |
144 | * | |
145 | * Gets an IRQ for a platform device. Device drivers should check the return | |
146 | * value for errors so as to not pass a negative integer value to the | |
147 | * request_irq() APIs. This is the same as platform_get_irq(), except that it | |
148 | * does not print an error message if an IRQ can not be obtained. | |
149 | * | |
150 | * Example: | |
151 | * int irq = platform_get_irq_optional(pdev, 0); | |
152 | * if (irq < 0) | |
153 | * return irq; | |
154 | * | |
155 | * Return: IRQ number on success, negative error number on failure. | |
156 | */ | |
157 | int platform_get_irq_optional(struct platform_device *dev, unsigned int num) | |
1da177e4 | 158 | { |
5cf8f7db AL |
159 | #ifdef CONFIG_SPARC |
160 | /* sparc does not have irqs represented as IORESOURCE_IRQ resources */ | |
161 | if (!dev || num >= dev->archdata.num_irqs) | |
162 | return -ENXIO; | |
163 | return dev->archdata.irqs[num]; | |
164 | #else | |
9ec36caf | 165 | struct resource *r; |
71564a26 | 166 | int ret; |
aff008ad | 167 | |
71564a26 | 168 | if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) { |
aff008ad | 169 | ret = of_irq_get(dev->dev.of_node, num); |
e330b9a6 | 170 | if (ret > 0 || ret == -EPROBE_DEFER) |
aff008ad GR |
171 | return ret; |
172 | } | |
9ec36caf RH |
173 | |
174 | r = platform_get_resource(dev, IORESOURCE_IRQ, num); | |
d44fa3d4 AVF |
175 | if (has_acpi_companion(&dev->dev)) { |
176 | if (r && r->flags & IORESOURCE_DISABLED) { | |
d44fa3d4 AVF |
177 | ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r); |
178 | if (ret) | |
179 | return ret; | |
180 | } | |
181 | } | |
182 | ||
7085a740 LW |
183 | /* |
184 | * The resources may pass trigger flags to the irqs that need | |
185 | * to be set up. It so happens that the trigger flags for | |
186 | * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER* | |
187 | * settings. | |
188 | */ | |
60ca5e0d GR |
189 | if (r && r->flags & IORESOURCE_BITS) { |
190 | struct irq_data *irqd; | |
191 | ||
192 | irqd = irq_get_irq_data(r->start); | |
193 | if (!irqd) | |
194 | return -ENXIO; | |
195 | irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS); | |
196 | } | |
1da177e4 | 197 | |
daaef255 EG |
198 | if (r) |
199 | return r->start; | |
200 | ||
201 | /* | |
202 | * For the index 0 interrupt, allow falling back to GpioInt | |
203 | * resources. While a device could have both Interrupt and GpioInt | |
204 | * resources, making this fallback ambiguous, in many common cases | |
205 | * the device will only expose one IRQ, and this fallback | |
206 | * allows a common code path across either kind of resource. | |
207 | */ | |
46c42d84 | 208 | if (num == 0 && has_acpi_companion(&dev->dev)) { |
71564a26 | 209 | ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num); |
46c42d84 BN |
210 | /* Our callers expect -ENXIO for missing IRQs. */ |
211 | if (ret >= 0 || ret == -EPROBE_DEFER) | |
212 | return ret; | |
213 | } | |
daaef255 EG |
214 | |
215 | return -ENXIO; | |
5cf8f7db | 216 | #endif |
1da177e4 | 217 | } |
ec4e2906 | 218 | EXPORT_SYMBOL_GPL(platform_get_irq_optional); |
7723f4c5 SB |
219 | |
220 | /** | |
221 | * platform_get_irq - get an IRQ for a device | |
222 | * @dev: platform device | |
223 | * @num: IRQ number index | |
224 | * | |
225 | * Gets an IRQ for a platform device and prints an error message if finding the | |
226 | * IRQ fails. Device drivers should check the return value for errors so as to | |
227 | * not pass a negative integer value to the request_irq() APIs. | |
228 | * | |
229 | * Example: | |
230 | * int irq = platform_get_irq(pdev, 0); | |
231 | * if (irq < 0) | |
232 | * return irq; | |
233 | * | |
234 | * Return: IRQ number on success, negative error number on failure. | |
235 | */ | |
236 | int platform_get_irq(struct platform_device *dev, unsigned int num) | |
237 | { | |
238 | int ret; | |
239 | ||
ec4e2906 | 240 | ret = platform_get_irq_optional(dev, num); |
7723f4c5 SB |
241 | if (ret < 0 && ret != -EPROBE_DEFER) |
242 | dev_err(&dev->dev, "IRQ index %u not found\n", num); | |
243 | ||
244 | return ret; | |
245 | } | |
a96b2042 | 246 | EXPORT_SYMBOL_GPL(platform_get_irq); |
1da177e4 | 247 | |
4b83555d SB |
248 | /** |
249 | * platform_irq_count - Count the number of IRQs a platform device uses | |
250 | * @dev: platform device | |
251 | * | |
252 | * Return: Number of IRQs a platform device uses or EPROBE_DEFER | |
253 | */ | |
254 | int platform_irq_count(struct platform_device *dev) | |
255 | { | |
256 | int ret, nr = 0; | |
257 | ||
ec4e2906 | 258 | while ((ret = platform_get_irq_optional(dev, nr)) >= 0) |
4b83555d SB |
259 | nr++; |
260 | ||
261 | if (ret == -EPROBE_DEFER) | |
262 | return ret; | |
263 | ||
264 | return nr; | |
265 | } | |
266 | EXPORT_SYMBOL_GPL(platform_irq_count); | |
267 | ||
1da177e4 | 268 | /** |
4a3ad20c GKH |
269 | * platform_get_resource_byname - get a resource for a device by name |
270 | * @dev: platform device | |
271 | * @type: resource type | |
272 | * @name: resource name | |
1da177e4 | 273 | */ |
4a3ad20c | 274 | struct resource *platform_get_resource_byname(struct platform_device *dev, |
c0afe7ba LW |
275 | unsigned int type, |
276 | const char *name) | |
1da177e4 | 277 | { |
39cc539f | 278 | u32 i; |
1da177e4 LT |
279 | |
280 | for (i = 0; i < dev->num_resources; i++) { | |
281 | struct resource *r = &dev->resource[i]; | |
282 | ||
1b8cb929 PU |
283 | if (unlikely(!r->name)) |
284 | continue; | |
285 | ||
c9f66169 MD |
286 | if (type == resource_type(r) && !strcmp(r->name, name)) |
287 | return r; | |
1da177e4 LT |
288 | } |
289 | return NULL; | |
290 | } | |
a96b2042 | 291 | EXPORT_SYMBOL_GPL(platform_get_resource_byname); |
1da177e4 | 292 | |
f1da567f HG |
293 | static int __platform_get_irq_byname(struct platform_device *dev, |
294 | const char *name) | |
1da177e4 | 295 | { |
ad69674e | 296 | struct resource *r; |
71564a26 | 297 | int ret; |
ad69674e | 298 | |
aff008ad | 299 | if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) { |
aff008ad | 300 | ret = of_irq_get_byname(dev->dev.of_node, name); |
e330b9a6 | 301 | if (ret > 0 || ret == -EPROBE_DEFER) |
aff008ad GR |
302 | return ret; |
303 | } | |
1da177e4 | 304 | |
ad69674e | 305 | r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name); |
7723f4c5 SB |
306 | if (r) |
307 | return r->start; | |
308 | ||
7723f4c5 | 309 | return -ENXIO; |
1da177e4 | 310 | } |
f1da567f HG |
311 | |
312 | /** | |
313 | * platform_get_irq_byname - get an IRQ for a device by name | |
314 | * @dev: platform device | |
315 | * @name: IRQ name | |
316 | * | |
317 | * Get an IRQ like platform_get_irq(), but then by name rather then by index. | |
318 | * | |
319 | * Return: IRQ number on success, negative error number on failure. | |
320 | */ | |
321 | int platform_get_irq_byname(struct platform_device *dev, const char *name) | |
322 | { | |
323 | int ret; | |
324 | ||
325 | ret = __platform_get_irq_byname(dev, name); | |
326 | if (ret < 0 && ret != -EPROBE_DEFER) | |
327 | dev_err(&dev->dev, "IRQ %s not found\n", name); | |
328 | ||
329 | return ret; | |
330 | } | |
a96b2042 | 331 | EXPORT_SYMBOL_GPL(platform_get_irq_byname); |
1da177e4 | 332 | |
f1da567f HG |
333 | /** |
334 | * platform_get_irq_byname_optional - get an optional IRQ for a device by name | |
335 | * @dev: platform device | |
336 | * @name: IRQ name | |
337 | * | |
338 | * Get an optional IRQ by name like platform_get_irq_byname(). Except that it | |
339 | * does not print an error message if an IRQ can not be obtained. | |
340 | * | |
341 | * Return: IRQ number on success, negative error number on failure. | |
342 | */ | |
343 | int platform_get_irq_byname_optional(struct platform_device *dev, | |
344 | const char *name) | |
345 | { | |
346 | return __platform_get_irq_byname(dev, name); | |
347 | } | |
348 | EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional); | |
349 | ||
1da177e4 | 350 | /** |
4a3ad20c GKH |
351 | * platform_add_devices - add a numbers of platform devices |
352 | * @devs: array of platform devices to add | |
353 | * @num: number of platform devices in array | |
1da177e4 LT |
354 | */ |
355 | int platform_add_devices(struct platform_device **devs, int num) | |
356 | { | |
357 | int i, ret = 0; | |
358 | ||
359 | for (i = 0; i < num; i++) { | |
360 | ret = platform_device_register(devs[i]); | |
361 | if (ret) { | |
362 | while (--i >= 0) | |
363 | platform_device_unregister(devs[i]); | |
364 | break; | |
365 | } | |
366 | } | |
367 | ||
368 | return ret; | |
369 | } | |
a96b2042 | 370 | EXPORT_SYMBOL_GPL(platform_add_devices); |
1da177e4 | 371 | |
37c12e74 RK |
372 | struct platform_object { |
373 | struct platform_device pdev; | |
1cec24c5 | 374 | char name[]; |
37c12e74 RK |
375 | }; |
376 | ||
cdfee562 CH |
377 | /* |
378 | * Set up default DMA mask for platform devices if the they weren't | |
379 | * previously set by the architecture / DT. | |
380 | */ | |
381 | static void setup_pdev_dma_masks(struct platform_device *pdev) | |
382 | { | |
9495b7e9 UH |
383 | pdev->dev.dma_parms = &pdev->dma_parms; |
384 | ||
cdfee562 CH |
385 | if (!pdev->dev.coherent_dma_mask) |
386 | pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); | |
e3a36eb6 CH |
387 | if (!pdev->dev.dma_mask) { |
388 | pdev->platform_dma_mask = DMA_BIT_MASK(32); | |
389 | pdev->dev.dma_mask = &pdev->platform_dma_mask; | |
390 | } | |
cdfee562 CH |
391 | }; |
392 | ||
1da177e4 | 393 | /** |
3c31f07a | 394 | * platform_device_put - destroy a platform device |
4a3ad20c | 395 | * @pdev: platform device to free |
37c12e74 | 396 | * |
4a3ad20c GKH |
397 | * Free all memory associated with a platform device. This function must |
398 | * _only_ be externally called in error cases. All other usage is a bug. | |
37c12e74 RK |
399 | */ |
400 | void platform_device_put(struct platform_device *pdev) | |
401 | { | |
99fef587 | 402 | if (!IS_ERR_OR_NULL(pdev)) |
37c12e74 RK |
403 | put_device(&pdev->dev); |
404 | } | |
405 | EXPORT_SYMBOL_GPL(platform_device_put); | |
406 | ||
407 | static void platform_device_release(struct device *dev) | |
408 | { | |
4a3ad20c GKH |
409 | struct platform_object *pa = container_of(dev, struct platform_object, |
410 | pdev.dev); | |
37c12e74 | 411 | |
7096d042 | 412 | of_device_node_put(&pa->pdev.dev); |
37c12e74 | 413 | kfree(pa->pdev.dev.platform_data); |
e710d7d5 | 414 | kfree(pa->pdev.mfd_cell); |
37c12e74 | 415 | kfree(pa->pdev.resource); |
3d713e0e | 416 | kfree(pa->pdev.driver_override); |
37c12e74 RK |
417 | kfree(pa); |
418 | } | |
419 | ||
420 | /** | |
3c31f07a | 421 | * platform_device_alloc - create a platform device |
4a3ad20c GKH |
422 | * @name: base name of the device we're adding |
423 | * @id: instance id | |
37c12e74 | 424 | * |
4a3ad20c GKH |
425 | * Create a platform device object which can have other objects attached |
426 | * to it, and which will have attached objects freed when it is released. | |
37c12e74 | 427 | */ |
1359555e | 428 | struct platform_device *platform_device_alloc(const char *name, int id) |
37c12e74 RK |
429 | { |
430 | struct platform_object *pa; | |
431 | ||
1cec24c5 | 432 | pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL); |
37c12e74 RK |
433 | if (pa) { |
434 | strcpy(pa->name, name); | |
435 | pa->pdev.name = pa->name; | |
436 | pa->pdev.id = id; | |
437 | device_initialize(&pa->pdev.dev); | |
438 | pa->pdev.dev.release = platform_device_release; | |
cdfee562 | 439 | setup_pdev_dma_masks(&pa->pdev); |
37c12e74 RK |
440 | } |
441 | ||
93ce3061 | 442 | return pa ? &pa->pdev : NULL; |
37c12e74 RK |
443 | } |
444 | EXPORT_SYMBOL_GPL(platform_device_alloc); | |
445 | ||
446 | /** | |
3c31f07a | 447 | * platform_device_add_resources - add resources to a platform device |
4a3ad20c GKH |
448 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
449 | * @res: set of resources that needs to be allocated for the device | |
450 | * @num: number of resources | |
37c12e74 | 451 | * |
4a3ad20c GKH |
452 | * Add a copy of the resources to the platform device. The memory |
453 | * associated with the resources will be freed when the platform device is | |
454 | * released. | |
37c12e74 | 455 | */ |
4a3ad20c | 456 | int platform_device_add_resources(struct platform_device *pdev, |
0b7f1a7e | 457 | const struct resource *res, unsigned int num) |
37c12e74 | 458 | { |
cea89623 | 459 | struct resource *r = NULL; |
37c12e74 | 460 | |
cea89623 UKK |
461 | if (res) { |
462 | r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL); | |
463 | if (!r) | |
464 | return -ENOMEM; | |
37c12e74 | 465 | } |
cea89623 | 466 | |
4a03d6f7 | 467 | kfree(pdev->resource); |
cea89623 UKK |
468 | pdev->resource = r; |
469 | pdev->num_resources = num; | |
470 | return 0; | |
37c12e74 RK |
471 | } |
472 | EXPORT_SYMBOL_GPL(platform_device_add_resources); | |
473 | ||
474 | /** | |
3c31f07a | 475 | * platform_device_add_data - add platform-specific data to a platform device |
4a3ad20c GKH |
476 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
477 | * @data: platform specific data for this platform device | |
478 | * @size: size of platform specific data | |
37c12e74 | 479 | * |
4a3ad20c GKH |
480 | * Add a copy of platform specific data to the platform device's |
481 | * platform_data pointer. The memory associated with the platform data | |
482 | * will be freed when the platform device is released. | |
37c12e74 | 483 | */ |
4a3ad20c GKH |
484 | int platform_device_add_data(struct platform_device *pdev, const void *data, |
485 | size_t size) | |
37c12e74 | 486 | { |
27a33f9e | 487 | void *d = NULL; |
5cfc64ce | 488 | |
27a33f9e UKK |
489 | if (data) { |
490 | d = kmemdup(data, size, GFP_KERNEL); | |
491 | if (!d) | |
492 | return -ENOMEM; | |
37c12e74 | 493 | } |
27a33f9e | 494 | |
251e031d | 495 | kfree(pdev->dev.platform_data); |
27a33f9e UKK |
496 | pdev->dev.platform_data = d; |
497 | return 0; | |
37c12e74 RK |
498 | } |
499 | EXPORT_SYMBOL_GPL(platform_device_add_data); | |
500 | ||
00bbc1d8 MW |
501 | /** |
502 | * platform_device_add_properties - add built-in properties to a platform device | |
503 | * @pdev: platform device to add properties to | |
f4d05266 | 504 | * @properties: null terminated array of properties to add |
00bbc1d8 | 505 | * |
f4d05266 HK |
506 | * The function will take deep copy of @properties and attach the copy to the |
507 | * platform device. The memory associated with properties will be freed when the | |
508 | * platform device is released. | |
00bbc1d8 MW |
509 | */ |
510 | int platform_device_add_properties(struct platform_device *pdev, | |
277036f0 | 511 | const struct property_entry *properties) |
00bbc1d8 | 512 | { |
f4d05266 | 513 | return device_add_properties(&pdev->dev, properties); |
00bbc1d8 MW |
514 | } |
515 | EXPORT_SYMBOL_GPL(platform_device_add_properties); | |
516 | ||
37c12e74 | 517 | /** |
4a3ad20c GKH |
518 | * platform_device_add - add a platform device to device hierarchy |
519 | * @pdev: platform device we're adding | |
1da177e4 | 520 | * |
4a3ad20c GKH |
521 | * This is part 2 of platform_device_register(), though may be called |
522 | * separately _iff_ pdev was allocated by platform_device_alloc(). | |
1da177e4 | 523 | */ |
37c12e74 | 524 | int platform_device_add(struct platform_device *pdev) |
1da177e4 | 525 | { |
39cc539f SS |
526 | u32 i; |
527 | int ret; | |
1da177e4 LT |
528 | |
529 | if (!pdev) | |
530 | return -EINVAL; | |
531 | ||
532 | if (!pdev->dev.parent) | |
533 | pdev->dev.parent = &platform_bus; | |
534 | ||
535 | pdev->dev.bus = &platform_bus_type; | |
536 | ||
689ae231 JD |
537 | switch (pdev->id) { |
538 | default: | |
1e0b2cf9 | 539 | dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); |
689ae231 JD |
540 | break; |
541 | case PLATFORM_DEVID_NONE: | |
acc0e90f | 542 | dev_set_name(&pdev->dev, "%s", pdev->name); |
689ae231 JD |
543 | break; |
544 | case PLATFORM_DEVID_AUTO: | |
545 | /* | |
546 | * Automatically allocated device ID. We mark it as such so | |
547 | * that we remember it must be freed, and we append a suffix | |
548 | * to avoid namespace collision with explicit IDs. | |
549 | */ | |
550 | ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL); | |
551 | if (ret < 0) | |
5da7f709 | 552 | goto err_out; |
689ae231 JD |
553 | pdev->id = ret; |
554 | pdev->id_auto = true; | |
555 | dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id); | |
556 | break; | |
557 | } | |
1da177e4 LT |
558 | |
559 | for (i = 0; i < pdev->num_resources; i++) { | |
5da7f709 | 560 | struct resource *p, *r = &pdev->resource[i]; |
1da177e4 LT |
561 | |
562 | if (r->name == NULL) | |
1e0b2cf9 | 563 | r->name = dev_name(&pdev->dev); |
1da177e4 LT |
564 | |
565 | p = r->parent; | |
566 | if (!p) { | |
0e6c861f | 567 | if (resource_type(r) == IORESOURCE_MEM) |
1da177e4 | 568 | p = &iomem_resource; |
0e6c861f | 569 | else if (resource_type(r) == IORESOURCE_IO) |
1da177e4 LT |
570 | p = &ioport_resource; |
571 | } | |
572 | ||
25ebcb7d AS |
573 | if (p) { |
574 | ret = insert_resource(p, r); | |
575 | if (ret) { | |
576 | dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r); | |
577 | goto failed; | |
578 | } | |
5da7f709 | 579 | } |
1da177e4 LT |
580 | } |
581 | ||
582 | pr_debug("Registering platform device '%s'. Parent at %s\n", | |
1e0b2cf9 | 583 | dev_name(&pdev->dev), dev_name(pdev->dev.parent)); |
1da177e4 | 584 | |
e3915532 | 585 | ret = device_add(&pdev->dev); |
8b2dceba GKH |
586 | if (ret == 0) |
587 | return ret; | |
588 | ||
5da7f709 | 589 | failed: |
8b2dceba GKH |
590 | if (pdev->id_auto) { |
591 | ida_simple_remove(&platform_devid_ida, pdev->id); | |
592 | pdev->id = PLATFORM_DEVID_AUTO; | |
593 | } | |
594 | ||
0707cfa5 | 595 | while (i--) { |
8b2dceba | 596 | struct resource *r = &pdev->resource[i]; |
7f5dcaf1 | 597 | if (r->parent) |
8b2dceba GKH |
598 | release_resource(r); |
599 | } | |
c9f66169 | 600 | |
5da7f709 | 601 | err_out: |
1da177e4 LT |
602 | return ret; |
603 | } | |
37c12e74 RK |
604 | EXPORT_SYMBOL_GPL(platform_device_add); |
605 | ||
606 | /** | |
4a3ad20c GKH |
607 | * platform_device_del - remove a platform-level device |
608 | * @pdev: platform device we're removing | |
1da177e4 | 609 | * |
4a3ad20c GKH |
610 | * Note that this function will also release all memory- and port-based |
611 | * resources owned by the device (@dev->resource). This function must | |
612 | * _only_ be externally called in error cases. All other usage is a bug. | |
1da177e4 | 613 | */ |
93ce3061 | 614 | void platform_device_del(struct platform_device *pdev) |
1da177e4 | 615 | { |
39cc539f | 616 | u32 i; |
c9f66169 | 617 | |
99fef587 | 618 | if (!IS_ERR_OR_NULL(pdev)) { |
8b2dceba GKH |
619 | device_del(&pdev->dev); |
620 | ||
621 | if (pdev->id_auto) { | |
622 | ida_simple_remove(&platform_devid_ida, pdev->id); | |
623 | pdev->id = PLATFORM_DEVID_AUTO; | |
624 | } | |
625 | ||
626 | for (i = 0; i < pdev->num_resources; i++) { | |
627 | struct resource *r = &pdev->resource[i]; | |
7f5dcaf1 | 628 | if (r->parent) |
8b2dceba GKH |
629 | release_resource(r); |
630 | } | |
631 | } | |
1da177e4 | 632 | } |
93ce3061 DT |
633 | EXPORT_SYMBOL_GPL(platform_device_del); |
634 | ||
635 | /** | |
4a3ad20c GKH |
636 | * platform_device_register - add a platform-level device |
637 | * @pdev: platform device we're adding | |
93ce3061 | 638 | */ |
4a3ad20c | 639 | int platform_device_register(struct platform_device *pdev) |
93ce3061 DT |
640 | { |
641 | device_initialize(&pdev->dev); | |
cdfee562 | 642 | setup_pdev_dma_masks(pdev); |
93ce3061 DT |
643 | return platform_device_add(pdev); |
644 | } | |
a96b2042 | 645 | EXPORT_SYMBOL_GPL(platform_device_register); |
93ce3061 DT |
646 | |
647 | /** | |
4a3ad20c GKH |
648 | * platform_device_unregister - unregister a platform-level device |
649 | * @pdev: platform device we're unregistering | |
93ce3061 | 650 | * |
4a3ad20c GKH |
651 | * Unregistration is done in 2 steps. First we release all resources |
652 | * and remove it from the subsystem, then we drop reference count by | |
653 | * calling platform_device_put(). | |
93ce3061 | 654 | */ |
4a3ad20c | 655 | void platform_device_unregister(struct platform_device *pdev) |
93ce3061 DT |
656 | { |
657 | platform_device_del(pdev); | |
658 | platform_device_put(pdev); | |
659 | } | |
a96b2042 | 660 | EXPORT_SYMBOL_GPL(platform_device_unregister); |
1da177e4 | 661 | |
1da177e4 | 662 | /** |
01dcc60a | 663 | * platform_device_register_full - add a platform-level device with |
44f28bde | 664 | * resources and platform-specific data |
49a4ec18 | 665 | * |
01dcc60a | 666 | * @pdevinfo: data used to create device |
d8bf2540 | 667 | * |
f0eae0ed | 668 | * Returns &struct platform_device pointer on success, or ERR_PTR() on error. |
d8bf2540 | 669 | */ |
01dcc60a | 670 | struct platform_device *platform_device_register_full( |
5a3072be | 671 | const struct platform_device_info *pdevinfo) |
d8bf2540 | 672 | { |
44f28bde | 673 | int ret = -ENOMEM; |
d8bf2540 | 674 | struct platform_device *pdev; |
d8bf2540 | 675 | |
01dcc60a | 676 | pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id); |
44f28bde | 677 | if (!pdev) |
36cf3b13 | 678 | return ERR_PTR(-ENOMEM); |
01dcc60a UKK |
679 | |
680 | pdev->dev.parent = pdevinfo->parent; | |
ce793486 | 681 | pdev->dev.fwnode = pdevinfo->fwnode; |
2c1ea6ab MR |
682 | pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode)); |
683 | pdev->dev.of_node_reused = pdevinfo->of_node_reused; | |
01dcc60a UKK |
684 | |
685 | if (pdevinfo->dma_mask) { | |
e3a36eb6 CH |
686 | pdev->platform_dma_mask = pdevinfo->dma_mask; |
687 | pdev->dev.dma_mask = &pdev->platform_dma_mask; | |
01dcc60a UKK |
688 | pdev->dev.coherent_dma_mask = pdevinfo->dma_mask; |
689 | } | |
d8bf2540 | 690 | |
01dcc60a UKK |
691 | ret = platform_device_add_resources(pdev, |
692 | pdevinfo->res, pdevinfo->num_res); | |
807508c8 AV |
693 | if (ret) |
694 | goto err; | |
44f28bde | 695 | |
01dcc60a UKK |
696 | ret = platform_device_add_data(pdev, |
697 | pdevinfo->data, pdevinfo->size_data); | |
807508c8 AV |
698 | if (ret) |
699 | goto err; | |
d8bf2540 | 700 | |
f4d05266 HK |
701 | if (pdevinfo->properties) { |
702 | ret = platform_device_add_properties(pdev, | |
703 | pdevinfo->properties); | |
00bbc1d8 MW |
704 | if (ret) |
705 | goto err; | |
706 | } | |
707 | ||
44f28bde UKK |
708 | ret = platform_device_add(pdev); |
709 | if (ret) { | |
710 | err: | |
7b199811 | 711 | ACPI_COMPANION_SET(&pdev->dev, NULL); |
44f28bde UKK |
712 | platform_device_put(pdev); |
713 | return ERR_PTR(ret); | |
714 | } | |
d8bf2540 DB |
715 | |
716 | return pdev; | |
d8bf2540 | 717 | } |
01dcc60a | 718 | EXPORT_SYMBOL_GPL(platform_device_register_full); |
d8bf2540 | 719 | |
00d3dcdd RK |
720 | static int platform_drv_probe(struct device *_dev) |
721 | { | |
722 | struct platform_driver *drv = to_platform_driver(_dev->driver); | |
723 | struct platform_device *dev = to_platform_device(_dev); | |
94d76d5d | 724 | int ret; |
00d3dcdd | 725 | |
86be408b SN |
726 | ret = of_clk_set_defaults(_dev->of_node, false); |
727 | if (ret < 0) | |
728 | return ret; | |
729 | ||
cb518413 | 730 | ret = dev_pm_domain_attach(_dev, true); |
88a9769e UH |
731 | if (ret) |
732 | goto out; | |
733 | ||
734 | if (drv->probe) { | |
735 | ret = drv->probe(dev); | |
736 | if (ret) | |
737 | dev_pm_domain_detach(_dev, true); | |
cb518413 | 738 | } |
94d76d5d | 739 | |
88a9769e | 740 | out: |
3f9120b0 JH |
741 | if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) { |
742 | dev_warn(_dev, "probe deferral not supported\n"); | |
743 | ret = -ENXIO; | |
744 | } | |
745 | ||
94d76d5d | 746 | return ret; |
00d3dcdd RK |
747 | } |
748 | ||
c67334fb DB |
749 | static int platform_drv_probe_fail(struct device *_dev) |
750 | { | |
751 | return -ENXIO; | |
752 | } | |
753 | ||
00d3dcdd RK |
754 | static int platform_drv_remove(struct device *_dev) |
755 | { | |
756 | struct platform_driver *drv = to_platform_driver(_dev->driver); | |
757 | struct platform_device *dev = to_platform_device(_dev); | |
b8b2c7d8 | 758 | int ret = 0; |
00d3dcdd | 759 | |
b8b2c7d8 UKK |
760 | if (drv->remove) |
761 | ret = drv->remove(dev); | |
cb518413 | 762 | dev_pm_domain_detach(_dev, true); |
94d76d5d RW |
763 | |
764 | return ret; | |
00d3dcdd RK |
765 | } |
766 | ||
767 | static void platform_drv_shutdown(struct device *_dev) | |
768 | { | |
769 | struct platform_driver *drv = to_platform_driver(_dev->driver); | |
770 | struct platform_device *dev = to_platform_device(_dev); | |
771 | ||
b8b2c7d8 UKK |
772 | if (drv->shutdown) |
773 | drv->shutdown(dev); | |
00d3dcdd RK |
774 | } |
775 | ||
00d3dcdd | 776 | /** |
9447057e | 777 | * __platform_driver_register - register a driver for platform-level devices |
4a3ad20c | 778 | * @drv: platform driver structure |
08801f96 | 779 | * @owner: owning module/driver |
00d3dcdd | 780 | */ |
9447057e LC |
781 | int __platform_driver_register(struct platform_driver *drv, |
782 | struct module *owner) | |
00d3dcdd | 783 | { |
9447057e | 784 | drv->driver.owner = owner; |
00d3dcdd | 785 | drv->driver.bus = &platform_bus_type; |
b8b2c7d8 UKK |
786 | drv->driver.probe = platform_drv_probe; |
787 | drv->driver.remove = platform_drv_remove; | |
788 | drv->driver.shutdown = platform_drv_shutdown; | |
783ea7d4 | 789 | |
00d3dcdd RK |
790 | return driver_register(&drv->driver); |
791 | } | |
9447057e | 792 | EXPORT_SYMBOL_GPL(__platform_driver_register); |
00d3dcdd RK |
793 | |
794 | /** | |
3c31f07a | 795 | * platform_driver_unregister - unregister a driver for platform-level devices |
4a3ad20c | 796 | * @drv: platform driver structure |
00d3dcdd RK |
797 | */ |
798 | void platform_driver_unregister(struct platform_driver *drv) | |
799 | { | |
800 | driver_unregister(&drv->driver); | |
801 | } | |
802 | EXPORT_SYMBOL_GPL(platform_driver_unregister); | |
803 | ||
c67334fb | 804 | /** |
c3b50dc2 | 805 | * __platform_driver_probe - register driver for non-hotpluggable device |
c67334fb | 806 | * @drv: platform driver structure |
3f9120b0 | 807 | * @probe: the driver probe routine, probably from an __init section |
c3b50dc2 | 808 | * @module: module which will be the owner of the driver |
c67334fb DB |
809 | * |
810 | * Use this instead of platform_driver_register() when you know the device | |
811 | * is not hotpluggable and has already been registered, and you want to | |
812 | * remove its run-once probe() infrastructure from memory after the driver | |
813 | * has bound to the device. | |
814 | * | |
815 | * One typical use for this would be with drivers for controllers integrated | |
816 | * into system-on-chip processors, where the controller devices have been | |
817 | * configured as part of board setup. | |
818 | * | |
3f9120b0 | 819 | * Note that this is incompatible with deferred probing. |
647c86d0 | 820 | * |
c67334fb DB |
821 | * Returns zero if the driver registered and bound to a device, else returns |
822 | * a negative error code and with the driver not registered. | |
823 | */ | |
c3b50dc2 WS |
824 | int __init_or_module __platform_driver_probe(struct platform_driver *drv, |
825 | int (*probe)(struct platform_device *), struct module *module) | |
c67334fb DB |
826 | { |
827 | int retval, code; | |
828 | ||
5c36eb2a DT |
829 | if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) { |
830 | pr_err("%s: drivers registered with %s can not be probed asynchronously\n", | |
831 | drv->driver.name, __func__); | |
832 | return -EINVAL; | |
833 | } | |
834 | ||
835 | /* | |
836 | * We have to run our probes synchronously because we check if | |
837 | * we find any devices to bind to and exit with error if there | |
838 | * are any. | |
839 | */ | |
840 | drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS; | |
841 | ||
3f9120b0 JH |
842 | /* |
843 | * Prevent driver from requesting probe deferral to avoid further | |
844 | * futile probe attempts. | |
845 | */ | |
846 | drv->prevent_deferred_probe = true; | |
847 | ||
1a6f2a75 DT |
848 | /* make sure driver won't have bind/unbind attributes */ |
849 | drv->driver.suppress_bind_attrs = true; | |
850 | ||
c67334fb DB |
851 | /* temporary section violation during probe() */ |
852 | drv->probe = probe; | |
c3b50dc2 | 853 | retval = code = __platform_driver_register(drv, module); |
c67334fb | 854 | |
1a6f2a75 DT |
855 | /* |
856 | * Fixup that section violation, being paranoid about code scanning | |
c67334fb DB |
857 | * the list of drivers in order to probe new devices. Check to see |
858 | * if the probe was successful, and make sure any forced probes of | |
859 | * new devices fail. | |
860 | */ | |
d79d3244 | 861 | spin_lock(&drv->driver.bus->p->klist_drivers.k_lock); |
c67334fb | 862 | drv->probe = NULL; |
e5dd1278 | 863 | if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list)) |
c67334fb DB |
864 | retval = -ENODEV; |
865 | drv->driver.probe = platform_drv_probe_fail; | |
d79d3244 | 866 | spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock); |
c67334fb DB |
867 | |
868 | if (code != retval) | |
869 | platform_driver_unregister(drv); | |
870 | return retval; | |
871 | } | |
c3b50dc2 | 872 | EXPORT_SYMBOL_GPL(__platform_driver_probe); |
1da177e4 | 873 | |
ecdf6ceb | 874 | /** |
291f653a | 875 | * __platform_create_bundle - register driver and create corresponding device |
ecdf6ceb DT |
876 | * @driver: platform driver structure |
877 | * @probe: the driver probe routine, probably from an __init section | |
878 | * @res: set of resources that needs to be allocated for the device | |
879 | * @n_res: number of resources | |
880 | * @data: platform specific data for this platform device | |
881 | * @size: size of platform specific data | |
291f653a | 882 | * @module: module which will be the owner of the driver |
ecdf6ceb DT |
883 | * |
884 | * Use this in legacy-style modules that probe hardware directly and | |
885 | * register a single platform device and corresponding platform driver. | |
f0eae0ed JN |
886 | * |
887 | * Returns &struct platform_device pointer on success, or ERR_PTR() on error. | |
ecdf6ceb | 888 | */ |
291f653a | 889 | struct platform_device * __init_or_module __platform_create_bundle( |
ecdf6ceb DT |
890 | struct platform_driver *driver, |
891 | int (*probe)(struct platform_device *), | |
892 | struct resource *res, unsigned int n_res, | |
291f653a | 893 | const void *data, size_t size, struct module *module) |
ecdf6ceb DT |
894 | { |
895 | struct platform_device *pdev; | |
896 | int error; | |
897 | ||
898 | pdev = platform_device_alloc(driver->driver.name, -1); | |
899 | if (!pdev) { | |
900 | error = -ENOMEM; | |
901 | goto err_out; | |
902 | } | |
903 | ||
807508c8 AV |
904 | error = platform_device_add_resources(pdev, res, n_res); |
905 | if (error) | |
906 | goto err_pdev_put; | |
ecdf6ceb | 907 | |
807508c8 AV |
908 | error = platform_device_add_data(pdev, data, size); |
909 | if (error) | |
910 | goto err_pdev_put; | |
ecdf6ceb DT |
911 | |
912 | error = platform_device_add(pdev); | |
913 | if (error) | |
914 | goto err_pdev_put; | |
915 | ||
291f653a | 916 | error = __platform_driver_probe(driver, probe, module); |
ecdf6ceb DT |
917 | if (error) |
918 | goto err_pdev_del; | |
919 | ||
920 | return pdev; | |
921 | ||
922 | err_pdev_del: | |
923 | platform_device_del(pdev); | |
924 | err_pdev_put: | |
925 | platform_device_put(pdev); | |
926 | err_out: | |
927 | return ERR_PTR(error); | |
928 | } | |
291f653a | 929 | EXPORT_SYMBOL_GPL(__platform_create_bundle); |
ecdf6ceb | 930 | |
dbe2256d TR |
931 | /** |
932 | * __platform_register_drivers - register an array of platform drivers | |
933 | * @drivers: an array of drivers to register | |
934 | * @count: the number of drivers to register | |
935 | * @owner: module owning the drivers | |
936 | * | |
937 | * Registers platform drivers specified by an array. On failure to register a | |
938 | * driver, all previously registered drivers will be unregistered. Callers of | |
939 | * this API should use platform_unregister_drivers() to unregister drivers in | |
940 | * the reverse order. | |
941 | * | |
942 | * Returns: 0 on success or a negative error code on failure. | |
943 | */ | |
944 | int __platform_register_drivers(struct platform_driver * const *drivers, | |
945 | unsigned int count, struct module *owner) | |
946 | { | |
947 | unsigned int i; | |
948 | int err; | |
949 | ||
950 | for (i = 0; i < count; i++) { | |
951 | pr_debug("registering platform driver %ps\n", drivers[i]); | |
952 | ||
953 | err = __platform_driver_register(drivers[i], owner); | |
954 | if (err < 0) { | |
955 | pr_err("failed to register platform driver %ps: %d\n", | |
956 | drivers[i], err); | |
957 | goto error; | |
958 | } | |
959 | } | |
960 | ||
961 | return 0; | |
962 | ||
963 | error: | |
964 | while (i--) { | |
965 | pr_debug("unregistering platform driver %ps\n", drivers[i]); | |
966 | platform_driver_unregister(drivers[i]); | |
967 | } | |
968 | ||
969 | return err; | |
970 | } | |
971 | EXPORT_SYMBOL_GPL(__platform_register_drivers); | |
972 | ||
973 | /** | |
974 | * platform_unregister_drivers - unregister an array of platform drivers | |
975 | * @drivers: an array of drivers to unregister | |
976 | * @count: the number of drivers to unregister | |
977 | * | |
978 | * Unegisters platform drivers specified by an array. This is typically used | |
979 | * to complement an earlier call to platform_register_drivers(). Drivers are | |
980 | * unregistered in the reverse order in which they were registered. | |
981 | */ | |
982 | void platform_unregister_drivers(struct platform_driver * const *drivers, | |
983 | unsigned int count) | |
984 | { | |
985 | while (count--) { | |
986 | pr_debug("unregistering platform driver %ps\n", drivers[count]); | |
987 | platform_driver_unregister(drivers[count]); | |
988 | } | |
989 | } | |
990 | EXPORT_SYMBOL_GPL(platform_unregister_drivers); | |
991 | ||
a0245f7a DB |
992 | /* modalias support enables more hands-off userspace setup: |
993 | * (a) environment variable lets new-style hotplug events work once system is | |
994 | * fully running: "modprobe $MODALIAS" | |
995 | * (b) sysfs attribute lets new-style coldplug recover from hotplug events | |
996 | * mishandled before system is fully running: "modprobe $(cat modalias)" | |
997 | */ | |
4a3ad20c GKH |
998 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
999 | char *buf) | |
a0245f7a DB |
1000 | { |
1001 | struct platform_device *pdev = to_platform_device(dev); | |
8c4ff6d0 ZR |
1002 | int len; |
1003 | ||
0634c295 | 1004 | len = of_device_modalias(dev, buf, PAGE_SIZE); |
b9f73067 ZR |
1005 | if (len != -ENODEV) |
1006 | return len; | |
1007 | ||
8c4ff6d0 ZR |
1008 | len = acpi_device_modalias(dev, buf, PAGE_SIZE -1); |
1009 | if (len != -ENODEV) | |
1010 | return len; | |
1011 | ||
391c0325 | 1012 | len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name); |
a0245f7a DB |
1013 | |
1014 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; | |
1015 | } | |
d06262e5 | 1016 | static DEVICE_ATTR_RO(modalias); |
a0245f7a | 1017 | |
3d713e0e KP |
1018 | static ssize_t driver_override_store(struct device *dev, |
1019 | struct device_attribute *attr, | |
1020 | const char *buf, size_t count) | |
1021 | { | |
1022 | struct platform_device *pdev = to_platform_device(dev); | |
62655397 | 1023 | char *driver_override, *old, *cp; |
3d713e0e | 1024 | |
bf563b01 NS |
1025 | /* We need to keep extra room for a newline */ |
1026 | if (count >= (PAGE_SIZE - 1)) | |
3d713e0e KP |
1027 | return -EINVAL; |
1028 | ||
1029 | driver_override = kstrndup(buf, count, GFP_KERNEL); | |
1030 | if (!driver_override) | |
1031 | return -ENOMEM; | |
1032 | ||
1033 | cp = strchr(driver_override, '\n'); | |
1034 | if (cp) | |
1035 | *cp = '\0'; | |
1036 | ||
62655397 AS |
1037 | device_lock(dev); |
1038 | old = pdev->driver_override; | |
3d713e0e KP |
1039 | if (strlen(driver_override)) { |
1040 | pdev->driver_override = driver_override; | |
1041 | } else { | |
1042 | kfree(driver_override); | |
1043 | pdev->driver_override = NULL; | |
1044 | } | |
62655397 | 1045 | device_unlock(dev); |
3d713e0e KP |
1046 | |
1047 | kfree(old); | |
1048 | ||
1049 | return count; | |
1050 | } | |
1051 | ||
1052 | static ssize_t driver_override_show(struct device *dev, | |
1053 | struct device_attribute *attr, char *buf) | |
1054 | { | |
1055 | struct platform_device *pdev = to_platform_device(dev); | |
62655397 | 1056 | ssize_t len; |
3d713e0e | 1057 | |
62655397 AS |
1058 | device_lock(dev); |
1059 | len = sprintf(buf, "%s\n", pdev->driver_override); | |
1060 | device_unlock(dev); | |
1061 | return len; | |
3d713e0e KP |
1062 | } |
1063 | static DEVICE_ATTR_RW(driver_override); | |
1064 | ||
1065 | ||
d06262e5 GKH |
1066 | static struct attribute *platform_dev_attrs[] = { |
1067 | &dev_attr_modalias.attr, | |
3d713e0e | 1068 | &dev_attr_driver_override.attr, |
d06262e5 | 1069 | NULL, |
a0245f7a | 1070 | }; |
d06262e5 | 1071 | ATTRIBUTE_GROUPS(platform_dev); |
a0245f7a | 1072 | |
7eff2e7a | 1073 | static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) |
a0245f7a DB |
1074 | { |
1075 | struct platform_device *pdev = to_platform_device(dev); | |
eca39301 GL |
1076 | int rc; |
1077 | ||
1078 | /* Some devices have extra OF data and an OF-style MODALIAS */ | |
0258e182 | 1079 | rc = of_device_uevent_modalias(dev, env); |
eca39301 GL |
1080 | if (rc != -ENODEV) |
1081 | return rc; | |
a0245f7a | 1082 | |
8c4ff6d0 ZR |
1083 | rc = acpi_device_uevent_modalias(dev, env); |
1084 | if (rc != -ENODEV) | |
1085 | return rc; | |
1086 | ||
57fee4a5 | 1087 | add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, |
391c0325 | 1088 | pdev->name); |
a0245f7a DB |
1089 | return 0; |
1090 | } | |
1091 | ||
57fee4a5 | 1092 | static const struct platform_device_id *platform_match_id( |
831fad2f | 1093 | const struct platform_device_id *id, |
57fee4a5 EM |
1094 | struct platform_device *pdev) |
1095 | { | |
1096 | while (id->name[0]) { | |
391c0325 | 1097 | if (strcmp(pdev->name, id->name) == 0) { |
57fee4a5 EM |
1098 | pdev->id_entry = id; |
1099 | return id; | |
1100 | } | |
1101 | id++; | |
1102 | } | |
1103 | return NULL; | |
1104 | } | |
1105 | ||
1da177e4 | 1106 | /** |
4a3ad20c GKH |
1107 | * platform_match - bind platform device to platform driver. |
1108 | * @dev: device. | |
1109 | * @drv: driver. | |
1da177e4 | 1110 | * |
4a3ad20c GKH |
1111 | * Platform device IDs are assumed to be encoded like this: |
1112 | * "<name><instance>", where <name> is a short description of the type of | |
1113 | * device, like "pci" or "floppy", and <instance> is the enumerated | |
1114 | * instance of the device, like '0' or '42'. Driver IDs are simply | |
1115 | * "<name>". So, extract the <name> from the platform_device structure, | |
1116 | * and compare it against the name of the driver. Return whether they match | |
1117 | * or not. | |
1da177e4 | 1118 | */ |
4a3ad20c | 1119 | static int platform_match(struct device *dev, struct device_driver *drv) |
1da177e4 | 1120 | { |
71b3e0c1 | 1121 | struct platform_device *pdev = to_platform_device(dev); |
57fee4a5 EM |
1122 | struct platform_driver *pdrv = to_platform_driver(drv); |
1123 | ||
3d713e0e KP |
1124 | /* When driver_override is set, only bind to the matching driver */ |
1125 | if (pdev->driver_override) | |
1126 | return !strcmp(pdev->driver_override, drv->name); | |
1127 | ||
05212157 GL |
1128 | /* Attempt an OF style match first */ |
1129 | if (of_driver_match_device(dev, drv)) | |
1130 | return 1; | |
1131 | ||
91e56878 MW |
1132 | /* Then try ACPI style match */ |
1133 | if (acpi_driver_match_device(dev, drv)) | |
1134 | return 1; | |
1135 | ||
05212157 | 1136 | /* Then try to match against the id table */ |
57fee4a5 EM |
1137 | if (pdrv->id_table) |
1138 | return platform_match_id(pdrv->id_table, pdev) != NULL; | |
1da177e4 | 1139 | |
57fee4a5 | 1140 | /* fall-back to driver name match */ |
391c0325 | 1141 | return (strcmp(pdev->name, drv->name) == 0); |
1da177e4 LT |
1142 | } |
1143 | ||
25e18499 RW |
1144 | #ifdef CONFIG_PM_SLEEP |
1145 | ||
1146 | static int platform_legacy_suspend(struct device *dev, pm_message_t mesg) | |
1da177e4 | 1147 | { |
783ea7d4 MD |
1148 | struct platform_driver *pdrv = to_platform_driver(dev->driver); |
1149 | struct platform_device *pdev = to_platform_device(dev); | |
1da177e4 LT |
1150 | int ret = 0; |
1151 | ||
783ea7d4 MD |
1152 | if (dev->driver && pdrv->suspend) |
1153 | ret = pdrv->suspend(pdev, mesg); | |
386415d8 DB |
1154 | |
1155 | return ret; | |
1156 | } | |
1157 | ||
25e18499 | 1158 | static int platform_legacy_resume(struct device *dev) |
1da177e4 | 1159 | { |
783ea7d4 MD |
1160 | struct platform_driver *pdrv = to_platform_driver(dev->driver); |
1161 | struct platform_device *pdev = to_platform_device(dev); | |
1da177e4 LT |
1162 | int ret = 0; |
1163 | ||
783ea7d4 MD |
1164 | if (dev->driver && pdrv->resume) |
1165 | ret = pdrv->resume(pdev); | |
9480e307 | 1166 | |
1da177e4 LT |
1167 | return ret; |
1168 | } | |
1169 | ||
69c9dd1e | 1170 | #endif /* CONFIG_PM_SLEEP */ |
9d730229 | 1171 | |
25e18499 RW |
1172 | #ifdef CONFIG_SUSPEND |
1173 | ||
69c9dd1e | 1174 | int platform_pm_suspend(struct device *dev) |
25e18499 RW |
1175 | { |
1176 | struct device_driver *drv = dev->driver; | |
1177 | int ret = 0; | |
1178 | ||
adf09493 RW |
1179 | if (!drv) |
1180 | return 0; | |
1181 | ||
1182 | if (drv->pm) { | |
25e18499 RW |
1183 | if (drv->pm->suspend) |
1184 | ret = drv->pm->suspend(dev); | |
1185 | } else { | |
1186 | ret = platform_legacy_suspend(dev, PMSG_SUSPEND); | |
1187 | } | |
1188 | ||
1189 | return ret; | |
1190 | } | |
1191 | ||
69c9dd1e | 1192 | int platform_pm_resume(struct device *dev) |
25e18499 RW |
1193 | { |
1194 | struct device_driver *drv = dev->driver; | |
1195 | int ret = 0; | |
1196 | ||
adf09493 RW |
1197 | if (!drv) |
1198 | return 0; | |
1199 | ||
1200 | if (drv->pm) { | |
25e18499 RW |
1201 | if (drv->pm->resume) |
1202 | ret = drv->pm->resume(dev); | |
1203 | } else { | |
1204 | ret = platform_legacy_resume(dev); | |
1205 | } | |
1206 | ||
1207 | return ret; | |
1208 | } | |
1209 | ||
69c9dd1e | 1210 | #endif /* CONFIG_SUSPEND */ |
25e18499 | 1211 | |
1f112cee | 1212 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
25e18499 | 1213 | |
69c9dd1e | 1214 | int platform_pm_freeze(struct device *dev) |
25e18499 RW |
1215 | { |
1216 | struct device_driver *drv = dev->driver; | |
1217 | int ret = 0; | |
1218 | ||
1219 | if (!drv) | |
1220 | return 0; | |
1221 | ||
1222 | if (drv->pm) { | |
1223 | if (drv->pm->freeze) | |
1224 | ret = drv->pm->freeze(dev); | |
1225 | } else { | |
1226 | ret = platform_legacy_suspend(dev, PMSG_FREEZE); | |
1227 | } | |
1228 | ||
1229 | return ret; | |
1230 | } | |
1231 | ||
69c9dd1e | 1232 | int platform_pm_thaw(struct device *dev) |
25e18499 RW |
1233 | { |
1234 | struct device_driver *drv = dev->driver; | |
1235 | int ret = 0; | |
1236 | ||
adf09493 RW |
1237 | if (!drv) |
1238 | return 0; | |
1239 | ||
1240 | if (drv->pm) { | |
25e18499 RW |
1241 | if (drv->pm->thaw) |
1242 | ret = drv->pm->thaw(dev); | |
1243 | } else { | |
1244 | ret = platform_legacy_resume(dev); | |
1245 | } | |
1246 | ||
1247 | return ret; | |
1248 | } | |
1249 | ||
69c9dd1e | 1250 | int platform_pm_poweroff(struct device *dev) |
25e18499 RW |
1251 | { |
1252 | struct device_driver *drv = dev->driver; | |
1253 | int ret = 0; | |
1254 | ||
adf09493 RW |
1255 | if (!drv) |
1256 | return 0; | |
1257 | ||
1258 | if (drv->pm) { | |
25e18499 RW |
1259 | if (drv->pm->poweroff) |
1260 | ret = drv->pm->poweroff(dev); | |
1261 | } else { | |
1262 | ret = platform_legacy_suspend(dev, PMSG_HIBERNATE); | |
1263 | } | |
1264 | ||
1265 | return ret; | |
1266 | } | |
1267 | ||
69c9dd1e | 1268 | int platform_pm_restore(struct device *dev) |
25e18499 RW |
1269 | { |
1270 | struct device_driver *drv = dev->driver; | |
1271 | int ret = 0; | |
1272 | ||
adf09493 RW |
1273 | if (!drv) |
1274 | return 0; | |
1275 | ||
1276 | if (drv->pm) { | |
25e18499 RW |
1277 | if (drv->pm->restore) |
1278 | ret = drv->pm->restore(dev); | |
1279 | } else { | |
1280 | ret = platform_legacy_resume(dev); | |
1281 | } | |
1282 | ||
1283 | return ret; | |
1284 | } | |
1285 | ||
69c9dd1e | 1286 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
25e18499 | 1287 | |
07397df2 NG |
1288 | int platform_dma_configure(struct device *dev) |
1289 | { | |
1290 | enum dev_dma_attr attr; | |
1291 | int ret = 0; | |
1292 | ||
1293 | if (dev->of_node) { | |
3d6ce86e | 1294 | ret = of_dma_configure(dev, dev->of_node, true); |
07397df2 NG |
1295 | } else if (has_acpi_companion(dev)) { |
1296 | attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode)); | |
e5361ca2 | 1297 | ret = acpi_dma_configure(dev, attr); |
07397df2 NG |
1298 | } |
1299 | ||
1300 | return ret; | |
1301 | } | |
1302 | ||
d9ab7716 | 1303 | static const struct dev_pm_ops platform_dev_pm_ops = { |
8b313a38 RW |
1304 | .runtime_suspend = pm_generic_runtime_suspend, |
1305 | .runtime_resume = pm_generic_runtime_resume, | |
69c9dd1e | 1306 | USE_PLATFORM_PM_SLEEP_OPS |
25e18499 RW |
1307 | }; |
1308 | ||
1da177e4 LT |
1309 | struct bus_type platform_bus_type = { |
1310 | .name = "platform", | |
d06262e5 | 1311 | .dev_groups = platform_dev_groups, |
1da177e4 | 1312 | .match = platform_match, |
a0245f7a | 1313 | .uevent = platform_uevent, |
07397df2 | 1314 | .dma_configure = platform_dma_configure, |
9d730229 | 1315 | .pm = &platform_dev_pm_ops, |
1da177e4 | 1316 | }; |
a96b2042 | 1317 | EXPORT_SYMBOL_GPL(platform_bus_type); |
1da177e4 | 1318 | |
492c8872 ST |
1319 | static inline int __platform_match(struct device *dev, const void *drv) |
1320 | { | |
1321 | return platform_match(dev, (struct device_driver *)drv); | |
1322 | } | |
1323 | ||
36f3313d SP |
1324 | /** |
1325 | * platform_find_device_by_driver - Find a platform device with a given | |
1326 | * driver. | |
1327 | * @start: The device to start the search from. | |
1328 | * @drv: The device driver to look for. | |
1329 | */ | |
1330 | struct device *platform_find_device_by_driver(struct device *start, | |
1331 | const struct device_driver *drv) | |
1332 | { | |
1333 | return bus_find_device(&platform_bus_type, start, drv, | |
492c8872 | 1334 | __platform_match); |
36f3313d SP |
1335 | } |
1336 | EXPORT_SYMBOL_GPL(platform_find_device_by_driver); | |
1337 | ||
eecd37e1 GR |
1338 | void __weak __init early_platform_cleanup(void) { } |
1339 | ||
1da177e4 LT |
1340 | int __init platform_bus_init(void) |
1341 | { | |
fbfb1445 CH |
1342 | int error; |
1343 | ||
eecd37e1 GR |
1344 | early_platform_cleanup(); |
1345 | ||
fbfb1445 | 1346 | error = device_register(&platform_bus); |
c8ae1674 AY |
1347 | if (error) { |
1348 | put_device(&platform_bus); | |
fbfb1445 | 1349 | return error; |
c8ae1674 | 1350 | } |
fbfb1445 CH |
1351 | error = bus_register(&platform_bus_type); |
1352 | if (error) | |
1353 | device_unregister(&platform_bus); | |
801d728c | 1354 | of_platform_register_reconfig_notifier(); |
fbfb1445 | 1355 | return error; |
1da177e4 | 1356 | } |