Home | History | Annotate | Line # | Download | only in dev
j720ssp.c revision 1.19
      1 /* $NetBSD: j720ssp.c,v 1.19 2003/08/07 16:27:47 agc Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1990 The Regents of the University of California.
     41  * All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * William Jolitz and Don Ahn.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)pccons.c	5.11 (Berkeley) 5/21/91
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: j720ssp.c,v 1.19 2003/08/07 16:27:47 agc Exp $");
     75 
     76 #include "apm.h"
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/device.h>
     81 #include <sys/kernel.h>
     82 #include <sys/malloc.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/kthread.h>
     85 #include <sys/lock.h>
     86 
     87 #include <machine/bus.h>
     88 #include <machine/config_hook.h>
     89 #include <machine/bootinfo.h>
     90 #include <machine/apmvar.h>
     91 
     92 #include <hpcarm/dev/sed1356var.h>
     93 
     94 #include <arm/sa11x0/sa11x0_var.h>
     95 #include <arm/sa11x0/sa11x0_gpioreg.h>
     96 #include <arm/sa11x0/sa11x0_ppcreg.h>
     97 #include <arm/sa11x0/sa11x0_sspreg.h>
     98 
     99 #include <dev/wscons/wsconsio.h>
    100 #include <dev/wscons/wskbdvar.h>
    101 #include <dev/wscons/wsksymdef.h>
    102 #include <dev/wscons/wsksymvar.h>
    103 #include <dev/wscons/wsmousevar.h>
    104 #include <dev/hpc/tpcalibvar.h>
    105 
    106 extern const struct wscons_keydesc j720kbd_keydesctab[];
    107 
    108 struct j720ssp_softc {
    109         struct device sc_dev;
    110 
    111 	bus_space_tag_t sc_iot;
    112 	bus_space_handle_t sc_gpioh;
    113 	bus_space_handle_t sc_ssph;
    114 
    115 	struct device *sc_wskbddev;
    116 	struct device *sc_wsmousedev;
    117 	struct tpcalib_softc sc_tpcalib;
    118 
    119 	void *sc_kbdsi;
    120 	void *sc_tpsi;
    121 	int sc_enabled;
    122 
    123 	struct proc *sc_ssp_kthread;
    124 	int sc_ssp_status;
    125 	struct simplelock sc_ssp_status_lock;
    126 };
    127 /* Values for struct softc's sc_ssp_status */
    128 #define J720_SSP_STATUS_NONE	0
    129 #define J720_SSP_STATUS_TP	1
    130 #define J720_SSP_STATUS_KBD	2
    131 
    132 void j720ssp_create_kthread __P((void *));
    133 void j720ssp_kthread __P((void *));
    134 int  j720kbd_intr __P((void *));
    135 int  j720tp_intr __P((void *));
    136 void j720kbd_poll __P((void *));
    137 int  j720tp_poll __P((void *));
    138 
    139 int  j720lcdparam __P((void *, int, long, void *));
    140 static void j720kbd_read __P((struct j720ssp_softc *, char *));
    141 static int  j720ssp_readwrite __P((struct j720ssp_softc *, int,
    142 	int, int *, int));
    143 
    144 int  j720sspprobe __P((struct device *, struct cfdata *, void *));
    145 void j720sspattach __P((struct device *, struct device *, void *));
    146 
    147 int  j720kbd_submatch __P((struct device *, struct cfdata *, void *));
    148 int  j720tp_submatch __P((struct device *, struct cfdata *, void *));
    149 int  apm_submatch __P((struct device *, struct cfdata *, void *));
    150 
    151 int  j720kbd_enable __P((void *, int));
    152 void j720kbd_set_leds __P((void *, int));
    153 int  j720kbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    154 
    155 int  j720lcdpower(void *, int, long, void *);
    156 int  hpcarm_apm_getpower __P((struct apm_power_info *, void *));
    157 
    158 CFATTACH_DECL(j720ssp, sizeof(struct j720ssp_softc),
    159     j720sspprobe, j720sspattach, NULL, NULL);
    160 
    161 const struct wskbd_accessops j720kbd_accessops = {
    162 	j720kbd_enable,
    163 	j720kbd_set_leds,
    164 	j720kbd_ioctl,
    165 };
    166 
    167 void j720kbd_cngetc __P((void *, u_int *, int *));
    168 void j720kbd_cnpollc __P((void *, int));
    169 void j720kbd_cnbell __P((void *, u_int, u_int, u_int));
    170 
    171 const struct wskbd_consops j720kbd_consops = {
    172 	j720kbd_cngetc,
    173 	j720kbd_cnpollc,
    174 	j720kbd_cnbell,
    175 };
    176 
    177 const struct wskbd_mapdata j720kbd_keymapdata = {
    178 	j720kbd_keydesctab,
    179 #ifdef J720KBD_LAYOUT
    180 	J720KBD_LAYOUT,
    181 #else
    182 	KB_US,
    183 #endif
    184 };
    185 
    186 static int  j720tp_enable __P((void *));
    187 static int  j720tp_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    188 static void j720tp_disable __P((void *));
    189 
    190 const struct wsmouse_accessops j720tp_accessops = {
    191 	j720tp_enable,
    192 	j720tp_ioctl,
    193 	j720tp_disable,
    194 };
    195 
    196 static int j720ssp_powerstate = 1;
    197 
    198 static struct j720ssp_softc j720kbdcons_sc;
    199 static int j720kbdcons_initstate = 0;
    200 
    201 #define DEBUG
    202 #ifdef DEBUG
    203 int j720sspwaitcnt;
    204 int j720sspwaittime;
    205 extern int gettick(void);
    206 #endif
    207 
    208 #define BIT_INVERT(x)	do {					\
    209 	(x) = ((((x) & 0xf0) >> 4) | (((x) & 0x0f) << 4));	\
    210 	(x) = ((((x) & 0xcc) >> 2) | (((x) & 0x33) << 2));	\
    211 	(x) = ((((x) & 0xaa) >> 1) | (((x) & 0x55) << 1));	\
    212 	} while(0)
    213 
    214 
    215 int
    216 j720sspprobe(parent, cf, aux)
    217 	struct device *parent;
    218 	struct cfdata *cf;
    219 	void *aux;
    220 {
    221 	return (1);
    222 }
    223 
    224 void
    225 j720sspattach(parent, self, aux)
    226 	struct device *parent;
    227 	struct device *self;
    228 	void *aux;
    229 {
    230 	struct j720ssp_softc *sc = (void *)self;
    231 	struct sa11x0_softc *psc = (void *)parent;
    232 	struct sa11x0_attach_args *sa = aux;
    233 	struct wskbddev_attach_args kbd_args;
    234 	struct wsmousedev_attach_args mouse_args;
    235 #if NAPM > 0
    236 	struct apm_attach_args apm_args;
    237 #endif
    238 
    239 	printf("\n");
    240 
    241 	sc->sc_iot = psc->sc_iot;
    242 	sc->sc_gpioh = psc->sc_gpioh;
    243 	if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0,
    244 			  &sc->sc_ssph)) {
    245 		printf("%s: unable to map SSP registers\n",
    246 		       sc->sc_dev.dv_xname);
    247 		return;
    248 	}
    249 
    250 	sc->sc_ssp_status = J720_SSP_STATUS_NONE;
    251 	simple_lock_init(&sc->sc_ssp_status_lock);
    252 	kthread_create(j720ssp_create_kthread, sc);
    253 
    254 	sc->sc_enabled = 0;
    255 
    256 	kbd_args.console = 0;
    257 
    258 	kbd_args.keymap = &j720kbd_keymapdata;
    259 
    260 	kbd_args.accessops = &j720kbd_accessops;
    261 	kbd_args.accesscookie = sc;
    262 
    263 	/* Do console initialization */
    264 	if (! (bootinfo->bi_cnuse & BI_CNUSE_SERIAL)) {
    265 		j720kbdcons_sc = *sc;
    266 		kbd_args.console = 1;
    267 
    268 		wskbd_cnattach(&j720kbd_consops, NULL, &j720kbd_keymapdata);
    269 		j720kbdcons_initstate = 1;
    270 	}
    271 
    272 	/*
    273 	 * Attach the wskbd, saving a handle to it.
    274 	 * XXX XXX XXX
    275 	 */
    276 	sc->sc_wskbddev = config_found_sm(self, &kbd_args, wskbddevprint,
    277 	    j720kbd_submatch);
    278 
    279 #ifdef DEBUG
    280 	/* Zero the stat counters */
    281 	j720sspwaitcnt = 0;
    282 	j720sspwaittime = 0;
    283 #endif
    284 
    285 	if (j720kbdcons_initstate == 1)
    286 		j720kbd_enable(sc, 1);
    287 
    288 	mouse_args.accessops = &j720tp_accessops;
    289 	mouse_args.accesscookie = sc;
    290 
    291 	sc->sc_wsmousedev = config_found_sm(self, &mouse_args,
    292 	    wsmousedevprint, j720tp_submatch);
    293 	tpcalib_init(&sc->sc_tpcalib);
    294 
    295 	/* XXX fill in "default" calibrate param */
    296 	{
    297 		static const struct wsmouse_calibcoords j720_default_calib = {
    298 			0, 0, 639, 239,
    299 			4,
    300 			{ { 988,  80,   0,   0 },
    301 			  {  88,  84, 639,   0 },
    302 			  { 988, 927,   0, 239 },
    303 			  {  88, 940, 639, 239 } } };
    304 		tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
    305 		    (caddr_t)&j720_default_calib, 0, 0);
    306 	}
    307 
    308 	j720tp_disable(sc);
    309 
    310 	/* Setup touchpad interrupt */
    311 	sa11x0_intr_establish(0, 9, 1, IPL_BIO, j720tp_intr, sc);
    312 
    313 	/* LCD control is on the same bus */
    314 	config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS,
    315 		    CONFIG_HOOK_SHARE, j720lcdparam, sc);
    316 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS,
    317 		    CONFIG_HOOK_SHARE, j720lcdparam, sc);
    318 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS_MAX,
    319 		    CONFIG_HOOK_SHARE, j720lcdparam, sc);
    320 
    321 	config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST,
    322 		    CONFIG_HOOK_SHARE, j720lcdparam, sc);
    323 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST,
    324 		    CONFIG_HOOK_SHARE, j720lcdparam, sc);
    325 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST_MAX,
    326 		    CONFIG_HOOK_SHARE, j720lcdparam, sc);
    327 
    328 #if NAPM > 0
    329 	/* attach APM emulation */
    330 	apm_args.aaa_magic = APM_ATTACH_ARGS_MAGIC; /* magic number */
    331 	(void)config_found_sm(self, &apm_args, NULL, apm_submatch);
    332 #endif
    333 
    334 	return;
    335 }
    336 
    337 void
    338 j720ssp_create_kthread(arg)
    339 	void *arg;
    340 {
    341 	struct j720ssp_softc *sc = arg;
    342 
    343 	if (kthread_create1(j720ssp_kthread, sc,
    344 	    &sc->sc_ssp_kthread, "j720ssp"))
    345 		panic("j720ssp_create_kthread");
    346 
    347 	return;
    348 }
    349 
    350 void
    351 j720ssp_kthread(arg)
    352 	void *arg;
    353 {
    354 	struct j720ssp_softc *sc = arg;
    355 	int ssp_status;
    356 
    357 	while (1) {
    358 		if (ssp_status & J720_SSP_STATUS_TP)
    359 			tsleep(&sc->sc_ssp_kthread, PRIBIO, "j720ssp", hz / 25);
    360 		else
    361 			tsleep(&sc->sc_ssp_kthread, PRIBIO, "j720ssp", 0);
    362 
    363 		simple_lock(&sc->sc_ssp_status_lock);
    364 		ssp_status = sc->sc_ssp_status;
    365 		sc->sc_ssp_status &= ~J720_SSP_STATUS_KBD;
    366 		simple_unlock(&sc->sc_ssp_status_lock);
    367 
    368 		if (ssp_status & J720_SSP_STATUS_KBD)
    369 			j720kbd_poll(sc);
    370 
    371 		if (ssp_status & J720_SSP_STATUS_TP) {
    372 			if (j720tp_poll(sc) == 0) {
    373 				simple_lock(&sc->sc_ssp_status_lock);
    374 				sc->sc_ssp_status &= ~J720_SSP_STATUS_TP;
    375 				simple_unlock(&sc->sc_ssp_status_lock);
    376 			}
    377 		}
    378 	}
    379 
    380 	/* NOTREACHED */
    381 }
    382 
    383 
    384 int
    385 j720kbd_submatch(parent, cf, aux)
    386 	struct device *parent;
    387 	struct cfdata *cf;
    388 	void *aux;
    389 {
    390 	if (strcmp(cf->cf_name, "wskbd") == 0)
    391 		return (1);
    392 	return (0);
    393 }
    394 
    395 int
    396 j720tp_submatch(parent, cf, aux)
    397 	struct device *parent;
    398 	struct cfdata *cf;
    399 	void *aux;
    400 {
    401 	if (strcmp(cf->cf_name, "wsmouse") == 0)
    402 		return (1);
    403 	return (0);
    404 }
    405 
    406 int
    407 apm_submatch(parent, cf, aux)
    408 	struct device *parent;
    409 	struct cfdata *cf;
    410 	void *aux;
    411 {
    412 	if (strcmp(cf->cf_name, "apm") == 0)
    413 		return (1);
    414 	return (0);
    415 }
    416 
    417 int
    418 j720kbd_enable(v, on)
    419 	void *v;
    420 	int on;
    421 {
    422 	struct j720ssp_softc *sc = v;
    423 
    424 	if (! sc->sc_enabled) {
    425 		sc->sc_enabled = 1;
    426 
    427 		sa11x0_intr_establish(0, 0, 1, IPL_BIO, j720kbd_intr, sc);
    428 	}
    429 	/* XXX */
    430 	return (0);
    431 }
    432 
    433 void
    434 j720kbd_set_leds(v, on)
    435 	void *v;
    436 	int on;
    437 {
    438 	/* XXX */
    439 	return;
    440 }
    441 
    442 int
    443 j720kbd_ioctl(v, cmd, data, flag, p)
    444 	void *v;
    445 	u_long cmd;
    446 	caddr_t data;
    447 	int flag;
    448 	struct proc *p;
    449 {
    450 	switch (cmd) {
    451 	case WSKBDIO_GTYPE:
    452 		*(int *)data = WSKBD_TYPE_HPC_KBD;
    453 		return 0;
    454 	}
    455 
    456 	return (EPASSTHROUGH);
    457 }
    458 
    459 int
    460 j720kbd_intr(arg)
    461 	void *arg;
    462 {
    463 	struct j720ssp_softc *sc = arg;
    464 
    465 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_EDR, 1);
    466 
    467 	simple_lock(&sc->sc_ssp_status_lock);
    468 	sc->sc_ssp_status |= J720_SSP_STATUS_KBD;
    469 	simple_unlock(&sc->sc_ssp_status_lock);
    470 
    471 	wakeup(&sc->sc_ssp_kthread);
    472 
    473 	return (1);
    474 }
    475 
    476 int
    477 j720tp_intr(arg)
    478 	void *arg;
    479 {
    480 	struct j720ssp_softc *sc = arg;
    481 
    482 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_EDR, 1 << 9);
    483 
    484 	simple_lock(&sc->sc_ssp_status_lock);
    485 	sc->sc_ssp_status |= J720_SSP_STATUS_TP;
    486 	simple_unlock(&sc->sc_ssp_status_lock);
    487 
    488 	j720tp_disable(sc);
    489 	wakeup(&sc->sc_ssp_kthread);
    490 
    491 	return (1);
    492 }
    493 
    494 void
    495 j720kbd_poll(arg)
    496 	void *arg;
    497 {
    498 	struct j720ssp_softc *sc = arg;
    499 	int s, type, value;
    500 	char buf[9], *p;
    501 
    502 	j720kbd_read(sc, buf);
    503 
    504 	for(p = buf; *p; p++) {
    505 		type = *p & 0x80 ? WSCONS_EVENT_KEY_UP :
    506 		    WSCONS_EVENT_KEY_DOWN;
    507 		value = *p & 0x7f;
    508 		s = spltty();
    509 		wskbd_input(sc->sc_wskbddev, type, value);
    510 		splx(s);
    511 		if (type == WSCONS_EVENT_KEY_DOWN &&
    512 		    value == 0x7f) {
    513 			j720ssp_powerstate = ! j720ssp_powerstate;
    514 			config_hook_call(CONFIG_HOOK_POWERCONTROL,
    515 					 CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
    516 					 (void *)j720ssp_powerstate);
    517 		}
    518 	}
    519 
    520 	return;
    521 }
    522 
    523 void
    524 j720kbd_read(sc, buf)
    525 	struct j720ssp_softc *sc;
    526 	char *buf;
    527 {
    528 	int data, count;
    529 #ifdef DEBUG
    530 	u_int32_t oscr;
    531 
    532 	oscr = gettick();
    533 #endif
    534 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PCR, 0x2000000);
    535 
    536 	/* send scan keycode command */
    537 	if (j720ssp_readwrite(sc, 1, 0x900, &data, 100) < 0 ||
    538 	    data != 0x88)
    539 		goto out;
    540 
    541 	/* read numbers of scancode available */
    542 	if (j720ssp_readwrite(sc, 0, 0x8800, &data, 100) < 0)
    543 		goto out;
    544 	BIT_INVERT(data);
    545 	count = data;
    546 
    547 	for(; count; count--) {
    548 		if (j720ssp_readwrite(sc, 0, 0x8800, &data, 100) < 0)
    549 			goto out;
    550 		BIT_INVERT(data);
    551 		*buf++ = data;
    552 	}
    553 	*buf = 0;
    554 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
    555 
    556 #ifdef DEBUG
    557 	oscr = (u_int32_t)gettick() - oscr;
    558 	j720sspwaitcnt++;
    559 	j720sspwaittime += oscr;
    560 #endif
    561 
    562 	return;
    563 
    564 out:
    565 	*buf = 0;
    566 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
    567 
    568 	/* reset SSP */
    569 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x307);
    570 	delay(100);
    571 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x387);
    572 
    573 	printf("j720kbd_read: error %x\n", data);
    574 	return;
    575 }
    576 
    577 int
    578 j720tp_poll(arg)
    579 	void *arg;
    580 {
    581 	struct j720ssp_softc *sc = arg;
    582 	int buf[8], data, i, x, y;
    583 
    584 	/*
    585 	 * If touch panel is not touched anymore,
    586 	 * stop polling and re-enable interrupt
    587 	 */
    588 	if (bus_space_read_4(sc->sc_iot,
    589 	    sc->sc_gpioh, SAGPIO_PLR) & (1 << 9)) {
    590 		wsmouse_input(sc->sc_wsmousedev, 0, 0, 0, 0, 0);
    591 		j720tp_enable(sc);
    592 		return 0;
    593 	}
    594 
    595 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PCR, 0x2000000);
    596 
    597 	/* send read touchpanel command */
    598 	if (j720ssp_readwrite(sc, 1, 0x500, &data, 100) < 0 ||
    599 	    data != 0x88)
    600 		goto out;
    601 
    602 	for(i = 0; i < 8; i++) {
    603 		if (j720ssp_readwrite(sc, 0, 0x8800, &data, 100) < 0)
    604 			goto out;
    605 		BIT_INVERT(data);
    606 		buf[i] = data;
    607 	}
    608 
    609 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
    610 
    611 	buf[6] <<= 8;
    612 	buf[7] <<= 8;
    613 	for(i = 0; i < 3; i++) {
    614 		buf[i] |= buf[6] & 0x300;
    615 		buf[6] >>= 2;
    616 		buf[i + 3] |= buf[7] & 0x300;
    617 		buf[7] >>= 2;
    618 	}
    619 #if 0
    620 	printf("j720tp_poll: %d %d %d  %d %d %d\n", buf[0], buf[1], buf[2],
    621 	    buf[3], buf[4], buf[5]);
    622 #endif
    623 
    624 	/* XXX buf[1], buf[2], ... should also be used */
    625 	tpcalib_trans(&sc->sc_tpcalib, buf[1], buf[4], &x, &y);
    626 	wsmouse_input(sc->sc_wsmousedev, 1, x, y, 0,
    627 	    WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
    628 
    629 	return 1;
    630 
    631 out:
    632 	*buf = 0;
    633 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
    634 
    635 	/* reset SSP */
    636 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x307);
    637 	delay(100);
    638 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x387);
    639 	printf("j720tp_poll: error %x\n", data);
    640 
    641 	return 0;
    642 }
    643 
    644 static int
    645 j720tp_enable(arg)
    646 	void *arg;
    647 {
    648 	struct j720ssp_softc *sc = arg;
    649 	int er, s;
    650 
    651 	s = splhigh();
    652 	er = bus_space_read_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_FER);
    653 	er |= 1 << 9;
    654 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_FER, er);
    655 	splx(s);
    656 
    657 	return (0);
    658 }
    659 
    660 static void
    661 j720tp_disable(arg)
    662 	void *arg;
    663 {
    664 	struct j720ssp_softc *sc = arg;
    665 	int er, s;
    666 
    667 	s = splhigh();
    668 	er = bus_space_read_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_FER);
    669 	er &= ~(1 << 9);
    670 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_FER, er);
    671 	splx(s);
    672 }
    673 
    674 static int
    675 j720tp_ioctl(arg, cmd, data, flag, p)
    676 	void *arg;
    677 	u_long cmd;
    678 	caddr_t data;
    679 	int flag;
    680 	struct proc *p;
    681 {
    682 	struct j720ssp_softc *sc = arg;
    683 
    684 	switch (cmd) {
    685 	case WSMOUSEIO_GTYPE:
    686 		*(u_int *)data = WSMOUSE_TYPE_TPANEL;
    687 		return (0);
    688 
    689 	case WSMOUSEIO_SCALIBCOORDS:
    690 	case WSMOUSEIO_GCALIBCOORDS:
    691 	case WSMOUSEIO_GETID:
    692 		return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, p);
    693 
    694 	default:
    695 		return (EPASSTHROUGH);
    696 	}
    697 }
    698 
    699 int
    700 j720lcdparam(ctx, type, id, msg)
    701 	void *ctx;
    702 	int type;
    703 	long id;
    704 	void *msg;
    705 {
    706 	struct j720ssp_softc *sc = ctx;
    707 	int i, s;
    708 	u_int32_t data[2], len;
    709 
    710 	switch (type) {
    711 	case CONFIG_HOOK_GET:
    712 		switch (id) {
    713 		case CONFIG_HOOK_BRIGHTNESS_MAX:
    714 		case CONFIG_HOOK_CONTRAST_MAX:
    715 			*(int *)msg = 255;
    716 			return 1;
    717 		case CONFIG_HOOK_BRIGHTNESS:
    718 			data[0] = 0x6b00;
    719 			data[1] = 0x8800;
    720 			len = 2;
    721 			break;
    722 		case CONFIG_HOOK_CONTRAST:
    723 			data[0] = 0x2b00;
    724 			data[1] = 0x8800;
    725 			len = 2;
    726 			break;
    727 		default:
    728 			return 0;
    729 		}
    730 		break;
    731 
    732 	case CONFIG_HOOK_SET:
    733 		switch (id) {
    734 		case CONFIG_HOOK_BRIGHTNESS:
    735 			if (*(int *)msg >= 0) {
    736 				data[0] = 0xcb00;
    737 				data[1] = *(int *)msg;
    738 				BIT_INVERT(data[1]);
    739 				data[1] <<= 8;
    740 				len = 2;
    741 			} else {
    742 				/* XXX hack */
    743 				data[0] = 0xfb00;
    744 				len = 1;
    745 			}
    746 			break;
    747 		case CONFIG_HOOK_CONTRAST:
    748 			data[0] = 0x8b00;
    749 			data[1] = *(int *)msg;
    750 			BIT_INVERT(data[1]);
    751 			data[1] <<= 8;
    752 			len = 2;
    753 			break;
    754 		default:
    755 			return 0;
    756 		}
    757 	}
    758 
    759 	s = splbio();
    760 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PCR, 0x2000000);
    761 
    762 	for (i = 0; i < len; i++) {
    763 		if (j720ssp_readwrite(sc, 1, data[i], &data[i], 500) < 0)
    764 			goto out;
    765 	}
    766 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
    767 	splx(s);
    768 
    769 	if (type == CONFIG_HOOK_SET)
    770 		return 1;
    771 
    772 	BIT_INVERT(data[1]);
    773 	*(int *)msg = data[1];
    774 
    775 	return 1;
    776 
    777 out:
    778 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
    779 
    780 	/* reset SSP */
    781 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x307);
    782 	delay(100);
    783 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x387);
    784 	splx(s);
    785 	return 0;
    786 }
    787 
    788 static int
    789 j720ssp_readwrite(sc, drainfifo, in, out, wait)
    790 	struct j720ssp_softc *sc;
    791 	int drainfifo;
    792 	int in;
    793 	int *out;
    794 	int wait;
    795 {
    796 	int timo;
    797 
    798 	timo = 100000;
    799 	while(bus_space_read_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PLR) & 0x400)
    800 		if (--timo == 0) {
    801 			printf("timo0\n");
    802 			return -1;
    803 		}
    804 	if (drainfifo) {
    805 		while(bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) &
    806 		      SR_RNE)
    807 			bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_DR);
    808 #if 1
    809 		delay(wait);
    810 #endif
    811 	}
    812 
    813 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_DR, in);
    814 
    815 	delay(wait);
    816 	timo = 100000;
    817 	while(! (bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) & SR_RNE))
    818 		if (--timo == 0) {
    819 			printf("timo1\n");
    820 			return -1;
    821 		}
    822 
    823 	*out = bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_DR);
    824 
    825 	return 0;
    826 }
    827 
    828 #if 0
    829 int
    830 j720kbd_cnattach(void)
    831 {
    832 	/* XXX defer initialization till j720sspattach */
    833 
    834 	return (0);
    835 }
    836 #endif
    837 
    838 /* ARGSUSED */
    839 void
    840 j720kbd_cngetc(v, type, data)
    841 	void *v;
    842 	u_int *type;
    843 	int *data;
    844 {
    845 	char buf[9];
    846 
    847 	if (j720kbdcons_initstate < 1)
    848 		return;
    849 
    850 	for (;;) {
    851 		j720kbd_read(&j720kbdcons_sc, buf);
    852 
    853 		if (buf[0] != 0) {
    854 			/* XXX we are discarding buffer contents */
    855 			*type = buf[0] & 0x80 ? WSCONS_EVENT_KEY_UP :
    856 			    WSCONS_EVENT_KEY_DOWN;
    857 			*data = buf[0] & 0x7f;
    858 			return;
    859 		}
    860 	}
    861 }
    862 
    863 void
    864 j720kbd_cnpollc(v, on)
    865 	void *v;
    866 	int on;
    867 {
    868 #if 0
    869 	/* XXX */
    870 	struct j720kbd_internal *t = v;
    871 
    872 	pckbc_set_poll(t->t_kbctag, t->t_kbcslot, on);
    873 #endif
    874 }
    875 
    876 void
    877 j720kbd_cnbell(v, pitch, period, volume)
    878 	void *v;
    879 	u_int pitch;
    880 	u_int period;
    881 	u_int volume;
    882 {
    883 }
    884 
    885 int
    886 j720lcdpower(ctx, type, id, msg)
    887 	void *ctx;
    888 	int type;
    889 	long id;
    890 	void *msg;
    891 {
    892 	struct sed1356_softc *sc = ctx;
    893 	struct sa11x0_softc *psc = sc->sc_parent;
    894 	int val;
    895 	u_int32_t reg;
    896 
    897 	if (type != CONFIG_HOOK_POWERCONTROL ||
    898 	    id != CONFIG_HOOK_POWERCONTROL_LCDLIGHT)
    899 		return 0;
    900 
    901 	sed1356_init_brightness(sc, 0);
    902 	sed1356_init_contrast(sc, 0);
    903 
    904 	if (msg) {
    905 		bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 0);
    906 
    907 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    908 		reg |= 0x1;
    909 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    910 		delay(50000);
    911 
    912 		val = sc->sc_contrast;
    913 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
    914 		delay(100000);
    915 
    916 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    917 		reg |= 0x4;
    918 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    919 
    920 		val = sc->sc_brightness;
    921 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
    922 
    923 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    924 		reg |= 0x2;
    925 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    926 	} else {
    927 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    928 		reg &= ~0x2;
    929 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    930 		reg &= ~0x4;
    931 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    932 		delay(100000);
    933 
    934 		val = -2;
    935 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
    936 
    937 		bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 1);
    938 
    939 		delay(100000);
    940 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    941 		reg &= ~0x1;
    942 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    943 	}
    944 	return 1;
    945 }
    946 
    947 int
    948 hpcarm_apm_getpower(api, parent)
    949 	struct apm_power_info *api;
    950 	void *parent;
    951 {
    952 	int data, pmdata[3], i;
    953 	struct j720ssp_softc *sc = (struct j720ssp_softc *)parent;
    954 
    955 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PCR, 0x2000000);
    956 
    957 	if (j720ssp_readwrite(sc, 1, 0x300, &data, 100) < 0 || data != 0x88)
    958 		goto out;
    959 
    960 	for (i = 0; i < 3; i++) {
    961 		if (j720ssp_readwrite(sc, 0, 0x8800, &pmdata[i], 500) < 0)
    962 			goto out;
    963 		BIT_INVERT(pmdata[i]);
    964 	}
    965 
    966 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
    967 
    968 	bzero(api, sizeof(struct apm_power_info));
    969 	api->ac_state = APM_AC_UNKNOWN;
    970 
    971 	/*
    972 	 * pmdata[0] is the main battery level
    973 	 * pmdata[1] is the backup battery level
    974 	 * pmdata[2] tells which battery is present
    975 	 */
    976 	switch(pmdata[2]) {
    977 	case 14: /* backup battery present */
    978 	case 2:  /* backup battery absent */
    979 		api->battery_state = APM_BATT_CHARGING;
    980 		api->minutes_left = (pmdata[0] * 840) / 170;
    981 		api->battery_life = (pmdata[0] * 100) / 170;
    982 		api->nbattery = 1;
    983 		break;
    984 	case 15: /* backup battery present */
    985 	case 3:  /* backup battery absent */
    986 		api->battery_state = APM_BATT_ABSENT;
    987 		api->battery_life = 0;
    988 		api->nbattery = 0;
    989 		break;
    990 	default:
    991 		api->battery_state = APM_BATT_UNKNOWN;
    992 		break;
    993 	}
    994 
    995 	return 0;
    996 
    997 out:
    998 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
    999 
   1000 	/* reset SSP */
   1001 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x307);
   1002 	delay(100);
   1003 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x387);
   1004 
   1005 	printf("hpcarm_apm_getpower: error %x\n", data);
   1006 	return EIO;
   1007 }
   1008