blob: bd9867f592b7fcda22723cef8e561f04c773aa1d [file] [log] [blame]
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01001/*
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>
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010021#include <linux/pm_runtime.h>
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010022
23#include "internal.h"
24
25ACPI_MODULE_NAME("acpi_lpss");
26
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010027#define LPSS_CLK_SIZE 0x04
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010028#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
Heikki Krogerus06d86412013-06-17 13:25:46 +030035#define LPSS_TX_INT 0x20
36#define LPSS_TX_INT_MASK BIT(1)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010037
Mika Westerbergf6272172013-05-13 12:42:44 +000038struct lpss_shared_clock {
39 const char *name;
40 unsigned long rate;
41 struct clk *clk;
42};
43
Heikki Krogerus06d86412013-06-17 13:25:46 +030044struct lpss_private_data;
45
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010046struct lpss_device_desc {
47 bool clk_required;
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030048 const char *clkdev_name;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010049 bool ltr_required;
50 unsigned int prv_offset;
Mika Westerbergf6272172013-05-13 12:42:44 +000051 bool clk_gate;
52 struct lpss_shared_clock *shared_clock;
Heikki Krogerus06d86412013-06-17 13:25:46 +030053 void (*setup)(struct lpss_private_data *pdata);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010054};
55
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030056static struct lpss_device_desc lpss_dma_desc = {
57 .clk_required = true,
58 .clkdev_name = "hclk",
59};
60
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010061struct lpss_private_data {
62 void __iomem *mmio_base;
63 resource_size_t mmio_size;
64 struct clk *clk;
65 const struct lpss_device_desc *dev_desc;
66};
67
Heikki Krogerus06d86412013-06-17 13:25:46 +030068static void lpss_uart_setup(struct lpss_private_data *pdata)
69{
70 unsigned int tx_int_offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
71 u32 reg;
72
73 reg = readl(pdata->mmio_base + tx_int_offset);
74 writel(reg | LPSS_TX_INT_MASK, pdata->mmio_base + tx_int_offset);
75}
76
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010077static struct lpss_device_desc lpt_dev_desc = {
78 .clk_required = true,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010079 .prv_offset = 0x800,
80 .ltr_required = true,
Mika Westerbergf6272172013-05-13 12:42:44 +000081 .clk_gate = true,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010082};
83
Heikki Krogerus06d86412013-06-17 13:25:46 +030084static struct lpss_device_desc lpt_uart_dev_desc = {
85 .clk_required = true,
86 .prv_offset = 0x800,
87 .ltr_required = true,
88 .clk_gate = true,
89 .setup = lpss_uart_setup,
90};
91
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010092static struct lpss_device_desc lpt_sdio_dev_desc = {
93 .prv_offset = 0x1000,
94 .ltr_required = true,
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010095};
96
Mika Westerbergf6272172013-05-13 12:42:44 +000097static struct lpss_shared_clock uart_clock = {
98 .name = "uart_clk",
99 .rate = 44236800,
100};
101
102static struct lpss_device_desc byt_uart_dev_desc = {
103 .clk_required = true,
104 .prv_offset = 0x800,
105 .clk_gate = true,
106 .shared_clock = &uart_clock,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300107 .setup = lpss_uart_setup,
Mika Westerbergf6272172013-05-13 12:42:44 +0000108};
109
110static struct lpss_shared_clock spi_clock = {
111 .name = "spi_clk",
112 .rate = 50000000,
113};
114
115static struct lpss_device_desc byt_spi_dev_desc = {
116 .clk_required = true,
117 .prv_offset = 0x400,
118 .clk_gate = true,
119 .shared_clock = &spi_clock,
120};
121
122static struct lpss_device_desc byt_sdio_dev_desc = {
123 .clk_required = true,
124};
125
126static struct lpss_shared_clock i2c_clock = {
127 .name = "i2c_clk",
128 .rate = 100000000,
129};
130
131static struct lpss_device_desc byt_i2c_dev_desc = {
132 .clk_required = true,
133 .prv_offset = 0x800,
134 .shared_clock = &i2c_clock,
135};
136
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100137static const struct acpi_device_id acpi_lpss_device_ids[] = {
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300138 /* Generic LPSS devices */
139 { "INTL9C60", (unsigned long)&lpss_dma_desc },
140
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100141 /* Lynxpoint LPSS devices */
142 { "INT33C0", (unsigned long)&lpt_dev_desc },
143 { "INT33C1", (unsigned long)&lpt_dev_desc },
144 { "INT33C2", (unsigned long)&lpt_dev_desc },
145 { "INT33C3", (unsigned long)&lpt_dev_desc },
Heikki Krogerus06d86412013-06-17 13:25:46 +0300146 { "INT33C4", (unsigned long)&lpt_uart_dev_desc },
147 { "INT33C5", (unsigned long)&lpt_uart_dev_desc },
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100148 { "INT33C6", (unsigned long)&lpt_sdio_dev_desc },
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100149 { "INT33C7", },
150
Mika Westerbergf6272172013-05-13 12:42:44 +0000151 /* BayTrail LPSS devices */
152 { "80860F0A", (unsigned long)&byt_uart_dev_desc },
153 { "80860F0E", (unsigned long)&byt_spi_dev_desc },
154 { "80860F14", (unsigned long)&byt_sdio_dev_desc },
155 { "80860F41", (unsigned long)&byt_i2c_dev_desc },
156 { "INT33B2", },
157
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100158 { }
159};
160
161static int is_memory(struct acpi_resource *res, void *not_used)
162{
163 struct resource r;
164 return !acpi_dev_resource_memory(res, &r);
165}
166
167/* LPSS main clock device. */
168static struct platform_device *lpss_clk_dev;
169
170static inline void lpt_register_clock_device(void)
171{
172 lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
173}
174
175static int register_device_clock(struct acpi_device *adev,
176 struct lpss_private_data *pdata)
177{
178 const struct lpss_device_desc *dev_desc = pdata->dev_desc;
Mika Westerbergf6272172013-05-13 12:42:44 +0000179 struct lpss_shared_clock *shared_clock = dev_desc->shared_clock;
180 struct clk *clk = ERR_PTR(-ENODEV);
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300181 struct lpss_clk_data *clk_data;
Mika Westerbergf6272172013-05-13 12:42:44 +0000182 const char *parent;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100183
184 if (!lpss_clk_dev)
185 lpt_register_clock_device();
186
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300187 clk_data = platform_get_drvdata(lpss_clk_dev);
188 if (!clk_data)
189 return -ENODEV;
190
191 if (dev_desc->clkdev_name) {
192 clk_register_clkdev(clk_data->clk, dev_desc->clkdev_name,
193 dev_name(&adev->dev));
194 return 0;
195 }
196
197 if (!pdata->mmio_base
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100198 || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100199 return -ENODATA;
200
Mika Westerbergf6272172013-05-13 12:42:44 +0000201 parent = clk_data->name;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100202
Mika Westerbergf6272172013-05-13 12:42:44 +0000203 if (shared_clock) {
204 clk = shared_clock->clk;
205 if (!clk) {
206 clk = clk_register_fixed_rate(NULL, shared_clock->name,
207 "lpss_clk", 0,
208 shared_clock->rate);
209 shared_clock->clk = clk;
210 }
211 parent = shared_clock->name;
212 }
213
214 if (dev_desc->clk_gate) {
215 clk = clk_register_gate(NULL, dev_name(&adev->dev), parent, 0,
216 pdata->mmio_base + dev_desc->prv_offset,
217 0, 0, NULL);
218 pdata->clk = clk;
219 }
220
221 if (IS_ERR(clk))
222 return PTR_ERR(clk);
223
224 clk_register_clkdev(clk, NULL, dev_name(&adev->dev));
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100225 return 0;
226}
227
228static int acpi_lpss_create_device(struct acpi_device *adev,
229 const struct acpi_device_id *id)
230{
231 struct lpss_device_desc *dev_desc;
232 struct lpss_private_data *pdata;
233 struct resource_list_entry *rentry;
234 struct list_head resource_list;
235 int ret;
236
237 dev_desc = (struct lpss_device_desc *)id->driver_data;
238 if (!dev_desc)
239 return acpi_create_platform_device(adev, id);
240
241 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
242 if (!pdata)
243 return -ENOMEM;
244
245 INIT_LIST_HEAD(&resource_list);
246 ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
247 if (ret < 0)
248 goto err_out;
249
250 list_for_each_entry(rentry, &resource_list, node)
251 if (resource_type(&rentry->res) == IORESOURCE_MEM) {
252 pdata->mmio_size = resource_size(&rentry->res);
253 pdata->mmio_base = ioremap(rentry->res.start,
254 pdata->mmio_size);
255 pdata->dev_desc = dev_desc;
256 break;
257 }
258
259 acpi_dev_free_resource_list(&resource_list);
260
261 if (dev_desc->clk_required) {
262 ret = register_device_clock(adev, pdata);
263 if (ret) {
264 /*
265 * Skip the device, but don't terminate the namespace
266 * scan.
267 */
Andy Shevchenkocf8df962013-03-20 12:14:30 +0000268 kfree(pdata);
269 return 0;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100270 }
271 }
272
Heikki Krogerus06d86412013-06-17 13:25:46 +0300273 if (dev_desc->setup)
274 dev_desc->setup(pdata);
275
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100276 adev->driver_data = pdata;
277 ret = acpi_create_platform_device(adev, id);
278 if (ret > 0)
279 return ret;
280
281 adev->driver_data = NULL;
282
283 err_out:
284 kfree(pdata);
285 return ret;
286}
287
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100288static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
289{
290 struct acpi_device *adev;
291 struct lpss_private_data *pdata;
292 unsigned long flags;
293 int ret;
294
295 ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
296 if (WARN_ON(ret))
297 return ret;
298
299 spin_lock_irqsave(&dev->power.lock, flags);
300 if (pm_runtime_suspended(dev)) {
301 ret = -EAGAIN;
302 goto out;
303 }
304 pdata = acpi_driver_data(adev);
305 if (WARN_ON(!pdata || !pdata->mmio_base)) {
306 ret = -ENODEV;
307 goto out;
308 }
309 *val = readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
310
311 out:
312 spin_unlock_irqrestore(&dev->power.lock, flags);
313 return ret;
314}
315
316static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
317 char *buf)
318{
319 u32 ltr_value = 0;
320 unsigned int reg;
321 int ret;
322
323 reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
324 ret = lpss_reg_read(dev, reg, &ltr_value);
325 if (ret)
326 return ret;
327
328 return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
329}
330
331static ssize_t lpss_ltr_mode_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
333{
334 u32 ltr_mode = 0;
335 char *outstr;
336 int ret;
337
338 ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
339 if (ret)
340 return ret;
341
342 outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
343 return sprintf(buf, "%s\n", outstr);
344}
345
346static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
347static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
348static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
349
350static struct attribute *lpss_attrs[] = {
351 &dev_attr_auto_ltr.attr,
352 &dev_attr_sw_ltr.attr,
353 &dev_attr_ltr_mode.attr,
354 NULL,
355};
356
357static struct attribute_group lpss_attr_group = {
358 .attrs = lpss_attrs,
359 .name = "lpss_ltr",
360};
361
362static int acpi_lpss_platform_notify(struct notifier_block *nb,
363 unsigned long action, void *data)
364{
365 struct platform_device *pdev = to_platform_device(data);
366 struct lpss_private_data *pdata;
367 struct acpi_device *adev;
368 const struct acpi_device_id *id;
369 int ret = 0;
370
371 id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
372 if (!id || !id->driver_data)
373 return 0;
374
375 if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
376 return 0;
377
378 pdata = acpi_driver_data(adev);
379 if (!pdata || !pdata->mmio_base || !pdata->dev_desc->ltr_required)
380 return 0;
381
382 if (pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
383 dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
384 return 0;
385 }
386
387 if (action == BUS_NOTIFY_ADD_DEVICE)
388 ret = sysfs_create_group(&pdev->dev.kobj, &lpss_attr_group);
389 else if (action == BUS_NOTIFY_DEL_DEVICE)
390 sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
391
392 return ret;
393}
394
395static struct notifier_block acpi_lpss_nb = {
396 .notifier_call = acpi_lpss_platform_notify,
397};
398
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100399static struct acpi_scan_handler lpss_handler = {
400 .ids = acpi_lpss_device_ids,
401 .attach = acpi_lpss_create_device,
402};
403
404void __init acpi_lpss_init(void)
405{
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100406 if (!lpt_clk_init()) {
407 bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100408 acpi_scan_add_handler(&lpss_handler);
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100409 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100410}