blob: a07e28eab7d7c52e87f48323d3274aa3f17b90e6 [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>
Simon Schwartz39cc5392019-12-10 17:41:37 -050030#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010032#include "base.h"
Rafael J. Wysockibed2b422012-08-06 01:45:11 +020033#include "power/power.h"
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010034
Jean Delvare689ae232012-07-27 22:14:59 +020035/* For automatically allocated device IDs */
36static DEFINE_IDA(platform_devid_ida);
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010039 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050041EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080044 * platform_get_resource - get a resource for a device
45 * @dev: platform device
46 * @type: resource type
47 * @num: resource index
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080049struct resource *platform_get_resource(struct platform_device *dev,
50 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Simon Schwartz39cc5392019-12-10 17:41:37 -050052 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 for (i = 0; i < dev->num_resources; i++) {
55 struct resource *r = &dev->resource[i];
56
Magnus Dammc9f66162008-10-15 22:05:15 -070057 if (type == resource_type(r) && num-- == 0)
58 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60 return NULL;
61}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050062EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Bartosz Golaszewskibb6243b2019-10-22 10:43:14 +020064#ifdef CONFIG_HAS_IOMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/**
Dejin Zheng890cc392020-03-24 00:06:08 +080066 * 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 */
74void __iomem *
75devm_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}
85EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource);
86
87/**
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000088 * 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 Golaszewski7067c962019-04-01 10:16:35 +020092 * resource management
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000093 * @index: resource index
94 */
95void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
96 unsigned int index)
97{
Dejin Zhengfd789012020-03-24 00:06:12 +080098 return devm_platform_get_and_ioremap_resource(pdev, index, NULL);
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000099}
100EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
Bartosz Golaszewskibb6243b2019-10-22 10:43:14 +0200101
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 */
110void __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 Golaszewskic9c86412019-10-22 10:43:16 +0200118
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 */
128void __iomem *
129devm_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}
137EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname);
Bartosz Golaszewski837ccda2019-02-21 17:26:27 +0100138#endif /* CONFIG_HAS_IOMEM */
Bartosz Golaszewski7945f922019-02-20 11:12:39 +0000139
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200140/**
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 Chehabf0825242020-04-14 18:48:45 +0200150 * For example::
151 *
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200152 * 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 */
158int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +0000160#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 Herring9ec36ca2014-04-23 17:57:41 -0500166 struct resource *r;
Andy Shevchenko71564a22019-10-23 15:25:05 +0300167 int ret;
Guenter Roeckaff008a2014-06-17 15:51:02 -0700168
Andy Shevchenko71564a22019-10-23 15:25:05 +0300169 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
Guenter Roeckaff008a2014-06-17 15:51:02 -0700170 ret = of_irq_get(dev->dev.of_node, num);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +0300171 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -0700172 return ret;
173 }
Rob Herring9ec36ca2014-04-23 17:57:41 -0500174
175 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
Agustin Vega-Friasd44fa3d2017-02-02 18:23:58 -0500176 if (has_acpi_companion(&dev->dev)) {
177 if (r && r->flags & IORESOURCE_DISABLED) {
Agustin Vega-Friasd44fa3d2017-02-02 18:23:58 -0500178 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
179 if (ret)
180 return ret;
181 }
182 }
183
Linus Walleij7085a742015-02-18 17:12:18 +0100184 /*
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 Roeck60ca5e02016-09-13 20:32:44 -0700190 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 Torvalds1da177e2005-04-16 15:20:36 -0700198
Enrico Granatadaaef252019-02-11 11:01:12 -0800199 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 Norris46c42d82019-07-29 13:49:54 -0700209 if (num == 0 && has_acpi_companion(&dev->dev)) {
Andy Shevchenko71564a22019-10-23 15:25:05 +0300210 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
Brian Norris46c42d82019-07-29 13:49:54 -0700211 /* Our callers expect -ENXIO for missing IRQs. */
212 if (ret >= 0 || ret == -EPROBE_DEFER)
213 return ret;
214 }
Enrico Granatadaaef252019-02-11 11:01:12 -0800215
216 return -ENXIO;
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +0000217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200219EXPORT_SYMBOL_GPL(platform_get_irq_optional);
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700220
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 Chehabf0825242020-04-14 18:48:45 +0200230 * For example::
231 *
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700232 * 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 */
238int platform_get_irq(struct platform_device *dev, unsigned int num)
239{
240 int ret;
241
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200242 ret = platform_get_irq_optional(dev, num);
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700243 if (ret < 0 && ret != -EPROBE_DEFER)
244 dev_err(&dev->dev, "IRQ index %u not found\n", num);
245
246 return ret;
247}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500248EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/**
Stephen Boyd4b835552016-01-06 17:12:47 -0800251 * 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 */
256int platform_irq_count(struct platform_device *dev)
257{
258 int ret, nr = 0;
259
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200260 while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
Stephen Boyd4b835552016-01-06 17:12:47 -0800261 nr++;
262
263 if (ret == -EPROBE_DEFER)
264 return ret;
265
266 return nr;
267}
268EXPORT_SYMBOL_GPL(platform_irq_count);
269
270/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800271 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700275 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800276struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200277 unsigned int type,
278 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Simon Schwartz39cc5392019-12-10 17:41:37 -0500280 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 for (i = 0; i < dev->num_resources; i++) {
283 struct resource *r = &dev->resource[i];
284
Peter Ujfalusi1b8cb922012-08-23 17:10:00 +0300285 if (unlikely(!r->name))
286 continue;
287
Magnus Dammc9f66162008-10-15 22:05:15 -0700288 if (type == resource_type(r) && !strcmp(r->name, name))
289 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291 return NULL;
292}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500293EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Hans de Goedef1da5672019-10-05 23:04:47 +0200295static int __platform_get_irq_byname(struct platform_device *dev,
296 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Grygorii Strashkoad696742014-05-20 13:42:02 +0300298 struct resource *r;
Andy Shevchenko71564a22019-10-23 15:25:05 +0300299 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Guenter Roeckaff008a2014-06-17 15:51:02 -0700301 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
Guenter Roeckaff008a2014-06-17 15:51:02 -0700302 ret = of_irq_get_byname(dev->dev.of_node, name);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +0300303 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -0700304 return ret;
305 }
Grygorii Strashkoad696742014-05-20 13:42:02 +0300306
307 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700308 if (r)
309 return r->start;
310
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700311 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
Hans de Goedef1da5672019-10-05 23:04:47 +0200313
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 */
323int 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 Torokhova96b2042005-12-10 01:36:28 -0500333EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335/**
Hans de Goedef1da5672019-10-05 23:04:47 +0200336 * 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 */
345int platform_get_irq_byname_optional(struct platform_device *dev,
346 const char *name)
347{
348 return __platform_get_irq_byname(dev, name);
349}
350EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
351
352/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800353 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700356 */
357int 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 Torokhova96b2042005-12-10 01:36:28 -0500372EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Russell King37c12e72005-11-05 21:19:33 +0000374struct platform_object {
375 struct platform_device pdev;
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200376 char name[];
Russell King37c12e72005-11-05 21:19:33 +0000377};
378
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200379/*
380 * Set up default DMA mask for platform devices if the they weren't
381 * previously set by the architecture / DT.
382 */
383static 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 Hellwige3a36eb2020-03-11 17:07:10 +0100387 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 Hellwigcdfee562019-08-16 08:24:35 +0200391};
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000394 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800395 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000396 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800397 * 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 King37c12e72005-11-05 21:19:33 +0000399 */
400void platform_device_put(struct platform_device *pdev)
401{
Andy Shevchenko99fef582018-12-03 20:21:41 +0200402 if (!IS_ERR_OR_NULL(pdev))
Russell King37c12e72005-11-05 21:19:33 +0000403 put_device(&pdev->dev);
404}
405EXPORT_SYMBOL_GPL(platform_device_put);
406
407static void platform_device_release(struct device *dev)
408{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800409 struct platform_object *pa = container_of(dev, struct platform_object,
410 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000411
Grant Likely7096d042010-10-20 11:45:13 -0600412 of_device_node_put(&pa->pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000413 kfree(pa->pdev.dev.platform_data);
Samuel Ortize710d7d2011-04-08 00:43:01 +0200414 kfree(pa->pdev.mfd_cell);
Russell King37c12e72005-11-05 21:19:33 +0000415 kfree(pa->pdev.resource);
Kim Phillips3d713e02014-06-02 19:42:58 -0500416 kfree(pa->pdev.driver_override);
Russell King37c12e72005-11-05 21:19:33 +0000417 kfree(pa);
418}
419
420/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000421 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800422 * @name: base name of the device we're adding
423 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000424 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800425 * 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 King37c12e72005-11-05 21:19:33 +0000427 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200428struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000429{
430 struct platform_object *pa;
431
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200432 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000433 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 Hellwigcdfee562019-08-16 08:24:35 +0200439 setup_pdev_dma_masks(&pa->pdev);
Russell King37c12e72005-11-05 21:19:33 +0000440 }
441
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500442 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000443}
444EXPORT_SYMBOL_GPL(platform_device_alloc);
445
446/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000447 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800448 * @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 King37c12e72005-11-05 21:19:33 +0000451 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800452 * 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 King37c12e72005-11-05 21:19:33 +0000455 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800456int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100457 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000458{
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200459 struct resource *r = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000460
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200461 if (res) {
462 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
463 if (!r)
464 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000465 }
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200466
Uwe Kleine-König4a03d6f2011-04-20 09:44:45 +0200467 kfree(pdev->resource);
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200468 pdev->resource = r;
469 pdev->num_resources = num;
470 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000471}
472EXPORT_SYMBOL_GPL(platform_device_add_resources);
473
474/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000475 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800476 * @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 King37c12e72005-11-05 21:19:33 +0000479 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800480 * 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 King37c12e72005-11-05 21:19:33 +0000483 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800484int platform_device_add_data(struct platform_device *pdev, const void *data,
485 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000486{
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200487 void *d = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000488
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200489 if (data) {
490 d = kmemdup(data, size, GFP_KERNEL);
491 if (!d)
492 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000493 }
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200494
Uwe Kleine-König251e0312011-04-20 09:44:43 +0200495 kfree(pdev->dev.platform_data);
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200496 pdev->dev.platform_data = d;
497 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000498}
499EXPORT_SYMBOL_GPL(platform_device_add_data);
500
501/**
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200502 * platform_device_add_properties - add built-in properties to a platform device
503 * @pdev: platform device to add properties to
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300504 * @properties: null terminated array of properties to add
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200505 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300506 * 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 Westerberg00bbc1d2015-11-30 17:11:38 +0200509 */
510int platform_device_add_properties(struct platform_device *pdev,
Jan Kiszka277036f2017-06-02 07:43:27 +0200511 const struct property_entry *properties)
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200512{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300513 return device_add_properties(&pdev->dev, properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200514}
515EXPORT_SYMBOL_GPL(platform_device_add_properties);
516
517/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800518 * platform_device_add - add a platform device to device hierarchy
519 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800521 * This is part 2 of platform_device_register(), though may be called
522 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
Russell King37c12e72005-11-05 21:19:33 +0000524int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Simon Schwartz39cc5392019-12-10 17:41:37 -0500526 u32 i;
527 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
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 Delvare689ae232012-07-27 22:14:59 +0200537 switch (pdev->id) {
538 default:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100539 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Jean Delvare689ae232012-07-27 22:14:59 +0200540 break;
541 case PLATFORM_DEVID_NONE:
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700542 dev_set_name(&pdev->dev, "%s", pdev->name);
Jean Delvare689ae232012-07-27 22:14:59 +0200543 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-Hartman5da7f702015-06-10 08:38:02 -0700552 goto err_out;
Jean Delvare689ae232012-07-27 22:14:59 +0200553 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 Torvalds1da177e2005-04-16 15:20:36 -0700558
559 for (i = 0; i < pdev->num_resources; i++) {
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700560 struct resource *p, *r = &pdev->resource[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100563 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 p = r->parent;
566 if (!p) {
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700567 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 p = &iomem_resource;
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700569 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 p = &ioport_resource;
571 }
572
Andy Shevchenko25ebcb72019-04-04 11:11:58 +0300573 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-Hartman5da7f702015-06-10 08:38:02 -0700579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
582 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100583 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Russell Kinge3915532006-05-06 08:15:26 +0100585 ret = device_add(&pdev->dev);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700586 if (ret == 0)
587 return ret;
588
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700589 failed:
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700590 if (pdev->id_auto) {
591 ida_simple_remove(&platform_devid_ida, pdev->id);
592 pdev->id = PLATFORM_DEVID_AUTO;
593 }
594
Colin Ian King0707cfa2020-01-16 17:57:58 +0000595 while (i--) {
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700596 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100597 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700598 release_resource(r);
599 }
Magnus Dammc9f66162008-10-15 22:05:15 -0700600
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700601 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return ret;
603}
Russell King37c12e72005-11-05 21:19:33 +0000604EXPORT_SYMBOL_GPL(platform_device_add);
605
606/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800607 * platform_device_del - remove a platform-level device
608 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500609 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800610 * 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 Torokhov93ce3062005-12-10 01:36:27 -0500613 */
614void platform_device_del(struct platform_device *pdev)
615{
Simon Schwartz39cc5392019-12-10 17:41:37 -0500616 u32 i;
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500617
Andy Shevchenko99fef582018-12-03 20:21:41 +0200618 if (!IS_ERR_OR_NULL(pdev)) {
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700619 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 Likely7f5dcaf2015-06-07 15:20:11 +0100628 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700629 release_resource(r);
630 }
631 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500632}
633EXPORT_SYMBOL_GPL(platform_device_del);
634
635/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800636 * platform_device_register - add a platform-level device
637 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000638 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800639int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000640{
641 device_initialize(&pdev->dev);
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200642 setup_pdev_dma_masks(pdev);
Russell King37c12e72005-11-05 21:19:33 +0000643 return platform_device_add(pdev);
644}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500645EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800648 * platform_device_unregister - unregister a platform-level device
649 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800651 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700654 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800655void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500657 platform_device_del(pdev);
658 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500660EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/**
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200663 * platform_device_register_full - add a platform-level device with
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200664 * resources and platform-specific data
665 *
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200666 * @pdevinfo: data used to create device
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700667 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200668 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700669 */
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200670struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +0100671 const struct platform_device_info *pdevinfo)
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700672{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200673 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700674 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700675
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200676 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200677 if (!pdev)
Johannes Berg36cf3b12019-03-01 13:24:47 +0100678 return ERR_PTR(-ENOMEM);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700679
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200680 pdev->dev.parent = pdevinfo->parent;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100681 pdev->dev.fwnode = pdevinfo->fwnode;
Mans Rullgard2c1ea6a2019-02-21 11:29:35 +0000682 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
683 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700684
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200685 if (pdevinfo->dma_mask) {
Christoph Hellwige3a36eb2020-03-11 17:07:10 +0100686 pdev->platform_dma_mask = pdevinfo->dma_mask;
687 pdev->dev.dma_mask = &pdev->platform_dma_mask;
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200688 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
689 }
690
691 ret = platform_device_add_resources(pdev,
692 pdevinfo->res, pdevinfo->num_res);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400693 if (ret)
694 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700695
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200696 ret = platform_device_add_data(pdev,
697 pdevinfo->data, pdevinfo->size_data);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400698 if (ret)
699 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200700
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300701 if (pdevinfo->properties) {
702 ret = platform_device_add_properties(pdev,
703 pdevinfo->properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200704 if (ret)
705 goto err;
706 }
707
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200708 ret = platform_device_add(pdev);
709 if (ret) {
710err:
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100711 ACPI_COMPANION_SET(&pdev->dev, NULL);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200712 platform_device_put(pdev);
713 return ERR_PTR(ret);
714 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700715
716 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700717}
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200718EXPORT_SYMBOL_GPL(platform_device_register_full);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700719
Russell King00d3dcd2005-11-09 17:23:39 +0000720static 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. Wysocki94d76d52012-11-26 10:04:53 +0100724 int ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000725
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200726 ret = of_clk_set_defaults(_dev->of_node, false);
727 if (ret < 0)
728 return ret;
729
Ulf Hanssoncb518412014-09-19 20:27:38 +0200730 ret = dev_pm_domain_attach(_dev, true);
Ulf Hansson88a97692018-04-26 10:53:06 +0200731 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 Hanssoncb518412014-09-19 20:27:38 +0200738 }
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100739
Ulf Hansson88a97692018-04-26 10:53:06 +0200740out:
Johan Hovold3f9120b2013-09-23 16:27:26 +0200741 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
742 dev_warn(_dev, "probe deferral not supported\n");
743 ret = -ENXIO;
744 }
745
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100746 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000747}
748
David Brownellc67334f2006-11-16 23:28:47 -0800749static int platform_drv_probe_fail(struct device *_dev)
750{
751 return -ENXIO;
752}
753
Russell King00d3dcd2005-11-09 17:23:39 +0000754static 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önigb8b2c7d2015-08-07 07:19:22 +0200758 int ret = 0;
Russell King00d3dcd2005-11-09 17:23:39 +0000759
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200760 if (drv->remove)
761 ret = drv->remove(dev);
Ulf Hanssoncb518412014-09-19 20:27:38 +0200762 dev_pm_domain_detach(_dev, true);
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100763
764 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000765}
766
767static 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önigb8b2c7d2015-08-07 07:19:22 +0200772 if (drv->shutdown)
773 drv->shutdown(dev);
Russell King00d3dcd2005-11-09 17:23:39 +0000774}
775
Russell King00d3dcd2005-11-09 17:23:39 +0000776/**
Libo Chen94470572013-05-25 12:40:50 +0800777 * __platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800778 * @drv: platform driver structure
Randy Dunlap08801f92013-07-14 17:43:06 -0700779 * @owner: owning module/driver
Russell King00d3dcd2005-11-09 17:23:39 +0000780 */
Libo Chen94470572013-05-25 12:40:50 +0800781int __platform_driver_register(struct platform_driver *drv,
782 struct module *owner)
Russell King00d3dcd2005-11-09 17:23:39 +0000783{
Libo Chen94470572013-05-25 12:40:50 +0800784 drv->driver.owner = owner;
Russell King00d3dcd2005-11-09 17:23:39 +0000785 drv->driver.bus = &platform_bus_type;
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200786 drv->driver.probe = platform_drv_probe;
787 drv->driver.remove = platform_drv_remove;
788 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200789
Russell King00d3dcd2005-11-09 17:23:39 +0000790 return driver_register(&drv->driver);
791}
Libo Chen94470572013-05-25 12:40:50 +0800792EXPORT_SYMBOL_GPL(__platform_driver_register);
Russell King00d3dcd2005-11-09 17:23:39 +0000793
794/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000795 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800796 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000797 */
798void platform_driver_unregister(struct platform_driver *drv)
799{
800 driver_unregister(&drv->driver);
801}
802EXPORT_SYMBOL_GPL(platform_driver_unregister);
803
David Brownellc67334f2006-11-16 23:28:47 -0800804/**
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100805 * __platform_driver_probe - register driver for non-hotpluggable device
David Brownellc67334f2006-11-16 23:28:47 -0800806 * @drv: platform driver structure
Johan Hovold3f9120b2013-09-23 16:27:26 +0200807 * @probe: the driver probe routine, probably from an __init section
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100808 * @module: module which will be the owner of the driver
David Brownellc67334f2006-11-16 23:28:47 -0800809 *
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 Hovold3f9120b2013-09-23 16:27:26 +0200819 * Note that this is incompatible with deferred probing.
Fabio Porcedda647c86d2013-03-26 10:35:15 +0100820 *
David Brownellc67334f2006-11-16 23:28:47 -0800821 * 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 Sangc3b50dc2014-10-28 17:40:41 +0100824int __init_or_module __platform_driver_probe(struct platform_driver *drv,
825 int (*probe)(struct platform_device *), struct module *module)
David Brownellc67334f2006-11-16 23:28:47 -0800826{
827 int retval, code;
828
Dmitry Torokhov5c36eb22015-03-30 16:20:07 -0700829 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 Hovold3f9120b2013-09-23 16:27:26 +0200842 /*
843 * Prevent driver from requesting probe deferral to avoid further
844 * futile probe attempts.
845 */
846 drv->prevent_deferred_probe = true;
847
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700848 /* make sure driver won't have bind/unbind attributes */
849 drv->driver.suppress_bind_attrs = true;
850
David Brownellc67334f2006-11-16 23:28:47 -0800851 /* temporary section violation during probe() */
852 drv->probe = probe;
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100853 retval = code = __platform_driver_register(drv, module);
David Brownellc67334f2006-11-16 23:28:47 -0800854
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700855 /*
856 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800857 * 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 Pannutod79d3242010-08-06 17:12:41 -0700861 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800862 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800863 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800864 retval = -ENODEV;
865 drv->driver.probe = platform_drv_probe_fail;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700866 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800867
868 if (code != retval)
869 platform_driver_unregister(drv);
870 return retval;
871}
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100872EXPORT_SYMBOL_GPL(__platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800874/**
Wolfram Sang291f6532014-10-28 17:40:42 +0100875 * __platform_create_bundle - register driver and create corresponding device
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800876 * @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 Sang291f6532014-10-28 17:40:42 +0100882 * @module: module which will be the owner of the driver
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800883 *
884 * Use this in legacy-style modules that probe hardware directly and
885 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200886 *
887 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800888 */
Wolfram Sang291f6532014-10-28 17:40:42 +0100889struct platform_device * __init_or_module __platform_create_bundle(
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800890 struct platform_driver *driver,
891 int (*probe)(struct platform_device *),
892 struct resource *res, unsigned int n_res,
Wolfram Sang291f6532014-10-28 17:40:42 +0100893 const void *data, size_t size, struct module *module)
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800894{
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 Vorontsov807508c2010-09-07 17:31:54 +0400904 error = platform_device_add_resources(pdev, res, n_res);
905 if (error)
906 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800907
Anton Vorontsov807508c2010-09-07 17:31:54 +0400908 error = platform_device_add_data(pdev, data, size);
909 if (error)
910 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800911
912 error = platform_device_add(pdev);
913 if (error)
914 goto err_pdev_put;
915
Wolfram Sang291f6532014-10-28 17:40:42 +0100916 error = __platform_driver_probe(driver, probe, module);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800917 if (error)
918 goto err_pdev_del;
919
920 return pdev;
921
922err_pdev_del:
923 platform_device_del(pdev);
924err_pdev_put:
925 platform_device_put(pdev);
926err_out:
927 return ERR_PTR(error);
928}
Wolfram Sang291f6532014-10-28 17:40:42 +0100929EXPORT_SYMBOL_GPL(__platform_create_bundle);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800930
Thierry Redingdbe22562015-09-25 17:29:04 +0200931/**
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 */
944int __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
963error:
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}
971EXPORT_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 */
982void 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}
990EXPORT_SYMBOL_GPL(platform_unregister_drivers);
991
David Brownella0245f72006-05-29 10:37:33 -0700992/* 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-Hartman4a3ad202008-01-24 22:50:12 -0800998static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
999 char *buf)
David Brownella0245f72006-05-29 10:37:33 -07001000{
1001 struct platform_device *pdev = to_platform_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001002 int len;
1003
Rob Herring0634c292017-03-22 09:16:27 -05001004 len = of_device_modalias(dev, buf, PAGE_SIZE);
Zhang Ruib9f73062014-01-14 16:46:38 +08001005 if (len != -ENODEV)
1006 return len;
1007
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001008 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
1009 if (len != -ENODEV)
1010 return len;
1011
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001012 len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -07001013
1014 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
1015}
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001016static DEVICE_ATTR_RO(modalias);
David Brownella0245f72006-05-29 10:37:33 -07001017
Kim Phillips3d713e02014-06-02 19:42:58 -05001018static 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 Salido626553972017-04-25 16:55:26 -07001023 char *driver_override, *old, *cp;
Kim Phillips3d713e02014-06-02 19:42:58 -05001024
Nicolai Stangebf563b02017-09-11 09:45:42 +02001025 /* We need to keep extra room for a newline */
1026 if (count >= (PAGE_SIZE - 1))
Kim Phillips3d713e02014-06-02 19:42:58 -05001027 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 Salido626553972017-04-25 16:55:26 -07001037 device_lock(dev);
1038 old = pdev->driver_override;
Kim Phillips3d713e02014-06-02 19:42:58 -05001039 if (strlen(driver_override)) {
1040 pdev->driver_override = driver_override;
1041 } else {
1042 kfree(driver_override);
1043 pdev->driver_override = NULL;
1044 }
Adrian Salido626553972017-04-25 16:55:26 -07001045 device_unlock(dev);
Kim Phillips3d713e02014-06-02 19:42:58 -05001046
1047 kfree(old);
1048
1049 return count;
1050}
1051
1052static 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 Salido626553972017-04-25 16:55:26 -07001056 ssize_t len;
Kim Phillips3d713e02014-06-02 19:42:58 -05001057
Adrian Salido626553972017-04-25 16:55:26 -07001058 device_lock(dev);
1059 len = sprintf(buf, "%s\n", pdev->driver_override);
1060 device_unlock(dev);
1061 return len;
Kim Phillips3d713e02014-06-02 19:42:58 -05001062}
1063static DEVICE_ATTR_RW(driver_override);
1064
1065
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001066static struct attribute *platform_dev_attrs[] = {
1067 &dev_attr_modalias.attr,
Kim Phillips3d713e02014-06-02 19:42:58 -05001068 &dev_attr_driver_override.attr,
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001069 NULL,
David Brownella0245f72006-05-29 10:37:33 -07001070};
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001071ATTRIBUTE_GROUPS(platform_dev);
David Brownella0245f72006-05-29 10:37:33 -07001072
Kay Sievers7eff2e72007-08-14 15:15:12 +02001073static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -07001074{
1075 struct platform_device *pdev = to_platform_device(dev);
Grant Likelyeca39302010-06-08 07:48:21 -06001076 int rc;
1077
1078 /* Some devices have extra OF data and an OF-style MODALIAS */
Fabio Porcedda0258e182013-03-26 10:35:16 +01001079 rc = of_device_uevent_modalias(dev, env);
Grant Likelyeca39302010-06-08 07:48:21 -06001080 if (rc != -ENODEV)
1081 return rc;
David Brownella0245f72006-05-29 10:37:33 -07001082
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001083 rc = acpi_device_uevent_modalias(dev, env);
1084 if (rc != -ENODEV)
1085 return rc;
1086
Eric Miao57fee4a2009-02-04 11:52:40 +08001087 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001088 pdev->name);
David Brownella0245f72006-05-29 10:37:33 -07001089 return 0;
1090}
1091
Eric Miao57fee4a2009-02-04 11:52:40 +08001092static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +01001093 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +08001094 struct platform_device *pdev)
1095{
1096 while (id->name[0]) {
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001097 if (strcmp(pdev->name, id->name) == 0) {
Eric Miao57fee4a2009-02-04 11:52:40 +08001098 pdev->id_entry = id;
1099 return id;
1100 }
1101 id++;
1102 }
1103 return NULL;
1104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001107 * platform_match - bind platform device to platform driver.
1108 * @dev: device.
1109 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001111 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001119static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Eric Miao71b3e0c2009-01-31 22:47:44 +08001121 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +08001122 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Kim Phillips3d713e02014-06-02 19:42:58 -05001124 /* 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 Likely05212152010-06-08 07:48:20 -06001128 /* Attempt an OF style match first */
1129 if (of_driver_match_device(dev, drv))
1130 return 1;
1131
Mika Westerberg91e56872012-10-31 22:45:02 +01001132 /* Then try ACPI style match */
1133 if (acpi_driver_match_device(dev, drv))
1134 return 1;
1135
Grant Likely05212152010-06-08 07:48:20 -06001136 /* Then try to match against the id table */
Eric Miao57fee4a2009-02-04 11:52:40 +08001137 if (pdrv->id_table)
1138 return platform_match_id(pdrv->id_table, pdev) != NULL;
1139
1140 /* fall-back to driver name match */
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001141 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001144#ifdef CONFIG_PM_SLEEP
1145
1146static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001148 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1149 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 int ret = 0;
1151
Magnus Damm783ea7d2009-06-04 22:13:33 +02001152 if (dev->driver && pdrv->suspend)
1153 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d82006-09-03 13:16:45 -07001154
1155 return ret;
1156}
1157
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001158static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001160 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1161 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 int ret = 0;
1163
Magnus Damm783ea7d2009-06-04 22:13:33 +02001164 if (dev->driver && pdrv->resume)
1165 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -07001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return ret;
1168}
1169
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001170#endif /* CONFIG_PM_SLEEP */
Magnus Damm9d730222009-08-20 20:25:32 +02001171
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001172#ifdef CONFIG_SUSPEND
1173
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001174int platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001175{
1176 struct device_driver *drv = dev->driver;
1177 int ret = 0;
1178
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001179 if (!drv)
1180 return 0;
1181
1182 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001183 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. Wysocki69c9dd12011-04-29 00:36:05 +02001192int platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001193{
1194 struct device_driver *drv = dev->driver;
1195 int ret = 0;
1196
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001197 if (!drv)
1198 return 0;
1199
1200 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001201 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. Wysocki69c9dd12011-04-29 00:36:05 +02001210#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001211
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +02001212#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001213
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001214int platform_pm_freeze(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001215{
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. Wysocki69c9dd12011-04-29 00:36:05 +02001232int platform_pm_thaw(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001233{
1234 struct device_driver *drv = dev->driver;
1235 int ret = 0;
1236
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001237 if (!drv)
1238 return 0;
1239
1240 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001241 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. Wysocki69c9dd12011-04-29 00:36:05 +02001250int platform_pm_poweroff(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001251{
1252 struct device_driver *drv = dev->driver;
1253 int ret = 0;
1254
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001255 if (!drv)
1256 return 0;
1257
1258 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001259 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. Wysocki69c9dd12011-04-29 00:36:05 +02001268int platform_pm_restore(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001269{
1270 struct device_driver *drv = dev->driver;
1271 int ret = 0;
1272
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001273 if (!drv)
1274 return 0;
1275
1276 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001277 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. Wysocki69c9dd12011-04-29 00:36:05 +02001286#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001287
Nipun Gupta07397df2018-04-28 08:21:58 +05301288int platform_dma_configure(struct device *dev)
1289{
1290 enum dev_dma_attr attr;
1291 int ret = 0;
1292
1293 if (dev->of_node) {
Christoph Hellwig3d6ce862018-05-03 16:25:08 +02001294 ret = of_dma_configure(dev, dev->of_node, true);
Nipun Gupta07397df2018-04-28 08:21:58 +05301295 } else if (has_acpi_companion(dev)) {
1296 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
Robin Murphye5361ca2018-12-06 13:20:49 -08001297 ret = acpi_dma_configure(dev, attr);
Nipun Gupta07397df2018-04-28 08:21:58 +05301298 }
1299
1300 return ret;
1301}
1302
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +02001303static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysocki8b313a32011-04-29 00:36:32 +02001304 .runtime_suspend = pm_generic_runtime_suspend,
1305 .runtime_resume = pm_generic_runtime_resume,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001306 USE_PLATFORM_PM_SLEEP_OPS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001307};
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309struct bus_type platform_bus_type = {
1310 .name = "platform",
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001311 .dev_groups = platform_dev_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -07001313 .uevent = platform_uevent,
Nipun Gupta07397df2018-04-28 08:21:58 +05301314 .dma_configure = platform_dma_configure,
Magnus Damm9d730222009-08-20 20:25:32 +02001315 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316};
Dmitry Torokhova96b2042005-12-10 01:36:28 -05001317EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Sami Tolvanen492c8872019-11-12 13:41:56 -08001319static inline int __platform_match(struct device *dev, const void *drv)
1320{
1321 return platform_match(dev, (struct device_driver *)drv);
1322}
1323
Suzuki K Poulose36f33132019-07-23 23:18:38 +01001324/**
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 */
1330struct 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 Tolvanen492c8872019-11-12 13:41:56 -08001334 __platform_match);
Suzuki K Poulose36f33132019-07-23 23:18:38 +01001335}
1336EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1337
Guenter Roeckeecd37e2019-12-03 12:58:52 -08001338void __weak __init early_platform_cleanup(void) { }
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340int __init platform_bus_init(void)
1341{
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001342 int error;
1343
Guenter Roeckeecd37e2019-12-03 12:58:52 -08001344 early_platform_cleanup();
1345
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001346 error = device_register(&platform_bus);
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301347 if (error) {
1348 put_device(&platform_bus);
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001349 return error;
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301350 }
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001351 error = bus_register(&platform_bus_type);
1352 if (error)
1353 device_unregister(&platform_bus);
Pantelis Antoniou801d7282014-10-28 22:36:01 +02001354 of_platform_register_reconfig_notifier();
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001355 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}