Home | History | Annotate | Line # | Download | only in dev
ztp.c revision 1.5.28.1
      1  1.5.28.1     skrll /*	$NetBSD: ztp.c,v 1.5.28.1 2009/03/03 18:29:49 skrll Exp $	*/
      2       1.1     peter /* $OpenBSD: zts.c,v 1.9 2005/04/24 18:55:49 uwe Exp $ */
      3       1.1     peter 
      4       1.1     peter /*
      5       1.1     peter  * Copyright (c) 2005 Dale Rahn <drahn (at) openbsd.org>
      6       1.1     peter  *
      7       1.1     peter  * Permission to use, copy, modify, and distribute this software for any
      8       1.1     peter  * purpose with or without fee is hereby granted, provided that the above
      9       1.1     peter  * copyright notice and this permission notice appear in all copies.
     10       1.1     peter  *
     11       1.1     peter  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12       1.1     peter  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13       1.1     peter  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14       1.1     peter  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15       1.1     peter  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16       1.1     peter  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17       1.1     peter  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18       1.1     peter  */
     19       1.1     peter 
     20       1.1     peter #include <sys/cdefs.h>
     21  1.5.28.1     skrll __KERNEL_RCSID(0, "$NetBSD: ztp.c,v 1.5.28.1 2009/03/03 18:29:49 skrll Exp $");
     22       1.1     peter 
     23       1.1     peter #include "lcd.h"
     24       1.1     peter 
     25       1.1     peter #include <sys/types.h>
     26       1.1     peter #include <sys/param.h>
     27       1.1     peter #include <sys/systm.h>
     28       1.1     peter #include <sys/device.h>
     29       1.1     peter #include <sys/malloc.h>
     30       1.1     peter #include <sys/kernel.h>
     31       1.1     peter #include <sys/callout.h>
     32       1.1     peter 
     33       1.1     peter #include <dev/wscons/wsconsio.h>
     34       1.1     peter #include <dev/wscons/wsmousevar.h>
     35       1.1     peter #include <dev/wscons/wsdisplayvar.h>
     36       1.1     peter 
     37       1.1     peter #include <dev/hpc/hpcfbio.h>		/* XXX: for tpctl */
     38       1.1     peter #include <dev/hpc/hpctpanelvar.h>
     39       1.1     peter 
     40       1.3    nonaka #include <arm/xscale/pxa2x0cpu.h>
     41       1.1     peter #include <arm/xscale/pxa2x0reg.h>
     42       1.1     peter #include <arm/xscale/pxa2x0var.h>
     43       1.3    nonaka #include <arm/xscale/xscalereg.h>
     44       1.1     peter #include <arm/xscale/pxa2x0_lcd.h>
     45       1.1     peter #include <arm/xscale/pxa2x0_gpio.h>
     46       1.1     peter 
     47       1.1     peter #include <zaurus/dev/zsspvar.h>
     48       1.1     peter 
     49       1.3    nonaka #ifdef ZTP_DEBUG
     50       1.3    nonaka #define	DPRINTF(s)	printf s
     51       1.3    nonaka #else
     52       1.3    nonaka #define	DPRINTF(s)
     53       1.3    nonaka #endif
     54       1.3    nonaka 
     55       1.1     peter /*
     56       1.1     peter  * ADS784x touch screen controller
     57       1.1     peter  */
     58       1.1     peter #define ADSCTRL_PD0_SH          0       /* PD0 bit */
     59       1.1     peter #define ADSCTRL_PD1_SH          1       /* PD1 bit */
     60       1.1     peter #define ADSCTRL_DFR_SH          2       /* SER/DFR bit */
     61       1.1     peter #define ADSCTRL_MOD_SH          3       /* Mode bit */
     62       1.1     peter #define ADSCTRL_ADR_SH          4       /* Address setting */
     63       1.1     peter #define ADSCTRL_STS_SH          7       /* Start bit */
     64       1.1     peter 
     65       1.1     peter #define GPIO_TP_INT_C3K		11
     66       1.1     peter #define GPIO_HSYNC_C3K		22
     67       1.1     peter 
     68       1.1     peter #define POLL_TIMEOUT_RATE0	((hz * 150)/1000)
     69       1.1     peter #define POLL_TIMEOUT_RATE1	(hz / 100) /* XXX every tick */
     70       1.1     peter 
     71       1.1     peter #define CCNT_HS_400_VGA_C3K 6250	/* 15.024us */
     72       1.1     peter 
     73       1.1     peter /* XXX need to ask zaurus_lcd.c for the screen dimension */
     74       1.1     peter #define CURRENT_DISPLAY (&sharp_zaurus_C3000)
     75       1.1     peter extern const struct lcd_panel_geometry sharp_zaurus_C3000;
     76       1.1     peter 
     77       1.1     peter /* Settable via sysctl. */
     78       1.3    nonaka int ztp_rawmode;
     79       1.1     peter 
     80       1.1     peter static const struct wsmouse_calibcoords ztp_default_calib = {
     81       1.3    nonaka 	0, 0, 479, 639,				/* minx, miny, maxx, maxy */
     82       1.3    nonaka 	5,					/* samplelen */
     83       1.3    nonaka 	{
     84       1.3    nonaka 		{ 1929, 2021, 240, 320 },	/* rawx, rawy, x, y */
     85       1.3    nonaka 		{  545, 3464,  48,  64 },
     86       1.3    nonaka 		{ 3308, 3452,  48, 576 },
     87       1.3    nonaka 		{ 2854,  768, 432, 576 },
     88       1.3    nonaka 		{  542,  593, 432,  64 }
     89       1.3    nonaka 	}
     90       1.3    nonaka };
     91       1.3    nonaka 
     92       1.3    nonaka struct ztp_pos {
     93       1.3    nonaka 	int x;
     94       1.3    nonaka 	int y;
     95       1.3    nonaka 	int z;			/* touch pressure */
     96       1.1     peter };
     97       1.1     peter 
     98       1.1     peter struct ztp_softc {
     99  1.5.28.1     skrll 	device_t sc_dev;
    100       1.1     peter 	struct callout sc_tp_poll;
    101       1.1     peter 	void *sc_gh;
    102       1.1     peter 	void *sc_powerhook;
    103       1.1     peter 	int sc_enabled;
    104       1.1     peter 	int sc_buttons; /* button emulation ? */
    105       1.1     peter 	struct device *sc_wsmousedev;
    106       1.3    nonaka 	struct ztp_pos sc_oldpos;
    107       1.1     peter 	int sc_resx;
    108       1.1     peter 	int sc_resy;
    109       1.1     peter 	struct tpcalib_softc sc_tpcalib;
    110       1.1     peter };
    111       1.1     peter 
    112  1.5.28.1     skrll static int	ztp_match(device_t, cfdata_t, void *);
    113  1.5.28.1     skrll static void	ztp_attach(device_t, device_t, void *);
    114       1.1     peter 
    115  1.5.28.1     skrll CFATTACH_DECL_NEW(ztp, sizeof(struct ztp_softc),
    116       1.1     peter 	ztp_match, ztp_attach, NULL, NULL);
    117       1.1     peter 
    118       1.1     peter static int	ztp_enable(void *);
    119       1.1     peter static void	ztp_disable(void *);
    120       1.1     peter static void	ztp_power(int, void *);
    121       1.1     peter static void	ztp_poll(void *);
    122       1.1     peter static int	ztp_irq(void *);
    123       1.2  christos static int	ztp_ioctl(void *, u_long, void *, int, struct lwp *);
    124       1.1     peter 
    125       1.3    nonaka static const struct wsmouse_accessops ztp_accessops = {
    126       1.1     peter         ztp_enable,
    127       1.1     peter 	ztp_ioctl,
    128       1.1     peter 	ztp_disable
    129       1.1     peter };
    130       1.1     peter 
    131       1.1     peter static int
    132  1.5.28.1     skrll ztp_match(device_t parent, cfdata_t cf, void *aux)
    133       1.1     peter {
    134       1.1     peter 
    135       1.1     peter 	return 1;
    136       1.1     peter }
    137       1.1     peter 
    138       1.1     peter static void
    139  1.5.28.1     skrll ztp_attach(device_t parent, device_t self, void *aux)
    140       1.1     peter {
    141  1.5.28.1     skrll 	struct ztp_softc *sc = device_private(self);
    142       1.1     peter 	struct wsmousedev_attach_args a;
    143       1.1     peter 
    144  1.5.28.1     skrll 	sc->sc_dev = self;
    145  1.5.28.1     skrll 
    146  1.5.28.1     skrll 	aprint_normal("\n");
    147  1.5.28.1     skrll 	aprint_naive("\n");
    148       1.3    nonaka 
    149       1.4        ad 	callout_init(&sc->sc_tp_poll, 0);
    150       1.1     peter 	callout_setfunc(&sc->sc_tp_poll, ztp_poll, sc);
    151       1.1     peter 
    152       1.1     peter 	/* Initialize ADS7846 Difference Reference mode */
    153       1.1     peter 	(void)zssp_ic_send(ZSSP_IC_ADS7846,
    154       1.1     peter 	    (1<<ADSCTRL_ADR_SH) | (1<<ADSCTRL_STS_SH));
    155       1.1     peter 	delay(5000);
    156       1.1     peter 	(void)zssp_ic_send(ZSSP_IC_ADS7846,
    157       1.1     peter 	    (3<<ADSCTRL_ADR_SH) | (1<<ADSCTRL_STS_SH));
    158       1.1     peter 	delay(5000);
    159       1.1     peter 	(void)zssp_ic_send(ZSSP_IC_ADS7846,
    160       1.1     peter 	    (4<<ADSCTRL_ADR_SH) | (1<<ADSCTRL_STS_SH));
    161       1.1     peter 	delay(5000);
    162       1.1     peter 	(void)zssp_ic_send(ZSSP_IC_ADS7846,
    163       1.1     peter 	    (5<<ADSCTRL_ADR_SH) | (1<<ADSCTRL_STS_SH));
    164       1.1     peter 	delay(5000);
    165       1.1     peter 
    166       1.1     peter 	a.accessops = &ztp_accessops;
    167       1.1     peter 	a.accesscookie = sc;
    168       1.1     peter 
    169       1.1     peter #if NLCD > 0
    170       1.1     peter 	sc->sc_resx = CURRENT_DISPLAY->panel_height;
    171       1.1     peter 	sc->sc_resy = CURRENT_DISPLAY->panel_width;
    172       1.1     peter #else
    173       1.1     peter 	sc->sc_resx = 480;	/* XXX */
    174       1.1     peter 	sc->sc_resy = 640;	/* XXX */
    175       1.1     peter #endif
    176       1.1     peter 
    177       1.1     peter 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    178       1.1     peter 
    179       1.1     peter 	/* Initialize calibration, set default parameters. */
    180       1.1     peter 	tpcalib_init(&sc->sc_tpcalib);
    181       1.1     peter 	tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
    182       1.1     peter 	    __UNCONST(&ztp_default_calib), 0, 0);
    183       1.1     peter }
    184       1.1     peter 
    185       1.1     peter static int
    186       1.1     peter ztp_enable(void *v)
    187       1.1     peter {
    188       1.1     peter 	struct ztp_softc *sc = (struct ztp_softc *)v;
    189       1.1     peter 
    190  1.5.28.1     skrll 	DPRINTF(("%s: ztp_enable()\n", device_xname(sc->sc_dev)));
    191       1.3    nonaka 
    192       1.3    nonaka 	if (sc->sc_enabled) {
    193  1.5.28.1     skrll 		DPRINTF(("%s: already enabled\n", device_xname(sc->sc_dev)));
    194       1.1     peter 		return EBUSY;
    195       1.3    nonaka 	}
    196       1.1     peter 
    197       1.1     peter 	callout_stop(&sc->sc_tp_poll);
    198       1.1     peter 
    199  1.5.28.1     skrll 	sc->sc_powerhook = powerhook_establish(device_xname(sc->sc_dev),
    200  1.5.28.1     skrll 	    ztp_power, sc);
    201       1.1     peter 	if (sc->sc_powerhook == NULL) {
    202  1.5.28.1     skrll 		aprint_error_dev(sc->sc_dev, "couldn't establish powerhook\n");
    203       1.1     peter 		return ENOMEM;
    204       1.1     peter 	}
    205       1.1     peter 
    206       1.1     peter 	pxa2x0_gpio_set_function(GPIO_TP_INT_C3K, GPIO_IN);
    207       1.1     peter 
    208       1.1     peter 	/* XXX */
    209       1.1     peter 	if (sc->sc_gh == NULL) {
    210       1.1     peter 		sc->sc_gh = pxa2x0_gpio_intr_establish(GPIO_TP_INT_C3K,
    211       1.1     peter 		    IST_EDGE_FALLING, IPL_TTY, ztp_irq, sc);
    212       1.1     peter 	} else {
    213       1.1     peter 		pxa2x0_gpio_intr_unmask(sc->sc_gh);
    214       1.1     peter 	}
    215       1.1     peter 
    216       1.1     peter 	/* enable interrupts */
    217       1.1     peter 	sc->sc_enabled = 1;
    218       1.1     peter 	sc->sc_buttons = 0;
    219       1.1     peter 
    220       1.1     peter 	return 0;
    221       1.1     peter }
    222       1.1     peter 
    223       1.1     peter static void
    224       1.1     peter ztp_disable(void *v)
    225       1.1     peter {
    226       1.1     peter 	struct ztp_softc *sc = (struct ztp_softc *)v;
    227       1.1     peter 
    228  1.5.28.1     skrll 	DPRINTF(("%s: ztp_disable()\n", device_xname(sc->sc_dev)));
    229       1.3    nonaka 
    230       1.1     peter 	callout_stop(&sc->sc_tp_poll);
    231       1.1     peter 
    232       1.1     peter 	if (sc->sc_powerhook != NULL) {
    233       1.1     peter 		powerhook_disestablish(sc->sc_powerhook);
    234       1.1     peter 		sc->sc_powerhook = NULL;
    235       1.1     peter 	}
    236       1.1     peter 
    237       1.3    nonaka 	if (sc->sc_gh) {
    238       1.1     peter #if 0
    239       1.1     peter 		pxa2x0_gpio_intr_disestablish(sc->sc_gh);
    240       1.1     peter 		sc->sc_gh = NULL;
    241       1.3    nonaka #else
    242       1.3    nonaka 		pxa2x0_gpio_intr_mask(sc->sc_gh);
    243       1.1     peter #endif
    244       1.1     peter 	}
    245       1.1     peter 
    246       1.1     peter 	/* disable interrupts */
    247       1.1     peter 	sc->sc_enabled = 0;
    248       1.1     peter }
    249       1.1     peter 
    250       1.1     peter static void
    251       1.1     peter ztp_power(int why, void *v)
    252       1.1     peter {
    253       1.1     peter 	struct ztp_softc *sc = (struct ztp_softc *)v;
    254       1.1     peter 
    255  1.5.28.1     skrll 	DPRINTF(("%s: ztp_power()\n", device_xname(sc->sc_dev)));
    256       1.3    nonaka 
    257       1.1     peter 	switch (why) {
    258       1.1     peter 	case PWR_STANDBY:
    259       1.1     peter 	case PWR_SUSPEND:
    260       1.1     peter 		sc->sc_enabled = 0;
    261       1.1     peter #if 0
    262       1.1     peter 		pxa2x0_gpio_intr_disestablish(sc->sc_gh);
    263       1.1     peter #endif
    264       1.1     peter 		callout_stop(&sc->sc_tp_poll);
    265       1.1     peter 
    266       1.1     peter 		pxa2x0_gpio_intr_mask(sc->sc_gh);
    267       1.1     peter 
    268       1.1     peter 		/* Turn off reference voltage but leave ADC on. */
    269       1.1     peter 		(void)zssp_ic_send(ZSSP_IC_ADS7846, (1 << ADSCTRL_PD1_SH) |
    270       1.1     peter 		    (1 << ADSCTRL_ADR_SH) | (1 << ADSCTRL_STS_SH));
    271       1.1     peter 
    272       1.3    nonaka 		pxa2x0_gpio_set_function(GPIO_TP_INT_C3K, GPIO_OUT | GPIO_SET);
    273       1.1     peter 		break;
    274       1.1     peter 
    275       1.1     peter 	case PWR_RESUME:
    276       1.1     peter 		pxa2x0_gpio_set_function(GPIO_TP_INT_C3K, GPIO_IN);
    277       1.1     peter 		pxa2x0_gpio_intr_mask(sc->sc_gh);
    278       1.1     peter 
    279       1.1     peter 		/* Enable automatic low power mode. */
    280       1.1     peter 		(void)zssp_ic_send(ZSSP_IC_ADS7846,
    281       1.1     peter 		    (4 << ADSCTRL_ADR_SH) | (1 << ADSCTRL_STS_SH));
    282       1.1     peter 
    283       1.1     peter #if 0
    284       1.1     peter 		sc->sc_gh = pxa2x0_gpio_intr_establish(GPIO_TP_INT_C3K,
    285       1.3    nonaka 		    IST_EDGE_FALLING, IPL_TTY, ztp_irq, sc);
    286       1.1     peter #else
    287       1.1     peter 		pxa2x0_gpio_intr_unmask(sc->sc_gh);
    288       1.1     peter #endif
    289       1.1     peter 		sc->sc_enabled = 1;
    290       1.1     peter 		break;
    291       1.1     peter 	}
    292       1.1     peter }
    293       1.1     peter 
    294       1.1     peter #define HSYNC()								\
    295       1.3    nonaka do {									\
    296       1.3    nonaka 	while (pxa2x0_gpio_get_bit(GPIO_HSYNC_C3K) == 0)		\
    297       1.3    nonaka 		continue;						\
    298       1.3    nonaka 	while (pxa2x0_gpio_get_bit(GPIO_HSYNC_C3K) != 0)		\
    299       1.3    nonaka 		continue;						\
    300       1.3    nonaka } while (/*CONSTCOND*/0)
    301       1.3    nonaka 
    302       1.3    nonaka static inline uint32_t pxa2x0_ccnt_enable(uint32_t);
    303       1.3    nonaka static inline uint32_t pxa2x0_read_ccnt(void);
    304       1.3    nonaka static uint32_t ztp_sync_ads784x(int, int, uint32_t);
    305       1.3    nonaka static void ztp_sync_send(uint32_t);
    306       1.3    nonaka static int ztp_readpos(struct ztp_pos *);
    307       1.1     peter 
    308       1.3    nonaka static inline uint32_t
    309       1.3    nonaka pxa2x0_ccnt_enable(uint32_t reg)
    310       1.1     peter {
    311       1.1     peter 	uint32_t rv;
    312       1.1     peter 
    313       1.1     peter 	__asm volatile("mrc p14, 0, %0, c0, c1, 0" : "=r" (rv));
    314       1.3    nonaka 	__asm volatile("mcr p14, 0, %0, c0, c1, 0" : : "r" (reg));
    315       1.3    nonaka 
    316       1.3    nonaka 	return rv;
    317       1.1     peter }
    318       1.1     peter 
    319       1.3    nonaka static inline uint32_t
    320       1.1     peter pxa2x0_read_ccnt(void)
    321       1.1     peter {
    322       1.1     peter 	uint32_t rv;
    323       1.1     peter 
    324       1.1     peter 	__asm volatile("mrc p14, 0, %0, c1, c1, 0" : "=r" (rv));
    325       1.3    nonaka 
    326       1.1     peter 	return rv;
    327       1.1     peter }
    328       1.1     peter 
    329       1.1     peter /*
    330       1.1     peter  * Communicate synchronously with the ADS784x touch screen controller.
    331       1.1     peter  */
    332       1.1     peter static uint32_t
    333       1.1     peter ztp_sync_ads784x(int dorecv/* XXX */, int dosend/* XXX */, uint32_t cmd)
    334       1.1     peter {
    335       1.3    nonaka 	uint32_t ccen;
    336       1.3    nonaka 	uint32_t rv = 0;
    337       1.1     peter 
    338       1.1     peter 	/* XXX poll hsync only if LCD is enabled */
    339       1.1     peter 
    340       1.1     peter 	/* start clock counter */
    341       1.3    nonaka 	ccen = pxa2x0_ccnt_enable(PMNC_E);
    342       1.1     peter 
    343       1.1     peter 	HSYNC();
    344       1.1     peter 
    345       1.1     peter 	if (dorecv) {
    346       1.1     peter 		/* read SSDR and disable ADS784x */
    347       1.1     peter 		rv = zssp_ic_stop(ZSSP_IC_ADS7846);
    348       1.1     peter 	}
    349       1.1     peter 
    350       1.3    nonaka 	if (dosend) {
    351       1.1     peter 		ztp_sync_send(cmd);
    352       1.3    nonaka 	}
    353       1.1     peter 
    354       1.1     peter 	/* stop clock counter */
    355       1.1     peter 	pxa2x0_ccnt_enable(ccen);
    356       1.1     peter 
    357       1.1     peter 	return rv;
    358       1.1     peter }
    359       1.1     peter 
    360       1.1     peter void
    361       1.1     peter ztp_sync_send(uint32_t cmd)
    362       1.1     peter {
    363       1.3    nonaka 	volatile uint32_t base, now;
    364       1.1     peter 	uint32_t tck;
    365       1.1     peter 
    366       1.1     peter 	/* XXX */
    367       1.1     peter 	tck = CCNT_HS_400_VGA_C3K - 151;
    368       1.1     peter 
    369       1.1     peter 	/* send dummy command; discard SSDR */
    370       1.1     peter 	(void)zssp_ic_send(ZSSP_IC_ADS7846, cmd);
    371       1.1     peter 
    372       1.1     peter 	/* wait for refresh */
    373       1.1     peter 	HSYNC();
    374       1.1     peter 
    375       1.1     peter 	/* wait after refresh */
    376       1.3    nonaka 	base = pxa2x0_read_ccnt();
    377       1.3    nonaka 	now = pxa2x0_read_ccnt();
    378       1.3    nonaka 	while ((now - base) < tck)
    379       1.3    nonaka 		now = pxa2x0_read_ccnt();
    380       1.1     peter 
    381       1.1     peter 	/* send the actual command; keep ADS784x enabled */
    382       1.1     peter 	zssp_ic_start(ZSSP_IC_ADS7846, cmd);
    383       1.1     peter }
    384       1.1     peter 
    385       1.1     peter static int
    386       1.1     peter ztp_readpos(struct ztp_pos *pos)
    387       1.1     peter {
    388       1.1     peter 	int cmd;
    389       1.1     peter 	int t0, t1;
    390       1.1     peter 	int down;
    391       1.1     peter 
    392       1.1     peter 	/* XXX */
    393       1.1     peter 	pxa2x0_gpio_set_function(GPIO_HSYNC_C3K, GPIO_IN);
    394       1.1     peter 
    395       1.1     peter 	/* check that pen is down */
    396       1.1     peter 	cmd = (1 << ADSCTRL_PD0_SH) | (1 << ADSCTRL_PD1_SH) |
    397       1.1     peter 	    (3 << ADSCTRL_ADR_SH) | (1 << ADSCTRL_STS_SH);
    398       1.3    nonaka 	t0 = zssp_ic_send(ZSSP_IC_ADS7846, cmd);
    399       1.3    nonaka 	DPRINTF(("ztp_readpos(): t0 = %d\n", t0));
    400       1.1     peter 
    401       1.3    nonaka 	down = (t0 >= 10);
    402       1.1     peter 	if (down == 0)
    403       1.1     peter 		goto out;
    404       1.1     peter 
    405       1.1     peter 	/* Y */
    406       1.1     peter 	cmd = (1 << ADSCTRL_PD0_SH) | (1 << ADSCTRL_PD1_SH) |
    407       1.1     peter 	    (1 << ADSCTRL_ADR_SH) | (1 << ADSCTRL_STS_SH);
    408       1.1     peter 	(void)ztp_sync_ads784x(0, 1, cmd);
    409       1.1     peter 
    410       1.1     peter 	/* Y */
    411       1.1     peter 	cmd = (1 << ADSCTRL_PD0_SH) | (1 << ADSCTRL_PD1_SH) |
    412       1.1     peter 	    (1 << ADSCTRL_ADR_SH) | (1 << ADSCTRL_STS_SH);
    413       1.1     peter 	(void)ztp_sync_ads784x(1, 1, cmd);
    414       1.1     peter 
    415       1.1     peter 	/* X */
    416       1.1     peter 	cmd = (1 << ADSCTRL_PD0_SH) | (1 << ADSCTRL_PD1_SH) |
    417       1.1     peter 	    (5 << ADSCTRL_ADR_SH) | (1 << ADSCTRL_STS_SH);
    418       1.1     peter 	pos->y = ztp_sync_ads784x(1, 1, cmd);
    419       1.3    nonaka 	DPRINTF(("ztp_readpos(): y = %d\n", pos->y));
    420       1.1     peter 
    421       1.1     peter 	/* T0 */
    422       1.1     peter 	cmd = (1 << ADSCTRL_PD0_SH) | (1 << ADSCTRL_PD1_SH) |
    423       1.1     peter 	    (3 << ADSCTRL_ADR_SH) | (1 << ADSCTRL_STS_SH);
    424       1.1     peter 	pos->x = ztp_sync_ads784x(1, 1, cmd);
    425       1.3    nonaka 	DPRINTF(("ztp_readpos(): x = %d\n", pos->x));
    426       1.1     peter 
    427       1.1     peter 	/* T1 */
    428       1.1     peter 	cmd = (1 << ADSCTRL_PD0_SH) | (1 << ADSCTRL_PD1_SH) |
    429       1.1     peter 	    (4 << ADSCTRL_ADR_SH) | (1 << ADSCTRL_STS_SH);
    430       1.1     peter 	t0 = ztp_sync_ads784x(1, 1, cmd);
    431       1.1     peter 	t1 = ztp_sync_ads784x(1, 0, cmd);
    432       1.3    nonaka 	DPRINTF(("ztp_readpos(): t0 = %d, t1 = %d\n", t0, t1));
    433       1.1     peter 
    434       1.1     peter 	/* check that pen is still down */
    435       1.1     peter 	/* XXX pressure sensitivity varies with X or what? */
    436       1.1     peter 	if (t0 == 0 || (pos->x * (t1 - t0) / t0) >= 15000)
    437       1.1     peter 		down = 0;
    438       1.1     peter 	pos->z = down;
    439       1.1     peter 
    440       1.1     peter out:
    441       1.1     peter 	/* Enable automatic low power mode. */
    442       1.1     peter         cmd = (4 << ADSCTRL_ADR_SH) | (1 << ADSCTRL_STS_SH);
    443       1.1     peter 	(void)zssp_ic_send(ZSSP_IC_ADS7846, cmd);
    444       1.1     peter 
    445       1.1     peter 	return down;
    446       1.1     peter }
    447       1.1     peter 
    448       1.1     peter static void
    449       1.1     peter ztp_poll(void *v)
    450       1.1     peter {
    451       1.1     peter 	int s;
    452       1.1     peter 
    453       1.1     peter 	s = spltty();
    454       1.1     peter 	(void)ztp_irq(v);
    455       1.1     peter 	splx(s);
    456       1.1     peter }
    457       1.1     peter 
    458       1.1     peter static int
    459       1.1     peter ztp_irq(void *v)
    460       1.1     peter {
    461       1.1     peter 	extern int zkbd_modstate;
    462       1.1     peter 	struct ztp_softc *sc = (struct ztp_softc *)v;
    463       1.1     peter 	struct ztp_pos tp = { 0, 0, 0 };
    464       1.1     peter 	int pindown;
    465       1.1     peter 	int down;
    466       1.1     peter 	int x, y;
    467       1.1     peter 	int s;
    468       1.1     peter 
    469       1.1     peter 	if (!sc->sc_enabled)
    470       1.1     peter 		return 0;
    471       1.1     peter 
    472       1.1     peter 	s = splhigh();
    473       1.1     peter 
    474       1.1     peter 	pindown = pxa2x0_gpio_get_bit(GPIO_TP_INT_C3K) ? 0 : 1;
    475  1.5.28.1     skrll 	DPRINTF(("%s: pindown = %d\n", device_xname(sc->sc_dev), pindown));
    476       1.1     peter 	if (pindown) {
    477       1.1     peter 		pxa2x0_gpio_intr_mask(sc->sc_gh);
    478       1.1     peter 		callout_schedule(&sc->sc_tp_poll, POLL_TIMEOUT_RATE1);
    479       1.1     peter 	}
    480       1.1     peter 
    481       1.1     peter 	down = ztp_readpos(&tp);
    482  1.5.28.1     skrll 	DPRINTF(("%s: x = %d, y = %d, z = %d, down = %d\n",
    483  1.5.28.1     skrll 	    device_xname(sc->sc_dev), tp.x, tp.y, tp.z, down));
    484       1.1     peter 
    485       1.1     peter 	if (!pindown) {
    486       1.1     peter 		pxa2x0_gpio_intr_unmask(sc->sc_gh);
    487       1.1     peter 		callout_schedule(&sc->sc_tp_poll, POLL_TIMEOUT_RATE0);
    488       1.1     peter 	}
    489       1.1     peter 	pxa2x0_gpio_clear_intr(GPIO_TP_INT_C3K);
    490       1.1     peter 
    491       1.1     peter 	splx(s);
    492       1.1     peter 
    493       1.1     peter 	if (down) {
    494       1.1     peter 		if (!ztp_rawmode) {
    495       1.1     peter 			tpcalib_trans(&sc->sc_tpcalib, tp.x, tp.y, &x, &y);
    496  1.5.28.1     skrll 			DPRINTF(("%s: x = %d, y = %d\n",
    497  1.5.28.1     skrll 			    device_xname(sc->sc_dev), x, y));
    498       1.1     peter 			tp.x = x;
    499       1.1     peter 			tp.y = y;
    500       1.1     peter 		}
    501       1.1     peter 	}
    502       1.1     peter 
    503       1.1     peter 	if (zkbd_modstate != 0 && down) {
    504       1.3    nonaka 		if (zkbd_modstate & (1 << 1)) {
    505       1.1     peter 			/* Fn */
    506       1.1     peter 			down = 2;
    507       1.3    nonaka 		} else if (zkbd_modstate & (1 << 2)) {
    508       1.1     peter 			/* 'Alt' */
    509       1.1     peter 			down = 4;
    510       1.1     peter 		}
    511       1.1     peter 	}
    512       1.1     peter 	if (!down) {
    513       1.1     peter 		/* x/y values are not reliable when pen is up */
    514       1.3    nonaka 		tp = sc->sc_oldpos;
    515       1.1     peter 	}
    516       1.1     peter 
    517       1.1     peter 	if (down || sc->sc_buttons != down) {
    518       1.3    nonaka 		wsmouse_input(sc->sc_wsmousedev, down, tp.x, tp.y, 0, 0,
    519       1.3    nonaka 		    WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
    520       1.1     peter 		sc->sc_buttons = down;
    521       1.3    nonaka 		sc->sc_oldpos = tp;
    522       1.1     peter 	}
    523       1.1     peter 
    524       1.1     peter 	return 1;
    525       1.1     peter }
    526       1.1     peter 
    527       1.1     peter static int
    528       1.2  christos ztp_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    529       1.1     peter {
    530       1.1     peter 	struct ztp_softc *sc = (struct ztp_softc *)v;
    531       1.1     peter 	struct wsmouse_id *id;
    532       1.1     peter 
    533       1.1     peter 	switch (cmd) {
    534       1.1     peter 	case WSMOUSEIO_GTYPE:
    535       1.1     peter 		*(u_int *)data = WSMOUSE_TYPE_TPANEL;
    536       1.1     peter 		return 0;
    537       1.1     peter 
    538       1.1     peter 	case WSMOUSEIO_GETID:
    539       1.1     peter 		/*
    540       1.1     peter 		 * return unique ID string,
    541       1.1     peter 		 * "<vendor> <model> <serial number>"
    542       1.1     peter 		 */
    543       1.1     peter 		id = (struct wsmouse_id *)data;
    544       1.1     peter 		if (id->type != WSMOUSE_ID_TYPE_UIDSTR)
    545       1.1     peter 			return EINVAL;
    546       1.1     peter 		strlcpy(id->data, "Sharp SL-C3x00 SN000000", WSMOUSE_ID_MAXLEN);
    547       1.1     peter 		id->length = strlen(id->data);
    548       1.1     peter 		return 0;
    549       1.1     peter 
    550       1.1     peter 	case WSMOUSEIO_SCALIBCOORDS:
    551       1.1     peter 	case WSMOUSEIO_GCALIBCOORDS:
    552       1.1     peter 		return hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
    553       1.1     peter 	}
    554       1.1     peter 
    555       1.1     peter 	return EPASSTHROUGH;
    556       1.1     peter }
    557