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