Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/*	$NetBSD: wzero3_kbd.c,v 1.8 2012/10/27 17:17:52 chs Exp $	*/

/*-
 * Copyright (C) 2008, 2009, 2010 NONAKA Kimihiro <nonaka@netbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: wzero3_kbd.c,v 1.8 2012/10/27 17:17:52 chs Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/callout.h>
#include <sys/bus.h>

#include <dev/sysmon/sysmonvar.h>
#include <dev/sysmon/sysmon_taskq.h>

#include <arm/xscale/pxa2x0cpu.h>
#include <arm/xscale/pxa2x0var.h>
#include <arm/xscale/pxa2x0_gpio.h>

#include <machine/bootinfo.h>
#include <machine/config_hook.h>
#include <machine/platid.h>
#include <machine/platid_mask.h>

#include <dev/hpc/hpckbdvar.h>

#include <arch/hpcarm/dev/wzero3_reg.h>

#ifdef DEBUG
#define DPRINTF(arg)	printf arg
#else
#define DPRINTF(arg)	/* nothing */
#endif

#define	CSR_READ1(r)	bus_space_read_1(sc->sc_iot, sc->sc_ioh, (r))
#define	CSR_WRITE1(r,v)	bus_space_write_1(sc->sc_iot, sc->sc_ioh, (r), (v))
#define	CSR_READ2(r)	bus_space_read_2(sc->sc_iot, sc->sc_ioh, (r))
#define	CSR_WRITE2(r,v)	bus_space_write_2(sc->sc_iot, sc->sc_ioh, (r), (v))
#define	CSR_READ4(r)	bus_space_read_4(sc->sc_iot, sc->sc_ioh, (r))
#define	CSR_WRITE4(r,v)	bus_space_write_4(sc->sc_iot, sc->sc_ioh, (r), (v))

/* register */
#define	KBDCOL_L	(0x00)	/* Write */
#define	KBDCOL_U	(0x04)	/* Write */
#define	KBDCHARGE	(0x08)	/* Write */
#define	KBDDATA		(0x08)	/* Read */
#define	REGMAPSIZE	0x0c

#define	KEYWAIT		20	/* us */

#define	WS003SH_NCOLUMN	12
#define	WS003SH_NROW	7

struct wzero3kbd_softc {
	device_t sc_dev;

	bus_space_tag_t sc_iot;
	bus_space_handle_t sc_ioh;

	int sc_ncolumn;
	int sc_nrow;
	uint8_t *sc_okeystat;
	uint8_t *sc_keystat;

	void *sc_key_ih;
	void *sc_power_ih;
	void *sc_reset_ih;

	int sc_key_pin;
	int sc_power_pin;
	int sc_reset_pin;

	struct hpckbd_ic_if sc_if;
	struct hpckbd_if *sc_hpckbd;

	struct sysmon_pswitch sc_smpsw; /* for reset key */

	int sc_enabled;

	/* polling stuff */
	struct callout sc_keyscan_ch;
	int sc_interval;
#define	KEY_INTERVAL	50	/* ms */

#if defined(KEYTEST) || defined(KEYTEST2) || defined(KEYTEST3) || defined(KEYTEST4) || defined(KEYTEST5)
	void *sc_test_ih;
	int sc_test_pin;
	int sc_nouse_pin;
	int sc_nouse_pin2;
	int sc_nouse_pin3;
	int sc_bit;
#endif
};

static int wzero3kbd_match(device_t, cfdata_t, void *);
static void wzero3kbd_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(wzero3kbd, sizeof(struct wzero3kbd_softc),
    wzero3kbd_match, wzero3kbd_attach, NULL, NULL);

static int wzero3kbd_intr(void *arg);
#if defined(KEYTEST)
static int wzero3kbd_intr2(void *arg);
#endif
#if defined(KEYTEST3)
static int wzero3kbd_intr3(void *arg);
#endif
static void wzero3kbd_tick(void *arg);
static int wzero3kbd_power_intr(void *arg);
static int wzero3kbd_reset_intr(void *arg);
static int wzero3kbd_input_establish(void *arg, struct hpckbd_if *kbdif);
static void wzero3kbd_sysmon_reset_event(void *arg);
static int wzero3kbd_poll(void *arg);
static int wzero3kbd_poll1(void *arg);

/*
 * WS003SH/WS004SH/WS007SH keyscan map
	col#0	col#1	col#2	col#3	col#4	col#5	col#6	col#7	col#8	col#9	col#10	col#11
row#0:	CTRL	1	3	5	6	7	9	0	BS	(none)	ROTATE	CAMERA
row#1:	(none)	2	4	r	y	8	i	o	p	(none)	VOL-	VOL+
row#2:	TAB	q	e	t	g	u	j	k	(none)	(none)	(none)	(none)
row#3:	(none)	w	s	f	v	h	m	l	(none)	(none)	SHIFT	(none)
row#4:	CALL	a	d	c	b	n	.	(none)	ENTER	(none)	WIN	(none)
row#5:	MAIL	z	x	-	SPACE	/	(none)	UP	(none)	(none)	LSOFT	FN
row#6:	IE	MOJI	(none)	OK	ACTION	,	LEFT	DOWN	RIGHT	(none)	RSOFT	(none)
*/

/*
 * WS011SH keyscan map
	col#0	col#1	col#2	col#3	col#4	col#5	col#6	col#7	col#8	col#9	col#10	col#11
row#0	Ctrl	(none)	(none)	(none)	(none)	(none)	(none)	(none)	Del	(none)	ROTATE	(none)
row#1	(none)	(none)	(none)	R	Y	(none)	I	O	P	(none)	(none)	(none)
row#2	Tab	Q	E	T	G	U	J	K	(none)	(none)	(none)	(none)
row#3	(none)	W	S	F	V	H	M	L	(none)	(none)	Shift	(none)
row#4	(none)	A	D	C	B	N	.	(none)	Enter	(none)	(none)	(none)
row#5	(none)	Z	X	-	Space	/	(none)	UP	(none)	(none)	(none)	Fn
row#6	(none)	MOJI	HAN/ZEN	OK	(none)	,	LEFT	DOWN	RIGHT	(none)	(none)	(none)
*/

/*
 * WS020SH keyscan map
	col#0	col#1	col#2	col#3	col#4	col#5	col#6	col#7	col#8	col#9	col#10	col#11
row#0	Ctrl	(none)	(none)	(none)	(none)	(none)	(none)	(none)	Del	(none)	ROTATE	(none)
row#1	(none)	(none)	(none)	R	Y	(none)	I	O	P	(none)	MEDIA	(none)
row#2	Tab	Q	E	T	G	U	J	K	(none)	(none)	(none)	(none)
row#3	(none)	W	S	F	V	H	M	L	(none)	(none)	LShift	(none)
row#4	(none)	A	D	C	B	N	.	(none)	Enter	(none)	RShift	(none)
row#5	(none)	Z	X	-	Space	/	(none)	UP	(none)	DOWN	(none)	Fn
row#6	(none)	MOJI	HAN/ZEN	OK	(none)	,	LEFT	(none)	RIGHT	(none)	(none)	(none)
*/

static const struct wzero3kbd_model {
	platid_mask_t *platid;
	int key_pin;
	int power_pin;
	int reset_pin;
	int ncolumn;
	int nrow;
} wzero3kbd_table[] = {
	/* WS003SH */
	{
		&platid_mask_MACH_SHARP_WZERO3_WS003SH,
		-1,	/* XXX */
		GPIO_WS003SH_POWER_BUTTON,
		-1,	/* None */
		WS003SH_NCOLUMN,
		WS003SH_NROW,
	},
	/* WS004SH */
	{
		&platid_mask_MACH_SHARP_WZERO3_WS004SH,
		-1,	/* XXX */
		GPIO_WS003SH_POWER_BUTTON,
		-1,	/* None */
		WS003SH_NCOLUMN,
		WS003SH_NROW,
	},
	/* WS007SH */
	{
		&platid_mask_MACH_SHARP_WZERO3_WS007SH,
		-1,	/* XXX */
		GPIO_WS007SH_POWER_BUTTON,
		GPIO_WS007SH_RESET_BUTTON,
		WS003SH_NCOLUMN,
		WS003SH_NROW,
	},
	/* WS011SH */
	{
		&platid_mask_MACH_SHARP_WZERO3_WS011SH,
		-1,	/* XXX */
		GPIO_WS011SH_POWER_BUTTON,
		GPIO_WS011SH_RESET_BUTTON,
		WS003SH_NCOLUMN,
		WS003SH_NROW,
	},
	/* WS020SH */
	{
		&platid_mask_MACH_SHARP_WZERO3_WS020SH,
		-1,	/* XXX */
		GPIO_WS020SH_POWER_BUTTON,
		GPIO_WS020SH_RESET_BUTTON,
		WS003SH_NCOLUMN,
		WS003SH_NROW,
	},

	{ NULL, -1, -1, -1, 0, 0, }
};

static const struct wzero3kbd_model *
wzero3kbd_lookup(void)
{
	const struct wzero3kbd_model *model;

	for (model = wzero3kbd_table; model->platid != NULL; model++) {
		if (platid_match(&platid, model->platid)) {
			return model;
		}
	}
	return NULL;
}

static int
wzero3kbd_match(device_t parent, cfdata_t cf, void *aux)
{

	if (strcmp(cf->cf_name, "wzero3kbd") != 0)
		return 0;
	if (wzero3kbd_lookup() == NULL)
		return 0;
	return 1;
}

static void
wzero3kbd_attach(device_t parent, device_t self, void *aux)
{
	struct wzero3kbd_softc *sc = device_private(self);
	struct pxaip_attach_args *pxa = (struct pxaip_attach_args *)aux;
	struct hpckbd_attach_args haa;
	const struct wzero3kbd_model *model;

	sc->sc_dev = self;

	model = wzero3kbd_lookup();
	if (model == NULL) {
		aprint_error(": unknown model\n");
		return;
	}

	aprint_normal(": keyboard\n");
	aprint_naive("\n");

	sc->sc_key_pin = model->key_pin;
	sc->sc_power_pin = model->power_pin;
	sc->sc_reset_pin = model->reset_pin;
	sc->sc_ncolumn = model->ncolumn;
	sc->sc_nrow = model->nrow;

	sc->sc_iot = pxa->pxa_iot;
	if (bus_space_map(sc->sc_iot, PXA2X0_CS2_START, REGMAPSIZE, 0,
	    &sc->sc_ioh)) {
		aprint_error_dev(self, "couldn't map registers.\n");
		return;
	}

	sc->sc_okeystat = malloc(sc->sc_nrow * sc->sc_ncolumn, M_DEVBUF,
	    M_NOWAIT | M_ZERO);
	sc->sc_keystat = malloc(sc->sc_nrow * sc->sc_ncolumn, M_DEVBUF,
	    M_NOWAIT | M_ZERO);
	if (sc->sc_okeystat == NULL || sc->sc_keystat == NULL) {
		aprint_error_dev(self, "couldn't alloc memory.\n");
		if (sc->sc_okeystat)
			free(sc->sc_okeystat, M_DEVBUF);
		if (sc->sc_keystat)
			free(sc->sc_keystat, M_DEVBUF);
		return;
	}

	sc->sc_if.hii_ctx = sc;
	sc->sc_if.hii_establish = wzero3kbd_input_establish;
	sc->sc_if.hii_poll = wzero3kbd_poll;

	/* Attach console if not using serial. */
	if (!(bootinfo->bi_cnuse & BI_CNUSE_SERIAL))
		hpckbd_cnattach(&sc->sc_if);

	/* Install interrupt handler. */
	if (sc->sc_key_pin >= 0) {
		pxa2x0_gpio_set_function(sc->sc_key_pin, GPIO_IN);
		sc->sc_key_ih = pxa2x0_gpio_intr_establish(sc->sc_key_pin,
		    IST_EDGE_BOTH, IPL_TTY, wzero3kbd_intr, sc);
		if (sc->sc_key_ih == NULL) {
			aprint_error_dev(sc->sc_dev,
			    "couldn't establish key interrupt\n");
		}
	} else {
		sc->sc_interval = KEY_INTERVAL / (1000 / hz);
		if (sc->sc_interval < 1)
			sc->sc_interval = 1;
		callout_init(&sc->sc_keyscan_ch, 0);
		callout_reset(&sc->sc_keyscan_ch, sc->sc_interval,
		    wzero3kbd_tick, sc);
	}

	/* power key */
	if (sc->sc_power_pin >= 0) {
		pxa2x0_gpio_set_function(sc->sc_power_pin, GPIO_IN);
		sc->sc_power_ih = pxa2x0_gpio_intr_establish(
		    sc->sc_power_pin, IST_EDGE_BOTH, IPL_TTY,
		    wzero3kbd_power_intr, sc);
		if (sc->sc_power_ih == NULL) {
			aprint_error_dev(sc->sc_dev,
			    "couldn't establish power key interrupt\n");
		}
	}

	/* reset button */
	if (sc->sc_reset_pin >= 0) {
		pxa2x0_gpio_set_function(sc->sc_reset_pin, GPIO_IN);
		sc->sc_reset_ih = pxa2x0_gpio_intr_establish(
		    sc->sc_reset_pin, IST_EDGE_BOTH, IPL_TTY,
		    wzero3kbd_reset_intr, sc);
		if (sc->sc_reset_ih == NULL) {
			aprint_error_dev(sc->sc_dev,
			    "couldn't establish reset key interrupt\n");
		}

		sc->sc_smpsw.smpsw_name = device_xname(self);
		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_RESET;
		if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
			aprint_error_dev(sc->sc_dev,
			    "unable to register reset event handler\n");
		}
	}

	/* Attach hpckbd. */
	haa.haa_ic = &sc->sc_if;
	config_found(self, &haa, hpckbd_print);

#if defined(KEYTEST) || defined(KEYTEST2) || defined(KEYTEST3) || defined(KEYTEST4) || defined(KEYTEST5)
	sc->sc_test_ih = NULL;
	sc->sc_test_pin = -1;
	sc->sc_nouse_pin = -1;
	sc->sc_nouse_pin2 = -1;
	sc->sc_nouse_pin3 = -1;
	sc->sc_bit = 0x01;
	if (platid_match(&platid, &platid_mask_MACH_SHARP_WZERO3_WS003SH)
	 || platid_match(&platid, &platid_mask_MACH_SHARP_WZERO3_WS004SH)) {
		sc->sc_nouse_pin = GPIO_WS003SH_SD_DETECT;  /* SD_DETECT */
		sc->sc_nouse_pin2 = 86;  /* Vsync? */
		sc->sc_nouse_pin3 = 89;  /* RESET? */
	}
	if (platid_match(&platid, &platid_mask_MACH_SHARP_WZERO3_WS007SH)) {
		sc->sc_nouse_pin = GPIO_WS007SH_SD_DETECT;  /* SD_DETECT */
		sc->sc_nouse_pin2 = 77;  /* Vsync? */
	}
	if (platid_match(&platid, &platid_mask_MACH_SHARP_WZERO3_WS011SH)) {
		sc->sc_nouse_pin = GPIO_WS011SH_SD_DETECT;  /* SD_DETECT */
		sc->sc_nouse_pin2 = 77;  /* Vsync? */
	}
	if (platid_match(&platid, &platid_mask_MACH_SHARP_WZERO3_WS020SH)) {
		sc->sc_nouse_pin = GPIO_WS020SH_SD_DETECT;  /* SD_DETECT */
		sc->sc_nouse_pin2 = 77;  /* Vsync? */
	}

#ifdef KEYTEST
	for (sc->sc_test_pin = 2; sc->sc_test_pin < PXA270_GPIO_NPINS; sc->sc_test_pin++) {
		if (sc->sc_test_pin != sc->sc_nouse_pin
		 && sc->sc_test_pin != sc->sc_nouse_pin2
		 && sc->sc_test_pin != sc->sc_nouse_pin3
		 && sc->sc_test_pin != sc->sc_key_pin
		 && sc->sc_test_pin != sc->sc_power_pin
		 && sc->sc_test_pin != sc->sc_reset_pin
		 && GPIO_IS_GPIO_IN(pxa2x0_gpio_get_function(sc->sc_test_pin)))
			break;
	}
	if (sc->sc_test_pin < PXA270_GPIO_NPINS) {
		printf("GPIO_IN: GPIO pin #%d\n", sc->sc_test_pin);
		sc->sc_test_ih = pxa2x0_gpio_intr_establish(sc->sc_test_pin,
		    IST_EDGE_BOTH, IPL_TTY, wzero3kbd_intr2, sc);
	} else {
		sc->sc_test_pin = -1;
	}
#endif

#ifdef KEYTEST3
	{
		int i;
		printf("pin: ");
		for (i = 0; i < PXA270_GPIO_NPINS; i++) {
			if (i == sc->sc_nouse_pin
			 || i == sc->sc_nouse_pin2
			 || i == sc->sc_nouse_pin3
			 || i == sc->sc_key_pin
			 || i == sc->sc_power_pin
			 || i == sc->sc_reset_pin)
				continue;

			printf("%d, ", i);
			if (GPIO_IS_GPIO_IN(pxa2x0_gpio_get_function(i))) {
				pxa2x0_gpio_intr_establish(i, IST_EDGE_BOTH,
				    IPL_TTY, wzero3kbd_intr3, (void *)(long)i);
			}
		}
	}
#endif

#ifdef KEYTEST4
	for (sc->sc_test_pin = 2; sc->sc_test_pin < PXA270_GPIO_NPINS; sc->sc_test_pin++) {
		if (sc->sc_test_pin != sc->sc_nouse_pin
		 && sc->sc_test_pin != sc->sc_nouse_pin2
		 && sc->sc_test_pin != sc->sc_nouse_pin3
		 && sc->sc_test_pin != sc->sc_key_pin
		 && sc->sc_test_pin != sc->sc_power_pin
		 && sc->sc_test_pin != sc->sc_reset_pin
		 && GPIO_IS_GPIO_OUT(pxa2x0_gpio_get_function(sc->sc_test_pin)))
			break;
	}
	if (sc->sc_test_pin < PXA270_GPIO_NPINS) {
		printf("GPIO_OUT: GPIO pin #%d\n", sc->sc_test_pin);
	} else {
		sc->sc_test_pin = -1;
	}
#endif
#ifdef KEYTEST5
	sc->sc_test_pin = 0x00;
	sc->sc_bit = 0x01;
#endif
#endif
}

static int
wzero3kbd_intr(void *arg)
{
	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;

#if defined(KEYTEST) || defined(KEYTEST2) || defined(KEYTEST3) || defined(KEYTEST4) || defined(KEYTEST5)
	printf("wzero3kbd_intr: GPIO pin #%d = %s\n", sc->sc_key_pin,
	    pxa2x0_gpio_get_bit(sc->sc_key_pin) ? "on" : "off");
#endif

#if defined(KEYTEST4)
	if (sc->sc_test_pin >= 0) {
		if (pxa2x0_gpio_get_bit(sc->sc_test_pin)) {
			printf("GPIO_OUT: GPIO pin #%d: L\n",sc->sc_test_pin);
			pxa2x0_gpio_clear_bit(sc->sc_test_pin);
		} else {
			printf("GPIO_OUT: GPIO pin #%d: H\n", sc->sc_test_pin);
			pxa2x0_gpio_set_bit(sc->sc_test_pin);
		}
	}
#endif
#if defined(KEYTEST5)
	printf("CPLD(%#x): value=%#x, mask=%#x\n",
	    sc->sc_test_pin, CSR_READ4(sc->sc_test_pin), sc->sc_bit);
	if (CSR_READ4(sc->sc_test_pin) & sc->sc_bit) {
		printf("CPLD_OUT: CPLD: L\n");
		CSR_WRITE4(sc->sc_test_pin,
		    CSR_READ4(sc->sc_test_pin) & ~sc->sc_bit);
	} else {
		printf("CPLD_OUT: CPLD: H\n");
		CSR_WRITE4(sc->sc_test_pin,
		    CSR_READ4(sc->sc_test_pin) | sc->sc_bit);
	}
#endif

	(void) wzero3kbd_poll1(sc);

	pxa2x0_gpio_clear_intr(sc->sc_key_pin);

	return 1;
}

#if defined(KEYTEST)
static int
wzero3kbd_intr2(void *arg)
{
	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;

	printf("wzero3kbd_intr2: GPIO_IN: GPIO pin #%d = %s\n", sc->sc_test_pin,
	    pxa2x0_gpio_get_bit(sc->sc_test_pin) ? "on" : "off");

	return 1;
}
#endif

#if defined(KEYTEST3)
static int
wzero3kbd_intr3(void *arg)
{
	int pin = (int)arg;

	printf("wzero3kbd_intr3: GPIO pin #%d = %s\n", pin,
	    pxa2x0_gpio_get_bit(pin) ? "on" : "off");

	return 1;
}
#endif


static void
wzero3kbd_tick(void *arg)
{
	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;

	(void) wzero3kbd_poll1(sc);

	callout_schedule(&sc->sc_keyscan_ch, sc->sc_interval);
}

static int
wzero3kbd_power_intr(void *arg)
{
	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;

#if defined(KEYTEST) || defined(KEYTEST2) || defined(KEYTEST3) || defined(KEYTEST4)
	printf("wzero3kbd_power_intr: status = %s\n",
	    pxa2x0_gpio_get_bit(sc->sc_power_pin) ? "on" : "off");
#endif

#if defined(KEYTEST)
	if (pxa2x0_gpio_get_bit(sc->sc_power_pin)) {
		if (sc->sc_test_pin >= 0) {
			int orig_pin = sc->sc_test_pin;
			pxa2x0_gpio_intr_disestablish(sc->sc_test_ih);
			sc->sc_test_ih = NULL;

			for (;;) {
				if (++sc->sc_test_pin >= PXA270_GPIO_NPINS)
					sc->sc_test_pin = 2;
				if (sc->sc_test_pin == orig_pin)
					break;
				if (sc->sc_test_pin != sc->sc_nouse_pin
				 && sc->sc_test_pin != sc->sc_nouse_pin2
				 && sc->sc_test_pin != sc->sc_nouse_pin3
				 && sc->sc_test_pin != sc->sc_key_pin
				 && sc->sc_test_pin != sc->sc_power_pin
				 && sc->sc_test_pin != sc->sc_reset_pin
				 && GPIO_IS_GPIO_IN(pxa2x0_gpio_get_function(sc->sc_test_pin)))
					break;
			}
			if (sc->sc_test_pin != orig_pin) {
				printf("GPIO_IN: GPIO pin #%d\n",
				    sc->sc_test_pin);
				sc->sc_test_ih =
				    pxa2x0_gpio_intr_establish(sc->sc_test_pin,
				    IST_EDGE_BOTH, IPL_TTY, wzero3kbd_intr2,sc);
			} else {
				sc->sc_test_pin = -1;
			}
		}
	}
#endif

#if defined(KEYTEST2)
	if (pxa2x0_gpio_get_bit(sc->sc_power_pin)) {
		sc->sc_enabled ^= 2;
		if (sc->sc_enabled & 2) {
			printf("print col/row\n");
		} else {
			printf("keyscan\n");
		}
	}
#endif
#if defined(KEYTEST4)
	if (pxa2x0_gpio_get_bit(sc->sc_power_pin)) {
		if (sc->sc_test_pin >= 0) {
			int orig_pin = sc->sc_test_pin;
			for (;;) {
				if (++sc->sc_test_pin >= PXA270_GPIO_NPINS)
					sc->sc_test_pin = 2;
				if (sc->sc_test_pin == orig_pin)
					break;
				if (sc->sc_test_pin != sc->sc_nouse_pin
				 && sc->sc_test_pin != sc->sc_nouse_pin2
				 && sc->sc_test_pin != sc->sc_nouse_pin3
				 && sc->sc_test_pin != sc->sc_key_pin
				 && sc->sc_test_pin != sc->sc_power_pin
				 && sc->sc_test_pin != sc->sc_reset_pin
				 && GPIO_IS_GPIO_OUT(pxa2x0_gpio_get_function(sc->sc_test_pin)))
				break;
			}
			if (sc->sc_test_pin != orig_pin) {
				printf("GPIO_OUT: GPIO pin #%d\n", sc->sc_test_pin);
			} else {
				sc->sc_test_pin = -1;
			}
		}
	}
#endif
#if defined(KEYTEST5)
	if (pxa2x0_gpio_get_bit(sc->sc_power_pin)) {
		sc->sc_bit <<= 1;
		if (sc->sc_bit & ~0xff) {
			sc->sc_bit = 0x01;
			sc->sc_test_pin += 0x4;
			if (sc->sc_test_pin >= 0x20) {
				sc->sc_test_pin = 0x00;
			}
		}
		printf("CPLD(%#x), mask=%#x\n", sc->sc_test_pin, sc->sc_bit);
	}
#endif

	pxa2x0_gpio_clear_intr(sc->sc_power_pin);

	return 1;
}

static int
wzero3kbd_reset_intr(void *arg)
{
	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;

	sysmon_task_queue_sched(0, wzero3kbd_sysmon_reset_event, sc);

	pxa2x0_gpio_clear_intr(sc->sc_reset_pin);

	return 1;
}

static int
wzero3kbd_input_establish(void *arg, struct hpckbd_if *kbdif)
{
	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;

	/* Save hpckbd interface. */
	sc->sc_hpckbd = kbdif;
	sc->sc_enabled = 1;

	return 0;
}

static void
wzero3kbd_sysmon_reset_event(void *arg)
{
	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;

	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
}

static int
wzero3kbd_poll(void *arg)
{
	int keydown;

	keydown = wzero3kbd_poll1(arg);

	return keydown;
}

static int
wzero3kbd_poll1(void *arg)
{
	struct wzero3kbd_softc *sc = (struct wzero3kbd_softc *)arg;
	int row, col, data;
	int keycol;
	int keydown;
	int i;
	int s;

	if (!sc->sc_enabled) {
		DPRINTF(("wzero3kbd_poll: disabled\n"));
		return 0;
	}

	s = spltty();

	for (col = 0; col < sc->sc_ncolumn; col++) {
		/* deselect column# and charge */
		CSR_WRITE1(KBDCOL_L, 0);
		CSR_WRITE1(KBDCOL_U, 0);
		CSR_WRITE1(KBDCHARGE, 1);
		delay(KEYWAIT);
		CSR_WRITE1(KBDCHARGE, 0);

		/* select scan column# */
		keycol = 1 << col;
		CSR_WRITE1(KBDCOL_L, keycol & 0xff);
		CSR_WRITE1(KBDCOL_U, keycol >> 8);
		delay(KEYWAIT);
		CSR_WRITE1(KBDCHARGE, 0);

		/* read key data */
		data = CSR_READ1(KBDDATA);
		for (row = 0; row < sc->sc_nrow; row++) {
#ifdef KEYTEST2
			if (!(sc->sc_enabled & 2)) {
#endif
			sc->sc_keystat[row + col * sc->sc_nrow] =
			    (data >> row) & 1;
#ifdef KEYTEST2
			} else if (data & (1 << row)) {
				printf("col = %d, row = %d, idx = %d, data = 0x%02x\n", col, row, row + col * sc->sc_nrow, data);
			}
#endif
		}
	}

	/* deselect column# and charge */
	CSR_WRITE1(KBDCOL_L, 0);
	CSR_WRITE1(KBDCOL_U, 0);
	CSR_WRITE1(KBDCHARGE, 1);
	delay(KEYWAIT);
	CSR_WRITE1(KBDCHARGE, 0);

	/* send key scan code */
	keydown = 0;
	for (i = 0; i < sc->sc_nrow * sc->sc_ncolumn; i++) {
		if (sc->sc_keystat[i] == sc->sc_okeystat[i])
			continue;

		keydown |= sc->sc_keystat[i];
		hpckbd_input(sc->sc_hpckbd, sc->sc_keystat[i], i);
		sc->sc_okeystat[i] = sc->sc_keystat[i];
	}

	splx(s);

	return keydown;
}