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