From nobody Tue Feb 10 03:39:51 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of _spf.google.com designates 209.85.128.50 as permitted sender) client-ip=209.85.128.50; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-wm1-f50.google.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.128.50 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1623930888; cv=none; d=zohomail.com; s=zohoarc; b=GNN5COBnYwdp1Zbqy317lLUhrHiBeaKsEHcdckFs+FIZeCFS8Ui75+N0IgO6rI3gLVHvid1QADBUFz5BdDRYz5Q6oLUNrho6rIMdhDC9m6gLT9fkfSkg3EIRf4TrKOm4n4M1k2alRmqy+ASAGGg4mTwqxuhnjuSJm5c762M4VOI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1623930888; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cTmXbAZuVSJ/iAUXGS3uU1Sg/xrnfGmybw7wWkCyin8=; b=mrypP7ArrLlM4voJYslUbfkjmWp7HQXbnCBI5+K1scdIcSzM+JVrNNupxP6MktbgYdmWUZmpHMmGA2Q0I0+ruoznvuA9GA6x2Lgi+dhnjLphIEiSvZJFNjX/AbDDSdAV/9oQxmsekmhwUyIcClRUIp4CoMs/DucxciXQSWGMku0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.128.50 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) by mx.zohomail.com with SMTPS id 1623930888010340.41047051253383; Thu, 17 Jun 2021 04:54:48 -0700 (PDT) Received: by mail-wm1-f50.google.com with SMTP id j18so3081114wms.3 for ; Thu, 17 Jun 2021 04:54:47 -0700 (PDT) Return-Path: Return-Path: Received: from x1w.. (93.red-83-35-24.dynamicip.rima-tde.net. [83.35.24.93]) by smtp.gmail.com with ESMTPSA id j1sm4415717wmi.44.2021.06.17.04.54.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 04:54:45 -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=cTmXbAZuVSJ/iAUXGS3uU1Sg/xrnfGmybw7wWkCyin8=; b=n5mOzI+QEg/h8yQEy2G7Yx79vFljUN7KfsGw5Tvirhh5YwQtSXCnyBCh5Au4JdnoQL 2djrqIc8ZUi5zryKsjF7RzLZfjGM8kM4iaGhiDGf4d2rz6dsTtMul/42sJPripR9xxQY XBnVZBXNGo4rP6GRz8tVO4IB+vjty1PKHHDO2d7yNZxqfL2QpqcoxGlbgPsqPoEE1ZMT 1V3btKBRJTN2DFoZrxdFGvHJGGExxJDGb9iMNYPNuGDYC+tTsH/eaME71E5A/itpir+j Vs1R+bEmej850VIfmuuvJRPo3BQ7qm49t5lx/y1Shj5TJzCZZuyoxH9Wh+AqyxbQeN0f 6LKw== 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=cTmXbAZuVSJ/iAUXGS3uU1Sg/xrnfGmybw7wWkCyin8=; b=EbZYDAVDwj8B+D+/XR9PCfBdXbObyR8PXkROMVsLPt1NtiRwzQM4QMoRTvGLYO+WQE AeIbe9CT2SjqMfCnOEYQWP6O65AJ+RaJyAi/Qd2TG75IcVM+eN6oFKpB2gW42A2vqAKx i8qcODzTgr1pPGU/RKsI3dBn1PL73oO9liadlOjyowlZUt6heIOfRFNBkrFh0ObPPoTq AKcT66ihuAANZynzq59BUn8QxRtuH91O6MERcduY2rI6ZWwaPHWKH+4vJsbVQHKfS3xy eSESd/RiTfF7ZUBprWIPzGcYcvy8ggt9Ty0hTTt4GRIEPtWIj4PHFPPkP8ilgKMpdMN4 078A== X-Gm-Message-State: AOAM530JUpLzrChIuTSlUN8AgoU5eLuplcV5jngqzHmmMn7n2WpXSk/h ikoyeOtcAX9eBMEQRswDBiA= X-Google-Smtp-Source: ABdhPJwwVeom4mbpOdacLMHniBDEf1Nw94Zd+2Qdz7LwchYqJZMcafxa8FAPgPfNmXxz0c5FweCbHQ== X-Received: by 2002:a05:600c:22cf:: with SMTP id 15mr4704358wmg.144.1623930886202; Thu, 17 Jun 2021 04:54:46 -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: Corey Minyard , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Richard Henderson Subject: [PATCH v5 15/15] hw/i2c: Introduce i2c_start_recv() and i2c_start_send() Date: Thu, 17 Jun 2021 13:53:34 +0200 Message-Id: <20210617115334.2761573-16-f4bug@amsat.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210617115334.2761573-1-f4bug@amsat.org> References: <20210617115334.2761573-1-f4bug@amsat.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) To ease reviewing code using the I2C bus API, introduce the i2c_start_recv() and i2c_start_send() helpers which don't take the confusing 'is_recv' boolean argument. Use these new helpers in the SMBus / AUX bus models. Suggested-by: Richard Henderson Reviewed-by: Richard Henderson Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- include/hw/i2c/i2c.h | 24 ++++++++++++++++++++++++ hw/i2c/core.c | 10 ++++++++++ hw/i2c/pm_smbus.c | 4 ++-- hw/i2c/smbus_master.c | 22 +++++++++++----------- hw/misc/auxbus.c | 12 ++++++------ 5 files changed, 53 insertions(+), 19 deletions(-) diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h index 21f2dba1bf7..5ca3b708c0b 100644 --- a/include/hw/i2c/i2c.h +++ b/include/hw/i2c/i2c.h @@ -88,9 +88,33 @@ int i2c_bus_busy(I2CBus *bus); * @address: address of the slave * @is_recv: indicates the transfer direction * + * When @is_recv is a known boolean constant, use the + * i2c_start_recv() or i2c_start_send() helper instead. + * * Returns: 0 on success, -1 on error */ int i2c_start_transfer(I2CBus *bus, uint8_t address, bool is_recv); + +/** + * i2c_start_recv: start a 'receive' transfer on an I2C bus. + * + * @bus: #I2CBus to be used + * @address: address of the slave + * + * Returns: 0 on success, -1 on error + */ +int i2c_start_recv(I2CBus *bus, uint8_t address); + +/** + * i2c_start_send: start a 'send' transfer on an I2C bus. + * + * @bus: #I2CBus to be used + * @address: address of the slave + * + * Returns: 0 on success, -1 on error + */ +int i2c_start_send(I2CBus *bus, uint8_t address); + void i2c_end_transfer(I2CBus *bus); void i2c_nack(I2CBus *bus); int i2c_send(I2CBus *bus, uint8_t data); diff --git a/hw/i2c/core.c b/hw/i2c/core.c index 5483bf95a3e..416372ad00c 100644 --- a/hw/i2c/core.c +++ b/hw/i2c/core.c @@ -180,6 +180,16 @@ int i2c_start_transfer(I2CBus *bus, uint8_t address, b= ool is_recv) : I2C_START_SEND); } =20 +int i2c_start_recv(I2CBus *bus, uint8_t address) +{ + return i2c_do_start_transfer(bus, address, I2C_START_RECV); +} + +int i2c_start_send(I2CBus *bus, uint8_t address) +{ + return i2c_do_start_transfer(bus, address, I2C_START_SEND); +} + void i2c_end_transfer(I2CBus *bus) { I2CSlaveClass *sc; diff --git a/hw/i2c/pm_smbus.c b/hw/i2c/pm_smbus.c index 06e1e5321b9..d7eae548cbc 100644 --- a/hw/i2c/pm_smbus.c +++ b/hw/i2c/pm_smbus.c @@ -128,14 +128,14 @@ static void smb_transaction(PMSMBus *s) * So at least Linux may or may not set the read bit here. * So just ignore the read bit for this command. */ - if (i2c_start_transfer(bus, addr, 0)) { + if (i2c_start_send(bus, addr)) { goto error; } ret =3D i2c_send(bus, s->smb_data1); if (ret) { goto error; } - if (i2c_start_transfer(bus, addr, 1)) { + if (i2c_start_recv(bus, addr)) { goto error; } s->in_i2c_block_read =3D true; diff --git a/hw/i2c/smbus_master.c b/hw/i2c/smbus_master.c index dc43b8637d1..6a53c34e70b 100644 --- a/hw/i2c/smbus_master.c +++ b/hw/i2c/smbus_master.c @@ -29,7 +29,7 @@ int smbus_receive_byte(I2CBus *bus, uint8_t addr) { uint8_t data; =20 - if (i2c_start_transfer(bus, addr, 1)) { + if (i2c_start_recv(bus, addr)) { return -1; } data =3D i2c_recv(bus); @@ -40,7 +40,7 @@ int smbus_receive_byte(I2CBus *bus, uint8_t addr) =20 int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data) { - if (i2c_start_transfer(bus, addr, 0)) { + if (i2c_start_send(bus, addr)) { return -1; } i2c_send(bus, data); @@ -51,11 +51,11 @@ int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t = data) int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command) { uint8_t data; - if (i2c_start_transfer(bus, addr, 0)) { + if (i2c_start_send(bus, addr)) { return -1; } i2c_send(bus, command); - if (i2c_start_transfer(bus, addr, 1)) { + if (i2c_start_recv(bus, addr)) { i2c_end_transfer(bus); return -1; } @@ -67,7 +67,7 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t co= mmand) =20 int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t d= ata) { - if (i2c_start_transfer(bus, addr, 0)) { + if (i2c_start_send(bus, addr)) { return -1; } i2c_send(bus, command); @@ -79,11 +79,11 @@ int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t= command, uint8_t data) int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command) { uint16_t data; - if (i2c_start_transfer(bus, addr, 0)) { + if (i2c_start_send(bus, addr)) { return -1; } i2c_send(bus, command); - if (i2c_start_transfer(bus, addr, 1)) { + if (i2c_start_recv(bus, addr)) { i2c_end_transfer(bus); return -1; } @@ -96,7 +96,7 @@ int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t co= mmand) =20 int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t = data) { - if (i2c_start_transfer(bus, addr, 0)) { + if (i2c_start_send(bus, addr)) { return -1; } i2c_send(bus, command); @@ -113,12 +113,12 @@ int smbus_read_block(I2CBus *bus, uint8_t addr, uint8= _t command, uint8_t *data, int i; =20 if (send_cmd) { - if (i2c_start_transfer(bus, addr, 0)) { + if (i2c_start_send(bus, addr)) { return -1; } i2c_send(bus, command); } - if (i2c_start_transfer(bus, addr, 1)) { + if (i2c_start_recv(bus, addr)) { if (send_cmd) { i2c_end_transfer(bus); } @@ -149,7 +149,7 @@ int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_= t command, uint8_t *data, len =3D 32; } =20 - if (i2c_start_transfer(bus, addr, 0)) { + if (i2c_start_send(bus, addr)) { return -1; } i2c_send(bus, command); diff --git a/hw/misc/auxbus.c b/hw/misc/auxbus.c index 44aa9730bc9..434ff8d910d 100644 --- a/hw/misc/auxbus.c +++ b/hw/misc/auxbus.c @@ -135,7 +135,7 @@ AUXReply aux_request(AUXBus *bus, AUXCommand cmd, uint3= 2_t address, i2c_end_transfer(i2c_bus); } =20 - if (i2c_start_transfer(i2c_bus, address, true)) { + if (i2c_start_recv(i2c_bus, address)) { ret =3D AUX_I2C_NACK; break; } @@ -151,7 +151,7 @@ AUXReply aux_request(AUXBus *bus, AUXCommand cmd, uint3= 2_t address, i2c_end_transfer(i2c_bus); } =20 - if (i2c_start_transfer(i2c_bus, address, false)) { + if (i2c_start_send(i2c_bus, address)) { ret =3D AUX_I2C_NACK; break; } @@ -179,7 +179,7 @@ AUXReply aux_request(AUXBus *bus, AUXCommand cmd, uint3= 2_t address, /* * No transactions started.. */ - if (i2c_start_transfer(i2c_bus, address, false)) { + if (i2c_start_send(i2c_bus, address)) { break; } } else if ((address !=3D bus->last_i2c_address) || @@ -188,7 +188,7 @@ AUXReply aux_request(AUXBus *bus, AUXCommand cmd, uint3= 2_t address, * Transaction started but we need to restart.. */ i2c_end_transfer(i2c_bus); - if (i2c_start_transfer(i2c_bus, address, false)) { + if (i2c_start_send(i2c_bus, address)) { break; } } @@ -210,7 +210,7 @@ AUXReply aux_request(AUXBus *bus, AUXCommand cmd, uint3= 2_t address, /* * No transactions started.. */ - if (i2c_start_transfer(i2c_bus, address, true)) { + if (i2c_start_recv(i2c_bus, address)) { break; } } else if ((address !=3D bus->last_i2c_address) || @@ -219,7 +219,7 @@ AUXReply aux_request(AUXBus *bus, AUXCommand cmd, uint3= 2_t address, * Transaction started but we need to restart.. */ i2c_end_transfer(i2c_bus); - if (i2c_start_transfer(i2c_bus, address, true)) { + if (i2c_start_recv(i2c_bus, address)) { break; } } --=20 2.31.1