Home | History | Annotate | Line # | Download | only in tsarm
tskp.c revision 1.2.10.2
      1     /* $NetBSD: tskp.c,v 1.2.10.2 2005/04/29 11:28:08 kent Exp $ */
      2     
      3     /*-
      4      * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5      * All rights reserved.
      6      *
      7      * This code is from software contributed to The NetBSD Foundation
      8      * by Jesse Off.
      9      *
     10      * Redistribution and use in source and binary forms, with or without
     11      * modification, are permitted provided that the following conditions
     12      * are met:
     13      * 1. Redistributions of source code must retain the above copyright
     14      *    notice, this list of conditions and the following disclaimer.
     15      * 2. Redistributions in binary form must reproduce the above copyright
     16      *    notice, this list of conditions and the following disclaimer in the
     17      *    documentation and/or other materials provided with the distribution.
     18      * 3. All advertising materials mentioning features or use of this software
     19      *    must display the following acknowledgement:
     20      *	This product includes software developed by the NetBSD
     21      *	Foundation, Inc. and its contributors.
     22      * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      *    contributors may be used to endorse or promote products derived
     24      *    from this software without specific prior written permission.
     25      *
     26      * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      * POSSIBILITY OF SUCH DAMAGE.
     37      */
     38     #include <sys/cdefs.h>
     39     __KERNEL_RCSID(0, "$NetBSD: tskp.c,v 1.2.10.2 2005/04/29 11:28:08 kent Exp $");
     40     
     41     #include <sys/param.h>
     42     #include <sys/systm.h>
     43     #include <sys/proc.h>
     44     #include <sys/poll.h>
     45     #include <sys/conf.h>
     46     #include <sys/uio.h>
     47     #include <sys/types.h>
     48     #include <sys/kernel.h>
     49     #include <sys/device.h>
     50     #include <sys/callout.h>
     51     #include <sys/select.h>
     52     #include <sys/conf.h>
     53     
     54     #include <machine/bus.h>
     55     #include <machine/autoconf.h>
     56     
     57     #include <dev/wscons/wsconsio.h>
     58     #include <dev/wscons/wskbdvar.h>
     59     #include <dev/wscons/wsksymdef.h>
     60     #include <dev/wscons/wsksymvar.h>
     61     
     62     #include <arm/ep93xx/ep93xxreg.h>
     63     #include <dev/ic/matrixkpvar.h>
     64     #include <evbarm/tsarm/tspldvar.h>
     65     #include <evbarm/tsarm/tsarmreg.h>
     66     
     67     struct tskp_softc {
     68     	struct device sc_dev;
     69     	struct matrixkp_softc sc_mxkp;
     70     	bus_space_tag_t sc_iot;
     71     	bus_space_handle_t sc_gpioh;
     72     };
     73     
     74     #define KC(n) KS_KEYCODE(n)
     75     static const keysym_t mxkp_keydesc_default[] = {
     76     /*  pos				normal			shifted		*/
     77     	KC(0), 			KS_1,
     78     	KC(1), 			KS_2,
     79     	KC(2), 			KS_3,
     80     	KC(3), 			KS_A,
     81     	KC(4), 			KS_4,
     82     	KC(5), 			KS_5,
     83     	KC(6), 			KS_6,
     84     	KC(7), 			KS_B,
     85     	KC(8), 			KS_7,
     86     	KC(9),	 		KS_8,
     87     	KC(10), 		KS_9,
     88     	KC(11), 		KS_C,
     89     	KC(12), 		KS_asterisk,
     90     	KC(13), 		KS_0,
     91     	KC(14), 		KS_numbersign,
     92     	KC(15), 		KS_D,
     93     };
     94     #undef KC
     95     #define KBD_MAP(name, base, map) \
     96     			{ name, base, sizeof(map)/sizeof(keysym_t), map }
     97     const struct wscons_keydesc mxkp_keydesctab[] = {
     98     	KBD_MAP(KB_US,		0,	mxkp_keydesc_default),
     99     	{0, 0, 0, 0}
    100     };
    101     #undef KBD_MAP
    102     
    103     struct wskbd_mapdata mxkp_keymapdata = {
    104     	mxkp_keydesctab,
    105     	KB_US,
    106     };
    107     
    108     static int	tskp_match(struct device *, struct cfdata *, void *);
    109     static void	tskp_attach(struct device *, struct device *, void *);
    110     static void	tskp_scankeys(struct matrixkp_softc *, u_int32_t *);
    111     
    112     CFATTACH_DECL(tskp, sizeof(struct tskp_softc),
    113         tskp_match, tskp_attach, NULL, NULL);
    114     
    115     static int
    116     tskp_match(parent, match, aux)
    117     	struct device *parent;
    118     	struct cfdata *match;
    119     	void *aux;
    120     {
    121     	return 1;
    122     }
    123     
    124     #define GPIO_GET(x)	bus_space_read_1(sc->sc_iot, sc->sc_gpioh, \
    125     	(EP93XX_GPIO_ ## x))
    126     
    127     #define GPIO_SET(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
    128     	(EP93XX_GPIO_ ## x), (y))
    129     
    130     #define GPIO_SETBITS(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
    131     	(EP93XX_GPIO_ ## x), GPIO_GET(x) | (y))
    132     
    133     #define GPIO_CLEARBITS(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
    134     	(EP93XX_GPIO_ ## x), GPIO_GET(x) & (~(y)))
    135     
    136     static void
    137     tskp_attach(parent, self, aux)
    138     	struct device *parent;
    139     	struct device *self;
    140     	void *aux;
    141     {
    142     	struct tskp_softc *sc = (void *)self;
    143     	struct tspld_attach_args *taa = aux;
    144     	struct wskbddev_attach_args wa;
    145     
    146     	sc->sc_iot = taa->ta_iot;
    147     	if (bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_GPIO,
    148     		EP93XX_APB_GPIO_SIZE, 0, &sc->sc_gpioh))
    149     		panic("tskp_attach: couldn't map GPIO registers");
    150     
    151     	sc->sc_mxkp.mxkp_scankeys = tskp_scankeys;
    152     	sc->sc_mxkp.mxkp_event = mxkp_wskbd_event;
    153     	sc->sc_mxkp.mxkp_nkeys = 16; 	/* 4 x 4 matrix keypad */
    154     	sc->sc_mxkp.debounce_stable_ms = 3;
    155     	sc->sc_mxkp.sc_dev = self;
    156     	sc->sc_mxkp.poll_freq = hz;
    157     
    158     	GPIO_SET(PBDDR, 0xff);		/* tristate all lines */
    159     
    160     	printf(": 4x4 matrix keypad, polling at %d hz\n", hz);
    161     
    162     	mxkp_attach(&sc->sc_mxkp);
    163     	wa.console = 0;
    164     	wa.keymap = &mxkp_keymapdata;
    165     	wa.accessops = &mxkp_accessops;
    166     	wa.accesscookie = &sc->sc_mxkp;
    167     	sc->sc_mxkp.sc_wskbddev = config_found(self, &wa, wskbddevprint);
    168     }
    169     
    170     static void
    171     tskp_scankeys(mxkp_sc, keys)
    172     	struct matrixkp_softc *mxkp_sc;
    173     	u_int32_t *keys;
    174     {
    175     	struct tskp_softc *sc = (void *)mxkp_sc->sc_dev;
    176     	u_int32_t pos;
    177     
    178     	for(pos = 0; pos < 4; pos++) {
    179     		GPIO_SET(PBDDR, (1 << pos));
    180     		GPIO_SET(PBDR, ~(1 << pos));
    181     		delay(1);
    182     		keys[0] |= (~(GPIO_GET(PBDR) >> 4) & 0xf) << (4 * pos);
    183     	}
    184     }
    185