[ALSA] Optimize for config without PROC_FS
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / platform.c
blob8827dafba9451a7f741b9330eb0e87b58210ab3f
1 /*
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
10 * information.
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>
21 #include "base.h"
23 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver))
25 struct device platform_bus = {
26 .bus_id = "platform",
29 /**
30 * platform_get_resource - get a resource for a device
31 * @dev: platform device
32 * @type: resource type
33 * @num: resource index
35 struct resource *
36 platform_get_resource(struct platform_device *dev, unsigned int type,
37 unsigned int num)
39 int i;
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))
46 == type)
47 if (num-- == 0)
48 return r;
50 return NULL;
53 /**
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;
65 /**
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
71 struct resource *
72 platform_get_resource_byname(struct platform_device *dev, unsigned int type,
73 char *name)
75 int i;
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))
83 return r;
85 return NULL;
88 /**
89 * platform_get_irq - get an IRQ for a device
90 * @dev: platform device
91 * @name: IRQ name
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)
107 int i, ret = 0;
109 for (i = 0; i < num; i++) {
110 ret = platform_device_register(devs[i]);
111 if (ret) {
112 while (--i >= 0)
113 platform_device_unregister(devs[i]);
114 break;
118 return ret;
121 struct platform_object {
122 struct platform_device pdev;
123 char name[1];
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
132 * is a bug.
134 void platform_device_put(struct platform_device *pdev)
136 if (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);
147 kfree(pa);
151 * platform_device_alloc
152 * @name: base name of the device we're adding
153 * @id: instance id
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);
163 if (pa) {
164 strcpy(pa->name, name);
165 pa->pdev.name = pa->name;
166 pa->pdev.id = id;
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)
187 struct resource *r;
189 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
190 if (r) {
191 memcpy(r, res, sizeof(struct resource) * num);
192 pdev->resource = r;
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)
211 void *d;
213 d = kmalloc(size, GFP_KERNEL);
214 if (d) {
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)
231 int i, ret = 0;
233 if (!pdev)
234 return -EINVAL;
236 if (!pdev->dev.parent)
237 pdev->dev.parent = &platform_bus;
239 pdev->dev.bus = &platform_bus_type;
241 if (pdev->id != -1)
242 snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
243 else
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];
249 if (r->name == NULL)
250 r->name = pdev->dev.bus_id;
252 p = r->parent;
253 if (!p) {
254 if (r->flags & IORESOURCE_MEM)
255 p = &iomem_resource;
256 else if (r->flags & IORESOURCE_IO)
257 p = &ioport_resource;
260 if (p && request_resource(p, r)) {
261 printk(KERN_ERR
262 "%s: failed to claim resource %d\n",
263 pdev->dev.bus_id, i);
264 ret = -EBUSY;
265 goto failed;
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);
273 if (ret == 0)
274 return ret;
276 failed:
277 while (--i >= 0)
278 if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
279 release_resource(&pdev->resource[i]);
280 return ret;
282 EXPORT_SYMBOL_GPL(platform_device_add);
285 * platform_device_register - add a platform-level device
286 * @pdev: platform device we're adding
289 int platform_device_register(struct platform_device * pdev)
291 device_initialize(&pdev->dev);
292 return platform_device_add(pdev);
296 * platform_device_unregister - 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).
302 void platform_device_unregister(struct platform_device * pdev)
304 int i;
306 if (pdev) {
307 for (i = 0; i < pdev->num_resources; i++) {
308 struct resource *r = &pdev->resource[i];
309 if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
310 release_resource(r);
313 device_unregister(&pdev->dev);
318 * platform_device_register_simple
319 * @name: base name of the device we're adding
320 * @id: instance id
321 * @res: set of resources that needs to be allocated for the device
322 * @num: number of resources
324 * This function creates a simple platform device that requires minimal
325 * resource and memory management. Canned release function freeing
326 * memory allocated for the device allows drivers using such devices
327 * to be unloaded iwithout waiting for the last reference to the device
328 * to be dropped.
330 struct platform_device *platform_device_register_simple(char *name, unsigned int id,
331 struct resource *res, unsigned int num)
333 struct platform_device *pdev;
334 int retval;
336 pdev = platform_device_alloc(name, id);
337 if (!pdev) {
338 retval = -ENOMEM;
339 goto error;
342 if (num) {
343 retval = platform_device_add_resources(pdev, res, num);
344 if (retval)
345 goto error;
348 retval = platform_device_add(pdev);
349 if (retval)
350 goto error;
352 return pdev;
354 error:
355 platform_device_put(pdev);
356 return ERR_PTR(retval);
359 static int platform_drv_probe(struct device *_dev)
361 struct platform_driver *drv = to_platform_driver(_dev->driver);
362 struct platform_device *dev = to_platform_device(_dev);
364 return drv->probe(dev);
367 static int platform_drv_remove(struct device *_dev)
369 struct platform_driver *drv = to_platform_driver(_dev->driver);
370 struct platform_device *dev = to_platform_device(_dev);
372 return drv->remove(dev);
375 static void platform_drv_shutdown(struct device *_dev)
377 struct platform_driver *drv = to_platform_driver(_dev->driver);
378 struct platform_device *dev = to_platform_device(_dev);
380 drv->shutdown(dev);
383 static int platform_drv_suspend(struct device *_dev, pm_message_t state)
385 struct platform_driver *drv = to_platform_driver(_dev->driver);
386 struct platform_device *dev = to_platform_device(_dev);
388 return drv->suspend(dev, state);
391 static int platform_drv_resume(struct device *_dev)
393 struct platform_driver *drv = to_platform_driver(_dev->driver);
394 struct platform_device *dev = to_platform_device(_dev);
396 return drv->resume(dev);
400 * platform_driver_register
401 * @drv: platform driver structure
403 int platform_driver_register(struct platform_driver *drv)
405 drv->driver.bus = &platform_bus_type;
406 if (drv->probe)
407 drv->driver.probe = platform_drv_probe;
408 if (drv->remove)
409 drv->driver.remove = platform_drv_remove;
410 if (drv->shutdown)
411 drv->driver.shutdown = platform_drv_shutdown;
412 if (drv->suspend)
413 drv->driver.suspend = platform_drv_suspend;
414 if (drv->resume)
415 drv->driver.resume = platform_drv_resume;
416 return driver_register(&drv->driver);
418 EXPORT_SYMBOL_GPL(platform_driver_register);
421 * platform_driver_unregister
422 * @drv: platform driver structure
424 void platform_driver_unregister(struct platform_driver *drv)
426 driver_unregister(&drv->driver);
428 EXPORT_SYMBOL_GPL(platform_driver_unregister);
432 * platform_match - bind platform device to platform driver.
433 * @dev: device.
434 * @drv: driver.
436 * Platform device IDs are assumed to be encoded like this:
437 * "<name><instance>", where <name> is a short description of the
438 * type of device, like "pci" or "floppy", and <instance> is the
439 * enumerated instance of the device, like '0' or '42'.
440 * Driver IDs are simply "<name>".
441 * So, extract the <name> from the platform_device structure,
442 * and compare it against the name of the driver. Return whether
443 * they match or not.
446 static int platform_match(struct device * dev, struct device_driver * drv)
448 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
450 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
453 static int platform_suspend(struct device * dev, pm_message_t state)
455 int ret = 0;
457 if (dev->driver && dev->driver->suspend)
458 ret = dev->driver->suspend(dev, state);
460 return ret;
463 static int platform_resume(struct device * dev)
465 int ret = 0;
467 if (dev->driver && dev->driver->resume)
468 ret = dev->driver->resume(dev);
470 return ret;
473 struct bus_type platform_bus_type = {
474 .name = "platform",
475 .match = platform_match,
476 .suspend = platform_suspend,
477 .resume = platform_resume,
480 int __init platform_bus_init(void)
482 device_register(&platform_bus);
483 return bus_register(&platform_bus_type);
486 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
487 u64 dma_get_required_mask(struct device *dev)
489 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
490 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
491 u64 mask;
493 if (!high_totalram) {
494 /* convert to mask just covering totalram */
495 low_totalram = (1 << (fls(low_totalram) - 1));
496 low_totalram += low_totalram - 1;
497 mask = low_totalram;
498 } else {
499 high_totalram = (1 << (fls(high_totalram) - 1));
500 high_totalram += high_totalram - 1;
501 mask = (((u64)high_totalram) << 32) + 0xffffffff;
503 return mask & *dev->dma_mask;
505 EXPORT_SYMBOL_GPL(dma_get_required_mask);
506 #endif
508 EXPORT_SYMBOL_GPL(platform_bus);
509 EXPORT_SYMBOL_GPL(platform_bus_type);
510 EXPORT_SYMBOL_GPL(platform_add_devices);
511 EXPORT_SYMBOL_GPL(platform_device_register);
512 EXPORT_SYMBOL_GPL(platform_device_register_simple);
513 EXPORT_SYMBOL_GPL(platform_device_unregister);
514 EXPORT_SYMBOL_GPL(platform_get_irq);
515 EXPORT_SYMBOL_GPL(platform_get_resource);
516 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
517 EXPORT_SYMBOL_GPL(platform_get_resource_byname);