Home | History | Annotate | Line # | Download | only in i2c
      1  1.13  macallan /* $NetBSD: pcagpio.c,v 1.13 2025/07/09 08:32:48 macallan 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.13  macallan __KERNEL_RCSID(0, "$NetBSD: pcagpio.c,v 1.13 2025/07/09 08:32:48 macallan 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.1  macallan #include <sys/conf.h>
     40   1.1  macallan #include <sys/bus.h>
     41   1.1  macallan 
     42   1.1  macallan #include <dev/i2c/i2cvar.h>
     43   1.2  macallan #include <dev/led.h>
     44   1.2  macallan 
     45   1.2  macallan #ifdef PCAGPIO_DEBUG
     46   1.2  macallan #define DPRINTF printf
     47   1.2  macallan #else
     48   1.2  macallan #define DPRINTF if (0) printf
     49   1.2  macallan #endif
     50   1.1  macallan 
     51   1.1  macallan /* commands */
     52   1.1  macallan #define PCAGPIO_INPUT	0x00	/* line status */
     53   1.1  macallan #define PCAGPIO_OUTPUT	0x01	/* output status */
     54   1.1  macallan #define PCAGPIO_REVERT	0x02	/* revert input if set */
     55   1.1  macallan #define PCAGPIO_CONFIG	0x03	/* input if set, output if not */
     56   1.1  macallan 
     57   1.1  macallan static int	pcagpio_match(device_t, cfdata_t, void *);
     58   1.1  macallan static void	pcagpio_attach(device_t, device_t, void *);
     59   1.4       jdc static int	pcagpio_detach(device_t, int);
     60   1.1  macallan 
     61   1.2  macallan /* we can only pass one cookie to led_attach() but we need several values... */
     62   1.2  macallan struct pcagpio_led {
     63   1.2  macallan 	void *cookie;
     64   1.6       jdc 	struct led_device *led;
     65   1.2  macallan 	uint32_t mask, v_on, v_off;
     66   1.2  macallan };
     67   1.2  macallan 
     68   1.1  macallan struct pcagpio_softc {
     69   1.1  macallan 	device_t	sc_dev;
     70   1.1  macallan 	i2c_tag_t	sc_i2c;
     71   1.1  macallan 	i2c_addr_t	sc_addr;
     72   1.1  macallan 
     73   1.1  macallan 	int		sc_is_16bit;
     74   1.2  macallan 	uint32_t	sc_state;
     75   1.2  macallan 	struct pcagpio_led sc_leds[16];
     76   1.2  macallan 	int		sc_nleds;
     77   1.4       jdc 
     78   1.4       jdc #ifdef PCAGPIO_DEBUG
     79   1.4       jdc 	uint32_t	sc_dir, sc_in;
     80   1.4       jdc #endif
     81   1.1  macallan };
     82   1.1  macallan 
     83   1.1  macallan 
     84   1.1  macallan static void 	pcagpio_writereg(struct pcagpio_softc *, int, uint32_t);
     85   1.1  macallan static uint32_t pcagpio_readreg(struct pcagpio_softc *, int);
     86   1.3  macallan static void	pcagpio_attach_led(
     87   1.3  macallan 			struct pcagpio_softc *, char *, int, int, int);
     88   1.2  macallan static int	pcagpio_get(void *);
     89   1.2  macallan static void	pcagpio_set(void *, int);
     90   1.1  macallan 
     91   1.1  macallan CFATTACH_DECL_NEW(pcagpio, sizeof(struct pcagpio_softc),
     92   1.4       jdc     pcagpio_match, pcagpio_attach, pcagpio_detach, NULL);
     93   1.1  macallan 
     94   1.1  macallan static const struct device_compatible_entry compat_data[] = {
     95   1.7   thorpej 	{ .compat = "i2c-pca9555",	.value = 1 },
     96   1.7   thorpej 	{ .compat = "pca9555",		.value = 1 },
     97   1.7   thorpej 	{ .compat = "i2c-pca9556",	.value = 0 },
     98   1.7   thorpej 	{ .compat = "pca9556",		.value = 0 },
     99  1.10   thorpej 	DEVICE_COMPAT_EOL
    100   1.1  macallan };
    101   1.1  macallan 
    102   1.1  macallan static int
    103   1.1  macallan pcagpio_match(device_t parent, cfdata_t match, void *aux)
    104   1.1  macallan {
    105   1.1  macallan 	struct i2c_attach_args *ia = aux;
    106   1.1  macallan 	int match_result;
    107   1.1  macallan 
    108   1.1  macallan 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    109   1.1  macallan 		return match_result;
    110   1.1  macallan 
    111   1.1  macallan 	return 0;
    112   1.1  macallan }
    113   1.1  macallan 
    114   1.2  macallan #ifdef PCAGPIO_DEBUG
    115   1.1  macallan static void
    116  1.13  macallan printdir(const char* name, uint32_t val, uint32_t mask, char letter)
    117   1.1  macallan {
    118   1.1  macallan 	char flags[17], bits[17];
    119   1.1  macallan 	uint32_t bit = 0x8000;
    120   1.1  macallan 	int i;
    121   1.1  macallan 
    122   1.1  macallan 	val &= mask;
    123   1.1  macallan 	for (i = 0; i < 16; i++) {
    124   1.1  macallan 		flags[i] = (mask & bit) ? letter : '-';
    125   1.1  macallan 		bits[i] = (val & bit) ? 'X' : ' ';
    126   1.1  macallan 		bit = bit >> 1;
    127   1.1  macallan 	}
    128   1.1  macallan 	flags[16] = 0;
    129   1.1  macallan 	bits[16] = 0;
    130   1.4       jdc 	printf("%s: dir: %s\n", name, flags);
    131   1.4       jdc 	printf("%s: lvl: %s\n", name, bits);
    132   1.1  macallan }
    133   1.2  macallan #endif
    134   1.1  macallan 
    135   1.1  macallan static void
    136   1.1  macallan pcagpio_attach(device_t parent, device_t self, void *aux)
    137   1.1  macallan {
    138   1.1  macallan 	struct pcagpio_softc *sc = device_private(self);
    139   1.1  macallan 	struct i2c_attach_args *ia = aux;
    140   1.1  macallan 	const struct device_compatible_entry *dce;
    141   1.2  macallan 	prop_dictionary_t dict = device_properties(self);
    142   1.2  macallan 	prop_array_t pins;
    143   1.2  macallan 	prop_dictionary_t pin;
    144   1.1  macallan 
    145   1.1  macallan 	sc->sc_dev = self;
    146   1.1  macallan 	sc->sc_i2c = ia->ia_tag;
    147   1.1  macallan 	sc->sc_addr = ia->ia_addr;
    148   1.2  macallan 	sc->sc_nleds = 0;
    149   1.1  macallan 
    150   1.1  macallan 	aprint_naive("\n");
    151   1.1  macallan 	sc->sc_is_16bit = 0;
    152   1.8   thorpej 	if ((dce = iic_compatible_lookup(ia, compat_data)) != NULL)
    153   1.7   thorpej 		sc->sc_is_16bit = dce->value;
    154   1.1  macallan 
    155   1.1  macallan 	aprint_normal(": %s\n", sc->sc_is_16bit ? "PCA9555" : "PCA9556");
    156   1.1  macallan 
    157   1.2  macallan 	sc->sc_state = pcagpio_readreg(sc, PCAGPIO_OUTPUT);
    158   1.2  macallan 
    159   1.2  macallan #ifdef PCAGPIO_DEBUG
    160   1.4       jdc 	uint32_t in, out;
    161   1.4       jdc 	sc->sc_dir = pcagpio_readreg(sc, PCAGPIO_CONFIG);
    162   1.4       jdc 	sc->sc_in = pcagpio_readreg(sc, PCAGPIO_INPUT);
    163   1.4       jdc 	in = sc-> sc_in;
    164   1.2  macallan 	out = sc->sc_state;
    165   1.1  macallan 
    166   1.4       jdc 	out &= ~sc->sc_dir;
    167   1.4       jdc 	in &= sc->sc_dir;
    168  1.12  riastrad 
    169  1.12  riastrad 	printdir(device_xname(sc->sc_dev), in, sc->sc_dir, 'I');
    170  1.12  riastrad 	printdir(device_xname(sc->sc_dev), out, ~sc->sc_dir, 'O');
    171   1.4       jdc 
    172   1.2  macallan #endif
    173   1.2  macallan 
    174   1.2  macallan 	pins = prop_dictionary_get(dict, "pins");
    175   1.2  macallan 	if (pins != NULL) {
    176   1.2  macallan 		int i, num, def;
    177   1.2  macallan 		char name[32];
    178   1.5       jdc 		const char *spptr, *nptr;
    179   1.2  macallan 		bool ok = TRUE, act;
    180   1.2  macallan 
    181   1.2  macallan 		for (i = 0; i < prop_array_count(pins); i++) {
    182   1.2  macallan 			nptr = NULL;
    183   1.2  macallan 			pin = prop_array_get(pins, i);
    184  1.11  christos 			ok &= prop_dictionary_get_string(pin, "name",
    185   1.3  macallan 			    &nptr);
    186   1.2  macallan 			ok &= prop_dictionary_get_uint32(pin, "pin", &num);
    187   1.5       jdc 			ok &= prop_dictionary_get_bool( pin, "active_high",
    188   1.5       jdc 			    &act);
    189   1.2  macallan 			/* optional default state */
    190   1.2  macallan 			def = -1;
    191   1.2  macallan 			prop_dictionary_get_int32(pin, "default_state", &def);
    192   1.5       jdc 			if (!ok)
    193   1.5       jdc 				continue;
    194   1.5       jdc 			/* Extract pin type from the name */
    195   1.5       jdc 			spptr = strstr(nptr, " ");
    196   1.5       jdc 			if (spptr == NULL)
    197   1.5       jdc 				continue;
    198   1.5       jdc 			spptr += 1;
    199   1.5       jdc 			strncpy(name, spptr, 31);
    200   1.5       jdc 			if (!strncmp(nptr, "LED ", 4))
    201   1.2  macallan 				pcagpio_attach_led(sc, name, num, act, def);
    202   1.2  macallan 		}
    203   1.2  macallan 	}
    204   1.1  macallan }
    205   1.1  macallan 
    206   1.4       jdc static int
    207   1.4       jdc pcagpio_detach(device_t self, int flags)
    208   1.4       jdc {
    209   1.4       jdc 	struct pcagpio_softc *sc = device_private(self);
    210   1.6       jdc 	int i;
    211   1.4       jdc 
    212   1.6       jdc 	for (i = 0; i < sc->sc_nleds; i++)
    213   1.6       jdc 		led_detach(sc->sc_leds[i].led);
    214   1.6       jdc 
    215   1.4       jdc 	return 0;
    216   1.4       jdc }
    217   1.4       jdc 
    218   1.1  macallan static void
    219   1.1  macallan pcagpio_writereg(struct pcagpio_softc *sc, int reg, uint32_t val)
    220   1.1  macallan {
    221   1.1  macallan 	uint8_t cmd;
    222   1.1  macallan 
    223   1.1  macallan 	iic_acquire_bus(sc->sc_i2c, 0);
    224   1.1  macallan 	if (sc->sc_is_16bit) {
    225   1.1  macallan 		uint16_t creg;
    226   1.1  macallan 		cmd = reg << 1;
    227   1.1  macallan 		creg = htole16(val);
    228   1.1  macallan 		iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    229   1.1  macallan 		    sc->sc_addr, &cmd, 1, &creg, 2, 0);
    230   1.1  macallan 	} else {
    231   1.1  macallan 		uint8_t creg;
    232   1.1  macallan 		cmd = reg;
    233   1.1  macallan 		creg = (uint8_t)val;
    234   1.1  macallan 		iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
    235   1.1  macallan 		    sc->sc_addr, &cmd, 1, &creg, 1, 0);
    236   1.1  macallan 	}
    237   1.2  macallan 	if (reg == PCAGPIO_OUTPUT) sc->sc_state = val;
    238   1.1  macallan 	iic_release_bus(sc->sc_i2c, 0);
    239   1.1  macallan }
    240   1.1  macallan 
    241   1.1  macallan static uint32_t pcagpio_readreg(struct pcagpio_softc *sc, int reg)
    242   1.1  macallan {
    243   1.1  macallan 	uint8_t cmd;
    244   1.1  macallan 	uint32_t ret;
    245   1.1  macallan 
    246   1.1  macallan 	iic_acquire_bus(sc->sc_i2c, 0);
    247   1.1  macallan 	if (sc->sc_is_16bit) {
    248   1.1  macallan 		uint16_t creg;
    249   1.1  macallan 		cmd = reg << 1;
    250   1.1  macallan 		iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    251   1.1  macallan 		    sc->sc_addr, &cmd, 1, &creg, 2, 0);
    252   1.1  macallan 		ret = le16toh(creg);
    253   1.1  macallan 	} else {
    254   1.1  macallan 		uint8_t creg;
    255   1.1  macallan 		cmd = reg;
    256   1.1  macallan 		iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    257   1.1  macallan 		    sc->sc_addr, &cmd, 1, &creg, 1, 0);
    258   1.1  macallan 		ret = creg;
    259   1.1  macallan 	}
    260   1.1  macallan 	iic_release_bus(sc->sc_i2c, 0);
    261   1.1  macallan 	return ret;
    262   1.1  macallan }
    263   1.2  macallan 
    264   1.2  macallan static void
    265   1.2  macallan pcagpio_attach_led(struct pcagpio_softc *sc, char *n, int pin, int act, int def)
    266   1.2  macallan {
    267   1.2  macallan 	struct pcagpio_led *l;
    268   1.2  macallan 
    269   1.2  macallan 	l = &sc->sc_leds[sc->sc_nleds];
    270   1.2  macallan 	l->cookie = sc;
    271   1.2  macallan 	l->mask = 1 << pin;
    272   1.2  macallan 	l->v_on = act ? l->mask : 0;
    273   1.2  macallan 	l->v_off = act ? 0 : l->mask;
    274   1.6       jdc 	l->led = led_attach(n, l, pcagpio_get, pcagpio_set);
    275   1.2  macallan 	if (def != -1) pcagpio_set(l, def);
    276   1.3  macallan 	DPRINTF("%s: %04x %04x %04x def %d\n",
    277   1.3  macallan 	    __func__, l->mask, l->v_on, l->v_off, def);
    278   1.2  macallan 	sc->sc_nleds++;
    279   1.2  macallan }
    280   1.2  macallan 
    281   1.2  macallan static int
    282   1.2  macallan pcagpio_get(void *cookie)
    283   1.2  macallan {
    284   1.2  macallan 	struct pcagpio_led *l = cookie;
    285   1.2  macallan 	struct pcagpio_softc *sc = l->cookie;
    286   1.2  macallan 
    287   1.2  macallan 	return ((sc->sc_state & l->mask) == l->v_on);
    288   1.2  macallan }
    289   1.2  macallan 
    290   1.2  macallan static void
    291   1.2  macallan pcagpio_set(void *cookie, int val)
    292   1.2  macallan {
    293   1.2  macallan 	struct pcagpio_led *l = cookie;
    294   1.2  macallan 	struct pcagpio_softc *sc = l->cookie;
    295   1.2  macallan 	uint32_t newstate;
    296   1.2  macallan 
    297   1.2  macallan 	newstate = sc->sc_state & ~l->mask;
    298   1.2  macallan 	newstate |= val ? l->v_on : l->v_off;
    299   1.2  macallan 	DPRINTF("%s: %04x -> %04x, %04x %04x %04x\n", __func__,
    300   1.2  macallan 	    sc->sc_state, newstate, l->mask, l->v_on, l->v_off);
    301   1.2  macallan 	if (newstate != sc->sc_state)
    302   1.2  macallan 		pcagpio_writereg(sc, PCAGPIO_OUTPUT, newstate);
    303   1.2  macallan }
    304