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