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/acpi.h>
25 #include <linux/leds.h>
27 #define LEGACY_CONTROL_GUID "A90597CE-A997-11DA-B012-B622A1EF5492"
28 #define LEGACY_POWER_CONTROL_GUID "A80593CE-A997-11DA-B012-B622A1EF5492"
29 #define WMAX_CONTROL_GUID "A70591CE-A997-11DA-B012-B622A1EF5492"
31 #define WMAX_METHOD_HDMI_SOURCE 0x1
32 #define WMAX_METHOD_HDMI_STATUS 0x2
33 #define WMAX_METHOD_BRIGHTNESS 0x3
34 #define WMAX_METHOD_ZONE_CONTROL 0x4
35 #define WMAX_METHOD_HDMI_CABLE 0x5
36 #define WMAX_METHOD_AMPLIFIER_CABLE 0x6
37 #define WMAX_METHOD_DEEP_SLEEP_CONTROL 0x0B
38 #define WMAX_METHOD_DEEP_SLEEP_STATUS 0x0C
40 MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>");
41 MODULE_DESCRIPTION("Alienware special feature control");
42 MODULE_LICENSE("GPL");
43 MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID);
44 MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID);
46 enum INTERFACE_FLAGS {
51 enum LEGACY_CONTROL_STATES {
57 enum WMAX_CONTROL_STATES {
70 static struct quirk_entry *quirks;
72 static struct quirk_entry quirk_unknown = {
79 static struct quirk_entry quirk_x51_r1_r2 = {
86 static struct quirk_entry quirk_x51_r3 = {
93 static struct quirk_entry quirk_asm100 = {
100 static struct quirk_entry quirk_asm200 = {
107 static struct quirk_entry quirk_asm201 = {
114 static int __init dmi_matched(const struct dmi_system_id *dmi)
116 quirks = dmi->driver_data;
120 static const struct dmi_system_id alienware_quirks[] __initconst = {
122 .callback = dmi_matched,
123 .ident = "Alienware X51 R3",
125 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
126 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R3"),
128 .driver_data = &quirk_x51_r3,
131 .callback = dmi_matched,
132 .ident = "Alienware X51 R2",
134 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
135 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
137 .driver_data = &quirk_x51_r1_r2,
140 .callback = dmi_matched,
141 .ident = "Alienware X51 R1",
143 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
144 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
146 .driver_data = &quirk_x51_r1_r2,
149 .callback = dmi_matched,
150 .ident = "Alienware ASM100",
152 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
153 DMI_MATCH(DMI_PRODUCT_NAME, "ASM100"),
155 .driver_data = &quirk_asm100,
158 .callback = dmi_matched,
159 .ident = "Alienware ASM200",
161 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
162 DMI_MATCH(DMI_PRODUCT_NAME, "ASM200"),
164 .driver_data = &quirk_asm200,
167 .callback = dmi_matched,
168 .ident = "Alienware ASM201",
170 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
171 DMI_MATCH(DMI_PRODUCT_NAME, "ASM201"),
173 .driver_data = &quirk_asm201,
178 struct color_platform {
184 struct platform_zone {
186 struct device_attribute *attr;
187 struct color_platform colors;
190 struct wmax_brightness_args {
195 struct wmax_basic_args {
199 struct legacy_led_args {
200 struct color_platform colors;
205 struct wmax_led_args {
207 struct color_platform colors;
211 static struct platform_device *platform_device;
212 static struct device_attribute *zone_dev_attrs;
213 static struct attribute **zone_attrs;
214 static struct platform_zone *zone_data;
216 static struct platform_driver platform_driver = {
218 .name = "alienware-wmi",
222 static struct attribute_group zone_attribute_group = {
227 static u8 lighting_control_state;
228 static u8 global_brightness;
231 * Helpers used for zone control
233 static int parse_rgb(const char *buf, struct platform_zone *zone)
235 long unsigned int rgb;
238 struct color_platform cp;
242 ret = kstrtoul(buf, 16, &rgb);
246 /* RGB triplet notation is 24-bit hexadecimal */
250 repackager.package = rgb & 0x0f0f0f0f;
251 pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
252 repackager.cp.red, repackager.cp.green, repackager.cp.blue);
253 zone->colors = repackager.cp;
257 static struct platform_zone *match_zone(struct device_attribute *attr)
260 for (i = 0; i < quirks->num_zones; i++) {
261 if ((struct device_attribute *)zone_data[i].attr == attr) {
262 pr_debug("alienware-wmi: matched zone location: %d\n",
263 zone_data[i].location);
264 return &zone_data[i];
271 * Individual RGB zone control
273 static int alienware_update_led(struct platform_zone *zone)
278 struct acpi_buffer input;
279 struct legacy_led_args legacy_args;
280 struct wmax_led_args wmax_basic_args;
281 if (interface == WMAX) {
282 wmax_basic_args.led_mask = 1 << zone->location;
283 wmax_basic_args.colors = zone->colors;
284 wmax_basic_args.state = lighting_control_state;
285 guid = WMAX_CONTROL_GUID;
286 method_id = WMAX_METHOD_ZONE_CONTROL;
288 input.length = (acpi_size) sizeof(wmax_basic_args);
289 input.pointer = &wmax_basic_args;
291 legacy_args.colors = zone->colors;
292 legacy_args.brightness = global_brightness;
293 legacy_args.state = 0;
294 if (lighting_control_state == LEGACY_BOOTING ||
295 lighting_control_state == LEGACY_SUSPEND) {
296 guid = LEGACY_POWER_CONTROL_GUID;
297 legacy_args.state = lighting_control_state;
299 guid = LEGACY_CONTROL_GUID;
300 method_id = zone->location + 1;
302 input.length = (acpi_size) sizeof(legacy_args);
303 input.pointer = &legacy_args;
305 pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
307 status = wmi_evaluate_method(guid, 1, method_id, &input, NULL);
308 if (ACPI_FAILURE(status))
309 pr_err("alienware-wmi: zone set failure: %u\n", status);
310 return ACPI_FAILURE(status);
313 static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
316 struct platform_zone *target_zone;
317 target_zone = match_zone(attr);
318 if (target_zone == NULL)
319 return sprintf(buf, "red: -1, green: -1, blue: -1\n");
320 return sprintf(buf, "red: %d, green: %d, blue: %d\n",
321 target_zone->colors.red,
322 target_zone->colors.green, target_zone->colors.blue);
326 static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
327 const char *buf, size_t count)
329 struct platform_zone *target_zone;
331 target_zone = match_zone(attr);
332 if (target_zone == NULL) {
333 pr_err("alienware-wmi: invalid target zone\n");
336 ret = parse_rgb(buf, target_zone);
339 ret = alienware_update_led(target_zone);
340 return ret ? ret : count;
344 * LED Brightness (Global)
346 static int wmax_brightness(int brightness)
349 struct acpi_buffer input;
350 struct wmax_brightness_args args = {
352 .percentage = brightness,
354 input.length = (acpi_size) sizeof(args);
355 input.pointer = &args;
356 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
357 WMAX_METHOD_BRIGHTNESS, &input, NULL);
358 if (ACPI_FAILURE(status))
359 pr_err("alienware-wmi: brightness set failure: %u\n", status);
360 return ACPI_FAILURE(status);
363 static void global_led_set(struct led_classdev *led_cdev,
364 enum led_brightness brightness)
367 global_brightness = brightness;
368 if (interface == WMAX)
369 ret = wmax_brightness(brightness);
371 ret = alienware_update_led(&zone_data[0]);
373 pr_err("LED brightness update failed\n");
376 static enum led_brightness global_led_get(struct led_classdev *led_cdev)
378 return global_brightness;
381 static struct led_classdev global_led = {
382 .brightness_set = global_led_set,
383 .brightness_get = global_led_get,
384 .name = "alienware::global_brightness",
388 * Lighting control state device attribute (Global)
390 static ssize_t show_control_state(struct device *dev,
391 struct device_attribute *attr, char *buf)
393 if (lighting_control_state == LEGACY_BOOTING)
394 return scnprintf(buf, PAGE_SIZE, "[booting] running suspend\n");
395 else if (lighting_control_state == LEGACY_SUSPEND)
396 return scnprintf(buf, PAGE_SIZE, "booting running [suspend]\n");
397 return scnprintf(buf, PAGE_SIZE, "booting [running] suspend\n");
400 static ssize_t store_control_state(struct device *dev,
401 struct device_attribute *attr,
402 const char *buf, size_t count)
404 long unsigned int val;
405 if (strcmp(buf, "booting\n") == 0)
406 val = LEGACY_BOOTING;
407 else if (strcmp(buf, "suspend\n") == 0)
408 val = LEGACY_SUSPEND;
409 else if (interface == LEGACY)
410 val = LEGACY_RUNNING;
413 lighting_control_state = val;
414 pr_debug("alienware-wmi: updated control state to %d\n",
415 lighting_control_state);
419 static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
420 store_control_state);
422 static int alienware_zone_init(struct platform_device *dev)
428 if (interface == WMAX) {
429 lighting_control_state = WMAX_RUNNING;
430 } else if (interface == LEGACY) {
431 lighting_control_state = LEGACY_RUNNING;
433 global_led.max_brightness = 0x0F;
434 global_brightness = global_led.max_brightness;
437 * - zone_dev_attrs num_zones + 1 is for individual zones and then
439 * - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
440 * the lighting control + null terminated
441 * - zone_data num_zones is for the distinct zones
444 kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
450 kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
456 kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
461 for (i = 0; i < quirks->num_zones; i++) {
462 sprintf(buffer, "zone%02X", i);
463 name = kstrdup(buffer, GFP_KERNEL);
466 sysfs_attr_init(&zone_dev_attrs[i].attr);
467 zone_dev_attrs[i].attr.name = name;
468 zone_dev_attrs[i].attr.mode = 0644;
469 zone_dev_attrs[i].show = zone_show;
470 zone_dev_attrs[i].store = zone_set;
471 zone_data[i].location = i;
472 zone_attrs[i] = &zone_dev_attrs[i].attr;
473 zone_data[i].attr = &zone_dev_attrs[i];
475 zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
476 zone_attribute_group.attrs = zone_attrs;
478 led_classdev_register(&dev->dev, &global_led);
480 return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
483 static void alienware_zone_exit(struct platform_device *dev)
485 sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
486 led_classdev_unregister(&global_led);
487 if (zone_dev_attrs) {
489 for (i = 0; i < quirks->num_zones; i++)
490 kfree(zone_dev_attrs[i].attr.name);
492 kfree(zone_dev_attrs);
497 static acpi_status alienware_wmax_command(struct wmax_basic_args *in_args,
498 u32 command, int *out_data)
501 union acpi_object *obj;
502 struct acpi_buffer input;
503 struct acpi_buffer output;
505 input.length = (acpi_size) sizeof(*in_args);
506 input.pointer = in_args;
508 output.length = ACPI_ALLOCATE_BUFFER;
509 output.pointer = NULL;
510 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
511 command, &input, &output);
512 if (ACPI_SUCCESS(status)) {
513 obj = (union acpi_object *)output.pointer;
514 if (obj && obj->type == ACPI_TYPE_INTEGER)
515 *out_data = (u32)obj->integer.value;
517 kfree(output.pointer);
519 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
520 command, &input, NULL);
526 * The HDMI mux sysfs node indicates the status of the HDMI input mux.
527 * It can toggle between standard system GPU output and HDMI input.
529 static ssize_t show_hdmi_cable(struct device *dev,
530 struct device_attribute *attr, char *buf)
534 struct wmax_basic_args in_args = {
538 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_CABLE,
540 if (ACPI_SUCCESS(status)) {
542 return scnprintf(buf, PAGE_SIZE,
543 "[unconnected] connected unknown\n");
544 else if (out_data == 1)
545 return scnprintf(buf, PAGE_SIZE,
546 "unconnected [connected] unknown\n");
548 pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
549 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
552 static ssize_t show_hdmi_source(struct device *dev,
553 struct device_attribute *attr, char *buf)
557 struct wmax_basic_args in_args = {
561 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_STATUS,
564 if (ACPI_SUCCESS(status)) {
566 return scnprintf(buf, PAGE_SIZE,
567 "[input] gpu unknown\n");
568 else if (out_data == 2)
569 return scnprintf(buf, PAGE_SIZE,
570 "input [gpu] unknown\n");
572 pr_err("alienware-wmi: unknown HDMI source status: %u\n", status);
573 return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
576 static ssize_t toggle_hdmi_source(struct device *dev,
577 struct device_attribute *attr,
578 const char *buf, size_t count)
581 struct wmax_basic_args args;
582 if (strcmp(buf, "gpu\n") == 0)
584 else if (strcmp(buf, "input\n") == 0)
588 pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
590 status = alienware_wmax_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
592 if (ACPI_FAILURE(status))
593 pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
598 static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
599 static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
602 static struct attribute *hdmi_attrs[] = {
603 &dev_attr_cable.attr,
604 &dev_attr_source.attr,
608 static struct attribute_group hdmi_attribute_group = {
613 static void remove_hdmi(struct platform_device *dev)
615 if (quirks->hdmi_mux > 0)
616 sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
619 static int create_hdmi(struct platform_device *dev)
623 ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
630 * Alienware GFX amplifier support
631 * - Currently supports reading cable status
632 * - Leaving expansion room to possibly support dock/undock events later
634 static ssize_t show_amplifier_status(struct device *dev,
635 struct device_attribute *attr, char *buf)
639 struct wmax_basic_args in_args = {
643 alienware_wmax_command(&in_args, WMAX_METHOD_AMPLIFIER_CABLE,
645 if (ACPI_SUCCESS(status)) {
647 return scnprintf(buf, PAGE_SIZE,
648 "[unconnected] connected unknown\n");
649 else if (out_data == 1)
650 return scnprintf(buf, PAGE_SIZE,
651 "unconnected [connected] unknown\n");
653 pr_err("alienware-wmi: unknown amplifier cable status: %d\n", status);
654 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
657 static DEVICE_ATTR(status, S_IRUGO, show_amplifier_status, NULL);
659 static struct attribute *amplifier_attrs[] = {
660 &dev_attr_status.attr,
664 static struct attribute_group amplifier_attribute_group = {
666 .attrs = amplifier_attrs,
669 static void remove_amplifier(struct platform_device *dev)
671 if (quirks->amplifier > 0)
672 sysfs_remove_group(&dev->dev.kobj, &lifier_attribute_group);
675 static int create_amplifier(struct platform_device *dev)
679 ret = sysfs_create_group(&dev->dev.kobj, &lifier_attribute_group);
681 remove_amplifier(dev);
686 * Deep Sleep Control support
687 * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
689 static ssize_t show_deepsleep_status(struct device *dev,
690 struct device_attribute *attr, char *buf)
694 struct wmax_basic_args in_args = {
697 status = alienware_wmax_command(&in_args, WMAX_METHOD_DEEP_SLEEP_STATUS,
699 if (ACPI_SUCCESS(status)) {
701 return scnprintf(buf, PAGE_SIZE,
702 "[disabled] s5 s5_s4\n");
703 else if (out_data == 1)
704 return scnprintf(buf, PAGE_SIZE,
705 "disabled [s5] s5_s4\n");
706 else if (out_data == 2)
707 return scnprintf(buf, PAGE_SIZE,
708 "disabled s5 [s5_s4]\n");
710 pr_err("alienware-wmi: unknown deep sleep status: %d\n", status);
711 return scnprintf(buf, PAGE_SIZE, "disabled s5 s5_s4 [unknown]\n");
714 static ssize_t toggle_deepsleep(struct device *dev,
715 struct device_attribute *attr,
716 const char *buf, size_t count)
719 struct wmax_basic_args args;
721 if (strcmp(buf, "disabled\n") == 0)
723 else if (strcmp(buf, "s5\n") == 0)
727 pr_debug("alienware-wmi: setting deep sleep to %d : %s", args.arg, buf);
729 status = alienware_wmax_command(&args, WMAX_METHOD_DEEP_SLEEP_CONTROL,
732 if (ACPI_FAILURE(status))
733 pr_err("alienware-wmi: deep sleep control failed: results: %u\n",
738 static DEVICE_ATTR(deepsleep, S_IRUGO | S_IWUSR, show_deepsleep_status, toggle_deepsleep);
740 static struct attribute *deepsleep_attrs[] = {
741 &dev_attr_deepsleep.attr,
745 static struct attribute_group deepsleep_attribute_group = {
747 .attrs = deepsleep_attrs,
750 static void remove_deepsleep(struct platform_device *dev)
752 if (quirks->deepslp > 0)
753 sysfs_remove_group(&dev->dev.kobj, &deepsleep_attribute_group);
756 static int create_deepsleep(struct platform_device *dev)
760 ret = sysfs_create_group(&dev->dev.kobj, &deepsleep_attribute_group);
762 remove_deepsleep(dev);
766 static int __init alienware_wmi_init(void)
770 if (wmi_has_guid(LEGACY_CONTROL_GUID))
772 else if (wmi_has_guid(WMAX_CONTROL_GUID))
775 pr_warn("alienware-wmi: No known WMI GUID found\n");
779 dmi_check_system(alienware_quirks);
781 quirks = &quirk_unknown;
783 ret = platform_driver_register(&platform_driver);
785 goto fail_platform_driver;
786 platform_device = platform_device_alloc("alienware-wmi", -1);
787 if (!platform_device) {
789 goto fail_platform_device1;
791 ret = platform_device_add(platform_device);
793 goto fail_platform_device2;
795 if (quirks->hdmi_mux > 0) {
796 ret = create_hdmi(platform_device);
801 if (quirks->amplifier > 0) {
802 ret = create_amplifier(platform_device);
804 goto fail_prep_amplifier;
807 if (quirks->deepslp > 0) {
808 ret = create_deepsleep(platform_device);
810 goto fail_prep_deepsleep;
813 ret = alienware_zone_init(platform_device);
815 goto fail_prep_zones;
820 alienware_zone_exit(platform_device);
824 platform_device_del(platform_device);
825 fail_platform_device2:
826 platform_device_put(platform_device);
827 fail_platform_device1:
828 platform_driver_unregister(&platform_driver);
829 fail_platform_driver:
833 module_init(alienware_wmi_init);
835 static void __exit alienware_wmi_exit(void)
837 if (platform_device) {
838 alienware_zone_exit(platform_device);
839 remove_hdmi(platform_device);
840 platform_device_unregister(platform_device);
841 platform_driver_unregister(&platform_driver);
845 module_exit(alienware_wmi_exit);