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