Home | History | Annotate | Line # | Download | only in i2c
pcf8583.c revision 1.13.18.1
      1  1.13.18.1     rmind /*	$NetBSD: pcf8583.c,v 1.13.18.1 2014/05/18 17:45:37 rmind Exp $	*/
      2        1.1   thorpej 
      3        1.1   thorpej /*
      4        1.1   thorpej  * Copyright (c) 2003 Wasabi Systems, Inc.
      5        1.1   thorpej  * All rights reserved.
      6        1.1   thorpej  *
      7        1.1   thorpej  * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
      8        1.1   thorpej  *
      9        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10        1.1   thorpej  * modification, are permitted provided that the following conditions
     11        1.1   thorpej  * are met:
     12        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17        1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18        1.1   thorpej  *    must display the following acknowledgement:
     19        1.1   thorpej  *      This product includes software developed for the NetBSD Project by
     20        1.1   thorpej  *      Wasabi Systems, Inc.
     21        1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22        1.1   thorpej  *    or promote products derived from this software without specific prior
     23        1.1   thorpej  *    written permission.
     24        1.1   thorpej  *
     25        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26        1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27        1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28        1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29        1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30        1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31        1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32        1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33        1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34        1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35        1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36        1.1   thorpej  */
     37        1.1   thorpej 
     38        1.1   thorpej /*
     39        1.1   thorpej  * Driver for the Philips PCF8583 Real Time Clock.
     40        1.1   thorpej  *
     41        1.1   thorpej  * This driver is partially derived from Ben Harris's PCF8583 driver
     42        1.1   thorpej  * for NetBSD/acorn26.
     43        1.1   thorpej  */
     44        1.1   thorpej 
     45        1.8     lukem #include <sys/cdefs.h>
     46  1.13.18.1     rmind __KERNEL_RCSID(0, "$NetBSD: pcf8583.c,v 1.13.18.1 2014/05/18 17:45:37 rmind Exp $");
     47        1.8     lukem 
     48        1.1   thorpej #include <sys/param.h>
     49        1.1   thorpej #include <sys/systm.h>
     50        1.1   thorpej #include <sys/device.h>
     51        1.1   thorpej #include <sys/kernel.h>
     52        1.1   thorpej #include <sys/fcntl.h>
     53        1.1   thorpej #include <sys/uio.h>
     54        1.1   thorpej #include <sys/conf.h>
     55        1.1   thorpej #include <sys/event.h>
     56        1.1   thorpej 
     57        1.1   thorpej #include <dev/clock_subr.h>
     58        1.1   thorpej 
     59        1.1   thorpej #include <dev/i2c/i2cvar.h>
     60        1.1   thorpej #include <dev/i2c/pcf8583reg.h>
     61        1.1   thorpej #include <dev/i2c/pcf8583var.h>
     62        1.1   thorpej 
     63        1.1   thorpej struct pcfrtc_softc {
     64       1.10   xtraeme 	device_t sc_dev;
     65        1.1   thorpej 	i2c_tag_t sc_tag;
     66        1.1   thorpej 	int sc_address;
     67        1.1   thorpej 	int sc_open;
     68        1.1   thorpej 	struct todr_chip_handle sc_todr;
     69        1.1   thorpej };
     70        1.1   thorpej 
     71       1.10   xtraeme static int  pcfrtc_match(device_t, cfdata_t, void *);
     72       1.10   xtraeme static void pcfrtc_attach(device_t, device_t, void *);
     73        1.1   thorpej 
     74       1.10   xtraeme CFATTACH_DECL_NEW(pcfrtc, sizeof(struct pcfrtc_softc),
     75        1.1   thorpej 	pcfrtc_match, pcfrtc_attach, NULL, NULL);
     76        1.1   thorpej extern struct cfdriver pcfrtc_cd;
     77        1.1   thorpej 
     78        1.1   thorpej dev_type_open(pcfrtc_open);
     79        1.1   thorpej dev_type_close(pcfrtc_close);
     80        1.1   thorpej dev_type_read(pcfrtc_read);
     81        1.1   thorpej dev_type_write(pcfrtc_write);
     82        1.1   thorpej 
     83        1.1   thorpej const struct cdevsw pcfrtc_cdevsw = {
     84  1.13.18.1     rmind 	.d_open = pcfrtc_open,
     85  1.13.18.1     rmind 	.d_close = pcfrtc_close,
     86  1.13.18.1     rmind 	.d_read = pcfrtc_read,
     87  1.13.18.1     rmind 	.d_write = pcfrtc_write,
     88  1.13.18.1     rmind 	.d_ioctl = noioctl,
     89  1.13.18.1     rmind 	.d_stop = nostop,
     90  1.13.18.1     rmind 	.d_tty = notty,
     91  1.13.18.1     rmind 	.d_poll = nopoll,
     92  1.13.18.1     rmind 	.d_mmap = nommap,
     93  1.13.18.1     rmind 	.d_kqfilter = nokqfilter,
     94  1.13.18.1     rmind 	.d_flag = D_OTHER
     95        1.1   thorpej };
     96        1.1   thorpej 
     97        1.1   thorpej static int pcfrtc_clock_read(struct pcfrtc_softc *, struct clock_ymdhms *,
     98        1.1   thorpej 			     uint8_t *);
     99        1.1   thorpej static int pcfrtc_clock_write(struct pcfrtc_softc *, struct clock_ymdhms *,
    100        1.1   thorpej 			      uint8_t);
    101       1.12   tsutsui static int pcfrtc_gettime(struct todr_chip_handle *, struct timeval *);
    102       1.12   tsutsui static int pcfrtc_settime(struct todr_chip_handle *, struct timeval *);
    103        1.1   thorpej 
    104        1.1   thorpej int
    105       1.10   xtraeme pcfrtc_match(device_t parent, cfdata_t cf, void *aux)
    106        1.1   thorpej {
    107        1.1   thorpej 	struct i2c_attach_args *ia = aux;
    108        1.1   thorpej 
    109        1.1   thorpej 	if ((ia->ia_addr & PCF8583_ADDRMASK) == PCF8583_ADDR)
    110        1.1   thorpej 		return (1);
    111        1.1   thorpej 
    112        1.1   thorpej 	return (0);
    113        1.1   thorpej }
    114        1.1   thorpej 
    115        1.1   thorpej void
    116       1.10   xtraeme pcfrtc_attach(device_t parent, device_t self, void *aux)
    117        1.1   thorpej {
    118        1.4   thorpej 	struct pcfrtc_softc *sc = device_private(self);
    119        1.1   thorpej 	struct i2c_attach_args *ia = aux;
    120        1.1   thorpej 	uint8_t cmdbuf[1], csr;
    121        1.1   thorpej 
    122        1.1   thorpej 	sc->sc_tag = ia->ia_tag;
    123        1.1   thorpej 	sc->sc_address = ia->ia_addr;
    124       1.10   xtraeme 	sc->sc_dev = self;
    125        1.1   thorpej 
    126        1.1   thorpej 	aprint_naive(": Real-time Clock/NVRAM\n");
    127        1.1   thorpej 	aprint_normal(": PCF8583 Real-time Clock/NVRAM\n");
    128        1.1   thorpej 
    129        1.1   thorpej 	cmdbuf[0] = PCF8583_REG_CSR;
    130        1.1   thorpej 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
    131        1.1   thorpej 	    cmdbuf, 1, &csr, 1, 0) != 0) {
    132       1.10   xtraeme 		aprint_error_dev(self, "unable to read CSR\n");
    133        1.1   thorpej 		return;
    134        1.1   thorpej 	}
    135       1.10   xtraeme 	aprint_normal_dev(sc->sc_dev, "");
    136        1.1   thorpej 	switch (csr & PCF8583_CSR_FN_MASK) {
    137        1.1   thorpej 	case PCF8583_CSR_FN_32768HZ:
    138        1.1   thorpej 		aprint_normal(" 32.768 kHz clock");
    139        1.1   thorpej 		break;
    140        1.1   thorpej 
    141        1.1   thorpej 	case PCF8583_CSR_FN_50HZ:
    142        1.1   thorpej 		aprint_normal(" 50 Hz clock");
    143        1.1   thorpej 		break;
    144        1.1   thorpej 
    145        1.1   thorpej 	case PCF8583_CSR_FN_EVENT:
    146        1.1   thorpej 		aprint_normal(" event counter");
    147        1.1   thorpej 		break;
    148        1.1   thorpej 
    149        1.1   thorpej 	case PCF8583_CSR_FN_TEST:
    150        1.1   thorpej 		aprint_normal(" test mode");
    151        1.1   thorpej 		break;
    152        1.1   thorpej 	}
    153        1.1   thorpej 	if (csr & PCF8583_CSR_STOP)
    154        1.1   thorpej 		aprint_normal(", stopped");
    155        1.1   thorpej 	if (csr & PCF8583_CSR_ALARMENABLE)
    156        1.1   thorpej 		aprint_normal(", alarm enabled");
    157        1.1   thorpej 	aprint_normal("\n");
    158        1.1   thorpej 
    159        1.1   thorpej 	sc->sc_open = 0;
    160        1.1   thorpej 
    161        1.1   thorpej 	sc->sc_todr.cookie = sc;
    162        1.1   thorpej 	sc->sc_todr.todr_gettime = pcfrtc_gettime;
    163        1.1   thorpej 	sc->sc_todr.todr_settime = pcfrtc_settime;
    164        1.1   thorpej 	sc->sc_todr.todr_setwen = NULL;
    165        1.1   thorpej 
    166        1.1   thorpej 	todr_attach(&sc->sc_todr);
    167        1.1   thorpej }
    168        1.1   thorpej 
    169        1.1   thorpej /*ARGSUSED*/
    170        1.1   thorpej int
    171        1.3  christos pcfrtc_open(dev_t dev, int flag, int fmt, struct lwp *l)
    172        1.1   thorpej {
    173        1.1   thorpej 	struct pcfrtc_softc *sc;
    174        1.1   thorpej 
    175       1.11   tsutsui 	if ((sc = device_lookup_private(&pcfrtc_cd, minor(dev))) == NULL)
    176        1.1   thorpej 		return (ENXIO);
    177        1.1   thorpej 
    178        1.1   thorpej 	/* XXX: Locking */
    179        1.1   thorpej 
    180        1.1   thorpej 	if (sc->sc_open)
    181        1.1   thorpej 		return (EBUSY);
    182        1.1   thorpej 
    183        1.1   thorpej 	sc->sc_open = 1;
    184        1.1   thorpej 	return (0);
    185        1.1   thorpej }
    186        1.1   thorpej 
    187        1.1   thorpej /*ARGSUSED*/
    188        1.1   thorpej int
    189        1.3  christos pcfrtc_close(dev_t dev, int flag, int fmt, struct lwp *l)
    190        1.1   thorpej {
    191        1.1   thorpej 	struct pcfrtc_softc *sc;
    192        1.1   thorpej 
    193       1.11   tsutsui 	if ((sc = device_lookup_private(&pcfrtc_cd, minor(dev))) == NULL)
    194        1.1   thorpej 		return (ENXIO);
    195        1.1   thorpej 
    196        1.1   thorpej 	sc->sc_open = 0;
    197        1.1   thorpej 	return (0);
    198        1.1   thorpej }
    199        1.1   thorpej 
    200        1.1   thorpej /*ARGSUSED*/
    201        1.1   thorpej int
    202        1.1   thorpej pcfrtc_read(dev_t dev, struct uio *uio, int flags)
    203        1.1   thorpej {
    204        1.1   thorpej 	struct pcfrtc_softc *sc;
    205        1.1   thorpej 	u_int8_t ch, cmdbuf[1];
    206        1.1   thorpej 	int a, error;
    207        1.1   thorpej 
    208       1.11   tsutsui 	if ((sc = device_lookup_private(&pcfrtc_cd, minor(dev))) == NULL)
    209        1.1   thorpej 		return (ENXIO);
    210        1.1   thorpej 
    211        1.1   thorpej 	if (uio->uio_offset >= PCF8583_NVRAM_SIZE)
    212        1.1   thorpej 		return (EINVAL);
    213        1.1   thorpej 
    214        1.1   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    215        1.1   thorpej 		return (error);
    216        1.1   thorpej 
    217        1.1   thorpej 	while (uio->uio_resid && uio->uio_offset < PCF8583_NVRAM_SIZE) {
    218        1.1   thorpej 		a = (int)uio->uio_offset;
    219        1.1   thorpej 		cmdbuf[0] = a + PCF8583_NVRAM_START;
    220        1.1   thorpej 		if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    221        1.1   thorpej 				      sc->sc_address, cmdbuf, 1,
    222        1.1   thorpej 				      &ch, 1, 0)) != 0) {
    223        1.1   thorpej 			iic_release_bus(sc->sc_tag, 0);
    224       1.10   xtraeme 			aprint_error_dev(sc->sc_dev,
    225       1.10   xtraeme 			    "pcfrtc_read: read failed at 0x%x\n", a);
    226        1.1   thorpej 			return (error);
    227        1.1   thorpej 		}
    228        1.1   thorpej 		if ((error = uiomove(&ch, 1, uio)) != 0) {
    229        1.1   thorpej 			iic_release_bus(sc->sc_tag, 0);
    230        1.1   thorpej 			return (error);
    231        1.1   thorpej 		}
    232        1.1   thorpej 	}
    233        1.1   thorpej 
    234        1.1   thorpej 	iic_release_bus(sc->sc_tag, 0);
    235        1.1   thorpej 
    236        1.1   thorpej 	return (0);
    237        1.1   thorpej }
    238        1.1   thorpej 
    239        1.1   thorpej /*ARGSUSED*/
    240        1.1   thorpej int
    241        1.1   thorpej pcfrtc_write(dev_t dev, struct uio *uio, int flags)
    242        1.1   thorpej {
    243        1.1   thorpej 	struct pcfrtc_softc *sc;
    244        1.1   thorpej 	u_int8_t cmdbuf[2];
    245        1.1   thorpej 	int a, error;
    246        1.1   thorpej 
    247       1.11   tsutsui 	if ((sc = device_lookup_private(&pcfrtc_cd, minor(dev))) == NULL)
    248        1.1   thorpej 		return (ENXIO);
    249        1.1   thorpej 
    250        1.1   thorpej 	if (uio->uio_offset >= PCF8583_NVRAM_SIZE)
    251        1.1   thorpej 		return (EINVAL);
    252        1.1   thorpej 
    253        1.1   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    254        1.1   thorpej 		return (error);
    255        1.1   thorpej 
    256        1.1   thorpej 	while (uio->uio_resid && uio->uio_offset < PCF8583_NVRAM_SIZE) {
    257        1.1   thorpej 		a = (int)uio->uio_offset;
    258        1.1   thorpej 		cmdbuf[0] = a + PCF8583_NVRAM_START;
    259        1.1   thorpej 		if ((error = uiomove(&cmdbuf[1], 1, uio)) != 0)
    260        1.1   thorpej 			break;
    261        1.1   thorpej 
    262        1.1   thorpej 		if ((error = iic_exec(sc->sc_tag,
    263        1.1   thorpej 		    uio->uio_resid ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
    264        1.1   thorpej 		    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, 0)) != 0) {
    265       1.10   xtraeme 			aprint_error_dev(sc->sc_dev,
    266       1.10   xtraeme 			    "pcfrtc_write: write failed at 0x%x\n", a);
    267        1.1   thorpej 			return (error);
    268        1.1   thorpej 		}
    269        1.1   thorpej 	}
    270        1.1   thorpej 
    271        1.1   thorpej 	iic_release_bus(sc->sc_tag, 0);
    272        1.1   thorpej 
    273        1.1   thorpej 	return (error);
    274        1.1   thorpej }
    275        1.1   thorpej 
    276        1.1   thorpej static int
    277       1.12   tsutsui pcfrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
    278        1.1   thorpej {
    279        1.1   thorpej 	struct pcfrtc_softc *sc = ch->cookie;
    280        1.1   thorpej 	struct clock_ymdhms dt;
    281        1.5     bjh21 	int err;
    282        1.1   thorpej 	uint8_t centi;
    283        1.1   thorpej 
    284        1.5     bjh21 	if ((err = pcfrtc_clock_read(sc, &dt, &centi)))
    285        1.5     bjh21 		return err;
    286        1.1   thorpej 
    287        1.1   thorpej 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
    288        1.1   thorpej 	tv->tv_usec = centi * 10000;
    289        1.1   thorpej 
    290        1.1   thorpej 	return (0);
    291        1.1   thorpej }
    292        1.1   thorpej 
    293        1.1   thorpej static int
    294       1.12   tsutsui pcfrtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
    295        1.1   thorpej {
    296        1.1   thorpej 	struct pcfrtc_softc *sc = ch->cookie;
    297        1.1   thorpej 	struct clock_ymdhms dt;
    298        1.5     bjh21 	int err;
    299        1.1   thorpej 
    300        1.1   thorpej 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
    301        1.1   thorpej 
    302       1.13   mbalmer 	if ((err = pcfrtc_clock_write(sc, &dt, tv->tv_usec / 10000)) != 0)
    303        1.5     bjh21 		return err;
    304        1.1   thorpej 
    305        1.1   thorpej 	return (0);
    306        1.1   thorpej }
    307        1.1   thorpej 
    308        1.1   thorpej static const int pcf8583_rtc_offset[] = {
    309        1.1   thorpej 	PCF8583_REG_CSR,
    310        1.1   thorpej 	PCF8583_REG_CENTI,
    311        1.1   thorpej 	PCF8583_REG_SEC,
    312        1.1   thorpej 	PCF8583_REG_MIN,
    313        1.1   thorpej 	PCF8583_REG_HOUR,
    314        1.1   thorpej 	PCF8583_REG_YEARDATE,
    315        1.1   thorpej 	PCF8583_REG_WKDYMON,
    316        1.1   thorpej 	PCF8583_REG_TIMER,
    317        1.1   thorpej 	0xc0,			/* NVRAM -- year stored here */
    318        1.1   thorpej 	0xc1,			/* NVRAM -- century stored here */
    319        1.1   thorpej };
    320        1.1   thorpej 
    321        1.1   thorpej static int
    322        1.1   thorpej pcfrtc_clock_read(struct pcfrtc_softc *sc, struct clock_ymdhms *dt,
    323        1.1   thorpej     uint8_t *centi)
    324        1.1   thorpej {
    325        1.1   thorpej 	u_int8_t bcd[10], cmdbuf[1];
    326        1.5     bjh21 	int i, err;
    327        1.1   thorpej 
    328        1.5     bjh21 	if ((err = iic_acquire_bus(sc->sc_tag, I2C_F_POLL))) {
    329       1.10   xtraeme 		aprint_error_dev(sc->sc_dev,
    330       1.10   xtraeme 		    "pcfrtc_clock_read: failed to acquire I2C bus\n");
    331        1.5     bjh21 		return err;
    332        1.1   thorpej 	}
    333        1.1   thorpej 
    334        1.1   thorpej 	/* Read each timekeeping register in order. */
    335        1.1   thorpej 	for (i = 0; i < 10; i++) {
    336        1.1   thorpej 		cmdbuf[0] = pcf8583_rtc_offset[i];
    337        1.1   thorpej 
    338        1.5     bjh21 		if ((err = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    339        1.1   thorpej 			     sc->sc_address, cmdbuf, 1,
    340        1.5     bjh21 			     &bcd[i], 1, I2C_F_POLL))) {
    341        1.1   thorpej 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    342       1.10   xtraeme 			aprint_error_dev(sc->sc_dev,
    343       1.10   xtraeme 			    "pcfrtc_clock_read: failed to read rtc "
    344        1.9    cegger 			    "at 0x%x\n",
    345        1.1   thorpej 			    pcf8583_rtc_offset[i]);
    346        1.5     bjh21 			return err;
    347        1.1   thorpej 		}
    348        1.1   thorpej 	}
    349        1.1   thorpej 
    350        1.1   thorpej 	/* Done with I2C */
    351        1.1   thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    352        1.1   thorpej 
    353        1.1   thorpej 	/*
    354        1.1   thorpej 	 * Convert the PCF8583's register values into something useable
    355        1.1   thorpej 	 */
    356        1.1   thorpej 	*centi      = FROMBCD(bcd[PCF8583_REG_CENTI]);
    357        1.1   thorpej 	dt->dt_sec  = FROMBCD(bcd[PCF8583_REG_SEC]);
    358        1.1   thorpej 	dt->dt_min  = FROMBCD(bcd[PCF8583_REG_MIN]);
    359        1.1   thorpej 	dt->dt_hour = FROMBCD(bcd[PCF8583_REG_HOUR] & PCF8583_HOUR_MASK);
    360        1.1   thorpej 	if (bcd[PCF8583_REG_HOUR] & PCF8583_HOUR_12H) {
    361        1.1   thorpej 		dt->dt_hour %= 12;	/* 12AM -> 0, 12PM -> 12 */
    362        1.1   thorpej 		if (bcd[PCF8583_REG_HOUR] & PCF8583_HOUR_PM)
    363        1.1   thorpej 			dt->dt_hour += 12;
    364        1.1   thorpej 	}
    365        1.1   thorpej 
    366        1.1   thorpej 	dt->dt_day = FROMBCD(bcd[PCF8583_REG_YEARDATE] & PCF8583_DATE_MASK);
    367        1.1   thorpej 	dt->dt_mon = FROMBCD(bcd[PCF8583_REG_WKDYMON] & PCF8583_MON_MASK);
    368        1.1   thorpej 
    369        1.1   thorpej 	dt->dt_year = bcd[8] + (bcd[9] * 100);
    370        1.1   thorpej 	/* Try to notice if the year's rolled over. */
    371        1.1   thorpej 	if (bcd[PCF8583_REG_CSR] & PCF8583_CSR_MASK)
    372       1.10   xtraeme 		aprint_error_dev(sc->sc_dev,
    373       1.10   xtraeme 		    "cannot check year in mask mode\n");
    374        1.1   thorpej 	else {
    375        1.1   thorpej 		while (dt->dt_year % 4 !=
    376        1.1   thorpej 		       (bcd[PCF8583_REG_YEARDATE] &
    377        1.1   thorpej 			PCF8583_YEAR_MASK) >> PCF8583_YEAR_SHIFT)
    378        1.1   thorpej 			dt->dt_year++;
    379        1.1   thorpej 	}
    380        1.1   thorpej 
    381        1.5     bjh21 	return 0;
    382        1.1   thorpej }
    383        1.1   thorpej 
    384        1.1   thorpej static int
    385        1.1   thorpej pcfrtc_clock_write(struct pcfrtc_softc *sc, struct clock_ymdhms *dt,
    386        1.1   thorpej     uint8_t centi)
    387        1.1   thorpej {
    388        1.1   thorpej 	uint8_t bcd[10], cmdbuf[2];
    389        1.5     bjh21 	int i, err;
    390        1.1   thorpej 
    391        1.1   thorpej 	/*
    392        1.1   thorpej 	 * Convert our time representation into something the PCF8583
    393        1.1   thorpej 	 * can understand.
    394        1.1   thorpej 	 */
    395        1.1   thorpej 	bcd[PCF8583_REG_CENTI]    = centi;
    396        1.1   thorpej 	bcd[PCF8583_REG_SEC]      = TOBCD(dt->dt_sec);
    397        1.1   thorpej 	bcd[PCF8583_REG_MIN]      = TOBCD(dt->dt_min);
    398        1.1   thorpej 	bcd[PCF8583_REG_HOUR]     = TOBCD(dt->dt_hour) & PCF8583_HOUR_MASK;
    399        1.1   thorpej 	bcd[PCF8583_REG_YEARDATE] = TOBCD(dt->dt_day) |
    400        1.1   thorpej 	    ((dt->dt_year % 4) << PCF8583_YEAR_SHIFT);
    401        1.1   thorpej 	bcd[PCF8583_REG_WKDYMON]  = TOBCD(dt->dt_mon) |
    402        1.1   thorpej 	    ((dt->dt_wday % 4) << PCF8583_WKDY_SHIFT);
    403        1.1   thorpej 	bcd[8]                    = dt->dt_year % 100;
    404        1.1   thorpej 	bcd[9]                    = dt->dt_year / 100;
    405        1.1   thorpej 
    406        1.5     bjh21 	if ((err = iic_acquire_bus(sc->sc_tag, I2C_F_POLL))) {
    407       1.10   xtraeme 		aprint_error_dev(sc->sc_dev,
    408       1.10   xtraeme 		    "pcfrtc_clock_write: failed to acquire I2C bus\n");
    409        1.5     bjh21 		return err;
    410        1.1   thorpej 	}
    411        1.1   thorpej 
    412        1.1   thorpej 	for (i = 1; i < 10; i++) {
    413        1.1   thorpej 		cmdbuf[0] = pcf8583_rtc_offset[i];
    414        1.5     bjh21 		if ((err = iic_exec(sc->sc_tag,
    415        1.1   thorpej 			     i != 9 ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
    416        1.1   thorpej 			     sc->sc_address, cmdbuf, 1,
    417        1.5     bjh21 			     &bcd[i], 1, I2C_F_POLL))) {
    418        1.1   thorpej 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    419       1.10   xtraeme 			aprint_error_dev(sc->sc_dev,
    420       1.10   xtraeme 			    "pcfrtc_clock_write: failed to write rtc "
    421        1.9    cegger 			    " at 0x%x\n",
    422        1.1   thorpej 			    pcf8583_rtc_offset[i]);
    423        1.5     bjh21 			return err;
    424        1.1   thorpej 		}
    425        1.1   thorpej 	}
    426        1.1   thorpej 
    427        1.1   thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    428        1.1   thorpej 
    429        1.5     bjh21 	return 0;
    430        1.1   thorpej }
    431        1.1   thorpej 
    432        1.1   thorpej int
    433        1.1   thorpej pcfrtc_bootstrap_read(i2c_tag_t tag, int i2caddr, int offset,
    434        1.1   thorpej     u_int8_t *rvp, size_t len)
    435        1.1   thorpej {
    436        1.1   thorpej 	u_int8_t cmdbuf[1];
    437        1.1   thorpej 
    438        1.1   thorpej 	/*
    439        1.1   thorpej 	 * NOTE: "offset" is an absolute offset into the PCF8583
    440        1.1   thorpej 	 * address space, not relative to the NVRAM.
    441        1.1   thorpej 	 */
    442        1.1   thorpej 
    443        1.1   thorpej 	if (len == 0)
    444        1.1   thorpej 		return (0);
    445        1.1   thorpej 
    446        1.1   thorpej 	if (iic_acquire_bus(tag, I2C_F_POLL) != 0)
    447        1.1   thorpej 		return (-1);
    448        1.1   thorpej 
    449        1.1   thorpej 	while (len) {
    450        1.1   thorpej 		/* Read a single byte. */
    451        1.1   thorpej 		cmdbuf[0] = offset;
    452        1.1   thorpej 		if (iic_exec(tag, I2C_OP_READ_WITH_STOP, i2caddr,
    453        1.1   thorpej 			     cmdbuf, 1, rvp, 1, I2C_F_POLL)) {
    454        1.1   thorpej 			iic_release_bus(tag, I2C_F_POLL);
    455        1.1   thorpej 			return (-1);
    456        1.1   thorpej 		}
    457        1.1   thorpej 
    458        1.1   thorpej 		len--;
    459        1.1   thorpej 		rvp++;
    460        1.1   thorpej 		offset++;
    461        1.1   thorpej 	}
    462        1.1   thorpej 
    463        1.1   thorpej 	iic_release_bus(tag, I2C_F_POLL);
    464        1.1   thorpej 	return (0);
    465        1.1   thorpej }
    466        1.1   thorpej 
    467        1.1   thorpej int
    468        1.1   thorpej pcfrtc_bootstrap_write(i2c_tag_t tag, int i2caddr, int offset,
    469        1.1   thorpej     u_int8_t *rvp, size_t len)
    470        1.1   thorpej {
    471        1.1   thorpej 	u_int8_t cmdbuf[1];
    472        1.1   thorpej 
    473        1.1   thorpej 	/*
    474        1.1   thorpej 	 * NOTE: "offset" is an absolute offset into the PCF8583
    475        1.1   thorpej 	 * address space, not relative to the NVRAM.
    476        1.1   thorpej 	 */
    477        1.1   thorpej 
    478        1.1   thorpej 	if (len == 0)
    479        1.1   thorpej 		return (0);
    480        1.1   thorpej 
    481        1.1   thorpej 	if (iic_acquire_bus(tag, I2C_F_POLL) != 0)
    482        1.1   thorpej 		return (-1);
    483        1.1   thorpej 
    484        1.1   thorpej 	while (len) {
    485        1.1   thorpej 		/* Write a single byte. */
    486        1.1   thorpej 		cmdbuf[0] = offset;
    487        1.1   thorpej 		if (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, i2caddr,
    488        1.1   thorpej 			     cmdbuf, 1, rvp, 1, I2C_F_POLL)) {
    489        1.1   thorpej 			iic_release_bus(tag, I2C_F_POLL);
    490        1.1   thorpej 			return (-1);
    491        1.1   thorpej 		}
    492        1.1   thorpej 
    493        1.1   thorpej 		len--;
    494        1.1   thorpej 		rvp++;
    495        1.1   thorpej 		offset++;
    496        1.1   thorpej 	}
    497        1.1   thorpej 
    498        1.1   thorpej 	iic_release_bus(tag, I2C_F_POLL);
    499        1.1   thorpej 	return (0);
    500        1.1   thorpej }
    501