1/* $NetBSD: tcagpio.c,v 1.8 2021/01/27 02:29:48 thorpej Exp $ */ 2 3/*- 4 * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca> 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#include <sys/cdefs.h> 30__KERNEL_RCSID(0, "$NetBSD: tcagpio.c,v 1.8 2021/01/27 02:29:48 thorpej Exp $"); 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/kernel.h> 35#include <sys/device.h> 36#include <sys/conf.h> 37#include <sys/bus.h> 38#include <sys/kmem.h> 39#include <sys/gpio.h> 40 41#include <dev/i2c/i2cvar.h> 42 43#include <dev/fdt/fdtvar.h> 44 45#define PORT_BANK(pin) (pin / 8) 46#define PORT_BIT(pin) __BIT(pin % 8) 47 48#define PORT_IN(pin) (0x00 + PORT_BANK(pin)) 49#define PORT_OUT(pin) (0x02 + PORT_BANK(pin)) 50#define PORT_POLARITY(pin) (0x04 + PORT_BANK(pin)) 51#define PORT_CFG(pin) (0x06 + PORT_BANK(pin)) 52 53#define TCA_PIN_COUNT 16 54 55struct tcagpio_softc { 56 device_t sc_dev; 57 i2c_tag_t sc_i2c; 58 i2c_addr_t sc_addr; 59 int sc_phandle; 60 61 int sc_npins; 62}; 63 64struct tcagpio_pin { 65 struct tcagpio_softc *pin_sc; 66 int pin_num; 67 int pin_flags; 68 bool pin_actlo; 69}; 70 71static const struct device_compatible_entry compat_data[] = { 72 { .compat = "ti,tca9539" }, 73 DEVICE_COMPAT_EOL 74}; 75 76static uint8_t 77tcagpio_read(struct tcagpio_softc *sc, uint8_t reg, int flags) 78{ 79 uint8_t val = 0; 80 int error; 81 82 error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr, 83 ®, 1, &val, 1, flags); 84 if (error != 0) 85 aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error); 86 87 return val; 88} 89 90static void 91tcagpio_write(struct tcagpio_softc *sc, uint8_t reg, uint8_t val, int flags) 92{ 93 uint8_t buf[2]; 94 int error; 95 96 buf[0] = reg; 97 buf[1] = val; 98 99 error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 100 NULL, 0, buf, 2, flags); 101 if (error != 0) 102 aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error); 103} 104 105#define I2C_READ(sc, reg) tcagpio_read((sc), (reg), 0) 106#define I2C_WRITE(sc, reg, val) tcagpio_write((sc), (reg), (val), 0) 107#define I2C_LOCK(sc) iic_acquire_bus((sc)->sc_i2c, 0) 108#define I2C_UNLOCK(sc) iic_release_bus((sc)->sc_i2c, 0) 109 110static int 111tcagpio_gpio_config(struct tcagpio_softc *sc, int pin, int flags) 112{ 113 uint16_t gpio; 114 115 KASSERT(pin >= 0 && pin < sc->sc_npins); 116 117 gpio = I2C_READ(sc, PORT_CFG(pin)); 118 119 switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) { 120 case GPIO_PIN_INPUT: 121 gpio |= PORT_BIT(pin); 122 break; 123 case GPIO_PIN_OUTPUT: 124 gpio &= ~PORT_BIT(pin); 125 break; 126 default: 127 return EINVAL; 128 } 129 130 I2C_WRITE(sc, PORT_CFG(pin), gpio); 131 132 return 0; 133} 134 135static void * 136tcagpio_gpio_acquire(device_t dev, const void *data, size_t len, int flags) 137{ 138 struct tcagpio_softc * const sc = device_private(dev); 139 struct tcagpio_pin *gpin; 140 const u_int *gpio = data; 141 int error; 142 143 if (len != 12) 144 return NULL; 145 146 const uint8_t pin = be32toh(gpio[1]) & 0xff; 147 const bool actlo = (be32toh(gpio[2]) & __BIT(0)) != 0; 148 149 if (pin >= sc->sc_npins) 150 return NULL; 151 152 I2C_LOCK(sc); 153 error = tcagpio_gpio_config(sc, pin, flags); 154 I2C_UNLOCK(sc); 155 156 if (error != 0) { 157 device_printf(dev, "bad pin %d config %#x\n", pin, flags); 158 return NULL; 159 } 160 161 gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP); 162 gpin->pin_sc = sc; 163 gpin->pin_num = pin; 164 gpin->pin_flags = flags; 165 gpin->pin_actlo = actlo; 166 167 return gpin; 168} 169 170static void 171tcagpio_gpio_release(device_t dev, void *priv) 172{ 173 struct tcagpio_softc * const sc = device_private(dev); 174 struct tcagpio_pin *gpin = priv; 175 176 I2C_LOCK(sc); 177 tcagpio_gpio_config(sc, gpin->pin_num, GPIO_PIN_INPUT); 178 I2C_UNLOCK(sc); 179 180 kmem_free(gpin, sizeof(*gpin)); 181} 182 183static int 184tcagpio_gpio_read(device_t dev, void *priv, bool raw) 185{ 186 struct tcagpio_softc * const sc = device_private(dev); 187 struct tcagpio_pin *gpin = priv; 188 uint8_t gpio; 189 int val; 190 191 I2C_LOCK(sc); 192 gpio = I2C_READ(sc, PORT_IN(gpin->pin_num)); 193 I2C_UNLOCK(sc); 194 195 val = __SHIFTOUT(gpio, PORT_BIT(gpin->pin_num)); 196 if (!raw && gpin->pin_actlo) 197 val = !val; 198 199#ifdef TCAGPIO_DEBUG 200 device_printf(sc->sc_dev, "GPIO%d rd %02x (%d %d)\n", 201 gpin->pin_num, gpio, 202 (int)__SHIFTOUT(gpio, PORT_BIT(gpin->pin_num)), val); 203#endif 204 205 return val; 206} 207 208static void 209tcagpio_gpio_write(device_t dev, void *priv, int val, bool raw) 210{ 211 struct tcagpio_softc * const sc = device_private(dev); 212 struct tcagpio_pin *gpin = priv; 213 uint8_t gpio; 214 215 if (!raw && gpin->pin_actlo) 216 val = !val; 217 218 I2C_LOCK(sc); 219 gpio = I2C_READ(sc, PORT_OUT(gpin->pin_num)); 220 gpio &= ~PORT_BIT(gpin->pin_num); 221 gpio |= __SHIFTIN(val, PORT_BIT(gpin->pin_num)); 222#ifdef TCAGPIO_DEBUG 223 device_printf(sc->sc_dev, "GPIO%d wr %02x -> %02x\n", 224 gpin->pin_num, 225 I2C_READ(sc, PORT_OUT(gpin->pin_num)), gpio); 226#endif 227 I2C_WRITE(sc, PORT_OUT(gpin->pin_num), gpio); 228 I2C_UNLOCK(sc); 229} 230 231static struct fdtbus_gpio_controller_func tcagpio_gpio_funcs = { 232 .acquire = tcagpio_gpio_acquire, 233 .release = tcagpio_gpio_release, 234 .read = tcagpio_gpio_read, 235 .write = tcagpio_gpio_write, 236}; 237 238static void 239tcagpio_gpio_attach(struct tcagpio_softc *sc) 240{ 241 fdtbus_register_gpio_controller(sc->sc_dev, sc->sc_phandle, 242 &tcagpio_gpio_funcs); 243} 244 245static int 246tcagpio_match(device_t parent, cfdata_t match, void *aux) 247{ 248 struct i2c_attach_args *ia = aux; 249 int match_result; 250 251 if (iic_use_direct_match(ia, match, compat_data, &match_result)) 252 return match_result; 253 254 return 0; 255} 256 257static void 258tcagpio_attach(device_t parent, device_t self, void *aux) 259{ 260 struct tcagpio_softc * const sc = device_private(self); 261 struct i2c_attach_args *ia = aux; 262 263 sc->sc_dev = self; 264 sc->sc_i2c = ia->ia_tag; 265 sc->sc_addr = ia->ia_addr; 266 sc->sc_phandle = ia->ia_cookie; 267 sc->sc_npins = TCA_PIN_COUNT; 268 269 aprint_naive("\n"); 270 aprint_normal(": I2C/SMBus I/O Expander\n"); 271 272 tcagpio_gpio_attach(sc); 273} 274 275CFATTACH_DECL_NEW(tcagpio, sizeof(struct tcagpio_softc), 276 tcagpio_match, tcagpio_attach, NULL, NULL); 277