Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
wdenk | a6840a6 | 2001-04-09 21:43:07 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | a6840a6 | 2001-04-09 21:43:07 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Generic RTC interface. |
| 9 | */ |
| 10 | #ifndef _RTC_H_ |
| 11 | #define _RTC_H_ |
| 12 | |
Albin Tonnerre | dfdea73 | 2009-08-13 15:31:12 +0200 | [diff] [blame] | 13 | /* bcd<->bin functions are needed by almost all the RTC drivers, let's include |
| 14 | * it there instead of in evey single driver */ |
| 15 | |
| 16 | #include <bcd.h> |
Simon Glass | 573e3f7 | 2015-04-20 12:37:21 -0600 | [diff] [blame] | 17 | #include <rtc_def.h> |
Sean Anderson | 446b628 | 2022-11-22 12:54:52 -0500 | [diff] [blame] | 18 | #include <linux/errno.h> |
wdenk | a6840a6 | 2001-04-09 21:43:07 +0000 | [diff] [blame] | 19 | |
Jan Kiszka | aa763a5 | 2022-04-24 11:34:56 +0200 | [diff] [blame] | 20 | typedef int64_t time64_t; |
AKASHI Takahiro | 80eb59a | 2019-11-13 09:44:48 +0900 | [diff] [blame] | 21 | struct udevice; |
| 22 | |
Sean Anderson | 446b628 | 2022-11-22 12:54:52 -0500 | [diff] [blame] | 23 | #if CONFIG_IS_ENABLED(DM_RTC) |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 24 | struct rtc_ops { |
| 25 | /** |
| 26 | * get() - get the current time |
| 27 | * |
| 28 | * Returns the current time read from the RTC device. The driver |
| 29 | * is responsible for setting up every field in the structure. |
| 30 | * |
| 31 | * @dev: Device to read from |
| 32 | * @time: Place to put the time that is read |
| 33 | */ |
| 34 | int (*get)(struct udevice *dev, struct rtc_time *time); |
| 35 | |
| 36 | /** |
| 37 | * set() - set the current time |
| 38 | * |
| 39 | * Sets the time in the RTC device. The driver can expect every |
| 40 | * field to be set correctly. |
| 41 | * |
| 42 | * @dev: Device to read from |
| 43 | * @time: Time to write |
| 44 | */ |
| 45 | int (*set)(struct udevice *dev, const struct rtc_time *time); |
| 46 | |
| 47 | /** |
| 48 | * reset() - reset the RTC to a known-good state |
| 49 | * |
| 50 | * This function resets the RTC to a known-good state. The time may |
| 51 | * be unset by this method, so should be set after this method is |
| 52 | * called. |
| 53 | * |
| 54 | * @dev: Device to read from |
| 55 | * @return 0 if OK, -ve on error |
| 56 | */ |
| 57 | int (*reset)(struct udevice *dev); |
| 58 | |
| 59 | /** |
Rasmus Villemoes | b9e40a2 | 2020-07-06 22:01:10 +0200 | [diff] [blame] | 60 | * read() - Read multiple 8-bit registers |
| 61 | * |
| 62 | * @dev: Device to read from |
| 63 | * @reg: First register to read |
| 64 | * @buf: Output buffer |
| 65 | * @len: Number of registers to read |
| 66 | * @return 0 if OK, -ve on error |
| 67 | */ |
| 68 | int (*read)(struct udevice *dev, unsigned int reg, |
| 69 | u8 *buf, unsigned int len); |
| 70 | |
| 71 | /** |
Rasmus Villemoes | 579c992 | 2020-07-06 22:01:11 +0200 | [diff] [blame] | 72 | * write() - Write multiple 8-bit registers |
| 73 | * |
| 74 | * @dev: Device to write to |
| 75 | * @reg: First register to write |
| 76 | * @buf: Input buffer |
| 77 | * @len: Number of registers to write |
| 78 | * @return 0 if OK, -ve on error |
| 79 | */ |
| 80 | int (*write)(struct udevice *dev, unsigned int reg, |
| 81 | const u8 *buf, unsigned int len); |
| 82 | |
| 83 | /** |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 84 | * read8() - Read an 8-bit register |
| 85 | * |
| 86 | * @dev: Device to read from |
| 87 | * @reg: Register to read |
| 88 | * @return value read, or -ve on error |
| 89 | */ |
| 90 | int (*read8)(struct udevice *dev, unsigned int reg); |
| 91 | |
| 92 | /** |
| 93 | * write8() - Write an 8-bit register |
| 94 | * |
| 95 | * @dev: Device to write to |
| 96 | * @reg: Register to write |
| 97 | * @value: Value to write |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 98 | * Return: 0 if OK, -ve on error |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 99 | */ |
| 100 | int (*write8)(struct udevice *dev, unsigned int reg, int val); |
| 101 | }; |
| 102 | |
| 103 | /* Access the operations for an RTC device */ |
| 104 | #define rtc_get_ops(dev) ((struct rtc_ops *)(dev)->driver->ops) |
| 105 | |
| 106 | /** |
| 107 | * dm_rtc_get() - Read the time from an RTC |
| 108 | * |
| 109 | * @dev: Device to read from |
| 110 | * @time: Place to put the current time |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 111 | * Return: 0 if OK, -ve on error |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 112 | */ |
| 113 | int dm_rtc_get(struct udevice *dev, struct rtc_time *time); |
| 114 | |
| 115 | /** |
Philipp Tomsich | e5568fc | 2018-11-25 19:32:54 +0100 | [diff] [blame] | 116 | * dm_rtc_set() - Write a time to an RTC |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 117 | * |
| 118 | * @dev: Device to read from |
| 119 | * @time: Time to write into the RTC |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 120 | * Return: 0 if OK, -ve on error |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 121 | */ |
| 122 | int dm_rtc_set(struct udevice *dev, struct rtc_time *time); |
| 123 | |
| 124 | /** |
| 125 | * dm_rtc_reset() - reset the RTC to a known-good state |
| 126 | * |
| 127 | * If the RTC appears to be broken (e.g. it is not counting up in seconds) |
| 128 | * it may need to be reset to a known good state. This function achieves this. |
| 129 | * After resetting the RTC the time should then be set to a known value by |
| 130 | * the caller. |
| 131 | * |
| 132 | * @dev: Device to read from |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 133 | * Return: 0 if OK, -ve on error |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 134 | */ |
| 135 | int dm_rtc_reset(struct udevice *dev); |
| 136 | |
| 137 | /** |
Rasmus Villemoes | b9e40a2 | 2020-07-06 22:01:10 +0200 | [diff] [blame] | 138 | * dm_rtc_read() - Read multiple 8-bit registers |
| 139 | * |
| 140 | * @dev: Device to read from |
| 141 | * @reg: First register to read |
| 142 | * @buf: Output buffer |
| 143 | * @len: Number of registers to read |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 144 | * Return: 0 if OK, -ve on error |
Rasmus Villemoes | b9e40a2 | 2020-07-06 22:01:10 +0200 | [diff] [blame] | 145 | */ |
| 146 | int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf, unsigned int len); |
| 147 | |
| 148 | /** |
Rasmus Villemoes | 579c992 | 2020-07-06 22:01:11 +0200 | [diff] [blame] | 149 | * dm_rtc_write() - Write multiple 8-bit registers |
| 150 | * |
| 151 | * @dev: Device to write to |
| 152 | * @reg: First register to write |
| 153 | * @buf: Input buffer |
| 154 | * @len: Number of registers to write |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 155 | * Return: 0 if OK, -ve on error |
Rasmus Villemoes | 579c992 | 2020-07-06 22:01:11 +0200 | [diff] [blame] | 156 | */ |
| 157 | int dm_rtc_write(struct udevice *dev, unsigned int reg, |
| 158 | const u8 *buf, unsigned int len); |
| 159 | |
| 160 | /** |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 161 | * rtc_read8() - Read an 8-bit register |
| 162 | * |
| 163 | * @dev: Device to read from |
| 164 | * @reg: Register to read |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 165 | * Return: value read, or -ve on error |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 166 | */ |
| 167 | int rtc_read8(struct udevice *dev, unsigned int reg); |
| 168 | |
| 169 | /** |
| 170 | * rtc_write8() - Write an 8-bit register |
| 171 | * |
| 172 | * @dev: Device to write to |
| 173 | * @reg: Register to write |
| 174 | * @value: Value to write |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 175 | * Return: 0 if OK, -ve on error |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 176 | */ |
| 177 | int rtc_write8(struct udevice *dev, unsigned int reg, int val); |
| 178 | |
| 179 | /** |
Bin Meng | fbfe53d | 2017-03-16 07:26:27 -0700 | [diff] [blame] | 180 | * rtc_read16() - Read a 16-bit value from the RTC |
| 181 | * |
| 182 | * @dev: Device to read from |
| 183 | * @reg: Offset to start reading from |
| 184 | * @valuep: Place to put the value that is read |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 185 | * Return: 0 if OK, -ve on error |
Bin Meng | fbfe53d | 2017-03-16 07:26:27 -0700 | [diff] [blame] | 186 | */ |
| 187 | int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep); |
| 188 | |
| 189 | /** |
| 190 | * rtc_write16() - Write a 16-bit value to the RTC |
| 191 | * |
| 192 | * @dev: Device to write to |
| 193 | * @reg: Register to start writing to |
| 194 | * @value: Value to write |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 195 | * Return: 0 if OK, -ve on error |
Bin Meng | fbfe53d | 2017-03-16 07:26:27 -0700 | [diff] [blame] | 196 | */ |
| 197 | int rtc_write16(struct udevice *dev, unsigned int reg, u16 value); |
| 198 | |
| 199 | /** |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 200 | * rtc_read32() - Read a 32-bit value from the RTC |
| 201 | * |
| 202 | * @dev: Device to read from |
| 203 | * @reg: Offset to start reading from |
| 204 | * @valuep: Place to put the value that is read |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 205 | * Return: 0 if OK, -ve on error |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 206 | */ |
| 207 | int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep); |
| 208 | |
| 209 | /** |
| 210 | * rtc_write32() - Write a 32-bit value to the RTC |
| 211 | * |
| 212 | * @dev: Device to write to |
| 213 | * @reg: Register to start writing to |
| 214 | * @value: Value to write |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 215 | * Return: 0 if OK, -ve on error |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 216 | */ |
| 217 | int rtc_write32(struct udevice *dev, unsigned int reg, u32 value); |
| 218 | |
Chuanhua Han | 4f97aac | 2019-07-26 19:24:00 +0800 | [diff] [blame] | 219 | #ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT |
| 220 | int rtc_enable_32khz_output(int busnum, int chip_addr); |
| 221 | #endif |
| 222 | |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 223 | #else |
Sean Anderson | 446b628 | 2022-11-22 12:54:52 -0500 | [diff] [blame] | 224 | static inline int dm_rtc_get(struct udevice *dev, struct rtc_time *time) |
| 225 | { |
| 226 | return -ENOSYS; |
| 227 | } |
| 228 | |
| 229 | static inline int dm_rtc_set(struct udevice *dev, struct rtc_time *time) |
| 230 | { |
| 231 | return -ENOSYS; |
| 232 | } |
| 233 | |
| 234 | static inline int dm_rtc_reset(struct udevice *dev) |
| 235 | { |
| 236 | return -ENOSYS; |
| 237 | } |
| 238 | |
| 239 | static inline int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf, |
| 240 | unsigned int len) |
| 241 | { |
| 242 | return -ENOSYS; |
| 243 | } |
| 244 | |
| 245 | static inline int dm_rtc_write(struct udevice *dev, unsigned int reg, |
| 246 | const u8 *buf, unsigned int len) |
| 247 | { |
| 248 | return -ENOSYS; |
| 249 | } |
| 250 | |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 251 | int rtc_get (struct rtc_time *); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 252 | int rtc_set (struct rtc_time *); |
wdenk | a6840a6 | 2001-04-09 21:43:07 +0000 | [diff] [blame] | 253 | void rtc_reset (void); |
Chuanhua Han | 4f97aac | 2019-07-26 19:24:00 +0800 | [diff] [blame] | 254 | #ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT |
Priyanka Jain | 28cf0bc | 2015-06-29 15:39:23 +0530 | [diff] [blame] | 255 | void rtc_enable_32khz_output(void); |
Chuanhua Han | 4f97aac | 2019-07-26 19:24:00 +0800 | [diff] [blame] | 256 | #endif |
wdenk | a6840a6 | 2001-04-09 21:43:07 +0000 | [diff] [blame] | 257 | |
Simon Glass | 538059d | 2014-11-14 18:18:26 -0700 | [diff] [blame] | 258 | /** |
Simon Glass | b4ae600 | 2015-01-19 22:16:10 -0700 | [diff] [blame] | 259 | * rtc_read8() - Read an 8-bit register |
| 260 | * |
| 261 | * @reg: Register to read |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 262 | * Return: value read |
Simon Glass | b4ae600 | 2015-01-19 22:16:10 -0700 | [diff] [blame] | 263 | */ |
| 264 | int rtc_read8(int reg); |
| 265 | |
| 266 | /** |
| 267 | * rtc_write8() - Write an 8-bit register |
| 268 | * |
| 269 | * @reg: Register to write |
| 270 | * @value: Value to write |
| 271 | */ |
| 272 | void rtc_write8(int reg, uchar val); |
| 273 | |
| 274 | /** |
| 275 | * rtc_read32() - Read a 32-bit value from the RTC |
| 276 | * |
| 277 | * @reg: Offset to start reading from |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 278 | * Return: value read |
Simon Glass | b4ae600 | 2015-01-19 22:16:10 -0700 | [diff] [blame] | 279 | */ |
| 280 | u32 rtc_read32(int reg); |
| 281 | |
| 282 | /** |
| 283 | * rtc_write32() - Write a 32-bit value to the RTC |
| 284 | * |
| 285 | * @reg: Register to start writing to |
| 286 | * @value: Value to write |
| 287 | */ |
| 288 | void rtc_write32(int reg, u32 value); |
| 289 | |
| 290 | /** |
Simon Glass | 538059d | 2014-11-14 18:18:26 -0700 | [diff] [blame] | 291 | * rtc_init() - Set up the real time clock ready for use |
| 292 | */ |
| 293 | void rtc_init(void); |
Heinrich Schuchardt | 57da69d | 2018-07-07 23:39:11 +0200 | [diff] [blame] | 294 | #endif /* CONFIG_DM_RTC */ |
Simon Glass | 538059d | 2014-11-14 18:18:26 -0700 | [diff] [blame] | 295 | |
Simon Glass | 2d937bb | 2015-04-20 12:37:17 -0600 | [diff] [blame] | 296 | /** |
Heinrich Schuchardt | 57da69d | 2018-07-07 23:39:11 +0200 | [diff] [blame] | 297 | * is_leap_year - Check if year is a leap year |
| 298 | * |
| 299 | * @year Year |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 300 | * Return: 1 if leap year |
Heinrich Schuchardt | 57da69d | 2018-07-07 23:39:11 +0200 | [diff] [blame] | 301 | */ |
| 302 | static inline bool is_leap_year(unsigned int year) |
| 303 | { |
| 304 | return (!(year % 4) && (year % 100)) || !(year % 400); |
| 305 | } |
| 306 | |
| 307 | /** |
Simon Glass | 2d937bb | 2015-04-20 12:37:17 -0600 | [diff] [blame] | 308 | * rtc_calc_weekday() - Work out the weekday from a time |
| 309 | * |
| 310 | * This only works for the Gregorian calendar - i.e. after 1752 (in the UK). |
| 311 | * It sets time->tm_wdaay to the correct day of the week. |
| 312 | * |
| 313 | * @time: Time to inspect. tm_wday is updated |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 314 | * Return: 0 if OK, -EINVAL if the weekday could not be determined |
Simon Glass | 2d937bb | 2015-04-20 12:37:17 -0600 | [diff] [blame] | 315 | */ |
| 316 | int rtc_calc_weekday(struct rtc_time *time); |
| 317 | |
Simon Glass | 1299466 | 2015-04-20 12:37:18 -0600 | [diff] [blame] | 318 | /** |
| 319 | * rtc_to_tm() - Convert a time_t value into a broken-out time |
| 320 | * |
| 321 | * The following fields are set up by this function: |
| 322 | * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday |
| 323 | * |
| 324 | * Note that tm_yday and tm_isdst are set to 0. |
| 325 | * |
| 326 | * @time_t: Number of seconds since 1970-01-01 00:00:00 |
| 327 | * @time: Place to put the broken-out time |
Simon Glass | 1299466 | 2015-04-20 12:37:18 -0600 | [diff] [blame] | 328 | */ |
Heinrich Schuchardt | 57da69d | 2018-07-07 23:39:11 +0200 | [diff] [blame] | 329 | void rtc_to_tm(u64 time_t, struct rtc_time *time); |
Simon Glass | 1299466 | 2015-04-20 12:37:18 -0600 | [diff] [blame] | 330 | |
Simon Glass | 4283e84 | 2015-04-20 12:37:19 -0600 | [diff] [blame] | 331 | /** |
Jan Kiszka | aa763a5 | 2022-04-24 11:34:56 +0200 | [diff] [blame] | 332 | * rtc_mktime() - Convert a broken-out time into a time64_t value |
Simon Glass | 4283e84 | 2015-04-20 12:37:19 -0600 | [diff] [blame] | 333 | * |
| 334 | * The following fields need to be valid for this function to work: |
| 335 | * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year |
| 336 | * |
| 337 | * Note that tm_wday and tm_yday are ignored. |
| 338 | * |
| 339 | * @time: Broken-out time to convert |
Jan Kiszka | aa763a5 | 2022-04-24 11:34:56 +0200 | [diff] [blame] | 340 | * Return: corresponding time64_t value, seconds since 1970-01-01 00:00:00 |
Simon Glass | 4283e84 | 2015-04-20 12:37:19 -0600 | [diff] [blame] | 341 | */ |
Jan Kiszka | aa763a5 | 2022-04-24 11:34:56 +0200 | [diff] [blame] | 342 | time64_t rtc_mktime(const struct rtc_time *time); |
Simon Glass | 4283e84 | 2015-04-20 12:37:19 -0600 | [diff] [blame] | 343 | |
Heinrich Schuchardt | df27efb | 2019-05-31 07:21:03 +0200 | [diff] [blame] | 344 | /** |
| 345 | * rtc_month_days() - The number of days in the month |
| 346 | * |
| 347 | * @month: month (January = 0) |
| 348 | * @year: year (4 digits) |
| 349 | */ |
| 350 | int rtc_month_days(unsigned int month, unsigned int year); |
| 351 | |
wdenk | a6840a6 | 2001-04-09 21:43:07 +0000 | [diff] [blame] | 352 | #endif /* _RTC_H_ */ |