summaryrefslogtreecommitdiff
path: root/drivers/rtc/rtc-uclass.c
blob: 8035f7fe9ccc5c3868cdf3c088a779359e56cb63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2015 Google, Inc
 * Written by Simon Glass <sjg@chromium.org>
 */

#include <common.h>
#include <dm.h>
#include <errno.h>
#include <log.h>
#include <rtc.h>

int dm_rtc_get(struct udevice *dev, struct rtc_time *time)
{
	struct rtc_ops *ops = rtc_get_ops(dev);

	assert(ops);
	if (!ops->get)
		return -ENOSYS;
	return ops->get(dev, time);
}

int dm_rtc_set(struct udevice *dev, struct rtc_time *time)
{
	struct rtc_ops *ops = rtc_get_ops(dev);

	assert(ops);
	if (!ops->set)
		return -ENOSYS;
	return ops->set(dev, time);
}

int dm_rtc_reset(struct udevice *dev)
{
	struct rtc_ops *ops = rtc_get_ops(dev);

	assert(ops);
	if (!ops->reset)
		return -ENOSYS;
	return ops->reset(dev);
}

int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf, unsigned int len)
{
	struct rtc_ops *ops = rtc_get_ops(dev);

	assert(ops);
	if (ops->read)
		return ops->read(dev, reg, buf, len);
	if (!ops->read8)
		return -ENOSYS;
	while (len--) {
		int ret = ops->read8(dev, reg++);

		if (ret < 0)
			return ret;
		*buf++ = ret;
	}
	return 0;
}

int dm_rtc_write(struct udevice *dev, unsigned int reg,
		 const u8 *buf, unsigned int len)
{
	struct rtc_ops *ops = rtc_get_ops(dev);

	assert(ops);
	if (ops->write)
		return ops->write(dev, reg, buf, len);
	if (!ops->write8)
		return -ENOSYS;
	while (len--) {
		int ret = ops->write8(dev, reg++, *buf++);

		if (ret < 0)
			return ret;
	}
	return 0;
}

int rtc_read8(struct udevice *dev, unsigned int reg)
{
	struct rtc_ops *ops = rtc_get_ops(dev);

	assert(ops);
	if (ops->read8)
		return ops->read8(dev, reg);
	if (ops->read) {
		u8 buf[1];
		int ret = ops->read(dev, reg, buf, 1);

		if (ret < 0)
			return ret;
		return buf[0];
	}
	return -ENOSYS;
}

int rtc_write8(struct udevice *dev, unsigned int reg, int val)
{
	struct rtc_ops *ops = rtc_get_ops(dev);

	assert(ops);
	if (ops->write8)
		return ops->write8(dev, reg, val);
	if (ops->write) {
		u8 buf[1] = { val };

		return ops->write(dev, reg, buf, 1);
	}
	return -ENOSYS;
}

int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep)
{
	u16 value = 0;
	int ret;
	int i;

	for (i = 0; i < sizeof(value); i++) {
		ret = rtc_read8(dev, reg + i);
		if (ret < 0)
			return ret;
		value |= ret << (i << 3);
	}

	*valuep = value;
	return 0;
}

int rtc_write16(struct udevice *dev, unsigned int reg, u16 value)
{
	int i, ret;

	for (i = 0; i < sizeof(value); i++) {
		ret = rtc_write8(dev, reg + i, (value >> (i << 3)) & 0xff);
		if (ret)
			return ret;
	}

	return 0;
}

int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep)
{
	u32 value = 0;
	int ret;
	int i;

	for (i = 0; i < sizeof(value); i++) {
		ret = rtc_read8(dev, reg + i);
		if (ret < 0)
			return ret;
		value |= ret << (i << 3);
	}

	*valuep = value;
	return 0;
}

int rtc_write32(struct udevice *dev, unsigned int reg, u32 value)
{
	int i, ret;

	for (i = 0; i < sizeof(value); i++) {
		ret = rtc_write8(dev, reg + i, (value >> (i << 3)) & 0xff);
		if (ret)
			return ret;
	}

	return 0;
}

UCLASS_DRIVER(rtc) = {
	.name		= "rtc",
	.id		= UCLASS_RTC,
	.post_bind	= dm_scan_fdt_dev,
};