xref: /linux/drivers/platform/x86/dell/alienware-wmi.c (revision 8e07e0e3964ca4e23ce7b68e2096fe660a888942)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Alienware AlienFX control
4  *
5  * Copyright (C) 2014 Dell Inc <Dell.Client.Kernel@dell.com>
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/acpi.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/dmi.h>
14 #include <linux/leds.h>
15 
16 #define LEGACY_CONTROL_GUID		"A90597CE-A997-11DA-B012-B622A1EF5492"
17 #define LEGACY_POWER_CONTROL_GUID	"A80593CE-A997-11DA-B012-B622A1EF5492"
18 #define WMAX_CONTROL_GUID		"A70591CE-A997-11DA-B012-B622A1EF5492"
19 
20 #define WMAX_METHOD_HDMI_SOURCE		0x1
21 #define WMAX_METHOD_HDMI_STATUS		0x2
22 #define WMAX_METHOD_BRIGHTNESS		0x3
23 #define WMAX_METHOD_ZONE_CONTROL	0x4
24 #define WMAX_METHOD_HDMI_CABLE		0x5
25 #define WMAX_METHOD_AMPLIFIER_CABLE	0x6
26 #define WMAX_METHOD_DEEP_SLEEP_CONTROL	0x0B
27 #define WMAX_METHOD_DEEP_SLEEP_STATUS	0x0C
28 
29 MODULE_AUTHOR("Mario Limonciello <mario.limonciello@outlook.com>");
30 MODULE_DESCRIPTION("Alienware special feature control");
31 MODULE_LICENSE("GPL");
32 MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID);
33 MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID);
34 
35 enum INTERFACE_FLAGS {
36 	LEGACY,
37 	WMAX,
38 };
39 
40 enum LEGACY_CONTROL_STATES {
41 	LEGACY_RUNNING = 1,
42 	LEGACY_BOOTING = 0,
43 	LEGACY_SUSPEND = 3,
44 };
45 
46 enum WMAX_CONTROL_STATES {
47 	WMAX_RUNNING = 0xFF,
48 	WMAX_BOOTING = 0,
49 	WMAX_SUSPEND = 3,
50 };
51 
52 struct quirk_entry {
53 	u8 num_zones;
54 	u8 hdmi_mux;
55 	u8 amplifier;
56 	u8 deepslp;
57 };
58 
59 static struct quirk_entry *quirks;
60 
61 
62 static struct quirk_entry quirk_inspiron5675 = {
63 	.num_zones = 2,
64 	.hdmi_mux = 0,
65 	.amplifier = 0,
66 	.deepslp = 0,
67 };
68 
69 static struct quirk_entry quirk_unknown = {
70 	.num_zones = 2,
71 	.hdmi_mux = 0,
72 	.amplifier = 0,
73 	.deepslp = 0,
74 };
75 
76 static struct quirk_entry quirk_x51_r1_r2 = {
77 	.num_zones = 3,
78 	.hdmi_mux = 0,
79 	.amplifier = 0,
80 	.deepslp = 0,
81 };
82 
83 static struct quirk_entry quirk_x51_r3 = {
84 	.num_zones = 4,
85 	.hdmi_mux = 0,
86 	.amplifier = 1,
87 	.deepslp = 0,
88 };
89 
90 static struct quirk_entry quirk_asm100 = {
91 	.num_zones = 2,
92 	.hdmi_mux = 1,
93 	.amplifier = 0,
94 	.deepslp = 0,
95 };
96 
97 static struct quirk_entry quirk_asm200 = {
98 	.num_zones = 2,
99 	.hdmi_mux = 1,
100 	.amplifier = 0,
101 	.deepslp = 1,
102 };
103 
104 static struct quirk_entry quirk_asm201 = {
105 	.num_zones = 2,
106 	.hdmi_mux = 1,
107 	.amplifier = 1,
108 	.deepslp = 1,
109 };
110 
111 static int __init dmi_matched(const struct dmi_system_id *dmi)
112 {
113 	quirks = dmi->driver_data;
114 	return 1;
115 }
116 
117 static const struct dmi_system_id alienware_quirks[] __initconst = {
118 	{
119 	 .callback = dmi_matched,
120 	 .ident = "Alienware X51 R3",
121 	 .matches = {
122 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
123 		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R3"),
124 		     },
125 	 .driver_data = &quirk_x51_r3,
126 	 },
127 	{
128 	 .callback = dmi_matched,
129 	 .ident = "Alienware X51 R2",
130 	 .matches = {
131 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
132 		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
133 		     },
134 	 .driver_data = &quirk_x51_r1_r2,
135 	 },
136 	{
137 	 .callback = dmi_matched,
138 	 .ident = "Alienware X51 R1",
139 	 .matches = {
140 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
141 		     DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
142 		     },
143 	 .driver_data = &quirk_x51_r1_r2,
144 	 },
145 	{
146 	 .callback = dmi_matched,
147 	 .ident = "Alienware ASM100",
148 	 .matches = {
149 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
150 		     DMI_MATCH(DMI_PRODUCT_NAME, "ASM100"),
151 		     },
152 	 .driver_data = &quirk_asm100,
153 	 },
154 	{
155 	 .callback = dmi_matched,
156 	 .ident = "Alienware ASM200",
157 	 .matches = {
158 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
159 		     DMI_MATCH(DMI_PRODUCT_NAME, "ASM200"),
160 		     },
161 	 .driver_data = &quirk_asm200,
162 	 },
163 	{
164 	 .callback = dmi_matched,
165 	 .ident = "Alienware ASM201",
166 	 .matches = {
167 		     DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
168 		     DMI_MATCH(DMI_PRODUCT_NAME, "ASM201"),
169 		     },
170 	 .driver_data = &quirk_asm201,
171 	 },
172 	 {
173 	 .callback = dmi_matched,
174 	 .ident = "Dell Inc. Inspiron 5675",
175 	 .matches = {
176 		     DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
177 		     DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5675"),
178 		     },
179 	 .driver_data = &quirk_inspiron5675,
180 	 },
181 	{}
182 };
183 
184 struct color_platform {
185 	u8 blue;
186 	u8 green;
187 	u8 red;
188 } __packed;
189 
190 struct platform_zone {
191 	u8 location;
192 	struct device_attribute *attr;
193 	struct color_platform colors;
194 };
195 
196 struct wmax_brightness_args {
197 	u32 led_mask;
198 	u32 percentage;
199 };
200 
201 struct wmax_basic_args {
202 	u8 arg;
203 };
204 
205 struct legacy_led_args {
206 	struct color_platform colors;
207 	u8 brightness;
208 	u8 state;
209 } __packed;
210 
211 struct wmax_led_args {
212 	u32 led_mask;
213 	struct color_platform colors;
214 	u8 state;
215 } __packed;
216 
217 static struct platform_device *platform_device;
218 static struct device_attribute *zone_dev_attrs;
219 static struct attribute **zone_attrs;
220 static struct platform_zone *zone_data;
221 
222 static struct platform_driver platform_driver = {
223 	.driver = {
224 		   .name = "alienware-wmi",
225 		   }
226 };
227 
228 static struct attribute_group zone_attribute_group = {
229 	.name = "rgb_zones",
230 };
231 
232 static u8 interface;
233 static u8 lighting_control_state;
234 static u8 global_brightness;
235 
236 /*
237  * Helpers used for zone control
238  */
239 static int parse_rgb(const char *buf, struct platform_zone *zone)
240 {
241 	long unsigned int rgb;
242 	int ret;
243 	union color_union {
244 		struct color_platform cp;
245 		int package;
246 	} repackager;
247 
248 	ret = kstrtoul(buf, 16, &rgb);
249 	if (ret)
250 		return ret;
251 
252 	/* RGB triplet notation is 24-bit hexadecimal */
253 	if (rgb > 0xFFFFFF)
254 		return -EINVAL;
255 
256 	repackager.package = rgb & 0x0f0f0f0f;
257 	pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
258 		 repackager.cp.red, repackager.cp.green, repackager.cp.blue);
259 	zone->colors = repackager.cp;
260 	return 0;
261 }
262 
263 static struct platform_zone *match_zone(struct device_attribute *attr)
264 {
265 	u8 zone;
266 
267 	for (zone = 0; zone < quirks->num_zones; zone++) {
268 		if ((struct device_attribute *)zone_data[zone].attr == attr) {
269 			pr_debug("alienware-wmi: matched zone location: %d\n",
270 				 zone_data[zone].location);
271 			return &zone_data[zone];
272 		}
273 	}
274 	return NULL;
275 }
276 
277 /*
278  * Individual RGB zone control
279  */
280 static int alienware_update_led(struct platform_zone *zone)
281 {
282 	int method_id;
283 	acpi_status status;
284 	char *guid;
285 	struct acpi_buffer input;
286 	struct legacy_led_args legacy_args;
287 	struct wmax_led_args wmax_basic_args;
288 	if (interface == WMAX) {
289 		wmax_basic_args.led_mask = 1 << zone->location;
290 		wmax_basic_args.colors = zone->colors;
291 		wmax_basic_args.state = lighting_control_state;
292 		guid = WMAX_CONTROL_GUID;
293 		method_id = WMAX_METHOD_ZONE_CONTROL;
294 
295 		input.length = (acpi_size) sizeof(wmax_basic_args);
296 		input.pointer = &wmax_basic_args;
297 	} else {
298 		legacy_args.colors = zone->colors;
299 		legacy_args.brightness = global_brightness;
300 		legacy_args.state = 0;
301 		if (lighting_control_state == LEGACY_BOOTING ||
302 		    lighting_control_state == LEGACY_SUSPEND) {
303 			guid = LEGACY_POWER_CONTROL_GUID;
304 			legacy_args.state = lighting_control_state;
305 		} else
306 			guid = LEGACY_CONTROL_GUID;
307 		method_id = zone->location + 1;
308 
309 		input.length = (acpi_size) sizeof(legacy_args);
310 		input.pointer = &legacy_args;
311 	}
312 	pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
313 
314 	status = wmi_evaluate_method(guid, 0, method_id, &input, NULL);
315 	if (ACPI_FAILURE(status))
316 		pr_err("alienware-wmi: zone set failure: %u\n", status);
317 	return ACPI_FAILURE(status);
318 }
319 
320 static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
321 			 char *buf)
322 {
323 	struct platform_zone *target_zone;
324 	target_zone = match_zone(attr);
325 	if (target_zone == NULL)
326 		return sprintf(buf, "red: -1, green: -1, blue: -1\n");
327 	return sprintf(buf, "red: %d, green: %d, blue: %d\n",
328 		       target_zone->colors.red,
329 		       target_zone->colors.green, target_zone->colors.blue);
330 
331 }
332 
333 static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
334 			const char *buf, size_t count)
335 {
336 	struct platform_zone *target_zone;
337 	int ret;
338 	target_zone = match_zone(attr);
339 	if (target_zone == NULL) {
340 		pr_err("alienware-wmi: invalid target zone\n");
341 		return 1;
342 	}
343 	ret = parse_rgb(buf, target_zone);
344 	if (ret)
345 		return ret;
346 	ret = alienware_update_led(target_zone);
347 	return ret ? ret : count;
348 }
349 
350 /*
351  * LED Brightness (Global)
352  */
353 static int wmax_brightness(int brightness)
354 {
355 	acpi_status status;
356 	struct acpi_buffer input;
357 	struct wmax_brightness_args args = {
358 		.led_mask = 0xFF,
359 		.percentage = brightness,
360 	};
361 	input.length = (acpi_size) sizeof(args);
362 	input.pointer = &args;
363 	status = wmi_evaluate_method(WMAX_CONTROL_GUID, 0,
364 				     WMAX_METHOD_BRIGHTNESS, &input, NULL);
365 	if (ACPI_FAILURE(status))
366 		pr_err("alienware-wmi: brightness set failure: %u\n", status);
367 	return ACPI_FAILURE(status);
368 }
369 
370 static void global_led_set(struct led_classdev *led_cdev,
371 			   enum led_brightness brightness)
372 {
373 	int ret;
374 	global_brightness = brightness;
375 	if (interface == WMAX)
376 		ret = wmax_brightness(brightness);
377 	else
378 		ret = alienware_update_led(&zone_data[0]);
379 	if (ret)
380 		pr_err("LED brightness update failed\n");
381 }
382 
383 static enum led_brightness global_led_get(struct led_classdev *led_cdev)
384 {
385 	return global_brightness;
386 }
387 
388 static struct led_classdev global_led = {
389 	.brightness_set = global_led_set,
390 	.brightness_get = global_led_get,
391 	.name = "alienware::global_brightness",
392 };
393 
394 /*
395  * Lighting control state device attribute (Global)
396  */
397 static ssize_t show_control_state(struct device *dev,
398 				  struct device_attribute *attr, char *buf)
399 {
400 	if (lighting_control_state == LEGACY_BOOTING)
401 		return sysfs_emit(buf, "[booting] running suspend\n");
402 	else if (lighting_control_state == LEGACY_SUSPEND)
403 		return sysfs_emit(buf, "booting running [suspend]\n");
404 	return sysfs_emit(buf, "booting [running] suspend\n");
405 }
406 
407 static ssize_t store_control_state(struct device *dev,
408 				   struct device_attribute *attr,
409 				   const char *buf, size_t count)
410 {
411 	long unsigned int val;
412 	if (strcmp(buf, "booting\n") == 0)
413 		val = LEGACY_BOOTING;
414 	else if (strcmp(buf, "suspend\n") == 0)
415 		val = LEGACY_SUSPEND;
416 	else if (interface == LEGACY)
417 		val = LEGACY_RUNNING;
418 	else
419 		val = WMAX_RUNNING;
420 	lighting_control_state = val;
421 	pr_debug("alienware-wmi: updated control state to %d\n",
422 		 lighting_control_state);
423 	return count;
424 }
425 
426 static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
427 		   store_control_state);
428 
429 static int alienware_zone_init(struct platform_device *dev)
430 {
431 	u8 zone;
432 	char buffer[10];
433 	char *name;
434 
435 	if (interface == WMAX) {
436 		lighting_control_state = WMAX_RUNNING;
437 	} else if (interface == LEGACY) {
438 		lighting_control_state = LEGACY_RUNNING;
439 	}
440 	global_led.max_brightness = 0x0F;
441 	global_brightness = global_led.max_brightness;
442 
443 	/*
444 	 *      - zone_dev_attrs num_zones + 1 is for individual zones and then
445 	 *        null terminated
446 	 *      - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
447 	 *        the lighting control + null terminated
448 	 *      - zone_data num_zones is for the distinct zones
449 	 */
450 	zone_dev_attrs =
451 	    kcalloc(quirks->num_zones + 1, sizeof(struct device_attribute),
452 		    GFP_KERNEL);
453 	if (!zone_dev_attrs)
454 		return -ENOMEM;
455 
456 	zone_attrs =
457 	    kcalloc(quirks->num_zones + 2, sizeof(struct attribute *),
458 		    GFP_KERNEL);
459 	if (!zone_attrs)
460 		return -ENOMEM;
461 
462 	zone_data =
463 	    kcalloc(quirks->num_zones, sizeof(struct platform_zone),
464 		    GFP_KERNEL);
465 	if (!zone_data)
466 		return -ENOMEM;
467 
468 	for (zone = 0; zone < quirks->num_zones; zone++) {
469 		sprintf(buffer, "zone%02hhX", zone);
470 		name = kstrdup(buffer, GFP_KERNEL);
471 		if (name == NULL)
472 			return 1;
473 		sysfs_attr_init(&zone_dev_attrs[zone].attr);
474 		zone_dev_attrs[zone].attr.name = name;
475 		zone_dev_attrs[zone].attr.mode = 0644;
476 		zone_dev_attrs[zone].show = zone_show;
477 		zone_dev_attrs[zone].store = zone_set;
478 		zone_data[zone].location = zone;
479 		zone_attrs[zone] = &zone_dev_attrs[zone].attr;
480 		zone_data[zone].attr = &zone_dev_attrs[zone];
481 	}
482 	zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
483 	zone_attribute_group.attrs = zone_attrs;
484 
485 	led_classdev_register(&dev->dev, &global_led);
486 
487 	return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
488 }
489 
490 static void alienware_zone_exit(struct platform_device *dev)
491 {
492 	u8 zone;
493 
494 	sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
495 	led_classdev_unregister(&global_led);
496 	if (zone_dev_attrs) {
497 		for (zone = 0; zone < quirks->num_zones; zone++)
498 			kfree(zone_dev_attrs[zone].attr.name);
499 	}
500 	kfree(zone_dev_attrs);
501 	kfree(zone_data);
502 	kfree(zone_attrs);
503 }
504 
505 static acpi_status alienware_wmax_command(struct wmax_basic_args *in_args,
506 					  u32 command, int *out_data)
507 {
508 	acpi_status status;
509 	union acpi_object *obj;
510 	struct acpi_buffer input;
511 	struct acpi_buffer output;
512 
513 	input.length = (acpi_size) sizeof(*in_args);
514 	input.pointer = in_args;
515 	if (out_data) {
516 		output.length = ACPI_ALLOCATE_BUFFER;
517 		output.pointer = NULL;
518 		status = wmi_evaluate_method(WMAX_CONTROL_GUID, 0,
519 					     command, &input, &output);
520 		if (ACPI_SUCCESS(status)) {
521 			obj = (union acpi_object *)output.pointer;
522 			if (obj && obj->type == ACPI_TYPE_INTEGER)
523 				*out_data = (u32)obj->integer.value;
524 		}
525 		kfree(output.pointer);
526 	} else {
527 		status = wmi_evaluate_method(WMAX_CONTROL_GUID, 0,
528 					     command, &input, NULL);
529 	}
530 	return status;
531 }
532 
533 /*
534  *	The HDMI mux sysfs node indicates the status of the HDMI input mux.
535  *	It can toggle between standard system GPU output and HDMI input.
536  */
537 static ssize_t show_hdmi_cable(struct device *dev,
538 			       struct device_attribute *attr, char *buf)
539 {
540 	acpi_status status;
541 	u32 out_data;
542 	struct wmax_basic_args in_args = {
543 		.arg = 0,
544 	};
545 	status =
546 	    alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_CABLE,
547 				   (u32 *) &out_data);
548 	if (ACPI_SUCCESS(status)) {
549 		if (out_data == 0)
550 			return sysfs_emit(buf, "[unconnected] connected unknown\n");
551 		else if (out_data == 1)
552 			return sysfs_emit(buf, "unconnected [connected] unknown\n");
553 	}
554 	pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
555 	return sysfs_emit(buf, "unconnected connected [unknown]\n");
556 }
557 
558 static ssize_t show_hdmi_source(struct device *dev,
559 				struct device_attribute *attr, char *buf)
560 {
561 	acpi_status status;
562 	u32 out_data;
563 	struct wmax_basic_args in_args = {
564 		.arg = 0,
565 	};
566 	status =
567 	    alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_STATUS,
568 				   (u32 *) &out_data);
569 
570 	if (ACPI_SUCCESS(status)) {
571 		if (out_data == 1)
572 			return sysfs_emit(buf, "[input] gpu unknown\n");
573 		else if (out_data == 2)
574 			return sysfs_emit(buf, "input [gpu] unknown\n");
575 	}
576 	pr_err("alienware-wmi: unknown HDMI source status: %u\n", status);
577 	return sysfs_emit(buf, "input gpu [unknown]\n");
578 }
579 
580 static ssize_t toggle_hdmi_source(struct device *dev,
581 				  struct device_attribute *attr,
582 				  const char *buf, size_t count)
583 {
584 	acpi_status status;
585 	struct wmax_basic_args args;
586 	if (strcmp(buf, "gpu\n") == 0)
587 		args.arg = 1;
588 	else if (strcmp(buf, "input\n") == 0)
589 		args.arg = 2;
590 	else
591 		args.arg = 3;
592 	pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
593 
594 	status = alienware_wmax_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
595 
596 	if (ACPI_FAILURE(status))
597 		pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
598 		       status);
599 	return count;
600 }
601 
602 static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
603 static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
604 		   toggle_hdmi_source);
605 
606 static struct attribute *hdmi_attrs[] = {
607 	&dev_attr_cable.attr,
608 	&dev_attr_source.attr,
609 	NULL,
610 };
611 
612 static const struct attribute_group hdmi_attribute_group = {
613 	.name = "hdmi",
614 	.attrs = hdmi_attrs,
615 };
616 
617 static void remove_hdmi(struct platform_device *dev)
618 {
619 	if (quirks->hdmi_mux > 0)
620 		sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
621 }
622 
623 static int create_hdmi(struct platform_device *dev)
624 {
625 	int ret;
626 
627 	ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
628 	if (ret)
629 		remove_hdmi(dev);
630 	return ret;
631 }
632 
633 /*
634  * Alienware GFX amplifier support
635  * - Currently supports reading cable status
636  * - Leaving expansion room to possibly support dock/undock events later
637  */
638 static ssize_t show_amplifier_status(struct device *dev,
639 				     struct device_attribute *attr, char *buf)
640 {
641 	acpi_status status;
642 	u32 out_data;
643 	struct wmax_basic_args in_args = {
644 		.arg = 0,
645 	};
646 	status =
647 	    alienware_wmax_command(&in_args, WMAX_METHOD_AMPLIFIER_CABLE,
648 				   (u32 *) &out_data);
649 	if (ACPI_SUCCESS(status)) {
650 		if (out_data == 0)
651 			return sysfs_emit(buf, "[unconnected] connected unknown\n");
652 		else if (out_data == 1)
653 			return sysfs_emit(buf, "unconnected [connected] unknown\n");
654 	}
655 	pr_err("alienware-wmi: unknown amplifier cable status: %d\n", status);
656 	return sysfs_emit(buf, "unconnected connected [unknown]\n");
657 }
658 
659 static DEVICE_ATTR(status, S_IRUGO, show_amplifier_status, NULL);
660 
661 static struct attribute *amplifier_attrs[] = {
662 	&dev_attr_status.attr,
663 	NULL,
664 };
665 
666 static const struct attribute_group amplifier_attribute_group = {
667 	.name = "amplifier",
668 	.attrs = amplifier_attrs,
669 };
670 
671 static 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 
677 static 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 
687 /*
688  * Deep Sleep Control support
689  * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
690  */
691 static 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 sysfs_emit(buf, "[disabled] s5 s5_s4\n");
704 		else if (out_data == 1)
705 			return sysfs_emit(buf, "disabled [s5] s5_s4\n");
706 		else if (out_data == 2)
707 			return sysfs_emit(buf, "disabled s5 [s5_s4]\n");
708 	}
709 	pr_err("alienware-wmi: unknown deep sleep status: %d\n", status);
710 	return sysfs_emit(buf, "disabled s5 s5_s4 [unknown]\n");
711 }
712 
713 static 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 
737 static DEVICE_ATTR(deepsleep, S_IRUGO | S_IWUSR, show_deepsleep_status, toggle_deepsleep);
738 
739 static struct attribute *deepsleep_attrs[] = {
740 	&dev_attr_deepsleep.attr,
741 	NULL,
742 };
743 
744 static const struct attribute_group deepsleep_attribute_group = {
745 	.name = "deepsleep",
746 	.attrs = deepsleep_attrs,
747 };
748 
749 static 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 
755 static 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 
765 static 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", PLATFORM_DEVID_NONE);
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 
794 	if (quirks->hdmi_mux > 0) {
795 		ret = create_hdmi(platform_device);
796 		if (ret)
797 			goto fail_prep_hdmi;
798 	}
799 
800 	if (quirks->amplifier > 0) {
801 		ret = create_amplifier(platform_device);
802 		if (ret)
803 			goto fail_prep_amplifier;
804 	}
805 
806 	if (quirks->deepslp > 0) {
807 		ret = create_deepsleep(platform_device);
808 		if (ret)
809 			goto fail_prep_deepsleep;
810 	}
811 
812 	ret = alienware_zone_init(platform_device);
813 	if (ret)
814 		goto fail_prep_zones;
815 
816 	return 0;
817 
818 fail_prep_zones:
819 	alienware_zone_exit(platform_device);
820 fail_prep_deepsleep:
821 fail_prep_amplifier:
822 fail_prep_hdmi:
823 	platform_device_del(platform_device);
824 fail_platform_device2:
825 	platform_device_put(platform_device);
826 fail_platform_device1:
827 	platform_driver_unregister(&platform_driver);
828 fail_platform_driver:
829 	return ret;
830 }
831 
832 module_init(alienware_wmi_init);
833 
834 static void __exit alienware_wmi_exit(void)
835 {
836 	if (platform_device) {
837 		alienware_zone_exit(platform_device);
838 		remove_hdmi(platform_device);
839 		platform_device_unregister(platform_device);
840 		platform_driver_unregister(&platform_driver);
841 	}
842 }
843 
844 module_exit(alienware_wmi_exit);
845