Home | History | Annotate | Line # | Download | only in dev
j720ssp.c revision 1.14
      1 /* $NetBSD: j720ssp.c,v 1.14 2002/10/02 05:18:52 thorpej 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. All advertising materials mentioning features or use of this software
     55  *    must display the following acknowledgement:
     56  *	This product includes software developed by the University of
     57  *	California, Berkeley and its contributors.
     58  * 4. Neither the name of the University nor the names of its contributors
     59  *    may be used to endorse or promote products derived from this software
     60  *    without specific prior written permission.
     61  *
     62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  * SUCH DAMAGE.
     73  *
     74  *	@(#)pccons.c	5.11 (Berkeley) 5/21/91
     75  */
     76 
     77 #include "apm.h"
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/device.h>
     82 #include <sys/kernel.h>
     83 #include <sys/malloc.h>
     84 #include <sys/ioctl.h>
     85 #include <sys/kthread.h>
     86 #include <sys/lock.h>
     87 
     88 #include <machine/bus.h>
     89 #include <machine/config_hook.h>
     90 #include <machine/bootinfo.h>
     91 #include <machine/apmvar.h>
     92 
     93 #include <hpcarm/dev/sed1356var.h>
     94 
     95 #include <arm/sa11x0/sa11x0_var.h>
     96 #include <arm/sa11x0/sa11x0_gpioreg.h>
     97 #include <arm/sa11x0/sa11x0_ppcreg.h>
     98 #include <arm/sa11x0/sa11x0_sspreg.h>
     99 
    100 #include <dev/wscons/wsconsio.h>
    101 #include <dev/wscons/wskbdvar.h>
    102 #include <dev/wscons/wsksymdef.h>
    103 #include <dev/wscons/wsksymvar.h>
    104 #include <dev/wscons/wsmousevar.h>
    105 #include <dev/hpc/tpcalibvar.h>
    106 
    107 extern const struct wscons_keydesc j720kbd_keydesctab[];
    108 
    109 struct j720ssp_softc {
    110         struct device sc_dev;
    111 
    112 	bus_space_tag_t sc_iot;
    113 	bus_space_handle_t sc_gpioh;
    114 	bus_space_handle_t sc_ssph;
    115 
    116 	struct device *sc_wskbddev;
    117 	struct device *sc_wsmousedev;
    118 	struct tpcalib_softc sc_tpcalib;
    119 
    120 	void *sc_kbdsi;
    121 	void *sc_tpsi;
    122 	int sc_enabled;
    123 
    124 	struct proc *sc_ssp_kthread;
    125 	int sc_ssp_status;
    126 	struct simplelock sc_ssp_status_lock;
    127 };
    128 /* Values for struct softc's sc_ssp_status */
    129 #define J720_SSP_STATUS_NONE	0
    130 #define J720_SSP_STATUS_TP	1
    131 #define J720_SSP_STATUS_KBD	2
    132 
    133 void j720ssp_create_kthread __P((void *));
    134 void j720ssp_kthread __P((void *));
    135 int  j720kbd_intr __P((void *));
    136 int  j720tp_intr __P((void *));
    137 void j720kbd_poll __P((void *));
    138 int  j720tp_poll __P((void *));
    139 
    140 int  j720lcdparam __P((void *, int, long, void *));
    141 static void j720kbd_read __P((struct j720ssp_softc *, char *));
    142 static int  j720ssp_readwrite __P((struct j720ssp_softc *, int,
    143 	int, int *, int));
    144 
    145 int  j720sspprobe __P((struct device *, struct cfdata *, void *));
    146 void j720sspattach __P((struct device *, struct device *, void *));
    147 
    148 int  j720kbd_submatch __P((struct device *, struct cfdata *, void *));
    149 int  j720tp_submatch __P((struct device *, struct cfdata *, void *));
    150 int  apm_submatch __P((struct device *, struct cfdata *, void *));
    151 
    152 int  j720kbd_enable __P((void *, int));
    153 void j720kbd_set_leds __P((void *, int));
    154 int  j720kbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    155 
    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();
    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 		return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, p);
    692 
    693 	default:
    694 		return (EPASSTHROUGH);
    695 	}
    696 }
    697 
    698 int
    699 j720lcdparam(ctx, type, id, msg)
    700 	void *ctx;
    701 	int type;
    702 	long id;
    703 	void *msg;
    704 {
    705 	struct j720ssp_softc *sc = ctx;
    706 	int i, s;
    707 	u_int32_t data[2], len;
    708 
    709 	switch (type) {
    710 	case CONFIG_HOOK_GET:
    711 		switch (id) {
    712 		case CONFIG_HOOK_BRIGHTNESS_MAX:
    713 		case CONFIG_HOOK_CONTRAST_MAX:
    714 			*(int *)msg = 255;
    715 			return 1;
    716 		case CONFIG_HOOK_BRIGHTNESS:
    717 			data[0] = 0x6b00;
    718 			data[1] = 0x8800;
    719 			len = 2;
    720 			break;
    721 		case CONFIG_HOOK_CONTRAST:
    722 			data[0] = 0x2b00;
    723 			data[1] = 0x8800;
    724 			len = 2;
    725 			break;
    726 		default:
    727 			return 0;
    728 		}
    729 		break;
    730 
    731 	case CONFIG_HOOK_SET:
    732 		switch (id) {
    733 		case CONFIG_HOOK_BRIGHTNESS:
    734 			if (*(int *)msg >= 0) {
    735 				data[0] = 0xcb00;
    736 				data[1] = *(int *)msg;
    737 				BIT_INVERT(data[1]);
    738 				data[1] <<= 8;
    739 				len = 2;
    740 			} else {
    741 				/* XXX hack */
    742 				data[0] = 0xfb00;
    743 				len = 1;
    744 			}
    745 			break;
    746 		case CONFIG_HOOK_CONTRAST:
    747 			data[0] = 0x8b00;
    748 			data[1] = *(int *)msg;
    749 			BIT_INVERT(data[1]);
    750 			data[1] <<= 8;
    751 			len = 2;
    752 			break;
    753 		default:
    754 			return 0;
    755 		}
    756 	}
    757 
    758 	s = splbio();
    759 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PCR, 0x2000000);
    760 
    761 	for (i = 0; i < len; i++) {
    762 		if (j720ssp_readwrite(sc, 1, data[i], &data[i], 500) < 0)
    763 			goto out;
    764 	}
    765 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
    766 	splx(s);
    767 
    768 	if (type == CONFIG_HOOK_SET)
    769 		return 1;
    770 
    771 	BIT_INVERT(data[1]);
    772 	*(int *)msg = data[1];
    773 
    774 	return 1;
    775 
    776 out:
    777 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
    778 
    779 	/* reset SSP */
    780 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x307);
    781 	delay(100);
    782 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x387);
    783 	splx(s);
    784 	return 0;
    785 }
    786 
    787 static int
    788 j720ssp_readwrite(sc, drainfifo, in, out, wait)
    789 	struct j720ssp_softc *sc;
    790 	int drainfifo;
    791 	int in;
    792 	int *out;
    793 	int wait;
    794 {
    795 	int timo;
    796 
    797 	timo = 100000;
    798 	while(bus_space_read_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PLR) & 0x400)
    799 		if (--timo == 0) {
    800 			printf("timo0\n");
    801 			return -1;
    802 		}
    803 	if (drainfifo) {
    804 		while(bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) &
    805 		      SR_RNE)
    806 			bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_DR);
    807 #if 1
    808 		delay(wait);
    809 #endif
    810 	}
    811 
    812 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_DR, in);
    813 
    814 	delay(wait);
    815 	timo = 100000;
    816 	while(! (bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) & SR_RNE))
    817 		if (--timo == 0) {
    818 			printf("timo1\n");
    819 			return -1;
    820 		}
    821 
    822 	*out = bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_DR);
    823 
    824 	return 0;
    825 }
    826 
    827 #if 0
    828 int
    829 j720kbd_cnattach(void)
    830 {
    831 	/* XXX defer initialization till j720sspattach */
    832 
    833 	return (0);
    834 }
    835 #endif
    836 
    837 /* ARGSUSED */
    838 void
    839 j720kbd_cngetc(v, type, data)
    840 	void *v;
    841 	u_int *type;
    842 	int *data;
    843 {
    844 	char buf[9];
    845 
    846 	if (j720kbdcons_initstate < 1)
    847 		return;
    848 
    849 	for (;;) {
    850 		j720kbd_read(&j720kbdcons_sc, buf);
    851 
    852 		if (buf[0] != 0) {
    853 			/* XXX we are discarding buffer contents */
    854 			*type = buf[0] & 0x80 ? WSCONS_EVENT_KEY_UP :
    855 			    WSCONS_EVENT_KEY_DOWN;
    856 			*data = buf[0] & 0x7f;
    857 			return;
    858 		}
    859 	}
    860 }
    861 
    862 void
    863 j720kbd_cnpollc(v, on)
    864 	void *v;
    865 	int on;
    866 {
    867 #if 0
    868 	/* XXX */
    869 	struct j720kbd_internal *t = v;
    870 
    871 	pckbc_set_poll(t->t_kbctag, t->t_kbcslot, on);
    872 #endif
    873 }
    874 
    875 void
    876 j720kbd_cnbell(v, pitch, period, volume)
    877 	void *v;
    878 	u_int pitch;
    879 	u_int period;
    880 	u_int volume;
    881 {
    882 }
    883 
    884 int
    885 j720lcdpower(ctx, type, id, msg)
    886 	void *ctx;
    887 	int type;
    888 	long id;
    889 	void *msg;
    890 {
    891 	struct sed1356_softc *sc = ctx;
    892 	struct sa11x0_softc *psc = sc->sc_parent;
    893 	int val;
    894 	u_int32_t reg;
    895 
    896 	if (type != CONFIG_HOOK_POWERCONTROL ||
    897 	    id != CONFIG_HOOK_POWERCONTROL_LCDLIGHT)
    898 		return 0;
    899 
    900 	sed1356_init_brightness(sc, 0);
    901 	sed1356_init_contrast(sc, 0);
    902 
    903 	if (msg) {
    904 		bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 0);
    905 
    906 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    907 		reg |= 0x1;
    908 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    909 		delay(50000);
    910 
    911 		val = sc->sc_contrast;
    912 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
    913 		delay(100000);
    914 
    915 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    916 		reg |= 0x4;
    917 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    918 
    919 		val = sc->sc_brightness;
    920 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
    921 
    922 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    923 		reg |= 0x2;
    924 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    925 	} else {
    926 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    927 		reg &= ~0x2;
    928 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    929 		reg &= ~0x4;
    930 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    931 		delay(100000);
    932 
    933 		val = -2;
    934 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
    935 
    936 		bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 1);
    937 
    938 		delay(100000);
    939 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    940 		reg &= ~0x1;
    941 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    942 	}
    943 	return 1;
    944 }
    945 
    946 int
    947 hpcarm_apm_getpower(api, parent)
    948 	struct apm_power_info *api;
    949 	void *parent;
    950 {
    951 	int data, pmdata[3], i;
    952 	struct j720ssp_softc *sc = (struct j720ssp_softc *)parent;
    953 
    954 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PCR, 0x2000000);
    955 
    956 	if (j720ssp_readwrite(sc, 1, 0x300, &data, 100) < 0 || data != 0x88)
    957 		goto out;
    958 
    959 	for (i = 0; i < 3; i++) {
    960 		if (j720ssp_readwrite(sc, 0, 0x8800, &pmdata[i], 500) < 0)
    961 			goto out;
    962 		BIT_INVERT(pmdata[i]);
    963 #if 0
    964 		printf("pmdata%d = %d ", i, pmdata[i]);
    965 #endif
    966 	}
    967 	printf("\n");
    968 
    969 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
    970 
    971 	bzero(api, sizeof(struct apm_power_info));
    972 	api->ac_state = APM_AC_UNKNOWN;
    973 
    974 	/*
    975 	 * pmdata[0] is the main battery level
    976 	 * pmdata[1] is the backup battery level
    977 	 * pmdata[2] tells which battery is present
    978 	 */
    979 	switch(pmdata[2]) {
    980 	case 14: /* backup battery present */
    981 	case 2:  /* backup battery absent */
    982 		api->battery_state = APM_BATT_CHARGING;
    983 		api->minutes_left = (pmdata[0] * 840) / 170;
    984 		api->battery_life = (pmdata[0] * 100) / 170;
    985 		api->nbattery = 1;
    986 		break;
    987 	case 15: /* backup battery present */
    988 	case 3:  /* backup battery absent */
    989 		api->battery_state = APM_BATT_ABSENT;
    990 		api->battery_life = 0;
    991 		api->nbattery = 0;
    992 		break;
    993 	default:
    994 		api->battery_state = APM_BATT_UNKNOWN;
    995 		break;
    996 	}
    997 
    998 	return 0;
    999 
   1000 out:
   1001 	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
   1002 
   1003 	/* reset SSP */
   1004 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x307);
   1005 	delay(100);
   1006 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x387);
   1007 
   1008 	printf("hpcarm_apm_getpower: error %x\n", data);
   1009 	return EIO;
   1010 }
   1011