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