Greg Kroah-Hartman | 989d42e | 2017-11-07 17:30:07 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * platform.c - platform 'pseudo' bus for legacy devices |
| 4 | * |
| 5 | * Copyright (c) 2002-3 Patrick Mochel |
| 6 | * Copyright (c) 2002-3 Open Source Development Labs |
| 7 | * |
Mauro Carvalho Chehab | fe34c89 | 2019-06-18 12:34:59 -0300 | [diff] [blame] | 8 | * Please see Documentation/driver-api/driver-model/platform.rst for more |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * information. |
| 10 | */ |
| 11 | |
Andrew Morton | daa4122 | 2009-08-06 16:00:44 -0700 | [diff] [blame] | 12 | #include <linux/string.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
Grant Likely | 0521215 | 2010-06-08 07:48:20 -0600 | [diff] [blame] | 14 | #include <linux/of_device.h> |
Rob Herring | 9ec36ca | 2014-04-23 17:57:41 -0500 | [diff] [blame] | 15 | #include <linux/of_irq.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> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 19 | #include <linux/memblock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Ulf Hansson | f48c767 | 2014-09-29 13:58:47 +0200 | [diff] [blame] | 23 | #include <linux/pm_domain.h> |
Jean Delvare | 689ae23 | 2012-07-27 22:14:59 +0200 | [diff] [blame] | 24 | #include <linux/idr.h> |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 25 | #include <linux/acpi.h> |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 26 | #include <linux/clk/clk-conf.h> |
Kim Phillips | 3d713e0 | 2014-06-02 19:42:58 -0500 | [diff] [blame] | 27 | #include <linux/limits.h> |
Mika Westerberg | 00bbc1d | 2015-11-30 17:11:38 +0200 | [diff] [blame] | 28 | #include <linux/property.h> |
Qian Cai | 967d301 | 2019-01-03 15:29:05 -0800 | [diff] [blame] | 29 | #include <linux/kmemleak.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 31 | #include "base.h" |
Rafael J. Wysocki | bed2b42 | 2012-08-06 01:45:11 +0200 | [diff] [blame] | 32 | #include "power/power.h" |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 33 | |
Jean Delvare | 689ae23 | 2012-07-27 22:14:59 +0200 | [diff] [blame] | 34 | /* For automatically allocated device IDs */ |
| 35 | static DEFINE_IDA(platform_devid_ida); |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | struct device platform_bus = { |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 38 | .init_name = "platform", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | }; |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 40 | EXPORT_SYMBOL_GPL(platform_bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 43 | * platform_get_resource - get a resource for a device |
| 44 | * @dev: platform device |
| 45 | * @type: resource type |
| 46 | * @num: resource index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 48 | struct resource *platform_get_resource(struct platform_device *dev, |
| 49 | unsigned int type, unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | int i; |
| 52 | |
| 53 | for (i = 0; i < dev->num_resources; i++) { |
| 54 | struct resource *r = &dev->resource[i]; |
| 55 | |
Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 56 | if (type == resource_type(r) && num-- == 0) |
| 57 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | return NULL; |
| 60 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 61 | EXPORT_SYMBOL_GPL(platform_get_resource); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Bartosz Golaszewski | bb6243b | 2019-10-22 10:43:14 +0200 | [diff] [blame] | 63 | #ifdef CONFIG_HAS_IOMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | /** |
Bartosz Golaszewski | 7945f92 | 2019-02-20 11:12:39 +0000 | [diff] [blame] | 65 | * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform |
| 66 | * device |
| 67 | * |
| 68 | * @pdev: platform device to use both for memory resource lookup as well as |
Bartosz Golaszewski | 7067c96 | 2019-04-01 10:16:35 +0200 | [diff] [blame] | 69 | * resource management |
Bartosz Golaszewski | 7945f92 | 2019-02-20 11:12:39 +0000 | [diff] [blame] | 70 | * @index: resource index |
| 71 | */ |
| 72 | void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev, |
| 73 | unsigned int index) |
| 74 | { |
| 75 | struct resource *res; |
| 76 | |
| 77 | res = platform_get_resource(pdev, IORESOURCE_MEM, index); |
| 78 | return devm_ioremap_resource(&pdev->dev, res); |
| 79 | } |
| 80 | EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource); |
Bartosz Golaszewski | bb6243b | 2019-10-22 10:43:14 +0200 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * devm_platform_ioremap_resource_wc - write-combined variant of |
| 84 | * devm_platform_ioremap_resource() |
| 85 | * |
| 86 | * @pdev: platform device to use both for memory resource lookup as well as |
| 87 | * resource management |
| 88 | * @index: resource index |
| 89 | */ |
| 90 | void __iomem *devm_platform_ioremap_resource_wc(struct platform_device *pdev, |
| 91 | unsigned int index) |
| 92 | { |
| 93 | struct resource *res; |
| 94 | |
| 95 | res = platform_get_resource(pdev, IORESOURCE_MEM, index); |
| 96 | return devm_ioremap_resource_wc(&pdev->dev, res); |
| 97 | } |
Bartosz Golaszewski | c9c8641 | 2019-10-22 10:43:16 +0200 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for |
| 101 | * a platform device, retrieve the |
| 102 | * resource by name |
| 103 | * |
| 104 | * @pdev: platform device to use both for memory resource lookup as well as |
| 105 | * resource management |
| 106 | * @name: name of the resource |
| 107 | */ |
| 108 | void __iomem * |
| 109 | devm_platform_ioremap_resource_byname(struct platform_device *pdev, |
| 110 | const char *name) |
| 111 | { |
| 112 | struct resource *res; |
| 113 | |
| 114 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name); |
| 115 | return devm_ioremap_resource(&pdev->dev, res); |
| 116 | } |
| 117 | EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname); |
Bartosz Golaszewski | 837ccda | 2019-02-21 17:26:27 +0100 | [diff] [blame] | 118 | #endif /* CONFIG_HAS_IOMEM */ |
Bartosz Golaszewski | 7945f92 | 2019-02-20 11:12:39 +0000 | [diff] [blame] | 119 | |
Uwe Kleine-König | ec4e290 | 2019-10-09 11:37:46 +0200 | [diff] [blame] | 120 | /** |
| 121 | * platform_get_irq_optional - get an optional IRQ for a device |
| 122 | * @dev: platform device |
| 123 | * @num: IRQ number index |
| 124 | * |
| 125 | * Gets an IRQ for a platform device. Device drivers should check the return |
| 126 | * value for errors so as to not pass a negative integer value to the |
| 127 | * request_irq() APIs. This is the same as platform_get_irq(), except that it |
| 128 | * does not print an error message if an IRQ can not be obtained. |
| 129 | * |
| 130 | * Example: |
| 131 | * int irq = platform_get_irq_optional(pdev, 0); |
| 132 | * if (irq < 0) |
| 133 | * return irq; |
| 134 | * |
| 135 | * Return: IRQ number on success, negative error number on failure. |
| 136 | */ |
| 137 | int platform_get_irq_optional(struct platform_device *dev, unsigned int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Andreas Larsson | 5cf8f7d | 2012-10-29 23:26:56 +0000 | [diff] [blame] | 139 | #ifdef CONFIG_SPARC |
| 140 | /* sparc does not have irqs represented as IORESOURCE_IRQ resources */ |
| 141 | if (!dev || num >= dev->archdata.num_irqs) |
| 142 | return -ENXIO; |
| 143 | return dev->archdata.irqs[num]; |
| 144 | #else |
Rob Herring | 9ec36ca | 2014-04-23 17:57:41 -0500 | [diff] [blame] | 145 | struct resource *r; |
Andy Shevchenko | 71564a2 | 2019-10-23 15:25:05 +0300 | [diff] [blame] | 146 | int ret; |
Guenter Roeck | aff008a | 2014-06-17 15:51:02 -0700 | [diff] [blame] | 147 | |
Andy Shevchenko | 71564a2 | 2019-10-23 15:25:05 +0300 | [diff] [blame] | 148 | if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) { |
Guenter Roeck | aff008a | 2014-06-17 15:51:02 -0700 | [diff] [blame] | 149 | ret = of_irq_get(dev->dev.of_node, num); |
Sergei Shtylyov | e330b9a | 2016-07-04 01:04:24 +0300 | [diff] [blame] | 150 | if (ret > 0 || ret == -EPROBE_DEFER) |
Guenter Roeck | aff008a | 2014-06-17 15:51:02 -0700 | [diff] [blame] | 151 | return ret; |
| 152 | } |
Rob Herring | 9ec36ca | 2014-04-23 17:57:41 -0500 | [diff] [blame] | 153 | |
| 154 | r = platform_get_resource(dev, IORESOURCE_IRQ, num); |
Agustin Vega-Frias | d44fa3d | 2017-02-02 18:23:58 -0500 | [diff] [blame] | 155 | if (has_acpi_companion(&dev->dev)) { |
| 156 | if (r && r->flags & IORESOURCE_DISABLED) { |
Agustin Vega-Frias | d44fa3d | 2017-02-02 18:23:58 -0500 | [diff] [blame] | 157 | ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r); |
| 158 | if (ret) |
| 159 | return ret; |
| 160 | } |
| 161 | } |
| 162 | |
Linus Walleij | 7085a74 | 2015-02-18 17:12:18 +0100 | [diff] [blame] | 163 | /* |
| 164 | * The resources may pass trigger flags to the irqs that need |
| 165 | * to be set up. It so happens that the trigger flags for |
| 166 | * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER* |
| 167 | * settings. |
| 168 | */ |
Guenter Roeck | 60ca5e0 | 2016-09-13 20:32:44 -0700 | [diff] [blame] | 169 | if (r && r->flags & IORESOURCE_BITS) { |
| 170 | struct irq_data *irqd; |
| 171 | |
| 172 | irqd = irq_get_irq_data(r->start); |
| 173 | if (!irqd) |
| 174 | return -ENXIO; |
| 175 | irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS); |
| 176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Enrico Granata | daaef25 | 2019-02-11 11:01:12 -0800 | [diff] [blame] | 178 | if (r) |
| 179 | return r->start; |
| 180 | |
| 181 | /* |
| 182 | * For the index 0 interrupt, allow falling back to GpioInt |
| 183 | * resources. While a device could have both Interrupt and GpioInt |
| 184 | * resources, making this fallback ambiguous, in many common cases |
| 185 | * the device will only expose one IRQ, and this fallback |
| 186 | * allows a common code path across either kind of resource. |
| 187 | */ |
Brian Norris | 46c42d8 | 2019-07-29 13:49:54 -0700 | [diff] [blame] | 188 | if (num == 0 && has_acpi_companion(&dev->dev)) { |
Andy Shevchenko | 71564a2 | 2019-10-23 15:25:05 +0300 | [diff] [blame] | 189 | ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num); |
Brian Norris | 46c42d8 | 2019-07-29 13:49:54 -0700 | [diff] [blame] | 190 | /* Our callers expect -ENXIO for missing IRQs. */ |
| 191 | if (ret >= 0 || ret == -EPROBE_DEFER) |
| 192 | return ret; |
| 193 | } |
Enrico Granata | daaef25 | 2019-02-11 11:01:12 -0800 | [diff] [blame] | 194 | |
| 195 | return -ENXIO; |
Andreas Larsson | 5cf8f7d | 2012-10-29 23:26:56 +0000 | [diff] [blame] | 196 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
Uwe Kleine-König | ec4e290 | 2019-10-09 11:37:46 +0200 | [diff] [blame] | 198 | EXPORT_SYMBOL_GPL(platform_get_irq_optional); |
Stephen Boyd | 7723f4c | 2019-07-29 22:38:43 -0700 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * platform_get_irq - get an IRQ for a device |
| 202 | * @dev: platform device |
| 203 | * @num: IRQ number index |
| 204 | * |
| 205 | * Gets an IRQ for a platform device and prints an error message if finding the |
| 206 | * IRQ fails. Device drivers should check the return value for errors so as to |
| 207 | * not pass a negative integer value to the request_irq() APIs. |
| 208 | * |
| 209 | * Example: |
| 210 | * int irq = platform_get_irq(pdev, 0); |
| 211 | * if (irq < 0) |
| 212 | * return irq; |
| 213 | * |
| 214 | * Return: IRQ number on success, negative error number on failure. |
| 215 | */ |
| 216 | int platform_get_irq(struct platform_device *dev, unsigned int num) |
| 217 | { |
| 218 | int ret; |
| 219 | |
Uwe Kleine-König | ec4e290 | 2019-10-09 11:37:46 +0200 | [diff] [blame] | 220 | ret = platform_get_irq_optional(dev, num); |
Stephen Boyd | 7723f4c | 2019-07-29 22:38:43 -0700 | [diff] [blame] | 221 | if (ret < 0 && ret != -EPROBE_DEFER) |
| 222 | dev_err(&dev->dev, "IRQ index %u not found\n", num); |
| 223 | |
| 224 | return ret; |
| 225 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 226 | EXPORT_SYMBOL_GPL(platform_get_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | /** |
Stephen Boyd | 4b83555 | 2016-01-06 17:12:47 -0800 | [diff] [blame] | 229 | * platform_irq_count - Count the number of IRQs a platform device uses |
| 230 | * @dev: platform device |
| 231 | * |
| 232 | * Return: Number of IRQs a platform device uses or EPROBE_DEFER |
| 233 | */ |
| 234 | int platform_irq_count(struct platform_device *dev) |
| 235 | { |
| 236 | int ret, nr = 0; |
| 237 | |
Uwe Kleine-König | ec4e290 | 2019-10-09 11:37:46 +0200 | [diff] [blame] | 238 | while ((ret = platform_get_irq_optional(dev, nr)) >= 0) |
Stephen Boyd | 4b83555 | 2016-01-06 17:12:47 -0800 | [diff] [blame] | 239 | nr++; |
| 240 | |
| 241 | if (ret == -EPROBE_DEFER) |
| 242 | return ret; |
| 243 | |
| 244 | return nr; |
| 245 | } |
| 246 | EXPORT_SYMBOL_GPL(platform_irq_count); |
| 247 | |
| 248 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 249 | * platform_get_resource_byname - get a resource for a device by name |
| 250 | * @dev: platform device |
| 251 | * @type: resource type |
| 252 | * @name: resource name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 254 | struct resource *platform_get_resource_byname(struct platform_device *dev, |
Linus Walleij | c0afe7b | 2009-04-27 02:38:16 +0200 | [diff] [blame] | 255 | unsigned int type, |
| 256 | const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
| 258 | int i; |
| 259 | |
| 260 | for (i = 0; i < dev->num_resources; i++) { |
| 261 | struct resource *r = &dev->resource[i]; |
| 262 | |
Peter Ujfalusi | 1b8cb92 | 2012-08-23 17:10:00 +0300 | [diff] [blame] | 263 | if (unlikely(!r->name)) |
| 264 | continue; |
| 265 | |
Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 266 | if (type == resource_type(r) && !strcmp(r->name, name)) |
| 267 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | return NULL; |
| 270 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 271 | EXPORT_SYMBOL_GPL(platform_get_resource_byname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Hans de Goede | f1da567 | 2019-10-05 23:04:47 +0200 | [diff] [blame] | 273 | static int __platform_get_irq_byname(struct platform_device *dev, |
| 274 | const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
Grygorii Strashko | ad69674 | 2014-05-20 13:42:02 +0300 | [diff] [blame] | 276 | struct resource *r; |
Andy Shevchenko | 71564a2 | 2019-10-23 15:25:05 +0300 | [diff] [blame] | 277 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Guenter Roeck | aff008a | 2014-06-17 15:51:02 -0700 | [diff] [blame] | 279 | if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) { |
Guenter Roeck | aff008a | 2014-06-17 15:51:02 -0700 | [diff] [blame] | 280 | ret = of_irq_get_byname(dev->dev.of_node, name); |
Sergei Shtylyov | e330b9a | 2016-07-04 01:04:24 +0300 | [diff] [blame] | 281 | if (ret > 0 || ret == -EPROBE_DEFER) |
Guenter Roeck | aff008a | 2014-06-17 15:51:02 -0700 | [diff] [blame] | 282 | return ret; |
| 283 | } |
Grygorii Strashko | ad69674 | 2014-05-20 13:42:02 +0300 | [diff] [blame] | 284 | |
| 285 | r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name); |
Stephen Boyd | 7723f4c | 2019-07-29 22:38:43 -0700 | [diff] [blame] | 286 | if (r) |
| 287 | return r->start; |
| 288 | |
Stephen Boyd | 7723f4c | 2019-07-29 22:38:43 -0700 | [diff] [blame] | 289 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
Hans de Goede | f1da567 | 2019-10-05 23:04:47 +0200 | [diff] [blame] | 291 | |
| 292 | /** |
| 293 | * platform_get_irq_byname - get an IRQ for a device by name |
| 294 | * @dev: platform device |
| 295 | * @name: IRQ name |
| 296 | * |
| 297 | * Get an IRQ like platform_get_irq(), but then by name rather then by index. |
| 298 | * |
| 299 | * Return: IRQ number on success, negative error number on failure. |
| 300 | */ |
| 301 | int platform_get_irq_byname(struct platform_device *dev, const char *name) |
| 302 | { |
| 303 | int ret; |
| 304 | |
| 305 | ret = __platform_get_irq_byname(dev, name); |
| 306 | if (ret < 0 && ret != -EPROBE_DEFER) |
| 307 | dev_err(&dev->dev, "IRQ %s not found\n", name); |
| 308 | |
| 309 | return ret; |
| 310 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 311 | EXPORT_SYMBOL_GPL(platform_get_irq_byname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
| 313 | /** |
Hans de Goede | f1da567 | 2019-10-05 23:04:47 +0200 | [diff] [blame] | 314 | * platform_get_irq_byname_optional - get an optional IRQ for a device by name |
| 315 | * @dev: platform device |
| 316 | * @name: IRQ name |
| 317 | * |
| 318 | * Get an optional IRQ by name like platform_get_irq_byname(). Except that it |
| 319 | * does not print an error message if an IRQ can not be obtained. |
| 320 | * |
| 321 | * Return: IRQ number on success, negative error number on failure. |
| 322 | */ |
| 323 | int platform_get_irq_byname_optional(struct platform_device *dev, |
| 324 | const char *name) |
| 325 | { |
| 326 | return __platform_get_irq_byname(dev, name); |
| 327 | } |
| 328 | EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional); |
| 329 | |
| 330 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 331 | * platform_add_devices - add a numbers of platform devices |
| 332 | * @devs: array of platform devices to add |
| 333 | * @num: number of platform devices in array |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | */ |
| 335 | int platform_add_devices(struct platform_device **devs, int num) |
| 336 | { |
| 337 | int i, ret = 0; |
| 338 | |
| 339 | for (i = 0; i < num; i++) { |
| 340 | ret = platform_device_register(devs[i]); |
| 341 | if (ret) { |
| 342 | while (--i >= 0) |
| 343 | platform_device_unregister(devs[i]); |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return ret; |
| 349 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 350 | EXPORT_SYMBOL_GPL(platform_add_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 352 | struct platform_object { |
| 353 | struct platform_device pdev; |
Yann Droneaud | 1cec24c | 2014-05-30 22:02:47 +0200 | [diff] [blame] | 354 | char name[]; |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 355 | }; |
| 356 | |
Christoph Hellwig | cdfee56 | 2019-08-16 08:24:35 +0200 | [diff] [blame] | 357 | /* |
| 358 | * Set up default DMA mask for platform devices if the they weren't |
| 359 | * previously set by the architecture / DT. |
| 360 | */ |
| 361 | static void setup_pdev_dma_masks(struct platform_device *pdev) |
| 362 | { |
| 363 | if (!pdev->dev.coherent_dma_mask) |
| 364 | pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
| 365 | if (!pdev->dma_mask) |
| 366 | pdev->dma_mask = DMA_BIT_MASK(32); |
| 367 | if (!pdev->dev.dma_mask) |
| 368 | pdev->dev.dma_mask = &pdev->dma_mask; |
| 369 | }; |
| 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | /** |
Ben Hutchings | 3c31f07 | 2010-02-14 14:18:53 +0000 | [diff] [blame] | 372 | * platform_device_put - destroy a platform device |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 373 | * @pdev: platform device to free |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 374 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 375 | * Free all memory associated with a platform device. This function must |
| 376 | * _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] | 377 | */ |
| 378 | void platform_device_put(struct platform_device *pdev) |
| 379 | { |
Andy Shevchenko | 99fef58 | 2018-12-03 20:21:41 +0200 | [diff] [blame] | 380 | if (!IS_ERR_OR_NULL(pdev)) |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 381 | put_device(&pdev->dev); |
| 382 | } |
| 383 | EXPORT_SYMBOL_GPL(platform_device_put); |
| 384 | |
| 385 | static void platform_device_release(struct device *dev) |
| 386 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 387 | struct platform_object *pa = container_of(dev, struct platform_object, |
| 388 | pdev.dev); |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 389 | |
Grant Likely | 7096d04 | 2010-10-20 11:45:13 -0600 | [diff] [blame] | 390 | of_device_node_put(&pa->pdev.dev); |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 391 | kfree(pa->pdev.dev.platform_data); |
Samuel Ortiz | e710d7d | 2011-04-08 00:43:01 +0200 | [diff] [blame] | 392 | kfree(pa->pdev.mfd_cell); |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 393 | kfree(pa->pdev.resource); |
Kim Phillips | 3d713e0 | 2014-06-02 19:42:58 -0500 | [diff] [blame] | 394 | kfree(pa->pdev.driver_override); |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 395 | kfree(pa); |
| 396 | } |
| 397 | |
| 398 | /** |
Ben Hutchings | 3c31f07 | 2010-02-14 14:18:53 +0000 | [diff] [blame] | 399 | * platform_device_alloc - create a platform device |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 400 | * @name: base name of the device we're adding |
| 401 | * @id: instance id |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 402 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 403 | * Create a platform device object which can have other objects attached |
| 404 | * 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] | 405 | */ |
Jean Delvare | 1359555e | 2007-09-09 12:54:16 +0200 | [diff] [blame] | 406 | struct platform_device *platform_device_alloc(const char *name, int id) |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 407 | { |
| 408 | struct platform_object *pa; |
| 409 | |
Yann Droneaud | 1cec24c | 2014-05-30 22:02:47 +0200 | [diff] [blame] | 410 | pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL); |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 411 | if (pa) { |
| 412 | strcpy(pa->name, name); |
| 413 | pa->pdev.name = pa->name; |
| 414 | pa->pdev.id = id; |
| 415 | device_initialize(&pa->pdev.dev); |
| 416 | pa->pdev.dev.release = platform_device_release; |
Christoph Hellwig | cdfee56 | 2019-08-16 08:24:35 +0200 | [diff] [blame] | 417 | setup_pdev_dma_masks(&pa->pdev); |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 420 | return pa ? &pa->pdev : NULL; |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 421 | } |
| 422 | EXPORT_SYMBOL_GPL(platform_device_alloc); |
| 423 | |
| 424 | /** |
Ben Hutchings | 3c31f07 | 2010-02-14 14:18:53 +0000 | [diff] [blame] | 425 | * platform_device_add_resources - add resources to a platform device |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 426 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
| 427 | * @res: set of resources that needs to be allocated for the device |
| 428 | * @num: number of resources |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 429 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 430 | * Add a copy of the resources to the platform device. The memory |
| 431 | * associated with the resources will be freed when the platform device is |
| 432 | * released. |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 433 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 434 | int platform_device_add_resources(struct platform_device *pdev, |
Geert Uytterhoeven | 0b7f1a7 | 2009-01-28 21:01:02 +0100 | [diff] [blame] | 435 | const struct resource *res, unsigned int num) |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 436 | { |
Uwe Kleine-König | cea8962 | 2011-04-20 09:44:44 +0200 | [diff] [blame] | 437 | struct resource *r = NULL; |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 438 | |
Uwe Kleine-König | cea8962 | 2011-04-20 09:44:44 +0200 | [diff] [blame] | 439 | if (res) { |
| 440 | r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL); |
| 441 | if (!r) |
| 442 | return -ENOMEM; |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 443 | } |
Uwe Kleine-König | cea8962 | 2011-04-20 09:44:44 +0200 | [diff] [blame] | 444 | |
Uwe Kleine-König | 4a03d6f | 2011-04-20 09:44:45 +0200 | [diff] [blame] | 445 | kfree(pdev->resource); |
Uwe Kleine-König | cea8962 | 2011-04-20 09:44:44 +0200 | [diff] [blame] | 446 | pdev->resource = r; |
| 447 | pdev->num_resources = num; |
| 448 | return 0; |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 449 | } |
| 450 | EXPORT_SYMBOL_GPL(platform_device_add_resources); |
| 451 | |
| 452 | /** |
Ben Hutchings | 3c31f07 | 2010-02-14 14:18:53 +0000 | [diff] [blame] | 453 | * 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] | 454 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
| 455 | * @data: platform specific data for this platform device |
| 456 | * @size: size of platform specific data |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 457 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 458 | * Add a copy of platform specific data to the platform device's |
| 459 | * platform_data pointer. The memory associated with the platform data |
| 460 | * will be freed when the platform device is released. |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 461 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 462 | int platform_device_add_data(struct platform_device *pdev, const void *data, |
| 463 | size_t size) |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 464 | { |
Uwe Kleine-König | 27a33f9 | 2011-04-20 09:44:42 +0200 | [diff] [blame] | 465 | void *d = NULL; |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 466 | |
Uwe Kleine-König | 27a33f9 | 2011-04-20 09:44:42 +0200 | [diff] [blame] | 467 | if (data) { |
| 468 | d = kmemdup(data, size, GFP_KERNEL); |
| 469 | if (!d) |
| 470 | return -ENOMEM; |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 471 | } |
Uwe Kleine-König | 27a33f9 | 2011-04-20 09:44:42 +0200 | [diff] [blame] | 472 | |
Uwe Kleine-König | 251e031 | 2011-04-20 09:44:43 +0200 | [diff] [blame] | 473 | kfree(pdev->dev.platform_data); |
Uwe Kleine-König | 27a33f9 | 2011-04-20 09:44:42 +0200 | [diff] [blame] | 474 | pdev->dev.platform_data = d; |
| 475 | return 0; |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 476 | } |
| 477 | EXPORT_SYMBOL_GPL(platform_device_add_data); |
| 478 | |
| 479 | /** |
Mika Westerberg | 00bbc1d | 2015-11-30 17:11:38 +0200 | [diff] [blame] | 480 | * platform_device_add_properties - add built-in properties to a platform device |
| 481 | * @pdev: platform device to add properties to |
Heikki Krogerus | f4d0526 | 2016-03-29 14:52:23 +0300 | [diff] [blame] | 482 | * @properties: null terminated array of properties to add |
Mika Westerberg | 00bbc1d | 2015-11-30 17:11:38 +0200 | [diff] [blame] | 483 | * |
Heikki Krogerus | f4d0526 | 2016-03-29 14:52:23 +0300 | [diff] [blame] | 484 | * The function will take deep copy of @properties and attach the copy to the |
| 485 | * platform device. The memory associated with properties will be freed when the |
| 486 | * platform device is released. |
Mika Westerberg | 00bbc1d | 2015-11-30 17:11:38 +0200 | [diff] [blame] | 487 | */ |
| 488 | int platform_device_add_properties(struct platform_device *pdev, |
Jan Kiszka | 277036f | 2017-06-02 07:43:27 +0200 | [diff] [blame] | 489 | const struct property_entry *properties) |
Mika Westerberg | 00bbc1d | 2015-11-30 17:11:38 +0200 | [diff] [blame] | 490 | { |
Heikki Krogerus | f4d0526 | 2016-03-29 14:52:23 +0300 | [diff] [blame] | 491 | return device_add_properties(&pdev->dev, properties); |
Mika Westerberg | 00bbc1d | 2015-11-30 17:11:38 +0200 | [diff] [blame] | 492 | } |
| 493 | EXPORT_SYMBOL_GPL(platform_device_add_properties); |
| 494 | |
| 495 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 496 | * platform_device_add - add a platform device to device hierarchy |
| 497 | * @pdev: platform device we're adding |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 499 | * This is part 2 of platform_device_register(), though may be called |
| 500 | * separately _iff_ pdev was allocated by platform_device_alloc(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | */ |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 502 | int platform_device_add(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | { |
Jean Delvare | 689ae23 | 2012-07-27 22:14:59 +0200 | [diff] [blame] | 504 | int i, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | if (!pdev) |
| 507 | return -EINVAL; |
| 508 | |
| 509 | if (!pdev->dev.parent) |
| 510 | pdev->dev.parent = &platform_bus; |
| 511 | |
| 512 | pdev->dev.bus = &platform_bus_type; |
| 513 | |
Jean Delvare | 689ae23 | 2012-07-27 22:14:59 +0200 | [diff] [blame] | 514 | switch (pdev->id) { |
| 515 | default: |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 516 | dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); |
Jean Delvare | 689ae23 | 2012-07-27 22:14:59 +0200 | [diff] [blame] | 517 | break; |
| 518 | case PLATFORM_DEVID_NONE: |
Greg Kroah-Hartman | acc0e90 | 2009-06-02 15:39:55 -0700 | [diff] [blame] | 519 | dev_set_name(&pdev->dev, "%s", pdev->name); |
Jean Delvare | 689ae23 | 2012-07-27 22:14:59 +0200 | [diff] [blame] | 520 | break; |
| 521 | case PLATFORM_DEVID_AUTO: |
| 522 | /* |
| 523 | * Automatically allocated device ID. We mark it as such so |
| 524 | * that we remember it must be freed, and we append a suffix |
| 525 | * to avoid namespace collision with explicit IDs. |
| 526 | */ |
| 527 | ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL); |
| 528 | if (ret < 0) |
Greg Kroah-Hartman | 5da7f70 | 2015-06-10 08:38:02 -0700 | [diff] [blame] | 529 | goto err_out; |
Jean Delvare | 689ae23 | 2012-07-27 22:14:59 +0200 | [diff] [blame] | 530 | pdev->id = ret; |
| 531 | pdev->id_auto = true; |
| 532 | dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id); |
| 533 | break; |
| 534 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | for (i = 0; i < pdev->num_resources; i++) { |
Greg Kroah-Hartman | 5da7f70 | 2015-06-10 08:38:02 -0700 | [diff] [blame] | 537 | struct resource *p, *r = &pdev->resource[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
| 539 | if (r->name == NULL) |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 540 | r->name = dev_name(&pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | p = r->parent; |
| 543 | if (!p) { |
Greg Kroah-Hartman | 0e6c861 | 2015-06-10 08:38:29 -0700 | [diff] [blame] | 544 | if (resource_type(r) == IORESOURCE_MEM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | p = &iomem_resource; |
Greg Kroah-Hartman | 0e6c861 | 2015-06-10 08:38:29 -0700 | [diff] [blame] | 546 | else if (resource_type(r) == IORESOURCE_IO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | p = &ioport_resource; |
| 548 | } |
| 549 | |
Andy Shevchenko | 25ebcb7 | 2019-04-04 11:11:58 +0300 | [diff] [blame] | 550 | if (p) { |
| 551 | ret = insert_resource(p, r); |
| 552 | if (ret) { |
| 553 | dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r); |
| 554 | goto failed; |
| 555 | } |
Greg Kroah-Hartman | 5da7f70 | 2015-06-10 08:38:02 -0700 | [diff] [blame] | 556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | pr_debug("Registering platform device '%s'. Parent at %s\n", |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 560 | dev_name(&pdev->dev), dev_name(pdev->dev.parent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Russell King | e391553 | 2006-05-06 08:15:26 +0100 | [diff] [blame] | 562 | ret = device_add(&pdev->dev); |
Greg Kroah-Hartman | 8b2dceb | 2015-06-10 08:36:50 -0700 | [diff] [blame] | 563 | if (ret == 0) |
| 564 | return ret; |
| 565 | |
Greg Kroah-Hartman | 5da7f70 | 2015-06-10 08:38:02 -0700 | [diff] [blame] | 566 | failed: |
Greg Kroah-Hartman | 8b2dceb | 2015-06-10 08:36:50 -0700 | [diff] [blame] | 567 | if (pdev->id_auto) { |
| 568 | ida_simple_remove(&platform_devid_ida, pdev->id); |
| 569 | pdev->id = PLATFORM_DEVID_AUTO; |
| 570 | } |
| 571 | |
| 572 | while (--i >= 0) { |
| 573 | struct resource *r = &pdev->resource[i]; |
Grant Likely | 7f5dcaf | 2015-06-07 15:20:11 +0100 | [diff] [blame] | 574 | if (r->parent) |
Greg Kroah-Hartman | 8b2dceb | 2015-06-10 08:36:50 -0700 | [diff] [blame] | 575 | release_resource(r); |
| 576 | } |
Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 577 | |
Greg Kroah-Hartman | 5da7f70 | 2015-06-10 08:38:02 -0700 | [diff] [blame] | 578 | err_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | return ret; |
| 580 | } |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 581 | EXPORT_SYMBOL_GPL(platform_device_add); |
| 582 | |
| 583 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 584 | * platform_device_del - remove a platform-level device |
| 585 | * @pdev: platform device we're removing |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 586 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 587 | * Note that this function will also release all memory- and port-based |
| 588 | * resources owned by the device (@dev->resource). This function must |
| 589 | * _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] | 590 | */ |
| 591 | void platform_device_del(struct platform_device *pdev) |
| 592 | { |
Greg Kroah-Hartman | 8b2dceb | 2015-06-10 08:36:50 -0700 | [diff] [blame] | 593 | int i; |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 594 | |
Andy Shevchenko | 99fef58 | 2018-12-03 20:21:41 +0200 | [diff] [blame] | 595 | if (!IS_ERR_OR_NULL(pdev)) { |
Greg Kroah-Hartman | 8b2dceb | 2015-06-10 08:36:50 -0700 | [diff] [blame] | 596 | device_del(&pdev->dev); |
| 597 | |
| 598 | if (pdev->id_auto) { |
| 599 | ida_simple_remove(&platform_devid_ida, pdev->id); |
| 600 | pdev->id = PLATFORM_DEVID_AUTO; |
| 601 | } |
| 602 | |
| 603 | for (i = 0; i < pdev->num_resources; i++) { |
| 604 | struct resource *r = &pdev->resource[i]; |
Grant Likely | 7f5dcaf | 2015-06-07 15:20:11 +0100 | [diff] [blame] | 605 | if (r->parent) |
Greg Kroah-Hartman | 8b2dceb | 2015-06-10 08:36:50 -0700 | [diff] [blame] | 606 | release_resource(r); |
| 607 | } |
| 608 | } |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 609 | } |
| 610 | EXPORT_SYMBOL_GPL(platform_device_del); |
| 611 | |
| 612 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 613 | * platform_device_register - add a platform-level device |
| 614 | * @pdev: platform device we're adding |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 615 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 616 | int platform_device_register(struct platform_device *pdev) |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 617 | { |
| 618 | device_initialize(&pdev->dev); |
Christoph Hellwig | cdfee56 | 2019-08-16 08:24:35 +0200 | [diff] [blame] | 619 | setup_pdev_dma_masks(pdev); |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 620 | return platform_device_add(pdev); |
| 621 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 622 | EXPORT_SYMBOL_GPL(platform_device_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 625 | * platform_device_unregister - unregister a platform-level device |
| 626 | * @pdev: platform device we're unregistering |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 628 | * Unregistration is done in 2 steps. First we release all resources |
| 629 | * and remove it from the subsystem, then we drop reference count by |
| 630 | * calling platform_device_put(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 632 | void platform_device_unregister(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | { |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 634 | platform_device_del(pdev); |
| 635 | platform_device_put(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 637 | EXPORT_SYMBOL_GPL(platform_device_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | /** |
Uwe Kleine-König | 01dcc60 | 2011-08-25 11:16:00 +0200 | [diff] [blame] | 640 | * platform_device_register_full - add a platform-level device with |
Uwe Kleine-König | 44f28bd | 2010-06-21 16:11:44 +0200 | [diff] [blame] | 641 | * resources and platform-specific data |
| 642 | * |
Uwe Kleine-König | 01dcc60 | 2011-08-25 11:16:00 +0200 | [diff] [blame] | 643 | * @pdevinfo: data used to create device |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 644 | * |
Jani Nikula | f0eae0e | 2010-03-11 18:11:45 +0200 | [diff] [blame] | 645 | * Returns &struct platform_device pointer on success, or ERR_PTR() on error. |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 646 | */ |
Uwe Kleine-König | 01dcc60 | 2011-08-25 11:16:00 +0200 | [diff] [blame] | 647 | struct platform_device *platform_device_register_full( |
Uwe Kleine-König | 5a3072b | 2011-12-08 22:53:29 +0100 | [diff] [blame] | 648 | const struct platform_device_info *pdevinfo) |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 649 | { |
Uwe Kleine-König | 44f28bd | 2010-06-21 16:11:44 +0200 | [diff] [blame] | 650 | int ret = -ENOMEM; |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 651 | struct platform_device *pdev; |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 652 | |
Uwe Kleine-König | 01dcc60 | 2011-08-25 11:16:00 +0200 | [diff] [blame] | 653 | pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id); |
Uwe Kleine-König | 44f28bd | 2010-06-21 16:11:44 +0200 | [diff] [blame] | 654 | if (!pdev) |
Johannes Berg | 36cf3b1 | 2019-03-01 13:24:47 +0100 | [diff] [blame] | 655 | return ERR_PTR(-ENOMEM); |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 656 | |
Uwe Kleine-König | 01dcc60 | 2011-08-25 11:16:00 +0200 | [diff] [blame] | 657 | pdev->dev.parent = pdevinfo->parent; |
Rafael J. Wysocki | ce79348 | 2015-03-16 23:49:03 +0100 | [diff] [blame] | 658 | pdev->dev.fwnode = pdevinfo->fwnode; |
Mans Rullgard | 2c1ea6a | 2019-02-21 11:29:35 +0000 | [diff] [blame] | 659 | pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode)); |
| 660 | pdev->dev.of_node_reused = pdevinfo->of_node_reused; |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 661 | |
Uwe Kleine-König | 01dcc60 | 2011-08-25 11:16:00 +0200 | [diff] [blame] | 662 | if (pdevinfo->dma_mask) { |
| 663 | /* |
| 664 | * This memory isn't freed when the device is put, |
| 665 | * I don't have a nice idea for that though. Conceptually |
| 666 | * dma_mask in struct device should not be a pointer. |
| 667 | * See http://thread.gmane.org/gmane.linux.kernel.pci/9081 |
| 668 | */ |
| 669 | pdev->dev.dma_mask = |
| 670 | kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL); |
| 671 | if (!pdev->dev.dma_mask) |
| 672 | goto err; |
| 673 | |
Qian Cai | 967d301 | 2019-01-03 15:29:05 -0800 | [diff] [blame] | 674 | kmemleak_ignore(pdev->dev.dma_mask); |
| 675 | |
Uwe Kleine-König | 01dcc60 | 2011-08-25 11:16:00 +0200 | [diff] [blame] | 676 | *pdev->dev.dma_mask = pdevinfo->dma_mask; |
| 677 | pdev->dev.coherent_dma_mask = pdevinfo->dma_mask; |
| 678 | } |
| 679 | |
| 680 | ret = platform_device_add_resources(pdev, |
| 681 | pdevinfo->res, pdevinfo->num_res); |
Anton Vorontsov | 807508c | 2010-09-07 17:31:54 +0400 | [diff] [blame] | 682 | if (ret) |
| 683 | goto err; |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 684 | |
Uwe Kleine-König | 01dcc60 | 2011-08-25 11:16:00 +0200 | [diff] [blame] | 685 | ret = platform_device_add_data(pdev, |
| 686 | pdevinfo->data, pdevinfo->size_data); |
Anton Vorontsov | 807508c | 2010-09-07 17:31:54 +0400 | [diff] [blame] | 687 | if (ret) |
| 688 | goto err; |
Uwe Kleine-König | 44f28bd | 2010-06-21 16:11:44 +0200 | [diff] [blame] | 689 | |
Heikki Krogerus | f4d0526 | 2016-03-29 14:52:23 +0300 | [diff] [blame] | 690 | if (pdevinfo->properties) { |
| 691 | ret = platform_device_add_properties(pdev, |
| 692 | pdevinfo->properties); |
Mika Westerberg | 00bbc1d | 2015-11-30 17:11:38 +0200 | [diff] [blame] | 693 | if (ret) |
| 694 | goto err; |
| 695 | } |
| 696 | |
Uwe Kleine-König | 44f28bd | 2010-06-21 16:11:44 +0200 | [diff] [blame] | 697 | ret = platform_device_add(pdev); |
| 698 | if (ret) { |
| 699 | err: |
Rafael J. Wysocki | 7b19981 | 2013-11-11 22:41:56 +0100 | [diff] [blame] | 700 | ACPI_COMPANION_SET(&pdev->dev, NULL); |
Uwe Kleine-König | 01dcc60 | 2011-08-25 11:16:00 +0200 | [diff] [blame] | 701 | kfree(pdev->dev.dma_mask); |
Uwe Kleine-König | 44f28bd | 2010-06-21 16:11:44 +0200 | [diff] [blame] | 702 | platform_device_put(pdev); |
| 703 | return ERR_PTR(ret); |
| 704 | } |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 705 | |
| 706 | return pdev; |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 707 | } |
Uwe Kleine-König | 01dcc60 | 2011-08-25 11:16:00 +0200 | [diff] [blame] | 708 | EXPORT_SYMBOL_GPL(platform_device_register_full); |
Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 709 | |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 710 | static int platform_drv_probe(struct device *_dev) |
| 711 | { |
| 712 | struct platform_driver *drv = to_platform_driver(_dev->driver); |
| 713 | struct platform_device *dev = to_platform_device(_dev); |
Rafael J. Wysocki | 94d76d5 | 2012-11-26 10:04:53 +0100 | [diff] [blame] | 714 | int ret; |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 715 | |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 716 | ret = of_clk_set_defaults(_dev->of_node, false); |
| 717 | if (ret < 0) |
| 718 | return ret; |
| 719 | |
Ulf Hansson | cb51841 | 2014-09-19 20:27:38 +0200 | [diff] [blame] | 720 | ret = dev_pm_domain_attach(_dev, true); |
Ulf Hansson | 88a9769 | 2018-04-26 10:53:06 +0200 | [diff] [blame] | 721 | if (ret) |
| 722 | goto out; |
| 723 | |
| 724 | if (drv->probe) { |
| 725 | ret = drv->probe(dev); |
| 726 | if (ret) |
| 727 | dev_pm_domain_detach(_dev, true); |
Ulf Hansson | cb51841 | 2014-09-19 20:27:38 +0200 | [diff] [blame] | 728 | } |
Rafael J. Wysocki | 94d76d5 | 2012-11-26 10:04:53 +0100 | [diff] [blame] | 729 | |
Ulf Hansson | 88a9769 | 2018-04-26 10:53:06 +0200 | [diff] [blame] | 730 | out: |
Johan Hovold | 3f9120b | 2013-09-23 16:27:26 +0200 | [diff] [blame] | 731 | if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) { |
| 732 | dev_warn(_dev, "probe deferral not supported\n"); |
| 733 | ret = -ENXIO; |
| 734 | } |
| 735 | |
Rafael J. Wysocki | 94d76d5 | 2012-11-26 10:04:53 +0100 | [diff] [blame] | 736 | return ret; |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 737 | } |
| 738 | |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 739 | static int platform_drv_probe_fail(struct device *_dev) |
| 740 | { |
| 741 | return -ENXIO; |
| 742 | } |
| 743 | |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 744 | static int platform_drv_remove(struct device *_dev) |
| 745 | { |
| 746 | struct platform_driver *drv = to_platform_driver(_dev->driver); |
| 747 | struct platform_device *dev = to_platform_device(_dev); |
Uwe Kleine-König | b8b2c7d | 2015-08-07 07:19:22 +0200 | [diff] [blame] | 748 | int ret = 0; |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 749 | |
Uwe Kleine-König | b8b2c7d | 2015-08-07 07:19:22 +0200 | [diff] [blame] | 750 | if (drv->remove) |
| 751 | ret = drv->remove(dev); |
Ulf Hansson | cb51841 | 2014-09-19 20:27:38 +0200 | [diff] [blame] | 752 | dev_pm_domain_detach(_dev, true); |
Rafael J. Wysocki | 94d76d5 | 2012-11-26 10:04:53 +0100 | [diff] [blame] | 753 | |
| 754 | return ret; |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | static void platform_drv_shutdown(struct device *_dev) |
| 758 | { |
| 759 | struct platform_driver *drv = to_platform_driver(_dev->driver); |
| 760 | struct platform_device *dev = to_platform_device(_dev); |
| 761 | |
Uwe Kleine-König | b8b2c7d | 2015-08-07 07:19:22 +0200 | [diff] [blame] | 762 | if (drv->shutdown) |
| 763 | drv->shutdown(dev); |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 766 | /** |
Libo Chen | 9447057 | 2013-05-25 12:40:50 +0800 | [diff] [blame] | 767 | * __platform_driver_register - register a driver for platform-level devices |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 768 | * @drv: platform driver structure |
Randy Dunlap | 08801f9 | 2013-07-14 17:43:06 -0700 | [diff] [blame] | 769 | * @owner: owning module/driver |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 770 | */ |
Libo Chen | 9447057 | 2013-05-25 12:40:50 +0800 | [diff] [blame] | 771 | int __platform_driver_register(struct platform_driver *drv, |
| 772 | struct module *owner) |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 773 | { |
Libo Chen | 9447057 | 2013-05-25 12:40:50 +0800 | [diff] [blame] | 774 | drv->driver.owner = owner; |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 775 | drv->driver.bus = &platform_bus_type; |
Uwe Kleine-König | b8b2c7d | 2015-08-07 07:19:22 +0200 | [diff] [blame] | 776 | drv->driver.probe = platform_drv_probe; |
| 777 | drv->driver.remove = platform_drv_remove; |
| 778 | drv->driver.shutdown = platform_drv_shutdown; |
Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 779 | |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 780 | return driver_register(&drv->driver); |
| 781 | } |
Libo Chen | 9447057 | 2013-05-25 12:40:50 +0800 | [diff] [blame] | 782 | EXPORT_SYMBOL_GPL(__platform_driver_register); |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 783 | |
| 784 | /** |
Ben Hutchings | 3c31f07 | 2010-02-14 14:18:53 +0000 | [diff] [blame] | 785 | * platform_driver_unregister - unregister a driver for platform-level devices |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 786 | * @drv: platform driver structure |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 787 | */ |
| 788 | void platform_driver_unregister(struct platform_driver *drv) |
| 789 | { |
| 790 | driver_unregister(&drv->driver); |
| 791 | } |
| 792 | EXPORT_SYMBOL_GPL(platform_driver_unregister); |
| 793 | |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 794 | /** |
Wolfram Sang | c3b50dc | 2014-10-28 17:40:41 +0100 | [diff] [blame] | 795 | * __platform_driver_probe - register driver for non-hotpluggable device |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 796 | * @drv: platform driver structure |
Johan Hovold | 3f9120b | 2013-09-23 16:27:26 +0200 | [diff] [blame] | 797 | * @probe: the driver probe routine, probably from an __init section |
Wolfram Sang | c3b50dc | 2014-10-28 17:40:41 +0100 | [diff] [blame] | 798 | * @module: module which will be the owner of the driver |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 799 | * |
| 800 | * Use this instead of platform_driver_register() when you know the device |
| 801 | * is not hotpluggable and has already been registered, and you want to |
| 802 | * remove its run-once probe() infrastructure from memory after the driver |
| 803 | * has bound to the device. |
| 804 | * |
| 805 | * One typical use for this would be with drivers for controllers integrated |
| 806 | * into system-on-chip processors, where the controller devices have been |
| 807 | * configured as part of board setup. |
| 808 | * |
Johan Hovold | 3f9120b | 2013-09-23 16:27:26 +0200 | [diff] [blame] | 809 | * Note that this is incompatible with deferred probing. |
Fabio Porcedda | 647c86d | 2013-03-26 10:35:15 +0100 | [diff] [blame] | 810 | * |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 811 | * Returns zero if the driver registered and bound to a device, else returns |
| 812 | * a negative error code and with the driver not registered. |
| 813 | */ |
Wolfram Sang | c3b50dc | 2014-10-28 17:40:41 +0100 | [diff] [blame] | 814 | int __init_or_module __platform_driver_probe(struct platform_driver *drv, |
| 815 | int (*probe)(struct platform_device *), struct module *module) |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 816 | { |
| 817 | int retval, code; |
| 818 | |
Dmitry Torokhov | 5c36eb2 | 2015-03-30 16:20:07 -0700 | [diff] [blame] | 819 | if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) { |
| 820 | pr_err("%s: drivers registered with %s can not be probed asynchronously\n", |
| 821 | drv->driver.name, __func__); |
| 822 | return -EINVAL; |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | * We have to run our probes synchronously because we check if |
| 827 | * we find any devices to bind to and exit with error if there |
| 828 | * are any. |
| 829 | */ |
| 830 | drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS; |
| 831 | |
Johan Hovold | 3f9120b | 2013-09-23 16:27:26 +0200 | [diff] [blame] | 832 | /* |
| 833 | * Prevent driver from requesting probe deferral to avoid further |
| 834 | * futile probe attempts. |
| 835 | */ |
| 836 | drv->prevent_deferred_probe = true; |
| 837 | |
Dmitry Torokhov | 1a6f2a7 | 2009-10-12 20:17:41 -0700 | [diff] [blame] | 838 | /* make sure driver won't have bind/unbind attributes */ |
| 839 | drv->driver.suppress_bind_attrs = true; |
| 840 | |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 841 | /* temporary section violation during probe() */ |
| 842 | drv->probe = probe; |
Wolfram Sang | c3b50dc | 2014-10-28 17:40:41 +0100 | [diff] [blame] | 843 | retval = code = __platform_driver_register(drv, module); |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 844 | |
Dmitry Torokhov | 1a6f2a7 | 2009-10-12 20:17:41 -0700 | [diff] [blame] | 845 | /* |
| 846 | * Fixup that section violation, being paranoid about code scanning |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 847 | * the list of drivers in order to probe new devices. Check to see |
| 848 | * if the probe was successful, and make sure any forced probes of |
| 849 | * new devices fail. |
| 850 | */ |
Patrick Pannuto | d79d324 | 2010-08-06 17:12:41 -0700 | [diff] [blame] | 851 | spin_lock(&drv->driver.bus->p->klist_drivers.k_lock); |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 852 | drv->probe = NULL; |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 853 | if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list)) |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 854 | retval = -ENODEV; |
| 855 | drv->driver.probe = platform_drv_probe_fail; |
Patrick Pannuto | d79d324 | 2010-08-06 17:12:41 -0700 | [diff] [blame] | 856 | spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock); |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 857 | |
| 858 | if (code != retval) |
| 859 | platform_driver_unregister(drv); |
| 860 | return retval; |
| 861 | } |
Wolfram Sang | c3b50dc | 2014-10-28 17:40:41 +0100 | [diff] [blame] | 862 | EXPORT_SYMBOL_GPL(__platform_driver_probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
Dmitry Torokhov | ecdf6ce | 2009-12-29 20:11:20 -0800 | [diff] [blame] | 864 | /** |
Wolfram Sang | 291f653 | 2014-10-28 17:40:42 +0100 | [diff] [blame] | 865 | * __platform_create_bundle - register driver and create corresponding device |
Dmitry Torokhov | ecdf6ce | 2009-12-29 20:11:20 -0800 | [diff] [blame] | 866 | * @driver: platform driver structure |
| 867 | * @probe: the driver probe routine, probably from an __init section |
| 868 | * @res: set of resources that needs to be allocated for the device |
| 869 | * @n_res: number of resources |
| 870 | * @data: platform specific data for this platform device |
| 871 | * @size: size of platform specific data |
Wolfram Sang | 291f653 | 2014-10-28 17:40:42 +0100 | [diff] [blame] | 872 | * @module: module which will be the owner of the driver |
Dmitry Torokhov | ecdf6ce | 2009-12-29 20:11:20 -0800 | [diff] [blame] | 873 | * |
| 874 | * Use this in legacy-style modules that probe hardware directly and |
| 875 | * register a single platform device and corresponding platform driver. |
Jani Nikula | f0eae0e | 2010-03-11 18:11:45 +0200 | [diff] [blame] | 876 | * |
| 877 | * Returns &struct platform_device pointer on success, or ERR_PTR() on error. |
Dmitry Torokhov | ecdf6ce | 2009-12-29 20:11:20 -0800 | [diff] [blame] | 878 | */ |
Wolfram Sang | 291f653 | 2014-10-28 17:40:42 +0100 | [diff] [blame] | 879 | struct platform_device * __init_or_module __platform_create_bundle( |
Dmitry Torokhov | ecdf6ce | 2009-12-29 20:11:20 -0800 | [diff] [blame] | 880 | struct platform_driver *driver, |
| 881 | int (*probe)(struct platform_device *), |
| 882 | struct resource *res, unsigned int n_res, |
Wolfram Sang | 291f653 | 2014-10-28 17:40:42 +0100 | [diff] [blame] | 883 | const void *data, size_t size, struct module *module) |
Dmitry Torokhov | ecdf6ce | 2009-12-29 20:11:20 -0800 | [diff] [blame] | 884 | { |
| 885 | struct platform_device *pdev; |
| 886 | int error; |
| 887 | |
| 888 | pdev = platform_device_alloc(driver->driver.name, -1); |
| 889 | if (!pdev) { |
| 890 | error = -ENOMEM; |
| 891 | goto err_out; |
| 892 | } |
| 893 | |
Anton Vorontsov | 807508c | 2010-09-07 17:31:54 +0400 | [diff] [blame] | 894 | error = platform_device_add_resources(pdev, res, n_res); |
| 895 | if (error) |
| 896 | goto err_pdev_put; |
Dmitry Torokhov | ecdf6ce | 2009-12-29 20:11:20 -0800 | [diff] [blame] | 897 | |
Anton Vorontsov | 807508c | 2010-09-07 17:31:54 +0400 | [diff] [blame] | 898 | error = platform_device_add_data(pdev, data, size); |
| 899 | if (error) |
| 900 | goto err_pdev_put; |
Dmitry Torokhov | ecdf6ce | 2009-12-29 20:11:20 -0800 | [diff] [blame] | 901 | |
| 902 | error = platform_device_add(pdev); |
| 903 | if (error) |
| 904 | goto err_pdev_put; |
| 905 | |
Wolfram Sang | 291f653 | 2014-10-28 17:40:42 +0100 | [diff] [blame] | 906 | error = __platform_driver_probe(driver, probe, module); |
Dmitry Torokhov | ecdf6ce | 2009-12-29 20:11:20 -0800 | [diff] [blame] | 907 | if (error) |
| 908 | goto err_pdev_del; |
| 909 | |
| 910 | return pdev; |
| 911 | |
| 912 | err_pdev_del: |
| 913 | platform_device_del(pdev); |
| 914 | err_pdev_put: |
| 915 | platform_device_put(pdev); |
| 916 | err_out: |
| 917 | return ERR_PTR(error); |
| 918 | } |
Wolfram Sang | 291f653 | 2014-10-28 17:40:42 +0100 | [diff] [blame] | 919 | EXPORT_SYMBOL_GPL(__platform_create_bundle); |
Dmitry Torokhov | ecdf6ce | 2009-12-29 20:11:20 -0800 | [diff] [blame] | 920 | |
Thierry Reding | dbe2256 | 2015-09-25 17:29:04 +0200 | [diff] [blame] | 921 | /** |
| 922 | * __platform_register_drivers - register an array of platform drivers |
| 923 | * @drivers: an array of drivers to register |
| 924 | * @count: the number of drivers to register |
| 925 | * @owner: module owning the drivers |
| 926 | * |
| 927 | * Registers platform drivers specified by an array. On failure to register a |
| 928 | * driver, all previously registered drivers will be unregistered. Callers of |
| 929 | * this API should use platform_unregister_drivers() to unregister drivers in |
| 930 | * the reverse order. |
| 931 | * |
| 932 | * Returns: 0 on success or a negative error code on failure. |
| 933 | */ |
| 934 | int __platform_register_drivers(struct platform_driver * const *drivers, |
| 935 | unsigned int count, struct module *owner) |
| 936 | { |
| 937 | unsigned int i; |
| 938 | int err; |
| 939 | |
| 940 | for (i = 0; i < count; i++) { |
| 941 | pr_debug("registering platform driver %ps\n", drivers[i]); |
| 942 | |
| 943 | err = __platform_driver_register(drivers[i], owner); |
| 944 | if (err < 0) { |
| 945 | pr_err("failed to register platform driver %ps: %d\n", |
| 946 | drivers[i], err); |
| 947 | goto error; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | return 0; |
| 952 | |
| 953 | error: |
| 954 | while (i--) { |
| 955 | pr_debug("unregistering platform driver %ps\n", drivers[i]); |
| 956 | platform_driver_unregister(drivers[i]); |
| 957 | } |
| 958 | |
| 959 | return err; |
| 960 | } |
| 961 | EXPORT_SYMBOL_GPL(__platform_register_drivers); |
| 962 | |
| 963 | /** |
| 964 | * platform_unregister_drivers - unregister an array of platform drivers |
| 965 | * @drivers: an array of drivers to unregister |
| 966 | * @count: the number of drivers to unregister |
| 967 | * |
| 968 | * Unegisters platform drivers specified by an array. This is typically used |
| 969 | * to complement an earlier call to platform_register_drivers(). Drivers are |
| 970 | * unregistered in the reverse order in which they were registered. |
| 971 | */ |
| 972 | void platform_unregister_drivers(struct platform_driver * const *drivers, |
| 973 | unsigned int count) |
| 974 | { |
| 975 | while (count--) { |
| 976 | pr_debug("unregistering platform driver %ps\n", drivers[count]); |
| 977 | platform_driver_unregister(drivers[count]); |
| 978 | } |
| 979 | } |
| 980 | EXPORT_SYMBOL_GPL(platform_unregister_drivers); |
| 981 | |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 982 | /* modalias support enables more hands-off userspace setup: |
| 983 | * (a) environment variable lets new-style hotplug events work once system is |
| 984 | * fully running: "modprobe $MODALIAS" |
| 985 | * (b) sysfs attribute lets new-style coldplug recover from hotplug events |
| 986 | * mishandled before system is fully running: "modprobe $(cat modalias)" |
| 987 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 988 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
| 989 | char *buf) |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 990 | { |
| 991 | struct platform_device *pdev = to_platform_device(dev); |
Zhang Rui | 8c4ff6d | 2014-01-14 16:46:37 +0800 | [diff] [blame] | 992 | int len; |
| 993 | |
Rob Herring | 0634c29 | 2017-03-22 09:16:27 -0500 | [diff] [blame] | 994 | len = of_device_modalias(dev, buf, PAGE_SIZE); |
Zhang Rui | b9f7306 | 2014-01-14 16:46:38 +0800 | [diff] [blame] | 995 | if (len != -ENODEV) |
| 996 | return len; |
| 997 | |
Zhang Rui | 8c4ff6d | 2014-01-14 16:46:37 +0800 | [diff] [blame] | 998 | len = acpi_device_modalias(dev, buf, PAGE_SIZE -1); |
| 999 | if (len != -ENODEV) |
| 1000 | return len; |
| 1001 | |
Greg Kroah-Hartman | 391c032 | 2019-04-29 19:49:21 +0200 | [diff] [blame] | 1002 | len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name); |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 1003 | |
| 1004 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
| 1005 | } |
Greg Kroah-Hartman | d06262e | 2013-08-23 14:24:37 -0700 | [diff] [blame] | 1006 | static DEVICE_ATTR_RO(modalias); |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 1007 | |
Kim Phillips | 3d713e0 | 2014-06-02 19:42:58 -0500 | [diff] [blame] | 1008 | static ssize_t driver_override_store(struct device *dev, |
| 1009 | struct device_attribute *attr, |
| 1010 | const char *buf, size_t count) |
| 1011 | { |
| 1012 | struct platform_device *pdev = to_platform_device(dev); |
Adrian Salido | 62655397 | 2017-04-25 16:55:26 -0700 | [diff] [blame] | 1013 | char *driver_override, *old, *cp; |
Kim Phillips | 3d713e0 | 2014-06-02 19:42:58 -0500 | [diff] [blame] | 1014 | |
Nicolai Stange | bf563b0 | 2017-09-11 09:45:42 +0200 | [diff] [blame] | 1015 | /* We need to keep extra room for a newline */ |
| 1016 | if (count >= (PAGE_SIZE - 1)) |
Kim Phillips | 3d713e0 | 2014-06-02 19:42:58 -0500 | [diff] [blame] | 1017 | return -EINVAL; |
| 1018 | |
| 1019 | driver_override = kstrndup(buf, count, GFP_KERNEL); |
| 1020 | if (!driver_override) |
| 1021 | return -ENOMEM; |
| 1022 | |
| 1023 | cp = strchr(driver_override, '\n'); |
| 1024 | if (cp) |
| 1025 | *cp = '\0'; |
| 1026 | |
Adrian Salido | 62655397 | 2017-04-25 16:55:26 -0700 | [diff] [blame] | 1027 | device_lock(dev); |
| 1028 | old = pdev->driver_override; |
Kim Phillips | 3d713e0 | 2014-06-02 19:42:58 -0500 | [diff] [blame] | 1029 | if (strlen(driver_override)) { |
| 1030 | pdev->driver_override = driver_override; |
| 1031 | } else { |
| 1032 | kfree(driver_override); |
| 1033 | pdev->driver_override = NULL; |
| 1034 | } |
Adrian Salido | 62655397 | 2017-04-25 16:55:26 -0700 | [diff] [blame] | 1035 | device_unlock(dev); |
Kim Phillips | 3d713e0 | 2014-06-02 19:42:58 -0500 | [diff] [blame] | 1036 | |
| 1037 | kfree(old); |
| 1038 | |
| 1039 | return count; |
| 1040 | } |
| 1041 | |
| 1042 | static ssize_t driver_override_show(struct device *dev, |
| 1043 | struct device_attribute *attr, char *buf) |
| 1044 | { |
| 1045 | struct platform_device *pdev = to_platform_device(dev); |
Adrian Salido | 62655397 | 2017-04-25 16:55:26 -0700 | [diff] [blame] | 1046 | ssize_t len; |
Kim Phillips | 3d713e0 | 2014-06-02 19:42:58 -0500 | [diff] [blame] | 1047 | |
Adrian Salido | 62655397 | 2017-04-25 16:55:26 -0700 | [diff] [blame] | 1048 | device_lock(dev); |
| 1049 | len = sprintf(buf, "%s\n", pdev->driver_override); |
| 1050 | device_unlock(dev); |
| 1051 | return len; |
Kim Phillips | 3d713e0 | 2014-06-02 19:42:58 -0500 | [diff] [blame] | 1052 | } |
| 1053 | static DEVICE_ATTR_RW(driver_override); |
| 1054 | |
| 1055 | |
Greg Kroah-Hartman | d06262e | 2013-08-23 14:24:37 -0700 | [diff] [blame] | 1056 | static struct attribute *platform_dev_attrs[] = { |
| 1057 | &dev_attr_modalias.attr, |
Kim Phillips | 3d713e0 | 2014-06-02 19:42:58 -0500 | [diff] [blame] | 1058 | &dev_attr_driver_override.attr, |
Greg Kroah-Hartman | d06262e | 2013-08-23 14:24:37 -0700 | [diff] [blame] | 1059 | NULL, |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 1060 | }; |
Greg Kroah-Hartman | d06262e | 2013-08-23 14:24:37 -0700 | [diff] [blame] | 1061 | ATTRIBUTE_GROUPS(platform_dev); |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 1062 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1063 | static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 1064 | { |
| 1065 | struct platform_device *pdev = to_platform_device(dev); |
Grant Likely | eca3930 | 2010-06-08 07:48:21 -0600 | [diff] [blame] | 1066 | int rc; |
| 1067 | |
| 1068 | /* Some devices have extra OF data and an OF-style MODALIAS */ |
Fabio Porcedda | 0258e18 | 2013-03-26 10:35:16 +0100 | [diff] [blame] | 1069 | rc = of_device_uevent_modalias(dev, env); |
Grant Likely | eca3930 | 2010-06-08 07:48:21 -0600 | [diff] [blame] | 1070 | if (rc != -ENODEV) |
| 1071 | return rc; |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 1072 | |
Zhang Rui | 8c4ff6d | 2014-01-14 16:46:37 +0800 | [diff] [blame] | 1073 | rc = acpi_device_uevent_modalias(dev, env); |
| 1074 | if (rc != -ENODEV) |
| 1075 | return rc; |
| 1076 | |
Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 1077 | add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, |
Greg Kroah-Hartman | 391c032 | 2019-04-29 19:49:21 +0200 | [diff] [blame] | 1078 | pdev->name); |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 1079 | return 0; |
| 1080 | } |
| 1081 | |
Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 1082 | static const struct platform_device_id *platform_match_id( |
Uwe Kleine-König | 831fad2 | 2010-01-26 09:35:00 +0100 | [diff] [blame] | 1083 | const struct platform_device_id *id, |
Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 1084 | struct platform_device *pdev) |
| 1085 | { |
| 1086 | while (id->name[0]) { |
Greg Kroah-Hartman | 391c032 | 2019-04-29 19:49:21 +0200 | [diff] [blame] | 1087 | if (strcmp(pdev->name, id->name) == 0) { |
Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 1088 | pdev->id_entry = id; |
| 1089 | return id; |
| 1090 | } |
| 1091 | id++; |
| 1092 | } |
| 1093 | return NULL; |
| 1094 | } |
| 1095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1097 | * platform_match - bind platform device to platform driver. |
| 1098 | * @dev: device. |
| 1099 | * @drv: driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1101 | * Platform device IDs are assumed to be encoded like this: |
| 1102 | * "<name><instance>", where <name> is a short description of the type of |
| 1103 | * device, like "pci" or "floppy", and <instance> is the enumerated |
| 1104 | * instance of the device, like '0' or '42'. Driver IDs are simply |
| 1105 | * "<name>". So, extract the <name> from the platform_device structure, |
| 1106 | * and compare it against the name of the driver. Return whether they match |
| 1107 | * or not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1109 | static int platform_match(struct device *dev, struct device_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | { |
Eric Miao | 71b3e0c | 2009-01-31 22:47:44 +0800 | [diff] [blame] | 1111 | struct platform_device *pdev = to_platform_device(dev); |
Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 1112 | struct platform_driver *pdrv = to_platform_driver(drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | |
Kim Phillips | 3d713e0 | 2014-06-02 19:42:58 -0500 | [diff] [blame] | 1114 | /* When driver_override is set, only bind to the matching driver */ |
| 1115 | if (pdev->driver_override) |
| 1116 | return !strcmp(pdev->driver_override, drv->name); |
| 1117 | |
Grant Likely | 0521215 | 2010-06-08 07:48:20 -0600 | [diff] [blame] | 1118 | /* Attempt an OF style match first */ |
| 1119 | if (of_driver_match_device(dev, drv)) |
| 1120 | return 1; |
| 1121 | |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 1122 | /* Then try ACPI style match */ |
| 1123 | if (acpi_driver_match_device(dev, drv)) |
| 1124 | return 1; |
| 1125 | |
Grant Likely | 0521215 | 2010-06-08 07:48:20 -0600 | [diff] [blame] | 1126 | /* Then try to match against the id table */ |
Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 1127 | if (pdrv->id_table) |
| 1128 | return platform_match_id(pdrv->id_table, pdev) != NULL; |
| 1129 | |
| 1130 | /* fall-back to driver name match */ |
Greg Kroah-Hartman | 391c032 | 2019-04-29 19:49:21 +0200 | [diff] [blame] | 1131 | return (strcmp(pdev->name, drv->name) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1134 | #ifdef CONFIG_PM_SLEEP |
| 1135 | |
| 1136 | static int platform_legacy_suspend(struct device *dev, pm_message_t mesg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | { |
Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 1138 | struct platform_driver *pdrv = to_platform_driver(dev->driver); |
| 1139 | struct platform_device *pdev = to_platform_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | int ret = 0; |
| 1141 | |
Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 1142 | if (dev->driver && pdrv->suspend) |
| 1143 | ret = pdrv->suspend(pdev, mesg); |
David Brownell | 386415d8 | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 1144 | |
| 1145 | return ret; |
| 1146 | } |
| 1147 | |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1148 | static int platform_legacy_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | { |
Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 1150 | struct platform_driver *pdrv = to_platform_driver(dev->driver); |
| 1151 | struct platform_device *pdev = to_platform_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | int ret = 0; |
| 1153 | |
Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 1154 | if (dev->driver && pdrv->resume) |
| 1155 | ret = pdrv->resume(pdev); |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 1156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | return ret; |
| 1158 | } |
| 1159 | |
Rafael J. Wysocki | 69c9dd1 | 2011-04-29 00:36:05 +0200 | [diff] [blame] | 1160 | #endif /* CONFIG_PM_SLEEP */ |
Magnus Damm | 9d73022 | 2009-08-20 20:25:32 +0200 | [diff] [blame] | 1161 | |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1162 | #ifdef CONFIG_SUSPEND |
| 1163 | |
Rafael J. Wysocki | 69c9dd1 | 2011-04-29 00:36:05 +0200 | [diff] [blame] | 1164 | int platform_pm_suspend(struct device *dev) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1165 | { |
| 1166 | struct device_driver *drv = dev->driver; |
| 1167 | int ret = 0; |
| 1168 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 1169 | if (!drv) |
| 1170 | return 0; |
| 1171 | |
| 1172 | if (drv->pm) { |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1173 | if (drv->pm->suspend) |
| 1174 | ret = drv->pm->suspend(dev); |
| 1175 | } else { |
| 1176 | ret = platform_legacy_suspend(dev, PMSG_SUSPEND); |
| 1177 | } |
| 1178 | |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
Rafael J. Wysocki | 69c9dd1 | 2011-04-29 00:36:05 +0200 | [diff] [blame] | 1182 | int platform_pm_resume(struct device *dev) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1183 | { |
| 1184 | struct device_driver *drv = dev->driver; |
| 1185 | int ret = 0; |
| 1186 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 1187 | if (!drv) |
| 1188 | return 0; |
| 1189 | |
| 1190 | if (drv->pm) { |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1191 | if (drv->pm->resume) |
| 1192 | ret = drv->pm->resume(dev); |
| 1193 | } else { |
| 1194 | ret = platform_legacy_resume(dev); |
| 1195 | } |
| 1196 | |
| 1197 | return ret; |
| 1198 | } |
| 1199 | |
Rafael J. Wysocki | 69c9dd1 | 2011-04-29 00:36:05 +0200 | [diff] [blame] | 1200 | #endif /* CONFIG_SUSPEND */ |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1201 | |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 1202 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1203 | |
Rafael J. Wysocki | 69c9dd1 | 2011-04-29 00:36:05 +0200 | [diff] [blame] | 1204 | int platform_pm_freeze(struct device *dev) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1205 | { |
| 1206 | struct device_driver *drv = dev->driver; |
| 1207 | int ret = 0; |
| 1208 | |
| 1209 | if (!drv) |
| 1210 | return 0; |
| 1211 | |
| 1212 | if (drv->pm) { |
| 1213 | if (drv->pm->freeze) |
| 1214 | ret = drv->pm->freeze(dev); |
| 1215 | } else { |
| 1216 | ret = platform_legacy_suspend(dev, PMSG_FREEZE); |
| 1217 | } |
| 1218 | |
| 1219 | return ret; |
| 1220 | } |
| 1221 | |
Rafael J. Wysocki | 69c9dd1 | 2011-04-29 00:36:05 +0200 | [diff] [blame] | 1222 | int platform_pm_thaw(struct device *dev) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1223 | { |
| 1224 | struct device_driver *drv = dev->driver; |
| 1225 | int ret = 0; |
| 1226 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 1227 | if (!drv) |
| 1228 | return 0; |
| 1229 | |
| 1230 | if (drv->pm) { |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1231 | if (drv->pm->thaw) |
| 1232 | ret = drv->pm->thaw(dev); |
| 1233 | } else { |
| 1234 | ret = platform_legacy_resume(dev); |
| 1235 | } |
| 1236 | |
| 1237 | return ret; |
| 1238 | } |
| 1239 | |
Rafael J. Wysocki | 69c9dd1 | 2011-04-29 00:36:05 +0200 | [diff] [blame] | 1240 | int platform_pm_poweroff(struct device *dev) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1241 | { |
| 1242 | struct device_driver *drv = dev->driver; |
| 1243 | int ret = 0; |
| 1244 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 1245 | if (!drv) |
| 1246 | return 0; |
| 1247 | |
| 1248 | if (drv->pm) { |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1249 | if (drv->pm->poweroff) |
| 1250 | ret = drv->pm->poweroff(dev); |
| 1251 | } else { |
| 1252 | ret = platform_legacy_suspend(dev, PMSG_HIBERNATE); |
| 1253 | } |
| 1254 | |
| 1255 | return ret; |
| 1256 | } |
| 1257 | |
Rafael J. Wysocki | 69c9dd1 | 2011-04-29 00:36:05 +0200 | [diff] [blame] | 1258 | int platform_pm_restore(struct device *dev) |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1259 | { |
| 1260 | struct device_driver *drv = dev->driver; |
| 1261 | int ret = 0; |
| 1262 | |
Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 1263 | if (!drv) |
| 1264 | return 0; |
| 1265 | |
| 1266 | if (drv->pm) { |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1267 | if (drv->pm->restore) |
| 1268 | ret = drv->pm->restore(dev); |
| 1269 | } else { |
| 1270 | ret = platform_legacy_resume(dev); |
| 1271 | } |
| 1272 | |
| 1273 | return ret; |
| 1274 | } |
| 1275 | |
Rafael J. Wysocki | 69c9dd1 | 2011-04-29 00:36:05 +0200 | [diff] [blame] | 1276 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1277 | |
Nipun Gupta | 07397df | 2018-04-28 08:21:58 +0530 | [diff] [blame] | 1278 | int platform_dma_configure(struct device *dev) |
| 1279 | { |
| 1280 | enum dev_dma_attr attr; |
| 1281 | int ret = 0; |
| 1282 | |
| 1283 | if (dev->of_node) { |
Christoph Hellwig | 3d6ce86 | 2018-05-03 16:25:08 +0200 | [diff] [blame] | 1284 | ret = of_dma_configure(dev, dev->of_node, true); |
Nipun Gupta | 07397df | 2018-04-28 08:21:58 +0530 | [diff] [blame] | 1285 | } else if (has_acpi_companion(dev)) { |
| 1286 | attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode)); |
Robin Murphy | e5361ca | 2018-12-06 13:20:49 -0800 | [diff] [blame] | 1287 | ret = acpi_dma_configure(dev, attr); |
Nipun Gupta | 07397df | 2018-04-28 08:21:58 +0530 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | return ret; |
| 1291 | } |
| 1292 | |
Dmitry Torokhov | d9ab771 | 2009-07-22 00:37:25 +0200 | [diff] [blame] | 1293 | static const struct dev_pm_ops platform_dev_pm_ops = { |
Rafael J. Wysocki | 8b313a3 | 2011-04-29 00:36:32 +0200 | [diff] [blame] | 1294 | .runtime_suspend = pm_generic_runtime_suspend, |
| 1295 | .runtime_resume = pm_generic_runtime_resume, |
Rafael J. Wysocki | 69c9dd1 | 2011-04-29 00:36:05 +0200 | [diff] [blame] | 1296 | USE_PLATFORM_PM_SLEEP_OPS |
Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 1297 | }; |
| 1298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | struct bus_type platform_bus_type = { |
| 1300 | .name = "platform", |
Greg Kroah-Hartman | d06262e | 2013-08-23 14:24:37 -0700 | [diff] [blame] | 1301 | .dev_groups = platform_dev_groups, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | .match = platform_match, |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 1303 | .uevent = platform_uevent, |
Nipun Gupta | 07397df | 2018-04-28 08:21:58 +0530 | [diff] [blame] | 1304 | .dma_configure = platform_dma_configure, |
Magnus Damm | 9d73022 | 2009-08-20 20:25:32 +0200 | [diff] [blame] | 1305 | .pm = &platform_dev_pm_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | }; |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 1307 | EXPORT_SYMBOL_GPL(platform_bus_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
Sami Tolvanen | 492c887 | 2019-11-12 13:41:56 -0800 | [diff] [blame] | 1309 | static inline int __platform_match(struct device *dev, const void *drv) |
| 1310 | { |
| 1311 | return platform_match(dev, (struct device_driver *)drv); |
| 1312 | } |
| 1313 | |
Suzuki K Poulose | 36f3313 | 2019-07-23 23:18:38 +0100 | [diff] [blame] | 1314 | /** |
| 1315 | * platform_find_device_by_driver - Find a platform device with a given |
| 1316 | * driver. |
| 1317 | * @start: The device to start the search from. |
| 1318 | * @drv: The device driver to look for. |
| 1319 | */ |
| 1320 | struct device *platform_find_device_by_driver(struct device *start, |
| 1321 | const struct device_driver *drv) |
| 1322 | { |
| 1323 | return bus_find_device(&platform_bus_type, start, drv, |
Sami Tolvanen | 492c887 | 2019-11-12 13:41:56 -0800 | [diff] [blame] | 1324 | __platform_match); |
Suzuki K Poulose | 36f3313 | 2019-07-23 23:18:38 +0100 | [diff] [blame] | 1325 | } |
| 1326 | EXPORT_SYMBOL_GPL(platform_find_device_by_driver); |
| 1327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | int __init platform_bus_init(void) |
| 1329 | { |
Cornelia Huck | fbfb144 | 2006-11-27 10:35:08 +0100 | [diff] [blame] | 1330 | int error; |
| 1331 | |
| 1332 | error = device_register(&platform_bus); |
Arvind Yadav | c8ae167 | 2018-03-11 11:25:49 +0530 | [diff] [blame] | 1333 | if (error) { |
| 1334 | put_device(&platform_bus); |
Cornelia Huck | fbfb144 | 2006-11-27 10:35:08 +0100 | [diff] [blame] | 1335 | return error; |
Arvind Yadav | c8ae167 | 2018-03-11 11:25:49 +0530 | [diff] [blame] | 1336 | } |
Cornelia Huck | fbfb144 | 2006-11-27 10:35:08 +0100 | [diff] [blame] | 1337 | error = bus_register(&platform_bus_type); |
| 1338 | if (error) |
| 1339 | device_unregister(&platform_bus); |
Pantelis Antoniou | 801d728 | 2014-10-28 22:36:01 +0200 | [diff] [blame] | 1340 | of_platform_register_reconfig_notifier(); |
Cornelia Huck | fbfb144 | 2006-11-27 10:35:08 +0100 | [diff] [blame] | 1341 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | } |