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 | |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <i2c.h> |
| 9 | #include <rtc.h> |
| 10 | #include <asm/rtc.h> |
Simon Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 11 | #include <dm/acpi.h> |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 12 | |
| 13 | #define REG_COUNT 0x80 |
| 14 | |
| 15 | static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time) |
| 16 | { |
Rasmus Villemoes | acffbea | 2020-07-06 22:01:16 +0200 | [diff] [blame] | 17 | u8 buf[7]; |
| 18 | int ret; |
| 19 | |
| 20 | ret = dm_i2c_read(dev, REG_SEC, buf, sizeof(buf)); |
| 21 | if (ret < 0) |
| 22 | return ret; |
| 23 | |
| 24 | time->tm_sec = buf[REG_SEC - REG_SEC]; |
| 25 | time->tm_min = buf[REG_MIN - REG_SEC]; |
| 26 | time->tm_hour = buf[REG_HOUR - REG_SEC]; |
| 27 | time->tm_mday = buf[REG_MDAY - REG_SEC]; |
| 28 | time->tm_mon = buf[REG_MON - REG_SEC]; |
| 29 | time->tm_year = buf[REG_YEAR - REG_SEC] + 1900; |
| 30 | time->tm_wday = buf[REG_WDAY - REG_SEC]; |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time) |
| 36 | { |
Rasmus Villemoes | acffbea | 2020-07-06 22:01:16 +0200 | [diff] [blame] | 37 | u8 buf[7]; |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 38 | int ret; |
| 39 | |
Rasmus Villemoes | acffbea | 2020-07-06 22:01:16 +0200 | [diff] [blame] | 40 | buf[REG_SEC - REG_SEC] = time->tm_sec; |
| 41 | buf[REG_MIN - REG_SEC] = time->tm_min; |
| 42 | buf[REG_HOUR - REG_SEC] = time->tm_hour; |
| 43 | buf[REG_MDAY - REG_SEC] = time->tm_mday; |
| 44 | buf[REG_MON - REG_SEC] = time->tm_mon; |
| 45 | buf[REG_YEAR - REG_SEC] = time->tm_year - 1900; |
| 46 | buf[REG_WDAY - REG_SEC] = time->tm_wday; |
| 47 | |
| 48 | ret = dm_i2c_write(dev, REG_SEC, buf, sizeof(buf)); |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 49 | if (ret < 0) |
| 50 | return ret; |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int sandbox_rtc_reset(struct udevice *dev) |
| 56 | { |
| 57 | return dm_i2c_reg_write(dev, REG_RESET, 0); |
| 58 | } |
| 59 | |
| 60 | static int sandbox_rtc_read8(struct udevice *dev, unsigned int reg) |
| 61 | { |
| 62 | return dm_i2c_reg_read(dev, reg); |
| 63 | } |
| 64 | |
| 65 | static int sandbox_rtc_write8(struct udevice *dev, unsigned int reg, int val) |
| 66 | { |
| 67 | return dm_i2c_reg_write(dev, reg, val); |
| 68 | } |
| 69 | |
Simon Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 70 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 71 | static int sandbox_rtc_get_name(const struct udevice *dev, char *out_name) |
| 72 | { |
| 73 | return acpi_copy_name(out_name, "RTCC"); |
| 74 | } |
| 75 | |
| 76 | struct acpi_ops sandbox_rtc_acpi_ops = { |
| 77 | .get_name = sandbox_rtc_get_name, |
| 78 | }; |
| 79 | #endif |
| 80 | |
Simon Glass | 65cb759 | 2021-03-15 17:25:30 +1300 | [diff] [blame] | 81 | static int sandbox_rtc_bind(struct udevice *dev) |
| 82 | { |
| 83 | #if CONFIG_IS_ENABLED(PLATDATA) |
| 84 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
| 85 | |
| 86 | /* Set up the emul_idx for i2c_emul_find() */ |
| 87 | i2c_emul_set_idx(dev, plat->dtplat.sandbox_emul->idx); |
| 88 | #endif |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 93 | static const struct rtc_ops sandbox_rtc_ops = { |
| 94 | .get = sandbox_rtc_get, |
| 95 | .set = sandbox_rtc_set, |
| 96 | .reset = sandbox_rtc_reset, |
| 97 | .read8 = sandbox_rtc_read8, |
| 98 | .write8 = sandbox_rtc_write8, |
| 99 | }; |
| 100 | |
| 101 | static const struct udevice_id sandbox_rtc_ids[] = { |
| 102 | { .compatible = "sandbox-rtc" }, |
| 103 | { } |
| 104 | }; |
| 105 | |
Simon Glass | 4d4558e | 2020-10-03 11:31:36 -0600 | [diff] [blame] | 106 | U_BOOT_DRIVER(sandbox_rtc) = { |
| 107 | .name = "sandbox_rtc", |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 108 | .id = UCLASS_RTC, |
| 109 | .of_match = sandbox_rtc_ids, |
| 110 | .ops = &sandbox_rtc_ops, |
Simon Glass | 65cb759 | 2021-03-15 17:25:30 +1300 | [diff] [blame] | 111 | .bind = sandbox_rtc_bind, |
Simon Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 112 | ACPI_OPS_PTR(&sandbox_rtc_acpi_ops) |
Simon Glass | c2a8c06 | 2015-04-20 12:37:25 -0600 | [diff] [blame] | 113 | }; |