2 * Copyright (c) 2006 Sam Leffler. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 * Dallas Semiconductor DS1672 RTC sitting on the I2C bus.
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/clock.h>
37 #include <sys/resource.h>
40 #include <dev/iicbus/iiconf.h>
42 #include "iicbus_if.h"
45 #define IIC_M_WR 0 /* write operation */
47 #define DS1672_ADDR 0xd0 /* slave address */
49 #define DS1672_COUNTER 0 /* counter (bytes 0-3) */
50 #define DS1672_CTRL 4 /* control (1 byte) */
51 #define DS1672_TRICKLE 5 /* trickle charger (1 byte) */
53 #define NANOSEC 1000000000
60 ds1672_probe(device_t dev)
62 /* XXX really probe? */
63 device_set_desc(dev, "Dallas Semiconductor DS1672 RTC");
68 ds1672_attach(device_t dev)
70 struct ds1672_softc *sc = device_get_softc(dev);
74 clock_register(dev, 1000);
79 ds1672_gettime(device_t dev, struct timespec *ts)
81 uint8_t addr[1] = { DS1672_COUNTER };
83 struct iic_msg msgs[2] = {
84 { DS1672_ADDR, IIC_M_WR, 1, addr },
85 { DS1672_ADDR, IIC_M_RD, 4, secs },
89 error = iicbus_transfer(dev, msgs, 2);
91 /* counter has seconds since epoch */
92 ts->tv_sec = (secs[3] << 24) | (secs[2] << 16)
93 | (secs[1] << 8) | (secs[0] << 0);
94 ts->tv_nsec = NANOSEC / 2;
100 ds1672_settime(device_t dev, struct timespec *ts)
102 /* NB: register pointer precedes actual data */
103 uint8_t data[5] = { DS1672_COUNTER };
104 struct iic_msg msgs[1] = {
105 { DS1672_ADDR, IIC_M_WR, 5, data },
108 data[1] = (ts->tv_sec >> 0) & 0xff;
109 data[2] = (ts->tv_sec >> 8) & 0xff;
110 data[3] = (ts->tv_sec >> 16) & 0xff;
111 data[4] = (ts->tv_sec >> 24) & 0xff;
113 return iicbus_transfer(dev, msgs, 1);
116 static device_method_t ds1672_methods[] = {
117 DEVMETHOD(device_probe, ds1672_probe),
118 DEVMETHOD(device_attach, ds1672_attach),
120 DEVMETHOD(clock_gettime, ds1672_gettime),
121 DEVMETHOD(clock_settime, ds1672_settime),
126 static driver_t ds1672_driver = {
129 sizeof(struct ds1672_softc),
131 static devclass_t ds1672_devclass;
133 DRIVER_MODULE(ds1672, iicbus, ds1672_driver, ds1672_devclass, 0, 0);
134 MODULE_VERSION(ds1672, 1);
135 MODULE_DEPEND(ds1672, iicbus, 1, 1, 1);