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