1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HWMON driver for ASUS B550/X570 motherboards that publish sensor
4 * values via the embedded controller registers.
6 * Copyright (C) 2021 Eugene Shalygin <eugene.shalygin@gmail.com>
7 * Copyright (C) 2018-2019 Ed Brindley <kernel@maidavale.org>
10 * - Chipset temperature
12 * - Motherboard temperature
13 * - T_Sensor temperature
15 * - Water In temperature
16 * - Water Out temperature
17 * - CPU Optional Fan RPM
19 * - Water Flow Fan RPM
23 #include <linux/acpi.h>
24 #include <linux/dmi.h>
25 #include <linux/hwmon.h>
26 #include <linux/init.h>
27 #include <linux/jiffies.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/nls.h>
32 #include <linux/units.h>
33 #include <linux/wmi.h>
35 #include <asm/unaligned.h>
37 #define ASUSWMI_MONITORING_GUID "466747A0-70EC-11DE-8A39-0800200C9A66"
38 #define ASUSWMI_METHODID_BLOCK_READ_EC 0x42524543 /* BREC */
39 /* From the ASUS DSDT source */
40 #define ASUSWMI_BREC_REGISTERS_MAX 16
41 #define ASUSWMI_MAX_BUF_LEN 128
42 #define SENSOR_LABEL_LEN 16
44 static u32 hwmon_attributes[hwmon_max] = {
45 [hwmon_chip] = HWMON_C_REGISTER_TZ,
46 [hwmon_temp] = HWMON_T_INPUT | HWMON_T_LABEL,
47 [hwmon_in] = HWMON_I_INPUT | HWMON_I_LABEL,
48 [hwmon_curr] = HWMON_C_INPUT | HWMON_C_LABEL,
49 [hwmon_fan] = HWMON_F_INPUT | HWMON_F_LABEL,
52 struct asus_wmi_ec_sensor_address {
58 #define MAKE_SENSOR_ADDRESS(size_i, bank_i, index_i) { \
64 struct ec_sensor_info {
65 struct asus_wmi_ec_sensor_address addr;
66 char label[SENSOR_LABEL_LEN];
67 enum hwmon_sensor_types type;
70 #define EC_SENSOR(sensor_label, sensor_type, size, bank, index) { \
71 .addr = MAKE_SENSOR_ADDRESS(size, bank, index), \
72 .label = sensor_label, \
73 .type = sensor_type, \
76 enum known_ec_sensor {
85 SENSOR_FAN_WATER_FLOW,
88 SENSOR_TEMP_WATER_OUT,
92 /* All known sensors for ASUS EC controllers */
93 static const struct ec_sensor_info known_ec_sensors[] = {
94 [SENSOR_TEMP_CHIPSET] = EC_SENSOR("Chipset", hwmon_temp, 1, 0x00, 0x3a),
95 [SENSOR_TEMP_CPU] = EC_SENSOR("CPU", hwmon_temp, 1, 0x00, 0x3b),
96 [SENSOR_TEMP_MB] = EC_SENSOR("Motherboard", hwmon_temp, 1, 0x00, 0x3c),
97 [SENSOR_TEMP_T_SENSOR] = EC_SENSOR("T_Sensor", hwmon_temp, 1, 0x00, 0x3d),
98 [SENSOR_TEMP_VRM] = EC_SENSOR("VRM", hwmon_temp, 1, 0x00, 0x3e),
99 [SENSOR_FAN_CPU_OPT] = EC_SENSOR("CPU_Opt", hwmon_fan, 2, 0x00, 0xb0),
100 [SENSOR_FAN_VRM_HS] = EC_SENSOR("VRM HS", hwmon_fan, 2, 0x00, 0xb2),
101 [SENSOR_FAN_CHIPSET] = EC_SENSOR("Chipset", hwmon_fan, 2, 0x00, 0xb4),
102 [SENSOR_FAN_WATER_FLOW] = EC_SENSOR("Water_Flow", hwmon_fan, 2, 0x00, 0xbc),
103 [SENSOR_CURR_CPU] = EC_SENSOR("CPU", hwmon_curr, 1, 0x00, 0xf4),
104 [SENSOR_TEMP_WATER_IN] = EC_SENSOR("Water_In", hwmon_temp, 1, 0x01, 0x00),
105 [SENSOR_TEMP_WATER_OUT] = EC_SENSOR("Water_Out", hwmon_temp, 1, 0x01, 0x01),
108 struct asus_wmi_data {
109 const enum known_ec_sensor known_board_sensors[SENSOR_MAX + 1];
112 /* boards with EC support */
113 static struct asus_wmi_data sensors_board_PW_X570_P = {
114 .known_board_sensors = {
115 SENSOR_TEMP_CHIPSET, SENSOR_TEMP_CPU, SENSOR_TEMP_MB,
116 SENSOR_TEMP_T_SENSOR, SENSOR_TEMP_VRM,
122 static struct asus_wmi_data sensors_board_PW_X570_A = {
123 .known_board_sensors = {
124 SENSOR_TEMP_CHIPSET, SENSOR_TEMP_CPU, SENSOR_TEMP_MB, SENSOR_TEMP_VRM,
131 static struct asus_wmi_data sensors_board_R_C8H = {
132 .known_board_sensors = {
133 SENSOR_TEMP_CHIPSET, SENSOR_TEMP_CPU, SENSOR_TEMP_MB,
134 SENSOR_TEMP_T_SENSOR, SENSOR_TEMP_VRM,
135 SENSOR_TEMP_WATER_IN, SENSOR_TEMP_WATER_OUT,
136 SENSOR_FAN_CPU_OPT, SENSOR_FAN_CHIPSET, SENSOR_FAN_WATER_FLOW,
142 /* Same as Hero but without chipset fan */
143 static struct asus_wmi_data sensors_board_R_C8DH = {
144 .known_board_sensors = {
145 SENSOR_TEMP_CHIPSET, SENSOR_TEMP_CPU, SENSOR_TEMP_MB,
146 SENSOR_TEMP_T_SENSOR, SENSOR_TEMP_VRM,
147 SENSOR_TEMP_WATER_IN, SENSOR_TEMP_WATER_OUT,
148 SENSOR_FAN_CPU_OPT, SENSOR_FAN_WATER_FLOW,
154 /* Same as Hero but without water */
155 static struct asus_wmi_data sensors_board_R_C8F = {
156 .known_board_sensors = {
157 SENSOR_TEMP_CHIPSET, SENSOR_TEMP_CPU, SENSOR_TEMP_MB,
158 SENSOR_TEMP_T_SENSOR, SENSOR_TEMP_VRM,
159 SENSOR_FAN_CPU_OPT, SENSOR_FAN_CHIPSET,
165 static struct asus_wmi_data sensors_board_RS_B550_E_G = {
166 .known_board_sensors = {
167 SENSOR_TEMP_CHIPSET, SENSOR_TEMP_CPU, SENSOR_TEMP_MB,
168 SENSOR_TEMP_T_SENSOR, SENSOR_TEMP_VRM,
174 static struct asus_wmi_data sensors_board_RS_B550_I_G = {
175 .known_board_sensors = {
176 SENSOR_TEMP_CHIPSET, SENSOR_TEMP_CPU, SENSOR_TEMP_MB,
177 SENSOR_TEMP_T_SENSOR, SENSOR_TEMP_VRM,
184 static struct asus_wmi_data sensors_board_RS_X570_E_G = {
185 .known_board_sensors = {
186 SENSOR_TEMP_CHIPSET, SENSOR_TEMP_CPU, SENSOR_TEMP_MB,
187 SENSOR_TEMP_T_SENSOR, SENSOR_TEMP_VRM,
194 #define DMI_EXACT_MATCH_ASUS_BOARD_NAME(name, sensors) { \
196 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC."), \
197 DMI_EXACT_MATCH(DMI_BOARD_NAME, name), \
199 .driver_data = sensors, \
202 static const struct dmi_system_id asus_wmi_ec_dmi_table[] = {
203 DMI_EXACT_MATCH_ASUS_BOARD_NAME("PRIME X570-PRO", &sensors_board_PW_X570_P),
204 DMI_EXACT_MATCH_ASUS_BOARD_NAME("Pro WS X570-ACE", &sensors_board_PW_X570_A),
205 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG CROSSHAIR VIII DARK HERO", &sensors_board_R_C8DH),
206 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG CROSSHAIR VIII FORMULA", &sensors_board_R_C8F),
207 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG CROSSHAIR VIII HERO", &sensors_board_R_C8H),
208 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG STRIX B550-E GAMING", &sensors_board_RS_B550_E_G),
209 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG STRIX B550-I GAMING", &sensors_board_RS_B550_I_G),
210 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG STRIX X570-E GAMING", &sensors_board_RS_X570_E_G),
213 MODULE_DEVICE_TABLE(dmi, asus_wmi_ec_dmi_table);
216 enum known_ec_sensor info_index;
221 * struct asus_wmi_ec_info - sensor info.
222 * @sensors: list of sensors.
223 * @read_arg: UTF-16LE string to pass to BRxx() WMI function.
224 * @read_buffer: decoded output from WMI result.
225 * @nr_sensors: number of board EC sensors.
226 * @nr_registers: number of EC registers to read (sensor might span more than 1 register).
227 * @last_updated: in jiffies.
229 struct asus_wmi_ec_info {
230 struct ec_sensor sensors[SENSOR_MAX];
231 char read_arg[(ASUSWMI_BREC_REGISTERS_MAX * 4 + 1) * 2];
232 u8 read_buffer[ASUSWMI_BREC_REGISTERS_MAX];
233 unsigned int nr_sensors;
234 unsigned int nr_registers;
235 unsigned long last_updated;
238 struct asus_wmi_sensors {
239 struct asus_wmi_ec_info ec;
240 /* lock access to internal cache */
244 static int asus_wmi_ec_fill_board_sensors(struct asus_wmi_ec_info *ec,
245 const enum known_ec_sensor *bsi)
247 struct ec_sensor *s = ec->sensors;
251 ec->nr_registers = 0;
253 for (i = 0; bsi[i] != SENSOR_MAX; i++) {
254 s[i].info_index = bsi[i];
256 ec->nr_registers += known_ec_sensors[bsi[i]].addr.size;
263 * The next four functions convert to or from BRxx string argument format.
264 * The format of the string is as follows:
265 * - The string consists of two-byte UTF-16LE characters.
266 * - The value of the very first byte in the string is equal to the total
267 * length of the next string in bytes, thus excluding the first two-byte
269 * - The rest of the string encodes the pairs of (bank, index) pairs, where
270 * both values are byte-long (0x00 to 0xFF).
271 * - Numbers are encoded as UTF-16LE hex values.
273 static int asus_wmi_ec_decode_reply_buffer(const u8 *in, u32 length, u8 *out)
275 char buffer[ASUSWMI_MAX_BUF_LEN * 2];
276 u32 len = min_t(u32, get_unaligned_le16(in), length - 2);
278 utf16s_to_utf8s((wchar_t *)(in + 2), len / 2, UTF16_LITTLE_ENDIAN, buffer, sizeof(buffer));
280 return hex2bin(out, buffer, len / 4);
283 static void asus_wmi_ec_encode_registers(const u8 *in, u32 len, char *out)
285 char buffer[ASUSWMI_MAX_BUF_LEN * 2];
287 bin2hex(buffer, in, len);
289 utf8s_to_utf16s(buffer, len * 2, UTF16_LITTLE_ENDIAN, (wchar_t *)(out + 2), len * 2);
291 put_unaligned_le16(len * 4, out);
294 static void asus_wmi_ec_make_block_read_query(struct asus_wmi_ec_info *ec)
296 u8 registers[ASUSWMI_BREC_REGISTERS_MAX * 2];
297 const struct ec_sensor_info *si;
301 for (i = 0; i < ec->nr_sensors; i++) {
302 si = &known_ec_sensors[ec->sensors[i].info_index];
303 for (j = 0; j < si->addr.size; j++) {
304 registers[offset++] = si->addr.bank;
305 registers[offset++] = si->addr.index + j;
309 asus_wmi_ec_encode_registers(registers, offset, ec->read_arg);
312 static int asus_wmi_ec_block_read(u32 method_id, char *query, u8 *out)
314 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
315 struct acpi_buffer input;
316 union acpi_object *obj;
320 /* The first byte of the BRxx() argument string has to be the string size. */
321 input.length = query[0] + 2;
322 input.pointer = query;
323 status = wmi_evaluate_method(ASUSWMI_MONITORING_GUID, 0, method_id, &input, &output);
324 if (ACPI_FAILURE(status))
327 obj = output.pointer;
331 if (obj->type != ACPI_TYPE_BUFFER || obj->buffer.length < 2) {
336 ret = asus_wmi_ec_decode_reply_buffer(obj->buffer.pointer, obj->buffer.length, out);
343 static inline long get_sensor_value(const struct ec_sensor_info *si, u8 *data)
345 switch (si->addr.size) {
349 return get_unaligned_be16(data);
351 return get_unaligned_be32(data);
357 static void asus_wmi_ec_update_ec_sensors(struct asus_wmi_ec_info *ec)
359 const struct ec_sensor_info *si;
364 data = ec->read_buffer;
365 for (i_sensor = 0; i_sensor < ec->nr_sensors; i_sensor++) {
366 s = &ec->sensors[i_sensor];
367 si = &known_ec_sensors[s->info_index];
368 s->cached_value = get_sensor_value(si, data);
369 data += si->addr.size;
373 static long asus_wmi_ec_scale_sensor_value(long value, int data_type)
379 return value * MILLI;
385 static int asus_wmi_ec_find_sensor_index(const struct asus_wmi_ec_info *ec,
386 enum hwmon_sensor_types type, int channel)
390 for (i = 0; i < ec->nr_sensors; i++) {
391 if (known_ec_sensors[ec->sensors[i].info_index].type == type) {
401 static int asus_wmi_ec_get_cached_value_or_update(struct asus_wmi_sensors *sensor_data,
405 struct asus_wmi_ec_info *ec = &sensor_data->ec;
408 mutex_lock(&sensor_data->lock);
410 if (time_after(jiffies, ec->last_updated + HZ)) {
411 ret = asus_wmi_ec_block_read(ASUSWMI_METHODID_BLOCK_READ_EC,
412 ec->read_arg, ec->read_buffer);
416 asus_wmi_ec_update_ec_sensors(ec);
417 ec->last_updated = jiffies;
420 *value = ec->sensors[sensor_index].cached_value;
423 mutex_unlock(&sensor_data->lock);
428 /* Now follow the functions that implement the hwmon interface */
430 static int asus_wmi_ec_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
431 u32 attr, int channel, long *val)
433 struct asus_wmi_sensors *sensor_data = dev_get_drvdata(dev);
434 struct asus_wmi_ec_info *ec = &sensor_data->ec;
435 int ret, sidx, info_index;
438 sidx = asus_wmi_ec_find_sensor_index(ec, type, channel);
442 ret = asus_wmi_ec_get_cached_value_or_update(sensor_data, sidx, &value);
446 info_index = ec->sensors[sidx].info_index;
447 *val = asus_wmi_ec_scale_sensor_value(value, known_ec_sensors[info_index].type);
452 static int asus_wmi_ec_hwmon_read_string(struct device *dev,
453 enum hwmon_sensor_types type, u32 attr,
454 int channel, const char **str)
456 struct asus_wmi_sensors *sensor_data = dev_get_drvdata(dev);
457 struct asus_wmi_ec_info *ec = &sensor_data->ec;
460 sensor_index = asus_wmi_ec_find_sensor_index(ec, type, channel);
461 *str = known_ec_sensors[ec->sensors[sensor_index].info_index].label;
466 static umode_t asus_wmi_ec_hwmon_is_visible(const void *drvdata,
467 enum hwmon_sensor_types type, u32 attr,
470 const struct asus_wmi_sensors *sensor_data = drvdata;
471 const struct asus_wmi_ec_info *ec = &sensor_data->ec;
474 index = asus_wmi_ec_find_sensor_index(ec, type, channel);
476 return index < 0 ? 0 : 0444;
479 static int asus_wmi_hwmon_add_chan_info(struct hwmon_channel_info *asus_wmi_hwmon_chan,
480 struct device *dev, int num,
481 enum hwmon_sensor_types type, u32 config)
485 cfg = devm_kcalloc(dev, num + 1, sizeof(*cfg), GFP_KERNEL);
489 asus_wmi_hwmon_chan->type = type;
490 asus_wmi_hwmon_chan->config = cfg;
491 memset32(cfg, config, num);
496 static const struct hwmon_ops asus_wmi_ec_hwmon_ops = {
497 .is_visible = asus_wmi_ec_hwmon_is_visible,
498 .read = asus_wmi_ec_hwmon_read,
499 .read_string = asus_wmi_ec_hwmon_read_string,
502 static struct hwmon_chip_info asus_wmi_ec_chip_info = {
503 .ops = &asus_wmi_ec_hwmon_ops,
506 static int asus_wmi_ec_configure_sensor_setup(struct device *dev,
507 const enum known_ec_sensor *bsi)
509 struct asus_wmi_sensors *sensor_data = dev_get_drvdata(dev);
510 struct asus_wmi_ec_info *ec = &sensor_data->ec;
511 struct hwmon_channel_info *asus_wmi_hwmon_chan;
512 const struct hwmon_channel_info **asus_wmi_ci;
513 int nr_count[hwmon_max] = {}, nr_types = 0;
514 const struct hwmon_chip_info *chip_info;
515 const struct ec_sensor_info *si;
516 enum hwmon_sensor_types type;
517 struct device *hwdev;
520 ret = asus_wmi_ec_fill_board_sensors(ec, bsi);
524 if (!sensor_data->ec.nr_sensors)
527 for (i = 0; i < ec->nr_sensors; i++) {
528 si = &known_ec_sensors[ec->sensors[i].info_index];
529 if (!nr_count[si->type])
531 nr_count[si->type]++;
534 if (nr_count[hwmon_temp]) {
535 nr_count[hwmon_chip]++;
540 * If we can get values for all the registers in a single query,
541 * the query will not change from call to call.
543 asus_wmi_ec_make_block_read_query(ec);
545 asus_wmi_hwmon_chan = devm_kcalloc(dev, nr_types, sizeof(*asus_wmi_hwmon_chan),
547 if (!asus_wmi_hwmon_chan)
550 asus_wmi_ci = devm_kcalloc(dev, nr_types + 1, sizeof(*asus_wmi_ci), GFP_KERNEL);
554 asus_wmi_ec_chip_info.info = asus_wmi_ci;
555 chip_info = &asus_wmi_ec_chip_info;
557 for (type = 0; type < hwmon_max; type++) {
561 ret = asus_wmi_hwmon_add_chan_info(asus_wmi_hwmon_chan, dev,
562 nr_count[type], type,
563 hwmon_attributes[type]);
567 *asus_wmi_ci++ = asus_wmi_hwmon_chan++;
570 dev_dbg(dev, "board has %d EC sensors that span %d registers",
571 ec->nr_sensors, ec->nr_registers);
573 hwdev = devm_hwmon_device_register_with_info(dev, "asus_wmi_ec_sensors",
574 sensor_data, chip_info, NULL);
576 return PTR_ERR_OR_ZERO(hwdev);
579 static int asus_wmi_probe(struct wmi_device *wdev, const void *context)
581 struct asus_wmi_sensors *sensor_data;
582 struct asus_wmi_data *board_sensors;
583 const struct dmi_system_id *dmi_id;
584 const enum known_ec_sensor *bsi;
585 struct device *dev = &wdev->dev;
587 dmi_id = dmi_first_match(asus_wmi_ec_dmi_table);
591 board_sensors = dmi_id->driver_data;
592 bsi = board_sensors->known_board_sensors;
594 sensor_data = devm_kzalloc(dev, sizeof(*sensor_data), GFP_KERNEL);
598 mutex_init(&sensor_data->lock);
600 dev_set_drvdata(dev, sensor_data);
602 return asus_wmi_ec_configure_sensor_setup(dev, bsi);
605 static const struct wmi_device_id asus_ec_wmi_id_table[] = {
606 { ASUSWMI_MONITORING_GUID, NULL },
610 static struct wmi_driver asus_sensors_wmi_driver = {
612 .name = "asus_wmi_ec_sensors",
614 .id_table = asus_ec_wmi_id_table,
615 .probe = asus_wmi_probe,
617 module_wmi_driver(asus_sensors_wmi_driver);
619 MODULE_AUTHOR("Ed Brindley <kernel@maidavale.org>");
620 MODULE_AUTHOR("Eugene Shalygin <eugene.shalygin@gmail.com>");
621 MODULE_DESCRIPTION("Asus WMI Sensors Driver");
622 MODULE_LICENSE("GPL");