1 // SPDX-License-Identifier: GPL-2.0
3 * platform.c - platform 'pseudo' bus for legacy devices
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
8 * Please see Documentation/driver-api/driver-model/platform.rst for more
12 #include <linux/string.h>
13 #include <linux/platform_device.h>
14 #include <linux/of_device.h>
15 #include <linux/of_irq.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/memblock.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/pm_domain.h>
24 #include <linux/idr.h>
25 #include <linux/acpi.h>
26 #include <linux/clk/clk-conf.h>
27 #include <linux/limits.h>
28 #include <linux/property.h>
29 #include <linux/kmemleak.h>
30 #include <linux/types.h>
33 #include "power/power.h"
35 /* For automatically allocated device IDs */
36 static DEFINE_IDA(platform_devid_ida);
38 struct device platform_bus = {
39 .init_name = "platform",
41 EXPORT_SYMBOL_GPL(platform_bus);
44 * platform_get_resource - get a resource for a device
45 * @dev: platform device
46 * @type: resource type
47 * @num: resource index
49 struct resource *platform_get_resource(struct platform_device *dev,
50 unsigned int type, unsigned int num)
54 for (i = 0; i < dev->num_resources; i++) {
55 struct resource *r = &dev->resource[i];
57 if (type == resource_type(r) && num-- == 0)
62 EXPORT_SYMBOL_GPL(platform_get_resource);
65 * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
68 * @pdev: platform device to use both for memory resource lookup as well as
70 * @index: resource index
72 #ifdef CONFIG_HAS_IOMEM
73 void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
78 res = platform_get_resource(pdev, IORESOURCE_MEM, index);
79 return devm_ioremap_resource(&pdev->dev, res);
81 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
82 #endif /* CONFIG_HAS_IOMEM */
84 static int __platform_get_irq(struct platform_device *dev, unsigned int num)
87 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
88 if (!dev || num >= dev->archdata.num_irqs)
90 return dev->archdata.irqs[num];
93 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
96 ret = of_irq_get(dev->dev.of_node, num);
97 if (ret > 0 || ret == -EPROBE_DEFER)
101 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
102 if (has_acpi_companion(&dev->dev)) {
103 if (r && r->flags & IORESOURCE_DISABLED) {
106 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
113 * The resources may pass trigger flags to the irqs that need
114 * to be set up. It so happens that the trigger flags for
115 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
118 if (r && r->flags & IORESOURCE_BITS) {
119 struct irq_data *irqd;
121 irqd = irq_get_irq_data(r->start);
124 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
131 * For the index 0 interrupt, allow falling back to GpioInt
132 * resources. While a device could have both Interrupt and GpioInt
133 * resources, making this fallback ambiguous, in many common cases
134 * the device will only expose one IRQ, and this fallback
135 * allows a common code path across either kind of resource.
137 if (num == 0 && has_acpi_companion(&dev->dev)) {
138 int ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
140 /* Our callers expect -ENXIO for missing IRQs. */
141 if (ret >= 0 || ret == -EPROBE_DEFER)
150 * platform_get_irq - get an IRQ for a device
151 * @dev: platform device
152 * @num: IRQ number index
154 * Gets an IRQ for a platform device and prints an error message if finding the
155 * IRQ fails. Device drivers should check the return value for errors so as to
156 * not pass a negative integer value to the request_irq() APIs.
159 * int irq = platform_get_irq(pdev, 0);
163 * Return: IRQ number on success, negative error number on failure.
165 int platform_get_irq(struct platform_device *dev, unsigned int num)
169 ret = __platform_get_irq(dev, num);
170 if (ret < 0 && ret != -EPROBE_DEFER)
171 dev_err(&dev->dev, "IRQ index %u not found\n", num);
175 EXPORT_SYMBOL_GPL(platform_get_irq);
178 * platform_get_irq_optional - get an optional IRQ for a device
179 * @dev: platform device
180 * @num: IRQ number index
182 * Gets an IRQ for a platform device. Device drivers should check the return
183 * value for errors so as to not pass a negative integer value to the
184 * request_irq() APIs. This is the same as platform_get_irq(), except that it
185 * does not print an error message if an IRQ can not be obtained.
188 * int irq = platform_get_irq_optional(pdev, 0);
192 * Return: IRQ number on success, negative error number on failure.
194 int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
196 return __platform_get_irq(dev, num);
198 EXPORT_SYMBOL_GPL(platform_get_irq_optional);
201 * platform_irq_count - Count the number of IRQs a platform device uses
202 * @dev: platform device
204 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
206 int platform_irq_count(struct platform_device *dev)
210 while ((ret = __platform_get_irq(dev, nr)) >= 0)
213 if (ret == -EPROBE_DEFER)
218 EXPORT_SYMBOL_GPL(platform_irq_count);
221 * platform_get_resource_byname - get a resource for a device by name
222 * @dev: platform device
223 * @type: resource type
224 * @name: resource name
226 struct resource *platform_get_resource_byname(struct platform_device *dev,
232 for (i = 0; i < dev->num_resources; i++) {
233 struct resource *r = &dev->resource[i];
235 if (unlikely(!r->name))
238 if (type == resource_type(r) && !strcmp(r->name, name))
243 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
245 static int __platform_get_irq_byname(struct platform_device *dev,
250 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
253 ret = of_irq_get_byname(dev->dev.of_node, name);
254 if (ret > 0 || ret == -EPROBE_DEFER)
258 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
266 * platform_get_irq_byname - get an IRQ for a device by name
267 * @dev: platform device
270 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
272 * Return: IRQ number on success, negative error number on failure.
274 int platform_get_irq_byname(struct platform_device *dev, const char *name)
278 ret = __platform_get_irq_byname(dev, name);
279 if (ret < 0 && ret != -EPROBE_DEFER)
280 dev_err(&dev->dev, "IRQ %s not found\n", name);
284 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
287 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
288 * @dev: platform device
291 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
292 * does not print an error message if an IRQ can not be obtained.
294 * Return: IRQ number on success, negative error number on failure.
296 int platform_get_irq_byname_optional(struct platform_device *dev,
299 return __platform_get_irq_byname(dev, name);
301 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
304 * platform_add_devices - add a numbers of platform devices
305 * @devs: array of platform devices to add
306 * @num: number of platform devices in array
308 int platform_add_devices(struct platform_device **devs, int num)
312 for (i = 0; i < num; i++) {
313 ret = platform_device_register(devs[i]);
316 platform_device_unregister(devs[i]);
323 EXPORT_SYMBOL_GPL(platform_add_devices);
325 struct platform_object {
326 struct platform_device pdev;
331 * Set up default DMA mask for platform devices if the they weren't
332 * previously set by the architecture / DT.
334 static void setup_pdev_dma_masks(struct platform_device *pdev)
336 if (!pdev->dev.coherent_dma_mask)
337 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
338 if (!pdev->dev.dma_mask) {
339 pdev->platform_dma_mask = DMA_BIT_MASK(32);
340 pdev->dev.dma_mask = &pdev->platform_dma_mask;
345 * platform_device_put - destroy a platform device
346 * @pdev: platform device to free
348 * Free all memory associated with a platform device. This function must
349 * _only_ be externally called in error cases. All other usage is a bug.
351 void platform_device_put(struct platform_device *pdev)
353 if (!IS_ERR_OR_NULL(pdev))
354 put_device(&pdev->dev);
356 EXPORT_SYMBOL_GPL(platform_device_put);
358 static void platform_device_release(struct device *dev)
360 struct platform_object *pa = container_of(dev, struct platform_object,
363 of_device_node_put(&pa->pdev.dev);
364 kfree(pa->pdev.dev.platform_data);
365 kfree(pa->pdev.mfd_cell);
366 kfree(pa->pdev.resource);
367 kfree(pa->pdev.driver_override);
372 * platform_device_alloc - create a platform device
373 * @name: base name of the device we're adding
376 * Create a platform device object which can have other objects attached
377 * to it, and which will have attached objects freed when it is released.
379 struct platform_device *platform_device_alloc(const char *name, int id)
381 struct platform_object *pa;
383 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
385 strcpy(pa->name, name);
386 pa->pdev.name = pa->name;
388 device_initialize(&pa->pdev.dev);
389 pa->pdev.dev.release = platform_device_release;
390 setup_pdev_dma_masks(&pa->pdev);
393 return pa ? &pa->pdev : NULL;
395 EXPORT_SYMBOL_GPL(platform_device_alloc);
398 * platform_device_add_resources - add resources to a platform device
399 * @pdev: platform device allocated by platform_device_alloc to add resources to
400 * @res: set of resources that needs to be allocated for the device
401 * @num: number of resources
403 * Add a copy of the resources to the platform device. The memory
404 * associated with the resources will be freed when the platform device is
407 int platform_device_add_resources(struct platform_device *pdev,
408 const struct resource *res, unsigned int num)
410 struct resource *r = NULL;
413 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
418 kfree(pdev->resource);
420 pdev->num_resources = num;
423 EXPORT_SYMBOL_GPL(platform_device_add_resources);
426 * platform_device_add_data - add platform-specific data to a platform device
427 * @pdev: platform device allocated by platform_device_alloc to add resources to
428 * @data: platform specific data for this platform device
429 * @size: size of platform specific data
431 * Add a copy of platform specific data to the platform device's
432 * platform_data pointer. The memory associated with the platform data
433 * will be freed when the platform device is released.
435 int platform_device_add_data(struct platform_device *pdev, const void *data,
441 d = kmemdup(data, size, GFP_KERNEL);
446 kfree(pdev->dev.platform_data);
447 pdev->dev.platform_data = d;
450 EXPORT_SYMBOL_GPL(platform_device_add_data);
453 * platform_device_add_properties - add built-in properties to a platform device
454 * @pdev: platform device to add properties to
455 * @properties: null terminated array of properties to add
457 * The function will take deep copy of @properties and attach the copy to the
458 * platform device. The memory associated with properties will be freed when the
459 * platform device is released.
461 int platform_device_add_properties(struct platform_device *pdev,
462 const struct property_entry *properties)
464 return device_add_properties(&pdev->dev, properties);
466 EXPORT_SYMBOL_GPL(platform_device_add_properties);
469 * platform_device_add - add a platform device to device hierarchy
470 * @pdev: platform device we're adding
472 * This is part 2 of platform_device_register(), though may be called
473 * separately _iff_ pdev was allocated by platform_device_alloc().
475 int platform_device_add(struct platform_device *pdev)
483 if (!pdev->dev.parent)
484 pdev->dev.parent = &platform_bus;
486 pdev->dev.bus = &platform_bus_type;
490 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
492 case PLATFORM_DEVID_NONE:
493 dev_set_name(&pdev->dev, "%s", pdev->name);
495 case PLATFORM_DEVID_AUTO:
497 * Automatically allocated device ID. We mark it as such so
498 * that we remember it must be freed, and we append a suffix
499 * to avoid namespace collision with explicit IDs.
501 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
505 pdev->id_auto = true;
506 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
510 for (i = 0; i < pdev->num_resources; i++) {
511 struct resource *p, *r = &pdev->resource[i];
514 r->name = dev_name(&pdev->dev);
518 if (resource_type(r) == IORESOURCE_MEM)
520 else if (resource_type(r) == IORESOURCE_IO)
521 p = &ioport_resource;
525 ret = insert_resource(p, r);
527 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
533 pr_debug("Registering platform device '%s'. Parent at %s\n",
534 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
536 ret = device_add(&pdev->dev);
542 ida_simple_remove(&platform_devid_ida, pdev->id);
543 pdev->id = PLATFORM_DEVID_AUTO;
547 struct resource *r = &pdev->resource[i];
555 EXPORT_SYMBOL_GPL(platform_device_add);
558 * platform_device_del - remove a platform-level device
559 * @pdev: platform device we're removing
561 * Note that this function will also release all memory- and port-based
562 * resources owned by the device (@dev->resource). This function must
563 * _only_ be externally called in error cases. All other usage is a bug.
565 void platform_device_del(struct platform_device *pdev)
569 if (!IS_ERR_OR_NULL(pdev)) {
570 device_del(&pdev->dev);
573 ida_simple_remove(&platform_devid_ida, pdev->id);
574 pdev->id = PLATFORM_DEVID_AUTO;
577 for (i = 0; i < pdev->num_resources; i++) {
578 struct resource *r = &pdev->resource[i];
584 EXPORT_SYMBOL_GPL(platform_device_del);
587 * platform_device_register - add a platform-level device
588 * @pdev: platform device we're adding
590 int platform_device_register(struct platform_device *pdev)
592 device_initialize(&pdev->dev);
593 setup_pdev_dma_masks(pdev);
594 return platform_device_add(pdev);
596 EXPORT_SYMBOL_GPL(platform_device_register);
599 * platform_device_unregister - unregister a platform-level device
600 * @pdev: platform device we're unregistering
602 * Unregistration is done in 2 steps. First we release all resources
603 * and remove it from the subsystem, then we drop reference count by
604 * calling platform_device_put().
606 void platform_device_unregister(struct platform_device *pdev)
608 platform_device_del(pdev);
609 platform_device_put(pdev);
611 EXPORT_SYMBOL_GPL(platform_device_unregister);
614 * platform_device_register_full - add a platform-level device with
615 * resources and platform-specific data
617 * @pdevinfo: data used to create device
619 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
621 struct platform_device *platform_device_register_full(
622 const struct platform_device_info *pdevinfo)
625 struct platform_device *pdev;
627 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
629 return ERR_PTR(-ENOMEM);
631 pdev->dev.parent = pdevinfo->parent;
632 pdev->dev.fwnode = pdevinfo->fwnode;
633 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
634 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
636 if (pdevinfo->dma_mask) {
637 pdev->platform_dma_mask = pdevinfo->dma_mask;
638 pdev->dev.dma_mask = &pdev->platform_dma_mask;
639 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
642 ret = platform_device_add_resources(pdev,
643 pdevinfo->res, pdevinfo->num_res);
647 ret = platform_device_add_data(pdev,
648 pdevinfo->data, pdevinfo->size_data);
652 if (pdevinfo->properties) {
653 ret = platform_device_add_properties(pdev,
654 pdevinfo->properties);
659 ret = platform_device_add(pdev);
662 ACPI_COMPANION_SET(&pdev->dev, NULL);
663 platform_device_put(pdev);
669 EXPORT_SYMBOL_GPL(platform_device_register_full);
671 static int platform_drv_probe(struct device *_dev)
673 struct platform_driver *drv = to_platform_driver(_dev->driver);
674 struct platform_device *dev = to_platform_device(_dev);
677 ret = of_clk_set_defaults(_dev->of_node, false);
681 ret = dev_pm_domain_attach(_dev, true);
686 ret = drv->probe(dev);
688 dev_pm_domain_detach(_dev, true);
692 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
693 dev_warn(_dev, "probe deferral not supported\n");
700 static int platform_drv_probe_fail(struct device *_dev)
705 static int platform_drv_remove(struct device *_dev)
707 struct platform_driver *drv = to_platform_driver(_dev->driver);
708 struct platform_device *dev = to_platform_device(_dev);
712 ret = drv->remove(dev);
713 dev_pm_domain_detach(_dev, true);
718 static void platform_drv_shutdown(struct device *_dev)
720 struct platform_driver *drv = to_platform_driver(_dev->driver);
721 struct platform_device *dev = to_platform_device(_dev);
728 * __platform_driver_register - register a driver for platform-level devices
729 * @drv: platform driver structure
730 * @owner: owning module/driver
732 int __platform_driver_register(struct platform_driver *drv,
733 struct module *owner)
735 drv->driver.owner = owner;
736 drv->driver.bus = &platform_bus_type;
737 drv->driver.probe = platform_drv_probe;
738 drv->driver.remove = platform_drv_remove;
739 drv->driver.shutdown = platform_drv_shutdown;
741 return driver_register(&drv->driver);
743 EXPORT_SYMBOL_GPL(__platform_driver_register);
746 * platform_driver_unregister - unregister a driver for platform-level devices
747 * @drv: platform driver structure
749 void platform_driver_unregister(struct platform_driver *drv)
751 driver_unregister(&drv->driver);
753 EXPORT_SYMBOL_GPL(platform_driver_unregister);
756 * __platform_driver_probe - register driver for non-hotpluggable device
757 * @drv: platform driver structure
758 * @probe: the driver probe routine, probably from an __init section
759 * @module: module which will be the owner of the driver
761 * Use this instead of platform_driver_register() when you know the device
762 * is not hotpluggable and has already been registered, and you want to
763 * remove its run-once probe() infrastructure from memory after the driver
764 * has bound to the device.
766 * One typical use for this would be with drivers for controllers integrated
767 * into system-on-chip processors, where the controller devices have been
768 * configured as part of board setup.
770 * Note that this is incompatible with deferred probing.
772 * Returns zero if the driver registered and bound to a device, else returns
773 * a negative error code and with the driver not registered.
775 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
776 int (*probe)(struct platform_device *), struct module *module)
780 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
781 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
782 drv->driver.name, __func__);
787 * We have to run our probes synchronously because we check if
788 * we find any devices to bind to and exit with error if there
791 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
794 * Prevent driver from requesting probe deferral to avoid further
795 * futile probe attempts.
797 drv->prevent_deferred_probe = true;
799 /* make sure driver won't have bind/unbind attributes */
800 drv->driver.suppress_bind_attrs = true;
802 /* temporary section violation during probe() */
804 retval = code = __platform_driver_register(drv, module);
807 * Fixup that section violation, being paranoid about code scanning
808 * the list of drivers in order to probe new devices. Check to see
809 * if the probe was successful, and make sure any forced probes of
812 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
814 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
816 drv->driver.probe = platform_drv_probe_fail;
817 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
820 platform_driver_unregister(drv);
823 EXPORT_SYMBOL_GPL(__platform_driver_probe);
826 * __platform_create_bundle - register driver and create corresponding device
827 * @driver: platform driver structure
828 * @probe: the driver probe routine, probably from an __init section
829 * @res: set of resources that needs to be allocated for the device
830 * @n_res: number of resources
831 * @data: platform specific data for this platform device
832 * @size: size of platform specific data
833 * @module: module which will be the owner of the driver
835 * Use this in legacy-style modules that probe hardware directly and
836 * register a single platform device and corresponding platform driver.
838 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
840 struct platform_device * __init_or_module __platform_create_bundle(
841 struct platform_driver *driver,
842 int (*probe)(struct platform_device *),
843 struct resource *res, unsigned int n_res,
844 const void *data, size_t size, struct module *module)
846 struct platform_device *pdev;
849 pdev = platform_device_alloc(driver->driver.name, -1);
855 error = platform_device_add_resources(pdev, res, n_res);
859 error = platform_device_add_data(pdev, data, size);
863 error = platform_device_add(pdev);
867 error = __platform_driver_probe(driver, probe, module);
874 platform_device_del(pdev);
876 platform_device_put(pdev);
878 return ERR_PTR(error);
880 EXPORT_SYMBOL_GPL(__platform_create_bundle);
883 * __platform_register_drivers - register an array of platform drivers
884 * @drivers: an array of drivers to register
885 * @count: the number of drivers to register
886 * @owner: module owning the drivers
888 * Registers platform drivers specified by an array. On failure to register a
889 * driver, all previously registered drivers will be unregistered. Callers of
890 * this API should use platform_unregister_drivers() to unregister drivers in
893 * Returns: 0 on success or a negative error code on failure.
895 int __platform_register_drivers(struct platform_driver * const *drivers,
896 unsigned int count, struct module *owner)
901 for (i = 0; i < count; i++) {
902 pr_debug("registering platform driver %ps\n", drivers[i]);
904 err = __platform_driver_register(drivers[i], owner);
906 pr_err("failed to register platform driver %ps: %d\n",
916 pr_debug("unregistering platform driver %ps\n", drivers[i]);
917 platform_driver_unregister(drivers[i]);
922 EXPORT_SYMBOL_GPL(__platform_register_drivers);
925 * platform_unregister_drivers - unregister an array of platform drivers
926 * @drivers: an array of drivers to unregister
927 * @count: the number of drivers to unregister
929 * Unegisters platform drivers specified by an array. This is typically used
930 * to complement an earlier call to platform_register_drivers(). Drivers are
931 * unregistered in the reverse order in which they were registered.
933 void platform_unregister_drivers(struct platform_driver * const *drivers,
937 pr_debug("unregistering platform driver %ps\n", drivers[count]);
938 platform_driver_unregister(drivers[count]);
941 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
943 /* modalias support enables more hands-off userspace setup:
944 * (a) environment variable lets new-style hotplug events work once system is
945 * fully running: "modprobe $MODALIAS"
946 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
947 * mishandled before system is fully running: "modprobe $(cat modalias)"
949 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
952 struct platform_device *pdev = to_platform_device(dev);
955 len = of_device_modalias(dev, buf, PAGE_SIZE);
959 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
963 len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
965 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
967 static DEVICE_ATTR_RO(modalias);
969 static ssize_t driver_override_store(struct device *dev,
970 struct device_attribute *attr,
971 const char *buf, size_t count)
973 struct platform_device *pdev = to_platform_device(dev);
974 char *driver_override, *old, *cp;
976 /* We need to keep extra room for a newline */
977 if (count >= (PAGE_SIZE - 1))
980 driver_override = kstrndup(buf, count, GFP_KERNEL);
981 if (!driver_override)
984 cp = strchr(driver_override, '\n');
989 old = pdev->driver_override;
990 if (strlen(driver_override)) {
991 pdev->driver_override = driver_override;
993 kfree(driver_override);
994 pdev->driver_override = NULL;
1003 static ssize_t driver_override_show(struct device *dev,
1004 struct device_attribute *attr, char *buf)
1006 struct platform_device *pdev = to_platform_device(dev);
1010 len = sprintf(buf, "%s\n", pdev->driver_override);
1014 static DEVICE_ATTR_RW(driver_override);
1017 static struct attribute *platform_dev_attrs[] = {
1018 &dev_attr_modalias.attr,
1019 &dev_attr_driver_override.attr,
1022 ATTRIBUTE_GROUPS(platform_dev);
1024 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1026 struct platform_device *pdev = to_platform_device(dev);
1029 /* Some devices have extra OF data and an OF-style MODALIAS */
1030 rc = of_device_uevent_modalias(dev, env);
1034 rc = acpi_device_uevent_modalias(dev, env);
1038 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1043 static const struct platform_device_id *platform_match_id(
1044 const struct platform_device_id *id,
1045 struct platform_device *pdev)
1047 while (id->name[0]) {
1048 if (strcmp(pdev->name, id->name) == 0) {
1049 pdev->id_entry = id;
1058 * platform_match - bind platform device to platform driver.
1062 * Platform device IDs are assumed to be encoded like this:
1063 * "<name><instance>", where <name> is a short description of the type of
1064 * device, like "pci" or "floppy", and <instance> is the enumerated
1065 * instance of the device, like '0' or '42'. Driver IDs are simply
1066 * "<name>". So, extract the <name> from the platform_device structure,
1067 * and compare it against the name of the driver. Return whether they match
1070 static int platform_match(struct device *dev, struct device_driver *drv)
1072 struct platform_device *pdev = to_platform_device(dev);
1073 struct platform_driver *pdrv = to_platform_driver(drv);
1075 /* When driver_override is set, only bind to the matching driver */
1076 if (pdev->driver_override)
1077 return !strcmp(pdev->driver_override, drv->name);
1079 /* Attempt an OF style match first */
1080 if (of_driver_match_device(dev, drv))
1083 /* Then try ACPI style match */
1084 if (acpi_driver_match_device(dev, drv))
1087 /* Then try to match against the id table */
1089 return platform_match_id(pdrv->id_table, pdev) != NULL;
1091 /* fall-back to driver name match */
1092 return (strcmp(pdev->name, drv->name) == 0);
1095 #ifdef CONFIG_PM_SLEEP
1097 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1099 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1100 struct platform_device *pdev = to_platform_device(dev);
1103 if (dev->driver && pdrv->suspend)
1104 ret = pdrv->suspend(pdev, mesg);
1109 static int platform_legacy_resume(struct device *dev)
1111 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1112 struct platform_device *pdev = to_platform_device(dev);
1115 if (dev->driver && pdrv->resume)
1116 ret = pdrv->resume(pdev);
1121 #endif /* CONFIG_PM_SLEEP */
1123 #ifdef CONFIG_SUSPEND
1125 int platform_pm_suspend(struct device *dev)
1127 struct device_driver *drv = dev->driver;
1134 if (drv->pm->suspend)
1135 ret = drv->pm->suspend(dev);
1137 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1143 int platform_pm_resume(struct device *dev)
1145 struct device_driver *drv = dev->driver;
1152 if (drv->pm->resume)
1153 ret = drv->pm->resume(dev);
1155 ret = platform_legacy_resume(dev);
1161 #endif /* CONFIG_SUSPEND */
1163 #ifdef CONFIG_HIBERNATE_CALLBACKS
1165 int platform_pm_freeze(struct device *dev)
1167 struct device_driver *drv = dev->driver;
1174 if (drv->pm->freeze)
1175 ret = drv->pm->freeze(dev);
1177 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1183 int platform_pm_thaw(struct device *dev)
1185 struct device_driver *drv = dev->driver;
1193 ret = drv->pm->thaw(dev);
1195 ret = platform_legacy_resume(dev);
1201 int platform_pm_poweroff(struct device *dev)
1203 struct device_driver *drv = dev->driver;
1210 if (drv->pm->poweroff)
1211 ret = drv->pm->poweroff(dev);
1213 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1219 int platform_pm_restore(struct device *dev)
1221 struct device_driver *drv = dev->driver;
1228 if (drv->pm->restore)
1229 ret = drv->pm->restore(dev);
1231 ret = platform_legacy_resume(dev);
1237 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1239 int platform_dma_configure(struct device *dev)
1241 enum dev_dma_attr attr;
1245 ret = of_dma_configure(dev, dev->of_node, true);
1246 } else if (has_acpi_companion(dev)) {
1247 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1248 ret = acpi_dma_configure(dev, attr);
1254 static const struct dev_pm_ops platform_dev_pm_ops = {
1255 .runtime_suspend = pm_generic_runtime_suspend,
1256 .runtime_resume = pm_generic_runtime_resume,
1257 USE_PLATFORM_PM_SLEEP_OPS
1260 struct bus_type platform_bus_type = {
1262 .dev_groups = platform_dev_groups,
1263 .match = platform_match,
1264 .uevent = platform_uevent,
1265 .dma_configure = platform_dma_configure,
1266 .pm = &platform_dev_pm_ops,
1268 EXPORT_SYMBOL_GPL(platform_bus_type);
1270 static inline int __platform_match(struct device *dev, const void *drv)
1272 return platform_match(dev, (struct device_driver *)drv);
1276 * platform_find_device_by_driver - Find a platform device with a given
1278 * @start: The device to start the search from.
1279 * @drv: The device driver to look for.
1281 struct device *platform_find_device_by_driver(struct device *start,
1282 const struct device_driver *drv)
1284 return bus_find_device(&platform_bus_type, start, drv,
1287 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1289 int __init platform_bus_init(void)
1293 early_platform_cleanup();
1295 error = device_register(&platform_bus);
1297 put_device(&platform_bus);
1300 error = bus_register(&platform_bus_type);
1302 device_unregister(&platform_bus);
1303 of_platform_register_reconfig_notifier();
1307 static __initdata LIST_HEAD(early_platform_driver_list);
1308 static __initdata LIST_HEAD(early_platform_device_list);
1311 * early_platform_driver_register - register early platform driver
1312 * @epdrv: early_platform driver structure
1313 * @buf: string passed from early_param()
1315 * Helper function for early_platform_init() / early_platform_init_buffer()
1317 int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1323 /* Simply add the driver to the end of the global list.
1324 * Drivers will by default be put on the list in compiled-in order.
1326 if (!epdrv->list.next) {
1327 INIT_LIST_HEAD(&epdrv->list);
1328 list_add_tail(&epdrv->list, &early_platform_driver_list);
1331 /* If the user has specified device then make sure the driver
1332 * gets prioritized. The driver of the last device specified on
1333 * command line will be put first on the list.
1335 n = strlen(epdrv->pdrv->driver.name);
1336 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1337 list_move(&epdrv->list, &early_platform_driver_list);
1339 /* Allow passing parameters after device name */
1340 if (buf[n] == '\0' || buf[n] == ',')
1341 epdrv->requested_id = -1;
1343 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1346 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1347 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1350 n += strcspn(&buf[n + 1], ",") + 1;
1356 if (epdrv->bufsize) {
1357 memcpy(epdrv->buffer, &buf[n],
1358 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1359 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1367 * early_platform_add_devices - adds a number of early platform devices
1368 * @devs: array of early platform devices to add
1369 * @num: number of early platform devices in array
1371 * Used by early architecture code to register early platform devices and
1372 * their platform data.
1374 void __init early_platform_add_devices(struct platform_device **devs, int num)
1379 /* simply add the devices to list */
1380 for (i = 0; i < num; i++) {
1381 dev = &devs[i]->dev;
1383 if (!dev->devres_head.next) {
1384 pm_runtime_early_init(dev);
1385 INIT_LIST_HEAD(&dev->devres_head);
1386 list_add_tail(&dev->devres_head,
1387 &early_platform_device_list);
1393 * early_platform_driver_register_all - register early platform drivers
1394 * @class_str: string to identify early platform driver class
1396 * Used by architecture code to register all early platform drivers
1397 * for a certain class. If omitted then only early platform drivers
1398 * with matching kernel command line class parameters will be registered.
1400 void __init early_platform_driver_register_all(char *class_str)
1402 /* The "class_str" parameter may or may not be present on the kernel
1403 * command line. If it is present then there may be more than one
1404 * matching parameter.
1406 * Since we register our early platform drivers using early_param()
1407 * we need to make sure that they also get registered in the case
1408 * when the parameter is missing from the kernel command line.
1410 * We use parse_early_options() to make sure the early_param() gets
1411 * called at least once. The early_param() may be called more than
1412 * once since the name of the preferred device may be specified on
1413 * the kernel command line. early_platform_driver_register() handles
1416 parse_early_options(class_str);
1420 * early_platform_match - find early platform device matching driver
1421 * @epdrv: early platform driver structure
1422 * @id: id to match against
1424 static struct platform_device * __init
1425 early_platform_match(struct early_platform_driver *epdrv, int id)
1427 struct platform_device *pd;
1429 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1430 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1438 * early_platform_left - check if early platform driver has matching devices
1439 * @epdrv: early platform driver structure
1440 * @id: return true if id or above exists
1442 static int __init early_platform_left(struct early_platform_driver *epdrv,
1445 struct platform_device *pd;
1447 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1448 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1456 * early_platform_driver_probe_id - probe drivers matching class_str and id
1457 * @class_str: string to identify early platform driver class
1458 * @id: id to match against
1459 * @nr_probe: number of platform devices to successfully probe before exiting
1461 static int __init early_platform_driver_probe_id(char *class_str,
1465 struct early_platform_driver *epdrv;
1466 struct platform_device *match;
1471 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1472 /* only use drivers matching our class_str */
1473 if (strcmp(class_str, epdrv->class_str))
1477 match_id = epdrv->requested_id;
1482 left += early_platform_left(epdrv, id);
1484 /* skip requested id */
1485 switch (epdrv->requested_id) {
1486 case EARLY_PLATFORM_ID_ERROR:
1487 case EARLY_PLATFORM_ID_UNSET:
1490 if (epdrv->requested_id == id)
1491 match_id = EARLY_PLATFORM_ID_UNSET;
1496 case EARLY_PLATFORM_ID_ERROR:
1497 pr_warn("%s: unable to parse %s parameter\n",
1498 class_str, epdrv->pdrv->driver.name);
1500 case EARLY_PLATFORM_ID_UNSET:
1504 match = early_platform_match(epdrv, match_id);
1509 * Set up a sensible init_name to enable
1510 * dev_name() and others to be used before the
1511 * rest of the driver core is initialized.
1513 if (!match->dev.init_name && slab_is_available()) {
1514 if (match->id != -1)
1515 match->dev.init_name =
1516 kasprintf(GFP_KERNEL, "%s.%d",
1520 match->dev.init_name =
1521 kasprintf(GFP_KERNEL, "%s",
1524 if (!match->dev.init_name)
1528 if (epdrv->pdrv->probe(match))
1529 pr_warn("%s: unable to probe %s early.\n",
1530 class_str, match->name);
1546 * early_platform_driver_probe - probe a class of registered drivers
1547 * @class_str: string to identify early platform driver class
1548 * @nr_probe: number of platform devices to successfully probe before exiting
1549 * @user_only: only probe user specified early platform devices
1551 * Used by architecture code to probe registered early platform drivers
1552 * within a certain class. For probe to happen a registered early platform
1553 * device matching a registered early platform driver is needed.
1555 int __init early_platform_driver_probe(char *class_str,
1562 for (i = -2; n < nr_probe; i++) {
1563 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1578 * early_platform_cleanup - clean up early platform code
1580 void __init early_platform_cleanup(void)
1582 struct platform_device *pd, *pd2;
1584 /* clean up the devres list used to chain devices */
1585 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1587 list_del(&pd->dev.devres_head);
1588 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));