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