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