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 = {
30 * platform_get_resource - get a resource for a device
31 * @dev: platform device
32 * @type: resource type
33 * @num: resource index
36 platform_get_resource(struct platform_device *dev, unsigned int type,
41 for (i = 0; i < dev->num_resources; i++) {
42 struct resource *r = &dev->resource[i];
44 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
45 IORESOURCE_IRQ|IORESOURCE_DMA))
54 * platform_get_irq - get an IRQ for a device
55 * @dev: platform device
56 * @num: IRQ number index
58 int platform_get_irq(struct platform_device *dev, unsigned int num)
60 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
62 return r ? r->start : 0;
66 * platform_get_resource_byname - get a resource for a device by name
67 * @dev: platform device
68 * @type: resource type
69 * @name: resource name
72 platform_get_resource_byname(struct platform_device *dev, unsigned int type,
77 for (i = 0; i < dev->num_resources; i++) {
78 struct resource *r = &dev->resource[i];
80 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
81 IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
82 if (!strcmp(r->name, name))
89 * platform_get_irq - get an IRQ for a device
90 * @dev: platform device
93 int platform_get_irq_byname(struct platform_device *dev, char *name)
95 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
97 return r ? r->start : 0;
101 * platform_add_devices - add a numbers of platform devices
102 * @devs: array of platform devices to add
103 * @num: number of platform devices in array
105 int platform_add_devices(struct platform_device **devs, int num)
109 for (i = 0; i < num; i++) {
110 ret = platform_device_register(devs[i]);
113 platform_device_unregister(devs[i]);
121 struct platform_object {
122 struct platform_device pdev;
127 * platform_device_put
128 * @pdev: platform device to free
130 * Free all memory associated with a platform device. This function
131 * must _only_ be externally called in error cases. All other usage
134 void platform_device_put(struct platform_device *pdev)
137 put_device(&pdev->dev);
139 EXPORT_SYMBOL_GPL(platform_device_put);
141 static void platform_device_release(struct device *dev)
143 struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev);
145 kfree(pa->pdev.dev.platform_data);
146 kfree(pa->pdev.resource);
151 * platform_device_alloc
152 * @name: base name of the device we're adding
155 * Create a platform device object which can have other objects attached
156 * to it, and which will have attached objects freed when it is released.
158 struct platform_device *platform_device_alloc(const char *name, unsigned int id)
160 struct platform_object *pa;
162 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
164 strcpy(pa->name, name);
165 pa->pdev.name = pa->name;
167 device_initialize(&pa->pdev.dev);
168 pa->pdev.dev.release = platform_device_release;
171 return pa ? &pa->pdev : NULL;
173 EXPORT_SYMBOL_GPL(platform_device_alloc);
176 * platform_device_add_resources
177 * @pdev: platform device allocated by platform_device_alloc to add resources to
178 * @res: set of resources that needs to be allocated for the device
179 * @num: number of resources
181 * Add a copy of the resources to the platform device. The memory
182 * associated with the resources will be freed when the platform
183 * device is released.
185 int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num)
189 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
191 memcpy(r, res, sizeof(struct resource) * num);
193 pdev->num_resources = num;
195 return r ? 0 : -ENOMEM;
197 EXPORT_SYMBOL_GPL(platform_device_add_resources);
200 * platform_device_add_data
201 * @pdev: platform device allocated by platform_device_alloc to add resources to
202 * @data: platform specific data for this platform device
203 * @size: size of platform specific data
205 * Add a copy of platform specific data to the platform device's platform_data
206 * pointer. The memory associated with the platform data will be freed
207 * when the platform device is released.
209 int platform_device_add_data(struct platform_device *pdev, void *data, size_t size)
213 d = kmalloc(size, GFP_KERNEL);
215 memcpy(d, data, size);
216 pdev->dev.platform_data = d;
218 return d ? 0 : -ENOMEM;
220 EXPORT_SYMBOL_GPL(platform_device_add_data);
223 * platform_device_add - add a platform device to device hierarchy
224 * @pdev: platform device we're adding
226 * This is part 2 of platform_device_register(), though may be called
227 * separately _iff_ pdev was allocated by platform_device_alloc().
229 int platform_device_add(struct platform_device *pdev)
236 if (!pdev->dev.parent)
237 pdev->dev.parent = &platform_bus;
239 pdev->dev.bus = &platform_bus_type;
242 snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
244 strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
246 for (i = 0; i < pdev->num_resources; i++) {
247 struct resource *p, *r = &pdev->resource[i];
250 r->name = pdev->dev.bus_id;
254 if (r->flags & IORESOURCE_MEM)
256 else if (r->flags & IORESOURCE_IO)
257 p = &ioport_resource;
260 if (p && insert_resource(p, r)) {
262 "%s: failed to claim resource %d\n",
263 pdev->dev.bus_id, i);
269 pr_debug("Registering platform device '%s'. Parent at %s\n",
270 pdev->dev.bus_id, pdev->dev.parent->bus_id);
272 ret = device_register(&pdev->dev);
278 if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
279 release_resource(&pdev->resource[i]);
282 EXPORT_SYMBOL_GPL(platform_device_add);
285 * platform_device_del - remove a platform-level device
286 * @pdev: platform device we're removing
288 * Note that this function will also release all memory- and port-based
289 * resources owned by the device (@dev->resource).
291 void platform_device_del(struct platform_device *pdev)
296 for (i = 0; i < pdev->num_resources; i++) {
297 struct resource *r = &pdev->resource[i];
298 if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
302 device_del(&pdev->dev);
305 EXPORT_SYMBOL_GPL(platform_device_del);
308 * platform_device_register - add a platform-level device
309 * @pdev: platform device we're adding
312 int platform_device_register(struct platform_device * pdev)
314 device_initialize(&pdev->dev);
315 return platform_device_add(pdev);
319 * platform_device_unregister - unregister a platform-level device
320 * @pdev: platform device we're unregistering
322 * Unregistration is done in 2 steps. Fisrt we release all resources
323 * and remove it from the sybsystem, then we drop reference count by
324 * calling platform_device_put().
326 void platform_device_unregister(struct platform_device * pdev)
328 platform_device_del(pdev);
329 platform_device_put(pdev);
333 * platform_device_register_simple
334 * @name: base name of the device we're adding
336 * @res: set of resources that needs to be allocated for the device
337 * @num: number of resources
339 * This function creates a simple platform device that requires minimal
340 * resource and memory management. Canned release function freeing
341 * memory allocated for the device allows drivers using such devices
342 * to be unloaded iwithout waiting for the last reference to the device
345 struct platform_device *platform_device_register_simple(char *name, unsigned int id,
346 struct resource *res, unsigned int num)
348 struct platform_device *pdev;
351 pdev = platform_device_alloc(name, id);
358 retval = platform_device_add_resources(pdev, res, num);
363 retval = platform_device_add(pdev);
370 platform_device_put(pdev);
371 return ERR_PTR(retval);
374 static int platform_drv_probe(struct device *_dev)
376 struct platform_driver *drv = to_platform_driver(_dev->driver);
377 struct platform_device *dev = to_platform_device(_dev);
379 return drv->probe(dev);
382 static int platform_drv_remove(struct device *_dev)
384 struct platform_driver *drv = to_platform_driver(_dev->driver);
385 struct platform_device *dev = to_platform_device(_dev);
387 return drv->remove(dev);
390 static void platform_drv_shutdown(struct device *_dev)
392 struct platform_driver *drv = to_platform_driver(_dev->driver);
393 struct platform_device *dev = to_platform_device(_dev);
398 static int platform_drv_suspend(struct device *_dev, pm_message_t state)
400 struct platform_driver *drv = to_platform_driver(_dev->driver);
401 struct platform_device *dev = to_platform_device(_dev);
403 return drv->suspend(dev, state);
406 static int platform_drv_resume(struct device *_dev)
408 struct platform_driver *drv = to_platform_driver(_dev->driver);
409 struct platform_device *dev = to_platform_device(_dev);
411 return drv->resume(dev);
415 * platform_driver_register
416 * @drv: platform driver structure
418 int platform_driver_register(struct platform_driver *drv)
420 drv->driver.bus = &platform_bus_type;
422 drv->driver.probe = platform_drv_probe;
424 drv->driver.remove = platform_drv_remove;
426 drv->driver.shutdown = platform_drv_shutdown;
428 drv->driver.suspend = platform_drv_suspend;
430 drv->driver.resume = platform_drv_resume;
431 return driver_register(&drv->driver);
433 EXPORT_SYMBOL_GPL(platform_driver_register);
436 * platform_driver_unregister
437 * @drv: platform driver structure
439 void platform_driver_unregister(struct platform_driver *drv)
441 driver_unregister(&drv->driver);
443 EXPORT_SYMBOL_GPL(platform_driver_unregister);
447 * platform_match - bind platform device to platform driver.
451 * Platform device IDs are assumed to be encoded like this:
452 * "<name><instance>", where <name> is a short description of the
453 * type of device, like "pci" or "floppy", and <instance> is the
454 * enumerated instance of the device, like '0' or '42'.
455 * Driver IDs are simply "<name>".
456 * So, extract the <name> from the platform_device structure,
457 * and compare it against the name of the driver. Return whether
461 static int platform_match(struct device * dev, struct device_driver * drv)
463 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
465 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
468 static int platform_suspend(struct device * dev, pm_message_t state)
472 if (dev->driver && dev->driver->suspend)
473 ret = dev->driver->suspend(dev, state);
478 static int platform_resume(struct device * dev)
482 if (dev->driver && dev->driver->resume)
483 ret = dev->driver->resume(dev);
488 struct bus_type platform_bus_type = {
490 .match = platform_match,
491 .suspend = platform_suspend,
492 .resume = platform_resume,
495 int __init platform_bus_init(void)
497 device_register(&platform_bus);
498 return bus_register(&platform_bus_type);
501 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
502 u64 dma_get_required_mask(struct device *dev)
504 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
505 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
508 if (!high_totalram) {
509 /* convert to mask just covering totalram */
510 low_totalram = (1 << (fls(low_totalram) - 1));
511 low_totalram += low_totalram - 1;
514 high_totalram = (1 << (fls(high_totalram) - 1));
515 high_totalram += high_totalram - 1;
516 mask = (((u64)high_totalram) << 32) + 0xffffffff;
518 return mask & *dev->dma_mask;
520 EXPORT_SYMBOL_GPL(dma_get_required_mask);
523 EXPORT_SYMBOL_GPL(platform_bus);
524 EXPORT_SYMBOL_GPL(platform_bus_type);
525 EXPORT_SYMBOL_GPL(platform_add_devices);
526 EXPORT_SYMBOL_GPL(platform_device_register);
527 EXPORT_SYMBOL_GPL(platform_device_register_simple);
528 EXPORT_SYMBOL_GPL(platform_device_unregister);
529 EXPORT_SYMBOL_GPL(platform_get_irq);
530 EXPORT_SYMBOL_GPL(platform_get_resource);
531 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
532 EXPORT_SYMBOL_GPL(platform_get_resource_byname);