blob: 0b67d41bab8ff182b633ed14724b82cbd638b1ca [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
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 *
David Brazdil0f672f62019-12-10 10:32:29 +00008 * Please see Documentation/driver-api/driver-model/platform.rst for more
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009 * information.
10 */
11
12#include <linux/string.h>
13#include <linux/platform_device.h>
14#include <linux/of_device.h>
15#include <linux/of_irq.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/dma-mapping.h>
David Brazdil0f672f62019-12-10 10:32:29 +000019#include <linux/memblock.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020#include <linux/err.h>
21#include <linux/slab.h>
22#include <linux/pm_runtime.h>
23#include <linux/pm_domain.h>
24#include <linux/idr.h>
25#include <linux/acpi.h>
26#include <linux/clk/clk-conf.h>
27#include <linux/limits.h>
28#include <linux/property.h>
David Brazdil0f672f62019-12-10 10:32:29 +000029#include <linux/kmemleak.h>
Olivier Deprez0e641232021-09-23 10:07:05 +020030#include <linux/types.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031
32#include "base.h"
33#include "power/power.h"
34
35/* For automatically allocated device IDs */
36static DEFINE_IDA(platform_devid_ida);
37
38struct device platform_bus = {
39 .init_name = "platform",
40};
41EXPORT_SYMBOL_GPL(platform_bus);
42
43/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044 * platform_get_resource - get a resource for a device
45 * @dev: platform device
46 * @type: resource type
47 * @num: resource index
48 */
49struct resource *platform_get_resource(struct platform_device *dev,
50 unsigned int type, unsigned int num)
51{
Olivier Deprez0e641232021-09-23 10:07:05 +020052 u32 i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053
54 for (i = 0; i < dev->num_resources; i++) {
55 struct resource *r = &dev->resource[i];
56
57 if (type == resource_type(r) && num-- == 0)
58 return r;
59 }
60 return NULL;
61}
62EXPORT_SYMBOL_GPL(platform_get_resource);
63
64/**
David Brazdil0f672f62019-12-10 10:32:29 +000065 * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
66 * device
67 *
68 * @pdev: platform device to use both for memory resource lookup as well as
69 * resource management
70 * @index: resource index
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071 */
David Brazdil0f672f62019-12-10 10:32:29 +000072#ifdef CONFIG_HAS_IOMEM
73void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
74 unsigned int index)
75{
76 struct resource *res;
77
78 res = platform_get_resource(pdev, IORESOURCE_MEM, index);
79 return devm_ioremap_resource(&pdev->dev, res);
80}
81EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
82#endif /* CONFIG_HAS_IOMEM */
83
84static int __platform_get_irq(struct platform_device *dev, unsigned int num)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085{
86#ifdef CONFIG_SPARC
87 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
88 if (!dev || num >= dev->archdata.num_irqs)
89 return -ENXIO;
90 return dev->archdata.irqs[num];
91#else
92 struct resource *r;
93 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
94 int ret;
95
96 ret = of_irq_get(dev->dev.of_node, num);
97 if (ret > 0 || ret == -EPROBE_DEFER)
98 return ret;
99 }
100
101 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
102 if (has_acpi_companion(&dev->dev)) {
103 if (r && r->flags & IORESOURCE_DISABLED) {
104 int ret;
105
106 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
107 if (ret)
108 return ret;
109 }
110 }
111
112 /*
113 * The resources may pass trigger flags to the irqs that need
114 * to be set up. It so happens that the trigger flags for
115 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
116 * settings.
117 */
118 if (r && r->flags & IORESOURCE_BITS) {
119 struct irq_data *irqd;
120
121 irqd = irq_get_irq_data(r->start);
122 if (!irqd)
123 return -ENXIO;
124 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
125 }
126
David Brazdil0f672f62019-12-10 10:32:29 +0000127 if (r)
128 return r->start;
129
130 /*
131 * For the index 0 interrupt, allow falling back to GpioInt
132 * resources. While a device could have both Interrupt and GpioInt
133 * resources, making this fallback ambiguous, in many common cases
134 * the device will only expose one IRQ, and this fallback
135 * allows a common code path across either kind of resource.
136 */
137 if (num == 0 && has_acpi_companion(&dev->dev)) {
138 int ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
139
140 /* Our callers expect -ENXIO for missing IRQs. */
141 if (ret >= 0 || ret == -EPROBE_DEFER)
142 return ret;
143 }
144
145 return -ENXIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000146#endif
147}
David Brazdil0f672f62019-12-10 10:32:29 +0000148
149/**
150 * platform_get_irq - get an IRQ for a device
151 * @dev: platform device
152 * @num: IRQ number index
153 *
154 * Gets an IRQ for a platform device and prints an error message if finding the
155 * IRQ fails. Device drivers should check the return value for errors so as to
156 * not pass a negative integer value to the request_irq() APIs.
157 *
158 * Example:
159 * int irq = platform_get_irq(pdev, 0);
160 * if (irq < 0)
161 * return irq;
162 *
163 * Return: IRQ number on success, negative error number on failure.
164 */
165int platform_get_irq(struct platform_device *dev, unsigned int num)
166{
167 int ret;
168
169 ret = __platform_get_irq(dev, num);
170 if (ret < 0 && ret != -EPROBE_DEFER)
171 dev_err(&dev->dev, "IRQ index %u not found\n", num);
172
173 return ret;
174}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000175EXPORT_SYMBOL_GPL(platform_get_irq);
176
177/**
David Brazdil0f672f62019-12-10 10:32:29 +0000178 * platform_get_irq_optional - get an optional IRQ for a device
179 * @dev: platform device
180 * @num: IRQ number index
181 *
182 * Gets an IRQ for a platform device. Device drivers should check the return
183 * value for errors so as to not pass a negative integer value to the
184 * request_irq() APIs. This is the same as platform_get_irq(), except that it
185 * does not print an error message if an IRQ can not be obtained.
186 *
187 * Example:
188 * int irq = platform_get_irq_optional(pdev, 0);
189 * if (irq < 0)
190 * return irq;
191 *
192 * Return: IRQ number on success, negative error number on failure.
193 */
194int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
195{
196 return __platform_get_irq(dev, num);
197}
198EXPORT_SYMBOL_GPL(platform_get_irq_optional);
199
200/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000201 * platform_irq_count - Count the number of IRQs a platform device uses
202 * @dev: platform device
203 *
204 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
205 */
206int platform_irq_count(struct platform_device *dev)
207{
208 int ret, nr = 0;
209
David Brazdil0f672f62019-12-10 10:32:29 +0000210 while ((ret = __platform_get_irq(dev, nr)) >= 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000211 nr++;
212
213 if (ret == -EPROBE_DEFER)
214 return ret;
215
216 return nr;
217}
218EXPORT_SYMBOL_GPL(platform_irq_count);
219
220/**
221 * platform_get_resource_byname - get a resource for a device by name
222 * @dev: platform device
223 * @type: resource type
224 * @name: resource name
225 */
226struct resource *platform_get_resource_byname(struct platform_device *dev,
227 unsigned int type,
228 const char *name)
229{
Olivier Deprez0e641232021-09-23 10:07:05 +0200230 u32 i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231
232 for (i = 0; i < dev->num_resources; i++) {
233 struct resource *r = &dev->resource[i];
234
235 if (unlikely(!r->name))
236 continue;
237
238 if (type == resource_type(r) && !strcmp(r->name, name))
239 return r;
240 }
241 return NULL;
242}
243EXPORT_SYMBOL_GPL(platform_get_resource_byname);
244
David Brazdil0f672f62019-12-10 10:32:29 +0000245static int __platform_get_irq_byname(struct platform_device *dev,
246 const char *name)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000247{
248 struct resource *r;
249
250 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
251 int ret;
252
253 ret = of_irq_get_byname(dev->dev.of_node, name);
254 if (ret > 0 || ret == -EPROBE_DEFER)
255 return ret;
256 }
257
258 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
David Brazdil0f672f62019-12-10 10:32:29 +0000259 if (r)
260 return r->start;
261
262 return -ENXIO;
263}
264
265/**
266 * platform_get_irq_byname - get an IRQ for a device by name
267 * @dev: platform device
268 * @name: IRQ name
269 *
270 * Get an IRQ like platform_get_irq(), but then by name rather then by index.
271 *
272 * Return: IRQ number on success, negative error number on failure.
273 */
274int platform_get_irq_byname(struct platform_device *dev, const char *name)
275{
276 int ret;
277
278 ret = __platform_get_irq_byname(dev, name);
279 if (ret < 0 && ret != -EPROBE_DEFER)
280 dev_err(&dev->dev, "IRQ %s not found\n", name);
281
282 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283}
284EXPORT_SYMBOL_GPL(platform_get_irq_byname);
285
286/**
David Brazdil0f672f62019-12-10 10:32:29 +0000287 * platform_get_irq_byname_optional - get an optional IRQ for a device by name
288 * @dev: platform device
289 * @name: IRQ name
290 *
291 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
292 * does not print an error message if an IRQ can not be obtained.
293 *
294 * Return: IRQ number on success, negative error number on failure.
295 */
296int platform_get_irq_byname_optional(struct platform_device *dev,
297 const char *name)
298{
299 return __platform_get_irq_byname(dev, name);
300}
301EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
302
303/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000304 * platform_add_devices - add a numbers of platform devices
305 * @devs: array of platform devices to add
306 * @num: number of platform devices in array
307 */
308int platform_add_devices(struct platform_device **devs, int num)
309{
310 int i, ret = 0;
311
312 for (i = 0; i < num; i++) {
313 ret = platform_device_register(devs[i]);
314 if (ret) {
315 while (--i >= 0)
316 platform_device_unregister(devs[i]);
317 break;
318 }
319 }
320
321 return ret;
322}
323EXPORT_SYMBOL_GPL(platform_add_devices);
324
325struct platform_object {
326 struct platform_device pdev;
327 char name[];
328};
329
David Brazdil0f672f62019-12-10 10:32:29 +0000330/*
331 * Set up default DMA mask for platform devices if the they weren't
332 * previously set by the architecture / DT.
333 */
334static void setup_pdev_dma_masks(struct platform_device *pdev)
335{
336 if (!pdev->dev.coherent_dma_mask)
337 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Olivier Deprez0e641232021-09-23 10:07:05 +0200338 if (!pdev->dev.dma_mask) {
339 pdev->platform_dma_mask = DMA_BIT_MASK(32);
340 pdev->dev.dma_mask = &pdev->platform_dma_mask;
341 }
David Brazdil0f672f62019-12-10 10:32:29 +0000342};
343
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000344/**
345 * platform_device_put - destroy a platform device
346 * @pdev: platform device to free
347 *
348 * Free all memory associated with a platform device. This function must
349 * _only_ be externally called in error cases. All other usage is a bug.
350 */
351void platform_device_put(struct platform_device *pdev)
352{
David Brazdil0f672f62019-12-10 10:32:29 +0000353 if (!IS_ERR_OR_NULL(pdev))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000354 put_device(&pdev->dev);
355}
356EXPORT_SYMBOL_GPL(platform_device_put);
357
358static void platform_device_release(struct device *dev)
359{
360 struct platform_object *pa = container_of(dev, struct platform_object,
361 pdev.dev);
362
363 of_device_node_put(&pa->pdev.dev);
364 kfree(pa->pdev.dev.platform_data);
365 kfree(pa->pdev.mfd_cell);
366 kfree(pa->pdev.resource);
367 kfree(pa->pdev.driver_override);
368 kfree(pa);
369}
370
371/**
372 * platform_device_alloc - create a platform device
373 * @name: base name of the device we're adding
374 * @id: instance id
375 *
376 * Create a platform device object which can have other objects attached
377 * to it, and which will have attached objects freed when it is released.
378 */
379struct platform_device *platform_device_alloc(const char *name, int id)
380{
381 struct platform_object *pa;
382
383 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
384 if (pa) {
385 strcpy(pa->name, name);
386 pa->pdev.name = pa->name;
387 pa->pdev.id = id;
388 device_initialize(&pa->pdev.dev);
389 pa->pdev.dev.release = platform_device_release;
David Brazdil0f672f62019-12-10 10:32:29 +0000390 setup_pdev_dma_masks(&pa->pdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000391 }
392
393 return pa ? &pa->pdev : NULL;
394}
395EXPORT_SYMBOL_GPL(platform_device_alloc);
396
397/**
398 * platform_device_add_resources - add resources to a platform device
399 * @pdev: platform device allocated by platform_device_alloc to add resources to
400 * @res: set of resources that needs to be allocated for the device
401 * @num: number of resources
402 *
403 * Add a copy of the resources to the platform device. The memory
404 * associated with the resources will be freed when the platform device is
405 * released.
406 */
407int platform_device_add_resources(struct platform_device *pdev,
408 const struct resource *res, unsigned int num)
409{
410 struct resource *r = NULL;
411
412 if (res) {
413 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
414 if (!r)
415 return -ENOMEM;
416 }
417
418 kfree(pdev->resource);
419 pdev->resource = r;
420 pdev->num_resources = num;
421 return 0;
422}
423EXPORT_SYMBOL_GPL(platform_device_add_resources);
424
425/**
426 * platform_device_add_data - add platform-specific data to a platform device
427 * @pdev: platform device allocated by platform_device_alloc to add resources to
428 * @data: platform specific data for this platform device
429 * @size: size of platform specific data
430 *
431 * Add a copy of platform specific data to the platform device's
432 * platform_data pointer. The memory associated with the platform data
433 * will be freed when the platform device is released.
434 */
435int platform_device_add_data(struct platform_device *pdev, const void *data,
436 size_t size)
437{
438 void *d = NULL;
439
440 if (data) {
441 d = kmemdup(data, size, GFP_KERNEL);
442 if (!d)
443 return -ENOMEM;
444 }
445
446 kfree(pdev->dev.platform_data);
447 pdev->dev.platform_data = d;
448 return 0;
449}
450EXPORT_SYMBOL_GPL(platform_device_add_data);
451
452/**
453 * platform_device_add_properties - add built-in properties to a platform device
454 * @pdev: platform device to add properties to
455 * @properties: null terminated array of properties to add
456 *
457 * The function will take deep copy of @properties and attach the copy to the
458 * platform device. The memory associated with properties will be freed when the
459 * platform device is released.
460 */
461int platform_device_add_properties(struct platform_device *pdev,
462 const struct property_entry *properties)
463{
464 return device_add_properties(&pdev->dev, properties);
465}
466EXPORT_SYMBOL_GPL(platform_device_add_properties);
467
468/**
469 * platform_device_add - add a platform device to device hierarchy
470 * @pdev: platform device we're adding
471 *
472 * This is part 2 of platform_device_register(), though may be called
473 * separately _iff_ pdev was allocated by platform_device_alloc().
474 */
475int platform_device_add(struct platform_device *pdev)
476{
Olivier Deprez0e641232021-09-23 10:07:05 +0200477 u32 i;
478 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000479
480 if (!pdev)
481 return -EINVAL;
482
483 if (!pdev->dev.parent)
484 pdev->dev.parent = &platform_bus;
485
486 pdev->dev.bus = &platform_bus_type;
487
488 switch (pdev->id) {
489 default:
490 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
491 break;
492 case PLATFORM_DEVID_NONE:
493 dev_set_name(&pdev->dev, "%s", pdev->name);
494 break;
495 case PLATFORM_DEVID_AUTO:
496 /*
497 * Automatically allocated device ID. We mark it as such so
498 * that we remember it must be freed, and we append a suffix
499 * to avoid namespace collision with explicit IDs.
500 */
501 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
502 if (ret < 0)
503 goto err_out;
504 pdev->id = ret;
505 pdev->id_auto = true;
506 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
507 break;
508 }
509
510 for (i = 0; i < pdev->num_resources; i++) {
511 struct resource *p, *r = &pdev->resource[i];
512
513 if (r->name == NULL)
514 r->name = dev_name(&pdev->dev);
515
516 p = r->parent;
517 if (!p) {
518 if (resource_type(r) == IORESOURCE_MEM)
519 p = &iomem_resource;
520 else if (resource_type(r) == IORESOURCE_IO)
521 p = &ioport_resource;
522 }
523
David Brazdil0f672f62019-12-10 10:32:29 +0000524 if (p) {
525 ret = insert_resource(p, r);
526 if (ret) {
527 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
528 goto failed;
529 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000530 }
531 }
532
533 pr_debug("Registering platform device '%s'. Parent at %s\n",
534 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
535
536 ret = device_add(&pdev->dev);
537 if (ret == 0)
538 return ret;
539
540 failed:
541 if (pdev->id_auto) {
542 ida_simple_remove(&platform_devid_ida, pdev->id);
543 pdev->id = PLATFORM_DEVID_AUTO;
544 }
545
Olivier Deprez0e641232021-09-23 10:07:05 +0200546 while (i--) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000547 struct resource *r = &pdev->resource[i];
548 if (r->parent)
549 release_resource(r);
550 }
551
552 err_out:
553 return ret;
554}
555EXPORT_SYMBOL_GPL(platform_device_add);
556
557/**
558 * platform_device_del - remove a platform-level device
559 * @pdev: platform device we're removing
560 *
561 * Note that this function will also release all memory- and port-based
562 * resources owned by the device (@dev->resource). This function must
563 * _only_ be externally called in error cases. All other usage is a bug.
564 */
565void platform_device_del(struct platform_device *pdev)
566{
Olivier Deprez0e641232021-09-23 10:07:05 +0200567 u32 i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000568
David Brazdil0f672f62019-12-10 10:32:29 +0000569 if (!IS_ERR_OR_NULL(pdev)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000570 device_del(&pdev->dev);
571
572 if (pdev->id_auto) {
573 ida_simple_remove(&platform_devid_ida, pdev->id);
574 pdev->id = PLATFORM_DEVID_AUTO;
575 }
576
577 for (i = 0; i < pdev->num_resources; i++) {
578 struct resource *r = &pdev->resource[i];
579 if (r->parent)
580 release_resource(r);
581 }
582 }
583}
584EXPORT_SYMBOL_GPL(platform_device_del);
585
586/**
587 * platform_device_register - add a platform-level device
588 * @pdev: platform device we're adding
589 */
590int platform_device_register(struct platform_device *pdev)
591{
592 device_initialize(&pdev->dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000593 setup_pdev_dma_masks(pdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000594 return platform_device_add(pdev);
595}
596EXPORT_SYMBOL_GPL(platform_device_register);
597
598/**
599 * platform_device_unregister - unregister a platform-level device
600 * @pdev: platform device we're unregistering
601 *
602 * Unregistration is done in 2 steps. First we release all resources
603 * and remove it from the subsystem, then we drop reference count by
604 * calling platform_device_put().
605 */
606void platform_device_unregister(struct platform_device *pdev)
607{
608 platform_device_del(pdev);
609 platform_device_put(pdev);
610}
611EXPORT_SYMBOL_GPL(platform_device_unregister);
612
613/**
614 * platform_device_register_full - add a platform-level device with
615 * resources and platform-specific data
616 *
617 * @pdevinfo: data used to create device
618 *
619 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
620 */
621struct platform_device *platform_device_register_full(
622 const struct platform_device_info *pdevinfo)
623{
624 int ret = -ENOMEM;
625 struct platform_device *pdev;
626
627 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
628 if (!pdev)
David Brazdil0f672f62019-12-10 10:32:29 +0000629 return ERR_PTR(-ENOMEM);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000630
631 pdev->dev.parent = pdevinfo->parent;
632 pdev->dev.fwnode = pdevinfo->fwnode;
David Brazdil0f672f62019-12-10 10:32:29 +0000633 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
634 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000635
636 if (pdevinfo->dma_mask) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200637 pdev->platform_dma_mask = pdevinfo->dma_mask;
638 pdev->dev.dma_mask = &pdev->platform_dma_mask;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000639 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
640 }
641
642 ret = platform_device_add_resources(pdev,
643 pdevinfo->res, pdevinfo->num_res);
644 if (ret)
645 goto err;
646
647 ret = platform_device_add_data(pdev,
648 pdevinfo->data, pdevinfo->size_data);
649 if (ret)
650 goto err;
651
652 if (pdevinfo->properties) {
653 ret = platform_device_add_properties(pdev,
654 pdevinfo->properties);
655 if (ret)
656 goto err;
657 }
658
659 ret = platform_device_add(pdev);
660 if (ret) {
661err:
662 ACPI_COMPANION_SET(&pdev->dev, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000663 platform_device_put(pdev);
664 return ERR_PTR(ret);
665 }
666
667 return pdev;
668}
669EXPORT_SYMBOL_GPL(platform_device_register_full);
670
671static int platform_drv_probe(struct device *_dev)
672{
673 struct platform_driver *drv = to_platform_driver(_dev->driver);
674 struct platform_device *dev = to_platform_device(_dev);
675 int ret;
676
677 ret = of_clk_set_defaults(_dev->of_node, false);
678 if (ret < 0)
679 return ret;
680
681 ret = dev_pm_domain_attach(_dev, true);
682 if (ret)
683 goto out;
684
685 if (drv->probe) {
686 ret = drv->probe(dev);
687 if (ret)
688 dev_pm_domain_detach(_dev, true);
689 }
690
691out:
692 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
693 dev_warn(_dev, "probe deferral not supported\n");
694 ret = -ENXIO;
695 }
696
697 return ret;
698}
699
700static int platform_drv_probe_fail(struct device *_dev)
701{
702 return -ENXIO;
703}
704
705static int platform_drv_remove(struct device *_dev)
706{
707 struct platform_driver *drv = to_platform_driver(_dev->driver);
708 struct platform_device *dev = to_platform_device(_dev);
709 int ret = 0;
710
711 if (drv->remove)
712 ret = drv->remove(dev);
713 dev_pm_domain_detach(_dev, true);
714
715 return ret;
716}
717
718static void platform_drv_shutdown(struct device *_dev)
719{
720 struct platform_driver *drv = to_platform_driver(_dev->driver);
721 struct platform_device *dev = to_platform_device(_dev);
722
723 if (drv->shutdown)
724 drv->shutdown(dev);
725}
726
727/**
728 * __platform_driver_register - register a driver for platform-level devices
729 * @drv: platform driver structure
730 * @owner: owning module/driver
731 */
732int __platform_driver_register(struct platform_driver *drv,
733 struct module *owner)
734{
735 drv->driver.owner = owner;
736 drv->driver.bus = &platform_bus_type;
737 drv->driver.probe = platform_drv_probe;
738 drv->driver.remove = platform_drv_remove;
739 drv->driver.shutdown = platform_drv_shutdown;
740
741 return driver_register(&drv->driver);
742}
743EXPORT_SYMBOL_GPL(__platform_driver_register);
744
745/**
746 * platform_driver_unregister - unregister a driver for platform-level devices
747 * @drv: platform driver structure
748 */
749void platform_driver_unregister(struct platform_driver *drv)
750{
751 driver_unregister(&drv->driver);
752}
753EXPORT_SYMBOL_GPL(platform_driver_unregister);
754
755/**
756 * __platform_driver_probe - register driver for non-hotpluggable device
757 * @drv: platform driver structure
758 * @probe: the driver probe routine, probably from an __init section
759 * @module: module which will be the owner of the driver
760 *
761 * Use this instead of platform_driver_register() when you know the device
762 * is not hotpluggable and has already been registered, and you want to
763 * remove its run-once probe() infrastructure from memory after the driver
764 * has bound to the device.
765 *
766 * One typical use for this would be with drivers for controllers integrated
767 * into system-on-chip processors, where the controller devices have been
768 * configured as part of board setup.
769 *
770 * Note that this is incompatible with deferred probing.
771 *
772 * Returns zero if the driver registered and bound to a device, else returns
773 * a negative error code and with the driver not registered.
774 */
775int __init_or_module __platform_driver_probe(struct platform_driver *drv,
776 int (*probe)(struct platform_device *), struct module *module)
777{
778 int retval, code;
779
780 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
781 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
782 drv->driver.name, __func__);
783 return -EINVAL;
784 }
785
786 /*
787 * We have to run our probes synchronously because we check if
788 * we find any devices to bind to and exit with error if there
789 * are any.
790 */
791 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
792
793 /*
794 * Prevent driver from requesting probe deferral to avoid further
795 * futile probe attempts.
796 */
797 drv->prevent_deferred_probe = true;
798
799 /* make sure driver won't have bind/unbind attributes */
800 drv->driver.suppress_bind_attrs = true;
801
802 /* temporary section violation during probe() */
803 drv->probe = probe;
804 retval = code = __platform_driver_register(drv, module);
Olivier Deprez0e641232021-09-23 10:07:05 +0200805 if (retval)
806 return retval;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000807
808 /*
809 * Fixup that section violation, being paranoid about code scanning
810 * the list of drivers in order to probe new devices. Check to see
811 * if the probe was successful, and make sure any forced probes of
812 * new devices fail.
813 */
814 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
815 drv->probe = NULL;
816 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
817 retval = -ENODEV;
818 drv->driver.probe = platform_drv_probe_fail;
819 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
820
821 if (code != retval)
822 platform_driver_unregister(drv);
823 return retval;
824}
825EXPORT_SYMBOL_GPL(__platform_driver_probe);
826
827/**
828 * __platform_create_bundle - register driver and create corresponding device
829 * @driver: platform driver structure
830 * @probe: the driver probe routine, probably from an __init section
831 * @res: set of resources that needs to be allocated for the device
832 * @n_res: number of resources
833 * @data: platform specific data for this platform device
834 * @size: size of platform specific data
835 * @module: module which will be the owner of the driver
836 *
837 * Use this in legacy-style modules that probe hardware directly and
838 * register a single platform device and corresponding platform driver.
839 *
840 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
841 */
842struct platform_device * __init_or_module __platform_create_bundle(
843 struct platform_driver *driver,
844 int (*probe)(struct platform_device *),
845 struct resource *res, unsigned int n_res,
846 const void *data, size_t size, struct module *module)
847{
848 struct platform_device *pdev;
849 int error;
850
851 pdev = platform_device_alloc(driver->driver.name, -1);
852 if (!pdev) {
853 error = -ENOMEM;
854 goto err_out;
855 }
856
857 error = platform_device_add_resources(pdev, res, n_res);
858 if (error)
859 goto err_pdev_put;
860
861 error = platform_device_add_data(pdev, data, size);
862 if (error)
863 goto err_pdev_put;
864
865 error = platform_device_add(pdev);
866 if (error)
867 goto err_pdev_put;
868
869 error = __platform_driver_probe(driver, probe, module);
870 if (error)
871 goto err_pdev_del;
872
873 return pdev;
874
875err_pdev_del:
876 platform_device_del(pdev);
877err_pdev_put:
878 platform_device_put(pdev);
879err_out:
880 return ERR_PTR(error);
881}
882EXPORT_SYMBOL_GPL(__platform_create_bundle);
883
884/**
885 * __platform_register_drivers - register an array of platform drivers
886 * @drivers: an array of drivers to register
887 * @count: the number of drivers to register
888 * @owner: module owning the drivers
889 *
890 * Registers platform drivers specified by an array. On failure to register a
891 * driver, all previously registered drivers will be unregistered. Callers of
892 * this API should use platform_unregister_drivers() to unregister drivers in
893 * the reverse order.
894 *
895 * Returns: 0 on success or a negative error code on failure.
896 */
897int __platform_register_drivers(struct platform_driver * const *drivers,
898 unsigned int count, struct module *owner)
899{
900 unsigned int i;
901 int err;
902
903 for (i = 0; i < count; i++) {
904 pr_debug("registering platform driver %ps\n", drivers[i]);
905
906 err = __platform_driver_register(drivers[i], owner);
907 if (err < 0) {
908 pr_err("failed to register platform driver %ps: %d\n",
909 drivers[i], err);
910 goto error;
911 }
912 }
913
914 return 0;
915
916error:
917 while (i--) {
918 pr_debug("unregistering platform driver %ps\n", drivers[i]);
919 platform_driver_unregister(drivers[i]);
920 }
921
922 return err;
923}
924EXPORT_SYMBOL_GPL(__platform_register_drivers);
925
926/**
927 * platform_unregister_drivers - unregister an array of platform drivers
928 * @drivers: an array of drivers to unregister
929 * @count: the number of drivers to unregister
930 *
931 * Unegisters platform drivers specified by an array. This is typically used
932 * to complement an earlier call to platform_register_drivers(). Drivers are
933 * unregistered in the reverse order in which they were registered.
934 */
935void platform_unregister_drivers(struct platform_driver * const *drivers,
936 unsigned int count)
937{
938 while (count--) {
939 pr_debug("unregistering platform driver %ps\n", drivers[count]);
940 platform_driver_unregister(drivers[count]);
941 }
942}
943EXPORT_SYMBOL_GPL(platform_unregister_drivers);
944
945/* modalias support enables more hands-off userspace setup:
946 * (a) environment variable lets new-style hotplug events work once system is
947 * fully running: "modprobe $MODALIAS"
948 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
949 * mishandled before system is fully running: "modprobe $(cat modalias)"
950 */
951static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
952 char *buf)
953{
954 struct platform_device *pdev = to_platform_device(dev);
955 int len;
956
957 len = of_device_modalias(dev, buf, PAGE_SIZE);
958 if (len != -ENODEV)
959 return len;
960
961 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
962 if (len != -ENODEV)
963 return len;
964
965 len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
966
967 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
968}
969static DEVICE_ATTR_RO(modalias);
970
971static ssize_t driver_override_store(struct device *dev,
972 struct device_attribute *attr,
973 const char *buf, size_t count)
974{
975 struct platform_device *pdev = to_platform_device(dev);
976 char *driver_override, *old, *cp;
977
978 /* We need to keep extra room for a newline */
979 if (count >= (PAGE_SIZE - 1))
980 return -EINVAL;
981
982 driver_override = kstrndup(buf, count, GFP_KERNEL);
983 if (!driver_override)
984 return -ENOMEM;
985
986 cp = strchr(driver_override, '\n');
987 if (cp)
988 *cp = '\0';
989
990 device_lock(dev);
991 old = pdev->driver_override;
992 if (strlen(driver_override)) {
993 pdev->driver_override = driver_override;
994 } else {
995 kfree(driver_override);
996 pdev->driver_override = NULL;
997 }
998 device_unlock(dev);
999
1000 kfree(old);
1001
1002 return count;
1003}
1004
1005static ssize_t driver_override_show(struct device *dev,
1006 struct device_attribute *attr, char *buf)
1007{
1008 struct platform_device *pdev = to_platform_device(dev);
1009 ssize_t len;
1010
1011 device_lock(dev);
1012 len = sprintf(buf, "%s\n", pdev->driver_override);
1013 device_unlock(dev);
1014 return len;
1015}
1016static DEVICE_ATTR_RW(driver_override);
1017
1018
1019static struct attribute *platform_dev_attrs[] = {
1020 &dev_attr_modalias.attr,
1021 &dev_attr_driver_override.attr,
1022 NULL,
1023};
1024ATTRIBUTE_GROUPS(platform_dev);
1025
1026static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1027{
1028 struct platform_device *pdev = to_platform_device(dev);
1029 int rc;
1030
1031 /* Some devices have extra OF data and an OF-style MODALIAS */
1032 rc = of_device_uevent_modalias(dev, env);
1033 if (rc != -ENODEV)
1034 return rc;
1035
1036 rc = acpi_device_uevent_modalias(dev, env);
1037 if (rc != -ENODEV)
1038 return rc;
1039
1040 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1041 pdev->name);
1042 return 0;
1043}
1044
1045static const struct platform_device_id *platform_match_id(
1046 const struct platform_device_id *id,
1047 struct platform_device *pdev)
1048{
1049 while (id->name[0]) {
1050 if (strcmp(pdev->name, id->name) == 0) {
1051 pdev->id_entry = id;
1052 return id;
1053 }
1054 id++;
1055 }
1056 return NULL;
1057}
1058
1059/**
1060 * platform_match - bind platform device to platform driver.
1061 * @dev: device.
1062 * @drv: driver.
1063 *
1064 * Platform device IDs are assumed to be encoded like this:
1065 * "<name><instance>", where <name> is a short description of the type of
1066 * device, like "pci" or "floppy", and <instance> is the enumerated
1067 * instance of the device, like '0' or '42'. Driver IDs are simply
1068 * "<name>". So, extract the <name> from the platform_device structure,
1069 * and compare it against the name of the driver. Return whether they match
1070 * or not.
1071 */
1072static int platform_match(struct device *dev, struct device_driver *drv)
1073{
1074 struct platform_device *pdev = to_platform_device(dev);
1075 struct platform_driver *pdrv = to_platform_driver(drv);
1076
1077 /* When driver_override is set, only bind to the matching driver */
1078 if (pdev->driver_override)
1079 return !strcmp(pdev->driver_override, drv->name);
1080
1081 /* Attempt an OF style match first */
1082 if (of_driver_match_device(dev, drv))
1083 return 1;
1084
1085 /* Then try ACPI style match */
1086 if (acpi_driver_match_device(dev, drv))
1087 return 1;
1088
1089 /* Then try to match against the id table */
1090 if (pdrv->id_table)
1091 return platform_match_id(pdrv->id_table, pdev) != NULL;
1092
1093 /* fall-back to driver name match */
1094 return (strcmp(pdev->name, drv->name) == 0);
1095}
1096
1097#ifdef CONFIG_PM_SLEEP
1098
1099static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1100{
1101 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1102 struct platform_device *pdev = to_platform_device(dev);
1103 int ret = 0;
1104
1105 if (dev->driver && pdrv->suspend)
1106 ret = pdrv->suspend(pdev, mesg);
1107
1108 return ret;
1109}
1110
1111static int platform_legacy_resume(struct device *dev)
1112{
1113 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1114 struct platform_device *pdev = to_platform_device(dev);
1115 int ret = 0;
1116
1117 if (dev->driver && pdrv->resume)
1118 ret = pdrv->resume(pdev);
1119
1120 return ret;
1121}
1122
1123#endif /* CONFIG_PM_SLEEP */
1124
1125#ifdef CONFIG_SUSPEND
1126
1127int platform_pm_suspend(struct device *dev)
1128{
1129 struct device_driver *drv = dev->driver;
1130 int ret = 0;
1131
1132 if (!drv)
1133 return 0;
1134
1135 if (drv->pm) {
1136 if (drv->pm->suspend)
1137 ret = drv->pm->suspend(dev);
1138 } else {
1139 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1140 }
1141
1142 return ret;
1143}
1144
1145int platform_pm_resume(struct device *dev)
1146{
1147 struct device_driver *drv = dev->driver;
1148 int ret = 0;
1149
1150 if (!drv)
1151 return 0;
1152
1153 if (drv->pm) {
1154 if (drv->pm->resume)
1155 ret = drv->pm->resume(dev);
1156 } else {
1157 ret = platform_legacy_resume(dev);
1158 }
1159
1160 return ret;
1161}
1162
1163#endif /* CONFIG_SUSPEND */
1164
1165#ifdef CONFIG_HIBERNATE_CALLBACKS
1166
1167int platform_pm_freeze(struct device *dev)
1168{
1169 struct device_driver *drv = dev->driver;
1170 int ret = 0;
1171
1172 if (!drv)
1173 return 0;
1174
1175 if (drv->pm) {
1176 if (drv->pm->freeze)
1177 ret = drv->pm->freeze(dev);
1178 } else {
1179 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1180 }
1181
1182 return ret;
1183}
1184
1185int platform_pm_thaw(struct device *dev)
1186{
1187 struct device_driver *drv = dev->driver;
1188 int ret = 0;
1189
1190 if (!drv)
1191 return 0;
1192
1193 if (drv->pm) {
1194 if (drv->pm->thaw)
1195 ret = drv->pm->thaw(dev);
1196 } else {
1197 ret = platform_legacy_resume(dev);
1198 }
1199
1200 return ret;
1201}
1202
1203int platform_pm_poweroff(struct device *dev)
1204{
1205 struct device_driver *drv = dev->driver;
1206 int ret = 0;
1207
1208 if (!drv)
1209 return 0;
1210
1211 if (drv->pm) {
1212 if (drv->pm->poweroff)
1213 ret = drv->pm->poweroff(dev);
1214 } else {
1215 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1216 }
1217
1218 return ret;
1219}
1220
1221int platform_pm_restore(struct device *dev)
1222{
1223 struct device_driver *drv = dev->driver;
1224 int ret = 0;
1225
1226 if (!drv)
1227 return 0;
1228
1229 if (drv->pm) {
1230 if (drv->pm->restore)
1231 ret = drv->pm->restore(dev);
1232 } else {
1233 ret = platform_legacy_resume(dev);
1234 }
1235
1236 return ret;
1237}
1238
1239#endif /* CONFIG_HIBERNATE_CALLBACKS */
1240
1241int platform_dma_configure(struct device *dev)
1242{
1243 enum dev_dma_attr attr;
1244 int ret = 0;
1245
1246 if (dev->of_node) {
1247 ret = of_dma_configure(dev, dev->of_node, true);
1248 } else if (has_acpi_companion(dev)) {
1249 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
David Brazdil0f672f62019-12-10 10:32:29 +00001250 ret = acpi_dma_configure(dev, attr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001251 }
1252
1253 return ret;
1254}
1255
1256static const struct dev_pm_ops platform_dev_pm_ops = {
1257 .runtime_suspend = pm_generic_runtime_suspend,
1258 .runtime_resume = pm_generic_runtime_resume,
1259 USE_PLATFORM_PM_SLEEP_OPS
1260};
1261
1262struct bus_type platform_bus_type = {
1263 .name = "platform",
1264 .dev_groups = platform_dev_groups,
1265 .match = platform_match,
1266 .uevent = platform_uevent,
1267 .dma_configure = platform_dma_configure,
1268 .pm = &platform_dev_pm_ops,
1269};
1270EXPORT_SYMBOL_GPL(platform_bus_type);
1271
David Brazdil0f672f62019-12-10 10:32:29 +00001272static inline int __platform_match(struct device *dev, const void *drv)
1273{
1274 return platform_match(dev, (struct device_driver *)drv);
1275}
1276
1277/**
1278 * platform_find_device_by_driver - Find a platform device with a given
1279 * driver.
1280 * @start: The device to start the search from.
1281 * @drv: The device driver to look for.
1282 */
1283struct device *platform_find_device_by_driver(struct device *start,
1284 const struct device_driver *drv)
1285{
1286 return bus_find_device(&platform_bus_type, start, drv,
1287 __platform_match);
1288}
1289EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1290
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001291int __init platform_bus_init(void)
1292{
1293 int error;
1294
1295 early_platform_cleanup();
1296
1297 error = device_register(&platform_bus);
1298 if (error) {
1299 put_device(&platform_bus);
1300 return error;
1301 }
1302 error = bus_register(&platform_bus_type);
1303 if (error)
1304 device_unregister(&platform_bus);
1305 of_platform_register_reconfig_notifier();
1306 return error;
1307}
1308
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001309static __initdata LIST_HEAD(early_platform_driver_list);
1310static __initdata LIST_HEAD(early_platform_device_list);
1311
1312/**
1313 * early_platform_driver_register - register early platform driver
1314 * @epdrv: early_platform driver structure
1315 * @buf: string passed from early_param()
1316 *
1317 * Helper function for early_platform_init() / early_platform_init_buffer()
1318 */
1319int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1320 char *buf)
1321{
1322 char *tmp;
1323 int n;
1324
1325 /* Simply add the driver to the end of the global list.
1326 * Drivers will by default be put on the list in compiled-in order.
1327 */
1328 if (!epdrv->list.next) {
1329 INIT_LIST_HEAD(&epdrv->list);
1330 list_add_tail(&epdrv->list, &early_platform_driver_list);
1331 }
1332
1333 /* If the user has specified device then make sure the driver
1334 * gets prioritized. The driver of the last device specified on
1335 * command line will be put first on the list.
1336 */
1337 n = strlen(epdrv->pdrv->driver.name);
1338 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1339 list_move(&epdrv->list, &early_platform_driver_list);
1340
1341 /* Allow passing parameters after device name */
1342 if (buf[n] == '\0' || buf[n] == ',')
1343 epdrv->requested_id = -1;
1344 else {
1345 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1346 &tmp, 10);
1347
1348 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1349 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1350 n = 0;
1351 } else
1352 n += strcspn(&buf[n + 1], ",") + 1;
1353 }
1354
1355 if (buf[n] == ',')
1356 n++;
1357
1358 if (epdrv->bufsize) {
1359 memcpy(epdrv->buffer, &buf[n],
1360 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1361 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1362 }
1363 }
1364
1365 return 0;
1366}
1367
1368/**
1369 * early_platform_add_devices - adds a number of early platform devices
1370 * @devs: array of early platform devices to add
1371 * @num: number of early platform devices in array
1372 *
1373 * Used by early architecture code to register early platform devices and
1374 * their platform data.
1375 */
1376void __init early_platform_add_devices(struct platform_device **devs, int num)
1377{
1378 struct device *dev;
1379 int i;
1380
1381 /* simply add the devices to list */
1382 for (i = 0; i < num; i++) {
1383 dev = &devs[i]->dev;
1384
1385 if (!dev->devres_head.next) {
1386 pm_runtime_early_init(dev);
1387 INIT_LIST_HEAD(&dev->devres_head);
1388 list_add_tail(&dev->devres_head,
1389 &early_platform_device_list);
1390 }
1391 }
1392}
1393
1394/**
1395 * early_platform_driver_register_all - register early platform drivers
1396 * @class_str: string to identify early platform driver class
1397 *
1398 * Used by architecture code to register all early platform drivers
1399 * for a certain class. If omitted then only early platform drivers
1400 * with matching kernel command line class parameters will be registered.
1401 */
1402void __init early_platform_driver_register_all(char *class_str)
1403{
1404 /* The "class_str" parameter may or may not be present on the kernel
1405 * command line. If it is present then there may be more than one
1406 * matching parameter.
1407 *
1408 * Since we register our early platform drivers using early_param()
1409 * we need to make sure that they also get registered in the case
1410 * when the parameter is missing from the kernel command line.
1411 *
1412 * We use parse_early_options() to make sure the early_param() gets
1413 * called at least once. The early_param() may be called more than
1414 * once since the name of the preferred device may be specified on
1415 * the kernel command line. early_platform_driver_register() handles
1416 * this case for us.
1417 */
1418 parse_early_options(class_str);
1419}
1420
1421/**
1422 * early_platform_match - find early platform device matching driver
1423 * @epdrv: early platform driver structure
1424 * @id: id to match against
1425 */
1426static struct platform_device * __init
1427early_platform_match(struct early_platform_driver *epdrv, int id)
1428{
1429 struct platform_device *pd;
1430
1431 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1432 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1433 if (pd->id == id)
1434 return pd;
1435
1436 return NULL;
1437}
1438
1439/**
1440 * early_platform_left - check if early platform driver has matching devices
1441 * @epdrv: early platform driver structure
1442 * @id: return true if id or above exists
1443 */
1444static int __init early_platform_left(struct early_platform_driver *epdrv,
1445 int id)
1446{
1447 struct platform_device *pd;
1448
1449 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1450 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1451 if (pd->id >= id)
1452 return 1;
1453
1454 return 0;
1455}
1456
1457/**
1458 * early_platform_driver_probe_id - probe drivers matching class_str and id
1459 * @class_str: string to identify early platform driver class
1460 * @id: id to match against
1461 * @nr_probe: number of platform devices to successfully probe before exiting
1462 */
1463static int __init early_platform_driver_probe_id(char *class_str,
1464 int id,
1465 int nr_probe)
1466{
1467 struct early_platform_driver *epdrv;
1468 struct platform_device *match;
1469 int match_id;
1470 int n = 0;
1471 int left = 0;
1472
1473 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1474 /* only use drivers matching our class_str */
1475 if (strcmp(class_str, epdrv->class_str))
1476 continue;
1477
1478 if (id == -2) {
1479 match_id = epdrv->requested_id;
1480 left = 1;
1481
1482 } else {
1483 match_id = id;
1484 left += early_platform_left(epdrv, id);
1485
1486 /* skip requested id */
1487 switch (epdrv->requested_id) {
1488 case EARLY_PLATFORM_ID_ERROR:
1489 case EARLY_PLATFORM_ID_UNSET:
1490 break;
1491 default:
1492 if (epdrv->requested_id == id)
1493 match_id = EARLY_PLATFORM_ID_UNSET;
1494 }
1495 }
1496
1497 switch (match_id) {
1498 case EARLY_PLATFORM_ID_ERROR:
1499 pr_warn("%s: unable to parse %s parameter\n",
1500 class_str, epdrv->pdrv->driver.name);
1501 /* fall-through */
1502 case EARLY_PLATFORM_ID_UNSET:
1503 match = NULL;
1504 break;
1505 default:
1506 match = early_platform_match(epdrv, match_id);
1507 }
1508
1509 if (match) {
1510 /*
1511 * Set up a sensible init_name to enable
1512 * dev_name() and others to be used before the
1513 * rest of the driver core is initialized.
1514 */
1515 if (!match->dev.init_name && slab_is_available()) {
1516 if (match->id != -1)
1517 match->dev.init_name =
1518 kasprintf(GFP_KERNEL, "%s.%d",
1519 match->name,
1520 match->id);
1521 else
1522 match->dev.init_name =
1523 kasprintf(GFP_KERNEL, "%s",
1524 match->name);
1525
1526 if (!match->dev.init_name)
1527 return -ENOMEM;
1528 }
1529
1530 if (epdrv->pdrv->probe(match))
1531 pr_warn("%s: unable to probe %s early.\n",
1532 class_str, match->name);
1533 else
1534 n++;
1535 }
1536
1537 if (n >= nr_probe)
1538 break;
1539 }
1540
1541 if (left)
1542 return n;
1543 else
1544 return -ENODEV;
1545}
1546
1547/**
1548 * early_platform_driver_probe - probe a class of registered drivers
1549 * @class_str: string to identify early platform driver class
1550 * @nr_probe: number of platform devices to successfully probe before exiting
1551 * @user_only: only probe user specified early platform devices
1552 *
1553 * Used by architecture code to probe registered early platform drivers
1554 * within a certain class. For probe to happen a registered early platform
1555 * device matching a registered early platform driver is needed.
1556 */
1557int __init early_platform_driver_probe(char *class_str,
1558 int nr_probe,
1559 int user_only)
1560{
1561 int k, n, i;
1562
1563 n = 0;
1564 for (i = -2; n < nr_probe; i++) {
1565 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1566
1567 if (k < 0)
1568 break;
1569
1570 n += k;
1571
1572 if (user_only)
1573 break;
1574 }
1575
1576 return n;
1577}
1578
1579/**
1580 * early_platform_cleanup - clean up early platform code
1581 */
1582void __init early_platform_cleanup(void)
1583{
1584 struct platform_device *pd, *pd2;
1585
1586 /* clean up the devres list used to chain devices */
1587 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1588 dev.devres_head) {
1589 list_del(&pd->dev.devres_head);
1590 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1591 }
1592}
1593