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