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
37 MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>");
38 MODULE_DESCRIPTION("Alienware special feature control");
39 MODULE_LICENSE("GPL");
40 MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID
);
41 MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID
);
43 enum INTERFACE_FLAGS
{
48 enum LEGACY_CONTROL_STATES
{
54 enum WMAX_CONTROL_STATES
{
65 static struct quirk_entry
*quirks
;
67 static struct quirk_entry quirk_unknown
= {
72 static struct quirk_entry quirk_x51_family
= {
77 static struct quirk_entry quirk_asm100
= {
82 static int __init
dmi_matched(const struct dmi_system_id
*dmi
)
84 quirks
= dmi
->driver_data
;
88 static const struct dmi_system_id alienware_quirks
[] __initconst
= {
90 .callback
= dmi_matched
,
91 .ident
= "Alienware X51 R1",
93 DMI_MATCH(DMI_SYS_VENDOR
, "Alienware"),
94 DMI_MATCH(DMI_PRODUCT_NAME
, "Alienware X51"),
96 .driver_data
= &quirk_x51_family
,
99 .callback
= dmi_matched
,
100 .ident
= "Alienware X51 R2",
102 DMI_MATCH(DMI_SYS_VENDOR
, "Alienware"),
103 DMI_MATCH(DMI_PRODUCT_NAME
, "Alienware X51 R2"),
105 .driver_data
= &quirk_x51_family
,
108 .callback
= dmi_matched
,
109 .ident
= "Alienware ASM100",
111 DMI_MATCH(DMI_SYS_VENDOR
, "Alienware"),
112 DMI_MATCH(DMI_PRODUCT_NAME
, "ASM100"),
114 .driver_data
= &quirk_asm100
,
119 struct color_platform
{
125 struct platform_zone
{
127 struct device_attribute
*attr
;
128 struct color_platform colors
;
131 struct wmax_brightness_args
{
140 struct legacy_led_args
{
141 struct color_platform colors
;
146 struct wmax_led_args
{
148 struct color_platform colors
;
152 static struct platform_device
*platform_device
;
153 static struct device_attribute
*zone_dev_attrs
;
154 static struct attribute
**zone_attrs
;
155 static struct platform_zone
*zone_data
;
157 static struct platform_driver platform_driver
= {
159 .name
= "alienware-wmi",
163 static struct attribute_group zone_attribute_group
= {
168 static u8 lighting_control_state
;
169 static u8 global_brightness
;
172 * Helpers used for zone control
174 static int parse_rgb(const char *buf
, struct platform_zone
*zone
)
176 long unsigned int rgb
;
179 struct color_platform cp
;
183 ret
= kstrtoul(buf
, 16, &rgb
);
187 /* RGB triplet notation is 24-bit hexadecimal */
191 repackager
.package
= rgb
& 0x0f0f0f0f;
192 pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
193 repackager
.cp
.red
, repackager
.cp
.green
, repackager
.cp
.blue
);
194 zone
->colors
= repackager
.cp
;
198 static struct platform_zone
*match_zone(struct device_attribute
*attr
)
201 for (i
= 0; i
< quirks
->num_zones
; i
++) {
202 if ((struct device_attribute
*)zone_data
[i
].attr
== attr
) {
203 pr_debug("alienware-wmi: matched zone location: %d\n",
204 zone_data
[i
].location
);
205 return &zone_data
[i
];
212 * Individual RGB zone control
214 static int alienware_update_led(struct platform_zone
*zone
)
219 struct acpi_buffer input
;
220 struct legacy_led_args legacy_args
;
221 struct wmax_led_args wmax_args
;
222 if (interface
== WMAX
) {
223 wmax_args
.led_mask
= 1 << zone
->location
;
224 wmax_args
.colors
= zone
->colors
;
225 wmax_args
.state
= lighting_control_state
;
226 guid
= WMAX_CONTROL_GUID
;
227 method_id
= WMAX_METHOD_ZONE_CONTROL
;
229 input
.length
= (acpi_size
) sizeof(wmax_args
);
230 input
.pointer
= &wmax_args
;
232 legacy_args
.colors
= zone
->colors
;
233 legacy_args
.brightness
= global_brightness
;
234 legacy_args
.state
= 0;
235 if (lighting_control_state
== LEGACY_BOOTING
||
236 lighting_control_state
== LEGACY_SUSPEND
) {
237 guid
= LEGACY_POWER_CONTROL_GUID
;
238 legacy_args
.state
= lighting_control_state
;
240 guid
= LEGACY_CONTROL_GUID
;
241 method_id
= zone
->location
+ 1;
243 input
.length
= (acpi_size
) sizeof(legacy_args
);
244 input
.pointer
= &legacy_args
;
246 pr_debug("alienware-wmi: guid %s method %d\n", guid
, method_id
);
248 status
= wmi_evaluate_method(guid
, 1, method_id
, &input
, NULL
);
249 if (ACPI_FAILURE(status
))
250 pr_err("alienware-wmi: zone set failure: %u\n", status
);
251 return ACPI_FAILURE(status
);
254 static ssize_t
zone_show(struct device
*dev
, struct device_attribute
*attr
,
257 struct platform_zone
*target_zone
;
258 target_zone
= match_zone(attr
);
259 if (target_zone
== NULL
)
260 return sprintf(buf
, "red: -1, green: -1, blue: -1\n");
261 return sprintf(buf
, "red: %d, green: %d, blue: %d\n",
262 target_zone
->colors
.red
,
263 target_zone
->colors
.green
, target_zone
->colors
.blue
);
267 static ssize_t
zone_set(struct device
*dev
, struct device_attribute
*attr
,
268 const char *buf
, size_t count
)
270 struct platform_zone
*target_zone
;
272 target_zone
= match_zone(attr
);
273 if (target_zone
== NULL
) {
274 pr_err("alienware-wmi: invalid target zone\n");
277 ret
= parse_rgb(buf
, target_zone
);
280 ret
= alienware_update_led(target_zone
);
281 return ret
? ret
: count
;
285 * LED Brightness (Global)
287 static int wmax_brightness(int brightness
)
290 struct acpi_buffer input
;
291 struct wmax_brightness_args args
= {
293 .percentage
= brightness
,
295 input
.length
= (acpi_size
) sizeof(args
);
296 input
.pointer
= &args
;
297 status
= wmi_evaluate_method(WMAX_CONTROL_GUID
, 1,
298 WMAX_METHOD_BRIGHTNESS
, &input
, NULL
);
299 if (ACPI_FAILURE(status
))
300 pr_err("alienware-wmi: brightness set failure: %u\n", status
);
301 return ACPI_FAILURE(status
);
304 static void global_led_set(struct led_classdev
*led_cdev
,
305 enum led_brightness brightness
)
308 global_brightness
= brightness
;
309 if (interface
== WMAX
)
310 ret
= wmax_brightness(brightness
);
312 ret
= alienware_update_led(&zone_data
[0]);
314 pr_err("LED brightness update failed\n");
317 static enum led_brightness
global_led_get(struct led_classdev
*led_cdev
)
319 return global_brightness
;
322 static struct led_classdev global_led
= {
323 .brightness_set
= global_led_set
,
324 .brightness_get
= global_led_get
,
325 .name
= "alienware::global_brightness",
329 * Lighting control state device attribute (Global)
331 static ssize_t
show_control_state(struct device
*dev
,
332 struct device_attribute
*attr
, char *buf
)
334 if (lighting_control_state
== LEGACY_BOOTING
)
335 return scnprintf(buf
, PAGE_SIZE
, "[booting] running suspend\n");
336 else if (lighting_control_state
== LEGACY_SUSPEND
)
337 return scnprintf(buf
, PAGE_SIZE
, "booting running [suspend]\n");
338 return scnprintf(buf
, PAGE_SIZE
, "booting [running] suspend\n");
341 static ssize_t
store_control_state(struct device
*dev
,
342 struct device_attribute
*attr
,
343 const char *buf
, size_t count
)
345 long unsigned int val
;
346 if (strcmp(buf
, "booting\n") == 0)
347 val
= LEGACY_BOOTING
;
348 else if (strcmp(buf
, "suspend\n") == 0)
349 val
= LEGACY_SUSPEND
;
350 else if (interface
== LEGACY
)
351 val
= LEGACY_RUNNING
;
354 lighting_control_state
= val
;
355 pr_debug("alienware-wmi: updated control state to %d\n",
356 lighting_control_state
);
360 static DEVICE_ATTR(lighting_control_state
, 0644, show_control_state
,
361 store_control_state
);
363 static int alienware_zone_init(struct platform_device
*dev
)
369 if (interface
== WMAX
) {
370 lighting_control_state
= WMAX_RUNNING
;
371 } else if (interface
== LEGACY
) {
372 lighting_control_state
= LEGACY_RUNNING
;
374 global_led
.max_brightness
= 0x0F;
375 global_brightness
= global_led
.max_brightness
;
378 * - zone_dev_attrs num_zones + 1 is for individual zones and then
380 * - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
381 * the lighting control + null terminated
382 * - zone_data num_zones is for the distinct zones
385 kzalloc(sizeof(struct device_attribute
) * (quirks
->num_zones
+ 1),
391 kzalloc(sizeof(struct attribute
*) * (quirks
->num_zones
+ 2),
397 kzalloc(sizeof(struct platform_zone
) * (quirks
->num_zones
),
402 for (i
= 0; i
< quirks
->num_zones
; i
++) {
403 sprintf(buffer
, "zone%02X", i
);
404 name
= kstrdup(buffer
, GFP_KERNEL
);
407 sysfs_attr_init(&zone_dev_attrs
[i
].attr
);
408 zone_dev_attrs
[i
].attr
.name
= name
;
409 zone_dev_attrs
[i
].attr
.mode
= 0644;
410 zone_dev_attrs
[i
].show
= zone_show
;
411 zone_dev_attrs
[i
].store
= zone_set
;
412 zone_data
[i
].location
= i
;
413 zone_attrs
[i
] = &zone_dev_attrs
[i
].attr
;
414 zone_data
[i
].attr
= &zone_dev_attrs
[i
];
416 zone_attrs
[quirks
->num_zones
] = &dev_attr_lighting_control_state
.attr
;
417 zone_attribute_group
.attrs
= zone_attrs
;
419 led_classdev_register(&dev
->dev
, &global_led
);
421 return sysfs_create_group(&dev
->dev
.kobj
, &zone_attribute_group
);
424 static void alienware_zone_exit(struct platform_device
*dev
)
426 sysfs_remove_group(&dev
->dev
.kobj
, &zone_attribute_group
);
427 led_classdev_unregister(&global_led
);
428 if (zone_dev_attrs
) {
430 for (i
= 0; i
< quirks
->num_zones
; i
++)
431 kfree(zone_dev_attrs
[i
].attr
.name
);
433 kfree(zone_dev_attrs
);
439 The HDMI mux sysfs node indicates the status of the HDMI input mux.
440 It can toggle between standard system GPU output and HDMI input.
442 static acpi_status
alienware_hdmi_command(struct hdmi_args
*in_args
,
443 u32 command
, int *out_data
)
446 union acpi_object
*obj
;
447 struct acpi_buffer input
;
448 struct acpi_buffer output
;
450 input
.length
= (acpi_size
) sizeof(*in_args
);
451 input
.pointer
= in_args
;
452 if (out_data
!= NULL
) {
453 output
.length
= ACPI_ALLOCATE_BUFFER
;
454 output
.pointer
= NULL
;
455 status
= wmi_evaluate_method(WMAX_CONTROL_GUID
, 1,
456 command
, &input
, &output
);
458 status
= wmi_evaluate_method(WMAX_CONTROL_GUID
, 1,
459 command
, &input
, NULL
);
461 if (ACPI_SUCCESS(status
) && out_data
!= NULL
) {
462 obj
= (union acpi_object
*)output
.pointer
;
463 if (obj
&& obj
->type
== ACPI_TYPE_INTEGER
)
464 *out_data
= (u32
) obj
->integer
.value
;
470 static ssize_t
show_hdmi_cable(struct device
*dev
,
471 struct device_attribute
*attr
, char *buf
)
475 struct hdmi_args in_args
= {
479 alienware_hdmi_command(&in_args
, WMAX_METHOD_HDMI_CABLE
,
481 if (ACPI_SUCCESS(status
)) {
483 return scnprintf(buf
, PAGE_SIZE
,
484 "[unconnected] connected unknown\n");
485 else if (out_data
== 1)
486 return scnprintf(buf
, PAGE_SIZE
,
487 "unconnected [connected] unknown\n");
489 pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status
);
490 return scnprintf(buf
, PAGE_SIZE
, "unconnected connected [unknown]\n");
493 static ssize_t
show_hdmi_source(struct device
*dev
,
494 struct device_attribute
*attr
, char *buf
)
498 struct hdmi_args in_args
= {
502 alienware_hdmi_command(&in_args
, WMAX_METHOD_HDMI_STATUS
,
505 if (ACPI_SUCCESS(status
)) {
507 return scnprintf(buf
, PAGE_SIZE
,
508 "[input] gpu unknown\n");
509 else if (out_data
== 2)
510 return scnprintf(buf
, PAGE_SIZE
,
511 "input [gpu] unknown\n");
513 pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data
);
514 return scnprintf(buf
, PAGE_SIZE
, "input gpu [unknown]\n");
517 static ssize_t
toggle_hdmi_source(struct device
*dev
,
518 struct device_attribute
*attr
,
519 const char *buf
, size_t count
)
522 struct hdmi_args args
;
523 if (strcmp(buf
, "gpu\n") == 0)
525 else if (strcmp(buf
, "input\n") == 0)
529 pr_debug("alienware-wmi: setting hdmi to %d : %s", args
.arg
, buf
);
531 status
= alienware_hdmi_command(&args
, WMAX_METHOD_HDMI_SOURCE
, NULL
);
533 if (ACPI_FAILURE(status
))
534 pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
539 static DEVICE_ATTR(cable
, S_IRUGO
, show_hdmi_cable
, NULL
);
540 static DEVICE_ATTR(source
, S_IRUGO
| S_IWUSR
, show_hdmi_source
,
543 static struct attribute
*hdmi_attrs
[] = {
544 &dev_attr_cable
.attr
,
545 &dev_attr_source
.attr
,
549 static struct attribute_group hdmi_attribute_group
= {
554 static void remove_hdmi(struct platform_device
*dev
)
556 if (quirks
->hdmi_mux
> 0)
557 sysfs_remove_group(&dev
->dev
.kobj
, &hdmi_attribute_group
);
560 static int create_hdmi(struct platform_device
*dev
)
564 ret
= sysfs_create_group(&dev
->dev
.kobj
, &hdmi_attribute_group
);
566 goto error_create_hdmi
;
574 static int __init
alienware_wmi_init(void)
578 if (wmi_has_guid(LEGACY_CONTROL_GUID
))
580 else if (wmi_has_guid(WMAX_CONTROL_GUID
))
583 pr_warn("alienware-wmi: No known WMI GUID found\n");
587 dmi_check_system(alienware_quirks
);
589 quirks
= &quirk_unknown
;
591 ret
= platform_driver_register(&platform_driver
);
593 goto fail_platform_driver
;
594 platform_device
= platform_device_alloc("alienware-wmi", -1);
595 if (!platform_device
) {
597 goto fail_platform_device1
;
599 ret
= platform_device_add(platform_device
);
601 goto fail_platform_device2
;
603 if (quirks
->hdmi_mux
> 0) {
604 ret
= create_hdmi(platform_device
);
609 ret
= alienware_zone_init(platform_device
);
611 goto fail_prep_zones
;
616 alienware_zone_exit(platform_device
);
618 platform_device_del(platform_device
);
619 fail_platform_device2
:
620 platform_device_put(platform_device
);
621 fail_platform_device1
:
622 platform_driver_unregister(&platform_driver
);
623 fail_platform_driver
:
627 module_init(alienware_wmi_init
);
629 static void __exit
alienware_wmi_exit(void)
631 if (platform_device
) {
632 alienware_zone_exit(platform_device
);
633 remove_hdmi(platform_device
);
634 platform_device_unregister(platform_device
);
635 platform_driver_unregister(&platform_driver
);
639 module_exit(alienware_wmi_exit
);