rtc-st-lpc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * rtc-st-lpc.c - ST's LPC RTC, powered by the Low Power Timer
  3. *
  4. * Copyright (C) 2014 STMicroelectronics Limited
  5. *
  6. * Author: David Paris <david.paris@st.com> for STMicroelectronics
  7. * Lee Jones <lee.jones@linaro.org> for STMicroelectronics
  8. *
  9. * Based on the original driver written by Stuart Menefy.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public Licence
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the Licence, or (at your option) any later version.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/irq.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/rtc.h>
  27. #include <dt-bindings/mfd/st-lpc.h>
  28. /* Low Power Timer */
  29. #define LPC_LPT_LSB_OFF 0x400
  30. #define LPC_LPT_MSB_OFF 0x404
  31. #define LPC_LPT_START_OFF 0x408
  32. /* Low Power Alarm */
  33. #define LPC_LPA_LSB_OFF 0x410
  34. #define LPC_LPA_MSB_OFF 0x414
  35. #define LPC_LPA_START_OFF 0x418
  36. /* LPC as WDT */
  37. #define LPC_WDT_OFF 0x510
  38. #define LPC_WDT_FLAG_OFF 0x514
  39. struct st_rtc {
  40. struct rtc_device *rtc_dev;
  41. struct rtc_wkalrm alarm;
  42. struct resource *res;
  43. struct clk *clk;
  44. unsigned long clkrate;
  45. void __iomem *ioaddr;
  46. bool irq_enabled:1;
  47. spinlock_t lock;
  48. short irq;
  49. };
  50. static void st_rtc_set_hw_alarm(struct st_rtc *rtc,
  51. unsigned long msb, unsigned long lsb)
  52. {
  53. unsigned long flags;
  54. spin_lock_irqsave(&rtc->lock, flags);
  55. writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
  56. writel_relaxed(msb, rtc->ioaddr + LPC_LPA_MSB_OFF);
  57. writel_relaxed(lsb, rtc->ioaddr + LPC_LPA_LSB_OFF);
  58. writel_relaxed(1, rtc->ioaddr + LPC_LPA_START_OFF);
  59. writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
  60. spin_unlock_irqrestore(&rtc->lock, flags);
  61. }
  62. static irqreturn_t st_rtc_handler(int this_irq, void *data)
  63. {
  64. struct st_rtc *rtc = (struct st_rtc *)data;
  65. rtc_update_irq(rtc->rtc_dev, 1, RTC_AF);
  66. return IRQ_HANDLED;
  67. }
  68. static int st_rtc_read_time(struct device *dev, struct rtc_time *tm)
  69. {
  70. struct st_rtc *rtc = dev_get_drvdata(dev);
  71. unsigned long lpt_lsb, lpt_msb;
  72. unsigned long long lpt;
  73. unsigned long flags;
  74. spin_lock_irqsave(&rtc->lock, flags);
  75. do {
  76. lpt_msb = readl_relaxed(rtc->ioaddr + LPC_LPT_MSB_OFF);
  77. lpt_lsb = readl_relaxed(rtc->ioaddr + LPC_LPT_LSB_OFF);
  78. } while (readl_relaxed(rtc->ioaddr + LPC_LPT_MSB_OFF) != lpt_msb);
  79. spin_unlock_irqrestore(&rtc->lock, flags);
  80. lpt = ((unsigned long long)lpt_msb << 32) | lpt_lsb;
  81. do_div(lpt, rtc->clkrate);
  82. rtc_time64_to_tm(lpt, tm);
  83. return 0;
  84. }
  85. static int st_rtc_set_time(struct device *dev, struct rtc_time *tm)
  86. {
  87. struct st_rtc *rtc = dev_get_drvdata(dev);
  88. unsigned long long lpt, secs;
  89. unsigned long flags;
  90. secs = rtc_tm_to_time64(tm);
  91. lpt = (unsigned long long)secs * rtc->clkrate;
  92. spin_lock_irqsave(&rtc->lock, flags);
  93. writel_relaxed(lpt >> 32, rtc->ioaddr + LPC_LPT_MSB_OFF);
  94. writel_relaxed(lpt, rtc->ioaddr + LPC_LPT_LSB_OFF);
  95. writel_relaxed(1, rtc->ioaddr + LPC_LPT_START_OFF);
  96. spin_unlock_irqrestore(&rtc->lock, flags);
  97. return 0;
  98. }
  99. static int st_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  100. {
  101. struct st_rtc *rtc = dev_get_drvdata(dev);
  102. unsigned long flags;
  103. spin_lock_irqsave(&rtc->lock, flags);
  104. memcpy(wkalrm, &rtc->alarm, sizeof(struct rtc_wkalrm));
  105. spin_unlock_irqrestore(&rtc->lock, flags);
  106. return 0;
  107. }
  108. static int st_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  109. {
  110. struct st_rtc *rtc = dev_get_drvdata(dev);
  111. if (enabled && !rtc->irq_enabled) {
  112. enable_irq(rtc->irq);
  113. rtc->irq_enabled = true;
  114. } else if (!enabled && rtc->irq_enabled) {
  115. disable_irq(rtc->irq);
  116. rtc->irq_enabled = false;
  117. }
  118. return 0;
  119. }
  120. static int st_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  121. {
  122. struct st_rtc *rtc = dev_get_drvdata(dev);
  123. struct rtc_time now;
  124. unsigned long long now_secs;
  125. unsigned long long alarm_secs;
  126. unsigned long long lpa;
  127. st_rtc_read_time(dev, &now);
  128. now_secs = rtc_tm_to_time64(&now);
  129. alarm_secs = rtc_tm_to_time64(&t->time);
  130. /* Invalid alarm time */
  131. if (now_secs > alarm_secs)
  132. return -EINVAL;
  133. memcpy(&rtc->alarm, t, sizeof(struct rtc_wkalrm));
  134. /* Now many secs to fire */
  135. alarm_secs -= now_secs;
  136. lpa = (unsigned long long)alarm_secs * rtc->clkrate;
  137. st_rtc_set_hw_alarm(rtc, lpa >> 32, lpa);
  138. st_rtc_alarm_irq_enable(dev, t->enabled);
  139. return 0;
  140. }
  141. static struct rtc_class_ops st_rtc_ops = {
  142. .read_time = st_rtc_read_time,
  143. .set_time = st_rtc_set_time,
  144. .read_alarm = st_rtc_read_alarm,
  145. .set_alarm = st_rtc_set_alarm,
  146. .alarm_irq_enable = st_rtc_alarm_irq_enable,
  147. };
  148. static int st_rtc_probe(struct platform_device *pdev)
  149. {
  150. struct device_node *np = pdev->dev.of_node;
  151. struct st_rtc *rtc;
  152. struct resource *res;
  153. uint32_t mode;
  154. int ret = 0;
  155. ret = of_property_read_u32(np, "st,lpc-mode", &mode);
  156. if (ret) {
  157. dev_err(&pdev->dev, "An LPC mode must be provided\n");
  158. return -EINVAL;
  159. }
  160. /* LPC can either run as a Clocksource or in RTC or WDT mode */
  161. if (mode != ST_LPC_MODE_RTC)
  162. return -ENODEV;
  163. rtc = devm_kzalloc(&pdev->dev, sizeof(struct st_rtc), GFP_KERNEL);
  164. if (!rtc)
  165. return -ENOMEM;
  166. rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
  167. if (IS_ERR(rtc->rtc_dev))
  168. return PTR_ERR(rtc->rtc_dev);
  169. spin_lock_init(&rtc->lock);
  170. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. rtc->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  172. if (IS_ERR(rtc->ioaddr))
  173. return PTR_ERR(rtc->ioaddr);
  174. rtc->irq = irq_of_parse_and_map(np, 0);
  175. if (!rtc->irq) {
  176. dev_err(&pdev->dev, "IRQ missing or invalid\n");
  177. return -EINVAL;
  178. }
  179. ret = devm_request_irq(&pdev->dev, rtc->irq, st_rtc_handler, 0,
  180. pdev->name, rtc);
  181. if (ret) {
  182. dev_err(&pdev->dev, "Failed to request irq %i\n", rtc->irq);
  183. return ret;
  184. }
  185. enable_irq_wake(rtc->irq);
  186. disable_irq(rtc->irq);
  187. rtc->clk = clk_get(&pdev->dev, NULL);
  188. if (IS_ERR(rtc->clk)) {
  189. dev_err(&pdev->dev, "Unable to request clock\n");
  190. return PTR_ERR(rtc->clk);
  191. }
  192. clk_prepare_enable(rtc->clk);
  193. rtc->clkrate = clk_get_rate(rtc->clk);
  194. if (!rtc->clkrate) {
  195. dev_err(&pdev->dev, "Unable to fetch clock rate\n");
  196. return -EINVAL;
  197. }
  198. device_set_wakeup_capable(&pdev->dev, 1);
  199. platform_set_drvdata(pdev, rtc);
  200. rtc->rtc_dev->ops = &st_rtc_ops;
  201. rtc->rtc_dev->range_max = U64_MAX;
  202. do_div(rtc->rtc_dev->range_max, rtc->clkrate);
  203. ret = rtc_register_device(rtc->rtc_dev);
  204. if (ret) {
  205. clk_disable_unprepare(rtc->clk);
  206. return ret;
  207. }
  208. return 0;
  209. }
  210. #ifdef CONFIG_PM_SLEEP
  211. static int st_rtc_suspend(struct device *dev)
  212. {
  213. struct st_rtc *rtc = dev_get_drvdata(dev);
  214. if (device_may_wakeup(dev))
  215. return 0;
  216. writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
  217. writel_relaxed(0, rtc->ioaddr + LPC_LPA_START_OFF);
  218. writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
  219. return 0;
  220. }
  221. static int st_rtc_resume(struct device *dev)
  222. {
  223. struct st_rtc *rtc = dev_get_drvdata(dev);
  224. rtc_alarm_irq_enable(rtc->rtc_dev, 0);
  225. /*
  226. * clean 'rtc->alarm' to allow a new
  227. * .set_alarm to the upper RTC layer
  228. */
  229. memset(&rtc->alarm, 0, sizeof(struct rtc_wkalrm));
  230. writel_relaxed(0, rtc->ioaddr + LPC_LPA_MSB_OFF);
  231. writel_relaxed(0, rtc->ioaddr + LPC_LPA_LSB_OFF);
  232. writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
  233. writel_relaxed(1, rtc->ioaddr + LPC_LPA_START_OFF);
  234. writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
  235. return 0;
  236. }
  237. #endif
  238. static SIMPLE_DEV_PM_OPS(st_rtc_pm_ops, st_rtc_suspend, st_rtc_resume);
  239. static const struct of_device_id st_rtc_match[] = {
  240. { .compatible = "st,stih407-lpc" },
  241. {}
  242. };
  243. MODULE_DEVICE_TABLE(of, st_rtc_match);
  244. static struct platform_driver st_rtc_platform_driver = {
  245. .driver = {
  246. .name = "st-lpc-rtc",
  247. .pm = &st_rtc_pm_ops,
  248. .of_match_table = st_rtc_match,
  249. },
  250. .probe = st_rtc_probe,
  251. };
  252. module_platform_driver(st_rtc_platform_driver);
  253. MODULE_DESCRIPTION("STMicroelectronics LPC RTC driver");
  254. MODULE_AUTHOR("David Paris <david.paris@st.com>");
  255. MODULE_LICENSE("GPL");