blob: b6fdbb60dc2b5a527c1a51c28b53ccc99da2bdcd [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenka6840a62001-04-09 21:43:07 +00002/*
3 * (C) Copyright 2001
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenka6840a62001-04-09 21:43:07 +00005 */
6
7/*
8 * Generic RTC interface.
9 */
10#ifndef _RTC_H_
11#define _RTC_H_
12
Albin Tonnerre885fc782009-08-13 15:31:12 +020013/* 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 Glassaac51192015-04-20 12:37:21 -060017#include <rtc_def.h>
Sean Anderson640aecb2022-11-22 12:54:52 -050018#include <linux/errno.h>
wdenka6840a62001-04-09 21:43:07 +000019
Jan Kiszka90c52422022-04-24 11:34:56 +020020typedef int64_t time64_t;
AKASHI Takahiro09030e02019-11-13 09:44:48 +090021struct udevice;
22
Sean Anderson640aecb2022-11-22 12:54:52 -050023#if CONFIG_IS_ENABLED(DM_RTC)
Simon Glassdbeda5b2015-04-20 12:37:23 -060024struct 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 Villemoesd8be0882020-07-06 22:01:10 +020060 * 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 Villemoes09381822020-07-06 22:01:11 +020072 * 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 Glassdbeda5b2015-04-20 12:37:23 -060084 * 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 Schuchardt185f8122022-01-19 18:05:50 +010098 * Return: 0 if OK, -ve on error
Simon Glassdbeda5b2015-04-20 12:37:23 -060099 */
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 Schuchardt185f8122022-01-19 18:05:50 +0100111 * Return: 0 if OK, -ve on error
Simon Glassdbeda5b2015-04-20 12:37:23 -0600112 */
113int dm_rtc_get(struct udevice *dev, struct rtc_time *time);
114
115/**
Philipp Tomsicha4b33c52018-11-25 19:32:54 +0100116 * dm_rtc_set() - Write a time to an RTC
Simon Glassdbeda5b2015-04-20 12:37:23 -0600117 *
118 * @dev: Device to read from
119 * @time: Time to write into the RTC
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100120 * Return: 0 if OK, -ve on error
Simon Glassdbeda5b2015-04-20 12:37:23 -0600121 */
122int 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 Schuchardt185f8122022-01-19 18:05:50 +0100133 * Return: 0 if OK, -ve on error
Simon Glassdbeda5b2015-04-20 12:37:23 -0600134 */
135int dm_rtc_reset(struct udevice *dev);
136
137/**
Rasmus Villemoesd8be0882020-07-06 22:01:10 +0200138 * 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 Schuchardt185f8122022-01-19 18:05:50 +0100144 * Return: 0 if OK, -ve on error
Rasmus Villemoesd8be0882020-07-06 22:01:10 +0200145 */
146int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf, unsigned int len);
147
148/**
Rasmus Villemoes09381822020-07-06 22:01:11 +0200149 * 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 Schuchardt185f8122022-01-19 18:05:50 +0100155 * Return: 0 if OK, -ve on error
Rasmus Villemoes09381822020-07-06 22:01:11 +0200156 */
157int dm_rtc_write(struct udevice *dev, unsigned int reg,
158 const u8 *buf, unsigned int len);
159
160/**
Simon Glassdbeda5b2015-04-20 12:37:23 -0600161 * rtc_read8() - Read an 8-bit register
162 *
163 * @dev: Device to read from
164 * @reg: Register to read
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100165 * Return: value read, or -ve on error
Simon Glassdbeda5b2015-04-20 12:37:23 -0600166 */
167int 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 Schuchardt185f8122022-01-19 18:05:50 +0100175 * Return: 0 if OK, -ve on error
Simon Glassdbeda5b2015-04-20 12:37:23 -0600176 */
177int rtc_write8(struct udevice *dev, unsigned int reg, int val);
178
179/**
Bin Mengd24c7fb2017-03-16 07:26:27 -0700180 * 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 Schuchardt185f8122022-01-19 18:05:50 +0100185 * Return: 0 if OK, -ve on error
Bin Mengd24c7fb2017-03-16 07:26:27 -0700186 */
187int 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 Schuchardt185f8122022-01-19 18:05:50 +0100195 * Return: 0 if OK, -ve on error
Bin Mengd24c7fb2017-03-16 07:26:27 -0700196 */
197int rtc_write16(struct udevice *dev, unsigned int reg, u16 value);
198
199/**
Simon Glassdbeda5b2015-04-20 12:37:23 -0600200 * 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 Schuchardt185f8122022-01-19 18:05:50 +0100205 * Return: 0 if OK, -ve on error
Simon Glassdbeda5b2015-04-20 12:37:23 -0600206 */
207int 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 Schuchardt185f8122022-01-19 18:05:50 +0100215 * Return: 0 if OK, -ve on error
Simon Glassdbeda5b2015-04-20 12:37:23 -0600216 */
217int rtc_write32(struct udevice *dev, unsigned int reg, u32 value);
218
Chuanhua Handb07c442019-07-26 19:24:00 +0800219#ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
220int rtc_enable_32khz_output(int busnum, int chip_addr);
221#endif
222
Simon Glassdbeda5b2015-04-20 12:37:23 -0600223#else
Sean Anderson640aecb2022-11-22 12:54:52 -0500224static inline int dm_rtc_get(struct udevice *dev, struct rtc_time *time)
225{
226 return -ENOSYS;
227}
228
229static inline int dm_rtc_set(struct udevice *dev, struct rtc_time *time)
230{
231 return -ENOSYS;
232}
233
234static inline int dm_rtc_reset(struct udevice *dev)
235{
236 return -ENOSYS;
237}
238
239static inline int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf,
240 unsigned int len)
241{
242 return -ENOSYS;
243}
244
245static 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 Tikhonovb73a19e2008-03-20 17:56:04 +0300251int rtc_get (struct rtc_time *);
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200252int rtc_set (struct rtc_time *);
wdenka6840a62001-04-09 21:43:07 +0000253void rtc_reset (void);
Chuanhua Handb07c442019-07-26 19:24:00 +0800254#ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
Priyanka Jainc3409412015-06-29 15:39:23 +0530255void rtc_enable_32khz_output(void);
Chuanhua Handb07c442019-07-26 19:24:00 +0800256#endif
wdenka6840a62001-04-09 21:43:07 +0000257
Simon Glassc6577f72014-11-14 18:18:26 -0700258/**
Simon Glassfc4860c2015-01-19 22:16:10 -0700259 * rtc_read8() - Read an 8-bit register
260 *
261 * @reg: Register to read
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100262 * Return: value read
Simon Glassfc4860c2015-01-19 22:16:10 -0700263 */
264int 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 */
272void 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 Schuchardt185f8122022-01-19 18:05:50 +0100278 * Return: value read
Simon Glassfc4860c2015-01-19 22:16:10 -0700279 */
280u32 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 */
288void rtc_write32(int reg, u32 value);
289
290/**
Simon Glassc6577f72014-11-14 18:18:26 -0700291 * rtc_init() - Set up the real time clock ready for use
292 */
293void rtc_init(void);
Heinrich Schuchardt992c1db2018-07-07 23:39:11 +0200294#endif /* CONFIG_DM_RTC */
295
296/**
297 * is_leap_year - Check if year is a leap year
298 *
299 * @year Year
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100300 * Return: 1 if leap year
Heinrich Schuchardt992c1db2018-07-07 23:39:11 +0200301 */
302static inline bool is_leap_year(unsigned int year)
303{
304 return (!(year % 4) && (year % 100)) || !(year % 400);
305}
Simon Glassc6577f72014-11-14 18:18:26 -0700306
Simon Glass199e87c2015-04-20 12:37:17 -0600307/**
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 Schuchardt185f8122022-01-19 18:05:50 +0100314 * Return: 0 if OK, -EINVAL if the weekday could not be determined
Simon Glass199e87c2015-04-20 12:37:17 -0600315 */
316int rtc_calc_weekday(struct rtc_time *time);
317
Simon Glass9f9276c2015-04-20 12:37:18 -0600318/**
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 Glass9f9276c2015-04-20 12:37:18 -0600328 */
Heinrich Schuchardt992c1db2018-07-07 23:39:11 +0200329void rtc_to_tm(u64 time_t, struct rtc_time *time);
Simon Glass9f9276c2015-04-20 12:37:18 -0600330
Simon Glass71420982015-04-20 12:37:19 -0600331/**
Jan Kiszka90c52422022-04-24 11:34:56 +0200332 * rtc_mktime() - Convert a broken-out time into a time64_t value
Simon Glass71420982015-04-20 12:37:19 -0600333 *
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 Kiszka90c52422022-04-24 11:34:56 +0200340 * Return: corresponding time64_t value, seconds since 1970-01-01 00:00:00
Simon Glass71420982015-04-20 12:37:19 -0600341 */
Jan Kiszka90c52422022-04-24 11:34:56 +0200342time64_t rtc_mktime(const struct rtc_time *time);
Simon Glass71420982015-04-20 12:37:19 -0600343
Heinrich Schuchardt3c1889e2019-05-31 07:21:03 +0200344/**
345 * rtc_month_days() - The number of days in the month
346 *
347 * @month: month (January = 0)
348 * @year: year (4 digits)
349 */
350int rtc_month_days(unsigned int month, unsigned int year);
351
wdenka6840a62001-04-09 21:43:07 +0000352#endif /* _RTC_H_ */