1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Lenovo GameZone WMI interface driver. 4 * 5 * The GameZone WMI interface provides platform profile and fan curve settings 6 * for devices that fall under the "Gaming Series" of Lenovo Legion devices. 7 * 8 * Copyright (C) 2025 Derek J. Clark <derekjohn.clark@gmail.com> 9 */ 10 11 #include <linux/acpi.h> 12 #include <linux/dmi.h> 13 #include <linux/export.h> 14 #include <linux/list.h> 15 #include <linux/module.h> 16 #include <linux/notifier.h> 17 #include <linux/platform_profile.h> 18 #include <linux/spinlock.h> 19 #include <linux/spinlock_types.h> 20 #include <linux/types.h> 21 #include <linux/wmi.h> 22 23 #include "wmi-events.h" 24 #include "wmi-gamezone.h" 25 #include "wmi-helpers.h" 26 #include "wmi-other.h" 27 28 #define LENOVO_GAMEZONE_GUID "887B54E3-DDDC-4B2C-8B88-68A26A8835D0" 29 30 #define LWMI_GZ_METHOD_ID_SMARTFAN_SUP 43 31 #define LWMI_GZ_METHOD_ID_SMARTFAN_SET 44 32 #define LWMI_GZ_METHOD_ID_SMARTFAN_GET 45 33 34 static BLOCKING_NOTIFIER_HEAD(gz_chain_head); 35 36 struct lwmi_gz_priv { 37 enum thermal_mode current_mode; 38 struct notifier_block event_nb; 39 struct notifier_block mode_nb; 40 spinlock_t gz_mode_lock; /* current_mode lock */ 41 struct wmi_device *wdev; 42 int extreme_supported; 43 struct device *ppdev; 44 }; 45 46 struct quirk_entry { 47 bool extreme_supported; 48 }; 49 50 static struct quirk_entry quirk_no_extreme_bug = { 51 .extreme_supported = false, 52 }; 53 54 /** 55 * lwmi_gz_mode_call() - Call method for lenovo-wmi-other driver notifier. 56 * 57 * @nb: The notifier_block registered to lenovo-wmi-other driver. 58 * @cmd: The event type. 59 * @data: Thermal mode enum pointer pointer for returning the thermal mode. 60 * 61 * For LWMI_GZ_GET_THERMAL_MODE, retrieve the current thermal mode. 62 * 63 * Return: Notifier_block status. 64 */ 65 static int lwmi_gz_mode_call(struct notifier_block *nb, unsigned long cmd, 66 void *data) 67 { 68 enum thermal_mode **mode = data; 69 struct lwmi_gz_priv *priv; 70 71 priv = container_of(nb, struct lwmi_gz_priv, mode_nb); 72 73 switch (cmd) { 74 case LWMI_GZ_GET_THERMAL_MODE: 75 scoped_guard(spinlock, &priv->gz_mode_lock) { 76 **mode = priv->current_mode; 77 } 78 return NOTIFY_OK; 79 default: 80 return NOTIFY_DONE; 81 } 82 } 83 84 /** 85 * lwmi_gz_event_call() - Call method for lenovo-wmi-events driver notifier. 86 * block call chain. 87 * @nb: The notifier_block registered to lenovo-wmi-events driver. 88 * @cmd: The event type. 89 * @data: The data to be updated by the event. 90 * 91 * For LWMI_EVENT_THERMAL_MODE, set current_mode and notify platform_profile 92 * of a change. 93 * 94 * Return: notifier_block status. 95 */ 96 static int lwmi_gz_event_call(struct notifier_block *nb, unsigned long cmd, 97 void *data) 98 { 99 enum thermal_mode *mode = data; 100 struct lwmi_gz_priv *priv; 101 102 priv = container_of(nb, struct lwmi_gz_priv, event_nb); 103 104 switch (cmd) { 105 case LWMI_EVENT_THERMAL_MODE: 106 scoped_guard(spinlock, &priv->gz_mode_lock) { 107 priv->current_mode = *mode; 108 } 109 platform_profile_notify(priv->ppdev); 110 return NOTIFY_STOP; 111 default: 112 return NOTIFY_DONE; 113 } 114 } 115 116 /** 117 * lwmi_gz_thermal_mode_supported() - Get the version of the WMI 118 * interface to determine the support level. 119 * @wdev: The Gamezone WMI device. 120 * @supported: Pointer to return the support level with. 121 * 122 * Return: 0 on success, or an error code. 123 */ 124 static int lwmi_gz_thermal_mode_supported(struct wmi_device *wdev, 125 int *supported) 126 { 127 return lwmi_dev_evaluate_int(wdev, 0x0, LWMI_GZ_METHOD_ID_SMARTFAN_SUP, 128 NULL, 0, supported); 129 } 130 131 /** 132 * lwmi_gz_thermal_mode_get() - Get the current thermal mode. 133 * @wdev: The Gamezone interface WMI device. 134 * @mode: Pointer to return the thermal mode with. 135 * 136 * Return: 0 on success, or an error code. 137 */ 138 static int lwmi_gz_thermal_mode_get(struct wmi_device *wdev, 139 enum thermal_mode *mode) 140 { 141 return lwmi_dev_evaluate_int(wdev, 0x0, LWMI_GZ_METHOD_ID_SMARTFAN_GET, 142 NULL, 0, mode); 143 } 144 145 /** 146 * lwmi_gz_profile_get() - Get the current platform profile. 147 * @dev: the Gamezone interface parent device. 148 * @profile: Pointer to provide the current platform profile with. 149 * 150 * Call lwmi_gz_thermal_mode_get and convert the thermal mode into a platform 151 * profile based on the support level of the interface. 152 * 153 * Return: 0 on success, or an error code. 154 */ 155 static int lwmi_gz_profile_get(struct device *dev, 156 enum platform_profile_option *profile) 157 { 158 struct lwmi_gz_priv *priv = dev_get_drvdata(dev); 159 enum thermal_mode mode; 160 int ret; 161 162 ret = lwmi_gz_thermal_mode_get(priv->wdev, &mode); 163 if (ret) 164 return ret; 165 166 switch (mode) { 167 case LWMI_GZ_THERMAL_MODE_QUIET: 168 *profile = PLATFORM_PROFILE_LOW_POWER; 169 break; 170 case LWMI_GZ_THERMAL_MODE_BALANCED: 171 *profile = PLATFORM_PROFILE_BALANCED; 172 break; 173 case LWMI_GZ_THERMAL_MODE_PERFORMANCE: 174 if (priv->extreme_supported) { 175 *profile = PLATFORM_PROFILE_BALANCED_PERFORMANCE; 176 break; 177 } 178 *profile = PLATFORM_PROFILE_PERFORMANCE; 179 break; 180 case LWMI_GZ_THERMAL_MODE_EXTREME: 181 *profile = PLATFORM_PROFILE_PERFORMANCE; 182 break; 183 case LWMI_GZ_THERMAL_MODE_CUSTOM: 184 *profile = PLATFORM_PROFILE_CUSTOM; 185 break; 186 default: 187 return -EINVAL; 188 } 189 190 guard(spinlock)(&priv->gz_mode_lock); 191 priv->current_mode = mode; 192 193 return 0; 194 } 195 196 /** 197 * lwmi_gz_profile_set() - Set the current platform profile. 198 * @dev: The Gamezone interface parent device. 199 * @profile: Pointer to the desired platform profile. 200 * 201 * Convert the given platform profile into a thermal mode based on the support 202 * level of the interface, then call the WMI method to set the thermal mode. 203 * 204 * Return: 0 on success, or an error code. 205 */ 206 static int lwmi_gz_profile_set(struct device *dev, 207 enum platform_profile_option profile) 208 { 209 struct lwmi_gz_priv *priv = dev_get_drvdata(dev); 210 struct wmi_method_args_32 args; 211 enum thermal_mode mode; 212 int ret; 213 214 switch (profile) { 215 case PLATFORM_PROFILE_LOW_POWER: 216 mode = LWMI_GZ_THERMAL_MODE_QUIET; 217 break; 218 case PLATFORM_PROFILE_BALANCED: 219 mode = LWMI_GZ_THERMAL_MODE_BALANCED; 220 break; 221 case PLATFORM_PROFILE_BALANCED_PERFORMANCE: 222 mode = LWMI_GZ_THERMAL_MODE_PERFORMANCE; 223 break; 224 case PLATFORM_PROFILE_PERFORMANCE: 225 if (priv->extreme_supported) { 226 mode = LWMI_GZ_THERMAL_MODE_EXTREME; 227 break; 228 } 229 mode = LWMI_GZ_THERMAL_MODE_PERFORMANCE; 230 break; 231 case PLATFORM_PROFILE_CUSTOM: 232 mode = LWMI_GZ_THERMAL_MODE_CUSTOM; 233 break; 234 default: 235 return -EOPNOTSUPP; 236 } 237 238 args.arg0 = mode; 239 240 ret = lwmi_dev_evaluate_int(priv->wdev, 0x0, 241 LWMI_GZ_METHOD_ID_SMARTFAN_SET, 242 (u8 *)&args, sizeof(args), NULL); 243 if (ret) 244 return ret; 245 246 guard(spinlock)(&priv->gz_mode_lock); 247 priv->current_mode = mode; 248 249 return 0; 250 } 251 252 static const struct dmi_system_id fwbug_list[] = { 253 { 254 .ident = "Legion Go 8APU1", 255 .matches = { 256 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 257 DMI_MATCH(DMI_PRODUCT_VERSION, "Legion Go 8APU1"), 258 }, 259 .driver_data = &quirk_no_extreme_bug, 260 }, 261 { 262 .ident = "Legion Go S 8APU1", 263 .matches = { 264 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 265 DMI_MATCH(DMI_PRODUCT_VERSION, "Legion Go S 8APU1"), 266 }, 267 .driver_data = &quirk_no_extreme_bug, 268 }, 269 { 270 .ident = "Legion Go S 8ARP1", 271 .matches = { 272 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 273 DMI_MATCH(DMI_PRODUCT_VERSION, "Legion Go S 8ARP1"), 274 }, 275 .driver_data = &quirk_no_extreme_bug, 276 }, 277 {}, 278 279 }; 280 281 /** 282 * lwmi_gz_extreme_supported() - Evaluate if a device supports extreme thermal mode. 283 * @profile_support_ver: Version of the WMI interface. 284 * 285 * Determine if the extreme thermal mode is supported by the hardware. 286 * Anything version 5 or lower does not. For devices with a version 6 or 287 * greater do a DMI check, as some devices report a version that supports 288 * extreme mode but have an incomplete entry in the BIOS. To ensure this 289 * cannot be set, quirk them to prevent assignment. 290 * 291 * Return: bool. 292 */ 293 static bool lwmi_gz_extreme_supported(int profile_support_ver) 294 { 295 const struct dmi_system_id *dmi_id; 296 struct quirk_entry *quirks; 297 298 if (profile_support_ver < 6) 299 return false; 300 301 dmi_id = dmi_first_match(fwbug_list); 302 if (!dmi_id) 303 return true; 304 305 quirks = dmi_id->driver_data; 306 307 return quirks->extreme_supported; 308 } 309 310 /** 311 * lwmi_gz_platform_profile_probe - Enable and set up the platform profile 312 * device. 313 * @drvdata: Driver data for the interface. 314 * @choices: Container for enabled platform profiles. 315 * 316 * Determine if thermal mode is supported, and if so to what feature level. 317 * Then enable all supported platform profiles. 318 * 319 * Return: 0 on success, or an error code. 320 */ 321 static int lwmi_gz_platform_profile_probe(void *drvdata, unsigned long *choices) 322 { 323 struct lwmi_gz_priv *priv = drvdata; 324 int profile_support_ver; 325 int ret; 326 327 ret = lwmi_gz_thermal_mode_supported(priv->wdev, &profile_support_ver); 328 if (ret) 329 return ret; 330 331 if (profile_support_ver < 1) 332 return -ENODEV; 333 334 set_bit(PLATFORM_PROFILE_LOW_POWER, choices); 335 set_bit(PLATFORM_PROFILE_BALANCED, choices); 336 set_bit(PLATFORM_PROFILE_PERFORMANCE, choices); 337 set_bit(PLATFORM_PROFILE_CUSTOM, choices); 338 339 priv->extreme_supported = lwmi_gz_extreme_supported(profile_support_ver); 340 if (priv->extreme_supported) 341 set_bit(PLATFORM_PROFILE_BALANCED_PERFORMANCE, choices); 342 343 return 0; 344 } 345 346 static const struct platform_profile_ops lwmi_gz_platform_profile_ops = { 347 .probe = lwmi_gz_platform_profile_probe, 348 .profile_get = lwmi_gz_profile_get, 349 .profile_set = lwmi_gz_profile_set, 350 }; 351 352 static int lwmi_gz_probe(struct wmi_device *wdev, const void *context) 353 { 354 struct lwmi_gz_priv *priv; 355 int ret; 356 357 priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL); 358 if (!priv) 359 return -ENOMEM; 360 361 priv->wdev = wdev; 362 dev_set_drvdata(&wdev->dev, priv); 363 364 priv->ppdev = devm_platform_profile_register(&wdev->dev, "lenovo-wmi-gamezone", 365 priv, &lwmi_gz_platform_profile_ops); 366 if (IS_ERR(priv->ppdev)) 367 return -ENODEV; 368 369 spin_lock_init(&priv->gz_mode_lock); 370 371 ret = lwmi_gz_thermal_mode_get(wdev, &priv->current_mode); 372 if (ret) 373 return ret; 374 375 priv->event_nb.notifier_call = lwmi_gz_event_call; 376 ret = devm_lwmi_events_register_notifier(&wdev->dev, &priv->event_nb); 377 if (ret) 378 return ret; 379 380 priv->mode_nb.notifier_call = lwmi_gz_mode_call; 381 return devm_lwmi_om_register_notifier(&wdev->dev, &priv->mode_nb); 382 } 383 384 static const struct wmi_device_id lwmi_gz_id_table[] = { 385 { LENOVO_GAMEZONE_GUID, NULL }, 386 {} 387 }; 388 389 static struct wmi_driver lwmi_gz_driver = { 390 .driver = { 391 .name = "lenovo_wmi_gamezone", 392 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 393 }, 394 .id_table = lwmi_gz_id_table, 395 .probe = lwmi_gz_probe, 396 .no_singleton = true, 397 }; 398 399 module_wmi_driver(lwmi_gz_driver); 400 401 MODULE_IMPORT_NS("LENOVO_WMI_EVENTS"); 402 MODULE_IMPORT_NS("LENOVO_WMI_HELPERS"); 403 MODULE_IMPORT_NS("LENOVO_WMI_OTHER"); 404 MODULE_DEVICE_TABLE(wmi, lwmi_gz_id_table); 405 MODULE_AUTHOR("Derek J. Clark <derekjohn.clark@gmail.com>"); 406 MODULE_DESCRIPTION("Lenovo GameZone WMI Driver"); 407 MODULE_LICENSE("GPL"); 408