Tim Harvey | ed8b974 | 2025-06-02 09:25:15 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Analog Devices DS1672 I2C RTC driver |
| 4 | * |
| 5 | * Copyright 2025 Gateworks Corporation. |
| 6 | */ |
| 7 | #include <dm.h> |
| 8 | #include <i2c.h> |
| 9 | #include <rtc.h> |
| 10 | #include <dm/device_compat.h> |
| 11 | |
| 12 | /* Registers */ |
| 13 | #define DS1672_REG_CNT_BASE 0 |
| 14 | #define DS1672_REG_CONTROL 4 |
| 15 | #define DS1672_REG_TRICKLE 5 |
| 16 | |
| 17 | #define DS1672_REG_CONTROL_EOSC 0x80 |
| 18 | |
| 19 | static int ds1672_read_time(struct udevice *dev, struct rtc_time *tm) |
| 20 | { |
| 21 | time64_t secs; |
| 22 | u8 regs[4]; |
| 23 | int ret; |
| 24 | |
| 25 | ret = dm_i2c_read(dev, DS1672_REG_CONTROL, regs, 1); |
| 26 | if (ret) |
| 27 | return ret; |
| 28 | |
| 29 | if (regs[0] & DS1672_REG_CONTROL_EOSC) { |
| 30 | dev_err(dev, "Oscillator not enabled. Set time to enable.\n"); |
| 31 | return -EINVAL; |
| 32 | } |
| 33 | |
| 34 | ret = dm_i2c_read(dev, DS1672_REG_CNT_BASE, regs, 4); |
| 35 | if (ret) |
| 36 | return ret; |
| 37 | dev_dbg(dev, "raw read: 0x%02x 0x%02x 0x%02x 0x%02x\n", |
| 38 | regs[0], regs[1], regs[2], regs[3]); |
| 39 | secs = ((unsigned long)regs[3] << 24) | (regs[2] << 16) | (regs[1] << 8) | regs[0]; |
| 40 | rtc_to_tm(secs, tm); |
| 41 | |
| 42 | dev_dbg(dev, "read %lld %4d-%02d-%02d (wday=%d) %2d:%02d:%02d\n", secs, |
| 43 | tm->tm_year, tm->tm_mon, tm->tm_mday, |
| 44 | tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int ds1672_set_time(struct udevice *dev, const struct rtc_time *tm) |
| 50 | { |
| 51 | time64_t secs = rtc_mktime(tm); |
| 52 | u8 regs[5]; |
| 53 | |
| 54 | dev_dbg(dev, "set %4d-%02d-%02d (wday=%d) %2d:%02d:%02d %lld\n", |
| 55 | tm->tm_year, tm->tm_mon, tm->tm_mday, |
| 56 | tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 57 | secs); |
| 58 | |
| 59 | if (tm->tm_year < 2000) { |
| 60 | dev_err(dev, "year %d (before 2000) not supported\n", |
| 61 | tm->tm_year); |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | |
| 65 | regs[0] = secs & 0x000000ff; |
| 66 | regs[1] = (secs & 0x0000ff00) >> 8; |
| 67 | regs[2] = (secs & 0x00ff0000) >> 16; |
| 68 | regs[3] = (secs & 0xff000000) >> 24; |
| 69 | regs[4] = 0; /* set control reg to enable counting */ |
| 70 | |
| 71 | return dm_i2c_write(dev, DS1672_REG_CNT_BASE, regs, 5); |
| 72 | } |
| 73 | |
| 74 | static int ds1672_reset(struct udevice *dev) |
| 75 | { |
| 76 | u8 regs[5] = { 0 }; |
| 77 | |
| 78 | return dm_i2c_write(dev, DS1672_REG_CNT_BASE, regs, 5); |
| 79 | } |
| 80 | |
| 81 | static int ds1672_read8(struct udevice *dev, unsigned int reg) |
| 82 | { |
| 83 | return dm_i2c_reg_read(dev, reg); |
| 84 | } |
| 85 | |
| 86 | static int ds1672_write8(struct udevice *dev, unsigned int reg, int val) |
| 87 | { |
| 88 | return dm_i2c_reg_write(dev, reg, val); |
| 89 | } |
| 90 | |
| 91 | static const struct rtc_ops ds1672_rtc_ops = { |
| 92 | .get = ds1672_read_time, |
| 93 | .set = ds1672_set_time, |
| 94 | .reset = ds1672_reset, |
| 95 | .read8 = ds1672_read8, |
| 96 | .write8 = ds1672_write8, |
| 97 | }; |
| 98 | |
| 99 | static int ds1672_probe(struct udevice *dev) |
| 100 | { |
| 101 | i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS | DM_I2C_CHIP_WR_ADDRESS); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static const struct udevice_id ds1672_of_id[] = { |
| 107 | { .compatible = "dallas,ds1672" }, |
| 108 | { } |
| 109 | }; |
| 110 | |
| 111 | U_BOOT_DRIVER(rtc_max313xx) = { |
| 112 | .name = "rtc-ds1672", |
| 113 | .id = UCLASS_RTC, |
| 114 | .probe = ds1672_probe, |
| 115 | .of_match = ds1672_of_id, |
| 116 | .ops = &ds1672_rtc_ops, |
| 117 | }; |