Merge branch 'acpi-platform'
[pandora-kernel.git] / drivers / acpi / acpi_platform.c
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/err.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20
21 #include "internal.h"
22
23 ACPI_MODULE_NAME("platform");
24
25 /*
26  * The following ACPI IDs are known to be suitable for representing as
27  * platform devices.
28  */
29 static const struct acpi_device_id acpi_platform_device_ids[] = {
30
31         { "PNP0D40" },
32         { "VPC2004" },
33         { "BCM4752" },
34         { "LNV4752" },
35         { "BCM2E1A" },
36         { "BCM2E39" },
37         { "BCM2E3D" },
38
39         /* Intel Smart Sound Technology */
40         { "INT33C8" },
41         { "80860F28" },
42
43         { }
44 };
45
46 /**
47  * acpi_create_platform_device - Create platform device for ACPI device node
48  * @adev: ACPI device node to create a platform device for.
49  *
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.
53  *
54  * Name of the platform device will be the same as @adev's.
55  */
56 struct platform_device *acpi_create_platform_device(struct acpi_device *adev)
57 {
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;
64         int count;
65
66         /* If the ACPI node already has a physical device attached, skip it. */
67         if (adev->physical_node_count)
68                 return NULL;
69
70         INIT_LIST_HEAD(&resource_list);
71         count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
72         if (count < 0) {
73                 return NULL;
74         } else if (count > 0) {
75                 resources = kmalloc(count * sizeof(struct resource),
76                                     GFP_KERNEL);
77                 if (!resources) {
78                         dev_err(&adev->dev, "No memory for resources\n");
79                         acpi_dev_free_resource_list(&resource_list);
80                         return ERR_PTR(-ENOMEM);
81                 }
82                 count = 0;
83                 list_for_each_entry(rentry, &resource_list, node)
84                         resources[count++] = rentry->res;
85
86                 acpi_dev_free_resource_list(&resource_list);
87         }
88
89         memset(&pdevinfo, 0, sizeof(pdevinfo));
90         /*
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.
94          */
95         pdevinfo.parent = NULL;
96         acpi_parent = adev->parent;
97         if (acpi_parent) {
98                 struct acpi_device_physical_node *entry;
99                 struct list_head *list;
100
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,
106                                         node);
107                         pdevinfo.parent = entry->dev;
108                 }
109                 mutex_unlock(&acpi_parent->physical_node_lock);
110         }
111         pdevinfo.name = dev_name(&adev->dev);
112         pdevinfo.id = -1;
113         pdevinfo.res = resources;
114         pdevinfo.num_res = count;
115         pdevinfo.acpi_node.companion = adev;
116         pdev = platform_device_register_full(&pdevinfo);
117         if (IS_ERR(pdev))
118                 dev_err(&adev->dev, "platform device creation failed: %ld\n",
119                         PTR_ERR(pdev));
120         else
121                 dev_dbg(&adev->dev, "created platform device %s\n",
122                         dev_name(&pdev->dev));
123
124         kfree(resources);
125         return pdev;
126 }
127
128 static int acpi_platform_attach(struct acpi_device *adev,
129                                 const struct acpi_device_id *id)
130 {
131         acpi_create_platform_device(adev);
132         return 1;
133 }
134
135 static struct acpi_scan_handler platform_handler = {
136         .ids = acpi_platform_device_ids,
137         .attach = acpi_platform_attach,
138 };
139
140 void __init acpi_platform_init(void)
141 {
142         acpi_scan_add_handler(&platform_handler);
143 }