1 // SPDX-License-Identifier: GPL-2.0
3 * platform.c - platform 'pseudo' bus for legacy devices
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
8 * Please see Documentation/driver-api/driver-model/platform.rst for more
12 #include <linux/string.h>
13 #include <linux/platform_device.h>
14 #include <linux/of_device.h>
15 #include <linux/of_irq.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/memblock.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/pm_domain.h>
24 #include <linux/idr.h>
25 #include <linux/acpi.h>
26 #include <linux/clk/clk-conf.h>
27 #include <linux/limits.h>
28 #include <linux/property.h>
29 #include <linux/kmemleak.h>
32 #include "power/power.h"
34 /* For automatically allocated device IDs */
35 static DEFINE_IDA(platform_devid_ida
);
37 struct device platform_bus
= {
38 .init_name
= "platform",
40 EXPORT_SYMBOL_GPL(platform_bus
);
43 * platform_get_resource - get a resource for a device
44 * @dev: platform device
45 * @type: resource type
46 * @num: resource index
48 struct resource
*platform_get_resource(struct platform_device
*dev
,
49 unsigned int type
, unsigned int num
)
53 for (i
= 0; i
< dev
->num_resources
; i
++) {
54 struct resource
*r
= &dev
->resource
[i
];
56 if (type
== resource_type(r
) && num
-- == 0)
61 EXPORT_SYMBOL_GPL(platform_get_resource
);
63 #ifdef CONFIG_HAS_IOMEM
65 * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
68 * @pdev: platform device to use both for memory resource lookup as well as
70 * @index: resource index
72 void __iomem
*devm_platform_ioremap_resource(struct platform_device
*pdev
,
77 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, index
);
78 return devm_ioremap_resource(&pdev
->dev
, res
);
80 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource
);
83 * devm_platform_ioremap_resource_wc - write-combined variant of
84 * devm_platform_ioremap_resource()
86 * @pdev: platform device to use both for memory resource lookup as well as
88 * @index: resource index
90 void __iomem
*devm_platform_ioremap_resource_wc(struct platform_device
*pdev
,
95 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, index
);
96 return devm_ioremap_resource_wc(&pdev
->dev
, res
);
100 * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for
101 * a platform device, retrieve the
104 * @pdev: platform device to use both for memory resource lookup as well as
105 * resource management
106 * @name: name of the resource
109 devm_platform_ioremap_resource_byname(struct platform_device
*pdev
,
112 struct resource
*res
;
114 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, name
);
115 return devm_ioremap_resource(&pdev
->dev
, res
);
117 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname
);
118 #endif /* CONFIG_HAS_IOMEM */
121 * platform_get_irq_optional - get an optional IRQ for a device
122 * @dev: platform device
123 * @num: IRQ number index
125 * Gets an IRQ for a platform device. Device drivers should check the return
126 * value for errors so as to not pass a negative integer value to the
127 * request_irq() APIs. This is the same as platform_get_irq(), except that it
128 * does not print an error message if an IRQ can not be obtained.
131 * int irq = platform_get_irq_optional(pdev, 0);
135 * Return: IRQ number on success, negative error number on failure.
137 int platform_get_irq_optional(struct platform_device
*dev
, unsigned int num
)
140 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
141 if (!dev
|| num
>= dev
->archdata
.num_irqs
)
143 return dev
->archdata
.irqs
[num
];
148 if (IS_ENABLED(CONFIG_OF_IRQ
) && dev
->dev
.of_node
) {
149 ret
= of_irq_get(dev
->dev
.of_node
, num
);
150 if (ret
> 0 || ret
== -EPROBE_DEFER
)
154 r
= platform_get_resource(dev
, IORESOURCE_IRQ
, num
);
155 if (has_acpi_companion(&dev
->dev
)) {
156 if (r
&& r
->flags
& IORESOURCE_DISABLED
) {
157 ret
= acpi_irq_get(ACPI_HANDLE(&dev
->dev
), num
, r
);
164 * The resources may pass trigger flags to the irqs that need
165 * to be set up. It so happens that the trigger flags for
166 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
169 if (r
&& r
->flags
& IORESOURCE_BITS
) {
170 struct irq_data
*irqd
;
172 irqd
= irq_get_irq_data(r
->start
);
175 irqd_set_trigger_type(irqd
, r
->flags
& IORESOURCE_BITS
);
182 * For the index 0 interrupt, allow falling back to GpioInt
183 * resources. While a device could have both Interrupt and GpioInt
184 * resources, making this fallback ambiguous, in many common cases
185 * the device will only expose one IRQ, and this fallback
186 * allows a common code path across either kind of resource.
188 if (num
== 0 && has_acpi_companion(&dev
->dev
)) {
189 ret
= acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev
->dev
), num
);
190 /* Our callers expect -ENXIO for missing IRQs. */
191 if (ret
>= 0 || ret
== -EPROBE_DEFER
)
198 EXPORT_SYMBOL_GPL(platform_get_irq_optional
);
201 * platform_get_irq - get an IRQ for a device
202 * @dev: platform device
203 * @num: IRQ number index
205 * Gets an IRQ for a platform device and prints an error message if finding the
206 * IRQ fails. Device drivers should check the return value for errors so as to
207 * not pass a negative integer value to the request_irq() APIs.
210 * int irq = platform_get_irq(pdev, 0);
214 * Return: IRQ number on success, negative error number on failure.
216 int platform_get_irq(struct platform_device
*dev
, unsigned int num
)
220 ret
= platform_get_irq_optional(dev
, num
);
221 if (ret
< 0 && ret
!= -EPROBE_DEFER
)
222 dev_err(&dev
->dev
, "IRQ index %u not found\n", num
);
226 EXPORT_SYMBOL_GPL(platform_get_irq
);
229 * platform_irq_count - Count the number of IRQs a platform device uses
230 * @dev: platform device
232 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
234 int platform_irq_count(struct platform_device
*dev
)
238 while ((ret
= platform_get_irq_optional(dev
, nr
)) >= 0)
241 if (ret
== -EPROBE_DEFER
)
246 EXPORT_SYMBOL_GPL(platform_irq_count
);
249 * platform_get_resource_byname - get a resource for a device by name
250 * @dev: platform device
251 * @type: resource type
252 * @name: resource name
254 struct resource
*platform_get_resource_byname(struct platform_device
*dev
,
260 for (i
= 0; i
< dev
->num_resources
; i
++) {
261 struct resource
*r
= &dev
->resource
[i
];
263 if (unlikely(!r
->name
))
266 if (type
== resource_type(r
) && !strcmp(r
->name
, name
))
271 EXPORT_SYMBOL_GPL(platform_get_resource_byname
);
273 static int __platform_get_irq_byname(struct platform_device
*dev
,
279 if (IS_ENABLED(CONFIG_OF_IRQ
) && dev
->dev
.of_node
) {
280 ret
= of_irq_get_byname(dev
->dev
.of_node
, name
);
281 if (ret
> 0 || ret
== -EPROBE_DEFER
)
285 r
= platform_get_resource_byname(dev
, IORESOURCE_IRQ
, name
);
293 * platform_get_irq_byname - get an IRQ for a device by name
294 * @dev: platform device
297 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
299 * Return: IRQ number on success, negative error number on failure.
301 int platform_get_irq_byname(struct platform_device
*dev
, const char *name
)
305 ret
= __platform_get_irq_byname(dev
, name
);
306 if (ret
< 0 && ret
!= -EPROBE_DEFER
)
307 dev_err(&dev
->dev
, "IRQ %s not found\n", name
);
311 EXPORT_SYMBOL_GPL(platform_get_irq_byname
);
314 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
315 * @dev: platform device
318 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
319 * does not print an error message if an IRQ can not be obtained.
321 * Return: IRQ number on success, negative error number on failure.
323 int platform_get_irq_byname_optional(struct platform_device
*dev
,
326 return __platform_get_irq_byname(dev
, name
);
328 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional
);
331 * platform_add_devices - add a numbers of platform devices
332 * @devs: array of platform devices to add
333 * @num: number of platform devices in array
335 int platform_add_devices(struct platform_device
**devs
, int num
)
339 for (i
= 0; i
< num
; i
++) {
340 ret
= platform_device_register(devs
[i
]);
343 platform_device_unregister(devs
[i
]);
350 EXPORT_SYMBOL_GPL(platform_add_devices
);
352 struct platform_object
{
353 struct platform_device pdev
;
358 * Set up default DMA mask for platform devices if the they weren't
359 * previously set by the architecture / DT.
361 static void setup_pdev_dma_masks(struct platform_device
*pdev
)
363 if (!pdev
->dev
.coherent_dma_mask
)
364 pdev
->dev
.coherent_dma_mask
= DMA_BIT_MASK(32);
366 pdev
->dma_mask
= DMA_BIT_MASK(32);
367 if (!pdev
->dev
.dma_mask
)
368 pdev
->dev
.dma_mask
= &pdev
->dma_mask
;
372 * platform_device_put - destroy a platform device
373 * @pdev: platform device to free
375 * Free all memory associated with a platform device. This function must
376 * _only_ be externally called in error cases. All other usage is a bug.
378 void platform_device_put(struct platform_device
*pdev
)
380 if (!IS_ERR_OR_NULL(pdev
))
381 put_device(&pdev
->dev
);
383 EXPORT_SYMBOL_GPL(platform_device_put
);
385 static void platform_device_release(struct device
*dev
)
387 struct platform_object
*pa
= container_of(dev
, struct platform_object
,
390 of_device_node_put(&pa
->pdev
.dev
);
391 kfree(pa
->pdev
.dev
.platform_data
);
392 kfree(pa
->pdev
.mfd_cell
);
393 kfree(pa
->pdev
.resource
);
394 kfree(pa
->pdev
.driver_override
);
399 * platform_device_alloc - create a platform device
400 * @name: base name of the device we're adding
403 * Create a platform device object which can have other objects attached
404 * to it, and which will have attached objects freed when it is released.
406 struct platform_device
*platform_device_alloc(const char *name
, int id
)
408 struct platform_object
*pa
;
410 pa
= kzalloc(sizeof(*pa
) + strlen(name
) + 1, GFP_KERNEL
);
412 strcpy(pa
->name
, name
);
413 pa
->pdev
.name
= pa
->name
;
415 device_initialize(&pa
->pdev
.dev
);
416 pa
->pdev
.dev
.release
= platform_device_release
;
417 setup_pdev_dma_masks(&pa
->pdev
);
420 return pa
? &pa
->pdev
: NULL
;
422 EXPORT_SYMBOL_GPL(platform_device_alloc
);
425 * platform_device_add_resources - add resources to a platform device
426 * @pdev: platform device allocated by platform_device_alloc to add resources to
427 * @res: set of resources that needs to be allocated for the device
428 * @num: number of resources
430 * Add a copy of the resources to the platform device. The memory
431 * associated with the resources will be freed when the platform device is
434 int platform_device_add_resources(struct platform_device
*pdev
,
435 const struct resource
*res
, unsigned int num
)
437 struct resource
*r
= NULL
;
440 r
= kmemdup(res
, sizeof(struct resource
) * num
, GFP_KERNEL
);
445 kfree(pdev
->resource
);
447 pdev
->num_resources
= num
;
450 EXPORT_SYMBOL_GPL(platform_device_add_resources
);
453 * platform_device_add_data - add platform-specific data to a platform device
454 * @pdev: platform device allocated by platform_device_alloc to add resources to
455 * @data: platform specific data for this platform device
456 * @size: size of platform specific data
458 * Add a copy of platform specific data to the platform device's
459 * platform_data pointer. The memory associated with the platform data
460 * will be freed when the platform device is released.
462 int platform_device_add_data(struct platform_device
*pdev
, const void *data
,
468 d
= kmemdup(data
, size
, GFP_KERNEL
);
473 kfree(pdev
->dev
.platform_data
);
474 pdev
->dev
.platform_data
= d
;
477 EXPORT_SYMBOL_GPL(platform_device_add_data
);
480 * platform_device_add_properties - add built-in properties to a platform device
481 * @pdev: platform device to add properties to
482 * @properties: null terminated array of properties to add
484 * The function will take deep copy of @properties and attach the copy to the
485 * platform device. The memory associated with properties will be freed when the
486 * platform device is released.
488 int platform_device_add_properties(struct platform_device
*pdev
,
489 const struct property_entry
*properties
)
491 return device_add_properties(&pdev
->dev
, properties
);
493 EXPORT_SYMBOL_GPL(platform_device_add_properties
);
496 * platform_device_add - add a platform device to device hierarchy
497 * @pdev: platform device we're adding
499 * This is part 2 of platform_device_register(), though may be called
500 * separately _iff_ pdev was allocated by platform_device_alloc().
502 int platform_device_add(struct platform_device
*pdev
)
509 if (!pdev
->dev
.parent
)
510 pdev
->dev
.parent
= &platform_bus
;
512 pdev
->dev
.bus
= &platform_bus_type
;
516 dev_set_name(&pdev
->dev
, "%s.%d", pdev
->name
, pdev
->id
);
518 case PLATFORM_DEVID_NONE
:
519 dev_set_name(&pdev
->dev
, "%s", pdev
->name
);
521 case PLATFORM_DEVID_AUTO
:
523 * Automatically allocated device ID. We mark it as such so
524 * that we remember it must be freed, and we append a suffix
525 * to avoid namespace collision with explicit IDs.
527 ret
= ida_simple_get(&platform_devid_ida
, 0, 0, GFP_KERNEL
);
531 pdev
->id_auto
= true;
532 dev_set_name(&pdev
->dev
, "%s.%d.auto", pdev
->name
, pdev
->id
);
536 for (i
= 0; i
< pdev
->num_resources
; i
++) {
537 struct resource
*p
, *r
= &pdev
->resource
[i
];
540 r
->name
= dev_name(&pdev
->dev
);
544 if (resource_type(r
) == IORESOURCE_MEM
)
546 else if (resource_type(r
) == IORESOURCE_IO
)
547 p
= &ioport_resource
;
551 ret
= insert_resource(p
, r
);
553 dev_err(&pdev
->dev
, "failed to claim resource %d: %pR\n", i
, r
);
559 pr_debug("Registering platform device '%s'. Parent at %s\n",
560 dev_name(&pdev
->dev
), dev_name(pdev
->dev
.parent
));
562 ret
= device_add(&pdev
->dev
);
568 ida_simple_remove(&platform_devid_ida
, pdev
->id
);
569 pdev
->id
= PLATFORM_DEVID_AUTO
;
573 struct resource
*r
= &pdev
->resource
[i
];
581 EXPORT_SYMBOL_GPL(platform_device_add
);
584 * platform_device_del - remove a platform-level device
585 * @pdev: platform device we're removing
587 * Note that this function will also release all memory- and port-based
588 * resources owned by the device (@dev->resource). This function must
589 * _only_ be externally called in error cases. All other usage is a bug.
591 void platform_device_del(struct platform_device
*pdev
)
595 if (!IS_ERR_OR_NULL(pdev
)) {
596 device_del(&pdev
->dev
);
599 ida_simple_remove(&platform_devid_ida
, pdev
->id
);
600 pdev
->id
= PLATFORM_DEVID_AUTO
;
603 for (i
= 0; i
< pdev
->num_resources
; i
++) {
604 struct resource
*r
= &pdev
->resource
[i
];
610 EXPORT_SYMBOL_GPL(platform_device_del
);
613 * platform_device_register - add a platform-level device
614 * @pdev: platform device we're adding
616 int platform_device_register(struct platform_device
*pdev
)
618 device_initialize(&pdev
->dev
);
619 setup_pdev_dma_masks(pdev
);
620 return platform_device_add(pdev
);
622 EXPORT_SYMBOL_GPL(platform_device_register
);
625 * platform_device_unregister - unregister a platform-level device
626 * @pdev: platform device we're unregistering
628 * Unregistration is done in 2 steps. First we release all resources
629 * and remove it from the subsystem, then we drop reference count by
630 * calling platform_device_put().
632 void platform_device_unregister(struct platform_device
*pdev
)
634 platform_device_del(pdev
);
635 platform_device_put(pdev
);
637 EXPORT_SYMBOL_GPL(platform_device_unregister
);
640 * platform_device_register_full - add a platform-level device with
641 * resources and platform-specific data
643 * @pdevinfo: data used to create device
645 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
647 struct platform_device
*platform_device_register_full(
648 const struct platform_device_info
*pdevinfo
)
651 struct platform_device
*pdev
;
653 pdev
= platform_device_alloc(pdevinfo
->name
, pdevinfo
->id
);
655 return ERR_PTR(-ENOMEM
);
657 pdev
->dev
.parent
= pdevinfo
->parent
;
658 pdev
->dev
.fwnode
= pdevinfo
->fwnode
;
659 pdev
->dev
.of_node
= of_node_get(to_of_node(pdev
->dev
.fwnode
));
660 pdev
->dev
.of_node_reused
= pdevinfo
->of_node_reused
;
662 if (pdevinfo
->dma_mask
) {
664 * This memory isn't freed when the device is put,
665 * I don't have a nice idea for that though. Conceptually
666 * dma_mask in struct device should not be a pointer.
667 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
670 kmalloc(sizeof(*pdev
->dev
.dma_mask
), GFP_KERNEL
);
671 if (!pdev
->dev
.dma_mask
)
674 kmemleak_ignore(pdev
->dev
.dma_mask
);
676 *pdev
->dev
.dma_mask
= pdevinfo
->dma_mask
;
677 pdev
->dev
.coherent_dma_mask
= pdevinfo
->dma_mask
;
680 ret
= platform_device_add_resources(pdev
,
681 pdevinfo
->res
, pdevinfo
->num_res
);
685 ret
= platform_device_add_data(pdev
,
686 pdevinfo
->data
, pdevinfo
->size_data
);
690 if (pdevinfo
->properties
) {
691 ret
= platform_device_add_properties(pdev
,
692 pdevinfo
->properties
);
697 ret
= platform_device_add(pdev
);
700 ACPI_COMPANION_SET(&pdev
->dev
, NULL
);
701 kfree(pdev
->dev
.dma_mask
);
702 platform_device_put(pdev
);
708 EXPORT_SYMBOL_GPL(platform_device_register_full
);
710 static int platform_drv_probe(struct device
*_dev
)
712 struct platform_driver
*drv
= to_platform_driver(_dev
->driver
);
713 struct platform_device
*dev
= to_platform_device(_dev
);
716 ret
= of_clk_set_defaults(_dev
->of_node
, false);
720 ret
= dev_pm_domain_attach(_dev
, true);
725 ret
= drv
->probe(dev
);
727 dev_pm_domain_detach(_dev
, true);
731 if (drv
->prevent_deferred_probe
&& ret
== -EPROBE_DEFER
) {
732 dev_warn(_dev
, "probe deferral not supported\n");
739 static int platform_drv_probe_fail(struct device
*_dev
)
744 static int platform_drv_remove(struct device
*_dev
)
746 struct platform_driver
*drv
= to_platform_driver(_dev
->driver
);
747 struct platform_device
*dev
= to_platform_device(_dev
);
751 ret
= drv
->remove(dev
);
752 dev_pm_domain_detach(_dev
, true);
757 static void platform_drv_shutdown(struct device
*_dev
)
759 struct platform_driver
*drv
= to_platform_driver(_dev
->driver
);
760 struct platform_device
*dev
= to_platform_device(_dev
);
767 * __platform_driver_register - register a driver for platform-level devices
768 * @drv: platform driver structure
769 * @owner: owning module/driver
771 int __platform_driver_register(struct platform_driver
*drv
,
772 struct module
*owner
)
774 drv
->driver
.owner
= owner
;
775 drv
->driver
.bus
= &platform_bus_type
;
776 drv
->driver
.probe
= platform_drv_probe
;
777 drv
->driver
.remove
= platform_drv_remove
;
778 drv
->driver
.shutdown
= platform_drv_shutdown
;
780 return driver_register(&drv
->driver
);
782 EXPORT_SYMBOL_GPL(__platform_driver_register
);
785 * platform_driver_unregister - unregister a driver for platform-level devices
786 * @drv: platform driver structure
788 void platform_driver_unregister(struct platform_driver
*drv
)
790 driver_unregister(&drv
->driver
);
792 EXPORT_SYMBOL_GPL(platform_driver_unregister
);
795 * __platform_driver_probe - register driver for non-hotpluggable device
796 * @drv: platform driver structure
797 * @probe: the driver probe routine, probably from an __init section
798 * @module: module which will be the owner of the driver
800 * Use this instead of platform_driver_register() when you know the device
801 * is not hotpluggable and has already been registered, and you want to
802 * remove its run-once probe() infrastructure from memory after the driver
803 * has bound to the device.
805 * One typical use for this would be with drivers for controllers integrated
806 * into system-on-chip processors, where the controller devices have been
807 * configured as part of board setup.
809 * Note that this is incompatible with deferred probing.
811 * Returns zero if the driver registered and bound to a device, else returns
812 * a negative error code and with the driver not registered.
814 int __init_or_module
__platform_driver_probe(struct platform_driver
*drv
,
815 int (*probe
)(struct platform_device
*), struct module
*module
)
819 if (drv
->driver
.probe_type
== PROBE_PREFER_ASYNCHRONOUS
) {
820 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
821 drv
->driver
.name
, __func__
);
826 * We have to run our probes synchronously because we check if
827 * we find any devices to bind to and exit with error if there
830 drv
->driver
.probe_type
= PROBE_FORCE_SYNCHRONOUS
;
833 * Prevent driver from requesting probe deferral to avoid further
834 * futile probe attempts.
836 drv
->prevent_deferred_probe
= true;
838 /* make sure driver won't have bind/unbind attributes */
839 drv
->driver
.suppress_bind_attrs
= true;
841 /* temporary section violation during probe() */
843 retval
= code
= __platform_driver_register(drv
, module
);
846 * Fixup that section violation, being paranoid about code scanning
847 * the list of drivers in order to probe new devices. Check to see
848 * if the probe was successful, and make sure any forced probes of
851 spin_lock(&drv
->driver
.bus
->p
->klist_drivers
.k_lock
);
853 if (code
== 0 && list_empty(&drv
->driver
.p
->klist_devices
.k_list
))
855 drv
->driver
.probe
= platform_drv_probe_fail
;
856 spin_unlock(&drv
->driver
.bus
->p
->klist_drivers
.k_lock
);
859 platform_driver_unregister(drv
);
862 EXPORT_SYMBOL_GPL(__platform_driver_probe
);
865 * __platform_create_bundle - register driver and create corresponding device
866 * @driver: platform driver structure
867 * @probe: the driver probe routine, probably from an __init section
868 * @res: set of resources that needs to be allocated for the device
869 * @n_res: number of resources
870 * @data: platform specific data for this platform device
871 * @size: size of platform specific data
872 * @module: module which will be the owner of the driver
874 * Use this in legacy-style modules that probe hardware directly and
875 * register a single platform device and corresponding platform driver.
877 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
879 struct platform_device
* __init_or_module
__platform_create_bundle(
880 struct platform_driver
*driver
,
881 int (*probe
)(struct platform_device
*),
882 struct resource
*res
, unsigned int n_res
,
883 const void *data
, size_t size
, struct module
*module
)
885 struct platform_device
*pdev
;
888 pdev
= platform_device_alloc(driver
->driver
.name
, -1);
894 error
= platform_device_add_resources(pdev
, res
, n_res
);
898 error
= platform_device_add_data(pdev
, data
, size
);
902 error
= platform_device_add(pdev
);
906 error
= __platform_driver_probe(driver
, probe
, module
);
913 platform_device_del(pdev
);
915 platform_device_put(pdev
);
917 return ERR_PTR(error
);
919 EXPORT_SYMBOL_GPL(__platform_create_bundle
);
922 * __platform_register_drivers - register an array of platform drivers
923 * @drivers: an array of drivers to register
924 * @count: the number of drivers to register
925 * @owner: module owning the drivers
927 * Registers platform drivers specified by an array. On failure to register a
928 * driver, all previously registered drivers will be unregistered. Callers of
929 * this API should use platform_unregister_drivers() to unregister drivers in
932 * Returns: 0 on success or a negative error code on failure.
934 int __platform_register_drivers(struct platform_driver
* const *drivers
,
935 unsigned int count
, struct module
*owner
)
940 for (i
= 0; i
< count
; i
++) {
941 pr_debug("registering platform driver %ps\n", drivers
[i
]);
943 err
= __platform_driver_register(drivers
[i
], owner
);
945 pr_err("failed to register platform driver %ps: %d\n",
955 pr_debug("unregistering platform driver %ps\n", drivers
[i
]);
956 platform_driver_unregister(drivers
[i
]);
961 EXPORT_SYMBOL_GPL(__platform_register_drivers
);
964 * platform_unregister_drivers - unregister an array of platform drivers
965 * @drivers: an array of drivers to unregister
966 * @count: the number of drivers to unregister
968 * Unegisters platform drivers specified by an array. This is typically used
969 * to complement an earlier call to platform_register_drivers(). Drivers are
970 * unregistered in the reverse order in which they were registered.
972 void platform_unregister_drivers(struct platform_driver
* const *drivers
,
976 pr_debug("unregistering platform driver %ps\n", drivers
[count
]);
977 platform_driver_unregister(drivers
[count
]);
980 EXPORT_SYMBOL_GPL(platform_unregister_drivers
);
982 /* modalias support enables more hands-off userspace setup:
983 * (a) environment variable lets new-style hotplug events work once system is
984 * fully running: "modprobe $MODALIAS"
985 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
986 * mishandled before system is fully running: "modprobe $(cat modalias)"
988 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*a
,
991 struct platform_device
*pdev
= to_platform_device(dev
);
994 len
= of_device_modalias(dev
, buf
, PAGE_SIZE
);
998 len
= acpi_device_modalias(dev
, buf
, PAGE_SIZE
-1);
1002 len
= snprintf(buf
, PAGE_SIZE
, "platform:%s\n", pdev
->name
);
1004 return (len
>= PAGE_SIZE
) ? (PAGE_SIZE
- 1) : len
;
1006 static DEVICE_ATTR_RO(modalias
);
1008 static ssize_t
driver_override_store(struct device
*dev
,
1009 struct device_attribute
*attr
,
1010 const char *buf
, size_t count
)
1012 struct platform_device
*pdev
= to_platform_device(dev
);
1013 char *driver_override
, *old
, *cp
;
1015 /* We need to keep extra room for a newline */
1016 if (count
>= (PAGE_SIZE
- 1))
1019 driver_override
= kstrndup(buf
, count
, GFP_KERNEL
);
1020 if (!driver_override
)
1023 cp
= strchr(driver_override
, '\n');
1028 old
= pdev
->driver_override
;
1029 if (strlen(driver_override
)) {
1030 pdev
->driver_override
= driver_override
;
1032 kfree(driver_override
);
1033 pdev
->driver_override
= NULL
;
1042 static ssize_t
driver_override_show(struct device
*dev
,
1043 struct device_attribute
*attr
, char *buf
)
1045 struct platform_device
*pdev
= to_platform_device(dev
);
1049 len
= sprintf(buf
, "%s\n", pdev
->driver_override
);
1053 static DEVICE_ATTR_RW(driver_override
);
1056 static struct attribute
*platform_dev_attrs
[] = {
1057 &dev_attr_modalias
.attr
,
1058 &dev_attr_driver_override
.attr
,
1061 ATTRIBUTE_GROUPS(platform_dev
);
1063 static int platform_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1065 struct platform_device
*pdev
= to_platform_device(dev
);
1068 /* Some devices have extra OF data and an OF-style MODALIAS */
1069 rc
= of_device_uevent_modalias(dev
, env
);
1073 rc
= acpi_device_uevent_modalias(dev
, env
);
1077 add_uevent_var(env
, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX
,
1082 static const struct platform_device_id
*platform_match_id(
1083 const struct platform_device_id
*id
,
1084 struct platform_device
*pdev
)
1086 while (id
->name
[0]) {
1087 if (strcmp(pdev
->name
, id
->name
) == 0) {
1088 pdev
->id_entry
= id
;
1097 * platform_match - bind platform device to platform driver.
1101 * Platform device IDs are assumed to be encoded like this:
1102 * "<name><instance>", where <name> is a short description of the type of
1103 * device, like "pci" or "floppy", and <instance> is the enumerated
1104 * instance of the device, like '0' or '42'. Driver IDs are simply
1105 * "<name>". So, extract the <name> from the platform_device structure,
1106 * and compare it against the name of the driver. Return whether they match
1109 static int platform_match(struct device
*dev
, struct device_driver
*drv
)
1111 struct platform_device
*pdev
= to_platform_device(dev
);
1112 struct platform_driver
*pdrv
= to_platform_driver(drv
);
1114 /* When driver_override is set, only bind to the matching driver */
1115 if (pdev
->driver_override
)
1116 return !strcmp(pdev
->driver_override
, drv
->name
);
1118 /* Attempt an OF style match first */
1119 if (of_driver_match_device(dev
, drv
))
1122 /* Then try ACPI style match */
1123 if (acpi_driver_match_device(dev
, drv
))
1126 /* Then try to match against the id table */
1128 return platform_match_id(pdrv
->id_table
, pdev
) != NULL
;
1130 /* fall-back to driver name match */
1131 return (strcmp(pdev
->name
, drv
->name
) == 0);
1134 #ifdef CONFIG_PM_SLEEP
1136 static int platform_legacy_suspend(struct device
*dev
, pm_message_t mesg
)
1138 struct platform_driver
*pdrv
= to_platform_driver(dev
->driver
);
1139 struct platform_device
*pdev
= to_platform_device(dev
);
1142 if (dev
->driver
&& pdrv
->suspend
)
1143 ret
= pdrv
->suspend(pdev
, mesg
);
1148 static int platform_legacy_resume(struct device
*dev
)
1150 struct platform_driver
*pdrv
= to_platform_driver(dev
->driver
);
1151 struct platform_device
*pdev
= to_platform_device(dev
);
1154 if (dev
->driver
&& pdrv
->resume
)
1155 ret
= pdrv
->resume(pdev
);
1160 #endif /* CONFIG_PM_SLEEP */
1162 #ifdef CONFIG_SUSPEND
1164 int platform_pm_suspend(struct device
*dev
)
1166 struct device_driver
*drv
= dev
->driver
;
1173 if (drv
->pm
->suspend
)
1174 ret
= drv
->pm
->suspend(dev
);
1176 ret
= platform_legacy_suspend(dev
, PMSG_SUSPEND
);
1182 int platform_pm_resume(struct device
*dev
)
1184 struct device_driver
*drv
= dev
->driver
;
1191 if (drv
->pm
->resume
)
1192 ret
= drv
->pm
->resume(dev
);
1194 ret
= platform_legacy_resume(dev
);
1200 #endif /* CONFIG_SUSPEND */
1202 #ifdef CONFIG_HIBERNATE_CALLBACKS
1204 int platform_pm_freeze(struct device
*dev
)
1206 struct device_driver
*drv
= dev
->driver
;
1213 if (drv
->pm
->freeze
)
1214 ret
= drv
->pm
->freeze(dev
);
1216 ret
= platform_legacy_suspend(dev
, PMSG_FREEZE
);
1222 int platform_pm_thaw(struct device
*dev
)
1224 struct device_driver
*drv
= dev
->driver
;
1232 ret
= drv
->pm
->thaw(dev
);
1234 ret
= platform_legacy_resume(dev
);
1240 int platform_pm_poweroff(struct device
*dev
)
1242 struct device_driver
*drv
= dev
->driver
;
1249 if (drv
->pm
->poweroff
)
1250 ret
= drv
->pm
->poweroff(dev
);
1252 ret
= platform_legacy_suspend(dev
, PMSG_HIBERNATE
);
1258 int platform_pm_restore(struct device
*dev
)
1260 struct device_driver
*drv
= dev
->driver
;
1267 if (drv
->pm
->restore
)
1268 ret
= drv
->pm
->restore(dev
);
1270 ret
= platform_legacy_resume(dev
);
1276 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1278 int platform_dma_configure(struct device
*dev
)
1280 enum dev_dma_attr attr
;
1284 ret
= of_dma_configure(dev
, dev
->of_node
, true);
1285 } else if (has_acpi_companion(dev
)) {
1286 attr
= acpi_get_dma_attr(to_acpi_device_node(dev
->fwnode
));
1287 ret
= acpi_dma_configure(dev
, attr
);
1293 static const struct dev_pm_ops platform_dev_pm_ops
= {
1294 .runtime_suspend
= pm_generic_runtime_suspend
,
1295 .runtime_resume
= pm_generic_runtime_resume
,
1296 USE_PLATFORM_PM_SLEEP_OPS
1299 struct bus_type platform_bus_type
= {
1301 .dev_groups
= platform_dev_groups
,
1302 .match
= platform_match
,
1303 .uevent
= platform_uevent
,
1304 .dma_configure
= platform_dma_configure
,
1305 .pm
= &platform_dev_pm_ops
,
1307 EXPORT_SYMBOL_GPL(platform_bus_type
);
1309 static inline int __platform_match(struct device
*dev
, const void *drv
)
1311 return platform_match(dev
, (struct device_driver
*)drv
);
1315 * platform_find_device_by_driver - Find a platform device with a given
1317 * @start: The device to start the search from.
1318 * @drv: The device driver to look for.
1320 struct device
*platform_find_device_by_driver(struct device
*start
,
1321 const struct device_driver
*drv
)
1323 return bus_find_device(&platform_bus_type
, start
, drv
,
1326 EXPORT_SYMBOL_GPL(platform_find_device_by_driver
);
1328 int __init
platform_bus_init(void)
1332 error
= device_register(&platform_bus
);
1334 put_device(&platform_bus
);
1337 error
= bus_register(&platform_bus_type
);
1339 device_unregister(&platform_bus
);
1340 of_platform_register_reconfig_notifier();