pcf8563.c revision 1.17 1 /* $NetBSD: pcf8563.c,v 1.17 2025/09/07 21:45:15 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2011 Jonathan A. Kollasch
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* XXX */
30 #if defined(__arm__) || defined(__aarch64__)
31 #include "opt_fdt.h"
32 #endif
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: pcf8563.c,v 1.17 2025/09/07 21:45:15 thorpej Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/kernel.h>
41
42 #include <dev/clock_subr.h>
43
44 #include <dev/i2c/i2cvar.h>
45 #include <dev/i2c/pcf8563reg.h>
46
47 #ifdef FDT
48 #include <dev/fdt/fdtvar.h>
49 #endif
50
51 static const struct device_compatible_entry compat_data[] = {
52 { .compat = "nxp,pcf8563" },
53 { .compat = "pcf8563rtc" },
54 DEVICE_COMPAT_EOL
55 };
56
57 struct pcf8563rtc_softc {
58 device_t sc_dev;
59 i2c_tag_t sc_tag;
60 int sc_addr;
61 struct todr_chip_handle sc_todr;
62 };
63
64 static int pcf8563rtc_match(device_t, cfdata_t, void *);
65 static void pcf8563rtc_attach(device_t, device_t, void *);
66
67 CFATTACH_DECL_NEW(pcf8563rtc, sizeof(struct pcf8563rtc_softc),
68 pcf8563rtc_match, pcf8563rtc_attach, NULL, NULL);
69
70 static int pcf8563rtc_clock_read(struct pcf8563rtc_softc *, struct clock_ymdhms *);
71 static int pcf8563rtc_clock_write(struct pcf8563rtc_softc *, struct clock_ymdhms *);
72 static int pcf8563rtc_gettime(struct todr_chip_handle *, struct clock_ymdhms *);
73 static int pcf8563rtc_settime(struct todr_chip_handle *, struct clock_ymdhms *);
74
75 static int
76 pcf8563rtc_match(device_t parent, cfdata_t cf, void *aux)
77 {
78 struct i2c_attach_args *ia = aux;
79 int match_result;
80
81 if (iic_use_direct_match(ia, cf, compat_data, &match_result))
82 return match_result;
83
84 /* indirect config - check typical address */
85 if (ia->ia_addr == PCF8563_ADDR)
86 return I2C_MATCH_ADDRESS_ONLY;
87
88 return 0;
89 }
90
91 static void
92 pcf8563rtc_attach(device_t parent, device_t self, void *aux)
93 {
94 struct pcf8563rtc_softc *sc = device_private(self);
95 struct i2c_attach_args *ia = aux;
96 int error;
97
98 aprint_naive(": Real-time Clock\n");
99 aprint_normal(": NXP PCF8563 Real-time Clock\n");
100
101 sc->sc_dev = self;
102 sc->sc_tag = ia->ia_tag;
103 sc->sc_addr = ia->ia_addr;
104 sc->sc_todr.todr_dev = self;
105 sc->sc_todr.todr_gettime_ymdhms = pcf8563rtc_gettime;
106 sc->sc_todr.todr_settime_ymdhms = pcf8563rtc_settime;
107
108 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
109 aprint_error_dev(sc->sc_dev,
110 "failed to acquire bus for attach\n");
111 return;
112 }
113 if ((error = iic_smbus_write_byte(sc->sc_tag, sc->sc_addr,
114 PCF8563_R_CS1, 0, 0)) == 0) {
115 error = iic_smbus_write_byte(sc->sc_tag, sc->sc_addr,
116 PCF8563_R_CS2, 0, 0);
117 }
118 iic_release_bus(sc->sc_tag, 0);
119 if (error) {
120 aprint_error_dev(sc->sc_dev, "failed to initialize RTC\n");
121 return;
122 }
123
124 #ifdef FDT
125 fdtbus_todr_attach(self, ia->ia_cookie, &sc->sc_todr);
126 #else
127 todr_attach(&sc->sc_todr);
128 #endif
129 }
130
131 static int
132 pcf8563rtc_gettime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
133 {
134 struct pcf8563rtc_softc *sc = device_private(ch->todr_dev);
135
136 return pcf8563rtc_clock_read(sc, dt);
137 }
138
139 static int
140 pcf8563rtc_settime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
141 {
142 struct pcf8563rtc_softc *sc = device_private(ch->todr_dev);
143
144 return pcf8563rtc_clock_write(sc, dt);
145 }
146
147 static int
148 pcf8563rtc_clock_read(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
149 {
150 uint8_t bcd[PCF8563_NREGS];
151 uint8_t reg = PCF8563_R_SECOND;
152 int error;
153
154 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
155 device_printf(sc->sc_dev, "acquire bus for read failed\n");
156 return error;
157 }
158
159 if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
160 ®, 1, &bcd[reg], PCF8563_R_YEAR - reg + 1,
161 0)) != 0) {
162 iic_release_bus(sc->sc_tag, 0);
163 device_printf(sc->sc_dev, "read failed\n");
164 return error;
165 }
166
167 iic_release_bus(sc->sc_tag, 0);
168
169 if (bcd[PCF8563_R_SECOND] & PCF8563_M_VL)
170 return EIO;
171
172 dt->dt_sec = bcdtobin(bcd[PCF8563_R_SECOND] & PCF8563_M_SECOND);
173 dt->dt_min = bcdtobin(bcd[PCF8563_R_MINUTE] & PCF8563_M_MINUTE);
174 dt->dt_hour = bcdtobin(bcd[PCF8563_R_HOUR] & PCF8563_M_HOUR);
175 dt->dt_day = bcdtobin(bcd[PCF8563_R_DAY] & PCF8563_M_DAY);
176 dt->dt_wday = bcdtobin(bcd[PCF8563_R_WEEKDAY] & PCF8563_M_WEEKDAY);
177 dt->dt_mon = bcdtobin(bcd[PCF8563_R_MONTH] & PCF8563_M_MONTH);
178 dt->dt_year = 1900 +
179 (bcdtobin(bcd[PCF8563_R_YEAR] & PCF8563_M_YEAR) % 100);
180 if ((bcd[PCF8563_R_MONTH] & PCF8563_M_CENTURY) == 0)
181 dt->dt_year += 100;
182
183 return error;
184 }
185
186 static int
187 pcf8563rtc_clock_write(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
188 {
189 uint8_t bcd[PCF8563_NREGS];
190 uint8_t reg = PCF8563_R_SECOND;
191 int error;
192
193 bcd[PCF8563_R_SECOND] = bintobcd(dt->dt_sec);
194 bcd[PCF8563_R_MINUTE] = bintobcd(dt->dt_min);
195 bcd[PCF8563_R_HOUR] = bintobcd(dt->dt_hour);
196 bcd[PCF8563_R_DAY] = bintobcd(dt->dt_day);
197 bcd[PCF8563_R_WEEKDAY] = bintobcd(dt->dt_wday);
198 bcd[PCF8563_R_MONTH] = bintobcd(dt->dt_mon);
199 bcd[PCF8563_R_YEAR] = bintobcd(dt->dt_year % 100);
200 if (dt->dt_year < 2000)
201 bcd[PCF8563_R_MONTH] |= PCF8563_M_CENTURY;
202
203 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
204 device_printf(sc->sc_dev, "acquire bus for write failed\n");
205 return error;
206 }
207
208 if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, ®, 1, &bcd[reg], PCF8563_R_YEAR - reg + 1, 0)) != 0) {
209 iic_release_bus(sc->sc_tag, 0);
210 device_printf(sc->sc_dev, "write failed\n");
211 return error;
212 }
213
214 iic_release_bus(sc->sc_tag, 0);
215
216 return error;
217 }
218