Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp942725lqt; Fri, 7 Jun 2024 03:34:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVtxctCo1QTwT0DdMDcgINWvwLVog2UJkGI9z77Gn+R5BPwc7ppQOw6Y4djy9SdSeCPUVbWZn+bbpFv+j0+CE1A7ph59j+fmUmvQVSkyA== X-Google-Smtp-Source: AGHT+IEf1GXdRGowUA1lcFqmOP/fjG0VJGhYLbhXNXXGzG2SqDwukHfQbbVkWwzMs9mts7fcdDwA X-Received: by 2002:a05:6a20:a10b:b0:1a7:590e:279e with SMTP id adf61e73a8af0-1b2e6920b9bmr3518953637.5.1717756447876; Fri, 07 Jun 2024 03:34:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717756447; cv=pass; d=google.com; s=arc-20160816; b=UDkNCOLiC+Do0W/yHKjqsk5x0Xy9kH/fxHD063JbiortnWwEuo9yDDHtqrVyzsHura rtI/PL/cweh02Ny1ru6JrcFCpDs86OhrhmDMPZoYMsPAdiSnSLRewMex/G6ZrZKJW/WR RV06OiPckKYOTTSa8tGgoDHnDKpVCNUNVbYW3S9xOXDQzPvaieHwzNbutZFqq7RbJKnc GiW7yp9tGrBx0XBKpbUpKTit6P2rdXo/zpjqTabGbrNn/9+F5O2xdAwBgtCr7r7XVOZu 4A0KvdpZ0sn+S2DZbAt30uhIfv5FKIuPSGPFlJSA1Z7Mz1naoaNWE2OexG0KUkm01fr0 PWdQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=vC9YfLlk7LlLp3bVQxQQxqnW9tFEmO8Ok5Bjp89caCY=; fh=L9/qPjUNZjXd00/ZXWHayMbIsutxs1XSrVB3uCX85wM=; b=qK9K69OeL9TPjClOr/JObd3jWtHqlaISVXIcZYk2KNRLLh18jY9ecmhdI1cOomJ+7W Wc96cULl0p7+SlJJr9TafjmwKOmSfDf0gsN74VsiNRnla2OS5+o3rAwY868wQs3WUPyx Wlp2RrmQM/mkaMuIq30QuQUmdbkrW6YIMSqQ1si1a8DbPymIBYSOBWjM1dGHs+idjp9/ fc1YhznNf6g/ulDE4CuyJzrxhP8jr54/qlMs1Ko6cTsP0zLbhGqQhlDqv3WYcNyLcU9O sKj9oJSArPDd8ASpEBzPedgRZrf27KgVPPooMdgOzp8GHF1DgIi3dV2AGOnDEOq7wVvv e5WQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PK645yfm; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-205829-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205829-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 41be03b00d2f7-6de261cadacsi144861a12.260.2024.06.07.03.34.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 03:34:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205829-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PK645yfm; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-205829-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205829-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id CA887286702 for ; Fri, 7 Jun 2024 10:34:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7C8DA15FCEA; Fri, 7 Jun 2024 10:32:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="PK645yfm" Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (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 8AF3115DBC8 for ; Fri, 7 Jun 2024 10:32:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717756367; cv=none; b=eyixoJLRvvOElYST37eqObF7MdICRa3bhaDiHPq/+arEjGLmHeyH5+0FwOxxcIAFSr1UYs45YjmYuXmczuOtHunLzl2MUjFBOccL4mBD1cTh7zQcgwSQ9pFIxfYJ0c4dCQJeLYAnGkW/RA3Cp0gz4UEpMhT5OKDQRQk8cshgR1k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717756367; c=relaxed/simple; bh=bDPI93m5Tt38Imf2xDmlzCR3mDjGDdiW6tBNCWrxUL8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QmwDhSZ1LdQsKG99F2fIa4yCJZUIBndG2f8c+hqbR1Pa6NfqUJqBbbOWFuoEJ6SERmOrhU3KPU/gZN03dtde+gODyOLdFc6LO6bZF48Ut5UxHjptUPLTI2/DrDNg9GyQwd2B8aUcGjwcKe1iq/DYiQrWvOd16m/IBIMfL4P96p0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=PK645yfm; arc=none smtp.client-ip=209.85.167.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-5295eb47b48so2476349e87.1 for ; Fri, 07 Jun 2024 03:32:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717756363; x=1718361163; darn=vger.kernel.org; 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=vC9YfLlk7LlLp3bVQxQQxqnW9tFEmO8Ok5Bjp89caCY=; b=PK645yfmfP6AyVZtVVGWQBRvyr3DUjK8Qt+ySmOzcbuUPQvZou2nW9Nh5aqVT1lvak tZEP4mI6Y3c0R4CQi4oyFyvrpupsWjhYw7hWtKXC+7O2wcMEEwqHARF5j9V4Ra/9FQ5u qmWfQkKHYfzsnw3HS8Q71/WrH6dbGq48mflDtpwcqUY+sFnr2jkxt+ikC6Nqxxlq0EYH psUs/eYsM6hrQsTJwJ7PqnmOgy8V507XcXJIHO8cg7HHkp6xjkkmMyTHGXrQJ9VyKzt3 N+Rsidis0CS+KEZ8UDou2/Qw4Au66k4qY2x95Ocu2QDrZQZ4qqDDxs+2HkGOM5woinvG x5zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717756363; x=1718361163; 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=vC9YfLlk7LlLp3bVQxQQxqnW9tFEmO8Ok5Bjp89caCY=; b=OjdeDK38n+z82jZSUfbhpUZ4v5+5l44r1pDDbI3HPaa5SIjg64YyNdKfJVopCDI1D4 K+vZAAdmaUUp4Epw8hjVZVj3CvFthAZm67ibnQexoeQKhUzeheSWvmAH2+FB2EFM3/+o 1Poi6b/oGIqYtE+G7aZqDO4HNZnpIAG4taHGiyyFoqk3dsYRVFeXyan846HetOx8ymEG rQ7rely59jzuWqlpLmPuIcpizwFbGHqBYqu6ENQyhbqCs291FWbYGrZfswAt1OtSUzhT WCnQRLNMrWT18mW6VVGlNCxZObSbvYbI6OEZDshazCVjiNxXwz+wO0G9UotI/M6OiUgw AmNQ== X-Forwarded-Encrypted: i=1; AJvYcCUeviSsJqGbkjwTjX4rZpoaf2hcNXv/d2laVd+OhLL3GPSBaP+pQUC0pXLEh9l5xCPbZYw3TwiLV8LC9aDebKlCbcEUhiB/hoxL9I5P X-Gm-Message-State: AOJu0YzIug/64XOkLo9LJBnWgLqdKmQdYnRYf8G5nhsDyE+BzKddxTQu dMlAmBK6KchDdB6sMjqempzYGjqpO6hSLhVTw1vBmlsoHTpXmFLwJqQyNoDxuD8= X-Received: by 2002:a05:6512:130b:b0:52b:bf8f:5690 with SMTP id 2adb3069b0e04-52bbf8f5813mr884860e87.52.1717756362506; Fri, 07 Jun 2024 03:32:42 -0700 (PDT) Received: from umbar.lan ([192.130.178.91]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-52bb433ccdbsm483448e87.283.2024.06.07.03.32.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 03:32:41 -0700 (PDT) From: Dmitry Baryshkov Date: Fri, 07 Jun 2024 13:32:38 +0300 Subject: [PATCH v5 3/6] usb: typec: ucsi: add Lenovo Yoga C630 glue driver Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240607-yoga-ec-driver-v5-3-1ac91a0b4326@linaro.org> References: <20240607-yoga-ec-driver-v5-0-1ac91a0b4326@linaro.org> In-Reply-To: <20240607-yoga-ec-driver-v5-0-1ac91a0b4326@linaro.org> To: Sebastian Reichel , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Bjorn Andersson , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Bryan O'Donoghue , Heikki Krogerus , Greg Kroah-Hartman , Konrad Dybcio Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-usb@vger.kernel.org, linux-arm-msm@vger.kernel.org, Nikita Travkin X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7205; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=bDPI93m5Tt38Imf2xDmlzCR3mDjGDdiW6tBNCWrxUL8=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmYuHG7DWpupvE88U4ZwcS5LPGbYU7zV0OPK+Wp 7Qv0GqVzmKJATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZmLhxgAKCRCLPIo+Aiko 1aNVB/4+2d1quCNlRlcxfMm3FCbRrZuLFpgn55P6PJRDQvT/npZH7xhRRTXH3Gqhbo2soyJwJgd zMvPQolFlbBYPhQUHqv0nUJ/yfTS13k6G67SXaKY7RoIXgXkcT5NVB6YNpgXFKYkgj6xwstLp21 P7OnEQOtaqNxY/oGb6cw5yINqbH2Jp8nDMkhog5qE2s9UfRUKTleSsAEkeyrnyppXYYT8t3DI2+ mbwtA6ZrvDuk8TzYP7wGUu0yF90bJsbLEn4XhSn6Le89xnYzpHYj8sj/dwlI5cxm9tF9ZZw+mkv /rynKsbVkgza4AA3RELw0/NSG6SOu9zKEZQdyc0vcy8X6ta4 X-Developer-Key: i=dmitry.baryshkov@linaro.org; a=openpgp; fpr=8F88381DD5C873E4AE487DA5199BF1243632046A The Lenovo Yoga C630 WOS laptop provides implements UCSI interface in the onboard EC. Add glue driver to interface the platform's UCSI implementation. Reviewed-by: Bryan O'Donoghue Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/Kconfig | 9 ++ drivers/usb/typec/ucsi/Makefile | 1 + drivers/usb/typec/ucsi/ucsi_yoga_c630.c | 202 ++++++++++++++++++++++++++++++++ 3 files changed, 212 insertions(+) diff --git a/drivers/usb/typec/ucsi/Kconfig b/drivers/usb/typec/ucsi/Kconfig index bdcb1764cfae..680e1b87b152 100644 --- a/drivers/usb/typec/ucsi/Kconfig +++ b/drivers/usb/typec/ucsi/Kconfig @@ -69,4 +69,13 @@ config UCSI_PMIC_GLINK To compile the driver as a module, choose M here: the module will be called ucsi_glink. +config UCSI_LENOVO_YOGA_C630 + tristate "UCSI Interface Driver for Lenovo Yoga C630" + depends on EC_LENOVO_YOGA_C630 + help + This driver enables UCSI support on the Lenovo Yoga C630 laptop. + + To compile the driver as a module, choose M here: the module will be + called ucsi_yoga_c630. + endif diff --git a/drivers/usb/typec/ucsi/Makefile b/drivers/usb/typec/ucsi/Makefile index b4679f94696b..aed41d23887b 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_UCSI_LENOVO_YOGA_C630) += ucsi_yoga_c630.o diff --git a/drivers/usb/typec/ucsi/ucsi_yoga_c630.c b/drivers/usb/typec/ucsi/ucsi_yoga_c630.c new file mode 100644 index 000000000000..194b49291f28 --- /dev/null +++ b/drivers/usb/typec/ucsi/ucsi_yoga_c630.c @@ -0,0 +1,202 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2024, Linaro Ltd + * Authors: + * Bjorn Andersson + * Dmitry Baryshkov + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ucsi.h" + +struct yoga_c630_ucsi { + struct yoga_c630_ec *ec; + struct ucsi *ucsi; + struct notifier_block nb; + struct completion complete; + unsigned long flags; +#define UCSI_C630_COMMAND_PENDING 0 +#define UCSI_C630_ACK_PENDING 1 + u16 version; +}; + +static int yoga_c630_ucsi_read(struct ucsi *ucsi, unsigned int offset, + void *val, size_t val_len) +{ + struct yoga_c630_ucsi *uec = ucsi_get_drvdata(ucsi); + u8 buf[YOGA_C630_UCSI_READ_SIZE]; + int ret; + + ret = yoga_c630_ec_ucsi_read(uec->ec, buf); + if (ret) + return ret; + + if (offset == UCSI_VERSION) { + memcpy(val, &uec->version, min(val_len, sizeof(uec->version))); + return 0; + } + + if (offset == UCSI_CCI) + memcpy(val, buf, min(val_len, YOGA_C630_UCSI_CCI_SIZE)); + else if (offset == UCSI_MESSAGE_IN) + memcpy(val, buf + YOGA_C630_UCSI_CCI_SIZE, + min(val_len, YOGA_C630_UCSI_DATA_SIZE)); + else + return -EINVAL; + + return 0; +} + +static int yoga_c630_ucsi_async_write(struct ucsi *ucsi, unsigned int offset, + const void *val, size_t val_len) +{ + struct yoga_c630_ucsi *uec = ucsi_get_drvdata(ucsi); + + if (offset != UCSI_CONTROL || + val_len != YOGA_C630_UCSI_WRITE_SIZE) + return -EINVAL; + + return yoga_c630_ec_ucsi_write(uec->ec, val); +} + +static int yoga_c630_ucsi_sync_write(struct ucsi *ucsi, unsigned int offset, + const void *val, size_t val_len) +{ + struct yoga_c630_ucsi *uec = ucsi_get_drvdata(ucsi); + bool ack = UCSI_COMMAND(*(u64 *)val) == UCSI_ACK_CC_CI; + int ret; + + if (ack) + set_bit(UCSI_C630_ACK_PENDING, &uec->flags); + else + set_bit(UCSI_C630_COMMAND_PENDING, &uec->flags); + + reinit_completion(&uec->complete); + + ret = yoga_c630_ucsi_async_write(ucsi, offset, val, val_len); + if (ret) + goto out_clear_bit; + + if (!wait_for_completion_timeout(&uec->complete, 5 * HZ)) + ret = -ETIMEDOUT; + +out_clear_bit: + if (ack) + clear_bit(UCSI_C630_ACK_PENDING, &uec->flags); + else + clear_bit(UCSI_C630_COMMAND_PENDING, &uec->flags); + + return ret; +} + +const struct ucsi_operations yoga_c630_ucsi_ops = { + .read = yoga_c630_ucsi_read, + .sync_write = yoga_c630_ucsi_sync_write, + .async_write = yoga_c630_ucsi_async_write, +}; + +static void yoga_c630_ucsi_notify_ucsi(struct yoga_c630_ucsi *uec, u32 cci) +{ + if (UCSI_CCI_CONNECTOR(cci)) + ucsi_connector_change(uec->ucsi, UCSI_CCI_CONNECTOR(cci)); + + if (cci & UCSI_CCI_ACK_COMPLETE && + test_bit(UCSI_C630_ACK_PENDING, &uec->flags)) + complete(&uec->complete); + + if (cci & UCSI_CCI_COMMAND_COMPLETE && + test_bit(UCSI_C630_COMMAND_PENDING, &uec->flags)) + complete(&uec->complete); +} + +static int yoga_c630_ucsi_notify(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct yoga_c630_ucsi *uec = container_of(nb, struct yoga_c630_ucsi, nb); + u32 cci; + int ret; + + switch (action) { + case LENOVO_EC_EVENT_USB: + case LENOVO_EC_EVENT_HPD: + ucsi_connector_change(uec->ucsi, 1); + return NOTIFY_OK; + + case LENOVO_EC_EVENT_UCSI: + ret = uec->ucsi->ops->read(uec->ucsi, UCSI_CCI, &cci, sizeof(cci)); + if (ret) + return NOTIFY_DONE; + + yoga_c630_ucsi_notify_ucsi(uec, cci); + + return NOTIFY_OK; + + default: + return NOTIFY_DONE; + } +} + +static int yoga_c630_ucsi_probe(struct auxiliary_device *adev, + const struct auxiliary_device_id *id) +{ + struct yoga_c630_ec *ec = adev->dev.platform_data; + struct yoga_c630_ucsi *uec; + int ret; + + uec = devm_kzalloc(&adev->dev, sizeof(*uec), GFP_KERNEL); + if (!uec) + return -ENOMEM; + + uec->ec = ec; + init_completion(&uec->complete); + uec->nb.notifier_call = yoga_c630_ucsi_notify; + + uec->ucsi = ucsi_create(&adev->dev, &yoga_c630_ucsi_ops); + if (IS_ERR(uec->ucsi)) + return PTR_ERR(uec->ucsi); + + ucsi_set_drvdata(uec->ucsi, uec); + + uec->version = yoga_c630_ec_ucsi_get_version(uec->ec); + + auxiliary_set_drvdata(adev, uec); + + ret = yoga_c630_ec_register_notify(ec, &uec->nb); + if (ret) + return ret; + + return ucsi_register(uec->ucsi); +} + +static void yoga_c630_ucsi_remove(struct auxiliary_device *adev) +{ + struct yoga_c630_ucsi *uec = auxiliary_get_drvdata(adev); + + yoga_c630_ec_unregister_notify(uec->ec, &uec->nb); + ucsi_unregister(uec->ucsi); +} + +static const struct auxiliary_device_id yoga_c630_ucsi_id_table[] = { + { .name = YOGA_C630_MOD_NAME "." YOGA_C630_DEV_UCSI, }, + {} +}; +MODULE_DEVICE_TABLE(auxiliary, yoga_c630_ucsi_id_table); + +static struct auxiliary_driver yoga_c630_ucsi_driver = { + .name = YOGA_C630_DEV_UCSI, + .id_table = yoga_c630_ucsi_id_table, + .probe = yoga_c630_ucsi_probe, + .remove = yoga_c630_ucsi_remove, +}; + +module_auxiliary_driver(yoga_c630_ucsi_driver); + +MODULE_DESCRIPTION("Lenovo Yoga C630 UCSI"); +MODULE_LICENSE("GPL"); -- 2.39.2