blob: 9cd34def2237b8a27eb827863826dbe51cca6709 [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)
Andy Shevchenkoc99f4eb2021-03-31 17:59:36 +0300195 goto out_not_found;
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);
Andy Shevchenkoc99f4eb2021-03-31 17:59:36 +0300226 if (!irqd)
227 goto out_not_found;
Guenter Roeck60ca5e02016-09-13 20:32:44 -0700228 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500231 if (r) {
232 ret = r->start;
233 goto out;
234 }
Enrico Granatadaaef252019-02-11 11:01:12 -0800235
236 /*
237 * For the index 0 interrupt, allow falling back to GpioInt
238 * resources. While a device could have both Interrupt and GpioInt
239 * resources, making this fallback ambiguous, in many common cases
240 * the device will only expose one IRQ, and this fallback
241 * allows a common code path across either kind of resource.
242 */
Brian Norris46c42d82019-07-29 13:49:54 -0700243 if (num == 0 && has_acpi_companion(&dev->dev)) {
Andy Shevchenko71564a22019-10-23 15:25:05 +0300244 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
Brian Norris46c42d82019-07-29 13:49:54 -0700245 /* Our callers expect -ENXIO for missing IRQs. */
246 if (ret >= 0 || ret == -EPROBE_DEFER)
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500247 goto out;
Brian Norris46c42d82019-07-29 13:49:54 -0700248 }
Enrico Granatadaaef252019-02-11 11:01:12 -0800249
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +0000250#endif
Andy Shevchenkoc99f4eb2021-03-31 17:59:36 +0300251out_not_found:
252 ret = -ENXIO;
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500253out:
254 WARN(ret == 0, "0 is an invalid IRQ number\n");
255 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200257EXPORT_SYMBOL_GPL(platform_get_irq_optional);
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700258
259/**
260 * platform_get_irq - get an IRQ for a device
261 * @dev: platform device
262 * @num: IRQ number index
263 *
264 * Gets an IRQ for a platform device and prints an error message if finding the
265 * IRQ fails. Device drivers should check the return value for errors so as to
266 * not pass a negative integer value to the request_irq() APIs.
267 *
Mauro Carvalho Chehabf0825242020-04-14 18:48:45 +0200268 * For example::
269 *
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700270 * int irq = platform_get_irq(pdev, 0);
271 * if (irq < 0)
272 * return irq;
273 *
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500274 * Return: non-zero IRQ number on success, negative error number on failure.
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700275 */
276int platform_get_irq(struct platform_device *dev, unsigned int num)
277{
278 int ret;
279
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200280 ret = platform_get_irq_optional(dev, num);
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700281 if (ret < 0 && ret != -EPROBE_DEFER)
282 dev_err(&dev->dev, "IRQ index %u not found\n", num);
283
284 return ret;
285}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500286EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/**
Stephen Boyd4b835552016-01-06 17:12:47 -0800289 * platform_irq_count - Count the number of IRQs a platform device uses
290 * @dev: platform device
291 *
292 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
293 */
294int platform_irq_count(struct platform_device *dev)
295{
296 int ret, nr = 0;
297
Uwe Kleine-Königec4e2902019-10-09 11:37:46 +0200298 while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
Stephen Boyd4b835552016-01-06 17:12:47 -0800299 nr++;
300
301 if (ret == -EPROBE_DEFER)
302 return ret;
303
304 return nr;
305}
306EXPORT_SYMBOL_GPL(platform_irq_count);
307
John Garrye15f2fa2020-12-02 18:36:56 +0800308struct irq_affinity_devres {
309 unsigned int count;
310 unsigned int irq[];
311};
312
313static void platform_disable_acpi_irq(struct platform_device *pdev, int index)
314{
315 struct resource *r;
316
317 r = platform_get_resource(pdev, IORESOURCE_IRQ, index);
318 if (r)
319 irqresource_disabled(r, 0);
320}
321
322static void devm_platform_get_irqs_affinity_release(struct device *dev,
323 void *res)
324{
325 struct irq_affinity_devres *ptr = res;
326 int i;
327
328 for (i = 0; i < ptr->count; i++) {
329 irq_dispose_mapping(ptr->irq[i]);
330
331 if (has_acpi_companion(dev))
332 platform_disable_acpi_irq(to_platform_device(dev), i);
333 }
334}
335
336/**
337 * devm_platform_get_irqs_affinity - devm method to get a set of IRQs for a
338 * device using an interrupt affinity descriptor
339 * @dev: platform device pointer
340 * @affd: affinity descriptor
341 * @minvec: minimum count of interrupt vectors
342 * @maxvec: maximum count of interrupt vectors
343 * @irqs: pointer holder for IRQ numbers
344 *
345 * Gets a set of IRQs for a platform device, and updates IRQ afffinty according
346 * to the passed affinity descriptor
347 *
348 * Return: Number of vectors on success, negative error number on failure.
349 */
350int devm_platform_get_irqs_affinity(struct platform_device *dev,
351 struct irq_affinity *affd,
352 unsigned int minvec,
353 unsigned int maxvec,
354 int **irqs)
355{
356 struct irq_affinity_devres *ptr;
357 struct irq_affinity_desc *desc;
358 size_t size;
359 int i, ret, nvec;
360
361 if (!affd)
362 return -EPERM;
363
364 if (maxvec < minvec)
365 return -ERANGE;
366
367 nvec = platform_irq_count(dev);
John Garrye1dc2092020-12-21 22:30:55 +0800368 if (nvec < 0)
369 return nvec;
John Garrye15f2fa2020-12-02 18:36:56 +0800370
371 if (nvec < minvec)
372 return -ENOSPC;
373
374 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
375 if (nvec < minvec)
376 return -ENOSPC;
377
378 if (nvec > maxvec)
379 nvec = maxvec;
380
381 size = sizeof(*ptr) + sizeof(unsigned int) * nvec;
382 ptr = devres_alloc(devm_platform_get_irqs_affinity_release, size,
383 GFP_KERNEL);
384 if (!ptr)
385 return -ENOMEM;
386
387 ptr->count = nvec;
388
389 for (i = 0; i < nvec; i++) {
390 int irq = platform_get_irq(dev, i);
391 if (irq < 0) {
392 ret = irq;
393 goto err_free_devres;
394 }
395 ptr->irq[i] = irq;
396 }
397
398 desc = irq_create_affinity_masks(nvec, affd);
399 if (!desc) {
400 ret = -ENOMEM;
401 goto err_free_devres;
402 }
403
404 for (i = 0; i < nvec; i++) {
405 ret = irq_update_affinity_desc(ptr->irq[i], &desc[i]);
406 if (ret) {
407 dev_err(&dev->dev, "failed to update irq%d affinity descriptor (%d)\n",
408 ptr->irq[i], ret);
409 goto err_free_desc;
410 }
411 }
412
413 devres_add(&dev->dev, ptr);
414
415 kfree(desc);
416
417 *irqs = ptr->irq;
418
419 return nvec;
420
421err_free_desc:
422 kfree(desc);
423err_free_devres:
424 devres_free(ptr);
425 return ret;
426}
427EXPORT_SYMBOL_GPL(devm_platform_get_irqs_affinity);
428
Stephen Boyd4b835552016-01-06 17:12:47 -0800429/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800430 * platform_get_resource_byname - get a resource for a device by name
431 * @dev: platform device
432 * @type: resource type
433 * @name: resource name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800435struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200436 unsigned int type,
437 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Simon Schwartz39cc5392019-12-10 17:41:37 -0500439 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 for (i = 0; i < dev->num_resources; i++) {
442 struct resource *r = &dev->resource[i];
443
Peter Ujfalusi1b8cb922012-08-23 17:10:00 +0300444 if (unlikely(!r->name))
445 continue;
446
Magnus Dammc9f66162008-10-15 22:05:15 -0700447 if (type == resource_type(r) && !strcmp(r->name, name))
448 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450 return NULL;
451}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500452EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Hans de Goedef1da5672019-10-05 23:04:47 +0200454static int __platform_get_irq_byname(struct platform_device *dev,
455 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Grygorii Strashkoad696742014-05-20 13:42:02 +0300457 struct resource *r;
Andy Shevchenko71564a22019-10-23 15:25:05 +0300458 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Guenter Roeckaff008a2014-06-17 15:51:02 -0700460 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
Guenter Roeckaff008a2014-06-17 15:51:02 -0700461 ret = of_irq_get_byname(dev->dev.of_node, name);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +0300462 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -0700463 return ret;
464 }
Grygorii Strashkoad696742014-05-20 13:42:02 +0300465
466 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500467 if (r) {
468 WARN(r->start == 0, "0 is an invalid IRQ number\n");
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700469 return r->start;
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500470 }
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700471
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700472 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
Hans de Goedef1da5672019-10-05 23:04:47 +0200474
475/**
476 * platform_get_irq_byname - get an IRQ for a device by name
477 * @dev: platform device
478 * @name: IRQ name
479 *
480 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
481 *
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500482 * Return: non-zero IRQ number on success, negative error number on failure.
Hans de Goedef1da5672019-10-05 23:04:47 +0200483 */
484int platform_get_irq_byname(struct platform_device *dev, const char *name)
485{
486 int ret;
487
488 ret = __platform_get_irq_byname(dev, name);
489 if (ret < 0 && ret != -EPROBE_DEFER)
490 dev_err(&dev->dev, "IRQ %s not found\n", name);
491
492 return ret;
493}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500494EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496/**
Hans de Goedef1da5672019-10-05 23:04:47 +0200497 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
498 * @dev: platform device
499 * @name: IRQ name
500 *
501 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
502 * does not print an error message if an IRQ can not be obtained.
503 *
Bjorn Helgaasa85a6c82020-03-16 16:43:38 -0500504 * Return: non-zero IRQ number on success, negative error number on failure.
Hans de Goedef1da5672019-10-05 23:04:47 +0200505 */
506int platform_get_irq_byname_optional(struct platform_device *dev,
507 const char *name)
508{
509 return __platform_get_irq_byname(dev, name);
510}
511EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
512
513/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800514 * platform_add_devices - add a numbers of platform devices
515 * @devs: array of platform devices to add
516 * @num: number of platform devices in array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
518int platform_add_devices(struct platform_device **devs, int num)
519{
520 int i, ret = 0;
521
522 for (i = 0; i < num; i++) {
523 ret = platform_device_register(devs[i]);
524 if (ret) {
525 while (--i >= 0)
526 platform_device_unregister(devs[i]);
527 break;
528 }
529 }
530
531 return ret;
532}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500533EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Russell King37c12e72005-11-05 21:19:33 +0000535struct platform_object {
536 struct platform_device pdev;
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200537 char name[];
Russell King37c12e72005-11-05 21:19:33 +0000538};
539
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200540/*
541 * Set up default DMA mask for platform devices if the they weren't
542 * previously set by the architecture / DT.
543 */
544static void setup_pdev_dma_masks(struct platform_device *pdev)
545{
Ulf Hansson9495b7e2020-04-22 12:09:54 +0200546 pdev->dev.dma_parms = &pdev->dma_parms;
547
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200548 if (!pdev->dev.coherent_dma_mask)
549 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Christoph Hellwige3a36eb2020-03-11 17:07:10 +0100550 if (!pdev->dev.dma_mask) {
551 pdev->platform_dma_mask = DMA_BIT_MASK(32);
552 pdev->dev.dma_mask = &pdev->platform_dma_mask;
553 }
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200554};
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000557 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800558 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000559 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800560 * Free all memory associated with a platform device. This function must
561 * _only_ be externally called in error cases. All other usage is a bug.
Russell King37c12e72005-11-05 21:19:33 +0000562 */
563void platform_device_put(struct platform_device *pdev)
564{
Andy Shevchenko99fef582018-12-03 20:21:41 +0200565 if (!IS_ERR_OR_NULL(pdev))
Russell King37c12e72005-11-05 21:19:33 +0000566 put_device(&pdev->dev);
567}
568EXPORT_SYMBOL_GPL(platform_device_put);
569
570static void platform_device_release(struct device *dev)
571{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800572 struct platform_object *pa = container_of(dev, struct platform_object,
573 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000574
Rob Herringcb8be8b2021-02-11 17:27:45 -0600575 of_node_put(pa->pdev.dev.of_node);
Russell King37c12e72005-11-05 21:19:33 +0000576 kfree(pa->pdev.dev.platform_data);
Samuel Ortize710d7d2011-04-08 00:43:01 +0200577 kfree(pa->pdev.mfd_cell);
Russell King37c12e72005-11-05 21:19:33 +0000578 kfree(pa->pdev.resource);
Kim Phillips3d713e02014-06-02 19:42:58 -0500579 kfree(pa->pdev.driver_override);
Russell King37c12e72005-11-05 21:19:33 +0000580 kfree(pa);
581}
582
583/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000584 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800585 * @name: base name of the device we're adding
586 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000587 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800588 * Create a platform device object which can have other objects attached
589 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000590 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200591struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000592{
593 struct platform_object *pa;
594
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200595 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000596 if (pa) {
597 strcpy(pa->name, name);
598 pa->pdev.name = pa->name;
599 pa->pdev.id = id;
600 device_initialize(&pa->pdev.dev);
601 pa->pdev.dev.release = platform_device_release;
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200602 setup_pdev_dma_masks(&pa->pdev);
Russell King37c12e72005-11-05 21:19:33 +0000603 }
604
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500605 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000606}
607EXPORT_SYMBOL_GPL(platform_device_alloc);
608
609/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000610 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800611 * @pdev: platform device allocated by platform_device_alloc to add resources to
612 * @res: set of resources that needs to be allocated for the device
613 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000614 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800615 * Add a copy of the resources to the platform device. The memory
616 * associated with the resources will be freed when the platform device is
617 * released.
Russell King37c12e72005-11-05 21:19:33 +0000618 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800619int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100620 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000621{
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200622 struct resource *r = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000623
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200624 if (res) {
625 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
626 if (!r)
627 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000628 }
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200629
Uwe Kleine-König4a03d6f2011-04-20 09:44:45 +0200630 kfree(pdev->resource);
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200631 pdev->resource = r;
632 pdev->num_resources = num;
633 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000634}
635EXPORT_SYMBOL_GPL(platform_device_add_resources);
636
637/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000638 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800639 * @pdev: platform device allocated by platform_device_alloc to add resources to
640 * @data: platform specific data for this platform device
641 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000642 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800643 * Add a copy of platform specific data to the platform device's
644 * platform_data pointer. The memory associated with the platform data
645 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000646 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800647int platform_device_add_data(struct platform_device *pdev, const void *data,
648 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000649{
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200650 void *d = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000651
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200652 if (data) {
653 d = kmemdup(data, size, GFP_KERNEL);
654 if (!d)
655 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000656 }
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200657
Uwe Kleine-König251e0312011-04-20 09:44:43 +0200658 kfree(pdev->dev.platform_data);
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200659 pdev->dev.platform_data = d;
660 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000661}
662EXPORT_SYMBOL_GPL(platform_device_add_data);
663
664/**
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200665 * platform_device_add_properties - add built-in properties to a platform device
666 * @pdev: platform device to add properties to
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300667 * @properties: null terminated array of properties to add
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200668 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300669 * The function will take deep copy of @properties and attach the copy to the
670 * platform device. The memory associated with properties will be freed when the
671 * platform device is released.
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200672 */
673int platform_device_add_properties(struct platform_device *pdev,
Jan Kiszka277036f2017-06-02 07:43:27 +0200674 const struct property_entry *properties)
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200675{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300676 return device_add_properties(&pdev->dev, properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200677}
678EXPORT_SYMBOL_GPL(platform_device_add_properties);
679
680/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800681 * platform_device_add - add a platform device to device hierarchy
682 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800684 * This is part 2 of platform_device_register(), though may be called
685 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
Russell King37c12e72005-11-05 21:19:33 +0000687int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Simon Schwartz39cc5392019-12-10 17:41:37 -0500689 u32 i;
690 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (!pdev)
693 return -EINVAL;
694
695 if (!pdev->dev.parent)
696 pdev->dev.parent = &platform_bus;
697
698 pdev->dev.bus = &platform_bus_type;
699
Jean Delvare689ae232012-07-27 22:14:59 +0200700 switch (pdev->id) {
701 default:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100702 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Jean Delvare689ae232012-07-27 22:14:59 +0200703 break;
704 case PLATFORM_DEVID_NONE:
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700705 dev_set_name(&pdev->dev, "%s", pdev->name);
Jean Delvare689ae232012-07-27 22:14:59 +0200706 break;
707 case PLATFORM_DEVID_AUTO:
708 /*
709 * Automatically allocated device ID. We mark it as such so
710 * that we remember it must be freed, and we append a suffix
711 * to avoid namespace collision with explicit IDs.
712 */
Bartosz Golaszewski0de75112020-09-09 20:02:48 +0200713 ret = ida_alloc(&platform_devid_ida, GFP_KERNEL);
Jean Delvare689ae232012-07-27 22:14:59 +0200714 if (ret < 0)
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700715 goto err_out;
Jean Delvare689ae232012-07-27 22:14:59 +0200716 pdev->id = ret;
717 pdev->id_auto = true;
718 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
719 break;
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 for (i = 0; i < pdev->num_resources; i++) {
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700723 struct resource *p, *r = &pdev->resource[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100726 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 p = r->parent;
729 if (!p) {
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700730 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 p = &iomem_resource;
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700732 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 p = &ioport_resource;
734 }
735
Andy Shevchenko25ebcb72019-04-04 11:11:58 +0300736 if (p) {
737 ret = insert_resource(p, r);
738 if (ret) {
739 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
740 goto failed;
741 }
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
745 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100746 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Russell Kinge3915532006-05-06 08:15:26 +0100748 ret = device_add(&pdev->dev);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700749 if (ret == 0)
750 return ret;
751
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700752 failed:
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700753 if (pdev->id_auto) {
Bartosz Golaszewski0de75112020-09-09 20:02:48 +0200754 ida_free(&platform_devid_ida, pdev->id);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700755 pdev->id = PLATFORM_DEVID_AUTO;
756 }
757
Colin Ian King0707cfa2020-01-16 17:57:58 +0000758 while (i--) {
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700759 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100760 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700761 release_resource(r);
762 }
Magnus Dammc9f66162008-10-15 22:05:15 -0700763
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700764 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return ret;
766}
Russell King37c12e72005-11-05 21:19:33 +0000767EXPORT_SYMBOL_GPL(platform_device_add);
768
769/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800770 * platform_device_del - remove a platform-level device
771 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500772 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800773 * Note that this function will also release all memory- and port-based
774 * resources owned by the device (@dev->resource). This function must
775 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500776 */
777void platform_device_del(struct platform_device *pdev)
778{
Simon Schwartz39cc5392019-12-10 17:41:37 -0500779 u32 i;
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500780
Andy Shevchenko99fef582018-12-03 20:21:41 +0200781 if (!IS_ERR_OR_NULL(pdev)) {
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700782 device_del(&pdev->dev);
783
784 if (pdev->id_auto) {
Bartosz Golaszewski0de75112020-09-09 20:02:48 +0200785 ida_free(&platform_devid_ida, pdev->id);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700786 pdev->id = PLATFORM_DEVID_AUTO;
787 }
788
789 for (i = 0; i < pdev->num_resources; i++) {
790 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100791 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700792 release_resource(r);
793 }
794 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500795}
796EXPORT_SYMBOL_GPL(platform_device_del);
797
798/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800799 * platform_device_register - add a platform-level device
800 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000801 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800802int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000803{
804 device_initialize(&pdev->dev);
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200805 setup_pdev_dma_masks(pdev);
Russell King37c12e72005-11-05 21:19:33 +0000806 return platform_device_add(pdev);
807}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500808EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800811 * platform_device_unregister - unregister a platform-level device
812 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800814 * Unregistration is done in 2 steps. First we release all resources
815 * and remove it from the subsystem, then we drop reference count by
816 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800818void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500820 platform_device_del(pdev);
821 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500823EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/**
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200826 * platform_device_register_full - add a platform-level device with
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200827 * resources and platform-specific data
828 *
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200829 * @pdevinfo: data used to create device
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700830 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200831 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700832 */
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200833struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +0100834 const struct platform_device_info *pdevinfo)
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700835{
Colin Ian King45bb08d2020-04-02 12:13:41 +0100836 int ret;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700837 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700838
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200839 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200840 if (!pdev)
Johannes Berg36cf3b12019-03-01 13:24:47 +0100841 return ERR_PTR(-ENOMEM);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700842
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200843 pdev->dev.parent = pdevinfo->parent;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100844 pdev->dev.fwnode = pdevinfo->fwnode;
Mans Rullgard2c1ea6a2019-02-21 11:29:35 +0000845 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
846 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700847
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200848 if (pdevinfo->dma_mask) {
Christoph Hellwige3a36eb2020-03-11 17:07:10 +0100849 pdev->platform_dma_mask = pdevinfo->dma_mask;
850 pdev->dev.dma_mask = &pdev->platform_dma_mask;
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200851 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
852 }
853
854 ret = platform_device_add_resources(pdev,
855 pdevinfo->res, pdevinfo->num_res);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400856 if (ret)
857 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700858
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200859 ret = platform_device_add_data(pdev,
860 pdevinfo->data, pdevinfo->size_data);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400861 if (ret)
862 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200863
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300864 if (pdevinfo->properties) {
865 ret = platform_device_add_properties(pdev,
866 pdevinfo->properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200867 if (ret)
868 goto err;
869 }
870
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200871 ret = platform_device_add(pdev);
872 if (ret) {
873err:
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100874 ACPI_COMPANION_SET(&pdev->dev, NULL);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200875 platform_device_put(pdev);
876 return ERR_PTR(ret);
877 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700878
879 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700880}
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200881EXPORT_SYMBOL_GPL(platform_device_register_full);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700882
Russell King00d3dcd2005-11-09 17:23:39 +0000883/**
Libo Chen94470572013-05-25 12:40:50 +0800884 * __platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800885 * @drv: platform driver structure
Randy Dunlap08801f92013-07-14 17:43:06 -0700886 * @owner: owning module/driver
Russell King00d3dcd2005-11-09 17:23:39 +0000887 */
Libo Chen94470572013-05-25 12:40:50 +0800888int __platform_driver_register(struct platform_driver *drv,
889 struct module *owner)
Russell King00d3dcd2005-11-09 17:23:39 +0000890{
Libo Chen94470572013-05-25 12:40:50 +0800891 drv->driver.owner = owner;
Russell King00d3dcd2005-11-09 17:23:39 +0000892 drv->driver.bus = &platform_bus_type;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200893
Russell King00d3dcd2005-11-09 17:23:39 +0000894 return driver_register(&drv->driver);
895}
Libo Chen94470572013-05-25 12:40:50 +0800896EXPORT_SYMBOL_GPL(__platform_driver_register);
Russell King00d3dcd2005-11-09 17:23:39 +0000897
898/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000899 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800900 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000901 */
902void platform_driver_unregister(struct platform_driver *drv)
903{
904 driver_unregister(&drv->driver);
905}
906EXPORT_SYMBOL_GPL(platform_driver_unregister);
907
Uwe Kleine-König16085662020-11-19 13:46:10 +0100908static int platform_probe_fail(struct platform_device *pdev)
Uwe Kleine-Könige21d740a2020-11-19 13:46:09 +0100909{
910 return -ENXIO;
911}
912
David Brownellc67334f2006-11-16 23:28:47 -0800913/**
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100914 * __platform_driver_probe - register driver for non-hotpluggable device
David Brownellc67334f2006-11-16 23:28:47 -0800915 * @drv: platform driver structure
Johan Hovold3f9120b2013-09-23 16:27:26 +0200916 * @probe: the driver probe routine, probably from an __init section
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100917 * @module: module which will be the owner of the driver
David Brownellc67334f2006-11-16 23:28:47 -0800918 *
919 * Use this instead of platform_driver_register() when you know the device
920 * is not hotpluggable and has already been registered, and you want to
921 * remove its run-once probe() infrastructure from memory after the driver
922 * has bound to the device.
923 *
924 * One typical use for this would be with drivers for controllers integrated
925 * into system-on-chip processors, where the controller devices have been
926 * configured as part of board setup.
927 *
Johan Hovold3f9120b2013-09-23 16:27:26 +0200928 * Note that this is incompatible with deferred probing.
Fabio Porcedda647c86d2013-03-26 10:35:15 +0100929 *
David Brownellc67334f2006-11-16 23:28:47 -0800930 * Returns zero if the driver registered and bound to a device, else returns
931 * a negative error code and with the driver not registered.
932 */
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100933int __init_or_module __platform_driver_probe(struct platform_driver *drv,
934 int (*probe)(struct platform_device *), struct module *module)
David Brownellc67334f2006-11-16 23:28:47 -0800935{
936 int retval, code;
937
Dmitry Torokhov5c36eb22015-03-30 16:20:07 -0700938 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
939 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
940 drv->driver.name, __func__);
941 return -EINVAL;
942 }
943
944 /*
945 * We have to run our probes synchronously because we check if
946 * we find any devices to bind to and exit with error if there
947 * are any.
948 */
949 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
950
Johan Hovold3f9120b2013-09-23 16:27:26 +0200951 /*
952 * Prevent driver from requesting probe deferral to avoid further
953 * futile probe attempts.
954 */
955 drv->prevent_deferred_probe = true;
956
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700957 /* make sure driver won't have bind/unbind attributes */
958 drv->driver.suppress_bind_attrs = true;
959
David Brownellc67334f2006-11-16 23:28:47 -0800960 /* temporary section violation during probe() */
961 drv->probe = probe;
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100962 retval = code = __platform_driver_register(drv, module);
Kuppuswamy Sathyanarayanan388bcc62020-04-08 14:40:03 -0700963 if (retval)
964 return retval;
David Brownellc67334f2006-11-16 23:28:47 -0800965
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700966 /*
967 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800968 * the list of drivers in order to probe new devices. Check to see
969 * if the probe was successful, and make sure any forced probes of
970 * new devices fail.
971 */
Patrick Pannutod79d3242010-08-06 17:12:41 -0700972 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
Uwe Kleine-König16085662020-11-19 13:46:10 +0100973 drv->probe = platform_probe_fail;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800974 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800975 retval = -ENODEV;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700976 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800977
978 if (code != retval)
979 platform_driver_unregister(drv);
980 return retval;
981}
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100982EXPORT_SYMBOL_GPL(__platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800984/**
Wolfram Sang291f6532014-10-28 17:40:42 +0100985 * __platform_create_bundle - register driver and create corresponding device
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800986 * @driver: platform driver structure
987 * @probe: the driver probe routine, probably from an __init section
988 * @res: set of resources that needs to be allocated for the device
989 * @n_res: number of resources
990 * @data: platform specific data for this platform device
991 * @size: size of platform specific data
Wolfram Sang291f6532014-10-28 17:40:42 +0100992 * @module: module which will be the owner of the driver
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800993 *
994 * Use this in legacy-style modules that probe hardware directly and
995 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200996 *
997 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800998 */
Wolfram Sang291f6532014-10-28 17:40:42 +0100999struct platform_device * __init_or_module __platform_create_bundle(
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -08001000 struct platform_driver *driver,
1001 int (*probe)(struct platform_device *),
1002 struct resource *res, unsigned int n_res,
Wolfram Sang291f6532014-10-28 17:40:42 +01001003 const void *data, size_t size, struct module *module)
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -08001004{
1005 struct platform_device *pdev;
1006 int error;
1007
1008 pdev = platform_device_alloc(driver->driver.name, -1);
1009 if (!pdev) {
1010 error = -ENOMEM;
1011 goto err_out;
1012 }
1013
Anton Vorontsov807508c2010-09-07 17:31:54 +04001014 error = platform_device_add_resources(pdev, res, n_res);
1015 if (error)
1016 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -08001017
Anton Vorontsov807508c2010-09-07 17:31:54 +04001018 error = platform_device_add_data(pdev, data, size);
1019 if (error)
1020 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -08001021
1022 error = platform_device_add(pdev);
1023 if (error)
1024 goto err_pdev_put;
1025
Wolfram Sang291f6532014-10-28 17:40:42 +01001026 error = __platform_driver_probe(driver, probe, module);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -08001027 if (error)
1028 goto err_pdev_del;
1029
1030 return pdev;
1031
1032err_pdev_del:
1033 platform_device_del(pdev);
1034err_pdev_put:
1035 platform_device_put(pdev);
1036err_out:
1037 return ERR_PTR(error);
1038}
Wolfram Sang291f6532014-10-28 17:40:42 +01001039EXPORT_SYMBOL_GPL(__platform_create_bundle);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -08001040
Thierry Redingdbe22562015-09-25 17:29:04 +02001041/**
1042 * __platform_register_drivers - register an array of platform drivers
1043 * @drivers: an array of drivers to register
1044 * @count: the number of drivers to register
1045 * @owner: module owning the drivers
1046 *
1047 * Registers platform drivers specified by an array. On failure to register a
1048 * driver, all previously registered drivers will be unregistered. Callers of
1049 * this API should use platform_unregister_drivers() to unregister drivers in
1050 * the reverse order.
1051 *
1052 * Returns: 0 on success or a negative error code on failure.
1053 */
1054int __platform_register_drivers(struct platform_driver * const *drivers,
1055 unsigned int count, struct module *owner)
1056{
1057 unsigned int i;
1058 int err;
1059
1060 for (i = 0; i < count; i++) {
1061 pr_debug("registering platform driver %ps\n", drivers[i]);
1062
1063 err = __platform_driver_register(drivers[i], owner);
1064 if (err < 0) {
1065 pr_err("failed to register platform driver %ps: %d\n",
1066 drivers[i], err);
1067 goto error;
1068 }
1069 }
1070
1071 return 0;
1072
1073error:
1074 while (i--) {
1075 pr_debug("unregistering platform driver %ps\n", drivers[i]);
1076 platform_driver_unregister(drivers[i]);
1077 }
1078
1079 return err;
1080}
1081EXPORT_SYMBOL_GPL(__platform_register_drivers);
1082
1083/**
1084 * platform_unregister_drivers - unregister an array of platform drivers
1085 * @drivers: an array of drivers to unregister
1086 * @count: the number of drivers to unregister
1087 *
Tang Binc82c83c2020-05-20 22:12:02 +08001088 * Unregisters platform drivers specified by an array. This is typically used
Thierry Redingdbe22562015-09-25 17:29:04 +02001089 * to complement an earlier call to platform_register_drivers(). Drivers are
1090 * unregistered in the reverse order in which they were registered.
1091 */
1092void platform_unregister_drivers(struct platform_driver * const *drivers,
1093 unsigned int count)
1094{
1095 while (count--) {
1096 pr_debug("unregistering platform driver %ps\n", drivers[count]);
1097 platform_driver_unregister(drivers[count]);
1098 }
1099}
1100EXPORT_SYMBOL_GPL(platform_unregister_drivers);
1101
Eric Miao57fee4a2009-02-04 11:52:40 +08001102static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +01001103 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +08001104 struct platform_device *pdev)
1105{
1106 while (id->name[0]) {
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001107 if (strcmp(pdev->name, id->name) == 0) {
Eric Miao57fee4a2009-02-04 11:52:40 +08001108 pdev->id_entry = id;
1109 return id;
1110 }
1111 id++;
1112 }
1113 return NULL;
1114}
1115
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001116#ifdef CONFIG_PM_SLEEP
1117
1118static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001120 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1121 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 int ret = 0;
1123
Magnus Damm783ea7d2009-06-04 22:13:33 +02001124 if (dev->driver && pdrv->suspend)
1125 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d82006-09-03 13:16:45 -07001126
1127 return ret;
1128}
1129
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001130static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001132 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1133 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 int ret = 0;
1135
Magnus Damm783ea7d2009-06-04 22:13:33 +02001136 if (dev->driver && pdrv->resume)
1137 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -07001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return ret;
1140}
1141
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001142#endif /* CONFIG_PM_SLEEP */
Magnus Damm9d730222009-08-20 20:25:32 +02001143
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001144#ifdef CONFIG_SUSPEND
1145
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001146int platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001147{
1148 struct device_driver *drv = dev->driver;
1149 int ret = 0;
1150
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001151 if (!drv)
1152 return 0;
1153
1154 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001155 if (drv->pm->suspend)
1156 ret = drv->pm->suspend(dev);
1157 } else {
1158 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1159 }
1160
1161 return ret;
1162}
1163
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001164int platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001165{
1166 struct device_driver *drv = dev->driver;
1167 int ret = 0;
1168
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001169 if (!drv)
1170 return 0;
1171
1172 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001173 if (drv->pm->resume)
1174 ret = drv->pm->resume(dev);
1175 } else {
1176 ret = platform_legacy_resume(dev);
1177 }
1178
1179 return ret;
1180}
1181
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001182#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001183
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +02001184#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001185
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001186int platform_pm_freeze(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001187{
1188 struct device_driver *drv = dev->driver;
1189 int ret = 0;
1190
1191 if (!drv)
1192 return 0;
1193
1194 if (drv->pm) {
1195 if (drv->pm->freeze)
1196 ret = drv->pm->freeze(dev);
1197 } else {
1198 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1199 }
1200
1201 return ret;
1202}
1203
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001204int platform_pm_thaw(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001205{
1206 struct device_driver *drv = dev->driver;
1207 int ret = 0;
1208
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001209 if (!drv)
1210 return 0;
1211
1212 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001213 if (drv->pm->thaw)
1214 ret = drv->pm->thaw(dev);
1215 } else {
1216 ret = platform_legacy_resume(dev);
1217 }
1218
1219 return ret;
1220}
1221
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001222int platform_pm_poweroff(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001223{
1224 struct device_driver *drv = dev->driver;
1225 int ret = 0;
1226
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001227 if (!drv)
1228 return 0;
1229
1230 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001231 if (drv->pm->poweroff)
1232 ret = drv->pm->poweroff(dev);
1233 } else {
1234 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1235 }
1236
1237 return ret;
1238}
1239
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001240int platform_pm_restore(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001241{
1242 struct device_driver *drv = dev->driver;
1243 int ret = 0;
1244
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001245 if (!drv)
1246 return 0;
1247
1248 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001249 if (drv->pm->restore)
1250 ret = drv->pm->restore(dev);
1251 } else {
1252 ret = platform_legacy_resume(dev);
1253 }
1254
1255 return ret;
1256}
1257
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001258#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001259
Uwe Kleine-Könige21d740a2020-11-19 13:46:09 +01001260/* modalias support enables more hands-off userspace setup:
1261 * (a) environment variable lets new-style hotplug events work once system is
1262 * fully running: "modprobe $MODALIAS"
1263 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
1264 * mishandled before system is fully running: "modprobe $(cat modalias)"
1265 */
1266static ssize_t modalias_show(struct device *dev,
1267 struct device_attribute *attr, char *buf)
1268{
1269 struct platform_device *pdev = to_platform_device(dev);
1270 int len;
1271
1272 len = of_device_modalias(dev, buf, PAGE_SIZE);
1273 if (len != -ENODEV)
1274 return len;
1275
1276 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
1277 if (len != -ENODEV)
1278 return len;
1279
1280 return sysfs_emit(buf, "platform:%s\n", pdev->name);
1281}
1282static DEVICE_ATTR_RO(modalias);
1283
1284static ssize_t numa_node_show(struct device *dev,
1285 struct device_attribute *attr, char *buf)
1286{
1287 return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1288}
1289static DEVICE_ATTR_RO(numa_node);
1290
1291static ssize_t driver_override_show(struct device *dev,
1292 struct device_attribute *attr, char *buf)
1293{
1294 struct platform_device *pdev = to_platform_device(dev);
1295 ssize_t len;
1296
1297 device_lock(dev);
1298 len = sysfs_emit(buf, "%s\n", pdev->driver_override);
1299 device_unlock(dev);
1300
1301 return len;
1302}
1303
1304static ssize_t driver_override_store(struct device *dev,
1305 struct device_attribute *attr,
1306 const char *buf, size_t count)
1307{
1308 struct platform_device *pdev = to_platform_device(dev);
1309 char *driver_override, *old, *cp;
1310
1311 /* We need to keep extra room for a newline */
1312 if (count >= (PAGE_SIZE - 1))
1313 return -EINVAL;
1314
1315 driver_override = kstrndup(buf, count, GFP_KERNEL);
1316 if (!driver_override)
1317 return -ENOMEM;
1318
1319 cp = strchr(driver_override, '\n');
1320 if (cp)
1321 *cp = '\0';
1322
1323 device_lock(dev);
1324 old = pdev->driver_override;
1325 if (strlen(driver_override)) {
1326 pdev->driver_override = driver_override;
1327 } else {
1328 kfree(driver_override);
1329 pdev->driver_override = NULL;
1330 }
1331 device_unlock(dev);
1332
1333 kfree(old);
1334
1335 return count;
1336}
1337static DEVICE_ATTR_RW(driver_override);
1338
1339static struct attribute *platform_dev_attrs[] = {
1340 &dev_attr_modalias.attr,
1341 &dev_attr_numa_node.attr,
1342 &dev_attr_driver_override.attr,
1343 NULL,
1344};
1345
1346static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a,
1347 int n)
1348{
1349 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1350
1351 if (a == &dev_attr_numa_node.attr &&
1352 dev_to_node(dev) == NUMA_NO_NODE)
1353 return 0;
1354
1355 return a->mode;
1356}
1357
1358static struct attribute_group platform_dev_group = {
1359 .attrs = platform_dev_attrs,
1360 .is_visible = platform_dev_attrs_visible,
1361};
1362__ATTRIBUTE_GROUPS(platform_dev);
1363
1364
1365/**
1366 * platform_match - bind platform device to platform driver.
1367 * @dev: device.
1368 * @drv: driver.
1369 *
1370 * Platform device IDs are assumed to be encoded like this:
1371 * "<name><instance>", where <name> is a short description of the type of
1372 * device, like "pci" or "floppy", and <instance> is the enumerated
1373 * instance of the device, like '0' or '42'. Driver IDs are simply
1374 * "<name>". So, extract the <name> from the platform_device structure,
1375 * and compare it against the name of the driver. Return whether they match
1376 * or not.
1377 */
1378static int platform_match(struct device *dev, struct device_driver *drv)
1379{
1380 struct platform_device *pdev = to_platform_device(dev);
1381 struct platform_driver *pdrv = to_platform_driver(drv);
1382
1383 /* When driver_override is set, only bind to the matching driver */
1384 if (pdev->driver_override)
1385 return !strcmp(pdev->driver_override, drv->name);
1386
1387 /* Attempt an OF style match first */
1388 if (of_driver_match_device(dev, drv))
1389 return 1;
1390
1391 /* Then try ACPI style match */
1392 if (acpi_driver_match_device(dev, drv))
1393 return 1;
1394
1395 /* Then try to match against the id table */
1396 if (pdrv->id_table)
1397 return platform_match_id(pdrv->id_table, pdev) != NULL;
1398
1399 /* fall-back to driver name match */
1400 return (strcmp(pdev->name, drv->name) == 0);
1401}
1402
1403static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1404{
1405 struct platform_device *pdev = to_platform_device(dev);
1406 int rc;
1407
1408 /* Some devices have extra OF data and an OF-style MODALIAS */
1409 rc = of_device_uevent_modalias(dev, env);
1410 if (rc != -ENODEV)
1411 return rc;
1412
1413 rc = acpi_device_uevent_modalias(dev, env);
1414 if (rc != -ENODEV)
1415 return rc;
1416
1417 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1418 pdev->name);
1419 return 0;
1420}
1421
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001422static int platform_probe(struct device *_dev)
1423{
1424 struct platform_driver *drv = to_platform_driver(_dev->driver);
1425 struct platform_device *dev = to_platform_device(_dev);
1426 int ret;
1427
1428 /*
1429 * A driver registered using platform_driver_probe() cannot be bound
1430 * again later because the probe function usually lives in __init code
1431 * and so is gone. For these drivers .probe is set to
1432 * platform_probe_fail in __platform_driver_probe(). Don't even prepare
1433 * clocks and PM domains for these to match the traditional behaviour.
1434 */
1435 if (unlikely(drv->probe == platform_probe_fail))
1436 return -ENXIO;
1437
1438 ret = of_clk_set_defaults(_dev->of_node, false);
1439 if (ret < 0)
1440 return ret;
1441
1442 ret = dev_pm_domain_attach(_dev, true);
1443 if (ret)
1444 goto out;
1445
1446 if (drv->probe) {
1447 ret = drv->probe(dev);
1448 if (ret)
1449 dev_pm_domain_detach(_dev, true);
1450 }
1451
1452out:
1453 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
1454 dev_warn(_dev, "probe deferral not supported\n");
1455 ret = -ENXIO;
1456 }
1457
1458 return ret;
1459}
1460
1461static int platform_remove(struct device *_dev)
1462{
1463 struct platform_driver *drv = to_platform_driver(_dev->driver);
1464 struct platform_device *dev = to_platform_device(_dev);
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001465
Uwe Kleine-Könige5e1c202021-02-07 22:15:37 +01001466 if (drv->remove) {
1467 int ret = drv->remove(dev);
1468
1469 if (ret)
1470 dev_warn(_dev, "remove callback returned a non-zero value. This will be ignored.\n");
1471 }
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001472 dev_pm_domain_detach(_dev, true);
1473
Uwe Kleine-Könige5e1c202021-02-07 22:15:37 +01001474 return 0;
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001475}
1476
1477static void platform_shutdown(struct device *_dev)
1478{
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001479 struct platform_device *dev = to_platform_device(_dev);
Dmitry Baryshkov46e85af2020-12-13 02:55:33 +03001480 struct platform_driver *drv;
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001481
Dmitry Baryshkov46e85af2020-12-13 02:55:33 +03001482 if (!_dev->driver)
1483 return;
1484
1485 drv = to_platform_driver(_dev->driver);
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001486 if (drv->shutdown)
1487 drv->shutdown(dev);
1488}
1489
1490
Nipun Gupta07397df2018-04-28 08:21:58 +05301491int platform_dma_configure(struct device *dev)
1492{
1493 enum dev_dma_attr attr;
1494 int ret = 0;
1495
1496 if (dev->of_node) {
Christoph Hellwig3d6ce862018-05-03 16:25:08 +02001497 ret = of_dma_configure(dev, dev->of_node, true);
Nipun Gupta07397df2018-04-28 08:21:58 +05301498 } else if (has_acpi_companion(dev)) {
1499 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
Robin Murphye5361ca2018-12-06 13:20:49 -08001500 ret = acpi_dma_configure(dev, attr);
Nipun Gupta07397df2018-04-28 08:21:58 +05301501 }
1502
1503 return ret;
1504}
1505
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +02001506static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysocki8b313a32011-04-29 00:36:32 +02001507 .runtime_suspend = pm_generic_runtime_suspend,
1508 .runtime_resume = pm_generic_runtime_resume,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001509 USE_PLATFORM_PM_SLEEP_OPS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001510};
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512struct bus_type platform_bus_type = {
1513 .name = "platform",
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001514 .dev_groups = platform_dev_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -07001516 .uevent = platform_uevent,
Uwe Kleine-König9c309212020-11-19 13:46:11 +01001517 .probe = platform_probe,
1518 .remove = platform_remove,
1519 .shutdown = platform_shutdown,
Nipun Gupta07397df2018-04-28 08:21:58 +05301520 .dma_configure = platform_dma_configure,
Magnus Damm9d730222009-08-20 20:25:32 +02001521 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522};
Dmitry Torokhova96b2042005-12-10 01:36:28 -05001523EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Sami Tolvanen492c8872019-11-12 13:41:56 -08001525static inline int __platform_match(struct device *dev, const void *drv)
1526{
1527 return platform_match(dev, (struct device_driver *)drv);
1528}
1529
Suzuki K Poulose36f33132019-07-23 23:18:38 +01001530/**
1531 * platform_find_device_by_driver - Find a platform device with a given
1532 * driver.
1533 * @start: The device to start the search from.
1534 * @drv: The device driver to look for.
1535 */
1536struct device *platform_find_device_by_driver(struct device *start,
1537 const struct device_driver *drv)
1538{
1539 return bus_find_device(&platform_bus_type, start, drv,
Sami Tolvanen492c8872019-11-12 13:41:56 -08001540 __platform_match);
Suzuki K Poulose36f33132019-07-23 23:18:38 +01001541}
1542EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1543
Guenter Roeckeecd37e2019-12-03 12:58:52 -08001544void __weak __init early_platform_cleanup(void) { }
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546int __init platform_bus_init(void)
1547{
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001548 int error;
1549
Guenter Roeckeecd37e2019-12-03 12:58:52 -08001550 early_platform_cleanup();
1551
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001552 error = device_register(&platform_bus);
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301553 if (error) {
1554 put_device(&platform_bus);
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001555 return error;
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301556 }
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001557 error = bus_register(&platform_bus_type);
1558 if (error)
1559 device_unregister(&platform_bus);
Pantelis Antoniou801d7282014-10-28 22:36:01 +02001560 of_platform_register_reconfig_notifier();
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001561 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}