1a6840a6eSwdenk /* 2a6840a6eSwdenk * (C) Copyright 2001 3a6840a6eSwdenk * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4a6840a6eSwdenk * 51a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 6a6840a6eSwdenk */ 7a6840a6eSwdenk 8a6840a6eSwdenk /* 9a6840a6eSwdenk * Generic RTC interface. 10a6840a6eSwdenk */ 11a6840a6eSwdenk #ifndef _RTC_H_ 12a6840a6eSwdenk #define _RTC_H_ 13a6840a6eSwdenk 14885fc78cSAlbin Tonnerre /* bcd<->bin functions are needed by almost all the RTC drivers, let's include 15885fc78cSAlbin Tonnerre * it there instead of in evey single driver */ 16885fc78cSAlbin Tonnerre 17885fc78cSAlbin Tonnerre #include <bcd.h> 18aac51198SSimon Glass #include <rtc_def.h> 19a6840a6eSwdenk 20dbeda5b2SSimon Glass #ifdef CONFIG_DM_RTC 21dbeda5b2SSimon Glass 22dbeda5b2SSimon Glass struct rtc_ops { 23dbeda5b2SSimon Glass /** 24dbeda5b2SSimon Glass * get() - get the current time 25dbeda5b2SSimon Glass * 26dbeda5b2SSimon Glass * Returns the current time read from the RTC device. The driver 27dbeda5b2SSimon Glass * is responsible for setting up every field in the structure. 28dbeda5b2SSimon Glass * 29dbeda5b2SSimon Glass * @dev: Device to read from 30dbeda5b2SSimon Glass * @time: Place to put the time that is read 31dbeda5b2SSimon Glass */ 32dbeda5b2SSimon Glass int (*get)(struct udevice *dev, struct rtc_time *time); 33dbeda5b2SSimon Glass 34dbeda5b2SSimon Glass /** 35dbeda5b2SSimon Glass * set() - set the current time 36dbeda5b2SSimon Glass * 37dbeda5b2SSimon Glass * Sets the time in the RTC device. The driver can expect every 38dbeda5b2SSimon Glass * field to be set correctly. 39dbeda5b2SSimon Glass * 40dbeda5b2SSimon Glass * @dev: Device to read from 41dbeda5b2SSimon Glass * @time: Time to write 42dbeda5b2SSimon Glass */ 43dbeda5b2SSimon Glass int (*set)(struct udevice *dev, const struct rtc_time *time); 44dbeda5b2SSimon Glass 45dbeda5b2SSimon Glass /** 46dbeda5b2SSimon Glass * reset() - reset the RTC to a known-good state 47dbeda5b2SSimon Glass * 48dbeda5b2SSimon Glass * This function resets the RTC to a known-good state. The time may 49dbeda5b2SSimon Glass * be unset by this method, so should be set after this method is 50dbeda5b2SSimon Glass * called. 51dbeda5b2SSimon Glass * 52dbeda5b2SSimon Glass * @dev: Device to read from 53dbeda5b2SSimon Glass * @return 0 if OK, -ve on error 54dbeda5b2SSimon Glass */ 55dbeda5b2SSimon Glass int (*reset)(struct udevice *dev); 56dbeda5b2SSimon Glass 57dbeda5b2SSimon Glass /** 58dbeda5b2SSimon Glass * read8() - Read an 8-bit register 59dbeda5b2SSimon Glass * 60dbeda5b2SSimon Glass * @dev: Device to read from 61dbeda5b2SSimon Glass * @reg: Register to read 62dbeda5b2SSimon Glass * @return value read, or -ve on error 63dbeda5b2SSimon Glass */ 64dbeda5b2SSimon Glass int (*read8)(struct udevice *dev, unsigned int reg); 65dbeda5b2SSimon Glass 66dbeda5b2SSimon Glass /** 67dbeda5b2SSimon Glass * write8() - Write an 8-bit register 68dbeda5b2SSimon Glass * 69dbeda5b2SSimon Glass * @dev: Device to write to 70dbeda5b2SSimon Glass * @reg: Register to write 71dbeda5b2SSimon Glass * @value: Value to write 72dbeda5b2SSimon Glass * @return 0 if OK, -ve on error 73dbeda5b2SSimon Glass */ 74dbeda5b2SSimon Glass int (*write8)(struct udevice *dev, unsigned int reg, int val); 75dbeda5b2SSimon Glass }; 76dbeda5b2SSimon Glass 77dbeda5b2SSimon Glass /* Access the operations for an RTC device */ 78dbeda5b2SSimon Glass #define rtc_get_ops(dev) ((struct rtc_ops *)(dev)->driver->ops) 79dbeda5b2SSimon Glass 80dbeda5b2SSimon Glass /** 81dbeda5b2SSimon Glass * dm_rtc_get() - Read the time from an RTC 82dbeda5b2SSimon Glass * 83dbeda5b2SSimon Glass * @dev: Device to read from 84dbeda5b2SSimon Glass * @time: Place to put the current time 85dbeda5b2SSimon Glass * @return 0 if OK, -ve on error 86dbeda5b2SSimon Glass */ 87dbeda5b2SSimon Glass int dm_rtc_get(struct udevice *dev, struct rtc_time *time); 88dbeda5b2SSimon Glass 89dbeda5b2SSimon Glass /** 90dbeda5b2SSimon Glass * dm_rtc_put() - Write a time to an RTC 91dbeda5b2SSimon Glass * 92dbeda5b2SSimon Glass * @dev: Device to read from 93dbeda5b2SSimon Glass * @time: Time to write into the RTC 94dbeda5b2SSimon Glass * @return 0 if OK, -ve on error 95dbeda5b2SSimon Glass */ 96dbeda5b2SSimon Glass int dm_rtc_set(struct udevice *dev, struct rtc_time *time); 97dbeda5b2SSimon Glass 98dbeda5b2SSimon Glass /** 99dbeda5b2SSimon Glass * dm_rtc_reset() - reset the RTC to a known-good state 100dbeda5b2SSimon Glass * 101dbeda5b2SSimon Glass * If the RTC appears to be broken (e.g. it is not counting up in seconds) 102dbeda5b2SSimon Glass * it may need to be reset to a known good state. This function achieves this. 103dbeda5b2SSimon Glass * After resetting the RTC the time should then be set to a known value by 104dbeda5b2SSimon Glass * the caller. 105dbeda5b2SSimon Glass * 106dbeda5b2SSimon Glass * @dev: Device to read from 107dbeda5b2SSimon Glass * @return 0 if OK, -ve on error 108dbeda5b2SSimon Glass */ 109dbeda5b2SSimon Glass int dm_rtc_reset(struct udevice *dev); 110dbeda5b2SSimon Glass 111dbeda5b2SSimon Glass /** 112dbeda5b2SSimon Glass * rtc_read8() - Read an 8-bit register 113dbeda5b2SSimon Glass * 114dbeda5b2SSimon Glass * @dev: Device to read from 115dbeda5b2SSimon Glass * @reg: Register to read 116dbeda5b2SSimon Glass * @return value read, or -ve on error 117dbeda5b2SSimon Glass */ 118dbeda5b2SSimon Glass int rtc_read8(struct udevice *dev, unsigned int reg); 119dbeda5b2SSimon Glass 120dbeda5b2SSimon Glass /** 121dbeda5b2SSimon Glass * rtc_write8() - Write an 8-bit register 122dbeda5b2SSimon Glass * 123dbeda5b2SSimon Glass * @dev: Device to write to 124dbeda5b2SSimon Glass * @reg: Register to write 125dbeda5b2SSimon Glass * @value: Value to write 126dbeda5b2SSimon Glass * @return 0 if OK, -ve on error 127dbeda5b2SSimon Glass */ 128dbeda5b2SSimon Glass int rtc_write8(struct udevice *dev, unsigned int reg, int val); 129dbeda5b2SSimon Glass 130dbeda5b2SSimon Glass /** 131*d24c7fbcSBin Meng * rtc_read16() - Read a 16-bit value from the RTC 132*d24c7fbcSBin Meng * 133*d24c7fbcSBin Meng * @dev: Device to read from 134*d24c7fbcSBin Meng * @reg: Offset to start reading from 135*d24c7fbcSBin Meng * @valuep: Place to put the value that is read 136*d24c7fbcSBin Meng * @return 0 if OK, -ve on error 137*d24c7fbcSBin Meng */ 138*d24c7fbcSBin Meng int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep); 139*d24c7fbcSBin Meng 140*d24c7fbcSBin Meng /** 141*d24c7fbcSBin Meng * rtc_write16() - Write a 16-bit value to the RTC 142*d24c7fbcSBin Meng * 143*d24c7fbcSBin Meng * @dev: Device to write to 144*d24c7fbcSBin Meng * @reg: Register to start writing to 145*d24c7fbcSBin Meng * @value: Value to write 146*d24c7fbcSBin Meng * @return 0 if OK, -ve on error 147*d24c7fbcSBin Meng */ 148*d24c7fbcSBin Meng int rtc_write16(struct udevice *dev, unsigned int reg, u16 value); 149*d24c7fbcSBin Meng 150*d24c7fbcSBin Meng /** 151dbeda5b2SSimon Glass * rtc_read32() - Read a 32-bit value from the RTC 152dbeda5b2SSimon Glass * 153dbeda5b2SSimon Glass * @dev: Device to read from 154dbeda5b2SSimon Glass * @reg: Offset to start reading from 155dbeda5b2SSimon Glass * @valuep: Place to put the value that is read 156dbeda5b2SSimon Glass * @return 0 if OK, -ve on error 157dbeda5b2SSimon Glass */ 158dbeda5b2SSimon Glass int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep); 159dbeda5b2SSimon Glass 160dbeda5b2SSimon Glass /** 161dbeda5b2SSimon Glass * rtc_write32() - Write a 32-bit value to the RTC 162dbeda5b2SSimon Glass * 163dbeda5b2SSimon Glass * @dev: Device to write to 164dbeda5b2SSimon Glass * @reg: Register to start writing to 165dbeda5b2SSimon Glass * @value: Value to write 166dbeda5b2SSimon Glass * @return 0 if OK, -ve on error 167dbeda5b2SSimon Glass */ 168dbeda5b2SSimon Glass int rtc_write32(struct udevice *dev, unsigned int reg, u32 value); 169dbeda5b2SSimon Glass 170dbeda5b2SSimon Glass #else 171b73a19e1SYuri Tikhonov int rtc_get (struct rtc_time *); 172d1e23194SJean-Christophe PLAGNIOL-VILLARD int rtc_set (struct rtc_time *); 173a6840a6eSwdenk void rtc_reset (void); 174c340941eSPriyanka Jain void rtc_enable_32khz_output(void); 175a6840a6eSwdenk 176c6577f72SSimon Glass /** 177fc4860c0SSimon Glass * rtc_read8() - Read an 8-bit register 178fc4860c0SSimon Glass * 179fc4860c0SSimon Glass * @reg: Register to read 180fc4860c0SSimon Glass * @return value read 181fc4860c0SSimon Glass */ 182fc4860c0SSimon Glass int rtc_read8(int reg); 183fc4860c0SSimon Glass 184fc4860c0SSimon Glass /** 185fc4860c0SSimon Glass * rtc_write8() - Write an 8-bit register 186fc4860c0SSimon Glass * 187fc4860c0SSimon Glass * @reg: Register to write 188fc4860c0SSimon Glass * @value: Value to write 189fc4860c0SSimon Glass */ 190fc4860c0SSimon Glass void rtc_write8(int reg, uchar val); 191fc4860c0SSimon Glass 192fc4860c0SSimon Glass /** 193fc4860c0SSimon Glass * rtc_read32() - Read a 32-bit value from the RTC 194fc4860c0SSimon Glass * 195fc4860c0SSimon Glass * @reg: Offset to start reading from 196fc4860c0SSimon Glass * @return value read 197fc4860c0SSimon Glass */ 198fc4860c0SSimon Glass u32 rtc_read32(int reg); 199fc4860c0SSimon Glass 200fc4860c0SSimon Glass /** 201fc4860c0SSimon Glass * rtc_write32() - Write a 32-bit value to the RTC 202fc4860c0SSimon Glass * 203fc4860c0SSimon Glass * @reg: Register to start writing to 204fc4860c0SSimon Glass * @value: Value to write 205fc4860c0SSimon Glass */ 206fc4860c0SSimon Glass void rtc_write32(int reg, u32 value); 207fc4860c0SSimon Glass 208fc4860c0SSimon Glass /** 209c6577f72SSimon Glass * rtc_init() - Set up the real time clock ready for use 210c6577f72SSimon Glass */ 211c6577f72SSimon Glass void rtc_init(void); 212dbeda5b2SSimon Glass #endif 213c6577f72SSimon Glass 214199e87c3SSimon Glass /** 215199e87c3SSimon Glass * rtc_calc_weekday() - Work out the weekday from a time 216199e87c3SSimon Glass * 217199e87c3SSimon Glass * This only works for the Gregorian calendar - i.e. after 1752 (in the UK). 218199e87c3SSimon Glass * It sets time->tm_wdaay to the correct day of the week. 219199e87c3SSimon Glass * 220199e87c3SSimon Glass * @time: Time to inspect. tm_wday is updated 221199e87c3SSimon Glass * @return 0 if OK, -EINVAL if the weekday could not be determined 222199e87c3SSimon Glass */ 223199e87c3SSimon Glass int rtc_calc_weekday(struct rtc_time *time); 224199e87c3SSimon Glass 2259f9276c3SSimon Glass /** 2269f9276c3SSimon Glass * rtc_to_tm() - Convert a time_t value into a broken-out time 2279f9276c3SSimon Glass * 2289f9276c3SSimon Glass * The following fields are set up by this function: 2299f9276c3SSimon Glass * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday 2309f9276c3SSimon Glass * 2319f9276c3SSimon Glass * Note that tm_yday and tm_isdst are set to 0. 2329f9276c3SSimon Glass * 2339f9276c3SSimon Glass * @time_t: Number of seconds since 1970-01-01 00:00:00 2349f9276c3SSimon Glass * @time: Place to put the broken-out time 2359f9276c3SSimon Glass * @return 0 if OK, -EINVAL if the weekday could not be determined 2369f9276c3SSimon Glass */ 2379f9276c3SSimon Glass int rtc_to_tm(int time_t, struct rtc_time *time); 2389f9276c3SSimon Glass 23971420983SSimon Glass /** 24071420983SSimon Glass * rtc_mktime() - Convert a broken-out time into a time_t value 24171420983SSimon Glass * 24271420983SSimon Glass * The following fields need to be valid for this function to work: 24371420983SSimon Glass * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year 24471420983SSimon Glass * 24571420983SSimon Glass * Note that tm_wday and tm_yday are ignored. 24671420983SSimon Glass * 24771420983SSimon Glass * @time: Broken-out time to convert 24871420983SSimon Glass * @return corresponding time_t value, seconds since 1970-01-01 00:00:00 24971420983SSimon Glass */ 25071420983SSimon Glass unsigned long rtc_mktime(const struct rtc_time *time); 25171420983SSimon Glass 252a6840a6eSwdenk #endif /* _RTC_H_ */ 253