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