From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: qemu-devel@nongnu.org
Cc: qemu-ppc@nongnu.org, qemu-arm@nongnu.org, qemu-block@nongnu.org,
"Dmitriy Sharikhin" <d.sharikhin@yadro.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Paolo Bonzini" <pbonzini@redhat.com>
Subject: [PULL 11/13] hw/gpio: introduce pcf8574 driver
Date: Tue, 12 Mar 2024 09:22:36 +0100 [thread overview]
Message-ID: <20240312082239.69696-12-philmd@linaro.org> (raw)
In-Reply-To: <20240312082239.69696-1-philmd@linaro.org>
From: Dmitriy Sharikhin <d.sharikhin@yadro.com>
NXP PCF8574 and compatible ICs are simple I2C GPIO expanders.
PCF8574 incorporates quasi-bidirectional IO, and simple
communication protocol, when IO read is I2C byte read, and
IO write is I2C byte write. User can think of it as
open-drain port, when line high state is input and line low
state is output.
Signed-off-by: Dmitrii Sharikhin <d.sharikhin@yadro.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-ID: <f1552d822276e878d84c01eba2cf2c7c9ebdde00.camel@yadro.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
MAINTAINERS | 6 ++
include/hw/gpio/pcf8574.h | 15 ++++
hw/gpio/pcf8574.c | 162 ++++++++++++++++++++++++++++++++++++++
hw/gpio/Kconfig | 4 +
hw/gpio/meson.build | 1 +
5 files changed, 188 insertions(+)
create mode 100644 include/hw/gpio/pcf8574.h
create mode 100644 hw/gpio/pcf8574.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 4d96f855de..72c23e3682 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2503,6 +2503,12 @@ S: Maintained
F: hw/i2c/i2c_mux_pca954x.c
F: include/hw/i2c/i2c_mux_pca954x.h
+pcf8574
+M: Dmitrii Sharikhin <d.sharikhin@yadro.com>
+S: Maintained
+F: hw/gpio/pcf8574.c
+F: include/gpio/pcf8574.h
+
Generic Loader
M: Alistair Francis <alistair@alistair23.me>
S: Maintained
diff --git a/include/hw/gpio/pcf8574.h b/include/hw/gpio/pcf8574.h
new file mode 100644
index 0000000000..3291d7dbbc
--- /dev/null
+++ b/include/hw/gpio/pcf8574.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/*
+ * NXP PCF8574 8-port I2C GPIO expansion chip.
+ *
+ * Copyright (c) 2024 KNS Group (YADRO).
+ * Written by Dmitrii Sharikhin <d.sharikhin@yadro.com>
+ */
+
+#ifndef _HW_GPIO_PCF8574
+#define _HW_GPIO_PCF8574
+
+#define TYPE_PCF8574 "pcf8574"
+
+#endif /* _HW_GPIO_PCF8574 */
diff --git a/hw/gpio/pcf8574.c b/hw/gpio/pcf8574.c
new file mode 100644
index 0000000000..d37909e2ad
--- /dev/null
+++ b/hw/gpio/pcf8574.c
@@ -0,0 +1,162 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/*
+ * NXP PCF8574 8-port I2C GPIO expansion chip.
+ * Copyright (c) 2024 KNS Group (YADRO).
+ * Written by Dmitrii Sharikhin <d.sharikhin@yadro.com>
+ */
+
+#include "qemu/osdep.h"
+#include "hw/i2c/i2c.h"
+#include "hw/gpio/pcf8574.h"
+#include "hw/irq.h"
+#include "migration/vmstate.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+#include "qom/object.h"
+
+/*
+ * PCF8574 and compatible chips incorporate quasi-bidirectional
+ * IO. Electrically it means that device sustain pull-up to line
+ * unless IO port is configured as output _and_ driven low.
+ *
+ * IO access is implemented as simple I2C single-byte read
+ * or write operation. So, to configure line to input user write 1
+ * to corresponding bit. To configure line to output and drive it low
+ * user write 0 to corresponding bit.
+ *
+ * In essence, user can think of quasi-bidirectional IO as
+ * open-drain line, except presence of builtin rising edge acceleration
+ * embedded in PCF8574 IC
+ *
+ * PCF8574 has interrupt request line, which is being pulled down when
+ * port line state differs from last read. Port read operation clears
+ * state and INT line returns to high state via pullup.
+ */
+
+OBJECT_DECLARE_SIMPLE_TYPE(PCF8574State, PCF8574)
+
+#define PORTS_COUNT (8)
+
+struct PCF8574State {
+ I2CSlave parent_obj;
+ uint8_t lastrq; /* Last requested state. If changed - assert irq */
+ uint8_t input; /* external electrical line state */
+ uint8_t output; /* Pull-up (1) or drive low (0) on bit */
+ qemu_irq handler[PORTS_COUNT];
+ qemu_irq intrq; /* External irq request */
+};
+
+static void pcf8574_reset(DeviceState *dev)
+{
+ PCF8574State *s = PCF8574(dev);
+ s->lastrq = MAKE_64BIT_MASK(0, PORTS_COUNT);
+ s->input = MAKE_64BIT_MASK(0, PORTS_COUNT);
+ s->output = MAKE_64BIT_MASK(0, PORTS_COUNT);
+}
+
+static inline uint8_t pcf8574_line_state(PCF8574State *s)
+{
+ /* we driving line low or external circuit does that */
+ return s->input & s->output;
+}
+
+static uint8_t pcf8574_rx(I2CSlave *i2c)
+{
+ PCF8574State *s = PCF8574(i2c);
+ uint8_t linestate = pcf8574_line_state(s);
+ if (s->lastrq != linestate) {
+ s->lastrq = linestate;
+ if (s->intrq) {
+ qemu_set_irq(s->intrq, 1);
+ }
+ }
+ return linestate;
+}
+
+static int pcf8574_tx(I2CSlave *i2c, uint8_t data)
+{
+ PCF8574State *s = PCF8574(i2c);
+ uint8_t prev;
+ uint8_t diff;
+ uint8_t actual;
+ int line = 0;
+
+ prev = pcf8574_line_state(s);
+ s->output = data;
+ actual = pcf8574_line_state(s);
+
+ for (diff = (actual ^ prev); diff; diff &= ~(1 << line)) {
+ line = ctz32(diff);
+ if (s->handler[line]) {
+ qemu_set_irq(s->handler[line], (actual >> line) & 1);
+ }
+ }
+
+ if (s->intrq) {
+ qemu_set_irq(s->intrq, actual == s->lastrq);
+ }
+
+ return 0;
+}
+
+static const VMStateDescription vmstate_pcf8574 = {
+ .name = "pcf8574",
+ .version_id = 0,
+ .minimum_version_id = 0,
+ .fields = (VMStateField[]) {
+ VMSTATE_I2C_SLAVE(parent_obj, PCF8574State),
+ VMSTATE_UINT8(lastrq, PCF8574State),
+ VMSTATE_UINT8(input, PCF8574State),
+ VMSTATE_UINT8(output, PCF8574State),
+ VMSTATE_END_OF_LIST()
+ }
+};
+
+static void pcf8574_gpio_set(void *opaque, int line, int level)
+{
+ PCF8574State *s = (PCF8574State *) opaque;
+ assert(line >= 0 && line < ARRAY_SIZE(s->handler));
+
+ if (level) {
+ s->input |= (1 << line);
+ } else {
+ s->input &= ~(1 << line);
+ }
+
+ if (pcf8574_line_state(s) != s->lastrq && s->intrq) {
+ qemu_set_irq(s->intrq, 0);
+ }
+}
+
+static void pcf8574_realize(DeviceState *dev, Error **errp)
+{
+ PCF8574State *s = PCF8574(dev);
+
+ qdev_init_gpio_in(dev, pcf8574_gpio_set, ARRAY_SIZE(s->handler));
+ qdev_init_gpio_out(dev, s->handler, ARRAY_SIZE(s->handler));
+ qdev_init_gpio_out_named(dev, &s->intrq, "nINT", 1);
+}
+
+static void pcf8574_class_init(ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+ I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
+
+ k->recv = pcf8574_rx;
+ k->send = pcf8574_tx;
+ dc->realize = pcf8574_realize;
+ dc->reset = pcf8574_reset;
+ dc->vmsd = &vmstate_pcf8574;
+}
+
+static const TypeInfo pcf8574_infos[] = {
+ {
+ .name = TYPE_PCF8574,
+ .parent = TYPE_I2C_SLAVE,
+ .instance_size = sizeof(PCF8574State),
+ .class_init = pcf8574_class_init,
+ }
+};
+
+DEFINE_TYPES(pcf8574_infos);
diff --git a/hw/gpio/Kconfig b/hw/gpio/Kconfig
index 712940b8e0..19c97cc823 100644
--- a/hw/gpio/Kconfig
+++ b/hw/gpio/Kconfig
@@ -19,3 +19,7 @@ config SIFIVE_GPIO
config STM32L4X5_GPIO
bool
+
+config PCF8574
+ bool
+ depends on I2C
diff --git a/hw/gpio/meson.build b/hw/gpio/meson.build
index 3454b503ae..791e93a97b 100644
--- a/hw/gpio/meson.build
+++ b/hw/gpio/meson.build
@@ -16,3 +16,4 @@ system_ss.add(when: 'CONFIG_RASPI', if_true: files(
system_ss.add(when: 'CONFIG_STM32L4X5_SOC', if_true: files('stm32l4x5_gpio.c'))
system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_gpio.c'))
system_ss.add(when: 'CONFIG_SIFIVE_GPIO', if_true: files('sifive_gpio.c'))
+system_ss.add(when: 'CONFIG_PCF8574', if_true: files('pcf8574.c'))
--
2.41.0
next prev parent reply other threads:[~2024-03-12 8:26 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-12 8:22 [PULL 00/13] Misc HW patches for 2024-03-12 Philippe Mathieu-Daudé
2024-03-12 8:22 ` [PULL 01/13] hw/ide/ahci: Rename ahci_internal.h to ahci-internal.h Philippe Mathieu-Daudé
2024-03-12 8:22 ` [PULL 02/13] hw/pci: add some convenient trace-events for pcie and shpc hotplug Philippe Mathieu-Daudé
2024-03-12 8:22 ` [PULL 03/13] hw/ppc/sam460ex: Support short options for adding drives Philippe Mathieu-Daudé
2024-03-12 8:22 ` [PULL 04/13] hw/core/loader-fit: Fix missing ERRP_GUARD() for error_prepend() Philippe Mathieu-Daudé
2024-03-12 8:22 ` [PULL 05/13] hw/core/qdev-properties-system: " Philippe Mathieu-Daudé
2024-03-12 8:22 ` [PULL 06/13] hw/misc/ivshmem: " Philippe Mathieu-Daudé
2024-03-12 8:22 ` [PULL 07/13] sun4u: remap ebus BAR0 to use unassigned_io_ops instead of alias to PCI IO space Philippe Mathieu-Daudé
2024-03-12 8:22 ` [PULL 08/13] hw/core: Cleanup unused included headers in cpu-common.c Philippe Mathieu-Daudé
2024-03-12 8:22 ` [PULL 09/13] hw/core: Cleanup unused included header in machine-qmp-cmds.c Philippe Mathieu-Daudé
2024-03-12 8:22 ` [PULL 10/13] hw/core: Cleanup unused included headers in numa.c Philippe Mathieu-Daudé
2024-03-12 8:22 ` Philippe Mathieu-Daudé [this message]
2024-03-12 8:22 ` [PULL 12/13] meson.build: Always require an objc compiler on macos hosts Philippe Mathieu-Daudé
2024-03-12 8:22 ` [PULL 13/13] docs/about/deprecated.rst: Move SMP configurations item to system emulator section Philippe Mathieu-Daudé
2024-03-12 16:28 ` [PULL 00/13] Misc HW patches for 2024-03-12 Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240312082239.69696-12-philmd@linaro.org \
--to=philmd@linaro.org \
--cc=d.sharikhin@yadro.com \
--cc=pbonzini@redhat.com \
--cc=qemu-arm@nongnu.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-ppc@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).