]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/iicbus/ds1672.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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 <machine/bus.h>
41 #include <machine/cpu.h>
42 #include <machine/cpufunc.h>
43 #include <machine/frame.h>
44 #include <machine/resource.h>
45 #include <machine/intr.h>
46
47 #include <dev/iicbus/iiconf.h>
48
49 #include "iicbus_if.h"
50 #include "clock_if.h"
51
52 #define IIC_M_WR        0       /* write operation */
53
54 #define DS1672_ADDR     0xd0    /* slave address */
55
56 #define DS1672_COUNTER  0       /* counter (bytes 0-3) */
57 #define DS1672_CTRL     4       /* control (1 byte) */
58 #define DS1672_TRICKLE  5       /* trickle charger (1 byte) */
59
60 #define NANOSEC         1000000000
61
62 struct ds1672_softc {
63         device_t                sc_dev;
64 };
65
66 static int
67 ds1672_probe(device_t dev)
68 {
69         /* XXX really probe? */
70         device_set_desc(dev, "Dallas Semiconductor DS1672 RTC");
71         return (0);
72 }
73
74 static int
75 ds1672_attach(device_t dev)
76 {
77         struct ds1672_softc *sc = device_get_softc(dev);
78
79         sc->sc_dev = dev;
80
81         clock_register(dev, 1000);
82         return (0);
83 }
84
85 static int
86 ds1672_gettime(device_t dev, struct timespec *ts)
87 {
88         uint8_t addr[1] = { DS1672_COUNTER };
89         uint8_t secs[4];
90         struct iic_msg msgs[2] = {
91              { DS1672_ADDR, IIC_M_WR, 1, addr },
92              { DS1672_ADDR, IIC_M_RD, 4, secs },
93         };
94         int error;
95
96         error = iicbus_transfer(dev, msgs, 2);
97         if (error == 0) {
98                 /* counter has seconds since epoch */
99                 ts->tv_sec = (secs[3] << 24) | (secs[2] << 16)
100                            | (secs[1] <<  8) | (secs[0] <<  0);
101                 ts->tv_nsec = NANOSEC / 2;
102         }
103         return error;
104 }
105
106 static int
107 ds1672_settime(device_t dev, struct timespec *ts)
108 {
109         /* NB: register pointer precedes actual data */
110         uint8_t data[5] = { DS1672_COUNTER };
111         struct iic_msg msgs[1] = {
112              { DS1672_ADDR, IIC_M_WR, 5, data },
113         };
114
115         data[1] = (ts->tv_sec >> 0) & 0xff;
116         data[2] = (ts->tv_sec >> 8) & 0xff;
117         data[3] = (ts->tv_sec >> 16) & 0xff;
118         data[4] = (ts->tv_sec >> 24) & 0xff;
119
120         return iicbus_transfer(dev, msgs, 1);
121 }
122
123 static device_method_t ds1672_methods[] = {
124         DEVMETHOD(device_probe,         ds1672_probe),
125         DEVMETHOD(device_attach,        ds1672_attach),
126
127         DEVMETHOD(clock_gettime,        ds1672_gettime),
128         DEVMETHOD(clock_settime,        ds1672_settime),
129
130         {0, 0},
131 };
132
133 static driver_t ds1672_driver = {
134         "ds1672",
135         ds1672_methods,
136         sizeof(struct ds1672_softc),
137 };
138 static devclass_t ds1672_devclass;
139
140 DRIVER_MODULE(ds1672, iicbus, ds1672_driver, ds1672_devclass, 0, 0);
141 MODULE_VERSION(ds1672, 1);
142 MODULE_DEPEND(ds1672, iicbus, 1, 1, 1);