blob: 7ac20d8b8f0733aa3d0950f0e925395a0a718a0a [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
Mika Westerberg91e56872012-10-31 22:45:02 +010022/**
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 */
32struct 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. Wysocki8e345c92012-11-15 00:30:21 +010037 struct resource_list_entry *rentry;
38 struct list_head resource_list;
39 struct resource *resources;
40 int count;
Mika Westerberg91e56872012-10-31 22:45:02 +010041
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. Wysocki8e345c92012-11-15 00:30:21 +010046 INIT_LIST_HEAD(&resource_list);
47 count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
48 if (count <= 0)
Mika Westerberg91e56872012-10-31 22:45:02 +010049 return NULL;
50
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +010051 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 Westerberg91e56872012-10-31 22:45:02 +010055 return NULL;
56 }
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +010057 count = 0;
58 list_for_each_entry(rentry, &resource_list, node)
59 resources[count++] = rentry->res;
Mika Westerberg91e56872012-10-31 22:45:02 +010060
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +010061 acpi_dev_free_resource_list(&resource_list);
Mika Westerberg91e56872012-10-31 22:45:02 +010062
Mika Westerberg91e56872012-10-31 22:45:02 +010063 /*
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 Westerbergb4b6cae2012-11-10 23:16:02 +010083 pdev = platform_device_register_resndata(parent, dev_name(&adev->dev),
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +010084 -1, resources, count, NULL, 0);
Mika Westerberg91e56872012-10-31 22:45:02 +010085 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. Wysocki8e345c92012-11-15 00:30:21 +010094 kfree(resources);
Mika Westerberg91e56872012-10-31 22:45:02 +010095 return pdev;
96}
97
98static 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 Westerbergb4b6cae2012-11-10 23:16:02 +0100113 if (!strcmp(dev_name(&pdev->dev), dev_name(&adev->dev))) {
Mika Westerberg91e56872012-10-31 22:45:02 +0100114 *(acpi_handle *)return_value = handle;
115 return AE_CTRL_TERMINATE;
116 }
117
118 return AE_OK;
119}
120
121static int acpi_platform_find_device(struct device *dev, acpi_handle *handle)
122{
123 struct platform_device *pdev = to_platform_device(dev);
Mika Westerbergb4b6cae2012-11-10 23:16:02 +0100124 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 Westerberg91e56872012-10-31 22:45:02 +0100141
142 *handle = NULL;
Mika Westerbergb4b6cae2012-11-10 23:16:02 +0100143 acpi_get_devices(hid, acpi_platform_match, pdev, handle);
Mika Westerberg91e56872012-10-31 22:45:02 +0100144
Mika Westerbergb4b6cae2012-11-10 23:16:02 +0100145 kfree(name);
Mika Westerberg91e56872012-10-31 22:45:02 +0100146 return *handle ? 0 : -ENODEV;
147}
148
149static struct acpi_bus_type acpi_platform_bus = {
150 .bus = &platform_bus_type,
151 .find_device = acpi_platform_find_device,
152};
153
154static int __init acpi_platform_init(void)
155{
156 return register_acpi_bus_type(&acpi_platform_bus);
157}
158arch_initcall(acpi_platform_init);