Skip to content

Commit 63be321

Browse files
qzedgroeck
authored andcommitted
hwmon: Add thermal sensor driver for Surface Aggregator Module
Some of the newer Microsoft Surface devices (such as the Surface Book 3 and Pro 9) have thermal sensors connected via the Surface Aggregator Module (the embedded controller on those devices). Add a basic driver to read out the temperature values of those sensors. The EC can have up to 16 thermal sensors connected via a single sub-device, each providing temperature readings and a label string. Link: linux-surface/surface-aggregator-module#59 Reviewed-by: Hans de Goede <hdegoede@redhat.com> Co-developed-by: Ivor Wanders <ivor@iwanders.net> Signed-off-by: Ivor Wanders <ivor@iwanders.net> Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com> Message-ID: <20240811001503.753728-1-luzmaximilian@gmail.com> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent 1f432e4 commit 63be321

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed

MAINTAINERS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15200,6 +15200,12 @@ S: Maintained
1520015200
F: Documentation/hwmon/surface_fan.rst
1520115201
F: drivers/hwmon/surface_fan.c
1520215202

15203+
MICROSOFT SURFACE SENSOR THERMAL DRIVER
15204+
M: Maximilian Luz <luzmaximilian@gmail.com>
15205+
L: linux-hwmon@vger.kernel.org
15206+
S: Maintained
15207+
F: drivers/hwmon/surface_temp.c
15208+
1520315209
MICROSOFT SURFACE GPE LID SUPPORT DRIVER
1520415210
M: Maximilian Luz <luzmaximilian@gmail.com>
1520515211
L: platform-driver-x86@vger.kernel.org

drivers/hwmon/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,17 @@ config SENSORS_SURFACE_FAN
20822082

20832083
Select M or Y here, if you want to be able to read the fan's speed.
20842084

2085+
config SENSORS_SURFACE_TEMP
2086+
tristate "Microsoft Surface Thermal Sensor Driver"
2087+
depends on SURFACE_AGGREGATOR
2088+
depends on SURFACE_AGGREGATOR_BUS
2089+
help
2090+
Driver for monitoring thermal sensors connected via the Surface
2091+
Aggregator Module (embedded controller) on Microsoft Surface devices.
2092+
2093+
This driver can also be built as a module. If so, the module
2094+
will be called surface_temp.
2095+
20852096
config SENSORS_ADC128D818
20862097
tristate "Texas Instruments ADC128D818"
20872098
depends on I2C

drivers/hwmon/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ obj-$(CONFIG_SENSORS_SPARX5) += sparx5-temp.o
209209
obj-$(CONFIG_SENSORS_SPD5118) += spd5118.o
210210
obj-$(CONFIG_SENSORS_STTS751) += stts751.o
211211
obj-$(CONFIG_SENSORS_SURFACE_FAN)+= surface_fan.o
212+
obj-$(CONFIG_SENSORS_SURFACE_TEMP)+= surface_temp.o
212213
obj-$(CONFIG_SENSORS_SY7636A) += sy7636a-hwmon.o
213214
obj-$(CONFIG_SENSORS_AMC6821) += amc6821.o
214215
obj-$(CONFIG_SENSORS_TC74) += tc74.o

drivers/hwmon/surface_temp.c

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* Thermal sensor subsystem driver for Surface System Aggregator Module (SSAM).
4+
*
5+
* Copyright (C) 2022-2023 Maximilian Luz <luzmaximilian@gmail.com>
6+
*/
7+
8+
#include <linux/bitops.h>
9+
#include <linux/hwmon.h>
10+
#include <linux/kernel.h>
11+
#include <linux/module.h>
12+
#include <linux/types.h>
13+
14+
#include <linux/surface_aggregator/controller.h>
15+
#include <linux/surface_aggregator/device.h>
16+
17+
/* -- SAM interface. -------------------------------------------------------- */
18+
19+
/*
20+
* Available sensors are indicated by a 16-bit bitfield, where a 1 marks the
21+
* presence of a sensor. So we have at most 16 possible sensors/channels.
22+
*/
23+
#define SSAM_TMP_SENSOR_MAX_COUNT 16
24+
25+
/*
26+
* All names observed so far are 6 characters long, but there's only
27+
* zeros after the name, so perhaps they can be longer. This number reflects
28+
* the maximum zero-padded space observed in the returned buffer.
29+
*/
30+
#define SSAM_TMP_SENSOR_NAME_LENGTH 18
31+
32+
struct ssam_tmp_get_name_rsp {
33+
__le16 unknown1;
34+
char unknown2;
35+
char name[SSAM_TMP_SENSOR_NAME_LENGTH];
36+
} __packed;
37+
38+
static_assert(sizeof(struct ssam_tmp_get_name_rsp) == 21);
39+
40+
SSAM_DEFINE_SYNC_REQUEST_CL_R(__ssam_tmp_get_available_sensors, __le16, {
41+
.target_category = SSAM_SSH_TC_TMP,
42+
.command_id = 0x04,
43+
});
44+
45+
SSAM_DEFINE_SYNC_REQUEST_MD_R(__ssam_tmp_get_temperature, __le16, {
46+
.target_category = SSAM_SSH_TC_TMP,
47+
.command_id = 0x01,
48+
});
49+
50+
SSAM_DEFINE_SYNC_REQUEST_MD_R(__ssam_tmp_get_name, struct ssam_tmp_get_name_rsp, {
51+
.target_category = SSAM_SSH_TC_TMP,
52+
.command_id = 0x0e,
53+
});
54+
55+
static int ssam_tmp_get_available_sensors(struct ssam_device *sdev, s16 *sensors)
56+
{
57+
__le16 sensors_le;
58+
int status;
59+
60+
status = __ssam_tmp_get_available_sensors(sdev, &sensors_le);
61+
if (status)
62+
return status;
63+
64+
*sensors = le16_to_cpu(sensors_le);
65+
return 0;
66+
}
67+
68+
static int ssam_tmp_get_temperature(struct ssam_device *sdev, u8 iid, long *temperature)
69+
{
70+
__le16 temp_le;
71+
int status;
72+
73+
status = __ssam_tmp_get_temperature(sdev->ctrl, sdev->uid.target, iid, &temp_le);
74+
if (status)
75+
return status;
76+
77+
/* Convert 1/10 °K to 1/1000 °C */
78+
*temperature = (le16_to_cpu(temp_le) - 2731) * 100L;
79+
return 0;
80+
}
81+
82+
static int ssam_tmp_get_name(struct ssam_device *sdev, u8 iid, char *buf, size_t buf_len)
83+
{
84+
struct ssam_tmp_get_name_rsp name_rsp;
85+
int status;
86+
87+
status = __ssam_tmp_get_name(sdev->ctrl, sdev->uid.target, iid, &name_rsp);
88+
if (status)
89+
return status;
90+
91+
/*
92+
* This should not fail unless the name in the returned struct is not
93+
* null-terminated or someone changed something in the struct
94+
* definitions above, since our buffer and struct have the same
95+
* capacity by design. So if this fails, log an error message. Since
96+
* the more likely cause is that the returned string isn't
97+
* null-terminated, we might have received garbage (as opposed to just
98+
* an incomplete string), so also fail the function.
99+
*/
100+
status = strscpy(buf, name_rsp.name, buf_len);
101+
if (status < 0) {
102+
dev_err(&sdev->dev, "received non-null-terminated sensor name string\n");
103+
return status;
104+
}
105+
106+
return 0;
107+
}
108+
109+
/* -- Driver.---------------------------------------------------------------- */
110+
111+
struct ssam_temp {
112+
struct ssam_device *sdev;
113+
s16 sensors;
114+
char names[SSAM_TMP_SENSOR_MAX_COUNT][SSAM_TMP_SENSOR_NAME_LENGTH];
115+
};
116+
117+
static umode_t ssam_temp_hwmon_is_visible(const void *data,
118+
enum hwmon_sensor_types type,
119+
u32 attr, int channel)
120+
{
121+
const struct ssam_temp *ssam_temp = data;
122+
123+
if (!(ssam_temp->sensors & BIT(channel)))
124+
return 0;
125+
126+
return 0444;
127+
}
128+
129+
static int ssam_temp_hwmon_read(struct device *dev,
130+
enum hwmon_sensor_types type,
131+
u32 attr, int channel, long *value)
132+
{
133+
const struct ssam_temp *ssam_temp = dev_get_drvdata(dev);
134+
135+
return ssam_tmp_get_temperature(ssam_temp->sdev, channel + 1, value);
136+
}
137+
138+
static int ssam_temp_hwmon_read_string(struct device *dev,
139+
enum hwmon_sensor_types type,
140+
u32 attr, int channel, const char **str)
141+
{
142+
const struct ssam_temp *ssam_temp = dev_get_drvdata(dev);
143+
144+
*str = ssam_temp->names[channel];
145+
return 0;
146+
}
147+
148+
static const struct hwmon_channel_info * const ssam_temp_hwmon_info[] = {
149+
HWMON_CHANNEL_INFO(chip,
150+
HWMON_C_REGISTER_TZ),
151+
HWMON_CHANNEL_INFO(temp,
152+
HWMON_T_INPUT | HWMON_T_LABEL,
153+
HWMON_T_INPUT | HWMON_T_LABEL,
154+
HWMON_T_INPUT | HWMON_T_LABEL,
155+
HWMON_T_INPUT | HWMON_T_LABEL,
156+
HWMON_T_INPUT | HWMON_T_LABEL,
157+
HWMON_T_INPUT | HWMON_T_LABEL,
158+
HWMON_T_INPUT | HWMON_T_LABEL,
159+
HWMON_T_INPUT | HWMON_T_LABEL,
160+
HWMON_T_INPUT | HWMON_T_LABEL,
161+
HWMON_T_INPUT | HWMON_T_LABEL,
162+
HWMON_T_INPUT | HWMON_T_LABEL,
163+
HWMON_T_INPUT | HWMON_T_LABEL,
164+
HWMON_T_INPUT | HWMON_T_LABEL,
165+
HWMON_T_INPUT | HWMON_T_LABEL,
166+
HWMON_T_INPUT | HWMON_T_LABEL,
167+
HWMON_T_INPUT | HWMON_T_LABEL),
168+
NULL
169+
};
170+
171+
static const struct hwmon_ops ssam_temp_hwmon_ops = {
172+
.is_visible = ssam_temp_hwmon_is_visible,
173+
.read = ssam_temp_hwmon_read,
174+
.read_string = ssam_temp_hwmon_read_string,
175+
};
176+
177+
static const struct hwmon_chip_info ssam_temp_hwmon_chip_info = {
178+
.ops = &ssam_temp_hwmon_ops,
179+
.info = ssam_temp_hwmon_info,
180+
};
181+
182+
static int ssam_temp_probe(struct ssam_device *sdev)
183+
{
184+
struct ssam_temp *ssam_temp;
185+
struct device *hwmon_dev;
186+
s16 sensors;
187+
int channel;
188+
int status;
189+
190+
status = ssam_tmp_get_available_sensors(sdev, &sensors);
191+
if (status)
192+
return status;
193+
194+
ssam_temp = devm_kzalloc(&sdev->dev, sizeof(*ssam_temp), GFP_KERNEL);
195+
if (!ssam_temp)
196+
return -ENOMEM;
197+
198+
ssam_temp->sdev = sdev;
199+
ssam_temp->sensors = sensors;
200+
201+
/* Retrieve the name for each available sensor. */
202+
for (channel = 0; channel < SSAM_TMP_SENSOR_MAX_COUNT; channel++) {
203+
if (!(sensors & BIT(channel)))
204+
continue;
205+
206+
status = ssam_tmp_get_name(sdev, channel + 1, ssam_temp->names[channel],
207+
SSAM_TMP_SENSOR_NAME_LENGTH);
208+
if (status)
209+
return status;
210+
}
211+
212+
hwmon_dev = devm_hwmon_device_register_with_info(&sdev->dev, "surface_thermal", ssam_temp,
213+
&ssam_temp_hwmon_chip_info, NULL);
214+
return PTR_ERR_OR_ZERO(hwmon_dev);
215+
}
216+
217+
static const struct ssam_device_id ssam_temp_match[] = {
218+
{ SSAM_SDEV(TMP, SAM, 0x00, 0x02) },
219+
{ },
220+
};
221+
MODULE_DEVICE_TABLE(ssam, ssam_temp_match);
222+
223+
static struct ssam_device_driver ssam_temp = {
224+
.probe = ssam_temp_probe,
225+
.match_table = ssam_temp_match,
226+
.driver = {
227+
.name = "surface_temp",
228+
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
229+
},
230+
};
231+
module_ssam_device_driver(ssam_temp);
232+
233+
MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
234+
MODULE_DESCRIPTION("Thermal sensor subsystem driver for Surface System Aggregator Module");
235+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)