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