blob: f30e908221a280ed163a585cd0fa64a299d8eba6 [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>
wdenka6840a62001-04-09 21:43:07 +000018
Simon Glassdbeda5b2015-04-20 12:37:23 -060019#ifdef CONFIG_DM_RTC
20
AKASHI Takahiro09030e02019-11-13 09:44:48 +090021struct udevice;
22
Simon Glassdbeda5b2015-04-20 12:37:23 -060023struct rtc_ops {
24 /**
25 * get() - get the current time
26 *
27 * Returns the current time read from the RTC device. The driver
28 * is responsible for setting up every field in the structure.
29 *
30 * @dev: Device to read from
31 * @time: Place to put the time that is read
32 */
33 int (*get)(struct udevice *dev, struct rtc_time *time);
34
35 /**
36 * set() - set the current time
37 *
38 * Sets the time in the RTC device. The driver can expect every
39 * field to be set correctly.
40 *
41 * @dev: Device to read from
42 * @time: Time to write
43 */
44 int (*set)(struct udevice *dev, const struct rtc_time *time);
45
46 /**
47 * reset() - reset the RTC to a known-good state
48 *
49 * This function resets the RTC to a known-good state. The time may
50 * be unset by this method, so should be set after this method is
51 * called.
52 *
53 * @dev: Device to read from
54 * @return 0 if OK, -ve on error
55 */
56 int (*reset)(struct udevice *dev);
57
58 /**
Rasmus Villemoesd8be0882020-07-06 22:01:10 +020059 * read() - Read multiple 8-bit registers
60 *
61 * @dev: Device to read from
62 * @reg: First register to read
63 * @buf: Output buffer
64 * @len: Number of registers to read
65 * @return 0 if OK, -ve on error
66 */
67 int (*read)(struct udevice *dev, unsigned int reg,
68 u8 *buf, unsigned int len);
69
70 /**
Simon Glassdbeda5b2015-04-20 12:37:23 -060071 * read8() - Read an 8-bit register
72 *
73 * @dev: Device to read from
74 * @reg: Register to read
75 * @return value read, or -ve on error
76 */
77 int (*read8)(struct udevice *dev, unsigned int reg);
78
79 /**
80 * write8() - Write an 8-bit register
81 *
82 * @dev: Device to write to
83 * @reg: Register to write
84 * @value: Value to write
85 * @return 0 if OK, -ve on error
86 */
87 int (*write8)(struct udevice *dev, unsigned int reg, int val);
88};
89
90/* Access the operations for an RTC device */
91#define rtc_get_ops(dev) ((struct rtc_ops *)(dev)->driver->ops)
92
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 */
100int dm_rtc_get(struct udevice *dev, struct rtc_time *time);
101
102/**
Philipp Tomsicha4b33c52018-11-25 19:32:54 +0100103 * dm_rtc_set() - Write a time to an RTC
Simon Glassdbeda5b2015-04-20 12:37:23 -0600104 *
105 * @dev: Device to read from
106 * @time: Time to write into the RTC
107 * @return 0 if OK, -ve on error
108 */
109int dm_rtc_set(struct udevice *dev, struct rtc_time *time);
110
111/**
112 * dm_rtc_reset() - reset the RTC to a known-good state
113 *
114 * If the RTC appears to be broken (e.g. it is not counting up in seconds)
115 * it may need to be reset to a known good state. This function achieves this.
116 * After resetting the RTC the time should then be set to a known value by
117 * the caller.
118 *
119 * @dev: Device to read from
120 * @return 0 if OK, -ve on error
121 */
122int dm_rtc_reset(struct udevice *dev);
123
124/**
Rasmus Villemoesd8be0882020-07-06 22:01:10 +0200125 * dm_rtc_read() - Read multiple 8-bit registers
126 *
127 * @dev: Device to read from
128 * @reg: First register to read
129 * @buf: Output buffer
130 * @len: Number of registers to read
131 * @return 0 if OK, -ve on error
132 */
133int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf, unsigned int len);
134
135/**
Simon Glassdbeda5b2015-04-20 12:37:23 -0600136 * rtc_read8() - Read an 8-bit register
137 *
138 * @dev: Device to read from
139 * @reg: Register to read
140 * @return value read, or -ve on error
141 */
142int rtc_read8(struct udevice *dev, unsigned int reg);
143
144/**
145 * rtc_write8() - Write an 8-bit register
146 *
147 * @dev: Device to write to
148 * @reg: Register to write
149 * @value: Value to write
150 * @return 0 if OK, -ve on error
151 */
152int rtc_write8(struct udevice *dev, unsigned int reg, int val);
153
154/**
Bin Mengd24c7fb2017-03-16 07:26:27 -0700155 * rtc_read16() - Read a 16-bit value from the RTC
156 *
157 * @dev: Device to read from
158 * @reg: Offset to start reading from
159 * @valuep: Place to put the value that is read
160 * @return 0 if OK, -ve on error
161 */
162int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep);
163
164/**
165 * rtc_write16() - Write a 16-bit value to the RTC
166 *
167 * @dev: Device to write to
168 * @reg: Register to start writing to
169 * @value: Value to write
170 * @return 0 if OK, -ve on error
171 */
172int rtc_write16(struct udevice *dev, unsigned int reg, u16 value);
173
174/**
Simon Glassdbeda5b2015-04-20 12:37:23 -0600175 * rtc_read32() - Read a 32-bit value from the RTC
176 *
177 * @dev: Device to read from
178 * @reg: Offset to start reading from
179 * @valuep: Place to put the value that is read
180 * @return 0 if OK, -ve on error
181 */
182int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep);
183
184/**
185 * rtc_write32() - Write a 32-bit value to the RTC
186 *
187 * @dev: Device to write to
188 * @reg: Register to start writing to
189 * @value: Value to write
190 * @return 0 if OK, -ve on error
191 */
192int rtc_write32(struct udevice *dev, unsigned int reg, u32 value);
193
Chuanhua Handb07c442019-07-26 19:24:00 +0800194#ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
195int rtc_enable_32khz_output(int busnum, int chip_addr);
196#endif
197
Simon Glassdbeda5b2015-04-20 12:37:23 -0600198#else
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300199int rtc_get (struct rtc_time *);
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200200int rtc_set (struct rtc_time *);
wdenka6840a62001-04-09 21:43:07 +0000201void rtc_reset (void);
Chuanhua Handb07c442019-07-26 19:24:00 +0800202#ifdef CONFIG_RTC_ENABLE_32KHZ_OUTPUT
Priyanka Jainc3409412015-06-29 15:39:23 +0530203void rtc_enable_32khz_output(void);
Chuanhua Handb07c442019-07-26 19:24:00 +0800204#endif
wdenka6840a62001-04-09 21:43:07 +0000205
Simon Glassc6577f72014-11-14 18:18:26 -0700206/**
Simon Glassfc4860c2015-01-19 22:16:10 -0700207 * rtc_read8() - Read an 8-bit register
208 *
209 * @reg: Register to read
210 * @return value read
211 */
212int rtc_read8(int reg);
213
214/**
215 * rtc_write8() - Write an 8-bit register
216 *
217 * @reg: Register to write
218 * @value: Value to write
219 */
220void rtc_write8(int reg, uchar val);
221
222/**
223 * rtc_read32() - Read a 32-bit value from the RTC
224 *
225 * @reg: Offset to start reading from
226 * @return value read
227 */
228u32 rtc_read32(int reg);
229
230/**
231 * rtc_write32() - Write a 32-bit value to the RTC
232 *
233 * @reg: Register to start writing to
234 * @value: Value to write
235 */
236void rtc_write32(int reg, u32 value);
237
238/**
Simon Glassc6577f72014-11-14 18:18:26 -0700239 * rtc_init() - Set up the real time clock ready for use
240 */
241void rtc_init(void);
Heinrich Schuchardt992c1db2018-07-07 23:39:11 +0200242#endif /* CONFIG_DM_RTC */
243
244/**
245 * is_leap_year - Check if year is a leap year
246 *
247 * @year Year
248 * @return 1 if leap year
249 */
250static inline bool is_leap_year(unsigned int year)
251{
252 return (!(year % 4) && (year % 100)) || !(year % 400);
253}
Simon Glassc6577f72014-11-14 18:18:26 -0700254
Simon Glass199e87c2015-04-20 12:37:17 -0600255/**
256 * rtc_calc_weekday() - Work out the weekday from a time
257 *
258 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK).
259 * It sets time->tm_wdaay to the correct day of the week.
260 *
261 * @time: Time to inspect. tm_wday is updated
262 * @return 0 if OK, -EINVAL if the weekday could not be determined
263 */
264int rtc_calc_weekday(struct rtc_time *time);
265
Simon Glass9f9276c2015-04-20 12:37:18 -0600266/**
267 * rtc_to_tm() - Convert a time_t value into a broken-out time
268 *
269 * The following fields are set up by this function:
270 * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday
271 *
272 * Note that tm_yday and tm_isdst are set to 0.
273 *
274 * @time_t: Number of seconds since 1970-01-01 00:00:00
275 * @time: Place to put the broken-out time
Simon Glass9f9276c2015-04-20 12:37:18 -0600276 */
Heinrich Schuchardt992c1db2018-07-07 23:39:11 +0200277void rtc_to_tm(u64 time_t, struct rtc_time *time);
Simon Glass9f9276c2015-04-20 12:37:18 -0600278
Simon Glass71420982015-04-20 12:37:19 -0600279/**
280 * rtc_mktime() - Convert a broken-out time into a time_t value
281 *
282 * The following fields need to be valid for this function to work:
283 * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year
284 *
285 * Note that tm_wday and tm_yday are ignored.
286 *
287 * @time: Broken-out time to convert
288 * @return corresponding time_t value, seconds since 1970-01-01 00:00:00
289 */
290unsigned long rtc_mktime(const struct rtc_time *time);
291
Heinrich Schuchardt3c1889e2019-05-31 07:21:03 +0200292/**
293 * rtc_month_days() - The number of days in the month
294 *
295 * @month: month (January = 0)
296 * @year: year (4 digits)
297 */
298int rtc_month_days(unsigned int month, unsigned int year);
299
wdenka6840a62001-04-09 21:43:07 +0000300#endif /* _RTC_H_ */