Home | History | Annotate | Line # | Download | only in gpio
gpioow.c revision 1.15.4.1
      1  1.15.4.1    martin /* $NetBSD: gpioow.c,v 1.15.4.1 2020/04/08 14:08:04 martin Exp $ */
      2       1.1       riz /*	$OpenBSD: gpioow.c,v 1.1 2006/03/04 16:27:03 grange Exp $	*/
      3       1.1       riz 
      4       1.1       riz /*
      5       1.1       riz  * Copyright (c) 2006 Alexander Yurchenko <grange (at) openbsd.org>
      6       1.1       riz  *
      7       1.1       riz  * Permission to use, copy, modify, and distribute this software for any
      8       1.1       riz  * purpose with or without fee is hereby granted, provided that the above
      9       1.1       riz  * copyright notice and this permission notice appear in all copies.
     10       1.1       riz  *
     11       1.1       riz  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12       1.1       riz  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13       1.1       riz  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14       1.1       riz  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15       1.1       riz  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16       1.1       riz  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17       1.1       riz  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18       1.1       riz  */
     19       1.1       riz 
     20       1.1       riz #include <sys/cdefs.h>
     21  1.15.4.1    martin __KERNEL_RCSID(0, "$NetBSD: gpioow.c,v 1.15.4.1 2020/04/08 14:08:04 martin Exp $");
     22       1.1       riz 
     23       1.1       riz /*
     24       1.1       riz  * 1-Wire bus bit-banging through GPIO pin.
     25       1.1       riz  */
     26       1.1       riz 
     27       1.1       riz #include <sys/param.h>
     28       1.1       riz #include <sys/systm.h>
     29       1.1       riz #include <sys/device.h>
     30       1.1       riz #include <sys/gpio.h>
     31      1.13   mbalmer #include <sys/module.h>
     32       1.1       riz 
     33       1.1       riz #include <dev/gpio/gpiovar.h>
     34       1.1       riz 
     35       1.1       riz #include <dev/onewire/onewirevar.h>
     36       1.1       riz 
     37      1.15  riastrad #include "ioconf.h"
     38      1.15  riastrad 
     39       1.1       riz #define GPIOOW_NPINS		1
     40       1.1       riz #define GPIOOW_PIN_DATA		0
     41       1.1       riz 
     42       1.1       riz struct gpioow_softc {
     43       1.1       riz 	void *			sc_gpio;
     44       1.1       riz 	struct gpio_pinmap	sc_map;
     45      1.10   mbalmer 	int			_map[GPIOOW_NPINS];
     46       1.1       riz 
     47       1.1       riz 	struct onewire_bus	sc_ow_bus;
     48       1.5   xtraeme 	device_t		sc_ow_dev;
     49       1.1       riz 
     50       1.1       riz 	int			sc_data;
     51       1.1       riz 	int			sc_dying;
     52       1.1       riz };
     53       1.1       riz 
     54  1.15.4.1    martin static int	gpioow_match(device_t, cfdata_t, void *);
     55  1.15.4.1    martin static void	gpioow_attach(device_t, device_t, void *);
     56  1.15.4.1    martin static int	gpioow_detach(device_t, int);
     57  1.15.4.1    martin static int	gpioow_activate(device_t, enum devact);
     58  1.15.4.1    martin 
     59  1.15.4.1    martin static int	gpioow_ow_reset(void *);
     60  1.15.4.1    martin static int	gpioow_ow_read_bit(void *);
     61  1.15.4.1    martin static void	gpioow_ow_write_bit(void *, int);
     62  1.15.4.1    martin 
     63  1.15.4.1    martin static void	gpioow_bb_rx(void *);
     64  1.15.4.1    martin static void	gpioow_bb_tx(void *);
     65  1.15.4.1    martin static int	gpioow_bb_get(void *);
     66  1.15.4.1    martin static void	gpioow_bb_set(void *, int);
     67       1.1       riz 
     68       1.5   xtraeme CFATTACH_DECL_NEW(gpioow, sizeof(struct gpioow_softc),
     69       1.1       riz 	gpioow_match, gpioow_attach, gpioow_detach, gpioow_activate);
     70       1.1       riz 
     71       1.1       riz static const struct onewire_bbops gpioow_bbops = {
     72       1.1       riz 	gpioow_bb_rx,
     73       1.1       riz 	gpioow_bb_tx,
     74       1.1       riz 	gpioow_bb_get,
     75       1.1       riz 	gpioow_bb_set
     76       1.1       riz };
     77       1.1       riz 
     78  1.15.4.1    martin static int
     79       1.9   mbalmer gpioow_match(device_t parent, cfdata_t cf, void *aux)
     80       1.1       riz {
     81       1.7   mbalmer 	struct gpio_attach_args *ga = aux;
     82       1.7   mbalmer 
     83       1.8   mbalmer 	if (strcmp(ga->ga_dvname, cf->cf_name))
     84       1.8   mbalmer 		return 0;
     85       1.8   mbalmer 
     86       1.7   mbalmer 	if (ga->ga_offset == -1)
     87       1.7   mbalmer 		return 0;
     88       1.7   mbalmer 
     89       1.8   mbalmer 	/* Check that we have enough pins */
     90       1.8   mbalmer 	if (gpio_npins(ga->ga_mask) != GPIOOW_NPINS) {
     91      1.14   mbalmer 		aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
     92       1.8   mbalmer 		    ga->ga_mask);
     93       1.8   mbalmer 		return 0;
     94       1.8   mbalmer 	}
     95       1.8   mbalmer 	return 1;
     96       1.1       riz }
     97       1.1       riz 
     98  1.15.4.1    martin static void
     99       1.4    cegger gpioow_attach(device_t parent, device_t self, void *aux)
    100       1.1       riz {
    101       1.1       riz 	struct gpioow_softc *sc = device_private(self);
    102       1.1       riz 	struct gpio_attach_args *ga = aux;
    103       1.1       riz 	struct onewirebus_attach_args oba;
    104       1.1       riz 	int caps;
    105       1.1       riz 
    106       1.1       riz 	/* Map pins */
    107       1.1       riz 	sc->sc_gpio = ga->ga_gpio;
    108      1.10   mbalmer 	sc->sc_map.pm_map = sc->_map;
    109       1.1       riz 	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
    110       1.1       riz 	    &sc->sc_map)) {
    111       1.8   mbalmer 		aprint_error(": can't map pins\n");
    112      1.13   mbalmer 		goto finish;
    113       1.1       riz 	}
    114       1.1       riz 
    115       1.1       riz 	/* Configure data pin */
    116       1.1       riz 	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA);
    117       1.1       riz 	if (!(caps & GPIO_PIN_OUTPUT)) {
    118       1.8   mbalmer 		aprint_error(": data pin is unable to drive output\n");
    119      1.13   mbalmer 		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    120      1.13   mbalmer 		goto finish;
    121       1.1       riz 	}
    122       1.1       riz 	if (!(caps & GPIO_PIN_INPUT)) {
    123       1.8   mbalmer 		aprint_error(": data pin is unable to read input\n");
    124      1.13   mbalmer 		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    125      1.13   mbalmer 		goto finish;
    126       1.1       riz 	}
    127       1.8   mbalmer 	aprint_normal(": DATA[%d]", sc->sc_map.pm_map[GPIOOW_PIN_DATA]);
    128       1.1       riz 	sc->sc_data = GPIO_PIN_OUTPUT;
    129       1.1       riz 	if (caps & GPIO_PIN_OPENDRAIN) {
    130       1.8   mbalmer 		aprint_normal(" open-drain");
    131       1.1       riz 		sc->sc_data |= GPIO_PIN_OPENDRAIN;
    132       1.1       riz 	} else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) {
    133       1.8   mbalmer 		aprint_normal(" push-pull tri-state");
    134       1.1       riz 		sc->sc_data |= GPIO_PIN_PUSHPULL;
    135       1.1       riz 	}
    136       1.1       riz 	if (caps & GPIO_PIN_PULLUP) {
    137       1.8   mbalmer 		aprint_normal(" pull-up");
    138       1.1       riz 		sc->sc_data |= GPIO_PIN_PULLUP;
    139       1.1       riz 	}
    140       1.1       riz 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, sc->sc_data);
    141       1.1       riz 
    142       1.8   mbalmer 	aprint_normal("\n");
    143       1.1       riz 
    144       1.1       riz 	/* Attach 1-Wire bus */
    145       1.1       riz 	sc->sc_ow_bus.bus_cookie = sc;
    146       1.1       riz 	sc->sc_ow_bus.bus_reset = gpioow_ow_reset;
    147  1.15.4.1    martin 	sc->sc_ow_bus.bus_read_bit = gpioow_ow_read_bit;
    148  1.15.4.1    martin 	sc->sc_ow_bus.bus_write_bit = gpioow_ow_write_bit;
    149       1.1       riz 
    150       1.6    cegger 	memset(&oba, 0, sizeof(oba));
    151       1.1       riz 	oba.oba_bus = &sc->sc_ow_bus;
    152       1.1       riz 	sc->sc_ow_dev = config_found(self, &oba, onewirebus_print);
    153       1.1       riz 
    154      1.11   mbalmer 	if (!pmf_device_register(self, NULL, NULL))
    155      1.11   mbalmer 		aprint_error("%s: could not establish power handler\n",
    156      1.11   mbalmer 		    device_xname(self));
    157      1.13   mbalmer finish:
    158       1.1       riz 	return;
    159       1.1       riz }
    160       1.1       riz 
    161  1.15.4.1    martin static int
    162       1.4    cegger gpioow_detach(device_t self, int flags)
    163       1.1       riz {
    164       1.1       riz 	struct gpioow_softc *sc = device_private(self);
    165       1.1       riz 	int rv = 0;
    166       1.1       riz 
    167       1.1       riz 	if (sc->sc_ow_dev != NULL)
    168       1.1       riz 		rv = config_detach(sc->sc_ow_dev, flags);
    169       1.1       riz 
    170      1.11   mbalmer 	if (!rv) {
    171      1.11   mbalmer 		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    172      1.11   mbalmer 		pmf_device_deregister(self);
    173      1.11   mbalmer 	}
    174       1.7   mbalmer 	return rv;
    175       1.1       riz }
    176       1.1       riz 
    177  1.15.4.1    martin static int
    178       1.4    cegger gpioow_activate(device_t self, enum devact act)
    179       1.1       riz {
    180       1.1       riz 	struct gpioow_softc *sc = device_private(self);
    181       1.1       riz 
    182       1.1       riz 	switch (act) {
    183       1.1       riz 	case DVACT_DEACTIVATE:
    184       1.1       riz 		sc->sc_dying = 1;
    185      1.12    dyoung 		return 0;
    186      1.12    dyoung 	default:
    187      1.12    dyoung 		return EOPNOTSUPP;
    188       1.1       riz 	}
    189       1.1       riz }
    190       1.1       riz 
    191  1.15.4.1    martin static int
    192       1.1       riz gpioow_ow_reset(void *arg)
    193       1.1       riz {
    194       1.1       riz 	return (onewire_bb_reset(&gpioow_bbops, arg));
    195       1.1       riz }
    196       1.1       riz 
    197  1.15.4.1    martin static int
    198  1.15.4.1    martin gpioow_ow_read_bit(void *arg)
    199       1.1       riz {
    200  1.15.4.1    martin 	return (onewire_bb_read_bit(&gpioow_bbops, arg));
    201       1.1       riz }
    202       1.1       riz 
    203  1.15.4.1    martin static void
    204  1.15.4.1    martin gpioow_ow_write_bit(void *arg, int value)
    205  1.15.4.1    martin {
    206  1.15.4.1    martin 	onewire_bb_write_bit(&gpioow_bbops, arg, value);
    207  1.15.4.1    martin }
    208  1.15.4.1    martin 
    209  1.15.4.1    martin static void
    210       1.1       riz gpioow_bb_rx(void *arg)
    211       1.1       riz {
    212       1.1       riz 	struct gpioow_softc *sc = arg;
    213       1.1       riz 	int data = sc->sc_data;
    214       1.1       riz 
    215       1.1       riz 	data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
    216       1.1       riz 	data |= GPIO_PIN_INPUT;
    217       1.1       riz 	if (data & GPIO_PIN_PUSHPULL)
    218       1.1       riz 		data |= GPIO_PIN_TRISTATE;
    219       1.1       riz 	if (sc->sc_data != data) {
    220       1.1       riz 		sc->sc_data = data;
    221       1.1       riz 		gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
    222       1.1       riz 		    sc->sc_data);
    223       1.1       riz 	}
    224       1.1       riz }
    225       1.1       riz 
    226  1.15.4.1    martin static void
    227       1.1       riz gpioow_bb_tx(void *arg)
    228       1.1       riz {
    229       1.1       riz 	struct gpioow_softc *sc = arg;
    230       1.1       riz 	int data = sc->sc_data;
    231       1.1       riz 
    232       1.1       riz 	data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
    233       1.1       riz 	data |= GPIO_PIN_OUTPUT;
    234       1.1       riz 	if (sc->sc_data != data) {
    235       1.1       riz 		sc->sc_data = data;
    236       1.1       riz 		gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
    237       1.1       riz 		    sc->sc_data);
    238       1.1       riz 	}
    239       1.1       riz }
    240       1.1       riz 
    241  1.15.4.1    martin static int
    242       1.1       riz gpioow_bb_get(void *arg)
    243       1.1       riz {
    244       1.1       riz 	struct gpioow_softc *sc = arg;
    245       1.1       riz 
    246       1.1       riz 	return (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA) ==
    247       1.1       riz 	    GPIO_PIN_HIGH ? 1 : 0);
    248       1.1       riz }
    249       1.1       riz 
    250  1.15.4.1    martin static void
    251       1.1       riz gpioow_bb_set(void *arg, int value)
    252       1.1       riz {
    253       1.1       riz 	struct gpioow_softc *sc = arg;
    254       1.1       riz 
    255       1.1       riz 	gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
    256       1.1       riz 	    value ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
    257       1.1       riz }
    258      1.13   mbalmer 
    259      1.13   mbalmer MODULE(MODULE_CLASS_DRIVER, gpioow, "gpio,onewire");
    260      1.13   mbalmer 
    261      1.13   mbalmer #ifdef _MODULE
    262      1.13   mbalmer #include "ioconf.c"
    263      1.13   mbalmer #endif
    264      1.13   mbalmer 
    265      1.13   mbalmer static int
    266      1.13   mbalmer gpioow_modcmd(modcmd_t cmd, void *opaque)
    267      1.13   mbalmer {
    268      1.13   mbalmer 	int error;
    269      1.13   mbalmer 
    270      1.13   mbalmer 	error = 0;
    271      1.13   mbalmer 	switch (cmd) {
    272      1.13   mbalmer 	case MODULE_CMD_INIT:
    273      1.13   mbalmer #ifdef _MODULE
    274      1.13   mbalmer 		error = config_init_component(cfdriver_ioconf_gpioow,
    275      1.13   mbalmer 		    cfattach_ioconf_gpioow, cfdata_ioconf_gpioow);
    276      1.13   mbalmer 		if (error)
    277      1.13   mbalmer 			aprint_error("%s: unable to init component\n",
    278      1.13   mbalmer 			    gpioow_cd.cd_name);
    279      1.13   mbalmer #endif
    280      1.13   mbalmer 		break;
    281      1.13   mbalmer 	case MODULE_CMD_FINI:
    282      1.13   mbalmer #ifdef _MODULE
    283      1.13   mbalmer 		config_fini_component(cfdriver_ioconf_gpioow,
    284      1.13   mbalmer 		    cfattach_ioconf_gpioow, cfdata_ioconf_gpioow);
    285      1.13   mbalmer #endif
    286      1.13   mbalmer 		break;
    287      1.13   mbalmer 	default:
    288      1.13   mbalmer 		error = ENOTTY;
    289      1.13   mbalmer 	}
    290      1.13   mbalmer 	return error;
    291      1.13   mbalmer }
    292