From: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
To: qemu-devel@nongnu.org
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
"Andrew Jeffery" <andrew@aj.id.au>,
"Philippe Mathieu-Daudé" <philmd@redhat.com>,
"Philippe Mathieu-Daudé" <f4bug@amsat.org>,
qemu-arm@nongnu.org, "Joel Stanley" <joel@jms.id.au>,
"Cédric Le Goater" <clg@kaod.org>
Subject: [PATCH v3 1/7] hw/misc: Add a LED device
Date: Sun, 21 Jun 2020 01:07:13 +0200 [thread overview]
Message-ID: <20200620230719.32139-2-f4bug@amsat.org> (raw)
In-Reply-To: <20200620230719.32139-1-f4bug@amsat.org>
Add a LED device which can be connected to a GPIO output.
LEDs are limited to a set of colors.
They can also be dimmed with PWM devices. For now we do
not implement the dimmed mode, but in preparation of a
future implementation, we start using the LED intensity.
When used with GPIOs, the intensity can only be either
minium or maximum. This depends of the polarity of the
GPIO.
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
include/hw/misc/led.h | 79 +++++++++++++++++++++++++++
hw/misc/led.c | 121 ++++++++++++++++++++++++++++++++++++++++++
MAINTAINERS | 6 +++
hw/misc/Kconfig | 3 ++
hw/misc/Makefile.objs | 1 +
hw/misc/trace-events | 3 ++
6 files changed, 213 insertions(+)
create mode 100644 include/hw/misc/led.h
create mode 100644 hw/misc/led.c
diff --git a/include/hw/misc/led.h b/include/hw/misc/led.h
new file mode 100644
index 0000000000..821ee1247d
--- /dev/null
+++ b/include/hw/misc/led.h
@@ -0,0 +1,79 @@
+/*
+ * QEMU single LED device
+ *
+ * Copyright (C) 2020 Philippe Mathieu-Daudé <f4bug@amsat.org>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_MISC_LED_H
+#define HW_MISC_LED_H
+
+#include "hw/qdev-core.h"
+
+#define TYPE_LED "led"
+#define LED(obj) OBJECT_CHECK(LEDState, (obj), TYPE_LED)
+
+typedef enum {
+ LED_COLOR_UNKNOWN,
+ LED_COLOR_RED,
+ LED_COLOR_ORANGE,
+ LED_COLOR_AMBER,
+ LED_COLOR_YELLOW,
+ LED_COLOR_GREEN,
+ LED_COLOR_BLUE,
+ LED_COLOR_VIOLET, /* PURPLE */
+ LED_COLOR_WHITE,
+ LED_COLOR_COUNT
+} LEDColor;
+
+/* Definitions useful when a LED is connected to a GPIO */
+#define LED_RESET_INTENSITY_ACTIVE_LOW UINT16_MAX
+#define LED_RESET_INTENSITY_ACTIVE_HIGH 0U
+
+typedef struct LEDState {
+ /* Private */
+ DeviceState parent_obj;
+ /* Public */
+
+ /* Properties */
+ char *description;
+ char *color;
+ /*
+ * When used with GPIO, the intensity at reset is related to GPIO polarity
+ */
+ uint16_t reset_intensity;
+} LEDState;
+
+/**
+ * led_set_intensity: set the intensity of a LED device
+ * @s: the LED object
+ * @intensity: new intensity
+ *
+ * This utility is meant for LED connected to PWM.
+ */
+void led_set_intensity(LEDState *s, uint16_t intensity);
+
+/**
+ * led_set_intensity: set the state of a LED device
+ * @s: the LED object
+ * @is_on: boolean indicating whether the LED is emitting
+ *
+ * This utility is meant for LED connected to GPIO.
+ */
+void led_set_state(LEDState *s, bool is_on);
+
+/**
+ * create_led: create and LED device
+ * @parent: the parent object
+ * @color: color of the LED
+ * @description: description of the LED (optional)
+ * @reset_intensity: LED intensity at reset
+ *
+ * This utility function creates a LED object.
+ */
+DeviceState *create_led(Object *parentobj,
+ LEDColor color,
+ const char *description,
+ uint16_t reset_intensity);
+
+#endif /* HW_MISC_LED_H */
diff --git a/hw/misc/led.c b/hw/misc/led.c
new file mode 100644
index 0000000000..e55ed7dbc4
--- /dev/null
+++ b/hw/misc/led.c
@@ -0,0 +1,121 @@
+/*
+ * QEMU single LED device
+ *
+ * Copyright (C) 2020 Philippe Mathieu-Daudé <f4bug@amsat.org>
+ *
+ * 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/misc/led.h"
+#include "trace.h"
+
+static const char *led_color(LEDColor color)
+{
+ static const char *color_name[LED_COLOR_COUNT] = {
+ [LED_COLOR_RED] = "red",
+ [LED_COLOR_ORANGE] = "orange",
+ [LED_COLOR_AMBER] = "amber",
+ [LED_COLOR_YELLOW] = "yellow",
+ [LED_COLOR_GREEN] = "green",
+ [LED_COLOR_BLUE] = "blue",
+ [LED_COLOR_VIOLET] = "violet", /* PURPLE */
+ [LED_COLOR_WHITE] = "white",
+ };
+ return color_name[color] ? color_name[color] : "unknown";
+}
+
+void led_set_intensity(LEDState *s, uint16_t new_intensity)
+{
+ trace_led_set_intensity(s->description ? s->description : "n/a",
+ s->color, new_intensity);
+ s->current_intensity = new_intensity;
+}
+
+void led_set_state(LEDState *s, bool is_on)
+{
+ led_set_intensity(s, is_on ? UINT16_MAX : 0);
+}
+
+static void led_reset(DeviceState *dev)
+{
+ LEDState *s = LED(dev);
+
+ led_set_intensity(s, s->reset_intensity);
+}
+
+static const VMStateDescription vmstate_led = {
+ .name = TYPE_LED,
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .fields = (VMStateField[]) {
+ VMSTATE_END_OF_LIST()
+ }
+};
+
+static void led_realize(DeviceState *dev, Error **errp)
+{
+ LEDState *s = LED(dev);
+
+ if (s->color == NULL) {
+ error_setg(errp, "property 'color' not specified");
+ return;
+ }
+}
+
+static Property led_properties[] = {
+ DEFINE_PROP_STRING("color", LEDState, color),
+ DEFINE_PROP_STRING("description", LEDState, description),
+ DEFINE_PROP_UINT16("reset_intensity", LEDState, reset_intensity, 0),
+ DEFINE_PROP_END_OF_LIST(),
+};
+
+static void led_class_init(ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+
+ dc->desc = "LED";
+ dc->vmsd = &vmstate_led;
+ dc->reset = led_reset;
+ dc->realize = led_realize;
+ set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
+ device_class_set_props(dc, led_properties);
+}
+
+static const TypeInfo led_info = {
+ .name = TYPE_LED,
+ .parent = TYPE_DEVICE,
+ .instance_size = sizeof(LEDState),
+ .class_init = led_class_init
+};
+
+static void led_register_types(void)
+{
+ type_register_static(&led_info);
+}
+
+type_init(led_register_types)
+
+DeviceState *create_led(Object *parentobj,
+ LEDColor color,
+ const char *description,
+ uint16_t reset_intensity)
+{
+ DeviceState *dev;
+ char *name;
+
+ assert(description);
+ dev = qdev_new(TYPE_LED);
+ qdev_prop_set_uint16(dev, "reset_intensity", reset_intensity);
+ qdev_prop_set_string(dev, "color", led_color(color));
+ qdev_prop_set_string(dev, "description", description);
+ name = g_ascii_strdown(description, -1);
+ name = g_strdelimit(name, " #", '-');
+ object_property_add_child(parentobj, name, OBJECT(dev));
+ g_free(name);
+ qdev_realize_and_unref(dev, NULL, &error_fatal);
+
+ return dev;
+}
diff --git a/MAINTAINERS b/MAINTAINERS
index 955cc8dd5c..0fb8896b43 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1863,6 +1863,12 @@ F: docs/specs/vmgenid.txt
F: tests/qtest/vmgenid-test.c
F: stubs/vmgenid.c
+LED
+M: Philippe Mathieu-Daudé <f4bug@amsat.org>
+S: Maintained
+F: include/hw/misc/led.h
+F: hw/misc/led.c
+
Unimplemented device
M: Peter Maydell <peter.maydell@linaro.org>
R: Philippe Mathieu-Daudé <f4bug@amsat.org>
diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig
index bdd77d8020..f60dce694d 100644
--- a/hw/misc/Kconfig
+++ b/hw/misc/Kconfig
@@ -126,6 +126,9 @@ config AUX
config UNIMP
bool
+config LED
+ bool
+
config MAC_VIA
bool
select MOS6522
diff --git a/hw/misc/Makefile.objs b/hw/misc/Makefile.objs
index 5aaca8a039..9efa3c941c 100644
--- a/hw/misc/Makefile.objs
+++ b/hw/misc/Makefile.objs
@@ -91,3 +91,4 @@ common-obj-$(CONFIG_NRF51_SOC) += nrf51_rng.o
obj-$(CONFIG_MAC_VIA) += mac_via.o
common-obj-$(CONFIG_GRLIB) += grlib_ahb_apb_pnp.o
+common-obj-$(CONFIG_LED) += led.o
diff --git a/hw/misc/trace-events b/hw/misc/trace-events
index 805d2110e0..f58853d367 100644
--- a/hw/misc/trace-events
+++ b/hw/misc/trace-events
@@ -207,6 +207,9 @@ via1_rtc_cmd_pram_sect_write(int sector, int offset, int addr, int value) "secto
grlib_ahb_pnp_read(uint64_t addr, uint32_t value) "AHB PnP read addr:0x%03"PRIx64" data:0x%08x"
grlib_apb_pnp_read(uint64_t addr, uint32_t value) "APB PnP read addr:0x%03"PRIx64" data:0x%08x"
+# led.c
+led_set_intensity(const char *color, const char *desc, uint16_t intensity) "LED desc:'%s' color:%s intensity: 0x%04"PRIx16
+
# pca9552.c
pca9552_gpio_status(const char *description, const char *buf) "%s GPIOs 0-15 [%s]"
pca9552_gpio_change(const char *description, unsigned id, unsigned prev_state, unsigned current_state) "%s GPIO id:%u status: %u -> %u"
--
2.21.3
next prev parent reply other threads:[~2020-06-20 23:10 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-20 23:07 [PATCH v3 0/7] hw/misc: Add LED device Philippe Mathieu-Daudé
2020-06-20 23:07 ` Philippe Mathieu-Daudé [this message]
2020-06-21 2:00 ` [PATCH v3 1/7] hw/misc: Add a " Richard Henderson
2020-06-21 20:35 ` Philippe Mathieu-Daudé
2020-06-21 20:50 ` Richard Henderson
2020-06-20 23:07 ` [PATCH v3 2/7] hw/misc/led: Add helper to connect LED to GPIO output Philippe Mathieu-Daudé
2020-06-21 19:09 ` Richard Henderson
2020-06-20 23:07 ` [PATCH v3 3/7] hw/misc/led: Emit a trace event when LED intensity has changed Philippe Mathieu-Daudé
2020-06-21 19:23 ` Richard Henderson
2020-06-21 22:25 ` Philippe Mathieu-Daudé
2020-06-22 16:48 ` Richard Henderson
2020-06-23 7:45 ` Philippe Mathieu-Daudé
2020-06-20 23:07 ` [PATCH v3 4/7] hw/arm/aspeed: Add the 3 front LEDs drived by the PCA9552 #1 Philippe Mathieu-Daudé
2020-06-21 20:51 ` Richard Henderson
2020-06-20 23:07 ` [PATCH v3 5/7] hw/misc/mps2-fpgaio: Use the LED device Philippe Mathieu-Daudé
2020-06-21 21:00 ` Richard Henderson
2020-06-21 21:09 ` Philippe Mathieu-Daudé
2020-06-20 23:07 ` [PATCH v3 6/7] hw/misc/mps2-scc: " Philippe Mathieu-Daudé
2020-06-21 17:31 ` Philippe Mathieu-Daudé
2020-06-21 21:05 ` Richard Henderson
2020-06-20 23:07 ` [PATCH v3 7/7] hw/arm/tosa: Replace fprintf() calls by LED devices Philippe Mathieu-Daudé
2020-06-21 16:52 ` Philippe Mathieu-Daudé
2020-06-20 23:58 ` [PATCH v3 0/7] hw/misc: Add LED device no-reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200620230719.32139-2-f4bug@amsat.org \
--to=f4bug@amsat.org \
--cc=andrew@aj.id.au \
--cc=clg@kaod.org \
--cc=joel@jms.id.au \
--cc=peter.maydell@linaro.org \
--cc=philmd@redhat.com \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).