xref: /netbsd-src/sys/arch/evbarm/tsarm/tskp.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /* $NetBSD: tskp.c,v 1.7 2009/03/14 15:36:05 dsl 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.7 2009/03/14 15:36:05 dsl 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(struct device *parent, struct cfdata *match, void *aux)
110 {
111 	return 1;
112 }
113 
114 #define GPIO_GET(x)	bus_space_read_1(sc->sc_iot, sc->sc_gpioh, \
115 	(EP93XX_GPIO_ ## x))
116 
117 #define GPIO_SET(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
118 	(EP93XX_GPIO_ ## x), (y))
119 
120 #define GPIO_SETBITS(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
121 	(EP93XX_GPIO_ ## x), GPIO_GET(x) | (y))
122 
123 #define GPIO_CLEARBITS(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
124 	(EP93XX_GPIO_ ## x), GPIO_GET(x) & (~(y)))
125 
126 static void
127 tskp_attach(struct device *parent, struct device *self, void *aux)
128 {
129 	struct tskp_softc *sc = (void *)self;
130 	struct tspld_attach_args *taa = aux;
131 	struct wskbddev_attach_args wa;
132 
133 	sc->sc_iot = taa->ta_iot;
134 	if (bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_GPIO,
135 		EP93XX_APB_GPIO_SIZE, 0, &sc->sc_gpioh))
136 		panic("tskp_attach: couldn't map GPIO registers");
137 
138 	sc->sc_mxkp.mxkp_scankeys = tskp_scankeys;
139 	sc->sc_mxkp.mxkp_event = mxkp_wskbd_event;
140 	sc->sc_mxkp.mxkp_nkeys = 16; 	/* 4 x 4 matrix keypad */
141 	sc->sc_mxkp.debounce_stable_ms = 3;
142 	sc->sc_mxkp.sc_dev = self;
143 	sc->sc_mxkp.poll_freq = hz;
144 
145 	GPIO_SET(PBDDR, 0xff);		/* tristate all lines */
146 
147 	printf(": 4x4 matrix keypad, polling at %d hz\n", hz);
148 
149 	mxkp_attach(&sc->sc_mxkp);
150 	wa.console = 0;
151 	wa.keymap = &mxkp_keymapdata;
152 	wa.accessops = &mxkp_accessops;
153 	wa.accesscookie = &sc->sc_mxkp;
154 	sc->sc_mxkp.sc_wskbddev = config_found(self, &wa, wskbddevprint);
155 }
156 
157 static void
158 tskp_scankeys(struct matrixkp_softc *mxkp_sc, u_int32_t *keys)
159 {
160 	struct tskp_softc *sc = (void *)mxkp_sc->sc_dev;
161 	u_int32_t pos;
162 
163 	for(pos = 0; pos < 4; pos++) {
164 		GPIO_SET(PBDDR, (1 << pos));
165 		GPIO_SET(PBDR, ~(1 << pos));
166 		delay(1);
167 		keys[0] |= (~(GPIO_GET(PBDR) >> 4) & 0xf) << (4 * pos);
168 	}
169 }
170