qemu-arm
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3 2/7] hw/misc/led: Add helper to connect LED to GPIO output


From: Philippe Mathieu-Daudé
Subject: [PATCH v3 2/7] hw/misc/led: Add helper to connect LED to GPIO output
Date: Sun, 21 Jun 2020 01:07:14 +0200

Some devices expose GPIO lines. Add the create_led_by_gpio_id()
helper to connect a LED to such GPIO.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
Adding support for named GPIO is trivial. We don't need it yet.
---
 include/hw/misc/led.h | 20 ++++++++++++++++++++
 hw/misc/led.c         | 25 +++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/include/hw/misc/led.h b/include/hw/misc/led.h
index 821ee1247d..883006bb8f 100644
--- a/include/hw/misc/led.h
+++ b/include/hw/misc/led.h
@@ -35,6 +35,8 @@ typedef struct LEDState {
     DeviceState parent_obj;
     /* Public */
 
+    qemu_irq irq;
+
     /* Properties */
     char *description;
     char *color;
@@ -76,4 +78,22 @@ DeviceState *create_led(Object *parentobj,
                         const char *description,
                         uint16_t reset_intensity);
 
+/**
+ * create_led_by_gpio_id: create and LED device and connect it to a GPIO output
+ * @parent: the parent object
+ * @gpio_dev: device exporting GPIOs
+ * @gpio_id: GPIO ID of this LED
+ * @color: color of the LED
+ * @description: description of the LED (optional)
+ * @reset_intensity: LED intensity at reset
+ *
+ * This utility function creates a LED and connects it to a
+ * GPIO exported by another device.
+ */
+DeviceState *create_led_by_gpio_id(Object *parentobj,
+                                   DeviceState *gpio_dev, unsigned gpio_id,
+                                   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
index e55ed7dbc4..8503dde777 100644
--- a/hw/misc/led.c
+++ b/hw/misc/led.c
@@ -10,6 +10,7 @@
 #include "migration/vmstate.h"
 #include "hw/qdev-properties.h"
 #include "hw/misc/led.h"
+#include "hw/irq.h"
 #include "trace.h"
 
 static const char *led_color(LEDColor color)
@@ -39,6 +40,14 @@ void led_set_state(LEDState *s, bool is_on)
     led_set_intensity(s, is_on ? UINT16_MAX : 0);
 }
 
+static void gpio_led_set(void *opaque, int line, int new_state)
+{
+    LEDState *s = LED(opaque);
+
+    assert(line == 0);
+    led_set_state(s, !!new_state);
+}
+
 static void led_reset(DeviceState *dev)
 {
     LEDState *s = LED(dev);
@@ -63,6 +72,8 @@ static void led_realize(DeviceState *dev, Error **errp)
         error_setg(errp, "property 'color' not specified");
         return;
     }
+
+    qdev_init_gpio_in(DEVICE(s), gpio_led_set, 1);
 }
 
 static Property led_properties[] = {
@@ -119,3 +130,17 @@ DeviceState *create_led(Object *parentobj,
 
     return dev;
 }
+
+DeviceState *create_led_by_gpio_id(Object *parentobj,
+                                   DeviceState *gpio_dev, unsigned gpio_id,
+                                   LEDColor color,
+                                   const char *description,
+                                   uint16_t reset_intensity)
+{
+    DeviceState *dev;
+
+    dev = create_led(parentobj, color, description, reset_intensity);
+    qdev_connect_gpio_out(gpio_dev, gpio_id, qdev_get_gpio_in(dev, 0));
+
+    return dev;
+}
-- 
2.21.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]