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