xref: /openbsd-src/sys/dev/i2c/rs5c372.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: rs5c372.c,v 1.4 2008/04/17 16:50:17 deraadt Exp $	*/
2 /*	$NetBSD: rs5c372.c,v 1.5 2006/03/29 06:41:24 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 2005 Kimihiro Nonaka
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/device.h>
33 #include <sys/kernel.h>
34 #include <sys/fcntl.h>
35 #include <sys/uio.h>
36 #include <sys/conf.h>
37 #include <sys/event.h>
38 
39 #include <dev/clock_subr.h>
40 
41 #include <dev/i2c/i2cvar.h>
42 
43 /*
44  * RS5C372[AB] Real-Time Clock
45  */
46 
47 #define	RICOHRTC_ADDR		0x32	/* Fixed I2C Slave Address */
48 
49 #define RICOHRTC_SECONDS	0
50 #define RICOHRTC_MINUTES	1
51 #define RICOHRTC_HOURS		2
52 #define RICOHRTC_DAY		3
53 #define RICOHRTC_DATE		4
54 #define RICOHRTC_MONTH		5
55 #define RICOHRTC_YEAR		6
56 #define RICOHRTC_CLOCK_CORRECT	7
57 #define RICOHRTC_ALARMA_MIN	8
58 #define RICOHRTC_ALARMA_HOUR	9
59 #define RICOHRTC_ALARMA_DATE	10
60 #define RICOHRTC_ALARMB_MIN	11
61 #define RICOHRTC_ALARMB_HOUR	12
62 #define RICOHRTC_ALARMB_DATE	13
63 #define RICOHRTC_CONTROL1	14
64 #define RICOHRTC_CONTROL2	15
65 #define	RICOHRTC_NREGS		16
66 #define	RICOHRTC_NRTC_REGS	7
67 
68 /*
69  * Bit definitions.
70  */
71 #define	RICOHRTC_SECONDS_MASK	0x7f
72 #define	RICOHRTC_MINUTES_MASK	0x7f
73 #define	RICOHRTC_HOURS_12HRS_PM	(1u << 5)	/* If 12 hr mode, set = PM */
74 #define	RICOHRTC_HOURS_12MASK	0x1f
75 #define	RICOHRTC_HOURS_24MASK	0x3f
76 #define	RICOHRTC_DAY_MASK	0x07
77 #define	RICOHRTC_DATE_MASK	0x3f
78 #define	RICOHRTC_MONTH_MASK	0x1f
79 #define	RICOHRTC_CONTROL2_24HRS	(1u << 5)
80 #define	RICOHRTC_CONTROL2_XSTP	(1u << 4)	/* read */
81 #define	RICOHRTC_CONTROL2_ADJ	(1u << 4)	/* write */
82 #define	RICOHRTC_CONTROL2_NCLEN	(1u << 3)
83 #define	RICOHRTC_CONTROL2_CTFG	(1u << 2)
84 #define	RICOHRTC_CONTROL2_AAFG	(1u << 1)
85 #define	RICOHRTC_CONTROL2_BAFG	(1u << 0)
86 
87 struct ricohrtc_softc {
88 	struct device sc_dev;
89 	i2c_tag_t sc_tag;
90 	int sc_address;
91 	struct todr_chip_handle sc_todr;
92 };
93 
94 int ricohrtc_match(struct device *, void *, void *);
95 void ricohrtc_attach(struct device *, struct device *, void *);
96 
97 struct cfattach ricohrtc_ca = {
98 	sizeof(struct ricohrtc_softc), ricohrtc_match, ricohrtc_attach
99 };
100 
101 struct cfdriver ricohrtc_cd = {
102 	NULL, "ricohrtc", DV_DULL
103 };
104 
105 void ricohrtc_reg_write(struct ricohrtc_softc *, int, uint8_t);
106 int ricohrtc_clock_read(struct ricohrtc_softc *, struct clock_ymdhms *);
107 int ricohrtc_clock_write(struct ricohrtc_softc *, struct clock_ymdhms *);
108 int ricohrtc_gettime(struct todr_chip_handle *, struct timeval *);
109 int ricohrtc_settime(struct todr_chip_handle *, struct timeval *);
110 int ricohrtc_getcal(struct todr_chip_handle *, int *);
111 int ricohrtc_setcal(struct todr_chip_handle *, int);
112 
113 int
114 ricohrtc_match(struct device *parent, void *v, void *arg)
115 {
116 	struct i2c_attach_args *ia = arg;
117 #ifdef PARANOID_CHECKS
118 	u_int8_t data, cmd;
119 #endif
120 
121 	if (ia->ia_addr != RICOHRTC_ADDR)
122 		return (0);
123 
124 #ifdef PARANOID_CHECKS
125 	/* Verify that the 'reserved bits in a few registers read 0 */
126 	if (iic_acquire_bus(ia->ia_tag, I2C_F_POLL)) {
127 		printf("ricohrtc acquire fail\n");
128 		return (0);
129 	}
130 
131 	cmd = RICOHRTC_SECONDS;
132 	if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
133 	    &cmd, sizeof cmd, &data, sizeof data, I2C_F_POLL)) {
134 		iic_release_bus(ia->ia_tag, I2C_F_POLL);
135 		printf("ricohrtc read %d fail\n", cmd);
136 		return (0);
137 	}
138 	if ((data & ~RICOHRTC_SECONDS_MASK) != 0) {
139 		printf("ricohrtc second %d\n",data);
140 		return (0);
141 	}
142 
143 	cmd = RICOHRTC_MINUTES;
144 	if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
145 	    &cmd, sizeof cmd, &data, sizeof data, I2C_F_POLL)) {
146 		iic_release_bus(ia->ia_tag, I2C_F_POLL);
147 		printf("ricohrtc read %d fail\n", cmd);
148 		return (0);
149 	}
150 
151 	if ((data & ~RICOHRTC_MINUTES_MASK) != 0) {
152 		printf("ricohrtc minute %d\n",data);
153 		return (0);
154 	}
155 
156 	cmd = RICOHRTC_HOURS;
157 	if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
158 	    &cmd, sizeof cmd, &data, sizeof data, I2C_F_POLL)) {
159 		iic_release_bus(ia->ia_tag, I2C_F_POLL);
160 		printf("ricohrtc read %d fail\n", cmd);
161 		return (0);
162 	}
163 	if ((data & ~RICOHRTC_HOURS_24MASK) != 0) {
164 		printf("ricohrtc hour %d\n",data);
165 		return (0);
166 	}
167 #endif
168 
169 	iic_release_bus(ia->ia_tag, I2C_F_POLL);
170 	return (1);
171 }
172 
173 void
174 ricohrtc_attach(struct device *parent, struct device *self, void *arg)
175 {
176 	struct ricohrtc_softc *sc = (struct ricohrtc_softc *)self;
177 	struct i2c_attach_args *ia = arg;
178 
179 	printf(": RICOH RS5C372[AB] Real-time Clock\n");
180 
181 	sc->sc_tag = ia->ia_tag;
182 	sc->sc_address = ia->ia_addr;
183 	sc->sc_todr.cookie = sc;
184 	sc->sc_todr.todr_gettime = ricohrtc_gettime;
185 	sc->sc_todr.todr_settime = ricohrtc_settime;
186 	sc->sc_todr.todr_getcal = ricohrtc_getcal;
187 	sc->sc_todr.todr_setcal = ricohrtc_setcal;
188 	sc->sc_todr.todr_setwen = NULL;
189 
190 #if 0
191 	todr_attach(&sc->sc_todr);
192 #else
193 	/* XXX */
194 	{
195 	extern todr_chip_handle_t todr_handle;
196 	todr_handle = &sc->sc_todr;
197 	}
198 #endif
199 
200 	/* Initialize RTC */
201 	ricohrtc_reg_write(sc, RICOHRTC_CONTROL2, RICOHRTC_CONTROL2_24HRS);
202 	ricohrtc_reg_write(sc, RICOHRTC_CONTROL1, 0);
203 }
204 
205 int
206 ricohrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
207 {
208 	struct ricohrtc_softc *sc = ch->cookie;
209 	struct clock_ymdhms dt;
210 
211 	memset(&dt, 0, sizeof(dt));
212 	if (ricohrtc_clock_read(sc, &dt) == 0)
213 		return (-1);
214 
215 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
216 	tv->tv_usec = 0;
217 	return (0);
218 }
219 
220 int
221 ricohrtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
222 {
223 	struct ricohrtc_softc *sc = ch->cookie;
224 	struct clock_ymdhms dt;
225 
226 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
227 
228 	if (ricohrtc_clock_write(sc, &dt) == 0)
229 		return (-1);
230 	return (0);
231 }
232 
233 int
234 ricohrtc_setcal(struct todr_chip_handle *ch, int cal)
235 {
236 
237 	return (EOPNOTSUPP);
238 }
239 
240 int
241 ricohrtc_getcal(struct todr_chip_handle *ch, int *cal)
242 {
243 
244 	return (EOPNOTSUPP);
245 }
246 
247 void
248 ricohrtc_reg_write(struct ricohrtc_softc *sc, int reg, uint8_t val)
249 {
250 	uint8_t cmd;
251 
252 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
253 	reg &= 0xf;
254 	cmd = (reg << 4);
255 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
256 	    &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL)) {
257 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
258 		printf("%s: ricohrtc_reg_write: failed to write reg%d\n",
259 		    sc->sc_dev.dv_xname, reg);
260 		return;
261 	}
262 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
263 }
264 
265 int
266 ricohrtc_clock_read(struct ricohrtc_softc *sc, struct clock_ymdhms *dt)
267 {
268 	uint8_t bcd[RICOHRTC_NRTC_REGS];
269 	uint8_t cmd;
270 
271 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
272 	cmd = (RICOHRTC_SECONDS << 4);
273 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
274 	    &cmd, sizeof cmd, bcd, RICOHRTC_NRTC_REGS, I2C_F_POLL)) {
275 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
276 		printf("%s: ricohrtc_clock_read: failed to read rtc\n",
277 		    sc->sc_dev.dv_xname);
278 		return (0);
279 	}
280 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
281 
282 	/*
283 	 * Convert the RICOHRTC's register values into something useable
284 	 */
285 	dt->dt_sec = FROMBCD(bcd[RICOHRTC_SECONDS] & RICOHRTC_SECONDS_MASK);
286 	dt->dt_min = FROMBCD(bcd[RICOHRTC_MINUTES] & RICOHRTC_MINUTES_MASK);
287 	dt->dt_hour = FROMBCD(bcd[RICOHRTC_HOURS] & RICOHRTC_HOURS_24MASK);
288 	dt->dt_day = FROMBCD(bcd[RICOHRTC_DATE] & RICOHRTC_DATE_MASK);
289 	dt->dt_mon = FROMBCD(bcd[RICOHRTC_MONTH] & RICOHRTC_MONTH_MASK);
290 	dt->dt_year = FROMBCD(bcd[RICOHRTC_YEAR]) + POSIX_BASE_YEAR;
291 	return (1);
292 }
293 
294 int
295 ricohrtc_clock_write(struct ricohrtc_softc *sc, struct clock_ymdhms *dt)
296 {
297 	uint8_t bcd[RICOHRTC_NRTC_REGS];
298 	uint8_t cmd;
299 
300 	/*
301 	 * Convert our time representation into something the RICOHRTC
302 	 * can understand.
303 	 */
304 	bcd[RICOHRTC_SECONDS] = TOBCD(dt->dt_sec);
305 	bcd[RICOHRTC_MINUTES] = TOBCD(dt->dt_min);
306 	bcd[RICOHRTC_HOURS] = TOBCD(dt->dt_hour);
307 	bcd[RICOHRTC_DATE] = TOBCD(dt->dt_day);
308 	bcd[RICOHRTC_DAY] = TOBCD(dt->dt_wday);
309 	bcd[RICOHRTC_MONTH] = TOBCD(dt->dt_mon);
310 	bcd[RICOHRTC_YEAR] = TOBCD(dt->dt_year - POSIX_BASE_YEAR);
311 
312 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
313 	cmd = (RICOHRTC_SECONDS << 4);
314 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
315 	    &cmd, sizeof cmd, bcd, RICOHRTC_NRTC_REGS, I2C_F_POLL)) {
316 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
317 		printf("%s: ricohrtc_clock_write: failed to write rtc\n",
318 		    sc->sc_dev.dv_xname);
319 		return (0);
320 	}
321 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
322 	return (1);
323 }
324