xref: /netbsd-src/sys/arch/hpcarm/dev/wzero3_keypad.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: wzero3_keypad.c,v 1.4 2012/10/27 17:17:52 chs Exp $	*/
2 
3 /*-
4  * Copyright (C) 2010 NONAKA Kimihiro <nonaka@netbsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: wzero3_keypad.c,v 1.4 2012/10/27 17:17:52 chs Exp $");
30 
31 #include "wzero3lcd.h"
32 #include "opt_wsdisplay_compat.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/kernel.h>
38 #include <sys/callout.h>
39 #include <sys/bus.h>
40 
41 #include <arm/xscale/pxa2x0cpu.h>
42 #include <arm/xscale/pxa2x0var.h>
43 #include <arm/xscale/pxa2x0_gpio.h>
44 
45 #include <machine/bootinfo.h>
46 #include <machine/config_hook.h>
47 #include <machine/platid.h>
48 #include <machine/platid_mask.h>
49 
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wskbdvar.h>
52 #include <dev/wscons/wsksymvar.h>
53 #include <dev/wscons/wsksymdef.h>
54 
55 #ifdef WSDISPLAY_COMPAT_RAWKBD
56 #include <dev/hpc/pckbd_encode.h>
57 #endif
58 
59 #include <arch/hpcarm/dev/wzero3_reg.h>
60 #include <arch/hpcarm/dev/wzero3_sspvar.h>
61 
62 enum {
63 	KD_0,
64 	KD_1,
65 	KD_2,
66 	KD_3,
67 	KD_4,
68 	KD_5,
69 	KD_6,
70 	KD_7,
71 	KD_8,
72 	KD_9,
73 	KD_ASTERISK,
74 	KD_NUMBER,
75 	KD_WINDOWS,
76 	KD_OK,
77 	KD_ONHOOK,
78 	KD_OFFHOOK,
79 	KD_CLEAR,
80 	KD_MOJI,
81 	KD_UP,
82 	KD_DOWN,
83 	KD_LEFT,
84 	KD_RIGHT,
85 	KD_CENTER_BUTTON,
86 	KD_LSOFT,
87 	KD_RSOFT,
88 	KD_NUM,
89 
90 	KD_INVALID = -1
91 };
92 
93 static int ws011sh_keyscan2keydown[32] = {
94 	KD_INVALID,
95 	KD_CLEAR,
96 	KD_INVALID,
97 	KD_OK,
98 	KD_INVALID,
99 	KD_LEFT,
100 	KD_INVALID,
101 	KD_ONHOOK,
102 	KD_INVALID,
103 	KD_UP,
104 	KD_DOWN,
105 	KD_MOJI,
106 	KD_INVALID,
107 	KD_WINDOWS,
108 	KD_INVALID,
109 	KD_RIGHT,
110 	KD_INVALID,
111 	KD_1,
112 	KD_4,
113 	KD_7,
114 	KD_ASTERISK,
115 	KD_2,
116 	KD_5,
117 	KD_8,
118 	KD_0,
119 	KD_CENTER_BUTTON,
120 	KD_INVALID,
121 	KD_3,
122 	KD_6,
123 	KD_9,
124 	KD_NUMBER,
125 	KD_INVALID,
126 };
127 
128 struct wzero3keypad_softc {
129 	device_t sc_dev;
130 
131 	void *sc_ih;
132 	int sc_intr_pin;
133 
134 	uint32_t sc_okeystat;
135 
136 	struct callout sc_poll_ch;
137 	int sc_poll_interval;
138 
139 	device_t sc_wskbddev;
140 
141 #ifdef WSDISPLAY_COMPAT_RAWKBD
142 	int sc_rawkbd;
143 #endif
144 };
145 
146 static int wzero3keypad_match(device_t, cfdata_t, void *);
147 static void wzero3keypad_attach(device_t, device_t, void *);
148 
149 CFATTACH_DECL_NEW(wzero3keypad, sizeof(struct wzero3keypad_softc),
150     wzero3keypad_match, wzero3keypad_attach, NULL, NULL);
151 
152 static int wzero3keypad_wskbd_enable(void *, int);
153 static void wzero3keypad_wskbd_set_leds(void *, int);
154 static int wzero3keypad_wskbd_ioctl(void *, u_long, void *, int, struct lwp *);
155 
156 static const int wzero3keypad_wskbd_keys[] = {
157 	82,	/* KD_0: 0 */
158 	79,	/* KD_1: 1 */
159 	80,	/* KD_2: 2 */
160 	81,	/* KD_3: 3 */
161 	75,	/* KD_4: 4 */
162 	76,	/* KD_5: 5 */
163 	77,	/* KD_6: 6 */
164 	71,	/* KD_7: 7 */
165 	72,	/* KD_8: 8 */
166 	73,	/* KD_9: 9 */
167 	64,	/* KD_ASTERISK: f6 */
168 	65,	/* KD_NUMBER: f7 */
169 	221,	/* KD_WINDOWS: Menu */
170 	61,	/* KD_OK: f3 */
171 	59,	/* KD_ONHOOK: f1 */
172 	60,	/* KD_OFFHOOK: f2 */
173 	62,	/* KD_CLEAR: f4 */
174 	63,	/* KD_MOJI: f5 */
175 	200,	/* KD_UP: Up */
176 	208,	/* KD_DOWN: Down */
177 	203,	/* KD_LEFT: Left */
178 	205,	/* KD_RIGHT: Right */
179 	156,	/* KD_CENTER_BUTTON: KP_Enter */
180 	87,	/* KD_LSOFT: f11 */
181 	88,	/* KD_RSOFT: f12 */
182 };
183 
184 static const keysym_t wzero3keypad_wskbd_keydesc[] = {
185 	KS_KEYCODE(59),		KS_f1,
186 	KS_KEYCODE(60),		KS_f2,
187 	KS_KEYCODE(61),		KS_f3,
188 	KS_KEYCODE(62),		KS_f4,
189 	KS_KEYCODE(63),		KS_f5,
190 	KS_KEYCODE(64),		KS_f6,
191 	KS_KEYCODE(65),		KS_f7,
192 	KS_KEYCODE(71),		KS_7,
193 	KS_KEYCODE(72),		KS_8,
194 	KS_KEYCODE(73),		KS_9,
195 	KS_KEYCODE(75),		KS_4,
196 	KS_KEYCODE(76),		KS_5,
197 	KS_KEYCODE(77),		KS_6,
198 	KS_KEYCODE(79),		KS_1,
199 	KS_KEYCODE(80),		KS_2,
200 	KS_KEYCODE(81),		KS_3,
201 	KS_KEYCODE(82),		KS_0,
202 	KS_KEYCODE(87),		KS_f11,
203 	KS_KEYCODE(88),		KS_f12,
204 	KS_KEYCODE(156),	KS_KP_Enter,
205 	KS_KEYCODE(200),	KS_Up,
206 	KS_KEYCODE(203),	KS_Left,
207 	KS_KEYCODE(205),	KS_Right,
208 	KS_KEYCODE(208),	KS_Down,
209 	KS_KEYCODE(221),	KS_Menu,
210 };
211 
212 static const struct wscons_keydesc wzero3keypad_wskbd_keydesctab[] = {
213 	{ KB_JP, 0,
214 	  sizeof(wzero3keypad_wskbd_keydesc) / sizeof(keysym_t),
215 	  wzero3keypad_wskbd_keydesc
216 	},
217 
218 	{ 0, 0, 0, 0 }
219 };
220 
221 static const struct wskbd_mapdata wzero3keypad_wskbd_keymapdata = {
222 	wzero3keypad_wskbd_keydesctab, KB_JP
223 };
224 
225 static const struct wskbd_accessops wzero3keypad_wskbd_accessops = {
226 	wzero3keypad_wskbd_enable,
227 	wzero3keypad_wskbd_set_leds,
228 	wzero3keypad_wskbd_ioctl,
229 };
230 
231 static int wzero3keypad_intr(void *);
232 static void wzero3keypad_poll(void *);
233 static void wzero3keypad_poll1(struct wzero3keypad_softc *, int);
234 
235 static void wzero3keypad_init(struct wzero3keypad_softc *);
236 static uint32_t wzero3keypad_getkeydown(struct wzero3keypad_softc *, int);
237 
238 static const struct wzero3keypad_model {
239 	platid_mask_t *platid;
240 	int intr_pin;
241 } wzero3keypad_table[] = {
242 #if 0
243 	/* WS007SH */
244 	{
245 		&platid_mask_MACH_SHARP_WZERO3_WS007SH,
246 		-1,	/* XXX */
247 	},
248 #endif
249 	/* WS011SH */
250 	{
251 		&platid_mask_MACH_SHARP_WZERO3_WS011SH,
252 		GPIO_WS011SH_KEYPAD,
253 	},
254 
255 	{ NULL, -1, }
256 };
257 
258 static const struct wzero3keypad_model *
259 wzero3keypad_lookup(void)
260 {
261 	const struct wzero3keypad_model *model;
262 
263 	for (model = wzero3keypad_table; model->platid != NULL; model++) {
264 		if (platid_match(&platid, model->platid)) {
265 			return model;
266 		}
267 	}
268 	return NULL;
269 }
270 
271 static int
272 wzero3keypad_match(device_t parent, cfdata_t cf, void *aux)
273 {
274 
275 	if (strcmp(cf->cf_name, "wzero3keypad") != 0)
276 		return 0;
277 	if (wzero3keypad_lookup() == NULL)
278 		return 0;
279 	return 1;
280 }
281 
282 static void
283 wzero3keypad_attach(device_t parent, device_t self, void *aux)
284 {
285 	struct wzero3keypad_softc *sc = device_private(self);
286 	const struct wzero3keypad_model *model;
287 	struct wskbddev_attach_args wska;
288 #if NWZERO3LCD > 0
289 	extern int screen_rotate;
290 #endif
291 
292 	sc->sc_dev = self;
293 	sc->sc_okeystat = 0;
294 #ifdef WSDISPLAY_COMPAT_RAWKBD
295 	sc->sc_rawkbd = 0;
296 #endif
297 
298 	model = wzero3keypad_lookup();
299 	if (model == NULL) {
300 		aprint_error(": unknown model\n");
301 		return;
302 	}
303 
304 	aprint_normal(": keypad\n");
305 	aprint_naive("\n");
306 
307 	sc->sc_intr_pin = model->intr_pin;
308 
309 	callout_init(&sc->sc_poll_ch, 0);
310 	callout_setfunc(&sc->sc_poll_ch, wzero3keypad_poll, sc);
311 	sc->sc_poll_interval = hz / 32;
312 
313 #if NWZERO3LCD > 0
314 	switch (screen_rotate) {
315 	default:
316 	case 0:
317 		break;
318 
319 	case 270:	/* counter clock-wise */
320 		ws011sh_keyscan2keydown[5] = KD_UP;
321 		ws011sh_keyscan2keydown[9] = KD_RIGHT;
322 		ws011sh_keyscan2keydown[10] = KD_LEFT;
323 		ws011sh_keyscan2keydown[15] = KD_DOWN;
324 		break;
325 	}
326 #endif
327 
328 	/* attach wskbd */
329 	wska.console = 0;
330 	wska.keymap = &wzero3keypad_wskbd_keymapdata;
331 	wska.accessops = &wzero3keypad_wskbd_accessops;
332 	wska.accesscookie = sc;
333 	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &wska,
334 	    wskbddevprint);
335 
336 	/* setup keypad interrupt */
337 	pxa2x0_gpio_set_function(sc->sc_intr_pin, GPIO_IN);
338 	sc->sc_ih = pxa2x0_gpio_intr_establish(sc->sc_intr_pin,
339 	    IST_EDGE_RISING, IPL_TTY, wzero3keypad_intr, sc);
340 	if (sc->sc_ih == NULL) {
341 		aprint_error_dev(sc->sc_dev,
342 		    "couldn't establish keypad interrupt\n");
343 	}
344 
345 	/* init hardware */
346 	wzero3keypad_init(sc);
347 }
348 
349 static int
350 wzero3keypad_wskbd_enable(void *arg, int onoff)
351 {
352 
353 	return 0;
354 }
355 
356 static void
357 wzero3keypad_wskbd_set_leds(void *arg, int leds)
358 {
359 
360 	/* Nothing to do */
361 }
362 
363 static int
364 wzero3keypad_wskbd_ioctl(void *arg, u_long cmd, void *data, int flags,
365     struct lwp *l)
366 {
367 #ifdef WSDISPLAY_COMPAT_RAWKBD
368 	struct wzero3keypad_softc *sc = (struct wzero3keypad_softc *)arg;
369 #endif
370 
371 	switch (cmd) {
372 	case WSKBDIO_GTYPE:
373 		*(int *)data = WSKBD_TYPE_HPC_KBD;
374 		return 0;
375 	case WSKBDIO_SETLEDS:
376 		return 0;
377 	case WSKBDIO_GETLEDS:
378 		*(int *)data = 0;
379 		return 0;
380 #ifdef WSDISPLAY_COMPAT_RAWKBD
381 	case WSKBDIO_SETMODE:
382 		sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
383 		return 0;
384 #endif
385 	}
386 
387 	return EPASSTHROUGH;
388 }
389 
390 static int
391 wzero3keypad_intr(void *arg)
392 {
393 	struct wzero3keypad_softc *sc = (struct wzero3keypad_softc *)arg;
394 
395 	pxa2x0_gpio_clear_intr(sc->sc_intr_pin);
396 
397 	wzero3keypad_poll1(sc, 0);
398 
399 	callout_schedule(&sc->sc_poll_ch, sc->sc_poll_interval);
400 
401 	return 1;
402 }
403 
404 static void
405 wzero3keypad_poll(void *v)
406 {
407 	struct wzero3keypad_softc *sc = (struct wzero3keypad_softc *)v;
408 
409 	wzero3keypad_poll1(sc, 1);
410 
411 	callout_stop(&sc->sc_poll_ch);
412 }
413 
414 static void
415 wzero3keypad_poll1(struct wzero3keypad_softc *sc, int doscan)
416 {
417 	uint32_t keydown;
418 	uint32_t diff;
419 	int i;
420 	int s;
421 
422 	s = spltty();
423 
424 	keydown = wzero3keypad_getkeydown(sc, doscan);
425 	diff = keydown ^ sc->sc_okeystat;
426 	if (diff == 0)
427 		goto out;
428 
429 	for (i = 0; i < KD_NUM; i++) {
430 		if (diff & (1 << i)) {
431 			int state = keydown & (1 << i);
432 			int type = state ? WSCONS_EVENT_KEY_DOWN :
433 			    WSCONS_EVENT_KEY_UP;
434 			int key = wzero3keypad_wskbd_keys[i];
435 #ifdef WSDISPLAY_COMPAT_RAWKBD
436 			if (sc->sc_rawkbd) {
437 				int n;
438 				u_char data[16];
439 
440 				n = pckbd_encode(type, key, data);
441 				wskbd_rawinput(sc->sc_wskbddev, data, n);
442 			} else
443 #endif
444 				wskbd_input(sc->sc_wskbddev, type, key);
445 		}
446 	}
447 	sc->sc_okeystat = keydown;
448 
449 out:
450 	splx(s);
451 }
452 
453 /*----------------------------------------------------------------------------
454  * AK4184 keypad controller for WS011SH
455  */
456 /* ak4184 command register */
457 #define	AKMCTRL_WR_SH	7
458 #define	AKMCTRL_PAGE_SH	6
459 #define	AKMCTRL_ADDR_SH	0
460 #define	AKMCTRL_WRITE	(0<<AKMCTRL_WR_SH)
461 #define	AKMCTRL_READ	(1<<AKMCTRL_WR_SH)
462 #define	AKMCTRL_DATA	(0<<AKMCTRL_PAGE_SH)
463 #define	AKMCTRL_CTRL	(1<<AKMCTRL_PAGE_SH)
464 
465 static void
466 wzero3keypad_init(struct wzero3keypad_softc *sc)
467 {
468 	int s;
469 
470 	s = spltty();
471 
472 #if 0
473 	/*
474 	 * - key interrupt enable
475 	 * - key touch scan
476 	 * - debounce time: 1ms
477 	 * - wait 100us for debounce time
478 	 */
479 	(void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD,
480 	    AKMCTRL_WRITE | AKMCTRL_CTRL | (0<<AKMCTRL_ADDR_SH), 0);
481 #endif
482 
483 	/* unmask all keys & columns */
484 	(void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD,
485 	    AKMCTRL_WRITE | AKMCTRL_CTRL | (1<<AKMCTRL_ADDR_SH), 0);
486 	(void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD,
487 	    AKMCTRL_WRITE | AKMCTRL_CTRL | (2<<AKMCTRL_ADDR_SH), 0);
488 	(void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD,
489 	    AKMCTRL_WRITE | AKMCTRL_CTRL | (3<<AKMCTRL_ADDR_SH), 0);
490 
491 	/* Enable keypad interrupt (kpdata dummy read) */
492 	(void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD,
493 	    AKMCTRL_READ | AKMCTRL_DATA | (1<<AKMCTRL_ADDR_SH), 0);
494 
495 	splx(s);
496 }
497 
498 static uint32_t
499 wzero3keypad_getkeydown(struct wzero3keypad_softc *sc, int doscan)
500 {
501 	uint32_t keydown = 0;
502 	uint16_t status;
503 	uint16_t kpdata;
504 	int timo;
505 
506 	if (doscan) {
507 		/* host scan */
508 		(void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD,
509 		    AKMCTRL_WRITE | AKMCTRL_CTRL | (4<<AKMCTRL_ADDR_SH), 0);
510 		delay(100);
511 	}
512 
513 	timo = 1000;
514 	do {
515 		status = wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD,
516 		    AKMCTRL_READ | AKMCTRL_CTRL | (0<<AKMCTRL_ADDR_SH), 0);
517 	} while ((status & 0xc000) == 0 && timo-- > 0);
518 
519 	kpdata = wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD,
520 	    AKMCTRL_READ | AKMCTRL_DATA | (1<<AKMCTRL_ADDR_SH), 0);
521 	if ((status & 0xc000) == 0xc000) {
522 		if (!(kpdata & 0x8000)) {
523 			int i;
524 
525 			for (i = 0; i < 3; i++) {
526 				int key, bits;
527 
528 				key = kpdata & 0x1f;
529 				if (key == 0)
530 					break;
531 				bits = ws011sh_keyscan2keydown[key];
532 				if (bits != KD_INVALID)
533 					keydown |= 1 << bits;
534 				kpdata >>= 5;
535 			}
536 		}
537 	}
538 
539 	return keydown;
540 }
541