From nobody Tue Nov 26 06:42:54 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=yadro.com ARC-Seal: i=1; a=rsa-sha256; t=1709278676; cv=none; d=zohomail.com; s=zohoarc; b=H6JaXkv8v8FUrDm2tZ5/nkZ1IVxz2hRFIoHdLXQv3UYtbW/DRiIEYqc5tgiAIJnGsKZPyyPF/dM35rVS7Rc/6my0Bxg8VLZWTyngVi5XsEGn4BddY+SxxpVlGxyQ9F3GM4Mi+DBG/kljeRGFsDzbAWltHXeTS9xmvo8jwCuQpqM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1709278676; h=Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=k7FblgxATpkBqsWWo3G0v7q4bUcJhsjSejWoLrfvUK4=; b=QCc1qF67fA3g4hQmzwuAPOkCbY9vgw3CqU67g4S7kjcpvoPgtzTflTxhCmiJe8L7G6FfbdjYk0qLaGVocbn4Oh6N0BDrcS7b92DKK2LWMIZNnpBdfW1+EU5qn+sMp2kvcmcIMDLPOijcMvtQ4r7Qf4bg3/fXj/hoISk9lLcfrbY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1709278676003489.60806814853925; Thu, 29 Feb 2024 23:37:56 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rfxS0-0003QA-Ii; Fri, 01 Mar 2024 02:37:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rfxRx-0003Pw-BR for qemu-devel@nongnu.org; Fri, 01 Mar 2024 02:36:57 -0500 Received: from mta-04.yadro.com ([89.207.88.248]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rfxRt-00013y-5d for qemu-devel@nongnu.org; Fri, 01 Mar 2024 02:36:57 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 mta-04.yadro.com 9B532C0004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yadro.com; s=mta-04; t=1709278603; bh=k7FblgxATpkBqsWWo3G0v7q4bUcJhsjSejWoLrfvUK4=; h=From:To:Subject:Date:Message-ID:Content-Type:MIME-Version:From; b=dm/JG/dBV1DbTxrQDi5erwMhvOwPtYAZ13m7UMZzHyQjudjFQZelHA0YKnPIzmCwr TI4g/XxkyyrKFBBbi8tBenNb2F3SpDWEd4gH4D0cw6sTCptSF9BiviJkN1shsPKDiv zED2U7xx1aExTKMbYEiKLqFusjuTJl7P0blqhOy8B+oLnzyytApXkvSoWYqEm9xbMN TK5ajmcHJzLX5796ddh06a1Lmriq5khFgr6imKadG8Ba3se7cN0z222v1EO/dCAgmi KXuwJf9s90v0PpaK5WRat3+BZLOP01v5w0v0l+OncdAfWYJdHWTFfYhiGU8r7DY/9v 1aa4S9V1oy7EQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yadro.com; s=mta-03; t=1709278603; bh=k7FblgxATpkBqsWWo3G0v7q4bUcJhsjSejWoLrfvUK4=; h=From:To:Subject:Date:Message-ID:Content-Type:MIME-Version:From; b=pGmhc0Ul4tYuYpv0k6ylcDMQNrJ81pceydYNJ0zVDja+cC8yqmCTUtRMjzR86yzvn /z2j76ZbFVGN9HuiNCOLmmg241XBNxT9K5lCPZDo+x/idzwNWwgArfUoTT6/K09IJa SD3PYRehFcHNIVjAzignX+Hb03k3fi3nKo4BMb5CBKMBg1R0IQAWpoPVHil/bKROV1 /GpBtdLHjE0zWnFEBvBpCHQD5jRQJS9ZHDaE05OxdQDWBMMecEaTNtXRKi1IpD5Hrm N0bW6HYFHidhkbRw0mLkMR+qPfrlE0YrGzInDvLxAl1P6LGaM9WJDFxCpgKcBZ9sDH 47wEshl++NNpg== From: Dmitriy Sharikhin To: "qemu-devel@nongnu.org" CC: Igor Kononenko , "Alexander A. Filippov" , Alexander Amelkin Subject: [PATCH] hw: gpio: introduce pcf8574 driver Thread-Topic: [PATCH] hw: gpio: introduce pcf8574 driver Thread-Index: AQHaa6s0kSa3DF4Dg0q8WutIAOtiBA== Date: Fri, 1 Mar 2024 07:36:42 +0000 Message-ID: Accept-Language: ru-RU, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Type: text/plain; charset="utf-8" Content-ID: <754AEE96F634FD49A88543F47CD7FBBE@yadro.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=89.207.88.248; envelope-from=d.sharikhin@yadro.com; helo=mta-04.yadro.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @yadro.com) (identity @yadro.com) X-ZM-MESSAGEID: 1709278679120100003 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. This patch allow to instantiate virtual I2C device called "pcf8574" in machine init code via generic mechanism. Signed-off-by: Dmitrii Sharikhin --- hw/gpio/Kconfig | 4 ++ hw/gpio/meson.build | 1 + hw/gpio/pcf8574.c | 139 ++++++++++++++++++++++++++++++++++++++ include/hw/gpio/pcf8574.h | 15 ++++ 4 files changed, 159 insertions(+) create mode 100644 hw/gpio/pcf8574.c create mode 100644 include/hw/gpio/pcf8574.h diff --git a/hw/gpio/Kconfig b/hw/gpio/Kconfig index d2cf3accc8..bb731ff4ce 100644 --- a/hw/gpio/Kconfig +++ b/hw/gpio/Kconfig @@ -16,3 +16,7 @@ config GPIO_PWR =20 config SIFIVE_GPIO bool + +config PCF8574 + bool + depends on I2C diff --git a/hw/gpio/meson.build b/hw/gpio/meson.build index 8a8d03d885..c0d9a3c757 100644 --- a/hw/gpio/meson.build +++ b/hw/gpio/meson.build @@ -15,3 +15,4 @@ system_ss.add(when: 'CONFIG_RASPI', if_true: files( )) 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')) diff --git a/hw/gpio/pcf8574.c b/hw/gpio/pcf8574.c new file mode 100644 index 0000000000..a6c6bd36fa --- /dev/null +++ b/hw/gpio/pcf8574.c @@ -0,0 +1,139 @@ +/* + * NXP PCF8574 8-port I2C GPIO expansion chip. + * + * Copyright (c) 2024 KNS Group (YADRO). + * Written by Dmitrii Sharikhin + * + * This file is licensed under GNU GPL. + */ + +#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. + *=20 + * 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. + *=20 + * In essence, user can think of quasi-bidirectional IO as + * open-drain line, except presence of builtin rising edge acceleration + * embedded in PCF8574 IC + **/ + +OBJECT_DECLARE_SIMPLE_TYPE(PCF8574State, PCF8574) + +struct PCF8574State { + I2CSlave parent_obj; + uint8_t input; /* external electrical line state */ + uint8_t output; /* Pull-up (1) or drive low (0) on bit */ + qemu_irq handler[8]; + qemu_irq *gpio_in; +}; + +static void pcf8574_reset(DeviceState *dev) +{ + PCF8574State *s =3D PCF8574(dev); + s->input =3D 0xFF; + s->output =3D 0xFF; +} + +static inline uint8_t pcf8574_line_state(PCF8574State *s) +{ + // we driving line low or external circuit does that + return s->input & s->output;=20 +} + +static uint8_t pcf8574_rx(I2CSlave *i2c) +{ + return pcf8574_line_state(PCF8574(i2c)); +} + +static int pcf8574_tx(I2CSlave *i2c, uint8_t data) +{ + PCF8574State *s =3D PCF8574(i2c); + uint8_t prev; + uint8_t diff; + uint8_t actual; + int line =3D 0; + + prev =3D pcf8574_line_state(s); + s->output =3D data; + actual =3D pcf8574_line_state(s); + + for (diff =3D (actual ^ prev); diff; diff &=3D ~(1 << line)) + { + line =3D ctz32(diff); + if (s->handler[line]) + qemu_set_irq(s->handler[line], (actual >> line) & 1); + } + + return 0; +} + +static const VMStateDescription vmstate_pcf8574 =3D { + .name =3D "pcf8574", + .version_id =3D 0, + .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_UINT8(input, PCF8574State), + VMSTATE_UINT8(output, PCF8574State), + VMSTATE_I2C_SLAVE(parent_obj, PCF8574State), + VMSTATE_END_OF_LIST() + } +}; + +static void pcf8574_gpio_set(void *opaque, int line, int level) +{ + PCF8574State *s =3D (PCF8574State *) opaque; + assert(line >=3D 0 && line < ARRAY_SIZE(s->handler));=20 + + if (level) + s->input |=3D (1 << line); + else + s->input &=3D ~(1 << line); +} + +static void pcf8574_realize(DeviceState *dev, Error **errp) +{ + PCF8574State *s =3D 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)); +} + +static void pcf8574_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + I2CSlaveClass *k =3D I2C_SLAVE_CLASS(klass); + + k->recv =3D pcf8574_rx; + k->send =3D pcf8574_tx; + dc->realize =3D pcf8574_realize; + dc->reset =3D pcf8574_reset; + dc->vmsd =3D &vmstate_pcf8574; +} + +static const TypeInfo pcf8574_info =3D { + .name =3D TYPE_PCF8574, + .parent =3D TYPE_I2C_SLAVE, + .instance_size =3D sizeof(PCF8574State), + .class_init =3D pcf8574_class_init, +}; + +static void pcf8574_register_types(void) +{ + type_register_static(&pcf8574_info); +} + +type_init(pcf8574_register_types) diff --git a/include/hw/gpio/pcf8574.h b/include/hw/gpio/pcf8574.h new file mode 100644 index 0000000000..c690e73487 --- /dev/null +++ b/include/hw/gpio/pcf8574.h @@ -0,0 +1,15 @@ +/* + * NXP PCF8574 8-port I2C GPIO expansion chip. + * + * Copyright (c) 2024 KNS Group (YADRO). + * Written by Dmitrii Sharikhin + * + * This file is licensed under GNU GPL. + */ + +#ifndef _HW_GPIO_PCF8574 +#define _HW_GPIO_PCF8574 + +#define TYPE_PCF8574 "pcf8574" + +#endif /* _HW_GPIO_PCF8574 */ --=20 2.39.2