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