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