1 /* $NetBSD: pcagpio.c,v 1.13 2025/07/09 08:32:48 macallan 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.13 2025/07/09 08:32:48 macallan Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/device.h> 39 #include <sys/conf.h> 40 #include <sys/bus.h> 41 42 #include <dev/i2c/i2cvar.h> 43 #include <dev/led.h> 44 45 #ifdef PCAGPIO_DEBUG 46 #define DPRINTF printf 47 #else 48 #define DPRINTF if (0) printf 49 #endif 50 51 /* commands */ 52 #define PCAGPIO_INPUT 0x00 /* line status */ 53 #define PCAGPIO_OUTPUT 0x01 /* output status */ 54 #define PCAGPIO_REVERT 0x02 /* revert input if set */ 55 #define PCAGPIO_CONFIG 0x03 /* input if set, output if not */ 56 57 static int pcagpio_match(device_t, cfdata_t, void *); 58 static void pcagpio_attach(device_t, device_t, void *); 59 static int pcagpio_detach(device_t, int); 60 61 /* we can only pass one cookie to led_attach() but we need several values... */ 62 struct pcagpio_led { 63 void *cookie; 64 struct led_device *led; 65 uint32_t mask, v_on, v_off; 66 }; 67 68 struct pcagpio_softc { 69 device_t sc_dev; 70 i2c_tag_t sc_i2c; 71 i2c_addr_t sc_addr; 72 73 int sc_is_16bit; 74 uint32_t sc_state; 75 struct pcagpio_led sc_leds[16]; 76 int sc_nleds; 77 78 #ifdef PCAGPIO_DEBUG 79 uint32_t sc_dir, sc_in; 80 #endif 81 }; 82 83 84 static void pcagpio_writereg(struct pcagpio_softc *, int, uint32_t); 85 static uint32_t pcagpio_readreg(struct pcagpio_softc *, int); 86 static void pcagpio_attach_led( 87 struct pcagpio_softc *, char *, int, int, int); 88 static int pcagpio_get(void *); 89 static void pcagpio_set(void *, int); 90 91 CFATTACH_DECL_NEW(pcagpio, sizeof(struct pcagpio_softc), 92 pcagpio_match, pcagpio_attach, pcagpio_detach, NULL); 93 94 static const struct device_compatible_entry compat_data[] = { 95 { .compat = "i2c-pca9555", .value = 1 }, 96 { .compat = "pca9555", .value = 1 }, 97 { .compat = "i2c-pca9556", .value = 0 }, 98 { .compat = "pca9556", .value = 0 }, 99 DEVICE_COMPAT_EOL 100 }; 101 102 static int 103 pcagpio_match(device_t parent, cfdata_t match, void *aux) 104 { 105 struct i2c_attach_args *ia = aux; 106 int match_result; 107 108 if (iic_use_direct_match(ia, match, compat_data, &match_result)) 109 return match_result; 110 111 return 0; 112 } 113 114 #ifdef PCAGPIO_DEBUG 115 static void 116 printdir(const char* name, uint32_t val, uint32_t mask, char letter) 117 { 118 char flags[17], bits[17]; 119 uint32_t bit = 0x8000; 120 int i; 121 122 val &= mask; 123 for (i = 0; i < 16; i++) { 124 flags[i] = (mask & bit) ? letter : '-'; 125 bits[i] = (val & bit) ? 'X' : ' '; 126 bit = bit >> 1; 127 } 128 flags[16] = 0; 129 bits[16] = 0; 130 printf("%s: dir: %s\n", name, flags); 131 printf("%s: lvl: %s\n", name, bits); 132 } 133 #endif 134 135 static void 136 pcagpio_attach(device_t parent, device_t self, void *aux) 137 { 138 struct pcagpio_softc *sc = device_private(self); 139 struct i2c_attach_args *ia = aux; 140 const struct device_compatible_entry *dce; 141 prop_dictionary_t dict = device_properties(self); 142 prop_array_t pins; 143 prop_dictionary_t pin; 144 145 sc->sc_dev = self; 146 sc->sc_i2c = ia->ia_tag; 147 sc->sc_addr = ia->ia_addr; 148 sc->sc_nleds = 0; 149 150 aprint_naive("\n"); 151 sc->sc_is_16bit = 0; 152 if ((dce = iic_compatible_lookup(ia, compat_data)) != NULL) 153 sc->sc_is_16bit = dce->value; 154 155 aprint_normal(": %s\n", sc->sc_is_16bit ? "PCA9555" : "PCA9556"); 156 157 sc->sc_state = pcagpio_readreg(sc, PCAGPIO_OUTPUT); 158 159 #ifdef PCAGPIO_DEBUG 160 uint32_t in, out; 161 sc->sc_dir = pcagpio_readreg(sc, PCAGPIO_CONFIG); 162 sc->sc_in = pcagpio_readreg(sc, PCAGPIO_INPUT); 163 in = sc-> sc_in; 164 out = sc->sc_state; 165 166 out &= ~sc->sc_dir; 167 in &= sc->sc_dir; 168 169 printdir(device_xname(sc->sc_dev), in, sc->sc_dir, 'I'); 170 printdir(device_xname(sc->sc_dev), out, ~sc->sc_dir, 'O'); 171 172 #endif 173 174 pins = prop_dictionary_get(dict, "pins"); 175 if (pins != NULL) { 176 int i, num, def; 177 char name[32]; 178 const char *spptr, *nptr; 179 bool ok = TRUE, act; 180 181 for (i = 0; i < prop_array_count(pins); i++) { 182 nptr = NULL; 183 pin = prop_array_get(pins, i); 184 ok &= prop_dictionary_get_string(pin, "name", 185 &nptr); 186 ok &= prop_dictionary_get_uint32(pin, "pin", &num); 187 ok &= prop_dictionary_get_bool( pin, "active_high", 188 &act); 189 /* optional default state */ 190 def = -1; 191 prop_dictionary_get_int32(pin, "default_state", &def); 192 if (!ok) 193 continue; 194 /* Extract pin type from the name */ 195 spptr = strstr(nptr, " "); 196 if (spptr == NULL) 197 continue; 198 spptr += 1; 199 strncpy(name, spptr, 31); 200 if (!strncmp(nptr, "LED ", 4)) 201 pcagpio_attach_led(sc, name, num, act, def); 202 } 203 } 204 } 205 206 static int 207 pcagpio_detach(device_t self, int flags) 208 { 209 struct pcagpio_softc *sc = device_private(self); 210 int i; 211 212 for (i = 0; i < sc->sc_nleds; i++) 213 led_detach(sc->sc_leds[i].led); 214 215 return 0; 216 } 217 218 static void 219 pcagpio_writereg(struct pcagpio_softc *sc, int reg, uint32_t val) 220 { 221 uint8_t cmd; 222 223 iic_acquire_bus(sc->sc_i2c, 0); 224 if (sc->sc_is_16bit) { 225 uint16_t creg; 226 cmd = reg << 1; 227 creg = htole16(val); 228 iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, 229 sc->sc_addr, &cmd, 1, &creg, 2, 0); 230 } else { 231 uint8_t creg; 232 cmd = reg; 233 creg = (uint8_t)val; 234 iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, 235 sc->sc_addr, &cmd, 1, &creg, 1, 0); 236 } 237 if (reg == PCAGPIO_OUTPUT) sc->sc_state = val; 238 iic_release_bus(sc->sc_i2c, 0); 239 } 240 241 static uint32_t pcagpio_readreg(struct pcagpio_softc *sc, int reg) 242 { 243 uint8_t cmd; 244 uint32_t ret; 245 246 iic_acquire_bus(sc->sc_i2c, 0); 247 if (sc->sc_is_16bit) { 248 uint16_t creg; 249 cmd = reg << 1; 250 iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, 251 sc->sc_addr, &cmd, 1, &creg, 2, 0); 252 ret = le16toh(creg); 253 } else { 254 uint8_t creg; 255 cmd = reg; 256 iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, 257 sc->sc_addr, &cmd, 1, &creg, 1, 0); 258 ret = creg; 259 } 260 iic_release_bus(sc->sc_i2c, 0); 261 return ret; 262 } 263 264 static void 265 pcagpio_attach_led(struct pcagpio_softc *sc, char *n, int pin, int act, int def) 266 { 267 struct pcagpio_led *l; 268 269 l = &sc->sc_leds[sc->sc_nleds]; 270 l->cookie = sc; 271 l->mask = 1 << pin; 272 l->v_on = act ? l->mask : 0; 273 l->v_off = act ? 0 : l->mask; 274 l->led = led_attach(n, l, pcagpio_get, pcagpio_set); 275 if (def != -1) pcagpio_set(l, def); 276 DPRINTF("%s: %04x %04x %04x def %d\n", 277 __func__, l->mask, l->v_on, l->v_off, def); 278 sc->sc_nleds++; 279 } 280 281 static int 282 pcagpio_get(void *cookie) 283 { 284 struct pcagpio_led *l = cookie; 285 struct pcagpio_softc *sc = l->cookie; 286 287 return ((sc->sc_state & l->mask) == l->v_on); 288 } 289 290 static void 291 pcagpio_set(void *cookie, int val) 292 { 293 struct pcagpio_led *l = cookie; 294 struct pcagpio_softc *sc = l->cookie; 295 uint32_t newstate; 296 297 newstate = sc->sc_state & ~l->mask; 298 newstate |= val ? l->v_on : l->v_off; 299 DPRINTF("%s: %04x -> %04x, %04x %04x %04x\n", __func__, 300 sc->sc_state, newstate, l->mask, l->v_on, l->v_off); 301 if (newstate != sc->sc_state) 302 pcagpio_writereg(sc, PCAGPIO_OUTPUT, newstate); 303 } 304