4 * Copyright (c) 2016 Linaro Limited
6 * Author: Shannon Zhao <shannon.zhao@linaro.org>
8 * Emulate a (human) keypress -- when the key is triggered by
9 * setting the incoming gpio line, the outbound irq line is
10 * raised for 100ms before being dropped again.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "qemu/osdep.h"
27 #include "hw/sysbus.h"
28 #include "migration/vmstate.h"
29 #include "qemu/module.h"
30 #include "qemu/timer.h"
31 #include "qom/object.h"
33 #define TYPE_GPIOKEY "gpio-key"
34 OBJECT_DECLARE_SIMPLE_TYPE(GPIOKEYState
, GPIOKEY
)
35 #define GPIO_KEY_LATENCY 100 /* 100ms */
38 SysBusDevice parent_obj
;
44 static const VMStateDescription vmstate_gpio_key
= {
47 .minimum_version_id
= 1,
48 .fields
= (const VMStateField
[]) {
49 VMSTATE_TIMER_PTR(timer
, GPIOKEYState
),
54 static void gpio_key_reset(DeviceState
*dev
)
56 GPIOKEYState
*s
= GPIOKEY(dev
);
61 static void gpio_key_timer_expired(void *opaque
)
63 GPIOKEYState
*s
= (GPIOKEYState
*)opaque
;
65 qemu_set_irq(s
->irq
, 0);
69 static void gpio_key_set_irq(void *opaque
, int irq
, int level
)
71 GPIOKEYState
*s
= (GPIOKEYState
*)opaque
;
73 qemu_set_irq(s
->irq
, 1);
75 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + GPIO_KEY_LATENCY
);
78 static void gpio_key_realize(DeviceState
*dev
, Error
**errp
)
80 GPIOKEYState
*s
= GPIOKEY(dev
);
81 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
83 sysbus_init_irq(sbd
, &s
->irq
);
84 qdev_init_gpio_in(dev
, gpio_key_set_irq
, 1);
85 s
->timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, gpio_key_timer_expired
, s
);
88 static void gpio_key_class_init(ObjectClass
*klass
, void *data
)
90 DeviceClass
*dc
= DEVICE_CLASS(klass
);
92 dc
->realize
= gpio_key_realize
;
93 dc
->vmsd
= &vmstate_gpio_key
;
94 dc
->reset
= &gpio_key_reset
;
97 static const TypeInfo gpio_key_info
= {
99 .parent
= TYPE_SYS_BUS_DEVICE
,
100 .instance_size
= sizeof(GPIOKEYState
),
101 .class_init
= gpio_key_class_init
,
104 static void gpio_key_register_types(void)
106 type_register_static(&gpio_key_info
);
109 type_init(gpio_key_register_types
)