From patchwork Mon Jan 13 17:50:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pengyu Luo X-Patchwork-Id: 857189 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 7E20C24022F; Mon, 13 Jan 2025 17:52:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736790748; cv=none; b=XdspP20W1iY4SAaCuhpxa4lXsLf3qVtIukNU9PtIsBP89Z0+IigI51fWQ84gFkg0QWT0mKZDsLPljrERx6m0jFC4rrwA+OXshOnv3Fxo/zB/7LCeJbCOxRnc2rakPJLKyG5Om5Ic6nwMtgXLoiNOZxhNErZ5Czub+CLDEhWs8eY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736790748; c=relaxed/simple; bh=8632s5sxj2XPiBeg+a+Ba0y8cFZJmBlr0tqXMcv/pdY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=OrYMZo3fO3PJXBhD1GKiilaxJJHqXcCqBNGacow3jUYdvgygNfnOXrKPb+fLP6Q95OqHrrdT/DNjDGlKE1+qgp3rN2y/c4Sg0u4ff3hCy3LNLGGAzKvv/8KwslFFkxRiNAiJ71JWCosu2p+GAWZEgMZlczvNHcfiaLa1Uo74cl4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mywoQopj; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mywoQopj" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-21a7ed0155cso78894175ad.3; Mon, 13 Jan 2025 09:52:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736790746; x=1737395546; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ibTVDee6wvl4HVurpUAtKl3Uyh5IZMqzY00oHtMrOws=; b=mywoQopj0mRFOO0dgzlnTMSRPsngO8O5Lh74cerVjaIclnFb70EWg+Db0eA72x54gW e4VboHEDRLTrrh6ihV4fcu/Epe4XFDX29fSumXY5CyCf1VBA3bBXM282zI3pzMR7aEyT kaCCVKIsQlZ9ur+/TQk9+WgcvhDG7PwO6yVNZxgLvHU93GfTHesiVEOFr7Xl9K57cwbk HIBZxSZj/+kSpikglODQJb3ztWY690K6KEtik2p2EgfYHdo4S84EDbQqjkRcO8pcZC+S TC8woCqzodtwa5PVZI05BAfIA83fZ1bGtqwc/8WtyJ+nbbRMfTpBu8pyp3Q6XDsbnIjP kNeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736790746; x=1737395546; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ibTVDee6wvl4HVurpUAtKl3Uyh5IZMqzY00oHtMrOws=; b=qVzRLTh7A9ThX+er5qXlCL2ZUZZUlkXCz4O3VPOMVlqe6kXm0/Ya4JYuM/AK6IgtEb OwPf2IA93LMOmNyn/KwStXRUmAepsgDXOZRAWzOHcVbPbdtLMgeMGQBqRQe8SZPTXyiJ TayBHEt3XA3IRa9GJ8kxFp8RbGu07QUcvBMc8c2G5DVL34XFLvaqq+tkBu0uvhIMvhew qdKylGrwUnAa/clwfs7VtZQGTnbUEulGufQpMeWSwzhZVbcr56/hprwFqQFr5nXzNOQP 4dAF4GZYtp0ZwAfdZoo386BlxxiW4PcLfsMZ+ZlN/egudzbcPUq7w0GVrGNzOzAuKQyp D5Ew== X-Forwarded-Encrypted: i=1; AJvYcCUD/jVd8mOYJ3dv9ZmAirIQFpGhKCAjD4LKty+uB9ZvvrMMfha8EVvhTiLxnophyJ+4sV5Xz8EWJEweYtk=@vger.kernel.org, AJvYcCV48bQQkwPNmt4gr/uxR6XHXKZxlxHST8BhtDHmQ8aCSttXpkCJpTRyMfxDqS7ZirsksOJ0mNi4QA37Yi+6@vger.kernel.org, AJvYcCVAkjvuOu242AJEaGNEIyAjMzVyo3FBQcidP2jE6t+tOIYM1flqtAO/+fzeicGNhy099IIgsufb6B5w@vger.kernel.org, AJvYcCVTKDdYTy1fj5w7zqXhLBqyChsXxEGZY+nqEuyasaAs6h60AoUk5CuYlR4pCTSZIPgua+6ivwPytPE=@vger.kernel.org, AJvYcCWBMbsMJUNgd6hKVnrGUF3Q/PSgPmh2i06xQDomqTc0vbquecEEmp1ZvTtMX0LfSGu9kLzzT9yS0pNbjlRr@vger.kernel.org, AJvYcCXiWgfwRPUWusxRtiEyQ6A/ThwPalr43Ot2T+JN/VC+V8kzCW3eLJQh/q0K4jEfxNt1yjUhHfTTnSpi3zbqdJF7E+2Hlw==@vger.kernel.org X-Gm-Message-State: AOJu0YyIcK4vWF5wC55B4mR9QTsZ9q2dLVlB0AQBStSzr8JK902+95Ww Z07SyxJYQJQsmHdWKFbWJPnFaSlLo7AVmec5Oj5hA30X2tOkt1oq X-Gm-Gg: ASbGnctZn4C6tUbWRASIaxOhGIM86lhKWvLDB3ASSDtaHBH8DlNBAEh1L8Ffs5Xqc7v z82y++ptAFZqJRbaz3eEZFMAhM8e8raKBnkykNs8e//tONKzr9bSKrZ/QBTlFUx7weIJ1HY2QEh X+vUqjBUdyq20nXgrbZzKp3qOTih4kEz34EakZgFih4eSNWEdIPTbbDqSWcZo01HggWx7vf5PtX hVZmYTnVfKPZQoePpZH9xIlbfc5NWFp7JsMh0/c6PLc9MrOkJ0/5d4= X-Google-Smtp-Source: AGHT+IFYdOF476BTPQvJqPtNcwB1L3ff9+rRXpghCYzRsFDPKCLhiSLuRSxrKL63RcN8+yY3wvm+ag== X-Received: by 2002:a05:6a00:929a:b0:72a:8b90:92e9 with SMTP id d2e1a72fcca58-72d21f30475mr28800901b3a.5.1736790745393; Mon, 13 Jan 2025 09:52:25 -0800 (PST) Received: from nuvole.. ([144.202.86.13]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72d4056a632sm6330669b3a.59.2025.01.13.09.52.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 09:52:24 -0800 (PST) From: Pengyu Luo To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Bjorn Andersson , Konrad Dybcio , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Bryan O'Donoghue , Sebastian Reichel , Heikki Krogerus , Greg Kroah-Hartman , Jean Delvare , Guenter Roeck Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-pm@vger.kernel.org, linux-usb@vger.kernel.org, linux-hwmon@vger.kernel.org, Pengyu Luo Subject: [PATCH v3 2/6] platform: arm64: add Huawei Matebook E Go EC driver Date: Tue, 14 Jan 2025 01:50:49 +0800 Message-ID: <20250113175049.590511-1-mitltlatltl@gmail.com> X-Mailer: git-send-email 2.47.1 Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There are three variants of which Huawei released the first two simultaneously. Huawei Matebook E Go LTE(sc8180x), codename seems to be gaokun2. Huawei Matebook E Go(sc8280xp@3.0GHz), codename must be gaokun3. (see [1]) Huawei Matebook E Go 2023(sc8280xp@2.69GHz), codename should be also gaokun3. Adding support for the latter two variants for now, this driver should also work for the sc8180x variant according to acpi table files, but I don't have the device to test yet. Different from other Qualcomm Snapdragon sc8280xp based machines, the Huawei Matebook E Go uses an embedded controller while others use a system called PMIC GLink. This embedded controller can be used to perform a set of various functions, including, but not limited to: - Battery and charger monitoring; - Charge control and smart charge; - Fn_lock settings; - Tablet lid status; - Temperature sensors; - USB Type-C notifications (ports orientation, DP alt mode HPD); - USB Type-C PD (according to observation, up to 48w). Add a driver for the EC which creates devices for UCSI and power supply devices. [1] https://bugzilla.kernel.org/show_bug.cgi?id=219645 Signed-off-by: Pengyu Luo --- MAINTAINERS | 9 + drivers/platform/arm64/Kconfig | 21 + drivers/platform/arm64/Makefile | 1 + drivers/platform/arm64/huawei-gaokun-ec.c | 841 ++++++++++++++++++ .../linux/platform_data/huawei-gaokun-ec.h | 80 ++ 5 files changed, 952 insertions(+) create mode 100644 drivers/platform/arm64/huawei-gaokun-ec.c create mode 100644 include/linux/platform_data/huawei-gaokun-ec.h diff --git a/MAINTAINERS b/MAINTAINERS index 520612b3d..0f9e0390a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10691,6 +10691,15 @@ S: Maintained F: Documentation/networking/device_drivers/ethernet/huawei/hinic.rst F: drivers/net/ethernet/huawei/hinic/ +HUAWEI MATEBOOK E GO EMBEDDED CONTROLLER DRIVER +M: Pengyu Luo +S: Maintained +F: Documentation/devicetree/bindings/platform/huawei,gaokun-ec.yaml +F: drivers/platform/arm64/huawei-gaokun-ec.c +F: drivers/power/supply/huawei-gaokun-battery.c +F: drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c +F: include/linux/platform_data/huawei-gaokun-ec.h + HUGETLB SUBSYSTEM M: Muchun Song L: linux-mm@kvack.org diff --git a/drivers/platform/arm64/Kconfig b/drivers/platform/arm64/Kconfig index f88395ea3..67dc7aa95 100644 --- a/drivers/platform/arm64/Kconfig +++ b/drivers/platform/arm64/Kconfig @@ -33,6 +33,27 @@ config EC_ACER_ASPIRE1 laptop where this information is not properly exposed via the standard ACPI devices. +config EC_HUAWEI_GAOKUN + tristate "Huawei Matebook E Go Embedded Controller driver" + depends on ARCH_QCOM || COMPILE_TEST + depends on I2C + depends on DRM + depends on POWER_SUPPLY + depends on INPUT + help + Say Y here to enable the EC driver for the Huawei Matebook E Go + which is a sc8280xp-based 2-in-1 tablet. The driver handles battery + (information, charge control) and USB Type-C DP HPD events as well + as some misc functions like the lid sensor and temperature sensors, + etc. + + This driver provides battery and AC status support for the mentioned + laptop where this information is not properly exposed via the + standard ACPI devices. + + Say M or Y here to include this support. + + config EC_LENOVO_YOGA_C630 tristate "Lenovo Yoga C630 Embedded Controller driver" depends on ARCH_QCOM || COMPILE_TEST diff --git a/drivers/platform/arm64/Makefile b/drivers/platform/arm64/Makefile index b2ae9114f..46a99eba3 100644 --- a/drivers/platform/arm64/Makefile +++ b/drivers/platform/arm64/Makefile @@ -6,4 +6,5 @@ # obj-$(CONFIG_EC_ACER_ASPIRE1) += acer-aspire1-ec.o +obj-$(CONFIG_EC_HUAWEI_GAOKUN) += huawei-gaokun-ec.o obj-$(CONFIG_EC_LENOVO_YOGA_C630) += lenovo-yoga-c630.o diff --git a/drivers/platform/arm64/huawei-gaokun-ec.c b/drivers/platform/arm64/huawei-gaokun-ec.c new file mode 100644 index 000000000..3cbecc81e --- /dev/null +++ b/drivers/platform/arm64/huawei-gaokun-ec.c @@ -0,0 +1,841 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * huawei-gaokun-ec - An EC driver for HUAWEI Matebook E Go + * + * reference: drivers/platform/arm64/acer-aspire1-ec.c + * drivers/platform/arm64/lenovo-yoga-c630.c + * drivers/platform/x86/huawei-wmi.c + * + * Copyright (C) 2024 Pengyu Luo + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define EC_EVENT 0x06 + +/* Also can be found in ACPI specification 12.3 */ +#define EC_READ 0x80 +#define EC_WRITE 0x81 +#define EC_BURST 0x82 +#define EC_QUERY 0x84 + +#define EC_FN_LOCK_ON 0x5A +#define EC_FN_LOCK_OFF 0x55 + +#define EC_EVENT_LID 0x81 + +#define EC_LID_STATE 0x80 +#define EC_LID_OPEN BIT(1) + +#define UCSI_REG_SIZE 7 + +/* + * for tx, command sequences are arranged as + * {master_cmd, slave_cmd, data_len, data_seq} + */ +#define REQ_HDR_SIZE 3 +#define INPUT_SIZE_OFFSET 2 +#define REQ_LEN(req) (REQ_HDR_SIZE + req[INPUT_SIZE_OFFSET]) + +/* + * for rx, data sequences are arranged as + * {status, data_len(unreliable), data_seq} + */ +#define RESP_HDR_SIZE 2 + +#define MKREQ(REG0, REG1, SIZE, ...) \ +{ \ + REG0, REG1, SIZE, \ + /* ## will remove comma when SIZE is 0 */ \ + ## __VA_ARGS__, \ + /* make sure len(pkt[3:]) >= SIZE */ \ + [3 + SIZE] = 0, \ +} + +#define MKRESP(SIZE) \ +{ \ + [RESP_HDR_SIZE + SIZE - 1] = 0, \ +} + +static inline void refill_req(u8 *dest, const u8 *src, size_t size) +{ + memcpy(dest + REQ_HDR_SIZE, src, size); +} + +static inline void extr_resp(u8 *dest, const u8 *src, size_t size) +{ + memcpy(dest, src + RESP_HDR_SIZE, size); +} + +struct gaokun_ec { + struct i2c_client *client; + struct mutex lock; /* EC transaction lock */ + struct blocking_notifier_head notifier_list; + struct device *hwmon_dev; + struct input_dev *idev; + bool suspended; +}; + +static int gaokun_ec_request(struct gaokun_ec *ec, const u8 *req, + size_t resp_len, u8 *resp) +{ + struct i2c_client *client = ec->client; + struct i2c_msg msgs[2] = { + { + .addr = client->addr, + .flags = client->flags, + .len = REQ_LEN(req), + .buf = req, + }, { + .addr = client->addr, + .flags = client->flags | I2C_M_RD, + .len = resp_len, + .buf = resp, + }, + }; + + mutex_lock(&ec->lock); + + i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); + usleep_range(2000, 2500); /* have a break, acpi did this */ + + mutex_unlock(&ec->lock); + + return *resp ? -EIO : 0; +} + +/* -------------------------------------------------------------------------- */ +/* Common API */ + +/** + * gaokun_ec_read - Read from EC + * @ec: The gaokun_ec + * @req: The sequence to request + * @resp_len: The size to read + * @resp: Where the data are read to + * + * Return: 0 on success or negative error code. + * + * This function is used to read data after writing a magic sequence to EC. + * All EC operations depend on this function. + * + * Huawei uses magic sequences everywhere to complete various functions, all + * these sequences are passed to ECCD(a ACPI method which is quiet similar + * to gaokun_ec_request), there is no good abstraction to generalize these + * sequences, so just wrap it for now. Almost all magic sequences are kept + * in this file. + */ +int gaokun_ec_read(struct gaokun_ec *ec, const u8 *req, + size_t resp_len, u8 *resp) +{ + return gaokun_ec_request(ec, req, resp_len, resp); +} +EXPORT_SYMBOL_GPL(gaokun_ec_read); + +/** + * gaokun_ec_write - Write to EC + * @ec: The gaokun_ec + * @req: The sequence to request + * + * Return: 0 on success or negative error code. + * + * This function has no big difference from gaokun_ec_read. When caller care + * only write status and no actual data are returned, then use it. + */ +int gaokun_ec_write(struct gaokun_ec *ec, u8 *req) +{ + u8 resp[] = MKRESP(0); + + return gaokun_ec_request(ec, req, sizeof(resp), resp); +} +EXPORT_SYMBOL_GPL(gaokun_ec_write); + +int gaokun_ec_read_byte(struct gaokun_ec *ec, u8 *req, u8 *byte) +{ + int ret; + u8 resp[] = MKRESP(sizeof(*byte)); + + ret = gaokun_ec_read(ec, req, sizeof(resp), resp); + extr_resp(byte, resp, sizeof(*byte)); + + return ret; +} +EXPORT_SYMBOL_GPL(gaokun_ec_read_byte); + +/** + * gaokun_ec_register_notify - Register a notifier callback for EC events. + * @ec: The gaokun_ec + * @nb: Notifier block pointer to register + * + * Return: 0 on success or negative error code. + */ +int gaokun_ec_register_notify(struct gaokun_ec *ec, struct notifier_block *nb) +{ + return blocking_notifier_chain_register(&ec->notifier_list, nb); +} +EXPORT_SYMBOL_GPL(gaokun_ec_register_notify); + +/** + * gaokun_ec_unregister_notify - Unregister notifier callback for EC events. + * @ec: The gaokun_ec + * @nb: Notifier block pointer to unregister + * + * Unregister a notifier callback that was previously registered with + * gaokun_ec_register_notify(). + */ +void gaokun_ec_unregister_notify(struct gaokun_ec *ec, struct notifier_block *nb) +{ + blocking_notifier_chain_unregister(&ec->notifier_list, nb); +} +EXPORT_SYMBOL_GPL(gaokun_ec_unregister_notify); + +/* -------------------------------------------------------------------------- */ +/* API For PSY */ + +/** + * gaokun_ec_psy_multi_read - Read contiguous registers + * @ec: The gaokun_ec + * @reg: The start register + * @resp_len: The number of registers to be read + * @resp: Where the data are read to + * + * Return: 0 on success or negative error code. + */ +int gaokun_ec_psy_multi_read(struct gaokun_ec *ec, u8 reg, + size_t resp_len, u8 *resp) +{ + int i, ret; + u8 _resp[] = MKRESP(1); + u8 req[] = MKREQ(0x02, EC_READ, 1, 0); + + for (i = 0; i < resp_len; ++i, reg++) { + refill_req(req, ®, 1); + ret = gaokun_ec_read(ec, req, sizeof(_resp), _resp); + if (ret) + return ret; + extr_resp(&resp[i], _resp, 1); + } + + return 0; +} +EXPORT_SYMBOL_GPL(gaokun_ec_psy_multi_read); + +/* Smart charge */ +#define are_thresholds_valid(start, end) ((end != 0) && (start <= end) && (end <= 100)) + +/** + * gaokun_ec_psy_get_smart_charge - read smart charge data from EC + * @ec: The gaokun_ec + * @data: Where the data are read to, (mode, delay, start, end) + * + * Return: 0 on success or negative error code. + */ +int gaokun_ec_psy_get_smart_charge(struct gaokun_ec *ec, + u8 data[GAOKUN_SMART_CHARGE_DATA_SIZE]) +{ + /* GBCM */ + u8 req[] = MKREQ(0x02, 0xE4, 0); + u8 resp[] = MKRESP(GAOKUN_SMART_CHARGE_DATA_SIZE); + int ret; + + ret = gaokun_ec_read(ec, req, sizeof(resp), resp); + if (ret) + return ret; + + extr_resp(data, resp, GAOKUN_SMART_CHARGE_DATA_SIZE); + + return 0; +} +EXPORT_SYMBOL_GPL(gaokun_ec_psy_get_smart_charge); + +/** + * gaokun_ec_psy_set_smart_charge - set smart charge data + * @ec: The gaokun_ec + * @data: The data are to be set + * + * Return: 0 on success or negative error code. + */ +int gaokun_ec_psy_set_smart_charge(struct gaokun_ec *ec, + u8 data[GAOKUN_SMART_CHARGE_DATA_SIZE]) +{ + /* SBCM */ + u8 req[] = MKREQ(0x02, 0XE3, GAOKUN_SMART_CHARGE_DATA_SIZE); + + if (!are_thresholds_valid(data[2], data[3])) + return -EINVAL; + + refill_req(req, data, GAOKUN_SMART_CHARGE_DATA_SIZE); + + return gaokun_ec_write(ec, req); +} +EXPORT_SYMBOL_GPL(gaokun_ec_psy_set_smart_charge); + +/* Smart charge enable */ +/** + * gaokun_ec_psy_get_smart_charge_enable - check if smart charge is enabled + * @ec: The gaokun_ec + * @on: The state + * + * Return: 0 on success or negative error code. + */ +int gaokun_ec_psy_get_smart_charge_enable(struct gaokun_ec *ec, bool *on) +{ + /* GBAC */ + *on = 0; /* clear other 3 Bytes */ + return gaokun_ec_read_byte(ec, (u8 [])MKREQ(0x02, 0xE6, 0), (u8 *)on); +} +EXPORT_SYMBOL_GPL(gaokun_ec_psy_get_smart_charge_enable); + +/** + * gaokun_ec_psy_set_smart_charge_enable - set smart charge state + * @ec: The gaokun_ec + * @on: The state + * + * Return: 0 on success or negative error code. + */ +int gaokun_ec_psy_set_smart_charge_enable(struct gaokun_ec *ec, bool on) +{ + /* SBAC */ + return gaokun_ec_write(ec, (u8 [])MKREQ(0x02, 0xE5, 1, on)); +} +EXPORT_SYMBOL_GPL(gaokun_ec_psy_set_smart_charge_enable); + +/* -------------------------------------------------------------------------- */ +/* API For UCSI */ + +/** + * gaokun_ec_ucsi_read - read ucsi data from EC, like CCI, MSGI + * @ec: The gaokun_ec + * @resp: Where the data are read to + * + * Return: 0 on success or negative error code. + * + * UCSI subdriver uses this function to read CCI and MSGI. + */ +int gaokun_ec_ucsi_read(struct gaokun_ec *ec, + u8 resp[GAOKUN_UCSI_READ_SIZE]) +{ + u8 req[] = MKREQ(0x03, 0xD5, 0); + u8 _resp[] = MKRESP(GAOKUN_UCSI_READ_SIZE); + int ret; + + ret = gaokun_ec_read(ec, req, sizeof(_resp), _resp); + if (ret) + return ret; + + extr_resp(resp, _resp, GAOKUN_UCSI_READ_SIZE); + return 0; +} +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_read); + +/** + * gaokun_ec_ucsi_write - write ucsi data to EC + * @ec: The gaokun_ec + * @req: The command + * + * Return: 0 on success or negative error code. + * + * UCSI subdriver uses this function to write control command. + */ +int gaokun_ec_ucsi_write(struct gaokun_ec *ec, + const u8 req[GAOKUN_UCSI_WRITE_SIZE]) +{ + u8 _req[] = MKREQ(0x03, 0xD4, GAOKUN_UCSI_WRITE_SIZE); + + refill_req(_req, req, GAOKUN_UCSI_WRITE_SIZE); + + return gaokun_ec_write(ec, _req); +} +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_write); + +/** + * gaokun_ec_ucsi_get_reg - get ucsi register from EC + * @ec: The gaokun_ec + * @ureg: The gaokun ucsi register + * + * Return: 0 on success or negative error code. + * + * UCSI subdriver uses this function to get ucsi register from EC. + */ +int gaokun_ec_ucsi_get_reg(struct gaokun_ec *ec, + struct gaokun_ucsi_reg *ureg) +{ + u8 req[] = MKREQ(0x03, 0xD3, 0); + u8 _resp[] = MKRESP(UCSI_REG_SIZE); + int ret; + + ret = gaokun_ec_read(ec, req, sizeof(_resp), _resp); + if (ret) + return ret; + + extr_resp((u8 *)ureg, _resp, UCSI_REG_SIZE); + + return 0; +} +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_get_reg); + +/** + * gaokun_ec_ucsi_pan_ack - ack pin assignment notifications from EC + * @ec: The gaokun_ec + * @port_id: The id of port which just received and handled the notifications + * + * Return: 0 on success or negative error code. + * + * UCSI subdriver uses this function to ack PANs from EC. + */ +int gaokun_ec_ucsi_pan_ack(struct gaokun_ec *ec, int port_id) +{ + u8 req[] = MKREQ(0x03, 0xD2, 1); + u8 data = 1 << port_id; + + if (port_id == GAOKUN_UCSI_NO_PORT_UPDATE) + data = 0; + + refill_req(req, &data, 1); + + return gaokun_ec_write(ec, req); +} +EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_pan_ack); + +/* -------------------------------------------------------------------------- */ +/* EC Sysfs */ + +/* Fn lock */ +static int gaokun_ec_get_fn_lock(struct gaokun_ec *ec, bool *on) +{ + /* GFRS */ + u8 req[] = MKREQ(0x02, 0x6B, 0); + int ret; + u8 val; + + ret = gaokun_ec_read_byte(ec, req, &val); + if (ret) + return ret; + + if (val == EC_FN_LOCK_ON) + *on = true; + else if (val == EC_FN_LOCK_OFF) + *on = false; + else + return -EIO; + + return 0; +} + +static int gaokun_ec_set_fn_lock(struct gaokun_ec *ec, bool on) +{ + /* SFRS */ + u8 req[] = MKREQ(0x02, 0x6C, 1); + u8 data; + + if (on) + data = EC_FN_LOCK_ON; + else + data = EC_FN_LOCK_OFF; + + refill_req(req, &data, 1); + + return gaokun_ec_write(ec, req); +} + +static ssize_t fn_lock_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct gaokun_ec *ec = dev_get_drvdata(dev); + bool on; + int ret; + + ret = gaokun_ec_get_fn_lock(ec, &on); + if (ret) + return ret; + + return sysfs_emit(buf, "%d\n", on); +} + +static ssize_t fn_lock_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + struct gaokun_ec *ec = dev_get_drvdata(dev); + bool on; + int ret; + + if (kstrtobool(buf, &on)) + return -EINVAL; + + ret = gaokun_ec_set_fn_lock(ec, on); + if (ret) + return ret; + + return size; +} + +static DEVICE_ATTR_RW(fn_lock); + +static struct attribute *gaokun_ec_attrs[] = { + &dev_attr_fn_lock.attr, + NULL, +}; +ATTRIBUTE_GROUPS(gaokun_ec); + +/* Thermal Zone HwMon */ +/* Range from 0 to 0x2C, partial valid */ +static const u8 temp_reg[20] = {0x05, 0x07, 0x08, 0x0E, 0x0F, 0x12, 0x15, 0x1E, + 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, + 0x27, 0x28, 0x29, 0x2A}; + +static int gaokun_ec_get_temp(struct gaokun_ec *ec, u8 idx, int *temp) +{ + /* GTMP */ + u8 req[] = MKREQ(0x02, 0x61, 1, temp_reg[idx]); + u8 resp[] = MKRESP(sizeof(__le16)); + __le16 tmp; + int ret; + + ret = gaokun_ec_read(ec, req, sizeof(resp), resp); + if (ret) + return ret; + + extr_resp((u8 *)&tmp, resp, sizeof(tmp)); + *temp = le16_to_cpu(tmp) * 100; /* convert to HwMon's unit */ + + return 0; +} + +static ssize_t get_ec_tz_temp(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct gaokun_ec *ec = dev_get_drvdata(dev); + int idx, ret, temp; + + idx = (to_sensor_dev_attr(attr))->index - 1; + ret = gaokun_ec_get_temp(ec, idx, &temp); + if (ret) + return ret; + + return sysfs_emit(buf, "%d\n", temp); +} + +static ssize_t ec_tz_temp_label(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + int idx = (to_sensor_dev_attr(attr))->index - 1; + + return sysfs_emit(buf, "EC Thermal Zone %2d Temperature\n", idx); +} + +static SENSOR_DEVICE_ATTR(temp1_input, 0444, get_ec_tz_temp, NULL, 1); +static SENSOR_DEVICE_ATTR(temp1_label, 0444, ec_tz_temp_label, NULL, 1); +static SENSOR_DEVICE_ATTR(temp2_input, 0444, get_ec_tz_temp, NULL, 2); +static SENSOR_DEVICE_ATTR(temp2_label, 0444, ec_tz_temp_label, NULL, 2); +static SENSOR_DEVICE_ATTR(temp3_input, 0444, get_ec_tz_temp, NULL, 3); +static SENSOR_DEVICE_ATTR(temp3_label, 0444, ec_tz_temp_label, NULL, 3); +static SENSOR_DEVICE_ATTR(temp4_input, 0444, get_ec_tz_temp, NULL, 4); +static SENSOR_DEVICE_ATTR(temp4_label, 0444, ec_tz_temp_label, NULL, 4); +static SENSOR_DEVICE_ATTR(temp5_input, 0444, get_ec_tz_temp, NULL, 5); +static SENSOR_DEVICE_ATTR(temp5_label, 0444, ec_tz_temp_label, NULL, 5); +static SENSOR_DEVICE_ATTR(temp6_input, 0444, get_ec_tz_temp, NULL, 6); +static SENSOR_DEVICE_ATTR(temp6_label, 0444, ec_tz_temp_label, NULL, 6); +static SENSOR_DEVICE_ATTR(temp7_input, 0444, get_ec_tz_temp, NULL, 7); +static SENSOR_DEVICE_ATTR(temp7_label, 0444, ec_tz_temp_label, NULL, 7); +static SENSOR_DEVICE_ATTR(temp8_input, 0444, get_ec_tz_temp, NULL, 8); +static SENSOR_DEVICE_ATTR(temp8_label, 0444, ec_tz_temp_label, NULL, 8); +static SENSOR_DEVICE_ATTR(temp9_input, 0444, get_ec_tz_temp, NULL, 9); +static SENSOR_DEVICE_ATTR(temp9_label, 0444, ec_tz_temp_label, NULL, 9); +static SENSOR_DEVICE_ATTR(temp10_input, 0444, get_ec_tz_temp, NULL, 10); +static SENSOR_DEVICE_ATTR(temp10_label, 0444, ec_tz_temp_label, NULL, 10); +static SENSOR_DEVICE_ATTR(temp11_input, 0444, get_ec_tz_temp, NULL, 11); +static SENSOR_DEVICE_ATTR(temp11_label, 0444, ec_tz_temp_label, NULL, 11); +static SENSOR_DEVICE_ATTR(temp12_input, 0444, get_ec_tz_temp, NULL, 12); +static SENSOR_DEVICE_ATTR(temp12_label, 0444, ec_tz_temp_label, NULL, 12); +static SENSOR_DEVICE_ATTR(temp13_input, 0444, get_ec_tz_temp, NULL, 13); +static SENSOR_DEVICE_ATTR(temp13_label, 0444, ec_tz_temp_label, NULL, 13); +static SENSOR_DEVICE_ATTR(temp14_input, 0444, get_ec_tz_temp, NULL, 14); +static SENSOR_DEVICE_ATTR(temp14_label, 0444, ec_tz_temp_label, NULL, 14); +static SENSOR_DEVICE_ATTR(temp15_input, 0444, get_ec_tz_temp, NULL, 15); +static SENSOR_DEVICE_ATTR(temp15_label, 0444, ec_tz_temp_label, NULL, 15); +static SENSOR_DEVICE_ATTR(temp16_input, 0444, get_ec_tz_temp, NULL, 16); +static SENSOR_DEVICE_ATTR(temp16_label, 0444, ec_tz_temp_label, NULL, 16); +static SENSOR_DEVICE_ATTR(temp17_input, 0444, get_ec_tz_temp, NULL, 17); +static SENSOR_DEVICE_ATTR(temp17_label, 0444, ec_tz_temp_label, NULL, 17); +static SENSOR_DEVICE_ATTR(temp18_input, 0444, get_ec_tz_temp, NULL, 18); +static SENSOR_DEVICE_ATTR(temp18_label, 0444, ec_tz_temp_label, NULL, 18); +static SENSOR_DEVICE_ATTR(temp19_input, 0444, get_ec_tz_temp, NULL, 19); +static SENSOR_DEVICE_ATTR(temp19_label, 0444, ec_tz_temp_label, NULL, 19); +static SENSOR_DEVICE_ATTR(temp20_input, 0444, get_ec_tz_temp, NULL, 20); +static SENSOR_DEVICE_ATTR(temp20_label, 0444, ec_tz_temp_label, NULL, 20); + +static struct attribute *gaokun_ec_hwmon_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_temp1_label.dev_attr.attr, + &sensor_dev_attr_temp2_input.dev_attr.attr, + &sensor_dev_attr_temp2_label.dev_attr.attr, + &sensor_dev_attr_temp3_input.dev_attr.attr, + &sensor_dev_attr_temp3_label.dev_attr.attr, + &sensor_dev_attr_temp4_input.dev_attr.attr, + &sensor_dev_attr_temp4_label.dev_attr.attr, + &sensor_dev_attr_temp5_input.dev_attr.attr, + &sensor_dev_attr_temp5_label.dev_attr.attr, + &sensor_dev_attr_temp6_input.dev_attr.attr, + &sensor_dev_attr_temp6_label.dev_attr.attr, + &sensor_dev_attr_temp7_input.dev_attr.attr, + &sensor_dev_attr_temp7_label.dev_attr.attr, + &sensor_dev_attr_temp8_input.dev_attr.attr, + &sensor_dev_attr_temp8_label.dev_attr.attr, + &sensor_dev_attr_temp9_input.dev_attr.attr, + &sensor_dev_attr_temp9_label.dev_attr.attr, + &sensor_dev_attr_temp10_input.dev_attr.attr, + &sensor_dev_attr_temp10_label.dev_attr.attr, + &sensor_dev_attr_temp11_input.dev_attr.attr, + &sensor_dev_attr_temp11_label.dev_attr.attr, + &sensor_dev_attr_temp12_input.dev_attr.attr, + &sensor_dev_attr_temp12_label.dev_attr.attr, + &sensor_dev_attr_temp13_input.dev_attr.attr, + &sensor_dev_attr_temp13_label.dev_attr.attr, + &sensor_dev_attr_temp14_input.dev_attr.attr, + &sensor_dev_attr_temp14_label.dev_attr.attr, + &sensor_dev_attr_temp15_input.dev_attr.attr, + &sensor_dev_attr_temp15_label.dev_attr.attr, + &sensor_dev_attr_temp16_input.dev_attr.attr, + &sensor_dev_attr_temp16_label.dev_attr.attr, + &sensor_dev_attr_temp17_input.dev_attr.attr, + &sensor_dev_attr_temp17_label.dev_attr.attr, + &sensor_dev_attr_temp18_input.dev_attr.attr, + &sensor_dev_attr_temp18_label.dev_attr.attr, + &sensor_dev_attr_temp19_input.dev_attr.attr, + &sensor_dev_attr_temp19_label.dev_attr.attr, + &sensor_dev_attr_temp20_input.dev_attr.attr, + &sensor_dev_attr_temp20_label.dev_attr.attr, + NULL +}; +ATTRIBUTE_GROUPS(gaokun_ec_hwmon); + +/* -------------------------------------------------------------------------- */ +/* Modern Standby */ + +static int gaokun_ec_suspend(struct device *dev) +{ + struct gaokun_ec *ec = dev_get_drvdata(dev); + u8 req[] = MKREQ(0x02, 0xB2, 1, 0xDB); + int ret; + + if (ec->suspended) + return 0; + + ret = gaokun_ec_write(ec, req); + + if (ret) + return ret; + + ec->suspended = true; + + return 0; +} + +static int gaokun_ec_resume(struct device *dev) +{ + struct gaokun_ec *ec = dev_get_drvdata(dev); + u8 req[] = MKREQ(0x02, 0xB2, 1, 0xEB); + int ret; + int i; + + if (!ec->suspended) + return 0; + + for (i = 0; i < 3; ++i) { + ret = gaokun_ec_write(ec, req); + if (ret == 0) + break; + + msleep(100); /* EC need time to resume */ + }; + + ec->suspended = false; + + return 0; +} + +static void gaokun_aux_release(struct device *dev) +{ + struct auxiliary_device *adev = to_auxiliary_dev(dev); + + kfree(adev); +} + +static void gaokun_aux_remove(void *data) +{ + struct auxiliary_device *adev = data; + + auxiliary_device_delete(adev); + auxiliary_device_uninit(adev); +} + +static int gaokun_aux_init(struct device *parent, const char *name, + struct gaokun_ec *ec) +{ + struct auxiliary_device *adev; + int ret; + + adev = kzalloc(sizeof(*adev), GFP_KERNEL); + if (!adev) + return -ENOMEM; + + adev->name = name; + adev->id = 0; + adev->dev.parent = parent; + adev->dev.release = gaokun_aux_release; + adev->dev.platform_data = ec; + /* Allow aux devices to access parent's DT nodes directly */ + device_set_of_node_from_dev(&adev->dev, parent); + + ret = auxiliary_device_init(adev); + if (ret) { + kfree(adev); + return ret; + } + + ret = auxiliary_device_add(adev); + if (ret) { + auxiliary_device_uninit(adev); + return ret; + } + + return devm_add_action_or_reset(parent, gaokun_aux_remove, adev); +} + +/* -------------------------------------------------------------------------- */ +/* EC */ + +static irqreturn_t gaokun_ec_irq_handler(int irq, void *data) +{ + struct gaokun_ec *ec = data; + u8 req[] = MKREQ(EC_EVENT, EC_QUERY, 0); + u8 status, id; + int ret; + + ret = gaokun_ec_read_byte(ec, req, &id); + if (ret) + return IRQ_HANDLED; + + switch (id) { + case 0x0: /* No event */ + break; + + case EC_EVENT_LID: + gaokun_ec_psy_read_byte(ec, EC_LID_STATE, &status); + status = EC_LID_OPEN & status; + input_report_switch(ec->idev, SW_LID, !status); + input_sync(ec->idev); + break; + + default: + blocking_notifier_call_chain(&ec->notifier_list, id, ec); + } + + return IRQ_HANDLED; +} + +static int gaokun_ec_probe(struct i2c_client *client) +{ + struct device *dev = &client->dev; + struct gaokun_ec *ec; + int ret; + + ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL); + if (!ec) + return -ENOMEM; + + mutex_init(&ec->lock); + ec->client = client; + i2c_set_clientdata(client, ec); + BLOCKING_INIT_NOTIFIER_HEAD(&ec->notifier_list); + + /* Lid switch */ + ec->idev = devm_input_allocate_device(dev); + if (!ec->idev) + return -ENOMEM; + + ec->idev->name = "LID"; + ec->idev->phys = "gaokun-ec/input0"; + input_set_capability(ec->idev, EV_SW, SW_LID); + + ret = input_register_device(ec->idev); + if (ret) + return dev_err_probe(dev, ret, "Failed to register input device\n"); + + ret = gaokun_aux_init(dev, "psy", ec); + if (ret) + return ret; + + ret = gaokun_aux_init(dev, "ucsi", ec); + if (ret) + return ret; + + ret = devm_request_threaded_irq(dev, client->irq, NULL, + gaokun_ec_irq_handler, IRQF_ONESHOT, + dev_name(dev), ec); + if (ret) + return dev_err_probe(dev, ret, "Failed to request irq\n"); + + ec->hwmon_dev = hwmon_device_register_with_groups(dev, "gaokun_ec_hwmon", + ec, gaokun_ec_hwmon_groups); + if (IS_ERR(ec->hwmon_dev)) { + dev_err(dev, "Failed to register hwmon device\n"); + return PTR_ERR(ec->hwmon_dev); + } + + return 0; +} + +static void gaokun_ec_remove(struct i2c_client *client) +{ + struct gaokun_ec *ec = i2c_get_clientdata(client); + hwmon_device_unregister(ec->hwmon_dev); +} + +static const struct i2c_device_id gaokun_ec_id[] = { + { "gaokun-ec", }, + { } +}; +MODULE_DEVICE_TABLE(i2c, gaokun_ec_id); + +static const struct of_device_id gaokun_ec_of_match[] = { + { .compatible = "huawei,gaokun3-ec", }, + { } +}; +MODULE_DEVICE_TABLE(of, gaokun_ec_of_match); + +static const struct dev_pm_ops gaokun_ec_pm_ops = { + NOIRQ_SYSTEM_SLEEP_PM_OPS(gaokun_ec_suspend, gaokun_ec_resume) +}; + +static struct i2c_driver gaokun_ec_driver = { + .driver = { + .name = "gaokun-ec", + .of_match_table = gaokun_ec_of_match, + .pm = &gaokun_ec_pm_ops, + .dev_groups = gaokun_ec_groups, + }, + .probe = gaokun_ec_probe, + .remove = gaokun_ec_remove, + .id_table = gaokun_ec_id, +}; +module_i2c_driver(gaokun_ec_driver); + +MODULE_DESCRIPTION("HUAWEI Matebook E Go EC driver"); +MODULE_AUTHOR("Pengyu Luo "); +MODULE_LICENSE("GPL"); diff --git a/include/linux/platform_data/huawei-gaokun-ec.h b/include/linux/platform_data/huawei-gaokun-ec.h new file mode 100644 index 000000000..dfd177bd9 --- /dev/null +++ b/include/linux/platform_data/huawei-gaokun-ec.h @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Huawei Matebook E Go Embedded Controller + * + * Copyright (C) 2024 Pengyu Luo + */ + +#ifndef __HUAWEI_GAOKUN_EC_H__ +#define __HUAWEI_GAOKUN_EC_H__ + +#define GAOKUN_UCSI_CCI_SIZE 4 +#define GAOKUN_UCSI_DATA_SIZE 16 +#define GAOKUN_UCSI_READ_SIZE (GAOKUN_UCSI_CCI_SIZE + GAOKUN_UCSI_DATA_SIZE) +#define GAOKUN_UCSI_WRITE_SIZE 0x18 + +#define GAOKUN_UCSI_NO_PORT_UPDATE (-1) + +#define GAOKUN_SMART_CHARGE_DATA_SIZE 4 /* mode, delay, start, end */ + +/* -------------------------------------------------------------------------- */ + +struct gaokun_ec; +struct gaokun_ucsi_reg; +struct notifier_block; + +#define GAOKUN_MOD_NAME "huawei_gaokun_ec" +#define GAOKUN_DEV_PSY "psy" +#define GAOKUN_DEV_UCSI "ucsi" + +/* -------------------------------------------------------------------------- */ +/* Common API */ + +int gaokun_ec_register_notify(struct gaokun_ec *ec, + struct notifier_block *nb); +void gaokun_ec_unregister_notify(struct gaokun_ec *ec, + struct notifier_block *nb); + +int gaokun_ec_read(struct gaokun_ec *ec, const u8 *req, + size_t resp_len, u8 *resp); +int gaokun_ec_write(struct gaokun_ec *ec, u8 *req); +int gaokun_ec_read_byte(struct gaokun_ec *ec, u8 *req, u8 *byte); + +/* -------------------------------------------------------------------------- */ +/* API For PSY */ + +int gaokun_ec_psy_multi_read(struct gaokun_ec *ec, u8 reg, + size_t resp_len, u8 *resp); + +static inline int gaokun_ec_psy_read_byte(struct gaokun_ec *ec, + u8 reg, u8 *byte) +{ + return gaokun_ec_psy_multi_read(ec, reg, sizeof(*byte), byte); +} + +static inline int gaokun_ec_psy_read_word(struct gaokun_ec *ec, + u8 reg, u16 *word) +{ + return gaokun_ec_psy_multi_read(ec, reg, sizeof(*word), (u8 *)word); +} + +int gaokun_ec_psy_get_smart_charge(struct gaokun_ec *ec, + u8 data[GAOKUN_SMART_CHARGE_DATA_SIZE]); +int gaokun_ec_psy_set_smart_charge(struct gaokun_ec *ec, + u8 data[GAOKUN_SMART_CHARGE_DATA_SIZE]); + +int gaokun_ec_psy_get_smart_charge_enable(struct gaokun_ec *ec, bool *on); +int gaokun_ec_psy_set_smart_charge_enable(struct gaokun_ec *ec, bool on); + +/* -------------------------------------------------------------------------- */ +/* API For UCSI */ + +int gaokun_ec_ucsi_read(struct gaokun_ec *ec, u8 resp[GAOKUN_UCSI_READ_SIZE]); +int gaokun_ec_ucsi_write(struct gaokun_ec *ec, + const u8 req[GAOKUN_UCSI_WRITE_SIZE]); + +int gaokun_ec_ucsi_get_reg(struct gaokun_ec *ec, struct gaokun_ucsi_reg *ureg); +int gaokun_ec_ucsi_pan_ack(struct gaokun_ec *ec, int port_id); + + +#endif /* __HUAWEI_GAOKUN_EC_H__ */ From patchwork Mon Jan 13 17:51:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pengyu Luo X-Patchwork-Id: 857539 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 29A6E1BD9FB; Mon, 13 Jan 2025 17:53:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736790785; cv=none; b=ZkHa4+d3A/kHhw++Hl9yyZFviLB4C7iHhlKMoJjxua9/7ptjvgqrazScsI+CPc+M0LqYrDry5/WgXDYi88yHNsODD+V8P9X1Ri/eClILxlX0p1pPrRXwb1PsQ4USbZ7kLkEieD3Dz3Bl8/OvjwZK7fj5rMJfxYMBmIUGyKO+3fc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736790785; c=relaxed/simple; bh=tVZ90TLcc9hnhgxdnwlzwfTH9WT3BCV3oE++gMl82Uk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=XglBm/7pEQTwDujmPwcxsONWWkyiGX+1uOZ3vD36XTrrrmJuFb3ALOqJlxXyoNp6S2fU86lXL6AC+/sbSuTlA2uZoIZwoQNVyjLp7WwoJF4C16p20avk/h6b2RjT7/9m2pfsF4pMlhH6ssI3VcxIH5LYsFzawqIFtgZC/gGXjp4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MIfY8p1b; arc=none smtp.client-ip=209.85.216.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MIfY8p1b" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2ee397a82f6so8050195a91.2; Mon, 13 Jan 2025 09:53:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736790783; x=1737395583; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=79vv5qRnf5VUTD34W/u3Z54GdzZcFo1kzpfwaK7haEg=; b=MIfY8p1bgarH0hHhJA8PmJ12+OA37VVjmdx1m5QmOmMGgnc4yWTZ0VkIBQf76LU5sr iJBZg23H3LMC+S9r17cwIYbQp4NqVBTxXaqppU3eRsh063+OdmAHpLCLvMs5nKNuXYrT 8bFheCL9FESXlpPGCverQF/XKqr2mUu9Zn8hhPmziWdp0rekBrhypprSIiCmwbrSJP6C ELAJTioX09F/WruhjvQ8WP+oPCX+RqrCg2Z+I/QU5y+HfwLBbpVTqck9+ZKf8QibZ0Xt R/5tG0ELocbCIrwPrbLEGpDJ14xCUWer0cIXl1iFebbZHNr13FeSCTQYM+I3ofZLRLTK 6c0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736790783; x=1737395583; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=79vv5qRnf5VUTD34W/u3Z54GdzZcFo1kzpfwaK7haEg=; b=LPsPHpHdo1D7trofK/6bID2LQEqUgQWZ4rw10iJtBztukF8vakRca764yhuDZQ6XOg RFeF4+GQOjc8D7eB7AqxM7L00WE/TTvEB3RaNJyuDCADIXZgN2YrVyoPl8M61/ErG/BB KOuQkJTPvgBE7+Xe4yhNAWYiw8aHjKIlQK45O2mNFnfv+tz3Idn2dy+NjNO1Eosw44jo dVamhMJlcrr1Dh5lghpax/Fhz16+tUQigoxchnMVvU1/lg0O36kRhnCoLAEnQoQig5fD ukDR1dR0Kz79xIcUvbFwSyaNr3VGIh/v8hTqPR9i9IgoWBnm4F2tI8dEFz8PYUhC2/eC 8i5A== X-Forwarded-Encrypted: i=1; AJvYcCUublJFdMXkP+DkAjQfCg+9/NDHP56FVmKgsFmXRTWuHEK6eF9jOHtFdUOShKqBgxt0pqcDvkXNA0hMcTg=@vger.kernel.org, AJvYcCVcTbBMsOyLQYEj8/0kj2AmPkWgY96fVT6cH+49+Qu0KdgiUtAz133F62m0JZEmUuMcxHinymCoekJj@vger.kernel.org X-Gm-Message-State: AOJu0Ywv0/EI2dDv7SLDHo40gquZg5Z4alxB8dFm0GI6oqxaQSsLqze9 6ZypZRqZYiCaiJv/ehOqA8ZNiJRBP8UTndheWDbwbRR68o7tDSBI1Ox96suR9iycFw== X-Gm-Gg: ASbGncs5JuY6AZacHx0HvU3xFy/RptQSBZX3Dq20P82xqEtAykaxFxFd9jenQaaUAeE Az+HpsAUgM8PgkRYFRs02CtCRMY2sOSJmiCfiok7TGnH4Lx700LjQtVTIOg+RVcI8LJtA62haGR GCv40cfkl1eEOdD93T0nYdGIpsZyQFUAXyR2YvY/1j0ID5WQ9BlQpYBL8GJhIuzB6j70NJ2qWYw Y1C7rSkDMduIkJ6zXnOaagZOFmQ8IpJ5bJyC9mPquBAG07UuW9Wlrg= X-Google-Smtp-Source: AGHT+IHbe+EU0u+vYn02K4peLD4fFtvyRBm69Bsz3NzncYR0z93HeKYcBXyuS/wtO87b5wQDSjlSiA== X-Received: by 2002:a17:90b:51c1:b0:2ee:f687:6acb with SMTP id 98e67ed59e1d1-2f548f35587mr30340159a91.13.1736790783334; Mon, 13 Jan 2025 09:53:03 -0800 (PST) Received: from nuvole.. ([144.202.86.13]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f10e0c4sm55724905ad.24.2025.01.13.09.52.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 09:53:02 -0800 (PST) From: Pengyu Luo To: Heikki Krogerus , Greg Kroah-Hartman Cc: Pengyu Luo , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 3/6] usb: typec: ucsi: Add a macro definition for UCSI v1.0 Date: Tue, 14 Jan 2025 01:51:25 +0800 Message-ID: <20250113175131.590683-1-mitltlatltl@gmail.com> X-Mailer: git-send-email 2.47.1 Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Many platforms from Qualcomm(before X elite) still use UCSI v1.0, part of them hardcoded the version number to dsdt, add this for them. Signed-off-by: Pengyu Luo --- drivers/usb/typec/ucsi/ucsi.h | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 5ff369c24..82735eb34 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -30,6 +30,7 @@ struct dentry; #define UCSIv2_MESSAGE_OUT 272 /* UCSI versions */ +#define UCSI_VERSION_1_0 0x0100 #define UCSI_VERSION_1_1 0x0110 #define UCSI_VERSION_1_2 0x0120 #define UCSI_VERSION_2_0 0x0200 From patchwork Mon Jan 13 17:51:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pengyu Luo X-Patchwork-Id: 857188 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 E2B6A1BD9DE; Mon, 13 Jan 2025 17:53:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736790791; cv=none; b=pthPGqfU+YeabaKpJnxsONZYW9ZbfEDpJJInlp5h6/3hIQlzxSBKo7U2dwrarwdtyOfDPKfq4/xnMm3dvlzuOBFDcto1ILqkF0gY4pEgJNV6AnSl95UKv7r9J6MkdmZkuGGaYt/Cw9FvEj2RDj0vC8HGvZQJLi+23K4TI0cyNGI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736790791; c=relaxed/simple; bh=stoETWSuuKd8yGMa+Oz06ke5DkTEN2Qp3anAcJVoLf4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LYETTEqko6Hyz4AkgxSr//vFt0HB/ZgFbwrjGUyQXIv61LTM67gjJNW4VaLs+MtI6D0zCrm5mlIkWGssgQcbO5DraA0mu2qOGu28upz1DAxPXBwzerI4ej0yA/XE9hniBgse49AXL2QK5N4PkYgT6JG3VE8p6ieuRTIJBxAPc90= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dVz0obvX; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dVz0obvX" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2165448243fso96714385ad.1; Mon, 13 Jan 2025 09:53:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736790789; x=1737395589; 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=lJ2wQDFgOB2rHz2yZe88WA1LR28iHgqRQU+ybcJDGfI=; b=dVz0obvXHqBIpe4Jt1QU632q6lVTsC787CgZMnB+tpgqObZnHxkvdWrcGvYsucnfAI O299KOlQJSarPa6cOvaKFQG33XTjPrkDPON0QQP7rRPCv8D+8Y8H+5jI554VFOMYenFU h+bP9fzZvN9RDCvXJORz49vXwvBf6DGegHyqNtFd9FgBVMeEwP5GqqeiDNitwSRKY8uz oSFqUbvF4FpE79OvXf73bwQ8O+i/j+5vJcj8jsDoRFQat7uJh/J6pASAUJQl+/KfMqtV LlEiNYo2HYcRdCdlv4jqTs0BsPnAMw5tHYEG4vSkkUhkdS2+pxK7RQzkaB6hl7vKPyOC mwHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736790789; x=1737395589; 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=lJ2wQDFgOB2rHz2yZe88WA1LR28iHgqRQU+ybcJDGfI=; b=UTW6UDC8Kd9hKRfTWp57FCbCmRfKzBWAawejgcvAb6Te1/WAofE6wM+6/60PYgaW1w DnkYPPfrnVEji8HAmpDHplmpac+3QxEqt1yYt9RMuoEs3iIuKTi4UbNOLmU377mUlbpz n9xkH8weZp0bmdj4de7DBgul9A5jJ51U2xdRezZqsNta8jSYRg0bHaxiGAiQokiWIJIb 9BWEv2dRAcF6hcDJADbTTG6/AAdDpWoaYI2DgWnUxKBGmELfqwJCTP7idwLZ1t6Zqe+N txvME/WbFkZu4850ZCr7WfQcYmgDYs4LFm5pkKYcwZbWuvUXTQMrj/vsy2hT2KuHwv9c 6+KQ== X-Forwarded-Encrypted: i=1; AJvYcCX6YQYBC5/GIWduU2nzJcjCBfUrFIjYYpwQt0DYoxGqnT0181lXKdzNMpluuVniquKRW7YjyEx5Ekc=@vger.kernel.org X-Gm-Message-State: AOJu0YxMuIlESNigchFPp74P8rtFbl2U2phDxLVQeldPtvQSYUw55NRc GbCrJ+HR+Uv18cSyUi9wLo+6CnP8DlSPe0NMKfp1RUbWGfvhEzN1 X-Gm-Gg: ASbGncuVNf/DzJZYPuXcrd4DGMcGPF2R7feBxnnyF1bWgYxQsxJii92UxDNsCIZBEyo ndiRdD8TAzLG9bBpCOmj8iD/mtcEqwutW10PkZLWRrVz7htriOAkgnW+kHJKDl7NwrJF9p0L2pm vnhaEisk1Z9Q4vM6PpcAZEvqm5ZKEZRltQllkbNxlP0l3s/+HDiIaDRqsn4eY/Dd/LUZy5kSXTs MEC+oyTuCgF7D/y/ITSZKPfIMdHVbG3EMvqFscIeI+x8GCA3DMoid8= X-Google-Smtp-Source: AGHT+IEQjB+Gtkl/fD8nnEYuLb3pv2Z0GOPfreRSb4KCCqCb8lbLxwMsdScyXfAhaQlRDsExbCax4w== X-Received: by 2002:a17:903:185:b0:216:4676:dfaf with SMTP id d9443c01a7336-21a83f96abamr330284875ad.34.1736790789135; Mon, 13 Jan 2025 09:53:09 -0800 (PST) Received: from nuvole.. ([144.202.86.13]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f10e0c4sm55724905ad.24.2025.01.13.09.53.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 09:53:08 -0800 (PST) From: Pengyu Luo To: Heikki Krogerus , Greg Kroah-Hartman , Pengyu Luo Cc: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Subject: [PATCH v3 4/6] usb: typec: ucsi: add Huawei Matebook E Go ucsi driver Date: Tue, 14 Jan 2025 01:51:26 +0800 Message-ID: <20250113175131.590683-2-mitltlatltl@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250113175131.590683-1-mitltlatltl@gmail.com> References: <20250113175131.590683-1-mitltlatltl@gmail.com> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The Huawei Matebook E Go tablet implements the UCSI interface in the onboard EC. Add the glue driver to interface with the platform's UCSI implementation. Signed-off-by: Pengyu Luo --- drivers/usb/typec/ucsi/Kconfig | 11 + drivers/usb/typec/ucsi/Makefile | 1 + drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 526 ++++++++++++++++++++ 3 files changed, 538 insertions(+) create mode 100644 drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c diff --git a/drivers/usb/typec/ucsi/Kconfig b/drivers/usb/typec/ucsi/Kconfig index 75559601f..e94956d27 100644 --- a/drivers/usb/typec/ucsi/Kconfig +++ b/drivers/usb/typec/ucsi/Kconfig @@ -91,4 +91,15 @@ config UCSI_LENOVO_YOGA_C630 To compile the driver as a module, choose M here: the module will be called ucsi_yoga_c630. +config UCSI_HUAWEI_GAOKUN + tristate "UCSI Interface Driver for Huawei Matebook E Go" + depends on EC_HUAWEI_GAOKUN + select DRM_AUX_HPD_BRIDGE + help + This driver enables UCSI support on the Huawei Matebook E Go tablet, + which is a sc8280xp-based 2-in-1 tablet. + + To compile the driver as a module, choose M here: the module will be + called ucsi_huawei_gaokun. + endif diff --git a/drivers/usb/typec/ucsi/Makefile b/drivers/usb/typec/ucsi/Makefile index be98a8791..dbc571763 100644 --- a/drivers/usb/typec/ucsi/Makefile +++ b/drivers/usb/typec/ucsi/Makefile @@ -23,3 +23,4 @@ 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 +obj-$(CONFIG_UCSI_HUAWEI_GAOKUN) += ucsi_huawei_gaokun.o diff --git a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c new file mode 100644 index 000000000..da1993805 --- /dev/null +++ b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c @@ -0,0 +1,527 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * ucsi-huawei-gaokun - A UCSI driver for HUAWEI Matebook E Go + * + * reference: drivers/usb/typec/ucsi/ucsi_yoga_c630.c + * drivers/usb/typec/ucsi/ucsi_glink.c + * drivers/soc/qcom/pmic_glink_altmode.c + * + * Copyright (C) 2024 Pengyu Luo + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ucsi.h" + +#define EC_EVENT_UCSI 0x21 +#define EC_EVENT_USB 0x22 + +#define GAOKUN_CCX_MASK GENMASK(1, 0) +#define GAOKUN_MUX_MASK GENMASK(3, 2) + +#define GAOKUN_DPAM_MASK GENMASK(3, 0) +#define GAOKUN_HPD_STATE_MASK BIT(4) +#define GAOKUN_HPD_IRQ_MASK BIT(5) + +#define GET_IDX(updt) (ffs(updt) - 1) + +#define CCX_TO_ORI(ccx) (++ccx % 3) /* convert ccx to enum typec_orientation */ + +/* Configuration Channel Extension */ +enum gaokun_ucsi_ccx { + USBC_CCX_NORMAL, + USBC_CCX_REVERSE, + USBC_CCX_NONE, +}; + +enum gaokun_ucsi_mux { + USBC_MUX_NONE, + USBC_MUX_USB_2L, + USBC_MUX_DP_4L, + USBC_MUX_USB_DP, +}; +/* based on pmic_glink_altmode_pin_assignment */ +enum gaokun_ucsi_dpam_pan { /* DP Alt Mode Pin Assignments */ + USBC_DPAM_PAN_NONE, + USBC_DPAM_PAN_A, /* Not supported after USB Type-C Standard v1.0b */ + USBC_DPAM_PAN_B, /* Not supported after USB Type-C Standard v1.0b */ + USBC_DPAM_PAN_C, /* USBC_DPAM_PAN_C_REVERSE - 6 */ + USBC_DPAM_PAN_D, + USBC_DPAM_PAN_E, + USBC_DPAM_PAN_F, /* Not supported after USB Type-C Standard v1.0b */ + USBC_DPAM_PAN_A_REVERSE,/* Not supported after USB Type-C Standard v1.0b */ + USBC_DPAM_PAN_B_REVERSE,/* Not supported after USB Type-C Standard v1.0b */ + USBC_DPAM_PAN_C_REVERSE, + USBC_DPAM_PAN_D_REVERSE, + USBC_DPAM_PAN_E_REVERSE, + USBC_DPAM_PAN_F_REVERSE,/* Not supported after USB Type-C Standard v1.0b */ +}; + +struct gaokun_ucsi_reg { + u8 num_ports; + u8 port_updt; + u8 port_data[4]; + u8 checksum; + u8 reserved; +} __packed; + +struct gaokun_ucsi_port { + struct completion usb_ack; + spinlock_t lock; + + struct gaokun_ucsi *ucsi; + struct auxiliary_device *bridge; + + int idx; + enum gaokun_ucsi_ccx ccx; + enum gaokun_ucsi_mux mux; + u8 mode; + u16 svid; + u8 hpd_state; + u8 hpd_irq; +}; + +struct gaokun_ucsi { + struct gaokun_ec *ec; + struct ucsi *ucsi; + struct gaokun_ucsi_port *ports; + struct device *dev; + struct delayed_work work; + struct notifier_block nb; + u16 version; + u8 num_ports; +}; + +/* -------------------------------------------------------------------------- */ +/* For UCSI */ + +static int gaokun_ucsi_read_version(struct ucsi *ucsi, u16 *version) +{ + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi); + + *version = uec->version; + + return 0; +} + +static int gaokun_ucsi_read_cci(struct ucsi *ucsi, u32 *cci) +{ + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi); + u8 buf[GAOKUN_UCSI_READ_SIZE]; + int ret; + + ret = gaokun_ec_ucsi_read(uec->ec, buf); + if (ret) + return ret; + + memcpy(cci, buf, sizeof(*cci)); + + return 0; +} + +static int gaokun_ucsi_read_message_in(struct ucsi *ucsi, + void *val, size_t val_len) +{ + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi); + u8 buf[GAOKUN_UCSI_READ_SIZE]; + int ret; + + ret = gaokun_ec_ucsi_read(uec->ec, buf); + if (ret) + return ret; + + memcpy(val, buf + GAOKUN_UCSI_CCI_SIZE, + min(val_len, GAOKUN_UCSI_DATA_SIZE)); + + return 0; +} + +static int gaokun_ucsi_async_control(struct ucsi *ucsi, u64 command) +{ + struct gaokun_ucsi *uec = ucsi_get_drvdata(ucsi); + u8 buf[GAOKUN_UCSI_WRITE_SIZE] = {}; + + memcpy(buf, &command, sizeof(command)); + + return gaokun_ec_ucsi_write(uec->ec, buf); +} + +static void gaokun_ucsi_update_connector(struct ucsi_connector *con) +{ + struct gaokun_ucsi *uec = ucsi_get_drvdata(con->ucsi); + + if (con->num > uec->num_ports) + return; + + con->typec_cap.orientation_aware = true; +} + +static void gaokun_set_orientation(struct ucsi_connector *con, + struct gaokun_ucsi_port *port) +{ + enum gaokun_ucsi_ccx ccx; + unsigned long flags; + + spin_lock_irqsave(&port->lock, flags); + ccx = port->ccx; + spin_unlock_irqrestore(&port->lock, flags); + + typec_set_orientation(con->port, CCX_TO_ORI(ccx)); +} + +static void gaokun_ucsi_connector_status(struct ucsi_connector *con) +{ + struct gaokun_ucsi *uec = ucsi_get_drvdata(con->ucsi); + int idx; + + idx = con->num - 1; + if (con->num > uec->num_ports) { + dev_warn(uec->dev, "set orientation out of range: con%d\n", idx); + return; + } + + gaokun_set_orientation(con, &uec->ports[idx]); +} + +const struct ucsi_operations gaokun_ucsi_ops = { + .read_version = gaokun_ucsi_read_version, + .read_cci = gaokun_ucsi_read_cci, + .read_message_in = gaokun_ucsi_read_message_in, + .sync_control = ucsi_sync_control_common, + .async_control = gaokun_ucsi_async_control, + .update_connector = gaokun_ucsi_update_connector, + .connector_status = gaokun_ucsi_connector_status, +}; + +/* -------------------------------------------------------------------------- */ +/* For Altmode */ + +static void gaokun_ucsi_port_update(struct gaokun_ucsi_port *port, + const u8 *port_data) +{ + struct gaokun_ucsi *uec = port->ucsi; + int offset = port->idx * 2; /* every port has 2 Bytes data */ + unsigned long flags; + u8 dcc, ddi; + + dcc = port_data[offset]; + ddi = port_data[offset + 1]; + + spin_lock_irqsave(&port->lock, flags); + + port->ccx = FIELD_GET(GAOKUN_CCX_MASK, dcc); + port->mux = FIELD_GET(GAOKUN_MUX_MASK, dcc); + port->mode = FIELD_GET(GAOKUN_DPAM_MASK, ddi); + port->hpd_state = FIELD_GET(GAOKUN_HPD_STATE_MASK, ddi); + port->hpd_irq = FIELD_GET(GAOKUN_HPD_IRQ_MASK, ddi); + + /* Mode and SVID are unused; keeping them to make things clearer */ + switch (port->mode) { + case USBC_DPAM_PAN_C: + case USBC_DPAM_PAN_C_REVERSE: + port->mode = DP_PIN_ASSIGN_C; /* correct it for usb later */ + break; + case USBC_DPAM_PAN_D: + case USBC_DPAM_PAN_D_REVERSE: + port->mode = DP_PIN_ASSIGN_D; + break; + case USBC_DPAM_PAN_E: + case USBC_DPAM_PAN_E_REVERSE: + port->mode = DP_PIN_ASSIGN_E; + break; + case USBC_DPAM_PAN_NONE: + port->mode = TYPEC_STATE_SAFE; + break; + default: + dev_warn(uec->dev, "unknown mode %d\n", port->mode); + break; + } + + switch (port->mux) { + case USBC_MUX_NONE: + port->svid = 0; + break; + case USBC_MUX_USB_2L: + port->svid = USB_SID_PD; + port->mode = TYPEC_STATE_USB; /* same as PAN_C, correct it */ + break; + case USBC_MUX_DP_4L: + case USBC_MUX_USB_DP: + port->svid = USB_SID_DISPLAYPORT; + break; + default: + dev_warn(uec->dev, "unknown mux state %d\n", port->mux); + break; + } + + spin_unlock_irqrestore(&port->lock, flags); +} + +static int gaokun_ucsi_refresh(struct gaokun_ucsi *uec) +{ + struct gaokun_ucsi_reg ureg; + int ret, idx; + + ret = gaokun_ec_ucsi_get_reg(uec->ec, &ureg); + if (ret) + return GAOKUN_UCSI_NO_PORT_UPDATE; + + uec->num_ports = ureg.num_ports; + idx = GET_IDX(ureg.port_updt); + + if (idx < 0 || idx >= ureg.num_ports) + return GAOKUN_UCSI_NO_PORT_UPDATE; + + gaokun_ucsi_port_update(&uec->ports[idx], ureg.port_data); + return idx; +} + +static void gaokun_ucsi_handle_altmode(struct gaokun_ucsi_port *port) +{ + struct gaokun_ucsi *uec = port->ucsi; + int idx = port->idx; + + if (idx >= uec->ucsi->cap.num_connectors) { + dev_warn(uec->dev, "altmode port out of range: %d\n", idx); + return; + } + + /* UCSI callback .connector_status() have set orientation */ + if (port->bridge) + drm_aux_hpd_bridge_notify(&port->bridge->dev, + port->hpd_state ? + connector_status_connected : + connector_status_disconnected); + + gaokun_ec_ucsi_pan_ack(uec->ec, port->idx); +} + +static void gaokun_ucsi_altmode_notify_ind(struct gaokun_ucsi *uec) +{ + int idx; + + if (!uec->ucsi->connector) { /* slow to register */ + dev_err(uec->dev, "ucsi connector is not initialized yet\n"); + return; + } + + idx = gaokun_ucsi_refresh(uec); + if (idx == GAOKUN_UCSI_NO_PORT_UPDATE) + gaokun_ec_ucsi_pan_ack(uec->ec, idx); /* ack directly if no update */ + else + gaokun_ucsi_handle_altmode(&uec->ports[idx]); +} + +/* + * When inserting, 2 UCSI events(connector change) are followed by USB events. + * If we received one USB event, that means USB events are not blocked, so we + * can complelte for all ports, and we should signal all events. + */ +static void gaokun_ucsi_complete_usb_ack(struct gaokun_ucsi *uec) +{ + struct gaokun_ucsi_port *port; + int idx = 0; + + while (idx < uec->num_ports) { + port = &uec->ports[idx++]; + if (!completion_done(&port->usb_ack)) + complete_all(&port->usb_ack); + } +} + +/* + * USB event is necessary for enabling altmode, the event should follow + * UCSI event, if not after timeout(this notify may be disabled somehow), + * then force to enable altmode. + */ +static void gaokun_ucsi_handle_no_usb_event(struct gaokun_ucsi *uec, int idx) +{ + struct gaokun_ucsi_port *port; + + port = &uec->ports[idx]; + if (!wait_for_completion_timeout(&port->usb_ack, 2 * HZ)) { + dev_warn(uec->dev, "No USB EVENT, triggered by UCSI EVENT"); + gaokun_ucsi_altmode_notify_ind(uec); + } +} + +static int gaokun_ucsi_notify(struct notifier_block *nb, + unsigned long action, void *data) +{ + u32 cci; + struct gaokun_ucsi *uec = container_of(nb, struct gaokun_ucsi, nb); + + switch (action) { + case EC_EVENT_USB: + gaokun_ucsi_complete_usb_ack(uec); + gaokun_ucsi_altmode_notify_ind(uec); + return NOTIFY_OK; + + case EC_EVENT_UCSI: + gaokun_ucsi_read_cci(uec->ucsi, &cci); + ucsi_notify_common(uec->ucsi, cci); + if (UCSI_CCI_CONNECTOR(cci)) + gaokun_ucsi_handle_no_usb_event(uec, UCSI_CCI_CONNECTOR(cci) - 1); + + return NOTIFY_OK; + + default: + return NOTIFY_DONE; + } +} + +static int gaokun_ucsi_ports_init(struct gaokun_ucsi *uec) +{ + struct gaokun_ucsi_port *ucsi_port; + struct gaokun_ucsi_reg ureg = {}; + struct device *dev = uec->dev; + struct fwnode_handle *fwnode; + int i, ret, num_ports; + u32 port; + + gaokun_ec_ucsi_get_reg(uec->ec, &ureg); + num_ports = ureg.num_ports; + uec->num_ports = num_ports; + + uec->ports = devm_kcalloc(dev, num_ports, sizeof(*(uec->ports)), + GFP_KERNEL); + if (!uec->ports) + return -ENOMEM; + + for (i = 0; i < num_ports; ++i) { + ucsi_port = &uec->ports[i]; + ucsi_port->ccx = USBC_CCX_NONE; + ucsi_port->idx = i; + ucsi_port->ucsi = uec; + init_completion(&ucsi_port->usb_ack); + spin_lock_init(&ucsi_port->lock); + } + + device_for_each_child_node(dev, fwnode) { + ret = fwnode_property_read_u32(fwnode, "reg", &port); + if (ret < 0) { + dev_err(dev, "missing reg property of %pOFn\n", fwnode); + fwnode_handle_put(fwnode); + return ret; + } + + if (port >= num_ports) { + dev_warn(dev, "invalid connector number %d, ignoring\n", port); + continue; + } + + ucsi_port = &uec->ports[port]; + ucsi_port->bridge = devm_drm_dp_hpd_bridge_alloc(dev, to_of_node(fwnode)); + if (IS_ERR(ucsi_port->bridge)) { + fwnode_handle_put(fwnode); + return PTR_ERR(ucsi_port->bridge); + } + } + + for (i = 0; i < num_ports; i++) { + if (!uec->ports[i].bridge) + continue; + + ret = devm_drm_dp_hpd_bridge_add(dev, uec->ports[i].bridge); + if (ret) + return ret; + } + + return 0; +} + +static void gaokun_ucsi_register_worker(struct work_struct *work) +{ + struct gaokun_ucsi *uec; + struct ucsi *ucsi; + int ret; + + uec = container_of(work, struct gaokun_ucsi, work.work); + ucsi = uec->ucsi; + + ret = gaokun_ec_register_notify(uec->ec, &uec->nb); + if (ret) { + dev_err_probe(ucsi->dev, ret, "notifier register failed\n"); + return; + } + + ret = ucsi_register(ucsi); + if (ret) + dev_err_probe(ucsi->dev, ret, "ucsi register failed\n"); +} + +static int gaokun_ucsi_probe(struct auxiliary_device *adev, + const struct auxiliary_device_id *id) +{ + struct gaokun_ec *ec = adev->dev.platform_data; + struct device *dev = &adev->dev; + struct gaokun_ucsi *uec; + int ret; + + uec = devm_kzalloc(dev, sizeof(*uec), GFP_KERNEL); + if (!uec) + return -ENOMEM; + + uec->ec = ec; + uec->dev = dev; + uec->version = UCSI_VERSION_1_0; + uec->nb.notifier_call = gaokun_ucsi_notify; + + INIT_DELAYED_WORK(&uec->work, gaokun_ucsi_register_worker); + + ret = gaokun_ucsi_ports_init(uec); + if (ret) + return ret; + + uec->ucsi = ucsi_create(dev, &gaokun_ucsi_ops); + if (IS_ERR(uec->ucsi)) + return PTR_ERR(uec->ucsi); + + ucsi_set_drvdata(uec->ucsi, uec); + auxiliary_set_drvdata(adev, uec); + + /* EC can't handle UCSI properly in the early stage */ + schedule_delayed_work(&uec->work, 3 * HZ); + + return 0; +} + +static void gaokun_ucsi_remove(struct auxiliary_device *adev) +{ + struct gaokun_ucsi *uec = auxiliary_get_drvdata(adev); + + gaokun_ec_unregister_notify(uec->ec, &uec->nb); + ucsi_unregister(uec->ucsi); + ucsi_destroy(uec->ucsi); +} + +static const struct auxiliary_device_id gaokun_ucsi_id_table[] = { + { .name = GAOKUN_MOD_NAME "." GAOKUN_DEV_UCSI, }, + {} +}; +MODULE_DEVICE_TABLE(auxiliary, gaokun_ucsi_id_table); + +static struct auxiliary_driver gaokun_ucsi_driver = { + .name = GAOKUN_DEV_UCSI, + .id_table = gaokun_ucsi_id_table, + .probe = gaokun_ucsi_probe, + .remove = gaokun_ucsi_remove, +}; + +module_auxiliary_driver(gaokun_ucsi_driver); + +MODULE_DESCRIPTION("HUAWEI Matebook E Go UCSI driver"); +MODULE_LICENSE("GPL");