rtc-coh901331.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (C) 2007-2009 ST-Ericsson AB
  3. * License terms: GNU General Public License (GPL) version 2
  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/rtc.h>
  12. #include <linux/clk.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/pm.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. /*
  19. * Registers in the COH 901 331
  20. */
  21. /* Alarm value 32bit (R/W) */
  22. #define COH901331_ALARM 0x00U
  23. /* Used to set current time 32bit (R/W) */
  24. #define COH901331_SET_TIME 0x04U
  25. /* Indication if current time is valid 32bit (R/-) */
  26. #define COH901331_VALID 0x08U
  27. /* Read the current time 32bit (R/-) */
  28. #define COH901331_CUR_TIME 0x0cU
  29. /* Event register for the "alarm" interrupt */
  30. #define COH901331_IRQ_EVENT 0x10U
  31. /* Mask register for the "alarm" interrupt */
  32. #define COH901331_IRQ_MASK 0x14U
  33. /* Force register for the "alarm" interrupt */
  34. #define COH901331_IRQ_FORCE 0x18U
  35. /*
  36. * Reference to RTC block clock
  37. * Notice that the frequent clk_enable()/clk_disable() on this
  38. * clock is mainly to be able to turn on/off other clocks in the
  39. * hierarchy as needed, the RTC clock is always on anyway.
  40. */
  41. struct coh901331_port {
  42. struct rtc_device *rtc;
  43. struct clk *clk;
  44. u32 phybase;
  45. u32 physize;
  46. void __iomem *virtbase;
  47. int irq;
  48. #ifdef CONFIG_PM
  49. u32 irqmaskstore;
  50. #endif
  51. };
  52. static irqreturn_t coh901331_interrupt(int irq, void *data)
  53. {
  54. struct coh901331_port *rtap = data;
  55. clk_enable(rtap->clk);
  56. /* Ack IRQ */
  57. writel(1, rtap->virtbase + COH901331_IRQ_EVENT);
  58. /*
  59. * Disable the interrupt. This is necessary because
  60. * the RTC lives on a lower-clocked line and will
  61. * not release the IRQ line until after a few (slower)
  62. * clock cycles. The interrupt will be re-enabled when
  63. * a new alarm is set anyway.
  64. */
  65. writel(0, rtap->virtbase + COH901331_IRQ_MASK);
  66. clk_disable(rtap->clk);
  67. /* Set alarm flag */
  68. rtc_update_irq(rtap->rtc, 1, RTC_AF);
  69. return IRQ_HANDLED;
  70. }
  71. static int coh901331_read_time(struct device *dev, struct rtc_time *tm)
  72. {
  73. struct coh901331_port *rtap = dev_get_drvdata(dev);
  74. clk_enable(rtap->clk);
  75. /* Check if the time is valid */
  76. if (readl(rtap->virtbase + COH901331_VALID)) {
  77. rtc_time_to_tm(readl(rtap->virtbase + COH901331_CUR_TIME), tm);
  78. clk_disable(rtap->clk);
  79. return rtc_valid_tm(tm);
  80. }
  81. clk_disable(rtap->clk);
  82. return -EINVAL;
  83. }
  84. static int coh901331_set_mmss(struct device *dev, unsigned long secs)
  85. {
  86. struct coh901331_port *rtap = dev_get_drvdata(dev);
  87. clk_enable(rtap->clk);
  88. writel(secs, rtap->virtbase + COH901331_SET_TIME);
  89. clk_disable(rtap->clk);
  90. return 0;
  91. }
  92. static int coh901331_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  93. {
  94. struct coh901331_port *rtap = dev_get_drvdata(dev);
  95. clk_enable(rtap->clk);
  96. rtc_time_to_tm(readl(rtap->virtbase + COH901331_ALARM), &alarm->time);
  97. alarm->pending = readl(rtap->virtbase + COH901331_IRQ_EVENT) & 1U;
  98. alarm->enabled = readl(rtap->virtbase + COH901331_IRQ_MASK) & 1U;
  99. clk_disable(rtap->clk);
  100. return 0;
  101. }
  102. static int coh901331_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  103. {
  104. struct coh901331_port *rtap = dev_get_drvdata(dev);
  105. unsigned long time;
  106. rtc_tm_to_time(&alarm->time, &time);
  107. clk_enable(rtap->clk);
  108. writel(time, rtap->virtbase + COH901331_ALARM);
  109. writel(alarm->enabled, rtap->virtbase + COH901331_IRQ_MASK);
  110. clk_disable(rtap->clk);
  111. return 0;
  112. }
  113. static int coh901331_alarm_irq_enable(struct device *dev, unsigned int enabled)
  114. {
  115. struct coh901331_port *rtap = dev_get_drvdata(dev);
  116. clk_enable(rtap->clk);
  117. if (enabled)
  118. writel(1, rtap->virtbase + COH901331_IRQ_MASK);
  119. else
  120. writel(0, rtap->virtbase + COH901331_IRQ_MASK);
  121. clk_disable(rtap->clk);
  122. return 0;
  123. }
  124. static struct rtc_class_ops coh901331_ops = {
  125. .read_time = coh901331_read_time,
  126. .set_mmss = coh901331_set_mmss,
  127. .read_alarm = coh901331_read_alarm,
  128. .set_alarm = coh901331_set_alarm,
  129. .alarm_irq_enable = coh901331_alarm_irq_enable,
  130. };
  131. static int __exit coh901331_remove(struct platform_device *pdev)
  132. {
  133. struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
  134. if (rtap) {
  135. free_irq(rtap->irq, rtap);
  136. rtc_device_unregister(rtap->rtc);
  137. clk_put(rtap->clk);
  138. iounmap(rtap->virtbase);
  139. release_mem_region(rtap->phybase, rtap->physize);
  140. platform_set_drvdata(pdev, NULL);
  141. kfree(rtap);
  142. }
  143. return 0;
  144. }
  145. static int __init coh901331_probe(struct platform_device *pdev)
  146. {
  147. int ret;
  148. struct coh901331_port *rtap;
  149. struct resource *res;
  150. rtap = kzalloc(sizeof(struct coh901331_port), GFP_KERNEL);
  151. if (!rtap)
  152. return -ENOMEM;
  153. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  154. if (!res) {
  155. ret = -ENOENT;
  156. goto out_no_resource;
  157. }
  158. rtap->phybase = res->start;
  159. rtap->physize = resource_size(res);
  160. if (request_mem_region(rtap->phybase, rtap->physize,
  161. "rtc-coh901331") == NULL) {
  162. ret = -EBUSY;
  163. goto out_no_memregion;
  164. }
  165. rtap->virtbase = ioremap(rtap->phybase, rtap->physize);
  166. if (!rtap->virtbase) {
  167. ret = -ENOMEM;
  168. goto out_no_remap;
  169. }
  170. rtap->irq = platform_get_irq(pdev, 0);
  171. if (request_irq(rtap->irq, coh901331_interrupt, 0,
  172. "RTC COH 901 331 Alarm", rtap)) {
  173. ret = -EIO;
  174. goto out_no_irq;
  175. }
  176. rtap->clk = clk_get(&pdev->dev, NULL);
  177. if (IS_ERR(rtap->clk)) {
  178. ret = PTR_ERR(rtap->clk);
  179. dev_err(&pdev->dev, "could not get clock\n");
  180. goto out_no_clk;
  181. }
  182. /* We enable/disable the clock only to assure it works */
  183. ret = clk_enable(rtap->clk);
  184. if (ret) {
  185. dev_err(&pdev->dev, "could not enable clock\n");
  186. goto out_no_clk_enable;
  187. }
  188. clk_disable(rtap->clk);
  189. platform_set_drvdata(pdev, rtap);
  190. rtap->rtc = rtc_device_register("coh901331", &pdev->dev, &coh901331_ops,
  191. THIS_MODULE);
  192. if (IS_ERR(rtap->rtc)) {
  193. ret = PTR_ERR(rtap->rtc);
  194. goto out_no_rtc;
  195. }
  196. return 0;
  197. out_no_rtc:
  198. platform_set_drvdata(pdev, NULL);
  199. out_no_clk_enable:
  200. clk_put(rtap->clk);
  201. out_no_clk:
  202. free_irq(rtap->irq, rtap);
  203. out_no_irq:
  204. iounmap(rtap->virtbase);
  205. out_no_remap:
  206. platform_set_drvdata(pdev, NULL);
  207. out_no_memregion:
  208. release_mem_region(rtap->phybase, SZ_4K);
  209. out_no_resource:
  210. kfree(rtap);
  211. return ret;
  212. }
  213. #ifdef CONFIG_PM
  214. static int coh901331_suspend(struct platform_device *pdev, pm_message_t state)
  215. {
  216. struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
  217. /*
  218. * If this RTC alarm will be used for waking the system up,
  219. * don't disable it of course. Else we just disable the alarm
  220. * and await suspension.
  221. */
  222. if (device_may_wakeup(&pdev->dev)) {
  223. enable_irq_wake(rtap->irq);
  224. } else {
  225. clk_enable(rtap->clk);
  226. rtap->irqmaskstore = readl(rtap->virtbase + COH901331_IRQ_MASK);
  227. writel(0, rtap->virtbase + COH901331_IRQ_MASK);
  228. clk_disable(rtap->clk);
  229. }
  230. return 0;
  231. }
  232. static int coh901331_resume(struct platform_device *pdev)
  233. {
  234. struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
  235. if (device_may_wakeup(&pdev->dev)) {
  236. disable_irq_wake(rtap->irq);
  237. } else {
  238. clk_enable(rtap->clk);
  239. writel(rtap->irqmaskstore, rtap->virtbase + COH901331_IRQ_MASK);
  240. clk_disable(rtap->clk);
  241. }
  242. return 0;
  243. }
  244. #else
  245. #define coh901331_suspend NULL
  246. #define coh901331_resume NULL
  247. #endif
  248. static void coh901331_shutdown(struct platform_device *pdev)
  249. {
  250. struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
  251. clk_enable(rtap->clk);
  252. writel(0, rtap->virtbase + COH901331_IRQ_MASK);
  253. clk_disable(rtap->clk);
  254. }
  255. static struct platform_driver coh901331_driver = {
  256. .driver = {
  257. .name = "rtc-coh901331",
  258. .owner = THIS_MODULE,
  259. },
  260. .remove = __exit_p(coh901331_remove),
  261. .suspend = coh901331_suspend,
  262. .resume = coh901331_resume,
  263. .shutdown = coh901331_shutdown,
  264. };
  265. static int __init coh901331_init(void)
  266. {
  267. return platform_driver_probe(&coh901331_driver, coh901331_probe);
  268. }
  269. static void __exit coh901331_exit(void)
  270. {
  271. platform_driver_unregister(&coh901331_driver);
  272. }
  273. module_init(coh901331_init);
  274. module_exit(coh901331_exit);
  275. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  276. MODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver");
  277. MODULE_LICENSE("GPL");