platform / ACPI: Attach/detach ACPI PM during probe/remove/shutdown
[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;
863f9f30 36 struct platform_device_info pdevinfo;
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
863f9f30 63 memset(&pdevinfo, 0, sizeof(pdevinfo));
91e56878
MW
64 /*
65 * If the ACPI node has a parent and that parent has a physical device
66 * attached to it, that physical device should be the parent of the
67 * platform device we are about to create.
68 */
863f9f30 69 pdevinfo.parent = NULL;
91e56878
MW
70 acpi_parent = adev->parent;
71 if (acpi_parent) {
72 struct acpi_device_physical_node *entry;
73 struct list_head *list;
74
75 mutex_lock(&acpi_parent->physical_node_lock);
76 list = &acpi_parent->physical_node_list;
77 if (!list_empty(list)) {
78 entry = list_first_entry(list,
79 struct acpi_device_physical_node,
80 node);
863f9f30 81 pdevinfo.parent = entry->dev;
91e56878
MW
82 }
83 mutex_unlock(&acpi_parent->physical_node_lock);
84 }
863f9f30
RW
85 pdevinfo.name = dev_name(&adev->dev);
86 pdevinfo.id = -1;
87 pdevinfo.res = resources;
88 pdevinfo.num_res = count;
89 pdevinfo.acpi_node.handle = adev->handle;
90 pdev = platform_device_register_full(&pdevinfo);
91e56878
MW
91 if (IS_ERR(pdev)) {
92 dev_err(&adev->dev, "platform device creation failed: %ld\n",
93 PTR_ERR(pdev));
94 pdev = NULL;
95 } else {
96 dev_dbg(&adev->dev, "created platform device %s\n",
97 dev_name(&pdev->dev));
98 }
99
8e345c99 100 kfree(resources);
91e56878
MW
101 return pdev;
102}
This page took 0.03011 seconds and 5 git commands to generate.