Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 11 | #include <rtc.h> |
| 12 | |
| 13 | int dm_rtc_get(struct udevice *dev, struct rtc_time *time) |
| 14 | { |
| 15 | struct rtc_ops *ops = rtc_get_ops(dev); |
| 16 | |
| 17 | assert(ops); |
| 18 | if (!ops->get) |
| 19 | return -ENOSYS; |
| 20 | return ops->get(dev, time); |
| 21 | } |
| 22 | |
| 23 | int dm_rtc_set(struct udevice *dev, struct rtc_time *time) |
| 24 | { |
| 25 | struct rtc_ops *ops = rtc_get_ops(dev); |
| 26 | |
| 27 | assert(ops); |
| 28 | if (!ops->set) |
| 29 | return -ENOSYS; |
| 30 | return ops->set(dev, time); |
| 31 | } |
| 32 | |
| 33 | int dm_rtc_reset(struct udevice *dev) |
| 34 | { |
| 35 | struct rtc_ops *ops = rtc_get_ops(dev); |
| 36 | |
| 37 | assert(ops); |
| 38 | if (!ops->reset) |
| 39 | return -ENOSYS; |
| 40 | return ops->reset(dev); |
| 41 | } |
| 42 | |
| 43 | int rtc_read8(struct udevice *dev, unsigned int reg) |
| 44 | { |
| 45 | struct rtc_ops *ops = rtc_get_ops(dev); |
| 46 | |
| 47 | assert(ops); |
| 48 | if (!ops->read8) |
| 49 | return -ENOSYS; |
| 50 | return ops->read8(dev, reg); |
| 51 | } |
| 52 | |
| 53 | int rtc_write8(struct udevice *dev, unsigned int reg, int val) |
| 54 | { |
| 55 | struct rtc_ops *ops = rtc_get_ops(dev); |
| 56 | |
| 57 | assert(ops); |
| 58 | if (!ops->write8) |
| 59 | return -ENOSYS; |
| 60 | return ops->write8(dev, reg, val); |
| 61 | } |
| 62 | |
Bin Meng | fbfe53d | 2017-03-16 07:26:27 -0700 | [diff] [blame] | 63 | int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep) |
| 64 | { |
| 65 | u16 value = 0; |
| 66 | int ret; |
| 67 | int i; |
| 68 | |
| 69 | for (i = 0; i < sizeof(value); i++) { |
| 70 | ret = rtc_read8(dev, reg + i); |
| 71 | if (ret < 0) |
| 72 | return ret; |
| 73 | value |= ret << (i << 3); |
| 74 | } |
| 75 | |
| 76 | *valuep = value; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int rtc_write16(struct udevice *dev, unsigned int reg, u16 value) |
| 81 | { |
| 82 | int i, ret; |
| 83 | |
| 84 | for (i = 0; i < sizeof(value); i++) { |
| 85 | ret = rtc_write8(dev, reg + i, (value >> (i << 3)) & 0xff); |
| 86 | if (ret) |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 93 | int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep) |
| 94 | { |
| 95 | u32 value = 0; |
| 96 | int ret; |
| 97 | int i; |
| 98 | |
| 99 | for (i = 0; i < sizeof(value); i++) { |
| 100 | ret = rtc_read8(dev, reg + i); |
Simon Glass | c60a5bc | 2015-10-18 15:55:31 -0600 | [diff] [blame] | 101 | if (ret < 0) |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 102 | return ret; |
| 103 | value |= ret << (i << 3); |
| 104 | } |
| 105 | |
| 106 | *valuep = value; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int rtc_write32(struct udevice *dev, unsigned int reg, u32 value) |
| 111 | { |
| 112 | int i, ret; |
| 113 | |
| 114 | for (i = 0; i < sizeof(value); i++) { |
| 115 | ret = rtc_write8(dev, reg + i, (value >> (i << 3)) & 0xff); |
| 116 | if (ret) |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | UCLASS_DRIVER(rtc) = { |
| 124 | .name = "rtc", |
| 125 | .id = UCLASS_RTC, |
Simon Glass | 302ea78 | 2018-11-18 08:14:35 -0700 | [diff] [blame] | 126 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | d9050b5 | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 127 | }; |