[PATCH v5 1/3] hwmon: (applesmc) Cache fan positions during register initialization
From: Shih-Yuan Lee
Date: Sat Jul 11 2026 - 03:58:38 EST
To support the read_string callback for fan labels in the modern HWMON
API, load and cache the fan position names in smcreg.fan_positions
during register initialization.
Pre-pad fallback labels with four spaces to match the "+ 4" pointer
arithmetic offset used by all fan labels in the read_string callback.
Signed-off-by: Shih-Yuan Lee <fourdollars@xxxxxxxxxx>
---
drivers/hwmon/applesmc.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index 90a14a7f2c4c..9b2d9ecb20c0 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -133,6 +133,7 @@ static struct applesmc_registers {
bool init_complete; /* true when fully initialized */
struct applesmc_entry *cache; /* cached key entries */
const char **index; /* temperature key index */
+ char fan_positions[10][17]; /* cached fan position labels */
} smcreg = {
.mutex = __MUTEX_INITIALIZER(smcreg.mutex),
};
@@ -566,7 +567,7 @@ static int applesmc_init_smcreg_try(void)
{
struct applesmc_registers *s = &smcreg;
bool left_light_sensor = false, right_light_sensor = false;
- unsigned int count;
+ unsigned int count, i;
u8 tmp[1];
int ret;
@@ -597,6 +598,16 @@ static int applesmc_init_smcreg_try(void)
if (s->fan_count > 10)
s->fan_count = 10;
+ for (i = 0; i < s->fan_count; i++) {
+ char newkey[5];
+
+ scnprintf(newkey, sizeof(newkey), FAN_ID_FMT, i);
+ ret = applesmc_read_key(newkey, s->fan_positions[i], 16);
+ s->fan_positions[i][16] = 0;
+ if (ret)
+ scnprintf(s->fan_positions[i], 17, " Fan %d", i);
+ }
+
ret = applesmc_get_lower_bound(&s->temp_begin, "T");
if (ret)
return ret;
--
2.39.5