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 | |
| 63 | int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep) |
| 64 | { |
| 65 | u32 value = 0; |
| 66 | int ret; |
| 67 | int i; |
| 68 | |
| 69 | for (i = 0; i < sizeof(value); i++) { |
| 70 | ret = rtc_read8(dev, reg + i); |
Simon Glass | 9a4eb59 | 2015-10-18 15:55:31 -0600 | [diff] [blame] | 71 | if (ret < 0) |
Simon Glass | dbeda5b | 2015-04-20 12:37:23 -0600 | [diff] [blame] | 72 | return ret; |
| 73 | value |= ret << (i << 3); |
| 74 | } |
| 75 | |
| 76 | *valuep = value; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int rtc_write32(struct udevice *dev, unsigned int reg, u32 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 | |
| 93 | UCLASS_DRIVER(rtc) = { |
| 94 | .name = "rtc", |
| 95 | .id = UCLASS_RTC, |
| 96 | }; |