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