1 /* $OpenBSD: isl1208.c,v 1.1 2018/04/07 18:16:35 kettenis Exp $ */ 2 /* 3 * Copyright (c) 2018 Mark Kettenis <kettenis@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/device.h> 21 #include <sys/malloc.h> 22 23 #include <dev/i2c/i2cvar.h> 24 25 #include <dev/clock_subr.h> 26 27 extern todr_chip_handle_t todr_handle; 28 29 #define ISL1208_SC 0x00 30 #define ISL1208_MN 0x01 31 #define ISL1208_HR 0x02 32 #define ISL1208_HR_HR21 (1 << 5) 33 #define ISL1208_HR_MIL (1 << 7) 34 #define ISL1208_DT 0x03 35 #define ISL1208_MO 0x04 36 #define ISL1208_YR 0x05 37 #define ISL1208_DW 0x06 38 #define ISL1208_SR 0x07 39 #define ISL1208_SR_RTCF (1 << 0) 40 #define ISL1208_SR_WRTC (1 << 4) 41 42 #define ISL1208_NRTC_REGS 7 43 44 struct islrtc_regdata { 45 const char *name; 46 uint8_t reg, mask; 47 uint32_t base, delta; 48 }; 49 50 struct islrtc_softc { 51 struct device sc_dev; 52 i2c_tag_t sc_tag; 53 i2c_addr_t sc_addr; 54 55 struct todr_chip_handle sc_todr; 56 }; 57 58 int islrtc_match(struct device *, void *, void *); 59 void islrtc_attach(struct device *, struct device *, void *); 60 61 struct cfattach islrtc_ca = { 62 sizeof(struct islrtc_softc), islrtc_match, islrtc_attach 63 }; 64 65 struct cfdriver islrtc_cd = { 66 NULL, "islrtc", DV_DULL 67 }; 68 69 uint8_t islrtc_reg_read(struct islrtc_softc *, int); 70 void islrtc_reg_write(struct islrtc_softc *, int, uint8_t); 71 int islrtc_clock_read(struct islrtc_softc *, struct clock_ymdhms *); 72 int islrtc_clock_write(struct islrtc_softc *, struct clock_ymdhms *); 73 int islrtc_gettime(struct todr_chip_handle *, struct timeval *); 74 int islrtc_settime(struct todr_chip_handle *, struct timeval *); 75 76 int 77 islrtc_match(struct device *parent, void *match, void *aux) 78 { 79 struct i2c_attach_args *ia = aux; 80 81 if (strcmp(ia->ia_name, "isil,isl1208") == 0 || 82 strcmp(ia->ia_name, "isil,isl1218") == 0) 83 return 1; 84 85 return 0; 86 } 87 88 void 89 islrtc_attach(struct device *parent, struct device *self, void *aux) 90 { 91 struct islrtc_softc *sc = (struct islrtc_softc *)self; 92 struct i2c_attach_args *ia = aux; 93 94 sc->sc_tag = ia->ia_tag; 95 sc->sc_addr = ia->ia_addr; 96 97 sc->sc_todr.cookie = sc; 98 sc->sc_todr.todr_gettime = islrtc_gettime; 99 sc->sc_todr.todr_settime = islrtc_settime; 100 todr_handle = &sc->sc_todr; 101 102 printf("\n"); 103 } 104 105 int 106 islrtc_gettime(struct todr_chip_handle *handle, struct timeval *tv) 107 { 108 struct islrtc_softc *sc = handle->cookie; 109 struct clock_ymdhms dt; 110 int error; 111 112 error = islrtc_clock_read(sc, &dt); 113 if (error) 114 return error; 115 116 if (dt.dt_sec > 59 || dt.dt_min > 59 || dt.dt_hour > 23 || 117 dt.dt_day > 31 || dt.dt_day == 0 || 118 dt.dt_mon > 12 || dt.dt_mon == 0 || 119 dt.dt_year < POSIX_BASE_YEAR) 120 return EINVAL; 121 122 tv->tv_sec = clock_ymdhms_to_secs(&dt); 123 tv->tv_usec = 0; 124 return 0; 125 } 126 127 int 128 islrtc_settime(struct todr_chip_handle *handle, struct timeval *tv) 129 { 130 struct islrtc_softc *sc = handle->cookie; 131 struct clock_ymdhms dt; 132 133 clock_secs_to_ymdhms(tv->tv_sec, &dt); 134 135 return islrtc_clock_write(sc, &dt); 136 } 137 138 uint8_t 139 islrtc_reg_read(struct islrtc_softc *sc, int reg) 140 { 141 uint8_t cmd = reg; 142 uint8_t val; 143 int error; 144 145 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 146 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 147 &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL); 148 iic_release_bus(sc->sc_tag, I2C_F_POLL); 149 150 if (error) { 151 printf("%s: can't read register 0x%02x\n", 152 sc->sc_dev.dv_xname, reg); 153 val = 0xff; 154 } 155 156 return val; 157 } 158 159 void 160 islrtc_reg_write(struct islrtc_softc *sc, int reg, uint8_t val) 161 { 162 uint8_t cmd = reg; 163 int error; 164 165 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 166 error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 167 &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL); 168 iic_release_bus(sc->sc_tag, I2C_F_POLL); 169 170 if (error) { 171 printf("%s: can't write register 0x%02x\n", 172 sc->sc_dev.dv_xname, reg); 173 } 174 } 175 176 int 177 islrtc_clock_read(struct islrtc_softc *sc, struct clock_ymdhms *dt) 178 { 179 uint8_t regs[ISL1208_NRTC_REGS]; 180 uint8_t cmd = ISL1208_SC; 181 uint8_t status; 182 int error; 183 184 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 185 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 186 &cmd, sizeof(cmd), regs, ISL1208_NRTC_REGS, I2C_F_POLL); 187 iic_release_bus(sc->sc_tag, I2C_F_POLL); 188 189 if (error) { 190 printf("%s: can't read RTC\n", sc->sc_dev.dv_xname); 191 return error; 192 } 193 194 /* 195 * Convert the ISL1208's register values into something useable. 196 */ 197 dt->dt_sec = FROMBCD(regs[0]); 198 dt->dt_min = FROMBCD(regs[1]); 199 if (regs[2] & ISL1208_HR_MIL) { 200 dt->dt_hour = FROMBCD(regs[2] & ~ISL1208_HR_MIL); 201 } else { 202 dt->dt_hour = FROMBCD(regs[2] & ~ISL1208_HR_HR21); 203 if (regs[2] & ISL1208_HR_HR21) 204 dt->dt_hour += 12; 205 } 206 dt->dt_day = FROMBCD(regs[3]); 207 dt->dt_mon = FROMBCD(regs[4]); 208 dt->dt_year = FROMBCD(regs[5]) + 2000; 209 210 /* Consider the time to be invalid if we lost power. */ 211 status = islrtc_reg_read(sc, ISL1208_SR); 212 if (status & ISL1208_SR_RTCF) 213 return EINVAL; 214 215 return 0; 216 } 217 218 int 219 islrtc_clock_write(struct islrtc_softc *sc, struct clock_ymdhms *dt) 220 { 221 uint8_t regs[ISL1208_NRTC_REGS]; 222 uint8_t cmd = ISL1208_SC; 223 uint8_t reg; 224 int error; 225 226 /* 227 * Convert our time representation into something the ISL1208 228 * can understand. 229 */ 230 regs[0] = TOBCD(dt->dt_sec); 231 regs[1] = TOBCD(dt->dt_min); 232 regs[2] = TOBCD(dt->dt_hour) | ISL1208_HR_MIL; 233 regs[3] = TOBCD(dt->dt_day); 234 regs[4] = TOBCD(dt->dt_mon); 235 regs[5] = TOBCD(dt->dt_year - 2000); 236 regs[6] = TOBCD(dt->dt_wday); 237 238 /* Stop RTC such that we can write to it. */ 239 reg = islrtc_reg_read(sc, ISL1208_SR); 240 if (reg == 0xff) { 241 error = EIO; 242 goto fail; 243 } 244 islrtc_reg_write(sc, ISL1208_SR, reg | ISL1208_SR_WRTC); 245 246 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 247 error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 248 &cmd, sizeof(cmd), regs, ISL1208_NRTC_REGS, I2C_F_POLL); 249 iic_release_bus(sc->sc_tag, I2C_F_POLL); 250 251 /* Restart RTC. */ 252 islrtc_reg_write(sc, ISL1208_SR, reg & ~ISL1208_SR_WRTC); 253 254 fail: 255 if (error) { 256 printf("%s: can't write RTC\n", sc->sc_dev.dv_xname); 257 return error; 258 } 259 260 return 0; 261 } 262