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 *clk_parent
;
40 unsigned int prv_offset
;
43 struct lpss_private_data
{
44 void __iomem
*mmio_base
;
45 resource_size_t mmio_size
;
47 const struct lpss_device_desc
*dev_desc
;
50 static struct lpss_device_desc lpt_dev_desc
= {
52 .clk_parent
= "lpss_clk",
57 static struct lpss_device_desc lpt_sdio_dev_desc
= {
62 static const struct acpi_device_id acpi_lpss_device_ids
[] = {
63 /* Lynxpoint LPSS devices */
64 { "INT33C0", (unsigned long)&lpt_dev_desc
},
65 { "INT33C1", (unsigned long)&lpt_dev_desc
},
66 { "INT33C2", (unsigned long)&lpt_dev_desc
},
67 { "INT33C3", (unsigned long)&lpt_dev_desc
},
68 { "INT33C4", (unsigned long)&lpt_dev_desc
},
69 { "INT33C5", (unsigned long)&lpt_dev_desc
},
70 { "INT33C6", (unsigned long)&lpt_sdio_dev_desc
},
76 static int is_memory(struct acpi_resource
*res
, void *not_used
)
79 return !acpi_dev_resource_memory(res
, &r
);
82 /* LPSS main clock device. */
83 static struct platform_device
*lpss_clk_dev
;
85 static inline void lpt_register_clock_device(void)
87 lpss_clk_dev
= platform_device_register_simple("clk-lpt", -1, NULL
, 0);
90 static int register_device_clock(struct acpi_device
*adev
,
91 struct lpss_private_data
*pdata
)
93 const struct lpss_device_desc
*dev_desc
= pdata
->dev_desc
;
96 lpt_register_clock_device();
98 if (!dev_desc
->clk_parent
|| !pdata
->mmio_base
99 || pdata
->mmio_size
< dev_desc
->prv_offset
+ LPSS_CLK_SIZE
)
102 pdata
->clk
= clk_register_gate(NULL
, dev_name(&adev
->dev
),
103 dev_desc
->clk_parent
, 0,
104 pdata
->mmio_base
+ dev_desc
->prv_offset
,
106 if (IS_ERR(pdata
->clk
))
107 return PTR_ERR(pdata
->clk
);
109 clk_register_clkdev(pdata
->clk
, NULL
, dev_name(&adev
->dev
));
113 static int acpi_lpss_create_device(struct acpi_device
*adev
,
114 const struct acpi_device_id
*id
)
116 struct lpss_device_desc
*dev_desc
;
117 struct lpss_private_data
*pdata
;
118 struct resource_list_entry
*rentry
;
119 struct list_head resource_list
;
122 dev_desc
= (struct lpss_device_desc
*)id
->driver_data
;
124 return acpi_create_platform_device(adev
, id
);
126 pdata
= kzalloc(sizeof(*pdata
), GFP_KERNEL
);
130 INIT_LIST_HEAD(&resource_list
);
131 ret
= acpi_dev_get_resources(adev
, &resource_list
, is_memory
, NULL
);
135 list_for_each_entry(rentry
, &resource_list
, node
)
136 if (resource_type(&rentry
->res
) == IORESOURCE_MEM
) {
137 pdata
->mmio_size
= resource_size(&rentry
->res
);
138 pdata
->mmio_base
= ioremap(rentry
->res
.start
,
140 pdata
->dev_desc
= dev_desc
;
144 acpi_dev_free_resource_list(&resource_list
);
146 if (dev_desc
->clk_required
) {
147 ret
= register_device_clock(adev
, pdata
);
150 * Skip the device, but don't terminate the namespace
158 adev
->driver_data
= pdata
;
159 ret
= acpi_create_platform_device(adev
, id
);
163 adev
->driver_data
= NULL
;
170 static int lpss_reg_read(struct device
*dev
, unsigned int reg
, u32
*val
)
172 struct acpi_device
*adev
;
173 struct lpss_private_data
*pdata
;
177 ret
= acpi_bus_get_device(ACPI_HANDLE(dev
), &adev
);
181 spin_lock_irqsave(&dev
->power
.lock
, flags
);
182 if (pm_runtime_suspended(dev
)) {
186 pdata
= acpi_driver_data(adev
);
187 if (WARN_ON(!pdata
|| !pdata
->mmio_base
)) {
191 *val
= readl(pdata
->mmio_base
+ pdata
->dev_desc
->prv_offset
+ reg
);
194 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
198 static ssize_t
lpss_ltr_show(struct device
*dev
, struct device_attribute
*attr
,
205 reg
= strcmp(attr
->attr
.name
, "auto_ltr") ? LPSS_SW_LTR
: LPSS_AUTO_LTR
;
206 ret
= lpss_reg_read(dev
, reg
, <r_value
);
210 return snprintf(buf
, PAGE_SIZE
, "%08x\n", ltr_value
);
213 static ssize_t
lpss_ltr_mode_show(struct device
*dev
,
214 struct device_attribute
*attr
, char *buf
)
220 ret
= lpss_reg_read(dev
, LPSS_GENERAL
, <r_mode
);
224 outstr
= (ltr_mode
& LPSS_GENERAL_LTR_MODE_SW
) ? "sw" : "auto";
225 return sprintf(buf
, "%s\n", outstr
);
228 static DEVICE_ATTR(auto_ltr
, S_IRUSR
, lpss_ltr_show
, NULL
);
229 static DEVICE_ATTR(sw_ltr
, S_IRUSR
, lpss_ltr_show
, NULL
);
230 static DEVICE_ATTR(ltr_mode
, S_IRUSR
, lpss_ltr_mode_show
, NULL
);
232 static struct attribute
*lpss_attrs
[] = {
233 &dev_attr_auto_ltr
.attr
,
234 &dev_attr_sw_ltr
.attr
,
235 &dev_attr_ltr_mode
.attr
,
239 static struct attribute_group lpss_attr_group
= {
244 static int acpi_lpss_platform_notify(struct notifier_block
*nb
,
245 unsigned long action
, void *data
)
247 struct platform_device
*pdev
= to_platform_device(data
);
248 struct lpss_private_data
*pdata
;
249 struct acpi_device
*adev
;
250 const struct acpi_device_id
*id
;
253 id
= acpi_match_device(acpi_lpss_device_ids
, &pdev
->dev
);
254 if (!id
|| !id
->driver_data
)
257 if (acpi_bus_get_device(ACPI_HANDLE(&pdev
->dev
), &adev
))
260 pdata
= acpi_driver_data(adev
);
261 if (!pdata
|| !pdata
->mmio_base
|| !pdata
->dev_desc
->ltr_required
)
264 if (pdata
->mmio_size
< pdata
->dev_desc
->prv_offset
+ LPSS_LTR_SIZE
) {
265 dev_err(&pdev
->dev
, "MMIO size insufficient to access LTR\n");
269 if (action
== BUS_NOTIFY_ADD_DEVICE
)
270 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &lpss_attr_group
);
271 else if (action
== BUS_NOTIFY_DEL_DEVICE
)
272 sysfs_remove_group(&pdev
->dev
.kobj
, &lpss_attr_group
);
277 static struct notifier_block acpi_lpss_nb
= {
278 .notifier_call
= acpi_lpss_platform_notify
,
281 static struct acpi_scan_handler lpss_handler
= {
282 .ids
= acpi_lpss_device_ids
,
283 .attach
= acpi_lpss_create_device
,
286 void __init
acpi_lpss_init(void)
288 if (!lpt_clk_init()) {
289 bus_register_notifier(&platform_bus_type
, &acpi_lpss_nb
);
290 acpi_scan_add_handler(&lpss_handler
);