xref: /linux/drivers/platform/x86/dell/alienware-wmi-wmax.c (revision db4a3f0fbedb0398f77b9047e8b8bb2b49f355bb)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Alienware WMAX WMI device driver
4  *
5  * Copyright (C) 2014 Dell Inc <Dell.Client.Kernel@dell.com>
6  * Copyright (C) 2025 Kurt Borja <kuurtb@gmail.com>
7  */
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/array_size.h>
12 #include <linux/bitfield.h>
13 #include <linux/bitmap.h>
14 #include <linux/bits.h>
15 #include <linux/debugfs.h>
16 #include <linux/dmi.h>
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/kstrtox.h>
20 #include <linux/minmax.h>
21 #include <linux/moduleparam.h>
22 #include <linux/platform_profile.h>
23 #include <linux/pm.h>
24 #include <linux/seq_file.h>
25 #include <linux/units.h>
26 #include <linux/wmi.h>
27 #include "alienware-wmi.h"
28 
29 #define WMAX_METHOD_HDMI_SOURCE			0x1
30 #define WMAX_METHOD_HDMI_STATUS			0x2
31 #define WMAX_METHOD_HDMI_CABLE			0x5
32 #define WMAX_METHOD_AMPLIFIER_CABLE		0x6
33 #define WMAX_METHOD_DEEP_SLEEP_CONTROL		0x0B
34 #define WMAX_METHOD_DEEP_SLEEP_STATUS		0x0C
35 #define WMAX_METHOD_BRIGHTNESS			0x3
36 #define WMAX_METHOD_ZONE_CONTROL		0x4
37 
38 #define AWCC_METHOD_GET_FAN_SENSORS		0x13
39 #define AWCC_METHOD_THERMAL_INFORMATION		0x14
40 #define AWCC_METHOD_THERMAL_CONTROL		0x15
41 #define AWCC_METHOD_FWUP_GPIO_CONTROL		0x20
42 #define AWCC_METHOD_READ_TOTAL_GPIOS		0x21
43 #define AWCC_METHOD_READ_GPIO_STATUS		0x22
44 #define AWCC_METHOD_GAME_SHIFT_STATUS		0x25
45 
46 #define AWCC_FAILURE_CODE			0xFFFFFFFF
47 #define AWCC_FAILURE_CODE_2			0xFFFFFFFE
48 
49 #define AWCC_SENSOR_ID_FLAG			BIT(8)
50 #define AWCC_THERMAL_MODE_MASK			GENMASK(3, 0)
51 #define AWCC_THERMAL_TABLE_MASK			GENMASK(7, 4)
52 #define AWCC_RESOURCE_ID_MASK			GENMASK(7, 0)
53 
54 /* Arbitrary limit based on supported models */
55 #define AWCC_MAX_RES_COUNT			16
56 #define AWCC_ID_BITMAP_SIZE			(U8_MAX + 1)
57 #define AWCC_ID_BITMAP_LONGS			BITS_TO_LONGS(AWCC_ID_BITMAP_SIZE)
58 
59 static bool force_hwmon;
60 module_param_unsafe(force_hwmon, bool, 0);
61 MODULE_PARM_DESC(force_hwmon, "Force probing for HWMON support without checking if the WMI backend is available");
62 
63 static bool force_platform_profile;
64 module_param_unsafe(force_platform_profile, bool, 0);
65 MODULE_PARM_DESC(force_platform_profile, "Forces auto-detecting thermal profiles without checking if WMI thermal backend is available");
66 
67 static bool force_gmode;
68 module_param_unsafe(force_gmode, bool, 0);
69 MODULE_PARM_DESC(force_gmode, "Forces G-Mode when performance profile is selected");
70 
71 struct awcc_quirks {
72 	bool hwmon;
73 	bool pprof;
74 	bool gmode;
75 };
76 
77 static struct awcc_quirks g_series_quirks = {
78 	.hwmon = true,
79 	.pprof = true,
80 	.gmode = true,
81 };
82 
83 static struct awcc_quirks generic_quirks = {
84 	.hwmon = true,
85 	.pprof = true,
86 	.gmode = false,
87 };
88 
89 static struct awcc_quirks empty_quirks;
90 
91 static const struct dmi_system_id awcc_dmi_table[] __initconst = {
92 	{
93 		.ident = "Alienware Area-51m",
94 		.matches = {
95 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
96 			DMI_MATCH(DMI_PRODUCT_NAME, "Alienware Area-51m"),
97 		},
98 		.driver_data = &generic_quirks,
99 	},
100 	{
101 		.ident = "Alienware m15",
102 		.matches = {
103 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
104 			DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m15"),
105 		},
106 		.driver_data = &generic_quirks,
107 	},
108 	{
109 		.ident = "Alienware m16 R1 AMD",
110 		.matches = {
111 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
112 			DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m16 R1 AMD"),
113 		},
114 		.driver_data = &generic_quirks,
115 	},
116 	{
117 		.ident = "Alienware m16 R1",
118 		.matches = {
119 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
120 			DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m16 R1"),
121 		},
122 		.driver_data = &g_series_quirks,
123 	},
124 	{
125 		.ident = "Alienware m16 R2",
126 		.matches = {
127 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
128 			DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m16 R2"),
129 		},
130 		.driver_data = &generic_quirks,
131 	},
132 	{
133 		.ident = "Alienware m17",
134 		.matches = {
135 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
136 			DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m17"),
137 		},
138 		.driver_data = &generic_quirks,
139 	},
140 	{
141 		.ident = "Alienware m18",
142 		.matches = {
143 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
144 			DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m18"),
145 		},
146 		.driver_data = &generic_quirks,
147 	},
148 	{
149 		.ident = "Alienware x15",
150 		.matches = {
151 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
152 			DMI_MATCH(DMI_PRODUCT_NAME, "Alienware x15"),
153 		},
154 		.driver_data = &generic_quirks,
155 	},
156 	{
157 		.ident = "Alienware x17",
158 		.matches = {
159 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
160 			DMI_MATCH(DMI_PRODUCT_NAME, "Alienware x17"),
161 		},
162 		.driver_data = &generic_quirks,
163 	},
164 	{
165 		.ident = "Dell Inc. G15",
166 		.matches = {
167 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
168 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell G15"),
169 		},
170 		.driver_data = &g_series_quirks,
171 	},
172 	{
173 		.ident = "Dell Inc. G16",
174 		.matches = {
175 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
176 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell G16"),
177 		},
178 		.driver_data = &g_series_quirks,
179 	},
180 	{
181 		.ident = "Dell Inc. G3",
182 		.matches = {
183 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
184 			DMI_MATCH(DMI_PRODUCT_NAME, "G3"),
185 		},
186 		.driver_data = &g_series_quirks,
187 	},
188 	{
189 		.ident = "Dell Inc. G5",
190 		.matches = {
191 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
192 			DMI_MATCH(DMI_PRODUCT_NAME, "G5"),
193 		},
194 		.driver_data = &g_series_quirks,
195 	},
196 	{}
197 };
198 
199 enum AWCC_GET_FAN_SENSORS_OPERATIONS {
200 	AWCC_OP_GET_TOTAL_FAN_TEMPS		= 0x01,
201 	AWCC_OP_GET_FAN_TEMP_ID			= 0x02,
202 };
203 
204 enum AWCC_THERMAL_INFORMATION_OPERATIONS {
205 	AWCC_OP_GET_SYSTEM_DESCRIPTION		= 0x02,
206 	AWCC_OP_GET_RESOURCE_ID			= 0x03,
207 	AWCC_OP_GET_TEMPERATURE			= 0x04,
208 	AWCC_OP_GET_FAN_RPM			= 0x05,
209 	AWCC_OP_GET_FAN_MIN_RPM			= 0x08,
210 	AWCC_OP_GET_FAN_MAX_RPM			= 0x09,
211 	AWCC_OP_GET_CURRENT_PROFILE		= 0x0B,
212 	AWCC_OP_GET_FAN_BOOST			= 0x0C,
213 };
214 
215 enum AWCC_THERMAL_CONTROL_OPERATIONS {
216 	AWCC_OP_ACTIVATE_PROFILE		= 0x01,
217 	AWCC_OP_SET_FAN_BOOST			= 0x02,
218 };
219 
220 enum AWCC_GAME_SHIFT_STATUS_OPERATIONS {
221 	AWCC_OP_TOGGLE_GAME_SHIFT		= 0x01,
222 	AWCC_OP_GET_GAME_SHIFT_STATUS		= 0x02,
223 };
224 
225 enum AWCC_THERMAL_TABLES {
226 	AWCC_THERMAL_TABLE_LEGACY		= 0x9,
227 	AWCC_THERMAL_TABLE_USTT			= 0xA,
228 };
229 
230 enum AWCC_SPECIAL_THERMAL_CODES {
231 	AWCC_SPECIAL_PROFILE_CUSTOM		= 0x00,
232 	AWCC_SPECIAL_PROFILE_GMODE		= 0xAB,
233 };
234 
235 enum AWCC_TEMP_SENSOR_TYPES {
236 	AWCC_TEMP_SENSOR_CPU			= 0x01,
237 	AWCC_TEMP_SENSOR_FRONT			= 0x03,
238 	AWCC_TEMP_SENSOR_GPU			= 0x06,
239 };
240 
241 enum AWCC_FAN_TYPES {
242 	AWCC_FAN_CPU_1				= 0x32,
243 	AWCC_FAN_GPU_1				= 0x33,
244 	AWCC_FAN_PCI				= 0x34,
245 	AWCC_FAN_MID				= 0x35,
246 	AWCC_FAN_TOP_1				= 0x36,
247 	AWCC_FAN_SIDE				= 0x37,
248 	AWCC_FAN_U2_1				= 0x38,
249 	AWCC_FAN_U2_2				= 0x39,
250 	AWCC_FAN_FRONT_1			= 0x3A,
251 	AWCC_FAN_CPU_2				= 0x3B,
252 	AWCC_FAN_GPU_2				= 0x3C,
253 	AWCC_FAN_TOP_2				= 0x3D,
254 	AWCC_FAN_TOP_3				= 0x3E,
255 	AWCC_FAN_FRONT_2			= 0x3F,
256 	AWCC_FAN_BOTTOM_1			= 0x40,
257 	AWCC_FAN_BOTTOM_2			= 0x41,
258 };
259 
260 enum awcc_thermal_profile {
261 	AWCC_PROFILE_USTT_BALANCED,
262 	AWCC_PROFILE_USTT_BALANCED_PERFORMANCE,
263 	AWCC_PROFILE_USTT_COOL,
264 	AWCC_PROFILE_USTT_QUIET,
265 	AWCC_PROFILE_USTT_PERFORMANCE,
266 	AWCC_PROFILE_USTT_LOW_POWER,
267 	AWCC_PROFILE_LEGACY_QUIET,
268 	AWCC_PROFILE_LEGACY_BALANCED,
269 	AWCC_PROFILE_LEGACY_BALANCED_PERFORMANCE,
270 	AWCC_PROFILE_LEGACY_PERFORMANCE,
271 	AWCC_PROFILE_LAST,
272 };
273 
274 struct wmax_led_args {
275 	u32 led_mask;
276 	struct color_platform colors;
277 	u8 state;
278 } __packed;
279 
280 struct wmax_brightness_args {
281 	u32 led_mask;
282 	u32 percentage;
283 };
284 
285 struct wmax_basic_args {
286 	u8 arg;
287 };
288 
289 struct wmax_u32_args {
290 	u8 operation;
291 	u8 arg1;
292 	u8 arg2;
293 	u8 arg3;
294 };
295 
296 struct awcc_fan_data {
297 	unsigned long auto_channels_temp;
298 	u32 min_rpm;
299 	u32 max_rpm;
300 	u8 suspend_cache;
301 	u8 id;
302 };
303 
304 struct awcc_priv {
305 	struct wmi_device *wdev;
306 	union {
307 		u32 system_description;
308 		struct {
309 			u8 fan_count;
310 			u8 temp_count;
311 			u8 unknown_count;
312 			u8 profile_count;
313 		};
314 		u8 res_count[4];
315 	};
316 
317 	struct device *ppdev;
318 	u8 supported_profiles[PLATFORM_PROFILE_LAST];
319 
320 	struct device *hwdev;
321 	struct awcc_fan_data **fan_data;
322 	unsigned long temp_sensors[AWCC_ID_BITMAP_LONGS];
323 
324 	u32 gpio_count;
325 };
326 
327 static const enum platform_profile_option awcc_mode_to_platform_profile[AWCC_PROFILE_LAST] = {
328 	[AWCC_PROFILE_USTT_BALANCED]			= PLATFORM_PROFILE_BALANCED,
329 	[AWCC_PROFILE_USTT_BALANCED_PERFORMANCE]	= PLATFORM_PROFILE_BALANCED_PERFORMANCE,
330 	[AWCC_PROFILE_USTT_COOL]			= PLATFORM_PROFILE_COOL,
331 	[AWCC_PROFILE_USTT_QUIET]			= PLATFORM_PROFILE_QUIET,
332 	[AWCC_PROFILE_USTT_PERFORMANCE]			= PLATFORM_PROFILE_PERFORMANCE,
333 	[AWCC_PROFILE_USTT_LOW_POWER]			= PLATFORM_PROFILE_LOW_POWER,
334 	[AWCC_PROFILE_LEGACY_QUIET]			= PLATFORM_PROFILE_QUIET,
335 	[AWCC_PROFILE_LEGACY_BALANCED]			= PLATFORM_PROFILE_BALANCED,
336 	[AWCC_PROFILE_LEGACY_BALANCED_PERFORMANCE]	= PLATFORM_PROFILE_BALANCED_PERFORMANCE,
337 	[AWCC_PROFILE_LEGACY_PERFORMANCE]		= PLATFORM_PROFILE_PERFORMANCE,
338 };
339 
340 static struct awcc_quirks *awcc;
341 
342 /*
343  *	The HDMI mux sysfs node indicates the status of the HDMI input mux.
344  *	It can toggle between standard system GPU output and HDMI input.
345  */
346 static ssize_t cable_show(struct device *dev, struct device_attribute *attr,
347 			  char *buf)
348 {
349 	struct alienfx_platdata *pdata = dev_get_platdata(dev);
350 	struct wmax_basic_args in_args = {
351 		.arg = 0,
352 	};
353 	u32 out_data;
354 	int ret;
355 
356 	ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_HDMI_CABLE,
357 				    &in_args, sizeof(in_args), &out_data);
358 	if (!ret) {
359 		if (out_data == 0)
360 			return sysfs_emit(buf, "[unconnected] connected unknown\n");
361 		else if (out_data == 1)
362 			return sysfs_emit(buf, "unconnected [connected] unknown\n");
363 	}
364 
365 	pr_err("alienware-wmi: unknown HDMI cable status: %d\n", ret);
366 	return sysfs_emit(buf, "unconnected connected [unknown]\n");
367 }
368 
369 static ssize_t source_show(struct device *dev, struct device_attribute *attr,
370 			   char *buf)
371 {
372 	struct alienfx_platdata *pdata = dev_get_platdata(dev);
373 	struct wmax_basic_args in_args = {
374 		.arg = 0,
375 	};
376 	u32 out_data;
377 	int ret;
378 
379 	ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_HDMI_STATUS,
380 				    &in_args, sizeof(in_args), &out_data);
381 	if (!ret) {
382 		if (out_data == 1)
383 			return sysfs_emit(buf, "[input] gpu unknown\n");
384 		else if (out_data == 2)
385 			return sysfs_emit(buf, "input [gpu] unknown\n");
386 	}
387 
388 	pr_err("alienware-wmi: unknown HDMI source status: %u\n", ret);
389 	return sysfs_emit(buf, "input gpu [unknown]\n");
390 }
391 
392 static ssize_t source_store(struct device *dev, struct device_attribute *attr,
393 			    const char *buf, size_t count)
394 {
395 	struct alienfx_platdata *pdata = dev_get_platdata(dev);
396 	struct wmax_basic_args args;
397 	int ret;
398 
399 	if (strcmp(buf, "gpu\n") == 0)
400 		args.arg = 1;
401 	else if (strcmp(buf, "input\n") == 0)
402 		args.arg = 2;
403 	else
404 		args.arg = 3;
405 	pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
406 
407 	ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_HDMI_SOURCE, &args,
408 				    sizeof(args), NULL);
409 	if (ret < 0)
410 		pr_err("alienware-wmi: HDMI toggle failed: results: %u\n", ret);
411 
412 	return count;
413 }
414 
415 static DEVICE_ATTR_RO(cable);
416 static DEVICE_ATTR_RW(source);
417 
418 static bool hdmi_group_visible(struct kobject *kobj)
419 {
420 	return alienware_interface == WMAX && alienfx->hdmi_mux;
421 }
422 DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(hdmi);
423 
424 static struct attribute *hdmi_attrs[] = {
425 	&dev_attr_cable.attr,
426 	&dev_attr_source.attr,
427 	NULL,
428 };
429 
430 const struct attribute_group wmax_hdmi_attribute_group = {
431 	.name = "hdmi",
432 	.is_visible = SYSFS_GROUP_VISIBLE(hdmi),
433 	.attrs = hdmi_attrs,
434 };
435 
436 /*
437  * Alienware GFX amplifier support
438  * - Currently supports reading cable status
439  * - Leaving expansion room to possibly support dock/undock events later
440  */
441 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
442 			   char *buf)
443 {
444 	struct alienfx_platdata *pdata = dev_get_platdata(dev);
445 	struct wmax_basic_args in_args = {
446 		.arg = 0,
447 	};
448 	u32 out_data;
449 	int ret;
450 
451 	ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_AMPLIFIER_CABLE,
452 				    &in_args, sizeof(in_args), &out_data);
453 	if (!ret) {
454 		if (out_data == 0)
455 			return sysfs_emit(buf, "[unconnected] connected unknown\n");
456 		else if (out_data == 1)
457 			return sysfs_emit(buf, "unconnected [connected] unknown\n");
458 	}
459 
460 	pr_err("alienware-wmi: unknown amplifier cable status: %d\n", ret);
461 	return sysfs_emit(buf, "unconnected connected [unknown]\n");
462 }
463 
464 static DEVICE_ATTR_RO(status);
465 
466 static bool amplifier_group_visible(struct kobject *kobj)
467 {
468 	return alienware_interface == WMAX && alienfx->amplifier;
469 }
470 DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(amplifier);
471 
472 static struct attribute *amplifier_attrs[] = {
473 	&dev_attr_status.attr,
474 	NULL,
475 };
476 
477 const struct attribute_group wmax_amplifier_attribute_group = {
478 	.name = "amplifier",
479 	.is_visible = SYSFS_GROUP_VISIBLE(amplifier),
480 	.attrs = amplifier_attrs,
481 };
482 
483 /*
484  * Deep Sleep Control support
485  * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
486  */
487 static ssize_t deepsleep_show(struct device *dev, struct device_attribute *attr,
488 			      char *buf)
489 {
490 	struct alienfx_platdata *pdata = dev_get_platdata(dev);
491 	struct wmax_basic_args in_args = {
492 		.arg = 0,
493 	};
494 	u32 out_data;
495 	int ret;
496 
497 	ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_DEEP_SLEEP_STATUS,
498 				    &in_args, sizeof(in_args), &out_data);
499 	if (!ret) {
500 		if (out_data == 0)
501 			return sysfs_emit(buf, "[disabled] s5 s5_s4\n");
502 		else if (out_data == 1)
503 			return sysfs_emit(buf, "disabled [s5] s5_s4\n");
504 		else if (out_data == 2)
505 			return sysfs_emit(buf, "disabled s5 [s5_s4]\n");
506 	}
507 
508 	pr_err("alienware-wmi: unknown deep sleep status: %d\n", ret);
509 	return sysfs_emit(buf, "disabled s5 s5_s4 [unknown]\n");
510 }
511 
512 static ssize_t deepsleep_store(struct device *dev, struct device_attribute *attr,
513 			       const char *buf, size_t count)
514 {
515 	struct alienfx_platdata *pdata = dev_get_platdata(dev);
516 	struct wmax_basic_args args;
517 	int ret;
518 
519 	if (strcmp(buf, "disabled\n") == 0)
520 		args.arg = 0;
521 	else if (strcmp(buf, "s5\n") == 0)
522 		args.arg = 1;
523 	else
524 		args.arg = 2;
525 	pr_debug("alienware-wmi: setting deep sleep to %d : %s", args.arg, buf);
526 
527 	ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_DEEP_SLEEP_CONTROL,
528 				    &args, sizeof(args), NULL);
529 	if (!ret)
530 		pr_err("alienware-wmi: deep sleep control failed: results: %u\n", ret);
531 
532 	return count;
533 }
534 
535 static DEVICE_ATTR_RW(deepsleep);
536 
537 static bool deepsleep_group_visible(struct kobject *kobj)
538 {
539 	return alienware_interface == WMAX && alienfx->deepslp;
540 }
541 DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(deepsleep);
542 
543 static struct attribute *deepsleep_attrs[] = {
544 	&dev_attr_deepsleep.attr,
545 	NULL,
546 };
547 
548 const struct attribute_group wmax_deepsleep_attribute_group = {
549 	.name = "deepsleep",
550 	.is_visible = SYSFS_GROUP_VISIBLE(deepsleep),
551 	.attrs = deepsleep_attrs,
552 };
553 
554 /*
555  * AWCC Helpers
556  */
557 static bool is_awcc_thermal_profile_id(u8 code)
558 {
559 	u8 table = FIELD_GET(AWCC_THERMAL_TABLE_MASK, code);
560 	u8 mode = FIELD_GET(AWCC_THERMAL_MODE_MASK, code);
561 
562 	if (mode >= AWCC_PROFILE_LAST)
563 		return false;
564 
565 	if (table == AWCC_THERMAL_TABLE_LEGACY && mode >= AWCC_PROFILE_LEGACY_QUIET)
566 		return true;
567 
568 	if (table == AWCC_THERMAL_TABLE_USTT && mode <= AWCC_PROFILE_USTT_LOW_POWER)
569 		return true;
570 
571 	return false;
572 }
573 
574 static int awcc_wmi_command(struct wmi_device *wdev, u32 method_id,
575 			    struct wmax_u32_args *args, u32 *out)
576 {
577 	int ret;
578 
579 	ret = alienware_wmi_command(wdev, method_id, args, sizeof(*args), out);
580 	if (ret)
581 		return ret;
582 
583 	if (*out == AWCC_FAILURE_CODE || *out == AWCC_FAILURE_CODE_2)
584 		return -EBADRQC;
585 
586 	return 0;
587 }
588 
589 static int awcc_get_fan_sensors(struct wmi_device *wdev, u8 operation,
590 				u8 fan_id, u8 index, u32 *out)
591 {
592 	struct wmax_u32_args args = {
593 		.operation = operation,
594 		.arg1 = fan_id,
595 		.arg2 = index,
596 		.arg3 = 0,
597 	};
598 
599 	return awcc_wmi_command(wdev, AWCC_METHOD_GET_FAN_SENSORS, &args, out);
600 }
601 
602 static int awcc_thermal_information(struct wmi_device *wdev, u8 operation, u8 arg,
603 				    u32 *out)
604 {
605 	struct wmax_u32_args args = {
606 		.operation = operation,
607 		.arg1 = arg,
608 		.arg2 = 0,
609 		.arg3 = 0,
610 	};
611 
612 	return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
613 }
614 
615 static int awcc_fwup_gpio_control(struct wmi_device *wdev, u8 pin, u8 status)
616 {
617 	struct wmax_u32_args args = {
618 		.operation = pin,
619 		.arg1 = status,
620 		.arg2 = 0,
621 		.arg3 = 0,
622 	};
623 	u32 out;
624 
625 	return awcc_wmi_command(wdev, AWCC_METHOD_FWUP_GPIO_CONTROL, &args, &out);
626 }
627 
628 static int awcc_read_total_gpios(struct wmi_device *wdev, u32 *count)
629 {
630 	struct wmax_u32_args args = {};
631 
632 	return awcc_wmi_command(wdev, AWCC_METHOD_READ_TOTAL_GPIOS, &args, count);
633 }
634 
635 static int awcc_read_gpio_status(struct wmi_device *wdev, u8 pin, u32 *status)
636 {
637 	struct wmax_u32_args args = {
638 		.operation = pin,
639 		.arg1 = 0,
640 		.arg2 = 0,
641 		.arg3 = 0,
642 	};
643 
644 	return awcc_wmi_command(wdev, AWCC_METHOD_READ_GPIO_STATUS, &args, status);
645 }
646 
647 static int awcc_game_shift_status(struct wmi_device *wdev, u8 operation,
648 				  u32 *out)
649 {
650 	struct wmax_u32_args args = {
651 		.operation = operation,
652 		.arg1 = 0,
653 		.arg2 = 0,
654 		.arg3 = 0,
655 	};
656 
657 	return awcc_wmi_command(wdev, AWCC_METHOD_GAME_SHIFT_STATUS, &args, out);
658 }
659 
660 /**
661  * awcc_op_get_resource_id - Get the resource ID at a given index
662  * @wdev: AWCC WMI device
663  * @index: Index
664  * @out: Value returned by the WMI call
665  *
666  * Get the resource ID at a given @index. Resource IDs are listed in the
667  * following order:
668  *
669  *	- Fan IDs
670  *	- Sensor IDs
671  *	- Unknown IDs
672  *	- Thermal Profile IDs
673  *
674  * The total number of IDs of a given type can be obtained with
675  * AWCC_OP_GET_SYSTEM_DESCRIPTION.
676  *
677  * Return: 0 on success, -errno on failure
678  */
679 static int awcc_op_get_resource_id(struct wmi_device *wdev, u8 index, u8 *out)
680 {
681 	struct wmax_u32_args args = {
682 		.operation = AWCC_OP_GET_RESOURCE_ID,
683 		.arg1 = index,
684 		.arg2 = 0,
685 		.arg3 = 0,
686 	};
687 	u32 out_data;
688 	int ret;
689 
690 	ret = awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, &out_data);
691 	if (ret)
692 		return ret;
693 
694 	*out = FIELD_GET(AWCC_RESOURCE_ID_MASK, out_data);
695 
696 	return 0;
697 }
698 
699 static int awcc_op_get_fan_rpm(struct wmi_device *wdev, u8 fan_id, u32 *out)
700 {
701 	struct wmax_u32_args args = {
702 		.operation = AWCC_OP_GET_FAN_RPM,
703 		.arg1 = fan_id,
704 		.arg2 = 0,
705 		.arg3 = 0,
706 	};
707 
708 	return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
709 }
710 
711 static int awcc_op_get_temperature(struct wmi_device *wdev, u8 temp_id, u32 *out)
712 {
713 	struct wmax_u32_args args = {
714 		.operation = AWCC_OP_GET_TEMPERATURE,
715 		.arg1 = temp_id,
716 		.arg2 = 0,
717 		.arg3 = 0,
718 	};
719 
720 	return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
721 }
722 
723 static int awcc_op_get_fan_boost(struct wmi_device *wdev, u8 fan_id, u32 *out)
724 {
725 	struct wmax_u32_args args = {
726 		.operation = AWCC_OP_GET_FAN_BOOST,
727 		.arg1 = fan_id,
728 		.arg2 = 0,
729 		.arg3 = 0,
730 	};
731 
732 	return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
733 }
734 
735 static int awcc_op_get_current_profile(struct wmi_device *wdev, u32 *out)
736 {
737 	struct wmax_u32_args args = {
738 		.operation = AWCC_OP_GET_CURRENT_PROFILE,
739 		.arg1 = 0,
740 		.arg2 = 0,
741 		.arg3 = 0,
742 	};
743 
744 	return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
745 }
746 
747 static int awcc_op_activate_profile(struct wmi_device *wdev, u8 profile)
748 {
749 	struct wmax_u32_args args = {
750 		.operation = AWCC_OP_ACTIVATE_PROFILE,
751 		.arg1 = profile,
752 		.arg2 = 0,
753 		.arg3 = 0,
754 	};
755 	u32 out;
756 
757 	return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_CONTROL, &args, &out);
758 }
759 
760 static int awcc_op_set_fan_boost(struct wmi_device *wdev, u8 fan_id, u8 boost)
761 {
762 	struct wmax_u32_args args = {
763 		.operation = AWCC_OP_SET_FAN_BOOST,
764 		.arg1 = fan_id,
765 		.arg2 = boost,
766 		.arg3 = 0,
767 	};
768 	u32 out;
769 
770 	return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_CONTROL, &args, &out);
771 }
772 
773 /*
774  * HWMON
775  *  - Provides temperature and fan speed monitoring as well as manual fan
776  *    control
777  */
778 static umode_t awcc_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
779 				     u32 attr, int channel)
780 {
781 	const struct awcc_priv *priv = drvdata;
782 	unsigned int temp_count;
783 
784 	switch (type) {
785 	case hwmon_temp:
786 		temp_count = bitmap_weight(priv->temp_sensors, AWCC_ID_BITMAP_SIZE);
787 
788 		return channel < temp_count ? 0444 : 0;
789 	case hwmon_fan:
790 		return channel < priv->fan_count ? 0444 : 0;
791 	case hwmon_pwm:
792 		return channel < priv->fan_count ? 0444 : 0;
793 	default:
794 		return 0;
795 	}
796 }
797 
798 static int awcc_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
799 			   u32 attr, int channel, long *val)
800 {
801 	struct awcc_priv *priv = dev_get_drvdata(dev);
802 	const struct awcc_fan_data *fan;
803 	u32 state;
804 	int ret;
805 	u8 temp;
806 
807 	switch (type) {
808 	case hwmon_temp:
809 		temp = find_nth_bit(priv->temp_sensors, AWCC_ID_BITMAP_SIZE, channel);
810 
811 		switch (attr) {
812 		case hwmon_temp_input:
813 			ret = awcc_op_get_temperature(priv->wdev, temp, &state);
814 			if (ret)
815 				return ret;
816 
817 			*val = state * MILLIDEGREE_PER_DEGREE;
818 			break;
819 		default:
820 			return -EOPNOTSUPP;
821 		}
822 
823 		break;
824 	case hwmon_fan:
825 		fan = priv->fan_data[channel];
826 
827 		switch (attr) {
828 		case hwmon_fan_input:
829 			ret = awcc_op_get_fan_rpm(priv->wdev, fan->id, &state);
830 			if (ret)
831 				return ret;
832 
833 			*val = state;
834 			break;
835 		case hwmon_fan_min:
836 			*val = fan->min_rpm;
837 			break;
838 		case hwmon_fan_max:
839 			*val = fan->max_rpm;
840 			break;
841 		default:
842 			return -EOPNOTSUPP;
843 		}
844 
845 		break;
846 	case hwmon_pwm:
847 		fan = priv->fan_data[channel];
848 
849 		switch (attr) {
850 		case hwmon_pwm_auto_channels_temp:
851 			*val = fan->auto_channels_temp;
852 			break;
853 		default:
854 			return -EOPNOTSUPP;
855 		}
856 
857 		break;
858 	default:
859 		return -EOPNOTSUPP;
860 	}
861 
862 	return 0;
863 }
864 
865 static int awcc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
866 				  u32 attr, int channel, const char **str)
867 {
868 	struct awcc_priv *priv = dev_get_drvdata(dev);
869 	u8 temp;
870 
871 	switch (type) {
872 	case hwmon_temp:
873 		temp = find_nth_bit(priv->temp_sensors, AWCC_ID_BITMAP_SIZE, channel);
874 
875 		switch (temp) {
876 		case AWCC_TEMP_SENSOR_CPU:
877 			*str = "CPU";
878 			break;
879 		case AWCC_TEMP_SENSOR_FRONT:
880 			*str = "Front";
881 			break;
882 		case AWCC_TEMP_SENSOR_GPU:
883 			*str = "GPU";
884 			break;
885 		default:
886 			*str = "Unknown";
887 			break;
888 		}
889 
890 		break;
891 	case hwmon_fan:
892 		switch (priv->fan_data[channel]->id) {
893 		case AWCC_FAN_CPU_1:
894 		case AWCC_FAN_CPU_2:
895 			*str = "CPU Fan";
896 			break;
897 		case AWCC_FAN_GPU_1:
898 		case AWCC_FAN_GPU_2:
899 			*str = "GPU Fan";
900 			break;
901 		case AWCC_FAN_PCI:
902 			*str = "PCI Fan";
903 			break;
904 		case AWCC_FAN_MID:
905 			*str = "Mid Fan";
906 			break;
907 		case AWCC_FAN_TOP_1:
908 		case AWCC_FAN_TOP_2:
909 		case AWCC_FAN_TOP_3:
910 			*str = "Top Fan";
911 			break;
912 		case AWCC_FAN_SIDE:
913 			*str = "Side Fan";
914 			break;
915 		case AWCC_FAN_U2_1:
916 		case AWCC_FAN_U2_2:
917 			*str = "U.2 Fan";
918 			break;
919 		case AWCC_FAN_FRONT_1:
920 		case AWCC_FAN_FRONT_2:
921 			*str = "Front Fan";
922 			break;
923 		case AWCC_FAN_BOTTOM_1:
924 		case AWCC_FAN_BOTTOM_2:
925 			*str = "Bottom Fan";
926 			break;
927 		default:
928 			*str = "Unknown Fan";
929 			break;
930 		}
931 
932 		break;
933 	default:
934 		return -EOPNOTSUPP;
935 	}
936 
937 	return 0;
938 }
939 
940 static const struct hwmon_ops awcc_hwmon_ops = {
941 	.is_visible = awcc_hwmon_is_visible,
942 	.read = awcc_hwmon_read,
943 	.read_string = awcc_hwmon_read_string,
944 };
945 
946 static const struct hwmon_channel_info * const awcc_hwmon_info[] = {
947 	HWMON_CHANNEL_INFO(temp,
948 			   HWMON_T_LABEL | HWMON_T_INPUT,
949 			   HWMON_T_LABEL | HWMON_T_INPUT,
950 			   HWMON_T_LABEL | HWMON_T_INPUT,
951 			   HWMON_T_LABEL | HWMON_T_INPUT,
952 			   HWMON_T_LABEL | HWMON_T_INPUT,
953 			   HWMON_T_LABEL | HWMON_T_INPUT
954 			   ),
955 	HWMON_CHANNEL_INFO(fan,
956 			   HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
957 			   HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
958 			   HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
959 			   HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
960 			   HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
961 			   HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX
962 			   ),
963 	HWMON_CHANNEL_INFO(pwm,
964 			   HWMON_PWM_AUTO_CHANNELS_TEMP,
965 			   HWMON_PWM_AUTO_CHANNELS_TEMP,
966 			   HWMON_PWM_AUTO_CHANNELS_TEMP,
967 			   HWMON_PWM_AUTO_CHANNELS_TEMP,
968 			   HWMON_PWM_AUTO_CHANNELS_TEMP,
969 			   HWMON_PWM_AUTO_CHANNELS_TEMP
970 			   ),
971 	NULL
972 };
973 
974 static const struct hwmon_chip_info awcc_hwmon_chip_info = {
975 	.ops = &awcc_hwmon_ops,
976 	.info = awcc_hwmon_info,
977 };
978 
979 static ssize_t fan_boost_show(struct device *dev, struct device_attribute *attr,
980 			      char *buf)
981 {
982 	struct awcc_priv *priv = dev_get_drvdata(dev);
983 	int index = to_sensor_dev_attr(attr)->index;
984 	struct awcc_fan_data *fan = priv->fan_data[index];
985 	u32 boost;
986 	int ret;
987 
988 	ret = awcc_op_get_fan_boost(priv->wdev, fan->id, &boost);
989 	if (ret)
990 		return ret;
991 
992 	return sysfs_emit(buf, "%u\n", boost);
993 }
994 
995 static ssize_t fan_boost_store(struct device *dev, struct device_attribute *attr,
996 			       const char *buf, size_t count)
997 {
998 	struct awcc_priv *priv = dev_get_drvdata(dev);
999 	int index = to_sensor_dev_attr(attr)->index;
1000 	struct awcc_fan_data *fan = priv->fan_data[index];
1001 	unsigned long val;
1002 	int ret;
1003 
1004 	ret = kstrtoul(buf, 0, &val);
1005 	if (ret)
1006 		return ret;
1007 
1008 	ret = awcc_op_set_fan_boost(priv->wdev, fan->id, clamp_val(val, 0, 255));
1009 
1010 	return ret ? ret : count;
1011 }
1012 
1013 static SENSOR_DEVICE_ATTR_RW(fan1_boost, fan_boost, 0);
1014 static SENSOR_DEVICE_ATTR_RW(fan2_boost, fan_boost, 1);
1015 static SENSOR_DEVICE_ATTR_RW(fan3_boost, fan_boost, 2);
1016 static SENSOR_DEVICE_ATTR_RW(fan4_boost, fan_boost, 3);
1017 static SENSOR_DEVICE_ATTR_RW(fan5_boost, fan_boost, 4);
1018 static SENSOR_DEVICE_ATTR_RW(fan6_boost, fan_boost, 5);
1019 
1020 static umode_t fan_boost_attr_visible(struct kobject *kobj, struct attribute *attr, int n)
1021 {
1022 	struct awcc_priv *priv = dev_get_drvdata(kobj_to_dev(kobj));
1023 
1024 	return n < priv->fan_count ? attr->mode : 0;
1025 }
1026 
1027 static bool fan_boost_group_visible(struct kobject *kobj)
1028 {
1029 	return true;
1030 }
1031 
1032 DEFINE_SYSFS_GROUP_VISIBLE(fan_boost);
1033 
1034 static struct attribute *fan_boost_attrs[] = {
1035 	&sensor_dev_attr_fan1_boost.dev_attr.attr,
1036 	&sensor_dev_attr_fan2_boost.dev_attr.attr,
1037 	&sensor_dev_attr_fan3_boost.dev_attr.attr,
1038 	&sensor_dev_attr_fan4_boost.dev_attr.attr,
1039 	&sensor_dev_attr_fan5_boost.dev_attr.attr,
1040 	&sensor_dev_attr_fan6_boost.dev_attr.attr,
1041 	NULL
1042 };
1043 
1044 static const struct attribute_group fan_boost_group = {
1045 	.attrs = fan_boost_attrs,
1046 	.is_visible = SYSFS_GROUP_VISIBLE(fan_boost),
1047 };
1048 
1049 static const struct attribute_group *awcc_hwmon_groups[] = {
1050 	&fan_boost_group,
1051 	NULL
1052 };
1053 
1054 static int awcc_hwmon_temps_init(struct wmi_device *wdev)
1055 {
1056 	struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
1057 	unsigned int i;
1058 	int ret;
1059 	u8 id;
1060 
1061 	for (i = 0; i < priv->temp_count; i++) {
1062 		/*
1063 		 * Temperature sensors IDs are listed after the fan IDs at
1064 		 * offset `fan_count`
1065 		 */
1066 		ret = awcc_op_get_resource_id(wdev, i + priv->fan_count, &id);
1067 		if (ret)
1068 			return ret;
1069 
1070 		__set_bit(id, priv->temp_sensors);
1071 	}
1072 
1073 	return 0;
1074 }
1075 
1076 static int awcc_hwmon_fans_init(struct wmi_device *wdev)
1077 {
1078 	struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
1079 	unsigned long fan_temps[AWCC_ID_BITMAP_LONGS];
1080 	unsigned long gather[AWCC_ID_BITMAP_LONGS];
1081 	u32 min_rpm, max_rpm, temp_count, temp_id;
1082 	struct awcc_fan_data *fan_data;
1083 	unsigned int i, j;
1084 	int ret;
1085 	u8 id;
1086 
1087 	for (i = 0; i < priv->fan_count; i++) {
1088 		fan_data = devm_kzalloc(&wdev->dev, sizeof(*fan_data), GFP_KERNEL);
1089 		if (!fan_data)
1090 			return -ENOMEM;
1091 
1092 		/*
1093 		 * Fan IDs are listed first at offset 0
1094 		 */
1095 		ret = awcc_op_get_resource_id(wdev, i, &id);
1096 		if (ret)
1097 			return ret;
1098 
1099 		ret = awcc_thermal_information(wdev, AWCC_OP_GET_FAN_MIN_RPM, id,
1100 					       &min_rpm);
1101 		if (ret)
1102 			return ret;
1103 
1104 		ret = awcc_thermal_information(wdev, AWCC_OP_GET_FAN_MAX_RPM, id,
1105 					       &max_rpm);
1106 		if (ret)
1107 			return ret;
1108 
1109 		ret = awcc_get_fan_sensors(wdev, AWCC_OP_GET_TOTAL_FAN_TEMPS, id,
1110 					   0, &temp_count);
1111 		if (ret)
1112 			return ret;
1113 
1114 		bitmap_zero(fan_temps, AWCC_ID_BITMAP_SIZE);
1115 
1116 		for (j = 0; j < temp_count; j++) {
1117 			ret = awcc_get_fan_sensors(wdev, AWCC_OP_GET_FAN_TEMP_ID,
1118 						   id, j, &temp_id);
1119 			if (ret)
1120 				break;
1121 
1122 			temp_id = FIELD_GET(AWCC_RESOURCE_ID_MASK, temp_id);
1123 			__set_bit(temp_id, fan_temps);
1124 		}
1125 
1126 		fan_data->id = id;
1127 		fan_data->min_rpm = min_rpm;
1128 		fan_data->max_rpm = max_rpm;
1129 		bitmap_gather(gather, fan_temps, priv->temp_sensors, AWCC_ID_BITMAP_SIZE);
1130 		bitmap_copy(&fan_data->auto_channels_temp, gather, BITS_PER_LONG);
1131 		priv->fan_data[i] = fan_data;
1132 	}
1133 
1134 	return 0;
1135 }
1136 
1137 static int awcc_hwmon_init(struct wmi_device *wdev)
1138 {
1139 	struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
1140 	int ret;
1141 
1142 	priv->fan_data = devm_kcalloc(&wdev->dev, priv->fan_count,
1143 				      sizeof(*priv->fan_data), GFP_KERNEL);
1144 	if (!priv->fan_data)
1145 		return -ENOMEM;
1146 
1147 	ret = awcc_hwmon_temps_init(wdev);
1148 	if (ret)
1149 		return ret;
1150 
1151 	ret = awcc_hwmon_fans_init(wdev);
1152 	if (ret)
1153 		return ret;
1154 
1155 	priv->hwdev = devm_hwmon_device_register_with_info(&wdev->dev, "alienware_wmi",
1156 							   priv, &awcc_hwmon_chip_info,
1157 							   awcc_hwmon_groups);
1158 
1159 	return PTR_ERR_OR_ZERO(priv->hwdev);
1160 }
1161 
1162 static void awcc_hwmon_suspend(struct device *dev)
1163 {
1164 	struct awcc_priv *priv = dev_get_drvdata(dev);
1165 	struct awcc_fan_data *fan;
1166 	unsigned int i;
1167 	u32 boost;
1168 	int ret;
1169 
1170 	for (i = 0; i < priv->fan_count; i++) {
1171 		fan = priv->fan_data[i];
1172 
1173 		ret = awcc_thermal_information(priv->wdev, AWCC_OP_GET_FAN_BOOST,
1174 					       fan->id, &boost);
1175 		if (ret)
1176 			dev_err(dev, "Failed to store Fan %u boost while suspending\n", i);
1177 
1178 		fan->suspend_cache = ret ? 0 : clamp_val(boost, 0, 255);
1179 
1180 		awcc_op_set_fan_boost(priv->wdev, fan->id, 0);
1181 		if (ret)
1182 			dev_err(dev, "Failed to set Fan %u boost to 0 while suspending\n", i);
1183 	}
1184 }
1185 
1186 static void awcc_hwmon_resume(struct device *dev)
1187 {
1188 	struct awcc_priv *priv = dev_get_drvdata(dev);
1189 	struct awcc_fan_data *fan;
1190 	unsigned int i;
1191 	int ret;
1192 
1193 	for (i = 0; i < priv->fan_count; i++) {
1194 		fan = priv->fan_data[i];
1195 
1196 		if (!fan->suspend_cache)
1197 			continue;
1198 
1199 		ret = awcc_op_set_fan_boost(priv->wdev, fan->id, fan->suspend_cache);
1200 		if (ret)
1201 			dev_err(dev, "Failed to restore Fan %u boost while resuming\n", i);
1202 	}
1203 }
1204 
1205 /*
1206  * Thermal Profile control
1207  *  - Provides thermal profile control through the Platform Profile API
1208  */
1209 static int awcc_platform_profile_get(struct device *dev,
1210 				     enum platform_profile_option *profile)
1211 {
1212 	struct awcc_priv *priv = dev_get_drvdata(dev);
1213 	u32 out_data;
1214 	int ret;
1215 
1216 	ret = awcc_op_get_current_profile(priv->wdev, &out_data);
1217 	if (ret)
1218 		return ret;
1219 
1220 	switch (out_data) {
1221 	case AWCC_SPECIAL_PROFILE_CUSTOM:
1222 		*profile = PLATFORM_PROFILE_CUSTOM;
1223 		return 0;
1224 	case AWCC_SPECIAL_PROFILE_GMODE:
1225 		*profile = PLATFORM_PROFILE_PERFORMANCE;
1226 		return 0;
1227 	default:
1228 		break;
1229 	}
1230 
1231 	if (!is_awcc_thermal_profile_id(out_data))
1232 		return -ENODATA;
1233 
1234 	out_data = FIELD_GET(AWCC_THERMAL_MODE_MASK, out_data);
1235 	*profile = awcc_mode_to_platform_profile[out_data];
1236 
1237 	return 0;
1238 }
1239 
1240 static int awcc_platform_profile_set(struct device *dev,
1241 				     enum platform_profile_option profile)
1242 {
1243 	struct awcc_priv *priv = dev_get_drvdata(dev);
1244 
1245 	if (awcc->gmode) {
1246 		u32 gmode_status;
1247 		int ret;
1248 
1249 		ret = awcc_game_shift_status(priv->wdev,
1250 					     AWCC_OP_GET_GAME_SHIFT_STATUS,
1251 					     &gmode_status);
1252 
1253 		if (ret < 0)
1254 			return ret;
1255 
1256 		if ((profile == PLATFORM_PROFILE_PERFORMANCE && !gmode_status) ||
1257 		    (profile != PLATFORM_PROFILE_PERFORMANCE && gmode_status)) {
1258 			ret = awcc_game_shift_status(priv->wdev,
1259 						     AWCC_OP_TOGGLE_GAME_SHIFT,
1260 						     &gmode_status);
1261 
1262 			if (ret < 0)
1263 				return ret;
1264 		}
1265 	}
1266 
1267 	return awcc_op_activate_profile(priv->wdev, priv->supported_profiles[profile]);
1268 }
1269 
1270 static int awcc_platform_profile_probe(void *drvdata, unsigned long *choices)
1271 {
1272 	enum platform_profile_option profile;
1273 	struct awcc_priv *priv = drvdata;
1274 	enum awcc_thermal_profile mode;
1275 	u8 id, offset = 0;
1276 	int ret;
1277 
1278 	/*
1279 	 * Thermal profile IDs are listed last at offset
1280 	 *	fan_count + temp_count + unknown_count
1281 	 */
1282 	for (unsigned int i = 0; i < ARRAY_SIZE(priv->res_count) - 1; i++)
1283 		offset += priv->res_count[i];
1284 
1285 	for (unsigned int i = 0; i < priv->profile_count; i++) {
1286 		ret = awcc_op_get_resource_id(priv->wdev, i + offset, &id);
1287 		/*
1288 		 * Some devices report an incorrect number of thermal profiles
1289 		 * so the resource ID list may end prematurely
1290 		 */
1291 		if (ret == -EBADRQC)
1292 			break;
1293 		if (ret)
1294 			return ret;
1295 
1296 		if (!is_awcc_thermal_profile_id(id)) {
1297 			dev_dbg(&priv->wdev->dev, "Unmapped thermal profile ID 0x%02x\n", id);
1298 			continue;
1299 		}
1300 
1301 		mode = FIELD_GET(AWCC_THERMAL_MODE_MASK, id);
1302 		profile = awcc_mode_to_platform_profile[mode];
1303 		priv->supported_profiles[profile] = id;
1304 
1305 		__set_bit(profile, choices);
1306 	}
1307 
1308 	if (bitmap_empty(choices, PLATFORM_PROFILE_LAST))
1309 		return -ENODEV;
1310 
1311 	if (awcc->gmode) {
1312 		priv->supported_profiles[PLATFORM_PROFILE_PERFORMANCE] =
1313 			AWCC_SPECIAL_PROFILE_GMODE;
1314 
1315 		__set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
1316 	}
1317 
1318 	/* Every model supports the "custom" profile */
1319 	priv->supported_profiles[PLATFORM_PROFILE_CUSTOM] =
1320 		AWCC_SPECIAL_PROFILE_CUSTOM;
1321 
1322 	__set_bit(PLATFORM_PROFILE_CUSTOM, choices);
1323 
1324 	return 0;
1325 }
1326 
1327 static const struct platform_profile_ops awcc_platform_profile_ops = {
1328 	.probe = awcc_platform_profile_probe,
1329 	.profile_get = awcc_platform_profile_get,
1330 	.profile_set = awcc_platform_profile_set,
1331 };
1332 
1333 static int awcc_platform_profile_init(struct wmi_device *wdev)
1334 {
1335 	struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
1336 
1337 	priv->ppdev = devm_platform_profile_register(&wdev->dev, "alienware-wmi",
1338 						     priv, &awcc_platform_profile_ops);
1339 
1340 	return PTR_ERR_OR_ZERO(priv->ppdev);
1341 }
1342 
1343 /*
1344  * DebugFS
1345  */
1346 static int awcc_debugfs_system_description_read(struct seq_file *seq, void *data)
1347 {
1348 	struct device *dev = seq->private;
1349 	struct awcc_priv *priv = dev_get_drvdata(dev);
1350 
1351 	seq_printf(seq, "0x%08x\n", priv->system_description);
1352 
1353 	return 0;
1354 }
1355 
1356 static int awcc_debugfs_hwmon_data_read(struct seq_file *seq, void *data)
1357 {
1358 	struct device *dev = seq->private;
1359 	struct awcc_priv *priv = dev_get_drvdata(dev);
1360 	const struct awcc_fan_data *fan;
1361 	unsigned int bit;
1362 
1363 	seq_printf(seq, "Number of fans: %u\n", priv->fan_count);
1364 	seq_printf(seq, "Number of temperature sensors: %u\n\n", priv->temp_count);
1365 
1366 	for (u32 i = 0; i < priv->fan_count; i++) {
1367 		fan = priv->fan_data[i];
1368 
1369 		seq_printf(seq, "Fan %u:\n", i);
1370 		seq_printf(seq, "  ID: 0x%02x\n", fan->id);
1371 		seq_printf(seq, "  Related temperature sensors bitmap: %lu\n",
1372 			   fan->auto_channels_temp);
1373 	}
1374 
1375 	seq_puts(seq, "\nTemperature sensor IDs:\n");
1376 	for_each_set_bit(bit, priv->temp_sensors, AWCC_ID_BITMAP_SIZE)
1377 		seq_printf(seq, "  0x%02x\n", bit);
1378 
1379 	return 0;
1380 }
1381 
1382 static int awcc_debugfs_pprof_data_read(struct seq_file *seq, void *data)
1383 {
1384 	struct device *dev = seq->private;
1385 	struct awcc_priv *priv = dev_get_drvdata(dev);
1386 
1387 	seq_printf(seq, "Number of thermal profiles: %u\n\n", priv->profile_count);
1388 
1389 	for (u32 i = 0; i < PLATFORM_PROFILE_LAST; i++) {
1390 		if (!priv->supported_profiles[i])
1391 			continue;
1392 
1393 		seq_printf(seq, "Platform profile %u:\n", i);
1394 		seq_printf(seq, "  ID: 0x%02x\n", priv->supported_profiles[i]);
1395 	}
1396 
1397 	return 0;
1398 }
1399 
1400 static int awcc_gpio_pin_show(struct seq_file *seq, void *data)
1401 {
1402 	unsigned long pin = debugfs_get_aux_num(seq->file);
1403 	struct wmi_device *wdev = seq->private;
1404 	u32 status;
1405 	int ret;
1406 
1407 	ret = awcc_read_gpio_status(wdev, pin, &status);
1408 	if (ret)
1409 		return ret;
1410 
1411 	seq_printf(seq, "%u\n", status);
1412 
1413 	return 0;
1414 }
1415 
1416 static ssize_t awcc_gpio_pin_write(struct file *file, const char __user *buf,
1417 				   size_t count, loff_t *ppos)
1418 {
1419 	unsigned long pin = debugfs_get_aux_num(file);
1420 	struct seq_file *seq = file->private_data;
1421 	struct wmi_device *wdev = seq->private;
1422 	bool status;
1423 	int ret;
1424 
1425 	if (!ppos || *ppos)
1426 		return -EINVAL;
1427 
1428 	ret = kstrtobool_from_user(buf, count, &status);
1429 	if (ret)
1430 		return ret;
1431 
1432 	ret = awcc_fwup_gpio_control(wdev, pin, status);
1433 	if (ret)
1434 		return ret;
1435 
1436 	return count;
1437 }
1438 
1439 DEFINE_SHOW_STORE_ATTRIBUTE(awcc_gpio_pin);
1440 
1441 static void awcc_debugfs_remove(void *data)
1442 {
1443 	struct dentry *root = data;
1444 
1445 	debugfs_remove(root);
1446 }
1447 
1448 static void awcc_debugfs_init(struct wmi_device *wdev)
1449 {
1450 	struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
1451 	struct dentry *root, *gpio_ctl;
1452 	u32 gpio_count;
1453 	char name[64];
1454 	int ret;
1455 
1456 	scnprintf(name, sizeof(name), "%s-%s", "alienware-wmi", dev_name(&wdev->dev));
1457 	root = debugfs_create_dir(name, NULL);
1458 
1459 	debugfs_create_devm_seqfile(&wdev->dev, "system_description", root,
1460 				    awcc_debugfs_system_description_read);
1461 
1462 	if (awcc->hwmon)
1463 		debugfs_create_devm_seqfile(&wdev->dev, "hwmon_data", root,
1464 					    awcc_debugfs_hwmon_data_read);
1465 
1466 	if (awcc->pprof)
1467 		debugfs_create_devm_seqfile(&wdev->dev, "pprof_data", root,
1468 					    awcc_debugfs_pprof_data_read);
1469 
1470 	ret = awcc_read_total_gpios(wdev, &gpio_count);
1471 	if (ret) {
1472 		dev_dbg(&wdev->dev, "Failed to get total GPIO Pin count\n");
1473 		goto out_add_action;
1474 	} else if (gpio_count > AWCC_MAX_RES_COUNT) {
1475 		dev_dbg(&wdev->dev, "Reported GPIO Pin count may be incorrect: %u\n", gpio_count);
1476 		goto out_add_action;
1477 	}
1478 
1479 	gpio_ctl = debugfs_create_dir("gpio_ctl", root);
1480 
1481 	priv->gpio_count = gpio_count;
1482 	debugfs_create_u32("total_gpios", 0444, gpio_ctl, &priv->gpio_count);
1483 
1484 	for (unsigned int i = 0; i < gpio_count; i++) {
1485 		scnprintf(name, sizeof(name), "pin%u", i);
1486 		debugfs_create_file_aux_num(name, 0644, gpio_ctl, wdev, i,
1487 					    &awcc_gpio_pin_fops);
1488 	}
1489 
1490 out_add_action:
1491 	devm_add_action_or_reset(&wdev->dev, awcc_debugfs_remove, root);
1492 }
1493 
1494 static int alienware_awcc_setup(struct wmi_device *wdev)
1495 {
1496 	struct awcc_priv *priv;
1497 	int ret;
1498 
1499 	priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
1500 	if (!priv)
1501 		return -ENOMEM;
1502 
1503 	ret = awcc_thermal_information(wdev, AWCC_OP_GET_SYSTEM_DESCRIPTION,
1504 				       0, &priv->system_description);
1505 	if (ret < 0)
1506 		return ret;
1507 
1508 	/* Sanity check */
1509 	for (unsigned int i = 0; i < ARRAY_SIZE(priv->res_count); i++) {
1510 		if (priv->res_count[i] > AWCC_MAX_RES_COUNT) {
1511 			dev_err(&wdev->dev, "Malformed system description: 0x%08x\n",
1512 				priv->system_description);
1513 			return -ENXIO;
1514 		}
1515 	}
1516 
1517 	priv->wdev = wdev;
1518 	dev_set_drvdata(&wdev->dev, priv);
1519 
1520 	if (awcc->hwmon) {
1521 		ret = awcc_hwmon_init(wdev);
1522 		if (ret)
1523 			return ret;
1524 	}
1525 
1526 	if (awcc->pprof) {
1527 		ret = awcc_platform_profile_init(wdev);
1528 		if (ret)
1529 			return ret;
1530 	}
1531 
1532 	awcc_debugfs_init(wdev);
1533 
1534 	return 0;
1535 }
1536 
1537 /*
1538  * WMAX WMI driver
1539  */
1540 static int wmax_wmi_update_led(struct alienfx_priv *priv,
1541 			       struct wmi_device *wdev, u8 location)
1542 {
1543 	struct wmax_led_args in_args = {
1544 		.led_mask = 1 << location,
1545 		.colors = priv->colors[location],
1546 		.state = priv->lighting_control_state,
1547 	};
1548 
1549 	return alienware_wmi_command(wdev, WMAX_METHOD_ZONE_CONTROL, &in_args,
1550 				     sizeof(in_args), NULL);
1551 }
1552 
1553 static int wmax_wmi_update_brightness(struct alienfx_priv *priv,
1554 				      struct wmi_device *wdev, u8 brightness)
1555 {
1556 	struct wmax_brightness_args in_args = {
1557 		.led_mask = 0xFF,
1558 		.percentage = brightness,
1559 	};
1560 
1561 	return alienware_wmi_command(wdev, WMAX_METHOD_BRIGHTNESS, &in_args,
1562 				     sizeof(in_args), NULL);
1563 }
1564 
1565 static int wmax_wmi_probe(struct wmi_device *wdev, const void *context)
1566 {
1567 	struct alienfx_platdata pdata = {
1568 		.wdev = wdev,
1569 		.ops = {
1570 			.upd_led = wmax_wmi_update_led,
1571 			.upd_brightness = wmax_wmi_update_brightness,
1572 		},
1573 	};
1574 	int ret;
1575 
1576 	if (awcc)
1577 		ret = alienware_awcc_setup(wdev);
1578 	else
1579 		ret = alienware_alienfx_setup(&pdata);
1580 
1581 	return ret;
1582 }
1583 
1584 static int wmax_wmi_suspend(struct device *dev)
1585 {
1586 	if (awcc && awcc->hwmon)
1587 		awcc_hwmon_suspend(dev);
1588 
1589 	return 0;
1590 }
1591 
1592 static int wmax_wmi_resume(struct device *dev)
1593 {
1594 	if (awcc && awcc->hwmon)
1595 		awcc_hwmon_resume(dev);
1596 
1597 	return 0;
1598 }
1599 
1600 static DEFINE_SIMPLE_DEV_PM_OPS(wmax_wmi_pm_ops, wmax_wmi_suspend, wmax_wmi_resume);
1601 
1602 static const struct wmi_device_id alienware_wmax_device_id_table[] = {
1603 	{ WMAX_CONTROL_GUID, NULL },
1604 	{ },
1605 };
1606 MODULE_DEVICE_TABLE(wmi, alienware_wmax_device_id_table);
1607 
1608 static struct wmi_driver alienware_wmax_wmi_driver = {
1609 	.driver = {
1610 		.name = "alienware-wmi-wmax",
1611 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1612 		.pm = pm_sleep_ptr(&wmax_wmi_pm_ops),
1613 	},
1614 	.id_table = alienware_wmax_device_id_table,
1615 	.probe = wmax_wmi_probe,
1616 	.no_singleton = true,
1617 };
1618 
1619 int __init alienware_wmax_wmi_init(void)
1620 {
1621 	const struct dmi_system_id *id;
1622 
1623 	id = dmi_first_match(awcc_dmi_table);
1624 	if (id)
1625 		awcc = id->driver_data;
1626 
1627 	if (force_hwmon) {
1628 		if (!awcc)
1629 			awcc = &empty_quirks;
1630 
1631 		awcc->hwmon = true;
1632 	}
1633 
1634 	if (force_platform_profile) {
1635 		if (!awcc)
1636 			awcc = &empty_quirks;
1637 
1638 		awcc->pprof = true;
1639 	}
1640 
1641 	if (force_gmode) {
1642 		if (awcc)
1643 			awcc->gmode = true;
1644 		else
1645 			pr_warn("force_gmode requires platform profile support\n");
1646 	}
1647 
1648 	return wmi_driver_register(&alienware_wmax_wmi_driver);
1649 }
1650 
1651 void __exit alienware_wmax_wmi_exit(void)
1652 {
1653 	wmi_driver_unregister(&alienware_wmax_wmi_driver);
1654 }
1655