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/platform_device.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/bootmem.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
23 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver))
25 struct device platform_bus
= {
28 EXPORT_SYMBOL_GPL(platform_bus
);
31 * platform_get_resource - get a resource for a device
32 * @dev: platform device
33 * @type: resource type
34 * @num: resource index
37 platform_get_resource(struct platform_device
*dev
, unsigned int type
,
42 for (i
= 0; i
< dev
->num_resources
; i
++) {
43 struct resource
*r
= &dev
->resource
[i
];
45 if ((r
->flags
& (IORESOURCE_IO
|IORESOURCE_MEM
|
46 IORESOURCE_IRQ
|IORESOURCE_DMA
))
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
75 platform_get_resource_byname(struct platform_device
*dev
, unsigned int type
,
80 for (i
= 0; i
< dev
->num_resources
; i
++) {
81 struct resource
*r
= &dev
->resource
[i
];
83 if ((r
->flags
& (IORESOURCE_IO
|IORESOURCE_MEM
|
84 IORESOURCE_IRQ
|IORESOURCE_DMA
)) == type
)
85 if (!strcmp(r
->name
, name
))
90 EXPORT_SYMBOL_GPL(platform_get_resource_byname
);
93 * platform_get_irq - get an IRQ for a device
94 * @dev: platform device
97 int platform_get_irq_byname(struct platform_device
*dev
, char *name
)
99 struct resource
*r
= platform_get_resource_byname(dev
, IORESOURCE_IRQ
, name
);
101 return r
? r
->start
: -ENXIO
;
103 EXPORT_SYMBOL_GPL(platform_get_irq_byname
);
106 * platform_add_devices - add a numbers of platform devices
107 * @devs: array of platform devices to add
108 * @num: number of platform devices in array
110 int platform_add_devices(struct platform_device
**devs
, int num
)
114 for (i
= 0; i
< num
; i
++) {
115 ret
= platform_device_register(devs
[i
]);
118 platform_device_unregister(devs
[i
]);
125 EXPORT_SYMBOL_GPL(platform_add_devices
);
127 struct platform_object
{
128 struct platform_device pdev
;
133 * platform_device_put
134 * @pdev: platform device to free
136 * Free all memory associated with a platform device. This function
137 * must _only_ be externally called in error cases. All other usage
140 void platform_device_put(struct platform_device
*pdev
)
143 put_device(&pdev
->dev
);
145 EXPORT_SYMBOL_GPL(platform_device_put
);
147 static void platform_device_release(struct device
*dev
)
149 struct platform_object
*pa
= container_of(dev
, struct platform_object
, pdev
.dev
);
151 kfree(pa
->pdev
.dev
.platform_data
);
152 kfree(pa
->pdev
.resource
);
157 * platform_device_alloc
158 * @name: base name of the device we're adding
161 * Create a platform device object which can have other objects attached
162 * to it, and which will have attached objects freed when it is released.
164 struct platform_device
*platform_device_alloc(const char *name
, unsigned int id
)
166 struct platform_object
*pa
;
168 pa
= kzalloc(sizeof(struct platform_object
) + strlen(name
), GFP_KERNEL
);
170 strcpy(pa
->name
, name
);
171 pa
->pdev
.name
= pa
->name
;
173 device_initialize(&pa
->pdev
.dev
);
174 pa
->pdev
.dev
.release
= platform_device_release
;
177 return pa
? &pa
->pdev
: NULL
;
179 EXPORT_SYMBOL_GPL(platform_device_alloc
);
182 * platform_device_add_resources
183 * @pdev: platform device allocated by platform_device_alloc to add resources to
184 * @res: set of resources that needs to be allocated for the device
185 * @num: number of resources
187 * Add a copy of the resources to the platform device. The memory
188 * associated with the resources will be freed when the platform
189 * device is released.
191 int platform_device_add_resources(struct platform_device
*pdev
, 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
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 platform_data
212 * pointer. The memory associated with the platform data will be freed
213 * when the platform device is released.
215 int platform_device_add_data(struct platform_device
*pdev
, void *data
, size_t size
)
219 d
= kmalloc(size
, GFP_KERNEL
);
221 memcpy(d
, data
, size
);
222 pdev
->dev
.platform_data
= d
;
224 return d
? 0 : -ENOMEM
;
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 snprintf(pdev
->dev
.bus_id
, BUS_ID_SIZE
, "%s.%u", pdev
->name
, pdev
->id
);
250 strlcpy(pdev
->dev
.bus_id
, pdev
->name
, BUS_ID_SIZE
);
252 for (i
= 0; i
< pdev
->num_resources
; i
++) {
253 struct resource
*p
, *r
= &pdev
->resource
[i
];
256 r
->name
= pdev
->dev
.bus_id
;
258 /* not much point checking for containment of IRQs -Jamey */
259 if (r
->flags
& IORESOURCE_IRQ
)
264 if (r
->flags
& IORESOURCE_MEM
)
266 else if (r
->flags
& IORESOURCE_IO
)
267 p
= &ioport_resource
;
270 if (p
&& insert_resource(p
, r
)) {
272 "%s: failed to claim resource %d\n",
273 pdev
->dev
.bus_id
, i
);
279 pr_debug("Registering platform device '%s'. Parent at %s\n",
280 pdev
->dev
.bus_id
, pdev
->dev
.parent
->bus_id
);
282 ret
= device_add(&pdev
->dev
);
288 if (pdev
->resource
[i
].flags
& (IORESOURCE_MEM
|IORESOURCE_IO
))
289 release_resource(&pdev
->resource
[i
]);
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).
301 void platform_device_del(struct platform_device
*pdev
)
306 for (i
= 0; i
< pdev
->num_resources
; i
++) {
307 struct resource
*r
= &pdev
->resource
[i
];
308 if (r
->flags
& (IORESOURCE_MEM
|IORESOURCE_IO
))
312 device_del(&pdev
->dev
);
315 EXPORT_SYMBOL_GPL(platform_device_del
);
318 * platform_device_register - add a platform-level device
319 * @pdev: platform device we're adding
322 int platform_device_register(struct platform_device
* pdev
)
324 device_initialize(&pdev
->dev
);
325 return platform_device_add(pdev
);
327 EXPORT_SYMBOL_GPL(platform_device_register
);
330 * platform_device_unregister - unregister a platform-level device
331 * @pdev: platform device we're unregistering
333 * Unregistration is done in 2 steps. First we release all resources
334 * and remove it from the subsystem, then we drop reference count by
335 * calling platform_device_put().
337 void platform_device_unregister(struct platform_device
* pdev
)
339 platform_device_del(pdev
);
340 platform_device_put(pdev
);
342 EXPORT_SYMBOL_GPL(platform_device_unregister
);
345 * platform_device_register_simple
346 * @name: base name of the device we're adding
348 * @res: set of resources that needs to be allocated for the device
349 * @num: number of resources
351 * This function creates a simple platform device that requires minimal
352 * resource and memory management. Canned release function freeing
353 * memory allocated for the device allows drivers using such devices
354 * to be unloaded iwithout waiting for the last reference to the device
357 struct platform_device
*platform_device_register_simple(char *name
, unsigned int id
,
358 struct resource
*res
, unsigned int num
)
360 struct platform_device
*pdev
;
363 pdev
= platform_device_alloc(name
, id
);
370 retval
= platform_device_add_resources(pdev
, res
, num
);
375 retval
= platform_device_add(pdev
);
382 platform_device_put(pdev
);
383 return ERR_PTR(retval
);
385 EXPORT_SYMBOL_GPL(platform_device_register_simple
);
387 static int platform_drv_probe(struct device
*_dev
)
389 struct platform_driver
*drv
= to_platform_driver(_dev
->driver
);
390 struct platform_device
*dev
= to_platform_device(_dev
);
392 return drv
->probe(dev
);
395 static int platform_drv_probe_fail(struct device
*_dev
)
400 static int platform_drv_remove(struct device
*_dev
)
402 struct platform_driver
*drv
= to_platform_driver(_dev
->driver
);
403 struct platform_device
*dev
= to_platform_device(_dev
);
405 return drv
->remove(dev
);
408 static void platform_drv_shutdown(struct device
*_dev
)
410 struct platform_driver
*drv
= to_platform_driver(_dev
->driver
);
411 struct platform_device
*dev
= to_platform_device(_dev
);
416 static int platform_drv_suspend(struct device
*_dev
, pm_message_t state
)
418 struct platform_driver
*drv
= to_platform_driver(_dev
->driver
);
419 struct platform_device
*dev
= to_platform_device(_dev
);
421 return drv
->suspend(dev
, state
);
424 static int platform_drv_resume(struct device
*_dev
)
426 struct platform_driver
*drv
= to_platform_driver(_dev
->driver
);
427 struct platform_device
*dev
= to_platform_device(_dev
);
429 return drv
->resume(dev
);
433 * platform_driver_register
434 * @drv: platform driver structure
436 int platform_driver_register(struct platform_driver
*drv
)
438 drv
->driver
.bus
= &platform_bus_type
;
440 drv
->driver
.probe
= platform_drv_probe
;
442 drv
->driver
.remove
= platform_drv_remove
;
444 drv
->driver
.shutdown
= platform_drv_shutdown
;
446 drv
->driver
.suspend
= platform_drv_suspend
;
448 drv
->driver
.resume
= platform_drv_resume
;
449 return driver_register(&drv
->driver
);
451 EXPORT_SYMBOL_GPL(platform_driver_register
);
454 * platform_driver_unregister
455 * @drv: platform driver structure
457 void platform_driver_unregister(struct platform_driver
*drv
)
459 driver_unregister(&drv
->driver
);
461 EXPORT_SYMBOL_GPL(platform_driver_unregister
);
464 * platform_driver_probe - register driver for non-hotpluggable device
465 * @drv: platform driver structure
466 * @probe: the driver probe routine, probably from an __init section
468 * Use this instead of platform_driver_register() when you know the device
469 * is not hotpluggable and has already been registered, and you want to
470 * remove its run-once probe() infrastructure from memory after the driver
471 * has bound to the device.
473 * One typical use for this would be with drivers for controllers integrated
474 * into system-on-chip processors, where the controller devices have been
475 * configured as part of board setup.
477 * Returns zero if the driver registered and bound to a device, else returns
478 * a negative error code and with the driver not registered.
480 int platform_driver_probe(struct platform_driver
*drv
,
481 int (*probe
)(struct platform_device
*))
485 /* temporary section violation during probe() */
487 retval
= code
= platform_driver_register(drv
);
489 /* Fixup that section violation, being paranoid about code scanning
490 * the list of drivers in order to probe new devices. Check to see
491 * if the probe was successful, and make sure any forced probes of
494 spin_lock(&platform_bus_type
.klist_drivers
.k_lock
);
496 if (code
== 0 && list_empty(&drv
->driver
.klist_devices
.k_list
))
498 drv
->driver
.probe
= platform_drv_probe_fail
;
499 spin_unlock(&platform_bus_type
.klist_drivers
.k_lock
);
502 platform_driver_unregister(drv
);
505 EXPORT_SYMBOL_GPL(platform_driver_probe
);
507 /* modalias support enables more hands-off userspace setup:
508 * (a) environment variable lets new-style hotplug events work once system is
509 * fully running: "modprobe $MODALIAS"
510 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
511 * mishandled before system is fully running: "modprobe $(cat modalias)"
514 modalias_show(struct device
*dev
, struct device_attribute
*a
, char *buf
)
516 struct platform_device
*pdev
= to_platform_device(dev
);
517 int len
= snprintf(buf
, PAGE_SIZE
, "%s\n", pdev
->name
);
519 return (len
>= PAGE_SIZE
) ? (PAGE_SIZE
- 1) : len
;
522 static struct device_attribute platform_dev_attrs
[] = {
527 static int platform_uevent(struct device
*dev
, char **envp
, int num_envp
,
528 char *buffer
, int buffer_size
)
530 struct platform_device
*pdev
= to_platform_device(dev
);
533 snprintf(buffer
, buffer_size
, "MODALIAS=%s", pdev
->name
);
539 * platform_match - bind platform device to platform driver.
543 * Platform device IDs are assumed to be encoded like this:
544 * "<name><instance>", where <name> is a short description of the
545 * type of device, like "pci" or "floppy", and <instance> is the
546 * enumerated instance of the device, like '0' or '42'.
547 * Driver IDs are simply "<name>".
548 * So, extract the <name> from the platform_device structure,
549 * and compare it against the name of the driver. Return whether
553 static int platform_match(struct device
* dev
, struct device_driver
* drv
)
555 struct platform_device
*pdev
= container_of(dev
, struct platform_device
, dev
);
557 return (strncmp(pdev
->name
, drv
->name
, BUS_ID_SIZE
) == 0);
560 static int platform_suspend(struct device
*dev
, pm_message_t mesg
)
564 if (dev
->driver
&& dev
->driver
->suspend
)
565 ret
= dev
->driver
->suspend(dev
, mesg
);
570 static int platform_suspend_late(struct device
*dev
, pm_message_t mesg
)
572 struct platform_driver
*drv
= to_platform_driver(dev
->driver
);
573 struct platform_device
*pdev
= container_of(dev
, struct platform_device
, dev
);
576 if (dev
->driver
&& drv
->suspend_late
)
577 ret
= drv
->suspend_late(pdev
, mesg
);
582 static int platform_resume_early(struct device
*dev
)
584 struct platform_driver
*drv
= to_platform_driver(dev
->driver
);
585 struct platform_device
*pdev
= container_of(dev
, struct platform_device
, dev
);
588 if (dev
->driver
&& drv
->resume_early
)
589 ret
= drv
->resume_early(pdev
);
594 static int platform_resume(struct device
* dev
)
598 if (dev
->driver
&& dev
->driver
->resume
)
599 ret
= dev
->driver
->resume(dev
);
604 struct bus_type platform_bus_type
= {
606 .dev_attrs
= platform_dev_attrs
,
607 .match
= platform_match
,
608 .uevent
= platform_uevent
,
609 .suspend
= platform_suspend
,
610 .suspend_late
= platform_suspend_late
,
611 .resume_early
= platform_resume_early
,
612 .resume
= platform_resume
,
614 EXPORT_SYMBOL_GPL(platform_bus_type
);
616 int __init
platform_bus_init(void)
618 device_register(&platform_bus
);
619 return bus_register(&platform_bus_type
);
622 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
623 u64
dma_get_required_mask(struct device
*dev
)
625 u32 low_totalram
= ((max_pfn
- 1) << PAGE_SHIFT
);
626 u32 high_totalram
= ((max_pfn
- 1) >> (32 - PAGE_SHIFT
));
629 if (!high_totalram
) {
630 /* convert to mask just covering totalram */
631 low_totalram
= (1 << (fls(low_totalram
) - 1));
632 low_totalram
+= low_totalram
- 1;
635 high_totalram
= (1 << (fls(high_totalram
) - 1));
636 high_totalram
+= high_totalram
- 1;
637 mask
= (((u64
)high_totalram
) << 32) + 0xffffffff;
639 return mask
& *dev
->dma_mask
;
641 EXPORT_SYMBOL_GPL(dma_get_required_mask
);