rtc.h 6.1 KB

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