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