2 * Copyright (c) 2005 Jesse Off. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * The matrix keypad is a primitive type of keying device
27 * commonly used in systems as a small, cheap, easy-to-build and rugged
28 * way to get user input in a variety of embedded environments. This
29 * driver can work for any size of keypad. A one key keypad (aka
30 * button) can also be used. The theory of operation is described
33 * 1) The keypad is connected to the NetBSD embedded system
34 * with digital I/O (DIO) pins connected to each column of
35 * the keypad and also to each row of the keypad.
37 * 2) When a button is pressed, a short is made between a
38 * column line and the intersecting row line.
40 * 3) Software is responsible to poll each row/column individually
41 * and also to debounce any key presses.
43 * To correctly wire up such a thing requires the input DIO
44 * lines to have pull-up resistors, otherwise an input may be read as a random
45 * value if not currently being shorted by a button press.
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: matrixkp_subr.c,v 1.6 2007/07/09 21:00:37 ad Exp $");
51 #include <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/callout.h>
56 #include <sys/kernel.h>
57 #include <sys/types.h>
59 #include <machine/autoconf.h>
63 #include <dev/wscons/wsconsio.h>
64 #include <dev/wscons/wskbdvar.h>
65 #include <dev/wscons/wsksymdef.h>
66 #include <dev/wscons/wsksymvar.h>
68 #include <dev/ic/matrixkpvar.h>
70 #define TV_ELAPSED_US(x, y) (((x).tv_sec - (y).tv_sec) * 1000000 + \
71 ((x).tv_usec - (y).tv_usec))
73 const struct wskbd_accessops mxkp_accessops
= {
80 mxkp_attach(struct matrixkp_softc
*sc
)
84 callout_init(&sc
->sc_callout
, 0);
85 callout_setfunc(&sc
->sc_callout
, mxkp_poll
, sc
);
86 if (sc
->poll_freq
> hz
|| sc
->poll_freq
== 0)
89 if (sc
->debounce_stable_ms
== 0)
90 sc
->sc_flags
|= MXKP_NODEBOUNCE
;
91 if (sc
->mxkp_event
== NULL
)
92 sc
->mxkp_event
= mxkp_wskbd_event
;
93 FOR_KEYS(i
, sc
->mxkp_pressed
[i
] = 0);
99 struct matrixkp_softc
*sc
= (struct matrixkp_softc
*)arg
;
100 u_int32_t i
, anychanged
;
101 u_int32_t scanned
[(MAXNKEYS
+ 31) / 32];
102 u_int32_t changed
[(MAXNKEYS
+ 31) / 32];
103 u_int32_t set
[(MAXNKEYS
+ 31) / 32];
104 u_int32_t cleared
[(MAXNKEYS
+ 31) / 32];
108 FOR_KEYS(i
, scanned
[i
] = 0);
109 sc
->mxkp_scankeys(sc
, scanned
);
110 FOR_KEYS(i
, changed
[i
] = sc
->mxkp_pressed
[i
] ^ scanned
[i
]);
111 FOR_KEYS(i
, anychanged
|= changed
[i
]);
113 if (!(sc
->sc_flags
& MXKP_NODEBOUNCE
) && anychanged
) {
114 mxkp_debounce(sc
, changed
, scanned
);
116 FOR_KEYS(i
, changed
[i
] &= sc
->mxkp_pressed
[i
] ^ scanned
[i
]);
117 FOR_KEYS(i
, anychanged
|= changed
[i
]);
120 FOR_KEYS(i
, set
[i
] = changed
[i
] & scanned
[i
]);
121 FOR_KEYS(i
, cleared
[i
] = changed
[i
] & sc
->mxkp_pressed
[i
]);
122 sc
->mxkp_event(sc
, set
, cleared
);
123 FOR_KEYS(i
, sc
->mxkp_pressed
[i
] &= ~cleared
[i
]);
124 FOR_KEYS(i
, sc
->mxkp_pressed
[i
] |= set
[i
]);
128 callout_schedule(&sc
->sc_callout
, hz
/ sc
->poll_freq
);
132 * debounce will return when masked keys have been stable
133 * for sc->debounce_stable_ms
136 mxkp_debounce(struct matrixkp_softc
*sc
, u_int32_t
*mask
, u_int32_t
*scan
) {
137 struct timeval verystart
, start
, now
;
138 u_int32_t last_val
[(MAXNKEYS
+ 31) / 32];
141 FOR_KEYS(i
, last_val
[i
] = scan
[i
]);
142 microtime(&verystart
);
145 FOR_KEYS(i
, scan
[i
] = 0);
146 sc
->mxkp_scankeys(sc
, scan
);
149 FOR_KEYS(i
, anyset
|= (scan
[i
] ^ last_val
[i
]) & mask
[i
]);
150 if (anyset
) /* bounce detected */
152 FOR_KEYS(i
, last_val
[i
] = scan
[i
]);
153 } while (TV_ELAPSED_US(now
, start
) <= (sc
->debounce_stable_ms
* 1000));
157 mxkp_wskbd_event(struct matrixkp_softc
*sc
, u_int32_t
*on
, u_int32_t
*off
)
161 for(i
= 0; i
< sc
->mxkp_nkeys
; i
++) {
162 if (off
[i
/ 32] & (1 << (i
% 32))) {
163 wskbd_input(sc
->sc_wskbddev
, WSCONS_EVENT_KEY_UP
, i
);
166 for(i
= 0; i
< sc
->mxkp_nkeys
; i
++) {
167 if (on
[i
/ 32] & (1 << (i
% 32))) {
168 wskbd_input(sc
->sc_wskbddev
, WSCONS_EVENT_KEY_DOWN
, i
);
174 mxkp_enable(void *v
, int on
)
176 struct matrixkp_softc
*sc
= v
;
183 callout_schedule(&sc
->sc_callout
, hz
/ sc
->poll_freq
);
192 mxkp_set_leds(void *v
, int leds
)
197 mxkp_ioctl(void *v
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
201 *(int *)data
= WSKBD_TYPE_MATRIXKP
;
203 case WSKBDIO_SETLEDS
:
205 case WSKBDIO_GETLEDS
:
208 case WSKBDIO_COMPLEXBELL
: