blob: d1c9b04e29a3f93953cc981427dc0c7878785c91 [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>
Heikki Krogerusc78b0832014-05-23 16:15:09 +030022#include <linux/delay.h>
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010023
24#include "internal.h"
25
26ACPI_MODULE_NAME("acpi_lpss");
27
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010028#define LPSS_CLK_SIZE 0x04
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010029#define LPSS_LTR_SIZE 0x18
30
31/* Offsets relative to LPSS_PRIVATE_OFFSET */
Heikki Krogerused3a8722014-05-19 14:42:07 +030032#define LPSS_CLK_DIVIDER_DEF_MASK (BIT(1) | BIT(16))
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010033#define LPSS_GENERAL 0x08
34#define LPSS_GENERAL_LTR_MODE_SW BIT(2)
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030035#define LPSS_GENERAL_UART_RTS_OVRD BIT(3)
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010036#define LPSS_SW_LTR 0x10
37#define LPSS_AUTO_LTR 0x14
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +010038#define LPSS_LTR_SNOOP_REQ BIT(15)
39#define LPSS_LTR_SNOOP_MASK 0x0000FFFF
40#define LPSS_LTR_SNOOP_LAT_1US 0x800
41#define LPSS_LTR_SNOOP_LAT_32US 0xC00
42#define LPSS_LTR_SNOOP_LAT_SHIFT 5
43#define LPSS_LTR_SNOOP_LAT_CUTOFF 3000
44#define LPSS_LTR_MAX_VAL 0x3FF
Heikki Krogerus06d86412013-06-17 13:25:46 +030045#define LPSS_TX_INT 0x20
46#define LPSS_TX_INT_MASK BIT(1)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010047
Heikki Krogerusc78b0832014-05-23 16:15:09 +030048#define LPSS_PRV_REG_COUNT 9
49
Mika Westerbergf6272172013-05-13 12:42:44 +000050struct lpss_shared_clock {
51 const char *name;
52 unsigned long rate;
53 struct clk *clk;
54};
55
Heikki Krogerus06d86412013-06-17 13:25:46 +030056struct lpss_private_data;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010057
58struct lpss_device_desc {
59 bool clk_required;
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030060 const char *clkdev_name;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010061 bool ltr_required;
62 unsigned int prv_offset;
Mika Westerberg958c4eb2013-06-18 16:51:35 +030063 size_t prv_size_override;
Heikki Krogerused3a8722014-05-19 14:42:07 +030064 bool clk_divider;
Mika Westerbergf6272172013-05-13 12:42:44 +000065 bool clk_gate;
Heikki Krogerusc78b0832014-05-23 16:15:09 +030066 bool save_ctx;
Mika Westerbergf6272172013-05-13 12:42:44 +000067 struct lpss_shared_clock *shared_clock;
Heikki Krogerus06d86412013-06-17 13:25:46 +030068 void (*setup)(struct lpss_private_data *pdata);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010069};
70
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030071static struct lpss_device_desc lpss_dma_desc = {
72 .clk_required = true,
73 .clkdev_name = "hclk",
74};
75
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010076struct lpss_private_data {
77 void __iomem *mmio_base;
78 resource_size_t mmio_size;
79 struct clk *clk;
80 const struct lpss_device_desc *dev_desc;
Heikki Krogerusc78b0832014-05-23 16:15:09 +030081 u32 prv_reg_ctx[LPSS_PRV_REG_COUNT];
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010082};
83
Heikki Krogerus06d86412013-06-17 13:25:46 +030084static void lpss_uart_setup(struct lpss_private_data *pdata)
85{
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030086 unsigned int offset;
Heikki Krogerus06d86412013-06-17 13:25:46 +030087 u32 reg;
88
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030089 offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
90 reg = readl(pdata->mmio_base + offset);
91 writel(reg | LPSS_TX_INT_MASK, pdata->mmio_base + offset);
92
93 offset = pdata->dev_desc->prv_offset + LPSS_GENERAL;
94 reg = readl(pdata->mmio_base + offset);
95 writel(reg | LPSS_GENERAL_UART_RTS_OVRD, pdata->mmio_base + offset);
Heikki Krogerus06d86412013-06-17 13:25:46 +030096}
97
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010098static struct lpss_device_desc lpt_dev_desc = {
99 .clk_required = true,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100100 .prv_offset = 0x800,
101 .ltr_required = true,
Heikki Krogerused3a8722014-05-19 14:42:07 +0300102 .clk_divider = true,
103 .clk_gate = true,
104};
105
106static struct lpss_device_desc lpt_i2c_dev_desc = {
107 .clk_required = true,
108 .prv_offset = 0x800,
109 .ltr_required = true,
Mika Westerbergf6272172013-05-13 12:42:44 +0000110 .clk_gate = true,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100111};
112
Heikki Krogerus06d86412013-06-17 13:25:46 +0300113static struct lpss_device_desc lpt_uart_dev_desc = {
114 .clk_required = true,
115 .prv_offset = 0x800,
116 .ltr_required = true,
Heikki Krogerused3a8722014-05-19 14:42:07 +0300117 .clk_divider = true,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300118 .clk_gate = true,
119 .setup = lpss_uart_setup,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100120};
121
122static struct lpss_device_desc lpt_sdio_dev_desc = {
123 .prv_offset = 0x1000,
Mika Westerberg958c4eb2013-06-18 16:51:35 +0300124 .prv_size_override = 0x1018,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100125 .ltr_required = true,
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100126};
127
Chew, Chiau Eee1c74812014-02-19 02:24:29 +0800128static struct lpss_shared_clock pwm_clock = {
129 .name = "pwm_clk",
130 .rate = 25000000,
131};
132
133static struct lpss_device_desc byt_pwm_dev_desc = {
134 .clk_required = true,
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300135 .save_ctx = true,
Chew, Chiau Eee1c74812014-02-19 02:24:29 +0800136 .shared_clock = &pwm_clock,
137};
138
Mika Westerbergf6272172013-05-13 12:42:44 +0000139static struct lpss_device_desc byt_uart_dev_desc = {
140 .clk_required = true,
141 .prv_offset = 0x800,
Heikki Krogerused3a8722014-05-19 14:42:07 +0300142 .clk_divider = true,
Mika Westerbergf6272172013-05-13 12:42:44 +0000143 .clk_gate = true,
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300144 .save_ctx = true,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300145 .setup = lpss_uart_setup,
Mika Westerbergf6272172013-05-13 12:42:44 +0000146};
147
Mika Westerbergf6272172013-05-13 12:42:44 +0000148static struct lpss_device_desc byt_spi_dev_desc = {
149 .clk_required = true,
150 .prv_offset = 0x400,
Heikki Krogerused3a8722014-05-19 14:42:07 +0300151 .clk_divider = true,
Mika Westerbergf6272172013-05-13 12:42:44 +0000152 .clk_gate = true,
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300153 .save_ctx = true,
Mika Westerbergf6272172013-05-13 12:42:44 +0000154};
155
156static struct lpss_device_desc byt_sdio_dev_desc = {
157 .clk_required = true,
158};
159
160static struct lpss_shared_clock i2c_clock = {
161 .name = "i2c_clk",
162 .rate = 100000000,
163};
164
165static struct lpss_device_desc byt_i2c_dev_desc = {
166 .clk_required = true,
167 .prv_offset = 0x800,
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300168 .save_ctx = true,
Mika Westerbergf6272172013-05-13 12:42:44 +0000169 .shared_clock = &i2c_clock,
170};
171
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100172static const struct acpi_device_id acpi_lpss_device_ids[] = {
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300173 /* Generic LPSS devices */
174 { "INTL9C60", (unsigned long)&lpss_dma_desc },
175
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100176 /* Lynxpoint LPSS devices */
177 { "INT33C0", (unsigned long)&lpt_dev_desc },
178 { "INT33C1", (unsigned long)&lpt_dev_desc },
Heikki Krogerused3a8722014-05-19 14:42:07 +0300179 { "INT33C2", (unsigned long)&lpt_i2c_dev_desc },
180 { "INT33C3", (unsigned long)&lpt_i2c_dev_desc },
Heikki Krogerus06d86412013-06-17 13:25:46 +0300181 { "INT33C4", (unsigned long)&lpt_uart_dev_desc },
182 { "INT33C5", (unsigned long)&lpt_uart_dev_desc },
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100183 { "INT33C6", (unsigned long)&lpt_sdio_dev_desc },
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100184 { "INT33C7", },
185
Mika Westerbergf6272172013-05-13 12:42:44 +0000186 /* BayTrail LPSS devices */
Chew, Chiau Eee1c74812014-02-19 02:24:29 +0800187 { "80860F09", (unsigned long)&byt_pwm_dev_desc },
Mika Westerbergf6272172013-05-13 12:42:44 +0000188 { "80860F0A", (unsigned long)&byt_uart_dev_desc },
189 { "80860F0E", (unsigned long)&byt_spi_dev_desc },
190 { "80860F14", (unsigned long)&byt_sdio_dev_desc },
191 { "80860F41", (unsigned long)&byt_i2c_dev_desc },
192 { "INT33B2", },
193
Mika Westerberga4d97532013-11-12 11:48:19 +0200194 { "INT3430", (unsigned long)&lpt_dev_desc },
195 { "INT3431", (unsigned long)&lpt_dev_desc },
Heikki Krogerused3a8722014-05-19 14:42:07 +0300196 { "INT3432", (unsigned long)&lpt_i2c_dev_desc },
197 { "INT3433", (unsigned long)&lpt_i2c_dev_desc },
Mika Westerberga4d97532013-11-12 11:48:19 +0200198 { "INT3434", (unsigned long)&lpt_uart_dev_desc },
199 { "INT3435", (unsigned long)&lpt_uart_dev_desc },
200 { "INT3436", (unsigned long)&lpt_sdio_dev_desc },
201 { "INT3437", },
202
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100203 { }
204};
205
206static int is_memory(struct acpi_resource *res, void *not_used)
207{
208 struct resource r;
209 return !acpi_dev_resource_memory(res, &r);
210}
211
212/* LPSS main clock device. */
213static struct platform_device *lpss_clk_dev;
214
215static inline void lpt_register_clock_device(void)
216{
217 lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
218}
219
220static int register_device_clock(struct acpi_device *adev,
221 struct lpss_private_data *pdata)
222{
223 const struct lpss_device_desc *dev_desc = pdata->dev_desc;
Mika Westerbergf6272172013-05-13 12:42:44 +0000224 struct lpss_shared_clock *shared_clock = dev_desc->shared_clock;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300225 const char *devname = dev_name(&adev->dev);
Mika Westerbergf6272172013-05-13 12:42:44 +0000226 struct clk *clk = ERR_PTR(-ENODEV);
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300227 struct lpss_clk_data *clk_data;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300228 const char *parent, *clk_name;
229 void __iomem *prv_base;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100230
231 if (!lpss_clk_dev)
232 lpt_register_clock_device();
233
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300234 clk_data = platform_get_drvdata(lpss_clk_dev);
235 if (!clk_data)
236 return -ENODEV;
237
238 if (dev_desc->clkdev_name) {
239 clk_register_clkdev(clk_data->clk, dev_desc->clkdev_name,
Heikki Krogerused3a8722014-05-19 14:42:07 +0300240 devname);
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300241 return 0;
242 }
243
244 if (!pdata->mmio_base
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100245 || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100246 return -ENODATA;
247
Mika Westerbergf6272172013-05-13 12:42:44 +0000248 parent = clk_data->name;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300249 prv_base = pdata->mmio_base + dev_desc->prv_offset;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100250
Mika Westerbergf6272172013-05-13 12:42:44 +0000251 if (shared_clock) {
252 clk = shared_clock->clk;
253 if (!clk) {
254 clk = clk_register_fixed_rate(NULL, shared_clock->name,
255 "lpss_clk", 0,
256 shared_clock->rate);
257 shared_clock->clk = clk;
258 }
259 parent = shared_clock->name;
260 }
261
262 if (dev_desc->clk_gate) {
Heikki Krogerused3a8722014-05-19 14:42:07 +0300263 clk = clk_register_gate(NULL, devname, parent, 0,
264 prv_base, 0, 0, NULL);
265 parent = devname;
266 }
267
268 if (dev_desc->clk_divider) {
269 /* Prevent division by zero */
270 if (!readl(prv_base))
271 writel(LPSS_CLK_DIVIDER_DEF_MASK, prv_base);
272
273 clk_name = kasprintf(GFP_KERNEL, "%s-div", devname);
274 if (!clk_name)
275 return -ENOMEM;
276 clk = clk_register_fractional_divider(NULL, clk_name, parent,
277 0, prv_base,
278 1, 15, 16, 15, 0, NULL);
279 parent = clk_name;
280
281 clk_name = kasprintf(GFP_KERNEL, "%s-update", devname);
282 if (!clk_name) {
283 kfree(parent);
284 return -ENOMEM;
285 }
286 clk = clk_register_gate(NULL, clk_name, parent,
287 CLK_SET_RATE_PARENT | CLK_SET_RATE_GATE,
288 prv_base, 31, 0, NULL);
289 kfree(parent);
290 kfree(clk_name);
Mika Westerbergf6272172013-05-13 12:42:44 +0000291 }
292
293 if (IS_ERR(clk))
294 return PTR_ERR(clk);
295
Heikki Krogerused3a8722014-05-19 14:42:07 +0300296 pdata->clk = clk;
297 clk_register_clkdev(clk, NULL, devname);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100298 return 0;
299}
300
301static int acpi_lpss_create_device(struct acpi_device *adev,
302 const struct acpi_device_id *id)
303{
304 struct lpss_device_desc *dev_desc;
305 struct lpss_private_data *pdata;
306 struct resource_list_entry *rentry;
307 struct list_head resource_list;
308 int ret;
309
310 dev_desc = (struct lpss_device_desc *)id->driver_data;
311 if (!dev_desc)
312 return acpi_create_platform_device(adev, id);
313
314 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
315 if (!pdata)
316 return -ENOMEM;
317
318 INIT_LIST_HEAD(&resource_list);
319 ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
320 if (ret < 0)
321 goto err_out;
322
323 list_for_each_entry(rentry, &resource_list, node)
324 if (resource_type(&rentry->res) == IORESOURCE_MEM) {
Mika Westerberg958c4eb2013-06-18 16:51:35 +0300325 if (dev_desc->prv_size_override)
326 pdata->mmio_size = dev_desc->prv_size_override;
327 else
328 pdata->mmio_size = resource_size(&rentry->res);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100329 pdata->mmio_base = ioremap(rentry->res.start,
330 pdata->mmio_size);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100331 break;
332 }
333
334 acpi_dev_free_resource_list(&resource_list);
335
Mika Westerbergaf65cfe2013-09-02 13:30:25 +0300336 pdata->dev_desc = dev_desc;
337
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100338 if (dev_desc->clk_required) {
339 ret = register_device_clock(adev, pdata);
340 if (ret) {
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200341 /* Skip the device, but continue the namespace scan. */
342 ret = 0;
343 goto err_out;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100344 }
345 }
346
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200347 /*
348 * This works around a known issue in ACPI tables where LPSS devices
349 * have _PS0 and _PS3 without _PSC (and no power resources), so
350 * acpi_bus_init_power() will assume that the BIOS has put them into D0.
351 */
352 ret = acpi_device_fix_up_power(adev);
353 if (ret) {
354 /* Skip the device, but continue the namespace scan. */
355 ret = 0;
356 goto err_out;
357 }
358
Heikki Krogerus06d86412013-06-17 13:25:46 +0300359 if (dev_desc->setup)
360 dev_desc->setup(pdata);
361
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100362 adev->driver_data = pdata;
363 ret = acpi_create_platform_device(adev, id);
364 if (ret > 0)
365 return ret;
366
367 adev->driver_data = NULL;
368
369 err_out:
370 kfree(pdata);
371 return ret;
372}
373
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100374static u32 __lpss_reg_read(struct lpss_private_data *pdata, unsigned int reg)
375{
376 return readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
377}
378
379static void __lpss_reg_write(u32 val, struct lpss_private_data *pdata,
380 unsigned int reg)
381{
382 writel(val, pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
383}
384
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100385static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
386{
387 struct acpi_device *adev;
388 struct lpss_private_data *pdata;
389 unsigned long flags;
390 int ret;
391
392 ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
393 if (WARN_ON(ret))
394 return ret;
395
396 spin_lock_irqsave(&dev->power.lock, flags);
397 if (pm_runtime_suspended(dev)) {
398 ret = -EAGAIN;
399 goto out;
400 }
401 pdata = acpi_driver_data(adev);
402 if (WARN_ON(!pdata || !pdata->mmio_base)) {
403 ret = -ENODEV;
404 goto out;
405 }
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100406 *val = __lpss_reg_read(pdata, reg);
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100407
408 out:
409 spin_unlock_irqrestore(&dev->power.lock, flags);
410 return ret;
411}
412
413static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
414 char *buf)
415{
416 u32 ltr_value = 0;
417 unsigned int reg;
418 int ret;
419
420 reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
421 ret = lpss_reg_read(dev, reg, &ltr_value);
422 if (ret)
423 return ret;
424
425 return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
426}
427
428static ssize_t lpss_ltr_mode_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
430{
431 u32 ltr_mode = 0;
432 char *outstr;
433 int ret;
434
435 ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
436 if (ret)
437 return ret;
438
439 outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
440 return sprintf(buf, "%s\n", outstr);
441}
442
443static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
444static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
445static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
446
447static struct attribute *lpss_attrs[] = {
448 &dev_attr_auto_ltr.attr,
449 &dev_attr_sw_ltr.attr,
450 &dev_attr_ltr_mode.attr,
451 NULL,
452};
453
454static struct attribute_group lpss_attr_group = {
455 .attrs = lpss_attrs,
456 .name = "lpss_ltr",
457};
458
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100459static void acpi_lpss_set_ltr(struct device *dev, s32 val)
460{
461 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
462 u32 ltr_mode, ltr_val;
463
464 ltr_mode = __lpss_reg_read(pdata, LPSS_GENERAL);
465 if (val < 0) {
466 if (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) {
467 ltr_mode &= ~LPSS_GENERAL_LTR_MODE_SW;
468 __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
469 }
470 return;
471 }
472 ltr_val = __lpss_reg_read(pdata, LPSS_SW_LTR) & ~LPSS_LTR_SNOOP_MASK;
473 if (val >= LPSS_LTR_SNOOP_LAT_CUTOFF) {
474 ltr_val |= LPSS_LTR_SNOOP_LAT_32US;
475 val = LPSS_LTR_MAX_VAL;
476 } else if (val > LPSS_LTR_MAX_VAL) {
477 ltr_val |= LPSS_LTR_SNOOP_LAT_32US | LPSS_LTR_SNOOP_REQ;
478 val >>= LPSS_LTR_SNOOP_LAT_SHIFT;
479 } else {
480 ltr_val |= LPSS_LTR_SNOOP_LAT_1US | LPSS_LTR_SNOOP_REQ;
481 }
482 ltr_val |= val;
483 __lpss_reg_write(ltr_val, pdata, LPSS_SW_LTR);
484 if (!(ltr_mode & LPSS_GENERAL_LTR_MODE_SW)) {
485 ltr_mode |= LPSS_GENERAL_LTR_MODE_SW;
486 __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
487 }
488}
489
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300490#ifdef CONFIG_PM
491/**
492 * acpi_lpss_save_ctx() - Save the private registers of LPSS device
493 * @dev: LPSS device
494 *
495 * Most LPSS devices have private registers which may loose their context when
496 * the device is powered down. acpi_lpss_save_ctx() saves those registers into
497 * prv_reg_ctx array.
498 */
499static void acpi_lpss_save_ctx(struct device *dev)
500{
501 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
502 unsigned int i;
503
504 for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
505 unsigned long offset = i * sizeof(u32);
506
507 pdata->prv_reg_ctx[i] = __lpss_reg_read(pdata, offset);
508 dev_dbg(dev, "saving 0x%08x from LPSS reg at offset 0x%02lx\n",
509 pdata->prv_reg_ctx[i], offset);
510 }
511}
512
513/**
514 * acpi_lpss_restore_ctx() - Restore the private registers of LPSS device
515 * @dev: LPSS device
516 *
517 * Restores the registers that were previously stored with acpi_lpss_save_ctx().
518 */
519static void acpi_lpss_restore_ctx(struct device *dev)
520{
521 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
522 unsigned int i;
523
524 /*
525 * The following delay is needed or the subsequent write operations may
526 * fail. The LPSS devices are actually PCI devices and the PCI spec
527 * expects 10ms delay before the device can be accessed after D3 to D0
528 * transition.
529 */
530 msleep(10);
531
532 for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
533 unsigned long offset = i * sizeof(u32);
534
535 __lpss_reg_write(pdata->prv_reg_ctx[i], pdata, offset);
536 dev_dbg(dev, "restoring 0x%08x to LPSS reg at offset 0x%02lx\n",
537 pdata->prv_reg_ctx[i], offset);
538 }
539}
540
541#ifdef CONFIG_PM_SLEEP
542static int acpi_lpss_suspend_late(struct device *dev)
543{
544 int ret = pm_generic_suspend_late(dev);
545
546 if (ret)
547 return ret;
548
549 acpi_lpss_save_ctx(dev);
550 return acpi_dev_suspend_late(dev);
551}
552
553static int acpi_lpss_restore_early(struct device *dev)
554{
555 int ret = acpi_dev_resume_early(dev);
556
557 if (ret)
558 return ret;
559
560 acpi_lpss_restore_ctx(dev);
561 return pm_generic_resume_early(dev);
562}
563#endif /* CONFIG_PM_SLEEP */
564
565#ifdef CONFIG_PM_RUNTIME
566static int acpi_lpss_runtime_suspend(struct device *dev)
567{
568 int ret = pm_generic_runtime_suspend(dev);
569
570 if (ret)
571 return ret;
572
573 acpi_lpss_save_ctx(dev);
574 return acpi_dev_runtime_suspend(dev);
575}
576
577static int acpi_lpss_runtime_resume(struct device *dev)
578{
579 int ret = acpi_dev_runtime_resume(dev);
580
581 if (ret)
582 return ret;
583
584 acpi_lpss_restore_ctx(dev);
585 return pm_generic_runtime_resume(dev);
586}
587#endif /* CONFIG_PM_RUNTIME */
588#endif /* CONFIG_PM */
589
590static struct dev_pm_domain acpi_lpss_pm_domain = {
591 .ops = {
592#ifdef CONFIG_PM_SLEEP
593 .suspend_late = acpi_lpss_suspend_late,
594 .restore_early = acpi_lpss_restore_early,
595 .prepare = acpi_subsys_prepare,
596 .complete = acpi_subsys_complete,
597 .suspend = acpi_subsys_suspend,
598 .resume_early = acpi_subsys_resume_early,
599 .freeze = acpi_subsys_freeze,
600 .poweroff = acpi_subsys_suspend,
601 .poweroff_late = acpi_subsys_suspend_late,
602#endif
603#ifdef CONFIG_PM_RUNTIME
604 .runtime_suspend = acpi_lpss_runtime_suspend,
605 .runtime_resume = acpi_lpss_runtime_resume,
606#endif
607 },
608};
609
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100610static int acpi_lpss_platform_notify(struct notifier_block *nb,
611 unsigned long action, void *data)
612{
613 struct platform_device *pdev = to_platform_device(data);
614 struct lpss_private_data *pdata;
615 struct acpi_device *adev;
616 const struct acpi_device_id *id;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100617
618 id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
619 if (!id || !id->driver_data)
620 return 0;
621
622 if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
623 return 0;
624
625 pdata = acpi_driver_data(adev);
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300626 if (!pdata || !pdata->mmio_base)
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100627 return 0;
628
629 if (pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
630 dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
631 return 0;
632 }
633
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300634 switch (action) {
635 case BUS_NOTIFY_BOUND_DRIVER:
636 if (pdata->dev_desc->save_ctx)
637 pdev->dev.pm_domain = &acpi_lpss_pm_domain;
638 break;
639 case BUS_NOTIFY_UNBOUND_DRIVER:
640 if (pdata->dev_desc->save_ctx)
641 pdev->dev.pm_domain = NULL;
642 break;
643 case BUS_NOTIFY_ADD_DEVICE:
644 if (pdata->dev_desc->ltr_required)
645 return sysfs_create_group(&pdev->dev.kobj,
646 &lpss_attr_group);
647 case BUS_NOTIFY_DEL_DEVICE:
648 if (pdata->dev_desc->ltr_required)
649 sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
650 default:
651 break;
652 }
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100653
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300654 return 0;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100655}
656
657static struct notifier_block acpi_lpss_nb = {
658 .notifier_call = acpi_lpss_platform_notify,
659};
660
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100661static void acpi_lpss_bind(struct device *dev)
662{
663 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
664
665 if (!pdata || !pdata->mmio_base || !pdata->dev_desc->ltr_required)
666 return;
667
668 if (pdata->mmio_size >= pdata->dev_desc->prv_offset + LPSS_LTR_SIZE)
669 dev->power.set_latency_tolerance = acpi_lpss_set_ltr;
670 else
671 dev_err(dev, "MMIO size insufficient to access LTR\n");
672}
673
674static void acpi_lpss_unbind(struct device *dev)
675{
676 dev->power.set_latency_tolerance = NULL;
677}
678
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100679static struct acpi_scan_handler lpss_handler = {
680 .ids = acpi_lpss_device_ids,
681 .attach = acpi_lpss_create_device,
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100682 .bind = acpi_lpss_bind,
683 .unbind = acpi_lpss_unbind,
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100684};
685
686void __init acpi_lpss_init(void)
687{
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100688 if (!lpt_clk_init()) {
689 bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100690 acpi_scan_add_handler(&lpss_handler);
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100691 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100692}