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-model/platform.txt 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>
32 #include "power/power.h"
34 /* For automatically allocated device IDs */
35 static DEFINE_IDA(platform_devid_ida);
37 struct device platform_bus = {
38 .init_name = "platform",
40 EXPORT_SYMBOL_GPL(platform_bus);
43 * arch_setup_pdev_archdata - Allow manipulation of archdata before its used
44 * @pdev: platform device
46 * This is called before platform_device_add() such that any pdev_archdata may
47 * be setup before the platform_notifier is called. So if a user needs to
48 * manipulate any relevant information in the pdev_archdata they can do:
50 * platform_device_alloc()
52 * platform_device_add()
54 * And if they don't care they can just call platform_device_register() and
55 * everything will just work out.
57 void __weak arch_setup_pdev_archdata(struct platform_device *pdev)
62 * platform_get_resource - get a resource for a device
63 * @dev: platform device
64 * @type: resource type
65 * @num: resource index
67 struct resource *platform_get_resource(struct platform_device *dev,
68 unsigned int type, unsigned int num)
72 for (i = 0; i < dev->num_resources; i++) {
73 struct resource *r = &dev->resource[i];
75 if (type == resource_type(r) && num-- == 0)
80 EXPORT_SYMBOL_GPL(platform_get_resource);
83 * platform_get_irq - get an IRQ for a device
84 * @dev: platform device
85 * @num: IRQ number index
87 int platform_get_irq(struct platform_device *dev, unsigned int num)
90 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
91 if (!dev || num >= dev->archdata.num_irqs)
93 return dev->archdata.irqs[num];
96 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
99 ret = of_irq_get(dev->dev.of_node, num);
100 if (ret > 0 || ret == -EPROBE_DEFER)
104 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
105 if (has_acpi_companion(&dev->dev)) {
106 if (r && r->flags & IORESOURCE_DISABLED) {
109 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
116 * The resources may pass trigger flags to the irqs that need
117 * to be set up. It so happens that the trigger flags for
118 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
121 if (r && r->flags & IORESOURCE_BITS) {
122 struct irq_data *irqd;
124 irqd = irq_get_irq_data(r->start);
127 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
134 * For the index 0 interrupt, allow falling back to GpioInt
135 * resources. While a device could have both Interrupt and GpioInt
136 * resources, making this fallback ambiguous, in many common cases
137 * the device will only expose one IRQ, and this fallback
138 * allows a common code path across either kind of resource.
140 if (num == 0 && has_acpi_companion(&dev->dev))
141 return acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
146 EXPORT_SYMBOL_GPL(platform_get_irq);
149 * platform_irq_count - Count the number of IRQs a platform device uses
150 * @dev: platform device
152 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
154 int platform_irq_count(struct platform_device *dev)
158 while ((ret = platform_get_irq(dev, nr)) >= 0)
161 if (ret == -EPROBE_DEFER)
166 EXPORT_SYMBOL_GPL(platform_irq_count);
169 * platform_get_resource_byname - get a resource for a device by name
170 * @dev: platform device
171 * @type: resource type
172 * @name: resource name
174 struct resource *platform_get_resource_byname(struct platform_device *dev,
180 for (i = 0; i < dev->num_resources; i++) {
181 struct resource *r = &dev->resource[i];
183 if (unlikely(!r->name))
186 if (type == resource_type(r) && !strcmp(r->name, name))
191 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
194 * platform_get_irq_byname - get an IRQ for a device by name
195 * @dev: platform device
198 int platform_get_irq_byname(struct platform_device *dev, const char *name)
202 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
205 ret = of_irq_get_byname(dev->dev.of_node, name);
206 if (ret > 0 || ret == -EPROBE_DEFER)
210 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
211 return r ? r->start : -ENXIO;
213 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
216 * platform_add_devices - add a numbers of platform devices
217 * @devs: array of platform devices to add
218 * @num: number of platform devices in array
220 int platform_add_devices(struct platform_device **devs, int num)
224 for (i = 0; i < num; i++) {
225 ret = platform_device_register(devs[i]);
228 platform_device_unregister(devs[i]);
235 EXPORT_SYMBOL_GPL(platform_add_devices);
237 struct platform_object {
238 struct platform_device pdev;
243 * platform_device_put - destroy a platform device
244 * @pdev: platform device to free
246 * Free all memory associated with a platform device. This function must
247 * _only_ be externally called in error cases. All other usage is a bug.
249 void platform_device_put(struct platform_device *pdev)
251 if (!IS_ERR_OR_NULL(pdev))
252 put_device(&pdev->dev);
254 EXPORT_SYMBOL_GPL(platform_device_put);
256 static void platform_device_release(struct device *dev)
258 struct platform_object *pa = container_of(dev, struct platform_object,
261 of_device_node_put(&pa->pdev.dev);
262 kfree(pa->pdev.dev.platform_data);
263 kfree(pa->pdev.mfd_cell);
264 kfree(pa->pdev.resource);
265 kfree(pa->pdev.driver_override);
270 * platform_device_alloc - create a platform device
271 * @name: base name of the device we're adding
274 * Create a platform device object which can have other objects attached
275 * to it, and which will have attached objects freed when it is released.
277 struct platform_device *platform_device_alloc(const char *name, int id)
279 struct platform_object *pa;
281 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
283 strcpy(pa->name, name);
284 pa->pdev.name = pa->name;
286 device_initialize(&pa->pdev.dev);
287 pa->pdev.dev.release = platform_device_release;
288 arch_setup_pdev_archdata(&pa->pdev);
291 return pa ? &pa->pdev : NULL;
293 EXPORT_SYMBOL_GPL(platform_device_alloc);
296 * platform_device_add_resources - add resources to a platform device
297 * @pdev: platform device allocated by platform_device_alloc to add resources to
298 * @res: set of resources that needs to be allocated for the device
299 * @num: number of resources
301 * Add a copy of the resources to the platform device. The memory
302 * associated with the resources will be freed when the platform device is
305 int platform_device_add_resources(struct platform_device *pdev,
306 const struct resource *res, unsigned int num)
308 struct resource *r = NULL;
311 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
316 kfree(pdev->resource);
318 pdev->num_resources = num;
321 EXPORT_SYMBOL_GPL(platform_device_add_resources);
324 * platform_device_add_data - add platform-specific data to a platform device
325 * @pdev: platform device allocated by platform_device_alloc to add resources to
326 * @data: platform specific data for this platform device
327 * @size: size of platform specific data
329 * Add a copy of platform specific data to the platform device's
330 * platform_data pointer. The memory associated with the platform data
331 * will be freed when the platform device is released.
333 int platform_device_add_data(struct platform_device *pdev, const void *data,
339 d = kmemdup(data, size, GFP_KERNEL);
344 kfree(pdev->dev.platform_data);
345 pdev->dev.platform_data = d;
348 EXPORT_SYMBOL_GPL(platform_device_add_data);
351 * platform_device_add_properties - add built-in properties to a platform device
352 * @pdev: platform device to add properties to
353 * @properties: null terminated array of properties to add
355 * The function will take deep copy of @properties and attach the copy to the
356 * platform device. The memory associated with properties will be freed when the
357 * platform device is released.
359 int platform_device_add_properties(struct platform_device *pdev,
360 const struct property_entry *properties)
362 return device_add_properties(&pdev->dev, properties);
364 EXPORT_SYMBOL_GPL(platform_device_add_properties);
367 * platform_device_add - add a platform device to device hierarchy
368 * @pdev: platform device we're adding
370 * This is part 2 of platform_device_register(), though may be called
371 * separately _iff_ pdev was allocated by platform_device_alloc().
373 int platform_device_add(struct platform_device *pdev)
380 if (!pdev->dev.parent)
381 pdev->dev.parent = &platform_bus;
383 pdev->dev.bus = &platform_bus_type;
387 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
389 case PLATFORM_DEVID_NONE:
390 dev_set_name(&pdev->dev, "%s", pdev->name);
392 case PLATFORM_DEVID_AUTO:
394 * Automatically allocated device ID. We mark it as such so
395 * that we remember it must be freed, and we append a suffix
396 * to avoid namespace collision with explicit IDs.
398 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
402 pdev->id_auto = true;
403 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
407 for (i = 0; i < pdev->num_resources; i++) {
408 struct resource *p, *r = &pdev->resource[i];
411 r->name = dev_name(&pdev->dev);
415 if (resource_type(r) == IORESOURCE_MEM)
417 else if (resource_type(r) == IORESOURCE_IO)
418 p = &ioport_resource;
421 if (p && insert_resource(p, r)) {
422 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
428 pr_debug("Registering platform device '%s'. Parent at %s\n",
429 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
431 ret = device_add(&pdev->dev);
437 ida_simple_remove(&platform_devid_ida, pdev->id);
438 pdev->id = PLATFORM_DEVID_AUTO;
442 struct resource *r = &pdev->resource[i];
450 EXPORT_SYMBOL_GPL(platform_device_add);
453 * platform_device_del - remove a platform-level device
454 * @pdev: platform device we're removing
456 * Note that this function will also release all memory- and port-based
457 * resources owned by the device (@dev->resource). This function must
458 * _only_ be externally called in error cases. All other usage is a bug.
460 void platform_device_del(struct platform_device *pdev)
464 if (!IS_ERR_OR_NULL(pdev)) {
465 device_del(&pdev->dev);
468 ida_simple_remove(&platform_devid_ida, pdev->id);
469 pdev->id = PLATFORM_DEVID_AUTO;
472 for (i = 0; i < pdev->num_resources; i++) {
473 struct resource *r = &pdev->resource[i];
479 EXPORT_SYMBOL_GPL(platform_device_del);
482 * platform_device_register - add a platform-level device
483 * @pdev: platform device we're adding
485 int platform_device_register(struct platform_device *pdev)
487 device_initialize(&pdev->dev);
488 arch_setup_pdev_archdata(pdev);
489 return platform_device_add(pdev);
491 EXPORT_SYMBOL_GPL(platform_device_register);
494 * platform_device_unregister - unregister a platform-level device
495 * @pdev: platform device we're unregistering
497 * Unregistration is done in 2 steps. First we release all resources
498 * and remove it from the subsystem, then we drop reference count by
499 * calling platform_device_put().
501 void platform_device_unregister(struct platform_device *pdev)
503 platform_device_del(pdev);
504 platform_device_put(pdev);
506 EXPORT_SYMBOL_GPL(platform_device_unregister);
509 * platform_device_register_full - add a platform-level device with
510 * resources and platform-specific data
512 * @pdevinfo: data used to create device
514 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
516 struct platform_device *platform_device_register_full(
517 const struct platform_device_info *pdevinfo)
520 struct platform_device *pdev;
522 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
524 return ERR_PTR(-ENOMEM);
526 pdev->dev.parent = pdevinfo->parent;
527 pdev->dev.fwnode = pdevinfo->fwnode;
528 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
529 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
531 if (pdevinfo->dma_mask) {
533 * This memory isn't freed when the device is put,
534 * I don't have a nice idea for that though. Conceptually
535 * dma_mask in struct device should not be a pointer.
536 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
539 kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
540 if (!pdev->dev.dma_mask)
543 kmemleak_ignore(pdev->dev.dma_mask);
545 *pdev->dev.dma_mask = pdevinfo->dma_mask;
546 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
549 ret = platform_device_add_resources(pdev,
550 pdevinfo->res, pdevinfo->num_res);
554 ret = platform_device_add_data(pdev,
555 pdevinfo->data, pdevinfo->size_data);
559 if (pdevinfo->properties) {
560 ret = platform_device_add_properties(pdev,
561 pdevinfo->properties);
566 ret = platform_device_add(pdev);
569 ACPI_COMPANION_SET(&pdev->dev, NULL);
570 kfree(pdev->dev.dma_mask);
571 platform_device_put(pdev);
577 EXPORT_SYMBOL_GPL(platform_device_register_full);
579 static int platform_drv_probe(struct device *_dev)
581 struct platform_driver *drv = to_platform_driver(_dev->driver);
582 struct platform_device *dev = to_platform_device(_dev);
585 ret = of_clk_set_defaults(_dev->of_node, false);
589 ret = dev_pm_domain_attach(_dev, true);
594 ret = drv->probe(dev);
596 dev_pm_domain_detach(_dev, true);
600 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
601 dev_warn(_dev, "probe deferral not supported\n");
608 static int platform_drv_probe_fail(struct device *_dev)
613 static int platform_drv_remove(struct device *_dev)
615 struct platform_driver *drv = to_platform_driver(_dev->driver);
616 struct platform_device *dev = to_platform_device(_dev);
620 ret = drv->remove(dev);
621 dev_pm_domain_detach(_dev, true);
626 static void platform_drv_shutdown(struct device *_dev)
628 struct platform_driver *drv = to_platform_driver(_dev->driver);
629 struct platform_device *dev = to_platform_device(_dev);
636 * __platform_driver_register - register a driver for platform-level devices
637 * @drv: platform driver structure
638 * @owner: owning module/driver
640 int __platform_driver_register(struct platform_driver *drv,
641 struct module *owner)
643 drv->driver.owner = owner;
644 drv->driver.bus = &platform_bus_type;
645 drv->driver.probe = platform_drv_probe;
646 drv->driver.remove = platform_drv_remove;
647 drv->driver.shutdown = platform_drv_shutdown;
649 return driver_register(&drv->driver);
651 EXPORT_SYMBOL_GPL(__platform_driver_register);
654 * platform_driver_unregister - unregister a driver for platform-level devices
655 * @drv: platform driver structure
657 void platform_driver_unregister(struct platform_driver *drv)
659 driver_unregister(&drv->driver);
661 EXPORT_SYMBOL_GPL(platform_driver_unregister);
664 * __platform_driver_probe - register driver for non-hotpluggable device
665 * @drv: platform driver structure
666 * @probe: the driver probe routine, probably from an __init section
667 * @module: module which will be the owner of the driver
669 * Use this instead of platform_driver_register() when you know the device
670 * is not hotpluggable and has already been registered, and you want to
671 * remove its run-once probe() infrastructure from memory after the driver
672 * has bound to the device.
674 * One typical use for this would be with drivers for controllers integrated
675 * into system-on-chip processors, where the controller devices have been
676 * configured as part of board setup.
678 * Note that this is incompatible with deferred probing.
680 * Returns zero if the driver registered and bound to a device, else returns
681 * a negative error code and with the driver not registered.
683 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
684 int (*probe)(struct platform_device *), struct module *module)
688 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
689 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
690 drv->driver.name, __func__);
695 * We have to run our probes synchronously because we check if
696 * we find any devices to bind to and exit with error if there
699 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
702 * Prevent driver from requesting probe deferral to avoid further
703 * futile probe attempts.
705 drv->prevent_deferred_probe = true;
707 /* make sure driver won't have bind/unbind attributes */
708 drv->driver.suppress_bind_attrs = true;
710 /* temporary section violation during probe() */
712 retval = code = __platform_driver_register(drv, module);
715 * Fixup that section violation, being paranoid about code scanning
716 * the list of drivers in order to probe new devices. Check to see
717 * if the probe was successful, and make sure any forced probes of
720 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
722 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
724 drv->driver.probe = platform_drv_probe_fail;
725 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
728 platform_driver_unregister(drv);
731 EXPORT_SYMBOL_GPL(__platform_driver_probe);
734 * __platform_create_bundle - register driver and create corresponding device
735 * @driver: platform driver structure
736 * @probe: the driver probe routine, probably from an __init section
737 * @res: set of resources that needs to be allocated for the device
738 * @n_res: number of resources
739 * @data: platform specific data for this platform device
740 * @size: size of platform specific data
741 * @module: module which will be the owner of the driver
743 * Use this in legacy-style modules that probe hardware directly and
744 * register a single platform device and corresponding platform driver.
746 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
748 struct platform_device * __init_or_module __platform_create_bundle(
749 struct platform_driver *driver,
750 int (*probe)(struct platform_device *),
751 struct resource *res, unsigned int n_res,
752 const void *data, size_t size, struct module *module)
754 struct platform_device *pdev;
757 pdev = platform_device_alloc(driver->driver.name, -1);
763 error = platform_device_add_resources(pdev, res, n_res);
767 error = platform_device_add_data(pdev, data, size);
771 error = platform_device_add(pdev);
775 error = __platform_driver_probe(driver, probe, module);
782 platform_device_del(pdev);
784 platform_device_put(pdev);
786 return ERR_PTR(error);
788 EXPORT_SYMBOL_GPL(__platform_create_bundle);
791 * __platform_register_drivers - register an array of platform drivers
792 * @drivers: an array of drivers to register
793 * @count: the number of drivers to register
794 * @owner: module owning the drivers
796 * Registers platform drivers specified by an array. On failure to register a
797 * driver, all previously registered drivers will be unregistered. Callers of
798 * this API should use platform_unregister_drivers() to unregister drivers in
801 * Returns: 0 on success or a negative error code on failure.
803 int __platform_register_drivers(struct platform_driver * const *drivers,
804 unsigned int count, struct module *owner)
809 for (i = 0; i < count; i++) {
810 pr_debug("registering platform driver %ps\n", drivers[i]);
812 err = __platform_driver_register(drivers[i], owner);
814 pr_err("failed to register platform driver %ps: %d\n",
824 pr_debug("unregistering platform driver %ps\n", drivers[i]);
825 platform_driver_unregister(drivers[i]);
830 EXPORT_SYMBOL_GPL(__platform_register_drivers);
833 * platform_unregister_drivers - unregister an array of platform drivers
834 * @drivers: an array of drivers to unregister
835 * @count: the number of drivers to unregister
837 * Unegisters platform drivers specified by an array. This is typically used
838 * to complement an earlier call to platform_register_drivers(). Drivers are
839 * unregistered in the reverse order in which they were registered.
841 void platform_unregister_drivers(struct platform_driver * const *drivers,
845 pr_debug("unregistering platform driver %ps\n", drivers[count]);
846 platform_driver_unregister(drivers[count]);
849 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
851 /* modalias support enables more hands-off userspace setup:
852 * (a) environment variable lets new-style hotplug events work once system is
853 * fully running: "modprobe $MODALIAS"
854 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
855 * mishandled before system is fully running: "modprobe $(cat modalias)"
857 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
860 struct platform_device *pdev = to_platform_device(dev);
863 len = of_device_modalias(dev, buf, PAGE_SIZE);
867 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
871 len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
873 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
875 static DEVICE_ATTR_RO(modalias);
877 static ssize_t driver_override_store(struct device *dev,
878 struct device_attribute *attr,
879 const char *buf, size_t count)
881 struct platform_device *pdev = to_platform_device(dev);
882 char *driver_override, *old, *cp;
884 /* We need to keep extra room for a newline */
885 if (count >= (PAGE_SIZE - 1))
888 driver_override = kstrndup(buf, count, GFP_KERNEL);
889 if (!driver_override)
892 cp = strchr(driver_override, '\n');
897 old = pdev->driver_override;
898 if (strlen(driver_override)) {
899 pdev->driver_override = driver_override;
901 kfree(driver_override);
902 pdev->driver_override = NULL;
911 static ssize_t driver_override_show(struct device *dev,
912 struct device_attribute *attr, char *buf)
914 struct platform_device *pdev = to_platform_device(dev);
918 len = sprintf(buf, "%s\n", pdev->driver_override);
922 static DEVICE_ATTR_RW(driver_override);
925 static struct attribute *platform_dev_attrs[] = {
926 &dev_attr_modalias.attr,
927 &dev_attr_driver_override.attr,
930 ATTRIBUTE_GROUPS(platform_dev);
932 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
934 struct platform_device *pdev = to_platform_device(dev);
937 /* Some devices have extra OF data and an OF-style MODALIAS */
938 rc = of_device_uevent_modalias(dev, env);
942 rc = acpi_device_uevent_modalias(dev, env);
946 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
951 static const struct platform_device_id *platform_match_id(
952 const struct platform_device_id *id,
953 struct platform_device *pdev)
955 while (id->name[0]) {
956 if (strcmp(pdev->name, id->name) == 0) {
966 * platform_match - bind platform device to platform driver.
970 * Platform device IDs are assumed to be encoded like this:
971 * "<name><instance>", where <name> is a short description of the type of
972 * device, like "pci" or "floppy", and <instance> is the enumerated
973 * instance of the device, like '0' or '42'. Driver IDs are simply
974 * "<name>". So, extract the <name> from the platform_device structure,
975 * and compare it against the name of the driver. Return whether they match
978 static int platform_match(struct device *dev, struct device_driver *drv)
980 struct platform_device *pdev = to_platform_device(dev);
981 struct platform_driver *pdrv = to_platform_driver(drv);
983 /* When driver_override is set, only bind to the matching driver */
984 if (pdev->driver_override)
985 return !strcmp(pdev->driver_override, drv->name);
987 /* Attempt an OF style match first */
988 if (of_driver_match_device(dev, drv))
991 /* Then try ACPI style match */
992 if (acpi_driver_match_device(dev, drv))
995 /* Then try to match against the id table */
997 return platform_match_id(pdrv->id_table, pdev) != NULL;
999 /* fall-back to driver name match */
1000 return (strcmp(pdev->name, drv->name) == 0);
1003 #ifdef CONFIG_PM_SLEEP
1005 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1007 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1008 struct platform_device *pdev = to_platform_device(dev);
1011 if (dev->driver && pdrv->suspend)
1012 ret = pdrv->suspend(pdev, mesg);
1017 static int platform_legacy_resume(struct device *dev)
1019 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1020 struct platform_device *pdev = to_platform_device(dev);
1023 if (dev->driver && pdrv->resume)
1024 ret = pdrv->resume(pdev);
1029 #endif /* CONFIG_PM_SLEEP */
1031 #ifdef CONFIG_SUSPEND
1033 int platform_pm_suspend(struct device *dev)
1035 struct device_driver *drv = dev->driver;
1042 if (drv->pm->suspend)
1043 ret = drv->pm->suspend(dev);
1045 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1051 int platform_pm_resume(struct device *dev)
1053 struct device_driver *drv = dev->driver;
1060 if (drv->pm->resume)
1061 ret = drv->pm->resume(dev);
1063 ret = platform_legacy_resume(dev);
1069 #endif /* CONFIG_SUSPEND */
1071 #ifdef CONFIG_HIBERNATE_CALLBACKS
1073 int platform_pm_freeze(struct device *dev)
1075 struct device_driver *drv = dev->driver;
1082 if (drv->pm->freeze)
1083 ret = drv->pm->freeze(dev);
1085 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1091 int platform_pm_thaw(struct device *dev)
1093 struct device_driver *drv = dev->driver;
1101 ret = drv->pm->thaw(dev);
1103 ret = platform_legacy_resume(dev);
1109 int platform_pm_poweroff(struct device *dev)
1111 struct device_driver *drv = dev->driver;
1118 if (drv->pm->poweroff)
1119 ret = drv->pm->poweroff(dev);
1121 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1127 int platform_pm_restore(struct device *dev)
1129 struct device_driver *drv = dev->driver;
1136 if (drv->pm->restore)
1137 ret = drv->pm->restore(dev);
1139 ret = platform_legacy_resume(dev);
1145 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1147 int platform_dma_configure(struct device *dev)
1149 enum dev_dma_attr attr;
1153 ret = of_dma_configure(dev, dev->of_node, true);
1154 } else if (has_acpi_companion(dev)) {
1155 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1156 ret = acpi_dma_configure(dev, attr);
1162 static const struct dev_pm_ops platform_dev_pm_ops = {
1163 .runtime_suspend = pm_generic_runtime_suspend,
1164 .runtime_resume = pm_generic_runtime_resume,
1165 USE_PLATFORM_PM_SLEEP_OPS
1168 struct bus_type platform_bus_type = {
1170 .dev_groups = platform_dev_groups,
1171 .match = platform_match,
1172 .uevent = platform_uevent,
1173 .dma_configure = platform_dma_configure,
1174 .pm = &platform_dev_pm_ops,
1176 EXPORT_SYMBOL_GPL(platform_bus_type);
1178 int __init platform_bus_init(void)
1182 early_platform_cleanup();
1184 error = device_register(&platform_bus);
1186 put_device(&platform_bus);
1189 error = bus_register(&platform_bus_type);
1191 device_unregister(&platform_bus);
1192 of_platform_register_reconfig_notifier();
1196 static __initdata LIST_HEAD(early_platform_driver_list);
1197 static __initdata LIST_HEAD(early_platform_device_list);
1200 * early_platform_driver_register - register early platform driver
1201 * @epdrv: early_platform driver structure
1202 * @buf: string passed from early_param()
1204 * Helper function for early_platform_init() / early_platform_init_buffer()
1206 int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1212 /* Simply add the driver to the end of the global list.
1213 * Drivers will by default be put on the list in compiled-in order.
1215 if (!epdrv->list.next) {
1216 INIT_LIST_HEAD(&epdrv->list);
1217 list_add_tail(&epdrv->list, &early_platform_driver_list);
1220 /* If the user has specified device then make sure the driver
1221 * gets prioritized. The driver of the last device specified on
1222 * command line will be put first on the list.
1224 n = strlen(epdrv->pdrv->driver.name);
1225 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1226 list_move(&epdrv->list, &early_platform_driver_list);
1228 /* Allow passing parameters after device name */
1229 if (buf[n] == '\0' || buf[n] == ',')
1230 epdrv->requested_id = -1;
1232 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1235 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1236 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1239 n += strcspn(&buf[n + 1], ",") + 1;
1245 if (epdrv->bufsize) {
1246 memcpy(epdrv->buffer, &buf[n],
1247 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1248 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1256 * early_platform_add_devices - adds a number of early platform devices
1257 * @devs: array of early platform devices to add
1258 * @num: number of early platform devices in array
1260 * Used by early architecture code to register early platform devices and
1261 * their platform data.
1263 void __init early_platform_add_devices(struct platform_device **devs, int num)
1268 /* simply add the devices to list */
1269 for (i = 0; i < num; i++) {
1270 dev = &devs[i]->dev;
1272 if (!dev->devres_head.next) {
1273 pm_runtime_early_init(dev);
1274 INIT_LIST_HEAD(&dev->devres_head);
1275 list_add_tail(&dev->devres_head,
1276 &early_platform_device_list);
1282 * early_platform_driver_register_all - register early platform drivers
1283 * @class_str: string to identify early platform driver class
1285 * Used by architecture code to register all early platform drivers
1286 * for a certain class. If omitted then only early platform drivers
1287 * with matching kernel command line class parameters will be registered.
1289 void __init early_platform_driver_register_all(char *class_str)
1291 /* The "class_str" parameter may or may not be present on the kernel
1292 * command line. If it is present then there may be more than one
1293 * matching parameter.
1295 * Since we register our early platform drivers using early_param()
1296 * we need to make sure that they also get registered in the case
1297 * when the parameter is missing from the kernel command line.
1299 * We use parse_early_options() to make sure the early_param() gets
1300 * called at least once. The early_param() may be called more than
1301 * once since the name of the preferred device may be specified on
1302 * the kernel command line. early_platform_driver_register() handles
1305 parse_early_options(class_str);
1309 * early_platform_match - find early platform device matching driver
1310 * @epdrv: early platform driver structure
1311 * @id: id to match against
1313 static struct platform_device * __init
1314 early_platform_match(struct early_platform_driver *epdrv, int id)
1316 struct platform_device *pd;
1318 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1319 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1327 * early_platform_left - check if early platform driver has matching devices
1328 * @epdrv: early platform driver structure
1329 * @id: return true if id or above exists
1331 static int __init early_platform_left(struct early_platform_driver *epdrv,
1334 struct platform_device *pd;
1336 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1337 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1345 * early_platform_driver_probe_id - probe drivers matching class_str and id
1346 * @class_str: string to identify early platform driver class
1347 * @id: id to match against
1348 * @nr_probe: number of platform devices to successfully probe before exiting
1350 static int __init early_platform_driver_probe_id(char *class_str,
1354 struct early_platform_driver *epdrv;
1355 struct platform_device *match;
1360 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1361 /* only use drivers matching our class_str */
1362 if (strcmp(class_str, epdrv->class_str))
1366 match_id = epdrv->requested_id;
1371 left += early_platform_left(epdrv, id);
1373 /* skip requested id */
1374 switch (epdrv->requested_id) {
1375 case EARLY_PLATFORM_ID_ERROR:
1376 case EARLY_PLATFORM_ID_UNSET:
1379 if (epdrv->requested_id == id)
1380 match_id = EARLY_PLATFORM_ID_UNSET;
1385 case EARLY_PLATFORM_ID_ERROR:
1386 pr_warn("%s: unable to parse %s parameter\n",
1387 class_str, epdrv->pdrv->driver.name);
1389 case EARLY_PLATFORM_ID_UNSET:
1393 match = early_platform_match(epdrv, match_id);
1398 * Set up a sensible init_name to enable
1399 * dev_name() and others to be used before the
1400 * rest of the driver core is initialized.
1402 if (!match->dev.init_name && slab_is_available()) {
1403 if (match->id != -1)
1404 match->dev.init_name =
1405 kasprintf(GFP_KERNEL, "%s.%d",
1409 match->dev.init_name =
1410 kasprintf(GFP_KERNEL, "%s",
1413 if (!match->dev.init_name)
1417 if (epdrv->pdrv->probe(match))
1418 pr_warn("%s: unable to probe %s early.\n",
1419 class_str, match->name);
1435 * early_platform_driver_probe - probe a class of registered drivers
1436 * @class_str: string to identify early platform driver class
1437 * @nr_probe: number of platform devices to successfully probe before exiting
1438 * @user_only: only probe user specified early platform devices
1440 * Used by architecture code to probe registered early platform drivers
1441 * within a certain class. For probe to happen a registered early platform
1442 * device matching a registered early platform driver is needed.
1444 int __init early_platform_driver_probe(char *class_str,
1451 for (i = -2; n < nr_probe; i++) {
1452 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1467 * early_platform_cleanup - clean up early platform code
1469 void __init early_platform_cleanup(void)
1471 struct platform_device *pd, *pd2;
1473 /* clean up the devres list used to chain devices */
1474 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1476 list_del(&pd->dev.devres_head);
1477 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));