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 int __init dmi_matched(const struct dmi_system_id *dmi)
102 quirks = dmi->driver_data;
106 static const struct dmi_system_id alienware_quirks[] __initconst = {
108 .callback = dmi_matched,
109 .ident = "Alienware X51 R3",
111 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
112 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R3"),
114 .driver_data = &quirk_x51_r3,
117 .callback = dmi_matched,
118 .ident = "Alienware X51 R2",
120 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
121 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
123 .driver_data = &quirk_x51_r1_r2,
126 .callback = dmi_matched,
127 .ident = "Alienware X51 R1",
129 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
130 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
132 .driver_data = &quirk_x51_r1_r2,
135 .callback = dmi_matched,
136 .ident = "Alienware ASM100",
138 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
139 DMI_MATCH(DMI_PRODUCT_NAME, "ASM100"),
141 .driver_data = &quirk_asm100,
146 struct color_platform {
152 struct platform_zone {
154 struct device_attribute *attr;
155 struct color_platform colors;
158 struct wmax_brightness_args {
163 struct wmax_basic_args {
167 struct legacy_led_args {
168 struct color_platform colors;
173 struct wmax_led_args {
175 struct color_platform colors;
179 static struct platform_device *platform_device;
180 static struct device_attribute *zone_dev_attrs;
181 static struct attribute **zone_attrs;
182 static struct platform_zone *zone_data;
184 static struct platform_driver platform_driver = {
186 .name = "alienware-wmi",
190 static struct attribute_group zone_attribute_group = {
195 static u8 lighting_control_state;
196 static u8 global_brightness;
199 * Helpers used for zone control
201 static int parse_rgb(const char *buf, struct platform_zone *zone)
203 long unsigned int rgb;
206 struct color_platform cp;
210 ret = kstrtoul(buf, 16, &rgb);
214 /* RGB triplet notation is 24-bit hexadecimal */
218 repackager.package = rgb & 0x0f0f0f0f;
219 pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
220 repackager.cp.red, repackager.cp.green, repackager.cp.blue);
221 zone->colors = repackager.cp;
225 static struct platform_zone *match_zone(struct device_attribute *attr)
228 for (i = 0; i < quirks->num_zones; i++) {
229 if ((struct device_attribute *)zone_data[i].attr == attr) {
230 pr_debug("alienware-wmi: matched zone location: %d\n",
231 zone_data[i].location);
232 return &zone_data[i];
239 * Individual RGB zone control
241 static int alienware_update_led(struct platform_zone *zone)
246 struct acpi_buffer input;
247 struct legacy_led_args legacy_args;
248 struct wmax_led_args wmax_basic_args;
249 if (interface == WMAX) {
250 wmax_basic_args.led_mask = 1 << zone->location;
251 wmax_basic_args.colors = zone->colors;
252 wmax_basic_args.state = lighting_control_state;
253 guid = WMAX_CONTROL_GUID;
254 method_id = WMAX_METHOD_ZONE_CONTROL;
256 input.length = (acpi_size) sizeof(wmax_basic_args);
257 input.pointer = &wmax_basic_args;
259 legacy_args.colors = zone->colors;
260 legacy_args.brightness = global_brightness;
261 legacy_args.state = 0;
262 if (lighting_control_state == LEGACY_BOOTING ||
263 lighting_control_state == LEGACY_SUSPEND) {
264 guid = LEGACY_POWER_CONTROL_GUID;
265 legacy_args.state = lighting_control_state;
267 guid = LEGACY_CONTROL_GUID;
268 method_id = zone->location + 1;
270 input.length = (acpi_size) sizeof(legacy_args);
271 input.pointer = &legacy_args;
273 pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
275 status = wmi_evaluate_method(guid, 1, method_id, &input, NULL);
276 if (ACPI_FAILURE(status))
277 pr_err("alienware-wmi: zone set failure: %u\n", status);
278 return ACPI_FAILURE(status);
281 static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
284 struct platform_zone *target_zone;
285 target_zone = match_zone(attr);
286 if (target_zone == NULL)
287 return sprintf(buf, "red: -1, green: -1, blue: -1\n");
288 return sprintf(buf, "red: %d, green: %d, blue: %d\n",
289 target_zone->colors.red,
290 target_zone->colors.green, target_zone->colors.blue);
294 static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
297 struct platform_zone *target_zone;
299 target_zone = match_zone(attr);
300 if (target_zone == NULL) {
301 pr_err("alienware-wmi: invalid target zone\n");
304 ret = parse_rgb(buf, target_zone);
307 ret = alienware_update_led(target_zone);
308 return ret ? ret : count;
312 * LED Brightness (Global)
314 static int wmax_brightness(int brightness)
317 struct acpi_buffer input;
318 struct wmax_brightness_args args = {
320 .percentage = brightness,
322 input.length = (acpi_size) sizeof(args);
323 input.pointer = &args;
324 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
325 WMAX_METHOD_BRIGHTNESS, &input, NULL);
326 if (ACPI_FAILURE(status))
327 pr_err("alienware-wmi: brightness set failure: %u\n", status);
328 return ACPI_FAILURE(status);
331 static void global_led_set(struct led_classdev *led_cdev,
332 enum led_brightness brightness)
335 global_brightness = brightness;
336 if (interface == WMAX)
337 ret = wmax_brightness(brightness);
339 ret = alienware_update_led(&zone_data[0]);
341 pr_err("LED brightness update failed\n");
344 static enum led_brightness global_led_get(struct led_classdev *led_cdev)
346 return global_brightness;
349 static struct led_classdev global_led = {
350 .brightness_set = global_led_set,
351 .brightness_get = global_led_get,
352 .name = "alienware::global_brightness",
356 * Lighting control state device attribute (Global)
358 static ssize_t show_control_state(struct device *dev,
359 struct device_attribute *attr, char *buf)
361 if (lighting_control_state == LEGACY_BOOTING)
362 return scnprintf(buf, PAGE_SIZE, "[booting] running suspend\n");
363 else if (lighting_control_state == LEGACY_SUSPEND)
364 return scnprintf(buf, PAGE_SIZE, "booting running [suspend]\n");
365 return scnprintf(buf, PAGE_SIZE, "booting [running] suspend\n");
368 static ssize_t store_control_state(struct device *dev,
369 struct device_attribute *attr,
370 const char *buf, size_t count)
372 long unsigned int val;
373 if (strcmp(buf, "booting\n") == 0)
374 val = LEGACY_BOOTING;
375 else if (strcmp(buf, "suspend\n") == 0)
376 val = LEGACY_SUSPEND;
377 else if (interface == LEGACY)
378 val = LEGACY_RUNNING;
381 lighting_control_state = val;
382 pr_debug("alienware-wmi: updated control state to %d\n",
383 lighting_control_state);
387 static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
388 store_control_state);
390 static int alienware_zone_init(struct platform_device *dev)
396 if (interface == WMAX) {
397 lighting_control_state = WMAX_RUNNING;
398 } else if (interface == LEGACY) {
399 lighting_control_state = LEGACY_RUNNING;
401 global_led.max_brightness = 0x0F;
402 global_brightness = global_led.max_brightness;
405 * - zone_dev_attrs num_zones + 1 is for individual zones and then
407 * - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
408 * the lighting control + null terminated
409 * - zone_data num_zones is for the distinct zones
412 kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
418 kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
424 kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
429 for (i = 0; i < quirks->num_zones; i++) {
430 sprintf(buffer, "zone%02X", i);
431 name = kstrdup(buffer, GFP_KERNEL);
434 sysfs_attr_init(&zone_dev_attrs[i].attr);
435 zone_dev_attrs[i].attr.name = name;
436 zone_dev_attrs[i].attr.mode = 0644;
437 zone_dev_attrs[i].show = zone_show;
438 zone_dev_attrs[i].store = zone_set;
439 zone_data[i].location = i;
440 zone_attrs[i] = &zone_dev_attrs[i].attr;
441 zone_data[i].attr = &zone_dev_attrs[i];
443 zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
444 zone_attribute_group.attrs = zone_attrs;
446 led_classdev_register(&dev->dev, &global_led);
448 return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
451 static void alienware_zone_exit(struct platform_device *dev)
453 sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
454 led_classdev_unregister(&global_led);
455 if (zone_dev_attrs) {
457 for (i = 0; i < quirks->num_zones; i++)
458 kfree(zone_dev_attrs[i].attr.name);
460 kfree(zone_dev_attrs);
465 static acpi_status alienware_wmax_command(struct wmax_basic_args *in_args,
466 u32 command, int *out_data)
469 union acpi_object *obj;
470 struct acpi_buffer input;
471 struct acpi_buffer output;
473 input.length = (acpi_size) sizeof(*in_args);
474 input.pointer = in_args;
475 if (out_data != NULL) {
476 output.length = ACPI_ALLOCATE_BUFFER;
477 output.pointer = NULL;
478 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
479 command, &input, &output);
481 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
482 command, &input, NULL);
484 if (ACPI_SUCCESS(status) && out_data != NULL) {
485 obj = (union acpi_object *)output.pointer;
486 if (obj && obj->type == ACPI_TYPE_INTEGER)
487 *out_data = (u32) obj->integer.value;
494 * The HDMI mux sysfs node indicates the status of the HDMI input mux.
495 * It can toggle between standard system GPU output and HDMI input.
497 static ssize_t show_hdmi_cable(struct device *dev,
498 struct device_attribute *attr, char *buf)
502 struct wmax_basic_args in_args = {
506 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_CABLE,
508 if (ACPI_SUCCESS(status)) {
510 return scnprintf(buf, PAGE_SIZE,
511 "[unconnected] connected unknown\n");
512 else if (out_data == 1)
513 return scnprintf(buf, PAGE_SIZE,
514 "unconnected [connected] unknown\n");
516 pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
517 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
520 static ssize_t show_hdmi_source(struct device *dev,
521 struct device_attribute *attr, char *buf)
525 struct wmax_basic_args in_args = {
529 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_STATUS,
532 if (ACPI_SUCCESS(status)) {
534 return scnprintf(buf, PAGE_SIZE,
535 "[input] gpu unknown\n");
536 else if (out_data == 2)
537 return scnprintf(buf, PAGE_SIZE,
538 "input [gpu] unknown\n");
540 pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data);
541 return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
544 static ssize_t toggle_hdmi_source(struct device *dev,
545 struct device_attribute *attr,
546 const char *buf, size_t count)
549 struct wmax_basic_args args;
550 if (strcmp(buf, "gpu\n") == 0)
552 else if (strcmp(buf, "input\n") == 0)
556 pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
558 status = alienware_wmax_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
560 if (ACPI_FAILURE(status))
561 pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
566 static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
567 static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
570 static struct attribute *hdmi_attrs[] = {
571 &dev_attr_cable.attr,
572 &dev_attr_source.attr,
576 static struct attribute_group hdmi_attribute_group = {
581 static void remove_hdmi(struct platform_device *dev)
583 if (quirks->hdmi_mux > 0)
584 sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
587 static int create_hdmi(struct platform_device *dev)
591 ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
593 goto error_create_hdmi;
602 * Alienware GFX amplifier support
603 * - Currently supports reading cable status
604 * - Leaving expansion room to possibly support dock/undock events later
606 static ssize_t show_amplifier_status(struct device *dev,
607 struct device_attribute *attr, char *buf)
611 struct wmax_basic_args in_args = {
615 alienware_wmax_command(&in_args, WMAX_METHOD_AMPLIFIER_CABLE,
617 if (ACPI_SUCCESS(status)) {
619 return scnprintf(buf, PAGE_SIZE,
620 "[unconnected] connected unknown\n");
621 else if (out_data == 1)
622 return scnprintf(buf, PAGE_SIZE,
623 "unconnected [connected] unknown\n");
625 pr_err("alienware-wmi: unknown amplifier cable status: %d\n", status);
626 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
629 static DEVICE_ATTR(status, S_IRUGO, show_amplifier_status, NULL);
631 static struct attribute *amplifier_attrs[] = {
632 &dev_attr_status.attr,
636 static struct attribute_group amplifier_attribute_group = {
638 .attrs = amplifier_attrs,
641 static void remove_amplifier(struct platform_device *dev)
643 if (quirks->amplifier > 0)
644 sysfs_remove_group(&dev->dev.kobj, &lifier_attribute_group);
647 static int create_amplifier(struct platform_device *dev)
651 ret = sysfs_create_group(&dev->dev.kobj, &lifier_attribute_group);
653 remove_amplifier(dev);
658 * Deep Sleep Control support
659 * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
661 static ssize_t show_deepsleep_status(struct device *dev,
662 struct device_attribute *attr, char *buf)
666 struct wmax_basic_args in_args = {
669 status = alienware_wmax_command(&in_args, WMAX_METHOD_DEEP_SLEEP_STATUS,
671 if (ACPI_SUCCESS(status)) {
673 return scnprintf(buf, PAGE_SIZE,
674 "[disabled] s5 s5_s4\n");
675 else if (out_data == 1)
676 return scnprintf(buf, PAGE_SIZE,
677 "disabled [s5] s5_s4\n");
678 else if (out_data == 2)
679 return scnprintf(buf, PAGE_SIZE,
680 "disabled s5 [s5_s4]\n");
682 pr_err("alienware-wmi: unknown deep sleep status: %d\n", status);
683 return scnprintf(buf, PAGE_SIZE, "disabled s5 s5_s4 [unknown]\n");
686 static ssize_t toggle_deepsleep(struct device *dev,
687 struct device_attribute *attr,
688 const char *buf, size_t count)
691 struct wmax_basic_args args;
693 if (strcmp(buf, "disabled\n") == 0)
695 else if (strcmp(buf, "s5\n") == 0)
699 pr_debug("alienware-wmi: setting deep sleep to %d : %s", args.arg, buf);
701 status = alienware_wmax_command(&args, WMAX_METHOD_DEEP_SLEEP_CONTROL,
704 if (ACPI_FAILURE(status))
705 pr_err("alienware-wmi: deep sleep control failed: results: %u\n",
710 static DEVICE_ATTR(deepsleep, S_IRUGO | S_IWUSR, show_deepsleep_status, toggle_deepsleep);
712 static struct attribute *deepsleep_attrs[] = {
713 &dev_attr_deepsleep.attr,
717 static struct attribute_group deepsleep_attribute_group = {
719 .attrs = deepsleep_attrs,
722 static void remove_deepsleep(struct platform_device *dev)
724 if (quirks->deepslp > 0)
725 sysfs_remove_group(&dev->dev.kobj, &deepsleep_attribute_group);
728 static int create_deepsleep(struct platform_device *dev)
732 ret = sysfs_create_group(&dev->dev.kobj, &deepsleep_attribute_group);
734 remove_deepsleep(dev);
738 static int __init alienware_wmi_init(void)
742 if (wmi_has_guid(LEGACY_CONTROL_GUID))
744 else if (wmi_has_guid(WMAX_CONTROL_GUID))
747 pr_warn("alienware-wmi: No known WMI GUID found\n");
751 dmi_check_system(alienware_quirks);
753 quirks = &quirk_unknown;
755 ret = platform_driver_register(&platform_driver);
757 goto fail_platform_driver;
758 platform_device = platform_device_alloc("alienware-wmi", -1);
759 if (!platform_device) {
761 goto fail_platform_device1;
763 ret = platform_device_add(platform_device);
765 goto fail_platform_device2;
767 if (quirks->hdmi_mux > 0) {
768 ret = create_hdmi(platform_device);
773 if (quirks->amplifier > 0) {
774 ret = create_amplifier(platform_device);
776 goto fail_prep_amplifier;
779 if (quirks->deepslp > 0) {
780 ret = create_deepsleep(platform_device);
782 goto fail_prep_deepsleep;
785 ret = alienware_zone_init(platform_device);
787 goto fail_prep_zones;
792 alienware_zone_exit(platform_device);
796 platform_device_del(platform_device);
797 fail_platform_device2:
798 platform_device_put(platform_device);
799 fail_platform_device1:
800 platform_driver_unregister(&platform_driver);
801 fail_platform_driver:
805 module_init(alienware_wmi_init);
807 static void __exit alienware_wmi_exit(void)
809 if (platform_device) {
810 alienware_zone_exit(platform_device);
811 remove_hdmi(platform_device);
812 platform_device_unregister(platform_device);
813 platform_driver_unregister(&platform_driver);
817 module_exit(alienware_wmi_exit);