Home | History | Annotate | Line # | Download | only in dev
j6x0tp.c revision 1.2
      1  1.2  uwe /*	$NetBSD: j6x0tp.c,v 1.2 2003/10/22 23:52:46 uwe Exp $ */
      2  1.1  uwe 
      3  1.1  uwe /*
      4  1.1  uwe  * Copyright (c) 2003 Valeriy E. Ushakov
      5  1.1  uwe  * All rights reserved.
      6  1.1  uwe  *
      7  1.1  uwe  * Redistribution and use in source and binary forms, with or without
      8  1.1  uwe  * modification, are permitted provided that the following conditions
      9  1.1  uwe  * are met:
     10  1.1  uwe  * 1. Redistributions of source code must retain the above copyright
     11  1.1  uwe  *    notice, this list of conditions and the following disclaimer.
     12  1.1  uwe  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  uwe  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  uwe  *    documentation and/or other materials provided with the distribution.
     15  1.1  uwe  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  uwe  *    derived from this software without specific prior written permission
     17  1.1  uwe  *
     18  1.1  uwe  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  uwe  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  uwe  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  uwe  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  uwe  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1  uwe  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1  uwe  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1  uwe  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1  uwe  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1  uwe  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  uwe  */
     29  1.1  uwe 
     30  1.1  uwe #include <sys/cdefs.h>
     31  1.2  uwe __KERNEL_RCSID(0, "$NetBSD: j6x0tp.c,v 1.2 2003/10/22 23:52:46 uwe Exp $");
     32  1.1  uwe 
     33  1.1  uwe #include <sys/param.h>
     34  1.1  uwe #include <sys/kernel.h>
     35  1.1  uwe #include <sys/device.h>
     36  1.1  uwe #include <sys/malloc.h>
     37  1.1  uwe #include <sys/systm.h>
     38  1.1  uwe #include <sys/callout.h>
     39  1.1  uwe #ifdef GPROF
     40  1.1  uwe #include <sys/gmon.h>
     41  1.1  uwe #endif
     42  1.1  uwe 
     43  1.1  uwe #include <dev/wscons/wsconsio.h>
     44  1.1  uwe #include <dev/wscons/wsmousevar.h>
     45  1.1  uwe #include <dev/hpc/tpcalibvar.h>
     46  1.1  uwe 
     47  1.1  uwe #include <machine/platid.h>
     48  1.1  uwe #include <machine/platid_mask.h>
     49  1.1  uwe 
     50  1.1  uwe #include <machine/intr.h>
     51  1.1  uwe 
     52  1.1  uwe #include <sh3/exception.h>
     53  1.1  uwe #include <sh3/intcreg.h>
     54  1.1  uwe #include <sh3/pfcreg.h>
     55  1.1  uwe #include <sh3/adcreg.h>
     56  1.1  uwe 
     57  1.2  uwe #include <sh3/dev/adcvar.h>
     58  1.2  uwe 
     59  1.2  uwe 
     60  1.1  uwe #define J6X0TP_DEBUG
     61  1.1  uwe #if 0 /* XXX: disabled in favor of local version that uses printf_nolog */
     62  1.1  uwe #define DPRINTF_ENABLE
     63  1.1  uwe #define DPRINTF_DEBUG	j6x0tp_debug
     64  1.1  uwe #define DPRINTF_LEVEL	0
     65  1.1  uwe #include <machine/debug.h>
     66  1.1  uwe #else
     67  1.1  uwe #ifdef J6X0TP_DEBUG
     68  1.1  uwe volatile int j6x0tp_debug = 0;
     69  1.1  uwe #define	DPRINTF(arg)		if (j6x0tp_debug) printf_nolog arg
     70  1.1  uwe #define DPRINTFN(n, arg)	if (j6x0tp_debug > (n)) printf_nolog arg
     71  1.1  uwe #else
     72  1.1  uwe #define	DPRINTF(arg)		((void)0)
     73  1.1  uwe #define DPRINTFN(n, arg)	((void)0)
     74  1.1  uwe #endif
     75  1.1  uwe #endif
     76  1.1  uwe 
     77  1.1  uwe 
     78  1.1  uwe /* PFC bits pertinent to Jornada 6x0 touchpanel */
     79  1.1  uwe #define PHDR_TP_PEN_DOWN	0x08
     80  1.1  uwe 
     81  1.1  uwe #define SCPDR_TP_SCAN_ENABLE	0x20
     82  1.1  uwe #define SCPDR_TP_SCAN_Y		0x02
     83  1.1  uwe #define SCPDR_TP_SCAN_X		0x01
     84  1.1  uwe 
     85  1.1  uwe /* A/D covnerter channels to get x/y from */
     86  1.1  uwe #define ADC_CHANNEL_TP_Y	1
     87  1.1  uwe #define ADC_CHANNEL_TP_X	2
     88  1.1  uwe 
     89  1.1  uwe 
     90  1.1  uwe struct j6x0tp_softc {
     91  1.1  uwe 	struct device sc_dev;
     92  1.1  uwe 
     93  1.1  uwe 	struct callout sc_touch_ch;
     94  1.1  uwe 	int sc_intrlevel;
     95  1.1  uwe 	int sc_enabled;
     96  1.1  uwe 
     97  1.1  uwe 	struct device *sc_wsmousedev;
     98  1.1  uwe 	struct tpcalib_softc sc_tpcalib;
     99  1.1  uwe };
    100  1.1  uwe 
    101  1.1  uwe static int	j6x0tp_match(struct device *, struct cfdata *, void *);
    102  1.1  uwe static void	j6x0tp_attach(struct device *, struct device *, void *);
    103  1.1  uwe 
    104  1.1  uwe CFATTACH_DECL(j6x0tp, sizeof(struct j6x0tp_softc),
    105  1.1  uwe     j6x0tp_match, j6x0tp_attach, NULL, NULL);
    106  1.1  uwe 
    107  1.1  uwe 
    108  1.1  uwe static int  j6x0tp_enable(void *);
    109  1.1  uwe static int  j6x0tp_ioctl(void *, u_long, caddr_t, int, struct proc *);
    110  1.1  uwe static void j6x0tp_disable(void *);
    111  1.1  uwe 
    112  1.1  uwe const struct wsmouse_accessops j6x0tp_accessops = {
    113  1.1  uwe 	j6x0tp_enable,
    114  1.1  uwe 	j6x0tp_ioctl,
    115  1.1  uwe 	j6x0tp_disable,
    116  1.1  uwe };
    117  1.1  uwe 
    118  1.1  uwe static const struct wsmouse_calibcoords j6x0tp_default_calib = {
    119  1.1  uwe 	0, 0, 639, 239,
    120  1.1  uwe 	4,
    121  1.1  uwe 	{{  38,  80,   0,   0 }, /* upper left  */
    122  1.1  uwe 	 { 950,  80, 639,   0 }, /* upper right */
    123  1.1  uwe 	 {  38, 900,   0, 239 }, /* lower left  */
    124  1.1  uwe 	 { 950, 900, 639, 239 }} /* lower right */
    125  1.1  uwe };
    126  1.1  uwe 
    127  1.1  uwe 
    128  1.1  uwe static int	j6x0tp_intr(void *);
    129  1.1  uwe static void	j6x0tp_poll_callout(void *self);
    130  1.1  uwe 
    131  1.1  uwe 
    132  1.1  uwe static int
    133  1.1  uwe j6x0tp_match(struct device *parent, struct cfdata *cfp, void *aux)
    134  1.1  uwe {
    135  1.1  uwe 
    136  1.1  uwe 	/*
    137  1.1  uwe 	 * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too?
    138  1.1  uwe 	 * Is 620 wired similarly?
    139  1.1  uwe 	 */
    140  1.1  uwe 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX))
    141  1.1  uwe 		return (0);
    142  1.1  uwe 
    143  1.1  uwe 	if (strcmp(cfp->cf_name, "j6x0tp") != 0)
    144  1.1  uwe 		return (0);
    145  1.1  uwe 
    146  1.1  uwe 	return (1);
    147  1.1  uwe }
    148  1.1  uwe 
    149  1.1  uwe 
    150  1.1  uwe static void
    151  1.1  uwe j6x0tp_attach(struct device *parent, struct device *self, void *aux)
    152  1.1  uwe {
    153  1.1  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    154  1.1  uwe 	struct wsmousedev_attach_args wsma;
    155  1.1  uwe 
    156  1.1  uwe 	printf("\n");
    157  1.1  uwe 
    158  1.1  uwe 	sc->sc_enabled = 0;
    159  1.1  uwe 
    160  1.1  uwe 	wsma.accessops = &j6x0tp_accessops;
    161  1.1  uwe 	wsma.accesscookie = sc;
    162  1.1  uwe 
    163  1.1  uwe 	sc->sc_wsmousedev = config_found(self, &wsma, wsmousedevprint);
    164  1.1  uwe 	if (sc->sc_wsmousedev == NULL)
    165  1.1  uwe 		return;
    166  1.1  uwe 
    167  1.1  uwe 	tpcalib_init(&sc->sc_tpcalib);
    168  1.1  uwe 	tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
    169  1.1  uwe 		      (caddr_t)&j6x0tp_default_calib, 0, 0);
    170  1.1  uwe 
    171  1.1  uwe 	callout_init(&sc->sc_touch_ch);
    172  1.1  uwe 
    173  1.1  uwe 	intc_intr_establish(SH7709_INTEVT2_IRQ3, IST_EDGE, IPL_TTY,
    174  1.1  uwe 			    j6x0tp_intr, sc);
    175  1.1  uwe 	sc->sc_intrlevel = intc_intr_disable(SH7709_INTEVT2_IRQ3);
    176  1.1  uwe }
    177  1.1  uwe 
    178  1.1  uwe 
    179  1.1  uwe static int
    180  1.1  uwe j6x0tp_enable(void *self)
    181  1.1  uwe {
    182  1.1  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    183  1.1  uwe 	int s;
    184  1.1  uwe 
    185  1.1  uwe 	DPRINTFN(1, ("%s: enable\n", sc->sc_dev.dv_xname));
    186  1.1  uwe 
    187  1.1  uwe 	s = spltty();
    188  1.1  uwe 
    189  1.1  uwe 	sc->sc_enabled = 1;
    190  1.1  uwe 	intc_intr_enable(SH7709_INTEVT2_IRQ3, sc->sc_intrlevel);
    191  1.1  uwe 
    192  1.1  uwe 	splx(s);
    193  1.1  uwe 	return (0);
    194  1.1  uwe }
    195  1.1  uwe 
    196  1.1  uwe 
    197  1.1  uwe static void
    198  1.1  uwe j6x0tp_disable(void *self)
    199  1.1  uwe {
    200  1.1  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    201  1.1  uwe 	int s;
    202  1.1  uwe 
    203  1.1  uwe 	s = spltty();
    204  1.1  uwe 
    205  1.1  uwe 	DPRINTFN(1, ("%s: disable\n", sc->sc_dev.dv_xname));
    206  1.1  uwe 
    207  1.1  uwe 	sc->sc_enabled = 0;
    208  1.1  uwe 	intc_intr_disable(SH7709_INTEVT2_IRQ3);
    209  1.1  uwe 	callout_stop(&sc->sc_touch_ch);
    210  1.1  uwe 
    211  1.1  uwe 	splx(s);
    212  1.1  uwe }
    213  1.1  uwe 
    214  1.1  uwe 
    215  1.1  uwe static int
    216  1.1  uwe j6x0tp_intr(void *self)
    217  1.1  uwe {
    218  1.1  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    219  1.1  uwe 
    220  1.1  uwe 	uint8_t irr0;
    221  1.1  uwe 	uint8_t phdr, touched;
    222  1.1  uwe 	unsigned int steady, tremor_timeout;
    223  1.1  uwe 
    224  1.1  uwe 	irr0 = _reg_read_1(SH7709_IRR0);
    225  1.1  uwe 	if ((irr0 & IRR0_IRQ3) == 0) {
    226  1.1  uwe #ifdef DIAGNOSTIC
    227  1.1  uwe 		printf("%s: irr0 %02x?\n", sc->sc_dev.dv_xname, irr0);
    228  1.1  uwe #endif
    229  1.1  uwe 		return (0);
    230  1.1  uwe 	}
    231  1.1  uwe 
    232  1.1  uwe 	/*
    233  1.1  uwe 	 * Number of times the "touched" bit should be read
    234  1.1  uwe 	 * consecutively.
    235  1.1  uwe 	 */
    236  1.1  uwe #	define TREMOR_THRESHOLD 0x300
    237  1.1  uwe 
    238  1.1  uwe 	steady = 0;
    239  1.1  uwe 	tremor_timeout = TREMOR_THRESHOLD * 16;	/* XXX: arbitrary */
    240  1.1  uwe 	touched = PHDR_TP_PEN_DOWN;	/* we start with "touched" state */
    241  1.1  uwe 
    242  1.1  uwe 	do {
    243  1.1  uwe 		phdr = _reg_read_1(SH7709_PHDR);
    244  1.1  uwe 
    245  1.1  uwe 		if ((phdr & PHDR_TP_PEN_DOWN) == touched)
    246  1.1  uwe 			++steady;
    247  1.1  uwe 		else {
    248  1.1  uwe 			steady = 0;
    249  1.1  uwe 			touched = phdr & PHDR_TP_PEN_DOWN;
    250  1.1  uwe 		}
    251  1.1  uwe 
    252  1.1  uwe 		if (--tremor_timeout == 0) {
    253  1.1  uwe 			DPRINTF(("%s: tremor timeout!\n",
    254  1.1  uwe 				 sc->sc_dev.dv_xname));
    255  1.1  uwe 			goto served;
    256  1.1  uwe 		}
    257  1.1  uwe 	} while (steady < TREMOR_THRESHOLD);
    258  1.1  uwe 
    259  1.1  uwe 	if (touched == 0) {
    260  1.1  uwe 		DPRINTFN(1, ("%s: tremor\n", sc->sc_dev.dv_xname));
    261  1.1  uwe 		goto served;
    262  1.1  uwe 	}
    263  1.1  uwe 
    264  1.1  uwe 	intc_intr_disable(SH7709_INTEVT2_IRQ3);
    265  1.1  uwe 
    266  1.1  uwe 	if (sc->sc_enabled)
    267  1.1  uwe 		callout_reset(&sc->sc_touch_ch, hz/32,
    268  1.1  uwe 			      j6x0tp_poll_callout, sc);
    269  1.1  uwe 	else
    270  1.1  uwe 		DPRINTFN(1, ("%s: intr: !sc_enabled\n", sc->sc_dev.dv_xname));
    271  1.1  uwe 
    272  1.1  uwe   served:
    273  1.1  uwe 	/* clear the interrupt */
    274  1.1  uwe 	_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ3);
    275  1.1  uwe 
    276  1.1  uwe 	return (1);
    277  1.1  uwe }
    278  1.1  uwe 
    279  1.1  uwe 
    280  1.1  uwe static void
    281  1.1  uwe j6x0tp_poll_callout(void *self)
    282  1.1  uwe {
    283  1.1  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    284  1.1  uwe 	uint8_t phdr, scpdr;
    285  1.1  uwe 	uint8_t irr0;
    286  1.1  uwe 	int rawx, rawy, x, y;
    287  1.1  uwe 	int s;
    288  1.1  uwe 
    289  1.1  uwe 	s = spltty();
    290  1.1  uwe 
    291  1.1  uwe 	if (!sc->sc_enabled) {
    292  1.1  uwe 		DPRINTFN(1, ("%s: callout: !sc_enabled\n",
    293  1.1  uwe 			     sc->sc_dev.dv_xname));
    294  1.1  uwe 		splx(s);
    295  1.1  uwe 		return;
    296  1.1  uwe 	}
    297  1.1  uwe 
    298  1.1  uwe 	phdr = _reg_read_1(SH7709_PHDR);
    299  1.1  uwe 	if ((phdr & PHDR_TP_PEN_DOWN) == 0) {
    300  1.1  uwe 		wsmouse_input(sc->sc_wsmousedev, 0, 0, 0, 0, 0); /* mouse up */
    301  1.1  uwe 
    302  1.1  uwe 		intc_intr_enable(SH7709_INTEVT2_IRQ3, sc->sc_intrlevel);
    303  1.1  uwe 
    304  1.1  uwe 		irr0 = _reg_read_1(SH7709_IRR0);
    305  1.1  uwe 		if ((irr0 & IRR0_IRQ3) != 0)
    306  1.1  uwe 			_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ3);
    307  1.1  uwe 
    308  1.1  uwe 		splx(s);
    309  1.1  uwe 		return;
    310  1.1  uwe 	}
    311  1.1  uwe 
    312  1.1  uwe 	/* XXX: protect accesses to SCPDR? */
    313  1.1  uwe 
    314  1.1  uwe 	/* Y axis */
    315  1.1  uwe 	scpdr = _reg_read_1(SH7709_SCPDR);
    316  1.1  uwe 	scpdr |=  SCPDR_TP_SCAN_ENABLE;
    317  1.1  uwe 	scpdr &= ~SCPDR_TP_SCAN_Y; /* pull low to scan */
    318  1.1  uwe 	_reg_write_1(SH7709_SCPDR, scpdr);
    319  1.1  uwe 	delay(10);
    320  1.1  uwe 	rawy = adc_sample_channel(ADC_CHANNEL_TP_Y);
    321  1.1  uwe 
    322  1.1  uwe 	/* X axis */
    323  1.1  uwe 	scpdr = _reg_read_1(SH7709_SCPDR);
    324  1.1  uwe 	scpdr |=  SCPDR_TP_SCAN_Y;
    325  1.1  uwe 	scpdr &= ~SCPDR_TP_SCAN_X; /* pull low to scan */
    326  1.1  uwe 	_reg_write_1(SH7709_SCPDR, scpdr);
    327  1.1  uwe 	delay(10);
    328  1.1  uwe 	rawx = adc_sample_channel(ADC_CHANNEL_TP_X);
    329  1.1  uwe 
    330  1.1  uwe 	/* restore SCPDR */
    331  1.1  uwe 	scpdr = _reg_read_1(SH7709_SCPDR);
    332  1.1  uwe 	scpdr |=  SCPDR_TP_SCAN_X;
    333  1.1  uwe 	scpdr &= ~SCPDR_TP_SCAN_ENABLE;
    334  1.1  uwe 	_reg_write_1(SH7709_SCPDR, scpdr);
    335  1.1  uwe 
    336  1.1  uwe 	tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y);
    337  1.1  uwe 
    338  1.1  uwe 	DPRINTFN(2, ("%s: %4d %4d -> %3d %3d\n",
    339  1.1  uwe 		     sc->sc_dev.dv_xname, rawx, rawy, x, y));
    340  1.1  uwe 
    341  1.1  uwe 	wsmouse_input(sc->sc_wsmousedev, 1, x, y, 0,
    342  1.1  uwe 		      WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
    343  1.1  uwe 
    344  1.1  uwe 	callout_schedule(&sc->sc_touch_ch, hz/32);
    345  1.1  uwe 	splx(s);
    346  1.1  uwe }
    347  1.1  uwe 
    348  1.1  uwe 
    349  1.1  uwe static int
    350  1.1  uwe j6x0tp_ioctl(void *self, u_long cmd, caddr_t data, int flag,
    351  1.1  uwe 		     struct proc *p)
    352  1.1  uwe {
    353  1.1  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    354  1.1  uwe 
    355  1.1  uwe 	switch (cmd) {
    356  1.1  uwe 	case WSMOUSEIO_GTYPE:
    357  1.1  uwe 		*(u_int *)data = WSMOUSE_TYPE_TPANEL;
    358  1.1  uwe 		return (0);
    359  1.1  uwe 
    360  1.1  uwe 	case WSMOUSEIO_SCALIBCOORDS:
    361  1.1  uwe 	case WSMOUSEIO_GCALIBCOORDS:
    362  1.1  uwe 	case WSMOUSEIO_GETID:
    363  1.1  uwe 		return (tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, p));
    364  1.1  uwe 
    365  1.1  uwe 	default:
    366  1.1  uwe 		return (EPASSTHROUGH);
    367  1.1  uwe 	}
    368  1.1  uwe }
    369