]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iicbus/ds1672.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / dev / iicbus / ds1672.c
1 /*-
2  * Copyright (c) 2006 Sam Leffler.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  */
24
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27 /*
28  * Dallas Semiconductor DS1672 RTC sitting on the I2C bus.
29  */
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>
35 #include <sys/time.h>
36 #include <sys/bus.h>
37 #include <sys/resource.h>
38 #include <sys/rman.h>
39
40 #include <dev/iicbus/iiconf.h>
41
42 #include "iicbus_if.h"
43 #include "clock_if.h"
44
45 #define IIC_M_WR        0       /* write operation */
46
47 #define DS1672_ADDR     0xd0    /* slave address */
48
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) */
52
53 #define NANOSEC         1000000000
54
55 struct ds1672_softc {
56         device_t                sc_dev;
57 };
58
59 static int
60 ds1672_probe(device_t dev)
61 {
62         /* XXX really probe? */
63         device_set_desc(dev, "Dallas Semiconductor DS1672 RTC");
64         return (0);
65 }
66
67 static int
68 ds1672_attach(device_t dev)
69 {
70         struct ds1672_softc *sc = device_get_softc(dev);
71
72         sc->sc_dev = dev;
73
74         clock_register(dev, 1000);
75         return (0);
76 }
77
78 static int
79 ds1672_gettime(device_t dev, struct timespec *ts)
80 {
81         uint8_t addr[1] = { DS1672_COUNTER };
82         uint8_t secs[4];
83         struct iic_msg msgs[2] = {
84              { DS1672_ADDR, IIC_M_WR, 1, addr },
85              { DS1672_ADDR, IIC_M_RD, 4, secs },
86         };
87         int error;
88
89         error = iicbus_transfer(dev, msgs, 2);
90         if (error == 0) {
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;
95         }
96         return error;
97 }
98
99 static int
100 ds1672_settime(device_t dev, struct timespec *ts)
101 {
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 },
106         };
107
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;
112
113         return iicbus_transfer(dev, msgs, 1);
114 }
115
116 static device_method_t ds1672_methods[] = {
117         DEVMETHOD(device_probe,         ds1672_probe),
118         DEVMETHOD(device_attach,        ds1672_attach),
119
120         DEVMETHOD(clock_gettime,        ds1672_gettime),
121         DEVMETHOD(clock_settime,        ds1672_settime),
122
123         {0, 0},
124 };
125
126 static driver_t ds1672_driver = {
127         "ds1672",
128         ds1672_methods,
129         sizeof(struct ds1672_softc),
130 };
131 static devclass_t ds1672_devclass;
132
133 DRIVER_MODULE(ds1672, iicbus, ds1672_driver, ds1672_devclass, 0, 0);
134 MODULE_VERSION(ds1672, 1);
135 MODULE_DEPEND(ds1672, iicbus, 1, 1, 1);