Home | History | Annotate | Line # | Download | only in vr
vrpiu.c revision 1.9
      1  1.9  takemura /*	$NetBSD: vrpiu.c,v 1.9 2001/01/20 12:24:22 takemura Exp $	*/
      2  1.1  takemura 
      3  1.1  takemura /*
      4  1.1  takemura  * Copyright (c) 1999 Shin Takemura All rights reserved.
      5  1.6      sato  * Copyright (c) 2000 SATO Kazumi, All rights reserved.
      6  1.6      sato  * Copyright (c) 1999,2000 PocketBSD Project. All rights reserved.
      7  1.1  takemura  *
      8  1.1  takemura  * Redistribution and use in source and binary forms, with or without
      9  1.1  takemura  * modification, are permitted provided that the following conditions
     10  1.1  takemura  * are met:
     11  1.1  takemura  * 1. Redistributions of source code must retain the above copyright
     12  1.1  takemura  *    notice, this list of conditions and the following disclaimer.
     13  1.1  takemura  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  takemura  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  takemura  *    documentation and/or other materials provided with the distribution.
     16  1.1  takemura  *
     17  1.1  takemura  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     18  1.1  takemura  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  1.1  takemura  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  1.1  takemura  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     21  1.1  takemura  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1  takemura  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  1.1  takemura  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.1  takemura  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.1  takemura  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.1  takemura  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.1  takemura  * SUCH DAMAGE.
     28  1.1  takemura  *
     29  1.1  takemura  */
     30  1.1  takemura 
     31  1.6      sato /*
     32  1.6      sato  * A/D polling part written by SATO Kazumi.
     33  1.6      sato  */
     34  1.6      sato 
     35  1.1  takemura #include <sys/param.h>
     36  1.1  takemura #include <sys/systm.h>
     37  1.1  takemura #include <sys/device.h>
     38  1.1  takemura #include <sys/kernel.h>
     39  1.6      sato #include <sys/callout.h>
     40  1.6      sato #include <sys/boot_flag.h>
     41  1.1  takemura 
     42  1.1  takemura #include <dev/wscons/wsconsio.h>
     43  1.1  takemura #include <dev/wscons/wsmousevar.h>
     44  1.1  takemura 
     45  1.1  takemura #include <machine/bus.h>
     46  1.5      matt #include <machine/platid.h>
     47  1.5      matt #include <machine/platid_mask.h>
     48  1.6      sato #include <machine/config_hook.h>
     49  1.1  takemura 
     50  1.6      sato #include <hpcmips/hpcmips/machdep.h>
     51  1.3  takemura #include <hpcmips/dev/tpcalibvar.h>
     52  1.6      sato 
     53  1.1  takemura #include <hpcmips/vr/vripvar.h>
     54  1.1  takemura #include <hpcmips/vr/cmureg.h>
     55  1.1  takemura #include <hpcmips/vr/vrpiuvar.h>
     56  1.1  takemura #include <hpcmips/vr/vrpiureg.h>
     57  1.1  takemura 
     58  1.1  takemura /*
     59  1.1  takemura  * contant and macro definitions
     60  1.1  takemura  */
     61  1.1  takemura #define VRPIUDEBUG
     62  1.1  takemura #ifdef VRPIUDEBUG
     63  1.1  takemura int	vrpiu_debug = 0;
     64  1.1  takemura #define	DPRINTF(arg) if (vrpiu_debug) printf arg;
     65  1.6      sato #define	VPRINTF(arg) if (bootverbose || vrpiu_debug) printf arg;
     66  1.1  takemura #else
     67  1.1  takemura #define	DPRINTF(arg)
     68  1.6      sato #define	VPRINTF(arg) if (bootverbose) printf arg;
     69  1.1  takemura #endif
     70  1.1  takemura 
     71  1.6      sato #ifndef VRPIU_AD_POLL_INTERVAL
     72  1.6      sato #define VRPIU_AD_POLL_INTERVAL	60	/* interval is 60 sec */
     73  1.6      sato #endif /* VRPIU_AD_POLL_INTERTVAL */
     74  1.8  takemura 
     75  1.8  takemura #define	PIUSIVL_SCANINTVAL_MIN	333			/* 10msec	*/
     76  1.8  takemura #define	PIUSIVL_SCANINTVAL_MAX	PIUSIVL_SCANINTVAL_MASK	/* 60msec	*/
     77  1.8  takemura 
     78  1.8  takemura #define TP_INTR	(PIUINT_ALLINTR & ~PIUINT_PADADPINTR)
     79  1.8  takemura #define AD_INTR	(PIUINT_PADADPINTR)
     80  1.8  takemura 
     81  1.1  takemura /*
     82  1.1  takemura  * data types
     83  1.1  takemura  */
     84  1.1  takemura /* struct vrpiu_softc is defined in vrpiuvar.h */
     85  1.1  takemura 
     86  1.1  takemura /*
     87  1.1  takemura  * function prototypes
     88  1.1  takemura  */
     89  1.1  takemura static int	vrpiumatch __P((struct device *, struct cfdata *, void *));
     90  1.1  takemura static void	vrpiuattach __P((struct device *, struct device *, void *));
     91  1.1  takemura 
     92  1.1  takemura static void	vrpiu_write __P((struct vrpiu_softc *, int, unsigned short));
     93  1.1  takemura static u_short	vrpiu_read __P((struct vrpiu_softc *, int));
     94  1.1  takemura 
     95  1.1  takemura static int	vrpiu_intr __P((void *));
     96  1.6      sato static void	vrpiu_tp_intr __P((struct vrpiu_softc *));
     97  1.6      sato static void	vrpiu_ad_intr __P((struct vrpiu_softc *));
     98  1.1  takemura #ifdef DEBUG
     99  1.1  takemura static void	vrpiu_dump_cntreg __P((unsigned int cmd));
    100  1.1  takemura #endif
    101  1.1  takemura 
    102  1.6      sato static int	vrpiu_tp_enable __P((void *));
    103  1.6      sato static int	vrpiu_tp_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    104  1.6      sato static void	vrpiu_tp_disable __P((void *));
    105  1.6      sato int		vrpiu_ad_enable __P((void *));
    106  1.6      sato void		vrpiu_ad_disable __P((void *));
    107  1.6      sato static void	vrpiu_start_powerstate __P((void *));
    108  1.6      sato static void	vrpiu_calc_powerstate __P((struct vrpiu_softc *));
    109  1.6      sato static void	vrpiu_power __P((int, void *));
    110  1.8  takemura static u_int	scan_interval __P((u_int data));
    111  1.1  takemura 
    112  1.1  takemura /* mra is defined in mra.c */
    113  1.1  takemura int mra_Y_AX1_BX2_C __P((int *y, int ys, int *x1, int x1s, int *x2, int x2s,
    114  1.1  takemura 			 int n, int scale, int *a, int *b, int *c));
    115  1.1  takemura 
    116  1.1  takemura /*
    117  1.1  takemura  * static or global variables
    118  1.1  takemura  */
    119  1.1  takemura struct cfattach vrpiu_ca = {
    120  1.1  takemura 	sizeof(struct vrpiu_softc), vrpiumatch, vrpiuattach
    121  1.1  takemura };
    122  1.1  takemura 
    123  1.1  takemura const struct wsmouse_accessops vrpiu_accessops = {
    124  1.6      sato 	vrpiu_tp_enable,
    125  1.6      sato 	vrpiu_tp_ioctl,
    126  1.6      sato 	vrpiu_tp_disable,
    127  1.1  takemura };
    128  1.1  takemura 
    129  1.6      sato int vrpiu_ad_poll_interval = VRPIU_AD_POLL_INTERVAL;
    130  1.6      sato 
    131  1.1  takemura /*
    132  1.1  takemura  * function definitions
    133  1.1  takemura  */
    134  1.1  takemura static inline void
    135  1.1  takemura vrpiu_write(sc, port, val)
    136  1.1  takemura 	struct vrpiu_softc *sc;
    137  1.1  takemura 	int port;
    138  1.1  takemura 	unsigned short val;
    139  1.1  takemura {
    140  1.1  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
    141  1.1  takemura }
    142  1.1  takemura 
    143  1.1  takemura static inline u_short
    144  1.1  takemura vrpiu_read(sc, port)
    145  1.1  takemura 	struct vrpiu_softc *sc;
    146  1.1  takemura 	int port;
    147  1.1  takemura {
    148  1.1  takemura 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
    149  1.1  takemura }
    150  1.1  takemura 
    151  1.1  takemura static int
    152  1.1  takemura vrpiumatch(parent, cf, aux)
    153  1.1  takemura 	struct device *parent;
    154  1.1  takemura 	struct cfdata *cf;
    155  1.1  takemura 	void *aux;
    156  1.1  takemura {
    157  1.1  takemura 	return 1;
    158  1.1  takemura }
    159  1.1  takemura 
    160  1.1  takemura static void
    161  1.1  takemura vrpiuattach(parent, self, aux)
    162  1.1  takemura 	struct device *parent;
    163  1.1  takemura 	struct device *self;
    164  1.1  takemura 	void *aux;
    165  1.1  takemura {
    166  1.1  takemura 	struct vrpiu_softc *sc = (struct vrpiu_softc *)self;
    167  1.1  takemura 	struct vrip_attach_args *va = aux;
    168  1.1  takemura 	struct wsmousedev_attach_args wsmaa;
    169  1.1  takemura 
    170  1.1  takemura 	bus_space_tag_t iot = va->va_iot;
    171  1.1  takemura 	bus_space_handle_t ioh;
    172  1.1  takemura 
    173  1.1  takemura 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
    174  1.9  takemura 	    printf(": can't map bus space\n");
    175  1.9  takemura 	    return;
    176  1.1  takemura 	}
    177  1.1  takemura 
    178  1.1  takemura 	sc->sc_iot = iot;
    179  1.1  takemura 	sc->sc_ioh = ioh;
    180  1.1  takemura 	sc->sc_vrip = va->va_vc;
    181  1.1  takemura 
    182  1.8  takemura 	sc->sc_interval = scan_interval(WSMOUSE_RES_DEFAULT);
    183  1.8  takemura 
    184  1.1  takemura 	/*
    185  1.1  takemura 	 * disable device until vrpiu_enable called
    186  1.1  takemura 	 */
    187  1.6      sato 	sc->sc_tpstat = VRPIU_TP_STAT_DISABLE;
    188  1.1  takemura 
    189  1.3  takemura 	tpcalib_init(&sc->sc_tpcalib);
    190  1.1  takemura #if 1
    191  1.1  takemura 	/*
    192  1.1  takemura 	 * XXX, calibrate parameters
    193  1.1  takemura 	 */
    194  1.1  takemura 	{
    195  1.9  takemura 	    int i;
    196  1.9  takemura 	    static const struct {
    197  1.9  takemura 		platid_mask_t *mask;
    198  1.9  takemura 		struct wsmouse_calibcoords coords;
    199  1.9  takemura 	    } calibrations[] = {
    200  1.9  takemura 		{ &platid_mask_MACH_NEC_MCR_700A,
    201  1.9  takemura 		  { 0, 0, 799, 599,
    202  1.9  takemura 		    4,
    203  1.9  takemura 		    { { 115,  80,   0,   0 },
    204  1.9  takemura 		      { 115, 966,   0, 599 },
    205  1.9  takemura 		      { 912,  80, 799,   0 },
    206  1.9  takemura 		      { 912, 966, 799, 599 } } } },
    207  1.9  takemura 
    208  1.9  takemura 		{ NULL,		/* samples got on my MC-R500 */
    209  1.9  takemura 		  { 0, 0, 639, 239,
    210  1.9  takemura 		    5,
    211  1.9  takemura 		    { { 502, 486, 320, 120 },
    212  1.9  takemura 		      {  55, 109,   0,   0 },
    213  1.9  takemura 		      {  54, 913,   0, 239 },
    214  1.9  takemura 		      { 973, 924, 639, 239 },
    215  1.9  takemura 		      { 975, 123, 639,   0 } } } },
    216  1.9  takemura 	    };
    217  1.9  takemura 	    for (i = 0; ; i++) {
    218  1.9  takemura 		if (calibrations[i].mask == NULL
    219  1.9  takemura 		    || platid_match(&platid, calibrations[i].mask))
    220  1.9  takemura 		    break;
    221  1.9  takemura 	    }
    222  1.9  takemura 	    tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
    223  1.9  takemura 			  (caddr_t)&calibrations[i].coords, 0, 0);
    224  1.1  takemura 	}
    225  1.1  takemura #endif
    226  1.1  takemura 
    227  1.1  takemura 	/* install interrupt handler and enable interrupt */
    228  1.1  takemura 	if (!(sc->sc_handler =
    229  1.9  takemura 	    vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY,
    230  1.9  takemura 				vrpiu_intr, sc))) {
    231  1.9  takemura 	    printf (": can't map interrupt line.\n");
    232  1.9  takemura 	    return;
    233  1.1  takemura 	}
    234  1.1  takemura 
    235  1.1  takemura 	/* mask level2 interrupt, stop scan sequencer and mask clock to piu */
    236  1.6      sato 	vrpiu_tp_disable(sc);
    237  1.1  takemura 
    238  1.1  takemura 	printf("\n");
    239  1.1  takemura 
    240  1.1  takemura 	wsmaa.accessops = &vrpiu_accessops;
    241  1.1  takemura 	wsmaa.accesscookie = sc;
    242  1.1  takemura 
    243  1.1  takemura 	/*
    244  1.1  takemura 	 * attach the wsmouse
    245  1.1  takemura 	 */
    246  1.1  takemura 	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint);
    247  1.6      sato 
    248  1.6      sato 	/*
    249  1.6      sato 	 * power management events
    250  1.6      sato 	 */
    251  1.6      sato 	sc->sc_power_hook = powerhook_establish(vrpiu_power, sc);
    252  1.6      sato 
    253  1.6      sato 	/*
    254  1.6      sato 	 * init A/D port polling.
    255  1.6      sato 	 */
    256  1.6      sato 	sc->sc_battery.n_values = 3;
    257  1.6      sato 	sc->sc_battery.value[0] = -1;
    258  1.6      sato 	sc->sc_battery.value[1] = -1;
    259  1.6      sato 	sc->sc_battery.value[2] = -1;
    260  1.6      sato 	sc->sc_battery.nextpoll = hz*vrpiu_ad_poll_interval;
    261  1.6      sato 	callout_init(&sc->sc_adpoll);
    262  1.9  takemura 	callout_reset(&sc->sc_adpoll, hz, vrpiu_start_powerstate, sc);
    263  1.1  takemura }
    264  1.1  takemura 
    265  1.8  takemura /*
    266  1.8  takemura  * calculate interval value
    267  1.8  takemura  *  input: WSMOUSE_RES_MIN - WSMOUSE_RES_MAX
    268  1.8  takemura  * output: value for PIUSIVL_REG
    269  1.8  takemura  */
    270  1.8  takemura static u_int
    271  1.8  takemura scan_interval(u_int data)
    272  1.8  takemura {
    273  1.8  takemura 	int scale;
    274  1.8  takemura 
    275  1.8  takemura 	if (data < WSMOUSE_RES_MIN)
    276  1.9  takemura 	    data = WSMOUSE_RES_MIN;
    277  1.8  takemura 
    278  1.8  takemura 	if (WSMOUSE_RES_MAX < data)
    279  1.9  takemura 	    data = WSMOUSE_RES_MAX;
    280  1.8  takemura 
    281  1.8  takemura 	scale = WSMOUSE_RES_MAX - WSMOUSE_RES_MIN;
    282  1.8  takemura 	data += WSMOUSE_RES_MIN;
    283  1.8  takemura 
    284  1.8  takemura 	return PIUSIVL_SCANINTVAL_MIN +
    285  1.8  takemura 	    (PIUSIVL_SCANINTVAL_MAX - PIUSIVL_SCANINTVAL_MIN) *
    286  1.8  takemura 	    (scale - data) / scale;
    287  1.8  takemura }
    288  1.8  takemura 
    289  1.1  takemura int
    290  1.6      sato vrpiu_ad_enable(v)
    291  1.1  takemura 	void *v;
    292  1.1  takemura {
    293  1.1  takemura 	struct vrpiu_softc *sc = v;
    294  1.1  takemura 	int s;
    295  1.1  takemura 	unsigned int cnt;
    296  1.1  takemura 
    297  1.8  takemura 	DPRINTF(("%s(%d): vrpiu_ad_enable(): interval=0x%03x\n",
    298  1.9  takemura 		 __FILE__, __LINE__, sc->sc_interval));
    299  1.6      sato 	if (sc->sc_adstat != VRPIU_AD_STAT_DISABLE)
    300  1.9  takemura 	    return EBUSY;
    301  1.1  takemura 
    302  1.1  takemura 	/* supply clock to PIU */
    303  1.1  takemura 	__vrcmu_supply(CMUMSKPIU, 1);
    304  1.1  takemura 
    305  1.8  takemura 	/* set scan interval */
    306  1.8  takemura 	vrpiu_write(sc, PIUSIVL_REG_W, sc->sc_interval);
    307  1.1  takemura 
    308  1.1  takemura 	s = spltty();
    309  1.1  takemura 
    310  1.1  takemura 	/* clear interrupt status */
    311  1.8  takemura 	vrpiu_write(sc, PIUINT_REG_W, AD_INTR);
    312  1.1  takemura 
    313  1.1  takemura 	/* Disable -> Standby */
    314  1.1  takemura 	cnt = PIUCNT_PIUPWR |
    315  1.1  takemura 		PIUCNT_PIUMODE_COORDINATE |
    316  1.1  takemura 		PIUCNT_PADATSTART | PIUCNT_PADATSTOP;
    317  1.1  takemura 	vrpiu_write(sc, PIUCNT_REG_W, cnt);
    318  1.1  takemura 
    319  1.6      sato 	/* Level2 interrupt register setting */
    320  1.8  takemura 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, AD_INTR, 1);
    321  1.6      sato 
    322  1.1  takemura 	/* save pen status, touch or release */
    323  1.1  takemura 	cnt = vrpiu_read(sc, PIUCNT_REG_W);
    324  1.1  takemura 
    325  1.6      sato 	/*
    326  1.6      sato 	 * Enable scan sequencer operation
    327  1.6      sato 	 * Standby -> WaitPenTouch
    328  1.6      sato 	 */
    329  1.6      sato 	cnt |= PIUCNT_PIUSEQEN;
    330  1.6      sato 	vrpiu_write(sc, PIUCNT_REG_W, cnt);
    331  1.6      sato 
    332  1.6      sato 	sc->sc_adstat = VRPIU_AD_STAT_ENABLE;
    333  1.6      sato 
    334  1.6      sato 	splx(s);
    335  1.6      sato 
    336  1.6      sato 	return 0;
    337  1.6      sato }
    338  1.6      sato 
    339  1.6      sato void
    340  1.6      sato vrpiu_ad_disable(v)
    341  1.6      sato 	void *v;
    342  1.6      sato {
    343  1.6      sato 	struct vrpiu_softc *sc = v;
    344  1.6      sato 
    345  1.6      sato 	DPRINTF(("%s(%d): vrpiu_ad_disable()\n", __FILE__, __LINE__));
    346  1.6      sato 
    347  1.6      sato 	/* Set level2 interrupt register to mask interrupts */
    348  1.8  takemura 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, AD_INTR, 0);
    349  1.6      sato 
    350  1.6      sato 	sc->sc_adstat = VRPIU_AD_STAT_DISABLE;
    351  1.6      sato 
    352  1.6      sato 	if (sc->sc_tpstat == VRPIU_TP_STAT_DISABLE){
    353  1.9  takemura 	    /* Disable scan sequencer operation and power off */
    354  1.9  takemura 	    vrpiu_write(sc, PIUCNT_REG_W, 0);
    355  1.6      sato 
    356  1.9  takemura 	    /* mask clock to PIU */
    357  1.9  takemura 	    __vrcmu_supply(CMUMSKPIU, 1);
    358  1.6      sato 	}
    359  1.6      sato }
    360  1.6      sato 
    361  1.6      sato int
    362  1.6      sato vrpiu_tp_enable(v)
    363  1.6      sato 	void *v;
    364  1.6      sato {
    365  1.6      sato 	struct vrpiu_softc *sc = v;
    366  1.6      sato 	int s;
    367  1.6      sato 	unsigned int cnt;
    368  1.6      sato 
    369  1.8  takemura 	DPRINTF(("%s(%d): vrpiu_tp_enable(): interval=0x%03x\n",
    370  1.9  takemura 		 __FILE__, __LINE__, sc->sc_interval));
    371  1.6      sato 	if (sc->sc_tpstat != VRPIU_TP_STAT_DISABLE)
    372  1.9  takemura 	    return EBUSY;
    373  1.6      sato 
    374  1.6      sato 	/* supply clock to PIU */
    375  1.6      sato 	__vrcmu_supply(CMUMSKPIU, 1);
    376  1.6      sato 
    377  1.8  takemura 	/* set scan interval */
    378  1.8  takemura 	vrpiu_write(sc, PIUSIVL_REG_W, sc->sc_interval);
    379  1.6      sato 
    380  1.6      sato 	s = spltty();
    381  1.6      sato 
    382  1.6      sato 	/* clear interrupt status */
    383  1.8  takemura 	vrpiu_write(sc, PIUINT_REG_W, TP_INTR);
    384  1.6      sato 
    385  1.6      sato 	/* Disable -> Standby */
    386  1.6      sato 	cnt = PIUCNT_PIUPWR |
    387  1.6      sato 		PIUCNT_PIUMODE_COORDINATE |
    388  1.6      sato 		PIUCNT_PADATSTART | PIUCNT_PADATSTOP;
    389  1.6      sato 	vrpiu_write(sc, PIUCNT_REG_W, cnt);
    390  1.6      sato 
    391  1.1  takemura 	/* Level2 interrupt register setting */
    392  1.8  takemura 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, TP_INTR, 1);
    393  1.6      sato 
    394  1.6      sato 	/* save pen status, touch or release */
    395  1.6      sato 	cnt = vrpiu_read(sc, PIUCNT_REG_W);
    396  1.1  takemura 
    397  1.1  takemura 	/*
    398  1.1  takemura 	 * Enable scan sequencer operation
    399  1.1  takemura 	 * Standby -> WaitPenTouch
    400  1.1  takemura 	 */
    401  1.1  takemura 	cnt |= PIUCNT_PIUSEQEN;
    402  1.1  takemura 	vrpiu_write(sc, PIUCNT_REG_W, cnt);
    403  1.1  takemura 
    404  1.1  takemura 	/* transit status DISABLE -> TOUCH or RELEASE */
    405  1.6      sato 	sc->sc_tpstat = (cnt & PIUCNT_PENSTC) ?
    406  1.6      sato 		VRPIU_TP_STAT_TOUCH : VRPIU_TP_STAT_RELEASE;
    407  1.1  takemura 
    408  1.1  takemura 	splx(s);
    409  1.1  takemura 
    410  1.1  takemura 	return 0;
    411  1.1  takemura }
    412  1.1  takemura 
    413  1.1  takemura void
    414  1.6      sato vrpiu_tp_disable(v)
    415  1.1  takemura 	void *v;
    416  1.1  takemura {
    417  1.1  takemura 	struct vrpiu_softc *sc = v;
    418  1.1  takemura 
    419  1.6      sato 	DPRINTF(("%s(%d): vrpiu_tp_disable()\n", __FILE__, __LINE__));
    420  1.1  takemura 
    421  1.1  takemura 	/* Set level2 interrupt register to mask interrupts */
    422  1.8  takemura 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, TP_INTR, 0);
    423  1.1  takemura 
    424  1.6      sato 	sc->sc_tpstat = VRPIU_TP_STAT_DISABLE;
    425  1.1  takemura 
    426  1.6      sato 	if (sc->sc_adstat == VRPIU_AD_STAT_DISABLE){
    427  1.9  takemura 	    /* Disable scan sequencer operation and power off */
    428  1.9  takemura 	    vrpiu_write(sc, PIUCNT_REG_W, 0);
    429  1.1  takemura 
    430  1.9  takemura 	    /* mask clock to PIU */
    431  1.9  takemura 	    __vrcmu_supply(CMUMSKPIU, 1);
    432  1.6      sato 	}
    433  1.1  takemura }
    434  1.1  takemura 
    435  1.1  takemura int
    436  1.6      sato vrpiu_tp_ioctl(v, cmd, data, flag, p)
    437  1.1  takemura 	void *v;
    438  1.1  takemura 	u_long cmd;
    439  1.1  takemura 	caddr_t data;
    440  1.1  takemura 	int flag;
    441  1.1  takemura 	struct proc *p;
    442  1.1  takemura {
    443  1.1  takemura 	struct vrpiu_softc *sc = v;
    444  1.1  takemura 
    445  1.6      sato 	DPRINTF(("%s(%d): vrpiu_tp_ioctl(%08lx)\n", __FILE__, __LINE__, cmd));
    446  1.1  takemura 
    447  1.1  takemura 	switch (cmd) {
    448  1.1  takemura 	case WSMOUSEIO_GTYPE:
    449  1.9  takemura 	    *(u_int *)data = WSMOUSE_TYPE_TPANEL;
    450  1.9  takemura 	    break;
    451  1.1  takemura 
    452  1.1  takemura 	case WSMOUSEIO_SRES:
    453  1.8  takemura 	    {
    454  1.8  takemura 		int tp_enable;
    455  1.8  takemura 		int ad_enable;
    456  1.8  takemura 
    457  1.8  takemura 		tp_enable = (sc->sc_tpstat != VRPIU_TP_STAT_DISABLE);
    458  1.8  takemura 		ad_enable = (sc->sc_adstat != VRPIU_AD_STAT_DISABLE);
    459  1.8  takemura 
    460  1.8  takemura 		if (tp_enable)
    461  1.9  takemura 		    vrpiu_tp_disable(sc);
    462  1.8  takemura 		if (ad_enable)
    463  1.9  takemura 		    vrpiu_ad_disable(sc);
    464  1.8  takemura 
    465  1.8  takemura 		sc->sc_interval = scan_interval(*(u_int *)data);
    466  1.8  takemura 		DPRINTF(("%s(%d): WSMOUSEIO_SRES: *data=%d, interval=0x%03x\n",
    467  1.8  takemura 		    __FILE__, __LINE__, *(u_int *)data, sc->sc_interval));
    468  1.8  takemura 
    469  1.8  takemura 		if (sc->sc_interval < PIUSIVL_SCANINTVAL_MIN)
    470  1.9  takemura 		    sc->sc_interval = PIUSIVL_SCANINTVAL_MIN;
    471  1.8  takemura 
    472  1.8  takemura 		if (PIUSIVL_SCANINTVAL_MAX < sc->sc_interval)
    473  1.9  takemura 		    sc->sc_interval = PIUSIVL_SCANINTVAL_MAX;
    474  1.8  takemura 
    475  1.8  takemura 		if (tp_enable)
    476  1.9  takemura 		    vrpiu_tp_enable(sc);
    477  1.8  takemura 		if (ad_enable)
    478  1.9  takemura 		     vrpiu_ad_enable(sc);
    479  1.8  takemura 	    }
    480  1.8  takemura 	    break;
    481  1.3  takemura 
    482  1.3  takemura 	case WSMOUSEIO_SCALIBCOORDS:
    483  1.3  takemura 	case WSMOUSEIO_GCALIBCOORDS:
    484  1.9  takemura             return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, p);
    485  1.1  takemura 
    486  1.1  takemura 	default:
    487  1.9  takemura 	    return (-1);
    488  1.1  takemura 	}
    489  1.1  takemura 	return (0);
    490  1.1  takemura }
    491  1.1  takemura 
    492  1.1  takemura /*
    493  1.6      sato  * PIU AD interrupt handler.
    494  1.6      sato  */
    495  1.6      sato void
    496  1.6      sato vrpiu_ad_intr(sc)
    497  1.6      sato 	struct vrpiu_softc *sc;
    498  1.6      sato {
    499  1.6      sato 	unsigned int i;
    500  1.6      sato 	unsigned int intrstat;
    501  1.6      sato 
    502  1.6      sato 	intrstat = vrpiu_read(sc, PIUINT_REG_W);
    503  1.6      sato 
    504  1.6      sato 	if (sc->sc_adstat == VRPIU_AD_STAT_DISABLE) {
    505  1.9  takemura 	    /*
    506  1.9  takemura 	     * the device isn't enabled. just clear interrupt.
    507  1.9  takemura 	     */
    508  1.9  takemura 	    vrpiu_write(sc, PIUINT_REG_W, AD_INTR);
    509  1.9  takemura 	    return;
    510  1.6      sato 	}
    511  1.6      sato 
    512  1.6      sato 	if (intrstat & PIUINT_PADADPINTR) {
    513  1.9  takemura 	    sc->sc_battery.value[0] = (unsigned int)vrpiu_read(sc, PIUAB(0));
    514  1.9  takemura 	    sc->sc_battery.value[1] = (unsigned int)vrpiu_read(sc, PIUAB(1));
    515  1.9  takemura 	    sc->sc_battery.value[2] = (unsigned int)vrpiu_read(sc, PIUAB(2));
    516  1.6      sato 	}
    517  1.6      sato 
    518  1.6      sato 	if (intrstat & PIUINT_PADADPINTR) {
    519  1.9  takemura 	    for (i = 0; i < 3; i++) {
    520  1.9  takemura 		if (sc->sc_battery.value[i] & PIUAB_VALID)
    521  1.9  takemura 		    sc->sc_battery.value[i] &= PIUAB_PADDATA_MASK;
    522  1.9  takemura 		else
    523  1.9  takemura 		    sc->sc_battery.value[i] = 0;
    524  1.9  takemura 	    }
    525  1.9  takemura 	    vrpiu_calc_powerstate(sc);
    526  1.6      sato 	}
    527  1.8  takemura 	vrpiu_write(sc, PIUINT_REG_W, AD_INTR);
    528  1.6      sato 
    529  1.6      sato 	return;
    530  1.6      sato }
    531  1.6      sato /*
    532  1.6      sato  * PIU TP interrupt handler.
    533  1.1  takemura  */
    534  1.6      sato void
    535  1.6      sato vrpiu_tp_intr(sc)
    536  1.6      sato 	struct vrpiu_softc *sc;
    537  1.1  takemura {
    538  1.1  takemura 	unsigned int cnt, i;
    539  1.1  takemura 	unsigned int intrstat, page;
    540  1.1  takemura 	int tpx0, tpx1, tpy0, tpy1;
    541  1.1  takemura 	int x, y, xraw, yraw;
    542  1.1  takemura 
    543  1.1  takemura 	intrstat = vrpiu_read(sc, PIUINT_REG_W);
    544  1.1  takemura 
    545  1.6      sato 	if (sc->sc_tpstat == VRPIU_TP_STAT_DISABLE) {
    546  1.9  takemura 	    /*
    547  1.9  takemura 	     * the device isn't enabled. just clear interrupt.
    548  1.9  takemura 	     */
    549  1.9  takemura 	    vrpiu_write(sc, PIUINT_REG_W, intrstat & TP_INTR);
    550  1.9  takemura 	    return;
    551  1.1  takemura 	}
    552  1.1  takemura 
    553  1.1  takemura 	page = (intrstat & PIUINT_OVP) ? 1 : 0;
    554  1.1  takemura 	if (intrstat & (PIUINT_PADPAGE0INTR | PIUINT_PADPAGE1INTR)) {
    555  1.9  takemura 	    tpx0 = vrpiu_read(sc, PIUPB(page, 0));
    556  1.9  takemura 	    tpx1 = vrpiu_read(sc, PIUPB(page, 1));
    557  1.9  takemura 	    tpy0 = vrpiu_read(sc, PIUPB(page, 2));
    558  1.9  takemura 	    tpy1 = vrpiu_read(sc, PIUPB(page, 3));
    559  1.1  takemura 	}
    560  1.1  takemura 
    561  1.1  takemura 	if (intrstat & PIUINT_PADDLOSTINTR) {
    562  1.9  takemura 	    page = page ? 0 : 1;
    563  1.9  takemura 	    for (i = 0; i < 4; i++)
    564  1.9  takemura 		vrpiu_read(sc, PIUPB(page, i));
    565  1.1  takemura 	}
    566  1.1  takemura 
    567  1.1  takemura 	cnt = vrpiu_read(sc, PIUCNT_REG_W);
    568  1.1  takemura #ifdef DEBUG
    569  1.1  takemura 	if (vrpiu_debug)
    570  1.9  takemura 	    vrpiu_dump_cntreg(cnt);
    571  1.1  takemura #endif
    572  1.1  takemura 
    573  1.1  takemura 	/* clear interrupt status */
    574  1.8  takemura 	vrpiu_write(sc, PIUINT_REG_W, intrstat & TP_INTR);
    575  1.1  takemura 
    576  1.1  takemura #if 0
    577  1.1  takemura 	DPRINTF(("vrpiu_intr: OVP=%d", page));
    578  1.1  takemura 	if (intrstat & PIUINT_PADCMDINTR)
    579  1.9  takemura 	    DPRINTF((" CMD"));
    580  1.1  takemura 	if (intrstat & PIUINT_PADADPINTR)
    581  1.9  takemura 	    DPRINTF((" A/D"));
    582  1.1  takemura 	if (intrstat & PIUINT_PADPAGE1INTR)
    583  1.9  takemura 	    DPRINTF((" PAGE1"));
    584  1.1  takemura 	if (intrstat & PIUINT_PADPAGE0INTR)
    585  1.9  takemura 	    DPRINTF((" PAGE0"));
    586  1.1  takemura 	if (intrstat & PIUINT_PADDLOSTINTR)
    587  1.9  takemura 	    DPRINTF((" DLOST"));
    588  1.1  takemura 	if (intrstat & PIUINT_PENCHGINTR)
    589  1.9  takemura 	    DPRINTF((" PENCHG"));
    590  1.1  takemura 	DPRINTF(("\n"));
    591  1.1  takemura #endif
    592  1.1  takemura 	if (intrstat & (PIUINT_PADPAGE0INTR | PIUINT_PADPAGE1INTR)) {
    593  1.9  takemura 	    if (!((tpx0 & PIUPB_VALID) && (tpx1 & PIUPB_VALID) &&
    594  1.9  takemura 		(tpy0 & PIUPB_VALID) && (tpy1 & PIUPB_VALID))) {
    595  1.9  takemura 		printf("vrpiu: internal error, data is not valid!\n");
    596  1.9  takemura 	    } else {
    597  1.9  takemura 		tpx0 &= PIUPB_PADDATA_MASK;
    598  1.9  takemura 		tpx1 &= PIUPB_PADDATA_MASK;
    599  1.9  takemura 		tpy0 &= PIUPB_PADDATA_MASK;
    600  1.9  takemura 		tpy1 &= PIUPB_PADDATA_MASK;
    601  1.1  takemura #define ISVALID(n, c, m)	((c) - (m) < (n) && (n) < (c) + (m))
    602  1.9  takemura 		if (ISVALID(tpx0 + tpx1, 1024, 200) &&
    603  1.9  takemura 		    ISVALID(tpx0 + tpx1, 1024, 200)) {
    604  1.1  takemura #if 0
    605  1.9  takemura 		    DPRINTF(("%04x %04x %04x %04x\n",
    606  1.9  takemura 			tpx0, tpx1, tpy0, tpy1));
    607  1.9  takemura 		    DPRINTF(("%3d %3d (%4d %4d)->", tpx0, tpy0,
    608  1.9  takemura 			tpx0 + tpx1, tpy0 + tpy1));
    609  1.1  takemura #endif
    610  1.9  takemura 		    xraw = tpy1 * 1024 / (tpy0 + tpy1);
    611  1.9  takemura 		    yraw = tpx1 * 1024 / (tpx0 + tpx1);
    612  1.9  takemura 		    DPRINTF(("%3d %3d", xraw, yraw));
    613  1.9  takemura 
    614  1.9  takemura 		    tpcalib_trans(&sc->sc_tpcalib, xraw, yraw, &x, &y);
    615  1.9  takemura 
    616  1.9  takemura 		    DPRINTF(("->%4d %4d", x, y));
    617  1.9  takemura 		    wsmouse_input(sc->sc_wsmousedev,
    618  1.9  takemura 			(cnt & PIUCNT_PENSTC) ? 1 : 0,
    619  1.9  takemura 			x, /* x */
    620  1.9  takemura 			y, /* y */
    621  1.9  takemura 			0, /* z */
    622  1.9  takemura 			WSMOUSE_INPUT_ABSOLUTE_X |
    623  1.9  takemura 			WSMOUSE_INPUT_ABSOLUTE_Y);
    624  1.9  takemura 		    DPRINTF(("\n"));
    625  1.4  takemura 		}
    626  1.9  takemura 	    }
    627  1.4  takemura 	}
    628  1.4  takemura 
    629  1.4  takemura 	if (cnt & PIUCNT_PENSTC) {
    630  1.9  takemura 	    if (sc->sc_tpstat == VRPIU_TP_STAT_RELEASE) {
    631  1.9  takemura 		/*
    632  1.9  takemura 		 * pen touch
    633  1.9  takemura 		 */
    634  1.9  takemura 		DPRINTF(("PEN TOUCH\n"));
    635  1.9  takemura 		sc->sc_tpstat = VRPIU_TP_STAT_TOUCH;
    636  1.9  takemura 		/*
    637  1.9  takemura 		 * We should not report button down event while
    638  1.9  takemura 		 * we don't know where it occur.
    639  1.9  takemura 		 */
    640  1.9  takemura 	    }
    641  1.4  takemura 	} else {
    642  1.9  takemura 	    if (sc->sc_tpstat == VRPIU_TP_STAT_TOUCH) {
    643  1.9  takemura 		/*
    644  1.9  takemura 		 * pen release
    645  1.9  takemura 		 */
    646  1.9  takemura 		DPRINTF(("RELEASE\n"));
    647  1.9  takemura 		sc->sc_tpstat = VRPIU_TP_STAT_RELEASE;
    648  1.9  takemura 		/* button 0 UP */
    649  1.9  takemura 		wsmouse_input(sc->sc_wsmousedev,
    650  1.9  takemura 		    0,
    651  1.9  takemura 		    0, 0, 0, 0);
    652  1.9  takemura 	    }
    653  1.1  takemura 	}
    654  1.1  takemura 
    655  1.1  takemura 	if (intrstat & PIUINT_PADDLOSTINTR) {
    656  1.9  takemura 	    cnt |= PIUCNT_PIUSEQEN;
    657  1.9  takemura 	    vrpiu_write(sc, PIUCNT_REG_W, cnt);
    658  1.1  takemura 	}
    659  1.1  takemura 
    660  1.6      sato 	return;
    661  1.6      sato }
    662  1.6      sato 
    663  1.6      sato /*
    664  1.6      sato  * PIU interrupt handler.
    665  1.6      sato  */
    666  1.6      sato int
    667  1.6      sato vrpiu_intr(arg)
    668  1.6      sato 	void *arg;
    669  1.6      sato {
    670  1.6      sato         struct vrpiu_softc *sc = arg;
    671  1.6      sato 
    672  1.6      sato 	vrpiu_ad_intr(sc);
    673  1.6      sato 	vrpiu_tp_intr(sc);
    674  1.6      sato 
    675  1.1  takemura 	return 0;
    676  1.6      sato }
    677  1.6      sato 
    678  1.6      sato void
    679  1.6      sato vrpiu_start_powerstate(v)
    680  1.6      sato 	void *v;
    681  1.6      sato {
    682  1.6      sato 	int mask;
    683  1.6      sato 	struct vrpiu_softc *sc = (struct vrpiu_softc *)v;
    684  1.6      sato 
    685  1.6      sato 	vrpiu_ad_enable(sc);
    686  1.6      sato 	mask = vrpiu_read(sc, PIUAMSK_REG_W);
    687  1.6      sato 	mask &= 0xff8f; /* XXX */
    688  1.6      sato 	vrpiu_write(sc, PIUAMSK_REG_W, mask);
    689  1.6      sato 	vrpiu_write(sc, PIUASCN_REG_W, PIUACN_ADPSSTART);
    690  1.6      sato 	/*
    691  1.6      sato 	 * restart next A/D polling
    692  1.6      sato 	 */
    693  1.6      sato 	callout_reset(&sc->sc_adpoll, hz*vrpiu_ad_poll_interval,
    694  1.9  takemura 		      vrpiu_start_powerstate, sc);
    695  1.6      sato }
    696  1.6      sato 
    697  1.6      sato void
    698  1.6      sato vrpiu_calc_powerstate(sc)
    699  1.6      sato 	struct vrpiu_softc *sc;
    700  1.6      sato {
    701  1.6      sato 	extern void vrgiu_diff_io __P((void));
    702  1.6      sato 	vrpiu_ad_disable(sc);
    703  1.6      sato 	VPRINTF(("vrpiu:AD: %d, %d, %d\n",
    704  1.6      sato 		sc->sc_battery.value[0],
    705  1.6      sato 		sc->sc_battery.value[1],
    706  1.6      sato 		sc->sc_battery.value[2]));
    707  1.6      sato 	sc->sc_battery.nextpoll = hz*vrpiu_ad_poll_interval;
    708  1.6      sato #ifdef notyet
    709  1.7      sato 	config_hook_call(CONFIG_HOOK_SET,
    710  1.7      sato 			 CONFIG_HOOK_BATTERYVAL,
    711  1.6      sato 			 (void *)&sc->sc_battery);
    712  1.6      sato #endif /* notyet */
    713  1.6      sato 	/*
    714  1.6      sato 	 * restart next A/D polling if change polling timming.
    715  1.6      sato 	 */
    716  1.6      sato 	if (sc->sc_battery.nextpoll != hz*vrpiu_ad_poll_interval)
    717  1.9  takemura 	    callout_reset(&sc->sc_adpoll, sc->sc_battery.nextpoll,
    718  1.9  takemura 			  vrpiu_start_powerstate, sc);
    719  1.6      sato 	if (bootverbose)
    720  1.9  takemura 	    vrgiu_diff_io();
    721  1.6      sato 
    722  1.6      sato }
    723  1.6      sato 
    724  1.6      sato static void
    725  1.6      sato vrpiu_power(why, arg)
    726  1.6      sato 	int why;
    727  1.6      sato 	void *arg;
    728  1.6      sato {
    729  1.6      sato 	struct vrpiu_softc *sc = arg;
    730  1.6      sato 
    731  1.6      sato 	switch (why) {
    732  1.6      sato 	case PWR_STANDBY:
    733  1.6      sato 	case PWR_SUSPEND:
    734  1.9  takemura 	    break;
    735  1.6      sato 	case PWR_RESUME:
    736  1.9  takemura 	    callout_reset(&sc->sc_adpoll, hz,
    737  1.9  takemura 			  vrpiu_start_powerstate, sc);
    738  1.9  takemura 	    break;
    739  1.6      sato 	}
    740  1.1  takemura }
    741  1.1  takemura 
    742  1.1  takemura #ifdef DEBUG
    743  1.1  takemura void
    744  1.1  takemura vrpiu_dump_cntreg(cnt)
    745  1.1  takemura 	unsigned int cnt;
    746  1.1  takemura {
    747  1.1  takemura 	printf("%s", (cnt & PIUCNT_PENSTC) ? "Touch" : "Release");
    748  1.1  takemura 	printf(" state=");
    749  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_CmdScan)
    750  1.9  takemura 	    printf("CmdScan");
    751  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_IntervalNextScan)
    752  1.9  takemura 	    printf("IntervalNextScan");
    753  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_PenDataScan)
    754  1.9  takemura 	    printf("PenDataScan");
    755  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_WaitPenTouch)
    756  1.9  takemura 	    printf("WaitPenTouch");
    757  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_RFU)
    758  1.9  takemura 	    printf("???");
    759  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_ADPortScan)
    760  1.9  takemura 	    printf("ADPortScan");
    761  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_Standby)
    762  1.9  takemura 	    printf("Standby");
    763  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_Disable)
    764  1.9  takemura 	    printf("Disable");
    765  1.1  takemura 	if (cnt & PIUCNT_PADATSTOP)
    766  1.9  takemura 	    printf(" AutoStop");
    767  1.1  takemura 	if (cnt & PIUCNT_PADATSTART)
    768  1.9  takemura 	    printf(" AutoStart");
    769  1.1  takemura 	if (cnt & PIUCNT_PADSCANSTOP)
    770  1.9  takemura 	    printf(" Stop");
    771  1.1  takemura 	if (cnt & PIUCNT_PADSCANSTART)
    772  1.9  takemura 	    printf(" Start");
    773  1.1  takemura 	if (cnt & PIUCNT_PADSCANTYPE)
    774  1.9  takemura 	    printf(" ScanPressure");
    775  1.1  takemura 	if ((cnt & PIUCNT_PIUMODE_MASK) == PIUCNT_PIUMODE_ADCONVERTER)
    776  1.9  takemura 	    printf(" A/D");
    777  1.1  takemura 	if ((cnt & PIUCNT_PIUMODE_MASK) == PIUCNT_PIUMODE_COORDINATE)
    778  1.9  takemura 	    printf(" Coordinate");
    779  1.1  takemura 	if (cnt & PIUCNT_PIUSEQEN)
    780  1.9  takemura 	    printf(" SeqEn");
    781  1.1  takemura 	if ((cnt & PIUCNT_PIUPWR) == 0)
    782  1.9  takemura 	    printf(" PowerOff");
    783  1.1  takemura 	if ((cnt & PIUCNT_PADRST) == 0)
    784  1.9  takemura 	    printf(" Reset");
    785  1.1  takemura 	printf("\n");
    786  1.1  takemura }
    787  1.1  takemura #endif
    788