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