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/module.h>
16 #include <linux/init.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/bootmem.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
25 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
28 struct device platform_bus = {
29 .init_name = "platform",
31 EXPORT_SYMBOL_GPL(platform_bus);
34 * platform_get_resource - get a resource for a device
35 * @dev: platform device
36 * @type: resource type
37 * @num: resource index
39 struct resource *platform_get_resource(struct platform_device *dev,
40 unsigned int type, unsigned int num)
44 for (i = 0; i < dev->num_resources; i++) {
45 struct resource *r = &dev->resource[i];
47 if (type == resource_type(r) && num-- == 0)
52 EXPORT_SYMBOL_GPL(platform_get_resource);
55 * platform_get_irq - get an IRQ for a device
56 * @dev: platform device
57 * @num: IRQ number index
59 int platform_get_irq(struct platform_device *dev, unsigned int num)
61 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
63 return r ? r->start : -ENXIO;
65 EXPORT_SYMBOL_GPL(platform_get_irq);
68 * platform_get_resource_byname - get a resource for a device by name
69 * @dev: platform device
70 * @type: resource type
71 * @name: resource name
73 struct resource *platform_get_resource_byname(struct platform_device *dev,
79 for (i = 0; i < dev->num_resources; i++) {
80 struct resource *r = &dev->resource[i];
82 if (type == resource_type(r) && !strcmp(r->name, name))
87 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
90 * platform_get_irq - get an IRQ for a device
91 * @dev: platform device
94 int platform_get_irq_byname(struct platform_device *dev, const char *name)
96 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
99 return r ? r->start : -ENXIO;
101 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
104 * platform_add_devices - add a numbers of platform devices
105 * @devs: array of platform devices to add
106 * @num: number of platform devices in array
108 int platform_add_devices(struct platform_device **devs, int num)
112 for (i = 0; i < num; i++) {
113 ret = platform_device_register(devs[i]);
116 platform_device_unregister(devs[i]);
123 EXPORT_SYMBOL_GPL(platform_add_devices);
125 struct platform_object {
126 struct platform_device pdev;
131 * platform_device_put - destroy a platform device
132 * @pdev: platform device to free
134 * Free all memory associated with a platform device. This function must
135 * _only_ be externally called in error cases. All other usage is a bug.
137 void platform_device_put(struct platform_device *pdev)
140 put_device(&pdev->dev);
142 EXPORT_SYMBOL_GPL(platform_device_put);
144 static void platform_device_release(struct device *dev)
146 struct platform_object *pa = container_of(dev, struct platform_object,
149 kfree(pa->pdev.dev.platform_data);
150 kfree(pa->pdev.resource);
155 * platform_device_alloc - create a platform device
156 * @name: base name of the device we're adding
159 * Create a platform device object which can have other objects attached
160 * to it, and which will have attached objects freed when it is released.
162 struct platform_device *platform_device_alloc(const char *name, int id)
164 struct platform_object *pa;
166 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
168 strcpy(pa->name, name);
169 pa->pdev.name = pa->name;
171 device_initialize(&pa->pdev.dev);
172 pa->pdev.dev.release = platform_device_release;
175 return pa ? &pa->pdev : NULL;
177 EXPORT_SYMBOL_GPL(platform_device_alloc);
180 * platform_device_add_resources - add resources to a platform device
181 * @pdev: platform device allocated by platform_device_alloc to add resources to
182 * @res: set of resources that needs to be allocated for the device
183 * @num: number of resources
185 * Add a copy of the resources to the platform device. The memory
186 * associated with the resources will be freed when the platform device is
189 int platform_device_add_resources(struct platform_device *pdev,
190 const struct resource *res, unsigned int num)
194 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
197 pdev->num_resources = num;
202 EXPORT_SYMBOL_GPL(platform_device_add_resources);
205 * platform_device_add_data - add platform-specific data to a platform device
206 * @pdev: platform device allocated by platform_device_alloc to add resources to
207 * @data: platform specific data for this platform device
208 * @size: size of platform specific data
210 * Add a copy of platform specific data to the platform device's
211 * platform_data pointer. The memory associated with the platform data
212 * will be freed when the platform device is released.
214 int platform_device_add_data(struct platform_device *pdev, const void *data,
217 void *d = kmemdup(data, size, GFP_KERNEL);
220 pdev->dev.platform_data = d;
225 EXPORT_SYMBOL_GPL(platform_device_add_data);
228 * platform_device_add - add a platform device to device hierarchy
229 * @pdev: platform device we're adding
231 * This is part 2 of platform_device_register(), though may be called
232 * separately _iff_ pdev was allocated by platform_device_alloc().
234 int platform_device_add(struct platform_device *pdev)
241 if (!pdev->dev.parent)
242 pdev->dev.parent = &platform_bus;
244 pdev->dev.bus = &platform_bus_type;
247 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
249 dev_set_name(&pdev->dev, "%s", pdev->name);
251 for (i = 0; i < pdev->num_resources; i++) {
252 struct resource *p, *r = &pdev->resource[i];
255 r->name = dev_name(&pdev->dev);
259 if (resource_type(r) == IORESOURCE_MEM)
261 else if (resource_type(r) == IORESOURCE_IO)
262 p = &ioport_resource;
265 if (p && insert_resource(p, r)) {
267 "%s: failed to claim resource %d\n",
268 dev_name(&pdev->dev), i);
274 pr_debug("Registering platform device '%s'. Parent at %s\n",
275 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
277 ret = device_add(&pdev->dev);
283 struct resource *r = &pdev->resource[i];
284 unsigned long type = resource_type(r);
286 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
292 EXPORT_SYMBOL_GPL(platform_device_add);
295 * platform_device_del - remove a platform-level device
296 * @pdev: platform device we're removing
298 * Note that this function will also release all memory- and port-based
299 * resources owned by the device (@dev->resource). This function must
300 * _only_ be externally called in error cases. All other usage is a bug.
302 void platform_device_del(struct platform_device *pdev)
307 device_del(&pdev->dev);
309 for (i = 0; i < pdev->num_resources; i++) {
310 struct resource *r = &pdev->resource[i];
311 unsigned long type = resource_type(r);
313 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
318 EXPORT_SYMBOL_GPL(platform_device_del);
321 * platform_device_register - add a platform-level device
322 * @pdev: platform device we're adding
324 int platform_device_register(struct platform_device *pdev)
326 device_initialize(&pdev->dev);
327 return platform_device_add(pdev);
329 EXPORT_SYMBOL_GPL(platform_device_register);
332 * platform_device_unregister - unregister a platform-level device
333 * @pdev: platform device we're unregistering
335 * Unregistration is done in 2 steps. First we release all resources
336 * and remove it from the subsystem, then we drop reference count by
337 * calling platform_device_put().
339 void platform_device_unregister(struct platform_device *pdev)
341 platform_device_del(pdev);
342 platform_device_put(pdev);
344 EXPORT_SYMBOL_GPL(platform_device_unregister);
347 * platform_device_register_resndata - add a platform-level device with
348 * resources and platform-specific data
350 * @parent: parent device for the device we're adding
351 * @name: base name of the device we're adding
353 * @res: set of resources that needs to be allocated for the device
354 * @num: number of resources
355 * @data: platform specific data for this platform device
356 * @size: size of platform specific data
358 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
360 struct platform_device *platform_device_register_resndata(
361 struct device *parent,
362 const char *name, int id,
363 const struct resource *res, unsigned int num,
364 const void *data, size_t size)
367 struct platform_device *pdev;
369 pdev = platform_device_alloc(name, id);
373 pdev->dev.parent = parent;
376 ret = platform_device_add_resources(pdev, res, num);
382 ret = platform_device_add_data(pdev, data, size);
387 ret = platform_device_add(pdev);
390 platform_device_put(pdev);
396 EXPORT_SYMBOL_GPL(platform_device_register_resndata);
398 static int platform_drv_probe(struct device *_dev)
400 struct platform_driver *drv = to_platform_driver(_dev->driver);
401 struct platform_device *dev = to_platform_device(_dev);
403 return drv->probe(dev);
406 static int platform_drv_probe_fail(struct device *_dev)
411 static int platform_drv_remove(struct device *_dev)
413 struct platform_driver *drv = to_platform_driver(_dev->driver);
414 struct platform_device *dev = to_platform_device(_dev);
416 return drv->remove(dev);
419 static void platform_drv_shutdown(struct device *_dev)
421 struct platform_driver *drv = to_platform_driver(_dev->driver);
422 struct platform_device *dev = to_platform_device(_dev);
428 * platform_driver_register - register a driver for platform-level devices
429 * @drv: platform driver structure
431 int platform_driver_register(struct platform_driver *drv)
433 drv->driver.bus = &platform_bus_type;
435 drv->driver.probe = platform_drv_probe;
437 drv->driver.remove = platform_drv_remove;
439 drv->driver.shutdown = platform_drv_shutdown;
441 return driver_register(&drv->driver);
443 EXPORT_SYMBOL_GPL(platform_driver_register);
446 * platform_driver_unregister - unregister a driver for platform-level devices
447 * @drv: platform driver structure
449 void platform_driver_unregister(struct platform_driver *drv)
451 driver_unregister(&drv->driver);
453 EXPORT_SYMBOL_GPL(platform_driver_unregister);
456 * platform_driver_probe - register driver for non-hotpluggable device
457 * @drv: platform driver structure
458 * @probe: the driver probe routine, probably from an __init section
460 * Use this instead of platform_driver_register() when you know the device
461 * is not hotpluggable and has already been registered, and you want to
462 * remove its run-once probe() infrastructure from memory after the driver
463 * has bound to the device.
465 * One typical use for this would be with drivers for controllers integrated
466 * into system-on-chip processors, where the controller devices have been
467 * configured as part of board setup.
469 * Returns zero if the driver registered and bound to a device, else returns
470 * a negative error code and with the driver not registered.
472 int __init_or_module platform_driver_probe(struct platform_driver *drv,
473 int (*probe)(struct platform_device *))
477 /* make sure driver won't have bind/unbind attributes */
478 drv->driver.suppress_bind_attrs = true;
480 /* temporary section violation during probe() */
482 retval = code = platform_driver_register(drv);
485 * Fixup that section violation, being paranoid about code scanning
486 * the list of drivers in order to probe new devices. Check to see
487 * if the probe was successful, and make sure any forced probes of
490 spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
492 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
494 drv->driver.probe = platform_drv_probe_fail;
495 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
498 platform_driver_unregister(drv);
501 EXPORT_SYMBOL_GPL(platform_driver_probe);
504 * platform_create_bundle - register driver and create corresponding device
505 * @driver: platform driver structure
506 * @probe: the driver probe routine, probably from an __init section
507 * @res: set of resources that needs to be allocated for the device
508 * @n_res: number of resources
509 * @data: platform specific data for this platform device
510 * @size: size of platform specific data
512 * Use this in legacy-style modules that probe hardware directly and
513 * register a single platform device and corresponding platform driver.
515 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
517 struct platform_device * __init_or_module platform_create_bundle(
518 struct platform_driver *driver,
519 int (*probe)(struct platform_device *),
520 struct resource *res, unsigned int n_res,
521 const void *data, size_t size)
523 struct platform_device *pdev;
526 pdev = platform_device_alloc(driver->driver.name, -1);
533 error = platform_device_add_resources(pdev, res, n_res);
539 error = platform_device_add_data(pdev, data, size);
544 error = platform_device_add(pdev);
548 error = platform_driver_probe(driver, probe);
555 platform_device_del(pdev);
557 platform_device_put(pdev);
559 return ERR_PTR(error);
561 EXPORT_SYMBOL_GPL(platform_create_bundle);
563 /* modalias support enables more hands-off userspace setup:
564 * (a) environment variable lets new-style hotplug events work once system is
565 * fully running: "modprobe $MODALIAS"
566 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
567 * mishandled before system is fully running: "modprobe $(cat modalias)"
569 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
572 struct platform_device *pdev = to_platform_device(dev);
573 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
575 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
578 static struct device_attribute platform_dev_attrs[] = {
583 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
585 struct platform_device *pdev = to_platform_device(dev);
587 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
588 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
592 static const struct platform_device_id *platform_match_id(
593 const struct platform_device_id *id,
594 struct platform_device *pdev)
596 while (id->name[0]) {
597 if (strcmp(pdev->name, id->name) == 0) {
607 * platform_match - bind platform device to platform driver.
611 * Platform device IDs are assumed to be encoded like this:
612 * "<name><instance>", where <name> is a short description of the type of
613 * device, like "pci" or "floppy", and <instance> is the enumerated
614 * instance of the device, like '0' or '42'. Driver IDs are simply
615 * "<name>". So, extract the <name> from the platform_device structure,
616 * and compare it against the name of the driver. Return whether they match
619 static int platform_match(struct device *dev, struct device_driver *drv)
621 struct platform_device *pdev = to_platform_device(dev);
622 struct platform_driver *pdrv = to_platform_driver(drv);
624 /* match against the id table first */
626 return platform_match_id(pdrv->id_table, pdev) != NULL;
628 /* fall-back to driver name match */
629 return (strcmp(pdev->name, drv->name) == 0);
632 #ifdef CONFIG_PM_SLEEP
634 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
636 struct platform_driver *pdrv = to_platform_driver(dev->driver);
637 struct platform_device *pdev = to_platform_device(dev);
640 if (dev->driver && pdrv->suspend)
641 ret = pdrv->suspend(pdev, mesg);
646 static int platform_legacy_resume(struct device *dev)
648 struct platform_driver *pdrv = to_platform_driver(dev->driver);
649 struct platform_device *pdev = to_platform_device(dev);
652 if (dev->driver && pdrv->resume)
653 ret = pdrv->resume(pdev);
658 static int platform_pm_prepare(struct device *dev)
660 struct device_driver *drv = dev->driver;
663 if (drv && drv->pm && drv->pm->prepare)
664 ret = drv->pm->prepare(dev);
669 static void platform_pm_complete(struct device *dev)
671 struct device_driver *drv = dev->driver;
673 if (drv && drv->pm && drv->pm->complete)
674 drv->pm->complete(dev);
677 #else /* !CONFIG_PM_SLEEP */
679 #define platform_pm_prepare NULL
680 #define platform_pm_complete NULL
682 #endif /* !CONFIG_PM_SLEEP */
684 #ifdef CONFIG_SUSPEND
686 int __weak platform_pm_suspend(struct device *dev)
688 struct device_driver *drv = dev->driver;
695 if (drv->pm->suspend)
696 ret = drv->pm->suspend(dev);
698 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
704 int __weak platform_pm_suspend_noirq(struct device *dev)
706 struct device_driver *drv = dev->driver;
713 if (drv->pm->suspend_noirq)
714 ret = drv->pm->suspend_noirq(dev);
720 int __weak platform_pm_resume(struct device *dev)
722 struct device_driver *drv = dev->driver;
730 ret = drv->pm->resume(dev);
732 ret = platform_legacy_resume(dev);
738 int __weak platform_pm_resume_noirq(struct device *dev)
740 struct device_driver *drv = dev->driver;
747 if (drv->pm->resume_noirq)
748 ret = drv->pm->resume_noirq(dev);
754 #else /* !CONFIG_SUSPEND */
756 #define platform_pm_suspend NULL
757 #define platform_pm_resume NULL
758 #define platform_pm_suspend_noirq NULL
759 #define platform_pm_resume_noirq NULL
761 #endif /* !CONFIG_SUSPEND */
763 #ifdef CONFIG_HIBERNATION
765 static int platform_pm_freeze(struct device *dev)
767 struct device_driver *drv = dev->driver;
775 ret = drv->pm->freeze(dev);
777 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
783 static int platform_pm_freeze_noirq(struct device *dev)
785 struct device_driver *drv = dev->driver;
792 if (drv->pm->freeze_noirq)
793 ret = drv->pm->freeze_noirq(dev);
799 static int platform_pm_thaw(struct device *dev)
801 struct device_driver *drv = dev->driver;
809 ret = drv->pm->thaw(dev);
811 ret = platform_legacy_resume(dev);
817 static int platform_pm_thaw_noirq(struct device *dev)
819 struct device_driver *drv = dev->driver;
826 if (drv->pm->thaw_noirq)
827 ret = drv->pm->thaw_noirq(dev);
833 static int platform_pm_poweroff(struct device *dev)
835 struct device_driver *drv = dev->driver;
842 if (drv->pm->poweroff)
843 ret = drv->pm->poweroff(dev);
845 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
851 static int platform_pm_poweroff_noirq(struct device *dev)
853 struct device_driver *drv = dev->driver;
860 if (drv->pm->poweroff_noirq)
861 ret = drv->pm->poweroff_noirq(dev);
867 static int platform_pm_restore(struct device *dev)
869 struct device_driver *drv = dev->driver;
876 if (drv->pm->restore)
877 ret = drv->pm->restore(dev);
879 ret = platform_legacy_resume(dev);
885 static int platform_pm_restore_noirq(struct device *dev)
887 struct device_driver *drv = dev->driver;
894 if (drv->pm->restore_noirq)
895 ret = drv->pm->restore_noirq(dev);
901 #else /* !CONFIG_HIBERNATION */
903 #define platform_pm_freeze NULL
904 #define platform_pm_thaw NULL
905 #define platform_pm_poweroff NULL
906 #define platform_pm_restore NULL
907 #define platform_pm_freeze_noirq NULL
908 #define platform_pm_thaw_noirq NULL
909 #define platform_pm_poweroff_noirq NULL
910 #define platform_pm_restore_noirq NULL
912 #endif /* !CONFIG_HIBERNATION */
914 #ifdef CONFIG_PM_RUNTIME
916 int __weak platform_pm_runtime_suspend(struct device *dev)
918 return pm_generic_runtime_suspend(dev);
921 int __weak platform_pm_runtime_resume(struct device *dev)
923 return pm_generic_runtime_resume(dev);
926 int __weak platform_pm_runtime_idle(struct device *dev)
928 return pm_generic_runtime_idle(dev);
931 #else /* !CONFIG_PM_RUNTIME */
933 #define platform_pm_runtime_suspend NULL
934 #define platform_pm_runtime_resume NULL
935 #define platform_pm_runtime_idle NULL
937 #endif /* !CONFIG_PM_RUNTIME */
939 static const struct dev_pm_ops platform_dev_pm_ops = {
940 .prepare = platform_pm_prepare,
941 .complete = platform_pm_complete,
942 .suspend = platform_pm_suspend,
943 .resume = platform_pm_resume,
944 .freeze = platform_pm_freeze,
945 .thaw = platform_pm_thaw,
946 .poweroff = platform_pm_poweroff,
947 .restore = platform_pm_restore,
948 .suspend_noirq = platform_pm_suspend_noirq,
949 .resume_noirq = platform_pm_resume_noirq,
950 .freeze_noirq = platform_pm_freeze_noirq,
951 .thaw_noirq = platform_pm_thaw_noirq,
952 .poweroff_noirq = platform_pm_poweroff_noirq,
953 .restore_noirq = platform_pm_restore_noirq,
954 .runtime_suspend = platform_pm_runtime_suspend,
955 .runtime_resume = platform_pm_runtime_resume,
956 .runtime_idle = platform_pm_runtime_idle,
959 struct bus_type platform_bus_type = {
961 .dev_attrs = platform_dev_attrs,
962 .match = platform_match,
963 .uevent = platform_uevent,
964 .pm = &platform_dev_pm_ops,
966 EXPORT_SYMBOL_GPL(platform_bus_type);
968 int __init platform_bus_init(void)
972 early_platform_cleanup();
974 error = device_register(&platform_bus);
977 error = bus_register(&platform_bus_type);
979 device_unregister(&platform_bus);
983 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
984 u64 dma_get_required_mask(struct device *dev)
986 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
987 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
990 if (!high_totalram) {
991 /* convert to mask just covering totalram */
992 low_totalram = (1 << (fls(low_totalram) - 1));
993 low_totalram += low_totalram - 1;
996 high_totalram = (1 << (fls(high_totalram) - 1));
997 high_totalram += high_totalram - 1;
998 mask = (((u64)high_totalram) << 32) + 0xffffffff;
1002 EXPORT_SYMBOL_GPL(dma_get_required_mask);
1005 static __initdata LIST_HEAD(early_platform_driver_list);
1006 static __initdata LIST_HEAD(early_platform_device_list);
1009 * early_platform_driver_register - register early platform driver
1010 * @epdrv: early_platform driver structure
1011 * @buf: string passed from early_param()
1013 * Helper function for early_platform_init() / early_platform_init_buffer()
1015 int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1021 /* Simply add the driver to the end of the global list.
1022 * Drivers will by default be put on the list in compiled-in order.
1024 if (!epdrv->list.next) {
1025 INIT_LIST_HEAD(&epdrv->list);
1026 list_add_tail(&epdrv->list, &early_platform_driver_list);
1029 /* If the user has specified device then make sure the driver
1030 * gets prioritized. The driver of the last device specified on
1031 * command line will be put first on the list.
1033 n = strlen(epdrv->pdrv->driver.name);
1034 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1035 list_move(&epdrv->list, &early_platform_driver_list);
1037 /* Allow passing parameters after device name */
1038 if (buf[n] == '\0' || buf[n] == ',')
1039 epdrv->requested_id = -1;
1041 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1044 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1045 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1048 n += strcspn(&buf[n + 1], ",") + 1;
1054 if (epdrv->bufsize) {
1055 memcpy(epdrv->buffer, &buf[n],
1056 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1057 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1065 * early_platform_add_devices - adds a number of early platform devices
1066 * @devs: array of early platform devices to add
1067 * @num: number of early platform devices in array
1069 * Used by early architecture code to register early platform devices and
1070 * their platform data.
1072 void __init early_platform_add_devices(struct platform_device **devs, int num)
1077 /* simply add the devices to list */
1078 for (i = 0; i < num; i++) {
1079 dev = &devs[i]->dev;
1081 if (!dev->devres_head.next) {
1082 INIT_LIST_HEAD(&dev->devres_head);
1083 list_add_tail(&dev->devres_head,
1084 &early_platform_device_list);
1090 * early_platform_driver_register_all - register early platform drivers
1091 * @class_str: string to identify early platform driver class
1093 * Used by architecture code to register all early platform drivers
1094 * for a certain class. If omitted then only early platform drivers
1095 * with matching kernel command line class parameters will be registered.
1097 void __init early_platform_driver_register_all(char *class_str)
1099 /* The "class_str" parameter may or may not be present on the kernel
1100 * command line. If it is present then there may be more than one
1101 * matching parameter.
1103 * Since we register our early platform drivers using early_param()
1104 * we need to make sure that they also get registered in the case
1105 * when the parameter is missing from the kernel command line.
1107 * We use parse_early_options() to make sure the early_param() gets
1108 * called at least once. The early_param() may be called more than
1109 * once since the name of the preferred device may be specified on
1110 * the kernel command line. early_platform_driver_register() handles
1113 parse_early_options(class_str);
1117 * early_platform_match - find early platform device matching driver
1118 * @epdrv: early platform driver structure
1119 * @id: id to match against
1121 static __init struct platform_device *
1122 early_platform_match(struct early_platform_driver *epdrv, int id)
1124 struct platform_device *pd;
1126 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1127 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1135 * early_platform_left - check if early platform driver has matching devices
1136 * @epdrv: early platform driver structure
1137 * @id: return true if id or above exists
1139 static __init int early_platform_left(struct early_platform_driver *epdrv,
1142 struct platform_device *pd;
1144 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1145 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1153 * early_platform_driver_probe_id - probe drivers matching class_str and id
1154 * @class_str: string to identify early platform driver class
1155 * @id: id to match against
1156 * @nr_probe: number of platform devices to successfully probe before exiting
1158 static int __init early_platform_driver_probe_id(char *class_str,
1162 struct early_platform_driver *epdrv;
1163 struct platform_device *match;
1168 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1169 /* only use drivers matching our class_str */
1170 if (strcmp(class_str, epdrv->class_str))
1174 match_id = epdrv->requested_id;
1179 left += early_platform_left(epdrv, id);
1181 /* skip requested id */
1182 switch (epdrv->requested_id) {
1183 case EARLY_PLATFORM_ID_ERROR:
1184 case EARLY_PLATFORM_ID_UNSET:
1187 if (epdrv->requested_id == id)
1188 match_id = EARLY_PLATFORM_ID_UNSET;
1193 case EARLY_PLATFORM_ID_ERROR:
1194 pr_warning("%s: unable to parse %s parameter\n",
1195 class_str, epdrv->pdrv->driver.name);
1197 case EARLY_PLATFORM_ID_UNSET:
1201 match = early_platform_match(epdrv, match_id);
1206 * Set up a sensible init_name to enable
1207 * dev_name() and others to be used before the
1208 * rest of the driver core is initialized.
1210 if (!match->dev.init_name && slab_is_available()) {
1211 if (match->id != -1)
1212 match->dev.init_name =
1213 kasprintf(GFP_KERNEL, "%s.%d",
1217 match->dev.init_name =
1218 kasprintf(GFP_KERNEL, "%s",
1221 if (!match->dev.init_name)
1225 if (epdrv->pdrv->probe(match))
1226 pr_warning("%s: unable to probe %s early.\n",
1227 class_str, match->name);
1243 * early_platform_driver_probe - probe a class of registered drivers
1244 * @class_str: string to identify early platform driver class
1245 * @nr_probe: number of platform devices to successfully probe before exiting
1246 * @user_only: only probe user specified early platform devices
1248 * Used by architecture code to probe registered early platform drivers
1249 * within a certain class. For probe to happen a registered early platform
1250 * device matching a registered early platform driver is needed.
1252 int __init early_platform_driver_probe(char *class_str,
1259 for (i = -2; n < nr_probe; i++) {
1260 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1275 * early_platform_cleanup - clean up early platform code
1277 void __init early_platform_cleanup(void)
1279 struct platform_device *pd, *pd2;
1281 /* clean up the devres list used to chain devices */
1282 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1284 list_del(&pd->dev.devres_head);
1285 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));