Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 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 |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 35 | #define WMAX_METHOD_HDMI_CABLE 0x5 |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 36 | #define WMAX_METHOD_AMPLIFIER_CABLE 0x6 |
Mario Limonciello | 8ea81ec | 2016-02-02 15:38:55 -0600 | [diff] [blame^] | 37 | #define WMAX_METHOD_DEEP_SLEEP_CONTROL 0x0B |
| 38 | #define WMAX_METHOD_DEEP_SLEEP_STATUS 0x0C |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 39 | |
| 40 | MODULE_AUTHOR("Mario Limonciello <mario_limonciello@dell.com>"); |
| 41 | MODULE_DESCRIPTION("Alienware special feature control"); |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | MODULE_ALIAS("wmi:" LEGACY_CONTROL_GUID); |
| 44 | MODULE_ALIAS("wmi:" WMAX_CONTROL_GUID); |
| 45 | |
| 46 | enum INTERFACE_FLAGS { |
| 47 | LEGACY, |
| 48 | WMAX, |
| 49 | }; |
| 50 | |
| 51 | enum LEGACY_CONTROL_STATES { |
| 52 | LEGACY_RUNNING = 1, |
| 53 | LEGACY_BOOTING = 0, |
| 54 | LEGACY_SUSPEND = 3, |
| 55 | }; |
| 56 | |
| 57 | enum WMAX_CONTROL_STATES { |
| 58 | WMAX_RUNNING = 0xFF, |
| 59 | WMAX_BOOTING = 0, |
| 60 | WMAX_SUSPEND = 3, |
| 61 | }; |
| 62 | |
| 63 | struct quirk_entry { |
| 64 | u8 num_zones; |
Mario Limonciello | fee4efd | 2014-07-23 23:19:23 -0500 | [diff] [blame] | 65 | u8 hdmi_mux; |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 66 | u8 amplifier; |
Mario Limonciello | 8ea81ec | 2016-02-02 15:38:55 -0600 | [diff] [blame^] | 67 | u8 deepslp; |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static struct quirk_entry *quirks; |
| 71 | |
| 72 | static struct quirk_entry quirk_unknown = { |
| 73 | .num_zones = 2, |
Mario Limonciello | fee4efd | 2014-07-23 23:19:23 -0500 | [diff] [blame] | 74 | .hdmi_mux = 0, |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 75 | .amplifier = 0, |
Mario Limonciello | 8ea81ec | 2016-02-02 15:38:55 -0600 | [diff] [blame^] | 76 | .deepslp = 0, |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 77 | }; |
| 78 | |
Mario Limonciello | 9e503a9 | 2016-02-02 15:38:53 -0600 | [diff] [blame] | 79 | static struct quirk_entry quirk_x51_r1_r2 = { |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 80 | .num_zones = 3, |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 81 | .hdmi_mux = 0, |
| 82 | .amplifier = 0, |
Mario Limonciello | 8ea81ec | 2016-02-02 15:38:55 -0600 | [diff] [blame^] | 83 | .deepslp = 0, |
Mario Limonciello | fee4efd | 2014-07-23 23:19:23 -0500 | [diff] [blame] | 84 | }; |
| 85 | |
Mario Limonciello | 9e503a9 | 2016-02-02 15:38:53 -0600 | [diff] [blame] | 86 | static struct quirk_entry quirk_x51_r3 = { |
| 87 | .num_zones = 4, |
| 88 | .hdmi_mux = 0, |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 89 | .amplifier = 1, |
Mario Limonciello | 8ea81ec | 2016-02-02 15:38:55 -0600 | [diff] [blame^] | 90 | .deepslp = 0, |
Mario Limonciello | 9e503a9 | 2016-02-02 15:38:53 -0600 | [diff] [blame] | 91 | }; |
| 92 | |
Mario Limonciello | fee4efd | 2014-07-23 23:19:23 -0500 | [diff] [blame] | 93 | static struct quirk_entry quirk_asm100 = { |
| 94 | .num_zones = 2, |
| 95 | .hdmi_mux = 1, |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 96 | .amplifier = 0, |
Mario Limonciello | 8ea81ec | 2016-02-02 15:38:55 -0600 | [diff] [blame^] | 97 | .deepslp = 0, |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 98 | }; |
| 99 | |
Mathias Krause | d997d88 | 2014-07-16 19:43:07 +0200 | [diff] [blame] | 100 | static int __init dmi_matched(const struct dmi_system_id *dmi) |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 101 | { |
| 102 | quirks = dmi->driver_data; |
| 103 | return 1; |
| 104 | } |
| 105 | |
Mathias Krause | d997d88 | 2014-07-16 19:43:07 +0200 | [diff] [blame] | 106 | static const struct dmi_system_id alienware_quirks[] __initconst = { |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 107 | { |
| 108 | .callback = dmi_matched, |
Mario Limonciello | 9e503a9 | 2016-02-02 15:38:53 -0600 | [diff] [blame] | 109 | .ident = "Alienware X51 R3", |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 110 | .matches = { |
| 111 | DMI_MATCH(DMI_SYS_VENDOR, "Alienware"), |
Mario Limonciello | 9e503a9 | 2016-02-02 15:38:53 -0600 | [diff] [blame] | 112 | DMI_MATCH(DMI_PRODUCT_NAME, "Alienware X51 R3"), |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 113 | }, |
Mario Limonciello | 9e503a9 | 2016-02-02 15:38:53 -0600 | [diff] [blame] | 114 | .driver_data = &quirk_x51_r3, |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 115 | }, |
| 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 Limonciello | 9e503a9 | 2016-02-02 15:38:53 -0600 | [diff] [blame] | 123 | .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 Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 133 | }, |
Mario Limonciello | fee4efd | 2014-07-23 23:19:23 -0500 | [diff] [blame] | 134 | { |
Mario Limonciello | 66ad0dd | 2016-02-02 15:38:52 -0600 | [diff] [blame] | 135 | .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 Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 143 | {} |
| 144 | }; |
| 145 | |
| 146 | struct color_platform { |
| 147 | u8 blue; |
| 148 | u8 green; |
| 149 | u8 red; |
| 150 | } __packed; |
| 151 | |
| 152 | struct platform_zone { |
| 153 | u8 location; |
| 154 | struct device_attribute *attr; |
| 155 | struct color_platform colors; |
| 156 | }; |
| 157 | |
| 158 | struct wmax_brightness_args { |
| 159 | u32 led_mask; |
| 160 | u32 percentage; |
| 161 | }; |
| 162 | |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 163 | struct wmax_basic_args { |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 164 | u8 arg; |
| 165 | }; |
| 166 | |
| 167 | struct legacy_led_args { |
| 168 | struct color_platform colors; |
| 169 | u8 brightness; |
| 170 | u8 state; |
| 171 | } __packed; |
| 172 | |
| 173 | struct wmax_led_args { |
| 174 | u32 led_mask; |
| 175 | struct color_platform colors; |
| 176 | u8 state; |
| 177 | } __packed; |
| 178 | |
| 179 | static struct platform_device *platform_device; |
| 180 | static struct device_attribute *zone_dev_attrs; |
| 181 | static struct attribute **zone_attrs; |
| 182 | static struct platform_zone *zone_data; |
| 183 | |
| 184 | static struct platform_driver platform_driver = { |
| 185 | .driver = { |
| 186 | .name = "alienware-wmi", |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 187 | } |
| 188 | }; |
| 189 | |
| 190 | static struct attribute_group zone_attribute_group = { |
| 191 | .name = "rgb_zones", |
| 192 | }; |
| 193 | |
| 194 | static u8 interface; |
| 195 | static u8 lighting_control_state; |
| 196 | static u8 global_brightness; |
| 197 | |
| 198 | /* |
| 199 | * Helpers used for zone control |
| 200 | */ |
| 201 | static 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 | |
| 225 | static 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 | */ |
| 241 | static 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 Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 248 | struct wmax_led_args wmax_basic_args; |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 249 | if (interface == WMAX) { |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 250 | wmax_basic_args.led_mask = 1 << zone->location; |
| 251 | wmax_basic_args.colors = zone->colors; |
| 252 | wmax_basic_args.state = lighting_control_state; |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 253 | guid = WMAX_CONTROL_GUID; |
| 254 | method_id = WMAX_METHOD_ZONE_CONTROL; |
| 255 | |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 256 | input.length = (acpi_size) sizeof(wmax_basic_args); |
| 257 | input.pointer = &wmax_basic_args; |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 258 | } 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 | |
| 281 | static 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 | |
| 294 | static 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 | */ |
| 314 | static 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 | |
| 331 | static 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 | |
| 344 | static enum led_brightness global_led_get(struct led_classdev *led_cdev) |
| 345 | { |
| 346 | return global_brightness; |
| 347 | } |
| 348 | |
| 349 | static 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 | */ |
| 358 | static 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 | |
| 368 | static 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 | |
| 387 | static DEVICE_ATTR(lighting_control_state, 0644, show_control_state, |
| 388 | store_control_state); |
| 389 | |
| 390 | static 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 Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 397 | lighting_control_state = WMAX_RUNNING; |
| 398 | } else if (interface == LEGACY) { |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 399 | lighting_control_state = LEGACY_RUNNING; |
| 400 | } |
Mario Limonciello | b998680 | 2014-05-07 15:08:09 -0500 | [diff] [blame] | 401 | global_led.max_brightness = 0x0F; |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 402 | 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 Limonciello | 562c7ce | 2014-04-04 14:40:20 -0500 | [diff] [blame] | 414 | if (!zone_dev_attrs) |
| 415 | return -ENOMEM; |
| 416 | |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 417 | zone_attrs = |
| 418 | kzalloc(sizeof(struct attribute *) * (quirks->num_zones + 2), |
| 419 | GFP_KERNEL); |
Mario Limonciello | 562c7ce | 2014-04-04 14:40:20 -0500 | [diff] [blame] | 420 | if (!zone_attrs) |
| 421 | return -ENOMEM; |
| 422 | |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 423 | zone_data = |
| 424 | kzalloc(sizeof(struct platform_zone) * (quirks->num_zones), |
| 425 | GFP_KERNEL); |
Mario Limonciello | 562c7ce | 2014-04-04 14:40:20 -0500 | [diff] [blame] | 426 | if (!zone_data) |
| 427 | return -ENOMEM; |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 428 | |
| 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 | |
| 451 | static 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 Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 465 | static acpi_status alienware_wmax_command(struct wmax_basic_args *in_args, |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 466 | u32 command, int *out_data) |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 467 | { |
| 468 | acpi_status status; |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 469 | union acpi_object *obj; |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 470 | 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 Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 493 | /* |
| 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 Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 497 | static 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 Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 502 | struct wmax_basic_args in_args = { |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 503 | .arg = 0, |
| 504 | }; |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 505 | status = |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 506 | alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_CABLE, |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 507 | (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 | |
| 520 | static 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 Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 525 | struct wmax_basic_args in_args = { |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 526 | .arg = 0, |
| 527 | }; |
| 528 | status = |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 529 | alienware_wmax_command(&in_args, WMAX_METHOD_HDMI_STATUS, |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 530 | (u32 *) &out_data); |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 531 | |
| 532 | if (ACPI_SUCCESS(status)) { |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 533 | if (out_data == 1) |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 534 | return scnprintf(buf, PAGE_SIZE, |
| 535 | "[input] gpu unknown\n"); |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 536 | else if (out_data == 2) |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 537 | return scnprintf(buf, PAGE_SIZE, |
| 538 | "input [gpu] unknown\n"); |
| 539 | } |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 540 | pr_err("alienware-wmi: unknown HDMI source status: %d\n", out_data); |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 541 | return scnprintf(buf, PAGE_SIZE, "input gpu [unknown]\n"); |
| 542 | } |
| 543 | |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 544 | static ssize_t toggle_hdmi_source(struct device *dev, |
| 545 | struct device_attribute *attr, |
| 546 | const char *buf, size_t count) |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 547 | { |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 548 | acpi_status status; |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 549 | struct wmax_basic_args args; |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 550 | 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 Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 557 | |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 558 | status = alienware_wmax_command(&args, WMAX_METHOD_HDMI_SOURCE, NULL); |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 559 | |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 560 | if (ACPI_FAILURE(status)) |
| 561 | pr_err("alienware-wmi: HDMI toggle failed: results: %u\n", |
| 562 | status); |
| 563 | return count; |
| 564 | } |
| 565 | |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 566 | static DEVICE_ATTR(cable, S_IRUGO, show_hdmi_cable, NULL); |
| 567 | static DEVICE_ATTR(source, S_IRUGO | S_IWUSR, show_hdmi_source, |
| 568 | toggle_hdmi_source); |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 569 | |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 570 | static struct attribute *hdmi_attrs[] = { |
| 571 | &dev_attr_cable.attr, |
| 572 | &dev_attr_source.attr, |
| 573 | NULL, |
| 574 | }; |
| 575 | |
| 576 | static struct attribute_group hdmi_attribute_group = { |
| 577 | .name = "hdmi", |
| 578 | .attrs = hdmi_attrs, |
| 579 | }; |
| 580 | |
| 581 | static void remove_hdmi(struct platform_device *dev) |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 582 | { |
Mario Limonciello | fee4efd | 2014-07-23 23:19:23 -0500 | [diff] [blame] | 583 | if (quirks->hdmi_mux > 0) |
| 584 | sysfs_remove_group(&dev->dev.kobj, &hdmi_attribute_group); |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 585 | } |
| 586 | |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 587 | static int create_hdmi(struct platform_device *dev) |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 588 | { |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 589 | int ret; |
| 590 | |
| 591 | ret = sysfs_create_group(&dev->dev.kobj, &hdmi_attribute_group); |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 592 | if (ret) |
| 593 | goto error_create_hdmi; |
| 594 | return 0; |
| 595 | |
| 596 | error_create_hdmi: |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 597 | remove_hdmi(dev); |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 598 | return ret; |
| 599 | } |
| 600 | |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 601 | /* |
| 602 | * Alienware GFX amplifier support |
| 603 | * - Currently supports reading cable status |
| 604 | * - Leaving expansion room to possibly support dock/undock events later |
| 605 | */ |
| 606 | static 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 | |
| 629 | static DEVICE_ATTR(status, S_IRUGO, show_amplifier_status, NULL); |
| 630 | |
| 631 | static struct attribute *amplifier_attrs[] = { |
| 632 | &dev_attr_status.attr, |
| 633 | NULL, |
| 634 | }; |
| 635 | |
| 636 | static struct attribute_group amplifier_attribute_group = { |
| 637 | .name = "amplifier", |
| 638 | .attrs = amplifier_attrs, |
| 639 | }; |
| 640 | |
| 641 | static void remove_amplifier(struct platform_device *dev) |
| 642 | { |
| 643 | if (quirks->amplifier > 0) |
| 644 | sysfs_remove_group(&dev->dev.kobj, &lifier_attribute_group); |
| 645 | } |
| 646 | |
| 647 | static int create_amplifier(struct platform_device *dev) |
| 648 | { |
| 649 | int ret; |
| 650 | |
| 651 | ret = sysfs_create_group(&dev->dev.kobj, &lifier_attribute_group); |
| 652 | if (ret) |
| 653 | remove_amplifier(dev); |
| 654 | return ret; |
| 655 | } |
| 656 | |
Mario Limonciello | 8ea81ec | 2016-02-02 15:38:55 -0600 | [diff] [blame^] | 657 | /* |
| 658 | * Deep Sleep Control support |
| 659 | * - Modifies BIOS setting for deep sleep control allowing extra wakeup events |
| 660 | */ |
| 661 | static 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 | |
| 686 | static 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 | |
| 710 | static DEVICE_ATTR(deepsleep, S_IRUGO | S_IWUSR, show_deepsleep_status, toggle_deepsleep); |
| 711 | |
| 712 | static struct attribute *deepsleep_attrs[] = { |
| 713 | &dev_attr_deepsleep.attr, |
| 714 | NULL, |
| 715 | }; |
| 716 | |
| 717 | static struct attribute_group deepsleep_attribute_group = { |
| 718 | .name = "deepsleep", |
| 719 | .attrs = deepsleep_attrs, |
| 720 | }; |
| 721 | |
| 722 | static 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 | |
| 728 | static 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 Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 738 | static 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 Limonciello | fee4efd | 2014-07-23 23:19:23 -0500 | [diff] [blame] | 767 | if (quirks->hdmi_mux > 0) { |
Mario Limonciello | bc2ef88 | 2014-05-07 15:08:10 -0500 | [diff] [blame] | 768 | ret = create_hdmi(platform_device); |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 769 | if (ret) |
| 770 | goto fail_prep_hdmi; |
| 771 | } |
| 772 | |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 773 | if (quirks->amplifier > 0) { |
| 774 | ret = create_amplifier(platform_device); |
| 775 | if (ret) |
| 776 | goto fail_prep_amplifier; |
| 777 | } |
| 778 | |
Mario Limonciello | 8ea81ec | 2016-02-02 15:38:55 -0600 | [diff] [blame^] | 779 | if (quirks->deepslp > 0) { |
| 780 | ret = create_deepsleep(platform_device); |
| 781 | if (ret) |
| 782 | goto fail_prep_deepsleep; |
| 783 | } |
| 784 | |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 785 | ret = alienware_zone_init(platform_device); |
| 786 | if (ret) |
| 787 | goto fail_prep_zones; |
| 788 | |
| 789 | return 0; |
| 790 | |
| 791 | fail_prep_zones: |
| 792 | alienware_zone_exit(platform_device); |
Mario Limonciello | 8ea81ec | 2016-02-02 15:38:55 -0600 | [diff] [blame^] | 793 | fail_prep_deepsleep: |
Mario Limonciello | cbbb50d6 | 2016-02-02 15:38:54 -0600 | [diff] [blame] | 794 | fail_prep_amplifier: |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 795 | fail_prep_hdmi: |
| 796 | platform_device_del(platform_device); |
| 797 | fail_platform_device2: |
| 798 | platform_device_put(platform_device); |
| 799 | fail_platform_device1: |
| 800 | platform_driver_unregister(&platform_driver); |
| 801 | fail_platform_driver: |
| 802 | return ret; |
| 803 | } |
| 804 | |
| 805 | module_init(alienware_wmi_init); |
| 806 | |
| 807 | static void __exit alienware_wmi_exit(void) |
| 808 | { |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 809 | if (platform_device) { |
Mario Limonciello | 562c7ce | 2014-04-04 14:40:20 -0500 | [diff] [blame] | 810 | alienware_zone_exit(platform_device); |
| 811 | remove_hdmi(platform_device); |
Mario Limonciello | a46ad0f | 2014-04-04 14:15:42 -0400 | [diff] [blame] | 812 | platform_device_unregister(platform_device); |
| 813 | platform_driver_unregister(&platform_driver); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | module_exit(alienware_wmi_exit); |