1 /* $NetBSD: psh3tp.c,v 1.15 2010/08/08 16:51:34 chs Exp $ */ 2 /* 3 * Copyright (c) 2005 KIYOHARA Takashi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 31 #include <sys/types.h> 32 #include <sys/param.h> 33 #include <sys/device.h> 34 #include <sys/errno.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/systm.h> 38 #include <sys/callout.h> 39 40 #include "opt_psh3tp.h" 41 42 #include <dev/wscons/wsconsio.h> 43 #include <dev/wscons/wsmousevar.h> 44 #include <dev/hpc/hpctpanelvar.h> 45 46 #include <machine/platid.h> 47 #include <machine/platid_mask.h> 48 49 #include <machine/intr.h> 50 51 #include <sh3/exception.h> 52 #include <sh3/intcreg.h> 53 #include <sh3/pfcreg.h> 54 #include <sh3/adcreg.h> 55 56 #include <sh3/dev/adcvar.h> 57 58 59 #ifdef PSH3TP_DEBUG 60 volatile int psh3tp_debug = 4; 61 #define DPRINTF_PRINTF printf_nolog 62 #define DPRINTF(arg) if (psh3tp_debug) DPRINTF_PRINTF arg 63 #define DPRINTFN(n, arg) if (psh3tp_debug > (n)) DPRINTF_PRINTF arg 64 #else 65 #define DPRINTF(arg) ((void)0) 66 #define DPRINTFN(n, arg) ((void)0) 67 #endif 68 69 70 /* 71 * PFC bits pertinent to PERSONA HPW-50PA touch-panel 72 */ 73 #define PHDR_TP_PEN_UP 0x40 74 #define SCPDR_TP_SCAN_ENABLE 0x20 75 #define SCPDR_TP_SCAN_DISABLE 0x01 76 #define SCPDR_TP_SCAN_X 0x06 77 #define SCPDR_TP_SCAN_Y 0x09 78 79 /* 80 * A/D converter channels to get x/y from 81 */ 82 #define ADC_CHANNEL_TP_X 1 83 #define ADC_CHANNEL_TP_Y 0 84 85 /* 86 * Default (read: my device) raw X/Y values for framebuffer edges. 87 */ 88 #define PSH3TP_FB_RIGHT 56 89 #define PSH3TP_FB_LEFT 969 90 #define PSH3TP_FB_TOP 848 91 #define PSH3TP_FB_BOTTOM 121 92 93 94 struct psh3tp_softc { 95 device_t sc_dev; 96 97 #define PSH3TP_WSMOUSE_ENABLED 0x01 98 int sc_enabled; 99 struct callout sc_touch_ch; 100 device_t sc_wsmousedev; 101 struct tpcalib_softc sc_tpcalib; /* calibration info for wsmouse */ 102 }; 103 104 105 /* config machinery */ 106 static int psh3tp_match(device_t, struct cfdata *, void *); 107 static void psh3tp_attach(device_t, device_t, void *); 108 109 /* wsmouse accessops */ 110 static int psh3tp_wsmouse_enable(void *); 111 static int psh3tp_wsmouse_ioctl(void *, u_long, void *, int, struct lwp *); 112 static void psh3tp_wsmouse_disable(void *); 113 114 /* internal driver routines */ 115 static void psh3tp_enable(struct psh3tp_softc *); 116 static void psh3tp_disable(struct psh3tp_softc *); 117 static int psh3tp_set_enable(struct psh3tp_softc *, int, int); 118 static int psh3tp_intr(void *); 119 static void psh3tp_start_polling(void *); 120 static void psh3tp_stop_polling(struct psh3tp_softc *); 121 static void psh3tp_callout_wsmouse(void *); 122 static void psh3tp_wsmouse_input(struct psh3tp_softc *, int, int); 123 static void psh3tp_get_raw_xy(int *, int *); 124 125 126 const struct wsmouse_accessops psh3tp_accessops = { 127 psh3tp_wsmouse_enable, 128 psh3tp_wsmouse_ioctl, 129 psh3tp_wsmouse_disable 130 }; 131 132 static const struct wsmouse_calibcoords psh3tp_default_calib = { 133 0, 0, 639, 239, 134 4, 135 {{ PSH3TP_FB_LEFT, PSH3TP_FB_TOP, 0, 0 }, 136 { PSH3TP_FB_RIGHT, PSH3TP_FB_TOP, 639, 0 }, 137 { PSH3TP_FB_LEFT, PSH3TP_FB_BOTTOM, 0, 239 }, 138 { PSH3TP_FB_RIGHT, PSH3TP_FB_BOTTOM, 639, 239 }} 139 }; 140 141 142 CFATTACH_DECL_NEW(psh3tp, sizeof(struct psh3tp_softc), 143 psh3tp_match, psh3tp_attach, NULL, NULL); 144 145 146 /* ARGSUSED */ 147 static int 148 psh3tp_match(device_t parent __unused, struct cfdata *cf, void *aux __unused) 149 { 150 151 if (!platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA)) 152 return 0; 153 154 if (strcmp(cf->cf_name, "psh3tp") != 0) 155 return 0; 156 157 return 1; 158 } 159 160 161 /* 162 * Attach the touch panel driver and its wsmouse child. 163 * 164 * Note that we have to use submatch to distinguish between child because 165 * wsmouse_match matches unconditionally. 166 */ 167 /* ARGSUSED */ 168 static void 169 psh3tp_attach(device_t parent __unused, device_t self, void *aux __unused) 170 { 171 struct psh3tp_softc *sc = device_private(self); 172 struct wsmousedev_attach_args wsma; 173 174 aprint_naive("\n"); 175 aprint_normal("\n"); 176 177 sc->sc_dev = self; 178 sc->sc_enabled = 0; 179 180 /* touch-panel as a pointing device */ 181 wsma.accessops = &psh3tp_accessops; 182 wsma.accesscookie = sc; 183 184 sc->sc_wsmousedev = config_found_ia( 185 self, "wsmousedev", &wsma, wsmousedevprint); 186 if (sc->sc_wsmousedev == NULL) 187 return; 188 189 /* init calibration, set default parameters */ 190 tpcalib_init(&sc->sc_tpcalib); 191 tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS, 192 (void *)__UNCONST(&psh3tp_default_calib), 0, 0); 193 194 /* used when in polling mode */ 195 callout_init(&sc->sc_touch_ch, 0); 196 197 /* establish interrupt handler, but disable until opened */ 198 intc_intr_establish(SH7709_INTEVT2_IRQ2, 199 IST_EDGE, IPL_TTY, psh3tp_intr, sc); 200 intc_intr_disable(SH7709_INTEVT2_IRQ2); 201 202 if (!pmf_device_register(self, NULL, NULL)) 203 aprint_error_dev(self, "unable to establish power handler\n"); 204 } 205 206 207 /* 208 * Enable touch panel: we start in interrupt mode. 209 * Must be called at spltty(). 210 */ 211 /* ARGSUSED */ 212 static void 213 psh3tp_enable(struct psh3tp_softc *sc __unused) 214 { 215 216 DPRINTFN(2, ("%s: enable\n", device_xname(sc->sc_dev))); 217 intc_intr_enable(SH7709_INTEVT2_IRQ2); 218 } 219 220 221 /* 222 * Disable touch panel: disable interrupt, cancel pending callout. 223 * Must be called at spltty(). 224 */ 225 static void 226 psh3tp_disable(struct psh3tp_softc *sc) 227 { 228 229 DPRINTFN(2, ("%s: disable\n", device_xname(sc->sc_dev))); 230 intc_intr_disable(SH7709_INTEVT2_IRQ2); 231 callout_stop(&sc->sc_touch_ch); 232 } 233 234 235 static int 236 psh3tp_set_enable(struct psh3tp_softc *sc, int on, int child) 237 { 238 int s = spltty(); 239 240 if (on) { 241 if (!sc->sc_enabled) 242 psh3tp_enable(sc); 243 sc->sc_enabled |= child; 244 } else { 245 sc->sc_enabled &= ~child; 246 if (!sc->sc_enabled) 247 psh3tp_disable(sc); 248 } 249 250 splx(s); 251 return 0; 252 } 253 254 255 static int 256 psh3tp_wsmouse_enable(void *cookie) 257 { 258 struct psh3tp_softc *sc = (struct psh3tp_softc *)cookie; 259 260 DPRINTFN(1, ("%s: wsmouse enable\n", device_xname(sc->sc_dev))); 261 return psh3tp_set_enable(sc, 1, PSH3TP_WSMOUSE_ENABLED); 262 } 263 264 265 static void 266 psh3tp_wsmouse_disable(void *cookie) 267 { 268 struct psh3tp_softc *sc = (struct psh3tp_softc *)cookie; 269 270 DPRINTFN(1, ("%s: wsmouse disable\n", device_xname(sc->sc_dev))); 271 psh3tp_set_enable(sc, 0, PSH3TP_WSMOUSE_ENABLED); 272 } 273 274 275 static int 276 psh3tp_intr(void *arg) 277 { 278 struct psh3tp_softc *sc = (struct psh3tp_softc *)arg; 279 280 uint8_t irr0; 281 uint8_t phdr, touched; 282 unsigned int steady, tremor_timeout; 283 284 irr0 = _reg_read_1(SH7709_IRR0); 285 if ((irr0 & IRR0_IRQ2) == 0) { 286 #ifdef DIAGNOSTIC 287 printf("%s: irr0 %02x?\n", device_xname(sc->sc_dev), irr0); 288 #endif 289 return 0; 290 } 291 292 if (!sc->sc_enabled) { 293 DPRINTFN(1, ("%s: intr: !sc_enabled\n", device_xname(sc->sc_dev))); 294 intc_intr_disable(SH7709_INTEVT2_IRQ2); 295 goto served; 296 } 297 298 /* 299 * Number of times the "touched" bit should be read 300 * consecutively. 301 */ 302 #define TREMOR_THRESHOLD 0x300 303 steady = 0; 304 tremor_timeout = TREMOR_THRESHOLD * 16; /* XXX: arbitrary */ 305 touched = true; /* we start with "touched" state */ 306 307 do { 308 uint8_t state; 309 310 phdr = _reg_read_1(SH7709_PHDR); 311 state = ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP); 312 313 if (state == touched) 314 ++steady; 315 else { 316 steady = 0; 317 touched = state; 318 } 319 320 if (--tremor_timeout == 0) { 321 DPRINTF(("%s: tremor timeout!\n", 322 device_xname(sc->sc_dev))); 323 goto served; 324 } 325 } while (steady < TREMOR_THRESHOLD); 326 327 if (touched) { 328 intc_intr_disable(SH7709_INTEVT2_IRQ2); 329 330 /* 331 * ADC readings are not stable yet, so schedule 332 * callout instead of accessing ADC from the interrupt 333 * handler only to immediately delay(). 334 */ 335 callout_reset(&sc->sc_touch_ch, 336 hz/32, psh3tp_start_polling, sc); 337 } else 338 DPRINTFN(1, ("%s: tremor\n", device_xname(sc->sc_dev))); 339 served: 340 /* clear the interrupt */ 341 _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2); 342 343 return 1; 344 } 345 346 347 /* 348 * Called from the interrupt handler at spltty() upon first touch. 349 * Decide if we are going to report this touch as a mouse click/drag. 350 */ 351 static void 352 psh3tp_start_polling(void *arg) 353 { 354 struct psh3tp_softc *sc = (struct psh3tp_softc *)arg; 355 uint8_t phdr; 356 int rawx, rawy; 357 358 phdr = _reg_read_1(SH7709_PHDR); 359 if ((phdr & PHDR_TP_PEN_UP) == PHDR_TP_PEN_UP) { 360 DPRINTFN(2, ("%s: start: pen is not down\n", 361 device_xname(sc->sc_dev))); 362 psh3tp_stop_polling(sc); 363 return; 364 } 365 366 psh3tp_get_raw_xy(&rawx, &rawy); 367 DPRINTFN(2, ("%s: start: %4d %4d -> ", 368 device_xname(sc->sc_dev), rawx, rawy)); 369 370 if (sc->sc_enabled & PSH3TP_WSMOUSE_ENABLED) { 371 DPRINTFN(2, ("mouse\n")); 372 psh3tp_wsmouse_input(sc, rawx, rawy); 373 callout_reset(&sc->sc_touch_ch, 374 hz/32, psh3tp_callout_wsmouse, sc); 375 } else { 376 DPRINTFN(2, ("ignore\n")); 377 psh3tp_stop_polling(sc); 378 } 379 } 380 381 382 /* 383 * Re-enable touch panel interrupt. 384 * Called at spltty() when polling code detects pen-up. 385 */ 386 /* ARGSUSED */ 387 static void 388 psh3tp_stop_polling(struct psh3tp_softc *sc __unused) 389 { 390 uint8_t irr0; 391 392 DPRINTFN(2, ("%s: stop\n", device_xname(sc->sc_dev))); 393 394 /* clear pending interrupt signal before re-enabling the interrupt */ 395 irr0 = _reg_read_1(SH7709_IRR0); 396 if ((irr0 & IRR0_IRQ2) != 0) 397 _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2); 398 399 intc_intr_enable(SH7709_INTEVT2_IRQ2); 400 } 401 402 403 /* 404 * We are reporting this touch as a mouse click/drag. 405 */ 406 static void 407 psh3tp_callout_wsmouse(void *arg) 408 { 409 struct psh3tp_softc *sc = (struct psh3tp_softc *)arg; 410 uint8_t phdr; 411 int rawx, rawy; 412 int s; 413 414 s = spltty(); 415 416 if (!sc->sc_enabled) { 417 DPRINTFN(1, ("%s: wsmouse callout: !sc_enabled\n", 418 device_xname(sc->sc_dev))); 419 splx(s); 420 return; 421 } 422 423 phdr = _reg_read_1(SH7709_PHDR); 424 if ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP) { 425 psh3tp_get_raw_xy(&rawx, &rawy); 426 psh3tp_wsmouse_input(sc, rawx, rawy); /* mouse dragged */ 427 callout_schedule(&sc->sc_touch_ch, hz/32); 428 } else { 429 wsmouse_input( /* button up */ 430 sc->sc_wsmousedev, 0, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA); 431 psh3tp_stop_polling(sc); 432 } 433 splx(s); 434 } 435 436 437 /* 438 * Report mouse click/drag. 439 */ 440 static void 441 psh3tp_wsmouse_input(struct psh3tp_softc *sc, int rawx, int rawy) 442 { 443 int x, y; 444 445 tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y); 446 447 DPRINTFN(3, ("%s: %4d %4d -> %3d %3d\n", 448 device_xname(sc->sc_dev), rawx, rawy, x, y)); 449 450 wsmouse_input(sc->sc_wsmousedev, 451 1, /* button */ 452 x, y, 0, 0, 453 WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y); 454 } 455 456 457 /* 458 * Read raw X/Y coordinates from the ADC. 459 */ 460 static void 461 psh3tp_get_raw_xy(int *rawxp, int *rawyp) 462 { 463 uint8_t scpdr; 464 465 /* X axis */ 466 scpdr = _reg_read_1(SH7709_SCPDR); 467 scpdr &= ~SCPDR_TP_SCAN_DISABLE; 468 scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_X); 469 _reg_write_1(SH7709_SCPDR, scpdr); 470 delay(40); 471 472 *rawxp = adc_sample_channel(ADC_CHANNEL_TP_X); 473 474 /* Y axis */ 475 scpdr = _reg_read_1(SH7709_SCPDR); 476 scpdr &= ~SCPDR_TP_SCAN_X; 477 scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y); 478 _reg_write_1(SH7709_SCPDR, scpdr); 479 delay(40); 480 481 *rawyp = adc_sample_channel(ADC_CHANNEL_TP_Y); 482 483 /* restore SCPDR */ 484 scpdr = _reg_read_1(SH7709_SCPDR); 485 scpdr &= ~(SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y); 486 scpdr |= SCPDR_TP_SCAN_DISABLE; 487 _reg_write_1(SH7709_SCPDR, scpdr); 488 } 489 490 491 static int 492 psh3tp_wsmouse_ioctl(void *cookie, u_long cmd, void *data, int flag, 493 struct lwp *l) 494 { 495 struct psh3tp_softc *sc = (struct psh3tp_softc *)cookie; 496 497 return hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, l); 498 } 499