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