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 */
171 static int platform_do_get_irq(struct platform_device *dev, unsigned int num)
175 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
176 if (!dev || num >= dev->archdata.num_irqs)
178 ret = dev->archdata.irqs[num];
183 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
184 ret = of_irq_get(dev->dev.of_node, num);
185 if (ret > 0 || ret == -EPROBE_DEFER)
189 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
190 if (has_acpi_companion(&dev->dev)) {
191 if (r && r->flags & IORESOURCE_DISABLED) {
192 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
199 * The resources may pass trigger flags to the irqs that need
200 * to be set up. It so happens that the trigger flags for
201 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
204 if (r && r->flags & IORESOURCE_BITS) {
205 struct irq_data *irqd;
207 irqd = irq_get_irq_data(r->start);
210 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
219 * For the index 0 interrupt, allow falling back to GpioInt
220 * resources. While a device could have both Interrupt and GpioInt
221 * resources, making this fallback ambiguous, in many common cases
222 * the device will only expose one IRQ, and this fallback
223 * allows a common code path across either kind of resource.
225 if (num == 0 && has_acpi_companion(&dev->dev)) {
226 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
227 /* Our callers expect -ENXIO for missing IRQs. */
228 if (ret >= 0 || ret == -EPROBE_DEFER)
236 WARN(ret == 0, "0 is an invalid IRQ number\n");
241 * platform_get_irq_optional - get an optional IRQ for a device
242 * @dev: platform device
243 * @num: IRQ number index
245 * Gets an IRQ for a platform device. Device drivers should check the return
246 * value for errors so as to not pass a negative integer value to the
247 * request_irq() APIs. This is the same as platform_get_irq(), except that it
248 * does not print an error message if an IRQ can not be obtained and returns
249 * 0 when IRQ resource has not been found.
253 * int irq = platform_get_irq_optional(pdev, 0);
257 * ...we have IRQ line defined...
259 * Return: non-zero IRQ number on success, negative error number on failure.
261 int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
265 ret = platform_do_get_irq(dev, num);
270 EXPORT_SYMBOL_GPL(platform_get_irq_optional);
273 * platform_get_irq - get an IRQ for a device
274 * @dev: platform device
275 * @num: IRQ number index
277 * Gets an IRQ for a platform device and prints an error message if finding the
278 * IRQ fails. Device drivers should check the return value for errors so as to
279 * not pass a negative integer value to the request_irq() APIs.
283 * int irq = platform_get_irq(pdev, 0);
287 * Return: non-zero IRQ number on success, negative error number on failure.
289 int platform_get_irq(struct platform_device *dev, unsigned int num)
293 ret = platform_do_get_irq(dev, num);
294 if (ret < 0 && ret != -EPROBE_DEFER)
295 dev_err(&dev->dev, "IRQ index %u not found\n", num);
299 EXPORT_SYMBOL_GPL(platform_get_irq);
302 * platform_irq_count - Count the number of IRQs a platform device uses
303 * @dev: platform device
305 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
307 int platform_irq_count(struct platform_device *dev)
311 while ((ret = platform_do_get_irq(dev, nr)) >= 0)
314 if (ret == -EPROBE_DEFER)
319 EXPORT_SYMBOL_GPL(platform_irq_count);
321 struct irq_affinity_devres {
326 static void platform_disable_acpi_irq(struct platform_device *pdev, int index)
330 r = platform_get_resource(pdev, IORESOURCE_IRQ, index);
332 irqresource_disabled(r, 0);
335 static void devm_platform_get_irqs_affinity_release(struct device *dev,
338 struct irq_affinity_devres *ptr = res;
341 for (i = 0; i < ptr->count; i++) {
342 irq_dispose_mapping(ptr->irq[i]);
344 if (has_acpi_companion(dev))
345 platform_disable_acpi_irq(to_platform_device(dev), i);
350 * devm_platform_get_irqs_affinity - devm method to get a set of IRQs for a
351 * device using an interrupt affinity descriptor
352 * @dev: platform device pointer
353 * @affd: affinity descriptor
354 * @minvec: minimum count of interrupt vectors
355 * @maxvec: maximum count of interrupt vectors
356 * @irqs: pointer holder for IRQ numbers
358 * Gets a set of IRQs for a platform device, and updates IRQ afffinty according
359 * to the passed affinity descriptor
361 * Return: Number of vectors on success, negative error number on failure.
363 int devm_platform_get_irqs_affinity(struct platform_device *dev,
364 struct irq_affinity *affd,
369 struct irq_affinity_devres *ptr;
370 struct irq_affinity_desc *desc;
380 nvec = platform_irq_count(dev);
387 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
394 size = sizeof(*ptr) + sizeof(unsigned int) * nvec;
395 ptr = devres_alloc(devm_platform_get_irqs_affinity_release, size,
402 for (i = 0; i < nvec; i++) {
403 int irq = platform_get_irq(dev, i);
406 goto err_free_devres;
411 desc = irq_create_affinity_masks(nvec, affd);
414 goto err_free_devres;
417 for (i = 0; i < nvec; i++) {
418 ret = irq_update_affinity_desc(ptr->irq[i], &desc[i]);
420 dev_err(&dev->dev, "failed to update irq%d affinity descriptor (%d)\n",
426 devres_add(&dev->dev, ptr);
440 EXPORT_SYMBOL_GPL(devm_platform_get_irqs_affinity);
443 * platform_get_resource_byname - get a resource for a device by name
444 * @dev: platform device
445 * @type: resource type
446 * @name: resource name
448 struct resource *platform_get_resource_byname(struct platform_device *dev,
454 for (i = 0; i < dev->num_resources; i++) {
455 struct resource *r = &dev->resource[i];
457 if (unlikely(!r->name))
460 if (type == resource_type(r) && !strcmp(r->name, name))
465 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
467 static int __platform_get_irq_byname(struct platform_device *dev,
473 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
474 ret = of_irq_get_byname(dev->dev.of_node, name);
475 if (ret > 0 || ret == -EPROBE_DEFER)
479 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
481 WARN(r->start == 0, "0 is an invalid IRQ number\n");
489 * platform_get_irq_byname - get an IRQ for a device by name
490 * @dev: platform device
493 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
495 * Return: non-zero IRQ number on success, negative error number on failure.
497 int platform_get_irq_byname(struct platform_device *dev, const char *name)
501 ret = __platform_get_irq_byname(dev, name);
502 if (ret < 0 && ret != -EPROBE_DEFER)
503 dev_err(&dev->dev, "IRQ %s not found\n", name);
507 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
510 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
511 * @dev: platform device
514 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
515 * does not print an error message if an IRQ can not be obtained.
517 * Return: non-zero IRQ number on success, negative error number on failure.
519 int platform_get_irq_byname_optional(struct platform_device *dev,
522 return __platform_get_irq_byname(dev, name);
524 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
527 * platform_add_devices - add a numbers of platform devices
528 * @devs: array of platform devices to add
529 * @num: number of platform devices in array
531 int platform_add_devices(struct platform_device **devs, int num)
535 for (i = 0; i < num; i++) {
536 ret = platform_device_register(devs[i]);
539 platform_device_unregister(devs[i]);
546 EXPORT_SYMBOL_GPL(platform_add_devices);
548 struct platform_object {
549 struct platform_device pdev;
554 * Set up default DMA mask for platform devices if the they weren't
555 * previously set by the architecture / DT.
557 static void setup_pdev_dma_masks(struct platform_device *pdev)
559 pdev->dev.dma_parms = &pdev->dma_parms;
561 if (!pdev->dev.coherent_dma_mask)
562 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
563 if (!pdev->dev.dma_mask) {
564 pdev->platform_dma_mask = DMA_BIT_MASK(32);
565 pdev->dev.dma_mask = &pdev->platform_dma_mask;
570 * platform_device_put - destroy a platform device
571 * @pdev: platform device to free
573 * Free all memory associated with a platform device. This function must
574 * _only_ be externally called in error cases. All other usage is a bug.
576 void platform_device_put(struct platform_device *pdev)
578 if (!IS_ERR_OR_NULL(pdev))
579 put_device(&pdev->dev);
581 EXPORT_SYMBOL_GPL(platform_device_put);
583 static void platform_device_release(struct device *dev)
585 struct platform_object *pa = container_of(dev, struct platform_object,
588 of_node_put(pa->pdev.dev.of_node);
589 kfree(pa->pdev.dev.platform_data);
590 kfree(pa->pdev.mfd_cell);
591 kfree(pa->pdev.resource);
592 kfree(pa->pdev.driver_override);
597 * platform_device_alloc - create a platform device
598 * @name: base name of the device we're adding
601 * Create a platform device object which can have other objects attached
602 * to it, and which will have attached objects freed when it is released.
604 struct platform_device *platform_device_alloc(const char *name, int id)
606 struct platform_object *pa;
608 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
610 strcpy(pa->name, name);
611 pa->pdev.name = pa->name;
613 device_initialize(&pa->pdev.dev);
614 pa->pdev.dev.release = platform_device_release;
615 setup_pdev_dma_masks(&pa->pdev);
618 return pa ? &pa->pdev : NULL;
620 EXPORT_SYMBOL_GPL(platform_device_alloc);
623 * platform_device_add_resources - add resources to a platform device
624 * @pdev: platform device allocated by platform_device_alloc to add resources to
625 * @res: set of resources that needs to be allocated for the device
626 * @num: number of resources
628 * Add a copy of the resources to the platform device. The memory
629 * associated with the resources will be freed when the platform device is
632 int platform_device_add_resources(struct platform_device *pdev,
633 const struct resource *res, unsigned int num)
635 struct resource *r = NULL;
638 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
643 kfree(pdev->resource);
645 pdev->num_resources = num;
648 EXPORT_SYMBOL_GPL(platform_device_add_resources);
651 * platform_device_add_data - add platform-specific data to a platform device
652 * @pdev: platform device allocated by platform_device_alloc to add resources to
653 * @data: platform specific data for this platform device
654 * @size: size of platform specific data
656 * Add a copy of platform specific data to the platform device's
657 * platform_data pointer. The memory associated with the platform data
658 * will be freed when the platform device is released.
660 int platform_device_add_data(struct platform_device *pdev, const void *data,
666 d = kmemdup(data, size, GFP_KERNEL);
671 kfree(pdev->dev.platform_data);
672 pdev->dev.platform_data = d;
675 EXPORT_SYMBOL_GPL(platform_device_add_data);
678 * platform_device_add_properties - add built-in properties to a platform device
679 * @pdev: platform device to add properties to
680 * @properties: null terminated array of properties to add
682 * The function will take deep copy of @properties and attach the copy to the
683 * platform device. The memory associated with properties will be freed when the
684 * platform device is released.
686 int platform_device_add_properties(struct platform_device *pdev,
687 const struct property_entry *properties)
689 return device_add_properties(&pdev->dev, properties);
691 EXPORT_SYMBOL_GPL(platform_device_add_properties);
694 * platform_device_add - add a platform device to device hierarchy
695 * @pdev: platform device we're adding
697 * This is part 2 of platform_device_register(), though may be called
698 * separately _iff_ pdev was allocated by platform_device_alloc().
700 int platform_device_add(struct platform_device *pdev)
708 if (!pdev->dev.parent)
709 pdev->dev.parent = &platform_bus;
711 pdev->dev.bus = &platform_bus_type;
715 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
717 case PLATFORM_DEVID_NONE:
718 dev_set_name(&pdev->dev, "%s", pdev->name);
720 case PLATFORM_DEVID_AUTO:
722 * Automatically allocated device ID. We mark it as such so
723 * that we remember it must be freed, and we append a suffix
724 * to avoid namespace collision with explicit IDs.
726 ret = ida_alloc(&platform_devid_ida, GFP_KERNEL);
730 pdev->id_auto = true;
731 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
735 for (i = 0; i < pdev->num_resources; i++) {
736 struct resource *p, *r = &pdev->resource[i];
739 r->name = dev_name(&pdev->dev);
743 if (resource_type(r) == IORESOURCE_MEM)
745 else if (resource_type(r) == IORESOURCE_IO)
746 p = &ioport_resource;
750 ret = insert_resource(p, r);
752 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
758 pr_debug("Registering platform device '%s'. Parent at %s\n",
759 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
761 ret = device_add(&pdev->dev);
767 ida_free(&platform_devid_ida, pdev->id);
768 pdev->id = PLATFORM_DEVID_AUTO;
772 struct resource *r = &pdev->resource[i];
780 EXPORT_SYMBOL_GPL(platform_device_add);
783 * platform_device_del - remove a platform-level device
784 * @pdev: platform device we're removing
786 * Note that this function will also release all memory- and port-based
787 * resources owned by the device (@dev->resource). This function must
788 * _only_ be externally called in error cases. All other usage is a bug.
790 void platform_device_del(struct platform_device *pdev)
794 if (!IS_ERR_OR_NULL(pdev)) {
795 device_del(&pdev->dev);
798 ida_free(&platform_devid_ida, pdev->id);
799 pdev->id = PLATFORM_DEVID_AUTO;
802 for (i = 0; i < pdev->num_resources; i++) {
803 struct resource *r = &pdev->resource[i];
809 EXPORT_SYMBOL_GPL(platform_device_del);
812 * platform_device_register - add a platform-level device
813 * @pdev: platform device we're adding
815 int platform_device_register(struct platform_device *pdev)
817 device_initialize(&pdev->dev);
818 setup_pdev_dma_masks(pdev);
819 return platform_device_add(pdev);
821 EXPORT_SYMBOL_GPL(platform_device_register);
824 * platform_device_unregister - unregister a platform-level device
825 * @pdev: platform device we're unregistering
827 * Unregistration is done in 2 steps. First we release all resources
828 * and remove it from the subsystem, then we drop reference count by
829 * calling platform_device_put().
831 void platform_device_unregister(struct platform_device *pdev)
833 platform_device_del(pdev);
834 platform_device_put(pdev);
836 EXPORT_SYMBOL_GPL(platform_device_unregister);
839 * platform_device_register_full - add a platform-level device with
840 * resources and platform-specific data
842 * @pdevinfo: data used to create device
844 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
846 struct platform_device *platform_device_register_full(
847 const struct platform_device_info *pdevinfo)
850 struct platform_device *pdev;
852 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
854 return ERR_PTR(-ENOMEM);
856 pdev->dev.parent = pdevinfo->parent;
857 pdev->dev.fwnode = pdevinfo->fwnode;
858 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
859 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
861 if (pdevinfo->dma_mask) {
862 pdev->platform_dma_mask = pdevinfo->dma_mask;
863 pdev->dev.dma_mask = &pdev->platform_dma_mask;
864 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
867 ret = platform_device_add_resources(pdev,
868 pdevinfo->res, pdevinfo->num_res);
872 ret = platform_device_add_data(pdev,
873 pdevinfo->data, pdevinfo->size_data);
877 if (pdevinfo->properties) {
878 ret = platform_device_add_properties(pdev,
879 pdevinfo->properties);
884 ret = platform_device_add(pdev);
887 ACPI_COMPANION_SET(&pdev->dev, NULL);
888 platform_device_put(pdev);
894 EXPORT_SYMBOL_GPL(platform_device_register_full);
897 * __platform_driver_register - register a driver for platform-level devices
898 * @drv: platform driver structure
899 * @owner: owning module/driver
901 int __platform_driver_register(struct platform_driver *drv,
902 struct module *owner)
904 drv->driver.owner = owner;
905 drv->driver.bus = &platform_bus_type;
907 return driver_register(&drv->driver);
909 EXPORT_SYMBOL_GPL(__platform_driver_register);
912 * platform_driver_unregister - unregister a driver for platform-level devices
913 * @drv: platform driver structure
915 void platform_driver_unregister(struct platform_driver *drv)
917 driver_unregister(&drv->driver);
919 EXPORT_SYMBOL_GPL(platform_driver_unregister);
921 static int platform_probe_fail(struct platform_device *pdev)
927 * __platform_driver_probe - register driver for non-hotpluggable device
928 * @drv: platform driver structure
929 * @probe: the driver probe routine, probably from an __init section
930 * @module: module which will be the owner of the driver
932 * Use this instead of platform_driver_register() when you know the device
933 * is not hotpluggable and has already been registered, and you want to
934 * remove its run-once probe() infrastructure from memory after the driver
935 * has bound to the device.
937 * One typical use for this would be with drivers for controllers integrated
938 * into system-on-chip processors, where the controller devices have been
939 * configured as part of board setup.
941 * Note that this is incompatible with deferred probing.
943 * Returns zero if the driver registered and bound to a device, else returns
944 * a negative error code and with the driver not registered.
946 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
947 int (*probe)(struct platform_device *), struct module *module)
951 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
952 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
953 drv->driver.name, __func__);
958 * We have to run our probes synchronously because we check if
959 * we find any devices to bind to and exit with error if there
962 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
965 * Prevent driver from requesting probe deferral to avoid further
966 * futile probe attempts.
968 drv->prevent_deferred_probe = true;
970 /* make sure driver won't have bind/unbind attributes */
971 drv->driver.suppress_bind_attrs = true;
973 /* temporary section violation during probe() */
975 retval = code = __platform_driver_register(drv, module);
980 * Fixup that section violation, being paranoid about code scanning
981 * the list of drivers in order to probe new devices. Check to see
982 * if the probe was successful, and make sure any forced probes of
985 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
986 drv->probe = platform_probe_fail;
987 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
989 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
992 platform_driver_unregister(drv);
995 EXPORT_SYMBOL_GPL(__platform_driver_probe);
998 * __platform_create_bundle - register driver and create corresponding device
999 * @driver: platform driver structure
1000 * @probe: the driver probe routine, probably from an __init section
1001 * @res: set of resources that needs to be allocated for the device
1002 * @n_res: number of resources
1003 * @data: platform specific data for this platform device
1004 * @size: size of platform specific data
1005 * @module: module which will be the owner of the driver
1007 * Use this in legacy-style modules that probe hardware directly and
1008 * register a single platform device and corresponding platform driver.
1010 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
1012 struct platform_device * __init_or_module __platform_create_bundle(
1013 struct platform_driver *driver,
1014 int (*probe)(struct platform_device *),
1015 struct resource *res, unsigned int n_res,
1016 const void *data, size_t size, struct module *module)
1018 struct platform_device *pdev;
1021 pdev = platform_device_alloc(driver->driver.name, -1);
1027 error = platform_device_add_resources(pdev, res, n_res);
1031 error = platform_device_add_data(pdev, data, size);
1035 error = platform_device_add(pdev);
1039 error = __platform_driver_probe(driver, probe, module);
1046 platform_device_del(pdev);
1048 platform_device_put(pdev);
1050 return ERR_PTR(error);
1052 EXPORT_SYMBOL_GPL(__platform_create_bundle);
1055 * __platform_register_drivers - register an array of platform drivers
1056 * @drivers: an array of drivers to register
1057 * @count: the number of drivers to register
1058 * @owner: module owning the drivers
1060 * Registers platform drivers specified by an array. On failure to register a
1061 * driver, all previously registered drivers will be unregistered. Callers of
1062 * this API should use platform_unregister_drivers() to unregister drivers in
1063 * the reverse order.
1065 * Returns: 0 on success or a negative error code on failure.
1067 int __platform_register_drivers(struct platform_driver * const *drivers,
1068 unsigned int count, struct module *owner)
1073 for (i = 0; i < count; i++) {
1074 pr_debug("registering platform driver %ps\n", drivers[i]);
1076 err = __platform_driver_register(drivers[i], owner);
1078 pr_err("failed to register platform driver %ps: %d\n",
1088 pr_debug("unregistering platform driver %ps\n", drivers[i]);
1089 platform_driver_unregister(drivers[i]);
1094 EXPORT_SYMBOL_GPL(__platform_register_drivers);
1097 * platform_unregister_drivers - unregister an array of platform drivers
1098 * @drivers: an array of drivers to unregister
1099 * @count: the number of drivers to unregister
1101 * Unregisters platform drivers specified by an array. This is typically used
1102 * to complement an earlier call to platform_register_drivers(). Drivers are
1103 * unregistered in the reverse order in which they were registered.
1105 void platform_unregister_drivers(struct platform_driver * const *drivers,
1109 pr_debug("unregistering platform driver %ps\n", drivers[count]);
1110 platform_driver_unregister(drivers[count]);
1113 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
1115 static const struct platform_device_id *platform_match_id(
1116 const struct platform_device_id *id,
1117 struct platform_device *pdev)
1119 while (id->name[0]) {
1120 if (strcmp(pdev->name, id->name) == 0) {
1121 pdev->id_entry = id;
1129 #ifdef CONFIG_PM_SLEEP
1131 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1133 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1134 struct platform_device *pdev = to_platform_device(dev);
1137 if (dev->driver && pdrv->suspend)
1138 ret = pdrv->suspend(pdev, mesg);
1143 static int platform_legacy_resume(struct device *dev)
1145 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1146 struct platform_device *pdev = to_platform_device(dev);
1149 if (dev->driver && pdrv->resume)
1150 ret = pdrv->resume(pdev);
1155 #endif /* CONFIG_PM_SLEEP */
1157 #ifdef CONFIG_SUSPEND
1159 int platform_pm_suspend(struct device *dev)
1161 struct device_driver *drv = dev->driver;
1168 if (drv->pm->suspend)
1169 ret = drv->pm->suspend(dev);
1171 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1177 int platform_pm_resume(struct device *dev)
1179 struct device_driver *drv = dev->driver;
1186 if (drv->pm->resume)
1187 ret = drv->pm->resume(dev);
1189 ret = platform_legacy_resume(dev);
1195 #endif /* CONFIG_SUSPEND */
1197 #ifdef CONFIG_HIBERNATE_CALLBACKS
1199 int platform_pm_freeze(struct device *dev)
1201 struct device_driver *drv = dev->driver;
1208 if (drv->pm->freeze)
1209 ret = drv->pm->freeze(dev);
1211 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1217 int platform_pm_thaw(struct device *dev)
1219 struct device_driver *drv = dev->driver;
1227 ret = drv->pm->thaw(dev);
1229 ret = platform_legacy_resume(dev);
1235 int platform_pm_poweroff(struct device *dev)
1237 struct device_driver *drv = dev->driver;
1244 if (drv->pm->poweroff)
1245 ret = drv->pm->poweroff(dev);
1247 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1253 int platform_pm_restore(struct device *dev)
1255 struct device_driver *drv = dev->driver;
1262 if (drv->pm->restore)
1263 ret = drv->pm->restore(dev);
1265 ret = platform_legacy_resume(dev);
1271 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1273 /* modalias support enables more hands-off userspace setup:
1274 * (a) environment variable lets new-style hotplug events work once system is
1275 * fully running: "modprobe $MODALIAS"
1276 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
1277 * mishandled before system is fully running: "modprobe $(cat modalias)"
1279 static ssize_t modalias_show(struct device *dev,
1280 struct device_attribute *attr, char *buf)
1282 struct platform_device *pdev = to_platform_device(dev);
1285 len = of_device_modalias(dev, buf, PAGE_SIZE);
1289 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
1293 return sysfs_emit(buf, "platform:%s\n", pdev->name);
1295 static DEVICE_ATTR_RO(modalias);
1297 static ssize_t numa_node_show(struct device *dev,
1298 struct device_attribute *attr, char *buf)
1300 return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1302 static DEVICE_ATTR_RO(numa_node);
1304 static ssize_t driver_override_show(struct device *dev,
1305 struct device_attribute *attr, char *buf)
1307 struct platform_device *pdev = to_platform_device(dev);
1311 len = sysfs_emit(buf, "%s\n", pdev->driver_override);
1317 static ssize_t driver_override_store(struct device *dev,
1318 struct device_attribute *attr,
1319 const char *buf, size_t count)
1321 struct platform_device *pdev = to_platform_device(dev);
1322 char *driver_override, *old, *cp;
1324 /* We need to keep extra room for a newline */
1325 if (count >= (PAGE_SIZE - 1))
1328 driver_override = kstrndup(buf, count, GFP_KERNEL);
1329 if (!driver_override)
1332 cp = strchr(driver_override, '\n');
1337 old = pdev->driver_override;
1338 if (strlen(driver_override)) {
1339 pdev->driver_override = driver_override;
1341 kfree(driver_override);
1342 pdev->driver_override = NULL;
1350 static DEVICE_ATTR_RW(driver_override);
1352 static struct attribute *platform_dev_attrs[] = {
1353 &dev_attr_modalias.attr,
1354 &dev_attr_numa_node.attr,
1355 &dev_attr_driver_override.attr,
1359 static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a,
1362 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1364 if (a == &dev_attr_numa_node.attr &&
1365 dev_to_node(dev) == NUMA_NO_NODE)
1371 static struct attribute_group platform_dev_group = {
1372 .attrs = platform_dev_attrs,
1373 .is_visible = platform_dev_attrs_visible,
1375 __ATTRIBUTE_GROUPS(platform_dev);
1379 * platform_match - bind platform device to platform driver.
1383 * Platform device IDs are assumed to be encoded like this:
1384 * "<name><instance>", where <name> is a short description of the type of
1385 * device, like "pci" or "floppy", and <instance> is the enumerated
1386 * instance of the device, like '0' or '42'. Driver IDs are simply
1387 * "<name>". So, extract the <name> from the platform_device structure,
1388 * and compare it against the name of the driver. Return whether they match
1391 static int platform_match(struct device *dev, struct device_driver *drv)
1393 struct platform_device *pdev = to_platform_device(dev);
1394 struct platform_driver *pdrv = to_platform_driver(drv);
1396 /* When driver_override is set, only bind to the matching driver */
1397 if (pdev->driver_override)
1398 return !strcmp(pdev->driver_override, drv->name);
1400 /* Attempt an OF style match first */
1401 if (of_driver_match_device(dev, drv))
1404 /* Then try ACPI style match */
1405 if (acpi_driver_match_device(dev, drv))
1408 /* Then try to match against the id table */
1410 return platform_match_id(pdrv->id_table, pdev) != NULL;
1412 /* fall-back to driver name match */
1413 return (strcmp(pdev->name, drv->name) == 0);
1416 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1418 struct platform_device *pdev = to_platform_device(dev);
1421 /* Some devices have extra OF data and an OF-style MODALIAS */
1422 rc = of_device_uevent_modalias(dev, env);
1426 rc = acpi_device_uevent_modalias(dev, env);
1430 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1435 static int platform_probe(struct device *_dev)
1437 struct platform_driver *drv = to_platform_driver(_dev->driver);
1438 struct platform_device *dev = to_platform_device(_dev);
1442 * A driver registered using platform_driver_probe() cannot be bound
1443 * again later because the probe function usually lives in __init code
1444 * and so is gone. For these drivers .probe is set to
1445 * platform_probe_fail in __platform_driver_probe(). Don't even prepare
1446 * clocks and PM domains for these to match the traditional behaviour.
1448 if (unlikely(drv->probe == platform_probe_fail))
1451 ret = of_clk_set_defaults(_dev->of_node, false);
1455 ret = dev_pm_domain_attach(_dev, true);
1460 ret = drv->probe(dev);
1462 dev_pm_domain_detach(_dev, true);
1466 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
1467 dev_warn(_dev, "probe deferral not supported\n");
1474 static int platform_remove(struct device *_dev)
1476 struct platform_driver *drv = to_platform_driver(_dev->driver);
1477 struct platform_device *dev = to_platform_device(_dev);
1480 int ret = drv->remove(dev);
1483 dev_warn(_dev, "remove callback returned a non-zero value. This will be ignored.\n");
1485 dev_pm_domain_detach(_dev, true);
1490 static void platform_shutdown(struct device *_dev)
1492 struct platform_device *dev = to_platform_device(_dev);
1493 struct platform_driver *drv;
1498 drv = to_platform_driver(_dev->driver);
1504 int platform_dma_configure(struct device *dev)
1506 enum dev_dma_attr attr;
1510 ret = of_dma_configure(dev, dev->of_node, true);
1511 } else if (has_acpi_companion(dev)) {
1512 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1513 ret = acpi_dma_configure(dev, attr);
1519 static const struct dev_pm_ops platform_dev_pm_ops = {
1520 .runtime_suspend = pm_generic_runtime_suspend,
1521 .runtime_resume = pm_generic_runtime_resume,
1522 USE_PLATFORM_PM_SLEEP_OPS
1525 struct bus_type platform_bus_type = {
1527 .dev_groups = platform_dev_groups,
1528 .match = platform_match,
1529 .uevent = platform_uevent,
1530 .probe = platform_probe,
1531 .remove = platform_remove,
1532 .shutdown = platform_shutdown,
1533 .dma_configure = platform_dma_configure,
1534 .pm = &platform_dev_pm_ops,
1536 EXPORT_SYMBOL_GPL(platform_bus_type);
1538 static inline int __platform_match(struct device *dev, const void *drv)
1540 return platform_match(dev, (struct device_driver *)drv);
1544 * platform_find_device_by_driver - Find a platform device with a given
1546 * @start: The device to start the search from.
1547 * @drv: The device driver to look for.
1549 struct device *platform_find_device_by_driver(struct device *start,
1550 const struct device_driver *drv)
1552 return bus_find_device(&platform_bus_type, start, drv,
1555 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1557 void __weak __init early_platform_cleanup(void) { }
1559 int __init platform_bus_init(void)
1563 early_platform_cleanup();
1565 error = device_register(&platform_bus);
1567 put_device(&platform_bus);
1570 error = bus_register(&platform_bus_type);
1572 device_unregister(&platform_bus);
1573 of_platform_register_reconfig_notifier();