2 * ACPI support for platform bus type.
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>
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.
14 #include <linux/acpi.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
23 ACPI_MODULE_NAME("platform");
26 * The following ACPI IDs are known to be suitable for representing as
29 static const struct acpi_device_id acpi_platform_device_ids[] = {
39 /* Intel Smart Sound Technology */
47 * acpi_create_platform_device - Create platform device for ACPI device node
48 * @adev: ACPI device node to create a platform device for.
50 * Check if the given @adev can be represented as a platform device and, if
51 * that's the case, create and register a platform device, populate its common
52 * resources and returns a pointer to it. Otherwise, return %NULL.
54 * Name of the platform device will be the same as @adev's.
56 struct platform_device *acpi_create_platform_device(struct acpi_device *adev)
58 struct platform_device *pdev = NULL;
59 struct acpi_device *acpi_parent;
60 struct platform_device_info pdevinfo;
61 struct resource_list_entry *rentry;
62 struct list_head resource_list;
63 struct resource *resources = NULL;
66 /* If the ACPI node already has a physical device attached, skip it. */
67 if (adev->physical_node_count)
70 INIT_LIST_HEAD(&resource_list);
71 count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
74 } else if (count > 0) {
75 resources = kmalloc(count * sizeof(struct resource),
78 dev_err(&adev->dev, "No memory for resources\n");
79 acpi_dev_free_resource_list(&resource_list);
80 return ERR_PTR(-ENOMEM);
83 list_for_each_entry(rentry, &resource_list, node)
84 resources[count++] = rentry->res;
86 acpi_dev_free_resource_list(&resource_list);
89 memset(&pdevinfo, 0, sizeof(pdevinfo));
91 * If the ACPI node has a parent and that parent has a physical device
92 * attached to it, that physical device should be the parent of the
93 * platform device we are about to create.
95 pdevinfo.parent = NULL;
96 acpi_parent = adev->parent;
98 struct acpi_device_physical_node *entry;
99 struct list_head *list;
101 mutex_lock(&acpi_parent->physical_node_lock);
102 list = &acpi_parent->physical_node_list;
103 if (!list_empty(list)) {
104 entry = list_first_entry(list,
105 struct acpi_device_physical_node,
107 pdevinfo.parent = entry->dev;
109 mutex_unlock(&acpi_parent->physical_node_lock);
111 pdevinfo.name = dev_name(&adev->dev);
113 pdevinfo.res = resources;
114 pdevinfo.num_res = count;
115 pdevinfo.acpi_node.companion = adev;
116 pdev = platform_device_register_full(&pdevinfo);
118 dev_err(&adev->dev, "platform device creation failed: %ld\n",
121 dev_dbg(&adev->dev, "created platform device %s\n",
122 dev_name(&pdev->dev));
128 static int acpi_platform_attach(struct acpi_device *adev,
129 const struct acpi_device_id *id)
131 acpi_create_platform_device(adev);
135 static struct acpi_scan_handler platform_handler = {
136 .ids = acpi_platform_device_ids,
137 .attach = acpi_platform_attach,
140 void __init acpi_platform_init(void)
142 acpi_scan_add_handler(&platform_handler);