rtc.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_read16() - Read a 16-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_read16(struct udevice *dev, unsigned int reg, u16 *valuep);
  123. /**
  124. * rtc_write16() - Write a 16-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_write16(struct udevice *dev, unsigned int reg, u16 value);
  132. /**
  133. * rtc_read32() - Read a 32-bit value from the RTC
  134. *
  135. * @dev: Device to read from
  136. * @reg: Offset to start reading from
  137. * @valuep: Place to put the value that is read
  138. * @return 0 if OK, -ve on error
  139. */
  140. int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep);
  141. /**
  142. * rtc_write32() - Write a 32-bit value to the RTC
  143. *
  144. * @dev: Device to write to
  145. * @reg: Register to start writing to
  146. * @value: Value to write
  147. * @return 0 if OK, -ve on error
  148. */
  149. int rtc_write32(struct udevice *dev, unsigned int reg, u32 value);
  150. #else
  151. int rtc_get (struct rtc_time *);
  152. int rtc_set (struct rtc_time *);
  153. void rtc_reset (void);
  154. void rtc_enable_32khz_output(void);
  155. /**
  156. * rtc_read8() - Read an 8-bit register
  157. *
  158. * @reg: Register to read
  159. * @return value read
  160. */
  161. int rtc_read8(int reg);
  162. /**
  163. * rtc_write8() - Write an 8-bit register
  164. *
  165. * @reg: Register to write
  166. * @value: Value to write
  167. */
  168. void rtc_write8(int reg, uchar val);
  169. /**
  170. * rtc_read32() - Read a 32-bit value from the RTC
  171. *
  172. * @reg: Offset to start reading from
  173. * @return value read
  174. */
  175. u32 rtc_read32(int reg);
  176. /**
  177. * rtc_write32() - Write a 32-bit value to the RTC
  178. *
  179. * @reg: Register to start writing to
  180. * @value: Value to write
  181. */
  182. void rtc_write32(int reg, u32 value);
  183. /**
  184. * rtc_init() - Set up the real time clock ready for use
  185. */
  186. void rtc_init(void);
  187. #endif
  188. /**
  189. * rtc_calc_weekday() - Work out the weekday from a time
  190. *
  191. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK).
  192. * It sets time->tm_wdaay to the correct day of the week.
  193. *
  194. * @time: Time to inspect. tm_wday is updated
  195. * @return 0 if OK, -EINVAL if the weekday could not be determined
  196. */
  197. int rtc_calc_weekday(struct rtc_time *time);
  198. /**
  199. * rtc_to_tm() - Convert a time_t value into a broken-out time
  200. *
  201. * The following fields are set up by this function:
  202. * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday
  203. *
  204. * Note that tm_yday and tm_isdst are set to 0.
  205. *
  206. * @time_t: Number of seconds since 1970-01-01 00:00:00
  207. * @time: Place to put the broken-out time
  208. * @return 0 if OK, -EINVAL if the weekday could not be determined
  209. */
  210. int rtc_to_tm(int time_t, struct rtc_time *time);
  211. /**
  212. * rtc_mktime() - Convert a broken-out time into a time_t value
  213. *
  214. * The following fields need to be valid for this function to work:
  215. * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year
  216. *
  217. * Note that tm_wday and tm_yday are ignored.
  218. *
  219. * @time: Broken-out time to convert
  220. * @return corresponding time_t value, seconds since 1970-01-01 00:00:00
  221. */
  222. unsigned long rtc_mktime(const struct rtc_time *time);
  223. #endif /* _RTC_H_ */