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