1 /* SPDX-License-Identifier: GPL-2.0+ */
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * Generic RTC interface.
13 /* bcd<->bin functions are needed by almost all the RTC drivers, let's include
14 * it there instead of in evey single driver */
18 #include <linux/errno.h>
20 typedef int64_t time64_t;
23 #if CONFIG_IS_ENABLED(DM_RTC)
26 * get() - get the current time
28 * Returns the current time read from the RTC device. The driver
29 * is responsible for setting up every field in the structure.
31 * @dev: Device to read from
32 * @time: Place to put the time that is read
34 int (*get)(struct udevice *dev, struct rtc_time *time);
37 * set() - set the current time
39 * Sets the time in the RTC device. The driver can expect every
40 * field to be set correctly.
42 * @dev: Device to read from
43 * @time: Time to write
45 int (*set)(struct udevice *dev, const struct rtc_time *time);
48 * reset() - reset the RTC to a known-good state
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
54 * @dev: Device to read from
55 * @return 0 if OK, -ve on error
57 int (*reset)(struct udevice *dev);
60 * read() - Read multiple 8-bit registers
62 * @dev: Device to read from
63 * @reg: First register to read
65 * @len: Number of registers to read
66 * @return 0 if OK, -ve on error
68 int (*read)(struct udevice *dev, unsigned int reg,
69 u8 *buf, unsigned int len);
72 * write() - Write multiple 8-bit registers
74 * @dev: Device to write to
75 * @reg: First register to write
77 * @len: Number of registers to write
78 * @return 0 if OK, -ve on error
80 int (*write)(struct udevice *dev, unsigned int reg,
81 const u8 *buf, unsigned int len);
84 * read8() - Read an 8-bit register
86 * @dev: Device to read from
87 * @reg: Register to read
88 * @return value read, or -ve on error
90 int (*read8)(struct udevice *dev, unsigned int reg);
93 * write8() - Write an 8-bit register
95 * @dev: Device to write to
96 * @reg: Register to write
97 * @value: Value to write
98 * Return: 0 if OK, -ve on error
100 int (*write8)(struct udevice *dev, unsigned int reg, int val);
103 /* Access the operations for an RTC device */
104 #define rtc_get_ops(dev) ((struct rtc_ops *)(dev)->driver->ops)
107 * dm_rtc_get() - Read the time from an RTC
109 * @dev: Device to read from
110 * @time: Place to put the current time
111 * Return: 0 if OK, -ve on error
113 int dm_rtc_get(struct udevice *dev, struct rtc_time *time);
116 * dm_rtc_set() - Write a time to an RTC
118 * @dev: Device to read from
119 * @time: Time to write into the RTC
120 * Return: 0 if OK, -ve on error
122 int dm_rtc_set(struct udevice *dev, struct rtc_time *time);
125 * dm_rtc_reset() - reset the RTC to a known-good state
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
132 * @dev: Device to read from
133 * Return: 0 if OK, -ve on error
135 int dm_rtc_reset(struct udevice *dev);
138 * dm_rtc_read() - Read multiple 8-bit registers
140 * @dev: Device to read from
141 * @reg: First register to read
142 * @buf: Output buffer
143 * @len: Number of registers to read
144 * Return: 0 if OK, -ve on error
146 int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf, unsigned int len);
149 * dm_rtc_write() - Write multiple 8-bit registers
151 * @dev: Device to write to
152 * @reg: First register to write
154 * @len: Number of registers to write
155 * Return: 0 if OK, -ve on error
157 int dm_rtc_write(struct udevice *dev, unsigned int reg,
158 const u8 *buf, unsigned int len);
161 * rtc_read8() - Read an 8-bit register
163 * @dev: Device to read from
164 * @reg: Register to read
165 * Return: value read, or -ve on error
167 int rtc_read8(struct udevice *dev, unsigned int reg);
170 * rtc_write8() - Write an 8-bit register
172 * @dev: Device to write to
173 * @reg: Register to write
174 * @value: Value to write
175 * Return: 0 if OK, -ve on error
177 int rtc_write8(struct udevice *dev, unsigned int reg, int val);
180 * rtc_read16() - Read a 16-bit value from the RTC
182 * @dev: Device to read from
183 * @reg: Offset to start reading from
184 * @valuep: Place to put the value that is read
185 * Return: 0 if OK, -ve on error
187 int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep);
190 * rtc_write16() - Write a 16-bit value to the RTC
192 * @dev: Device to write to
193 * @reg: Register to start writing to
194 * @value: Value to write
195 * Return: 0 if OK, -ve on error
197 int rtc_write16(struct udevice *dev, unsigned int reg, u16 value);
200 * rtc_read32() - Read a 32-bit value from the RTC
202 * @dev: Device to read from
203 * @reg: Offset to start reading from
204 * @valuep: Place to put the value that is read
205 * Return: 0 if OK, -ve on error
207 int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep);
210 * rtc_write32() - Write a 32-bit value to the RTC
212 * @dev: Device to write to
213 * @reg: Register to start writing to
214 * @value: Value to write
215 * Return: 0 if OK, -ve on error
217 int rtc_write32(struct udevice *dev, unsigned int reg, u32 value);
219 #ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
220 int rtc_enable_32khz_output(int busnum, int chip_addr);
224 static inline int dm_rtc_get(struct udevice *dev, struct rtc_time *time)
229 static inline int dm_rtc_set(struct udevice *dev, struct rtc_time *time)
234 static inline int dm_rtc_reset(struct udevice *dev)
239 static inline int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf,
245 static inline int dm_rtc_write(struct udevice *dev, unsigned int reg,
246 const u8 *buf, unsigned int len)
251 int rtc_get (struct rtc_time *);
252 int rtc_set (struct rtc_time *);
253 void rtc_reset (void);
254 #ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
255 void rtc_enable_32khz_output(void);
259 * rtc_read8() - Read an 8-bit register
261 * @reg: Register to read
264 int rtc_read8(int reg);
267 * rtc_write8() - Write an 8-bit register
269 * @reg: Register to write
270 * @value: Value to write
272 void rtc_write8(int reg, uchar val);
275 * rtc_read32() - Read a 32-bit value from the RTC
277 * @reg: Offset to start reading from
280 u32 rtc_read32(int reg);
283 * rtc_write32() - Write a 32-bit value to the RTC
285 * @reg: Register to start writing to
286 * @value: Value to write
288 void rtc_write32(int reg, u32 value);
291 * rtc_init() - Set up the real time clock ready for use
294 #endif /* CONFIG_DM_RTC */
297 * is_leap_year - Check if year is a leap year
300 * Return: 1 if leap year
302 static inline bool is_leap_year(unsigned int year)
304 return (!(year % 4) && (year % 100)) || !(year % 400);
308 * rtc_calc_weekday() - Work out the weekday from a time
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.
313 * @time: Time to inspect. tm_wday is updated
314 * Return: 0 if OK, -EINVAL if the weekday could not be determined
316 int rtc_calc_weekday(struct rtc_time *time);
319 * rtc_to_tm() - Convert a time_t value into a broken-out time
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
324 * Note that tm_yday and tm_isdst are set to 0.
326 * @time_t: Number of seconds since 1970-01-01 00:00:00
327 * @time: Place to put the broken-out time
329 void rtc_to_tm(u64 time_t, struct rtc_time *time);
332 * rtc_mktime() - Convert a broken-out time into a time64_t value
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
337 * Note that tm_wday and tm_yday are ignored.
339 * @time: Broken-out time to convert
340 * Return: corresponding time64_t value, seconds since 1970-01-01 00:00:00
342 time64_t rtc_mktime(const struct rtc_time *time);
345 * rtc_month_days() - The number of days in the month
347 * @month: month (January = 0)
348 * @year: year (4 digits)
350 int rtc_month_days(unsigned int month, unsigned int year);