rtc.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 udevice;
  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_set() - 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. #ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
  151. int rtc_enable_32khz_output(int busnum, int chip_addr);
  152. #endif
  153. #else
  154. int rtc_get (struct rtc_time *);
  155. int rtc_set (struct rtc_time *);
  156. void rtc_reset (void);
  157. #ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
  158. void rtc_enable_32khz_output(void);
  159. #endif
  160. /**
  161. * rtc_read8() - Read an 8-bit register
  162. *
  163. * @reg: Register to read
  164. * @return value read
  165. */
  166. int rtc_read8(int reg);
  167. /**
  168. * rtc_write8() - Write an 8-bit register
  169. *
  170. * @reg: Register to write
  171. * @value: Value to write
  172. */
  173. void rtc_write8(int reg, uchar val);
  174. /**
  175. * rtc_read32() - Read a 32-bit value from the RTC
  176. *
  177. * @reg: Offset to start reading from
  178. * @return value read
  179. */
  180. u32 rtc_read32(int reg);
  181. /**
  182. * rtc_write32() - Write a 32-bit value to the RTC
  183. *
  184. * @reg: Register to start writing to
  185. * @value: Value to write
  186. */
  187. void rtc_write32(int reg, u32 value);
  188. /**
  189. * rtc_init() - Set up the real time clock ready for use
  190. */
  191. void rtc_init(void);
  192. #endif /* CONFIG_DM_RTC */
  193. /**
  194. * is_leap_year - Check if year is a leap year
  195. *
  196. * @year Year
  197. * @return 1 if leap year
  198. */
  199. static inline bool is_leap_year(unsigned int year)
  200. {
  201. return (!(year % 4) && (year % 100)) || !(year % 400);
  202. }
  203. /**
  204. * rtc_calc_weekday() - Work out the weekday from a time
  205. *
  206. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK).
  207. * It sets time->tm_wdaay to the correct day of the week.
  208. *
  209. * @time: Time to inspect. tm_wday is updated
  210. * @return 0 if OK, -EINVAL if the weekday could not be determined
  211. */
  212. int rtc_calc_weekday(struct rtc_time *time);
  213. /**
  214. * rtc_to_tm() - Convert a time_t value into a broken-out time
  215. *
  216. * The following fields are set up by this function:
  217. * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday
  218. *
  219. * Note that tm_yday and tm_isdst are set to 0.
  220. *
  221. * @time_t: Number of seconds since 1970-01-01 00:00:00
  222. * @time: Place to put the broken-out time
  223. */
  224. void rtc_to_tm(u64 time_t, struct rtc_time *time);
  225. /**
  226. * rtc_mktime() - Convert a broken-out time into a time_t value
  227. *
  228. * The following fields need to be valid for this function to work:
  229. * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year
  230. *
  231. * Note that tm_wday and tm_yday are ignored.
  232. *
  233. * @time: Broken-out time to convert
  234. * @return corresponding time_t value, seconds since 1970-01-01 00:00:00
  235. */
  236. unsigned long rtc_mktime(const struct rtc_time *time);
  237. /**
  238. * rtc_month_days() - The number of days in the month
  239. *
  240. * @month: month (January = 0)
  241. * @year: year (4 digits)
  242. */
  243. int rtc_month_days(unsigned int month, unsigned int year);
  244. #endif /* _RTC_H_ */