Skip to content

Commit 00049e7

Browse files
iwandersReillyBrogan
authored andcommitted
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. Signed-off-by: Ivor Wanders <ivor@iwanders.net> Link: linux-surface/kernel#144 Patchset: surface-sam
1 parent 1f6f456 commit 00049e7

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed

Documentation/hwmon/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ Hardware Monitoring Kernel Drivers
202202
smsc47m1
203203
sparx5-temp
204204
stpddc60
205+
surface_fan
205206
sy7636a-hwmon
206207
tc654
207208
tc74
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.. SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
Kernel driver surface_fan
4+
=========================
5+
6+
Supported Devices:
7+
8+
* Microsoft Surface Pro 9
9+
10+
Author: Ivor Wanders <ivor@iwanders.net>
11+
12+
Description
13+
-----------
14+
15+
This provides monitoring of the fan found in some Microsoft Surface Pro devices,
16+
like the Surface Pro 9. The fan is always controlled by the onboard controller.
17+
18+
Sysfs interface
19+
---------------
20+
21+
======================= ======= =========================================
22+
Name Perm Description
23+
======================= ======= =========================================
24+
``fan1_input`` RO Current fan speed in RPM.
25+
======================= ======= =========================================

MAINTAINERS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14211,6 +14211,14 @@ F: Documentation/driver-api/surface_aggregator/clients/dtx.rst
1421114211
F: drivers/platform/surface/surface_dtx.c
1421214212
F: include/uapi/linux/surface_aggregator/dtx.h
1421314213

14214+
MICROSOFT SURFACE SENSOR FAN DRIVER
14215+
M: Maximilian Luz <luzmaximilian@gmail.com>
14216+
M: Ivor Wanders <ivor@iwanders.net>
14217+
L: linux-hwmon@vger.kernel.org
14218+
S: Maintained
14219+
F: Documentation/hwmon/surface_fan.rst
14220+
F: drivers/hwmon/surface_fan.c
14221+
1421414222
MICROSOFT SURFACE GPE LID SUPPORT DRIVER
1421514223
M: Maximilian Luz <luzmaximilian@gmail.com>
1421614224
L: platform-driver-x86@vger.kernel.org

drivers/hwmon/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,19 @@ config SENSORS_SFCTEMP
19611961
This driver can also be built as a module. If so, the module
19621962
will be called sfctemp.
19631963

1964+
config SENSORS_SURFACE_FAN
1965+
tristate "Surface Fan Driver"
1966+
depends on SURFACE_AGGREGATOR
1967+
help
1968+
Driver that provides monitoring of the fan on Surface Pro devices that
1969+
have a fan, like the Surface Pro 9.
1970+
1971+
This makes the fan's current speed accessible through the hwmon
1972+
system. It does not provide control over the fan, the firmware is
1973+
responsible for that, this driver merely provides monitoring.
1974+
1975+
Select M or Y here, if you want to be able to read the fan's speed.
1976+
19641977
config SENSORS_ADC128D818
19651978
tristate "Texas Instruments ADC128D818"
19661979
depends on I2C

drivers/hwmon/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ obj-$(CONFIG_SENSORS_SMSC47M1) += smsc47m1.o
198198
obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
199199
obj-$(CONFIG_SENSORS_SPARX5) += sparx5-temp.o
200200
obj-$(CONFIG_SENSORS_STTS751) += stts751.o
201+
obj-$(CONFIG_SENSORS_SURFACE_FAN)+= surface_fan.o
201202
obj-$(CONFIG_SENSORS_SY7636A) += sy7636a-hwmon.o
202203
obj-$(CONFIG_SENSORS_AMC6821) += amc6821.o
203204
obj-$(CONFIG_SENSORS_TC74) += tc74.o

drivers/hwmon/surface_fan.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* Surface Fan driver for Surface System Aggregator Module. It provides access
4+
* to the fan's rpm through the hwmon system.
5+
*
6+
* Copyright (C) 2023 Ivor Wanders <ivor@iwanders.net>
7+
*/
8+
9+
#include <linux/hwmon.h>
10+
#include <linux/kernel.h>
11+
#include <linux/module.h>
12+
#include <linux/surface_aggregator/device.h>
13+
#include <linux/types.h>
14+
15+
// SSAM
16+
SSAM_DEFINE_SYNC_REQUEST_CL_R(__ssam_fan_rpm_get, __le16, {
17+
.target_category = SSAM_SSH_TC_FAN,
18+
.command_id = 0x01,
19+
});
20+
21+
// hwmon
22+
umode_t surface_fan_hwmon_is_visible(const void *drvdata,
23+
enum hwmon_sensor_types type, u32 attr,
24+
int channel)
25+
{
26+
return 0444;
27+
}
28+
29+
static int surface_fan_hwmon_read(struct device *dev,
30+
enum hwmon_sensor_types type, u32 attr,
31+
int channel, long *val)
32+
{
33+
struct ssam_device *sdev = dev_get_drvdata(dev);
34+
int ret;
35+
__le16 value;
36+
37+
ret = __ssam_fan_rpm_get(sdev, &value);
38+
if (ret)
39+
return ret;
40+
41+
*val = le16_to_cpu(value);
42+
43+
return ret;
44+
}
45+
46+
static const struct hwmon_channel_info *const surface_fan_info[] = {
47+
HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT),
48+
NULL
49+
};
50+
51+
static const struct hwmon_ops surface_fan_hwmon_ops = {
52+
.is_visible = surface_fan_hwmon_is_visible,
53+
.read = surface_fan_hwmon_read,
54+
};
55+
56+
static const struct hwmon_chip_info surface_fan_chip_info = {
57+
.ops = &surface_fan_hwmon_ops,
58+
.info = surface_fan_info,
59+
};
60+
61+
static int surface_fan_probe(struct ssam_device *sdev)
62+
{
63+
struct device *hdev;
64+
65+
hdev = devm_hwmon_device_register_with_info(&sdev->dev,
66+
"surface_fan", sdev,
67+
&surface_fan_chip_info,
68+
NULL);
69+
if (IS_ERR(hdev))
70+
return PTR_ERR(hdev);
71+
72+
return 0;
73+
}
74+
75+
static const struct ssam_device_id ssam_fan_match[] = {
76+
{ SSAM_SDEV(FAN, SAM, 0x01, 0x01) },
77+
{},
78+
};
79+
MODULE_DEVICE_TABLE(ssam, ssam_fan_match);
80+
81+
static struct ssam_device_driver surface_fan = {
82+
.probe = surface_fan_probe,
83+
.match_table = ssam_fan_match,
84+
.driver = {
85+
.name = "surface_fan",
86+
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
87+
},
88+
};
89+
module_ssam_device_driver(surface_fan);
90+
91+
MODULE_AUTHOR("Ivor Wanders <ivor@iwanders.net>");
92+
MODULE_DESCRIPTION("Fan Driver for Surface System Aggregator Module");
93+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)