From nobody Tue Feb 10 14:32:14 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of _spf.google.com designates 209.85.221.67 as permitted sender) client-ip=209.85.221.67; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-wr1-f67.google.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.221.67 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1599918049; cv=none; d=zohomail.com; s=zohoarc; b=I3PDYBTrxq3FHf0Y9lHg4rIQIp+LgcKLm5zSTGEt1CFOGtFJ2dr8RZWiEfUnCx5piMonyHI63wvDYrp9FOwy83Q72nuoeehn2j81xFAp4og0fks4LYvoQb8L9khqJHHyIzxKQT8uth9/PwlEoDcY4+Hpw5Jv/6+Jca7yIEWAbvM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599918049; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Sender:Subject:To; bh=w8Eo5/7klXOolpStkbQL0lu8B2pZTUunJlMPyHZPBkE=; b=Fc1DEu6zDzVdhtyjnxO9l9Mhv4xyy/Ekis3tR1ZLtUKYEthSAStulzCBBfl2iJqCu/p263EfCmcz5avqjhpVRg1DsajzYGLAFb2qcRBa9M9A6ZdgI6RW+zzFq0wSOZ76YJtDrBCSS9c/2tyrys5Z2UzaKl289jRagDmpoB23YgQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.221.67 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by mx.zohomail.com with SMTPS id 1599918049273961.6736173575712; Sat, 12 Sep 2020 06:40:49 -0700 (PDT) Received: by mail-wr1-f67.google.com with SMTP id z1so14103992wrt.3 for ; Sat, 12 Sep 2020 06:40:48 -0700 (PDT) Return-Path: Return-Path: Received: from localhost.localdomain (65.red-83-57-170.dynamicip.rima-tde.net. [83.57.170.65]) by smtp.gmail.com with ESMTPSA id u66sm10309144wmg.44.2020.09.12.06.40.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Sep 2020 06:40:46 -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=w8Eo5/7klXOolpStkbQL0lu8B2pZTUunJlMPyHZPBkE=; b=uciR+5a5X9kHY5AYZgFmM7NhPz72lwUQmpehWGgPBVPbW1sxTyBC17FhEFSbfsadde Zv7LFOoe82E/xIKzAtxSVdbFCCYyf1k9A08g7vnnbZ12O004xEUG7paM5skUrWn6EFq/ f0BkNYYu6hMfj6pOM+mXd/rfsx0Wp7swL8CqGJmd8MWaCN3ce9UB38res5wB6TNFuFhM u61wsgZ2OmqeVyI6EcT2KRQ9FXvqhhj3KZLQ8BS46g3k8VHBlCD2dupHjNOfCAQjFx4v gkncRIMlVAs13S2oFqZjYhnb5NZq4TU/PJX70MJVDqqVgpk9CWqn3c5UQkPdZmE+cHrF z+VA== 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=w8Eo5/7klXOolpStkbQL0lu8B2pZTUunJlMPyHZPBkE=; b=dzCDv6peue/mXrRJGn4Uz31xquJMPJB+4vpzIIjiHA16z8dPiCYpSRqB7BFn69pS9T 6NcHtFJLUTQIdB21/u7Cmp9VU5VTnsxbsBRBJEJ1fDe55DxjfmMhZJCxj6r6LGcu2v9M J7PY+KjvyY1VJttdx8N1bJ4ErfuX58JvWnntp0jZTeaRbLLmow5YSJU+uqanJsTB7Hfx OC6h0viZ3z65AMyIfj5c4dBHHgCmsUSKwYc9J05f48VFLi623FLNOoC82PIu/neNG7/D gijr+jbqLsi2BfM1xcy6nqnS9LhawdjKKJFop8udTHtV8my4hGdad0yru5Cd9q0ODFxu H35A== X-Gm-Message-State: AOAM533qp6ExYc17GNb/voBlcYlleCBg9TrfzueoHcC2K0d65855Kbnb Z5eL2VC+1QeN83J80fPDsZA= X-Google-Smtp-Source: ABdhPJxRA+UTEeWLXheVHVA4YGt++jvqhZ+BJpD3sXZWTdvLfh57eeW6G/nyVzRtyfGiG5A2dWg2Lw== X-Received: by 2002:adf:f586:: with SMTP id f6mr6852900wro.299.1599918047130; Sat, 12 Sep 2020 06:40:47 -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: Andrew Jeffery , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Paolo Bonzini , Joel Stanley , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Peter Maydell , Luc Michel , Andrzej Zaborowski , Eduardo Habkost , qemu-arm@nongnu.org Subject: [PATCH v6 2/7] hw/misc/led: Allow connecting from GPIO output Date: Sat, 12 Sep 2020 15:40:36 +0200 Message-Id: <20200912134041.946260-3-f4bug@amsat.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200912134041.946260-1-f4bug@amsat.org> References: <20200912134041.946260-1-f4bug@amsat.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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: Luc Michel Reviewed-by: Richard Henderson --- 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 e025ba9653f..ec1f1efc37b 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -423,6 +423,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