1 /* $OpenBSD: pcf8563.c,v 1.3 2021/01/13 11:01:19 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 #include <sys/kernel.h> 34 #include <sys/fcntl.h> 35 #include <sys/uio.h> 36 #include <sys/conf.h> 37 #include <sys/event.h> 38 39 #include <dev/clock_subr.h> 40 41 #include <dev/i2c/i2cvar.h> 42 43 /* 44 * PCF8563 Real-Time Clock 45 */ 46 47 #define PCF8563_CONTROL1 0x00 48 #define PCF8563_CONTROL2 0x01 49 #define PCF8563_SECONDS 0x02 50 #define PCF8563_MINUTES 0x03 51 #define PCF8563_HOURS 0x04 52 #define PCF8563_DAY 0x05 53 #define PCF8563_WDAY 0x06 54 #define PCF8563_MONTH 0x07 55 #define PCF8563_YEAR 0x08 56 57 #define PCF8563_NREGS 12 58 #define PCF8563_NRTC_REGS 7 59 60 /* 61 * Bit definitions. 62 */ 63 #define PCF8563_CONTROL1_TESTC (1 << 3) 64 #define PCF8563_CONTROL1_STOP (1 << 5) 65 #define PCF8563_CONTROL1_TEST1 (1 << 1) 66 #define PCF8563_SECONDS_MASK 0x7f 67 #define PCF8563_SECONDS_VL (1 << 7) 68 #define PCF8563_MINUTES_MASK 0x7f 69 #define PCF8563_HOURS_MASK 0x3f 70 #define PCF8563_DAY_MASK 0x3f 71 #define PCF8563_WDAY_MASK 0x07 72 #define PCF8563_MONTH_MASK 0x1f 73 #define PCF8563_MONTH_C (1 << 7) 74 75 struct pcxrtc_softc { 76 struct device sc_dev; 77 i2c_tag_t sc_tag; 78 int sc_address; 79 struct todr_chip_handle sc_todr; 80 }; 81 82 int pcxrtc_match(struct device *, void *, void *); 83 void pcxrtc_attach(struct device *, struct device *, void *); 84 85 struct cfattach pcxrtc_ca = { 86 sizeof(struct pcxrtc_softc), pcxrtc_match, pcxrtc_attach 87 }; 88 89 struct cfdriver pcxrtc_cd = { 90 NULL, "pcxrtc", DV_DULL 91 }; 92 93 uint8_t pcxrtc_reg_read(struct pcxrtc_softc *, int); 94 void pcxrtc_reg_write(struct pcxrtc_softc *, int, uint8_t); 95 int pcxrtc_clock_read(struct pcxrtc_softc *, struct clock_ymdhms *); 96 int pcxrtc_clock_write(struct pcxrtc_softc *, struct clock_ymdhms *); 97 int pcxrtc_gettime(struct todr_chip_handle *, struct timeval *); 98 int pcxrtc_settime(struct todr_chip_handle *, struct timeval *); 99 int pcxrtc_getcal(struct todr_chip_handle *, int *); 100 int pcxrtc_setcal(struct todr_chip_handle *, int); 101 102 int 103 pcxrtc_match(struct device *parent, void *v, void *arg) 104 { 105 struct i2c_attach_args *ia = arg; 106 107 if (strcmp(ia->ia_name, "nxp,pcf8563") == 0 || 108 strcmp(ia->ia_name, "haoyu,hym8563") == 0) 109 return (1); 110 111 return (0); 112 } 113 114 void 115 pcxrtc_attach(struct device *parent, struct device *self, void *arg) 116 { 117 struct pcxrtc_softc *sc = (struct pcxrtc_softc *)self; 118 struct i2c_attach_args *ia = arg; 119 uint8_t reg; 120 121 sc->sc_tag = ia->ia_tag; 122 sc->sc_address = ia->ia_addr; 123 sc->sc_todr.cookie = sc; 124 sc->sc_todr.todr_gettime = pcxrtc_gettime; 125 sc->sc_todr.todr_settime = pcxrtc_settime; 126 sc->sc_todr.todr_getcal = pcxrtc_getcal; 127 sc->sc_todr.todr_setcal = pcxrtc_setcal; 128 sc->sc_todr.todr_setwen = NULL; 129 130 #if 0 131 todr_attach(&sc->sc_todr); 132 #else 133 /* XXX */ 134 { 135 extern todr_chip_handle_t todr_handle; 136 todr_handle = &sc->sc_todr; 137 } 138 #endif 139 140 /* Enable. */ 141 reg = pcxrtc_reg_read(sc, PCF8563_CONTROL1); 142 reg &= ~PCF8563_CONTROL1_STOP; 143 pcxrtc_reg_write(sc, PCF8563_CONTROL1, reg); 144 145 /* Report battery status. */ 146 reg = pcxrtc_reg_read(sc, PCF8563_SECONDS); 147 printf(": battery %s\n", (reg & PCF8563_SECONDS_VL) ? "low" : "ok"); 148 } 149 150 int 151 pcxrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) 152 { 153 struct pcxrtc_softc *sc = ch->cookie; 154 struct clock_ymdhms dt; 155 156 memset(&dt, 0, sizeof(dt)); 157 if (pcxrtc_clock_read(sc, &dt) == 0) 158 return (-1); 159 160 tv->tv_sec = clock_ymdhms_to_secs(&dt); 161 tv->tv_usec = 0; 162 return (0); 163 } 164 165 int 166 pcxrtc_settime(struct todr_chip_handle *ch, struct timeval *tv) 167 { 168 struct pcxrtc_softc *sc = ch->cookie; 169 struct clock_ymdhms dt; 170 171 clock_secs_to_ymdhms(tv->tv_sec, &dt); 172 173 if (pcxrtc_clock_write(sc, &dt) == 0) 174 return (-1); 175 return (0); 176 } 177 178 int 179 pcxrtc_setcal(struct todr_chip_handle *ch, int cal) 180 { 181 return (EOPNOTSUPP); 182 } 183 184 int 185 pcxrtc_getcal(struct todr_chip_handle *ch, int *cal) 186 { 187 return (EOPNOTSUPP); 188 } 189 190 uint8_t 191 pcxrtc_reg_read(struct pcxrtc_softc *sc, int reg) 192 { 193 uint8_t cmd = reg; 194 uint8_t val; 195 196 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 197 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address, 198 &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL)) { 199 iic_release_bus(sc->sc_tag, I2C_F_POLL); 200 printf("%s: pcxrtc_reg_read: failed to read reg%d\n", 201 sc->sc_dev.dv_xname, reg); 202 return 0; 203 } 204 iic_release_bus(sc->sc_tag, I2C_F_POLL); 205 return val; 206 } 207 208 void 209 pcxrtc_reg_write(struct pcxrtc_softc *sc, int reg, uint8_t val) 210 { 211 uint8_t cmd = reg; 212 213 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 214 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address, 215 &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL)) { 216 iic_release_bus(sc->sc_tag, I2C_F_POLL); 217 printf("%s: pcxrtc_reg_write: failed to write reg%d\n", 218 sc->sc_dev.dv_xname, reg); 219 return; 220 } 221 iic_release_bus(sc->sc_tag, I2C_F_POLL); 222 } 223 224 int 225 pcxrtc_clock_read(struct pcxrtc_softc *sc, struct clock_ymdhms *dt) 226 { 227 uint8_t regs[PCF8563_NRTC_REGS]; 228 uint8_t cmd = PCF8563_SECONDS; 229 230 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 231 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address, 232 &cmd, sizeof(cmd), regs, PCF8563_NRTC_REGS, I2C_F_POLL)) { 233 iic_release_bus(sc->sc_tag, I2C_F_POLL); 234 printf("%s: pcxrtc_clock_read: failed to read rtc\n", 235 sc->sc_dev.dv_xname); 236 return (0); 237 } 238 iic_release_bus(sc->sc_tag, I2C_F_POLL); 239 240 /* 241 * Convert the PCF8563's register values into something useable 242 */ 243 dt->dt_sec = FROMBCD(regs[0] & PCF8563_SECONDS_MASK); 244 dt->dt_min = FROMBCD(regs[1] & PCF8563_MINUTES_MASK); 245 dt->dt_hour = FROMBCD(regs[2] & PCF8563_HOURS_MASK); 246 dt->dt_day = FROMBCD(regs[3] & PCF8563_DAY_MASK); 247 dt->dt_mon = FROMBCD(regs[5] & PCF8563_MONTH_MASK); 248 dt->dt_year = FROMBCD(regs[6]) + 2000; 249 250 if ((regs[0] & PCF8563_SECONDS_VL)) 251 return (0); 252 253 return (1); 254 } 255 256 int 257 pcxrtc_clock_write(struct pcxrtc_softc *sc, struct clock_ymdhms *dt) 258 { 259 uint8_t regs[PCF8563_NRTC_REGS]; 260 uint8_t cmd = PCF8563_SECONDS; 261 262 /* 263 * Convert our time representation into something the PCF8563 264 * can understand. 265 */ 266 regs[0] = TOBCD(dt->dt_sec); 267 regs[1] = TOBCD(dt->dt_min); 268 regs[2] = TOBCD(dt->dt_hour); 269 regs[3] = TOBCD(dt->dt_day); 270 regs[4] = TOBCD(dt->dt_wday); 271 regs[5] = TOBCD(dt->dt_mon); 272 regs[6] = TOBCD(dt->dt_year - 2000); 273 274 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 275 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address, 276 &cmd, sizeof(cmd), regs, PCF8563_NRTC_REGS, I2C_F_POLL)) { 277 iic_release_bus(sc->sc_tag, I2C_F_POLL); 278 printf("%s: pcxrtc_clock_write: failed to write rtc\n", 279 sc->sc_dev.dv_xname); 280 return (0); 281 } 282 iic_release_bus(sc->sc_tag, I2C_F_POLL); 283 return (1); 284 } 285