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