[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 2/3] hw/misc/host_power: Add a simple host power device
From: |
Jian Zhang |
Subject: |
[PATCH 2/3] hw/misc/host_power: Add a simple host power device |
Date: |
Tue, 20 Sep 2022 01:21:11 +0800 |
This Host Power device privide a simple power control logic for a host,
like use a bmc to control the power of a host.
This device has 2 gpio, one is input named "button", another gpio is
output named "power-good", when button have a falling edge, invert the
"power-good" gpio.
Signed-off-by: Jian Zhang <zhangjian.3032@bytedance.com>
---
MAINTAINERS | 2 +
hw/arm/Kconfig | 1 +
hw/misc/Kconfig | 3 +
hw/misc/host_power.c | 105 +++++++++++++++++++++++++++++++++++
hw/misc/meson.build | 1 +
include/hw/misc/host_power.h | 41 ++++++++++++++
6 files changed, 153 insertions(+)
create mode 100644 hw/misc/host_power.c
create mode 100644 include/hw/misc/host_power.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 472fbf4f42..5a27a78985 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1068,6 +1068,8 @@ F: tests/qtest/*aspeed*
F: hw/arm/fby35.c
F: hw/misc/fby35_sb_cpld.c
F: hw/misc/intel_me.c
+F: include/hw/misc/host_power.h
+F: hw/misc/host_power.c
NRF51
M: Joel Stanley <joel@jms.id.au>
diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig
index 23330cca52..f6fa364ab7 100644
--- a/hw/arm/Kconfig
+++ b/hw/arm/Kconfig
@@ -455,6 +455,7 @@ config ASPEED_SOC
select EMC141X
select UNIMP
select LED
+ select HOST_POWER
select PMBUS
select MAX31785
diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig
index d0e691990a..e0b168ec1d 100644
--- a/hw/misc/Kconfig
+++ b/hw/misc/Kconfig
@@ -147,6 +147,9 @@ config UNIMP
config LED
bool
+config HOST_POWER
+ bool
+
config MAC_VIA
bool
select MOS6522
diff --git a/hw/misc/host_power.c b/hw/misc/host_power.c
new file mode 100644
index 0000000000..18d2573d5e
--- /dev/null
+++ b/hw/misc/host_power.c
@@ -0,0 +1,105 @@
+/*
+ * QEMU single Host Power device
+ *
+ * Copyright (C) 2022 Jian Zhang <zhangjian.3032@bytedance.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "migration/vmstate.h"
+#include "hw/qdev-properties.h"
+#include "hw/irq.h"
+#include "hw/misc/host_power.h"
+#include "trace.h"
+
+static void power_control(HostPowerState *s, bool on)
+{
+ if (on) {
+ qemu_set_irq(s->power_good, 1);
+ } else {
+ qemu_set_irq(s->power_good, 0);
+ }
+ s->power_status = on;
+}
+
+static void power_button_handler(void *opaque, int line, int new_state)
+{
+ HostPowerState *s = HOST_POWER(opaque);
+
+ assert(line == 0);
+
+ if (new_state == 0) {
+ /* falling edge, reverse the power status */
+ if (s->power_status == 0) {
+ power_control(s, true);
+ } else {
+ power_control(s, false);
+ }
+ }
+}
+
+static void host_power_reset(DeviceState *dev)
+{
+ HostPowerState *s = HOST_POWER(dev);
+ s->power_status = false;
+}
+
+static const VMStateDescription vmstate_host_power = {
+ .name = TYPE_HOST_POWER,
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .fields = (VMStateField[]) {
+ VMSTATE_END_OF_LIST()
+ }
+};
+
+static void host_power_realize(DeviceState *dev, Error **errp)
+{
+ HostPowerState *s = HOST_POWER(dev);
+ s->power_status = false;
+
+ /* init a power button gpio as input pin */
+ qdev_init_gpio_in_named(dev, power_button_handler, "power-button", 1);
+
+ /* init a power good gpio as output pin */
+ qdev_init_gpio_out_named(dev, &(s->power_good), "power-good", 1);
+}
+
+static void host_power_class_init(ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+
+ dc->desc = "Host Power";
+ dc->vmsd = &vmstate_host_power;
+ dc->reset = host_power_reset;
+ dc->realize = host_power_realize;
+ set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
+}
+
+static const TypeInfo host_power_info = {
+ .name = TYPE_HOST_POWER,
+ .parent = TYPE_DEVICE,
+ .instance_size = sizeof(HostPowerState),
+ .class_init = host_power_class_init
+};
+
+static void host_power_register_types(void)
+{
+ type_register_static(&host_power_info);
+}
+
+type_init(host_power_register_types)
+
+HostPowerState *host_power_create_simple(Object *parentobj)
+{
+ static const char *name = "host-power";
+ DeviceState *dev;
+
+ dev = qdev_new(TYPE_HOST_POWER);
+
+ object_property_add_child(parentobj, name, OBJECT(dev));
+ qdev_realize_and_unref(dev, NULL, &error_fatal);
+
+ return HOST_POWER(dev);
+}
diff --git a/hw/misc/meson.build b/hw/misc/meson.build
index 87d65c16a6..be14c1399a 100644
--- a/hw/misc/meson.build
+++ b/hw/misc/meson.build
@@ -9,6 +9,7 @@ softmmu_ss.add(when: 'CONFIG_SGA', if_true: files('sga.c'))
softmmu_ss.add(when: 'CONFIG_UNIMP', if_true: files('unimp.c'))
softmmu_ss.add(when: 'CONFIG_EMPTY_SLOT', if_true: files('empty_slot.c'))
softmmu_ss.add(when: 'CONFIG_LED', if_true: files('led.c'))
+softmmu_ss.add(when: 'CONFIG_HOST_POWER', if_true: files('host_power.c'))
softmmu_ss.add(when: 'CONFIG_PVPANIC_COMMON', if_true: files('pvpanic.c'))
# ARM devices
diff --git a/include/hw/misc/host_power.h b/include/hw/misc/host_power.h
new file mode 100644
index 0000000000..a2d12ded27
--- /dev/null
+++ b/include/hw/misc/host_power.h
@@ -0,0 +1,41 @@
+/*
+ * QEMU Host Power device
+ *
+ * Copyright (C) Jian Zhang <zhangjian.3032@bytedance.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_MISC_HOST_POWER_H
+#define HW_MISC_HOST_POWER_H
+
+#include "qom/object.h"
+#include "hw/qdev-core.h"
+
+#define TYPE_HOST_POWER "host-power"
+
+struct HostPowerState {
+ /* Private */
+ DeviceState parent_obj;
+ /* Public */
+
+ qemu_irq power_button;
+ qemu_irq power_good;
+
+ /* Properties */
+ bool power_status;
+ char *description;
+};
+typedef struct HostPowerState HostPowerState;
+DECLARE_INSTANCE_CHECKER(HostPowerState, HOST_POWER, TYPE_HOST_POWER)
+
+/**
+ * host_power_create_simple: Create and realize a device
+ * @parentobj: the parent object
+ *
+ * Create the device state structure, initialize it, and
+ * drop the reference to it (the device is realized).
+ *
+ */
+HostPowerState *host_power_create_simple(Object *parentobj);
+
+#endif /* HW_MISC_HOST_POWER_H */
--
2.25.1