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>
23 #include <linux/idr.h>
26 #include "power/power.h"
28 /* For automatically allocated device IDs */
29 static DEFINE_IDA(platform_devid_ida
);
31 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
34 struct device platform_bus
= {
35 .init_name
= "platform",
37 EXPORT_SYMBOL_GPL(platform_bus
);
40 * arch_setup_pdev_archdata - Allow manipulation of archdata before its used
41 * @pdev: platform device
43 * This is called before platform_device_add() such that any pdev_archdata may
44 * be setup before the platform_notifier is called. So if a user needs to
45 * manipulate any relevant information in the pdev_archdata they can do:
47 * platform_device_alloc()
49 * platform_device_add()
51 * And if they don't care they can just call platform_device_register() and
52 * everything will just work out.
54 void __weak
arch_setup_pdev_archdata(struct platform_device
*pdev
)
59 * platform_get_resource - get a resource for a device
60 * @dev: platform device
61 * @type: resource type
62 * @num: resource index
64 struct resource
*platform_get_resource(struct platform_device
*dev
,
65 unsigned int type
, unsigned int num
)
69 for (i
= 0; i
< dev
->num_resources
; i
++) {
70 struct resource
*r
= &dev
->resource
[i
];
72 if (type
== resource_type(r
) && num
-- == 0)
77 EXPORT_SYMBOL_GPL(platform_get_resource
);
80 * platform_get_irq - get an IRQ for a device
81 * @dev: platform device
82 * @num: IRQ number index
84 int platform_get_irq(struct platform_device
*dev
, unsigned int num
)
87 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
88 if (!dev
|| num
>= dev
->archdata
.num_irqs
)
90 return dev
->archdata
.irqs
[num
];
92 struct resource
*r
= platform_get_resource(dev
, IORESOURCE_IRQ
, num
);
94 return r
? r
->start
: -ENXIO
;
97 EXPORT_SYMBOL_GPL(platform_get_irq
);
100 * platform_get_resource_byname - get a resource for a device by name
101 * @dev: platform device
102 * @type: resource type
103 * @name: resource name
105 struct resource
*platform_get_resource_byname(struct platform_device
*dev
,
111 for (i
= 0; i
< dev
->num_resources
; i
++) {
112 struct resource
*r
= &dev
->resource
[i
];
114 if (unlikely(!r
->name
))
117 if (type
== resource_type(r
) && !strcmp(r
->name
, name
))
122 EXPORT_SYMBOL_GPL(platform_get_resource_byname
);
125 * platform_get_irq - get an IRQ for a device
126 * @dev: platform device
129 int platform_get_irq_byname(struct platform_device
*dev
, const char *name
)
131 struct resource
*r
= platform_get_resource_byname(dev
, IORESOURCE_IRQ
,
134 return r
? r
->start
: -ENXIO
;
136 EXPORT_SYMBOL_GPL(platform_get_irq_byname
);
139 * platform_add_devices - add a numbers of platform devices
140 * @devs: array of platform devices to add
141 * @num: number of platform devices in array
143 int platform_add_devices(struct platform_device
**devs
, int num
)
147 for (i
= 0; i
< num
; i
++) {
148 ret
= platform_device_register(devs
[i
]);
151 platform_device_unregister(devs
[i
]);
158 EXPORT_SYMBOL_GPL(platform_add_devices
);
160 struct platform_object
{
161 struct platform_device pdev
;
166 * platform_device_put - destroy a platform device
167 * @pdev: platform device to free
169 * Free all memory associated with a platform device. This function must
170 * _only_ be externally called in error cases. All other usage is a bug.
172 void platform_device_put(struct platform_device
*pdev
)
175 put_device(&pdev
->dev
);
177 EXPORT_SYMBOL_GPL(platform_device_put
);
179 static void platform_device_release(struct device
*dev
)
181 struct platform_object
*pa
= container_of(dev
, struct platform_object
,
184 of_device_node_put(&pa
->pdev
.dev
);
185 kfree(pa
->pdev
.dev
.platform_data
);
186 kfree(pa
->pdev
.mfd_cell
);
187 kfree(pa
->pdev
.resource
);
192 * platform_device_alloc - create a platform device
193 * @name: base name of the device we're adding
196 * Create a platform device object which can have other objects attached
197 * to it, and which will have attached objects freed when it is released.
199 struct platform_device
*platform_device_alloc(const char *name
, int id
)
201 struct platform_object
*pa
;
203 pa
= kzalloc(sizeof(struct platform_object
) + strlen(name
), GFP_KERNEL
);
205 strcpy(pa
->name
, name
);
206 pa
->pdev
.name
= pa
->name
;
208 device_initialize(&pa
->pdev
.dev
);
209 pa
->pdev
.dev
.release
= platform_device_release
;
210 arch_setup_pdev_archdata(&pa
->pdev
);
213 return pa
? &pa
->pdev
: NULL
;
215 EXPORT_SYMBOL_GPL(platform_device_alloc
);
218 * platform_device_add_resources - add resources to a platform device
219 * @pdev: platform device allocated by platform_device_alloc to add resources to
220 * @res: set of resources that needs to be allocated for the device
221 * @num: number of resources
223 * Add a copy of the resources to the platform device. The memory
224 * associated with the resources will be freed when the platform device is
227 int platform_device_add_resources(struct platform_device
*pdev
,
228 const struct resource
*res
, unsigned int num
)
230 struct resource
*r
= NULL
;
233 r
= kmemdup(res
, sizeof(struct resource
) * num
, GFP_KERNEL
);
238 kfree(pdev
->resource
);
240 pdev
->num_resources
= num
;
243 EXPORT_SYMBOL_GPL(platform_device_add_resources
);
246 * platform_device_add_data - add platform-specific data to a platform device
247 * @pdev: platform device allocated by platform_device_alloc to add resources to
248 * @data: platform specific data for this platform device
249 * @size: size of platform specific data
251 * Add a copy of platform specific data to the platform device's
252 * platform_data pointer. The memory associated with the platform data
253 * will be freed when the platform device is released.
255 int platform_device_add_data(struct platform_device
*pdev
, const void *data
,
261 d
= kmemdup(data
, size
, GFP_KERNEL
);
266 kfree(pdev
->dev
.platform_data
);
267 pdev
->dev
.platform_data
= d
;
270 EXPORT_SYMBOL_GPL(platform_device_add_data
);
273 * platform_device_add - add a platform device to device hierarchy
274 * @pdev: platform device we're adding
276 * This is part 2 of platform_device_register(), though may be called
277 * separately _iff_ pdev was allocated by platform_device_alloc().
279 int platform_device_add(struct platform_device
*pdev
)
286 if (!pdev
->dev
.parent
)
287 pdev
->dev
.parent
= &platform_bus
;
289 pdev
->dev
.bus
= &platform_bus_type
;
293 dev_set_name(&pdev
->dev
, "%s.%d", pdev
->name
, pdev
->id
);
295 case PLATFORM_DEVID_NONE
:
296 dev_set_name(&pdev
->dev
, "%s", pdev
->name
);
298 case PLATFORM_DEVID_AUTO
:
300 * Automatically allocated device ID. We mark it as such so
301 * that we remember it must be freed, and we append a suffix
302 * to avoid namespace collision with explicit IDs.
304 ret
= ida_simple_get(&platform_devid_ida
, 0, 0, GFP_KERNEL
);
308 pdev
->id_auto
= true;
309 dev_set_name(&pdev
->dev
, "%s.%d.auto", pdev
->name
, pdev
->id
);
313 for (i
= 0; i
< pdev
->num_resources
; i
++) {
314 struct resource
*p
, *r
= &pdev
->resource
[i
];
317 r
->name
= dev_name(&pdev
->dev
);
321 if (resource_type(r
) == IORESOURCE_MEM
)
323 else if (resource_type(r
) == IORESOURCE_IO
)
324 p
= &ioport_resource
;
327 if (p
&& insert_resource(p
, r
)) {
329 "%s: failed to claim resource %d\n",
330 dev_name(&pdev
->dev
), i
);
336 pr_debug("Registering platform device '%s'. Parent at %s\n",
337 dev_name(&pdev
->dev
), dev_name(pdev
->dev
.parent
));
339 ret
= device_add(&pdev
->dev
);
345 ida_simple_remove(&platform_devid_ida
, pdev
->id
);
346 pdev
->id
= PLATFORM_DEVID_AUTO
;
350 struct resource
*r
= &pdev
->resource
[i
];
351 unsigned long type
= resource_type(r
);
353 if (type
== IORESOURCE_MEM
|| type
== IORESOURCE_IO
)
360 EXPORT_SYMBOL_GPL(platform_device_add
);
363 * platform_device_del - remove a platform-level device
364 * @pdev: platform device we're removing
366 * Note that this function will also release all memory- and port-based
367 * resources owned by the device (@dev->resource). This function must
368 * _only_ be externally called in error cases. All other usage is a bug.
370 void platform_device_del(struct platform_device
*pdev
)
375 device_del(&pdev
->dev
);
378 ida_simple_remove(&platform_devid_ida
, pdev
->id
);
379 pdev
->id
= PLATFORM_DEVID_AUTO
;
382 for (i
= 0; i
< pdev
->num_resources
; i
++) {
383 struct resource
*r
= &pdev
->resource
[i
];
384 unsigned long type
= resource_type(r
);
386 if (type
== IORESOURCE_MEM
|| type
== IORESOURCE_IO
)
391 EXPORT_SYMBOL_GPL(platform_device_del
);
394 * platform_device_register - add a platform-level device
395 * @pdev: platform device we're adding
397 int platform_device_register(struct platform_device
*pdev
)
399 device_initialize(&pdev
->dev
);
400 arch_setup_pdev_archdata(pdev
);
401 return platform_device_add(pdev
);
403 EXPORT_SYMBOL_GPL(platform_device_register
);
406 * platform_device_unregister - unregister a platform-level device
407 * @pdev: platform device we're unregistering
409 * Unregistration is done in 2 steps. First we release all resources
410 * and remove it from the subsystem, then we drop reference count by
411 * calling platform_device_put().
413 void platform_device_unregister(struct platform_device
*pdev
)
415 platform_device_del(pdev
);
416 platform_device_put(pdev
);
418 EXPORT_SYMBOL_GPL(platform_device_unregister
);
421 * platform_device_register_full - add a platform-level device with
422 * resources and platform-specific data
424 * @pdevinfo: data used to create device
426 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
428 struct platform_device
*platform_device_register_full(
429 const struct platform_device_info
*pdevinfo
)
432 struct platform_device
*pdev
;
434 pdev
= platform_device_alloc(pdevinfo
->name
, pdevinfo
->id
);
438 pdev
->dev
.parent
= pdevinfo
->parent
;
440 if (pdevinfo
->dma_mask
) {
442 * This memory isn't freed when the device is put,
443 * I don't have a nice idea for that though. Conceptually
444 * dma_mask in struct device should not be a pointer.
445 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
448 kmalloc(sizeof(*pdev
->dev
.dma_mask
), GFP_KERNEL
);
449 if (!pdev
->dev
.dma_mask
)
452 *pdev
->dev
.dma_mask
= pdevinfo
->dma_mask
;
453 pdev
->dev
.coherent_dma_mask
= pdevinfo
->dma_mask
;
456 ret
= platform_device_add_resources(pdev
,
457 pdevinfo
->res
, pdevinfo
->num_res
);
461 ret
= platform_device_add_data(pdev
,
462 pdevinfo
->data
, pdevinfo
->size_data
);
466 ret
= platform_device_add(pdev
);
469 kfree(pdev
->dev
.dma_mask
);
472 platform_device_put(pdev
);
478 EXPORT_SYMBOL_GPL(platform_device_register_full
);
480 static int platform_drv_probe(struct device
*_dev
)
482 struct platform_driver
*drv
= to_platform_driver(_dev
->driver
);
483 struct platform_device
*dev
= to_platform_device(_dev
);
485 return drv
->probe(dev
);
488 static int platform_drv_probe_fail(struct device
*_dev
)
493 static int platform_drv_remove(struct device
*_dev
)
495 struct platform_driver
*drv
= to_platform_driver(_dev
->driver
);
496 struct platform_device
*dev
= to_platform_device(_dev
);
498 return drv
->remove(dev
);
501 static void platform_drv_shutdown(struct device
*_dev
)
503 struct platform_driver
*drv
= to_platform_driver(_dev
->driver
);
504 struct platform_device
*dev
= to_platform_device(_dev
);
510 * platform_driver_register - register a driver for platform-level devices
511 * @drv: platform driver structure
513 int platform_driver_register(struct platform_driver
*drv
)
515 drv
->driver
.bus
= &platform_bus_type
;
517 drv
->driver
.probe
= platform_drv_probe
;
519 drv
->driver
.remove
= platform_drv_remove
;
521 drv
->driver
.shutdown
= platform_drv_shutdown
;
523 return driver_register(&drv
->driver
);
525 EXPORT_SYMBOL_GPL(platform_driver_register
);
528 * platform_driver_unregister - unregister a driver for platform-level devices
529 * @drv: platform driver structure
531 void platform_driver_unregister(struct platform_driver
*drv
)
533 driver_unregister(&drv
->driver
);
535 EXPORT_SYMBOL_GPL(platform_driver_unregister
);
538 * platform_driver_probe - register driver for non-hotpluggable device
539 * @drv: platform driver structure
540 * @probe: the driver probe routine, probably from an __init section
542 * Use this instead of platform_driver_register() when you know the device
543 * is not hotpluggable and has already been registered, and you want to
544 * remove its run-once probe() infrastructure from memory after the driver
545 * has bound to the device.
547 * One typical use for this would be with drivers for controllers integrated
548 * into system-on-chip processors, where the controller devices have been
549 * configured as part of board setup.
551 * Returns zero if the driver registered and bound to a device, else returns
552 * a negative error code and with the driver not registered.
554 int __init_or_module
platform_driver_probe(struct platform_driver
*drv
,
555 int (*probe
)(struct platform_device
*))
559 /* make sure driver won't have bind/unbind attributes */
560 drv
->driver
.suppress_bind_attrs
= true;
562 /* temporary section violation during probe() */
564 retval
= code
= platform_driver_register(drv
);
567 * Fixup that section violation, being paranoid about code scanning
568 * the list of drivers in order to probe new devices. Check to see
569 * if the probe was successful, and make sure any forced probes of
572 spin_lock(&drv
->driver
.bus
->p
->klist_drivers
.k_lock
);
574 if (code
== 0 && list_empty(&drv
->driver
.p
->klist_devices
.k_list
))
576 drv
->driver
.probe
= platform_drv_probe_fail
;
577 spin_unlock(&drv
->driver
.bus
->p
->klist_drivers
.k_lock
);
580 platform_driver_unregister(drv
);
583 EXPORT_SYMBOL_GPL(platform_driver_probe
);
586 * platform_create_bundle - register driver and create corresponding device
587 * @driver: platform driver structure
588 * @probe: the driver probe routine, probably from an __init section
589 * @res: set of resources that needs to be allocated for the device
590 * @n_res: number of resources
591 * @data: platform specific data for this platform device
592 * @size: size of platform specific data
594 * Use this in legacy-style modules that probe hardware directly and
595 * register a single platform device and corresponding platform driver.
597 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
599 struct platform_device
* __init_or_module
platform_create_bundle(
600 struct platform_driver
*driver
,
601 int (*probe
)(struct platform_device
*),
602 struct resource
*res
, unsigned int n_res
,
603 const void *data
, size_t size
)
605 struct platform_device
*pdev
;
608 pdev
= platform_device_alloc(driver
->driver
.name
, -1);
614 error
= platform_device_add_resources(pdev
, res
, n_res
);
618 error
= platform_device_add_data(pdev
, data
, size
);
622 error
= platform_device_add(pdev
);
626 error
= platform_driver_probe(driver
, probe
);
633 platform_device_del(pdev
);
635 platform_device_put(pdev
);
637 return ERR_PTR(error
);
639 EXPORT_SYMBOL_GPL(platform_create_bundle
);
641 /* modalias support enables more hands-off userspace setup:
642 * (a) environment variable lets new-style hotplug events work once system is
643 * fully running: "modprobe $MODALIAS"
644 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
645 * mishandled before system is fully running: "modprobe $(cat modalias)"
647 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*a
,
650 struct platform_device
*pdev
= to_platform_device(dev
);
651 int len
= snprintf(buf
, PAGE_SIZE
, "platform:%s\n", pdev
->name
);
653 return (len
>= PAGE_SIZE
) ? (PAGE_SIZE
- 1) : len
;
656 static struct device_attribute platform_dev_attrs
[] = {
661 static int platform_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
663 struct platform_device
*pdev
= to_platform_device(dev
);
666 /* Some devices have extra OF data and an OF-style MODALIAS */
667 rc
= of_device_uevent_modalias(dev
,env
);
671 add_uevent_var(env
, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX
,
676 static const struct platform_device_id
*platform_match_id(
677 const struct platform_device_id
*id
,
678 struct platform_device
*pdev
)
680 while (id
->name
[0]) {
681 if (strcmp(pdev
->name
, id
->name
) == 0) {
691 * platform_match - bind platform device to platform driver.
695 * Platform device IDs are assumed to be encoded like this:
696 * "<name><instance>", where <name> is a short description of the type of
697 * device, like "pci" or "floppy", and <instance> is the enumerated
698 * instance of the device, like '0' or '42'. Driver IDs are simply
699 * "<name>". So, extract the <name> from the platform_device structure,
700 * and compare it against the name of the driver. Return whether they match
703 static int platform_match(struct device
*dev
, struct device_driver
*drv
)
705 struct platform_device
*pdev
= to_platform_device(dev
);
706 struct platform_driver
*pdrv
= to_platform_driver(drv
);
708 /* Attempt an OF style match first */
709 if (of_driver_match_device(dev
, drv
))
712 /* Then try to match against the id table */
714 return platform_match_id(pdrv
->id_table
, pdev
) != NULL
;
716 /* fall-back to driver name match */
717 return (strcmp(pdev
->name
, drv
->name
) == 0);
720 #ifdef CONFIG_PM_SLEEP
722 static int platform_legacy_suspend(struct device
*dev
, pm_message_t mesg
)
724 struct platform_driver
*pdrv
= to_platform_driver(dev
->driver
);
725 struct platform_device
*pdev
= to_platform_device(dev
);
728 if (dev
->driver
&& pdrv
->suspend
)
729 ret
= pdrv
->suspend(pdev
, mesg
);
734 static int platform_legacy_resume(struct device
*dev
)
736 struct platform_driver
*pdrv
= to_platform_driver(dev
->driver
);
737 struct platform_device
*pdev
= to_platform_device(dev
);
740 if (dev
->driver
&& pdrv
->resume
)
741 ret
= pdrv
->resume(pdev
);
746 #endif /* CONFIG_PM_SLEEP */
748 #ifdef CONFIG_SUSPEND
750 int platform_pm_suspend(struct device
*dev
)
752 struct device_driver
*drv
= dev
->driver
;
759 if (drv
->pm
->suspend
)
760 ret
= drv
->pm
->suspend(dev
);
762 ret
= platform_legacy_suspend(dev
, PMSG_SUSPEND
);
768 int platform_pm_resume(struct device
*dev
)
770 struct device_driver
*drv
= dev
->driver
;
778 ret
= drv
->pm
->resume(dev
);
780 ret
= platform_legacy_resume(dev
);
786 #endif /* CONFIG_SUSPEND */
788 #ifdef CONFIG_HIBERNATE_CALLBACKS
790 int platform_pm_freeze(struct device
*dev
)
792 struct device_driver
*drv
= dev
->driver
;
800 ret
= drv
->pm
->freeze(dev
);
802 ret
= platform_legacy_suspend(dev
, PMSG_FREEZE
);
808 int platform_pm_thaw(struct device
*dev
)
810 struct device_driver
*drv
= dev
->driver
;
818 ret
= drv
->pm
->thaw(dev
);
820 ret
= platform_legacy_resume(dev
);
826 int platform_pm_poweroff(struct device
*dev
)
828 struct device_driver
*drv
= dev
->driver
;
835 if (drv
->pm
->poweroff
)
836 ret
= drv
->pm
->poweroff(dev
);
838 ret
= platform_legacy_suspend(dev
, PMSG_HIBERNATE
);
844 int platform_pm_restore(struct device
*dev
)
846 struct device_driver
*drv
= dev
->driver
;
853 if (drv
->pm
->restore
)
854 ret
= drv
->pm
->restore(dev
);
856 ret
= platform_legacy_resume(dev
);
862 #endif /* CONFIG_HIBERNATE_CALLBACKS */
864 static const struct dev_pm_ops platform_dev_pm_ops
= {
865 .runtime_suspend
= pm_generic_runtime_suspend
,
866 .runtime_resume
= pm_generic_runtime_resume
,
867 .runtime_idle
= pm_generic_runtime_idle
,
868 USE_PLATFORM_PM_SLEEP_OPS
871 struct bus_type platform_bus_type
= {
873 .dev_attrs
= platform_dev_attrs
,
874 .match
= platform_match
,
875 .uevent
= platform_uevent
,
876 .pm
= &platform_dev_pm_ops
,
878 EXPORT_SYMBOL_GPL(platform_bus_type
);
880 int __init
platform_bus_init(void)
884 early_platform_cleanup();
886 error
= device_register(&platform_bus
);
889 error
= bus_register(&platform_bus_type
);
891 device_unregister(&platform_bus
);
895 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
896 u64
dma_get_required_mask(struct device
*dev
)
898 u32 low_totalram
= ((max_pfn
- 1) << PAGE_SHIFT
);
899 u32 high_totalram
= ((max_pfn
- 1) >> (32 - PAGE_SHIFT
));
902 if (!high_totalram
) {
903 /* convert to mask just covering totalram */
904 low_totalram
= (1 << (fls(low_totalram
) - 1));
905 low_totalram
+= low_totalram
- 1;
908 high_totalram
= (1 << (fls(high_totalram
) - 1));
909 high_totalram
+= high_totalram
- 1;
910 mask
= (((u64
)high_totalram
) << 32) + 0xffffffff;
914 EXPORT_SYMBOL_GPL(dma_get_required_mask
);
917 static __initdata
LIST_HEAD(early_platform_driver_list
);
918 static __initdata
LIST_HEAD(early_platform_device_list
);
921 * early_platform_driver_register - register early platform driver
922 * @epdrv: early_platform driver structure
923 * @buf: string passed from early_param()
925 * Helper function for early_platform_init() / early_platform_init_buffer()
927 int __init
early_platform_driver_register(struct early_platform_driver
*epdrv
,
933 /* Simply add the driver to the end of the global list.
934 * Drivers will by default be put on the list in compiled-in order.
936 if (!epdrv
->list
.next
) {
937 INIT_LIST_HEAD(&epdrv
->list
);
938 list_add_tail(&epdrv
->list
, &early_platform_driver_list
);
941 /* If the user has specified device then make sure the driver
942 * gets prioritized. The driver of the last device specified on
943 * command line will be put first on the list.
945 n
= strlen(epdrv
->pdrv
->driver
.name
);
946 if (buf
&& !strncmp(buf
, epdrv
->pdrv
->driver
.name
, n
)) {
947 list_move(&epdrv
->list
, &early_platform_driver_list
);
949 /* Allow passing parameters after device name */
950 if (buf
[n
] == '\0' || buf
[n
] == ',')
951 epdrv
->requested_id
= -1;
953 epdrv
->requested_id
= simple_strtoul(&buf
[n
+ 1],
956 if (buf
[n
] != '.' || (tmp
== &buf
[n
+ 1])) {
957 epdrv
->requested_id
= EARLY_PLATFORM_ID_ERROR
;
960 n
+= strcspn(&buf
[n
+ 1], ",") + 1;
966 if (epdrv
->bufsize
) {
967 memcpy(epdrv
->buffer
, &buf
[n
],
968 min_t(int, epdrv
->bufsize
, strlen(&buf
[n
]) + 1));
969 epdrv
->buffer
[epdrv
->bufsize
- 1] = '\0';
977 * early_platform_add_devices - adds a number of early platform devices
978 * @devs: array of early platform devices to add
979 * @num: number of early platform devices in array
981 * Used by early architecture code to register early platform devices and
982 * their platform data.
984 void __init
early_platform_add_devices(struct platform_device
**devs
, int num
)
989 /* simply add the devices to list */
990 for (i
= 0; i
< num
; i
++) {
993 if (!dev
->devres_head
.next
) {
994 pm_runtime_early_init(dev
);
995 INIT_LIST_HEAD(&dev
->devres_head
);
996 list_add_tail(&dev
->devres_head
,
997 &early_platform_device_list
);
1003 * early_platform_driver_register_all - register early platform drivers
1004 * @class_str: string to identify early platform driver class
1006 * Used by architecture code to register all early platform drivers
1007 * for a certain class. If omitted then only early platform drivers
1008 * with matching kernel command line class parameters will be registered.
1010 void __init
early_platform_driver_register_all(char *class_str
)
1012 /* The "class_str" parameter may or may not be present on the kernel
1013 * command line. If it is present then there may be more than one
1014 * matching parameter.
1016 * Since we register our early platform drivers using early_param()
1017 * we need to make sure that they also get registered in the case
1018 * when the parameter is missing from the kernel command line.
1020 * We use parse_early_options() to make sure the early_param() gets
1021 * called at least once. The early_param() may be called more than
1022 * once since the name of the preferred device may be specified on
1023 * the kernel command line. early_platform_driver_register() handles
1026 parse_early_options(class_str
);
1030 * early_platform_match - find early platform device matching driver
1031 * @epdrv: early platform driver structure
1032 * @id: id to match against
1034 static __init
struct platform_device
*
1035 early_platform_match(struct early_platform_driver
*epdrv
, int id
)
1037 struct platform_device
*pd
;
1039 list_for_each_entry(pd
, &early_platform_device_list
, dev
.devres_head
)
1040 if (platform_match(&pd
->dev
, &epdrv
->pdrv
->driver
))
1048 * early_platform_left - check if early platform driver has matching devices
1049 * @epdrv: early platform driver structure
1050 * @id: return true if id or above exists
1052 static __init
int early_platform_left(struct early_platform_driver
*epdrv
,
1055 struct platform_device
*pd
;
1057 list_for_each_entry(pd
, &early_platform_device_list
, dev
.devres_head
)
1058 if (platform_match(&pd
->dev
, &epdrv
->pdrv
->driver
))
1066 * early_platform_driver_probe_id - probe drivers matching class_str and id
1067 * @class_str: string to identify early platform driver class
1068 * @id: id to match against
1069 * @nr_probe: number of platform devices to successfully probe before exiting
1071 static int __init
early_platform_driver_probe_id(char *class_str
,
1075 struct early_platform_driver
*epdrv
;
1076 struct platform_device
*match
;
1081 list_for_each_entry(epdrv
, &early_platform_driver_list
, list
) {
1082 /* only use drivers matching our class_str */
1083 if (strcmp(class_str
, epdrv
->class_str
))
1087 match_id
= epdrv
->requested_id
;
1092 left
+= early_platform_left(epdrv
, id
);
1094 /* skip requested id */
1095 switch (epdrv
->requested_id
) {
1096 case EARLY_PLATFORM_ID_ERROR
:
1097 case EARLY_PLATFORM_ID_UNSET
:
1100 if (epdrv
->requested_id
== id
)
1101 match_id
= EARLY_PLATFORM_ID_UNSET
;
1106 case EARLY_PLATFORM_ID_ERROR
:
1107 pr_warning("%s: unable to parse %s parameter\n",
1108 class_str
, epdrv
->pdrv
->driver
.name
);
1110 case EARLY_PLATFORM_ID_UNSET
:
1114 match
= early_platform_match(epdrv
, match_id
);
1119 * Set up a sensible init_name to enable
1120 * dev_name() and others to be used before the
1121 * rest of the driver core is initialized.
1123 if (!match
->dev
.init_name
&& slab_is_available()) {
1124 if (match
->id
!= -1)
1125 match
->dev
.init_name
=
1126 kasprintf(GFP_KERNEL
, "%s.%d",
1130 match
->dev
.init_name
=
1131 kasprintf(GFP_KERNEL
, "%s",
1134 if (!match
->dev
.init_name
)
1138 if (epdrv
->pdrv
->probe(match
))
1139 pr_warning("%s: unable to probe %s early.\n",
1140 class_str
, match
->name
);
1156 * early_platform_driver_probe - probe a class of registered drivers
1157 * @class_str: string to identify early platform driver class
1158 * @nr_probe: number of platform devices to successfully probe before exiting
1159 * @user_only: only probe user specified early platform devices
1161 * Used by architecture code to probe registered early platform drivers
1162 * within a certain class. For probe to happen a registered early platform
1163 * device matching a registered early platform driver is needed.
1165 int __init
early_platform_driver_probe(char *class_str
,
1172 for (i
= -2; n
< nr_probe
; i
++) {
1173 k
= early_platform_driver_probe_id(class_str
, i
, nr_probe
- n
);
1188 * early_platform_cleanup - clean up early platform code
1190 void __init
early_platform_cleanup(void)
1192 struct platform_device
*pd
, *pd2
;
1194 /* clean up the devres list used to chain devices */
1195 list_for_each_entry_safe(pd
, pd2
, &early_platform_device_list
,
1197 list_del(&pd
->dev
.devres_head
);
1198 memset(&pd
->dev
.devres_head
, 0, sizeof(pd
->dev
.devres_head
));