1 /* $NetBSD: wzero3_keypad.c,v 1.6 2021/08/07 16:18:53 thorpej Exp $ */ 2 3 /*- 4 * Copyright (C) 2010 NONAKA Kimihiro <nonaka@netbsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: wzero3_keypad.c,v 1.6 2021/08/07 16:18:53 thorpej Exp $"); 30 31 #include "wzero3lcd.h" 32 #include "opt_wsdisplay_compat.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/device.h> 37 #include <sys/kernel.h> 38 #include <sys/callout.h> 39 #include <sys/bus.h> 40 41 #include <arm/xscale/pxa2x0cpu.h> 42 #include <arm/xscale/pxa2x0var.h> 43 #include <arm/xscale/pxa2x0_gpio.h> 44 45 #include <machine/bootinfo.h> 46 #include <machine/config_hook.h> 47 #include <machine/platid.h> 48 #include <machine/platid_mask.h> 49 50 #include <dev/wscons/wsconsio.h> 51 #include <dev/wscons/wskbdvar.h> 52 #include <dev/wscons/wsksymvar.h> 53 #include <dev/wscons/wsksymdef.h> 54 55 #ifdef WSDISPLAY_COMPAT_RAWKBD 56 #include <dev/hpc/pckbd_encode.h> 57 #endif 58 59 #include <arch/hpcarm/dev/wzero3_reg.h> 60 #include <arch/hpcarm/dev/wzero3_sspvar.h> 61 62 enum { 63 KD_0, 64 KD_1, 65 KD_2, 66 KD_3, 67 KD_4, 68 KD_5, 69 KD_6, 70 KD_7, 71 KD_8, 72 KD_9, 73 KD_ASTERISK, 74 KD_NUMBER, 75 KD_WINDOWS, 76 KD_OK, 77 KD_ONHOOK, 78 KD_OFFHOOK, 79 KD_CLEAR, 80 KD_MOJI, 81 KD_UP, 82 KD_DOWN, 83 KD_LEFT, 84 KD_RIGHT, 85 KD_CENTER_BUTTON, 86 KD_LSOFT, 87 KD_RSOFT, 88 KD_NUM, 89 90 KD_INVALID = -1 91 }; 92 93 static int ws011sh_keyscan2keydown[32] = { 94 KD_INVALID, 95 KD_CLEAR, 96 KD_INVALID, 97 KD_OK, 98 KD_INVALID, 99 KD_LEFT, 100 KD_INVALID, 101 KD_ONHOOK, 102 KD_INVALID, 103 KD_UP, 104 KD_DOWN, 105 KD_MOJI, 106 KD_INVALID, 107 KD_WINDOWS, 108 KD_INVALID, 109 KD_RIGHT, 110 KD_INVALID, 111 KD_1, 112 KD_4, 113 KD_7, 114 KD_ASTERISK, 115 KD_2, 116 KD_5, 117 KD_8, 118 KD_0, 119 KD_CENTER_BUTTON, 120 KD_INVALID, 121 KD_3, 122 KD_6, 123 KD_9, 124 KD_NUMBER, 125 KD_INVALID, 126 }; 127 128 struct wzero3keypad_softc { 129 device_t sc_dev; 130 131 void *sc_ih; 132 int sc_intr_pin; 133 134 uint32_t sc_okeystat; 135 136 struct callout sc_poll_ch; 137 int sc_poll_interval; 138 139 device_t sc_wskbddev; 140 141 #ifdef WSDISPLAY_COMPAT_RAWKBD 142 int sc_rawkbd; 143 #endif 144 }; 145 146 static int wzero3keypad_match(device_t, cfdata_t, void *); 147 static void wzero3keypad_attach(device_t, device_t, void *); 148 149 CFATTACH_DECL_NEW(wzero3keypad, sizeof(struct wzero3keypad_softc), 150 wzero3keypad_match, wzero3keypad_attach, NULL, NULL); 151 152 static int wzero3keypad_wskbd_enable(void *, int); 153 static void wzero3keypad_wskbd_set_leds(void *, int); 154 static int wzero3keypad_wskbd_ioctl(void *, u_long, void *, int, struct lwp *); 155 156 static const int wzero3keypad_wskbd_keys[] = { 157 82, /* KD_0: 0 */ 158 79, /* KD_1: 1 */ 159 80, /* KD_2: 2 */ 160 81, /* KD_3: 3 */ 161 75, /* KD_4: 4 */ 162 76, /* KD_5: 5 */ 163 77, /* KD_6: 6 */ 164 71, /* KD_7: 7 */ 165 72, /* KD_8: 8 */ 166 73, /* KD_9: 9 */ 167 64, /* KD_ASTERISK: f6 */ 168 65, /* KD_NUMBER: f7 */ 169 221, /* KD_WINDOWS: Menu */ 170 61, /* KD_OK: f3 */ 171 59, /* KD_ONHOOK: f1 */ 172 60, /* KD_OFFHOOK: f2 */ 173 62, /* KD_CLEAR: f4 */ 174 63, /* KD_MOJI: f5 */ 175 200, /* KD_UP: Up */ 176 208, /* KD_DOWN: Down */ 177 203, /* KD_LEFT: Left */ 178 205, /* KD_RIGHT: Right */ 179 156, /* KD_CENTER_BUTTON: KP_Enter */ 180 87, /* KD_LSOFT: f11 */ 181 88, /* KD_RSOFT: f12 */ 182 }; 183 184 static const keysym_t wzero3keypad_wskbd_keydesc[] = { 185 KS_KEYCODE(59), KS_f1, 186 KS_KEYCODE(60), KS_f2, 187 KS_KEYCODE(61), KS_f3, 188 KS_KEYCODE(62), KS_f4, 189 KS_KEYCODE(63), KS_f5, 190 KS_KEYCODE(64), KS_f6, 191 KS_KEYCODE(65), KS_f7, 192 KS_KEYCODE(71), KS_7, 193 KS_KEYCODE(72), KS_8, 194 KS_KEYCODE(73), KS_9, 195 KS_KEYCODE(75), KS_4, 196 KS_KEYCODE(76), KS_5, 197 KS_KEYCODE(77), KS_6, 198 KS_KEYCODE(79), KS_1, 199 KS_KEYCODE(80), KS_2, 200 KS_KEYCODE(81), KS_3, 201 KS_KEYCODE(82), KS_0, 202 KS_KEYCODE(87), KS_f11, 203 KS_KEYCODE(88), KS_f12, 204 KS_KEYCODE(156), KS_KP_Enter, 205 KS_KEYCODE(200), KS_Up, 206 KS_KEYCODE(203), KS_Left, 207 KS_KEYCODE(205), KS_Right, 208 KS_KEYCODE(208), KS_Down, 209 KS_KEYCODE(221), KS_Menu, 210 }; 211 212 static const struct wscons_keydesc wzero3keypad_wskbd_keydesctab[] = { 213 { KB_JP, 0, 214 sizeof(wzero3keypad_wskbd_keydesc) / sizeof(keysym_t), 215 wzero3keypad_wskbd_keydesc 216 }, 217 218 { 0, 0, 0, 0 } 219 }; 220 221 static const struct wskbd_mapdata wzero3keypad_wskbd_keymapdata = { 222 wzero3keypad_wskbd_keydesctab, KB_JP 223 }; 224 225 static const struct wskbd_accessops wzero3keypad_wskbd_accessops = { 226 wzero3keypad_wskbd_enable, 227 wzero3keypad_wskbd_set_leds, 228 wzero3keypad_wskbd_ioctl, 229 }; 230 231 static int wzero3keypad_intr(void *); 232 static void wzero3keypad_poll(void *); 233 static void wzero3keypad_poll1(struct wzero3keypad_softc *, int); 234 235 static void wzero3keypad_init(struct wzero3keypad_softc *); 236 static uint32_t wzero3keypad_getkeydown(struct wzero3keypad_softc *, int); 237 238 static const struct wzero3keypad_model { 239 platid_mask_t *platid; 240 int intr_pin; 241 } wzero3keypad_table[] = { 242 #if 0 243 /* WS007SH */ 244 { 245 &platid_mask_MACH_SHARP_WZERO3_WS007SH, 246 -1, /* XXX */ 247 }, 248 #endif 249 /* WS011SH */ 250 { 251 &platid_mask_MACH_SHARP_WZERO3_WS011SH, 252 GPIO_WS011SH_KEYPAD, 253 }, 254 255 { NULL, -1, } 256 }; 257 258 static const struct wzero3keypad_model * 259 wzero3keypad_lookup(void) 260 { 261 const struct wzero3keypad_model *model; 262 263 for (model = wzero3keypad_table; model->platid != NULL; model++) { 264 if (platid_match(&platid, model->platid)) { 265 return model; 266 } 267 } 268 return NULL; 269 } 270 271 static int 272 wzero3keypad_match(device_t parent, cfdata_t cf, void *aux) 273 { 274 275 if (strcmp(cf->cf_name, "wzero3keypad") != 0) 276 return 0; 277 if (wzero3keypad_lookup() == NULL) 278 return 0; 279 return 1; 280 } 281 282 static void 283 wzero3keypad_attach(device_t parent, device_t self, void *aux) 284 { 285 struct wzero3keypad_softc *sc = device_private(self); 286 const struct wzero3keypad_model *model; 287 struct wskbddev_attach_args wska; 288 #if NWZERO3LCD > 0 289 extern int screen_rotate; 290 #endif 291 292 sc->sc_dev = self; 293 sc->sc_okeystat = 0; 294 #ifdef WSDISPLAY_COMPAT_RAWKBD 295 sc->sc_rawkbd = 0; 296 #endif 297 298 model = wzero3keypad_lookup(); 299 if (model == NULL) { 300 aprint_error(": unknown model\n"); 301 return; 302 } 303 304 aprint_normal(": keypad\n"); 305 aprint_naive("\n"); 306 307 sc->sc_intr_pin = model->intr_pin; 308 309 callout_init(&sc->sc_poll_ch, 0); 310 callout_setfunc(&sc->sc_poll_ch, wzero3keypad_poll, sc); 311 sc->sc_poll_interval = hz / 32; 312 313 #if NWZERO3LCD > 0 314 switch (screen_rotate) { 315 default: 316 case 0: 317 break; 318 319 case 270: /* counter clock-wise */ 320 ws011sh_keyscan2keydown[5] = KD_UP; 321 ws011sh_keyscan2keydown[9] = KD_RIGHT; 322 ws011sh_keyscan2keydown[10] = KD_LEFT; 323 ws011sh_keyscan2keydown[15] = KD_DOWN; 324 break; 325 } 326 #endif 327 328 /* attach wskbd */ 329 wska.console = 0; 330 wska.keymap = &wzero3keypad_wskbd_keymapdata; 331 wska.accessops = &wzero3keypad_wskbd_accessops; 332 wska.accesscookie = sc; 333 sc->sc_wskbddev = config_found(self, &wska, wskbddevprint, CFARGS_NONE); 334 335 /* setup keypad interrupt */ 336 pxa2x0_gpio_set_function(sc->sc_intr_pin, GPIO_IN); 337 sc->sc_ih = pxa2x0_gpio_intr_establish(sc->sc_intr_pin, 338 IST_EDGE_RISING, IPL_TTY, wzero3keypad_intr, sc); 339 if (sc->sc_ih == NULL) { 340 aprint_error_dev(sc->sc_dev, 341 "couldn't establish keypad interrupt\n"); 342 } 343 344 /* init hardware */ 345 wzero3keypad_init(sc); 346 } 347 348 static int 349 wzero3keypad_wskbd_enable(void *arg, int onoff) 350 { 351 352 return 0; 353 } 354 355 static void 356 wzero3keypad_wskbd_set_leds(void *arg, int leds) 357 { 358 359 /* Nothing to do */ 360 } 361 362 static int 363 wzero3keypad_wskbd_ioctl(void *arg, u_long cmd, void *data, int flags, 364 struct lwp *l) 365 { 366 #ifdef WSDISPLAY_COMPAT_RAWKBD 367 struct wzero3keypad_softc *sc = (struct wzero3keypad_softc *)arg; 368 #endif 369 370 switch (cmd) { 371 case WSKBDIO_GTYPE: 372 *(int *)data = WSKBD_TYPE_HPC_KBD; 373 return 0; 374 case WSKBDIO_SETLEDS: 375 return 0; 376 case WSKBDIO_GETLEDS: 377 *(int *)data = 0; 378 return 0; 379 #ifdef WSDISPLAY_COMPAT_RAWKBD 380 case WSKBDIO_SETMODE: 381 sc->sc_rawkbd = (*(int *)data == WSKBD_RAW); 382 return 0; 383 #endif 384 } 385 386 return EPASSTHROUGH; 387 } 388 389 static int 390 wzero3keypad_intr(void *arg) 391 { 392 struct wzero3keypad_softc *sc = (struct wzero3keypad_softc *)arg; 393 394 pxa2x0_gpio_clear_intr(sc->sc_intr_pin); 395 396 wzero3keypad_poll1(sc, 0); 397 398 callout_schedule(&sc->sc_poll_ch, sc->sc_poll_interval); 399 400 return 1; 401 } 402 403 static void 404 wzero3keypad_poll(void *v) 405 { 406 struct wzero3keypad_softc *sc = (struct wzero3keypad_softc *)v; 407 408 wzero3keypad_poll1(sc, 1); 409 410 callout_stop(&sc->sc_poll_ch); 411 } 412 413 static void 414 wzero3keypad_poll1(struct wzero3keypad_softc *sc, int doscan) 415 { 416 uint32_t keydown; 417 uint32_t diff; 418 int i; 419 int s; 420 421 s = spltty(); 422 423 keydown = wzero3keypad_getkeydown(sc, doscan); 424 diff = keydown ^ sc->sc_okeystat; 425 if (diff == 0) 426 goto out; 427 428 for (i = 0; i < KD_NUM; i++) { 429 if (diff & (1 << i)) { 430 int state = keydown & (1 << i); 431 int type = state ? WSCONS_EVENT_KEY_DOWN : 432 WSCONS_EVENT_KEY_UP; 433 int key = wzero3keypad_wskbd_keys[i]; 434 #ifdef WSDISPLAY_COMPAT_RAWKBD 435 if (sc->sc_rawkbd) { 436 int n; 437 u_char data[16]; 438 439 n = pckbd_encode(type, key, data); 440 wskbd_rawinput(sc->sc_wskbddev, data, n); 441 } else 442 #endif 443 wskbd_input(sc->sc_wskbddev, type, key); 444 } 445 } 446 sc->sc_okeystat = keydown; 447 448 out: 449 splx(s); 450 } 451 452 /*---------------------------------------------------------------------------- 453 * AK4184 keypad controller for WS011SH 454 */ 455 /* ak4184 command register */ 456 #define AKMCTRL_WR_SH 7 457 #define AKMCTRL_PAGE_SH 6 458 #define AKMCTRL_ADDR_SH 0 459 #define AKMCTRL_WRITE (0<<AKMCTRL_WR_SH) 460 #define AKMCTRL_READ (1<<AKMCTRL_WR_SH) 461 #define AKMCTRL_DATA (0<<AKMCTRL_PAGE_SH) 462 #define AKMCTRL_CTRL (1<<AKMCTRL_PAGE_SH) 463 464 static void 465 wzero3keypad_init(struct wzero3keypad_softc *sc) 466 { 467 int s; 468 469 s = spltty(); 470 471 #if 0 472 /* 473 * - key interrupt enable 474 * - key touch scan 475 * - debounce time: 1ms 476 * - wait 100us for debounce time 477 */ 478 (void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD, 479 AKMCTRL_WRITE | AKMCTRL_CTRL | (0<<AKMCTRL_ADDR_SH), 0); 480 #endif 481 482 /* unmask all keys & columns */ 483 (void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD, 484 AKMCTRL_WRITE | AKMCTRL_CTRL | (1<<AKMCTRL_ADDR_SH), 0); 485 (void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD, 486 AKMCTRL_WRITE | AKMCTRL_CTRL | (2<<AKMCTRL_ADDR_SH), 0); 487 (void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD, 488 AKMCTRL_WRITE | AKMCTRL_CTRL | (3<<AKMCTRL_ADDR_SH), 0); 489 490 /* Enable keypad interrupt (kpdata dummy read) */ 491 (void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD, 492 AKMCTRL_READ | AKMCTRL_DATA | (1<<AKMCTRL_ADDR_SH), 0); 493 494 splx(s); 495 } 496 497 static uint32_t 498 wzero3keypad_getkeydown(struct wzero3keypad_softc *sc, int doscan) 499 { 500 uint32_t keydown = 0; 501 uint16_t status; 502 uint16_t kpdata; 503 int timo; 504 505 if (doscan) { 506 /* host scan */ 507 (void) wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD, 508 AKMCTRL_WRITE | AKMCTRL_CTRL | (4<<AKMCTRL_ADDR_SH), 0); 509 delay(100); 510 } 511 512 timo = 1000; 513 do { 514 status = wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD, 515 AKMCTRL_READ | AKMCTRL_CTRL | (0<<AKMCTRL_ADDR_SH), 0); 516 } while ((status & 0xc000) == 0 && timo-- > 0); 517 518 kpdata = wzero3ssp_ic_send(WZERO3_SSP_IC_AK4184_KEYPAD, 519 AKMCTRL_READ | AKMCTRL_DATA | (1<<AKMCTRL_ADDR_SH), 0); 520 if ((status & 0xc000) == 0xc000) { 521 if (!(kpdata & 0x8000)) { 522 int i; 523 524 for (i = 0; i < 3; i++) { 525 int key, bits; 526 527 key = kpdata & 0x1f; 528 if (key == 0) 529 break; 530 bits = ws011sh_keyscan2keydown[key]; 531 if (bits != KD_INVALID) 532 keydown |= 1 << bits; 533 kpdata >>= 5; 534 } 535 } 536 } 537 538 return keydown; 539 } 540