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