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>
30 #include <linux/types.h>
33 #include "power/power.h"
35 /* For automatically allocated device IDs */
36 static DEFINE_IDA(platform_devid_ida);
38 struct device platform_bus = {
39 .init_name = "platform",
41 EXPORT_SYMBOL_GPL(platform_bus);
44 * platform_get_resource - get a resource for a device
45 * @dev: platform device
46 * @type: resource type
47 * @num: resource index
49 * Return: a pointer to the resource or NULL on failure.
51 struct resource *platform_get_resource(struct platform_device *dev,
52 unsigned int type, unsigned int num)
56 for (i = 0; i < dev->num_resources; i++) {
57 struct resource *r = &dev->resource[i];
59 if (type == resource_type(r) && num-- == 0)
64 EXPORT_SYMBOL_GPL(platform_get_resource);
66 struct resource *platform_get_mem_or_io(struct platform_device *dev,
71 for (i = 0; i < dev->num_resources; i++) {
72 struct resource *r = &dev->resource[i];
74 if ((resource_type(r) & (IORESOURCE_MEM|IORESOURCE_IO)) && num-- == 0)
79 EXPORT_SYMBOL_GPL(platform_get_mem_or_io);
81 #ifdef CONFIG_HAS_IOMEM
83 * devm_platform_get_and_ioremap_resource - call devm_ioremap_resource() for a
84 * platform device and get resource
86 * @pdev: platform device to use both for memory resource lookup as well as
88 * @index: resource index
89 * @res: optional output parameter to store a pointer to the obtained resource.
91 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
95 devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
96 unsigned int index, struct resource **res)
100 r = platform_get_resource(pdev, IORESOURCE_MEM, index);
103 return devm_ioremap_resource(&pdev->dev, r);
105 EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource);
108 * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
111 * @pdev: platform device to use both for memory resource lookup as well as
112 * resource management
113 * @index: resource index
115 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
118 void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
121 return devm_platform_get_and_ioremap_resource(pdev, index, NULL);
123 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
126 * devm_platform_ioremap_resource_wc - write-combined variant of
127 * devm_platform_ioremap_resource()
129 * @pdev: platform device to use both for memory resource lookup as well as
130 * resource management
131 * @index: resource index
133 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
136 void __iomem *devm_platform_ioremap_resource_wc(struct platform_device *pdev,
139 struct resource *res;
141 res = platform_get_resource(pdev, IORESOURCE_MEM, index);
142 return devm_ioremap_resource_wc(&pdev->dev, res);
146 * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for
147 * a platform device, retrieve the
150 * @pdev: platform device to use both for memory resource lookup as well as
151 * resource management
152 * @name: name of the resource
154 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
158 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
161 struct resource *res;
163 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
164 return devm_ioremap_resource(&pdev->dev, res);
166 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname);
167 #endif /* CONFIG_HAS_IOMEM */
170 * platform_get_irq_optional - get an optional IRQ for a device
171 * @dev: platform device
172 * @num: IRQ number index
174 * Gets an IRQ for a platform device. Device drivers should check the return
175 * value for errors so as to not pass a negative integer value to the
176 * request_irq() APIs. This is the same as platform_get_irq(), except that it
177 * does not print an error message if an IRQ can not be obtained.
181 * int irq = platform_get_irq_optional(pdev, 0);
185 * Return: non-zero IRQ number on success, negative error number on failure.
187 int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
191 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
192 if (!dev || num >= dev->archdata.num_irqs)
194 ret = dev->archdata.irqs[num];
199 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
200 ret = of_irq_get(dev->dev.of_node, num);
201 if (ret > 0 || ret == -EPROBE_DEFER)
205 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
206 if (has_acpi_companion(&dev->dev)) {
207 if (r && r->flags & IORESOURCE_DISABLED) {
208 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
215 * The resources may pass trigger flags to the irqs that need
216 * to be set up. It so happens that the trigger flags for
217 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
220 if (r && r->flags & IORESOURCE_BITS) {
221 struct irq_data *irqd;
223 irqd = irq_get_irq_data(r->start);
228 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
237 * For the index 0 interrupt, allow falling back to GpioInt
238 * resources. While a device could have both Interrupt and GpioInt
239 * resources, making this fallback ambiguous, in many common cases
240 * the device will only expose one IRQ, and this fallback
241 * allows a common code path across either kind of resource.
243 if (num == 0 && has_acpi_companion(&dev->dev)) {
244 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
245 /* Our callers expect -ENXIO for missing IRQs. */
246 if (ret >= 0 || ret == -EPROBE_DEFER)
253 WARN(ret == 0, "0 is an invalid IRQ number\n");
256 EXPORT_SYMBOL_GPL(platform_get_irq_optional);
259 * platform_get_irq - get an IRQ for a device
260 * @dev: platform device
261 * @num: IRQ number index
263 * Gets an IRQ for a platform device and prints an error message if finding the
264 * IRQ fails. Device drivers should check the return value for errors so as to
265 * not pass a negative integer value to the request_irq() APIs.
269 * int irq = platform_get_irq(pdev, 0);
273 * Return: non-zero IRQ number on success, negative error number on failure.
275 int platform_get_irq(struct platform_device *dev, unsigned int num)
279 ret = platform_get_irq_optional(dev, num);
280 if (ret < 0 && ret != -EPROBE_DEFER)
281 dev_err(&dev->dev, "IRQ index %u not found\n", num);
285 EXPORT_SYMBOL_GPL(platform_get_irq);
288 * platform_irq_count - Count the number of IRQs a platform device uses
289 * @dev: platform device
291 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
293 int platform_irq_count(struct platform_device *dev)
297 while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
300 if (ret == -EPROBE_DEFER)
305 EXPORT_SYMBOL_GPL(platform_irq_count);
308 * platform_get_resource_byname - get a resource for a device by name
309 * @dev: platform device
310 * @type: resource type
311 * @name: resource name
313 struct resource *platform_get_resource_byname(struct platform_device *dev,
319 for (i = 0; i < dev->num_resources; i++) {
320 struct resource *r = &dev->resource[i];
322 if (unlikely(!r->name))
325 if (type == resource_type(r) && !strcmp(r->name, name))
330 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
332 static int __platform_get_irq_byname(struct platform_device *dev,
338 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
339 ret = of_irq_get_byname(dev->dev.of_node, name);
340 if (ret > 0 || ret == -EPROBE_DEFER)
344 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
346 WARN(r->start == 0, "0 is an invalid IRQ number\n");
354 * platform_get_irq_byname - get an IRQ for a device by name
355 * @dev: platform device
358 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
360 * Return: non-zero IRQ number on success, negative error number on failure.
362 int platform_get_irq_byname(struct platform_device *dev, const char *name)
366 ret = __platform_get_irq_byname(dev, name);
367 if (ret < 0 && ret != -EPROBE_DEFER)
368 dev_err(&dev->dev, "IRQ %s not found\n", name);
372 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
375 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
376 * @dev: platform device
379 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
380 * does not print an error message if an IRQ can not be obtained.
382 * Return: non-zero IRQ number on success, negative error number on failure.
384 int platform_get_irq_byname_optional(struct platform_device *dev,
387 return __platform_get_irq_byname(dev, name);
389 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
392 * platform_add_devices - add a numbers of platform devices
393 * @devs: array of platform devices to add
394 * @num: number of platform devices in array
396 int platform_add_devices(struct platform_device **devs, int num)
400 for (i = 0; i < num; i++) {
401 ret = platform_device_register(devs[i]);
404 platform_device_unregister(devs[i]);
411 EXPORT_SYMBOL_GPL(platform_add_devices);
413 struct platform_object {
414 struct platform_device pdev;
419 * Set up default DMA mask for platform devices if the they weren't
420 * previously set by the architecture / DT.
422 static void setup_pdev_dma_masks(struct platform_device *pdev)
424 pdev->dev.dma_parms = &pdev->dma_parms;
426 if (!pdev->dev.coherent_dma_mask)
427 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
428 if (!pdev->dev.dma_mask) {
429 pdev->platform_dma_mask = DMA_BIT_MASK(32);
430 pdev->dev.dma_mask = &pdev->platform_dma_mask;
435 * platform_device_put - destroy a platform device
436 * @pdev: platform device to free
438 * Free all memory associated with a platform device. This function must
439 * _only_ be externally called in error cases. All other usage is a bug.
441 void platform_device_put(struct platform_device *pdev)
443 if (!IS_ERR_OR_NULL(pdev))
444 put_device(&pdev->dev);
446 EXPORT_SYMBOL_GPL(platform_device_put);
448 static void platform_device_release(struct device *dev)
450 struct platform_object *pa = container_of(dev, struct platform_object,
453 of_device_node_put(&pa->pdev.dev);
454 kfree(pa->pdev.dev.platform_data);
455 kfree(pa->pdev.mfd_cell);
456 kfree(pa->pdev.resource);
457 kfree(pa->pdev.driver_override);
462 * platform_device_alloc - create a platform device
463 * @name: base name of the device we're adding
466 * Create a platform device object which can have other objects attached
467 * to it, and which will have attached objects freed when it is released.
469 struct platform_device *platform_device_alloc(const char *name, int id)
471 struct platform_object *pa;
473 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
475 strcpy(pa->name, name);
476 pa->pdev.name = pa->name;
478 device_initialize(&pa->pdev.dev);
479 pa->pdev.dev.release = platform_device_release;
480 setup_pdev_dma_masks(&pa->pdev);
483 return pa ? &pa->pdev : NULL;
485 EXPORT_SYMBOL_GPL(platform_device_alloc);
488 * platform_device_add_resources - add resources to a platform device
489 * @pdev: platform device allocated by platform_device_alloc to add resources to
490 * @res: set of resources that needs to be allocated for the device
491 * @num: number of resources
493 * Add a copy of the resources to the platform device. The memory
494 * associated with the resources will be freed when the platform device is
497 int platform_device_add_resources(struct platform_device *pdev,
498 const struct resource *res, unsigned int num)
500 struct resource *r = NULL;
503 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
508 kfree(pdev->resource);
510 pdev->num_resources = num;
513 EXPORT_SYMBOL_GPL(platform_device_add_resources);
516 * platform_device_add_data - add platform-specific data to a platform device
517 * @pdev: platform device allocated by platform_device_alloc to add resources to
518 * @data: platform specific data for this platform device
519 * @size: size of platform specific data
521 * Add a copy of platform specific data to the platform device's
522 * platform_data pointer. The memory associated with the platform data
523 * will be freed when the platform device is released.
525 int platform_device_add_data(struct platform_device *pdev, const void *data,
531 d = kmemdup(data, size, GFP_KERNEL);
536 kfree(pdev->dev.platform_data);
537 pdev->dev.platform_data = d;
540 EXPORT_SYMBOL_GPL(platform_device_add_data);
543 * platform_device_add_properties - add built-in properties to a platform device
544 * @pdev: platform device to add properties to
545 * @properties: null terminated array of properties to add
547 * The function will take deep copy of @properties and attach the copy to the
548 * platform device. The memory associated with properties will be freed when the
549 * platform device is released.
551 int platform_device_add_properties(struct platform_device *pdev,
552 const struct property_entry *properties)
554 return device_add_properties(&pdev->dev, properties);
556 EXPORT_SYMBOL_GPL(platform_device_add_properties);
559 * platform_device_add - add a platform device to device hierarchy
560 * @pdev: platform device we're adding
562 * This is part 2 of platform_device_register(), though may be called
563 * separately _iff_ pdev was allocated by platform_device_alloc().
565 int platform_device_add(struct platform_device *pdev)
573 if (!pdev->dev.parent)
574 pdev->dev.parent = &platform_bus;
576 pdev->dev.bus = &platform_bus_type;
580 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
582 case PLATFORM_DEVID_NONE:
583 dev_set_name(&pdev->dev, "%s", pdev->name);
585 case PLATFORM_DEVID_AUTO:
587 * Automatically allocated device ID. We mark it as such so
588 * that we remember it must be freed, and we append a suffix
589 * to avoid namespace collision with explicit IDs.
591 ret = ida_alloc(&platform_devid_ida, GFP_KERNEL);
595 pdev->id_auto = true;
596 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
600 for (i = 0; i < pdev->num_resources; i++) {
601 struct resource *p, *r = &pdev->resource[i];
604 r->name = dev_name(&pdev->dev);
608 if (resource_type(r) == IORESOURCE_MEM)
610 else if (resource_type(r) == IORESOURCE_IO)
611 p = &ioport_resource;
615 ret = insert_resource(p, r);
617 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
623 pr_debug("Registering platform device '%s'. Parent at %s\n",
624 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
626 ret = device_add(&pdev->dev);
632 ida_free(&platform_devid_ida, pdev->id);
633 pdev->id = PLATFORM_DEVID_AUTO;
637 struct resource *r = &pdev->resource[i];
645 EXPORT_SYMBOL_GPL(platform_device_add);
648 * platform_device_del - remove a platform-level device
649 * @pdev: platform device we're removing
651 * Note that this function will also release all memory- and port-based
652 * resources owned by the device (@dev->resource). This function must
653 * _only_ be externally called in error cases. All other usage is a bug.
655 void platform_device_del(struct platform_device *pdev)
659 if (!IS_ERR_OR_NULL(pdev)) {
660 device_del(&pdev->dev);
663 ida_free(&platform_devid_ida, pdev->id);
664 pdev->id = PLATFORM_DEVID_AUTO;
667 for (i = 0; i < pdev->num_resources; i++) {
668 struct resource *r = &pdev->resource[i];
674 EXPORT_SYMBOL_GPL(platform_device_del);
677 * platform_device_register - add a platform-level device
678 * @pdev: platform device we're adding
680 int platform_device_register(struct platform_device *pdev)
682 device_initialize(&pdev->dev);
683 setup_pdev_dma_masks(pdev);
684 return platform_device_add(pdev);
686 EXPORT_SYMBOL_GPL(platform_device_register);
689 * platform_device_unregister - unregister a platform-level device
690 * @pdev: platform device we're unregistering
692 * Unregistration is done in 2 steps. First we release all resources
693 * and remove it from the subsystem, then we drop reference count by
694 * calling platform_device_put().
696 void platform_device_unregister(struct platform_device *pdev)
698 platform_device_del(pdev);
699 platform_device_put(pdev);
701 EXPORT_SYMBOL_GPL(platform_device_unregister);
704 * platform_device_register_full - add a platform-level device with
705 * resources and platform-specific data
707 * @pdevinfo: data used to create device
709 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
711 struct platform_device *platform_device_register_full(
712 const struct platform_device_info *pdevinfo)
715 struct platform_device *pdev;
717 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
719 return ERR_PTR(-ENOMEM);
721 pdev->dev.parent = pdevinfo->parent;
722 pdev->dev.fwnode = pdevinfo->fwnode;
723 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
724 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
726 if (pdevinfo->dma_mask) {
727 pdev->platform_dma_mask = pdevinfo->dma_mask;
728 pdev->dev.dma_mask = &pdev->platform_dma_mask;
729 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
732 ret = platform_device_add_resources(pdev,
733 pdevinfo->res, pdevinfo->num_res);
737 ret = platform_device_add_data(pdev,
738 pdevinfo->data, pdevinfo->size_data);
742 if (pdevinfo->properties) {
743 ret = platform_device_add_properties(pdev,
744 pdevinfo->properties);
749 ret = platform_device_add(pdev);
752 ACPI_COMPANION_SET(&pdev->dev, NULL);
753 platform_device_put(pdev);
759 EXPORT_SYMBOL_GPL(platform_device_register_full);
762 * __platform_driver_register - register a driver for platform-level devices
763 * @drv: platform driver structure
764 * @owner: owning module/driver
766 int __platform_driver_register(struct platform_driver *drv,
767 struct module *owner)
769 drv->driver.owner = owner;
770 drv->driver.bus = &platform_bus_type;
772 return driver_register(&drv->driver);
774 EXPORT_SYMBOL_GPL(__platform_driver_register);
777 * platform_driver_unregister - unregister a driver for platform-level devices
778 * @drv: platform driver structure
780 void platform_driver_unregister(struct platform_driver *drv)
782 driver_unregister(&drv->driver);
784 EXPORT_SYMBOL_GPL(platform_driver_unregister);
786 static int platform_probe_fail(struct platform_device *pdev)
792 * __platform_driver_probe - register driver for non-hotpluggable device
793 * @drv: platform driver structure
794 * @probe: the driver probe routine, probably from an __init section
795 * @module: module which will be the owner of the driver
797 * Use this instead of platform_driver_register() when you know the device
798 * is not hotpluggable and has already been registered, and you want to
799 * remove its run-once probe() infrastructure from memory after the driver
800 * has bound to the device.
802 * One typical use for this would be with drivers for controllers integrated
803 * into system-on-chip processors, where the controller devices have been
804 * configured as part of board setup.
806 * Note that this is incompatible with deferred probing.
808 * Returns zero if the driver registered and bound to a device, else returns
809 * a negative error code and with the driver not registered.
811 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
812 int (*probe)(struct platform_device *), struct module *module)
816 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
817 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
818 drv->driver.name, __func__);
823 * We have to run our probes synchronously because we check if
824 * we find any devices to bind to and exit with error if there
827 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
830 * Prevent driver from requesting probe deferral to avoid further
831 * futile probe attempts.
833 drv->prevent_deferred_probe = true;
835 /* make sure driver won't have bind/unbind attributes */
836 drv->driver.suppress_bind_attrs = true;
838 /* temporary section violation during probe() */
840 retval = code = __platform_driver_register(drv, module);
845 * Fixup that section violation, being paranoid about code scanning
846 * the list of drivers in order to probe new devices. Check to see
847 * if the probe was successful, and make sure any forced probes of
850 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
851 drv->probe = platform_probe_fail;
852 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
854 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
857 platform_driver_unregister(drv);
860 EXPORT_SYMBOL_GPL(__platform_driver_probe);
863 * __platform_create_bundle - register driver and create corresponding device
864 * @driver: platform driver structure
865 * @probe: the driver probe routine, probably from an __init section
866 * @res: set of resources that needs to be allocated for the device
867 * @n_res: number of resources
868 * @data: platform specific data for this platform device
869 * @size: size of platform specific data
870 * @module: module which will be the owner of the driver
872 * Use this in legacy-style modules that probe hardware directly and
873 * register a single platform device and corresponding platform driver.
875 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
877 struct platform_device * __init_or_module __platform_create_bundle(
878 struct platform_driver *driver,
879 int (*probe)(struct platform_device *),
880 struct resource *res, unsigned int n_res,
881 const void *data, size_t size, struct module *module)
883 struct platform_device *pdev;
886 pdev = platform_device_alloc(driver->driver.name, -1);
892 error = platform_device_add_resources(pdev, res, n_res);
896 error = platform_device_add_data(pdev, data, size);
900 error = platform_device_add(pdev);
904 error = __platform_driver_probe(driver, probe, module);
911 platform_device_del(pdev);
913 platform_device_put(pdev);
915 return ERR_PTR(error);
917 EXPORT_SYMBOL_GPL(__platform_create_bundle);
920 * __platform_register_drivers - register an array of platform drivers
921 * @drivers: an array of drivers to register
922 * @count: the number of drivers to register
923 * @owner: module owning the drivers
925 * Registers platform drivers specified by an array. On failure to register a
926 * driver, all previously registered drivers will be unregistered. Callers of
927 * this API should use platform_unregister_drivers() to unregister drivers in
930 * Returns: 0 on success or a negative error code on failure.
932 int __platform_register_drivers(struct platform_driver * const *drivers,
933 unsigned int count, struct module *owner)
938 for (i = 0; i < count; i++) {
939 pr_debug("registering platform driver %ps\n", drivers[i]);
941 err = __platform_driver_register(drivers[i], owner);
943 pr_err("failed to register platform driver %ps: %d\n",
953 pr_debug("unregistering platform driver %ps\n", drivers[i]);
954 platform_driver_unregister(drivers[i]);
959 EXPORT_SYMBOL_GPL(__platform_register_drivers);
962 * platform_unregister_drivers - unregister an array of platform drivers
963 * @drivers: an array of drivers to unregister
964 * @count: the number of drivers to unregister
966 * Unregisters platform drivers specified by an array. This is typically used
967 * to complement an earlier call to platform_register_drivers(). Drivers are
968 * unregistered in the reverse order in which they were registered.
970 void platform_unregister_drivers(struct platform_driver * const *drivers,
974 pr_debug("unregistering platform driver %ps\n", drivers[count]);
975 platform_driver_unregister(drivers[count]);
978 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
980 static const struct platform_device_id *platform_match_id(
981 const struct platform_device_id *id,
982 struct platform_device *pdev)
984 while (id->name[0]) {
985 if (strcmp(pdev->name, id->name) == 0) {
994 #ifdef CONFIG_PM_SLEEP
996 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
998 struct platform_driver *pdrv = to_platform_driver(dev->driver);
999 struct platform_device *pdev = to_platform_device(dev);
1002 if (dev->driver && pdrv->suspend)
1003 ret = pdrv->suspend(pdev, mesg);
1008 static int platform_legacy_resume(struct device *dev)
1010 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1011 struct platform_device *pdev = to_platform_device(dev);
1014 if (dev->driver && pdrv->resume)
1015 ret = pdrv->resume(pdev);
1020 #endif /* CONFIG_PM_SLEEP */
1022 #ifdef CONFIG_SUSPEND
1024 int platform_pm_suspend(struct device *dev)
1026 struct device_driver *drv = dev->driver;
1033 if (drv->pm->suspend)
1034 ret = drv->pm->suspend(dev);
1036 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1042 int platform_pm_resume(struct device *dev)
1044 struct device_driver *drv = dev->driver;
1051 if (drv->pm->resume)
1052 ret = drv->pm->resume(dev);
1054 ret = platform_legacy_resume(dev);
1060 #endif /* CONFIG_SUSPEND */
1062 #ifdef CONFIG_HIBERNATE_CALLBACKS
1064 int platform_pm_freeze(struct device *dev)
1066 struct device_driver *drv = dev->driver;
1073 if (drv->pm->freeze)
1074 ret = drv->pm->freeze(dev);
1076 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1082 int platform_pm_thaw(struct device *dev)
1084 struct device_driver *drv = dev->driver;
1092 ret = drv->pm->thaw(dev);
1094 ret = platform_legacy_resume(dev);
1100 int platform_pm_poweroff(struct device *dev)
1102 struct device_driver *drv = dev->driver;
1109 if (drv->pm->poweroff)
1110 ret = drv->pm->poweroff(dev);
1112 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1118 int platform_pm_restore(struct device *dev)
1120 struct device_driver *drv = dev->driver;
1127 if (drv->pm->restore)
1128 ret = drv->pm->restore(dev);
1130 ret = platform_legacy_resume(dev);
1136 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1138 /* modalias support enables more hands-off userspace setup:
1139 * (a) environment variable lets new-style hotplug events work once system is
1140 * fully running: "modprobe $MODALIAS"
1141 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
1142 * mishandled before system is fully running: "modprobe $(cat modalias)"
1144 static ssize_t modalias_show(struct device *dev,
1145 struct device_attribute *attr, char *buf)
1147 struct platform_device *pdev = to_platform_device(dev);
1150 len = of_device_modalias(dev, buf, PAGE_SIZE);
1154 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
1158 return sysfs_emit(buf, "platform:%s\n", pdev->name);
1160 static DEVICE_ATTR_RO(modalias);
1162 static ssize_t numa_node_show(struct device *dev,
1163 struct device_attribute *attr, char *buf)
1165 return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1167 static DEVICE_ATTR_RO(numa_node);
1169 static ssize_t driver_override_show(struct device *dev,
1170 struct device_attribute *attr, char *buf)
1172 struct platform_device *pdev = to_platform_device(dev);
1176 len = sysfs_emit(buf, "%s\n", pdev->driver_override);
1182 static ssize_t driver_override_store(struct device *dev,
1183 struct device_attribute *attr,
1184 const char *buf, size_t count)
1186 struct platform_device *pdev = to_platform_device(dev);
1187 char *driver_override, *old, *cp;
1189 /* We need to keep extra room for a newline */
1190 if (count >= (PAGE_SIZE - 1))
1193 driver_override = kstrndup(buf, count, GFP_KERNEL);
1194 if (!driver_override)
1197 cp = strchr(driver_override, '\n');
1202 old = pdev->driver_override;
1203 if (strlen(driver_override)) {
1204 pdev->driver_override = driver_override;
1206 kfree(driver_override);
1207 pdev->driver_override = NULL;
1215 static DEVICE_ATTR_RW(driver_override);
1217 static struct attribute *platform_dev_attrs[] = {
1218 &dev_attr_modalias.attr,
1219 &dev_attr_numa_node.attr,
1220 &dev_attr_driver_override.attr,
1224 static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a,
1227 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1229 if (a == &dev_attr_numa_node.attr &&
1230 dev_to_node(dev) == NUMA_NO_NODE)
1236 static struct attribute_group platform_dev_group = {
1237 .attrs = platform_dev_attrs,
1238 .is_visible = platform_dev_attrs_visible,
1240 __ATTRIBUTE_GROUPS(platform_dev);
1244 * platform_match - bind platform device to platform driver.
1248 * Platform device IDs are assumed to be encoded like this:
1249 * "<name><instance>", where <name> is a short description of the type of
1250 * device, like "pci" or "floppy", and <instance> is the enumerated
1251 * instance of the device, like '0' or '42'. Driver IDs are simply
1252 * "<name>". So, extract the <name> from the platform_device structure,
1253 * and compare it against the name of the driver. Return whether they match
1256 static int platform_match(struct device *dev, struct device_driver *drv)
1258 struct platform_device *pdev = to_platform_device(dev);
1259 struct platform_driver *pdrv = to_platform_driver(drv);
1261 /* When driver_override is set, only bind to the matching driver */
1262 if (pdev->driver_override)
1263 return !strcmp(pdev->driver_override, drv->name);
1265 /* Attempt an OF style match first */
1266 if (of_driver_match_device(dev, drv))
1269 /* Then try ACPI style match */
1270 if (acpi_driver_match_device(dev, drv))
1273 /* Then try to match against the id table */
1275 return platform_match_id(pdrv->id_table, pdev) != NULL;
1277 /* fall-back to driver name match */
1278 return (strcmp(pdev->name, drv->name) == 0);
1281 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1283 struct platform_device *pdev = to_platform_device(dev);
1286 /* Some devices have extra OF data and an OF-style MODALIAS */
1287 rc = of_device_uevent_modalias(dev, env);
1291 rc = acpi_device_uevent_modalias(dev, env);
1295 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1300 static int platform_probe(struct device *_dev)
1302 struct platform_driver *drv = to_platform_driver(_dev->driver);
1303 struct platform_device *dev = to_platform_device(_dev);
1307 * A driver registered using platform_driver_probe() cannot be bound
1308 * again later because the probe function usually lives in __init code
1309 * and so is gone. For these drivers .probe is set to
1310 * platform_probe_fail in __platform_driver_probe(). Don't even prepare
1311 * clocks and PM domains for these to match the traditional behaviour.
1313 if (unlikely(drv->probe == platform_probe_fail))
1316 ret = of_clk_set_defaults(_dev->of_node, false);
1320 ret = dev_pm_domain_attach(_dev, true);
1325 ret = drv->probe(dev);
1327 dev_pm_domain_detach(_dev, true);
1331 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
1332 dev_warn(_dev, "probe deferral not supported\n");
1339 static int platform_remove(struct device *_dev)
1341 struct platform_driver *drv = to_platform_driver(_dev->driver);
1342 struct platform_device *dev = to_platform_device(_dev);
1346 ret = drv->remove(dev);
1347 dev_pm_domain_detach(_dev, true);
1352 static void platform_shutdown(struct device *_dev)
1354 struct platform_device *dev = to_platform_device(_dev);
1355 struct platform_driver *drv;
1360 drv = to_platform_driver(_dev->driver);
1366 int platform_dma_configure(struct device *dev)
1368 enum dev_dma_attr attr;
1372 ret = of_dma_configure(dev, dev->of_node, true);
1373 } else if (has_acpi_companion(dev)) {
1374 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1375 ret = acpi_dma_configure(dev, attr);
1381 static const struct dev_pm_ops platform_dev_pm_ops = {
1382 .runtime_suspend = pm_generic_runtime_suspend,
1383 .runtime_resume = pm_generic_runtime_resume,
1384 USE_PLATFORM_PM_SLEEP_OPS
1387 struct bus_type platform_bus_type = {
1389 .dev_groups = platform_dev_groups,
1390 .match = platform_match,
1391 .uevent = platform_uevent,
1392 .probe = platform_probe,
1393 .remove = platform_remove,
1394 .shutdown = platform_shutdown,
1395 .dma_configure = platform_dma_configure,
1396 .pm = &platform_dev_pm_ops,
1398 EXPORT_SYMBOL_GPL(platform_bus_type);
1400 static inline int __platform_match(struct device *dev, const void *drv)
1402 return platform_match(dev, (struct device_driver *)drv);
1406 * platform_find_device_by_driver - Find a platform device with a given
1408 * @start: The device to start the search from.
1409 * @drv: The device driver to look for.
1411 struct device *platform_find_device_by_driver(struct device *start,
1412 const struct device_driver *drv)
1414 return bus_find_device(&platform_bus_type, start, drv,
1417 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1419 void __weak __init early_platform_cleanup(void) { }
1421 int __init platform_bus_init(void)
1425 early_platform_cleanup();
1427 error = device_register(&platform_bus);
1429 put_device(&platform_bus);
1432 error = bus_register(&platform_bus_type);
1434 device_unregister(&platform_bus);
1435 of_platform_register_reconfig_notifier();