rtc-ftrtc010.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/mod_devicetable.h>
  29. #include <linux/clk.h>
  30. #define DRV_NAME "rtc-ftrtc010"
  31. MODULE_AUTHOR("Hans Ulli Kroll <ulli.kroll@googlemail.com>");
  32. MODULE_DESCRIPTION("RTC driver for Gemini SoC");
  33. MODULE_LICENSE("GPL");
  34. MODULE_ALIAS("platform:" DRV_NAME);
  35. struct ftrtc010_rtc {
  36. struct rtc_device *rtc_dev;
  37. void __iomem *rtc_base;
  38. int rtc_irq;
  39. struct clk *pclk;
  40. struct clk *extclk;
  41. };
  42. enum ftrtc010_rtc_offsets {
  43. FTRTC010_RTC_SECOND = 0x00,
  44. FTRTC010_RTC_MINUTE = 0x04,
  45. FTRTC010_RTC_HOUR = 0x08,
  46. FTRTC010_RTC_DAYS = 0x0C,
  47. FTRTC010_RTC_ALARM_SECOND = 0x10,
  48. FTRTC010_RTC_ALARM_MINUTE = 0x14,
  49. FTRTC010_RTC_ALARM_HOUR = 0x18,
  50. FTRTC010_RTC_RECORD = 0x1C,
  51. FTRTC010_RTC_CR = 0x20,
  52. };
  53. static irqreturn_t ftrtc010_rtc_interrupt(int irq, void *dev)
  54. {
  55. return IRQ_HANDLED;
  56. }
  57. /*
  58. * Looks like the RTC in the Gemini SoC is (totaly) broken
  59. * We can't read/write directly the time from RTC registers.
  60. * We must do some "offset" calculation to get the real time
  61. *
  62. * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
  63. * the same thing, without the rtc-lib.c calls.
  64. */
  65. static int ftrtc010_rtc_read_time(struct device *dev, struct rtc_time *tm)
  66. {
  67. struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
  68. u32 days, hour, min, sec, offset;
  69. timeu64_t time;
  70. sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
  71. min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
  72. hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
  73. days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
  74. offset = readl(rtc->rtc_base + FTRTC010_RTC_RECORD);
  75. time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
  76. rtc_time64_to_tm(time, tm);
  77. return 0;
  78. }
  79. static int ftrtc010_rtc_set_time(struct device *dev, struct rtc_time *tm)
  80. {
  81. struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
  82. u32 sec, min, hour, day, offset;
  83. timeu64_t time;
  84. time = rtc_tm_to_time64(tm);
  85. sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
  86. min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
  87. hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
  88. day = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
  89. offset = time - (day * 86400 + hour * 3600 + min * 60 + sec);
  90. writel(offset, rtc->rtc_base + FTRTC010_RTC_RECORD);
  91. writel(0x01, rtc->rtc_base + FTRTC010_RTC_CR);
  92. return 0;
  93. }
  94. static const struct rtc_class_ops ftrtc010_rtc_ops = {
  95. .read_time = ftrtc010_rtc_read_time,
  96. .set_time = ftrtc010_rtc_set_time,
  97. };
  98. static int ftrtc010_rtc_probe(struct platform_device *pdev)
  99. {
  100. u32 days, hour, min, sec;
  101. struct ftrtc010_rtc *rtc;
  102. struct device *dev = &pdev->dev;
  103. struct resource *res;
  104. int ret;
  105. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  106. if (unlikely(!rtc))
  107. return -ENOMEM;
  108. platform_set_drvdata(pdev, rtc);
  109. rtc->pclk = devm_clk_get(dev, "PCLK");
  110. if (IS_ERR(rtc->pclk)) {
  111. dev_err(dev, "could not get PCLK\n");
  112. } else {
  113. ret = clk_prepare_enable(rtc->pclk);
  114. if (ret) {
  115. dev_err(dev, "failed to enable PCLK\n");
  116. return ret;
  117. }
  118. }
  119. rtc->extclk = devm_clk_get(dev, "EXTCLK");
  120. if (IS_ERR(rtc->extclk)) {
  121. dev_err(dev, "could not get EXTCLK\n");
  122. } else {
  123. ret = clk_prepare_enable(rtc->extclk);
  124. if (ret) {
  125. dev_err(dev, "failed to enable EXTCLK\n");
  126. return ret;
  127. }
  128. }
  129. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  130. if (!res)
  131. return -ENODEV;
  132. rtc->rtc_irq = res->start;
  133. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  134. if (!res)
  135. return -ENODEV;
  136. rtc->rtc_base = devm_ioremap(dev, res->start,
  137. resource_size(res));
  138. if (!rtc->rtc_base)
  139. return -ENOMEM;
  140. rtc->rtc_dev = devm_rtc_allocate_device(dev);
  141. if (IS_ERR(rtc->rtc_dev))
  142. return PTR_ERR(rtc->rtc_dev);
  143. rtc->rtc_dev->ops = &ftrtc010_rtc_ops;
  144. sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
  145. min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
  146. hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
  147. days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
  148. rtc->rtc_dev->range_min = (u64)days * 86400 + hour * 3600 +
  149. min * 60 + sec;
  150. rtc->rtc_dev->range_max = U32_MAX + rtc->rtc_dev->range_min;
  151. ret = devm_request_irq(dev, rtc->rtc_irq, ftrtc010_rtc_interrupt,
  152. IRQF_SHARED, pdev->name, dev);
  153. if (unlikely(ret))
  154. return ret;
  155. return rtc_register_device(rtc->rtc_dev);
  156. }
  157. static int ftrtc010_rtc_remove(struct platform_device *pdev)
  158. {
  159. struct ftrtc010_rtc *rtc = platform_get_drvdata(pdev);
  160. if (!IS_ERR(rtc->extclk))
  161. clk_disable_unprepare(rtc->extclk);
  162. if (!IS_ERR(rtc->pclk))
  163. clk_disable_unprepare(rtc->pclk);
  164. return 0;
  165. }
  166. static const struct of_device_id ftrtc010_rtc_dt_match[] = {
  167. { .compatible = "cortina,gemini-rtc" },
  168. { .compatible = "faraday,ftrtc010" },
  169. { }
  170. };
  171. MODULE_DEVICE_TABLE(of, ftrtc010_rtc_dt_match);
  172. static struct platform_driver ftrtc010_rtc_driver = {
  173. .driver = {
  174. .name = DRV_NAME,
  175. .of_match_table = ftrtc010_rtc_dt_match,
  176. },
  177. .probe = ftrtc010_rtc_probe,
  178. .remove = ftrtc010_rtc_remove,
  179. };
  180. module_platform_driver_probe(ftrtc010_rtc_driver, ftrtc010_rtc_probe);