rtc-goldfish.c 5.5 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. } else {
  77. /*
  78. * if this function was called with enabled=0
  79. * then it could mean that the application is
  80. * trying to cancel an ongoing alarm
  81. */
  82. rtc_status_reg = readl(base + TIMER_ALARM_STATUS);
  83. if (rtc_status_reg)
  84. writel(1, base + TIMER_CLEAR_ALARM);
  85. }
  86. return ret;
  87. }
  88. static int goldfish_rtc_alarm_irq_enable(struct device *dev,
  89. unsigned int enabled)
  90. {
  91. void __iomem *base;
  92. struct goldfish_rtc *rtcdrv;
  93. rtcdrv = dev_get_drvdata(dev);
  94. base = rtcdrv->base;
  95. if (enabled)
  96. writel(1, base + TIMER_IRQ_ENABLED);
  97. else
  98. writel(0, base + TIMER_IRQ_ENABLED);
  99. return 0;
  100. }
  101. static irqreturn_t goldfish_rtc_interrupt(int irq, void *dev_id)
  102. {
  103. struct goldfish_rtc *rtcdrv = dev_id;
  104. void __iomem *base = rtcdrv->base;
  105. writel(1, base + TIMER_CLEAR_INTERRUPT);
  106. rtc_update_irq(rtcdrv->rtc, 1, RTC_IRQF | RTC_AF);
  107. return IRQ_HANDLED;
  108. }
  109. static int goldfish_rtc_read_time(struct device *dev, struct rtc_time *tm)
  110. {
  111. struct goldfish_rtc *rtcdrv;
  112. void __iomem *base;
  113. u64 time_high;
  114. u64 time_low;
  115. u64 time;
  116. rtcdrv = dev_get_drvdata(dev);
  117. base = rtcdrv->base;
  118. time_low = readl(base + TIMER_TIME_LOW);
  119. time_high = readl(base + TIMER_TIME_HIGH);
  120. time = (time_high << 32) | time_low;
  121. do_div(time, NSEC_PER_SEC);
  122. rtc_time_to_tm(time, tm);
  123. return 0;
  124. }
  125. static int goldfish_rtc_set_time(struct device *dev, struct rtc_time *tm)
  126. {
  127. struct goldfish_rtc *rtcdrv;
  128. void __iomem *base;
  129. unsigned long now;
  130. u64 now64;
  131. int ret;
  132. rtcdrv = dev_get_drvdata(dev);
  133. base = rtcdrv->base;
  134. ret = rtc_tm_to_time(tm, &now);
  135. if (ret == 0) {
  136. now64 = now * NSEC_PER_SEC;
  137. writel((now64 >> 32), base + TIMER_TIME_HIGH);
  138. writel(now64, base + TIMER_TIME_LOW);
  139. }
  140. return ret;
  141. }
  142. static const struct rtc_class_ops goldfish_rtc_ops = {
  143. .read_time = goldfish_rtc_read_time,
  144. .set_time = goldfish_rtc_set_time,
  145. .read_alarm = goldfish_rtc_read_alarm,
  146. .set_alarm = goldfish_rtc_set_alarm,
  147. .alarm_irq_enable = goldfish_rtc_alarm_irq_enable
  148. };
  149. static int goldfish_rtc_probe(struct platform_device *pdev)
  150. {
  151. struct goldfish_rtc *rtcdrv;
  152. struct resource *r;
  153. int err;
  154. rtcdrv = devm_kzalloc(&pdev->dev, sizeof(*rtcdrv), GFP_KERNEL);
  155. if (!rtcdrv)
  156. return -ENOMEM;
  157. platform_set_drvdata(pdev, rtcdrv);
  158. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  159. if (!r)
  160. return -ENODEV;
  161. rtcdrv->base = devm_ioremap_resource(&pdev->dev, r);
  162. if (IS_ERR(rtcdrv->base))
  163. return -ENODEV;
  164. rtcdrv->irq = platform_get_irq(pdev, 0);
  165. if (rtcdrv->irq < 0)
  166. return -ENODEV;
  167. rtcdrv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  168. &goldfish_rtc_ops,
  169. THIS_MODULE);
  170. if (IS_ERR(rtcdrv->rtc))
  171. return PTR_ERR(rtcdrv->rtc);
  172. err = devm_request_irq(&pdev->dev, rtcdrv->irq,
  173. goldfish_rtc_interrupt,
  174. 0, pdev->name, rtcdrv);
  175. if (err)
  176. return err;
  177. return 0;
  178. }
  179. static const struct of_device_id goldfish_rtc_of_match[] = {
  180. { .compatible = "google,goldfish-rtc", },
  181. {},
  182. };
  183. MODULE_DEVICE_TABLE(of, goldfish_rtc_of_match);
  184. static struct platform_driver goldfish_rtc = {
  185. .probe = goldfish_rtc_probe,
  186. .driver = {
  187. .name = "goldfish_rtc",
  188. .of_match_table = goldfish_rtc_of_match,
  189. }
  190. };
  191. module_platform_driver(goldfish_rtc);
  192. MODULE_LICENSE("GPL v2");