xref: /openbmc/linux/drivers/acpi/acpi_lpss.c (revision 24b1944f)
1 /*
2  * ACPI support for Intel Lynxpoint LPSS.
3  *
4  * Copyright (C) 2013, Intel Corporation
5  * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
6  *          Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #include <linux/acpi.h>
14 #include <linux/clk.h>
15 #include <linux/clkdev.h>
16 #include <linux/clk-provider.h>
17 #include <linux/err.h>
18 #include <linux/io.h>
19 #include <linux/platform_device.h>
20 #include <linux/platform_data/clk-lpss.h>
21 #include <linux/pm_runtime.h>
22 
23 #include "internal.h"
24 
25 ACPI_MODULE_NAME("acpi_lpss");
26 
27 #define LPSS_CLK_SIZE	0x04
28 #define LPSS_LTR_SIZE	0x18
29 
30 /* Offsets relative to LPSS_PRIVATE_OFFSET */
31 #define LPSS_GENERAL			0x08
32 #define LPSS_GENERAL_LTR_MODE_SW	BIT(2)
33 #define LPSS_SW_LTR			0x10
34 #define LPSS_AUTO_LTR			0x14
35 
36 struct lpss_device_desc {
37 	bool clk_required;
38 	const char *clkdev_name;
39 	bool ltr_required;
40 	unsigned int prv_offset;
41 };
42 
43 static struct lpss_device_desc lpss_dma_desc = {
44 	.clk_required = true,
45 	.clkdev_name = "hclk",
46 };
47 
48 struct lpss_private_data {
49 	void __iomem *mmio_base;
50 	resource_size_t mmio_size;
51 	struct clk *clk;
52 	const struct lpss_device_desc *dev_desc;
53 };
54 
55 static struct lpss_device_desc lpt_dev_desc = {
56 	.clk_required = true,
57 	.prv_offset = 0x800,
58 	.ltr_required = true,
59 };
60 
61 static struct lpss_device_desc lpt_sdio_dev_desc = {
62 	.prv_offset = 0x1000,
63 	.ltr_required = true,
64 };
65 
66 static const struct acpi_device_id acpi_lpss_device_ids[] = {
67 	/* Generic LPSS devices */
68 	{ "INTL9C60", (unsigned long)&lpss_dma_desc },
69 
70 	/* Lynxpoint LPSS devices */
71 	{ "INT33C0", (unsigned long)&lpt_dev_desc },
72 	{ "INT33C1", (unsigned long)&lpt_dev_desc },
73 	{ "INT33C2", (unsigned long)&lpt_dev_desc },
74 	{ "INT33C3", (unsigned long)&lpt_dev_desc },
75 	{ "INT33C4", (unsigned long)&lpt_dev_desc },
76 	{ "INT33C5", (unsigned long)&lpt_dev_desc },
77 	{ "INT33C6", (unsigned long)&lpt_sdio_dev_desc },
78 	{ "INT33C7", },
79 
80 	{ }
81 };
82 
83 static int is_memory(struct acpi_resource *res, void *not_used)
84 {
85 	struct resource r;
86 	return !acpi_dev_resource_memory(res, &r);
87 }
88 
89 /* LPSS main clock device. */
90 static struct platform_device *lpss_clk_dev;
91 
92 static inline void lpt_register_clock_device(void)
93 {
94 	lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
95 }
96 
97 static int register_device_clock(struct acpi_device *adev,
98 				 struct lpss_private_data *pdata)
99 {
100 	const struct lpss_device_desc *dev_desc = pdata->dev_desc;
101 	struct lpss_clk_data *clk_data;
102 
103 	if (!lpss_clk_dev)
104 		lpt_register_clock_device();
105 
106 	clk_data = platform_get_drvdata(lpss_clk_dev);
107 	if (!clk_data)
108 		return -ENODEV;
109 
110 	if (dev_desc->clkdev_name) {
111 		clk_register_clkdev(clk_data->clk, dev_desc->clkdev_name,
112 				    dev_name(&adev->dev));
113 		return 0;
114 	}
115 
116 	if (!pdata->mmio_base
117 	    || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
118 		return -ENODATA;
119 
120 	pdata->clk = clk_register_gate(NULL, dev_name(&adev->dev),
121 				       clk_data->name, 0,
122 				       pdata->mmio_base + dev_desc->prv_offset,
123 				       0, 0, NULL);
124 	if (IS_ERR(pdata->clk))
125 		return PTR_ERR(pdata->clk);
126 
127 	clk_register_clkdev(pdata->clk, NULL, dev_name(&adev->dev));
128 	return 0;
129 }
130 
131 static int acpi_lpss_create_device(struct acpi_device *adev,
132 				   const struct acpi_device_id *id)
133 {
134 	struct lpss_device_desc *dev_desc;
135 	struct lpss_private_data *pdata;
136 	struct resource_list_entry *rentry;
137 	struct list_head resource_list;
138 	int ret;
139 
140 	dev_desc = (struct lpss_device_desc *)id->driver_data;
141 	if (!dev_desc)
142 		return acpi_create_platform_device(adev, id);
143 
144 	pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
145 	if (!pdata)
146 		return -ENOMEM;
147 
148 	INIT_LIST_HEAD(&resource_list);
149 	ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
150 	if (ret < 0)
151 		goto err_out;
152 
153 	list_for_each_entry(rentry, &resource_list, node)
154 		if (resource_type(&rentry->res) == IORESOURCE_MEM) {
155 			pdata->mmio_size = resource_size(&rentry->res);
156 			pdata->mmio_base = ioremap(rentry->res.start,
157 						   pdata->mmio_size);
158 			pdata->dev_desc = dev_desc;
159 			break;
160 		}
161 
162 	acpi_dev_free_resource_list(&resource_list);
163 
164 	if (dev_desc->clk_required) {
165 		ret = register_device_clock(adev, pdata);
166 		if (ret) {
167 			/*
168 			 * Skip the device, but don't terminate the namespace
169 			 * scan.
170 			 */
171 			kfree(pdata);
172 			return 0;
173 		}
174 	}
175 
176 	adev->driver_data = pdata;
177 	ret = acpi_create_platform_device(adev, id);
178 	if (ret > 0)
179 		return ret;
180 
181 	adev->driver_data = NULL;
182 
183  err_out:
184 	kfree(pdata);
185 	return ret;
186 }
187 
188 static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
189 {
190 	struct acpi_device *adev;
191 	struct lpss_private_data *pdata;
192 	unsigned long flags;
193 	int ret;
194 
195 	ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
196 	if (WARN_ON(ret))
197 		return ret;
198 
199 	spin_lock_irqsave(&dev->power.lock, flags);
200 	if (pm_runtime_suspended(dev)) {
201 		ret = -EAGAIN;
202 		goto out;
203 	}
204 	pdata = acpi_driver_data(adev);
205 	if (WARN_ON(!pdata || !pdata->mmio_base)) {
206 		ret = -ENODEV;
207 		goto out;
208 	}
209 	*val = readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
210 
211  out:
212 	spin_unlock_irqrestore(&dev->power.lock, flags);
213 	return ret;
214 }
215 
216 static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
217 			     char *buf)
218 {
219 	u32 ltr_value = 0;
220 	unsigned int reg;
221 	int ret;
222 
223 	reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
224 	ret = lpss_reg_read(dev, reg, &ltr_value);
225 	if (ret)
226 		return ret;
227 
228 	return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
229 }
230 
231 static ssize_t lpss_ltr_mode_show(struct device *dev,
232 				  struct device_attribute *attr, char *buf)
233 {
234 	u32 ltr_mode = 0;
235 	char *outstr;
236 	int ret;
237 
238 	ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
239 	if (ret)
240 		return ret;
241 
242 	outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
243 	return sprintf(buf, "%s\n", outstr);
244 }
245 
246 static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
247 static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
248 static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
249 
250 static struct attribute *lpss_attrs[] = {
251 	&dev_attr_auto_ltr.attr,
252 	&dev_attr_sw_ltr.attr,
253 	&dev_attr_ltr_mode.attr,
254 	NULL,
255 };
256 
257 static struct attribute_group lpss_attr_group = {
258 	.attrs = lpss_attrs,
259 	.name = "lpss_ltr",
260 };
261 
262 static int acpi_lpss_platform_notify(struct notifier_block *nb,
263 				     unsigned long action, void *data)
264 {
265 	struct platform_device *pdev = to_platform_device(data);
266 	struct lpss_private_data *pdata;
267 	struct acpi_device *adev;
268 	const struct acpi_device_id *id;
269 	int ret = 0;
270 
271 	id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
272 	if (!id || !id->driver_data)
273 		return 0;
274 
275 	if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
276 		return 0;
277 
278 	pdata = acpi_driver_data(adev);
279 	if (!pdata || !pdata->mmio_base || !pdata->dev_desc->ltr_required)
280 		return 0;
281 
282 	if (pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
283 		dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
284 		return 0;
285 	}
286 
287 	if (action == BUS_NOTIFY_ADD_DEVICE)
288 		ret = sysfs_create_group(&pdev->dev.kobj, &lpss_attr_group);
289 	else if (action == BUS_NOTIFY_DEL_DEVICE)
290 		sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
291 
292 	return ret;
293 }
294 
295 static struct notifier_block acpi_lpss_nb = {
296 	.notifier_call = acpi_lpss_platform_notify,
297 };
298 
299 static struct acpi_scan_handler lpss_handler = {
300 	.ids = acpi_lpss_device_ids,
301 	.attach = acpi_lpss_create_device,
302 };
303 
304 void __init acpi_lpss_init(void)
305 {
306 	if (!lpt_clk_init()) {
307 		bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
308 		acpi_scan_add_handler(&lpss_handler);
309 	}
310 }
311