pcagpio.c revision 1.5 1 1.4 jdc /* $NetBSD: pcagpio.c,v 1.5 2020/10/29 06:50:53 jdc 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.4 jdc __KERNEL_RCSID(0, "$NetBSD: pcagpio.c,v 1.5 2020/10/29 06:50:53 jdc 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.2 macallan uint32_t mask, v_on, v_off;
71 1.2 macallan };
72 1.2 macallan
73 1.1 macallan struct pcagpio_softc {
74 1.1 macallan device_t sc_dev;
75 1.1 macallan i2c_tag_t sc_i2c;
76 1.1 macallan i2c_addr_t sc_addr;
77 1.1 macallan
78 1.1 macallan int sc_is_16bit;
79 1.2 macallan uint32_t sc_state;
80 1.2 macallan struct pcagpio_led sc_leds[16];
81 1.2 macallan int sc_nleds;
82 1.4 jdc
83 1.4 jdc #ifdef PCAGPIO_DEBUG
84 1.4 jdc uint32_t sc_dir, sc_in;
85 1.4 jdc callout_t sc_timer;
86 1.4 jdc #endif
87 1.1 macallan };
88 1.1 macallan
89 1.1 macallan
90 1.1 macallan static void pcagpio_writereg(struct pcagpio_softc *, int, uint32_t);
91 1.1 macallan static uint32_t pcagpio_readreg(struct pcagpio_softc *, int);
92 1.3 macallan static void pcagpio_attach_led(
93 1.3 macallan struct pcagpio_softc *, char *, int, int, int);
94 1.2 macallan static int pcagpio_get(void *);
95 1.2 macallan static void pcagpio_set(void *, int);
96 1.1 macallan
97 1.1 macallan CFATTACH_DECL_NEW(pcagpio, sizeof(struct pcagpio_softc),
98 1.4 jdc pcagpio_match, pcagpio_attach, pcagpio_detach, NULL);
99 1.1 macallan
100 1.1 macallan static const struct device_compatible_entry compat_data[] = {
101 1.1 macallan { "i2c-pca9555", 1 },
102 1.1 macallan { "pca9555", 1 },
103 1.1 macallan { "i2c-pca9556", 0 },
104 1.1 macallan { "pca9556", 0 },
105 1.1 macallan { NULL, 0 }
106 1.1 macallan };
107 1.1 macallan
108 1.1 macallan static int
109 1.1 macallan pcagpio_match(device_t parent, cfdata_t match, void *aux)
110 1.1 macallan {
111 1.1 macallan struct i2c_attach_args *ia = aux;
112 1.1 macallan int match_result;
113 1.1 macallan
114 1.1 macallan if (iic_use_direct_match(ia, match, compat_data, &match_result))
115 1.1 macallan return match_result;
116 1.1 macallan
117 1.1 macallan return 0;
118 1.1 macallan }
119 1.1 macallan
120 1.2 macallan #ifdef PCAGPIO_DEBUG
121 1.1 macallan static void
122 1.4 jdc printdir(char* name, uint32_t val, uint32_t mask, char letter)
123 1.1 macallan {
124 1.1 macallan char flags[17], bits[17];
125 1.1 macallan uint32_t bit = 0x8000;
126 1.1 macallan int i;
127 1.1 macallan
128 1.1 macallan val &= mask;
129 1.1 macallan for (i = 0; i < 16; i++) {
130 1.1 macallan flags[i] = (mask & bit) ? letter : '-';
131 1.1 macallan bits[i] = (val & bit) ? 'X' : ' ';
132 1.1 macallan bit = bit >> 1;
133 1.1 macallan }
134 1.1 macallan flags[16] = 0;
135 1.1 macallan bits[16] = 0;
136 1.4 jdc printf("%s: dir: %s\n", name, flags);
137 1.4 jdc printf("%s: lvl: %s\n", name, bits);
138 1.1 macallan }
139 1.2 macallan #endif
140 1.1 macallan
141 1.1 macallan static void
142 1.1 macallan pcagpio_attach(device_t parent, device_t self, void *aux)
143 1.1 macallan {
144 1.1 macallan struct pcagpio_softc *sc = device_private(self);
145 1.1 macallan struct i2c_attach_args *ia = aux;
146 1.1 macallan const struct device_compatible_entry *dce;
147 1.2 macallan prop_dictionary_t dict = device_properties(self);
148 1.2 macallan prop_array_t pins;
149 1.2 macallan prop_dictionary_t pin;
150 1.1 macallan
151 1.1 macallan sc->sc_dev = self;
152 1.1 macallan sc->sc_i2c = ia->ia_tag;
153 1.1 macallan sc->sc_addr = ia->ia_addr;
154 1.2 macallan sc->sc_nleds = 0;
155 1.1 macallan
156 1.1 macallan aprint_naive("\n");
157 1.1 macallan sc->sc_is_16bit = 0;
158 1.1 macallan if (iic_compatible_match(ia, compat_data, &dce))
159 1.1 macallan sc->sc_is_16bit = dce->data;
160 1.1 macallan
161 1.1 macallan aprint_normal(": %s\n", sc->sc_is_16bit ? "PCA9555" : "PCA9556");
162 1.1 macallan
163 1.2 macallan sc->sc_state = pcagpio_readreg(sc, PCAGPIO_OUTPUT);
164 1.2 macallan
165 1.2 macallan #ifdef PCAGPIO_DEBUG
166 1.4 jdc uint32_t in, out;
167 1.4 jdc sc->sc_dir = pcagpio_readreg(sc, PCAGPIO_CONFIG);
168 1.4 jdc sc->sc_in = pcagpio_readreg(sc, PCAGPIO_INPUT);
169 1.4 jdc in = sc-> sc_in;
170 1.2 macallan out = sc->sc_state;
171 1.1 macallan
172 1.4 jdc out &= ~sc->sc_dir;
173 1.4 jdc in &= sc->sc_dir;
174 1.1 macallan
175 1.4 jdc printdir(sc->sc_dev->dv_xname, in, sc->sc_dir, 'I');
176 1.4 jdc printdir(sc->sc_dev->dv_xname, out, ~sc->sc_dir, 'O');
177 1.4 jdc
178 1.4 jdc callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
179 1.4 jdc callout_reset(&sc->sc_timer, hz*20, pcagpio_timeout, sc);
180 1.4 jdc
181 1.2 macallan #endif
182 1.2 macallan
183 1.2 macallan pins = prop_dictionary_get(dict, "pins");
184 1.2 macallan if (pins != NULL) {
185 1.2 macallan int i, num, def;
186 1.2 macallan char name[32];
187 1.5 jdc const char *spptr, *nptr;
188 1.2 macallan bool ok = TRUE, act;
189 1.2 macallan
190 1.2 macallan for (i = 0; i < prop_array_count(pins); i++) {
191 1.2 macallan nptr = NULL;
192 1.2 macallan pin = prop_array_get(pins, i);
193 1.3 macallan ok &= prop_dictionary_get_cstring_nocopy(pin, "name",
194 1.3 macallan &nptr);
195 1.2 macallan ok &= prop_dictionary_get_uint32(pin, "pin", &num);
196 1.5 jdc ok &= prop_dictionary_get_bool( pin, "active_high",
197 1.5 jdc &act);
198 1.2 macallan /* optional default state */
199 1.2 macallan def = -1;
200 1.2 macallan prop_dictionary_get_int32(pin, "default_state", &def);
201 1.5 jdc if (!ok)
202 1.5 jdc continue;
203 1.5 jdc /* Extract pin type from the name */
204 1.5 jdc spptr = strstr(nptr, " ");
205 1.5 jdc if (spptr == NULL)
206 1.5 jdc continue;
207 1.5 jdc spptr += 1;
208 1.5 jdc strncpy(name, spptr, 31);
209 1.5 jdc if (!strncmp(nptr, "LED ", 4))
210 1.2 macallan pcagpio_attach_led(sc, name, num, act, def);
211 1.2 macallan }
212 1.2 macallan }
213 1.1 macallan }
214 1.1 macallan
215 1.4 jdc static int
216 1.4 jdc pcagpio_detach(device_t self, int flags)
217 1.4 jdc {
218 1.4 jdc #ifdef PCAGPIO_DEBUG
219 1.4 jdc struct pcagpio_softc *sc = device_private(self);
220 1.4 jdc
221 1.4 jdc callout_halt(&sc->sc_timer, NULL);
222 1.4 jdc callout_destroy(&sc->sc_timer);
223 1.4 jdc #endif
224 1.4 jdc
225 1.4 jdc return 0;
226 1.4 jdc }
227 1.4 jdc
228 1.4 jdc #ifdef PCAGPIO_DEBUG
229 1.4 jdc static void
230 1.4 jdc pcagpio_timeout(void *v)
231 1.4 jdc {
232 1.4 jdc struct pcagpio_softc *sc = v;
233 1.4 jdc uint32_t out, dir, in, o_out, o_in;
234 1.4 jdc
235 1.4 jdc out = pcagpio_readreg(sc, PCAGPIO_OUTPUT);
236 1.4 jdc dir = pcagpio_readreg(sc, PCAGPIO_CONFIG);
237 1.4 jdc in = pcagpio_readreg(sc, PCAGPIO_INPUT);
238 1.4 jdc if (out != sc->sc_state || dir != sc->sc_dir || in != sc->sc_in) {
239 1.4 jdc aprint_normal_dev(sc->sc_dev, "status change\n");
240 1.4 jdc o_out = sc->sc_state;
241 1.4 jdc o_in = sc->sc_in;
242 1.4 jdc o_out &= ~sc->sc_dir;
243 1.4 jdc o_in &= sc->sc_dir;
244 1.4 jdc printdir(sc->sc_dev->dv_xname, o_in, sc->sc_dir, 'I');
245 1.4 jdc printdir(sc->sc_dev->dv_xname, o_out, ~sc->sc_dir, 'O');
246 1.4 jdc sc->sc_state = out;
247 1.4 jdc sc->sc_dir = dir;
248 1.4 jdc sc->sc_in = in;
249 1.4 jdc out &= ~sc->sc_dir;
250 1.4 jdc in &= sc->sc_dir;
251 1.4 jdc printdir(sc->sc_dev->dv_xname, in, sc->sc_dir, 'I');
252 1.4 jdc printdir(sc->sc_dev->dv_xname, out, ~sc->sc_dir, 'O');
253 1.4 jdc }
254 1.4 jdc callout_reset(&sc->sc_timer, hz*60, pcagpio_timeout, sc);
255 1.4 jdc }
256 1.4 jdc #endif
257 1.4 jdc
258 1.1 macallan static void
259 1.1 macallan pcagpio_writereg(struct pcagpio_softc *sc, int reg, uint32_t val)
260 1.1 macallan {
261 1.1 macallan uint8_t cmd;
262 1.1 macallan
263 1.1 macallan iic_acquire_bus(sc->sc_i2c, 0);
264 1.1 macallan if (sc->sc_is_16bit) {
265 1.1 macallan uint16_t creg;
266 1.1 macallan cmd = reg << 1;
267 1.1 macallan creg = htole16(val);
268 1.1 macallan iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
269 1.1 macallan sc->sc_addr, &cmd, 1, &creg, 2, 0);
270 1.1 macallan } else {
271 1.1 macallan uint8_t creg;
272 1.1 macallan cmd = reg;
273 1.1 macallan creg = (uint8_t)val;
274 1.1 macallan iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
275 1.1 macallan sc->sc_addr, &cmd, 1, &creg, 1, 0);
276 1.1 macallan }
277 1.2 macallan if (reg == PCAGPIO_OUTPUT) sc->sc_state = val;
278 1.1 macallan iic_release_bus(sc->sc_i2c, 0);
279 1.1 macallan }
280 1.1 macallan
281 1.1 macallan static uint32_t pcagpio_readreg(struct pcagpio_softc *sc, int reg)
282 1.1 macallan {
283 1.1 macallan uint8_t cmd;
284 1.1 macallan uint32_t ret;
285 1.1 macallan
286 1.1 macallan iic_acquire_bus(sc->sc_i2c, 0);
287 1.1 macallan if (sc->sc_is_16bit) {
288 1.1 macallan uint16_t creg;
289 1.1 macallan cmd = reg << 1;
290 1.1 macallan iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
291 1.1 macallan sc->sc_addr, &cmd, 1, &creg, 2, 0);
292 1.1 macallan ret = le16toh(creg);
293 1.1 macallan } else {
294 1.1 macallan uint8_t creg;
295 1.1 macallan cmd = reg;
296 1.1 macallan iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
297 1.1 macallan sc->sc_addr, &cmd, 1, &creg, 1, 0);
298 1.1 macallan ret = creg;
299 1.1 macallan }
300 1.1 macallan iic_release_bus(sc->sc_i2c, 0);
301 1.1 macallan return ret;
302 1.1 macallan }
303 1.2 macallan
304 1.2 macallan static void
305 1.2 macallan pcagpio_attach_led(struct pcagpio_softc *sc, char *n, int pin, int act, int def)
306 1.2 macallan {
307 1.2 macallan struct pcagpio_led *l;
308 1.2 macallan
309 1.2 macallan l = &sc->sc_leds[sc->sc_nleds];
310 1.2 macallan l->cookie = sc;
311 1.2 macallan l->mask = 1 << pin;
312 1.2 macallan l->v_on = act ? l->mask : 0;
313 1.2 macallan l->v_off = act ? 0 : l->mask;
314 1.2 macallan led_attach(n, l, pcagpio_get, pcagpio_set);
315 1.2 macallan if (def != -1) pcagpio_set(l, def);
316 1.3 macallan DPRINTF("%s: %04x %04x %04x def %d\n",
317 1.3 macallan __func__, l->mask, l->v_on, l->v_off, def);
318 1.2 macallan sc->sc_nleds++;
319 1.2 macallan }
320 1.2 macallan
321 1.2 macallan static int
322 1.2 macallan pcagpio_get(void *cookie)
323 1.2 macallan {
324 1.2 macallan struct pcagpio_led *l = cookie;
325 1.2 macallan struct pcagpio_softc *sc = l->cookie;
326 1.2 macallan
327 1.2 macallan return ((sc->sc_state & l->mask) == l->v_on);
328 1.2 macallan }
329 1.2 macallan
330 1.2 macallan static void
331 1.2 macallan pcagpio_set(void *cookie, int val)
332 1.2 macallan {
333 1.2 macallan struct pcagpio_led *l = cookie;
334 1.2 macallan struct pcagpio_softc *sc = l->cookie;
335 1.2 macallan uint32_t newstate;
336 1.2 macallan
337 1.2 macallan newstate = sc->sc_state & ~l->mask;
338 1.2 macallan newstate |= val ? l->v_on : l->v_off;
339 1.2 macallan DPRINTF("%s: %04x -> %04x, %04x %04x %04x\n", __func__,
340 1.2 macallan sc->sc_state, newstate, l->mask, l->v_on, l->v_off);
341 1.2 macallan if (newstate != sc->sc_state)
342 1.2 macallan pcagpio_writereg(sc, PCAGPIO_OUTPUT, newstate);
343 1.2 macallan }
344