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
35 #define LPSS_TX_INT 0x20
36 #define LPSS_TX_INT_MASK BIT(1)
38 struct lpss_shared_clock
{
44 struct lpss_private_data
;
46 struct lpss_device_desc
{
48 const char *clkdev_name
;
50 unsigned int prv_offset
;
51 size_t prv_size_override
;
53 struct lpss_shared_clock
*shared_clock
;
54 void (*setup
)(struct lpss_private_data
*pdata
);
57 static struct lpss_device_desc lpss_dma_desc
= {
59 .clkdev_name
= "hclk",
62 struct lpss_private_data
{
63 void __iomem
*mmio_base
;
64 resource_size_t mmio_size
;
66 const struct lpss_device_desc
*dev_desc
;
69 static void lpss_uart_setup(struct lpss_private_data
*pdata
)
71 unsigned int tx_int_offset
= pdata
->dev_desc
->prv_offset
+ LPSS_TX_INT
;
74 reg
= readl(pdata
->mmio_base
+ tx_int_offset
);
75 writel(reg
| LPSS_TX_INT_MASK
, pdata
->mmio_base
+ tx_int_offset
);
78 static struct lpss_device_desc lpt_dev_desc
= {
85 static struct lpss_device_desc lpt_uart_dev_desc
= {
90 .setup
= lpss_uart_setup
,
93 static struct lpss_device_desc lpt_sdio_dev_desc
= {
95 .prv_size_override
= 0x1018,
99 static struct lpss_shared_clock uart_clock
= {
104 static struct lpss_device_desc byt_uart_dev_desc
= {
105 .clk_required
= true,
108 .shared_clock
= &uart_clock
,
109 .setup
= lpss_uart_setup
,
112 static struct lpss_shared_clock spi_clock
= {
117 static struct lpss_device_desc byt_spi_dev_desc
= {
118 .clk_required
= true,
121 .shared_clock
= &spi_clock
,
124 static struct lpss_device_desc byt_sdio_dev_desc
= {
125 .clk_required
= true,
128 static struct lpss_shared_clock i2c_clock
= {
133 static struct lpss_device_desc byt_i2c_dev_desc
= {
134 .clk_required
= true,
136 .shared_clock
= &i2c_clock
,
139 static const struct acpi_device_id acpi_lpss_device_ids
[] = {
140 /* Generic LPSS devices */
141 { "INTL9C60", (unsigned long)&lpss_dma_desc
},
143 /* Lynxpoint LPSS devices */
144 { "INT33C0", (unsigned long)&lpt_dev_desc
},
145 { "INT33C1", (unsigned long)&lpt_dev_desc
},
146 { "INT33C2", (unsigned long)&lpt_dev_desc
},
147 { "INT33C3", (unsigned long)&lpt_dev_desc
},
148 { "INT33C4", (unsigned long)&lpt_uart_dev_desc
},
149 { "INT33C5", (unsigned long)&lpt_uart_dev_desc
},
150 { "INT33C6", (unsigned long)&lpt_sdio_dev_desc
},
153 /* BayTrail LPSS devices */
154 { "80860F0A", (unsigned long)&byt_uart_dev_desc
},
155 { "80860F0E", (unsigned long)&byt_spi_dev_desc
},
156 { "80860F14", (unsigned long)&byt_sdio_dev_desc
},
157 { "80860F41", (unsigned long)&byt_i2c_dev_desc
},
163 static int is_memory(struct acpi_resource
*res
, void *not_used
)
166 return !acpi_dev_resource_memory(res
, &r
);
169 /* LPSS main clock device. */
170 static struct platform_device
*lpss_clk_dev
;
172 static inline void lpt_register_clock_device(void)
174 lpss_clk_dev
= platform_device_register_simple("clk-lpt", -1, NULL
, 0);
177 static int register_device_clock(struct acpi_device
*adev
,
178 struct lpss_private_data
*pdata
)
180 const struct lpss_device_desc
*dev_desc
= pdata
->dev_desc
;
181 struct lpss_shared_clock
*shared_clock
= dev_desc
->shared_clock
;
182 struct clk
*clk
= ERR_PTR(-ENODEV
);
183 struct lpss_clk_data
*clk_data
;
187 lpt_register_clock_device();
189 clk_data
= platform_get_drvdata(lpss_clk_dev
);
193 if (dev_desc
->clkdev_name
) {
194 clk_register_clkdev(clk_data
->clk
, dev_desc
->clkdev_name
,
195 dev_name(&adev
->dev
));
199 if (!pdata
->mmio_base
200 || pdata
->mmio_size
< dev_desc
->prv_offset
+ LPSS_CLK_SIZE
)
203 parent
= clk_data
->name
;
206 clk
= shared_clock
->clk
;
208 clk
= clk_register_fixed_rate(NULL
, shared_clock
->name
,
211 shared_clock
->clk
= clk
;
213 parent
= shared_clock
->name
;
216 if (dev_desc
->clk_gate
) {
217 clk
= clk_register_gate(NULL
, dev_name(&adev
->dev
), parent
, 0,
218 pdata
->mmio_base
+ dev_desc
->prv_offset
,
226 clk_register_clkdev(clk
, NULL
, dev_name(&adev
->dev
));
230 static int acpi_lpss_create_device(struct acpi_device
*adev
,
231 const struct acpi_device_id
*id
)
233 struct lpss_device_desc
*dev_desc
;
234 struct lpss_private_data
*pdata
;
235 struct resource_list_entry
*rentry
;
236 struct list_head resource_list
;
239 dev_desc
= (struct lpss_device_desc
*)id
->driver_data
;
241 return acpi_create_platform_device(adev
, id
);
243 pdata
= kzalloc(sizeof(*pdata
), GFP_KERNEL
);
247 INIT_LIST_HEAD(&resource_list
);
248 ret
= acpi_dev_get_resources(adev
, &resource_list
, is_memory
, NULL
);
252 list_for_each_entry(rentry
, &resource_list
, node
)
253 if (resource_type(&rentry
->res
) == IORESOURCE_MEM
) {
254 if (dev_desc
->prv_size_override
)
255 pdata
->mmio_size
= dev_desc
->prv_size_override
;
257 pdata
->mmio_size
= resource_size(&rentry
->res
);
258 pdata
->mmio_base
= ioremap(rentry
->res
.start
,
263 acpi_dev_free_resource_list(&resource_list
);
265 pdata
->dev_desc
= dev_desc
;
267 if (dev_desc
->clk_required
) {
268 ret
= register_device_clock(adev
, pdata
);
270 /* Skip the device, but continue the namespace scan. */
277 * This works around a known issue in ACPI tables where LPSS devices
278 * have _PS0 and _PS3 without _PSC (and no power resources), so
279 * acpi_bus_init_power() will assume that the BIOS has put them into D0.
281 ret
= acpi_device_fix_up_power(adev
);
283 /* Skip the device, but continue the namespace scan. */
289 dev_desc
->setup(pdata
);
291 adev
->driver_data
= pdata
;
292 ret
= acpi_create_platform_device(adev
, id
);
296 adev
->driver_data
= NULL
;
303 static int lpss_reg_read(struct device
*dev
, unsigned int reg
, u32
*val
)
305 struct acpi_device
*adev
;
306 struct lpss_private_data
*pdata
;
310 ret
= acpi_bus_get_device(ACPI_HANDLE(dev
), &adev
);
314 spin_lock_irqsave(&dev
->power
.lock
, flags
);
315 if (pm_runtime_suspended(dev
)) {
319 pdata
= acpi_driver_data(adev
);
320 if (WARN_ON(!pdata
|| !pdata
->mmio_base
)) {
324 *val
= readl(pdata
->mmio_base
+ pdata
->dev_desc
->prv_offset
+ reg
);
327 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
331 static ssize_t
lpss_ltr_show(struct device
*dev
, struct device_attribute
*attr
,
338 reg
= strcmp(attr
->attr
.name
, "auto_ltr") ? LPSS_SW_LTR
: LPSS_AUTO_LTR
;
339 ret
= lpss_reg_read(dev
, reg
, <r_value
);
343 return snprintf(buf
, PAGE_SIZE
, "%08x\n", ltr_value
);
346 static ssize_t
lpss_ltr_mode_show(struct device
*dev
,
347 struct device_attribute
*attr
, char *buf
)
353 ret
= lpss_reg_read(dev
, LPSS_GENERAL
, <r_mode
);
357 outstr
= (ltr_mode
& LPSS_GENERAL_LTR_MODE_SW
) ? "sw" : "auto";
358 return sprintf(buf
, "%s\n", outstr
);
361 static DEVICE_ATTR(auto_ltr
, S_IRUSR
, lpss_ltr_show
, NULL
);
362 static DEVICE_ATTR(sw_ltr
, S_IRUSR
, lpss_ltr_show
, NULL
);
363 static DEVICE_ATTR(ltr_mode
, S_IRUSR
, lpss_ltr_mode_show
, NULL
);
365 static struct attribute
*lpss_attrs
[] = {
366 &dev_attr_auto_ltr
.attr
,
367 &dev_attr_sw_ltr
.attr
,
368 &dev_attr_ltr_mode
.attr
,
372 static struct attribute_group lpss_attr_group
= {
377 static int acpi_lpss_platform_notify(struct notifier_block
*nb
,
378 unsigned long action
, void *data
)
380 struct platform_device
*pdev
= to_platform_device(data
);
381 struct lpss_private_data
*pdata
;
382 struct acpi_device
*adev
;
383 const struct acpi_device_id
*id
;
386 id
= acpi_match_device(acpi_lpss_device_ids
, &pdev
->dev
);
387 if (!id
|| !id
->driver_data
)
390 if (acpi_bus_get_device(ACPI_HANDLE(&pdev
->dev
), &adev
))
393 pdata
= acpi_driver_data(adev
);
394 if (!pdata
|| !pdata
->mmio_base
|| !pdata
->dev_desc
->ltr_required
)
397 if (pdata
->mmio_size
< pdata
->dev_desc
->prv_offset
+ LPSS_LTR_SIZE
) {
398 dev_err(&pdev
->dev
, "MMIO size insufficient to access LTR\n");
402 if (action
== BUS_NOTIFY_ADD_DEVICE
)
403 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &lpss_attr_group
);
404 else if (action
== BUS_NOTIFY_DEL_DEVICE
)
405 sysfs_remove_group(&pdev
->dev
.kobj
, &lpss_attr_group
);
410 static struct notifier_block acpi_lpss_nb
= {
411 .notifier_call
= acpi_lpss_platform_notify
,
414 static struct acpi_scan_handler lpss_handler
= {
415 .ids
= acpi_lpss_device_ids
,
416 .attach
= acpi_lpss_create_device
,
419 void __init
acpi_lpss_init(void)
421 if (!lpt_clk_init()) {
422 bus_register_notifier(&platform_bus_type
, &acpi_lpss_nb
);
423 acpi_scan_add_handler(&lpss_handler
);