summaryrefslogtreecommitdiff
path: root/drivers/rtc/rx8010sj.c
blob: 82c5185e2e5e2edb09b1beb8a3b1a454b5c7e43f (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Epson RX8010 RTC driver.
 *
 * Copyright (c) 2017, General Electric Company
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

/*---------------------------------------------------------------------*/
/* #undef DEBUG_RTC */

#ifdef DEBUG_RTC
#define DEBUGR(fmt, args...) printf(fmt, ##args)
#else
#define DEBUGR(fmt, args...)
#endif
/*---------------------------------------------------------------------*/

#ifndef CONFIG_SYS_I2C_RTC_ADDR
# define CONFIG_SYS_I2C_RTC_ADDR	0x32
#endif

/*
 * RTC register addresses
 */
#define RX8010_SEC     0x10
#define RX8010_MIN     0x11
#define RX8010_HOUR    0x12
#define RX8010_WDAY    0x13
#define RX8010_MDAY    0x14
#define RX8010_MONTH   0x15
#define RX8010_YEAR    0x16
#define RX8010_YEAR    0x16
#define RX8010_RESV17  0x17
#define RX8010_ALMIN   0x18
#define RX8010_ALHOUR  0x19
#define RX8010_ALWDAY  0x1A
#define RX8010_TCOUNT0 0x1B
#define RX8010_TCOUNT1 0x1C
#define RX8010_EXT     0x1D
#define RX8010_FLAG    0x1E
#define RX8010_CTRL    0x1F
/* 0x20 to 0x2F are user registers */
#define RX8010_RESV30  0x30
#define RX8010_RESV31  0x32
#define RX8010_IRQ     0x32

#define RX8010_EXT_WADA  BIT(3)

#define RX8010_FLAG_VLF  BIT(1)
#define RX8010_FLAG_AF   BIT(3)
#define RX8010_FLAG_TF   BIT(4)
#define RX8010_FLAG_UF   BIT(5)

#define RX8010_CTRL_AIE  BIT(3)
#define RX8010_CTRL_UIE  BIT(5)
#define RX8010_CTRL_STOP BIT(6)
#define RX8010_CTRL_TEST BIT(7)

#define RX8010_ALARM_AE  BIT(7)

#ifdef CONFIG_DM_RTC

#define DEV_TYPE struct udevice

#else

/* Local udevice */
struct ludevice {
	u8 chip;
};

#define DEV_TYPE struct ludevice

#endif

static int rx8010sj_rtc_read8(DEV_TYPE *dev, unsigned int reg)
{
	u8 val;
	int ret;

#ifdef CONFIG_DM_RTC
	ret = dm_i2c_read(dev, reg, &val, sizeof(val));
#else
	ret = i2c_read(dev->chip, reg, 1, &val, 1);
#endif

	return ret < 0 ? ret : val;
}

static int rx8010sj_rtc_write8(DEV_TYPE *dev, unsigned int reg, int val)
{
	int ret;
	u8 lval = val;

#ifdef CONFIG_DM_RTC
	ret = dm_i2c_write(dev, reg, &lval, 1);
#else
	ret = i2c_write(dev->chip, reg, 1, &lval, 1);
#endif

	return ret < 0 ? ret : 0;
}

static int validate_time(const struct rtc_time *tm)
{
	if ((tm->tm_year < 2000) || (tm->tm_year > 2099))
		return -EINVAL;

	if ((tm->tm_mon < 1) || (tm->tm_mon > 12))
		return -EINVAL;

	if ((tm->tm_mday < 1) || (tm->tm_mday > 31))
		return -EINVAL;

	if ((tm->tm_wday < 0) || (tm->tm_wday > 6))
		return -EINVAL;

	if ((tm->tm_hour < 0) || (tm->tm_hour > 23))
		return -EINVAL;

	if ((tm->tm_min < 0) || (tm->tm_min > 59))
		return -EINVAL;

	if ((tm->tm_sec < 0) || (tm->tm_sec > 59))
		return -EINVAL;

	return 0;
}

void rx8010sj_rtc_init(DEV_TYPE *dev)
{
	u8 ctrl[2];
	int need_clear = 0, ret = 0;

	/* Initialize reserved registers as specified in datasheet */
	ret = rx8010sj_rtc_write8(dev, RX8010_RESV17, 0xD8);
	if (ret < 0)
		goto error;

	ret = rx8010sj_rtc_write8(dev, RX8010_RESV30, 0x00);
	if (ret < 0)
		goto error;

	ret = rx8010sj_rtc_write8(dev, RX8010_RESV31, 0x08);
	if (ret < 0)
		goto error;

	ret = rx8010sj_rtc_write8(dev, RX8010_IRQ, 0x00);
	if (ret < 0)
		goto error;

	for (int i = 0; i < 2; i++) {
		ret = rx8010sj_rtc_read8(dev, RX8010_FLAG + i);
		if (ret < 0)
			goto error;

		ctrl[i] = ret;
	}

	if (ctrl[0] & RX8010_FLAG_VLF)
		printf("RTC low voltage detected\n");

	if (ctrl[0] & RX8010_FLAG_AF) {
		printf("Alarm was detected\n");
		need_clear = 1;
	}

	if (ctrl[0] & RX8010_FLAG_TF)
		need_clear = 1;

	if (ctrl[0] & RX8010_FLAG_UF)
		need_clear = 1;

	if (need_clear) {
		ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
		ret = rx8010sj_rtc_write8(dev, RX8010_FLAG, ctrl[0]);
		if (ret < 0)
			goto error;
	}

	return;

error:
	printf("Error rtc init.\n");
}

/* Get the current time from the RTC */
static int rx8010sj_rtc_get(DEV_TYPE *dev, struct rtc_time *tmp)
{
	u8 date[7];
	int flagreg;
	int ret;

	flagreg = rx8010sj_rtc_read8(dev, RX8010_FLAG);
	if (flagreg < 0) {
		DEBUGR("Error reading from RTC. err: %d\n", flagreg);
		return -EIO;
	}

	if (flagreg & RX8010_FLAG_VLF) {
		DEBUGR("RTC low voltage detected\n");
		return -EINVAL;
	}

	for (int i = 0; i < 7; i++) {
		ret = rx8010sj_rtc_read8(dev, RX8010_SEC + i);
		if (ret < 0) {
			DEBUGR("Error reading from RTC. err: %d\n", ret);
			return -EIO;
		}
		date[i] = ret;
	}

	tmp->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
	tmp->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
	tmp->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
	tmp->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
	tmp->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f);
	tmp->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 2000;
	tmp->tm_wday = 0;
	tmp->tm_yday = 0;
	tmp->tm_isdst = 0;

	DEBUGR("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
	       tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
	       tmp->tm_hour, tmp->tm_min, tmp->tm_sec);

	return 0;
}

/* Set the RTC */
static int rx8010sj_rtc_set(DEV_TYPE *dev, const struct rtc_time *tm)
{
	u8 date[7];
	int ctrl, flagreg;
	int ret;

	ret = validate_time(tm);
	if (ret < 0)
		return -EINVAL;

	/* set STOP bit before changing clock/calendar */
	ctrl = rx8010sj_rtc_read8(dev, RX8010_CTRL);
	if (ctrl < 0)
		return ctrl;
	ret = rx8010sj_rtc_write8(dev, RX8010_CTRL, ctrl | RX8010_CTRL_STOP);
	if (ret < 0)
		return ret;

	date[RX8010_SEC - RX8010_SEC] = bin2bcd(tm->tm_sec);
	date[RX8010_MIN - RX8010_SEC] = bin2bcd(tm->tm_min);
	date[RX8010_HOUR - RX8010_SEC] = bin2bcd(tm->tm_hour);
	date[RX8010_MDAY - RX8010_SEC] = bin2bcd(tm->tm_mday);
	date[RX8010_MONTH - RX8010_SEC] = bin2bcd(tm->tm_mon);
	date[RX8010_YEAR - RX8010_SEC] = bin2bcd(tm->tm_year - 2000);
	date[RX8010_WDAY - RX8010_SEC] = bin2bcd(tm->tm_wday);

	for (int i = 0; i < 7; i++) {
		ret = rx8010sj_rtc_write8(dev, RX8010_SEC + i, date[i]);
		if (ret < 0) {
			DEBUGR("Error writing to RTC. err: %d\n", ret);
			return -EIO;
		}
	}

	/* clear STOP bit after changing clock/calendar */
	ctrl = rx8010sj_rtc_read8(dev, RX8010_CTRL);
	if (ctrl < 0)
		return ctrl;

	ret = rx8010sj_rtc_write8(dev, RX8010_CTRL, ctrl & ~RX8010_CTRL_STOP);
	if (ret < 0)
		return ret;

	flagreg = rx8010sj_rtc_read8(dev, RX8010_FLAG);
	if (flagreg < 0)
		return flagreg;

	if (flagreg & RX8010_FLAG_VLF)
		ret = rx8010sj_rtc_write8(dev, RX8010_FLAG,
					  flagreg & ~RX8010_FLAG_VLF);

	return 0;
}

/* Reset the RTC. */
static int rx8010sj_rtc_reset(DEV_TYPE *dev)
{
	/* Not needed */
	return 0;
}

#ifndef CONFIG_DM_RTC

int rtc_get(struct rtc_time *tm)
{
	struct ludevice dev = {
			.chip = CONFIG_SYS_I2C_RTC_ADDR,
	};

	return rx8010sj_rtc_get(&dev, tm);
}

int rtc_set(struct rtc_time *tm)
{
	struct ludevice dev = {
			.chip = CONFIG_SYS_I2C_RTC_ADDR,
	};

	return rx8010sj_rtc_set(&dev, tm);
}

void rtc_reset(void)
{
	struct ludevice dev = {
			.chip = CONFIG_SYS_I2C_RTC_ADDR,
	};

	rx8010sj_rtc_reset(&dev);
}

void rtc_init(void)
{
	struct ludevice dev = {
			.chip = CONFIG_SYS_I2C_RTC_ADDR,
	};

	rx8010sj_rtc_init(&dev);
}

#else

static int rx8010sj_probe(struct udevice *dev)
{
	rx8010sj_rtc_init(dev);

	return 0;
}

static const struct rtc_ops rx8010sj_rtc_ops = {
	.get = rx8010sj_rtc_get,
	.set = rx8010sj_rtc_set,
	.read8 = rx8010sj_rtc_read8,
	.write8 = rx8010sj_rtc_write8,
	.reset = rx8010sj_rtc_reset,
};

static const struct udevice_id rx8010sj_rtc_ids[] = {
	{ .compatible = "epson,rx8010sj-rtc" },
	{ .compatible = "epson,rx8010" },
	{ }
};

U_BOOT_DRIVER(rx8010sj_rtc) = {
	.name	  = "rx8010sj_rtc",
	.id	      = UCLASS_RTC,
	.probe    = rx8010sj_probe,
	.of_match = rx8010sj_rtc_ids,
	.ops	  = &rx8010sj_rtc_ops,
};

#endif