1 /* $OpenBSD: pcf8563.c,v 1.5 2021/11/22 20:19:23 kettenis Exp $ */ 2 3 /* 4 * Copyright (c) 2005 Kimihiro Nonaka 5 * Copyright (c) 2016, 2017 Mark Kettenis 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/device.h> 33 34 #include <dev/clock_subr.h> 35 36 #include <dev/i2c/i2cvar.h> 37 38 /* 39 * PCF8563 Real-Time Clock 40 */ 41 42 #define PCF8563_CONTROL1 0x00 43 #define PCF8563_CONTROL2 0x01 44 #define PCF8563_SECONDS 0x02 45 #define PCF8563_MINUTES 0x03 46 #define PCF8563_HOURS 0x04 47 #define PCF8563_DAY 0x05 48 #define PCF8563_WDAY 0x06 49 #define PCF8563_MONTH 0x07 50 #define PCF8563_YEAR 0x08 51 52 #define PCF8563_NREGS 12 53 #define PCF8563_NRTC_REGS 7 54 55 /* 56 * Bit definitions. 57 */ 58 #define PCF8563_CONTROL1_TESTC (1 << 3) 59 #define PCF8563_CONTROL1_STOP (1 << 5) 60 #define PCF8563_CONTROL1_TEST1 (1 << 1) 61 #define PCF8563_SECONDS_MASK 0x7f 62 #define PCF8563_SECONDS_VL (1 << 7) 63 #define PCF8563_MINUTES_MASK 0x7f 64 #define PCF8563_HOURS_MASK 0x3f 65 #define PCF8563_DAY_MASK 0x3f 66 #define PCF8563_WDAY_MASK 0x07 67 #define PCF8563_MONTH_MASK 0x1f 68 #define PCF8563_MONTH_C (1 << 7) 69 70 struct pcxrtc_softc { 71 struct device sc_dev; 72 i2c_tag_t sc_tag; 73 int sc_address; 74 struct todr_chip_handle sc_todr; 75 }; 76 77 int pcxrtc_match(struct device *, void *, void *); 78 void pcxrtc_attach(struct device *, struct device *, void *); 79 80 struct cfattach pcxrtc_ca = { 81 sizeof(struct pcxrtc_softc), pcxrtc_match, pcxrtc_attach 82 }; 83 84 struct cfdriver pcxrtc_cd = { 85 NULL, "pcxrtc", DV_DULL 86 }; 87 88 uint8_t pcxrtc_reg_read(struct pcxrtc_softc *, int); 89 void pcxrtc_reg_write(struct pcxrtc_softc *, int, uint8_t); 90 int pcxrtc_clock_read(struct pcxrtc_softc *, struct clock_ymdhms *); 91 int pcxrtc_clock_write(struct pcxrtc_softc *, struct clock_ymdhms *); 92 int pcxrtc_gettime(struct todr_chip_handle *, struct timeval *); 93 int pcxrtc_settime(struct todr_chip_handle *, struct timeval *); 94 95 int 96 pcxrtc_match(struct device *parent, void *v, void *arg) 97 { 98 struct i2c_attach_args *ia = arg; 99 100 if (strcmp(ia->ia_name, "nxp,pcf8563") == 0 || 101 strcmp(ia->ia_name, "haoyu,hym8563") == 0) 102 return (1); 103 104 return (0); 105 } 106 107 void 108 pcxrtc_attach(struct device *parent, struct device *self, void *arg) 109 { 110 struct pcxrtc_softc *sc = (struct pcxrtc_softc *)self; 111 struct i2c_attach_args *ia = arg; 112 uint8_t reg; 113 114 sc->sc_tag = ia->ia_tag; 115 sc->sc_address = ia->ia_addr; 116 sc->sc_todr.cookie = sc; 117 sc->sc_todr.todr_gettime = pcxrtc_gettime; 118 sc->sc_todr.todr_settime = pcxrtc_settime; 119 sc->sc_todr.todr_setwen = NULL; 120 121 #if 0 122 todr_attach(&sc->sc_todr); 123 #else 124 /* XXX */ 125 { 126 extern todr_chip_handle_t todr_handle; 127 todr_handle = &sc->sc_todr; 128 } 129 #endif 130 131 /* Enable. */ 132 reg = pcxrtc_reg_read(sc, PCF8563_CONTROL1); 133 reg &= ~PCF8563_CONTROL1_STOP; 134 pcxrtc_reg_write(sc, PCF8563_CONTROL1, reg); 135 136 /* Report battery status. */ 137 reg = pcxrtc_reg_read(sc, PCF8563_SECONDS); 138 printf(": battery %s\n", (reg & PCF8563_SECONDS_VL) ? "low" : "ok"); 139 } 140 141 int 142 pcxrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) 143 { 144 struct pcxrtc_softc *sc = ch->cookie; 145 struct clock_ymdhms dt; 146 147 memset(&dt, 0, sizeof(dt)); 148 if (pcxrtc_clock_read(sc, &dt) == 0) 149 return (-1); 150 151 tv->tv_sec = clock_ymdhms_to_secs(&dt); 152 tv->tv_usec = 0; 153 return (0); 154 } 155 156 int 157 pcxrtc_settime(struct todr_chip_handle *ch, struct timeval *tv) 158 { 159 struct pcxrtc_softc *sc = ch->cookie; 160 struct clock_ymdhms dt; 161 162 clock_secs_to_ymdhms(tv->tv_sec, &dt); 163 164 if (pcxrtc_clock_write(sc, &dt) == 0) 165 return (-1); 166 return (0); 167 } 168 169 uint8_t 170 pcxrtc_reg_read(struct pcxrtc_softc *sc, int reg) 171 { 172 uint8_t cmd = reg; 173 uint8_t val; 174 175 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 176 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address, 177 &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL)) { 178 iic_release_bus(sc->sc_tag, I2C_F_POLL); 179 printf("%s: pcxrtc_reg_read: failed to read reg%d\n", 180 sc->sc_dev.dv_xname, reg); 181 return 0; 182 } 183 iic_release_bus(sc->sc_tag, I2C_F_POLL); 184 return val; 185 } 186 187 void 188 pcxrtc_reg_write(struct pcxrtc_softc *sc, int reg, uint8_t val) 189 { 190 uint8_t cmd = reg; 191 192 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 193 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address, 194 &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL)) { 195 iic_release_bus(sc->sc_tag, I2C_F_POLL); 196 printf("%s: pcxrtc_reg_write: failed to write reg%d\n", 197 sc->sc_dev.dv_xname, reg); 198 return; 199 } 200 iic_release_bus(sc->sc_tag, I2C_F_POLL); 201 } 202 203 int 204 pcxrtc_clock_read(struct pcxrtc_softc *sc, struct clock_ymdhms *dt) 205 { 206 uint8_t regs[PCF8563_NRTC_REGS]; 207 uint8_t cmd = PCF8563_SECONDS; 208 209 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 210 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address, 211 &cmd, sizeof(cmd), regs, PCF8563_NRTC_REGS, I2C_F_POLL)) { 212 iic_release_bus(sc->sc_tag, I2C_F_POLL); 213 printf("%s: pcxrtc_clock_read: failed to read rtc\n", 214 sc->sc_dev.dv_xname); 215 return (0); 216 } 217 iic_release_bus(sc->sc_tag, I2C_F_POLL); 218 219 /* 220 * Convert the PCF8563's register values into something useable 221 */ 222 dt->dt_sec = FROMBCD(regs[0] & PCF8563_SECONDS_MASK); 223 dt->dt_min = FROMBCD(regs[1] & PCF8563_MINUTES_MASK); 224 dt->dt_hour = FROMBCD(regs[2] & PCF8563_HOURS_MASK); 225 dt->dt_day = FROMBCD(regs[3] & PCF8563_DAY_MASK); 226 dt->dt_mon = FROMBCD(regs[5] & PCF8563_MONTH_MASK); 227 dt->dt_year = FROMBCD(regs[6]) + 2000; 228 229 if ((regs[0] & PCF8563_SECONDS_VL)) 230 return (0); 231 232 return (1); 233 } 234 235 int 236 pcxrtc_clock_write(struct pcxrtc_softc *sc, struct clock_ymdhms *dt) 237 { 238 uint8_t regs[PCF8563_NRTC_REGS]; 239 uint8_t cmd = PCF8563_SECONDS; 240 241 /* 242 * Convert our time representation into something the PCF8563 243 * can understand. 244 */ 245 regs[0] = TOBCD(dt->dt_sec); 246 regs[1] = TOBCD(dt->dt_min); 247 regs[2] = TOBCD(dt->dt_hour); 248 regs[3] = TOBCD(dt->dt_day); 249 regs[4] = TOBCD(dt->dt_wday); 250 regs[5] = TOBCD(dt->dt_mon); 251 regs[6] = TOBCD(dt->dt_year - 2000); 252 253 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 254 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address, 255 &cmd, sizeof(cmd), regs, PCF8563_NRTC_REGS, I2C_F_POLL)) { 256 iic_release_bus(sc->sc_tag, I2C_F_POLL); 257 printf("%s: pcxrtc_clock_write: failed to write rtc\n", 258 sc->sc_dev.dv_xname); 259 return (0); 260 } 261 iic_release_bus(sc->sc_tag, I2C_F_POLL); 262 return (1); 263 } 264