xref: /linux-6.15/drivers/base/platform.c (revision be576734)
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-api/driver-model/platform.rst 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/memblock.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 
31 #include "base.h"
32 #include "power/power.h"
33 
34 /* For automatically allocated device IDs */
35 static DEFINE_IDA(platform_devid_ida);
36 
37 struct device platform_bus = {
38 	.init_name	= "platform",
39 };
40 EXPORT_SYMBOL_GPL(platform_bus);
41 
42 /**
43  * platform_get_resource - get a resource for a device
44  * @dev: platform device
45  * @type: resource type
46  * @num: resource index
47  */
48 struct resource *platform_get_resource(struct platform_device *dev,
49 				       unsigned int type, unsigned int num)
50 {
51 	int i;
52 
53 	for (i = 0; i < dev->num_resources; i++) {
54 		struct resource *r = &dev->resource[i];
55 
56 		if (type == resource_type(r) && num-- == 0)
57 			return r;
58 	}
59 	return NULL;
60 }
61 EXPORT_SYMBOL_GPL(platform_get_resource);
62 
63 /**
64  * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
65  *				    device
66  *
67  * @pdev: platform device to use both for memory resource lookup as well as
68  *        resource management
69  * @index: resource index
70  */
71 #ifdef CONFIG_HAS_IOMEM
72 void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
73 					     unsigned int index)
74 {
75 	struct resource *res;
76 
77 	res = platform_get_resource(pdev, IORESOURCE_MEM, index);
78 	return devm_ioremap_resource(&pdev->dev, res);
79 }
80 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
81 #endif /* CONFIG_HAS_IOMEM */
82 
83 /**
84  * platform_get_irq_optional - get an optional IRQ for a device
85  * @dev: platform device
86  * @num: IRQ number index
87  *
88  * Gets an IRQ for a platform device. Device drivers should check the return
89  * value for errors so as to not pass a negative integer value to the
90  * request_irq() APIs. This is the same as platform_get_irq(), except that it
91  * does not print an error message if an IRQ can not be obtained.
92  *
93  * Example:
94  *		int irq = platform_get_irq_optional(pdev, 0);
95  *		if (irq < 0)
96  *			return irq;
97  *
98  * Return: IRQ number on success, negative error number on failure.
99  */
100 int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
101 {
102 #ifdef CONFIG_SPARC
103 	/* sparc does not have irqs represented as IORESOURCE_IRQ resources */
104 	if (!dev || num >= dev->archdata.num_irqs)
105 		return -ENXIO;
106 	return dev->archdata.irqs[num];
107 #else
108 	struct resource *r;
109 	if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
110 		int ret;
111 
112 		ret = of_irq_get(dev->dev.of_node, num);
113 		if (ret > 0 || ret == -EPROBE_DEFER)
114 			return ret;
115 	}
116 
117 	r = platform_get_resource(dev, IORESOURCE_IRQ, num);
118 	if (has_acpi_companion(&dev->dev)) {
119 		if (r && r->flags & IORESOURCE_DISABLED) {
120 			int ret;
121 
122 			ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
123 			if (ret)
124 				return ret;
125 		}
126 	}
127 
128 	/*
129 	 * The resources may pass trigger flags to the irqs that need
130 	 * to be set up. It so happens that the trigger flags for
131 	 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
132 	 * settings.
133 	 */
134 	if (r && r->flags & IORESOURCE_BITS) {
135 		struct irq_data *irqd;
136 
137 		irqd = irq_get_irq_data(r->start);
138 		if (!irqd)
139 			return -ENXIO;
140 		irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
141 	}
142 
143 	if (r)
144 		return r->start;
145 
146 	/*
147 	 * For the index 0 interrupt, allow falling back to GpioInt
148 	 * resources. While a device could have both Interrupt and GpioInt
149 	 * resources, making this fallback ambiguous, in many common cases
150 	 * the device will only expose one IRQ, and this fallback
151 	 * allows a common code path across either kind of resource.
152 	 */
153 	if (num == 0 && has_acpi_companion(&dev->dev)) {
154 		int ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
155 
156 		/* Our callers expect -ENXIO for missing IRQs. */
157 		if (ret >= 0 || ret == -EPROBE_DEFER)
158 			return ret;
159 	}
160 
161 	return -ENXIO;
162 #endif
163 }
164 EXPORT_SYMBOL_GPL(platform_get_irq_optional);
165 
166 /**
167  * platform_get_irq - get an IRQ for a device
168  * @dev: platform device
169  * @num: IRQ number index
170  *
171  * Gets an IRQ for a platform device and prints an error message if finding the
172  * IRQ fails. Device drivers should check the return value for errors so as to
173  * not pass a negative integer value to the request_irq() APIs.
174  *
175  * Example:
176  *		int irq = platform_get_irq(pdev, 0);
177  *		if (irq < 0)
178  *			return irq;
179  *
180  * Return: IRQ number on success, negative error number on failure.
181  */
182 int platform_get_irq(struct platform_device *dev, unsigned int num)
183 {
184 	int ret;
185 
186 	ret = platform_get_irq_optional(dev, num);
187 	if (ret < 0 && ret != -EPROBE_DEFER)
188 		dev_err(&dev->dev, "IRQ index %u not found\n", num);
189 
190 	return ret;
191 }
192 EXPORT_SYMBOL_GPL(platform_get_irq);
193 
194 /**
195  * platform_irq_count - Count the number of IRQs a platform device uses
196  * @dev: platform device
197  *
198  * Return: Number of IRQs a platform device uses or EPROBE_DEFER
199  */
200 int platform_irq_count(struct platform_device *dev)
201 {
202 	int ret, nr = 0;
203 
204 	while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
205 		nr++;
206 
207 	if (ret == -EPROBE_DEFER)
208 		return ret;
209 
210 	return nr;
211 }
212 EXPORT_SYMBOL_GPL(platform_irq_count);
213 
214 /**
215  * platform_get_resource_byname - get a resource for a device by name
216  * @dev: platform device
217  * @type: resource type
218  * @name: resource name
219  */
220 struct resource *platform_get_resource_byname(struct platform_device *dev,
221 					      unsigned int type,
222 					      const char *name)
223 {
224 	int i;
225 
226 	for (i = 0; i < dev->num_resources; i++) {
227 		struct resource *r = &dev->resource[i];
228 
229 		if (unlikely(!r->name))
230 			continue;
231 
232 		if (type == resource_type(r) && !strcmp(r->name, name))
233 			return r;
234 	}
235 	return NULL;
236 }
237 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
238 
239 /**
240  * platform_get_irq_byname - get an IRQ for a device by name
241  * @dev: platform device
242  * @name: IRQ name
243  */
244 int platform_get_irq_byname(struct platform_device *dev, const char *name)
245 {
246 	struct resource *r;
247 
248 	if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
249 		int ret;
250 
251 		ret = of_irq_get_byname(dev->dev.of_node, name);
252 		if (ret > 0 || ret == -EPROBE_DEFER)
253 			return ret;
254 	}
255 
256 	r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
257 	if (r)
258 		return r->start;
259 
260 	dev_err(&dev->dev, "IRQ %s not found\n", name);
261 	return -ENXIO;
262 }
263 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
264 
265 /**
266  * platform_add_devices - add a numbers of platform devices
267  * @devs: array of platform devices to add
268  * @num: number of platform devices in array
269  */
270 int platform_add_devices(struct platform_device **devs, int num)
271 {
272 	int i, ret = 0;
273 
274 	for (i = 0; i < num; i++) {
275 		ret = platform_device_register(devs[i]);
276 		if (ret) {
277 			while (--i >= 0)
278 				platform_device_unregister(devs[i]);
279 			break;
280 		}
281 	}
282 
283 	return ret;
284 }
285 EXPORT_SYMBOL_GPL(platform_add_devices);
286 
287 struct platform_object {
288 	struct platform_device pdev;
289 	char name[];
290 };
291 
292 /*
293  * Set up default DMA mask for platform devices if the they weren't
294  * previously set by the architecture / DT.
295  */
296 static void setup_pdev_dma_masks(struct platform_device *pdev)
297 {
298 	if (!pdev->dev.coherent_dma_mask)
299 		pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
300 	if (!pdev->dma_mask)
301 		pdev->dma_mask = DMA_BIT_MASK(32);
302 	if (!pdev->dev.dma_mask)
303 		pdev->dev.dma_mask = &pdev->dma_mask;
304 };
305 
306 /**
307  * platform_device_put - destroy a platform device
308  * @pdev: platform device to free
309  *
310  * Free all memory associated with a platform device.  This function must
311  * _only_ be externally called in error cases.  All other usage is a bug.
312  */
313 void platform_device_put(struct platform_device *pdev)
314 {
315 	if (!IS_ERR_OR_NULL(pdev))
316 		put_device(&pdev->dev);
317 }
318 EXPORT_SYMBOL_GPL(platform_device_put);
319 
320 static void platform_device_release(struct device *dev)
321 {
322 	struct platform_object *pa = container_of(dev, struct platform_object,
323 						  pdev.dev);
324 
325 	of_device_node_put(&pa->pdev.dev);
326 	kfree(pa->pdev.dev.platform_data);
327 	kfree(pa->pdev.mfd_cell);
328 	kfree(pa->pdev.resource);
329 	kfree(pa->pdev.driver_override);
330 	kfree(pa);
331 }
332 
333 /**
334  * platform_device_alloc - create a platform device
335  * @name: base name of the device we're adding
336  * @id: instance id
337  *
338  * Create a platform device object which can have other objects attached
339  * to it, and which will have attached objects freed when it is released.
340  */
341 struct platform_device *platform_device_alloc(const char *name, int id)
342 {
343 	struct platform_object *pa;
344 
345 	pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
346 	if (pa) {
347 		strcpy(pa->name, name);
348 		pa->pdev.name = pa->name;
349 		pa->pdev.id = id;
350 		device_initialize(&pa->pdev.dev);
351 		pa->pdev.dev.release = platform_device_release;
352 		setup_pdev_dma_masks(&pa->pdev);
353 	}
354 
355 	return pa ? &pa->pdev : NULL;
356 }
357 EXPORT_SYMBOL_GPL(platform_device_alloc);
358 
359 /**
360  * platform_device_add_resources - add resources to a platform device
361  * @pdev: platform device allocated by platform_device_alloc to add resources to
362  * @res: set of resources that needs to be allocated for the device
363  * @num: number of resources
364  *
365  * Add a copy of the resources to the platform device.  The memory
366  * associated with the resources will be freed when the platform device is
367  * released.
368  */
369 int platform_device_add_resources(struct platform_device *pdev,
370 				  const struct resource *res, unsigned int num)
371 {
372 	struct resource *r = NULL;
373 
374 	if (res) {
375 		r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
376 		if (!r)
377 			return -ENOMEM;
378 	}
379 
380 	kfree(pdev->resource);
381 	pdev->resource = r;
382 	pdev->num_resources = num;
383 	return 0;
384 }
385 EXPORT_SYMBOL_GPL(platform_device_add_resources);
386 
387 /**
388  * platform_device_add_data - add platform-specific data to a platform device
389  * @pdev: platform device allocated by platform_device_alloc to add resources to
390  * @data: platform specific data for this platform device
391  * @size: size of platform specific data
392  *
393  * Add a copy of platform specific data to the platform device's
394  * platform_data pointer.  The memory associated with the platform data
395  * will be freed when the platform device is released.
396  */
397 int platform_device_add_data(struct platform_device *pdev, const void *data,
398 			     size_t size)
399 {
400 	void *d = NULL;
401 
402 	if (data) {
403 		d = kmemdup(data, size, GFP_KERNEL);
404 		if (!d)
405 			return -ENOMEM;
406 	}
407 
408 	kfree(pdev->dev.platform_data);
409 	pdev->dev.platform_data = d;
410 	return 0;
411 }
412 EXPORT_SYMBOL_GPL(platform_device_add_data);
413 
414 /**
415  * platform_device_add_properties - add built-in properties to a platform device
416  * @pdev: platform device to add properties to
417  * @properties: null terminated array of properties to add
418  *
419  * The function will take deep copy of @properties and attach the copy to the
420  * platform device. The memory associated with properties will be freed when the
421  * platform device is released.
422  */
423 int platform_device_add_properties(struct platform_device *pdev,
424 				   const struct property_entry *properties)
425 {
426 	return device_add_properties(&pdev->dev, properties);
427 }
428 EXPORT_SYMBOL_GPL(platform_device_add_properties);
429 
430 /**
431  * platform_device_add - add a platform device to device hierarchy
432  * @pdev: platform device we're adding
433  *
434  * This is part 2 of platform_device_register(), though may be called
435  * separately _iff_ pdev was allocated by platform_device_alloc().
436  */
437 int platform_device_add(struct platform_device *pdev)
438 {
439 	int i, ret;
440 
441 	if (!pdev)
442 		return -EINVAL;
443 
444 	if (!pdev->dev.parent)
445 		pdev->dev.parent = &platform_bus;
446 
447 	pdev->dev.bus = &platform_bus_type;
448 
449 	switch (pdev->id) {
450 	default:
451 		dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);
452 		break;
453 	case PLATFORM_DEVID_NONE:
454 		dev_set_name(&pdev->dev, "%s", pdev->name);
455 		break;
456 	case PLATFORM_DEVID_AUTO:
457 		/*
458 		 * Automatically allocated device ID. We mark it as such so
459 		 * that we remember it must be freed, and we append a suffix
460 		 * to avoid namespace collision with explicit IDs.
461 		 */
462 		ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
463 		if (ret < 0)
464 			goto err_out;
465 		pdev->id = ret;
466 		pdev->id_auto = true;
467 		dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
468 		break;
469 	}
470 
471 	for (i = 0; i < pdev->num_resources; i++) {
472 		struct resource *p, *r = &pdev->resource[i];
473 
474 		if (r->name == NULL)
475 			r->name = dev_name(&pdev->dev);
476 
477 		p = r->parent;
478 		if (!p) {
479 			if (resource_type(r) == IORESOURCE_MEM)
480 				p = &iomem_resource;
481 			else if (resource_type(r) == IORESOURCE_IO)
482 				p = &ioport_resource;
483 		}
484 
485 		if (p) {
486 			ret = insert_resource(p, r);
487 			if (ret) {
488 				dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
489 				goto failed;
490 			}
491 		}
492 	}
493 
494 	pr_debug("Registering platform device '%s'. Parent at %s\n",
495 		 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
496 
497 	ret = device_add(&pdev->dev);
498 	if (ret == 0)
499 		return ret;
500 
501  failed:
502 	if (pdev->id_auto) {
503 		ida_simple_remove(&platform_devid_ida, pdev->id);
504 		pdev->id = PLATFORM_DEVID_AUTO;
505 	}
506 
507 	while (--i >= 0) {
508 		struct resource *r = &pdev->resource[i];
509 		if (r->parent)
510 			release_resource(r);
511 	}
512 
513  err_out:
514 	return ret;
515 }
516 EXPORT_SYMBOL_GPL(platform_device_add);
517 
518 /**
519  * platform_device_del - remove a platform-level device
520  * @pdev: platform device we're removing
521  *
522  * Note that this function will also release all memory- and port-based
523  * resources owned by the device (@dev->resource).  This function must
524  * _only_ be externally called in error cases.  All other usage is a bug.
525  */
526 void platform_device_del(struct platform_device *pdev)
527 {
528 	int i;
529 
530 	if (!IS_ERR_OR_NULL(pdev)) {
531 		device_del(&pdev->dev);
532 
533 		if (pdev->id_auto) {
534 			ida_simple_remove(&platform_devid_ida, pdev->id);
535 			pdev->id = PLATFORM_DEVID_AUTO;
536 		}
537 
538 		for (i = 0; i < pdev->num_resources; i++) {
539 			struct resource *r = &pdev->resource[i];
540 			if (r->parent)
541 				release_resource(r);
542 		}
543 	}
544 }
545 EXPORT_SYMBOL_GPL(platform_device_del);
546 
547 /**
548  * platform_device_register - add a platform-level device
549  * @pdev: platform device we're adding
550  */
551 int platform_device_register(struct platform_device *pdev)
552 {
553 	device_initialize(&pdev->dev);
554 	setup_pdev_dma_masks(pdev);
555 	return platform_device_add(pdev);
556 }
557 EXPORT_SYMBOL_GPL(platform_device_register);
558 
559 /**
560  * platform_device_unregister - unregister a platform-level device
561  * @pdev: platform device we're unregistering
562  *
563  * Unregistration is done in 2 steps. First we release all resources
564  * and remove it from the subsystem, then we drop reference count by
565  * calling platform_device_put().
566  */
567 void platform_device_unregister(struct platform_device *pdev)
568 {
569 	platform_device_del(pdev);
570 	platform_device_put(pdev);
571 }
572 EXPORT_SYMBOL_GPL(platform_device_unregister);
573 
574 /**
575  * platform_device_register_full - add a platform-level device with
576  * resources and platform-specific data
577  *
578  * @pdevinfo: data used to create device
579  *
580  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
581  */
582 struct platform_device *platform_device_register_full(
583 		const struct platform_device_info *pdevinfo)
584 {
585 	int ret = -ENOMEM;
586 	struct platform_device *pdev;
587 
588 	pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
589 	if (!pdev)
590 		return ERR_PTR(-ENOMEM);
591 
592 	pdev->dev.parent = pdevinfo->parent;
593 	pdev->dev.fwnode = pdevinfo->fwnode;
594 	pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
595 	pdev->dev.of_node_reused = pdevinfo->of_node_reused;
596 
597 	if (pdevinfo->dma_mask) {
598 		/*
599 		 * This memory isn't freed when the device is put,
600 		 * I don't have a nice idea for that though.  Conceptually
601 		 * dma_mask in struct device should not be a pointer.
602 		 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
603 		 */
604 		pdev->dev.dma_mask =
605 			kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
606 		if (!pdev->dev.dma_mask)
607 			goto err;
608 
609 		kmemleak_ignore(pdev->dev.dma_mask);
610 
611 		*pdev->dev.dma_mask = pdevinfo->dma_mask;
612 		pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
613 	}
614 
615 	ret = platform_device_add_resources(pdev,
616 			pdevinfo->res, pdevinfo->num_res);
617 	if (ret)
618 		goto err;
619 
620 	ret = platform_device_add_data(pdev,
621 			pdevinfo->data, pdevinfo->size_data);
622 	if (ret)
623 		goto err;
624 
625 	if (pdevinfo->properties) {
626 		ret = platform_device_add_properties(pdev,
627 						     pdevinfo->properties);
628 		if (ret)
629 			goto err;
630 	}
631 
632 	ret = platform_device_add(pdev);
633 	if (ret) {
634 err:
635 		ACPI_COMPANION_SET(&pdev->dev, NULL);
636 		kfree(pdev->dev.dma_mask);
637 		platform_device_put(pdev);
638 		return ERR_PTR(ret);
639 	}
640 
641 	return pdev;
642 }
643 EXPORT_SYMBOL_GPL(platform_device_register_full);
644 
645 static int platform_drv_probe(struct device *_dev)
646 {
647 	struct platform_driver *drv = to_platform_driver(_dev->driver);
648 	struct platform_device *dev = to_platform_device(_dev);
649 	int ret;
650 
651 	ret = of_clk_set_defaults(_dev->of_node, false);
652 	if (ret < 0)
653 		return ret;
654 
655 	ret = dev_pm_domain_attach(_dev, true);
656 	if (ret)
657 		goto out;
658 
659 	if (drv->probe) {
660 		ret = drv->probe(dev);
661 		if (ret)
662 			dev_pm_domain_detach(_dev, true);
663 	}
664 
665 out:
666 	if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
667 		dev_warn(_dev, "probe deferral not supported\n");
668 		ret = -ENXIO;
669 	}
670 
671 	return ret;
672 }
673 
674 static int platform_drv_probe_fail(struct device *_dev)
675 {
676 	return -ENXIO;
677 }
678 
679 static int platform_drv_remove(struct device *_dev)
680 {
681 	struct platform_driver *drv = to_platform_driver(_dev->driver);
682 	struct platform_device *dev = to_platform_device(_dev);
683 	int ret = 0;
684 
685 	if (drv->remove)
686 		ret = drv->remove(dev);
687 	dev_pm_domain_detach(_dev, true);
688 
689 	return ret;
690 }
691 
692 static void platform_drv_shutdown(struct device *_dev)
693 {
694 	struct platform_driver *drv = to_platform_driver(_dev->driver);
695 	struct platform_device *dev = to_platform_device(_dev);
696 
697 	if (drv->shutdown)
698 		drv->shutdown(dev);
699 }
700 
701 /**
702  * __platform_driver_register - register a driver for platform-level devices
703  * @drv: platform driver structure
704  * @owner: owning module/driver
705  */
706 int __platform_driver_register(struct platform_driver *drv,
707 				struct module *owner)
708 {
709 	drv->driver.owner = owner;
710 	drv->driver.bus = &platform_bus_type;
711 	drv->driver.probe = platform_drv_probe;
712 	drv->driver.remove = platform_drv_remove;
713 	drv->driver.shutdown = platform_drv_shutdown;
714 
715 	return driver_register(&drv->driver);
716 }
717 EXPORT_SYMBOL_GPL(__platform_driver_register);
718 
719 /**
720  * platform_driver_unregister - unregister a driver for platform-level devices
721  * @drv: platform driver structure
722  */
723 void platform_driver_unregister(struct platform_driver *drv)
724 {
725 	driver_unregister(&drv->driver);
726 }
727 EXPORT_SYMBOL_GPL(platform_driver_unregister);
728 
729 /**
730  * __platform_driver_probe - register driver for non-hotpluggable device
731  * @drv: platform driver structure
732  * @probe: the driver probe routine, probably from an __init section
733  * @module: module which will be the owner of the driver
734  *
735  * Use this instead of platform_driver_register() when you know the device
736  * is not hotpluggable and has already been registered, and you want to
737  * remove its run-once probe() infrastructure from memory after the driver
738  * has bound to the device.
739  *
740  * One typical use for this would be with drivers for controllers integrated
741  * into system-on-chip processors, where the controller devices have been
742  * configured as part of board setup.
743  *
744  * Note that this is incompatible with deferred probing.
745  *
746  * Returns zero if the driver registered and bound to a device, else returns
747  * a negative error code and with the driver not registered.
748  */
749 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
750 		int (*probe)(struct platform_device *), struct module *module)
751 {
752 	int retval, code;
753 
754 	if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
755 		pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
756 			 drv->driver.name, __func__);
757 		return -EINVAL;
758 	}
759 
760 	/*
761 	 * We have to run our probes synchronously because we check if
762 	 * we find any devices to bind to and exit with error if there
763 	 * are any.
764 	 */
765 	drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
766 
767 	/*
768 	 * Prevent driver from requesting probe deferral to avoid further
769 	 * futile probe attempts.
770 	 */
771 	drv->prevent_deferred_probe = true;
772 
773 	/* make sure driver won't have bind/unbind attributes */
774 	drv->driver.suppress_bind_attrs = true;
775 
776 	/* temporary section violation during probe() */
777 	drv->probe = probe;
778 	retval = code = __platform_driver_register(drv, module);
779 
780 	/*
781 	 * Fixup that section violation, being paranoid about code scanning
782 	 * the list of drivers in order to probe new devices.  Check to see
783 	 * if the probe was successful, and make sure any forced probes of
784 	 * new devices fail.
785 	 */
786 	spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
787 	drv->probe = NULL;
788 	if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
789 		retval = -ENODEV;
790 	drv->driver.probe = platform_drv_probe_fail;
791 	spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
792 
793 	if (code != retval)
794 		platform_driver_unregister(drv);
795 	return retval;
796 }
797 EXPORT_SYMBOL_GPL(__platform_driver_probe);
798 
799 /**
800  * __platform_create_bundle - register driver and create corresponding device
801  * @driver: platform driver structure
802  * @probe: the driver probe routine, probably from an __init section
803  * @res: set of resources that needs to be allocated for the device
804  * @n_res: number of resources
805  * @data: platform specific data for this platform device
806  * @size: size of platform specific data
807  * @module: module which will be the owner of the driver
808  *
809  * Use this in legacy-style modules that probe hardware directly and
810  * register a single platform device and corresponding platform driver.
811  *
812  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
813  */
814 struct platform_device * __init_or_module __platform_create_bundle(
815 			struct platform_driver *driver,
816 			int (*probe)(struct platform_device *),
817 			struct resource *res, unsigned int n_res,
818 			const void *data, size_t size, struct module *module)
819 {
820 	struct platform_device *pdev;
821 	int error;
822 
823 	pdev = platform_device_alloc(driver->driver.name, -1);
824 	if (!pdev) {
825 		error = -ENOMEM;
826 		goto err_out;
827 	}
828 
829 	error = platform_device_add_resources(pdev, res, n_res);
830 	if (error)
831 		goto err_pdev_put;
832 
833 	error = platform_device_add_data(pdev, data, size);
834 	if (error)
835 		goto err_pdev_put;
836 
837 	error = platform_device_add(pdev);
838 	if (error)
839 		goto err_pdev_put;
840 
841 	error = __platform_driver_probe(driver, probe, module);
842 	if (error)
843 		goto err_pdev_del;
844 
845 	return pdev;
846 
847 err_pdev_del:
848 	platform_device_del(pdev);
849 err_pdev_put:
850 	platform_device_put(pdev);
851 err_out:
852 	return ERR_PTR(error);
853 }
854 EXPORT_SYMBOL_GPL(__platform_create_bundle);
855 
856 /**
857  * __platform_register_drivers - register an array of platform drivers
858  * @drivers: an array of drivers to register
859  * @count: the number of drivers to register
860  * @owner: module owning the drivers
861  *
862  * Registers platform drivers specified by an array. On failure to register a
863  * driver, all previously registered drivers will be unregistered. Callers of
864  * this API should use platform_unregister_drivers() to unregister drivers in
865  * the reverse order.
866  *
867  * Returns: 0 on success or a negative error code on failure.
868  */
869 int __platform_register_drivers(struct platform_driver * const *drivers,
870 				unsigned int count, struct module *owner)
871 {
872 	unsigned int i;
873 	int err;
874 
875 	for (i = 0; i < count; i++) {
876 		pr_debug("registering platform driver %ps\n", drivers[i]);
877 
878 		err = __platform_driver_register(drivers[i], owner);
879 		if (err < 0) {
880 			pr_err("failed to register platform driver %ps: %d\n",
881 			       drivers[i], err);
882 			goto error;
883 		}
884 	}
885 
886 	return 0;
887 
888 error:
889 	while (i--) {
890 		pr_debug("unregistering platform driver %ps\n", drivers[i]);
891 		platform_driver_unregister(drivers[i]);
892 	}
893 
894 	return err;
895 }
896 EXPORT_SYMBOL_GPL(__platform_register_drivers);
897 
898 /**
899  * platform_unregister_drivers - unregister an array of platform drivers
900  * @drivers: an array of drivers to unregister
901  * @count: the number of drivers to unregister
902  *
903  * Unegisters platform drivers specified by an array. This is typically used
904  * to complement an earlier call to platform_register_drivers(). Drivers are
905  * unregistered in the reverse order in which they were registered.
906  */
907 void platform_unregister_drivers(struct platform_driver * const *drivers,
908 				 unsigned int count)
909 {
910 	while (count--) {
911 		pr_debug("unregistering platform driver %ps\n", drivers[count]);
912 		platform_driver_unregister(drivers[count]);
913 	}
914 }
915 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
916 
917 /* modalias support enables more hands-off userspace setup:
918  * (a) environment variable lets new-style hotplug events work once system is
919  *     fully running:  "modprobe $MODALIAS"
920  * (b) sysfs attribute lets new-style coldplug recover from hotplug events
921  *     mishandled before system is fully running:  "modprobe $(cat modalias)"
922  */
923 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
924 			     char *buf)
925 {
926 	struct platform_device	*pdev = to_platform_device(dev);
927 	int len;
928 
929 	len = of_device_modalias(dev, buf, PAGE_SIZE);
930 	if (len != -ENODEV)
931 		return len;
932 
933 	len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
934 	if (len != -ENODEV)
935 		return len;
936 
937 	len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
938 
939 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
940 }
941 static DEVICE_ATTR_RO(modalias);
942 
943 static ssize_t driver_override_store(struct device *dev,
944 				     struct device_attribute *attr,
945 				     const char *buf, size_t count)
946 {
947 	struct platform_device *pdev = to_platform_device(dev);
948 	char *driver_override, *old, *cp;
949 
950 	/* We need to keep extra room for a newline */
951 	if (count >= (PAGE_SIZE - 1))
952 		return -EINVAL;
953 
954 	driver_override = kstrndup(buf, count, GFP_KERNEL);
955 	if (!driver_override)
956 		return -ENOMEM;
957 
958 	cp = strchr(driver_override, '\n');
959 	if (cp)
960 		*cp = '\0';
961 
962 	device_lock(dev);
963 	old = pdev->driver_override;
964 	if (strlen(driver_override)) {
965 		pdev->driver_override = driver_override;
966 	} else {
967 		kfree(driver_override);
968 		pdev->driver_override = NULL;
969 	}
970 	device_unlock(dev);
971 
972 	kfree(old);
973 
974 	return count;
975 }
976 
977 static ssize_t driver_override_show(struct device *dev,
978 				    struct device_attribute *attr, char *buf)
979 {
980 	struct platform_device *pdev = to_platform_device(dev);
981 	ssize_t len;
982 
983 	device_lock(dev);
984 	len = sprintf(buf, "%s\n", pdev->driver_override);
985 	device_unlock(dev);
986 	return len;
987 }
988 static DEVICE_ATTR_RW(driver_override);
989 
990 
991 static struct attribute *platform_dev_attrs[] = {
992 	&dev_attr_modalias.attr,
993 	&dev_attr_driver_override.attr,
994 	NULL,
995 };
996 ATTRIBUTE_GROUPS(platform_dev);
997 
998 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
999 {
1000 	struct platform_device	*pdev = to_platform_device(dev);
1001 	int rc;
1002 
1003 	/* Some devices have extra OF data and an OF-style MODALIAS */
1004 	rc = of_device_uevent_modalias(dev, env);
1005 	if (rc != -ENODEV)
1006 		return rc;
1007 
1008 	rc = acpi_device_uevent_modalias(dev, env);
1009 	if (rc != -ENODEV)
1010 		return rc;
1011 
1012 	add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1013 			pdev->name);
1014 	return 0;
1015 }
1016 
1017 static const struct platform_device_id *platform_match_id(
1018 			const struct platform_device_id *id,
1019 			struct platform_device *pdev)
1020 {
1021 	while (id->name[0]) {
1022 		if (strcmp(pdev->name, id->name) == 0) {
1023 			pdev->id_entry = id;
1024 			return id;
1025 		}
1026 		id++;
1027 	}
1028 	return NULL;
1029 }
1030 
1031 /**
1032  * platform_match - bind platform device to platform driver.
1033  * @dev: device.
1034  * @drv: driver.
1035  *
1036  * Platform device IDs are assumed to be encoded like this:
1037  * "<name><instance>", where <name> is a short description of the type of
1038  * device, like "pci" or "floppy", and <instance> is the enumerated
1039  * instance of the device, like '0' or '42'.  Driver IDs are simply
1040  * "<name>".  So, extract the <name> from the platform_device structure,
1041  * and compare it against the name of the driver. Return whether they match
1042  * or not.
1043  */
1044 static int platform_match(struct device *dev, struct device_driver *drv)
1045 {
1046 	struct platform_device *pdev = to_platform_device(dev);
1047 	struct platform_driver *pdrv = to_platform_driver(drv);
1048 
1049 	/* When driver_override is set, only bind to the matching driver */
1050 	if (pdev->driver_override)
1051 		return !strcmp(pdev->driver_override, drv->name);
1052 
1053 	/* Attempt an OF style match first */
1054 	if (of_driver_match_device(dev, drv))
1055 		return 1;
1056 
1057 	/* Then try ACPI style match */
1058 	if (acpi_driver_match_device(dev, drv))
1059 		return 1;
1060 
1061 	/* Then try to match against the id table */
1062 	if (pdrv->id_table)
1063 		return platform_match_id(pdrv->id_table, pdev) != NULL;
1064 
1065 	/* fall-back to driver name match */
1066 	return (strcmp(pdev->name, drv->name) == 0);
1067 }
1068 
1069 #ifdef CONFIG_PM_SLEEP
1070 
1071 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1072 {
1073 	struct platform_driver *pdrv = to_platform_driver(dev->driver);
1074 	struct platform_device *pdev = to_platform_device(dev);
1075 	int ret = 0;
1076 
1077 	if (dev->driver && pdrv->suspend)
1078 		ret = pdrv->suspend(pdev, mesg);
1079 
1080 	return ret;
1081 }
1082 
1083 static int platform_legacy_resume(struct device *dev)
1084 {
1085 	struct platform_driver *pdrv = to_platform_driver(dev->driver);
1086 	struct platform_device *pdev = to_platform_device(dev);
1087 	int ret = 0;
1088 
1089 	if (dev->driver && pdrv->resume)
1090 		ret = pdrv->resume(pdev);
1091 
1092 	return ret;
1093 }
1094 
1095 #endif /* CONFIG_PM_SLEEP */
1096 
1097 #ifdef CONFIG_SUSPEND
1098 
1099 int platform_pm_suspend(struct device *dev)
1100 {
1101 	struct device_driver *drv = dev->driver;
1102 	int ret = 0;
1103 
1104 	if (!drv)
1105 		return 0;
1106 
1107 	if (drv->pm) {
1108 		if (drv->pm->suspend)
1109 			ret = drv->pm->suspend(dev);
1110 	} else {
1111 		ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1112 	}
1113 
1114 	return ret;
1115 }
1116 
1117 int platform_pm_resume(struct device *dev)
1118 {
1119 	struct device_driver *drv = dev->driver;
1120 	int ret = 0;
1121 
1122 	if (!drv)
1123 		return 0;
1124 
1125 	if (drv->pm) {
1126 		if (drv->pm->resume)
1127 			ret = drv->pm->resume(dev);
1128 	} else {
1129 		ret = platform_legacy_resume(dev);
1130 	}
1131 
1132 	return ret;
1133 }
1134 
1135 #endif /* CONFIG_SUSPEND */
1136 
1137 #ifdef CONFIG_HIBERNATE_CALLBACKS
1138 
1139 int platform_pm_freeze(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->freeze)
1149 			ret = drv->pm->freeze(dev);
1150 	} else {
1151 		ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1152 	}
1153 
1154 	return ret;
1155 }
1156 
1157 int platform_pm_thaw(struct device *dev)
1158 {
1159 	struct device_driver *drv = dev->driver;
1160 	int ret = 0;
1161 
1162 	if (!drv)
1163 		return 0;
1164 
1165 	if (drv->pm) {
1166 		if (drv->pm->thaw)
1167 			ret = drv->pm->thaw(dev);
1168 	} else {
1169 		ret = platform_legacy_resume(dev);
1170 	}
1171 
1172 	return ret;
1173 }
1174 
1175 int platform_pm_poweroff(struct device *dev)
1176 {
1177 	struct device_driver *drv = dev->driver;
1178 	int ret = 0;
1179 
1180 	if (!drv)
1181 		return 0;
1182 
1183 	if (drv->pm) {
1184 		if (drv->pm->poweroff)
1185 			ret = drv->pm->poweroff(dev);
1186 	} else {
1187 		ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1188 	}
1189 
1190 	return ret;
1191 }
1192 
1193 int platform_pm_restore(struct device *dev)
1194 {
1195 	struct device_driver *drv = dev->driver;
1196 	int ret = 0;
1197 
1198 	if (!drv)
1199 		return 0;
1200 
1201 	if (drv->pm) {
1202 		if (drv->pm->restore)
1203 			ret = drv->pm->restore(dev);
1204 	} else {
1205 		ret = platform_legacy_resume(dev);
1206 	}
1207 
1208 	return ret;
1209 }
1210 
1211 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1212 
1213 int platform_dma_configure(struct device *dev)
1214 {
1215 	enum dev_dma_attr attr;
1216 	int ret = 0;
1217 
1218 	if (dev->of_node) {
1219 		ret = of_dma_configure(dev, dev->of_node, true);
1220 	} else if (has_acpi_companion(dev)) {
1221 		attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1222 		ret = acpi_dma_configure(dev, attr);
1223 	}
1224 
1225 	return ret;
1226 }
1227 
1228 static const struct dev_pm_ops platform_dev_pm_ops = {
1229 	.runtime_suspend = pm_generic_runtime_suspend,
1230 	.runtime_resume = pm_generic_runtime_resume,
1231 	USE_PLATFORM_PM_SLEEP_OPS
1232 };
1233 
1234 struct bus_type platform_bus_type = {
1235 	.name		= "platform",
1236 	.dev_groups	= platform_dev_groups,
1237 	.match		= platform_match,
1238 	.uevent		= platform_uevent,
1239 	.dma_configure	= platform_dma_configure,
1240 	.pm		= &platform_dev_pm_ops,
1241 };
1242 EXPORT_SYMBOL_GPL(platform_bus_type);
1243 
1244 /**
1245  * platform_find_device_by_driver - Find a platform device with a given
1246  * driver.
1247  * @start: The device to start the search from.
1248  * @drv: The device driver to look for.
1249  */
1250 struct device *platform_find_device_by_driver(struct device *start,
1251 					      const struct device_driver *drv)
1252 {
1253 	return bus_find_device(&platform_bus_type, start, drv,
1254 			       (void *)platform_match);
1255 }
1256 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1257 
1258 int __init platform_bus_init(void)
1259 {
1260 	int error;
1261 
1262 	error = device_register(&platform_bus);
1263 	if (error) {
1264 		put_device(&platform_bus);
1265 		return error;
1266 	}
1267 	error =  bus_register(&platform_bus_type);
1268 	if (error)
1269 		device_unregister(&platform_bus);
1270 	of_platform_register_reconfig_notifier();
1271 	return error;
1272 }
1273