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