tskp.c revision 1.10 1 1.10 skrll /* $NetBSD: tskp.c,v 1.10 2012/11/12 18:00:40 skrll 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.10 skrll __KERNEL_RCSID(0, "$NetBSD: tskp.c,v 1.10 2012/11/12 18:00:40 skrll 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 matrixkp_softc sc_mxkp;
62 1.1 joff bus_space_tag_t sc_iot;
63 1.1 joff bus_space_handle_t sc_gpioh;
64 1.1 joff };
65 1.1 joff
66 1.1 joff #define KC(n) KS_KEYCODE(n)
67 1.1 joff static const keysym_t mxkp_keydesc_default[] = {
68 1.1 joff /* pos normal shifted */
69 1.2 joff KC(0), KS_1,
70 1.2 joff KC(1), KS_2,
71 1.2 joff KC(2), KS_3,
72 1.2 joff KC(3), KS_A,
73 1.2 joff KC(4), KS_4,
74 1.2 joff KC(5), KS_5,
75 1.2 joff KC(6), KS_6,
76 1.2 joff KC(7), KS_B,
77 1.2 joff KC(8), KS_7,
78 1.2 joff KC(9), KS_8,
79 1.2 joff KC(10), KS_9,
80 1.2 joff KC(11), KS_C,
81 1.2 joff KC(12), KS_asterisk,
82 1.2 joff KC(13), KS_0,
83 1.2 joff KC(14), KS_numbersign,
84 1.2 joff KC(15), KS_D,
85 1.1 joff };
86 1.1 joff #undef KC
87 1.1 joff #define KBD_MAP(name, base, map) \
88 1.1 joff { name, base, sizeof(map)/sizeof(keysym_t), map }
89 1.1 joff const struct wscons_keydesc mxkp_keydesctab[] = {
90 1.1 joff KBD_MAP(KB_US, 0, mxkp_keydesc_default),
91 1.1 joff {0, 0, 0, 0}
92 1.1 joff };
93 1.1 joff #undef KBD_MAP
94 1.1 joff
95 1.1 joff struct wskbd_mapdata mxkp_keymapdata = {
96 1.1 joff mxkp_keydesctab,
97 1.1 joff KB_US,
98 1.1 joff };
99 1.1 joff
100 1.9 chs static int tskp_match(device_t, cfdata_t, void *);
101 1.9 chs static void tskp_attach(device_t, device_t, void *);
102 1.10 skrll static void tskp_scankeys(struct matrixkp_softc *, uint32_t *);
103 1.1 joff
104 1.9 chs CFATTACH_DECL_NEW(tskp, sizeof(struct tskp_softc),
105 1.1 joff tskp_match, tskp_attach, NULL, NULL);
106 1.1 joff
107 1.1 joff static int
108 1.9 chs tskp_match(device_t parent, cfdata_t match, void *aux)
109 1.1 joff {
110 1.1 joff return 1;
111 1.1 joff }
112 1.1 joff
113 1.1 joff #define GPIO_GET(x) bus_space_read_1(sc->sc_iot, sc->sc_gpioh, \
114 1.1 joff (EP93XX_GPIO_ ## x))
115 1.1 joff
116 1.1 joff #define GPIO_SET(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
117 1.1 joff (EP93XX_GPIO_ ## x), (y))
118 1.1 joff
119 1.1 joff #define GPIO_SETBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
120 1.1 joff (EP93XX_GPIO_ ## x), GPIO_GET(x) | (y))
121 1.1 joff
122 1.1 joff #define GPIO_CLEARBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
123 1.1 joff (EP93XX_GPIO_ ## x), GPIO_GET(x) & (~(y)))
124 1.1 joff
125 1.1 joff static void
126 1.9 chs tskp_attach(device_t parent, device_t self, void *aux)
127 1.1 joff {
128 1.9 chs struct tskp_softc *sc = device_private(self);
129 1.1 joff struct tspld_attach_args *taa = aux;
130 1.1 joff struct wskbddev_attach_args wa;
131 1.1 joff
132 1.1 joff sc->sc_iot = taa->ta_iot;
133 1.1 joff if (bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_GPIO,
134 1.1 joff EP93XX_APB_GPIO_SIZE, 0, &sc->sc_gpioh))
135 1.1 joff panic("tskp_attach: couldn't map GPIO registers");
136 1.1 joff
137 1.1 joff sc->sc_mxkp.mxkp_scankeys = tskp_scankeys;
138 1.1 joff sc->sc_mxkp.mxkp_event = mxkp_wskbd_event;
139 1.1 joff sc->sc_mxkp.mxkp_nkeys = 16; /* 4 x 4 matrix keypad */
140 1.1 joff sc->sc_mxkp.debounce_stable_ms = 3;
141 1.1 joff sc->sc_mxkp.sc_dev = self;
142 1.1 joff sc->sc_mxkp.poll_freq = hz;
143 1.1 joff
144 1.1 joff GPIO_SET(PBDDR, 0xff); /* tristate all lines */
145 1.1 joff
146 1.1 joff printf(": 4x4 matrix keypad, polling at %d hz\n", hz);
147 1.1 joff
148 1.1 joff mxkp_attach(&sc->sc_mxkp);
149 1.1 joff wa.console = 0;
150 1.1 joff wa.keymap = &mxkp_keymapdata;
151 1.1 joff wa.accessops = &mxkp_accessops;
152 1.1 joff wa.accesscookie = &sc->sc_mxkp;
153 1.1 joff sc->sc_mxkp.sc_wskbddev = config_found(self, &wa, wskbddevprint);
154 1.1 joff }
155 1.1 joff
156 1.1 joff static void
157 1.10 skrll tskp_scankeys(struct matrixkp_softc *mxkp_sc, uint32_t *keys)
158 1.1 joff {
159 1.9 chs struct tskp_softc *sc = device_private(mxkp_sc->sc_dev);
160 1.10 skrll uint32_t pos;
161 1.1 joff
162 1.1 joff for(pos = 0; pos < 4; pos++) {
163 1.1 joff GPIO_SET(PBDDR, (1 << pos));
164 1.1 joff GPIO_SET(PBDR, ~(1 << pos));
165 1.1 joff delay(1);
166 1.1 joff keys[0] |= (~(GPIO_GET(PBDR) >> 4) & 0xf) << (4 * pos);
167 1.1 joff }
168 1.1 joff }
169