Home | History | Annotate | Line # | Download | only in i2c
pcf8563.c revision 1.3
      1  1.3       phx /*	$NetBSD: pcf8563.c,v 1.3 2012/01/07 21:02:15 phx Exp $	*/
      2  1.1  jakllsch 
      3  1.1  jakllsch /*
      4  1.1  jakllsch  * Copyright (c) 2011 Jonathan A. Kollasch
      5  1.1  jakllsch  * All rights reserved.
      6  1.1  jakllsch  *
      7  1.1  jakllsch  * Redistribution and use in source and binary forms, with or without
      8  1.1  jakllsch  * modification, are permitted provided that the following conditions
      9  1.1  jakllsch  * are met:
     10  1.1  jakllsch  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jakllsch  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jakllsch  *    documentation and/or other materials provided with the distribution.
     15  1.1  jakllsch  *
     16  1.1  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  1.1  jakllsch  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jakllsch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jakllsch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     20  1.1  jakllsch  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  1.1  jakllsch  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  1.1  jakllsch  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     23  1.1  jakllsch  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  1.1  jakllsch  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25  1.1  jakllsch  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     26  1.1  jakllsch  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jakllsch  */
     28  1.1  jakllsch 
     29  1.1  jakllsch #include <sys/cdefs.h>
     30  1.3       phx __KERNEL_RCSID(0, "$NetBSD: pcf8563.c,v 1.3 2012/01/07 21:02:15 phx Exp $");
     31  1.1  jakllsch 
     32  1.1  jakllsch #include <sys/param.h>
     33  1.1  jakllsch #include <sys/systm.h>
     34  1.1  jakllsch #include <sys/device.h>
     35  1.1  jakllsch #include <sys/kernel.h>
     36  1.1  jakllsch 
     37  1.1  jakllsch #include <dev/clock_subr.h>
     38  1.1  jakllsch 
     39  1.1  jakllsch #include <dev/i2c/i2cvar.h>
     40  1.1  jakllsch #include <dev/i2c/pcf8563reg.h>
     41  1.1  jakllsch 
     42  1.1  jakllsch struct pcf8563rtc_softc {
     43  1.1  jakllsch 	device_t sc_dev;
     44  1.1  jakllsch 	i2c_tag_t sc_tag;
     45  1.1  jakllsch 	int sc_addr;
     46  1.1  jakllsch 	struct todr_chip_handle sc_todr;
     47  1.1  jakllsch };
     48  1.1  jakllsch 
     49  1.1  jakllsch static int pcf8563rtc_match(device_t, cfdata_t, void *);
     50  1.1  jakllsch static void pcf8563rtc_attach(device_t, device_t, void *);
     51  1.1  jakllsch 
     52  1.1  jakllsch CFATTACH_DECL_NEW(pcf8563rtc, sizeof(struct pcf8563rtc_softc),
     53  1.1  jakllsch     pcf8563rtc_match, pcf8563rtc_attach, NULL, NULL);
     54  1.1  jakllsch 
     55  1.1  jakllsch static int pcf8563rtc_clock_read(struct pcf8563rtc_softc *, struct clock_ymdhms *);
     56  1.1  jakllsch static int pcf8563rtc_clock_write(struct pcf8563rtc_softc *, struct clock_ymdhms *);
     57  1.2  jakllsch static int pcf8563rtc_gettime(struct todr_chip_handle *, struct clock_ymdhms *);
     58  1.2  jakllsch static int pcf8563rtc_settime(struct todr_chip_handle *, struct clock_ymdhms *);
     59  1.1  jakllsch 
     60  1.1  jakllsch static int
     61  1.1  jakllsch pcf8563rtc_match(device_t parent, cfdata_t cf, void *aux)
     62  1.1  jakllsch {
     63  1.1  jakllsch 	struct i2c_attach_args *ia = aux;
     64  1.1  jakllsch 
     65  1.3       phx 	if (ia->ia_name) {
     66  1.3       phx 		/* direct config - check name */
     67  1.3       phx 		if (strcmp(ia->ia_name, "pcf8563rtc") == 0)
     68  1.3       phx 			return 1;
     69  1.3       phx 	} else {
     70  1.3       phx 		/* indirect config - check typical address */
     71  1.3       phx 		if (ia->ia_addr == PCF8563_ADDR)
     72  1.3       phx 			return 1;
     73  1.3       phx 	}
     74  1.1  jakllsch 	return 0;
     75  1.1  jakllsch }
     76  1.1  jakllsch 
     77  1.1  jakllsch static void
     78  1.1  jakllsch pcf8563rtc_attach(device_t parent, device_t self, void *aux)
     79  1.1  jakllsch {
     80  1.1  jakllsch 	struct pcf8563rtc_softc *sc = device_private(self);
     81  1.1  jakllsch 	struct i2c_attach_args *ia = aux;
     82  1.1  jakllsch 
     83  1.1  jakllsch 	aprint_naive(": Real-time Clock\n");
     84  1.1  jakllsch 	aprint_normal(": NXP PCF8563 Real-time Clock\n");
     85  1.1  jakllsch 
     86  1.1  jakllsch 	sc->sc_dev = self;
     87  1.1  jakllsch 	sc->sc_tag = ia->ia_tag;
     88  1.1  jakllsch 	sc->sc_addr = ia->ia_addr;
     89  1.1  jakllsch 	sc->sc_todr.cookie = sc;
     90  1.2  jakllsch 	sc->sc_todr.todr_gettime_ymdhms = pcf8563rtc_gettime;
     91  1.2  jakllsch 	sc->sc_todr.todr_settime_ymdhms = pcf8563rtc_settime;
     92  1.1  jakllsch 	sc->sc_todr.todr_setwen = NULL;
     93  1.1  jakllsch 
     94  1.1  jakllsch 	todr_attach(&sc->sc_todr);
     95  1.1  jakllsch }
     96  1.1  jakllsch 
     97  1.1  jakllsch static int
     98  1.2  jakllsch pcf8563rtc_gettime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
     99  1.1  jakllsch {
    100  1.1  jakllsch 	struct pcf8563rtc_softc *sc = ch->cookie;
    101  1.2  jakllsch 
    102  1.2  jakllsch 	if (pcf8563rtc_clock_read(sc, dt) == 0)
    103  1.1  jakllsch 		return -1;
    104  1.1  jakllsch 
    105  1.1  jakllsch 	return 0;
    106  1.1  jakllsch }
    107  1.1  jakllsch 
    108  1.1  jakllsch static int
    109  1.2  jakllsch pcf8563rtc_settime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    110  1.1  jakllsch {
    111  1.1  jakllsch         struct pcf8563rtc_softc *sc = ch->cookie;
    112  1.1  jakllsch 
    113  1.2  jakllsch 	if (pcf8563rtc_clock_write(sc, dt) == 0)
    114  1.1  jakllsch 		return -1;
    115  1.1  jakllsch 
    116  1.1  jakllsch 	return 0;
    117  1.1  jakllsch }
    118  1.1  jakllsch 
    119  1.1  jakllsch static int
    120  1.1  jakllsch pcf8563rtc_clock_read(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
    121  1.1  jakllsch {
    122  1.1  jakllsch 	uint8_t bcd[PCF8563_NREGS];
    123  1.1  jakllsch 	uint8_t reg = PCF8563_R_CS1;
    124  1.1  jakllsch 
    125  1.1  jakllsch 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    126  1.1  jakllsch 		device_printf(sc->sc_dev, "acquire bus for read failed\n");
    127  1.1  jakllsch 		return 0;
    128  1.1  jakllsch 	}
    129  1.1  jakllsch 
    130  1.1  jakllsch 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, &reg, 1,
    131  1.1  jakllsch 		     &bcd[reg], PCF8563_R_YEAR - reg + 1, I2C_F_POLL)) {
    132  1.1  jakllsch 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    133  1.1  jakllsch 		device_printf(sc->sc_dev, "read failed\n");
    134  1.1  jakllsch 		return 0;
    135  1.1  jakllsch 	}
    136  1.1  jakllsch 
    137  1.1  jakllsch 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    138  1.1  jakllsch 
    139  1.1  jakllsch 	dt->dt_sec = FROMBCD(bcd[PCF8563_R_SECOND] & PCF8563_M_SECOND);
    140  1.1  jakllsch 	dt->dt_min = FROMBCD(bcd[PCF8563_R_MINUTE] & PCF8563_M_MINUTE);
    141  1.1  jakllsch 	dt->dt_hour = FROMBCD(bcd[PCF8563_R_HOUR] & PCF8563_M_HOUR);
    142  1.1  jakllsch 	dt->dt_day = FROMBCD(bcd[PCF8563_R_DAY] & PCF8563_M_DAY);
    143  1.1  jakllsch 	dt->dt_mon = FROMBCD(bcd[PCF8563_R_MONTH] & PCF8563_M_MONTH);
    144  1.1  jakllsch 	dt->dt_year = FROMBCD(bcd[PCF8563_R_YEAR] & PCF8563_M_YEAR);
    145  1.1  jakllsch 	dt->dt_year += 2000;
    146  1.1  jakllsch 
    147  1.1  jakllsch 	return 1;
    148  1.1  jakllsch }
    149  1.1  jakllsch 
    150  1.1  jakllsch static int
    151  1.1  jakllsch pcf8563rtc_clock_write(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
    152  1.1  jakllsch {
    153  1.1  jakllsch 	uint8_t bcd[PCF8563_NREGS];
    154  1.1  jakllsch 	uint8_t reg = PCF8563_R_SECOND;
    155  1.1  jakllsch 
    156  1.1  jakllsch 	bcd[PCF8563_R_SECOND] = TOBCD(dt->dt_sec);
    157  1.1  jakllsch 	bcd[PCF8563_R_MINUTE] = TOBCD(dt->dt_min);
    158  1.1  jakllsch 	bcd[PCF8563_R_HOUR] = TOBCD(dt->dt_hour);
    159  1.1  jakllsch 	bcd[PCF8563_R_DAY] = TOBCD(dt->dt_day);
    160  1.1  jakllsch 	bcd[PCF8563_R_WEEKDAY] = TOBCD(dt->dt_wday);
    161  1.1  jakllsch 	bcd[PCF8563_R_MONTH] = TOBCD(dt->dt_mon);
    162  1.1  jakllsch 	bcd[PCF8563_R_YEAR] = TOBCD(dt->dt_year % 100);
    163  1.1  jakllsch 
    164  1.1  jakllsch 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    165  1.1  jakllsch 		device_printf(sc->sc_dev, "acquire bus for write failed\n");
    166  1.1  jakllsch 		return 0;
    167  1.1  jakllsch 	}
    168  1.1  jakllsch 
    169  1.1  jakllsch 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, &reg, 1,
    170  1.1  jakllsch 		     &bcd[reg], PCF8563_R_YEAR - reg + 1, I2C_F_POLL)) {
    171  1.1  jakllsch 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    172  1.1  jakllsch 		device_printf(sc->sc_dev, "write failed\n");
    173  1.1  jakllsch 		return 0;
    174  1.1  jakllsch 	}
    175  1.1  jakllsch 
    176  1.1  jakllsch 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    177  1.1  jakllsch 
    178  1.1  jakllsch 	return 1;
    179  1.1  jakllsch }
    180