ACPI / driver core: Introduce struct acpi_dev_node and related macros
[deliverable/linux.git] / drivers / acpi / acpi_platform.c
CommitLineData
91e56878
MW
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
20ACPI_MODULE_NAME("platform");
21
91e56878
MW
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 */
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;
8e345c99
RW
37 struct resource_list_entry *rentry;
38 struct list_head resource_list;
39 struct resource *resources;
40 int count;
91e56878
MW
41
42 /* If the ACPI node already has a physical device attached, skip it. */
43 if (adev->physical_node_count)
44 return NULL;
45
8e345c99
RW
46 INIT_LIST_HEAD(&resource_list);
47 count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
48 if (count <= 0)
91e56878
MW
49 return NULL;
50
8e345c99
RW
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);
91e56878
MW
55 return NULL;
56 }
8e345c99
RW
57 count = 0;
58 list_for_each_entry(rentry, &resource_list, node)
59 resources[count++] = rentry->res;
91e56878 60
8e345c99 61 acpi_dev_free_resource_list(&resource_list);
91e56878 62
91e56878
MW
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 }
b4b6cae2 83 pdev = platform_device_register_resndata(parent, dev_name(&adev->dev),
8e345c99 84 -1, resources, count, NULL, 0);
91e56878
MW
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
8e345c99 94 kfree(resources);
91e56878
MW
95 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
b4b6cae2 113 if (!strcmp(dev_name(&pdev->dev), dev_name(&adev->dev))) {
91e56878
MW
114 *(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);
b4b6cae2
MW
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 }
91e56878
MW
141
142 *handle = NULL;
b4b6cae2 143 acpi_get_devices(hid, acpi_platform_match, pdev, handle);
91e56878 144
b4b6cae2 145 kfree(name);
91e56878
MW
146 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);
This page took 0.036692 seconds and 5 git commands to generate.