Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 128 additions & 2 deletions drivers/hwmon/surface_temp.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
#include <linux/bitops.h>
#include <linux/hwmon.h>
#include <linux/kernel.h>
#include <linux/math.h>
#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/types.h>

#include <linux/surface_aggregator/controller.h>
Expand All @@ -29,6 +32,14 @@
*/
#define SSAM_TMP_SENSOR_NAME_LENGTH 18

/*
* Temperature offset limits in millidegrees Celsius and the 0-based
* channel index for sensor 9, which corresponds to I_VTS1.
*/
#define SSAM_TMP_SENSOR_OFFSET_CHANNEL 8
#define SSAM_TMP_OFFSET_MIN (-5000L)
#define SSAM_TMP_OFFSET_MAX 5000L

struct ssam_tmp_get_name_rsp {
__le16 unknown1;
char unknown2;
Expand All @@ -37,6 +48,13 @@ struct ssam_tmp_get_name_rsp {

static_assert(sizeof(struct ssam_tmp_get_name_rsp) == 21);

struct ssam_tmp_set_offset_req {
__u8 iid;
__le32 offset_f32;
} __packed;

static_assert(sizeof(struct ssam_tmp_set_offset_req) == 5);

SSAM_DEFINE_SYNC_REQUEST_CL_R(__ssam_tmp_get_available_sensors, __le16, {
.target_category = SSAM_SSH_TC_TMP,
.command_id = 0x04,
Expand All @@ -52,6 +70,11 @@ SSAM_DEFINE_SYNC_REQUEST_MD_R(__ssam_tmp_get_name, struct ssam_tmp_get_name_rsp,
.command_id = 0x0e,
});

SSAM_DEFINE_SYNC_REQUEST_MD_W(__ssam_tmp_set_offset, struct ssam_tmp_set_offset_req, {
.target_category = SSAM_SSH_TC_TMP,
.command_id = 0x24,
});

static int ssam_tmp_get_available_sensors(struct ssam_device *sdev, s16 *sensors)
{
__le16 sensors_le;
Expand Down Expand Up @@ -106,12 +129,52 @@ static int ssam_tmp_get_name(struct ssam_device *sdev, u8 iid, char *buf, size_t
return 0;
}

static u32 ssam_tmp_mc_to_f32(long mc)
{
u32 sign = 0;
u64 scaled;
u32 mant;
int shift;
int msb;

if (mc == 0)
return 0;

if (mc < 0) {
sign = BIT(31);
mc = -mc;
}

scaled = DIV_ROUND_CLOSEST_ULL((u64)mc << 34, 1000);
msb = fls64(scaled) - 1;
shift = msb - 23;
mant = (scaled + BIT_ULL(shift - 1)) >> shift;
if (mant >= BIT(24)) {
mant >>= 1;
msb++;
}

return sign | ((127 + msb - 34) << 23) | (mant & GENMASK(22, 0));
}

static int ssam_tmp_set_offset(struct ssam_device *sdev, u8 iid, long offset_mc)
{
struct ssam_tmp_set_offset_req req = {
.iid = iid,
.offset_f32 = cpu_to_le32(ssam_tmp_mc_to_f32(offset_mc)),
};

return __ssam_tmp_set_offset(sdev->ctrl, sdev->uid.target, iid, &req);
}

/* -- Driver.---------------------------------------------------------------- */

struct ssam_temp {
struct ssam_device *sdev;
s16 sensors;
char names[SSAM_TMP_SENSOR_MAX_COUNT][SSAM_TMP_SENSOR_NAME_LENGTH];
bool has_offset;
long offset;
};

static umode_t ssam_temp_hwmon_is_visible(const void *data,
Expand All @@ -123,6 +186,9 @@ static umode_t ssam_temp_hwmon_is_visible(const void *data,
if (!(ssam_temp->sensors & BIT(channel)))
return 0;

if (type == hwmon_temp && attr == hwmon_temp_offset)
return ssam_temp->has_offset ? 0644 : 0;

return 0444;
}

Expand All @@ -132,7 +198,35 @@ static int ssam_temp_hwmon_read(struct device *dev,
{
const struct ssam_temp *ssam_temp = dev_get_drvdata(dev);

return ssam_tmp_get_temperature(ssam_temp->sdev, channel + 1, value);
switch (attr) {
case hwmon_temp_input:
return ssam_tmp_get_temperature(ssam_temp->sdev, channel + 1, value);
case hwmon_temp_offset:
*value = ssam_temp->offset;
return 0;
default:
return -EOPNOTSUPP;
}
}

static int ssam_temp_hwmon_write(struct device *dev,
enum hwmon_sensor_types type,
u32 attr, int channel, long value)
{
struct ssam_temp *ssam_temp = dev_get_drvdata(dev);
int status;

if (type != hwmon_temp || attr != hwmon_temp_offset ||
channel != SSAM_TMP_SENSOR_OFFSET_CHANNEL || !ssam_temp->has_offset)
return -EOPNOTSUPP;

value = clamp_val(value, SSAM_TMP_OFFSET_MIN, SSAM_TMP_OFFSET_MAX);

status = ssam_tmp_set_offset(ssam_temp->sdev, channel + 1, value);
if (!status)
ssam_temp->offset = value;

return status;
}

static int ssam_temp_hwmon_read_string(struct device *dev,
Expand All @@ -157,7 +251,7 @@ static const struct hwmon_channel_info * const ssam_temp_hwmon_info[] = {
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_OFFSET,
HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_INPUT | HWMON_T_LABEL,
Expand All @@ -171,6 +265,7 @@ static const struct hwmon_channel_info * const ssam_temp_hwmon_info[] = {
static const struct hwmon_ops ssam_temp_hwmon_ops = {
.is_visible = ssam_temp_hwmon_is_visible,
.read = ssam_temp_hwmon_read,
.write = ssam_temp_hwmon_write,
.read_string = ssam_temp_hwmon_read_string,
};

Expand All @@ -179,6 +274,13 @@ static const struct hwmon_chip_info ssam_temp_hwmon_chip_info = {
.info = ssam_temp_hwmon_info,
};

static void ssam_temp_reset_offset(void *data)
{
struct ssam_temp *ssam_temp = data;

ssam_tmp_set_offset(ssam_temp->sdev, SSAM_TMP_SENSOR_OFFSET_CHANNEL + 1, 0);
}

static int ssam_temp_probe(struct ssam_device *sdev)
{
struct ssam_temp *ssam_temp;
Expand All @@ -197,6 +299,7 @@ static int ssam_temp_probe(struct ssam_device *sdev)

ssam_temp->sdev = sdev;
ssam_temp->sensors = sensors;
ssam_device_set_drvdata(sdev, ssam_temp);

/* Retrieve the name for each available sensor. */
for (channel = 0; channel < SSAM_TMP_SENSOR_MAX_COUNT; channel++) {
Expand All @@ -209,11 +312,33 @@ static int ssam_temp_probe(struct ssam_device *sdev)
return status;
}

if (device_property_read_bool(&sdev->dev, "has_offset") &&
(sensors & BIT(SSAM_TMP_SENSOR_OFFSET_CHANNEL))) {
status = ssam_tmp_set_offset(sdev, SSAM_TMP_SENSOR_OFFSET_CHANNEL + 1, 0);
if (status)
return status;

ssam_temp->has_offset = true;

status = devm_add_action_or_reset(&sdev->dev, ssam_temp_reset_offset, ssam_temp);
if (status)
return status;
}

hwmon_dev = devm_hwmon_device_register_with_info(&sdev->dev, "surface_thermal", ssam_temp,
&ssam_temp_hwmon_chip_info, NULL);
return PTR_ERR_OR_ZERO(hwmon_dev);
}

static void ssam_temp_shutdown(struct device *dev)
{
struct ssam_temp *ssam_temp = dev_get_drvdata(dev);

if (ssam_temp && ssam_temp->has_offset)
ssam_tmp_set_offset(ssam_temp->sdev,
SSAM_TMP_SENSOR_OFFSET_CHANNEL + 1, 0);
}

static const struct ssam_device_id ssam_temp_match[] = {
{ SSAM_SDEV(TMP, SAM, 0x00, 0x02) },
{ SSAM_SDEV(TMP, KIP, 0x00, 0x02) },
Expand All @@ -226,6 +351,7 @@ static struct ssam_device_driver ssam_temp = {
.match_table = ssam_temp_match,
.driver = {
.name = "surface_temp",
.shutdown = ssam_temp_shutdown,
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
},
};
Expand Down
31 changes: 28 additions & 3 deletions drivers/platform/surface/surface_aggregator_registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ static const struct software_node ssam_node_tmp_sensors = {
.parent = &ssam_node_root,
};

/* Thermal sensors with sensor 9 temperature offset support. */
static const struct property_entry ssam_node_tmp_sensors_has_offset[] = {
PROPERTY_ENTRY_BOOL("has_offset"),
{ }
};

static const struct software_node ssam_node_tmp_sensors_with_offset = {
.name = "ssam:01:03:01:00:02",
.parent = &ssam_node_root,
.properties = ssam_node_tmp_sensors_has_offset,
};

/* Thermal sensors of the Surface Book 3 base (KIP). */
static const struct software_node ssam_node_tmp_sensors_sb3base = {
.name = "ssam:01:03:02:00:02",
Expand Down Expand Up @@ -393,12 +405,25 @@ static const struct software_node *ssam_node_group_sls2[] = {
NULL,
};

/* Devices for Surface Laptop Go. */
/* Devices for Surface Laptop Go 1. */
static const struct software_node *ssam_node_group_slg1[] = {
&ssam_node_root,
&ssam_node_bat_ac_dptf,
&ssam_node_bat_main_dptf,
&ssam_node_tmp_perf_profile,
&ssam_node_tmp_sensors_with_offset,
&ssam_node_fan_speed,
NULL,
};

/* Devices for Surface Laptop Go 2 and 3. */
static const struct software_node *ssam_node_group_slg2[] = {
&ssam_node_root,
&ssam_node_bat_ac_dptf,
&ssam_node_bat_main_dptf,
&ssam_node_tmp_perf_profile,
&ssam_node_tmp_sensors,
&ssam_node_fan_speed,
NULL,
};

Expand Down Expand Up @@ -584,10 +609,10 @@ static const struct acpi_device_id ssam_platform_hub_acpi_match[] = {
{ "MSHW0118", (unsigned long)ssam_node_group_slg1 },

/* Surface Laptop Go 2 */
{ "MSHW0290", (unsigned long)ssam_node_group_slg1 },
{ "MSHW0290", (unsigned long)ssam_node_group_slg2 },

/* Surface Laptop Go 3 */
{ "MSHW0440", (unsigned long)ssam_node_group_slg1 },
{ "MSHW0440", (unsigned long)ssam_node_group_slg2 },

/* Surface Laptop Studio 1 */
{ "MSHW0123", (unsigned long)ssam_node_group_sls1 },
Expand Down