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