1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2021 Thomas Weißschuh <thomas@weissschuh.net>
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/acpi.h>
9 #include <linux/hwmon.h>
10 #include <linux/module.h>
11 #include <linux/wmi.h>
13 #define GIGABYTE_WMI_GUID "DEADBEEF-2001-0000-00A0-C90629100000"
14 #define NUM_TEMPERATURE_SENSORS 6
16 static bool force_load;
17 module_param(force_load, bool, 0444);
18 MODULE_PARM_DESC(force_load, "Force loading on unknown platform");
20 static u8 usable_sensors_mask;
22 enum gigabyte_wmi_commandtype {
23 GIGABYTE_WMI_BUILD_DATE_QUERY = 0x1,
24 GIGABYTE_WMI_MAINBOARD_TYPE_QUERY = 0x2,
25 GIGABYTE_WMI_FIRMWARE_VERSION_QUERY = 0x4,
26 GIGABYTE_WMI_MAINBOARD_NAME_QUERY = 0x5,
27 GIGABYTE_WMI_TEMPERATURE_QUERY = 0x125,
30 struct gigabyte_wmi_args {
34 static int gigabyte_wmi_perform_query(struct wmi_device *wdev,
35 enum gigabyte_wmi_commandtype command,
36 struct gigabyte_wmi_args *args, struct acpi_buffer *out)
38 const struct acpi_buffer in = {
39 .length = sizeof(*args),
43 acpi_status ret = wmidev_evaluate_method(wdev, 0x0, command, &in, out);
45 if (ACPI_FAILURE(ret))
51 static int gigabyte_wmi_query_integer(struct wmi_device *wdev,
52 enum gigabyte_wmi_commandtype command,
53 struct gigabyte_wmi_args *args, u64 *res)
55 union acpi_object *obj;
56 struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
59 ret = gigabyte_wmi_perform_query(wdev, command, args, &result);
63 if (obj && obj->type == ACPI_TYPE_INTEGER)
64 *res = obj->integer.value;
67 kfree(result.pointer);
71 static int gigabyte_wmi_temperature(struct wmi_device *wdev, u8 sensor, long *res)
73 struct gigabyte_wmi_args args = {
79 ret = gigabyte_wmi_query_integer(wdev, GIGABYTE_WMI_TEMPERATURE_QUERY, &args, &temp);
83 *res = (s8)temp * 1000; // value is a signed 8-bit integer
88 static int gigabyte_wmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
89 u32 attr, int channel, long *val)
91 struct wmi_device *wdev = dev_get_drvdata(dev);
93 return gigabyte_wmi_temperature(wdev, channel, val);
96 static umode_t gigabyte_wmi_hwmon_is_visible(const void *data, enum hwmon_sensor_types type,
97 u32 attr, int channel)
99 return usable_sensors_mask & BIT(channel) ? 0444 : 0;
102 static const struct hwmon_channel_info *gigabyte_wmi_hwmon_info[] = {
103 HWMON_CHANNEL_INFO(temp,
113 static const struct hwmon_ops gigabyte_wmi_hwmon_ops = {
114 .read = gigabyte_wmi_hwmon_read,
115 .is_visible = gigabyte_wmi_hwmon_is_visible,
118 static const struct hwmon_chip_info gigabyte_wmi_hwmon_chip_info = {
119 .ops = &gigabyte_wmi_hwmon_ops,
120 .info = gigabyte_wmi_hwmon_info,
123 static u8 gigabyte_wmi_detect_sensor_usability(struct wmi_device *wdev)
129 for (i = 0; i < NUM_TEMPERATURE_SENSORS; i++) {
130 if (!gigabyte_wmi_temperature(wdev, i, &temp))
136 #define DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME(name) \
138 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."), \
139 DMI_EXACT_MATCH(DMI_BOARD_NAME, name), \
142 static const struct dmi_system_id gigabyte_wmi_known_working_platforms[] = {
143 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("B450M S2H V2"),
144 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("B550 AORUS ELITE AX V2"),
145 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("B550 AORUS ELITE"),
146 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("B550 AORUS ELITE V2"),
147 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("B550 GAMING X V2"),
148 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("B550I AORUS PRO AX"),
149 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("B550M AORUS PRO-P"),
150 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("B550M DS3H"),
151 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("Z390 I AORUS PRO WIFI-CF"),
152 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("X570 AORUS ELITE"),
153 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("X570 GAMING X"),
154 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("X570 I AORUS PRO WIFI"),
155 DMI_EXACT_MATCH_GIGABYTE_BOARD_NAME("X570 UD"),
159 static int gigabyte_wmi_probe(struct wmi_device *wdev, const void *context)
161 struct device *hwmon_dev;
163 if (!dmi_check_system(gigabyte_wmi_known_working_platforms)) {
166 dev_warn(&wdev->dev, "Forcing load on unknown platform");
169 usable_sensors_mask = gigabyte_wmi_detect_sensor_usability(wdev);
170 if (!usable_sensors_mask) {
171 dev_info(&wdev->dev, "No temperature sensors usable");
175 hwmon_dev = devm_hwmon_device_register_with_info(&wdev->dev, "gigabyte_wmi", wdev,
176 &gigabyte_wmi_hwmon_chip_info, NULL);
178 return PTR_ERR_OR_ZERO(hwmon_dev);
181 static const struct wmi_device_id gigabyte_wmi_id_table[] = {
182 { GIGABYTE_WMI_GUID, NULL },
186 static struct wmi_driver gigabyte_wmi_driver = {
188 .name = "gigabyte-wmi",
190 .id_table = gigabyte_wmi_id_table,
191 .probe = gigabyte_wmi_probe,
193 module_wmi_driver(gigabyte_wmi_driver);
195 MODULE_DEVICE_TABLE(wmi, gigabyte_wmi_id_table);
196 MODULE_AUTHOR("Thomas Weißschuh <thomas@weissschuh.net>");
197 MODULE_DESCRIPTION("Gigabyte WMI temperature driver");
198 MODULE_LICENSE("GPL");