blob: 95fd1549f87de38dcbad0846e923b06ee25bdbd2 [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>
John Garrye15f2fa2020-12-02 18:36:56 +080018#include <linux/interrupt.h>
19#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/dma-mapping.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070021#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <linux/slab.h>
Magnus Damm9d730222009-08-20 20:25:32 +020024#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020025#include <linux/pm_domain.h>
Jean Delvare689ae232012-07-27 22:14:59 +020026#include <linux/idr.h>
Mika Westerberg91e56872012-10-31 22:45:02 +010027#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020028#include <linux/clk/clk-conf.h>
Kim Phillips3d713e02014-06-02 19:42:58 -050029#include <linux/limits.h>
Mika Westerberg00bbc1d2015-11-30 17:11:38 +020030#include <linux/property.h>
Qian Cai967d3012019-01-03 15:29:05 -080031#include <linux/kmemleak.h>
Simon Schwartz39cc5392019-12-10 17:41:37 -050032#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010034#include "base.h"
Rafael J. Wysockibed2b422012-08-06 01:45:11 +020035#include "power/power.h"
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010036
Jean Delvare689ae232012-07-27 22:14:59 +020037/* For automatically allocated device IDs */
38static DEFINE_IDA(platform_devid_ida);
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010041 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050043EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080046 * platform_get_resource - get a resource for a device
47 * @dev: platform device
48 * @type: resource type
49 * @num: resource index
Stephen Boyd0c7a6b92020-09-09 23:04:40 -070050 *
51 * Return: a pointer to the resource or NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080053struct resource *platform_get_resource(struct platform_device *dev,
54 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Simon Schwartz39cc5392019-12-10 17:41:37 -050056 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 for (i = 0; i < dev->num_resources; i++) {
59 struct resource *r = &dev->resource[i];
60
Magnus Dammc9f66162008-10-15 22:05:15 -070061 if (type == resource_type(r) && num-- == 0)
62 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64 return NULL;
65}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050066EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Andy Shevchenko0aec2da2020-12-09 22:36:38 +020068struct resource *platform_get_mem_or_io(struct platform_device *dev,
69 unsigned int num)
70{
71 u32 i;
72
73 for (i = 0; i < dev->num_resources; i++) {
74 struct resource *r = &dev->resource[i];
75
76 if ((resource_type(r) & (IORESOURCE_MEM|IORESOURCE_IO)) && num-- == 0)
77 return r;
78 }
79 return NULL;
80}
81EXPORT_SYMBOL_GPL(platform_get_mem_or_io);
82
Bartosz Golaszewskibb6243b2019-10-22 10:43:14 +020083#ifdef CONFIG_HAS_IOMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/**
Dejin Zheng890cc392020-03-24 00:06:08 +080085 * devm_platform_get_and_ioremap_resource - call devm_ioremap_resource() for a
86 * platform device and get resource
87 *
88 * @pdev: platform device to use both for memory resource lookup as well as
89 * resource management
90 * @index: resource index
91 * @res: optional output parameter to store a pointer to the obtained resource.
Stephen Boyd0c7a6b92020-09-09 23:04:40 -070092 *
93 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
94 * on failure.
Dejin Zheng890cc392020-03-24 00:06:08 +080095 */
96void __iomem *
97devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
98 unsigned int index, struct resource **res)
99{
100 struct resource *r;
101
102 r = platform_get_resource(pdev, IORESOURCE_MEM, index);
103 if (res)
104 *res = r;
105 return devm_ioremap_resource(&pdev->dev, r);
106}
107EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource);
108
109/**
Bartosz Golaszewski7945f922019-02-20 11:12:39 +0000110 * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
111 * device
112 *
113 * @pdev: platform device to use both for memory resource lookup as well as
Bartosz Golaszewski7067c962019-04-01 10:16:35 +0200114 * resource management
Bartosz Golaszewski7945f922019-02-20 11:12:39 +0000115 * @index: resource index
Stephen Boyd0c7a6b92020-09-09 23:04:40 -0700116 *
117 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
118 * on failure.
Bartosz Golaszewski7945f922019-02-20 11:12:39 +0000119 */
120void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
121 unsigned int index)
122{
Dejin Zhengfd789012020-03-24 00:06:12 +0800123 return devm_platform_get_and_ioremap_resource(pdev, index, NULL);
Bartosz Golaszewski7945f922019-02-20 11:12:39 +0000124}
125EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
Bartosz Golaszewskibb6243b2019-10-22 10:43:14 +0200126
127/**
128 * devm_platform_ioremap_resource_wc - write-combined variant of
129 * devm_platform_ioremap_resource()
130 *
131 * @pdev: platform device to use both for memory resource lookup as well as
132 * resource management
133 * @index: resource index
Stephen Boyd0c7a6b92020-09-09 23:04:40 -0700134 *
135 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
136 * on failure.
Bartosz Golaszewskibb6243b2019-10-22 10:43:14 +0200137 */
138void __iomem *devm_platform_ioremap_resource_wc(struct platform_device *pdev,
139 unsigned int index)
140{
141 struct resource *res;
142
143 res = platform_get_resource(pdev, IORESOURCE_MEM, index);
144 return devm_ioremap_resource_wc(&pdev->dev, res);
145}
Bartosz Golaszewskic9c86412019-10-22 10:43:16 +0200146
147/**
148 * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for
149 * a platform device, retrieve the
150 * resource by name
151 *
152 * @pdev: platform device to use both for memory resource lookup as well as
153 * resource management
154 * @name: name of the resource
Stephen Boyd0c7a6b92020-09-09 23:04:40 -0700155 *
156 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
157 * on failure.
Bartosz Golaszewskic9c86412019-10-22 10:43:16 +0200158 */
159void __iomem *
160devm_platform_ioremap_resource_byname(struct platform_device *pdev,
161 const char *name)
162{
163 struct resource *res;
164
165 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
166 return devm_ioremap_resource(&pdev->dev, res);
167}
168EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname);
Bartosz Golaszewski837ccda2019-02-21 17:26:27 +0100169#endif /* CONFIG_HAS_IOMEM */
Bartosz Golaszewski7945f922019-02-20 11:12:39 +0000170
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200171/**
172 * platform_get_irq_optional - get an optional IRQ for a device
173 * @dev: platform device
174 * @num: IRQ number index
175 *
176 * Gets an IRQ for a platform device. Device drivers should check the return
177 * value for errors so as to not pass a negative integer value to the
178 * request_irq() APIs. This is the same as platform_get_irq(), except that it
179 * does not print an error message if an IRQ can not be obtained.
180 *
Mauro Carvalho Chehabf0825242020-04-14 18:48:45 +0200181 * For example::
182 *
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200183 * int irq = platform_get_irq_optional(pdev, 0);
184 * if (irq < 0)
185 * return irq;
186 *
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500187 * Return: non-zero IRQ number on success, negative error number on failure.
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200188 */
189int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500191 int ret;
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +0000192#ifdef CONFIG_SPARC
193 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
194 if (!dev || num >= dev->archdata.num_irqs)
195 return -ENXIO;
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500196 ret = dev->archdata.irqs[num];
197 goto out;
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +0000198#else
Rob Herring9ec36ca2014-04-23 17:57:41 -0500199 struct resource *r;
Guenter Roeckaff008a2014-06-17 15:51:02 -0700200
Andy Shevchenko71564a22019-10-23 15:25:05 +0300201 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
Guenter Roeckaff008a2014-06-17 15:51:02 -0700202 ret = of_irq_get(dev->dev.of_node, num);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +0300203 if (ret > 0 || ret == -EPROBE_DEFER)
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500204 goto out;
Guenter Roeckaff008a2014-06-17 15:51:02 -0700205 }
Rob Herring9ec36ca2014-04-23 17:57:41 -0500206
207 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
Agustin Vega-Friasd44fa3d2017-02-02 18:23:58 -0500208 if (has_acpi_companion(&dev->dev)) {
209 if (r && r->flags & IORESOURCE_DISABLED) {
Agustin Vega-Friasd44fa3d2017-02-02 18:23:58 -0500210 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
211 if (ret)
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500212 goto out;
Agustin Vega-Friasd44fa3d2017-02-02 18:23:58 -0500213 }
214 }
215
Linus Walleij7085a742015-02-18 17:12:18 +0100216 /*
217 * The resources may pass trigger flags to the irqs that need
218 * to be set up. It so happens that the trigger flags for
219 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
220 * settings.
221 */
Guenter Roeck60ca5e02016-09-13 20:32:44 -0700222 if (r && r->flags & IORESOURCE_BITS) {
223 struct irq_data *irqd;
224
225 irqd = irq_get_irq_data(r->start);
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500226 if (!irqd) {
227 ret = -ENXIO;
228 goto out;
229 }
Guenter Roeck60ca5e02016-09-13 20:32:44 -0700230 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500233 if (r) {
234 ret = r->start;
235 goto out;
236 }
Enrico Granatadaaef252019-02-11 11:01:12 -0800237
238 /*
239 * For the index 0 interrupt, allow falling back to GpioInt
240 * resources. While a device could have both Interrupt and GpioInt
241 * resources, making this fallback ambiguous, in many common cases
242 * the device will only expose one IRQ, and this fallback
243 * allows a common code path across either kind of resource.
244 */
Brian Norris46c42d82019-07-29 13:49:54 -0700245 if (num == 0 && has_acpi_companion(&dev->dev)) {
Andy Shevchenko71564a22019-10-23 15:25:05 +0300246 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
Brian Norris46c42d82019-07-29 13:49:54 -0700247 /* Our callers expect -ENXIO for missing IRQs. */
248 if (ret >= 0 || ret == -EPROBE_DEFER)
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500249 goto out;
Brian Norris46c42d82019-07-29 13:49:54 -0700250 }
Enrico Granatadaaef252019-02-11 11:01:12 -0800251
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500252 ret = -ENXIO;
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +0000253#endif
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500254out:
255 WARN(ret == 0, "0 is an invalid IRQ number\n");
256 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200258EXPORT_SYMBOL_GPL(platform_get_irq_optional);
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700259
260/**
261 * platform_get_irq - get an IRQ for a device
262 * @dev: platform device
263 * @num: IRQ number index
264 *
265 * Gets an IRQ for a platform device and prints an error message if finding the
266 * IRQ fails. Device drivers should check the return value for errors so as to
267 * not pass a negative integer value to the request_irq() APIs.
268 *
Mauro Carvalho Chehabf0825242020-04-14 18:48:45 +0200269 * For example::
270 *
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700271 * int irq = platform_get_irq(pdev, 0);
272 * if (irq < 0)
273 * return irq;
274 *
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500275 * Return: non-zero IRQ number on success, negative error number on failure.
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700276 */
277int platform_get_irq(struct platform_device *dev, unsigned int num)
278{
279 int ret;
280
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200281 ret = platform_get_irq_optional(dev, num);
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700282 if (ret < 0 && ret != -EPROBE_DEFER)
283 dev_err(&dev->dev, "IRQ index %u not found\n", num);
284
285 return ret;
286}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500287EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/**
Stephen Boyd4b835552016-01-06 17:12:47 -0800290 * platform_irq_count - Count the number of IRQs a platform device uses
291 * @dev: platform device
292 *
293 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
294 */
295int platform_irq_count(struct platform_device *dev)
296{
297 int ret, nr = 0;
298
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200299 while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
Stephen Boyd4b835552016-01-06 17:12:47 -0800300 nr++;
301
302 if (ret == -EPROBE_DEFER)
303 return ret;
304
305 return nr;
306}
307EXPORT_SYMBOL_GPL(platform_irq_count);
308
John Garrye15f2fa2020-12-02 18:36:56 +0800309struct irq_affinity_devres {
310 unsigned int count;
311 unsigned int irq[];
312};
313
314static void platform_disable_acpi_irq(struct platform_device *pdev, int index)
315{
316 struct resource *r;
317
318 r = platform_get_resource(pdev, IORESOURCE_IRQ, index);
319 if (r)
320 irqresource_disabled(r, 0);
321}
322
323static void devm_platform_get_irqs_affinity_release(struct device *dev,
324 void *res)
325{
326 struct irq_affinity_devres *ptr = res;
327 int i;
328
329 for (i = 0; i < ptr->count; i++) {
330 irq_dispose_mapping(ptr->irq[i]);
331
332 if (has_acpi_companion(dev))
333 platform_disable_acpi_irq(to_platform_device(dev), i);
334 }
335}
336
337/**
338 * devm_platform_get_irqs_affinity - devm method to get a set of IRQs for a
339 * device using an interrupt affinity descriptor
340 * @dev: platform device pointer
341 * @affd: affinity descriptor
342 * @minvec: minimum count of interrupt vectors
343 * @maxvec: maximum count of interrupt vectors
344 * @irqs: pointer holder for IRQ numbers
345 *
346 * Gets a set of IRQs for a platform device, and updates IRQ afffinty according
347 * to the passed affinity descriptor
348 *
349 * Return: Number of vectors on success, negative error number on failure.
350 */
351int devm_platform_get_irqs_affinity(struct platform_device *dev,
352 struct irq_affinity *affd,
353 unsigned int minvec,
354 unsigned int maxvec,
355 int **irqs)
356{
357 struct irq_affinity_devres *ptr;
358 struct irq_affinity_desc *desc;
359 size_t size;
360 int i, ret, nvec;
361
362 if (!affd)
363 return -EPERM;
364
365 if (maxvec < minvec)
366 return -ERANGE;
367
368 nvec = platform_irq_count(dev);
369
370 if (nvec < minvec)
371 return -ENOSPC;
372
373 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
374 if (nvec < minvec)
375 return -ENOSPC;
376
377 if (nvec > maxvec)
378 nvec = maxvec;
379
380 size = sizeof(*ptr) + sizeof(unsigned int) * nvec;
381 ptr = devres_alloc(devm_platform_get_irqs_affinity_release, size,
382 GFP_KERNEL);
383 if (!ptr)
384 return -ENOMEM;
385
386 ptr->count = nvec;
387
388 for (i = 0; i < nvec; i++) {
389 int irq = platform_get_irq(dev, i);
390 if (irq < 0) {
391 ret = irq;
392 goto err_free_devres;
393 }
394 ptr->irq[i] = irq;
395 }
396
397 desc = irq_create_affinity_masks(nvec, affd);
398 if (!desc) {
399 ret = -ENOMEM;
400 goto err_free_devres;
401 }
402
403 for (i = 0; i < nvec; i++) {
404 ret = irq_update_affinity_desc(ptr->irq[i], &desc[i]);
405 if (ret) {
406 dev_err(&dev->dev, "failed to update irq%d affinity descriptor (%d)\n",
407 ptr->irq[i], ret);
408 goto err_free_desc;
409 }
410 }
411
412 devres_add(&dev->dev, ptr);
413
414 kfree(desc);
415
416 *irqs = ptr->irq;
417
418 return nvec;
419
420err_free_desc:
421 kfree(desc);
422err_free_devres:
423 devres_free(ptr);
424 return ret;
425}
426EXPORT_SYMBOL_GPL(devm_platform_get_irqs_affinity);
427
Stephen Boyd4b835552016-01-06 17:12:47 -0800428/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800429 * platform_get_resource_byname - get a resource for a device by name
430 * @dev: platform device
431 * @type: resource type
432 * @name: resource name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800434struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200435 unsigned int type,
436 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Simon Schwartz39cc5392019-12-10 17:41:37 -0500438 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 for (i = 0; i < dev->num_resources; i++) {
441 struct resource *r = &dev->resource[i];
442
Peter Ujfalusi1b8cb922012-08-23 17:10:00 +0300443 if (unlikely(!r->name))
444 continue;
445
Magnus Dammc9f66162008-10-15 22:05:15 -0700446 if (type == resource_type(r) && !strcmp(r->name, name))
447 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 return NULL;
450}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500451EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Hans de Goedef1da5672019-10-05 23:04:47 +0200453static int __platform_get_irq_byname(struct platform_device *dev,
454 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Grygorii Strashkoad696742014-05-20 13:42:02 +0300456 struct resource *r;
Andy Shevchenko71564a22019-10-23 15:25:05 +0300457 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Guenter Roeckaff008a2014-06-17 15:51:02 -0700459 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
Guenter Roeckaff008a2014-06-17 15:51:02 -0700460 ret = of_irq_get_byname(dev->dev.of_node, name);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +0300461 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -0700462 return ret;
463 }
Grygorii Strashkoad696742014-05-20 13:42:02 +0300464
465 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500466 if (r) {
467 WARN(r->start == 0, "0 is an invalid IRQ number\n");
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700468 return r->start;
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500469 }
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700470
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700471 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
Hans de Goedef1da5672019-10-05 23:04:47 +0200473
474/**
475 * platform_get_irq_byname - get an IRQ for a device by name
476 * @dev: platform device
477 * @name: IRQ name
478 *
479 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
480 *
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500481 * Return: non-zero IRQ number on success, negative error number on failure.
Hans de Goedef1da5672019-10-05 23:04:47 +0200482 */
483int platform_get_irq_byname(struct platform_device *dev, const char *name)
484{
485 int ret;
486
487 ret = __platform_get_irq_byname(dev, name);
488 if (ret < 0 && ret != -EPROBE_DEFER)
489 dev_err(&dev->dev, "IRQ %s not found\n", name);
490
491 return ret;
492}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500493EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495/**
Hans de Goedef1da5672019-10-05 23:04:47 +0200496 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
497 * @dev: platform device
498 * @name: IRQ name
499 *
500 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
501 * does not print an error message if an IRQ can not be obtained.
502 *
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500503 * Return: non-zero IRQ number on success, negative error number on failure.
Hans de Goedef1da5672019-10-05 23:04:47 +0200504 */
505int platform_get_irq_byname_optional(struct platform_device *dev,
506 const char *name)
507{
508 return __platform_get_irq_byname(dev, name);
509}
510EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
511
512/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800513 * platform_add_devices - add a numbers of platform devices
514 * @devs: array of platform devices to add
515 * @num: number of platform devices in array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
517int platform_add_devices(struct platform_device **devs, int num)
518{
519 int i, ret = 0;
520
521 for (i = 0; i < num; i++) {
522 ret = platform_device_register(devs[i]);
523 if (ret) {
524 while (--i >= 0)
525 platform_device_unregister(devs[i]);
526 break;
527 }
528 }
529
530 return ret;
531}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500532EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Russell King37c12e72005-11-05 21:19:33 +0000534struct platform_object {
535 struct platform_device pdev;
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200536 char name[];
Russell King37c12e72005-11-05 21:19:33 +0000537};
538
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200539/*
540 * Set up default DMA mask for platform devices if the they weren't
541 * previously set by the architecture / DT.
542 */
543static void setup_pdev_dma_masks(struct platform_device *pdev)
544{
Ulf Hansson9495b7e2020-04-22 12:09:54 +0200545 pdev->dev.dma_parms = &pdev->dma_parms;
546
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200547 if (!pdev->dev.coherent_dma_mask)
548 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Christoph Hellwige3a36eb2020-03-11 17:07:10 +0100549 if (!pdev->dev.dma_mask) {
550 pdev->platform_dma_mask = DMA_BIT_MASK(32);
551 pdev->dev.dma_mask = &pdev->platform_dma_mask;
552 }
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200553};
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000556 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800557 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000558 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800559 * Free all memory associated with a platform device. This function must
560 * _only_ be externally called in error cases. All other usage is a bug.
Russell King37c12e72005-11-05 21:19:33 +0000561 */
562void platform_device_put(struct platform_device *pdev)
563{
Andy Shevchenko99fef582018-12-03 20:21:41 +0200564 if (!IS_ERR_OR_NULL(pdev))
Russell King37c12e72005-11-05 21:19:33 +0000565 put_device(&pdev->dev);
566}
567EXPORT_SYMBOL_GPL(platform_device_put);
568
569static void platform_device_release(struct device *dev)
570{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800571 struct platform_object *pa = container_of(dev, struct platform_object,
572 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000573
Grant Likely7096d042010-10-20 11:45:13 -0600574 of_device_node_put(&pa->pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000575 kfree(pa->pdev.dev.platform_data);
Samuel Ortize710d7d2011-04-08 00:43:01 +0200576 kfree(pa->pdev.mfd_cell);
Russell King37c12e72005-11-05 21:19:33 +0000577 kfree(pa->pdev.resource);
Kim Phillips3d713e02014-06-02 19:42:58 -0500578 kfree(pa->pdev.driver_override);
Russell King37c12e72005-11-05 21:19:33 +0000579 kfree(pa);
580}
581
582/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000583 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800584 * @name: base name of the device we're adding
585 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000586 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800587 * Create a platform device object which can have other objects attached
588 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000589 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200590struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000591{
592 struct platform_object *pa;
593
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200594 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000595 if (pa) {
596 strcpy(pa->name, name);
597 pa->pdev.name = pa->name;
598 pa->pdev.id = id;
599 device_initialize(&pa->pdev.dev);
600 pa->pdev.dev.release = platform_device_release;
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200601 setup_pdev_dma_masks(&pa->pdev);
Russell King37c12e72005-11-05 21:19:33 +0000602 }
603
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500604 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000605}
606EXPORT_SYMBOL_GPL(platform_device_alloc);
607
608/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000609 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800610 * @pdev: platform device allocated by platform_device_alloc to add resources to
611 * @res: set of resources that needs to be allocated for the device
612 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000613 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800614 * Add a copy of the resources to the platform device. The memory
615 * associated with the resources will be freed when the platform device is
616 * released.
Russell King37c12e72005-11-05 21:19:33 +0000617 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800618int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100619 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000620{
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200621 struct resource *r = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000622
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200623 if (res) {
624 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
625 if (!r)
626 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000627 }
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200628
Uwe Kleine-König4a03d6f2011-04-20 09:44:45 +0200629 kfree(pdev->resource);
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200630 pdev->resource = r;
631 pdev->num_resources = num;
632 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000633}
634EXPORT_SYMBOL_GPL(platform_device_add_resources);
635
636/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000637 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800638 * @pdev: platform device allocated by platform_device_alloc to add resources to
639 * @data: platform specific data for this platform device
640 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000641 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800642 * Add a copy of platform specific data to the platform device's
643 * platform_data pointer. The memory associated with the platform data
644 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000645 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800646int platform_device_add_data(struct platform_device *pdev, const void *data,
647 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000648{
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200649 void *d = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000650
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200651 if (data) {
652 d = kmemdup(data, size, GFP_KERNEL);
653 if (!d)
654 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000655 }
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200656
Uwe Kleine-König251e0312011-04-20 09:44:43 +0200657 kfree(pdev->dev.platform_data);
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200658 pdev->dev.platform_data = d;
659 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000660}
661EXPORT_SYMBOL_GPL(platform_device_add_data);
662
663/**
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200664 * platform_device_add_properties - add built-in properties to a platform device
665 * @pdev: platform device to add properties to
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300666 * @properties: null terminated array of properties to add
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200667 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300668 * The function will take deep copy of @properties and attach the copy to the
669 * platform device. The memory associated with properties will be freed when the
670 * platform device is released.
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200671 */
672int platform_device_add_properties(struct platform_device *pdev,
Jan Kiszka277036f2017-06-02 07:43:27 +0200673 const struct property_entry *properties)
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200674{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300675 return device_add_properties(&pdev->dev, properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200676}
677EXPORT_SYMBOL_GPL(platform_device_add_properties);
678
679/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800680 * platform_device_add - add a platform device to device hierarchy
681 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800683 * This is part 2 of platform_device_register(), though may be called
684 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Russell King37c12e72005-11-05 21:19:33 +0000686int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Simon Schwartz39cc5392019-12-10 17:41:37 -0500688 u32 i;
689 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (!pdev)
692 return -EINVAL;
693
694 if (!pdev->dev.parent)
695 pdev->dev.parent = &platform_bus;
696
697 pdev->dev.bus = &platform_bus_type;
698
Jean Delvare689ae232012-07-27 22:14:59 +0200699 switch (pdev->id) {
700 default:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100701 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Jean Delvare689ae232012-07-27 22:14:59 +0200702 break;
703 case PLATFORM_DEVID_NONE:
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700704 dev_set_name(&pdev->dev, "%s", pdev->name);
Jean Delvare689ae232012-07-27 22:14:59 +0200705 break;
706 case PLATFORM_DEVID_AUTO:
707 /*
708 * Automatically allocated device ID. We mark it as such so
709 * that we remember it must be freed, and we append a suffix
710 * to avoid namespace collision with explicit IDs.
711 */
Bartosz Golaszewski0de75112020-09-09 20:02:48 +0200712 ret = ida_alloc(&platform_devid_ida, GFP_KERNEL);
Jean Delvare689ae232012-07-27 22:14:59 +0200713 if (ret < 0)
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700714 goto err_out;
Jean Delvare689ae232012-07-27 22:14:59 +0200715 pdev->id = ret;
716 pdev->id_auto = true;
717 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
718 break;
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 for (i = 0; i < pdev->num_resources; i++) {
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700722 struct resource *p, *r = &pdev->resource[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100725 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 p = r->parent;
728 if (!p) {
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700729 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 p = &iomem_resource;
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700731 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 p = &ioport_resource;
733 }
734
Andy Shevchenko25ebcb72019-04-04 11:11:58 +0300735 if (p) {
736 ret = insert_resource(p, r);
737 if (ret) {
738 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
739 goto failed;
740 }
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
744 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100745 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Russell Kinge3915532006-05-06 08:15:26 +0100747 ret = device_add(&pdev->dev);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700748 if (ret == 0)
749 return ret;
750
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700751 failed:
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700752 if (pdev->id_auto) {
Bartosz Golaszewski0de75112020-09-09 20:02:48 +0200753 ida_free(&platform_devid_ida, pdev->id);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700754 pdev->id = PLATFORM_DEVID_AUTO;
755 }
756
Colin Ian King0707cfa2020-01-16 17:57:58 +0000757 while (i--) {
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700758 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100759 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700760 release_resource(r);
761 }
Magnus Dammc9f66162008-10-15 22:05:15 -0700762
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700763 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return ret;
765}
Russell King37c12e72005-11-05 21:19:33 +0000766EXPORT_SYMBOL_GPL(platform_device_add);
767
768/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800769 * platform_device_del - remove a platform-level device
770 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500771 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800772 * Note that this function will also release all memory- and port-based
773 * resources owned by the device (@dev->resource). This function must
774 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500775 */
776void platform_device_del(struct platform_device *pdev)
777{
Simon Schwartz39cc5392019-12-10 17:41:37 -0500778 u32 i;
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500779
Andy Shevchenko99fef582018-12-03 20:21:41 +0200780 if (!IS_ERR_OR_NULL(pdev)) {
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700781 device_del(&pdev->dev);
782
783 if (pdev->id_auto) {
Bartosz Golaszewski0de75112020-09-09 20:02:48 +0200784 ida_free(&platform_devid_ida, pdev->id);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700785 pdev->id = PLATFORM_DEVID_AUTO;
786 }
787
788 for (i = 0; i < pdev->num_resources; i++) {
789 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100790 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700791 release_resource(r);
792 }
793 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500794}
795EXPORT_SYMBOL_GPL(platform_device_del);
796
797/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800798 * platform_device_register - add a platform-level device
799 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000800 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800801int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000802{
803 device_initialize(&pdev->dev);
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200804 setup_pdev_dma_masks(pdev);
Russell King37c12e72005-11-05 21:19:33 +0000805 return platform_device_add(pdev);
806}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500807EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800810 * platform_device_unregister - unregister a platform-level device
811 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800813 * Unregistration is done in 2 steps. First we release all resources
814 * and remove it from the subsystem, then we drop reference count by
815 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800817void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500819 platform_device_del(pdev);
820 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500822EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824/**
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200825 * platform_device_register_full - add a platform-level device with
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200826 * resources and platform-specific data
827 *
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200828 * @pdevinfo: data used to create device
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700829 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200830 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700831 */
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200832struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +0100833 const struct platform_device_info *pdevinfo)
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700834{
Colin Ian King45bb08d2020-04-02 12:13:41 +0100835 int ret;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700836 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700837
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200838 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200839 if (!pdev)
Johannes Berg36cf3b12019-03-01 13:24:47 +0100840 return ERR_PTR(-ENOMEM);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700841
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200842 pdev->dev.parent = pdevinfo->parent;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100843 pdev->dev.fwnode = pdevinfo->fwnode;
Mans Rullgard2c1ea6a2019-02-21 11:29:35 +0000844 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
845 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700846
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200847 if (pdevinfo->dma_mask) {
Christoph Hellwige3a36eb2020-03-11 17:07:10 +0100848 pdev->platform_dma_mask = pdevinfo->dma_mask;
849 pdev->dev.dma_mask = &pdev->platform_dma_mask;
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200850 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
851 }
852
853 ret = platform_device_add_resources(pdev,
854 pdevinfo->res, pdevinfo->num_res);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400855 if (ret)
856 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700857
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200858 ret = platform_device_add_data(pdev,
859 pdevinfo->data, pdevinfo->size_data);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400860 if (ret)
861 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200862
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300863 if (pdevinfo->properties) {
864 ret = platform_device_add_properties(pdev,
865 pdevinfo->properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200866 if (ret)
867 goto err;
868 }
869
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200870 ret = platform_device_add(pdev);
871 if (ret) {
872err:
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100873 ACPI_COMPANION_SET(&pdev->dev, NULL);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200874 platform_device_put(pdev);
875 return ERR_PTR(ret);
876 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700877
878 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700879}
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200880EXPORT_SYMBOL_GPL(platform_device_register_full);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700881
Russell King00d3dcd2005-11-09 17:23:39 +0000882/**
Libo Chen94470572013-05-25 12:40:50 +0800883 * __platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800884 * @drv: platform driver structure
Randy Dunlap08801f92013-07-14 17:43:06 -0700885 * @owner: owning module/driver
Russell King00d3dcd2005-11-09 17:23:39 +0000886 */
Libo Chen94470572013-05-25 12:40:50 +0800887int __platform_driver_register(struct platform_driver *drv,
888 struct module *owner)
Russell King00d3dcd2005-11-09 17:23:39 +0000889{
Libo Chen94470572013-05-25 12:40:50 +0800890 drv->driver.owner = owner;
Russell King00d3dcd2005-11-09 17:23:39 +0000891 drv->driver.bus = &platform_bus_type;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200892
Russell King00d3dcd2005-11-09 17:23:39 +0000893 return driver_register(&drv->driver);
894}
Libo Chen94470572013-05-25 12:40:50 +0800895EXPORT_SYMBOL_GPL(__platform_driver_register);
Russell King00d3dcd2005-11-09 17:23:39 +0000896
897/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000898 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800899 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000900 */
901void platform_driver_unregister(struct platform_driver *drv)
902{
903 driver_unregister(&drv->driver);
904}
905EXPORT_SYMBOL_GPL(platform_driver_unregister);
906
Uwe Kleine-König16085662020-11-19 13:46:10 +0100907static int platform_probe_fail(struct platform_device *pdev)
Uwe Kleine-Könige21d740a2020-11-19 13:46:09 +0100908{
909 return -ENXIO;
910}
911
David Brownellc67334f2006-11-16 23:28:47 -0800912/**
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100913 * __platform_driver_probe - register driver for non-hotpluggable device
David Brownellc67334f2006-11-16 23:28:47 -0800914 * @drv: platform driver structure
Johan Hovold3f9120b2013-09-23 16:27:26 +0200915 * @probe: the driver probe routine, probably from an __init section
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100916 * @module: module which will be the owner of the driver
David Brownellc67334f2006-11-16 23:28:47 -0800917 *
918 * Use this instead of platform_driver_register() when you know the device
919 * is not hotpluggable and has already been registered, and you want to
920 * remove its run-once probe() infrastructure from memory after the driver
921 * has bound to the device.
922 *
923 * One typical use for this would be with drivers for controllers integrated
924 * into system-on-chip processors, where the controller devices have been
925 * configured as part of board setup.
926 *
Johan Hovold3f9120b2013-09-23 16:27:26 +0200927 * Note that this is incompatible with deferred probing.
Fabio Porcedda647c86d2013-03-26 10:35:15 +0100928 *
David Brownellc67334f2006-11-16 23:28:47 -0800929 * Returns zero if the driver registered and bound to a device, else returns
930 * a negative error code and with the driver not registered.
931 */
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100932int __init_or_module __platform_driver_probe(struct platform_driver *drv,
933 int (*probe)(struct platform_device *), struct module *module)
David Brownellc67334f2006-11-16 23:28:47 -0800934{
935 int retval, code;
936
Dmitry Torokhov5c36eb22015-03-30 16:20:07 -0700937 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
938 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
939 drv->driver.name, __func__);
940 return -EINVAL;
941 }
942
943 /*
944 * We have to run our probes synchronously because we check if
945 * we find any devices to bind to and exit with error if there
946 * are any.
947 */
948 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
949
Johan Hovold3f9120b2013-09-23 16:27:26 +0200950 /*
951 * Prevent driver from requesting probe deferral to avoid further
952 * futile probe attempts.
953 */
954 drv->prevent_deferred_probe = true;
955
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700956 /* make sure driver won't have bind/unbind attributes */
957 drv->driver.suppress_bind_attrs = true;
958
David Brownellc67334f2006-11-16 23:28:47 -0800959 /* temporary section violation during probe() */
960 drv->probe = probe;
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100961 retval = code = __platform_driver_register(drv, module);
Kuppuswamy Sathyanarayanan388bcc62020-04-08 14:40:03 -0700962 if (retval)
963 return retval;
David Brownellc67334f2006-11-16 23:28:47 -0800964
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700965 /*
966 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800967 * the list of drivers in order to probe new devices. Check to see
968 * if the probe was successful, and make sure any forced probes of
969 * new devices fail.
970 */
Patrick Pannutod79d3242010-08-06 17:12:41 -0700971 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
Uwe Kleine-König16085662020-11-19 13:46:10 +0100972 drv->probe = platform_probe_fail;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800973 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800974 retval = -ENODEV;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700975 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800976
977 if (code != retval)
978 platform_driver_unregister(drv);
979 return retval;
980}
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100981EXPORT_SYMBOL_GPL(__platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800983/**
Wolfram Sang291f6532014-10-28 17:40:42 +0100984 * __platform_create_bundle - register driver and create corresponding device
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800985 * @driver: platform driver structure
986 * @probe: the driver probe routine, probably from an __init section
987 * @res: set of resources that needs to be allocated for the device
988 * @n_res: number of resources
989 * @data: platform specific data for this platform device
990 * @size: size of platform specific data
Wolfram Sang291f6532014-10-28 17:40:42 +0100991 * @module: module which will be the owner of the driver
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800992 *
993 * Use this in legacy-style modules that probe hardware directly and
994 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200995 *
996 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800997 */
Wolfram Sang291f6532014-10-28 17:40:42 +0100998struct platform_device * __init_or_module __platform_create_bundle(
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800999 struct platform_driver *driver,
1000 int (*probe)(struct platform_device *),
1001 struct resource *res, unsigned int n_res,
Wolfram Sang291f6532014-10-28 17:40:42 +01001002 const void *data, size_t size, struct module *module)
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -08001003{
1004 struct platform_device *pdev;
1005 int error;
1006
1007 pdev = platform_device_alloc(driver->driver.name, -1);
1008 if (!pdev) {
1009 error = -ENOMEM;
1010 goto err_out;
1011 }
1012
Anton Vorontsov807508c2010-09-07 17:31:54 +04001013 error = platform_device_add_resources(pdev, res, n_res);
1014 if (error)
1015 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -08001016
Anton Vorontsov807508c2010-09-07 17:31:54 +04001017 error = platform_device_add_data(pdev, data, size);
1018 if (error)
1019 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -08001020
1021 error = platform_device_add(pdev);
1022 if (error)
1023 goto err_pdev_put;
1024
Wolfram Sang291f6532014-10-28 17:40:42 +01001025 error = __platform_driver_probe(driver, probe, module);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -08001026 if (error)
1027 goto err_pdev_del;
1028
1029 return pdev;
1030
1031err_pdev_del:
1032 platform_device_del(pdev);
1033err_pdev_put:
1034 platform_device_put(pdev);
1035err_out:
1036 return ERR_PTR(error);
1037}
Wolfram Sang291f6532014-10-28 17:40:42 +01001038EXPORT_SYMBOL_GPL(__platform_create_bundle);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -08001039
Thierry Redingdbe22562015-09-25 17:29:04 +02001040/**
1041 * __platform_register_drivers - register an array of platform drivers
1042 * @drivers: an array of drivers to register
1043 * @count: the number of drivers to register
1044 * @owner: module owning the drivers
1045 *
1046 * Registers platform drivers specified by an array. On failure to register a
1047 * driver, all previously registered drivers will be unregistered. Callers of
1048 * this API should use platform_unregister_drivers() to unregister drivers in
1049 * the reverse order.
1050 *
1051 * Returns: 0 on success or a negative error code on failure.
1052 */
1053int __platform_register_drivers(struct platform_driver * const *drivers,
1054 unsigned int count, struct module *owner)
1055{
1056 unsigned int i;
1057 int err;
1058
1059 for (i = 0; i < count; i++) {
1060 pr_debug("registering platform driver %ps\n", drivers[i]);
1061
1062 err = __platform_driver_register(drivers[i], owner);
1063 if (err < 0) {
1064 pr_err("failed to register platform driver %ps: %d\n",
1065 drivers[i], err);
1066 goto error;
1067 }
1068 }
1069
1070 return 0;
1071
1072error:
1073 while (i--) {
1074 pr_debug("unregistering platform driver %ps\n", drivers[i]);
1075 platform_driver_unregister(drivers[i]);
1076 }
1077
1078 return err;
1079}
1080EXPORT_SYMBOL_GPL(__platform_register_drivers);
1081
1082/**
1083 * platform_unregister_drivers - unregister an array of platform drivers
1084 * @drivers: an array of drivers to unregister
1085 * @count: the number of drivers to unregister
1086 *
Tang Binc82c83c2020-05-20 22:12:02 +08001087 * Unregisters platform drivers specified by an array. This is typically used
Thierry Redingdbe22562015-09-25 17:29:04 +02001088 * to complement an earlier call to platform_register_drivers(). Drivers are
1089 * unregistered in the reverse order in which they were registered.
1090 */
1091void platform_unregister_drivers(struct platform_driver * const *drivers,
1092 unsigned int count)
1093{
1094 while (count--) {
1095 pr_debug("unregistering platform driver %ps\n", drivers[count]);
1096 platform_driver_unregister(drivers[count]);
1097 }
1098}
1099EXPORT_SYMBOL_GPL(platform_unregister_drivers);
1100
Eric Miao57fee4a2009-02-04 11:52:40 +08001101static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +01001102 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +08001103 struct platform_device *pdev)
1104{
1105 while (id->name[0]) {
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001106 if (strcmp(pdev->name, id->name) == 0) {
Eric Miao57fee4a2009-02-04 11:52:40 +08001107 pdev->id_entry = id;
1108 return id;
1109 }
1110 id++;
1111 }
1112 return NULL;
1113}
1114
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001115#ifdef CONFIG_PM_SLEEP
1116
1117static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001119 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1120 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int ret = 0;
1122
Magnus Damm783ea7d2009-06-04 22:13:33 +02001123 if (dev->driver && pdrv->suspend)
1124 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d82006-09-03 13:16:45 -07001125
1126 return ret;
1127}
1128
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001129static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001131 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1132 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 int ret = 0;
1134
Magnus Damm783ea7d2009-06-04 22:13:33 +02001135 if (dev->driver && pdrv->resume)
1136 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return ret;
1139}
1140
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001141#endif /* CONFIG_PM_SLEEP */
Magnus Damm9d730222009-08-20 20:25:32 +02001142
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001143#ifdef CONFIG_SUSPEND
1144
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001145int platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001146{
1147 struct device_driver *drv = dev->driver;
1148 int ret = 0;
1149
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001150 if (!drv)
1151 return 0;
1152
1153 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001154 if (drv->pm->suspend)
1155 ret = drv->pm->suspend(dev);
1156 } else {
1157 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1158 }
1159
1160 return ret;
1161}
1162
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001163int platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001164{
1165 struct device_driver *drv = dev->driver;
1166 int ret = 0;
1167
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001168 if (!drv)
1169 return 0;
1170
1171 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001172 if (drv->pm->resume)
1173 ret = drv->pm->resume(dev);
1174 } else {
1175 ret = platform_legacy_resume(dev);
1176 }
1177
1178 return ret;
1179}
1180
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001181#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001182
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +02001183#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001184
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001185int platform_pm_freeze(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001186{
1187 struct device_driver *drv = dev->driver;
1188 int ret = 0;
1189
1190 if (!drv)
1191 return 0;
1192
1193 if (drv->pm) {
1194 if (drv->pm->freeze)
1195 ret = drv->pm->freeze(dev);
1196 } else {
1197 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1198 }
1199
1200 return ret;
1201}
1202
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001203int platform_pm_thaw(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001204{
1205 struct device_driver *drv = dev->driver;
1206 int ret = 0;
1207
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001208 if (!drv)
1209 return 0;
1210
1211 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001212 if (drv->pm->thaw)
1213 ret = drv->pm->thaw(dev);
1214 } else {
1215 ret = platform_legacy_resume(dev);
1216 }
1217
1218 return ret;
1219}
1220
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001221int platform_pm_poweroff(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001222{
1223 struct device_driver *drv = dev->driver;
1224 int ret = 0;
1225
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001226 if (!drv)
1227 return 0;
1228
1229 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001230 if (drv->pm->poweroff)
1231 ret = drv->pm->poweroff(dev);
1232 } else {
1233 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1234 }
1235
1236 return ret;
1237}
1238
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001239int platform_pm_restore(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001240{
1241 struct device_driver *drv = dev->driver;
1242 int ret = 0;
1243
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001244 if (!drv)
1245 return 0;
1246
1247 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001248 if (drv->pm->restore)
1249 ret = drv->pm->restore(dev);
1250 } else {
1251 ret = platform_legacy_resume(dev);
1252 }
1253
1254 return ret;
1255}
1256
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001257#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001258
Uwe Kleine-Könige21d740a2020-11-19 13:46:09 +01001259/* modalias support enables more hands-off userspace setup:
1260 * (a) environment variable lets new-style hotplug events work once system is
1261 * fully running: "modprobe $MODALIAS"
1262 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
1263 * mishandled before system is fully running: "modprobe $(cat modalias)"
1264 */
1265static ssize_t modalias_show(struct device *dev,
1266 struct device_attribute *attr, char *buf)
1267{
1268 struct platform_device *pdev = to_platform_device(dev);
1269 int len;
1270
1271 len = of_device_modalias(dev, buf, PAGE_SIZE);
1272 if (len != -ENODEV)
1273 return len;
1274
1275 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
1276 if (len != -ENODEV)
1277 return len;
1278
1279 return sysfs_emit(buf, "platform:%s\n", pdev->name);
1280}
1281static DEVICE_ATTR_RO(modalias);
1282
1283static ssize_t numa_node_show(struct device *dev,
1284 struct device_attribute *attr, char *buf)
1285{
1286 return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1287}
1288static DEVICE_ATTR_RO(numa_node);
1289
1290static ssize_t driver_override_show(struct device *dev,
1291 struct device_attribute *attr, char *buf)
1292{
1293 struct platform_device *pdev = to_platform_device(dev);
1294 ssize_t len;
1295
1296 device_lock(dev);
1297 len = sysfs_emit(buf, "%s\n", pdev->driver_override);
1298 device_unlock(dev);
1299
1300 return len;
1301}
1302
1303static ssize_t driver_override_store(struct device *dev,
1304 struct device_attribute *attr,
1305 const char *buf, size_t count)
1306{
1307 struct platform_device *pdev = to_platform_device(dev);
1308 char *driver_override, *old, *cp;
1309
1310 /* We need to keep extra room for a newline */
1311 if (count >= (PAGE_SIZE - 1))
1312 return -EINVAL;
1313
1314 driver_override = kstrndup(buf, count, GFP_KERNEL);
1315 if (!driver_override)
1316 return -ENOMEM;
1317
1318 cp = strchr(driver_override, '\n');
1319 if (cp)
1320 *cp = '\0';
1321
1322 device_lock(dev);
1323 old = pdev->driver_override;
1324 if (strlen(driver_override)) {
1325 pdev->driver_override = driver_override;
1326 } else {
1327 kfree(driver_override);
1328 pdev->driver_override = NULL;
1329 }
1330 device_unlock(dev);
1331
1332 kfree(old);
1333
1334 return count;
1335}
1336static DEVICE_ATTR_RW(driver_override);
1337
1338static struct attribute *platform_dev_attrs[] = {
1339 &dev_attr_modalias.attr,
1340 &dev_attr_numa_node.attr,
1341 &dev_attr_driver_override.attr,
1342 NULL,
1343};
1344
1345static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a,
1346 int n)
1347{
1348 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1349
1350 if (a == &dev_attr_numa_node.attr &&
1351 dev_to_node(dev) == NUMA_NO_NODE)
1352 return 0;
1353
1354 return a->mode;
1355}
1356
1357static struct attribute_group platform_dev_group = {
1358 .attrs = platform_dev_attrs,
1359 .is_visible = platform_dev_attrs_visible,
1360};
1361__ATTRIBUTE_GROUPS(platform_dev);
1362
1363
1364/**
1365 * platform_match - bind platform device to platform driver.
1366 * @dev: device.
1367 * @drv: driver.
1368 *
1369 * Platform device IDs are assumed to be encoded like this:
1370 * "<name><instance>", where <name> is a short description of the type of
1371 * device, like "pci" or "floppy", and <instance> is the enumerated
1372 * instance of the device, like '0' or '42'. Driver IDs are simply
1373 * "<name>". So, extract the <name> from the platform_device structure,
1374 * and compare it against the name of the driver. Return whether they match
1375 * or not.
1376 */
1377static int platform_match(struct device *dev, struct device_driver *drv)
1378{
1379 struct platform_device *pdev = to_platform_device(dev);
1380 struct platform_driver *pdrv = to_platform_driver(drv);
1381
1382 /* When driver_override is set, only bind to the matching driver */
1383 if (pdev->driver_override)
1384 return !strcmp(pdev->driver_override, drv->name);
1385
1386 /* Attempt an OF style match first */
1387 if (of_driver_match_device(dev, drv))
1388 return 1;
1389
1390 /* Then try ACPI style match */
1391 if (acpi_driver_match_device(dev, drv))
1392 return 1;
1393
1394 /* Then try to match against the id table */
1395 if (pdrv->id_table)
1396 return platform_match_id(pdrv->id_table, pdev) != NULL;
1397
1398 /* fall-back to driver name match */
1399 return (strcmp(pdev->name, drv->name) == 0);
1400}
1401
1402static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1403{
1404 struct platform_device *pdev = to_platform_device(dev);
1405 int rc;
1406
1407 /* Some devices have extra OF data and an OF-style MODALIAS */
1408 rc = of_device_uevent_modalias(dev, env);
1409 if (rc != -ENODEV)
1410 return rc;
1411
1412 rc = acpi_device_uevent_modalias(dev, env);
1413 if (rc != -ENODEV)
1414 return rc;
1415
1416 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1417 pdev->name);
1418 return 0;
1419}
1420
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001421static int platform_probe(struct device *_dev)
1422{
1423 struct platform_driver *drv = to_platform_driver(_dev->driver);
1424 struct platform_device *dev = to_platform_device(_dev);
1425 int ret;
1426
1427 /*
1428 * A driver registered using platform_driver_probe() cannot be bound
1429 * again later because the probe function usually lives in __init code
1430 * and so is gone. For these drivers .probe is set to
1431 * platform_probe_fail in __platform_driver_probe(). Don't even prepare
1432 * clocks and PM domains for these to match the traditional behaviour.
1433 */
1434 if (unlikely(drv->probe == platform_probe_fail))
1435 return -ENXIO;
1436
1437 ret = of_clk_set_defaults(_dev->of_node, false);
1438 if (ret < 0)
1439 return ret;
1440
1441 ret = dev_pm_domain_attach(_dev, true);
1442 if (ret)
1443 goto out;
1444
1445 if (drv->probe) {
1446 ret = drv->probe(dev);
1447 if (ret)
1448 dev_pm_domain_detach(_dev, true);
1449 }
1450
1451out:
1452 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
1453 dev_warn(_dev, "probe deferral not supported\n");
1454 ret = -ENXIO;
1455 }
1456
1457 return ret;
1458}
1459
1460static int platform_remove(struct device *_dev)
1461{
1462 struct platform_driver *drv = to_platform_driver(_dev->driver);
1463 struct platform_device *dev = to_platform_device(_dev);
1464 int ret = 0;
1465
1466 if (drv->remove)
1467 ret = drv->remove(dev);
1468 dev_pm_domain_detach(_dev, true);
1469
1470 return ret;
1471}
1472
1473static void platform_shutdown(struct device *_dev)
1474{
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001475 struct platform_device *dev = to_platform_device(_dev);
Dmitry Baryshkov46e85af2020-12-13 02:55:33 +03001476 struct platform_driver *drv;
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001477
Dmitry Baryshkov46e85af2020-12-13 02:55:33 +03001478 if (!_dev->driver)
1479 return;
1480
1481 drv = to_platform_driver(_dev->driver);
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001482 if (drv->shutdown)
1483 drv->shutdown(dev);
1484}
1485
1486
Nipun Gupta07397df2018-04-28 08:21:58 +05301487int platform_dma_configure(struct device *dev)
1488{
1489 enum dev_dma_attr attr;
1490 int ret = 0;
1491
1492 if (dev->of_node) {
Christoph Hellwig3d6ce862018-05-03 16:25:08 +02001493 ret = of_dma_configure(dev, dev->of_node, true);
Nipun Gupta07397df2018-04-28 08:21:58 +05301494 } else if (has_acpi_companion(dev)) {
1495 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
Robin Murphye5361ca2018-12-06 13:20:49 -08001496 ret = acpi_dma_configure(dev, attr);
Nipun Gupta07397df2018-04-28 08:21:58 +05301497 }
1498
1499 return ret;
1500}
1501
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +02001502static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysocki8b313a32011-04-29 00:36:32 +02001503 .runtime_suspend = pm_generic_runtime_suspend,
1504 .runtime_resume = pm_generic_runtime_resume,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001505 USE_PLATFORM_PM_SLEEP_OPS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001506};
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508struct bus_type platform_bus_type = {
1509 .name = "platform",
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001510 .dev_groups = platform_dev_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -07001512 .uevent = platform_uevent,
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001513 .probe = platform_probe,
1514 .remove = platform_remove,
1515 .shutdown = platform_shutdown,
Nipun Gupta07397df2018-04-28 08:21:58 +05301516 .dma_configure = platform_dma_configure,
Magnus Damm9d730222009-08-20 20:25:32 +02001517 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518};
Dmitry Torokhova96b2042005-12-10 01:36:28 -05001519EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Sami Tolvanen492c8872019-11-12 13:41:56 -08001521static inline int __platform_match(struct device *dev, const void *drv)
1522{
1523 return platform_match(dev, (struct device_driver *)drv);
1524}
1525
Suzuki K Poulose36f33132019-07-23 23:18:38 +01001526/**
1527 * platform_find_device_by_driver - Find a platform device with a given
1528 * driver.
1529 * @start: The device to start the search from.
1530 * @drv: The device driver to look for.
1531 */
1532struct device *platform_find_device_by_driver(struct device *start,
1533 const struct device_driver *drv)
1534{
1535 return bus_find_device(&platform_bus_type, start, drv,
Sami Tolvanen492c8872019-11-12 13:41:56 -08001536 __platform_match);
Suzuki K Poulose36f33132019-07-23 23:18:38 +01001537}
1538EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1539
Guenter Roeckeecd37e2019-12-03 12:58:52 -08001540void __weak __init early_platform_cleanup(void) { }
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542int __init platform_bus_init(void)
1543{
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001544 int error;
1545
Guenter Roeckeecd37e2019-12-03 12:58:52 -08001546 early_platform_cleanup();
1547
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001548 error = device_register(&platform_bus);
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301549 if (error) {
1550 put_device(&platform_bus);
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001551 return error;
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301552 }
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001553 error = bus_register(&platform_bus_type);
1554 if (error)
1555 device_unregister(&platform_bus);
Pantelis Antoniou801d7282014-10-28 22:36:01 +02001556 of_platform_register_reconfig_notifier();
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001557 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}