blob: 26fce4b8a632a3b8537f4baa16ae36f84d6530d1 [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>
Mika Westerberge3753252013-01-18 13:46:01 +000016#include <linux/err.h>
Mika Westerberg91e56872012-10-31 22:45:02 +010017#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20
Andy Shevchenko5923f982012-11-26 10:35:07 +010021#include "internal.h"
22
Mika Westerberg91e56872012-10-31 22:45:02 +010023ACPI_MODULE_NAME("platform");
24
Rafael J. Wysocki141a2972013-01-30 14:27:40 +010025/* Flags for acpi_create_platform_device */
26#define ACPI_PLATFORM_CLK BIT(0)
27
28/*
29 * The following ACPI IDs are known to be suitable for representing as
30 * platform devices.
31 */
32static const struct acpi_device_id acpi_platform_device_ids[] = {
33
34 { "PNP0D40" },
35
36 /* Haswell LPSS devices */
37 { "INT33C0", ACPI_PLATFORM_CLK },
38 { "INT33C1", ACPI_PLATFORM_CLK },
39 { "INT33C2", ACPI_PLATFORM_CLK },
40 { "INT33C3", ACPI_PLATFORM_CLK },
41 { "INT33C4", ACPI_PLATFORM_CLK },
42 { "INT33C5", ACPI_PLATFORM_CLK },
43 { "INT33C6", ACPI_PLATFORM_CLK },
44 { "INT33C7", ACPI_PLATFORM_CLK },
45
46 { }
47};
48
Mika Westerberge3753252013-01-18 13:46:01 +000049static int acpi_create_platform_clks(struct acpi_device *adev)
50{
51 static struct platform_device *pdev;
52
53 /* Create Lynxpoint LPSS clocks */
54 if (!pdev && !strncmp(acpi_device_hid(adev), "INT33C", 6)) {
55 pdev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
56 if (IS_ERR(pdev))
57 return PTR_ERR(pdev);
58 }
59
60 return 0;
61}
62
Mika Westerberg91e56872012-10-31 22:45:02 +010063/**
64 * acpi_create_platform_device - Create platform device for ACPI device node
65 * @adev: ACPI device node to create a platform device for.
Rafael J. Wysocki141a2972013-01-30 14:27:40 +010066 * @id: ACPI device ID used to match @adev.
Mika Westerberg91e56872012-10-31 22:45:02 +010067 *
68 * Check if the given @adev can be represented as a platform device and, if
69 * that's the case, create and register a platform device, populate its common
70 * resources and returns a pointer to it. Otherwise, return %NULL.
71 *
Mika Westerberg7eaa2802013-01-18 14:09:35 +000072 * Name of the platform device will be the same as @adev's.
Mika Westerberg91e56872012-10-31 22:45:02 +010073 */
Rafael J. Wysocki141a2972013-01-30 14:27:40 +010074static int acpi_create_platform_device(struct acpi_device *adev,
75 const struct acpi_device_id *id)
Mika Westerberg91e56872012-10-31 22:45:02 +010076{
Rafael J. Wysocki141a2972013-01-30 14:27:40 +010077 unsigned long flags = id->driver_data;
Mika Westerberg91e56872012-10-31 22:45:02 +010078 struct platform_device *pdev = NULL;
79 struct acpi_device *acpi_parent;
Rafael J. Wysocki863f9f32012-11-21 00:21:59 +010080 struct platform_device_info pdevinfo;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +010081 struct resource_list_entry *rentry;
82 struct list_head resource_list;
83 struct resource *resources;
84 int count;
Mika Westerberg91e56872012-10-31 22:45:02 +010085
Rafael J. Wysocki141a2972013-01-30 14:27:40 +010086 if (flags & ACPI_PLATFORM_CLK) {
87 int ret = acpi_create_platform_clks(adev);
88 if (ret) {
89 dev_err(&adev->dev, "failed to create clocks\n");
90 return ret;
91 }
Mika Westerberge3753252013-01-18 13:46:01 +000092 }
93
Mika Westerberg91e56872012-10-31 22:45:02 +010094 /* If the ACPI node already has a physical device attached, skip it. */
95 if (adev->physical_node_count)
Rafael J. Wysocki141a2972013-01-30 14:27:40 +010096 return 0;
Mika Westerberg91e56872012-10-31 22:45:02 +010097
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +010098 INIT_LIST_HEAD(&resource_list);
99 count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
100 if (count <= 0)
Rafael J. Wysocki141a2972013-01-30 14:27:40 +0100101 return 0;
Mika Westerberg91e56872012-10-31 22:45:02 +0100102
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100103 resources = kmalloc(count * sizeof(struct resource), GFP_KERNEL);
104 if (!resources) {
105 dev_err(&adev->dev, "No memory for resources\n");
106 acpi_dev_free_resource_list(&resource_list);
Rafael J. Wysocki141a2972013-01-30 14:27:40 +0100107 return -ENOMEM;
Mika Westerberg91e56872012-10-31 22:45:02 +0100108 }
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100109 count = 0;
110 list_for_each_entry(rentry, &resource_list, node)
111 resources[count++] = rentry->res;
Mika Westerberg91e56872012-10-31 22:45:02 +0100112
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100113 acpi_dev_free_resource_list(&resource_list);
Mika Westerberg91e56872012-10-31 22:45:02 +0100114
Rafael J. Wysocki863f9f32012-11-21 00:21:59 +0100115 memset(&pdevinfo, 0, sizeof(pdevinfo));
Mika Westerberg91e56872012-10-31 22:45:02 +0100116 /*
117 * If the ACPI node has a parent and that parent has a physical device
118 * attached to it, that physical device should be the parent of the
119 * platform device we are about to create.
120 */
Rafael J. Wysocki863f9f32012-11-21 00:21:59 +0100121 pdevinfo.parent = NULL;
Mika Westerberg91e56872012-10-31 22:45:02 +0100122 acpi_parent = adev->parent;
123 if (acpi_parent) {
124 struct acpi_device_physical_node *entry;
125 struct list_head *list;
126
127 mutex_lock(&acpi_parent->physical_node_lock);
128 list = &acpi_parent->physical_node_list;
129 if (!list_empty(list)) {
130 entry = list_first_entry(list,
131 struct acpi_device_physical_node,
132 node);
Rafael J. Wysocki863f9f32012-11-21 00:21:59 +0100133 pdevinfo.parent = entry->dev;
Mika Westerberg91e56872012-10-31 22:45:02 +0100134 }
135 mutex_unlock(&acpi_parent->physical_node_lock);
136 }
Rafael J. Wysocki863f9f32012-11-21 00:21:59 +0100137 pdevinfo.name = dev_name(&adev->dev);
138 pdevinfo.id = -1;
139 pdevinfo.res = resources;
140 pdevinfo.num_res = count;
141 pdevinfo.acpi_node.handle = adev->handle;
142 pdev = platform_device_register_full(&pdevinfo);
Mika Westerberg91e56872012-10-31 22:45:02 +0100143 if (IS_ERR(pdev)) {
144 dev_err(&adev->dev, "platform device creation failed: %ld\n",
145 PTR_ERR(pdev));
146 pdev = NULL;
147 } else {
148 dev_dbg(&adev->dev, "created platform device %s\n",
149 dev_name(&pdev->dev));
150 }
151
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100152 kfree(resources);
Rafael J. Wysocki141a2972013-01-30 14:27:40 +0100153 return 1;
154}
155
156static struct acpi_scan_handler platform_handler = {
157 .ids = acpi_platform_device_ids,
158 .attach = acpi_create_platform_device,
159};
160
161void __init acpi_platform_init(void)
162{
163 acpi_scan_add_handler(&platform_handler);
Mika Westerberg91e56872012-10-31 22:45:02 +0100164}