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>
35 #include "power/power.h"
37 /* For automatically allocated device IDs */
38 static DEFINE_IDA(platform_devid_ida);
40 struct device platform_bus = {
41 .init_name = "platform",
43 EXPORT_SYMBOL_GPL(platform_bus);
46 * platform_get_resource - get a resource for a device
47 * @dev: platform device
48 * @type: resource type
49 * @num: resource index
51 * Return: a pointer to the resource or NULL on failure.
53 struct resource *platform_get_resource(struct platform_device *dev,
54 unsigned int type, unsigned int num)
58 for (i = 0; i < dev->num_resources; i++) {
59 struct resource *r = &dev->resource[i];
61 if (type == resource_type(r) && num-- == 0)
66 EXPORT_SYMBOL_GPL(platform_get_resource);
68 struct resource *platform_get_mem_or_io(struct platform_device *dev,
73 for (i = 0; i < dev->num_resources; i++) {
74 struct resource *r = &dev->resource[i];
76 if ((resource_type(r) & (IORESOURCE_MEM|IORESOURCE_IO)) && num-- == 0)
81 EXPORT_SYMBOL_GPL(platform_get_mem_or_io);
83 #ifdef CONFIG_HAS_IOMEM
85 * devm_platform_get_and_ioremap_resource - call devm_ioremap_resource() for a
86 * platform device and get resource
88 * @pdev: platform device to use both for memory resource lookup as well as
90 * @index: resource index
91 * @res: optional output parameter to store a pointer to the obtained resource.
93 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
97 devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
98 unsigned int index, struct resource **res)
102 r = platform_get_resource(pdev, IORESOURCE_MEM, index);
105 return devm_ioremap_resource(&pdev->dev, r);
107 EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource);
110 * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
113 * @pdev: platform device to use both for memory resource lookup as well as
114 * resource management
115 * @index: resource index
117 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
120 void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
123 return devm_platform_get_and_ioremap_resource(pdev, index, NULL);
125 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
128 * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for
129 * a platform device, retrieve the
132 * @pdev: platform device to use both for memory resource lookup as well as
133 * resource management
134 * @name: name of the resource
136 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
140 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
143 struct resource *res;
145 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
146 return devm_ioremap_resource(&pdev->dev, res);
148 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname);
149 #endif /* CONFIG_HAS_IOMEM */
152 * platform_get_irq_optional - get an optional IRQ for a device
153 * @dev: platform device
154 * @num: IRQ number index
156 * Gets an IRQ for a platform device. Device drivers should check the return
157 * value for errors so as to not pass a negative integer value to the
158 * request_irq() APIs. This is the same as platform_get_irq(), except that it
159 * does not print an error message if an IRQ can not be obtained.
163 * int irq = platform_get_irq_optional(pdev, 0);
167 * Return: non-zero IRQ number on success, negative error number on failure.
169 int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
173 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
174 if (!dev || num >= dev->archdata.num_irqs)
176 ret = dev->archdata.irqs[num];
181 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
182 ret = of_irq_get(dev->dev.of_node, num);
183 if (ret > 0 || ret == -EPROBE_DEFER)
187 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
188 if (has_acpi_companion(&dev->dev)) {
189 if (r && r->flags & IORESOURCE_DISABLED) {
190 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
197 * The resources may pass trigger flags to the irqs that need
198 * to be set up. It so happens that the trigger flags for
199 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
202 if (r && r->flags & IORESOURCE_BITS) {
203 struct irq_data *irqd;
205 irqd = irq_get_irq_data(r->start);
208 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
217 * For the index 0 interrupt, allow falling back to GpioInt
218 * resources. While a device could have both Interrupt and GpioInt
219 * resources, making this fallback ambiguous, in many common cases
220 * the device will only expose one IRQ, and this fallback
221 * allows a common code path across either kind of resource.
223 if (num == 0 && has_acpi_companion(&dev->dev)) {
224 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
225 /* Our callers expect -ENXIO for missing IRQs. */
226 if (ret >= 0 || ret == -EPROBE_DEFER)
234 WARN(ret == 0, "0 is an invalid IRQ number\n");
237 EXPORT_SYMBOL_GPL(platform_get_irq_optional);
240 * platform_get_irq - get an IRQ for a device
241 * @dev: platform device
242 * @num: IRQ number index
244 * Gets an IRQ for a platform device and prints an error message if finding the
245 * IRQ fails. Device drivers should check the return value for errors so as to
246 * not pass a negative integer value to the request_irq() APIs.
250 * int irq = platform_get_irq(pdev, 0);
254 * Return: non-zero IRQ number on success, negative error number on failure.
256 int platform_get_irq(struct platform_device *dev, unsigned int num)
260 ret = platform_get_irq_optional(dev, num);
262 return dev_err_probe(&dev->dev, ret,
263 "IRQ index %u not found\n", num);
267 EXPORT_SYMBOL_GPL(platform_get_irq);
270 * platform_irq_count - Count the number of IRQs a platform device uses
271 * @dev: platform device
273 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
275 int platform_irq_count(struct platform_device *dev)
279 while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
282 if (ret == -EPROBE_DEFER)
287 EXPORT_SYMBOL_GPL(platform_irq_count);
289 struct irq_affinity_devres {
294 static void platform_disable_acpi_irq(struct platform_device *pdev, int index)
298 r = platform_get_resource(pdev, IORESOURCE_IRQ, index);
300 irqresource_disabled(r, 0);
303 static void devm_platform_get_irqs_affinity_release(struct device *dev,
306 struct irq_affinity_devres *ptr = res;
309 for (i = 0; i < ptr->count; i++) {
310 irq_dispose_mapping(ptr->irq[i]);
312 if (has_acpi_companion(dev))
313 platform_disable_acpi_irq(to_platform_device(dev), i);
318 * devm_platform_get_irqs_affinity - devm method to get a set of IRQs for a
319 * device using an interrupt affinity descriptor
320 * @dev: platform device pointer
321 * @affd: affinity descriptor
322 * @minvec: minimum count of interrupt vectors
323 * @maxvec: maximum count of interrupt vectors
324 * @irqs: pointer holder for IRQ numbers
326 * Gets a set of IRQs for a platform device, and updates IRQ afffinty according
327 * to the passed affinity descriptor
329 * Return: Number of vectors on success, negative error number on failure.
331 int devm_platform_get_irqs_affinity(struct platform_device *dev,
332 struct irq_affinity *affd,
337 struct irq_affinity_devres *ptr;
338 struct irq_affinity_desc *desc;
348 nvec = platform_irq_count(dev);
355 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
362 size = sizeof(*ptr) + sizeof(unsigned int) * nvec;
363 ptr = devres_alloc(devm_platform_get_irqs_affinity_release, size,
370 for (i = 0; i < nvec; i++) {
371 int irq = platform_get_irq(dev, i);
374 goto err_free_devres;
379 desc = irq_create_affinity_masks(nvec, affd);
382 goto err_free_devres;
385 for (i = 0; i < nvec; i++) {
386 ret = irq_update_affinity_desc(ptr->irq[i], &desc[i]);
388 dev_err(&dev->dev, "failed to update irq%d affinity descriptor (%d)\n",
394 devres_add(&dev->dev, ptr);
408 EXPORT_SYMBOL_GPL(devm_platform_get_irqs_affinity);
411 * platform_get_resource_byname - get a resource for a device by name
412 * @dev: platform device
413 * @type: resource type
414 * @name: resource name
416 struct resource *platform_get_resource_byname(struct platform_device *dev,
422 for (i = 0; i < dev->num_resources; i++) {
423 struct resource *r = &dev->resource[i];
425 if (unlikely(!r->name))
428 if (type == resource_type(r) && !strcmp(r->name, name))
433 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
435 static int __platform_get_irq_byname(struct platform_device *dev,
441 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
442 ret = of_irq_get_byname(dev->dev.of_node, name);
443 if (ret > 0 || ret == -EPROBE_DEFER)
447 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
449 WARN(r->start == 0, "0 is an invalid IRQ number\n");
457 * platform_get_irq_byname - get an IRQ for a device by name
458 * @dev: platform device
461 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
463 * Return: non-zero IRQ number on success, negative error number on failure.
465 int platform_get_irq_byname(struct platform_device *dev, const char *name)
469 ret = __platform_get_irq_byname(dev, name);
470 if (ret < 0 && ret != -EPROBE_DEFER)
471 dev_err(&dev->dev, "IRQ %s not found\n", name);
475 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
478 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
479 * @dev: platform device
482 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
483 * does not print an error message if an IRQ can not be obtained.
485 * Return: non-zero IRQ number on success, negative error number on failure.
487 int platform_get_irq_byname_optional(struct platform_device *dev,
490 return __platform_get_irq_byname(dev, name);
492 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
495 * platform_add_devices - add a numbers of platform devices
496 * @devs: array of platform devices to add
497 * @num: number of platform devices in array
499 int platform_add_devices(struct platform_device **devs, int num)
503 for (i = 0; i < num; i++) {
504 ret = platform_device_register(devs[i]);
507 platform_device_unregister(devs[i]);
514 EXPORT_SYMBOL_GPL(platform_add_devices);
516 struct platform_object {
517 struct platform_device pdev;
522 * Set up default DMA mask for platform devices if the they weren't
523 * previously set by the architecture / DT.
525 static void setup_pdev_dma_masks(struct platform_device *pdev)
527 pdev->dev.dma_parms = &pdev->dma_parms;
529 if (!pdev->dev.coherent_dma_mask)
530 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
531 if (!pdev->dev.dma_mask) {
532 pdev->platform_dma_mask = DMA_BIT_MASK(32);
533 pdev->dev.dma_mask = &pdev->platform_dma_mask;
538 * platform_device_put - destroy a platform device
539 * @pdev: platform device to free
541 * Free all memory associated with a platform device. This function must
542 * _only_ be externally called in error cases. All other usage is a bug.
544 void platform_device_put(struct platform_device *pdev)
546 if (!IS_ERR_OR_NULL(pdev))
547 put_device(&pdev->dev);
549 EXPORT_SYMBOL_GPL(platform_device_put);
551 static void platform_device_release(struct device *dev)
553 struct platform_object *pa = container_of(dev, struct platform_object,
556 of_node_put(pa->pdev.dev.of_node);
557 kfree(pa->pdev.dev.platform_data);
558 kfree(pa->pdev.mfd_cell);
559 kfree(pa->pdev.resource);
560 kfree(pa->pdev.driver_override);
565 * platform_device_alloc - create a platform device
566 * @name: base name of the device we're adding
569 * Create a platform device object which can have other objects attached
570 * to it, and which will have attached objects freed when it is released.
572 struct platform_device *platform_device_alloc(const char *name, int id)
574 struct platform_object *pa;
576 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
578 strcpy(pa->name, name);
579 pa->pdev.name = pa->name;
581 device_initialize(&pa->pdev.dev);
582 pa->pdev.dev.release = platform_device_release;
583 setup_pdev_dma_masks(&pa->pdev);
586 return pa ? &pa->pdev : NULL;
588 EXPORT_SYMBOL_GPL(platform_device_alloc);
591 * platform_device_add_resources - add resources to a platform device
592 * @pdev: platform device allocated by platform_device_alloc to add resources to
593 * @res: set of resources that needs to be allocated for the device
594 * @num: number of resources
596 * Add a copy of the resources to the platform device. The memory
597 * associated with the resources will be freed when the platform device is
600 int platform_device_add_resources(struct platform_device *pdev,
601 const struct resource *res, unsigned int num)
603 struct resource *r = NULL;
606 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
611 kfree(pdev->resource);
613 pdev->num_resources = num;
616 EXPORT_SYMBOL_GPL(platform_device_add_resources);
619 * platform_device_add_data - add platform-specific data to a platform device
620 * @pdev: platform device allocated by platform_device_alloc to add resources to
621 * @data: platform specific data for this platform device
622 * @size: size of platform specific data
624 * Add a copy of platform specific data to the platform device's
625 * platform_data pointer. The memory associated with the platform data
626 * will be freed when the platform device is released.
628 int platform_device_add_data(struct platform_device *pdev, const void *data,
634 d = kmemdup(data, size, GFP_KERNEL);
639 kfree(pdev->dev.platform_data);
640 pdev->dev.platform_data = d;
643 EXPORT_SYMBOL_GPL(platform_device_add_data);
646 * platform_device_add - add a platform device to device hierarchy
647 * @pdev: platform device we're adding
649 * This is part 2 of platform_device_register(), though may be called
650 * separately _iff_ pdev was allocated by platform_device_alloc().
652 int platform_device_add(struct platform_device *pdev)
660 if (!pdev->dev.parent)
661 pdev->dev.parent = &platform_bus;
663 pdev->dev.bus = &platform_bus_type;
667 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
669 case PLATFORM_DEVID_NONE:
670 dev_set_name(&pdev->dev, "%s", pdev->name);
672 case PLATFORM_DEVID_AUTO:
674 * Automatically allocated device ID. We mark it as such so
675 * that we remember it must be freed, and we append a suffix
676 * to avoid namespace collision with explicit IDs.
678 ret = ida_alloc(&platform_devid_ida, GFP_KERNEL);
682 pdev->id_auto = true;
683 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
687 for (i = 0; i < pdev->num_resources; i++) {
688 struct resource *p, *r = &pdev->resource[i];
691 r->name = dev_name(&pdev->dev);
695 if (resource_type(r) == IORESOURCE_MEM)
697 else if (resource_type(r) == IORESOURCE_IO)
698 p = &ioport_resource;
702 ret = insert_resource(p, r);
704 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
710 pr_debug("Registering platform device '%s'. Parent at %s\n",
711 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
713 ret = device_add(&pdev->dev);
719 ida_free(&platform_devid_ida, pdev->id);
720 pdev->id = PLATFORM_DEVID_AUTO;
724 struct resource *r = &pdev->resource[i];
732 EXPORT_SYMBOL_GPL(platform_device_add);
735 * platform_device_del - remove a platform-level device
736 * @pdev: platform device we're removing
738 * Note that this function will also release all memory- and port-based
739 * resources owned by the device (@dev->resource). This function must
740 * _only_ be externally called in error cases. All other usage is a bug.
742 void platform_device_del(struct platform_device *pdev)
746 if (!IS_ERR_OR_NULL(pdev)) {
747 device_del(&pdev->dev);
750 ida_free(&platform_devid_ida, pdev->id);
751 pdev->id = PLATFORM_DEVID_AUTO;
754 for (i = 0; i < pdev->num_resources; i++) {
755 struct resource *r = &pdev->resource[i];
761 EXPORT_SYMBOL_GPL(platform_device_del);
764 * platform_device_register - add a platform-level device
765 * @pdev: platform device we're adding
767 * NOTE: _Never_ directly free @pdev after calling this function, even if it
768 * returned an error! Always use platform_device_put() to give up the
769 * reference initialised in this function instead.
771 int platform_device_register(struct platform_device *pdev)
773 device_initialize(&pdev->dev);
774 setup_pdev_dma_masks(pdev);
775 return platform_device_add(pdev);
777 EXPORT_SYMBOL_GPL(platform_device_register);
780 * platform_device_unregister - unregister a platform-level device
781 * @pdev: platform device we're unregistering
783 * Unregistration is done in 2 steps. First we release all resources
784 * and remove it from the subsystem, then we drop reference count by
785 * calling platform_device_put().
787 void platform_device_unregister(struct platform_device *pdev)
789 platform_device_del(pdev);
790 platform_device_put(pdev);
792 EXPORT_SYMBOL_GPL(platform_device_unregister);
795 * platform_device_register_full - add a platform-level device with
796 * resources and platform-specific data
798 * @pdevinfo: data used to create device
800 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
802 struct platform_device *platform_device_register_full(
803 const struct platform_device_info *pdevinfo)
806 struct platform_device *pdev;
808 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
810 return ERR_PTR(-ENOMEM);
812 pdev->dev.parent = pdevinfo->parent;
813 pdev->dev.fwnode = pdevinfo->fwnode;
814 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
815 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
817 if (pdevinfo->dma_mask) {
818 pdev->platform_dma_mask = pdevinfo->dma_mask;
819 pdev->dev.dma_mask = &pdev->platform_dma_mask;
820 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
823 ret = platform_device_add_resources(pdev,
824 pdevinfo->res, pdevinfo->num_res);
828 ret = platform_device_add_data(pdev,
829 pdevinfo->data, pdevinfo->size_data);
833 if (pdevinfo->properties) {
834 ret = device_create_managed_software_node(&pdev->dev,
835 pdevinfo->properties, NULL);
840 ret = platform_device_add(pdev);
843 ACPI_COMPANION_SET(&pdev->dev, NULL);
844 platform_device_put(pdev);
850 EXPORT_SYMBOL_GPL(platform_device_register_full);
853 * __platform_driver_register - register a driver for platform-level devices
854 * @drv: platform driver structure
855 * @owner: owning module/driver
857 int __platform_driver_register(struct platform_driver *drv,
858 struct module *owner)
860 drv->driver.owner = owner;
861 drv->driver.bus = &platform_bus_type;
863 return driver_register(&drv->driver);
865 EXPORT_SYMBOL_GPL(__platform_driver_register);
868 * platform_driver_unregister - unregister a driver for platform-level devices
869 * @drv: platform driver structure
871 void platform_driver_unregister(struct platform_driver *drv)
873 driver_unregister(&drv->driver);
875 EXPORT_SYMBOL_GPL(platform_driver_unregister);
877 static int platform_probe_fail(struct platform_device *pdev)
883 * __platform_driver_probe - register driver for non-hotpluggable device
884 * @drv: platform driver structure
885 * @probe: the driver probe routine, probably from an __init section
886 * @module: module which will be the owner of the driver
888 * Use this instead of platform_driver_register() when you know the device
889 * is not hotpluggable and has already been registered, and you want to
890 * remove its run-once probe() infrastructure from memory after the driver
891 * has bound to the device.
893 * One typical use for this would be with drivers for controllers integrated
894 * into system-on-chip processors, where the controller devices have been
895 * configured as part of board setup.
897 * Note that this is incompatible with deferred probing.
899 * Returns zero if the driver registered and bound to a device, else returns
900 * a negative error code and with the driver not registered.
902 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
903 int (*probe)(struct platform_device *), struct module *module)
907 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
908 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
909 drv->driver.name, __func__);
914 * We have to run our probes synchronously because we check if
915 * we find any devices to bind to and exit with error if there
918 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
921 * Prevent driver from requesting probe deferral to avoid further
922 * futile probe attempts.
924 drv->prevent_deferred_probe = true;
926 /* make sure driver won't have bind/unbind attributes */
927 drv->driver.suppress_bind_attrs = true;
929 /* temporary section violation during probe() */
931 retval = code = __platform_driver_register(drv, module);
936 * Fixup that section violation, being paranoid about code scanning
937 * the list of drivers in order to probe new devices. Check to see
938 * if the probe was successful, and make sure any forced probes of
941 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
942 drv->probe = platform_probe_fail;
943 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
945 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
948 platform_driver_unregister(drv);
951 EXPORT_SYMBOL_GPL(__platform_driver_probe);
954 * __platform_create_bundle - register driver and create corresponding device
955 * @driver: platform driver structure
956 * @probe: the driver probe routine, probably from an __init section
957 * @res: set of resources that needs to be allocated for the device
958 * @n_res: number of resources
959 * @data: platform specific data for this platform device
960 * @size: size of platform specific data
961 * @module: module which will be the owner of the driver
963 * Use this in legacy-style modules that probe hardware directly and
964 * register a single platform device and corresponding platform driver.
966 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
968 struct platform_device * __init_or_module __platform_create_bundle(
969 struct platform_driver *driver,
970 int (*probe)(struct platform_device *),
971 struct resource *res, unsigned int n_res,
972 const void *data, size_t size, struct module *module)
974 struct platform_device *pdev;
977 pdev = platform_device_alloc(driver->driver.name, -1);
983 error = platform_device_add_resources(pdev, res, n_res);
987 error = platform_device_add_data(pdev, data, size);
991 error = platform_device_add(pdev);
995 error = __platform_driver_probe(driver, probe, module);
1002 platform_device_del(pdev);
1004 platform_device_put(pdev);
1006 return ERR_PTR(error);
1008 EXPORT_SYMBOL_GPL(__platform_create_bundle);
1011 * __platform_register_drivers - register an array of platform drivers
1012 * @drivers: an array of drivers to register
1013 * @count: the number of drivers to register
1014 * @owner: module owning the drivers
1016 * Registers platform drivers specified by an array. On failure to register a
1017 * driver, all previously registered drivers will be unregistered. Callers of
1018 * this API should use platform_unregister_drivers() to unregister drivers in
1019 * the reverse order.
1021 * Returns: 0 on success or a negative error code on failure.
1023 int __platform_register_drivers(struct platform_driver * const *drivers,
1024 unsigned int count, struct module *owner)
1029 for (i = 0; i < count; i++) {
1030 pr_debug("registering platform driver %ps\n", drivers[i]);
1032 err = __platform_driver_register(drivers[i], owner);
1034 pr_err("failed to register platform driver %ps: %d\n",
1044 pr_debug("unregistering platform driver %ps\n", drivers[i]);
1045 platform_driver_unregister(drivers[i]);
1050 EXPORT_SYMBOL_GPL(__platform_register_drivers);
1053 * platform_unregister_drivers - unregister an array of platform drivers
1054 * @drivers: an array of drivers to unregister
1055 * @count: the number of drivers to unregister
1057 * Unregisters platform drivers specified by an array. This is typically used
1058 * to complement an earlier call to platform_register_drivers(). Drivers are
1059 * unregistered in the reverse order in which they were registered.
1061 void platform_unregister_drivers(struct platform_driver * const *drivers,
1065 pr_debug("unregistering platform driver %ps\n", drivers[count]);
1066 platform_driver_unregister(drivers[count]);
1069 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
1071 static const struct platform_device_id *platform_match_id(
1072 const struct platform_device_id *id,
1073 struct platform_device *pdev)
1075 while (id->name[0]) {
1076 if (strcmp(pdev->name, id->name) == 0) {
1077 pdev->id_entry = id;
1085 #ifdef CONFIG_PM_SLEEP
1087 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1089 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1090 struct platform_device *pdev = to_platform_device(dev);
1093 if (dev->driver && pdrv->suspend)
1094 ret = pdrv->suspend(pdev, mesg);
1099 static int platform_legacy_resume(struct device *dev)
1101 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1102 struct platform_device *pdev = to_platform_device(dev);
1105 if (dev->driver && pdrv->resume)
1106 ret = pdrv->resume(pdev);
1111 #endif /* CONFIG_PM_SLEEP */
1113 #ifdef CONFIG_SUSPEND
1115 int platform_pm_suspend(struct device *dev)
1117 struct device_driver *drv = dev->driver;
1124 if (drv->pm->suspend)
1125 ret = drv->pm->suspend(dev);
1127 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1133 int platform_pm_resume(struct device *dev)
1135 struct device_driver *drv = dev->driver;
1142 if (drv->pm->resume)
1143 ret = drv->pm->resume(dev);
1145 ret = platform_legacy_resume(dev);
1151 #endif /* CONFIG_SUSPEND */
1153 #ifdef CONFIG_HIBERNATE_CALLBACKS
1155 int platform_pm_freeze(struct device *dev)
1157 struct device_driver *drv = dev->driver;
1164 if (drv->pm->freeze)
1165 ret = drv->pm->freeze(dev);
1167 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1173 int platform_pm_thaw(struct device *dev)
1175 struct device_driver *drv = dev->driver;
1183 ret = drv->pm->thaw(dev);
1185 ret = platform_legacy_resume(dev);
1191 int platform_pm_poweroff(struct device *dev)
1193 struct device_driver *drv = dev->driver;
1200 if (drv->pm->poweroff)
1201 ret = drv->pm->poweroff(dev);
1203 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1209 int platform_pm_restore(struct device *dev)
1211 struct device_driver *drv = dev->driver;
1218 if (drv->pm->restore)
1219 ret = drv->pm->restore(dev);
1221 ret = platform_legacy_resume(dev);
1227 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1229 /* modalias support enables more hands-off userspace setup:
1230 * (a) environment variable lets new-style hotplug events work once system is
1231 * fully running: "modprobe $MODALIAS"
1232 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
1233 * mishandled before system is fully running: "modprobe $(cat modalias)"
1235 static ssize_t modalias_show(struct device *dev,
1236 struct device_attribute *attr, char *buf)
1238 struct platform_device *pdev = to_platform_device(dev);
1241 len = of_device_modalias(dev, buf, PAGE_SIZE);
1245 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
1249 return sysfs_emit(buf, "platform:%s\n", pdev->name);
1251 static DEVICE_ATTR_RO(modalias);
1253 static ssize_t numa_node_show(struct device *dev,
1254 struct device_attribute *attr, char *buf)
1256 return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1258 static DEVICE_ATTR_RO(numa_node);
1260 static ssize_t driver_override_show(struct device *dev,
1261 struct device_attribute *attr, char *buf)
1263 struct platform_device *pdev = to_platform_device(dev);
1267 len = sysfs_emit(buf, "%s\n", pdev->driver_override);
1273 static ssize_t driver_override_store(struct device *dev,
1274 struct device_attribute *attr,
1275 const char *buf, size_t count)
1277 struct platform_device *pdev = to_platform_device(dev);
1278 char *driver_override, *old, *cp;
1280 /* We need to keep extra room for a newline */
1281 if (count >= (PAGE_SIZE - 1))
1284 driver_override = kstrndup(buf, count, GFP_KERNEL);
1285 if (!driver_override)
1288 cp = strchr(driver_override, '\n');
1293 old = pdev->driver_override;
1294 if (strlen(driver_override)) {
1295 pdev->driver_override = driver_override;
1297 kfree(driver_override);
1298 pdev->driver_override = NULL;
1306 static DEVICE_ATTR_RW(driver_override);
1308 static struct attribute *platform_dev_attrs[] = {
1309 &dev_attr_modalias.attr,
1310 &dev_attr_numa_node.attr,
1311 &dev_attr_driver_override.attr,
1315 static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a,
1318 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1320 if (a == &dev_attr_numa_node.attr &&
1321 dev_to_node(dev) == NUMA_NO_NODE)
1327 static const struct attribute_group platform_dev_group = {
1328 .attrs = platform_dev_attrs,
1329 .is_visible = platform_dev_attrs_visible,
1331 __ATTRIBUTE_GROUPS(platform_dev);
1335 * platform_match - bind platform device to platform driver.
1339 * Platform device IDs are assumed to be encoded like this:
1340 * "<name><instance>", where <name> is a short description of the type of
1341 * device, like "pci" or "floppy", and <instance> is the enumerated
1342 * instance of the device, like '0' or '42'. Driver IDs are simply
1343 * "<name>". So, extract the <name> from the platform_device structure,
1344 * and compare it against the name of the driver. Return whether they match
1347 static int platform_match(struct device *dev, struct device_driver *drv)
1349 struct platform_device *pdev = to_platform_device(dev);
1350 struct platform_driver *pdrv = to_platform_driver(drv);
1352 /* When driver_override is set, only bind to the matching driver */
1353 if (pdev->driver_override)
1354 return !strcmp(pdev->driver_override, drv->name);
1356 /* Attempt an OF style match first */
1357 if (of_driver_match_device(dev, drv))
1360 /* Then try ACPI style match */
1361 if (acpi_driver_match_device(dev, drv))
1364 /* Then try to match against the id table */
1366 return platform_match_id(pdrv->id_table, pdev) != NULL;
1368 /* fall-back to driver name match */
1369 return (strcmp(pdev->name, drv->name) == 0);
1372 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1374 struct platform_device *pdev = to_platform_device(dev);
1377 /* Some devices have extra OF data and an OF-style MODALIAS */
1378 rc = of_device_uevent_modalias(dev, env);
1382 rc = acpi_device_uevent_modalias(dev, env);
1386 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1391 static int platform_probe(struct device *_dev)
1393 struct platform_driver *drv = to_platform_driver(_dev->driver);
1394 struct platform_device *dev = to_platform_device(_dev);
1398 * A driver registered using platform_driver_probe() cannot be bound
1399 * again later because the probe function usually lives in __init code
1400 * and so is gone. For these drivers .probe is set to
1401 * platform_probe_fail in __platform_driver_probe(). Don't even prepare
1402 * clocks and PM domains for these to match the traditional behaviour.
1404 if (unlikely(drv->probe == platform_probe_fail))
1407 ret = of_clk_set_defaults(_dev->of_node, false);
1411 ret = dev_pm_domain_attach(_dev, true);
1416 ret = drv->probe(dev);
1418 dev_pm_domain_detach(_dev, true);
1422 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
1423 dev_warn(_dev, "probe deferral not supported\n");
1430 static void platform_remove(struct device *_dev)
1432 struct platform_driver *drv = to_platform_driver(_dev->driver);
1433 struct platform_device *dev = to_platform_device(_dev);
1436 int ret = drv->remove(dev);
1439 dev_warn(_dev, "remove callback returned a non-zero value. This will be ignored.\n");
1441 dev_pm_domain_detach(_dev, true);
1444 static void platform_shutdown(struct device *_dev)
1446 struct platform_device *dev = to_platform_device(_dev);
1447 struct platform_driver *drv;
1452 drv = to_platform_driver(_dev->driver);
1458 int platform_dma_configure(struct device *dev)
1460 enum dev_dma_attr attr;
1464 ret = of_dma_configure(dev, dev->of_node, true);
1465 } else if (has_acpi_companion(dev)) {
1466 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1467 ret = acpi_dma_configure(dev, attr);
1473 static const struct dev_pm_ops platform_dev_pm_ops = {
1474 SET_RUNTIME_PM_OPS(pm_generic_runtime_suspend, pm_generic_runtime_resume, NULL)
1475 USE_PLATFORM_PM_SLEEP_OPS
1478 struct bus_type platform_bus_type = {
1480 .dev_groups = platform_dev_groups,
1481 .match = platform_match,
1482 .uevent = platform_uevent,
1483 .probe = platform_probe,
1484 .remove = platform_remove,
1485 .shutdown = platform_shutdown,
1486 .dma_configure = platform_dma_configure,
1487 .pm = &platform_dev_pm_ops,
1489 EXPORT_SYMBOL_GPL(platform_bus_type);
1491 static inline int __platform_match(struct device *dev, const void *drv)
1493 return platform_match(dev, (struct device_driver *)drv);
1497 * platform_find_device_by_driver - Find a platform device with a given
1499 * @start: The device to start the search from.
1500 * @drv: The device driver to look for.
1502 struct device *platform_find_device_by_driver(struct device *start,
1503 const struct device_driver *drv)
1505 return bus_find_device(&platform_bus_type, start, drv,
1508 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1510 void __weak __init early_platform_cleanup(void) { }
1512 int __init platform_bus_init(void)
1516 early_platform_cleanup();
1518 error = device_register(&platform_bus);
1520 put_device(&platform_bus);
1523 error = bus_register(&platform_bus_type);
1525 device_unregister(&platform_bus);
1526 of_platform_register_reconfig_notifier();