From nobody Mon Nov 17 12:05:10 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of _spf.google.com designates 209.85.208.44 as permitted sender) client-ip=209.85.208.44; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-ed1-f44.google.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.208.44 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=1603750001; cv=none; d=zohomail.com; s=zohoarc; b=JfPmL2+TkrgD0rbCvvjH4JULvtTx386KrUia2DufIB7kiFufi6/aS5ULy74r+FeWZ8IKYN+iqaWB0WYVtZTyTRZ5I0ilsUCONscCo1GKlkCDQsIViHQ4pgIKWf4ZZjXngtjnRk/4GRubIRVwBvW8plWJ2zEPbfaVaO8BL4DJhPw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1603750001; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Sender:Subject:To; bh=dMuCpvg2jvs8wv2yJweoNJ0vPwlnnXLBSkbONBKjyck=; b=iTwYMuY2rth+dpo7HAk73XTHtz/9YfYf1ZuRMDN9evpB/jZunGtoGCXYS0+O+76CvrThNynDBqCmGjD30/v1HqXA+wG98zDuZIjB34dXuf+aXWZr7QKod4/vF+LGdbrM3dcGnZV1eBXENwm9leCXrt/5RSfFJzyBrTxdClybecA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.208.44 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com; dmarc=fail header.from= (p=none dis=none) header.from= Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) by mx.zohomail.com with SMTPS id 1603750001047689.8114972293464; Mon, 26 Oct 2020 15:06:41 -0700 (PDT) Received: by mail-ed1-f44.google.com with SMTP id bc23so11284173edb.5 for ; Mon, 26 Oct 2020 15:06:40 -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 ld2sm1287166ejb.94.2020.10.26.15.06.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Oct 2020 15:06:38 -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=dMuCpvg2jvs8wv2yJweoNJ0vPwlnnXLBSkbONBKjyck=; b=pe8NOjvIu85re1H6MPR7cdc2wMW0nE4BoXaIStxKCOtIxWGkSG4SzQE2nFNzLhe+/T IQ3qLhF5dKDYpWcXN7w3OrqBJsj0cPdHpUejFeo2yYgYLr/Jgb9NfAoFcdhakJ+/wFNm DwKdEhA2IVoRZtvOhp/SENUmj97S+EyFJJW1Za7zF94QO+qavku0dB9xkvQK+6zDvpf8 NOELvlcwA6tbzohmrhq+wIDYCxvlXmiCqqDYrllklV3AmodWRGB/jpZwrBMerQT/rbeu dImPwz22RIqpq/3B4YEmjxMT99YasItIvrmT+Yn08p8lRHIohGcA6nmvFu8Mx3/vpyK4 r4rw== 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=dMuCpvg2jvs8wv2yJweoNJ0vPwlnnXLBSkbONBKjyck=; b=HFcZJPPKPUFH6L0IJBhmNpW8pnQpBL+qw9Y633hWHf2om6QLacHuvoHhfEmrMxLY9F SkUQwZauGW3G1fUZB4A/nEGVohFiy0BT/6KyypOnv/6mN1qMc8dg8hIYIAeQXXnQRpcp 1jo9snJC4Fn2aiyyuT27P4ikIQKdQ5zX9HDk8FItjPtBHpCBiPJlfgTBJ1hTiKpFzuLz OLBQNJIsn4gOqWlvPCutS69O/nxOW7DBjN3399534X8uJn+jhzurNv4I8LJe9nWHTgDV hrnIFtkXyx79wlBkJqk7z+K1QOPrYw7Qs33RpdVIhRT+Id+NFn8gMDmBWz+/nPj9+kPo g+Kg== X-Gm-Message-State: AOAM530iGcF1onIl8Tp6EC0T2Ly1vNo/ByG93g39EDETbqXF8+At7PhG IEHW9Iw2G/GRXCYvOO/VgrY= X-Google-Smtp-Source: ABdhPJz1PqOnu3wNrP++2R0bWoY3NBBwfni/CvOyxaZZ8CSzGWD6P+Rj7Wkv3ZueE6dHJr4SFuA+TA== X-Received: by 2002:aa7:cd14:: with SMTP id b20mr17950877edw.366.1603749999128; Mon, 26 Oct 2020 15:06:39 -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 , Richard Henderson , Luc Michel Subject: [PULL 2/7] hw/misc/led: Allow connecting from GPIO output Date: Mon, 26 Oct 2020 23:06:19 +0100 Message-Id: <20201026220624.60878-3-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) Some devices expose GPIO lines. Add a GPIO qdev input to our LED device, so we can connect a GPIO output using qdev_connect_gpio_out(). When used with GPIOs, the intensity can only be either minium or maximum. This depends of the polarity of the GPIO (which can be inverted). Declare the GpioPolarity type to model the polarity. Signed-off-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Richard Henderson Reviewed-by: Luc Michel Message-Id: <20200912134041.946260-3-f4bug@amsat.org> --- include/hw/misc/led.h | 10 ++++++++++ include/hw/qdev-core.h | 16 ++++++++++++++++ hw/misc/led.c | 17 ++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/include/hw/misc/led.h b/include/hw/misc/led.h index 286d37c75c1..aa359b87c20 100644 --- a/include/hw/misc/led.h +++ b/include/hw/misc/led.h @@ -9,6 +9,7 @@ #define HW_MISC_LED_H =20 #include "qom/object.h" +#include "hw/qdev-core.h" =20 #define TYPE_LED "led" =20 @@ -37,10 +38,17 @@ struct LEDState { /* Public */ =20 uint8_t intensity_percent; + qemu_irq irq; =20 /* Properties */ char *description; char *color; + /* + * Determines whether a GPIO is using a positive (active-high) + * logic (when used with GPIO, the intensity at reset is related + * to the GPIO polarity). + */ + bool gpio_active_high; }; typedef struct LEDState LEDState; DECLARE_INSTANCE_CHECKER(LEDState, LED, TYPE_LED) @@ -72,6 +80,7 @@ void led_set_state(LEDState *s, bool is_emitting); /** * led_create_simple: Create and realize a LED device * @parentobj: the parent object + * @gpio_polarity: GPIO polarity * @color: color of the LED * @description: description of the LED (optional) * @@ -81,6 +90,7 @@ void led_set_state(LEDState *s, bool is_emitting); * Returns: The newly allocated and instantiated LED object. */ LEDState *led_create_simple(Object *parentobj, + GpioPolarity gpio_polarity, LEDColor color, const char *description); =20 diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 868973319ee..a653295d6fc 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -443,6 +443,22 @@ void qdev_simple_device_unplug_cb(HotplugHandler *hotp= lug_dev, void qdev_machine_creation_done(void); bool qdev_machine_modified(void); =20 +/** + * GpioPolarity: Polarity of a GPIO line + * + * GPIO lines use either positive (active-high) logic, + * or negative (active-low) logic. + * + * In active-high logic (%GPIO_POLARITY_ACTIVE_HIGH), a pin is + * active when the voltage on the pin is high (relative to ground); + * whereas in active-low logic (%GPIO_POLARITY_ACTIVE_LOW), a pin + * is active when the voltage on the pin is low (or grounded). + */ +typedef enum { + GPIO_POLARITY_ACTIVE_LOW, + GPIO_POLARITY_ACTIVE_HIGH +} GpioPolarity; + /** * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines * @dev: Device whose GPIO we want diff --git a/hw/misc/led.c b/hw/misc/led.c index 1e2f49c5710..c5fa09a613a 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" =20 #define LED_INTENSITY_PERCENT_MAX 100 @@ -53,11 +54,19 @@ void led_set_state(LEDState *s, bool is_emitting) led_set_intensity(s, is_emitting ? LED_INTENSITY_PERCENT_MAX : 0); } =20 +static void led_set_state_gpio_handler(void *opaque, int line, int new_sta= te) +{ + LEDState *s =3D LED(opaque); + + assert(line =3D=3D 0); + led_set_state(s, !!new_state !=3D s->gpio_active_high); +} + static void led_reset(DeviceState *dev) { LEDState *s =3D LED(dev); =20 - led_set_state(s, false); + led_set_state(s, s->gpio_active_high); } =20 static const VMStateDescription vmstate_led =3D { @@ -84,11 +93,14 @@ static void led_realize(DeviceState *dev, Error **errp) if (s->description =3D=3D NULL) { s->description =3D g_strdup("n/a"); } + + qdev_init_gpio_in(DEVICE(s), led_set_state_gpio_handler, 1); } =20 static Property led_properties[] =3D { DEFINE_PROP_STRING("color", LEDState, color), DEFINE_PROP_STRING("description", LEDState, description), + DEFINE_PROP_BOOL("gpio-active-high", LEDState, gpio_active_high, true), DEFINE_PROP_END_OF_LIST(), }; =20 @@ -119,6 +131,7 @@ static void led_register_types(void) type_init(led_register_types) =20 LEDState *led_create_simple(Object *parentobj, + GpioPolarity gpio_polarity, LEDColor color, const char *description) { @@ -126,6 +139,8 @@ LEDState *led_create_simple(Object *parentobj, DeviceState *dev; =20 dev =3D qdev_new(TYPE_LED); + qdev_prop_set_bit(dev, "gpio-active-high", + gpio_polarity =3D=3D GPIO_POLARITY_ACTIVE_HIGH); qdev_prop_set_string(dev, "color", led_color_name[color]); if (!description) { static unsigned undescribed_led_id; --=20 2.26.2