Commit cbb938cb5ae55e49d00c9c1c1692414a578d0f3c
1 parent
e6952381
GPIO rotary encoder implementation
This patch introduces an emulation of the rotary encoder that could be connected to GPIO controller. An internal state is maintained with the values of the output pins. When a key is pressed the state is modified accordingly and the information is signaled on the output pins. Key mappings are specified by the "key-left" and "key-left-alt" properties for a counter-clockwise direction and "key-right" and "key-right-alt" for a clockwise direction. Signed-off-by: Filip Navara <filip.navara@gmail.com>
Showing
2 changed files
with
150 additions
and
0 deletions
Makefile.target
... | ... | @@ -431,6 +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 | 435 | |
435 | 436 | ifeq ($(TARGET_BASE_ARCH), arm) |
436 | 437 | CPPFLAGS += -DHAS_AUDIO | ... | ... |
hw/gpio_rotary.c
0 → 100644
1 | +/* | |
2 | + * GPIO Rotary Coder | |
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 RotaryCoderState { | |
29 | + SysBusDevice busdev; | |
30 | + qemu_irq out[2]; | |
31 | + uint8_t state; | |
32 | + uint8_t key_left; | |
33 | + uint8_t key_right; | |
34 | + uint8_t key_left_alt; | |
35 | + uint8_t key_right_alt; | |
36 | + uint8_t extension; | |
37 | +} RotaryCoderState; | |
38 | + | |
39 | +static void rotary_update(RotaryCoderState *s, int direction) | |
40 | +{ | |
41 | + s->state += direction; | |
42 | + s->state %= 4; | |
43 | + | |
44 | + qemu_set_irq(s->out[0], s->state == 1 || s->state == 2); | |
45 | + qemu_set_irq(s->out[1], s->state == 2 || s->state == 3); | |
46 | +} | |
47 | + | |
48 | +static void rotary_keyboard_event(void *opaque, int keycode) | |
49 | +{ | |
50 | + RotaryCoderState *s = opaque; | |
51 | + | |
52 | + if (keycode == 0xe0 && !s->extension) { | |
53 | + s->extension = 0x80; | |
54 | + return; | |
55 | + } | |
56 | + | |
57 | + if (!(keycode & 0x80)) { | |
58 | + keycode &= 0x7f; | |
59 | + keycode |= s->extension; | |
60 | + | |
61 | + if (keycode == s->key_left || keycode == s->key_left_alt) { | |
62 | + rotary_update(s, 3); | |
63 | + } else if (keycode == s->key_right || keycode == s->key_right_alt) { | |
64 | + rotary_update(s, 1); | |
65 | + } | |
66 | + } | |
67 | + | |
68 | + s->extension = 0; | |
69 | +} | |
70 | + | |
71 | +static void rotary_save(QEMUFile *f, void *opaque) | |
72 | +{ | |
73 | + RotaryCoderState *s = opaque; | |
74 | + | |
75 | + qemu_put_byte(f, s->state); | |
76 | + qemu_put_byte(f, s->extension); | |
77 | +} | |
78 | + | |
79 | +static int rotary_load(QEMUFile *f, void *opaque, int version_id) | |
80 | +{ | |
81 | + RotaryCoderState *s = opaque; | |
82 | + | |
83 | + if (version_id != 1) | |
84 | + return -EINVAL; | |
85 | + | |
86 | + s->state = qemu_get_byte(f); | |
87 | + s->extension = qemu_get_byte(f); | |
88 | + | |
89 | + return 0; | |
90 | +} | |
91 | + | |
92 | +/* | |
93 | +static void rotary_late_init(DeviceState *dev) | |
94 | +{ | |
95 | + RotaryCoderState *s = FROM_SYSBUS(RotaryCoderState, sysbus_from_qdev(dev)); | |
96 | + | |
97 | + rotary_update(s); | |
98 | +} | |
99 | +*/ | |
100 | + | |
101 | +static void rotary_init(SysBusDevice *dev) | |
102 | +{ | |
103 | + RotaryCoderState *s = FROM_SYSBUS(RotaryCoderState, dev); | |
104 | + | |
105 | + qdev_init_gpio_out(&dev->qdev, s->out, 2); | |
106 | + qemu_add_kbd_event_handler(rotary_keyboard_event, s); | |
107 | + register_savevm("gpio_rotary", -1, 1, rotary_save, rotary_load, s); | |
108 | +} | |
109 | + | |
110 | +static SysBusDeviceInfo rotary_info = { | |
111 | + .init = rotary_init, | |
112 | + /* .qdev.late_init = rotary_late_init, */ | |
113 | + .qdev.name = "gpio,rotary", | |
114 | + .qdev.size = sizeof(RotaryCoderState), | |
115 | + .qdev.props = (Property[]) { | |
116 | + { | |
117 | + .name = "key-left", | |
118 | + .info = &qdev_prop_uint32, | |
119 | + .offset = offsetof(RotaryCoderState, key_left), | |
120 | + .defval = (uint32_t[]) { 0xcb }, | |
121 | + }, | |
122 | + { | |
123 | + .name = "key-right", | |
124 | + .info = &qdev_prop_uint32, | |
125 | + .offset = offsetof(RotaryCoderState, key_right), | |
126 | + .defval = (uint32_t[]) { 0xcd }, | |
127 | + }, | |
128 | + { | |
129 | + .name = "key-left-alt", | |
130 | + .info = &qdev_prop_uint32, | |
131 | + .offset = offsetof(RotaryCoderState, key_left_alt), | |
132 | + .defval = (uint32_t[]) { 0x4b }, | |
133 | + }, | |
134 | + { | |
135 | + .name = "key-right-alt", | |
136 | + .info = &qdev_prop_uint32, | |
137 | + .offset = offsetof(RotaryCoderState, key_right_alt), | |
138 | + .defval = (uint32_t[]) { 0x4d }, | |
139 | + }, | |
140 | + {/* end of list */} | |
141 | + } | |
142 | +}; | |
143 | + | |
144 | +static void rotary_register(void) | |
145 | +{ | |
146 | + sysbus_register_withprop(&rotary_info); | |
147 | +} | |
148 | + | |
149 | +device_init(rotary_register) | ... | ... |