rtc-ftrtc010.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. unsigned int days, hour, min, sec;
  68. unsigned long offset, 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_time_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. unsigned int sec, min, hour, day;
  82. unsigned long offset, time;
  83. if (tm->tm_year >= 2148) /* EPOCH Year + 179 */
  84. return -EINVAL;
  85. rtc_tm_to_time(tm, &time);
  86. sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
  87. min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
  88. hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
  89. day = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
  90. offset = time - (day * 86400 + hour * 3600 + min * 60 + sec);
  91. writel(offset, rtc->rtc_base + FTRTC010_RTC_RECORD);
  92. writel(0x01, rtc->rtc_base + FTRTC010_RTC_CR);
  93. return 0;
  94. }
  95. static const struct rtc_class_ops ftrtc010_rtc_ops = {
  96. .read_time = ftrtc010_rtc_read_time,
  97. .set_time = ftrtc010_rtc_set_time,
  98. };
  99. static int ftrtc010_rtc_probe(struct platform_device *pdev)
  100. {
  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. ret = devm_request_irq(dev, rtc->rtc_irq, ftrtc010_rtc_interrupt,
  141. IRQF_SHARED, pdev->name, dev);
  142. if (unlikely(ret))
  143. return ret;
  144. rtc->rtc_dev = rtc_device_register(pdev->name, dev,
  145. &ftrtc010_rtc_ops, THIS_MODULE);
  146. return PTR_ERR_OR_ZERO(rtc->rtc_dev);
  147. }
  148. static int ftrtc010_rtc_remove(struct platform_device *pdev)
  149. {
  150. struct ftrtc010_rtc *rtc = platform_get_drvdata(pdev);
  151. if (!IS_ERR(rtc->extclk))
  152. clk_disable_unprepare(rtc->extclk);
  153. if (!IS_ERR(rtc->pclk))
  154. clk_disable_unprepare(rtc->pclk);
  155. rtc_device_unregister(rtc->rtc_dev);
  156. return 0;
  157. }
  158. static const struct of_device_id ftrtc010_rtc_dt_match[] = {
  159. { .compatible = "cortina,gemini-rtc" },
  160. { .compatible = "faraday,ftrtc010" },
  161. { }
  162. };
  163. MODULE_DEVICE_TABLE(of, ftrtc010_rtc_dt_match);
  164. static struct platform_driver ftrtc010_rtc_driver = {
  165. .driver = {
  166. .name = DRV_NAME,
  167. .of_match_table = ftrtc010_rtc_dt_match,
  168. },
  169. .probe = ftrtc010_rtc_probe,
  170. .remove = ftrtc010_rtc_remove,
  171. };
  172. module_platform_driver_probe(ftrtc010_rtc_driver, ftrtc010_rtc_probe);