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