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