File:  [NetBSD] / src / sys / dev / gpio / gpiolock.c
Revision 1.3: download - view: text, annotated - select for diffs
Sun Dec 6 23:33:44 2009 CET (4 years, 7 months ago) by dyoung
Branches: MAIN
CVS tags: yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, yamt-nfs-mp-base9, yamt-nfs-mp-base11, yamt-nfs-mp-base10, uebayasi-xip-base4, uebayasi-xip-base3, uebayasi-xip-base2, uebayasi-xip-base1, uebayasi-xip-base, uebayasi-xip, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, rmind-uvmplock-nbase, rmind-uvmplock-base, rmind-uvmplock, rmind-smpnet-nbase, rmind-smpnet-base, rmind-smpnet, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, matt-premerge-20091211, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-mips64-premerge-20101231, khorben-n900, jruoho-x86intr-base, jruoho-x86intr, jmcneill-usbmp-pre-base2, jmcneill-usbmp-base9, jmcneill-usbmp-base8, jmcneill-usbmp-base7, jmcneill-usbmp-base6, jmcneill-usbmp-base5, jmcneill-usbmp-base4, jmcneill-usbmp-base3, jmcneill-usbmp-base2, jmcneill-usbmp-base10, jmcneill-usbmp-base, jmcneill-usbmp, jmcneill-audiomp3-base, jmcneill-audiomp3, cherry-xenmp-base, cherry-xenmp, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, HEAD
Diff to previous 1.2: preferred, colored
Changes since revision 1.2: +4 -5 lines
Delete unnecessary device-activation hooks.  Simplify the rest.

/tmp/CVS_MARKUP.4996
/* $NetBSD: gpiolock.c,v 1.2 2009/08/15 09:43:59 mbalmer Exp $ */

/*
 * Copyright (c) 2009 Marc Balmer <marc@msys.ch>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Driver for multi-position keylocks on GPIO pins
 */

#include "opt_keylock.h"

#include <sys/param.h>
#include <sys/device.h>
#include <sys/gpio.h>

#include <dev/gpio/gpiovar.h>
#include <dev/keylock.h>

#define GPIOLOCK_MAXPINS	4
#define GPIOLOCK_MINPINS	2

struct gpiolock_softc {
	void *			sc_gpio;
	struct gpio_pinmap	sc_map;
	int			_map[GPIOLOCK_MAXPINS];

	int			sc_npins;
	int			sc_data;
	int			sc_dying;
};

int gpiolock_match(device_t, cfdata_t, void *);
void gpiolock_attach(device_t, device_t, void *);
int gpiolock_detach(device_t, int);
int gpiolock_activate(device_t, enum devact);
int gpiolock_position(void *);

CFATTACH_DECL_NEW(gpiolock, sizeof(struct gpiolock_softc),
	gpiolock_match, gpiolock_attach, gpiolock_detach, gpiolock_activate);

extern struct cfdriver gpiolock_cd;

int
gpiolock_match(device_t parent, cfdata_t cf,
    void *aux)
{
	struct gpio_attach_args *ga = aux;
	int npins;

	if (strcmp(ga->ga_dvname, cf->cf_name))
		return 0;

	if (ga->ga_offset == -1)
		return 0;

	/* Check number of pins */
	npins = gpio_npins(ga->ga_mask);
	if (npins < GPIOLOCK_MINPINS || npins > GPIOLOCK_MAXPINS) {
		aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
		    ga->ga_mask);
		return 0;
	}

	return 1;
}

void
gpiolock_attach(device_t parent, device_t self, void *aux)
{
	struct gpiolock_softc *sc = device_private(self);
	struct gpio_attach_args *ga = aux;
	int pin, caps;

	sc->sc_npins = gpio_npins(ga->ga_mask);

	/* Map pins */
	sc->sc_gpio = ga->ga_gpio;
	sc->sc_map.pm_map = sc->_map;
	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
	    &sc->sc_map)) {
		aprint_error(": can't map pins\n");
		return;
	}

	/* Configure data pins */
	for (pin = 0; pin < sc->sc_npins; pin++) {
		caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, pin);
		if (!(caps & GPIO_PIN_INPUT)) {
			aprint_error(": data pin is unable to read input\n");
			goto fail;
		}
		aprint_normal(" [%d]", sc->sc_map.pm_map[pin]);
		sc->sc_data = GPIO_PIN_INPUT;
		gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, pin, sc->sc_data);
	}

#ifdef KEYLOCK
	/* Register keylock */
	if (keylock_register(self, sc->sc_npins, gpiolock_position)) {
		aprint_error(": can't register keylock\n");
		goto fail;
	}
#endif
	pmf_device_register(self, NULL, NULL);

	aprint_normal("\n");
	return;

fail:
	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
}

int
gpiolock_detach(device_t self, int flags)
{
	struct gpiolock_softc *sc = device_private(self);

	pmf_device_deregister(self);
#ifdef KEYLOCK
	keylock_unregister(self, gpiolock_position);
#endif
	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);

	return 0;
}

int
gpiolock_activate(device_t self, enum devact act)
{
	struct gpiolock_softc *sc = device_private(self);

	switch (act) {
	case DVACT_DEACTIVATE:
		sc->sc_dying = 1;
		return 0;
	default:
		return EOPNOTSUPP;
	}

}

int
gpiolock_position(void *arg)
{
	struct gpiolock_softc *sc = device_private((device_t)arg);
	int pos, pin;

	for (pos = pin = 0; pin < sc->sc_npins; pin++) {
		if (gpio_pin_read(sc->sc_gpio, &sc->sc_map, pin) ==
		    GPIO_PIN_HIGH)
			pos = pin + 1;
	}
	return pos;
}



cvsweb.de.netbsd.org <zafer@aydogan.de>