Home | History | Annotate | Line # | Download | only in i2c
max77620.c revision 1.9
      1 /* $NetBSD: max77620.c,v 1.9 2021/01/17 21:42:35 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: max77620.c,v 1.9 2021/01/17 21:42:35 thorpej Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/mutex.h>
     35 #include <sys/kernel.h>
     36 #include <sys/device.h>
     37 #include <sys/conf.h>
     38 #include <sys/bus.h>
     39 #include <sys/kmem.h>
     40 #include <sys/gpio.h>
     41 
     42 #include <dev/i2c/i2cvar.h>
     43 
     44 #include <dev/fdt/fdtvar.h>
     45 
     46 #define	MAX_GPIO_REG(n)		(0x36 + (n))
     47 #define	 MAX_GPIO_DEBOUNCE	__BITS(7,6)
     48 #define	 MAX_GPIO_INT		__BITS(5,4)
     49 #define	 MAX_GPIO_OUTPUT_VAL	__BIT(3)
     50 #define	 MAX_GPIO_INPUT_VAL	__BIT(2)
     51 #define	 MAX_GPIO_DIR		__BIT(1)
     52 #define	 MAX_GPIO_DRV		__BIT(0)
     53 
     54 #define	MAX_GPIO_COUNT		8
     55 
     56 struct max77620_softc {
     57 	device_t	sc_dev;
     58 	i2c_tag_t	sc_i2c;
     59 	i2c_addr_t	sc_addr;
     60 	int		sc_phandle;
     61 };
     62 
     63 struct max77620_pin {
     64 	struct max77620_softc	*pin_sc;
     65 	int			pin_num;
     66 	int			pin_flags;
     67 	bool			pin_actlo;
     68 };
     69 
     70 static const struct device_compatible_entry compat_data[] = {
     71 	{ .compat = "maxim,max77620" },
     72 
     73 	{ 0 }
     74 };
     75 
     76 static uint8_t
     77 max77620_read(struct max77620_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 	    &reg, 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 
     90 static void
     91 max77620_write(struct max77620_softc *sc, uint8_t reg, uint8_t val, int flags)
     92 {
     93 	int error;
     94 
     95 	error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
     96 	    &reg, 1, &val, 1, flags);
     97 	if (error != 0)
     98 		aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
     99 }
    100 
    101 #define	I2C_READ(sc, reg)	max77620_read((sc), (reg), 0)
    102 #define	I2C_WRITE(sc, reg, val)	max77620_write((sc), (reg), (val), 0)
    103 #define	I2C_LOCK(sc)		iic_acquire_bus((sc)->sc_i2c, 0)
    104 #define	I2C_UNLOCK(sc)		iic_release_bus((sc)->sc_i2c, 0)
    105 
    106 static int
    107 max77620_gpio_config(struct max77620_softc *sc, int pin, int flags)
    108 {
    109 	uint32_t gpio;
    110 
    111 	KASSERT(pin >= 0 && pin < MAX_GPIO_COUNT);
    112 
    113 	gpio = I2C_READ(sc, MAX_GPIO_REG(pin));
    114 
    115 	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
    116 	case GPIO_PIN_INPUT:
    117 		gpio |= MAX_GPIO_DIR;
    118 		break;
    119 	case GPIO_PIN_OUTPUT:
    120 		gpio &= ~MAX_GPIO_DIR;
    121 		break;
    122 	default:
    123 		return EINVAL;
    124 	}
    125 
    126 	switch (flags & (GPIO_PIN_PUSHPULL|GPIO_PIN_OPENDRAIN)) {
    127 	case GPIO_PIN_PUSHPULL:
    128 		gpio |= MAX_GPIO_DRV;
    129 		break;
    130 	case GPIO_PIN_OPENDRAIN:
    131 		gpio &= ~MAX_GPIO_DRV;
    132 		break;
    133 	default:
    134 		return EINVAL;
    135 	}
    136 
    137 	I2C_WRITE(sc, MAX_GPIO_REG(pin), gpio);
    138 
    139 	return 0;
    140 }
    141 
    142 static void *
    143 max77620_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
    144 {
    145 	struct max77620_softc * const sc = device_private(dev);
    146 	struct max77620_pin *gpin;
    147 	const u_int *gpio = data;
    148 	int error;
    149 
    150 	if (len != 12)
    151 		return NULL;
    152 
    153 	const uint8_t pin = be32toh(gpio[1]) & 0xff;
    154 	const bool actlo = (be32toh(gpio[2]) & __BIT(0)) != 0;
    155 	const bool pushpull = (be32toh(gpio[2]) & __BIT(1)) == 0;
    156 	const bool opendrain = (be32toh(gpio[2]) & __BIT(2)) != 0;
    157 
    158 	if (pushpull)
    159 		flags |= GPIO_PIN_PUSHPULL;
    160 	if (opendrain)
    161 		flags |= GPIO_PIN_OPENDRAIN;
    162 
    163 	if (pin >= MAX_GPIO_COUNT)
    164 		return NULL;
    165 
    166 	I2C_LOCK(sc);
    167 	error = max77620_gpio_config(sc, pin, flags);
    168 	I2C_UNLOCK(sc);
    169 
    170 	if (error != 0) {
    171 		device_printf(dev, "bad pin %d config %#x\n", pin, flags);
    172 		return NULL;
    173 	}
    174 
    175 	gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
    176 	gpin->pin_sc = sc;
    177 	gpin->pin_num = pin;
    178 	gpin->pin_flags = flags;
    179 	gpin->pin_actlo = actlo;
    180 
    181 	return gpin;
    182 }
    183 
    184 static void
    185 max77620_gpio_release(device_t dev, void *priv)
    186 {
    187 	struct max77620_softc * const sc = device_private(dev);
    188 	struct max77620_pin *gpin = priv;
    189 
    190 	I2C_LOCK(sc);
    191 	max77620_gpio_config(sc, gpin->pin_num, GPIO_PIN_INPUT|GPIO_PIN_OPENDRAIN);
    192 	I2C_UNLOCK(sc);
    193 
    194 	kmem_free(gpin, sizeof(*gpin));
    195 }
    196 
    197 static int
    198 max77620_gpio_read(device_t dev, void *priv, bool raw)
    199 {
    200 	struct max77620_softc * const sc = device_private(dev);
    201 	struct max77620_pin *gpin = priv;
    202 	uint8_t gpio;
    203 	int val;
    204 
    205 	/*
    206 	 * Performing a register read only; no need to acquire
    207 	 * the max77620 lock.
    208 	 */
    209 
    210 	I2C_LOCK(sc);
    211 	gpio = I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num));
    212 	I2C_UNLOCK(sc);
    213 
    214 	val = __SHIFTOUT(gpio, MAX_GPIO_INPUT_VAL);
    215 	if (!raw && gpin->pin_actlo)
    216 		val = !val;
    217 
    218 #ifdef MAX77620_DEBUG
    219 	device_printf(sc->sc_dev, "GPIO%d rd %02x (%d %d)\n",
    220 	    gpin->pin_num, gpio,
    221 	    (int)__SHIFTOUT(gpio, MAX_GPIO_INPUT_VAL), val);
    222 #endif
    223 
    224 	return val;
    225 }
    226 
    227 static void
    228 max77620_gpio_write(device_t dev, void *priv, int val, bool raw)
    229 {
    230 	struct max77620_softc * const sc = device_private(dev);
    231 	struct max77620_pin *gpin = priv;
    232 	uint8_t gpio;
    233 
    234 	if (!raw && gpin->pin_actlo)
    235 		val = !val;
    236 
    237 	I2C_LOCK(sc);
    238 	gpio = I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num));
    239 	gpio &= ~MAX_GPIO_OUTPUT_VAL;
    240 	gpio |= __SHIFTIN(val, MAX_GPIO_OUTPUT_VAL);
    241 #ifdef MAX77620_DEBUG
    242 	device_printf(sc->sc_dev, "GPIO%d wr %02x -> %02x\n",
    243 	    gpin->pin_num,
    244 	    I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num)), gpio);
    245 #endif
    246 	I2C_WRITE(sc, MAX_GPIO_REG(gpin->pin_num), gpio);
    247 	I2C_UNLOCK(sc);
    248 }
    249 
    250 static struct fdtbus_gpio_controller_func max77620_gpio_funcs = {
    251 	.acquire = max77620_gpio_acquire,
    252 	.release = max77620_gpio_release,
    253 	.read = max77620_gpio_read,
    254 	.write = max77620_gpio_write,
    255 };
    256 
    257 static void
    258 max77620_gpio_attach(struct max77620_softc *sc)
    259 {
    260 	fdtbus_register_gpio_controller(sc->sc_dev, sc->sc_phandle,
    261 	    &max77620_gpio_funcs);
    262 }
    263 
    264 static int
    265 max77620_match(device_t parent, cfdata_t match, void *aux)
    266 {
    267 	struct i2c_attach_args *ia = aux;
    268 	int match_result;
    269 
    270 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    271 		return match_result;
    272 
    273 	return 0;
    274 }
    275 
    276 static void
    277 max77620_attach(device_t parent, device_t self, void *aux)
    278 {
    279 	struct max77620_softc * const sc = device_private(self);
    280 	struct i2c_attach_args *ia = aux;
    281 
    282 	sc->sc_dev = self;
    283 	sc->sc_i2c = ia->ia_tag;
    284 	sc->sc_addr = ia->ia_addr;
    285 	sc->sc_phandle = ia->ia_cookie;
    286 
    287 	aprint_naive("\n");
    288 	aprint_normal(": MAX77620 Power Management IC\n");
    289 
    290 	max77620_gpio_attach(sc);
    291 }
    292 
    293 CFATTACH_DECL_NEW(max77620pmic, sizeof(struct max77620_softc),
    294     max77620_match, max77620_attach, NULL, NULL);
    295