+ .extreme_supported = false,
+};
+
+/* Platform Profile Methods & Setup */
+static int
+lenovo_wmi_gz_platform_profile_supported(struct lenovo_wmi_gz_priv *priv,
+ int *supported)
+{
+ return lenovo_wmidev_evaluate_method_1(priv->wdev, 0x0,
+ WMI_METHOD_ID_SMARTFAN_SUPP, 0, supported);
+}
+
+static int lenovo_wmi_gz_profile_get(struct device *dev,
+ enum platform_profile_option *profile)
+{
+ struct lenovo_wmi_gz_priv *priv = dev_get_drvdata(dev);
+ int sel_prof;
+ int ret;
+
+ ret = lenovo_wmidev_evaluate_method_1(priv->wdev, 0x0,
+ WMI_METHOD_ID_SMARTFAN_GET, 0, &sel_prof);
+ if (ret)
+ return ret;
+
+ switch (sel_prof) {
+ case SMARTFAN_MODE_QUIET:
+ *profile = PLATFORM_PROFILE_LOW_POWER;
+ break;
+ case SMARTFAN_MODE_BALANCED:
+ *profile = PLATFORM_PROFILE_BALANCED;
+ break;
+ case SMARTFAN_MODE_PERFORMANCE:
+ if (priv->extreme_supported) {
+ *profile = PLATFORM_PROFILE_BALANCED_PERFORMANCE;
+ break;
+ }
+ *profile = PLATFORM_PROFILE_PERFORMANCE;
+ break;
+ case SMARTFAN_MODE_EXTREME:
+ *profile = PLATFORM_PROFILE_PERFORMANCE;
+ break;
+ case SMARTFAN_MODE_CUSTOM:
+ *profile = PLATFORM_PROFILE_CUSTOM;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ priv->current_profile = *profile;
+
+ ret = blocking_notifier_call_chain(&gz_chain_head, THERMAL_MODE_EVENT,
+ &sel_prof);
+ if (ret == NOTIFY_BAD)
+ pr_err("Failed to send notification to call chain for WMI event %u\n",
+ priv->type);
+ return 0;
+}
+
+static int lenovo_wmi_gz_profile_set(struct device *dev,
+ enum platform_profile_option profile)
+{
+ struct lenovo_wmi_gz_priv *priv = dev_get_drvdata(dev);
+ int sel_prof;
+ int ret;
+
+ switch (profile) {
+ case PLATFORM_PROFILE_LOW_POWER:
+ sel_prof = SMARTFAN_MODE_QUIET;
+ break;
+ case PLATFORM_PROFILE_BALANCED:
+ sel_prof = SMARTFAN_MODE_BALANCED;
+ break;
+ case PLATFORM_PROFILE_BALANCED_PERFORMANCE:
+ sel_prof = SMARTFAN_MODE_PERFORMANCE;
+ break;
+ case PLATFORM_PROFILE_PERFORMANCE:
+ if (priv->extreme_supported) {
+ sel_prof = SMARTFAN_MODE_EXTREME;
+ break;
+ }
+ sel_prof = SMARTFAN_MODE_PERFORMANCE;
+ break;
+ case PLATFORM_PROFILE_CUSTOM:
+ sel_prof = SMARTFAN_MODE_CUSTOM;
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+
+ ret = lenovo_wmidev_evaluate_method_1(priv->wdev, 0x0,
+ WMI_METHOD_ID_SMARTFAN_SET, sel_prof, NULL);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
+static const struct dmi_system_id fwbug_list[] = {
+ {
+ .ident = "Legion Go 8APU1",
+ .matches = {
+ DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
+ DMI_MATCH(DMI_PRODUCT_VERSION, "Legion Go 8APU1"),
+ },
+ .driver_data = &quirk_no_extreme_bug,
+ },
+ {
+ .ident = "Legion Go S 8ARP1",
+ .matches = {
+ DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
+ DMI_MATCH(DMI_PRODUCT_VERSION, "Legion Go S 8ARP1"),
+ },
+ .driver_data = &quirk_no_extreme_bug,
+ },
+ {
+ .ident = "Legion Go S 8APU1",
+ .matches = {
+ DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
+ DMI_MATCH(DMI_PRODUCT_VERSION, "Legion Go S 8APU1"),
+ },
+ .driver_data = &quirk_no_extreme_bug,
+ },
+ {},
+
+};
+
+static bool extreme_supported(int profile_support_ver)
+{
+ const struct dmi_system_id *dmi_id;
+ struct quirk_entry *quirks;
+
+ if (profile_support_ver < 6)
+ return false;
+
+ dmi_id = dmi_first_match(fwbug_list);
+ if (!dmi_id)
+ return true;
+
+ quirks = dmi_id->driver_data;
+ return quirks->extreme_supported;
+}
+
+static int lenovo_wmi_platform_profile_probe(void *drvdata,
+ unsigned long *choices)
+{
+ struct lenovo_wmi_gz_priv *priv = drvdata;
+ enum platform_profile_option profile;
+ int profile_support_ver;
+ int ret;
+
+ ret = lenovo_wmi_gz_platform_profile_supported(priv,
+ &profile_support_ver);
+ if (ret)
+ return ret;
+
+ if (profile_support_ver < 1)
+ return -ENODEV;
+
+ priv->extreme_supported = extreme_supported(profile_support_ver);
+
+ set_bit(PLATFORM_PROFILE_LOW_POWER, choices);
+ set_bit(PLATFORM_PROFILE_BALANCED, choices);
+ set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
+ set_bit(PLATFORM_PROFILE_CUSTOM, choices);
+
+ if (priv->extreme_supported)
+ set_bit(PLATFORM_PROFILE_BALANCED_PERFORMANCE, choices);
+
+ return 0;
+}
+
+static const struct platform_profile_ops lenovo_wmi_gz_platform_profile_ops = {
+ .probe = lenovo_wmi_platform_profile_probe,
+ .profile_get = lenovo_wmi_gz_profile_get,
+ .profile_set = lenovo_wmi_gz_profile_set,
+};
+
+/* Notifier Methods */
+int lenovo_wmi_gz_register_notifier(struct notifier_block *nb)
+{
+ guard(mutex)(&gz_chain_mutex);
+ return blocking_notifier_chain_register(&gz_chain_head, nb);
+}
+EXPORT_SYMBOL_NS_GPL(lenovo_wmi_gz_register_notifier, "GZ_WMI");
+
+int lenovo_wmi_gz_unregister_notifier(struct notifier_block *nb)
+{
+ guard(mutex)(&gz_chain_mutex);
+ return blocking_notifier_chain_unregister(&gz_chain_head, nb);
+}
+EXPORT_SYMBOL_NS_GPL(lenovo_wmi_gz_unregister_notifier, "GZ_WMI");
+
+static void devm_lenovo_wmi_gz_unregister_notifier(void *data)
+{
+ struct notifier_block *nb = data;
+
+ lenovo_wmi_gz_unregister_notifier(nb);
+}
+
+int devm_lenovo_wmi_gz_register_notifier(struct device *dev,
+ struct notifier_block *nb)
+{
+ int ret;
+
+ ret = lenovo_wmi_gz_register_notifier(nb);
+ if (ret < 0)
+ return ret;
+
+ return devm_add_action_or_reset(dev, devm_lenovo_wmi_gz_unregister_notifier, nb);
+}
+EXPORT_SYMBOL_NS_GPL(devm_lenovo_wmi_gz_register_notifier, "GZ_WMI");
+
+/* Driver Methods */
+static void lenovo_wmi_gz_notify(struct wmi_device *wdev,
+ union acpi_object *obj)
+{
+ struct lenovo_wmi_gz_priv *tm_priv = dev_get_drvdata(&wdev->dev);
+ struct lenovo_wmi_gz_priv *gz_priv =
+ container_of(&gz_chain_head, struct lenovo_wmi_gz_priv, nhead);
+ int sel_prof;
+ int ret;
+
+ if (obj->type != ACPI_TYPE_INTEGER)
+ return;
+
+ switch (tm_priv->type) {
+ case THERMAL_MODE:
+ sel_prof = obj->integer.value;
+ break;
+ default:
+ return;
+ }
+
+ /* Update primary Gamezone instance */
+ switch (sel_prof) {
+ case SMARTFAN_MODE_QUIET:
+ gz_priv->current_profile = PLATFORM_PROFILE_LOW_POWER;
+ break;
+ case SMARTFAN_MODE_BALANCED:
+ gz_priv->current_profile = PLATFORM_PROFILE_BALANCED;
+ break;
+ case SMARTFAN_MODE_PERFORMANCE:
+ if (gz_priv->extreme_supported) {
+ gz_priv->current_profile =
+ PLATFORM_PROFILE_BALANCED_PERFORMANCE;
+ break;
+ }
+ gz_priv->current_profile = PLATFORM_PROFILE_PERFORMANCE;
+ break;
+ case SMARTFAN_MODE_EXTREME:
+ gz_priv->current_profile = PLATFORM_PROFILE_PERFORMANCE;
+ break;
+ case SMARTFAN_MODE_CUSTOM:
+ gz_priv->current_profile = PLATFORM_PROFILE_CUSTOM;
+ break;
+ default:
+ break;
+ }
+
+ ret = blocking_notifier_call_chain(&gz_chain_head, THERMAL_MODE_EVENT,
+ &sel_prof);
+ if (ret == NOTIFY_BAD)
+ pr_err("Failed to send notification to call chain for WMI event %u\n",
+ tm_priv->type);
+}
+
+static int lenovo_wmi_gz_probe(struct wmi_device *wdev, const void *context)
+{
+ struct lenovo_wmi_gz_priv *priv =
+ devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
+
+ if (!priv)
+ return -ENOMEM;
+
+ if (!context)
+ return -EINVAL;
+
+ priv->wdev = wdev;
+ priv->type = *((enum lenovo_wmi_gz_type *)context);
+
+ dev_set_drvdata(&wdev->dev, priv);
+
+ if (priv->type != GAMEZONE_FULL)
+ return 0;
+
+ priv->nhead = gz_chain_head;
+ priv->ppdev = platform_profile_register(&wdev->dev, "lenovo-wmi-gamezone",
+ priv, &lenovo_wmi_gz_platform_profile_ops);
+
+ return 0;
+}
+
+static const struct wmi_device_id lenovo_wmi_gz_id_table[] = {
+ { GAMEZONE_WMI_DEVICE(LENOVO_GAMEZONE_GUID, GAMEZONE_FULL) },
+ { GAMEZONE_WMI_DEVICE(THERMAL_MODE_EVENT_GUID, THERMAL_MODE) },
+ {}
+};
+
+static struct wmi_driver lenovo_wmi_gz_driver = {
+ .driver = {
+ .name = "lenovo_wmi_gamezone",
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
+ },
+ .id_table = lenovo_wmi_gz_id_table,
+ .probe = lenovo_wmi_gz_probe,
+ .notify = lenovo_wmi_gz_notify,
+ .no_singleton = true,
+};
+
+module_wmi_driver(lenovo_wmi_gz_driver);
+
+MODULE_IMPORT_NS("LENOVO_WMI");
+MODULE_DEVICE_TABLE(wmi, lenovo_wmi_gz_id_table);
+MODULE_AUTHOR("Derek J. Clark <derekjohn.clark@xxxxxxxxx>");
+MODULE_DESCRIPTION("Lenovo GameZone WMI Driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/platform/x86/lenovo-wmi.c b/drivers/platform/x86/lenovo-wmi.c
new file mode 100644
index 000000000000..0de2c37e69bd
--- /dev/null
+++ b/drivers/platform/x86/lenovo-wmi.c
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Lenovo Legion WMI interface driver. The Lenovo Legion WMI interface is
+ * broken up into multiple GUID interfaces that require cross-references
+ * between GUID's for some functionality. The "Custom Method" interface is a
+ * legacy interface for managing and displaying CPU & GPU power and hwmon
+ * settings and readings. The "Other Mode" interface is a modern interface
+ * that replaces or extends the "Custom Method" interface methods. The
+ * "Gamezone" interface adds advanced features such as fan profiles and
+ * overclocking. The "Lighting" interface adds control of various status
+ * lights related to different hardware components. "Other Mode" uses
+ * the data structs LENOVO_CAPABILITY_DATA_00, LENOVO_CAPABILITY_DATA_01
+ * and LENOVO_CAPABILITY_DATA_02 structs for capability information.
+ *
+ * Copyright(C) 2024 Derek J. Clark <derekjohn.clark@xxxxxxxxx>
+ *
+ */
+
+#include <linux/wmi.h>
+#include "lenovo-wmi.h"
+
+/* wmidev_evaluate_method helper functions */
+static int lenovo_wmidev_evaluate_method(struct wmi_device *wdev, u8 instance,
+ u32 method_id, struct acpi_buffer *in,
+ struct acpi_buffer *out)
+{
+ acpi_status status;
+
+ status = wmidev_evaluate_method(wdev, instance, method_id, in, out);
+
+ if (ACPI_FAILURE(status))
+ return -EIO;
+
+ return 0;
+};
+
+int lenovo_wmidev_evaluate_method_2(struct wmi_device *wdev, u8 instance,
+ u32 method_id, u32 arg0, u32 arg1,
+ u32 *retval)
+{
+ struct wmi_method_args args = { arg0, arg1 };
+ struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
+ struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
+ union acpi_object *ret_obj __free(kfree) = NULL;
+ int err;
+
+ err = lenovo_wmidev_evaluate_method(wdev, instance, method_id, &input,
+ &output);
+
+ if (err)
+ return err;
+
+ if (retval) {
+ ret_obj = (union acpi_object *)output.pointer;
+ if (!ret_obj)
+ return -ENODATA;
+
+ if (ret_obj->type != ACPI_TYPE_INTEGER)
+ return -ENXIO;
+
+ *retval = (u32)ret_obj->integer.value;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(lenovo_wmidev_evaluate_method_2, "LENOVO_WMI");
+
+int lenovo_wmidev_evaluate_method_1(struct wmi_device *wdev, u8 instance,
+ u32 method_id, u32 arg0, u32 *retval)
+{
+ return lenovo_wmidev_evaluate_method_2(wdev, instance, method_id, arg0,
+ 0, retval);
+}
+EXPORT_SYMBOL_NS_GPL(lenovo_wmidev_evaluate_method_1, "LENOVO_WMI");
+MODULE_AUTHOR("Derek J. Clark <derekjohn.clark@xxxxxxxxx>");
+MODULE_DESCRIPTION("Lenovo WMI Common Driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/platform/x86/lenovo-wmi.h b/drivers/platform/x86/lenovo-wmi.h
new file mode 100644
index 000000000000..113928b4fc0f
--- /dev/null
+++ b/drivers/platform/x86/lenovo-wmi.h
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Lenovo Legion WMI interface driver. The Lenovo Legion WMI interface is
+ * broken up into multiple GUID interfaces that require cross-references
+ * between GUID's for some functionality. The "Custom Method" interface is a
+ * legacy interface for managing and displaying CPU & GPU power and hwmon
+ * settings and readings. The "Other Mode" interface is a modern interface
+ * that replaces or extends the "Custom Method" interface methods. The
+ * "Gamezone" interface adds advanced features such as fan profiles and
+ * overclocking. The "Lighting" interface adds control of various status
+ * lights related to different hardware components. "Other Mode" uses
+ * the data structs LENOVO_CAPABILITY_DATA_00, LENOVO_CAPABILITY_DATA_01
+ * and LENOVO_CAPABILITY_DATA_02 structs for capability information.
+ *
+ * Copyright(C) 2024 Derek J. Clark <derekjohn.clark@xxxxxxxxx>
+ *
+ */
+#include <linux/notifier.h>
+#include <linux/platform_profile.h>
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#ifndef _LENOVO_WMI_H_
+#define _LENOVO_WMI_H_
+
+#include <linux/bits.h>
+#include <linux/types.h>
+#include <linux/wmi.h>
+
+struct wmi_method_args {
+ u32 arg0;
+ u32 arg1;
+};
+
+/* gamezone structs */
+enum thermal_mode {
+ SMARTFAN_MODE_QUIET = 0x01,
+ SMARTFAN_MODE_BALANCED = 0x02,
+ SMARTFAN_MODE_PERFORMANCE = 0x03,
+ SMARTFAN_MODE_EXTREME = 0xE0, /* Ver 6+ */
+ SMARTFAN_MODE_CUSTOM = 0xFF,
+};
+
+enum lenovo_wmi_action {
+ THERMAL_MODE_EVENT = 1,
+};
+
+/* wmidev_evaluate_method helper functions */
+int lenovo_wmidev_evaluate_method_2(struct wmi_device *wdev, u8 instance,
+ u32 method_id, u32 arg0, u32 arg1,
+ u32 *retval);
+int lenovo_wmidev_evaluate_method_1(struct wmi_device *wdev, u8 instance,
+ u32 method_id, u32 arg0, u32 *retval);
+
+/* lenovo_wmi_gz_driver notifier functions */
+int lenovo_wmi_gz_notifier_call(struct notifier_block *nb, unsigned long action,
+ enum platform_profile_option *profile);
+int lenovo_wmi_gz_register_notifier(struct notifier_block *nb);
+int lenovo_wmi_gz_unregister_notifier(struct notifier_block *nb);
+int devm_lenovo_wmi_gz_register_notifier(struct device *dev,
+ struct notifier_block *nb);
+#endif /* !_LENOVO_WMI_H_ */