Home | History | Annotate | Line # | Download | only in gpio
gpioow.c revision 1.3.52.3
      1 /* $NetBSD: gpioow.c,v 1.3.52.3 2009/08/19 18:47:05 yamt Exp $ */
      2 /*	$OpenBSD: gpioow.c,v 1.1 2006/03/04 16:27:03 grange 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: gpioow.c,v 1.3.52.3 2009/08/19 18:47:05 yamt Exp $");
     22 
     23 /*
     24  * 1-Wire bus bit-banging through GPIO pin.
     25  */
     26 
     27 #include <sys/param.h>
     28 #include <sys/systm.h>
     29 #include <sys/device.h>
     30 #include <sys/gpio.h>
     31 
     32 #include <dev/gpio/gpiovar.h>
     33 
     34 #include <dev/onewire/onewirevar.h>
     35 
     36 #define GPIOOW_NPINS		1
     37 #define GPIOOW_PIN_DATA		0
     38 
     39 struct gpioow_softc {
     40 	void *			sc_gpio;
     41 	struct gpio_pinmap	sc_map;
     42 	int			_map[GPIOOW_NPINS];
     43 
     44 	struct onewire_bus	sc_ow_bus;
     45 	device_t		sc_ow_dev;
     46 
     47 	int			sc_data;
     48 	int			sc_dying;
     49 };
     50 
     51 int	gpioow_match(device_t, cfdata_t, void *);
     52 void	gpioow_attach(device_t, device_t, void *);
     53 int	gpioow_detach(device_t, int);
     54 int	gpioow_activate(device_t, enum devact);
     55 
     56 int	gpioow_ow_reset(void *);
     57 int	gpioow_ow_bit(void *, int);
     58 
     59 void	gpioow_bb_rx(void *);
     60 void	gpioow_bb_tx(void *);
     61 int	gpioow_bb_get(void *);
     62 void	gpioow_bb_set(void *, int);
     63 
     64 CFATTACH_DECL_NEW(gpioow, sizeof(struct gpioow_softc),
     65 	gpioow_match, gpioow_attach, gpioow_detach, gpioow_activate);
     66 
     67 extern struct cfdriver gpioow_cd;
     68 
     69 static const struct onewire_bbops gpioow_bbops = {
     70 	gpioow_bb_rx,
     71 	gpioow_bb_tx,
     72 	gpioow_bb_get,
     73 	gpioow_bb_set
     74 };
     75 
     76 int
     77 gpioow_match(device_t parent, cfdata_t cf, void *aux)
     78 {
     79 	struct gpio_attach_args *ga = aux;
     80 
     81 	if (strcmp(ga->ga_dvname, cf->cf_name))
     82 		return 0;
     83 
     84 	if (ga->ga_offset == -1)
     85 		return 0;
     86 
     87 	/* Check that we have enough pins */
     88 	if (gpio_npins(ga->ga_mask) != GPIOOW_NPINS) {
     89 		aprint_debug("%s: invalid pin mask 0x%02x/n", cf->cf_name,
     90 		    ga->ga_mask);
     91 		return 0;
     92 	}
     93 	return 1;
     94 }
     95 
     96 void
     97 gpioow_attach(device_t parent, device_t self, void *aux)
     98 {
     99 	struct gpioow_softc *sc = device_private(self);
    100 	struct gpio_attach_args *ga = aux;
    101 	struct onewirebus_attach_args oba;
    102 	int caps;
    103 
    104 	/* Map pins */
    105 	sc->sc_gpio = ga->ga_gpio;
    106 	sc->sc_map.pm_map = sc->_map;
    107 	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
    108 	    &sc->sc_map)) {
    109 		aprint_error(": can't map pins\n");
    110 		return;
    111 	}
    112 
    113 	/* Configure data pin */
    114 	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA);
    115 	if (!(caps & GPIO_PIN_OUTPUT)) {
    116 		aprint_error(": data pin is unable to drive output\n");
    117 		goto fail;
    118 	}
    119 	if (!(caps & GPIO_PIN_INPUT)) {
    120 		aprint_error(": data pin is unable to read input\n");
    121 		goto fail;
    122 	}
    123 	aprint_normal(": DATA[%d]", sc->sc_map.pm_map[GPIOOW_PIN_DATA]);
    124 	sc->sc_data = GPIO_PIN_OUTPUT;
    125 	if (caps & GPIO_PIN_OPENDRAIN) {
    126 		aprint_normal(" open-drain");
    127 		sc->sc_data |= GPIO_PIN_OPENDRAIN;
    128 	} else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) {
    129 		aprint_normal(" push-pull tri-state");
    130 		sc->sc_data |= GPIO_PIN_PUSHPULL;
    131 	}
    132 	if (caps & GPIO_PIN_PULLUP) {
    133 		aprint_normal(" pull-up");
    134 		sc->sc_data |= GPIO_PIN_PULLUP;
    135 	}
    136 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, sc->sc_data);
    137 
    138 	aprint_normal("\n");
    139 
    140 	/* Attach 1-Wire bus */
    141 	sc->sc_ow_bus.bus_cookie = sc;
    142 	sc->sc_ow_bus.bus_reset = gpioow_ow_reset;
    143 	sc->sc_ow_bus.bus_bit = gpioow_ow_bit;
    144 
    145 	memset(&oba, 0, sizeof(oba));
    146 	oba.oba_bus = &sc->sc_ow_bus;
    147 	sc->sc_ow_dev = config_found(self, &oba, onewirebus_print);
    148 
    149 	if (!pmf_device_register(self, NULL, NULL))
    150 		aprint_error("%s: could not establish power handler\n",
    151 		    device_xname(self));
    152 	return;
    153 
    154 fail:
    155 	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    156 }
    157 
    158 int
    159 gpioow_detach(device_t self, int flags)
    160 {
    161 	struct gpioow_softc *sc = device_private(self);
    162 	int rv = 0;
    163 
    164 	if (sc->sc_ow_dev != NULL)
    165 		rv = config_detach(sc->sc_ow_dev, flags);
    166 
    167 	if (!rv) {
    168 		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    169 		pmf_device_deregister(self);
    170 	}
    171 	return rv;
    172 }
    173 
    174 int
    175 gpioow_activate(device_t self, enum devact act)
    176 {
    177 	struct gpioow_softc *sc = device_private(self);
    178 	int rv = 0;
    179 
    180 	switch (act) {
    181 	case DVACT_ACTIVATE:
    182 		return EOPNOTSUPP;
    183 	case DVACT_DEACTIVATE:
    184 		sc->sc_dying = 1;
    185 		if (sc->sc_ow_dev != NULL)
    186 			rv = config_deactivate(sc->sc_ow_dev);
    187 		break;
    188 	}
    189 	return rv;
    190 }
    191 
    192 int
    193 gpioow_ow_reset(void *arg)
    194 {
    195 	return (onewire_bb_reset(&gpioow_bbops, arg));
    196 }
    197 
    198 int
    199 gpioow_ow_bit(void *arg, int value)
    200 {
    201 	return (onewire_bb_bit(&gpioow_bbops, arg, value));
    202 }
    203 
    204 void
    205 gpioow_bb_rx(void *arg)
    206 {
    207 	struct gpioow_softc *sc = arg;
    208 	int data = sc->sc_data;
    209 
    210 	data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
    211 	data |= GPIO_PIN_INPUT;
    212 	if (data & GPIO_PIN_PUSHPULL)
    213 		data |= GPIO_PIN_TRISTATE;
    214 	if (sc->sc_data != data) {
    215 		sc->sc_data = data;
    216 		gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
    217 		    sc->sc_data);
    218 	}
    219 }
    220 
    221 void
    222 gpioow_bb_tx(void *arg)
    223 {
    224 	struct gpioow_softc *sc = arg;
    225 	int data = sc->sc_data;
    226 
    227 	data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
    228 	data |= GPIO_PIN_OUTPUT;
    229 	if (sc->sc_data != data) {
    230 		sc->sc_data = data;
    231 		gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
    232 		    sc->sc_data);
    233 	}
    234 }
    235 
    236 int
    237 gpioow_bb_get(void *arg)
    238 {
    239 	struct gpioow_softc *sc = arg;
    240 
    241 	return (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA) ==
    242 	    GPIO_PIN_HIGH ? 1 : 0);
    243 }
    244 
    245 void
    246 gpioow_bb_set(void *arg, int value)
    247 {
    248 	struct gpioow_softc *sc = arg;
    249 
    250 	gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
    251 	    value ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
    252 }
    253