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