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