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