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