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