Merge 4.14.73 into android-4.14-p
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / drivers / platform / x86 / alienware-wmi.c
CommitLineData
a46ad0f1
ML
1/*
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>
a46ad0f1
ML
24#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
bc2ef884 34#define WMAX_METHOD_HDMI_CABLE 0x5
cbbb50d6 35#define WMAX_METHOD_AMPLIFIER_CABLE 0x6
8ea81ec6
ML
36#define WMAX_METHOD_DEEP_SLEEP_CONTROL 0x0B
37#define WMAX_METHOD_DEEP_SLEEP_STATUS 0x0C
a46ad0f1
ML
38
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;
fee4efd7 64 u8 hdmi_mux;
cbbb50d6 65 u8 amplifier;
8ea81ec6 66 u8 deepslp;
a46ad0f1
ML
67};
68
69static struct quirk_entry *quirks;
70
71static struct quirk_entry quirk_unknown = {
72 .num_zones = 2,
fee4efd7 73 .hdmi_mux = 0,
cbbb50d6 74 .amplifier = 0,
8ea81ec6 75 .deepslp = 0,
a46ad0f1
ML
76};
77
9e503a9d 78static struct quirk_entry quirk_x51_r1_r2 = {
a46ad0f1 79 .num_zones = 3,
cbbb50d6
ML
80 .hdmi_mux = 0,
81 .amplifier = 0,
8ea81ec6 82 .deepslp = 0,
fee4efd7
ML
83};
84
9e503a9d
ML
85static struct quirk_entry quirk_x51_r3 = {
86 .num_zones = 4,
87 .hdmi_mux = 0,
cbbb50d6 88 .amplifier = 1,
8ea81ec6 89 .deepslp = 0,
9e503a9d
ML
90};
91
fee4efd7
ML
92static struct quirk_entry quirk_asm100 = {
93 .num_zones = 2,
94 .hdmi_mux = 1,
cbbb50d6 95 .amplifier = 0,
8ea81ec6 96 .deepslp = 0,
a46ad0f1
ML
97};
98
b332f82e
ML
99static 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
d997d88e 113static int __init dmi_matched(const struct dmi_system_id *dmi)
a46ad0f1
ML
114{
115 quirks = dmi->driver_data;
116 return 1;
117}
118
d997d88e 119static const struct dmi_system_id alienware_quirks[] __initconst = {
a46ad0f1
ML
120 {
121 .callback = dmi_matched,
9e503a9d 122 .ident = "Alienware X51 R3",
a46ad0f1
ML
123 .matches = {
124 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
9e503a9d 125 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R3"),
a46ad0f1 126 },
9e503a9d 127 .driver_data = &quirk_x51_r3,
a46ad0f1
ML
128 },
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 },
9e503a9d
ML
136 .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,
a46ad0f1 146 },
fee4efd7 147 {
66ad0dd3
ML
148 .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 },
b332f82e
ML
156 {
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 },
a46ad0f1
ML
174 {}
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
cbbb50d6 194struct wmax_basic_args {
a46ad0f1
ML
195 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",
a46ad0f1
ML
218 }
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
ec5eeadc 231 */
a46ad0f1
ML
232static 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{
22ff1a36
AB
258 u8 zone;
259
260 for (zone = 0; zone < quirks->num_zones; zone++) {
261 if ((struct device_attribute *)zone_data[zone].attr == attr) {
a46ad0f1 262 pr_debug("alienware-wmi: matched zone location: %d\n",
22ff1a36
AB
263 zone_data[zone].location);
264 return &zone_data[zone];
a46ad0f1
ML
265 }
266 }
267 return NULL;
268}
269
270/*
271 * Individual RGB zone control
ec5eeadc 272 */
a46ad0f1
ML
273static int alienware_update_led(struct platform_zone *zone)
274{
275 int method_id;
276 acpi_status status;
277 char *guid;
278 struct acpi_buffer input;
279 struct legacy_led_args legacy_args;
cbbb50d6 280 struct wmax_led_args wmax_basic_args;
a46ad0f1 281 if (interface == WMAX) {
cbbb50d6
ML
282 wmax_basic_args.led_mask = 1 << zone->location;
283 wmax_basic_args.colors = zone->colors;
284 wmax_basic_args.state = lighting_control_state;
a46ad0f1
ML
285 guid = WMAX_CONTROL_GUID;
286 method_id = WMAX_METHOD_ZONE_CONTROL;
287
cbbb50d6
ML
288 input.length = (acpi_size) sizeof(wmax_basic_args);
289 input.pointer = &wmax_basic_args;
a46ad0f1
ML
290 } else {
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;
298 } else
299 guid = LEGACY_CONTROL_GUID;
300 method_id = zone->location + 1;
301
302 input.length = (acpi_size) sizeof(legacy_args);
303 input.pointer = &legacy_args;
304 }
305 pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
306
c0e4aa78 307 status = wmi_evaluate_method(guid, 0, method_id, &input, NULL);
a46ad0f1
ML
308 if (ACPI_FAILURE(status))
309 pr_err("alienware-wmi: zone set failure: %u\n", status);
310 return ACPI_FAILURE(status);
311}
312
313static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
314 char *buf)
315{
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);
323
324}
325
326static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
327 const char *buf, size_t count)
328{
329 struct platform_zone *target_zone;
330 int ret;
331 target_zone = match_zone(attr);
332 if (target_zone == NULL) {
333 pr_err("alienware-wmi: invalid target zone\n");
334 return 1;
335 }
336 ret = parse_rgb(buf, target_zone);
337 if (ret)
338 return ret;
339 ret = alienware_update_led(target_zone);
340 return ret ? ret : count;
341}
342
343/*
344 * LED Brightness (Global)
ec5eeadc 345 */
a46ad0f1
ML
346static int wmax_brightness(int brightness)
347{
348 acpi_status status;
349 struct acpi_buffer input;
350 struct wmax_brightness_args args = {
351 .led_mask = 0xFF,
352 .percentage = brightness,
353 };
354 input.length = (acpi_size) sizeof(args);
355 input.pointer = &args;
c0e4aa78 356 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 0,
a46ad0f1
ML
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);
361}
362
363static void global_led_set(struct led_classdev *led_cdev,
364 enum led_brightness brightness)
365{
366 int ret;
367 global_brightness = brightness;
368 if (interface == WMAX)
369 ret = wmax_brightness(brightness);
370 else
371 ret = alienware_update_led(&zone_data[0]);
372 if (ret)
373 pr_err("LED brightness update failed\n");
374}
375
376static enum led_brightness global_led_get(struct led_classdev *led_cdev)
377{
378 return global_brightness;
379}
380
381static struct led_classdev global_led = {
382 .brightness_set = global_led_set,
383 .brightness_get = global_led_get,
384 .name = "alienware::global_brightness",
385};
386
387/*
388 * Lighting control state device attribute (Global)
ec5eeadc 389 */
a46ad0f1
ML
390static ssize_t show_control_state(struct device *dev,
391 struct device_attribute *attr, char *buf)
392{
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");
398}
399
400static ssize_t store_control_state(struct device *dev,
401 struct device_attribute *attr,
402 const char *buf, size_t count)
403{
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;
411 else
412 val = WMAX_RUNNING;
413 lighting_control_state = val;
414 pr_debug("alienware-wmi: updated control state to %d\n",
415 lighting_control_state);
416 return count;
417}
418
419static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
420 store_control_state);
421
422static int alienware_zone_init(struct platform_device *dev)
423{
22ff1a36 424 u8 zone;
a46ad0f1
ML
425 char buffer[10];
426 char *name;
427
428 if (interface == WMAX) {
a46ad0f1
ML
429 lighting_control_state = WMAX_RUNNING;
430 } else if (interface == LEGACY) {
a46ad0f1
ML
431 lighting_control_state = LEGACY_RUNNING;
432 }
b998680e 433 global_led.max_brightness = 0x0F;
a46ad0f1
ML
434 global_brightness = global_led.max_brightness;
435
436 /*
437 * - zone_dev_attrs num_zones + 1 is for individual zones and then
438 * null terminated
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
442 */
443 zone_dev_attrs =
444 kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
445 GFP_KERNEL);
562c7cec
ML
446 if (!zone_dev_attrs)
447 return -ENOMEM;
448
a46ad0f1
ML
449 zone_attrs =
450 kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
451 GFP_KERNEL);
562c7cec
ML
452 if (!zone_attrs)
453 return -ENOMEM;
454
a46ad0f1
ML
455 zone_data =
456 kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
457 GFP_KERNEL);
562c7cec
ML
458 if (!zone_data)
459 return -ENOMEM;
a46ad0f1 460
22ff1a36
AB
461 for (zone = 0; zone < quirks->num_zones; zone++) {
462 sprintf(buffer, "zone%02hhX", zone);
a46ad0f1
ML
463 name = kstrdup(buffer, GFP_KERNEL);
464 if (name == NULL)
465 return 1;
22ff1a36
AB
466 sysfs_attr_init(&zone_dev_attrs[zone].attr);
467 zone_dev_attrs[zone].attr.name = name;
468 zone_dev_attrs[zone].attr.mode = 0644;
469 zone_dev_attrs[zone].show = zone_show;
470 zone_dev_attrs[zone].store = zone_set;
471 zone_data[zone].location = zone;
472 zone_attrs[zone] = &zone_dev_attrs[zone].attr;
473 zone_data[zone].attr = &zone_dev_attrs[zone];
a46ad0f1
ML
474 }
475 zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
476 zone_attribute_group.attrs = zone_attrs;
477
478 led_classdev_register(&dev->dev, &global_led);
479
480 return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
481}
482
483static void alienware_zone_exit(struct platform_device *dev)
484{
22ff1a36
AB
485 u8 zone;
486
a46ad0f1
ML
487 sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
488 led_classdev_unregister(&global_led);
489 if (zone_dev_attrs) {
22ff1a36
AB
490 for (zone = 0; zone < quirks->num_zones; zone++)
491 kfree(zone_dev_attrs[zone].attr.name);
a46ad0f1
ML
492 }
493 kfree(zone_dev_attrs);
494 kfree(zone_data);
495 kfree(zone_attrs);
496}
497
cbbb50d6 498static acpi_status alienware_wmax_command(struct wmax_basic_args *in_args,
bc2ef884 499 u32 command, int *out_data)
a46ad0f1
ML
500{
501 acpi_status status;
a46ad0f1 502 union acpi_object *obj;
bc2ef884
ML
503 struct acpi_buffer input;
504 struct acpi_buffer output;
505
506 input.length = (acpi_size) sizeof(*in_args);
507 input.pointer = in_args;
508 if (out_data != NULL) {
509 output.length = ACPI_ALLOCATE_BUFFER;
510 output.pointer = NULL;
c0e4aa78 511 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 0,
bc2ef884
ML
512 command, &input, &output);
513 } else
c0e4aa78 514 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 0,
bc2ef884
ML
515 command, &input, NULL);
516
517 if (ACPI_SUCCESS(status) && out_data != NULL) {
518 obj = (union acpi_object *)output.pointer;
519 if (obj && obj->type == ACPI_TYPE_INTEGER)
520 *out_data = (u32) obj->integer.value;
521 }
e2d5285b 522 kfree(output.pointer);
bc2ef884
ML
523 return status;
524
525}
526
cbbb50d6
ML
527/*
528 * The HDMI mux sysfs node indicates the status of the HDMI input mux.
529 * It can toggle between standard system GPU output and HDMI input.
530 */
bc2ef884
ML
531static ssize_t show_hdmi_cable(struct device *dev,
532 struct device_attribute *attr, char *buf)
533{
534 acpi_status status;
535 u32 out_data;
cbbb50d6 536 struct wmax_basic_args in_args = {
a46ad0f1
ML
537 .arg = 0,
538 };
bc2ef884 539 status =
cbbb50d6 540 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_CABLE,
bc2ef884
ML
541 (u32 *) &out_data);
542 if (ACPI_SUCCESS(status)) {
543 if (out_data == 0)
544 return scnprintf(buf, PAGE_SIZE,
545 "[unconnected] connected unknown\n");
546 else if (out_data == 1)
547 return scnprintf(buf, PAGE_SIZE,
548 "unconnected [connected] unknown\n");
549 }
550 pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
551 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
552}
553
554static ssize_t show_hdmi_source(struct device *dev,
555 struct device_attribute *attr, char *buf)
556{
557 acpi_status status;
558 u32 out_data;
cbbb50d6 559 struct wmax_basic_args in_args = {
bc2ef884
ML
560 .arg = 0,
561 };
562 status =
cbbb50d6 563 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_STATUS,
bc2ef884 564 (u32 *) &out_data);
a46ad0f1
ML
565
566 if (ACPI_SUCCESS(status)) {
bc2ef884 567 if (out_data == 1)
a46ad0f1
ML
568 return scnprintf(buf, PAGE_SIZE,
569 "[input] gpu unknown\n");
bc2ef884 570 else if (out_data == 2)
a46ad0f1
ML
571 return scnprintf(buf, PAGE_SIZE,
572 "input [gpu] unknown\n");
573 }
bc2ef884 574 pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data);
a46ad0f1
ML
575 return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
576}
577
bc2ef884
ML
578static ssize_t toggle_hdmi_source(struct device *dev,
579 struct device_attribute *attr,
580 const char *buf, size_t count)
a46ad0f1 581{
a46ad0f1 582 acpi_status status;
cbbb50d6 583 struct wmax_basic_args args;
a46ad0f1
ML
584 if (strcmp(buf, "gpu\n") == 0)
585 args.arg = 1;
586 else if (strcmp(buf, "input\n") == 0)
587 args.arg = 2;
588 else
589 args.arg = 3;
590 pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
bc2ef884 591
cbbb50d6 592 status = alienware_wmax_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
bc2ef884 593
a46ad0f1
ML
594 if (ACPI_FAILURE(status))
595 pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
596 status);
597 return count;
598}
599
bc2ef884
ML
600static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
601static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
602 toggle_hdmi_source);
603
604static struct attribute *hdmi_attrs[] = {
605 &dev_attr_cable.attr,
606 &dev_attr_source.attr,
607 NULL,
608};
a46ad0f1 609
4b7942d8 610static const struct attribute_group hdmi_attribute_group = {
bc2ef884
ML
611 .name = "hdmi",
612 .attrs = hdmi_attrs,
613};
614
615static void remove_hdmi(struct platform_device *dev)
a46ad0f1 616{
fee4efd7
ML
617 if (quirks->hdmi_mux > 0)
618 sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
a46ad0f1
ML
619}
620
bc2ef884 621static int create_hdmi(struct platform_device *dev)
a46ad0f1 622{
bc2ef884
ML
623 int ret;
624
625 ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
a46ad0f1 626 if (ret)
ec5eeadc 627 remove_hdmi(dev);
a46ad0f1
ML
628 return ret;
629}
630
cbbb50d6
ML
631/*
632 * Alienware GFX amplifier support
633 * - Currently supports reading cable status
634 * - Leaving expansion room to possibly support dock/undock events later
635 */
636static ssize_t show_amplifier_status(struct device *dev,
637 struct device_attribute *attr, char *buf)
638{
639 acpi_status status;
640 u32 out_data;
641 struct wmax_basic_args in_args = {
642 .arg = 0,
643 };
644 status =
645 alienware_wmax_command(&in_args, WMAX_METHOD_AMPLIFIER_CABLE,
646 (u32 *) &out_data);
647 if (ACPI_SUCCESS(status)) {
648 if (out_data == 0)
649 return scnprintf(buf, PAGE_SIZE,
650 "[unconnected] connected unknown\n");
651 else if (out_data == 1)
652 return scnprintf(buf, PAGE_SIZE,
653 "unconnected [connected] unknown\n");
654 }
655 pr_err("alienware-wmi: unknown amplifier cable status: %d\n", status);
656 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
657}
658
659static DEVICE_ATTR(status, S_IRUGO, show_amplifier_status, NULL);
660
661static struct attribute *amplifier_attrs[] = {
662 &dev_attr_status.attr,
663 NULL,
664};
665
4b7942d8 666static const struct attribute_group amplifier_attribute_group = {
cbbb50d6
ML
667 .name = "amplifier",
668 .attrs = amplifier_attrs,
669};
670
671static void remove_amplifier(struct platform_device *dev)
672{
673 if (quirks->amplifier > 0)
674 sysfs_remove_group(&dev->dev.kobj, &amplifier_attribute_group);
675}
676
677static int create_amplifier(struct platform_device *dev)
678{
679 int ret;
680
681 ret = sysfs_create_group(&dev->dev.kobj, &amplifier_attribute_group);
682 if (ret)
683 remove_amplifier(dev);
684 return ret;
685}
686
8ea81ec6
ML
687/*
688 * Deep Sleep Control support
689 * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
690 */
691static ssize_t show_deepsleep_status(struct device *dev,
692 struct device_attribute *attr, char *buf)
693{
694 acpi_status status;
695 u32 out_data;
696 struct wmax_basic_args in_args = {
697 .arg = 0,
698 };
699 status = alienware_wmax_command(&in_args, WMAX_METHOD_DEEP_SLEEP_STATUS,
700 (u32 *) &out_data);
701 if (ACPI_SUCCESS(status)) {
702 if (out_data == 0)
703 return scnprintf(buf, PAGE_SIZE,
704 "[disabled] s5 s5_s4\n");
705 else if (out_data == 1)
706 return scnprintf(buf, PAGE_SIZE,
707 "disabled [s5] s5_s4\n");
708 else if (out_data == 2)
709 return scnprintf(buf, PAGE_SIZE,
710 "disabled s5 [s5_s4]\n");
711 }
712 pr_err("alienware-wmi: unknown deep sleep status: %d\n", status);
713 return scnprintf(buf, PAGE_SIZE, "disabled s5 s5_s4 [unknown]\n");
714}
715
716static ssize_t toggle_deepsleep(struct device *dev,
717 struct device_attribute *attr,
718 const char *buf, size_t count)
719{
720 acpi_status status;
721 struct wmax_basic_args args;
722
723 if (strcmp(buf, "disabled\n") == 0)
724 args.arg = 0;
725 else if (strcmp(buf, "s5\n") == 0)
726 args.arg = 1;
727 else
728 args.arg = 2;
729 pr_debug("alienware-wmi: setting deep sleep to %d : %s", args.arg, buf);
730
731 status = alienware_wmax_command(&args, WMAX_METHOD_DEEP_SLEEP_CONTROL,
732 NULL);
733
734 if (ACPI_FAILURE(status))
735 pr_err("alienware-wmi: deep sleep control failed: results: %u\n",
736 status);
737 return count;
738}
739
740static DEVICE_ATTR(deepsleep, S_IRUGO | S_IWUSR, show_deepsleep_status, toggle_deepsleep);
741
742static struct attribute *deepsleep_attrs[] = {
743 &dev_attr_deepsleep.attr,
744 NULL,
745};
746
4b7942d8 747static const struct attribute_group deepsleep_attribute_group = {
8ea81ec6
ML
748 .name = "deepsleep",
749 .attrs = deepsleep_attrs,
750};
751
752static void remove_deepsleep(struct platform_device *dev)
753{
754 if (quirks->deepslp > 0)
755 sysfs_remove_group(&dev->dev.kobj, &deepsleep_attribute_group);
756}
757
758static int create_deepsleep(struct platform_device *dev)
759{
760 int ret;
761
762 ret = sysfs_create_group(&dev->dev.kobj, &deepsleep_attribute_group);
763 if (ret)
764 remove_deepsleep(dev);
765 return ret;
766}
767
a46ad0f1
ML
768static int __init alienware_wmi_init(void)
769{
770 int ret;
771
772 if (wmi_has_guid(LEGACY_CONTROL_GUID))
773 interface = LEGACY;
774 else if (wmi_has_guid(WMAX_CONTROL_GUID))
775 interface = WMAX;
776 else {
777 pr_warn("alienware-wmi: No known WMI GUID found\n");
778 return -ENODEV;
779 }
780
781 dmi_check_system(alienware_quirks);
782 if (quirks == NULL)
783 quirks = &quirk_unknown;
784
785 ret = platform_driver_register(&platform_driver);
786 if (ret)
787 goto fail_platform_driver;
788 platform_device = platform_device_alloc("alienware-wmi", -1);
789 if (!platform_device) {
790 ret = -ENOMEM;
791 goto fail_platform_device1;
792 }
793 ret = platform_device_add(platform_device);
794 if (ret)
795 goto fail_platform_device2;
796
fee4efd7 797 if (quirks->hdmi_mux > 0) {
bc2ef884 798 ret = create_hdmi(platform_device);
a46ad0f1
ML
799 if (ret)
800 goto fail_prep_hdmi;
801 }
802
cbbb50d6
ML
803 if (quirks->amplifier > 0) {
804 ret = create_amplifier(platform_device);
805 if (ret)
806 goto fail_prep_amplifier;
807 }
808
8ea81ec6
ML
809 if (quirks->deepslp > 0) {
810 ret = create_deepsleep(platform_device);
811 if (ret)
812 goto fail_prep_deepsleep;
813 }
814
a46ad0f1
ML
815 ret = alienware_zone_init(platform_device);
816 if (ret)
817 goto fail_prep_zones;
818
819 return 0;
820
821fail_prep_zones:
822 alienware_zone_exit(platform_device);
8ea81ec6 823fail_prep_deepsleep:
cbbb50d6 824fail_prep_amplifier:
a46ad0f1
ML
825fail_prep_hdmi:
826 platform_device_del(platform_device);
827fail_platform_device2:
828 platform_device_put(platform_device);
829fail_platform_device1:
830 platform_driver_unregister(&platform_driver);
831fail_platform_driver:
832 return ret;
833}
834
835module_init(alienware_wmi_init);
836
837static void __exit alienware_wmi_exit(void)
838{
a46ad0f1 839 if (platform_device) {
562c7cec
ML
840 alienware_zone_exit(platform_device);
841 remove_hdmi(platform_device);
a46ad0f1
ML
842 platform_device_unregister(platform_device);
843 platform_driver_unregister(&platform_driver);
844 }
845}
846
847module_exit(alienware_wmi_exit);