Commit 6c2e4d748c1395c5f4c42e0e6263613a9c05b641

Authored by Filip Navara
1 parent cbb938cb

GPIO matrix keyboard implementation

This patch introduces an emulation of the standard 4x4 matrix keyboard that
could be connected to GPIO controller.

It doesn't emulate any pull-up resistors connected to the wires and depends on
the GPIO controller for their emulation. This requires the GPIO controller to
support the notion of special -1 value on the GPIO pins, which signals that
the wire is currently disconnected.

The emulation of keys is implemented through standard QEMU keyboard emulation
capabilities. An individual key mapping is specified using a device property
"keys", which should be set to array of 16 bytes where each byte corresponds
to scan code of the key.

Signed-off-by: Filip Navara <filip.navara@gmail.com>
Makefile.target
... ... @@ -431,7 +431,7 @@ obj-arm-y += framebuffer.o
431 431 obj-arm-y += syborg.o syborg_fb.o syborg_interrupt.o syborg_keyboard.o
432 432 obj-arm-y += syborg_serial.o syborg_timer.o syborg_pointer.o syborg_rtc.o
433 433 obj-arm-y += syborg_virtio.o
434   -obj-arm-y += gpio_rotary.o
  434 +obj-arm-y += gpio_rotary.o gpio_keypad.o
435 435  
436 436 ifeq ($(TARGET_BASE_ARCH), arm)
437 437 CPPFLAGS += -DHAS_AUDIO
... ...
hw/gpio_keypad.c 0 → 100644
  1 +/*
  2 + * GPIO 4x4 Matrix Keyboard
  3 + *
  4 + * Copyright (c) 2009 Filip Navara
  5 + *
  6 + * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 + * of this software and associated documentation files (the "Software"), to deal
  8 + * in the Software without restriction, including without limitation the rights
  9 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 + * copies of the Software, and to permit persons to whom the Software is
  11 + * furnished to do so, subject to the following conditions:
  12 + *
  13 + * The above copyright notice and this permission notice shall be included in
  14 + * all copies or substantial portions of the Software.
  15 + *
  16 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 + * THE SOFTWARE.
  23 + */
  24 +
  25 +#include "sysbus.h"
  26 +#include "console.h"
  27 +
  28 +typedef struct KeyPadState {
  29 + SysBusDevice busdev;
  30 + qemu_irq out[8];
  31 + uint32_t in[8];
  32 + uint32_t *keys;
  33 + uint16_t keymask;
  34 + uint16_t keymap[256];
  35 + uint8_t extension;
  36 +} KeyPadState;
  37 +
  38 +static void keypad_update(KeyPadState *s)
  39 +{
  40 + int pin;
  41 + int keymask;
  42 +
  43 + for (pin = 0; pin < 4; pin++) {
  44 + if (s->keymask & (0xf << (pin * 4))) {
  45 + keymask = s->keymask >> (pin * 4);
  46 + qemu_set_irq(
  47 + s->out[pin + 4],
  48 + (s->in[0] && (keymask & 0x1)) ||
  49 + (s->in[1] && (keymask & 0x2)) ||
  50 + (s->in[2] && (keymask & 0x4)) ||
  51 + (s->in[3] && (keymask & 0x8)));
  52 + } else {
  53 + qemu_set_irq(s->out[pin + 4], -1);
  54 + }
  55 +
  56 + if (s->keymask & (0x1111 << pin)) {
  57 + keymask = s->keymask >> pin;
  58 + qemu_set_irq(
  59 + s->out[pin],
  60 + (s->in[4] && (keymask & 0x1)) ||
  61 + (s->in[5] && (keymask & 0x10)) ||
  62 + (s->in[6] && (keymask & 0x100)) ||
  63 + (s->in[7] && (keymask & 0x1000)));
  64 + } else {
  65 + qemu_set_irq(s->out[pin], -1);
  66 + }
  67 + }
  68 +}
  69 +
  70 +static void keypad_set_pin(void *opaque, int pin, int level)
  71 +{
  72 + KeyPadState *s = opaque;
  73 +
  74 + s->in[pin] = level;
  75 + keypad_update(s);
  76 +}
  77 +
  78 +static void keypad_keyboard_event(void *opaque, int keycode)
  79 +{
  80 + KeyPadState *s = opaque;
  81 + int index;
  82 +
  83 + if (keycode == 0xe0 && !s->extension) {
  84 + s->extension = 0x80;
  85 + return;
  86 + }
  87 +
  88 + index = (keycode & 0x7f) | s->extension;
  89 + s->extension = 0;
  90 + if (keycode & 0x80)
  91 + s->keymask &= ~s->keymap[index];
  92 + else if (keycode)
  93 + s->keymask |= s->keymap[index];
  94 +
  95 + keypad_update(s);
  96 +}
  97 +
  98 +static void keypad_save(QEMUFile *f, void *opaque)
  99 +{
  100 + KeyPadState *s = opaque;
  101 + int i;
  102 +
  103 + for (i = 0; i < 8; i++) {
  104 + qemu_put_be32(f, s->in[i]);
  105 + }
  106 + qemu_put_be16(f, s->keymask);
  107 + qemu_put_byte(f, s->extension);
  108 +}
  109 +
  110 +static int keypad_load(QEMUFile *f, void *opaque, int version_id)
  111 +{
  112 + KeyPadState *s = opaque;
  113 + int i;
  114 +
  115 + if (version_id != 1)
  116 + return -EINVAL;
  117 +
  118 + for (i = 0; i < 8; i++) {
  119 + s->in[i] = qemu_get_be32(f);
  120 + }
  121 + s->keymask = qemu_get_be16(f);
  122 + s->extension = qemu_get_byte(f);
  123 +
  124 + return 0;
  125 +}
  126 +
  127 +
  128 +/*
  129 +static void keypad_late_init(DeviceState *dev)
  130 +{
  131 + KeyPadState *s = FROM_SYSBUS(KeyPadState, sysbus_from_qdev(dev));
  132 +
  133 + keypad_update(s);
  134 +}
  135 +*/
  136 +
  137 +static void keypad_init(SysBusDevice *dev)
  138 +{
  139 + KeyPadState *s = FROM_SYSBUS(KeyPadState, dev);
  140 + int i;
  141 +
  142 + for (i = 0; i < 16; i++) {
  143 + s->keymap[s->keys[i] & 0xff] = 1 << i;
  144 + }
  145 +
  146 + qdev_init_gpio_in(&dev->qdev, keypad_set_pin, 8);
  147 + qdev_init_gpio_out(&dev->qdev, s->out, 8);
  148 + qemu_add_kbd_event_handler(keypad_keyboard_event, s);
  149 + register_savevm("gpio_keypad", -1, 1, keypad_save, keypad_load, s);
  150 +}
  151 +
  152 +static SysBusDeviceInfo keypad_info = {
  153 + .init = keypad_init,
  154 + /* .qdev.late_init = keypad_late_init, */
  155 + .qdev.name = "gpio,keypad",
  156 + .qdev.size = sizeof(KeyPadState),
  157 + .qdev.props = (Property[]) {
  158 + {
  159 + .name = "keys",
  160 + .info = &qdev_prop_ptr, /* FIXME: Make it array! */
  161 + .offset = offsetof(KeyPadState, keys),
  162 + },
  163 + {/* end of list */}
  164 + }
  165 +};
  166 +
  167 +static void keypad_register(void)
  168 +{
  169 + sysbus_register_withprop(&keypad_info);
  170 +}
  171 +
  172 +device_init(keypad_register)
... ...