blob: 7c532548b0a62d8d0ac99d8d052de4a8d045ef18 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Chehabfe34c892019-06-18 12:34:59 -03008 * Please see Documentation/driver-api/driver-model/platform.rst for more
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * information.
10 */
11
Andrew Mortondaa41222009-08-06 16:00:44 -070012#include <linux/string.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010013#include <linux/platform_device.h>
Grant Likely05212152010-06-08 07:48:20 -060014#include <linux/of_device.h>
Rob Herring9ec36ca2014-04-23 17:57:41 -050015#include <linux/of_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/dma-mapping.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070019#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080021#include <linux/slab.h>
Magnus Damm9d730222009-08-20 20:25:32 +020022#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020023#include <linux/pm_domain.h>
Jean Delvare689ae232012-07-27 22:14:59 +020024#include <linux/idr.h>
Mika Westerberg91e56872012-10-31 22:45:02 +010025#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020026#include <linux/clk/clk-conf.h>
Kim Phillips3d713e02014-06-02 19:42:58 -050027#include <linux/limits.h>
Mika Westerberg00bbc1d2015-11-30 17:11:38 +020028#include <linux/property.h>
Qian Cai967d3012019-01-03 15:29:05 -080029#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010031#include "base.h"
Rafael J. Wysockibed2b422012-08-06 01:45:11 +020032#include "power/power.h"
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010033
Jean Delvare689ae232012-07-27 22:14:59 +020034/* For automatically allocated device IDs */
35static DEFINE_IDA(platform_devid_ida);
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010038 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050040EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080043 * platform_get_resource - get a resource for a device
44 * @dev: platform device
45 * @type: resource type
46 * @num: resource index
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080048struct resource *platform_get_resource(struct platform_device *dev,
49 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 int i;
52
53 for (i = 0; i < dev->num_resources; i++) {
54 struct resource *r = &dev->resource[i];
55
Magnus Dammc9f66162008-10-15 22:05:15 -070056 if (type == resource_type(r) && num-- == 0)
57 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
59 return NULL;
60}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050061EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Bartosz Golaszewskibb6243b2019-10-22 10:43:14 +020063#ifdef CONFIG_HAS_IOMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/**
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000065 * 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 Golaszewski7067c962019-04-01 10:16:35 +020069 * resource management
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000070 * @index: resource index
71 */
72void __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}
80EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
Bartosz Golaszewskibb6243b2019-10-22 10:43:14 +020081
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 */
90void __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 Golaszewskic9c86412019-10-22 10:43:16 +020098
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 */
108void __iomem *
109devm_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}
117EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname);
Bartosz Golaszewski837ccda2019-02-21 17:26:27 +0100118#endif /* CONFIG_HAS_IOMEM */
Bartosz Golaszewski7945f922019-02-20 11:12:39 +0000119
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200120/**
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 */
137int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +0000139#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 Herring9ec36ca2014-04-23 17:57:41 -0500145 struct resource *r;
Andy Shevchenko71564a22019-10-23 15:25:05 +0300146 int ret;
Guenter Roeckaff008a2014-06-17 15:51:02 -0700147
Andy Shevchenko71564a22019-10-23 15:25:05 +0300148 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
Guenter Roeckaff008a2014-06-17 15:51:02 -0700149 ret = of_irq_get(dev->dev.of_node, num);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +0300150 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -0700151 return ret;
152 }
Rob Herring9ec36ca2014-04-23 17:57:41 -0500153
154 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
Agustin Vega-Friasd44fa3d2017-02-02 18:23:58 -0500155 if (has_acpi_companion(&dev->dev)) {
156 if (r && r->flags & IORESOURCE_DISABLED) {
Agustin Vega-Friasd44fa3d2017-02-02 18:23:58 -0500157 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
158 if (ret)
159 return ret;
160 }
161 }
162
Linus Walleij7085a742015-02-18 17:12:18 +0100163 /*
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 Roeck60ca5e02016-09-13 20:32:44 -0700169 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 Torvalds1da177e2005-04-16 15:20:36 -0700177
Enrico Granatadaaef252019-02-11 11:01:12 -0800178 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 Norris46c42d82019-07-29 13:49:54 -0700188 if (num == 0 && has_acpi_companion(&dev->dev)) {
Andy Shevchenko71564a22019-10-23 15:25:05 +0300189 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
Brian Norris46c42d82019-07-29 13:49:54 -0700190 /* Our callers expect -ENXIO for missing IRQs. */
191 if (ret >= 0 || ret == -EPROBE_DEFER)
192 return ret;
193 }
Enrico Granatadaaef252019-02-11 11:01:12 -0800194
195 return -ENXIO;
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +0000196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200198EXPORT_SYMBOL_GPL(platform_get_irq_optional);
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700199
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 */
216int platform_get_irq(struct platform_device *dev, unsigned int num)
217{
218 int ret;
219
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200220 ret = platform_get_irq_optional(dev, num);
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700221 if (ret < 0 && ret != -EPROBE_DEFER)
222 dev_err(&dev->dev, "IRQ index %u not found\n", num);
223
224 return ret;
225}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500226EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228/**
Stephen Boyd4b835552016-01-06 17:12:47 -0800229 * 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 */
234int platform_irq_count(struct platform_device *dev)
235{
236 int ret, nr = 0;
237
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200238 while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
Stephen Boyd4b835552016-01-06 17:12:47 -0800239 nr++;
240
241 if (ret == -EPROBE_DEFER)
242 return ret;
243
244 return nr;
245}
246EXPORT_SYMBOL_GPL(platform_irq_count);
247
248/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800249 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700253 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800254struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200255 unsigned int type,
256 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 int i;
259
260 for (i = 0; i < dev->num_resources; i++) {
261 struct resource *r = &dev->resource[i];
262
Peter Ujfalusi1b8cb922012-08-23 17:10:00 +0300263 if (unlikely(!r->name))
264 continue;
265
Magnus Dammc9f66162008-10-15 22:05:15 -0700266 if (type == resource_type(r) && !strcmp(r->name, name))
267 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 return NULL;
270}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500271EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Hans de Goedef1da5672019-10-05 23:04:47 +0200273static int __platform_get_irq_byname(struct platform_device *dev,
274 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Grygorii Strashkoad696742014-05-20 13:42:02 +0300276 struct resource *r;
Andy Shevchenko71564a22019-10-23 15:25:05 +0300277 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Guenter Roeckaff008a2014-06-17 15:51:02 -0700279 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
Guenter Roeckaff008a2014-06-17 15:51:02 -0700280 ret = of_irq_get_byname(dev->dev.of_node, name);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +0300281 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -0700282 return ret;
283 }
Grygorii Strashkoad696742014-05-20 13:42:02 +0300284
285 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700286 if (r)
287 return r->start;
288
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700289 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
Hans de Goedef1da5672019-10-05 23:04:47 +0200291
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 */
301int 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 Torokhova96b2042005-12-10 01:36:28 -0500311EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313/**
Hans de Goedef1da5672019-10-05 23:04:47 +0200314 * 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 */
323int platform_get_irq_byname_optional(struct platform_device *dev,
324 const char *name)
325{
326 return __platform_get_irq_byname(dev, name);
327}
328EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
329
330/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800331 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700334 */
335int 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 Torokhova96b2042005-12-10 01:36:28 -0500350EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Russell King37c12e72005-11-05 21:19:33 +0000352struct platform_object {
353 struct platform_device pdev;
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200354 char name[];
Russell King37c12e72005-11-05 21:19:33 +0000355};
356
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200357/*
358 * Set up default DMA mask for platform devices if the they weren't
359 * previously set by the architecture / DT.
360 */
361static 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 Torvalds1da177e2005-04-16 15:20:36 -0700371/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000372 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800373 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000374 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800375 * 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 King37c12e72005-11-05 21:19:33 +0000377 */
378void platform_device_put(struct platform_device *pdev)
379{
Andy Shevchenko99fef582018-12-03 20:21:41 +0200380 if (!IS_ERR_OR_NULL(pdev))
Russell King37c12e72005-11-05 21:19:33 +0000381 put_device(&pdev->dev);
382}
383EXPORT_SYMBOL_GPL(platform_device_put);
384
385static void platform_device_release(struct device *dev)
386{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800387 struct platform_object *pa = container_of(dev, struct platform_object,
388 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000389
Grant Likely7096d042010-10-20 11:45:13 -0600390 of_device_node_put(&pa->pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000391 kfree(pa->pdev.dev.platform_data);
Samuel Ortize710d7d2011-04-08 00:43:01 +0200392 kfree(pa->pdev.mfd_cell);
Russell King37c12e72005-11-05 21:19:33 +0000393 kfree(pa->pdev.resource);
Kim Phillips3d713e02014-06-02 19:42:58 -0500394 kfree(pa->pdev.driver_override);
Russell King37c12e72005-11-05 21:19:33 +0000395 kfree(pa);
396}
397
398/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000399 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800400 * @name: base name of the device we're adding
401 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000402 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800403 * 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 King37c12e72005-11-05 21:19:33 +0000405 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200406struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000407{
408 struct platform_object *pa;
409
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200410 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000411 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 Hellwigcdfee562019-08-16 08:24:35 +0200417 setup_pdev_dma_masks(&pa->pdev);
Russell King37c12e72005-11-05 21:19:33 +0000418 }
419
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500420 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000421}
422EXPORT_SYMBOL_GPL(platform_device_alloc);
423
424/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000425 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800426 * @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 King37c12e72005-11-05 21:19:33 +0000429 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800430 * 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 King37c12e72005-11-05 21:19:33 +0000433 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800434int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100435 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000436{
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200437 struct resource *r = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000438
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200439 if (res) {
440 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
441 if (!r)
442 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000443 }
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200444
Uwe Kleine-König4a03d6f2011-04-20 09:44:45 +0200445 kfree(pdev->resource);
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200446 pdev->resource = r;
447 pdev->num_resources = num;
448 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000449}
450EXPORT_SYMBOL_GPL(platform_device_add_resources);
451
452/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000453 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800454 * @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 King37c12e72005-11-05 21:19:33 +0000457 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800458 * 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 King37c12e72005-11-05 21:19:33 +0000461 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800462int platform_device_add_data(struct platform_device *pdev, const void *data,
463 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000464{
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200465 void *d = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000466
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200467 if (data) {
468 d = kmemdup(data, size, GFP_KERNEL);
469 if (!d)
470 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000471 }
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200472
Uwe Kleine-König251e0312011-04-20 09:44:43 +0200473 kfree(pdev->dev.platform_data);
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200474 pdev->dev.platform_data = d;
475 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000476}
477EXPORT_SYMBOL_GPL(platform_device_add_data);
478
479/**
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200480 * platform_device_add_properties - add built-in properties to a platform device
481 * @pdev: platform device to add properties to
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300482 * @properties: null terminated array of properties to add
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200483 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300484 * 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 Westerberg00bbc1d2015-11-30 17:11:38 +0200487 */
488int platform_device_add_properties(struct platform_device *pdev,
Jan Kiszka277036f2017-06-02 07:43:27 +0200489 const struct property_entry *properties)
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200490{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300491 return device_add_properties(&pdev->dev, properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200492}
493EXPORT_SYMBOL_GPL(platform_device_add_properties);
494
495/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800496 * platform_device_add - add a platform device to device hierarchy
497 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800499 * This is part 2 of platform_device_register(), though may be called
500 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
Russell King37c12e72005-11-05 21:19:33 +0000502int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Jean Delvare689ae232012-07-27 22:14:59 +0200504 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
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 Delvare689ae232012-07-27 22:14:59 +0200514 switch (pdev->id) {
515 default:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100516 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Jean Delvare689ae232012-07-27 22:14:59 +0200517 break;
518 case PLATFORM_DEVID_NONE:
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700519 dev_set_name(&pdev->dev, "%s", pdev->name);
Jean Delvare689ae232012-07-27 22:14:59 +0200520 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-Hartman5da7f702015-06-10 08:38:02 -0700529 goto err_out;
Jean Delvare689ae232012-07-27 22:14:59 +0200530 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 Torvalds1da177e2005-04-16 15:20:36 -0700535
536 for (i = 0; i < pdev->num_resources; i++) {
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700537 struct resource *p, *r = &pdev->resource[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100540 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 p = r->parent;
543 if (!p) {
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700544 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 p = &iomem_resource;
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700546 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 p = &ioport_resource;
548 }
549
Andy Shevchenko25ebcb72019-04-04 11:11:58 +0300550 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-Hartman5da7f702015-06-10 08:38:02 -0700556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
559 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100560 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Russell Kinge3915532006-05-06 08:15:26 +0100562 ret = device_add(&pdev->dev);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700563 if (ret == 0)
564 return ret;
565
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700566 failed:
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700567 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 Likely7f5dcaf2015-06-07 15:20:11 +0100574 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700575 release_resource(r);
576 }
Magnus Dammc9f66162008-10-15 22:05:15 -0700577
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700578 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return ret;
580}
Russell King37c12e72005-11-05 21:19:33 +0000581EXPORT_SYMBOL_GPL(platform_device_add);
582
583/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800584 * platform_device_del - remove a platform-level device
585 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500586 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800587 * 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 Torokhov93ce3062005-12-10 01:36:27 -0500590 */
591void platform_device_del(struct platform_device *pdev)
592{
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700593 int i;
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500594
Andy Shevchenko99fef582018-12-03 20:21:41 +0200595 if (!IS_ERR_OR_NULL(pdev)) {
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700596 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 Likely7f5dcaf2015-06-07 15:20:11 +0100605 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700606 release_resource(r);
607 }
608 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500609}
610EXPORT_SYMBOL_GPL(platform_device_del);
611
612/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800613 * platform_device_register - add a platform-level device
614 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000615 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800616int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000617{
618 device_initialize(&pdev->dev);
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200619 setup_pdev_dma_masks(pdev);
Russell King37c12e72005-11-05 21:19:33 +0000620 return platform_device_add(pdev);
621}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500622EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800625 * platform_device_unregister - unregister a platform-level device
626 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800628 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700631 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800632void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500634 platform_device_del(pdev);
635 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500637EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/**
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200640 * platform_device_register_full - add a platform-level device with
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200641 * resources and platform-specific data
642 *
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200643 * @pdevinfo: data used to create device
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700644 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200645 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700646 */
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200647struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +0100648 const struct platform_device_info *pdevinfo)
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700649{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200650 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700651 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700652
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200653 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200654 if (!pdev)
Johannes Berg36cf3b12019-03-01 13:24:47 +0100655 return ERR_PTR(-ENOMEM);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700656
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200657 pdev->dev.parent = pdevinfo->parent;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100658 pdev->dev.fwnode = pdevinfo->fwnode;
Mans Rullgard2c1ea6a2019-02-21 11:29:35 +0000659 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
660 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700661
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200662 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 Cai967d3012019-01-03 15:29:05 -0800674 kmemleak_ignore(pdev->dev.dma_mask);
675
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200676 *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 Vorontsov807508c2010-09-07 17:31:54 +0400682 if (ret)
683 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700684
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200685 ret = platform_device_add_data(pdev,
686 pdevinfo->data, pdevinfo->size_data);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400687 if (ret)
688 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200689
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300690 if (pdevinfo->properties) {
691 ret = platform_device_add_properties(pdev,
692 pdevinfo->properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200693 if (ret)
694 goto err;
695 }
696
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200697 ret = platform_device_add(pdev);
698 if (ret) {
699err:
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100700 ACPI_COMPANION_SET(&pdev->dev, NULL);
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200701 kfree(pdev->dev.dma_mask);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200702 platform_device_put(pdev);
703 return ERR_PTR(ret);
704 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700705
706 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700707}
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200708EXPORT_SYMBOL_GPL(platform_device_register_full);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700709
Russell King00d3dcd2005-11-09 17:23:39 +0000710static 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. Wysocki94d76d52012-11-26 10:04:53 +0100714 int ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000715
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200716 ret = of_clk_set_defaults(_dev->of_node, false);
717 if (ret < 0)
718 return ret;
719
Ulf Hanssoncb518412014-09-19 20:27:38 +0200720 ret = dev_pm_domain_attach(_dev, true);
Ulf Hansson88a97692018-04-26 10:53:06 +0200721 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 Hanssoncb518412014-09-19 20:27:38 +0200728 }
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100729
Ulf Hansson88a97692018-04-26 10:53:06 +0200730out:
Johan Hovold3f9120b2013-09-23 16:27:26 +0200731 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
732 dev_warn(_dev, "probe deferral not supported\n");
733 ret = -ENXIO;
734 }
735
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100736 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000737}
738
David Brownellc67334f2006-11-16 23:28:47 -0800739static int platform_drv_probe_fail(struct device *_dev)
740{
741 return -ENXIO;
742}
743
Russell King00d3dcd2005-11-09 17:23:39 +0000744static 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önigb8b2c7d2015-08-07 07:19:22 +0200748 int ret = 0;
Russell King00d3dcd2005-11-09 17:23:39 +0000749
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200750 if (drv->remove)
751 ret = drv->remove(dev);
Ulf Hanssoncb518412014-09-19 20:27:38 +0200752 dev_pm_domain_detach(_dev, true);
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100753
754 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000755}
756
757static 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önigb8b2c7d2015-08-07 07:19:22 +0200762 if (drv->shutdown)
763 drv->shutdown(dev);
Russell King00d3dcd2005-11-09 17:23:39 +0000764}
765
Russell King00d3dcd2005-11-09 17:23:39 +0000766/**
Libo Chen94470572013-05-25 12:40:50 +0800767 * __platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800768 * @drv: platform driver structure
Randy Dunlap08801f92013-07-14 17:43:06 -0700769 * @owner: owning module/driver
Russell King00d3dcd2005-11-09 17:23:39 +0000770 */
Libo Chen94470572013-05-25 12:40:50 +0800771int __platform_driver_register(struct platform_driver *drv,
772 struct module *owner)
Russell King00d3dcd2005-11-09 17:23:39 +0000773{
Libo Chen94470572013-05-25 12:40:50 +0800774 drv->driver.owner = owner;
Russell King00d3dcd2005-11-09 17:23:39 +0000775 drv->driver.bus = &platform_bus_type;
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200776 drv->driver.probe = platform_drv_probe;
777 drv->driver.remove = platform_drv_remove;
778 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200779
Russell King00d3dcd2005-11-09 17:23:39 +0000780 return driver_register(&drv->driver);
781}
Libo Chen94470572013-05-25 12:40:50 +0800782EXPORT_SYMBOL_GPL(__platform_driver_register);
Russell King00d3dcd2005-11-09 17:23:39 +0000783
784/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000785 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800786 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000787 */
788void platform_driver_unregister(struct platform_driver *drv)
789{
790 driver_unregister(&drv->driver);
791}
792EXPORT_SYMBOL_GPL(platform_driver_unregister);
793
David Brownellc67334f2006-11-16 23:28:47 -0800794/**
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100795 * __platform_driver_probe - register driver for non-hotpluggable device
David Brownellc67334f2006-11-16 23:28:47 -0800796 * @drv: platform driver structure
Johan Hovold3f9120b2013-09-23 16:27:26 +0200797 * @probe: the driver probe routine, probably from an __init section
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100798 * @module: module which will be the owner of the driver
David Brownellc67334f2006-11-16 23:28:47 -0800799 *
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 Hovold3f9120b2013-09-23 16:27:26 +0200809 * Note that this is incompatible with deferred probing.
Fabio Porcedda647c86d2013-03-26 10:35:15 +0100810 *
David Brownellc67334f2006-11-16 23:28:47 -0800811 * 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 Sangc3b50dc2014-10-28 17:40:41 +0100814int __init_or_module __platform_driver_probe(struct platform_driver *drv,
815 int (*probe)(struct platform_device *), struct module *module)
David Brownellc67334f2006-11-16 23:28:47 -0800816{
817 int retval, code;
818
Dmitry Torokhov5c36eb22015-03-30 16:20:07 -0700819 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 Hovold3f9120b2013-09-23 16:27:26 +0200832 /*
833 * Prevent driver from requesting probe deferral to avoid further
834 * futile probe attempts.
835 */
836 drv->prevent_deferred_probe = true;
837
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700838 /* make sure driver won't have bind/unbind attributes */
839 drv->driver.suppress_bind_attrs = true;
840
David Brownellc67334f2006-11-16 23:28:47 -0800841 /* temporary section violation during probe() */
842 drv->probe = probe;
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100843 retval = code = __platform_driver_register(drv, module);
David Brownellc67334f2006-11-16 23:28:47 -0800844
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700845 /*
846 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800847 * 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 Pannutod79d3242010-08-06 17:12:41 -0700851 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800852 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800853 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800854 retval = -ENODEV;
855 drv->driver.probe = platform_drv_probe_fail;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700856 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800857
858 if (code != retval)
859 platform_driver_unregister(drv);
860 return retval;
861}
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100862EXPORT_SYMBOL_GPL(__platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800864/**
Wolfram Sang291f6532014-10-28 17:40:42 +0100865 * __platform_create_bundle - register driver and create corresponding device
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800866 * @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 Sang291f6532014-10-28 17:40:42 +0100872 * @module: module which will be the owner of the driver
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800873 *
874 * Use this in legacy-style modules that probe hardware directly and
875 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200876 *
877 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800878 */
Wolfram Sang291f6532014-10-28 17:40:42 +0100879struct platform_device * __init_or_module __platform_create_bundle(
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800880 struct platform_driver *driver,
881 int (*probe)(struct platform_device *),
882 struct resource *res, unsigned int n_res,
Wolfram Sang291f6532014-10-28 17:40:42 +0100883 const void *data, size_t size, struct module *module)
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800884{
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 Vorontsov807508c2010-09-07 17:31:54 +0400894 error = platform_device_add_resources(pdev, res, n_res);
895 if (error)
896 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800897
Anton Vorontsov807508c2010-09-07 17:31:54 +0400898 error = platform_device_add_data(pdev, data, size);
899 if (error)
900 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800901
902 error = platform_device_add(pdev);
903 if (error)
904 goto err_pdev_put;
905
Wolfram Sang291f6532014-10-28 17:40:42 +0100906 error = __platform_driver_probe(driver, probe, module);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800907 if (error)
908 goto err_pdev_del;
909
910 return pdev;
911
912err_pdev_del:
913 platform_device_del(pdev);
914err_pdev_put:
915 platform_device_put(pdev);
916err_out:
917 return ERR_PTR(error);
918}
Wolfram Sang291f6532014-10-28 17:40:42 +0100919EXPORT_SYMBOL_GPL(__platform_create_bundle);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800920
Thierry Redingdbe22562015-09-25 17:29:04 +0200921/**
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 */
934int __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
953error:
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}
961EXPORT_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 */
972void 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}
980EXPORT_SYMBOL_GPL(platform_unregister_drivers);
981
David Brownella0245f72006-05-29 10:37:33 -0700982/* 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-Hartman4a3ad202008-01-24 22:50:12 -0800988static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
989 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700990{
991 struct platform_device *pdev = to_platform_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800992 int len;
993
Rob Herring0634c292017-03-22 09:16:27 -0500994 len = of_device_modalias(dev, buf, PAGE_SIZE);
Zhang Ruib9f73062014-01-14 16:46:38 +0800995 if (len != -ENODEV)
996 return len;
997
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800998 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
999 if (len != -ENODEV)
1000 return len;
1001
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001002 len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -07001003
1004 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
1005}
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001006static DEVICE_ATTR_RO(modalias);
David Brownella0245f72006-05-29 10:37:33 -07001007
Kim Phillips3d713e02014-06-02 19:42:58 -05001008static 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 Salido626553972017-04-25 16:55:26 -07001013 char *driver_override, *old, *cp;
Kim Phillips3d713e02014-06-02 19:42:58 -05001014
Nicolai Stangebf563b02017-09-11 09:45:42 +02001015 /* We need to keep extra room for a newline */
1016 if (count >= (PAGE_SIZE - 1))
Kim Phillips3d713e02014-06-02 19:42:58 -05001017 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 Salido626553972017-04-25 16:55:26 -07001027 device_lock(dev);
1028 old = pdev->driver_override;
Kim Phillips3d713e02014-06-02 19:42:58 -05001029 if (strlen(driver_override)) {
1030 pdev->driver_override = driver_override;
1031 } else {
1032 kfree(driver_override);
1033 pdev->driver_override = NULL;
1034 }
Adrian Salido626553972017-04-25 16:55:26 -07001035 device_unlock(dev);
Kim Phillips3d713e02014-06-02 19:42:58 -05001036
1037 kfree(old);
1038
1039 return count;
1040}
1041
1042static 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 Salido626553972017-04-25 16:55:26 -07001046 ssize_t len;
Kim Phillips3d713e02014-06-02 19:42:58 -05001047
Adrian Salido626553972017-04-25 16:55:26 -07001048 device_lock(dev);
1049 len = sprintf(buf, "%s\n", pdev->driver_override);
1050 device_unlock(dev);
1051 return len;
Kim Phillips3d713e02014-06-02 19:42:58 -05001052}
1053static DEVICE_ATTR_RW(driver_override);
1054
1055
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001056static struct attribute *platform_dev_attrs[] = {
1057 &dev_attr_modalias.attr,
Kim Phillips3d713e02014-06-02 19:42:58 -05001058 &dev_attr_driver_override.attr,
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001059 NULL,
David Brownella0245f72006-05-29 10:37:33 -07001060};
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001061ATTRIBUTE_GROUPS(platform_dev);
David Brownella0245f72006-05-29 10:37:33 -07001062
Kay Sievers7eff2e72007-08-14 15:15:12 +02001063static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -07001064{
1065 struct platform_device *pdev = to_platform_device(dev);
Grant Likelyeca39302010-06-08 07:48:21 -06001066 int rc;
1067
1068 /* Some devices have extra OF data and an OF-style MODALIAS */
Fabio Porcedda0258e182013-03-26 10:35:16 +01001069 rc = of_device_uevent_modalias(dev, env);
Grant Likelyeca39302010-06-08 07:48:21 -06001070 if (rc != -ENODEV)
1071 return rc;
David Brownella0245f72006-05-29 10:37:33 -07001072
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001073 rc = acpi_device_uevent_modalias(dev, env);
1074 if (rc != -ENODEV)
1075 return rc;
1076
Eric Miao57fee4a2009-02-04 11:52:40 +08001077 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001078 pdev->name);
David Brownella0245f72006-05-29 10:37:33 -07001079 return 0;
1080}
1081
Eric Miao57fee4a2009-02-04 11:52:40 +08001082static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +01001083 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +08001084 struct platform_device *pdev)
1085{
1086 while (id->name[0]) {
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001087 if (strcmp(pdev->name, id->name) == 0) {
Eric Miao57fee4a2009-02-04 11:52:40 +08001088 pdev->id_entry = id;
1089 return id;
1090 }
1091 id++;
1092 }
1093 return NULL;
1094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001097 * platform_match - bind platform device to platform driver.
1098 * @dev: device.
1099 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001101 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001108 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001109static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Eric Miao71b3e0c2009-01-31 22:47:44 +08001111 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +08001112 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Kim Phillips3d713e02014-06-02 19:42:58 -05001114 /* 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 Likely05212152010-06-08 07:48:20 -06001118 /* Attempt an OF style match first */
1119 if (of_driver_match_device(dev, drv))
1120 return 1;
1121
Mika Westerberg91e56872012-10-31 22:45:02 +01001122 /* Then try ACPI style match */
1123 if (acpi_driver_match_device(dev, drv))
1124 return 1;
1125
Grant Likely05212152010-06-08 07:48:20 -06001126 /* Then try to match against the id table */
Eric Miao57fee4a2009-02-04 11:52:40 +08001127 if (pdrv->id_table)
1128 return platform_match_id(pdrv->id_table, pdev) != NULL;
1129
1130 /* fall-back to driver name match */
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001131 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001134#ifdef CONFIG_PM_SLEEP
1135
1136static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001138 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1139 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 int ret = 0;
1141
Magnus Damm783ea7d2009-06-04 22:13:33 +02001142 if (dev->driver && pdrv->suspend)
1143 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d82006-09-03 13:16:45 -07001144
1145 return ret;
1146}
1147
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001148static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001150 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1151 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int ret = 0;
1153
Magnus Damm783ea7d2009-06-04 22:13:33 +02001154 if (dev->driver && pdrv->resume)
1155 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -07001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return ret;
1158}
1159
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001160#endif /* CONFIG_PM_SLEEP */
Magnus Damm9d730222009-08-20 20:25:32 +02001161
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001162#ifdef CONFIG_SUSPEND
1163
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001164int platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001165{
1166 struct device_driver *drv = dev->driver;
1167 int ret = 0;
1168
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001169 if (!drv)
1170 return 0;
1171
1172 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001173 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. Wysocki69c9dd12011-04-29 00:36:05 +02001182int platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001183{
1184 struct device_driver *drv = dev->driver;
1185 int ret = 0;
1186
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001187 if (!drv)
1188 return 0;
1189
1190 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001191 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. Wysocki69c9dd12011-04-29 00:36:05 +02001200#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001201
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +02001202#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001203
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001204int platform_pm_freeze(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001205{
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. Wysocki69c9dd12011-04-29 00:36:05 +02001222int platform_pm_thaw(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001223{
1224 struct device_driver *drv = dev->driver;
1225 int ret = 0;
1226
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001227 if (!drv)
1228 return 0;
1229
1230 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001231 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. Wysocki69c9dd12011-04-29 00:36:05 +02001240int platform_pm_poweroff(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001241{
1242 struct device_driver *drv = dev->driver;
1243 int ret = 0;
1244
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001245 if (!drv)
1246 return 0;
1247
1248 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001249 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. Wysocki69c9dd12011-04-29 00:36:05 +02001258int platform_pm_restore(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001259{
1260 struct device_driver *drv = dev->driver;
1261 int ret = 0;
1262
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001263 if (!drv)
1264 return 0;
1265
1266 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001267 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. Wysocki69c9dd12011-04-29 00:36:05 +02001276#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001277
Nipun Gupta07397df2018-04-28 08:21:58 +05301278int platform_dma_configure(struct device *dev)
1279{
1280 enum dev_dma_attr attr;
1281 int ret = 0;
1282
1283 if (dev->of_node) {
Christoph Hellwig3d6ce862018-05-03 16:25:08 +02001284 ret = of_dma_configure(dev, dev->of_node, true);
Nipun Gupta07397df2018-04-28 08:21:58 +05301285 } else if (has_acpi_companion(dev)) {
1286 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
Robin Murphye5361ca2018-12-06 13:20:49 -08001287 ret = acpi_dma_configure(dev, attr);
Nipun Gupta07397df2018-04-28 08:21:58 +05301288 }
1289
1290 return ret;
1291}
1292
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +02001293static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysocki8b313a32011-04-29 00:36:32 +02001294 .runtime_suspend = pm_generic_runtime_suspend,
1295 .runtime_resume = pm_generic_runtime_resume,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001296 USE_PLATFORM_PM_SLEEP_OPS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001297};
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299struct bus_type platform_bus_type = {
1300 .name = "platform",
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001301 .dev_groups = platform_dev_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -07001303 .uevent = platform_uevent,
Nipun Gupta07397df2018-04-28 08:21:58 +05301304 .dma_configure = platform_dma_configure,
Magnus Damm9d730222009-08-20 20:25:32 +02001305 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306};
Dmitry Torokhova96b2042005-12-10 01:36:28 -05001307EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Sami Tolvanen492c8872019-11-12 13:41:56 -08001309static inline int __platform_match(struct device *dev, const void *drv)
1310{
1311 return platform_match(dev, (struct device_driver *)drv);
1312}
1313
Suzuki K Poulose36f33132019-07-23 23:18:38 +01001314/**
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 */
1320struct 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 Tolvanen492c8872019-11-12 13:41:56 -08001324 __platform_match);
Suzuki K Poulose36f33132019-07-23 23:18:38 +01001325}
1326EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328int __init platform_bus_init(void)
1329{
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001330 int error;
1331
1332 error = device_register(&platform_bus);
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301333 if (error) {
1334 put_device(&platform_bus);
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001335 return error;
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301336 }
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001337 error = bus_register(&platform_bus_type);
1338 if (error)
1339 device_unregister(&platform_bus);
Pantelis Antoniou801d7282014-10-28 22:36:01 +02001340 of_platform_register_reconfig_notifier();
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001341 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}