1 /* $OpenBSD: isl1208.c,v 1.5 2022/10/15 18:22:53 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
22 #include <dev/i2c/i2cvar.h>
23
24 #include <dev/clock_subr.h>
25
26 #define ISL1208_SC 0x00
27 #define ISL1208_MN 0x01
28 #define ISL1208_HR 0x02
29 #define ISL1208_HR_HR21 (1 << 5)
30 #define ISL1208_HR_MIL (1 << 7)
31 #define ISL1208_DT 0x03
32 #define ISL1208_MO 0x04
33 #define ISL1208_YR 0x05
34 #define ISL1208_DW 0x06
35 #define ISL1208_SR 0x07
36 #define ISL1208_SR_RTCF (1 << 0)
37 #define ISL1208_SR_WRTC (1 << 4)
38
39 #define ISL1208_NRTC_REGS 7
40
41 struct islrtc_softc {
42 struct device sc_dev;
43 i2c_tag_t sc_tag;
44 i2c_addr_t sc_addr;
45
46 struct todr_chip_handle sc_todr;
47 };
48
49 int islrtc_match(struct device *, void *, void *);
50 void islrtc_attach(struct device *, struct device *, void *);
51
52 const struct cfattach islrtc_ca = {
53 sizeof(struct islrtc_softc), islrtc_match, islrtc_attach
54 };
55
56 struct cfdriver islrtc_cd = {
57 NULL, "islrtc", DV_DULL
58 };
59
60 uint8_t islrtc_reg_read(struct islrtc_softc *, int);
61 void islrtc_reg_write(struct islrtc_softc *, int, uint8_t);
62 int islrtc_clock_read(struct islrtc_softc *, struct clock_ymdhms *);
63 int islrtc_clock_write(struct islrtc_softc *, struct clock_ymdhms *);
64 int islrtc_gettime(struct todr_chip_handle *, struct timeval *);
65 int islrtc_settime(struct todr_chip_handle *, struct timeval *);
66
67 int
islrtc_match(struct device * parent,void * match,void * aux)68 islrtc_match(struct device *parent, void *match, void *aux)
69 {
70 struct i2c_attach_args *ia = aux;
71
72 if (strcmp(ia->ia_name, "isil,isl1208") == 0 ||
73 strcmp(ia->ia_name, "isil,isl1218") == 0)
74 return 1;
75
76 return 0;
77 }
78
79 void
islrtc_attach(struct device * parent,struct device * self,void * aux)80 islrtc_attach(struct device *parent, struct device *self, void *aux)
81 {
82 struct islrtc_softc *sc = (struct islrtc_softc *)self;
83 struct i2c_attach_args *ia = aux;
84
85 sc->sc_tag = ia->ia_tag;
86 sc->sc_addr = ia->ia_addr;
87
88 sc->sc_todr.cookie = sc;
89 sc->sc_todr.todr_gettime = islrtc_gettime;
90 sc->sc_todr.todr_settime = islrtc_settime;
91 sc->sc_todr.todr_quality = 1000;
92 todr_attach(&sc->sc_todr);
93
94 printf("\n");
95 }
96
97 int
islrtc_gettime(struct todr_chip_handle * handle,struct timeval * tv)98 islrtc_gettime(struct todr_chip_handle *handle, struct timeval *tv)
99 {
100 struct islrtc_softc *sc = handle->cookie;
101 struct clock_ymdhms dt;
102 int error;
103
104 error = islrtc_clock_read(sc, &dt);
105 if (error)
106 return error;
107
108 if (dt.dt_sec > 59 || dt.dt_min > 59 || dt.dt_hour > 23 ||
109 dt.dt_day > 31 || dt.dt_day == 0 ||
110 dt.dt_mon > 12 || dt.dt_mon == 0 ||
111 dt.dt_year < POSIX_BASE_YEAR)
112 return EINVAL;
113
114 tv->tv_sec = clock_ymdhms_to_secs(&dt);
115 tv->tv_usec = 0;
116 return 0;
117 }
118
119 int
islrtc_settime(struct todr_chip_handle * handle,struct timeval * tv)120 islrtc_settime(struct todr_chip_handle *handle, struct timeval *tv)
121 {
122 struct islrtc_softc *sc = handle->cookie;
123 struct clock_ymdhms dt;
124
125 clock_secs_to_ymdhms(tv->tv_sec, &dt);
126
127 return islrtc_clock_write(sc, &dt);
128 }
129
130 uint8_t
islrtc_reg_read(struct islrtc_softc * sc,int reg)131 islrtc_reg_read(struct islrtc_softc *sc, int reg)
132 {
133 uint8_t cmd = reg;
134 uint8_t val;
135 int error;
136
137 iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
138 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
139 &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL);
140 iic_release_bus(sc->sc_tag, I2C_F_POLL);
141
142 if (error) {
143 printf("%s: can't read register 0x%02x\n",
144 sc->sc_dev.dv_xname, reg);
145 val = 0xff;
146 }
147
148 return val;
149 }
150
151 void
islrtc_reg_write(struct islrtc_softc * sc,int reg,uint8_t val)152 islrtc_reg_write(struct islrtc_softc *sc, int reg, uint8_t val)
153 {
154 uint8_t cmd = reg;
155 int error;
156
157 iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
158 error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
159 &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL);
160 iic_release_bus(sc->sc_tag, I2C_F_POLL);
161
162 if (error) {
163 printf("%s: can't write register 0x%02x\n",
164 sc->sc_dev.dv_xname, reg);
165 }
166 }
167
168 int
islrtc_clock_read(struct islrtc_softc * sc,struct clock_ymdhms * dt)169 islrtc_clock_read(struct islrtc_softc *sc, struct clock_ymdhms *dt)
170 {
171 uint8_t regs[ISL1208_NRTC_REGS];
172 uint8_t cmd = ISL1208_SC;
173 uint8_t status;
174 int error;
175
176 iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
177 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
178 &cmd, sizeof(cmd), regs, ISL1208_NRTC_REGS, I2C_F_POLL);
179 iic_release_bus(sc->sc_tag, I2C_F_POLL);
180
181 if (error) {
182 printf("%s: can't read RTC\n", sc->sc_dev.dv_xname);
183 return error;
184 }
185
186 /*
187 * Convert the ISL1208's register values into something useable.
188 */
189 dt->dt_sec = FROMBCD(regs[0]);
190 dt->dt_min = FROMBCD(regs[1]);
191 if (regs[2] & ISL1208_HR_MIL) {
192 dt->dt_hour = FROMBCD(regs[2] & ~ISL1208_HR_MIL);
193 } else {
194 dt->dt_hour = FROMBCD(regs[2] & ~ISL1208_HR_HR21);
195 if (regs[2] & ISL1208_HR_HR21)
196 dt->dt_hour += 12;
197 }
198 dt->dt_day = FROMBCD(regs[3]);
199 dt->dt_mon = FROMBCD(regs[4]);
200 dt->dt_year = FROMBCD(regs[5]) + 2000;
201
202 /* Consider the time to be invalid if we lost power. */
203 status = islrtc_reg_read(sc, ISL1208_SR);
204 if (status & ISL1208_SR_RTCF)
205 return EINVAL;
206
207 return 0;
208 }
209
210 int
islrtc_clock_write(struct islrtc_softc * sc,struct clock_ymdhms * dt)211 islrtc_clock_write(struct islrtc_softc *sc, struct clock_ymdhms *dt)
212 {
213 uint8_t regs[ISL1208_NRTC_REGS];
214 uint8_t cmd = ISL1208_SC;
215 uint8_t reg;
216 int error;
217
218 /*
219 * Convert our time representation into something the ISL1208
220 * can understand.
221 */
222 regs[0] = TOBCD(dt->dt_sec);
223 regs[1] = TOBCD(dt->dt_min);
224 regs[2] = TOBCD(dt->dt_hour) | ISL1208_HR_MIL;
225 regs[3] = TOBCD(dt->dt_day);
226 regs[4] = TOBCD(dt->dt_mon);
227 regs[5] = TOBCD(dt->dt_year - 2000);
228 regs[6] = TOBCD(dt->dt_wday);
229
230 /* Stop RTC such that we can write to it. */
231 reg = islrtc_reg_read(sc, ISL1208_SR);
232 if (reg == 0xff) {
233 error = EIO;
234 goto fail;
235 }
236 islrtc_reg_write(sc, ISL1208_SR, reg | ISL1208_SR_WRTC);
237
238 iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
239 error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
240 &cmd, sizeof(cmd), regs, ISL1208_NRTC_REGS, I2C_F_POLL);
241 iic_release_bus(sc->sc_tag, I2C_F_POLL);
242
243 /* Restart RTC. */
244 islrtc_reg_write(sc, ISL1208_SR, reg & ~ISL1208_SR_WRTC);
245
246 fail:
247 if (error) {
248 printf("%s: can't write RTC\n", sc->sc_dev.dv_xname);
249 return error;
250 }
251
252 return 0;
253 }
254