[PATCH 2/5] hwmon: (cros_ec) Add support for fan target speed

From: Thomas Weißschuh
Date: Sat Jun 08 2024 - 04:13:07 EST


Use EC_CMD_PWM_GET_FAN_TARGET_RPM to retrieve the target fan speed.
The CrOS EC only supports this for the first fan.

Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
---
Documentation/hwmon/cros_ec_hwmon.rst | 6 +++++-
drivers/hwmon/cros_ec_hwmon.c | 26 +++++++++++++++++++++++++-
2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/Documentation/hwmon/cros_ec_hwmon.rst b/Documentation/hwmon/cros_ec_hwmon.rst
index 47ecae983bdb..4aede331baeb 100644
--- a/Documentation/hwmon/cros_ec_hwmon.rst
+++ b/Documentation/hwmon/cros_ec_hwmon.rst
@@ -23,4 +23,8 @@ ChromeOS embedded controller used in Chromebooks and other devices.

The channel labels exposed via hwmon are retrieved from the EC itself.

-Fan and temperature readings are supported.
+Supported features:
+
+ - Current fan speed
+ - Target fan speed (for fan 1 only)
+ - Current temperature
diff --git a/drivers/hwmon/cros_ec_hwmon.c b/drivers/hwmon/cros_ec_hwmon.c
index 5514cf780b8b..09b8057e1223 100644
--- a/drivers/hwmon/cros_ec_hwmon.c
+++ b/drivers/hwmon/cros_ec_hwmon.c
@@ -36,6 +36,20 @@ static int cros_ec_hwmon_read_fan_speed(struct cros_ec_device *cros_ec, u8 index
return 0;
}

+static int cros_ec_hwmon_read_fan_target(struct cros_ec_device *cros_ec, u16 *speed)
+{
+ struct ec_response_pwm_get_fan_rpm resp;
+ int ret;
+
+ ret = cros_ec_cmd(cros_ec, 0, EC_CMD_PWM_GET_FAN_TARGET_RPM,
+ NULL, 0, &resp, sizeof(resp));
+ if (ret < 0)
+ return ret;
+
+ *speed = resp.rpm;
+ return 0;
+}
+
static int cros_ec_hwmon_read_temp(struct cros_ec_device *cros_ec, u8 index, u8 *temp)
{
unsigned int offset;
@@ -91,6 +105,11 @@ static int cros_ec_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
ret = cros_ec_hwmon_read_fan_speed(priv->cros_ec, channel, &speed);
if (ret == 0)
*val = cros_ec_hwmon_is_error_fan(speed);
+
+ } else if (attr == hwmon_fan_target) {
+ ret = cros_ec_hwmon_read_fan_target(priv->cros_ec, &speed);
+ if (ret == 0)
+ *val = speed;
}
} else if (type == hwmon_temp) {
if (attr == hwmon_temp_input) {
@@ -128,8 +147,13 @@ static umode_t cros_ec_hwmon_is_visible(const void *data, enum hwmon_sensor_type
u32 attr, int channel)
{
const struct cros_ec_hwmon_priv *priv = data;
+ u16 speed;

if (type == hwmon_fan) {
+ if (attr == hwmon_fan_target &&
+ cros_ec_hwmon_read_fan_target(priv->cros_ec, &speed) == -EOPNOTSUPP)
+ return 0;
+
if (priv->usable_fans & BIT(channel))
return 0444;
} else if (type == hwmon_temp) {
@@ -142,7 +166,7 @@ static umode_t cros_ec_hwmon_is_visible(const void *data, enum hwmon_sensor_type

static const struct hwmon_channel_info * const cros_ec_hwmon_info[] = {
HWMON_CHANNEL_INFO(fan,
- HWMON_F_INPUT | HWMON_F_FAULT,
+ HWMON_F_INPUT | HWMON_F_FAULT | HWMON_F_TARGET,
HWMON_F_INPUT | HWMON_F_FAULT,
HWMON_F_INPUT | HWMON_F_FAULT,
HWMON_F_INPUT | HWMON_F_FAULT),

--
2.45.2