Home | History | Annotate | Line # | Download | only in gpio
gpiolock.c revision 1.2.2.2
      1  1.2.2.2  yamt /* $NetBSD: gpiolock.c,v 1.2.2.2 2009/08/19 18:47:05 yamt Exp $ */
      2  1.2.2.2  yamt 
      3  1.2.2.2  yamt /*
      4  1.2.2.2  yamt  * Copyright (c) 2009 Marc Balmer <marc (at) msys.ch>
      5  1.2.2.2  yamt  * All rights reserved.
      6  1.2.2.2  yamt  *
      7  1.2.2.2  yamt  * Redistribution and use in source and binary forms, with or without
      8  1.2.2.2  yamt  * modification, are permitted provided that the following conditions
      9  1.2.2.2  yamt  * are met:
     10  1.2.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.2.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.2.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     15  1.2.2.2  yamt  *
     16  1.2.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.2.2.2  yamt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.2.2.2  yamt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.2.2.2  yamt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.2.2.2  yamt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.2.2.2  yamt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.2.2.2  yamt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.2.2.2  yamt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.2.2.2  yamt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.2.2.2  yamt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.2.2.2  yamt  */
     27  1.2.2.2  yamt 
     28  1.2.2.2  yamt /*
     29  1.2.2.2  yamt  * Driver for multi-position keylocks on GPIO pins
     30  1.2.2.2  yamt  */
     31  1.2.2.2  yamt 
     32  1.2.2.2  yamt #include "opt_keylock.h"
     33  1.2.2.2  yamt 
     34  1.2.2.2  yamt #include <sys/param.h>
     35  1.2.2.2  yamt #include <sys/device.h>
     36  1.2.2.2  yamt #include <sys/gpio.h>
     37  1.2.2.2  yamt 
     38  1.2.2.2  yamt #include <dev/gpio/gpiovar.h>
     39  1.2.2.2  yamt #include <dev/keylock.h>
     40  1.2.2.2  yamt 
     41  1.2.2.2  yamt #define GPIOLOCK_MAXPINS	4
     42  1.2.2.2  yamt #define GPIOLOCK_MINPINS	2
     43  1.2.2.2  yamt 
     44  1.2.2.2  yamt struct gpiolock_softc {
     45  1.2.2.2  yamt 	void *			sc_gpio;
     46  1.2.2.2  yamt 	struct gpio_pinmap	sc_map;
     47  1.2.2.2  yamt 	int			_map[GPIOLOCK_MAXPINS];
     48  1.2.2.2  yamt 
     49  1.2.2.2  yamt 	int			sc_npins;
     50  1.2.2.2  yamt 	int			sc_data;
     51  1.2.2.2  yamt 	int			sc_dying;
     52  1.2.2.2  yamt };
     53  1.2.2.2  yamt 
     54  1.2.2.2  yamt int gpiolock_match(device_t, cfdata_t, void *);
     55  1.2.2.2  yamt void gpiolock_attach(device_t, device_t, void *);
     56  1.2.2.2  yamt int gpiolock_detach(device_t, int);
     57  1.2.2.2  yamt int gpiolock_activate(device_t, enum devact);
     58  1.2.2.2  yamt int gpiolock_position(void *);
     59  1.2.2.2  yamt 
     60  1.2.2.2  yamt CFATTACH_DECL_NEW(gpiolock, sizeof(struct gpiolock_softc),
     61  1.2.2.2  yamt 	gpiolock_match, gpiolock_attach, gpiolock_detach, gpiolock_activate);
     62  1.2.2.2  yamt 
     63  1.2.2.2  yamt extern struct cfdriver gpiolock_cd;
     64  1.2.2.2  yamt 
     65  1.2.2.2  yamt int
     66  1.2.2.2  yamt gpiolock_match(device_t parent, cfdata_t cf,
     67  1.2.2.2  yamt     void *aux)
     68  1.2.2.2  yamt {
     69  1.2.2.2  yamt 	struct gpio_attach_args *ga = aux;
     70  1.2.2.2  yamt 	int npins;
     71  1.2.2.2  yamt 
     72  1.2.2.2  yamt 	if (strcmp(ga->ga_dvname, cf->cf_name))
     73  1.2.2.2  yamt 		return 0;
     74  1.2.2.2  yamt 
     75  1.2.2.2  yamt 	if (ga->ga_offset == -1)
     76  1.2.2.2  yamt 		return 0;
     77  1.2.2.2  yamt 
     78  1.2.2.2  yamt 	/* Check number of pins */
     79  1.2.2.2  yamt 	npins = gpio_npins(ga->ga_mask);
     80  1.2.2.2  yamt 	if (npins < GPIOLOCK_MINPINS || npins > GPIOLOCK_MAXPINS) {
     81  1.2.2.2  yamt 		aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
     82  1.2.2.2  yamt 		    ga->ga_mask);
     83  1.2.2.2  yamt 		return 0;
     84  1.2.2.2  yamt 	}
     85  1.2.2.2  yamt 
     86  1.2.2.2  yamt 	return 1;
     87  1.2.2.2  yamt }
     88  1.2.2.2  yamt 
     89  1.2.2.2  yamt void
     90  1.2.2.2  yamt gpiolock_attach(device_t parent, device_t self, void *aux)
     91  1.2.2.2  yamt {
     92  1.2.2.2  yamt 	struct gpiolock_softc *sc = device_private(self);
     93  1.2.2.2  yamt 	struct gpio_attach_args *ga = aux;
     94  1.2.2.2  yamt 	int pin, caps;
     95  1.2.2.2  yamt 
     96  1.2.2.2  yamt 	sc->sc_npins = gpio_npins(ga->ga_mask);
     97  1.2.2.2  yamt 
     98  1.2.2.2  yamt 	/* Map pins */
     99  1.2.2.2  yamt 	sc->sc_gpio = ga->ga_gpio;
    100  1.2.2.2  yamt 	sc->sc_map.pm_map = sc->_map;
    101  1.2.2.2  yamt 	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
    102  1.2.2.2  yamt 	    &sc->sc_map)) {
    103  1.2.2.2  yamt 		aprint_error(": can't map pins\n");
    104  1.2.2.2  yamt 		return;
    105  1.2.2.2  yamt 	}
    106  1.2.2.2  yamt 
    107  1.2.2.2  yamt 	/* Configure data pins */
    108  1.2.2.2  yamt 	for (pin = 0; pin < sc->sc_npins; pin++) {
    109  1.2.2.2  yamt 		caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, pin);
    110  1.2.2.2  yamt 		if (!(caps & GPIO_PIN_INPUT)) {
    111  1.2.2.2  yamt 			aprint_error(": data pin is unable to read input\n");
    112  1.2.2.2  yamt 			goto fail;
    113  1.2.2.2  yamt 		}
    114  1.2.2.2  yamt 		aprint_normal(" [%d]", sc->sc_map.pm_map[pin]);
    115  1.2.2.2  yamt 		sc->sc_data = GPIO_PIN_INPUT;
    116  1.2.2.2  yamt 		gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, pin, sc->sc_data);
    117  1.2.2.2  yamt 	}
    118  1.2.2.2  yamt 
    119  1.2.2.2  yamt #ifdef KEYLOCK
    120  1.2.2.2  yamt 	/* Register keylock */
    121  1.2.2.2  yamt 	if (keylock_register(self, sc->sc_npins, gpiolock_position)) {
    122  1.2.2.2  yamt 		aprint_error(": can't register keylock\n");
    123  1.2.2.2  yamt 		goto fail;
    124  1.2.2.2  yamt 	}
    125  1.2.2.2  yamt #endif
    126  1.2.2.2  yamt 	pmf_device_register(self, NULL, NULL);
    127  1.2.2.2  yamt 
    128  1.2.2.2  yamt 	aprint_normal("\n");
    129  1.2.2.2  yamt 	return;
    130  1.2.2.2  yamt 
    131  1.2.2.2  yamt fail:
    132  1.2.2.2  yamt 	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    133  1.2.2.2  yamt }
    134  1.2.2.2  yamt 
    135  1.2.2.2  yamt int
    136  1.2.2.2  yamt gpiolock_detach(device_t self, int flags)
    137  1.2.2.2  yamt {
    138  1.2.2.2  yamt 	struct gpiolock_softc *sc = device_private(self);
    139  1.2.2.2  yamt 
    140  1.2.2.2  yamt 	pmf_device_deregister(self);
    141  1.2.2.2  yamt #ifdef KEYLOCK
    142  1.2.2.2  yamt 	keylock_unregister(self, gpiolock_position);
    143  1.2.2.2  yamt #endif
    144  1.2.2.2  yamt 	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
    145  1.2.2.2  yamt 
    146  1.2.2.2  yamt 	return 0;
    147  1.2.2.2  yamt }
    148  1.2.2.2  yamt 
    149  1.2.2.2  yamt int
    150  1.2.2.2  yamt gpiolock_activate(device_t self, enum devact act)
    151  1.2.2.2  yamt {
    152  1.2.2.2  yamt 	struct gpiolock_softc *sc = device_private(self);
    153  1.2.2.2  yamt 
    154  1.2.2.2  yamt 	switch (act) {
    155  1.2.2.2  yamt 	case DVACT_ACTIVATE:
    156  1.2.2.2  yamt 		return EOPNOTSUPP;
    157  1.2.2.2  yamt 	case DVACT_DEACTIVATE:
    158  1.2.2.2  yamt 		sc->sc_dying = 1;
    159  1.2.2.2  yamt 		break;
    160  1.2.2.2  yamt 	}
    161  1.2.2.2  yamt 
    162  1.2.2.2  yamt 	return 0;
    163  1.2.2.2  yamt }
    164  1.2.2.2  yamt 
    165  1.2.2.2  yamt int
    166  1.2.2.2  yamt gpiolock_position(void *arg)
    167  1.2.2.2  yamt {
    168  1.2.2.2  yamt 	struct gpiolock_softc *sc = device_private((device_t)arg);
    169  1.2.2.2  yamt 	int pos, pin;
    170  1.2.2.2  yamt 
    171  1.2.2.2  yamt 	for (pos = pin = 0; pin < sc->sc_npins; pin++) {
    172  1.2.2.2  yamt 		if (gpio_pin_read(sc->sc_gpio, &sc->sc_map, pin) ==
    173  1.2.2.2  yamt 		    GPIO_PIN_HIGH)
    174  1.2.2.2  yamt 			pos = pin + 1;
    175  1.2.2.2  yamt 	}
    176  1.2.2.2  yamt 	return pos;
    177  1.2.2.2  yamt }
    178  1.2.2.2  yamt 
    179