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_SW_LTR 0x10
34 #define LPSS_AUTO_LTR 0x14
36 struct lpss_device_desc
{
38 const char *clkdev_name
;
40 unsigned int prv_offset
;
43 static struct lpss_device_desc lpss_dma_desc
= {
45 .clkdev_name
= "hclk",
48 struct lpss_private_data
{
49 void __iomem
*mmio_base
;
50 resource_size_t mmio_size
;
52 const struct lpss_device_desc
*dev_desc
;
55 static struct lpss_device_desc lpt_dev_desc
= {
61 static struct lpss_device_desc lpt_sdio_dev_desc
= {
66 static const struct acpi_device_id acpi_lpss_device_ids
[] = {
67 /* Generic LPSS devices */
68 { "INTL9C60", (unsigned long)&lpss_dma_desc
},
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
},
83 static int is_memory(struct acpi_resource
*res
, void *not_used
)
86 return !acpi_dev_resource_memory(res
, &r
);
89 /* LPSS main clock device. */
90 static struct platform_device
*lpss_clk_dev
;
92 static inline void lpt_register_clock_device(void)
94 lpss_clk_dev
= platform_device_register_simple("clk-lpt", -1, NULL
, 0);
97 static int register_device_clock(struct acpi_device
*adev
,
98 struct lpss_private_data
*pdata
)
100 const struct lpss_device_desc
*dev_desc
= pdata
->dev_desc
;
101 struct lpss_clk_data
*clk_data
;
104 lpt_register_clock_device();
106 clk_data
= platform_get_drvdata(lpss_clk_dev
);
110 if (dev_desc
->clkdev_name
) {
111 clk_register_clkdev(clk_data
->clk
, dev_desc
->clkdev_name
,
112 dev_name(&adev
->dev
));
116 if (!pdata
->mmio_base
117 || pdata
->mmio_size
< dev_desc
->prv_offset
+ LPSS_CLK_SIZE
)
120 pdata
->clk
= clk_register_gate(NULL
, dev_name(&adev
->dev
),
122 pdata
->mmio_base
+ dev_desc
->prv_offset
,
124 if (IS_ERR(pdata
->clk
))
125 return PTR_ERR(pdata
->clk
);
127 clk_register_clkdev(pdata
->clk
, NULL
, dev_name(&adev
->dev
));
131 static int acpi_lpss_create_device(struct acpi_device
*adev
,
132 const struct acpi_device_id
*id
)
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
;
140 dev_desc
= (struct lpss_device_desc
*)id
->driver_data
;
142 return acpi_create_platform_device(adev
, id
);
144 pdata
= kzalloc(sizeof(*pdata
), GFP_KERNEL
);
148 INIT_LIST_HEAD(&resource_list
);
149 ret
= acpi_dev_get_resources(adev
, &resource_list
, is_memory
, NULL
);
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
,
161 acpi_dev_free_resource_list(&resource_list
);
163 pdata
->dev_desc
= dev_desc
;
165 if (dev_desc
->clk_required
) {
166 ret
= register_device_clock(adev
, pdata
);
168 /* Skip the device, but continue the namespace scan. */
175 * This works around a known issue in ACPI tables where LPSS devices
176 * have _PS0 and _PS3 without _PSC (and no power resources), so
177 * acpi_bus_init_power() will assume that the BIOS has put them into D0.
179 ret
= acpi_device_fix_up_power(adev
);
181 /* Skip the device, but continue the namespace scan. */
186 adev
->driver_data
= pdata
;
187 ret
= acpi_create_platform_device(adev
, id
);
191 adev
->driver_data
= NULL
;
198 static int lpss_reg_read(struct device
*dev
, unsigned int reg
, u32
*val
)
200 struct acpi_device
*adev
;
201 struct lpss_private_data
*pdata
;
205 ret
= acpi_bus_get_device(ACPI_HANDLE(dev
), &adev
);
209 spin_lock_irqsave(&dev
->power
.lock
, flags
);
210 if (pm_runtime_suspended(dev
)) {
214 pdata
= acpi_driver_data(adev
);
215 if (WARN_ON(!pdata
|| !pdata
->mmio_base
)) {
219 *val
= readl(pdata
->mmio_base
+ pdata
->dev_desc
->prv_offset
+ reg
);
222 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
226 static ssize_t
lpss_ltr_show(struct device
*dev
, struct device_attribute
*attr
,
233 reg
= strcmp(attr
->attr
.name
, "auto_ltr") ? LPSS_SW_LTR
: LPSS_AUTO_LTR
;
234 ret
= lpss_reg_read(dev
, reg
, <r_value
);
238 return snprintf(buf
, PAGE_SIZE
, "%08x\n", ltr_value
);
241 static ssize_t
lpss_ltr_mode_show(struct device
*dev
,
242 struct device_attribute
*attr
, char *buf
)
248 ret
= lpss_reg_read(dev
, LPSS_GENERAL
, <r_mode
);
252 outstr
= (ltr_mode
& LPSS_GENERAL_LTR_MODE_SW
) ? "sw" : "auto";
253 return sprintf(buf
, "%s\n", outstr
);
256 static DEVICE_ATTR(auto_ltr
, S_IRUSR
, lpss_ltr_show
, NULL
);
257 static DEVICE_ATTR(sw_ltr
, S_IRUSR
, lpss_ltr_show
, NULL
);
258 static DEVICE_ATTR(ltr_mode
, S_IRUSR
, lpss_ltr_mode_show
, NULL
);
260 static struct attribute
*lpss_attrs
[] = {
261 &dev_attr_auto_ltr
.attr
,
262 &dev_attr_sw_ltr
.attr
,
263 &dev_attr_ltr_mode
.attr
,
267 static struct attribute_group lpss_attr_group
= {
272 static int acpi_lpss_platform_notify(struct notifier_block
*nb
,
273 unsigned long action
, void *data
)
275 struct platform_device
*pdev
= to_platform_device(data
);
276 struct lpss_private_data
*pdata
;
277 struct acpi_device
*adev
;
278 const struct acpi_device_id
*id
;
281 id
= acpi_match_device(acpi_lpss_device_ids
, &pdev
->dev
);
282 if (!id
|| !id
->driver_data
)
285 if (acpi_bus_get_device(ACPI_HANDLE(&pdev
->dev
), &adev
))
288 pdata
= acpi_driver_data(adev
);
289 if (!pdata
|| !pdata
->mmio_base
|| !pdata
->dev_desc
->ltr_required
)
292 if (pdata
->mmio_size
< pdata
->dev_desc
->prv_offset
+ LPSS_LTR_SIZE
) {
293 dev_err(&pdev
->dev
, "MMIO size insufficient to access LTR\n");
297 if (action
== BUS_NOTIFY_ADD_DEVICE
)
298 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &lpss_attr_group
);
299 else if (action
== BUS_NOTIFY_DEL_DEVICE
)
300 sysfs_remove_group(&pdev
->dev
.kobj
, &lpss_attr_group
);
305 static struct notifier_block acpi_lpss_nb
= {
306 .notifier_call
= acpi_lpss_platform_notify
,
309 static struct acpi_scan_handler lpss_handler
= {
310 .ids
= acpi_lpss_device_ids
,
311 .attach
= acpi_lpss_create_device
,
314 void __init
acpi_lpss_init(void)
316 if (!lpt_clk_init()) {
317 bus_register_notifier(&platform_bus_type
, &acpi_lpss_nb
);
318 acpi_scan_add_handler(&lpss_handler
);