@@ -28,4 +28,18 @@ typedef struct LEDState {
uint8_t reset_state; /* TODO [GPIO_ACTIVE_LOW, GPIO_ACTIVE_HIGH] */
} LEDState;
+/**
+ * create_led_by_gpio_id: create and LED device
+ * @parent: the parent object
+ * @gpio_dev: device exporting GPIOs
+ * @gpio_id: GPIO ID of this LED
+ * @name: name of the LED
+ *
+ * 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,
+ const char *led_name);
+
#endif /* HW_MISC_LED_H */
@@ -104,3 +104,23 @@ static void led_register_types(void)
}
type_init(led_register_types)
+
+DeviceState *create_led_by_gpio_id(Object *parentobj,
+ DeviceState *gpio_dev, unsigned gpio_id,
+ const char *led_name)
+{
+ DeviceState *dev;
+ char *name;
+
+ dev = qdev_create(NULL, TYPE_LED);
+ /* TODO set "reset_state" */
+ qdev_prop_set_string(dev, "name", led_name);
+ name = g_ascii_strdown(led_name, -1);
+ name = g_strdelimit(name, " #", '-');
+ object_property_add_child(parentobj, name, OBJECT(dev));
+ g_free(name);
+ qdev_init_nofail(dev);
+ qdev_connect_gpio_out(gpio_dev, gpio_id, qdev_get_gpio_in(dev, 0));
+
+ return dev;
+}
Add create_led_by_gpio_id() to easily connect a LED to a GPIO output. Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org> --- include/hw/misc/led.h | 14 ++++++++++++++ hw/misc/led.c | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+)