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