rtc-at32ap700x.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * An RTC driver for the AVR32 AT32AP700x processor series.
  3. *
  4. * Copyright (C) 2007 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/rtc.h>
  14. #include <linux/io.h>
  15. /*
  16. * This is a bare-bones RTC. It runs during most system sleep states, but has
  17. * no battery backup and gets reset during system restart. It must be
  18. * initialized from an external clock (network, I2C, etc) before it can be of
  19. * much use.
  20. *
  21. * The alarm functionality is limited by the hardware, not supporting
  22. * periodic interrupts.
  23. */
  24. #define RTC_CTRL 0x00
  25. #define RTC_CTRL_EN 0
  26. #define RTC_CTRL_PCLR 1
  27. #define RTC_CTRL_TOPEN 2
  28. #define RTC_CTRL_PSEL 8
  29. #define RTC_VAL 0x04
  30. #define RTC_TOP 0x08
  31. #define RTC_IER 0x10
  32. #define RTC_IER_TOPI 0
  33. #define RTC_IDR 0x14
  34. #define RTC_IDR_TOPI 0
  35. #define RTC_IMR 0x18
  36. #define RTC_IMR_TOPI 0
  37. #define RTC_ISR 0x1c
  38. #define RTC_ISR_TOPI 0
  39. #define RTC_ICR 0x20
  40. #define RTC_ICR_TOPI 0
  41. #define RTC_BIT(name) (1 << RTC_##name)
  42. #define RTC_BF(name, value) ((value) << RTC_##name)
  43. #define rtc_readl(dev, reg) \
  44. __raw_readl((dev)->regs + RTC_##reg)
  45. #define rtc_writel(dev, reg, value) \
  46. __raw_writel((value), (dev)->regs + RTC_##reg)
  47. struct rtc_at32ap700x {
  48. struct rtc_device *rtc;
  49. void __iomem *regs;
  50. unsigned long alarm_time;
  51. unsigned long irq;
  52. /* Protect against concurrent register access. */
  53. spinlock_t lock;
  54. };
  55. static int at32_rtc_readtime(struct device *dev, struct rtc_time *tm)
  56. {
  57. struct rtc_at32ap700x *rtc = dev_get_drvdata(dev);
  58. unsigned long now;
  59. now = rtc_readl(rtc, VAL);
  60. rtc_time_to_tm(now, tm);
  61. return 0;
  62. }
  63. static int at32_rtc_settime(struct device *dev, struct rtc_time *tm)
  64. {
  65. struct rtc_at32ap700x *rtc = dev_get_drvdata(dev);
  66. unsigned long now;
  67. int ret;
  68. ret = rtc_tm_to_time(tm, &now);
  69. if (ret == 0)
  70. rtc_writel(rtc, VAL, now);
  71. return ret;
  72. }
  73. static int at32_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  74. {
  75. struct rtc_at32ap700x *rtc = dev_get_drvdata(dev);
  76. spin_lock_irq(&rtc->lock);
  77. rtc_time_to_tm(rtc->alarm_time, &alrm->time);
  78. alrm->enabled = rtc_readl(rtc, IMR) & RTC_BIT(IMR_TOPI) ? 1 : 0;
  79. alrm->pending = rtc_readl(rtc, ISR) & RTC_BIT(ISR_TOPI) ? 1 : 0;
  80. spin_unlock_irq(&rtc->lock);
  81. return 0;
  82. }
  83. static int at32_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  84. {
  85. struct rtc_at32ap700x *rtc = dev_get_drvdata(dev);
  86. unsigned long rtc_unix_time;
  87. unsigned long alarm_unix_time;
  88. int ret;
  89. rtc_unix_time = rtc_readl(rtc, VAL);
  90. ret = rtc_tm_to_time(&alrm->time, &alarm_unix_time);
  91. if (ret)
  92. return ret;
  93. if (alarm_unix_time < rtc_unix_time)
  94. return -EINVAL;
  95. spin_lock_irq(&rtc->lock);
  96. rtc->alarm_time = alarm_unix_time;
  97. rtc_writel(rtc, TOP, rtc->alarm_time);
  98. if (alrm->enabled)
  99. rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL)
  100. | RTC_BIT(CTRL_TOPEN));
  101. else
  102. rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL)
  103. & ~RTC_BIT(CTRL_TOPEN));
  104. spin_unlock_irq(&rtc->lock);
  105. return ret;
  106. }
  107. static int at32_rtc_ioctl(struct device *dev, unsigned int cmd,
  108. unsigned long arg)
  109. {
  110. struct rtc_at32ap700x *rtc = dev_get_drvdata(dev);
  111. int ret = 0;
  112. spin_lock_irq(&rtc->lock);
  113. switch (cmd) {
  114. case RTC_AIE_ON:
  115. if (rtc_readl(rtc, VAL) > rtc->alarm_time) {
  116. ret = -EINVAL;
  117. break;
  118. }
  119. rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL)
  120. | RTC_BIT(CTRL_TOPEN));
  121. rtc_writel(rtc, ICR, RTC_BIT(ICR_TOPI));
  122. rtc_writel(rtc, IER, RTC_BIT(IER_TOPI));
  123. break;
  124. case RTC_AIE_OFF:
  125. rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL)
  126. & ~RTC_BIT(CTRL_TOPEN));
  127. rtc_writel(rtc, IDR, RTC_BIT(IDR_TOPI));
  128. rtc_writel(rtc, ICR, RTC_BIT(ICR_TOPI));
  129. break;
  130. default:
  131. ret = -ENOIOCTLCMD;
  132. break;
  133. }
  134. spin_unlock_irq(&rtc->lock);
  135. return ret;
  136. }
  137. static irqreturn_t at32_rtc_interrupt(int irq, void *dev_id)
  138. {
  139. struct rtc_at32ap700x *rtc = (struct rtc_at32ap700x *)dev_id;
  140. unsigned long isr = rtc_readl(rtc, ISR);
  141. unsigned long events = 0;
  142. int ret = IRQ_NONE;
  143. spin_lock(&rtc->lock);
  144. if (isr & RTC_BIT(ISR_TOPI)) {
  145. rtc_writel(rtc, ICR, RTC_BIT(ICR_TOPI));
  146. rtc_writel(rtc, IDR, RTC_BIT(IDR_TOPI));
  147. rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL)
  148. & ~RTC_BIT(CTRL_TOPEN));
  149. rtc_writel(rtc, VAL, rtc->alarm_time);
  150. events = RTC_AF | RTC_IRQF;
  151. rtc_update_irq(rtc->rtc, 1, events);
  152. ret = IRQ_HANDLED;
  153. }
  154. spin_unlock(&rtc->lock);
  155. return ret;
  156. }
  157. static struct rtc_class_ops at32_rtc_ops = {
  158. .ioctl = at32_rtc_ioctl,
  159. .read_time = at32_rtc_readtime,
  160. .set_time = at32_rtc_settime,
  161. .read_alarm = at32_rtc_readalarm,
  162. .set_alarm = at32_rtc_setalarm,
  163. };
  164. static int __init at32_rtc_probe(struct platform_device *pdev)
  165. {
  166. struct resource *regs;
  167. struct rtc_at32ap700x *rtc;
  168. int irq = -1;
  169. int ret;
  170. rtc = kzalloc(sizeof(struct rtc_at32ap700x), GFP_KERNEL);
  171. if (!rtc) {
  172. dev_dbg(&pdev->dev, "out of memory\n");
  173. return -ENOMEM;
  174. }
  175. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  176. if (!regs) {
  177. dev_dbg(&pdev->dev, "no mmio resource defined\n");
  178. ret = -ENXIO;
  179. goto out;
  180. }
  181. irq = platform_get_irq(pdev, 0);
  182. if (irq < 0) {
  183. dev_dbg(&pdev->dev, "could not get irq\n");
  184. ret = -ENXIO;
  185. goto out;
  186. }
  187. rtc->irq = irq;
  188. rtc->regs = ioremap(regs->start, regs->end - regs->start + 1);
  189. if (!rtc->regs) {
  190. ret = -ENOMEM;
  191. dev_dbg(&pdev->dev, "could not map I/O memory\n");
  192. goto out;
  193. }
  194. spin_lock_init(&rtc->lock);
  195. /*
  196. * Maybe init RTC: count from zero at 1 Hz, disable wrap irq.
  197. *
  198. * Do not reset VAL register, as it can hold an old time
  199. * from last JTAG reset.
  200. */
  201. if (!(rtc_readl(rtc, CTRL) & RTC_BIT(CTRL_EN))) {
  202. rtc_writel(rtc, CTRL, RTC_BIT(CTRL_PCLR));
  203. rtc_writel(rtc, IDR, RTC_BIT(IDR_TOPI));
  204. rtc_writel(rtc, CTRL, RTC_BF(CTRL_PSEL, 0xe)
  205. | RTC_BIT(CTRL_EN));
  206. }
  207. ret = request_irq(irq, at32_rtc_interrupt, IRQF_SHARED, "rtc", rtc);
  208. if (ret) {
  209. dev_dbg(&pdev->dev, "could not request irq %d\n", irq);
  210. goto out_iounmap;
  211. }
  212. rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
  213. &at32_rtc_ops, THIS_MODULE);
  214. if (IS_ERR(rtc->rtc)) {
  215. dev_dbg(&pdev->dev, "could not register rtc device\n");
  216. ret = PTR_ERR(rtc->rtc);
  217. goto out_free_irq;
  218. }
  219. platform_set_drvdata(pdev, rtc);
  220. device_init_wakeup(&pdev->dev, 1);
  221. dev_info(&pdev->dev, "Atmel RTC for AT32AP700x at %08lx irq %ld\n",
  222. (unsigned long)rtc->regs, rtc->irq);
  223. return 0;
  224. out_free_irq:
  225. free_irq(irq, rtc);
  226. out_iounmap:
  227. iounmap(rtc->regs);
  228. out:
  229. kfree(rtc);
  230. return ret;
  231. }
  232. static int __exit at32_rtc_remove(struct platform_device *pdev)
  233. {
  234. struct rtc_at32ap700x *rtc = platform_get_drvdata(pdev);
  235. device_init_wakeup(&pdev->dev, 0);
  236. free_irq(rtc->irq, rtc);
  237. iounmap(rtc->regs);
  238. rtc_device_unregister(rtc->rtc);
  239. kfree(rtc);
  240. platform_set_drvdata(pdev, NULL);
  241. return 0;
  242. }
  243. MODULE_ALIAS("platform:at32ap700x_rtc");
  244. static struct platform_driver at32_rtc_driver = {
  245. .remove = __exit_p(at32_rtc_remove),
  246. .driver = {
  247. .name = "at32ap700x_rtc",
  248. .owner = THIS_MODULE,
  249. },
  250. };
  251. static int __init at32_rtc_init(void)
  252. {
  253. return platform_driver_probe(&at32_rtc_driver, at32_rtc_probe);
  254. }
  255. module_init(at32_rtc_init);
  256. static void __exit at32_rtc_exit(void)
  257. {
  258. platform_driver_unregister(&at32_rtc_driver);
  259. }
  260. module_exit(at32_rtc_exit);
  261. MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
  262. MODULE_DESCRIPTION("Real time clock for AVR32 AT32AP700x");
  263. MODULE_LICENSE("GPL");