blob: c676f0ff359a1bc8572a4c4214dfc625d4d4adfe [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdbeda5b2015-04-20 12:37:23 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassdbeda5b2015-04-20 12:37:23 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <rtc.h>
11
12int 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
22int 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
32int 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
42int 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
52int 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 Mengd24c7fb2017-03-16 07:26:27 -070062int 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
79int 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 Glassdbeda5b2015-04-20 12:37:23 -060092int 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 Glass9a4eb592015-10-18 15:55:31 -0600100 if (ret < 0)
Simon Glassdbeda5b2015-04-20 12:37:23 -0600101 return ret;
102 value |= ret << (i << 3);
103 }
104
105 *valuep = value;
106 return 0;
107}
108
109int 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
122UCLASS_DRIVER(rtc) = {
123 .name = "rtc",
124 .id = UCLASS_RTC,
125};