blob: 373657f7e35a9cac3a2a951cd030c00ef6fbd06d [file] [log] [blame]
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01001/*
2 * ACPI support for Intel Lynxpoint LPSS.
3 *
Rafael J. Wysocki3df2da92015-02-03 14:29:43 +01004 * Copyright (C) 2013, Intel Corporation
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01005 * 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>
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010014#include <linux/clkdev.h>
15#include <linux/clk-provider.h>
16#include <linux/err.h>
17#include <linux/io.h>
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +010018#include <linux/mutex.h>
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010019#include <linux/platform_device.h>
20#include <linux/platform_data/clk-lpss.h>
Tomeu Vizoso989561d2016-01-07 16:46:13 +010021#include <linux/pm_domain.h>
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010022#include <linux/pm_runtime.h>
Heikki Krogerusc78b0832014-05-23 16:15:09 +030023#include <linux/delay.h>
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010024
25#include "internal.h"
26
27ACPI_MODULE_NAME("acpi_lpss");
28
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +020029#ifdef CONFIG_X86_INTEL_LPSS
30
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +010031#include <asm/cpu_device_id.h>
Dave Hansen4626d842016-06-02 17:19:46 -070032#include <asm/intel-family.h>
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +010033#include <asm/iosf_mbi.h>
34#include <asm/pmc_atom.h>
35
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +020036#define LPSS_ADDR(desc) ((unsigned long)&desc)
37
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010038#define LPSS_CLK_SIZE 0x04
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010039#define LPSS_LTR_SIZE 0x18
40
41/* Offsets relative to LPSS_PRIVATE_OFFSET */
Heikki Krogerused3a8722014-05-19 14:42:07 +030042#define LPSS_CLK_DIVIDER_DEF_MASK (BIT(1) | BIT(16))
Mika Westerberg765bdd42014-06-17 14:33:39 +030043#define LPSS_RESETS 0x04
44#define LPSS_RESETS_RESET_FUNC BIT(0)
45#define LPSS_RESETS_RESET_APB BIT(1)
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010046#define LPSS_GENERAL 0x08
47#define LPSS_GENERAL_LTR_MODE_SW BIT(2)
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030048#define LPSS_GENERAL_UART_RTS_OVRD BIT(3)
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010049#define LPSS_SW_LTR 0x10
50#define LPSS_AUTO_LTR 0x14
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +010051#define LPSS_LTR_SNOOP_REQ BIT(15)
52#define LPSS_LTR_SNOOP_MASK 0x0000FFFF
53#define LPSS_LTR_SNOOP_LAT_1US 0x800
54#define LPSS_LTR_SNOOP_LAT_32US 0xC00
55#define LPSS_LTR_SNOOP_LAT_SHIFT 5
56#define LPSS_LTR_SNOOP_LAT_CUTOFF 3000
57#define LPSS_LTR_MAX_VAL 0x3FF
Heikki Krogerus06d86412013-06-17 13:25:46 +030058#define LPSS_TX_INT 0x20
59#define LPSS_TX_INT_MASK BIT(1)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010060
Heikki Krogerusc78b0832014-05-23 16:15:09 +030061#define LPSS_PRV_REG_COUNT 9
62
Heikki Krogerusff8c1af2014-09-02 10:55:07 +030063/* LPSS Flags */
64#define LPSS_CLK BIT(0)
65#define LPSS_CLK_GATE BIT(1)
66#define LPSS_CLK_DIVIDER BIT(2)
67#define LPSS_LTR BIT(3)
68#define LPSS_SAVE_CTX BIT(4)
Srinidhi Kasagarb00855a2015-08-27 21:30:55 +053069#define LPSS_NO_D3_DELAY BIT(5)
Mika Westerbergf6272172013-05-13 12:42:44 +000070
Heikki Krogerus06d86412013-06-17 13:25:46 +030071struct lpss_private_data;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010072
73struct lpss_device_desc {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +030074 unsigned int flags;
Heikki Krogerusfcf07892015-03-06 15:48:38 +020075 const char *clk_con_id;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010076 unsigned int prv_offset;
Mika Westerberg958c4eb2013-06-18 16:51:35 +030077 size_t prv_size_override;
Heikki Krogerusa5565cf2016-08-23 11:33:27 +030078 struct property_entry *properties;
Heikki Krogerus06d86412013-06-17 13:25:46 +030079 void (*setup)(struct lpss_private_data *pdata);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010080};
81
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +010082static const struct lpss_device_desc lpss_dma_desc = {
Rafael J. Wysocki3df2da92015-02-03 14:29:43 +010083 .flags = LPSS_CLK,
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030084};
85
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010086struct lpss_private_data {
87 void __iomem *mmio_base;
88 resource_size_t mmio_size;
Heikki Krogerus03f09f72014-09-02 10:55:09 +030089 unsigned int fixed_clk_rate;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010090 struct clk *clk;
91 const struct lpss_device_desc *dev_desc;
Heikki Krogerusc78b0832014-05-23 16:15:09 +030092 u32 prv_reg_ctx[LPSS_PRV_REG_COUNT];
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010093};
94
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +010095/* LPSS run time quirks */
96static unsigned int lpss_quirks;
97
98/*
99 * LPSS_QUIRK_ALWAYS_POWER_ON: override power state for LPSS DMA device.
100 *
Andy Shevchenkofa9e93b2015-12-21 22:31:09 +0200101 * The LPSS DMA controller has neither _PS0 nor _PS3 method. Moreover
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +0100102 * it can be powered off automatically whenever the last LPSS device goes down.
103 * In case of no power any access to the DMA controller will hang the system.
104 * The behaviour is reproduced on some HP laptops based on Intel BayTrail as
105 * well as on ASuS T100TA transformer.
106 *
107 * This quirk overrides power state of entire LPSS island to keep DMA powered
108 * on whenever we have at least one other device in use.
109 */
110#define LPSS_QUIRK_ALWAYS_POWER_ON BIT(0)
111
Heikki Krogerus1f47a772014-09-11 15:19:33 +0300112/* UART Component Parameter Register */
113#define LPSS_UART_CPR 0xF4
114#define LPSS_UART_CPR_AFCE BIT(4)
115
Heikki Krogerus06d86412013-06-17 13:25:46 +0300116static void lpss_uart_setup(struct lpss_private_data *pdata)
117{
Heikki Krogerus088f1fd2013-10-09 09:49:20 +0300118 unsigned int offset;
Heikki Krogerus1f47a772014-09-11 15:19:33 +0300119 u32 val;
Heikki Krogerus06d86412013-06-17 13:25:46 +0300120
Heikki Krogerus088f1fd2013-10-09 09:49:20 +0300121 offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
Heikki Krogerus1f47a772014-09-11 15:19:33 +0300122 val = readl(pdata->mmio_base + offset);
123 writel(val | LPSS_TX_INT_MASK, pdata->mmio_base + offset);
Heikki Krogerus088f1fd2013-10-09 09:49:20 +0300124
Heikki Krogerus1f47a772014-09-11 15:19:33 +0300125 val = readl(pdata->mmio_base + LPSS_UART_CPR);
126 if (!(val & LPSS_UART_CPR_AFCE)) {
127 offset = pdata->dev_desc->prv_offset + LPSS_GENERAL;
128 val = readl(pdata->mmio_base + offset);
129 val |= LPSS_GENERAL_UART_RTS_OVRD;
130 writel(val, pdata->mmio_base + offset);
131 }
Heikki Krogerus06d86412013-06-17 13:25:46 +0300132}
133
Mika Westerberg30957942015-02-18 13:50:17 +0200134static void lpss_deassert_reset(struct lpss_private_data *pdata)
Mika Westerberg765bdd42014-06-17 14:33:39 +0300135{
136 unsigned int offset;
137 u32 val;
138
139 offset = pdata->dev_desc->prv_offset + LPSS_RESETS;
140 val = readl(pdata->mmio_base + offset);
141 val |= LPSS_RESETS_RESET_APB | LPSS_RESETS_RESET_FUNC;
142 writel(val, pdata->mmio_base + offset);
Mika Westerberg30957942015-02-18 13:50:17 +0200143}
144
145#define LPSS_I2C_ENABLE 0x6c
146
147static void byt_i2c_setup(struct lpss_private_data *pdata)
148{
149 lpss_deassert_reset(pdata);
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300150
151 if (readl(pdata->mmio_base + pdata->dev_desc->prv_offset))
152 pdata->fixed_clk_rate = 133000000;
Mika Westerberg3293c7b2015-02-18 13:50:16 +0200153
154 writel(0, pdata->mmio_base + LPSS_I2C_ENABLE);
Mika Westerberg765bdd42014-06-17 14:33:39 +0300155}
156
Mathias Krauseb2687cd2015-06-13 14:26:50 +0200157static const struct lpss_device_desc lpt_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300158 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100159 .prv_offset = 0x800,
Heikki Krogerused3a8722014-05-19 14:42:07 +0300160};
161
Mathias Krauseb2687cd2015-06-13 14:26:50 +0200162static const struct lpss_device_desc lpt_i2c_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300163 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_LTR,
Heikki Krogerused3a8722014-05-19 14:42:07 +0300164 .prv_offset = 0x800,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100165};
166
Heikki Krogerusa5565cf2016-08-23 11:33:27 +0300167static struct property_entry uart_properties[] = {
168 PROPERTY_ENTRY_U32("reg-io-width", 4),
169 PROPERTY_ENTRY_U32("reg-shift", 2),
170 PROPERTY_ENTRY_BOOL("snps,uart-16550-compatible"),
171 { },
172};
173
Mathias Krauseb2687cd2015-06-13 14:26:50 +0200174static const struct lpss_device_desc lpt_uart_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300175 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR,
Heikki Krogerusfcf07892015-03-06 15:48:38 +0200176 .clk_con_id = "baudclk",
Heikki Krogerus06d86412013-06-17 13:25:46 +0300177 .prv_offset = 0x800,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300178 .setup = lpss_uart_setup,
Heikki Krogerusa5565cf2016-08-23 11:33:27 +0300179 .properties = uart_properties,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100180};
181
Mathias Krauseb2687cd2015-06-13 14:26:50 +0200182static const struct lpss_device_desc lpt_sdio_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300183 .flags = LPSS_LTR,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100184 .prv_offset = 0x1000,
Mika Westerberg958c4eb2013-06-18 16:51:35 +0300185 .prv_size_override = 0x1018,
Chew, Chiau Eee1c74812014-02-19 02:24:29 +0800186};
187
Mathias Krauseb2687cd2015-06-13 14:26:50 +0200188static const struct lpss_device_desc byt_pwm_dev_desc = {
Heikki Krogerus3f56bf32014-09-02 10:55:10 +0300189 .flags = LPSS_SAVE_CTX,
Chew, Chiau Eee1c74812014-02-19 02:24:29 +0800190};
191
Srinidhi Kasagarb00855a2015-08-27 21:30:55 +0530192static const struct lpss_device_desc bsw_pwm_dev_desc = {
193 .flags = LPSS_SAVE_CTX | LPSS_NO_D3_DELAY,
194};
195
Mathias Krauseb2687cd2015-06-13 14:26:50 +0200196static const struct lpss_device_desc byt_uart_dev_desc = {
Rafael J. Wysocki3df2da92015-02-03 14:29:43 +0100197 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX,
Heikki Krogerusfcf07892015-03-06 15:48:38 +0200198 .clk_con_id = "baudclk",
Mika Westerbergf6272172013-05-13 12:42:44 +0000199 .prv_offset = 0x800,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300200 .setup = lpss_uart_setup,
Heikki Krogerusa5565cf2016-08-23 11:33:27 +0300201 .properties = uart_properties,
Mika Westerbergf6272172013-05-13 12:42:44 +0000202};
203
Srinidhi Kasagarb00855a2015-08-27 21:30:55 +0530204static const struct lpss_device_desc bsw_uart_dev_desc = {
205 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX
206 | LPSS_NO_D3_DELAY,
207 .clk_con_id = "baudclk",
208 .prv_offset = 0x800,
209 .setup = lpss_uart_setup,
Heikki Krogerusa5565cf2016-08-23 11:33:27 +0300210 .properties = uart_properties,
Srinidhi Kasagarb00855a2015-08-27 21:30:55 +0530211};
212
Mathias Krauseb2687cd2015-06-13 14:26:50 +0200213static const struct lpss_device_desc byt_spi_dev_desc = {
Rafael J. Wysocki3df2da92015-02-03 14:29:43 +0100214 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX,
Mika Westerbergf6272172013-05-13 12:42:44 +0000215 .prv_offset = 0x400,
Mika Westerbergf6272172013-05-13 12:42:44 +0000216};
217
Mathias Krauseb2687cd2015-06-13 14:26:50 +0200218static const struct lpss_device_desc byt_sdio_dev_desc = {
Rafael J. Wysocki3df2da92015-02-03 14:29:43 +0100219 .flags = LPSS_CLK,
Mika Westerbergf6272172013-05-13 12:42:44 +0000220};
221
Mathias Krauseb2687cd2015-06-13 14:26:50 +0200222static const struct lpss_device_desc byt_i2c_dev_desc = {
Rafael J. Wysocki3df2da92015-02-03 14:29:43 +0100223 .flags = LPSS_CLK | LPSS_SAVE_CTX,
Mika Westerbergf6272172013-05-13 12:42:44 +0000224 .prv_offset = 0x800,
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300225 .setup = byt_i2c_setup,
Alan Cox1bfbd8e2014-08-19 15:55:22 +0300226};
227
Srinidhi Kasagarb00855a2015-08-27 21:30:55 +0530228static const struct lpss_device_desc bsw_i2c_dev_desc = {
229 .flags = LPSS_CLK | LPSS_SAVE_CTX | LPSS_NO_D3_DELAY,
230 .prv_offset = 0x800,
231 .setup = byt_i2c_setup,
232};
233
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +0100234static const struct lpss_device_desc bsw_spi_dev_desc = {
Srinidhi Kasagarb00855a2015-08-27 21:30:55 +0530235 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX
236 | LPSS_NO_D3_DELAY,
Mika Westerberg30957942015-02-18 13:50:17 +0200237 .prv_offset = 0x400,
238 .setup = lpss_deassert_reset,
239};
240
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +0100241#define ICPU(model) { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, }
242
243static const struct x86_cpu_id lpss_cpu_ids[] = {
Dave Hansen4626d842016-06-02 17:19:46 -0700244 ICPU(INTEL_FAM6_ATOM_SILVERMONT1), /* Valleyview, Bay Trail */
245 ICPU(INTEL_FAM6_ATOM_AIRMONT), /* Braswell, Cherry Trail */
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +0100246 {}
247};
248
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200249#else
250
251#define LPSS_ADDR(desc) (0UL)
252
253#endif /* CONFIG_X86_INTEL_LPSS */
254
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100255static const struct acpi_device_id acpi_lpss_device_ids[] = {
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300256 /* Generic LPSS devices */
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200257 { "INTL9C60", LPSS_ADDR(lpss_dma_desc) },
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300258
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100259 /* Lynxpoint LPSS devices */
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200260 { "INT33C0", LPSS_ADDR(lpt_dev_desc) },
261 { "INT33C1", LPSS_ADDR(lpt_dev_desc) },
262 { "INT33C2", LPSS_ADDR(lpt_i2c_dev_desc) },
263 { "INT33C3", LPSS_ADDR(lpt_i2c_dev_desc) },
264 { "INT33C4", LPSS_ADDR(lpt_uart_dev_desc) },
265 { "INT33C5", LPSS_ADDR(lpt_uart_dev_desc) },
266 { "INT33C6", LPSS_ADDR(lpt_sdio_dev_desc) },
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100267 { "INT33C7", },
268
Mika Westerbergf6272172013-05-13 12:42:44 +0000269 /* BayTrail LPSS devices */
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200270 { "80860F09", LPSS_ADDR(byt_pwm_dev_desc) },
271 { "80860F0A", LPSS_ADDR(byt_uart_dev_desc) },
272 { "80860F0E", LPSS_ADDR(byt_spi_dev_desc) },
273 { "80860F14", LPSS_ADDR(byt_sdio_dev_desc) },
274 { "80860F41", LPSS_ADDR(byt_i2c_dev_desc) },
Mika Westerbergf6272172013-05-13 12:42:44 +0000275 { "INT33B2", },
Jin Yao20482d32014-05-15 18:28:46 +0300276 { "INT33FC", },
Mika Westerbergf6272172013-05-13 12:42:44 +0000277
Alan Cox1bfbd8e2014-08-19 15:55:22 +0300278 /* Braswell LPSS devices */
Srinidhi Kasagarb00855a2015-08-27 21:30:55 +0530279 { "80862288", LPSS_ADDR(bsw_pwm_dev_desc) },
280 { "8086228A", LPSS_ADDR(bsw_uart_dev_desc) },
Mika Westerberg30957942015-02-18 13:50:17 +0200281 { "8086228E", LPSS_ADDR(bsw_spi_dev_desc) },
Srinidhi Kasagarb00855a2015-08-27 21:30:55 +0530282 { "808622C1", LPSS_ADDR(bsw_i2c_dev_desc) },
Alan Cox1bfbd8e2014-08-19 15:55:22 +0300283
Srinidhi Kasagarb00855a2015-08-27 21:30:55 +0530284 /* Broadwell LPSS devices */
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200285 { "INT3430", LPSS_ADDR(lpt_dev_desc) },
286 { "INT3431", LPSS_ADDR(lpt_dev_desc) },
287 { "INT3432", LPSS_ADDR(lpt_i2c_dev_desc) },
288 { "INT3433", LPSS_ADDR(lpt_i2c_dev_desc) },
289 { "INT3434", LPSS_ADDR(lpt_uart_dev_desc) },
290 { "INT3435", LPSS_ADDR(lpt_uart_dev_desc) },
291 { "INT3436", LPSS_ADDR(lpt_sdio_dev_desc) },
Mika Westerberga4d97532013-11-12 11:48:19 +0200292 { "INT3437", },
293
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300294 /* Wildcat Point LPSS devices */
295 { "INT3438", LPSS_ADDR(lpt_dev_desc) },
Jie Yang43218a12014-08-01 09:06:35 +0800296
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100297 { }
298};
299
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200300#ifdef CONFIG_X86_INTEL_LPSS
301
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100302static int is_memory(struct acpi_resource *res, void *not_used)
303{
304 struct resource r;
305 return !acpi_dev_resource_memory(res, &r);
306}
307
308/* LPSS main clock device. */
309static struct platform_device *lpss_clk_dev;
310
311static inline void lpt_register_clock_device(void)
312{
313 lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
314}
315
316static int register_device_clock(struct acpi_device *adev,
317 struct lpss_private_data *pdata)
318{
319 const struct lpss_device_desc *dev_desc = pdata->dev_desc;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300320 const char *devname = dev_name(&adev->dev);
Mika Westerbergf6272172013-05-13 12:42:44 +0000321 struct clk *clk = ERR_PTR(-ENODEV);
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300322 struct lpss_clk_data *clk_data;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300323 const char *parent, *clk_name;
324 void __iomem *prv_base;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100325
326 if (!lpss_clk_dev)
327 lpt_register_clock_device();
328
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300329 clk_data = platform_get_drvdata(lpss_clk_dev);
330 if (!clk_data)
331 return -ENODEV;
Heikki Krogerusb0d00f82014-09-02 10:55:08 +0300332 clk = clk_data->clk;
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300333
334 if (!pdata->mmio_base
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100335 || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100336 return -ENODATA;
337
Mika Westerbergf6272172013-05-13 12:42:44 +0000338 parent = clk_data->name;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300339 prv_base = pdata->mmio_base + dev_desc->prv_offset;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100340
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300341 if (pdata->fixed_clk_rate) {
342 clk = clk_register_fixed_rate(NULL, devname, parent, 0,
343 pdata->fixed_clk_rate);
344 goto out;
Mika Westerbergf6272172013-05-13 12:42:44 +0000345 }
346
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300347 if (dev_desc->flags & LPSS_CLK_GATE) {
Heikki Krogerused3a8722014-05-19 14:42:07 +0300348 clk = clk_register_gate(NULL, devname, parent, 0,
349 prv_base, 0, 0, NULL);
350 parent = devname;
351 }
352
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300353 if (dev_desc->flags & LPSS_CLK_DIVIDER) {
Heikki Krogerused3a8722014-05-19 14:42:07 +0300354 /* Prevent division by zero */
355 if (!readl(prv_base))
356 writel(LPSS_CLK_DIVIDER_DEF_MASK, prv_base);
357
358 clk_name = kasprintf(GFP_KERNEL, "%s-div", devname);
359 if (!clk_name)
360 return -ENOMEM;
361 clk = clk_register_fractional_divider(NULL, clk_name, parent,
362 0, prv_base,
363 1, 15, 16, 15, 0, NULL);
364 parent = clk_name;
365
366 clk_name = kasprintf(GFP_KERNEL, "%s-update", devname);
367 if (!clk_name) {
368 kfree(parent);
369 return -ENOMEM;
370 }
371 clk = clk_register_gate(NULL, clk_name, parent,
372 CLK_SET_RATE_PARENT | CLK_SET_RATE_GATE,
373 prv_base, 31, 0, NULL);
374 kfree(parent);
375 kfree(clk_name);
Mika Westerbergf6272172013-05-13 12:42:44 +0000376 }
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300377out:
Mika Westerbergf6272172013-05-13 12:42:44 +0000378 if (IS_ERR(clk))
379 return PTR_ERR(clk);
380
Heikki Krogerused3a8722014-05-19 14:42:07 +0300381 pdata->clk = clk;
Heikki Krogerusfcf07892015-03-06 15:48:38 +0200382 clk_register_clkdev(clk, dev_desc->clk_con_id, devname);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100383 return 0;
384}
385
386static int acpi_lpss_create_device(struct acpi_device *adev,
387 const struct acpi_device_id *id)
388{
Mathias Krauseb2687cd2015-06-13 14:26:50 +0200389 const struct lpss_device_desc *dev_desc;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100390 struct lpss_private_data *pdata;
Jiang Liu90e97822015-02-05 13:44:43 +0800391 struct resource_entry *rentry;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100392 struct list_head resource_list;
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200393 struct platform_device *pdev;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100394 int ret;
395
Mathias Krauseb2687cd2015-06-13 14:26:50 +0200396 dev_desc = (const struct lpss_device_desc *)id->driver_data;
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200397 if (!dev_desc) {
Heikki Krogerus15718752016-11-03 16:21:26 +0200398 pdev = acpi_create_platform_device(adev, NULL);
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200399 return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
400 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100401 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
402 if (!pdata)
403 return -ENOMEM;
404
405 INIT_LIST_HEAD(&resource_list);
406 ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
407 if (ret < 0)
408 goto err_out;
409
410 list_for_each_entry(rentry, &resource_list, node)
Jiang Liu90e97822015-02-05 13:44:43 +0800411 if (resource_type(rentry->res) == IORESOURCE_MEM) {
Mika Westerberg958c4eb2013-06-18 16:51:35 +0300412 if (dev_desc->prv_size_override)
413 pdata->mmio_size = dev_desc->prv_size_override;
414 else
Jiang Liu90e97822015-02-05 13:44:43 +0800415 pdata->mmio_size = resource_size(rentry->res);
416 pdata->mmio_base = ioremap(rentry->res->start,
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100417 pdata->mmio_size);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100418 break;
419 }
420
421 acpi_dev_free_resource_list(&resource_list);
422
Rafael J. Wysockid3e13ff2015-07-07 00:31:47 +0200423 if (!pdata->mmio_base) {
424 ret = -ENOMEM;
425 goto err_out;
426 }
427
Mika Westerbergaf65cfe2013-09-02 13:30:25 +0300428 pdata->dev_desc = dev_desc;
429
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300430 if (dev_desc->setup)
431 dev_desc->setup(pdata);
432
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300433 if (dev_desc->flags & LPSS_CLK) {
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100434 ret = register_device_clock(adev, pdata);
435 if (ret) {
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200436 /* Skip the device, but continue the namespace scan. */
437 ret = 0;
438 goto err_out;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100439 }
440 }
441
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200442 /*
443 * This works around a known issue in ACPI tables where LPSS devices
444 * have _PS0 and _PS3 without _PSC (and no power resources), so
445 * acpi_bus_init_power() will assume that the BIOS has put them into D0.
446 */
447 ret = acpi_device_fix_up_power(adev);
448 if (ret) {
449 /* Skip the device, but continue the namespace scan. */
450 ret = 0;
451 goto err_out;
452 }
453
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100454 adev->driver_data = pdata;
Heikki Krogerus15718752016-11-03 16:21:26 +0200455 pdev = acpi_create_platform_device(adev, dev_desc->properties);
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200456 if (!IS_ERR_OR_NULL(pdev)) {
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200457 return 1;
458 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100459
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200460 ret = PTR_ERR(pdev);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100461 adev->driver_data = NULL;
462
463 err_out:
464 kfree(pdata);
465 return ret;
466}
467
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100468static u32 __lpss_reg_read(struct lpss_private_data *pdata, unsigned int reg)
469{
470 return readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
471}
472
473static void __lpss_reg_write(u32 val, struct lpss_private_data *pdata,
474 unsigned int reg)
475{
476 writel(val, pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
477}
478
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100479static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
480{
481 struct acpi_device *adev;
482 struct lpss_private_data *pdata;
483 unsigned long flags;
484 int ret;
485
486 ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
487 if (WARN_ON(ret))
488 return ret;
489
490 spin_lock_irqsave(&dev->power.lock, flags);
491 if (pm_runtime_suspended(dev)) {
492 ret = -EAGAIN;
493 goto out;
494 }
495 pdata = acpi_driver_data(adev);
496 if (WARN_ON(!pdata || !pdata->mmio_base)) {
497 ret = -ENODEV;
498 goto out;
499 }
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100500 *val = __lpss_reg_read(pdata, reg);
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100501
502 out:
503 spin_unlock_irqrestore(&dev->power.lock, flags);
504 return ret;
505}
506
507static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
508 char *buf)
509{
510 u32 ltr_value = 0;
511 unsigned int reg;
512 int ret;
513
514 reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
515 ret = lpss_reg_read(dev, reg, &ltr_value);
516 if (ret)
517 return ret;
518
519 return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
520}
521
522static ssize_t lpss_ltr_mode_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
524{
525 u32 ltr_mode = 0;
526 char *outstr;
527 int ret;
528
529 ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
530 if (ret)
531 return ret;
532
533 outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
534 return sprintf(buf, "%s\n", outstr);
535}
536
537static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
538static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
539static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
540
541static struct attribute *lpss_attrs[] = {
542 &dev_attr_auto_ltr.attr,
543 &dev_attr_sw_ltr.attr,
544 &dev_attr_ltr_mode.attr,
545 NULL,
546};
547
548static struct attribute_group lpss_attr_group = {
549 .attrs = lpss_attrs,
550 .name = "lpss_ltr",
551};
552
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100553static void acpi_lpss_set_ltr(struct device *dev, s32 val)
554{
555 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
556 u32 ltr_mode, ltr_val;
557
558 ltr_mode = __lpss_reg_read(pdata, LPSS_GENERAL);
559 if (val < 0) {
560 if (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) {
561 ltr_mode &= ~LPSS_GENERAL_LTR_MODE_SW;
562 __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
563 }
564 return;
565 }
566 ltr_val = __lpss_reg_read(pdata, LPSS_SW_LTR) & ~LPSS_LTR_SNOOP_MASK;
567 if (val >= LPSS_LTR_SNOOP_LAT_CUTOFF) {
568 ltr_val |= LPSS_LTR_SNOOP_LAT_32US;
569 val = LPSS_LTR_MAX_VAL;
570 } else if (val > LPSS_LTR_MAX_VAL) {
571 ltr_val |= LPSS_LTR_SNOOP_LAT_32US | LPSS_LTR_SNOOP_REQ;
572 val >>= LPSS_LTR_SNOOP_LAT_SHIFT;
573 } else {
574 ltr_val |= LPSS_LTR_SNOOP_LAT_1US | LPSS_LTR_SNOOP_REQ;
575 }
576 ltr_val |= val;
577 __lpss_reg_write(ltr_val, pdata, LPSS_SW_LTR);
578 if (!(ltr_mode & LPSS_GENERAL_LTR_MODE_SW)) {
579 ltr_mode |= LPSS_GENERAL_LTR_MODE_SW;
580 __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
581 }
582}
583
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300584#ifdef CONFIG_PM
585/**
586 * acpi_lpss_save_ctx() - Save the private registers of LPSS device
587 * @dev: LPSS device
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200588 * @pdata: pointer to the private data of the LPSS device
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300589 *
590 * Most LPSS devices have private registers which may loose their context when
591 * the device is powered down. acpi_lpss_save_ctx() saves those registers into
592 * prv_reg_ctx array.
593 */
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200594static void acpi_lpss_save_ctx(struct device *dev,
595 struct lpss_private_data *pdata)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300596{
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300597 unsigned int i;
598
599 for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
600 unsigned long offset = i * sizeof(u32);
601
602 pdata->prv_reg_ctx[i] = __lpss_reg_read(pdata, offset);
603 dev_dbg(dev, "saving 0x%08x from LPSS reg at offset 0x%02lx\n",
604 pdata->prv_reg_ctx[i], offset);
605 }
606}
607
608/**
609 * acpi_lpss_restore_ctx() - Restore the private registers of LPSS device
610 * @dev: LPSS device
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200611 * @pdata: pointer to the private data of the LPSS device
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300612 *
613 * Restores the registers that were previously stored with acpi_lpss_save_ctx().
614 */
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200615static void acpi_lpss_restore_ctx(struct device *dev,
616 struct lpss_private_data *pdata)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300617{
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300618 unsigned int i;
619
Andy Shevchenko02b98542015-12-04 23:49:21 +0200620 for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
621 unsigned long offset = i * sizeof(u32);
622
623 __lpss_reg_write(pdata->prv_reg_ctx[i], pdata, offset);
624 dev_dbg(dev, "restoring 0x%08x to LPSS reg at offset 0x%02lx\n",
625 pdata->prv_reg_ctx[i], offset);
626 }
627}
628
629static void acpi_lpss_d3_to_d0_delay(struct lpss_private_data *pdata)
630{
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300631 /*
632 * The following delay is needed or the subsequent write operations may
633 * fail. The LPSS devices are actually PCI devices and the PCI spec
634 * expects 10ms delay before the device can be accessed after D3 to D0
Srinidhi Kasagarb00855a2015-08-27 21:30:55 +0530635 * transition. However some platforms like BSW does not need this delay.
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300636 */
Srinidhi Kasagarb00855a2015-08-27 21:30:55 +0530637 unsigned int delay = 10; /* default 10ms delay */
638
639 if (pdata->dev_desc->flags & LPSS_NO_D3_DELAY)
640 delay = 0;
641
642 msleep(delay);
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300643}
644
Andy Shevchenkoc3a49cf2015-12-04 23:49:20 +0200645static int acpi_lpss_activate(struct device *dev)
646{
647 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
648 int ret;
649
650 ret = acpi_dev_runtime_resume(dev);
651 if (ret)
652 return ret;
653
654 acpi_lpss_d3_to_d0_delay(pdata);
655
656 /*
657 * This is called only on ->probe() stage where a device is either in
658 * known state defined by BIOS or most likely powered off. Due to this
659 * we have to deassert reset line to be sure that ->probe() will
660 * recognize the device.
661 */
662 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
663 lpss_deassert_reset(pdata);
664
665 return 0;
666}
667
668static void acpi_lpss_dismiss(struct device *dev)
669{
670 acpi_dev_runtime_suspend(dev);
671}
672
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300673#ifdef CONFIG_PM_SLEEP
674static int acpi_lpss_suspend_late(struct device *dev)
675{
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200676 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
677 int ret;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300678
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200679 ret = pm_generic_suspend_late(dev);
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300680 if (ret)
681 return ret;
682
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200683 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
684 acpi_lpss_save_ctx(dev, pdata);
685
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300686 return acpi_dev_suspend_late(dev);
687}
688
Fu Zhonghuif4168b62014-09-09 16:30:06 +0200689static int acpi_lpss_resume_early(struct device *dev)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300690{
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200691 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
692 int ret;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300693
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200694 ret = acpi_dev_resume_early(dev);
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300695 if (ret)
696 return ret;
697
Andy Shevchenko02b98542015-12-04 23:49:21 +0200698 acpi_lpss_d3_to_d0_delay(pdata);
699
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200700 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
701 acpi_lpss_restore_ctx(dev, pdata);
702
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300703 return pm_generic_resume_early(dev);
704}
705#endif /* CONFIG_PM_SLEEP */
706
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +0100707/* IOSF SB for LPSS island */
708#define LPSS_IOSF_UNIT_LPIOEP 0xA0
709#define LPSS_IOSF_UNIT_LPIO1 0xAB
710#define LPSS_IOSF_UNIT_LPIO2 0xAC
711
712#define LPSS_IOSF_PMCSR 0x84
713#define LPSS_PMCSR_D0 0
714#define LPSS_PMCSR_D3hot 3
715#define LPSS_PMCSR_Dx_MASK GENMASK(1, 0)
716
717#define LPSS_IOSF_GPIODEF0 0x154
718#define LPSS_GPIODEF0_DMA1_D3 BIT(2)
719#define LPSS_GPIODEF0_DMA2_D3 BIT(3)
720#define LPSS_GPIODEF0_DMA_D3_MASK GENMASK(3, 2)
721
722static DEFINE_MUTEX(lpss_iosf_mutex);
723
724static void lpss_iosf_enter_d3_state(void)
725{
726 u32 value1 = 0;
727 u32 mask1 = LPSS_GPIODEF0_DMA_D3_MASK;
728 u32 value2 = LPSS_PMCSR_D3hot;
729 u32 mask2 = LPSS_PMCSR_Dx_MASK;
730 /*
731 * PMC provides an information about actual status of the LPSS devices.
732 * Here we read the values related to LPSS power island, i.e. LPSS
733 * devices, excluding both LPSS DMA controllers, along with SCC domain.
734 */
735 u32 func_dis, d3_sts_0, pmc_status, pmc_mask = 0xfe000ffe;
736 int ret;
737
738 ret = pmc_atom_read(PMC_FUNC_DIS, &func_dis);
739 if (ret)
740 return;
741
742 mutex_lock(&lpss_iosf_mutex);
743
744 ret = pmc_atom_read(PMC_D3_STS_0, &d3_sts_0);
745 if (ret)
746 goto exit;
747
748 /*
749 * Get the status of entire LPSS power island per device basis.
750 * Shutdown both LPSS DMA controllers if and only if all other devices
751 * are already in D3hot.
752 */
753 pmc_status = (~(d3_sts_0 | func_dis)) & pmc_mask;
754 if (pmc_status)
755 goto exit;
756
757 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO1, MBI_CFG_WRITE,
758 LPSS_IOSF_PMCSR, value2, mask2);
759
760 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO2, MBI_CFG_WRITE,
761 LPSS_IOSF_PMCSR, value2, mask2);
762
763 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIOEP, MBI_CR_WRITE,
764 LPSS_IOSF_GPIODEF0, value1, mask1);
765exit:
766 mutex_unlock(&lpss_iosf_mutex);
767}
768
769static void lpss_iosf_exit_d3_state(void)
770{
771 u32 value1 = LPSS_GPIODEF0_DMA1_D3 | LPSS_GPIODEF0_DMA2_D3;
772 u32 mask1 = LPSS_GPIODEF0_DMA_D3_MASK;
773 u32 value2 = LPSS_PMCSR_D0;
774 u32 mask2 = LPSS_PMCSR_Dx_MASK;
775
776 mutex_lock(&lpss_iosf_mutex);
777
778 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIOEP, MBI_CR_WRITE,
779 LPSS_IOSF_GPIODEF0, value1, mask1);
780
781 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO2, MBI_CFG_WRITE,
782 LPSS_IOSF_PMCSR, value2, mask2);
783
784 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO1, MBI_CFG_WRITE,
785 LPSS_IOSF_PMCSR, value2, mask2);
786
787 mutex_unlock(&lpss_iosf_mutex);
788}
789
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300790static int acpi_lpss_runtime_suspend(struct device *dev)
791{
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200792 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
793 int ret;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300794
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200795 ret = pm_generic_runtime_suspend(dev);
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300796 if (ret)
797 return ret;
798
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200799 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
800 acpi_lpss_save_ctx(dev, pdata);
801
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +0100802 ret = acpi_dev_runtime_suspend(dev);
803
804 /*
805 * This call must be last in the sequence, otherwise PMC will return
806 * wrong status for devices being about to be powered off. See
807 * lpss_iosf_enter_d3_state() for further information.
808 */
809 if (lpss_quirks & LPSS_QUIRK_ALWAYS_POWER_ON && iosf_mbi_available())
810 lpss_iosf_enter_d3_state();
811
812 return ret;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300813}
814
815static int acpi_lpss_runtime_resume(struct device *dev)
816{
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200817 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
818 int ret;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300819
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +0100820 /*
821 * This call is kept first to be in symmetry with
822 * acpi_lpss_runtime_suspend() one.
823 */
824 if (lpss_quirks & LPSS_QUIRK_ALWAYS_POWER_ON && iosf_mbi_available())
825 lpss_iosf_exit_d3_state();
826
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200827 ret = acpi_dev_runtime_resume(dev);
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300828 if (ret)
829 return ret;
830
Andy Shevchenko02b98542015-12-04 23:49:21 +0200831 acpi_lpss_d3_to_d0_delay(pdata);
832
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200833 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
834 acpi_lpss_restore_ctx(dev, pdata);
835
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300836 return pm_generic_runtime_resume(dev);
837}
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300838#endif /* CONFIG_PM */
839
840static struct dev_pm_domain acpi_lpss_pm_domain = {
Andy Shevchenkoc3a49cf2015-12-04 23:49:20 +0200841#ifdef CONFIG_PM
842 .activate = acpi_lpss_activate,
843 .dismiss = acpi_lpss_dismiss,
844#endif
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300845 .ops = {
Rafael J. Wysocki5de21bb92014-11-27 22:38:23 +0100846#ifdef CONFIG_PM
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300847#ifdef CONFIG_PM_SLEEP
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300848 .prepare = acpi_subsys_prepare,
Rafael J. Wysocki58a1fbb2015-10-07 00:50:24 +0200849 .complete = pm_complete_with_resume_check,
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300850 .suspend = acpi_subsys_suspend,
Fu Zhonghuif4168b62014-09-09 16:30:06 +0200851 .suspend_late = acpi_lpss_suspend_late,
852 .resume_early = acpi_lpss_resume_early,
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300853 .freeze = acpi_subsys_freeze,
854 .poweroff = acpi_subsys_suspend,
Fu Zhonghuif4168b62014-09-09 16:30:06 +0200855 .poweroff_late = acpi_lpss_suspend_late,
856 .restore_early = acpi_lpss_resume_early,
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300857#endif
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300858 .runtime_suspend = acpi_lpss_runtime_suspend,
859 .runtime_resume = acpi_lpss_runtime_resume,
860#endif
861 },
862};
863
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100864static int acpi_lpss_platform_notify(struct notifier_block *nb,
865 unsigned long action, void *data)
866{
867 struct platform_device *pdev = to_platform_device(data);
868 struct lpss_private_data *pdata;
869 struct acpi_device *adev;
870 const struct acpi_device_id *id;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100871
872 id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
873 if (!id || !id->driver_data)
874 return 0;
875
876 if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
877 return 0;
878
879 pdata = acpi_driver_data(adev);
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200880 if (!pdata)
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100881 return 0;
882
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200883 if (pdata->mmio_base &&
884 pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100885 dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
886 return 0;
887 }
888
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300889 switch (action) {
Andy Shevchenkode16d552015-12-04 23:49:19 +0200890 case BUS_NOTIFY_BIND_DRIVER:
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100891 dev_pm_domain_set(&pdev->dev, &acpi_lpss_pm_domain);
Andy Shevchenkob5f88dd2015-12-04 23:49:18 +0200892 break;
Andy Shevchenkode16d552015-12-04 23:49:19 +0200893 case BUS_NOTIFY_DRIVER_NOT_BOUND:
Andy Shevchenkob5f88dd2015-12-04 23:49:18 +0200894 case BUS_NOTIFY_UNBOUND_DRIVER:
Andy Shevchenko5be6ada2016-02-01 16:17:38 +0200895 dev_pm_domain_set(&pdev->dev, NULL);
Andy Shevchenkob5f88dd2015-12-04 23:49:18 +0200896 break;
897 case BUS_NOTIFY_ADD_DEVICE:
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100898 dev_pm_domain_set(&pdev->dev, &acpi_lpss_pm_domain);
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300899 if (pdata->dev_desc->flags & LPSS_LTR)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300900 return sysfs_create_group(&pdev->dev.kobj,
901 &lpss_attr_group);
Andy Shevchenko01ac1702014-11-05 18:34:46 +0200902 break;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300903 case BUS_NOTIFY_DEL_DEVICE:
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300904 if (pdata->dev_desc->flags & LPSS_LTR)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300905 sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100906 dev_pm_domain_set(&pdev->dev, NULL);
Andy Shevchenko01ac1702014-11-05 18:34:46 +0200907 break;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300908 default:
909 break;
910 }
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100911
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300912 return 0;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100913}
914
915static struct notifier_block acpi_lpss_nb = {
916 .notifier_call = acpi_lpss_platform_notify,
917};
918
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100919static void acpi_lpss_bind(struct device *dev)
920{
921 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
922
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300923 if (!pdata || !pdata->mmio_base || !(pdata->dev_desc->flags & LPSS_LTR))
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100924 return;
925
926 if (pdata->mmio_size >= pdata->dev_desc->prv_offset + LPSS_LTR_SIZE)
927 dev->power.set_latency_tolerance = acpi_lpss_set_ltr;
928 else
929 dev_err(dev, "MMIO size insufficient to access LTR\n");
930}
931
932static void acpi_lpss_unbind(struct device *dev)
933{
934 dev->power.set_latency_tolerance = NULL;
935}
936
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100937static struct acpi_scan_handler lpss_handler = {
938 .ids = acpi_lpss_device_ids,
939 .attach = acpi_lpss_create_device,
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100940 .bind = acpi_lpss_bind,
941 .unbind = acpi_lpss_unbind,
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100942};
943
944void __init acpi_lpss_init(void)
945{
Andy Shevchenkoeebb3e82015-12-12 02:45:06 +0100946 const struct x86_cpu_id *id;
947 int ret;
948
949 ret = lpt_clk_init();
950 if (ret)
951 return;
952
953 id = x86_match_cpu(lpss_cpu_ids);
954 if (id)
955 lpss_quirks |= LPSS_QUIRK_ALWAYS_POWER_ON;
956
957 bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
958 acpi_scan_add_handler(&lpss_handler);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100959}
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200960
961#else
962
963static struct acpi_scan_handler lpss_handler = {
964 .ids = acpi_lpss_device_ids,
965};
966
967void __init acpi_lpss_init(void)
968{
969 acpi_scan_add_handler(&lpss_handler);
970}
971
972#endif /* CONFIG_X86_INTEL_LPSS */