2 * ACPI support for Intel Lynxpoint LPSS.
4 * Copyright (C) 2013, Intel Corporation
5 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
6 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
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.
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>
19 #include <linux/platform_device.h>
20 #include <linux/platform_data/clk-lpss.h>
21 #include <linux/pm_runtime.h>
25 ACPI_MODULE_NAME("acpi_lpss");
27 #define LPSS_CLK_SIZE 0x04
28 #define LPSS_LTR_SIZE 0x18
30 /* Offsets relative to LPSS_PRIVATE_OFFSET */
31 #define LPSS_GENERAL 0x08
32 #define LPSS_GENERAL_LTR_MODE_SW BIT(2)
33 #define LPSS_GENERAL_UART_RTS_OVRD BIT(3)
34 #define LPSS_SW_LTR 0x10
35 #define LPSS_AUTO_LTR 0x14
36 #define LPSS_TX_INT 0x20
37 #define LPSS_TX_INT_MASK BIT(1)
39 struct lpss_shared_clock {
45 struct lpss_private_data;
47 struct lpss_device_desc {
49 const char *clkdev_name;
51 unsigned int prv_offset;
52 size_t prv_size_override;
54 struct lpss_shared_clock *shared_clock;
55 void (*setup)(struct lpss_private_data *pdata);
58 static struct lpss_device_desc lpss_dma_desc = {
60 .clkdev_name = "hclk",
63 struct lpss_private_data {
64 void __iomem *mmio_base;
65 resource_size_t mmio_size;
67 const struct lpss_device_desc *dev_desc;
70 static void lpss_uart_setup(struct lpss_private_data *pdata)
75 offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
76 reg = readl(pdata->mmio_base + offset);
77 writel(reg | LPSS_TX_INT_MASK, pdata->mmio_base + offset);
79 offset = pdata->dev_desc->prv_offset + LPSS_GENERAL;
80 reg = readl(pdata->mmio_base + offset);
81 writel(reg | LPSS_GENERAL_UART_RTS_OVRD, pdata->mmio_base + offset);
84 static struct lpss_device_desc lpt_dev_desc = {
91 static struct lpss_device_desc lpt_uart_dev_desc = {
96 .setup = lpss_uart_setup,
99 static struct lpss_device_desc lpt_sdio_dev_desc = {
100 .prv_offset = 0x1000,
101 .prv_size_override = 0x1018,
102 .ltr_required = true,
105 static struct lpss_shared_clock uart_clock = {
110 static struct lpss_device_desc byt_uart_dev_desc = {
111 .clk_required = true,
114 .shared_clock = &uart_clock,
115 .setup = lpss_uart_setup,
118 static struct lpss_shared_clock spi_clock = {
123 static struct lpss_device_desc byt_spi_dev_desc = {
124 .clk_required = true,
127 .shared_clock = &spi_clock,
130 static struct lpss_device_desc byt_sdio_dev_desc = {
131 .clk_required = true,
134 static struct lpss_shared_clock i2c_clock = {
139 static struct lpss_device_desc byt_i2c_dev_desc = {
140 .clk_required = true,
142 .shared_clock = &i2c_clock,
145 static const struct acpi_device_id acpi_lpss_device_ids[] = {
146 /* Generic LPSS devices */
147 { "INTL9C60", (unsigned long)&lpss_dma_desc },
149 /* Lynxpoint LPSS devices */
150 { "INT33C0", (unsigned long)&lpt_dev_desc },
151 { "INT33C1", (unsigned long)&lpt_dev_desc },
152 { "INT33C2", (unsigned long)&lpt_dev_desc },
153 { "INT33C3", (unsigned long)&lpt_dev_desc },
154 { "INT33C4", (unsigned long)&lpt_uart_dev_desc },
155 { "INT33C5", (unsigned long)&lpt_uart_dev_desc },
156 { "INT33C6", (unsigned long)&lpt_sdio_dev_desc },
159 /* BayTrail LPSS devices */
160 { "80860F0A", (unsigned long)&byt_uart_dev_desc },
161 { "80860F0E", (unsigned long)&byt_spi_dev_desc },
162 { "80860F14", (unsigned long)&byt_sdio_dev_desc },
163 { "80860F41", (unsigned long)&byt_i2c_dev_desc },
167 { "INT3430", (unsigned long)&lpt_dev_desc },
168 { "INT3431", (unsigned long)&lpt_dev_desc },
169 { "INT3432", (unsigned long)&lpt_dev_desc },
170 { "INT3433", (unsigned long)&lpt_dev_desc },
171 { "INT3434", (unsigned long)&lpt_uart_dev_desc },
172 { "INT3435", (unsigned long)&lpt_uart_dev_desc },
173 { "INT3436", (unsigned long)&lpt_sdio_dev_desc },
179 static int is_memory(struct acpi_resource *res, void *not_used)
182 return !acpi_dev_resource_memory(res, &r);
185 /* LPSS main clock device. */
186 static struct platform_device *lpss_clk_dev;
188 static inline void lpt_register_clock_device(void)
190 lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
193 static int register_device_clock(struct acpi_device *adev,
194 struct lpss_private_data *pdata)
196 const struct lpss_device_desc *dev_desc = pdata->dev_desc;
197 struct lpss_shared_clock *shared_clock = dev_desc->shared_clock;
198 struct clk *clk = ERR_PTR(-ENODEV);
199 struct lpss_clk_data *clk_data;
203 lpt_register_clock_device();
205 clk_data = platform_get_drvdata(lpss_clk_dev);
209 if (dev_desc->clkdev_name) {
210 clk_register_clkdev(clk_data->clk, dev_desc->clkdev_name,
211 dev_name(&adev->dev));
215 if (!pdata->mmio_base
216 || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
219 parent = clk_data->name;
222 clk = shared_clock->clk;
224 clk = clk_register_fixed_rate(NULL, shared_clock->name,
227 shared_clock->clk = clk;
229 parent = shared_clock->name;
232 if (dev_desc->clk_gate) {
233 clk = clk_register_gate(NULL, dev_name(&adev->dev), parent, 0,
234 pdata->mmio_base + dev_desc->prv_offset,
242 clk_register_clkdev(clk, NULL, dev_name(&adev->dev));
246 static int acpi_lpss_create_device(struct acpi_device *adev,
247 const struct acpi_device_id *id)
249 struct lpss_device_desc *dev_desc;
250 struct lpss_private_data *pdata;
251 struct resource_list_entry *rentry;
252 struct list_head resource_list;
255 dev_desc = (struct lpss_device_desc *)id->driver_data;
257 return acpi_create_platform_device(adev, id);
259 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
263 INIT_LIST_HEAD(&resource_list);
264 ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
268 list_for_each_entry(rentry, &resource_list, node)
269 if (resource_type(&rentry->res) == IORESOURCE_MEM) {
270 if (dev_desc->prv_size_override)
271 pdata->mmio_size = dev_desc->prv_size_override;
273 pdata->mmio_size = resource_size(&rentry->res);
274 pdata->mmio_base = ioremap(rentry->res.start,
279 acpi_dev_free_resource_list(&resource_list);
281 pdata->dev_desc = dev_desc;
283 if (dev_desc->clk_required) {
284 ret = register_device_clock(adev, pdata);
286 /* Skip the device, but continue the namespace scan. */
293 * This works around a known issue in ACPI tables where LPSS devices
294 * have _PS0 and _PS3 without _PSC (and no power resources), so
295 * acpi_bus_init_power() will assume that the BIOS has put them into D0.
297 ret = acpi_device_fix_up_power(adev);
299 /* Skip the device, but continue the namespace scan. */
305 dev_desc->setup(pdata);
307 adev->driver_data = pdata;
308 ret = acpi_create_platform_device(adev, id);
312 adev->driver_data = NULL;
319 static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
321 struct acpi_device *adev;
322 struct lpss_private_data *pdata;
326 ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
330 spin_lock_irqsave(&dev->power.lock, flags);
331 if (pm_runtime_suspended(dev)) {
335 pdata = acpi_driver_data(adev);
336 if (WARN_ON(!pdata || !pdata->mmio_base)) {
340 *val = readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
343 spin_unlock_irqrestore(&dev->power.lock, flags);
347 static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
354 reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
355 ret = lpss_reg_read(dev, reg, <r_value);
359 return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
362 static ssize_t lpss_ltr_mode_show(struct device *dev,
363 struct device_attribute *attr, char *buf)
369 ret = lpss_reg_read(dev, LPSS_GENERAL, <r_mode);
373 outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
374 return sprintf(buf, "%s\n", outstr);
377 static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
378 static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
379 static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
381 static struct attribute *lpss_attrs[] = {
382 &dev_attr_auto_ltr.attr,
383 &dev_attr_sw_ltr.attr,
384 &dev_attr_ltr_mode.attr,
388 static struct attribute_group lpss_attr_group = {
393 static int acpi_lpss_platform_notify(struct notifier_block *nb,
394 unsigned long action, void *data)
396 struct platform_device *pdev = to_platform_device(data);
397 struct lpss_private_data *pdata;
398 struct acpi_device *adev;
399 const struct acpi_device_id *id;
402 id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
403 if (!id || !id->driver_data)
406 if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
409 pdata = acpi_driver_data(adev);
410 if (!pdata || !pdata->mmio_base || !pdata->dev_desc->ltr_required)
413 if (pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
414 dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
418 if (action == BUS_NOTIFY_ADD_DEVICE)
419 ret = sysfs_create_group(&pdev->dev.kobj, &lpss_attr_group);
420 else if (action == BUS_NOTIFY_DEL_DEVICE)
421 sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
426 static struct notifier_block acpi_lpss_nb = {
427 .notifier_call = acpi_lpss_platform_notify,
430 static struct acpi_scan_handler lpss_handler = {
431 .ids = acpi_lpss_device_ids,
432 .attach = acpi_lpss_create_device,
435 void __init acpi_lpss_init(void)
437 if (!lpt_clk_init()) {
438 bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
439 acpi_scan_add_handler(&lpss_handler);