pcagpio.c revision 1.4 1 /* $NetBSD: pcagpio.c,v 1.4 2020/10/27 20:13:21 jdc Exp $ */
2
3 /*-
4 * Copyright (c) 2020 Michael Lorenz
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 NETBSD FOUNDATION, INC. 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 FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * a driver for Philips Semiconductor PCA9555 GPIO controllers
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: pcagpio.c,v 1.4 2020/10/27 20:13:21 jdc Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #ifdef PCAGPIO_DEBUG
40 #include <sys/kernel.h>
41 #endif
42 #include <sys/conf.h>
43 #include <sys/bus.h>
44
45 #include <dev/i2c/i2cvar.h>
46 #include <dev/led.h>
47
48 #ifdef PCAGPIO_DEBUG
49 #define DPRINTF printf
50 #else
51 #define DPRINTF if (0) printf
52 #endif
53
54 /* commands */
55 #define PCAGPIO_INPUT 0x00 /* line status */
56 #define PCAGPIO_OUTPUT 0x01 /* output status */
57 #define PCAGPIO_REVERT 0x02 /* revert input if set */
58 #define PCAGPIO_CONFIG 0x03 /* input if set, output if not */
59
60 static int pcagpio_match(device_t, cfdata_t, void *);
61 static void pcagpio_attach(device_t, device_t, void *);
62 static int pcagpio_detach(device_t, int);
63 #ifdef PCAGPIO_DEBUG
64 static void pcagpio_timeout(void *);
65 #endif
66
67 /* we can only pass one cookie to led_attach() but we need several values... */
68 struct pcagpio_led {
69 void *cookie;
70 uint32_t mask, v_on, v_off;
71 };
72
73 struct pcagpio_softc {
74 device_t sc_dev;
75 i2c_tag_t sc_i2c;
76 i2c_addr_t sc_addr;
77
78 int sc_is_16bit;
79 uint32_t sc_state;
80 struct pcagpio_led sc_leds[16];
81 int sc_nleds;
82
83 #ifdef PCAGPIO_DEBUG
84 uint32_t sc_dir, sc_in;
85 callout_t sc_timer;
86 #endif
87 };
88
89
90 static void pcagpio_writereg(struct pcagpio_softc *, int, uint32_t);
91 static uint32_t pcagpio_readreg(struct pcagpio_softc *, int);
92 static void pcagpio_attach_led(
93 struct pcagpio_softc *, char *, int, int, int);
94 static int pcagpio_get(void *);
95 static void pcagpio_set(void *, int);
96
97 CFATTACH_DECL_NEW(pcagpio, sizeof(struct pcagpio_softc),
98 pcagpio_match, pcagpio_attach, pcagpio_detach, NULL);
99
100 static const struct device_compatible_entry compat_data[] = {
101 { "i2c-pca9555", 1 },
102 { "pca9555", 1 },
103 { "i2c-pca9556", 0 },
104 { "pca9556", 0 },
105 { NULL, 0 }
106 };
107
108 static int
109 pcagpio_match(device_t parent, cfdata_t match, void *aux)
110 {
111 struct i2c_attach_args *ia = aux;
112 int match_result;
113
114 if (iic_use_direct_match(ia, match, compat_data, &match_result))
115 return match_result;
116
117 return 0;
118 }
119
120 #ifdef PCAGPIO_DEBUG
121 static void
122 printdir(char* name, uint32_t val, uint32_t mask, char letter)
123 {
124 char flags[17], bits[17];
125 uint32_t bit = 0x8000;
126 int i;
127
128 val &= mask;
129 for (i = 0; i < 16; i++) {
130 flags[i] = (mask & bit) ? letter : '-';
131 bits[i] = (val & bit) ? 'X' : ' ';
132 bit = bit >> 1;
133 }
134 flags[16] = 0;
135 bits[16] = 0;
136 printf("%s: dir: %s\n", name, flags);
137 printf("%s: lvl: %s\n", name, bits);
138 }
139 #endif
140
141 static void
142 pcagpio_attach(device_t parent, device_t self, void *aux)
143 {
144 struct pcagpio_softc *sc = device_private(self);
145 struct i2c_attach_args *ia = aux;
146 const struct device_compatible_entry *dce;
147 prop_dictionary_t dict = device_properties(self);
148 prop_array_t pins;
149 prop_dictionary_t pin;
150
151 sc->sc_dev = self;
152 sc->sc_i2c = ia->ia_tag;
153 sc->sc_addr = ia->ia_addr;
154 sc->sc_nleds = 0;
155
156 aprint_naive("\n");
157 sc->sc_is_16bit = 0;
158 if (iic_compatible_match(ia, compat_data, &dce))
159 sc->sc_is_16bit = dce->data;
160
161 aprint_normal(": %s\n", sc->sc_is_16bit ? "PCA9555" : "PCA9556");
162
163 sc->sc_state = pcagpio_readreg(sc, PCAGPIO_OUTPUT);
164
165 #ifdef PCAGPIO_DEBUG
166 uint32_t in, out;
167 sc->sc_dir = pcagpio_readreg(sc, PCAGPIO_CONFIG);
168 sc->sc_in = pcagpio_readreg(sc, PCAGPIO_INPUT);
169 in = sc-> sc_in;
170 out = sc->sc_state;
171
172 out &= ~sc->sc_dir;
173 in &= sc->sc_dir;
174
175 printdir(sc->sc_dev->dv_xname, in, sc->sc_dir, 'I');
176 printdir(sc->sc_dev->dv_xname, out, ~sc->sc_dir, 'O');
177
178 callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
179 callout_reset(&sc->sc_timer, hz*20, pcagpio_timeout, sc);
180
181 #endif
182
183 pins = prop_dictionary_get(dict, "pins");
184 if (pins != NULL) {
185 int i, num, def;
186 char name[32];
187 const char *nptr;
188 bool ok = TRUE, act;
189
190 for (i = 0; i < prop_array_count(pins); i++) {
191 nptr = NULL;
192 pin = prop_array_get(pins, i);
193 ok &= prop_dictionary_get_cstring_nocopy(pin, "name",
194 &nptr);
195 strncpy(name, nptr, 31);
196 ok &= prop_dictionary_get_uint32(pin, "pin", &num);
197 ok &= prop_dictionary_get_bool(
198 pin, "active_high", &act);
199 /* optional default state */
200 def = -1;
201 prop_dictionary_get_int32(pin, "default_state", &def);
202 if (ok) {
203 pcagpio_attach_led(sc, name, num, act, def);
204 }
205 }
206 }
207 }
208
209 static int
210 pcagpio_detach(device_t self, int flags)
211 {
212 #ifdef PCAGPIO_DEBUG
213 struct pcagpio_softc *sc = device_private(self);
214
215 callout_halt(&sc->sc_timer, NULL);
216 callout_destroy(&sc->sc_timer);
217 #endif
218
219 return 0;
220 }
221
222 #ifdef PCAGPIO_DEBUG
223 static void
224 pcagpio_timeout(void *v)
225 {
226 struct pcagpio_softc *sc = v;
227 uint32_t out, dir, in, o_out, o_in;
228
229 out = pcagpio_readreg(sc, PCAGPIO_OUTPUT);
230 dir = pcagpio_readreg(sc, PCAGPIO_CONFIG);
231 in = pcagpio_readreg(sc, PCAGPIO_INPUT);
232 if (out != sc->sc_state || dir != sc->sc_dir || in != sc->sc_in) {
233 aprint_normal_dev(sc->sc_dev, "status change\n");
234 o_out = sc->sc_state;
235 o_in = sc->sc_in;
236 o_out &= ~sc->sc_dir;
237 o_in &= sc->sc_dir;
238 printdir(sc->sc_dev->dv_xname, o_in, sc->sc_dir, 'I');
239 printdir(sc->sc_dev->dv_xname, o_out, ~sc->sc_dir, 'O');
240 sc->sc_state = out;
241 sc->sc_dir = dir;
242 sc->sc_in = in;
243 out &= ~sc->sc_dir;
244 in &= sc->sc_dir;
245 printdir(sc->sc_dev->dv_xname, in, sc->sc_dir, 'I');
246 printdir(sc->sc_dev->dv_xname, out, ~sc->sc_dir, 'O');
247 }
248 callout_reset(&sc->sc_timer, hz*60, pcagpio_timeout, sc);
249 }
250 #endif
251
252 static void
253 pcagpio_writereg(struct pcagpio_softc *sc, int reg, uint32_t val)
254 {
255 uint8_t cmd;
256
257 iic_acquire_bus(sc->sc_i2c, 0);
258 if (sc->sc_is_16bit) {
259 uint16_t creg;
260 cmd = reg << 1;
261 creg = htole16(val);
262 iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
263 sc->sc_addr, &cmd, 1, &creg, 2, 0);
264 } else {
265 uint8_t creg;
266 cmd = reg;
267 creg = (uint8_t)val;
268 iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
269 sc->sc_addr, &cmd, 1, &creg, 1, 0);
270 }
271 if (reg == PCAGPIO_OUTPUT) sc->sc_state = val;
272 iic_release_bus(sc->sc_i2c, 0);
273 }
274
275 static uint32_t pcagpio_readreg(struct pcagpio_softc *sc, int reg)
276 {
277 uint8_t cmd;
278 uint32_t ret;
279
280 iic_acquire_bus(sc->sc_i2c, 0);
281 if (sc->sc_is_16bit) {
282 uint16_t creg;
283 cmd = reg << 1;
284 iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
285 sc->sc_addr, &cmd, 1, &creg, 2, 0);
286 ret = le16toh(creg);
287 } else {
288 uint8_t creg;
289 cmd = reg;
290 iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
291 sc->sc_addr, &cmd, 1, &creg, 1, 0);
292 ret = creg;
293 }
294 iic_release_bus(sc->sc_i2c, 0);
295 return ret;
296 }
297
298 static void
299 pcagpio_attach_led(struct pcagpio_softc *sc, char *n, int pin, int act, int def)
300 {
301 struct pcagpio_led *l;
302
303 l = &sc->sc_leds[sc->sc_nleds];
304 l->cookie = sc;
305 l->mask = 1 << pin;
306 l->v_on = act ? l->mask : 0;
307 l->v_off = act ? 0 : l->mask;
308 led_attach(n, l, pcagpio_get, pcagpio_set);
309 if (def != -1) pcagpio_set(l, def);
310 DPRINTF("%s: %04x %04x %04x def %d\n",
311 __func__, l->mask, l->v_on, l->v_off, def);
312 sc->sc_nleds++;
313 }
314
315 static int
316 pcagpio_get(void *cookie)
317 {
318 struct pcagpio_led *l = cookie;
319 struct pcagpio_softc *sc = l->cookie;
320
321 return ((sc->sc_state & l->mask) == l->v_on);
322 }
323
324 static void
325 pcagpio_set(void *cookie, int val)
326 {
327 struct pcagpio_led *l = cookie;
328 struct pcagpio_softc *sc = l->cookie;
329 uint32_t newstate;
330
331 newstate = sc->sc_state & ~l->mask;
332 newstate |= val ? l->v_on : l->v_off;
333 DPRINTF("%s: %04x -> %04x, %04x %04x %04x\n", __func__,
334 sc->sc_state, newstate, l->mask, l->v_on, l->v_off);
335 if (newstate != sc->sc_state)
336 pcagpio_writereg(sc, PCAGPIO_OUTPUT, newstate);
337 }
338