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_wc - write-combined variant of
129 * devm_platform_ioremap_resource()
131 * @pdev: platform device to use both for memory resource lookup as well as
132 * resource management
133 * @index: resource index
135 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
138 void __iomem *devm_platform_ioremap_resource_wc(struct platform_device *pdev,
141 struct resource *res;
143 res = platform_get_resource(pdev, IORESOURCE_MEM, index);
144 return devm_ioremap_resource_wc(&pdev->dev, res);
148 * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for
149 * a platform device, retrieve the
152 * @pdev: platform device to use both for memory resource lookup as well as
153 * resource management
154 * @name: name of the resource
156 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
160 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
163 struct resource *res;
165 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
166 return devm_ioremap_resource(&pdev->dev, res);
168 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname);
169 #endif /* CONFIG_HAS_IOMEM */
172 * platform_get_irq_optional - get an optional IRQ for a device
173 * @dev: platform device
174 * @num: IRQ number index
176 * Gets an IRQ for a platform device. Device drivers should check the return
177 * value for errors so as to not pass a negative integer value to the
178 * request_irq() APIs. This is the same as platform_get_irq(), except that it
179 * does not print an error message if an IRQ can not be obtained.
183 * int irq = platform_get_irq_optional(pdev, 0);
187 * Return: non-zero IRQ number on success, negative error number on failure.
189 int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
193 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
194 if (!dev || num >= dev->archdata.num_irqs)
196 ret = dev->archdata.irqs[num];
201 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
202 ret = of_irq_get(dev->dev.of_node, num);
203 if (ret > 0 || ret == -EPROBE_DEFER)
207 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
208 if (has_acpi_companion(&dev->dev)) {
209 if (r && r->flags & IORESOURCE_DISABLED) {
210 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
217 * The resources may pass trigger flags to the irqs that need
218 * to be set up. It so happens that the trigger flags for
219 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
222 if (r && r->flags & IORESOURCE_BITS) {
223 struct irq_data *irqd;
225 irqd = irq_get_irq_data(r->start);
230 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
239 * For the index 0 interrupt, allow falling back to GpioInt
240 * resources. While a device could have both Interrupt and GpioInt
241 * resources, making this fallback ambiguous, in many common cases
242 * the device will only expose one IRQ, and this fallback
243 * allows a common code path across either kind of resource.
245 if (num == 0 && has_acpi_companion(&dev->dev)) {
246 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
247 /* Our callers expect -ENXIO for missing IRQs. */
248 if (ret >= 0 || ret == -EPROBE_DEFER)
255 WARN(ret == 0, "0 is an invalid IRQ number\n");
258 EXPORT_SYMBOL_GPL(platform_get_irq_optional);
261 * platform_get_irq - get an IRQ for a device
262 * @dev: platform device
263 * @num: IRQ number index
265 * Gets an IRQ for a platform device and prints an error message if finding the
266 * IRQ fails. Device drivers should check the return value for errors so as to
267 * not pass a negative integer value to the request_irq() APIs.
271 * int irq = platform_get_irq(pdev, 0);
275 * Return: non-zero IRQ number on success, negative error number on failure.
277 int platform_get_irq(struct platform_device *dev, unsigned int num)
281 ret = platform_get_irq_optional(dev, num);
282 if (ret < 0 && ret != -EPROBE_DEFER)
283 dev_err(&dev->dev, "IRQ index %u not found\n", num);
287 EXPORT_SYMBOL_GPL(platform_get_irq);
290 * platform_irq_count - Count the number of IRQs a platform device uses
291 * @dev: platform device
293 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
295 int platform_irq_count(struct platform_device *dev)
299 while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
302 if (ret == -EPROBE_DEFER)
307 EXPORT_SYMBOL_GPL(platform_irq_count);
309 struct irq_affinity_devres {
314 static void platform_disable_acpi_irq(struct platform_device *pdev, int index)
318 r = platform_get_resource(pdev, IORESOURCE_IRQ, index);
320 irqresource_disabled(r, 0);
323 static void devm_platform_get_irqs_affinity_release(struct device *dev,
326 struct irq_affinity_devres *ptr = res;
329 for (i = 0; i < ptr->count; i++) {
330 irq_dispose_mapping(ptr->irq[i]);
332 if (has_acpi_companion(dev))
333 platform_disable_acpi_irq(to_platform_device(dev), i);
338 * devm_platform_get_irqs_affinity - devm method to get a set of IRQs for a
339 * device using an interrupt affinity descriptor
340 * @dev: platform device pointer
341 * @affd: affinity descriptor
342 * @minvec: minimum count of interrupt vectors
343 * @maxvec: maximum count of interrupt vectors
344 * @irqs: pointer holder for IRQ numbers
346 * Gets a set of IRQs for a platform device, and updates IRQ afffinty according
347 * to the passed affinity descriptor
349 * Return: Number of vectors on success, negative error number on failure.
351 int devm_platform_get_irqs_affinity(struct platform_device *dev,
352 struct irq_affinity *affd,
357 struct irq_affinity_devres *ptr;
358 struct irq_affinity_desc *desc;
368 nvec = platform_irq_count(dev);
375 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
382 size = sizeof(*ptr) + sizeof(unsigned int) * nvec;
383 ptr = devres_alloc(devm_platform_get_irqs_affinity_release, size,
390 for (i = 0; i < nvec; i++) {
391 int irq = platform_get_irq(dev, i);
394 goto err_free_devres;
399 desc = irq_create_affinity_masks(nvec, affd);
402 goto err_free_devres;
405 for (i = 0; i < nvec; i++) {
406 ret = irq_update_affinity_desc(ptr->irq[i], &desc[i]);
408 dev_err(&dev->dev, "failed to update irq%d affinity descriptor (%d)\n",
414 devres_add(&dev->dev, ptr);
428 EXPORT_SYMBOL_GPL(devm_platform_get_irqs_affinity);
431 * platform_get_resource_byname - get a resource for a device by name
432 * @dev: platform device
433 * @type: resource type
434 * @name: resource name
436 struct resource *platform_get_resource_byname(struct platform_device *dev,
442 for (i = 0; i < dev->num_resources; i++) {
443 struct resource *r = &dev->resource[i];
445 if (unlikely(!r->name))
448 if (type == resource_type(r) && !strcmp(r->name, name))
453 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
455 static int __platform_get_irq_byname(struct platform_device *dev,
461 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
462 ret = of_irq_get_byname(dev->dev.of_node, name);
463 if (ret > 0 || ret == -EPROBE_DEFER)
467 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
469 WARN(r->start == 0, "0 is an invalid IRQ number\n");
477 * platform_get_irq_byname - get an IRQ for a device by name
478 * @dev: platform device
481 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
483 * Return: non-zero IRQ number on success, negative error number on failure.
485 int platform_get_irq_byname(struct platform_device *dev, const char *name)
489 ret = __platform_get_irq_byname(dev, name);
490 if (ret < 0 && ret != -EPROBE_DEFER)
491 dev_err(&dev->dev, "IRQ %s not found\n", name);
495 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
498 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
499 * @dev: platform device
502 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
503 * does not print an error message if an IRQ can not be obtained.
505 * Return: non-zero IRQ number on success, negative error number on failure.
507 int platform_get_irq_byname_optional(struct platform_device *dev,
510 return __platform_get_irq_byname(dev, name);
512 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
515 * platform_add_devices - add a numbers of platform devices
516 * @devs: array of platform devices to add
517 * @num: number of platform devices in array
519 int platform_add_devices(struct platform_device **devs, int num)
523 for (i = 0; i < num; i++) {
524 ret = platform_device_register(devs[i]);
527 platform_device_unregister(devs[i]);
534 EXPORT_SYMBOL_GPL(platform_add_devices);
536 struct platform_object {
537 struct platform_device pdev;
542 * Set up default DMA mask for platform devices if the they weren't
543 * previously set by the architecture / DT.
545 static void setup_pdev_dma_masks(struct platform_device *pdev)
547 pdev->dev.dma_parms = &pdev->dma_parms;
549 if (!pdev->dev.coherent_dma_mask)
550 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
551 if (!pdev->dev.dma_mask) {
552 pdev->platform_dma_mask = DMA_BIT_MASK(32);
553 pdev->dev.dma_mask = &pdev->platform_dma_mask;
558 * platform_device_put - destroy a platform device
559 * @pdev: platform device to free
561 * Free all memory associated with a platform device. This function must
562 * _only_ be externally called in error cases. All other usage is a bug.
564 void platform_device_put(struct platform_device *pdev)
566 if (!IS_ERR_OR_NULL(pdev))
567 put_device(&pdev->dev);
569 EXPORT_SYMBOL_GPL(platform_device_put);
571 static void platform_device_release(struct device *dev)
573 struct platform_object *pa = container_of(dev, struct platform_object,
576 of_node_put(pa->pdev.dev.of_node);
577 kfree(pa->pdev.dev.platform_data);
578 kfree(pa->pdev.mfd_cell);
579 kfree(pa->pdev.resource);
580 kfree(pa->pdev.driver_override);
585 * platform_device_alloc - create a platform device
586 * @name: base name of the device we're adding
589 * Create a platform device object which can have other objects attached
590 * to it, and which will have attached objects freed when it is released.
592 struct platform_device *platform_device_alloc(const char *name, int id)
594 struct platform_object *pa;
596 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
598 strcpy(pa->name, name);
599 pa->pdev.name = pa->name;
601 device_initialize(&pa->pdev.dev);
602 pa->pdev.dev.release = platform_device_release;
603 setup_pdev_dma_masks(&pa->pdev);
606 return pa ? &pa->pdev : NULL;
608 EXPORT_SYMBOL_GPL(platform_device_alloc);
611 * platform_device_add_resources - add resources to a platform device
612 * @pdev: platform device allocated by platform_device_alloc to add resources to
613 * @res: set of resources that needs to be allocated for the device
614 * @num: number of resources
616 * Add a copy of the resources to the platform device. The memory
617 * associated with the resources will be freed when the platform device is
620 int platform_device_add_resources(struct platform_device *pdev,
621 const struct resource *res, unsigned int num)
623 struct resource *r = NULL;
626 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
631 kfree(pdev->resource);
633 pdev->num_resources = num;
636 EXPORT_SYMBOL_GPL(platform_device_add_resources);
639 * platform_device_add_data - add platform-specific data to a platform device
640 * @pdev: platform device allocated by platform_device_alloc to add resources to
641 * @data: platform specific data for this platform device
642 * @size: size of platform specific data
644 * Add a copy of platform specific data to the platform device's
645 * platform_data pointer. The memory associated with the platform data
646 * will be freed when the platform device is released.
648 int platform_device_add_data(struct platform_device *pdev, const void *data,
654 d = kmemdup(data, size, GFP_KERNEL);
659 kfree(pdev->dev.platform_data);
660 pdev->dev.platform_data = d;
663 EXPORT_SYMBOL_GPL(platform_device_add_data);
666 * platform_device_add_properties - add built-in properties to a platform device
667 * @pdev: platform device to add properties to
668 * @properties: null terminated array of properties to add
670 * The function will take deep copy of @properties and attach the copy to the
671 * platform device. The memory associated with properties will be freed when the
672 * platform device is released.
674 int platform_device_add_properties(struct platform_device *pdev,
675 const struct property_entry *properties)
677 return device_add_properties(&pdev->dev, properties);
679 EXPORT_SYMBOL_GPL(platform_device_add_properties);
682 * platform_device_add - add a platform device to device hierarchy
683 * @pdev: platform device we're adding
685 * This is part 2 of platform_device_register(), though may be called
686 * separately _iff_ pdev was allocated by platform_device_alloc().
688 int platform_device_add(struct platform_device *pdev)
696 if (!pdev->dev.parent)
697 pdev->dev.parent = &platform_bus;
699 pdev->dev.bus = &platform_bus_type;
703 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
705 case PLATFORM_DEVID_NONE:
706 dev_set_name(&pdev->dev, "%s", pdev->name);
708 case PLATFORM_DEVID_AUTO:
710 * Automatically allocated device ID. We mark it as such so
711 * that we remember it must be freed, and we append a suffix
712 * to avoid namespace collision with explicit IDs.
714 ret = ida_alloc(&platform_devid_ida, GFP_KERNEL);
718 pdev->id_auto = true;
719 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
723 for (i = 0; i < pdev->num_resources; i++) {
724 struct resource *p, *r = &pdev->resource[i];
727 r->name = dev_name(&pdev->dev);
731 if (resource_type(r) == IORESOURCE_MEM)
733 else if (resource_type(r) == IORESOURCE_IO)
734 p = &ioport_resource;
738 ret = insert_resource(p, r);
740 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
746 pr_debug("Registering platform device '%s'. Parent at %s\n",
747 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
749 ret = device_add(&pdev->dev);
755 ida_free(&platform_devid_ida, pdev->id);
756 pdev->id = PLATFORM_DEVID_AUTO;
760 struct resource *r = &pdev->resource[i];
768 EXPORT_SYMBOL_GPL(platform_device_add);
771 * platform_device_del - remove a platform-level device
772 * @pdev: platform device we're removing
774 * Note that this function will also release all memory- and port-based
775 * resources owned by the device (@dev->resource). This function must
776 * _only_ be externally called in error cases. All other usage is a bug.
778 void platform_device_del(struct platform_device *pdev)
782 if (!IS_ERR_OR_NULL(pdev)) {
783 device_del(&pdev->dev);
786 ida_free(&platform_devid_ida, pdev->id);
787 pdev->id = PLATFORM_DEVID_AUTO;
790 for (i = 0; i < pdev->num_resources; i++) {
791 struct resource *r = &pdev->resource[i];
797 EXPORT_SYMBOL_GPL(platform_device_del);
800 * platform_device_register - add a platform-level device
801 * @pdev: platform device we're adding
803 int platform_device_register(struct platform_device *pdev)
805 device_initialize(&pdev->dev);
806 setup_pdev_dma_masks(pdev);
807 return platform_device_add(pdev);
809 EXPORT_SYMBOL_GPL(platform_device_register);
812 * platform_device_unregister - unregister a platform-level device
813 * @pdev: platform device we're unregistering
815 * Unregistration is done in 2 steps. First we release all resources
816 * and remove it from the subsystem, then we drop reference count by
817 * calling platform_device_put().
819 void platform_device_unregister(struct platform_device *pdev)
821 platform_device_del(pdev);
822 platform_device_put(pdev);
824 EXPORT_SYMBOL_GPL(platform_device_unregister);
827 * platform_device_register_full - add a platform-level device with
828 * resources and platform-specific data
830 * @pdevinfo: data used to create device
832 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
834 struct platform_device *platform_device_register_full(
835 const struct platform_device_info *pdevinfo)
838 struct platform_device *pdev;
840 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
842 return ERR_PTR(-ENOMEM);
844 pdev->dev.parent = pdevinfo->parent;
845 pdev->dev.fwnode = pdevinfo->fwnode;
846 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
847 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
849 if (pdevinfo->dma_mask) {
850 pdev->platform_dma_mask = pdevinfo->dma_mask;
851 pdev->dev.dma_mask = &pdev->platform_dma_mask;
852 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
855 ret = platform_device_add_resources(pdev,
856 pdevinfo->res, pdevinfo->num_res);
860 ret = platform_device_add_data(pdev,
861 pdevinfo->data, pdevinfo->size_data);
865 if (pdevinfo->properties) {
866 ret = platform_device_add_properties(pdev,
867 pdevinfo->properties);
872 ret = platform_device_add(pdev);
875 ACPI_COMPANION_SET(&pdev->dev, NULL);
876 platform_device_put(pdev);
882 EXPORT_SYMBOL_GPL(platform_device_register_full);
885 * __platform_driver_register - register a driver for platform-level devices
886 * @drv: platform driver structure
887 * @owner: owning module/driver
889 int __platform_driver_register(struct platform_driver *drv,
890 struct module *owner)
892 drv->driver.owner = owner;
893 drv->driver.bus = &platform_bus_type;
895 return driver_register(&drv->driver);
897 EXPORT_SYMBOL_GPL(__platform_driver_register);
900 * platform_driver_unregister - unregister a driver for platform-level devices
901 * @drv: platform driver structure
903 void platform_driver_unregister(struct platform_driver *drv)
905 driver_unregister(&drv->driver);
907 EXPORT_SYMBOL_GPL(platform_driver_unregister);
909 static int platform_probe_fail(struct platform_device *pdev)
915 * __platform_driver_probe - register driver for non-hotpluggable device
916 * @drv: platform driver structure
917 * @probe: the driver probe routine, probably from an __init section
918 * @module: module which will be the owner of the driver
920 * Use this instead of platform_driver_register() when you know the device
921 * is not hotpluggable and has already been registered, and you want to
922 * remove its run-once probe() infrastructure from memory after the driver
923 * has bound to the device.
925 * One typical use for this would be with drivers for controllers integrated
926 * into system-on-chip processors, where the controller devices have been
927 * configured as part of board setup.
929 * Note that this is incompatible with deferred probing.
931 * Returns zero if the driver registered and bound to a device, else returns
932 * a negative error code and with the driver not registered.
934 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
935 int (*probe)(struct platform_device *), struct module *module)
939 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
940 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
941 drv->driver.name, __func__);
946 * We have to run our probes synchronously because we check if
947 * we find any devices to bind to and exit with error if there
950 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
953 * Prevent driver from requesting probe deferral to avoid further
954 * futile probe attempts.
956 drv->prevent_deferred_probe = true;
958 /* make sure driver won't have bind/unbind attributes */
959 drv->driver.suppress_bind_attrs = true;
961 /* temporary section violation during probe() */
963 retval = code = __platform_driver_register(drv, module);
968 * Fixup that section violation, being paranoid about code scanning
969 * the list of drivers in order to probe new devices. Check to see
970 * if the probe was successful, and make sure any forced probes of
973 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
974 drv->probe = platform_probe_fail;
975 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
977 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
980 platform_driver_unregister(drv);
983 EXPORT_SYMBOL_GPL(__platform_driver_probe);
986 * __platform_create_bundle - register driver and create corresponding device
987 * @driver: platform driver structure
988 * @probe: the driver probe routine, probably from an __init section
989 * @res: set of resources that needs to be allocated for the device
990 * @n_res: number of resources
991 * @data: platform specific data for this platform device
992 * @size: size of platform specific data
993 * @module: module which will be the owner of the driver
995 * Use this in legacy-style modules that probe hardware directly and
996 * register a single platform device and corresponding platform driver.
998 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
1000 struct platform_device * __init_or_module __platform_create_bundle(
1001 struct platform_driver *driver,
1002 int (*probe)(struct platform_device *),
1003 struct resource *res, unsigned int n_res,
1004 const void *data, size_t size, struct module *module)
1006 struct platform_device *pdev;
1009 pdev = platform_device_alloc(driver->driver.name, -1);
1015 error = platform_device_add_resources(pdev, res, n_res);
1019 error = platform_device_add_data(pdev, data, size);
1023 error = platform_device_add(pdev);
1027 error = __platform_driver_probe(driver, probe, module);
1034 platform_device_del(pdev);
1036 platform_device_put(pdev);
1038 return ERR_PTR(error);
1040 EXPORT_SYMBOL_GPL(__platform_create_bundle);
1043 * __platform_register_drivers - register an array of platform drivers
1044 * @drivers: an array of drivers to register
1045 * @count: the number of drivers to register
1046 * @owner: module owning the drivers
1048 * Registers platform drivers specified by an array. On failure to register a
1049 * driver, all previously registered drivers will be unregistered. Callers of
1050 * this API should use platform_unregister_drivers() to unregister drivers in
1051 * the reverse order.
1053 * Returns: 0 on success or a negative error code on failure.
1055 int __platform_register_drivers(struct platform_driver * const *drivers,
1056 unsigned int count, struct module *owner)
1061 for (i = 0; i < count; i++) {
1062 pr_debug("registering platform driver %ps\n", drivers[i]);
1064 err = __platform_driver_register(drivers[i], owner);
1066 pr_err("failed to register platform driver %ps: %d\n",
1076 pr_debug("unregistering platform driver %ps\n", drivers[i]);
1077 platform_driver_unregister(drivers[i]);
1082 EXPORT_SYMBOL_GPL(__platform_register_drivers);
1085 * platform_unregister_drivers - unregister an array of platform drivers
1086 * @drivers: an array of drivers to unregister
1087 * @count: the number of drivers to unregister
1089 * Unregisters platform drivers specified by an array. This is typically used
1090 * to complement an earlier call to platform_register_drivers(). Drivers are
1091 * unregistered in the reverse order in which they were registered.
1093 void platform_unregister_drivers(struct platform_driver * const *drivers,
1097 pr_debug("unregistering platform driver %ps\n", drivers[count]);
1098 platform_driver_unregister(drivers[count]);
1101 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
1103 static const struct platform_device_id *platform_match_id(
1104 const struct platform_device_id *id,
1105 struct platform_device *pdev)
1107 while (id->name[0]) {
1108 if (strcmp(pdev->name, id->name) == 0) {
1109 pdev->id_entry = id;
1117 #ifdef CONFIG_PM_SLEEP
1119 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1121 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1122 struct platform_device *pdev = to_platform_device(dev);
1125 if (dev->driver && pdrv->suspend)
1126 ret = pdrv->suspend(pdev, mesg);
1131 static int platform_legacy_resume(struct device *dev)
1133 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1134 struct platform_device *pdev = to_platform_device(dev);
1137 if (dev->driver && pdrv->resume)
1138 ret = pdrv->resume(pdev);
1143 #endif /* CONFIG_PM_SLEEP */
1145 #ifdef CONFIG_SUSPEND
1147 int platform_pm_suspend(struct device *dev)
1149 struct device_driver *drv = dev->driver;
1156 if (drv->pm->suspend)
1157 ret = drv->pm->suspend(dev);
1159 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1165 int platform_pm_resume(struct device *dev)
1167 struct device_driver *drv = dev->driver;
1174 if (drv->pm->resume)
1175 ret = drv->pm->resume(dev);
1177 ret = platform_legacy_resume(dev);
1183 #endif /* CONFIG_SUSPEND */
1185 #ifdef CONFIG_HIBERNATE_CALLBACKS
1187 int platform_pm_freeze(struct device *dev)
1189 struct device_driver *drv = dev->driver;
1196 if (drv->pm->freeze)
1197 ret = drv->pm->freeze(dev);
1199 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1205 int platform_pm_thaw(struct device *dev)
1207 struct device_driver *drv = dev->driver;
1215 ret = drv->pm->thaw(dev);
1217 ret = platform_legacy_resume(dev);
1223 int platform_pm_poweroff(struct device *dev)
1225 struct device_driver *drv = dev->driver;
1232 if (drv->pm->poweroff)
1233 ret = drv->pm->poweroff(dev);
1235 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1241 int platform_pm_restore(struct device *dev)
1243 struct device_driver *drv = dev->driver;
1250 if (drv->pm->restore)
1251 ret = drv->pm->restore(dev);
1253 ret = platform_legacy_resume(dev);
1259 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1261 /* modalias support enables more hands-off userspace setup:
1262 * (a) environment variable lets new-style hotplug events work once system is
1263 * fully running: "modprobe $MODALIAS"
1264 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
1265 * mishandled before system is fully running: "modprobe $(cat modalias)"
1267 static ssize_t modalias_show(struct device *dev,
1268 struct device_attribute *attr, char *buf)
1270 struct platform_device *pdev = to_platform_device(dev);
1273 len = of_device_modalias(dev, buf, PAGE_SIZE);
1277 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
1281 return sysfs_emit(buf, "platform:%s\n", pdev->name);
1283 static DEVICE_ATTR_RO(modalias);
1285 static ssize_t numa_node_show(struct device *dev,
1286 struct device_attribute *attr, char *buf)
1288 return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1290 static DEVICE_ATTR_RO(numa_node);
1292 static ssize_t driver_override_show(struct device *dev,
1293 struct device_attribute *attr, char *buf)
1295 struct platform_device *pdev = to_platform_device(dev);
1299 len = sysfs_emit(buf, "%s\n", pdev->driver_override);
1305 static ssize_t driver_override_store(struct device *dev,
1306 struct device_attribute *attr,
1307 const char *buf, size_t count)
1309 struct platform_device *pdev = to_platform_device(dev);
1310 char *driver_override, *old, *cp;
1312 /* We need to keep extra room for a newline */
1313 if (count >= (PAGE_SIZE - 1))
1316 driver_override = kstrndup(buf, count, GFP_KERNEL);
1317 if (!driver_override)
1320 cp = strchr(driver_override, '\n');
1325 old = pdev->driver_override;
1326 if (strlen(driver_override)) {
1327 pdev->driver_override = driver_override;
1329 kfree(driver_override);
1330 pdev->driver_override = NULL;
1338 static DEVICE_ATTR_RW(driver_override);
1340 static struct attribute *platform_dev_attrs[] = {
1341 &dev_attr_modalias.attr,
1342 &dev_attr_numa_node.attr,
1343 &dev_attr_driver_override.attr,
1347 static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a,
1350 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1352 if (a == &dev_attr_numa_node.attr &&
1353 dev_to_node(dev) == NUMA_NO_NODE)
1359 static struct attribute_group platform_dev_group = {
1360 .attrs = platform_dev_attrs,
1361 .is_visible = platform_dev_attrs_visible,
1363 __ATTRIBUTE_GROUPS(platform_dev);
1367 * platform_match - bind platform device to platform driver.
1371 * Platform device IDs are assumed to be encoded like this:
1372 * "<name><instance>", where <name> is a short description of the type of
1373 * device, like "pci" or "floppy", and <instance> is the enumerated
1374 * instance of the device, like '0' or '42'. Driver IDs are simply
1375 * "<name>". So, extract the <name> from the platform_device structure,
1376 * and compare it against the name of the driver. Return whether they match
1379 static int platform_match(struct device *dev, struct device_driver *drv)
1381 struct platform_device *pdev = to_platform_device(dev);
1382 struct platform_driver *pdrv = to_platform_driver(drv);
1384 /* When driver_override is set, only bind to the matching driver */
1385 if (pdev->driver_override)
1386 return !strcmp(pdev->driver_override, drv->name);
1388 /* Attempt an OF style match first */
1389 if (of_driver_match_device(dev, drv))
1392 /* Then try ACPI style match */
1393 if (acpi_driver_match_device(dev, drv))
1396 /* Then try to match against the id table */
1398 return platform_match_id(pdrv->id_table, pdev) != NULL;
1400 /* fall-back to driver name match */
1401 return (strcmp(pdev->name, drv->name) == 0);
1404 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1406 struct platform_device *pdev = to_platform_device(dev);
1409 /* Some devices have extra OF data and an OF-style MODALIAS */
1410 rc = of_device_uevent_modalias(dev, env);
1414 rc = acpi_device_uevent_modalias(dev, env);
1418 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1423 static int platform_probe(struct device *_dev)
1425 struct platform_driver *drv = to_platform_driver(_dev->driver);
1426 struct platform_device *dev = to_platform_device(_dev);
1430 * A driver registered using platform_driver_probe() cannot be bound
1431 * again later because the probe function usually lives in __init code
1432 * and so is gone. For these drivers .probe is set to
1433 * platform_probe_fail in __platform_driver_probe(). Don't even prepare
1434 * clocks and PM domains for these to match the traditional behaviour.
1436 if (unlikely(drv->probe == platform_probe_fail))
1439 ret = of_clk_set_defaults(_dev->of_node, false);
1443 ret = dev_pm_domain_attach(_dev, true);
1448 ret = drv->probe(dev);
1450 dev_pm_domain_detach(_dev, true);
1454 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
1455 dev_warn(_dev, "probe deferral not supported\n");
1462 static int platform_remove(struct device *_dev)
1464 struct platform_driver *drv = to_platform_driver(_dev->driver);
1465 struct platform_device *dev = to_platform_device(_dev);
1468 int ret = drv->remove(dev);
1471 dev_warn(_dev, "remove callback returned a non-zero value. This will be ignored.\n");
1473 dev_pm_domain_detach(_dev, true);
1478 static void platform_shutdown(struct device *_dev)
1480 struct platform_device *dev = to_platform_device(_dev);
1481 struct platform_driver *drv;
1486 drv = to_platform_driver(_dev->driver);
1492 int platform_dma_configure(struct device *dev)
1494 enum dev_dma_attr attr;
1498 ret = of_dma_configure(dev, dev->of_node, true);
1499 } else if (has_acpi_companion(dev)) {
1500 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1501 ret = acpi_dma_configure(dev, attr);
1507 static const struct dev_pm_ops platform_dev_pm_ops = {
1508 .runtime_suspend = pm_generic_runtime_suspend,
1509 .runtime_resume = pm_generic_runtime_resume,
1510 USE_PLATFORM_PM_SLEEP_OPS
1513 struct bus_type platform_bus_type = {
1515 .dev_groups = platform_dev_groups,
1516 .match = platform_match,
1517 .uevent = platform_uevent,
1518 .probe = platform_probe,
1519 .remove = platform_remove,
1520 .shutdown = platform_shutdown,
1521 .dma_configure = platform_dma_configure,
1522 .pm = &platform_dev_pm_ops,
1524 EXPORT_SYMBOL_GPL(platform_bus_type);
1526 static inline int __platform_match(struct device *dev, const void *drv)
1528 return platform_match(dev, (struct device_driver *)drv);
1532 * platform_find_device_by_driver - Find a platform device with a given
1534 * @start: The device to start the search from.
1535 * @drv: The device driver to look for.
1537 struct device *platform_find_device_by_driver(struct device *start,
1538 const struct device_driver *drv)
1540 return bus_find_device(&platform_bus_type, start, drv,
1543 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1545 void __weak __init early_platform_cleanup(void) { }
1547 int __init platform_bus_init(void)
1551 early_platform_cleanup();
1553 error = device_register(&platform_bus);
1555 put_device(&platform_bus);
1558 error = bus_register(&platform_bus_type);
1560 device_unregister(&platform_bus);
1561 of_platform_register_reconfig_notifier();