Home | History | Annotate | Line # | Download | only in i2c
max77620.c revision 1.11.14.1
      1  1.11.14.1   thorpej /* $NetBSD: max77620.c,v 1.11.14.1 2021/08/09 00:30:09 thorpej Exp $ */
      2        1.1  jmcneill 
      3        1.1  jmcneill /*-
      4        1.1  jmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5        1.1  jmcneill  * All rights reserved.
      6        1.1  jmcneill  *
      7        1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8        1.1  jmcneill  * modification, are permitted provided that the following conditions
      9        1.1  jmcneill  * are met:
     10        1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11        1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12        1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14        1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15        1.1  jmcneill  *
     16        1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17        1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18        1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19        1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20        1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21        1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22        1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23        1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24        1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25        1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26        1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27        1.1  jmcneill  */
     28        1.1  jmcneill 
     29        1.1  jmcneill #include <sys/cdefs.h>
     30  1.11.14.1   thorpej __KERNEL_RCSID(0, "$NetBSD: max77620.c,v 1.11.14.1 2021/08/09 00:30:09 thorpej Exp $");
     31        1.1  jmcneill 
     32        1.1  jmcneill #include <sys/param.h>
     33        1.1  jmcneill #include <sys/systm.h>
     34        1.7   thorpej #include <sys/mutex.h>
     35        1.1  jmcneill #include <sys/kernel.h>
     36        1.1  jmcneill #include <sys/device.h>
     37        1.1  jmcneill #include <sys/conf.h>
     38        1.1  jmcneill #include <sys/bus.h>
     39        1.1  jmcneill #include <sys/kmem.h>
     40        1.1  jmcneill #include <sys/gpio.h>
     41        1.1  jmcneill 
     42        1.1  jmcneill #include <dev/i2c/i2cvar.h>
     43        1.1  jmcneill 
     44        1.1  jmcneill #include <dev/fdt/fdtvar.h>
     45        1.1  jmcneill 
     46        1.1  jmcneill #define	MAX_GPIO_REG(n)		(0x36 + (n))
     47        1.1  jmcneill #define	 MAX_GPIO_DEBOUNCE	__BITS(7,6)
     48        1.1  jmcneill #define	 MAX_GPIO_INT		__BITS(5,4)
     49        1.1  jmcneill #define	 MAX_GPIO_OUTPUT_VAL	__BIT(3)
     50        1.1  jmcneill #define	 MAX_GPIO_INPUT_VAL	__BIT(2)
     51        1.1  jmcneill #define	 MAX_GPIO_DIR		__BIT(1)
     52        1.1  jmcneill #define	 MAX_GPIO_DRV		__BIT(0)
     53        1.1  jmcneill 
     54        1.1  jmcneill #define	MAX_GPIO_COUNT		8
     55        1.1  jmcneill 
     56        1.1  jmcneill struct max77620_softc {
     57        1.1  jmcneill 	device_t	sc_dev;
     58        1.1  jmcneill 	i2c_tag_t	sc_i2c;
     59        1.1  jmcneill 	i2c_addr_t	sc_addr;
     60        1.1  jmcneill 	int		sc_phandle;
     61        1.1  jmcneill };
     62        1.1  jmcneill 
     63        1.1  jmcneill struct max77620_pin {
     64        1.1  jmcneill 	struct max77620_softc	*pin_sc;
     65        1.1  jmcneill 	int			pin_num;
     66        1.1  jmcneill 	int			pin_flags;
     67        1.1  jmcneill 	bool			pin_actlo;
     68        1.1  jmcneill };
     69        1.1  jmcneill 
     70        1.6   thorpej static const struct device_compatible_entry compat_data[] = {
     71        1.9   thorpej 	{ .compat = "maxim,max77620" },
     72       1.11   thorpej 	DEVICE_COMPAT_EOL
     73        1.5   thorpej };
     74        1.5   thorpej 
     75        1.1  jmcneill static uint8_t
     76        1.1  jmcneill max77620_read(struct max77620_softc *sc, uint8_t reg, int flags)
     77        1.1  jmcneill {
     78        1.1  jmcneill 	uint8_t val = 0;
     79        1.1  jmcneill 	int error;
     80        1.1  jmcneill 
     81        1.1  jmcneill 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
     82        1.1  jmcneill 	    &reg, 1, &val, 1, flags);
     83        1.1  jmcneill 	if (error != 0)
     84        1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
     85        1.1  jmcneill 
     86        1.1  jmcneill 	return val;
     87        1.1  jmcneill }
     88        1.1  jmcneill 
     89        1.1  jmcneill static void
     90        1.1  jmcneill max77620_write(struct max77620_softc *sc, uint8_t reg, uint8_t val, int flags)
     91        1.1  jmcneill {
     92        1.1  jmcneill 	int error;
     93        1.1  jmcneill 
     94        1.1  jmcneill 	error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
     95        1.1  jmcneill 	    &reg, 1, &val, 1, flags);
     96        1.1  jmcneill 	if (error != 0)
     97        1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
     98        1.1  jmcneill }
     99        1.1  jmcneill 
    100        1.7   thorpej #define	I2C_READ(sc, reg)	max77620_read((sc), (reg), 0)
    101        1.7   thorpej #define	I2C_WRITE(sc, reg, val)	max77620_write((sc), (reg), (val), 0)
    102        1.7   thorpej #define	I2C_LOCK(sc)		iic_acquire_bus((sc)->sc_i2c, 0)
    103        1.7   thorpej #define	I2C_UNLOCK(sc)		iic_release_bus((sc)->sc_i2c, 0)
    104        1.1  jmcneill 
    105        1.1  jmcneill static int
    106        1.1  jmcneill max77620_gpio_config(struct max77620_softc *sc, int pin, int flags)
    107        1.1  jmcneill {
    108        1.1  jmcneill 	uint32_t gpio;
    109        1.1  jmcneill 
    110        1.1  jmcneill 	KASSERT(pin >= 0 && pin < MAX_GPIO_COUNT);
    111        1.1  jmcneill 
    112        1.1  jmcneill 	gpio = I2C_READ(sc, MAX_GPIO_REG(pin));
    113        1.1  jmcneill 
    114        1.1  jmcneill 	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
    115        1.1  jmcneill 	case GPIO_PIN_INPUT:
    116        1.1  jmcneill 		gpio |= MAX_GPIO_DIR;
    117        1.1  jmcneill 		break;
    118        1.1  jmcneill 	case GPIO_PIN_OUTPUT:
    119        1.1  jmcneill 		gpio &= ~MAX_GPIO_DIR;
    120        1.1  jmcneill 		break;
    121        1.1  jmcneill 	default:
    122        1.1  jmcneill 		return EINVAL;
    123        1.1  jmcneill 	}
    124        1.1  jmcneill 
    125        1.1  jmcneill 	switch (flags & (GPIO_PIN_PUSHPULL|GPIO_PIN_OPENDRAIN)) {
    126        1.1  jmcneill 	case GPIO_PIN_PUSHPULL:
    127        1.1  jmcneill 		gpio |= MAX_GPIO_DRV;
    128        1.1  jmcneill 		break;
    129        1.1  jmcneill 	case GPIO_PIN_OPENDRAIN:
    130        1.1  jmcneill 		gpio &= ~MAX_GPIO_DRV;
    131        1.1  jmcneill 		break;
    132        1.1  jmcneill 	default:
    133        1.1  jmcneill 		return EINVAL;
    134        1.1  jmcneill 	}
    135        1.1  jmcneill 
    136        1.1  jmcneill 	I2C_WRITE(sc, MAX_GPIO_REG(pin), gpio);
    137        1.1  jmcneill 
    138        1.1  jmcneill 	return 0;
    139        1.1  jmcneill }
    140        1.1  jmcneill 
    141        1.1  jmcneill static void *
    142        1.1  jmcneill max77620_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
    143        1.1  jmcneill {
    144        1.1  jmcneill 	struct max77620_softc * const sc = device_private(dev);
    145        1.1  jmcneill 	struct max77620_pin *gpin;
    146        1.1  jmcneill 	const u_int *gpio = data;
    147        1.1  jmcneill 	int error;
    148        1.1  jmcneill 
    149        1.1  jmcneill 	if (len != 12)
    150        1.1  jmcneill 		return NULL;
    151        1.1  jmcneill 
    152        1.1  jmcneill 	const uint8_t pin = be32toh(gpio[1]) & 0xff;
    153        1.1  jmcneill 	const bool actlo = (be32toh(gpio[2]) & __BIT(0)) != 0;
    154        1.1  jmcneill 	const bool pushpull = (be32toh(gpio[2]) & __BIT(1)) == 0;
    155        1.1  jmcneill 	const bool opendrain = (be32toh(gpio[2]) & __BIT(2)) != 0;
    156        1.1  jmcneill 
    157        1.1  jmcneill 	if (pushpull)
    158        1.1  jmcneill 		flags |= GPIO_PIN_PUSHPULL;
    159        1.1  jmcneill 	if (opendrain)
    160        1.1  jmcneill 		flags |= GPIO_PIN_OPENDRAIN;
    161        1.1  jmcneill 
    162        1.1  jmcneill 	if (pin >= MAX_GPIO_COUNT)
    163        1.1  jmcneill 		return NULL;
    164        1.1  jmcneill 
    165        1.1  jmcneill 	I2C_LOCK(sc);
    166        1.1  jmcneill 	error = max77620_gpio_config(sc, pin, flags);
    167        1.1  jmcneill 	I2C_UNLOCK(sc);
    168        1.1  jmcneill 
    169        1.1  jmcneill 	if (error != 0) {
    170        1.1  jmcneill 		device_printf(dev, "bad pin %d config %#x\n", pin, flags);
    171        1.1  jmcneill 		return NULL;
    172        1.1  jmcneill 	}
    173        1.1  jmcneill 
    174        1.1  jmcneill 	gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
    175        1.1  jmcneill 	gpin->pin_sc = sc;
    176        1.1  jmcneill 	gpin->pin_num = pin;
    177        1.1  jmcneill 	gpin->pin_flags = flags;
    178        1.1  jmcneill 	gpin->pin_actlo = actlo;
    179        1.1  jmcneill 
    180        1.1  jmcneill 	return gpin;
    181        1.1  jmcneill }
    182        1.1  jmcneill 
    183        1.1  jmcneill static void
    184        1.1  jmcneill max77620_gpio_release(device_t dev, void *priv)
    185        1.1  jmcneill {
    186        1.1  jmcneill 	struct max77620_softc * const sc = device_private(dev);
    187        1.1  jmcneill 	struct max77620_pin *gpin = priv;
    188        1.1  jmcneill 
    189        1.1  jmcneill 	I2C_LOCK(sc);
    190        1.1  jmcneill 	max77620_gpio_config(sc, gpin->pin_num, GPIO_PIN_INPUT|GPIO_PIN_OPENDRAIN);
    191        1.1  jmcneill 	I2C_UNLOCK(sc);
    192        1.1  jmcneill 
    193        1.1  jmcneill 	kmem_free(gpin, sizeof(*gpin));
    194        1.1  jmcneill }
    195        1.1  jmcneill 
    196        1.1  jmcneill static int
    197        1.1  jmcneill max77620_gpio_read(device_t dev, void *priv, bool raw)
    198        1.1  jmcneill {
    199        1.1  jmcneill 	struct max77620_softc * const sc = device_private(dev);
    200        1.1  jmcneill 	struct max77620_pin *gpin = priv;
    201        1.1  jmcneill 	uint8_t gpio;
    202        1.1  jmcneill 	int val;
    203        1.1  jmcneill 
    204        1.7   thorpej 	/*
    205        1.7   thorpej 	 * Performing a register read only; no need to acquire
    206        1.7   thorpej 	 * the max77620 lock.
    207        1.7   thorpej 	 */
    208        1.7   thorpej 
    209        1.1  jmcneill 	I2C_LOCK(sc);
    210        1.1  jmcneill 	gpio = I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num));
    211        1.1  jmcneill 	I2C_UNLOCK(sc);
    212        1.1  jmcneill 
    213        1.1  jmcneill 	val = __SHIFTOUT(gpio, MAX_GPIO_INPUT_VAL);
    214        1.1  jmcneill 	if (!raw && gpin->pin_actlo)
    215        1.1  jmcneill 		val = !val;
    216        1.1  jmcneill 
    217        1.1  jmcneill #ifdef MAX77620_DEBUG
    218        1.1  jmcneill 	device_printf(sc->sc_dev, "GPIO%d rd %02x (%d %d)\n",
    219        1.1  jmcneill 	    gpin->pin_num, gpio,
    220        1.1  jmcneill 	    (int)__SHIFTOUT(gpio, MAX_GPIO_INPUT_VAL), val);
    221        1.1  jmcneill #endif
    222        1.1  jmcneill 
    223        1.1  jmcneill 	return val;
    224        1.1  jmcneill }
    225        1.1  jmcneill 
    226        1.1  jmcneill static void
    227        1.1  jmcneill max77620_gpio_write(device_t dev, void *priv, int val, bool raw)
    228        1.1  jmcneill {
    229        1.1  jmcneill 	struct max77620_softc * const sc = device_private(dev);
    230        1.1  jmcneill 	struct max77620_pin *gpin = priv;
    231        1.1  jmcneill 	uint8_t gpio;
    232        1.1  jmcneill 
    233        1.1  jmcneill 	if (!raw && gpin->pin_actlo)
    234        1.1  jmcneill 		val = !val;
    235        1.1  jmcneill 
    236        1.1  jmcneill 	I2C_LOCK(sc);
    237        1.1  jmcneill 	gpio = I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num));
    238        1.1  jmcneill 	gpio &= ~MAX_GPIO_OUTPUT_VAL;
    239        1.1  jmcneill 	gpio |= __SHIFTIN(val, MAX_GPIO_OUTPUT_VAL);
    240        1.1  jmcneill #ifdef MAX77620_DEBUG
    241        1.1  jmcneill 	device_printf(sc->sc_dev, "GPIO%d wr %02x -> %02x\n",
    242        1.1  jmcneill 	    gpin->pin_num,
    243        1.1  jmcneill 	    I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num)), gpio);
    244        1.1  jmcneill #endif
    245        1.1  jmcneill 	I2C_WRITE(sc, MAX_GPIO_REG(gpin->pin_num), gpio);
    246        1.1  jmcneill 	I2C_UNLOCK(sc);
    247        1.1  jmcneill }
    248        1.1  jmcneill 
    249        1.1  jmcneill static struct fdtbus_gpio_controller_func max77620_gpio_funcs = {
    250        1.1  jmcneill 	.acquire = max77620_gpio_acquire,
    251        1.1  jmcneill 	.release = max77620_gpio_release,
    252        1.1  jmcneill 	.read = max77620_gpio_read,
    253        1.1  jmcneill 	.write = max77620_gpio_write,
    254        1.1  jmcneill };
    255        1.1  jmcneill 
    256        1.1  jmcneill static void
    257        1.1  jmcneill max77620_gpio_attach(struct max77620_softc *sc)
    258        1.1  jmcneill {
    259        1.1  jmcneill 	fdtbus_register_gpio_controller(sc->sc_dev, sc->sc_phandle,
    260        1.1  jmcneill 	    &max77620_gpio_funcs);
    261        1.1  jmcneill }
    262        1.1  jmcneill 
    263        1.1  jmcneill static int
    264        1.1  jmcneill max77620_match(device_t parent, cfdata_t match, void *aux)
    265        1.1  jmcneill {
    266        1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    267        1.3   thorpej 	int match_result;
    268        1.1  jmcneill 
    269        1.6   thorpej 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    270        1.3   thorpej 		return match_result;
    271        1.1  jmcneill 
    272        1.3   thorpej 	return 0;
    273        1.1  jmcneill }
    274        1.1  jmcneill 
    275        1.1  jmcneill static void
    276        1.1  jmcneill max77620_attach(device_t parent, device_t self, void *aux)
    277        1.1  jmcneill {
    278        1.1  jmcneill 	struct max77620_softc * const sc = device_private(self);
    279        1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    280        1.1  jmcneill 
    281        1.1  jmcneill 	sc->sc_dev = self;
    282        1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
    283        1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
    284  1.11.14.1   thorpej 	sc->sc_phandle = devhandle_to_of(device_handle(self));
    285        1.1  jmcneill 
    286        1.1  jmcneill 	aprint_naive("\n");
    287        1.1  jmcneill 	aprint_normal(": MAX77620 Power Management IC\n");
    288        1.1  jmcneill 
    289        1.1  jmcneill 	max77620_gpio_attach(sc);
    290        1.1  jmcneill }
    291        1.1  jmcneill 
    292        1.1  jmcneill CFATTACH_DECL_NEW(max77620pmic, sizeof(struct max77620_softc),
    293        1.1  jmcneill     max77620_match, max77620_attach, NULL, NULL);
    294