No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / i2c / pcf8583.c
blobbecb98c1a2e4a18bcdfed38efd886228f0937c7a
1 /* $NetBSD: pcf8583.c,v 1.11 2008/06/08 03:49:26 tsutsui Exp $ */
3 /*
4 * Copyright (c) 2003 Wasabi Systems, Inc.
5 * All rights reserved.
7 * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
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 copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * Driver for the Philips PCF8583 Real Time Clock.
41 * This driver is partially derived from Ben Harris's PCF8583 driver
42 * for NetBSD/acorn26.
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: pcf8583.c,v 1.11 2008/06/08 03:49:26 tsutsui Exp $");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 #include <sys/kernel.h>
52 #include <sys/fcntl.h>
53 #include <sys/uio.h>
54 #include <sys/conf.h>
55 #include <sys/event.h>
57 #include <dev/clock_subr.h>
59 #include <dev/i2c/i2cvar.h>
60 #include <dev/i2c/pcf8583reg.h>
61 #include <dev/i2c/pcf8583var.h>
63 struct pcfrtc_softc {
64 device_t sc_dev;
65 i2c_tag_t sc_tag;
66 int sc_address;
67 int sc_open;
68 struct todr_chip_handle sc_todr;
71 static int pcfrtc_match(device_t, cfdata_t, void *);
72 static void pcfrtc_attach(device_t, device_t, void *);
74 CFATTACH_DECL_NEW(pcfrtc, sizeof(struct pcfrtc_softc),
75 pcfrtc_match, pcfrtc_attach, NULL, NULL);
76 extern struct cfdriver pcfrtc_cd;
78 dev_type_open(pcfrtc_open);
79 dev_type_close(pcfrtc_close);
80 dev_type_read(pcfrtc_read);
81 dev_type_write(pcfrtc_write);
83 const struct cdevsw pcfrtc_cdevsw = {
84 pcfrtc_open, pcfrtc_close, pcfrtc_read, pcfrtc_write, noioctl,
85 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
88 static int pcfrtc_clock_read(struct pcfrtc_softc *, struct clock_ymdhms *,
89 uint8_t *);
90 static int pcfrtc_clock_write(struct pcfrtc_softc *, struct clock_ymdhms *,
91 uint8_t);
92 static int pcfrtc_gettime(struct todr_chip_handle *, struct timeval *);
93 static int pcfrtc_settime(struct todr_chip_handle *, struct timeval *);
95 int
96 pcfrtc_match(device_t parent, cfdata_t cf, void *aux)
98 struct i2c_attach_args *ia = aux;
100 if ((ia->ia_addr & PCF8583_ADDRMASK) == PCF8583_ADDR)
101 return (1);
103 return (0);
106 void
107 pcfrtc_attach(device_t parent, device_t self, void *aux)
109 struct pcfrtc_softc *sc = device_private(self);
110 struct i2c_attach_args *ia = aux;
111 uint8_t cmdbuf[1], csr;
113 sc->sc_tag = ia->ia_tag;
114 sc->sc_address = ia->ia_addr;
115 sc->sc_dev = self;
117 aprint_naive(": Real-time Clock/NVRAM\n");
118 aprint_normal(": PCF8583 Real-time Clock/NVRAM\n");
120 cmdbuf[0] = PCF8583_REG_CSR;
121 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
122 cmdbuf, 1, &csr, 1, 0) != 0) {
123 aprint_error_dev(self, "unable to read CSR\n");
124 return;
126 aprint_normal_dev(sc->sc_dev, "");
127 switch (csr & PCF8583_CSR_FN_MASK) {
128 case PCF8583_CSR_FN_32768HZ:
129 aprint_normal(" 32.768 kHz clock");
130 break;
132 case PCF8583_CSR_FN_50HZ:
133 aprint_normal(" 50 Hz clock");
134 break;
136 case PCF8583_CSR_FN_EVENT:
137 aprint_normal(" event counter");
138 break;
140 case PCF8583_CSR_FN_TEST:
141 aprint_normal(" test mode");
142 break;
144 if (csr & PCF8583_CSR_STOP)
145 aprint_normal(", stopped");
146 if (csr & PCF8583_CSR_ALARMENABLE)
147 aprint_normal(", alarm enabled");
148 aprint_normal("\n");
150 sc->sc_open = 0;
152 sc->sc_todr.cookie = sc;
153 sc->sc_todr.todr_gettime = pcfrtc_gettime;
154 sc->sc_todr.todr_settime = pcfrtc_settime;
155 sc->sc_todr.todr_setwen = NULL;
157 todr_attach(&sc->sc_todr);
160 /*ARGSUSED*/
162 pcfrtc_open(dev_t dev, int flag, int fmt, struct lwp *l)
164 struct pcfrtc_softc *sc;
166 if ((sc = device_lookup_private(&pcfrtc_cd, minor(dev))) == NULL)
167 return (ENXIO);
169 /* XXX: Locking */
171 if (sc->sc_open)
172 return (EBUSY);
174 sc->sc_open = 1;
175 return (0);
178 /*ARGSUSED*/
180 pcfrtc_close(dev_t dev, int flag, int fmt, struct lwp *l)
182 struct pcfrtc_softc *sc;
184 if ((sc = device_lookup_private(&pcfrtc_cd, minor(dev))) == NULL)
185 return (ENXIO);
187 sc->sc_open = 0;
188 return (0);
191 /*ARGSUSED*/
193 pcfrtc_read(dev_t dev, struct uio *uio, int flags)
195 struct pcfrtc_softc *sc;
196 u_int8_t ch, cmdbuf[1];
197 int a, error;
199 if ((sc = device_lookup_private(&pcfrtc_cd, minor(dev))) == NULL)
200 return (ENXIO);
202 if (uio->uio_offset >= PCF8583_NVRAM_SIZE)
203 return (EINVAL);
205 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
206 return (error);
208 while (uio->uio_resid && uio->uio_offset < PCF8583_NVRAM_SIZE) {
209 a = (int)uio->uio_offset;
210 cmdbuf[0] = a + PCF8583_NVRAM_START;
211 if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
212 sc->sc_address, cmdbuf, 1,
213 &ch, 1, 0)) != 0) {
214 iic_release_bus(sc->sc_tag, 0);
215 aprint_error_dev(sc->sc_dev,
216 "pcfrtc_read: read failed at 0x%x\n", a);
217 return (error);
219 if ((error = uiomove(&ch, 1, uio)) != 0) {
220 iic_release_bus(sc->sc_tag, 0);
221 return (error);
225 iic_release_bus(sc->sc_tag, 0);
227 return (0);
230 /*ARGSUSED*/
232 pcfrtc_write(dev_t dev, struct uio *uio, int flags)
234 struct pcfrtc_softc *sc;
235 u_int8_t cmdbuf[2];
236 int a, error;
238 if ((sc = device_lookup_private(&pcfrtc_cd, minor(dev))) == NULL)
239 return (ENXIO);
241 if (uio->uio_offset >= PCF8583_NVRAM_SIZE)
242 return (EINVAL);
244 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
245 return (error);
247 while (uio->uio_resid && uio->uio_offset < PCF8583_NVRAM_SIZE) {
248 a = (int)uio->uio_offset;
249 cmdbuf[0] = a + PCF8583_NVRAM_START;
250 if ((error = uiomove(&cmdbuf[1], 1, uio)) != 0)
251 break;
253 if ((error = iic_exec(sc->sc_tag,
254 uio->uio_resid ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
255 sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, 0)) != 0) {
256 aprint_error_dev(sc->sc_dev,
257 "pcfrtc_write: write failed at 0x%x\n", a);
258 return (error);
262 iic_release_bus(sc->sc_tag, 0);
264 return (error);
267 static int
268 pcfrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
270 struct pcfrtc_softc *sc = ch->cookie;
271 struct clock_ymdhms dt;
272 int err;
273 uint8_t centi;
275 if ((err = pcfrtc_clock_read(sc, &dt, &centi)))
276 return err;
278 tv->tv_sec = clock_ymdhms_to_secs(&dt);
279 tv->tv_usec = centi * 10000;
281 return (0);
284 static int
285 pcfrtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
287 struct pcfrtc_softc *sc = ch->cookie;
288 struct clock_ymdhms dt;
289 int err;
291 clock_secs_to_ymdhms(tv->tv_sec, &dt);
293 if ((err = pcfrtc_clock_write(sc, &dt, tv->tv_usec / 10000) == 0))
294 return err;
296 return (0);
299 static const int pcf8583_rtc_offset[] = {
300 PCF8583_REG_CSR,
301 PCF8583_REG_CENTI,
302 PCF8583_REG_SEC,
303 PCF8583_REG_MIN,
304 PCF8583_REG_HOUR,
305 PCF8583_REG_YEARDATE,
306 PCF8583_REG_WKDYMON,
307 PCF8583_REG_TIMER,
308 0xc0, /* NVRAM -- year stored here */
309 0xc1, /* NVRAM -- century stored here */
312 static int
313 pcfrtc_clock_read(struct pcfrtc_softc *sc, struct clock_ymdhms *dt,
314 uint8_t *centi)
316 u_int8_t bcd[10], cmdbuf[1];
317 int i, err;
319 if ((err = iic_acquire_bus(sc->sc_tag, I2C_F_POLL))) {
320 aprint_error_dev(sc->sc_dev,
321 "pcfrtc_clock_read: failed to acquire I2C bus\n");
322 return err;
325 /* Read each timekeeping register in order. */
326 for (i = 0; i < 10; i++) {
327 cmdbuf[0] = pcf8583_rtc_offset[i];
329 if ((err = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
330 sc->sc_address, cmdbuf, 1,
331 &bcd[i], 1, I2C_F_POLL))) {
332 iic_release_bus(sc->sc_tag, I2C_F_POLL);
333 aprint_error_dev(sc->sc_dev,
334 "pcfrtc_clock_read: failed to read rtc "
335 "at 0x%x\n",
336 pcf8583_rtc_offset[i]);
337 return err;
341 /* Done with I2C */
342 iic_release_bus(sc->sc_tag, I2C_F_POLL);
345 * Convert the PCF8583's register values into something useable
347 *centi = FROMBCD(bcd[PCF8583_REG_CENTI]);
348 dt->dt_sec = FROMBCD(bcd[PCF8583_REG_SEC]);
349 dt->dt_min = FROMBCD(bcd[PCF8583_REG_MIN]);
350 dt->dt_hour = FROMBCD(bcd[PCF8583_REG_HOUR] & PCF8583_HOUR_MASK);
351 if (bcd[PCF8583_REG_HOUR] & PCF8583_HOUR_12H) {
352 dt->dt_hour %= 12; /* 12AM -> 0, 12PM -> 12 */
353 if (bcd[PCF8583_REG_HOUR] & PCF8583_HOUR_PM)
354 dt->dt_hour += 12;
357 dt->dt_day = FROMBCD(bcd[PCF8583_REG_YEARDATE] & PCF8583_DATE_MASK);
358 dt->dt_mon = FROMBCD(bcd[PCF8583_REG_WKDYMON] & PCF8583_MON_MASK);
360 dt->dt_year = bcd[8] + (bcd[9] * 100);
361 /* Try to notice if the year's rolled over. */
362 if (bcd[PCF8583_REG_CSR] & PCF8583_CSR_MASK)
363 aprint_error_dev(sc->sc_dev,
364 "cannot check year in mask mode\n");
365 else {
366 while (dt->dt_year % 4 !=
367 (bcd[PCF8583_REG_YEARDATE] &
368 PCF8583_YEAR_MASK) >> PCF8583_YEAR_SHIFT)
369 dt->dt_year++;
372 return 0;
375 static int
376 pcfrtc_clock_write(struct pcfrtc_softc *sc, struct clock_ymdhms *dt,
377 uint8_t centi)
379 uint8_t bcd[10], cmdbuf[2];
380 int i, err;
383 * Convert our time representation into something the PCF8583
384 * can understand.
386 bcd[PCF8583_REG_CENTI] = centi;
387 bcd[PCF8583_REG_SEC] = TOBCD(dt->dt_sec);
388 bcd[PCF8583_REG_MIN] = TOBCD(dt->dt_min);
389 bcd[PCF8583_REG_HOUR] = TOBCD(dt->dt_hour) & PCF8583_HOUR_MASK;
390 bcd[PCF8583_REG_YEARDATE] = TOBCD(dt->dt_day) |
391 ((dt->dt_year % 4) << PCF8583_YEAR_SHIFT);
392 bcd[PCF8583_REG_WKDYMON] = TOBCD(dt->dt_mon) |
393 ((dt->dt_wday % 4) << PCF8583_WKDY_SHIFT);
394 bcd[8] = dt->dt_year % 100;
395 bcd[9] = dt->dt_year / 100;
397 if ((err = iic_acquire_bus(sc->sc_tag, I2C_F_POLL))) {
398 aprint_error_dev(sc->sc_dev,
399 "pcfrtc_clock_write: failed to acquire I2C bus\n");
400 return err;
403 for (i = 1; i < 10; i++) {
404 cmdbuf[0] = pcf8583_rtc_offset[i];
405 if ((err = iic_exec(sc->sc_tag,
406 i != 9 ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
407 sc->sc_address, cmdbuf, 1,
408 &bcd[i], 1, I2C_F_POLL))) {
409 iic_release_bus(sc->sc_tag, I2C_F_POLL);
410 aprint_error_dev(sc->sc_dev,
411 "pcfrtc_clock_write: failed to write rtc "
412 " at 0x%x\n",
413 pcf8583_rtc_offset[i]);
414 return err;
418 iic_release_bus(sc->sc_tag, I2C_F_POLL);
420 return 0;
424 pcfrtc_bootstrap_read(i2c_tag_t tag, int i2caddr, int offset,
425 u_int8_t *rvp, size_t len)
427 u_int8_t cmdbuf[1];
430 * NOTE: "offset" is an absolute offset into the PCF8583
431 * address space, not relative to the NVRAM.
434 if (len == 0)
435 return (0);
437 if (iic_acquire_bus(tag, I2C_F_POLL) != 0)
438 return (-1);
440 while (len) {
441 /* Read a single byte. */
442 cmdbuf[0] = offset;
443 if (iic_exec(tag, I2C_OP_READ_WITH_STOP, i2caddr,
444 cmdbuf, 1, rvp, 1, I2C_F_POLL)) {
445 iic_release_bus(tag, I2C_F_POLL);
446 return (-1);
449 len--;
450 rvp++;
451 offset++;
454 iic_release_bus(tag, I2C_F_POLL);
455 return (0);
459 pcfrtc_bootstrap_write(i2c_tag_t tag, int i2caddr, int offset,
460 u_int8_t *rvp, size_t len)
462 u_int8_t cmdbuf[1];
465 * NOTE: "offset" is an absolute offset into the PCF8583
466 * address space, not relative to the NVRAM.
469 if (len == 0)
470 return (0);
472 if (iic_acquire_bus(tag, I2C_F_POLL) != 0)
473 return (-1);
475 while (len) {
476 /* Write a single byte. */
477 cmdbuf[0] = offset;
478 if (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, i2caddr,
479 cmdbuf, 1, rvp, 1, I2C_F_POLL)) {
480 iic_release_bus(tag, I2C_F_POLL);
481 return (-1);
484 len--;
485 rvp++;
486 offset++;
489 iic_release_bus(tag, I2C_F_POLL);
490 return (0);