Surface fan monitoring driver
Subsystems (6)
Ivor Wanders
maintainerv2 cover Surface fan monitoring driver
Second version of a hwmon driver to monitor the fan's rpm on Microsoft Surface devices, originally submitted in [1].
Changes in v2:
- Removed all unsupported sysfs attributes from the hwmon driver, leaving the fan input as the only supported attribute.
[1] https://lore.kernel.org/linux-hwmon/[email protected]/T/
Ivor Wanders (2):
hwmon: add fan speed monitoring driver for Surface devices platform/surface: aggregator_registry: add entry for fan speed
Documentation/hwmon/index.rst | 1 + Documentation/hwmon/surface_fan.rst | 25 +++++ MAINTAINERS | 8 ++ drivers/hwmon/Kconfig | 13 +++ drivers/hwmon/Makefile | 1 + drivers/hwmon/surface_fan.c | 105 ++++++++++++++++++ .../surface/surface_aggregator_registry.c | 7 ++ 7 files changed, 160 insertions(+) create mode 100644 Documentation/hwmon/surface_fan.rst create mode 100644 drivers/hwmon/surface_fan.c
Signature · 1 line
2.17.1
Ivor Wanders
maintainerPATCH v2 1/2 hwmon: add fan speed monitoring driver for Surface devices
Adds a driver that provides read only access to the fan speed for Microsoft Surface Pro devices. The fan speed is always regulated by the EC and cannot be influenced directly.
---
Changes in v2:
- Removed all sysfs attributes except fan1_input. Simplified code and updated documentation accordingly.
--- Documentation/hwmon/index.rst | 1 + Documentation/hwmon/surface_fan.rst | 25 +++++++ MAINTAINERS | 8 +++ drivers/hwmon/Kconfig | 13 ++++ drivers/hwmon/Makefile | 1 + drivers/hwmon/surface_fan.c | 105 ++++++++++++++++++++++++++++ 6 files changed, 153 insertions(+) create mode 100644 Documentation/hwmon/surface_fan.rst create mode 100644 drivers/hwmon/surface_fan.c
Documentation/hwmon/index.rst +1 -0
@@ -202,6 +202,7 @@ Hardware Monitoring Kernel Drivers smsc47m1 sparx5-temp stpddc60 + surface_fan sy7636a-hwmon tc654 tc74
Documentation/hwmon/surface_fan.rst new file +25 -0
@@ -0,0 +1,25 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Kernel driver surface_fan +========================= + +Supported Devices: + + * Microsoft Surface Pro 9 + +Author: Ivor Wanders <[email protected]> + +Description +----------- + +This provides monitoring of the fan found in some Microsoft Surface Pro devices, +like the Surface Pro 9. The fan is always controlled by the onboard controller. + +Sysfs interface +--------------- + +======================= ======= ========================================= +Name Perm Description +======================= ======= ========================================= +``fan1_input`` RO Current fan speed in RPM. +======================= ======= =========================================
MAINTAINERS +8 -0
@@ -14078,6 +14078,14 @@ F: Documentation/driver-api/surface_aggregator/clients/dtx.rst F: drivers/platform/surface/surface_dtx.c F: include/uapi/linux/surface_aggregator/dtx.h +MICROSOFT SURFACE SENSOR FAN DRIVER +M: Maximilian Luz <[email protected]> +M: Ivor Wanders <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/hwmon/surface_fan.rst +F: drivers/hwmon/surface_fan.c + MICROSOFT SURFACE GPE LID SUPPORT DRIVER M: Maximilian Luz <[email protected]> L: [email protected]
drivers/hwmon/Kconfig +13 -0
@@ -1965,6 +1965,19 @@ config SENSORS_SMM665 This driver can also be built as a module. If so, the module will be called smm665. +config SENSORS_SURFACE_FAN + tristate "Surface Fan Driver" + depends on SURFACE_AGGREGATOR + help + Driver that provides monitoring of the fan on Surface Pro devices that + have a fan, like the Surface Pro 9. + + This makes the fan's current speed accessible through the hwmon + system. It does not provide control over the fan, the firmware is + responsible for that, this driver merely provides monitoring. + + Select M or Y here, if you want to be able to read the fan's speed. + config SENSORS_ADC128D818 tristate "Texas Instruments ADC128D818" depends on I2C
drivers/hwmon/Makefile +1 -0
@@ -198,6 +198,7 @@ obj-$(CONFIG_SENSORS_SMSC47M1) += smsc47m1.o obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o obj-$(CONFIG_SENSORS_SPARX5) += sparx5-temp.o obj-$(CONFIG_SENSORS_STTS751) += stts751.o +obj-$(CONFIG_SENSORS_SURFACE_FAN)+= surface_fan.o obj-$(CONFIG_SENSORS_SY7636A) += sy7636a-hwmon.o obj-$(CONFIG_SENSORS_AMC6821) += amc6821.o obj-$(CONFIG_SENSORS_TC74) += tc74.o
drivers/hwmon/surface_fan.c new file +105 -0
105 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v2 1/2] hwmon: add fan speed monitoring driver for Surface devices
Am 28.12.23 um 01:34 schrieb Ivor Wanders:
Quoted 146 lines
Adds a driver that provides read only access to the fan speed for Microsoft Surface Pro devices. The fan speed is always regulated by the EC and cannot be influenced directly.
---
Changes in v2:
- Removed all sysfs attributes exceptfan1_input. Simplified code and updated documentation accordingly.--- Documentation/hwmon/index.rst | 1 + Documentation/hwmon/surface_fan.rst | 25 +++++++ MAINTAINERS | 8 +++ drivers/hwmon/Kconfig | 13 ++++ drivers/hwmon/Makefile | 1 + drivers/hwmon/surface_fan.c | 105 ++++++++++++++++++++++++++++ 6 files changed, 153 insertions(+)create mode 100644
Documentation/hwmon/surface_fan.rstcreate mode 100644drivers/hwmon/surface_fan.cDocumentation/hwmon/index.rst +1
-0@@ -202,6 +202,7 @@ Hardware Monitoring Kernel Drivers smsc47m1 sparx5-temp stpddc60 + surface_fan sy7636a-hwmon tc654 tc74Documentation/hwmon/surface_fan.rst new file +25
-0@@ -0,0 +1,25 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Kernel driver surface_fan +========================= + +Supported Devices: + + * Microsoft Surface Pro 9 + +Author: Ivor Wanders <[email protected]> + +Description +----------- + +This provides monitoring of the fan found in some Microsoft Surface Pro devices, +like the Surface Pro 9. The fan is always controlled by the onboard controller. + +Sysfs interface +--------------- + +======================= ======= ========================================= +Name Perm Description +======================= ======= ========================================= +``fan1_input`` RO Current fan speed in RPM. +======================= ======= =========================================MAINTAINERS +8
-0@@ -14078,6 +14078,14 @@ F: Documentation/driver-api/surface_aggregator/clients/dtx.rst F: drivers/platform/surface/surface_dtx.c F: include/uapi/linux/surface_aggregator/dtx.h +MICROSOFT SURFACE SENSOR FAN DRIVER +M: Maximilian Luz <[email protected]> +M: Ivor Wanders <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/hwmon/surface_fan.rst +F: drivers/hwmon/surface_fan.c + MICROSOFT SURFACE GPE LID SUPPORT DRIVER M: Maximilian Luz <[email protected]> L: [email protected]drivers/hwmon/Kconfig +13
-0@@ -1965,6 +1965,19 @@ config SENSORS_SMM665 This driver can also be built as a module. If so, the module will be called smm665. +config SENSORS_SURFACE_FAN + tristate "Surface Fan Driver" + depends on SURFACE_AGGREGATOR + help + Driver that provides monitoring of the fan on Surface Pro devices that + have a fan, like the Surface Pro 9. + + This makes the fan's current speed accessible through the hwmon + system. It does not provide control over the fan, the firmware is + responsible for that, this driver merely provides monitoring. + + Select M or Y here, if you want to be able to read the fan's speed. + config SENSORS_ADC128D818 tristate "Texas Instruments ADC128D818" depends on I2Cdrivers/hwmon/Makefile +1
-0@@ -198,6 +198,7 @@ obj-$(CONFIG_SENSORS_SMSC47M1) += smsc47m1.o obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o obj-$(CONFIG_SENSORS_SPARX5) += sparx5-temp.o obj-$(CONFIG_SENSORS_STTS751) += stts751.o +obj-$(CONFIG_SENSORS_SURFACE_FAN)+= surface_fan.o obj-$(CONFIG_SENSORS_SY7636A) += sy7636a-hwmon.o obj-$(CONFIG_SENSORS_AMC6821) += amc6821.o obj-$(CONFIG_SENSORS_TC74) += tc74.odrivers/hwmon/surface_fan.c new file +21
-0@@ -0,0 +1,105 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Surface Fan driver for Surface System Aggregator Module. It provides access +* to the fan's rpm through the hwmon system. +* +* Copyright (C) 2023 Ivor Wanders <[email protected]> +*/ + +#include <linux/hwmon.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/surface_aggregator/device.h> +#include <linux/types.h> + +// SSAM +SSAM_DEFINE_SYNC_REQUEST_CL_R (__ssam_fan_rpm_get, __le16, { + .target_category = SSAM_SSH_TC_FAN, + .command_id = 0x01, +}); +
drivers/hwmon/surface_fan.c line 31
Quoted 10 lines
drivers/hwmon/surface_fan.c new file +10
-0+// hwmon +umode_t surface_fan_hwmon_is_visible (const void *drvdata, +enum hwmon_sensor_types type,u32 attr, +int channel) +{ +if (type != hwmon_fan) +return 0 ; + +if (attr != hwmon_fan_input) +return 0 ;
Hi,
You can drop those checks, the hwmon core will only call surface_fan_hwmon_is_visible() for attributes in surface_fan_info[].
Quoted 6 lines
drivers/hwmon/surface_fan.c new file +6
-0+ +return 0444 ; +} + +static int surface_fan_hwmon_read(struct device *dev, + enum hwmon_sensor_types type, u32 attr,
drivers/hwmon/surface_fan.c line 47
Quoted 10 lines
drivers/hwmon/surface_fan.c new file +10
-0+ int channel, long *val) +{ + struct ssam_device *sdev = dev_get_drvdata(dev); +__le16 value; + +if (type != hwmon_fan) +return -EOPNOTSUPP ; + +if (attr != hwmon_fan_input) +return -EOPNOTSUPP ;
Same as above
drivers/hwmon/surface_fan.c line 50
Quoted 3 lines
drivers/hwmon/surface_fan.c new file +3
-0+ +if (__ssam_fan_rpm_get (sdev, &value)) +return -EIO ;
Please propagate the error from __ssam_fan_rpm_get().
Quoted 18 lines
drivers/hwmon/surface_fan.c new file +18
-0+ + *val =le16_to_cpu (value); + +return 0 ; +} + +static const struct hwmon_channel_info *const surface_fan_info[] = { +HWMON_CHANNEL_INFO (fan,HWMON_F_INPUT ), +NULL +}; + +static const struct hwmon_ops surface_fan_hwmon_ops = { + .is_visible = surface_fan_hwmon_is_visible, + .read = surface_fan_hwmon_read, +}; + +static const struct hwmon_chip_info surface_fan_chip_info = { + .ops = &surface_fan_hwmon_ops,
drivers/hwmon/surface_fan.c line 78
Quoted 10 lines
drivers/hwmon/surface_fan.c new file +10
-0+ .info = surface_fan_info, +}; + +static int surface_fan_probe (struct ssam_device *sdev) +{ +struct device *hdev; + + hdev =devm_hwmon_device_register_with_info (&sdev->dev,"fan" , sdev, + &surface_fan_chip_info, +NULL );
Maybe you could use a more unique name for the hwmon chip, like "surface_fan".
drivers/hwmon/surface_fan.c line 82
Quoted 4 lines
drivers/hwmon/surface_fan.c new file +4
-0+if (IS_ERR (hdev)) +return PTR_ERR (hdev); + +ssam_device_set_drvdata (sdev, sdev);
What exactly is the purpose of calling ssam_device_set_drvdata() here?
Thanks,
Armin Wolf
Quoted 23 lines
drivers/hwmon/surface_fan.c new file +23
-0+ +return 0 ; +} + +static const struct ssam_device_id ssam_fan_match[] = { + {SSAM_SDEV (FAN ,SAM ,0x01 ,0x01 ) }, + {}, +}; +MODULE_DEVICE_TABLE (ssam, ssam_fan_match); + +static struct ssam_device_driver surface_fan = { + .probe = surface_fan_probe, + .match_table = ssam_fan_match, + .driver = { + .name ="surface_fan" , + .probe_type =PROBE_PREFER_ASYNCHRONOUS , + }, +}; +module_ssam_device_driver (surface_fan); + +MODULE_AUTHOR ("Ivor Wanders <[email protected]>" ); +MODULE_DESCRIPTION ("Fan Driver for Surface System Aggregator Module" ); +MODULE_LICENSE ("GPL" );
Ivor Wanders
maintainerRe: [PATCH v2 1/2] hwmon: add fan speed monitoring driver for Surface devices
Hi Armin, thank you for your time and comments.
Quoted 2 lines
You can drop those checks, the hwmon core.
Maybe you could use a more unique name for the hwmon chip, like "surface_fan".
Sure, incorporated both suggestions.
Quoted 1 line
Please propagate the error from
__ssam_fan_rpm_get().
Done. Currently that function can only return -EIO and 0, but indeed best to propagate that in case more error codes are returned in the future.
Quoted 1 line
What exactly is the purpose of calling
ssam_device_set_drvdata()here?
That line was necessary when this driver also hooked into the thermal subsystem. Integration with the thermal subsystem was removed in downstream iterations when I discovered we can't directly control the fan speed, see [1] for more info. I've removed this line, as it no longer serves any purpose here.
~Ivor
[1]: https://github.com/linux-surface/kernel/pull/144#issuecomment-1853123261
Maximilian Luz
maintainerRe: [PATCH v2 1/2] hwmon: add fan speed monitoring driver for Surface devices
In reply to Ivor WandersQuoted 21 lines · Ivor Wanders
Adds a driver that provides read only access to the fan speed for Microsoft Surface Pro devices. The fan speed is always regulated by the EC and cannot be influenced directly.
---
Changes in v2:
- Removed all sysfs attributes exceptfan1_input. Simplified code and updated documentation accordingly.--- Documentation/hwmon/index.rst | 1 + Documentation/hwmon/surface_fan.rst | 25 +++++++ MAINTAINERS | 8 +++ drivers/hwmon/Kconfig | 13 ++++ drivers/hwmon/Makefile | 1 + drivers/hwmon/surface_fan.c | 105 ++++++++++++++++++++++++++++ 6 files changed, 153 insertions(+)create mode 100644
Documentation/hwmon/surface_fan.rstcreate mode 100644drivers/hwmon/surface_fan.c
[...]
Quoted 8 lines
drivers/hwmon/surface_fan.c new file +2
-0@@ -0,0 +1,105 @@ +// SPDX-License-Identifier: GPL-2.0+ +/*
drivers/hwmon/surface_fan.c line 12
Quoted 10 lines
drivers/hwmon/surface_fan.c new file +10
-0+* Surface Fan driver for Surface System Aggregator Module. It provides access +* to the fan's rpm through the hwmon system. +* +* Copyright (C) 2023 Ivor Wanders <[email protected]> +*/ + +#include <linux/hwmon.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/platform_device.h>
As far as I can see, linux/platform_device.h is not needed.
Regards,
Max
Quoted 93 lines
drivers/hwmon/surface_fan.c new file +93
-0+#include <linux/surface_aggregator/device.h> +#include <linux/types.h> + +// SSAM +SSAM_DEFINE_SYNC_REQUEST_CL_R (__ssam_fan_rpm_get, __le16, { + .target_category = SSAM_SSH_TC_FAN, + .command_id = 0x01, +}); + +// hwmon +umode_t surface_fan_hwmon_is_visible (const void *drvdata, +enum hwmon_sensor_types type,u32 attr, +int channel) +{ +if (type != hwmon_fan) +return 0 ; + +if (attr != hwmon_fan_input) +return 0 ; + +return 0444 ; +} + +static int surface_fan_hwmon_read (struct device *dev, +enum hwmon_sensor_types type,u32 attr, +int channel,long *val) +{ +struct ssam_device *sdev =dev_get_drvdata (dev); +__le16 value; + +if (type != hwmon_fan) +return -EOPNOTSUPP ; + +if (attr != hwmon_fan_input) +return -EOPNOTSUPP ; + +if (__ssam_fan_rpm_get (sdev, &value)) +return -EIO ; + + *val =le16_to_cpu (value); + +return 0 ; +} + +static const struct hwmon_channel_info *const surface_fan_info[] = { +HWMON_CHANNEL_INFO (fan,HWMON_F_INPUT ), +NULL +}; + +static const struct hwmon_ops surface_fan_hwmon_ops = { + .is_visible = surface_fan_hwmon_is_visible, + .read = surface_fan_hwmon_read, +}; + +static const struct hwmon_chip_info surface_fan_chip_info = { + .ops = &surface_fan_hwmon_ops, + .info = surface_fan_info, +}; + +static int surface_fan_probe (struct ssam_device *sdev) +{ +struct device *hdev; + + hdev =devm_hwmon_device_register_with_info (&sdev->dev,"fan" , sdev, + &surface_fan_chip_info, +NULL ); +if (IS_ERR (hdev)) +return PTR_ERR (hdev); + +ssam_device_set_drvdata (sdev, sdev); + +return 0 ; +} + +static const struct ssam_device_id ssam_fan_match[] = { + {SSAM_SDEV (FAN ,SAM ,0x01 ,0x01 ) }, + {}, +}; +MODULE_DEVICE_TABLE (ssam, ssam_fan_match); + +static struct ssam_device_driver surface_fan = { + .probe = surface_fan_probe, + .match_table = ssam_fan_match, + .driver = { + .name ="surface_fan" , + .probe_type =PROBE_PREFER_ASYNCHRONOUS , + }, +}; +module_ssam_device_driver (surface_fan); + +MODULE_AUTHOR ("Ivor Wanders <[email protected]>" ); +MODULE_DESCRIPTION ("Fan Driver for Surface System Aggregator Module" ); +MODULE_LICENSE ("GPL" );
Ivor Wanders
maintainerRe: [PATCH v2 1/2] hwmon: add fan speed monitoring driver for Surface devices
Quoted 1 line
As far as I can see,
linux/platform_device.his not needed.
Correct, it's no longer necessary, I've removed it, thanks!
~Ivor
Ivor Wanders
maintainerPATCH v2 2/2 platform/surface: aggregator_registry: add entry for fan speed
In reply to Ivor WandersAdd an entry for the fan speed function.
Add this new entry to the Surface Pro 9 group.
---
Changes in v2:
- No changes in this patch.
--- drivers/platform/surface/surface_aggregator_registry.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/platform/surface/surface_aggregator_registry.c +7 -0
@@ -74,6 +74,12 @@ static const struct software_node ssam_node_tmp_pprof = { .parent = &ssam_node_root, }; +/* Fan speed function. */ +static const struct software_node ssam_node_fan_speed = { + .name ="ssam:01:05:01:01:01" , + .parent = &ssam_node_root, +}; +/* Tablet-mode switch via KIP subsystem. */ static const struct software_node ssam_node_kip_tablet_switch = { .name ="ssam:01:0e:01:00:01" ,
@@ -319,6 +325,7 @@ static const struct software_node *ssam_node_group_sp9[] = { &ssam_node_bat_ac, &ssam_node_bat_main, &ssam_node_tmp_pprof, + &ssam_node_fan_speed, &ssam_node_pos_tablet_switch, &ssam_node_hid_kip_keyboard, &ssam_node_hid_kip_penstash,
Signature · 1 line
2.17.1
Re: [PATCH v2 2/2] platform/surface: aggregator_registry: add entry for fan speed
Quoted 17 lines · Ivor Wanders
Add an entry for the fan speed function.
Add this new entry to the Surface Pro 9 group.---
Changes in v2:
- No changes in this patch.--- drivers/platform/surface/surface_aggregator_registry.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/platform/surface/surface_aggregator_registry.c +0
-0
drivers/platform/surface/surface_aggregator_registry.c static const struct software_node ssam_node_tmp_pprof = { · line 81
Quoted 8 lines
drivers/platform/surface/surface_aggregator_registry.c +5
-0@@ -74,6 +74,12 @@ static const struct software_node ssam_node_tmp_pprof = { .parent = &ssam_node_root, }; +/* Fan speed function. */ +static const struct software_node ssam_node_fan_speed = { + .name ="ssam:01:05:01:01:01" , + .parent = &ssam_node_root, +};
I would prefer if we could keep the subsystem prefix for node names. So something like ssam_node_tmp_fan_speed.
Otherwise, this looks good to me. With that changed:
Quoted 12 lines
drivers/platform/surface/surface_aggregator_registry.c +2
-0+/* Tablet-mode switch via KIP subsystem. */ static const struct software_node ssam_node_kip_tablet_switch = { .name ="ssam:01:0e:01:00:01" ,@@ -319,6 +325,7 @@ static const struct software_node *ssam_node_group_sp9[] = { &ssam_node_bat_ac, &ssam_node_bat_main, &ssam_node_tmp_pprof, + &ssam_node_fan_speed, &ssam_node_pos_tablet_switch, &ssam_node_hid_kip_keyboard, &ssam_node_hid_kip_penstash,
Maximilian Luz
maintainerRe: [PATCH v2 2/2] platform/surface: aggregator_registry: add entry for fan speed
Quoted 13 lines · Maximilian Luz
Quoted 12 lines · Ivor Wanders
Add an entry for the fan speed function.
Add this new entry to the Surface Pro 9 group.---
Changes in v2:
- No changes in this patch.
---
drivers/platform/surface/surface_aggregator_registry.c| 7 +++++++1 file changed, 7 insertions(+)
Quoted 15 lines
drivers/platform/surface/surface_aggregator_registry.c static const struct software_node ssam_node_tmp_pprof = { · line 80
Quoted 12 lines
drivers/platform/surface/surface_aggregator_registry.c +5
-0@@ -74,6 +74,12 @@ static const struct software_node ssam_node_tmp_pprof = { .parent = &ssam_node_root, }; +/* Fan speed function. */ +static const struct software_node ssam_node_fan_speed = { + .name = "ssam:01:05:01:01:01", + .parent = &ssam_node_root, +};I would prefer if we could keep the subsystem prefix for node names. So something like
ssam_node_tmp_fan_speed.
Please disregard that comment. Somehow I thought it's part of the TMP subsystem, but it's in its own FAN subsystem. So all is good.
Quoted 16 lines
Otherwise, this looks good to me. With that changed:
Reviewed-by Maximilian Luz <[email protected]>Quoted 12 lines
drivers/platform/surface/surface_aggregator_registry.c +2
-0+ /* Tablet-mode switch via KIP subsystem. */ static const struct software_node ssam_node_kip_tablet_switch = { .name = "ssam:01:0e:01:00:01",@@ -319,6 +325,7 @@ static const struct software_node *ssam_node_group_sp9[] = { &ssam_node_bat_ac, &ssam_node_bat_main, &ssam_node_tmp_pprof, + &ssam_node_fan_speed, &ssam_node_pos_tablet_switch, &ssam_node_hid_kip_keyboard, &ssam_node_hid_kip_penstash,