aboutsummaryrefslogtreecommitdiff
path: root/drivers/amba/acpi.c
blob: ae180757f3353e635774e9b3eba23675ce2034be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * AMBA Connector Resource for ACPI
 *
 * Copyright (C) 2013 Advanced Micro Devices, Inc.
 *
 * Author: Brandon Anderson <brandon.anderson@amd.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#ifdef CONFIG_ACPI

#include <linux/module.h>
#include <linux/amba/bus.h>
#include <linux/platform_device.h>
#include <linux/acpi.h>
#include <linux/clkdev.h>
#include <linux/amba/acpi.h>

struct acpi_amba_bus_info {
	struct platform_device *pdev;
	struct clk *clk;
	char *clk_name;
};

static int acpi_amba_add_resource(struct acpi_resource *ares, void *data)
{
	struct amba_device *dev = data;
	struct resource r;
	int irq_idx;

	switch (ares->type) {
	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
		if (!acpi_dev_resource_memory(ares, &dev->res))
			pr_err("failed to map memory resource\n");
		break;
	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
		for (irq_idx = 0; irq_idx < AMBA_NR_IRQS; irq_idx++) {
			if (acpi_dev_resource_interrupt(ares, irq_idx, &r))
				dev->irq[irq_idx] = r.start;
			else
				break;
		}

		break;
	case ACPI_RESOURCE_TYPE_END_TAG:
		/* ignore the end tag */
		break;
	default:
		/* log an error, but proceed with driver probe */
		pr_err("unhandled acpi resource type= %d\n",
				ares->type);
		break;
	}

	return 1; /* Tell ACPI core that this resource has been handled */
}

static struct clk *acpi_amba_get_clk(acpi_handle handle, int index,
		char **clk_name)
{
	acpi_handle clk_handle;
	struct acpi_device *adev, *clk_adev;
	const char **clk_paths;
	struct clk *clk = NULL;
	int ret;

	if (acpi_bus_get_device(handle, &adev)) {
		pr_err("cannot get device from handle\n");
		return NULL;
	}

	/* Get number of entries */
	ret = acpi_dev_get_property_array_string(adev, "clocks", NULL, 0);

	if ((ret < 0) || (index >= ret))
		return NULL;

	clk_paths = kzalloc(sizeof(*clk_paths) * ret, GFP_KERNEL);

	/* look under the clock device for the clock that matches the entry */
	ret = acpi_dev_get_property_array_string(adev, "clocks", clk_paths,
			ret);

	/* Locate the acpi_device from the device name */
	acpi_get_handle(NULL, (acpi_string)clk_paths[index], &clk_handle);
	if (!clk_handle)
		goto error;
	acpi_bus_get_device(clk_handle, &clk_adev);
	if (!clk_adev)
		goto error;

	clk = clk_get_sys(dev_name(&clk_adev->dev), NULL);

error:
	kfree(clk_paths);
	return clk;
}

static void acpi_amba_register_clocks(struct acpi_device *adev,
		struct clk *default_clk, const char *default_clk_name)
{
	struct clk *clk;
	int i;
	char *clk_name;

	for (i = 0;; i++) {
		clk_name = NULL;
		clk = acpi_amba_get_clk(ACPI_HANDLE(&adev->dev), i, &clk_name);
		if (!clk)
			break;

		clk_register_clkdev(clk, clk_name, dev_name(&adev->dev));

		kfree(clk_name);
	}

	if (default_clk) {
		/* for amba_get_enable_pclk() ... */
		clk_register_clkdev(default_clk, default_clk_name,
				dev_name(&adev->dev));
		/* for devm_clk_get() ... */
		clk_register_clkdev(default_clk, NULL, dev_name(&adev->dev));
	}
}

/* acpi_amba_add_device()
 *
 * ACPI equivalent to of_amba_device_create()
 */
static acpi_status acpi_amba_add_device(acpi_handle handle,
				u32 lvl_not_used, void *data, void **not_used)
{
	struct list_head resource_list;
	struct acpi_device *adev;
	struct amba_device *dev;
	int ret;
	struct acpi_amba_bus_info *bus_info = data;
	struct platform_device *bus_pdev = bus_info->pdev;

	if (acpi_bus_get_device(handle, &adev)) {
		pr_err("%s: acpi_bus_get_device failed\n", __func__);
		return AE_OK;
	}

	pr_debug("Creating amba device %s\n", dev_name(&adev->dev));

	dev = amba_device_alloc(NULL, 0, 0);
	if (!dev) {
		pr_err("%s(): amba_device_alloc() failed for %s\n",
		       __func__, dev_name(&adev->dev));
		return AE_CTRL_TERMINATE;
	}

	/* setup generic device info */
	dev->dev.coherent_dma_mask = ~0;
	dev->dev.parent = &bus_pdev->dev;
	dev_set_name(&dev->dev, "%s", dev_name(&adev->dev));

	/* setup amba-specific device info */
	ACPI_COMPANION_SET(&dev->dev, adev);
	ACPI_COMPANION_SET(&adev->dev, adev);

	INIT_LIST_HEAD(&resource_list);
	acpi_dev_get_resources(adev, &resource_list,
				     acpi_amba_add_resource, dev);
	acpi_dev_free_resource_list(&resource_list);

	/* Add clocks */
	acpi_amba_register_clocks(adev, bus_info->clk, bus_info->clk_name);

	/* Read AMBA hardware ID and add device to system. If a driver matching
	 *	hardware ID has already been registered, bind this device to it.
	 *	Otherwise, the platform subsystem will match up the hardware ID
	 *	when the matching driver is registered.
	*/
	ret = amba_device_add(dev, &iomem_resource);
	if (ret) {
		pr_err("%s(): amba_device_add() failed (%d) for %s\n",
		       __func__, ret, dev_name(&adev->dev));
		goto err_free;
	}

	return AE_OK;

err_free:
	amba_device_put(dev);

	return AE_OK; /* don't prevent other devices from being probed */
}

static int acpi_amba_bus_probe(struct platform_device *pdev)
{
	struct acpi_amba_bus_info bus_info;
	bus_info.clk_name = NULL;

	/* see if there's a top-level clock to use as default for sub-devices */
	bus_info.clk = acpi_amba_get_clk(ACPI_HANDLE(&pdev->dev), 0,
			&bus_info.clk_name);

	/* probe each device */
	bus_info.pdev = pdev;
	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_HANDLE(&pdev->dev), 1,
			acpi_amba_add_device, NULL, &bus_info, NULL);

	kfree(bus_info.clk_name);

	return 0;
}

static const struct acpi_device_id amba_bus_acpi_match[] = {
	{ "LNRO001A", 0 },
	{ },
};

static struct platform_driver amba_bus_acpi_driver = {
	.driver = {
		.name = "amba-acpi",
		.owner = THIS_MODULE,
		.acpi_match_table = ACPI_PTR(amba_bus_acpi_match),
	},
	.probe	= acpi_amba_bus_probe,
};

static int __init acpi_amba_bus_init(void)
{
	return platform_driver_register(&amba_bus_acpi_driver);
}

postcore_initcall(acpi_amba_bus_init);

MODULE_AUTHOR("Brandon Anderson <brandon.anderson@amd.com>");
MODULE_DESCRIPTION("ACPI Connector Resource for AMBA bus");
MODULE_LICENSE("GPLv2");
MODULE_ALIAS("platform:amba-acpi");

#endif /* CONFIG_ACPI */