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 16 Area-51",
94 .matches = {
95 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
96 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware 16 Area-51"),
97 },
98 .driver_data = &g_series_quirks,
99 },
100 {
101 .ident = "Alienware 16X Aurora",
102 .matches = {
103 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
104 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware 16X Aurora"),
105 },
106 .driver_data = &g_series_quirks,
107 },
108 {
109 .ident = "Alienware 18 Area-51",
110 .matches = {
111 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
112 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware 18 Area-51"),
113 },
114 .driver_data = &g_series_quirks,
115 },
116 {
117 .ident = "Alienware 16 Aurora",
118 .matches = {
119 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
120 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware 16 Aurora"),
121 },
122 .driver_data = &g_series_quirks,
123 },
124 {
125 .ident = "Alienware Area-51m",
126 .matches = {
127 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
128 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware Area-51m"),
129 },
130 .driver_data = &generic_quirks,
131 },
132 {
133 .ident = "Alienware m15",
134 .matches = {
135 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
136 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m15"),
137 },
138 .driver_data = &generic_quirks,
139 },
140 {
141 .ident = "Alienware m16 R1 AMD",
142 .matches = {
143 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
144 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m16 R1 AMD"),
145 },
146 .driver_data = &generic_quirks,
147 },
148 {
149 .ident = "Alienware m16 R1",
150 .matches = {
151 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
152 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m16 R1"),
153 },
154 .driver_data = &g_series_quirks,
155 },
156 {
157 .ident = "Alienware m16 R2",
158 .matches = {
159 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
160 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m16 R2"),
161 },
162 .driver_data = &generic_quirks,
163 },
164 {
165 .ident = "Alienware m17",
166 .matches = {
167 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
168 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m17"),
169 },
170 .driver_data = &generic_quirks,
171 },
172 {
173 .ident = "Alienware m18",
174 .matches = {
175 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
176 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware m18"),
177 },
178 .driver_data = &generic_quirks,
179 },
180 {
181 .ident = "Alienware x15",
182 .matches = {
183 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
184 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware x15"),
185 },
186 .driver_data = &generic_quirks,
187 },
188 {
189 .ident = "Alienware x16",
190 .matches = {
191 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
192 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware x16"),
193 },
194 .driver_data = &g_series_quirks,
195 },
196 {
197 .ident = "Alienware x17",
198 .matches = {
199 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
200 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware x17"),
201 },
202 .driver_data = &generic_quirks,
203 },
204 {
205 .ident = "Dell Inc. G15",
206 .matches = {
207 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
208 DMI_MATCH(DMI_PRODUCT_NAME, "Dell G15"),
209 },
210 .driver_data = &g_series_quirks,
211 },
212 {
213 .ident = "Dell Inc. G16",
214 .matches = {
215 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
216 DMI_MATCH(DMI_PRODUCT_NAME, "Dell G16"),
217 },
218 .driver_data = &g_series_quirks,
219 },
220 {
221 .ident = "Dell Inc. G3",
222 .matches = {
223 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
224 DMI_MATCH(DMI_PRODUCT_NAME, "G3"),
225 },
226 .driver_data = &g_series_quirks,
227 },
228 {
229 .ident = "Dell Inc. G5",
230 .matches = {
231 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
232 DMI_MATCH(DMI_PRODUCT_NAME, "G5"),
233 },
234 .driver_data = &g_series_quirks,
235 },
236 {}
237 };
238
239 enum AWCC_GET_FAN_SENSORS_OPERATIONS {
240 AWCC_OP_GET_TOTAL_FAN_TEMPS = 0x01,
241 AWCC_OP_GET_FAN_TEMP_ID = 0x02,
242 };
243
244 enum AWCC_THERMAL_INFORMATION_OPERATIONS {
245 AWCC_OP_GET_SYSTEM_DESCRIPTION = 0x02,
246 AWCC_OP_GET_RESOURCE_ID = 0x03,
247 AWCC_OP_GET_TEMPERATURE = 0x04,
248 AWCC_OP_GET_FAN_RPM = 0x05,
249 AWCC_OP_GET_FAN_MIN_RPM = 0x08,
250 AWCC_OP_GET_FAN_MAX_RPM = 0x09,
251 AWCC_OP_GET_CURRENT_PROFILE = 0x0B,
252 AWCC_OP_GET_FAN_BOOST = 0x0C,
253 };
254
255 enum AWCC_THERMAL_CONTROL_OPERATIONS {
256 AWCC_OP_ACTIVATE_PROFILE = 0x01,
257 AWCC_OP_SET_FAN_BOOST = 0x02,
258 };
259
260 enum AWCC_GAME_SHIFT_STATUS_OPERATIONS {
261 AWCC_OP_TOGGLE_GAME_SHIFT = 0x01,
262 AWCC_OP_GET_GAME_SHIFT_STATUS = 0x02,
263 };
264
265 enum AWCC_THERMAL_TABLES {
266 AWCC_THERMAL_TABLE_LEGACY = 0x9,
267 AWCC_THERMAL_TABLE_USTT = 0xA,
268 };
269
270 enum AWCC_TEMP_SENSOR_TYPES {
271 AWCC_TEMP_SENSOR_CPU = 0x01,
272 AWCC_TEMP_SENSOR_FRONT = 0x03,
273 AWCC_TEMP_SENSOR_GPU = 0x06,
274 };
275
276 enum AWCC_FAN_TYPES {
277 AWCC_FAN_CPU_1 = 0x32,
278 AWCC_FAN_GPU_1 = 0x33,
279 AWCC_FAN_PCI = 0x34,
280 AWCC_FAN_MID = 0x35,
281 AWCC_FAN_TOP_1 = 0x36,
282 AWCC_FAN_SIDE = 0x37,
283 AWCC_FAN_U2_1 = 0x38,
284 AWCC_FAN_U2_2 = 0x39,
285 AWCC_FAN_FRONT_1 = 0x3A,
286 AWCC_FAN_CPU_2 = 0x3B,
287 AWCC_FAN_GPU_2 = 0x3C,
288 AWCC_FAN_TOP_2 = 0x3D,
289 AWCC_FAN_TOP_3 = 0x3E,
290 AWCC_FAN_FRONT_2 = 0x3F,
291 AWCC_FAN_BOTTOM_1 = 0x40,
292 AWCC_FAN_BOTTOM_2 = 0x41,
293 };
294
295 enum awcc_thermal_profile {
296 AWCC_PROFILE_SPECIAL_CUSTOM = 0x00,
297 AWCC_PROFILE_LEGACY_QUIET = 0x96,
298 AWCC_PROFILE_LEGACY_BALANCED = 0x97,
299 AWCC_PROFILE_LEGACY_BALANCED_PERFORMANCE = 0x98,
300 AWCC_PROFILE_LEGACY_PERFORMANCE = 0x99,
301 AWCC_PROFILE_USTT_BALANCED = 0xA0,
302 AWCC_PROFILE_USTT_BALANCED_PERFORMANCE = 0xA1,
303 AWCC_PROFILE_USTT_COOL = 0xA2,
304 AWCC_PROFILE_USTT_QUIET = 0xA3,
305 AWCC_PROFILE_USTT_PERFORMANCE = 0xA4,
306 AWCC_PROFILE_USTT_LOW_POWER = 0xA5,
307 AWCC_PROFILE_SPECIAL_GMODE = 0xAB,
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 u32 min_rpm;
335 u32 max_rpm;
336 u8 suspend_cache;
337 u8 id;
338 };
339
340 struct awcc_priv {
341 struct wmi_device *wdev;
342 union {
343 u32 system_description;
344 struct {
345 u8 fan_count;
346 u8 temp_count;
347 u8 unknown_count;
348 u8 profile_count;
349 };
350 u8 res_count[4];
351 };
352
353 struct device *ppdev;
354 u8 supported_profiles[PLATFORM_PROFILE_LAST];
355
356 struct device *hwdev;
357 struct awcc_fan_data **fan_data;
358 unsigned long temp_sensors[AWCC_ID_BITMAP_LONGS];
359
360 u32 gpio_count;
361 };
362
363 static struct awcc_quirks *awcc;
364
365 /*
366 * The HDMI mux sysfs node indicates the status of the HDMI input mux.
367 * It can toggle between standard system GPU output and HDMI input.
368 */
cable_show(struct device * dev,struct device_attribute * attr,char * buf)369 static ssize_t cable_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_CABLE,
380 &in_args, sizeof(in_args), &out_data);
381 if (!ret) {
382 if (out_data == 0)
383 return sysfs_emit(buf, "[unconnected] connected unknown\n");
384 else if (out_data == 1)
385 return sysfs_emit(buf, "unconnected [connected] unknown\n");
386 }
387
388 pr_err("alienware-wmi: unknown HDMI cable status: %d\n", ret);
389 return sysfs_emit(buf, "unconnected connected [unknown]\n");
390 }
391
source_show(struct device * dev,struct device_attribute * attr,char * buf)392 static ssize_t source_show(struct device *dev, struct device_attribute *attr,
393 char *buf)
394 {
395 struct alienfx_platdata *pdata = dev_get_platdata(dev);
396 struct wmax_basic_args in_args = {
397 .arg = 0,
398 };
399 u32 out_data;
400 int ret;
401
402 ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_HDMI_STATUS,
403 &in_args, sizeof(in_args), &out_data);
404 if (!ret) {
405 if (out_data == 1)
406 return sysfs_emit(buf, "[input] gpu unknown\n");
407 else if (out_data == 2)
408 return sysfs_emit(buf, "input [gpu] unknown\n");
409 }
410
411 pr_err("alienware-wmi: unknown HDMI source status: %u\n", ret);
412 return sysfs_emit(buf, "input gpu [unknown]\n");
413 }
414
source_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)415 static ssize_t source_store(struct device *dev, struct device_attribute *attr,
416 const char *buf, size_t count)
417 {
418 struct alienfx_platdata *pdata = dev_get_platdata(dev);
419 struct wmax_basic_args args;
420 int ret;
421
422 if (strcmp(buf, "gpu\n") == 0)
423 args.arg = 1;
424 else if (strcmp(buf, "input\n") == 0)
425 args.arg = 2;
426 else
427 args.arg = 3;
428 pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
429
430 ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_HDMI_SOURCE, &args,
431 sizeof(args), NULL);
432 if (ret < 0)
433 pr_err("alienware-wmi: HDMI toggle failed: results: %u\n", ret);
434
435 return count;
436 }
437
438 static DEVICE_ATTR_RO(cable);
439 static DEVICE_ATTR_RW(source);
440
hdmi_group_visible(struct kobject * kobj)441 static bool hdmi_group_visible(struct kobject *kobj)
442 {
443 return alienware_interface == WMAX && alienfx->hdmi_mux;
444 }
445 DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(hdmi);
446
447 static struct attribute *hdmi_attrs[] = {
448 &dev_attr_cable.attr,
449 &dev_attr_source.attr,
450 NULL,
451 };
452
453 const struct attribute_group wmax_hdmi_attribute_group = {
454 .name = "hdmi",
455 .is_visible = SYSFS_GROUP_VISIBLE(hdmi),
456 .attrs = hdmi_attrs,
457 };
458
459 /*
460 * Alienware GFX amplifier support
461 * - Currently supports reading cable status
462 * - Leaving expansion room to possibly support dock/undock events later
463 */
status_show(struct device * dev,struct device_attribute * attr,char * buf)464 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
465 char *buf)
466 {
467 struct alienfx_platdata *pdata = dev_get_platdata(dev);
468 struct wmax_basic_args in_args = {
469 .arg = 0,
470 };
471 u32 out_data;
472 int ret;
473
474 ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_AMPLIFIER_CABLE,
475 &in_args, sizeof(in_args), &out_data);
476 if (!ret) {
477 if (out_data == 0)
478 return sysfs_emit(buf, "[unconnected] connected unknown\n");
479 else if (out_data == 1)
480 return sysfs_emit(buf, "unconnected [connected] unknown\n");
481 }
482
483 pr_err("alienware-wmi: unknown amplifier cable status: %d\n", ret);
484 return sysfs_emit(buf, "unconnected connected [unknown]\n");
485 }
486
487 static DEVICE_ATTR_RO(status);
488
amplifier_group_visible(struct kobject * kobj)489 static bool amplifier_group_visible(struct kobject *kobj)
490 {
491 return alienware_interface == WMAX && alienfx->amplifier;
492 }
493 DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(amplifier);
494
495 static struct attribute *amplifier_attrs[] = {
496 &dev_attr_status.attr,
497 NULL,
498 };
499
500 const struct attribute_group wmax_amplifier_attribute_group = {
501 .name = "amplifier",
502 .is_visible = SYSFS_GROUP_VISIBLE(amplifier),
503 .attrs = amplifier_attrs,
504 };
505
506 /*
507 * Deep Sleep Control support
508 * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
509 */
deepsleep_show(struct device * dev,struct device_attribute * attr,char * buf)510 static ssize_t deepsleep_show(struct device *dev, struct device_attribute *attr,
511 char *buf)
512 {
513 struct alienfx_platdata *pdata = dev_get_platdata(dev);
514 struct wmax_basic_args in_args = {
515 .arg = 0,
516 };
517 u32 out_data;
518 int ret;
519
520 ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_DEEP_SLEEP_STATUS,
521 &in_args, sizeof(in_args), &out_data);
522 if (!ret) {
523 if (out_data == 0)
524 return sysfs_emit(buf, "[disabled] s5 s5_s4\n");
525 else if (out_data == 1)
526 return sysfs_emit(buf, "disabled [s5] s5_s4\n");
527 else if (out_data == 2)
528 return sysfs_emit(buf, "disabled s5 [s5_s4]\n");
529 }
530
531 pr_err("alienware-wmi: unknown deep sleep status: %d\n", ret);
532 return sysfs_emit(buf, "disabled s5 s5_s4 [unknown]\n");
533 }
534
deepsleep_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)535 static ssize_t deepsleep_store(struct device *dev, struct device_attribute *attr,
536 const char *buf, size_t count)
537 {
538 struct alienfx_platdata *pdata = dev_get_platdata(dev);
539 struct wmax_basic_args args;
540 int ret;
541
542 if (strcmp(buf, "disabled\n") == 0)
543 args.arg = 0;
544 else if (strcmp(buf, "s5\n") == 0)
545 args.arg = 1;
546 else
547 args.arg = 2;
548 pr_debug("alienware-wmi: setting deep sleep to %d : %s", args.arg, buf);
549
550 ret = alienware_wmi_command(pdata->wdev, WMAX_METHOD_DEEP_SLEEP_CONTROL,
551 &args, sizeof(args), NULL);
552 if (!ret)
553 pr_err("alienware-wmi: deep sleep control failed: results: %u\n", ret);
554
555 return count;
556 }
557
558 static DEVICE_ATTR_RW(deepsleep);
559
deepsleep_group_visible(struct kobject * kobj)560 static bool deepsleep_group_visible(struct kobject *kobj)
561 {
562 return alienware_interface == WMAX && alienfx->deepslp;
563 }
564 DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(deepsleep);
565
566 static struct attribute *deepsleep_attrs[] = {
567 &dev_attr_deepsleep.attr,
568 NULL,
569 };
570
571 const struct attribute_group wmax_deepsleep_attribute_group = {
572 .name = "deepsleep",
573 .is_visible = SYSFS_GROUP_VISIBLE(deepsleep),
574 .attrs = deepsleep_attrs,
575 };
576
577 /*
578 * AWCC Helpers
579 */
awcc_profile_to_pprof(enum awcc_thermal_profile profile,enum platform_profile_option * pprof)580 static int awcc_profile_to_pprof(enum awcc_thermal_profile profile,
581 enum platform_profile_option *pprof)
582 {
583 switch (profile) {
584 case AWCC_PROFILE_SPECIAL_CUSTOM:
585 *pprof = PLATFORM_PROFILE_CUSTOM;
586 break;
587 case AWCC_PROFILE_LEGACY_QUIET:
588 case AWCC_PROFILE_USTT_QUIET:
589 *pprof = PLATFORM_PROFILE_QUIET;
590 break;
591 case AWCC_PROFILE_LEGACY_BALANCED:
592 case AWCC_PROFILE_USTT_BALANCED:
593 *pprof = PLATFORM_PROFILE_BALANCED;
594 break;
595 case AWCC_PROFILE_LEGACY_BALANCED_PERFORMANCE:
596 case AWCC_PROFILE_USTT_BALANCED_PERFORMANCE:
597 *pprof = PLATFORM_PROFILE_BALANCED_PERFORMANCE;
598 break;
599 case AWCC_PROFILE_LEGACY_PERFORMANCE:
600 case AWCC_PROFILE_USTT_PERFORMANCE:
601 case AWCC_PROFILE_SPECIAL_GMODE:
602 *pprof = PLATFORM_PROFILE_PERFORMANCE;
603 break;
604 case AWCC_PROFILE_USTT_COOL:
605 *pprof = PLATFORM_PROFILE_COOL;
606 break;
607 case AWCC_PROFILE_USTT_LOW_POWER:
608 *pprof = PLATFORM_PROFILE_LOW_POWER;
609 break;
610 default:
611 return -EINVAL;
612 }
613
614 return 0;
615 }
616
awcc_wmi_command(struct wmi_device * wdev,u32 method_id,struct wmax_u32_args * args,u32 * out)617 static int awcc_wmi_command(struct wmi_device *wdev, u32 method_id,
618 struct wmax_u32_args *args, u32 *out)
619 {
620 int ret;
621
622 ret = alienware_wmi_command(wdev, method_id, args, sizeof(*args), out);
623 if (ret)
624 return ret;
625
626 if (*out == AWCC_FAILURE_CODE || *out == AWCC_FAILURE_CODE_2)
627 return -EBADRQC;
628
629 return 0;
630 }
631
awcc_get_fan_sensors(struct wmi_device * wdev,u8 operation,u8 fan_id,u8 index,u32 * out)632 static int awcc_get_fan_sensors(struct wmi_device *wdev, u8 operation,
633 u8 fan_id, u8 index, u32 *out)
634 {
635 struct wmax_u32_args args = {
636 .operation = operation,
637 .arg1 = fan_id,
638 .arg2 = index,
639 .arg3 = 0,
640 };
641
642 return awcc_wmi_command(wdev, AWCC_METHOD_GET_FAN_SENSORS, &args, out);
643 }
644
awcc_thermal_information(struct wmi_device * wdev,u8 operation,u8 arg,u32 * out)645 static int awcc_thermal_information(struct wmi_device *wdev, u8 operation, u8 arg,
646 u32 *out)
647 {
648 struct wmax_u32_args args = {
649 .operation = operation,
650 .arg1 = arg,
651 .arg2 = 0,
652 .arg3 = 0,
653 };
654
655 return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
656 }
657
awcc_fwup_gpio_control(struct wmi_device * wdev,u8 pin,u8 status)658 static int awcc_fwup_gpio_control(struct wmi_device *wdev, u8 pin, u8 status)
659 {
660 struct wmax_u32_args args = {
661 .operation = pin,
662 .arg1 = status,
663 .arg2 = 0,
664 .arg3 = 0,
665 };
666 u32 out;
667
668 return awcc_wmi_command(wdev, AWCC_METHOD_FWUP_GPIO_CONTROL, &args, &out);
669 }
670
awcc_read_total_gpios(struct wmi_device * wdev,u32 * count)671 static int awcc_read_total_gpios(struct wmi_device *wdev, u32 *count)
672 {
673 struct wmax_u32_args args = {};
674
675 return awcc_wmi_command(wdev, AWCC_METHOD_READ_TOTAL_GPIOS, &args, count);
676 }
677
awcc_read_gpio_status(struct wmi_device * wdev,u8 pin,u32 * status)678 static int awcc_read_gpio_status(struct wmi_device *wdev, u8 pin, u32 *status)
679 {
680 struct wmax_u32_args args = {
681 .operation = pin,
682 .arg1 = 0,
683 .arg2 = 0,
684 .arg3 = 0,
685 };
686
687 return awcc_wmi_command(wdev, AWCC_METHOD_READ_GPIO_STATUS, &args, status);
688 }
689
awcc_game_shift_status(struct wmi_device * wdev,u8 operation,u32 * out)690 static int awcc_game_shift_status(struct wmi_device *wdev, u8 operation,
691 u32 *out)
692 {
693 struct wmax_u32_args args = {
694 .operation = operation,
695 .arg1 = 0,
696 .arg2 = 0,
697 .arg3 = 0,
698 };
699
700 return awcc_wmi_command(wdev, AWCC_METHOD_GAME_SHIFT_STATUS, &args, out);
701 }
702
703 /**
704 * awcc_op_get_resource_id - Get the resource ID at a given index
705 * @wdev: AWCC WMI device
706 * @index: Index
707 * @out: Value returned by the WMI call
708 *
709 * Get the resource ID at a given @index. Resource IDs are listed in the
710 * following order:
711 *
712 * - Fan IDs
713 * - Sensor IDs
714 * - Unknown IDs
715 * - Thermal Profile IDs
716 *
717 * The total number of IDs of a given type can be obtained with
718 * AWCC_OP_GET_SYSTEM_DESCRIPTION.
719 *
720 * Return: 0 on success, -errno on failure
721 */
awcc_op_get_resource_id(struct wmi_device * wdev,u8 index,u8 * out)722 static int awcc_op_get_resource_id(struct wmi_device *wdev, u8 index, u8 *out)
723 {
724 struct wmax_u32_args args = {
725 .operation = AWCC_OP_GET_RESOURCE_ID,
726 .arg1 = index,
727 .arg2 = 0,
728 .arg3 = 0,
729 };
730 u32 out_data;
731 int ret;
732
733 ret = awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, &out_data);
734 if (ret)
735 return ret;
736
737 *out = FIELD_GET(AWCC_RESOURCE_ID_MASK, out_data);
738
739 return 0;
740 }
741
awcc_op_get_fan_rpm(struct wmi_device * wdev,u8 fan_id,u32 * out)742 static int awcc_op_get_fan_rpm(struct wmi_device *wdev, u8 fan_id, u32 *out)
743 {
744 struct wmax_u32_args args = {
745 .operation = AWCC_OP_GET_FAN_RPM,
746 .arg1 = fan_id,
747 .arg2 = 0,
748 .arg3 = 0,
749 };
750
751 return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
752 }
753
awcc_op_get_temperature(struct wmi_device * wdev,u8 temp_id,u32 * out)754 static int awcc_op_get_temperature(struct wmi_device *wdev, u8 temp_id, u32 *out)
755 {
756 struct wmax_u32_args args = {
757 .operation = AWCC_OP_GET_TEMPERATURE,
758 .arg1 = temp_id,
759 .arg2 = 0,
760 .arg3 = 0,
761 };
762
763 return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
764 }
765
awcc_op_get_fan_boost(struct wmi_device * wdev,u8 fan_id,u32 * out)766 static int awcc_op_get_fan_boost(struct wmi_device *wdev, u8 fan_id, u32 *out)
767 {
768 struct wmax_u32_args args = {
769 .operation = AWCC_OP_GET_FAN_BOOST,
770 .arg1 = fan_id,
771 .arg2 = 0,
772 .arg3 = 0,
773 };
774
775 return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
776 }
777
awcc_op_get_current_profile(struct wmi_device * wdev,u32 * out)778 static int awcc_op_get_current_profile(struct wmi_device *wdev, u32 *out)
779 {
780 struct wmax_u32_args args = {
781 .operation = AWCC_OP_GET_CURRENT_PROFILE,
782 .arg1 = 0,
783 .arg2 = 0,
784 .arg3 = 0,
785 };
786
787 return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_INFORMATION, &args, out);
788 }
789
awcc_op_activate_profile(struct wmi_device * wdev,u8 profile)790 static int awcc_op_activate_profile(struct wmi_device *wdev, u8 profile)
791 {
792 struct wmax_u32_args args = {
793 .operation = AWCC_OP_ACTIVATE_PROFILE,
794 .arg1 = profile,
795 .arg2 = 0,
796 .arg3 = 0,
797 };
798 u32 out;
799
800 return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_CONTROL, &args, &out);
801 }
802
awcc_op_set_fan_boost(struct wmi_device * wdev,u8 fan_id,u8 boost)803 static int awcc_op_set_fan_boost(struct wmi_device *wdev, u8 fan_id, u8 boost)
804 {
805 struct wmax_u32_args args = {
806 .operation = AWCC_OP_SET_FAN_BOOST,
807 .arg1 = fan_id,
808 .arg2 = boost,
809 .arg3 = 0,
810 };
811 u32 out;
812
813 return awcc_wmi_command(wdev, AWCC_METHOD_THERMAL_CONTROL, &args, &out);
814 }
815
816 /*
817 * HWMON
818 * - Provides temperature and fan speed monitoring as well as manual fan
819 * control
820 */
awcc_hwmon_is_visible(const void * drvdata,enum hwmon_sensor_types type,u32 attr,int channel)821 static umode_t awcc_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
822 u32 attr, int channel)
823 {
824 const struct awcc_priv *priv = drvdata;
825 unsigned int temp_count;
826
827 switch (type) {
828 case hwmon_temp:
829 temp_count = bitmap_weight(priv->temp_sensors, AWCC_ID_BITMAP_SIZE);
830
831 return channel < temp_count ? 0444 : 0;
832 case hwmon_fan:
833 return channel < priv->fan_count ? 0444 : 0;
834 case hwmon_pwm:
835 return channel < priv->fan_count ? 0444 : 0;
836 default:
837 return 0;
838 }
839 }
840
awcc_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)841 static int awcc_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
842 u32 attr, int channel, long *val)
843 {
844 struct awcc_priv *priv = dev_get_drvdata(dev);
845 const struct awcc_fan_data *fan;
846 u32 state;
847 int ret;
848 u8 temp;
849
850 switch (type) {
851 case hwmon_temp:
852 temp = find_nth_bit(priv->temp_sensors, AWCC_ID_BITMAP_SIZE, channel);
853
854 switch (attr) {
855 case hwmon_temp_input:
856 ret = awcc_op_get_temperature(priv->wdev, temp, &state);
857 if (ret)
858 return ret;
859
860 *val = state * MILLIDEGREE_PER_DEGREE;
861 break;
862 default:
863 return -EOPNOTSUPP;
864 }
865
866 break;
867 case hwmon_fan:
868 fan = priv->fan_data[channel];
869
870 switch (attr) {
871 case hwmon_fan_input:
872 ret = awcc_op_get_fan_rpm(priv->wdev, fan->id, &state);
873 if (ret)
874 return ret;
875
876 *val = state;
877 break;
878 case hwmon_fan_min:
879 *val = fan->min_rpm;
880 break;
881 case hwmon_fan_max:
882 *val = fan->max_rpm;
883 break;
884 default:
885 return -EOPNOTSUPP;
886 }
887
888 break;
889 case hwmon_pwm:
890 fan = priv->fan_data[channel];
891
892 switch (attr) {
893 case hwmon_pwm_auto_channels_temp:
894 *val = fan->auto_channels_temp;
895 break;
896 default:
897 return -EOPNOTSUPP;
898 }
899
900 break;
901 default:
902 return -EOPNOTSUPP;
903 }
904
905 return 0;
906 }
907
awcc_hwmon_read_string(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,const char ** str)908 static int awcc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
909 u32 attr, int channel, const char **str)
910 {
911 struct awcc_priv *priv = dev_get_drvdata(dev);
912 u8 temp;
913
914 switch (type) {
915 case hwmon_temp:
916 temp = find_nth_bit(priv->temp_sensors, AWCC_ID_BITMAP_SIZE, channel);
917
918 switch (temp) {
919 case AWCC_TEMP_SENSOR_CPU:
920 *str = "CPU";
921 break;
922 case AWCC_TEMP_SENSOR_FRONT:
923 *str = "Front";
924 break;
925 case AWCC_TEMP_SENSOR_GPU:
926 *str = "GPU";
927 break;
928 default:
929 *str = "Unknown";
930 break;
931 }
932
933 break;
934 case hwmon_fan:
935 switch (priv->fan_data[channel]->id) {
936 case AWCC_FAN_CPU_1:
937 case AWCC_FAN_CPU_2:
938 *str = "CPU Fan";
939 break;
940 case AWCC_FAN_GPU_1:
941 case AWCC_FAN_GPU_2:
942 *str = "GPU Fan";
943 break;
944 case AWCC_FAN_PCI:
945 *str = "PCI Fan";
946 break;
947 case AWCC_FAN_MID:
948 *str = "Mid Fan";
949 break;
950 case AWCC_FAN_TOP_1:
951 case AWCC_FAN_TOP_2:
952 case AWCC_FAN_TOP_3:
953 *str = "Top Fan";
954 break;
955 case AWCC_FAN_SIDE:
956 *str = "Side Fan";
957 break;
958 case AWCC_FAN_U2_1:
959 case AWCC_FAN_U2_2:
960 *str = "U.2 Fan";
961 break;
962 case AWCC_FAN_FRONT_1:
963 case AWCC_FAN_FRONT_2:
964 *str = "Front Fan";
965 break;
966 case AWCC_FAN_BOTTOM_1:
967 case AWCC_FAN_BOTTOM_2:
968 *str = "Bottom Fan";
969 break;
970 default:
971 *str = "Unknown Fan";
972 break;
973 }
974
975 break;
976 default:
977 return -EOPNOTSUPP;
978 }
979
980 return 0;
981 }
982
983 static const struct hwmon_ops awcc_hwmon_ops = {
984 .is_visible = awcc_hwmon_is_visible,
985 .read = awcc_hwmon_read,
986 .read_string = awcc_hwmon_read_string,
987 };
988
989 static const struct hwmon_channel_info * const awcc_hwmon_info[] = {
990 HWMON_CHANNEL_INFO(temp,
991 HWMON_T_LABEL | HWMON_T_INPUT,
992 HWMON_T_LABEL | HWMON_T_INPUT,
993 HWMON_T_LABEL | HWMON_T_INPUT,
994 HWMON_T_LABEL | HWMON_T_INPUT,
995 HWMON_T_LABEL | HWMON_T_INPUT,
996 HWMON_T_LABEL | HWMON_T_INPUT
997 ),
998 HWMON_CHANNEL_INFO(fan,
999 HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
1000 HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
1001 HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
1002 HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
1003 HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX,
1004 HWMON_F_LABEL | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX
1005 ),
1006 HWMON_CHANNEL_INFO(pwm,
1007 HWMON_PWM_AUTO_CHANNELS_TEMP,
1008 HWMON_PWM_AUTO_CHANNELS_TEMP,
1009 HWMON_PWM_AUTO_CHANNELS_TEMP,
1010 HWMON_PWM_AUTO_CHANNELS_TEMP,
1011 HWMON_PWM_AUTO_CHANNELS_TEMP,
1012 HWMON_PWM_AUTO_CHANNELS_TEMP
1013 ),
1014 NULL
1015 };
1016
1017 static const struct hwmon_chip_info awcc_hwmon_chip_info = {
1018 .ops = &awcc_hwmon_ops,
1019 .info = awcc_hwmon_info,
1020 };
1021
fan_boost_show(struct device * dev,struct device_attribute * attr,char * buf)1022 static ssize_t fan_boost_show(struct device *dev, struct device_attribute *attr,
1023 char *buf)
1024 {
1025 struct awcc_priv *priv = dev_get_drvdata(dev);
1026 int index = to_sensor_dev_attr(attr)->index;
1027 struct awcc_fan_data *fan = priv->fan_data[index];
1028 u32 boost;
1029 int ret;
1030
1031 ret = awcc_op_get_fan_boost(priv->wdev, fan->id, &boost);
1032 if (ret)
1033 return ret;
1034
1035 return sysfs_emit(buf, "%u\n", boost);
1036 }
1037
fan_boost_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1038 static ssize_t fan_boost_store(struct device *dev, struct device_attribute *attr,
1039 const char *buf, size_t count)
1040 {
1041 struct awcc_priv *priv = dev_get_drvdata(dev);
1042 int index = to_sensor_dev_attr(attr)->index;
1043 struct awcc_fan_data *fan = priv->fan_data[index];
1044 unsigned long val;
1045 int ret;
1046
1047 ret = kstrtoul(buf, 0, &val);
1048 if (ret)
1049 return ret;
1050
1051 ret = awcc_op_set_fan_boost(priv->wdev, fan->id, clamp_val(val, 0, 255));
1052
1053 return ret ? ret : count;
1054 }
1055
1056 static SENSOR_DEVICE_ATTR_RW(fan1_boost, fan_boost, 0);
1057 static SENSOR_DEVICE_ATTR_RW(fan2_boost, fan_boost, 1);
1058 static SENSOR_DEVICE_ATTR_RW(fan3_boost, fan_boost, 2);
1059 static SENSOR_DEVICE_ATTR_RW(fan4_boost, fan_boost, 3);
1060 static SENSOR_DEVICE_ATTR_RW(fan5_boost, fan_boost, 4);
1061 static SENSOR_DEVICE_ATTR_RW(fan6_boost, fan_boost, 5);
1062
fan_boost_attr_visible(struct kobject * kobj,struct attribute * attr,int n)1063 static umode_t fan_boost_attr_visible(struct kobject *kobj, struct attribute *attr, int n)
1064 {
1065 struct awcc_priv *priv = dev_get_drvdata(kobj_to_dev(kobj));
1066
1067 return n < priv->fan_count ? attr->mode : 0;
1068 }
1069
fan_boost_group_visible(struct kobject * kobj)1070 static bool fan_boost_group_visible(struct kobject *kobj)
1071 {
1072 return true;
1073 }
1074
1075 DEFINE_SYSFS_GROUP_VISIBLE(fan_boost);
1076
1077 static struct attribute *fan_boost_attrs[] = {
1078 &sensor_dev_attr_fan1_boost.dev_attr.attr,
1079 &sensor_dev_attr_fan2_boost.dev_attr.attr,
1080 &sensor_dev_attr_fan3_boost.dev_attr.attr,
1081 &sensor_dev_attr_fan4_boost.dev_attr.attr,
1082 &sensor_dev_attr_fan5_boost.dev_attr.attr,
1083 &sensor_dev_attr_fan6_boost.dev_attr.attr,
1084 NULL
1085 };
1086
1087 static const struct attribute_group fan_boost_group = {
1088 .attrs = fan_boost_attrs,
1089 .is_visible = SYSFS_GROUP_VISIBLE(fan_boost),
1090 };
1091
1092 static const struct attribute_group *awcc_hwmon_groups[] = {
1093 &fan_boost_group,
1094 NULL
1095 };
1096
awcc_hwmon_temps_init(struct wmi_device * wdev)1097 static int awcc_hwmon_temps_init(struct wmi_device *wdev)
1098 {
1099 struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
1100 unsigned int i;
1101 int ret;
1102 u8 id;
1103
1104 for (i = 0; i < priv->temp_count; i++) {
1105 /*
1106 * Temperature sensors IDs are listed after the fan IDs at
1107 * offset `fan_count`
1108 */
1109 ret = awcc_op_get_resource_id(wdev, i + priv->fan_count, &id);
1110 if (ret)
1111 return ret;
1112
1113 __set_bit(id, priv->temp_sensors);
1114 }
1115
1116 return 0;
1117 }
1118
awcc_hwmon_fans_init(struct wmi_device * wdev)1119 static int awcc_hwmon_fans_init(struct wmi_device *wdev)
1120 {
1121 struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
1122 unsigned long fan_temps[AWCC_ID_BITMAP_LONGS];
1123 unsigned long gather[AWCC_ID_BITMAP_LONGS];
1124 u32 min_rpm, max_rpm, temp_count, temp_id;
1125 struct awcc_fan_data *fan_data;
1126 unsigned int i, j;
1127 int ret;
1128 u8 id;
1129
1130 for (i = 0; i < priv->fan_count; i++) {
1131 fan_data = devm_kzalloc(&wdev->dev, sizeof(*fan_data), GFP_KERNEL);
1132 if (!fan_data)
1133 return -ENOMEM;
1134
1135 /*
1136 * Fan IDs are listed first at offset 0
1137 */
1138 ret = awcc_op_get_resource_id(wdev, i, &id);
1139 if (ret)
1140 return ret;
1141
1142 ret = awcc_thermal_information(wdev, AWCC_OP_GET_FAN_MIN_RPM, id,
1143 &min_rpm);
1144 if (ret)
1145 return ret;
1146
1147 ret = awcc_thermal_information(wdev, AWCC_OP_GET_FAN_MAX_RPM, id,
1148 &max_rpm);
1149 if (ret)
1150 return ret;
1151
1152 ret = awcc_get_fan_sensors(wdev, AWCC_OP_GET_TOTAL_FAN_TEMPS, id,
1153 0, &temp_count);
1154 if (ret)
1155 return ret;
1156
1157 bitmap_zero(fan_temps, AWCC_ID_BITMAP_SIZE);
1158
1159 for (j = 0; j < temp_count; j++) {
1160 ret = awcc_get_fan_sensors(wdev, AWCC_OP_GET_FAN_TEMP_ID,
1161 id, j, &temp_id);
1162 if (ret)
1163 break;
1164
1165 temp_id = FIELD_GET(AWCC_RESOURCE_ID_MASK, temp_id);
1166 __set_bit(temp_id, fan_temps);
1167 }
1168
1169 fan_data->id = id;
1170 fan_data->min_rpm = min_rpm;
1171 fan_data->max_rpm = max_rpm;
1172 bitmap_gather(gather, fan_temps, priv->temp_sensors, AWCC_ID_BITMAP_SIZE);
1173 bitmap_copy(&fan_data->auto_channels_temp, gather, BITS_PER_LONG);
1174 priv->fan_data[i] = fan_data;
1175 }
1176
1177 return 0;
1178 }
1179
awcc_hwmon_init(struct wmi_device * wdev)1180 static int awcc_hwmon_init(struct wmi_device *wdev)
1181 {
1182 struct awcc_priv *priv = dev_get_drvdata(&wdev->dev);
1183 int ret;
1184
1185 priv->fan_data = devm_kcalloc(&wdev->dev, priv->fan_count,
1186 sizeof(*priv->fan_data), GFP_KERNEL);
1187 if (!priv->fan_data)
1188 return -ENOMEM;
1189
1190 ret = awcc_hwmon_temps_init(wdev);
1191 if (ret)
1192 return ret;
1193
1194 ret = awcc_hwmon_fans_init(wdev);
1195 if (ret)
1196 return ret;
1197
1198 priv->hwdev = devm_hwmon_device_register_with_info(&wdev->dev, "alienware_wmi",
1199 priv, &awcc_hwmon_chip_info,
1200 awcc_hwmon_groups);
1201
1202 return PTR_ERR_OR_ZERO(priv->hwdev);
1203 }
1204
awcc_hwmon_suspend(struct device * dev)1205 static void awcc_hwmon_suspend(struct device *dev)
1206 {
1207 struct awcc_priv *priv = dev_get_drvdata(dev);
1208 struct awcc_fan_data *fan;
1209 unsigned int i;
1210 u32 boost;
1211 int ret;
1212
1213 for (i = 0; i < priv->fan_count; i++) {
1214 fan = priv->fan_data[i];
1215
1216 ret = awcc_thermal_information(priv->wdev, AWCC_OP_GET_FAN_BOOST,
1217 fan->id, &boost);
1218 if (ret)
1219 dev_err(dev, "Failed to store Fan %u boost while suspending\n", i);
1220
1221 fan->suspend_cache = ret ? 0 : clamp_val(boost, 0, 255);
1222
1223 awcc_op_set_fan_boost(priv->wdev, fan->id, 0);
1224 if (ret)
1225 dev_err(dev, "Failed to set Fan %u boost to 0 while suspending\n", i);
1226 }
1227 }
1228
awcc_hwmon_resume(struct device * dev)1229 static void awcc_hwmon_resume(struct device *dev)
1230 {
1231 struct awcc_priv *priv = dev_get_drvdata(dev);
1232 struct awcc_fan_data *fan;
1233 unsigned int i;
1234 int ret;
1235
1236 for (i = 0; i < priv->fan_count; i++) {
1237 fan = priv->fan_data[i];
1238
1239 if (!fan->suspend_cache)
1240 continue;
1241
1242 ret = awcc_op_set_fan_boost(priv->wdev, fan->id, fan->suspend_cache);
1243 if (ret)
1244 dev_err(dev, "Failed to restore Fan %u boost while resuming\n", i);
1245 }
1246 }
1247
1248 /*
1249 * Thermal Profile control
1250 * - Provides thermal profile control through the Platform Profile API
1251 */
awcc_platform_profile_get(struct device * dev,enum platform_profile_option * profile)1252 static int awcc_platform_profile_get(struct device *dev,
1253 enum platform_profile_option *profile)
1254 {
1255 struct awcc_priv *priv = dev_get_drvdata(dev);
1256 u32 out_data;
1257 int ret;
1258
1259 ret = awcc_op_get_current_profile(priv->wdev, &out_data);
1260 if (ret)
1261 return ret;
1262
1263 return awcc_profile_to_pprof(out_data, profile);
1264 }
1265
awcc_platform_profile_set(struct device * dev,enum platform_profile_option profile)1266 static int awcc_platform_profile_set(struct device *dev,
1267 enum platform_profile_option profile)
1268 {
1269 struct awcc_priv *priv = dev_get_drvdata(dev);
1270
1271 if (awcc->gmode) {
1272 u32 gmode_status;
1273 int ret;
1274
1275 ret = awcc_game_shift_status(priv->wdev,
1276 AWCC_OP_GET_GAME_SHIFT_STATUS,
1277 &gmode_status);
1278
1279 if (ret < 0)
1280 return ret;
1281
1282 if ((profile == PLATFORM_PROFILE_PERFORMANCE && !gmode_status) ||
1283 (profile != PLATFORM_PROFILE_PERFORMANCE && gmode_status)) {
1284 ret = awcc_game_shift_status(priv->wdev,
1285 AWCC_OP_TOGGLE_GAME_SHIFT,
1286 &gmode_status);
1287
1288 if (ret < 0)
1289 return ret;
1290 }
1291 }
1292
1293 return awcc_op_activate_profile(priv->wdev, priv->supported_profiles[profile]);
1294 }
1295
awcc_platform_profile_probe(void * drvdata,unsigned long * choices)1296 static int awcc_platform_profile_probe(void *drvdata, unsigned long *choices)
1297 {
1298 enum platform_profile_option profile;
1299 struct awcc_priv *priv = drvdata;
1300 u8 id, offset = 0;
1301 int ret;
1302
1303 /*
1304 * Thermal profile IDs are listed last at offset
1305 * fan_count + temp_count + unknown_count
1306 */
1307 for (unsigned int i = 0; i < ARRAY_SIZE(priv->res_count) - 1; i++)
1308 offset += priv->res_count[i];
1309
1310 for (unsigned int i = 0; i < priv->profile_count; i++) {
1311 ret = awcc_op_get_resource_id(priv->wdev, i + offset, &id);
1312 /*
1313 * Some devices report an incorrect number of thermal profiles
1314 * so the resource ID list may end prematurely
1315 */
1316 if (ret == -EBADRQC)
1317 break;
1318 if (ret)
1319 return ret;
1320
1321 /*
1322 * G-Mode profile ID is not listed consistently across modeles
1323 * that support it, therefore we handle it through quirks.
1324 */
1325 if (id == AWCC_PROFILE_SPECIAL_GMODE)
1326 continue;
1327
1328 ret = awcc_profile_to_pprof(id, &profile);
1329 if (ret) {
1330 dev_dbg(&priv->wdev->dev, "Unmapped thermal profile ID 0x%02x\n", id);
1331 continue;
1332 }
1333
1334 priv->supported_profiles[profile] = id;
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_PROFILE_SPECIAL_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_PROFILE_SPECIAL_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
awcc_platform_profile_init(struct wmi_device * wdev)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 */
awcc_debugfs_system_description_read(struct seq_file * seq,void * data)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
awcc_debugfs_hwmon_data_read(struct seq_file * seq,void * data)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
awcc_debugfs_pprof_data_read(struct seq_file * seq,void * data)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
awcc_gpio_pin_show(struct seq_file * seq,void * data)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
awcc_gpio_pin_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)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
awcc_debugfs_remove(void * data)1471 static void awcc_debugfs_remove(void *data)
1472 {
1473 struct dentry *root = data;
1474
1475 debugfs_remove(root);
1476 }
1477
awcc_debugfs_init(struct wmi_device * wdev)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
alienware_awcc_setup(struct wmi_device * wdev)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 */
wmax_wmi_update_led(struct alienfx_priv * priv,struct wmi_device * wdev,u8 location)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
wmax_wmi_update_brightness(struct alienfx_priv * priv,struct wmi_device * wdev,u8 brightness)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
wmax_wmi_probe(struct wmi_device * wdev,const void * context)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
wmax_wmi_suspend(struct device * dev)1614 static int wmax_wmi_suspend(struct device *dev)
1615 {
1616 if (awcc && awcc->hwmon)
1617 awcc_hwmon_suspend(dev);
1618
1619 return 0;
1620 }
1621
wmax_wmi_resume(struct device * dev)1622 static int wmax_wmi_resume(struct device *dev)
1623 {
1624 if (awcc && 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
alienware_wmax_wmi_init(void)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
alienware_wmax_wmi_exit(void)1681 void __exit alienware_wmax_wmi_exit(void)
1682 {
1683 wmi_driver_unregister(&alienware_wmax_wmi_driver);
1684 }
1685