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