rtc.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. * read() - Read multiple 8-bit registers
  51. *
  52. * @dev: Device to read from
  53. * @reg: First register to read
  54. * @buf: Output buffer
  55. * @len: Number of registers to read
  56. * @return 0 if OK, -ve on error
  57. */
  58. int (*read)(struct udevice *dev, unsigned int reg,
  59. u8 *buf, unsigned int len);
  60. /**
  61. * write() - Write multiple 8-bit registers
  62. *
  63. * @dev: Device to write to
  64. * @reg: First register to write
  65. * @buf: Input buffer
  66. * @len: Number of registers to write
  67. * @return 0 if OK, -ve on error
  68. */
  69. int (*write)(struct udevice *dev, unsigned int reg,
  70. const u8 *buf, unsigned int len);
  71. /**
  72. * read8() - Read an 8-bit register
  73. *
  74. * @dev: Device to read from
  75. * @reg: Register to read
  76. * @return value read, or -ve on error
  77. */
  78. int (*read8)(struct udevice *dev, unsigned int reg);
  79. /**
  80. * write8() - Write an 8-bit register
  81. *
  82. * @dev: Device to write to
  83. * @reg: Register to write
  84. * @value: Value to write
  85. * @return 0 if OK, -ve on error
  86. */
  87. int (*write8)(struct udevice *dev, unsigned int reg, int val);
  88. };
  89. /* Access the operations for an RTC device */
  90. #define rtc_get_ops(dev) ((struct rtc_ops *)(dev)->driver->ops)
  91. /**
  92. * dm_rtc_get() - Read the time from an RTC
  93. *
  94. * @dev: Device to read from
  95. * @time: Place to put the current time
  96. * @return 0 if OK, -ve on error
  97. */
  98. int dm_rtc_get(struct udevice *dev, struct rtc_time *time);
  99. /**
  100. * dm_rtc_set() - Write a time to an RTC
  101. *
  102. * @dev: Device to read from
  103. * @time: Time to write into the RTC
  104. * @return 0 if OK, -ve on error
  105. */
  106. int dm_rtc_set(struct udevice *dev, struct rtc_time *time);
  107. /**
  108. * dm_rtc_reset() - reset the RTC to a known-good state
  109. *
  110. * If the RTC appears to be broken (e.g. it is not counting up in seconds)
  111. * it may need to be reset to a known good state. This function achieves this.
  112. * After resetting the RTC the time should then be set to a known value by
  113. * the caller.
  114. *
  115. * @dev: Device to read from
  116. * @return 0 if OK, -ve on error
  117. */
  118. int dm_rtc_reset(struct udevice *dev);
  119. /**
  120. * dm_rtc_read() - Read multiple 8-bit registers
  121. *
  122. * @dev: Device to read from
  123. * @reg: First register to read
  124. * @buf: Output buffer
  125. * @len: Number of registers to read
  126. * @return 0 if OK, -ve on error
  127. */
  128. int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf, unsigned int len);
  129. /**
  130. * dm_rtc_write() - Write multiple 8-bit registers
  131. *
  132. * @dev: Device to write to
  133. * @reg: First register to write
  134. * @buf: Input buffer
  135. * @len: Number of registers to write
  136. * @return 0 if OK, -ve on error
  137. */
  138. int dm_rtc_write(struct udevice *dev, unsigned int reg,
  139. const u8 *buf, unsigned int len);
  140. /**
  141. * rtc_read8() - Read an 8-bit register
  142. *
  143. * @dev: Device to read from
  144. * @reg: Register to read
  145. * @return value read, or -ve on error
  146. */
  147. int rtc_read8(struct udevice *dev, unsigned int reg);
  148. /**
  149. * rtc_write8() - Write an 8-bit register
  150. *
  151. * @dev: Device to write to
  152. * @reg: Register to write
  153. * @value: Value to write
  154. * @return 0 if OK, -ve on error
  155. */
  156. int rtc_write8(struct udevice *dev, unsigned int reg, int val);
  157. /**
  158. * rtc_read16() - Read a 16-bit value from the RTC
  159. *
  160. * @dev: Device to read from
  161. * @reg: Offset to start reading from
  162. * @valuep: Place to put the value that is read
  163. * @return 0 if OK, -ve on error
  164. */
  165. int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep);
  166. /**
  167. * rtc_write16() - Write a 16-bit value to the RTC
  168. *
  169. * @dev: Device to write to
  170. * @reg: Register to start writing to
  171. * @value: Value to write
  172. * @return 0 if OK, -ve on error
  173. */
  174. int rtc_write16(struct udevice *dev, unsigned int reg, u16 value);
  175. /**
  176. * rtc_read32() - Read a 32-bit value from the RTC
  177. *
  178. * @dev: Device to read from
  179. * @reg: Offset to start reading from
  180. * @valuep: Place to put the value that is read
  181. * @return 0 if OK, -ve on error
  182. */
  183. int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep);
  184. /**
  185. * rtc_write32() - Write a 32-bit value to the RTC
  186. *
  187. * @dev: Device to write to
  188. * @reg: Register to start writing to
  189. * @value: Value to write
  190. * @return 0 if OK, -ve on error
  191. */
  192. int rtc_write32(struct udevice *dev, unsigned int reg, u32 value);
  193. #ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
  194. int rtc_enable_32khz_output(int busnum, int chip_addr);
  195. #endif
  196. #else
  197. int rtc_get (struct rtc_time *);
  198. int rtc_set (struct rtc_time *);
  199. void rtc_reset (void);
  200. #ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
  201. void rtc_enable_32khz_output(void);
  202. #endif
  203. /**
  204. * rtc_read8() - Read an 8-bit register
  205. *
  206. * @reg: Register to read
  207. * @return value read
  208. */
  209. int rtc_read8(int reg);
  210. /**
  211. * rtc_write8() - Write an 8-bit register
  212. *
  213. * @reg: Register to write
  214. * @value: Value to write
  215. */
  216. void rtc_write8(int reg, uchar val);
  217. /**
  218. * rtc_read32() - Read a 32-bit value from the RTC
  219. *
  220. * @reg: Offset to start reading from
  221. * @return value read
  222. */
  223. u32 rtc_read32(int reg);
  224. /**
  225. * rtc_write32() - Write a 32-bit value to the RTC
  226. *
  227. * @reg: Register to start writing to
  228. * @value: Value to write
  229. */
  230. void rtc_write32(int reg, u32 value);
  231. /**
  232. * rtc_init() - Set up the real time clock ready for use
  233. */
  234. void rtc_init(void);
  235. #endif /* CONFIG_DM_RTC */
  236. /**
  237. * is_leap_year - Check if year is a leap year
  238. *
  239. * @year Year
  240. * @return 1 if leap year
  241. */
  242. static inline bool is_leap_year(unsigned int year)
  243. {
  244. return (!(year % 4) && (year % 100)) || !(year % 400);
  245. }
  246. /**
  247. * rtc_calc_weekday() - Work out the weekday from a time
  248. *
  249. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK).
  250. * It sets time->tm_wdaay to the correct day of the week.
  251. *
  252. * @time: Time to inspect. tm_wday is updated
  253. * @return 0 if OK, -EINVAL if the weekday could not be determined
  254. */
  255. int rtc_calc_weekday(struct rtc_time *time);
  256. /**
  257. * rtc_to_tm() - Convert a time_t value into a broken-out time
  258. *
  259. * The following fields are set up by this function:
  260. * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday
  261. *
  262. * Note that tm_yday and tm_isdst are set to 0.
  263. *
  264. * @time_t: Number of seconds since 1970-01-01 00:00:00
  265. * @time: Place to put the broken-out time
  266. */
  267. void rtc_to_tm(u64 time_t, struct rtc_time *time);
  268. /**
  269. * rtc_mktime() - Convert a broken-out time into a time_t value
  270. *
  271. * The following fields need to be valid for this function to work:
  272. * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year
  273. *
  274. * Note that tm_wday and tm_yday are ignored.
  275. *
  276. * @time: Broken-out time to convert
  277. * @return corresponding time_t value, seconds since 1970-01-01 00:00:00
  278. */
  279. unsigned long rtc_mktime(const struct rtc_time *time);
  280. /**
  281. * rtc_month_days() - The number of days in the month
  282. *
  283. * @month: month (January = 0)
  284. * @year: year (4 digits)
  285. */
  286. int rtc_month_days(unsigned int month, unsigned int year);
  287. #endif /* _RTC_H_ */