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 | |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 19 | #include <linux/slab.h> |
Magnus Damm | 9d73022 | 2009-08-20 20:25:32 +0200 | [diff] [blame^] | 20 | #include <linux/pm_runtime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 22 | #include "base.h" |
| 23 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 24 | #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \ |
| 25 | driver)) |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | struct device platform_bus = { |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 28 | .init_name = "platform", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | }; |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 30 | EXPORT_SYMBOL_GPL(platform_bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 33 | * platform_get_resource - get a resource for a device |
| 34 | * @dev: platform device |
| 35 | * @type: resource type |
| 36 | * @num: resource index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 38 | struct resource *platform_get_resource(struct platform_device *dev, |
| 39 | unsigned int type, unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
| 41 | int i; |
| 42 | |
| 43 | for (i = 0; i < dev->num_resources; i++) { |
| 44 | struct resource *r = &dev->resource[i]; |
| 45 | |
Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 46 | if (type == resource_type(r) && num-- == 0) |
| 47 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | return NULL; |
| 50 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 51 | EXPORT_SYMBOL_GPL(platform_get_resource); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 54 | * platform_get_irq - get an IRQ for a device |
| 55 | * @dev: platform device |
| 56 | * @num: IRQ number index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | */ |
| 58 | int platform_get_irq(struct platform_device *dev, unsigned int num) |
| 59 | { |
| 60 | struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num); |
| 61 | |
David Vrabel | 305b322 | 2006-01-19 17:52:27 +0000 | [diff] [blame] | 62 | return r ? r->start : -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 64 | EXPORT_SYMBOL_GPL(platform_get_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 67 | * platform_get_resource_byname - get a resource for a device by name |
| 68 | * @dev: platform device |
| 69 | * @type: resource type |
| 70 | * @name: resource name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 72 | struct resource *platform_get_resource_byname(struct platform_device *dev, |
Linus Walleij | c0afe7b | 2009-04-27 02:38:16 +0200 | [diff] [blame] | 73 | unsigned int type, |
| 74 | const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
| 76 | int i; |
| 77 | |
| 78 | for (i = 0; i < dev->num_resources; i++) { |
| 79 | struct resource *r = &dev->resource[i]; |
| 80 | |
Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 81 | if (type == resource_type(r) && !strcmp(r->name, name)) |
| 82 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | return NULL; |
| 85 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 86 | EXPORT_SYMBOL_GPL(platform_get_resource_byname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 89 | * platform_get_irq - get an IRQ for a device |
| 90 | * @dev: platform device |
| 91 | * @name: IRQ name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
Linus Walleij | c0afe7b | 2009-04-27 02:38:16 +0200 | [diff] [blame] | 93 | int platform_get_irq_byname(struct platform_device *dev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 95 | struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, |
| 96 | name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
David Vrabel | 305b322 | 2006-01-19 17:52:27 +0000 | [diff] [blame] | 98 | return r ? r->start : -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 100 | EXPORT_SYMBOL_GPL(platform_get_irq_byname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 103 | * platform_add_devices - add a numbers of platform devices |
| 104 | * @devs: array of platform devices to add |
| 105 | * @num: number of platform devices in array |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | */ |
| 107 | int platform_add_devices(struct platform_device **devs, int num) |
| 108 | { |
| 109 | int i, ret = 0; |
| 110 | |
| 111 | for (i = 0; i < num; i++) { |
| 112 | ret = platform_device_register(devs[i]); |
| 113 | if (ret) { |
| 114 | while (--i >= 0) |
| 115 | platform_device_unregister(devs[i]); |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return ret; |
| 121 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 122 | EXPORT_SYMBOL_GPL(platform_add_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 124 | struct platform_object { |
| 125 | struct platform_device pdev; |
| 126 | char name[1]; |
| 127 | }; |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 130 | * platform_device_put |
| 131 | * @pdev: platform device to free |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 132 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 133 | * Free all memory associated with a platform device. This function must |
| 134 | * _only_ be externally called in error cases. All other usage is a bug. |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 135 | */ |
| 136 | void platform_device_put(struct platform_device *pdev) |
| 137 | { |
| 138 | if (pdev) |
| 139 | put_device(&pdev->dev); |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(platform_device_put); |
| 142 | |
| 143 | static void platform_device_release(struct device *dev) |
| 144 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 145 | struct platform_object *pa = container_of(dev, struct platform_object, |
| 146 | pdev.dev); |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 147 | |
| 148 | kfree(pa->pdev.dev.platform_data); |
| 149 | kfree(pa->pdev.resource); |
| 150 | kfree(pa); |
| 151 | } |
| 152 | |
| 153 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 154 | * platform_device_alloc |
| 155 | * @name: base name of the device we're adding |
| 156 | * @id: instance id |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 157 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 158 | * Create a platform device object which can have other objects attached |
| 159 | * to it, and which will have attached objects freed when it is released. |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 160 | */ |
Jean Delvare | 1359555e | 2007-09-09 12:54:16 +0200 | [diff] [blame] | 161 | struct platform_device *platform_device_alloc(const char *name, int id) |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 162 | { |
| 163 | struct platform_object *pa; |
| 164 | |
| 165 | pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL); |
| 166 | if (pa) { |
| 167 | strcpy(pa->name, name); |
| 168 | pa->pdev.name = pa->name; |
| 169 | pa->pdev.id = id; |
| 170 | device_initialize(&pa->pdev.dev); |
| 171 | pa->pdev.dev.release = platform_device_release; |
| 172 | } |
| 173 | |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 174 | return pa ? &pa->pdev : NULL; |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 175 | } |
| 176 | EXPORT_SYMBOL_GPL(platform_device_alloc); |
| 177 | |
| 178 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 179 | * platform_device_add_resources |
| 180 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
| 181 | * @res: set of resources that needs to be allocated for the device |
| 182 | * @num: number of resources |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 183 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 184 | * Add a copy of the resources to the platform device. The memory |
| 185 | * associated with the resources will be freed when the platform device is |
| 186 | * released. |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 187 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 188 | int platform_device_add_resources(struct platform_device *pdev, |
| 189 | struct resource *res, unsigned int num) |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 190 | { |
| 191 | struct resource *r; |
| 192 | |
| 193 | r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL); |
| 194 | if (r) { |
| 195 | memcpy(r, res, sizeof(struct resource) * num); |
| 196 | pdev->resource = r; |
| 197 | pdev->num_resources = num; |
| 198 | } |
| 199 | return r ? 0 : -ENOMEM; |
| 200 | } |
| 201 | EXPORT_SYMBOL_GPL(platform_device_add_resources); |
| 202 | |
| 203 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 204 | * platform_device_add_data |
| 205 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
| 206 | * @data: platform specific data for this platform device |
| 207 | * @size: size of platform specific data |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 208 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 209 | * Add a copy of platform specific data to the platform device's |
| 210 | * platform_data pointer. The memory associated with the platform data |
| 211 | * will be freed when the platform device is released. |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 212 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 213 | int platform_device_add_data(struct platform_device *pdev, const void *data, |
| 214 | size_t size) |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 215 | { |
| 216 | void *d; |
| 217 | |
| 218 | d = kmalloc(size, GFP_KERNEL); |
| 219 | if (d) { |
| 220 | memcpy(d, data, size); |
| 221 | pdev->dev.platform_data = d; |
| 222 | } |
| 223 | return d ? 0 : -ENOMEM; |
| 224 | } |
| 225 | EXPORT_SYMBOL_GPL(platform_device_add_data); |
| 226 | |
| 227 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 228 | * platform_device_add - add a platform device to device hierarchy |
| 229 | * @pdev: platform device we're adding |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 231 | * This is part 2 of platform_device_register(), though may be called |
| 232 | * separately _iff_ pdev was allocated by platform_device_alloc(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | */ |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 234 | int platform_device_add(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
| 236 | int i, ret = 0; |
| 237 | |
| 238 | if (!pdev) |
| 239 | return -EINVAL; |
| 240 | |
| 241 | if (!pdev->dev.parent) |
| 242 | pdev->dev.parent = &platform_bus; |
| 243 | |
| 244 | pdev->dev.bus = &platform_bus_type; |
| 245 | |
| 246 | if (pdev->id != -1) |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 247 | dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | else |
Greg Kroah-Hartman | acc0e90 | 2009-06-02 15:39:55 -0700 | [diff] [blame] | 249 | dev_set_name(&pdev->dev, "%s", pdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | for (i = 0; i < pdev->num_resources; i++) { |
| 252 | struct resource *p, *r = &pdev->resource[i]; |
| 253 | |
| 254 | if (r->name == NULL) |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 255 | r->name = dev_name(&pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | p = r->parent; |
| 258 | if (!p) { |
Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 259 | if (resource_type(r) == IORESOURCE_MEM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | p = &iomem_resource; |
Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 261 | else if (resource_type(r) == IORESOURCE_IO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | p = &ioport_resource; |
| 263 | } |
| 264 | |
Kumar Gala | d960bb4 | 2005-11-28 10:15:39 -0600 | [diff] [blame] | 265 | if (p && insert_resource(p, r)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | printk(KERN_ERR |
| 267 | "%s: failed to claim resource %d\n", |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 268 | dev_name(&pdev->dev), i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | ret = -EBUSY; |
| 270 | goto failed; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | pr_debug("Registering platform device '%s'. Parent at %s\n", |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 275 | dev_name(&pdev->dev), dev_name(pdev->dev.parent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Russell King | e391553 | 2006-05-06 08:15:26 +0100 | [diff] [blame] | 277 | ret = device_add(&pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if (ret == 0) |
| 279 | return ret; |
| 280 | |
| 281 | failed: |
Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 282 | while (--i >= 0) { |
| 283 | struct resource *r = &pdev->resource[i]; |
| 284 | unsigned long type = resource_type(r); |
| 285 | |
| 286 | if (type == IORESOURCE_MEM || type == IORESOURCE_IO) |
| 287 | release_resource(r); |
| 288 | } |
| 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | return ret; |
| 291 | } |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 292 | EXPORT_SYMBOL_GPL(platform_device_add); |
| 293 | |
| 294 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 295 | * platform_device_del - remove a platform-level device |
| 296 | * @pdev: platform device we're removing |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 297 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 298 | * Note that this function will also release all memory- and port-based |
| 299 | * resources owned by the device (@dev->resource). This function must |
| 300 | * _only_ be externally called in error cases. All other usage is a bug. |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 301 | */ |
| 302 | void platform_device_del(struct platform_device *pdev) |
| 303 | { |
| 304 | int i; |
| 305 | |
| 306 | if (pdev) { |
Jean Delvare | dc4c15d | 2007-05-02 20:55:54 +0200 | [diff] [blame] | 307 | device_del(&pdev->dev); |
| 308 | |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 309 | for (i = 0; i < pdev->num_resources; i++) { |
| 310 | struct resource *r = &pdev->resource[i]; |
Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 311 | unsigned long type = resource_type(r); |
| 312 | |
| 313 | if (type == IORESOURCE_MEM || type == IORESOURCE_IO) |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 314 | release_resource(r); |
| 315 | } |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | EXPORT_SYMBOL_GPL(platform_device_del); |
| 319 | |
| 320 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 321 | * platform_device_register - add a platform-level device |
| 322 | * @pdev: platform device we're adding |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 323 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 324 | int platform_device_register(struct platform_device *pdev) |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 325 | { |
| 326 | device_initialize(&pdev->dev); |
| 327 | return platform_device_add(pdev); |
| 328 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 329 | EXPORT_SYMBOL_GPL(platform_device_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
| 331 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 332 | * platform_device_unregister - unregister a platform-level device |
| 333 | * @pdev: platform device we're unregistering |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 335 | * Unregistration is done in 2 steps. First we release all resources |
| 336 | * and remove it from the subsystem, then we drop reference count by |
| 337 | * calling platform_device_put(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 339 | void platform_device_unregister(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | { |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 341 | platform_device_del(pdev); |
| 342 | platform_device_put(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 344 | EXPORT_SYMBOL_GPL(platform_device_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 347 | * platform_device_register_simple |
| 348 | * @name: base name of the device we're adding |
| 349 | * @id: instance id |
| 350 | * @res: set of resources that needs to be allocated for the device |
| 351 | * @num: number of resources |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 353 | * This function creates a simple platform device that requires minimal |
| 354 | * resource and memory management. Canned release function freeing memory |
| 355 | * allocated for the device allows drivers using such devices to be |
| 356 | * unloaded without waiting for the last reference to the device to be |
| 357 | * dropped. |
David Brownell | 49a4ec1 | 2007-05-08 00:29:39 -0700 | [diff] [blame] | 358 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 359 | * This interface is primarily intended for use with legacy drivers which |
| 360 | * probe hardware directly. Because such drivers create sysfs device nodes |
| 361 | * themselves, rather than letting system infrastructure handle such device |
| 362 | * enumeration tasks, they don't fully conform to the Linux driver model. |
| 363 | * In particular, when such drivers are built as modules, they can't be |
| 364 | * "hotplugged". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 366 | struct platform_device *platform_device_register_simple(const char *name, |
| 367 | int id, |
| 368 | struct resource *res, |
| 369 | unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 371 | struct platform_device *pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | int retval; |
| 373 | |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 374 | pdev = platform_device_alloc(name, id); |
| 375 | if (!pdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | retval = -ENOMEM; |
| 377 | goto error; |
| 378 | } |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (num) { |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 381 | retval = platform_device_add_resources(pdev, res, num); |
| 382 | if (retval) |
| 383 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 386 | retval = platform_device_add(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (retval) |
| 388 | goto error; |
| 389 | |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 390 | return pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | error: |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 393 | platform_device_put(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | return ERR_PTR(retval); |
| 395 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 396 | EXPORT_SYMBOL_GPL(platform_device_register_simple); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 398 | /** |
| 399 | * platform_device_register_data |
| 400 | * @parent: parent device for the device we're adding |
| 401 | * @name: base name of the device we're adding |
| 402 | * @id: instance id |
| 403 | * @data: platform specific data for this platform device |
| 404 | * @size: size of platform specific data |
| 405 | * |
| 406 | * This function creates a simple platform device that requires minimal |
| 407 | * resource and memory management. Canned release function freeing memory |
| 408 | * allocated for the device allows drivers using such devices to be |
| 409 | * unloaded without waiting for the last reference to the device to be |
| 410 | * dropped. |
| 411 | */ |
| 412 | struct platform_device *platform_device_register_data( |
| 413 | struct device *parent, |
| 414 | const char *name, int id, |
| 415 | const void *data, size_t size) |
| 416 | { |
| 417 | struct platform_device *pdev; |
| 418 | int retval; |
| 419 | |
| 420 | pdev = platform_device_alloc(name, id); |
| 421 | if (!pdev) { |
| 422 | retval = -ENOMEM; |
| 423 | goto error; |
| 424 | } |
| 425 | |
| 426 | pdev->dev.parent = parent; |
| 427 | |
| 428 | if (size) { |
| 429 | retval = platform_device_add_data(pdev, data, size); |
| 430 | if (retval) |
| 431 | goto error; |
| 432 | } |
| 433 | |
| 434 | retval = platform_device_add(pdev); |
| 435 | if (retval) |
| 436 | goto error; |
| 437 | |
| 438 | return pdev; |
| 439 | |
| 440 | error: |
| 441 | platform_device_put(pdev); |
| 442 | return ERR_PTR(retval); |
| 443 | } |
| 444 | |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 445 | static int platform_drv_probe(struct device *_dev) |
| 446 | { |
| 447 | struct platform_driver *drv = to_platform_driver(_dev->driver); |
| 448 | struct platform_device *dev = to_platform_device(_dev); |
| 449 | |
| 450 | return drv->probe(dev); |
| 451 | } |
| 452 | |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 453 | static int platform_drv_probe_fail(struct device *_dev) |
| 454 | { |
| 455 | return -ENXIO; |
| 456 | } |
| 457 | |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 458 | static int platform_drv_remove(struct device *_dev) |
| 459 | { |
| 460 | struct platform_driver *drv = to_platform_driver(_dev->driver); |
| 461 | struct platform_device *dev = to_platform_device(_dev); |
| 462 | |
| 463 | return drv->remove(dev); |
| 464 | } |
| 465 | |
| 466 | static void platform_drv_shutdown(struct device *_dev) |
| 467 | { |
| 468 | struct platform_driver *drv = to_platform_driver(_dev->driver); |
| 469 | struct platform_device *dev = to_platform_device(_dev); |
| 470 | |
| 471 | drv->shutdown(dev); |
| 472 | } |
| 473 | |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 474 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 475 | * platform_driver_register |
| 476 | * @drv: platform driver structure |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 477 | */ |
| 478 | int platform_driver_register(struct platform_driver *drv) |
| 479 | { |
| 480 | drv->driver.bus = &platform_bus_type; |
| 481 | if (drv->probe) |
| 482 | drv->driver.probe = platform_drv_probe; |
| 483 | if (drv->remove) |
| 484 | drv->driver.remove = platform_drv_remove; |
| 485 | if (drv->shutdown) |
| 486 | drv->driver.shutdown = platform_drv_shutdown; |
Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 487 | |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 488 | return driver_register(&drv->driver); |
| 489 | } |
| 490 | EXPORT_SYMBOL_GPL(platform_driver_register); |
| 491 | |
| 492 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 493 | * platform_driver_unregister |
| 494 | * @drv: platform driver structure |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 495 | */ |
| 496 | void platform_driver_unregister(struct platform_driver *drv) |
| 497 | { |
| 498 | driver_unregister(&drv->driver); |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(platform_driver_unregister); |
| 501 | |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 502 | /** |
| 503 | * platform_driver_probe - register driver for non-hotpluggable device |
| 504 | * @drv: platform driver structure |
| 505 | * @probe: the driver probe routine, probably from an __init section |
| 506 | * |
| 507 | * Use this instead of platform_driver_register() when you know the device |
| 508 | * is not hotpluggable and has already been registered, and you want to |
| 509 | * remove its run-once probe() infrastructure from memory after the driver |
| 510 | * has bound to the device. |
| 511 | * |
| 512 | * One typical use for this would be with drivers for controllers integrated |
| 513 | * into system-on-chip processors, where the controller devices have been |
| 514 | * configured as part of board setup. |
| 515 | * |
| 516 | * Returns zero if the driver registered and bound to a device, else returns |
| 517 | * a negative error code and with the driver not registered. |
| 518 | */ |
Andrew Morton | c63e078 | 2006-12-04 14:56:36 -0800 | [diff] [blame] | 519 | int __init_or_module platform_driver_probe(struct platform_driver *drv, |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 520 | int (*probe)(struct platform_device *)) |
| 521 | { |
| 522 | int retval, code; |
| 523 | |
| 524 | /* temporary section violation during probe() */ |
| 525 | drv->probe = probe; |
| 526 | retval = code = platform_driver_register(drv); |
| 527 | |
| 528 | /* Fixup that section violation, being paranoid about code scanning |
| 529 | * the list of drivers in order to probe new devices. Check to see |
| 530 | * if the probe was successful, and make sure any forced probes of |
| 531 | * new devices fail. |
| 532 | */ |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 533 | spin_lock(&platform_bus_type.p->klist_drivers.k_lock); |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 534 | drv->probe = NULL; |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 535 | if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list)) |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 536 | retval = -ENODEV; |
| 537 | drv->driver.probe = platform_drv_probe_fail; |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 538 | spin_unlock(&platform_bus_type.p->klist_drivers.k_lock); |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 539 | |
| 540 | if (code != retval) |
| 541 | platform_driver_unregister(drv); |
| 542 | return retval; |
| 543 | } |
| 544 | EXPORT_SYMBOL_GPL(platform_driver_probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 546 | /* modalias support enables more hands-off userspace setup: |
| 547 | * (a) environment variable lets new-style hotplug events work once system is |
| 548 | * fully running: "modprobe $MODALIAS" |
| 549 | * (b) sysfs attribute lets new-style coldplug recover from hotplug events |
| 550 | * mishandled before system is fully running: "modprobe $(cat modalias)" |
| 551 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 552 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
| 553 | char *buf) |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 554 | { |
| 555 | struct platform_device *pdev = to_platform_device(dev); |
Kay Sievers | 43cc71e | 2007-08-18 04:40:39 +0200 | [diff] [blame] | 556 | int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name); |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 557 | |
| 558 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
| 559 | } |
| 560 | |
| 561 | static struct device_attribute platform_dev_attrs[] = { |
| 562 | __ATTR_RO(modalias), |
| 563 | __ATTR_NULL, |
| 564 | }; |
| 565 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 566 | static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 567 | { |
| 568 | struct platform_device *pdev = to_platform_device(dev); |
| 569 | |
Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 570 | add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, |
| 571 | (pdev->id_entry) ? pdev->id_entry->name : pdev->name); |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | |
Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 575 | static const struct platform_device_id *platform_match_id( |
| 576 | struct platform_device_id *id, |
| 577 | struct platform_device *pdev) |
| 578 | { |
| 579 | while (id->name[0]) { |
| 580 | if (strcmp(pdev->name, id->name) == 0) { |
| 581 | pdev->id_entry = id; |
| 582 | return id; |
| 583 | } |
| 584 | id++; |
| 585 | } |
| 586 | return NULL; |
| 587 | } |
| 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 590 | * platform_match - bind platform device to platform driver. |
| 591 | * @dev: device. |
| 592 | * @drv: driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 594 | * Platform device IDs are assumed to be encoded like this: |
| 595 | * "<name><instance>", where <name> is a short description of the type of |
| 596 | * device, like "pci" or "floppy", and <instance> is the enumerated |
| 597 | * instance of the device, like '0' or '42'. Driver IDs are simply |
| 598 | * "<name>". So, extract the <name> from the platform_device structure, |
| 599 | * and compare it against the name of the driver. Return whether they match |
| 600 | * or not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 602 | static int platform_match(struct device *dev, struct device_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | { |
Eric Miao | 71b3e0c | 2009-01-31 22:47:44 +0800 | [diff] [blame] | 604 | struct platform_device *pdev = to_platform_device(dev); |
Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 605 | struct platform_driver *pdrv = to_platform_driver(drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 607 | /* match against the id table first */ |
| 608 | if (pdrv->id_table) |
| 609 | return platform_match_id(pdrv->id_table, pdev) != NULL; |
| 610 | |
| 611 | /* fall-back to driver name match */ |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 612 | return (strcmp(pdev->name, drv->name) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 615 | #ifdef CONFIG_PM_SLEEP |
| 616 | |
| 617 | static int platform_legacy_suspend(struct device *dev, pm_message_t mesg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 619 | struct platform_driver *pdrv = to_platform_driver(dev->driver); |
| 620 | struct platform_device *pdev = to_platform_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | int ret = 0; |
| 622 | |
Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 623 | if (dev->driver && pdrv->suspend) |
| 624 | ret = pdrv->suspend(pdev, mesg); |
David Brownell | 386415d | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 625 | |
| 626 | return ret; |
| 627 | } |
| 628 | |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 629 | static int platform_legacy_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | { |
Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 631 | struct platform_driver *pdrv = to_platform_driver(dev->driver); |
| 632 | struct platform_device *pdev = to_platform_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | int ret = 0; |
| 634 | |
Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 635 | if (dev->driver && pdrv->resume) |
| 636 | ret = pdrv->resume(pdev); |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | return ret; |
| 639 | } |
| 640 | |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 641 | static int platform_pm_prepare(struct device *dev) |
| 642 | { |
| 643 | struct device_driver *drv = dev->driver; |
| 644 | int ret = 0; |
| 645 | |
| 646 | if (drv && drv->pm && drv->pm->prepare) |
| 647 | ret = drv->pm->prepare(dev); |
| 648 | |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | static void platform_pm_complete(struct device *dev) |
| 653 | { |
| 654 | struct device_driver *drv = dev->driver; |
| 655 | |
| 656 | if (drv && drv->pm && drv->pm->complete) |
| 657 | drv->pm->complete(dev); |
| 658 | } |
| 659 | |
Magnus Damm | 9d73022 | 2009-08-20 20:25:32 +0200 | [diff] [blame^] | 660 | #else /* !CONFIG_PM_SLEEP */ |
| 661 | |
| 662 | #define platform_pm_prepare NULL |
| 663 | #define platform_pm_complete NULL |
| 664 | |
| 665 | #endif /* !CONFIG_PM_SLEEP */ |
| 666 | |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 667 | #ifdef CONFIG_SUSPEND |
| 668 | |
| 669 | static int platform_pm_suspend(struct device *dev) |
| 670 | { |
| 671 | struct device_driver *drv = dev->driver; |
| 672 | int ret = 0; |
| 673 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 674 | if (!drv) |
| 675 | return 0; |
| 676 | |
| 677 | if (drv->pm) { |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 678 | if (drv->pm->suspend) |
| 679 | ret = drv->pm->suspend(dev); |
| 680 | } else { |
| 681 | ret = platform_legacy_suspend(dev, PMSG_SUSPEND); |
| 682 | } |
| 683 | |
| 684 | return ret; |
| 685 | } |
| 686 | |
| 687 | static int platform_pm_suspend_noirq(struct device *dev) |
| 688 | { |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 689 | struct device_driver *drv = dev->driver; |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 690 | int ret = 0; |
| 691 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 692 | if (!drv) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 693 | return 0; |
| 694 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 695 | if (drv->pm) { |
| 696 | if (drv->pm->suspend_noirq) |
| 697 | ret = drv->pm->suspend_noirq(dev); |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | return ret; |
| 701 | } |
| 702 | |
| 703 | static int platform_pm_resume(struct device *dev) |
| 704 | { |
| 705 | struct device_driver *drv = dev->driver; |
| 706 | int ret = 0; |
| 707 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 708 | if (!drv) |
| 709 | return 0; |
| 710 | |
| 711 | if (drv->pm) { |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 712 | if (drv->pm->resume) |
| 713 | ret = drv->pm->resume(dev); |
| 714 | } else { |
| 715 | ret = platform_legacy_resume(dev); |
| 716 | } |
| 717 | |
| 718 | return ret; |
| 719 | } |
| 720 | |
| 721 | static int platform_pm_resume_noirq(struct device *dev) |
| 722 | { |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 723 | struct device_driver *drv = dev->driver; |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 724 | int ret = 0; |
| 725 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 726 | if (!drv) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 727 | return 0; |
| 728 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 729 | if (drv->pm) { |
| 730 | if (drv->pm->resume_noirq) |
| 731 | ret = drv->pm->resume_noirq(dev); |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | return ret; |
| 735 | } |
| 736 | |
| 737 | #else /* !CONFIG_SUSPEND */ |
| 738 | |
| 739 | #define platform_pm_suspend NULL |
| 740 | #define platform_pm_resume NULL |
| 741 | #define platform_pm_suspend_noirq NULL |
| 742 | #define platform_pm_resume_noirq NULL |
| 743 | |
| 744 | #endif /* !CONFIG_SUSPEND */ |
| 745 | |
| 746 | #ifdef CONFIG_HIBERNATION |
| 747 | |
| 748 | static int platform_pm_freeze(struct device *dev) |
| 749 | { |
| 750 | struct device_driver *drv = dev->driver; |
| 751 | int ret = 0; |
| 752 | |
| 753 | if (!drv) |
| 754 | return 0; |
| 755 | |
| 756 | if (drv->pm) { |
| 757 | if (drv->pm->freeze) |
| 758 | ret = drv->pm->freeze(dev); |
| 759 | } else { |
| 760 | ret = platform_legacy_suspend(dev, PMSG_FREEZE); |
| 761 | } |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | static int platform_pm_freeze_noirq(struct device *dev) |
| 767 | { |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 768 | struct device_driver *drv = dev->driver; |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 769 | int ret = 0; |
| 770 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 771 | if (!drv) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 772 | return 0; |
| 773 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 774 | if (drv->pm) { |
| 775 | if (drv->pm->freeze_noirq) |
| 776 | ret = drv->pm->freeze_noirq(dev); |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | return ret; |
| 780 | } |
| 781 | |
| 782 | static int platform_pm_thaw(struct device *dev) |
| 783 | { |
| 784 | struct device_driver *drv = dev->driver; |
| 785 | int ret = 0; |
| 786 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 787 | if (!drv) |
| 788 | return 0; |
| 789 | |
| 790 | if (drv->pm) { |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 791 | if (drv->pm->thaw) |
| 792 | ret = drv->pm->thaw(dev); |
| 793 | } else { |
| 794 | ret = platform_legacy_resume(dev); |
| 795 | } |
| 796 | |
| 797 | return ret; |
| 798 | } |
| 799 | |
| 800 | static int platform_pm_thaw_noirq(struct device *dev) |
| 801 | { |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 802 | struct device_driver *drv = dev->driver; |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 803 | int ret = 0; |
| 804 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 805 | if (!drv) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 806 | return 0; |
| 807 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 808 | if (drv->pm) { |
| 809 | if (drv->pm->thaw_noirq) |
| 810 | ret = drv->pm->thaw_noirq(dev); |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | static int platform_pm_poweroff(struct device *dev) |
| 817 | { |
| 818 | struct device_driver *drv = dev->driver; |
| 819 | int ret = 0; |
| 820 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 821 | if (!drv) |
| 822 | return 0; |
| 823 | |
| 824 | if (drv->pm) { |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 825 | if (drv->pm->poweroff) |
| 826 | ret = drv->pm->poweroff(dev); |
| 827 | } else { |
| 828 | ret = platform_legacy_suspend(dev, PMSG_HIBERNATE); |
| 829 | } |
| 830 | |
| 831 | return ret; |
| 832 | } |
| 833 | |
| 834 | static int platform_pm_poweroff_noirq(struct device *dev) |
| 835 | { |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 836 | struct device_driver *drv = dev->driver; |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 837 | int ret = 0; |
| 838 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 839 | if (!drv) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 840 | return 0; |
| 841 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 842 | if (drv->pm) { |
| 843 | if (drv->pm->poweroff_noirq) |
| 844 | ret = drv->pm->poweroff_noirq(dev); |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | return ret; |
| 848 | } |
| 849 | |
| 850 | static int platform_pm_restore(struct device *dev) |
| 851 | { |
| 852 | struct device_driver *drv = dev->driver; |
| 853 | int ret = 0; |
| 854 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 855 | if (!drv) |
| 856 | return 0; |
| 857 | |
| 858 | if (drv->pm) { |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 859 | if (drv->pm->restore) |
| 860 | ret = drv->pm->restore(dev); |
| 861 | } else { |
| 862 | ret = platform_legacy_resume(dev); |
| 863 | } |
| 864 | |
| 865 | return ret; |
| 866 | } |
| 867 | |
| 868 | static int platform_pm_restore_noirq(struct device *dev) |
| 869 | { |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 870 | struct device_driver *drv = dev->driver; |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 871 | int ret = 0; |
| 872 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 873 | if (!drv) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 874 | return 0; |
| 875 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 876 | if (drv->pm) { |
| 877 | if (drv->pm->restore_noirq) |
| 878 | ret = drv->pm->restore_noirq(dev); |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | #else /* !CONFIG_HIBERNATION */ |
| 885 | |
| 886 | #define platform_pm_freeze NULL |
| 887 | #define platform_pm_thaw NULL |
| 888 | #define platform_pm_poweroff NULL |
| 889 | #define platform_pm_restore NULL |
| 890 | #define platform_pm_freeze_noirq NULL |
| 891 | #define platform_pm_thaw_noirq NULL |
| 892 | #define platform_pm_poweroff_noirq NULL |
| 893 | #define platform_pm_restore_noirq NULL |
| 894 | |
| 895 | #endif /* !CONFIG_HIBERNATION */ |
| 896 | |
Magnus Damm | 9d73022 | 2009-08-20 20:25:32 +0200 | [diff] [blame^] | 897 | #ifdef CONFIG_PM_RUNTIME |
| 898 | |
| 899 | int __weak platform_pm_runtime_suspend(struct device *dev) |
| 900 | { |
| 901 | return -ENOSYS; |
| 902 | }; |
| 903 | |
| 904 | int __weak platform_pm_runtime_resume(struct device *dev) |
| 905 | { |
| 906 | return -ENOSYS; |
| 907 | }; |
| 908 | |
| 909 | int __weak platform_pm_runtime_idle(struct device *dev) |
| 910 | { |
| 911 | return -ENOSYS; |
| 912 | }; |
| 913 | |
| 914 | #else /* !CONFIG_PM_RUNTIME */ |
| 915 | |
| 916 | #define platform_pm_runtime_suspend NULL |
| 917 | #define platform_pm_runtime_resume NULL |
| 918 | #define platform_pm_runtime_idle NULL |
| 919 | |
| 920 | #endif /* !CONFIG_PM_RUNTIME */ |
| 921 | |
Dmitry Torokhov | d9ab771 | 2009-07-22 00:37:25 +0200 | [diff] [blame] | 922 | static const struct dev_pm_ops platform_dev_pm_ops = { |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 923 | .prepare = platform_pm_prepare, |
| 924 | .complete = platform_pm_complete, |
| 925 | .suspend = platform_pm_suspend, |
| 926 | .resume = platform_pm_resume, |
| 927 | .freeze = platform_pm_freeze, |
| 928 | .thaw = platform_pm_thaw, |
| 929 | .poweroff = platform_pm_poweroff, |
| 930 | .restore = platform_pm_restore, |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 931 | .suspend_noirq = platform_pm_suspend_noirq, |
| 932 | .resume_noirq = platform_pm_resume_noirq, |
| 933 | .freeze_noirq = platform_pm_freeze_noirq, |
| 934 | .thaw_noirq = platform_pm_thaw_noirq, |
| 935 | .poweroff_noirq = platform_pm_poweroff_noirq, |
| 936 | .restore_noirq = platform_pm_restore_noirq, |
Magnus Damm | 9d73022 | 2009-08-20 20:25:32 +0200 | [diff] [blame^] | 937 | .runtime_suspend = platform_pm_runtime_suspend, |
| 938 | .runtime_resume = platform_pm_runtime_resume, |
| 939 | .runtime_idle = platform_pm_runtime_idle, |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 940 | }; |
| 941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | struct bus_type platform_bus_type = { |
| 943 | .name = "platform", |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 944 | .dev_attrs = platform_dev_attrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | .match = platform_match, |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 946 | .uevent = platform_uevent, |
Magnus Damm | 9d73022 | 2009-08-20 20:25:32 +0200 | [diff] [blame^] | 947 | .pm = &platform_dev_pm_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | }; |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 949 | EXPORT_SYMBOL_GPL(platform_bus_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
| 951 | int __init platform_bus_init(void) |
| 952 | { |
Cornelia Huck | fbfb144 | 2006-11-27 10:35:08 +0100 | [diff] [blame] | 953 | int error; |
| 954 | |
Magnus Damm | 1397709 | 2009-03-30 14:37:25 -0700 | [diff] [blame] | 955 | early_platform_cleanup(); |
| 956 | |
Cornelia Huck | fbfb144 | 2006-11-27 10:35:08 +0100 | [diff] [blame] | 957 | error = device_register(&platform_bus); |
| 958 | if (error) |
| 959 | return error; |
| 960 | error = bus_register(&platform_bus_type); |
| 961 | if (error) |
| 962 | device_unregister(&platform_bus); |
| 963 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK |
| 967 | u64 dma_get_required_mask(struct device *dev) |
| 968 | { |
| 969 | u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT); |
| 970 | u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT)); |
| 971 | u64 mask; |
| 972 | |
| 973 | if (!high_totalram) { |
| 974 | /* convert to mask just covering totalram */ |
| 975 | low_totalram = (1 << (fls(low_totalram) - 1)); |
| 976 | low_totalram += low_totalram - 1; |
| 977 | mask = low_totalram; |
| 978 | } else { |
| 979 | high_totalram = (1 << (fls(high_totalram) - 1)); |
| 980 | high_totalram += high_totalram - 1; |
| 981 | mask = (((u64)high_totalram) << 32) + 0xffffffff; |
| 982 | } |
James Bottomley | e88a0c2 | 2008-03-09 11:57:56 -0500 | [diff] [blame] | 983 | return mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | } |
| 985 | EXPORT_SYMBOL_GPL(dma_get_required_mask); |
| 986 | #endif |
Magnus Damm | 1397709 | 2009-03-30 14:37:25 -0700 | [diff] [blame] | 987 | |
| 988 | static __initdata LIST_HEAD(early_platform_driver_list); |
| 989 | static __initdata LIST_HEAD(early_platform_device_list); |
| 990 | |
| 991 | /** |
| 992 | * early_platform_driver_register |
Randy Dunlap | d86c130 | 2009-04-21 07:22:53 -0700 | [diff] [blame] | 993 | * @epdrv: early_platform driver structure |
Magnus Damm | 1397709 | 2009-03-30 14:37:25 -0700 | [diff] [blame] | 994 | * @buf: string passed from early_param() |
| 995 | */ |
| 996 | int __init early_platform_driver_register(struct early_platform_driver *epdrv, |
| 997 | char *buf) |
| 998 | { |
| 999 | unsigned long index; |
| 1000 | int n; |
| 1001 | |
| 1002 | /* Simply add the driver to the end of the global list. |
| 1003 | * Drivers will by default be put on the list in compiled-in order. |
| 1004 | */ |
| 1005 | if (!epdrv->list.next) { |
| 1006 | INIT_LIST_HEAD(&epdrv->list); |
| 1007 | list_add_tail(&epdrv->list, &early_platform_driver_list); |
| 1008 | } |
| 1009 | |
| 1010 | /* If the user has specified device then make sure the driver |
| 1011 | * gets prioritized. The driver of the last device specified on |
| 1012 | * command line will be put first on the list. |
| 1013 | */ |
| 1014 | n = strlen(epdrv->pdrv->driver.name); |
| 1015 | if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) { |
| 1016 | list_move(&epdrv->list, &early_platform_driver_list); |
| 1017 | |
| 1018 | if (!strcmp(buf, epdrv->pdrv->driver.name)) |
| 1019 | epdrv->requested_id = -1; |
| 1020 | else if (buf[n] == '.' && strict_strtoul(&buf[n + 1], 10, |
| 1021 | &index) == 0) |
| 1022 | epdrv->requested_id = index; |
| 1023 | else |
| 1024 | epdrv->requested_id = EARLY_PLATFORM_ID_ERROR; |
| 1025 | } |
| 1026 | |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * early_platform_add_devices - add a numbers of early platform devices |
| 1032 | * @devs: array of early platform devices to add |
| 1033 | * @num: number of early platform devices in array |
| 1034 | */ |
| 1035 | void __init early_platform_add_devices(struct platform_device **devs, int num) |
| 1036 | { |
| 1037 | struct device *dev; |
| 1038 | int i; |
| 1039 | |
| 1040 | /* simply add the devices to list */ |
| 1041 | for (i = 0; i < num; i++) { |
| 1042 | dev = &devs[i]->dev; |
| 1043 | |
| 1044 | if (!dev->devres_head.next) { |
| 1045 | INIT_LIST_HEAD(&dev->devres_head); |
| 1046 | list_add_tail(&dev->devres_head, |
| 1047 | &early_platform_device_list); |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | /** |
| 1053 | * early_platform_driver_register_all |
| 1054 | * @class_str: string to identify early platform driver class |
| 1055 | */ |
| 1056 | void __init early_platform_driver_register_all(char *class_str) |
| 1057 | { |
| 1058 | /* The "class_str" parameter may or may not be present on the kernel |
| 1059 | * command line. If it is present then there may be more than one |
| 1060 | * matching parameter. |
| 1061 | * |
| 1062 | * Since we register our early platform drivers using early_param() |
| 1063 | * we need to make sure that they also get registered in the case |
| 1064 | * when the parameter is missing from the kernel command line. |
| 1065 | * |
| 1066 | * We use parse_early_options() to make sure the early_param() gets |
| 1067 | * called at least once. The early_param() may be called more than |
| 1068 | * once since the name of the preferred device may be specified on |
| 1069 | * the kernel command line. early_platform_driver_register() handles |
| 1070 | * this case for us. |
| 1071 | */ |
| 1072 | parse_early_options(class_str); |
| 1073 | } |
| 1074 | |
| 1075 | /** |
| 1076 | * early_platform_match |
Randy Dunlap | d86c130 | 2009-04-21 07:22:53 -0700 | [diff] [blame] | 1077 | * @epdrv: early platform driver structure |
Magnus Damm | 1397709 | 2009-03-30 14:37:25 -0700 | [diff] [blame] | 1078 | * @id: id to match against |
| 1079 | */ |
| 1080 | static __init struct platform_device * |
| 1081 | early_platform_match(struct early_platform_driver *epdrv, int id) |
| 1082 | { |
| 1083 | struct platform_device *pd; |
| 1084 | |
| 1085 | list_for_each_entry(pd, &early_platform_device_list, dev.devres_head) |
| 1086 | if (platform_match(&pd->dev, &epdrv->pdrv->driver)) |
| 1087 | if (pd->id == id) |
| 1088 | return pd; |
| 1089 | |
| 1090 | return NULL; |
| 1091 | } |
| 1092 | |
| 1093 | /** |
| 1094 | * early_platform_left |
Randy Dunlap | d86c130 | 2009-04-21 07:22:53 -0700 | [diff] [blame] | 1095 | * @epdrv: early platform driver structure |
Magnus Damm | 1397709 | 2009-03-30 14:37:25 -0700 | [diff] [blame] | 1096 | * @id: return true if id or above exists |
| 1097 | */ |
| 1098 | static __init int early_platform_left(struct early_platform_driver *epdrv, |
| 1099 | int id) |
| 1100 | { |
| 1101 | struct platform_device *pd; |
| 1102 | |
| 1103 | list_for_each_entry(pd, &early_platform_device_list, dev.devres_head) |
| 1104 | if (platform_match(&pd->dev, &epdrv->pdrv->driver)) |
| 1105 | if (pd->id >= id) |
| 1106 | return 1; |
| 1107 | |
| 1108 | return 0; |
| 1109 | } |
| 1110 | |
| 1111 | /** |
| 1112 | * early_platform_driver_probe_id |
| 1113 | * @class_str: string to identify early platform driver class |
| 1114 | * @id: id to match against |
| 1115 | * @nr_probe: number of platform devices to successfully probe before exiting |
| 1116 | */ |
| 1117 | static int __init early_platform_driver_probe_id(char *class_str, |
| 1118 | int id, |
| 1119 | int nr_probe) |
| 1120 | { |
| 1121 | struct early_platform_driver *epdrv; |
| 1122 | struct platform_device *match; |
| 1123 | int match_id; |
| 1124 | int n = 0; |
| 1125 | int left = 0; |
| 1126 | |
| 1127 | list_for_each_entry(epdrv, &early_platform_driver_list, list) { |
| 1128 | /* only use drivers matching our class_str */ |
| 1129 | if (strcmp(class_str, epdrv->class_str)) |
| 1130 | continue; |
| 1131 | |
| 1132 | if (id == -2) { |
| 1133 | match_id = epdrv->requested_id; |
| 1134 | left = 1; |
| 1135 | |
| 1136 | } else { |
| 1137 | match_id = id; |
| 1138 | left += early_platform_left(epdrv, id); |
| 1139 | |
| 1140 | /* skip requested id */ |
| 1141 | switch (epdrv->requested_id) { |
| 1142 | case EARLY_PLATFORM_ID_ERROR: |
| 1143 | case EARLY_PLATFORM_ID_UNSET: |
| 1144 | break; |
| 1145 | default: |
| 1146 | if (epdrv->requested_id == id) |
| 1147 | match_id = EARLY_PLATFORM_ID_UNSET; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | switch (match_id) { |
| 1152 | case EARLY_PLATFORM_ID_ERROR: |
| 1153 | pr_warning("%s: unable to parse %s parameter\n", |
| 1154 | class_str, epdrv->pdrv->driver.name); |
| 1155 | /* fall-through */ |
| 1156 | case EARLY_PLATFORM_ID_UNSET: |
| 1157 | match = NULL; |
| 1158 | break; |
| 1159 | default: |
| 1160 | match = early_platform_match(epdrv, match_id); |
| 1161 | } |
| 1162 | |
| 1163 | if (match) { |
| 1164 | if (epdrv->pdrv->probe(match)) |
| 1165 | pr_warning("%s: unable to probe %s early.\n", |
| 1166 | class_str, match->name); |
| 1167 | else |
| 1168 | n++; |
| 1169 | } |
| 1170 | |
| 1171 | if (n >= nr_probe) |
| 1172 | break; |
| 1173 | } |
| 1174 | |
| 1175 | if (left) |
| 1176 | return n; |
| 1177 | else |
| 1178 | return -ENODEV; |
| 1179 | } |
| 1180 | |
| 1181 | /** |
| 1182 | * early_platform_driver_probe |
| 1183 | * @class_str: string to identify early platform driver class |
| 1184 | * @nr_probe: number of platform devices to successfully probe before exiting |
| 1185 | * @user_only: only probe user specified early platform devices |
| 1186 | */ |
| 1187 | int __init early_platform_driver_probe(char *class_str, |
| 1188 | int nr_probe, |
| 1189 | int user_only) |
| 1190 | { |
| 1191 | int k, n, i; |
| 1192 | |
| 1193 | n = 0; |
| 1194 | for (i = -2; n < nr_probe; i++) { |
| 1195 | k = early_platform_driver_probe_id(class_str, i, nr_probe - n); |
| 1196 | |
| 1197 | if (k < 0) |
| 1198 | break; |
| 1199 | |
| 1200 | n += k; |
| 1201 | |
| 1202 | if (user_only) |
| 1203 | break; |
| 1204 | } |
| 1205 | |
| 1206 | return n; |
| 1207 | } |
| 1208 | |
| 1209 | /** |
| 1210 | * early_platform_cleanup - clean up early platform code |
| 1211 | */ |
| 1212 | void __init early_platform_cleanup(void) |
| 1213 | { |
| 1214 | struct platform_device *pd, *pd2; |
| 1215 | |
| 1216 | /* clean up the devres list used to chain devices */ |
| 1217 | list_for_each_entry_safe(pd, pd2, &early_platform_device_list, |
| 1218 | dev.devres_head) { |
| 1219 | list_del(&pd->dev.devres_head); |
| 1220 | memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head)); |
| 1221 | } |
| 1222 | } |
| 1223 | |