rtc-coh901331.c 7.6 KB

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