183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
2a6840a6eSwdenk /*
3a6840a6eSwdenk * (C) Copyright 2001
4a6840a6eSwdenk * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5a6840a6eSwdenk */
6a6840a6eSwdenk
7a6840a6eSwdenk /*
8a6840a6eSwdenk * Generic RTC interface.
9a6840a6eSwdenk */
10a6840a6eSwdenk #ifndef _RTC_H_
11a6840a6eSwdenk #define _RTC_H_
12a6840a6eSwdenk
13885fc78cSAlbin Tonnerre /* bcd<->bin functions are needed by almost all the RTC drivers, let's include
14885fc78cSAlbin Tonnerre * it there instead of in evey single driver */
15885fc78cSAlbin Tonnerre
16885fc78cSAlbin Tonnerre #include <bcd.h>
17aac51198SSimon Glass #include <rtc_def.h>
18a6840a6eSwdenk
19dbeda5b2SSimon Glass #ifdef CONFIG_DM_RTC
20dbeda5b2SSimon Glass
21dbeda5b2SSimon Glass struct rtc_ops {
22dbeda5b2SSimon Glass /**
23dbeda5b2SSimon Glass * get() - get the current time
24dbeda5b2SSimon Glass *
25dbeda5b2SSimon Glass * Returns the current time read from the RTC device. The driver
26dbeda5b2SSimon Glass * is responsible for setting up every field in the structure.
27dbeda5b2SSimon Glass *
28dbeda5b2SSimon Glass * @dev: Device to read from
29dbeda5b2SSimon Glass * @time: Place to put the time that is read
30dbeda5b2SSimon Glass */
31dbeda5b2SSimon Glass int (*get)(struct udevice *dev, struct rtc_time *time);
32dbeda5b2SSimon Glass
33dbeda5b2SSimon Glass /**
34dbeda5b2SSimon Glass * set() - set the current time
35dbeda5b2SSimon Glass *
36dbeda5b2SSimon Glass * Sets the time in the RTC device. The driver can expect every
37dbeda5b2SSimon Glass * field to be set correctly.
38dbeda5b2SSimon Glass *
39dbeda5b2SSimon Glass * @dev: Device to read from
40dbeda5b2SSimon Glass * @time: Time to write
41dbeda5b2SSimon Glass */
42dbeda5b2SSimon Glass int (*set)(struct udevice *dev, const struct rtc_time *time);
43dbeda5b2SSimon Glass
44dbeda5b2SSimon Glass /**
45dbeda5b2SSimon Glass * reset() - reset the RTC to a known-good state
46dbeda5b2SSimon Glass *
47dbeda5b2SSimon Glass * This function resets the RTC to a known-good state. The time may
48dbeda5b2SSimon Glass * be unset by this method, so should be set after this method is
49dbeda5b2SSimon Glass * called.
50dbeda5b2SSimon Glass *
51dbeda5b2SSimon Glass * @dev: Device to read from
52dbeda5b2SSimon Glass * @return 0 if OK, -ve on error
53dbeda5b2SSimon Glass */
54dbeda5b2SSimon Glass int (*reset)(struct udevice *dev);
55dbeda5b2SSimon Glass
56dbeda5b2SSimon Glass /**
57dbeda5b2SSimon Glass * read8() - Read an 8-bit register
58dbeda5b2SSimon Glass *
59dbeda5b2SSimon Glass * @dev: Device to read from
60dbeda5b2SSimon Glass * @reg: Register to read
61dbeda5b2SSimon Glass * @return value read, or -ve on error
62dbeda5b2SSimon Glass */
63dbeda5b2SSimon Glass int (*read8)(struct udevice *dev, unsigned int reg);
64dbeda5b2SSimon Glass
65dbeda5b2SSimon Glass /**
66dbeda5b2SSimon Glass * write8() - Write an 8-bit register
67dbeda5b2SSimon Glass *
68dbeda5b2SSimon Glass * @dev: Device to write to
69dbeda5b2SSimon Glass * @reg: Register to write
70dbeda5b2SSimon Glass * @value: Value to write
71dbeda5b2SSimon Glass * @return 0 if OK, -ve on error
72dbeda5b2SSimon Glass */
73dbeda5b2SSimon Glass int (*write8)(struct udevice *dev, unsigned int reg, int val);
74dbeda5b2SSimon Glass };
75dbeda5b2SSimon Glass
76dbeda5b2SSimon Glass /* Access the operations for an RTC device */
77dbeda5b2SSimon Glass #define rtc_get_ops(dev) ((struct rtc_ops *)(dev)->driver->ops)
78dbeda5b2SSimon Glass
79dbeda5b2SSimon Glass /**
80dbeda5b2SSimon Glass * dm_rtc_get() - Read the time from an RTC
81dbeda5b2SSimon Glass *
82dbeda5b2SSimon Glass * @dev: Device to read from
83dbeda5b2SSimon Glass * @time: Place to put the current time
84dbeda5b2SSimon Glass * @return 0 if OK, -ve on error
85dbeda5b2SSimon Glass */
86dbeda5b2SSimon Glass int dm_rtc_get(struct udevice *dev, struct rtc_time *time);
87dbeda5b2SSimon Glass
88dbeda5b2SSimon Glass /**
89*a4b33c5aSPhilipp Tomsich * dm_rtc_set() - Write a time to an RTC
90dbeda5b2SSimon Glass *
91dbeda5b2SSimon Glass * @dev: Device to read from
92dbeda5b2SSimon Glass * @time: Time to write into the RTC
93dbeda5b2SSimon Glass * @return 0 if OK, -ve on error
94dbeda5b2SSimon Glass */
95dbeda5b2SSimon Glass int dm_rtc_set(struct udevice *dev, struct rtc_time *time);
96dbeda5b2SSimon Glass
97dbeda5b2SSimon Glass /**
98dbeda5b2SSimon Glass * dm_rtc_reset() - reset the RTC to a known-good state
99dbeda5b2SSimon Glass *
100dbeda5b2SSimon Glass * If the RTC appears to be broken (e.g. it is not counting up in seconds)
101dbeda5b2SSimon Glass * it may need to be reset to a known good state. This function achieves this.
102dbeda5b2SSimon Glass * After resetting the RTC the time should then be set to a known value by
103dbeda5b2SSimon Glass * the caller.
104dbeda5b2SSimon Glass *
105dbeda5b2SSimon Glass * @dev: Device to read from
106dbeda5b2SSimon Glass * @return 0 if OK, -ve on error
107dbeda5b2SSimon Glass */
108dbeda5b2SSimon Glass int dm_rtc_reset(struct udevice *dev);
109dbeda5b2SSimon Glass
110dbeda5b2SSimon Glass /**
111dbeda5b2SSimon Glass * rtc_read8() - Read an 8-bit register
112dbeda5b2SSimon Glass *
113dbeda5b2SSimon Glass * @dev: Device to read from
114dbeda5b2SSimon Glass * @reg: Register to read
115dbeda5b2SSimon Glass * @return value read, or -ve on error
116dbeda5b2SSimon Glass */
117dbeda5b2SSimon Glass int rtc_read8(struct udevice *dev, unsigned int reg);
118dbeda5b2SSimon Glass
119dbeda5b2SSimon Glass /**
120dbeda5b2SSimon Glass * rtc_write8() - Write an 8-bit register
121dbeda5b2SSimon Glass *
122dbeda5b2SSimon Glass * @dev: Device to write to
123dbeda5b2SSimon Glass * @reg: Register to write
124dbeda5b2SSimon Glass * @value: Value to write
125dbeda5b2SSimon Glass * @return 0 if OK, -ve on error
126dbeda5b2SSimon Glass */
127dbeda5b2SSimon Glass int rtc_write8(struct udevice *dev, unsigned int reg, int val);
128dbeda5b2SSimon Glass
129dbeda5b2SSimon Glass /**
130d24c7fbcSBin Meng * rtc_read16() - Read a 16-bit value from the RTC
131d24c7fbcSBin Meng *
132d24c7fbcSBin Meng * @dev: Device to read from
133d24c7fbcSBin Meng * @reg: Offset to start reading from
134d24c7fbcSBin Meng * @valuep: Place to put the value that is read
135d24c7fbcSBin Meng * @return 0 if OK, -ve on error
136d24c7fbcSBin Meng */
137d24c7fbcSBin Meng int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep);
138d24c7fbcSBin Meng
139d24c7fbcSBin Meng /**
140d24c7fbcSBin Meng * rtc_write16() - Write a 16-bit value to the RTC
141d24c7fbcSBin Meng *
142d24c7fbcSBin Meng * @dev: Device to write to
143d24c7fbcSBin Meng * @reg: Register to start writing to
144d24c7fbcSBin Meng * @value: Value to write
145d24c7fbcSBin Meng * @return 0 if OK, -ve on error
146d24c7fbcSBin Meng */
147d24c7fbcSBin Meng int rtc_write16(struct udevice *dev, unsigned int reg, u16 value);
148d24c7fbcSBin Meng
149d24c7fbcSBin Meng /**
150dbeda5b2SSimon Glass * rtc_read32() - Read a 32-bit value from the RTC
151dbeda5b2SSimon Glass *
152dbeda5b2SSimon Glass * @dev: Device to read from
153dbeda5b2SSimon Glass * @reg: Offset to start reading from
154dbeda5b2SSimon Glass * @valuep: Place to put the value that is read
155dbeda5b2SSimon Glass * @return 0 if OK, -ve on error
156dbeda5b2SSimon Glass */
157dbeda5b2SSimon Glass int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep);
158dbeda5b2SSimon Glass
159dbeda5b2SSimon Glass /**
160dbeda5b2SSimon Glass * rtc_write32() - Write a 32-bit value to the RTC
161dbeda5b2SSimon Glass *
162dbeda5b2SSimon Glass * @dev: Device to write to
163dbeda5b2SSimon Glass * @reg: Register to start writing to
164dbeda5b2SSimon Glass * @value: Value to write
165dbeda5b2SSimon Glass * @return 0 if OK, -ve on error
166dbeda5b2SSimon Glass */
167dbeda5b2SSimon Glass int rtc_write32(struct udevice *dev, unsigned int reg, u32 value);
168dbeda5b2SSimon Glass
169dbeda5b2SSimon Glass #else
170b73a19e1SYuri Tikhonov int rtc_get (struct rtc_time *);
171d1e23194SJean-Christophe PLAGNIOL-VILLARD int rtc_set (struct rtc_time *);
172a6840a6eSwdenk void rtc_reset (void);
173c340941eSPriyanka Jain void rtc_enable_32khz_output(void);
174a6840a6eSwdenk
175c6577f72SSimon Glass /**
176fc4860c0SSimon Glass * rtc_read8() - Read an 8-bit register
177fc4860c0SSimon Glass *
178fc4860c0SSimon Glass * @reg: Register to read
179fc4860c0SSimon Glass * @return value read
180fc4860c0SSimon Glass */
181fc4860c0SSimon Glass int rtc_read8(int reg);
182fc4860c0SSimon Glass
183fc4860c0SSimon Glass /**
184fc4860c0SSimon Glass * rtc_write8() - Write an 8-bit register
185fc4860c0SSimon Glass *
186fc4860c0SSimon Glass * @reg: Register to write
187fc4860c0SSimon Glass * @value: Value to write
188fc4860c0SSimon Glass */
189fc4860c0SSimon Glass void rtc_write8(int reg, uchar val);
190fc4860c0SSimon Glass
191fc4860c0SSimon Glass /**
192fc4860c0SSimon Glass * rtc_read32() - Read a 32-bit value from the RTC
193fc4860c0SSimon Glass *
194fc4860c0SSimon Glass * @reg: Offset to start reading from
195fc4860c0SSimon Glass * @return value read
196fc4860c0SSimon Glass */
197fc4860c0SSimon Glass u32 rtc_read32(int reg);
198fc4860c0SSimon Glass
199fc4860c0SSimon Glass /**
200fc4860c0SSimon Glass * rtc_write32() - Write a 32-bit value to the RTC
201fc4860c0SSimon Glass *
202fc4860c0SSimon Glass * @reg: Register to start writing to
203fc4860c0SSimon Glass * @value: Value to write
204fc4860c0SSimon Glass */
205fc4860c0SSimon Glass void rtc_write32(int reg, u32 value);
206fc4860c0SSimon Glass
207fc4860c0SSimon Glass /**
208c6577f72SSimon Glass * rtc_init() - Set up the real time clock ready for use
209c6577f72SSimon Glass */
210c6577f72SSimon Glass void rtc_init(void);
211992c1db4SHeinrich Schuchardt #endif /* CONFIG_DM_RTC */
212992c1db4SHeinrich Schuchardt
213992c1db4SHeinrich Schuchardt /**
214992c1db4SHeinrich Schuchardt * is_leap_year - Check if year is a leap year
215992c1db4SHeinrich Schuchardt *
216992c1db4SHeinrich Schuchardt * @year Year
217992c1db4SHeinrich Schuchardt * @return 1 if leap year
218992c1db4SHeinrich Schuchardt */
is_leap_year(unsigned int year)219992c1db4SHeinrich Schuchardt static inline bool is_leap_year(unsigned int year)
220992c1db4SHeinrich Schuchardt {
221992c1db4SHeinrich Schuchardt return (!(year % 4) && (year % 100)) || !(year % 400);
222992c1db4SHeinrich Schuchardt }
223c6577f72SSimon Glass
224199e87c3SSimon Glass /**
225199e87c3SSimon Glass * rtc_calc_weekday() - Work out the weekday from a time
226199e87c3SSimon Glass *
227199e87c3SSimon Glass * This only works for the Gregorian calendar - i.e. after 1752 (in the UK).
228199e87c3SSimon Glass * It sets time->tm_wdaay to the correct day of the week.
229199e87c3SSimon Glass *
230199e87c3SSimon Glass * @time: Time to inspect. tm_wday is updated
231199e87c3SSimon Glass * @return 0 if OK, -EINVAL if the weekday could not be determined
232199e87c3SSimon Glass */
233199e87c3SSimon Glass int rtc_calc_weekday(struct rtc_time *time);
234199e87c3SSimon Glass
2359f9276c3SSimon Glass /**
2369f9276c3SSimon Glass * rtc_to_tm() - Convert a time_t value into a broken-out time
2379f9276c3SSimon Glass *
2389f9276c3SSimon Glass * The following fields are set up by this function:
2399f9276c3SSimon Glass * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday
2409f9276c3SSimon Glass *
2419f9276c3SSimon Glass * Note that tm_yday and tm_isdst are set to 0.
2429f9276c3SSimon Glass *
2439f9276c3SSimon Glass * @time_t: Number of seconds since 1970-01-01 00:00:00
2449f9276c3SSimon Glass * @time: Place to put the broken-out time
2459f9276c3SSimon Glass */
246992c1db4SHeinrich Schuchardt void rtc_to_tm(u64 time_t, struct rtc_time *time);
2479f9276c3SSimon Glass
24871420983SSimon Glass /**
24971420983SSimon Glass * rtc_mktime() - Convert a broken-out time into a time_t value
25071420983SSimon Glass *
25171420983SSimon Glass * The following fields need to be valid for this function to work:
25271420983SSimon Glass * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year
25371420983SSimon Glass *
25471420983SSimon Glass * Note that tm_wday and tm_yday are ignored.
25571420983SSimon Glass *
25671420983SSimon Glass * @time: Broken-out time to convert
25771420983SSimon Glass * @return corresponding time_t value, seconds since 1970-01-01 00:00:00
25871420983SSimon Glass */
25971420983SSimon Glass unsigned long rtc_mktime(const struct rtc_time *time);
26071420983SSimon Glass
261a6840a6eSwdenk #endif /* _RTC_H_ */
262