rtc-spacemit-p1.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the RTC found in the SpacemiT P1 PMIC
  4. *
  5. * Copyright (C) 2025 by RISCstar Solutions Corporation. All rights reserved.
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/regmap.h>
  12. #include <linux/rtc.h>
  13. #define MOD_NAME "spacemit-p1-rtc"
  14. /*
  15. * Six consecutive 1-byte registers hold the seconds, minutes, hours,
  16. * day-of-month, month, and year (respectively).
  17. *
  18. * The range of values in these registers is:
  19. * seconds 0-59
  20. * minutes 0-59
  21. * hours 0-59
  22. * day 0-30 (struct tm is 1-31)
  23. * month 0-11
  24. * year years since 2000 (struct tm is since 1900)
  25. *
  26. * Note that the day and month must be converted after reading and
  27. * before writing.
  28. */
  29. #define RTC_TIME 0x0d /* Offset of the seconds register */
  30. #define RTC_CTRL 0x1d
  31. #define RTC_EN BIT(2)
  32. /* Number of attempts to read a consistent time stamp before giving up */
  33. #define RTC_READ_TRIES 20 /* At least 1 */
  34. struct p1_rtc {
  35. struct regmap *regmap;
  36. struct rtc_device *rtc;
  37. };
  38. /*
  39. * The P1 hardware documentation states that the register values are
  40. * latched to ensure a consistent time snapshot within the registers,
  41. * but these are in fact unstable due to a bug in the hardware design.
  42. * So we loop until we get two identical readings.
  43. */
  44. static int p1_rtc_read_time(struct device *dev, struct rtc_time *t)
  45. {
  46. struct p1_rtc *p1 = dev_get_drvdata(dev);
  47. struct regmap *regmap = p1->regmap;
  48. u32 count = RTC_READ_TRIES;
  49. u8 seconds;
  50. u8 time[6];
  51. int ret;
  52. if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN))
  53. return -EINVAL; /* RTC is disabled */
  54. ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
  55. if (ret)
  56. return ret;
  57. do {
  58. seconds = time[0];
  59. ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
  60. if (ret)
  61. return ret;
  62. } while (time[0] != seconds && --count);
  63. if (!count)
  64. return -EIO; /* Unable to get a consistent result */
  65. t->tm_sec = time[0] & GENMASK(5, 0);
  66. t->tm_min = time[1] & GENMASK(5, 0);
  67. t->tm_hour = time[2] & GENMASK(4, 0);
  68. t->tm_mday = (time[3] & GENMASK(4, 0)) + 1;
  69. t->tm_mon = time[4] & GENMASK(3, 0);
  70. t->tm_year = (time[5] & GENMASK(5, 0)) + 100;
  71. return 0;
  72. }
  73. /*
  74. * The P1 hardware documentation states that values in the registers are
  75. * latched so when written they represent a consistent time snapshot.
  76. * Nevertheless, this is not guaranteed by the implementation, so we must
  77. * disable the RTC while updating it.
  78. */
  79. static int p1_rtc_set_time(struct device *dev, struct rtc_time *t)
  80. {
  81. struct p1_rtc *p1 = dev_get_drvdata(dev);
  82. struct regmap *regmap = p1->regmap;
  83. u8 time[6];
  84. int ret;
  85. time[0] = t->tm_sec;
  86. time[1] = t->tm_min;
  87. time[2] = t->tm_hour;
  88. time[3] = t->tm_mday - 1;
  89. time[4] = t->tm_mon;
  90. time[5] = t->tm_year - 100;
  91. /* Disable the RTC to update; re-enable again when done */
  92. ret = regmap_clear_bits(regmap, RTC_CTRL, RTC_EN);
  93. if (ret)
  94. return ret;
  95. /* If something goes wrong, leave the RTC disabled */
  96. ret = regmap_bulk_write(regmap, RTC_TIME, time, sizeof(time));
  97. if (ret)
  98. return ret;
  99. return regmap_set_bits(regmap, RTC_CTRL, RTC_EN);
  100. }
  101. static const struct rtc_class_ops p1_rtc_class_ops = {
  102. .read_time = p1_rtc_read_time,
  103. .set_time = p1_rtc_set_time,
  104. };
  105. static int p1_rtc_probe(struct platform_device *pdev)
  106. {
  107. struct device *dev = &pdev->dev;
  108. struct rtc_device *rtc;
  109. struct p1_rtc *p1;
  110. p1 = devm_kzalloc(dev, sizeof(*p1), GFP_KERNEL);
  111. if (!p1)
  112. return -ENOMEM;
  113. dev_set_drvdata(dev, p1);
  114. p1->regmap = dev_get_regmap(dev->parent, NULL);
  115. if (!p1->regmap)
  116. return dev_err_probe(dev, -ENODEV, "failed to get regmap\n");
  117. rtc = devm_rtc_allocate_device(dev);
  118. if (IS_ERR(rtc))
  119. return dev_err_probe(dev, PTR_ERR(rtc),
  120. "error allocating device\n");
  121. p1->rtc = rtc;
  122. rtc->ops = &p1_rtc_class_ops;
  123. rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
  124. rtc->range_max = RTC_TIMESTAMP_END_2063;
  125. clear_bit(RTC_FEATURE_ALARM, rtc->features);
  126. clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
  127. return devm_rtc_register_device(rtc);
  128. }
  129. static struct platform_driver p1_rtc_driver = {
  130. .probe = p1_rtc_probe,
  131. .driver = {
  132. .name = MOD_NAME,
  133. },
  134. };
  135. module_platform_driver(p1_rtc_driver);
  136. MODULE_DESCRIPTION("SpacemiT P1 RTC driver");
  137. MODULE_LICENSE("GPL");
  138. MODULE_ALIAS("platform:" MOD_NAME);