Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[cascardo/linux.git] / drivers / platform / x86 / alienware-wmi.c
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>
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
35 #define WMAX_METHOD_HDMI_CABLE          0x5
36
37 MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>");
38 MODULE_DESCRIPTION("Alienware special feature control");
39 MODULE_LICENSE("GPL");
40 MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID);
41 MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID);
42
43 enum INTERFACE_FLAGS {
44         LEGACY,
45         WMAX,
46 };
47
48 enum LEGACY_CONTROL_STATES {
49         LEGACY_RUNNING = 1,
50         LEGACY_BOOTING = 0,
51         LEGACY_SUSPEND = 3,
52 };
53
54 enum WMAX_CONTROL_STATES {
55         WMAX_RUNNING = 0xFF,
56         WMAX_BOOTING = 0,
57         WMAX_SUSPEND = 3,
58 };
59
60 struct quirk_entry {
61         u8 num_zones;
62 };
63
64 static struct quirk_entry *quirks;
65
66 static struct quirk_entry quirk_unknown = {
67         .num_zones = 2,
68 };
69
70 static struct quirk_entry quirk_x51_family = {
71         .num_zones = 3,
72 };
73
74 static int dmi_matched(const struct dmi_system_id *dmi)
75 {
76         quirks = dmi->driver_data;
77         return 1;
78 }
79
80 static struct dmi_system_id alienware_quirks[] = {
81         {
82          .callback = dmi_matched,
83          .ident = "Alienware X51 R1",
84          .matches = {
85                      DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
86                      DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51"),
87                      },
88          .driver_data = &quirk_x51_family,
89          },
90         {
91          .callback = dmi_matched,
92          .ident = "Alienware X51 R2",
93          .matches = {
94                      DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
95                      DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R2"),
96                      },
97          .driver_data = &quirk_x51_family,
98          },
99         {}
100 };
101
102 struct color_platform {
103         u8 blue;
104         u8 green;
105         u8 red;
106 } __packed;
107
108 struct platform_zone {
109         u8 location;
110         struct device_attribute *attr;
111         struct color_platform colors;
112 };
113
114 struct wmax_brightness_args {
115         u32 led_mask;
116         u32 percentage;
117 };
118
119 struct hdmi_args {
120         u8 arg;
121 };
122
123 struct legacy_led_args {
124         struct color_platform colors;
125         u8 brightness;
126         u8 state;
127 } __packed;
128
129 struct wmax_led_args {
130         u32 led_mask;
131         struct color_platform colors;
132         u8 state;
133 } __packed;
134
135 static struct platform_device *platform_device;
136 static struct device_attribute *zone_dev_attrs;
137 static struct attribute **zone_attrs;
138 static struct platform_zone *zone_data;
139
140 static struct platform_driver platform_driver = {
141         .driver = {
142                    .name = "alienware-wmi",
143                    .owner = THIS_MODULE,
144                    }
145 };
146
147 static struct attribute_group zone_attribute_group = {
148         .name = "rgb_zones",
149 };
150
151 static u8 interface;
152 static u8 lighting_control_state;
153 static u8 global_brightness;
154
155 /*
156  * Helpers used for zone control
157 */
158 static int parse_rgb(const char *buf, struct platform_zone *zone)
159 {
160         long unsigned int rgb;
161         int ret;
162         union color_union {
163                 struct color_platform cp;
164                 int package;
165         } repackager;
166
167         ret = kstrtoul(buf, 16, &rgb);
168         if (ret)
169                 return ret;
170
171         /* RGB triplet notation is 24-bit hexadecimal */
172         if (rgb > 0xFFFFFF)
173                 return -EINVAL;
174
175         repackager.package = rgb & 0x0f0f0f0f;
176         pr_debug("alienware-wmi: r: %d g:%d b: %d\n",
177                  repackager.cp.red, repackager.cp.green, repackager.cp.blue);
178         zone->colors = repackager.cp;
179         return 0;
180 }
181
182 static struct platform_zone *match_zone(struct device_attribute *attr)
183 {
184         int i;
185         for (i = 0; i < quirks->num_zones; i++) {
186                 if ((struct device_attribute *)zone_data[i].attr == attr) {
187                         pr_debug("alienware-wmi: matched zone location: %d\n",
188                                  zone_data[i].location);
189                         return &zone_data[i];
190                 }
191         }
192         return NULL;
193 }
194
195 /*
196  * Individual RGB zone control
197 */
198 static int alienware_update_led(struct platform_zone *zone)
199 {
200         int method_id;
201         acpi_status status;
202         char *guid;
203         struct acpi_buffer input;
204         struct legacy_led_args legacy_args;
205         struct wmax_led_args wmax_args;
206         if (interface == WMAX) {
207                 wmax_args.led_mask = 1 << zone->location;
208                 wmax_args.colors = zone->colors;
209                 wmax_args.state = lighting_control_state;
210                 guid = WMAX_CONTROL_GUID;
211                 method_id = WMAX_METHOD_ZONE_CONTROL;
212
213                 input.length = (acpi_size) sizeof(wmax_args);
214                 input.pointer = &wmax_args;
215         } else {
216                 legacy_args.colors = zone->colors;
217                 legacy_args.brightness = global_brightness;
218                 legacy_args.state = 0;
219                 if (lighting_control_state == LEGACY_BOOTING ||
220                     lighting_control_state == LEGACY_SUSPEND) {
221                         guid = LEGACY_POWER_CONTROL_GUID;
222                         legacy_args.state = lighting_control_state;
223                 } else
224                         guid = LEGACY_CONTROL_GUID;
225                 method_id = zone->location + 1;
226
227                 input.length = (acpi_size) sizeof(legacy_args);
228                 input.pointer = &legacy_args;
229         }
230         pr_debug("alienware-wmi: guid %s method %d\n", guid, method_id);
231
232         status = wmi_evaluate_method(guid, 1, method_id, &input, NULL);
233         if (ACPI_FAILURE(status))
234                 pr_err("alienware-wmi: zone set failure: %u\n", status);
235         return ACPI_FAILURE(status);
236 }
237
238 static ssize_t zone_show(struct device *dev, struct device_attribute *attr,
239                          char *buf)
240 {
241         struct platform_zone *target_zone;
242         target_zone = match_zone(attr);
243         if (target_zone == NULL)
244                 return sprintf(buf, "red: -1, green: -1, blue: -1\n");
245         return sprintf(buf, "red: %d, green: %d, blue: %d\n",
246                        target_zone->colors.red,
247                        target_zone->colors.green, target_zone->colors.blue);
248
249 }
250
251 static ssize_t zone_set(struct device *dev, struct device_attribute *attr,
252                         const char *buf, size_t count)
253 {
254         struct platform_zone *target_zone;
255         int ret;
256         target_zone = match_zone(attr);
257         if (target_zone == NULL) {
258                 pr_err("alienware-wmi: invalid target zone\n");
259                 return 1;
260         }
261         ret = parse_rgb(buf, target_zone);
262         if (ret)
263                 return ret;
264         ret = alienware_update_led(target_zone);
265         return ret ? ret : count;
266 }
267
268 /*
269  * LED Brightness (Global)
270 */
271 static int wmax_brightness(int brightness)
272 {
273         acpi_status status;
274         struct acpi_buffer input;
275         struct wmax_brightness_args args = {
276                 .led_mask = 0xFF,
277                 .percentage = brightness,
278         };
279         input.length = (acpi_size) sizeof(args);
280         input.pointer = &args;
281         status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
282                                      WMAX_METHOD_BRIGHTNESS, &input, NULL);
283         if (ACPI_FAILURE(status))
284                 pr_err("alienware-wmi: brightness set failure: %u\n", status);
285         return ACPI_FAILURE(status);
286 }
287
288 static void global_led_set(struct led_classdev *led_cdev,
289                            enum led_brightness brightness)
290 {
291         int ret;
292         global_brightness = brightness;
293         if (interface == WMAX)
294                 ret = wmax_brightness(brightness);
295         else
296                 ret = alienware_update_led(&zone_data[0]);
297         if (ret)
298                 pr_err("LED brightness update failed\n");
299 }
300
301 static enum led_brightness global_led_get(struct led_classdev *led_cdev)
302 {
303         return global_brightness;
304 }
305
306 static struct led_classdev global_led = {
307         .brightness_set = global_led_set,
308         .brightness_get = global_led_get,
309         .name = "alienware::global_brightness",
310 };
311
312 /*
313  * Lighting control state device attribute (Global)
314 */
315 static ssize_t show_control_state(struct device *dev,
316                                   struct device_attribute *attr, char *buf)
317 {
318         if (lighting_control_state == LEGACY_BOOTING)
319                 return scnprintf(buf, PAGE_SIZE, "[booting] running suspend\n");
320         else if (lighting_control_state == LEGACY_SUSPEND)
321                 return scnprintf(buf, PAGE_SIZE, "booting running [suspend]\n");
322         return scnprintf(buf, PAGE_SIZE, "booting [running] suspend\n");
323 }
324
325 static ssize_t store_control_state(struct device *dev,
326                                    struct device_attribute *attr,
327                                    const char *buf, size_t count)
328 {
329         long unsigned int val;
330         if (strcmp(buf, "booting\n") == 0)
331                 val = LEGACY_BOOTING;
332         else if (strcmp(buf, "suspend\n") == 0)
333                 val = LEGACY_SUSPEND;
334         else if (interface == LEGACY)
335                 val = LEGACY_RUNNING;
336         else
337                 val = WMAX_RUNNING;
338         lighting_control_state = val;
339         pr_debug("alienware-wmi: updated control state to %d\n",
340                  lighting_control_state);
341         return count;
342 }
343
344 static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
345                    store_control_state);
346
347 static int alienware_zone_init(struct platform_device *dev)
348 {
349         int i;
350         char buffer[10];
351         char *name;
352
353         if (interface == WMAX) {
354                 lighting_control_state = WMAX_RUNNING;
355         } else if (interface == LEGACY) {
356                 lighting_control_state = LEGACY_RUNNING;
357         }
358         global_led.max_brightness = 0x0F;
359         global_brightness = global_led.max_brightness;
360
361         /*
362          *      - zone_dev_attrs num_zones + 1 is for individual zones and then
363          *        null terminated
364          *      - zone_attrs num_zones + 2 is for all attrs in zone_dev_attrs +
365          *        the lighting control + null terminated
366          *      - zone_data num_zones is for the distinct zones
367          */
368         zone_dev_attrs =
369             kzalloc(sizeof(struct device_attribute) * (quirks->num_zones + 1),
370                     GFP_KERNEL);
371         if (!zone_dev_attrs)
372                 return -ENOMEM;
373
374         zone_attrs =
375             kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2),
376                     GFP_KERNEL);
377         if (!zone_attrs)
378                 return -ENOMEM;
379
380         zone_data =
381             kzalloc(sizeof(struct platform_zone) * (quirks->num_zones),
382                     GFP_KERNEL);
383         if (!zone_data)
384                 return -ENOMEM;
385
386         for (i = 0; i < quirks->num_zones; i++) {
387                 sprintf(buffer, "zone%02X", i);
388                 name = kstrdup(buffer, GFP_KERNEL);
389                 if (name == NULL)
390                         return 1;
391                 sysfs_attr_init(&zone_dev_attrs[i].attr);
392                 zone_dev_attrs[i].attr.name = name;
393                 zone_dev_attrs[i].attr.mode = 0644;
394                 zone_dev_attrs[i].show = zone_show;
395                 zone_dev_attrs[i].store = zone_set;
396                 zone_data[i].location = i;
397                 zone_attrs[i] = &zone_dev_attrs[i].attr;
398                 zone_data[i].attr = &zone_dev_attrs[i];
399         }
400         zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
401         zone_attribute_group.attrs = zone_attrs;
402
403         led_classdev_register(&dev->dev, &global_led);
404
405         return sysfs_create_group(&dev->dev.kobj, &zone_attribute_group);
406 }
407
408 static void alienware_zone_exit(struct platform_device *dev)
409 {
410         sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
411         led_classdev_unregister(&global_led);
412         if (zone_dev_attrs) {
413                 int i;
414                 for (i = 0; i < quirks->num_zones; i++)
415                         kfree(zone_dev_attrs[i].attr.name);
416         }
417         kfree(zone_dev_attrs);
418         kfree(zone_data);
419         kfree(zone_attrs);
420 }
421
422 /*
423         The HDMI mux sysfs node indicates the status of the HDMI input mux.
424         It can toggle between standard system GPU output and HDMI input.
425 */
426 static acpi_status alienware_hdmi_command(struct hdmi_args *in_args,
427                                           u32 command, int *out_data)
428 {
429         acpi_status status;
430         union acpi_object *obj;
431         struct acpi_buffer input;
432         struct acpi_buffer output;
433
434         input.length = (acpi_size) sizeof(*in_args);
435         input.pointer = in_args;
436         if (out_data != NULL) {
437                 output.length = ACPI_ALLOCATE_BUFFER;
438                 output.pointer = NULL;
439                 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
440                                              command, &input, &output);
441         } else
442                 status = wmi_evaluate_method(WMAX_CONTROL_GUID, 1,
443                                              command, &input, NULL);
444
445         if (ACPI_SUCCESS(status) && out_data != NULL) {
446                 obj = (union acpi_object *)output.pointer;
447                 if (obj && obj->type == ACPI_TYPE_INTEGER)
448                         *out_data = (u32) obj->integer.value;
449         }
450         return status;
451
452 }
453
454 static ssize_t show_hdmi_cable(struct device *dev,
455                                struct device_attribute *attr, char *buf)
456 {
457         acpi_status status;
458         u32 out_data;
459         struct hdmi_args in_args = {
460                 .arg = 0,
461         };
462         status =
463             alienware_hdmi_command(&in_args, WMAX_METHOD_HDMI_CABLE,
464                                    (u32 *) &out_data);
465         if (ACPI_SUCCESS(status)) {
466                 if (out_data == 0)
467                         return scnprintf(buf, PAGE_SIZE,
468                                          "[unconnected] connected unknown\n");
469                 else if (out_data == 1)
470                         return scnprintf(buf, PAGE_SIZE,
471                                          "unconnected [connected] unknown\n");
472         }
473         pr_err("alienware-wmi: unknown HDMI cable status: %d\n", status);
474         return scnprintf(buf, PAGE_SIZE, "unconnected connected [unknown]\n");
475 }
476
477 static ssize_t show_hdmi_source(struct device *dev,
478                                 struct device_attribute *attr, char *buf)
479 {
480         acpi_status status;
481         u32 out_data;
482         struct hdmi_args in_args = {
483                 .arg = 0,
484         };
485         status =
486             alienware_hdmi_command(&in_args, WMAX_METHOD_HDMI_STATUS,
487                                    (u32 *) &out_data);
488
489         if (ACPI_SUCCESS(status)) {
490                 if (out_data == 1)
491                         return scnprintf(buf, PAGE_SIZE,
492                                          "[input] gpu unknown\n");
493                 else if (out_data == 2)
494                         return scnprintf(buf, PAGE_SIZE,
495                                          "input [gpu] unknown\n");
496         }
497         pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data);
498         return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n");
499 }
500
501 static ssize_t toggle_hdmi_source(struct device *dev,
502                                   struct device_attribute *attr,
503                                   const char *buf, size_t count)
504 {
505         acpi_status status;
506         struct hdmi_args args;
507         if (strcmp(buf, "gpu\n") == 0)
508                 args.arg = 1;
509         else if (strcmp(buf, "input\n") == 0)
510                 args.arg = 2;
511         else
512                 args.arg = 3;
513         pr_debug("alienware-wmi: setting hdmi to %d : %s", args.arg, buf);
514
515         status = alienware_hdmi_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL);
516
517         if (ACPI_FAILURE(status))
518                 pr_err("alienware-wmi: HDMI toggle failed: results: %u\n",
519                        status);
520         return count;
521 }
522
523 static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL);
524 static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source,
525                    toggle_hdmi_source);
526
527 static struct attribute *hdmi_attrs[] = {
528         &dev_attr_cable.attr,
529         &dev_attr_source.attr,
530         NULL,
531 };
532
533 static struct attribute_group hdmi_attribute_group = {
534         .name = "hdmi",
535         .attrs = hdmi_attrs,
536 };
537
538 static void remove_hdmi(struct platform_device *dev)
539 {
540         sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group);
541 }
542
543 static int create_hdmi(struct platform_device *dev)
544 {
545         int ret;
546
547         ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group);
548         if (ret)
549                 goto error_create_hdmi;
550         return 0;
551
552 error_create_hdmi:
553         remove_hdmi(dev);
554         return ret;
555 }
556
557 static int __init alienware_wmi_init(void)
558 {
559         int ret;
560
561         if (wmi_has_guid(LEGACY_CONTROL_GUID))
562                 interface = LEGACY;
563         else if (wmi_has_guid(WMAX_CONTROL_GUID))
564                 interface = WMAX;
565         else {
566                 pr_warn("alienware-wmi: No known WMI GUID found\n");
567                 return -ENODEV;
568         }
569
570         dmi_check_system(alienware_quirks);
571         if (quirks == NULL)
572                 quirks = &quirk_unknown;
573
574         ret = platform_driver_register(&platform_driver);
575         if (ret)
576                 goto fail_platform_driver;
577         platform_device = platform_device_alloc("alienware-wmi", -1);
578         if (!platform_device) {
579                 ret = -ENOMEM;
580                 goto fail_platform_device1;
581         }
582         ret = platform_device_add(platform_device);
583         if (ret)
584                 goto fail_platform_device2;
585
586         if (interface == WMAX) {
587                 ret = create_hdmi(platform_device);
588                 if (ret)
589                         goto fail_prep_hdmi;
590         }
591
592         ret = alienware_zone_init(platform_device);
593         if (ret)
594                 goto fail_prep_zones;
595
596         return 0;
597
598 fail_prep_zones:
599         alienware_zone_exit(platform_device);
600 fail_prep_hdmi:
601         platform_device_del(platform_device);
602 fail_platform_device2:
603         platform_device_put(platform_device);
604 fail_platform_device1:
605         platform_driver_unregister(&platform_driver);
606 fail_platform_driver:
607         return ret;
608 }
609
610 module_init(alienware_wmi_init);
611
612 static void __exit alienware_wmi_exit(void)
613 {
614         if (platform_device) {
615                 alienware_zone_exit(platform_device);
616                 remove_hdmi(platform_device);
617                 platform_device_unregister(platform_device);
618                 platform_driver_unregister(&platform_driver);
619         }
620 }
621
622 module_exit(alienware_wmi_exit);