From patchwork Sun Feb 18 22:20:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Ehrhardt X-Patchwork-Id: 13562088 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0669AC48BF6 for ; Sun, 18 Feb 2024 22:31:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=IxO9dRqzZlsUDQxdOhpJXSp10rk/PA1+mGIN/fULdCE=; b=ztRIccWn0JVUts 2VYfgfVhw8GIIXT2j764BYAZG5uvt7zQFlj3Yzt6ptmWgQ1shY+YWZKTFUV75ylqTsrJ54JoGkdUA a1t/Yj7iWtliK6Qgs6Bm5LBlTFv4jknLXVIW6TDcNBw2oQ/s848ehizZ4NHrhX6E/0WgmYNMHUN9J jKulxPj+P7PJtEgp3F/N9psqlbMwrZrgY21oNBXttly2p/8r8MM/ObKkfstQTManiGvCFoDt9en7G 5KckWTA8xCfseEsJ9Pp+B9FlEg/mtZb/D7N3ATo6KzceKAqwJEPVf30V0Kr8kaJEVh2k8d3nyX4D+ QuUfHaQvOC4AZJd8zYEg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgx-00000008YZR-3zRR; Sun, 18 Feb 2024 22:31:23 +0000 Received: from cae.in-ulm.de ([217.10.14.231]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgv-00000008YXv-13am for linux-arm-kernel@lists.infradead.org; Sun, 18 Feb 2024 22:31:22 +0000 Received: by cae.in-ulm.de (Postfix, from userid 1000) id 0456414092E; Sun, 18 Feb 2024 23:21:07 +0100 (CET) From: "Christian A. Ehrhardt" To: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Christian A. Ehrhardt" , Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , =?utf-8?q?Samuel_?= =?utf-8?q?=C4=8Cavoj?= , Hans de Goede , Neil Armstrong , Prashanth K , Dmitry Baryshkov , Saranya Gopal , Haotien Hsu , Andy Shevchenko , Utkarsh Patel , Bjorn Andersson , Luca Weiss , Min-Hua Chen , Rob Herring , Rajaram Regupathy , linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, Abhishek Pandit-Subedi Subject: [RFC PATCH 5/6] usb: typec: ucsi: Introdcue ->read_data and ->write_data Date: Sun, 18 Feb 2024 23:20:38 +0100 Message-Id: <20240218222039.822040-6-lk@c--e.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240218222039.822040-1-lk@c--e.de> References: <20240218222039.822040-1-lk@c--e.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240218_143121_460587_287346D0 X-CRM114-Status: GOOD ( 16.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org With the previous refactoring ->read() is only used to read from UCSI_MESSAGE_IN. So rename ->read() to ->read_data(). Before this change there is (at least for some backends) no way to write to UCSI_MESSAGE_OUT without starting a command. As a result it is not possible to send a command with additional data on these backends. Currently, the UCSI core does not send such commands but they are defined in the spec and presumably will be required. Introduce ->write_data() to write to UCSI_MESSAGE_OUT without starting a command. This is symmetrical to ->read_data(). Signed-off-by: Christian A. Ehrhardt --- drivers/usb/typec/ucsi/ucsi.c | 6 ++--- drivers/usb/typec/ucsi/ucsi.h | 7 ++--- drivers/usb/typec/ucsi/ucsi_acpi.c | 21 +++++++++++---- drivers/usb/typec/ucsi/ucsi_ccg.c | 18 ++++++++----- drivers/usb/typec/ucsi/ucsi_glink.c | 37 +++++++++++++++++---------- drivers/usb/typec/ucsi/ucsi_stm32g0.c | 30 +++++++++++++++++----- 6 files changed, 82 insertions(+), 37 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index a4ae3d5966a0..df3fe04cb9cd 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -46,7 +46,7 @@ static int ucsi_read_message_in(struct ucsi *ucsi, void *buf, if (ucsi->version <= UCSI_VERSION_1_2) buf_size = clamp(buf_size, 0, 16); - return ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, buf, buf_size); + return ucsi->ops->read_data(ucsi, buf, buf_size); } static int ucsi_acknowledge_command(struct ucsi *ucsi) @@ -1548,8 +1548,8 @@ struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops) { struct ucsi *ucsi; - if (!ops || !ops->poll_cci || !ops->read || !ops->sync_write || - !ops->async_write) + if (!ops || !ops->poll_cci || !ops->read_data || !ops->write_data || + !ops->sync_write || !ops->async_write) return ERR_PTR(-EINVAL); ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL); diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 55e5c5a09b32..2ad68124511b 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -56,7 +56,8 @@ struct dentry; /** * struct ucsi_operations - UCSI I/O operations * @poll_cci: Update the cached CCI value from hardware. Required for reset. - * @read: Read operation + * @read_data: Read MESSAGE_IN data + * @write_data: Write MESSAGE_OUT data * @sync_write: Blocking write operation * @async_write: Non-blocking write operation * @update_altmodes: Squashes duplicate DP altmodes @@ -67,8 +68,8 @@ struct dentry; */ struct ucsi_operations { int (*poll_cci)(struct ucsi *ucsi); - int (*read)(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len); + int (*read_data)(struct ucsi *ucsi, void *val, size_t val_len); + int (*write_data)(struct ucsi *ucsi, const void *val, size_t val_len); int (*sync_write)(struct ucsi *ucsi, unsigned int offset, const void *val, size_t val_len); int (*async_write)(struct ucsi *ucsi, unsigned int offset, diff --git a/drivers/usb/typec/ucsi/ucsi_acpi.c b/drivers/usb/typec/ucsi/ucsi_acpi.c index 3aedfe78fb65..79b47b433e35 100644 --- a/drivers/usb/typec/ucsi/ucsi_acpi.c +++ b/drivers/usb/typec/ucsi/ucsi_acpi.c @@ -58,12 +58,21 @@ static int ucsi_acpi_poll_cci(struct ucsi *ucsi) return 0; } -static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int ucsi_acpi_read_data(struct ucsi *ucsi, void *val, size_t val_len) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); - memcpy(val, ua->base + offset, val_len); + memcpy(val, ua->base + UCSI_MESSAGE_IN, val_len); + + return 0; +} + +static int ucsi_acpi_write_data(struct ucsi *ucsi, const void *val, + size_t val_len) +{ + struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); + + memcpy(ua->base + UCSI_MESSAGE_OUT, val, val_len); return 0; } @@ -108,7 +117,8 @@ static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset, static const struct ucsi_operations ucsi_acpi_ops = { .poll_cci = ucsi_acpi_poll_cci, - .read = ucsi_acpi_read, + .read_data = ucsi_acpi_read_data, + .write_data = ucsi_acpi_write_data, .sync_write = ucsi_acpi_sync_write, .async_write = ucsi_acpi_async_write }; @@ -163,7 +173,8 @@ ucsi_dell_sync_write(struct ucsi *ucsi, unsigned int offset, static const struct ucsi_operations ucsi_dell_ops = { .poll_cci = ucsi_acpi_poll_cci, - .read = ucsi_acpi_read, + .read_data = ucsi_acpi_read_data, + .write_data = ucsi_acpi_write_data, .sync_write = ucsi_dell_sync_write, .async_write = ucsi_acpi_async_write }; diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index 55d0fe5324f4..d6026f61a0ed 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -559,16 +559,12 @@ static int ucsi_ccg_poll_cci(struct ucsi *ucsi) return 0; } -static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int ucsi_ccg_read_data(struct ucsi *ucsi, void *val, size_t val_len) { struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); struct ucsi_capability *cap; struct ucsi_altmode *alt; - if (offset != UCSI_MESSAGE_IN) - return -EINVAL; - spin_lock(&uc->op_lock); memcpy(val, &uc->op_data.message_in, val_len); spin_unlock(&uc->op_lock); @@ -600,6 +596,15 @@ static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset, return 0; } +static int ucsi_ccg_write_data(struct ucsi *ucsi, const void *val, + size_t val_len) +{ + struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); + u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_MESSAGE_OUT); + + return ccg_write(uc, reg, val, val_len); +} + static int ucsi_ccg_async_write(struct ucsi *ucsi, unsigned int offset, const void *val, size_t val_len) { @@ -656,7 +661,8 @@ static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsigned int offset, static const struct ucsi_operations ucsi_ccg_ops = { .poll_cci = ucsi_ccg_poll_cci, - .read = ucsi_ccg_read, + .read_data = ucsi_ccg_read_data, + .write_data = ucsi_ccg_write_data, .sync_write = ucsi_ccg_sync_write, .async_write = ucsi_ccg_async_write, .update_altmodes = ucsi_ccg_update_altmodes diff --git a/drivers/usb/typec/ucsi/ucsi_glink.c b/drivers/usb/typec/ucsi/ucsi_glink.c index d65ab81b4ed6..9dab1b428ad9 100644 --- a/drivers/usb/typec/ucsi/ucsi_glink.c +++ b/drivers/usb/typec/ucsi/ucsi_glink.c @@ -75,6 +75,7 @@ struct pmic_glink_ucsi { struct work_struct register_work; u8 read_buf[UCSI_BUF_SIZE]; + u8 write_buf[UCSI_BUF_SIZE]; }; static int pmic_glink_sync_read_buf(struct pmic_glink_ucsi *ucsi) @@ -129,18 +130,28 @@ static int pmic_glink_ucsi_poll_cci(struct ucsi *__ucsi) return 0; } -static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset, - void *val, size_t val_len) +static int pmic_glink_ucsi_read_data(struct ucsi *__ucsi, + void *val, size_t val_len) { struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); mutex_lock(&ucsi->lock); - memcpy(val, &ucsi->read_buf[offset], val_len); + memcpy(val, &ucsi->read_buf[UCSI_MESSAGE_IN], val_len); mutex_unlock(&ucsi->lock); return 0; } +static int pmic_glink_ucsi_write_data(struct ucsi *__ucsi, + const void *val, size_t val_len) +{ + struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); + + memcpy(&ucsi->write_buf[UCSI_MESSAGE_OUT], val, val_len); + + return 0; +} + static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned int offset, const void *val, size_t val_len) { @@ -148,10 +159,15 @@ static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned i unsigned long left; int ret; + if (offset != UCSI_CONTROL || val_len != sizeof(u64)) + return -EINVAL; + memcpy(ucsi->write_buf + UCSI_CONTROL, val, val_len); + req.hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_USBC); req.hdr.type = cpu_to_le32(MSG_TYPE_REQ_RESP); req.hdr.opcode = cpu_to_le32(UC_UCSI_WRITE_BUF_REQ); - memcpy(&req.buf[offset], val, val_len); + memcpy(req.buf, ucsi->write_buf, UCSI_BUF_SIZE); + memset(ucsi->write_buf, 0, sizeof(ucsi->write_buf)); reinit_completion(&ucsi->write_ack); @@ -214,7 +230,8 @@ static int pmic_glink_ucsi_sync_write(struct ucsi *__ucsi, unsigned int offset, static const struct ucsi_operations pmic_glink_ucsi_ops = { .poll_cci = pmic_glink_ucsi_poll_cci, - .read = pmic_glink_ucsi_read, + .read_data = pmic_glink_ucsi_read_data, + .write_data = pmic_glink_ucsi_write_data, .sync_write = pmic_glink_ucsi_sync_write, .async_write = pmic_glink_ucsi_async_write }; @@ -286,7 +303,6 @@ static void pmic_glink_ucsi_notify(struct work_struct *work) static void pmic_glink_ucsi_register(struct work_struct *work) { struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, register_work); - __le16 version; int ret; ret = pmic_glink_sync_read_buf(ucsi); @@ -294,14 +310,9 @@ static void pmic_glink_ucsi_register(struct work_struct *work) dev_err(ucsi->dev, "cannot sync read buf: %d\n", ret); return; } - ret = pmic_glink_ucsi_read(ucsi->ucsi, UCSI_VERSION, &version, - sizeof(version)); - if (ret < 0) { - dev_err(ucsi->dev, "cannot read version: %d\n", ret); - return; - } - ucsi_register(ucsi->ucsi, le16_to_cpu(version)); + ucsi_register(ucsi->ucsi, + le16_to_cpu(*(__le16 *)(ucsi->read_buf + UCSI_VERSION))); } static void pmic_glink_ucsi_callback(const void *data, size_t len, void *priv) diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi/ucsi_stm32g0.c index bab2363b73f4..d68aca118e41 100644 --- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c +++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c @@ -372,18 +372,17 @@ static int ucsi_stm32g0_poll_cci(struct ucsi *ucsi) return 0; } -static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t len) +static int ucsi_stm32g0_read_data(struct ucsi *ucsi, void *val, size_t len) { struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); - return ucsi_stm32g0_read_from_hw(g0, offset, val, len); + return ucsi_stm32g0_read_from_hw(g0, UCSI_MESSAGE_IN, val, len); } -static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, const void *val, - size_t len) +static int ucsi_stm32g0_write_to_hw(struct ucsi_stm32g0 *g0, + unsigned int offset, + const void *val, size_t len) { - struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); struct i2c_client *client = g0->client; struct i2c_msg msg[] = { { @@ -414,6 +413,22 @@ static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, cons return 0; } +static int ucsi_stm32g0_write_data(struct ucsi *ucsi, + const void *val, size_t len) +{ + struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); + + return ucsi_stm32g0_write_to_hw(g0, UCSI_MESSAGE_OUT, val, len); +} + +static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, + const void *val, size_t len) +{ + struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); + + return ucsi_stm32g0_write_to_hw(g0, offset, val, len); +} + static int ucsi_stm32g0_sync_write(struct ucsi *ucsi, unsigned int offset, const void *val, size_t len) { @@ -463,7 +478,8 @@ static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data) static const struct ucsi_operations ucsi_stm32g0_ops = { .poll_cci = ucsi_stm32g0_poll_cci, - .read = ucsi_stm32g0_read, + .read_data = ucsi_stm32g0_read_data, + .write_data = ucsi_stm32g0_write_data, .sync_write = ucsi_stm32g0_sync_write, .async_write = ucsi_stm32g0_async_write, };