blob: a9f8d3044cd3686ed31a0f14117d1d1fb3e2f4af [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>
24#include <linux/acpi.h>
25#include <linux/leds.h>
26
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"
30
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
Mario Limonciellobc2ef882014-05-07 15:08:10 -050035#define WMAX_METHOD_HDMI_CABLE 0x5
Mario Limonciellocbbb50d62016-02-02 15:38:54 -060036#define WMAX_METHOD_AMPLIFIER_CABLE 0x6
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060037#define WMAX_METHOD_DEEP_SLEEP_CONTROL 0x0B
38#define WMAX_METHOD_DEEP_SLEEP_STATUS 0x0C
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040039
40MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>");
41MODULE_DESCRIPTION("Alienware special feature control");
42MODULE_LICENSE("GPL");
43MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID);
44MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID);
45
46enum INTERFACE_FLAGS {
47 LEGACY,
48 WMAX,
49};
50
51enum LEGACY_CONTROL_STATES {
52 LEGACY_RUNNING = 1,
53 LEGACY_BOOTING = 0,
54 LEGACY_SUSPEND = 3,
55};
56
57enum WMAX_CONTROL_STATES {
58 WMAX_RUNNING = 0xFF,
59 WMAX_BOOTING = 0,
60 WMAX_SUSPEND = 3,
61};
62
63struct quirk_entry {
64 u8 num_zones;
Mario Limonciellofee4efd2014-07-23 23:19:23 -050065 u8 hdmi_mux;
Mario Limonciellocbbb50d62016-02-02 15:38:54 -060066 u8 amplifier;
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060067 u8 deepslp;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040068};
69
70static struct quirk_entry *quirks;
71
72static struct quirk_entry quirk_unknown = {
73 .num_zones = 2,
Mario Limonciellofee4efd2014-07-23 23:19:23 -050074 .hdmi_mux = 0,
Mario Limonciellocbbb50d62016-02-02 15:38:54 -060075 .amplifier = 0,
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060076 .deepslp = 0,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040077};
78
Mario Limonciello9e503a92016-02-02 15:38:53 -060079static struct quirk_entry quirk_x51_r1_r2 = {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040080 .num_zones = 3,
Mario Limonciellocbbb50d62016-02-02 15:38:54 -060081 .hdmi_mux = 0,
82 .amplifier = 0,
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060083 .deepslp = 0,
Mario Limonciellofee4efd2014-07-23 23:19:23 -050084};
85
Mario Limonciello9e503a92016-02-02 15:38:53 -060086static struct quirk_entry quirk_x51_r3 = {
87 .num_zones = 4,
88 .hdmi_mux = 0,
Mario Limonciellocbbb50d62016-02-02 15:38:54 -060089 .amplifier = 1,
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060090 .deepslp = 0,
Mario Limonciello9e503a92016-02-02 15:38:53 -060091};
92
Mario Limonciellofee4efd2014-07-23 23:19:23 -050093static struct quirk_entry quirk_asm100 = {
94 .num_zones = 2,
95 .hdmi_mux = 1,
Mario Limonciellocbbb50d62016-02-02 15:38:54 -060096 .amplifier = 0,
Mario Limonciello8ea81ec2016-02-02 15:38:55 -060097 .deepslp = 0,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040098};
99
Mathias Kraused997d882014-07-16 19:43:07 +0200100static int __init dmi_matched(const struct dmi_system_id *dmi)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400101{
102 quirks = dmi->driver_data;
103 return 1;
104}
105
Mathias Kraused997d882014-07-16 19:43:07 +0200106static const struct dmi_system_id alienware_quirks[] __initconst = {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400107 {
108 .callback = dmi_matched,
Mario Limonciello9e503a92016-02-02 15:38:53 -0600109 .ident = "Alienware X51 R3",
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400110 .matches = {
111 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
Mario Limonciello9e503a92016-02-02 15:38:53 -0600112 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R3"),
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400113 },
Mario Limonciello9e503a92016-02-02 15:38:53 -0600114 .driver_data = &quirk_x51_r3,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400115 },
116 {
117 .callback = dmi_matched,
118 .ident = "Alienware X51 R2",
119 .matches = {
120 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
121 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
122 },
Mario Limonciello9e503a92016-02-02 15:38:53 -0600123 .driver_data = &quirk_x51_r1_r2,
124 },
125 {
126 .callback = dmi_matched,
127 .ident = "Alienware X51 R1",
128 .matches = {
129 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
130 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
131 },
132 .driver_data = &quirk_x51_r1_r2,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400133 },
Mario Limonciellofee4efd2014-07-23 23:19:23 -0500134 {
Mario Limonciello66ad0dd2016-02-02 15:38:52 -0600135 .callback = dmi_matched,
136 .ident = "Alienware ASM100",
137 .matches = {
138 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
139 DMI_MATCH(DMI_PRODUCT_NAME, "ASM100"),
140 },
141 .driver_data = &quirk_asm100,
142 },
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400143 {}
144};
145
146struct color_platform {
147 u8 blue;
148 u8 green;
149 u8 red;
150} __packed;
151
152struct platform_zone {
153 u8 location;
154 struct device_attribute *attr;
155 struct color_platform colors;
156};
157
158struct wmax_brightness_args {
159 u32 led_mask;
160 u32 percentage;
161};
162
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600163struct wmax_basic_args {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400164 u8 arg;
165};
166
167struct legacy_led_args {
168 struct color_platform colors;
169 u8 brightness;
170 u8 state;
171} __packed;
172
173struct wmax_led_args {
174 u32 led_mask;
175 struct color_platform colors;
176 u8 state;
177} __packed;
178
179static struct platform_device *platform_device;
180static struct device_attribute *zone_dev_attrs;
181static struct attribute **zone_attrs;
182static struct platform_zone *zone_data;
183
184static struct platform_driver platform_driver = {
185 .driver = {
186 .name = "alienware-wmi",
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400187 }
188};
189
190static struct attribute_group zone_attribute_group = {
191 .name = "rgb_zones",
192};
193
194static u8 interface;
195static u8 lighting_control_state;
196static u8 global_brightness;
197
198/*
199 * Helpers used for zone control
200*/
201static int parse_rgb(const char *buf, struct platform_zone *zone)
202{
203 long unsigned int rgb;
204 int ret;
205 union color_union {
206 struct color_platform cp;
207 int package;
208 } repackager;
209
210 ret = kstrtoul(buf, 16, &rgb);
211 if (ret)
212 return ret;
213
214 /* RGB triplet notation is 24-bit hexadecimal */
215 if (rgb > 0xFFFFFF)
216 return -EINVAL;
217
218 repackager.package = rgb & 0x0f0f0f0f;
219 pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
220 repackager.cp.red, repackager.cp.green, repackager.cp.blue);
221 zone->colors = repackager.cp;
222 return 0;
223}
224
225static struct platform_zone *match_zone(struct device_attribute *attr)
226{
227 int i;
228 for (i = 0; i < quirks->num_zones; i++) {
229 if ((struct device_attribute *)zone_data[i].attr == attr) {
230 pr_debug("alienware-wmi: matched zone location: %d\n",
231 zone_data[i].location);
232 return &zone_data[i];
233 }
234 }
235 return NULL;
236}
237
238/*
239 * Individual RGB zone control
240*/
241static int alienware_update_led(struct platform_zone *zone)
242{
243 int method_id;
244 acpi_status status;
245 char *guid;
246 struct acpi_buffer input;
247 struct legacy_led_args legacy_args;
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600248 struct wmax_led_args wmax_basic_args;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400249 if (interface == WMAX) {
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600250 wmax_basic_args.led_mask = 1 << zone->location;
251 wmax_basic_args.colors = zone->colors;
252 wmax_basic_args.state = lighting_control_state;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400253 guid = WMAX_CONTROL_GUID;
254 method_id = WMAX_METHOD_ZONE_CONTROL;
255
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600256 input.length = (acpi_size) sizeof(wmax_basic_args);
257 input.pointer = &wmax_basic_args;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400258 } else {
259 legacy_args.colors = zone->colors;
260 legacy_args.brightness = global_brightness;
261 legacy_args.state = 0;
262 if (lighting_control_state == LEGACY_BOOTING ||
263 lighting_control_state == LEGACY_SUSPEND) {
264 guid = LEGACY_POWER_CONTROL_GUID;
265 legacy_args.state = lighting_control_state;
266 } else
267 guid = LEGACY_CONTROL_GUID;
268 method_id = zone->location + 1;
269
270 input.length = (acpi_size) sizeof(legacy_args);
271 input.pointer = &legacy_args;
272 }
273 pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
274
275 status = wmi_evaluate_method(guid, 1, method_id, &input, NULL);
276 if (ACPI_FAILURE(status))
277 pr_err("alienware-wmi: zone set failure: %u\n", status);
278 return ACPI_FAILURE(status);
279}
280
281static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
282 char *buf)
283{
284 struct platform_zone *target_zone;
285 target_zone = match_zone(attr);
286 if (target_zone == NULL)
287 return sprintf(buf, "red: -1, green: -1, blue: -1\n");
288 return sprintf(buf, "red: %d, green: %d, blue: %d\n",
289 target_zone->colors.red,
290 target_zone->colors.green, target_zone->colors.blue);
291
292}
293
294static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
296{
297 struct platform_zone *target_zone;
298 int ret;
299 target_zone = match_zone(attr);
300 if (target_zone == NULL) {
301 pr_err("alienware-wmi: invalid target zone\n");
302 return 1;
303 }
304 ret = parse_rgb(buf, target_zone);
305 if (ret)
306 return ret;
307 ret = alienware_update_led(target_zone);
308 return ret ? ret : count;
309}
310
311/*
312 * LED Brightness (Global)
313*/
314static int wmax_brightness(int brightness)
315{
316 acpi_status status;
317 struct acpi_buffer input;
318 struct wmax_brightness_args args = {
319 .led_mask = 0xFF,
320 .percentage = brightness,
321 };
322 input.length = (acpi_size) sizeof(args);
323 input.pointer = &args;
324 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
325 WMAX_METHOD_BRIGHTNESS, &input, NULL);
326 if (ACPI_FAILURE(status))
327 pr_err("alienware-wmi: brightness set failure: %u\n", status);
328 return ACPI_FAILURE(status);
329}
330
331static void global_led_set(struct led_classdev *led_cdev,
332 enum led_brightness brightness)
333{
334 int ret;
335 global_brightness = brightness;
336 if (interface == WMAX)
337 ret = wmax_brightness(brightness);
338 else
339 ret = alienware_update_led(&zone_data[0]);
340 if (ret)
341 pr_err("LED brightness update failed\n");
342}
343
344static enum led_brightness global_led_get(struct led_classdev *led_cdev)
345{
346 return global_brightness;
347}
348
349static struct led_classdev global_led = {
350 .brightness_set = global_led_set,
351 .brightness_get = global_led_get,
352 .name = "alienware::global_brightness",
353};
354
355/*
356 * Lighting control state device attribute (Global)
357*/
358static ssize_t show_control_state(struct device *dev,
359 struct device_attribute *attr, char *buf)
360{
361 if (lighting_control_state == LEGACY_BOOTING)
362 return scnprintf(buf, PAGE_SIZE, "[booting] running suspend\n");
363 else if (lighting_control_state == LEGACY_SUSPEND)
364 return scnprintf(buf, PAGE_SIZE, "booting running [suspend]\n");
365 return scnprintf(buf, PAGE_SIZE, "booting [running] suspend\n");
366}
367
368static ssize_t store_control_state(struct device *dev,
369 struct device_attribute *attr,
370 const char *buf, size_t count)
371{
372 long unsigned int val;
373 if (strcmp(buf, "booting\n") == 0)
374 val = LEGACY_BOOTING;
375 else if (strcmp(buf, "suspend\n") == 0)
376 val = LEGACY_SUSPEND;
377 else if (interface == LEGACY)
378 val = LEGACY_RUNNING;
379 else
380 val = WMAX_RUNNING;
381 lighting_control_state = val;
382 pr_debug("alienware-wmi: updated control state to %d\n",
383 lighting_control_state);
384 return count;
385}
386
387static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
388 store_control_state);
389
390static int alienware_zone_init(struct platform_device *dev)
391{
392 int i;
393 char buffer[10];
394 char *name;
395
396 if (interface == WMAX) {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400397 lighting_control_state = WMAX_RUNNING;
398 } else if (interface == LEGACY) {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400399 lighting_control_state = LEGACY_RUNNING;
400 }
Mario Limonciellob9986802014-05-07 15:08:09 -0500401 global_led.max_brightness = 0x0F;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400402 global_brightness = global_led.max_brightness;
403
404 /*
405 * - zone_dev_attrs num_zones + 1 is for individual zones and then
406 * null terminated
407 * - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
408 * the lighting control + null terminated
409 * - zone_data num_zones is for the distinct zones
410 */
411 zone_dev_attrs =
412 kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
413 GFP_KERNEL);
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500414 if (!zone_dev_attrs)
415 return -ENOMEM;
416
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400417 zone_attrs =
418 kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
419 GFP_KERNEL);
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500420 if (!zone_attrs)
421 return -ENOMEM;
422
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400423 zone_data =
424 kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
425 GFP_KERNEL);
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500426 if (!zone_data)
427 return -ENOMEM;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400428
429 for (i = 0; i < quirks->num_zones; i++) {
430 sprintf(buffer, "zone%02X", i);
431 name = kstrdup(buffer, GFP_KERNEL);
432 if (name == NULL)
433 return 1;
434 sysfs_attr_init(&zone_dev_attrs[i].attr);
435 zone_dev_attrs[i].attr.name = name;
436 zone_dev_attrs[i].attr.mode = 0644;
437 zone_dev_attrs[i].show = zone_show;
438 zone_dev_attrs[i].store = zone_set;
439 zone_data[i].location = i;
440 zone_attrs[i] = &zone_dev_attrs[i].attr;
441 zone_data[i].attr = &zone_dev_attrs[i];
442 }
443 zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
444 zone_attribute_group.attrs = zone_attrs;
445
446 led_classdev_register(&dev->dev, &global_led);
447
448 return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
449}
450
451static void alienware_zone_exit(struct platform_device *dev)
452{
453 sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
454 led_classdev_unregister(&global_led);
455 if (zone_dev_attrs) {
456 int i;
457 for (i = 0; i < quirks->num_zones; i++)
458 kfree(zone_dev_attrs[i].attr.name);
459 }
460 kfree(zone_dev_attrs);
461 kfree(zone_data);
462 kfree(zone_attrs);
463}
464
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600465static acpi_status alienware_wmax_command(struct wmax_basic_args *in_args,
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500466 u32 command, int *out_data)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400467{
468 acpi_status status;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400469 union acpi_object *obj;
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500470 struct acpi_buffer input;
471 struct acpi_buffer output;
472
473 input.length = (acpi_size) sizeof(*in_args);
474 input.pointer = in_args;
475 if (out_data != NULL) {
476 output.length = ACPI_ALLOCATE_BUFFER;
477 output.pointer = NULL;
478 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
479 command, &input, &output);
480 } else
481 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
482 command, &input, NULL);
483
484 if (ACPI_SUCCESS(status) && out_data != NULL) {
485 obj = (union acpi_object *)output.pointer;
486 if (obj && obj->type == ACPI_TYPE_INTEGER)
487 *out_data = (u32) obj->integer.value;
488 }
489 return status;
490
491}
492
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600493/*
494 * The HDMI mux sysfs node indicates the status of the HDMI input mux.
495 * It can toggle between standard system GPU output and HDMI input.
496 */
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500497static ssize_t show_hdmi_cable(struct device *dev,
498 struct device_attribute *attr, char *buf)
499{
500 acpi_status status;
501 u32 out_data;
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600502 struct wmax_basic_args in_args = {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400503 .arg = 0,
504 };
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500505 status =
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600506 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_CABLE,
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500507 (u32 *) &out_data);
508 if (ACPI_SUCCESS(status)) {
509 if (out_data == 0)
510 return scnprintf(buf, PAGE_SIZE,
511 "[unconnected] connected unknown\n");
512 else if (out_data == 1)
513 return scnprintf(buf, PAGE_SIZE,
514 "unconnected [connected] unknown\n");
515 }
516 pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
517 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
518}
519
520static ssize_t show_hdmi_source(struct device *dev,
521 struct device_attribute *attr, char *buf)
522{
523 acpi_status status;
524 u32 out_data;
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600525 struct wmax_basic_args in_args = {
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500526 .arg = 0,
527 };
528 status =
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600529 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_STATUS,
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500530 (u32 *) &out_data);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400531
532 if (ACPI_SUCCESS(status)) {
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500533 if (out_data == 1)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400534 return scnprintf(buf, PAGE_SIZE,
535 "[input] gpu unknown\n");
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500536 else if (out_data == 2)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400537 return scnprintf(buf, PAGE_SIZE,
538 "input [gpu] unknown\n");
539 }
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500540 pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400541 return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
542}
543
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500544static ssize_t toggle_hdmi_source(struct device *dev,
545 struct device_attribute *attr,
546 const char *buf, size_t count)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400547{
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400548 acpi_status status;
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600549 struct wmax_basic_args args;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400550 if (strcmp(buf, "gpu\n") == 0)
551 args.arg = 1;
552 else if (strcmp(buf, "input\n") == 0)
553 args.arg = 2;
554 else
555 args.arg = 3;
556 pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500557
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600558 status = alienware_wmax_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500559
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400560 if (ACPI_FAILURE(status))
561 pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
562 status);
563 return count;
564}
565
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500566static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
567static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
568 toggle_hdmi_source);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400569
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500570static struct attribute *hdmi_attrs[] = {
571 &dev_attr_cable.attr,
572 &dev_attr_source.attr,
573 NULL,
574};
575
576static struct attribute_group hdmi_attribute_group = {
577 .name = "hdmi",
578 .attrs = hdmi_attrs,
579};
580
581static void remove_hdmi(struct platform_device *dev)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400582{
Mario Limonciellofee4efd2014-07-23 23:19:23 -0500583 if (quirks->hdmi_mux > 0)
584 sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400585}
586
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500587static int create_hdmi(struct platform_device *dev)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400588{
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500589 int ret;
590
591 ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400592 if (ret)
593 goto error_create_hdmi;
594 return 0;
595
596error_create_hdmi:
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500597 remove_hdmi(dev);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400598 return ret;
599}
600
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600601/*
602 * Alienware GFX amplifier support
603 * - Currently supports reading cable status
604 * - Leaving expansion room to possibly support dock/undock events later
605 */
606static ssize_t show_amplifier_status(struct device *dev,
607 struct device_attribute *attr, char *buf)
608{
609 acpi_status status;
610 u32 out_data;
611 struct wmax_basic_args in_args = {
612 .arg = 0,
613 };
614 status =
615 alienware_wmax_command(&in_args, WMAX_METHOD_AMPLIFIER_CABLE,
616 (u32 *) &out_data);
617 if (ACPI_SUCCESS(status)) {
618 if (out_data == 0)
619 return scnprintf(buf, PAGE_SIZE,
620 "[unconnected] connected unknown\n");
621 else if (out_data == 1)
622 return scnprintf(buf, PAGE_SIZE,
623 "unconnected [connected] unknown\n");
624 }
625 pr_err("alienware-wmi: unknown amplifier cable status: %d\n", status);
626 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
627}
628
629static DEVICE_ATTR(status, S_IRUGO, show_amplifier_status, NULL);
630
631static struct attribute *amplifier_attrs[] = {
632 &dev_attr_status.attr,
633 NULL,
634};
635
636static struct attribute_group amplifier_attribute_group = {
637 .name = "amplifier",
638 .attrs = amplifier_attrs,
639};
640
641static void remove_amplifier(struct platform_device *dev)
642{
643 if (quirks->amplifier > 0)
644 sysfs_remove_group(&dev->dev.kobj, &amplifier_attribute_group);
645}
646
647static int create_amplifier(struct platform_device *dev)
648{
649 int ret;
650
651 ret = sysfs_create_group(&dev->dev.kobj, &amplifier_attribute_group);
652 if (ret)
653 remove_amplifier(dev);
654 return ret;
655}
656
Mario Limonciello8ea81ec2016-02-02 15:38:55 -0600657/*
658 * Deep Sleep Control support
659 * - Modifies BIOS setting for deep sleep control allowing extra wakeup events
660 */
661static ssize_t show_deepsleep_status(struct device *dev,
662 struct device_attribute *attr, char *buf)
663{
664 acpi_status status;
665 u32 out_data;
666 struct wmax_basic_args in_args = {
667 .arg = 0,
668 };
669 status = alienware_wmax_command(&in_args, WMAX_METHOD_DEEP_SLEEP_STATUS,
670 (u32 *) &out_data);
671 if (ACPI_SUCCESS(status)) {
672 if (out_data == 0)
673 return scnprintf(buf, PAGE_SIZE,
674 "[disabled] s5 s5_s4\n");
675 else if (out_data == 1)
676 return scnprintf(buf, PAGE_SIZE,
677 "disabled [s5] s5_s4\n");
678 else if (out_data == 2)
679 return scnprintf(buf, PAGE_SIZE,
680 "disabled s5 [s5_s4]\n");
681 }
682 pr_err("alienware-wmi: unknown deep sleep status: %d\n", status);
683 return scnprintf(buf, PAGE_SIZE, "disabled s5 s5_s4 [unknown]\n");
684}
685
686static ssize_t toggle_deepsleep(struct device *dev,
687 struct device_attribute *attr,
688 const char *buf, size_t count)
689{
690 acpi_status status;
691 struct wmax_basic_args args;
692
693 if (strcmp(buf, "disabled\n") == 0)
694 args.arg = 0;
695 else if (strcmp(buf, "s5\n") == 0)
696 args.arg = 1;
697 else
698 args.arg = 2;
699 pr_debug("alienware-wmi: setting deep sleep to %d : %s", args.arg, buf);
700
701 status = alienware_wmax_command(&args, WMAX_METHOD_DEEP_SLEEP_CONTROL,
702 NULL);
703
704 if (ACPI_FAILURE(status))
705 pr_err("alienware-wmi: deep sleep control failed: results: %u\n",
706 status);
707 return count;
708}
709
710static DEVICE_ATTR(deepsleep, S_IRUGO | S_IWUSR, show_deepsleep_status, toggle_deepsleep);
711
712static struct attribute *deepsleep_attrs[] = {
713 &dev_attr_deepsleep.attr,
714 NULL,
715};
716
717static struct attribute_group deepsleep_attribute_group = {
718 .name = "deepsleep",
719 .attrs = deepsleep_attrs,
720};
721
722static void remove_deepsleep(struct platform_device *dev)
723{
724 if (quirks->deepslp > 0)
725 sysfs_remove_group(&dev->dev.kobj, &deepsleep_attribute_group);
726}
727
728static int create_deepsleep(struct platform_device *dev)
729{
730 int ret;
731
732 ret = sysfs_create_group(&dev->dev.kobj, &deepsleep_attribute_group);
733 if (ret)
734 remove_deepsleep(dev);
735 return ret;
736}
737
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400738static int __init alienware_wmi_init(void)
739{
740 int ret;
741
742 if (wmi_has_guid(LEGACY_CONTROL_GUID))
743 interface = LEGACY;
744 else if (wmi_has_guid(WMAX_CONTROL_GUID))
745 interface = WMAX;
746 else {
747 pr_warn("alienware-wmi: No known WMI GUID found\n");
748 return -ENODEV;
749 }
750
751 dmi_check_system(alienware_quirks);
752 if (quirks == NULL)
753 quirks = &quirk_unknown;
754
755 ret = platform_driver_register(&platform_driver);
756 if (ret)
757 goto fail_platform_driver;
758 platform_device = platform_device_alloc("alienware-wmi", -1);
759 if (!platform_device) {
760 ret = -ENOMEM;
761 goto fail_platform_device1;
762 }
763 ret = platform_device_add(platform_device);
764 if (ret)
765 goto fail_platform_device2;
766
Mario Limonciellofee4efd2014-07-23 23:19:23 -0500767 if (quirks->hdmi_mux > 0) {
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500768 ret = create_hdmi(platform_device);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400769 if (ret)
770 goto fail_prep_hdmi;
771 }
772
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600773 if (quirks->amplifier > 0) {
774 ret = create_amplifier(platform_device);
775 if (ret)
776 goto fail_prep_amplifier;
777 }
778
Mario Limonciello8ea81ec2016-02-02 15:38:55 -0600779 if (quirks->deepslp > 0) {
780 ret = create_deepsleep(platform_device);
781 if (ret)
782 goto fail_prep_deepsleep;
783 }
784
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400785 ret = alienware_zone_init(platform_device);
786 if (ret)
787 goto fail_prep_zones;
788
789 return 0;
790
791fail_prep_zones:
792 alienware_zone_exit(platform_device);
Mario Limonciello8ea81ec2016-02-02 15:38:55 -0600793fail_prep_deepsleep:
Mario Limonciellocbbb50d62016-02-02 15:38:54 -0600794fail_prep_amplifier:
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400795fail_prep_hdmi:
796 platform_device_del(platform_device);
797fail_platform_device2:
798 platform_device_put(platform_device);
799fail_platform_device1:
800 platform_driver_unregister(&platform_driver);
801fail_platform_driver:
802 return ret;
803}
804
805module_init(alienware_wmi_init);
806
807static void __exit alienware_wmi_exit(void)
808{
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400809 if (platform_device) {
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500810 alienware_zone_exit(platform_device);
811 remove_hdmi(platform_device);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400812 platform_device_unregister(platform_device);
813 platform_driver_unregister(&platform_driver);
814 }
815}
816
817module_exit(alienware_wmi_exit);