123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- From 3e6733db885b3a91339039897cbf0f3e05cda2a5 Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Sat, 30 Dec 2023 18:07:54 +0100
- Subject: [PATCH] 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.
- Link: https://github.com/linux-surface/surface-aggregator-module/issues/59
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Patchset: surface-sam
- ---
- drivers/hwmon/Kconfig | 10 +++
- drivers/hwmon/Makefile | 1 +
- drivers/hwmon/surface_temp.c | 165 +++++++++++++++++++++++++++++++++++
- 3 files changed, 176 insertions(+)
- create mode 100644 drivers/hwmon/surface_temp.c
- diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
- index e14ae18a973b..76eabe3e4435 100644
- --- a/drivers/hwmon/Kconfig
- +++ b/drivers/hwmon/Kconfig
- @@ -2093,6 +2093,16 @@ config SENSORS_SURFACE_FAN
-
- Select M or Y here, if you want to be able to read the fan's speed.
-
- +config SENSORS_SURFACE_TEMP
- + tristate "Microsoft Surface Thermal Sensor Driver"
- + depends on SURFACE_AGGREGATOR
- + help
- + Driver for monitoring thermal sensors connected via the Surface
- + Aggregator Module (embedded controller) on Microsoft Surface devices.
- +
- + This driver can also be built as a module. If so, the module
- + will be called surface_temp.
- +
- config SENSORS_ADC128D818
- tristate "Texas Instruments ADC128D818"
- depends on I2C
- diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
- index e3f25475d1f0..eff74ab7f720 100644
- --- a/drivers/hwmon/Makefile
- +++ b/drivers/hwmon/Makefile
- @@ -209,6 +209,7 @@ 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_SURFACE_TEMP)+= surface_temp.o
- obj-$(CONFIG_SENSORS_SY7636A) += sy7636a-hwmon.o
- obj-$(CONFIG_SENSORS_AMC6821) += amc6821.o
- obj-$(CONFIG_SENSORS_TC74) += tc74.o
- diff --git a/drivers/hwmon/surface_temp.c b/drivers/hwmon/surface_temp.c
- new file mode 100644
- index 000000000000..48c3e826713f
- --- /dev/null
- +++ b/drivers/hwmon/surface_temp.c
- @@ -0,0 +1,165 @@
- +// SPDX-License-Identifier: GPL-2.0+
- +/*
- + * Thermal sensor subsystem driver for Surface System Aggregator Module (SSAM).
- + *
- + * Copyright (C) 2022-2023 Maximilian Luz <luzmaximilian@gmail.com>
- + */
- +
- +#include <linux/bitops.h>
- +#include <linux/hwmon.h>
- +#include <linux/kernel.h>
- +#include <linux/module.h>
- +#include <linux/types.h>
- +
- +#include <linux/surface_aggregator/controller.h>
- +#include <linux/surface_aggregator/device.h>
- +
- +
- +/* -- SAM interface. -------------------------------------------------------- */
- +
- +SSAM_DEFINE_SYNC_REQUEST_CL_R(__ssam_tmp_get_available_sensors, __le16, {
- + .target_category = SSAM_SSH_TC_TMP,
- + .command_id = 0x04,
- +});
- +
- +SSAM_DEFINE_SYNC_REQUEST_MD_R(__ssam_tmp_get_temperature, __le16, {
- + .target_category = SSAM_SSH_TC_TMP,
- + .command_id = 0x01,
- +});
- +
- +static int ssam_tmp_get_available_sensors(struct ssam_device *sdev, s16 *sensors)
- +{
- + __le16 sensors_le;
- + int status;
- +
- + status = __ssam_tmp_get_available_sensors(sdev, &sensors_le);
- + if (status)
- + return status;
- +
- + *sensors = le16_to_cpu(sensors_le);
- + return 0;
- +}
- +
- +static int ssam_tmp_get_temperature(struct ssam_device *sdev, u8 iid, long *temperature)
- +{
- + __le16 temp_le;
- + int status;
- +
- + status = __ssam_tmp_get_temperature(sdev->ctrl, sdev->uid.target, iid, &temp_le);
- + if (status)
- + return status;
- +
- + /* Convert 1/10 °K to 1/1000 °C */
- + *temperature = (le16_to_cpu(temp_le) - 2731) * 100L;
- + return 0;
- +}
- +
- +
- +/* -- Driver.---------------------------------------------------------------- */
- +
- +struct ssam_temp {
- + struct ssam_device *sdev;
- + s16 sensors;
- +};
- +
- +static umode_t ssam_temp_hwmon_is_visible(const void *data,
- + enum hwmon_sensor_types type,
- + u32 attr, int channel)
- +{
- + const struct ssam_temp *ssam_temp = data;
- +
- + if (!(ssam_temp->sensors & BIT(channel)))
- + return 0;
- +
- + return 0444;
- +}
- +
- +static int ssam_temp_hwmon_read(struct device *dev,
- + enum hwmon_sensor_types type,
- + u32 attr, int channel, long *value)
- +{
- + const struct ssam_temp *ssam_temp = dev_get_drvdata(dev);
- +
- + return ssam_tmp_get_temperature(ssam_temp->sdev, channel + 1, value);
- +}
- +
- +static const struct hwmon_channel_info * const ssam_temp_hwmon_info[] = {
- + HWMON_CHANNEL_INFO(chip,
- + HWMON_C_REGISTER_TZ),
- + /* We have at most 16 thermal sensor channels. */
- + HWMON_CHANNEL_INFO(temp,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT,
- + HWMON_T_INPUT),
- + NULL
- +};
- +
- +static const struct hwmon_ops ssam_temp_hwmon_ops = {
- + .is_visible = ssam_temp_hwmon_is_visible,
- + .read = ssam_temp_hwmon_read,
- +};
- +
- +static const struct hwmon_chip_info ssam_temp_hwmon_chip_info = {
- + .ops = &ssam_temp_hwmon_ops,
- + .info = ssam_temp_hwmon_info,
- +};
- +
- +static int ssam_temp_probe(struct ssam_device *sdev)
- +{
- + struct ssam_temp *ssam_temp;
- + struct device *hwmon_dev;
- + s16 sensors;
- + int status;
- +
- + status = ssam_tmp_get_available_sensors(sdev, &sensors);
- + if (status)
- + return status;
- +
- + ssam_temp = devm_kzalloc(&sdev->dev, sizeof(*ssam_temp), GFP_KERNEL);
- + if (!ssam_temp)
- + return -ENOMEM;
- +
- + ssam_temp->sdev = sdev;
- + ssam_temp->sensors = sensors;
- +
- + hwmon_dev = devm_hwmon_device_register_with_info(&sdev->dev,
- + "surface_thermal", ssam_temp, &ssam_temp_hwmon_chip_info,
- + NULL);
- + if (IS_ERR(hwmon_dev))
- + return PTR_ERR(hwmon_dev);
- +
- + return 0;
- +}
- +
- +static const struct ssam_device_id ssam_temp_match[] = {
- + { SSAM_SDEV(TMP, SAM, 0x00, 0x02) },
- + { },
- +};
- +MODULE_DEVICE_TABLE(ssam, ssam_temp_match);
- +
- +static struct ssam_device_driver ssam_temp = {
- + .probe = ssam_temp_probe,
- + .match_table = ssam_temp_match,
- + .driver = {
- + .name = "surface_temp",
- + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- + },
- +};
- +module_ssam_device_driver(ssam_temp);
- +
- +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
- +MODULE_DESCRIPTION("Thermal sensor subsystem driver for Surface System Aggregator Module");
- +MODULE_LICENSE("GPL");
- --
- 2.46.1
- From 3e4479ac556ff21b45485822edb0980bbb27fdba Mon Sep 17 00:00:00 2001
- From: Maximilian Luz <luzmaximilian@gmail.com>
- Date: Sat, 30 Dec 2023 18:12:23 +0100
- Subject: [PATCH] hwmon: surface_temp: Add support for sensor names
- The thermal subsystem of the Surface Aggregator Module allows us to
- query the names of the respective thermal sensors. Forward those to
- userspace.
- Signed-off-by: Ivor Wanders <ivor@iwanders.net>
- Co-Developed-by: Maximilian Luz <luzmaximilian@gmail.com>
- Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
- Patchset: surface-sam
- ---
- drivers/hwmon/surface_temp.c | 113 +++++++++++++++++++++++++++++------
- 1 file changed, 96 insertions(+), 17 deletions(-)
- diff --git a/drivers/hwmon/surface_temp.c b/drivers/hwmon/surface_temp.c
- index 48c3e826713f..4c08926139db 100644
- --- a/drivers/hwmon/surface_temp.c
- +++ b/drivers/hwmon/surface_temp.c
- @@ -17,6 +17,27 @@
-
- /* -- SAM interface. -------------------------------------------------------- */
-
- +/*
- + * Available sensors are indicated by a 16-bit bitfield, where a 1 marks the
- + * presence of a sensor. So we have at most 16 possible sensors/channels.
- + */
- +#define SSAM_TMP_SENSOR_MAX_COUNT 16
- +
- +/*
- + * All names observed so far are 6 characters long, but there's only
- + * zeros after the name, so perhaps they can be longer. This number reflects
- + * the maximum zero-padded space observed in the returned buffer.
- + */
- +#define SSAM_TMP_SENSOR_NAME_LENGTH 18
- +
- +struct ssam_tmp_get_name_rsp {
- + __le16 unknown1;
- + char unknown2;
- + char name[SSAM_TMP_SENSOR_NAME_LENGTH];
- +} __packed;
- +
- +static_assert(sizeof(struct ssam_tmp_get_name_rsp) == 21);
- +
- SSAM_DEFINE_SYNC_REQUEST_CL_R(__ssam_tmp_get_available_sensors, __le16, {
- .target_category = SSAM_SSH_TC_TMP,
- .command_id = 0x04,
- @@ -27,6 +48,11 @@ SSAM_DEFINE_SYNC_REQUEST_MD_R(__ssam_tmp_get_temperature, __le16, {
- .command_id = 0x01,
- });
-
- +SSAM_DEFINE_SYNC_REQUEST_MD_R(__ssam_tmp_get_name, struct ssam_tmp_get_name_rsp, {
- + .target_category = SSAM_SSH_TC_TMP,
- + .command_id = 0x0e,
- +});
- +
- static int ssam_tmp_get_available_sensors(struct ssam_device *sdev, s16 *sensors)
- {
- __le16 sensors_le;
- @@ -54,12 +80,37 @@ static int ssam_tmp_get_temperature(struct ssam_device *sdev, u8 iid, long *temp
- return 0;
- }
-
- +static int ssam_tmp_get_name(struct ssam_device *sdev, u8 iid, char *buf, size_t buf_len)
- +{
- + struct ssam_tmp_get_name_rsp name_rsp;
- + int status;
- +
- + status = __ssam_tmp_get_name(sdev->ctrl, sdev->uid.target, iid, &name_rsp);
- + if (status)
- + return status;
- +
- + /*
- + * This should not fail unless the name in the returned struct is not
- + * null-terminated or someone changed something in the struct
- + * definitions above, since our buffer and struct have the same
- + * capacity by design. So if this fails blow this up with a warning.
- + * Since the more likely cause is that the returned string isn't
- + * null-terminated, we might have received garbage (as opposed to just
- + * an incomplete string), so also fail the function.
- + */
- + status = strscpy(buf, name_rsp.name, buf_len);
- + WARN_ON(status < 0);
- +
- + return status < 0 ? status : 0;
- +}
- +
-
- /* -- Driver.---------------------------------------------------------------- */
-
- struct ssam_temp {
- struct ssam_device *sdev;
- s16 sensors;
- + char names[SSAM_TMP_SENSOR_MAX_COUNT][SSAM_TMP_SENSOR_NAME_LENGTH];
- };
-
- static umode_t ssam_temp_hwmon_is_visible(const void *data,
- @@ -83,33 +134,47 @@ static int ssam_temp_hwmon_read(struct device *dev,
- return ssam_tmp_get_temperature(ssam_temp->sdev, channel + 1, value);
- }
-
- +static int ssam_temp_hwmon_read_string(struct device *dev,
- + enum hwmon_sensor_types type,
- + u32 attr, int channel, const char **str)
- +{
- + const struct ssam_temp *ssam_temp = dev_get_drvdata(dev);
- +
- + *str = ssam_temp->names[channel];
- + return 0;
- +}
- +
- static const struct hwmon_channel_info * const ssam_temp_hwmon_info[] = {
- HWMON_CHANNEL_INFO(chip,
- HWMON_C_REGISTER_TZ),
- - /* We have at most 16 thermal sensor channels. */
- + /*
- + * We have at most SSAM_TMP_SENSOR_MAX_COUNT = 16 thermal sensor
- + * channels.
- + */
- HWMON_CHANNEL_INFO(temp,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT,
- - HWMON_T_INPUT),
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL,
- + HWMON_T_INPUT | HWMON_T_LABEL),
- NULL
- };
-
- static const struct hwmon_ops ssam_temp_hwmon_ops = {
- .is_visible = ssam_temp_hwmon_is_visible,
- .read = ssam_temp_hwmon_read,
- + .read_string = ssam_temp_hwmon_read_string,
- };
-
- static const struct hwmon_chip_info ssam_temp_hwmon_chip_info = {
- @@ -122,6 +187,7 @@ static int ssam_temp_probe(struct ssam_device *sdev)
- struct ssam_temp *ssam_temp;
- struct device *hwmon_dev;
- s16 sensors;
- + int channel;
- int status;
-
- status = ssam_tmp_get_available_sensors(sdev, &sensors);
- @@ -135,6 +201,19 @@ static int ssam_temp_probe(struct ssam_device *sdev)
- ssam_temp->sdev = sdev;
- ssam_temp->sensors = sensors;
-
- + /* Retrieve the name for each available sensor. */
- + for (channel = 0; channel < SSAM_TMP_SENSOR_MAX_COUNT; channel++)
- + {
- + if (!(sensors & BIT(channel)))
- + continue;
- +
- + status = ssam_tmp_get_name(sdev, channel + 1,
- + ssam_temp->names[channel],
- + SSAM_TMP_SENSOR_NAME_LENGTH);
- + if (status)
- + return status;
- + }
- +
- hwmon_dev = devm_hwmon_device_register_with_info(&sdev->dev,
- "surface_thermal", ssam_temp, &ssam_temp_hwmon_chip_info,
- NULL);
- --
- 2.46.1
|