From patchwork Tue Apr 9 02:27:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavan Holla X-Patchwork-Id: 13621720 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 176BE4F5E6 for ; Tue, 9 Apr 2024 02:27:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712629662; cv=none; b=lROgJXJWwvKf7wj9NwAPYKia4HYoHew8+OE1ZIzV40qcH/NUTDRm3K18kvPlFTpL819NvkJEUSTJqsCbac6zIw1bdz1r5PoA2ab9GrVmZoMvsDREMJH8fN++ZGWORAVYIxx7nQen+BoGt9BdEeqRprl77nHXSGEpUdGcwUPwgRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712629662; c=relaxed/simple; bh=qqGF9xLxJC4LQcm92S4IX/hRHYqAmBrg8Pca6o1qles=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BnL+7IX9mSxy8HYbVUbNF6tO3BLYZficPVfu4NU4NMyb0R6NBVG4DBB1jXCb5awFXg9e7aY3kOL+t7O9tVzH7glmILfDKewc7jOk7/hXA1/TiPLj8ZznIT887enH7vted598c0dpsKPjjHXR97OvW13VTETA0OhukXqw0jX/zeI= 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=YP6dSA3R; arc=none smtp.client-ip=209.85.214.181 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="YP6dSA3R" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1e3e84a302eso15788265ad.0 for ; Mon, 08 Apr 2024 19:27:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1712629660; x=1713234460; darn=lists.linux.dev; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=FJlHPBrdTvEXRp/sm58R6lF6ryZ2TinFdRAYOgnph1w=; b=YP6dSA3RoOKit/78Vg+5AdrblStbZs7jBcYHeOB1+DyIhbSFRnftdB093HHoXGWL/g xwRS4/X4pqeZGKXW2jLnsc3vg8EQGEGwWj1axkaum0/7sLnuTsXCYJHB8nJg0YVtviec 7X3T1+ANSn82aJLolUbYNEQXJChWmxg/Q08J0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712629660; x=1713234460; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FJlHPBrdTvEXRp/sm58R6lF6ryZ2TinFdRAYOgnph1w=; b=CvqpDQAMz5dNAdDnAeV4Qu64J9PK8zXvHRzsfDFTD5I8gUjVMnR12YiXRrRgk/ciwc 8c2gVUVL7i5Ske6HMb7sGTc0J5OA2fCqP6vNii8bb/utPaVElL5Lq+Hs3UoHlCTlQpUZ XZtu2TnSQ9aKpD1+uFCac+N82CWTB6EYy+7UfvotJ70WyBoIn7vzFQPtGl3K1KEOMCoN ybs6sJVZFDGUPzpln6gL7IvsTgOzv7h1pWjXHSVOd/P/2lG4rWwb6AtwzDl1q2J/Cytl ygI5K3Xx3V2tOEdrYmLNSRtNfjxZ+cwQBlPBcBfUKU4GcH+di+X3V5CLAVw0ORdRcPwy RNNQ== X-Forwarded-Encrypted: i=1; AJvYcCXi5ia47MtlPdIGETlY5YTOoX9wIj6Sd3l0NE+BoHwza5pBgojkmCD4MXwAy8PfUfGpTWdowt+qyspbi06uU4TLq7QD2qdt0YBXO+krFEqK X-Gm-Message-State: AOJu0YzxExbSH7j5lbcnBPQpR3S/8POndLZlXEpEquMj+b4uTuptWLZb NE8MmP86wpFF22kN1VzCmLbYg2fJAph5Ih42ptAEt5bJ2kt9WfncM35HyFURyw== X-Google-Smtp-Source: AGHT+IHf4Sv9h3Ugg+GncrCdIfZ/whdR6D0SZ1rLF9ak4Hj03Z+Q7L/YWNHH9bezFfO6GyHZ1cHvXw== X-Received: by 2002:a17:902:d4c9:b0:1e3:e257:2c8d with SMTP id o9-20020a170902d4c900b001e3e2572c8dmr9714710plg.11.1712629660427; Mon, 08 Apr 2024 19:27:40 -0700 (PDT) Received: from pholla1.c.googlers.com (210.73.125.34.bc.googleusercontent.com. [34.125.73.210]) by smtp.gmail.com with ESMTPSA id h7-20020a170902680700b001e2b4f513e1sm7880850plk.106.2024.04.08.19.27.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 19:27:40 -0700 (PDT) From: Pavan Holla Date: Tue, 09 Apr 2024 02:27:36 +0000 Subject: [PATCH v4 1/2] platform/chrome: Update ChromeOS EC header for UCSI Precedence: bulk X-Mailing-List: chrome-platform@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240409-public-ucsi-h-v4-1-e770735222a2@chromium.org> References: <20240409-public-ucsi-h-v4-0-e770735222a2@chromium.org> In-Reply-To: <20240409-public-ucsi-h-v4-0-e770735222a2@chromium.org> To: Greg Kroah-Hartman , Heikki Krogerus , Benson Leung , Tzung-Bi Shih , Guenter Roeck Cc: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Abhishek Pandit-Subedi , chrome-platform@lists.linux.dev, Pavan Holla X-Mailer: b4 0.12.4 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. Signed-off-by: Pavan Holla --- include/linux/platform_data/cros_ec_commands.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/linux/platform_data/cros_ec_commands.h b/include/linux/platform_data/cros_ec_commands.h index ecc47d5fe239..c0f6d054a566 100644 --- a/include/linux/platform_data/cros_ec_commands.h +++ b/include/linux/platform_data/cros_ec_commands.h @@ -4933,6 +4933,8 @@ 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 */ } __ec_align4; @@ -5994,6 +5996,24 @@ struct ec_response_typec_vdm_response { #undef VDO_MAX_SIZE +/* + * Read/write interface for UCSI OPM <-> PPM communication. + */ +#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 + +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 Apr 9 02:27:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavan Holla X-Patchwork-Id: 13621721 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 EFDE64F5FB for ; Tue, 9 Apr 2024 02:27:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712629664; cv=none; b=FN4gWJDMRs1MdNKbK6v3YM/swqEbe8Yrir1ZczQy7jPgT2/Cjxh/JuskNaFViShZDnwQDvMgRoUi1/rDKiFOKzf0rBZUZavR7dtkyce4bGy0LD8tveAH7LZLoQRupwELZnxg7CGIJChwzNtdnnUk81DEF1ksRm4I2IaqKqFw2AY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712629664; c=relaxed/simple; bh=I1NSQo9n7gs4xvwopxPj6iZmDyAcmmO/EQl+NNibUH0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NWz7hv/yGCAdDARqogTiwltqnt87VD4CjxHF7SQXVluXYhqj0Vvew5ZKA4Gpe+vGHcndA93IgtDXMqHFdiBtzoEXPQakNVVOqZZpvACRSpQd8gu+xkyvZuoYPz7SYkiTA9XtccsKmDR7tKHvYc7bAM+x5zQ9imwD3q0TKh3kFYc= 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=A4vZAO/z; arc=none smtp.client-ip=209.85.214.171 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="A4vZAO/z" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1e2b137d666so37675525ad.2 for ; Mon, 08 Apr 2024 19:27:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1712629661; x=1713234461; darn=lists.linux.dev; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=xKXQV0jQWU2gsAPx7KAvDjhnscpl0uX7n6QW/DFPTfo=; b=A4vZAO/ze9dm+ZDt6SHl0aSuyFNT5ll0ERyMO1F11jhN62mHZJcBNWF2sMn7RrNk/B rhGX3FTcRFJsvlgYxLZqWbjOFocvV0sG2bRf/axFw/dMqTxlO6don2hvrkf1YZ72OXYI Qg8EXaPg+kVMa7MCXw6LenQBlfJuw4Tr7T8Pc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712629661; x=1713234461; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xKXQV0jQWU2gsAPx7KAvDjhnscpl0uX7n6QW/DFPTfo=; b=KYgSA8DN3ExvHbHRgu5i3yQofWt5qNvrxQkycanaedPLJjR/E+TXWjG+78RQssxG9x c/M1mFTjoWBwuh6UcRd92ijrI+N6IY9Mc41mU37/wzxjHa1ZHD71+92gJ3moR8NOwrlw LZPDGQWZFHv7F+xiIH1u7OwgK+qtAUFMvzdb7kEo3p8Bd9f2putkv8i5zcTVrgxFAPSs Kgb1XCv0hTjRG3o00ZOo/+MXUmlDC6mTWh9N+jaC04Mj+YW/HLEB+d9YPAqKmFv+Cysg UIblpID2tfE0dnVxI09wk0ce36BbiVkXsfkN4hyAMdFcMV0l4GbmlekDnekAuN7EkPtG Yf2Q== X-Forwarded-Encrypted: i=1; AJvYcCU6OJYG3P/7a7hxsQIbF7cnicPYzGtqh9gEI1jYaM2i5IjtGjHMLBx5qs8CafpECO51mTC2Nr5RcwqSuhavFTOdIpAXRd+hBG6Ktz7TETdT X-Gm-Message-State: AOJu0YwL5ub4wp/YyYCLaiOuJrQiMNW9Uy9qQdXBG2iY+eekOqJ/iBsl XiLtMkKpoUJVjvntcubcKCBbZ3uOh/hdnM+Hj4EKWDrRMXz6Xn912Jnbeqqcig== X-Google-Smtp-Source: AGHT+IGuqSfM4tpC/VRlYd83ci9ryhXo0mjflW2f9miBOLwOZbzLmseZUs9QmFauGSZd6maMopQIwA== X-Received: by 2002:a17:902:d315:b0:1de:e5aa:5ce with SMTP id b21-20020a170902d31500b001dee5aa05cemr9777841plc.41.1712629661282; Mon, 08 Apr 2024 19:27:41 -0700 (PDT) Received: from pholla1.c.googlers.com (210.73.125.34.bc.googleusercontent.com. [34.125.73.210]) by smtp.gmail.com with ESMTPSA id h7-20020a170902680700b001e2b4f513e1sm7880850plk.106.2024.04.08.19.27.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 19:27:41 -0700 (PDT) From: Pavan Holla Date: Tue, 09 Apr 2024 02:27:37 +0000 Subject: [PATCH v4 2/2] usb: typec: ucsi: Implement ChromeOS UCSI driver Precedence: bulk X-Mailing-List: chrome-platform@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240409-public-ucsi-h-v4-2-e770735222a2@chromium.org> References: <20240409-public-ucsi-h-v4-0-e770735222a2@chromium.org> In-Reply-To: <20240409-public-ucsi-h-v4-0-e770735222a2@chromium.org> To: Greg Kroah-Hartman , Heikki Krogerus , Benson Leung , Tzung-Bi Shih , Guenter Roeck Cc: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Abhishek Pandit-Subedi , chrome-platform@lists.linux.dev, Pavan Holla X-Mailer: b4 0.12.4 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 --- drivers/usb/typec/ucsi/Kconfig | 13 ++ drivers/usb/typec/ucsi/Makefile | 1 + drivers/usb/typec/ucsi/cros_ec_ucsi.c | 259 ++++++++++++++++++++++++++++++++++ 3 files changed, 273 insertions(+) diff --git a/drivers/usb/typec/ucsi/Kconfig b/drivers/usb/typec/ucsi/Kconfig index bdcb1764cfae..4dceb14a66ee 100644 --- a/drivers/usb/typec/ucsi/Kconfig +++ b/drivers/usb/typec/ucsi/Kconfig @@ -69,4 +69,17 @@ 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. + endif diff --git a/drivers/usb/typec/ucsi/Makefile b/drivers/usb/typec/ucsi/Makefile index b4679f94696b..cb336eee055c 100644 --- a/drivers/usb/typec/ucsi/Makefile +++ b/drivers/usb/typec/ucsi/Makefile @@ -21,3 +21,4 @@ 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 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..487bcfac48fa --- /dev/null +++ b/drivers/usb/typec/ucsi/cros_ec_ucsi.c @@ -0,0 +1,259 @@ +// 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" + +#define DRV_NAME "cros-ec-ucsi" + +#define MAX_EC_DATA_SIZE 256 +#define WRITE_TMO_MS 500 + +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_async_write(struct ucsi *ucsi, unsigned int offset, + const void *val, size_t val_len) +{ + struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi); + uint8_t ec_buffer[MAX_EC_DATA_SIZE + sizeof(struct ec_params_ucsi_ppm_set)]; + struct ec_params_ucsi_ppm_set *req = (struct ec_params_ucsi_ppm_set *)ec_buffer; + int ret; + + if (val_len > MAX_EC_DATA_SIZE) { + dev_err(udata->dev, "Can't write %zu bytes. Too big.", val_len); + return -EINVAL; + } + + req->offset = offset; + memcpy(req->data, val, val_len); + ret = cros_ec_cmd(udata->ec, 0, EC_CMD_UCSI_PPM_SET, + req, sizeof(*req), 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_write(struct ucsi *ucsi, unsigned int offset, + const void *val, size_t val_len) +{ + struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi); + bool ack = UCSI_COMMAND(*(u64 *)val) == UCSI_ACK_CC_CI; + int ret; + + if (ack) + set_bit(ACK_PENDING, &udata->flags); + else + set_bit(COMMAND_PENDING, &udata->flags); + + ret = cros_ucsi_async_write(ucsi, offset, val, val_len); + if (ret) + goto out; + + if (!wait_for_completion_timeout(&udata->complete, WRITE_TMO_MS)) + ret = -ETIMEDOUT; + +out: + if (ack) + clear_bit(ACK_PENDING, &udata->flags); + else + clear_bit(COMMAND_PENDING, &udata->flags); + return ret; +} + +struct ucsi_operations cros_ucsi_ops = { + .read = cros_ucsi_read, + .async_write = cros_ucsi_async_write, + .sync_write = cros_ucsi_sync_write, +}; + +static void cros_ucsi_work(struct work_struct *work) +{ + struct cros_ucsi_data *udata = container_of(work, struct cros_ucsi_data, work); + u32 cci; + int ret; + + ret = cros_ucsi_read(udata->ucsi, UCSI_CCI, &cci, sizeof(cci)); + if (ret) + return; + + if (UCSI_CCI_CONNECTOR(cci)) + ucsi_connector_change(udata->ucsi, UCSI_CCI_CONNECTOR(cci)); + + if (cci & UCSI_CCI_ACK_COMPLETE && test_bit(ACK_PENDING, &udata->flags)) + complete(&udata->complete); + if (cci & UCSI_CCI_COMMAND_COMPLETE && + test_bit(COMMAND_PENDING, &udata->flags)) + complete(&udata->complete); +} + +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 int cros_ucsi_remove(struct platform_device *dev) +{ + struct cros_ucsi_data *udata = platform_get_drvdata(dev); + + ucsi_unregister(udata->ucsi); + cros_ucsi_destroy(udata); + return 0; +} + +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 int __maybe_unused cros_ucsi_resume(struct device *dev) +{ + struct cros_ucsi_data *udata = dev_get_drvdata(dev); + + return ucsi_resume(udata->ucsi); +} + +static SIMPLE_DEV_PM_OPS(cros_ucsi_pm_ops, cros_ucsi_suspend, + cros_ucsi_resume); + +static const struct platform_device_id cros_ucsi_id[] = { + { DRV_NAME, 0}, + {} +}; +MODULE_DEVICE_TABLE(platform, cros_ucsi_id); + +static struct platform_driver cros_ucsi_driver = { + .driver = { + .name = DRV_NAME, + .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");