rtc.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Generic RTC interface.
  9. */
  10. #ifndef _RTC_H_
  11. #define _RTC_H_
  12. /* bcd<->bin functions are needed by almost all the RTC drivers, let's include
  13. * it there instead of in evey single driver */
  14. #include <bcd.h>
  15. #include <rtc_def.h>
  16. #ifdef CONFIG_DM_RTC
  17. struct rtc_ops {
  18. /**
  19. * get() - get the current time
  20. *
  21. * Returns the current time read from the RTC device. The driver
  22. * is responsible for setting up every field in the structure.
  23. *
  24. * @dev: Device to read from
  25. * @time: Place to put the time that is read
  26. */
  27. int (*get)(struct udevice *dev, struct rtc_time *time);
  28. /**
  29. * set() - set the current time
  30. *
  31. * Sets the time in the RTC device. The driver can expect every
  32. * field to be set correctly.
  33. *
  34. * @dev: Device to read from
  35. * @time: Time to write
  36. */
  37. int (*set)(struct udevice *dev, const struct rtc_time *time);
  38. /**
  39. * reset() - reset the RTC to a known-good state
  40. *
  41. * This function resets the RTC to a known-good state. The time may
  42. * be unset by this method, so should be set after this method is
  43. * called.
  44. *
  45. * @dev: Device to read from
  46. * @return 0 if OK, -ve on error
  47. */
  48. int (*reset)(struct udevice *dev);
  49. /**
  50. * read8() - Read an 8-bit register
  51. *
  52. * @dev: Device to read from
  53. * @reg: Register to read
  54. * @return value read, or -ve on error
  55. */
  56. int (*read8)(struct udevice *dev, unsigned int reg);
  57. /**
  58. * write8() - Write an 8-bit register
  59. *
  60. * @dev: Device to write to
  61. * @reg: Register to write
  62. * @value: Value to write
  63. * @return 0 if OK, -ve on error
  64. */
  65. int (*write8)(struct udevice *dev, unsigned int reg, int val);
  66. };
  67. /* Access the operations for an RTC device */
  68. #define rtc_get_ops(dev) ((struct rtc_ops *)(dev)->driver->ops)
  69. /**
  70. * dm_rtc_get() - Read the time from an RTC
  71. *
  72. * @dev: Device to read from
  73. * @time: Place to put the current time
  74. * @return 0 if OK, -ve on error
  75. */
  76. int dm_rtc_get(struct udevice *dev, struct rtc_time *time);
  77. /**
  78. * dm_rtc_put() - Write a time to an RTC
  79. *
  80. * @dev: Device to read from
  81. * @time: Time to write into the RTC
  82. * @return 0 if OK, -ve on error
  83. */
  84. int dm_rtc_set(struct udevice *dev, struct rtc_time *time);
  85. /**
  86. * dm_rtc_reset() - reset the RTC to a known-good state
  87. *
  88. * If the RTC appears to be broken (e.g. it is not counting up in seconds)
  89. * it may need to be reset to a known good state. This function achieves this.
  90. * After resetting the RTC the time should then be set to a known value by
  91. * the caller.
  92. *
  93. * @dev: Device to read from
  94. * @return 0 if OK, -ve on error
  95. */
  96. int dm_rtc_reset(struct udevice *dev);
  97. /**
  98. * rtc_read8() - Read an 8-bit register
  99. *
  100. * @dev: Device to read from
  101. * @reg: Register to read
  102. * @return value read, or -ve on error
  103. */
  104. int rtc_read8(struct udevice *dev, unsigned int reg);
  105. /**
  106. * rtc_write8() - Write an 8-bit register
  107. *
  108. * @dev: Device to write to
  109. * @reg: Register to write
  110. * @value: Value to write
  111. * @return 0 if OK, -ve on error
  112. */
  113. int rtc_write8(struct udevice *dev, unsigned int reg, int val);
  114. /**
  115. * rtc_read32() - Read a 32-bit value from the RTC
  116. *
  117. * @dev: Device to read from
  118. * @reg: Offset to start reading from
  119. * @valuep: Place to put the value that is read
  120. * @return 0 if OK, -ve on error
  121. */
  122. int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep);
  123. /**
  124. * rtc_write32() - Write a 32-bit value to the RTC
  125. *
  126. * @dev: Device to write to
  127. * @reg: Register to start writing to
  128. * @value: Value to write
  129. * @return 0 if OK, -ve on error
  130. */
  131. int rtc_write32(struct udevice *dev, unsigned int reg, u32 value);
  132. #else
  133. int rtc_get (struct rtc_time *);
  134. int rtc_set (struct rtc_time *);
  135. void rtc_reset (void);
  136. void rtc_enable_32khz_output(void);
  137. /**
  138. * rtc_read8() - Read an 8-bit register
  139. *
  140. * @reg: Register to read
  141. * @return value read
  142. */
  143. int rtc_read8(int reg);
  144. /**
  145. * rtc_write8() - Write an 8-bit register
  146. *
  147. * @reg: Register to write
  148. * @value: Value to write
  149. */
  150. void rtc_write8(int reg, uchar val);
  151. /**
  152. * rtc_read32() - Read a 32-bit value from the RTC
  153. *
  154. * @reg: Offset to start reading from
  155. * @return value read
  156. */
  157. u32 rtc_read32(int reg);
  158. /**
  159. * rtc_write32() - Write a 32-bit value to the RTC
  160. *
  161. * @reg: Register to start writing to
  162. * @value: Value to write
  163. */
  164. void rtc_write32(int reg, u32 value);
  165. /**
  166. * rtc_init() - Set up the real time clock ready for use
  167. */
  168. void rtc_init(void);
  169. #endif
  170. /**
  171. * rtc_calc_weekday() - Work out the weekday from a time
  172. *
  173. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK).
  174. * It sets time->tm_wdaay to the correct day of the week.
  175. *
  176. * @time: Time to inspect. tm_wday is updated
  177. * @return 0 if OK, -EINVAL if the weekday could not be determined
  178. */
  179. int rtc_calc_weekday(struct rtc_time *time);
  180. /**
  181. * rtc_to_tm() - Convert a time_t value into a broken-out time
  182. *
  183. * The following fields are set up by this function:
  184. * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday
  185. *
  186. * Note that tm_yday and tm_isdst are set to 0.
  187. *
  188. * @time_t: Number of seconds since 1970-01-01 00:00:00
  189. * @time: Place to put the broken-out time
  190. * @return 0 if OK, -EINVAL if the weekday could not be determined
  191. */
  192. int rtc_to_tm(int time_t, struct rtc_time *time);
  193. /**
  194. * rtc_mktime() - Convert a broken-out time into a time_t value
  195. *
  196. * The following fields need to be valid for this function to work:
  197. * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year
  198. *
  199. * Note that tm_wday and tm_yday are ignored.
  200. *
  201. * @time: Broken-out time to convert
  202. * @return corresponding time_t value, seconds since 1970-01-01 00:00:00
  203. */
  204. unsigned long rtc_mktime(const struct rtc_time *time);
  205. #endif /* _RTC_H_ */