Home | History | Annotate | Line # | Download | only in i2c
pcf8563.c revision 1.12
      1 /*	$NetBSD: pcf8563.c,v 1.12 2020/01/02 16:48:05 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.12 2020/01/02 16:48:05 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 struct device_compatible_entry compat_data[] = {
     52 	{ "nxp,pcf8563",		0 },
     53 	{ "pcf8563rtc",			0 },
     54 	{ NULL,				0 }
     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, compat_data, &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 	int error;
     97 
     98 	aprint_naive(": Real-time Clock\n");
     99 	aprint_normal(": NXP PCF8563 Real-time Clock\n");
    100 
    101 	sc->sc_dev = self;
    102 	sc->sc_tag = ia->ia_tag;
    103 	sc->sc_addr = ia->ia_addr;
    104 	sc->sc_todr.cookie = sc;
    105 	sc->sc_todr.todr_gettime_ymdhms = pcf8563rtc_gettime;
    106 	sc->sc_todr.todr_settime_ymdhms = pcf8563rtc_settime;
    107 	sc->sc_todr.todr_setwen = NULL;
    108 
    109 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
    110 		aprint_error_dev(sc->sc_dev,
    111 		    "failed to acquire bus for attach\n");
    112 		return;
    113 	}
    114 	if ((error = iic_smbus_write_byte(sc->sc_tag, sc->sc_addr,
    115 					  PCF8563_R_CS1, 0, 0)) == 0) {
    116 		error = iic_smbus_write_byte(sc->sc_tag, sc->sc_addr,
    117 					     PCF8563_R_CS2, 0, 0);
    118 	}
    119 	iic_release_bus(sc->sc_tag, 0);
    120 	if (error) {
    121 		aprint_error_dev(sc->sc_dev, "failed to initialize RTC\n");
    122 		return;
    123 	}
    124 
    125 #ifdef FDT
    126 	fdtbus_todr_attach(self, ia->ia_cookie, &sc->sc_todr);
    127 #else
    128 	todr_attach(&sc->sc_todr);
    129 #endif
    130 }
    131 
    132 static int
    133 pcf8563rtc_gettime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    134 {
    135 	struct pcf8563rtc_softc *sc = ch->cookie;
    136 
    137 	return pcf8563rtc_clock_read(sc, dt);
    138 }
    139 
    140 static int
    141 pcf8563rtc_settime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    142 {
    143         struct pcf8563rtc_softc *sc = ch->cookie;
    144 
    145 	return pcf8563rtc_clock_write(sc, dt);
    146 }
    147 
    148 static int
    149 pcf8563rtc_clock_read(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
    150 {
    151 	uint8_t bcd[PCF8563_NREGS];
    152 	uint8_t reg = PCF8563_R_SECOND;
    153 	int error;
    154 
    155 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
    156 		device_printf(sc->sc_dev, "acquire bus for read failed\n");
    157 		return error;
    158 	}
    159 
    160 	if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    161 			      &reg, 1, &bcd[reg], PCF8563_R_YEAR - reg + 1,
    162 			      0)) != 0) {
    163 		iic_release_bus(sc->sc_tag, 0);
    164 		device_printf(sc->sc_dev, "read failed\n");
    165 		return error;
    166 	}
    167 
    168 	iic_release_bus(sc->sc_tag, 0);
    169 
    170 	if (bcd[PCF8563_R_SECOND] & PCF8563_M_VL)
    171 		return EIO;
    172 
    173 	dt->dt_sec = bcdtobin(bcd[PCF8563_R_SECOND] & PCF8563_M_SECOND);
    174 	dt->dt_min = bcdtobin(bcd[PCF8563_R_MINUTE] & PCF8563_M_MINUTE);
    175 	dt->dt_hour = bcdtobin(bcd[PCF8563_R_HOUR] & PCF8563_M_HOUR);
    176 	dt->dt_day = bcdtobin(bcd[PCF8563_R_DAY] & PCF8563_M_DAY);
    177 	dt->dt_wday = bcdtobin(bcd[PCF8563_R_WEEKDAY] & PCF8563_M_WEEKDAY);
    178 	dt->dt_mon = bcdtobin(bcd[PCF8563_R_MONTH] & PCF8563_M_MONTH);
    179 	dt->dt_year = 1900 +
    180 	    (bcdtobin(bcd[PCF8563_R_YEAR] & PCF8563_M_YEAR) % 100);
    181 	if ((bcd[PCF8563_R_MONTH] & PCF8563_M_CENTURY) == 0)
    182 		dt->dt_year += 100;
    183 
    184 	return error;
    185 }
    186 
    187 static int
    188 pcf8563rtc_clock_write(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
    189 {
    190 	uint8_t bcd[PCF8563_NREGS];
    191 	uint8_t reg = PCF8563_R_SECOND;
    192 	int error;
    193 
    194 	bcd[PCF8563_R_SECOND] = bintobcd(dt->dt_sec);
    195 	bcd[PCF8563_R_MINUTE] = bintobcd(dt->dt_min);
    196 	bcd[PCF8563_R_HOUR] = bintobcd(dt->dt_hour);
    197 	bcd[PCF8563_R_DAY] = bintobcd(dt->dt_day);
    198 	bcd[PCF8563_R_WEEKDAY] = bintobcd(dt->dt_wday);
    199 	bcd[PCF8563_R_MONTH] = bintobcd(dt->dt_mon);
    200 	bcd[PCF8563_R_YEAR] = bintobcd(dt->dt_year % 100);
    201 	if (dt->dt_year < 2000)
    202 		bcd[PCF8563_R_MONTH] |= PCF8563_M_CENTURY;
    203 
    204 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
    205 		device_printf(sc->sc_dev, "acquire bus for write failed\n");
    206 		return error;
    207 	}
    208 
    209 	if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, &reg, 1, &bcd[reg], PCF8563_R_YEAR - reg + 1, 0)) != 0) {
    210 		iic_release_bus(sc->sc_tag, 0);
    211 		device_printf(sc->sc_dev, "write failed\n");
    212 		return error;
    213 	}
    214 
    215 	iic_release_bus(sc->sc_tag, 0);
    216 
    217 	return error;
    218 }
    219