/* $NetBSD: psh3tp.c,v 1.18 2023/12/20 14:50:02 thorpej Exp $ */
/*
* Copyright (c) 2005 KIYOHARA Takashi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
if (!platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
return 0;
if (strcmp(cf->cf_name, "psh3tp") != 0)
return 0;
return 1;
}
/*
* Attach the touch panel driver and its wsmouse child.
*
* Note that we have to use submatch to distinguish between child because
* wsmouse_match matches unconditionally.
*/
/* ARGSUSED */
static void
psh3tp_attach(device_t parent __unused, device_t self, void *aux __unused)
{
struct psh3tp_softc *sc = device_private(self);
struct wsmousedev_attach_args wsma;
aprint_naive("\n");
aprint_normal("\n");
sc->sc_dev = self;
sc->sc_enabled = 0;
/* touch-panel as a pointing device */
wsma.accessops = &psh3tp_accessops;
wsma.accesscookie = sc;
/* used when in polling mode */
callout_init(&sc->sc_touch_ch, 0);
/* establish interrupt handler, but disable until opened */
intc_intr_establish(SH7709_INTEVT2_IRQ2,
IST_EDGE, IPL_TTY, psh3tp_intr, sc);
intc_intr_disable(SH7709_INTEVT2_IRQ2);
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "unable to establish power handler\n");
}
/*
* Enable touch panel: we start in interrupt mode.
* Must be called at spltty().
*/
/* ARGSUSED */
static void
psh3tp_enable(struct psh3tp_softc *sc __unused)
{
/*
* Number of times the "touched" bit should be read
* consecutively.
*/
#define TREMOR_THRESHOLD 0x300
steady = 0;
tremor_timeout = TREMOR_THRESHOLD * 16; /* XXX: arbitrary */
touched = true; /* we start with "touched" state */
do {
uint8_t state;
phdr = _reg_read_1(SH7709_PHDR);
state = ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP);
if (--tremor_timeout == 0) {
DPRINTF(("%s: tremor timeout!\n",
device_xname(sc->sc_dev)));
goto served;
}
} while (steady < TREMOR_THRESHOLD);
if (touched) {
intc_intr_disable(SH7709_INTEVT2_IRQ2);
/*
* ADC readings are not stable yet, so schedule
* callout instead of accessing ADC from the interrupt
* handler only to immediately delay().
*/
callout_reset(&sc->sc_touch_ch,
hz/32, psh3tp_start_polling, sc);
} else
DPRINTFN(1, ("%s: tremor\n", device_xname(sc->sc_dev)));
served:
/* clear the interrupt */
_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2);
return 1;
}
/*
* Called from the interrupt handler at spltty() upon first touch.
* Decide if we are going to report this touch as a mouse click/drag.
*/
static void
psh3tp_start_polling(void *arg)
{
struct psh3tp_softc *sc = (struct psh3tp_softc *)arg;
uint8_t phdr;
int rawx, rawy;
phdr = _reg_read_1(SH7709_PHDR);
if ((phdr & PHDR_TP_PEN_UP) == PHDR_TP_PEN_UP) {
DPRINTFN(2, ("%s: start: pen is not down\n",
device_xname(sc->sc_dev)));
psh3tp_stop_polling(sc);
return;
}
/* clear pending interrupt signal before re-enabling the interrupt */
irr0 = _reg_read_1(SH7709_IRR0);
if ((irr0 & IRR0_IRQ2) != 0)
_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2);
intc_intr_enable(SH7709_INTEVT2_IRQ2);
}
/*
* We are reporting this touch as a mouse click/drag.
*/
static void
psh3tp_callout_wsmouse(void *arg)
{
struct psh3tp_softc *sc = (struct psh3tp_softc *)arg;
uint8_t phdr;
int rawx, rawy;
int s;