rtc-coh901331.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007-2009 ST-Ericsson AB
  4. * Real Time Clock interface for ST-Ericsson AB COH 901 331 RTC.
  5. * Author: Linus Walleij <linus.walleij@stericsson.com>
  6. * Based on rtc-pl031.c by Deepak Saxena <dsaxena@plexity.net>
  7. * Copyright 2006 (c) MontaVista Software, Inc.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/rtc.h>
  13. #include <linux/clk.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/pm.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <linux/slab.h>
  19. /*
  20. * Registers in the COH 901 331
  21. */
  22. /* Alarm value 32bit (R/W) */
  23. #define COH901331_ALARM 0x00U
  24. /* Used to set current time 32bit (R/W) */
  25. #define COH901331_SET_TIME 0x04U
  26. /* Indication if current time is valid 32bit (R/-) */
  27. #define COH901331_VALID 0x08U
  28. /* Read the current time 32bit (R/-) */
  29. #define COH901331_CUR_TIME 0x0cU
  30. /* Event register for the "alarm" interrupt */
  31. #define COH901331_IRQ_EVENT 0x10U
  32. /* Mask register for the "alarm" interrupt */
  33. #define COH901331_IRQ_MASK 0x14U
  34. /* Force register for the "alarm" interrupt */
  35. #define COH901331_IRQ_FORCE 0x18U
  36. /*
  37. * Reference to RTC block clock
  38. * Notice that the frequent clk_enable()/clk_disable() on this
  39. * clock is mainly to be able to turn on/off other clocks in the
  40. * hierarchy as needed, the RTC clock is always on anyway.
  41. */
  42. struct coh901331_port {
  43. struct rtc_device *rtc;
  44. struct clk *clk;
  45. void __iomem *virtbase;
  46. int irq;
  47. #ifdef CONFIG_PM_SLEEP
  48. u32 irqmaskstore;
  49. #endif
  50. };
  51. static irqreturn_t coh901331_interrupt(int irq, void *data)
  52. {
  53. struct coh901331_port *rtap = data;
  54. clk_enable(rtap->clk);
  55. /* Ack IRQ */
  56. writel(1, rtap->virtbase + COH901331_IRQ_EVENT);
  57. /*
  58. * Disable the interrupt. This is necessary because
  59. * the RTC lives on a lower-clocked line and will
  60. * not release the IRQ line until after a few (slower)
  61. * clock cycles. The interrupt will be re-enabled when
  62. * a new alarm is set anyway.
  63. */
  64. writel(0, rtap->virtbase + COH901331_IRQ_MASK);
  65. clk_disable(rtap->clk);
  66. /* Set alarm flag */
  67. rtc_update_irq(rtap->rtc, 1, RTC_AF);
  68. return IRQ_HANDLED;
  69. }
  70. static int coh901331_read_time(struct device *dev, struct rtc_time *tm)
  71. {
  72. struct coh901331_port *rtap = dev_get_drvdata(dev);
  73. clk_enable(rtap->clk);
  74. /* Check if the time is valid */
  75. if (!readl(rtap->virtbase + COH901331_VALID)) {
  76. clk_disable(rtap->clk);
  77. return -EINVAL;
  78. }
  79. rtc_time64_to_tm(readl(rtap->virtbase + COH901331_CUR_TIME), tm);
  80. clk_disable(rtap->clk);
  81. return 0;
  82. }
  83. static int coh901331_set_time(struct device *dev, struct rtc_time *tm)
  84. {
  85. struct coh901331_port *rtap = dev_get_drvdata(dev);
  86. clk_enable(rtap->clk);
  87. writel(rtc_tm_to_time64(tm), rtap->virtbase + COH901331_SET_TIME);
  88. clk_disable(rtap->clk);
  89. return 0;
  90. }
  91. static int coh901331_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  92. {
  93. struct coh901331_port *rtap = dev_get_drvdata(dev);
  94. clk_enable(rtap->clk);
  95. rtc_time64_to_tm(readl(rtap->virtbase + COH901331_ALARM), &alarm->time);
  96. alarm->pending = readl(rtap->virtbase + COH901331_IRQ_EVENT) & 1U;
  97. alarm->enabled = readl(rtap->virtbase + COH901331_IRQ_MASK) & 1U;
  98. clk_disable(rtap->clk);
  99. return 0;
  100. }
  101. static int coh901331_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  102. {
  103. struct coh901331_port *rtap = dev_get_drvdata(dev);
  104. unsigned long time = rtc_tm_to_time64(&alarm->time);
  105. clk_enable(rtap->clk);
  106. writel(time, rtap->virtbase + COH901331_ALARM);
  107. writel(alarm->enabled, rtap->virtbase + COH901331_IRQ_MASK);
  108. clk_disable(rtap->clk);
  109. return 0;
  110. }
  111. static int coh901331_alarm_irq_enable(struct device *dev, unsigned int enabled)
  112. {
  113. struct coh901331_port *rtap = dev_get_drvdata(dev);
  114. clk_enable(rtap->clk);
  115. if (enabled)
  116. writel(1, rtap->virtbase + COH901331_IRQ_MASK);
  117. else
  118. writel(0, rtap->virtbase + COH901331_IRQ_MASK);
  119. clk_disable(rtap->clk);
  120. return 0;
  121. }
  122. static const struct rtc_class_ops coh901331_ops = {
  123. .read_time = coh901331_read_time,
  124. .set_time = coh901331_set_time,
  125. .read_alarm = coh901331_read_alarm,
  126. .set_alarm = coh901331_set_alarm,
  127. .alarm_irq_enable = coh901331_alarm_irq_enable,
  128. };
  129. static int __exit coh901331_remove(struct platform_device *pdev)
  130. {
  131. struct coh901331_port *rtap = platform_get_drvdata(pdev);
  132. if (rtap)
  133. clk_unprepare(rtap->clk);
  134. return 0;
  135. }
  136. static int __init coh901331_probe(struct platform_device *pdev)
  137. {
  138. int ret;
  139. struct coh901331_port *rtap;
  140. rtap = devm_kzalloc(&pdev->dev,
  141. sizeof(struct coh901331_port), GFP_KERNEL);
  142. if (!rtap)
  143. return -ENOMEM;
  144. rtap->virtbase = devm_platform_ioremap_resource(pdev, 0);
  145. if (IS_ERR(rtap->virtbase))
  146. return PTR_ERR(rtap->virtbase);
  147. rtap->irq = platform_get_irq(pdev, 0);
  148. if (devm_request_irq(&pdev->dev, rtap->irq, coh901331_interrupt, 0,
  149. "RTC COH 901 331 Alarm", rtap))
  150. return -EIO;
  151. rtap->clk = devm_clk_get(&pdev->dev, NULL);
  152. if (IS_ERR(rtap->clk)) {
  153. ret = PTR_ERR(rtap->clk);
  154. dev_err(&pdev->dev, "could not get clock\n");
  155. return ret;
  156. }
  157. rtap->rtc = devm_rtc_allocate_device(&pdev->dev);
  158. if (IS_ERR(rtap->rtc))
  159. return PTR_ERR(rtap->rtc);
  160. rtap->rtc->ops = &coh901331_ops;
  161. rtap->rtc->range_max = U32_MAX;
  162. /* We enable/disable the clock only to assure it works */
  163. ret = clk_prepare_enable(rtap->clk);
  164. if (ret) {
  165. dev_err(&pdev->dev, "could not enable clock\n");
  166. return ret;
  167. }
  168. clk_disable(rtap->clk);
  169. platform_set_drvdata(pdev, rtap);
  170. ret = rtc_register_device(rtap->rtc);
  171. if (ret)
  172. goto out_no_rtc;
  173. return 0;
  174. out_no_rtc:
  175. clk_unprepare(rtap->clk);
  176. return ret;
  177. }
  178. #ifdef CONFIG_PM_SLEEP
  179. static int coh901331_suspend(struct device *dev)
  180. {
  181. struct coh901331_port *rtap = dev_get_drvdata(dev);
  182. /*
  183. * If this RTC alarm will be used for waking the system up,
  184. * don't disable it of course. Else we just disable the alarm
  185. * and await suspension.
  186. */
  187. if (device_may_wakeup(dev)) {
  188. enable_irq_wake(rtap->irq);
  189. } else {
  190. clk_enable(rtap->clk);
  191. rtap->irqmaskstore = readl(rtap->virtbase + COH901331_IRQ_MASK);
  192. writel(0, rtap->virtbase + COH901331_IRQ_MASK);
  193. clk_disable(rtap->clk);
  194. }
  195. clk_unprepare(rtap->clk);
  196. return 0;
  197. }
  198. static int coh901331_resume(struct device *dev)
  199. {
  200. int ret;
  201. struct coh901331_port *rtap = dev_get_drvdata(dev);
  202. ret = clk_prepare(rtap->clk);
  203. if (ret)
  204. return ret;
  205. if (device_may_wakeup(dev)) {
  206. disable_irq_wake(rtap->irq);
  207. } else {
  208. clk_enable(rtap->clk);
  209. writel(rtap->irqmaskstore, rtap->virtbase + COH901331_IRQ_MASK);
  210. clk_disable(rtap->clk);
  211. }
  212. return 0;
  213. }
  214. #endif
  215. static SIMPLE_DEV_PM_OPS(coh901331_pm_ops, coh901331_suspend, coh901331_resume);
  216. static void coh901331_shutdown(struct platform_device *pdev)
  217. {
  218. struct coh901331_port *rtap = platform_get_drvdata(pdev);
  219. clk_enable(rtap->clk);
  220. writel(0, rtap->virtbase + COH901331_IRQ_MASK);
  221. clk_disable_unprepare(rtap->clk);
  222. }
  223. static const struct of_device_id coh901331_dt_match[] = {
  224. { .compatible = "stericsson,coh901331" },
  225. {},
  226. };
  227. MODULE_DEVICE_TABLE(of, coh901331_dt_match);
  228. static struct platform_driver coh901331_driver = {
  229. .driver = {
  230. .name = "rtc-coh901331",
  231. .pm = &coh901331_pm_ops,
  232. .of_match_table = coh901331_dt_match,
  233. },
  234. .remove = __exit_p(coh901331_remove),
  235. .shutdown = coh901331_shutdown,
  236. };
  237. module_platform_driver_probe(coh901331_driver, coh901331_probe);
  238. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  239. MODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver");
  240. MODULE_LICENSE("GPL");