rtc-goldfish.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* drivers/rtc/rtc-goldfish.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. * Copyright (C) 2017 Imagination Technologies Ltd.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/rtc.h>
  19. #include <linux/io.h>
  20. #define TIMER_TIME_LOW 0x00 /* get low bits of current time */
  21. /* and update TIMER_TIME_HIGH */
  22. #define TIMER_TIME_HIGH 0x04 /* get high bits of time at last */
  23. /* TIMER_TIME_LOW read */
  24. #define TIMER_ALARM_LOW 0x08 /* set low bits of alarm and */
  25. /* activate it */
  26. #define TIMER_ALARM_HIGH 0x0c /* set high bits of next alarm */
  27. #define TIMER_IRQ_ENABLED 0x10
  28. #define TIMER_CLEAR_ALARM 0x14
  29. #define TIMER_ALARM_STATUS 0x18
  30. #define TIMER_CLEAR_INTERRUPT 0x1c
  31. struct goldfish_rtc {
  32. void __iomem *base;
  33. int irq;
  34. struct rtc_device *rtc;
  35. };
  36. static int goldfish_rtc_read_alarm(struct device *dev,
  37. struct rtc_wkalrm *alrm)
  38. {
  39. u64 rtc_alarm;
  40. u64 rtc_alarm_low;
  41. u64 rtc_alarm_high;
  42. void __iomem *base;
  43. struct goldfish_rtc *rtcdrv;
  44. rtcdrv = dev_get_drvdata(dev);
  45. base = rtcdrv->base;
  46. rtc_alarm_low = readl(base + TIMER_ALARM_LOW);
  47. rtc_alarm_high = readl(base + TIMER_ALARM_HIGH);
  48. rtc_alarm = (rtc_alarm_high << 32) | rtc_alarm_low;
  49. do_div(rtc_alarm, NSEC_PER_SEC);
  50. memset(alrm, 0, sizeof(struct rtc_wkalrm));
  51. rtc_time_to_tm(rtc_alarm, &alrm->time);
  52. if (readl(base + TIMER_ALARM_STATUS))
  53. alrm->enabled = 1;
  54. else
  55. alrm->enabled = 0;
  56. return 0;
  57. }
  58. static int goldfish_rtc_set_alarm(struct device *dev,
  59. struct rtc_wkalrm *alrm)
  60. {
  61. struct goldfish_rtc *rtcdrv;
  62. unsigned long rtc_alarm;
  63. u64 rtc_alarm64;
  64. u64 rtc_status_reg;
  65. void __iomem *base;
  66. int ret = 0;
  67. rtcdrv = dev_get_drvdata(dev);
  68. base = rtcdrv->base;
  69. if (alrm->enabled) {
  70. ret = rtc_tm_to_time(&alrm->time, &rtc_alarm);
  71. if (ret != 0)
  72. return ret;
  73. rtc_alarm64 = rtc_alarm * NSEC_PER_SEC;
  74. writel((rtc_alarm64 >> 32), base + TIMER_ALARM_HIGH);
  75. writel(rtc_alarm64, base + TIMER_ALARM_LOW);
  76. writel(1, base + TIMER_IRQ_ENABLED);
  77. } else {
  78. /*
  79. * if this function was called with enabled=0
  80. * then it could mean that the application is
  81. * trying to cancel an ongoing alarm
  82. */
  83. rtc_status_reg = readl(base + TIMER_ALARM_STATUS);
  84. if (rtc_status_reg)
  85. writel(1, base + TIMER_CLEAR_ALARM);
  86. }
  87. return ret;
  88. }
  89. static int goldfish_rtc_alarm_irq_enable(struct device *dev,
  90. unsigned int enabled)
  91. {
  92. void __iomem *base;
  93. struct goldfish_rtc *rtcdrv;
  94. rtcdrv = dev_get_drvdata(dev);
  95. base = rtcdrv->base;
  96. if (enabled)
  97. writel(1, base + TIMER_IRQ_ENABLED);
  98. else
  99. writel(0, base + TIMER_IRQ_ENABLED);
  100. return 0;
  101. }
  102. static irqreturn_t goldfish_rtc_interrupt(int irq, void *dev_id)
  103. {
  104. struct goldfish_rtc *rtcdrv = dev_id;
  105. void __iomem *base = rtcdrv->base;
  106. writel(1, base + TIMER_CLEAR_INTERRUPT);
  107. rtc_update_irq(rtcdrv->rtc, 1, RTC_IRQF | RTC_AF);
  108. return IRQ_HANDLED;
  109. }
  110. static int goldfish_rtc_read_time(struct device *dev, struct rtc_time *tm)
  111. {
  112. struct goldfish_rtc *rtcdrv;
  113. void __iomem *base;
  114. u64 time_high;
  115. u64 time_low;
  116. u64 time;
  117. rtcdrv = dev_get_drvdata(dev);
  118. base = rtcdrv->base;
  119. time_low = readl(base + TIMER_TIME_LOW);
  120. time_high = readl(base + TIMER_TIME_HIGH);
  121. time = (time_high << 32) | time_low;
  122. do_div(time, NSEC_PER_SEC);
  123. rtc_time_to_tm(time, tm);
  124. return 0;
  125. }
  126. static int goldfish_rtc_set_time(struct device *dev, struct rtc_time *tm)
  127. {
  128. struct goldfish_rtc *rtcdrv;
  129. void __iomem *base;
  130. unsigned long now;
  131. u64 now64;
  132. int ret;
  133. rtcdrv = dev_get_drvdata(dev);
  134. base = rtcdrv->base;
  135. ret = rtc_tm_to_time(tm, &now);
  136. if (ret == 0) {
  137. now64 = now * NSEC_PER_SEC;
  138. writel((now64 >> 32), base + TIMER_TIME_HIGH);
  139. writel(now64, base + TIMER_TIME_LOW);
  140. }
  141. return ret;
  142. }
  143. static const struct rtc_class_ops goldfish_rtc_ops = {
  144. .read_time = goldfish_rtc_read_time,
  145. .set_time = goldfish_rtc_set_time,
  146. .read_alarm = goldfish_rtc_read_alarm,
  147. .set_alarm = goldfish_rtc_set_alarm,
  148. .alarm_irq_enable = goldfish_rtc_alarm_irq_enable
  149. };
  150. static int goldfish_rtc_probe(struct platform_device *pdev)
  151. {
  152. struct goldfish_rtc *rtcdrv;
  153. struct resource *r;
  154. int err;
  155. rtcdrv = devm_kzalloc(&pdev->dev, sizeof(*rtcdrv), GFP_KERNEL);
  156. if (!rtcdrv)
  157. return -ENOMEM;
  158. platform_set_drvdata(pdev, rtcdrv);
  159. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  160. if (!r)
  161. return -ENODEV;
  162. rtcdrv->base = devm_ioremap_resource(&pdev->dev, r);
  163. if (IS_ERR(rtcdrv->base))
  164. return -ENODEV;
  165. rtcdrv->irq = platform_get_irq(pdev, 0);
  166. if (rtcdrv->irq < 0)
  167. return -ENODEV;
  168. rtcdrv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  169. &goldfish_rtc_ops,
  170. THIS_MODULE);
  171. if (IS_ERR(rtcdrv->rtc))
  172. return PTR_ERR(rtcdrv->rtc);
  173. err = devm_request_irq(&pdev->dev, rtcdrv->irq,
  174. goldfish_rtc_interrupt,
  175. 0, pdev->name, rtcdrv);
  176. if (err)
  177. return err;
  178. return 0;
  179. }
  180. static const struct of_device_id goldfish_rtc_of_match[] = {
  181. { .compatible = "google,goldfish-rtc", },
  182. {},
  183. };
  184. MODULE_DEVICE_TABLE(of, goldfish_rtc_of_match);
  185. static struct platform_driver goldfish_rtc = {
  186. .probe = goldfish_rtc_probe,
  187. .driver = {
  188. .name = "goldfish_rtc",
  189. .of_match_table = goldfish_rtc_of_match,
  190. }
  191. };
  192. module_platform_driver(goldfish_rtc);
  193. MODULE_LICENSE("GPL v2");