Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | ACPI_MODULE_NAME("platform"); |
| 21 | |
| 22 | struct resource_info { |
| 23 | struct device *dev; |
| 24 | struct resource *res; |
| 25 | size_t n, cur; |
| 26 | }; |
| 27 | |
| 28 | static acpi_status acpi_platform_count_resources(struct acpi_resource *res, |
| 29 | void *data) |
| 30 | { |
| 31 | struct acpi_resource_extended_irq *acpi_xirq; |
Rafael J. Wysocki | 97d69dc | 2012-11-15 00:30:12 +0100 | [diff] [blame^] | 32 | struct acpi_resource_irq *acpi_irq; |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 33 | struct resource_info *ri = data; |
| 34 | |
| 35 | switch (res->type) { |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 36 | case ACPI_RESOURCE_TYPE_IRQ: |
Rafael J. Wysocki | 97d69dc | 2012-11-15 00:30:12 +0100 | [diff] [blame^] | 37 | acpi_irq = &res->data.irq; |
| 38 | ri->n += acpi_irq->interrupt_count; |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 39 | break; |
| 40 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: |
| 41 | acpi_xirq = &res->data.extended_irq; |
| 42 | ri->n += acpi_xirq->interrupt_count; |
| 43 | break; |
Rafael J. Wysocki | 97d69dc | 2012-11-15 00:30:12 +0100 | [diff] [blame^] | 44 | default: |
| 45 | ri->n++; |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | return AE_OK; |
| 49 | } |
| 50 | |
| 51 | static acpi_status acpi_platform_add_resources(struct acpi_resource *res, |
| 52 | void *data) |
| 53 | { |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 54 | struct resource_info *ri = data; |
| 55 | struct resource *r; |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 56 | |
Rafael J. Wysocki | 97d69dc | 2012-11-15 00:30:12 +0100 | [diff] [blame^] | 57 | r = ri->res + ri->cur; |
| 58 | if (acpi_dev_resource_memory(res, r) |
| 59 | || acpi_dev_resource_io(res, r) |
| 60 | || acpi_dev_resource_address_space(res, r) |
| 61 | || acpi_dev_resource_ext_address_space(res, r)) { |
| 62 | ri->cur++; |
| 63 | return AE_OK; |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 64 | } |
Rafael J. Wysocki | 97d69dc | 2012-11-15 00:30:12 +0100 | [diff] [blame^] | 65 | if (acpi_dev_resource_interrupt(res, 0, r)) { |
| 66 | int i; |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 67 | |
Rafael J. Wysocki | 97d69dc | 2012-11-15 00:30:12 +0100 | [diff] [blame^] | 68 | r++; |
| 69 | for (i = 1; acpi_dev_resource_interrupt(res, i, r); i++) |
| 70 | r++; |
| 71 | |
| 72 | ri->cur += i; |
| 73 | } |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 74 | return AE_OK; |
| 75 | } |
| 76 | |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 77 | /** |
| 78 | * acpi_create_platform_device - Create platform device for ACPI device node |
| 79 | * @adev: ACPI device node to create a platform device for. |
| 80 | * |
| 81 | * Check if the given @adev can be represented as a platform device and, if |
| 82 | * that's the case, create and register a platform device, populate its common |
| 83 | * resources and returns a pointer to it. Otherwise, return %NULL. |
| 84 | * |
| 85 | * The platform device's name will be taken from the @adev's _HID and _UID. |
| 86 | */ |
| 87 | struct platform_device *acpi_create_platform_device(struct acpi_device *adev) |
| 88 | { |
| 89 | struct platform_device *pdev = NULL; |
| 90 | struct acpi_device *acpi_parent; |
| 91 | struct device *parent = NULL; |
| 92 | struct resource_info ri; |
| 93 | acpi_status status; |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 94 | |
| 95 | /* If the ACPI node already has a physical device attached, skip it. */ |
| 96 | if (adev->physical_node_count) |
| 97 | return NULL; |
| 98 | |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 99 | memset(&ri, 0, sizeof(ri)); |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 100 | /* First, count the resources. */ |
| 101 | status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS, |
| 102 | acpi_platform_count_resources, &ri); |
| 103 | if (ACPI_FAILURE(status) || !ri.n) |
| 104 | return NULL; |
| 105 | |
| 106 | /* Next, allocate memory for all the resources and populate it. */ |
| 107 | ri.dev = &adev->dev; |
| 108 | ri.res = kzalloc(ri.n * sizeof(struct resource), GFP_KERNEL); |
| 109 | if (!ri.res) { |
| 110 | dev_err(&adev->dev, |
| 111 | "failed to allocate memory for resources\n"); |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS, |
| 116 | acpi_platform_add_resources, &ri); |
| 117 | if (ACPI_FAILURE(status)) { |
| 118 | dev_err(&adev->dev, "failed to walk resources\n"); |
| 119 | goto out; |
| 120 | } |
| 121 | |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 122 | /* |
| 123 | * If the ACPI node has a parent and that parent has a physical device |
| 124 | * attached to it, that physical device should be the parent of the |
| 125 | * platform device we are about to create. |
| 126 | */ |
| 127 | acpi_parent = adev->parent; |
| 128 | if (acpi_parent) { |
| 129 | struct acpi_device_physical_node *entry; |
| 130 | struct list_head *list; |
| 131 | |
| 132 | mutex_lock(&acpi_parent->physical_node_lock); |
| 133 | list = &acpi_parent->physical_node_list; |
| 134 | if (!list_empty(list)) { |
| 135 | entry = list_first_entry(list, |
| 136 | struct acpi_device_physical_node, |
| 137 | node); |
| 138 | parent = entry->dev; |
| 139 | } |
| 140 | mutex_unlock(&acpi_parent->physical_node_lock); |
| 141 | } |
Mika Westerberg | b4b6cae | 2012-11-10 23:16:02 +0100 | [diff] [blame] | 142 | pdev = platform_device_register_resndata(parent, dev_name(&adev->dev), |
Rafael J. Wysocki | 97d69dc | 2012-11-15 00:30:12 +0100 | [diff] [blame^] | 143 | -1, ri.res, ri.cur, NULL, 0); |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 144 | if (IS_ERR(pdev)) { |
| 145 | dev_err(&adev->dev, "platform device creation failed: %ld\n", |
| 146 | PTR_ERR(pdev)); |
| 147 | pdev = NULL; |
| 148 | } else { |
| 149 | dev_dbg(&adev->dev, "created platform device %s\n", |
| 150 | dev_name(&pdev->dev)); |
| 151 | } |
| 152 | |
| 153 | out: |
| 154 | kfree(ri.res); |
| 155 | return pdev; |
| 156 | } |
| 157 | |
| 158 | static acpi_status acpi_platform_match(acpi_handle handle, u32 depth, |
| 159 | void *data, void **return_value) |
| 160 | { |
| 161 | struct platform_device *pdev = data; |
| 162 | struct acpi_device *adev; |
| 163 | acpi_status status; |
| 164 | |
| 165 | status = acpi_bus_get_device(handle, &adev); |
| 166 | if (ACPI_FAILURE(status)) |
| 167 | return status; |
| 168 | |
| 169 | /* Skip ACPI devices that have physical device attached */ |
| 170 | if (adev->physical_node_count) |
| 171 | return AE_OK; |
| 172 | |
Mika Westerberg | b4b6cae | 2012-11-10 23:16:02 +0100 | [diff] [blame] | 173 | if (!strcmp(dev_name(&pdev->dev), dev_name(&adev->dev))) { |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 174 | *(acpi_handle *)return_value = handle; |
| 175 | return AE_CTRL_TERMINATE; |
| 176 | } |
| 177 | |
| 178 | return AE_OK; |
| 179 | } |
| 180 | |
| 181 | static int acpi_platform_find_device(struct device *dev, acpi_handle *handle) |
| 182 | { |
| 183 | struct platform_device *pdev = to_platform_device(dev); |
Mika Westerberg | b4b6cae | 2012-11-10 23:16:02 +0100 | [diff] [blame] | 184 | char *name, *tmp, *hid; |
| 185 | |
| 186 | /* |
| 187 | * The platform device is named using the ACPI device name |
| 188 | * _HID:INSTANCE so we strip the INSTANCE out in order to find the |
| 189 | * correct device using its _HID. |
| 190 | */ |
| 191 | name = kstrdup(dev_name(dev), GFP_KERNEL); |
| 192 | if (!name) |
| 193 | return -ENOMEM; |
| 194 | |
| 195 | tmp = name; |
| 196 | hid = strsep(&tmp, ":"); |
| 197 | if (!hid) { |
| 198 | kfree(name); |
| 199 | return -ENODEV; |
| 200 | } |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 201 | |
| 202 | *handle = NULL; |
Mika Westerberg | b4b6cae | 2012-11-10 23:16:02 +0100 | [diff] [blame] | 203 | acpi_get_devices(hid, acpi_platform_match, pdev, handle); |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 204 | |
Mika Westerberg | b4b6cae | 2012-11-10 23:16:02 +0100 | [diff] [blame] | 205 | kfree(name); |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 206 | return *handle ? 0 : -ENODEV; |
| 207 | } |
| 208 | |
| 209 | static struct acpi_bus_type acpi_platform_bus = { |
| 210 | .bus = &platform_bus_type, |
| 211 | .find_device = acpi_platform_find_device, |
| 212 | }; |
| 213 | |
| 214 | static int __init acpi_platform_init(void) |
| 215 | { |
| 216 | return register_acpi_bus_type(&acpi_platform_bus); |
| 217 | } |
| 218 | arch_initcall(acpi_platform_init); |