Home | History | Annotate | Line # | Download | only in dev
j720tp.c revision 1.2
      1  1.2  peter /*	$NetBSD: j720tp.c,v 1.2 2006/06/27 14:36:50 peter Exp $	*/
      2  1.1  peter 
      3  1.1  peter /*-
      4  1.1  peter  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  1.1  peter  * All rights reserved.
      6  1.1  peter  *
      7  1.1  peter  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  peter  * by IWAMOTO Toshihiro and Peter Postma.
      9  1.1  peter  *
     10  1.1  peter  * Redistribution and use in source and binary forms, with or without
     11  1.1  peter  * modification, are permitted provided that the following conditions
     12  1.1  peter  * are met:
     13  1.1  peter  * 1. Redistributions of source code must retain the above copyright
     14  1.1  peter  *    notice, this list of conditions and the following disclaimer.
     15  1.1  peter  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  peter  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  peter  *    documentation and/or other materials provided with the distribution.
     18  1.1  peter  * 3. All advertising materials mentioning features or use of this software
     19  1.1  peter  *    must display the following acknowledgement:
     20  1.1  peter  *        This product includes software developed by the NetBSD
     21  1.1  peter  *        Foundation, Inc. and its contributors.
     22  1.1  peter  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  peter  *    contributors may be used to endorse or promote products derived
     24  1.1  peter  *    from this software without specific prior written permission.
     25  1.1  peter  *
     26  1.1  peter  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  peter  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  peter  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  peter  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  peter  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  peter  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  peter  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  peter  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  peter  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  peter  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  peter  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  peter  */
     38  1.1  peter 
     39  1.1  peter /* Jornada 720 touch-panel driver. */
     40  1.1  peter 
     41  1.1  peter #include <sys/cdefs.h>
     42  1.2  peter __KERNEL_RCSID(0, "$NetBSD: j720tp.c,v 1.2 2006/06/27 14:36:50 peter Exp $");
     43  1.1  peter 
     44  1.1  peter #include <sys/param.h>
     45  1.1  peter #include <sys/systm.h>
     46  1.1  peter #include <sys/device.h>
     47  1.1  peter #include <sys/kernel.h>
     48  1.1  peter #include <sys/malloc.h>
     49  1.1  peter #include <sys/ioctl.h>
     50  1.1  peter #include <sys/callout.h>
     51  1.1  peter 
     52  1.1  peter #include <machine/platid.h>
     53  1.1  peter #include <machine/platid_mask.h>
     54  1.1  peter 
     55  1.1  peter #include <arm/sa11x0/sa11x0_var.h>
     56  1.1  peter #include <arm/sa11x0/sa11x0_gpioreg.h>
     57  1.1  peter #include <arm/sa11x0/sa11x0_ppcreg.h>
     58  1.1  peter #include <arm/sa11x0/sa11x0_sspreg.h>
     59  1.1  peter 
     60  1.1  peter #include <dev/wscons/wsconsio.h>
     61  1.1  peter #include <dev/wscons/wsmousevar.h>
     62  1.1  peter #include <dev/hpc/hpctpanelvar.h>
     63  1.1  peter 
     64  1.1  peter #include <hpcarm/dev/j720sspvar.h>
     65  1.1  peter 
     66  1.1  peter #include "opt_j720tp.h"
     67  1.1  peter 
     68  1.1  peter #ifdef J720TP_DEBUG
     69  1.1  peter #define DPRINTF(arg)	printf arg
     70  1.1  peter #else
     71  1.1  peter #define DPRINTF(arg)	/* nothing */
     72  1.1  peter #endif
     73  1.1  peter 
     74  1.1  peter struct j720tp_softc {
     75  1.1  peter 	struct device		sc_dev;
     76  1.1  peter 
     77  1.1  peter 	int			sc_enabled;
     78  1.1  peter 
     79  1.1  peter 	struct callout		sc_tpcallout;
     80  1.1  peter 	struct j720ssp_softc	*sc_ssp;
     81  1.1  peter 
     82  1.1  peter 	struct device		*sc_wsmousedev;
     83  1.1  peter 
     84  1.1  peter 	struct tpcalib_softc	sc_tpcalib;
     85  1.1  peter };
     86  1.1  peter 
     87  1.1  peter static int	j720tp_match(struct device *, struct cfdata *, void *);
     88  1.1  peter static void	j720tp_attach(struct device *, struct device *, void *);
     89  1.1  peter 
     90  1.1  peter static int	j720tp_wsmouse_enable(void *);
     91  1.1  peter static int	j720tp_wsmouse_ioctl(void *, u_long, caddr_t, int,
     92  1.1  peter 			struct lwp *);
     93  1.1  peter static void	j720tp_wsmouse_disable(void *);
     94  1.1  peter 
     95  1.1  peter static void	j720tp_enable_intr(struct j720tp_softc *);
     96  1.1  peter static void	j720tp_disable_intr(struct j720tp_softc *);
     97  1.1  peter static int	j720tp_intr(void *);
     98  1.1  peter static int	j720tp_get_rawxy(struct j720tp_softc *, int *, int *);
     99  1.1  peter static void	j720tp_wsmouse_input(struct j720tp_softc *, int, int);
    100  1.1  peter static void	j720tp_wsmouse_callout(void *);
    101  1.1  peter 
    102  1.1  peter 
    103  1.1  peter const struct wsmouse_accessops j720tp_wsmouse_accessops = {
    104  1.1  peter 	j720tp_wsmouse_enable,
    105  1.1  peter 	j720tp_wsmouse_ioctl,
    106  1.1  peter 	j720tp_wsmouse_disable
    107  1.1  peter };
    108  1.1  peter 
    109  1.1  peter static const struct wsmouse_calibcoords j720tp_default_calib = {
    110  1.1  peter 	0, 0, 639, 239,
    111  1.1  peter 	4,
    112  1.1  peter 	{{ 988,  80,   0,   0 },
    113  1.1  peter 	 {  88,  84, 639,   0 },
    114  1.1  peter 	 { 988, 927,   0, 239 },
    115  1.1  peter 	 {  88, 940, 639, 239 }}
    116  1.1  peter };
    117  1.1  peter 
    118  1.1  peter CFATTACH_DECL(j720tp, sizeof(struct j720tp_softc),
    119  1.1  peter     j720tp_match, j720tp_attach, NULL, NULL);
    120  1.1  peter 
    121  1.1  peter 
    122  1.1  peter static int
    123  1.1  peter j720tp_match(struct device *parent, struct cfdata *cf, void *aux)
    124  1.1  peter {
    125  1.1  peter 
    126  1.1  peter 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
    127  1.1  peter 		return 0;
    128  1.1  peter 	if (strcmp(cf->cf_name, "j720tp") != 0)
    129  1.1  peter 		return 0;
    130  1.1  peter 
    131  1.1  peter 	return 1;
    132  1.1  peter }
    133  1.1  peter 
    134  1.1  peter static void
    135  1.1  peter j720tp_attach(struct device *parent, struct device *self, void *aux)
    136  1.1  peter {
    137  1.1  peter         struct j720tp_softc *sc = (struct j720tp_softc *)self;
    138  1.1  peter 	struct wsmousedev_attach_args wsma;
    139  1.1  peter 
    140  1.1  peter 	printf("\n");
    141  1.1  peter 
    142  1.1  peter 	sc->sc_ssp = (struct j720ssp_softc *)parent;
    143  1.1  peter 	sc->sc_enabled = 0;
    144  1.1  peter 
    145  1.1  peter 	/* Touch-panel as a pointing device. */
    146  1.1  peter 	wsma.accessops = &j720tp_wsmouse_accessops;
    147  1.1  peter 	wsma.accesscookie = sc;
    148  1.1  peter 
    149  1.1  peter 	sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &wsma,
    150  1.1  peter 	    wsmousedevprint);
    151  1.1  peter 
    152  1.1  peter 	/* Initialize calibration, set default parameters. */
    153  1.1  peter 	tpcalib_init(&sc->sc_tpcalib);
    154  1.1  peter 	tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
    155  1.1  peter 	    __UNCONST(&j720tp_default_calib), 0, 0);
    156  1.1  peter 
    157  1.1  peter 	j720tp_wsmouse_disable(sc);
    158  1.1  peter 	callout_init(&sc->sc_tpcallout);
    159  1.1  peter 
    160  1.1  peter 	/* Setup touch-panel interrupt. */
    161  1.1  peter 	sa11x0_intr_establish(0, 9, 1, IPL_TTY, j720tp_intr, sc);
    162  1.1  peter }
    163  1.1  peter 
    164  1.1  peter static int
    165  1.1  peter j720tp_wsmouse_enable(void *self)
    166  1.1  peter {
    167  1.1  peter 	struct j720tp_softc *sc = self;
    168  1.1  peter 	int s;
    169  1.1  peter 
    170  1.1  peter 	s = spltty();
    171  1.1  peter 
    172  1.1  peter 	j720tp_enable_intr(sc);
    173  1.1  peter 
    174  1.1  peter 	sc->sc_enabled = 1;
    175  1.1  peter 
    176  1.1  peter 	splx(s);
    177  1.1  peter 	return 0;
    178  1.1  peter }
    179  1.1  peter 
    180  1.1  peter static int
    181  1.1  peter j720tp_wsmouse_ioctl(void *self, u_long cmd, caddr_t data, int flag,
    182  1.1  peter     struct lwp *l)
    183  1.1  peter {
    184  1.1  peter 	struct j720tp_softc *sc = self;
    185  1.1  peter 
    186  1.1  peter 	return hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
    187  1.1  peter }
    188  1.1  peter 
    189  1.1  peter static void
    190  1.1  peter j720tp_wsmouse_disable(void *self)
    191  1.1  peter {
    192  1.1  peter 	struct j720tp_softc *sc = self;
    193  1.1  peter 	int s;
    194  1.1  peter 
    195  1.1  peter 	s = spltty();
    196  1.1  peter 
    197  1.1  peter 	j720tp_disable_intr(sc);
    198  1.1  peter 	callout_stop(&sc->sc_tpcallout);
    199  1.1  peter 
    200  1.1  peter 	sc->sc_enabled = 0;
    201  1.1  peter 
    202  1.1  peter 	splx(s);
    203  1.1  peter }
    204  1.1  peter 
    205  1.1  peter /*
    206  1.1  peter  * Enable touch-panel interrupt.  Must be called at spltty().
    207  1.1  peter  */
    208  1.1  peter static void
    209  1.1  peter j720tp_enable_intr(struct j720tp_softc *sc)
    210  1.1  peter {
    211  1.1  peter 	struct j720ssp_softc *ssp = sc->sc_ssp;
    212  1.1  peter 	uint32_t er;
    213  1.1  peter 
    214  1.1  peter 	er = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER);
    215  1.1  peter 	er |= 1 << 9;
    216  1.1  peter 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER, er);
    217  1.1  peter }
    218  1.1  peter 
    219  1.1  peter /*
    220  1.1  peter  * Disable touch-panel interrupt.  Must be called at spltty().
    221  1.1  peter  */
    222  1.1  peter static void
    223  1.1  peter j720tp_disable_intr(struct j720tp_softc *sc)
    224  1.1  peter {
    225  1.1  peter 	struct j720ssp_softc *ssp = sc->sc_ssp;
    226  1.1  peter 	uint32_t er;
    227  1.1  peter 
    228  1.1  peter 	er = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER);
    229  1.1  peter 	er &= ~(1 << 9);
    230  1.1  peter 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER, er);
    231  1.1  peter }
    232  1.1  peter 
    233  1.1  peter static int
    234  1.1  peter j720tp_intr(void *arg)
    235  1.1  peter {
    236  1.1  peter 	struct j720tp_softc *sc = arg;
    237  1.1  peter 	struct j720ssp_softc *ssp = sc->sc_ssp;
    238  1.1  peter 	int rawx, rawy;
    239  1.1  peter 
    240  1.1  peter 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_EDR, 1 << 9);
    241  1.1  peter 
    242  1.1  peter 	if (!sc->sc_enabled) {
    243  1.1  peter 		DPRINTF(("j720tp_intr: !sc_enabled\n"));
    244  1.1  peter 		return 0;
    245  1.1  peter 	}
    246  1.1  peter 
    247  1.1  peter 	j720tp_disable_intr(sc);
    248  1.1  peter 
    249  1.1  peter 	if (j720tp_get_rawxy(sc, &rawx, &rawy))
    250  1.1  peter 		j720tp_wsmouse_input(sc, rawx, rawy);
    251  1.1  peter 
    252  1.1  peter 	callout_reset(&sc->sc_tpcallout, hz / 32, j720tp_wsmouse_callout, sc);
    253  1.1  peter 
    254  1.1  peter 	return 1;
    255  1.1  peter }
    256  1.1  peter 
    257  1.1  peter static int
    258  1.1  peter j720tp_get_rawxy(struct j720tp_softc *sc, int *rawx, int *rawy)
    259  1.1  peter {
    260  1.1  peter 	struct j720ssp_softc *ssp = sc->sc_ssp;
    261  1.1  peter 	int buf[8], data, i;
    262  1.1  peter 
    263  1.1  peter 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
    264  1.1  peter 
    265  1.1  peter 	/* Send read touch-panel command. */
    266  1.2  peter 	if (j720ssp_readwrite(ssp, 1, 0xa0, &data, 100) < 0 || data != 0x11) {
    267  1.1  peter 		DPRINTF(("j720tp_get_rawxy: no dummy received\n"));
    268  1.1  peter 		goto out;
    269  1.1  peter 	}
    270  1.1  peter 
    271  1.1  peter 	for (i = 0; i < 8; i++) {
    272  1.2  peter 		if (j720ssp_readwrite(ssp, 0, 0x11, &data, 100) < 0)
    273  1.1  peter 			goto out;
    274  1.1  peter 		buf[i] = data;
    275  1.1  peter 	}
    276  1.1  peter 
    277  1.1  peter 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
    278  1.1  peter 
    279  1.1  peter 	buf[6] <<= 8;
    280  1.1  peter 	buf[7] <<= 8;
    281  1.1  peter 	for (i = 0; i < 3; i++) {
    282  1.1  peter 		buf[i] |= buf[6] & 0x300;
    283  1.1  peter 		buf[6] >>= 2;
    284  1.1  peter 		buf[i + 3] |= buf[7] & 0x300;
    285  1.1  peter 		buf[7] >>= 2;
    286  1.1  peter 	}
    287  1.1  peter 
    288  1.1  peter #if 0
    289  1.1  peter 	DPRINTF(("j720tp_get_rawxy: %d:%d:%d:%d:%d:%d:%d:%d\n", buf[0], buf[1],
    290  1.1  peter 	    buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]));
    291  1.1  peter #endif
    292  1.1  peter 
    293  1.1  peter 	*rawx = buf[1];
    294  1.1  peter 	*rawy = buf[4];
    295  1.1  peter 
    296  1.1  peter 	return 1;
    297  1.1  peter out:
    298  1.1  peter 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
    299  1.1  peter 
    300  1.1  peter 	/* reset SSP */
    301  1.1  peter 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
    302  1.1  peter 	delay(100);
    303  1.1  peter 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
    304  1.1  peter 
    305  1.1  peter 	DPRINTF(("j720tp_get_rawxy: error %x\n", data));
    306  1.1  peter 	return 0;
    307  1.1  peter }
    308  1.1  peter 
    309  1.1  peter static void
    310  1.1  peter j720tp_wsmouse_input(struct j720tp_softc *sc, int rawx, int rawy)
    311  1.1  peter {
    312  1.1  peter 	int x, y;
    313  1.1  peter 
    314  1.1  peter 	tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y);
    315  1.1  peter 	wsmouse_input(sc->sc_wsmousedev, 1, x, y, 0,
    316  1.1  peter 	    WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
    317  1.1  peter }
    318  1.1  peter 
    319  1.1  peter static void
    320  1.1  peter j720tp_wsmouse_callout(void *arg)
    321  1.1  peter {
    322  1.1  peter 	struct j720tp_softc *sc = arg;
    323  1.1  peter 	struct j720ssp_softc *ssp = sc->sc_ssp;
    324  1.1  peter 	int rawx, rawy, s;
    325  1.1  peter 
    326  1.1  peter 	s = spltty();
    327  1.1  peter 
    328  1.1  peter 	if (!sc->sc_enabled) {
    329  1.1  peter 		DPRINTF(("j720tp_wsmouse_callout: !sc_enabled\n"));
    330  1.1  peter 		splx(s);
    331  1.1  peter 		return;
    332  1.1  peter 	}
    333  1.1  peter 
    334  1.1  peter 	if (bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PLR) & (1<<9)) {
    335  1.1  peter 		wsmouse_input(sc->sc_wsmousedev, 0, 0, 0, 0, 0);
    336  1.1  peter 		j720tp_enable_intr(sc);
    337  1.1  peter 	} else {
    338  1.1  peter 		if (j720tp_get_rawxy(sc, &rawx, &rawy))
    339  1.1  peter 			j720tp_wsmouse_input(sc, rawx, rawy);
    340  1.1  peter 
    341  1.1  peter 		callout_schedule(&sc->sc_tpcallout, hz / 32);
    342  1.1  peter 	}
    343  1.1  peter 
    344  1.1  peter 	splx(s);
    345  1.1  peter }
    346