rtc-xgene.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * APM X-Gene SoC Real Time Clock Driver
  3. *
  4. * Copyright (c) 2014, Applied Micro Circuits Corporation
  5. * Author: Rameshwar Prasad Sahu <rsahu@apm.com>
  6. * Loc Ho <lho@apm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/io.h>
  27. #include <linux/slab.h>
  28. #include <linux/clk.h>
  29. #include <linux/delay.h>
  30. #include <linux/rtc.h>
  31. /* RTC CSR Registers */
  32. #define RTC_CCVR 0x00
  33. #define RTC_CMR 0x04
  34. #define RTC_CLR 0x08
  35. #define RTC_CCR 0x0C
  36. #define RTC_CCR_IE BIT(0)
  37. #define RTC_CCR_MASK BIT(1)
  38. #define RTC_CCR_EN BIT(2)
  39. #define RTC_CCR_WEN BIT(3)
  40. #define RTC_STAT 0x10
  41. #define RTC_STAT_BIT BIT(0)
  42. #define RTC_RSTAT 0x14
  43. #define RTC_EOI 0x18
  44. #define RTC_VER 0x1C
  45. struct xgene_rtc_dev {
  46. struct rtc_device *rtc;
  47. struct device *dev;
  48. unsigned long alarm_time;
  49. void __iomem *csr_base;
  50. struct clk *clk;
  51. unsigned int irq_wake;
  52. };
  53. static int xgene_rtc_read_time(struct device *dev, struct rtc_time *tm)
  54. {
  55. struct xgene_rtc_dev *pdata = dev_get_drvdata(dev);
  56. rtc_time_to_tm(readl(pdata->csr_base + RTC_CCVR), tm);
  57. return rtc_valid_tm(tm);
  58. }
  59. static int xgene_rtc_set_mmss(struct device *dev, unsigned long secs)
  60. {
  61. struct xgene_rtc_dev *pdata = dev_get_drvdata(dev);
  62. /*
  63. * NOTE: After the following write, the RTC_CCVR is only reflected
  64. * after the update cycle of 1 seconds.
  65. */
  66. writel((u32) secs, pdata->csr_base + RTC_CLR);
  67. readl(pdata->csr_base + RTC_CLR); /* Force a barrier */
  68. return 0;
  69. }
  70. static int xgene_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  71. {
  72. struct xgene_rtc_dev *pdata = dev_get_drvdata(dev);
  73. rtc_time_to_tm(pdata->alarm_time, &alrm->time);
  74. alrm->enabled = readl(pdata->csr_base + RTC_CCR) & RTC_CCR_IE;
  75. return 0;
  76. }
  77. static int xgene_rtc_alarm_irq_enable(struct device *dev, u32 enabled)
  78. {
  79. struct xgene_rtc_dev *pdata = dev_get_drvdata(dev);
  80. u32 ccr;
  81. ccr = readl(pdata->csr_base + RTC_CCR);
  82. if (enabled) {
  83. ccr &= ~RTC_CCR_MASK;
  84. ccr |= RTC_CCR_IE;
  85. } else {
  86. ccr &= ~RTC_CCR_IE;
  87. ccr |= RTC_CCR_MASK;
  88. }
  89. writel(ccr, pdata->csr_base + RTC_CCR);
  90. return 0;
  91. }
  92. static int xgene_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  93. {
  94. struct xgene_rtc_dev *pdata = dev_get_drvdata(dev);
  95. unsigned long rtc_time;
  96. unsigned long alarm_time;
  97. rtc_time = readl(pdata->csr_base + RTC_CCVR);
  98. rtc_tm_to_time(&alrm->time, &alarm_time);
  99. pdata->alarm_time = alarm_time;
  100. writel((u32) pdata->alarm_time, pdata->csr_base + RTC_CMR);
  101. xgene_rtc_alarm_irq_enable(dev, alrm->enabled);
  102. return 0;
  103. }
  104. static const struct rtc_class_ops xgene_rtc_ops = {
  105. .read_time = xgene_rtc_read_time,
  106. .set_mmss = xgene_rtc_set_mmss,
  107. .read_alarm = xgene_rtc_read_alarm,
  108. .set_alarm = xgene_rtc_set_alarm,
  109. .alarm_irq_enable = xgene_rtc_alarm_irq_enable,
  110. };
  111. static irqreturn_t xgene_rtc_interrupt(int irq, void *id)
  112. {
  113. struct xgene_rtc_dev *pdata = (struct xgene_rtc_dev *) id;
  114. /* Check if interrupt asserted */
  115. if (!(readl(pdata->csr_base + RTC_STAT) & RTC_STAT_BIT))
  116. return IRQ_NONE;
  117. /* Clear interrupt */
  118. readl(pdata->csr_base + RTC_EOI);
  119. rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
  120. return IRQ_HANDLED;
  121. }
  122. static int xgene_rtc_probe(struct platform_device *pdev)
  123. {
  124. struct xgene_rtc_dev *pdata;
  125. struct resource *res;
  126. int ret;
  127. int irq;
  128. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  129. if (!pdata)
  130. return -ENOMEM;
  131. platform_set_drvdata(pdev, pdata);
  132. pdata->dev = &pdev->dev;
  133. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  134. pdata->csr_base = devm_ioremap_resource(&pdev->dev, res);
  135. if (IS_ERR(pdata->csr_base))
  136. return PTR_ERR(pdata->csr_base);
  137. irq = platform_get_irq(pdev, 0);
  138. if (irq < 0) {
  139. dev_err(&pdev->dev, "No IRQ resource\n");
  140. return irq;
  141. }
  142. ret = devm_request_irq(&pdev->dev, irq, xgene_rtc_interrupt, 0,
  143. dev_name(&pdev->dev), pdata);
  144. if (ret) {
  145. dev_err(&pdev->dev, "Could not request IRQ\n");
  146. return ret;
  147. }
  148. pdata->clk = devm_clk_get(&pdev->dev, NULL);
  149. if (IS_ERR(pdata->clk)) {
  150. dev_err(&pdev->dev, "Couldn't get the clock for RTC\n");
  151. return -ENODEV;
  152. }
  153. clk_prepare_enable(pdata->clk);
  154. /* Turn on the clock and the crystal */
  155. writel(RTC_CCR_EN, pdata->csr_base + RTC_CCR);
  156. device_init_wakeup(&pdev->dev, 1);
  157. pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  158. &xgene_rtc_ops, THIS_MODULE);
  159. if (IS_ERR(pdata->rtc)) {
  160. clk_disable_unprepare(pdata->clk);
  161. return PTR_ERR(pdata->rtc);
  162. }
  163. /* HW does not support update faster than 1 seconds */
  164. pdata->rtc->uie_unsupported = 1;
  165. return 0;
  166. }
  167. static int xgene_rtc_remove(struct platform_device *pdev)
  168. {
  169. struct xgene_rtc_dev *pdata = platform_get_drvdata(pdev);
  170. xgene_rtc_alarm_irq_enable(&pdev->dev, 0);
  171. device_init_wakeup(&pdev->dev, 0);
  172. clk_disable_unprepare(pdata->clk);
  173. return 0;
  174. }
  175. #ifdef CONFIG_PM_SLEEP
  176. static int xgene_rtc_suspend(struct device *dev)
  177. {
  178. struct platform_device *pdev = to_platform_device(dev);
  179. struct xgene_rtc_dev *pdata = platform_get_drvdata(pdev);
  180. int irq;
  181. irq = platform_get_irq(pdev, 0);
  182. if (device_may_wakeup(&pdev->dev)) {
  183. if (!enable_irq_wake(irq))
  184. pdata->irq_wake = 1;
  185. } else {
  186. xgene_rtc_alarm_irq_enable(dev, 0);
  187. clk_disable(pdata->clk);
  188. }
  189. return 0;
  190. }
  191. static int xgene_rtc_resume(struct device *dev)
  192. {
  193. struct platform_device *pdev = to_platform_device(dev);
  194. struct xgene_rtc_dev *pdata = platform_get_drvdata(pdev);
  195. int irq;
  196. irq = platform_get_irq(pdev, 0);
  197. if (device_may_wakeup(&pdev->dev)) {
  198. if (pdata->irq_wake) {
  199. disable_irq_wake(irq);
  200. pdata->irq_wake = 0;
  201. }
  202. } else {
  203. clk_enable(pdata->clk);
  204. xgene_rtc_alarm_irq_enable(dev, 1);
  205. }
  206. return 0;
  207. }
  208. #endif
  209. static SIMPLE_DEV_PM_OPS(xgene_rtc_pm_ops, xgene_rtc_suspend, xgene_rtc_resume);
  210. #ifdef CONFIG_OF
  211. static const struct of_device_id xgene_rtc_of_match[] = {
  212. {.compatible = "apm,xgene-rtc" },
  213. { }
  214. };
  215. MODULE_DEVICE_TABLE(of, xgene_rtc_of_match);
  216. #endif
  217. static struct platform_driver xgene_rtc_driver = {
  218. .probe = xgene_rtc_probe,
  219. .remove = xgene_rtc_remove,
  220. .driver = {
  221. .name = "xgene-rtc",
  222. .pm = &xgene_rtc_pm_ops,
  223. .of_match_table = of_match_ptr(xgene_rtc_of_match),
  224. },
  225. };
  226. module_platform_driver(xgene_rtc_driver);
  227. MODULE_DESCRIPTION("APM X-Gene SoC RTC driver");
  228. MODULE_AUTHOR("Rameshwar Sahu <rsahu@apm.com>");
  229. MODULE_LICENSE("GPL");