rtc-brcmstb-waketimer.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright © 2014-2017 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/clk.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/irqreturn.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm.h>
  26. #include <linux/pm_wakeup.h>
  27. #include <linux/reboot.h>
  28. #include <linux/rtc.h>
  29. #include <linux/stat.h>
  30. #include <linux/suspend.h>
  31. struct brcmstb_waketmr {
  32. struct rtc_device *rtc;
  33. struct device *dev;
  34. void __iomem *base;
  35. int irq;
  36. struct notifier_block reboot_notifier;
  37. struct clk *clk;
  38. u32 rate;
  39. };
  40. #define BRCMSTB_WKTMR_EVENT 0x00
  41. #define BRCMSTB_WKTMR_COUNTER 0x04
  42. #define BRCMSTB_WKTMR_ALARM 0x08
  43. #define BRCMSTB_WKTMR_PRESCALER 0x0C
  44. #define BRCMSTB_WKTMR_PRESCALER_VAL 0x10
  45. #define BRCMSTB_WKTMR_DEFAULT_FREQ 27000000
  46. static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr *timer)
  47. {
  48. writel_relaxed(1, timer->base + BRCMSTB_WKTMR_EVENT);
  49. (void)readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT);
  50. }
  51. static void brcmstb_waketmr_set_alarm(struct brcmstb_waketmr *timer,
  52. unsigned int secs)
  53. {
  54. brcmstb_waketmr_clear_alarm(timer);
  55. /* Make sure we are actually counting in seconds */
  56. writel_relaxed(timer->rate, timer->base + BRCMSTB_WKTMR_PRESCALER);
  57. writel_relaxed(secs + 1, timer->base + BRCMSTB_WKTMR_ALARM);
  58. }
  59. static irqreturn_t brcmstb_waketmr_irq(int irq, void *data)
  60. {
  61. struct brcmstb_waketmr *timer = data;
  62. pm_wakeup_event(timer->dev, 0);
  63. return IRQ_HANDLED;
  64. }
  65. struct wktmr_time {
  66. u32 sec;
  67. u32 pre;
  68. };
  69. static void wktmr_read(struct brcmstb_waketmr *timer,
  70. struct wktmr_time *t)
  71. {
  72. u32 tmp;
  73. do {
  74. t->sec = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER);
  75. tmp = readl_relaxed(timer->base + BRCMSTB_WKTMR_PRESCALER_VAL);
  76. } while (tmp >= timer->rate);
  77. t->pre = timer->rate - tmp;
  78. }
  79. static int brcmstb_waketmr_prepare_suspend(struct brcmstb_waketmr *timer)
  80. {
  81. struct device *dev = timer->dev;
  82. int ret = 0;
  83. if (device_may_wakeup(dev)) {
  84. ret = enable_irq_wake(timer->irq);
  85. if (ret) {
  86. dev_err(dev, "failed to enable wake-up interrupt\n");
  87. return ret;
  88. }
  89. }
  90. return ret;
  91. }
  92. /* If enabled as a wakeup-source, arm the timer when powering off */
  93. static int brcmstb_waketmr_reboot(struct notifier_block *nb,
  94. unsigned long action, void *data)
  95. {
  96. struct brcmstb_waketmr *timer;
  97. timer = container_of(nb, struct brcmstb_waketmr, reboot_notifier);
  98. /* Set timer for cold boot */
  99. if (action == SYS_POWER_OFF)
  100. brcmstb_waketmr_prepare_suspend(timer);
  101. return NOTIFY_DONE;
  102. }
  103. static int brcmstb_waketmr_gettime(struct device *dev,
  104. struct rtc_time *tm)
  105. {
  106. struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
  107. struct wktmr_time now;
  108. wktmr_read(timer, &now);
  109. rtc_time_to_tm(now.sec, tm);
  110. return 0;
  111. }
  112. static int brcmstb_waketmr_settime(struct device *dev,
  113. struct rtc_time *tm)
  114. {
  115. struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
  116. time64_t sec;
  117. sec = rtc_tm_to_time64(tm);
  118. writel_relaxed(sec, timer->base + BRCMSTB_WKTMR_COUNTER);
  119. return 0;
  120. }
  121. static int brcmstb_waketmr_getalarm(struct device *dev,
  122. struct rtc_wkalrm *alarm)
  123. {
  124. struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
  125. time64_t sec;
  126. u32 reg;
  127. sec = readl_relaxed(timer->base + BRCMSTB_WKTMR_ALARM);
  128. if (sec != 0) {
  129. /* Alarm is enabled */
  130. alarm->enabled = 1;
  131. rtc_time64_to_tm(sec, &alarm->time);
  132. }
  133. reg = readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT);
  134. alarm->pending = !!(reg & 1);
  135. return 0;
  136. }
  137. static int brcmstb_waketmr_setalarm(struct device *dev,
  138. struct rtc_wkalrm *alarm)
  139. {
  140. struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
  141. time64_t sec;
  142. if (alarm->enabled)
  143. sec = rtc_tm_to_time64(&alarm->time);
  144. else
  145. sec = 0;
  146. brcmstb_waketmr_set_alarm(timer, sec);
  147. return 0;
  148. }
  149. /*
  150. * Does not do much but keep the RTC class happy. We always support
  151. * alarms.
  152. */
  153. static int brcmstb_waketmr_alarm_enable(struct device *dev,
  154. unsigned int enabled)
  155. {
  156. return 0;
  157. }
  158. static const struct rtc_class_ops brcmstb_waketmr_ops = {
  159. .read_time = brcmstb_waketmr_gettime,
  160. .set_time = brcmstb_waketmr_settime,
  161. .read_alarm = brcmstb_waketmr_getalarm,
  162. .set_alarm = brcmstb_waketmr_setalarm,
  163. .alarm_irq_enable = brcmstb_waketmr_alarm_enable,
  164. };
  165. static int brcmstb_waketmr_probe(struct platform_device *pdev)
  166. {
  167. struct device *dev = &pdev->dev;
  168. struct brcmstb_waketmr *timer;
  169. struct resource *res;
  170. int ret;
  171. timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
  172. if (!timer)
  173. return -ENOMEM;
  174. platform_set_drvdata(pdev, timer);
  175. timer->dev = dev;
  176. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  177. timer->base = devm_ioremap_resource(dev, res);
  178. if (IS_ERR(timer->base))
  179. return PTR_ERR(timer->base);
  180. timer->rtc = devm_rtc_allocate_device(dev);
  181. if (IS_ERR(timer->rtc))
  182. return PTR_ERR(timer->rtc);
  183. /*
  184. * Set wakeup capability before requesting wakeup interrupt, so we can
  185. * process boot-time "wakeups" (e.g., from S5 soft-off)
  186. */
  187. device_set_wakeup_capable(dev, true);
  188. device_wakeup_enable(dev);
  189. timer->irq = platform_get_irq(pdev, 0);
  190. if (timer->irq < 0)
  191. return -ENODEV;
  192. timer->clk = devm_clk_get(dev, NULL);
  193. if (!IS_ERR(timer->clk)) {
  194. ret = clk_prepare_enable(timer->clk);
  195. if (ret)
  196. return ret;
  197. timer->rate = clk_get_rate(timer->clk);
  198. if (!timer->rate)
  199. timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
  200. } else {
  201. timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
  202. timer->clk = NULL;
  203. }
  204. ret = devm_request_irq(dev, timer->irq, brcmstb_waketmr_irq, 0,
  205. "brcmstb-waketimer", timer);
  206. if (ret < 0)
  207. goto err_clk;
  208. timer->reboot_notifier.notifier_call = brcmstb_waketmr_reboot;
  209. register_reboot_notifier(&timer->reboot_notifier);
  210. timer->rtc->ops = &brcmstb_waketmr_ops;
  211. timer->rtc->range_max = U32_MAX;
  212. ret = rtc_register_device(timer->rtc);
  213. if (ret) {
  214. dev_err(dev, "unable to register device\n");
  215. goto err_notifier;
  216. }
  217. dev_info(dev, "registered, with irq %d\n", timer->irq);
  218. return 0;
  219. err_notifier:
  220. unregister_reboot_notifier(&timer->reboot_notifier);
  221. err_clk:
  222. if (timer->clk)
  223. clk_disable_unprepare(timer->clk);
  224. return ret;
  225. }
  226. static int brcmstb_waketmr_remove(struct platform_device *pdev)
  227. {
  228. struct brcmstb_waketmr *timer = dev_get_drvdata(&pdev->dev);
  229. unregister_reboot_notifier(&timer->reboot_notifier);
  230. clk_disable_unprepare(timer->clk);
  231. return 0;
  232. }
  233. #ifdef CONFIG_PM_SLEEP
  234. static int brcmstb_waketmr_suspend(struct device *dev)
  235. {
  236. struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
  237. return brcmstb_waketmr_prepare_suspend(timer);
  238. }
  239. static int brcmstb_waketmr_resume(struct device *dev)
  240. {
  241. struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
  242. int ret;
  243. if (!device_may_wakeup(dev))
  244. return 0;
  245. ret = disable_irq_wake(timer->irq);
  246. brcmstb_waketmr_clear_alarm(timer);
  247. return ret;
  248. }
  249. #endif /* CONFIG_PM_SLEEP */
  250. static SIMPLE_DEV_PM_OPS(brcmstb_waketmr_pm_ops,
  251. brcmstb_waketmr_suspend, brcmstb_waketmr_resume);
  252. static const struct of_device_id brcmstb_waketmr_of_match[] = {
  253. { .compatible = "brcm,brcmstb-waketimer" },
  254. { /* sentinel */ },
  255. };
  256. static struct platform_driver brcmstb_waketmr_driver = {
  257. .probe = brcmstb_waketmr_probe,
  258. .remove = brcmstb_waketmr_remove,
  259. .driver = {
  260. .name = "brcmstb-waketimer",
  261. .pm = &brcmstb_waketmr_pm_ops,
  262. .of_match_table = of_match_ptr(brcmstb_waketmr_of_match),
  263. }
  264. };
  265. module_platform_driver(brcmstb_waketmr_driver);
  266. MODULE_LICENSE("GPL v2");
  267. MODULE_AUTHOR("Brian Norris");
  268. MODULE_AUTHOR("Markus Mayer");
  269. MODULE_DESCRIPTION("Wake-up timer driver for STB chips");