Sat Apr 11 20:05:44 2015 UTC ()
Always use polling mode for now, hummingbird doesn't work otherwise.


(joerg)
diff -r1.6 -r1.7 src/sys/dev/i2c/pcf8563.c

cvs diff -r1.6 -r1.7 src/sys/dev/i2c/pcf8563.c (switch to unified diff)

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