From nobody Fri Sep 25 22:19:31 2026 Received: from mail-43102.protonmail.ch (mail-43102.protonmail.ch [185.70.43.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 879C53290BD for ; Tue, 8 Sep 2026 01:57:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.102 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788832638; cv=none; b=qdumbpZQVmPORLgj+fPS2CVpm86XJnMF2WJxqZp2cSsRHT2SPVmrobgBP6D7sNo6okqJzg4ovNdR/ORRO0xE41Bd2Bbd3tFGH1T0qK69IbeqnHaZ6YarIE0MB126SRt0aIL3pnqe3fM3reI0LP1feKEeDhy0fhKnpH74efgsbVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788832638; c=relaxed/simple; bh=oXcWliJZrFYGn9oUL9Dt2qIuZ3mHzre9fMagf0Dwm+A=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hnGsGbrFCVR2ng3IjI/06zhqGZd8I/A9EMb2lgM9BZsFUDJOfTCdm1UBOuvcZ+JVXI/5DawQXVM8/0ZzTaUTk0iE4Ywh9NJI6GXvMuxFAbLg926IFgGS6OJtUlT7A+7yCNmrETqqE+M9EdWxw7I76Cx0rAljdIkcxIohGwNto1w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=qkksedQy; arc=none smtp.client-ip=185.70.43.102 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="qkksedQy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1788832633; x=1789091833; bh=dkN5Ib9x7TMwaPTvQvcjGgmjcysTnaep98xds7uLb/w=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=qkksedQy/bUoWvTVqz/pdckkuWqA99uBtf5EIBtyxwqvyU01q53KedcQsd2RMN3KW 644cqc1LjIA+1BfA5dsaDScNTWn9ajnhIoVYXPISfi3kMEwyTvpHrXxJO300pPvD/x RcbTP0AiiVT8DIxnD//+XZGltOz+wd24tR2TP1RPgrFNFmsxlsdMwsngCRC6XB0UFK 5MrKbpP6a2rTXSWJa2B5sQdXoy1zJz1NqrKeqqLpVSTy3yRXoZfQfxf5jppLafKtXx PF071rRk51pxKcFkgMc+MjAAWGPsAeeYq+EUTTZPUe4d/R9jdFrBas33U9XqBe5R1a 1obcQp0hfJd4g== Date: Tue, 08 Sep 2026 01:57:07 +0000 To: Anvesh Jain P , Sibi Sankar , Hans de Goede , =?utf-8?Q?Ilpo_J=C3=A4rvinen?= From: Birk Skyum Cc: Bryan O'Donoghue , Guenter Roeck , Jonathan Corbet , Shuah Khan , Randy Dunlap , Daniel J Blueman , Maya Matuszczyk , linux-arm-msm@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/2] platform: arm64: qcom-hamoa-ec: monitor Slim 7x legacy sensors Message-ID: <20260908015650.84401-2-birk.skyum@pm.me> In-Reply-To: <20260908015650.84401-1-birk.skyum@pm.me> References: <20260908015650.84401-1-birk.skyum@pm.me> Feedback-ID: 21326998:user:proton X-Pm-Message-ID: 294115e400cccced0ad229cbaed5c2ee5b4c72ce Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The Yoga Slim 7x EC returns an all-zero response to the reference-board thermal capability command. Probe fails even though its legacy RPM and thermistor queries work and firmware controls the fan automatically. Select a read-only path for the Lenovo compatible. Expose its first RPM channel and thermistor through hwmon, validating the transfer length and RPM payload count and treating the thermistor's 0xff sentinel as ENODATA. Do not enable SCI, register manual cooling controls or send standby commands on this path. Keep the reference-board behavior and strict capability validation unchanged. The transactions are documented by the Yoga's FC22 and FC29/TZ39 DSDT methods and Maya Matuszczyk's EC protocol notes. Anvesh Jain P's pending fan-profile series also documents the RPM query. This change does not include its calibration, LUT programming or temperature reporting. Link: https://github.com/aarch64-laptops/build/blob/2e58842f5fa2f87771c2df0= 17ae4d8c65225ef10/misc/lenovo-yoga-slim-7x/acpi/dsdt.dsl Link: https://lkml.iu.edu/hypermail/linux/kernel/2607.3/08806.html Signed-off-by: Birk Skyum --- Documentation/hwmon/index.rst | 1 + Documentation/hwmon/qcom-ec.rst | 40 +++++++ MAINTAINERS | 1 + drivers/platform/arm64/Kconfig | 4 +- drivers/platform/arm64/qcom-hamoa-ec.c | 153 ++++++++++++++++++++++++- 5 files changed, 194 insertions(+), 5 deletions(-) create mode 100644 Documentation/hwmon/qcom-ec.rst diff --git a/Documentation/hwmon/index.rst b/Documentation/hwmon/index.rst index 9955a5254..7ce23ea5a 100644 --- a/Documentation/hwmon/index.rst +++ b/Documentation/hwmon/index.rst @@ -233,6 +233,7 @@ Hardware Monitoring Kernel Drivers pxe1610 pwm-fan q54sj108a2 + qcom-ec qnap-mcu-hwmon raspberrypi-hwmon sbrmi diff --git a/Documentation/hwmon/qcom-ec.rst b/Documentation/hwmon/qcom-ec.= rst new file mode 100644 index 000000000..11f9f2347 --- /dev/null +++ b/Documentation/hwmon/qcom-ec.rst @@ -0,0 +1,40 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +Kernel driver qcom-hamoa-ec +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D + +Supported systems: + + * Lenovo Yoga Slim 7x (14Q8X9) + + Prefix: 'qcom_ec' + + I2C address: 0x76, instantiated from the device tree. No scanning. + +Description +----------- + +The Yoga Slim 7x embedded controller provides a fan speed channel and a +thermistor through a legacy interface. It does not provide the thermal +capability response used by the Qualcomm reference-board interface. + +On this system the driver only reads sensors. Cooling remains under firmwa= re +control, including when the driver is unloaded. The driver does not change +fan curves, fan profiles, PWM settings, SCI events or EC standby state. +The exposed RPM channel does not establish the number of physical fans. + +The hwmon interface is selected by the ``lenovo,yoga-slim7x-ec`` compatibl= e. +It is not exposed on Qualcomm reference boards by this driver. + +Sysfs attributes +---------------- + +All attributes below are read-only. + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +fan1_input Fan speed in RPM. Zero is valid when the fan is stopped. +temp1_input EC thermistor temperature in millidegrees Celsius. +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +An unavailable thermistor reading returns ``ENODATA``. A malformed fan +response or an incomplete bus transfer returns an error, not a zero speed. diff --git a/MAINTAINERS b/MAINTAINERS index 6215fcb07..8518a97b4 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22441,6 +22441,7 @@ M: Sibi Sankar L: linux-arm-msm@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/embedded-controller/qcom,hamoa-crd-ec= .yaml +F: Documentation/hwmon/qcom-ec.rst F: drivers/platform/arm64/qcom-hamoa-ec.c =20 QUALCOMM HEXAGON ARCHITECTURE diff --git a/drivers/platform/arm64/Kconfig b/drivers/platform/arm64/Kconfig index e32e01b2a..4f338c715 100644 --- a/drivers/platform/arm64/Kconfig +++ b/drivers/platform/arm64/Kconfig @@ -94,6 +94,7 @@ config EC_QCOM_HAMOA tristate "Embedded Controller driver for Qualcomm Hamoa/Glymur reference = devices" depends on ARCH_QCOM || COMPILE_TEST depends on I2C + depends on HWMON depends on THERMAL || THERMAL=3Dn help Say M or Y here to enable the Embedded Controller driver for Qualcomm @@ -101,6 +102,7 @@ config EC_QCOM_HAMOA control, temperature sensors, access to EC state changes and supports reporting suspend entry/exit to the EC. =20 - This driver currently supports Hamoa/Purwa/Glymur reference devices. + This driver supports Hamoa/Purwa/Glymur reference devices and read-only + fan speed and temperature monitoring on the Lenovo Yoga Slim 7x. =20 endif # ARM64_PLATFORM_DEVICES diff --git a/drivers/platform/arm64/qcom-hamoa-ec.c b/drivers/platform/arm6= 4/qcom-hamoa-ec.c index 4d2ad042a..3a4ca8a0d 100644 --- a/drivers/platform/arm64/qcom-hamoa-ec.c +++ b/drivers/platform/arm64/qcom-hamoa-ec.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -16,10 +17,13 @@ #include #include #include +#include =20 #define EC_SCI_EVT_READ_CMD 0x05 #define EC_FW_VERSION_CMD 0x0e +#define EC_FAN_RPM_QUERY_CMD 0x22 #define EC_MODERN_STANDBY_CMD 0x23 +#define EC_THERMISTOR1_CMD 0x29 #define EC_FAN_DBG_CONTROL_CMD 0x30 #define EC_SCI_EVT_CONTROL_CMD 0x35 #define EC_THERMAL_CAP_CMD 0x42 @@ -76,13 +80,126 @@ struct qcom_ec_cooling_dev { u8 state; }; =20 +struct qcom_ec_variant { + bool monitor_only; +}; + struct qcom_ec { + const struct qcom_ec_variant *variant; struct qcom_ec_cooling_dev *ec_cdev; struct qcom_ec_thermal_cap thermal_cap; struct qcom_ec_version version; struct i2c_client *client; }; =20 +/* FC22 in the Slim 7x DSDT: command, fan ID; count, little-endian RPM. */ +static int qcom_ec_read_fan_rpm(struct qcom_ec *ec, long *val) +{ + struct i2c_client *client =3D ec->client; + u8 request[] =3D { EC_FAN_RPM_QUERY_CMD, 1 }; + u8 response[3]; + struct i2c_msg messages[] =3D { + { + .addr =3D client->addr, + .len =3D sizeof(request), + .buf =3D request, + }, { + .addr =3D client->addr, + .flags =3D I2C_M_RD, + .len =3D sizeof(response), + .buf =3D response, + }, + }; + int ret; + + ret =3D i2c_transfer(client->adapter, messages, ARRAY_SIZE(messages)); + if (ret < 0) + return ret; + if (ret !=3D ARRAY_SIZE(messages)) + return -EIO; + if (response[0] !=3D sizeof(response) - 1) + return -EPROTO; + + *val =3D get_unaligned_le16(&response[1]); + + return 0; +} + +static umode_t qcom_ec_hwmon_is_visible(const void *data, enum hwmon_senso= r_types type, + u32 attr, int channel) +{ + if (channel) + return 0; + if ((type =3D=3D hwmon_fan && attr =3D=3D hwmon_fan_input) || + (type =3D=3D hwmon_temp && attr =3D=3D hwmon_temp_input)) + return 0444; + + return 0; +} + +static int qcom_ec_hwmon_read(struct device *dev, enum hwmon_sensor_types = type, + u32 attr, int channel, long *val) +{ + struct qcom_ec *ec =3D dev_get_drvdata(dev); + int ret; + + if (channel) + return -EOPNOTSUPP; + + if (type =3D=3D hwmon_fan && attr =3D=3D hwmon_fan_input) + return qcom_ec_read_fan_rpm(ec, val); + + if (type !=3D hwmon_temp || attr !=3D hwmon_temp_input) + return -EOPNOTSUPP; + + /* FC29/TZ39 report degrees Celsius, or 0xff if unavailable. */ + ret =3D i2c_smbus_read_byte_data(ec->client, EC_THERMISTOR1_CMD); + if (ret < 0) + return ret; + if (ret =3D=3D 0xff) + return -ENODATA; + + *val =3D ret * 1000; + + return 0; +} + +static const struct hwmon_ops qcom_ec_hwmon_ops =3D { + .is_visible =3D qcom_ec_hwmon_is_visible, + .read =3D qcom_ec_hwmon_read, +}; + +static const struct hwmon_channel_info * const qcom_ec_hwmon_info[] =3D { + HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT), + HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT), + NULL, +}; + +static const struct hwmon_chip_info qcom_ec_hwmon_chip_info =3D { + .ops =3D &qcom_ec_hwmon_ops, + .info =3D qcom_ec_hwmon_info, +}; + +static int qcom_ec_hwmon_probe(struct qcom_ec *ec) +{ + struct device *dev =3D &ec->client->dev; + struct device *hwmon; + long rpm; + int ret; + + if (!i2c_check_functionality(ec->client->adapter, + I2C_FUNC_I2C | I2C_FUNC_SMBUS_READ_BYTE_DATA)) + return -EOPNOTSUPP; + + ret =3D qcom_ec_read_fan_rpm(ec, &rpm); + if (ret) + return dev_err_probe(dev, ret, "Failed to read fan RPM\n"); + + hwmon =3D devm_hwmon_device_register_with_info(dev, "qcom_ec", ec, + &qcom_ec_hwmon_chip_info, NULL); + return PTR_ERR_OR_ZERO(hwmon); +} + static int qcom_ec_read(struct qcom_ec *ec, u8 cmd, u8 resp_len, u8 *resp) { int ret; @@ -331,6 +448,10 @@ static const struct thermal_cooling_device_ops qcom_ec= _thermal_ops =3D { static int qcom_ec_resume(struct device *dev) { struct i2c_client *client =3D to_i2c_client(dev); + struct qcom_ec *ec =3D i2c_get_clientdata(client); + + if (ec->variant->monitor_only) + return 0; =20 return i2c_smbus_write_byte_data(client, EC_MODERN_STANDBY_CMD, EC_MODERN_STANDBY_EXIT); @@ -339,6 +460,10 @@ static int qcom_ec_resume(struct device *dev) static int qcom_ec_suspend(struct device *dev) { struct i2c_client *client =3D to_i2c_client(dev); + struct qcom_ec *ec =3D i2c_get_clientdata(client); + + if (ec->variant->monitor_only) + return 0; =20 return i2c_smbus_write_byte_data(client, EC_MODERN_STANDBY_CMD, EC_MODERN_STANDBY_ENTER); @@ -356,14 +481,24 @@ static int qcom_ec_probe(struct i2c_client *client) return -ENOMEM; =20 ec->client =3D client; + ec->variant =3D i2c_get_match_data(client); + if (!ec->variant) + return -ENODEV; + i2c_set_clientdata(client, ec); + + /* + * The Slim 7x does not implement the reference-board capability command. + * Its firmware controls cooling autonomously. Only expose its legacy + * sensors; do not enable SCI, fan debug control or standby notifications. + */ + if (ec->variant->monitor_only) + return qcom_ec_hwmon_probe(ec); =20 ret =3D devm_request_threaded_irq(dev, client->irq, NULL, qcom_ec_irq, IRQF_ONESHOT, "qcom_ec", ec); if (ret < 0) return ret; =20 - i2c_set_clientdata(client, ec); - ret =3D qcom_ec_read_fw_version(dev); if (ret < 0) return dev_err_probe(dev, ret, "Failed to read EC firmware version\n"); @@ -410,6 +545,9 @@ static void qcom_ec_remove(struct i2c_client *client) struct device *dev =3D &client->dev; int ret; =20 + if (ec->variant->monitor_only) + return; + ret =3D qcom_ec_sci_evt_control(dev, false); if (ret < 0) dev_err(dev, "Failed to disable SCI events: %d\n", ret); @@ -421,14 +559,21 @@ static void qcom_ec_remove(struct i2c_client *client) } } =20 +static const struct qcom_ec_variant qcom_ec_reference =3D {}; + +static const struct qcom_ec_variant qcom_ec_slim7x =3D { + .monitor_only =3D true, +}; + static const struct of_device_id qcom_ec_of_match[] =3D { - { .compatible =3D "qcom,hamoa-crd-ec" }, + { .compatible =3D "lenovo,yoga-slim7x-ec", .data =3D &qcom_ec_slim7x }, + { .compatible =3D "qcom,hamoa-crd-ec", .data =3D &qcom_ec_reference }, {} }; MODULE_DEVICE_TABLE(of, qcom_ec_of_match); =20 static const struct i2c_device_id qcom_ec_i2c_id_table[] =3D { - { "qcom-hamoa-ec", }, + { "qcom-hamoa-ec", (kernel_ulong_t)&qcom_ec_reference }, {} }; MODULE_DEVICE_TABLE(i2c, qcom_ec_i2c_id_table); --=20 2.53.0 From nobody Fri Sep 25 22:19:31 2026 Received: from mail-106121.protonmail.ch (mail-106121.protonmail.ch [79.135.106.121]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1A05329E7E; Tue, 8 Sep 2026 01:57:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.135.106.121 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788832639; cv=none; b=VodDEzJHROMnoAvd53yAm2puJc9f2HyLUSRibyvbavTu7cHd5BdjWkcukVnRA1FhcYl27BfCK1e3B7lXrM8677gPL71ZU+//GhbuZvk1VKW5z4tdqCJfjmmr0NmRPyY+P+poaEngSn50rjzb/QYFLGlgVifdyj7ciEw8N5IEd+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788832639; c=relaxed/simple; bh=I90uLmxBcumxygR08+v6n0cjwhkEbdY+f+9Tedk0hHA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=I6G3VS5lPC4VEzEEnijEAU/tuK6vj1SGfvJBqU7mNZwayk+ufPE4JmhSjJiaWkRjaNJRa3hGkJ7Rljexkas2N+M10jdzn522PFksWq0HcpL9H6eeW1i9cLCP9pd8l5Tf9t9hvDzbtybTccJmVvWJr2GBdCUzrQyzNAJyEG93aB4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=X7LZJuJz; arc=none smtp.client-ip=79.135.106.121 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="X7LZJuJz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1788832634; x=1789091834; bh=DfNVEzVrMl35YHsNx82vR8LdSSqAsyGmW5TEJdGFRC0=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=X7LZJuJzkVN3i6OnXsBYZcaci+pbfbufCChAyWj2/oleMqh0PW3+FvQtDHWnBKsjh HzEZHnBwUEvYY57Bu/+XlB1+/439zRP0moAr2GIE9Zy4pg4EXlMdeAWKTC/HPJmTuq 1p++jAduqeHFJrsOE9yWE4dBwlOA4Rawj0nheM+ew2MtxwWtq8FAloUqxrWmt6rcHd qZXtOK89ycWIfBl0yLfDwOZ79iV2kk2GQN42ClPLrnKrBpCvefoPMFRO0Lbl0gzxuQ jmsVZ/ylSKWHHgkmlLHxbU1aJmbRh2R0bdXwiwXj9dBNhX7bOe0o6PObhDTch9BR9B 8J6jb4quRQSTQ== Date: Tue, 08 Sep 2026 01:57:11 +0000 To: Anvesh Jain P , Sibi Sankar , Hans de Goede , =?utf-8?Q?Ilpo_J=C3=A4rvinen?= From: Birk Skyum Cc: Bryan O'Donoghue , Guenter Roeck , Jonathan Corbet , Shuah Khan , Randy Dunlap , Daniel J Blueman , Maya Matuszczyk , linux-arm-msm@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/2] platform: arm64: qcom-hamoa-ec: test sensor and variant handling Message-ID: <20260908015650.84401-3-birk.skyum@pm.me> In-Reply-To: <20260908015650.84401-1-birk.skyum@pm.me> References: <20260908015650.84401-1-birk.skyum@pm.me> Feedback-ID: 21326998:user:proton X-Pm-Message-ID: 8090e75d779578d1058d8d9ab132b75e3c05e7e6 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Exercise the sensor callbacks against a simulated I2C adapter. Check little-endian RPM decoding, short transfers, malformed byte counts, the unavailable-temperature sentinel and propagation of bus errors. Check that the Lenovo compatible takes precedence over its reference- board fallback, that its sensor attributes are read-only, and that its probe and PM/remove callbacks do not issue control writes. Keep coverage of the strict reference-board capability-response validation. The tests use KUnit and never access a physical EC. Signed-off-by: Birk Skyum --- MAINTAINERS | 1 + drivers/platform/arm64/Kconfig | 12 + drivers/platform/arm64/qcom-hamoa-ec-test.c | 294 ++++++++++++++++++++ drivers/platform/arm64/qcom-hamoa-ec.c | 4 + 4 files changed, 311 insertions(+) create mode 100644 drivers/platform/arm64/qcom-hamoa-ec-test.c diff --git a/MAINTAINERS b/MAINTAINERS index 8518a97b4..58010a3e4 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22442,6 +22442,7 @@ L: linux-arm-msm@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/embedded-controller/qcom,hamoa-crd-ec= .yaml F: Documentation/hwmon/qcom-ec.rst +F: drivers/platform/arm64/qcom-hamoa-ec-test.c F: drivers/platform/arm64/qcom-hamoa-ec.c =20 QUALCOMM HEXAGON ARCHITECTURE diff --git a/drivers/platform/arm64/Kconfig b/drivers/platform/arm64/Kconfig index 4f338c715..d940973be 100644 --- a/drivers/platform/arm64/Kconfig +++ b/drivers/platform/arm64/Kconfig @@ -105,4 +105,16 @@ config EC_QCOM_HAMOA This driver supports Hamoa/Purwa/Glymur reference devices and read-only fan speed and temperature monitoring on the Lenovo Yoga Slim 7x. =20 +config EC_QCOM_HAMOA_KUNIT_TEST + bool "Tests for the Qualcomm EC driver" if !KUNIT_ALL_TESTS + depends on EC_QCOM_HAMOA && KUNIT && OF + depends on EC_QCOM_HAMOA=3Dm || KUNIT=3Dy + default KUNIT_ALL_TESTS + help + Test the Qualcomm EC driver's sensor transactions and read-only + callbacks using a simulated I2C adapter. No physical EC is accessed. + Run these tests in a disposable test kernel, for example under QEMU. + + If unsure, say N. + endif # ARM64_PLATFORM_DEVICES diff --git a/drivers/platform/arm64/qcom-hamoa-ec-test.c b/drivers/platform= /arm64/qcom-hamoa-ec-test.c new file mode 100644 index 000000000..68868ef76 --- /dev/null +++ b/drivers/platform/arm64/qcom-hamoa-ec-test.c @@ -0,0 +1,294 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Included by qcom-hamoa-ec.c to test its private transport and callbacks= . */ + +#include +#include +#include + +struct qcom_ec_test_context { + struct i2c_adapter adapter; + struct qcom_ec ec; + u32 functionality; + int transfer_result; + int byte_result; + u8 reply[4]; + u8 block_length; + unsigned int transactions; + unsigned int writes; +}; + +static int qcom_ec_test_transfer(struct i2c_adapter *adapter, + struct i2c_msg *messages, int count) +{ + struct qcom_ec_test_context *ctx =3D i2c_get_adapdata(adapter); + + ctx->transactions++; + /* Only the RPM query's combined write/read transaction is allowed. */ + if (count !=3D 2 || messages[0].addr !=3D 0x76 || messages[1].addr !=3D 0= x76 || + messages[0].flags || messages[1].flags !=3D I2C_M_RD || + messages[0].len !=3D 2 || messages[1].len !=3D 3 || + messages[0].buf[0] !=3D 0x22 || messages[0].buf[1] !=3D 1) { + ctx->writes++; + return -EPROTO; + } + + if (ctx->transfer_result =3D=3D 2) + memcpy(messages[1].buf, ctx->reply, messages[1].len); + + return ctx->transfer_result; +} + +static s32 qcom_ec_test_smbus(struct i2c_adapter *adapter, u16 address, + unsigned short flags, char read_write, u8 command, + int size, union i2c_smbus_data *data) +{ + struct qcom_ec_test_context *ctx =3D i2c_get_adapdata(adapter); + + ctx->transactions++; + if (read_write !=3D I2C_SMBUS_READ) { + ctx->writes++; + return -EPROTO; + } + if (address !=3D 0x76 || flags) + return -EPROTO; + + if (command =3D=3D 0x29 && size =3D=3D I2C_SMBUS_BYTE_DATA) { + if (ctx->byte_result < 0) + return ctx->byte_result; + data->byte =3D ctx->byte_result; + return 0; + } + + if ((command =3D=3D 0x0e || command =3D=3D 0x42) && size =3D=3D I2C_SMBUS= _I2C_BLOCK_DATA) { + data->block[0] =3D ctx->block_length; + memcpy(&data->block[1], ctx->reply, ctx->block_length); + return 0; + } + + return -EPROTO; +} + +static u32 qcom_ec_test_functionality(struct i2c_adapter *adapter) +{ + struct qcom_ec_test_context *ctx =3D i2c_get_adapdata(adapter); + + return ctx->functionality; +} + +static const struct i2c_algorithm qcom_ec_test_algorithm =3D { + .master_xfer =3D qcom_ec_test_transfer, + .smbus_xfer =3D qcom_ec_test_smbus, + .functionality =3D qcom_ec_test_functionality, +}; + +static void qcom_ec_test_delete_adapter(void *data) +{ + i2c_del_adapter(data); +} + +static void qcom_ec_test_unregister_client(void *data) +{ + i2c_unregister_device(data); +} + +static int qcom_ec_test_init(struct kunit *test) +{ + struct qcom_ec_test_context *ctx; + int ret; + + ctx =3D kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctx); + test->priv =3D ctx; + ctx->functionality =3D I2C_FUNC_I2C | I2C_FUNC_SMBUS_READ_BYTE_DATA | + I2C_FUNC_SMBUS_READ_I2C_BLOCK; + ctx->transfer_result =3D 2; + ctx->reply[0] =3D 2; + ctx->byte_result =3D 36; + ctx->adapter.owner =3D THIS_MODULE; + ctx->adapter.algo =3D &qcom_ec_test_algorithm; + strscpy(ctx->adapter.name, "qcom-ec-kunit", sizeof(ctx->adapter.name)); + i2c_set_adapdata(&ctx->adapter, ctx); + ret =3D i2c_add_adapter(&ctx->adapter); + KUNIT_ASSERT_EQ(test, ret, 0); + ret =3D kunit_add_action_or_reset(test, qcom_ec_test_delete_adapter, &ctx= ->adapter); + KUNIT_ASSERT_EQ(test, ret, 0); + + ctx->ec.client =3D i2c_new_dummy_device(&ctx->adapter, 0x76); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->ec.client); + ret =3D kunit_add_action_or_reset(test, qcom_ec_test_unregister_client, c= tx->ec.client); + KUNIT_ASSERT_EQ(test, ret, 0); + ctx->ec.variant =3D &qcom_ec_slim7x; + i2c_set_clientdata(ctx->ec.client, &ctx->ec); + + return 0; +} + +static void qcom_ec_rpm_test(struct kunit *test) +{ + struct qcom_ec_test_context *ctx =3D test->priv; + static const struct { + int transfer_result; + u8 count; + int error; + } failures[] =3D { + { 0, 2, -EIO }, + { 1, 2, -EIO }, + { -EREMOTEIO, 2, -EREMOTEIO }, + { -ETIMEDOUT, 2, -ETIMEDOUT }, + { 2, 0, -EPROTO }, + { 2, 1, -EPROTO }, + { 2, 3, -EPROTO }, + { 2, 0xff, -EPROTO }, + }; + long value =3D -1; + int i; + + KUNIT_EXPECT_EQ(test, qcom_ec_read_fan_rpm(&ctx->ec, &value), 0); + KUNIT_EXPECT_EQ(test, value, 0); + ctx->reply[1] =3D 0xef; + ctx->reply[2] =3D 0xbe; + KUNIT_EXPECT_EQ(test, qcom_ec_read_fan_rpm(&ctx->ec, &value), 0); + KUNIT_EXPECT_EQ(test, value, 0xbeef); + + for (i =3D 0; i < ARRAY_SIZE(failures); i++) { + value =3D -1; + ctx->transfer_result =3D failures[i].transfer_result; + ctx->reply[0] =3D failures[i].count; + KUNIT_EXPECT_EQ_MSG(test, qcom_ec_read_fan_rpm(&ctx->ec, &value), + failures[i].error, "case %d", i); + KUNIT_EXPECT_EQ(test, value, -1); + } + KUNIT_EXPECT_EQ(test, ctx->writes, 0); +} + +static void qcom_ec_temperature_test(struct kunit *test) +{ + struct qcom_ec_test_context *ctx =3D test->priv; + static const int temperatures[] =3D { 0, 36, 254, 255, -EREMOTEIO }; + long value; + int i, ret; + + for (i =3D 0; i < ARRAY_SIZE(temperatures); i++) { + value =3D -1; + ctx->byte_result =3D temperatures[i]; + ret =3D qcom_ec_hwmon_read(&ctx->ec.client->dev, hwmon_temp, + hwmon_temp_input, 0, &value); + if (temperatures[i] < 0) { + KUNIT_EXPECT_EQ(test, ret, temperatures[i]); + KUNIT_EXPECT_EQ(test, value, -1); + } else if (temperatures[i] =3D=3D 255) { + KUNIT_EXPECT_EQ(test, ret, -ENODATA); + KUNIT_EXPECT_EQ(test, value, -1); + } else { + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_EQ(test, value, temperatures[i] * 1000); + } + } + KUNIT_EXPECT_EQ(test, ctx->writes, 0); +} + +static void qcom_ec_read_only_test(struct kunit *test) +{ + struct qcom_ec_test_context *ctx =3D test->priv; + struct device *dev =3D &ctx->ec.client->dev; + long value =3D -1; + + KUNIT_EXPECT_EQ(test, qcom_ec_hwmon_is_visible(NULL, hwmon_fan, hwmon_fan= _input, 0), + 0444); + KUNIT_EXPECT_EQ(test, qcom_ec_hwmon_is_visible(NULL, hwmon_temp, hwmon_te= mp_input, 0), + 0444); + KUNIT_EXPECT_EQ(test, qcom_ec_hwmon_is_visible(NULL, hwmon_fan, hwmon_fan= _input, 1), 0); + KUNIT_EXPECT_EQ(test, qcom_ec_hwmon_is_visible(NULL, hwmon_pwm, hwmon_pwm= _input, 0), 0); + KUNIT_EXPECT_EQ(test, qcom_ec_hwmon_read(dev, hwmon_fan, hwmon_fan_input,= 1, &value), + -EOPNOTSUPP); + KUNIT_EXPECT_EQ(test, qcom_ec_hwmon_read(dev, hwmon_pwm, hwmon_pwm_input,= 0, &value), + -EOPNOTSUPP); + KUNIT_EXPECT_EQ(test, value, -1); + KUNIT_EXPECT_EQ(test, qcom_ec_suspend(dev), 0); + KUNIT_EXPECT_EQ(test, qcom_ec_resume(dev), 0); + qcom_ec_remove(ctx->ec.client); + KUNIT_EXPECT_EQ(test, ctx->transactions, 0); +} + +static void qcom_ec_probe_transport_test(struct kunit *test) +{ + struct qcom_ec_test_context *ctx =3D test->priv; + + ctx->functionality =3D I2C_FUNC_I2C; + KUNIT_EXPECT_EQ(test, qcom_ec_hwmon_probe(&ctx->ec), -EOPNOTSUPP); + ctx->functionality =3D I2C_FUNC_SMBUS_READ_BYTE_DATA; + KUNIT_EXPECT_EQ(test, qcom_ec_hwmon_probe(&ctx->ec), -EOPNOTSUPP); + KUNIT_EXPECT_EQ(test, ctx->transactions, 0); +} + +static void qcom_ec_probe_sensor_test(struct kunit *test) +{ + struct qcom_ec_test_context *ctx =3D test->priv; + + KUNIT_EXPECT_EQ(test, qcom_ec_hwmon_probe(&ctx->ec), 0); + KUNIT_EXPECT_EQ(test, ctx->transactions, 1); + KUNIT_EXPECT_EQ(test, ctx->writes, 0); + ctx->reply[0] =3D 0; + KUNIT_EXPECT_EQ(test, qcom_ec_hwmon_probe(&ctx->ec), -EPROTO); + KUNIT_EXPECT_EQ(test, ctx->writes, 0); +} + +static void qcom_ec_reference_validation_test(struct kunit *test) +{ + struct qcom_ec_test_context *ctx =3D test->priv; + u8 response[3]; + + ctx->block_length =3D 3; + KUNIT_EXPECT_EQ(test, qcom_ec_read(&ctx->ec, EC_THERMAL_CAP_CMD, 3, respo= nse), 0); + ctx->block_length =3D 2; + KUNIT_EXPECT_EQ(test, qcom_ec_read(&ctx->ec, EC_THERMAL_CAP_CMD, 3, respo= nse), -EIO); + ctx->block_length =3D 0; + KUNIT_EXPECT_EQ(test, qcom_ec_read(&ctx->ec, EC_THERMAL_CAP_CMD, 3, respo= nse), + -EOPNOTSUPP); + ctx->block_length =3D 3; + ctx->reply[0] =3D 0; + KUNIT_EXPECT_EQ(test, qcom_ec_read(&ctx->ec, EC_THERMAL_CAP_CMD, 3, respo= nse), -EINVAL); + ctx->reply[0] =3D 1; + KUNIT_EXPECT_EQ(test, qcom_ec_read(&ctx->ec, EC_THERMAL_CAP_CMD, 3, respo= nse), -EINVAL); + KUNIT_EXPECT_EQ(test, ctx->writes, 0); +} + +static void qcom_ec_variant_test(struct kunit *test) +{ + static const char compatible[] =3D "lenovo,yoga-slim7x-ec\0qcom,hamoa-crd= -ec"; + struct property property =3D { + .name =3D "compatible", + .length =3D sizeof(compatible), + .value =3D (void *)compatible, + }; + struct device_node node =3D { .properties =3D &property }; + const struct of_device_id *match; + + match =3D of_match_node(qcom_ec_of_match, &node); + KUNIT_ASSERT_NOT_NULL(test, match); + KUNIT_EXPECT_PTR_EQ(test, match->data, &qcom_ec_slim7x); + property.value =3D "qcom,hamoa-crd-ec"; + property.length =3D sizeof("qcom,hamoa-crd-ec"); + match =3D of_match_node(qcom_ec_of_match, &node); + KUNIT_ASSERT_NOT_NULL(test, match); + KUNIT_EXPECT_PTR_EQ(test, match->data, &qcom_ec_reference); +} + +static struct kunit_case qcom_ec_test_cases[] =3D { + KUNIT_CASE(qcom_ec_rpm_test), + KUNIT_CASE(qcom_ec_temperature_test), + KUNIT_CASE(qcom_ec_read_only_test), + KUNIT_CASE(qcom_ec_probe_transport_test), + KUNIT_CASE(qcom_ec_probe_sensor_test), + KUNIT_CASE(qcom_ec_reference_validation_test), + KUNIT_CASE(qcom_ec_variant_test), + {} +}; + +static struct kunit_suite qcom_ec_test_suite =3D { + .name =3D "qcom-hamoa-ec", + .init =3D qcom_ec_test_init, + .test_cases =3D qcom_ec_test_cases, +}; + +kunit_test_suite(qcom_ec_test_suite); diff --git a/drivers/platform/arm64/qcom-hamoa-ec.c b/drivers/platform/arm6= 4/qcom-hamoa-ec.c index 3a4ca8a0d..043f6ab7e 100644 --- a/drivers/platform/arm64/qcom-hamoa-ec.c +++ b/drivers/platform/arm64/qcom-hamoa-ec.c @@ -596,3 +596,7 @@ module_i2c_driver(qcom_ec_i2c_driver); =20 MODULE_DESCRIPTION("QCOM Hamoa Embedded Controller"); MODULE_LICENSE("GPL"); + +#if IS_ENABLED(CONFIG_EC_QCOM_HAMOA_KUNIT_TEST) +#include "qcom-hamoa-ec-test.c" +#endif --=20 2.53.0