xref: /openbmc/qemu/hw/gpio/gpio_key.c (revision 28ae3179fc52d2e4d870b635c4a412aab99759e7)
1e5a8152cSShannon Zhao /*
2e5a8152cSShannon Zhao  * GPIO key
3e5a8152cSShannon Zhao  *
4e5a8152cSShannon Zhao  * Copyright (c) 2016 Linaro Limited
5e5a8152cSShannon Zhao  *
6e5a8152cSShannon Zhao  * Author: Shannon Zhao <shannon.zhao@linaro.org>
7e5a8152cSShannon Zhao  *
8e5a8152cSShannon Zhao  * Emulate a (human) keypress -- when the key is triggered by
9e5a8152cSShannon Zhao  * setting the incoming gpio line, the outbound irq line is
10e5a8152cSShannon Zhao  * raised for 100ms before being dropped again.
11e5a8152cSShannon Zhao  *
12e5a8152cSShannon Zhao  * This program is free software; you can redistribute it and/or modify
13e5a8152cSShannon Zhao  * it under the terms of the GNU General Public License; either version 2
14e5a8152cSShannon Zhao  * of the License, or (at your option) any later version.
15e5a8152cSShannon Zhao  *
16e5a8152cSShannon Zhao  * This program is distributed in the hope that it will be useful,
17e5a8152cSShannon Zhao  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18e5a8152cSShannon Zhao  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19e5a8152cSShannon Zhao  * GNU General Public License for more details.
20e5a8152cSShannon Zhao  *
21e5a8152cSShannon Zhao  * You should have received a copy of the GNU General Public License along
22e5a8152cSShannon Zhao  * with this program; if not, see <http://www.gnu.org/licenses/>.
23e5a8152cSShannon Zhao  */
24e5a8152cSShannon Zhao 
25e5a8152cSShannon Zhao #include "qemu/osdep.h"
2664552b6bSMarkus Armbruster #include "hw/irq.h"
27e5a8152cSShannon Zhao #include "hw/sysbus.h"
28d6454270SMarkus Armbruster #include "migration/vmstate.h"
290b8fa32fSMarkus Armbruster #include "qemu/module.h"
30df43d49cSPaolo Bonzini #include "qemu/timer.h"
31db1015e9SEduardo Habkost #include "qom/object.h"
32e5a8152cSShannon Zhao 
33e5a8152cSShannon Zhao #define TYPE_GPIOKEY "gpio-key"
348063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(GPIOKEYState, GPIOKEY)
35e5a8152cSShannon Zhao #define GPIO_KEY_LATENCY 100 /* 100ms */
36e5a8152cSShannon Zhao 
37db1015e9SEduardo Habkost struct GPIOKEYState {
38e5a8152cSShannon Zhao     SysBusDevice parent_obj;
39e5a8152cSShannon Zhao 
40e5a8152cSShannon Zhao     QEMUTimer *timer;
41e5a8152cSShannon Zhao     qemu_irq irq;
42db1015e9SEduardo Habkost };
43e5a8152cSShannon Zhao 
44e5a8152cSShannon Zhao static const VMStateDescription vmstate_gpio_key = {
45e5a8152cSShannon Zhao     .name = "gpio-key",
46e5a8152cSShannon Zhao     .version_id = 1,
47e5a8152cSShannon Zhao     .minimum_version_id = 1,
483b9e779bSRichard Henderson     .fields = (const VMStateField[]) {
49e5a8152cSShannon Zhao         VMSTATE_TIMER_PTR(timer, GPIOKEYState),
50e5a8152cSShannon Zhao         VMSTATE_END_OF_LIST()
51e5a8152cSShannon Zhao     }
52e5a8152cSShannon Zhao };
53e5a8152cSShannon Zhao 
gpio_key_reset(DeviceState * dev)54e5a8152cSShannon Zhao static void gpio_key_reset(DeviceState *dev)
55e5a8152cSShannon Zhao {
56e5a8152cSShannon Zhao     GPIOKEYState *s = GPIOKEY(dev);
57e5a8152cSShannon Zhao 
58e5a8152cSShannon Zhao     timer_del(s->timer);
59e5a8152cSShannon Zhao }
60e5a8152cSShannon Zhao 
gpio_key_timer_expired(void * opaque)61e5a8152cSShannon Zhao static void gpio_key_timer_expired(void *opaque)
62e5a8152cSShannon Zhao {
63e5a8152cSShannon Zhao     GPIOKEYState *s = (GPIOKEYState *)opaque;
64e5a8152cSShannon Zhao 
65e5a8152cSShannon Zhao     qemu_set_irq(s->irq, 0);
66e5a8152cSShannon Zhao     timer_del(s->timer);
67e5a8152cSShannon Zhao }
68e5a8152cSShannon Zhao 
gpio_key_set_irq(void * opaque,int irq,int level)69e5a8152cSShannon Zhao static void gpio_key_set_irq(void *opaque, int irq, int level)
70e5a8152cSShannon Zhao {
71e5a8152cSShannon Zhao     GPIOKEYState *s = (GPIOKEYState *)opaque;
72e5a8152cSShannon Zhao 
73e5a8152cSShannon Zhao     qemu_set_irq(s->irq, 1);
74e5a8152cSShannon Zhao     timer_mod(s->timer,
75e5a8152cSShannon Zhao               qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + GPIO_KEY_LATENCY);
76e5a8152cSShannon Zhao }
77e5a8152cSShannon Zhao 
gpio_key_realize(DeviceState * dev,Error ** errp)78e5a8152cSShannon Zhao static void gpio_key_realize(DeviceState *dev, Error **errp)
79e5a8152cSShannon Zhao {
80e5a8152cSShannon Zhao     GPIOKEYState *s = GPIOKEY(dev);
81e5a8152cSShannon Zhao     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
82e5a8152cSShannon Zhao 
83e5a8152cSShannon Zhao     sysbus_init_irq(sbd, &s->irq);
84e5a8152cSShannon Zhao     qdev_init_gpio_in(dev, gpio_key_set_irq, 1);
85e5a8152cSShannon Zhao     s->timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, gpio_key_timer_expired, s);
86e5a8152cSShannon Zhao }
87e5a8152cSShannon Zhao 
gpio_key_class_init(ObjectClass * klass,void * data)88e5a8152cSShannon Zhao static void gpio_key_class_init(ObjectClass *klass, void *data)
89e5a8152cSShannon Zhao {
90e5a8152cSShannon Zhao     DeviceClass *dc = DEVICE_CLASS(klass);
91e5a8152cSShannon Zhao 
92e5a8152cSShannon Zhao     dc->realize = gpio_key_realize;
93e5a8152cSShannon Zhao     dc->vmsd = &vmstate_gpio_key;
94*e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, gpio_key_reset);
95e5a8152cSShannon Zhao }
96e5a8152cSShannon Zhao 
97e5a8152cSShannon Zhao static const TypeInfo gpio_key_info = {
98e5a8152cSShannon Zhao     .name          = TYPE_GPIOKEY,
99e5a8152cSShannon Zhao     .parent        = TYPE_SYS_BUS_DEVICE,
100e5a8152cSShannon Zhao     .instance_size = sizeof(GPIOKEYState),
101e5a8152cSShannon Zhao     .class_init    = gpio_key_class_init,
102e5a8152cSShannon Zhao };
103e5a8152cSShannon Zhao 
gpio_key_register_types(void)104e5a8152cSShannon Zhao static void gpio_key_register_types(void)
105e5a8152cSShannon Zhao {
106e5a8152cSShannon Zhao     type_register_static(&gpio_key_info);
107e5a8152cSShannon Zhao }
108e5a8152cSShannon Zhao 
109e5a8152cSShannon Zhao type_init(gpio_key_register_types)
110