Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <i2c.h> |
| 10 | #include <rtc.h> |
| 11 | #include <asm/rtc.h> |
Simon Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 12 | #include <dm/acpi.h> |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 13 | |
| 14 | #define REG_COUNT 0x80 |
| 15 | |
| 16 | static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time) |
| 17 | { |
Rasmus Villemoes | acffbea | 2020-07-06 22:01:16 +0200 | [diff] [blame] | 18 | u8 buf[7]; |
| 19 | int ret; |
| 20 | |
| 21 | ret = dm_i2c_read(dev, REG_SEC, buf, sizeof(buf)); |
| 22 | if (ret < 0) |
| 23 | return ret; |
| 24 | |
| 25 | time->tm_sec = buf[REG_SEC - REG_SEC]; |
| 26 | time->tm_min = buf[REG_MIN - REG_SEC]; |
| 27 | time->tm_hour = buf[REG_HOUR - REG_SEC]; |
| 28 | time->tm_mday = buf[REG_MDAY - REG_SEC]; |
| 29 | time->tm_mon = buf[REG_MON - REG_SEC]; |
| 30 | time->tm_year = buf[REG_YEAR - REG_SEC] + 1900; |
| 31 | time->tm_wday = buf[REG_WDAY - REG_SEC]; |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time) |
| 37 | { |
Rasmus Villemoes | acffbea | 2020-07-06 22:01:16 +0200 | [diff] [blame] | 38 | u8 buf[7]; |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 39 | int ret; |
| 40 | |
Rasmus Villemoes | acffbea | 2020-07-06 22:01:16 +0200 | [diff] [blame] | 41 | buf[REG_SEC - REG_SEC] = time->tm_sec; |
| 42 | buf[REG_MIN - REG_SEC] = time->tm_min; |
| 43 | buf[REG_HOUR - REG_SEC] = time->tm_hour; |
| 44 | buf[REG_MDAY - REG_SEC] = time->tm_mday; |
| 45 | buf[REG_MON - REG_SEC] = time->tm_mon; |
| 46 | buf[REG_YEAR - REG_SEC] = time->tm_year - 1900; |
| 47 | buf[REG_WDAY - REG_SEC] = time->tm_wday; |
| 48 | |
| 49 | ret = dm_i2c_write(dev, REG_SEC, buf, sizeof(buf)); |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 50 | if (ret < 0) |
| 51 | return ret; |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int sandbox_rtc_reset(struct udevice *dev) |
| 57 | { |
| 58 | return dm_i2c_reg_write(dev, REG_RESET, 0); |
| 59 | } |
| 60 | |
| 61 | static int sandbox_rtc_read8(struct udevice *dev, unsigned int reg) |
| 62 | { |
| 63 | return dm_i2c_reg_read(dev, reg); |
| 64 | } |
| 65 | |
| 66 | static int sandbox_rtc_write8(struct udevice *dev, unsigned int reg, int val) |
| 67 | { |
| 68 | return dm_i2c_reg_write(dev, reg, val); |
| 69 | } |
| 70 | |
Simon Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 71 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 72 | static int sandbox_rtc_get_name(const struct udevice *dev, char *out_name) |
| 73 | { |
| 74 | return acpi_copy_name(out_name, "RTCC"); |
| 75 | } |
| 76 | |
| 77 | struct acpi_ops sandbox_rtc_acpi_ops = { |
| 78 | .get_name = sandbox_rtc_get_name, |
| 79 | }; |
| 80 | #endif |
| 81 | |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 82 | static const struct rtc_ops sandbox_rtc_ops = { |
| 83 | .get = sandbox_rtc_get, |
| 84 | .set = sandbox_rtc_set, |
| 85 | .reset = sandbox_rtc_reset, |
| 86 | .read8 = sandbox_rtc_read8, |
| 87 | .write8 = sandbox_rtc_write8, |
| 88 | }; |
| 89 | |
| 90 | static const struct udevice_id sandbox_rtc_ids[] = { |
| 91 | { .compatible = "sandbox-rtc" }, |
| 92 | { } |
| 93 | }; |
| 94 | |
Simon Glass | 4d4558e | 2020-10-03 11:31:36 -0600 | [diff] [blame] | 95 | U_BOOT_DRIVER(sandbox_rtc) = { |
| 96 | .name = "sandbox_rtc", |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 97 | .id = UCLASS_RTC, |
| 98 | .of_match = sandbox_rtc_ids, |
| 99 | .ops = &sandbox_rtc_ops, |
Simon Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 100 | ACPI_OPS_PTR(&sandbox_rtc_acpi_ops) |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 101 | }; |