420-rtc-armada38x-add-support-for-trimming-the-RTC.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. commit f94ffbc2c2a4128c4412bb483d0807722dfb682b
  2. Author: Russell King <rmk+kernel@armlinux.org.uk>
  3. Date: Fri Sep 29 11:23:31 2017 +0100
  4. rtc: armada38x: add support for trimming the RTC
  5. Add support for trimming the RTC using the offset mechanism. This RTC
  6. supports two modes: low update mode and high update mode. Low update
  7. mode has finer precision than high update mode, so we use the low mode
  8. where possible.
  9. Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
  10. Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
  11. --- a/drivers/rtc/rtc-armada38x.c
  12. +++ b/drivers/rtc/rtc-armada38x.c
  13. @@ -28,6 +28,8 @@
  14. #define RTC_IRQ_AL_EN BIT(0)
  15. #define RTC_IRQ_FREQ_EN BIT(1)
  16. #define RTC_IRQ_FREQ_1HZ BIT(2)
  17. +#define RTC_CCR 0x18
  18. +#define RTC_CCR_MODE BIT(15)
  19. #define RTC_TIME 0xC
  20. #define RTC_ALARM1 0x10
  21. @@ -343,18 +345,117 @@ static irqreturn_t armada38x_rtc_alarm_i
  22. return IRQ_HANDLED;
  23. }
  24. +/*
  25. + * The information given in the Armada 388 functional spec is complex.
  26. + * They give two different formulas for calculating the offset value,
  27. + * but when considering "Offset" as an 8-bit signed integer, they both
  28. + * reduce down to (we shall rename "Offset" as "val" here):
  29. + *
  30. + * val = (f_ideal / f_measured - 1) / resolution where f_ideal = 32768
  31. + *
  32. + * Converting to time, f = 1/t:
  33. + * val = (t_measured / t_ideal - 1) / resolution where t_ideal = 1/32768
  34. + *
  35. + * => t_measured / t_ideal = val * resolution + 1
  36. + *
  37. + * "offset" in the RTC interface is defined as:
  38. + * t = t0 * (1 + offset * 1e-9)
  39. + * where t is the desired period, t0 is the measured period with a zero
  40. + * offset, which is t_measured above. With t0 = t_measured and t = t_ideal,
  41. + * offset = (t_ideal / t_measured - 1) / 1e-9
  42. + *
  43. + * => t_ideal / t_measured = offset * 1e-9 + 1
  44. + *
  45. + * so:
  46. + *
  47. + * offset * 1e-9 + 1 = 1 / (val * resolution + 1)
  48. + *
  49. + * We want "resolution" to be an integer, so resolution = R * 1e-9, giving
  50. + * offset = 1e18 / (val * R + 1e9) - 1e9
  51. + * val = (1e18 / (offset + 1e9) - 1e9) / R
  52. + * with a common transformation:
  53. + * f(x) = 1e18 / (x + 1e9) - 1e9
  54. + * offset = f(val * R)
  55. + * val = f(offset) / R
  56. + *
  57. + * Armada 38x supports two modes, fine mode (954ppb) and coarse mode (3815ppb).
  58. + */
  59. +static long armada38x_ppb_convert(long ppb)
  60. +{
  61. + long div = ppb + 1000000000L;
  62. +
  63. + return div_s64(1000000000000000000LL + div / 2, div) - 1000000000L;
  64. +}
  65. +
  66. +static int armada38x_rtc_read_offset(struct device *dev, long *offset)
  67. +{
  68. + struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  69. + unsigned long ccr, flags;
  70. + long ppb_cor;
  71. +
  72. + spin_lock_irqsave(&rtc->lock, flags);
  73. + ccr = rtc->data->read_rtc_reg(rtc, RTC_CCR);
  74. + spin_unlock_irqrestore(&rtc->lock, flags);
  75. +
  76. + ppb_cor = (ccr & RTC_CCR_MODE ? 3815 : 954) * (s8)ccr;
  77. + /* ppb_cor + 1000000000L can never be zero */
  78. + *offset = armada38x_ppb_convert(ppb_cor);
  79. +
  80. + return 0;
  81. +}
  82. +
  83. +static int armada38x_rtc_set_offset(struct device *dev, long offset)
  84. +{
  85. + struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  86. + unsigned long ccr = 0;
  87. + long ppb_cor, off;
  88. +
  89. + /*
  90. + * The maximum ppb_cor is -128 * 3815 .. 127 * 3815, but we
  91. + * need to clamp the input. This equates to -484270 .. 488558.
  92. + * Not only is this to stop out of range "off" but also to
  93. + * avoid the division by zero in armada38x_ppb_convert().
  94. + */
  95. + offset = clamp(offset, -484270L, 488558L);
  96. +
  97. + ppb_cor = armada38x_ppb_convert(offset);
  98. +
  99. + /*
  100. + * Use low update mode where possible, which gives a better
  101. + * resolution of correction.
  102. + */
  103. + off = DIV_ROUND_CLOSEST(ppb_cor, 954);
  104. + if (off > 127 || off < -128) {
  105. + ccr = RTC_CCR_MODE;
  106. + off = DIV_ROUND_CLOSEST(ppb_cor, 3815);
  107. + }
  108. +
  109. + /*
  110. + * Armada 388 requires a bit pattern in bits 14..8 depending on
  111. + * the sign bit: { 0, ~S, S, S, S, S, S }
  112. + */
  113. + ccr |= (off & 0x3fff) ^ 0x2000;
  114. + rtc_delayed_write(ccr, rtc, RTC_CCR);
  115. +
  116. + return 0;
  117. +}
  118. +
  119. static const struct rtc_class_ops armada38x_rtc_ops = {
  120. .read_time = armada38x_rtc_read_time,
  121. .set_time = armada38x_rtc_set_time,
  122. .read_alarm = armada38x_rtc_read_alarm,
  123. .set_alarm = armada38x_rtc_set_alarm,
  124. .alarm_irq_enable = armada38x_rtc_alarm_irq_enable,
  125. + .read_offset = armada38x_rtc_read_offset,
  126. + .set_offset = armada38x_rtc_set_offset,
  127. };
  128. static const struct rtc_class_ops armada38x_rtc_ops_noirq = {
  129. .read_time = armada38x_rtc_read_time,
  130. .set_time = armada38x_rtc_set_time,
  131. .read_alarm = armada38x_rtc_read_alarm,
  132. + .read_offset = armada38x_rtc_read_offset,
  133. + .set_offset = armada38x_rtc_set_offset,
  134. };
  135. static const struct armada38x_rtc_data armada38x_data = {