of: property: Fix fw_devlink handling of interrupts/interrupts-extended
[linux-2.6-block.git] / drivers / base / platform.c
CommitLineData
989d42e8 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * platform.c - platform 'pseudo' bus for legacy devices
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 *
fe34c89d 8 * Please see Documentation/driver-api/driver-model/platform.rst for more
1da177e4
LT
9 * information.
10 */
11
daa41226 12#include <linux/string.h>
d052d1be 13#include <linux/platform_device.h>
05212157 14#include <linux/of_device.h>
9ec36caf 15#include <linux/of_irq.h>
1da177e4
LT
16#include <linux/module.h>
17#include <linux/init.h>
e15f2fa9
JG
18#include <linux/interrupt.h>
19#include <linux/ioport.h>
1da177e4 20#include <linux/dma-mapping.h>
57c8a661 21#include <linux/memblock.h>
1da177e4 22#include <linux/err.h>
4e57b681 23#include <linux/slab.h>
9d730229 24#include <linux/pm_runtime.h>
f48c767c 25#include <linux/pm_domain.h>
689ae231 26#include <linux/idr.h>
91e56878 27#include <linux/acpi.h>
86be408b 28#include <linux/clk/clk-conf.h>
3d713e0e 29#include <linux/limits.h>
00bbc1d8 30#include <linux/property.h>
967d3010 31#include <linux/kmemleak.h>
39cc539f 32#include <linux/types.h>
1da177e4 33
a1bdc7aa 34#include "base.h"
bed2b42d 35#include "power/power.h"
a1bdc7aa 36
689ae231
JD
37/* For automatically allocated device IDs */
38static DEFINE_IDA(platform_devid_ida);
39
1da177e4 40struct device platform_bus = {
1e0b2cf9 41 .init_name = "platform",
1da177e4 42};
a96b2042 43EXPORT_SYMBOL_GPL(platform_bus);
1da177e4
LT
44
45/**
4a3ad20c
GKH
46 * platform_get_resource - get a resource for a device
47 * @dev: platform device
48 * @type: resource type
49 * @num: resource index
0c7a6b91
SB
50 *
51 * Return: a pointer to the resource or NULL on failure.
1da177e4 52 */
4a3ad20c
GKH
53struct resource *platform_get_resource(struct platform_device *dev,
54 unsigned int type, unsigned int num)
1da177e4 55{
39cc539f 56 u32 i;
1da177e4
LT
57
58 for (i = 0; i < dev->num_resources; i++) {
59 struct resource *r = &dev->resource[i];
60
c9f66169
MD
61 if (type == resource_type(r) && num-- == 0)
62 return r;
1da177e4
LT
63 }
64 return NULL;
65}
a96b2042 66EXPORT_SYMBOL_GPL(platform_get_resource);
1da177e4 67
0aec2da4
AS
68struct 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
bb6243b4 83#ifdef CONFIG_HAS_IOMEM
890cc39a
DZ
84/**
85 * 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.
0c7a6b91
SB
92 *
93 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
94 * on failure.
890cc39a
DZ
95 */
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
7945f929
BG
109/**
110 * 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
7067c96e 114 * resource management
7945f929 115 * @index: resource index
0c7a6b91
SB
116 *
117 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
118 * on failure.
7945f929
BG
119 */
120void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
121 unsigned int index)
122{
fd78901c 123 return devm_platform_get_and_ioremap_resource(pdev, index, NULL);
7945f929
BG
124}
125EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
bb6243b4
BG
126
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
0c7a6b91
SB
134 *
135 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
136 * on failure.
bb6243b4
BG
137 */
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}
c9c8641d
BG
146
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
0c7a6b91
SB
155 *
156 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
157 * on failure.
c9c8641d
BG
158 */
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);
837ccda3 169#endif /* CONFIG_HAS_IOMEM */
7945f929 170
ec4e2906
UKK
171/**
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 *
f0825246
MCC
181 * For example::
182 *
ec4e2906
UKK
183 * int irq = platform_get_irq_optional(pdev, 0);
184 * if (irq < 0)
185 * return irq;
186 *
a85a6c86 187 * Return: non-zero IRQ number on success, negative error number on failure.
ec4e2906
UKK
188 */
189int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
1da177e4 190{
a85a6c86 191 int ret;
5cf8f7db
AL
192#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;
a85a6c86
BH
196 ret = dev->archdata.irqs[num];
197 goto out;
5cf8f7db 198#else
9ec36caf 199 struct resource *r;
aff008ad 200
71564a26 201 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
aff008ad 202 ret = of_irq_get(dev->dev.of_node, num);
e330b9a6 203 if (ret > 0 || ret == -EPROBE_DEFER)
a85a6c86 204 goto out;
aff008ad 205 }
9ec36caf
RH
206
207 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
d44fa3d4
AVF
208 if (has_acpi_companion(&dev->dev)) {
209 if (r && r->flags & IORESOURCE_DISABLED) {
d44fa3d4
AVF
210 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
211 if (ret)
a85a6c86 212 goto out;
d44fa3d4
AVF
213 }
214 }
215
7085a740
LW
216 /*
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 */
60ca5e0d
GR
222 if (r && r->flags & IORESOURCE_BITS) {
223 struct irq_data *irqd;
224
225 irqd = irq_get_irq_data(r->start);
a85a6c86
BH
226 if (!irqd) {
227 ret = -ENXIO;
228 goto out;
229 }
60ca5e0d
GR
230 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
231 }
1da177e4 232
a85a6c86
BH
233 if (r) {
234 ret = r->start;
235 goto out;
236 }
daaef255
EG
237
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 */
46c42d84 245 if (num == 0 && has_acpi_companion(&dev->dev)) {
71564a26 246 ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
46c42d84
BN
247 /* Our callers expect -ENXIO for missing IRQs. */
248 if (ret >= 0 || ret == -EPROBE_DEFER)
a85a6c86 249 goto out;
46c42d84 250 }
daaef255 251
a85a6c86 252 ret = -ENXIO;
5cf8f7db 253#endif
a85a6c86
BH
254out:
255 WARN(ret == 0, "0 is an invalid IRQ number\n");
256 return ret;
1da177e4 257}
ec4e2906 258EXPORT_SYMBOL_GPL(platform_get_irq_optional);
7723f4c5
SB
259
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 *
f0825246
MCC
269 * For example::
270 *
7723f4c5
SB
271 * int irq = platform_get_irq(pdev, 0);
272 * if (irq < 0)
273 * return irq;
274 *
a85a6c86 275 * Return: non-zero IRQ number on success, negative error number on failure.
7723f4c5
SB
276 */
277int platform_get_irq(struct platform_device *dev, unsigned int num)
278{
279 int ret;
280
ec4e2906 281 ret = platform_get_irq_optional(dev, num);
7723f4c5
SB
282 if (ret < 0 && ret != -EPROBE_DEFER)
283 dev_err(&dev->dev, "IRQ index %u not found\n", num);
284
285 return ret;
286}
a96b2042 287EXPORT_SYMBOL_GPL(platform_get_irq);
1da177e4 288
4b83555d
SB
289/**
290 * 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
ec4e2906 299 while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
4b83555d
SB
300 nr++;
301
302 if (ret == -EPROBE_DEFER)
303 return ret;
304
305 return nr;
306}
307EXPORT_SYMBOL_GPL(platform_irq_count);
308
e15f2fa9
JG
309struct 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);
e1dc2099
JG
369 if (nvec < 0)
370 return nvec;
e15f2fa9
JG
371
372 if (nvec < minvec)
373 return -ENOSPC;
374
375 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
376 if (nvec < minvec)
377 return -ENOSPC;
378
379 if (nvec > maxvec)
380 nvec = maxvec;
381
382 size = sizeof(*ptr) + sizeof(unsigned int) * nvec;
383 ptr = devres_alloc(devm_platform_get_irqs_affinity_release, size,
384 GFP_KERNEL);
385 if (!ptr)
386 return -ENOMEM;
387
388 ptr->count = nvec;
389
390 for (i = 0; i < nvec; i++) {
391 int irq = platform_get_irq(dev, i);
392 if (irq < 0) {
393 ret = irq;
394 goto err_free_devres;
395 }
396 ptr->irq[i] = irq;
397 }
398
399 desc = irq_create_affinity_masks(nvec, affd);
400 if (!desc) {
401 ret = -ENOMEM;
402 goto err_free_devres;
403 }
404
405 for (i = 0; i < nvec; i++) {
406 ret = irq_update_affinity_desc(ptr->irq[i], &desc[i]);
407 if (ret) {
408 dev_err(&dev->dev, "failed to update irq%d affinity descriptor (%d)\n",
409 ptr->irq[i], ret);
410 goto err_free_desc;
411 }
412 }
413
414 devres_add(&dev->dev, ptr);
415
416 kfree(desc);
417
418 *irqs = ptr->irq;
419
420 return nvec;
421
422err_free_desc:
423 kfree(desc);
424err_free_devres:
425 devres_free(ptr);
426 return ret;
427}
428EXPORT_SYMBOL_GPL(devm_platform_get_irqs_affinity);
429
1da177e4 430/**
4a3ad20c
GKH
431 * platform_get_resource_byname - get a resource for a device by name
432 * @dev: platform device
433 * @type: resource type
434 * @name: resource name
1da177e4 435 */
4a3ad20c 436struct resource *platform_get_resource_byname(struct platform_device *dev,
c0afe7ba
LW
437 unsigned int type,
438 const char *name)
1da177e4 439{
39cc539f 440 u32 i;
1da177e4
LT
441
442 for (i = 0; i < dev->num_resources; i++) {
443 struct resource *r = &dev->resource[i];
444
1b8cb929
PU
445 if (unlikely(!r->name))
446 continue;
447
c9f66169
MD
448 if (type == resource_type(r) && !strcmp(r->name, name))
449 return r;
1da177e4
LT
450 }
451 return NULL;
452}
a96b2042 453EXPORT_SYMBOL_GPL(platform_get_resource_byname);
1da177e4 454
f1da567f
HG
455static int __platform_get_irq_byname(struct platform_device *dev,
456 const char *name)
1da177e4 457{
ad69674e 458 struct resource *r;
71564a26 459 int ret;
ad69674e 460
aff008ad 461 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
aff008ad 462 ret = of_irq_get_byname(dev->dev.of_node, name);
e330b9a6 463 if (ret > 0 || ret == -EPROBE_DEFER)
aff008ad
GR
464 return ret;
465 }
1da177e4 466
ad69674e 467 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
a85a6c86
BH
468 if (r) {
469 WARN(r->start == 0, "0 is an invalid IRQ number\n");
7723f4c5 470 return r->start;
a85a6c86 471 }
7723f4c5 472
7723f4c5 473 return -ENXIO;
1da177e4 474}
f1da567f
HG
475
476/**
477 * platform_get_irq_byname - get an IRQ for a device by name
478 * @dev: platform device
479 * @name: IRQ name
480 *
481 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
482 *
a85a6c86 483 * Return: non-zero IRQ number on success, negative error number on failure.
f1da567f
HG
484 */
485int platform_get_irq_byname(struct platform_device *dev, const char *name)
486{
487 int ret;
488
489 ret = __platform_get_irq_byname(dev, name);
490 if (ret < 0 && ret != -EPROBE_DEFER)
491 dev_err(&dev->dev, "IRQ %s not found\n", name);
492
493 return ret;
494}
a96b2042 495EXPORT_SYMBOL_GPL(platform_get_irq_byname);
1da177e4 496
f1da567f
HG
497/**
498 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
499 * @dev: platform device
500 * @name: IRQ name
501 *
502 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
503 * does not print an error message if an IRQ can not be obtained.
504 *
a85a6c86 505 * Return: non-zero IRQ number on success, negative error number on failure.
f1da567f
HG
506 */
507int platform_get_irq_byname_optional(struct platform_device *dev,
508 const char *name)
509{
510 return __platform_get_irq_byname(dev, name);
511}
512EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
513
1da177e4 514/**
4a3ad20c
GKH
515 * platform_add_devices - add a numbers of platform devices
516 * @devs: array of platform devices to add
517 * @num: number of platform devices in array
1da177e4
LT
518 */
519int platform_add_devices(struct platform_device **devs, int num)
520{
521 int i, ret = 0;
522
523 for (i = 0; i < num; i++) {
524 ret = platform_device_register(devs[i]);
525 if (ret) {
526 while (--i >= 0)
527 platform_device_unregister(devs[i]);
528 break;
529 }
530 }
531
532 return ret;
533}
a96b2042 534EXPORT_SYMBOL_GPL(platform_add_devices);
1da177e4 535
37c12e74
RK
536struct platform_object {
537 struct platform_device pdev;
1cec24c5 538 char name[];
37c12e74
RK
539};
540
cdfee562
CH
541/*
542 * Set up default DMA mask for platform devices if the they weren't
543 * previously set by the architecture / DT.
544 */
545static void setup_pdev_dma_masks(struct platform_device *pdev)
546{
9495b7e9
UH
547 pdev->dev.dma_parms = &pdev->dma_parms;
548
cdfee562
CH
549 if (!pdev->dev.coherent_dma_mask)
550 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
e3a36eb6
CH
551 if (!pdev->dev.dma_mask) {
552 pdev->platform_dma_mask = DMA_BIT_MASK(32);
553 pdev->dev.dma_mask = &pdev->platform_dma_mask;
554 }
cdfee562
CH
555};
556
1da177e4 557/**
3c31f07a 558 * platform_device_put - destroy a platform device
4a3ad20c 559 * @pdev: platform device to free
37c12e74 560 *
4a3ad20c
GKH
561 * Free all memory associated with a platform device. This function must
562 * _only_ be externally called in error cases. All other usage is a bug.
37c12e74
RK
563 */
564void platform_device_put(struct platform_device *pdev)
565{
99fef587 566 if (!IS_ERR_OR_NULL(pdev))
37c12e74
RK
567 put_device(&pdev->dev);
568}
569EXPORT_SYMBOL_GPL(platform_device_put);
570
571static void platform_device_release(struct device *dev)
572{
4a3ad20c
GKH
573 struct platform_object *pa = container_of(dev, struct platform_object,
574 pdev.dev);
37c12e74 575
7096d042 576 of_device_node_put(&pa->pdev.dev);
37c12e74 577 kfree(pa->pdev.dev.platform_data);
e710d7d5 578 kfree(pa->pdev.mfd_cell);
37c12e74 579 kfree(pa->pdev.resource);
3d713e0e 580 kfree(pa->pdev.driver_override);
37c12e74
RK
581 kfree(pa);
582}
583
584/**
3c31f07a 585 * platform_device_alloc - create a platform device
4a3ad20c
GKH
586 * @name: base name of the device we're adding
587 * @id: instance id
37c12e74 588 *
4a3ad20c
GKH
589 * Create a platform device object which can have other objects attached
590 * to it, and which will have attached objects freed when it is released.
37c12e74 591 */
1359555e 592struct platform_device *platform_device_alloc(const char *name, int id)
37c12e74
RK
593{
594 struct platform_object *pa;
595
1cec24c5 596 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
37c12e74
RK
597 if (pa) {
598 strcpy(pa->name, name);
599 pa->pdev.name = pa->name;
600 pa->pdev.id = id;
601 device_initialize(&pa->pdev.dev);
602 pa->pdev.dev.release = platform_device_release;
cdfee562 603 setup_pdev_dma_masks(&pa->pdev);
37c12e74
RK
604 }
605
93ce3061 606 return pa ? &pa->pdev : NULL;
37c12e74
RK
607}
608EXPORT_SYMBOL_GPL(platform_device_alloc);
609
610/**
3c31f07a 611 * platform_device_add_resources - add resources to a platform device
4a3ad20c
GKH
612 * @pdev: platform device allocated by platform_device_alloc to add resources to
613 * @res: set of resources that needs to be allocated for the device
614 * @num: number of resources
37c12e74 615 *
4a3ad20c
GKH
616 * Add a copy of the resources to the platform device. The memory
617 * associated with the resources will be freed when the platform device is
618 * released.
37c12e74 619 */
4a3ad20c 620int platform_device_add_resources(struct platform_device *pdev,
0b7f1a7e 621 const struct resource *res, unsigned int num)
37c12e74 622{
cea89623 623 struct resource *r = NULL;
37c12e74 624
cea89623
UKK
625 if (res) {
626 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
627 if (!r)
628 return -ENOMEM;
37c12e74 629 }
cea89623 630
4a03d6f7 631 kfree(pdev->resource);
cea89623
UKK
632 pdev->resource = r;
633 pdev->num_resources = num;
634 return 0;
37c12e74
RK
635}
636EXPORT_SYMBOL_GPL(platform_device_add_resources);
637
638/**
3c31f07a 639 * platform_device_add_data - add platform-specific data to a platform device
4a3ad20c
GKH
640 * @pdev: platform device allocated by platform_device_alloc to add resources to
641 * @data: platform specific data for this platform device
642 * @size: size of platform specific data
37c12e74 643 *
4a3ad20c
GKH
644 * Add a copy of platform specific data to the platform device's
645 * platform_data pointer. The memory associated with the platform data
646 * will be freed when the platform device is released.
37c12e74 647 */
4a3ad20c
GKH
648int platform_device_add_data(struct platform_device *pdev, const void *data,
649 size_t size)
37c12e74 650{
27a33f9e 651 void *d = NULL;
5cfc64ce 652
27a33f9e
UKK
653 if (data) {
654 d = kmemdup(data, size, GFP_KERNEL);
655 if (!d)
656 return -ENOMEM;
37c12e74 657 }
27a33f9e 658
251e031d 659 kfree(pdev->dev.platform_data);
27a33f9e
UKK
660 pdev->dev.platform_data = d;
661 return 0;
37c12e74
RK
662}
663EXPORT_SYMBOL_GPL(platform_device_add_data);
664
00bbc1d8
MW
665/**
666 * platform_device_add_properties - add built-in properties to a platform device
667 * @pdev: platform device to add properties to
f4d05266 668 * @properties: null terminated array of properties to add
00bbc1d8 669 *
f4d05266
HK
670 * The function will take deep copy of @properties and attach the copy to the
671 * platform device. The memory associated with properties will be freed when the
672 * platform device is released.
00bbc1d8
MW
673 */
674int platform_device_add_properties(struct platform_device *pdev,
277036f0 675 const struct property_entry *properties)
00bbc1d8 676{
f4d05266 677 return device_add_properties(&pdev->dev, properties);
00bbc1d8
MW
678}
679EXPORT_SYMBOL_GPL(platform_device_add_properties);
680
37c12e74 681/**
4a3ad20c
GKH
682 * platform_device_add - add a platform device to device hierarchy
683 * @pdev: platform device we're adding
1da177e4 684 *
4a3ad20c
GKH
685 * This is part 2 of platform_device_register(), though may be called
686 * separately _iff_ pdev was allocated by platform_device_alloc().
1da177e4 687 */
37c12e74 688int platform_device_add(struct platform_device *pdev)
1da177e4 689{
39cc539f
SS
690 u32 i;
691 int ret;
1da177e4
LT
692
693 if (!pdev)
694 return -EINVAL;
695
696 if (!pdev->dev.parent)
697 pdev->dev.parent = &platform_bus;
698
699 pdev->dev.bus = &platform_bus_type;
700
689ae231
JD
701 switch (pdev->id) {
702 default:
1e0b2cf9 703 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
689ae231
JD
704 break;
705 case PLATFORM_DEVID_NONE:
acc0e90f 706 dev_set_name(&pdev->dev, "%s", pdev->name);
689ae231
JD
707 break;
708 case PLATFORM_DEVID_AUTO:
709 /*
710 * Automatically allocated device ID. We mark it as such so
711 * that we remember it must be freed, and we append a suffix
712 * to avoid namespace collision with explicit IDs.
713 */
0de75116 714 ret = ida_alloc(&platform_devid_ida, GFP_KERNEL);
689ae231 715 if (ret < 0)
5da7f709 716 goto err_out;
689ae231
JD
717 pdev->id = ret;
718 pdev->id_auto = true;
719 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
720 break;
721 }
1da177e4
LT
722
723 for (i = 0; i < pdev->num_resources; i++) {
5da7f709 724 struct resource *p, *r = &pdev->resource[i];
1da177e4
LT
725
726 if (r->name == NULL)
1e0b2cf9 727 r->name = dev_name(&pdev->dev);
1da177e4
LT
728
729 p = r->parent;
730 if (!p) {
0e6c861f 731 if (resource_type(r) == IORESOURCE_MEM)
1da177e4 732 p = &iomem_resource;
0e6c861f 733 else if (resource_type(r) == IORESOURCE_IO)
1da177e4
LT
734 p = &ioport_resource;
735 }
736
25ebcb7d
AS
737 if (p) {
738 ret = insert_resource(p, r);
739 if (ret) {
740 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
741 goto failed;
742 }
5da7f709 743 }
1da177e4
LT
744 }
745
746 pr_debug("Registering platform device '%s'. Parent at %s\n",
1e0b2cf9 747 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
1da177e4 748
e3915532 749 ret = device_add(&pdev->dev);
8b2dceba
GKH
750 if (ret == 0)
751 return ret;
752
5da7f709 753 failed:
8b2dceba 754 if (pdev->id_auto) {
0de75116 755 ida_free(&platform_devid_ida, pdev->id);
8b2dceba
GKH
756 pdev->id = PLATFORM_DEVID_AUTO;
757 }
758
0707cfa5 759 while (i--) {
8b2dceba 760 struct resource *r = &pdev->resource[i];
7f5dcaf1 761 if (r->parent)
8b2dceba
GKH
762 release_resource(r);
763 }
c9f66169 764
5da7f709 765 err_out:
1da177e4
LT
766 return ret;
767}
37c12e74
RK
768EXPORT_SYMBOL_GPL(platform_device_add);
769
770/**
4a3ad20c
GKH
771 * platform_device_del - remove a platform-level device
772 * @pdev: platform device we're removing
1da177e4 773 *
4a3ad20c
GKH
774 * Note that this function will also release all memory- and port-based
775 * resources owned by the device (@dev->resource). This function must
776 * _only_ be externally called in error cases. All other usage is a bug.
1da177e4 777 */
93ce3061 778void platform_device_del(struct platform_device *pdev)
1da177e4 779{
39cc539f 780 u32 i;
c9f66169 781
99fef587 782 if (!IS_ERR_OR_NULL(pdev)) {
8b2dceba
GKH
783 device_del(&pdev->dev);
784
785 if (pdev->id_auto) {
0de75116 786 ida_free(&platform_devid_ida, pdev->id);
8b2dceba
GKH
787 pdev->id = PLATFORM_DEVID_AUTO;
788 }
789
790 for (i = 0; i < pdev->num_resources; i++) {
791 struct resource *r = &pdev->resource[i];
7f5dcaf1 792 if (r->parent)
8b2dceba
GKH
793 release_resource(r);
794 }
795 }
1da177e4 796}
93ce3061
DT
797EXPORT_SYMBOL_GPL(platform_device_del);
798
799/**
4a3ad20c
GKH
800 * platform_device_register - add a platform-level device
801 * @pdev: platform device we're adding
93ce3061 802 */
4a3ad20c 803int platform_device_register(struct platform_device *pdev)
93ce3061
DT
804{
805 device_initialize(&pdev->dev);
cdfee562 806 setup_pdev_dma_masks(pdev);
93ce3061
DT
807 return platform_device_add(pdev);
808}
a96b2042 809EXPORT_SYMBOL_GPL(platform_device_register);
93ce3061
DT
810
811/**
4a3ad20c
GKH
812 * platform_device_unregister - unregister a platform-level device
813 * @pdev: platform device we're unregistering
93ce3061 814 *
4a3ad20c
GKH
815 * Unregistration is done in 2 steps. First we release all resources
816 * and remove it from the subsystem, then we drop reference count by
817 * calling platform_device_put().
93ce3061 818 */
4a3ad20c 819void platform_device_unregister(struct platform_device *pdev)
93ce3061
DT
820{
821 platform_device_del(pdev);
822 platform_device_put(pdev);
823}
a96b2042 824EXPORT_SYMBOL_GPL(platform_device_unregister);
1da177e4 825
1da177e4 826/**
01dcc60a 827 * platform_device_register_full - add a platform-level device with
44f28bde 828 * resources and platform-specific data
49a4ec18 829 *
01dcc60a 830 * @pdevinfo: data used to create device
d8bf2540 831 *
f0eae0ed 832 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
d8bf2540 833 */
01dcc60a 834struct platform_device *platform_device_register_full(
5a3072be 835 const struct platform_device_info *pdevinfo)
d8bf2540 836{
45bb08de 837 int ret;
d8bf2540 838 struct platform_device *pdev;
d8bf2540 839
01dcc60a 840 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
44f28bde 841 if (!pdev)
36cf3b13 842 return ERR_PTR(-ENOMEM);
01dcc60a
UKK
843
844 pdev->dev.parent = pdevinfo->parent;
ce793486 845 pdev->dev.fwnode = pdevinfo->fwnode;
2c1ea6ab
MR
846 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
847 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
01dcc60a
UKK
848
849 if (pdevinfo->dma_mask) {
e3a36eb6
CH
850 pdev->platform_dma_mask = pdevinfo->dma_mask;
851 pdev->dev.dma_mask = &pdev->platform_dma_mask;
01dcc60a
UKK
852 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
853 }
d8bf2540 854
01dcc60a
UKK
855 ret = platform_device_add_resources(pdev,
856 pdevinfo->res, pdevinfo->num_res);
807508c8
AV
857 if (ret)
858 goto err;
44f28bde 859
01dcc60a
UKK
860 ret = platform_device_add_data(pdev,
861 pdevinfo->data, pdevinfo->size_data);
807508c8
AV
862 if (ret)
863 goto err;
d8bf2540 864
f4d05266
HK
865 if (pdevinfo->properties) {
866 ret = platform_device_add_properties(pdev,
867 pdevinfo->properties);
00bbc1d8
MW
868 if (ret)
869 goto err;
870 }
871
44f28bde
UKK
872 ret = platform_device_add(pdev);
873 if (ret) {
874err:
7b199811 875 ACPI_COMPANION_SET(&pdev->dev, NULL);
44f28bde
UKK
876 platform_device_put(pdev);
877 return ERR_PTR(ret);
878 }
d8bf2540
DB
879
880 return pdev;
d8bf2540 881}
01dcc60a 882EXPORT_SYMBOL_GPL(platform_device_register_full);
d8bf2540 883
00d3dcdd 884/**
9447057e 885 * __platform_driver_register - register a driver for platform-level devices
4a3ad20c 886 * @drv: platform driver structure
08801f96 887 * @owner: owning module/driver
00d3dcdd 888 */
9447057e
LC
889int __platform_driver_register(struct platform_driver *drv,
890 struct module *owner)
00d3dcdd 891{
9447057e 892 drv->driver.owner = owner;
00d3dcdd 893 drv->driver.bus = &platform_bus_type;
783ea7d4 894
00d3dcdd
RK
895 return driver_register(&drv->driver);
896}
9447057e 897EXPORT_SYMBOL_GPL(__platform_driver_register);
00d3dcdd
RK
898
899/**
3c31f07a 900 * platform_driver_unregister - unregister a driver for platform-level devices
4a3ad20c 901 * @drv: platform driver structure
00d3dcdd
RK
902 */
903void platform_driver_unregister(struct platform_driver *drv)
904{
905 driver_unregister(&drv->driver);
906}
907EXPORT_SYMBOL_GPL(platform_driver_unregister);
908
16085668 909static int platform_probe_fail(struct platform_device *pdev)
e21d740a
UKK
910{
911 return -ENXIO;
912}
913
c67334fb 914/**
c3b50dc2 915 * __platform_driver_probe - register driver for non-hotpluggable device
c67334fb 916 * @drv: platform driver structure
3f9120b0 917 * @probe: the driver probe routine, probably from an __init section
c3b50dc2 918 * @module: module which will be the owner of the driver
c67334fb
DB
919 *
920 * Use this instead of platform_driver_register() when you know the device
921 * is not hotpluggable and has already been registered, and you want to
922 * remove its run-once probe() infrastructure from memory after the driver
923 * has bound to the device.
924 *
925 * One typical use for this would be with drivers for controllers integrated
926 * into system-on-chip processors, where the controller devices have been
927 * configured as part of board setup.
928 *
3f9120b0 929 * Note that this is incompatible with deferred probing.
647c86d0 930 *
c67334fb
DB
931 * Returns zero if the driver registered and bound to a device, else returns
932 * a negative error code and with the driver not registered.
933 */
c3b50dc2
WS
934int __init_or_module __platform_driver_probe(struct platform_driver *drv,
935 int (*probe)(struct platform_device *), struct module *module)
c67334fb
DB
936{
937 int retval, code;
938
5c36eb2a
DT
939 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
940 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
941 drv->driver.name, __func__);
942 return -EINVAL;
943 }
944
945 /*
946 * We have to run our probes synchronously because we check if
947 * we find any devices to bind to and exit with error if there
948 * are any.
949 */
950 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
951
3f9120b0
JH
952 /*
953 * Prevent driver from requesting probe deferral to avoid further
954 * futile probe attempts.
955 */
956 drv->prevent_deferred_probe = true;
957
1a6f2a75
DT
958 /* make sure driver won't have bind/unbind attributes */
959 drv->driver.suppress_bind_attrs = true;
960
c67334fb
DB
961 /* temporary section violation during probe() */
962 drv->probe = probe;
c3b50dc2 963 retval = code = __platform_driver_register(drv, module);
388bcc6e
KS
964 if (retval)
965 return retval;
c67334fb 966
1a6f2a75
DT
967 /*
968 * Fixup that section violation, being paranoid about code scanning
c67334fb
DB
969 * the list of drivers in order to probe new devices. Check to see
970 * if the probe was successful, and make sure any forced probes of
971 * new devices fail.
972 */
d79d3244 973 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
16085668 974 drv->probe = platform_probe_fail;
e5dd1278 975 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
c67334fb 976 retval = -ENODEV;
d79d3244 977 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
c67334fb
DB
978
979 if (code != retval)
980 platform_driver_unregister(drv);
981 return retval;
982}
c3b50dc2 983EXPORT_SYMBOL_GPL(__platform_driver_probe);
1da177e4 984
ecdf6ceb 985/**
291f653a 986 * __platform_create_bundle - register driver and create corresponding device
ecdf6ceb
DT
987 * @driver: platform driver structure
988 * @probe: the driver probe routine, probably from an __init section
989 * @res: set of resources that needs to be allocated for the device
990 * @n_res: number of resources
991 * @data: platform specific data for this platform device
992 * @size: size of platform specific data
291f653a 993 * @module: module which will be the owner of the driver
ecdf6ceb
DT
994 *
995 * Use this in legacy-style modules that probe hardware directly and
996 * register a single platform device and corresponding platform driver.
f0eae0ed
JN
997 *
998 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
ecdf6ceb 999 */
291f653a 1000struct platform_device * __init_or_module __platform_create_bundle(
ecdf6ceb
DT
1001 struct platform_driver *driver,
1002 int (*probe)(struct platform_device *),
1003 struct resource *res, unsigned int n_res,
291f653a 1004 const void *data, size_t size, struct module *module)
ecdf6ceb
DT
1005{
1006 struct platform_device *pdev;
1007 int error;
1008
1009 pdev = platform_device_alloc(driver->driver.name, -1);
1010 if (!pdev) {
1011 error = -ENOMEM;
1012 goto err_out;
1013 }
1014
807508c8
AV
1015 error = platform_device_add_resources(pdev, res, n_res);
1016 if (error)
1017 goto err_pdev_put;
ecdf6ceb 1018
807508c8
AV
1019 error = platform_device_add_data(pdev, data, size);
1020 if (error)
1021 goto err_pdev_put;
ecdf6ceb
DT
1022
1023 error = platform_device_add(pdev);
1024 if (error)
1025 goto err_pdev_put;
1026
291f653a 1027 error = __platform_driver_probe(driver, probe, module);
ecdf6ceb
DT
1028 if (error)
1029 goto err_pdev_del;
1030
1031 return pdev;
1032
1033err_pdev_del:
1034 platform_device_del(pdev);
1035err_pdev_put:
1036 platform_device_put(pdev);
1037err_out:
1038 return ERR_PTR(error);
1039}
291f653a 1040EXPORT_SYMBOL_GPL(__platform_create_bundle);
ecdf6ceb 1041
dbe2256d
TR
1042/**
1043 * __platform_register_drivers - register an array of platform drivers
1044 * @drivers: an array of drivers to register
1045 * @count: the number of drivers to register
1046 * @owner: module owning the drivers
1047 *
1048 * Registers platform drivers specified by an array. On failure to register a
1049 * driver, all previously registered drivers will be unregistered. Callers of
1050 * this API should use platform_unregister_drivers() to unregister drivers in
1051 * the reverse order.
1052 *
1053 * Returns: 0 on success or a negative error code on failure.
1054 */
1055int __platform_register_drivers(struct platform_driver * const *drivers,
1056 unsigned int count, struct module *owner)
1057{
1058 unsigned int i;
1059 int err;
1060
1061 for (i = 0; i < count; i++) {
1062 pr_debug("registering platform driver %ps\n", drivers[i]);
1063
1064 err = __platform_driver_register(drivers[i], owner);
1065 if (err < 0) {
1066 pr_err("failed to register platform driver %ps: %d\n",
1067 drivers[i], err);
1068 goto error;
1069 }
1070 }
1071
1072 return 0;
1073
1074error:
1075 while (i--) {
1076 pr_debug("unregistering platform driver %ps\n", drivers[i]);
1077 platform_driver_unregister(drivers[i]);
1078 }
1079
1080 return err;
1081}
1082EXPORT_SYMBOL_GPL(__platform_register_drivers);
1083
1084/**
1085 * platform_unregister_drivers - unregister an array of platform drivers
1086 * @drivers: an array of drivers to unregister
1087 * @count: the number of drivers to unregister
1088 *
c82c83c3 1089 * Unregisters platform drivers specified by an array. This is typically used
dbe2256d
TR
1090 * to complement an earlier call to platform_register_drivers(). Drivers are
1091 * unregistered in the reverse order in which they were registered.
1092 */
1093void platform_unregister_drivers(struct platform_driver * const *drivers,
1094 unsigned int count)
1095{
1096 while (count--) {
1097 pr_debug("unregistering platform driver %ps\n", drivers[count]);
1098 platform_driver_unregister(drivers[count]);
1099 }
1100}
1101EXPORT_SYMBOL_GPL(platform_unregister_drivers);
1102
57fee4a5 1103static const struct platform_device_id *platform_match_id(
831fad2f 1104 const struct platform_device_id *id,
57fee4a5
EM
1105 struct platform_device *pdev)
1106{
1107 while (id->name[0]) {
391c0325 1108 if (strcmp(pdev->name, id->name) == 0) {
57fee4a5
EM
1109 pdev->id_entry = id;
1110 return id;
1111 }
1112 id++;
1113 }
1114 return NULL;
1115}
1116
25e18499
RW
1117#ifdef CONFIG_PM_SLEEP
1118
1119static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1da177e4 1120{
783ea7d4
MD
1121 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1122 struct platform_device *pdev = to_platform_device(dev);
1da177e4
LT
1123 int ret = 0;
1124
783ea7d4
MD
1125 if (dev->driver && pdrv->suspend)
1126 ret = pdrv->suspend(pdev, mesg);
386415d8
DB
1127
1128 return ret;
1129}
1130
25e18499 1131static int platform_legacy_resume(struct device *dev)
1da177e4 1132{
783ea7d4
MD
1133 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1134 struct platform_device *pdev = to_platform_device(dev);
1da177e4
LT
1135 int ret = 0;
1136
783ea7d4
MD
1137 if (dev->driver && pdrv->resume)
1138 ret = pdrv->resume(pdev);
9480e307 1139
1da177e4
LT
1140 return ret;
1141}
1142
69c9dd1e 1143#endif /* CONFIG_PM_SLEEP */
9d730229 1144
25e18499
RW
1145#ifdef CONFIG_SUSPEND
1146
69c9dd1e 1147int platform_pm_suspend(struct device *dev)
25e18499
RW
1148{
1149 struct device_driver *drv = dev->driver;
1150 int ret = 0;
1151
adf09493
RW
1152 if (!drv)
1153 return 0;
1154
1155 if (drv->pm) {
25e18499
RW
1156 if (drv->pm->suspend)
1157 ret = drv->pm->suspend(dev);
1158 } else {
1159 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1160 }
1161
1162 return ret;
1163}
1164
69c9dd1e 1165int platform_pm_resume(struct device *dev)
25e18499
RW
1166{
1167 struct device_driver *drv = dev->driver;
1168 int ret = 0;
1169
adf09493
RW
1170 if (!drv)
1171 return 0;
1172
1173 if (drv->pm) {
25e18499
RW
1174 if (drv->pm->resume)
1175 ret = drv->pm->resume(dev);
1176 } else {
1177 ret = platform_legacy_resume(dev);
1178 }
1179
1180 return ret;
1181}
1182
69c9dd1e 1183#endif /* CONFIG_SUSPEND */
25e18499 1184
1f112cee 1185#ifdef CONFIG_HIBERNATE_CALLBACKS
25e18499 1186
69c9dd1e 1187int platform_pm_freeze(struct device *dev)
25e18499
RW
1188{
1189 struct device_driver *drv = dev->driver;
1190 int ret = 0;
1191
1192 if (!drv)
1193 return 0;
1194
1195 if (drv->pm) {
1196 if (drv->pm->freeze)
1197 ret = drv->pm->freeze(dev);
1198 } else {
1199 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1200 }
1201
1202 return ret;
1203}
1204
69c9dd1e 1205int platform_pm_thaw(struct device *dev)
25e18499
RW
1206{
1207 struct device_driver *drv = dev->driver;
1208 int ret = 0;
1209
adf09493
RW
1210 if (!drv)
1211 return 0;
1212
1213 if (drv->pm) {
25e18499
RW
1214 if (drv->pm->thaw)
1215 ret = drv->pm->thaw(dev);
1216 } else {
1217 ret = platform_legacy_resume(dev);
1218 }
1219
1220 return ret;
1221}
1222
69c9dd1e 1223int platform_pm_poweroff(struct device *dev)
25e18499
RW
1224{
1225 struct device_driver *drv = dev->driver;
1226 int ret = 0;
1227
adf09493
RW
1228 if (!drv)
1229 return 0;
1230
1231 if (drv->pm) {
25e18499
RW
1232 if (drv->pm->poweroff)
1233 ret = drv->pm->poweroff(dev);
1234 } else {
1235 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1236 }
1237
1238 return ret;
1239}
1240
69c9dd1e 1241int platform_pm_restore(struct device *dev)
25e18499
RW
1242{
1243 struct device_driver *drv = dev->driver;
1244 int ret = 0;
1245
adf09493
RW
1246 if (!drv)
1247 return 0;
1248
1249 if (drv->pm) {
25e18499
RW
1250 if (drv->pm->restore)
1251 ret = drv->pm->restore(dev);
1252 } else {
1253 ret = platform_legacy_resume(dev);
1254 }
1255
1256 return ret;
1257}
1258
69c9dd1e 1259#endif /* CONFIG_HIBERNATE_CALLBACKS */
25e18499 1260
e21d740a
UKK
1261/* modalias support enables more hands-off userspace setup:
1262 * (a) environment variable lets new-style hotplug events work once system is
1263 * fully running: "modprobe $MODALIAS"
1264 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
1265 * mishandled before system is fully running: "modprobe $(cat modalias)"
1266 */
1267static ssize_t modalias_show(struct device *dev,
1268 struct device_attribute *attr, char *buf)
1269{
1270 struct platform_device *pdev = to_platform_device(dev);
1271 int len;
1272
1273 len = of_device_modalias(dev, buf, PAGE_SIZE);
1274 if (len != -ENODEV)
1275 return len;
1276
1277 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
1278 if (len != -ENODEV)
1279 return len;
1280
1281 return sysfs_emit(buf, "platform:%s\n", pdev->name);
1282}
1283static DEVICE_ATTR_RO(modalias);
1284
1285static ssize_t numa_node_show(struct device *dev,
1286 struct device_attribute *attr, char *buf)
1287{
1288 return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1289}
1290static DEVICE_ATTR_RO(numa_node);
1291
1292static ssize_t driver_override_show(struct device *dev,
1293 struct device_attribute *attr, char *buf)
1294{
1295 struct platform_device *pdev = to_platform_device(dev);
1296 ssize_t len;
1297
1298 device_lock(dev);
1299 len = sysfs_emit(buf, "%s\n", pdev->driver_override);
1300 device_unlock(dev);
1301
1302 return len;
1303}
1304
1305static ssize_t driver_override_store(struct device *dev,
1306 struct device_attribute *attr,
1307 const char *buf, size_t count)
1308{
1309 struct platform_device *pdev = to_platform_device(dev);
1310 char *driver_override, *old, *cp;
1311
1312 /* We need to keep extra room for a newline */
1313 if (count >= (PAGE_SIZE - 1))
1314 return -EINVAL;
1315
1316 driver_override = kstrndup(buf, count, GFP_KERNEL);
1317 if (!driver_override)
1318 return -ENOMEM;
1319
1320 cp = strchr(driver_override, '\n');
1321 if (cp)
1322 *cp = '\0';
1323
1324 device_lock(dev);
1325 old = pdev->driver_override;
1326 if (strlen(driver_override)) {
1327 pdev->driver_override = driver_override;
1328 } else {
1329 kfree(driver_override);
1330 pdev->driver_override = NULL;
1331 }
1332 device_unlock(dev);
1333
1334 kfree(old);
1335
1336 return count;
1337}
1338static DEVICE_ATTR_RW(driver_override);
1339
1340static struct attribute *platform_dev_attrs[] = {
1341 &dev_attr_modalias.attr,
1342 &dev_attr_numa_node.attr,
1343 &dev_attr_driver_override.attr,
1344 NULL,
1345};
1346
1347static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a,
1348 int n)
1349{
1350 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1351
1352 if (a == &dev_attr_numa_node.attr &&
1353 dev_to_node(dev) == NUMA_NO_NODE)
1354 return 0;
1355
1356 return a->mode;
1357}
1358
1359static struct attribute_group platform_dev_group = {
1360 .attrs = platform_dev_attrs,
1361 .is_visible = platform_dev_attrs_visible,
1362};
1363__ATTRIBUTE_GROUPS(platform_dev);
1364
1365
1366/**
1367 * platform_match - bind platform device to platform driver.
1368 * @dev: device.
1369 * @drv: driver.
1370 *
1371 * Platform device IDs are assumed to be encoded like this:
1372 * "<name><instance>", where <name> is a short description of the type of
1373 * device, like "pci" or "floppy", and <instance> is the enumerated
1374 * instance of the device, like '0' or '42'. Driver IDs are simply
1375 * "<name>". So, extract the <name> from the platform_device structure,
1376 * and compare it against the name of the driver. Return whether they match
1377 * or not.
1378 */
1379static int platform_match(struct device *dev, struct device_driver *drv)
1380{
1381 struct platform_device *pdev = to_platform_device(dev);
1382 struct platform_driver *pdrv = to_platform_driver(drv);
1383
1384 /* When driver_override is set, only bind to the matching driver */
1385 if (pdev->driver_override)
1386 return !strcmp(pdev->driver_override, drv->name);
1387
1388 /* Attempt an OF style match first */
1389 if (of_driver_match_device(dev, drv))
1390 return 1;
1391
1392 /* Then try ACPI style match */
1393 if (acpi_driver_match_device(dev, drv))
1394 return 1;
1395
1396 /* Then try to match against the id table */
1397 if (pdrv->id_table)
1398 return platform_match_id(pdrv->id_table, pdev) != NULL;
1399
1400 /* fall-back to driver name match */
1401 return (strcmp(pdev->name, drv->name) == 0);
1402}
1403
1404static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1405{
1406 struct platform_device *pdev = to_platform_device(dev);
1407 int rc;
1408
1409 /* Some devices have extra OF data and an OF-style MODALIAS */
1410 rc = of_device_uevent_modalias(dev, env);
1411 if (rc != -ENODEV)
1412 return rc;
1413
1414 rc = acpi_device_uevent_modalias(dev, env);
1415 if (rc != -ENODEV)
1416 return rc;
1417
1418 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1419 pdev->name);
1420 return 0;
1421}
1422
9c30921f
UKK
1423static int platform_probe(struct device *_dev)
1424{
1425 struct platform_driver *drv = to_platform_driver(_dev->driver);
1426 struct platform_device *dev = to_platform_device(_dev);
1427 int ret;
1428
1429 /*
1430 * A driver registered using platform_driver_probe() cannot be bound
1431 * again later because the probe function usually lives in __init code
1432 * and so is gone. For these drivers .probe is set to
1433 * platform_probe_fail in __platform_driver_probe(). Don't even prepare
1434 * clocks and PM domains for these to match the traditional behaviour.
1435 */
1436 if (unlikely(drv->probe == platform_probe_fail))
1437 return -ENXIO;
1438
1439 ret = of_clk_set_defaults(_dev->of_node, false);
1440 if (ret < 0)
1441 return ret;
1442
1443 ret = dev_pm_domain_attach(_dev, true);
1444 if (ret)
1445 goto out;
1446
1447 if (drv->probe) {
1448 ret = drv->probe(dev);
1449 if (ret)
1450 dev_pm_domain_detach(_dev, true);
1451 }
1452
1453out:
1454 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
1455 dev_warn(_dev, "probe deferral not supported\n");
1456 ret = -ENXIO;
1457 }
1458
1459 return ret;
1460}
1461
1462static int platform_remove(struct device *_dev)
1463{
1464 struct platform_driver *drv = to_platform_driver(_dev->driver);
1465 struct platform_device *dev = to_platform_device(_dev);
1466 int ret = 0;
1467
1468 if (drv->remove)
1469 ret = drv->remove(dev);
1470 dev_pm_domain_detach(_dev, true);
1471
1472 return ret;
1473}
1474
1475static void platform_shutdown(struct device *_dev)
1476{
9c30921f 1477 struct platform_device *dev = to_platform_device(_dev);
46e85af0
DB
1478 struct platform_driver *drv;
1479
1480 if (!_dev->driver)
1481 return;
9c30921f 1482
46e85af0 1483 drv = to_platform_driver(_dev->driver);
9c30921f
UKK
1484 if (drv->shutdown)
1485 drv->shutdown(dev);
1486}
1487
1488
07397df2
NG
1489int platform_dma_configure(struct device *dev)
1490{
1491 enum dev_dma_attr attr;
1492 int ret = 0;
1493
1494 if (dev->of_node) {
3d6ce86e 1495 ret = of_dma_configure(dev, dev->of_node, true);
07397df2
NG
1496 } else if (has_acpi_companion(dev)) {
1497 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
e5361ca2 1498 ret = acpi_dma_configure(dev, attr);
07397df2
NG
1499 }
1500
1501 return ret;
1502}
1503
d9ab7716 1504static const struct dev_pm_ops platform_dev_pm_ops = {
8b313a38
RW
1505 .runtime_suspend = pm_generic_runtime_suspend,
1506 .runtime_resume = pm_generic_runtime_resume,
69c9dd1e 1507 USE_PLATFORM_PM_SLEEP_OPS
25e18499
RW
1508};
1509
1da177e4
LT
1510struct bus_type platform_bus_type = {
1511 .name = "platform",
d06262e5 1512 .dev_groups = platform_dev_groups,
1da177e4 1513 .match = platform_match,
a0245f7a 1514 .uevent = platform_uevent,
9c30921f
UKK
1515 .probe = platform_probe,
1516 .remove = platform_remove,
1517 .shutdown = platform_shutdown,
07397df2 1518 .dma_configure = platform_dma_configure,
9d730229 1519 .pm = &platform_dev_pm_ops,
1da177e4 1520};
a96b2042 1521EXPORT_SYMBOL_GPL(platform_bus_type);
1da177e4 1522
492c8872
ST
1523static inline int __platform_match(struct device *dev, const void *drv)
1524{
1525 return platform_match(dev, (struct device_driver *)drv);
1526}
1527
36f3313d
SP
1528/**
1529 * platform_find_device_by_driver - Find a platform device with a given
1530 * driver.
1531 * @start: The device to start the search from.
1532 * @drv: The device driver to look for.
1533 */
1534struct device *platform_find_device_by_driver(struct device *start,
1535 const struct device_driver *drv)
1536{
1537 return bus_find_device(&platform_bus_type, start, drv,
492c8872 1538 __platform_match);
36f3313d
SP
1539}
1540EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1541
eecd37e1
GR
1542void __weak __init early_platform_cleanup(void) { }
1543
1da177e4
LT
1544int __init platform_bus_init(void)
1545{
fbfb1445
CH
1546 int error;
1547
eecd37e1
GR
1548 early_platform_cleanup();
1549
fbfb1445 1550 error = device_register(&platform_bus);
c8ae1674
AY
1551 if (error) {
1552 put_device(&platform_bus);
fbfb1445 1553 return error;
c8ae1674 1554 }
fbfb1445
CH
1555 error = bus_register(&platform_bus_type);
1556 if (error)
1557 device_unregister(&platform_bus);
801d728c 1558 of_platform_register_reconfig_notifier();
fbfb1445 1559 return error;
1da177e4 1560}