From patchwork Tue Dec 10 14:45:26 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: 13901551 Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.47]) (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 324F170806 for ; Tue, 10 Dec 2024 14:45:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733841944; cv=none; b=FnABmeKKmgjUfzz98qYsMPfRL4wHrfWzW/XdHpy80IdZFx2p5LD27An5mkBIEYSlgC5RM481ORrhDr3TwiYnoYEDJoMe9CEGU3i1fyKntNeVZIas8aWVKGN39VZ+GskG1+T4+rKOK9VhE1YJLa8IqzCkpkXGxofhyvwKthYgRGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733841944; c=relaxed/simple; bh=FsVPWUYW3r8+vYPca7TkDYiQezN0cxbYZZeUC077jgw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bDhc6ktSrvmt5H/pqunsVf5h/aQ68NCKO50wbNZYCaMeAb1tN43kBIrgDWCbdBz5PZWFtiCxDjwHti1XKN3SBTtkygC8vrXOHABcx1PIMUDFqeSxj7hAS90vuyWVqDPRvHeAskzxXTAiXu1mn1c1F3/bO/AIKdvXS5gsvUGg3s0= 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=NM0TW/in; arc=none smtp.client-ip=209.85.208.47 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="NM0TW/in" Received: by mail-ed1-f47.google.com with SMTP id 4fb4d7f45d1cf-5d3f249f3b2so3418674a12.3 for ; Tue, 10 Dec 2024 06:45:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1733841939; x=1734446739; darn=lists.linux.dev; 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=/VBmObv79eNi+4KOGkigLjUPpqUoMrzCTZAwaSnc+fc=; b=NM0TW/inG8nucdp591h3DmzdKWjXa0LMx+gMp0QXq3WQt2igo/y3MAB+Q7siERwCam UGDdr2oyGe0OdF1sHTdZCqSSgV3zeQQ3h4IhBNvAAQtArZo8mXH2lIeBAnRKSqg7XA/s soTCnT7JCv5TbvZXTWUo/33c8b22ammnoOI2U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733841939; x=1734446739; 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=/VBmObv79eNi+4KOGkigLjUPpqUoMrzCTZAwaSnc+fc=; b=kE/XnDYoS9KMfhkkLo1vgQL/eoXqx9DSYYkRSvquPejaFGUTKFCs6LgwJ5sxND9YH0 LQQdiQSID8iLNm5dkFa4Xf1nQY8L5rA9hcHIiKpeN3670vJ1gRt7KoyjaEbz1j6HgTSS gkiaaEFEqpGNv4eCBX4cc2zh7wE1AseSgzz82Rq+vCUgNl+EWlFGUrO4boeTAlNZe35a gWvDwRwl1EMElEnFERH9MxwWe2J71hB+ZjD4/ELJKQsclJsxtDWHej9P+NZ8ul1fE7rE UD7To9TXiHpByRNc5E2MjDx7zJdsR8bqwwcCcjeI5ccyBYx51m/ickuhFAxPIMP0a92/ X3jw== X-Forwarded-Encrypted: i=1; AJvYcCUdg2JPSSv9iCXBKVw+NPV+CPasU2QvzNCA+KzCeH+7U5xQK1rGIG/Trc7p1WdvkBecd+bToBcRv0nM0754J6Y=@lists.linux.dev X-Gm-Message-State: AOJu0Yz+VEg/sD3ztGlar2s/mjzvHOTcFk1VHSVIDZLa5NWUx+BgCN/S DGko4RwazLtnxf0nRBT6n8xfebVo1QdkDDC33WYQyjaKgCvz48Mpxtm9v3Tf X-Gm-Gg: ASbGncvOog0RgRB+Zt34U8y83z9oFPOzcPon/OWie/J6byZ272XUQYYFZdyePpljvZN PaAUB1z7dFqicqnX3LzpvO8lU9buzf3YchGL+7z5HRfifoK82XwZmCD9MhkQpw4rxmnCSciAaZy ctkjjEF0mxjLtN5Ux8yWub4gS/LaBnWK30MgkpN+H1Zk4iyPNxbpt7RO6bwLYQpkuJhOOQpRJdX AhmA70rC22N14pls8zD1NHfSZ2VFtSqqLTumMMv4q+PfhoDOBLc66KiTYtbW2vp0nIO0zsCtXUI QH5b8BiNQKpNOXr3Nak3EYHBu81dinQWEYvgluw= X-Google-Smtp-Source: AGHT+IGq2WVqY9FHE4Kw8mZJ334PewE8m0Sr0rxYDq1ur49OrklFDakgDoOr7S8rT9zpBAXeMwU8Vw== X-Received: by 2002:a05:6402:5241:b0:5d0:81f5:a398 with SMTP id 4fb4d7f45d1cf-5d4184dd140mr11278224a12.1.1733841939141; Tue, 10 Dec 2024 06:45:39 -0800 (PST) Received: from ukaszb-ng.c.googlers.com.com (208.220.32.34.bc.googleusercontent.com. [34.32.220.208]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa69dd81272sm168996966b.161.2024.12.10.06.45.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Dec 2024 06:45:38 -0800 (PST) From: =?utf-8?q?=C5=81ukasz_Bartosik?= To: Heikki Krogerus , Greg Kroah-Hartman , Benson Leung , Dmitry Baryshkov Cc: Abhishek Pandit-Subedi , Jameson Thies , Pavan Holla , Tzung-Bi Shih , linux-usb@vger.kernel.org, chrome-platform@lists.linux.dev Subject: [PATCH v11 1/2] platform/chrome: Update ChromeOS EC header for UCSI Date: Tue, 10 Dec 2024 14:45:26 +0000 Message-ID: <20241210144527.1657888-2-ukaszb@chromium.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241210144527.1657888-1-ukaszb@chromium.org> References: <20241210144527.1657888-1-ukaszb@chromium.org> Precedence: bulk X-Mailing-List: chrome-platform@lists.linux.dev 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 b3c4993e656e..ecf290a0c98f 100644 --- a/include/linux/platform_data/cros_ec_commands.h +++ b/include/linux/platform_data/cros_ec_commands.h @@ -5044,8 +5044,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 */ @@ -6105,6 +6108,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 Tue Dec 10 14:45:27 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: 13901552 Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) (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 D56381A0BD1 for ; Tue, 10 Dec 2024 14:45:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733841944; cv=none; b=CtDTjZRp1bTakofqdfflNOQztuMvDkaz6/H3EtraUhwLn9fdrChKLK4LPrIFKhX0avAnuHsyPXTN1kShwIY6Bj/KugKoKHVCw94p9MC4eYCXfa4I80Taz0Eq7xemFcNbXijOYW5vSS1CX2Q64wLgpIDE/nZNopeVkPO7znNgcdI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733841944; c=relaxed/simple; bh=rO7+A397vKDDcv2sWjFDy8so8mFvoNlkMmSfxTfNakw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ZJU+0waq7leWLKebh755gV1ZbxJnHBDRHkQ+6xcASSlcm04UgJmIxqSk5jSRAzUm2upflLB0K4FGg1Of6sb1rW6/+m6vsziqvMZM5K7PYoqUNCGz0dcxnd/H9g1K5leo9rV7MJL9e35ALeEjqUgXVt9nLd0ue846srqLse2NMlQ= 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=BSrouZnw; arc=none smtp.client-ip=209.85.218.54 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="BSrouZnw" Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-aa6aad76beeso54415666b.2 for ; Tue, 10 Dec 2024 06:45:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1733841941; x=1734446741; darn=lists.linux.dev; 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=bKflouy+Iu7oUzSVMwZq0S0qrOOmgN0EQe/PJ5wz+VQ=; b=BSrouZnwFfnIMMJWuuCLOmg2nsCAvFesP4pkTMV3y8IfHEalVXFemrof+bk3OmXU3U /UdUygeGlYw/U7gy84feSgKXonHJ9cvdyXMBsyja41AOpuJ5psVPTHB8htXjheB+dBaB OVmtVcHhBru/1WgHtX5LAQXuwrSKqTEX5RelA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733841941; x=1734446741; 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=bKflouy+Iu7oUzSVMwZq0S0qrOOmgN0EQe/PJ5wz+VQ=; b=oxR5bcEaJ1mnZVfT3EYLCh80lzufZIJPtN7lM8LIGaEirl847U0mOcdPJUsjYPCjft g4GIAIf8QCbuk7vLitbQ7UL7fnrOmcjpJ6GFaEW019wNP9qQ93LtKzvkg5mDE/vApttD 1iJDeye85j87EWBxngesoIPdFB6NoUdUBQn2gtlGWsUP6yfZ+q2J/jvggNdyEAyLPKZD W/lpxMSw3inIS1RJprLd6fJ9QoVjaRi3MlsjO0xpcbxWddnJhcpXskJGnG25oSgt465u 2AS2NuXGL1XxyumsjhDRjnvnmBode8TJ6cksPrq7VIUMp2K02JOTz2NXXOQZHGqf4GUL EAQw== X-Forwarded-Encrypted: i=1; AJvYcCXFv6GK+TmuoihtAL9+per/7b4Xii1iU+DW9cWHljqMBcjtGBx/GYu4cZ9MvbQ3BIm2XvF0RPx6Cq677Wde/B8=@lists.linux.dev X-Gm-Message-State: AOJu0YwuheRNHjlHsRV+OR9FyeZ6zfe1jhseY1mu3ufqmH3qonnmFW7R XCDlNDtOr06pSJ9c3GBzzkWPgWrwgO7iAK60Mc2WuY+lmAVX8UR5PwAknBbU X-Gm-Gg: ASbGncvu5PkVEonnngoAZa/4kq7ZzFkVKexSfvg6Ugb+kNLkhbAZRwF852WrXDVnK4o R7X++2ceCsh3+iSITP+HOAdTAW8kmGKdRh9qVgCH6bUn+yW7g83nXU1OU96v1WTvVKb5l/YUI23 YhMk9DwTL+y2X8Tt0OKxsUXGpr4QrzQeAeLbAHdqsne/Qiwpv3QnLhAuo9ECoMCWNUqsHG5lwYi 3AhWwDivY2MwozY+NEp6UEd9sPA4PlYGI233kB3YDwTG7f/cU6XZmSfj9gkC7Iqa+azOCdPi8ag VqXWDvFUIBwDKoMcfcUP0+A7M+5slb6KLpMVakA= X-Google-Smtp-Source: AGHT+IHK1lej+n8b/2uhhrAO5Mnpj48iBnznE2JuotPLpqkFjPISPPhcavi9CZmsEZ+v5BsFNM6NOA== X-Received: by 2002:a17:906:c4d1:b0:aa6:a05c:b06d with SMTP id a640c23a62f3a-aa6a05cb251mr334174766b.55.1733841941016; Tue, 10 Dec 2024 06:45:41 -0800 (PST) Received: from ukaszb-ng.c.googlers.com.com (208.220.32.34.bc.googleusercontent.com. [34.32.220.208]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa69dd81272sm168996966b.161.2024.12.10.06.45.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Dec 2024 06:45:39 -0800 (PST) From: =?utf-8?q?=C5=81ukasz_Bartosik?= To: Heikki Krogerus , Greg Kroah-Hartman , Benson Leung , Dmitry Baryshkov Cc: Abhishek Pandit-Subedi , Jameson Thies , Pavan Holla , Tzung-Bi Shih , linux-usb@vger.kernel.org, chrome-platform@lists.linux.dev Subject: [PATCH v11 2/2] usb: typec: ucsi: Implement ChromeOS UCSI driver Date: Tue, 10 Dec 2024 14:45:27 +0000 Message-ID: <20241210144527.1657888-3-ukaszb@chromium.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241210144527.1657888-1-ukaszb@chromium.org> References: <20241210144527.1657888-1-ukaszb@chromium.org> Precedence: bulk X-Mailing-List: chrome-platform@lists.linux.dev 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 Reviewed-by: Dmitry Baryshkov Reviewed-by: Heikki Krogerus --- MAINTAINERS | 7 + drivers/usb/typec/ucsi/Kconfig | 13 + drivers/usb/typec/ucsi/Makefile | 1 + drivers/usb/typec/ucsi/cros_ec_ucsi.c | 337 ++++++++++++++++++++++++++ 4 files changed, 358 insertions(+) create mode 100644 drivers/usb/typec/ucsi/cros_ec_ucsi.c diff --git a/MAINTAINERS b/MAINTAINERS index 17daa9ee9384..6d24db48421d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5454,6 +5454,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..55b7ccc3f4e1 --- /dev/null +++ b/drivers/usb/typec/ucsi/cros_ec_ucsi.c @@ -0,0 +1,337 @@ +// 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 + +#include "ucsi.h" + +/* + * Maximum size in bytes of a UCSI message between AP and EC + */ +#define MAX_EC_DATA_SIZE 256 + +/* + * Maximum time in milliseconds the cros_ec_ucsi driver + * will wait for a response to a command or and ack. + */ +#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; + + 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; +}; + +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) +{ + struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi); + int ret; + + ret = ucsi_sync_control_common(ucsi, cmd); + switch (ret) { + case -EBUSY: + /* EC may return -EBUSY if CCI.busy is set. + * Convert this to a timeout. + */ + case -ETIMEDOUT: + /* Schedule recovery attempt when we timeout + * or tried to send a command while still busy. + */ + cancel_delayed_work_sync(&udata->write_tmo); + schedule_delayed_work(&udata->write_tmo, + msecs_to_jiffies(WRITE_TMO_MS)); + break; + case 0: + /* Successful write. Cancel any pending recovery work. */ + cancel_delayed_work_sync(&udata->write_tmo); + break; + } + + return ret; +} + +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 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) +{ + 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_delayed_work_sync(&udata->write_tmo); + 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_DELAYED_WORK(&udata->write_tmo, cros_ucsi_write_timeout); + 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_delayed_work_sync(&udata->write_tmo); + 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); +} + +/* + * UCSI protocol is also used on ChromeOS platforms which reply on + * cros_ec_lpc.c driver for communication with embedded controller (EC). + * On such platforms communication with the EC is not available until + * the .complete() callback of the cros_ec_lpc driver is executed. + * For this reason we delay ucsi_resume() until the .complete() stage + * otherwise UCSI SET_NOTIFICATION_ENABLE command will fail and we won't + * receive any UCSI notifications from the EC where PPM is implemented. + */ +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");