1 /* 2 * (C) Copyright 2001 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 /* 9 * Generic RTC interface. 10 */ 11 #ifndef _RTC_H_ 12 #define _RTC_H_ 13 14 /* 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> 18 #include <rtc_def.h> 19 20 #ifdef CONFIG_DM_RTC 21 22 struct 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 * alarm_trigger() 78 * @dev: Device to write to 79 * @return 1 if rtc alarm trigger boot on 80 */ 81 int (*alarm_trigger)(struct udevice *dev); 82 }; 83 84 /* Access the operations for an RTC device */ 85 #define rtc_get_ops(dev) ((struct rtc_ops *)(dev)->driver->ops) 86 87 /** 88 * dm_rtc_get() - Read the time from an RTC 89 * 90 * @dev: Device to read from 91 * @time: Place to put the current time 92 * @return 0 if OK, -ve on error 93 */ 94 int dm_rtc_get(struct udevice *dev, struct rtc_time *time); 95 96 /** 97 * dm_rtc_put() - Write a time to an RTC 98 * 99 * @dev: Device to read from 100 * @time: Time to write into the RTC 101 * @return 0 if OK, -ve on error 102 */ 103 int dm_rtc_set(struct udevice *dev, struct rtc_time *time); 104 105 /** 106 * dm_rtc_reset() - reset the RTC to a known-good state 107 * 108 * If the RTC appears to be broken (e.g. it is not counting up in seconds) 109 * it may need to be reset to a known good state. This function achieves this. 110 * After resetting the RTC the time should then be set to a known value by 111 * the caller. 112 * 113 * @dev: Device to read from 114 * @return 0 if OK, -ve on error 115 */ 116 int dm_rtc_reset(struct udevice *dev); 117 118 /** 119 * rtc_read8() - Read an 8-bit register 120 * 121 * @dev: Device to read from 122 * @reg: Register to read 123 * @return value read, or -ve on error 124 */ 125 int rtc_read8(struct udevice *dev, unsigned int reg); 126 127 /** 128 * rtc_write8() - Write an 8-bit register 129 * 130 * @dev: Device to write to 131 * @reg: Register to write 132 * @value: Value to write 133 * @return 0 if OK, -ve on error 134 */ 135 int rtc_write8(struct udevice *dev, unsigned int reg, int val); 136 137 /** 138 * rtc_read16() - Read a 16-bit value from the RTC 139 * 140 * @dev: Device to read from 141 * @reg: Offset to start reading from 142 * @valuep: Place to put the value that is read 143 * @return 0 if OK, -ve on error 144 */ 145 int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep); 146 147 /** 148 * rtc_write16() - Write a 16-bit value to the RTC 149 * 150 * @dev: Device to write to 151 * @reg: Register to start writing to 152 * @value: Value to write 153 * @return 0 if OK, -ve on error 154 */ 155 int rtc_write16(struct udevice *dev, unsigned int reg, u16 value); 156 157 /** 158 * rtc_read32() - Read a 32-bit value from the RTC 159 * 160 * @dev: Device to read from 161 * @reg: Offset to start reading from 162 * @valuep: Place to put the value that is read 163 * @return 0 if OK, -ve on error 164 */ 165 int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep); 166 167 /** 168 * rtc_write32() - Write a 32-bit value to the RTC 169 * 170 * @dev: Device to write to 171 * @reg: Register to start writing to 172 * @value: Value to write 173 * @return 0 if OK, -ve on error 174 */ 175 int rtc_write32(struct udevice *dev, unsigned int reg, u32 value); 176 177 /** 178 * rtc_alarm_trigger() 179 * 180 * @dev: Device to write to 181 * @return 1 if rtc alarm trigger boot on 182 */ 183 int rtc_alarm_trigger(struct udevice *dev); 184 #else 185 int rtc_get (struct rtc_time *); 186 int rtc_set (struct rtc_time *); 187 void rtc_reset (void); 188 void rtc_enable_32khz_output(void); 189 190 /** 191 * rtc_read8() - Read an 8-bit register 192 * 193 * @reg: Register to read 194 * @return value read 195 */ 196 int rtc_read8(int reg); 197 198 /** 199 * rtc_write8() - Write an 8-bit register 200 * 201 * @reg: Register to write 202 * @value: Value to write 203 */ 204 void rtc_write8(int reg, uchar val); 205 206 /** 207 * rtc_read32() - Read a 32-bit value from the RTC 208 * 209 * @reg: Offset to start reading from 210 * @return value read 211 */ 212 u32 rtc_read32(int reg); 213 214 /** 215 * rtc_write32() - Write a 32-bit value to the RTC 216 * 217 * @reg: Register to start writing to 218 * @value: Value to write 219 */ 220 void rtc_write32(int reg, u32 value); 221 222 /** 223 * rtc_init() - Set up the real time clock ready for use 224 */ 225 void rtc_init(void); 226 #endif 227 228 /** 229 * rtc_calc_weekday() - Work out the weekday from a time 230 * 231 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK). 232 * It sets time->tm_wdaay to the correct day of the week. 233 * 234 * @time: Time to inspect. tm_wday is updated 235 * @return 0 if OK, -EINVAL if the weekday could not be determined 236 */ 237 int rtc_calc_weekday(struct rtc_time *time); 238 239 /** 240 * rtc_to_tm() - Convert a time_t value into a broken-out time 241 * 242 * The following fields are set up by this function: 243 * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday 244 * 245 * Note that tm_yday and tm_isdst are set to 0. 246 * 247 * @time_t: Number of seconds since 1970-01-01 00:00:00 248 * @time: Place to put the broken-out time 249 * @return 0 if OK, -EINVAL if the weekday could not be determined 250 */ 251 int rtc_to_tm(int time_t, struct rtc_time *time); 252 253 /** 254 * rtc_mktime() - Convert a broken-out time into a time_t value 255 * 256 * The following fields need to be valid for this function to work: 257 * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year 258 * 259 * Note that tm_wday and tm_yday are ignored. 260 * 261 * @time: Broken-out time to convert 262 * @return corresponding time_t value, seconds since 1970-01-01 00:00:00 263 */ 264 unsigned long rtc_mktime(const struct rtc_time *time); 265 266 #endif /* _RTC_H_ */ 267