xref: /netbsd-src/sys/dev/i2c/r2025.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /* $NetBSD: r2025.c,v 1.4 2008/04/06 20:25:59 cegger Exp $ */
2 
3 /*-
4  * Copyright (c) 2006 Shigeyuki Fukushima.
5  * All rights reserved.
6  *
7  * Written by Shigeyuki Fukushima.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above
15  *    copyright notice, this list of conditions and the following
16  *    disclaimer in the documentation and/or other materials provided
17  *    with the distribution.
18  * 3. The name of the author may not be used to endorse or promote
19  *    products derived from this software without specific prior
20  *    written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: r2025.c,v 1.4 2008/04/06 20:25:59 cegger Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/kernel.h>
42 #include <sys/fcntl.h>
43 #include <sys/uio.h>
44 #include <sys/conf.h>
45 #include <sys/event.h>
46 
47 #include <dev/clock_subr.h>
48 
49 #include <dev/i2c/i2cvar.h>
50 #include <dev/i2c/r2025reg.h>
51 
52 struct r2025rtc_softc {
53 	struct device		sc_dev;
54 	i2c_tag_t		sc_tag;
55 	int			sc_address;
56 	int			sc_open;
57 	struct todr_chip_handle	sc_todr;
58 };
59 
60 static void	r2025rtc_attach(struct device *, struct device *, void *);
61 static int	r2025rtc_match(struct device *, struct cfdata *, void *);
62 
63 CFATTACH_DECL(r2025rtc, sizeof(struct r2025rtc_softc),
64 	r2025rtc_match, r2025rtc_attach, NULL, NULL);
65 
66 static int	r2025rtc_gettime(struct todr_chip_handle *,
67 				volatile struct timeval *);
68 static int	r2025rtc_settime(struct todr_chip_handle *,
69 				volatile struct timeval *);
70 static int	r2025rtc_reg_write(struct r2025rtc_softc *, int, uint8_t*, int);
71 static int	r2025rtc_reg_read(struct r2025rtc_softc *, int, uint8_t*, int);
72 
73 
74 static int
75 r2025rtc_match(struct device *parent, struct cfdata *cf, void *arg)
76 {
77 	struct i2c_attach_args *ia = arg;
78 
79 	/* match only R2025 RTC devices */
80 	if (ia->ia_addr == R2025_ADDR)
81 		return 1;
82 
83 	return 0;
84 }
85 
86 static void
87 r2025rtc_attach(struct device *parent, struct device *self, void *arg)
88 {
89 	struct r2025rtc_softc *sc = device_private(self);
90 	struct i2c_attach_args *ia = arg;
91 
92 	aprint_normal(": RICOH R2025S/D Real-time Clock\n");
93 
94 	sc->sc_tag = ia->ia_tag;
95 	sc->sc_address = ia->ia_addr;
96 	sc->sc_open = 0;
97 	sc->sc_todr.cookie = sc;
98 	sc->sc_todr.todr_gettime = r2025rtc_gettime;
99 	sc->sc_todr.todr_settime = r2025rtc_settime;
100 	sc->sc_todr.todr_setwen = NULL;
101 
102 	todr_attach(&sc->sc_todr);
103 }
104 
105 static int
106 r2025rtc_gettime(struct todr_chip_handle *ch, volatile struct timeval *tv)
107 {
108 	struct r2025rtc_softc *sc = ch->cookie;
109 	struct clock_ymdhms dt;
110 	uint8_t rctrl;
111 	uint8_t bcd[R2025_CLK_SIZE];
112 	int hour;
113 
114 	memset(&dt, 0, sizeof(dt));
115 
116 	if (r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1) != 0) {
117 		aprint_error_dev(&sc->sc_dev, "r2025rtc_gettime: failed to read registers.\n");
118 		return -1;
119 	}
120 
121 	if (r2025rtc_reg_read(sc, R2025_REG_SEC, &bcd[0], R2025_CLK_SIZE)
122 		!= 0) {
123 		aprint_error_dev(&sc->sc_dev, "r2025rtc_gettime: failed to read registers.\n");
124 		return -1;
125 	}
126 
127 	dt.dt_sec = FROMBCD(bcd[R2025_REG_SEC] & R2025_REG_SEC_MASK);
128 	dt.dt_min = FROMBCD(bcd[R2025_REG_MIN] & R2025_REG_MIN_MASK);
129 	hour = FROMBCD(bcd[R2025_REG_HOUR] & R2025_REG_HOUR_MASK);
130 	if (rctrl & R2025_REG_CTRL1_H1224) {
131 		dt.dt_hour = hour;
132 	} else {
133 		if (hour == 12) {
134 			dt.dt_hour = 0;
135 		} else if (hour == 32) {
136 			dt.dt_hour = 12;
137 		} else if (hour > 13) {
138 			dt.dt_hour = (hour - 8);
139 		} else { /* (hour < 12) */
140 			dt.dt_hour = hour;
141 		}
142 	}
143 	dt.dt_wday = FROMBCD(bcd[R2025_REG_WDAY] & R2025_REG_WDAY_MASK);
144 	dt.dt_day = FROMBCD(bcd[R2025_REG_DAY] & R2025_REG_DAY_MASK);
145 	dt.dt_mon = FROMBCD(bcd[R2025_REG_MON] & R2025_REG_MON_MASK);
146 	dt.dt_year = FROMBCD(bcd[R2025_REG_YEAR] & R2025_REG_YEAR_MASK)
147 		+ ((bcd[R2025_REG_MON] & R2025_REG_MON_Y1920) ? 2000 : 1900);
148 
149 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
150 	tv->tv_usec = 0;
151 
152 	return 0;
153 }
154 
155 static int
156 r2025rtc_settime(struct todr_chip_handle *ch, volatile struct timeval *tv)
157 {
158 	struct r2025rtc_softc *sc = ch->cookie;
159 	struct clock_ymdhms dt;
160 	uint8_t rctrl;
161 	uint8_t bcd[R2025_CLK_SIZE];
162 
163 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
164 
165 	/* Y3K problem */
166 	if (dt.dt_year >= 3000) {
167 		aprint_error_dev(&sc->sc_dev, "r2025rtc_settime: "
168 			"RTC does not support year 3000 or over.\n");
169 		return -1;
170 	}
171 
172 	if (r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1) != 0) {
173 		aprint_error_dev(&sc->sc_dev, "r2025rtc_settime: failed to read register.\n");
174 		return -1;
175 	}
176 	rctrl |= R2025_REG_CTRL1_H1224;
177 
178 	/* setup registers 0x00-0x06 (7 byte) */
179 	bcd[R2025_REG_SEC] = TOBCD(dt.dt_sec) & R2025_REG_SEC_MASK;
180 	bcd[R2025_REG_MIN] = TOBCD(dt.dt_min) & R2025_REG_MIN_MASK;
181 	bcd[R2025_REG_HOUR] = TOBCD(dt.dt_hour) & R2025_REG_HOUR_MASK;
182 	bcd[R2025_REG_WDAY] = TOBCD(dt.dt_wday) & R2025_REG_WDAY_MASK;
183 	bcd[R2025_REG_DAY] = TOBCD(dt.dt_day) & R2025_REG_DAY_MASK;
184 	bcd[R2025_REG_MON] = (TOBCD(dt.dt_mon) & R2025_REG_MON_MASK)
185 		| ((dt.dt_year >= 2000) ? R2025_REG_MON_Y1920 : 0);
186 	bcd[R2025_REG_YEAR] = TOBCD(dt.dt_year % 100) & R2025_REG_YEAR_MASK;
187 
188 	/* Write RTC register */
189 	if (r2025rtc_reg_write(sc, R2025_REG_CTRL1, &rctrl, 1) != 0) {
190 		aprint_error_dev(&sc->sc_dev, "r2025rtc_settime: failed to write registers.\n");
191 		return -1;
192 	}
193 	if (r2025rtc_reg_write(sc, R2025_REG_SEC, bcd, R2025_CLK_SIZE) != 0) {
194 		aprint_error_dev(&sc->sc_dev, "r2025rtc_settime: failed to write registers.\n");
195 		return -1;
196 	}
197 
198 	return 0;
199 }
200 
201 static int
202 r2025rtc_reg_write(struct r2025rtc_softc *sc, int reg, uint8_t *val, int len)
203 {
204 	int i;
205 	uint8_t buf[1];
206 	uint8_t cmdbuf[1];
207 
208 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
209 		aprint_error_dev(&sc->sc_dev, "r2025rtc_clock_write: failed to acquire I2C bus\n");
210 		return -1;
211 	}
212 
213 	for (i = 0 ; i < len ; i++) {
214 		cmdbuf[0] = (((reg + i) << 4) & 0xf0);
215 		buf[0] = val[i];
216 		if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
217 				cmdbuf, 1, buf, 1, I2C_F_POLL)) {
218 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
219 			aprint_error_dev(&sc->sc_dev, "r2025rtc_reg_write: "
220 				"failed to write registers\n");
221 			return -1;
222 		}
223 	}
224 
225 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
226 
227 	return 0;
228 }
229 
230 static int
231 r2025rtc_reg_read(struct r2025rtc_softc *sc, int reg, uint8_t *val, int len)
232 {
233 	int i;
234 	uint8_t buf[1];
235 	uint8_t cmdbuf[1];
236 
237 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
238 		aprint_error_dev(&sc->sc_dev, "r2025rtc_clock_read: failed to acquire I2C bus\n");
239 		return -1;
240 	}
241 
242 	for (i = 0 ; i < len ; i++) {
243 		cmdbuf[0] = (((reg + i) << 4) & 0xf0);
244 		buf[0] = 0;
245 		if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
246 				cmdbuf, 1, buf, 1, I2C_F_POLL)) {
247 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
248 			aprint_error_dev(&sc->sc_dev, "r2025rtc_reg_read: "
249 				"failed to write registers\n");
250 			return -1;
251 		}
252 
253 		*(val + i) = buf[0];
254 	}
255 
256 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
257 
258 	return 0;
259 }
260