summaryrefslogtreecommitdiff
path: root/drivers/rtc/rv3028.c
blob: 9f63afc14a8e53daab4eaf9789986713ebdbefa4 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// SPDX-License-Identifier: GPL-2.0+
/*
 * RTC driver for the Micro Crystal RV3028
 *
 * based on linux driver from
 * Copyright (C) 2019 Micro Crystal SA
 *
 * Alexandre Belloni <alexandre.belloni@bootlin.com>
 *
 */

#include <dm.h>
#include <i2c.h>
#include <rtc.h>

#define RV3028_SEC			0x00
#define RV3028_MIN			0x01
#define RV3028_HOUR			0x02
#define RV3028_WDAY			0x03
#define RV3028_DAY			0x04
#define RV3028_MONTH			0x05
#define RV3028_YEAR			0x06
#define RV3028_ALARM_MIN		0x07
#define RV3028_ALARM_HOUR		0x08
#define RV3028_ALARM_DAY		0x09
#define RV3028_STATUS			0x0E
#define RV3028_CTRL1			0x0F
#define RV3028_CTRL2			0x10
#define RV3028_EVT_CTRL			0x13
#define RV3028_TS_COUNT			0x14
#define RV3028_TS_SEC			0x15
#define RV3028_RAM1			0x1F
#define RV3028_EEPROM_ADDR		0x25
#define RV3028_EEPROM_DATA		0x26
#define RV3028_EEPROM_CMD		0x27
#define RV3028_CLKOUT			0x35
#define RV3028_OFFSET			0x36
#define RV3028_BACKUP			0x37

#define RV3028_STATUS_PORF		BIT(0)
#define RV3028_STATUS_EVF		BIT(1)
#define RV3028_STATUS_AF		BIT(2)
#define RV3028_STATUS_TF		BIT(3)
#define RV3028_STATUS_UF		BIT(4)
#define RV3028_STATUS_BSF		BIT(5)
#define RV3028_STATUS_CLKF		BIT(6)
#define RV3028_STATUS_EEBUSY		BIT(7)

#define RV3028_CLKOUT_FD_MASK		GENMASK(2, 0)
#define RV3028_CLKOUT_PORIE		BIT(3)
#define RV3028_CLKOUT_CLKSY		BIT(6)
#define RV3028_CLKOUT_CLKOE		BIT(7)

#define RV3028_CTRL1_EERD		BIT(3)
#define RV3028_CTRL1_WADA		BIT(5)

#define RV3028_CTRL2_RESET		BIT(0)
#define RV3028_CTRL2_12_24		BIT(1)
#define RV3028_CTRL2_EIE		BIT(2)
#define RV3028_CTRL2_AIE		BIT(3)
#define RV3028_CTRL2_TIE		BIT(4)
#define RV3028_CTRL2_UIE		BIT(5)
#define RV3028_CTRL2_TSE		BIT(7)

#define RV3028_EVT_CTRL_TSR		BIT(2)

#define RV3028_EEPROM_CMD_UPDATE	0x11
#define RV3028_EEPROM_CMD_WRITE		0x21
#define RV3028_EEPROM_CMD_READ		0x22

#define RV3028_EEBUSY_POLL		10000
#define RV3028_EEBUSY_TIMEOUT		100000

#define RV3028_BACKUP_TCE		BIT(5)
#define RV3028_BACKUP_TCR_MASK		GENMASK(1, 0)

#define OFFSET_STEP_PPT			953674

#define RTC_RV3028_LEN			7

static int rv3028_rtc_get(struct udevice *dev, struct rtc_time *tm)
{
	u8 regs[RTC_RV3028_LEN];
	u8 status;
	int ret;

	ret = dm_i2c_read(dev, RV3028_STATUS, &status, 1);
	if (ret < 0) {
		printf("%s: error reading RTC status: %x\n", __func__, ret);
		return -EIO;
	}

	if (status & RV3028_STATUS_PORF) {
		printf("Voltage low, data is invalid.\n");
		return -EINVAL;
	}

	ret = dm_i2c_read(dev, RV3028_SEC, regs, sizeof(regs));
	if (ret < 0) {
		printf("%s: error reading RTC: %x\n", __func__, ret);
		return -EIO;
	}

	tm->tm_sec = bcd2bin(regs[RV3028_SEC] & 0x7f);
	tm->tm_min = bcd2bin(regs[RV3028_MIN] & 0x7f);
	tm->tm_hour = bcd2bin(regs[RV3028_HOUR] & 0x3f);
	tm->tm_wday = regs[RV3028_WDAY] & 0x7;
	tm->tm_mday = bcd2bin(regs[RV3028_DAY] & 0x3f);
	tm->tm_mon  = bcd2bin(regs[RV3028_MONTH] & 0x1f);
	tm->tm_year = bcd2bin(regs[RV3028_YEAR]) + 2000;
	tm->tm_yday = 0;
	tm->tm_isdst = 0;

	debug("%s: %4d-%02d-%02d (wday=%d) %2d:%02d:%02d\n",
	      __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
	      tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);

	return 0;
}

static int rv3028_rtc_set(struct udevice *dev, const struct rtc_time *tm)
{
	u8 regs[RTC_RV3028_LEN];
	u8 status;
	int ret;

	debug("%s: %4d-%02d-%02d (wday=%d( %2d:%02d:%02d\n",
	      __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
	      tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);

	if (tm->tm_year < 2000) {
		printf("%s: year %d (before 2000) not supported\n",
		       __func__, tm->tm_year);
		return -EINVAL;
	}

	regs[RV3028_SEC] = bin2bcd(tm->tm_sec);
	regs[RV3028_MIN] = bin2bcd(tm->tm_min);
	regs[RV3028_HOUR] = bin2bcd(tm->tm_hour);
	regs[RV3028_WDAY]  = tm->tm_wday;
	regs[RV3028_DAY]   = bin2bcd(tm->tm_mday);
	regs[RV3028_MONTH] = bin2bcd(tm->tm_mon);
	regs[RV3028_YEAR]  = bin2bcd(tm->tm_year - 2000);

	ret = dm_i2c_write(dev, RV3028_SEC, regs, sizeof(regs));
	if (ret) {
		printf("%s: set rtc error: %d\n", __func__, ret);
		return ret;
	}

	ret = dm_i2c_read(dev, RV3028_STATUS, &status, 1);
	if (ret < 0) {
		printf("%s: error reading RTC status: %x\n", __func__, ret);
		return -EIO;
	}
	status |= RV3028_STATUS_PORF;
	return dm_i2c_write(dev, RV3028_STATUS, &status, 1);
}

static int rv3028_rtc_reset(struct udevice *dev)
{
	return 0;
}

static int rv3028_rtc_read8(struct udevice *dev, unsigned int reg)
{
	u8 data;
	int ret;

	ret = dm_i2c_read(dev, reg, &data, sizeof(data));
	return ret < 0 ? ret : data;
}

static int rv3028_rtc_write8(struct udevice *dev, unsigned int reg, int val)
{
	u8 data = val;

	return dm_i2c_write(dev, reg, &data, 1);
}

static int rv3028_probe(struct udevice *dev)
{
	i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
				DM_I2C_CHIP_WR_ADDRESS);

	return 0;
}

static const struct rtc_ops rv3028_rtc_ops = {
	.get = rv3028_rtc_get,
	.set = rv3028_rtc_set,
	.read8 = rv3028_rtc_read8,
	.write8 = rv3028_rtc_write8,
	.reset = rv3028_rtc_reset,
};

static const struct udevice_id rv3028_rtc_ids[] = {
	{ .compatible = "microcrystal,rv3028" },
	{ }
};

U_BOOT_DRIVER(rtc_rv3028) = {
	.name	= "rtc-rv3028",
	.id	= UCLASS_RTC,
	.probe	= rv3028_probe,
	.of_match = rv3028_rtc_ids,
	.ops	= &rv3028_rtc_ops,
};