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/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>
22 struct device platform_bus = {
27 * platform_get_resource - get a resource for a device
28 * @dev: platform device
29 * @type: resource type
30 * @num: resource index
33 platform_get_resource(struct platform_device *dev, unsigned int type,
38 for (i = 0; i < dev->num_resources; i++) {
39 struct resource *r = &dev->resource[i];
41 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
42 IORESOURCE_IRQ|IORESOURCE_DMA))
51 * platform_get_irq - get an IRQ for a device
52 * @dev: platform device
53 * @num: IRQ number index
55 int platform_get_irq(struct platform_device *dev, unsigned int num)
57 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
59 return r ? r->start : 0;
63 * platform_get_resource_byname - get a resource for a device by name
64 * @dev: platform device
65 * @type: resource type
66 * @name: resource name
69 platform_get_resource_byname(struct platform_device *dev, unsigned int type,
74 for (i = 0; i < dev->num_resources; i++) {
75 struct resource *r = &dev->resource[i];
77 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
78 IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
79 if (!strcmp(r->name, name))
86 * platform_get_irq - get an IRQ for a device
87 * @dev: platform device
90 int platform_get_irq_byname(struct platform_device *dev, char *name)
92 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
94 return r ? r->start : 0;
98 * platform_add_devices - add a numbers of platform devices
99 * @devs: array of platform devices to add
100 * @num: number of platform devices in array
102 int platform_add_devices(struct platform_device **devs, int num)
106 for (i = 0; i < num; i++) {
107 ret = platform_device_register(devs[i]);
110 platform_device_unregister(devs[i]);
119 * platform_device_register - add a platform-level device
120 * @pdev: platform device we're adding
123 int platform_device_register(struct platform_device * pdev)
130 if (!pdev->dev.parent)
131 pdev->dev.parent = &platform_bus;
133 pdev->dev.bus = &platform_bus_type;
136 snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
138 strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
140 for (i = 0; i < pdev->num_resources; i++) {
141 struct resource *p, *r = &pdev->resource[i];
144 r->name = pdev->dev.bus_id;
148 if (r->flags & IORESOURCE_MEM)
150 else if (r->flags & IORESOURCE_IO)
151 p = &ioport_resource;
154 if (p && request_resource(p, r)) {
156 "%s: failed to claim resource %d\n",
157 pdev->dev.bus_id, i);
163 pr_debug("Registering platform device '%s'. Parent at %s\n",
164 pdev->dev.bus_id, pdev->dev.parent->bus_id);
166 ret = device_register(&pdev->dev);
172 if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
173 release_resource(&pdev->resource[i]);
178 * platform_device_unregister - remove a platform-level device
179 * @pdev: platform device we're removing
181 * Note that this function will also release all memory- and port-based
182 * resources owned by the device (@dev->resource).
184 void platform_device_unregister(struct platform_device * pdev)
189 for (i = 0; i < pdev->num_resources; i++) {
190 struct resource *r = &pdev->resource[i];
191 if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
195 device_unregister(&pdev->dev);
199 struct platform_object {
200 struct platform_device pdev;
201 struct resource resources[0];
204 static void platform_device_release_simple(struct device *dev)
206 struct platform_device *pdev = to_platform_device(dev);
208 kfree(container_of(pdev, struct platform_object, pdev));
212 * platform_device_register_simple
213 * @name: base name of the device we're adding
215 * @res: set of resources that needs to be allocated for the device
216 * @num: number of resources
218 * This function creates a simple platform device that requires minimal
219 * resource and memory management. Canned release function freeing
220 * memory allocated for the device allows drivers using such devices
221 * to be unloaded iwithout waiting for the last reference to the device
224 struct platform_device *platform_device_register_simple(char *name, unsigned int id,
225 struct resource *res, unsigned int num)
227 struct platform_object *pobj;
230 pobj = kzalloc(sizeof(*pobj) + sizeof(struct resource) * num, GFP_KERNEL);
236 pobj->pdev.name = name;
238 pobj->pdev.dev.release = platform_device_release_simple;
241 memcpy(pobj->resources, res, sizeof(struct resource) * num);
242 pobj->pdev.resource = pobj->resources;
243 pobj->pdev.num_resources = num;
246 retval = platform_device_register(&pobj->pdev);
254 return ERR_PTR(retval);
259 * platform_match - bind platform device to platform driver.
263 * Platform device IDs are assumed to be encoded like this:
264 * "<name><instance>", where <name> is a short description of the
265 * type of device, like "pci" or "floppy", and <instance> is the
266 * enumerated instance of the device, like '0' or '42'.
267 * Driver IDs are simply "<name>".
268 * So, extract the <name> from the platform_device structure,
269 * and compare it against the name of the driver. Return whether
273 static int platform_match(struct device * dev, struct device_driver * drv)
275 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
277 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
280 static int platform_suspend(struct device * dev, pm_message_t state)
284 if (dev->driver && dev->driver->suspend)
285 ret = dev->driver->suspend(dev, state);
290 static int platform_resume(struct device * dev)
294 if (dev->driver && dev->driver->resume)
295 ret = dev->driver->resume(dev);
300 struct bus_type platform_bus_type = {
302 .match = platform_match,
303 .suspend = platform_suspend,
304 .resume = platform_resume,
307 int __init platform_bus_init(void)
309 device_register(&platform_bus);
310 return bus_register(&platform_bus_type);
313 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
314 u64 dma_get_required_mask(struct device *dev)
316 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
317 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
320 if (!high_totalram) {
321 /* convert to mask just covering totalram */
322 low_totalram = (1 << (fls(low_totalram) - 1));
323 low_totalram += low_totalram - 1;
326 high_totalram = (1 << (fls(high_totalram) - 1));
327 high_totalram += high_totalram - 1;
328 mask = (((u64)high_totalram) << 32) + 0xffffffff;
330 return mask & *dev->dma_mask;
332 EXPORT_SYMBOL_GPL(dma_get_required_mask);
335 EXPORT_SYMBOL_GPL(platform_bus);
336 EXPORT_SYMBOL_GPL(platform_bus_type);
337 EXPORT_SYMBOL_GPL(platform_add_devices);
338 EXPORT_SYMBOL_GPL(platform_device_register);
339 EXPORT_SYMBOL_GPL(platform_device_register_simple);
340 EXPORT_SYMBOL_GPL(platform_device_unregister);
341 EXPORT_SYMBOL_GPL(platform_get_irq);
342 EXPORT_SYMBOL_GPL(platform_get_resource);
343 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
344 EXPORT_SYMBOL_GPL(platform_get_resource_byname);