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/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/memblock.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/pm_domain.h>
26 #include <linux/idr.h>
27 #include <linux/acpi.h>
28 #include <linux/clk/clk-conf.h>
29 #include <linux/limits.h>
30 #include <linux/property.h>
31 #include <linux/kmemleak.h>
32 #include <linux/types.h>
33 #include <linux/iommu.h>
34 #include <linux/dma-map-ops.h>
37 #include "power/power.h"
39 /* For automatically allocated device IDs */
40 static DEFINE_IDA(platform_devid_ida);
42 struct device platform_bus = {
43 .init_name = "platform",
45 EXPORT_SYMBOL_GPL(platform_bus);
48 * platform_get_resource - get a resource for a device
49 * @dev: platform device
50 * @type: resource type
51 * @num: resource index
53 * Return: a pointer to the resource or NULL on failure.
55 struct resource *platform_get_resource(struct platform_device *dev,
56 unsigned int type, unsigned int num)
60 for (i = 0; i < dev->num_resources; i++) {
61 struct resource *r = &dev->resource[i];
63 if (type == resource_type(r) && num-- == 0)
68 EXPORT_SYMBOL_GPL(platform_get_resource);
70 struct resource *platform_get_mem_or_io(struct platform_device *dev,
75 for (i = 0; i < dev->num_resources; i++) {
76 struct resource *r = &dev->resource[i];
78 if ((resource_type(r) & (IORESOURCE_MEM|IORESOURCE_IO)) && num-- == 0)
83 EXPORT_SYMBOL_GPL(platform_get_mem_or_io);
85 #ifdef CONFIG_HAS_IOMEM
87 * devm_platform_get_and_ioremap_resource - call devm_ioremap_resource() for a
88 * platform device and get resource
90 * @pdev: platform device to use both for memory resource lookup as well as
92 * @index: resource index
93 * @res: optional output parameter to store a pointer to the obtained resource.
95 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
99 devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
100 unsigned int index, struct resource **res)
104 r = platform_get_resource(pdev, IORESOURCE_MEM, index);
107 return devm_ioremap_resource(&pdev->dev, r);
109 EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource);
112 * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
115 * @pdev: platform device to use both for memory resource lookup as well as
116 * resource management
117 * @index: resource index
119 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
122 void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
125 return devm_platform_get_and_ioremap_resource(pdev, index, NULL);
127 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
130 * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for
131 * a platform device, retrieve the
134 * @pdev: platform device to use both for memory resource lookup as well as
135 * resource management
136 * @name: name of the resource
138 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
142 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
145 struct resource *res;
147 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
148 return devm_ioremap_resource(&pdev->dev, res);
150 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname);
151 #endif /* CONFIG_HAS_IOMEM */
154 * platform_get_irq_optional - get an optional IRQ for a device
155 * @dev: platform device
156 * @num: IRQ number index
158 * Gets an IRQ for a platform device. Device drivers should check the return
159 * value for errors so as to not pass a negative integer value to the
160 * request_irq() APIs. This is the same as platform_get_irq(), except that it
161 * does not print an error message if an IRQ can not be obtained.
165 * int irq = platform_get_irq_optional(pdev, 0);
169 * Return: non-zero IRQ number on success, negative error number on failure.
171 int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
175 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
176 if (!dev || num >= dev->archdata.num_irqs)
178 ret = dev->archdata.irqs[num];
183 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
184 ret = of_irq_get(dev->dev.of_node, num);
185 if (ret > 0 || ret == -EPROBE_DEFER)
189 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
190 if (has_acpi_companion(&dev->dev)) {
191 if (r && r->flags & IORESOURCE_DISABLED) {
192 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
199 * The resources may pass trigger flags to the irqs that need
200 * to be set up. It so happens that the trigger flags for
201 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
204 if (r && r->flags & IORESOURCE_BITS) {
205 struct irq_data *irqd;
207 irqd = irq_get_irq_data(r->start);
210 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
219 * For the index 0 interrupt, allow falling back to GpioInt
220 * resources. While a device could have both Interrupt and GpioInt
221 * resources, making this fallback ambiguous, in many common cases
222 * the device will only expose one IRQ, and this fallback
223 * allows a common code path across either kind of resource.
225 if (num == 0 && has_acpi_companion(&dev->dev)) {
226 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
227 /* Our callers expect -ENXIO for missing IRQs. */
228 if (ret >= 0 || ret == -EPROBE_DEFER)
236 WARN(ret == 0, "0 is an invalid IRQ number\n");
239 EXPORT_SYMBOL_GPL(platform_get_irq_optional);
242 * platform_get_irq - get an IRQ for a device
243 * @dev: platform device
244 * @num: IRQ number index
246 * Gets an IRQ for a platform device and prints an error message if finding the
247 * IRQ fails. Device drivers should check the return value for errors so as to
248 * not pass a negative integer value to the request_irq() APIs.
252 * int irq = platform_get_irq(pdev, 0);
256 * Return: non-zero IRQ number on success, negative error number on failure.
258 int platform_get_irq(struct platform_device *dev, unsigned int num)
262 ret = platform_get_irq_optional(dev, num);
264 return dev_err_probe(&dev->dev, ret,
265 "IRQ index %u not found\n", num);
269 EXPORT_SYMBOL_GPL(platform_get_irq);
272 * platform_irq_count - Count the number of IRQs a platform device uses
273 * @dev: platform device
275 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
277 int platform_irq_count(struct platform_device *dev)
281 while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
284 if (ret == -EPROBE_DEFER)
289 EXPORT_SYMBOL_GPL(platform_irq_count);
291 struct irq_affinity_devres {
296 static void platform_disable_acpi_irq(struct platform_device *pdev, int index)
300 r = platform_get_resource(pdev, IORESOURCE_IRQ, index);
302 irqresource_disabled(r, 0);
305 static void devm_platform_get_irqs_affinity_release(struct device *dev,
308 struct irq_affinity_devres *ptr = res;
311 for (i = 0; i < ptr->count; i++) {
312 irq_dispose_mapping(ptr->irq[i]);
314 if (has_acpi_companion(dev))
315 platform_disable_acpi_irq(to_platform_device(dev), i);
320 * devm_platform_get_irqs_affinity - devm method to get a set of IRQs for a
321 * device using an interrupt affinity descriptor
322 * @dev: platform device pointer
323 * @affd: affinity descriptor
324 * @minvec: minimum count of interrupt vectors
325 * @maxvec: maximum count of interrupt vectors
326 * @irqs: pointer holder for IRQ numbers
328 * Gets a set of IRQs for a platform device, and updates IRQ afffinty according
329 * to the passed affinity descriptor
331 * Return: Number of vectors on success, negative error number on failure.
333 int devm_platform_get_irqs_affinity(struct platform_device *dev,
334 struct irq_affinity *affd,
339 struct irq_affinity_devres *ptr;
340 struct irq_affinity_desc *desc;
350 nvec = platform_irq_count(dev);
357 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
364 size = sizeof(*ptr) + sizeof(unsigned int) * nvec;
365 ptr = devres_alloc(devm_platform_get_irqs_affinity_release, size,
372 for (i = 0; i < nvec; i++) {
373 int irq = platform_get_irq(dev, i);
376 goto err_free_devres;
381 desc = irq_create_affinity_masks(nvec, affd);
384 goto err_free_devres;
387 for (i = 0; i < nvec; i++) {
388 ret = irq_update_affinity_desc(ptr->irq[i], &desc[i]);
390 dev_err(&dev->dev, "failed to update irq%d affinity descriptor (%d)\n",
396 devres_add(&dev->dev, ptr);
410 EXPORT_SYMBOL_GPL(devm_platform_get_irqs_affinity);
413 * platform_get_resource_byname - get a resource for a device by name
414 * @dev: platform device
415 * @type: resource type
416 * @name: resource name
418 struct resource *platform_get_resource_byname(struct platform_device *dev,
424 for (i = 0; i < dev->num_resources; i++) {
425 struct resource *r = &dev->resource[i];
427 if (unlikely(!r->name))
430 if (type == resource_type(r) && !strcmp(r->name, name))
435 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
437 static int __platform_get_irq_byname(struct platform_device *dev,
443 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
444 ret = of_irq_get_byname(dev->dev.of_node, name);
445 if (ret > 0 || ret == -EPROBE_DEFER)
449 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
451 WARN(r->start == 0, "0 is an invalid IRQ number\n");
459 * platform_get_irq_byname - get an IRQ for a device by name
460 * @dev: platform device
463 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
465 * Return: non-zero IRQ number on success, negative error number on failure.
467 int platform_get_irq_byname(struct platform_device *dev, const char *name)
471 ret = __platform_get_irq_byname(dev, name);
473 return dev_err_probe(&dev->dev, ret, "IRQ %s not found\n",
477 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
480 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
481 * @dev: platform device
484 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
485 * does not print an error message if an IRQ can not be obtained.
487 * Return: non-zero IRQ number on success, negative error number on failure.
489 int platform_get_irq_byname_optional(struct platform_device *dev,
492 return __platform_get_irq_byname(dev, name);
494 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
497 * platform_add_devices - add a numbers of platform devices
498 * @devs: array of platform devices to add
499 * @num: number of platform devices in array
501 int platform_add_devices(struct platform_device **devs, int num)
505 for (i = 0; i < num; i++) {
506 ret = platform_device_register(devs[i]);
509 platform_device_unregister(devs[i]);
516 EXPORT_SYMBOL_GPL(platform_add_devices);
518 struct platform_object {
519 struct platform_device pdev;
524 * Set up default DMA mask for platform devices if the they weren't
525 * previously set by the architecture / DT.
527 static void setup_pdev_dma_masks(struct platform_device *pdev)
529 pdev->dev.dma_parms = &pdev->dma_parms;
531 if (!pdev->dev.coherent_dma_mask)
532 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
533 if (!pdev->dev.dma_mask) {
534 pdev->platform_dma_mask = DMA_BIT_MASK(32);
535 pdev->dev.dma_mask = &pdev->platform_dma_mask;
540 * platform_device_put - destroy a platform device
541 * @pdev: platform device to free
543 * Free all memory associated with a platform device. This function must
544 * _only_ be externally called in error cases. All other usage is a bug.
546 void platform_device_put(struct platform_device *pdev)
548 if (!IS_ERR_OR_NULL(pdev))
549 put_device(&pdev->dev);
551 EXPORT_SYMBOL_GPL(platform_device_put);
553 static void platform_device_release(struct device *dev)
555 struct platform_object *pa = container_of(dev, struct platform_object,
558 of_node_put(pa->pdev.dev.of_node);
559 kfree(pa->pdev.dev.platform_data);
560 kfree(pa->pdev.mfd_cell);
561 kfree(pa->pdev.resource);
562 kfree(pa->pdev.driver_override);
567 * platform_device_alloc - create a platform device
568 * @name: base name of the device we're adding
571 * Create a platform device object which can have other objects attached
572 * to it, and which will have attached objects freed when it is released.
574 struct platform_device *platform_device_alloc(const char *name, int id)
576 struct platform_object *pa;
578 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
580 strcpy(pa->name, name);
581 pa->pdev.name = pa->name;
583 device_initialize(&pa->pdev.dev);
584 pa->pdev.dev.release = platform_device_release;
585 setup_pdev_dma_masks(&pa->pdev);
588 return pa ? &pa->pdev : NULL;
590 EXPORT_SYMBOL_GPL(platform_device_alloc);
593 * platform_device_add_resources - add resources to a platform device
594 * @pdev: platform device allocated by platform_device_alloc to add resources to
595 * @res: set of resources that needs to be allocated for the device
596 * @num: number of resources
598 * Add a copy of the resources to the platform device. The memory
599 * associated with the resources will be freed when the platform device is
602 int platform_device_add_resources(struct platform_device *pdev,
603 const struct resource *res, unsigned int num)
605 struct resource *r = NULL;
608 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
613 kfree(pdev->resource);
615 pdev->num_resources = num;
618 EXPORT_SYMBOL_GPL(platform_device_add_resources);
621 * platform_device_add_data - add platform-specific data to a platform device
622 * @pdev: platform device allocated by platform_device_alloc to add resources to
623 * @data: platform specific data for this platform device
624 * @size: size of platform specific data
626 * Add a copy of platform specific data to the platform device's
627 * platform_data pointer. The memory associated with the platform data
628 * will be freed when the platform device is released.
630 int platform_device_add_data(struct platform_device *pdev, const void *data,
636 d = kmemdup(data, size, GFP_KERNEL);
641 kfree(pdev->dev.platform_data);
642 pdev->dev.platform_data = d;
645 EXPORT_SYMBOL_GPL(platform_device_add_data);
648 * platform_device_add - add a platform device to device hierarchy
649 * @pdev: platform device we're adding
651 * This is part 2 of platform_device_register(), though may be called
652 * separately _iff_ pdev was allocated by platform_device_alloc().
654 int platform_device_add(struct platform_device *pdev)
662 if (!pdev->dev.parent)
663 pdev->dev.parent = &platform_bus;
665 pdev->dev.bus = &platform_bus_type;
669 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
671 case PLATFORM_DEVID_NONE:
672 dev_set_name(&pdev->dev, "%s", pdev->name);
674 case PLATFORM_DEVID_AUTO:
676 * Automatically allocated device ID. We mark it as such so
677 * that we remember it must be freed, and we append a suffix
678 * to avoid namespace collision with explicit IDs.
680 ret = ida_alloc(&platform_devid_ida, GFP_KERNEL);
684 pdev->id_auto = true;
685 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
689 for (i = 0; i < pdev->num_resources; i++) {
690 struct resource *p, *r = &pdev->resource[i];
693 r->name = dev_name(&pdev->dev);
697 if (resource_type(r) == IORESOURCE_MEM)
699 else if (resource_type(r) == IORESOURCE_IO)
700 p = &ioport_resource;
704 ret = insert_resource(p, r);
706 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
712 pr_debug("Registering platform device '%s'. Parent at %s\n",
713 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
715 ret = device_add(&pdev->dev);
721 ida_free(&platform_devid_ida, pdev->id);
722 pdev->id = PLATFORM_DEVID_AUTO;
726 struct resource *r = &pdev->resource[i];
734 EXPORT_SYMBOL_GPL(platform_device_add);
737 * platform_device_del - remove a platform-level device
738 * @pdev: platform device we're removing
740 * Note that this function will also release all memory- and port-based
741 * resources owned by the device (@dev->resource). This function must
742 * _only_ be externally called in error cases. All other usage is a bug.
744 void platform_device_del(struct platform_device *pdev)
748 if (!IS_ERR_OR_NULL(pdev)) {
749 device_del(&pdev->dev);
752 ida_free(&platform_devid_ida, pdev->id);
753 pdev->id = PLATFORM_DEVID_AUTO;
756 for (i = 0; i < pdev->num_resources; i++) {
757 struct resource *r = &pdev->resource[i];
763 EXPORT_SYMBOL_GPL(platform_device_del);
766 * platform_device_register - add a platform-level device
767 * @pdev: platform device we're adding
769 * NOTE: _Never_ directly free @pdev after calling this function, even if it
770 * returned an error! Always use platform_device_put() to give up the
771 * reference initialised in this function instead.
773 int platform_device_register(struct platform_device *pdev)
775 device_initialize(&pdev->dev);
776 setup_pdev_dma_masks(pdev);
777 return platform_device_add(pdev);
779 EXPORT_SYMBOL_GPL(platform_device_register);
782 * platform_device_unregister - unregister a platform-level device
783 * @pdev: platform device we're unregistering
785 * Unregistration is done in 2 steps. First we release all resources
786 * and remove it from the subsystem, then we drop reference count by
787 * calling platform_device_put().
789 void platform_device_unregister(struct platform_device *pdev)
791 platform_device_del(pdev);
792 platform_device_put(pdev);
794 EXPORT_SYMBOL_GPL(platform_device_unregister);
797 * platform_device_register_full - add a platform-level device with
798 * resources and platform-specific data
800 * @pdevinfo: data used to create device
802 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
804 struct platform_device *platform_device_register_full(
805 const struct platform_device_info *pdevinfo)
808 struct platform_device *pdev;
810 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
812 return ERR_PTR(-ENOMEM);
814 pdev->dev.parent = pdevinfo->parent;
815 pdev->dev.fwnode = pdevinfo->fwnode;
816 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
817 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
819 if (pdevinfo->dma_mask) {
820 pdev->platform_dma_mask = pdevinfo->dma_mask;
821 pdev->dev.dma_mask = &pdev->platform_dma_mask;
822 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
825 ret = platform_device_add_resources(pdev,
826 pdevinfo->res, pdevinfo->num_res);
830 ret = platform_device_add_data(pdev,
831 pdevinfo->data, pdevinfo->size_data);
835 if (pdevinfo->properties) {
836 ret = device_create_managed_software_node(&pdev->dev,
837 pdevinfo->properties, NULL);
842 ret = platform_device_add(pdev);
845 ACPI_COMPANION_SET(&pdev->dev, NULL);
846 platform_device_put(pdev);
852 EXPORT_SYMBOL_GPL(platform_device_register_full);
855 * __platform_driver_register - register a driver for platform-level devices
856 * @drv: platform driver structure
857 * @owner: owning module/driver
859 int __platform_driver_register(struct platform_driver *drv,
860 struct module *owner)
862 drv->driver.owner = owner;
863 drv->driver.bus = &platform_bus_type;
865 return driver_register(&drv->driver);
867 EXPORT_SYMBOL_GPL(__platform_driver_register);
870 * platform_driver_unregister - unregister a driver for platform-level devices
871 * @drv: platform driver structure
873 void platform_driver_unregister(struct platform_driver *drv)
875 driver_unregister(&drv->driver);
877 EXPORT_SYMBOL_GPL(platform_driver_unregister);
879 static int platform_probe_fail(struct platform_device *pdev)
885 * __platform_driver_probe - register driver for non-hotpluggable device
886 * @drv: platform driver structure
887 * @probe: the driver probe routine, probably from an __init section
888 * @module: module which will be the owner of the driver
890 * Use this instead of platform_driver_register() when you know the device
891 * is not hotpluggable and has already been registered, and you want to
892 * remove its run-once probe() infrastructure from memory after the driver
893 * has bound to the device.
895 * One typical use for this would be with drivers for controllers integrated
896 * into system-on-chip processors, where the controller devices have been
897 * configured as part of board setup.
899 * Note that this is incompatible with deferred probing.
901 * Returns zero if the driver registered and bound to a device, else returns
902 * a negative error code and with the driver not registered.
904 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
905 int (*probe)(struct platform_device *), struct module *module)
909 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
910 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
911 drv->driver.name, __func__);
916 * We have to run our probes synchronously because we check if
917 * we find any devices to bind to and exit with error if there
920 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
923 * Prevent driver from requesting probe deferral to avoid further
924 * futile probe attempts.
926 drv->prevent_deferred_probe = true;
928 /* make sure driver won't have bind/unbind attributes */
929 drv->driver.suppress_bind_attrs = true;
931 /* temporary section violation during probe() */
933 retval = code = __platform_driver_register(drv, module);
938 * Fixup that section violation, being paranoid about code scanning
939 * the list of drivers in order to probe new devices. Check to see
940 * if the probe was successful, and make sure any forced probes of
943 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
944 drv->probe = platform_probe_fail;
945 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
947 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
950 platform_driver_unregister(drv);
953 EXPORT_SYMBOL_GPL(__platform_driver_probe);
956 * __platform_create_bundle - register driver and create corresponding device
957 * @driver: platform driver structure
958 * @probe: the driver probe routine, probably from an __init section
959 * @res: set of resources that needs to be allocated for the device
960 * @n_res: number of resources
961 * @data: platform specific data for this platform device
962 * @size: size of platform specific data
963 * @module: module which will be the owner of the driver
965 * Use this in legacy-style modules that probe hardware directly and
966 * register a single platform device and corresponding platform driver.
968 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
970 struct platform_device * __init_or_module __platform_create_bundle(
971 struct platform_driver *driver,
972 int (*probe)(struct platform_device *),
973 struct resource *res, unsigned int n_res,
974 const void *data, size_t size, struct module *module)
976 struct platform_device *pdev;
979 pdev = platform_device_alloc(driver->driver.name, -1);
985 error = platform_device_add_resources(pdev, res, n_res);
989 error = platform_device_add_data(pdev, data, size);
993 error = platform_device_add(pdev);
997 error = __platform_driver_probe(driver, probe, module);
1004 platform_device_del(pdev);
1006 platform_device_put(pdev);
1008 return ERR_PTR(error);
1010 EXPORT_SYMBOL_GPL(__platform_create_bundle);
1013 * __platform_register_drivers - register an array of platform drivers
1014 * @drivers: an array of drivers to register
1015 * @count: the number of drivers to register
1016 * @owner: module owning the drivers
1018 * Registers platform drivers specified by an array. On failure to register a
1019 * driver, all previously registered drivers will be unregistered. Callers of
1020 * this API should use platform_unregister_drivers() to unregister drivers in
1021 * the reverse order.
1023 * Returns: 0 on success or a negative error code on failure.
1025 int __platform_register_drivers(struct platform_driver * const *drivers,
1026 unsigned int count, struct module *owner)
1031 for (i = 0; i < count; i++) {
1032 pr_debug("registering platform driver %ps\n", drivers[i]);
1034 err = __platform_driver_register(drivers[i], owner);
1036 pr_err("failed to register platform driver %ps: %d\n",
1046 pr_debug("unregistering platform driver %ps\n", drivers[i]);
1047 platform_driver_unregister(drivers[i]);
1052 EXPORT_SYMBOL_GPL(__platform_register_drivers);
1055 * platform_unregister_drivers - unregister an array of platform drivers
1056 * @drivers: an array of drivers to unregister
1057 * @count: the number of drivers to unregister
1059 * Unregisters platform drivers specified by an array. This is typically used
1060 * to complement an earlier call to platform_register_drivers(). Drivers are
1061 * unregistered in the reverse order in which they were registered.
1063 void platform_unregister_drivers(struct platform_driver * const *drivers,
1067 pr_debug("unregistering platform driver %ps\n", drivers[count]);
1068 platform_driver_unregister(drivers[count]);
1071 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
1073 static const struct platform_device_id *platform_match_id(
1074 const struct platform_device_id *id,
1075 struct platform_device *pdev)
1077 while (id->name[0]) {
1078 if (strcmp(pdev->name, id->name) == 0) {
1079 pdev->id_entry = id;
1087 #ifdef CONFIG_PM_SLEEP
1089 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1091 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1092 struct platform_device *pdev = to_platform_device(dev);
1095 if (dev->driver && pdrv->suspend)
1096 ret = pdrv->suspend(pdev, mesg);
1101 static int platform_legacy_resume(struct device *dev)
1103 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1104 struct platform_device *pdev = to_platform_device(dev);
1107 if (dev->driver && pdrv->resume)
1108 ret = pdrv->resume(pdev);
1113 #endif /* CONFIG_PM_SLEEP */
1115 #ifdef CONFIG_SUSPEND
1117 int platform_pm_suspend(struct device *dev)
1119 struct device_driver *drv = dev->driver;
1126 if (drv->pm->suspend)
1127 ret = drv->pm->suspend(dev);
1129 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1135 int platform_pm_resume(struct device *dev)
1137 struct device_driver *drv = dev->driver;
1144 if (drv->pm->resume)
1145 ret = drv->pm->resume(dev);
1147 ret = platform_legacy_resume(dev);
1153 #endif /* CONFIG_SUSPEND */
1155 #ifdef CONFIG_HIBERNATE_CALLBACKS
1157 int platform_pm_freeze(struct device *dev)
1159 struct device_driver *drv = dev->driver;
1166 if (drv->pm->freeze)
1167 ret = drv->pm->freeze(dev);
1169 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1175 int platform_pm_thaw(struct device *dev)
1177 struct device_driver *drv = dev->driver;
1185 ret = drv->pm->thaw(dev);
1187 ret = platform_legacy_resume(dev);
1193 int platform_pm_poweroff(struct device *dev)
1195 struct device_driver *drv = dev->driver;
1202 if (drv->pm->poweroff)
1203 ret = drv->pm->poweroff(dev);
1205 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1211 int platform_pm_restore(struct device *dev)
1213 struct device_driver *drv = dev->driver;
1220 if (drv->pm->restore)
1221 ret = drv->pm->restore(dev);
1223 ret = platform_legacy_resume(dev);
1229 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1231 /* modalias support enables more hands-off userspace setup:
1232 * (a) environment variable lets new-style hotplug events work once system is
1233 * fully running: "modprobe $MODALIAS"
1234 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
1235 * mishandled before system is fully running: "modprobe $(cat modalias)"
1237 static ssize_t modalias_show(struct device *dev,
1238 struct device_attribute *attr, char *buf)
1240 struct platform_device *pdev = to_platform_device(dev);
1243 len = of_device_modalias(dev, buf, PAGE_SIZE);
1247 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
1251 return sysfs_emit(buf, "platform:%s\n", pdev->name);
1253 static DEVICE_ATTR_RO(modalias);
1255 static ssize_t numa_node_show(struct device *dev,
1256 struct device_attribute *attr, char *buf)
1258 return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1260 static DEVICE_ATTR_RO(numa_node);
1262 static ssize_t driver_override_show(struct device *dev,
1263 struct device_attribute *attr, char *buf)
1265 struct platform_device *pdev = to_platform_device(dev);
1269 len = sysfs_emit(buf, "%s\n", pdev->driver_override);
1275 static ssize_t driver_override_store(struct device *dev,
1276 struct device_attribute *attr,
1277 const char *buf, size_t count)
1279 struct platform_device *pdev = to_platform_device(dev);
1280 char *driver_override, *old, *cp;
1282 /* We need to keep extra room for a newline */
1283 if (count >= (PAGE_SIZE - 1))
1286 driver_override = kstrndup(buf, count, GFP_KERNEL);
1287 if (!driver_override)
1290 cp = strchr(driver_override, '\n');
1295 old = pdev->driver_override;
1296 if (strlen(driver_override)) {
1297 pdev->driver_override = driver_override;
1299 kfree(driver_override);
1300 pdev->driver_override = NULL;
1308 static DEVICE_ATTR_RW(driver_override);
1310 static struct attribute *platform_dev_attrs[] = {
1311 &dev_attr_modalias.attr,
1312 &dev_attr_numa_node.attr,
1313 &dev_attr_driver_override.attr,
1317 static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a,
1320 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1322 if (a == &dev_attr_numa_node.attr &&
1323 dev_to_node(dev) == NUMA_NO_NODE)
1329 static const struct attribute_group platform_dev_group = {
1330 .attrs = platform_dev_attrs,
1331 .is_visible = platform_dev_attrs_visible,
1333 __ATTRIBUTE_GROUPS(platform_dev);
1337 * platform_match - bind platform device to platform driver.
1341 * Platform device IDs are assumed to be encoded like this:
1342 * "<name><instance>", where <name> is a short description of the type of
1343 * device, like "pci" or "floppy", and <instance> is the enumerated
1344 * instance of the device, like '0' or '42'. Driver IDs are simply
1345 * "<name>". So, extract the <name> from the platform_device structure,
1346 * and compare it against the name of the driver. Return whether they match
1349 static int platform_match(struct device *dev, struct device_driver *drv)
1351 struct platform_device *pdev = to_platform_device(dev);
1352 struct platform_driver *pdrv = to_platform_driver(drv);
1354 /* When driver_override is set, only bind to the matching driver */
1355 if (pdev->driver_override)
1356 return !strcmp(pdev->driver_override, drv->name);
1358 /* Attempt an OF style match first */
1359 if (of_driver_match_device(dev, drv))
1362 /* Then try ACPI style match */
1363 if (acpi_driver_match_device(dev, drv))
1366 /* Then try to match against the id table */
1368 return platform_match_id(pdrv->id_table, pdev) != NULL;
1370 /* fall-back to driver name match */
1371 return (strcmp(pdev->name, drv->name) == 0);
1374 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1376 struct platform_device *pdev = to_platform_device(dev);
1379 /* Some devices have extra OF data and an OF-style MODALIAS */
1380 rc = of_device_uevent_modalias(dev, env);
1384 rc = acpi_device_uevent_modalias(dev, env);
1388 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1393 static int platform_probe(struct device *_dev)
1395 struct platform_driver *drv = to_platform_driver(_dev->driver);
1396 struct platform_device *dev = to_platform_device(_dev);
1400 * A driver registered using platform_driver_probe() cannot be bound
1401 * again later because the probe function usually lives in __init code
1402 * and so is gone. For these drivers .probe is set to
1403 * platform_probe_fail in __platform_driver_probe(). Don't even prepare
1404 * clocks and PM domains for these to match the traditional behaviour.
1406 if (unlikely(drv->probe == platform_probe_fail))
1409 ret = of_clk_set_defaults(_dev->of_node, false);
1413 ret = dev_pm_domain_attach(_dev, true);
1418 ret = drv->probe(dev);
1420 dev_pm_domain_detach(_dev, true);
1424 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
1425 dev_warn(_dev, "probe deferral not supported\n");
1432 static void platform_remove(struct device *_dev)
1434 struct platform_driver *drv = to_platform_driver(_dev->driver);
1435 struct platform_device *dev = to_platform_device(_dev);
1438 int ret = drv->remove(dev);
1441 dev_warn(_dev, "remove callback returned a non-zero value. This will be ignored.\n");
1443 dev_pm_domain_detach(_dev, true);
1446 static void platform_shutdown(struct device *_dev)
1448 struct platform_device *dev = to_platform_device(_dev);
1449 struct platform_driver *drv;
1454 drv = to_platform_driver(_dev->driver);
1459 static int platform_dma_configure(struct device *dev)
1461 struct platform_driver *drv = to_platform_driver(dev->driver);
1462 enum dev_dma_attr attr;
1466 ret = of_dma_configure(dev, dev->of_node, true);
1467 } else if (has_acpi_companion(dev)) {
1468 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1469 ret = acpi_dma_configure(dev, attr);
1472 if (!ret && !drv->driver_managed_dma) {
1473 ret = iommu_device_use_default_domain(dev);
1475 arch_teardown_dma_ops(dev);
1481 static void platform_dma_cleanup(struct device *dev)
1483 struct platform_driver *drv = to_platform_driver(dev->driver);
1485 if (!drv->driver_managed_dma)
1486 iommu_device_unuse_default_domain(dev);
1489 static const struct dev_pm_ops platform_dev_pm_ops = {
1490 SET_RUNTIME_PM_OPS(pm_generic_runtime_suspend, pm_generic_runtime_resume, NULL)
1491 USE_PLATFORM_PM_SLEEP_OPS
1494 struct bus_type platform_bus_type = {
1496 .dev_groups = platform_dev_groups,
1497 .match = platform_match,
1498 .uevent = platform_uevent,
1499 .probe = platform_probe,
1500 .remove = platform_remove,
1501 .shutdown = platform_shutdown,
1502 .dma_configure = platform_dma_configure,
1503 .dma_cleanup = platform_dma_cleanup,
1504 .pm = &platform_dev_pm_ops,
1506 EXPORT_SYMBOL_GPL(platform_bus_type);
1508 static inline int __platform_match(struct device *dev, const void *drv)
1510 return platform_match(dev, (struct device_driver *)drv);
1514 * platform_find_device_by_driver - Find a platform device with a given
1516 * @start: The device to start the search from.
1517 * @drv: The device driver to look for.
1519 struct device *platform_find_device_by_driver(struct device *start,
1520 const struct device_driver *drv)
1522 return bus_find_device(&platform_bus_type, start, drv,
1525 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1527 void __weak __init early_platform_cleanup(void) { }
1529 int __init platform_bus_init(void)
1533 early_platform_cleanup();
1535 error = device_register(&platform_bus);
1537 put_device(&platform_bus);
1540 error = bus_register(&platform_bus_type);
1542 device_unregister(&platform_bus);
1543 of_platform_register_reconfig_notifier();