blob: dbb31d61e31027b4e7c19d44e83bb5172bfacca6 [file] [log] [blame]
Mika Westerberg91e56872012-10-31 22:45:02 +01001/*
2 * ACPI support for platform bus type.
3 *
4 * Copyright (C) 2012, Intel Corporation
5 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
6 * Mathias Nyman <mathias.nyman@linux.intel.com>
7 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/acpi.h>
15#include <linux/device.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/platform_device.h>
19
20ACPI_MODULE_NAME("platform");
21
22struct resource_info {
23 struct device *dev;
24 struct resource *res;
25 size_t n, cur;
26};
27
28static acpi_status acpi_platform_count_resources(struct acpi_resource *res,
29 void *data)
30{
31 struct acpi_resource_extended_irq *acpi_xirq;
32 struct resource_info *ri = data;
33
34 switch (res->type) {
35 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
36 case ACPI_RESOURCE_TYPE_IRQ:
37 ri->n++;
38 break;
39 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
40 acpi_xirq = &res->data.extended_irq;
41 ri->n += acpi_xirq->interrupt_count;
42 break;
43 case ACPI_RESOURCE_TYPE_ADDRESS32:
44 if (res->data.address32.resource_type == ACPI_IO_RANGE)
45 ri->n++;
46 break;
47 }
48
49 return AE_OK;
50}
51
52static acpi_status acpi_platform_add_resources(struct acpi_resource *res,
53 void *data)
54{
55 struct acpi_resource_fixed_memory32 *acpi_mem;
56 struct acpi_resource_address32 *acpi_add32;
57 struct acpi_resource_extended_irq *acpi_xirq;
58 struct acpi_resource_irq *acpi_irq;
59 struct resource_info *ri = data;
60 struct resource *r;
61 int irq, i;
62
63 switch (res->type) {
64 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
65 acpi_mem = &res->data.fixed_memory32;
66 r = &ri->res[ri->cur++];
67
68 r->start = acpi_mem->address;
69 r->end = r->start + acpi_mem->address_length - 1;
70 r->flags = IORESOURCE_MEM;
71
72 dev_dbg(ri->dev, "Memory32Fixed %pR\n", r);
73 break;
74
75 case ACPI_RESOURCE_TYPE_ADDRESS32:
76 acpi_add32 = &res->data.address32;
77
78 if (acpi_add32->resource_type == ACPI_IO_RANGE) {
79 r = &ri->res[ri->cur++];
80 r->start = acpi_add32->minimum;
81 r->end = r->start + acpi_add32->address_length - 1;
82 r->flags = IORESOURCE_IO;
83 dev_dbg(ri->dev, "Address32 %pR\n", r);
84 }
85 break;
86
87 case ACPI_RESOURCE_TYPE_IRQ:
88 acpi_irq = &res->data.irq;
89 r = &ri->res[ri->cur++];
90
91 irq = acpi_register_gsi(ri->dev,
92 acpi_irq->interrupts[0],
93 acpi_irq->triggering,
94 acpi_irq->polarity);
95
96 r->start = r->end = irq;
97 r->flags = IORESOURCE_IRQ;
98
99 dev_dbg(ri->dev, "IRQ %pR\n", r);
100 break;
101
102 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
103 acpi_xirq = &res->data.extended_irq;
104
105 for (i = 0; i < acpi_xirq->interrupt_count; i++, ri->cur++) {
106 r = &ri->res[ri->cur];
107 irq = acpi_register_gsi(ri->dev,
108 acpi_xirq->interrupts[i],
109 acpi_xirq->triggering,
110 acpi_xirq->polarity);
111
112 r->start = r->end = irq;
113 r->flags = IORESOURCE_IRQ;
114
115 dev_dbg(ri->dev, "Interrupt %pR\n", r);
116 }
117 break;
118 }
119
120 return AE_OK;
121}
122
Mika Westerberg91e56872012-10-31 22:45:02 +0100123/**
124 * acpi_create_platform_device - Create platform device for ACPI device node
125 * @adev: ACPI device node to create a platform device for.
126 *
127 * Check if the given @adev can be represented as a platform device and, if
128 * that's the case, create and register a platform device, populate its common
129 * resources and returns a pointer to it. Otherwise, return %NULL.
130 *
131 * The platform device's name will be taken from the @adev's _HID and _UID.
132 */
133struct platform_device *acpi_create_platform_device(struct acpi_device *adev)
134{
135 struct platform_device *pdev = NULL;
136 struct acpi_device *acpi_parent;
137 struct device *parent = NULL;
138 struct resource_info ri;
139 acpi_status status;
Mika Westerberg91e56872012-10-31 22:45:02 +0100140
141 /* If the ACPI node already has a physical device attached, skip it. */
142 if (adev->physical_node_count)
143 return NULL;
144
Mika Westerberg91e56872012-10-31 22:45:02 +0100145 memset(&ri, 0, sizeof(ri));
Mika Westerberg91e56872012-10-31 22:45:02 +0100146 /* First, count the resources. */
147 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
148 acpi_platform_count_resources, &ri);
149 if (ACPI_FAILURE(status) || !ri.n)
150 return NULL;
151
152 /* Next, allocate memory for all the resources and populate it. */
153 ri.dev = &adev->dev;
154 ri.res = kzalloc(ri.n * sizeof(struct resource), GFP_KERNEL);
155 if (!ri.res) {
156 dev_err(&adev->dev,
157 "failed to allocate memory for resources\n");
158 return NULL;
159 }
160
161 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
162 acpi_platform_add_resources, &ri);
163 if (ACPI_FAILURE(status)) {
164 dev_err(&adev->dev, "failed to walk resources\n");
165 goto out;
166 }
167
168 if (WARN_ON(ri.n != ri.cur))
169 goto out;
170
171 /*
172 * If the ACPI node has a parent and that parent has a physical device
173 * attached to it, that physical device should be the parent of the
174 * platform device we are about to create.
175 */
176 acpi_parent = adev->parent;
177 if (acpi_parent) {
178 struct acpi_device_physical_node *entry;
179 struct list_head *list;
180
181 mutex_lock(&acpi_parent->physical_node_lock);
182 list = &acpi_parent->physical_node_list;
183 if (!list_empty(list)) {
184 entry = list_first_entry(list,
185 struct acpi_device_physical_node,
186 node);
187 parent = entry->dev;
188 }
189 mutex_unlock(&acpi_parent->physical_node_lock);
190 }
Mika Westerbergb4b6cae2012-11-10 23:16:02 +0100191 pdev = platform_device_register_resndata(parent, dev_name(&adev->dev),
192 -1, ri.res, ri.n, NULL, 0);
Mika Westerberg91e56872012-10-31 22:45:02 +0100193 if (IS_ERR(pdev)) {
194 dev_err(&adev->dev, "platform device creation failed: %ld\n",
195 PTR_ERR(pdev));
196 pdev = NULL;
197 } else {
198 dev_dbg(&adev->dev, "created platform device %s\n",
199 dev_name(&pdev->dev));
200 }
201
202 out:
203 kfree(ri.res);
204 return pdev;
205}
206
207static acpi_status acpi_platform_match(acpi_handle handle, u32 depth,
208 void *data, void **return_value)
209{
210 struct platform_device *pdev = data;
211 struct acpi_device *adev;
212 acpi_status status;
213
214 status = acpi_bus_get_device(handle, &adev);
215 if (ACPI_FAILURE(status))
216 return status;
217
218 /* Skip ACPI devices that have physical device attached */
219 if (adev->physical_node_count)
220 return AE_OK;
221
Mika Westerbergb4b6cae2012-11-10 23:16:02 +0100222 if (!strcmp(dev_name(&pdev->dev), dev_name(&adev->dev))) {
Mika Westerberg91e56872012-10-31 22:45:02 +0100223 *(acpi_handle *)return_value = handle;
224 return AE_CTRL_TERMINATE;
225 }
226
227 return AE_OK;
228}
229
230static int acpi_platform_find_device(struct device *dev, acpi_handle *handle)
231{
232 struct platform_device *pdev = to_platform_device(dev);
Mika Westerbergb4b6cae2012-11-10 23:16:02 +0100233 char *name, *tmp, *hid;
234
235 /*
236 * The platform device is named using the ACPI device name
237 * _HID:INSTANCE so we strip the INSTANCE out in order to find the
238 * correct device using its _HID.
239 */
240 name = kstrdup(dev_name(dev), GFP_KERNEL);
241 if (!name)
242 return -ENOMEM;
243
244 tmp = name;
245 hid = strsep(&tmp, ":");
246 if (!hid) {
247 kfree(name);
248 return -ENODEV;
249 }
Mika Westerberg91e56872012-10-31 22:45:02 +0100250
251 *handle = NULL;
Mika Westerbergb4b6cae2012-11-10 23:16:02 +0100252 acpi_get_devices(hid, acpi_platform_match, pdev, handle);
Mika Westerberg91e56872012-10-31 22:45:02 +0100253
Mika Westerbergb4b6cae2012-11-10 23:16:02 +0100254 kfree(name);
Mika Westerberg91e56872012-10-31 22:45:02 +0100255 return *handle ? 0 : -ENODEV;
256}
257
258static struct acpi_bus_type acpi_platform_bus = {
259 .bus = &platform_bus_type,
260 .find_device = acpi_platform_find_device,
261};
262
263static int __init acpi_platform_init(void)
264{
265 return register_acpi_bus_type(&acpi_platform_bus);
266}
267arch_initcall(acpi_platform_init);