1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HWMON driver for ASUS motherboards that provides sensor readouts via WMI
4 * interface present in the UEFI of the X370/X470/B450/X399 Ryzen motherboards.
8 * WMI interface provides:
24 * - Chassis Fan 1 RPM,
25 * - Chassis Fan 2 RPM,
26 * - Chassis Fan 3 RPM,
33 * - CPU Socket Temperature,
34 * - Motherboard Temperature,
35 * - Chipset Temperature,
36 * - Tsensor 1 Temperature,
37 * - CPU VRM Temperature,
40 * - CPU VRM Output Current.
43 #include <linux/acpi.h>
44 #include <linux/dmi.h>
45 #include <linux/hwmon.h>
46 #include <linux/init.h>
47 #include <linux/jiffies.h>
48 #include <linux/kernel.h>
49 #include <linux/module.h>
50 #include <linux/mutex.h>
51 #include <linux/units.h>
52 #include <linux/wmi.h>
54 #define ASUSWMI_MONITORING_GUID "466747A0-70EC-11DE-8A39-0800200C9A66"
55 #define ASUSWMI_METHODID_GET_VALUE 0x52574543 /* RWEC */
56 #define ASUSWMI_METHODID_UPDATE_BUFFER 0x51574543 /* QWEC */
57 #define ASUSWMI_METHODID_GET_INFO 0x50574543 /* PWEC */
58 #define ASUSWMI_METHODID_GET_NUMBER 0x50574572 /* PWEr */
59 #define ASUSWMI_METHODID_GET_VERSION 0x50574574 /* PWEt */
61 #define ASUS_WMI_MAX_STR_SIZE 32
63 #define DMI_EXACT_MATCH_ASUS_BOARD_NAME(name) { \
65 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC."), \
66 DMI_EXACT_MATCH(DMI_BOARD_NAME, name), \
70 static const struct dmi_system_id asus_wmi_dmi_table[] = {
71 DMI_EXACT_MATCH_ASUS_BOARD_NAME("PRIME X399-A"),
72 DMI_EXACT_MATCH_ASUS_BOARD_NAME("PRIME X470-PRO"),
73 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG CROSSHAIR VI EXTREME"),
74 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG CROSSHAIR VI HERO"),
75 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG CROSSHAIR VI HERO (WI-FI AC)"),
76 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG CROSSHAIR VII HERO"),
77 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG CROSSHAIR VII HERO (WI-FI)"),
78 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG STRIX B450-E GAMING"),
79 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG STRIX B450-F GAMING"),
80 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG STRIX B450-I GAMING"),
81 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG STRIX X399-E GAMING"),
82 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG STRIX X470-F GAMING"),
83 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG STRIX X470-I GAMING"),
84 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG ZENITH EXTREME"),
85 DMI_EXACT_MATCH_ASUS_BOARD_NAME("ROG ZENITH EXTREME ALPHA"),
88 MODULE_DEVICE_TABLE(dmi, asus_wmi_dmi_table);
90 enum asus_wmi_sensor_class {
98 enum asus_wmi_location {
115 enum asus_wmi_source {
120 static enum hwmon_sensor_types asus_data_types[] = {
121 [VOLTAGE] = hwmon_in,
122 [TEMPERATURE_C] = hwmon_temp,
123 [FAN_RPM] = hwmon_fan,
124 [CURRENT] = hwmon_curr,
125 [WATER_FLOW] = hwmon_fan,
128 static u32 hwmon_attributes[hwmon_max] = {
129 [hwmon_chip] = HWMON_C_REGISTER_TZ,
130 [hwmon_temp] = HWMON_T_INPUT | HWMON_T_LABEL,
131 [hwmon_in] = HWMON_I_INPUT | HWMON_I_LABEL,
132 [hwmon_curr] = HWMON_C_INPUT | HWMON_C_LABEL,
133 [hwmon_fan] = HWMON_F_INPUT | HWMON_F_LABEL,
137 * struct asus_wmi_sensor_info - sensor info.
139 * @data_type: sensor class e.g. voltage, temp etc.
140 * @location: sensor location.
141 * @name: sensor name.
142 * @source: sensor source.
143 * @type: sensor type signed, unsigned etc.
144 * @cached_value: cached sensor value.
146 struct asus_wmi_sensor_info {
150 char name[ASUS_WMI_MAX_STR_SIZE];
156 struct asus_wmi_wmi_info {
157 unsigned long source_last_updated[3]; /* in jiffies */
160 const struct asus_wmi_sensor_info **info[hwmon_max];
161 struct asus_wmi_sensor_info **info_by_id;
164 struct asus_wmi_sensors {
165 struct asus_wmi_wmi_info wmi;
166 /* lock access to internal cache */
171 * Universal method for calling WMI method
173 static int asus_wmi_call_method(u32 method_id, u32 *args, struct acpi_buffer *output)
175 struct acpi_buffer input = {(acpi_size) sizeof(*args), args };
178 status = wmi_evaluate_method(ASUSWMI_MONITORING_GUID, 0,
179 method_id, &input, output);
180 if (ACPI_FAILURE(status))
187 * Gets the version of the ASUS sensors interface implemented
189 static int asus_wmi_get_version(u32 *version)
191 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
192 u32 args[] = {0, 0, 0};
193 union acpi_object *obj;
196 err = asus_wmi_call_method(ASUSWMI_METHODID_GET_VERSION, args, &output);
200 obj = output.pointer;
204 if (obj->type != ACPI_TYPE_INTEGER) {
210 *version = obj->integer.value;
218 * Gets the number of sensor items
220 static int asus_wmi_get_item_count(u32 *count)
222 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
223 u32 args[] = {0, 0, 0};
224 union acpi_object *obj;
227 err = asus_wmi_call_method(ASUSWMI_METHODID_GET_NUMBER, args, &output);
231 obj = output.pointer;
235 if (obj->type != ACPI_TYPE_INTEGER) {
241 *count = obj->integer.value;
248 static int asus_wmi_hwmon_add_chan_info(struct hwmon_channel_info *asus_wmi_hwmon_chan,
249 struct device *dev, int num,
250 enum hwmon_sensor_types type, u32 config)
254 cfg = devm_kcalloc(dev, num + 1, sizeof(*cfg), GFP_KERNEL);
258 asus_wmi_hwmon_chan->type = type;
259 asus_wmi_hwmon_chan->config = cfg;
260 memset32(cfg, config, num);
266 * For a given sensor item returns details e.g. type (voltage/temperature/fan speed etc), bank etc
268 static int asus_wmi_sensor_info(int index, struct asus_wmi_sensor_info *s)
270 union acpi_object name_obj, data_type_obj, location_obj, source_obj, type_obj;
271 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
272 u32 args[] = {index, 0};
273 union acpi_object *obj;
276 err = asus_wmi_call_method(ASUSWMI_METHODID_GET_INFO, args, &output);
282 obj = output.pointer;
286 if (obj->type != ACPI_TYPE_PACKAGE) {
291 if (obj->package.count != 5) {
296 name_obj = obj->package.elements[0];
297 if (name_obj.type != ACPI_TYPE_STRING) {
302 strncpy(s->name, name_obj.string.pointer, sizeof(s->name) - 1);
304 data_type_obj = obj->package.elements[1];
305 if (data_type_obj.type != ACPI_TYPE_INTEGER) {
310 s->data_type = data_type_obj.integer.value;
312 location_obj = obj->package.elements[2];
313 if (location_obj.type != ACPI_TYPE_INTEGER) {
318 s->location = location_obj.integer.value;
320 source_obj = obj->package.elements[3];
321 if (source_obj.type != ACPI_TYPE_INTEGER) {
326 s->source = source_obj.integer.value;
328 type_obj = obj->package.elements[4];
329 if (type_obj.type != ACPI_TYPE_INTEGER) {
335 s->type = type_obj.integer.value;
342 static int asus_wmi_update_buffer(int source)
344 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
345 u32 args[] = {source, 0};
347 return asus_wmi_call_method(ASUSWMI_METHODID_UPDATE_BUFFER, args, &output);
350 static int asus_wmi_get_sensor_value(u8 index, long *value)
352 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
353 u32 args[] = {index, 0};
354 union acpi_object *obj;
357 err = asus_wmi_call_method(ASUSWMI_METHODID_GET_VALUE, args, &output);
361 obj = output.pointer;
365 if (obj->type != ACPI_TYPE_INTEGER) {
371 *value = obj->integer.value;
378 static int asus_wmi_update_values_for_source(u8 source, struct asus_wmi_sensors *sensor_data)
380 struct asus_wmi_sensor_info *sensor;
385 for (i = 0; i < sensor_data->wmi.sensor_count; i++) {
386 sensor = sensor_data->wmi.info_by_id[i];
387 if (sensor && sensor->source == source) {
388 ret = asus_wmi_get_sensor_value(sensor->id, &value);
392 sensor->cached_value = value;
399 static int asus_wmi_scale_sensor_value(u32 value, int data_type)
401 /* FAN_RPM and WATER_FLOW don't need scaling */
404 /* value in microVolts */
405 return DIV_ROUND_CLOSEST(value, KILO);
407 /* value in Celsius */
408 return value * MILLIDEGREE_PER_DEGREE;
410 /* value in Amperes */
411 return value * MILLI;
416 static int asus_wmi_get_cached_value_or_update(const struct asus_wmi_sensor_info *sensor,
417 struct asus_wmi_sensors *sensor_data,
422 mutex_lock(&sensor_data->lock);
424 if (time_after(jiffies, sensor_data->wmi.source_last_updated[sensor->source] + HZ)) {
425 ret = asus_wmi_update_buffer(sensor->source);
429 ret = asus_wmi_update_values_for_source(sensor->source, sensor_data);
433 sensor_data->wmi.source_last_updated[sensor->source] = jiffies;
436 *value = sensor->cached_value;
439 mutex_unlock(&sensor_data->lock);
444 /* Now follow the functions that implement the hwmon interface */
445 static int asus_wmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
446 u32 attr, int channel, long *val)
448 const struct asus_wmi_sensor_info *sensor;
452 struct asus_wmi_sensors *sensor_data = dev_get_drvdata(dev);
454 sensor = *(sensor_data->wmi.info[type] + channel);
456 ret = asus_wmi_get_cached_value_or_update(sensor, sensor_data, &value);
460 *val = asus_wmi_scale_sensor_value(value, sensor->data_type);
465 static int asus_wmi_hwmon_read_string(struct device *dev,
466 enum hwmon_sensor_types type, u32 attr,
467 int channel, const char **str)
469 struct asus_wmi_sensors *sensor_data = dev_get_drvdata(dev);
470 const struct asus_wmi_sensor_info *sensor;
472 sensor = *(sensor_data->wmi.info[type] + channel);
478 static umode_t asus_wmi_hwmon_is_visible(const void *drvdata,
479 enum hwmon_sensor_types type, u32 attr,
482 const struct asus_wmi_sensors *sensor_data = drvdata;
483 const struct asus_wmi_sensor_info *sensor;
485 sensor = *(sensor_data->wmi.info[type] + channel);
492 static const struct hwmon_ops asus_wmi_hwmon_ops = {
493 .is_visible = asus_wmi_hwmon_is_visible,
494 .read = asus_wmi_hwmon_read,
495 .read_string = asus_wmi_hwmon_read_string,
498 static struct hwmon_chip_info asus_wmi_chip_info = {
499 .ops = &asus_wmi_hwmon_ops,
503 static int asus_wmi_configure_sensor_setup(struct device *dev,
504 struct asus_wmi_sensors *sensor_data)
506 const struct hwmon_channel_info **ptr_asus_wmi_ci;
507 struct hwmon_channel_info *asus_wmi_hwmon_chan;
508 int nr_count[hwmon_max] = {}, nr_types = 0;
509 struct asus_wmi_sensor_info *temp_sensor;
510 const struct hwmon_chip_info *chip_info;
511 enum hwmon_sensor_types type;
512 struct device *hwdev;
516 temp_sensor = devm_kcalloc(dev, 1, sizeof(*temp_sensor), GFP_KERNEL);
520 for (i = 0; i < sensor_data->wmi.sensor_count; i++) {
521 err = asus_wmi_sensor_info(i, temp_sensor);
525 switch (temp_sensor->data_type) {
531 type = asus_data_types[temp_sensor->data_type];
539 if (nr_count[hwmon_temp])
540 nr_count[hwmon_chip]++, nr_types++;
542 asus_wmi_hwmon_chan = devm_kcalloc(dev, nr_types,
543 sizeof(*asus_wmi_hwmon_chan),
545 if (!asus_wmi_hwmon_chan)
548 ptr_asus_wmi_ci = devm_kcalloc(dev, nr_types + 1,
549 sizeof(*ptr_asus_wmi_ci), GFP_KERNEL);
550 if (!ptr_asus_wmi_ci)
553 asus_wmi_chip_info.info = ptr_asus_wmi_ci;
554 chip_info = &asus_wmi_chip_info;
556 sensor_data->wmi.info_by_id = devm_kcalloc(dev, sensor_data->wmi.sensor_count,
557 sizeof(*sensor_data->wmi.info_by_id),
560 if (!sensor_data->wmi.info_by_id)
563 for (type = 0; type < hwmon_max; type++) {
567 err = asus_wmi_hwmon_add_chan_info(asus_wmi_hwmon_chan, dev,
568 nr_count[type], type,
569 hwmon_attributes[type]);
573 *ptr_asus_wmi_ci++ = asus_wmi_hwmon_chan++;
575 sensor_data->wmi.info[type] = devm_kcalloc(dev,
577 sizeof(*sensor_data->wmi.info),
579 if (!sensor_data->wmi.info[type])
583 for (i = sensor_data->wmi.sensor_count - 1; i >= 0; i--) {
584 temp_sensor = devm_kzalloc(dev, sizeof(*temp_sensor), GFP_KERNEL);
588 err = asus_wmi_sensor_info(i, temp_sensor);
592 switch (temp_sensor->data_type) {
598 type = asus_data_types[temp_sensor->data_type];
599 idx = --nr_count[type];
600 *(sensor_data->wmi.info[type] + idx) = temp_sensor;
601 sensor_data->wmi.info_by_id[i] = temp_sensor;
606 dev_dbg(dev, "board has %d sensors",
607 sensor_data->wmi.sensor_count);
609 hwdev = devm_hwmon_device_register_with_info(dev, "asus_wmi_sensors",
610 sensor_data, chip_info, NULL);
612 return PTR_ERR_OR_ZERO(hwdev);
615 static int asus_wmi_probe(struct wmi_device *wdev, const void *context)
617 struct asus_wmi_sensors *sensor_data;
618 struct device *dev = &wdev->dev;
621 if (!dmi_check_system(asus_wmi_dmi_table))
624 sensor_data = devm_kzalloc(dev, sizeof(*sensor_data), GFP_KERNEL);
628 if (asus_wmi_get_version(&version))
631 if (asus_wmi_get_item_count(&sensor_data->wmi.sensor_count))
634 if (sensor_data->wmi.sensor_count <= 0 || version < 2) {
635 dev_info(dev, "version: %u with %d sensors is unsupported\n",
636 version, sensor_data->wmi.sensor_count);
641 mutex_init(&sensor_data->lock);
643 dev_set_drvdata(dev, sensor_data);
645 return asus_wmi_configure_sensor_setup(dev, sensor_data);
648 static const struct wmi_device_id asus_wmi_id_table[] = {
649 { ASUSWMI_MONITORING_GUID, NULL },
653 static struct wmi_driver asus_sensors_wmi_driver = {
655 .name = "asus_wmi_sensors",
657 .id_table = asus_wmi_id_table,
658 .probe = asus_wmi_probe,
660 module_wmi_driver(asus_sensors_wmi_driver);
663 MODULE_DESCRIPTION("Asus WMI Sensors Driver");
664 MODULE_LICENSE("GPL");