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