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