summaryrefslogtreecommitdiff
path: root/sys/dev/i2c/r2025.c
blob: ebe83b96b3cf091da68d9f1d7a73df70d4991f14 (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
/* $NetBSD: r2025.c,v 1.10 2020/01/02 19:00:34 thorpej Exp $ */

/*-
 * Copyright (c) 2006 Shigeyuki Fukushima.
 * All rights reserved.
 *
 * Written by Shigeyuki Fukushima.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: r2025.c,v 1.10 2020/01/02 19:00:34 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/fcntl.h>
#include <sys/uio.h>
#include <sys/conf.h>
#include <sys/event.h>

#include <dev/clock_subr.h>

#include <dev/i2c/i2cvar.h>
#include <dev/i2c/r2025reg.h>

struct r2025rtc_softc {
	device_t		sc_dev;
	i2c_tag_t		sc_tag;
	int			sc_address;
	int			sc_open;
	struct todr_chip_handle	sc_todr;
};

static void	r2025rtc_attach(device_t, device_t, void *);
static int	r2025rtc_match(device_t, cfdata_t, void *);

CFATTACH_DECL_NEW(r2025rtc, sizeof(struct r2025rtc_softc),
	r2025rtc_match, r2025rtc_attach, NULL, NULL);

static int	r2025rtc_gettime_ymdhms(struct todr_chip_handle *,
					struct clock_ymdhms *);
static int	r2025rtc_settime_ymdhms(struct todr_chip_handle *,
					struct clock_ymdhms *);
static int	r2025rtc_reg_write(struct r2025rtc_softc *, int, uint8_t*, int);
static int	r2025rtc_reg_read(struct r2025rtc_softc *, int, uint8_t*, int);


static int
r2025rtc_match(device_t parent, cfdata_t cf, void *arg)
{
	struct i2c_attach_args *ia = arg;

	/* match only R2025 RTC devices */
	if (ia->ia_addr == R2025_ADDR)
		return I2C_MATCH_ADDRESS_ONLY;

	return 0;
}

static void
r2025rtc_attach(device_t parent, device_t self, void *arg)
{
	struct r2025rtc_softc *sc = device_private(self);
	struct i2c_attach_args *ia = arg;

	aprint_normal(": RICOH R2025S/D Real-time Clock\n");

	sc->sc_tag = ia->ia_tag;
	sc->sc_address = ia->ia_addr;
	sc->sc_dev = self;
	sc->sc_open = 0;
	sc->sc_todr.cookie = sc;
	sc->sc_todr.todr_gettime = NULL;
	sc->sc_todr.todr_settime = NULL;
	sc->sc_todr.todr_gettime_ymdhms = r2025rtc_gettime_ymdhms;
	sc->sc_todr.todr_settime_ymdhms = r2025rtc_settime_ymdhms;
	sc->sc_todr.todr_setwen = NULL;

	todr_attach(&sc->sc_todr);
}

static int
r2025rtc_gettime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
{
	struct r2025rtc_softc *sc = ch->cookie;
	uint8_t rctrl;
	uint8_t bcd[R2025_CLK_SIZE];
	int hour;
	int error;

	memset(dt, 0, sizeof(*dt));

	if ((error = r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) {
		aprint_error_dev(sc->sc_dev,
		    "r2025rtc_gettime: failed to read registers.\n");
		return error;
	}

	if ((error = r2025rtc_reg_read(sc, R2025_REG_SEC, &bcd[0],
				       R2025_CLK_SIZE)) != 0) {
		aprint_error_dev(sc->sc_dev,
		    "r2025rtc_gettime: failed to read registers.\n");
		return error;
	}

	dt->dt_sec = bcdtobin(bcd[R2025_REG_SEC] & R2025_REG_SEC_MASK);
	dt->dt_min = bcdtobin(bcd[R2025_REG_MIN] & R2025_REG_MIN_MASK);
	hour = bcdtobin(bcd[R2025_REG_HOUR] & R2025_REG_HOUR_MASK);
	if (rctrl & R2025_REG_CTRL1_H1224) {
		dt->dt_hour = hour;
	} else {
		if (hour == 12) {
			dt->dt_hour = 0;
		} else if (hour == 32) {
			dt->dt_hour = 12;
		} else if (hour > 13) {
			dt->dt_hour = (hour - 8);
		} else { /* (hour < 12) */
			dt->dt_hour = hour;
		}
	}
	dt->dt_wday = bcdtobin(bcd[R2025_REG_WDAY] & R2025_REG_WDAY_MASK);
	dt->dt_day = bcdtobin(bcd[R2025_REG_DAY] & R2025_REG_DAY_MASK);
	dt->dt_mon = bcdtobin(bcd[R2025_REG_MON] & R2025_REG_MON_MASK);
	dt->dt_year = bcdtobin(bcd[R2025_REG_YEAR] & R2025_REG_YEAR_MASK)
		+ ((bcd[R2025_REG_MON] & R2025_REG_MON_Y1920) ? 2000 : 1900);

	return 0;
}

static int
r2025rtc_settime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
{
	struct r2025rtc_softc *sc = ch->cookie;
	uint8_t rctrl;
	uint8_t bcd[R2025_CLK_SIZE];
	int error;

	/* Y3K problem */
	if (dt->dt_year >= 3000) {
		aprint_error_dev(sc->sc_dev,
		    "r2025rtc_settime: "
		    "RTC does not support year 3000 or over.\n");
		return EINVAL;
	}

	if ((error = r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) {
		aprint_error_dev(sc->sc_dev,
		    "r2025rtc_settime: failed to read register.\n");
		return error;
	}
	rctrl |= R2025_REG_CTRL1_H1224;

	/* setup registers 0x00-0x06 (7 byte) */
	bcd[R2025_REG_SEC] = bintobcd(dt->dt_sec) & R2025_REG_SEC_MASK;
	bcd[R2025_REG_MIN] = bintobcd(dt->dt_min) & R2025_REG_MIN_MASK;
	bcd[R2025_REG_HOUR] = bintobcd(dt->dt_hour) & R2025_REG_HOUR_MASK;
	bcd[R2025_REG_WDAY] = bintobcd(dt->dt_wday) & R2025_REG_WDAY_MASK;
	bcd[R2025_REG_DAY] = bintobcd(dt->dt_day) & R2025_REG_DAY_MASK;
	bcd[R2025_REG_MON] = (bintobcd(dt->dt_mon) & R2025_REG_MON_MASK)
		| ((dt->dt_year >= 2000) ? R2025_REG_MON_Y1920 : 0);
	bcd[R2025_REG_YEAR] = bintobcd(dt->dt_year % 100) & R2025_REG_YEAR_MASK;

	/* Write RTC register */
	if ((error = r2025rtc_reg_write(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) {
		aprint_error_dev(sc->sc_dev,
		    "r2025rtc_settime: failed to write registers.\n");
		return error;
	}
	if ((error = r2025rtc_reg_write(sc, R2025_REG_SEC, bcd,
					R2025_CLK_SIZE)) != 0) {
		aprint_error_dev(sc->sc_dev,
		    "r2025rtc_settime: failed to write registers.\n");
		return error;
	}

	return 0;
}

static int
r2025rtc_reg_write(struct r2025rtc_softc *sc, int reg, uint8_t *val, int len)
{
	int i;
	uint8_t buf[1];
	uint8_t cmdbuf[1];
	int error;

	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
		aprint_error_dev(sc->sc_dev,
		    "r2025rtc_reg_write: failed to acquire I2C bus\n");
		return error;
	}

	for (i = 0 ; i < len ; i++) {
		cmdbuf[0] = (((reg + i) << 4) & 0xf0);
		buf[0] = val[i];
		if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
				      sc->sc_address, cmdbuf, 1, buf, 1,
				      0)) != 0) {
			iic_release_bus(sc->sc_tag, 0);
			aprint_error_dev(sc->sc_dev, "r2025rtc_reg_write: "
				"failed to write registers\n");
			return error;
		}
	}

	iic_release_bus(sc->sc_tag, 0);

	return 0;
}

static int
r2025rtc_reg_read(struct r2025rtc_softc *sc, int reg, uint8_t *val, int len)
{
	int i;
	uint8_t buf[1];
	uint8_t cmdbuf[1];
	int error;

	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
		aprint_error_dev(sc->sc_dev,
		    "r2025rtc_reg_read: failed to acquire I2C bus\n");
		return error;
	}

	for (i = 0 ; i < len ; i++) {
		cmdbuf[0] = (((reg + i) << 4) & 0xf0);
		buf[0] = 0;
		if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
				      sc->sc_address, cmdbuf, 1, buf, 1,
				      0)) != 0) {
			iic_release_bus(sc->sc_tag, 0);
			aprint_error_dev(sc->sc_dev, "r2025rtc_reg_read: "
				"failed to read registers\n");
			return error;
		}

		*(val + i) = buf[0];
	}

	iic_release_bus(sc->sc_tag, 0);

	return 0;
}