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