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