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