Home | History | Annotate | Line # | Download | only in i2c
pcagpio.c revision 1.3.6.2
      1  1.3.6.2  martin /* $NetBSD: pcagpio.c,v 1.3.6.2 2020/04/08 14:08:05 martin Exp $ */
      2  1.3.6.2  martin 
      3  1.3.6.2  martin /*-
      4  1.3.6.2  martin  * Copyright (c) 2020 Michael Lorenz
      5  1.3.6.2  martin  * All rights reserved.
      6  1.3.6.2  martin  *
      7  1.3.6.2  martin  * Redistribution and use in source and binary forms, with or without
      8  1.3.6.2  martin  * modification, are permitted provided that the following conditions
      9  1.3.6.2  martin  * are met:
     10  1.3.6.2  martin  * 1. Redistributions of source code must retain the above copyright
     11  1.3.6.2  martin  *    notice, this list of conditions and the following disclaimer.
     12  1.3.6.2  martin  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.3.6.2  martin  *    notice, this list of conditions and the following disclaimer in the
     14  1.3.6.2  martin  *    documentation and/or other materials provided with the distribution.
     15  1.3.6.2  martin  *
     16  1.3.6.2  martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.3.6.2  martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.3.6.2  martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.3.6.2  martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.3.6.2  martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.3.6.2  martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.3.6.2  martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.3.6.2  martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.3.6.2  martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.3.6.2  martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.3.6.2  martin  * POSSIBILITY OF SUCH DAMAGE.
     27  1.3.6.2  martin  */
     28  1.3.6.2  martin 
     29  1.3.6.2  martin /*
     30  1.3.6.2  martin  * a driver for Philips Semiconductor PCA9555 GPIO controllers
     31  1.3.6.2  martin  */
     32  1.3.6.2  martin 
     33  1.3.6.2  martin #include <sys/cdefs.h>
     34  1.3.6.2  martin __KERNEL_RCSID(0, "$NetBSD: pcagpio.c,v 1.3.6.2 2020/04/08 14:08:05 martin Exp $");
     35  1.3.6.2  martin 
     36  1.3.6.2  martin #include <sys/param.h>
     37  1.3.6.2  martin #include <sys/systm.h>
     38  1.3.6.2  martin #include <sys/device.h>
     39  1.3.6.2  martin #include <sys/conf.h>
     40  1.3.6.2  martin #include <sys/bus.h>
     41  1.3.6.2  martin 
     42  1.3.6.2  martin #include <dev/i2c/i2cvar.h>
     43  1.3.6.2  martin #include <dev/led.h>
     44  1.3.6.2  martin 
     45  1.3.6.2  martin #ifdef PCAGPIO_DEBUG
     46  1.3.6.2  martin #define DPRINTF printf
     47  1.3.6.2  martin #else
     48  1.3.6.2  martin #define DPRINTF if (0) printf
     49  1.3.6.2  martin #endif
     50  1.3.6.2  martin 
     51  1.3.6.2  martin /* commands */
     52  1.3.6.2  martin #define PCAGPIO_INPUT	0x00	/* line status */
     53  1.3.6.2  martin #define PCAGPIO_OUTPUT	0x01	/* output status */
     54  1.3.6.2  martin #define PCAGPIO_REVERT	0x02	/* revert input if set */
     55  1.3.6.2  martin #define PCAGPIO_CONFIG	0x03	/* input if set, output if not */
     56  1.3.6.2  martin 
     57  1.3.6.2  martin static int	pcagpio_match(device_t, cfdata_t, void *);
     58  1.3.6.2  martin static void	pcagpio_attach(device_t, device_t, void *);
     59  1.3.6.2  martin 
     60  1.3.6.2  martin /* we can only pass one cookie to led_attach() but we need several values... */
     61  1.3.6.2  martin struct pcagpio_led {
     62  1.3.6.2  martin 	void *cookie;
     63  1.3.6.2  martin 	uint32_t mask, v_on, v_off;
     64  1.3.6.2  martin };
     65  1.3.6.2  martin 
     66  1.3.6.2  martin struct pcagpio_softc {
     67  1.3.6.2  martin 	device_t	sc_dev;
     68  1.3.6.2  martin 	i2c_tag_t	sc_i2c;
     69  1.3.6.2  martin 	i2c_addr_t	sc_addr;
     70  1.3.6.2  martin 
     71  1.3.6.2  martin 	int		sc_is_16bit;
     72  1.3.6.2  martin 	uint32_t	sc_state;
     73  1.3.6.2  martin 	struct pcagpio_led sc_leds[16];
     74  1.3.6.2  martin 	int		sc_nleds;
     75  1.3.6.2  martin };
     76  1.3.6.2  martin 
     77  1.3.6.2  martin 
     78  1.3.6.2  martin static void 	pcagpio_writereg(struct pcagpio_softc *, int, uint32_t);
     79  1.3.6.2  martin static uint32_t pcagpio_readreg(struct pcagpio_softc *, int);
     80  1.3.6.2  martin static void	pcagpio_attach_led(
     81  1.3.6.2  martin 			struct pcagpio_softc *, char *, int, int, int);
     82  1.3.6.2  martin static int	pcagpio_get(void *);
     83  1.3.6.2  martin static void	pcagpio_set(void *, int);
     84  1.3.6.2  martin 
     85  1.3.6.2  martin CFATTACH_DECL_NEW(pcagpio, sizeof(struct pcagpio_softc),
     86  1.3.6.2  martin     pcagpio_match, pcagpio_attach, NULL, NULL);
     87  1.3.6.2  martin 
     88  1.3.6.2  martin static const struct device_compatible_entry compat_data[] = {
     89  1.3.6.2  martin 	{ "i2c-pca9555",	1 },
     90  1.3.6.2  martin 	{ "pca9555",		1 },
     91  1.3.6.2  martin 	{ "i2c-pca9556",	0 },
     92  1.3.6.2  martin 	{ "pca9556",		0 },
     93  1.3.6.2  martin 	{ NULL,			0 }
     94  1.3.6.2  martin };
     95  1.3.6.2  martin 
     96  1.3.6.2  martin static int
     97  1.3.6.2  martin pcagpio_match(device_t parent, cfdata_t match, void *aux)
     98  1.3.6.2  martin {
     99  1.3.6.2  martin 	struct i2c_attach_args *ia = aux;
    100  1.3.6.2  martin 	int match_result;
    101  1.3.6.2  martin 
    102  1.3.6.2  martin 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    103  1.3.6.2  martin 		return match_result;
    104  1.3.6.2  martin 
    105  1.3.6.2  martin 	return 0;
    106  1.3.6.2  martin }
    107  1.3.6.2  martin 
    108  1.3.6.2  martin #ifdef PCAGPIO_DEBUG
    109  1.3.6.2  martin static void
    110  1.3.6.2  martin printdir(uint32_t val, uint32_t mask, char letter)
    111  1.3.6.2  martin {
    112  1.3.6.2  martin 	char flags[17], bits[17];
    113  1.3.6.2  martin 	uint32_t bit = 0x8000;
    114  1.3.6.2  martin 	int i;
    115  1.3.6.2  martin 
    116  1.3.6.2  martin 	val &= mask;
    117  1.3.6.2  martin 	for (i = 0; i < 16; i++) {
    118  1.3.6.2  martin 		flags[i] = (mask & bit) ? letter : '-';
    119  1.3.6.2  martin 		bits[i] = (val & bit) ? 'X' : ' ';
    120  1.3.6.2  martin 		bit = bit >> 1;
    121  1.3.6.2  martin 	}
    122  1.3.6.2  martin 	flags[16] = 0;
    123  1.3.6.2  martin 	bits[16] = 0;
    124  1.3.6.2  martin 	printf("dir: %s\n", flags);
    125  1.3.6.2  martin 	printf("lvl: %s\n", bits);
    126  1.3.6.2  martin }
    127  1.3.6.2  martin #endif
    128  1.3.6.2  martin 
    129  1.3.6.2  martin static void
    130  1.3.6.2  martin pcagpio_attach(device_t parent, device_t self, void *aux)
    131  1.3.6.2  martin {
    132  1.3.6.2  martin 	struct pcagpio_softc *sc = device_private(self);
    133  1.3.6.2  martin 	struct i2c_attach_args *ia = aux;
    134  1.3.6.2  martin 	const struct device_compatible_entry *dce;
    135  1.3.6.2  martin 	prop_dictionary_t dict = device_properties(self);
    136  1.3.6.2  martin 	prop_array_t pins;
    137  1.3.6.2  martin 	prop_dictionary_t pin;
    138  1.3.6.2  martin 
    139  1.3.6.2  martin 	sc->sc_dev = self;
    140  1.3.6.2  martin 	sc->sc_i2c = ia->ia_tag;
    141  1.3.6.2  martin 	sc->sc_addr = ia->ia_addr;
    142  1.3.6.2  martin 	sc->sc_nleds = 0;
    143  1.3.6.2  martin 
    144  1.3.6.2  martin 	aprint_naive("\n");
    145  1.3.6.2  martin 	sc->sc_is_16bit = 0;
    146  1.3.6.2  martin 	if (iic_compatible_match(ia, compat_data, &dce))
    147  1.3.6.2  martin 		sc->sc_is_16bit = dce->data;
    148  1.3.6.2  martin 
    149  1.3.6.2  martin 	aprint_normal(": %s\n", sc->sc_is_16bit ? "PCA9555" : "PCA9556");
    150  1.3.6.2  martin 
    151  1.3.6.2  martin 	sc->sc_state = pcagpio_readreg(sc, PCAGPIO_OUTPUT);
    152  1.3.6.2  martin 
    153  1.3.6.2  martin #ifdef PCAGPIO_DEBUG
    154  1.3.6.2  martin 	uint32_t dir, in, out;
    155  1.3.6.2  martin 	dir = pcagpio_readreg(sc, PCAGPIO_CONFIG);
    156  1.3.6.2  martin 	in = pcagpio_readreg(sc, PCAGPIO_INPUT);
    157  1.3.6.2  martin 	out = sc->sc_state;
    158  1.3.6.2  martin 
    159  1.3.6.2  martin 	out &= ~dir;
    160  1.3.6.2  martin 	in &= dir;
    161  1.3.6.2  martin 
    162  1.3.6.2  martin 	printdir(in, dir, 'I');
    163  1.3.6.2  martin 	printdir(out, ~dir, 'O');
    164  1.3.6.2  martin #endif
    165  1.3.6.2  martin 
    166  1.3.6.2  martin 	pins = prop_dictionary_get(dict, "pins");
    167  1.3.6.2  martin 	if (pins != NULL) {
    168  1.3.6.2  martin 		int i, num, def;
    169  1.3.6.2  martin 		char name[32];
    170  1.3.6.2  martin 		const char *nptr;
    171  1.3.6.2  martin 		bool ok = TRUE, act;
    172  1.3.6.2  martin 
    173  1.3.6.2  martin 		for (i = 0; i < prop_array_count(pins); i++) {
    174  1.3.6.2  martin 			nptr = NULL;
    175  1.3.6.2  martin 			pin = prop_array_get(pins, i);
    176  1.3.6.2  martin 			ok &= prop_dictionary_get_cstring_nocopy(pin, "name",
    177  1.3.6.2  martin 			    &nptr);
    178  1.3.6.2  martin 			strncpy(name, nptr, 31);
    179  1.3.6.2  martin 			ok &= prop_dictionary_get_uint32(pin, "pin", &num);
    180  1.3.6.2  martin 			ok &= prop_dictionary_get_bool(
    181  1.3.6.2  martin 			    pin, "active_high", &act);
    182  1.3.6.2  martin 			/* optional default state */
    183  1.3.6.2  martin 			def = -1;
    184  1.3.6.2  martin 			prop_dictionary_get_int32(pin, "default_state", &def);
    185  1.3.6.2  martin 			if (ok) {
    186  1.3.6.2  martin 				pcagpio_attach_led(sc, name, num, act, def);
    187  1.3.6.2  martin 			}
    188  1.3.6.2  martin 		}
    189  1.3.6.2  martin 	}
    190  1.3.6.2  martin }
    191  1.3.6.2  martin 
    192  1.3.6.2  martin static void
    193  1.3.6.2  martin pcagpio_writereg(struct pcagpio_softc *sc, int reg, uint32_t val)
    194  1.3.6.2  martin {
    195  1.3.6.2  martin 	uint8_t cmd;
    196  1.3.6.2  martin 
    197  1.3.6.2  martin 	iic_acquire_bus(sc->sc_i2c, 0);
    198  1.3.6.2  martin 	if (sc->sc_is_16bit) {
    199  1.3.6.2  martin 		uint16_t creg;
    200  1.3.6.2  martin 		cmd = reg << 1;
    201  1.3.6.2  martin 		creg = htole16(val);
    202  1.3.6.2  martin 		iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    203  1.3.6.2  martin 		    sc->sc_addr, &cmd, 1, &creg, 2, 0);
    204  1.3.6.2  martin 	} else {
    205  1.3.6.2  martin 		uint8_t creg;
    206  1.3.6.2  martin 		cmd = reg;
    207  1.3.6.2  martin 		creg = (uint8_t)val;
    208  1.3.6.2  martin 		iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    209  1.3.6.2  martin 		    sc->sc_addr, &cmd, 1, &creg, 1, 0);
    210  1.3.6.2  martin 	}
    211  1.3.6.2  martin 	if (reg == PCAGPIO_OUTPUT) sc->sc_state = val;
    212  1.3.6.2  martin 	iic_release_bus(sc->sc_i2c, 0);
    213  1.3.6.2  martin }
    214  1.3.6.2  martin 
    215  1.3.6.2  martin static uint32_t pcagpio_readreg(struct pcagpio_softc *sc, int reg)
    216  1.3.6.2  martin {
    217  1.3.6.2  martin 	uint8_t cmd;
    218  1.3.6.2  martin 	uint32_t ret;
    219  1.3.6.2  martin 
    220  1.3.6.2  martin 	iic_acquire_bus(sc->sc_i2c, 0);
    221  1.3.6.2  martin 	if (sc->sc_is_16bit) {
    222  1.3.6.2  martin 		uint16_t creg;
    223  1.3.6.2  martin 		cmd = reg << 1;
    224  1.3.6.2  martin 		iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    225  1.3.6.2  martin 		    sc->sc_addr, &cmd, 1, &creg, 2, 0);
    226  1.3.6.2  martin 		ret = le16toh(creg);
    227  1.3.6.2  martin 	} else {
    228  1.3.6.2  martin 		uint8_t creg;
    229  1.3.6.2  martin 		cmd = reg;
    230  1.3.6.2  martin 		iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    231  1.3.6.2  martin 		    sc->sc_addr, &cmd, 1, &creg, 1, 0);
    232  1.3.6.2  martin 		ret = creg;
    233  1.3.6.2  martin 	}
    234  1.3.6.2  martin 	iic_release_bus(sc->sc_i2c, 0);
    235  1.3.6.2  martin 	return ret;
    236  1.3.6.2  martin }
    237  1.3.6.2  martin 
    238  1.3.6.2  martin static void
    239  1.3.6.2  martin pcagpio_attach_led(struct pcagpio_softc *sc, char *n, int pin, int act, int def)
    240  1.3.6.2  martin {
    241  1.3.6.2  martin 	struct pcagpio_led *l;
    242  1.3.6.2  martin 
    243  1.3.6.2  martin 	l = &sc->sc_leds[sc->sc_nleds];
    244  1.3.6.2  martin 	l->cookie = sc;
    245  1.3.6.2  martin 	l->mask = 1 << pin;
    246  1.3.6.2  martin 	l->v_on = act ? l->mask : 0;
    247  1.3.6.2  martin 	l->v_off = act ? 0 : l->mask;
    248  1.3.6.2  martin 	led_attach(n, l, pcagpio_get, pcagpio_set);
    249  1.3.6.2  martin 	if (def != -1) pcagpio_set(l, def);
    250  1.3.6.2  martin 	DPRINTF("%s: %04x %04x %04x def %d\n",
    251  1.3.6.2  martin 	    __func__, l->mask, l->v_on, l->v_off, def);
    252  1.3.6.2  martin 	sc->sc_nleds++;
    253  1.3.6.2  martin }
    254  1.3.6.2  martin 
    255  1.3.6.2  martin static int
    256  1.3.6.2  martin pcagpio_get(void *cookie)
    257  1.3.6.2  martin {
    258  1.3.6.2  martin 	struct pcagpio_led *l = cookie;
    259  1.3.6.2  martin 	struct pcagpio_softc *sc = l->cookie;
    260  1.3.6.2  martin 
    261  1.3.6.2  martin 	return ((sc->sc_state & l->mask) == l->v_on);
    262  1.3.6.2  martin }
    263  1.3.6.2  martin 
    264  1.3.6.2  martin static void
    265  1.3.6.2  martin pcagpio_set(void *cookie, int val)
    266  1.3.6.2  martin {
    267  1.3.6.2  martin 	struct pcagpio_led *l = cookie;
    268  1.3.6.2  martin 	struct pcagpio_softc *sc = l->cookie;
    269  1.3.6.2  martin 	uint32_t newstate;
    270  1.3.6.2  martin 
    271  1.3.6.2  martin 	newstate = sc->sc_state & ~l->mask;
    272  1.3.6.2  martin 	newstate |= val ? l->v_on : l->v_off;
    273  1.3.6.2  martin 	DPRINTF("%s: %04x -> %04x, %04x %04x %04x\n", __func__,
    274  1.3.6.2  martin 	    sc->sc_state, newstate, l->mask, l->v_on, l->v_off);
    275  1.3.6.2  martin 	if (newstate != sc->sc_state)
    276  1.3.6.2  martin 		pcagpio_writereg(sc, PCAGPIO_OUTPUT, newstate);
    277  1.3.6.2  martin }
    278