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 #ifdef CONFIG_HAS_IOMEM
70 * devm_platform_get_and_ioremap_resource - call devm_ioremap_resource() for a
71 * platform device and get resource
73 * @pdev: platform device to use both for memory resource lookup as well as
75 * @index: resource index
76 * @res: optional output parameter to store a pointer to the obtained resource.
78 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
82 devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
83 unsigned int index, struct resource **res)
87 r = platform_get_resource(pdev, IORESOURCE_MEM, index);
90 return devm_ioremap_resource(&pdev->dev, r);
92 EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource);
95 * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
98 * @pdev: platform device to use both for memory resource lookup as well as
100 * @index: resource index
102 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
105 void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
108 return devm_platform_get_and_ioremap_resource(pdev, index, NULL);
110 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
113 * devm_platform_ioremap_resource_wc - write-combined variant of
114 * devm_platform_ioremap_resource()
116 * @pdev: platform device to use both for memory resource lookup as well as
117 * resource management
118 * @index: resource index
120 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
123 void __iomem *devm_platform_ioremap_resource_wc(struct platform_device *pdev,
126 struct resource *res;
128 res = platform_get_resource(pdev, IORESOURCE_MEM, index);
129 return devm_ioremap_resource_wc(&pdev->dev, res);
133 * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for
134 * a platform device, retrieve the
137 * @pdev: platform device to use both for memory resource lookup as well as
138 * resource management
139 * @name: name of the resource
141 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
145 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
148 struct resource *res;
150 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
151 return devm_ioremap_resource(&pdev->dev, res);
153 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname);
154 #endif /* CONFIG_HAS_IOMEM */
157 * platform_get_irq_optional - get an optional IRQ for a device
158 * @dev: platform device
159 * @num: IRQ number index
161 * Gets an IRQ for a platform device. Device drivers should check the return
162 * value for errors so as to not pass a negative integer value to the
163 * request_irq() APIs. This is the same as platform_get_irq(), except that it
164 * does not print an error message if an IRQ can not be obtained.
168 * int irq = platform_get_irq_optional(pdev, 0);
172 * Return: non-zero IRQ number on success, negative error number on failure.
174 int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
178 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
179 if (!dev || num >= dev->archdata.num_irqs)
181 ret = dev->archdata.irqs[num];
186 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
187 ret = of_irq_get(dev->dev.of_node, num);
188 if (ret > 0 || ret == -EPROBE_DEFER)
192 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
193 if (has_acpi_companion(&dev->dev)) {
194 if (r && r->flags & IORESOURCE_DISABLED) {
195 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
202 * The resources may pass trigger flags to the irqs that need
203 * to be set up. It so happens that the trigger flags for
204 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
207 if (r && r->flags & IORESOURCE_BITS) {
208 struct irq_data *irqd;
210 irqd = irq_get_irq_data(r->start);
215 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
224 * For the index 0 interrupt, allow falling back to GpioInt
225 * resources. While a device could have both Interrupt and GpioInt
226 * resources, making this fallback ambiguous, in many common cases
227 * the device will only expose one IRQ, and this fallback
228 * allows a common code path across either kind of resource.
230 if (num == 0 && has_acpi_companion(&dev->dev)) {
231 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
232 /* Our callers expect -ENXIO for missing IRQs. */
233 if (ret >= 0 || ret == -EPROBE_DEFER)
240 WARN(ret == 0, "0 is an invalid IRQ number\n");
243 EXPORT_SYMBOL_GPL(platform_get_irq_optional);
246 * platform_get_irq - get an IRQ for a device
247 * @dev: platform device
248 * @num: IRQ number index
250 * Gets an IRQ for a platform device and prints an error message if finding the
251 * IRQ fails. Device drivers should check the return value for errors so as to
252 * not pass a negative integer value to the request_irq() APIs.
256 * int irq = platform_get_irq(pdev, 0);
260 * Return: non-zero IRQ number on success, negative error number on failure.
262 int platform_get_irq(struct platform_device *dev, unsigned int num)
266 ret = platform_get_irq_optional(dev, num);
267 if (ret < 0 && ret != -EPROBE_DEFER)
268 dev_err(&dev->dev, "IRQ index %u not found\n", num);
272 EXPORT_SYMBOL_GPL(platform_get_irq);
275 * platform_irq_count - Count the number of IRQs a platform device uses
276 * @dev: platform device
278 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
280 int platform_irq_count(struct platform_device *dev)
284 while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
287 if (ret == -EPROBE_DEFER)
292 EXPORT_SYMBOL_GPL(platform_irq_count);
294 struct irq_affinity_devres {
299 static void platform_disable_acpi_irq(struct platform_device *pdev, int index)
303 r = platform_get_resource(pdev, IORESOURCE_IRQ, index);
305 irqresource_disabled(r, 0);
308 static void devm_platform_get_irqs_affinity_release(struct device *dev,
311 struct irq_affinity_devres *ptr = res;
314 for (i = 0; i < ptr->count; i++) {
315 irq_dispose_mapping(ptr->irq[i]);
317 if (has_acpi_companion(dev))
318 platform_disable_acpi_irq(to_platform_device(dev), i);
323 * devm_platform_get_irqs_affinity - devm method to get a set of IRQs for a
324 * device using an interrupt affinity descriptor
325 * @dev: platform device pointer
326 * @affd: affinity descriptor
327 * @minvec: minimum count of interrupt vectors
328 * @maxvec: maximum count of interrupt vectors
329 * @irqs: pointer holder for IRQ numbers
331 * Gets a set of IRQs for a platform device, and updates IRQ afffinty according
332 * to the passed affinity descriptor
334 * Return: Number of vectors on success, negative error number on failure.
336 int devm_platform_get_irqs_affinity(struct platform_device *dev,
337 struct irq_affinity *affd,
342 struct irq_affinity_devres *ptr;
343 struct irq_affinity_desc *desc;
353 nvec = platform_irq_count(dev);
358 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
365 size = sizeof(*ptr) + sizeof(unsigned int) * nvec;
366 ptr = devres_alloc(devm_platform_get_irqs_affinity_release, size,
373 for (i = 0; i < nvec; i++) {
374 int irq = platform_get_irq(dev, i);
377 goto err_free_devres;
382 desc = irq_create_affinity_masks(nvec, affd);
385 goto err_free_devres;
388 for (i = 0; i < nvec; i++) {
389 ret = irq_update_affinity_desc(ptr->irq[i], &desc[i]);
391 dev_err(&dev->dev, "failed to update irq%d affinity descriptor (%d)\n",
397 devres_add(&dev->dev, ptr);
411 EXPORT_SYMBOL_GPL(devm_platform_get_irqs_affinity);
414 * platform_get_resource_byname - get a resource for a device by name
415 * @dev: platform device
416 * @type: resource type
417 * @name: resource name
419 struct resource *platform_get_resource_byname(struct platform_device *dev,
425 for (i = 0; i < dev->num_resources; i++) {
426 struct resource *r = &dev->resource[i];
428 if (unlikely(!r->name))
431 if (type == resource_type(r) && !strcmp(r->name, name))
436 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
438 static int __platform_get_irq_byname(struct platform_device *dev,
444 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
445 ret = of_irq_get_byname(dev->dev.of_node, name);
446 if (ret > 0 || ret == -EPROBE_DEFER)
450 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
452 WARN(r->start == 0, "0 is an invalid IRQ number\n");
460 * platform_get_irq_byname - get an IRQ for a device by name
461 * @dev: platform device
464 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
466 * Return: non-zero IRQ number on success, negative error number on failure.
468 int platform_get_irq_byname(struct platform_device *dev, const char *name)
472 ret = __platform_get_irq_byname(dev, name);
473 if (ret < 0 && ret != -EPROBE_DEFER)
474 dev_err(&dev->dev, "IRQ %s not found\n", name);
478 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
481 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
482 * @dev: platform device
485 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
486 * does not print an error message if an IRQ can not be obtained.
488 * Return: non-zero IRQ number on success, negative error number on failure.
490 int platform_get_irq_byname_optional(struct platform_device *dev,
493 return __platform_get_irq_byname(dev, name);
495 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
498 * platform_add_devices - add a numbers of platform devices
499 * @devs: array of platform devices to add
500 * @num: number of platform devices in array
502 int platform_add_devices(struct platform_device **devs, int num)
506 for (i = 0; i < num; i++) {
507 ret = platform_device_register(devs[i]);
510 platform_device_unregister(devs[i]);
517 EXPORT_SYMBOL_GPL(platform_add_devices);
519 struct platform_object {
520 struct platform_device pdev;
525 * Set up default DMA mask for platform devices if the they weren't
526 * previously set by the architecture / DT.
528 static void setup_pdev_dma_masks(struct platform_device *pdev)
530 pdev->dev.dma_parms = &pdev->dma_parms;
532 if (!pdev->dev.coherent_dma_mask)
533 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
534 if (!pdev->dev.dma_mask) {
535 pdev->platform_dma_mask = DMA_BIT_MASK(32);
536 pdev->dev.dma_mask = &pdev->platform_dma_mask;
541 * platform_device_put - destroy a platform device
542 * @pdev: platform device to free
544 * Free all memory associated with a platform device. This function must
545 * _only_ be externally called in error cases. All other usage is a bug.
547 void platform_device_put(struct platform_device *pdev)
549 if (!IS_ERR_OR_NULL(pdev))
550 put_device(&pdev->dev);
552 EXPORT_SYMBOL_GPL(platform_device_put);
554 static void platform_device_release(struct device *dev)
556 struct platform_object *pa = container_of(dev, struct platform_object,
559 of_device_node_put(&pa->pdev.dev);
560 kfree(pa->pdev.dev.platform_data);
561 kfree(pa->pdev.mfd_cell);
562 kfree(pa->pdev.resource);
563 kfree(pa->pdev.driver_override);
568 * platform_device_alloc - create a platform device
569 * @name: base name of the device we're adding
572 * Create a platform device object which can have other objects attached
573 * to it, and which will have attached objects freed when it is released.
575 struct platform_device *platform_device_alloc(const char *name, int id)
577 struct platform_object *pa;
579 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
581 strcpy(pa->name, name);
582 pa->pdev.name = pa->name;
584 device_initialize(&pa->pdev.dev);
585 pa->pdev.dev.release = platform_device_release;
586 setup_pdev_dma_masks(&pa->pdev);
589 return pa ? &pa->pdev : NULL;
591 EXPORT_SYMBOL_GPL(platform_device_alloc);
594 * platform_device_add_resources - add resources to a platform device
595 * @pdev: platform device allocated by platform_device_alloc to add resources to
596 * @res: set of resources that needs to be allocated for the device
597 * @num: number of resources
599 * Add a copy of the resources to the platform device. The memory
600 * associated with the resources will be freed when the platform device is
603 int platform_device_add_resources(struct platform_device *pdev,
604 const struct resource *res, unsigned int num)
606 struct resource *r = NULL;
609 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
614 kfree(pdev->resource);
616 pdev->num_resources = num;
619 EXPORT_SYMBOL_GPL(platform_device_add_resources);
622 * platform_device_add_data - add platform-specific data to a platform device
623 * @pdev: platform device allocated by platform_device_alloc to add resources to
624 * @data: platform specific data for this platform device
625 * @size: size of platform specific data
627 * Add a copy of platform specific data to the platform device's
628 * platform_data pointer. The memory associated with the platform data
629 * will be freed when the platform device is released.
631 int platform_device_add_data(struct platform_device *pdev, const void *data,
637 d = kmemdup(data, size, GFP_KERNEL);
642 kfree(pdev->dev.platform_data);
643 pdev->dev.platform_data = d;
646 EXPORT_SYMBOL_GPL(platform_device_add_data);
649 * platform_device_add_properties - add built-in properties to a platform device
650 * @pdev: platform device to add properties to
651 * @properties: null terminated array of properties to add
653 * The function will take deep copy of @properties and attach the copy to the
654 * platform device. The memory associated with properties will be freed when the
655 * platform device is released.
657 int platform_device_add_properties(struct platform_device *pdev,
658 const struct property_entry *properties)
660 return device_add_properties(&pdev->dev, properties);
662 EXPORT_SYMBOL_GPL(platform_device_add_properties);
665 * platform_device_add - add a platform device to device hierarchy
666 * @pdev: platform device we're adding
668 * This is part 2 of platform_device_register(), though may be called
669 * separately _iff_ pdev was allocated by platform_device_alloc().
671 int platform_device_add(struct platform_device *pdev)
679 if (!pdev->dev.parent)
680 pdev->dev.parent = &platform_bus;
682 pdev->dev.bus = &platform_bus_type;
686 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
688 case PLATFORM_DEVID_NONE:
689 dev_set_name(&pdev->dev, "%s", pdev->name);
691 case PLATFORM_DEVID_AUTO:
693 * Automatically allocated device ID. We mark it as such so
694 * that we remember it must be freed, and we append a suffix
695 * to avoid namespace collision with explicit IDs.
697 ret = ida_alloc(&platform_devid_ida, GFP_KERNEL);
701 pdev->id_auto = true;
702 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
706 for (i = 0; i < pdev->num_resources; i++) {
707 struct resource *p, *r = &pdev->resource[i];
710 r->name = dev_name(&pdev->dev);
714 if (resource_type(r) == IORESOURCE_MEM)
716 else if (resource_type(r) == IORESOURCE_IO)
717 p = &ioport_resource;
721 ret = insert_resource(p, r);
723 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
729 pr_debug("Registering platform device '%s'. Parent at %s\n",
730 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
732 ret = device_add(&pdev->dev);
738 ida_free(&platform_devid_ida, pdev->id);
739 pdev->id = PLATFORM_DEVID_AUTO;
743 struct resource *r = &pdev->resource[i];
751 EXPORT_SYMBOL_GPL(platform_device_add);
754 * platform_device_del - remove a platform-level device
755 * @pdev: platform device we're removing
757 * Note that this function will also release all memory- and port-based
758 * resources owned by the device (@dev->resource). This function must
759 * _only_ be externally called in error cases. All other usage is a bug.
761 void platform_device_del(struct platform_device *pdev)
765 if (!IS_ERR_OR_NULL(pdev)) {
766 device_del(&pdev->dev);
769 ida_free(&platform_devid_ida, pdev->id);
770 pdev->id = PLATFORM_DEVID_AUTO;
773 for (i = 0; i < pdev->num_resources; i++) {
774 struct resource *r = &pdev->resource[i];
780 EXPORT_SYMBOL_GPL(platform_device_del);
783 * platform_device_register - add a platform-level device
784 * @pdev: platform device we're adding
786 int platform_device_register(struct platform_device *pdev)
788 device_initialize(&pdev->dev);
789 setup_pdev_dma_masks(pdev);
790 return platform_device_add(pdev);
792 EXPORT_SYMBOL_GPL(platform_device_register);
795 * platform_device_unregister - unregister a platform-level device
796 * @pdev: platform device we're unregistering
798 * Unregistration is done in 2 steps. First we release all resources
799 * and remove it from the subsystem, then we drop reference count by
800 * calling platform_device_put().
802 void platform_device_unregister(struct platform_device *pdev)
804 platform_device_del(pdev);
805 platform_device_put(pdev);
807 EXPORT_SYMBOL_GPL(platform_device_unregister);
810 * platform_device_register_full - add a platform-level device with
811 * resources and platform-specific data
813 * @pdevinfo: data used to create device
815 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
817 struct platform_device *platform_device_register_full(
818 const struct platform_device_info *pdevinfo)
821 struct platform_device *pdev;
823 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
825 return ERR_PTR(-ENOMEM);
827 pdev->dev.parent = pdevinfo->parent;
828 pdev->dev.fwnode = pdevinfo->fwnode;
829 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
830 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
832 if (pdevinfo->dma_mask) {
833 pdev->platform_dma_mask = pdevinfo->dma_mask;
834 pdev->dev.dma_mask = &pdev->platform_dma_mask;
835 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
838 ret = platform_device_add_resources(pdev,
839 pdevinfo->res, pdevinfo->num_res);
843 ret = platform_device_add_data(pdev,
844 pdevinfo->data, pdevinfo->size_data);
848 if (pdevinfo->properties) {
849 ret = platform_device_add_properties(pdev,
850 pdevinfo->properties);
855 ret = platform_device_add(pdev);
858 ACPI_COMPANION_SET(&pdev->dev, NULL);
859 platform_device_put(pdev);
865 EXPORT_SYMBOL_GPL(platform_device_register_full);
867 static int platform_drv_probe(struct device *_dev)
869 struct platform_driver *drv = to_platform_driver(_dev->driver);
870 struct platform_device *dev = to_platform_device(_dev);
873 ret = of_clk_set_defaults(_dev->of_node, false);
877 ret = dev_pm_domain_attach(_dev, true);
882 ret = drv->probe(dev);
884 dev_pm_domain_detach(_dev, true);
888 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
889 dev_warn(_dev, "probe deferral not supported\n");
896 static int platform_drv_probe_fail(struct device *_dev)
901 static int platform_drv_remove(struct device *_dev)
903 struct platform_driver *drv = to_platform_driver(_dev->driver);
904 struct platform_device *dev = to_platform_device(_dev);
908 ret = drv->remove(dev);
909 dev_pm_domain_detach(_dev, true);
914 static void platform_drv_shutdown(struct device *_dev)
916 struct platform_driver *drv = to_platform_driver(_dev->driver);
917 struct platform_device *dev = to_platform_device(_dev);
924 * __platform_driver_register - register a driver for platform-level devices
925 * @drv: platform driver structure
926 * @owner: owning module/driver
928 int __platform_driver_register(struct platform_driver *drv,
929 struct module *owner)
931 drv->driver.owner = owner;
932 drv->driver.bus = &platform_bus_type;
933 drv->driver.probe = platform_drv_probe;
934 drv->driver.remove = platform_drv_remove;
935 drv->driver.shutdown = platform_drv_shutdown;
937 return driver_register(&drv->driver);
939 EXPORT_SYMBOL_GPL(__platform_driver_register);
942 * platform_driver_unregister - unregister a driver for platform-level devices
943 * @drv: platform driver structure
945 void platform_driver_unregister(struct platform_driver *drv)
947 driver_unregister(&drv->driver);
949 EXPORT_SYMBOL_GPL(platform_driver_unregister);
952 * __platform_driver_probe - register driver for non-hotpluggable device
953 * @drv: platform driver structure
954 * @probe: the driver probe routine, probably from an __init section
955 * @module: module which will be the owner of the driver
957 * Use this instead of platform_driver_register() when you know the device
958 * is not hotpluggable and has already been registered, and you want to
959 * remove its run-once probe() infrastructure from memory after the driver
960 * has bound to the device.
962 * One typical use for this would be with drivers for controllers integrated
963 * into system-on-chip processors, where the controller devices have been
964 * configured as part of board setup.
966 * Note that this is incompatible with deferred probing.
968 * Returns zero if the driver registered and bound to a device, else returns
969 * a negative error code and with the driver not registered.
971 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
972 int (*probe)(struct platform_device *), struct module *module)
976 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
977 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
978 drv->driver.name, __func__);
983 * We have to run our probes synchronously because we check if
984 * we find any devices to bind to and exit with error if there
987 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
990 * Prevent driver from requesting probe deferral to avoid further
991 * futile probe attempts.
993 drv->prevent_deferred_probe = true;
995 /* make sure driver won't have bind/unbind attributes */
996 drv->driver.suppress_bind_attrs = true;
998 /* temporary section violation during probe() */
1000 retval = code = __platform_driver_register(drv, module);
1005 * Fixup that section violation, being paranoid about code scanning
1006 * the list of drivers in order to probe new devices. Check to see
1007 * if the probe was successful, and make sure any forced probes of
1010 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
1012 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
1014 drv->driver.probe = platform_drv_probe_fail;
1015 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
1018 platform_driver_unregister(drv);
1021 EXPORT_SYMBOL_GPL(__platform_driver_probe);
1024 * __platform_create_bundle - register driver and create corresponding device
1025 * @driver: platform driver structure
1026 * @probe: the driver probe routine, probably from an __init section
1027 * @res: set of resources that needs to be allocated for the device
1028 * @n_res: number of resources
1029 * @data: platform specific data for this platform device
1030 * @size: size of platform specific data
1031 * @module: module which will be the owner of the driver
1033 * Use this in legacy-style modules that probe hardware directly and
1034 * register a single platform device and corresponding platform driver.
1036 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
1038 struct platform_device * __init_or_module __platform_create_bundle(
1039 struct platform_driver *driver,
1040 int (*probe)(struct platform_device *),
1041 struct resource *res, unsigned int n_res,
1042 const void *data, size_t size, struct module *module)
1044 struct platform_device *pdev;
1047 pdev = platform_device_alloc(driver->driver.name, -1);
1053 error = platform_device_add_resources(pdev, res, n_res);
1057 error = platform_device_add_data(pdev, data, size);
1061 error = platform_device_add(pdev);
1065 error = __platform_driver_probe(driver, probe, module);
1072 platform_device_del(pdev);
1074 platform_device_put(pdev);
1076 return ERR_PTR(error);
1078 EXPORT_SYMBOL_GPL(__platform_create_bundle);
1081 * __platform_register_drivers - register an array of platform drivers
1082 * @drivers: an array of drivers to register
1083 * @count: the number of drivers to register
1084 * @owner: module owning the drivers
1086 * Registers platform drivers specified by an array. On failure to register a
1087 * driver, all previously registered drivers will be unregistered. Callers of
1088 * this API should use platform_unregister_drivers() to unregister drivers in
1089 * the reverse order.
1091 * Returns: 0 on success or a negative error code on failure.
1093 int __platform_register_drivers(struct platform_driver * const *drivers,
1094 unsigned int count, struct module *owner)
1099 for (i = 0; i < count; i++) {
1100 pr_debug("registering platform driver %ps\n", drivers[i]);
1102 err = __platform_driver_register(drivers[i], owner);
1104 pr_err("failed to register platform driver %ps: %d\n",
1114 pr_debug("unregistering platform driver %ps\n", drivers[i]);
1115 platform_driver_unregister(drivers[i]);
1120 EXPORT_SYMBOL_GPL(__platform_register_drivers);
1123 * platform_unregister_drivers - unregister an array of platform drivers
1124 * @drivers: an array of drivers to unregister
1125 * @count: the number of drivers to unregister
1127 * Unregisters platform drivers specified by an array. This is typically used
1128 * to complement an earlier call to platform_register_drivers(). Drivers are
1129 * unregistered in the reverse order in which they were registered.
1131 void platform_unregister_drivers(struct platform_driver * const *drivers,
1135 pr_debug("unregistering platform driver %ps\n", drivers[count]);
1136 platform_driver_unregister(drivers[count]);
1139 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
1141 /* modalias support enables more hands-off userspace setup:
1142 * (a) environment variable lets new-style hotplug events work once system is
1143 * fully running: "modprobe $MODALIAS"
1144 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
1145 * mishandled before system is fully running: "modprobe $(cat modalias)"
1147 static ssize_t modalias_show(struct device *dev,
1148 struct device_attribute *attr, char *buf)
1150 struct platform_device *pdev = to_platform_device(dev);
1153 len = of_device_modalias(dev, buf, PAGE_SIZE);
1157 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
1161 return sysfs_emit(buf, "platform:%s\n", pdev->name);
1163 static DEVICE_ATTR_RO(modalias);
1165 static ssize_t driver_override_store(struct device *dev,
1166 struct device_attribute *attr,
1167 const char *buf, size_t count)
1169 struct platform_device *pdev = to_platform_device(dev);
1170 char *driver_override, *old, *cp;
1172 /* We need to keep extra room for a newline */
1173 if (count >= (PAGE_SIZE - 1))
1176 driver_override = kstrndup(buf, count, GFP_KERNEL);
1177 if (!driver_override)
1180 cp = strchr(driver_override, '\n');
1185 old = pdev->driver_override;
1186 if (strlen(driver_override)) {
1187 pdev->driver_override = driver_override;
1189 kfree(driver_override);
1190 pdev->driver_override = NULL;
1199 static ssize_t driver_override_show(struct device *dev,
1200 struct device_attribute *attr, char *buf)
1202 struct platform_device *pdev = to_platform_device(dev);
1206 len = sysfs_emit(buf, "%s\n", pdev->driver_override);
1211 static DEVICE_ATTR_RW(driver_override);
1213 static ssize_t numa_node_show(struct device *dev,
1214 struct device_attribute *attr, char *buf)
1216 return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1218 static DEVICE_ATTR_RO(numa_node);
1220 static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a,
1223 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1225 if (a == &dev_attr_numa_node.attr &&
1226 dev_to_node(dev) == NUMA_NO_NODE)
1232 static struct attribute *platform_dev_attrs[] = {
1233 &dev_attr_modalias.attr,
1234 &dev_attr_numa_node.attr,
1235 &dev_attr_driver_override.attr,
1239 static struct attribute_group platform_dev_group = {
1240 .attrs = platform_dev_attrs,
1241 .is_visible = platform_dev_attrs_visible,
1243 __ATTRIBUTE_GROUPS(platform_dev);
1245 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1247 struct platform_device *pdev = to_platform_device(dev);
1250 /* Some devices have extra OF data and an OF-style MODALIAS */
1251 rc = of_device_uevent_modalias(dev, env);
1255 rc = acpi_device_uevent_modalias(dev, env);
1259 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1264 static const struct platform_device_id *platform_match_id(
1265 const struct platform_device_id *id,
1266 struct platform_device *pdev)
1268 while (id->name[0]) {
1269 if (strcmp(pdev->name, id->name) == 0) {
1270 pdev->id_entry = id;
1279 * platform_match - bind platform device to platform driver.
1283 * Platform device IDs are assumed to be encoded like this:
1284 * "<name><instance>", where <name> is a short description of the type of
1285 * device, like "pci" or "floppy", and <instance> is the enumerated
1286 * instance of the device, like '0' or '42'. Driver IDs are simply
1287 * "<name>". So, extract the <name> from the platform_device structure,
1288 * and compare it against the name of the driver. Return whether they match
1291 static int platform_match(struct device *dev, struct device_driver *drv)
1293 struct platform_device *pdev = to_platform_device(dev);
1294 struct platform_driver *pdrv = to_platform_driver(drv);
1296 /* When driver_override is set, only bind to the matching driver */
1297 if (pdev->driver_override)
1298 return !strcmp(pdev->driver_override, drv->name);
1300 /* Attempt an OF style match first */
1301 if (of_driver_match_device(dev, drv))
1304 /* Then try ACPI style match */
1305 if (acpi_driver_match_device(dev, drv))
1308 /* Then try to match against the id table */
1310 return platform_match_id(pdrv->id_table, pdev) != NULL;
1312 /* fall-back to driver name match */
1313 return (strcmp(pdev->name, drv->name) == 0);
1316 #ifdef CONFIG_PM_SLEEP
1318 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1320 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1321 struct platform_device *pdev = to_platform_device(dev);
1324 if (dev->driver && pdrv->suspend)
1325 ret = pdrv->suspend(pdev, mesg);
1330 static int platform_legacy_resume(struct device *dev)
1332 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1333 struct platform_device *pdev = to_platform_device(dev);
1336 if (dev->driver && pdrv->resume)
1337 ret = pdrv->resume(pdev);
1342 #endif /* CONFIG_PM_SLEEP */
1344 #ifdef CONFIG_SUSPEND
1346 int platform_pm_suspend(struct device *dev)
1348 struct device_driver *drv = dev->driver;
1355 if (drv->pm->suspend)
1356 ret = drv->pm->suspend(dev);
1358 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1364 int platform_pm_resume(struct device *dev)
1366 struct device_driver *drv = dev->driver;
1373 if (drv->pm->resume)
1374 ret = drv->pm->resume(dev);
1376 ret = platform_legacy_resume(dev);
1382 #endif /* CONFIG_SUSPEND */
1384 #ifdef CONFIG_HIBERNATE_CALLBACKS
1386 int platform_pm_freeze(struct device *dev)
1388 struct device_driver *drv = dev->driver;
1395 if (drv->pm->freeze)
1396 ret = drv->pm->freeze(dev);
1398 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1404 int platform_pm_thaw(struct device *dev)
1406 struct device_driver *drv = dev->driver;
1414 ret = drv->pm->thaw(dev);
1416 ret = platform_legacy_resume(dev);
1422 int platform_pm_poweroff(struct device *dev)
1424 struct device_driver *drv = dev->driver;
1431 if (drv->pm->poweroff)
1432 ret = drv->pm->poweroff(dev);
1434 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1440 int platform_pm_restore(struct device *dev)
1442 struct device_driver *drv = dev->driver;
1449 if (drv->pm->restore)
1450 ret = drv->pm->restore(dev);
1452 ret = platform_legacy_resume(dev);
1458 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1460 int platform_dma_configure(struct device *dev)
1462 enum dev_dma_attr attr;
1466 ret = of_dma_configure(dev, dev->of_node, true);
1467 } else if (has_acpi_companion(dev)) {
1468 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1469 ret = acpi_dma_configure(dev, attr);
1475 static const struct dev_pm_ops platform_dev_pm_ops = {
1476 .runtime_suspend = pm_generic_runtime_suspend,
1477 .runtime_resume = pm_generic_runtime_resume,
1478 USE_PLATFORM_PM_SLEEP_OPS
1481 struct bus_type platform_bus_type = {
1483 .dev_groups = platform_dev_groups,
1484 .match = platform_match,
1485 .uevent = platform_uevent,
1486 .dma_configure = platform_dma_configure,
1487 .pm = &platform_dev_pm_ops,
1489 EXPORT_SYMBOL_GPL(platform_bus_type);
1491 static inline int __platform_match(struct device *dev, const void *drv)
1493 return platform_match(dev, (struct device_driver *)drv);
1497 * platform_find_device_by_driver - Find a platform device with a given
1499 * @start: The device to start the search from.
1500 * @drv: The device driver to look for.
1502 struct device *platform_find_device_by_driver(struct device *start,
1503 const struct device_driver *drv)
1505 return bus_find_device(&platform_bus_type, start, drv,
1508 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1510 void __weak __init early_platform_cleanup(void) { }
1512 int __init platform_bus_init(void)
1516 early_platform_cleanup();
1518 error = device_register(&platform_bus);
1520 put_device(&platform_bus);
1523 error = bus_register(&platform_bus_type);
1525 device_unregister(&platform_bus);
1526 of_platform_register_reconfig_notifier();