From nobody Wed Dec 17 08:57:52 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 911F623C514; Thu, 19 Jun 2025 07:42:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750318977; cv=none; b=seZQyxsdN75pPWJT5QBtTRrqKrKodGUUnNF1reJOyFMRelrxQ1bQCVG9WlrHC1m0Jaf7/AvriywTNQzD+KfTwLgWKDIowGtrN8kBzS/3I53jBJ5VUSMXV+/G/OJe/aAGsXuB/HsBV9PA2P1G7yIGVCN1kDfRwye0x0xKu2ggdmQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750318977; c=relaxed/simple; bh=b9H0FqFFqqf2pAqLpYWLcjBdkrZBk0Ky1iKnCLvRI9I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZBbIn0oWDFGt/XzFiXPvzb1XR8OuvbSA91fDd/j/8SU6X7KcJoI7bzKgvhwR3zf0HiksukPnw7HvZ0VR3aQ3ph2aBO1Y5voHuABZAG2qTMY+n0ufb9QHTzVOAucct+MyoGwyqbksBMQNxRBQhFT/BkcESKZhDrpCZcL/Ht6KYtA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rqmH35+j; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rqmH35+j" Received: by smtp.kernel.org (Postfix) with ESMTPS id 3FC15C4CEEA; Thu, 19 Jun 2025 07:42:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750318977; bh=b9H0FqFFqqf2pAqLpYWLcjBdkrZBk0Ky1iKnCLvRI9I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=rqmH35+jtZenmjM56osIg0bIvaSaUcCnVKTIJBfFU6MgwZYpoD0wRGj2NHr9ht87s TI5C/jehznwWdHNPHk2KvMDGjO3zYEcg6ZOpEs6mc6GfSwJWZ8Krvov0iPpLU2gaUA UDNT/BBU4YB+yMEOi09scfTJNdyhy/RYidUc857a0Mki7VsHJrCGIyURM//hOvki04 9BDi8h1WczRfnC+1MXfNImpou+JQC6zCpunJBiKwXBLoOJvCozPTzrx+z7psyYv3WM /R8ajZKz0NRtwtk3sf0jjmYi3lkJ0m3pwiR19E2WwWWlMzIlosBZspP57h694zPtj5 LdazrcxFhnpvw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29725C7115B; Thu, 19 Jun 2025 07:42:57 +0000 (UTC) From: Sung-Chi Li via B4 Relay Date: Thu, 19 Jun 2025 15:42:54 +0800 Subject: [PATCH v4 1/3] platform/chrome: update pwm fan control host commands 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: quoted-printable Message-Id: <20250619-cros_ec_fan-v4-1-ca11548ce449@chromium.org> References: <20250619-cros_ec_fan-v4-0-ca11548ce449@chromium.org> In-Reply-To: <20250619-cros_ec_fan-v4-0-ca11548ce449@chromium.org> To: Benson Leung , Guenter Roeck , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Jean Delvare , Guenter Roeck , Jonathan Corbet , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Cc: chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org, Sung-Chi Li , Sung-Chi Li X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1750318976; l=2194; i=lschyi@chromium.org; s=20250429; h=from:subject:message-id; bh=eTkgZyPaJnNGt3GF+wBV3FuUrCXpaB4J4qpBRGHJ4Jc=; b=KJ/LKjnHbQ8IDrDi4tvGf1Yc9/hvC7gnMFmvQOY1cl9sJihv3PvIhF1Fud7tsQjiMQOtYhIih TFk2S4jVwLNA/Z8w7sjVHXrQAgAReNpR9HjZcQexsQIvOtpCXTdHfZ6 X-Developer-Key: i=lschyi@chromium.org; a=ed25519; pk=9gCZPRJmYyHDt6VN9FV2UreFcUr73JFrwYvmsltW9Y8= X-Endpoint-Received: by B4 Relay for lschyi@chromium.org/20250429 with auth_id=392 X-Original-From: Sung-Chi Li Reply-To: lschyi@chromium.org From: Sung-Chi Li Update cros_ec_commands.h to include definitions for getting PWM fan duty, getting and setting the fan control mode. Signed-off-by: Sung-Chi Li Reviewed-by: Thomas Wei=C3=9Fschuh --- include/linux/platform_data/cros_ec_commands.h | 29 ++++++++++++++++++++++= +++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/include/linux/platform_data/cros_ec_commands.h b/include/linux= /platform_data/cros_ec_commands.h index c74edd735a0e3992a06affa62d63a0882bb75967..e41e04d01e93d33704af76a7eac= 95d53bdb1fd54 100644 --- a/include/linux/platform_data/cros_ec_commands.h +++ b/include/linux/platform_data/cros_ec_commands.h @@ -1825,6 +1825,16 @@ struct ec_response_pwm_get_duty { uint16_t duty; /* Duty cycle, EC_PWM_MAX_DUTY =3D 100% */ } __ec_align2; =20 +#define EC_CMD_PWM_GET_FAN_DUTY 0x0027 + +struct ec_params_pwm_get_fan_duty { + uint8_t fan_idx; +} __ec_align1; + +struct ec_response_pwm_get_fan_duty { + uint32_t percent; /* Percentage of duty cycle, ranging from 0 ~ 100 */ +} __ec_align4; + /*************************************************************************= ****/ /* * Lightbar commands. This looks worse than it is. Since we only use one H= OST @@ -3137,14 +3147,31 @@ struct ec_params_thermal_set_threshold_v1 { =20 /*************************************************************************= ***/ =20 -/* Toggle automatic fan control */ +/* Set or get fan control mode */ #define EC_CMD_THERMAL_AUTO_FAN_CTRL 0x0052 =20 +enum ec_auto_fan_ctrl_cmd { + EC_AUTO_FAN_CONTROL_CMD_SET =3D 0, + EC_AUTO_FAN_CONTROL_CMD_GET, +}; + /* Version 1 of input params */ struct ec_params_auto_fan_ctrl_v1 { uint8_t fan_idx; } __ec_align1; =20 +/* Version 2 of input params */ +struct ec_params_auto_fan_ctrl_v2 { + uint8_t fan_idx; + uint8_t cmd; /* enum ec_auto_fan_ctrl_cmd */ + uint8_t set_auto; /* only used with EC_AUTO_FAN_CONTROL_CMD_SET - bool + */ +} __ec_align4; + +struct ec_response_auto_fan_control { + uint8_t is_auto; /* bool */ +} __ec_align1; + /* Get/Set TMP006 calibration data */ #define EC_CMD_TMP006_GET_CALIBRATION 0x0053 #define EC_CMD_TMP006_SET_CALIBRATION 0x0054 --=20 2.50.0.rc2.701.gf1e915cc24-goog From nobody Wed Dec 17 08:57:52 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 911901624FE; Thu, 19 Jun 2025 07:42:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750318977; cv=none; b=mgvhDtaliRYaELH8bq/PCdzrRDTVMsIrZBoxzmrRkNeV0cF3IN6Vk8ln8JAY5iTHm7R1EV+vELhZWt6tN9WWpn0OffD72iGdFQQKnNb+e7DwcIiIDjn6ScMgqVZY+PgzTx2kPsMmDvyaI6yRcZWbqI0wRsYCMtFqv1oJOGhc7QM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750318977; c=relaxed/simple; bh=ST/nxq/a0One4o0jnedcWbTgIppe6vQ4/8Udu6Zzki8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Wx3yGggaPZxIW4haTp4Sx3Se7e+kyrxzVT4ZM21w0c8eQZFMM98R/esh1FH6xb7TvWZIz4ejNpoL2esoiM6O3S6gb4LGjF61y0ac0qMjhpN45IKasVTK4gco+ZdMfp/RP73jo+o2fSHxnBETNKmDKR17jlJWLUUI/6OH18/cmqA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Cof9uqJQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Cof9uqJQ" Received: by smtp.kernel.org (Postfix) with ESMTPS id 46682C4CEEF; Thu, 19 Jun 2025 07:42:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750318977; bh=ST/nxq/a0One4o0jnedcWbTgIppe6vQ4/8Udu6Zzki8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Cof9uqJQsjyFAKmLViAeLdV6o65z1VxLStlxno4n639SuZI5g7CsavlfLTMTWwz+y p8Gff+5xaFlQYb7hJO7NBSLu9NG9mDkUTcwPrMNVF4txmdYTJ3reQ6HlGsHUrWeHxf nmhkLxAOSyXldbRF7Xp35CP/TGkVY/dbZKg68CdkquZgiGKcmotnK2t/yjlNSNq5J/ RiwiTs3ZgWfuZoe2wbgw4d96ZlgeQs/6aw+SfTmDX7IvygRQ4QvSF3YxQBIxtWUWiQ rVyxdFv+N5cMD5Io2Ey3AENkANegSJkLIhW2kZfHm+WStHjV+96gVh58m5b7TLQLiy ftnkuG+RcRF3g== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3938FC7115E; Thu, 19 Jun 2025 07:42:57 +0000 (UTC) From: Sung-Chi Li via B4 Relay Date: Thu, 19 Jun 2025 15:42:55 +0800 Subject: [PATCH v4 2/3] hwmon: (cros_ec) add PWM control over fans 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: quoted-printable Message-Id: <20250619-cros_ec_fan-v4-2-ca11548ce449@chromium.org> References: <20250619-cros_ec_fan-v4-0-ca11548ce449@chromium.org> In-Reply-To: <20250619-cros_ec_fan-v4-0-ca11548ce449@chromium.org> To: Benson Leung , Guenter Roeck , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Jean Delvare , Guenter Roeck , Jonathan Corbet , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Cc: chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org, Sung-Chi Li , Sung-Chi Li X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1750318976; l=12660; i=lschyi@chromium.org; s=20250429; h=from:subject:message-id; bh=LeW+TVJm1dJGeVqUnhmeKPD6SxaPkqHZyFAHrNFKHJU=; b=ZfQxxdDi85K4P5N0e4ZP65dGr6W2eWB5jpjvfyic1caS+biuD4LQ7vTuwqDMqc1uxpUcyRjm7 pCds16SOrgRBg8IHKomCZgN3iuR5AOXAKfNxgqmFrrXGgD/TaVfv2fm X-Developer-Key: i=lschyi@chromium.org; a=ed25519; pk=9gCZPRJmYyHDt6VN9FV2UreFcUr73JFrwYvmsltW9Y8= X-Endpoint-Received: by B4 Relay for lschyi@chromium.org/20250429 with auth_id=392 X-Original-From: Sung-Chi Li Reply-To: lschyi@chromium.org From: Sung-Chi Li Newer EC firmware supports controlling fans through host commands, so adding corresponding implementations for controlling these fans in the driver for other kernel services and userspace to control them. The driver will first probe the supported host command versions (get and set of fan PWM values, get and set of fan control mode) to see if the connected EC fulfills the requirements of controlling the fan, then exposes corresponding sysfs nodes for userspace to control the fan with corresponding read and write implementations. As EC will automatically change the fan mode to auto when the device is suspended, the power management hooks are added as well to keep the fan control mode and fan PWM value consistent during suspend and resume. As we need to access the hwmon device in the power management hook, update the driver by storing the hwmon device in the driver data as well. Signed-off-by: Sung-Chi Li Acked-by: Thomas Wei=C3=9Fschuh --- Documentation/hwmon/cros_ec_hwmon.rst | 5 +- drivers/hwmon/cros_ec_hwmon.c | 225 ++++++++++++++++++++++++++++++= ++++ 2 files changed, 229 insertions(+), 1 deletion(-) diff --git a/Documentation/hwmon/cros_ec_hwmon.rst b/Documentation/hwmon/cr= os_ec_hwmon.rst index 47ecae983bdbef4bfcafc5dd2fff3de039f77f8e..355557a08c9a54b4c177bafde37= 43e7dc02218be 100644 --- a/Documentation/hwmon/cros_ec_hwmon.rst +++ b/Documentation/hwmon/cros_ec_hwmon.rst @@ -23,4 +23,7 @@ ChromeOS embedded controller used in Chromebooks and othe= r devices. =20 The channel labels exposed via hwmon are retrieved from the EC itself. =20 -Fan and temperature readings are supported. +Fan and temperature readings are supported. PWM fan control is also suppor= ted if +the EC also supports setting fan PWM values and fan mode. Note that EC will +switch fan control mode back to auto when suspended. This driver will rest= ore +the fan state to what they were before suspended when resumed. diff --git a/drivers/hwmon/cros_ec_hwmon.c b/drivers/hwmon/cros_ec_hwmon.c index 5514cf780b8beab272670d6591ed298ddac5afbf..c0c4ffa6f85419edf5dacd73019= 2e7f1c28e2e5c 100644 --- a/drivers/hwmon/cros_ec_hwmon.c +++ b/drivers/hwmon/cros_ec_hwmon.c @@ -7,6 +7,7 @@ =20 #include #include +#include #include #include #include @@ -17,10 +18,17 @@ =20 #define DRV_NAME "cros-ec-hwmon" =20 +#define CROS_EC_HWMON_PWM_GET_FAN_DUTY_CMD_VERSION 0 +#define CROS_EC_HWMON_PWM_SET_FAN_DUTY_CMD_VERSION 1 +#define CROS_EC_HWMON_THERMAL_AUTO_FAN_CTRL_CMD_VERSION 2 + struct cros_ec_hwmon_priv { struct cros_ec_device *cros_ec; const char *temp_sensor_names[EC_TEMP_SENSOR_ENTRIES + EC_TEMP_SENSOR_B_E= NTRIES]; u8 usable_fans; + bool fan_control_supported; + u8 manual_fans; /* bits to indicate whether the fan is set to manual */ + u8 manual_fan_pwm[EC_FAN_SPEED_ENTRIES]; }; =20 static int cros_ec_hwmon_read_fan_speed(struct cros_ec_device *cros_ec, u8= index, u16 *speed) @@ -36,6 +44,42 @@ static int cros_ec_hwmon_read_fan_speed(struct cros_ec_d= evice *cros_ec, u8 index return 0; } =20 +static int cros_ec_hwmon_read_pwm_value(struct cros_ec_device *cros_ec, u8= index, u8 *pwm_value) +{ + struct ec_params_pwm_get_fan_duty req =3D { + .fan_idx =3D index, + }; + struct ec_response_pwm_get_fan_duty resp; + int ret; + + ret =3D cros_ec_cmd(cros_ec, CROS_EC_HWMON_PWM_GET_FAN_DUTY_CMD_VERSION, + EC_CMD_PWM_GET_FAN_DUTY, &req, sizeof(req), &resp, sizeof(resp)); + if (ret < 0) + return ret; + + *pwm_value =3D (u8)DIV_ROUND_CLOSEST(le32_to_cpu(resp.percent) * 255, 100= ); + return 0; +} + +static int cros_ec_hwmon_read_pwm_enable(struct cros_ec_device *cros_ec, u= 8 index, + u8 *control_method) +{ + struct ec_params_auto_fan_ctrl_v2 req =3D { + .cmd =3D EC_AUTO_FAN_CONTROL_CMD_GET, + .fan_idx =3D index, + }; + struct ec_response_auto_fan_control resp; + int ret; + + ret =3D cros_ec_cmd(cros_ec, CROS_EC_HWMON_THERMAL_AUTO_FAN_CTRL_CMD_VERS= ION, + EC_CMD_THERMAL_AUTO_FAN_CTRL, &req, sizeof(req), &resp, sizeof(resp)); + if (ret < 0) + return ret; + + *control_method =3D resp.is_auto ? 2 : 1; + return 0; +} + static int cros_ec_hwmon_read_temp(struct cros_ec_device *cros_ec, u8 inde= x, u8 *temp) { unsigned int offset; @@ -75,6 +119,8 @@ static int cros_ec_hwmon_read(struct device *dev, enum h= wmon_sensor_types type, { struct cros_ec_hwmon_priv *priv =3D dev_get_drvdata(dev); int ret =3D -EOPNOTSUPP; + u8 control_method; + u8 pwm_value; u16 speed; u8 temp; =20 @@ -92,6 +138,16 @@ static int cros_ec_hwmon_read(struct device *dev, enum = hwmon_sensor_types type, if (ret =3D=3D 0) *val =3D cros_ec_hwmon_is_error_fan(speed); } + } else if (type =3D=3D hwmon_pwm) { + if (attr =3D=3D hwmon_pwm_enable) { + ret =3D cros_ec_hwmon_read_pwm_enable(priv->cros_ec, channel, &control_= method); + if (ret =3D=3D 0) + *val =3D control_method; + } else if (attr =3D=3D hwmon_pwm_input) { + ret =3D cros_ec_hwmon_read_pwm_value(priv->cros_ec, channel, &pwm_value= ); + if (ret =3D=3D 0) + *val =3D pwm_value; + } } else if (type =3D=3D hwmon_temp) { if (attr =3D=3D hwmon_temp_input) { ret =3D cros_ec_hwmon_read_temp(priv->cros_ec, channel, &temp); @@ -124,6 +180,74 @@ static int cros_ec_hwmon_read_string(struct device *de= v, enum hwmon_sensor_types return -EOPNOTSUPP; } =20 +static int cros_ec_hwmon_set_fan_pwm_val(struct cros_ec_device *cros_ec, u= 8 index, u8 val) +{ + struct ec_params_pwm_set_fan_duty_v1 req =3D { + .fan_idx =3D index, + .percent =3D DIV_ROUND_CLOSEST((uint32_t)val * 100, 255), + }; + int ret; + + ret =3D cros_ec_cmd(cros_ec, CROS_EC_HWMON_PWM_SET_FAN_DUTY_CMD_VERSION, + EC_CMD_PWM_SET_FAN_DUTY, &req, sizeof(req), NULL, 0); + if (ret < 0) + return ret; + return 0; +} + +static int cros_ec_hwmon_write_pwm_input(struct cros_ec_device *cros_ec, u= 8 index, u8 val) +{ + u8 control_method; + int ret; + + ret =3D cros_ec_hwmon_read_pwm_enable(cros_ec, index, &control_method); + if (ret) + return ret; + if (control_method !=3D 1) + return -EOPNOTSUPP; + + return cros_ec_hwmon_set_fan_pwm_val(cros_ec, index, val); +} + +static int cros_ec_hwmon_write_pwm_enable(struct cros_ec_device *cros_ec, = u8 index, u8 val) +{ + struct ec_params_auto_fan_ctrl_v2 req =3D { + .fan_idx =3D index, + .cmd =3D EC_AUTO_FAN_CONTROL_CMD_SET, + }; + int ret; + + /* No CROS EC supports no fan speed control */ + if (val =3D=3D 0) + return -EOPNOTSUPP; + + req.set_auto =3D (val !=3D 1) ? true : false; + ret =3D cros_ec_cmd(cros_ec, CROS_EC_HWMON_THERMAL_AUTO_FAN_CTRL_CMD_VERS= ION, + EC_CMD_THERMAL_AUTO_FAN_CTRL, &req, sizeof(req), NULL, 0); + if (ret < 0) + return ret; + return 0; +} + +static int cros_ec_hwmon_write(struct device *dev, enum hwmon_sensor_types= type, u32 attr, + int channel, long val) +{ + struct cros_ec_hwmon_priv *priv =3D dev_get_drvdata(dev); + + if (type =3D=3D hwmon_pwm) { + switch (attr) { + case hwmon_pwm_input: + return cros_ec_hwmon_write_pwm_input(priv->cros_ec, channel, val); + case hwmon_pwm_enable: + return cros_ec_hwmon_write_pwm_enable(priv->cros_ec, channel, val); + default: + return -EOPNOTSUPP; + } + } + + return -EOPNOTSUPP; +} + static umode_t cros_ec_hwmon_is_visible(const void *data, enum hwmon_senso= r_types type, u32 attr, int channel) { @@ -132,6 +256,9 @@ static umode_t cros_ec_hwmon_is_visible(const void *dat= a, enum hwmon_sensor_type if (type =3D=3D hwmon_fan) { if (priv->usable_fans & BIT(channel)) return 0444; + } else if (type =3D=3D hwmon_pwm) { + if (priv->fan_control_supported && priv->usable_fans & BIT(channel)) + return 0644; } else if (type =3D=3D hwmon_temp) { if (priv->temp_sensor_names[channel]) return 0444; @@ -146,6 +273,11 @@ static const struct hwmon_channel_info * const cros_ec= _hwmon_info[] =3D { HWMON_F_INPUT | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_FAULT), + HWMON_CHANNEL_INFO(pwm, + HWMON_PWM_INPUT | HWMON_PWM_ENABLE, + HWMON_PWM_INPUT | HWMON_PWM_ENABLE, + HWMON_PWM_INPUT | HWMON_PWM_ENABLE, + HWMON_PWM_INPUT | HWMON_PWM_ENABLE), HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL, HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_LABEL, @@ -177,6 +309,7 @@ static const struct hwmon_channel_info * const cros_ec_= hwmon_info[] =3D { static const struct hwmon_ops cros_ec_hwmon_ops =3D { .read =3D cros_ec_hwmon_read, .read_string =3D cros_ec_hwmon_read_string, + .write =3D cros_ec_hwmon_write, .is_visible =3D cros_ec_hwmon_is_visible, }; =20 @@ -232,6 +365,22 @@ static void cros_ec_hwmon_probe_fans(struct cros_ec_hw= mon_priv *priv) } } =20 +static inline bool is_cros_ec_cmd_available(struct cros_ec_device *cros_ec, + u16 cmd, u8 version) +{ + int ret; + + ret =3D cros_ec_get_cmd_versions(cros_ec, cmd); + return ret >=3D 0 && (ret & EC_VER_MASK(version)); +} + +static bool cros_ec_hwmon_probe_fan_control_supported(struct cros_ec_devic= e *cros_ec) +{ + return is_cros_ec_cmd_available(cros_ec, EC_CMD_PWM_GET_FAN_DUTY, CROS_EC= _HWMON_PWM_GET_FAN_DUTY_CMD_VERSION) && + is_cros_ec_cmd_available(cros_ec, EC_CMD_PWM_SET_FAN_DUTY, CROS_EC= _HWMON_PWM_SET_FAN_DUTY_CMD_VERSION) && + is_cros_ec_cmd_available(cros_ec, EC_CMD_THERMAL_AUTO_FAN_CTRL, CR= OS_EC_HWMON_THERMAL_AUTO_FAN_CTRL_CMD_VERSION); +} + static int cros_ec_hwmon_probe(struct platform_device *pdev) { struct device *dev =3D &pdev->dev; @@ -258,13 +407,87 @@ static int cros_ec_hwmon_probe(struct platform_device= *pdev) =20 cros_ec_hwmon_probe_temp_sensors(dev, priv, thermal_version); cros_ec_hwmon_probe_fans(priv); + priv->fan_control_supported =3D cros_ec_hwmon_probe_fan_control_supported= (priv->cros_ec); =20 hwmon_dev =3D devm_hwmon_device_register_with_info(dev, "cros_ec", priv, &cros_ec_hwmon_chip_info, NULL); + platform_set_drvdata(pdev, priv); =20 return PTR_ERR_OR_ZERO(hwmon_dev); } =20 +static int cros_ec_hwmon_suspend(struct platform_device *pdev, pm_message_= t state) +{ + struct cros_ec_hwmon_priv *priv =3D platform_get_drvdata(pdev); + u8 control_method; + size_t i; + int ret; + + if (!priv->fan_control_supported) + return 0; + + /* EC sets fan control to auto after suspended, store settings before sus= pending. */ + for (i =3D 0; i < EC_FAN_SPEED_ENTRIES; i++) { + if (!(priv->usable_fans & BIT(i))) + continue; + + ret =3D cros_ec_hwmon_read_pwm_enable(priv->cros_ec, i, &control_method); + if (ret) { + dev_warn(&pdev->dev, "failed to get mode setting for fan %zu: %d\n", i,= ret); + continue; + } + + if (control_method !=3D 1) { + priv->manual_fans &=3D ~BIT(i); + continue; + } else { + priv->manual_fans |=3D BIT(i); + } + + ret =3D cros_ec_hwmon_read_pwm_value( + priv->cros_ec, i, &priv->manual_fan_pwm[i]); + /* + * If storing the value failed, invalidate the stored mode value by sett= ing it + * to auto control. EC will automatically switch to auto mode for that f= an after + * suspended. + */ + if (ret) { + dev_warn(&pdev->dev, "failed to get PWM setting for fan %zu: %pe\n", i,= ERR_PTR(ret)); + priv->manual_fans &=3D ~BIT(i); + continue; + } + } + + return 0; +} + +static int cros_ec_hwmon_resume(struct platform_device *pdev) +{ + const struct cros_ec_hwmon_priv *priv =3D platform_get_drvdata(pdev); + size_t i; + int ret; + + if (!priv->fan_control_supported) + return 0; + + /* EC sets fan control to auto after suspend, restore to settings before = suspend. */ + for (i =3D 0; i < EC_FAN_SPEED_ENTRIES; i++) { + if (!(priv->manual_fans & BIT(i))) + continue; + + /* + * Setting fan PWM value to EC will change the mode to manual for that f= an in EC as + * well, so we do not need to issue a separate fan mode to manual call. + */ + ret =3D cros_ec_hwmon_set_fan_pwm_val( + priv->cros_ec, i, priv->manual_fan_pwm[i]); + if (ret) + dev_warn(&pdev->dev, "failed to restore settings for fan %zu: %d\n", i,= ret); + } + + return 0; +} + static const struct platform_device_id cros_ec_hwmon_id[] =3D { { DRV_NAME, 0 }, {} @@ -273,6 +496,8 @@ static const struct platform_device_id cros_ec_hwmon_id= [] =3D { static struct platform_driver cros_ec_hwmon_driver =3D { .driver.name =3D DRV_NAME, .probe =3D cros_ec_hwmon_probe, + .suspend =3D pm_ptr(cros_ec_hwmon_suspend), + .resume =3D pm_ptr(cros_ec_hwmon_resume), .id_table =3D cros_ec_hwmon_id, }; module_platform_driver(cros_ec_hwmon_driver); --=20 2.50.0.rc2.701.gf1e915cc24-goog From nobody Wed Dec 17 08:57:52 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A7BE7248868; Thu, 19 Jun 2025 07:42:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750318977; cv=none; b=b1hbZueK0BW6DteIijaTwQNEHhD77YAkf3rxq7/hNHmR/qTqW1L+Gcd84yMivp1+rwrJWmp2QqCrzRQWNJEUeYpj051l4vXeuswqtwJ+h/A/c8KMToTA9CaSLSDJc26lNGAtaGnw395dPtzDT/BxZeNn7Gvp7V+NHO8w65p7Rag= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750318977; c=relaxed/simple; bh=RikCrKLnCHgGPRI5B8T21KzfkjhIAbjsuwYMRU52dRA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WwA2Ed+Ctex/PMAVySb+0HwTjQiCoCV47JGdYxRGcQnP2zJmYes+HP7aW4HpdnWnkD1LOaWGyI81OuxoMM0/I8f+lKyRWbkT67/54OS1sfkgJSK6HFVDabPggjtl5AI+B8HJH0c7VhsbViWKrq4RD0SrJeLorsN3SAJhrrVbKbs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FMNFZ1nU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FMNFZ1nU" Received: by smtp.kernel.org (Postfix) with ESMTPS id 5505DC4CEF0; Thu, 19 Jun 2025 07:42:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750318977; bh=RikCrKLnCHgGPRI5B8T21KzfkjhIAbjsuwYMRU52dRA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=FMNFZ1nUawzVMsnxuAV8ZvSLCdcu6V/8dqwWDMNXcZ8307//KT+H9Jh96rABj7OKA Ysd5TiqF4xmP2sxZdMC38/ZG5x4lqrjStLb+h+3svSfJXCtcuWcLu0awC6v7EfI2JD m0oUHZL0zskinZOrW1roH4IBkcB85Dn0EPHgu93HsJan7fVMlC4DfHACHaJdGgZSq0 MYy/+96SYmgKOuTlxa1dXL7pJeykeLxw0CHYUPC4WUOjjc1fTBBi/oiq1CpnV3ekH7 ydwx5Jslr00WDLHFDWgz9OAX08/1YgTzY7RiKomz76eWThraxI6O81aPnZCRt8sXtf RT7MpY5upztrw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46D00C761AE; Thu, 19 Jun 2025 07:42:57 +0000 (UTC) From: Sung-Chi Li via B4 Relay Date: Thu, 19 Jun 2025 15:42:56 +0800 Subject: [PATCH v4 3/3] hwmon: (cros_ec) register fans into thermal framework cooling devices 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: quoted-printable Message-Id: <20250619-cros_ec_fan-v4-3-ca11548ce449@chromium.org> References: <20250619-cros_ec_fan-v4-0-ca11548ce449@chromium.org> In-Reply-To: <20250619-cros_ec_fan-v4-0-ca11548ce449@chromium.org> To: Benson Leung , Guenter Roeck , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Jean Delvare , Guenter Roeck , Jonathan Corbet , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Cc: chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org, Sung-Chi Li , Sung-Chi Li X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1750318976; l=5521; i=lschyi@chromium.org; s=20250429; h=from:subject:message-id; bh=dGrNQjFZmAqqQuLX31fi6rvA+UA6e5udimOyMjEIouo=; b=0dwadgCMsVUj2zNX3LAZTyaW6PXKOphWxSH6/DPNS5uCEwqeWx63ZWVIgvgYoHCvDsHjphEi1 L4uaCbewRHCA9AiGKf2p1+FNA5FaKMC2pN+rrjROQ6BHBtWACsiXCfq X-Developer-Key: i=lschyi@chromium.org; a=ed25519; pk=9gCZPRJmYyHDt6VN9FV2UreFcUr73JFrwYvmsltW9Y8= X-Endpoint-Received: by B4 Relay for lschyi@chromium.org/20250429 with auth_id=392 X-Original-From: Sung-Chi Li Reply-To: lschyi@chromium.org From: Sung-Chi Li Register fans connected under EC as thermal cooling devices as well, so these fans can then work with the thermal framework. During the driver probing phase, we will also try to register each fan as a thermal cooling device based on previous probe result (whether the there are fans connected on that channel, and whether EC supports fan control). The basic get max state, get current state, and set current state methods are then implemented as well. Signed-off-by: Sung-Chi Li Acked-by: Thomas Wei=C3=9Fschuh --- Documentation/hwmon/cros_ec_hwmon.rst | 2 + drivers/hwmon/cros_ec_hwmon.c | 85 +++++++++++++++++++++++++++++++= ++++ 2 files changed, 87 insertions(+) diff --git a/Documentation/hwmon/cros_ec_hwmon.rst b/Documentation/hwmon/cr= os_ec_hwmon.rst index 355557a08c9a54b4c177bafde3743e7dc02218be..6db812708325f7abb6d319af331= 2b4079e6923c6 100644 --- a/Documentation/hwmon/cros_ec_hwmon.rst +++ b/Documentation/hwmon/cros_ec_hwmon.rst @@ -27,3 +27,5 @@ Fan and temperature readings are supported. PWM fan contr= ol is also supported if the EC also supports setting fan PWM values and fan mode. Note that EC will switch fan control mode back to auto when suspended. This driver will rest= ore the fan state to what they were before suspended when resumed. +If a fan is controllable, this driver will register that fan as a cooling = device +in the thermal framework as well. diff --git a/drivers/hwmon/cros_ec_hwmon.c b/drivers/hwmon/cros_ec_hwmon.c index c0c4ffa6f85419edf5dacd730192e7f1c28e2e5c..572b194e51fb1a05fa5151abb08= 26d1c8ba2948d 100644 --- a/drivers/hwmon/cros_ec_hwmon.c +++ b/drivers/hwmon/cros_ec_hwmon.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include =20 @@ -31,6 +32,11 @@ struct cros_ec_hwmon_priv { u8 manual_fan_pwm[EC_FAN_SPEED_ENTRIES]; }; =20 +struct cros_ec_hwmon_cooling_priv { + struct cros_ec_hwmon_priv *hwmon_priv; + u8 index; +}; + static int cros_ec_hwmon_read_fan_speed(struct cros_ec_device *cros_ec, u8= index, u16 *speed) { int ret; @@ -306,6 +312,42 @@ static const struct hwmon_channel_info * const cros_ec= _hwmon_info[] =3D { NULL }; =20 +static int cros_ec_hwmon_cooling_get_max_state(struct thermal_cooling_devi= ce *cdev, + unsigned long *val) +{ + *val =3D 255; + return 0; +} + +static int cros_ec_hwmon_cooling_get_cur_state(struct thermal_cooling_devi= ce *cdev, + unsigned long *val) +{ + const struct cros_ec_hwmon_cooling_priv *priv =3D cdev->devdata; + u8 read_val; + int ret; + + ret =3D cros_ec_hwmon_read_pwm_value(priv->hwmon_priv->cros_ec, priv->ind= ex, &read_val); + if (ret) + return ret; + + *val =3D read_val; + return ret; +} + +static int cros_ec_hwmon_cooling_set_cur_state(struct thermal_cooling_devi= ce *cdev, + unsigned long val) +{ + const struct cros_ec_hwmon_cooling_priv *priv =3D cdev->devdata; + + return cros_ec_hwmon_write_pwm_input(priv->hwmon_priv->cros_ec, priv->ind= ex, val); +} + +static const struct thermal_cooling_device_ops cros_ec_thermal_cooling_ops= =3D { + .get_max_state =3D cros_ec_hwmon_cooling_get_max_state, + .get_cur_state =3D cros_ec_hwmon_cooling_get_cur_state, + .set_cur_state =3D cros_ec_hwmon_cooling_set_cur_state, +}; + static const struct hwmon_ops cros_ec_hwmon_ops =3D { .read =3D cros_ec_hwmon_read, .read_string =3D cros_ec_hwmon_read_string, @@ -381,6 +423,48 @@ static bool cros_ec_hwmon_probe_fan_control_supported(= struct cros_ec_device *cro is_cros_ec_cmd_available(cros_ec, EC_CMD_THERMAL_AUTO_FAN_CTRL, CR= OS_EC_HWMON_THERMAL_AUTO_FAN_CTRL_CMD_VERSION); } =20 +static void cros_ec_hwmon_register_fan_cooling_devices(struct device *dev, + struct cros_ec_hwmon_priv *priv) +{ + struct cros_ec_hwmon_cooling_priv *cpriv; + struct thermal_cooling_device *cdev; + char *type; + size_t i; + + if (!IS_ENABLED(CONFIG_THERMAL)) + return; + + if (!priv->fan_control_supported) + return; + + for (i =3D 0; i < EC_FAN_SPEED_ENTRIES; i++) { + if (!(priv->usable_fans & BIT(i))) + continue; + + cpriv =3D devm_kzalloc(dev, sizeof(*cpriv), GFP_KERNEL); + if (!cpriv) { + dev_warn(dev, "no memory for registering fan %zu as a cooling device\n"= , i); + continue; + } + + type =3D devm_kasprintf(dev, GFP_KERNEL, "%s-fan%zu", dev_name(dev), i); + if (!type) { + dev_warn(dev, "no memory to compose cooling device type for fan %zu\n",= i); + continue; + } + + cpriv->hwmon_priv =3D priv; + cpriv->index =3D i; + cdev =3D devm_thermal_of_cooling_device_register(dev, NULL, type, cpriv, + &cros_ec_thermal_cooling_ops); + if (IS_ERR(cdev)) { + dev_warn(dev, "failed to register fan %zu as a cooling device: %pe\n", = i, + cdev); + continue; + } + } +} + static int cros_ec_hwmon_probe(struct platform_device *pdev) { struct device *dev =3D &pdev->dev; @@ -408,6 +492,7 @@ static int cros_ec_hwmon_probe(struct platform_device *= pdev) cros_ec_hwmon_probe_temp_sensors(dev, priv, thermal_version); cros_ec_hwmon_probe_fans(priv); priv->fan_control_supported =3D cros_ec_hwmon_probe_fan_control_supported= (priv->cros_ec); + cros_ec_hwmon_register_fan_cooling_devices(dev, priv); =20 hwmon_dev =3D devm_hwmon_device_register_with_info(dev, "cros_ec", priv, &cros_ec_hwmon_chip_info, NULL); --=20 2.50.0.rc2.701.gf1e915cc24-goog