xref: /OK3568_Linux_fs/kernel/drivers/acpi/acpi_platform.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * ACPI support for platform bus type.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2012, Intel Corporation
6*4882a593Smuzhiyun  * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
7*4882a593Smuzhiyun  *          Mathias Nyman <mathias.nyman@linux.intel.com>
8*4882a593Smuzhiyun  *          Rafael J. Wysocki <rafael.j.wysocki@intel.com>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/acpi.h>
12*4882a593Smuzhiyun #include <linux/device.h>
13*4882a593Smuzhiyun #include <linux/err.h>
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/dma-mapping.h>
17*4882a593Smuzhiyun #include <linux/pci.h>
18*4882a593Smuzhiyun #include <linux/platform_device.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include "internal.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun static const struct acpi_device_id forbidden_id_list[] = {
23*4882a593Smuzhiyun 	{"PNP0000",  0},	/* PIC */
24*4882a593Smuzhiyun 	{"PNP0100",  0},	/* Timer */
25*4882a593Smuzhiyun 	{"PNP0200",  0},	/* AT DMA Controller */
26*4882a593Smuzhiyun 	{"ACPI0009", 0},	/* IOxAPIC */
27*4882a593Smuzhiyun 	{"ACPI000A", 0},	/* IOAPIC */
28*4882a593Smuzhiyun 	{"SMB0001",  0},	/* ACPI SMBUS virtual device */
29*4882a593Smuzhiyun 	{"", 0},
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun 
acpi_platform_device_find_by_companion(struct acpi_device * adev)32*4882a593Smuzhiyun static struct platform_device *acpi_platform_device_find_by_companion(struct acpi_device *adev)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun 	struct device *dev;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	dev = bus_find_device_by_acpi_dev(&platform_bus_type, adev);
37*4882a593Smuzhiyun 	return dev ? to_platform_device(dev) : NULL;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
acpi_platform_device_remove_notify(struct notifier_block * nb,unsigned long value,void * arg)40*4882a593Smuzhiyun static int acpi_platform_device_remove_notify(struct notifier_block *nb,
41*4882a593Smuzhiyun 					      unsigned long value, void *arg)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	struct acpi_device *adev = arg;
44*4882a593Smuzhiyun 	struct platform_device *pdev;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	switch (value) {
47*4882a593Smuzhiyun 	case ACPI_RECONFIG_DEVICE_ADD:
48*4882a593Smuzhiyun 		/* Nothing to do here */
49*4882a593Smuzhiyun 		break;
50*4882a593Smuzhiyun 	case ACPI_RECONFIG_DEVICE_REMOVE:
51*4882a593Smuzhiyun 		if (!acpi_device_enumerated(adev))
52*4882a593Smuzhiyun 			break;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 		pdev = acpi_platform_device_find_by_companion(adev);
55*4882a593Smuzhiyun 		if (!pdev)
56*4882a593Smuzhiyun 			break;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 		platform_device_unregister(pdev);
59*4882a593Smuzhiyun 		put_device(&pdev->dev);
60*4882a593Smuzhiyun 		break;
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	return NOTIFY_OK;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun static struct notifier_block acpi_platform_notifier = {
67*4882a593Smuzhiyun 	.notifier_call = acpi_platform_device_remove_notify,
68*4882a593Smuzhiyun };
69*4882a593Smuzhiyun 
acpi_platform_fill_resource(struct acpi_device * adev,const struct resource * src,struct resource * dest)70*4882a593Smuzhiyun static void acpi_platform_fill_resource(struct acpi_device *adev,
71*4882a593Smuzhiyun 	const struct resource *src, struct resource *dest)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	struct device *parent;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	*dest = *src;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	/*
78*4882a593Smuzhiyun 	 * If the device has parent we need to take its resources into
79*4882a593Smuzhiyun 	 * account as well because this device might consume part of those.
80*4882a593Smuzhiyun 	 */
81*4882a593Smuzhiyun 	parent = acpi_get_first_physical_node(adev->parent);
82*4882a593Smuzhiyun 	if (parent && dev_is_pci(parent))
83*4882a593Smuzhiyun 		dest->parent = pci_find_resource(to_pci_dev(parent), dest);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /**
87*4882a593Smuzhiyun  * acpi_create_platform_device - Create platform device for ACPI device node
88*4882a593Smuzhiyun  * @adev: ACPI device node to create a platform device for.
89*4882a593Smuzhiyun  * @properties: Optional collection of build-in properties.
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  * Check if the given @adev can be represented as a platform device and, if
92*4882a593Smuzhiyun  * that's the case, create and register a platform device, populate its common
93*4882a593Smuzhiyun  * resources and returns a pointer to it.  Otherwise, return %NULL.
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * Name of the platform device will be the same as @adev's.
96*4882a593Smuzhiyun  */
acpi_create_platform_device(struct acpi_device * adev,struct property_entry * properties)97*4882a593Smuzhiyun struct platform_device *acpi_create_platform_device(struct acpi_device *adev,
98*4882a593Smuzhiyun 					struct property_entry *properties)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	struct platform_device *pdev = NULL;
101*4882a593Smuzhiyun 	struct platform_device_info pdevinfo;
102*4882a593Smuzhiyun 	struct resource_entry *rentry;
103*4882a593Smuzhiyun 	struct list_head resource_list;
104*4882a593Smuzhiyun 	struct resource *resources = NULL;
105*4882a593Smuzhiyun 	int count;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	/* If the ACPI node already has a physical device attached, skip it. */
108*4882a593Smuzhiyun 	if (adev->physical_node_count)
109*4882a593Smuzhiyun 		return NULL;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	if (!acpi_match_device_ids(adev, forbidden_id_list))
112*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	INIT_LIST_HEAD(&resource_list);
115*4882a593Smuzhiyun 	count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
116*4882a593Smuzhiyun 	if (count < 0) {
117*4882a593Smuzhiyun 		return NULL;
118*4882a593Smuzhiyun 	} else if (count > 0) {
119*4882a593Smuzhiyun 		resources = kcalloc(count, sizeof(struct resource),
120*4882a593Smuzhiyun 				    GFP_KERNEL);
121*4882a593Smuzhiyun 		if (!resources) {
122*4882a593Smuzhiyun 			dev_err(&adev->dev, "No memory for resources\n");
123*4882a593Smuzhiyun 			acpi_dev_free_resource_list(&resource_list);
124*4882a593Smuzhiyun 			return ERR_PTR(-ENOMEM);
125*4882a593Smuzhiyun 		}
126*4882a593Smuzhiyun 		count = 0;
127*4882a593Smuzhiyun 		list_for_each_entry(rentry, &resource_list, node)
128*4882a593Smuzhiyun 			acpi_platform_fill_resource(adev, rentry->res,
129*4882a593Smuzhiyun 						    &resources[count++]);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 		acpi_dev_free_resource_list(&resource_list);
132*4882a593Smuzhiyun 	}
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	memset(&pdevinfo, 0, sizeof(pdevinfo));
135*4882a593Smuzhiyun 	/*
136*4882a593Smuzhiyun 	 * If the ACPI node has a parent and that parent has a physical device
137*4882a593Smuzhiyun 	 * attached to it, that physical device should be the parent of the
138*4882a593Smuzhiyun 	 * platform device we are about to create.
139*4882a593Smuzhiyun 	 */
140*4882a593Smuzhiyun 	pdevinfo.parent = adev->parent ?
141*4882a593Smuzhiyun 		acpi_get_first_physical_node(adev->parent) : NULL;
142*4882a593Smuzhiyun 	pdevinfo.name = dev_name(&adev->dev);
143*4882a593Smuzhiyun 	pdevinfo.id = -1;
144*4882a593Smuzhiyun 	pdevinfo.res = resources;
145*4882a593Smuzhiyun 	pdevinfo.num_res = count;
146*4882a593Smuzhiyun 	pdevinfo.fwnode = acpi_fwnode_handle(adev);
147*4882a593Smuzhiyun 	pdevinfo.properties = properties;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	if (acpi_dma_supported(adev))
150*4882a593Smuzhiyun 		pdevinfo.dma_mask = DMA_BIT_MASK(32);
151*4882a593Smuzhiyun 	else
152*4882a593Smuzhiyun 		pdevinfo.dma_mask = 0;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	pdev = platform_device_register_full(&pdevinfo);
155*4882a593Smuzhiyun 	if (IS_ERR(pdev))
156*4882a593Smuzhiyun 		dev_err(&adev->dev, "platform device creation failed: %ld\n",
157*4882a593Smuzhiyun 			PTR_ERR(pdev));
158*4882a593Smuzhiyun 	else {
159*4882a593Smuzhiyun 		set_dev_node(&pdev->dev, acpi_get_node(adev->handle));
160*4882a593Smuzhiyun 		dev_dbg(&adev->dev, "created platform device %s\n",
161*4882a593Smuzhiyun 			dev_name(&pdev->dev));
162*4882a593Smuzhiyun 	}
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	kfree(resources);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	return pdev;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_create_platform_device);
169*4882a593Smuzhiyun 
acpi_platform_init(void)170*4882a593Smuzhiyun void __init acpi_platform_init(void)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	acpi_reconfig_notifier_register(&acpi_platform_notifier);
173*4882a593Smuzhiyun }
174