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