blob: fa86b7d5d6b5f1d3d92bc08a73c6a5be6820f88b [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 Limonciellocbbb50d2016-02-02 15:38:54 -060036#define WMAX_METHOD_AMPLIFIER_CABLE 0x6
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040037
38MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>");
39MODULE_DESCRIPTION("Alienware special feature control");
40MODULE_LICENSE("GPL");
41MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID);
42MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID);
43
44enum INTERFACE_FLAGS {
45 LEGACY,
46 WMAX,
47};
48
49enum LEGACY_CONTROL_STATES {
50 LEGACY_RUNNING = 1,
51 LEGACY_BOOTING = 0,
52 LEGACY_SUSPEND = 3,
53};
54
55enum WMAX_CONTROL_STATES {
56 WMAX_RUNNING = 0xFF,
57 WMAX_BOOTING = 0,
58 WMAX_SUSPEND = 3,
59};
60
61struct quirk_entry {
62 u8 num_zones;
Mario Limonciellofee4efd2014-07-23 23:19:23 -050063 u8 hdmi_mux;
Mario Limonciellocbbb50d2016-02-02 15:38:54 -060064 u8 amplifier;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040065};
66
67static struct quirk_entry *quirks;
68
69static struct quirk_entry quirk_unknown = {
70 .num_zones = 2,
Mario Limonciellofee4efd2014-07-23 23:19:23 -050071 .hdmi_mux = 0,
Mario Limonciellocbbb50d2016-02-02 15:38:54 -060072 .amplifier = 0,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040073};
74
Mario Limonciello9e503a92016-02-02 15:38:53 -060075static struct quirk_entry quirk_x51_r1_r2 = {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040076 .num_zones = 3,
Mario Limonciellocbbb50d2016-02-02 15:38:54 -060077 .hdmi_mux = 0,
78 .amplifier = 0,
Mario Limonciellofee4efd2014-07-23 23:19:23 -050079};
80
Mario Limonciello9e503a92016-02-02 15:38:53 -060081static struct quirk_entry quirk_x51_r3 = {
82 .num_zones = 4,
83 .hdmi_mux = 0,
Mario Limonciellocbbb50d2016-02-02 15:38:54 -060084 .amplifier = 1,
Mario Limonciello9e503a92016-02-02 15:38:53 -060085};
86
Mario Limonciellofee4efd2014-07-23 23:19:23 -050087static struct quirk_entry quirk_asm100 = {
88 .num_zones = 2,
89 .hdmi_mux = 1,
Mario Limonciellocbbb50d2016-02-02 15:38:54 -060090 .amplifier = 0,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040091};
92
Mathias Kraused997d882014-07-16 19:43:07 +020093static int __init dmi_matched(const struct dmi_system_id *dmi)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -040094{
95 quirks = dmi->driver_data;
96 return 1;
97}
98
Mathias Kraused997d882014-07-16 19:43:07 +020099static const struct dmi_system_id alienware_quirks[] __initconst = {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400100 {
101 .callback = dmi_matched,
Mario Limonciello9e503a92016-02-02 15:38:53 -0600102 .ident = "Alienware X51 R3",
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400103 .matches = {
104 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
Mario Limonciello9e503a92016-02-02 15:38:53 -0600105 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R3"),
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400106 },
Mario Limonciello9e503a92016-02-02 15:38:53 -0600107 .driver_data = &quirk_x51_r3,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400108 },
109 {
110 .callback = dmi_matched,
111 .ident = "Alienware X51 R2",
112 .matches = {
113 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
114 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
115 },
Mario Limonciello9e503a92016-02-02 15:38:53 -0600116 .driver_data = &quirk_x51_r1_r2,
117 },
118 {
119 .callback = dmi_matched,
120 .ident = "Alienware X51 R1",
121 .matches = {
122 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
123 DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
124 },
125 .driver_data = &quirk_x51_r1_r2,
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400126 },
Mario Limonciellofee4efd2014-07-23 23:19:23 -0500127 {
Mario Limonciello66ad0dd2016-02-02 15:38:52 -0600128 .callback = dmi_matched,
129 .ident = "Alienware ASM100",
130 .matches = {
131 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
132 DMI_MATCH(DMI_PRODUCT_NAME, "ASM100"),
133 },
134 .driver_data = &quirk_asm100,
135 },
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400136 {}
137};
138
139struct color_platform {
140 u8 blue;
141 u8 green;
142 u8 red;
143} __packed;
144
145struct platform_zone {
146 u8 location;
147 struct device_attribute *attr;
148 struct color_platform colors;
149};
150
151struct wmax_brightness_args {
152 u32 led_mask;
153 u32 percentage;
154};
155
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600156struct wmax_basic_args {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400157 u8 arg;
158};
159
160struct legacy_led_args {
161 struct color_platform colors;
162 u8 brightness;
163 u8 state;
164} __packed;
165
166struct wmax_led_args {
167 u32 led_mask;
168 struct color_platform colors;
169 u8 state;
170} __packed;
171
172static struct platform_device *platform_device;
173static struct device_attribute *zone_dev_attrs;
174static struct attribute **zone_attrs;
175static struct platform_zone *zone_data;
176
177static struct platform_driver platform_driver = {
178 .driver = {
179 .name = "alienware-wmi",
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400180 }
181};
182
183static struct attribute_group zone_attribute_group = {
184 .name = "rgb_zones",
185};
186
187static u8 interface;
188static u8 lighting_control_state;
189static u8 global_brightness;
190
191/*
192 * Helpers used for zone control
193*/
194static int parse_rgb(const char *buf, struct platform_zone *zone)
195{
196 long unsigned int rgb;
197 int ret;
198 union color_union {
199 struct color_platform cp;
200 int package;
201 } repackager;
202
203 ret = kstrtoul(buf, 16, &rgb);
204 if (ret)
205 return ret;
206
207 /* RGB triplet notation is 24-bit hexadecimal */
208 if (rgb > 0xFFFFFF)
209 return -EINVAL;
210
211 repackager.package = rgb & 0x0f0f0f0f;
212 pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
213 repackager.cp.red, repackager.cp.green, repackager.cp.blue);
214 zone->colors = repackager.cp;
215 return 0;
216}
217
218static struct platform_zone *match_zone(struct device_attribute *attr)
219{
220 int i;
221 for (i = 0; i < quirks->num_zones; i++) {
222 if ((struct device_attribute *)zone_data[i].attr == attr) {
223 pr_debug("alienware-wmi: matched zone location: %d\n",
224 zone_data[i].location);
225 return &zone_data[i];
226 }
227 }
228 return NULL;
229}
230
231/*
232 * Individual RGB zone control
233*/
234static int alienware_update_led(struct platform_zone *zone)
235{
236 int method_id;
237 acpi_status status;
238 char *guid;
239 struct acpi_buffer input;
240 struct legacy_led_args legacy_args;
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600241 struct wmax_led_args wmax_basic_args;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400242 if (interface == WMAX) {
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600243 wmax_basic_args.led_mask = 1 << zone->location;
244 wmax_basic_args.colors = zone->colors;
245 wmax_basic_args.state = lighting_control_state;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400246 guid = WMAX_CONTROL_GUID;
247 method_id = WMAX_METHOD_ZONE_CONTROL;
248
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600249 input.length = (acpi_size) sizeof(wmax_basic_args);
250 input.pointer = &wmax_basic_args;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400251 } else {
252 legacy_args.colors = zone->colors;
253 legacy_args.brightness = global_brightness;
254 legacy_args.state = 0;
255 if (lighting_control_state == LEGACY_BOOTING ||
256 lighting_control_state == LEGACY_SUSPEND) {
257 guid = LEGACY_POWER_CONTROL_GUID;
258 legacy_args.state = lighting_control_state;
259 } else
260 guid = LEGACY_CONTROL_GUID;
261 method_id = zone->location + 1;
262
263 input.length = (acpi_size) sizeof(legacy_args);
264 input.pointer = &legacy_args;
265 }
266 pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
267
268 status = wmi_evaluate_method(guid, 1, method_id, &input, NULL);
269 if (ACPI_FAILURE(status))
270 pr_err("alienware-wmi: zone set failure: %u\n", status);
271 return ACPI_FAILURE(status);
272}
273
274static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
275 char *buf)
276{
277 struct platform_zone *target_zone;
278 target_zone = match_zone(attr);
279 if (target_zone == NULL)
280 return sprintf(buf, "red: -1, green: -1, blue: -1\n");
281 return sprintf(buf, "red: %d, green: %d, blue: %d\n",
282 target_zone->colors.red,
283 target_zone->colors.green, target_zone->colors.blue);
284
285}
286
287static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
288 const char *buf, size_t count)
289{
290 struct platform_zone *target_zone;
291 int ret;
292 target_zone = match_zone(attr);
293 if (target_zone == NULL) {
294 pr_err("alienware-wmi: invalid target zone\n");
295 return 1;
296 }
297 ret = parse_rgb(buf, target_zone);
298 if (ret)
299 return ret;
300 ret = alienware_update_led(target_zone);
301 return ret ? ret : count;
302}
303
304/*
305 * LED Brightness (Global)
306*/
307static int wmax_brightness(int brightness)
308{
309 acpi_status status;
310 struct acpi_buffer input;
311 struct wmax_brightness_args args = {
312 .led_mask = 0xFF,
313 .percentage = brightness,
314 };
315 input.length = (acpi_size) sizeof(args);
316 input.pointer = &args;
317 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
318 WMAX_METHOD_BRIGHTNESS, &input, NULL);
319 if (ACPI_FAILURE(status))
320 pr_err("alienware-wmi: brightness set failure: %u\n", status);
321 return ACPI_FAILURE(status);
322}
323
324static void global_led_set(struct led_classdev *led_cdev,
325 enum led_brightness brightness)
326{
327 int ret;
328 global_brightness = brightness;
329 if (interface == WMAX)
330 ret = wmax_brightness(brightness);
331 else
332 ret = alienware_update_led(&zone_data[0]);
333 if (ret)
334 pr_err("LED brightness update failed\n");
335}
336
337static enum led_brightness global_led_get(struct led_classdev *led_cdev)
338{
339 return global_brightness;
340}
341
342static struct led_classdev global_led = {
343 .brightness_set = global_led_set,
344 .brightness_get = global_led_get,
345 .name = "alienware::global_brightness",
346};
347
348/*
349 * Lighting control state device attribute (Global)
350*/
351static ssize_t show_control_state(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
354 if (lighting_control_state == LEGACY_BOOTING)
355 return scnprintf(buf, PAGE_SIZE, "[booting] running suspend\n");
356 else if (lighting_control_state == LEGACY_SUSPEND)
357 return scnprintf(buf, PAGE_SIZE, "booting running [suspend]\n");
358 return scnprintf(buf, PAGE_SIZE, "booting [running] suspend\n");
359}
360
361static ssize_t store_control_state(struct device *dev,
362 struct device_attribute *attr,
363 const char *buf, size_t count)
364{
365 long unsigned int val;
366 if (strcmp(buf, "booting\n") == 0)
367 val = LEGACY_BOOTING;
368 else if (strcmp(buf, "suspend\n") == 0)
369 val = LEGACY_SUSPEND;
370 else if (interface == LEGACY)
371 val = LEGACY_RUNNING;
372 else
373 val = WMAX_RUNNING;
374 lighting_control_state = val;
375 pr_debug("alienware-wmi: updated control state to %d\n",
376 lighting_control_state);
377 return count;
378}
379
380static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
381 store_control_state);
382
383static int alienware_zone_init(struct platform_device *dev)
384{
385 int i;
386 char buffer[10];
387 char *name;
388
389 if (interface == WMAX) {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400390 lighting_control_state = WMAX_RUNNING;
391 } else if (interface == LEGACY) {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400392 lighting_control_state = LEGACY_RUNNING;
393 }
Mario Limonciellob9986802014-05-07 15:08:09 -0500394 global_led.max_brightness = 0x0F;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400395 global_brightness = global_led.max_brightness;
396
397 /*
398 * - zone_dev_attrs num_zones + 1 is for individual zones and then
399 * null terminated
400 * - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
401 * the lighting control + null terminated
402 * - zone_data num_zones is for the distinct zones
403 */
404 zone_dev_attrs =
405 kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
406 GFP_KERNEL);
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500407 if (!zone_dev_attrs)
408 return -ENOMEM;
409
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400410 zone_attrs =
411 kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
412 GFP_KERNEL);
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500413 if (!zone_attrs)
414 return -ENOMEM;
415
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400416 zone_data =
417 kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
418 GFP_KERNEL);
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500419 if (!zone_data)
420 return -ENOMEM;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400421
422 for (i = 0; i < quirks->num_zones; i++) {
423 sprintf(buffer, "zone%02X", i);
424 name = kstrdup(buffer, GFP_KERNEL);
425 if (name == NULL)
426 return 1;
427 sysfs_attr_init(&zone_dev_attrs[i].attr);
428 zone_dev_attrs[i].attr.name = name;
429 zone_dev_attrs[i].attr.mode = 0644;
430 zone_dev_attrs[i].show = zone_show;
431 zone_dev_attrs[i].store = zone_set;
432 zone_data[i].location = i;
433 zone_attrs[i] = &zone_dev_attrs[i].attr;
434 zone_data[i].attr = &zone_dev_attrs[i];
435 }
436 zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
437 zone_attribute_group.attrs = zone_attrs;
438
439 led_classdev_register(&dev->dev, &global_led);
440
441 return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
442}
443
444static void alienware_zone_exit(struct platform_device *dev)
445{
446 sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
447 led_classdev_unregister(&global_led);
448 if (zone_dev_attrs) {
449 int i;
450 for (i = 0; i < quirks->num_zones; i++)
451 kfree(zone_dev_attrs[i].attr.name);
452 }
453 kfree(zone_dev_attrs);
454 kfree(zone_data);
455 kfree(zone_attrs);
456}
457
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600458static acpi_status alienware_wmax_command(struct wmax_basic_args *in_args,
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500459 u32 command, int *out_data)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400460{
461 acpi_status status;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400462 union acpi_object *obj;
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500463 struct acpi_buffer input;
464 struct acpi_buffer output;
465
466 input.length = (acpi_size) sizeof(*in_args);
467 input.pointer = in_args;
468 if (out_data != NULL) {
469 output.length = ACPI_ALLOCATE_BUFFER;
470 output.pointer = NULL;
471 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
472 command, &input, &output);
473 } else
474 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
475 command, &input, NULL);
476
477 if (ACPI_SUCCESS(status) && out_data != NULL) {
478 obj = (union acpi_object *)output.pointer;
479 if (obj && obj->type == ACPI_TYPE_INTEGER)
480 *out_data = (u32) obj->integer.value;
481 }
482 return status;
483
484}
485
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600486/*
487 * The HDMI mux sysfs node indicates the status of the HDMI input mux.
488 * It can toggle between standard system GPU output and HDMI input.
489 */
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500490static ssize_t show_hdmi_cable(struct device *dev,
491 struct device_attribute *attr, char *buf)
492{
493 acpi_status status;
494 u32 out_data;
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600495 struct wmax_basic_args in_args = {
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400496 .arg = 0,
497 };
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500498 status =
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600499 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_CABLE,
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500500 (u32 *) &out_data);
501 if (ACPI_SUCCESS(status)) {
502 if (out_data == 0)
503 return scnprintf(buf, PAGE_SIZE,
504 "[unconnected] connected unknown\n");
505 else if (out_data == 1)
506 return scnprintf(buf, PAGE_SIZE,
507 "unconnected [connected] unknown\n");
508 }
509 pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
510 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
511}
512
513static ssize_t show_hdmi_source(struct device *dev,
514 struct device_attribute *attr, char *buf)
515{
516 acpi_status status;
517 u32 out_data;
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600518 struct wmax_basic_args in_args = {
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500519 .arg = 0,
520 };
521 status =
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600522 alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_STATUS,
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500523 (u32 *) &out_data);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400524
525 if (ACPI_SUCCESS(status)) {
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500526 if (out_data == 1)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400527 return scnprintf(buf, PAGE_SIZE,
528 "[input] gpu unknown\n");
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500529 else if (out_data == 2)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400530 return scnprintf(buf, PAGE_SIZE,
531 "input [gpu] unknown\n");
532 }
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500533 pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400534 return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
535}
536
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500537static ssize_t toggle_hdmi_source(struct device *dev,
538 struct device_attribute *attr,
539 const char *buf, size_t count)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400540{
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400541 acpi_status status;
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600542 struct wmax_basic_args args;
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400543 if (strcmp(buf, "gpu\n") == 0)
544 args.arg = 1;
545 else if (strcmp(buf, "input\n") == 0)
546 args.arg = 2;
547 else
548 args.arg = 3;
549 pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500550
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600551 status = alienware_wmax_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500552
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400553 if (ACPI_FAILURE(status))
554 pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
555 status);
556 return count;
557}
558
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500559static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
560static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
561 toggle_hdmi_source);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400562
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500563static struct attribute *hdmi_attrs[] = {
564 &dev_attr_cable.attr,
565 &dev_attr_source.attr,
566 NULL,
567};
568
569static struct attribute_group hdmi_attribute_group = {
570 .name = "hdmi",
571 .attrs = hdmi_attrs,
572};
573
574static void remove_hdmi(struct platform_device *dev)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400575{
Mario Limonciellofee4efd2014-07-23 23:19:23 -0500576 if (quirks->hdmi_mux > 0)
577 sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400578}
579
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500580static int create_hdmi(struct platform_device *dev)
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400581{
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500582 int ret;
583
584 ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400585 if (ret)
586 goto error_create_hdmi;
587 return 0;
588
589error_create_hdmi:
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500590 remove_hdmi(dev);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400591 return ret;
592}
593
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600594/*
595 * Alienware GFX amplifier support
596 * - Currently supports reading cable status
597 * - Leaving expansion room to possibly support dock/undock events later
598 */
599static ssize_t show_amplifier_status(struct device *dev,
600 struct device_attribute *attr, char *buf)
601{
602 acpi_status status;
603 u32 out_data;
604 struct wmax_basic_args in_args = {
605 .arg = 0,
606 };
607 status =
608 alienware_wmax_command(&in_args, WMAX_METHOD_AMPLIFIER_CABLE,
609 (u32 *) &out_data);
610 if (ACPI_SUCCESS(status)) {
611 if (out_data == 0)
612 return scnprintf(buf, PAGE_SIZE,
613 "[unconnected] connected unknown\n");
614 else if (out_data == 1)
615 return scnprintf(buf, PAGE_SIZE,
616 "unconnected [connected] unknown\n");
617 }
618 pr_err("alienware-wmi: unknown amplifier cable status: %d\n", status);
619 return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
620}
621
622static DEVICE_ATTR(status, S_IRUGO, show_amplifier_status, NULL);
623
624static struct attribute *amplifier_attrs[] = {
625 &dev_attr_status.attr,
626 NULL,
627};
628
629static struct attribute_group amplifier_attribute_group = {
630 .name = "amplifier",
631 .attrs = amplifier_attrs,
632};
633
634static void remove_amplifier(struct platform_device *dev)
635{
636 if (quirks->amplifier > 0)
637 sysfs_remove_group(&dev->dev.kobj, &amplifier_attribute_group);
638}
639
640static int create_amplifier(struct platform_device *dev)
641{
642 int ret;
643
644 ret = sysfs_create_group(&dev->dev.kobj, &amplifier_attribute_group);
645 if (ret)
646 remove_amplifier(dev);
647 return ret;
648}
649
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400650static int __init alienware_wmi_init(void)
651{
652 int ret;
653
654 if (wmi_has_guid(LEGACY_CONTROL_GUID))
655 interface = LEGACY;
656 else if (wmi_has_guid(WMAX_CONTROL_GUID))
657 interface = WMAX;
658 else {
659 pr_warn("alienware-wmi: No known WMI GUID found\n");
660 return -ENODEV;
661 }
662
663 dmi_check_system(alienware_quirks);
664 if (quirks == NULL)
665 quirks = &quirk_unknown;
666
667 ret = platform_driver_register(&platform_driver);
668 if (ret)
669 goto fail_platform_driver;
670 platform_device = platform_device_alloc("alienware-wmi", -1);
671 if (!platform_device) {
672 ret = -ENOMEM;
673 goto fail_platform_device1;
674 }
675 ret = platform_device_add(platform_device);
676 if (ret)
677 goto fail_platform_device2;
678
Mario Limonciellofee4efd2014-07-23 23:19:23 -0500679 if (quirks->hdmi_mux > 0) {
Mario Limonciellobc2ef882014-05-07 15:08:10 -0500680 ret = create_hdmi(platform_device);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400681 if (ret)
682 goto fail_prep_hdmi;
683 }
684
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600685 if (quirks->amplifier > 0) {
686 ret = create_amplifier(platform_device);
687 if (ret)
688 goto fail_prep_amplifier;
689 }
690
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400691 ret = alienware_zone_init(platform_device);
692 if (ret)
693 goto fail_prep_zones;
694
695 return 0;
696
697fail_prep_zones:
698 alienware_zone_exit(platform_device);
Mario Limonciellocbbb50d2016-02-02 15:38:54 -0600699fail_prep_amplifier:
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400700fail_prep_hdmi:
701 platform_device_del(platform_device);
702fail_platform_device2:
703 platform_device_put(platform_device);
704fail_platform_device1:
705 platform_driver_unregister(&platform_driver);
706fail_platform_driver:
707 return ret;
708}
709
710module_init(alienware_wmi_init);
711
712static void __exit alienware_wmi_exit(void)
713{
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400714 if (platform_device) {
Mario Limonciello562c7ce2014-04-04 14:40:20 -0500715 alienware_zone_exit(platform_device);
716 remove_hdmi(platform_device);
Mario Limoncielloa46ad0f2014-04-04 14:15:42 -0400717 platform_device_unregister(platform_device);
718 platform_driver_unregister(&platform_driver);
719 }
720}
721
722module_exit(alienware_wmi_exit);