From nobody Tue Feb 10 20:48:59 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of _spf.google.com designates 209.85.218.65 as permitted sender) client-ip=209.85.218.65; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-ej1-f65.google.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.218.65 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1599496384; cv=none; d=zohomail.com; s=zohoarc; b=EiCF0/U2moFdOmF+YMsH+NUb+H0+5eH6sookFeUZ9BdFpjrb5xLvfcsvA5uGgFJgET68IKnxn40YAJJ2hwlstDsB0CeeBc6cjOjlcPQ09fTL5dQZx8aLJQpBABSjnfEHQSKQtoN6dNYJKL7N3QOa9lyLAve8p42f2TyRLtH5714= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599496384; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Sender:Subject:To; bh=VNbcpVtna5bpU7n81cix+fetWM20QpCF3F6nhCiudhQ=; b=CvuQYp/dYAwpoH2aoHzce3S5HND9fJqcoN+pNy2k0AAdYQUAgaVr8/hwL44kZwZHX/yw/dA7AP2Hbr78+4IJsOP9uxRGTVZaXnmEfwfw5p5pwTgR0VGp51n0KL5qkU9nowxt0EKY0gO+VCVYGgGsMNX6EcPNRS5OFIFlxE0HI9k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.218.65 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com Received: from mail-ej1-f65.google.com (mail-ej1-f65.google.com [209.85.218.65]) by mx.zohomail.com with SMTPS id 1599496384860885.3989967204217; Mon, 7 Sep 2020 09:33:04 -0700 (PDT) Received: by mail-ej1-f65.google.com with SMTP id a26so18971568ejc.2 for ; Mon, 07 Sep 2020 09:33:04 -0700 (PDT) Return-Path: Return-Path: Received: from x1w.redhat.com (65.red-83-57-170.dynamicip.rima-tde.net. [83.57.170.65]) by smtp.gmail.com with ESMTPSA id u13sm15700199ejn.82.2020.09.07.09.33.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Sep 2020 09:33:02 -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=VNbcpVtna5bpU7n81cix+fetWM20QpCF3F6nhCiudhQ=; b=Lsra9HzP9MY8fC6amVNSnK9RIttSVRIDNAo7ZVenbXPeC+d7FLhru2Larq7F0cwxH9 7A+1x5dtEHr7d7X8o+moA6ydLEfoLQqdWvDjl9Kfr2Jf0WEA08VsQbSXsul5VP4f4WBz 5wO80IO5hMpD7iGTf+HkodI05Ua3cKKD5Ta3eG+HyCX7ZPo+sb7xQjR/lGRUsyP1sgCn RaA+8spjY4Xc1aE1pY6zEFS+6NZZP76V8o4bOMdzvnXgltI7KtodTz4wJxEDAg0oxqUp UzH0WDeBNBtX7N3HRP7NJTPa7H6EKVF2rMQGib4ZiGq0PUA7CyKtjr8HIMXhjymB6io2 9UWg== 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=VNbcpVtna5bpU7n81cix+fetWM20QpCF3F6nhCiudhQ=; b=OQjOBXFfGSEGbt/dE5Lni4i+QS3fQU/PlctDGoU5BziHdWRGAoG8IAlB+H0rGsrYpt jKbtXX3fzIgIxIiBzbujtcdgs+jyVfJa+EazuSYdjLXZ4W9woyqkPgIosT6ACa8zkoFu EkuDpxYB62+HWVIFiZK3GeVW+njAGvv5YmySe5D8HyJAdTTC20neyySC7fbNmyogMI3d fiyOtoFL3jNZemgkORRwMszYuT+5yBP/Wxq4HtloerI2/1d3iO269+zLHgwLaT1YcWgD VJxKKhLS3a6VFCmOLVfJGV9wlNufUAyiM7sKtbTgj7cu/LKxivXF9jUx/9raliFqljQN SDsQ== X-Gm-Message-State: AOAM533KMwZzklypN7YUKAbh7IyU1Dkfviw/GTjiPfBwqnSa1+8j4uH0 E/SehZnpYJo3xh4Y3GXiBtM= X-Google-Smtp-Source: ABdhPJxGLaRMjJjguE+3rJHPw8QpT0NRuDthkCZFOR88+Gn0lmfnBM2KpBc+w/J/zRInktHy4AP+GA== X-Received: by 2002:a17:906:2552:: with SMTP id j18mr21262657ejb.476.1599496382901; Mon, 07 Sep 2020 09:33:02 -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: Andrzej Zaborowski , Peter Maydell , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Luc Michel , qemu-arm@nongnu.org, Andrew Jeffery , Joaquin de Andres , Joel Stanley , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eduardo Habkost Subject: [PATCH v4 2/8] hw/misc/led: Allow connecting from GPIO output Date: Mon, 7 Sep 2020 18:32:51 +0200 Message-Id: <20200907163257.46527-3-f4bug@amsat.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200907163257.46527-1-f4bug@amsat.org> References: <20200907163257.46527-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 --- include/hw/misc/led.h | 8 ++++++++ include/hw/qdev-core.h | 8 ++++++++ hw/misc/led.c | 17 ++++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/include/hw/misc/led.h b/include/hw/misc/led.h index 1aaabbebafc..c8dd6da74d5 100644 --- a/include/hw/misc/led.h +++ b/include/hw/misc/led.h @@ -38,10 +38,16 @@ typedef struct LEDState { /* Public */ =20 uint8_t intensity_percent; + qemu_irq irq; =20 /* Properties */ char *description; char *color; + /* + * When used with GPIO, the intensity at reset is related + * to the GPIO polarity. + */ + bool inverted_polarity; } LEDState; =20 /** @@ -71,6 +77,7 @@ void led_set_state(LEDState *s, bool is_emitting); /** * led_create_simple: Create and realize a LED device * @parent: the parent object + * @gpio_polarity: GPIO polarity * @color: color of the LED * @description: description of the LED (optional) * @@ -78,6 +85,7 @@ void led_set_state(LEDState *s, bool is_emitting); * drop the reference to it (the device is realized). */ 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 ea3f73a282d..846354736a5 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -424,6 +424,14 @@ 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 + */ +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 f2140739b68..1acade1d592 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->inverted_polarity); +} + static void led_reset(DeviceState *dev) { LEDState *s =3D LED(dev); =20 - led_set_state(s, false); + led_set_state(s, s->inverted_polarity); } =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("polarity-inverted", LEDState, inverted_polarity, fal= se), 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, "polarity-inverted", + gpio_polarity =3D=3D GPIO_POLARITY_ACTIVE_LOW); qdev_prop_set_string(dev, "color", led_color_name[color]); if (!description) { static unsigned undescribed_led_id; --=20 2.26.2