summaryrefslogtreecommitdiff
path: root/sys/arch/hpcsh/dev/j6x0tp.c
blob: f17d298196f55f8368019cb0ad1c2d152848a02f (plain)
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
/*	$NetBSD: j6x0tp.c,v 1.25 2021/08/07 16:18:54 thorpej Exp $ */

/*
 * Copyright (c) 2003 Valeriy E. Ushakov
 * 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.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * 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: j6x0tp.c,v 1.25 2021/08/07 16:18:54 thorpej Exp $");

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

#include "opt_j6x0tp.h"

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>
#include <dev/wscons/wskbdvar.h>
#include <dev/wscons/wsksymvar.h>
#include <dev/wscons/wsksymdef.h>
#include <dev/hpc/hpctpanelvar.h>

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

#include <machine/intr.h>

#include <sh3/exception.h>
#include <sh3/intcreg.h>
#include <sh3/pfcreg.h>
#include <sh3/adcreg.h>

#include <sh3/dev/adcvar.h>


#if 0 /* XXX: disabled in favor of local version that uses printf_nolog */
#define DPRINTF_ENABLE
#define DPRINTF_DEBUG	j6x0tp_debug
#define DPRINTF_LEVEL	0
#include <machine/debug.h>
#else
#ifdef J6X0TP_DEBUG
volatile int j6x0tp_debug = 0;
#define DPRINTF_PRINTF		printf_nolog
#define DPRINTF(arg)		if (j6x0tp_debug) DPRINTF_PRINTF arg
#define DPRINTFN(n, arg)	if (j6x0tp_debug > (n)) DPRINTF_PRINTF arg
#else
#define DPRINTF(arg)		((void)0)
#define DPRINTFN(n, arg)	((void)0)
#endif
#endif


/*
 * PFC bits pertinent to Jornada 6x0 touchpanel
 */
#define PHDR_TP_PEN_DOWN	0x08

#define SCPDR_TP_SCAN_ENABLE	0x20
#define SCPDR_TP_SCAN_Y		0x02
#define SCPDR_TP_SCAN_X		0x01

/*
 * A/D converter channels to get x/y from
 */
#define ADC_CHANNEL_TP_Y	1
#define ADC_CHANNEL_TP_X	2

/*
 * Default (read: my device :) raw X/Y values for framebuffer edges.
 * XXX: defopt these?
 */
#define J6X0TP_FB_LEFT		 38
#define J6X0TP_FB_RIGHT		950
#define J6X0TP_FB_TOP		 80
#define J6X0TP_FB_BOTTOM	900

/*
 * Bottom of the n'th hard icon (n = 1..4)
 */
#define J6X0TP_HARD_ICON_MAX_Y(n) \
	(J6X0TP_FB_TOP + ((J6X0TP_FB_BOTTOM - J6X0TP_FB_TOP) / 4) * (n))


struct j6x0tp_softc {
	device_t sc_dev;

#define J6X0TP_WSMOUSE_ENABLED	0x01
#define J6X0TP_WSKBD_ENABLED	0x02
	int sc_enabled;

	int sc_hard_icon;

	struct callout sc_touch_ch;

	device_t sc_wsmousedev;
	device_t sc_wskbddev;

	struct tpcalib_softc sc_tpcalib; /* calibration info for wsmouse */
};


/* config machinery */
static int	j6x0tp_match(device_t, cfdata_t, void *);
static void	j6x0tp_attach(device_t, device_t, void *);

/* wsmouse accessops */
static int	j6x0tp_wsmouse_enable(void *);
static int	j6x0tp_wsmouse_ioctl(void *, u_long, void *, int, lwp_t *);
static void	j6x0tp_wsmouse_disable(void *);

/* wskbd accessops */
static int	j6x0tp_wskbd_enable(void *, int);
static void	j6x0tp_wskbd_set_leds(void *, int);
static int	j6x0tp_wskbd_ioctl(void *, u_long, void *, int, lwp_t *);

/* internal driver routines */
static void	j6x0tp_enable(struct j6x0tp_softc *);
static void	j6x0tp_disable(struct j6x0tp_softc *);
static int	j6x0tp_enable_child(struct j6x0tp_softc *, int, int);
static int	j6x0tp_intr(void *);
static void	j6x0tp_start_polling(void *);
static void	j6x0tp_stop_polling(struct j6x0tp_softc *);
static void	j6x0tp_callout_wsmouse(void *);
static void	j6x0tp_callout_wskbd(void *);
static void	j6x0tp_wsmouse_input(struct j6x0tp_softc *, int, int);
static void	j6x0tp_get_raw_xy(int *, int *);
static int	j6x0tp_get_hard_icon(int, int);


static const struct wsmouse_accessops j6x0tp_accessops = {
	j6x0tp_wsmouse_enable,
	j6x0tp_wsmouse_ioctl,
	j6x0tp_wsmouse_disable
};

static const struct wsmouse_calibcoords j6x0tp_default_calib = {
	0, 0, 639, 239,
	4,
	{{ J6X0TP_FB_LEFT,  J6X0TP_FB_TOP,      0,   0 },
	 { J6X0TP_FB_RIGHT, J6X0TP_FB_TOP,    639,   0 },
	 { J6X0TP_FB_LEFT,  J6X0TP_FB_BOTTOM,   0, 239 },
	 { J6X0TP_FB_RIGHT, J6X0TP_FB_BOTTOM, 639, 239 }}
};

static const struct wskbd_accessops j6x0tp_wskbd_accessops = {
	j6x0tp_wskbd_enable,
	j6x0tp_wskbd_set_leds,
	j6x0tp_wskbd_ioctl
};


#ifndef J6X0TP_SETTINGS_ICON_KEYSYM
#define J6X0TP_SETTINGS_ICON_KEYSYM	KS_Home
#endif
#ifndef J6X0TP_PGUP_ICON_KEYSYM
#define J6X0TP_PGUP_ICON_KEYSYM		KS_Prior
#endif
#ifndef J6X0TP_PGDN_ICON_KEYSYM
#define J6X0TP_PGDN_ICON_KEYSYM		KS_Next
#endif
#ifndef J6X0TP_SWITCH_ICON_KEYSYM
#define J6X0TP_SWITCH_ICON_KEYSYM	KS_End
#endif

static const keysym_t j6x0tp_wskbd_keydesc[] = {
	KS_KEYCODE(1), J6X0TP_SETTINGS_ICON_KEYSYM,
	KS_KEYCODE(2), J6X0TP_PGUP_ICON_KEYSYM,
	KS_KEYCODE(3), J6X0TP_PGDN_ICON_KEYSYM,
	KS_KEYCODE(4), J6X0TP_SWITCH_ICON_KEYSYM
};

static const struct wscons_keydesc j6x0tp_wskbd_keydesctab[] = {
	{ KB_US, 0,
	  sizeof(j6x0tp_wskbd_keydesc)/sizeof(keysym_t),
	  j6x0tp_wskbd_keydesc
	},
	{0, 0, 0, 0}
};

static const struct wskbd_mapdata j6x0tp_wskbd_keymapdata = {
        j6x0tp_wskbd_keydesctab, KB_US
};


CFATTACH_DECL_NEW(j6x0tp, sizeof(struct j6x0tp_softc),
    j6x0tp_match, j6x0tp_attach, NULL, NULL);


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

	/*
	 * XXX: platid_mask_MACH_HP_LX also matches 360LX.  It's not
	 * confirmed whether touch panel in 360LX is connected this
	 * way.  We may need to regroup platid masks.
	 */
	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX)
	    && !platid_match(&platid, &platid_mask_MACH_HP_LX))
		return (0);

	if (strcmp(cf->cf_name, "j6x0tp") != 0)
		return (0);

	return (1);
}


static void
j6x0tp_attach(device_t parent, device_t self, void *aux)
{
	struct j6x0tp_softc *sc;
	struct wsmousedev_attach_args wsma;
	struct wskbddev_attach_args wska;

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

	sc = device_private(self);
	sc->sc_dev = self;

	sc->sc_enabled = 0;
	sc->sc_hard_icon = 0;

	/* touch-panel as a pointing device */
	wsma.accessops = &j6x0tp_accessops;
	wsma.accesscookie = sc;

	sc->sc_wsmousedev = config_found(self, &wsma, wsmousedevprint,
	    CFARGS(.iattr = "wsmousedev"));
	if (sc->sc_wsmousedev == NULL)
		return;

	/* on-screen "hard icons" as a keyboard device */
	wska.console = 0;
	wska.keymap = &j6x0tp_wskbd_keymapdata;
	wska.accessops = &j6x0tp_wskbd_accessops;
	wska.accesscookie = sc;

	sc->sc_wskbddev = config_found(self, &wska, wskbddevprint,
	    CFARGS(.iattr = "wskbddev"));

	/* init calibration, set default parameters */
	tpcalib_init(&sc->sc_tpcalib);
	tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
		      (void *)__UNCONST(&j6x0tp_default_calib), 0, 0);

	/* used when in polling mode */
	callout_init(&sc->sc_touch_ch, 0);

	/* establish interrupt handler, but disable until opened */
	intc_intr_establish(SH7709_INTEVT2_IRQ3, IST_EDGE, IPL_TTY,
			    j6x0tp_intr, sc);
	intc_intr_disable(SH7709_INTEVT2_IRQ3);

	if (!pmf_device_register(self, NULL, NULL))
		aprint_error_dev(self, "unable to establish power handler\n");
}


/*
 * Enable touch panel:  we start in interrupt mode.
 * Must be called as spltty().
 */
static void
j6x0tp_enable(struct j6x0tp_softc *sc)
{

	DPRINTFN(2, ("%s: enable\n", device_xname(sc->sc_dev)));
	intc_intr_enable(SH7709_INTEVT2_IRQ3);
}


/*
 * Disable touch panel: disable interrupt, cancel pending callout.
 * Must be called as spltty().
 */
static void
j6x0tp_disable(struct j6x0tp_softc *sc)
{

	DPRINTFN(2, ("%s: disable\n", device_xname(sc->sc_dev)));
	intc_intr_disable(SH7709_INTEVT2_IRQ3);
	callout_stop(&sc->sc_touch_ch);
}


static int
j6x0tp_enable_child(struct j6x0tp_softc *sc, int child, int on)
{
	int s = spltty();

	if (on) {
		if (!sc->sc_enabled)
			j6x0tp_enable(sc);
		sc->sc_enabled |= child;
	} else {
		sc->sc_enabled &= ~child;
		if (!sc->sc_enabled)
			j6x0tp_disable(sc);
	}

	splx(s);
	return (0);
}


static int
j6x0tp_wsmouse_enable(void *arg)
{
	struct j6x0tp_softc *sc = arg;

	DPRINTFN(1, ("%s: wsmouse enable\n", device_xname(sc->sc_dev)));
	return (j6x0tp_enable_child(sc, J6X0TP_WSMOUSE_ENABLED, 1));
}


static void
j6x0tp_wsmouse_disable(void *arg)
{
	struct j6x0tp_softc *sc = arg;

	DPRINTFN(1, ("%s: wsmouse disable\n", device_xname(sc->sc_dev)));
	j6x0tp_enable_child(sc, J6X0TP_WSMOUSE_ENABLED, 0);
}


static int
j6x0tp_wskbd_enable(void *arg, int on)
{
	struct j6x0tp_softc *sc = arg;

	DPRINTFN(1, ("%s: wskbd %sable\n", device_xname(sc->sc_dev),
		     on ? "en" : "dis"));
	return (j6x0tp_enable_child(sc, J6X0TP_WSKBD_ENABLED, on));
}


static int
j6x0tp_intr(void *arg)
{
	struct j6x0tp_softc *sc = arg;

	uint8_t irr0;
	uint8_t phdr, touched;
	unsigned int steady, tremor_timeout;

	irr0 = _reg_read_1(SH7709_IRR0);
	if ((irr0 & IRR0_IRQ3) == 0) {
#ifdef DIAGNOSTIC
		printf("%s: irr0 %02x?\n", device_xname(sc->sc_dev), irr0);
#endif
		return (0);
	}

	if (!sc->sc_enabled) {
		DPRINTFN(1, ("%s: intr: !sc_enabled\n",
			     device_xname(sc->sc_dev)));
		intc_intr_disable(SH7709_INTEVT2_IRQ3);
		goto served;
	}


	/*
	 * Number of times the "touched" bit should be read
	 * consecutively.
	 */
#	define TREMOR_THRESHOLD 0x300

	steady = 0;
	tremor_timeout = TREMOR_THRESHOLD * 16;	/* XXX: arbitrary */
	touched = PHDR_TP_PEN_DOWN;	/* we start with "touched" state */

	do {
		phdr = _reg_read_1(SH7709_PHDR);

		if ((phdr & PHDR_TP_PEN_DOWN) == touched)
			++steady;
		else {
			steady = 0;
			touched = phdr & PHDR_TP_PEN_DOWN;
		}

		if (--tremor_timeout == 0) {
			DPRINTF(("%s: tremor timeout!\n",
				 device_xname(sc->sc_dev)));
			goto served;
		}
	} while (steady < TREMOR_THRESHOLD);

	if (touched) {
		intc_intr_disable(SH7709_INTEVT2_IRQ3);

		/*
		 * ADC readings are not stable yet, so schedule
		 * callout instead of accessing ADC from the interrupt
		 * handler only to immediately delay().
		 */
		callout_reset(&sc->sc_touch_ch, hz/32,
			      j6x0tp_start_polling, sc);
	} else
		DPRINTFN(1, ("%s: tremor\n", device_xname(sc->sc_dev)));
  served:
	/* clear the interrupt (XXX: protect access?) */
	_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ3);

	return (1);
}


/*
 * Called from the interrupt handler at spltty() upon first touch.
 * Decide if we are going to report this touch as a mouse click/drag
 * or as a key press.
 */
static void
j6x0tp_start_polling(void *arg)
{
	struct j6x0tp_softc *sc = arg;
	uint8_t phdr;
	int do_mouse, do_kbd;
	int rawx, rawy;
	int icon;

	phdr = _reg_read_1(SH7709_PHDR);
	if ((phdr & PHDR_TP_PEN_DOWN) == 0) {
		DPRINTFN(2, ("%s: start: pen is not down\n",
			     device_xname(sc->sc_dev)));
		j6x0tp_stop_polling(sc);
	}

	j6x0tp_get_raw_xy(&rawx, &rawy);
	DPRINTFN(2, ("%s: start: %4d %4d -> ",
		     device_xname(sc->sc_dev), rawx, rawy));

	do_mouse = sc->sc_enabled & J6X0TP_WSMOUSE_ENABLED;
#ifdef J6X0TP_WSMOUSE_EXCLUSIVE
	if (do_mouse)
		do_kbd = 0;
	else
#endif
		do_kbd = sc->sc_enabled & J6X0TP_WSKBD_ENABLED;

	icon = 0;
	if (do_kbd)
		icon = j6x0tp_get_hard_icon(rawx, rawy);

	if (icon != 0) {
		DPRINTFN(2, ("icon %d\n", icon));
		sc->sc_hard_icon = icon;
		wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, icon);
		callout_reset(&sc->sc_touch_ch, hz/32,
			      j6x0tp_callout_wskbd, sc);
	} else if (do_mouse) {
		DPRINTFN(2, ("mouse\n"));
		j6x0tp_wsmouse_input(sc, rawx, rawy);
		callout_reset(&sc->sc_touch_ch, hz/32,
			      j6x0tp_callout_wsmouse, sc);
	} else {
		DPRINTFN(2, ("ignore\n"));
		j6x0tp_stop_polling(sc);
	}
}


/*
 * Re-enable touch panel interrupt.
 * Called as spltty() when polling code detects pen-up.
 */
static void
j6x0tp_stop_polling(struct j6x0tp_softc *sc)
{
	uint8_t irr0;

	DPRINTFN(2, ("%s: stop\n", device_xname(sc->sc_dev)));

	/* clear pending interrupt signal before re-enabling the interrupt */
	irr0 = _reg_read_1(SH7709_IRR0);
	if ((irr0 & IRR0_IRQ3) != 0)
		_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ3);

	intc_intr_enable(SH7709_INTEVT2_IRQ3);
}


/*
 * We are reporting this touch as a keyboard event.
 * Poll touch screen waiting for pen-up.
 */
static void
j6x0tp_callout_wskbd(void *arg)
{
	struct j6x0tp_softc *sc = arg;
	uint8_t phdr;
	int s;

	s = spltty();

	if (!sc->sc_enabled) {
		DPRINTFN(1, ("%s: wskbd callout: !sc_enabled\n",
			     device_xname(sc->sc_dev)));
		splx(s);
		return;
	}

	phdr = _reg_read_1(SH7709_PHDR);
	if ((phdr & PHDR_TP_PEN_DOWN) != 0) {
		/*
		 * Pen is still down, continue polling.  Wskbd's
		 * auto-repeat takes care of repeating the key.
		 */
		callout_schedule(&sc->sc_touch_ch, hz/32);
	} else {
		wskbd_input(sc->sc_wskbddev,
			    WSCONS_EVENT_KEY_UP, sc->sc_hard_icon);
		j6x0tp_stop_polling(sc);
	}
	splx(s);
}


/*
 * We are reporting this touch as a mouse click/drag.
 */
static void
j6x0tp_callout_wsmouse(void *arg)
{
	struct j6x0tp_softc *sc = arg;
	uint8_t phdr;
	int rawx, rawy;
	int s;

	s = spltty();

	if (!sc->sc_enabled) {
		DPRINTFN(1, ("%s: wsmouse callout: !sc_enabled\n",
			     device_xname(sc->sc_dev)));
		splx(s);
		return;
	}

	phdr = _reg_read_1(SH7709_PHDR);
	if ((phdr & PHDR_TP_PEN_DOWN) != 0) {
		j6x0tp_get_raw_xy(&rawx, &rawy);
		j6x0tp_wsmouse_input(sc, rawx, rawy); /* mouse dragged */
		callout_schedule(&sc->sc_touch_ch, hz/32);
	} else {
		wsmouse_input(sc->sc_wsmousedev, 0, 0, 0, 0, 0, /* button up */
			      WSMOUSE_INPUT_DELTA);
		j6x0tp_stop_polling(sc);
	}
	splx(s);
}


/*
 * Report mouse click/drag.
 */
static void
j6x0tp_wsmouse_input(struct j6x0tp_softc *sc, int rawx, int rawy)
{
	int x, y;

	tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y);
		
	DPRINTFN(3, ("%s: %4d %4d -> %3d %3d\n",
		     device_xname(sc->sc_dev), rawx, rawy, x, y));

	wsmouse_input(sc->sc_wsmousedev,
			1,	/* button */
			x, y, 0, 0,
			WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
}


/*
 * Read raw X/Y coordinates from the ADC.
 * XXX: protect accesses to SCPDR?
 */
static void
j6x0tp_get_raw_xy(int *rawxp, int *rawyp)
{
	uint8_t scpdr;

	/* Y axis */
	scpdr = _reg_read_1(SH7709_SCPDR);
	scpdr |=  SCPDR_TP_SCAN_ENABLE;
	scpdr &= ~SCPDR_TP_SCAN_Y; /* pull low to scan */
	_reg_write_1(SH7709_SCPDR, scpdr);
	delay(10);

	*rawyp = adc_sample_channel(ADC_CHANNEL_TP_Y);

	/* X axis */
	scpdr = _reg_read_1(SH7709_SCPDR);
	scpdr |=  SCPDR_TP_SCAN_Y;
	scpdr &= ~SCPDR_TP_SCAN_X; /* pull low to scan */
	_reg_write_1(SH7709_SCPDR, scpdr);
	delay(10);

	*rawxp = adc_sample_channel(ADC_CHANNEL_TP_X);

	/* restore SCPDR */
	scpdr = _reg_read_1(SH7709_SCPDR);
	scpdr |=  SCPDR_TP_SCAN_X;
	scpdr &= ~SCPDR_TP_SCAN_ENABLE;
	_reg_write_1(SH7709_SCPDR, scpdr);
}


/*
 * Check if the (rawx, rawy) is inside one of the 4 hard icons.
 * Return the icon number 1..4, or 0 if not inside an icon.
 */
static int
j6x0tp_get_hard_icon(int rawx, int rawy)
{
	if (rawx <= J6X0TP_FB_RIGHT)
		return (0);

	if (rawy < J6X0TP_HARD_ICON_MAX_Y(1))
		return (1);
	else if (rawy < J6X0TP_HARD_ICON_MAX_Y(2))
		return (2);
	else if (rawy < J6X0TP_HARD_ICON_MAX_Y(3))
		return (3);
	else
		return (4);
}


static int
j6x0tp_wsmouse_ioctl(void *arg, u_long cmd, void *data, int flag, lwp_t *l)
{
	struct j6x0tp_softc *sc = arg;

	return hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
}


static int
j6x0tp_wskbd_ioctl(void *arg, u_long cmd, void *data, int flag, lwp_t *l)
{
	/* struct j6x0tp_softc *sc = arg; */

	switch (cmd) {
	case WSKBDIO_GTYPE:
		*(int *)data = WSKBD_TYPE_HPC_BTN; /* may be use new type? */
		return (0);

	case WSKBDIO_GETLEDS:
		*(int *)data = 0;
		return (0);

	default:
		return (EPASSTHROUGH);
	}
}


static void
j6x0tp_wskbd_set_leds(void *arg, int leds)
{

	/* nothing to do*/
	return;
}