pcf8563.c revision 1.18 1 /* $NetBSD: pcf8563.c,v 1.18 2025/09/08 13:06:16 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 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: pcf8563.c,v 1.18 2025/09/08 13:06:16 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/kernel.h>
36
37 #include <dev/clock_subr.h>
38
39 #include <dev/i2c/i2cvar.h>
40 #include <dev/i2c/pcf8563reg.h>
41
42 static const struct device_compatible_entry compat_data[] = {
43 { .compat = "nxp,pcf8563" },
44 { .compat = "pcf8563rtc" },
45 DEVICE_COMPAT_EOL
46 };
47
48 struct pcf8563rtc_softc {
49 device_t sc_dev;
50 i2c_tag_t sc_tag;
51 int sc_addr;
52 struct todr_chip_handle sc_todr;
53 };
54
55 static int pcf8563rtc_match(device_t, cfdata_t, void *);
56 static void pcf8563rtc_attach(device_t, device_t, void *);
57
58 CFATTACH_DECL_NEW(pcf8563rtc, sizeof(struct pcf8563rtc_softc),
59 pcf8563rtc_match, pcf8563rtc_attach, NULL, NULL);
60
61 static int pcf8563rtc_clock_read(struct pcf8563rtc_softc *, struct clock_ymdhms *);
62 static int pcf8563rtc_clock_write(struct pcf8563rtc_softc *, struct clock_ymdhms *);
63 static int pcf8563rtc_gettime(struct todr_chip_handle *, struct clock_ymdhms *);
64 static int pcf8563rtc_settime(struct todr_chip_handle *, struct clock_ymdhms *);
65
66 static int
67 pcf8563rtc_match(device_t parent, cfdata_t cf, void *aux)
68 {
69 struct i2c_attach_args *ia = aux;
70 int match_result;
71
72 if (iic_use_direct_match(ia, cf, compat_data, &match_result))
73 return match_result;
74
75 /* indirect config - check typical address */
76 if (ia->ia_addr == PCF8563_ADDR)
77 return I2C_MATCH_ADDRESS_ONLY;
78
79 return 0;
80 }
81
82 static void
83 pcf8563rtc_attach(device_t parent, device_t self, void *aux)
84 {
85 struct pcf8563rtc_softc *sc = device_private(self);
86 struct i2c_attach_args *ia = aux;
87 int error;
88
89 aprint_naive(": Real-time Clock\n");
90 aprint_normal(": NXP PCF8563 Real-time Clock\n");
91
92 sc->sc_dev = self;
93 sc->sc_tag = ia->ia_tag;
94 sc->sc_addr = ia->ia_addr;
95 sc->sc_todr.todr_dev = self;
96 sc->sc_todr.todr_gettime_ymdhms = pcf8563rtc_gettime;
97 sc->sc_todr.todr_settime_ymdhms = pcf8563rtc_settime;
98
99 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
100 aprint_error_dev(sc->sc_dev,
101 "failed to acquire bus for attach\n");
102 return;
103 }
104 if ((error = iic_smbus_write_byte(sc->sc_tag, sc->sc_addr,
105 PCF8563_R_CS1, 0, 0)) == 0) {
106 error = iic_smbus_write_byte(sc->sc_tag, sc->sc_addr,
107 PCF8563_R_CS2, 0, 0);
108 }
109 iic_release_bus(sc->sc_tag, 0);
110 if (error) {
111 aprint_error_dev(sc->sc_dev, "failed to initialize RTC\n");
112 return;
113 }
114
115 todr_attach(&sc->sc_todr);
116 }
117
118 static int
119 pcf8563rtc_gettime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
120 {
121 struct pcf8563rtc_softc *sc = device_private(ch->todr_dev);
122
123 return pcf8563rtc_clock_read(sc, dt);
124 }
125
126 static int
127 pcf8563rtc_settime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
128 {
129 struct pcf8563rtc_softc *sc = device_private(ch->todr_dev);
130
131 return pcf8563rtc_clock_write(sc, dt);
132 }
133
134 static int
135 pcf8563rtc_clock_read(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
136 {
137 uint8_t bcd[PCF8563_NREGS];
138 uint8_t reg = PCF8563_R_SECOND;
139 int error;
140
141 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
142 device_printf(sc->sc_dev, "acquire bus for read failed\n");
143 return error;
144 }
145
146 if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
147 ®, 1, &bcd[reg], PCF8563_R_YEAR - reg + 1,
148 0)) != 0) {
149 iic_release_bus(sc->sc_tag, 0);
150 device_printf(sc->sc_dev, "read failed\n");
151 return error;
152 }
153
154 iic_release_bus(sc->sc_tag, 0);
155
156 if (bcd[PCF8563_R_SECOND] & PCF8563_M_VL)
157 return EIO;
158
159 dt->dt_sec = bcdtobin(bcd[PCF8563_R_SECOND] & PCF8563_M_SECOND);
160 dt->dt_min = bcdtobin(bcd[PCF8563_R_MINUTE] & PCF8563_M_MINUTE);
161 dt->dt_hour = bcdtobin(bcd[PCF8563_R_HOUR] & PCF8563_M_HOUR);
162 dt->dt_day = bcdtobin(bcd[PCF8563_R_DAY] & PCF8563_M_DAY);
163 dt->dt_wday = bcdtobin(bcd[PCF8563_R_WEEKDAY] & PCF8563_M_WEEKDAY);
164 dt->dt_mon = bcdtobin(bcd[PCF8563_R_MONTH] & PCF8563_M_MONTH);
165 dt->dt_year = 1900 +
166 (bcdtobin(bcd[PCF8563_R_YEAR] & PCF8563_M_YEAR) % 100);
167 if ((bcd[PCF8563_R_MONTH] & PCF8563_M_CENTURY) == 0)
168 dt->dt_year += 100;
169
170 return error;
171 }
172
173 static int
174 pcf8563rtc_clock_write(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
175 {
176 uint8_t bcd[PCF8563_NREGS];
177 uint8_t reg = PCF8563_R_SECOND;
178 int error;
179
180 bcd[PCF8563_R_SECOND] = bintobcd(dt->dt_sec);
181 bcd[PCF8563_R_MINUTE] = bintobcd(dt->dt_min);
182 bcd[PCF8563_R_HOUR] = bintobcd(dt->dt_hour);
183 bcd[PCF8563_R_DAY] = bintobcd(dt->dt_day);
184 bcd[PCF8563_R_WEEKDAY] = bintobcd(dt->dt_wday);
185 bcd[PCF8563_R_MONTH] = bintobcd(dt->dt_mon);
186 bcd[PCF8563_R_YEAR] = bintobcd(dt->dt_year % 100);
187 if (dt->dt_year < 2000)
188 bcd[PCF8563_R_MONTH] |= PCF8563_M_CENTURY;
189
190 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
191 device_printf(sc->sc_dev, "acquire bus for write failed\n");
192 return error;
193 }
194
195 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) {
196 iic_release_bus(sc->sc_tag, 0);
197 device_printf(sc->sc_dev, "write failed\n");
198 return error;
199 }
200
201 iic_release_bus(sc->sc_tag, 0);
202
203 return error;
204 }
205