ACPI: resource: Add ASUS model S5402ZA to quirks
[linux-2.6-block.git] / drivers / acpi / acpi_lpss.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
f58b082a
RW
2/*
3 * ACPI support for Intel Lynxpoint LPSS.
4 *
3df2da96 5 * Copyright (C) 2013, Intel Corporation
f58b082a
RW
6 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
7 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
f58b082a
RW
8 */
9
10#include <linux/acpi.h>
f58b082a
RW
11#include <linux/clkdev.h>
12#include <linux/clk-provider.h>
6025e2fa 13#include <linux/dmi.h>
f58b082a
RW
14#include <linux/err.h>
15#include <linux/io.h>
eebb3e8d 16#include <linux/mutex.h>
1e30124a 17#include <linux/pci.h>
f58b082a 18#include <linux/platform_device.h>
a9443a63 19#include <linux/platform_data/x86/clk-lpss.h>
80a7581f 20#include <linux/platform_data/x86/pmc_atom.h>
989561de 21#include <linux/pm_domain.h>
2e0f8822 22#include <linux/pm_runtime.h>
bf7696a1 23#include <linux/pwm.h>
620c803f 24#include <linux/pxa2xx_ssp.h>
a09c5913 25#include <linux/suspend.h>
c78b0830 26#include <linux/delay.h>
f58b082a
RW
27
28#include "internal.h"
29
d6ddaaac
RW
30#ifdef CONFIG_X86_INTEL_LPSS
31
eebb3e8d 32#include <asm/cpu_device_id.h>
4626d840 33#include <asm/intel-family.h>
eebb3e8d 34#include <asm/iosf_mbi.h>
eebb3e8d 35
d6ddaaac
RW
36#define LPSS_ADDR(desc) ((unsigned long)&desc)
37
f58b082a 38#define LPSS_CLK_SIZE 0x04
2e0f8822
RW
39#define LPSS_LTR_SIZE 0x18
40
41/* Offsets relative to LPSS_PRIVATE_OFFSET */
ed3a872e 42#define LPSS_CLK_DIVIDER_DEF_MASK (BIT(1) | BIT(16))
765bdd4e
MW
43#define LPSS_RESETS 0x04
44#define LPSS_RESETS_RESET_FUNC BIT(0)
45#define LPSS_RESETS_RESET_APB BIT(1)
2e0f8822
RW
46#define LPSS_GENERAL 0x08
47#define LPSS_GENERAL_LTR_MODE_SW BIT(2)
088f1fd2 48#define LPSS_GENERAL_UART_RTS_OVRD BIT(3)
2e0f8822
RW
49#define LPSS_SW_LTR 0x10
50#define LPSS_AUTO_LTR 0x14
1a8f8351
RW
51#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
06d86415
HK
58#define LPSS_TX_INT 0x20
59#define LPSS_TX_INT_MASK BIT(1)
f58b082a 60
c78b0830
HK
61#define LPSS_PRV_REG_COUNT 9
62
ff8c1af5
HK
63/* 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)
15aa5e4c
HG
69/*
70 * For some devices the DSDT AML code for another device turns off the device
71 * before our suspend handler runs, causing us to read/save all 1-s (0xffffffff)
72 * as ctx register values.
73 * Luckily these devices always use the same ctx register values, so we can
74 * work around this by saving the ctx registers once on activation.
75 */
76#define LPSS_SAVE_CTX_ONCE BIT(5)
77#define LPSS_NO_D3_DELAY BIT(6)
f6272170 78
06d86415 79struct lpss_private_data;
f58b082a
RW
80
81struct lpss_device_desc {
ff8c1af5 82 unsigned int flags;
fcf0789a 83 const char *clk_con_id;
2e0f8822 84 unsigned int prv_offset;
958c4eb2 85 size_t prv_size_override;
f167c1a1 86 const struct property_entry *properties;
06d86415 87 void (*setup)(struct lpss_private_data *pdata);
48402cee 88 bool resume_from_noirq;
f58b082a
RW
89};
90
eebb3e8d 91static const struct lpss_device_desc lpss_dma_desc = {
3df2da96 92 .flags = LPSS_CLK,
b59cc200
RW
93};
94
f58b082a 95struct lpss_private_data {
dd242a08 96 struct acpi_device *adev;
f58b082a
RW
97 void __iomem *mmio_base;
98 resource_size_t mmio_size;
03f09f73 99 unsigned int fixed_clk_rate;
f58b082a
RW
100 struct clk *clk;
101 const struct lpss_device_desc *dev_desc;
c78b0830 102 u32 prv_reg_ctx[LPSS_PRV_REG_COUNT];
f58b082a
RW
103};
104
86b62e5c
HG
105/* Devices which need to be in D3 before lpss_iosf_enter_d3_state() proceeds */
106static u32 pmc_atom_d3_mask = 0xfe000ffe;
107
eebb3e8d
AS
108/* LPSS run time quirks */
109static unsigned int lpss_quirks;
110
111/*
112 * LPSS_QUIRK_ALWAYS_POWER_ON: override power state for LPSS DMA device.
113 *
fa9e93b1 114 * The LPSS DMA controller has neither _PS0 nor _PS3 method. Moreover
eebb3e8d
AS
115 * it can be powered off automatically whenever the last LPSS device goes down.
116 * In case of no power any access to the DMA controller will hang the system.
117 * The behaviour is reproduced on some HP laptops based on Intel BayTrail as
118 * well as on ASuS T100TA transformer.
119 *
120 * This quirk overrides power state of entire LPSS island to keep DMA powered
121 * on whenever we have at least one other device in use.
122 */
123#define LPSS_QUIRK_ALWAYS_POWER_ON BIT(0)
124
1f47a77c
HK
125/* UART Component Parameter Register */
126#define LPSS_UART_CPR 0xF4
127#define LPSS_UART_CPR_AFCE BIT(4)
128
06d86415
HK
129static void lpss_uart_setup(struct lpss_private_data *pdata)
130{
088f1fd2 131 unsigned int offset;
1f47a77c 132 u32 val;
06d86415 133
088f1fd2 134 offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
1f47a77c
HK
135 val = readl(pdata->mmio_base + offset);
136 writel(val | LPSS_TX_INT_MASK, pdata->mmio_base + offset);
137
138 val = readl(pdata->mmio_base + LPSS_UART_CPR);
139 if (!(val & LPSS_UART_CPR_AFCE)) {
140 offset = pdata->dev_desc->prv_offset + LPSS_GENERAL;
141 val = readl(pdata->mmio_base + offset);
142 val |= LPSS_GENERAL_UART_RTS_OVRD;
143 writel(val, pdata->mmio_base + offset);
144 }
06d86415
HK
145}
146
3095794a 147static void lpss_deassert_reset(struct lpss_private_data *pdata)
765bdd4e
MW
148{
149 unsigned int offset;
150 u32 val;
151
152 offset = pdata->dev_desc->prv_offset + LPSS_RESETS;
153 val = readl(pdata->mmio_base + offset);
154 val |= LPSS_RESETS_RESET_APB | LPSS_RESETS_RESET_FUNC;
155 writel(val, pdata->mmio_base + offset);
3095794a
MW
156}
157
04434ab5
HG
158/*
159 * BYT PWM used for backlight control by the i915 driver on systems without
160 * the Crystal Cove PMIC.
161 */
162static struct pwm_lookup byt_pwm_lookup[] = {
163 PWM_LOOKUP_WITH_MODULE("80860F09:00", 0, "0000:00:02.0",
b2147a3a 164 "pwm_soc_backlight", 0, PWM_POLARITY_NORMAL,
04434ab5
HG
165 "pwm-lpss-platform"),
166};
167
168static void byt_pwm_setup(struct lpss_private_data *pdata)
169{
dd242a08
HG
170 struct acpi_device *adev = pdata->adev;
171
172 /* Only call pwm_add_table for the first PWM controller */
173 if (!adev->pnp.unique_id || strcmp(adev->pnp.unique_id, "1"))
174 return;
175
b2147a3a 176 pwm_add_table(byt_pwm_lookup, ARRAY_SIZE(byt_pwm_lookup));
04434ab5
HG
177}
178
3095794a
MW
179#define LPSS_I2C_ENABLE 0x6c
180
181static void byt_i2c_setup(struct lpss_private_data *pdata)
182{
86b62e5c
HG
183 const char *uid_str = acpi_device_uid(pdata->adev);
184 acpi_handle handle = pdata->adev->handle;
185 unsigned long long shared_host = 0;
186 acpi_status status;
187 long uid = 0;
188
189 /* Expected to always be true, but better safe then sorry */
8e3ecc68
LS
190 if (uid_str && !kstrtol(uid_str, 10, &uid) && uid) {
191 /* Detect I2C bus shared with PUNIT and ignore its d3 status */
192 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
193 if (ACPI_SUCCESS(status) && shared_host)
194 pmc_atom_d3_mask &= ~(BIT_LPSS2_F1_I2C1 << (uid - 1));
195 }
86b62e5c 196
3095794a 197 lpss_deassert_reset(pdata);
765bdd4e 198
03f09f73
HK
199 if (readl(pdata->mmio_base + pdata->dev_desc->prv_offset))
200 pdata->fixed_clk_rate = 133000000;
3293c7b8
MW
201
202 writel(0, pdata->mmio_base + LPSS_I2C_ENABLE);
765bdd4e 203}
43218a1b 204
bf7696a1
HG
205/* BSW PWM used for backlight control by the i915 driver */
206static struct pwm_lookup bsw_pwm_lookup[] = {
207 PWM_LOOKUP_WITH_MODULE("80862288:00", 0, "0000:00:02.0",
b2147a3a 208 "pwm_soc_backlight", 0, PWM_POLARITY_NORMAL,
bf7696a1
HG
209 "pwm-lpss-platform"),
210};
211
212static void bsw_pwm_setup(struct lpss_private_data *pdata)
213{
dd242a08
HG
214 struct acpi_device *adev = pdata->adev;
215
216 /* Only call pwm_add_table for the first PWM controller */
217 if (!adev->pnp.unique_id || strcmp(adev->pnp.unique_id, "1"))
218 return;
219
bf7696a1
HG
220 pwm_add_table(bsw_pwm_lookup, ARRAY_SIZE(bsw_pwm_lookup));
221}
222
620c803f
AS
223static const struct property_entry lpt_spi_properties[] = {
224 PROPERTY_ENTRY_U32("intel,spi-pxa2xx-type", LPSS_LPT_SSP),
225 { }
226};
227
228static const struct lpss_device_desc lpt_spi_dev_desc = {
57b30064
JN
229 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR
230 | LPSS_SAVE_CTX,
ed3a872e 231 .prv_offset = 0x800,
620c803f 232 .properties = lpt_spi_properties,
ed3a872e
HK
233};
234
b2687cd7 235static const struct lpss_device_desc lpt_i2c_dev_desc = {
57b30064 236 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_LTR | LPSS_SAVE_CTX,
2e0f8822 237 .prv_offset = 0x800,
2e0f8822
RW
238};
239
a5565cf2
HK
240static struct property_entry uart_properties[] = {
241 PROPERTY_ENTRY_U32("reg-io-width", 4),
242 PROPERTY_ENTRY_U32("reg-shift", 2),
243 PROPERTY_ENTRY_BOOL("snps,uart-16550-compatible"),
244 { },
245};
246
b2687cd7 247static const struct lpss_device_desc lpt_uart_dev_desc = {
57b30064
JN
248 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR
249 | LPSS_SAVE_CTX,
fcf0789a 250 .clk_con_id = "baudclk",
06d86415 251 .prv_offset = 0x800,
06d86415 252 .setup = lpss_uart_setup,
a5565cf2 253 .properties = uart_properties,
2e0f8822
RW
254};
255
b2687cd7 256static const struct lpss_device_desc lpt_sdio_dev_desc = {
ff8c1af5 257 .flags = LPSS_LTR,
2e0f8822 258 .prv_offset = 0x1000,
958c4eb2 259 .prv_size_override = 0x1018,
e1c74817
CCE
260};
261
b2687cd7 262static const struct lpss_device_desc byt_pwm_dev_desc = {
3f56bf3e 263 .flags = LPSS_SAVE_CTX,
fdcb613d 264 .prv_offset = 0x800,
04434ab5 265 .setup = byt_pwm_setup,
e1c74817
CCE
266};
267
b00855ae 268static const struct lpss_device_desc bsw_pwm_dev_desc = {
15aa5e4c 269 .flags = LPSS_SAVE_CTX_ONCE | LPSS_NO_D3_DELAY,
fdcb613d 270 .prv_offset = 0x800,
bf7696a1 271 .setup = bsw_pwm_setup,
5e31ee84 272 .resume_from_noirq = true,
b00855ae
SK
273};
274
b2687cd7 275static const struct lpss_device_desc byt_uart_dev_desc = {
3df2da96 276 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX,
fcf0789a 277 .clk_con_id = "baudclk",
f6272170 278 .prv_offset = 0x800,
06d86415 279 .setup = lpss_uart_setup,
a5565cf2 280 .properties = uart_properties,
f6272170
MW
281};
282
b00855ae
SK
283static const struct lpss_device_desc bsw_uart_dev_desc = {
284 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX
285 | LPSS_NO_D3_DELAY,
286 .clk_con_id = "baudclk",
287 .prv_offset = 0x800,
288 .setup = lpss_uart_setup,
a5565cf2 289 .properties = uart_properties,
b00855ae
SK
290};
291
620c803f
AS
292static const struct property_entry byt_spi_properties[] = {
293 PROPERTY_ENTRY_U32("intel,spi-pxa2xx-type", LPSS_BYT_SSP),
294 { }
295};
296
b2687cd7 297static const struct lpss_device_desc byt_spi_dev_desc = {
3df2da96 298 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX,
f6272170 299 .prv_offset = 0x400,
620c803f 300 .properties = byt_spi_properties,
f6272170
MW
301};
302
b2687cd7 303static const struct lpss_device_desc byt_sdio_dev_desc = {
3df2da96 304 .flags = LPSS_CLK,
f6272170
MW
305};
306
b2687cd7 307static const struct lpss_device_desc byt_i2c_dev_desc = {
3df2da96 308 .flags = LPSS_CLK | LPSS_SAVE_CTX,
f6272170 309 .prv_offset = 0x800,
03f09f73 310 .setup = byt_i2c_setup,
48402cee 311 .resume_from_noirq = true,
1bfbd8eb
AC
312};
313
b00855ae
SK
314static const struct lpss_device_desc bsw_i2c_dev_desc = {
315 .flags = LPSS_CLK | LPSS_SAVE_CTX | LPSS_NO_D3_DELAY,
316 .prv_offset = 0x800,
317 .setup = byt_i2c_setup,
48402cee 318 .resume_from_noirq = true,
b00855ae
SK
319};
320
620c803f
AS
321static const struct property_entry bsw_spi_properties[] = {
322 PROPERTY_ENTRY_U32("intel,spi-pxa2xx-type", LPSS_BSW_SSP),
323 { }
324};
325
eebb3e8d 326static const struct lpss_device_desc bsw_spi_dev_desc = {
b00855ae
SK
327 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX
328 | LPSS_NO_D3_DELAY,
3095794a
MW
329 .prv_offset = 0x400,
330 .setup = lpss_deassert_reset,
620c803f 331 .properties = bsw_spi_properties,
3095794a
MW
332};
333
eebb3e8d 334static const struct x86_cpu_id lpss_cpu_ids[] = {
e36cf2f7
TG
335 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT, NULL),
336 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT, NULL),
eebb3e8d
AS
337 {}
338};
339
d6ddaaac
RW
340#else
341
342#define LPSS_ADDR(desc) (0UL)
343
344#endif /* CONFIG_X86_INTEL_LPSS */
345
f58b082a 346static const struct acpi_device_id acpi_lpss_device_ids[] = {
b59cc200 347 /* Generic LPSS devices */
d6ddaaac 348 { "INTL9C60", LPSS_ADDR(lpss_dma_desc) },
b59cc200 349
f58b082a 350 /* Lynxpoint LPSS devices */
620c803f
AS
351 { "INT33C0", LPSS_ADDR(lpt_spi_dev_desc) },
352 { "INT33C1", LPSS_ADDR(lpt_spi_dev_desc) },
d6ddaaac
RW
353 { "INT33C2", LPSS_ADDR(lpt_i2c_dev_desc) },
354 { "INT33C3", LPSS_ADDR(lpt_i2c_dev_desc) },
355 { "INT33C4", LPSS_ADDR(lpt_uart_dev_desc) },
356 { "INT33C5", LPSS_ADDR(lpt_uart_dev_desc) },
357 { "INT33C6", LPSS_ADDR(lpt_sdio_dev_desc) },
f58b082a
RW
358 { "INT33C7", },
359
f6272170 360 /* BayTrail LPSS devices */
d6ddaaac
RW
361 { "80860F09", LPSS_ADDR(byt_pwm_dev_desc) },
362 { "80860F0A", LPSS_ADDR(byt_uart_dev_desc) },
363 { "80860F0E", LPSS_ADDR(byt_spi_dev_desc) },
364 { "80860F14", LPSS_ADDR(byt_sdio_dev_desc) },
365 { "80860F41", LPSS_ADDR(byt_i2c_dev_desc) },
f6272170 366 { "INT33B2", },
20482d32 367 { "INT33FC", },
f6272170 368
1bfbd8eb 369 /* Braswell LPSS devices */
24071406 370 { "80862286", LPSS_ADDR(lpss_dma_desc) },
b00855ae
SK
371 { "80862288", LPSS_ADDR(bsw_pwm_dev_desc) },
372 { "8086228A", LPSS_ADDR(bsw_uart_dev_desc) },
3095794a 373 { "8086228E", LPSS_ADDR(bsw_spi_dev_desc) },
24071406 374 { "808622C0", LPSS_ADDR(lpss_dma_desc) },
b00855ae 375 { "808622C1", LPSS_ADDR(bsw_i2c_dev_desc) },
1bfbd8eb 376
b00855ae 377 /* Broadwell LPSS devices */
620c803f
AS
378 { "INT3430", LPSS_ADDR(lpt_spi_dev_desc) },
379 { "INT3431", LPSS_ADDR(lpt_spi_dev_desc) },
d6ddaaac
RW
380 { "INT3432", LPSS_ADDR(lpt_i2c_dev_desc) },
381 { "INT3433", LPSS_ADDR(lpt_i2c_dev_desc) },
382 { "INT3434", LPSS_ADDR(lpt_uart_dev_desc) },
383 { "INT3435", LPSS_ADDR(lpt_uart_dev_desc) },
384 { "INT3436", LPSS_ADDR(lpt_sdio_dev_desc) },
a4d97536
MW
385 { "INT3437", },
386
ff8c1af5 387 /* Wildcat Point LPSS devices */
620c803f 388 { "INT3438", LPSS_ADDR(lpt_spi_dev_desc) },
43218a1b 389
f58b082a
RW
390 { }
391};
392
d6ddaaac
RW
393#ifdef CONFIG_X86_INTEL_LPSS
394
f58b082a
RW
395/* LPSS main clock device. */
396static struct platform_device *lpss_clk_dev;
397
398static inline void lpt_register_clock_device(void)
399{
cf0a9565
AS
400 lpss_clk_dev = platform_device_register_simple("clk-lpss-atom",
401 PLATFORM_DEVID_NONE,
402 NULL, 0);
f58b082a
RW
403}
404
405static int register_device_clock(struct acpi_device *adev,
406 struct lpss_private_data *pdata)
407{
408 const struct lpss_device_desc *dev_desc = pdata->dev_desc;
ed3a872e 409 const char *devname = dev_name(&adev->dev);
71c50dbe 410 struct clk *clk;
b59cc200 411 struct lpss_clk_data *clk_data;
ed3a872e
HK
412 const char *parent, *clk_name;
413 void __iomem *prv_base;
f58b082a
RW
414
415 if (!lpss_clk_dev)
416 lpt_register_clock_device();
417
b4f1f61e 418 if (IS_ERR(lpss_clk_dev))
419 return PTR_ERR(lpss_clk_dev);
420
b59cc200
RW
421 clk_data = platform_get_drvdata(lpss_clk_dev);
422 if (!clk_data)
423 return -ENODEV;
b0d00f8b 424 clk = clk_data->clk;
b59cc200
RW
425
426 if (!pdata->mmio_base
2e0f8822 427 || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
f58b082a
RW
428 return -ENODATA;
429
f6272170 430 parent = clk_data->name;
ed3a872e 431 prv_base = pdata->mmio_base + dev_desc->prv_offset;
f6272170 432
03f09f73
HK
433 if (pdata->fixed_clk_rate) {
434 clk = clk_register_fixed_rate(NULL, devname, parent, 0,
435 pdata->fixed_clk_rate);
436 goto out;
f6272170
MW
437 }
438
ff8c1af5 439 if (dev_desc->flags & LPSS_CLK_GATE) {
ed3a872e
HK
440 clk = clk_register_gate(NULL, devname, parent, 0,
441 prv_base, 0, 0, NULL);
442 parent = devname;
443 }
444
ff8c1af5 445 if (dev_desc->flags & LPSS_CLK_DIVIDER) {
ed3a872e
HK
446 /* Prevent division by zero */
447 if (!readl(prv_base))
448 writel(LPSS_CLK_DIVIDER_DEF_MASK, prv_base);
449
450 clk_name = kasprintf(GFP_KERNEL, "%s-div", devname);
451 if (!clk_name)
452 return -ENOMEM;
453 clk = clk_register_fractional_divider(NULL, clk_name, parent,
82f53f9e
AS
454 CLK_FRAC_DIVIDER_POWER_OF_TWO_PS,
455 prv_base, 1, 15, 16, 15, 0, NULL);
ed3a872e
HK
456 parent = clk_name;
457
458 clk_name = kasprintf(GFP_KERNEL, "%s-update", devname);
459 if (!clk_name) {
460 kfree(parent);
461 return -ENOMEM;
462 }
463 clk = clk_register_gate(NULL, clk_name, parent,
464 CLK_SET_RATE_PARENT | CLK_SET_RATE_GATE,
465 prv_base, 31, 0, NULL);
466 kfree(parent);
467 kfree(clk_name);
f6272170 468 }
03f09f73 469out:
f6272170
MW
470 if (IS_ERR(clk))
471 return PTR_ERR(clk);
f58b082a 472
ed3a872e 473 pdata->clk = clk;
fcf0789a 474 clk_register_clkdev(clk, dev_desc->clk_con_id, devname);
f58b082a
RW
475 return 0;
476}
477
e6ce0ce3
AH
478struct lpss_device_links {
479 const char *supplier_hid;
480 const char *supplier_uid;
481 const char *consumer_hid;
482 const char *consumer_uid;
483 u32 flags;
6025e2fa
HG
484 const struct dmi_system_id *dep_missing_ids;
485};
486
487/* Please keep this list sorted alphabetically by vendor and model */
488static const struct dmi_system_id i2c1_dep_missing_dmi_ids[] = {
489 {
490 .matches = {
491 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
492 DMI_MATCH(DMI_PRODUCT_NAME, "T200TA"),
493 },
494 },
495 {}
e6ce0ce3
AH
496};
497
498/*
499 * The _DEP method is used to identify dependencies but instead of creating
500 * device links for every handle in _DEP, only links in the following list are
501 * created. That is necessary because, in the general case, _DEP can refer to
502 * devices that might not have drivers, or that are on different buses, or where
503 * the supplier is not enumerated until after the consumer is probed.
504 */
505static const struct lpss_device_links lpss_device_links[] = {
cc18735f 506 /* CHT External sdcard slot controller depends on PMIC I2C ctrl */
e6ce0ce3 507 {"808622C1", "7", "80860F14", "3", DL_FLAG_PM_RUNTIME},
cc18735f 508 /* CHT iGPU depends on PMIC I2C controller */
bd0f4e34 509 {"808622C1", "7", "LNXVIDEO", NULL, DL_FLAG_PM_RUNTIME},
b3b3519c 510 /* BYT iGPU depends on the Embedded Controller I2C controller (UID 1) */
6025e2fa
HG
511 {"80860F41", "1", "LNXVIDEO", NULL, DL_FLAG_PM_RUNTIME,
512 i2c1_dep_missing_dmi_ids},
cc18735f 513 /* BYT CR iGPU depends on PMIC I2C controller (UID 5 on CR) */
2d71ee0c 514 {"80860F41", "5", "LNXVIDEO", NULL, DL_FLAG_PM_RUNTIME},
cc18735f
HG
515 /* BYT iGPU depends on PMIC I2C controller (UID 7 on non CR) */
516 {"80860F41", "7", "LNXVIDEO", NULL, DL_FLAG_PM_RUNTIME},
e6ce0ce3
AH
517};
518
e6ce0ce3
AH
519static bool acpi_lpss_is_supplier(struct acpi_device *adev,
520 const struct lpss_device_links *link)
521{
7e70c8ac 522 return acpi_dev_hid_uid_match(adev, link->supplier_hid, link->supplier_uid);
e6ce0ce3
AH
523}
524
525static bool acpi_lpss_is_consumer(struct acpi_device *adev,
526 const struct lpss_device_links *link)
527{
7e70c8ac 528 return acpi_dev_hid_uid_match(adev, link->consumer_hid, link->consumer_uid);
e6ce0ce3
AH
529}
530
531struct hid_uid {
532 const char *hid;
533 const char *uid;
534};
535
418e3ea1 536static int match_hid_uid(struct device *dev, const void *data)
e6ce0ce3
AH
537{
538 struct acpi_device *adev = ACPI_COMPANION(dev);
418e3ea1 539 const struct hid_uid *id = data;
e6ce0ce3
AH
540
541 if (!adev)
542 return 0;
543
7e70c8ac 544 return acpi_dev_hid_uid_match(adev, id->hid, id->uid);
e6ce0ce3
AH
545}
546
547static struct device *acpi_lpss_find_device(const char *hid, const char *uid)
548{
1e30124a
HG
549 struct device *dev;
550
e6ce0ce3
AH
551 struct hid_uid data = {
552 .hid = hid,
553 .uid = uid,
554 };
555
1e30124a
HG
556 dev = bus_find_device(&platform_bus_type, NULL, &data, match_hid_uid);
557 if (dev)
558 return dev;
559
560 return bus_find_device(&pci_bus_type, NULL, &data, match_hid_uid);
e6ce0ce3
AH
561}
562
563static bool acpi_lpss_dep(struct acpi_device *adev, acpi_handle handle)
564{
565 struct acpi_handle_list dep_devices;
566 acpi_status status;
567 int i;
568
569 if (!acpi_has_method(adev->handle, "_DEP"))
570 return false;
571
572 status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
573 &dep_devices);
574 if (ACPI_FAILURE(status)) {
575 dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
576 return false;
577 }
578
579 for (i = 0; i < dep_devices.count; i++) {
580 if (dep_devices.handles[i] == handle)
581 return true;
582 }
583
584 return false;
585}
586
587static void acpi_lpss_link_consumer(struct device *dev1,
588 const struct lpss_device_links *link)
589{
590 struct device *dev2;
591
592 dev2 = acpi_lpss_find_device(link->consumer_hid, link->consumer_uid);
593 if (!dev2)
594 return;
595
6025e2fa
HG
596 if ((link->dep_missing_ids && dmi_check_system(link->dep_missing_ids))
597 || acpi_lpss_dep(ACPI_COMPANION(dev2), ACPI_HANDLE(dev1)))
e6ce0ce3
AH
598 device_link_add(dev2, dev1, link->flags);
599
600 put_device(dev2);
601}
602
603static void acpi_lpss_link_supplier(struct device *dev1,
604 const struct lpss_device_links *link)
605{
606 struct device *dev2;
607
608 dev2 = acpi_lpss_find_device(link->supplier_hid, link->supplier_uid);
609 if (!dev2)
610 return;
611
6025e2fa
HG
612 if ((link->dep_missing_ids && dmi_check_system(link->dep_missing_ids))
613 || acpi_lpss_dep(ACPI_COMPANION(dev1), ACPI_HANDLE(dev2)))
e6ce0ce3
AH
614 device_link_add(dev1, dev2, link->flags);
615
616 put_device(dev2);
617}
618
619static void acpi_lpss_create_device_links(struct acpi_device *adev,
620 struct platform_device *pdev)
621{
622 int i;
623
624 for (i = 0; i < ARRAY_SIZE(lpss_device_links); i++) {
625 const struct lpss_device_links *link = &lpss_device_links[i];
626
627 if (acpi_lpss_is_supplier(adev, link))
628 acpi_lpss_link_consumer(&pdev->dev, link);
629
630 if (acpi_lpss_is_consumer(adev, link))
631 acpi_lpss_link_supplier(&pdev->dev, link);
632 }
633}
634
f58b082a
RW
635static int acpi_lpss_create_device(struct acpi_device *adev,
636 const struct acpi_device_id *id)
637{
b2687cd7 638 const struct lpss_device_desc *dev_desc;
f58b082a 639 struct lpss_private_data *pdata;
90e97820 640 struct resource_entry *rentry;
f58b082a 641 struct list_head resource_list;
8ce62f85 642 struct platform_device *pdev;
f58b082a
RW
643 int ret;
644
b2687cd7 645 dev_desc = (const struct lpss_device_desc *)id->driver_data;
8ce62f85 646 if (!dev_desc) {
1571875b 647 pdev = acpi_create_platform_device(adev, NULL);
8ce62f85
RW
648 return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
649 }
f58b082a
RW
650 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
651 if (!pdata)
652 return -ENOMEM;
653
654 INIT_LIST_HEAD(&resource_list);
840baca4 655 ret = acpi_dev_get_memory_resources(adev, &resource_list);
f58b082a
RW
656 if (ret < 0)
657 goto err_out;
658
659 list_for_each_entry(rentry, &resource_list, node)
90e97820 660 if (resource_type(rentry->res) == IORESOURCE_MEM) {
958c4eb2
MW
661 if (dev_desc->prv_size_override)
662 pdata->mmio_size = dev_desc->prv_size_override;
663 else
90e97820
JL
664 pdata->mmio_size = resource_size(rentry->res);
665 pdata->mmio_base = ioremap(rentry->res->start,
f58b082a 666 pdata->mmio_size);
f58b082a
RW
667 break;
668 }
669
670 acpi_dev_free_resource_list(&resource_list);
671
d3e13ff3 672 if (!pdata->mmio_base) {
e1681599
HG
673 /* Avoid acpi_bus_attach() instantiating a pdev for this dev. */
674 adev->pnp.type.platform_id = 0;
a4bb2b49
RT
675 /* Skip the device, but continue the namespace scan. */
676 ret = 0;
d3e13ff3
RW
677 goto err_out;
678 }
679
dd242a08 680 pdata->adev = adev;
af65cfe9
MW
681 pdata->dev_desc = dev_desc;
682
03f09f73
HK
683 if (dev_desc->setup)
684 dev_desc->setup(pdata);
685
ff8c1af5 686 if (dev_desc->flags & LPSS_CLK) {
f58b082a
RW
687 ret = register_device_clock(adev, pdata);
688 if (ret) {
b9e95fc6
RW
689 /* Skip the device, but continue the namespace scan. */
690 ret = 0;
691 goto err_out;
f58b082a
RW
692 }
693 }
694
b9e95fc6
RW
695 /*
696 * This works around a known issue in ACPI tables where LPSS devices
697 * have _PS0 and _PS3 without _PSC (and no power resources), so
698 * acpi_bus_init_power() will assume that the BIOS has put them into D0.
699 */
1a2fa02f 700 acpi_device_fix_up_power(adev);
b9e95fc6 701
f58b082a 702 adev->driver_data = pdata;
1571875b 703 pdev = acpi_create_platform_device(adev, dev_desc->properties);
8ce62f85 704 if (!IS_ERR_OR_NULL(pdev)) {
e6ce0ce3 705 acpi_lpss_create_device_links(adev, pdev);
8ce62f85
RW
706 return 1;
707 }
f58b082a 708
8ce62f85 709 ret = PTR_ERR(pdev);
f58b082a
RW
710 adev->driver_data = NULL;
711
712 err_out:
713 kfree(pdata);
714 return ret;
715}
716
1a8f8351
RW
717static u32 __lpss_reg_read(struct lpss_private_data *pdata, unsigned int reg)
718{
719 return readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
720}
721
722static void __lpss_reg_write(u32 val, struct lpss_private_data *pdata,
723 unsigned int reg)
724{
725 writel(val, pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
726}
727
2e0f8822
RW
728static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
729{
50861d43 730 struct acpi_device *adev = ACPI_COMPANION(dev);
2e0f8822
RW
731 struct lpss_private_data *pdata;
732 unsigned long flags;
733 int ret;
734
50861d43
RW
735 if (WARN_ON(!adev))
736 return -ENODEV;
2e0f8822
RW
737
738 spin_lock_irqsave(&dev->power.lock, flags);
739 if (pm_runtime_suspended(dev)) {
740 ret = -EAGAIN;
741 goto out;
742 }
743 pdata = acpi_driver_data(adev);
744 if (WARN_ON(!pdata || !pdata->mmio_base)) {
745 ret = -ENODEV;
746 goto out;
747 }
1a8f8351 748 *val = __lpss_reg_read(pdata, reg);
50861d43 749 ret = 0;
2e0f8822
RW
750
751 out:
752 spin_unlock_irqrestore(&dev->power.lock, flags);
753 return ret;
754}
755
756static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
757 char *buf)
758{
759 u32 ltr_value = 0;
760 unsigned int reg;
761 int ret;
762
763 reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
764 ret = lpss_reg_read(dev, reg, &ltr_value);
765 if (ret)
766 return ret;
767
d47e983e 768 return sysfs_emit(buf, "%08x\n", ltr_value);
2e0f8822
RW
769}
770
771static ssize_t lpss_ltr_mode_show(struct device *dev,
772 struct device_attribute *attr, char *buf)
773{
774 u32 ltr_mode = 0;
775 char *outstr;
776 int ret;
777
778 ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
779 if (ret)
780 return ret;
781
782 outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
783 return sprintf(buf, "%s\n", outstr);
784}
785
786static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
787static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
788static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
789
790static struct attribute *lpss_attrs[] = {
791 &dev_attr_auto_ltr.attr,
792 &dev_attr_sw_ltr.attr,
793 &dev_attr_ltr_mode.attr,
794 NULL,
795};
796
31945d0e 797static const struct attribute_group lpss_attr_group = {
2e0f8822
RW
798 .attrs = lpss_attrs,
799 .name = "lpss_ltr",
800};
801
1a8f8351
RW
802static void acpi_lpss_set_ltr(struct device *dev, s32 val)
803{
804 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
805 u32 ltr_mode, ltr_val;
806
807 ltr_mode = __lpss_reg_read(pdata, LPSS_GENERAL);
808 if (val < 0) {
809 if (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) {
810 ltr_mode &= ~LPSS_GENERAL_LTR_MODE_SW;
811 __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
812 }
813 return;
814 }
815 ltr_val = __lpss_reg_read(pdata, LPSS_SW_LTR) & ~LPSS_LTR_SNOOP_MASK;
816 if (val >= LPSS_LTR_SNOOP_LAT_CUTOFF) {
817 ltr_val |= LPSS_LTR_SNOOP_LAT_32US;
818 val = LPSS_LTR_MAX_VAL;
819 } else if (val > LPSS_LTR_MAX_VAL) {
820 ltr_val |= LPSS_LTR_SNOOP_LAT_32US | LPSS_LTR_SNOOP_REQ;
821 val >>= LPSS_LTR_SNOOP_LAT_SHIFT;
822 } else {
823 ltr_val |= LPSS_LTR_SNOOP_LAT_1US | LPSS_LTR_SNOOP_REQ;
824 }
825 ltr_val |= val;
826 __lpss_reg_write(ltr_val, pdata, LPSS_SW_LTR);
827 if (!(ltr_mode & LPSS_GENERAL_LTR_MODE_SW)) {
828 ltr_mode |= LPSS_GENERAL_LTR_MODE_SW;
829 __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
830 }
831}
832
c78b0830
HK
833#ifdef CONFIG_PM
834/**
835 * acpi_lpss_save_ctx() - Save the private registers of LPSS device
836 * @dev: LPSS device
cb39dcdd 837 * @pdata: pointer to the private data of the LPSS device
c78b0830
HK
838 *
839 * Most LPSS devices have private registers which may loose their context when
840 * the device is powered down. acpi_lpss_save_ctx() saves those registers into
841 * prv_reg_ctx array.
842 */
cb39dcdd
AS
843static void acpi_lpss_save_ctx(struct device *dev,
844 struct lpss_private_data *pdata)
c78b0830 845{
c78b0830
HK
846 unsigned int i;
847
848 for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
849 unsigned long offset = i * sizeof(u32);
850
851 pdata->prv_reg_ctx[i] = __lpss_reg_read(pdata, offset);
852 dev_dbg(dev, "saving 0x%08x from LPSS reg at offset 0x%02lx\n",
853 pdata->prv_reg_ctx[i], offset);
854 }
855}
856
857/**
858 * acpi_lpss_restore_ctx() - Restore the private registers of LPSS device
859 * @dev: LPSS device
cb39dcdd 860 * @pdata: pointer to the private data of the LPSS device
c78b0830
HK
861 *
862 * Restores the registers that were previously stored with acpi_lpss_save_ctx().
863 */
cb39dcdd
AS
864static void acpi_lpss_restore_ctx(struct device *dev,
865 struct lpss_private_data *pdata)
c78b0830 866{
c78b0830
HK
867 unsigned int i;
868
02b98540
AS
869 for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
870 unsigned long offset = i * sizeof(u32);
871
872 __lpss_reg_write(pdata->prv_reg_ctx[i], pdata, offset);
873 dev_dbg(dev, "restoring 0x%08x to LPSS reg at offset 0x%02lx\n",
874 pdata->prv_reg_ctx[i], offset);
875 }
876}
877
878static void acpi_lpss_d3_to_d0_delay(struct lpss_private_data *pdata)
879{
c78b0830
HK
880 /*
881 * The following delay is needed or the subsequent write operations may
882 * fail. The LPSS devices are actually PCI devices and the PCI spec
883 * expects 10ms delay before the device can be accessed after D3 to D0
b00855ae 884 * transition. However some platforms like BSW does not need this delay.
c78b0830 885 */
b00855ae
SK
886 unsigned int delay = 10; /* default 10ms delay */
887
888 if (pdata->dev_desc->flags & LPSS_NO_D3_DELAY)
889 delay = 0;
890
891 msleep(delay);
c78b0830
HK
892}
893
c3a49cf3
AS
894static int acpi_lpss_activate(struct device *dev)
895{
896 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
897 int ret;
898
63705c40 899 ret = acpi_dev_resume(dev);
c3a49cf3
AS
900 if (ret)
901 return ret;
902
903 acpi_lpss_d3_to_d0_delay(pdata);
904
905 /*
906 * This is called only on ->probe() stage where a device is either in
907 * known state defined by BIOS or most likely powered off. Due to this
908 * we have to deassert reset line to be sure that ->probe() will
909 * recognize the device.
910 */
15aa5e4c 911 if (pdata->dev_desc->flags & (LPSS_SAVE_CTX | LPSS_SAVE_CTX_ONCE))
c3a49cf3
AS
912 lpss_deassert_reset(pdata);
913
15aa5e4c
HG
914#ifdef CONFIG_PM
915 if (pdata->dev_desc->flags & LPSS_SAVE_CTX_ONCE)
916 acpi_lpss_save_ctx(dev, pdata);
917#endif
918
c3a49cf3
AS
919 return 0;
920}
921
922static void acpi_lpss_dismiss(struct device *dev)
923{
cbe25ce3 924 acpi_dev_suspend(dev, false);
c3a49cf3
AS
925}
926
eebb3e8d
AS
927/* IOSF SB for LPSS island */
928#define LPSS_IOSF_UNIT_LPIOEP 0xA0
929#define LPSS_IOSF_UNIT_LPIO1 0xAB
930#define LPSS_IOSF_UNIT_LPIO2 0xAC
931
932#define LPSS_IOSF_PMCSR 0x84
933#define LPSS_PMCSR_D0 0
934#define LPSS_PMCSR_D3hot 3
935#define LPSS_PMCSR_Dx_MASK GENMASK(1, 0)
936
937#define LPSS_IOSF_GPIODEF0 0x154
938#define LPSS_GPIODEF0_DMA1_D3 BIT(2)
939#define LPSS_GPIODEF0_DMA2_D3 BIT(3)
940#define LPSS_GPIODEF0_DMA_D3_MASK GENMASK(3, 2)
d132d6d5 941#define LPSS_GPIODEF0_DMA_LLP BIT(13)
eebb3e8d
AS
942
943static DEFINE_MUTEX(lpss_iosf_mutex);
f11fc4bc 944static bool lpss_iosf_d3_entered = true;
eebb3e8d
AS
945
946static void lpss_iosf_enter_d3_state(void)
947{
948 u32 value1 = 0;
d132d6d5 949 u32 mask1 = LPSS_GPIODEF0_DMA_D3_MASK | LPSS_GPIODEF0_DMA_LLP;
eebb3e8d
AS
950 u32 value2 = LPSS_PMCSR_D3hot;
951 u32 mask2 = LPSS_PMCSR_Dx_MASK;
952 /*
953 * PMC provides an information about actual status of the LPSS devices.
954 * Here we read the values related to LPSS power island, i.e. LPSS
955 * devices, excluding both LPSS DMA controllers, along with SCC domain.
956 */
86b62e5c 957 u32 func_dis, d3_sts_0, pmc_status;
eebb3e8d
AS
958 int ret;
959
960 ret = pmc_atom_read(PMC_FUNC_DIS, &func_dis);
961 if (ret)
962 return;
963
964 mutex_lock(&lpss_iosf_mutex);
965
966 ret = pmc_atom_read(PMC_D3_STS_0, &d3_sts_0);
967 if (ret)
968 goto exit;
969
970 /*
971 * Get the status of entire LPSS power island per device basis.
972 * Shutdown both LPSS DMA controllers if and only if all other devices
973 * are already in D3hot.
974 */
86b62e5c 975 pmc_status = (~(d3_sts_0 | func_dis)) & pmc_atom_d3_mask;
eebb3e8d
AS
976 if (pmc_status)
977 goto exit;
978
979 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO1, MBI_CFG_WRITE,
980 LPSS_IOSF_PMCSR, value2, mask2);
981
982 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO2, MBI_CFG_WRITE,
983 LPSS_IOSF_PMCSR, value2, mask2);
984
985 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIOEP, MBI_CR_WRITE,
986 LPSS_IOSF_GPIODEF0, value1, mask1);
12864ff8
RW
987
988 lpss_iosf_d3_entered = true;
989
eebb3e8d
AS
990exit:
991 mutex_unlock(&lpss_iosf_mutex);
992}
993
994static void lpss_iosf_exit_d3_state(void)
995{
d132d6d5
AS
996 u32 value1 = LPSS_GPIODEF0_DMA1_D3 | LPSS_GPIODEF0_DMA2_D3 |
997 LPSS_GPIODEF0_DMA_LLP;
998 u32 mask1 = LPSS_GPIODEF0_DMA_D3_MASK | LPSS_GPIODEF0_DMA_LLP;
eebb3e8d
AS
999 u32 value2 = LPSS_PMCSR_D0;
1000 u32 mask2 = LPSS_PMCSR_Dx_MASK;
1001
1002 mutex_lock(&lpss_iosf_mutex);
1003
12864ff8
RW
1004 if (!lpss_iosf_d3_entered)
1005 goto exit;
1006
1007 lpss_iosf_d3_entered = false;
1008
eebb3e8d
AS
1009 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIOEP, MBI_CR_WRITE,
1010 LPSS_IOSF_GPIODEF0, value1, mask1);
1011
1012 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO2, MBI_CFG_WRITE,
1013 LPSS_IOSF_PMCSR, value2, mask2);
1014
1015 iosf_mbi_modify(LPSS_IOSF_UNIT_LPIO1, MBI_CFG_WRITE,
1016 LPSS_IOSF_PMCSR, value2, mask2);
1017
12864ff8 1018exit:
eebb3e8d
AS
1019 mutex_unlock(&lpss_iosf_mutex);
1020}
1021
12864ff8 1022static int acpi_lpss_suspend(struct device *dev, bool wakeup)
c78b0830 1023{
cb39dcdd
AS
1024 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
1025 int ret;
c78b0830 1026
cb39dcdd
AS
1027 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
1028 acpi_lpss_save_ctx(dev, pdata);
1029
a192aa92 1030 ret = acpi_dev_suspend(dev, wakeup);
eebb3e8d
AS
1031
1032 /*
1033 * This call must be last in the sequence, otherwise PMC will return
1034 * wrong status for devices being about to be powered off. See
1035 * lpss_iosf_enter_d3_state() for further information.
1036 */
12864ff8 1037 if (acpi_target_system_state() == ACPI_STATE_S0 &&
a09c5913 1038 lpss_quirks & LPSS_QUIRK_ALWAYS_POWER_ON && iosf_mbi_available())
eebb3e8d
AS
1039 lpss_iosf_enter_d3_state();
1040
1041 return ret;
c78b0830
HK
1042}
1043
12864ff8 1044static int acpi_lpss_resume(struct device *dev)
c78b0830 1045{
cb39dcdd
AS
1046 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
1047 int ret;
c78b0830 1048
eebb3e8d
AS
1049 /*
1050 * This call is kept first to be in symmetry with
1051 * acpi_lpss_runtime_suspend() one.
1052 */
12864ff8 1053 if (lpss_quirks & LPSS_QUIRK_ALWAYS_POWER_ON && iosf_mbi_available())
eebb3e8d
AS
1054 lpss_iosf_exit_d3_state();
1055
63705c40 1056 ret = acpi_dev_resume(dev);
c78b0830
HK
1057 if (ret)
1058 return ret;
1059
02b98540
AS
1060 acpi_lpss_d3_to_d0_delay(pdata);
1061
15aa5e4c 1062 if (pdata->dev_desc->flags & (LPSS_SAVE_CTX | LPSS_SAVE_CTX_ONCE))
cb39dcdd
AS
1063 acpi_lpss_restore_ctx(dev, pdata);
1064
a192aa92
RW
1065 return 0;
1066}
1067
1068#ifdef CONFIG_PM_SLEEP
48402cee 1069static int acpi_lpss_do_suspend_late(struct device *dev)
a192aa92 1070{
05087360
RW
1071 int ret;
1072
fa2bfead 1073 if (dev_pm_skip_suspend(dev))
05087360 1074 return 0;
a192aa92 1075
05087360 1076 ret = pm_generic_suspend_late(dev);
12864ff8 1077 return ret ? ret : acpi_lpss_suspend(dev, device_may_wakeup(dev));
a192aa92
RW
1078}
1079
48402cee
HG
1080static int acpi_lpss_suspend_late(struct device *dev)
1081{
1082 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
1083
1084 if (pdata->dev_desc->resume_from_noirq)
1085 return 0;
1086
1087 return acpi_lpss_do_suspend_late(dev);
1088}
1089
1090static int acpi_lpss_suspend_noirq(struct device *dev)
1091{
1092 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
1093 int ret;
1094
1095 if (pdata->dev_desc->resume_from_noirq) {
c95b7595
RW
1096 /*
1097 * The driver's ->suspend_late callback will be invoked by
1098 * acpi_lpss_do_suspend_late(), with the assumption that the
1099 * driver really wanted to run that code in ->suspend_noirq, but
1100 * it could not run after acpi_dev_suspend() and the driver
1101 * expected the latter to be called in the "late" phase.
1102 */
48402cee
HG
1103 ret = acpi_lpss_do_suspend_late(dev);
1104 if (ret)
1105 return ret;
1106 }
1107
1108 return acpi_subsys_suspend_noirq(dev);
1109}
1110
1111static int acpi_lpss_do_resume_early(struct device *dev)
a192aa92 1112{
12864ff8 1113 int ret = acpi_lpss_resume(dev);
a192aa92
RW
1114
1115 return ret ? ret : pm_generic_resume_early(dev);
1116}
48402cee
HG
1117
1118static int acpi_lpss_resume_early(struct device *dev)
1119{
1120 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
1121
1122 if (pdata->dev_desc->resume_from_noirq)
1123 return 0;
1124
76c70cb5 1125 if (dev_pm_skip_resume(dev))
6e176bf8
RW
1126 return 0;
1127
48402cee
HG
1128 return acpi_lpss_do_resume_early(dev);
1129}
1130
1131static int acpi_lpss_resume_noirq(struct device *dev)
1132{
1133 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
1134 int ret;
1135
3cd7957e 1136 /* Follow acpi_subsys_resume_noirq(). */
76c70cb5 1137 if (dev_pm_skip_resume(dev))
3cd7957e
RW
1138 return 0;
1139
3cd7957e 1140 ret = pm_generic_resume_noirq(dev);
48402cee
HG
1141 if (ret)
1142 return ret;
1143
3cd7957e
RW
1144 if (!pdata->dev_desc->resume_from_noirq)
1145 return 0;
48402cee 1146
3cd7957e
RW
1147 /*
1148 * The driver's ->resume_early callback will be invoked by
1149 * acpi_lpss_do_resume_early(), with the assumption that the driver
1150 * really wanted to run that code in ->resume_noirq, but it could not
1151 * run before acpi_dev_resume() and the driver expected the latter to be
1152 * called in the "early" phase.
1153 */
1154 return acpi_lpss_do_resume_early(dev);
1155}
1156
1157static int acpi_lpss_do_restore_early(struct device *dev)
1158{
1159 int ret = acpi_lpss_resume(dev);
1160
1161 return ret ? ret : pm_generic_restore_early(dev);
48402cee
HG
1162}
1163
3cd7957e
RW
1164static int acpi_lpss_restore_early(struct device *dev)
1165{
1166 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
1167
1168 if (pdata->dev_desc->resume_from_noirq)
1169 return 0;
1170
1171 return acpi_lpss_do_restore_early(dev);
48402cee
HG
1172}
1173
3cd7957e
RW
1174static int acpi_lpss_restore_noirq(struct device *dev)
1175{
1176 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
1177 int ret;
1178
1179 ret = pm_generic_restore_noirq(dev);
1180 if (ret)
1181 return ret;
1182
1183 if (!pdata->dev_desc->resume_from_noirq)
1184 return 0;
1185
1186 /* This is analogous to what happens in acpi_lpss_resume_noirq(). */
1187 return acpi_lpss_do_restore_early(dev);
1188}
c95b7595
RW
1189
1190static int acpi_lpss_do_poweroff_late(struct device *dev)
1191{
1192 int ret = pm_generic_poweroff_late(dev);
1193
1194 return ret ? ret : acpi_lpss_suspend(dev, device_may_wakeup(dev));
1195}
1196
1197static int acpi_lpss_poweroff_late(struct device *dev)
1198{
1199 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
1200
fa2bfead 1201 if (dev_pm_skip_suspend(dev))
c95b7595
RW
1202 return 0;
1203
1204 if (pdata->dev_desc->resume_from_noirq)
1205 return 0;
1206
1207 return acpi_lpss_do_poweroff_late(dev);
1208}
1209
1210static int acpi_lpss_poweroff_noirq(struct device *dev)
1211{
1212 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
1213
fa2bfead 1214 if (dev_pm_skip_suspend(dev))
c95b7595
RW
1215 return 0;
1216
1217 if (pdata->dev_desc->resume_from_noirq) {
1218 /* This is analogous to the acpi_lpss_suspend_noirq() case. */
1219 int ret = acpi_lpss_do_poweroff_late(dev);
bb415ed5 1220
c95b7595
RW
1221 if (ret)
1222 return ret;
1223 }
1224
1225 return pm_generic_poweroff_noirq(dev);
1226}
a192aa92
RW
1227#endif /* CONFIG_PM_SLEEP */
1228
1229static int acpi_lpss_runtime_suspend(struct device *dev)
1230{
1231 int ret = pm_generic_runtime_suspend(dev);
1232
1233 return ret ? ret : acpi_lpss_suspend(dev, true);
1234}
1235
1236static int acpi_lpss_runtime_resume(struct device *dev)
1237{
12864ff8 1238 int ret = acpi_lpss_resume(dev);
a192aa92
RW
1239
1240 return ret ? ret : pm_generic_runtime_resume(dev);
c78b0830 1241}
c78b0830
HK
1242#endif /* CONFIG_PM */
1243
1244static struct dev_pm_domain acpi_lpss_pm_domain = {
c3a49cf3
AS
1245#ifdef CONFIG_PM
1246 .activate = acpi_lpss_activate,
1247 .dismiss = acpi_lpss_dismiss,
1248#endif
c78b0830 1249 .ops = {
5de21bb9 1250#ifdef CONFIG_PM
c78b0830 1251#ifdef CONFIG_PM_SLEEP
c78b0830 1252 .prepare = acpi_subsys_prepare,
e4da817d 1253 .complete = acpi_subsys_complete,
c78b0830 1254 .suspend = acpi_subsys_suspend,
f4168b61 1255 .suspend_late = acpi_lpss_suspend_late,
48402cee
HG
1256 .suspend_noirq = acpi_lpss_suspend_noirq,
1257 .resume_noirq = acpi_lpss_resume_noirq,
f4168b61 1258 .resume_early = acpi_lpss_resume_early,
c78b0830 1259 .freeze = acpi_subsys_freeze,
c95b7595
RW
1260 .poweroff = acpi_subsys_poweroff,
1261 .poweroff_late = acpi_lpss_poweroff_late,
1262 .poweroff_noirq = acpi_lpss_poweroff_noirq,
3cd7957e
RW
1263 .restore_noirq = acpi_lpss_restore_noirq,
1264 .restore_early = acpi_lpss_restore_early,
c78b0830 1265#endif
c78b0830
HK
1266 .runtime_suspend = acpi_lpss_runtime_suspend,
1267 .runtime_resume = acpi_lpss_runtime_resume,
1268#endif
1269 },
1270};
1271
2e0f8822
RW
1272static int acpi_lpss_platform_notify(struct notifier_block *nb,
1273 unsigned long action, void *data)
1274{
1275 struct platform_device *pdev = to_platform_device(data);
1276 struct lpss_private_data *pdata;
1277 struct acpi_device *adev;
1278 const struct acpi_device_id *id;
2e0f8822
RW
1279
1280 id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
1281 if (!id || !id->driver_data)
1282 return 0;
1283
50861d43
RW
1284 adev = ACPI_COMPANION(&pdev->dev);
1285 if (!adev)
2e0f8822
RW
1286 return 0;
1287
1288 pdata = acpi_driver_data(adev);
cb39dcdd 1289 if (!pdata)
2e0f8822
RW
1290 return 0;
1291
cb39dcdd
AS
1292 if (pdata->mmio_base &&
1293 pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
2e0f8822
RW
1294 dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
1295 return 0;
1296 }
1297
c78b0830 1298 switch (action) {
de16d552 1299 case BUS_NOTIFY_BIND_DRIVER:
989561de 1300 dev_pm_domain_set(&pdev->dev, &acpi_lpss_pm_domain);
b5f88dd1 1301 break;
de16d552 1302 case BUS_NOTIFY_DRIVER_NOT_BOUND:
b5f88dd1 1303 case BUS_NOTIFY_UNBOUND_DRIVER:
5be6ada3 1304 dev_pm_domain_set(&pdev->dev, NULL);
b5f88dd1
AS
1305 break;
1306 case BUS_NOTIFY_ADD_DEVICE:
989561de 1307 dev_pm_domain_set(&pdev->dev, &acpi_lpss_pm_domain);
ff8c1af5 1308 if (pdata->dev_desc->flags & LPSS_LTR)
c78b0830
HK
1309 return sysfs_create_group(&pdev->dev.kobj,
1310 &lpss_attr_group);
01ac170b 1311 break;
c78b0830 1312 case BUS_NOTIFY_DEL_DEVICE:
ff8c1af5 1313 if (pdata->dev_desc->flags & LPSS_LTR)
c78b0830 1314 sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
989561de 1315 dev_pm_domain_set(&pdev->dev, NULL);
01ac170b 1316 break;
c78b0830
HK
1317 default:
1318 break;
1319 }
2e0f8822 1320
c78b0830 1321 return 0;
2e0f8822
RW
1322}
1323
1324static struct notifier_block acpi_lpss_nb = {
1325 .notifier_call = acpi_lpss_platform_notify,
1326};
1327
1a8f8351
RW
1328static void acpi_lpss_bind(struct device *dev)
1329{
1330 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
1331
ff8c1af5 1332 if (!pdata || !pdata->mmio_base || !(pdata->dev_desc->flags & LPSS_LTR))
1a8f8351
RW
1333 return;
1334
1335 if (pdata->mmio_size >= pdata->dev_desc->prv_offset + LPSS_LTR_SIZE)
1336 dev->power.set_latency_tolerance = acpi_lpss_set_ltr;
1337 else
1338 dev_err(dev, "MMIO size insufficient to access LTR\n");
1339}
1340
1341static void acpi_lpss_unbind(struct device *dev)
1342{
1343 dev->power.set_latency_tolerance = NULL;
1344}
1345
f58b082a
RW
1346static struct acpi_scan_handler lpss_handler = {
1347 .ids = acpi_lpss_device_ids,
1348 .attach = acpi_lpss_create_device,
1a8f8351
RW
1349 .bind = acpi_lpss_bind,
1350 .unbind = acpi_lpss_unbind,
f58b082a
RW
1351};
1352
1353void __init acpi_lpss_init(void)
1354{
eebb3e8d
AS
1355 const struct x86_cpu_id *id;
1356 int ret;
1357
cf0a9565 1358 ret = lpss_atom_clk_init();
eebb3e8d
AS
1359 if (ret)
1360 return;
1361
1362 id = x86_match_cpu(lpss_cpu_ids);
1363 if (id)
1364 lpss_quirks |= LPSS_QUIRK_ALWAYS_POWER_ON;
1365
1366 bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
1367 acpi_scan_add_handler(&lpss_handler);
f58b082a 1368}
d6ddaaac
RW
1369
1370#else
1371
1372static struct acpi_scan_handler lpss_handler = {
1373 .ids = acpi_lpss_device_ids,
1374};
1375
1376void __init acpi_lpss_init(void)
1377{
1378 acpi_scan_add_handler(&lpss_handler);
1379}
1380
1381#endif /* CONFIG_X86_INTEL_LPSS */