xref: /netbsd-src/sys/arch/hpcarm/dev/wzero3_kbd.c (revision bbde328be4e75ea9ad02e9715ea13ca54b797ada)
1 /*	$NetBSD: wzero3_kbd.c,v 1.3 2010/04/24 21:51:56 nonaka Exp $	*/
2 
3 /*
4  * Copyright (c) 2008, 2009 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 REGENTS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: wzero3_kbd.c,v 1.3 2010/04/24 21:51:56 nonaka Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/callout.h>
38 
39 #include <dev/sysmon/sysmonvar.h>
40 #include <dev/sysmon/sysmon_taskq.h>
41 
42 #include <arm/xscale/pxa2x0cpu.h>
43 #include <arm/xscale/pxa2x0var.h>
44 #include <arm/xscale/pxa2x0_gpio.h>
45 
46 #include <machine/bus.h>
47 #include <machine/bootinfo.h>
48 #include <machine/config_hook.h>
49 #include <machine/platid.h>
50 #include <machine/platid_mask.h>
51 
52 #include <dev/hpc/hpckbdvar.h>
53 
54 #include <arch/hpcarm/dev/wzero3_reg.h>
55 
56 #ifdef DEBUG
57 #define DPRINTF(arg)	printf arg
58 #else
59 #define DPRINTF(arg)	/* nothing */
60 #endif
61 
62 #define	CSR_READ1(r)	bus_space_read_1(sc->sc_iot, sc->sc_ioh, (r))
63 #define	CSR_WRITE1(r,v)	bus_space_write_1(sc->sc_iot, sc->sc_ioh, (r), (v))
64 #define	CSR_READ2(r)	bus_space_read_2(sc->sc_iot, sc->sc_ioh, (r))
65 #define	CSR_WRITE2(r,v)	bus_space_write_2(sc->sc_iot, sc->sc_ioh, (r), (v))
66 #define	CSR_READ4(r)	bus_space_read_4(sc->sc_iot, sc->sc_ioh, (r))
67 #define	CSR_WRITE4(r,v)	bus_space_write_4(sc->sc_iot, sc->sc_ioh, (r), (v))
68 
69 /* register */
70 #define	KBDCOL_L	(0x00)	/* Write */
71 #define	KBDCOL_U	(0x04)	/* Write */
72 #define	KBDCHARGE	(0x08)	/* Write */
73 #define	KBDDATA		(0x08)	/* Read */
74 #define	REGMAPSIZE	0x0c
75 
76 #define	KEYWAIT		20	/* us */
77 
78 #define	WS003SH_NCOLUMN	12
79 #define	WS003SH_NROW	7
80 
81 struct wzero3kbd_softc {
82 	device_t sc_dev;
83 
84 	bus_space_tag_t sc_iot;
85 	bus_space_handle_t sc_ioh;
86 
87 	int sc_ncolumn;
88 	int sc_nrow;
89 	uint8_t *sc_okeystat;
90 	uint8_t *sc_keystat;
91 
92 	void *sc_key_ih;
93 	void *sc_power_ih;
94 	void *sc_reset_ih;
95 
96 	int sc_key_pin;
97 	int sc_power_pin;
98 	int sc_reset_pin;
99 
100 	struct hpckbd_ic_if sc_if;
101 	struct hpckbd_if *sc_hpckbd;
102 
103 	struct sysmon_pswitch sc_smpsw; /* for reset key */
104 
105 	int sc_enabled;
106 
107 	/* polling stuff */
108 	struct callout sc_keyscan_ch;
109 	int sc_interval;
110 #define	KEY_INTERVAL	50	/* ms */
111 
112 #if defined(KEYTEST) || defined(KEYTEST2) || defined(KEYTEST3) || defined(KEYTEST4) || defined(KEYTEST5)
113 	void *sc_test_ih;
114 	int sc_test_pin;
115 	int sc_nouse_pin;
116 	int sc_nouse_pin2;
117 	int sc_nouse_pin3;
118 	int sc_bit;
119 #endif
120 };
121 
122 static int wzero3kbd_match(device_t, cfdata_t, void *);
123 static void wzero3kbd_attach(device_t, device_t, void *);
124 
125 CFATTACH_DECL_NEW(wzero3kbd, sizeof(struct wzero3kbd_softc),
126     wzero3kbd_match, wzero3kbd_attach, NULL, NULL);
127 
128 static int wzero3kbd_intr(void *arg);
129 #if defined(KEYTEST)
130 static int wzero3kbd_intr2(void *arg);
131 #endif
132 #if defined(KEYTEST3)
133 static int wzero3kbd_intr3(void *arg);
134 #endif
135 static void wzero3kbd_tick(void *arg);
136 static int wzero3kbd_power_intr(void *arg);
137 static int wzero3kbd_reset_intr(void *arg);
138 static int wzero3kbd_input_establish(void *arg, struct hpckbd_if *kbdif);
139 static void wzero3kbd_sysmon_reset_event(void *arg);
140 static int wzero3kbd_poll(void *arg);
141 static int wzero3kbd_poll1(void *arg);
142 
143 /*
144  * WS003SH keyscan map
145 	col#0	col#1	col#2	col#3	col#4	col#5	col#6	col#7	col#8	col#9	col#10	col#11
146 row#0:	CTRL	1	3	5	6	7	9	0	BS	(none)	ROTATE	CAMERA
147 row#1:	(none)	2	4	r	y	8	i	o	p	(none)	VOL-	VOL+
148 row#2:	TAB	q	e	t	g	u	j	k	(none)	(none)	(none)	(none)
149 row#3:	(none)	w	s	f	v	h	m	l	(none)	(none)	SHIFT	(none)
150 row#4:	CALL	a	d	c	b	n	.	(none)	ENTER	(none)	WIN	(none)
151 row#5:	MAIL	z	x	-	SPACE	/	(none)	UP	(none)	(none)	LSOFT	FN
152 row#6:	IE	MOJI	(none)	OK	ACTION	,	LEFT	DOWN	RIGHT	(none)	RSOFT	(none)
153 */
154 
155 /*
156  * WS011SH keyscan map
157 	col#0	col#1	col#2	col#3	col#4	col#5	col#6	col#7	col#8	col#9	col#10	col#11
158 row#0	Ctrl	(none)	(none)	(none)	(none)	(none)	(none)	(none)	Del	(none)	ROTATE	(none)
159 row#1	(none)	(none)	(none)	R	Y	(none)	I	O	(none)	(none)	(none)	(none)
160 row#2	Tab	Q	E	T	G	U	J	K	(none)	(none)	(none)	(none)
161 row#3	(none)	W	S	F	V	H	M	L	(none)	(none)	Shift	(none)
162 row#4	(none)	A	D	C	B	N	.	(none)	Enter	(none)	(none)	(none)
163 row#5	(none)	Z	X	-	Space	/	(none)	UP	(none)	(none)	(none)	(none)
164 row#6	(none)	MOJI	HAN/ZEN	OK	(none)	,	LEFT	DOWN	RIGHT	(none)	Fn	(none)
165 */
166 
167 static const struct wzero3kbd_model {
168 	platid_mask_t *platid;
169 	int key_pin;
170 	int power_pin;
171 	int reset_pin;
172 	int ncolumn;
173 	int nrow;
174 } wzero3kbd_table[] = {
175 	/* WS003SH */
176 	{
177 		&platid_mask_MACH_SHARP_WZERO3_WS003SH,
178 		-1,	/* XXX */
179 		GPIO_WS003SH_POWER_BUTTON,
180 		-1,	/* None */
181 		WS003SH_NCOLUMN,
182 		WS003SH_NROW,
183 	},
184 	/* WS004SH */
185 	{
186 		&platid_mask_MACH_SHARP_WZERO3_WS004SH,
187 		-1,	/* XXX */
188 		GPIO_WS003SH_POWER_BUTTON,
189 		-1,	/* None */
190 		WS003SH_NCOLUMN,
191 		WS003SH_NROW,
192 	},
193 	/* WS007SH */
194 	{
195 		&platid_mask_MACH_SHARP_WZERO3_WS007SH,
196 		-1,	/* XXX */
197 		GPIO_WS007SH_POWER_BUTTON,
198 		GPIO_WS007SH_RESET_BUTTON,
199 		WS003SH_NCOLUMN,
200 		WS003SH_NROW,
201 	},
202 	/* WS011SH */
203 	{
204 		&platid_mask_MACH_SHARP_WZERO3_WS011SH,
205 		-1,	/* XXX */
206 		GPIO_WS011SH_POWER_BUTTON,
207 		GPIO_WS011SH_RESET_BUTTON,
208 		WS003SH_NCOLUMN,
209 		WS003SH_NROW,
210 	},
211 	/* WS020SH */
212 	{
213 		&platid_mask_MACH_SHARP_WZERO3_WS020SH,
214 		-1,	/* XXX */
215 		-1,	/* XXX */
216 		-1,	/* XXX */
217 		WS003SH_NCOLUMN,
218 		WS003SH_NROW,
219 	},
220 
221 	{ NULL, -1, -1, -1, 0, 0, }
222 };
223 
224 static const struct wzero3kbd_model *
225 wzero3kbd_lookup(void)
226 {
227 	const struct wzero3kbd_model *model;
228 
229 	for (model = wzero3kbd_table; model->platid != NULL; model++) {
230 		if (platid_match(&platid, model->platid)) {
231 			return model;
232 		}
233 	}
234 	return NULL;
235 }
236 
237 static int
238 wzero3kbd_match(struct device *parent, struct cfdata *cf, void *aux)
239 {
240 
241 	if (strcmp(cf->cf_name, "wzero3kbd") != 0)
242 		return 0;
243 	if (wzero3kbd_lookup() == NULL)
244 		return 0;
245 	return 1;
246 }
247 
248 static void
249 wzero3kbd_attach(struct device *parent, struct device *self, void *aux)
250 {
251 	struct wzero3kbd_softc *sc = device_private(self);
252 	struct pxaip_attach_args *pxa = (struct pxaip_attach_args *)aux;
253 	struct hpckbd_attach_args haa;
254 	const struct wzero3kbd_model *model;
255 
256 	sc->sc_dev = self;
257 
258 	model = wzero3kbd_lookup();
259 	if (model == NULL) {
260 		aprint_error(": unknown model\n");
261 		return;
262 	}
263 
264 	aprint_normal(": keyboard\n");
265 	aprint_naive("\n");
266 
267 	sc->sc_key_pin = model->key_pin;
268 	sc->sc_power_pin = model->power_pin;
269 	sc->sc_reset_pin = model->reset_pin;
270 	sc->sc_ncolumn = model->ncolumn;
271 	sc->sc_nrow = model->nrow;
272 
273 	sc->sc_iot = pxa->pxa_iot;
274 	if (bus_space_map(sc->sc_iot, PXA2X0_CS2_START, REGMAPSIZE, 0,
275 	    &sc->sc_ioh)) {
276 		aprint_error_dev(self, "couldn't map registers.\n");
277 		return;
278 	}
279 
280 	sc->sc_okeystat = malloc(sc->sc_nrow * sc->sc_ncolumn, M_DEVBUF,
281 	    M_NOWAIT | M_ZERO);
282 	sc->sc_keystat = malloc(sc->sc_nrow * sc->sc_ncolumn, M_DEVBUF,
283 	    M_NOWAIT | M_ZERO);
284 	if (sc->sc_okeystat == NULL || sc->sc_keystat == NULL) {
285 		aprint_error_dev(self, "couldn't alloc memory.\n");
286 		if (sc->sc_okeystat)
287 			free(sc->sc_okeystat, M_DEVBUF);
288 		if (sc->sc_keystat)
289 			free(sc->sc_keystat, M_DEVBUF);
290 		return;
291 	}
292 
293 	sc->sc_if.hii_ctx = sc;
294 	sc->sc_if.hii_establish = wzero3kbd_input_establish;
295 	sc->sc_if.hii_poll = wzero3kbd_poll;
296 
297 	/* Attach console if not using serial. */
298 	if (!(bootinfo->bi_cnuse & BI_CNUSE_SERIAL))
299 		hpckbd_cnattach(&sc->sc_if);
300 
301 	/* Install interrupt handler. */
302 	if (sc->sc_key_pin >= 0) {
303 		pxa2x0_gpio_set_function(sc->sc_key_pin, GPIO_IN);
304 		sc->sc_key_ih = pxa2x0_gpio_intr_establish(sc->sc_key_pin,
305 		    IST_EDGE_BOTH, IPL_TTY, wzero3kbd_intr, sc);
306 		if (sc->sc_key_ih == NULL) {
307 			aprint_error_dev(sc->sc_dev,
308 			    "couldn't establish key interrupt\n");
309 		}
310 	} else {
311 		sc->sc_interval = KEY_INTERVAL / (1000 / hz);
312 		if (sc->sc_interval < 1)
313 			sc->sc_interval = 1;
314 		callout_init(&sc->sc_keyscan_ch, 0);
315 		callout_reset(&sc->sc_keyscan_ch, sc->sc_interval,
316 		    wzero3kbd_tick, sc);
317 	}
318 
319 	/* power key */
320 	if (sc->sc_power_pin >= 0) {
321 		pxa2x0_gpio_set_function(sc->sc_power_pin, GPIO_IN);
322 		sc->sc_power_ih = pxa2x0_gpio_intr_establish(
323 		    sc->sc_power_pin, IST_EDGE_BOTH, IPL_TTY,
324 		    wzero3kbd_power_intr, sc);
325 		if (sc->sc_power_ih == NULL) {
326 			aprint_error_dev(sc->sc_dev,
327 			    "couldn't establish power key interrupt\n");
328 		}
329 	}
330 
331 	/* reset button */
332 	if (sc->sc_reset_pin >= 0) {
333 		pxa2x0_gpio_set_function(sc->sc_reset_pin, GPIO_IN);
334 		sc->sc_reset_ih = pxa2x0_gpio_intr_establish(
335 		    sc->sc_reset_pin, IST_EDGE_BOTH, IPL_TTY,
336 		    wzero3kbd_reset_intr, sc);
337 		if (sc->sc_reset_ih == NULL) {
338 			aprint_error_dev(sc->sc_dev,
339 			    "couldn't establish reset key interrupt\n");
340 		}
341 
342 		sc->sc_smpsw.smpsw_name = device_xname(self);
343 		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_RESET;
344 		if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
345 			aprint_error_dev(sc->sc_dev,
346 			    "unable to register reset event handler\n");
347 		}
348 	}
349 
350 	/* Attach hpckbd. */
351 	haa.haa_ic = &sc->sc_if;
352 	config_found(self, &haa, hpckbd_print);
353 
354 #if defined(KEYTEST) || defined(KEYTEST2) || defined(KEYTEST3) || defined(KEYTEST4) || defined(KEYTEST5)
355 	sc->sc_test_ih = NULL;
356 	sc->sc_test_pin = -1;
357 	sc->sc_nouse_pin = -1;
358 	sc->sc_nouse_pin2 = -1;
359 	sc->sc_nouse_pin3 = -1;
360 	sc->sc_bit = 0x01;
361 	if (platid_match(&platid, &platid_mask_MACH_SHARP_WZERO3_WS003SH)
362 	 || platid_match(&platid, &platid_mask_MACH_SHARP_WZERO3_WS004SH)) {
363 		sc->sc_nouse_pin = GPIO_WS003SH_SD_DETECT;  /* SD_DETECT */
364 		sc->sc_nouse_pin2 = 86;  /* Vsync? */
365 		sc->sc_nouse_pin3 = 89;  /* RESET? */
366 	}
367 	if (platid_match(&platid, &platid_mask_MACH_SHARP_WZERO3_WS007SH)) {
368 		sc->sc_nouse_pin = GPIO_WS007SH_SD_DETECT;  /* SD_DETECT */
369 		sc->sc_nouse_pin2 = 77;  /* Vsync? */
370 	}
371 	if (platid_match(&platid, &platid_mask_MACH_SHARP_WZERO3_WS011SH)) {
372 		sc->sc_nouse_pin = GPIO_WS011SH_SD_DETECT;  /* SD_DETECT */
373 		sc->sc_nouse_pin2 = 77;  /* Vsync? */
374 	}
375 
376 #ifdef KEYTEST
377 	for (sc->sc_test_pin = 2; sc->sc_test_pin < PXA270_GPIO_NPINS; sc->sc_test_pin++) {
378 		if (sc->sc_test_pin != sc->sc_nouse_pin
379 		 && sc->sc_test_pin != sc->sc_nouse_pin2
380 		 && sc->sc_test_pin != sc->sc_nouse_pin3
381 		 && sc->sc_test_pin != sc->sc_key_pin
382 		 && sc->sc_test_pin != sc->sc_power_pin
383 		 && sc->sc_test_pin != sc->sc_reset_pin
384 		 && GPIO_IS_GPIO_IN(pxa2x0_gpio_get_function(sc->sc_test_pin)))
385 			break;
386 	}
387 	if (sc->sc_test_pin < PXA270_GPIO_NPINS) {
388 		printf("GPIO_IN: GPIO pin #%d\n", sc->sc_test_pin);
389 		sc->sc_test_ih = pxa2x0_gpio_intr_establish(sc->sc_test_pin,
390 		    IST_EDGE_BOTH, IPL_TTY, wzero3kbd_intr2, sc);
391 	} else {
392 		sc->sc_test_pin = -1;
393 	}
394 #endif
395 
396 #ifdef KEYTEST3
397 	{
398 		int i;
399 		printf("pin: ");
400 		for (i = 0; i < PXA270_GPIO_NPINS; i++) {
401 			if (i == sc->sc_nouse_pin
402 			 || i == sc->sc_nouse_pin2
403 			 || i == sc->sc_nouse_pin3
404 			 || i == sc->sc_key_pin
405 			 || i == sc->sc_power_pin
406 			 || i == sc->sc_reset_pin)
407 				continue;
408 
409 			printf("%d, ", i);
410 			if (GPIO_IS_GPIO_IN(pxa2x0_gpio_get_function(i))) {
411 				pxa2x0_gpio_intr_establish(i, IST_EDGE_BOTH,
412 				    IPL_TTY, wzero3kbd_intr3, (void *)(long)i);
413 			}
414 		}
415 	}
416 #endif
417 
418 #ifdef KEYTEST4
419 	for (sc->sc_test_pin = 2; sc->sc_test_pin < PXA270_GPIO_NPINS; sc->sc_test_pin++) {
420 		if (sc->sc_test_pin != sc->sc_nouse_pin
421 		 && sc->sc_test_pin != sc->sc_nouse_pin2
422 		 && sc->sc_test_pin != sc->sc_nouse_pin3
423 		 && sc->sc_test_pin != sc->sc_key_pin
424 		 && sc->sc_test_pin != sc->sc_power_pin
425 		 && sc->sc_test_pin != sc->sc_reset_pin
426 		 && GPIO_IS_GPIO_OUT(pxa2x0_gpio_get_function(sc->sc_test_pin)))
427 			break;
428 	}
429 	if (sc->sc_test_pin < PXA270_GPIO_NPINS) {
430 		printf("GPIO_OUT: GPIO pin #%d\n", sc->sc_test_pin);
431 	} else {
432 		sc->sc_test_pin = -1;
433 	}
434 #endif
435 #ifdef KEYTEST5
436 	sc->sc_test_pin = 0x00;
437 	sc->sc_bit = 0x01;
438 #endif
439 #endif
440 }
441 
442 static int
443 wzero3kbd_intr(void *arg)
444 {
445 	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;
446 
447 #if defined(KEYTEST) || defined(KEYTEST2) || defined(KEYTEST3) || defined(KEYTEST4) || defined(KEYTEST5)
448 	printf("wzero3kbd_intr: GPIO pin #%d = %s\n", sc->sc_key_pin,
449 	    pxa2x0_gpio_get_bit(sc->sc_key_pin) ? "on" : "off");
450 #endif
451 
452 #if defined(KEYTEST4)
453 	if (sc->sc_test_pin >= 0) {
454 		if (pxa2x0_gpio_get_bit(sc->sc_test_pin)) {
455 			printf("GPIO_OUT: GPIO pin #%d: L\n",sc->sc_test_pin);
456 			pxa2x0_gpio_clear_bit(sc->sc_test_pin);
457 		} else {
458 			printf("GPIO_OUT: GPIO pin #%d: H\n", sc->sc_test_pin);
459 			pxa2x0_gpio_set_bit(sc->sc_test_pin);
460 		}
461 	}
462 #endif
463 #if defined(KEYTEST5)
464 	printf("CPLD(%#x): value=%#x, mask=%#x\n",
465 	    sc->sc_test_pin, CSR_READ4(sc->sc_test_pin), sc->sc_bit);
466 	if (CSR_READ4(sc->sc_test_pin) & sc->sc_bit) {
467 		printf("CPLD_OUT: CPLD: L\n");
468 		CSR_WRITE4(sc->sc_test_pin,
469 		    CSR_READ4(sc->sc_test_pin) & ~sc->sc_bit);
470 	} else {
471 		printf("CPLD_OUT: CPLD: H\n");
472 		CSR_WRITE4(sc->sc_test_pin,
473 		    CSR_READ4(sc->sc_test_pin) | sc->sc_bit);
474 	}
475 #endif
476 
477 	(void) wzero3kbd_poll1(sc);
478 
479 	pxa2x0_gpio_clear_intr(sc->sc_key_pin);
480 
481 	return 1;
482 }
483 
484 #if defined(KEYTEST)
485 static int
486 wzero3kbd_intr2(void *arg)
487 {
488 	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;
489 
490 	printf("wzero3kbd_intr2: GPIO_IN: GPIO pin #%d = %s\n", sc->sc_test_pin,
491 	    pxa2x0_gpio_get_bit(sc->sc_test_pin) ? "on" : "off");
492 
493 	return 1;
494 }
495 #endif
496 
497 #if defined(KEYTEST3)
498 static int
499 wzero3kbd_intr3(void *arg)
500 {
501 	int pin = (int)arg;
502 
503 	printf("wzero3kbd_intr3: GPIO pin #%d = %s\n", pin,
504 	    pxa2x0_gpio_get_bit(pin) ? "on" : "off");
505 
506 	return 1;
507 }
508 #endif
509 
510 
511 static void
512 wzero3kbd_tick(void *arg)
513 {
514 	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;
515 
516 	(void) wzero3kbd_poll1(sc);
517 
518 	callout_reset(&sc->sc_keyscan_ch, sc->sc_interval, wzero3kbd_tick, sc);
519 }
520 
521 static int
522 wzero3kbd_power_intr(void *arg)
523 {
524 	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;
525 
526 #if defined(KEYTEST) || defined(KEYTEST2) || defined(KEYTEST3) || defined(KEYTEST4)
527 	printf("wzero3kbd_power_intr: status = %s\n",
528 	    pxa2x0_gpio_get_bit(sc->sc_power_pin) ? "on" : "off");
529 #endif
530 
531 #if defined(KEYTEST)
532 	if (pxa2x0_gpio_get_bit(sc->sc_power_pin)) {
533 		if (sc->sc_test_pin >= 0) {
534 			int orig_pin = sc->sc_test_pin;
535 			pxa2x0_gpio_intr_disestablish(sc->sc_test_ih);
536 			sc->sc_test_ih = NULL;
537 
538 			for (;;) {
539 				if (++sc->sc_test_pin >= PXA270_GPIO_NPINS)
540 					sc->sc_test_pin = 2;
541 				if (sc->sc_test_pin == orig_pin)
542 					break;
543 				if (sc->sc_test_pin != sc->sc_nouse_pin
544 				 && sc->sc_test_pin != sc->sc_nouse_pin2
545 				 && sc->sc_test_pin != sc->sc_nouse_pin3
546 				 && sc->sc_test_pin != sc->sc_key_pin
547 				 && sc->sc_test_pin != sc->sc_power_pin
548 				 && sc->sc_test_pin != sc->sc_reset_pin
549 				 && GPIO_IS_GPIO_IN(pxa2x0_gpio_get_function(sc->sc_test_pin)))
550 					break;
551 			}
552 			if (sc->sc_test_pin != orig_pin) {
553 				printf("GPIO_IN: GPIO pin #%d\n",
554 				    sc->sc_test_pin);
555 				sc->sc_test_ih =
556 				    pxa2x0_gpio_intr_establish(sc->sc_test_pin,
557 				    IST_EDGE_BOTH, IPL_TTY, wzero3kbd_intr2,sc);
558 			} else {
559 				sc->sc_test_pin = -1;
560 			}
561 		}
562 	}
563 #endif
564 
565 #if defined(KEYTEST2)
566 	if (pxa2x0_gpio_get_bit(sc->sc_power_pin)) {
567 		sc->sc_enabled ^= 2;
568 		if (sc->sc_enabled & 2) {
569 			printf("print col/row\n");
570 		} else {
571 			printf("keyscan\n");
572 		}
573 	}
574 #endif
575 #if defined(KEYTEST4)
576 	if (pxa2x0_gpio_get_bit(sc->sc_power_pin)) {
577 		if (sc->sc_test_pin >= 0) {
578 			int orig_pin = sc->sc_test_pin;
579 			for (;;) {
580 				if (++sc->sc_test_pin >= PXA270_GPIO_NPINS)
581 					sc->sc_test_pin = 2;
582 				if (sc->sc_test_pin == orig_pin)
583 					break;
584 				if (sc->sc_test_pin != sc->sc_nouse_pin
585 				 && sc->sc_test_pin != sc->sc_nouse_pin2
586 				 && sc->sc_test_pin != sc->sc_nouse_pin3
587 				 && sc->sc_test_pin != sc->sc_key_pin
588 				 && sc->sc_test_pin != sc->sc_power_pin
589 				 && sc->sc_test_pin != sc->sc_reset_pin
590 				 && GPIO_IS_GPIO_OUT(pxa2x0_gpio_get_function(sc->sc_test_pin)))
591 				break;
592 			}
593 			if (sc->sc_test_pin != orig_pin) {
594 				printf("GPIO_OUT: GPIO pin #%d\n", sc->sc_test_pin);
595 			} else {
596 				sc->sc_test_pin = -1;
597 			}
598 		}
599 	}
600 #endif
601 #if defined(KEYTEST5)
602 	if (pxa2x0_gpio_get_bit(sc->sc_power_pin)) {
603 		sc->sc_bit <<= 1;
604 		if (sc->sc_bit & ~0xff) {
605 			sc->sc_bit = 0x01;
606 			sc->sc_test_pin += 0x4;
607 			if (sc->sc_test_pin >= 0x20) {
608 				sc->sc_test_pin = 0x00;
609 			}
610 		}
611 		printf("CPLD(%#x), mask=%#x\n", sc->sc_test_pin, sc->sc_bit);
612 	}
613 #endif
614 
615 	pxa2x0_gpio_clear_intr(sc->sc_power_pin);
616 
617 	return 1;
618 }
619 
620 static int
621 wzero3kbd_reset_intr(void *arg)
622 {
623 	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;
624 
625 	sysmon_task_queue_sched(0, wzero3kbd_sysmon_reset_event, sc);
626 
627 	pxa2x0_gpio_clear_intr(sc->sc_reset_pin);
628 
629 	return 1;
630 }
631 
632 static int
633 wzero3kbd_input_establish(void *arg, struct hpckbd_if *kbdif)
634 {
635 	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;
636 
637 	/* Save hpckbd interface. */
638 	sc->sc_hpckbd = kbdif;
639 	sc->sc_enabled = 1;
640 
641 	return 0;
642 }
643 
644 static void
645 wzero3kbd_sysmon_reset_event(void *arg)
646 {
647 	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;
648 
649 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
650 }
651 
652 static int
653 wzero3kbd_poll(void *arg)
654 {
655 	int keydown;
656 
657 	keydown = wzero3kbd_poll1(arg);
658 
659 	return keydown;
660 }
661 
662 static int
663 wzero3kbd_poll1(void *arg)
664 {
665 	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;
666 	int row, col, data;
667 	int keycol;
668 	int keydown;
669 	int i;
670 	int s;
671 
672 	if (!sc->sc_enabled) {
673 		DPRINTF(("wzero3kbd_poll: disabled\n"));
674 		return 0;
675 	}
676 
677 	s = spltty();
678 
679 	for (col = 0; col < sc->sc_ncolumn; col++) {
680 		/* deselect column# and charge */
681 		CSR_WRITE1(KBDCOL_L, 0);
682 		CSR_WRITE1(KBDCOL_U, 0);
683 		CSR_WRITE1(KBDCHARGE, 1);
684 		delay(KEYWAIT);
685 		CSR_WRITE1(KBDCHARGE, 0);
686 
687 		/* select scan column# */
688 		keycol = 1 << col;
689 		CSR_WRITE1(KBDCOL_L, keycol & 0xff);
690 		CSR_WRITE1(KBDCOL_U, keycol >> 8);
691 		delay(KEYWAIT);
692 		CSR_WRITE1(KBDCHARGE, 0);
693 
694 		/* read key data */
695 		data = CSR_READ1(KBDDATA);
696 		for (row = 0; row < sc->sc_nrow; row++) {
697 #ifdef KEYTEST2
698 			if (!(sc->sc_enabled & 2)) {
699 #endif
700 			sc->sc_keystat[row + col * sc->sc_nrow] =
701 			    (data >> row) & 1;
702 #ifdef KEYTEST2
703 			} else if (data & (1 << row)) {
704 				printf("col = %d, row = %d, idx = %d, data = 0x%02x\n", col, row, row + col * sc->sc_nrow, data);
705 			}
706 #endif
707 		}
708 	}
709 
710 	/* deselect column# and charge */
711 	CSR_WRITE1(KBDCOL_L, 0);
712 	CSR_WRITE1(KBDCOL_U, 0);
713 	CSR_WRITE1(KBDCHARGE, 1);
714 	delay(KEYWAIT);
715 	CSR_WRITE1(KBDCHARGE, 0);
716 
717 	/* send key scan code */
718 	keydown = 0;
719 	for (i = 0; i < sc->sc_nrow * sc->sc_ncolumn; i++) {
720 		if (sc->sc_keystat[i] == sc->sc_okeystat[i])
721 			continue;
722 
723 		keydown |= sc->sc_keystat[i];
724 		hpckbd_input(sc->sc_hpckbd, sc->sc_keystat[i], i);
725 		sc->sc_okeystat[i] = sc->sc_keystat[i];
726 	}
727 
728 	splx(s);
729 
730 	return keydown;
731 }
732