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