FreeBSD kernel IICBUS device code
ds1307.c
Go to the documentation of this file.
1/*-
2 * Copyright (c) 2015 Luiz Otavio O Souza <loos@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30/*
31 * Driver for Maxim DS1307 I2C real-time clock/calendar.
32 */
33
34#include "opt_platform.h"
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/bus.h>
39#include <sys/clock.h>
40#include <sys/kernel.h>
41#include <sys/module.h>
42#include <sys/sysctl.h>
43
44#include <dev/iicbus/iicbus.h>
45#include <dev/iicbus/iiconf.h>
46#ifdef FDT
47#include <dev/ofw/openfirm.h>
48#include <dev/ofw/ofw_bus.h>
49#include <dev/ofw/ofw_bus_subr.h>
50#endif
51
53
54#include "clock_if.h"
55#include "iicbus_if.h"
56
58 device_t sc_dev;
59 struct intr_config_hook
61 uint8_t sc_ctrl;
64};
65
66static void ds1307_start(void *);
67
68#ifdef FDT
69static const struct ofw_compat_data ds1307_compat_data[] = {
70 {"dallas,ds1307", (uintptr_t)"Dallas DS1307 RTC"},
71 {"maxim,ds1307", (uintptr_t)"Maxim DS1307 RTC"},
72 {"microchip,mcp7941x", (uintptr_t)"Microchip MCP7941x RTC"},
73 { NULL, 0 }
74};
75#endif
76
77static int
78ds1307_read1(device_t dev, uint8_t reg, uint8_t *data)
79{
80
81 return (iicdev_readfrom(dev, reg, data, 1, IIC_INTRWAIT));
82}
83
84static int
85ds1307_write1(device_t dev, uint8_t reg, uint8_t data)
86{
87
88 return (iicdev_writeto(dev, reg, &data, 1, IIC_INTRWAIT));
89}
90
91static int
93{
94 int error;
95
96 sc->sc_ctrl = 0;
97 error = ds1307_read1(sc->sc_dev, DS1307_CONTROL, &sc->sc_ctrl);
98 if (error) {
99 device_printf(sc->sc_dev, "cannot read from RTC.\n");
100 return (error);
101 }
102
103 return (0);
104}
105
106static int
108{
109 int error;
110 uint8_t ctrl;
111
112 ctrl = sc->sc_ctrl & DS1307_CTRL_MASK;
113 error = ds1307_write1(sc->sc_dev, DS1307_CONTROL, ctrl);
114 if (error != 0)
115 device_printf(sc->sc_dev, "cannot write to RTC.\n");
116
117 return (error);
118}
119
120static int
121ds1307_sqwe_sysctl(SYSCTL_HANDLER_ARGS)
122{
123 int sqwe, error, newv, sqwe_bit;
124 struct ds1307_softc *sc;
125
126 sc = (struct ds1307_softc *)arg1;
127 error = ds1307_ctrl_read(sc);
128 if (error != 0)
129 return (error);
130 if (sc->sc_mcp7941x)
131 sqwe_bit = MCP7941X_CTRL_SQWE;
132 else
133 sqwe_bit = DS1307_CTRL_SQWE;
134 sqwe = newv = (sc->sc_ctrl & sqwe_bit) ? 1 : 0;
135 error = sysctl_handle_int(oidp, &newv, 0, req);
136 if (error != 0 || req->newptr == NULL)
137 return (error);
138 if (sqwe != newv) {
139 sc->sc_ctrl &= ~sqwe_bit;
140 if (newv)
141 sc->sc_ctrl |= sqwe_bit;
142 error = ds1307_ctrl_write(sc);
143 if (error != 0)
144 return (error);
145 }
146
147 return (error);
148}
149
150static int
151ds1307_sqw_freq_sysctl(SYSCTL_HANDLER_ARGS)
152{
153 int ds1307_sqw_freq[] = { 1, 4096, 8192, 32768 };
154 int error, freq, i, newf, tmp;
155 struct ds1307_softc *sc;
156
157 sc = (struct ds1307_softc *)arg1;
158 error = ds1307_ctrl_read(sc);
159 if (error != 0)
160 return (error);
161 tmp = (sc->sc_ctrl & DS1307_CTRL_RS_MASK);
162 if (tmp >= nitems(ds1307_sqw_freq))
163 tmp = nitems(ds1307_sqw_freq) - 1;
164 freq = ds1307_sqw_freq[tmp];
165 error = sysctl_handle_int(oidp, &freq, 0, req);
166 if (error != 0 || req->newptr == NULL)
167 return (error);
168 if (freq != ds1307_sqw_freq[tmp]) {
169 newf = 0;
170 for (i = 0; i < nitems(ds1307_sqw_freq); i++)
171 if (freq >= ds1307_sqw_freq[i])
172 newf = i;
173 sc->sc_ctrl &= ~DS1307_CTRL_RS_MASK;
174 sc->sc_ctrl |= newf;
175 error = ds1307_ctrl_write(sc);
176 if (error != 0)
177 return (error);
178 }
179
180 return (error);
181}
182
183static int
184ds1307_sqw_out_sysctl(SYSCTL_HANDLER_ARGS)
185{
186 int sqwe, error, newv;
187 struct ds1307_softc *sc;
188
189 sc = (struct ds1307_softc *)arg1;
190 error = ds1307_ctrl_read(sc);
191 if (error != 0)
192 return (error);
193 sqwe = newv = (sc->sc_ctrl & DS1307_CTRL_OUT) ? 1 : 0;
194 error = sysctl_handle_int(oidp, &newv, 0, req);
195 if (error != 0 || req->newptr == NULL)
196 return (error);
197 if (sqwe != newv) {
198 sc->sc_ctrl &= ~DS1307_CTRL_OUT;
199 if (newv)
201 error = ds1307_ctrl_write(sc);
202 if (error != 0)
203 return (error);
204 }
205
206 return (error);
207}
208
209static int
211{
212#ifdef FDT
213 const struct ofw_compat_data *compat;
214
215 if (!ofw_bus_status_okay(dev))
216 return (ENXIO);
217
218 compat = ofw_bus_search_compatible(dev, ds1307_compat_data);
219 if (compat->ocd_str != NULL) {
220 device_set_desc(dev, (const char *)compat->ocd_data);
221 return (BUS_PROBE_DEFAULT);
222 }
223#endif
224 device_set_desc(dev, "Maxim DS1307 RTC");
225 return (BUS_PROBE_NOWILDCARD);
226}
227
228static int
230{
231 struct ds1307_softc *sc;
232
233 sc = device_get_softc(dev);
234 sc->sc_dev = dev;
235 sc->enum_hook.ich_func = ds1307_start;
236 sc->enum_hook.ich_arg = dev;
237
238#ifdef FDT
239 if (ofw_bus_is_compatible(dev, "microchip,mcp7941x"))
240 sc->sc_mcp7941x = 1;
241#endif
242
243 /*
244 * We have to wait until interrupts are enabled. Usually I2C read
245 * and write only works when the interrupts are available.
246 */
247 if (config_intrhook_establish(&sc->enum_hook) != 0)
248 return (ENOMEM);
249
250 return (0);
251}
252
253static int
255{
256
257 clock_unregister(dev);
258 return (0);
259}
260
261static void
262ds1307_start(void *xdev)
263{
264 device_t dev;
265 struct ds1307_softc *sc;
266 struct sysctl_ctx_list *ctx;
267 struct sysctl_oid *tree_node;
268 struct sysctl_oid_list *tree;
269 uint8_t secs;
270 uint8_t osc_en;
271
272 dev = (device_t)xdev;
273 sc = device_get_softc(dev);
274 ctx = device_get_sysctl_ctx(dev);
275 tree_node = device_get_sysctl_tree(dev);
276 tree = SYSCTL_CHILDREN(tree_node);
277
278 config_intrhook_disestablish(&sc->enum_hook);
279
280 /* Check if the oscillator is disabled. */
281 if (ds1307_read1(sc->sc_dev, DS1307_SECS, &secs) != 0) {
282 device_printf(sc->sc_dev, "cannot read from RTC.\n");
283 return;
284 }
285 if (sc->sc_mcp7941x)
286 osc_en = 0x80;
287 else
288 osc_en = 0x00;
289
290 if (((secs & DS1307_SECS_CH) ^ osc_en) != 0) {
291 device_printf(sc->sc_dev,
292 "WARNING: RTC clock stopped, check the battery.\n");
293 }
294
295 /* Configuration parameters. */
296 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "sqwe",
297 CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,
298 ds1307_sqwe_sysctl, "IU", "DS1307 square-wave enable");
299 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "sqw_freq",
300 CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,
302 "DS1307 square-wave output frequency");
303 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "sqw_out",
304 CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,
305 ds1307_sqw_out_sysctl, "IU", "DS1307 square-wave output state");
306
307 /*
308 * Register as a clock with 1 second resolution. Schedule the
309 * clock_settime() method to be called just after top-of-second;
310 * resetting the time resets top-of-second in the hardware.
311 */
312 clock_register_flags(dev, 1000000, CLOCKF_SETTIME_NO_ADJ);
313 clock_schedule(dev, 1);
314}
315
316static int
317ds1307_gettime(device_t dev, struct timespec *ts)
318{
319 int error;
320 struct bcd_clocktime bct;
321 struct ds1307_softc *sc;
322 uint8_t data[7], hourmask, st_mask;
323
324 sc = device_get_softc(dev);
325 error = iicdev_readfrom(sc->sc_dev, DS1307_SECS, data, sizeof(data),
327 if (error != 0) {
328 device_printf(dev, "cannot read from RTC.\n");
329 return (error);
330 }
331
332 /* If the clock halted, we don't have good data. */
333 if (sc->sc_mcp7941x)
334 st_mask = 0x80;
335 else
336 st_mask = 0x00;
337
338 if (((data[DS1307_SECS] & DS1307_SECS_CH) ^ st_mask) != 0)
339 return (EINVAL);
340
341 /* If chip is in AM/PM mode remember that. */
343 sc->sc_use_ampm = true;
344 hourmask = DS1307_HOUR_MASK_12HR;
345 } else
346 hourmask = DS1307_HOUR_MASK_24HR;
347
348 bct.nsec = 0;
349 bct.ispm = (data[DS1307_HOUR] & DS1307_HOUR_IS_PM) != 0;
350 bct.sec = data[DS1307_SECS] & DS1307_SECS_MASK;
351 bct.min = data[DS1307_MINS] & DS1307_MINS_MASK;
352 bct.hour = data[DS1307_HOUR] & hourmask;
353 bct.day = data[DS1307_DATE] & DS1307_DATE_MASK;
355 bct.year = data[DS1307_YEAR] & DS1307_YEAR_MASK;
356
357 clock_dbgprint_bcd(sc->sc_dev, CLOCK_DBG_READ, &bct);
358 return (clock_bcd_to_ts(&bct, ts, sc->sc_use_ampm));
359}
360
361static int
362ds1307_settime(device_t dev, struct timespec *ts)
363{
364 struct bcd_clocktime bct;
365 struct ds1307_softc *sc;
366 int error, year;
367 uint8_t data[7];
368 uint8_t pmflags;
369
370 sc = device_get_softc(dev);
371
372 /*
373 * We request a timespec with no resolution-adjustment. That also
374 * disables utc adjustment, so apply that ourselves.
375 */
376 ts->tv_sec -= utc_offset();
377 clock_ts_to_bcd(ts, &bct, sc->sc_use_ampm);
378 clock_dbgprint_bcd(sc->sc_dev, CLOCK_DBG_WRITE, &bct);
379
380 /* If the chip is in AM/PM mode, adjust hour and set flags as needed. */
381 if (sc->sc_use_ampm) {
382 pmflags = DS1307_HOUR_USE_AMPM;
383 if (bct.ispm)
384 pmflags |= DS1307_HOUR_IS_PM;
385 } else
386 pmflags = 0;
387
388 data[DS1307_SECS] = bct.sec;
389 data[DS1307_MINS] = bct.min;
390 data[DS1307_HOUR] = bct.hour | pmflags;
391 data[DS1307_DATE] = bct.day;
392 data[DS1307_WEEKDAY] = bct.dow;
393 data[DS1307_MONTH] = bct.mon;
394 data[DS1307_YEAR] = bct.year & 0xff;
395 if (sc->sc_mcp7941x) {
398 year = bcd2bin(bct.year >> 8) * 100 + bcd2bin(bct.year & 0xff);
399 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
401 }
402 /* Write the time back to RTC. */
403 error = iicdev_writeto(sc->sc_dev, DS1307_SECS, data, sizeof(data),
405 if (error != 0)
406 device_printf(dev, "cannot write to RTC.\n");
407
408 return (error);
409}
410
411static device_method_t ds1307_methods[] = {
412 DEVMETHOD(device_probe, ds1307_probe),
413 DEVMETHOD(device_attach, ds1307_attach),
414 DEVMETHOD(device_detach, ds1307_detach),
415
416 DEVMETHOD(clock_gettime, ds1307_gettime),
417 DEVMETHOD(clock_settime, ds1307_settime),
418
419 DEVMETHOD_END
420};
421
422static driver_t ds1307_driver = {
423 "ds1307",
425 sizeof(struct ds1307_softc),
426};
427
428static devclass_t ds1307_devclass;
429
431MODULE_VERSION(ds1307, 1);
432MODULE_DEPEND(ds1307, iicbus, 1, 1, 1);
433IICBUS_FDT_PNP_INFO(ds1307_compat_data);
static devclass_t ds1307_devclass
Definition: ds1307.c:428
static int ds1307_gettime(device_t dev, struct timespec *ts)
Definition: ds1307.c:317
static int ds1307_detach(device_t dev)
Definition: ds1307.c:254
static int ds1307_probe(device_t dev)
Definition: ds1307.c:210
static int ds1307_read1(device_t dev, uint8_t reg, uint8_t *data)
Definition: ds1307.c:78
MODULE_VERSION(ds1307, 1)
static driver_t ds1307_driver
Definition: ds1307.c:422
static device_method_t ds1307_methods[]
Definition: ds1307.c:411
IICBUS_FDT_PNP_INFO(ds1307_compat_data)
static void ds1307_start(void *)
Definition: ds1307.c:262
static int ds1307_attach(device_t dev)
Definition: ds1307.c:229
static int ds1307_write1(device_t dev, uint8_t reg, uint8_t data)
Definition: ds1307.c:85
DRIVER_MODULE(ds1307, iicbus, ds1307_driver, ds1307_devclass, NULL, NULL)
__FBSDID("$FreeBSD$")
static int ds1307_ctrl_read(struct ds1307_softc *sc)
Definition: ds1307.c:92
MODULE_DEPEND(ds1307, iicbus, 1, 1, 1)
static int ds1307_sqwe_sysctl(SYSCTL_HANDLER_ARGS)
Definition: ds1307.c:121
static int ds1307_settime(device_t dev, struct timespec *ts)
Definition: ds1307.c:362
static int ds1307_sqw_freq_sysctl(SYSCTL_HANDLER_ARGS)
Definition: ds1307.c:151
static int ds1307_ctrl_write(struct ds1307_softc *sc)
Definition: ds1307.c:107
static int ds1307_sqw_out_sysctl(SYSCTL_HANDLER_ARGS)
Definition: ds1307.c:184
#define DS1307_CTRL_SQWE
Definition: ds1307reg.h:60
#define DS1307_YEAR_MASK
Definition: ds1307reg.h:56
#define DS1307_HOUR_MASK_24HR
Definition: ds1307reg.h:44
#define DS1307_CTRL_MASK
Definition: ds1307reg.h:64
#define MCP7941X_SECS_ST
Definition: ds1307reg.h:39
#define DS1307_HOUR_IS_PM
Definition: ds1307reg.h:45
#define DS1307_WEEKDAY
Definition: ds1307reg.h:47
#define DS1307_MONTH_MASK
Definition: ds1307reg.h:54
#define DS1307_MINS_MASK
Definition: ds1307reg.h:41
#define DS1307_SECS_CH
Definition: ds1307reg.h:38
#define DS1307_HOUR_USE_AMPM
Definition: ds1307reg.h:46
#define MCP7941X_CTRL_SQWE
Definition: ds1307reg.h:59
#define DS1307_CTRL_OUT
Definition: ds1307reg.h:58
#define DS1307_HOUR
Definition: ds1307reg.h:42
#define DS1307_CONTROL
Definition: ds1307reg.h:57
#define MCP7941X_MONTH_LPYR
Definition: ds1307reg.h:53
#define DS1307_SECS_MASK
Definition: ds1307reg.h:37
#define DS1307_MONTH
Definition: ds1307reg.h:52
#define DS1307_DATE_MASK
Definition: ds1307reg.h:51
#define DS1307_HOUR_MASK_12HR
Definition: ds1307reg.h:43
#define DS1307_CTRL_RS_MASK
Definition: ds1307reg.h:63
#define DS1307_MINS
Definition: ds1307reg.h:40
#define MCP7941X_WEEKDAY_VBATEN
Definition: ds1307reg.h:48
#define DS1307_SECS
Definition: ds1307reg.h:36
#define DS1307_DATE
Definition: ds1307reg.h:50
#define DS1307_YEAR
Definition: ds1307reg.h:55
caddr_t data
Definition: iicbb_if.m:61
INTERFACE iicbus
Definition: iicbus_if.m:32
int iicdev_readfrom(device_t slavedev, uint8_t regaddr, void *buffer, uint16_t buflen, int waithow)
Definition: iiconf.c:524
int iicdev_writeto(device_t slavedev, uint8_t regaddr, void *buffer, uint16_t buflen, int waithow)
Definition: iiconf.c:549
#define IIC_INTRWAIT
Definition: iiconf.h:48
device_t dev
Definition: ofw_iicbus_if.m:38
struct intr_config_hook enum_hook
Definition: ds1307.c:59
device_t sc_dev
Definition: ds1307.c:58
uint8_t sc_ctrl
Definition: ds1307.c:61
bool sc_mcp7941x
Definition: ds1307.c:62
bool sc_use_ampm
Definition: ds1307.c:63