From nobody Mon Nov 17 12:05:11 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of _spf.google.com designates 209.85.208.67 as permitted sender) client-ip=209.85.208.67; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-ed1-f67.google.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.208.67 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com; dmarc=fail(p=none dis=none) header.from=amsat.org ARC-Seal: i=1; a=rsa-sha256; t=1603749995; cv=none; d=zohomail.com; s=zohoarc; b=OVJqrh4pFyFOi09p9imWi6mcNn+ldCXwbCj+ZKvReKDthrmllr4QficuRAVggAgX0EQMhVQBOZlaBREI07BwdbFHOV6nEuxRkRw2CDB4ZQ5dq9NPQkVCvNwVV0yFRIaZyybq5icDzRyMJd65OI3B2GwBs1UIPdY8U1qVNoVdfdk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1603749995; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Sender:Subject:To; bh=rp+PT0DKslfkK4k99N9IovK0uPHrIUK2nuMArbsAM+E=; b=A+kjWf/8pbxXfxOdLuPXXFL/53bdLVXFpTPS6ausJ+mEJyOJpECiHgdcx58GHxoQlckFSg6edlpeAg1jZKEda8eUAkP+ZczMJMz4liw2l0U5YKNuSLrSbSKj55/TbuRCL9V77eKycOPLeTVGtZlWQyJEv0+ROPHKOtPjg9Aiz+g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.208.67 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com; dmarc=fail header.from= (p=none dis=none) header.from= Received: from mail-ed1-f67.google.com (mail-ed1-f67.google.com [209.85.208.67]) by mx.zohomail.com with SMTPS id 1603749995293494.223804821144; Mon, 26 Oct 2020 15:06:35 -0700 (PDT) Received: by mail-ed1-f67.google.com with SMTP id bc23so11283877edb.5 for ; Mon, 26 Oct 2020 15:06:34 -0700 (PDT) Return-Path: Return-Path: Received: from x1w.redhat.com (237.red-88-18-140.staticip.rima-tde.net. [88.18.140.237]) by smtp.gmail.com with ESMTPSA id ao17sm6696333ejc.18.2020.10.26.15.06.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Oct 2020 15:06:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rp+PT0DKslfkK4k99N9IovK0uPHrIUK2nuMArbsAM+E=; b=qz1tu9XjbMz5jQTmKqvIerLy11A/omROPUfKyVmtCtuVCvcAy8x7yVO85WvocIrNx+ /128MWnsyb5SHxtGV5T5Zgkaaadwoq+W0RYmCiiL6AzrYQpOI+vvYR4h4Q1wyH5540l5 t0dwyFYZVJr5l5w3Ggy11bmVF7ju7tEYc/oHtH0LhTVwS0SNqXxLEDWBgd3txj9zhyBP noYz9WrW2QvocPuyNimbpMpkE0E09CaNrU5iQfuoeU0RY/bNfny9HoH8TcukeH5y7lX7 G/ZEljfnvtjMpXp9xWXffGY+xXaX/u4eLZav+925RX5aRMfEVGWtPra6Qksx6b0AdL4h 63Kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=rp+PT0DKslfkK4k99N9IovK0uPHrIUK2nuMArbsAM+E=; b=DQAI66irFnuE5jEJnTALM+5B/EOE+R6IddaJWTBU8k3kPfOEhIZ4RB4IUQ5VCUqz6O kwQSRFIM37bM3YZNFkb354F6rT7LoBHGeI60RBRdLusKJhWsZIue3bXq+n8T3eHcePtB l+wpixrU8GFqq3ZuXKpSzckUkoEO6hOSJsdBg/7dxGCosjz79PsUQakA5i4VdQKJdzVC vdHk/VzGCn0nYH07m+hxwNCaHmYpIxRR9tASkMCiLjn0tyXXAXUbyRD+CU7iERZfEaXo Eyc7GoHDYfEgTDfusqxws5KLVxv9QUlzflqMK2IEPqrVyOXYeHCiFLd2J+U8wwXEsYeV pvFQ== X-Gm-Message-State: AOAM530gv5PJai5VprQuzjaIjxhcENjSPUKd4FIxKvY/ngEZKRGGam7i ZZ/xHtMcCRKqPWNd00+qVHg= X-Google-Smtp-Source: ABdhPJyFYECewczaZ6w3tXITbgzxryHLD/LrkanichJCdHInM0QSjuG4Z5mGp6ChlcZGffnv4yWX5Q== X-Received: by 2002:a05:6402:143d:: with SMTP id c29mr12995229edx.277.1603749993173; Mon, 26 Oct 2020 15:06:33 -0700 (PDT) Sender: =?UTF-8?Q?Philippe_Mathieu=2DDaud=C3=A9?= From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Andrew Jeffery , Joel Stanley , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Andrzej Zaborowski , qemu-arm@nongnu.org, Paolo Bonzini , Peter Maydell , Luc Michel , Richard Henderson Subject: [PULL 1/7] hw/misc/led: Add a LED device Date: Mon, 26 Oct 2020 23:06:18 +0100 Message-Id: <20201026220624.60878-2-f4bug@amsat.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201026220624.60878-1-f4bug@amsat.org> References: <20201026220624.60878-1-f4bug@amsat.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) Add a LED device which can be connected to a GPIO output. 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. LEDs are limited to a fixed set of colors. Reviewed-by: Luc Michel Reviewed-by: Richard Henderson Signed-off-by: Philippe Mathieu-Daud=C3=A9 Message-Id: <20200912134041.946260-2-f4bug@amsat.org> --- include/hw/misc/led.h | 87 ++++++++++++++++++++++++++ hw/misc/led.c | 142 ++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 6 ++ hw/misc/Kconfig | 3 + hw/misc/meson.build | 1 + hw/misc/trace-events | 3 + 6 files changed, 242 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 00000000000..286d37c75c1 --- /dev/null +++ b/include/hw/misc/led.h @@ -0,0 +1,87 @@ +/* + * QEMU single LED device + * + * Copyright (C) 2020 Philippe Mathieu-Daud=C3=A9 + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef HW_MISC_LED_H +#define HW_MISC_LED_H + +#include "qom/object.h" + +#define TYPE_LED "led" + +/** + * LEDColor: Color of a LED + * + * This set is restricted to physically available LED colors. + * + * LED colors from 'Table 1. Product performance of LUXEON Rebel Color + * Line' of the 'DS68 LUXEON Rebel Color Line' datasheet available at: + * https://www.lumileds.com/products/color-leds/luxeon-rebel-color/ + */ +typedef enum { /* Coarse wavelength range */ + LED_COLOR_VIOLET, /* 425 nm */ + LED_COLOR_BLUE, /* 475 nm */ + LED_COLOR_CYAN, /* 500 nm */ + LED_COLOR_GREEN, /* 535 nm */ + LED_COLOR_AMBER, /* 590 nm */ + LED_COLOR_ORANGE, /* 615 nm */ + LED_COLOR_RED, /* 630 nm */ +} LEDColor; + +struct LEDState { + /* Private */ + DeviceState parent_obj; + /* Public */ + + uint8_t intensity_percent; + + /* Properties */ + char *description; + char *color; +}; +typedef struct LEDState LEDState; +DECLARE_INSTANCE_CHECKER(LEDState, LED, TYPE_LED) + +/** + * led_set_intensity: Set the intensity of a LED device + * @s: the LED object + * @intensity_percent: intensity as percentage in range 0 to 100. + */ +void led_set_intensity(LEDState *s, unsigned intensity_percent); + +/** + * led_get_intensity: + * @s: the LED object + * + * Returns: The LED intensity as percentage in range 0 to 100. + */ +unsigned led_get_intensity(LEDState *s); + +/** + * led_set_state: Set the state of a LED device + * @s: the LED object + * @is_emitting: boolean indicating whether the LED is emitting + * + * This utility is meant for LED connected to GPIO. + */ +void led_set_state(LEDState *s, bool is_emitting); + +/** + * led_create_simple: Create and realize a LED device + * @parentobj: the parent object + * @color: color of the LED + * @description: description of the LED (optional) + * + * Create the device state structure, initialize it, and + * drop the reference to it (the device is realized). + * + * Returns: The newly allocated and instantiated LED object. + */ +LEDState *led_create_simple(Object *parentobj, + LEDColor color, + const char *description); + +#endif /* HW_MISC_LED_H */ diff --git a/hw/misc/led.c b/hw/misc/led.c new file mode 100644 index 00000000000..1e2f49c5710 --- /dev/null +++ b/hw/misc/led.c @@ -0,0 +1,142 @@ +/* + * QEMU single LED device + * + * Copyright (C) 2020 Philippe Mathieu-Daud=C3=A9 + * + * 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" + +#define LED_INTENSITY_PERCENT_MAX 100 + +static const char * const led_color_name[] =3D { + [LED_COLOR_VIOLET] =3D "violet", + [LED_COLOR_BLUE] =3D "blue", + [LED_COLOR_CYAN] =3D "cyan", + [LED_COLOR_GREEN] =3D "green", + [LED_COLOR_AMBER] =3D "amber", + [LED_COLOR_ORANGE] =3D "orange", + [LED_COLOR_RED] =3D "red", +}; + +static bool led_color_name_is_valid(const char *color_name) +{ + for (size_t i =3D 0; i < ARRAY_SIZE(led_color_name); i++) { + if (strcmp(color_name, led_color_name[i]) =3D=3D 0) { + return true; + } + } + return false; +} + +void led_set_intensity(LEDState *s, unsigned intensity_percent) +{ + if (intensity_percent > LED_INTENSITY_PERCENT_MAX) { + intensity_percent =3D LED_INTENSITY_PERCENT_MAX; + } + trace_led_set_intensity(s->description, s->color, intensity_percent); + s->intensity_percent =3D intensity_percent; +} + +unsigned led_get_intensity(LEDState *s) +{ + return s->intensity_percent; +} + +void led_set_state(LEDState *s, bool is_emitting) +{ + led_set_intensity(s, is_emitting ? LED_INTENSITY_PERCENT_MAX : 0); +} + +static void led_reset(DeviceState *dev) +{ + LEDState *s =3D LED(dev); + + led_set_state(s, false); +} + +static const VMStateDescription vmstate_led =3D { + .name =3D TYPE_LED, + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT8(intensity_percent, LEDState), + VMSTATE_END_OF_LIST() + } +}; + +static void led_realize(DeviceState *dev, Error **errp) +{ + LEDState *s =3D LED(dev); + + if (s->color =3D=3D NULL) { + error_setg(errp, "property 'color' not specified"); + return; + } else if (!led_color_name_is_valid(s->color)) { + error_setg(errp, "property 'color' invalid or not supported"); + return; + } + if (s->description =3D=3D NULL) { + s->description =3D g_strdup("n/a"); + } +} + +static Property led_properties[] =3D { + DEFINE_PROP_STRING("color", LEDState, color), + DEFINE_PROP_STRING("description", LEDState, description), + DEFINE_PROP_END_OF_LIST(), +}; + +static void led_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + + dc->desc =3D "LED"; + dc->vmsd =3D &vmstate_led; + dc->reset =3D led_reset; + dc->realize =3D led_realize; + set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories); + device_class_set_props(dc, led_properties); +} + +static const TypeInfo led_info =3D { + .name =3D TYPE_LED, + .parent =3D TYPE_DEVICE, + .instance_size =3D sizeof(LEDState), + .class_init =3D led_class_init +}; + +static void led_register_types(void) +{ + type_register_static(&led_info); +} + +type_init(led_register_types) + +LEDState *led_create_simple(Object *parentobj, + LEDColor color, + const char *description) +{ + g_autofree char *name =3D NULL; + DeviceState *dev; + + dev =3D qdev_new(TYPE_LED); + qdev_prop_set_string(dev, "color", led_color_name[color]); + if (!description) { + static unsigned undescribed_led_id; + name =3D g_strdup_printf("undescribed-led-#%u", undescribed_led_id= ++); + } else { + qdev_prop_set_string(dev, "description", description); + name =3D g_ascii_strdown(description, -1); + name =3D g_strdelimit(name, " #", '-'); + } + object_property_add_child(parentobj, name, OBJECT(dev)); + qdev_realize_and_unref(dev, NULL, &error_fatal); + + return LED(dev); +} diff --git a/MAINTAINERS b/MAINTAINERS index ef6f5c73998..7e442b52472 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1967,6 +1967,12 @@ F: docs/specs/vmgenid.txt F: tests/qtest/vmgenid-test.c F: stubs/vmgenid.c =20 +LED +M: Philippe Mathieu-Daud=C3=A9 +S: Maintained +F: include/hw/misc/led.h +F: hw/misc/led.c + Unimplemented device M: Peter Maydell R: Philippe Mathieu-Daud=C3=A9 diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig index 31854561105..e4d059591ad 100644 --- a/hw/misc/Kconfig +++ b/hw/misc/Kconfig @@ -126,6 +126,9 @@ config AUX config UNIMP bool =20 +config LED + bool + config MAC_VIA bool select MOS6522 diff --git a/hw/misc/meson.build b/hw/misc/meson.build index 793d45b1dc4..aa8ec3bca9b 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -11,6 +11,7 @@ softmmu_ss.add(when: 'CONFIG_TMP421', if_true: files('tmp421.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')) =20 # ARM devices softmmu_ss.add(when: 'CONFIG_PL310', if_true: files('arm_l2x0.c')) diff --git a/hw/misc/trace-events b/hw/misc/trace-events index 6054f9adf35..2b6310dbe4b 100644 --- a/hw/misc/trace-events +++ b/hw/misc/trace-events @@ -223,6 +223,9 @@ via1_adb_poll(uint8_t data, const char *vadbint, int st= atus, int index, int size 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" =20 +# led.c +led_set_intensity(const char *color, const char *desc, uint8_t intensity_p= ercent) "LED desc:'%s' color:%s intensity: %u%%" + # pca9552.c pca955x_gpio_status(const char *description, const char *buf) "%s GPIOs 0-= 15 [%s]" pca955x_gpio_change(const char *description, unsigned id, unsigned prev_st= ate, unsigned current_state) "%s GPIO id:%u status: %u -> %u" --=20 2.26.2