rtc-ftrtc010.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Faraday Technology FTRTC010 driver
  3. *
  4. * Copyright (C) 2009 Janos Laube <janos.dev@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Original code for older kernel 2.6.15 are from Stormlinksemi
  17. * first update from Janos Laube for > 2.6.29 kernels
  18. *
  19. * checkpatch fixes and usage of rtc-lib code
  20. * Hans Ulli Kroll <ulli.kroll@googlemail.com>
  21. */
  22. #include <linux/rtc.h>
  23. #include <linux/io.h>
  24. #include <linux/slab.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/clk.h>
  29. #define DRV_NAME "rtc-ftrtc010"
  30. MODULE_AUTHOR("Hans Ulli Kroll <ulli.kroll@googlemail.com>");
  31. MODULE_DESCRIPTION("RTC driver for Gemini SoC");
  32. MODULE_LICENSE("GPL");
  33. MODULE_ALIAS("platform:" DRV_NAME);
  34. struct ftrtc010_rtc {
  35. struct rtc_device *rtc_dev;
  36. void __iomem *rtc_base;
  37. int rtc_irq;
  38. struct clk *pclk;
  39. struct clk *extclk;
  40. };
  41. enum ftrtc010_rtc_offsets {
  42. FTRTC010_RTC_SECOND = 0x00,
  43. FTRTC010_RTC_MINUTE = 0x04,
  44. FTRTC010_RTC_HOUR = 0x08,
  45. FTRTC010_RTC_DAYS = 0x0C,
  46. FTRTC010_RTC_ALARM_SECOND = 0x10,
  47. FTRTC010_RTC_ALARM_MINUTE = 0x14,
  48. FTRTC010_RTC_ALARM_HOUR = 0x18,
  49. FTRTC010_RTC_RECORD = 0x1C,
  50. FTRTC010_RTC_CR = 0x20,
  51. };
  52. static irqreturn_t ftrtc010_rtc_interrupt(int irq, void *dev)
  53. {
  54. return IRQ_HANDLED;
  55. }
  56. /*
  57. * Looks like the RTC in the Gemini SoC is (totaly) broken
  58. * We can't read/write directly the time from RTC registers.
  59. * We must do some "offset" calculation to get the real time
  60. *
  61. * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
  62. * the same thing, without the rtc-lib.c calls.
  63. */
  64. static int ftrtc010_rtc_read_time(struct device *dev, struct rtc_time *tm)
  65. {
  66. struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
  67. u32 days, hour, min, sec, offset;
  68. timeu64_t time;
  69. sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
  70. min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
  71. hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
  72. days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
  73. offset = readl(rtc->rtc_base + FTRTC010_RTC_RECORD);
  74. time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
  75. rtc_time64_to_tm(time, tm);
  76. return 0;
  77. }
  78. static int ftrtc010_rtc_set_time(struct device *dev, struct rtc_time *tm)
  79. {
  80. struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
  81. u32 sec, min, hour, day, offset;
  82. timeu64_t time;
  83. time = rtc_tm_to_time64(tm);
  84. sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
  85. min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
  86. hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
  87. day = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
  88. offset = time - (day * 86400 + hour * 3600 + min * 60 + sec);
  89. writel(offset, rtc->rtc_base + FTRTC010_RTC_RECORD);
  90. writel(0x01, rtc->rtc_base + FTRTC010_RTC_CR);
  91. return 0;
  92. }
  93. static const struct rtc_class_ops ftrtc010_rtc_ops = {
  94. .read_time = ftrtc010_rtc_read_time,
  95. .set_time = ftrtc010_rtc_set_time,
  96. };
  97. static int ftrtc010_rtc_probe(struct platform_device *pdev)
  98. {
  99. u32 days, hour, min, sec;
  100. struct ftrtc010_rtc *rtc;
  101. struct device *dev = &pdev->dev;
  102. struct resource *res;
  103. int ret;
  104. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  105. if (unlikely(!rtc))
  106. return -ENOMEM;
  107. platform_set_drvdata(pdev, rtc);
  108. rtc->pclk = devm_clk_get(dev, "PCLK");
  109. if (IS_ERR(rtc->pclk)) {
  110. dev_err(dev, "could not get PCLK\n");
  111. } else {
  112. ret = clk_prepare_enable(rtc->pclk);
  113. if (ret) {
  114. dev_err(dev, "failed to enable PCLK\n");
  115. return ret;
  116. }
  117. }
  118. rtc->extclk = devm_clk_get(dev, "EXTCLK");
  119. if (IS_ERR(rtc->extclk)) {
  120. dev_err(dev, "could not get EXTCLK\n");
  121. } else {
  122. ret = clk_prepare_enable(rtc->extclk);
  123. if (ret) {
  124. dev_err(dev, "failed to enable EXTCLK\n");
  125. return ret;
  126. }
  127. }
  128. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  129. if (!res)
  130. return -ENODEV;
  131. rtc->rtc_irq = res->start;
  132. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  133. if (!res)
  134. return -ENODEV;
  135. rtc->rtc_base = devm_ioremap(dev, res->start,
  136. resource_size(res));
  137. if (!rtc->rtc_base)
  138. return -ENOMEM;
  139. rtc->rtc_dev = devm_rtc_allocate_device(dev);
  140. if (IS_ERR(rtc->rtc_dev))
  141. return PTR_ERR(rtc->rtc_dev);
  142. rtc->rtc_dev->ops = &ftrtc010_rtc_ops;
  143. sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
  144. min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
  145. hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
  146. days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
  147. rtc->rtc_dev->range_min = (u64)days * 86400 + hour * 3600 +
  148. min * 60 + sec;
  149. rtc->rtc_dev->range_max = U32_MAX + rtc->rtc_dev->range_min;
  150. ret = devm_request_irq(dev, rtc->rtc_irq, ftrtc010_rtc_interrupt,
  151. IRQF_SHARED, pdev->name, dev);
  152. if (unlikely(ret))
  153. return ret;
  154. return rtc_register_device(rtc->rtc_dev);
  155. }
  156. static int ftrtc010_rtc_remove(struct platform_device *pdev)
  157. {
  158. struct ftrtc010_rtc *rtc = platform_get_drvdata(pdev);
  159. if (!IS_ERR(rtc->extclk))
  160. clk_disable_unprepare(rtc->extclk);
  161. if (!IS_ERR(rtc->pclk))
  162. clk_disable_unprepare(rtc->pclk);
  163. return 0;
  164. }
  165. static const struct of_device_id ftrtc010_rtc_dt_match[] = {
  166. { .compatible = "cortina,gemini-rtc" },
  167. { .compatible = "faraday,ftrtc010" },
  168. { }
  169. };
  170. MODULE_DEVICE_TABLE(of, ftrtc010_rtc_dt_match);
  171. static struct platform_driver ftrtc010_rtc_driver = {
  172. .driver = {
  173. .name = DRV_NAME,
  174. .of_match_table = ftrtc010_rtc_dt_match,
  175. },
  176. .probe = ftrtc010_rtc_probe,
  177. .remove = ftrtc010_rtc_remove,
  178. };
  179. module_platform_driver_probe(ftrtc010_rtc_driver, ftrtc010_rtc_probe);