Home | History | Annotate | Line # | Download | only in gpio
      1 /* $NetBSD: gpioiic.c,v 1.13 2025/09/15 13:23:03 thorpej Exp $ */
      2 /*	$OpenBSD: gpioiic.c,v 1.8 2008/11/24 12:12:12 mbalmer Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2006 Alexander Yurchenko <grange (at) openbsd.org>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 #include <sys/cdefs.h>
     21 __KERNEL_RCSID(0, "$NetBSD: gpioiic.c,v 1.13 2025/09/15 13:23:03 thorpej Exp $");
     22 
     23 /*
     24  * I2C bus bit-banging through GPIO pins.
     25  */
     26 
     27 #include <sys/param.h>
     28 #include <sys/systm.h>
     29 #include <sys/device.h>
     30 #include <sys/gpio.h>
     31 #include <sys/rwlock.h>
     32 #include <sys/module.h>
     33 
     34 #include <dev/gpio/gpiovar.h>
     35 
     36 #include <dev/i2c/i2cvar.h>
     37 #include <dev/i2c/i2c_bitbang.h>
     38 
     39 #include "ioconf.h"
     40 
     41 #define GPIOIIC_PIN_SDA		0
     42 #define GPIOIIC_PIN_SCL		1
     43 #define GPIOIIC_NPINS		2
     44 
     45 #define GPIOIIC_SDA		0x01
     46 #define GPIOIIC_SCL		0x02
     47 
     48 #define GPIOIIC_PIN_REVERSE	0x01
     49 
     50 struct gpioiic_softc {
     51 	void *			sc_gpio;
     52 	struct gpio_pinmap	sc_map;
     53 	int			_map[GPIOIIC_NPINS];
     54 
     55 	struct i2c_controller	sc_i2c_tag;
     56 	device_t		sc_i2c_dev;
     57 
     58 	int			sc_pin_sda;
     59 	int			sc_pin_scl;
     60 
     61 	int			sc_sda;
     62 	int			sc_scl;
     63 };
     64 
     65 int		gpioiic_match(device_t, cfdata_t, void *);
     66 void		gpioiic_attach(device_t, device_t, void *);
     67 int		gpioiic_detach(device_t, int);
     68 
     69 int		gpioiic_i2c_send_start(void *, int);
     70 int		gpioiic_i2c_send_stop(void *, int);
     71 int		gpioiic_i2c_initiate_xfer(void *, i2c_addr_t, int);
     72 int		gpioiic_i2c_read_byte(void *, uint8_t *, int);
     73 int		gpioiic_i2c_write_byte(void *, uint8_t, int);
     74 
     75 void		gpioiic_bb_set_bits(void *, uint32_t);
     76 void		gpioiic_bb_set_dir(void *, uint32_t);
     77 uint32_t	gpioiic_bb_read_bits(void *);
     78 
     79 CFATTACH_DECL_NEW(gpioiic, sizeof(struct gpioiic_softc),
     80 	gpioiic_match, gpioiic_attach, gpioiic_detach, NULL);
     81 
     82 static const struct i2c_bitbang_ops gpioiic_bbops = {
     83 	gpioiic_bb_set_bits,
     84 	gpioiic_bb_set_dir,
     85 	gpioiic_bb_read_bits,
     86 	{ GPIOIIC_SDA, GPIOIIC_SCL, GPIOIIC_SDA, 0 }
     87 };
     88 
     89 int
     90 gpioiic_match(device_t parent, cfdata_t cf, void *aux)
     91 {
     92 	struct gpio_attach_args *ga = aux;
     93 
     94 	if (strcmp(ga->ga_dvname, cf->cf_name))
     95 		return 0;
     96 
     97 	if (ga->ga_offset == -1)
     98 		return 0;
     99 
    100 	/* Check that we have enough pins */
    101 	if (gpio_npins(ga->ga_mask) != GPIOIIC_NPINS) {
    102 		aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
    103 		    ga->ga_mask);
    104 		return 0;
    105 	}
    106 	return 1;
    107 }
    108 
    109 void
    110 gpioiic_attach(device_t parent, device_t self, void *aux)
    111 {
    112 	struct gpioiic_softc *sc = device_private(self);
    113 	struct gpio_attach_args *ga = aux;
    114 	int caps;
    115 
    116 	/* Map pins */
    117 	sc->sc_gpio = ga->ga_gpio;
    118 	sc->sc_map.pm_map = sc->_map;
    119 
    120 
    121 	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
    122 	    &sc->sc_map)) {
    123 		aprint_error(": can't map pins\n");
    124 		return;
    125 	}
    126 
    127 	if (ga->ga_flags & GPIOIIC_PIN_REVERSE) {
    128 		sc->sc_pin_sda = GPIOIIC_PIN_SCL;
    129 		sc->sc_pin_scl = GPIOIIC_PIN_SDA;
    130 	} else {
    131 		sc->sc_pin_sda = GPIOIIC_PIN_SDA;
    132 		sc->sc_pin_scl = GPIOIIC_PIN_SCL;
    133 	}
    134 
    135 	/* Configure SDA pin */
    136 	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda);
    137 	if (!(caps & GPIO_PIN_OUTPUT)) {
    138 		aprint_error(": SDA pin is unable to drive output\n");
    139 		goto fail;
    140 	}
    141 	if (!(caps & GPIO_PIN_INPUT)) {
    142 		aprint_error(": SDA pin is unable to read input\n");
    143 		goto fail;
    144 	}
    145 	aprint_normal(": SDA[%d]", sc->sc_map.pm_map[sc->sc_pin_sda]);
    146 	sc->sc_sda = GPIO_PIN_OUTPUT;
    147 	if (caps & GPIO_PIN_OPENDRAIN) {
    148 		aprint_normal(" open-drain");
    149 		sc->sc_sda |= GPIO_PIN_OPENDRAIN;
    150 	} else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) {
    151 		aprint_normal(" push-pull tri-state");
    152 		sc->sc_sda |= GPIO_PIN_PUSHPULL;
    153 	}
    154 	if (caps & GPIO_PIN_PULLUP) {
    155 		aprint_normal(" pull-up");
    156 		sc->sc_sda |= GPIO_PIN_PULLUP;
    157 	}
    158 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda, sc->sc_sda);
    159 
    160 	/* Configure SCL pin */
    161 	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, sc->sc_pin_scl);
    162 	if (!(caps & GPIO_PIN_OUTPUT)) {
    163 		aprint_error(": SCL pin is unable to drive output\n");
    164 		goto fail;
    165 	}
    166 	aprint_normal(", SCL[%d]", sc->sc_map.pm_map[sc->sc_pin_scl]);
    167 	sc->sc_scl = GPIO_PIN_OUTPUT;
    168 	if (caps & GPIO_PIN_OPENDRAIN) {
    169 		aprint_normal(" open-drain");
    170 		sc->sc_scl |= GPIO_PIN_OPENDRAIN;
    171 		if (caps & GPIO_PIN_PULLUP) {
    172 			aprint_normal(" pull-up");
    173 			sc->sc_scl |= GPIO_PIN_PULLUP;
    174 		}
    175 	} else if (caps & GPIO_PIN_PUSHPULL) {
    176 		aprint_normal(" push-pull");
    177 		sc->sc_scl |= GPIO_PIN_PUSHPULL;
    178 	}
    179 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, sc->sc_pin_scl, sc->sc_scl);
    180 
    181 	aprint_normal("\n");
    182 
    183 	/* Attach I2C bus */
    184 	iic_tag_init(&sc->sc_i2c_tag);
    185 	sc->sc_i2c_tag.ic_cookie = sc;
    186 	sc->sc_i2c_tag.ic_send_start = gpioiic_i2c_send_start;
    187 	sc->sc_i2c_tag.ic_send_stop = gpioiic_i2c_send_stop;
    188 	sc->sc_i2c_tag.ic_initiate_xfer = gpioiic_i2c_initiate_xfer;
    189 	sc->sc_i2c_tag.ic_read_byte = gpioiic_i2c_read_byte;
    190 	sc->sc_i2c_tag.ic_write_byte = gpioiic_i2c_write_byte;
    191 
    192 	sc->sc_i2c_dev = iicbus_attach(self, &sc->sc_i2c_tag);
    193 
    194 	if (!pmf_device_register(self, NULL, NULL))
    195 		aprint_error("%s: could not establish power handler\n",
    196 		    device_xname(self));
    197 	return;
    198 
    199 fail:
    200 	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    201 }
    202 
    203 int
    204 gpioiic_detach(device_t self, int flags)
    205 {
    206 	struct gpioiic_softc *sc = device_private(self);
    207 	int error;
    208 
    209 	error = config_detach_children(self, flags);
    210 	if (error)
    211 		return error;
    212 
    213 	iic_tag_fini(&sc->sc_i2c_tag);
    214 	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    215 	pmf_device_deregister(self);
    216 	return 0;
    217 }
    218 
    219 int
    220 gpioiic_i2c_send_start(void *cookie, int flags)
    221 {
    222 	return i2c_bitbang_send_start(cookie, flags, &gpioiic_bbops);
    223 }
    224 
    225 int
    226 gpioiic_i2c_send_stop(void *cookie, int flags)
    227 {
    228 	return i2c_bitbang_send_stop(cookie, flags, &gpioiic_bbops);
    229 }
    230 
    231 int
    232 gpioiic_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    233 {
    234 	return i2c_bitbang_initiate_xfer(cookie, addr, flags, &gpioiic_bbops);
    235 }
    236 
    237 int
    238 gpioiic_i2c_read_byte(void *cookie, uint8_t *bytep, int flags)
    239 {
    240 	return i2c_bitbang_read_byte(cookie, bytep, flags, &gpioiic_bbops);
    241 }
    242 
    243 int
    244 gpioiic_i2c_write_byte(void *cookie, uint8_t byte, int flags)
    245 {
    246 	return i2c_bitbang_write_byte(cookie, byte, flags, &gpioiic_bbops);
    247 }
    248 
    249 void
    250 gpioiic_bb_set_bits(void *cookie, uint32_t bits)
    251 {
    252 	struct gpioiic_softc *sc = cookie;
    253 
    254 	gpio_pin_write(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda,
    255 	    bits & GPIOIIC_SDA ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
    256 	gpio_pin_write(sc->sc_gpio, &sc->sc_map, sc->sc_pin_scl,
    257 	    bits & GPIOIIC_SCL ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
    258 }
    259 
    260 void
    261 gpioiic_bb_set_dir(void *cookie, uint32_t bits)
    262 {
    263 	struct gpioiic_softc *sc = cookie;
    264 	int sda = sc->sc_sda;
    265 
    266 	sda &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
    267 	sda |= (bits & GPIOIIC_SDA ? GPIO_PIN_OUTPUT : GPIO_PIN_INPUT);
    268 	if ((sda & GPIO_PIN_PUSHPULL) && !(bits & GPIOIIC_SDA))
    269 		sda |= GPIO_PIN_TRISTATE;
    270 	if (sc->sc_sda != sda) {
    271 		sc->sc_sda = sda;
    272 		gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda,
    273 		    sc->sc_sda);
    274 	}
    275 }
    276 
    277 uint32_t
    278 gpioiic_bb_read_bits(void *cookie)
    279 {
    280 	struct gpioiic_softc *sc = cookie;
    281 	uint32_t bits = 0;
    282 
    283 	if (gpio_pin_read(sc->sc_gpio, &sc->sc_map,
    284 	    sc->sc_pin_sda) == GPIO_PIN_HIGH)
    285 		bits |= GPIOIIC_SDA;
    286 	if (gpio_pin_read(sc->sc_gpio, &sc->sc_map,
    287 	    sc->sc_pin_scl) == GPIO_PIN_HIGH)
    288 		bits |= GPIOIIC_SCL;
    289 
    290 	return bits;
    291 }
    292 
    293 MODULE(MODULE_CLASS_DRIVER, gpioiic, "gpio,iic");
    294 
    295 #ifdef _MODULE
    296 #include "ioconf.c"
    297 #endif
    298 
    299 static int
    300 gpioiic_modcmd(modcmd_t cmd, void *opaque)
    301 {
    302 	int error;
    303 
    304 	error = 0;
    305 	switch (cmd) {
    306 	case MODULE_CMD_INIT:
    307 #ifdef _MODULE
    308 		error = config_init_component(cfdriver_ioconf_gpioiic,
    309 		    cfattach_ioconf_gpioiic, cfdata_ioconf_gpioiic);
    310 		if (error)
    311 			aprint_error("%s: unable to init component\n",
    312 			    gpioiic_cd.cd_name);
    313 #endif
    314 		break;
    315 	case MODULE_CMD_FINI:
    316 #ifdef _MODULE
    317 		config_fini_component(cfdriver_ioconf_gpioiic,
    318 		    cfattach_ioconf_gpioiic, cfdata_ioconf_gpioiic);
    319 #endif
    320 		break;
    321 	default:
    322 		error = ENOTTY;
    323 	}
    324 	return error;
    325 }
    326