From patchwork Fri Nov 15 15:52:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?=C5=81ukasz_Bartosik?= X-Patchwork-Id: 13876517 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5EAE71D514C for ; Fri, 15 Nov 2024 15:52:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731685964; cv=none; b=TfXjB2g0Kz9T97EAtwpPffn5HVULwZAb4+I/sKpkQQ2sLUNwdYnjfTSWjqSRcTRoL/8PB2OpHCiwgCroZKnROL07ZbLaP4SAEktL5/Oe+TTeu/67OWzAQcWKIJu3jdTitTJHUOTnS4UJ02otgF9NsnhIIrdru0cTJKYr8P9cd6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731685964; c=relaxed/simple; bh=l6xy+IFEiECfEGnprsekfHBbUSUvzcyN3jyJsZdVimU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gkwEUK4c+/CixP0ybtoj2tyeoNPbfMIOL87n8JMV0qdtdBPF8ebYKw5kLcHZw5cYOSAB1Wcy2C3pBNKJuAkvj7ZAGInFjNyqgC0mCKSS4B4PSJL9RNsM3KeIj+Lf8BSVDqRK5Kz7Eqq5c1E30WkgwcCxMXra63A+4vukAJUu8Hw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=DDWppxgM; arc=none smtp.client-ip=209.85.218.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="DDWppxgM" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a9a6b4ca29bso119028166b.3 for ; Fri, 15 Nov 2024 07:52:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1731685961; x=1732290761; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Cw85X3F42ul1Ipw+2TID221KCP/qXBuXOFD4iWUJphc=; b=DDWppxgMh1aJ2oBJMdlia5W84v4yUTUQw651dUkfMft2QJZCf1WcU/P6Qk81pfi2Jw rHJiHuVpSkN3gJb5zN7Vnifpb1LMf7va5lJ8sHckC5EI+2cDMJt+HD8XKkclYvF7jgOQ zGRGiW6mkh1vpexHhtNlbfyzuTLmOdCkW/JVM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731685961; x=1732290761; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Cw85X3F42ul1Ipw+2TID221KCP/qXBuXOFD4iWUJphc=; b=pd7mB25KAkUocvnmMsW1PCXmnqVeBPcySECQ66uxg6FnVI+B7SVacm/ItwXxUDM/et 1NxSqhLl3OmhJP9bm7Q/p9NNIw7zYif5dohj7RkxFgfHcoil/zaxqg/0opmrzhxXk4eN W2yOTY3qcpajEeJEJAeMZdmp0Mu09HGUo63Rgz75IqjDSyDvu306KChdJnx1Tv7OF7fC bWBtZDKuuK5VNSdlZMqhrfuTdjLz4k5KZqdE8Ll7ZvHjThSaZ2U79cFBY03+qxxVM+tr NtqTUxFoInI9QuN8pg1NHJJ1ZG88i+dN24+AqSXTwkSgSFNKS2sjxwok1cGpl+2QzE84 9tKA== X-Forwarded-Encrypted: i=1; AJvYcCVLdGbbpVcTdvMVNK2yiXmjzNiget2RfbU96Qo/oKdeE5s3xltGyRlphaYem514fQtwCMa/sR1AV5I=@vger.kernel.org X-Gm-Message-State: AOJu0YwVRBPZ09CxpmUGugJRZ0+PyiorClL03teTjh6SAp/TlD7zsLpK ckPnlUvphSZ7N8iFfSeIspvkxM11EwZyBBKzZAtPdlPF29SxkOBW0bHULRp6 X-Google-Smtp-Source: AGHT+IGRjbVOsm42oCKFTm1W70lgoQkyBdu2JYgahcAw2aegTSvvju6pnXdwOKn1Rmv35xtTe4B2qg== X-Received: by 2002:a17:907:25c2:b0:aa1:e695:f84 with SMTP id a640c23a62f3a-aa483479236mr311724666b.36.1731685960657; Fri, 15 Nov 2024 07:52:40 -0800 (PST) Received: from ukaszb-ng.c.googlers.com.com (103.45.147.34.bc.googleusercontent.com. [34.147.45.103]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa20e086244sm187758166b.177.2024.11.15.07.52.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2024 07:52:40 -0800 (PST) From: =?utf-8?q?=C5=81ukasz_Bartosik?= To: Heikki Krogerus , Greg Kroah-Hartman , Benson Leung Cc: Abhishek Pandit-Subedi , Jameson Thies , Pavan Holla , Tzung-Bi Shih , linux-usb@vger.kernel.org, chrome-platform@lists.linux.dev Subject: [PATCH v7 1/3] platform/chrome: Update ChromeOS EC header for UCSI Date: Fri, 15 Nov 2024 15:52:32 +0000 Message-ID: <20241115155234.1587589-2-ukaszb@chromium.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241115155234.1587589-1-ukaszb@chromium.org> References: <20241115155234.1587589-1-ukaszb@chromium.org> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Pavan Holla Add EC host commands for reading and writing UCSI structures in the EC. The corresponding kernel driver is cros-ec-ucsi. Also update PD events supported by the EC. Acked-by: Tzung-Bi Shih Signed-off-by: Pavan Holla Signed-off-by: Łukasz Bartosik --- .../linux/platform_data/cros_ec_commands.h | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/include/linux/platform_data/cros_ec_commands.h b/include/linux/platform_data/cros_ec_commands.h index e574b790be6f..8dbb6a769e4f 100644 --- a/include/linux/platform_data/cros_ec_commands.h +++ b/include/linux/platform_data/cros_ec_commands.h @@ -5012,8 +5012,11 @@ struct ec_response_pd_status { #define PD_EVENT_POWER_CHANGE BIT(1) #define PD_EVENT_IDENTITY_RECEIVED BIT(2) #define PD_EVENT_DATA_SWAP BIT(3) +#define PD_EVENT_TYPEC BIT(4) +#define PD_EVENT_PPM BIT(5) + struct ec_response_host_event_status { - uint32_t status; /* PD MCU host event status */ + uint32_t status; /* PD MCU host event status */ } __ec_align4; /* Set USB type-C port role and muxes */ @@ -6073,6 +6076,29 @@ struct ec_response_typec_vdm_response { #undef VDO_MAX_SIZE +/* + * UCSI OPM-PPM commands + * + * These commands are used for communication between OPM and PPM. + * Only UCSI3.0 is tested. + */ + +#define EC_CMD_UCSI_PPM_SET 0x0140 + +/* The data size is stored in the host command protocol header. */ +struct ec_params_ucsi_ppm_set { + uint16_t offset; + uint8_t data[]; +} __ec_align2; + +#define EC_CMD_UCSI_PPM_GET 0x0141 + +/* For 'GET' sub-commands, data will be returned as a raw payload. */ +struct ec_params_ucsi_ppm_get { + uint16_t offset; + uint8_t size; +} __ec_align2; + /*****************************************************************************/ /* The command range 0x200-0x2FF is reserved for Rotor. */ From patchwork Fri Nov 15 15:52:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?=C5=81ukasz_Bartosik?= X-Patchwork-Id: 13876518 Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 712011D516F for ; Fri, 15 Nov 2024 15:52:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731685966; cv=none; b=AtVMQkO2vIplZEzuXCwKDtKS5wW0wNfSXY/3EdpLTKvi0VMTv8yneSDbguNrQjYZoJmaIvNP4SJFTZAkputvjQzm+GF+op/UOqCcEdgHQmpicRAeFHfN8AaB2OVHsX4uL7+Mb6SjkMv5NNR7mpdc+PcHkFrxZHWzg7Xb3ol9qyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731685966; c=relaxed/simple; bh=d8iORzWAkRlRDkNmgQKv2SPq3OHuyHXPg0ZOEguhGJM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=je79ofMtFH1wnst0+H2t2VaQOOy3uODlocQXVoWOsFZ5Ox4npJYUsi+8hAsjk7E4CHGY2sEl+J9BhpsPHj0hLb3dLhuiUMDnIeVh9nD3XQ4j+VbaWDld/9lItkvhMaJRr2DiWqKBJnzkBNuEwykZ5DaKRigBir94rqNhSIcytKM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=icQkLND/; arc=none smtp.client-ip=209.85.218.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="icQkLND/" Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-a9ed0ec0e92so251139366b.0 for ; Fri, 15 Nov 2024 07:52:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1731685963; x=1732290763; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=S+q+FtIFJacUPcbO+g3+ZKujDF+9VdAofyeFCU/U1w8=; b=icQkLND/fxuYpNms+5COO77EFYNA2M2+aBTASlLrdBeCSDi4o1GBQyvJhaVO/jHkfT TZkRZY15eL56aw3XROaZJYbI0bOZBt+aBibOjd3PbNmhqTFUFsw50bFl8f3R6mM8a52x tHiIWcKQSl4wguHh+kGssnSFcJuhfmWL4qorM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731685963; x=1732290763; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=S+q+FtIFJacUPcbO+g3+ZKujDF+9VdAofyeFCU/U1w8=; b=EuetXVmQV1sxGgWLGeNRDYcY9eghKKZgdgM+uOO/7k8yjeGLnPIh19dNntY8Ictoxd pU4sW3GO68kGWEoIXpI/e0UUT5GBg+ctZ9ssYmZEJO16AAwmeMBeO2Bj/aep8+g8z/4S YJrkkwkOFu9sXTt5fE1xCs/13hktq9a+96lREU0XpkiYEyv4QdVnh0UNdMb7WWQMWnbz gKMYr0YyW6W0eJ9Psm9zglRHqjnlkOx2+WhOxIlEVOt4aClYPfg2eM2tNDZHveR/26Se 2rBpiQewKWvqDwjV3NWmO+Ul5EWMqGbKlMwTpOBj0syTY0Y1NGTPPC1fyYR4+XM80NdY JCTQ== X-Forwarded-Encrypted: i=1; AJvYcCXaWq/JBr/iWyF6V5qo1QMJuZiGx32IqoPHEAPOR+1LnoxpoqJJR9tW6ewevO0zp63+ueulgnALpSA=@vger.kernel.org X-Gm-Message-State: AOJu0YxneCGYDVzmkq3cLgdCRx/fepz7O7jP6pSRmc7JGPPflFPQZnLG h5HnETuQPaiZXB/11O2DdHKrakKoL3UZvAY8i2kWYPd1UYrhiD0phHZkOqOgBLQJSwbI0pxBqaI 1 X-Google-Smtp-Source: AGHT+IFc0M/KiyuuTODMgLJo3wXWPFDMf3upJHgE707Z09SNTiFQ5GLlRsAom2N2VumW7iN4ajfUTA== X-Received: by 2002:a17:907:3e0f:b0:a7a:9f0f:ab18 with SMTP id a640c23a62f3a-aa48341caa6mr267874366b.20.1731685962541; Fri, 15 Nov 2024 07:52:42 -0800 (PST) Received: from ukaszb-ng.c.googlers.com.com (103.45.147.34.bc.googleusercontent.com. [34.147.45.103]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa20e086244sm187758166b.177.2024.11.15.07.52.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2024 07:52:40 -0800 (PST) From: =?utf-8?q?=C5=81ukasz_Bartosik?= To: Heikki Krogerus , Greg Kroah-Hartman , Benson Leung Cc: Abhishek Pandit-Subedi , Jameson Thies , Pavan Holla , Tzung-Bi Shih , linux-usb@vger.kernel.org, chrome-platform@lists.linux.dev Subject: [PATCH v7 2/3] usb: typec: ucsi: Implement ChromeOS UCSI driver Date: Fri, 15 Nov 2024 15:52:33 +0000 Message-ID: <20241115155234.1587589-3-ukaszb@chromium.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241115155234.1587589-1-ukaszb@chromium.org> References: <20241115155234.1587589-1-ukaszb@chromium.org> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Pavan Holla Implementation of a UCSI transport driver for ChromeOS. This driver will be loaded if the ChromeOS EC implements a PPM. Signed-off-by: Pavan Holla Co-developed-by: Abhishek Pandit-Subedi Signed-off-by: Abhishek Pandit-Subedi Co-developed-by: Łukasz Bartosik Signed-off-by: Łukasz Bartosik --- MAINTAINERS | 7 + drivers/usb/typec/ucsi/Kconfig | 13 ++ drivers/usb/typec/ucsi/Makefile | 1 + drivers/usb/typec/ucsi/cros_ec_ucsi.c | 248 ++++++++++++++++++++++++++ 4 files changed, 269 insertions(+) create mode 100644 drivers/usb/typec/ucsi/cros_ec_ucsi.c diff --git a/MAINTAINERS b/MAINTAINERS index 21fdaa19229a..b5c57bb68c44 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5389,6 +5389,13 @@ L: chrome-platform@lists.linux.dev S: Maintained F: drivers/watchdog/cros_ec_wdt.c +CHROMEOS UCSI DRIVER +M: Abhishek Pandit-Subedi +M: Łukasz Bartosik +L: chrome-platform@lists.linux.dev +S: Maintained +F: drivers/usb/typec/ucsi/cros_ec_ucsi.c + CHRONTEL CH7322 CEC DRIVER M: Joe Tessler L: linux-media@vger.kernel.org diff --git a/drivers/usb/typec/ucsi/Kconfig b/drivers/usb/typec/ucsi/Kconfig index 680e1b87b152..75559601fe8f 100644 --- a/drivers/usb/typec/ucsi/Kconfig +++ b/drivers/usb/typec/ucsi/Kconfig @@ -69,6 +69,19 @@ config UCSI_PMIC_GLINK To compile the driver as a module, choose M here: the module will be called ucsi_glink. +config CROS_EC_UCSI + tristate "UCSI Driver for ChromeOS EC" + depends on MFD_CROS_EC_DEV + depends on CROS_USBPD_NOTIFY + depends on !EXTCON_TCSS_CROS_EC + default MFD_CROS_EC_DEV + help + This driver enables UCSI support for a ChromeOS EC. The EC is + expected to implement a PPM. + + To compile the driver as a module, choose M here: the module + will be called cros_ec_ucsi. + config UCSI_LENOVO_YOGA_C630 tristate "UCSI Interface Driver for Lenovo Yoga C630" depends on EC_LENOVO_YOGA_C630 diff --git a/drivers/usb/typec/ucsi/Makefile b/drivers/usb/typec/ucsi/Makefile index aed41d23887b..be98a879104d 100644 --- a/drivers/usb/typec/ucsi/Makefile +++ b/drivers/usb/typec/ucsi/Makefile @@ -21,4 +21,5 @@ obj-$(CONFIG_UCSI_ACPI) += ucsi_acpi.o obj-$(CONFIG_UCSI_CCG) += ucsi_ccg.o obj-$(CONFIG_UCSI_STM32G0) += ucsi_stm32g0.o obj-$(CONFIG_UCSI_PMIC_GLINK) += ucsi_glink.o +obj-$(CONFIG_CROS_EC_UCSI) += cros_ec_ucsi.o obj-$(CONFIG_UCSI_LENOVO_YOGA_C630) += ucsi_yoga_c630.o diff --git a/drivers/usb/typec/ucsi/cros_ec_ucsi.c b/drivers/usb/typec/ucsi/cros_ec_ucsi.c new file mode 100644 index 000000000000..aae0bf106494 --- /dev/null +++ b/drivers/usb/typec/ucsi/cros_ec_ucsi.c @@ -0,0 +1,248 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * UCSI driver for ChromeOS EC + * + * Copyright 2024 Google LLC. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ucsi.h" + +/* + * Maximum size in bytes of a UCSI message between AP and EC + */ +#define MAX_EC_DATA_SIZE 256 + +/* + * Maximum time in miliseconds the cros_ec_ucsi driver + * will wait for a response to a command or and ack. + */ +#define WRITE_TMO_MS 5000 + +struct cros_ucsi_data { + struct device *dev; + struct ucsi *ucsi; + + struct cros_ec_device *ec; + struct notifier_block nb; + struct work_struct work; + + struct completion complete; + unsigned long flags; +}; + +static int cros_ucsi_read(struct ucsi *ucsi, unsigned int offset, void *val, + size_t val_len) +{ + struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi); + struct ec_params_ucsi_ppm_get req = { + .offset = offset, + .size = val_len, + }; + int ret; + + if (val_len > MAX_EC_DATA_SIZE) { + dev_err(udata->dev, "Can't read %zu bytes. Too big.", val_len); + return -EINVAL; + } + + ret = cros_ec_cmd(udata->ec, 0, EC_CMD_UCSI_PPM_GET, + &req, sizeof(req), val, val_len); + if (ret < 0) { + dev_warn(udata->dev, "Failed to send EC message UCSI_PPM_GET: error=%d", ret); + return ret; + } + return 0; +} + +static int cros_ucsi_read_version(struct ucsi *ucsi, u16 *version) +{ + return cros_ucsi_read(ucsi, UCSI_VERSION, version, sizeof(*version)); +} + +static int cros_ucsi_read_cci(struct ucsi *ucsi, u32 *cci) +{ + return cros_ucsi_read(ucsi, UCSI_CCI, cci, sizeof(*cci)); +} + +static int cros_ucsi_read_message_in(struct ucsi *ucsi, void *val, + size_t val_len) +{ + return cros_ucsi_read(ucsi, UCSI_MESSAGE_IN, val, val_len); +} + +static int cros_ucsi_async_control(struct ucsi *ucsi, u64 cmd) +{ + struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi); + u8 ec_buf[sizeof(struct ec_params_ucsi_ppm_set) + sizeof(cmd)]; + struct ec_params_ucsi_ppm_set *req = (struct ec_params_ucsi_ppm_set *) ec_buf; + int ret; + + req->offset = UCSI_CONTROL; + memcpy(req->data, &cmd, sizeof(cmd)); + ret = cros_ec_cmd(udata->ec, 0, EC_CMD_UCSI_PPM_SET, + req, sizeof(ec_buf), NULL, 0); + if (ret < 0) { + dev_warn(udata->dev, "Failed to send EC message UCSI_PPM_SET: error=%d", ret); + return ret; + } + return 0; +} + +static int cros_ucsi_sync_control(struct ucsi *ucsi, u64 cmd) +{ + return ucsi_sync_control_common(ucsi, cmd); +} + +struct ucsi_operations cros_ucsi_ops = { + .read_version = cros_ucsi_read_version, + .read_cci = cros_ucsi_read_cci, + .read_message_in = cros_ucsi_read_message_in, + .async_control = cros_ucsi_async_control, + .sync_control = cros_ucsi_sync_control, +}; + +static void cros_ucsi_work(struct work_struct *work) +{ + struct cros_ucsi_data *udata = container_of(work, struct cros_ucsi_data, work); + u32 cci; + + if (cros_ucsi_read_cci(udata->ucsi, &cci)) + return; + + ucsi_notify_common(udata->ucsi, cci); +} + +static int cros_ucsi_event(struct notifier_block *nb, + unsigned long host_event, void *_notify) +{ + struct cros_ucsi_data *udata = container_of(nb, struct cros_ucsi_data, nb); + + if (!(host_event & PD_EVENT_PPM)) + return NOTIFY_OK; + + dev_dbg(udata->dev, "UCSI notification received"); + flush_work(&udata->work); + schedule_work(&udata->work); + + return NOTIFY_OK; +} + +static void cros_ucsi_destroy(struct cros_ucsi_data *udata) +{ + cros_usbpd_unregister_notify(&udata->nb); + cancel_work_sync(&udata->work); + ucsi_destroy(udata->ucsi); +} + +static int cros_ucsi_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct cros_ec_dev *ec_data = dev_get_drvdata(dev->parent); + struct cros_ucsi_data *udata; + int ret; + + udata = devm_kzalloc(dev, sizeof(*udata), GFP_KERNEL); + if (!udata) + return -ENOMEM; + + udata->dev = dev; + + udata->ec = ec_data->ec_dev; + if (!udata->ec) { + dev_err(dev, "couldn't find parent EC device"); + return -ENODEV; + } + + platform_set_drvdata(pdev, udata); + + INIT_WORK(&udata->work, cros_ucsi_work); + init_completion(&udata->complete); + + udata->ucsi = ucsi_create(dev, &cros_ucsi_ops); + if (IS_ERR(udata->ucsi)) { + dev_err(dev, "failed to allocate UCSI instance"); + return PTR_ERR(udata->ucsi); + } + + ucsi_set_drvdata(udata->ucsi, udata); + + udata->nb.notifier_call = cros_ucsi_event; + ret = cros_usbpd_register_notify(&udata->nb); + if (ret) { + dev_err(dev, "failed to register notifier: error=%d", ret); + ucsi_destroy(udata->ucsi); + return ret; + } + + ret = ucsi_register(udata->ucsi); + if (ret) { + dev_err(dev, "failed to register UCSI: error=%d", ret); + cros_ucsi_destroy(udata); + return ret; + } + + return 0; +} + +static void cros_ucsi_remove(struct platform_device *dev) +{ + struct cros_ucsi_data *udata = platform_get_drvdata(dev); + + ucsi_unregister(udata->ucsi); + cros_ucsi_destroy(udata); +} + +static int __maybe_unused cros_ucsi_suspend(struct device *dev) +{ + struct cros_ucsi_data *udata = dev_get_drvdata(dev); + + cancel_work_sync(&udata->work); + + return 0; +} + +static void __maybe_unused cros_ucsi_complete(struct device *dev) +{ + struct cros_ucsi_data *udata = dev_get_drvdata(dev); + + ucsi_resume(udata->ucsi); +} + +static const struct dev_pm_ops cros_ucsi_pm_ops = { +#ifdef CONFIG_PM_SLEEP + .suspend = cros_ucsi_suspend, + .complete = cros_ucsi_complete, +#endif +}; + +static const struct platform_device_id cros_ucsi_id[] = { + { KBUILD_MODNAME, 0 }, + {} +}; +MODULE_DEVICE_TABLE(platform, cros_ucsi_id); + +static struct platform_driver cros_ucsi_driver = { + .driver = { + .name = KBUILD_MODNAME, + .pm = &cros_ucsi_pm_ops, + }, + .id_table = cros_ucsi_id, + .probe = cros_ucsi_probe, + .remove = cros_ucsi_remove, +}; + +module_platform_driver(cros_ucsi_driver); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("UCSI driver for ChromeOS EC"); From patchwork Fri Nov 15 15:52:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?=C5=81ukasz_Bartosik?= X-Patchwork-Id: 13876519 Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB1E31D5ADC for ; Fri, 15 Nov 2024 15:52:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731685966; cv=none; b=VFxV+MX+kkYX3llOxvTPkBeqsgpRW9q2dUvQ4GGcjouwYMBHceZBz81vvHeMAqd5glhBpcMOiPTCfkQDss39m4Iucrx5eMAyxj8DH6uuR1Vhpu3VFhTC8+lkw9qiCdreqvdcwgSzSO6Jdj0DRfrvQHxdYoY2y1m3t17waxi2Nzk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731685966; c=relaxed/simple; bh=IA0lrnNas0YoO/jbHCyNZSl7uiH/6eRgBzzm6A//b1A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=acFgxqR2vnhFFixMgV5jlZm8fkrmEMoALB9TpegOGB1HfBvUDhpBbwd+kzaheBLTgoZ6Xnp8sqae2xunwF//gTD9lbQMaLzlFBllzRKimOcTnjHdxop/LVd4xM+K8MZwU4cMxtFbwMJ0jloL6wsMzNvvKiLVUY3OVyfU4/xxXXk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Dpij7YxI; arc=none smtp.client-ip=209.85.208.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Dpij7YxI" Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-5cf7b79c6a6so2825071a12.0 for ; Fri, 15 Nov 2024 07:52:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1731685963; x=1732290763; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=l+h/4OtNQOmknexdS73X7hyoeknIzANxsH8Xqm1Epi0=; b=Dpij7YxIZ4wLGG9S4Z23L3K5noCBMVWvNmn44HIf/G/oFzTVhsLpKbJXPH7NrLFnyS UvEdigEWpi/f/lcYfQ9ehuRlRiD6eC+HmbKjfg5A0TLrW0gkWF0r00aayF5zoSKLICXh lhM8T8a9KtlEvy8q+IUPFq253fKQ6VEpJH0ks= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731685963; x=1732290763; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=l+h/4OtNQOmknexdS73X7hyoeknIzANxsH8Xqm1Epi0=; b=Ho6NE/Aj19HP9FqB0M44hW9f1Q+v/EW1DzNxYN26Ri4JXLET3PELd2S3a6ESeo2bUA ScpN+7nsifw10mSofcq4utpoRzYSGXWYuvZs0iDjW47TECdykGQDWwzXne30jSWLTr9X 8I3VytB3Jp2mIZ7ns56RDDNKNpgZVQ6Aox03ISkVrASwDEWm3cQIdQhc1YdMkGzMQxw/ yEbvZ2e2Fhdo9OiUJpGlarxDFGcwgYEWEcUsBdC+QSoLHGtLpNThSXPDEL4desu3BQMq SbCdCgfj3umBzxtx0uJrghs2UrxU8EDJgsffqxlehCbxt4zNbeNUqhBnFOyxqL72OKPf ousQ== X-Forwarded-Encrypted: i=1; AJvYcCUAmVSC2n1cFCCPAc0PmmgQCMvRsnQlkZ2w++6dXVLCAISToTSclGXSPFUFqV2zM15oLW9PWi5RvEw=@vger.kernel.org X-Gm-Message-State: AOJu0YxdJ9qKF8VxJkU5QbyeJ5Yj9O74GPDfQezno0NAmaaBI3Nh3bT1 WlaeMtPRhmZ2BALqf1IFkhV9fusJYBEEy106YJL9ShF2HiNUwN40ciZYywh+ X-Google-Smtp-Source: AGHT+IHIW/jLgqdVPuNl3GJnBFPcKnN15+ULgCZmftrtTOho9nPQQD34rXutZqYJHJ2kxZxYaBb9qw== X-Received: by 2002:a17:907:2d93:b0:a9e:82f3:d4ab with SMTP id a640c23a62f3a-aa207687241mr701475266b.9.1731685963146; Fri, 15 Nov 2024 07:52:43 -0800 (PST) Received: from ukaszb-ng.c.googlers.com.com (103.45.147.34.bc.googleusercontent.com. [34.147.45.103]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa20e086244sm187758166b.177.2024.11.15.07.52.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2024 07:52:42 -0800 (PST) From: =?utf-8?q?=C5=81ukasz_Bartosik?= To: Heikki Krogerus , Greg Kroah-Hartman , Benson Leung Cc: Abhishek Pandit-Subedi , Jameson Thies , Pavan Holla , Tzung-Bi Shih , linux-usb@vger.kernel.org, chrome-platform@lists.linux.dev Subject: [PATCH v7 3/3] usb: typec: cros_ec_ucsi: Recover from write timeouts Date: Fri, 15 Nov 2024 15:52:34 +0000 Message-ID: <20241115155234.1587589-4-ukaszb@chromium.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241115155234.1587589-1-ukaszb@chromium.org> References: <20241115155234.1587589-1-ukaszb@chromium.org> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Abhishek Pandit-Subedi In a suspend-resume edge case, the OPM is timing out in ucsi_resume and the PPM is getting stuck waiting for a command complete ack. Add a write timeout recovery task that will get us out of this state. Signed-off-by: Abhishek Pandit-Subedi Signed-off-by: Łukasz Bartosik --- drivers/usb/typec/ucsi/cros_ec_ucsi.c | 83 ++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/drivers/usb/typec/ucsi/cros_ec_ucsi.c b/drivers/usb/typec/ucsi/cros_ec_ucsi.c index aae0bf106494..3e6b59aba275 100644 --- a/drivers/usb/typec/ucsi/cros_ec_ucsi.c +++ b/drivers/usb/typec/ucsi/cros_ec_ucsi.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -29,6 +30,9 @@ */ #define WRITE_TMO_MS 5000 +/* Number of times to attempt recovery from a write timeout before giving up. */ +#define WRITE_TMO_CTR_MAX 5 + struct cros_ucsi_data { struct device *dev; struct ucsi *ucsi; @@ -36,6 +40,8 @@ struct cros_ucsi_data { struct cros_ec_device *ec; struct notifier_block nb; struct work_struct work; + struct delayed_work write_tmo; + int tmo_counter; struct completion complete; unsigned long flags; @@ -101,7 +107,33 @@ static int cros_ucsi_async_control(struct ucsi *ucsi, u64 cmd) static int cros_ucsi_sync_control(struct ucsi *ucsi, u64 cmd) { - return ucsi_sync_control_common(ucsi, cmd); + struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi); + int ret; + + ret = ucsi_sync_control_common(ucsi, cmd); + if (ret) + goto out; + + /* Successful write. Cancel any pending recovery work. */ + cancel_delayed_work_sync(&udata->write_tmo); + + return 0; +out: + /* EC may return -EBUSY if CCI.busy is set. Convert this to a timeout. + */ + if (ret == -EBUSY) + ret = -ETIMEDOUT; + + /* Schedule recovery attempt when we timeout or tried to send a command + * while still busy. + */ + if (ret == -ETIMEDOUT) { + cancel_delayed_work_sync(&udata->write_tmo); + schedule_delayed_work(&udata->write_tmo, + msecs_to_jiffies(WRITE_TMO_MS)); + } + + return ret; } struct ucsi_operations cros_ucsi_ops = { @@ -123,6 +155,54 @@ static void cros_ucsi_work(struct work_struct *work) ucsi_notify_common(udata->ucsi, cci); } +static void cros_ucsi_write_timeout(struct work_struct *work) +{ + struct cros_ucsi_data *udata = + container_of(work, struct cros_ucsi_data, write_tmo.work); + u32 cci; + u64 cmd; + + if (cros_ucsi_read(udata->ucsi, UCSI_CCI, &cci, sizeof(cci))) { + dev_err(udata->dev, + "Reading CCI failed; no write timeout recovery possible."); + return; + } + + if (cci & UCSI_CCI_BUSY) { + udata->tmo_counter++; + + if (udata->tmo_counter <= WRITE_TMO_CTR_MAX) + schedule_delayed_work(&udata->write_tmo, + msecs_to_jiffies(WRITE_TMO_MS)); + else + dev_err(udata->dev, + "PPM unresponsive - too many write timeouts."); + + return; + } + + /* No longer busy means we can reset our timeout counter. */ + udata->tmo_counter = 0; + + /* Need to ack previous command which may have timed out. */ + if (cci & UCSI_CCI_COMMAND_COMPLETE) { + cmd = UCSI_ACK_CC_CI | UCSI_ACK_COMMAND_COMPLETE; + cros_ucsi_async_control(udata->ucsi, &cmd); + + /* Check again after a few seconds that the system has + * recovered to make sure our async write above was successful. + */ + schedule_delayed_work(&udata->write_tmo, + msecs_to_jiffies(WRITE_TMO_MS)); + return; + } + + /* We recovered from a previous timeout. Treat this as a recovery from + * suspend and call resume. + */ + ucsi_resume(udata->ucsi); +} + static int cros_ucsi_event(struct notifier_block *nb, unsigned long host_event, void *_notify) { @@ -167,6 +247,7 @@ static int cros_ucsi_probe(struct platform_device *pdev) platform_set_drvdata(pdev, udata); INIT_WORK(&udata->work, cros_ucsi_work); + INIT_DELAYED_WORK(&udata->write_tmo, cros_ucsi_write_timeout); init_completion(&udata->complete); udata->ucsi = ucsi_create(dev, &cros_ucsi_ops);