xref: /linux/drivers/platform/x86/lenovo/wmi-gamezone.c (revision 9d588a1140b9ae211581a7a154d0b806d8cd8238)
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  */
lwmi_gz_mode_call(struct notifier_block * nb,unsigned long cmd,void * data)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  */
lwmi_gz_event_call(struct notifier_block * nb,unsigned long cmd,void * data)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  */
lwmi_gz_thermal_mode_supported(struct wmi_device * wdev,int * supported)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  */
lwmi_gz_thermal_mode_get(struct wmi_device * wdev,enum thermal_mode * mode)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  */
lwmi_gz_profile_get(struct device * dev,enum platform_profile_option * profile)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 		*profile = PLATFORM_PROFILE_PERFORMANCE;
175 		break;
176 	case LWMI_GZ_THERMAL_MODE_EXTREME:
177 		*profile = PLATFORM_PROFILE_MAX_POWER;
178 		break;
179 	case LWMI_GZ_THERMAL_MODE_CUSTOM:
180 		*profile = PLATFORM_PROFILE_CUSTOM;
181 		break;
182 	default:
183 		return -EINVAL;
184 	}
185 
186 	guard(spinlock)(&priv->gz_mode_lock);
187 	priv->current_mode = mode;
188 
189 	return 0;
190 }
191 
192 /**
193  * lwmi_gz_profile_set() - Set the current platform profile.
194  * @dev: The Gamezone interface parent device.
195  * @profile: Pointer to the desired platform profile.
196  *
197  * Convert the given platform profile into a thermal mode based on the support
198  * level of the interface, then call the WMI method to set the thermal mode.
199  *
200  * Return: 0 on success, or an error code.
201  */
lwmi_gz_profile_set(struct device * dev,enum platform_profile_option profile)202 static int lwmi_gz_profile_set(struct device *dev,
203 			       enum platform_profile_option profile)
204 {
205 	struct lwmi_gz_priv *priv = dev_get_drvdata(dev);
206 	struct wmi_method_args_32 args;
207 	enum thermal_mode mode;
208 	int ret;
209 
210 	switch (profile) {
211 	case PLATFORM_PROFILE_LOW_POWER:
212 		mode = LWMI_GZ_THERMAL_MODE_QUIET;
213 		break;
214 	case PLATFORM_PROFILE_BALANCED:
215 		mode = LWMI_GZ_THERMAL_MODE_BALANCED;
216 		break;
217 	case PLATFORM_PROFILE_PERFORMANCE:
218 		mode = LWMI_GZ_THERMAL_MODE_PERFORMANCE;
219 		break;
220 	case PLATFORM_PROFILE_MAX_POWER:
221 		mode = LWMI_GZ_THERMAL_MODE_EXTREME;
222 		break;
223 	case PLATFORM_PROFILE_CUSTOM:
224 		mode = LWMI_GZ_THERMAL_MODE_CUSTOM;
225 		break;
226 	default:
227 		return -EOPNOTSUPP;
228 	}
229 
230 	args.arg0 = mode;
231 
232 	ret = lwmi_dev_evaluate_int(priv->wdev, 0x0,
233 				    LWMI_GZ_METHOD_ID_SMARTFAN_SET,
234 				    (u8 *)&args, sizeof(args), NULL);
235 	if (ret)
236 		return ret;
237 
238 	guard(spinlock)(&priv->gz_mode_lock);
239 	priv->current_mode = mode;
240 
241 	return 0;
242 }
243 
244 static const struct dmi_system_id fwbug_list[] = {
245 	{
246 		.ident = "Legion Go 8APU1",
247 		.matches = {
248 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
249 			DMI_MATCH(DMI_PRODUCT_VERSION, "Legion Go 8APU1"),
250 		},
251 		.driver_data = &quirk_no_extreme_bug,
252 	},
253 	{
254 		.ident = "Legion Go S 8APU1",
255 		.matches = {
256 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
257 			DMI_MATCH(DMI_PRODUCT_VERSION, "Legion Go S 8APU1"),
258 		},
259 		.driver_data = &quirk_no_extreme_bug,
260 	},
261 	{
262 		.ident = "Legion Go S 8ARP1",
263 		.matches = {
264 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
265 			DMI_MATCH(DMI_PRODUCT_VERSION, "Legion Go S 8ARP1"),
266 		},
267 		.driver_data = &quirk_no_extreme_bug,
268 	},
269 	{
270 		.ident = "Legion Go 8ASP2",
271 		.matches = {
272 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
273 			DMI_MATCH(DMI_PRODUCT_VERSION, "Legion Go 8ASP2"),
274 		},
275 		.driver_data = &quirk_no_extreme_bug,
276 	},
277 	{
278 		.ident = "Legion Go 8AHP2",
279 		.matches = {
280 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
281 			DMI_MATCH(DMI_PRODUCT_VERSION, "Legion Go 8AHP2"),
282 		},
283 		.driver_data = &quirk_no_extreme_bug,
284 	},
285 	{},
286 };
287 
288 /**
289  * lwmi_gz_extreme_supported() - Evaluate if a device supports extreme thermal mode.
290  * @profile_support_ver: Version of the WMI interface.
291  *
292  * Determine if the extreme thermal mode is supported by the hardware.
293  * Anything version 5 or lower does not. For devices with a version 6 or
294  * greater do a DMI check, as some devices report a version that supports
295  * extreme mode but have an incomplete entry in the BIOS. To ensure this
296  * cannot be set, quirk them to prevent assignment.
297  *
298  * Return: bool.
299  */
lwmi_gz_extreme_supported(int profile_support_ver)300 static bool lwmi_gz_extreme_supported(int profile_support_ver)
301 {
302 	const struct dmi_system_id *dmi_id;
303 	struct quirk_entry *quirks;
304 
305 	if (profile_support_ver < 6)
306 		return false;
307 
308 	dmi_id = dmi_first_match(fwbug_list);
309 	if (!dmi_id)
310 		return true;
311 
312 	quirks = dmi_id->driver_data;
313 
314 	return quirks->extreme_supported;
315 }
316 
317 /**
318  * lwmi_gz_platform_profile_probe - Enable and set up the platform profile
319  * device.
320  * @drvdata: Driver data for the interface.
321  * @choices: Container for enabled platform profiles.
322  *
323  * Determine if thermal mode is supported, and if so to what feature level.
324  * Then enable all supported platform profiles.
325  *
326  * Return: 0 on success, or an error code.
327  */
lwmi_gz_platform_profile_probe(void * drvdata,unsigned long * choices)328 static int lwmi_gz_platform_profile_probe(void *drvdata, unsigned long *choices)
329 {
330 	struct lwmi_gz_priv *priv = drvdata;
331 	int profile_support_ver;
332 	int ret;
333 
334 	ret = lwmi_gz_thermal_mode_supported(priv->wdev, &profile_support_ver);
335 	if (ret)
336 		return ret;
337 
338 	if (profile_support_ver < 1)
339 		return -ENODEV;
340 
341 	set_bit(PLATFORM_PROFILE_LOW_POWER, choices);
342 	set_bit(PLATFORM_PROFILE_BALANCED, choices);
343 	set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
344 	set_bit(PLATFORM_PROFILE_CUSTOM, choices);
345 
346 	priv->extreme_supported = lwmi_gz_extreme_supported(profile_support_ver);
347 	if (priv->extreme_supported)
348 		set_bit(PLATFORM_PROFILE_MAX_POWER, choices);
349 
350 	return 0;
351 }
352 
353 static const struct platform_profile_ops lwmi_gz_platform_profile_ops = {
354 	.probe = lwmi_gz_platform_profile_probe,
355 	.profile_get = lwmi_gz_profile_get,
356 	.profile_set = lwmi_gz_profile_set,
357 };
358 
lwmi_gz_probe(struct wmi_device * wdev,const void * context)359 static int lwmi_gz_probe(struct wmi_device *wdev, const void *context)
360 {
361 	struct lwmi_gz_priv *priv;
362 	int ret;
363 
364 	priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
365 	if (!priv)
366 		return -ENOMEM;
367 
368 	priv->wdev = wdev;
369 	dev_set_drvdata(&wdev->dev, priv);
370 
371 	priv->ppdev = devm_platform_profile_register(&wdev->dev, "lenovo-wmi-gamezone",
372 						     priv, &lwmi_gz_platform_profile_ops);
373 	if (IS_ERR(priv->ppdev))
374 		return -ENODEV;
375 
376 	spin_lock_init(&priv->gz_mode_lock);
377 
378 	ret = lwmi_gz_thermal_mode_get(wdev, &priv->current_mode);
379 	if (ret)
380 		return ret;
381 
382 	priv->event_nb.notifier_call = lwmi_gz_event_call;
383 	ret = devm_lwmi_events_register_notifier(&wdev->dev, &priv->event_nb);
384 	if (ret)
385 		return ret;
386 
387 	priv->mode_nb.notifier_call = lwmi_gz_mode_call;
388 	return devm_lwmi_om_register_notifier(&wdev->dev, &priv->mode_nb);
389 }
390 
391 static const struct wmi_device_id lwmi_gz_id_table[] = {
392 	{ LENOVO_GAMEZONE_GUID, NULL },
393 	{}
394 };
395 
396 static struct wmi_driver lwmi_gz_driver = {
397 	.driver = {
398 		.name = "lenovo_wmi_gamezone",
399 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
400 	},
401 	.id_table = lwmi_gz_id_table,
402 	.probe = lwmi_gz_probe,
403 	.no_singleton = true,
404 };
405 
406 module_wmi_driver(lwmi_gz_driver);
407 
408 MODULE_IMPORT_NS("LENOVO_WMI_EVENTS");
409 MODULE_IMPORT_NS("LENOVO_WMI_HELPERS");
410 MODULE_IMPORT_NS("LENOVO_WMI_OTHER");
411 MODULE_DEVICE_TABLE(wmi, lwmi_gz_id_table);
412 MODULE_AUTHOR("Derek J. Clark <derekjohn.clark@gmail.com>");
413 MODULE_DESCRIPTION("Lenovo GameZone WMI Driver");
414 MODULE_LICENSE("GPL");
415