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 | |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 22 | /** |
| 23 | * acpi_create_platform_device - Create platform device for ACPI device node |
| 24 | * @adev: ACPI device node to create a platform device for. |
| 25 | * |
| 26 | * Check if the given @adev can be represented as a platform device and, if |
| 27 | * that's the case, create and register a platform device, populate its common |
| 28 | * resources and returns a pointer to it. Otherwise, return %NULL. |
| 29 | * |
| 30 | * The platform device's name will be taken from the @adev's _HID and _UID. |
| 31 | */ |
| 32 | struct platform_device *acpi_create_platform_device(struct acpi_device *adev) |
| 33 | { |
| 34 | struct platform_device *pdev = NULL; |
| 35 | struct acpi_device *acpi_parent; |
| 36 | struct device *parent = NULL; |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame^] | 37 | struct resource_list_entry *rentry; |
| 38 | struct list_head resource_list; |
| 39 | struct resource *resources; |
| 40 | int count; |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 41 | |
| 42 | /* If the ACPI node already has a physical device attached, skip it. */ |
| 43 | if (adev->physical_node_count) |
| 44 | return NULL; |
| 45 | |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame^] | 46 | INIT_LIST_HEAD(&resource_list); |
| 47 | count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL); |
| 48 | if (count <= 0) |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 49 | return NULL; |
| 50 | |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame^] | 51 | resources = kmalloc(count * sizeof(struct resource), GFP_KERNEL); |
| 52 | if (!resources) { |
| 53 | dev_err(&adev->dev, "No memory for resources\n"); |
| 54 | acpi_dev_free_resource_list(&resource_list); |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame^] | 57 | count = 0; |
| 58 | list_for_each_entry(rentry, &resource_list, node) |
| 59 | resources[count++] = rentry->res; |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 60 | |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame^] | 61 | acpi_dev_free_resource_list(&resource_list); |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 62 | |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 63 | /* |
| 64 | * If the ACPI node has a parent and that parent has a physical device |
| 65 | * attached to it, that physical device should be the parent of the |
| 66 | * platform device we are about to create. |
| 67 | */ |
| 68 | acpi_parent = adev->parent; |
| 69 | if (acpi_parent) { |
| 70 | struct acpi_device_physical_node *entry; |
| 71 | struct list_head *list; |
| 72 | |
| 73 | mutex_lock(&acpi_parent->physical_node_lock); |
| 74 | list = &acpi_parent->physical_node_list; |
| 75 | if (!list_empty(list)) { |
| 76 | entry = list_first_entry(list, |
| 77 | struct acpi_device_physical_node, |
| 78 | node); |
| 79 | parent = entry->dev; |
| 80 | } |
| 81 | mutex_unlock(&acpi_parent->physical_node_lock); |
| 82 | } |
Mika Westerberg | b4b6cae | 2012-11-10 23:16:02 +0100 | [diff] [blame] | 83 | pdev = platform_device_register_resndata(parent, dev_name(&adev->dev), |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame^] | 84 | -1, resources, count, NULL, 0); |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 85 | if (IS_ERR(pdev)) { |
| 86 | dev_err(&adev->dev, "platform device creation failed: %ld\n", |
| 87 | PTR_ERR(pdev)); |
| 88 | pdev = NULL; |
| 89 | } else { |
| 90 | dev_dbg(&adev->dev, "created platform device %s\n", |
| 91 | dev_name(&pdev->dev)); |
| 92 | } |
| 93 | |
Rafael J. Wysocki | 8e345c9 | 2012-11-15 00:30:21 +0100 | [diff] [blame^] | 94 | kfree(resources); |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 95 | return pdev; |
| 96 | } |
| 97 | |
| 98 | static acpi_status acpi_platform_match(acpi_handle handle, u32 depth, |
| 99 | void *data, void **return_value) |
| 100 | { |
| 101 | struct platform_device *pdev = data; |
| 102 | struct acpi_device *adev; |
| 103 | acpi_status status; |
| 104 | |
| 105 | status = acpi_bus_get_device(handle, &adev); |
| 106 | if (ACPI_FAILURE(status)) |
| 107 | return status; |
| 108 | |
| 109 | /* Skip ACPI devices that have physical device attached */ |
| 110 | if (adev->physical_node_count) |
| 111 | return AE_OK; |
| 112 | |
Mika Westerberg | b4b6cae | 2012-11-10 23:16:02 +0100 | [diff] [blame] | 113 | if (!strcmp(dev_name(&pdev->dev), dev_name(&adev->dev))) { |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 114 | *(acpi_handle *)return_value = handle; |
| 115 | return AE_CTRL_TERMINATE; |
| 116 | } |
| 117 | |
| 118 | return AE_OK; |
| 119 | } |
| 120 | |
| 121 | static int acpi_platform_find_device(struct device *dev, acpi_handle *handle) |
| 122 | { |
| 123 | struct platform_device *pdev = to_platform_device(dev); |
Mika Westerberg | b4b6cae | 2012-11-10 23:16:02 +0100 | [diff] [blame] | 124 | char *name, *tmp, *hid; |
| 125 | |
| 126 | /* |
| 127 | * The platform device is named using the ACPI device name |
| 128 | * _HID:INSTANCE so we strip the INSTANCE out in order to find the |
| 129 | * correct device using its _HID. |
| 130 | */ |
| 131 | name = kstrdup(dev_name(dev), GFP_KERNEL); |
| 132 | if (!name) |
| 133 | return -ENOMEM; |
| 134 | |
| 135 | tmp = name; |
| 136 | hid = strsep(&tmp, ":"); |
| 137 | if (!hid) { |
| 138 | kfree(name); |
| 139 | return -ENODEV; |
| 140 | } |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 141 | |
| 142 | *handle = NULL; |
Mika Westerberg | b4b6cae | 2012-11-10 23:16:02 +0100 | [diff] [blame] | 143 | acpi_get_devices(hid, acpi_platform_match, pdev, handle); |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 144 | |
Mika Westerberg | b4b6cae | 2012-11-10 23:16:02 +0100 | [diff] [blame] | 145 | kfree(name); |
Mika Westerberg | 91e5687 | 2012-10-31 22:45:02 +0100 | [diff] [blame] | 146 | return *handle ? 0 : -ENODEV; |
| 147 | } |
| 148 | |
| 149 | static struct acpi_bus_type acpi_platform_bus = { |
| 150 | .bus = &platform_bus_type, |
| 151 | .find_device = acpi_platform_find_device, |
| 152 | }; |
| 153 | |
| 154 | static int __init acpi_platform_init(void) |
| 155 | { |
| 156 | return register_acpi_bus_type(&acpi_platform_bus); |
| 157 | } |
| 158 | arch_initcall(acpi_platform_init); |