xref: /freebsd/sys/dev/iicbus/rtc/ds1672.c (revision 2f16049c985a364e2bd2b256f5bef9af17e10c62)
1*2f16049cSEmmanuel Vadot /*-
2*2f16049cSEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause
3*2f16049cSEmmanuel Vadot  *
4*2f16049cSEmmanuel Vadot  * Copyright (c) 2006 Sam Leffler.  All rights reserved.
5*2f16049cSEmmanuel Vadot  *
6*2f16049cSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
7*2f16049cSEmmanuel Vadot  * modification, are permitted provided that the following conditions
8*2f16049cSEmmanuel Vadot  * are met:
9*2f16049cSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
10*2f16049cSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
11*2f16049cSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
12*2f16049cSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
13*2f16049cSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
14*2f16049cSEmmanuel Vadot  *
15*2f16049cSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*2f16049cSEmmanuel Vadot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*2f16049cSEmmanuel Vadot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*2f16049cSEmmanuel Vadot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*2f16049cSEmmanuel Vadot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*2f16049cSEmmanuel Vadot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*2f16049cSEmmanuel Vadot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*2f16049cSEmmanuel Vadot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*2f16049cSEmmanuel Vadot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*2f16049cSEmmanuel Vadot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*2f16049cSEmmanuel Vadot  */
26*2f16049cSEmmanuel Vadot 
27*2f16049cSEmmanuel Vadot #include <sys/cdefs.h>
28*2f16049cSEmmanuel Vadot /*
29*2f16049cSEmmanuel Vadot  * Dallas Semiconductor DS1672 RTC sitting on the I2C bus.
30*2f16049cSEmmanuel Vadot  */
31*2f16049cSEmmanuel Vadot #include <sys/param.h>
32*2f16049cSEmmanuel Vadot #include <sys/systm.h>
33*2f16049cSEmmanuel Vadot #include <sys/kernel.h>
34*2f16049cSEmmanuel Vadot #include <sys/module.h>
35*2f16049cSEmmanuel Vadot #include <sys/clock.h>
36*2f16049cSEmmanuel Vadot #include <sys/time.h>
37*2f16049cSEmmanuel Vadot #include <sys/bus.h>
38*2f16049cSEmmanuel Vadot #include <sys/resource.h>
39*2f16049cSEmmanuel Vadot #include <sys/rman.h>
40*2f16049cSEmmanuel Vadot 
41*2f16049cSEmmanuel Vadot #include <dev/iicbus/iiconf.h>
42*2f16049cSEmmanuel Vadot 
43*2f16049cSEmmanuel Vadot #include "iicbus_if.h"
44*2f16049cSEmmanuel Vadot #include "clock_if.h"
45*2f16049cSEmmanuel Vadot 
46*2f16049cSEmmanuel Vadot #define	IIC_M_WR	0	/* write operation */
47*2f16049cSEmmanuel Vadot 
48*2f16049cSEmmanuel Vadot #define	DS1672_ADDR	0xd0	/* slave address */
49*2f16049cSEmmanuel Vadot 
50*2f16049cSEmmanuel Vadot #define	DS1672_COUNTER	0	/* counter (bytes 0-3) */
51*2f16049cSEmmanuel Vadot #define	DS1672_CTRL	4	/* control (1 byte) */
52*2f16049cSEmmanuel Vadot #define	DS1672_TRICKLE	5	/* trickle charger (1 byte) */
53*2f16049cSEmmanuel Vadot 
54*2f16049cSEmmanuel Vadot #define	DS1672_CTRL_EOSC	(1 << 7)	/* Stop/start flag. */
55*2f16049cSEmmanuel Vadot 
56*2f16049cSEmmanuel Vadot #define	MAX_IIC_DATA_SIZE	4
57*2f16049cSEmmanuel Vadot 
58*2f16049cSEmmanuel Vadot struct ds1672_softc {
59*2f16049cSEmmanuel Vadot 	device_t		sc_dev;
60*2f16049cSEmmanuel Vadot };
61*2f16049cSEmmanuel Vadot 
62*2f16049cSEmmanuel Vadot static int
ds1672_probe(device_t dev)63*2f16049cSEmmanuel Vadot ds1672_probe(device_t dev)
64*2f16049cSEmmanuel Vadot {
65*2f16049cSEmmanuel Vadot 	/* XXX really probe? */
66*2f16049cSEmmanuel Vadot 	device_set_desc(dev, "Dallas Semiconductor DS1672 RTC");
67*2f16049cSEmmanuel Vadot 	return (BUS_PROBE_NOWILDCARD);
68*2f16049cSEmmanuel Vadot }
69*2f16049cSEmmanuel Vadot 
70*2f16049cSEmmanuel Vadot static int
ds1672_read(device_t dev,uint8_t addr,uint8_t * data,uint8_t size)71*2f16049cSEmmanuel Vadot ds1672_read(device_t dev, uint8_t addr, uint8_t *data, uint8_t size)
72*2f16049cSEmmanuel Vadot {
73*2f16049cSEmmanuel Vadot 	struct iic_msg msgs[2] = {
74*2f16049cSEmmanuel Vadot 	     { DS1672_ADDR, IIC_M_WR, 1, &addr },
75*2f16049cSEmmanuel Vadot 	     { DS1672_ADDR, IIC_M_RD, size, data }
76*2f16049cSEmmanuel Vadot 	};
77*2f16049cSEmmanuel Vadot 
78*2f16049cSEmmanuel Vadot 	return (iicbus_transfer(dev, msgs, 2));
79*2f16049cSEmmanuel Vadot }
80*2f16049cSEmmanuel Vadot 
81*2f16049cSEmmanuel Vadot static int
ds1672_write(device_t dev,uint8_t addr,uint8_t * data,uint8_t size)82*2f16049cSEmmanuel Vadot ds1672_write(device_t dev, uint8_t addr, uint8_t *data, uint8_t size)
83*2f16049cSEmmanuel Vadot {
84*2f16049cSEmmanuel Vadot 	uint8_t buffer[MAX_IIC_DATA_SIZE + 1];
85*2f16049cSEmmanuel Vadot 	struct iic_msg msgs[1] = {
86*2f16049cSEmmanuel Vadot 	     { DS1672_ADDR, IIC_M_WR, size + 1, buffer },
87*2f16049cSEmmanuel Vadot 	};
88*2f16049cSEmmanuel Vadot 
89*2f16049cSEmmanuel Vadot 	if (size > MAX_IIC_DATA_SIZE)
90*2f16049cSEmmanuel Vadot 		return (ENOMEM);
91*2f16049cSEmmanuel Vadot 	/* NB: register pointer precedes actual data */
92*2f16049cSEmmanuel Vadot 	buffer[0] = addr;
93*2f16049cSEmmanuel Vadot 	memcpy(buffer + 1, data, size);
94*2f16049cSEmmanuel Vadot 	return (iicbus_transfer(dev, msgs, 1));
95*2f16049cSEmmanuel Vadot }
96*2f16049cSEmmanuel Vadot 
97*2f16049cSEmmanuel Vadot static int
ds1672_init(device_t dev)98*2f16049cSEmmanuel Vadot ds1672_init(device_t dev)
99*2f16049cSEmmanuel Vadot {
100*2f16049cSEmmanuel Vadot 	uint8_t ctrl;
101*2f16049cSEmmanuel Vadot 	int error;
102*2f16049cSEmmanuel Vadot 
103*2f16049cSEmmanuel Vadot 	error = ds1672_read(dev, DS1672_CTRL, &ctrl, 1);
104*2f16049cSEmmanuel Vadot 	if (error)
105*2f16049cSEmmanuel Vadot 		return (error);
106*2f16049cSEmmanuel Vadot 
107*2f16049cSEmmanuel Vadot 	/*
108*2f16049cSEmmanuel Vadot 	 * Check if oscialltor is not runned.
109*2f16049cSEmmanuel Vadot 	 */
110*2f16049cSEmmanuel Vadot 	if (ctrl & DS1672_CTRL_EOSC) {
111*2f16049cSEmmanuel Vadot 		device_printf(dev, "RTC oscillator was stopped. Check system"
112*2f16049cSEmmanuel Vadot 		    " time and RTC battery.\n");
113*2f16049cSEmmanuel Vadot 		ctrl &= ~DS1672_CTRL_EOSC;	/* Start oscillator. */
114*2f16049cSEmmanuel Vadot 		error = ds1672_write(dev, DS1672_CTRL, &ctrl, 1);
115*2f16049cSEmmanuel Vadot 	}
116*2f16049cSEmmanuel Vadot 	return (error);
117*2f16049cSEmmanuel Vadot }
118*2f16049cSEmmanuel Vadot 
119*2f16049cSEmmanuel Vadot static int
ds1672_detach(device_t dev)120*2f16049cSEmmanuel Vadot ds1672_detach(device_t dev)
121*2f16049cSEmmanuel Vadot {
122*2f16049cSEmmanuel Vadot 
123*2f16049cSEmmanuel Vadot     clock_unregister(dev);
124*2f16049cSEmmanuel Vadot     return (0);
125*2f16049cSEmmanuel Vadot }
126*2f16049cSEmmanuel Vadot 
127*2f16049cSEmmanuel Vadot static int
ds1672_attach(device_t dev)128*2f16049cSEmmanuel Vadot ds1672_attach(device_t dev)
129*2f16049cSEmmanuel Vadot {
130*2f16049cSEmmanuel Vadot 	struct ds1672_softc *sc = device_get_softc(dev);
131*2f16049cSEmmanuel Vadot 	int error;
132*2f16049cSEmmanuel Vadot 
133*2f16049cSEmmanuel Vadot 	sc->sc_dev = dev;
134*2f16049cSEmmanuel Vadot 	error = ds1672_init(dev);
135*2f16049cSEmmanuel Vadot 	if (error)
136*2f16049cSEmmanuel Vadot 		return (error);
137*2f16049cSEmmanuel Vadot 	clock_register(dev, 1000);
138*2f16049cSEmmanuel Vadot 	return (0);
139*2f16049cSEmmanuel Vadot }
140*2f16049cSEmmanuel Vadot 
141*2f16049cSEmmanuel Vadot static int
ds1672_gettime(device_t dev,struct timespec * ts)142*2f16049cSEmmanuel Vadot ds1672_gettime(device_t dev, struct timespec *ts)
143*2f16049cSEmmanuel Vadot {
144*2f16049cSEmmanuel Vadot 	uint8_t secs[4];
145*2f16049cSEmmanuel Vadot 	int error;
146*2f16049cSEmmanuel Vadot 
147*2f16049cSEmmanuel Vadot 	error = ds1672_read(dev, DS1672_COUNTER, secs, 4);
148*2f16049cSEmmanuel Vadot 	if (error == 0) {
149*2f16049cSEmmanuel Vadot 		/* counter has seconds since epoch */
150*2f16049cSEmmanuel Vadot 		ts->tv_sec = (secs[3] << 24) | (secs[2] << 16)
151*2f16049cSEmmanuel Vadot 			   | (secs[1] <<  8) | (secs[0] <<  0);
152*2f16049cSEmmanuel Vadot 		ts->tv_nsec = 0;
153*2f16049cSEmmanuel Vadot 	}
154*2f16049cSEmmanuel Vadot 	clock_dbgprint_ts(dev, CLOCK_DBG_READ, ts);
155*2f16049cSEmmanuel Vadot 	return (error);
156*2f16049cSEmmanuel Vadot }
157*2f16049cSEmmanuel Vadot 
158*2f16049cSEmmanuel Vadot static int
ds1672_settime(device_t dev,struct timespec * ts)159*2f16049cSEmmanuel Vadot ds1672_settime(device_t dev, struct timespec *ts)
160*2f16049cSEmmanuel Vadot {
161*2f16049cSEmmanuel Vadot 	uint8_t data[4];
162*2f16049cSEmmanuel Vadot 
163*2f16049cSEmmanuel Vadot 	data[0] = (ts->tv_sec >> 0) & 0xff;
164*2f16049cSEmmanuel Vadot 	data[1] = (ts->tv_sec >> 8) & 0xff;
165*2f16049cSEmmanuel Vadot 	data[2] = (ts->tv_sec >> 16) & 0xff;
166*2f16049cSEmmanuel Vadot 	data[3] = (ts->tv_sec >> 24) & 0xff;
167*2f16049cSEmmanuel Vadot 
168*2f16049cSEmmanuel Vadot 	ts->tv_nsec = 0;
169*2f16049cSEmmanuel Vadot 	clock_dbgprint_ts(dev, CLOCK_DBG_WRITE, ts);
170*2f16049cSEmmanuel Vadot 	return (ds1672_write(dev, DS1672_COUNTER, data, 4));
171*2f16049cSEmmanuel Vadot }
172*2f16049cSEmmanuel Vadot 
173*2f16049cSEmmanuel Vadot static device_method_t ds1672_methods[] = {
174*2f16049cSEmmanuel Vadot 	DEVMETHOD(device_probe,		ds1672_probe),
175*2f16049cSEmmanuel Vadot 	DEVMETHOD(device_attach,	ds1672_attach),
176*2f16049cSEmmanuel Vadot 	DEVMETHOD(device_detach,	ds1672_detach),
177*2f16049cSEmmanuel Vadot 
178*2f16049cSEmmanuel Vadot 	DEVMETHOD(clock_gettime,	ds1672_gettime),
179*2f16049cSEmmanuel Vadot 	DEVMETHOD(clock_settime,	ds1672_settime),
180*2f16049cSEmmanuel Vadot 
181*2f16049cSEmmanuel Vadot 	DEVMETHOD_END
182*2f16049cSEmmanuel Vadot };
183*2f16049cSEmmanuel Vadot 
184*2f16049cSEmmanuel Vadot static driver_t ds1672_driver = {
185*2f16049cSEmmanuel Vadot 	"ds1672_rtc",
186*2f16049cSEmmanuel Vadot 	ds1672_methods,
187*2f16049cSEmmanuel Vadot 	sizeof(struct ds1672_softc),
188*2f16049cSEmmanuel Vadot };
189*2f16049cSEmmanuel Vadot 
190*2f16049cSEmmanuel Vadot DRIVER_MODULE(ds1672, iicbus, ds1672_driver, 0, 0);
191*2f16049cSEmmanuel Vadot MODULE_VERSION(ds1672, 1);
192*2f16049cSEmmanuel Vadot MODULE_DEPEND(ds1672, iicbus, 1, 1, 1);
193