2 * platform.c - platform 'pseudo' bus for legacy devices
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
7 * This file is released under the GPLv2
9 * Please see Documentation/driver-model/platform.txt for more
13 #include <linux/string.h>
14 #include <linux/platform_device.h>
15 #include <linux/of_device.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/bootmem.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <linux/pm_runtime.h>
26 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
29 struct device platform_bus = {
30 .init_name = "platform",
32 EXPORT_SYMBOL_GPL(platform_bus);
35 * platform_get_resource - get a resource for a device
36 * @dev: platform device
37 * @type: resource type
38 * @num: resource index
40 struct resource *platform_get_resource(struct platform_device *dev,
41 unsigned int type, unsigned int num)
45 for (i = 0; i < dev->num_resources; i++) {
46 struct resource *r = &dev->resource[i];
48 if (type == resource_type(r) && num-- == 0)
53 EXPORT_SYMBOL_GPL(platform_get_resource);
56 * platform_get_irq - get an IRQ for a device
57 * @dev: platform device
58 * @num: IRQ number index
60 int platform_get_irq(struct platform_device *dev, unsigned int num)
62 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
64 return r ? r->start : -ENXIO;
66 EXPORT_SYMBOL_GPL(platform_get_irq);
69 * platform_get_resource_byname - get a resource for a device by name
70 * @dev: platform device
71 * @type: resource type
72 * @name: resource name
74 struct resource *platform_get_resource_byname(struct platform_device *dev,
80 for (i = 0; i < dev->num_resources; i++) {
81 struct resource *r = &dev->resource[i];
83 if (type == resource_type(r) && !strcmp(r->name, name))
88 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
91 * platform_get_irq - get an IRQ for a device
92 * @dev: platform device
95 int platform_get_irq_byname(struct platform_device *dev, const char *name)
97 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
100 return r ? r->start : -ENXIO;
102 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
105 * platform_add_devices - add a numbers of platform devices
106 * @devs: array of platform devices to add
107 * @num: number of platform devices in array
109 int platform_add_devices(struct platform_device **devs, int num)
113 for (i = 0; i < num; i++) {
114 ret = platform_device_register(devs[i]);
117 platform_device_unregister(devs[i]);
124 EXPORT_SYMBOL_GPL(platform_add_devices);
126 struct platform_object {
127 struct platform_device pdev;
132 * platform_device_put - destroy a platform device
133 * @pdev: platform device to free
135 * Free all memory associated with a platform device. This function must
136 * _only_ be externally called in error cases. All other usage is a bug.
138 void platform_device_put(struct platform_device *pdev)
141 put_device(&pdev->dev);
143 EXPORT_SYMBOL_GPL(platform_device_put);
145 static void platform_device_release(struct device *dev)
147 struct platform_object *pa = container_of(dev, struct platform_object,
150 kfree(pa->pdev.dev.platform_data);
151 kfree(pa->pdev.resource);
156 * platform_device_alloc - create a platform device
157 * @name: base name of the device we're adding
160 * Create a platform device object which can have other objects attached
161 * to it, and which will have attached objects freed when it is released.
163 struct platform_device *platform_device_alloc(const char *name, int id)
165 struct platform_object *pa;
167 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
169 strcpy(pa->name, name);
170 pa->pdev.name = pa->name;
172 device_initialize(&pa->pdev.dev);
173 pa->pdev.dev.release = platform_device_release;
176 return pa ? &pa->pdev : NULL;
178 EXPORT_SYMBOL_GPL(platform_device_alloc);
181 * platform_device_add_resources - add resources to a platform device
182 * @pdev: platform device allocated by platform_device_alloc to add resources to
183 * @res: set of resources that needs to be allocated for the device
184 * @num: number of resources
186 * Add a copy of the resources to the platform device. The memory
187 * associated with the resources will be freed when the platform device is
190 int platform_device_add_resources(struct platform_device *pdev,
191 const struct resource *res, unsigned int num)
195 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
197 memcpy(r, res, sizeof(struct resource) * num);
199 pdev->num_resources = num;
201 return r ? 0 : -ENOMEM;
203 EXPORT_SYMBOL_GPL(platform_device_add_resources);
206 * platform_device_add_data - add platform-specific data to a platform device
207 * @pdev: platform device allocated by platform_device_alloc to add resources to
208 * @data: platform specific data for this platform device
209 * @size: size of platform specific data
211 * Add a copy of platform specific data to the platform device's
212 * platform_data pointer. The memory associated with the platform data
213 * will be freed when the platform device is released.
215 int platform_device_add_data(struct platform_device *pdev, const void *data,
218 void *d = kmemdup(data, size, GFP_KERNEL);
221 pdev->dev.platform_data = d;
226 EXPORT_SYMBOL_GPL(platform_device_add_data);
229 * platform_device_add - add a platform device to device hierarchy
230 * @pdev: platform device we're adding
232 * This is part 2 of platform_device_register(), though may be called
233 * separately _iff_ pdev was allocated by platform_device_alloc().
235 int platform_device_add(struct platform_device *pdev)
242 if (!pdev->dev.parent)
243 pdev->dev.parent = &platform_bus;
245 pdev->dev.bus = &platform_bus_type;
248 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
250 dev_set_name(&pdev->dev, "%s", pdev->name);
252 for (i = 0; i < pdev->num_resources; i++) {
253 struct resource *p, *r = &pdev->resource[i];
256 r->name = dev_name(&pdev->dev);
260 if (resource_type(r) == IORESOURCE_MEM)
262 else if (resource_type(r) == IORESOURCE_IO)
263 p = &ioport_resource;
266 if (p && insert_resource(p, r)) {
268 "%s: failed to claim resource %d\n",
269 dev_name(&pdev->dev), i);
275 pr_debug("Registering platform device '%s'. Parent at %s\n",
276 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
278 ret = device_add(&pdev->dev);
284 struct resource *r = &pdev->resource[i];
285 unsigned long type = resource_type(r);
287 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
293 EXPORT_SYMBOL_GPL(platform_device_add);
296 * platform_device_del - remove a platform-level device
297 * @pdev: platform device we're removing
299 * Note that this function will also release all memory- and port-based
300 * resources owned by the device (@dev->resource). This function must
301 * _only_ be externally called in error cases. All other usage is a bug.
303 void platform_device_del(struct platform_device *pdev)
308 device_del(&pdev->dev);
310 for (i = 0; i < pdev->num_resources; i++) {
311 struct resource *r = &pdev->resource[i];
312 unsigned long type = resource_type(r);
314 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
319 EXPORT_SYMBOL_GPL(platform_device_del);
322 * platform_device_register - add a platform-level device
323 * @pdev: platform device we're adding
325 int platform_device_register(struct platform_device *pdev)
327 device_initialize(&pdev->dev);
328 return platform_device_add(pdev);
330 EXPORT_SYMBOL_GPL(platform_device_register);
333 * platform_device_unregister - unregister a platform-level device
334 * @pdev: platform device we're unregistering
336 * Unregistration is done in 2 steps. First we release all resources
337 * and remove it from the subsystem, then we drop reference count by
338 * calling platform_device_put().
340 void platform_device_unregister(struct platform_device *pdev)
342 platform_device_del(pdev);
343 platform_device_put(pdev);
345 EXPORT_SYMBOL_GPL(platform_device_unregister);
348 * platform_device_register_simple - add a platform-level device and its resources
349 * @name: base name of the device we're adding
351 * @res: set of resources that needs to be allocated for the device
352 * @num: number of resources
354 * This function creates a simple platform device that requires minimal
355 * resource and memory management. Canned release function freeing memory
356 * allocated for the device allows drivers using such devices to be
357 * unloaded without waiting for the last reference to the device to be
360 * This interface is primarily intended for use with legacy drivers which
361 * probe hardware directly. Because such drivers create sysfs device nodes
362 * themselves, rather than letting system infrastructure handle such device
363 * enumeration tasks, they don't fully conform to the Linux driver model.
364 * In particular, when such drivers are built as modules, they can't be
367 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
369 struct platform_device *platform_device_register_simple(const char *name,
371 const struct resource *res,
374 struct platform_device *pdev;
377 pdev = platform_device_alloc(name, id);
384 retval = platform_device_add_resources(pdev, res, num);
389 retval = platform_device_add(pdev);
396 platform_device_put(pdev);
397 return ERR_PTR(retval);
399 EXPORT_SYMBOL_GPL(platform_device_register_simple);
402 * platform_device_register_data - add a platform-level device with platform-specific data
403 * @parent: parent device for the device we're adding
404 * @name: base name of the device we're adding
406 * @data: platform specific data for this platform device
407 * @size: size of platform specific data
409 * This function creates a simple platform device that requires minimal
410 * resource and memory management. Canned release function freeing memory
411 * allocated for the device allows drivers using such devices to be
412 * unloaded without waiting for the last reference to the device to be
415 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
417 struct platform_device *platform_device_register_data(
418 struct device *parent,
419 const char *name, int id,
420 const void *data, size_t size)
422 struct platform_device *pdev;
425 pdev = platform_device_alloc(name, id);
431 pdev->dev.parent = parent;
434 retval = platform_device_add_data(pdev, data, size);
439 retval = platform_device_add(pdev);
446 platform_device_put(pdev);
447 return ERR_PTR(retval);
449 EXPORT_SYMBOL_GPL(platform_device_register_data);
451 static int platform_drv_probe(struct device *_dev)
453 struct platform_driver *drv = to_platform_driver(_dev->driver);
454 struct platform_device *dev = to_platform_device(_dev);
456 return drv->probe(dev);
459 static int platform_drv_probe_fail(struct device *_dev)
464 static int platform_drv_remove(struct device *_dev)
466 struct platform_driver *drv = to_platform_driver(_dev->driver);
467 struct platform_device *dev = to_platform_device(_dev);
469 return drv->remove(dev);
472 static void platform_drv_shutdown(struct device *_dev)
474 struct platform_driver *drv = to_platform_driver(_dev->driver);
475 struct platform_device *dev = to_platform_device(_dev);
481 * platform_driver_register - register a driver for platform-level devices
482 * @drv: platform driver structure
484 int platform_driver_register(struct platform_driver *drv)
486 drv->driver.bus = &platform_bus_type;
488 drv->driver.probe = platform_drv_probe;
490 drv->driver.remove = platform_drv_remove;
492 drv->driver.shutdown = platform_drv_shutdown;
494 return driver_register(&drv->driver);
496 EXPORT_SYMBOL_GPL(platform_driver_register);
499 * platform_driver_unregister - unregister a driver for platform-level devices
500 * @drv: platform driver structure
502 void platform_driver_unregister(struct platform_driver *drv)
504 driver_unregister(&drv->driver);
506 EXPORT_SYMBOL_GPL(platform_driver_unregister);
509 * platform_driver_probe - register driver for non-hotpluggable device
510 * @drv: platform driver structure
511 * @probe: the driver probe routine, probably from an __init section
513 * Use this instead of platform_driver_register() when you know the device
514 * is not hotpluggable and has already been registered, and you want to
515 * remove its run-once probe() infrastructure from memory after the driver
516 * has bound to the device.
518 * One typical use for this would be with drivers for controllers integrated
519 * into system-on-chip processors, where the controller devices have been
520 * configured as part of board setup.
522 * Returns zero if the driver registered and bound to a device, else returns
523 * a negative error code and with the driver not registered.
525 int __init_or_module platform_driver_probe(struct platform_driver *drv,
526 int (*probe)(struct platform_device *))
530 /* make sure driver won't have bind/unbind attributes */
531 drv->driver.suppress_bind_attrs = true;
533 /* temporary section violation during probe() */
535 retval = code = platform_driver_register(drv);
538 * Fixup that section violation, being paranoid about code scanning
539 * the list of drivers in order to probe new devices. Check to see
540 * if the probe was successful, and make sure any forced probes of
543 spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
545 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
547 drv->driver.probe = platform_drv_probe_fail;
548 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
551 platform_driver_unregister(drv);
554 EXPORT_SYMBOL_GPL(platform_driver_probe);
557 * platform_create_bundle - register driver and create corresponding device
558 * @driver: platform driver structure
559 * @probe: the driver probe routine, probably from an __init section
560 * @res: set of resources that needs to be allocated for the device
561 * @n_res: number of resources
562 * @data: platform specific data for this platform device
563 * @size: size of platform specific data
565 * Use this in legacy-style modules that probe hardware directly and
566 * register a single platform device and corresponding platform driver.
568 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
570 struct platform_device * __init_or_module platform_create_bundle(
571 struct platform_driver *driver,
572 int (*probe)(struct platform_device *),
573 struct resource *res, unsigned int n_res,
574 const void *data, size_t size)
576 struct platform_device *pdev;
579 pdev = platform_device_alloc(driver->driver.name, -1);
586 error = platform_device_add_resources(pdev, res, n_res);
592 error = platform_device_add_data(pdev, data, size);
597 error = platform_device_add(pdev);
601 error = platform_driver_probe(driver, probe);
608 platform_device_del(pdev);
610 platform_device_put(pdev);
612 return ERR_PTR(error);
614 EXPORT_SYMBOL_GPL(platform_create_bundle);
616 /* modalias support enables more hands-off userspace setup:
617 * (a) environment variable lets new-style hotplug events work once system is
618 * fully running: "modprobe $MODALIAS"
619 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
620 * mishandled before system is fully running: "modprobe $(cat modalias)"
622 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
625 struct platform_device *pdev = to_platform_device(dev);
626 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
628 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
631 static struct device_attribute platform_dev_attrs[] = {
636 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
638 struct platform_device *pdev = to_platform_device(dev);
641 /* Some devices have extra OF data and an OF-style MODALIAS */
642 rc = of_device_uevent(dev,env);
646 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
647 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
651 static const struct platform_device_id *platform_match_id(
652 const struct platform_device_id *id,
653 struct platform_device *pdev)
655 while (id->name[0]) {
656 if (strcmp(pdev->name, id->name) == 0) {
666 * platform_match - bind platform device to platform driver.
670 * Platform device IDs are assumed to be encoded like this:
671 * "<name><instance>", where <name> is a short description of the type of
672 * device, like "pci" or "floppy", and <instance> is the enumerated
673 * instance of the device, like '0' or '42'. Driver IDs are simply
674 * "<name>". So, extract the <name> from the platform_device structure,
675 * and compare it against the name of the driver. Return whether they match
678 static int platform_match(struct device *dev, struct device_driver *drv)
680 struct platform_device *pdev = to_platform_device(dev);
681 struct platform_driver *pdrv = to_platform_driver(drv);
683 /* Attempt an OF style match first */
684 if (of_driver_match_device(dev, drv))
687 /* Then try to match against the id table */
689 return platform_match_id(pdrv->id_table, pdev) != NULL;
691 /* fall-back to driver name match */
692 return (strcmp(pdev->name, drv->name) == 0);
695 #ifdef CONFIG_PM_SLEEP
697 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
699 struct platform_driver *pdrv = to_platform_driver(dev->driver);
700 struct platform_device *pdev = to_platform_device(dev);
703 if (dev->driver && pdrv->suspend)
704 ret = pdrv->suspend(pdev, mesg);
709 static int platform_legacy_resume(struct device *dev)
711 struct platform_driver *pdrv = to_platform_driver(dev->driver);
712 struct platform_device *pdev = to_platform_device(dev);
715 if (dev->driver && pdrv->resume)
716 ret = pdrv->resume(pdev);
721 static int platform_pm_prepare(struct device *dev)
723 struct device_driver *drv = dev->driver;
726 if (drv && drv->pm && drv->pm->prepare)
727 ret = drv->pm->prepare(dev);
732 static void platform_pm_complete(struct device *dev)
734 struct device_driver *drv = dev->driver;
736 if (drv && drv->pm && drv->pm->complete)
737 drv->pm->complete(dev);
740 #else /* !CONFIG_PM_SLEEP */
742 #define platform_pm_prepare NULL
743 #define platform_pm_complete NULL
745 #endif /* !CONFIG_PM_SLEEP */
747 #ifdef CONFIG_SUSPEND
749 int __weak platform_pm_suspend(struct device *dev)
751 struct device_driver *drv = dev->driver;
758 if (drv->pm->suspend)
759 ret = drv->pm->suspend(dev);
761 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
767 int __weak platform_pm_suspend_noirq(struct device *dev)
769 struct device_driver *drv = dev->driver;
776 if (drv->pm->suspend_noirq)
777 ret = drv->pm->suspend_noirq(dev);
783 int __weak platform_pm_resume(struct device *dev)
785 struct device_driver *drv = dev->driver;
793 ret = drv->pm->resume(dev);
795 ret = platform_legacy_resume(dev);
801 int __weak platform_pm_resume_noirq(struct device *dev)
803 struct device_driver *drv = dev->driver;
810 if (drv->pm->resume_noirq)
811 ret = drv->pm->resume_noirq(dev);
817 #else /* !CONFIG_SUSPEND */
819 #define platform_pm_suspend NULL
820 #define platform_pm_resume NULL
821 #define platform_pm_suspend_noirq NULL
822 #define platform_pm_resume_noirq NULL
824 #endif /* !CONFIG_SUSPEND */
826 #ifdef CONFIG_HIBERNATION
828 static int platform_pm_freeze(struct device *dev)
830 struct device_driver *drv = dev->driver;
838 ret = drv->pm->freeze(dev);
840 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
846 static int platform_pm_freeze_noirq(struct device *dev)
848 struct device_driver *drv = dev->driver;
855 if (drv->pm->freeze_noirq)
856 ret = drv->pm->freeze_noirq(dev);
862 static int platform_pm_thaw(struct device *dev)
864 struct device_driver *drv = dev->driver;
872 ret = drv->pm->thaw(dev);
874 ret = platform_legacy_resume(dev);
880 static int platform_pm_thaw_noirq(struct device *dev)
882 struct device_driver *drv = dev->driver;
889 if (drv->pm->thaw_noirq)
890 ret = drv->pm->thaw_noirq(dev);
896 static int platform_pm_poweroff(struct device *dev)
898 struct device_driver *drv = dev->driver;
905 if (drv->pm->poweroff)
906 ret = drv->pm->poweroff(dev);
908 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
914 static int platform_pm_poweroff_noirq(struct device *dev)
916 struct device_driver *drv = dev->driver;
923 if (drv->pm->poweroff_noirq)
924 ret = drv->pm->poweroff_noirq(dev);
930 static int platform_pm_restore(struct device *dev)
932 struct device_driver *drv = dev->driver;
939 if (drv->pm->restore)
940 ret = drv->pm->restore(dev);
942 ret = platform_legacy_resume(dev);
948 static int platform_pm_restore_noirq(struct device *dev)
950 struct device_driver *drv = dev->driver;
957 if (drv->pm->restore_noirq)
958 ret = drv->pm->restore_noirq(dev);
964 #else /* !CONFIG_HIBERNATION */
966 #define platform_pm_freeze NULL
967 #define platform_pm_thaw NULL
968 #define platform_pm_poweroff NULL
969 #define platform_pm_restore NULL
970 #define platform_pm_freeze_noirq NULL
971 #define platform_pm_thaw_noirq NULL
972 #define platform_pm_poweroff_noirq NULL
973 #define platform_pm_restore_noirq NULL
975 #endif /* !CONFIG_HIBERNATION */
977 #ifdef CONFIG_PM_RUNTIME
979 int __weak platform_pm_runtime_suspend(struct device *dev)
981 return pm_generic_runtime_suspend(dev);
984 int __weak platform_pm_runtime_resume(struct device *dev)
986 return pm_generic_runtime_resume(dev);
989 int __weak platform_pm_runtime_idle(struct device *dev)
991 return pm_generic_runtime_idle(dev);
994 #else /* !CONFIG_PM_RUNTIME */
996 #define platform_pm_runtime_suspend NULL
997 #define platform_pm_runtime_resume NULL
998 #define platform_pm_runtime_idle NULL
1000 #endif /* !CONFIG_PM_RUNTIME */
1002 static const struct dev_pm_ops platform_dev_pm_ops = {
1003 .prepare = platform_pm_prepare,
1004 .complete = platform_pm_complete,
1005 .suspend = platform_pm_suspend,
1006 .resume = platform_pm_resume,
1007 .freeze = platform_pm_freeze,
1008 .thaw = platform_pm_thaw,
1009 .poweroff = platform_pm_poweroff,
1010 .restore = platform_pm_restore,
1011 .suspend_noirq = platform_pm_suspend_noirq,
1012 .resume_noirq = platform_pm_resume_noirq,
1013 .freeze_noirq = platform_pm_freeze_noirq,
1014 .thaw_noirq = platform_pm_thaw_noirq,
1015 .poweroff_noirq = platform_pm_poweroff_noirq,
1016 .restore_noirq = platform_pm_restore_noirq,
1017 .runtime_suspend = platform_pm_runtime_suspend,
1018 .runtime_resume = platform_pm_runtime_resume,
1019 .runtime_idle = platform_pm_runtime_idle,
1022 struct bus_type platform_bus_type = {
1024 .dev_attrs = platform_dev_attrs,
1025 .match = platform_match,
1026 .uevent = platform_uevent,
1027 .pm = &platform_dev_pm_ops,
1029 EXPORT_SYMBOL_GPL(platform_bus_type);
1031 int __init platform_bus_init(void)
1035 early_platform_cleanup();
1037 error = device_register(&platform_bus);
1040 error = bus_register(&platform_bus_type);
1042 device_unregister(&platform_bus);
1046 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
1047 u64 dma_get_required_mask(struct device *dev)
1049 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
1050 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
1053 if (!high_totalram) {
1054 /* convert to mask just covering totalram */
1055 low_totalram = (1 << (fls(low_totalram) - 1));
1056 low_totalram += low_totalram - 1;
1057 mask = low_totalram;
1059 high_totalram = (1 << (fls(high_totalram) - 1));
1060 high_totalram += high_totalram - 1;
1061 mask = (((u64)high_totalram) << 32) + 0xffffffff;
1065 EXPORT_SYMBOL_GPL(dma_get_required_mask);
1068 static __initdata LIST_HEAD(early_platform_driver_list);
1069 static __initdata LIST_HEAD(early_platform_device_list);
1072 * early_platform_driver_register - register early platform driver
1073 * @epdrv: early_platform driver structure
1074 * @buf: string passed from early_param()
1076 * Helper function for early_platform_init() / early_platform_init_buffer()
1078 int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1084 /* Simply add the driver to the end of the global list.
1085 * Drivers will by default be put on the list in compiled-in order.
1087 if (!epdrv->list.next) {
1088 INIT_LIST_HEAD(&epdrv->list);
1089 list_add_tail(&epdrv->list, &early_platform_driver_list);
1092 /* If the user has specified device then make sure the driver
1093 * gets prioritized. The driver of the last device specified on
1094 * command line will be put first on the list.
1096 n = strlen(epdrv->pdrv->driver.name);
1097 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1098 list_move(&epdrv->list, &early_platform_driver_list);
1100 /* Allow passing parameters after device name */
1101 if (buf[n] == '\0' || buf[n] == ',')
1102 epdrv->requested_id = -1;
1104 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1107 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1108 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1111 n += strcspn(&buf[n + 1], ",") + 1;
1117 if (epdrv->bufsize) {
1118 memcpy(epdrv->buffer, &buf[n],
1119 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1120 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1128 * early_platform_add_devices - adds a number of early platform devices
1129 * @devs: array of early platform devices to add
1130 * @num: number of early platform devices in array
1132 * Used by early architecture code to register early platform devices and
1133 * their platform data.
1135 void __init early_platform_add_devices(struct platform_device **devs, int num)
1140 /* simply add the devices to list */
1141 for (i = 0; i < num; i++) {
1142 dev = &devs[i]->dev;
1144 if (!dev->devres_head.next) {
1145 INIT_LIST_HEAD(&dev->devres_head);
1146 list_add_tail(&dev->devres_head,
1147 &early_platform_device_list);
1153 * early_platform_driver_register_all - register early platform drivers
1154 * @class_str: string to identify early platform driver class
1156 * Used by architecture code to register all early platform drivers
1157 * for a certain class. If omitted then only early platform drivers
1158 * with matching kernel command line class parameters will be registered.
1160 void __init early_platform_driver_register_all(char *class_str)
1162 /* The "class_str" parameter may or may not be present on the kernel
1163 * command line. If it is present then there may be more than one
1164 * matching parameter.
1166 * Since we register our early platform drivers using early_param()
1167 * we need to make sure that they also get registered in the case
1168 * when the parameter is missing from the kernel command line.
1170 * We use parse_early_options() to make sure the early_param() gets
1171 * called at least once. The early_param() may be called more than
1172 * once since the name of the preferred device may be specified on
1173 * the kernel command line. early_platform_driver_register() handles
1176 parse_early_options(class_str);
1180 * early_platform_match - find early platform device matching driver
1181 * @epdrv: early platform driver structure
1182 * @id: id to match against
1184 static __init struct platform_device *
1185 early_platform_match(struct early_platform_driver *epdrv, int id)
1187 struct platform_device *pd;
1189 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1190 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1198 * early_platform_left - check if early platform driver has matching devices
1199 * @epdrv: early platform driver structure
1200 * @id: return true if id or above exists
1202 static __init int early_platform_left(struct early_platform_driver *epdrv,
1205 struct platform_device *pd;
1207 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1208 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1216 * early_platform_driver_probe_id - probe drivers matching class_str and id
1217 * @class_str: string to identify early platform driver class
1218 * @id: id to match against
1219 * @nr_probe: number of platform devices to successfully probe before exiting
1221 static int __init early_platform_driver_probe_id(char *class_str,
1225 struct early_platform_driver *epdrv;
1226 struct platform_device *match;
1231 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1232 /* only use drivers matching our class_str */
1233 if (strcmp(class_str, epdrv->class_str))
1237 match_id = epdrv->requested_id;
1242 left += early_platform_left(epdrv, id);
1244 /* skip requested id */
1245 switch (epdrv->requested_id) {
1246 case EARLY_PLATFORM_ID_ERROR:
1247 case EARLY_PLATFORM_ID_UNSET:
1250 if (epdrv->requested_id == id)
1251 match_id = EARLY_PLATFORM_ID_UNSET;
1256 case EARLY_PLATFORM_ID_ERROR:
1257 pr_warning("%s: unable to parse %s parameter\n",
1258 class_str, epdrv->pdrv->driver.name);
1260 case EARLY_PLATFORM_ID_UNSET:
1264 match = early_platform_match(epdrv, match_id);
1269 * Set up a sensible init_name to enable
1270 * dev_name() and others to be used before the
1271 * rest of the driver core is initialized.
1273 if (!match->dev.init_name && slab_is_available()) {
1274 if (match->id != -1)
1275 match->dev.init_name =
1276 kasprintf(GFP_KERNEL, "%s.%d",
1280 match->dev.init_name =
1281 kasprintf(GFP_KERNEL, "%s",
1284 if (!match->dev.init_name)
1288 if (epdrv->pdrv->probe(match))
1289 pr_warning("%s: unable to probe %s early.\n",
1290 class_str, match->name);
1306 * early_platform_driver_probe - probe a class of registered drivers
1307 * @class_str: string to identify early platform driver class
1308 * @nr_probe: number of platform devices to successfully probe before exiting
1309 * @user_only: only probe user specified early platform devices
1311 * Used by architecture code to probe registered early platform drivers
1312 * within a certain class. For probe to happen a registered early platform
1313 * device matching a registered early platform driver is needed.
1315 int __init early_platform_driver_probe(char *class_str,
1322 for (i = -2; n < nr_probe; i++) {
1323 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1338 * early_platform_cleanup - clean up early platform code
1340 void __init early_platform_cleanup(void)
1342 struct platform_device *pd, *pd2;
1344 /* clean up the devres list used to chain devices */
1345 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1347 list_del(&pd->dev.devres_head);
1348 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));