intel-lpss.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Intel Sunrisepoint LPSS core support.
  3. *
  4. * Copyright (C) 2015, Intel Corporation
  5. *
  6. * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  7. * Mika Westerberg <mika.westerberg@linux.intel.com>
  8. * Heikki Krogerus <heikki.krogerus@linux.intel.com>
  9. * Jarkko Nikula <jarkko.nikula@linux.intel.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/clkdev.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/idr.h>
  20. #include <linux/ioport.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/mfd/core.h>
  24. #include <linux/pm_qos.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/property.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/io-64-nonatomic-lo-hi.h>
  29. #include "intel-lpss.h"
  30. #define LPSS_DEV_OFFSET 0x000
  31. #define LPSS_DEV_SIZE 0x200
  32. #define LPSS_PRIV_OFFSET 0x200
  33. #define LPSS_PRIV_SIZE 0x100
  34. #define LPSS_IDMA64_OFFSET 0x800
  35. #define LPSS_IDMA64_SIZE 0x800
  36. /* Offsets from lpss->priv */
  37. #define LPSS_PRIV_RESETS 0x04
  38. #define LPSS_PRIV_RESETS_FUNC BIT(2)
  39. #define LPSS_PRIV_RESETS_IDMA 0x3
  40. #define LPSS_PRIV_ACTIVELTR 0x10
  41. #define LPSS_PRIV_IDLELTR 0x14
  42. #define LPSS_PRIV_LTR_REQ BIT(15)
  43. #define LPSS_PRIV_LTR_SCALE_MASK 0xc00
  44. #define LPSS_PRIV_LTR_SCALE_1US 0x800
  45. #define LPSS_PRIV_LTR_SCALE_32US 0xc00
  46. #define LPSS_PRIV_LTR_VALUE_MASK 0x3ff
  47. #define LPSS_PRIV_SSP_REG 0x20
  48. #define LPSS_PRIV_SSP_REG_DIS_DMA_FIN BIT(0)
  49. #define LPSS_PRIV_REMAP_ADDR 0x40
  50. #define LPSS_PRIV_CAPS 0xfc
  51. #define LPSS_PRIV_CAPS_NO_IDMA BIT(8)
  52. #define LPSS_PRIV_CAPS_TYPE_SHIFT 4
  53. #define LPSS_PRIV_CAPS_TYPE_MASK (0xf << LPSS_PRIV_CAPS_TYPE_SHIFT)
  54. /* This matches the type field in CAPS register */
  55. enum intel_lpss_dev_type {
  56. LPSS_DEV_I2C = 0,
  57. LPSS_DEV_UART,
  58. LPSS_DEV_SPI,
  59. };
  60. struct intel_lpss {
  61. const struct intel_lpss_platform_info *info;
  62. enum intel_lpss_dev_type type;
  63. struct clk *clk;
  64. struct clk_lookup *clock;
  65. struct mfd_cell *cell;
  66. struct device *dev;
  67. void __iomem *priv;
  68. int devid;
  69. u32 caps;
  70. u32 active_ltr;
  71. u32 idle_ltr;
  72. struct dentry *debugfs;
  73. };
  74. static const struct resource intel_lpss_dev_resources[] = {
  75. DEFINE_RES_MEM_NAMED(LPSS_DEV_OFFSET, LPSS_DEV_SIZE, "lpss_dev"),
  76. DEFINE_RES_MEM_NAMED(LPSS_PRIV_OFFSET, LPSS_PRIV_SIZE, "lpss_priv"),
  77. DEFINE_RES_IRQ(0),
  78. };
  79. static const struct resource intel_lpss_idma64_resources[] = {
  80. DEFINE_RES_MEM(LPSS_IDMA64_OFFSET, LPSS_IDMA64_SIZE),
  81. DEFINE_RES_IRQ(0),
  82. };
  83. #define LPSS_IDMA64_DRIVER_NAME "idma64"
  84. /*
  85. * Cells needs to be ordered so that the iDMA is created first. This is
  86. * because we need to be sure the DMA is available when the host controller
  87. * driver is probed.
  88. */
  89. static const struct mfd_cell intel_lpss_idma64_cell = {
  90. .name = LPSS_IDMA64_DRIVER_NAME,
  91. .num_resources = ARRAY_SIZE(intel_lpss_idma64_resources),
  92. .resources = intel_lpss_idma64_resources,
  93. };
  94. static const struct mfd_cell intel_lpss_i2c_cell = {
  95. .name = "i2c_designware",
  96. .num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
  97. .resources = intel_lpss_dev_resources,
  98. };
  99. static const struct mfd_cell intel_lpss_uart_cell = {
  100. .name = "dw-apb-uart",
  101. .num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
  102. .resources = intel_lpss_dev_resources,
  103. };
  104. static const struct mfd_cell intel_lpss_spi_cell = {
  105. .name = "pxa2xx-spi",
  106. .num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
  107. .resources = intel_lpss_dev_resources,
  108. };
  109. static DEFINE_IDA(intel_lpss_devid_ida);
  110. static struct dentry *intel_lpss_debugfs;
  111. static int intel_lpss_request_dma_module(const char *name)
  112. {
  113. static bool intel_lpss_dma_requested;
  114. if (intel_lpss_dma_requested)
  115. return 0;
  116. intel_lpss_dma_requested = true;
  117. return request_module("%s", name);
  118. }
  119. static void intel_lpss_cache_ltr(struct intel_lpss *lpss)
  120. {
  121. lpss->active_ltr = readl(lpss->priv + LPSS_PRIV_ACTIVELTR);
  122. lpss->idle_ltr = readl(lpss->priv + LPSS_PRIV_IDLELTR);
  123. }
  124. static int intel_lpss_debugfs_add(struct intel_lpss *lpss)
  125. {
  126. struct dentry *dir;
  127. dir = debugfs_create_dir(dev_name(lpss->dev), intel_lpss_debugfs);
  128. if (IS_ERR(dir))
  129. return PTR_ERR(dir);
  130. /* Cache the values into lpss structure */
  131. intel_lpss_cache_ltr(lpss);
  132. debugfs_create_x32("capabilities", S_IRUGO, dir, &lpss->caps);
  133. debugfs_create_x32("active_ltr", S_IRUGO, dir, &lpss->active_ltr);
  134. debugfs_create_x32("idle_ltr", S_IRUGO, dir, &lpss->idle_ltr);
  135. lpss->debugfs = dir;
  136. return 0;
  137. }
  138. static void intel_lpss_debugfs_remove(struct intel_lpss *lpss)
  139. {
  140. debugfs_remove_recursive(lpss->debugfs);
  141. }
  142. static void intel_lpss_ltr_set(struct device *dev, s32 val)
  143. {
  144. struct intel_lpss *lpss = dev_get_drvdata(dev);
  145. u32 ltr;
  146. /*
  147. * Program latency tolerance (LTR) accordingly what has been asked
  148. * by the PM QoS layer or disable it in case we were passed
  149. * negative value or PM_QOS_LATENCY_ANY.
  150. */
  151. ltr = readl(lpss->priv + LPSS_PRIV_ACTIVELTR);
  152. if (val == PM_QOS_LATENCY_ANY || val < 0) {
  153. ltr &= ~LPSS_PRIV_LTR_REQ;
  154. } else {
  155. ltr |= LPSS_PRIV_LTR_REQ;
  156. ltr &= ~LPSS_PRIV_LTR_SCALE_MASK;
  157. ltr &= ~LPSS_PRIV_LTR_VALUE_MASK;
  158. if (val > LPSS_PRIV_LTR_VALUE_MASK)
  159. ltr |= LPSS_PRIV_LTR_SCALE_32US | val >> 5;
  160. else
  161. ltr |= LPSS_PRIV_LTR_SCALE_1US | val;
  162. }
  163. if (ltr == lpss->active_ltr)
  164. return;
  165. writel(ltr, lpss->priv + LPSS_PRIV_ACTIVELTR);
  166. writel(ltr, lpss->priv + LPSS_PRIV_IDLELTR);
  167. /* Cache the values into lpss structure */
  168. intel_lpss_cache_ltr(lpss);
  169. }
  170. static void intel_lpss_ltr_expose(struct intel_lpss *lpss)
  171. {
  172. lpss->dev->power.set_latency_tolerance = intel_lpss_ltr_set;
  173. dev_pm_qos_expose_latency_tolerance(lpss->dev);
  174. }
  175. static void intel_lpss_ltr_hide(struct intel_lpss *lpss)
  176. {
  177. dev_pm_qos_hide_latency_tolerance(lpss->dev);
  178. lpss->dev->power.set_latency_tolerance = NULL;
  179. }
  180. static int intel_lpss_assign_devs(struct intel_lpss *lpss)
  181. {
  182. const struct mfd_cell *cell;
  183. unsigned int type;
  184. type = lpss->caps & LPSS_PRIV_CAPS_TYPE_MASK;
  185. type >>= LPSS_PRIV_CAPS_TYPE_SHIFT;
  186. switch (type) {
  187. case LPSS_DEV_I2C:
  188. cell = &intel_lpss_i2c_cell;
  189. break;
  190. case LPSS_DEV_UART:
  191. cell = &intel_lpss_uart_cell;
  192. break;
  193. case LPSS_DEV_SPI:
  194. cell = &intel_lpss_spi_cell;
  195. break;
  196. default:
  197. return -ENODEV;
  198. }
  199. lpss->cell = devm_kmemdup(lpss->dev, cell, sizeof(*cell), GFP_KERNEL);
  200. if (!lpss->cell)
  201. return -ENOMEM;
  202. lpss->type = type;
  203. return 0;
  204. }
  205. static bool intel_lpss_has_idma(const struct intel_lpss *lpss)
  206. {
  207. return (lpss->caps & LPSS_PRIV_CAPS_NO_IDMA) == 0;
  208. }
  209. static void intel_lpss_set_remap_addr(const struct intel_lpss *lpss)
  210. {
  211. resource_size_t addr = lpss->info->mem->start;
  212. lo_hi_writeq(addr, lpss->priv + LPSS_PRIV_REMAP_ADDR);
  213. }
  214. static void intel_lpss_deassert_reset(const struct intel_lpss *lpss)
  215. {
  216. u32 value = LPSS_PRIV_RESETS_FUNC | LPSS_PRIV_RESETS_IDMA;
  217. /* Bring out the device from reset */
  218. writel(value, lpss->priv + LPSS_PRIV_RESETS);
  219. }
  220. static void intel_lpss_init_dev(const struct intel_lpss *lpss)
  221. {
  222. u32 value = LPSS_PRIV_SSP_REG_DIS_DMA_FIN;
  223. intel_lpss_deassert_reset(lpss);
  224. if (!intel_lpss_has_idma(lpss))
  225. return;
  226. intel_lpss_set_remap_addr(lpss);
  227. /* Make sure that SPI multiblock DMA transfers are re-enabled */
  228. if (lpss->type == LPSS_DEV_SPI)
  229. writel(value, lpss->priv + LPSS_PRIV_SSP_REG);
  230. }
  231. static void intel_lpss_unregister_clock_tree(struct clk *clk)
  232. {
  233. struct clk *parent;
  234. while (clk) {
  235. parent = clk_get_parent(clk);
  236. clk_unregister(clk);
  237. clk = parent;
  238. }
  239. }
  240. static int intel_lpss_register_clock_divider(struct intel_lpss *lpss,
  241. const char *devname,
  242. struct clk **clk)
  243. {
  244. char name[32];
  245. struct clk *tmp = *clk;
  246. snprintf(name, sizeof(name), "%s-enable", devname);
  247. tmp = clk_register_gate(NULL, name, __clk_get_name(tmp), 0,
  248. lpss->priv, 0, 0, NULL);
  249. if (IS_ERR(tmp))
  250. return PTR_ERR(tmp);
  251. snprintf(name, sizeof(name), "%s-div", devname);
  252. tmp = clk_register_fractional_divider(NULL, name, __clk_get_name(tmp),
  253. 0, lpss->priv, 1, 15, 16, 15, 0,
  254. NULL);
  255. if (IS_ERR(tmp))
  256. return PTR_ERR(tmp);
  257. *clk = tmp;
  258. snprintf(name, sizeof(name), "%s-update", devname);
  259. tmp = clk_register_gate(NULL, name, __clk_get_name(tmp),
  260. CLK_SET_RATE_PARENT, lpss->priv, 31, 0, NULL);
  261. if (IS_ERR(tmp))
  262. return PTR_ERR(tmp);
  263. *clk = tmp;
  264. return 0;
  265. }
  266. static int intel_lpss_register_clock(struct intel_lpss *lpss)
  267. {
  268. const struct mfd_cell *cell = lpss->cell;
  269. struct clk *clk;
  270. char devname[24];
  271. int ret;
  272. if (!lpss->info->clk_rate)
  273. return 0;
  274. /* Root clock */
  275. clk = clk_register_fixed_rate(NULL, dev_name(lpss->dev), NULL,
  276. CLK_IS_ROOT, lpss->info->clk_rate);
  277. if (IS_ERR(clk))
  278. return PTR_ERR(clk);
  279. snprintf(devname, sizeof(devname), "%s.%d", cell->name, lpss->devid);
  280. /*
  281. * Support for clock divider only if it has some preset value.
  282. * Otherwise we assume that the divider is not used.
  283. */
  284. if (lpss->type != LPSS_DEV_I2C) {
  285. ret = intel_lpss_register_clock_divider(lpss, devname, &clk);
  286. if (ret)
  287. goto err_clk_register;
  288. }
  289. ret = -ENOMEM;
  290. /* Clock for the host controller */
  291. lpss->clock = clkdev_create(clk, lpss->info->clk_con_id, "%s", devname);
  292. if (!lpss->clock)
  293. goto err_clk_register;
  294. lpss->clk = clk;
  295. return 0;
  296. err_clk_register:
  297. intel_lpss_unregister_clock_tree(clk);
  298. return ret;
  299. }
  300. static void intel_lpss_unregister_clock(struct intel_lpss *lpss)
  301. {
  302. if (IS_ERR_OR_NULL(lpss->clk))
  303. return;
  304. clkdev_drop(lpss->clock);
  305. intel_lpss_unregister_clock_tree(lpss->clk);
  306. }
  307. int intel_lpss_probe(struct device *dev,
  308. const struct intel_lpss_platform_info *info)
  309. {
  310. struct intel_lpss *lpss;
  311. int ret;
  312. if (!info || !info->mem || info->irq <= 0)
  313. return -EINVAL;
  314. lpss = devm_kzalloc(dev, sizeof(*lpss), GFP_KERNEL);
  315. if (!lpss)
  316. return -ENOMEM;
  317. lpss->priv = devm_ioremap(dev, info->mem->start + LPSS_PRIV_OFFSET,
  318. LPSS_PRIV_SIZE);
  319. if (!lpss->priv)
  320. return -ENOMEM;
  321. lpss->info = info;
  322. lpss->dev = dev;
  323. lpss->caps = readl(lpss->priv + LPSS_PRIV_CAPS);
  324. dev_set_drvdata(dev, lpss);
  325. ret = intel_lpss_assign_devs(lpss);
  326. if (ret)
  327. return ret;
  328. lpss->cell->pset = info->pset;
  329. intel_lpss_init_dev(lpss);
  330. lpss->devid = ida_simple_get(&intel_lpss_devid_ida, 0, 0, GFP_KERNEL);
  331. if (lpss->devid < 0)
  332. return lpss->devid;
  333. ret = intel_lpss_register_clock(lpss);
  334. if (ret)
  335. goto err_clk_register;
  336. intel_lpss_ltr_expose(lpss);
  337. ret = intel_lpss_debugfs_add(lpss);
  338. if (ret)
  339. dev_warn(dev, "Failed to create debugfs entries\n");
  340. if (intel_lpss_has_idma(lpss)) {
  341. /*
  342. * Ensure the DMA driver is loaded before the host
  343. * controller device appears, so that the host controller
  344. * driver can request its DMA channels as early as
  345. * possible.
  346. *
  347. * If the DMA module is not there that's OK as well.
  348. */
  349. intel_lpss_request_dma_module(LPSS_IDMA64_DRIVER_NAME);
  350. ret = mfd_add_devices(dev, lpss->devid, &intel_lpss_idma64_cell,
  351. 1, info->mem, info->irq, NULL);
  352. if (ret)
  353. dev_warn(dev, "Failed to add %s, fallback to PIO\n",
  354. LPSS_IDMA64_DRIVER_NAME);
  355. }
  356. ret = mfd_add_devices(dev, lpss->devid, lpss->cell,
  357. 1, info->mem, info->irq, NULL);
  358. if (ret)
  359. goto err_remove_ltr;
  360. return 0;
  361. err_remove_ltr:
  362. intel_lpss_debugfs_remove(lpss);
  363. intel_lpss_ltr_hide(lpss);
  364. intel_lpss_unregister_clock(lpss);
  365. err_clk_register:
  366. ida_simple_remove(&intel_lpss_devid_ida, lpss->devid);
  367. return ret;
  368. }
  369. EXPORT_SYMBOL_GPL(intel_lpss_probe);
  370. void intel_lpss_remove(struct device *dev)
  371. {
  372. struct intel_lpss *lpss = dev_get_drvdata(dev);
  373. mfd_remove_devices(dev);
  374. intel_lpss_debugfs_remove(lpss);
  375. intel_lpss_ltr_hide(lpss);
  376. intel_lpss_unregister_clock(lpss);
  377. ida_simple_remove(&intel_lpss_devid_ida, lpss->devid);
  378. }
  379. EXPORT_SYMBOL_GPL(intel_lpss_remove);
  380. static int resume_lpss_device(struct device *dev, void *data)
  381. {
  382. pm_runtime_resume(dev);
  383. return 0;
  384. }
  385. int intel_lpss_prepare(struct device *dev)
  386. {
  387. /*
  388. * Resume both child devices before entering system sleep. This
  389. * ensures that they are in proper state before they get suspended.
  390. */
  391. device_for_each_child_reverse(dev, NULL, resume_lpss_device);
  392. return 0;
  393. }
  394. EXPORT_SYMBOL_GPL(intel_lpss_prepare);
  395. int intel_lpss_suspend(struct device *dev)
  396. {
  397. return 0;
  398. }
  399. EXPORT_SYMBOL_GPL(intel_lpss_suspend);
  400. int intel_lpss_resume(struct device *dev)
  401. {
  402. struct intel_lpss *lpss = dev_get_drvdata(dev);
  403. intel_lpss_init_dev(lpss);
  404. return 0;
  405. }
  406. EXPORT_SYMBOL_GPL(intel_lpss_resume);
  407. static int __init intel_lpss_init(void)
  408. {
  409. intel_lpss_debugfs = debugfs_create_dir("intel_lpss", NULL);
  410. return 0;
  411. }
  412. module_init(intel_lpss_init);
  413. static void __exit intel_lpss_exit(void)
  414. {
  415. debugfs_remove(intel_lpss_debugfs);
  416. }
  417. module_exit(intel_lpss_exit);
  418. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  419. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  420. MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
  421. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@linux.intel.com>");
  422. MODULE_DESCRIPTION("Intel LPSS core driver");
  423. MODULE_LICENSE("GPL v2");