rs5c372.c revision 1.17 1 1.17 thorpej /* $NetBSD: rs5c372.c,v 1.17 2021/01/30 17:38:27 thorpej Exp $ */
2 1.1 nonaka
3 1.12 nonaka /*-
4 1.12 nonaka * Copyright (C) 2005 NONAKA Kimihiro <nonaka (at) netbsd.org>
5 1.1 nonaka * All rights reserved.
6 1.1 nonaka *
7 1.1 nonaka * Redistribution and use in source and binary forms, with or without
8 1.1 nonaka * modification, are permitted provided that the following conditions
9 1.1 nonaka * are met:
10 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
11 1.1 nonaka * notice, this list of conditions and the following disclaimer.
12 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
14 1.1 nonaka * documentation and/or other materials provided with the distribution.
15 1.1 nonaka *
16 1.12 nonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.12 nonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.12 nonaka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.12 nonaka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.12 nonaka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.12 nonaka * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.12 nonaka * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.12 nonaka * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.12 nonaka * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.12 nonaka * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 nonaka */
27 1.1 nonaka
28 1.7 lukem #include <sys/cdefs.h>
29 1.17 thorpej __KERNEL_RCSID(0, "$NetBSD: rs5c372.c,v 1.17 2021/01/30 17:38:27 thorpej Exp $");
30 1.7 lukem
31 1.1 nonaka #include <sys/param.h>
32 1.1 nonaka #include <sys/systm.h>
33 1.1 nonaka #include <sys/device.h>
34 1.1 nonaka #include <sys/kernel.h>
35 1.1 nonaka #include <sys/fcntl.h>
36 1.1 nonaka #include <sys/uio.h>
37 1.1 nonaka #include <sys/conf.h>
38 1.1 nonaka #include <sys/event.h>
39 1.1 nonaka
40 1.1 nonaka #include <dev/clock_subr.h>
41 1.1 nonaka
42 1.1 nonaka #include <dev/i2c/i2cvar.h>
43 1.1 nonaka #include <dev/i2c/rs5c372reg.h>
44 1.1 nonaka
45 1.1 nonaka struct rs5c372rtc_softc {
46 1.9 xtraeme device_t sc_dev;
47 1.1 nonaka i2c_tag_t sc_tag;
48 1.1 nonaka int sc_address;
49 1.1 nonaka struct todr_chip_handle sc_todr;
50 1.1 nonaka };
51 1.1 nonaka
52 1.9 xtraeme static int rs5c372rtc_match(device_t, cfdata_t, void *);
53 1.9 xtraeme static void rs5c372rtc_attach(device_t, device_t, void *);
54 1.1 nonaka
55 1.9 xtraeme CFATTACH_DECL_NEW(rs5c372rtc, sizeof(struct rs5c372rtc_softc),
56 1.1 nonaka rs5c372rtc_match, rs5c372rtc_attach, NULL, NULL);
57 1.1 nonaka
58 1.16 thorpej static int rs5c372rtc_reg_write(struct rs5c372rtc_softc *, int, uint8_t);
59 1.1 nonaka static int rs5c372rtc_clock_read(struct rs5c372rtc_softc *, struct clock_ymdhms *);
60 1.1 nonaka static int rs5c372rtc_clock_write(struct rs5c372rtc_softc *, struct clock_ymdhms *);
61 1.13 tsutsui static int rs5c372rtc_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
62 1.13 tsutsui static int rs5c372rtc_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
63 1.1 nonaka
64 1.17 thorpej static const struct device_compatible_entry compat_data[] = {
65 1.17 thorpej { .compat = "ricoh,rs5c372a" },
66 1.17 thorpej { .compat = "ricoh,rs5c372b" },
67 1.17 thorpej DEVICE_COMPAT_EOL
68 1.17 thorpej };
69 1.17 thorpej
70 1.1 nonaka static int
71 1.9 xtraeme rs5c372rtc_match(device_t parent, cfdata_t cf, void *arg)
72 1.1 nonaka {
73 1.1 nonaka struct i2c_attach_args *ia = arg;
74 1.15 thorpej int match_result;
75 1.15 thorpej
76 1.17 thorpej if (iic_use_direct_match(ia, cf, compat_data, &match_result))
77 1.15 thorpej return match_result;
78 1.15 thorpej
79 1.15 thorpej /* indirect config - check typical address */
80 1.15 thorpej if (ia->ia_addr == RS5C372_ADDR)
81 1.15 thorpej return I2C_MATCH_ADDRESS_ONLY;
82 1.1 nonaka
83 1.11 phx return 0;
84 1.1 nonaka }
85 1.1 nonaka
86 1.1 nonaka static void
87 1.9 xtraeme rs5c372rtc_attach(device_t parent, device_t self, void *arg)
88 1.1 nonaka {
89 1.5 thorpej struct rs5c372rtc_softc *sc = device_private(self);
90 1.1 nonaka struct i2c_attach_args *ia = arg;
91 1.1 nonaka
92 1.1 nonaka aprint_naive(": Real-time Clock\n");
93 1.1 nonaka aprint_normal(": RICOH RS5C372[AB] Real-time Clock\n");
94 1.1 nonaka
95 1.1 nonaka sc->sc_tag = ia->ia_tag;
96 1.1 nonaka sc->sc_address = ia->ia_addr;
97 1.9 xtraeme sc->sc_dev = self;
98 1.1 nonaka sc->sc_todr.cookie = sc;
99 1.13 tsutsui sc->sc_todr.todr_gettime_ymdhms = rs5c372rtc_gettime_ymdhms;
100 1.13 tsutsui sc->sc_todr.todr_settime_ymdhms = rs5c372rtc_settime_ymdhms;
101 1.1 nonaka sc->sc_todr.todr_setwen = NULL;
102 1.1 nonaka
103 1.1 nonaka todr_attach(&sc->sc_todr);
104 1.1 nonaka
105 1.2 nonaka /* Initialize RTC */
106 1.2 nonaka rs5c372rtc_reg_write(sc, RS5C372_CONTROL2, RS5C372_CONTROL2_24HRS);
107 1.2 nonaka rs5c372rtc_reg_write(sc, RS5C372_CONTROL1, 0);
108 1.1 nonaka }
109 1.1 nonaka
110 1.1 nonaka static int
111 1.13 tsutsui rs5c372rtc_gettime_ymdhms(todr_chip_handle_t ch, struct clock_ymdhms *dt)
112 1.1 nonaka {
113 1.1 nonaka struct rs5c372rtc_softc *sc = ch->cookie;
114 1.1 nonaka
115 1.16 thorpej return rs5c372rtc_clock_read(sc, dt);
116 1.1 nonaka }
117 1.1 nonaka
118 1.1 nonaka static int
119 1.13 tsutsui rs5c372rtc_settime_ymdhms(todr_chip_handle_t ch, struct clock_ymdhms *dt)
120 1.1 nonaka {
121 1.1 nonaka struct rs5c372rtc_softc *sc = ch->cookie;
122 1.1 nonaka
123 1.16 thorpej return rs5c372rtc_clock_write(sc, dt);
124 1.1 nonaka }
125 1.1 nonaka
126 1.16 thorpej static int
127 1.2 nonaka rs5c372rtc_reg_write(struct rs5c372rtc_softc *sc, int reg, uint8_t val)
128 1.1 nonaka {
129 1.1 nonaka uint8_t cmdbuf[2];
130 1.16 thorpej int error;
131 1.1 nonaka
132 1.16 thorpej if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
133 1.9 xtraeme aprint_error_dev(sc->sc_dev,
134 1.9 xtraeme "rs5c372rtc_reg_write: failed to acquire I2C bus\n");
135 1.16 thorpej return error;
136 1.1 nonaka }
137 1.1 nonaka
138 1.2 nonaka reg &= 0xf;
139 1.2 nonaka cmdbuf[0] = (reg << 4);
140 1.2 nonaka cmdbuf[1] = val;
141 1.16 thorpej if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
142 1.16 thorpej sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1,
143 1.16 thorpej 0)) != 0) {
144 1.16 thorpej iic_release_bus(sc->sc_tag, 0);
145 1.9 xtraeme aprint_error_dev(sc->sc_dev,
146 1.9 xtraeme "rs5c372rtc_reg_write: failed to write reg%d\n", reg);
147 1.16 thorpej return error;
148 1.1 nonaka }
149 1.1 nonaka
150 1.16 thorpej iic_release_bus(sc->sc_tag, 0);
151 1.16 thorpej
152 1.16 thorpej return 0;
153 1.1 nonaka }
154 1.1 nonaka
155 1.1 nonaka static int
156 1.1 nonaka rs5c372rtc_clock_read(struct rs5c372rtc_softc *sc, struct clock_ymdhms *dt)
157 1.1 nonaka {
158 1.1 nonaka uint8_t bcd[RS5C372_NRTC_REGS];
159 1.1 nonaka uint8_t cmdbuf[1];
160 1.16 thorpej int error;
161 1.1 nonaka
162 1.16 thorpej if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
163 1.9 xtraeme aprint_error_dev(sc->sc_dev,
164 1.9 xtraeme "rs5c372rtc_clock_read: failed to acquire I2C bus\n");
165 1.16 thorpej return (error);
166 1.1 nonaka }
167 1.1 nonaka
168 1.1 nonaka cmdbuf[0] = (RS5C372_SECONDS << 4);
169 1.16 thorpej if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
170 1.16 thorpej cmdbuf, 1, bcd, RS5C372_NRTC_REGS, 0)) != 0) {
171 1.16 thorpej iic_release_bus(sc->sc_tag, 0);
172 1.9 xtraeme aprint_error_dev(sc->sc_dev,
173 1.9 xtraeme "rs5c372rtc_clock_read: failed to read rtc\n");
174 1.16 thorpej return (error);
175 1.1 nonaka }
176 1.1 nonaka
177 1.16 thorpej iic_release_bus(sc->sc_tag, 0);
178 1.1 nonaka
179 1.1 nonaka /*
180 1.1 nonaka * Convert the RS5C372's register values into something useable
181 1.1 nonaka */
182 1.14 christos dt->dt_sec = bcdtobin(bcd[RS5C372_SECONDS] & RS5C372_SECONDS_MASK);
183 1.14 christos dt->dt_min = bcdtobin(bcd[RS5C372_MINUTES] & RS5C372_MINUTES_MASK);
184 1.14 christos dt->dt_hour = bcdtobin(bcd[RS5C372_HOURS] & RS5C372_HOURS_24MASK);
185 1.14 christos dt->dt_day = bcdtobin(bcd[RS5C372_DATE] & RS5C372_DATE_MASK);
186 1.14 christos dt->dt_mon = bcdtobin(bcd[RS5C372_MONTH] & RS5C372_MONTH_MASK);
187 1.14 christos dt->dt_year = bcdtobin(bcd[RS5C372_YEAR]) + 2000;
188 1.1 nonaka
189 1.16 thorpej return (0);
190 1.1 nonaka }
191 1.1 nonaka
192 1.1 nonaka static int
193 1.1 nonaka rs5c372rtc_clock_write(struct rs5c372rtc_softc *sc, struct clock_ymdhms *dt)
194 1.1 nonaka {
195 1.1 nonaka uint8_t bcd[RS5C372_NRTC_REGS];
196 1.1 nonaka uint8_t cmdbuf[1];
197 1.16 thorpej int error;
198 1.1 nonaka
199 1.1 nonaka /*
200 1.1 nonaka * Convert our time representation into something the RS5C372
201 1.1 nonaka * can understand.
202 1.1 nonaka */
203 1.14 christos bcd[RS5C372_SECONDS] = bintobcd(dt->dt_sec);
204 1.14 christos bcd[RS5C372_MINUTES] = bintobcd(dt->dt_min);
205 1.14 christos bcd[RS5C372_HOURS] = bintobcd(dt->dt_hour);
206 1.14 christos bcd[RS5C372_DATE] = bintobcd(dt->dt_day);
207 1.14 christos bcd[RS5C372_DAY] = bintobcd(dt->dt_wday);
208 1.14 christos bcd[RS5C372_MONTH] = bintobcd(dt->dt_mon);
209 1.14 christos bcd[RS5C372_YEAR] = bintobcd(dt->dt_year % 100);
210 1.1 nonaka
211 1.16 thorpej if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
212 1.9 xtraeme aprint_error_dev(sc->sc_dev, "rs5c372rtc_clock_write: failed to "
213 1.8 cegger "acquire I2C bus\n");
214 1.16 thorpej return (error);
215 1.1 nonaka }
216 1.1 nonaka
217 1.1 nonaka cmdbuf[0] = (RS5C372_SECONDS << 4);
218 1.16 thorpej if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
219 1.16 thorpej sc->sc_address, cmdbuf, 1, bcd,
220 1.16 thorpej RS5C372_NRTC_REGS, 0)) != 0) {
221 1.16 thorpej iic_release_bus(sc->sc_tag, 0);
222 1.9 xtraeme aprint_error_dev(sc->sc_dev,
223 1.9 xtraeme "rs5c372rtc_clock_write: failed to write rtc\n");
224 1.16 thorpej return (error);
225 1.1 nonaka }
226 1.1 nonaka
227 1.16 thorpej iic_release_bus(sc->sc_tag, 0);
228 1.1 nonaka
229 1.16 thorpej return (0);
230 1.1 nonaka }
231