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 struct device platform_bus = {
28 * platform_get_resource - get a resource for a device
29 * @dev: platform device
30 * @type: resource type
31 * @num: resource index
34 platform_get_resource(struct platform_device *dev, unsigned int type,
39 for (i = 0; i < dev->num_resources; i++) {
40 struct resource *r = &dev->resource[i];
42 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
43 IORESOURCE_IRQ|IORESOURCE_DMA))
52 * platform_get_irq - get an IRQ for a device
53 * @dev: platform device
54 * @num: IRQ number index
56 int platform_get_irq(struct platform_device *dev, unsigned int num)
58 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
60 return r ? r->start : 0;
64 * platform_get_resource_byname - get a resource for a device by name
65 * @dev: platform device
66 * @type: resource type
67 * @name: resource name
70 platform_get_resource_byname(struct platform_device *dev, unsigned int type,
75 for (i = 0; i < dev->num_resources; i++) {
76 struct resource *r = &dev->resource[i];
78 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
79 IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
80 if (!strcmp(r->name, name))
87 * platform_get_irq - get an IRQ for a device
88 * @dev: platform device
91 int platform_get_irq_byname(struct platform_device *dev, char *name)
93 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
95 return r ? r->start : 0;
99 * platform_add_devices - add a numbers of platform devices
100 * @devs: array of platform devices to add
101 * @num: number of platform devices in array
103 int platform_add_devices(struct platform_device **devs, int num)
107 for (i = 0; i < num; i++) {
108 ret = platform_device_register(devs[i]);
111 platform_device_unregister(devs[i]);
119 struct platform_object {
120 struct platform_device pdev;
125 * platform_device_put
126 * @pdev: platform device to free
128 * Free all memory associated with a platform device. This function
129 * must _only_ be externally called in error cases. All other usage
132 void platform_device_put(struct platform_device *pdev)
135 put_device(&pdev->dev);
137 EXPORT_SYMBOL_GPL(platform_device_put);
139 static void platform_device_release(struct device *dev)
141 struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev);
143 kfree(pa->pdev.dev.platform_data);
144 kfree(pa->pdev.resource);
149 * platform_device_alloc
150 * @name: base name of the device we're adding
153 * Create a platform device object which can have other objects attached
154 * to it, and which will have attached objects freed when it is released.
156 struct platform_device *platform_device_alloc(const char *name, unsigned int id)
158 struct platform_object *pa;
160 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
162 strcpy(pa->name, name);
163 pa->pdev.name = pa->name;
165 device_initialize(&pa->pdev.dev);
166 pa->pdev.dev.release = platform_device_release;
169 return pa ? &pa->pdev : NULL;
171 EXPORT_SYMBOL_GPL(platform_device_alloc);
174 * platform_device_add_resources
175 * @pdev: platform device allocated by platform_device_alloc to add resources to
176 * @res: set of resources that needs to be allocated for the device
177 * @num: number of resources
179 * Add a copy of the resources to the platform device. The memory
180 * associated with the resources will be freed when the platform
181 * device is released.
183 int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num)
187 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
189 memcpy(r, res, sizeof(struct resource) * num);
191 pdev->num_resources = num;
193 return r ? 0 : -ENOMEM;
195 EXPORT_SYMBOL_GPL(platform_device_add_resources);
198 * platform_device_add_data
199 * @pdev: platform device allocated by platform_device_alloc to add resources to
200 * @data: platform specific data for this platform device
201 * @size: size of platform specific data
203 * Add a copy of platform specific data to the platform device's platform_data
204 * pointer. The memory associated with the platform data will be freed
205 * when the platform device is released.
207 int platform_device_add_data(struct platform_device *pdev, void *data, size_t size)
211 d = kmalloc(size, GFP_KERNEL);
213 memcpy(d, data, size);
214 pdev->dev.platform_data = d;
216 return d ? 0 : -ENOMEM;
218 EXPORT_SYMBOL_GPL(platform_device_add_data);
221 * platform_device_add - add a platform device to device hierarchy
222 * @pdev: platform device we're adding
224 * This is part 2 of platform_device_register(), though may be called
225 * separately _iff_ pdev was allocated by platform_device_alloc().
227 int platform_device_add(struct platform_device *pdev)
234 if (!pdev->dev.parent)
235 pdev->dev.parent = &platform_bus;
237 pdev->dev.bus = &platform_bus_type;
240 snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
242 strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
244 for (i = 0; i < pdev->num_resources; i++) {
245 struct resource *p, *r = &pdev->resource[i];
248 r->name = pdev->dev.bus_id;
252 if (r->flags & IORESOURCE_MEM)
254 else if (r->flags & IORESOURCE_IO)
255 p = &ioport_resource;
258 if (p && request_resource(p, r)) {
260 "%s: failed to claim resource %d\n",
261 pdev->dev.bus_id, i);
267 pr_debug("Registering platform device '%s'. Parent at %s\n",
268 pdev->dev.bus_id, pdev->dev.parent->bus_id);
270 ret = device_register(&pdev->dev);
276 if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
277 release_resource(&pdev->resource[i]);
280 EXPORT_SYMBOL_GPL(platform_device_add);
283 * platform_device_register - add a platform-level device
284 * @pdev: platform device we're adding
287 int platform_device_register(struct platform_device * pdev)
289 device_initialize(&pdev->dev);
290 return platform_device_add(pdev);
294 * platform_device_unregister - remove a platform-level device
295 * @pdev: platform device we're removing
297 * Note that this function will also release all memory- and port-based
298 * resources owned by the device (@dev->resource).
300 void platform_device_unregister(struct platform_device * pdev)
305 for (i = 0; i < pdev->num_resources; i++) {
306 struct resource *r = &pdev->resource[i];
307 if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
311 device_unregister(&pdev->dev);
316 * platform_device_register_simple
317 * @name: base name of the device we're adding
319 * @res: set of resources that needs to be allocated for the device
320 * @num: number of resources
322 * This function creates a simple platform device that requires minimal
323 * resource and memory management. Canned release function freeing
324 * memory allocated for the device allows drivers using such devices
325 * to be unloaded iwithout waiting for the last reference to the device
328 struct platform_device *platform_device_register_simple(char *name, unsigned int id,
329 struct resource *res, unsigned int num)
331 struct platform_device *pdev;
334 pdev = platform_device_alloc(name, id);
341 retval = platform_device_add_resources(pdev, res, num);
346 retval = platform_device_add(pdev);
353 platform_device_put(pdev);
354 return ERR_PTR(retval);
359 * platform_match - bind platform device to platform driver.
363 * Platform device IDs are assumed to be encoded like this:
364 * "<name><instance>", where <name> is a short description of the
365 * type of device, like "pci" or "floppy", and <instance> is the
366 * enumerated instance of the device, like '0' or '42'.
367 * Driver IDs are simply "<name>".
368 * So, extract the <name> from the platform_device structure,
369 * and compare it against the name of the driver. Return whether
373 static int platform_match(struct device * dev, struct device_driver * drv)
375 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
377 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
380 static int platform_suspend(struct device * dev, pm_message_t state)
384 if (dev->driver && dev->driver->suspend)
385 ret = dev->driver->suspend(dev, state);
390 static int platform_resume(struct device * dev)
394 if (dev->driver && dev->driver->resume)
395 ret = dev->driver->resume(dev);
400 struct bus_type platform_bus_type = {
402 .match = platform_match,
403 .suspend = platform_suspend,
404 .resume = platform_resume,
407 int __init platform_bus_init(void)
409 device_register(&platform_bus);
410 return bus_register(&platform_bus_type);
413 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
414 u64 dma_get_required_mask(struct device *dev)
416 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
417 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
420 if (!high_totalram) {
421 /* convert to mask just covering totalram */
422 low_totalram = (1 << (fls(low_totalram) - 1));
423 low_totalram += low_totalram - 1;
426 high_totalram = (1 << (fls(high_totalram) - 1));
427 high_totalram += high_totalram - 1;
428 mask = (((u64)high_totalram) << 32) + 0xffffffff;
430 return mask & *dev->dma_mask;
432 EXPORT_SYMBOL_GPL(dma_get_required_mask);
435 EXPORT_SYMBOL_GPL(platform_bus);
436 EXPORT_SYMBOL_GPL(platform_bus_type);
437 EXPORT_SYMBOL_GPL(platform_add_devices);
438 EXPORT_SYMBOL_GPL(platform_device_register);
439 EXPORT_SYMBOL_GPL(platform_device_register_simple);
440 EXPORT_SYMBOL_GPL(platform_device_unregister);
441 EXPORT_SYMBOL_GPL(platform_get_irq);
442 EXPORT_SYMBOL_GPL(platform_get_resource);
443 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
444 EXPORT_SYMBOL_GPL(platform_get_resource_byname);