From nobody Fri May 3 00:04:51 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1574057988; cv=none; d=zoho.com; s=zohoarc; b=W+tzQkNwdC2e6EXcTePL4sRPtaHyZdxAWKclZJsF2nSNkt4F1Sv3gHR8bMiNWYQ2+I7zMvi4S51OvgEiAAen+rAvq3JUOIBd9mp9a/6MulIRJY+XHZYoEDXnyo+juVlvOLYoQnnUO7LD4JgT7ZPeaHNOg9JGAhsBFNeEU9H4th4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1574057988; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=JykI96YRsis8dgCEg/shW1vRhmHTLEtMbhdBgs5yPxU=; b=kk8qSojywf1PNAZLuOjkf4EnrQbae0BYnY8KgNYHeOOyCbG9wsavxQAWUOvyZQJGlYnUoDliL9xYPyAdFNTEEY5GI3VctXRz1M5H0wgpITV8n4qLkGH2refYyaMmeimhmumsls2v+15y/J3Hv7JUlxM2PE+gMPY5YgRrGU02lXs= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 157405798871095.24585843934267; Sun, 17 Nov 2019 22:19:48 -0800 (PST) Received: from localhost ([::1]:58422 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWaO3-0007ki-8K for importer@patchew.org; Mon, 18 Nov 2019 01:19:47 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:36184) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWaMX-00071W-GM for qemu-devel@nongnu.org; Mon, 18 Nov 2019 01:18:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iWaMW-0005zE-0b for qemu-devel@nongnu.org; Mon, 18 Nov 2019 01:18:13 -0500 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]:35027) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iWaMS-0005u0-7k; Mon, 18 Nov 2019 01:18:08 -0500 Received: by mail-pg1-x542.google.com with SMTP id k32so3408275pgl.2; Sun, 17 Nov 2019 22:18:07 -0800 (PST) Received: from voyager.ibm.com ([36.255.48.244]) by smtp.gmail.com with ESMTPSA id u7sm16602843pjx.19.2019.11.17.22.18.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Nov 2019 22:18:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=JykI96YRsis8dgCEg/shW1vRhmHTLEtMbhdBgs5yPxU=; b=PePEmBFJ9ddf4opsptO/b+fwxkwxeQx887M9wvI3czpv8q+BQ9ytHFT/k4IqazdzCr pU387EzF0vxctAuhN3c+M/6TN/hc58v7YfNbeKhD1cYG6FzzC8GbDTkwtkPnwKTO1sWj L2j8cgO7G8HPkEgZHA8QzZwZdK4X+QJKkOBrFGUdf4vhN9s791p15gpqJty401ufZsnz 9wa0w5sbMwfADwPPDK1whap688Zs7nh9D8lAwFti0pIOWnU1bH/V4S86/djDxnmfUpHn p1GM3Pf05ci4V+IeQtQemQUa96MZ1Il99I15u4h5Tbn1fM0n9APUqMdgcvuQefeJC/tP oPPQ== 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 :mime-version:content-transfer-encoding; bh=JykI96YRsis8dgCEg/shW1vRhmHTLEtMbhdBgs5yPxU=; b=RicD7fpBKIDyQ7rdsnULBmQU7FNllJDiD3bZ/TuJ49EUqqsiTZLlL0mtm+7nyK2TeD yNkc8W7jorg7nQYS/PmALeZpLT2WTzdCKVTb0c6RiHKKRQXPjAjmQJUrlIyeZsZ3Kkaz k3l6JMORU0E6u9J5eHF+SAjT20e1d5e4fvLvLh34aKFZnQGtsaI9tdTl5J5ksg4Rpkmd yHFyDU4CLZZEsO4E+nGL1GR6qyKIcAcGZ4IlXb9PBCjNQRQa7VK3h+VABn5mc6G9Kqi/ IzyDx1crNqISDE81hNdZxO3CavqNd0C5xczD8FyazkIk5njX0l4IrT30xcxLDeMq+oID MF+g== X-Gm-Message-State: APjAAAX6IybGn2xGs5VedOuD/omhCB9vv8sCazdLCvcT6is3aajKWXT6 93HhZ9bdhG5YABtXn2O4liU= X-Google-Smtp-Source: APXvYqzKVrqppKp0vLUeWakdH558Eh9BqolQiKsIJmg2NuAUxLexjownvSXj8pJzcr+bO19NtFgUdQ== X-Received: by 2002:a63:7218:: with SMTP id n24mr19354968pgc.100.1574057885950; Sun, 17 Nov 2019 22:18:05 -0800 (PST) From: Joel Stanley To: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH] misc/pca9552: Add qom set and get Date: Mon, 18 Nov 2019 16:47:57 +1030 Message-Id: <20191118061757.52550-1-joel@jms.id.au> X-Mailer: git-send-email 2.24.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::542 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Jeffery , Peter Maydell , Rashmica Gupta , qemu-arm@nongnu.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Following the pattern of the work recently done with the ASPEED GPIO model, this adds support for inspecting and modifying the PCA9552 LEDs from the monitor. (qemu) qom-set /machine/unattached/device[17] led0 on (qemu) qom-get /machine/unattached/device[17] led0 "on" (qemu) qom-set /machine/unattached/device[17] led0 off (qemu) qom-get /machine/unattached/device[17] led0 "off" (qemu) qom-set /machine/unattached/device[17] led0 pwm0 (qemu) qom-get /machine/unattached/device[17] led0 "pwm0" (qemu) qom-set /machine/unattached/device[17] led0 pwm1 (qemu) qom-get /machine/unattached/device[17] led0 "pwm1" Signed-off-by: Joel Stanley --- The qom device in mainline qemu is a different path. Using the monitor examine `info qom-tree /machine/unattached/` to discover it. This can be tested with a Witherspoon image. $ wget https://openpower.xyz/job/openbmc-build/distro=3Dubuntu,label=3Dbuil= der,target=3Dwitherspoon/lastSuccessfulBuild/artifact/deploy/images/withers= poon/obmc-phosphor-image-witherspoon.ubi.mtd $ qemu-system-arm -M witherspoon-bmc -serial pty -monitor pty -nographic \ -drive file=3Dobmc-phosphor-image-witherspoon.ubi.mtd,format=3Draw,if=3Dmtd char device redirected to /dev/pts/5 (label compat_monitor0) char device redirected to /dev/pts/10 (label serial0) $ screen /dev/pts/5 QEMU 4.1.91 monitor - type 'help' for more information (qemu) qom-get /machine/unattached/device[17] led0 "off" $ screen /dev/pts/19 root@witherspoon:~# cd /sys/class/gpio/ root@witherspoon:/sys/class/gpio# echo 248 > export root@witherspoon:/sys/class/gpio# cat gpio248/value 0 (qemu) qom-set /machine/unattached/device[17] led0 on root@witherspoon:/sys/class/gpio# echo out > gpio248/direction root@witherspoon:/sys/class/gpio# cat gpio248/value 1 (qemu) qom-get /machine/unattached/device[17] led0 "on" (qemu) qom-set /machine/unattached/device[17] led0 off (qemu) qom-get /machine/unattached/device[17] led0 "off" root@witherspoon:/sys/class/gpio# cat gpio248/value 0 Signed-off-by: Joel Stanley --- hw/misc/pca9552.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/hw/misc/pca9552.c b/hw/misc/pca9552.c index 73be28d9369c..0362aac8c862 100644 --- a/hw/misc/pca9552.c +++ b/hw/misc/pca9552.c @@ -15,12 +15,16 @@ #include "hw/misc/pca9552.h" #include "hw/misc/pca9552_regs.h" #include "migration/vmstate.h" +#include "qapi/error.h" +#include "qapi/visitor.h" =20 #define PCA9552_LED_ON 0x0 #define PCA9552_LED_OFF 0x1 #define PCA9552_LED_PWM0 0x2 #define PCA9552_LED_PWM1 0x3 =20 +static const char *led_state[] =3D {"on", "off", "pwm0", "pwm1"}; + static uint8_t pca9552_pin_get_config(PCA9552State *s, int pin) { uint8_t reg =3D PCA9552_LS0 + (pin / 4); @@ -169,6 +173,84 @@ static int pca9552_event(I2CSlave *i2c, enum i2c_event= event) return 0; } =20 +static void pca9552_get_led(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + PCA9552State *s =3D PCA9552(obj); + int led, rc, reg; + char *str; + uint8_t state; + + rc =3D sscanf(name, "led%2d", &led); + if (rc !=3D 1) { + error_setg(errp, "%s: error reading %s", __func__, name); + return; + } + if (led < 0 || led > s->nr_leds) { + error_setg(errp, "%s invalid led %s", __func__, name); + return; + } + /* + * Get the LSx register as the qom interface should expose the device + * state, not the modeled 'input line' behaviour which would come from + * reading the INPUTx reg + */ + reg =3D PCA9552_LS0 + led / 4; + state =3D (pca9552_read(s, reg) >> (led % 8)) & 0x3; + str =3D g_strdup(led_state[state]); + visit_type_str(v, name, &str, errp); +} + +/* + * Return an LED selector register value based on an existing one, with + * the appropriate 2-bit state value set for the given LED number (0-3). + */ +static inline uint8_t pca955x_ledsel(uint8_t oldval, int led_num, int stat= e) +{ + return (oldval & (~(0x3 << (led_num << 1)))) | + ((state & 0x3) << (led_num << 1)); +} + +static void pca9552_set_led(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + PCA9552State *s =3D PCA9552(obj); + Error *local_err =3D NULL; + int led, rc, reg, val; + uint8_t state; + char *state_str; + + visit_type_str(v, name, &state_str, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + rc =3D sscanf(name, "led%2d", &led); + if (rc !=3D 1) { + error_setg(errp, "%s: error reading %s", __func__, name); + return; + } + if (led < 0 || led > s->nr_leds) { + error_setg(errp, "%s invalid led %s", __func__, name); + return; + } + + for (state =3D 0; state < ARRAY_SIZE(led_state); state++) { + if (!strcmp(state_str, led_state[state])) { + break; + } + } + if (state >=3D ARRAY_SIZE(led_state)) { + error_setg(errp, "%s invalid led state %s", __func__, state_str); + return; + } + + reg =3D PCA9552_LS0 + led / 4; + val =3D pca9552_read(s, reg); + val =3D pca955x_ledsel(val, led % 4, state); + pca9552_write(s, reg, val); +} + static const VMStateDescription pca9552_vmstate =3D { .name =3D "PCA9552", .version_id =3D 0, @@ -204,6 +286,7 @@ static void pca9552_reset(DeviceState *dev) static void pca9552_initfn(Object *obj) { PCA9552State *s =3D PCA9552(obj); + int led; =20 /* If support for the other PCA955X devices are implemented, these * constant values might be part of class structure describing the @@ -211,6 +294,14 @@ static void pca9552_initfn(Object *obj) */ s->max_reg =3D PCA9552_LS3; s->nr_leds =3D 16; + + for (led =3D 0; led < s->nr_leds; led++) { + char *name; + + name =3D g_strdup_printf("led%d", led); + object_property_add(obj, name, "bool", pca9552_get_led, pca9552_se= t_led, + NULL, NULL, NULL); + } } =20 static void pca9552_class_init(ObjectClass *klass, void *data) --=20 2.24.0