xref: /linux/drivers/base/platform.c (revision 5b193d6a8973655f19de87a7873ce31dee2d5fe6)
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/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/memblock.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/pm_domain.h>
26 #include <linux/idr.h>
27 #include <linux/acpi.h>
28 #include <linux/clk/clk-conf.h>
29 #include <linux/limits.h>
30 #include <linux/property.h>
31 #include <linux/kmemleak.h>
32 #include <linux/types.h>
33 #include <linux/iommu.h>
34 #include <linux/dma-map-ops.h>
35 
36 #include "base.h"
37 #include "power/power.h"
38 
39 /* For automatically allocated device IDs */
40 static DEFINE_IDA(platform_devid_ida);
41 
42 struct device platform_bus = {
43 	.init_name	= "platform",
44 };
45 EXPORT_SYMBOL_GPL(platform_bus);
46 
47 /**
48  * platform_get_resource - get a resource for a device
49  * @dev: platform device
50  * @type: resource type
51  * @num: resource index
52  *
53  * Return: a pointer to the resource or NULL on failure.
54  */
55 struct resource *platform_get_resource(struct platform_device *dev,
56 				       unsigned int type, unsigned int num)
57 {
58 	u32 i;
59 
60 	for (i = 0; i < dev->num_resources; i++) {
61 		struct resource *r = &dev->resource[i];
62 
63 		if (type == resource_type(r) && num-- == 0)
64 			return r;
65 	}
66 	return NULL;
67 }
68 EXPORT_SYMBOL_GPL(platform_get_resource);
69 
70 struct resource *platform_get_mem_or_io(struct platform_device *dev,
71 					unsigned int num)
72 {
73 	u32 i;
74 
75 	for (i = 0; i < dev->num_resources; i++) {
76 		struct resource *r = &dev->resource[i];
77 
78 		if ((resource_type(r) & (IORESOURCE_MEM | IORESOURCE_IO)) && num-- == 0)
79 			return r;
80 	}
81 	return NULL;
82 }
83 EXPORT_SYMBOL_GPL(platform_get_mem_or_io);
84 
85 #ifdef CONFIG_HAS_IOMEM
86 /**
87  * devm_platform_get_and_ioremap_resource - call devm_ioremap_resource() for a
88  *					    platform device and get resource
89  *
90  * @pdev: platform device to use both for memory resource lookup as well as
91  *        resource management
92  * @index: resource index
93  * @res: optional output parameter to store a pointer to the obtained resource.
94  *
95  * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
96  * on failure.
97  */
98 void __iomem *
99 devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
100 				       unsigned int index, struct resource **res)
101 {
102 	struct resource *r;
103 
104 	r = platform_get_resource(pdev, IORESOURCE_MEM, index);
105 	if (res)
106 		*res = r;
107 	return devm_ioremap_resource(&pdev->dev, r);
108 }
109 EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource);
110 
111 /**
112  * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
113  *				    device
114  *
115  * @pdev: platform device to use both for memory resource lookup as well as
116  *        resource management
117  * @index: resource index
118  *
119  * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
120  * on failure.
121  */
122 void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
123 					     unsigned int index)
124 {
125 	return devm_platform_get_and_ioremap_resource(pdev, index, NULL);
126 }
127 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
128 
129 /**
130  * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for
131  *					   a platform device, retrieve the
132  *					   resource by name
133  *
134  * @pdev: platform device to use both for memory resource lookup as well as
135  *	  resource management
136  * @name: name of the resource
137  *
138  * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
139  * on failure.
140  */
141 void __iomem *
142 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
143 				      const char *name)
144 {
145 	struct resource *res;
146 
147 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
148 	return devm_ioremap_resource(&pdev->dev, res);
149 }
150 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname);
151 #endif /* CONFIG_HAS_IOMEM */
152 
153 static const struct cpumask *get_irq_affinity(struct platform_device *dev,
154 					      unsigned int num)
155 {
156 	const struct cpumask *mask = NULL;
157 #ifndef CONFIG_SPARC
158 	struct fwnode_handle *fwnode = dev_fwnode(&dev->dev);
159 
160 	if (is_of_node(fwnode))
161 		mask = of_irq_get_affinity(to_of_node(fwnode), num);
162 	else if (is_acpi_device_node(fwnode))
163 		mask = acpi_irq_get_affinity(ACPI_HANDLE_FWNODE(fwnode), num);
164 #endif
165 
166 	return mask ?: cpu_possible_mask;
167 }
168 
169 /**
170  * platform_get_irq_affinity - get an optional IRQ and its affinity for a device
171  * @dev:	platform device
172  * @num:	interrupt number index
173  * @affinity:	optional cpumask pointer to get the affinity of a per-cpu interrupt
174  *
175  * Gets an interrupt for a platform device. Device drivers should check the
176  * return value for errors so as to not pass a negative integer value to
177  * the request_irq() APIs. Optional affinity information is provided in the
178  * affinity pointer if available, and NULL otherwise.
179  *
180  * Return: non-zero interrupt number on success, negative error number on failure.
181  */
182 int platform_get_irq_affinity(struct platform_device *dev, unsigned int num,
183 			      const struct cpumask **affinity)
184 {
185 	int ret;
186 #ifdef CONFIG_SPARC
187 	/* sparc does not have irqs represented as IORESOURCE_IRQ resources */
188 	if (!dev || num >= dev->archdata.num_irqs)
189 		goto out_not_found;
190 	ret = dev->archdata.irqs[num];
191 	goto out;
192 #else
193 	struct fwnode_handle *fwnode = dev_fwnode(&dev->dev);
194 	struct resource *r;
195 
196 	if (is_of_node(fwnode)) {
197 		ret = of_irq_get(to_of_node(fwnode), num);
198 		if (ret > 0 || ret == -EPROBE_DEFER)
199 			goto out;
200 	}
201 
202 	r = platform_get_resource(dev, IORESOURCE_IRQ, num);
203 	if (is_acpi_device_node(fwnode)) {
204 		if (r && r->flags & IORESOURCE_DISABLED) {
205 			ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), num, r);
206 			if (ret)
207 				goto out;
208 		}
209 	}
210 
211 	/*
212 	 * The resources may pass trigger flags to the irqs that need
213 	 * to be set up. It so happens that the trigger flags for
214 	 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
215 	 * settings.
216 	 */
217 	if (r && r->flags & IORESOURCE_BITS) {
218 		struct irq_data *irqd;
219 
220 		irqd = irq_get_irq_data(r->start);
221 		if (!irqd)
222 			goto out_not_found;
223 		irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
224 	}
225 
226 	if (r) {
227 		ret = r->start;
228 		goto out;
229 	}
230 
231 	/*
232 	 * For the index 0 interrupt, allow falling back to GpioInt
233 	 * resources. While a device could have both Interrupt and GpioInt
234 	 * resources, making this fallback ambiguous, in many common cases
235 	 * the device will only expose one IRQ, and this fallback
236 	 * allows a common code path across either kind of resource.
237 	 */
238 	if (num == 0 && is_acpi_device_node(fwnode)) {
239 		ret = acpi_dev_gpio_irq_get(to_acpi_device_node(fwnode), num);
240 		/* Our callers expect -ENXIO for missing IRQs. */
241 		if (ret >= 0 || ret == -EPROBE_DEFER)
242 			goto out;
243 	}
244 
245 #endif
246 out_not_found:
247 	ret = -ENXIO;
248 out:
249 	if (WARN(!ret, "0 is an invalid IRQ number\n"))
250 		return -EINVAL;
251 
252 	if (ret > 0 && affinity)
253 		*affinity = get_irq_affinity(dev, num);
254 
255 	return ret;
256 }
257 EXPORT_SYMBOL_GPL(platform_get_irq_affinity);
258 
259 /**
260  * platform_get_irq_optional - get an optional interrupt for a device
261  * @dev:	platform device
262  * @num:	interrupt number index
263  *
264  * Gets an interrupt for a platform device. Device drivers should check the
265  * return value for errors so as to not pass a negative integer value to
266  * the request_irq() APIs. This is the same as platform_get_irq(), except
267  * that it does not print an error message if an interrupt can not be
268  * obtained.
269  *
270  * For example::
271  *
272  *		int irq = platform_get_irq_optional(pdev, 0);
273  *		if (irq < 0)
274  *			return irq;
275  *
276  * Return: non-zero interrupt number on success, negative error number on failure.
277  */
278 int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
279 {
280 	return platform_get_irq_affinity(dev, num, NULL);
281 }
282 EXPORT_SYMBOL_GPL(platform_get_irq_optional);
283 
284 /**
285  * platform_get_irq - get an IRQ for a device
286  * @dev: platform device
287  * @num: IRQ number index
288  *
289  * Gets an IRQ for a platform device and prints an error message if finding the
290  * IRQ fails. Device drivers should check the return value for errors so as to
291  * not pass a negative integer value to the request_irq() APIs.
292  *
293  * For example::
294  *
295  *		int irq = platform_get_irq(pdev, 0);
296  *		if (irq < 0)
297  *			return irq;
298  *
299  * Return: non-zero IRQ number on success, negative error number on failure.
300  */
301 int platform_get_irq(struct platform_device *dev, unsigned int num)
302 {
303 	int ret;
304 
305 	ret = platform_get_irq_optional(dev, num);
306 	if (ret < 0)
307 		return dev_err_probe(&dev->dev, ret,
308 				     "IRQ index %u not found\n", num);
309 
310 	return ret;
311 }
312 EXPORT_SYMBOL_GPL(platform_get_irq);
313 
314 /**
315  * platform_irq_count - Count the number of IRQs a platform device uses
316  * @dev: platform device
317  *
318  * Return: Number of IRQs a platform device uses or EPROBE_DEFER
319  */
320 int platform_irq_count(struct platform_device *dev)
321 {
322 	int ret, nr = 0;
323 
324 	while ((ret = platform_get_irq_optional(dev, nr)) >= 0)
325 		nr++;
326 
327 	if (ret == -EPROBE_DEFER)
328 		return ret;
329 
330 	return nr;
331 }
332 EXPORT_SYMBOL_GPL(platform_irq_count);
333 
334 struct irq_affinity_devres {
335 	unsigned int count;
336 	unsigned int irq[] __counted_by(count);
337 };
338 
339 static void platform_disable_acpi_irq(struct platform_device *pdev, int index)
340 {
341 	struct resource *r;
342 
343 	r = platform_get_resource(pdev, IORESOURCE_IRQ, index);
344 	if (r)
345 		irqresource_disabled(r, 0);
346 }
347 
348 static void devm_platform_get_irqs_affinity_release(struct device *dev,
349 						    void *res)
350 {
351 	struct irq_affinity_devres *ptr = res;
352 	int i;
353 
354 	for (i = 0; i < ptr->count; i++) {
355 		irq_dispose_mapping(ptr->irq[i]);
356 
357 		if (is_acpi_device_node(dev_fwnode(dev)))
358 			platform_disable_acpi_irq(to_platform_device(dev), i);
359 	}
360 }
361 
362 /**
363  * devm_platform_get_irqs_affinity - devm method to get a set of IRQs for a
364  *				device using an interrupt affinity descriptor
365  * @dev: platform device pointer
366  * @affd: affinity descriptor
367  * @minvec: minimum count of interrupt vectors
368  * @maxvec: maximum count of interrupt vectors
369  * @irqs: pointer holder for IRQ numbers
370  *
371  * Gets a set of IRQs for a platform device, and updates IRQ afffinty according
372  * to the passed affinity descriptor
373  *
374  * Return: Number of vectors on success, negative error number on failure.
375  */
376 int devm_platform_get_irqs_affinity(struct platform_device *dev,
377 				    struct irq_affinity *affd,
378 				    unsigned int minvec,
379 				    unsigned int maxvec,
380 				    int **irqs)
381 {
382 	struct irq_affinity_devres *ptr;
383 	struct irq_affinity_desc *desc;
384 	size_t size;
385 	int i, ret, nvec;
386 
387 	if (!affd)
388 		return -EPERM;
389 
390 	if (maxvec < minvec)
391 		return -ERANGE;
392 
393 	nvec = platform_irq_count(dev);
394 	if (nvec < 0)
395 		return nvec;
396 
397 	if (nvec < minvec)
398 		return -ENOSPC;
399 
400 	nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
401 	if (nvec < minvec)
402 		return -ENOSPC;
403 
404 	if (nvec > maxvec)
405 		nvec = maxvec;
406 
407 	size = sizeof(*ptr) + sizeof(unsigned int) * nvec;
408 	ptr = devres_alloc(devm_platform_get_irqs_affinity_release, size,
409 			   GFP_KERNEL);
410 	if (!ptr)
411 		return -ENOMEM;
412 
413 	ptr->count = nvec;
414 
415 	for (i = 0; i < nvec; i++) {
416 		int irq = platform_get_irq(dev, i);
417 		if (irq < 0) {
418 			ret = irq;
419 			goto err_free_devres;
420 		}
421 		ptr->irq[i] = irq;
422 	}
423 
424 	desc = irq_create_affinity_masks(nvec, affd);
425 	if (!desc) {
426 		ret = -ENOMEM;
427 		goto err_free_devres;
428 	}
429 
430 	for (i = 0; i < nvec; i++) {
431 		ret = irq_update_affinity_desc(ptr->irq[i], &desc[i]);
432 		if (ret) {
433 			dev_err(&dev->dev, "failed to update irq%d affinity descriptor (%d)\n",
434 				ptr->irq[i], ret);
435 			goto err_free_desc;
436 		}
437 	}
438 
439 	devres_add(&dev->dev, ptr);
440 
441 	kfree(desc);
442 
443 	*irqs = ptr->irq;
444 
445 	return nvec;
446 
447 err_free_desc:
448 	kfree(desc);
449 err_free_devres:
450 	devres_free(ptr);
451 	return ret;
452 }
453 EXPORT_SYMBOL_GPL(devm_platform_get_irqs_affinity);
454 
455 /**
456  * platform_get_resource_byname - get a resource for a device by name
457  * @dev: platform device
458  * @type: resource type
459  * @name: resource name
460  */
461 struct resource *platform_get_resource_byname(struct platform_device *dev,
462 					      unsigned int type,
463 					      const char *name)
464 {
465 	u32 i;
466 
467 	for (i = 0; i < dev->num_resources; i++) {
468 		struct resource *r = &dev->resource[i];
469 
470 		if (unlikely(!r->name))
471 			continue;
472 
473 		if (type == resource_type(r) && !strcmp(r->name, name))
474 			return r;
475 	}
476 	return NULL;
477 }
478 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
479 
480 static int __platform_get_irq_byname(struct platform_device *dev,
481 				     const char *name)
482 {
483 	struct resource *r;
484 	int ret;
485 
486 	ret = fwnode_irq_get_byname(dev_fwnode(&dev->dev), name);
487 	if (ret > 0 || ret == -EPROBE_DEFER)
488 		return ret;
489 
490 	r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
491 	if (r) {
492 		if (WARN(!r->start, "0 is an invalid IRQ number\n"))
493 			return -EINVAL;
494 		return r->start;
495 	}
496 
497 	return -ENXIO;
498 }
499 
500 /**
501  * platform_get_irq_byname - get an IRQ for a device by name
502  * @dev: platform device
503  * @name: IRQ name
504  *
505  * Get an IRQ like platform_get_irq(), but then by name rather then by index.
506  *
507  * Return: non-zero IRQ number on success, negative error number on failure.
508  */
509 int platform_get_irq_byname(struct platform_device *dev, const char *name)
510 {
511 	int ret;
512 
513 	ret = __platform_get_irq_byname(dev, name);
514 	if (ret < 0)
515 		return dev_err_probe(&dev->dev, ret, "IRQ %s not found\n",
516 				     name);
517 	return ret;
518 }
519 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
520 
521 /**
522  * platform_get_irq_byname_optional - get an optional IRQ for a device by name
523  * @dev: platform device
524  * @name: IRQ name
525  *
526  * Get an optional IRQ by name like platform_get_irq_byname(). Except that it
527  * does not print an error message if an IRQ can not be obtained.
528  *
529  * Return: non-zero IRQ number on success, negative error number on failure.
530  */
531 int platform_get_irq_byname_optional(struct platform_device *dev,
532 				     const char *name)
533 {
534 	return __platform_get_irq_byname(dev, name);
535 }
536 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
537 
538 /**
539  * platform_add_devices - add a numbers of platform devices
540  * @devs: array of platform devices to add
541  * @num: number of platform devices in array
542  *
543  * Return: 0 on success, negative error number on failure.
544  */
545 int platform_add_devices(struct platform_device **devs, int num)
546 {
547 	int i, ret = 0;
548 
549 	for (i = 0; i < num; i++) {
550 		ret = platform_device_register(devs[i]);
551 		if (ret) {
552 			while (--i >= 0)
553 				platform_device_unregister(devs[i]);
554 			break;
555 		}
556 	}
557 
558 	return ret;
559 }
560 EXPORT_SYMBOL_GPL(platform_add_devices);
561 
562 struct platform_object {
563 	struct platform_device pdev;
564 	char name[];
565 };
566 
567 /*
568  * Set up default DMA mask for platform devices if the they weren't
569  * previously set by the architecture / DT.
570  */
571 static void setup_pdev_dma_masks(struct platform_device *pdev)
572 {
573 	pdev->dev.dma_parms = &pdev->dma_parms;
574 
575 	if (!pdev->dev.coherent_dma_mask)
576 		pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
577 	if (!pdev->dev.dma_mask) {
578 		pdev->platform_dma_mask = DMA_BIT_MASK(32);
579 		pdev->dev.dma_mask = &pdev->platform_dma_mask;
580 	}
581 };
582 
583 /**
584  * platform_device_put - destroy a platform device
585  * @pdev: platform device to free
586  *
587  * Free all memory associated with a platform device.  This function must
588  * _only_ be externally called in error cases.  All other usage is a bug.
589  */
590 void platform_device_put(struct platform_device *pdev)
591 {
592 	if (!IS_ERR_OR_NULL(pdev))
593 		put_device(&pdev->dev);
594 }
595 EXPORT_SYMBOL_GPL(platform_device_put);
596 
597 static void platform_device_release(struct device *dev)
598 {
599 	struct platform_object *pa = container_of(dev, struct platform_object,
600 						  pdev.dev);
601 
602 	device_remove_software_node(dev);
603 	/*
604 	 * If the primary firmware node is a software node, its reference count
605 	 * was already decreased by the call to device_remove_software_node().
606 	 */
607 	if (!is_software_node(dev_fwnode(dev)))
608 		fwnode_handle_put(pa->pdev.dev.fwnode);
609 	kfree(pa->pdev.dev.platform_data);
610 	kfree(pa->pdev.mfd_cell);
611 	kfree(pa->pdev.resource);
612 	kfree(pa);
613 }
614 
615 /**
616  * platform_device_alloc - create a platform device
617  * @name: base name of the device we're adding
618  * @id: instance id
619  *
620  * Create a platform device object which can have other objects attached
621  * to it, and which will have attached objects freed when it is released.
622  *
623  * The following fields of the dynamically allocated platform device must not
624  * be modified manually: resource, num_resources, dev.platform_data,
625  * dev.of_node and dev.fwnode. Users wishing to do the split platform device
626  * registration with platform_device_alloc() + platform_device_add() are
627  * required to use dedicated helpers for adding resources, platform data or
628  * assigning firmware nodes.
629  */
630 struct platform_device *platform_device_alloc(const char *name, int id)
631 {
632 	struct platform_object *pa;
633 
634 	pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
635 	if (!pa)
636 		return NULL;
637 
638 	strcpy(pa->name, name);
639 	pa->pdev.name = pa->name;
640 	pa->pdev.id = id;
641 	device_initialize(&pa->pdev.dev);
642 	pa->pdev.dev.release = platform_device_release;
643 	setup_pdev_dma_masks(&pa->pdev);
644 
645 	return &pa->pdev;
646 }
647 EXPORT_SYMBOL_GPL(platform_device_alloc);
648 
649 /**
650  * platform_device_add_resources - add resources to a platform device
651  * @pdev: platform device allocated by platform_device_alloc to add resources to
652  * @res: set of resources that needs to be allocated for the device
653  * @num: number of resources
654  *
655  * Add a copy of the resources to the platform device.  The memory
656  * associated with the resources will be freed when the platform device is
657  * released.
658  */
659 int platform_device_add_resources(struct platform_device *pdev,
660 				  const struct resource *res, unsigned int num)
661 {
662 	struct resource *r = NULL;
663 
664 	if (res) {
665 		r = kmemdup_array(res, num, sizeof(*r), GFP_KERNEL);
666 		if (!r)
667 			return -ENOMEM;
668 	}
669 
670 	kfree(pdev->resource);
671 	pdev->resource = r;
672 	pdev->num_resources = num;
673 	return 0;
674 }
675 EXPORT_SYMBOL_GPL(platform_device_add_resources);
676 
677 /**
678  * platform_device_add_data - add platform-specific data to a platform device
679  * @pdev: platform device allocated by platform_device_alloc to add resources to
680  * @data: platform specific data for this platform device
681  * @size: size of platform specific data
682  *
683  * Add a copy of platform specific data to the platform device's
684  * platform_data pointer.  The memory associated with the platform data
685  * will be freed when the platform device is released.
686  */
687 int platform_device_add_data(struct platform_device *pdev, const void *data,
688 			     size_t size)
689 {
690 	void *d = NULL;
691 
692 	if (data) {
693 		d = kmemdup(data, size, GFP_KERNEL);
694 		if (!d)
695 			return -ENOMEM;
696 	}
697 
698 	kfree(pdev->dev.platform_data);
699 	pdev->dev.platform_data = d;
700 	return 0;
701 }
702 EXPORT_SYMBOL_GPL(platform_device_add_data);
703 
704 /**
705  * platform_device_set_of_node - assign an OF node to device
706  * @pdev: platform device to add the node for
707  * @np: new device node
708  *
709  * Assign an OF node to this platform device. Internally keep track of the
710  * reference count. Devices created with platform_device_alloc() must use this
711  * function instead of assigning the node manually. This function must not be
712  * called for a platform device that already has a software node as its primary
713  * firmware node assigned.
714  */
715 void platform_device_set_of_node(struct platform_device *pdev,
716 				 struct device_node *np)
717 {
718 	platform_device_set_fwnode(pdev, of_fwnode_handle(np));
719 }
720 EXPORT_SYMBOL_GPL(platform_device_set_of_node);
721 
722 /**
723  * platform_device_set_fwnode - assign a firmware node to device
724  * @pdev: platform device to set the node for
725  * @fwnode: new firmware node
726  *
727  * Assign a firmware node to this platform device. Internally keep track of the
728  * reference count. Devices created with platform_device_alloc() must use this
729  * function instead of assigning the node manually. This function must not be
730  * called for a platform device that already has a software node as its primary
731  * firmware node assigned.
732  */
733 void platform_device_set_fwnode(struct platform_device *pdev,
734 				struct fwnode_handle *fwnode)
735 {
736 	/*
737 	 * If we call this function for a platform device whose primary
738 	 * firmware node is a software node, we'll never end up calling the
739 	 * symmetric software_node_notify_remove(). There are no users for this
740 	 * right now in the tree so just disallow it.
741 	 */
742 	WARN_ON(is_software_node(dev_fwnode(&pdev->dev)));
743 	fwnode_handle_put(pdev->dev.fwnode);
744 	device_set_node(&pdev->dev, fwnode_handle_get(fwnode));
745 }
746 EXPORT_SYMBOL_GPL(platform_device_set_fwnode);
747 
748 /**
749  * platform_device_set_of_node_from_dev - reuse OF node of another device
750  * @pdev: platform device to set the node for
751  * @dev2: device whose OF node to reuse
752  *
753  * Reuses the OF node of another device in this platform device while
754  * internally keeping track of reference counting. This function must not be
755  * called for a platform device that already has a software node as its primary
756  * firmware node assigned.
757  */
758 void platform_device_set_of_node_from_dev(struct platform_device *pdev,
759 					  const struct device *dev2)
760 {
761 	/* See platform_device_set_fwnode(). */
762 	WARN_ON(is_software_node(dev_fwnode(&pdev->dev)));
763 	device_set_of_node_from_dev(&pdev->dev, dev2);
764 	pdev->dev.fwnode = of_fwnode_handle(pdev->dev.of_node);
765 }
766 EXPORT_SYMBOL_GPL(platform_device_set_of_node_from_dev);
767 
768 /**
769  * platform_device_add - add a platform device to device hierarchy
770  * @pdev: platform device we're adding
771  *
772  * This is part 2 of platform_device_register(), though may be called
773  * separately _iff_ pdev was allocated by platform_device_alloc().
774  */
775 int platform_device_add(struct platform_device *pdev)
776 {
777 	struct device *dev = &pdev->dev;
778 	u32 i;
779 	int ret;
780 
781 	if (!dev->parent)
782 		dev->parent = &platform_bus;
783 
784 	dev->bus = &platform_bus_type;
785 
786 	switch (pdev->id) {
787 	default:
788 		dev_set_name(dev, "%s.%d", pdev->name,  pdev->id);
789 		break;
790 	case PLATFORM_DEVID_NONE:
791 		dev_set_name(dev, "%s", pdev->name);
792 		break;
793 	case PLATFORM_DEVID_AUTO:
794 		/*
795 		 * Automatically allocated device ID. We mark it as such so
796 		 * that we remember it must be freed, and we append a suffix
797 		 * to avoid namespace collision with explicit IDs.
798 		 */
799 		ret = ida_alloc(&platform_devid_ida, GFP_KERNEL);
800 		if (ret < 0)
801 			return ret;
802 		pdev->id = ret;
803 		pdev->id_auto = true;
804 		dev_set_name(dev, "%s.%d.auto", pdev->name, pdev->id);
805 		break;
806 	}
807 
808 	for (i = 0; i < pdev->num_resources; i++) {
809 		struct resource *p, *r = &pdev->resource[i];
810 
811 		if (r->name == NULL)
812 			r->name = dev_name(dev);
813 
814 		p = r->parent;
815 		if (!p) {
816 			if (resource_type(r) == IORESOURCE_MEM)
817 				p = &iomem_resource;
818 			else if (resource_type(r) == IORESOURCE_IO)
819 				p = &ioport_resource;
820 		}
821 
822 		if (p) {
823 			ret = insert_resource(p, r);
824 			if (ret) {
825 				dev_err(dev, "failed to claim resource %d: %pR\n", i, r);
826 				goto failed;
827 			}
828 		}
829 	}
830 
831 	pr_debug("Registering platform device '%s'. Parent at %s\n", dev_name(dev),
832 		 dev_name(dev->parent));
833 
834 	ret = device_add(dev);
835 	if (ret)
836 		goto failed;
837 
838 	return 0;
839 
840  failed:
841 	if (pdev->id_auto) {
842 		ida_free(&platform_devid_ida, pdev->id);
843 		pdev->id = PLATFORM_DEVID_AUTO;
844 	}
845 
846 	while (i--) {
847 		struct resource *r = &pdev->resource[i];
848 		if (r->parent)
849 			release_resource(r);
850 	}
851 
852 	return ret;
853 }
854 EXPORT_SYMBOL_GPL(platform_device_add);
855 
856 /**
857  * platform_device_del - remove a platform-level device
858  * @pdev: platform device we're removing
859  *
860  * Note that this function will also release all memory- and port-based
861  * resources owned by the device (@dev->resource).  This function must
862  * _only_ be externally called in error cases.  All other usage is a bug.
863  */
864 void platform_device_del(struct platform_device *pdev)
865 {
866 	u32 i;
867 
868 	if (!IS_ERR_OR_NULL(pdev)) {
869 		device_del(&pdev->dev);
870 
871 		if (pdev->id_auto) {
872 			ida_free(&platform_devid_ida, pdev->id);
873 			pdev->id = PLATFORM_DEVID_AUTO;
874 		}
875 
876 		for (i = 0; i < pdev->num_resources; i++) {
877 			struct resource *r = &pdev->resource[i];
878 			if (r->parent)
879 				release_resource(r);
880 		}
881 	}
882 }
883 EXPORT_SYMBOL_GPL(platform_device_del);
884 
885 /**
886  * platform_device_register - add a platform-level device
887  * @pdev: platform device we're adding
888  *
889  * NOTE: _Never_ directly free @pdev after calling this function, even if it
890  * returned an error! Always use platform_device_put() to give up the
891  * reference initialised in this function instead.
892  */
893 int platform_device_register(struct platform_device *pdev)
894 {
895 	device_initialize(&pdev->dev);
896 	setup_pdev_dma_masks(pdev);
897 	return platform_device_add(pdev);
898 }
899 EXPORT_SYMBOL_GPL(platform_device_register);
900 
901 /**
902  * platform_device_unregister - unregister a platform-level device
903  * @pdev: platform device we're unregistering
904  *
905  * Unregistration is done in 2 steps. First we release all resources
906  * and remove it from the subsystem, then we drop reference count by
907  * calling platform_device_put().
908  */
909 void platform_device_unregister(struct platform_device *pdev)
910 {
911 	platform_device_del(pdev);
912 	platform_device_put(pdev);
913 }
914 EXPORT_SYMBOL_GPL(platform_device_unregister);
915 
916 /**
917  * platform_device_register_full - add a platform-level device with
918  * resources and platform-specific data
919  *
920  * @pdevinfo: data used to create device
921  *
922  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
923  */
924 struct platform_device *platform_device_register_full(const struct platform_device_info *pdevinfo)
925 {
926 	int ret;
927 	struct platform_device *pdev;
928 
929 	/*
930 	 * Only one software node per device is allowed. Make sure we don't
931 	 * accept or create two.
932 	 */
933 	if ((pdevinfo->swnode && pdevinfo->properties) ||
934 	    (pdevinfo->swnode && is_software_node(pdevinfo->fwnode)) ||
935 	    (pdevinfo->properties && is_software_node(pdevinfo->fwnode)))
936 		return ERR_PTR(-EINVAL);
937 
938 	pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
939 	if (!pdev)
940 		return ERR_PTR(-ENOMEM);
941 
942 	pdev->dev.parent = pdevinfo->parent;
943 	device_set_node(&pdev->dev, fwnode_handle_get(pdevinfo->fwnode));
944 	dev_assign_of_node_reused(&pdev->dev, pdevinfo->of_node_reused);
945 
946 	if (pdevinfo->dma_mask) {
947 		pdev->platform_dma_mask = pdevinfo->dma_mask;
948 		pdev->dev.dma_mask = &pdev->platform_dma_mask;
949 		pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
950 	}
951 
952 	ret = platform_device_add_resources(pdev, pdevinfo->res, pdevinfo->num_res);
953 	if (ret)
954 		goto err;
955 
956 	ret = platform_device_add_data(pdev, pdevinfo->data, pdevinfo->size_data);
957 	if (ret)
958 		goto err;
959 
960 	if (pdevinfo->swnode) {
961 		ret = device_add_software_node(&pdev->dev, pdevinfo->swnode);
962 		if (ret)
963 			goto err;
964 	} else if (pdevinfo->properties) {
965 		ret = device_create_managed_software_node(&pdev->dev,
966 							  pdevinfo->properties, NULL);
967 		if (ret)
968 			goto err;
969 	}
970 
971 	ret = platform_device_add(pdev);
972 	if (ret) {
973 err:
974 		ACPI_COMPANION_SET(&pdev->dev, NULL);
975 		platform_device_put(pdev);
976 		return ERR_PTR(ret);
977 	}
978 
979 	return pdev;
980 }
981 EXPORT_SYMBOL_GPL(platform_device_register_full);
982 
983 /**
984  * __platform_driver_register - register a driver for platform-level devices
985  * @drv: platform driver structure
986  * @owner: owning module/driver
987  * @mod_name: module name string
988  */
989 int __platform_driver_register(struct platform_driver *drv, struct module *owner,
990 			       const char *mod_name)
991 {
992 	drv->driver.owner = owner;
993 	drv->driver.bus = &platform_bus_type;
994 	drv->driver.mod_name = mod_name;
995 
996 	return driver_register(&drv->driver);
997 }
998 EXPORT_SYMBOL_GPL(__platform_driver_register);
999 
1000 /**
1001  * platform_driver_unregister - unregister a driver for platform-level devices
1002  * @drv: platform driver structure
1003  */
1004 void platform_driver_unregister(struct platform_driver *drv)
1005 {
1006 	driver_unregister(&drv->driver);
1007 }
1008 EXPORT_SYMBOL_GPL(platform_driver_unregister);
1009 
1010 static int platform_probe_fail(struct platform_device *pdev)
1011 {
1012 	return -ENXIO;
1013 }
1014 
1015 static int is_bound_to_driver(struct device *dev, void *driver)
1016 {
1017 	if (dev->driver == driver)
1018 		return 1;
1019 	return 0;
1020 }
1021 
1022 /**
1023  * __platform_driver_probe - register driver for non-hotpluggable device
1024  * @drv: platform driver structure
1025  * @probe: the driver probe routine, probably from an __init section
1026  * @module: module which will be the owner of the driver
1027  * @mod_name: module name string
1028  *
1029  * Use this instead of platform_driver_register() when you know the device
1030  * is not hotpluggable and has already been registered, and you want to
1031  * remove its run-once probe() infrastructure from memory after the driver
1032  * has bound to the device.
1033  *
1034  * One typical use for this would be with drivers for controllers integrated
1035  * into system-on-chip processors, where the controller devices have been
1036  * configured as part of board setup.
1037  *
1038  * Note that this is incompatible with deferred probing.
1039  *
1040  * Returns zero if the driver registered and bound to a device, else returns
1041  * a negative error code and with the driver not registered.
1042  */
1043 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
1044 					     int (*probe)(struct platform_device *),
1045 					     struct module *module,
1046 					     const char *mod_name)
1047 {
1048 	int retval;
1049 
1050 	if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
1051 		pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
1052 		       drv->driver.name, __func__);
1053 		return -EINVAL;
1054 	}
1055 
1056 	/*
1057 	 * We have to run our probes synchronously because we check if
1058 	 * we find any devices to bind to and exit with error if there
1059 	 * are any.
1060 	 */
1061 	drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
1062 
1063 	/*
1064 	 * Prevent driver from requesting probe deferral to avoid further
1065 	 * futile probe attempts.
1066 	 */
1067 	drv->prevent_deferred_probe = true;
1068 
1069 	/* make sure driver won't have bind/unbind attributes */
1070 	drv->driver.suppress_bind_attrs = true;
1071 
1072 	/* temporary section violation during probe() */
1073 	drv->probe = probe;
1074 	retval = __platform_driver_register(drv, module, mod_name);
1075 	if (retval)
1076 		return retval;
1077 
1078 	/* Force all new probes of this driver to fail */
1079 	drv->probe = platform_probe_fail;
1080 
1081 	/* Walk all platform devices and see if any actually bound to this driver.
1082 	 * If not, return an error as the device should have done so by now.
1083 	 */
1084 	if (!bus_for_each_dev(&platform_bus_type, NULL, &drv->driver, is_bound_to_driver)) {
1085 		retval = -ENODEV;
1086 		platform_driver_unregister(drv);
1087 	}
1088 
1089 	return retval;
1090 }
1091 EXPORT_SYMBOL_GPL(__platform_driver_probe);
1092 
1093 /**
1094  * __platform_create_bundle - register driver and create corresponding device
1095  * @driver: platform driver structure
1096  * @probe: the driver probe routine, probably from an __init section
1097  * @res: set of resources that needs to be allocated for the device
1098  * @n_res: number of resources
1099  * @data: platform specific data for this platform device
1100  * @size: size of platform specific data
1101  * @module: module which will be the owner of the driver
1102  * @mod_name: module name string
1103  *
1104  * Use this in legacy-style modules that probe hardware directly and
1105  * register a single platform device and corresponding platform driver.
1106  *
1107  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
1108  */
1109 struct platform_device * __init_or_module
1110 __platform_create_bundle(struct platform_driver *driver,
1111 			 int (*probe)(struct platform_device *),
1112 			 struct resource *res, unsigned int n_res,
1113 			 const void *data, size_t size, struct module *module, const char *mod_name)
1114 {
1115 	struct platform_device *pdev;
1116 	int error;
1117 
1118 	pdev = platform_device_alloc(driver->driver.name, PLATFORM_DEVID_NONE);
1119 	if (!pdev) {
1120 		error = -ENOMEM;
1121 		goto err_out;
1122 	}
1123 
1124 	error = platform_device_add_resources(pdev, res, n_res);
1125 	if (error)
1126 		goto err_pdev_put;
1127 
1128 	error = platform_device_add_data(pdev, data, size);
1129 	if (error)
1130 		goto err_pdev_put;
1131 
1132 	error = platform_device_add(pdev);
1133 	if (error)
1134 		goto err_pdev_put;
1135 
1136 	error = __platform_driver_probe(driver, probe, module, mod_name);
1137 	if (error)
1138 		goto err_pdev_del;
1139 
1140 	return pdev;
1141 
1142 err_pdev_del:
1143 	platform_device_del(pdev);
1144 err_pdev_put:
1145 	platform_device_put(pdev);
1146 err_out:
1147 	return ERR_PTR(error);
1148 }
1149 EXPORT_SYMBOL_GPL(__platform_create_bundle);
1150 
1151 /**
1152  * __platform_register_drivers - register an array of platform drivers
1153  * @drivers: an array of drivers to register
1154  * @count: the number of drivers to register
1155  * @owner: module owning the drivers
1156  * @mod_name: module name string
1157  *
1158  * Registers platform drivers specified by an array. On failure to register a
1159  * driver, all previously registered drivers will be unregistered. Callers of
1160  * this API should use platform_unregister_drivers() to unregister drivers in
1161  * the reverse order.
1162  *
1163  * Returns: 0 on success or a negative error code on failure.
1164  */
1165 int __platform_register_drivers(struct platform_driver * const *drivers,
1166 				unsigned int count, struct module *owner, const char *mod_name)
1167 {
1168 	unsigned int i;
1169 	int err;
1170 
1171 	for (i = 0; i < count; i++) {
1172 		pr_debug("registering platform driver %ps\n", drivers[i]);
1173 
1174 		err = __platform_driver_register(drivers[i], owner, mod_name);
1175 		if (err < 0) {
1176 			pr_err("failed to register platform driver %ps: %d\n",
1177 			       drivers[i], err);
1178 			goto error;
1179 		}
1180 	}
1181 
1182 	return 0;
1183 
1184 error:
1185 	while (i--) {
1186 		pr_debug("unregistering platform driver %ps\n", drivers[i]);
1187 		platform_driver_unregister(drivers[i]);
1188 	}
1189 
1190 	return err;
1191 }
1192 EXPORT_SYMBOL_GPL(__platform_register_drivers);
1193 
1194 /**
1195  * platform_unregister_drivers - unregister an array of platform drivers
1196  * @drivers: an array of drivers to unregister
1197  * @count: the number of drivers to unregister
1198  *
1199  * Unregisters platform drivers specified by an array. This is typically used
1200  * to complement an earlier call to platform_register_drivers(). Drivers are
1201  * unregistered in the reverse order in which they were registered.
1202  */
1203 void platform_unregister_drivers(struct platform_driver * const *drivers,
1204 				 unsigned int count)
1205 {
1206 	while (count--) {
1207 		pr_debug("unregistering platform driver %ps\n", drivers[count]);
1208 		platform_driver_unregister(drivers[count]);
1209 	}
1210 }
1211 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
1212 
1213 static const struct platform_device_id *
1214 platform_match_id(const struct platform_device_id *id, struct platform_device *pdev)
1215 {
1216 	while (id->name[0]) {
1217 		if (strcmp(pdev->name, id->name) == 0) {
1218 			pdev->id_entry = id;
1219 			return id;
1220 		}
1221 		id++;
1222 	}
1223 	return NULL;
1224 }
1225 
1226 #ifdef CONFIG_PM_SLEEP
1227 
1228 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1229 {
1230 	struct platform_driver *pdrv = to_platform_driver(dev->driver);
1231 	struct platform_device *pdev = to_platform_device(dev);
1232 	int ret = 0;
1233 
1234 	if (dev->driver && pdrv->suspend)
1235 		ret = pdrv->suspend(pdev, mesg);
1236 
1237 	return ret;
1238 }
1239 
1240 static int platform_legacy_resume(struct device *dev)
1241 {
1242 	struct platform_driver *pdrv = to_platform_driver(dev->driver);
1243 	struct platform_device *pdev = to_platform_device(dev);
1244 	int ret = 0;
1245 
1246 	if (dev->driver && pdrv->resume)
1247 		ret = pdrv->resume(pdev);
1248 
1249 	return ret;
1250 }
1251 
1252 #endif /* CONFIG_PM_SLEEP */
1253 
1254 #ifdef CONFIG_SUSPEND
1255 
1256 int platform_pm_suspend(struct device *dev)
1257 {
1258 	const struct device_driver *drv = dev->driver;
1259 	int ret = 0;
1260 
1261 	if (!drv)
1262 		return 0;
1263 
1264 	if (drv->pm) {
1265 		if (drv->pm->suspend)
1266 			ret = drv->pm->suspend(dev);
1267 	} else {
1268 		ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1269 	}
1270 
1271 	return ret;
1272 }
1273 
1274 int platform_pm_resume(struct device *dev)
1275 {
1276 	const struct device_driver *drv = dev->driver;
1277 	int ret = 0;
1278 
1279 	if (!drv)
1280 		return 0;
1281 
1282 	if (drv->pm) {
1283 		if (drv->pm->resume)
1284 			ret = drv->pm->resume(dev);
1285 	} else {
1286 		ret = platform_legacy_resume(dev);
1287 	}
1288 
1289 	return ret;
1290 }
1291 
1292 #endif /* CONFIG_SUSPEND */
1293 
1294 #ifdef CONFIG_HIBERNATE_CALLBACKS
1295 
1296 int platform_pm_freeze(struct device *dev)
1297 {
1298 	const struct device_driver *drv = dev->driver;
1299 	int ret = 0;
1300 
1301 	if (!drv)
1302 		return 0;
1303 
1304 	if (drv->pm) {
1305 		if (drv->pm->freeze)
1306 			ret = drv->pm->freeze(dev);
1307 	} else {
1308 		ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1309 	}
1310 
1311 	return ret;
1312 }
1313 
1314 int platform_pm_thaw(struct device *dev)
1315 {
1316 	const struct device_driver *drv = dev->driver;
1317 	int ret = 0;
1318 
1319 	if (!drv)
1320 		return 0;
1321 
1322 	if (drv->pm) {
1323 		if (drv->pm->thaw)
1324 			ret = drv->pm->thaw(dev);
1325 	} else {
1326 		ret = platform_legacy_resume(dev);
1327 	}
1328 
1329 	return ret;
1330 }
1331 
1332 int platform_pm_poweroff(struct device *dev)
1333 {
1334 	const struct device_driver *drv = dev->driver;
1335 	int ret = 0;
1336 
1337 	if (!drv)
1338 		return 0;
1339 
1340 	if (drv->pm) {
1341 		if (drv->pm->poweroff)
1342 			ret = drv->pm->poweroff(dev);
1343 	} else {
1344 		ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1345 	}
1346 
1347 	return ret;
1348 }
1349 
1350 int platform_pm_restore(struct device *dev)
1351 {
1352 	const struct device_driver *drv = dev->driver;
1353 	int ret = 0;
1354 
1355 	if (!drv)
1356 		return 0;
1357 
1358 	if (drv->pm) {
1359 		if (drv->pm->restore)
1360 			ret = drv->pm->restore(dev);
1361 	} else {
1362 		ret = platform_legacy_resume(dev);
1363 	}
1364 
1365 	return ret;
1366 }
1367 
1368 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1369 
1370 /* modalias support enables more hands-off userspace setup:
1371  * (a) environment variable lets new-style hotplug events work once system is
1372  *     fully running:  "modprobe $MODALIAS"
1373  * (b) sysfs attribute lets new-style coldplug recover from hotplug events
1374  *     mishandled before system is fully running:  "modprobe $(cat modalias)"
1375  */
1376 static ssize_t modalias_show(struct device *dev,
1377 			     struct device_attribute *attr, char *buf)
1378 {
1379 	struct platform_device *pdev = to_platform_device(dev);
1380 	int len;
1381 
1382 	len = of_device_modalias(dev, buf, PAGE_SIZE);
1383 	if (len != -ENODEV)
1384 		return len;
1385 
1386 	len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
1387 	if (len != -ENODEV)
1388 		return len;
1389 
1390 	return sysfs_emit(buf, "platform:%s\n", pdev->name);
1391 }
1392 static DEVICE_ATTR_RO(modalias);
1393 
1394 static ssize_t numa_node_show(struct device *dev,
1395 			      struct device_attribute *attr, char *buf)
1396 {
1397 	return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1398 }
1399 static DEVICE_ATTR_RO(numa_node);
1400 
1401 static struct attribute *platform_dev_attrs[] = {
1402 	&dev_attr_modalias.attr,
1403 	&dev_attr_numa_node.attr,
1404 	NULL,
1405 };
1406 
1407 static umode_t platform_dev_attrs_visible(struct kobject *kobj,
1408 					  struct attribute *a, int n)
1409 {
1410 	struct device *dev = container_of(kobj, typeof(*dev), kobj);
1411 
1412 	if (a == &dev_attr_numa_node.attr && dev_to_node(dev) == NUMA_NO_NODE)
1413 		return 0;
1414 
1415 	return a->mode;
1416 }
1417 
1418 static const struct attribute_group platform_dev_group = {
1419 	.attrs = platform_dev_attrs,
1420 	.is_visible = platform_dev_attrs_visible,
1421 };
1422 __ATTRIBUTE_GROUPS(platform_dev);
1423 
1424 /**
1425  * platform_match - bind platform device to platform driver.
1426  * @dev: device.
1427  * @drv: driver.
1428  *
1429  * Platform device IDs are assumed to be encoded like this:
1430  * "<name><instance>", where <name> is a short description of the type of
1431  * device, like "pci" or "floppy", and <instance> is the enumerated
1432  * instance of the device, like '0' or '42'.  Driver IDs are simply
1433  * "<name>".  So, extract the <name> from the platform_device structure,
1434  * and compare it against the name of the driver. Return whether they match
1435  * or not.
1436  */
1437 static int platform_match(struct device *dev, const struct device_driver *drv)
1438 {
1439 	struct platform_device *pdev = to_platform_device(dev);
1440 	struct platform_driver *pdrv = to_platform_driver(drv);
1441 	int ret;
1442 
1443 	/* When driver_override is set, only bind to the matching driver */
1444 	ret = device_match_driver_override(dev, drv);
1445 	if (ret >= 0)
1446 		return ret;
1447 
1448 	/* Attempt an OF style match first */
1449 	if (of_driver_match_device(dev, drv))
1450 		return 1;
1451 
1452 	/* Then try ACPI style match */
1453 	if (acpi_driver_match_device(dev, drv))
1454 		return 1;
1455 
1456 	/* Then try to match against the id table */
1457 	if (pdrv->id_table)
1458 		return platform_match_id(pdrv->id_table, pdev) != NULL;
1459 
1460 	/* fall-back to driver name match */
1461 	return (strcmp(pdev->name, drv->name) == 0);
1462 }
1463 
1464 static int platform_uevent(const struct device *dev, struct kobj_uevent_env *env)
1465 {
1466 	const struct platform_device *pdev = to_platform_device(dev);
1467 	int rc;
1468 
1469 	/* Some devices have extra OF data and an OF-style MODALIAS */
1470 	rc = of_device_uevent_modalias(dev, env);
1471 	if (rc != -ENODEV)
1472 		return rc;
1473 
1474 	rc = acpi_device_uevent_modalias(dev, env);
1475 	if (rc != -ENODEV)
1476 		return rc;
1477 
1478 	add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, pdev->name);
1479 	return 0;
1480 }
1481 
1482 static int platform_probe(struct device *_dev)
1483 {
1484 	struct platform_driver *drv = to_platform_driver(_dev->driver);
1485 	struct platform_device *dev = to_platform_device(_dev);
1486 	int ret;
1487 
1488 	/*
1489 	 * A driver registered using platform_driver_probe() cannot be bound
1490 	 * again later because the probe function usually lives in __init code
1491 	 * and so is gone. For these drivers .probe is set to
1492 	 * platform_probe_fail in __platform_driver_probe(). Don't even prepare
1493 	 * clocks and PM domains for these to match the traditional behaviour.
1494 	 */
1495 	if (unlikely(drv->probe == platform_probe_fail))
1496 		return -ENXIO;
1497 
1498 	ret = of_clk_set_defaults(_dev->of_node, false);
1499 	if (ret < 0)
1500 		return ret;
1501 
1502 	ret = dev_pm_domain_attach(_dev, PD_FLAG_ATTACH_POWER_ON |
1503 					 PD_FLAG_DETACH_POWER_OFF);
1504 	if (ret)
1505 		goto out;
1506 
1507 	if (drv->probe)
1508 		ret = drv->probe(dev);
1509 
1510 out:
1511 	if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
1512 		dev_warn(_dev, "probe deferral not supported\n");
1513 		ret = -ENXIO;
1514 	}
1515 
1516 	return ret;
1517 }
1518 
1519 static void platform_remove(struct device *_dev)
1520 {
1521 	struct platform_driver *drv = to_platform_driver(_dev->driver);
1522 	struct platform_device *dev = to_platform_device(_dev);
1523 
1524 	if (drv->remove)
1525 		drv->remove(dev);
1526 }
1527 
1528 static void platform_shutdown(struct device *_dev)
1529 {
1530 	struct platform_device *dev = to_platform_device(_dev);
1531 	struct platform_driver *drv;
1532 
1533 	if (!_dev->driver)
1534 		return;
1535 
1536 	drv = to_platform_driver(_dev->driver);
1537 	if (drv->shutdown)
1538 		drv->shutdown(dev);
1539 }
1540 
1541 static int platform_dma_configure(struct device *dev)
1542 {
1543 	struct device_driver *drv = READ_ONCE(dev->driver);
1544 	struct fwnode_handle *fwnode = dev_fwnode(dev);
1545 	enum dev_dma_attr attr;
1546 	int ret = 0;
1547 
1548 	if (is_of_node(fwnode)) {
1549 		ret = of_dma_configure(dev, to_of_node(fwnode), true);
1550 	} else if (is_acpi_device_node(fwnode)) {
1551 		attr = acpi_get_dma_attr(to_acpi_device_node(fwnode));
1552 		ret = acpi_dma_configure(dev, attr);
1553 	}
1554 	/* @dev->driver may not be valid when we're called from the IOMMU layer */
1555 	if (ret || !drv || to_platform_driver(drv)->driver_managed_dma)
1556 		return ret;
1557 
1558 	ret = iommu_device_use_default_domain(dev);
1559 	if (ret)
1560 		arch_teardown_dma_ops(dev);
1561 
1562 	return ret;
1563 }
1564 
1565 static void platform_dma_cleanup(struct device *dev)
1566 {
1567 	struct platform_driver *drv = to_platform_driver(dev->driver);
1568 
1569 	if (!drv->driver_managed_dma)
1570 		iommu_device_unuse_default_domain(dev);
1571 }
1572 
1573 static const struct dev_pm_ops platform_dev_pm_ops = {
1574 	SET_RUNTIME_PM_OPS(pm_generic_runtime_suspend, pm_generic_runtime_resume, NULL)
1575 	USE_PLATFORM_PM_SLEEP_OPS
1576 };
1577 
1578 const struct bus_type platform_bus_type = {
1579 	.name		= "platform",
1580 	.dev_groups	= platform_dev_groups,
1581 	.driver_override = true,
1582 	.match		= platform_match,
1583 	.uevent		= platform_uevent,
1584 	.probe		= platform_probe,
1585 	.remove		= platform_remove,
1586 	.shutdown	= platform_shutdown,
1587 	.dma_configure	= platform_dma_configure,
1588 	.dma_cleanup	= platform_dma_cleanup,
1589 	.pm		= &platform_dev_pm_ops,
1590 };
1591 EXPORT_SYMBOL_GPL(platform_bus_type);
1592 
1593 static inline int __platform_match(struct device *dev, const void *drv)
1594 {
1595 	return platform_match(dev, (struct device_driver *)drv);
1596 }
1597 
1598 /**
1599  * platform_find_device_by_driver - Find a platform device with a given
1600  * driver.
1601  * @start: The device to start the search from.
1602  * @drv: The device driver to look for.
1603  */
1604 struct device *platform_find_device_by_driver(struct device *start,
1605 					      const struct device_driver *drv)
1606 {
1607 	return bus_find_device(&platform_bus_type, start, drv,
1608 			       __platform_match);
1609 }
1610 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1611 
1612 void __weak __init early_platform_cleanup(void) { }
1613 
1614 int __init platform_bus_init(void)
1615 {
1616 	int error;
1617 
1618 	early_platform_cleanup();
1619 
1620 	error = device_register(&platform_bus);
1621 	if (error) {
1622 		put_device(&platform_bus);
1623 		return error;
1624 	}
1625 	error =  bus_register(&platform_bus_type);
1626 	if (error)
1627 		device_unregister(&platform_bus);
1628 
1629 	return error;
1630 }
1631