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 #ifdef CONFIG_HAS_IOMEM
68 * devm_platform_get_and_ioremap_resource - call devm_ioremap_resource() for a
69 * platform device and get resource
71 * @pdev: platform device to use both for memory resource lookup as well as
73 * @index: resource index
74 * @res: optional output parameter to store a pointer to the obtained resource.
76 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
80 devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
81 unsigned int index, struct resource **res)
85 r = platform_get_resource(pdev, IORESOURCE_MEM, index);
88 return devm_ioremap_resource(&pdev->dev, r);
90 EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource);
93 * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
96 * @pdev: platform device to use both for memory resource lookup as well as
98 * @index: resource index
100 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
103 void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
106 return devm_platform_get_and_ioremap_resource(pdev, index, NULL);
108 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
111 * devm_platform_ioremap_resource_wc - write-combined variant of
112 * devm_platform_ioremap_resource()
114 * @pdev: platform device to use both for memory resource lookup as well as
115 * resource management
116 * @index: resource index
118 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
121 void __iomem *devm_platform_ioremap_resource_wc(struct platform_device *pdev,
124 struct resource *res;
126 res = platform_get_resource(pdev, IORESOURCE_MEM, index);
127 return devm_ioremap_resource_wc(&pdev->dev, res);
131 * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for
132 * a platform device, retrieve the
135 * @pdev: platform device to use both for memory resource lookup as well as
136 * resource management
137 * @name: name of the resource
139 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
143 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
146 struct resource *res;
148 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
149 return devm_ioremap_resource(&pdev->dev, res);
151 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname);
152 #endif /* CONFIG_HAS_IOMEM */
155 * platform_get_irq_optional - get an optional IRQ for a device
156 * @dev: platform device
157 * @num: IRQ number index
159 * Gets an IRQ for a platform device. Device drivers should check the return
160 * value for errors so as to not pass a negative integer value to the
161 * request_irq() APIs. This is the same as platform_get_irq(), except that it
162 * does not print an error message if an IRQ can not be obtained.
166 * int irq = platform_get_irq_optional(pdev, 0);
170 * Return: non-zero IRQ number on success, negative error number on failure.
172 int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
176 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
177 if (!dev || num >= dev->archdata.num_irqs)
179 ret = dev->archdata.irqs[num];
184 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
185 ret = of_irq_get(dev->dev.of_node, num);
186 if (ret > 0 || ret == -EPROBE_DEFER)
190 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
191 if (has_acpi_companion(&dev->dev)) {
192 if (r && r->flags & IORESOURCE_DISABLED) {
193 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
200 * The resources may pass trigger flags to the irqs that need
201 * to be set up. It so happens that the trigger flags for
202 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
205 if (r && r->flags & IORESOURCE_BITS) {
206 struct irq_data *irqd;
208 irqd = irq_get_irq_data(r->start);
213 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
222 * For the index 0 interrupt, allow falling back to GpioInt
223 * resources. While a device could have both Interrupt and GpioInt
224 * resources, making this fallback ambiguous, in many common cases
225 * the device will only expose one IRQ, and this fallback
226 * allows a common code path across either kind of resource.
228 if (num == 0 && has_acpi_companion(&dev->dev)) {
229 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
230 /* Our callers expect -ENXIO for missing IRQs. */
231 if (ret >= 0 || ret == -EPROBE_DEFER)
238 WARN(ret == 0, "0 is an invalid IRQ number\n");
241 EXPORT_SYMBOL_GPL(platform_get_irq_optional);
244 * platform_get_irq - get an IRQ for a device
245 * @dev: platform device
246 * @num: IRQ number index
248 * Gets an IRQ for a platform device and prints an error message if finding the
249 * IRQ fails. Device drivers should check the return value for errors so as to
250 * not pass a negative integer value to the request_irq() APIs.
254 * int irq = platform_get_irq(pdev, 0);
258 * Return: non-zero IRQ number on success, negative error number on failure.
260 int platform_get_irq(struct platform_device *dev, unsigned int num)
264 ret = platform_get_irq_optional(dev, num);
265 if (ret < 0 && ret != -EPROBE_DEFER)
266 dev_err(&dev->dev, "IRQ index %u not found\n", num);
270 EXPORT_SYMBOL_GPL(platform_get_irq);
273 * platform_irq_count - Count the number of IRQs a platform device uses
274 * @dev: platform device
276 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
278 int platform_irq_count(struct platform_device *dev)
282 while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
285 if (ret == -EPROBE_DEFER)
290 EXPORT_SYMBOL_GPL(platform_irq_count);
293 * platform_get_resource_byname - get a resource for a device by name
294 * @dev: platform device
295 * @type: resource type
296 * @name: resource name
298 struct resource *platform_get_resource_byname(struct platform_device *dev,
304 for (i = 0; i < dev->num_resources; i++) {
305 struct resource *r = &dev->resource[i];
307 if (unlikely(!r->name))
310 if (type == resource_type(r) && !strcmp(r->name, name))
315 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
317 static int __platform_get_irq_byname(struct platform_device *dev,
323 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
324 ret = of_irq_get_byname(dev->dev.of_node, name);
325 if (ret > 0 || ret == -EPROBE_DEFER)
329 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
331 WARN(r->start == 0, "0 is an invalid IRQ number\n");
339 * platform_get_irq_byname - get an IRQ for a device by name
340 * @dev: platform device
343 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
345 * Return: non-zero IRQ number on success, negative error number on failure.
347 int platform_get_irq_byname(struct platform_device *dev, const char *name)
351 ret = __platform_get_irq_byname(dev, name);
352 if (ret < 0 && ret != -EPROBE_DEFER)
353 dev_err(&dev->dev, "IRQ %s not found\n", name);
357 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
360 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
361 * @dev: platform device
364 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
365 * does not print an error message if an IRQ can not be obtained.
367 * Return: non-zero IRQ number on success, negative error number on failure.
369 int platform_get_irq_byname_optional(struct platform_device *dev,
372 return __platform_get_irq_byname(dev, name);
374 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
377 * platform_add_devices - add a numbers of platform devices
378 * @devs: array of platform devices to add
379 * @num: number of platform devices in array
381 int platform_add_devices(struct platform_device **devs, int num)
385 for (i = 0; i < num; i++) {
386 ret = platform_device_register(devs[i]);
389 platform_device_unregister(devs[i]);
396 EXPORT_SYMBOL_GPL(platform_add_devices);
398 struct platform_object {
399 struct platform_device pdev;
404 * Set up default DMA mask for platform devices if the they weren't
405 * previously set by the architecture / DT.
407 static void setup_pdev_dma_masks(struct platform_device *pdev)
409 pdev->dev.dma_parms = &pdev->dma_parms;
411 if (!pdev->dev.coherent_dma_mask)
412 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
413 if (!pdev->dev.dma_mask) {
414 pdev->platform_dma_mask = DMA_BIT_MASK(32);
415 pdev->dev.dma_mask = &pdev->platform_dma_mask;
420 * platform_device_put - destroy a platform device
421 * @pdev: platform device to free
423 * Free all memory associated with a platform device. This function must
424 * _only_ be externally called in error cases. All other usage is a bug.
426 void platform_device_put(struct platform_device *pdev)
428 if (!IS_ERR_OR_NULL(pdev))
429 put_device(&pdev->dev);
431 EXPORT_SYMBOL_GPL(platform_device_put);
433 static void platform_device_release(struct device *dev)
435 struct platform_object *pa = container_of(dev, struct platform_object,
438 of_device_node_put(&pa->pdev.dev);
439 kfree(pa->pdev.dev.platform_data);
440 kfree(pa->pdev.mfd_cell);
441 kfree(pa->pdev.resource);
442 kfree(pa->pdev.driver_override);
447 * platform_device_alloc - create a platform device
448 * @name: base name of the device we're adding
451 * Create a platform device object which can have other objects attached
452 * to it, and which will have attached objects freed when it is released.
454 struct platform_device *platform_device_alloc(const char *name, int id)
456 struct platform_object *pa;
458 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
460 strcpy(pa->name, name);
461 pa->pdev.name = pa->name;
463 device_initialize(&pa->pdev.dev);
464 pa->pdev.dev.release = platform_device_release;
465 setup_pdev_dma_masks(&pa->pdev);
468 return pa ? &pa->pdev : NULL;
470 EXPORT_SYMBOL_GPL(platform_device_alloc);
473 * platform_device_add_resources - add resources to a platform device
474 * @pdev: platform device allocated by platform_device_alloc to add resources to
475 * @res: set of resources that needs to be allocated for the device
476 * @num: number of resources
478 * Add a copy of the resources to the platform device. The memory
479 * associated with the resources will be freed when the platform device is
482 int platform_device_add_resources(struct platform_device *pdev,
483 const struct resource *res, unsigned int num)
485 struct resource *r = NULL;
488 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
493 kfree(pdev->resource);
495 pdev->num_resources = num;
498 EXPORT_SYMBOL_GPL(platform_device_add_resources);
501 * platform_device_add_data - add platform-specific data to a platform device
502 * @pdev: platform device allocated by platform_device_alloc to add resources to
503 * @data: platform specific data for this platform device
504 * @size: size of platform specific data
506 * Add a copy of platform specific data to the platform device's
507 * platform_data pointer. The memory associated with the platform data
508 * will be freed when the platform device is released.
510 int platform_device_add_data(struct platform_device *pdev, const void *data,
516 d = kmemdup(data, size, GFP_KERNEL);
521 kfree(pdev->dev.platform_data);
522 pdev->dev.platform_data = d;
525 EXPORT_SYMBOL_GPL(platform_device_add_data);
528 * platform_device_add_properties - add built-in properties to a platform device
529 * @pdev: platform device to add properties to
530 * @properties: null terminated array of properties to add
532 * The function will take deep copy of @properties and attach the copy to the
533 * platform device. The memory associated with properties will be freed when the
534 * platform device is released.
536 int platform_device_add_properties(struct platform_device *pdev,
537 const struct property_entry *properties)
539 return device_add_properties(&pdev->dev, properties);
541 EXPORT_SYMBOL_GPL(platform_device_add_properties);
544 * platform_device_add - add a platform device to device hierarchy
545 * @pdev: platform device we're adding
547 * This is part 2 of platform_device_register(), though may be called
548 * separately _iff_ pdev was allocated by platform_device_alloc().
550 int platform_device_add(struct platform_device *pdev)
558 if (!pdev->dev.parent)
559 pdev->dev.parent = &platform_bus;
561 pdev->dev.bus = &platform_bus_type;
565 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
567 case PLATFORM_DEVID_NONE:
568 dev_set_name(&pdev->dev, "%s", pdev->name);
570 case PLATFORM_DEVID_AUTO:
572 * Automatically allocated device ID. We mark it as such so
573 * that we remember it must be freed, and we append a suffix
574 * to avoid namespace collision with explicit IDs.
576 ret = ida_alloc(&platform_devid_ida, GFP_KERNEL);
580 pdev->id_auto = true;
581 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
585 for (i = 0; i < pdev->num_resources; i++) {
586 struct resource *p, *r = &pdev->resource[i];
589 r->name = dev_name(&pdev->dev);
593 if (resource_type(r) == IORESOURCE_MEM)
595 else if (resource_type(r) == IORESOURCE_IO)
596 p = &ioport_resource;
600 ret = insert_resource(p, r);
602 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
608 pr_debug("Registering platform device '%s'. Parent at %s\n",
609 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
611 ret = device_add(&pdev->dev);
617 ida_free(&platform_devid_ida, pdev->id);
618 pdev->id = PLATFORM_DEVID_AUTO;
622 struct resource *r = &pdev->resource[i];
630 EXPORT_SYMBOL_GPL(platform_device_add);
633 * platform_device_del - remove a platform-level device
634 * @pdev: platform device we're removing
636 * Note that this function will also release all memory- and port-based
637 * resources owned by the device (@dev->resource). This function must
638 * _only_ be externally called in error cases. All other usage is a bug.
640 void platform_device_del(struct platform_device *pdev)
644 if (!IS_ERR_OR_NULL(pdev)) {
645 device_del(&pdev->dev);
648 ida_free(&platform_devid_ida, pdev->id);
649 pdev->id = PLATFORM_DEVID_AUTO;
652 for (i = 0; i < pdev->num_resources; i++) {
653 struct resource *r = &pdev->resource[i];
659 EXPORT_SYMBOL_GPL(platform_device_del);
662 * platform_device_register - add a platform-level device
663 * @pdev: platform device we're adding
665 int platform_device_register(struct platform_device *pdev)
667 device_initialize(&pdev->dev);
668 setup_pdev_dma_masks(pdev);
669 return platform_device_add(pdev);
671 EXPORT_SYMBOL_GPL(platform_device_register);
674 * platform_device_unregister - unregister a platform-level device
675 * @pdev: platform device we're unregistering
677 * Unregistration is done in 2 steps. First we release all resources
678 * and remove it from the subsystem, then we drop reference count by
679 * calling platform_device_put().
681 void platform_device_unregister(struct platform_device *pdev)
683 platform_device_del(pdev);
684 platform_device_put(pdev);
686 EXPORT_SYMBOL_GPL(platform_device_unregister);
689 * platform_device_register_full - add a platform-level device with
690 * resources and platform-specific data
692 * @pdevinfo: data used to create device
694 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
696 struct platform_device *platform_device_register_full(
697 const struct platform_device_info *pdevinfo)
700 struct platform_device *pdev;
702 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
704 return ERR_PTR(-ENOMEM);
706 pdev->dev.parent = pdevinfo->parent;
707 pdev->dev.fwnode = pdevinfo->fwnode;
708 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
709 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
711 if (pdevinfo->dma_mask) {
712 pdev->platform_dma_mask = pdevinfo->dma_mask;
713 pdev->dev.dma_mask = &pdev->platform_dma_mask;
714 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
717 ret = platform_device_add_resources(pdev,
718 pdevinfo->res, pdevinfo->num_res);
722 ret = platform_device_add_data(pdev,
723 pdevinfo->data, pdevinfo->size_data);
727 if (pdevinfo->properties) {
728 ret = platform_device_add_properties(pdev,
729 pdevinfo->properties);
734 ret = platform_device_add(pdev);
737 ACPI_COMPANION_SET(&pdev->dev, NULL);
738 platform_device_put(pdev);
744 EXPORT_SYMBOL_GPL(platform_device_register_full);
746 static int platform_drv_probe(struct device *_dev)
748 struct platform_driver *drv = to_platform_driver(_dev->driver);
749 struct platform_device *dev = to_platform_device(_dev);
752 ret = of_clk_set_defaults(_dev->of_node, false);
756 ret = dev_pm_domain_attach(_dev, true);
761 ret = drv->probe(dev);
763 dev_pm_domain_detach(_dev, true);
767 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
768 dev_warn(_dev, "probe deferral not supported\n");
775 static int platform_drv_probe_fail(struct device *_dev)
780 static int platform_drv_remove(struct device *_dev)
782 struct platform_driver *drv = to_platform_driver(_dev->driver);
783 struct platform_device *dev = to_platform_device(_dev);
787 ret = drv->remove(dev);
788 dev_pm_domain_detach(_dev, true);
793 static void platform_drv_shutdown(struct device *_dev)
795 struct platform_driver *drv = to_platform_driver(_dev->driver);
796 struct platform_device *dev = to_platform_device(_dev);
803 * __platform_driver_register - register a driver for platform-level devices
804 * @drv: platform driver structure
805 * @owner: owning module/driver
807 int __platform_driver_register(struct platform_driver *drv,
808 struct module *owner)
810 drv->driver.owner = owner;
811 drv->driver.bus = &platform_bus_type;
812 drv->driver.probe = platform_drv_probe;
813 drv->driver.remove = platform_drv_remove;
814 drv->driver.shutdown = platform_drv_shutdown;
816 return driver_register(&drv->driver);
818 EXPORT_SYMBOL_GPL(__platform_driver_register);
821 * platform_driver_unregister - unregister a driver for platform-level devices
822 * @drv: platform driver structure
824 void platform_driver_unregister(struct platform_driver *drv)
826 driver_unregister(&drv->driver);
828 EXPORT_SYMBOL_GPL(platform_driver_unregister);
831 * __platform_driver_probe - register driver for non-hotpluggable device
832 * @drv: platform driver structure
833 * @probe: the driver probe routine, probably from an __init section
834 * @module: module which will be the owner of the driver
836 * Use this instead of platform_driver_register() when you know the device
837 * is not hotpluggable and has already been registered, and you want to
838 * remove its run-once probe() infrastructure from memory after the driver
839 * has bound to the device.
841 * One typical use for this would be with drivers for controllers integrated
842 * into system-on-chip processors, where the controller devices have been
843 * configured as part of board setup.
845 * Note that this is incompatible with deferred probing.
847 * Returns zero if the driver registered and bound to a device, else returns
848 * a negative error code and with the driver not registered.
850 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
851 int (*probe)(struct platform_device *), struct module *module)
855 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
856 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
857 drv->driver.name, __func__);
862 * We have to run our probes synchronously because we check if
863 * we find any devices to bind to and exit with error if there
866 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
869 * Prevent driver from requesting probe deferral to avoid further
870 * futile probe attempts.
872 drv->prevent_deferred_probe = true;
874 /* make sure driver won't have bind/unbind attributes */
875 drv->driver.suppress_bind_attrs = true;
877 /* temporary section violation during probe() */
879 retval = code = __platform_driver_register(drv, module);
884 * Fixup that section violation, being paranoid about code scanning
885 * the list of drivers in order to probe new devices. Check to see
886 * if the probe was successful, and make sure any forced probes of
889 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
891 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
893 drv->driver.probe = platform_drv_probe_fail;
894 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
897 platform_driver_unregister(drv);
900 EXPORT_SYMBOL_GPL(__platform_driver_probe);
903 * __platform_create_bundle - register driver and create corresponding device
904 * @driver: platform driver structure
905 * @probe: the driver probe routine, probably from an __init section
906 * @res: set of resources that needs to be allocated for the device
907 * @n_res: number of resources
908 * @data: platform specific data for this platform device
909 * @size: size of platform specific data
910 * @module: module which will be the owner of the driver
912 * Use this in legacy-style modules that probe hardware directly and
913 * register a single platform device and corresponding platform driver.
915 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
917 struct platform_device * __init_or_module __platform_create_bundle(
918 struct platform_driver *driver,
919 int (*probe)(struct platform_device *),
920 struct resource *res, unsigned int n_res,
921 const void *data, size_t size, struct module *module)
923 struct platform_device *pdev;
926 pdev = platform_device_alloc(driver->driver.name, -1);
932 error = platform_device_add_resources(pdev, res, n_res);
936 error = platform_device_add_data(pdev, data, size);
940 error = platform_device_add(pdev);
944 error = __platform_driver_probe(driver, probe, module);
951 platform_device_del(pdev);
953 platform_device_put(pdev);
955 return ERR_PTR(error);
957 EXPORT_SYMBOL_GPL(__platform_create_bundle);
960 * __platform_register_drivers - register an array of platform drivers
961 * @drivers: an array of drivers to register
962 * @count: the number of drivers to register
963 * @owner: module owning the drivers
965 * Registers platform drivers specified by an array. On failure to register a
966 * driver, all previously registered drivers will be unregistered. Callers of
967 * this API should use platform_unregister_drivers() to unregister drivers in
970 * Returns: 0 on success or a negative error code on failure.
972 int __platform_register_drivers(struct platform_driver * const *drivers,
973 unsigned int count, struct module *owner)
978 for (i = 0; i < count; i++) {
979 pr_debug("registering platform driver %ps\n", drivers[i]);
981 err = __platform_driver_register(drivers[i], owner);
983 pr_err("failed to register platform driver %ps: %d\n",
993 pr_debug("unregistering platform driver %ps\n", drivers[i]);
994 platform_driver_unregister(drivers[i]);
999 EXPORT_SYMBOL_GPL(__platform_register_drivers);
1002 * platform_unregister_drivers - unregister an array of platform drivers
1003 * @drivers: an array of drivers to unregister
1004 * @count: the number of drivers to unregister
1006 * Unregisters platform drivers specified by an array. This is typically used
1007 * to complement an earlier call to platform_register_drivers(). Drivers are
1008 * unregistered in the reverse order in which they were registered.
1010 void platform_unregister_drivers(struct platform_driver * const *drivers,
1014 pr_debug("unregistering platform driver %ps\n", drivers[count]);
1015 platform_driver_unregister(drivers[count]);
1018 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
1020 /* modalias support enables more hands-off userspace setup:
1021 * (a) environment variable lets new-style hotplug events work once system is
1022 * fully running: "modprobe $MODALIAS"
1023 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
1024 * mishandled before system is fully running: "modprobe $(cat modalias)"
1026 static ssize_t modalias_show(struct device *dev,
1027 struct device_attribute *attr, char *buf)
1029 struct platform_device *pdev = to_platform_device(dev);
1032 len = of_device_modalias(dev, buf, PAGE_SIZE);
1036 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
1040 return sysfs_emit(buf, "platform:%s\n", pdev->name);
1042 static DEVICE_ATTR_RO(modalias);
1044 static ssize_t driver_override_store(struct device *dev,
1045 struct device_attribute *attr,
1046 const char *buf, size_t count)
1048 struct platform_device *pdev = to_platform_device(dev);
1049 char *driver_override, *old, *cp;
1051 /* We need to keep extra room for a newline */
1052 if (count >= (PAGE_SIZE - 1))
1055 driver_override = kstrndup(buf, count, GFP_KERNEL);
1056 if (!driver_override)
1059 cp = strchr(driver_override, '\n');
1064 old = pdev->driver_override;
1065 if (strlen(driver_override)) {
1066 pdev->driver_override = driver_override;
1068 kfree(driver_override);
1069 pdev->driver_override = NULL;
1078 static ssize_t driver_override_show(struct device *dev,
1079 struct device_attribute *attr, char *buf)
1081 struct platform_device *pdev = to_platform_device(dev);
1085 len = sysfs_emit(buf, "%s\n", pdev->driver_override);
1090 static DEVICE_ATTR_RW(driver_override);
1092 static ssize_t numa_node_show(struct device *dev,
1093 struct device_attribute *attr, char *buf)
1095 return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1097 static DEVICE_ATTR_RO(numa_node);
1099 static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a,
1102 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1104 if (a == &dev_attr_numa_node.attr &&
1105 dev_to_node(dev) == NUMA_NO_NODE)
1111 static struct attribute *platform_dev_attrs[] = {
1112 &dev_attr_modalias.attr,
1113 &dev_attr_numa_node.attr,
1114 &dev_attr_driver_override.attr,
1118 static struct attribute_group platform_dev_group = {
1119 .attrs = platform_dev_attrs,
1120 .is_visible = platform_dev_attrs_visible,
1122 __ATTRIBUTE_GROUPS(platform_dev);
1124 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1126 struct platform_device *pdev = to_platform_device(dev);
1129 /* Some devices have extra OF data and an OF-style MODALIAS */
1130 rc = of_device_uevent_modalias(dev, env);
1134 rc = acpi_device_uevent_modalias(dev, env);
1138 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1143 static const struct platform_device_id *platform_match_id(
1144 const struct platform_device_id *id,
1145 struct platform_device *pdev)
1147 while (id->name[0]) {
1148 if (strcmp(pdev->name, id->name) == 0) {
1149 pdev->id_entry = id;
1158 * platform_match - bind platform device to platform driver.
1162 * Platform device IDs are assumed to be encoded like this:
1163 * "<name><instance>", where <name> is a short description of the type of
1164 * device, like "pci" or "floppy", and <instance> is the enumerated
1165 * instance of the device, like '0' or '42'. Driver IDs are simply
1166 * "<name>". So, extract the <name> from the platform_device structure,
1167 * and compare it against the name of the driver. Return whether they match
1170 static int platform_match(struct device *dev, struct device_driver *drv)
1172 struct platform_device *pdev = to_platform_device(dev);
1173 struct platform_driver *pdrv = to_platform_driver(drv);
1175 /* When driver_override is set, only bind to the matching driver */
1176 if (pdev->driver_override)
1177 return !strcmp(pdev->driver_override, drv->name);
1179 /* Attempt an OF style match first */
1180 if (of_driver_match_device(dev, drv))
1183 /* Then try ACPI style match */
1184 if (acpi_driver_match_device(dev, drv))
1187 /* Then try to match against the id table */
1189 return platform_match_id(pdrv->id_table, pdev) != NULL;
1191 /* fall-back to driver name match */
1192 return (strcmp(pdev->name, drv->name) == 0);
1195 #ifdef CONFIG_PM_SLEEP
1197 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1199 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1200 struct platform_device *pdev = to_platform_device(dev);
1203 if (dev->driver && pdrv->suspend)
1204 ret = pdrv->suspend(pdev, mesg);
1209 static int platform_legacy_resume(struct device *dev)
1211 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1212 struct platform_device *pdev = to_platform_device(dev);
1215 if (dev->driver && pdrv->resume)
1216 ret = pdrv->resume(pdev);
1221 #endif /* CONFIG_PM_SLEEP */
1223 #ifdef CONFIG_SUSPEND
1225 int platform_pm_suspend(struct device *dev)
1227 struct device_driver *drv = dev->driver;
1234 if (drv->pm->suspend)
1235 ret = drv->pm->suspend(dev);
1237 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1243 int platform_pm_resume(struct device *dev)
1245 struct device_driver *drv = dev->driver;
1252 if (drv->pm->resume)
1253 ret = drv->pm->resume(dev);
1255 ret = platform_legacy_resume(dev);
1261 #endif /* CONFIG_SUSPEND */
1263 #ifdef CONFIG_HIBERNATE_CALLBACKS
1265 int platform_pm_freeze(struct device *dev)
1267 struct device_driver *drv = dev->driver;
1274 if (drv->pm->freeze)
1275 ret = drv->pm->freeze(dev);
1277 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1283 int platform_pm_thaw(struct device *dev)
1285 struct device_driver *drv = dev->driver;
1293 ret = drv->pm->thaw(dev);
1295 ret = platform_legacy_resume(dev);
1301 int platform_pm_poweroff(struct device *dev)
1303 struct device_driver *drv = dev->driver;
1310 if (drv->pm->poweroff)
1311 ret = drv->pm->poweroff(dev);
1313 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1319 int platform_pm_restore(struct device *dev)
1321 struct device_driver *drv = dev->driver;
1328 if (drv->pm->restore)
1329 ret = drv->pm->restore(dev);
1331 ret = platform_legacy_resume(dev);
1337 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1339 int platform_dma_configure(struct device *dev)
1341 enum dev_dma_attr attr;
1345 ret = of_dma_configure(dev, dev->of_node, true);
1346 } else if (has_acpi_companion(dev)) {
1347 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1348 ret = acpi_dma_configure(dev, attr);
1354 static const struct dev_pm_ops platform_dev_pm_ops = {
1355 .runtime_suspend = pm_generic_runtime_suspend,
1356 .runtime_resume = pm_generic_runtime_resume,
1357 USE_PLATFORM_PM_SLEEP_OPS
1360 struct bus_type platform_bus_type = {
1362 .dev_groups = platform_dev_groups,
1363 .match = platform_match,
1364 .uevent = platform_uevent,
1365 .dma_configure = platform_dma_configure,
1366 .pm = &platform_dev_pm_ops,
1368 EXPORT_SYMBOL_GPL(platform_bus_type);
1370 static inline int __platform_match(struct device *dev, const void *drv)
1372 return platform_match(dev, (struct device_driver *)drv);
1376 * platform_find_device_by_driver - Find a platform device with a given
1378 * @start: The device to start the search from.
1379 * @drv: The device driver to look for.
1381 struct device *platform_find_device_by_driver(struct device *start,
1382 const struct device_driver *drv)
1384 return bus_find_device(&platform_bus_type, start, drv,
1387 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1389 void __weak __init early_platform_cleanup(void) { }
1391 int __init platform_bus_init(void)
1395 early_platform_cleanup();
1397 error = device_register(&platform_bus);
1399 put_device(&platform_bus);
1402 error = bus_register(&platform_bus_type);
1404 device_unregister(&platform_bus);
1405 of_platform_register_reconfig_notifier();