blob: 9866fec78c1c93138934967f1309014dfae89390 [file] [log] [blame]
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -04001/*
2 * Alienware AlienFX control
3 *
4 * Copyright (C) 2014 Dell Inc <mario_limonciello@dell.com>
5 *
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.
10 *
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.
15 *
16 */
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20#include <linux/acpi.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/dmi.h>
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040024#include <linux/leds.h>
25
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"
29
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
Mario Limonciellobc2ef882014-05-07 15:08:10 -050034#define WMAX_METHOD_HDMI_CABLE 0x5
Mario Limonciellocbbb50d2016-02-02 15:38:54 -060035#define WMAX_METHOD_AMPLIFIER_CABLE 0x6
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060036#define WMAX_METHOD_DEEP_SLEEP_CONTROL 0x0B
37#define WMAX_METHOD_DEEP_SLEEP_STATUS 0x0C
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040038
39MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>");
40MODULE_DESCRIPTION("Alienware special feature control");
41MODULE_LICENSE("GPL");
42MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID);
43MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID);
44
45enum INTERFACE_FLAGS {
46 LEGACY,
47 WMAX,
48};
49
50enum LEGACY_CONTROL_STATES {
51 LEGACY_RUNNING = 1,
52 LEGACY_BOOTING = 0,
53 LEGACY_SUSPEND = 3,
54};
55
56enum WMAX_CONTROL_STATES {
57 WMAX_RUNNING = 0xFF,
58 WMAX_BOOTING = 0,
59 WMAX_SUSPEND = 3,
60};
61
62struct quirk_entry {
63 u8 num_zones;
Mario Limonciellofee4efd2014-07-23 23:19:23 -050064 u8 hdmi_mux;
Mario Limonciellocbbb50d2016-02-02 15:38:54 -060065 u8 amplifier;
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060066 u8 deepslp;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040067};
68
69static struct quirk_entry *quirks;
70
71static struct quirk_entry quirk_unknown = {
72 .num_zones = 2,
Mario Limonciellofee4efd2014-07-23 23:19:23 -050073 .hdmi_mux = 0,
Mario Limonciellocbbb50d2016-02-02 15:38:54 -060074 .amplifier = 0,
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060075 .deepslp = 0,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040076};
77
Mario Limonciello9e503a92016-02-02 15:38:53 -060078static struct quirk_entry quirk_x51_r1_r2 = {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040079 .num_zones = 3,
Mario Limonciellocbbb50d2016-02-02 15:38:54 -060080 .hdmi_mux = 0,
81 .amplifier = 0,
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060082 .deepslp = 0,
Mario Limonciellofee4efd2014-07-23 23:19:23 -050083};
84
Mario Limonciello9e503a92016-02-02 15:38:53 -060085static struct quirk_entry quirk_x51_r3 = {
86 .num_zones = 4,
87 .hdmi_mux = 0,
Mario Limonciellocbbb50d2016-02-02 15:38:54 -060088 .amplifier = 1,
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060089 .deepslp = 0,
Mario Limonciello9e503a92016-02-02 15:38:53 -060090};
91
Mario Limonciellofee4efd2014-07-23 23:19:23 -050092static struct quirk_entry quirk_asm100 = {
93 .num_zones = 2,
94 .hdmi_mux = 1,
Mario Limonciellocbbb50d2016-02-02 15:38:54 -060095 .amplifier = 0,
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060096 .deepslp = 0,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040097};
98
Mario Limonciellob332f822016-02-02 15:38:56 -060099static struct quirk_entry quirk_asm200 = {
100 .num_zones = 2,
101 .hdmi_mux = 1,
102 .amplifier = 0,
103 .deepslp = 1,
104};
105
106static struct quirk_entry quirk_asm201 = {
107 .num_zones = 2,
108 .hdmi_mux = 1,
109 .amplifier = 1,
110 .deepslp = 1,
111};
112
Mathias Kraused997d882014-07-16 19:43:07 +0200113static int __init dmi_matched(const struct dmi_system_id *dmi)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400114{
115 quirks = dmi->driver_data;
116 return 1;
117}
118
Mathias Kraused997d882014-07-16 19:43:07 +0200119static const struct dmi_system_id alienware_quirks[] __initconst = {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400120 {
121 .callback = dmi_matched,
Mario Limonciello9e503a92016-02-02 15:38:53 -0600122 .ident = "Alienware X51 R3",
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400123 .matches = {
124 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
Mario Limonciello9e503a92016-02-02 15:38:53 -0600125 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R3"),
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400126 },
Mario Limonciello9e503a92016-02-02 15:38:53 -0600127 .driver_data = &quirk_x51_r3,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400128 },
129 {
130 .callback = dmi_matched,
131 .ident = "Alienware X51 R2",
132 .matches = {
133 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
134 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
135 },
Mario Limonciello9e503a92016-02-02 15:38:53 -0600136 .driver_data = &quirk_x51_r1_r2,
137 },
138 {
139 .callback = dmi_matched,
140 .ident = "Alienware X51 R1",
141 .matches = {
142 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
143 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
144 },
145 .driver_data = &quirk_x51_r1_r2,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400146 },
Mario Limonciellofee4efd2014-07-23 23:19:23 -0500147 {
Mario Limonciello66ad0dd2016-02-02 15:38:52 -0600148 .callback = dmi_matched,
149 .ident = "Alienware ASM100",
150 .matches = {
151 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
152 DMI_MATCH(DMI_PRODUCT_NAME, "ASM100"),
153 },
154 .driver_data = &quirk_asm100,
155 },
Mario Limonciellob332f822016-02-02 15:38:56 -0600156 {
157 .callback = dmi_matched,
158 .ident = "Alienware ASM200",
159 .matches = {
160 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
161 DMI_MATCH(DMI_PRODUCT_NAME, "ASM200"),
162 },
163 .driver_data = &quirk_asm200,
164 },
165 {
166 .callback = dmi_matched,
167 .ident = "Alienware ASM201",
168 .matches = {
169 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
170 DMI_MATCH(DMI_PRODUCT_NAME, "ASM201"),
171 },
172 .driver_data = &quirk_asm201,
173 },
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400174 {}
175};
176
177struct color_platform {
178 u8 blue;
179 u8 green;
180 u8 red;
181} __packed;
182
183struct platform_zone {
184 u8 location;
185 struct device_attribute *attr;
186 struct color_platform colors;
187};
188
189struct wmax_brightness_args {
190 u32 led_mask;
191 u32 percentage;
192};
193
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600194struct wmax_basic_args {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400195 u8 arg;
196};
197
198struct legacy_led_args {
199 struct color_platform colors;
200 u8 brightness;
201 u8 state;
202} __packed;
203
204struct wmax_led_args {
205 u32 led_mask;
206 struct color_platform colors;
207 u8 state;
208} __packed;
209
210static struct platform_device *platform_device;
211static struct device_attribute *zone_dev_attrs;
212static struct attribute **zone_attrs;
213static struct platform_zone *zone_data;
214
215static struct platform_driver platform_driver = {
216 .driver = {
217 .name = "alienware-wmi",
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400218 }
219};
220
221static struct attribute_group zone_attribute_group = {
222 .name = "rgb_zones",
223};
224
225static u8 interface;
226static u8 lighting_control_state;
227static u8 global_brightness;
228
229/*
230 * Helpers used for zone control
Mario Limoncielloec5eead2016-02-02 15:38:57 -0600231 */
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400232static int parse_rgb(const char *buf, struct platform_zone *zone)
233{
234 long unsigned int rgb;
235 int ret;
236 union color_union {
237 struct color_platform cp;
238 int package;
239 } repackager;
240
241 ret = kstrtoul(buf, 16, &rgb);
242 if (ret)
243 return ret;
244
245 /* RGB triplet notation is 24-bit hexadecimal */
246 if (rgb > 0xFFFFFF)
247 return -EINVAL;
248
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;
253 return 0;
254}
255
256static struct platform_zone *match_zone(struct device_attribute *attr)
257{
258 int i;
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];
264 }
265 }
266 return NULL;
267}
268
269/*
270 * Individual RGB zone control
Mario Limoncielloec5eead2016-02-02 15:38:57 -0600271 */
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400272static int alienware_update_led(struct platform_zone *zone)
273{
274 int method_id;
275 acpi_status status;
276 char *guid;
277 struct acpi_buffer input;
278 struct legacy_led_args legacy_args;
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600279 struct wmax_led_args wmax_basic_args;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400280 if (interface == WMAX) {
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600281 wmax_basic_args.led_mask = 1 << zone->location;
282 wmax_basic_args.colors = zone->colors;
283 wmax_basic_args.state = lighting_control_state;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400284 guid = WMAX_CONTROL_GUID;
285 method_id = WMAX_METHOD_ZONE_CONTROL;
286
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600287 input.length = (acpi_size) sizeof(wmax_basic_args);
288 input.pointer = &wmax_basic_args;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400289 } else {
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;
297 } else
298 guid = LEGACY_CONTROL_GUID;
299 method_id = zone->location + 1;
300
301 input.length = (acpi_size) sizeof(legacy_args);
302 input.pointer = &legacy_args;
303 }
304 pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
305
Mario Limoncielloc0e4aa72017-06-21 17:01:35 -0500306 status = wmi_evaluate_method(guid, 0, method_id, &input, NULL);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400307 if (ACPI_FAILURE(status))
308 pr_err("alienware-wmi: zone set failure: %u\n", status);
309 return ACPI_FAILURE(status);
310}
311
312static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
313 char *buf)
314{
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);
322
323}
324
325static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
326 const char *buf, size_t count)
327{
328 struct platform_zone *target_zone;
329 int ret;
330 target_zone = match_zone(attr);
331 if (target_zone == NULL) {
332 pr_err("alienware-wmi: invalid target zone\n");
333 return 1;
334 }
335 ret = parse_rgb(buf, target_zone);
336 if (ret)
337 return ret;
338 ret = alienware_update_led(target_zone);
339 return ret ? ret : count;
340}
341
342/*
343 * LED Brightness (Global)
Mario Limoncielloec5eead2016-02-02 15:38:57 -0600344 */
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400345static int wmax_brightness(int brightness)
346{
347 acpi_status status;
348 struct acpi_buffer input;
349 struct wmax_brightness_args args = {
350 .led_mask = 0xFF,
351 .percentage = brightness,
352 };
353 input.length = (acpi_size) sizeof(args);
354 input.pointer = &args;
Mario Limoncielloc0e4aa72017-06-21 17:01:35 -0500355 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 0,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400356 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);
360}
361
362static void global_led_set(struct led_classdev *led_cdev,
363 enum led_brightness brightness)
364{
365 int ret;
366 global_brightness = brightness;
367 if (interface == WMAX)
368 ret = wmax_brightness(brightness);
369 else
370 ret = alienware_update_led(&zone_data[0]);
371 if (ret)
372 pr_err("LED brightness update failed\n");
373}
374
375static enum led_brightness global_led_get(struct led_classdev *led_cdev)
376{
377 return global_brightness;
378}
379
380static struct led_classdev global_led = {
381 .brightness_set = global_led_set,
382 .brightness_get = global_led_get,
383 .name = "alienware::global_brightness",
384};
385
386/*
387 * Lighting control state device attribute (Global)
Mario Limoncielloec5eead2016-02-02 15:38:57 -0600388 */
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400389static ssize_t show_control_state(struct device *dev,
390 struct device_attribute *attr, char *buf)
391{
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");
397}
398
399static ssize_t store_control_state(struct device *dev,
400 struct device_attribute *attr,
401 const char *buf, size_t count)
402{
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;
410 else
411 val = WMAX_RUNNING;
412 lighting_control_state = val;
413 pr_debug("alienware-wmi: updated control state to %d\n",
414 lighting_control_state);
415 return count;
416}
417
418static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
419 store_control_state);
420
421static int alienware_zone_init(struct platform_device *dev)
422{
423 int i;
424 char buffer[10];
425 char *name;
426
427 if (interface == WMAX) {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400428 lighting_control_state = WMAX_RUNNING;
429 } else if (interface == LEGACY) {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400430 lighting_control_state = LEGACY_RUNNING;
431 }
Mario Limonciellob9986802014-05-07 15:08:09 -0500432 global_led.max_brightness = 0x0F;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400433 global_brightness = global_led.max_brightness;
434
435 /*
436 * - zone_dev_attrs num_zones + 1 is for individual zones and then
437 * null terminated
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
441 */
442 zone_dev_attrs =
443 kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
444 GFP_KERNEL);
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500445 if (!zone_dev_attrs)
446 return -ENOMEM;
447
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400448 zone_attrs =
449 kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
450 GFP_KERNEL);
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500451 if (!zone_attrs)
452 return -ENOMEM;
453
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400454 zone_data =
455 kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
456 GFP_KERNEL);
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500457 if (!zone_data)
458 return -ENOMEM;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400459
460 for (i = 0; i < quirks->num_zones; i++) {
461 sprintf(buffer, "zone%02X", i);
462 name = kstrdup(buffer, GFP_KERNEL);
463 if (name == NULL)
464 return 1;
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];
473 }
474 zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
475 zone_attribute_group.attrs = zone_attrs;
476
477 led_classdev_register(&dev->dev, &global_led);
478
479 return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
480}
481
482static void alienware_zone_exit(struct platform_device *dev)
483{
484 sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
485 led_classdev_unregister(&global_led);
486 if (zone_dev_attrs) {
487 int i;
488 for (i = 0; i < quirks->num_zones; i++)
489 kfree(zone_dev_attrs[i].attr.name);
490 }
491 kfree(zone_dev_attrs);
492 kfree(zone_data);
493 kfree(zone_attrs);
494}
495
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600496static acpi_status alienware_wmax_command(struct wmax_basic_args *in_args,
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500497 u32 command, int *out_data)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400498{
499 acpi_status status;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400500 union acpi_object *obj;
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500501 struct acpi_buffer input;
502 struct acpi_buffer output;
503
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;
Mario Limoncielloc0e4aa72017-06-21 17:01:35 -0500509 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 0,
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500510 command, &input, &output);
511 } else
Mario Limoncielloc0e4aa72017-06-21 17:01:35 -0500512 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 0,
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500513 command, &input, NULL);
514
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;
519 }
520 return status;
521
522}
523
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600524/*
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.
527 */
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500528static ssize_t show_hdmi_cable(struct device *dev,
529 struct device_attribute *attr, char *buf)
530{
531 acpi_status status;
532 u32 out_data;
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600533 struct wmax_basic_args in_args = {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400534 .arg = 0,
535 };
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500536 status =
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600537 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_CABLE,
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500538 (u32 *) &out_data);
539 if (ACPI_SUCCESS(status)) {
540 if (out_data == 0)
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");
546 }
547 pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
548 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
549}
550
551static ssize_t show_hdmi_source(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
554 acpi_status status;
555 u32 out_data;
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600556 struct wmax_basic_args in_args = {
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500557 .arg = 0,
558 };
559 status =
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600560 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_STATUS,
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500561 (u32 *) &out_data);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400562
563 if (ACPI_SUCCESS(status)) {
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500564 if (out_data == 1)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400565 return scnprintf(buf, PAGE_SIZE,
566 "[input] gpu unknown\n");
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500567 else if (out_data == 2)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400568 return scnprintf(buf, PAGE_SIZE,
569 "input [gpu] unknown\n");
570 }
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500571 pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400572 return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
573}
574
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500575static ssize_t toggle_hdmi_source(struct device *dev,
576 struct device_attribute *attr,
577 const char *buf, size_t count)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400578{
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400579 acpi_status status;
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600580 struct wmax_basic_args args;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400581 if (strcmp(buf, "gpu\n") == 0)
582 args.arg = 1;
583 else if (strcmp(buf, "input\n") == 0)
584 args.arg = 2;
585 else
586 args.arg = 3;
587 pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500588
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600589 status = alienware_wmax_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500590
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400591 if (ACPI_FAILURE(status))
592 pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
593 status);
594 return count;
595}
596
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500597static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
598static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
599 toggle_hdmi_source);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400600
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500601static struct attribute *hdmi_attrs[] = {
602 &dev_attr_cable.attr,
603 &dev_attr_source.attr,
604 NULL,
605};
606
607static struct attribute_group hdmi_attribute_group = {
608 .name = "hdmi",
609 .attrs = hdmi_attrs,
610};
611
612static void remove_hdmi(struct platform_device *dev)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400613{
Mario Limonciellofee4efd2014-07-23 23:19:23 -0500614 if (quirks->hdmi_mux > 0)
615 sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400616}
617
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500618static int create_hdmi(struct platform_device *dev)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400619{
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500620 int ret;
621
622 ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400623 if (ret)
Mario Limoncielloec5eead2016-02-02 15:38:57 -0600624 remove_hdmi(dev);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400625 return ret;
626}
627
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600628/*
629 * Alienware GFX amplifier support
630 * - Currently supports reading cable status
631 * - Leaving expansion room to possibly support dock/undock events later
632 */
633static ssize_t show_amplifier_status(struct device *dev,
634 struct device_attribute *attr, char *buf)
635{
636 acpi_status status;
637 u32 out_data;
638 struct wmax_basic_args in_args = {
639 .arg = 0,
640 };
641 status =
642 alienware_wmax_command(&in_args, WMAX_METHOD_AMPLIFIER_CABLE,
643 (u32 *) &out_data);
644 if (ACPI_SUCCESS(status)) {
645 if (out_data == 0)
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");
651 }
652 pr_err("alienware-wmi: unknown amplifier cable status: %d\n", status);
653 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
654}
655
656static DEVICE_ATTR(status, S_IRUGO, show_amplifier_status, NULL);
657
658static struct attribute *amplifier_attrs[] = {
659 &dev_attr_status.attr,
660 NULL,
661};
662
663static struct attribute_group amplifier_attribute_group = {
664 .name = "amplifier",
665 .attrs = amplifier_attrs,
666};
667
668static void remove_amplifier(struct platform_device *dev)
669{
670 if (quirks->amplifier > 0)
671 sysfs_remove_group(&dev->dev.kobj, &amplifier_attribute_group);
672}
673
674static int create_amplifier(struct platform_device *dev)
675{
676 int ret;
677
678 ret = sysfs_create_group(&dev->dev.kobj, &amplifier_attribute_group);
679 if (ret)
680 remove_amplifier(dev);
681 return ret;
682}
683
Mario Limonciello8ea81ec2016-02-02 15:38:55 -0600684/*
685 * Deep Sleep Control support
686 * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
687 */
688static ssize_t show_deepsleep_status(struct device *dev,
689 struct device_attribute *attr, char *buf)
690{
691 acpi_status status;
692 u32 out_data;
693 struct wmax_basic_args in_args = {
694 .arg = 0,
695 };
696 status = alienware_wmax_command(&in_args, WMAX_METHOD_DEEP_SLEEP_STATUS,
697 (u32 *) &out_data);
698 if (ACPI_SUCCESS(status)) {
699 if (out_data == 0)
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");
708 }
709 pr_err("alienware-wmi: unknown deep sleep status: %d\n", status);
710 return scnprintf(buf, PAGE_SIZE, "disabled s5 s5_s4 [unknown]\n");
711}
712
713static ssize_t toggle_deepsleep(struct device *dev,
714 struct device_attribute *attr,
715 const char *buf, size_t count)
716{
717 acpi_status status;
718 struct wmax_basic_args args;
719
720 if (strcmp(buf, "disabled\n") == 0)
721 args.arg = 0;
722 else if (strcmp(buf, "s5\n") == 0)
723 args.arg = 1;
724 else
725 args.arg = 2;
726 pr_debug("alienware-wmi: setting deep sleep to %d : %s", args.arg, buf);
727
728 status = alienware_wmax_command(&args, WMAX_METHOD_DEEP_SLEEP_CONTROL,
729 NULL);
730
731 if (ACPI_FAILURE(status))
732 pr_err("alienware-wmi: deep sleep control failed: results: %u\n",
733 status);
734 return count;
735}
736
737static DEVICE_ATTR(deepsleep, S_IRUGO | S_IWUSR, show_deepsleep_status, toggle_deepsleep);
738
739static struct attribute *deepsleep_attrs[] = {
740 &dev_attr_deepsleep.attr,
741 NULL,
742};
743
744static struct attribute_group deepsleep_attribute_group = {
745 .name = "deepsleep",
746 .attrs = deepsleep_attrs,
747};
748
749static void remove_deepsleep(struct platform_device *dev)
750{
751 if (quirks->deepslp > 0)
752 sysfs_remove_group(&dev->dev.kobj, &deepsleep_attribute_group);
753}
754
755static int create_deepsleep(struct platform_device *dev)
756{
757 int ret;
758
759 ret = sysfs_create_group(&dev->dev.kobj, &deepsleep_attribute_group);
760 if (ret)
761 remove_deepsleep(dev);
762 return ret;
763}
764
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400765static int __init alienware_wmi_init(void)
766{
767 int ret;
768
769 if (wmi_has_guid(LEGACY_CONTROL_GUID))
770 interface = LEGACY;
771 else if (wmi_has_guid(WMAX_CONTROL_GUID))
772 interface = WMAX;
773 else {
774 pr_warn("alienware-wmi: No known WMI GUID found\n");
775 return -ENODEV;
776 }
777
778 dmi_check_system(alienware_quirks);
779 if (quirks == NULL)
780 quirks = &quirk_unknown;
781
782 ret = platform_driver_register(&platform_driver);
783 if (ret)
784 goto fail_platform_driver;
785 platform_device = platform_device_alloc("alienware-wmi", -1);
786 if (!platform_device) {
787 ret = -ENOMEM;
788 goto fail_platform_device1;
789 }
790 ret = platform_device_add(platform_device);
791 if (ret)
792 goto fail_platform_device2;
793
Mario Limonciellofee4efd2014-07-23 23:19:23 -0500794 if (quirks->hdmi_mux > 0) {
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500795 ret = create_hdmi(platform_device);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400796 if (ret)
797 goto fail_prep_hdmi;
798 }
799
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600800 if (quirks->amplifier > 0) {
801 ret = create_amplifier(platform_device);
802 if (ret)
803 goto fail_prep_amplifier;
804 }
805
Mario Limonciello8ea81ec2016-02-02 15:38:55 -0600806 if (quirks->deepslp > 0) {
807 ret = create_deepsleep(platform_device);
808 if (ret)
809 goto fail_prep_deepsleep;
810 }
811
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400812 ret = alienware_zone_init(platform_device);
813 if (ret)
814 goto fail_prep_zones;
815
816 return 0;
817
818fail_prep_zones:
819 alienware_zone_exit(platform_device);
Mario Limonciello8ea81ec2016-02-02 15:38:55 -0600820fail_prep_deepsleep:
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600821fail_prep_amplifier:
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400822fail_prep_hdmi:
823 platform_device_del(platform_device);
824fail_platform_device2:
825 platform_device_put(platform_device);
826fail_platform_device1:
827 platform_driver_unregister(&platform_driver);
828fail_platform_driver:
829 return ret;
830}
831
832module_init(alienware_wmi_init);
833
834static void __exit alienware_wmi_exit(void)
835{
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400836 if (platform_device) {
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500837 alienware_zone_exit(platform_device);
838 remove_hdmi(platform_device);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400839 platform_device_unregister(platform_device);
840 platform_driver_unregister(&platform_driver);
841 }
842}
843
844module_exit(alienware_wmi_exit);