Michael Walle | 2febcea | 2019-05-29 01:29:58 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Date & Time support for Micro Crystal RV-8803-C7. |
| 4 | * |
| 5 | * based on ds1307.c which is |
| 6 | * (C) Copyright 2001, 2002, 2003 |
| 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 8 | * Keith Outwater, keith_outwater@mvis.com` |
| 9 | * Steven Scholz, steven.scholz@imc-berlin.de |
| 10 | * |
| 11 | */ |
| 12 | |
Michael Walle | 2febcea | 2019-05-29 01:29:58 +0200 | [diff] [blame] | 13 | #include <command.h> |
| 14 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Michael Walle | 2febcea | 2019-05-29 01:29:58 +0200 | [diff] [blame] | 16 | #include <rtc.h> |
| 17 | #include <i2c.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Michael Walle | 2febcea | 2019-05-29 01:29:58 +0200 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * RTC register addresses |
| 22 | */ |
| 23 | #define RTC_SEC_REG_ADDR 0x00 |
| 24 | #define RTC_MIN_REG_ADDR 0x01 |
| 25 | #define RTC_HR_REG_ADDR 0x02 |
| 26 | #define RTC_DAY_REG_ADDR 0x03 |
| 27 | #define RTC_DATE_REG_ADDR 0x04 |
| 28 | #define RTC_MON_REG_ADDR 0x05 |
| 29 | #define RTC_YR_REG_ADDR 0x06 |
| 30 | |
| 31 | #define RTC_FLAG_REG_ADDR 0x0E |
| 32 | #define RTC_FLAG_BIT_V1F BIT(0) |
| 33 | #define RTC_FLAG_BIT_V2F BIT(1) |
| 34 | |
| 35 | #define RTC_CTL_REG_ADDR 0x0F |
| 36 | #define RTC_CTL_BIT_RST BIT(0) |
| 37 | |
| 38 | static int rv8803_rtc_set(struct udevice *dev, const struct rtc_time *tm) |
| 39 | { |
| 40 | int ret; |
| 41 | u8 buf[7]; |
| 42 | |
| 43 | debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 44 | tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday, |
| 45 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 46 | |
| 47 | if (tm->tm_year < 2000 || tm->tm_year > 2099) |
| 48 | printf("WARNING: year should be between 2000 and 2099!\n"); |
| 49 | |
| 50 | buf[RTC_YR_REG_ADDR] = bin2bcd(tm->tm_year % 100); |
Oliver Graute | 9b36188 | 2022-04-26 09:26:12 +0200 | [diff] [blame] | 51 | buf[RTC_MON_REG_ADDR] = bin2bcd(tm->tm_mon + 1); |
Michael Walle | 2febcea | 2019-05-29 01:29:58 +0200 | [diff] [blame] | 52 | buf[RTC_DAY_REG_ADDR] = 1 << (tm->tm_wday & 0x7); |
| 53 | buf[RTC_DATE_REG_ADDR] = bin2bcd(tm->tm_mday); |
| 54 | buf[RTC_HR_REG_ADDR] = bin2bcd(tm->tm_hour); |
| 55 | buf[RTC_MIN_REG_ADDR] = bin2bcd(tm->tm_min); |
| 56 | buf[RTC_SEC_REG_ADDR] = bin2bcd(tm->tm_sec); |
| 57 | |
| 58 | ret = dm_i2c_write(dev, 0, buf, sizeof(buf)); |
| 59 | if (ret < 0) |
| 60 | return ret; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int rv8803_rtc_get(struct udevice *dev, struct rtc_time *tm) |
| 66 | { |
| 67 | int ret; |
| 68 | u8 buf[7]; |
| 69 | int flags; |
| 70 | |
| 71 | flags = dm_i2c_reg_read(dev, RTC_FLAG_REG_ADDR); |
| 72 | if (flags < 0) |
| 73 | return flags; |
| 74 | debug("%s: flags=%Xh\n", __func__, flags); |
| 75 | |
| 76 | if (flags & RTC_FLAG_BIT_V1F) |
| 77 | printf("### Warning: temperature compensation has stopped\n"); |
| 78 | |
| 79 | if (flags & RTC_FLAG_BIT_V2F) { |
| 80 | printf("### Warning: Voltage low, data is invalid\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | ret = dm_i2c_read(dev, 0, buf, sizeof(buf)); |
| 85 | if (ret < 0) |
| 86 | return ret; |
| 87 | |
| 88 | tm->tm_sec = bcd2bin(buf[RTC_SEC_REG_ADDR] & 0x7F); |
| 89 | tm->tm_min = bcd2bin(buf[RTC_MIN_REG_ADDR] & 0x7F); |
| 90 | tm->tm_hour = bcd2bin(buf[RTC_HR_REG_ADDR] & 0x3F); |
| 91 | tm->tm_mday = bcd2bin(buf[RTC_DATE_REG_ADDR] & 0x3F); |
Oliver Graute | 9b36188 | 2022-04-26 09:26:12 +0200 | [diff] [blame] | 92 | tm->tm_mon = bcd2bin(buf[RTC_MON_REG_ADDR] & 0x1F) - 1; |
Michael Walle | 2febcea | 2019-05-29 01:29:58 +0200 | [diff] [blame] | 93 | tm->tm_year = bcd2bin(buf[RTC_YR_REG_ADDR]) + 2000; |
| 94 | tm->tm_wday = fls(buf[RTC_DAY_REG_ADDR] & 0x7F) - 1; |
| 95 | tm->tm_yday = 0; |
| 96 | tm->tm_isdst = 0; |
| 97 | |
| 98 | debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 99 | tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday, |
| 100 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int rv8803_rtc_reset(struct udevice *dev) |
| 106 | { |
| 107 | int ret; |
| 108 | struct rtc_time tmp = { |
| 109 | .tm_year = 2000, |
| 110 | .tm_mon = 1, |
| 111 | .tm_mday = 1, |
| 112 | .tm_hour = 0, |
| 113 | .tm_min = 0, |
| 114 | .tm_sec = 0, |
| 115 | }; |
| 116 | |
| 117 | /* assert reset */ |
| 118 | ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, RTC_CTL_BIT_RST); |
| 119 | if (ret < 0) |
| 120 | return ret; |
| 121 | |
| 122 | /* clear all flags */ |
| 123 | ret = dm_i2c_reg_write(dev, RTC_FLAG_REG_ADDR, 0); |
| 124 | if (ret < 0) |
| 125 | return ret; |
| 126 | |
| 127 | ret = rv8803_rtc_set(dev, &tmp); |
| 128 | if (ret < 0) |
| 129 | return ret; |
| 130 | |
| 131 | /* clear reset */ |
| 132 | ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, 0); |
| 133 | if (ret < 0) |
| 134 | return ret; |
| 135 | |
| 136 | debug("RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n", |
| 137 | tmp.tm_year, tmp.tm_mon, tmp.tm_mday, |
| 138 | tmp.tm_hour, tmp.tm_min, tmp.tm_sec); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int rv8803_probe(struct udevice *dev) |
| 144 | { |
| 145 | i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS | |
| 146 | DM_I2C_CHIP_WR_ADDRESS); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static const struct rtc_ops rv8803_rtc_ops = { |
| 152 | .get = rv8803_rtc_get, |
| 153 | .set = rv8803_rtc_set, |
| 154 | .reset = rv8803_rtc_reset, |
| 155 | }; |
| 156 | |
| 157 | static const struct udevice_id rv8803_rtc_ids[] = { |
| 158 | { .compatible = "microcrystal,rv8803", }, |
Heiko Thiery | 9792c0a | 2021-09-23 11:14:32 +0200 | [diff] [blame] | 159 | { .compatible = "epson,rx8803" }, |
| 160 | { .compatible = "epson,rx8900" }, |
Michael Walle | 2febcea | 2019-05-29 01:29:58 +0200 | [diff] [blame] | 161 | { } |
| 162 | }; |
| 163 | |
| 164 | U_BOOT_DRIVER(rtc_rv8803) = { |
| 165 | .name = "rtc-rv8803", |
| 166 | .id = UCLASS_RTC, |
| 167 | .probe = rv8803_probe, |
| 168 | .of_match = rv8803_rtc_ids, |
| 169 | .ops = &rv8803_rtc_ops, |
| 170 | }; |