acpi_lpss.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  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. #include <linux/acpi.h>
  13. #include <linux/clk.h>
  14. #include <linux/clkdev.h>
  15. #include <linux/clk-provider.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/platform_data/clk-lpss.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/delay.h>
  22. #include "internal.h"
  23. ACPI_MODULE_NAME("acpi_lpss");
  24. #ifdef CONFIG_X86_INTEL_LPSS
  25. #define LPSS_ADDR(desc) ((unsigned long)&desc)
  26. #define LPSS_CLK_SIZE 0x04
  27. #define LPSS_LTR_SIZE 0x18
  28. /* Offsets relative to LPSS_PRIVATE_OFFSET */
  29. #define LPSS_CLK_DIVIDER_DEF_MASK (BIT(1) | BIT(16))
  30. #define LPSS_RESETS 0x04
  31. #define LPSS_RESETS_RESET_FUNC BIT(0)
  32. #define LPSS_RESETS_RESET_APB BIT(1)
  33. #define LPSS_GENERAL 0x08
  34. #define LPSS_GENERAL_LTR_MODE_SW BIT(2)
  35. #define LPSS_GENERAL_UART_RTS_OVRD BIT(3)
  36. #define LPSS_SW_LTR 0x10
  37. #define LPSS_AUTO_LTR 0x14
  38. #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
  45. #define LPSS_TX_INT 0x20
  46. #define LPSS_TX_INT_MASK BIT(1)
  47. #define LPSS_PRV_REG_COUNT 9
  48. /* LPSS Flags */
  49. #define LPSS_CLK BIT(0)
  50. #define LPSS_CLK_GATE BIT(1)
  51. #define LPSS_CLK_DIVIDER BIT(2)
  52. #define LPSS_LTR BIT(3)
  53. #define LPSS_SAVE_CTX BIT(4)
  54. struct lpss_private_data;
  55. struct lpss_device_desc {
  56. unsigned int flags;
  57. const char *clk_con_id;
  58. unsigned int prv_offset;
  59. size_t prv_size_override;
  60. void (*setup)(struct lpss_private_data *pdata);
  61. };
  62. static struct lpss_device_desc lpss_dma_desc = {
  63. .flags = LPSS_CLK,
  64. };
  65. struct lpss_private_data {
  66. void __iomem *mmio_base;
  67. resource_size_t mmio_size;
  68. unsigned int fixed_clk_rate;
  69. struct clk *clk;
  70. const struct lpss_device_desc *dev_desc;
  71. u32 prv_reg_ctx[LPSS_PRV_REG_COUNT];
  72. };
  73. /* UART Component Parameter Register */
  74. #define LPSS_UART_CPR 0xF4
  75. #define LPSS_UART_CPR_AFCE BIT(4)
  76. static void lpss_uart_setup(struct lpss_private_data *pdata)
  77. {
  78. unsigned int offset;
  79. u32 val;
  80. offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
  81. val = readl(pdata->mmio_base + offset);
  82. writel(val | LPSS_TX_INT_MASK, pdata->mmio_base + offset);
  83. val = readl(pdata->mmio_base + LPSS_UART_CPR);
  84. if (!(val & LPSS_UART_CPR_AFCE)) {
  85. offset = pdata->dev_desc->prv_offset + LPSS_GENERAL;
  86. val = readl(pdata->mmio_base + offset);
  87. val |= LPSS_GENERAL_UART_RTS_OVRD;
  88. writel(val, pdata->mmio_base + offset);
  89. }
  90. }
  91. static void lpss_deassert_reset(struct lpss_private_data *pdata)
  92. {
  93. unsigned int offset;
  94. u32 val;
  95. offset = pdata->dev_desc->prv_offset + LPSS_RESETS;
  96. val = readl(pdata->mmio_base + offset);
  97. val |= LPSS_RESETS_RESET_APB | LPSS_RESETS_RESET_FUNC;
  98. writel(val, pdata->mmio_base + offset);
  99. }
  100. #define LPSS_I2C_ENABLE 0x6c
  101. static void byt_i2c_setup(struct lpss_private_data *pdata)
  102. {
  103. lpss_deassert_reset(pdata);
  104. if (readl(pdata->mmio_base + pdata->dev_desc->prv_offset))
  105. pdata->fixed_clk_rate = 133000000;
  106. writel(0, pdata->mmio_base + LPSS_I2C_ENABLE);
  107. }
  108. static const struct lpss_device_desc lpt_dev_desc = {
  109. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR,
  110. .prv_offset = 0x800,
  111. };
  112. static const struct lpss_device_desc lpt_i2c_dev_desc = {
  113. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_LTR,
  114. .prv_offset = 0x800,
  115. };
  116. static const struct lpss_device_desc lpt_uart_dev_desc = {
  117. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR,
  118. .clk_con_id = "baudclk",
  119. .prv_offset = 0x800,
  120. .setup = lpss_uart_setup,
  121. };
  122. static const struct lpss_device_desc lpt_sdio_dev_desc = {
  123. .flags = LPSS_LTR,
  124. .prv_offset = 0x1000,
  125. .prv_size_override = 0x1018,
  126. };
  127. static const struct lpss_device_desc byt_pwm_dev_desc = {
  128. .flags = LPSS_SAVE_CTX,
  129. };
  130. static const struct lpss_device_desc byt_uart_dev_desc = {
  131. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX,
  132. .clk_con_id = "baudclk",
  133. .prv_offset = 0x800,
  134. .setup = lpss_uart_setup,
  135. };
  136. static const struct lpss_device_desc byt_spi_dev_desc = {
  137. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX,
  138. .prv_offset = 0x400,
  139. };
  140. static const struct lpss_device_desc byt_sdio_dev_desc = {
  141. .flags = LPSS_CLK,
  142. };
  143. static const struct lpss_device_desc byt_i2c_dev_desc = {
  144. .flags = LPSS_CLK | LPSS_SAVE_CTX,
  145. .prv_offset = 0x800,
  146. .setup = byt_i2c_setup,
  147. };
  148. static struct lpss_device_desc bsw_spi_dev_desc = {
  149. .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX,
  150. .prv_offset = 0x400,
  151. .setup = lpss_deassert_reset,
  152. };
  153. #else
  154. #define LPSS_ADDR(desc) (0UL)
  155. #endif /* CONFIG_X86_INTEL_LPSS */
  156. static const struct acpi_device_id acpi_lpss_device_ids[] = {
  157. /* Generic LPSS devices */
  158. { "INTL9C60", LPSS_ADDR(lpss_dma_desc) },
  159. /* Lynxpoint LPSS devices */
  160. { "INT33C0", LPSS_ADDR(lpt_dev_desc) },
  161. { "INT33C1", LPSS_ADDR(lpt_dev_desc) },
  162. { "INT33C2", LPSS_ADDR(lpt_i2c_dev_desc) },
  163. { "INT33C3", LPSS_ADDR(lpt_i2c_dev_desc) },
  164. { "INT33C4", LPSS_ADDR(lpt_uart_dev_desc) },
  165. { "INT33C5", LPSS_ADDR(lpt_uart_dev_desc) },
  166. { "INT33C6", LPSS_ADDR(lpt_sdio_dev_desc) },
  167. { "INT33C7", },
  168. /* BayTrail LPSS devices */
  169. { "80860F09", LPSS_ADDR(byt_pwm_dev_desc) },
  170. { "80860F0A", LPSS_ADDR(byt_uart_dev_desc) },
  171. { "80860F0E", LPSS_ADDR(byt_spi_dev_desc) },
  172. { "80860F14", LPSS_ADDR(byt_sdio_dev_desc) },
  173. { "80860F41", LPSS_ADDR(byt_i2c_dev_desc) },
  174. { "INT33B2", },
  175. { "INT33FC", },
  176. /* Braswell LPSS devices */
  177. { "80862288", LPSS_ADDR(byt_pwm_dev_desc) },
  178. { "8086228A", LPSS_ADDR(byt_uart_dev_desc) },
  179. { "8086228E", LPSS_ADDR(bsw_spi_dev_desc) },
  180. { "808622C1", LPSS_ADDR(byt_i2c_dev_desc) },
  181. { "INT3430", LPSS_ADDR(lpt_dev_desc) },
  182. { "INT3431", LPSS_ADDR(lpt_dev_desc) },
  183. { "INT3432", LPSS_ADDR(lpt_i2c_dev_desc) },
  184. { "INT3433", LPSS_ADDR(lpt_i2c_dev_desc) },
  185. { "INT3434", LPSS_ADDR(lpt_uart_dev_desc) },
  186. { "INT3435", LPSS_ADDR(lpt_uart_dev_desc) },
  187. { "INT3436", LPSS_ADDR(lpt_sdio_dev_desc) },
  188. { "INT3437", },
  189. /* Wildcat Point LPSS devices */
  190. { "INT3438", LPSS_ADDR(lpt_dev_desc) },
  191. { }
  192. };
  193. #ifdef CONFIG_X86_INTEL_LPSS
  194. static int is_memory(struct acpi_resource *res, void *not_used)
  195. {
  196. struct resource r;
  197. return !acpi_dev_resource_memory(res, &r);
  198. }
  199. /* LPSS main clock device. */
  200. static struct platform_device *lpss_clk_dev;
  201. static inline void lpt_register_clock_device(void)
  202. {
  203. lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
  204. }
  205. static int register_device_clock(struct acpi_device *adev,
  206. struct lpss_private_data *pdata)
  207. {
  208. const struct lpss_device_desc *dev_desc = pdata->dev_desc;
  209. const char *devname = dev_name(&adev->dev);
  210. struct clk *clk = ERR_PTR(-ENODEV);
  211. struct lpss_clk_data *clk_data;
  212. const char *parent, *clk_name;
  213. void __iomem *prv_base;
  214. if (!lpss_clk_dev)
  215. lpt_register_clock_device();
  216. clk_data = platform_get_drvdata(lpss_clk_dev);
  217. if (!clk_data)
  218. return -ENODEV;
  219. clk = clk_data->clk;
  220. if (!pdata->mmio_base
  221. || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
  222. return -ENODATA;
  223. parent = clk_data->name;
  224. prv_base = pdata->mmio_base + dev_desc->prv_offset;
  225. if (pdata->fixed_clk_rate) {
  226. clk = clk_register_fixed_rate(NULL, devname, parent, 0,
  227. pdata->fixed_clk_rate);
  228. goto out;
  229. }
  230. if (dev_desc->flags & LPSS_CLK_GATE) {
  231. clk = clk_register_gate(NULL, devname, parent, 0,
  232. prv_base, 0, 0, NULL);
  233. parent = devname;
  234. }
  235. if (dev_desc->flags & LPSS_CLK_DIVIDER) {
  236. /* Prevent division by zero */
  237. if (!readl(prv_base))
  238. writel(LPSS_CLK_DIVIDER_DEF_MASK, prv_base);
  239. clk_name = kasprintf(GFP_KERNEL, "%s-div", devname);
  240. if (!clk_name)
  241. return -ENOMEM;
  242. clk = clk_register_fractional_divider(NULL, clk_name, parent,
  243. 0, prv_base,
  244. 1, 15, 16, 15, 0, NULL);
  245. parent = clk_name;
  246. clk_name = kasprintf(GFP_KERNEL, "%s-update", devname);
  247. if (!clk_name) {
  248. kfree(parent);
  249. return -ENOMEM;
  250. }
  251. clk = clk_register_gate(NULL, clk_name, parent,
  252. CLK_SET_RATE_PARENT | CLK_SET_RATE_GATE,
  253. prv_base, 31, 0, NULL);
  254. kfree(parent);
  255. kfree(clk_name);
  256. }
  257. out:
  258. if (IS_ERR(clk))
  259. return PTR_ERR(clk);
  260. pdata->clk = clk;
  261. clk_register_clkdev(clk, dev_desc->clk_con_id, devname);
  262. return 0;
  263. }
  264. static int acpi_lpss_create_device(struct acpi_device *adev,
  265. const struct acpi_device_id *id)
  266. {
  267. const struct lpss_device_desc *dev_desc;
  268. struct lpss_private_data *pdata;
  269. struct resource_entry *rentry;
  270. struct list_head resource_list;
  271. struct platform_device *pdev;
  272. int ret;
  273. dev_desc = (const struct lpss_device_desc *)id->driver_data;
  274. if (!dev_desc) {
  275. pdev = acpi_create_platform_device(adev);
  276. return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
  277. }
  278. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  279. if (!pdata)
  280. return -ENOMEM;
  281. INIT_LIST_HEAD(&resource_list);
  282. ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
  283. if (ret < 0)
  284. goto err_out;
  285. list_for_each_entry(rentry, &resource_list, node)
  286. if (resource_type(rentry->res) == IORESOURCE_MEM) {
  287. if (dev_desc->prv_size_override)
  288. pdata->mmio_size = dev_desc->prv_size_override;
  289. else
  290. pdata->mmio_size = resource_size(rentry->res);
  291. pdata->mmio_base = ioremap(rentry->res->start,
  292. pdata->mmio_size);
  293. if (!pdata->mmio_base)
  294. goto err_out;
  295. break;
  296. }
  297. acpi_dev_free_resource_list(&resource_list);
  298. pdata->dev_desc = dev_desc;
  299. if (dev_desc->setup)
  300. dev_desc->setup(pdata);
  301. if (dev_desc->flags & LPSS_CLK) {
  302. ret = register_device_clock(adev, pdata);
  303. if (ret) {
  304. /* Skip the device, but continue the namespace scan. */
  305. ret = 0;
  306. goto err_out;
  307. }
  308. }
  309. /*
  310. * This works around a known issue in ACPI tables where LPSS devices
  311. * have _PS0 and _PS3 without _PSC (and no power resources), so
  312. * acpi_bus_init_power() will assume that the BIOS has put them into D0.
  313. */
  314. ret = acpi_device_fix_up_power(adev);
  315. if (ret) {
  316. /* Skip the device, but continue the namespace scan. */
  317. ret = 0;
  318. goto err_out;
  319. }
  320. adev->driver_data = pdata;
  321. pdev = acpi_create_platform_device(adev);
  322. if (!IS_ERR_OR_NULL(pdev)) {
  323. return 1;
  324. }
  325. ret = PTR_ERR(pdev);
  326. adev->driver_data = NULL;
  327. err_out:
  328. kfree(pdata);
  329. return ret;
  330. }
  331. static u32 __lpss_reg_read(struct lpss_private_data *pdata, unsigned int reg)
  332. {
  333. return readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
  334. }
  335. static void __lpss_reg_write(u32 val, struct lpss_private_data *pdata,
  336. unsigned int reg)
  337. {
  338. writel(val, pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
  339. }
  340. static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
  341. {
  342. struct acpi_device *adev;
  343. struct lpss_private_data *pdata;
  344. unsigned long flags;
  345. int ret;
  346. ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
  347. if (WARN_ON(ret))
  348. return ret;
  349. spin_lock_irqsave(&dev->power.lock, flags);
  350. if (pm_runtime_suspended(dev)) {
  351. ret = -EAGAIN;
  352. goto out;
  353. }
  354. pdata = acpi_driver_data(adev);
  355. if (WARN_ON(!pdata || !pdata->mmio_base)) {
  356. ret = -ENODEV;
  357. goto out;
  358. }
  359. *val = __lpss_reg_read(pdata, reg);
  360. out:
  361. spin_unlock_irqrestore(&dev->power.lock, flags);
  362. return ret;
  363. }
  364. static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
  365. char *buf)
  366. {
  367. u32 ltr_value = 0;
  368. unsigned int reg;
  369. int ret;
  370. reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
  371. ret = lpss_reg_read(dev, reg, &ltr_value);
  372. if (ret)
  373. return ret;
  374. return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
  375. }
  376. static ssize_t lpss_ltr_mode_show(struct device *dev,
  377. struct device_attribute *attr, char *buf)
  378. {
  379. u32 ltr_mode = 0;
  380. char *outstr;
  381. int ret;
  382. ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
  383. if (ret)
  384. return ret;
  385. outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
  386. return sprintf(buf, "%s\n", outstr);
  387. }
  388. static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
  389. static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
  390. static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
  391. static struct attribute *lpss_attrs[] = {
  392. &dev_attr_auto_ltr.attr,
  393. &dev_attr_sw_ltr.attr,
  394. &dev_attr_ltr_mode.attr,
  395. NULL,
  396. };
  397. static struct attribute_group lpss_attr_group = {
  398. .attrs = lpss_attrs,
  399. .name = "lpss_ltr",
  400. };
  401. static void acpi_lpss_set_ltr(struct device *dev, s32 val)
  402. {
  403. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  404. u32 ltr_mode, ltr_val;
  405. ltr_mode = __lpss_reg_read(pdata, LPSS_GENERAL);
  406. if (val < 0) {
  407. if (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) {
  408. ltr_mode &= ~LPSS_GENERAL_LTR_MODE_SW;
  409. __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
  410. }
  411. return;
  412. }
  413. ltr_val = __lpss_reg_read(pdata, LPSS_SW_LTR) & ~LPSS_LTR_SNOOP_MASK;
  414. if (val >= LPSS_LTR_SNOOP_LAT_CUTOFF) {
  415. ltr_val |= LPSS_LTR_SNOOP_LAT_32US;
  416. val = LPSS_LTR_MAX_VAL;
  417. } else if (val > LPSS_LTR_MAX_VAL) {
  418. ltr_val |= LPSS_LTR_SNOOP_LAT_32US | LPSS_LTR_SNOOP_REQ;
  419. val >>= LPSS_LTR_SNOOP_LAT_SHIFT;
  420. } else {
  421. ltr_val |= LPSS_LTR_SNOOP_LAT_1US | LPSS_LTR_SNOOP_REQ;
  422. }
  423. ltr_val |= val;
  424. __lpss_reg_write(ltr_val, pdata, LPSS_SW_LTR);
  425. if (!(ltr_mode & LPSS_GENERAL_LTR_MODE_SW)) {
  426. ltr_mode |= LPSS_GENERAL_LTR_MODE_SW;
  427. __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
  428. }
  429. }
  430. #ifdef CONFIG_PM
  431. /**
  432. * acpi_lpss_save_ctx() - Save the private registers of LPSS device
  433. * @dev: LPSS device
  434. * @pdata: pointer to the private data of the LPSS device
  435. *
  436. * Most LPSS devices have private registers which may loose their context when
  437. * the device is powered down. acpi_lpss_save_ctx() saves those registers into
  438. * prv_reg_ctx array.
  439. */
  440. static void acpi_lpss_save_ctx(struct device *dev,
  441. struct lpss_private_data *pdata)
  442. {
  443. unsigned int i;
  444. for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
  445. unsigned long offset = i * sizeof(u32);
  446. pdata->prv_reg_ctx[i] = __lpss_reg_read(pdata, offset);
  447. dev_dbg(dev, "saving 0x%08x from LPSS reg at offset 0x%02lx\n",
  448. pdata->prv_reg_ctx[i], offset);
  449. }
  450. }
  451. /**
  452. * acpi_lpss_restore_ctx() - Restore the private registers of LPSS device
  453. * @dev: LPSS device
  454. * @pdata: pointer to the private data of the LPSS device
  455. *
  456. * Restores the registers that were previously stored with acpi_lpss_save_ctx().
  457. */
  458. static void acpi_lpss_restore_ctx(struct device *dev,
  459. struct lpss_private_data *pdata)
  460. {
  461. unsigned int i;
  462. /*
  463. * The following delay is needed or the subsequent write operations may
  464. * fail. The LPSS devices are actually PCI devices and the PCI spec
  465. * expects 10ms delay before the device can be accessed after D3 to D0
  466. * transition.
  467. */
  468. msleep(10);
  469. for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
  470. unsigned long offset = i * sizeof(u32);
  471. __lpss_reg_write(pdata->prv_reg_ctx[i], pdata, offset);
  472. dev_dbg(dev, "restoring 0x%08x to LPSS reg at offset 0x%02lx\n",
  473. pdata->prv_reg_ctx[i], offset);
  474. }
  475. }
  476. #ifdef CONFIG_PM_SLEEP
  477. static int acpi_lpss_suspend_late(struct device *dev)
  478. {
  479. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  480. int ret;
  481. ret = pm_generic_suspend_late(dev);
  482. if (ret)
  483. return ret;
  484. if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
  485. acpi_lpss_save_ctx(dev, pdata);
  486. return acpi_dev_suspend_late(dev);
  487. }
  488. static int acpi_lpss_resume_early(struct device *dev)
  489. {
  490. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  491. int ret;
  492. ret = acpi_dev_resume_early(dev);
  493. if (ret)
  494. return ret;
  495. if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
  496. acpi_lpss_restore_ctx(dev, pdata);
  497. return pm_generic_resume_early(dev);
  498. }
  499. #endif /* CONFIG_PM_SLEEP */
  500. static int acpi_lpss_runtime_suspend(struct device *dev)
  501. {
  502. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  503. int ret;
  504. ret = pm_generic_runtime_suspend(dev);
  505. if (ret)
  506. return ret;
  507. if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
  508. acpi_lpss_save_ctx(dev, pdata);
  509. return acpi_dev_runtime_suspend(dev);
  510. }
  511. static int acpi_lpss_runtime_resume(struct device *dev)
  512. {
  513. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  514. int ret;
  515. ret = acpi_dev_runtime_resume(dev);
  516. if (ret)
  517. return ret;
  518. if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
  519. acpi_lpss_restore_ctx(dev, pdata);
  520. return pm_generic_runtime_resume(dev);
  521. }
  522. #endif /* CONFIG_PM */
  523. static struct dev_pm_domain acpi_lpss_pm_domain = {
  524. .ops = {
  525. #ifdef CONFIG_PM
  526. #ifdef CONFIG_PM_SLEEP
  527. .prepare = acpi_subsys_prepare,
  528. .complete = acpi_subsys_complete,
  529. .suspend = acpi_subsys_suspend,
  530. .suspend_late = acpi_lpss_suspend_late,
  531. .resume_early = acpi_lpss_resume_early,
  532. .freeze = acpi_subsys_freeze,
  533. .poweroff = acpi_subsys_suspend,
  534. .poweroff_late = acpi_lpss_suspend_late,
  535. .restore_early = acpi_lpss_resume_early,
  536. #endif
  537. .runtime_suspend = acpi_lpss_runtime_suspend,
  538. .runtime_resume = acpi_lpss_runtime_resume,
  539. #endif
  540. },
  541. };
  542. static int acpi_lpss_platform_notify(struct notifier_block *nb,
  543. unsigned long action, void *data)
  544. {
  545. struct platform_device *pdev = to_platform_device(data);
  546. struct lpss_private_data *pdata;
  547. struct acpi_device *adev;
  548. const struct acpi_device_id *id;
  549. id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
  550. if (!id || !id->driver_data)
  551. return 0;
  552. if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
  553. return 0;
  554. pdata = acpi_driver_data(adev);
  555. if (!pdata)
  556. return 0;
  557. if (pdata->mmio_base &&
  558. pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
  559. dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
  560. return 0;
  561. }
  562. switch (action) {
  563. case BUS_NOTIFY_ADD_DEVICE:
  564. pdev->dev.pm_domain = &acpi_lpss_pm_domain;
  565. if (pdata->dev_desc->flags & LPSS_LTR)
  566. return sysfs_create_group(&pdev->dev.kobj,
  567. &lpss_attr_group);
  568. break;
  569. case BUS_NOTIFY_DEL_DEVICE:
  570. if (pdata->dev_desc->flags & LPSS_LTR)
  571. sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
  572. pdev->dev.pm_domain = NULL;
  573. break;
  574. default:
  575. break;
  576. }
  577. return 0;
  578. }
  579. static struct notifier_block acpi_lpss_nb = {
  580. .notifier_call = acpi_lpss_platform_notify,
  581. };
  582. static void acpi_lpss_bind(struct device *dev)
  583. {
  584. struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
  585. if (!pdata || !pdata->mmio_base || !(pdata->dev_desc->flags & LPSS_LTR))
  586. return;
  587. if (pdata->mmio_size >= pdata->dev_desc->prv_offset + LPSS_LTR_SIZE)
  588. dev->power.set_latency_tolerance = acpi_lpss_set_ltr;
  589. else
  590. dev_err(dev, "MMIO size insufficient to access LTR\n");
  591. }
  592. static void acpi_lpss_unbind(struct device *dev)
  593. {
  594. dev->power.set_latency_tolerance = NULL;
  595. }
  596. static struct acpi_scan_handler lpss_handler = {
  597. .ids = acpi_lpss_device_ids,
  598. .attach = acpi_lpss_create_device,
  599. .bind = acpi_lpss_bind,
  600. .unbind = acpi_lpss_unbind,
  601. };
  602. void __init acpi_lpss_init(void)
  603. {
  604. if (!lpt_clk_init()) {
  605. bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
  606. acpi_scan_add_handler(&lpss_handler);
  607. }
  608. }
  609. #else
  610. static struct acpi_scan_handler lpss_handler = {
  611. .ids = acpi_lpss_device_ids,
  612. };
  613. void __init acpi_lpss_init(void)
  614. {
  615. acpi_scan_add_handler(&lpss_handler);
  616. }
  617. #endif /* CONFIG_X86_INTEL_LPSS */