xref: /openbsd-src/sys/dev/i2c/ds1307.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /*	$OpenBSD: ds1307.c,v 1.1 2016/06/20 13:42:42 mglocker Exp $ */
2 
3 /*
4  * Copyright (c) 2016 Marcus Glocker <mglocker@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/device.h>
22 
23 #include <dev/clock_subr.h>
24 #include <dev/i2c/i2cvar.h>
25 
26 /*
27  * Defines.
28  */
29 /* RTC Registers */
30 #define DS1307_SEC_REG		0x00
31 #define DS1307_SEC_MASK		0x7f
32 #define DS1307_SEC_MASK_CH	0x80	/* Clock Halt bit */
33 #define DS1307_SEC_BIT_CH	7	/* 0 = osc enabled, 1 = osc disabled */
34 #define DS1307_MIN_REG		0x01
35 #define DS1307_MIN_MASK		0x7f
36 #define DS1307_HOUR_REG		0x02
37 #define DS1307_HOUR_MASK	0x3f
38 #define DS1307_HOUR_MASK_MODE	0x40	/* Hour Mode bit */
39 #define DS1307_HOUR_BIT_MODE	6	/* 0 = 24h mode, 1 = 12h mode */
40 #define DS1307_WDAY_REG		0x03
41 #define DS1307_WDAY_MASK	0x07
42 #define DS1307_DATE_REG		0x04
43 #define DS1307_DATE_MASK	0x3f
44 #define DS1307_MONTH_REG	0x05
45 #define DS1307_MONTH_MASK	0x1f
46 #define DS1307_YEAR_REG		0x06
47 #define DS1307_YEAR_MASK	0xff
48 #define DS1307_CTRL_REG		0x07
49 
50 /* RAM Registers */
51 #define DS1307_RAM_REG		0x08	/* RAM address space 0x08 - 0x3f */
52 
53 /*
54  * Driver structure.
55  */
56 struct maxrtc_softc {
57 	struct device		sc_dev;
58 	i2c_tag_t		sc_tag;
59 	int			sc_addr;
60 	struct todr_chip_handle sc_todr;
61 };
62 
63 /*
64  * Prototypes.
65  */
66 int	maxrtc_match(struct device *, void *, void *);
67 void	maxrtc_attach(struct device *, struct device *, void *);
68 int	maxrtc_read(struct maxrtc_softc *, uint8_t *, uint8_t,
69 	    uint8_t *, uint8_t);
70 int	maxrtc_write(struct maxrtc_softc *, uint8_t *, uint8_t);
71 int	maxrtc_enable_osc(struct maxrtc_softc *);
72 int	maxrtc_set_24h_mode(struct maxrtc_softc *);
73 int	maxrtc_gettime(struct todr_chip_handle *, struct timeval *);
74 int	maxrtc_settime(struct todr_chip_handle *, struct timeval *);
75 int	maxrtc_getcal(struct todr_chip_handle *, int *);
76 int	maxrtc_setcal(struct todr_chip_handle *, int);
77 
78 /*
79  * Driver glue structures.
80  */
81 struct cfattach maxrtc_ca = {
82 	sizeof(struct maxrtc_softc), maxrtc_match, maxrtc_attach
83 };
84 
85 struct cfdriver maxrtc_cd = {
86 	NULL, "maxrtc", DV_DULL
87 };
88 
89 extern todr_chip_handle_t todr_handle;
90 
91 /*
92  * Functions.
93  */
94 int
95 maxrtc_match(struct device *parent, void *v, void *arg)
96 {
97 	struct i2c_attach_args *ia = arg;
98 
99 	if (strcmp(ia->ia_name, "ds1307") == 0)
100 		return (1);
101 
102 	return (0);
103 }
104 
105 void
106 maxrtc_attach(struct device *parent, struct device *self, void *arg)
107 {
108 	struct maxrtc_softc *sc = (struct maxrtc_softc *)self;
109 	struct i2c_attach_args *ia = arg;
110 
111 	sc->sc_tag = ia->ia_tag;
112 	sc->sc_addr = ia->ia_addr;
113 	sc->sc_todr.cookie = sc;
114 	sc->sc_todr.todr_gettime = maxrtc_gettime;
115 	sc->sc_todr.todr_settime = maxrtc_settime;
116 	sc->sc_todr.todr_getcal = maxrtc_getcal;
117 	sc->sc_todr.todr_setcal = maxrtc_setcal;
118 	sc->sc_todr.todr_setwen = NULL;
119 
120 	if (maxrtc_enable_osc(sc) == -1)
121 		return;
122 
123 	if (maxrtc_set_24h_mode(sc) == -1)
124 		return;
125 
126 	/* register our time handlers */
127 	if (todr_handle != NULL) {
128 		printf("%s: overwriting existing rtc handler\n",
129 		    sc->sc_dev.dv_xname);
130 	}
131 	/* XXX just overwrite existing rtc handler? */
132 	todr_handle = &sc->sc_todr;
133 }
134 
135 int
136 maxrtc_read(struct maxrtc_softc *sc, uint8_t *cmd, uint8_t cmd_len,
137     uint8_t *data, uint8_t data_len)
138 {
139 	int r;
140 
141 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
142 	if ((r = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
143 	    cmd, cmd_len, data, data_len, I2C_F_POLL)))
144 		printf("%s: maxrtc_read failed\n", sc->sc_dev.dv_xname);
145 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
146 
147 	return (r);
148 }
149 
150 int
151 maxrtc_write(struct maxrtc_softc *sc, uint8_t *data, uint8_t data_len)
152 {
153 	int r;
154 
155 	/*
156 	 * On write operation the DS1307 requires the target address to be
157 	 * stored in the first byte of the data packet.  Therefore we don't
158 	 * fill up the command packet here.
159 	 */
160 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
161 	if ((r = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
162 	    NULL, 0, data, data_len, I2C_F_POLL)))
163 		printf("%s: maxrtc_write failed\n", sc->sc_dev.dv_xname);
164 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
165 
166 	return (r);
167 }
168 
169 int
170 maxrtc_enable_osc(struct maxrtc_softc *sc)
171 {
172 	uint8_t cmd;
173 	uint8_t data_r;
174 	uint8_t data_w[2];
175 
176 	cmd = DS1307_SEC_REG;
177 	data_r = 0;
178 	if (maxrtc_read(sc, &cmd, sizeof(cmd), &data_r, sizeof(data_r))) {
179 		printf("%s: maxrtc_enable_osc failed\n", sc->sc_dev.dv_xname);
180 		return (-1);
181 	}
182 	if ((data_r & DS1307_SEC_MASK_CH) == 0) {
183 		/* oscilliator is already enabled */
184 		printf(": rtc is ok\n");
185 		return (0);
186 	}
187 	printf(": rtc was halted, check battery\n");
188 
189 	/* enable the oscillator */
190 	data_r |= 0 << DS1307_SEC_BIT_CH;
191 	data_w[0] = DS1307_SEC_REG;
192 	data_w[1] = data_r;
193 	if (maxrtc_write(sc, data_w, sizeof(data_w))) {
194 		printf("%s: maxrtc_enable_osc failed\n", sc->sc_dev.dv_xname);
195 		return (-1);
196 	}
197 
198 	return (0);
199 }
200 
201 int
202 maxrtc_set_24h_mode(struct maxrtc_softc *sc)
203 {
204 	uint8_t cmd;
205 	uint8_t data_r;
206 	uint8_t data_w[2];
207 
208 	cmd = DS1307_HOUR_REG;
209 	data_r = 0;
210 	if (maxrtc_read(sc, &cmd, sizeof(cmd), &data_r, sizeof(data_r))) {
211 		printf("%s: maxrtc_set_24h_mode failed\n", sc->sc_dev.dv_xname);
212 		return (-1);
213 	}
214 	if ((data_r & DS1307_HOUR_MASK_MODE) == 0) {
215 		/* 24h mode is already set */
216 		return (0);
217 	}
218 
219 	/* set 24h mode */
220 	data_r |= 0 << DS1307_HOUR_BIT_MODE;
221 	data_w[0] = DS1307_HOUR_REG;
222 	data_w[1] = data_r;
223 	if (maxrtc_write(sc, data_w, sizeof(data_w))) {
224 		printf("%s: maxrtc_set_24h_mode failed\n", sc->sc_dev.dv_xname);
225 		return (-1);
226 	}
227 
228 	return (0);
229 }
230 
231 int
232 maxrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
233 {
234 	struct maxrtc_softc *sc = ch->cookie;
235 	struct clock_ymdhms dt;
236 	uint8_t cmd;
237 	uint8_t data[7];
238 
239 	cmd = DS1307_SEC_REG;
240 	memset(data, 0, sizeof(data));
241 	if (maxrtc_read(sc, &cmd, sizeof(cmd), data, sizeof(data))) {
242 		printf("%s: maxrtc_gettime failed\n", sc->sc_dev.dv_xname);
243 		return (-1);
244 	}
245 
246 	dt.dt_sec = FROMBCD(data[DS1307_SEC_REG] & DS1307_SEC_MASK);
247 	dt.dt_min = FROMBCD(data[DS1307_MIN_REG] & DS1307_MIN_MASK);
248 	dt.dt_hour = FROMBCD(data[DS1307_HOUR_REG] & DS1307_HOUR_MASK);
249 	dt.dt_wday = FROMBCD(data[DS1307_WDAY_REG] & DS1307_WDAY_MASK);
250 	dt.dt_day = FROMBCD(data[DS1307_DATE_REG] & DS1307_DATE_MASK);
251 	dt.dt_mon = FROMBCD(data[DS1307_MONTH_REG] & DS1307_MONTH_MASK);
252 	dt.dt_year = FROMBCD(data[DS1307_YEAR_REG] & DS1307_YEAR_MASK) + 2000;
253 
254 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
255 	tv->tv_usec = 0;
256 
257 	return (0);
258 }
259 
260 int
261 maxrtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
262 {
263 	struct maxrtc_softc *sc = ch->cookie;
264 	struct clock_ymdhms dt;
265 	uint8_t data[8];
266 
267 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
268 
269 	data[0] = DS1307_SEC_REG;
270 	data[1] = TOBCD(dt.dt_sec);	/* this will also enable the osc */
271 	data[2] = TOBCD(dt.dt_min);
272 	data[3] = TOBCD(dt.dt_hour);	/* this will also set 24h mode */
273 	data[4] = TOBCD(dt.dt_wday);
274 	data[5] = TOBCD(dt.dt_day);
275 	data[6] = TOBCD(dt.dt_mon);
276 	data[7] = TOBCD(dt.dt_year - 2000);
277 	if (maxrtc_write(sc, data, sizeof(data))) {
278 		printf("%s: maxrtc_settime failed\n", sc->sc_dev.dv_xname);
279 		return (-1);
280 	}
281 
282 	return (0);
283 }
284 
285 int
286 maxrtc_getcal(struct todr_chip_handle *ch, int *cal)
287 {
288 	return (EOPNOTSUPP);
289 }
290 
291 int
292 maxrtc_setcal(struct todr_chip_handle *ch, int cal)
293 {
294 	return (EOPNOTSUPP);
295 }
296