blob: bd8621d60b93d4491d33ee477ccd04b7438c65a7 [file] [log] [blame]
wdenka6840a62001-04-09 21:43:07 +00001/*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenka6840a62001-04-09 21:43:07 +00006 */
7
8/*
9 * Generic RTC interface.
10 */
11#ifndef _RTC_H_
12#define _RTC_H_
13
Albin Tonnerredfdea732009-08-13 15:31:12 +020014/* bcd<->bin functions are needed by almost all the RTC drivers, let's include
15 * it there instead of in evey single driver */
16
17#include <bcd.h>
Simon Glass573e3f72015-04-20 12:37:21 -060018#include <rtc_def.h>
wdenka6840a62001-04-09 21:43:07 +000019
Simon Glassd9050b52015-04-20 12:37:23 -060020#ifdef CONFIG_DM_RTC
21
22struct rtc_ops {
23 /**
24 * get() - get the current time
25 *
26 * Returns the current time read from the RTC device. The driver
27 * is responsible for setting up every field in the structure.
28 *
29 * @dev: Device to read from
30 * @time: Place to put the time that is read
31 */
32 int (*get)(struct udevice *dev, struct rtc_time *time);
33
34 /**
35 * set() - set the current time
36 *
37 * Sets the time in the RTC device. The driver can expect every
38 * field to be set correctly.
39 *
40 * @dev: Device to read from
41 * @time: Time to write
42 */
43 int (*set)(struct udevice *dev, const struct rtc_time *time);
44
45 /**
46 * reset() - reset the RTC to a known-good state
47 *
48 * This function resets the RTC to a known-good state. The time may
49 * be unset by this method, so should be set after this method is
50 * called.
51 *
52 * @dev: Device to read from
53 * @return 0 if OK, -ve on error
54 */
55 int (*reset)(struct udevice *dev);
56
57 /**
58 * read8() - Read an 8-bit register
59 *
60 * @dev: Device to read from
61 * @reg: Register to read
62 * @return value read, or -ve on error
63 */
64 int (*read8)(struct udevice *dev, unsigned int reg);
65
66 /**
67 * write8() - Write an 8-bit register
68 *
69 * @dev: Device to write to
70 * @reg: Register to write
71 * @value: Value to write
72 * @return 0 if OK, -ve on error
73 */
74 int (*write8)(struct udevice *dev, unsigned int reg, int val);
75};
76
77/* Access the operations for an RTC device */
78#define rtc_get_ops(dev) ((struct rtc_ops *)(dev)->driver->ops)
79
80/**
81 * dm_rtc_get() - Read the time from an RTC
82 *
83 * @dev: Device to read from
84 * @time: Place to put the current time
85 * @return 0 if OK, -ve on error
86 */
87int dm_rtc_get(struct udevice *dev, struct rtc_time *time);
88
89/**
90 * dm_rtc_put() - Write a time to an RTC
91 *
92 * @dev: Device to read from
93 * @time: Time to write into the RTC
94 * @return 0 if OK, -ve on error
95 */
96int dm_rtc_set(struct udevice *dev, struct rtc_time *time);
97
98/**
99 * dm_rtc_reset() - reset the RTC to a known-good state
100 *
101 * If the RTC appears to be broken (e.g. it is not counting up in seconds)
102 * it may need to be reset to a known good state. This function achieves this.
103 * After resetting the RTC the time should then be set to a known value by
104 * the caller.
105 *
106 * @dev: Device to read from
107 * @return 0 if OK, -ve on error
108 */
109int dm_rtc_reset(struct udevice *dev);
110
111/**
112 * rtc_read8() - Read an 8-bit register
113 *
114 * @dev: Device to read from
115 * @reg: Register to read
116 * @return value read, or -ve on error
117 */
118int rtc_read8(struct udevice *dev, unsigned int reg);
119
120/**
121 * rtc_write8() - Write an 8-bit register
122 *
123 * @dev: Device to write to
124 * @reg: Register to write
125 * @value: Value to write
126 * @return 0 if OK, -ve on error
127 */
128int rtc_write8(struct udevice *dev, unsigned int reg, int val);
129
130/**
131 * rtc_read32() - Read a 32-bit value from the RTC
132 *
133 * @dev: Device to read from
134 * @reg: Offset to start reading from
135 * @valuep: Place to put the value that is read
136 * @return 0 if OK, -ve on error
137 */
138int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep);
139
140/**
141 * rtc_write32() - Write a 32-bit value to the RTC
142 *
143 * @dev: Device to write to
144 * @reg: Register to start writing to
145 * @value: Value to write
146 * @return 0 if OK, -ve on error
147 */
148int rtc_write32(struct udevice *dev, unsigned int reg, u32 value);
149
150#else
Yuri Tikhonov9bacd942008-03-20 17:56:04 +0300151int rtc_get (struct rtc_time *);
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +0200152int rtc_set (struct rtc_time *);
wdenka6840a62001-04-09 21:43:07 +0000153void rtc_reset (void);
154
Simon Glass538059d2014-11-14 18:18:26 -0700155/**
Simon Glassb4ae6002015-01-19 22:16:10 -0700156 * rtc_read8() - Read an 8-bit register
157 *
158 * @reg: Register to read
159 * @return value read
160 */
161int rtc_read8(int reg);
162
163/**
164 * rtc_write8() - Write an 8-bit register
165 *
166 * @reg: Register to write
167 * @value: Value to write
168 */
169void rtc_write8(int reg, uchar val);
170
171/**
172 * rtc_read32() - Read a 32-bit value from the RTC
173 *
174 * @reg: Offset to start reading from
175 * @return value read
176 */
177u32 rtc_read32(int reg);
178
179/**
180 * rtc_write32() - Write a 32-bit value to the RTC
181 *
182 * @reg: Register to start writing to
183 * @value: Value to write
184 */
185void rtc_write32(int reg, u32 value);
186
187/**
Simon Glass538059d2014-11-14 18:18:26 -0700188 * rtc_init() - Set up the real time clock ready for use
189 */
190void rtc_init(void);
Simon Glassd9050b52015-04-20 12:37:23 -0600191#endif
Simon Glass538059d2014-11-14 18:18:26 -0700192
Simon Glass2d937bb2015-04-20 12:37:17 -0600193/**
194 * rtc_calc_weekday() - Work out the weekday from a time
195 *
196 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK).
197 * It sets time->tm_wdaay to the correct day of the week.
198 *
199 * @time: Time to inspect. tm_wday is updated
200 * @return 0 if OK, -EINVAL if the weekday could not be determined
201 */
202int rtc_calc_weekday(struct rtc_time *time);
203
Simon Glass12994662015-04-20 12:37:18 -0600204/**
205 * rtc_to_tm() - Convert a time_t value into a broken-out time
206 *
207 * The following fields are set up by this function:
208 * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday
209 *
210 * Note that tm_yday and tm_isdst are set to 0.
211 *
212 * @time_t: Number of seconds since 1970-01-01 00:00:00
213 * @time: Place to put the broken-out time
214 * @return 0 if OK, -EINVAL if the weekday could not be determined
215 */
216int rtc_to_tm(int time_t, struct rtc_time *time);
217
Simon Glass4283e842015-04-20 12:37:19 -0600218/**
219 * rtc_mktime() - Convert a broken-out time into a time_t value
220 *
221 * The following fields need to be valid for this function to work:
222 * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year
223 *
224 * Note that tm_wday and tm_yday are ignored.
225 *
226 * @time: Broken-out time to convert
227 * @return corresponding time_t value, seconds since 1970-01-01 00:00:00
228 */
229unsigned long rtc_mktime(const struct rtc_time *time);
230
wdenka6840a62001-04-09 21:43:07 +0000231#endif /* _RTC_H_ */