Home | History | Annotate | Line # | Download | only in vr
vrpiu.c revision 1.7
      1  1.7      sato /*	$NetBSD: vrpiu.c,v 1.7 2000/12/27 12:22:07 sato 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.1  takemura /*
     75  1.1  takemura  * data types
     76  1.1  takemura  */
     77  1.1  takemura /* struct vrpiu_softc is defined in vrpiuvar.h */
     78  1.1  takemura 
     79  1.1  takemura /*
     80  1.1  takemura  * function prototypes
     81  1.1  takemura  */
     82  1.1  takemura static int	vrpiumatch __P((struct device *, struct cfdata *, void *));
     83  1.1  takemura static void	vrpiuattach __P((struct device *, struct device *, void *));
     84  1.1  takemura 
     85  1.1  takemura static void	vrpiu_write __P((struct vrpiu_softc *, int, unsigned short));
     86  1.1  takemura static u_short	vrpiu_read __P((struct vrpiu_softc *, int));
     87  1.1  takemura 
     88  1.1  takemura static int	vrpiu_intr __P((void *));
     89  1.6      sato static void	vrpiu_tp_intr __P((struct vrpiu_softc *));
     90  1.6      sato static void	vrpiu_ad_intr __P((struct vrpiu_softc *));
     91  1.1  takemura #ifdef DEBUG
     92  1.1  takemura static void	vrpiu_dump_cntreg __P((unsigned int cmd));
     93  1.1  takemura #endif
     94  1.1  takemura 
     95  1.6      sato static int	vrpiu_tp_enable __P((void *));
     96  1.6      sato static int	vrpiu_tp_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     97  1.6      sato static void	vrpiu_tp_disable __P((void *));
     98  1.6      sato int		vrpiu_ad_enable __P((void *));
     99  1.6      sato void		vrpiu_ad_disable __P((void *));
    100  1.6      sato static void	vrpiu_start_powerstate __P((void *));
    101  1.6      sato static void	vrpiu_calc_powerstate __P((struct vrpiu_softc *));
    102  1.6      sato static void	vrpiu_power __P((int, void *));
    103  1.1  takemura 
    104  1.1  takemura /* mra is defined in mra.c */
    105  1.1  takemura int mra_Y_AX1_BX2_C __P((int *y, int ys, int *x1, int x1s, int *x2, int x2s,
    106  1.1  takemura 			 int n, int scale, int *a, int *b, int *c));
    107  1.1  takemura 
    108  1.1  takemura /*
    109  1.1  takemura  * static or global variables
    110  1.1  takemura  */
    111  1.1  takemura struct cfattach vrpiu_ca = {
    112  1.1  takemura 	sizeof(struct vrpiu_softc), vrpiumatch, vrpiuattach
    113  1.1  takemura };
    114  1.1  takemura 
    115  1.1  takemura const struct wsmouse_accessops vrpiu_accessops = {
    116  1.6      sato 	vrpiu_tp_enable,
    117  1.6      sato 	vrpiu_tp_ioctl,
    118  1.6      sato 	vrpiu_tp_disable,
    119  1.1  takemura };
    120  1.1  takemura 
    121  1.6      sato int vrpiu_ad_poll_interval = VRPIU_AD_POLL_INTERVAL;
    122  1.6      sato 
    123  1.1  takemura /*
    124  1.1  takemura  * function definitions
    125  1.1  takemura  */
    126  1.1  takemura static inline void
    127  1.1  takemura vrpiu_write(sc, port, val)
    128  1.1  takemura 	struct vrpiu_softc *sc;
    129  1.1  takemura 	int port;
    130  1.1  takemura 	unsigned short val;
    131  1.1  takemura {
    132  1.1  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
    133  1.1  takemura }
    134  1.1  takemura 
    135  1.1  takemura static inline u_short
    136  1.1  takemura vrpiu_read(sc, port)
    137  1.1  takemura 	struct vrpiu_softc *sc;
    138  1.1  takemura 	int port;
    139  1.1  takemura {
    140  1.1  takemura 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
    141  1.1  takemura }
    142  1.1  takemura 
    143  1.1  takemura static int
    144  1.1  takemura vrpiumatch(parent, cf, aux)
    145  1.1  takemura 	struct device *parent;
    146  1.1  takemura 	struct cfdata *cf;
    147  1.1  takemura 	void *aux;
    148  1.1  takemura {
    149  1.1  takemura 	return 1;
    150  1.1  takemura }
    151  1.1  takemura 
    152  1.1  takemura static void
    153  1.1  takemura vrpiuattach(parent, self, aux)
    154  1.1  takemura 	struct device *parent;
    155  1.1  takemura 	struct device *self;
    156  1.1  takemura 	void *aux;
    157  1.1  takemura {
    158  1.1  takemura 	struct vrpiu_softc *sc = (struct vrpiu_softc *)self;
    159  1.1  takemura 	struct vrip_attach_args *va = aux;
    160  1.1  takemura 	struct wsmousedev_attach_args wsmaa;
    161  1.1  takemura 
    162  1.1  takemura 	bus_space_tag_t iot = va->va_iot;
    163  1.1  takemura 	bus_space_handle_t ioh;
    164  1.1  takemura 
    165  1.1  takemura 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
    166  1.1  takemura 		printf(": can't map bus space\n");
    167  1.1  takemura 		return;
    168  1.1  takemura 	}
    169  1.1  takemura 
    170  1.1  takemura 	sc->sc_iot = iot;
    171  1.1  takemura 	sc->sc_ioh = ioh;
    172  1.1  takemura 	sc->sc_vrip = va->va_vc;
    173  1.1  takemura 
    174  1.1  takemura 	/*
    175  1.1  takemura 	 * disable device until vrpiu_enable called
    176  1.1  takemura 	 */
    177  1.6      sato 	sc->sc_tpstat = VRPIU_TP_STAT_DISABLE;
    178  1.1  takemura 
    179  1.3  takemura 	tpcalib_init(&sc->sc_tpcalib);
    180  1.1  takemura #if 1
    181  1.1  takemura 	/*
    182  1.1  takemura 	 * XXX, calibrate parameters
    183  1.1  takemura 	 */
    184  1.1  takemura 	{
    185  1.5      matt 		int i;
    186  1.5      matt 		static const struct {
    187  1.5      matt 			platid_mask_t *mask;
    188  1.5      matt 			struct wsmouse_calibcoords coords;
    189  1.5      matt 		} calibrations[] = {
    190  1.5      matt 			{ &platid_mask_MACH_NEC_MCR_700A,
    191  1.5      matt 				{ 0, 0, 799, 599,
    192  1.5      matt 				  4,
    193  1.5      matt 				{ { 115,  80,   0,   0 },
    194  1.5      matt 				  { 115, 966,   0, 599 },
    195  1.5      matt 				  { 912,  80, 799,   0 },
    196  1.5      matt 				  { 912, 966, 799, 599 } } } },
    197  1.5      matt 
    198  1.5      matt 			{ NULL,		/* samples got on my MC-R500 */
    199  1.5      matt 				{ 0, 0, 639, 239,
    200  1.5      matt 				5,
    201  1.5      matt 				{ { 502, 486, 320, 120 },
    202  1.5      matt 				  {  55, 109,   0,   0 },
    203  1.5      matt 				  {  54, 913,   0, 239 },
    204  1.5      matt 				  { 973, 924, 639, 239 },
    205  1.5      matt 				  { 975, 123, 639,   0 } } } },
    206  1.1  takemura 		};
    207  1.5      matt 		for (i = 0; ; i++) {
    208  1.5      matt 			if (calibrations[i].mask == NULL
    209  1.5      matt 			    || platid_match(&platid, calibrations[i].mask))
    210  1.5      matt 				break;
    211  1.5      matt 		}
    212  1.3  takemura 		tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
    213  1.5      matt 			      (caddr_t)&calibrations[i].coords, 0, 0);
    214  1.1  takemura 	}
    215  1.1  takemura #endif
    216  1.1  takemura 
    217  1.1  takemura 	/* install interrupt handler and enable interrupt */
    218  1.1  takemura 	if (!(sc->sc_handler =
    219  1.1  takemura 	      vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY,
    220  1.1  takemura 				  vrpiu_intr, sc))) {
    221  1.1  takemura 		printf (": can't map interrupt line.\n");
    222  1.1  takemura 		return;
    223  1.1  takemura 	}
    224  1.1  takemura 
    225  1.1  takemura 	/* mask level2 interrupt, stop scan sequencer and mask clock to piu */
    226  1.6      sato 	vrpiu_tp_disable(sc);
    227  1.1  takemura 
    228  1.1  takemura 	printf("\n");
    229  1.1  takemura 
    230  1.1  takemura 	wsmaa.accessops = &vrpiu_accessops;
    231  1.1  takemura 	wsmaa.accesscookie = sc;
    232  1.1  takemura 
    233  1.1  takemura 	/*
    234  1.1  takemura 	 * attach the wsmouse
    235  1.1  takemura 	 */
    236  1.1  takemura 	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint);
    237  1.6      sato 
    238  1.6      sato 	/*
    239  1.6      sato 	 * power management events
    240  1.6      sato 	 */
    241  1.6      sato 	sc->sc_power_hook = powerhook_establish(vrpiu_power, sc);
    242  1.6      sato 
    243  1.6      sato 	/*
    244  1.6      sato 	 * init A/D port polling.
    245  1.6      sato 	 */
    246  1.6      sato 	sc->sc_battery.n_values = 3;
    247  1.6      sato 	sc->sc_battery.value[0] = -1;
    248  1.6      sato 	sc->sc_battery.value[1] = -1;
    249  1.6      sato 	sc->sc_battery.value[2] = -1;
    250  1.6      sato 	sc->sc_battery.nextpoll = hz*vrpiu_ad_poll_interval;
    251  1.6      sato 	callout_init(&sc->sc_adpoll);
    252  1.6      sato 	callout_reset(&sc->sc_adpoll, hz,
    253  1.6      sato 			  vrpiu_start_powerstate, sc);
    254  1.1  takemura }
    255  1.1  takemura 
    256  1.1  takemura int
    257  1.6      sato vrpiu_ad_enable(v)
    258  1.1  takemura 	void *v;
    259  1.1  takemura {
    260  1.1  takemura 	struct vrpiu_softc *sc = v;
    261  1.1  takemura 	int s;
    262  1.1  takemura 	unsigned int cnt;
    263  1.1  takemura 
    264  1.6      sato 	DPRINTF(("%s(%d): vrpiu_ad_enable()\n", __FILE__, __LINE__));
    265  1.6      sato 	if (sc->sc_adstat != VRPIU_AD_STAT_DISABLE)
    266  1.1  takemura 		return EBUSY;
    267  1.1  takemura 
    268  1.1  takemura 	/* supply clock to PIU */
    269  1.1  takemura 	__vrcmu_supply(CMUMSKPIU, 1);
    270  1.1  takemura 
    271  1.1  takemura 	/* Scan interval 0x7FF is maximum value */
    272  1.1  takemura 	vrpiu_write(sc, PIUSIVL_REG_W, 0x7FF);
    273  1.1  takemura 
    274  1.1  takemura 	s = spltty();
    275  1.1  takemura 
    276  1.1  takemura 	/* clear interrupt status */
    277  1.6      sato 	vrpiu_write(sc, PIUINT_REG_W, PIUINT_PADADPINTR);
    278  1.1  takemura 
    279  1.1  takemura 	/* Disable -> Standby */
    280  1.1  takemura 	cnt = PIUCNT_PIUPWR |
    281  1.1  takemura 		PIUCNT_PIUMODE_COORDINATE |
    282  1.1  takemura 		PIUCNT_PADATSTART | PIUCNT_PADATSTOP;
    283  1.1  takemura 	vrpiu_write(sc, PIUCNT_REG_W, cnt);
    284  1.1  takemura 
    285  1.6      sato 	/* Level2 interrupt register setting */
    286  1.6      sato 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, PIUINT_PADADPINTR, 1);
    287  1.6      sato 
    288  1.1  takemura 	/* save pen status, touch or release */
    289  1.1  takemura 	cnt = vrpiu_read(sc, PIUCNT_REG_W);
    290  1.1  takemura 
    291  1.6      sato 	/*
    292  1.6      sato 	 * Enable scan sequencer operation
    293  1.6      sato 	 * Standby -> WaitPenTouch
    294  1.6      sato 	 */
    295  1.6      sato 	cnt |= PIUCNT_PIUSEQEN;
    296  1.6      sato 	vrpiu_write(sc, PIUCNT_REG_W, cnt);
    297  1.6      sato 
    298  1.6      sato 	sc->sc_adstat = VRPIU_AD_STAT_ENABLE;
    299  1.6      sato 
    300  1.6      sato 	splx(s);
    301  1.6      sato 
    302  1.6      sato 	return 0;
    303  1.6      sato }
    304  1.6      sato 
    305  1.6      sato void
    306  1.6      sato vrpiu_ad_disable(v)
    307  1.6      sato 	void *v;
    308  1.6      sato {
    309  1.6      sato 	struct vrpiu_softc *sc = v;
    310  1.6      sato 
    311  1.6      sato 	DPRINTF(("%s(%d): vrpiu_ad_disable()\n", __FILE__, __LINE__));
    312  1.6      sato 
    313  1.6      sato 	/* Set level2 interrupt register to mask interrupts */
    314  1.6      sato 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, PIUINT_PADADPINTR, 0);
    315  1.6      sato 
    316  1.6      sato 	sc->sc_adstat = VRPIU_AD_STAT_DISABLE;
    317  1.6      sato 
    318  1.6      sato 	if (sc->sc_tpstat == VRPIU_TP_STAT_DISABLE){
    319  1.6      sato 		/* Disable scan sequencer operation and power off */
    320  1.6      sato 		vrpiu_write(sc, PIUCNT_REG_W, 0);
    321  1.6      sato 
    322  1.6      sato 		/* mask clock to PIU */
    323  1.6      sato 		__vrcmu_supply(CMUMSKPIU, 1);
    324  1.6      sato 	}
    325  1.6      sato }
    326  1.6      sato 
    327  1.6      sato int
    328  1.6      sato vrpiu_tp_enable(v)
    329  1.6      sato 	void *v;
    330  1.6      sato {
    331  1.6      sato 	struct vrpiu_softc *sc = v;
    332  1.6      sato 	int s;
    333  1.6      sato 	unsigned int cnt;
    334  1.6      sato 
    335  1.6      sato 	DPRINTF(("%s(%d): vrpiu_tp_enable()\n", __FILE__, __LINE__));
    336  1.6      sato 	if (sc->sc_tpstat != VRPIU_TP_STAT_DISABLE)
    337  1.6      sato 		return EBUSY;
    338  1.6      sato 
    339  1.6      sato 	/* supply clock to PIU */
    340  1.6      sato 	__vrcmu_supply(CMUMSKPIU, 1);
    341  1.6      sato 
    342  1.6      sato 	/* Scan interval 0x7FF is maximum value */
    343  1.6      sato 	vrpiu_write(sc, PIUSIVL_REG_W, 0x7FF);
    344  1.6      sato 
    345  1.6      sato 	s = spltty();
    346  1.6      sato 
    347  1.6      sato 	/* clear interrupt status */
    348  1.6      sato 	vrpiu_write(sc, PIUINT_REG_W, PIUINT_ALLINTR&~PIUINT_PADADPINTR);
    349  1.6      sato 
    350  1.6      sato 	/* Disable -> Standby */
    351  1.6      sato 	cnt = PIUCNT_PIUPWR |
    352  1.6      sato 		PIUCNT_PIUMODE_COORDINATE |
    353  1.6      sato 		PIUCNT_PADATSTART | PIUCNT_PADATSTOP;
    354  1.6      sato 	vrpiu_write(sc, PIUCNT_REG_W, cnt);
    355  1.6      sato 
    356  1.1  takemura 	/* Level2 interrupt register setting */
    357  1.6      sato 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, PIUINT_ALLINTR&~PIUINT_PADADPINTR, 1);
    358  1.6      sato 
    359  1.6      sato 	/* save pen status, touch or release */
    360  1.6      sato 	cnt = vrpiu_read(sc, PIUCNT_REG_W);
    361  1.1  takemura 
    362  1.1  takemura 	/*
    363  1.1  takemura 	 * Enable scan sequencer operation
    364  1.1  takemura 	 * Standby -> WaitPenTouch
    365  1.1  takemura 	 */
    366  1.1  takemura 	cnt |= PIUCNT_PIUSEQEN;
    367  1.1  takemura 	vrpiu_write(sc, PIUCNT_REG_W, cnt);
    368  1.1  takemura 
    369  1.1  takemura 	/* transit status DISABLE -> TOUCH or RELEASE */
    370  1.6      sato 	sc->sc_tpstat = (cnt & PIUCNT_PENSTC) ?
    371  1.6      sato 		VRPIU_TP_STAT_TOUCH : VRPIU_TP_STAT_RELEASE;
    372  1.1  takemura 
    373  1.1  takemura 	splx(s);
    374  1.1  takemura 
    375  1.1  takemura 	return 0;
    376  1.1  takemura }
    377  1.1  takemura 
    378  1.1  takemura void
    379  1.6      sato vrpiu_tp_disable(v)
    380  1.1  takemura 	void *v;
    381  1.1  takemura {
    382  1.1  takemura 	struct vrpiu_softc *sc = v;
    383  1.1  takemura 
    384  1.6      sato 	DPRINTF(("%s(%d): vrpiu_tp_disable()\n", __FILE__, __LINE__));
    385  1.1  takemura 
    386  1.1  takemura 	/* Set level2 interrupt register to mask interrupts */
    387  1.6      sato 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, PIUINT_ALLINTR&~PIUINT_PADADPINTR, 0);
    388  1.1  takemura 
    389  1.6      sato 	sc->sc_tpstat = VRPIU_TP_STAT_DISABLE;
    390  1.1  takemura 
    391  1.6      sato 	if (sc->sc_adstat == VRPIU_AD_STAT_DISABLE){
    392  1.6      sato 		/* Disable scan sequencer operation and power off */
    393  1.6      sato 		vrpiu_write(sc, PIUCNT_REG_W, 0);
    394  1.1  takemura 
    395  1.6      sato 		/* mask clock to PIU */
    396  1.6      sato 		__vrcmu_supply(CMUMSKPIU, 1);
    397  1.6      sato 	}
    398  1.1  takemura }
    399  1.1  takemura 
    400  1.1  takemura int
    401  1.6      sato vrpiu_tp_ioctl(v, cmd, data, flag, p)
    402  1.1  takemura 	void *v;
    403  1.1  takemura 	u_long cmd;
    404  1.1  takemura 	caddr_t data;
    405  1.1  takemura 	int flag;
    406  1.1  takemura 	struct proc *p;
    407  1.1  takemura {
    408  1.1  takemura 	struct vrpiu_softc *sc = v;
    409  1.1  takemura 
    410  1.6      sato 	DPRINTF(("%s(%d): vrpiu_tp_ioctl(%08lx)\n", __FILE__, __LINE__, cmd));
    411  1.1  takemura 
    412  1.1  takemura 	switch (cmd) {
    413  1.1  takemura 	case WSMOUSEIO_GTYPE:
    414  1.2  takemura 		*(u_int *)data = WSMOUSE_TYPE_TPANEL;
    415  1.1  takemura 		break;
    416  1.1  takemura 
    417  1.1  takemura 	case WSMOUSEIO_SRES:
    418  1.1  takemura 		printf("%s(%d): WSMOUSRIO_SRES is not supported",
    419  1.1  takemura 		       __FILE__, __LINE__);
    420  1.1  takemura 		break;
    421  1.3  takemura 
    422  1.3  takemura 	case WSMOUSEIO_SCALIBCOORDS:
    423  1.3  takemura 	case WSMOUSEIO_GCALIBCOORDS:
    424  1.3  takemura                 return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, p);
    425  1.1  takemura 
    426  1.1  takemura 	default:
    427  1.1  takemura 		return (-1);
    428  1.1  takemura 	}
    429  1.1  takemura 	return (0);
    430  1.1  takemura }
    431  1.1  takemura 
    432  1.1  takemura /*
    433  1.6      sato  * PIU AD interrupt handler.
    434  1.6      sato  */
    435  1.6      sato void
    436  1.6      sato vrpiu_ad_intr(sc)
    437  1.6      sato 	struct vrpiu_softc *sc;
    438  1.6      sato {
    439  1.6      sato 	unsigned int i;
    440  1.6      sato 	unsigned int intrstat;
    441  1.6      sato 
    442  1.6      sato 	intrstat = vrpiu_read(sc, PIUINT_REG_W);
    443  1.6      sato 
    444  1.6      sato 	if (sc->sc_adstat == VRPIU_AD_STAT_DISABLE) {
    445  1.6      sato 		/*
    446  1.6      sato 		 * the device isn't enabled. just clear interrupt.
    447  1.6      sato 		 */
    448  1.6      sato 		vrpiu_write(sc, PIUINT_REG_W, PIUINT_PADADPINTR);
    449  1.6      sato 		return;
    450  1.6      sato 	}
    451  1.6      sato 
    452  1.6      sato 	if (intrstat & PIUINT_PADADPINTR) {
    453  1.6      sato 		sc->sc_battery.value[0] = (unsigned int)vrpiu_read(sc, PIUAB(0));
    454  1.6      sato 		sc->sc_battery.value[1] = (unsigned int)vrpiu_read(sc, PIUAB(1));
    455  1.6      sato 		sc->sc_battery.value[2] = (unsigned int)vrpiu_read(sc, PIUAB(2));
    456  1.6      sato 	}
    457  1.6      sato 
    458  1.6      sato 	if (intrstat & PIUINT_PADADPINTR) {
    459  1.6      sato 		for (i = 0; i < 3; i++) {
    460  1.6      sato 			if (sc->sc_battery.value[i] & PIUAB_VALID)
    461  1.6      sato 				sc->sc_battery.value[i] &= PIUAB_PADDATA_MASK;
    462  1.6      sato 			else
    463  1.6      sato 				sc->sc_battery.value[i] = 0;
    464  1.6      sato 		}
    465  1.6      sato 		vrpiu_calc_powerstate(sc);
    466  1.6      sato 	}
    467  1.6      sato 	vrpiu_write(sc, PIUINT_REG_W, PIUINT_PADADPINTR);
    468  1.6      sato 
    469  1.6      sato 	return;
    470  1.6      sato }
    471  1.6      sato /*
    472  1.6      sato  * PIU TP interrupt handler.
    473  1.1  takemura  */
    474  1.6      sato void
    475  1.6      sato vrpiu_tp_intr(sc)
    476  1.6      sato 	struct vrpiu_softc *sc;
    477  1.1  takemura {
    478  1.1  takemura 	unsigned int cnt, i;
    479  1.1  takemura 	unsigned int intrstat, page;
    480  1.1  takemura 	int tpx0, tpx1, tpy0, tpy1;
    481  1.1  takemura 	int x, y, xraw, yraw;
    482  1.1  takemura 
    483  1.1  takemura 	intrstat = vrpiu_read(sc, PIUINT_REG_W);
    484  1.1  takemura 
    485  1.6      sato 	if (sc->sc_tpstat == VRPIU_TP_STAT_DISABLE) {
    486  1.6      sato 	/*
    487  1.1  takemura 		 * the device isn't enabled. just clear interrupt.
    488  1.1  takemura 		 */
    489  1.6      sato 		vrpiu_write(sc, PIUINT_REG_W, intrstat&~PIUINT_PADADPINTR);
    490  1.6      sato 		return;
    491  1.1  takemura 	}
    492  1.1  takemura 
    493  1.1  takemura 	page = (intrstat & PIUINT_OVP) ? 1 : 0;
    494  1.1  takemura 	if (intrstat & (PIUINT_PADPAGE0INTR | PIUINT_PADPAGE1INTR)) {
    495  1.1  takemura 		tpx0 = vrpiu_read(sc, PIUPB(page, 0));
    496  1.1  takemura 		tpx1 = vrpiu_read(sc, PIUPB(page, 1));
    497  1.1  takemura 		tpy0 = vrpiu_read(sc, PIUPB(page, 2));
    498  1.1  takemura 		tpy1 = vrpiu_read(sc, PIUPB(page, 3));
    499  1.1  takemura 	}
    500  1.1  takemura 
    501  1.1  takemura 	if (intrstat & PIUINT_PADDLOSTINTR) {
    502  1.1  takemura 		page = page ? 0 : 1;
    503  1.1  takemura 		for (i = 0; i < 4; i++)
    504  1.1  takemura 			vrpiu_read(sc, PIUPB(page, i));
    505  1.1  takemura 	}
    506  1.1  takemura 
    507  1.1  takemura 	cnt = vrpiu_read(sc, PIUCNT_REG_W);
    508  1.1  takemura #ifdef DEBUG
    509  1.1  takemura 	if (vrpiu_debug)
    510  1.1  takemura 		vrpiu_dump_cntreg(cnt);
    511  1.1  takemura #endif
    512  1.1  takemura 
    513  1.1  takemura 	/* clear interrupt status */
    514  1.6      sato 	vrpiu_write(sc, PIUINT_REG_W, intrstat&~PIUINT_PADADPINTR);
    515  1.1  takemura 
    516  1.1  takemura #if 0
    517  1.1  takemura 	DPRINTF(("vrpiu_intr: OVP=%d", page));
    518  1.1  takemura 	if (intrstat & PIUINT_PADCMDINTR)
    519  1.1  takemura 		DPRINTF((" CMD"));
    520  1.1  takemura 	if (intrstat & PIUINT_PADADPINTR)
    521  1.1  takemura 		DPRINTF((" A/D"));
    522  1.1  takemura 	if (intrstat & PIUINT_PADPAGE1INTR)
    523  1.1  takemura 		DPRINTF((" PAGE1"));
    524  1.1  takemura 	if (intrstat & PIUINT_PADPAGE0INTR)
    525  1.1  takemura 		DPRINTF((" PAGE0"));
    526  1.1  takemura 	if (intrstat & PIUINT_PADDLOSTINTR)
    527  1.1  takemura 		DPRINTF((" DLOST"));
    528  1.1  takemura 	if (intrstat & PIUINT_PENCHGINTR)
    529  1.1  takemura 		DPRINTF((" PENCHG"));
    530  1.1  takemura 	DPRINTF(("\n"));
    531  1.1  takemura #endif
    532  1.1  takemura 	if (intrstat & (PIUINT_PADPAGE0INTR | PIUINT_PADPAGE1INTR)) {
    533  1.1  takemura 		if (!((tpx0 & PIUPB_VALID) && (tpx1 & PIUPB_VALID) &&
    534  1.1  takemura 		      (tpy0 & PIUPB_VALID) && (tpy1 & PIUPB_VALID))) {
    535  1.1  takemura 			printf("vrpiu: internal error, data is not valid!\n");
    536  1.1  takemura 		} else {
    537  1.1  takemura 			tpx0 &= PIUPB_PADDATA_MASK;
    538  1.1  takemura 			tpx1 &= PIUPB_PADDATA_MASK;
    539  1.1  takemura 			tpy0 &= PIUPB_PADDATA_MASK;
    540  1.1  takemura 			tpy1 &= PIUPB_PADDATA_MASK;
    541  1.1  takemura #define ISVALID(n, c, m)	((c) - (m) < (n) && (n) < (c) + (m))
    542  1.1  takemura 			if (ISVALID(tpx0 + tpx1, 1024, 200) &&
    543  1.1  takemura 			    ISVALID(tpx0 + tpx1, 1024, 200)) {
    544  1.1  takemura #if 0
    545  1.1  takemura 				DPRINTF(("%04x %04x %04x %04x\n",
    546  1.1  takemura 					 tpx0, tpx1, tpy0, tpy1));
    547  1.1  takemura 				DPRINTF(("%3d %3d (%4d %4d)->", tpx0, tpy0,
    548  1.4  takemura 					 tpx0 + tpx1, tpy0 + tpy1));
    549  1.1  takemura #endif
    550  1.1  takemura 				xraw = tpy1 * 1024 / (tpy0 + tpy1);
    551  1.1  takemura 				yraw = tpx1 * 1024 / (tpx0 + tpx1);
    552  1.1  takemura 				DPRINTF(("%3d %3d", xraw, yraw));
    553  1.1  takemura 
    554  1.3  takemura 				tpcalib_trans(&sc->sc_tpcalib,
    555  1.3  takemura 					      xraw, yraw, &x, &y);
    556  1.3  takemura 
    557  1.1  takemura 				DPRINTF(("->%4d %4d", x, y));
    558  1.2  takemura 				wsmouse_input(sc->sc_wsmousedev,
    559  1.2  takemura 					      (cnt & PIUCNT_PENSTC) ? 1 : 0,
    560  1.2  takemura 					      x, /* x */
    561  1.2  takemura 					      y, /* y */
    562  1.2  takemura 					      0, /* z */
    563  1.2  takemura 					      WSMOUSE_INPUT_ABSOLUTE_X |
    564  1.2  takemura 					      WSMOUSE_INPUT_ABSOLUTE_Y);
    565  1.1  takemura 				DPRINTF(("\n"));
    566  1.1  takemura 			}
    567  1.4  takemura 		}
    568  1.4  takemura 	}
    569  1.4  takemura 
    570  1.4  takemura 	if (cnt & PIUCNT_PENSTC) {
    571  1.6      sato 		if (sc->sc_tpstat == VRPIU_TP_STAT_RELEASE) {
    572  1.4  takemura 			/*
    573  1.4  takemura 			 * pen touch
    574  1.4  takemura 			 */
    575  1.4  takemura 			DPRINTF(("PEN TOUCH\n"));
    576  1.6      sato 			sc->sc_tpstat = VRPIU_TP_STAT_TOUCH;
    577  1.4  takemura 			/*
    578  1.4  takemura 			 * We should not report button down event while
    579  1.4  takemura 			 * we don't know where it occur.
    580  1.4  takemura 			 */
    581  1.4  takemura 		}
    582  1.4  takemura 	} else {
    583  1.6      sato 		if (sc->sc_tpstat == VRPIU_TP_STAT_TOUCH) {
    584  1.4  takemura 			/*
    585  1.4  takemura 			 * pen release
    586  1.4  takemura 			 */
    587  1.4  takemura 			DPRINTF(("RELEASE\n"));
    588  1.6      sato 			sc->sc_tpstat = VRPIU_TP_STAT_RELEASE;
    589  1.4  takemura 			/* button 0 UP */
    590  1.4  takemura 			wsmouse_input(sc->sc_wsmousedev,
    591  1.4  takemura 				      0,
    592  1.4  takemura 				      0, 0, 0, 0);
    593  1.1  takemura 		}
    594  1.1  takemura 	}
    595  1.1  takemura 
    596  1.1  takemura 	if (intrstat & PIUINT_PADDLOSTINTR) {
    597  1.1  takemura 		cnt |= PIUCNT_PIUSEQEN;
    598  1.1  takemura 		vrpiu_write(sc, PIUCNT_REG_W, cnt);
    599  1.1  takemura 	}
    600  1.1  takemura 
    601  1.6      sato 	return;
    602  1.6      sato }
    603  1.6      sato 
    604  1.6      sato /*
    605  1.6      sato  * PIU interrupt handler.
    606  1.6      sato  */
    607  1.6      sato int
    608  1.6      sato vrpiu_intr(arg)
    609  1.6      sato 	void *arg;
    610  1.6      sato {
    611  1.6      sato         struct vrpiu_softc *sc = arg;
    612  1.6      sato 
    613  1.6      sato 	vrpiu_ad_intr(sc);
    614  1.6      sato 	vrpiu_tp_intr(sc);
    615  1.6      sato 
    616  1.1  takemura 	return 0;
    617  1.6      sato }
    618  1.6      sato 
    619  1.6      sato void
    620  1.6      sato vrpiu_start_powerstate(v)
    621  1.6      sato 	void *v;
    622  1.6      sato {
    623  1.6      sato 	int mask;
    624  1.6      sato 	struct vrpiu_softc *sc = (struct vrpiu_softc *)v;
    625  1.6      sato 
    626  1.6      sato 	vrpiu_ad_enable(sc);
    627  1.6      sato 	mask = vrpiu_read(sc, PIUAMSK_REG_W);
    628  1.6      sato 	mask &= 0xff8f; /* XXX */
    629  1.6      sato 	vrpiu_write(sc, PIUAMSK_REG_W, mask);
    630  1.6      sato 	vrpiu_write(sc, PIUASCN_REG_W, PIUACN_ADPSSTART);
    631  1.6      sato 	/*
    632  1.6      sato 	 * restart next A/D polling
    633  1.6      sato 	 */
    634  1.6      sato 	callout_reset(&sc->sc_adpoll, hz*vrpiu_ad_poll_interval,
    635  1.6      sato 			 vrpiu_start_powerstate, sc);
    636  1.6      sato }
    637  1.6      sato 
    638  1.6      sato void
    639  1.6      sato vrpiu_calc_powerstate(sc)
    640  1.6      sato 	struct vrpiu_softc *sc;
    641  1.6      sato {
    642  1.6      sato 	extern void vrgiu_diff_io __P((void));
    643  1.6      sato 	vrpiu_ad_disable(sc);
    644  1.6      sato 	VPRINTF(("vrpiu:AD: %d, %d, %d\n",
    645  1.6      sato 		sc->sc_battery.value[0],
    646  1.6      sato 		sc->sc_battery.value[1],
    647  1.6      sato 		sc->sc_battery.value[2]));
    648  1.6      sato 	sc->sc_battery.nextpoll = hz*vrpiu_ad_poll_interval;
    649  1.6      sato #ifdef notyet
    650  1.7      sato 	config_hook_call(CONFIG_HOOK_SET,
    651  1.7      sato 			 CONFIG_HOOK_BATTERYVAL,
    652  1.6      sato 			 (void *)&sc->sc_battery);
    653  1.6      sato #endif /* notyet */
    654  1.6      sato 	/*
    655  1.6      sato 	 * restart next A/D polling if change polling timming.
    656  1.6      sato 	 */
    657  1.6      sato 	if (sc->sc_battery.nextpoll != hz*vrpiu_ad_poll_interval)
    658  1.6      sato 		callout_reset(&sc->sc_adpoll, sc->sc_battery.nextpoll,
    659  1.6      sato 				 vrpiu_start_powerstate, sc);
    660  1.6      sato 	if (bootverbose)
    661  1.6      sato 		vrgiu_diff_io();
    662  1.6      sato 
    663  1.6      sato }
    664  1.6      sato 
    665  1.6      sato static void
    666  1.6      sato vrpiu_power(why, arg)
    667  1.6      sato 	int why;
    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 	switch (why) {
    673  1.6      sato 	case PWR_STANDBY:
    674  1.6      sato 	case PWR_SUSPEND:
    675  1.6      sato 		break;
    676  1.6      sato 	case PWR_RESUME:
    677  1.6      sato 		callout_reset(&sc->sc_adpoll, hz,
    678  1.6      sato 				  vrpiu_start_powerstate, sc);
    679  1.6      sato 		break;
    680  1.6      sato 	}
    681  1.1  takemura }
    682  1.1  takemura 
    683  1.1  takemura #ifdef DEBUG
    684  1.1  takemura void
    685  1.1  takemura vrpiu_dump_cntreg(cnt)
    686  1.1  takemura 	unsigned int cnt;
    687  1.1  takemura {
    688  1.1  takemura 	printf("%s", (cnt & PIUCNT_PENSTC) ? "Touch" : "Release");
    689  1.1  takemura 	printf(" state=");
    690  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_CmdScan)
    691  1.1  takemura 		printf("CmdScan");
    692  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_IntervalNextScan)
    693  1.1  takemura 		printf("IntervalNextScan");
    694  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_PenDataScan)
    695  1.1  takemura 		printf("PenDataScan");
    696  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_WaitPenTouch)
    697  1.1  takemura 		printf("WaitPenTouch");
    698  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_RFU)
    699  1.1  takemura 		printf("???");
    700  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_ADPortScan)
    701  1.1  takemura 		printf("ADPortScan");
    702  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_Standby)
    703  1.1  takemura 		printf("Standby");
    704  1.1  takemura 	if ((cnt & PIUCNT_PADSTATE_MASK) == PIUCNT_PADSTATE_Disable)
    705  1.1  takemura 		printf("Disable");
    706  1.1  takemura 	if (cnt & PIUCNT_PADATSTOP)
    707  1.1  takemura 		printf(" AutoStop");
    708  1.1  takemura 	if (cnt & PIUCNT_PADATSTART)
    709  1.1  takemura 		printf(" AutoStart");
    710  1.1  takemura 	if (cnt & PIUCNT_PADSCANSTOP)
    711  1.1  takemura 		printf(" Stop");
    712  1.1  takemura 	if (cnt & PIUCNT_PADSCANSTART)
    713  1.1  takemura 		printf(" Start");
    714  1.1  takemura 	if (cnt & PIUCNT_PADSCANTYPE)
    715  1.1  takemura 		printf(" ScanPressure");
    716  1.1  takemura 	if ((cnt & PIUCNT_PIUMODE_MASK) == PIUCNT_PIUMODE_ADCONVERTER)
    717  1.1  takemura 		printf(" A/D");
    718  1.1  takemura 	if ((cnt & PIUCNT_PIUMODE_MASK) == PIUCNT_PIUMODE_COORDINATE)
    719  1.1  takemura 		printf(" Coordinate");
    720  1.1  takemura 	if (cnt & PIUCNT_PIUSEQEN)
    721  1.1  takemura 		printf(" SeqEn");
    722  1.1  takemura 	if ((cnt & PIUCNT_PIUPWR) == 0)
    723  1.1  takemura 		printf(" PowerOff");
    724  1.1  takemura 	if ((cnt & PIUCNT_PADRST) == 0)
    725  1.1  takemura 		printf(" Reset");
    726  1.1  takemura 	printf("\n");
    727  1.1  takemura }
    728  1.1  takemura #endif
    729