2 * Alienware AlienFX control
4 * Copyright (C) 2014 Dell Inc <mario_limonciello@dell.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/acpi.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/dmi.h>
24 #include <linux/leds.h>
26 #define LEGACY_CONTROL_GUID "A90597CE-A997-11DA-B012-B622A1EF5492"
27 #define LEGACY_POWER_CONTROL_GUID "A80593CE-A997-11DA-B012-B622A1EF5492"
28 #define WMAX_CONTROL_GUID "A70591CE-A997-11DA-B012-B622A1EF5492"
30 #define WMAX_METHOD_HDMI_SOURCE 0x1
31 #define WMAX_METHOD_HDMI_STATUS 0x2
32 #define WMAX_METHOD_BRIGHTNESS 0x3
33 #define WMAX_METHOD_ZONE_CONTROL 0x4
34 #define WMAX_METHOD_HDMI_CABLE 0x5
35 #define WMAX_METHOD_AMPLIFIER_CABLE 0x6
36 #define WMAX_METHOD_DEEP_SLEEP_CONTROL 0x0B
37 #define WMAX_METHOD_DEEP_SLEEP_STATUS 0x0C
39 MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>");
40 MODULE_DESCRIPTION("Alienware special feature control");
41 MODULE_LICENSE("GPL");
42 MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID
);
43 MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID
);
45 enum INTERFACE_FLAGS
{
50 enum LEGACY_CONTROL_STATES
{
56 enum WMAX_CONTROL_STATES
{
69 static struct quirk_entry
*quirks
;
71 static struct quirk_entry quirk_unknown
= {
78 static struct quirk_entry quirk_x51_r1_r2
= {
85 static struct quirk_entry quirk_x51_r3
= {
92 static struct quirk_entry quirk_asm100
= {
99 static struct quirk_entry quirk_asm200
= {
106 static struct quirk_entry quirk_asm201
= {
113 static int __init
dmi_matched(const struct dmi_system_id
*dmi
)
115 quirks
= dmi
->driver_data
;
119 static const struct dmi_system_id alienware_quirks
[] __initconst
= {
121 .callback
= dmi_matched
,
122 .ident
= "Alienware X51 R3",
124 DMI_MATCH(DMI_SYS_VENDOR
, "Alienware"),
125 DMI_MATCH(DMI_PRODUCT_NAME
, "Alienware X51 R3"),
127 .driver_data
= &quirk_x51_r3
,
130 .callback
= dmi_matched
,
131 .ident
= "Alienware X51 R2",
133 DMI_MATCH(DMI_SYS_VENDOR
, "Alienware"),
134 DMI_MATCH(DMI_PRODUCT_NAME
, "Alienware X51 R2"),
136 .driver_data
= &quirk_x51_r1_r2
,
139 .callback
= dmi_matched
,
140 .ident
= "Alienware X51 R1",
142 DMI_MATCH(DMI_SYS_VENDOR
, "Alienware"),
143 DMI_MATCH(DMI_PRODUCT_NAME
, "Alienware X51"),
145 .driver_data
= &quirk_x51_r1_r2
,
148 .callback
= dmi_matched
,
149 .ident
= "Alienware ASM100",
151 DMI_MATCH(DMI_SYS_VENDOR
, "Alienware"),
152 DMI_MATCH(DMI_PRODUCT_NAME
, "ASM100"),
154 .driver_data
= &quirk_asm100
,
157 .callback
= dmi_matched
,
158 .ident
= "Alienware ASM200",
160 DMI_MATCH(DMI_SYS_VENDOR
, "Alienware"),
161 DMI_MATCH(DMI_PRODUCT_NAME
, "ASM200"),
163 .driver_data
= &quirk_asm200
,
166 .callback
= dmi_matched
,
167 .ident
= "Alienware ASM201",
169 DMI_MATCH(DMI_SYS_VENDOR
, "Alienware"),
170 DMI_MATCH(DMI_PRODUCT_NAME
, "ASM201"),
172 .driver_data
= &quirk_asm201
,
177 struct color_platform
{
183 struct platform_zone
{
185 struct device_attribute
*attr
;
186 struct color_platform colors
;
189 struct wmax_brightness_args
{
194 struct wmax_basic_args
{
198 struct legacy_led_args
{
199 struct color_platform colors
;
204 struct wmax_led_args
{
206 struct color_platform colors
;
210 static struct platform_device
*platform_device
;
211 static struct device_attribute
*zone_dev_attrs
;
212 static struct attribute
**zone_attrs
;
213 static struct platform_zone
*zone_data
;
215 static struct platform_driver platform_driver
= {
217 .name
= "alienware-wmi",
221 static struct attribute_group zone_attribute_group
= {
226 static u8 lighting_control_state
;
227 static u8 global_brightness
;
230 * Helpers used for zone control
232 static int parse_rgb(const char *buf
, struct platform_zone
*zone
)
234 long unsigned int rgb
;
237 struct color_platform cp
;
241 ret
= kstrtoul(buf
, 16, &rgb
);
245 /* RGB triplet notation is 24-bit hexadecimal */
249 repackager
.package
= rgb
& 0x0f0f0f0f;
250 pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
251 repackager
.cp
.red
, repackager
.cp
.green
, repackager
.cp
.blue
);
252 zone
->colors
= repackager
.cp
;
256 static struct platform_zone
*match_zone(struct device_attribute
*attr
)
259 for (i
= 0; i
< quirks
->num_zones
; i
++) {
260 if ((struct device_attribute
*)zone_data
[i
].attr
== attr
) {
261 pr_debug("alienware-wmi: matched zone location: %d\n",
262 zone_data
[i
].location
);
263 return &zone_data
[i
];
270 * Individual RGB zone control
272 static int alienware_update_led(struct platform_zone
*zone
)
277 struct acpi_buffer input
;
278 struct legacy_led_args legacy_args
;
279 struct wmax_led_args wmax_basic_args
;
280 if (interface
== WMAX
) {
281 wmax_basic_args
.led_mask
= 1 << zone
->location
;
282 wmax_basic_args
.colors
= zone
->colors
;
283 wmax_basic_args
.state
= lighting_control_state
;
284 guid
= WMAX_CONTROL_GUID
;
285 method_id
= WMAX_METHOD_ZONE_CONTROL
;
287 input
.length
= (acpi_size
) sizeof(wmax_basic_args
);
288 input
.pointer
= &wmax_basic_args
;
290 legacy_args
.colors
= zone
->colors
;
291 legacy_args
.brightness
= global_brightness
;
292 legacy_args
.state
= 0;
293 if (lighting_control_state
== LEGACY_BOOTING
||
294 lighting_control_state
== LEGACY_SUSPEND
) {
295 guid
= LEGACY_POWER_CONTROL_GUID
;
296 legacy_args
.state
= lighting_control_state
;
298 guid
= LEGACY_CONTROL_GUID
;
299 method_id
= zone
->location
+ 1;
301 input
.length
= (acpi_size
) sizeof(legacy_args
);
302 input
.pointer
= &legacy_args
;
304 pr_debug("alienware-wmi: guid %s method %d\n", guid
, method_id
);
306 status
= wmi_evaluate_method(guid
, 1, method_id
, &input
, NULL
);
307 if (ACPI_FAILURE(status
))
308 pr_err("alienware-wmi: zone set failure: %u\n", status
);
309 return ACPI_FAILURE(status
);
312 static ssize_t
zone_show(struct device
*dev
, struct device_attribute
*attr
,
315 struct platform_zone
*target_zone
;
316 target_zone
= match_zone(attr
);
317 if (target_zone
== NULL
)
318 return sprintf(buf
, "red: -1, green: -1, blue: -1\n");
319 return sprintf(buf
, "red: %d, green: %d, blue: %d\n",
320 target_zone
->colors
.red
,
321 target_zone
->colors
.green
, target_zone
->colors
.blue
);
325 static ssize_t
zone_set(struct device
*dev
, struct device_attribute
*attr
,
326 const char *buf
, size_t count
)
328 struct platform_zone
*target_zone
;
330 target_zone
= match_zone(attr
);
331 if (target_zone
== NULL
) {
332 pr_err("alienware-wmi: invalid target zone\n");
335 ret
= parse_rgb(buf
, target_zone
);
338 ret
= alienware_update_led(target_zone
);
339 return ret
? ret
: count
;
343 * LED Brightness (Global)
345 static int wmax_brightness(int brightness
)
348 struct acpi_buffer input
;
349 struct wmax_brightness_args args
= {
351 .percentage
= brightness
,
353 input
.length
= (acpi_size
) sizeof(args
);
354 input
.pointer
= &args
;
355 status
= wmi_evaluate_method(WMAX_CONTROL_GUID
, 1,
356 WMAX_METHOD_BRIGHTNESS
, &input
, NULL
);
357 if (ACPI_FAILURE(status
))
358 pr_err("alienware-wmi: brightness set failure: %u\n", status
);
359 return ACPI_FAILURE(status
);
362 static void global_led_set(struct led_classdev
*led_cdev
,
363 enum led_brightness brightness
)
366 global_brightness
= brightness
;
367 if (interface
== WMAX
)
368 ret
= wmax_brightness(brightness
);
370 ret
= alienware_update_led(&zone_data
[0]);
372 pr_err("LED brightness update failed\n");
375 static enum led_brightness
global_led_get(struct led_classdev
*led_cdev
)
377 return global_brightness
;
380 static struct led_classdev global_led
= {
381 .brightness_set
= global_led_set
,
382 .brightness_get
= global_led_get
,
383 .name
= "alienware::global_brightness",
387 * Lighting control state device attribute (Global)
389 static ssize_t
show_control_state(struct device
*dev
,
390 struct device_attribute
*attr
, char *buf
)
392 if (lighting_control_state
== LEGACY_BOOTING
)
393 return scnprintf(buf
, PAGE_SIZE
, "[booting] running suspend\n");
394 else if (lighting_control_state
== LEGACY_SUSPEND
)
395 return scnprintf(buf
, PAGE_SIZE
, "booting running [suspend]\n");
396 return scnprintf(buf
, PAGE_SIZE
, "booting [running] suspend\n");
399 static ssize_t
store_control_state(struct device
*dev
,
400 struct device_attribute
*attr
,
401 const char *buf
, size_t count
)
403 long unsigned int val
;
404 if (strcmp(buf
, "booting\n") == 0)
405 val
= LEGACY_BOOTING
;
406 else if (strcmp(buf
, "suspend\n") == 0)
407 val
= LEGACY_SUSPEND
;
408 else if (interface
== LEGACY
)
409 val
= LEGACY_RUNNING
;
412 lighting_control_state
= val
;
413 pr_debug("alienware-wmi: updated control state to %d\n",
414 lighting_control_state
);
418 static DEVICE_ATTR(lighting_control_state
, 0644, show_control_state
,
419 store_control_state
);
421 static int alienware_zone_init(struct platform_device
*dev
)
427 if (interface
== WMAX
) {
428 lighting_control_state
= WMAX_RUNNING
;
429 } else if (interface
== LEGACY
) {
430 lighting_control_state
= LEGACY_RUNNING
;
432 global_led
.max_brightness
= 0x0F;
433 global_brightness
= global_led
.max_brightness
;
436 * - zone_dev_attrs num_zones + 1 is for individual zones and then
438 * - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
439 * the lighting control + null terminated
440 * - zone_data num_zones is for the distinct zones
443 kzalloc(sizeof(struct device_attribute
) * (quirks
->num_zones
+ 1),
449 kzalloc(sizeof(struct attribute
*) * (quirks
->num_zones
+ 2),
455 kzalloc(sizeof(struct platform_zone
) * (quirks
->num_zones
),
460 for (i
= 0; i
< quirks
->num_zones
; i
++) {
461 sprintf(buffer
, "zone%02X", i
);
462 name
= kstrdup(buffer
, GFP_KERNEL
);
465 sysfs_attr_init(&zone_dev_attrs
[i
].attr
);
466 zone_dev_attrs
[i
].attr
.name
= name
;
467 zone_dev_attrs
[i
].attr
.mode
= 0644;
468 zone_dev_attrs
[i
].show
= zone_show
;
469 zone_dev_attrs
[i
].store
= zone_set
;
470 zone_data
[i
].location
= i
;
471 zone_attrs
[i
] = &zone_dev_attrs
[i
].attr
;
472 zone_data
[i
].attr
= &zone_dev_attrs
[i
];
474 zone_attrs
[quirks
->num_zones
] = &dev_attr_lighting_control_state
.attr
;
475 zone_attribute_group
.attrs
= zone_attrs
;
477 led_classdev_register(&dev
->dev
, &global_led
);
479 return sysfs_create_group(&dev
->dev
.kobj
, &zone_attribute_group
);
482 static void alienware_zone_exit(struct platform_device
*dev
)
484 sysfs_remove_group(&dev
->dev
.kobj
, &zone_attribute_group
);
485 led_classdev_unregister(&global_led
);
486 if (zone_dev_attrs
) {
488 for (i
= 0; i
< quirks
->num_zones
; i
++)
489 kfree(zone_dev_attrs
[i
].attr
.name
);
491 kfree(zone_dev_attrs
);
496 static acpi_status
alienware_wmax_command(struct wmax_basic_args
*in_args
,
497 u32 command
, int *out_data
)
500 union acpi_object
*obj
;
501 struct acpi_buffer input
;
502 struct acpi_buffer output
;
504 input
.length
= (acpi_size
) sizeof(*in_args
);
505 input
.pointer
= in_args
;
506 if (out_data
!= NULL
) {
507 output
.length
= ACPI_ALLOCATE_BUFFER
;
508 output
.pointer
= NULL
;
509 status
= wmi_evaluate_method(WMAX_CONTROL_GUID
, 1,
510 command
, &input
, &output
);
512 status
= wmi_evaluate_method(WMAX_CONTROL_GUID
, 1,
513 command
, &input
, NULL
);
515 if (ACPI_SUCCESS(status
) && out_data
!= NULL
) {
516 obj
= (union acpi_object
*)output
.pointer
;
517 if (obj
&& obj
->type
== ACPI_TYPE_INTEGER
)
518 *out_data
= (u32
) obj
->integer
.value
;
525 * The HDMI mux sysfs node indicates the status of the HDMI input mux.
526 * It can toggle between standard system GPU output and HDMI input.
528 static ssize_t
show_hdmi_cable(struct device
*dev
,
529 struct device_attribute
*attr
, char *buf
)
533 struct wmax_basic_args in_args
= {
537 alienware_wmax_command(&in_args
, WMAX_METHOD_HDMI_CABLE
,
539 if (ACPI_SUCCESS(status
)) {
541 return scnprintf(buf
, PAGE_SIZE
,
542 "[unconnected] connected unknown\n");
543 else if (out_data
== 1)
544 return scnprintf(buf
, PAGE_SIZE
,
545 "unconnected [connected] unknown\n");
547 pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status
);
548 return scnprintf(buf
, PAGE_SIZE
, "unconnected connected [unknown]\n");
551 static ssize_t
show_hdmi_source(struct device
*dev
,
552 struct device_attribute
*attr
, char *buf
)
556 struct wmax_basic_args in_args
= {
560 alienware_wmax_command(&in_args
, WMAX_METHOD_HDMI_STATUS
,
563 if (ACPI_SUCCESS(status
)) {
565 return scnprintf(buf
, PAGE_SIZE
,
566 "[input] gpu unknown\n");
567 else if (out_data
== 2)
568 return scnprintf(buf
, PAGE_SIZE
,
569 "input [gpu] unknown\n");
571 pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data
);
572 return scnprintf(buf
, PAGE_SIZE
, "input gpu [unknown]\n");
575 static ssize_t
toggle_hdmi_source(struct device
*dev
,
576 struct device_attribute
*attr
,
577 const char *buf
, size_t count
)
580 struct wmax_basic_args args
;
581 if (strcmp(buf
, "gpu\n") == 0)
583 else if (strcmp(buf
, "input\n") == 0)
587 pr_debug("alienware-wmi: setting hdmi to %d : %s", args
.arg
, buf
);
589 status
= alienware_wmax_command(&args
, WMAX_METHOD_HDMI_SOURCE
, NULL
);
591 if (ACPI_FAILURE(status
))
592 pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
597 static DEVICE_ATTR(cable
, S_IRUGO
, show_hdmi_cable
, NULL
);
598 static DEVICE_ATTR(source
, S_IRUGO
| S_IWUSR
, show_hdmi_source
,
601 static struct attribute
*hdmi_attrs
[] = {
602 &dev_attr_cable
.attr
,
603 &dev_attr_source
.attr
,
607 static struct attribute_group hdmi_attribute_group
= {
612 static void remove_hdmi(struct platform_device
*dev
)
614 if (quirks
->hdmi_mux
> 0)
615 sysfs_remove_group(&dev
->dev
.kobj
, &hdmi_attribute_group
);
618 static int create_hdmi(struct platform_device
*dev
)
622 ret
= sysfs_create_group(&dev
->dev
.kobj
, &hdmi_attribute_group
);
629 * Alienware GFX amplifier support
630 * - Currently supports reading cable status
631 * - Leaving expansion room to possibly support dock/undock events later
633 static ssize_t
show_amplifier_status(struct device
*dev
,
634 struct device_attribute
*attr
, char *buf
)
638 struct wmax_basic_args in_args
= {
642 alienware_wmax_command(&in_args
, WMAX_METHOD_AMPLIFIER_CABLE
,
644 if (ACPI_SUCCESS(status
)) {
646 return scnprintf(buf
, PAGE_SIZE
,
647 "[unconnected] connected unknown\n");
648 else if (out_data
== 1)
649 return scnprintf(buf
, PAGE_SIZE
,
650 "unconnected [connected] unknown\n");
652 pr_err("alienware-wmi: unknown amplifier cable status: %d\n", status
);
653 return scnprintf(buf
, PAGE_SIZE
, "unconnected connected [unknown]\n");
656 static DEVICE_ATTR(status
, S_IRUGO
, show_amplifier_status
, NULL
);
658 static struct attribute
*amplifier_attrs
[] = {
659 &dev_attr_status
.attr
,
663 static struct attribute_group amplifier_attribute_group
= {
665 .attrs
= amplifier_attrs
,
668 static void remove_amplifier(struct platform_device
*dev
)
670 if (quirks
->amplifier
> 0)
671 sysfs_remove_group(&dev
->dev
.kobj
, &lifier_attribute_group
);
674 static int create_amplifier(struct platform_device
*dev
)
678 ret
= sysfs_create_group(&dev
->dev
.kobj
, &lifier_attribute_group
);
680 remove_amplifier(dev
);
685 * Deep Sleep Control support
686 * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
688 static ssize_t
show_deepsleep_status(struct device
*dev
,
689 struct device_attribute
*attr
, char *buf
)
693 struct wmax_basic_args in_args
= {
696 status
= alienware_wmax_command(&in_args
, WMAX_METHOD_DEEP_SLEEP_STATUS
,
698 if (ACPI_SUCCESS(status
)) {
700 return scnprintf(buf
, PAGE_SIZE
,
701 "[disabled] s5 s5_s4\n");
702 else if (out_data
== 1)
703 return scnprintf(buf
, PAGE_SIZE
,
704 "disabled [s5] s5_s4\n");
705 else if (out_data
== 2)
706 return scnprintf(buf
, PAGE_SIZE
,
707 "disabled s5 [s5_s4]\n");
709 pr_err("alienware-wmi: unknown deep sleep status: %d\n", status
);
710 return scnprintf(buf
, PAGE_SIZE
, "disabled s5 s5_s4 [unknown]\n");
713 static ssize_t
toggle_deepsleep(struct device
*dev
,
714 struct device_attribute
*attr
,
715 const char *buf
, size_t count
)
718 struct wmax_basic_args args
;
720 if (strcmp(buf
, "disabled\n") == 0)
722 else if (strcmp(buf
, "s5\n") == 0)
726 pr_debug("alienware-wmi: setting deep sleep to %d : %s", args
.arg
, buf
);
728 status
= alienware_wmax_command(&args
, WMAX_METHOD_DEEP_SLEEP_CONTROL
,
731 if (ACPI_FAILURE(status
))
732 pr_err("alienware-wmi: deep sleep control failed: results: %u\n",
737 static DEVICE_ATTR(deepsleep
, S_IRUGO
| S_IWUSR
, show_deepsleep_status
, toggle_deepsleep
);
739 static struct attribute
*deepsleep_attrs
[] = {
740 &dev_attr_deepsleep
.attr
,
744 static struct attribute_group deepsleep_attribute_group
= {
746 .attrs
= deepsleep_attrs
,
749 static void remove_deepsleep(struct platform_device
*dev
)
751 if (quirks
->deepslp
> 0)
752 sysfs_remove_group(&dev
->dev
.kobj
, &deepsleep_attribute_group
);
755 static int create_deepsleep(struct platform_device
*dev
)
759 ret
= sysfs_create_group(&dev
->dev
.kobj
, &deepsleep_attribute_group
);
761 remove_deepsleep(dev
);
765 static int __init
alienware_wmi_init(void)
769 if (wmi_has_guid(LEGACY_CONTROL_GUID
))
771 else if (wmi_has_guid(WMAX_CONTROL_GUID
))
774 pr_warn("alienware-wmi: No known WMI GUID found\n");
778 dmi_check_system(alienware_quirks
);
780 quirks
= &quirk_unknown
;
782 ret
= platform_driver_register(&platform_driver
);
784 goto fail_platform_driver
;
785 platform_device
= platform_device_alloc("alienware-wmi", -1);
786 if (!platform_device
) {
788 goto fail_platform_device1
;
790 ret
= platform_device_add(platform_device
);
792 goto fail_platform_device2
;
794 if (quirks
->hdmi_mux
> 0) {
795 ret
= create_hdmi(platform_device
);
800 if (quirks
->amplifier
> 0) {
801 ret
= create_amplifier(platform_device
);
803 goto fail_prep_amplifier
;
806 if (quirks
->deepslp
> 0) {
807 ret
= create_deepsleep(platform_device
);
809 goto fail_prep_deepsleep
;
812 ret
= alienware_zone_init(platform_device
);
814 goto fail_prep_zones
;
819 alienware_zone_exit(platform_device
);
823 platform_device_del(platform_device
);
824 fail_platform_device2
:
825 platform_device_put(platform_device
);
826 fail_platform_device1
:
827 platform_driver_unregister(&platform_driver
);
828 fail_platform_driver
:
832 module_init(alienware_wmi_init
);
834 static void __exit
alienware_wmi_exit(void)
836 if (platform_device
) {
837 alienware_zone_exit(platform_device
);
838 remove_hdmi(platform_device
);
839 platform_device_unregister(platform_device
);
840 platform_driver_unregister(&platform_driver
);
844 module_exit(alienware_wmi_exit
);