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