Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * platform.c - platform 'pseudo' bus for legacy devices |
| 3 | * |
| 4 | * Copyright (c) 2002-3 Patrick Mochel |
| 5 | * Copyright (c) 2002-3 Open Source Development Labs |
| 6 | * |
| 7 | * This file is released under the GPLv2 |
| 8 | * |
| 9 | * Please see Documentation/driver-model/platform.txt for more |
| 10 | * information. |
| 11 | */ |
| 12 | |
| 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> |
| 19 | |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 20 | #include "base.h" |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | struct device platform_bus = { |
| 23 | .bus_id = "platform", |
| 24 | }; |
| 25 | |
| 26 | /** |
| 27 | * platform_get_resource - get a resource for a device |
| 28 | * @dev: platform device |
| 29 | * @type: resource type |
| 30 | * @num: resource index |
| 31 | */ |
| 32 | struct resource * |
| 33 | platform_get_resource(struct platform_device *dev, unsigned int type, |
| 34 | unsigned int num) |
| 35 | { |
| 36 | int i; |
| 37 | |
| 38 | for (i = 0; i < dev->num_resources; i++) { |
| 39 | struct resource *r = &dev->resource[i]; |
| 40 | |
| 41 | if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM| |
| 42 | IORESOURCE_IRQ|IORESOURCE_DMA)) |
| 43 | == type) |
| 44 | if (num-- == 0) |
| 45 | return r; |
| 46 | } |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * platform_get_irq - get an IRQ for a device |
| 52 | * @dev: platform device |
| 53 | * @num: IRQ number index |
| 54 | */ |
| 55 | int platform_get_irq(struct platform_device *dev, unsigned int num) |
| 56 | { |
| 57 | struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num); |
| 58 | |
| 59 | return r ? r->start : 0; |
| 60 | } |
| 61 | |
| 62 | /** |
| 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 |
| 67 | */ |
| 68 | struct resource * |
| 69 | platform_get_resource_byname(struct platform_device *dev, unsigned int type, |
| 70 | char *name) |
| 71 | { |
| 72 | int i; |
| 73 | |
| 74 | for (i = 0; i < dev->num_resources; i++) { |
| 75 | struct resource *r = &dev->resource[i]; |
| 76 | |
| 77 | if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM| |
| 78 | IORESOURCE_IRQ|IORESOURCE_DMA)) == type) |
| 79 | if (!strcmp(r->name, name)) |
| 80 | return r; |
| 81 | } |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * platform_get_irq - get an IRQ for a device |
| 87 | * @dev: platform device |
| 88 | * @name: IRQ name |
| 89 | */ |
| 90 | int platform_get_irq_byname(struct platform_device *dev, char *name) |
| 91 | { |
| 92 | struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name); |
| 93 | |
| 94 | return r ? r->start : 0; |
| 95 | } |
| 96 | |
| 97 | /** |
| 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 |
| 101 | */ |
| 102 | int platform_add_devices(struct platform_device **devs, int num) |
| 103 | { |
| 104 | int i, ret = 0; |
| 105 | |
| 106 | for (i = 0; i < num; i++) { |
| 107 | ret = platform_device_register(devs[i]); |
| 108 | if (ret) { |
| 109 | while (--i >= 0) |
| 110 | platform_device_unregister(devs[i]); |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * platform_device_register - add a platform-level device |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 120 | * @pdev: platform device we're adding |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | * |
| 122 | */ |
| 123 | int platform_device_register(struct platform_device * pdev) |
| 124 | { |
| 125 | int i, ret = 0; |
| 126 | |
| 127 | if (!pdev) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | if (!pdev->dev.parent) |
| 131 | pdev->dev.parent = &platform_bus; |
| 132 | |
| 133 | pdev->dev.bus = &platform_bus_type; |
| 134 | |
| 135 | if (pdev->id != -1) |
| 136 | snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id); |
| 137 | else |
| 138 | strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE); |
| 139 | |
| 140 | for (i = 0; i < pdev->num_resources; i++) { |
| 141 | struct resource *p, *r = &pdev->resource[i]; |
| 142 | |
| 143 | if (r->name == NULL) |
| 144 | r->name = pdev->dev.bus_id; |
| 145 | |
| 146 | p = r->parent; |
| 147 | if (!p) { |
| 148 | if (r->flags & IORESOURCE_MEM) |
| 149 | p = &iomem_resource; |
| 150 | else if (r->flags & IORESOURCE_IO) |
| 151 | p = &ioport_resource; |
| 152 | } |
| 153 | |
| 154 | if (p && request_resource(p, r)) { |
| 155 | printk(KERN_ERR |
| 156 | "%s: failed to claim resource %d\n", |
| 157 | pdev->dev.bus_id, i); |
| 158 | ret = -EBUSY; |
| 159 | goto failed; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | pr_debug("Registering platform device '%s'. Parent at %s\n", |
| 164 | pdev->dev.bus_id, pdev->dev.parent->bus_id); |
| 165 | |
| 166 | ret = device_register(&pdev->dev); |
| 167 | if (ret == 0) |
| 168 | return ret; |
| 169 | |
| 170 | failed: |
| 171 | while (--i >= 0) |
| 172 | if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO)) |
| 173 | release_resource(&pdev->resource[i]); |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * platform_device_unregister - remove a platform-level device |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 179 | * @pdev: platform device we're removing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | * |
| 181 | * Note that this function will also release all memory- and port-based |
| 182 | * resources owned by the device (@dev->resource). |
| 183 | */ |
| 184 | void platform_device_unregister(struct platform_device * pdev) |
| 185 | { |
| 186 | int i; |
| 187 | |
| 188 | if (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)) |
| 192 | release_resource(r); |
| 193 | } |
| 194 | |
| 195 | device_unregister(&pdev->dev); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | struct platform_object { |
| 200 | struct platform_device pdev; |
| 201 | struct resource resources[0]; |
| 202 | }; |
| 203 | |
| 204 | static void platform_device_release_simple(struct device *dev) |
| 205 | { |
| 206 | struct platform_device *pdev = to_platform_device(dev); |
| 207 | |
| 208 | kfree(container_of(pdev, struct platform_object, pdev)); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * platform_device_register_simple |
| 213 | * @name: base name of the device we're adding |
| 214 | * @id: instance id |
| 215 | * @res: set of resources that needs to be allocated for the device |
| 216 | * @num: number of resources |
| 217 | * |
| 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 |
| 222 | * to be dropped. |
| 223 | */ |
| 224 | struct platform_device *platform_device_register_simple(char *name, unsigned int id, |
| 225 | struct resource *res, unsigned int num) |
| 226 | { |
| 227 | struct platform_object *pobj; |
| 228 | int retval; |
| 229 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 230 | pobj = kzalloc(sizeof(*pobj) + sizeof(struct resource) * num, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (!pobj) { |
| 232 | retval = -ENOMEM; |
| 233 | goto error; |
| 234 | } |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | pobj->pdev.name = name; |
| 237 | pobj->pdev.id = id; |
| 238 | pobj->pdev.dev.release = platform_device_release_simple; |
| 239 | |
| 240 | if (num) { |
| 241 | memcpy(pobj->resources, res, sizeof(struct resource) * num); |
| 242 | pobj->pdev.resource = pobj->resources; |
| 243 | pobj->pdev.num_resources = num; |
| 244 | } |
| 245 | |
| 246 | retval = platform_device_register(&pobj->pdev); |
| 247 | if (retval) |
| 248 | goto error; |
| 249 | |
| 250 | return &pobj->pdev; |
| 251 | |
| 252 | error: |
| 253 | kfree(pobj); |
| 254 | return ERR_PTR(retval); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /** |
| 259 | * platform_match - bind platform device to platform driver. |
| 260 | * @dev: device. |
| 261 | * @drv: driver. |
| 262 | * |
| 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 |
| 270 | * they match or not. |
| 271 | */ |
| 272 | |
| 273 | static int platform_match(struct device * dev, struct device_driver * drv) |
| 274 | { |
| 275 | struct platform_device *pdev = container_of(dev, struct platform_device, dev); |
| 276 | |
| 277 | return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0); |
| 278 | } |
| 279 | |
| 280 | static int platform_suspend(struct device * dev, pm_message_t state) |
| 281 | { |
| 282 | int ret = 0; |
| 283 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 284 | if (dev->driver && dev->driver->suspend) |
| 285 | ret = dev->driver->suspend(dev, state); |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static int platform_resume(struct device * dev) |
| 291 | { |
| 292 | int ret = 0; |
| 293 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 294 | if (dev->driver && dev->driver->resume) |
| 295 | ret = dev->driver->resume(dev); |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | struct bus_type platform_bus_type = { |
| 301 | .name = "platform", |
| 302 | .match = platform_match, |
| 303 | .suspend = platform_suspend, |
| 304 | .resume = platform_resume, |
| 305 | }; |
| 306 | |
| 307 | int __init platform_bus_init(void) |
| 308 | { |
| 309 | device_register(&platform_bus); |
| 310 | return bus_register(&platform_bus_type); |
| 311 | } |
| 312 | |
| 313 | #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK |
| 314 | u64 dma_get_required_mask(struct device *dev) |
| 315 | { |
| 316 | u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT); |
| 317 | u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT)); |
| 318 | u64 mask; |
| 319 | |
| 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; |
| 324 | mask = low_totalram; |
| 325 | } else { |
| 326 | high_totalram = (1 << (fls(high_totalram) - 1)); |
| 327 | high_totalram += high_totalram - 1; |
| 328 | mask = (((u64)high_totalram) << 32) + 0xffffffff; |
| 329 | } |
| 330 | return mask & *dev->dma_mask; |
| 331 | } |
| 332 | EXPORT_SYMBOL_GPL(dma_get_required_mask); |
| 333 | #endif |
| 334 | |
| 335 | EXPORT_SYMBOL_GPL(platform_bus); |
| 336 | EXPORT_SYMBOL_GPL(platform_bus_type); |
Robert Schwebel | 46ea0d6 | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 337 | EXPORT_SYMBOL_GPL(platform_add_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |