2 * Copyright (c) 2012 Marius Strobl <marius@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Driver for NXP PCF8563 real-time clock/calendar
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/clock.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
41 #include <dev/iicbus/iicbus.h>
42 #include <dev/iicbus/iiconf.h>
43 #include <dev/iicbus/pcf8563reg.h>
46 #include "iicbus_if.h"
48 #define PCF8563_NCLOCKREGS (PCF8563_R_YEAR - PCF8563_R_CS1 + 1)
50 struct pcf8563_softc {
52 #define PCF8563_CPOL (1 << 0) /* PCF8563_R_MONTH_C means 19xx */
53 uint16_t sc_addr; /* PCF8563 slave address */
54 uint16_t sc_year0; /* TOD clock year 0 */
57 static device_attach_t pcf8563_attach;
58 static device_probe_t pcf8563_probe;
59 static clock_gettime_t pcf8563_gettime;
60 static clock_settime_t pcf8563_settime;
63 pcf8563_probe(device_t dev)
66 device_set_desc(dev, "NXP PCF8563 RTC");
67 return (BUS_PROBE_NOWILDCARD);
71 pcf8563_attach(device_t dev)
73 uint8_t reg = PCF8563_R_SECOND, val;
74 struct iic_msg msgs[] = {
75 { 0, IIC_M_WR, sizeof(reg), ® },
76 { 0, IIC_M_RD, sizeof(val), &val }
78 struct pcf8563_softc *sc;
81 sc = device_get_softc(dev);
82 sc->sc_addr = iicbus_get_addr(dev);
84 sc->sc_addr = PCF8563_ADDR;
87 * NB: PCF8563_R_SECOND_VL doesn't automatically clear when VDD
88 * rises above Vlow again and needs to be cleared manually.
89 * However, apparently this needs all of the time registers to be
90 * set, i.e. pcf8563_settime(), and not just PCF8563_R_SECOND in
91 * order for PCF8563_R_SECOND_VL to stick. Thus, we just issue a
92 * warning here rather than failing with ENXIO in case it is set.
93 * Note that pcf8563_settime() will also clear PCF8563_R_SECOND_VL
96 msgs[0].slave = msgs[1].slave = sc->sc_addr;
97 error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) /
100 device_printf(dev, "%s: cannot read RTC\n", __func__);
103 if ((val & PCF8563_R_SECOND_VL) != 0)
104 device_printf(dev, "%s: battery low\n", __func__);
107 clock_register(dev, 1000000); /* 1 second resolution */
112 pcf8563_gettime(device_t dev, struct timespec *ts)
115 uint8_t reg = PCF8563_R_SECOND, val[PCF8563_NCLOCKREGS];
116 struct iic_msg msgs[] = {
117 { 0, IIC_M_WR, sizeof(reg), ® },
118 { 0, IIC_M_RD, PCF8563_NCLOCKREGS, &val[PCF8563_R_SECOND] }
120 struct pcf8563_softc *sc;
123 sc = device_get_softc(dev);
124 msgs[0].slave = msgs[1].slave = sc->sc_addr;
125 error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) /
128 device_printf(dev, "%s: cannot read RTC\n", __func__);
133 ct.sec = FROMBCD(val[PCF8563_R_SECOND] & PCF8563_M_SECOND);
134 ct.min = FROMBCD(val[PCF8563_R_MINUTE] & PCF8563_M_MINUTE);
135 ct.hour = FROMBCD(val[PCF8563_R_HOUR] & PCF8563_M_HOUR);
136 ct.day = FROMBCD(val[PCF8563_R_DAY] & PCF8563_M_DAY);
137 ct.dow = val[PCF8563_R_WEEKDAY] & PCF8563_M_WEEKDAY;
138 ct.mon = FROMBCD(val[PCF8563_R_MONTH] & PCF8563_M_MONTH);
139 ct.year = FROMBCD(val[PCF8563_R_YEAR] & PCF8563_M_YEAR);
140 ct.year += sc->sc_year0;
141 if (ct.year < POSIX_BASE_YEAR)
142 ct.year += 100; /* assume [1970, 2069] */
143 if ((val[PCF8563_R_MONTH] & PCF8563_R_MONTH_C) != 0) {
144 if (ct.year >= 100 + sc->sc_year0)
145 sc->sc_flags |= PCF8563_CPOL;
146 } else if (ct.year < 100 + sc->sc_year0)
147 sc->sc_flags |= PCF8563_CPOL;
148 return (clock_ct_to_ts(&ct, ts));
152 pcf8563_settime(device_t dev, struct timespec *ts)
155 uint8_t val[PCF8563_NCLOCKREGS];
156 struct iic_msg msgs[] = {
157 { 0, IIC_M_WR, PCF8563_NCLOCKREGS - 1, &val[PCF8563_R_CS2] }
159 struct pcf8563_softc *sc;
162 sc = device_get_softc(dev);
163 val[PCF8563_R_CS2] = PCF8563_R_SECOND; /* abuse */
164 /* Accuracy is only one second. */
165 if (ts->tv_nsec >= 500000000)
168 clock_ts_to_ct(ts, &ct);
169 val[PCF8563_R_SECOND] = TOBCD(ct.sec);
170 val[PCF8563_R_MINUTE] = TOBCD(ct.min);
171 val[PCF8563_R_HOUR] = TOBCD(ct.hour);
172 val[PCF8563_R_DAY] = TOBCD(ct.day);
173 val[PCF8563_R_WEEKDAY] = ct.dow;
174 val[PCF8563_R_MONTH] = TOBCD(ct.mon);
175 val[PCF8563_R_YEAR] = TOBCD(ct.year % 100);
176 if ((sc->sc_flags & PCF8563_CPOL) != 0) {
177 if (ct.year >= 100 + sc->sc_year0)
178 val[PCF8563_R_MONTH] |= PCF8563_R_MONTH_C;
179 } else if (ct.year < 100 + sc->sc_year0)
180 val[PCF8563_R_MONTH] |= PCF8563_R_MONTH_C;
182 msgs[0].slave = sc->sc_addr;
183 error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) /
186 device_printf(dev, "%s: cannot write RTC\n", __func__);
190 static device_method_t pcf8563_methods[] = {
191 DEVMETHOD(device_probe, pcf8563_probe),
192 DEVMETHOD(device_attach, pcf8563_attach),
194 DEVMETHOD(clock_gettime, pcf8563_gettime),
195 DEVMETHOD(clock_settime, pcf8563_settime),
200 static driver_t pcf8563_driver = {
203 sizeof(struct pcf8563_softc),
206 static devclass_t pcf8563_devclass;
208 DRIVER_MODULE(pcf8563, iicbus, pcf8563_driver, pcf8563_devclass, NULL, NULL);
209 MODULE_VERSION(pcf8563, 1);
210 MODULE_DEPEND(pcf8563, iicbus, 1, 1, 1);