rtc.h 8.4 KB

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