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 */
23 * get() - get the current time
25 * Returns the current time read from the RTC device. The driver
26 * is responsible for setting up every field in the structure.
28 * @dev: Device to read from
29 * @time: Place to put the time that is read
31 int (*get)(struct udevice *dev, struct rtc_time *time);
34 * set() - set the current time
36 * Sets the time in the RTC device. The driver can expect every
37 * field to be set correctly.
39 * @dev: Device to read from
40 * @time: Time to write
42 int (*set)(struct udevice *dev, const struct rtc_time *time);
45 * reset() - reset the RTC to a known-good state
47 * This function resets the RTC to a known-good state. The time may
48 * be unset by this method, so should be set after this method is
51 * @dev: Device to read from
52 * @return 0 if OK, -ve on error
54 int (*reset)(struct udevice *dev);
57 * read8() - Read an 8-bit register
59 * @dev: Device to read from
60 * @reg: Register to read
61 * @return value read, or -ve on error
63 int (*read8)(struct udevice *dev, unsigned int reg);
66 * write8() - Write an 8-bit register
68 * @dev: Device to write to
69 * @reg: Register to write
70 * @value: Value to write
71 * @return 0 if OK, -ve on error
73 int (*write8)(struct udevice *dev, unsigned int reg, int val);
76 /* Access the operations for an RTC device */
77 #define rtc_get_ops(dev) ((struct rtc_ops *)(dev)->driver->ops)
80 * dm_rtc_get() - Read the time from an RTC
82 * @dev: Device to read from
83 * @time: Place to put the current time
84 * @return 0 if OK, -ve on error
86 int dm_rtc_get(struct udevice *dev, struct rtc_time *time);
89 * dm_rtc_set() - Write a time to an RTC
91 * @dev: Device to read from
92 * @time: Time to write into the RTC
93 * @return 0 if OK, -ve on error
95 int dm_rtc_set(struct udevice *dev, struct rtc_time *time);
98 * dm_rtc_reset() - reset the RTC to a known-good state
100 * If the RTC appears to be broken (e.g. it is not counting up in seconds)
101 * it may need to be reset to a known good state. This function achieves this.
102 * After resetting the RTC the time should then be set to a known value by
105 * @dev: Device to read from
106 * @return 0 if OK, -ve on error
108 int dm_rtc_reset(struct udevice *dev);
111 * rtc_read8() - Read an 8-bit register
113 * @dev: Device to read from
114 * @reg: Register to read
115 * @return value read, or -ve on error
117 int rtc_read8(struct udevice *dev, unsigned int reg);
120 * rtc_write8() - Write an 8-bit register
122 * @dev: Device to write to
123 * @reg: Register to write
124 * @value: Value to write
125 * @return 0 if OK, -ve on error
127 int rtc_write8(struct udevice *dev, unsigned int reg, int val);
130 * rtc_read16() - Read a 16-bit value from the RTC
132 * @dev: Device to read from
133 * @reg: Offset to start reading from
134 * @valuep: Place to put the value that is read
135 * @return 0 if OK, -ve on error
137 int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep);
140 * rtc_write16() - Write a 16-bit value to the RTC
142 * @dev: Device to write to
143 * @reg: Register to start writing to
144 * @value: Value to write
145 * @return 0 if OK, -ve on error
147 int rtc_write16(struct udevice *dev, unsigned int reg, u16 value);
150 * rtc_read32() - Read a 32-bit value from the RTC
152 * @dev: Device to read from
153 * @reg: Offset to start reading from
154 * @valuep: Place to put the value that is read
155 * @return 0 if OK, -ve on error
157 int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep);
160 * rtc_write32() - Write a 32-bit value to the RTC
162 * @dev: Device to write to
163 * @reg: Register to start writing to
164 * @value: Value to write
165 * @return 0 if OK, -ve on error
167 int rtc_write32(struct udevice *dev, unsigned int reg, u32 value);
170 int rtc_get (struct rtc_time *);
171 int rtc_set (struct rtc_time *);
172 void rtc_reset (void);
173 void rtc_enable_32khz_output(void);
176 * rtc_read8() - Read an 8-bit register
178 * @reg: Register to read
181 int rtc_read8(int reg);
184 * rtc_write8() - Write an 8-bit register
186 * @reg: Register to write
187 * @value: Value to write
189 void rtc_write8(int reg, uchar val);
192 * rtc_read32() - Read a 32-bit value from the RTC
194 * @reg: Offset to start reading from
197 u32 rtc_read32(int reg);
200 * rtc_write32() - Write a 32-bit value to the RTC
202 * @reg: Register to start writing to
203 * @value: Value to write
205 void rtc_write32(int reg, u32 value);
208 * rtc_init() - Set up the real time clock ready for use
211 #endif /* CONFIG_DM_RTC */
214 * is_leap_year - Check if year is a leap year
217 * @return 1 if leap year
219 static inline bool is_leap_year(unsigned int year)
221 return (!(year % 4) && (year % 100)) || !(year % 400);
225 * rtc_calc_weekday() - Work out the weekday from a time
227 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK).
228 * It sets time->tm_wdaay to the correct day of the week.
230 * @time: Time to inspect. tm_wday is updated
231 * @return 0 if OK, -EINVAL if the weekday could not be determined
233 int rtc_calc_weekday(struct rtc_time *time);
236 * rtc_to_tm() - Convert a time_t value into a broken-out time
238 * The following fields are set up by this function:
239 * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday
241 * Note that tm_yday and tm_isdst are set to 0.
243 * @time_t: Number of seconds since 1970-01-01 00:00:00
244 * @time: Place to put the broken-out time
246 void rtc_to_tm(u64 time_t, struct rtc_time *time);
249 * rtc_mktime() - Convert a broken-out time into a time_t value
251 * The following fields need to be valid for this function to work:
252 * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year
254 * Note that tm_wday and tm_yday are ignored.
256 * @time: Broken-out time to convert
257 * @return corresponding time_t value, seconds since 1970-01-01 00:00:00
259 unsigned long rtc_mktime(const struct rtc_time *time);
262 * rtc_month_days() - The number of days in the month
264 * @month: month (January = 0)
265 * @year: year (4 digits)
267 int rtc_month_days(unsigned int month, unsigned int year);