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