pcf8563.c revision 1.10 1 /* $NetBSD: pcf8563.c,v 1.10 2018/06/18 17:07:07 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.10 2018/06/18 17:07:07 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 char *compatible[] = {
52 "nxp,pcf8563",
53 "pcf8563rtc",
54 NULL
55 };
56
57 static const struct device_compatible_entry pcf8563rtc_compat_data[] = {
58 DEVICE_COMPAT_ENTRY(compatible),
59 DEVICE_COMPAT_TERMINATOR
60 };
61
62 struct pcf8563rtc_softc {
63 device_t sc_dev;
64 i2c_tag_t sc_tag;
65 int sc_addr;
66 struct todr_chip_handle sc_todr;
67 };
68
69 static int pcf8563rtc_match(device_t, cfdata_t, void *);
70 static void pcf8563rtc_attach(device_t, device_t, void *);
71
72 CFATTACH_DECL_NEW(pcf8563rtc, sizeof(struct pcf8563rtc_softc),
73 pcf8563rtc_match, pcf8563rtc_attach, NULL, NULL);
74
75 static int pcf8563rtc_clock_read(struct pcf8563rtc_softc *, struct clock_ymdhms *);
76 static int pcf8563rtc_clock_write(struct pcf8563rtc_softc *, struct clock_ymdhms *);
77 static int pcf8563rtc_gettime(struct todr_chip_handle *, struct clock_ymdhms *);
78 static int pcf8563rtc_settime(struct todr_chip_handle *, struct clock_ymdhms *);
79
80 static int
81 pcf8563rtc_match(device_t parent, cfdata_t cf, void *aux)
82 {
83 struct i2c_attach_args *ia = aux;
84 int match_result;
85
86 if (iic_use_direct_match(ia, cf, pcf8563rtc_compat_data, &match_result))
87 return match_result;
88
89 /* indirect config - check typical address */
90 if (ia->ia_addr == PCF8563_ADDR)
91 return I2C_MATCH_ADDRESS_ONLY;
92
93 return 0;
94 }
95
96 static void
97 pcf8563rtc_attach(device_t parent, device_t self, void *aux)
98 {
99 struct pcf8563rtc_softc *sc = device_private(self);
100 struct i2c_attach_args *ia = aux;
101
102 aprint_naive(": Real-time Clock\n");
103 aprint_normal(": NXP PCF8563 Real-time Clock\n");
104
105 sc->sc_dev = self;
106 sc->sc_tag = ia->ia_tag;
107 sc->sc_addr = ia->ia_addr;
108 sc->sc_todr.cookie = sc;
109 sc->sc_todr.todr_gettime_ymdhms = pcf8563rtc_gettime;
110 sc->sc_todr.todr_settime_ymdhms = pcf8563rtc_settime;
111 sc->sc_todr.todr_setwen = NULL;
112
113 iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
114 iic_smbus_write_byte(sc->sc_tag, sc->sc_addr, PCF8563_R_CS1, 0,
115 I2C_F_POLL);
116 iic_smbus_write_byte(sc->sc_tag, sc->sc_addr, PCF8563_R_CS2, 0,
117 I2C_F_POLL);
118 iic_release_bus(sc->sc_tag, I2C_F_POLL);
119
120 #ifdef FDT
121 fdtbus_todr_attach(self, ia->ia_cookie, &sc->sc_todr);
122 #else
123 todr_attach(&sc->sc_todr);
124 #endif
125 }
126
127 static int
128 pcf8563rtc_gettime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
129 {
130 struct pcf8563rtc_softc *sc = ch->cookie;
131
132 if (pcf8563rtc_clock_read(sc, dt) == 0)
133 return -1;
134
135 return 0;
136 }
137
138 static int
139 pcf8563rtc_settime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
140 {
141 struct pcf8563rtc_softc *sc = ch->cookie;
142
143 if (pcf8563rtc_clock_write(sc, dt) == 0)
144 return -1;
145
146 return 0;
147 }
148
149 static int
150 pcf8563rtc_clock_read(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
151 {
152 uint8_t bcd[PCF8563_NREGS];
153 uint8_t reg = PCF8563_R_SECOND;
154 const int flags = I2C_F_POLL;
155
156 if (iic_acquire_bus(sc->sc_tag, flags)) {
157 device_printf(sc->sc_dev, "acquire bus for read failed\n");
158 return 0;
159 }
160
161 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, ®, 1,
162 &bcd[reg], PCF8563_R_YEAR - reg + 1, flags)) {
163 iic_release_bus(sc->sc_tag, flags);
164 device_printf(sc->sc_dev, "read failed\n");
165 return 0;
166 }
167
168 iic_release_bus(sc->sc_tag, flags);
169
170 if (bcd[PCF8563_R_SECOND] & PCF8563_M_VL)
171 return 0;
172
173 dt->dt_sec = bcdtobin(bcd[PCF8563_R_SECOND] & PCF8563_M_SECOND);
174 dt->dt_min = bcdtobin(bcd[PCF8563_R_MINUTE] & PCF8563_M_MINUTE);
175 dt->dt_hour = bcdtobin(bcd[PCF8563_R_HOUR] & PCF8563_M_HOUR);
176 dt->dt_day = bcdtobin(bcd[PCF8563_R_DAY] & PCF8563_M_DAY);
177 dt->dt_wday = bcdtobin(bcd[PCF8563_R_WEEKDAY] & PCF8563_M_WEEKDAY);
178 dt->dt_mon = bcdtobin(bcd[PCF8563_R_MONTH] & PCF8563_M_MONTH);
179 dt->dt_year = 1900 +
180 (bcdtobin(bcd[PCF8563_R_YEAR] & PCF8563_M_YEAR) % 100);
181 if ((bcd[PCF8563_R_MONTH] & PCF8563_M_CENTURY) == 0)
182 dt->dt_year += 100;
183
184 return 1;
185 }
186
187 static int
188 pcf8563rtc_clock_write(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
189 {
190 uint8_t bcd[PCF8563_NREGS];
191 uint8_t reg = PCF8563_R_SECOND;
192 const int flags = I2C_F_POLL;
193
194 bcd[PCF8563_R_SECOND] = bintobcd(dt->dt_sec);
195 bcd[PCF8563_R_MINUTE] = bintobcd(dt->dt_min);
196 bcd[PCF8563_R_HOUR] = bintobcd(dt->dt_hour);
197 bcd[PCF8563_R_DAY] = bintobcd(dt->dt_day);
198 bcd[PCF8563_R_WEEKDAY] = bintobcd(dt->dt_wday);
199 bcd[PCF8563_R_MONTH] = bintobcd(dt->dt_mon);
200 bcd[PCF8563_R_YEAR] = bintobcd(dt->dt_year % 100);
201 if (dt->dt_year < 2000)
202 bcd[PCF8563_R_MONTH] |= PCF8563_M_CENTURY;
203
204 if (iic_acquire_bus(sc->sc_tag, flags)) {
205 device_printf(sc->sc_dev, "acquire bus for write failed\n");
206 return 0;
207 }
208
209 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, ®, 1,
210 &bcd[reg], PCF8563_R_YEAR - reg + 1, flags)) {
211 iic_release_bus(sc->sc_tag, flags);
212 device_printf(sc->sc_dev, "write failed\n");
213 return 0;
214 }
215
216 iic_release_bus(sc->sc_tag, flags);
217
218 return 1;
219 }
220