blob: 926cca234e849bea544c111d0ae3b4e89320587e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd9050b52015-04-20 12:37:23 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassd9050b52015-04-20 12:37:23 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glassd9050b52015-04-20 12:37:23 -060011#include <rtc.h>
12
13int 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
23int 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
33int 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
43int 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
53int 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 Mengfbfe53d2017-03-16 07:26:27 -070063int 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
80int 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 Glassd9050b52015-04-20 12:37:23 -060093int 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 Glassc60a5bc2015-10-18 15:55:31 -0600101 if (ret < 0)
Simon Glassd9050b52015-04-20 12:37:23 -0600102 return ret;
103 value |= ret << (i << 3);
104 }
105
106 *valuep = value;
107 return 0;
108}
109
110int 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
123UCLASS_DRIVER(rtc) = {
124 .name = "rtc",
125 .id = UCLASS_RTC,
Simon Glass302ea782018-11-18 08:14:35 -0700126 .post_bind = dm_scan_fdt_dev,
Simon Glassd9050b52015-04-20 12:37:23 -0600127};