rtc-st-lpc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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_time_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;
  89. unsigned long secs, flags;
  90. int ret;
  91. ret = rtc_tm_to_time(tm, &secs);
  92. if (ret)
  93. return ret;
  94. lpt = (unsigned long long)secs * rtc->clkrate;
  95. spin_lock_irqsave(&rtc->lock, flags);
  96. writel_relaxed(lpt >> 32, rtc->ioaddr + LPC_LPT_MSB_OFF);
  97. writel_relaxed(lpt, rtc->ioaddr + LPC_LPT_LSB_OFF);
  98. writel_relaxed(1, rtc->ioaddr + LPC_LPT_START_OFF);
  99. spin_unlock_irqrestore(&rtc->lock, flags);
  100. return 0;
  101. }
  102. static int st_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  103. {
  104. struct st_rtc *rtc = dev_get_drvdata(dev);
  105. unsigned long flags;
  106. spin_lock_irqsave(&rtc->lock, flags);
  107. memcpy(wkalrm, &rtc->alarm, sizeof(struct rtc_wkalrm));
  108. spin_unlock_irqrestore(&rtc->lock, flags);
  109. return 0;
  110. }
  111. static int st_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  112. {
  113. struct st_rtc *rtc = dev_get_drvdata(dev);
  114. if (enabled && !rtc->irq_enabled) {
  115. enable_irq(rtc->irq);
  116. rtc->irq_enabled = true;
  117. } else if (!enabled && rtc->irq_enabled) {
  118. disable_irq(rtc->irq);
  119. rtc->irq_enabled = false;
  120. }
  121. return 0;
  122. }
  123. static int st_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  124. {
  125. struct st_rtc *rtc = dev_get_drvdata(dev);
  126. struct rtc_time now;
  127. unsigned long now_secs;
  128. unsigned long alarm_secs;
  129. unsigned long long lpa;
  130. st_rtc_read_time(dev, &now);
  131. rtc_tm_to_time(&now, &now_secs);
  132. rtc_tm_to_time(&t->time, &alarm_secs);
  133. /* Invalid alarm time */
  134. if (now_secs > alarm_secs)
  135. return -EINVAL;
  136. memcpy(&rtc->alarm, t, sizeof(struct rtc_wkalrm));
  137. /* Now many secs to fire */
  138. alarm_secs -= now_secs;
  139. lpa = (unsigned long long)alarm_secs * rtc->clkrate;
  140. st_rtc_set_hw_alarm(rtc, lpa >> 32, lpa);
  141. st_rtc_alarm_irq_enable(dev, t->enabled);
  142. return 0;
  143. }
  144. static struct rtc_class_ops st_rtc_ops = {
  145. .read_time = st_rtc_read_time,
  146. .set_time = st_rtc_set_time,
  147. .read_alarm = st_rtc_read_alarm,
  148. .set_alarm = st_rtc_set_alarm,
  149. .alarm_irq_enable = st_rtc_alarm_irq_enable,
  150. };
  151. static int st_rtc_probe(struct platform_device *pdev)
  152. {
  153. struct device_node *np = pdev->dev.of_node;
  154. struct st_rtc *rtc;
  155. struct resource *res;
  156. struct rtc_time tm_check;
  157. uint32_t mode;
  158. int ret = 0;
  159. ret = of_property_read_u32(np, "st,lpc-mode", &mode);
  160. if (ret) {
  161. dev_err(&pdev->dev, "An LPC mode must be provided\n");
  162. return -EINVAL;
  163. }
  164. /* LPC can either run as a Clocksource or in RTC or WDT mode */
  165. if (mode != ST_LPC_MODE_RTC)
  166. return -ENODEV;
  167. rtc = devm_kzalloc(&pdev->dev, sizeof(struct st_rtc), GFP_KERNEL);
  168. if (!rtc)
  169. return -ENOMEM;
  170. spin_lock_init(&rtc->lock);
  171. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  172. rtc->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  173. if (IS_ERR(rtc->ioaddr))
  174. return PTR_ERR(rtc->ioaddr);
  175. rtc->irq = irq_of_parse_and_map(np, 0);
  176. if (!rtc->irq) {
  177. dev_err(&pdev->dev, "IRQ missing or invalid\n");
  178. return -EINVAL;
  179. }
  180. ret = devm_request_irq(&pdev->dev, rtc->irq, st_rtc_handler, 0,
  181. pdev->name, rtc);
  182. if (ret) {
  183. dev_err(&pdev->dev, "Failed to request irq %i\n", rtc->irq);
  184. return ret;
  185. }
  186. enable_irq_wake(rtc->irq);
  187. disable_irq(rtc->irq);
  188. rtc->clk = clk_get(&pdev->dev, NULL);
  189. if (IS_ERR(rtc->clk)) {
  190. dev_err(&pdev->dev, "Unable to request clock\n");
  191. return PTR_ERR(rtc->clk);
  192. }
  193. clk_prepare_enable(rtc->clk);
  194. rtc->clkrate = clk_get_rate(rtc->clk);
  195. if (!rtc->clkrate) {
  196. dev_err(&pdev->dev, "Unable to fetch clock rate\n");
  197. return -EINVAL;
  198. }
  199. device_set_wakeup_capable(&pdev->dev, 1);
  200. platform_set_drvdata(pdev, rtc);
  201. /*
  202. * The RTC-LPC is able to manage date.year > 2038
  203. * but currently the kernel can not manage this date!
  204. * If the RTC-LPC has a date.year > 2038 then
  205. * it's set to the epoch "Jan 1st 2000"
  206. */
  207. st_rtc_read_time(&pdev->dev, &tm_check);
  208. if (tm_check.tm_year >= (2038 - 1900)) {
  209. memset(&tm_check, 0, sizeof(tm_check));
  210. tm_check.tm_year = 100;
  211. tm_check.tm_mday = 1;
  212. st_rtc_set_time(&pdev->dev, &tm_check);
  213. }
  214. rtc->rtc_dev = rtc_device_register("st-lpc-rtc", &pdev->dev,
  215. &st_rtc_ops, THIS_MODULE);
  216. if (IS_ERR(rtc->rtc_dev)) {
  217. clk_disable_unprepare(rtc->clk);
  218. return PTR_ERR(rtc->rtc_dev);
  219. }
  220. return 0;
  221. }
  222. static int st_rtc_remove(struct platform_device *pdev)
  223. {
  224. struct st_rtc *rtc = platform_get_drvdata(pdev);
  225. if (likely(rtc->rtc_dev))
  226. rtc_device_unregister(rtc->rtc_dev);
  227. return 0;
  228. }
  229. #ifdef CONFIG_PM_SLEEP
  230. static int st_rtc_suspend(struct device *dev)
  231. {
  232. struct st_rtc *rtc = dev_get_drvdata(dev);
  233. if (device_may_wakeup(dev))
  234. return 0;
  235. writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
  236. writel_relaxed(0, rtc->ioaddr + LPC_LPA_START_OFF);
  237. writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
  238. return 0;
  239. }
  240. static int st_rtc_resume(struct device *dev)
  241. {
  242. struct st_rtc *rtc = dev_get_drvdata(dev);
  243. rtc_alarm_irq_enable(rtc->rtc_dev, 0);
  244. /*
  245. * clean 'rtc->alarm' to allow a new
  246. * .set_alarm to the upper RTC layer
  247. */
  248. memset(&rtc->alarm, 0, sizeof(struct rtc_wkalrm));
  249. writel_relaxed(0, rtc->ioaddr + LPC_LPA_MSB_OFF);
  250. writel_relaxed(0, rtc->ioaddr + LPC_LPA_LSB_OFF);
  251. writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
  252. writel_relaxed(1, rtc->ioaddr + LPC_LPA_START_OFF);
  253. writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
  254. return 0;
  255. }
  256. #endif
  257. static SIMPLE_DEV_PM_OPS(st_rtc_pm_ops, st_rtc_suspend, st_rtc_resume);
  258. static const struct of_device_id st_rtc_match[] = {
  259. { .compatible = "st,stih407-lpc" },
  260. {}
  261. };
  262. MODULE_DEVICE_TABLE(of, st_rtc_match);
  263. static struct platform_driver st_rtc_platform_driver = {
  264. .driver = {
  265. .name = "st-lpc-rtc",
  266. .pm = &st_rtc_pm_ops,
  267. .of_match_table = st_rtc_match,
  268. },
  269. .probe = st_rtc_probe,
  270. .remove = st_rtc_remove,
  271. };
  272. module_platform_driver(st_rtc_platform_driver);
  273. MODULE_DESCRIPTION("STMicroelectronics LPC RTC driver");
  274. MODULE_AUTHOR("David Paris <david.paris@st.com>");
  275. MODULE_LICENSE("GPL");