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 /** 131dbeda5b2SSimon Glass * rtc_read32() - Read a 32-bit value from the RTC 132dbeda5b2SSimon Glass * 133dbeda5b2SSimon Glass * @dev: Device to read from 134dbeda5b2SSimon Glass * @reg: Offset to start reading from 135dbeda5b2SSimon Glass * @valuep: Place to put the value that is read 136dbeda5b2SSimon Glass * @return 0 if OK, -ve on error 137dbeda5b2SSimon Glass */ 138dbeda5b2SSimon Glass int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep); 139dbeda5b2SSimon Glass 140dbeda5b2SSimon Glass /** 141dbeda5b2SSimon Glass * rtc_write32() - Write a 32-bit value to the RTC 142dbeda5b2SSimon Glass * 143dbeda5b2SSimon Glass * @dev: Device to write to 144dbeda5b2SSimon Glass * @reg: Register to start writing to 145dbeda5b2SSimon Glass * @value: Value to write 146dbeda5b2SSimon Glass * @return 0 if OK, -ve on error 147dbeda5b2SSimon Glass */ 148dbeda5b2SSimon Glass int rtc_write32(struct udevice *dev, unsigned int reg, u32 value); 149dbeda5b2SSimon Glass 150dbeda5b2SSimon Glass #else 151b73a19e1SYuri Tikhonov int rtc_get (struct rtc_time *); 152d1e23194SJean-Christophe PLAGNIOL-VILLARD int rtc_set (struct rtc_time *); 153a6840a6eSwdenk void rtc_reset (void); 154*c340941eSPriyanka Jain void rtc_enable_32khz_output(void); 155a6840a6eSwdenk 156c6577f72SSimon Glass /** 157fc4860c0SSimon Glass * rtc_read8() - Read an 8-bit register 158fc4860c0SSimon Glass * 159fc4860c0SSimon Glass * @reg: Register to read 160fc4860c0SSimon Glass * @return value read 161fc4860c0SSimon Glass */ 162fc4860c0SSimon Glass int rtc_read8(int reg); 163fc4860c0SSimon Glass 164fc4860c0SSimon Glass /** 165fc4860c0SSimon Glass * rtc_write8() - Write an 8-bit register 166fc4860c0SSimon Glass * 167fc4860c0SSimon Glass * @reg: Register to write 168fc4860c0SSimon Glass * @value: Value to write 169fc4860c0SSimon Glass */ 170fc4860c0SSimon Glass void rtc_write8(int reg, uchar val); 171fc4860c0SSimon Glass 172fc4860c0SSimon Glass /** 173fc4860c0SSimon Glass * rtc_read32() - Read a 32-bit value from the RTC 174fc4860c0SSimon Glass * 175fc4860c0SSimon Glass * @reg: Offset to start reading from 176fc4860c0SSimon Glass * @return value read 177fc4860c0SSimon Glass */ 178fc4860c0SSimon Glass u32 rtc_read32(int reg); 179fc4860c0SSimon Glass 180fc4860c0SSimon Glass /** 181fc4860c0SSimon Glass * rtc_write32() - Write a 32-bit value to the RTC 182fc4860c0SSimon Glass * 183fc4860c0SSimon Glass * @reg: Register to start writing to 184fc4860c0SSimon Glass * @value: Value to write 185fc4860c0SSimon Glass */ 186fc4860c0SSimon Glass void rtc_write32(int reg, u32 value); 187fc4860c0SSimon Glass 188fc4860c0SSimon Glass /** 189c6577f72SSimon Glass * rtc_init() - Set up the real time clock ready for use 190c6577f72SSimon Glass */ 191c6577f72SSimon Glass void rtc_init(void); 192dbeda5b2SSimon Glass #endif 193c6577f72SSimon Glass 194199e87c3SSimon Glass /** 195199e87c3SSimon Glass * rtc_calc_weekday() - Work out the weekday from a time 196199e87c3SSimon Glass * 197199e87c3SSimon Glass * This only works for the Gregorian calendar - i.e. after 1752 (in the UK). 198199e87c3SSimon Glass * It sets time->tm_wdaay to the correct day of the week. 199199e87c3SSimon Glass * 200199e87c3SSimon Glass * @time: Time to inspect. tm_wday is updated 201199e87c3SSimon Glass * @return 0 if OK, -EINVAL if the weekday could not be determined 202199e87c3SSimon Glass */ 203199e87c3SSimon Glass int rtc_calc_weekday(struct rtc_time *time); 204199e87c3SSimon Glass 2059f9276c3SSimon Glass /** 2069f9276c3SSimon Glass * rtc_to_tm() - Convert a time_t value into a broken-out time 2079f9276c3SSimon Glass * 2089f9276c3SSimon Glass * The following fields are set up by this function: 2099f9276c3SSimon Glass * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday 2109f9276c3SSimon Glass * 2119f9276c3SSimon Glass * Note that tm_yday and tm_isdst are set to 0. 2129f9276c3SSimon Glass * 2139f9276c3SSimon Glass * @time_t: Number of seconds since 1970-01-01 00:00:00 2149f9276c3SSimon Glass * @time: Place to put the broken-out time 2159f9276c3SSimon Glass * @return 0 if OK, -EINVAL if the weekday could not be determined 2169f9276c3SSimon Glass */ 2179f9276c3SSimon Glass int rtc_to_tm(int time_t, struct rtc_time *time); 2189f9276c3SSimon Glass 21971420983SSimon Glass /** 22071420983SSimon Glass * rtc_mktime() - Convert a broken-out time into a time_t value 22171420983SSimon Glass * 22271420983SSimon Glass * The following fields need to be valid for this function to work: 22371420983SSimon Glass * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year 22471420983SSimon Glass * 22571420983SSimon Glass * Note that tm_wday and tm_yday are ignored. 22671420983SSimon Glass * 22771420983SSimon Glass * @time: Broken-out time to convert 22871420983SSimon Glass * @return corresponding time_t value, seconds since 1970-01-01 00:00:00 22971420983SSimon Glass */ 23071420983SSimon Glass unsigned long rtc_mktime(const struct rtc_time *time); 23171420983SSimon Glass 232a6840a6eSwdenk #endif /* _RTC_H_ */ 233