Home | History | Annotate | Line # | Download | only in dev
psh3tp.c revision 1.3.8.2
      1  1.3.8.2  skrll /*	$NetBSD: psh3tp.c,v 1.3.8.2 2005/11/10 13:56:31 skrll Exp $	*/
      2  1.3.8.2  skrll /*
      3  1.3.8.2  skrll  * Copyright (c) 2005 KIYOHARA Takashi
      4  1.3.8.2  skrll  * All rights reserved.
      5  1.3.8.2  skrll  *
      6  1.3.8.2  skrll  * Redistribution and use in source and binary forms, with or without
      7  1.3.8.2  skrll  * modification, are permitted provided that the following conditions
      8  1.3.8.2  skrll  * are met:
      9  1.3.8.2  skrll  * 1. Redistributions of source code must retain the above copyright
     10  1.3.8.2  skrll  *    notice, this list of conditions and the following disclaimer.
     11  1.3.8.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.3.8.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     13  1.3.8.2  skrll  *    documentation and/or other materials provided with the distribution.
     14  1.3.8.2  skrll  *
     15  1.3.8.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.3.8.2  skrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.3.8.2  skrll  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.3.8.2  skrll  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  1.3.8.2  skrll  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  1.3.8.2  skrll  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.3.8.2  skrll  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.3.8.2  skrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  1.3.8.2  skrll  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  1.3.8.2  skrll  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.3.8.2  skrll  * POSSIBILITY OF SUCH DAMAGE.
     26  1.3.8.2  skrll  *
     27  1.3.8.2  skrll  */
     28  1.3.8.2  skrll 
     29  1.3.8.2  skrll #include <sys/cdefs.h>
     30  1.3.8.2  skrll 
     31  1.3.8.2  skrll #include <sys/param.h>
     32  1.3.8.2  skrll #include <sys/kernel.h>
     33  1.3.8.2  skrll #include <sys/device.h>
     34  1.3.8.2  skrll #include <sys/malloc.h>
     35  1.3.8.2  skrll #include <sys/systm.h>
     36  1.3.8.2  skrll #include <sys/callout.h>
     37  1.3.8.2  skrll 
     38  1.3.8.2  skrll #include "opt_psh3tp.h"
     39  1.3.8.2  skrll 
     40  1.3.8.2  skrll #include <dev/wscons/wsconsio.h>
     41  1.3.8.2  skrll #include <dev/wscons/wsmousevar.h>
     42  1.3.8.2  skrll #include <dev/hpc/hpctpanelvar.h>
     43  1.3.8.2  skrll 
     44  1.3.8.2  skrll #include <machine/platid.h>
     45  1.3.8.2  skrll #include <machine/platid_mask.h>
     46  1.3.8.2  skrll 
     47  1.3.8.2  skrll #include <machine/intr.h>
     48  1.3.8.2  skrll 
     49  1.3.8.2  skrll #include <sh3/exception.h>
     50  1.3.8.2  skrll #include <sh3/intcreg.h>
     51  1.3.8.2  skrll #include <sh3/pfcreg.h>
     52  1.3.8.2  skrll #include <sh3/adcreg.h>
     53  1.3.8.2  skrll 
     54  1.3.8.2  skrll #include <sh3/dev/adcvar.h>
     55  1.3.8.2  skrll 
     56  1.3.8.2  skrll 
     57  1.3.8.2  skrll #ifdef PSH3TP_DEBUG
     58  1.3.8.2  skrll volatile int psh3tp_debug = 4;
     59  1.3.8.2  skrll #define DPRINTF_PRINTF		printf_nolog
     60  1.3.8.2  skrll #define DPRINTF(arg)		if (psh3tp_debug) DPRINTF_PRINTF arg
     61  1.3.8.2  skrll #define DPRINTFN(n, arg)	if (psh3tp_debug > (n)) DPRINTF_PRINTF arg
     62  1.3.8.2  skrll #else
     63  1.3.8.2  skrll #define DPRINTF(arg)		((void)0)
     64  1.3.8.2  skrll #define DPRINTFN(n, arg)	((void)0)
     65  1.3.8.2  skrll #endif
     66  1.3.8.2  skrll 
     67  1.3.8.2  skrll 
     68  1.3.8.2  skrll /*
     69  1.3.8.2  skrll  * PFC bits pertinent to PERSONA HPW-50PA touch-panel
     70  1.3.8.2  skrll  */
     71  1.3.8.2  skrll #define PHDR_TP_PEN_UP		0x40
     72  1.3.8.2  skrll #define SCPDR_TP_SCAN_ENABLE	0x20
     73  1.3.8.2  skrll #define SCPDR_TP_SCAN_DISABLE	0x01
     74  1.3.8.2  skrll #define SCPDR_TP_SCAN_X		0x06
     75  1.3.8.2  skrll #define SCPDR_TP_SCAN_Y		0x09
     76  1.3.8.2  skrll 
     77  1.3.8.2  skrll /*
     78  1.3.8.2  skrll  * A/D converter channels to get x/y from
     79  1.3.8.2  skrll  */
     80  1.3.8.2  skrll #define ADC_CHANNEL_TP_X	1
     81  1.3.8.2  skrll #define ADC_CHANNEL_TP_Y	0
     82  1.3.8.2  skrll 
     83  1.3.8.2  skrll /*
     84  1.3.8.2  skrll  * Default (read: my device) raw X/Y values for framebuffer edges.
     85  1.3.8.2  skrll  */
     86  1.3.8.2  skrll #define PSH3TP_FB_RIGHT		 56
     87  1.3.8.2  skrll #define PSH3TP_FB_LEFT		969
     88  1.3.8.2  skrll #define PSH3TP_FB_TOP		848
     89  1.3.8.2  skrll #define PSH3TP_FB_BOTTOM	121
     90  1.3.8.2  skrll 
     91  1.3.8.2  skrll 
     92  1.3.8.2  skrll struct psh3tp_softc {
     93  1.3.8.2  skrll 	struct device sc_dev;
     94  1.3.8.2  skrll 
     95  1.3.8.2  skrll #define PSH3TP_WSMOUSE_ENABLED	0x01
     96  1.3.8.2  skrll 	int sc_enabled;
     97  1.3.8.2  skrll 	struct callout sc_touch_ch;
     98  1.3.8.2  skrll 	struct device *sc_wsmousedev;
     99  1.3.8.2  skrll 	struct tpcalib_softc sc_tpcalib; /* calibration info for wsmouse */
    100  1.3.8.2  skrll };
    101  1.3.8.2  skrll 
    102  1.3.8.2  skrll 
    103  1.3.8.2  skrll /* config machinery */
    104  1.3.8.2  skrll static int psh3tp_match(struct device *, struct cfdata *, void *);
    105  1.3.8.2  skrll static void psh3tp_attach(struct device *, struct device *, void *);
    106  1.3.8.2  skrll 
    107  1.3.8.2  skrll /* wsmouse accessops */
    108  1.3.8.2  skrll static int psh3tp_wsmouse_enable(void *);
    109  1.3.8.2  skrll static int psh3tp_wsmouse_ioctl(void *, u_long, caddr_t, int, struct proc *);
    110  1.3.8.2  skrll static void psh3tp_wsmouse_disable(void *);
    111  1.3.8.2  skrll 
    112  1.3.8.2  skrll /* internal driver routines */
    113  1.3.8.2  skrll static void psh3tp_enable(struct psh3tp_softc *);
    114  1.3.8.2  skrll static void psh3tp_disable(struct psh3tp_softc *);
    115  1.3.8.2  skrll static int psh3tp_set_enable(struct psh3tp_softc *, int, int);
    116  1.3.8.2  skrll static int psh3tp_intr(void *);
    117  1.3.8.2  skrll static void psh3tp_start_polling(void *);
    118  1.3.8.2  skrll static void psh3tp_stop_polling(struct psh3tp_softc *);
    119  1.3.8.2  skrll static void psh3tp_callout_wsmouse(void *);
    120  1.3.8.2  skrll static void psh3tp_wsmouse_input(struct psh3tp_softc *, int, int);
    121  1.3.8.2  skrll static void psh3tp_get_raw_xy(int *, int *);
    122  1.3.8.2  skrll 
    123  1.3.8.2  skrll 
    124  1.3.8.2  skrll const struct wsmouse_accessops psh3tp_accessops = {
    125  1.3.8.2  skrll 	psh3tp_wsmouse_enable,
    126  1.3.8.2  skrll 	psh3tp_wsmouse_ioctl,
    127  1.3.8.2  skrll 	psh3tp_wsmouse_disable
    128  1.3.8.2  skrll };
    129  1.3.8.2  skrll 
    130  1.3.8.2  skrll static const struct wsmouse_calibcoords psh3tp_default_calib = {
    131  1.3.8.2  skrll 	0, 0, 639, 239,
    132  1.3.8.2  skrll 	4,
    133  1.3.8.2  skrll 	{{ PSH3TP_FB_LEFT,  PSH3TP_FB_TOP,      0,   0 },
    134  1.3.8.2  skrll 	 { PSH3TP_FB_RIGHT, PSH3TP_FB_TOP,    639,   0 },
    135  1.3.8.2  skrll 	 { PSH3TP_FB_LEFT,  PSH3TP_FB_BOTTOM,   0, 239 },
    136  1.3.8.2  skrll 	 { PSH3TP_FB_RIGHT, PSH3TP_FB_BOTTOM, 639, 239 }}
    137  1.3.8.2  skrll };
    138  1.3.8.2  skrll 
    139  1.3.8.2  skrll 
    140  1.3.8.2  skrll CFATTACH_DECL(psh3tp, sizeof(struct psh3tp_softc),
    141  1.3.8.2  skrll     psh3tp_match, psh3tp_attach, NULL, NULL);
    142  1.3.8.2  skrll 
    143  1.3.8.2  skrll 
    144  1.3.8.2  skrll static int
    145  1.3.8.2  skrll psh3tp_match(struct device *parent, struct cfdata *cf, void *aux)
    146  1.3.8.2  skrll {
    147  1.3.8.2  skrll 
    148  1.3.8.2  skrll 	if (!platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
    149  1.3.8.2  skrll 		return (0);
    150  1.3.8.2  skrll 
    151  1.3.8.2  skrll 	if (strcmp(cf->cf_name, "psh3tp") != 0)
    152  1.3.8.2  skrll 		return (0);
    153  1.3.8.2  skrll 
    154  1.3.8.2  skrll 	return (1);
    155  1.3.8.2  skrll }
    156  1.3.8.2  skrll 
    157  1.3.8.2  skrll 
    158  1.3.8.2  skrll /*
    159  1.3.8.2  skrll  * Attach the touch panel driver and its wsmouse child.
    160  1.3.8.2  skrll  *
    161  1.3.8.2  skrll  * Note that we have to use submatch to distinguish between child because
    162  1.3.8.2  skrll  * wsmouse_match matches unconditionally.
    163  1.3.8.2  skrll  */
    164  1.3.8.2  skrll static void
    165  1.3.8.2  skrll psh3tp_attach(struct device *parent, struct device *self, void *aux)
    166  1.3.8.2  skrll {
    167  1.3.8.2  skrll 	struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
    168  1.3.8.2  skrll 	struct wsmousedev_attach_args wsma;
    169  1.3.8.2  skrll 
    170  1.3.8.2  skrll 	printf("\n");
    171  1.3.8.2  skrll 
    172  1.3.8.2  skrll 	sc->sc_enabled = 0;
    173  1.3.8.2  skrll 
    174  1.3.8.2  skrll 	/* touch-panel as a pointing device */
    175  1.3.8.2  skrll 	wsma.accessops = &psh3tp_accessops;
    176  1.3.8.2  skrll 	wsma.accesscookie = sc;
    177  1.3.8.2  skrll 
    178  1.3.8.2  skrll 	sc->sc_wsmousedev = config_found_ia(
    179  1.3.8.2  skrll 	    self, "wsmousedev", &wsma, wsmousedevprint);
    180  1.3.8.2  skrll 	if (sc->sc_wsmousedev == NULL)
    181  1.3.8.2  skrll 		return;
    182  1.3.8.2  skrll 
    183  1.3.8.2  skrll 	/* init calibration, set default parameters */
    184  1.3.8.2  skrll 	tpcalib_init(&sc->sc_tpcalib);
    185  1.3.8.2  skrll 	tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
    186  1.3.8.2  skrll 		      (caddr_t)__UNCONST(&psh3tp_default_calib), 0, 0);
    187  1.3.8.2  skrll 
    188  1.3.8.2  skrll 	/* used when in polling mode */
    189  1.3.8.2  skrll 	callout_init(&sc->sc_touch_ch);
    190  1.3.8.2  skrll 
    191  1.3.8.2  skrll 	/* establish interrupt handler, but disable until opened */
    192  1.3.8.2  skrll 	intc_intr_establish(SH7709_INTEVT2_IRQ2,
    193  1.3.8.2  skrll 	    IST_EDGE, IPL_TTY, psh3tp_intr, sc);
    194  1.3.8.2  skrll 	intc_intr_disable(SH7709_INTEVT2_IRQ2);
    195  1.3.8.2  skrll }
    196  1.3.8.2  skrll 
    197  1.3.8.2  skrll 
    198  1.3.8.2  skrll /*
    199  1.3.8.2  skrll  * Enable touch panel:  we start in interrupt mode.
    200  1.3.8.2  skrll  * Must be called at spltty().
    201  1.3.8.2  skrll  */
    202  1.3.8.2  skrll static void
    203  1.3.8.2  skrll psh3tp_enable(struct psh3tp_softc *sc)
    204  1.3.8.2  skrll {
    205  1.3.8.2  skrll 
    206  1.3.8.2  skrll 	DPRINTFN(2, ("%s: enable\n", sc->sc_dev.dv_xname));
    207  1.3.8.2  skrll 	intc_intr_enable(SH7709_INTEVT2_IRQ2);
    208  1.3.8.2  skrll }
    209  1.3.8.2  skrll 
    210  1.3.8.2  skrll 
    211  1.3.8.2  skrll /*
    212  1.3.8.2  skrll  * Disable touch panel: disable interrupt, cancel pending callout.
    213  1.3.8.2  skrll  * Must be called at spltty().
    214  1.3.8.2  skrll  */
    215  1.3.8.2  skrll static void
    216  1.3.8.2  skrll psh3tp_disable(struct psh3tp_softc *sc)
    217  1.3.8.2  skrll {
    218  1.3.8.2  skrll 
    219  1.3.8.2  skrll 	DPRINTFN(2, ("%s: disable\n", sc->sc_dev.dv_xname));
    220  1.3.8.2  skrll 	intc_intr_disable(SH7709_INTEVT2_IRQ2);
    221  1.3.8.2  skrll 	callout_stop(&sc->sc_touch_ch);
    222  1.3.8.2  skrll }
    223  1.3.8.2  skrll 
    224  1.3.8.2  skrll 
    225  1.3.8.2  skrll static int
    226  1.3.8.2  skrll psh3tp_set_enable(struct psh3tp_softc *sc, int on, int child)
    227  1.3.8.2  skrll {
    228  1.3.8.2  skrll 	int s = spltty();
    229  1.3.8.2  skrll 
    230  1.3.8.2  skrll 	if (on) {
    231  1.3.8.2  skrll 		if (!sc->sc_enabled)
    232  1.3.8.2  skrll 			psh3tp_enable(sc);
    233  1.3.8.2  skrll 		sc->sc_enabled |= child;
    234  1.3.8.2  skrll 	} else {
    235  1.3.8.2  skrll 		sc->sc_enabled &= ~child;
    236  1.3.8.2  skrll 		if (!sc->sc_enabled)
    237  1.3.8.2  skrll 			psh3tp_disable(sc);
    238  1.3.8.2  skrll 	}
    239  1.3.8.2  skrll 
    240  1.3.8.2  skrll 	splx(s);
    241  1.3.8.2  skrll 	return (0);
    242  1.3.8.2  skrll }
    243  1.3.8.2  skrll 
    244  1.3.8.2  skrll 
    245  1.3.8.2  skrll static int
    246  1.3.8.2  skrll psh3tp_wsmouse_enable(void *self)
    247  1.3.8.2  skrll {
    248  1.3.8.2  skrll 	struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
    249  1.3.8.2  skrll 
    250  1.3.8.2  skrll 	DPRINTFN(1, ("%s: wsmouse enable\n", sc->sc_dev.dv_xname));
    251  1.3.8.2  skrll 	return (psh3tp_set_enable(sc, 1, PSH3TP_WSMOUSE_ENABLED));
    252  1.3.8.2  skrll }
    253  1.3.8.2  skrll 
    254  1.3.8.2  skrll 
    255  1.3.8.2  skrll static void
    256  1.3.8.2  skrll psh3tp_wsmouse_disable(void *self)
    257  1.3.8.2  skrll {
    258  1.3.8.2  skrll 	struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
    259  1.3.8.2  skrll 
    260  1.3.8.2  skrll 	DPRINTFN(1, ("%s: wsmouse disable\n", sc->sc_dev.dv_xname));
    261  1.3.8.2  skrll 	psh3tp_set_enable(sc, 0, PSH3TP_WSMOUSE_ENABLED);
    262  1.3.8.2  skrll }
    263  1.3.8.2  skrll 
    264  1.3.8.2  skrll 
    265  1.3.8.2  skrll static int
    266  1.3.8.2  skrll psh3tp_intr(void *self)
    267  1.3.8.2  skrll {
    268  1.3.8.2  skrll 	struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
    269  1.3.8.2  skrll 
    270  1.3.8.2  skrll 	uint8_t irr0;
    271  1.3.8.2  skrll 	uint8_t phdr, touched;
    272  1.3.8.2  skrll 	unsigned int steady, tremor_timeout;
    273  1.3.8.2  skrll 
    274  1.3.8.2  skrll 	irr0 = _reg_read_1(SH7709_IRR0);
    275  1.3.8.2  skrll 	if ((irr0 & IRR0_IRQ2) == 0) {
    276  1.3.8.2  skrll #ifdef DIAGNOSTIC
    277  1.3.8.2  skrll 		printf("%s: irr0 %02x?\n", sc->sc_dev.dv_xname, irr0);
    278  1.3.8.2  skrll #endif
    279  1.3.8.2  skrll 		return (0);
    280  1.3.8.2  skrll 	}
    281  1.3.8.2  skrll 
    282  1.3.8.2  skrll 	if (!sc->sc_enabled) {
    283  1.3.8.2  skrll 		DPRINTFN(1, ("%s: intr: !sc_enabled\n", sc->sc_dev.dv_xname));
    284  1.3.8.2  skrll 		intc_intr_disable(SH7709_INTEVT2_IRQ2);
    285  1.3.8.2  skrll 		goto served;
    286  1.3.8.2  skrll 	}
    287  1.3.8.2  skrll 
    288  1.3.8.2  skrll 	/*
    289  1.3.8.2  skrll 	 * Number of times the "touched" bit should be read
    290  1.3.8.2  skrll 	 * consecutively.
    291  1.3.8.2  skrll 	 */
    292  1.3.8.2  skrll #define TREMOR_THRESHOLD 0x300
    293  1.3.8.2  skrll 	steady = 0;
    294  1.3.8.2  skrll 	tremor_timeout = TREMOR_THRESHOLD * 16;	/* XXX: arbitrary */
    295  1.3.8.2  skrll 	touched = TRUE;		/* we start with "touched" state */
    296  1.3.8.2  skrll 
    297  1.3.8.2  skrll 	do {
    298  1.3.8.2  skrll 		uint8_t state;
    299  1.3.8.2  skrll 
    300  1.3.8.2  skrll 		phdr = _reg_read_1(SH7709_PHDR);
    301  1.3.8.2  skrll 		state = ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP);
    302  1.3.8.2  skrll 
    303  1.3.8.2  skrll 		if (state == touched)
    304  1.3.8.2  skrll 			++steady;
    305  1.3.8.2  skrll 		else {
    306  1.3.8.2  skrll 			steady = 0;
    307  1.3.8.2  skrll 			touched = state;
    308  1.3.8.2  skrll 		}
    309  1.3.8.2  skrll 
    310  1.3.8.2  skrll 		if (--tremor_timeout == 0) {
    311  1.3.8.2  skrll 			DPRINTF(("%s: tremor timeout!\n", sc->sc_dev.dv_xname));
    312  1.3.8.2  skrll 			goto served;
    313  1.3.8.2  skrll 		}
    314  1.3.8.2  skrll 	} while (steady < TREMOR_THRESHOLD);
    315  1.3.8.2  skrll 
    316  1.3.8.2  skrll 	if (touched) {
    317  1.3.8.2  skrll 		intc_intr_disable(SH7709_INTEVT2_IRQ2);
    318  1.3.8.2  skrll 
    319  1.3.8.2  skrll 		/*
    320  1.3.8.2  skrll 		 * ADC readings are not stable yet, so schedule
    321  1.3.8.2  skrll 		 * callout instead of accessing ADC from the interrupt
    322  1.3.8.2  skrll 		 * handler only to immediately delay().
    323  1.3.8.2  skrll 		 */
    324  1.3.8.2  skrll 		callout_reset(&sc->sc_touch_ch,
    325  1.3.8.2  skrll 		    hz/32, psh3tp_start_polling, sc);
    326  1.3.8.2  skrll 	} else
    327  1.3.8.2  skrll 		DPRINTFN(1, ("%s: tremor\n", sc->sc_dev.dv_xname));
    328  1.3.8.2  skrll served:
    329  1.3.8.2  skrll 	/* clear the interrupt */
    330  1.3.8.2  skrll 	_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2);
    331  1.3.8.2  skrll 
    332  1.3.8.2  skrll 	return (1);
    333  1.3.8.2  skrll }
    334  1.3.8.2  skrll 
    335  1.3.8.2  skrll 
    336  1.3.8.2  skrll /*
    337  1.3.8.2  skrll  * Called from the interrupt handler at spltty() upon first touch.
    338  1.3.8.2  skrll  * Decide if we are going to report this touch as a mouse click/drag.
    339  1.3.8.2  skrll  */
    340  1.3.8.2  skrll static void
    341  1.3.8.2  skrll psh3tp_start_polling(void *self)
    342  1.3.8.2  skrll {
    343  1.3.8.2  skrll 	struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
    344  1.3.8.2  skrll 	uint8_t phdr;
    345  1.3.8.2  skrll 	int rawx, rawy;
    346  1.3.8.2  skrll 
    347  1.3.8.2  skrll 	phdr = _reg_read_1(SH7709_PHDR);
    348  1.3.8.2  skrll 	if ((phdr & PHDR_TP_PEN_UP) == PHDR_TP_PEN_UP) {
    349  1.3.8.2  skrll 		DPRINTFN(2, ("%s: start: pen is not down\n",
    350  1.3.8.2  skrll 		    sc->sc_dev.dv_xname));
    351  1.3.8.2  skrll 		psh3tp_stop_polling(sc);
    352  1.3.8.2  skrll 		return;
    353  1.3.8.2  skrll 	}
    354  1.3.8.2  skrll 
    355  1.3.8.2  skrll 	psh3tp_get_raw_xy(&rawx, &rawy);
    356  1.3.8.2  skrll 	DPRINTFN(2, ("%s: start: %4d %4d -> ",
    357  1.3.8.2  skrll 	    sc->sc_dev.dv_xname, rawx, rawy));
    358  1.3.8.2  skrll 
    359  1.3.8.2  skrll 	if (sc->sc_enabled & PSH3TP_WSMOUSE_ENABLED) {
    360  1.3.8.2  skrll 		DPRINTFN(2, ("mouse\n"));
    361  1.3.8.2  skrll 		psh3tp_wsmouse_input(sc, rawx, rawy);
    362  1.3.8.2  skrll 		callout_reset(&sc->sc_touch_ch,
    363  1.3.8.2  skrll 		    hz/32, psh3tp_callout_wsmouse, sc);
    364  1.3.8.2  skrll 	} else {
    365  1.3.8.2  skrll 		DPRINTFN(2, ("ignore\n"));
    366  1.3.8.2  skrll 		psh3tp_stop_polling(sc);
    367  1.3.8.2  skrll 	}
    368  1.3.8.2  skrll }
    369  1.3.8.2  skrll 
    370  1.3.8.2  skrll 
    371  1.3.8.2  skrll /*
    372  1.3.8.2  skrll  * Re-enable touch panel interrupt.
    373  1.3.8.2  skrll  * Called at spltty() when polling code detects pen-up.
    374  1.3.8.2  skrll  */
    375  1.3.8.2  skrll static void
    376  1.3.8.2  skrll psh3tp_stop_polling(struct psh3tp_softc *sc)
    377  1.3.8.2  skrll {
    378  1.3.8.2  skrll 	uint8_t irr0;
    379  1.3.8.2  skrll 
    380  1.3.8.2  skrll 	DPRINTFN(2, ("%s: stop\n", sc->sc_dev.dv_xname));
    381  1.3.8.2  skrll 
    382  1.3.8.2  skrll 	/* clear pending interrupt signal before re-enabling the interrupt */
    383  1.3.8.2  skrll 	irr0 = _reg_read_1(SH7709_IRR0);
    384  1.3.8.2  skrll 	if ((irr0 & IRR0_IRQ2) != 0)
    385  1.3.8.2  skrll 		_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2);
    386  1.3.8.2  skrll 
    387  1.3.8.2  skrll 	intc_intr_enable(SH7709_INTEVT2_IRQ2);
    388  1.3.8.2  skrll }
    389  1.3.8.2  skrll 
    390  1.3.8.2  skrll 
    391  1.3.8.2  skrll /*
    392  1.3.8.2  skrll  * We are reporting this touch as a mouse click/drag.
    393  1.3.8.2  skrll  */
    394  1.3.8.2  skrll static void
    395  1.3.8.2  skrll psh3tp_callout_wsmouse(void *self)
    396  1.3.8.2  skrll {
    397  1.3.8.2  skrll 	struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
    398  1.3.8.2  skrll 	uint8_t phdr;
    399  1.3.8.2  skrll 	int rawx, rawy;
    400  1.3.8.2  skrll 	int s;
    401  1.3.8.2  skrll 
    402  1.3.8.2  skrll 	s = spltty();
    403  1.3.8.2  skrll 
    404  1.3.8.2  skrll 	if (!sc->sc_enabled) {
    405  1.3.8.2  skrll 		DPRINTFN(1, ("%s: wsmouse callout: !sc_enabled\n",
    406  1.3.8.2  skrll 		    sc->sc_dev.dv_xname));
    407  1.3.8.2  skrll 		splx(s);
    408  1.3.8.2  skrll 		return;
    409  1.3.8.2  skrll 	}
    410  1.3.8.2  skrll 
    411  1.3.8.2  skrll 	phdr = _reg_read_1(SH7709_PHDR);
    412  1.3.8.2  skrll 	if ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP) {
    413  1.3.8.2  skrll 		psh3tp_get_raw_xy(&rawx, &rawy);
    414  1.3.8.2  skrll 		psh3tp_wsmouse_input(sc, rawx, rawy); /* mouse dragged */
    415  1.3.8.2  skrll 		callout_schedule(&sc->sc_touch_ch, hz/32);
    416  1.3.8.2  skrll 	} else {
    417  1.3.8.2  skrll 		wsmouse_input( /* button up */
    418  1.3.8.2  skrll 		    sc->sc_wsmousedev, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
    419  1.3.8.2  skrll 		psh3tp_stop_polling(sc);
    420  1.3.8.2  skrll 	}
    421  1.3.8.2  skrll 	splx(s);
    422  1.3.8.2  skrll }
    423  1.3.8.2  skrll 
    424  1.3.8.2  skrll 
    425  1.3.8.2  skrll /*
    426  1.3.8.2  skrll  * Report mouse click/drag.
    427  1.3.8.2  skrll  */
    428  1.3.8.2  skrll static void
    429  1.3.8.2  skrll psh3tp_wsmouse_input(struct psh3tp_softc *sc, int rawx, int rawy)
    430  1.3.8.2  skrll {
    431  1.3.8.2  skrll 	int x, y;
    432  1.3.8.2  skrll 
    433  1.3.8.2  skrll 	tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y);
    434  1.3.8.2  skrll 
    435  1.3.8.2  skrll 	DPRINTFN(3, ("%s: %4d %4d -> %3d %3d\n",
    436  1.3.8.2  skrll 	     sc->sc_dev.dv_xname, rawx, rawy, x, y));
    437  1.3.8.2  skrll 
    438  1.3.8.2  skrll 	wsmouse_input(sc->sc_wsmousedev,
    439  1.3.8.2  skrll 	    1,	/* button */
    440  1.3.8.2  skrll 	    x, y,
    441  1.3.8.2  skrll 	    0,	/* flags */
    442  1.3.8.2  skrll 	    WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
    443  1.3.8.2  skrll }
    444  1.3.8.2  skrll 
    445  1.3.8.2  skrll 
    446  1.3.8.2  skrll /*
    447  1.3.8.2  skrll  * Read raw X/Y coordinates from the ADC.
    448  1.3.8.2  skrll  */
    449  1.3.8.2  skrll static void
    450  1.3.8.2  skrll psh3tp_get_raw_xy(int *rawxp, int *rawyp)
    451  1.3.8.2  skrll {
    452  1.3.8.2  skrll 	uint8_t scpdr;
    453  1.3.8.2  skrll 
    454  1.3.8.2  skrll 	/* X axis */
    455  1.3.8.2  skrll 	scpdr = _reg_read_1(SH7709_SCPDR);
    456  1.3.8.2  skrll 	scpdr &= ~SCPDR_TP_SCAN_DISABLE;
    457  1.3.8.2  skrll 	scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_X);
    458  1.3.8.2  skrll 	_reg_write_1(SH7709_SCPDR, scpdr);
    459  1.3.8.2  skrll 	delay(40);
    460  1.3.8.2  skrll 
    461  1.3.8.2  skrll 	*rawxp = adc_sample_channel(ADC_CHANNEL_TP_X);
    462  1.3.8.2  skrll 
    463  1.3.8.2  skrll 	/* Y axis */
    464  1.3.8.2  skrll 	scpdr = _reg_read_1(SH7709_SCPDR);
    465  1.3.8.2  skrll 	scpdr &=  ~SCPDR_TP_SCAN_X;
    466  1.3.8.2  skrll 	scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y);
    467  1.3.8.2  skrll 	_reg_write_1(SH7709_SCPDR, scpdr);
    468  1.3.8.2  skrll 	delay(40);
    469  1.3.8.2  skrll 
    470  1.3.8.2  skrll 	*rawyp = adc_sample_channel(ADC_CHANNEL_TP_Y);
    471  1.3.8.2  skrll 
    472  1.3.8.2  skrll 	/* restore SCPDR */
    473  1.3.8.2  skrll 	scpdr = _reg_read_1(SH7709_SCPDR);
    474  1.3.8.2  skrll 	scpdr &= ~(SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y);
    475  1.3.8.2  skrll 	scpdr |= SCPDR_TP_SCAN_DISABLE;
    476  1.3.8.2  skrll 	_reg_write_1(SH7709_SCPDR, scpdr);
    477  1.3.8.2  skrll }
    478  1.3.8.2  skrll 
    479  1.3.8.2  skrll 
    480  1.3.8.2  skrll static int
    481  1.3.8.2  skrll psh3tp_wsmouse_ioctl(
    482  1.3.8.2  skrll     void *self, u_long cmd, caddr_t data, int flag, struct proc *p)
    483  1.3.8.2  skrll {
    484  1.3.8.2  skrll 	struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
    485  1.3.8.2  skrll 
    486  1.3.8.2  skrll 	return (hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, p));
    487  1.3.8.2  skrll }
    488