Simon Glass | dbeda5b | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2015 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 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 | d24c7fb | 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 | dbeda5b | 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 | 9a4eb59 | 2015-10-18 15:55:31 -0600 | [diff] [blame] | 101 | if (ret < 0) |
Simon Glass | dbeda5b | 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, |
| 126 | }; |