1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Asus PC WMI hotkey driver 4 * 5 * Copyright(C) 2010 Intel Corporation. 6 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com> 7 * 8 * Portions based on wistron_btns.c: 9 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz> 10 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org> 11 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru> 12 */ 13 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15 16 #include <linux/acpi.h> 17 #include <linux/backlight.h> 18 #include <linux/debugfs.h> 19 #include <linux/delay.h> 20 #include <linux/dmi.h> 21 #include <linux/fb.h> 22 #include <linux/hwmon.h> 23 #include <linux/hwmon-sysfs.h> 24 #include <linux/init.h> 25 #include <linux/input.h> 26 #include <linux/input/sparse-keymap.h> 27 #include <linux/kernel.h> 28 #include <linux/leds.h> 29 #include <linux/minmax.h> 30 #include <linux/module.h> 31 #include <linux/pci.h> 32 #include <linux/pci_hotplug.h> 33 #include <linux/platform_data/x86/asus-wmi.h> 34 #include <linux/platform_device.h> 35 #include <linux/platform_profile.h> 36 #include <linux/power_supply.h> 37 #include <linux/rfkill.h> 38 #include <linux/seq_file.h> 39 #include <linux/slab.h> 40 #include <linux/types.h> 41 #include <linux/units.h> 42 43 #include <acpi/battery.h> 44 #include <acpi/video.h> 45 46 #include "asus-wmi.h" 47 48 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>"); 49 MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>"); 50 MODULE_DESCRIPTION("Asus Generic WMI Driver"); 51 MODULE_LICENSE("GPL"); 52 53 static bool fnlock_default = true; 54 module_param(fnlock_default, bool, 0444); 55 56 #define to_asus_wmi_driver(pdrv) \ 57 (container_of((pdrv), struct asus_wmi_driver, platform_driver)) 58 59 #define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66" 60 61 #define NOTIFY_BRNUP_MIN 0x11 62 #define NOTIFY_BRNUP_MAX 0x1f 63 #define NOTIFY_BRNDOWN_MIN 0x20 64 #define NOTIFY_BRNDOWN_MAX 0x2e 65 #define NOTIFY_FNLOCK_TOGGLE 0x4e 66 #define NOTIFY_KBD_DOCK_CHANGE 0x75 67 #define NOTIFY_KBD_BRTUP 0xc4 68 #define NOTIFY_KBD_BRTDWN 0xc5 69 #define NOTIFY_KBD_BRTTOGGLE 0xc7 70 #define NOTIFY_KBD_FBM 0x99 71 #define NOTIFY_KBD_TTP 0xae 72 #define NOTIFY_LID_FLIP 0xfa 73 #define NOTIFY_LID_FLIP_ROG 0xbd 74 75 #define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0) 76 77 #define ASUS_MID_FAN_DESC "mid_fan" 78 #define ASUS_GPU_FAN_DESC "gpu_fan" 79 #define ASUS_FAN_DESC "cpu_fan" 80 #define ASUS_FAN_MFUN 0x13 81 #define ASUS_FAN_SFUN_READ 0x06 82 #define ASUS_FAN_SFUN_WRITE 0x07 83 84 /* Based on standard hwmon pwmX_enable values */ 85 #define ASUS_FAN_CTRL_FULLSPEED 0 86 #define ASUS_FAN_CTRL_MANUAL 1 87 #define ASUS_FAN_CTRL_AUTO 2 88 89 #define ASUS_FAN_BOOST_MODE_NORMAL 0 90 #define ASUS_FAN_BOOST_MODE_OVERBOOST 1 91 #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01 92 #define ASUS_FAN_BOOST_MODE_SILENT 2 93 #define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02 94 #define ASUS_FAN_BOOST_MODES_MASK 0x03 95 96 #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT 0 97 #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST 1 98 #define ASUS_THROTTLE_THERMAL_POLICY_SILENT 2 99 100 #define USB_INTEL_XUSB2PR 0xD0 101 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31 102 103 #define ASUS_ACPI_UID_ASUSWMI "ASUSWMI" 104 105 #define WMI_EVENT_MASK 0xFFFF 106 107 #define FAN_CURVE_POINTS 8 108 #define FAN_CURVE_BUF_LEN 32 109 #define FAN_CURVE_DEV_CPU 0x00 110 #define FAN_CURVE_DEV_GPU 0x01 111 #define FAN_CURVE_DEV_MID 0x02 112 /* Mask to determine if setting temperature or percentage */ 113 #define FAN_CURVE_PWM_MASK 0x04 114 115 /* Limits for tunables available on ASUS ROG laptops */ 116 #define PPT_TOTAL_MIN 5 117 #define PPT_TOTAL_MAX 250 118 #define PPT_CPU_MIN 5 119 #define PPT_CPU_MAX 130 120 #define NVIDIA_BOOST_MIN 5 121 #define NVIDIA_BOOST_MAX 25 122 #define NVIDIA_TEMP_MIN 75 123 #define NVIDIA_TEMP_MAX 87 124 125 #define ASUS_SCREENPAD_BRIGHT_MIN 20 126 #define ASUS_SCREENPAD_BRIGHT_MAX 255 127 #define ASUS_SCREENPAD_BRIGHT_DEFAULT 60 128 129 #define ASUS_MINI_LED_MODE_MASK 0x03 130 /* Standard modes for devices with only on/off */ 131 #define ASUS_MINI_LED_OFF 0x00 132 #define ASUS_MINI_LED_ON 0x01 133 /* New mode on some devices, define here to clarify remapping later */ 134 #define ASUS_MINI_LED_STRONG_MODE 0x02 135 /* New modes for devices with 3 mini-led mode types */ 136 #define ASUS_MINI_LED_2024_WEAK 0x00 137 #define ASUS_MINI_LED_2024_STRONG 0x01 138 #define ASUS_MINI_LED_2024_OFF 0x02 139 140 /* Controls the power state of the USB0 hub on ROG Ally which input is on */ 141 #define ASUS_USB0_PWR_EC0_CSEE "\\_SB.PCI0.SBRG.EC0.CSEE" 142 /* 300ms so far seems to produce a reliable result on AC and battery */ 143 #define ASUS_USB0_PWR_EC0_CSEE_WAIT 1500 144 145 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL }; 146 147 static int throttle_thermal_policy_write(struct asus_wmi *); 148 149 static bool ashs_present(void) 150 { 151 int i = 0; 152 while (ashs_ids[i]) { 153 if (acpi_dev_found(ashs_ids[i++])) 154 return true; 155 } 156 return false; 157 } 158 159 struct bios_args { 160 u32 arg0; 161 u32 arg1; 162 u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */ 163 u32 arg3; 164 u32 arg4; /* Some ROG laptops require a full 5 input args */ 165 u32 arg5; 166 } __packed; 167 168 /* 169 * Struct that's used for all methods called via AGFN. Naming is 170 * identically to the AML code. 171 */ 172 struct agfn_args { 173 u16 mfun; /* probably "Multi-function" to be called */ 174 u16 sfun; /* probably "Sub-function" to be called */ 175 u16 len; /* size of the hole struct, including subfunction fields */ 176 u8 stas; /* not used by now */ 177 u8 err; /* zero on success */ 178 } __packed; 179 180 /* struct used for calling fan read and write methods */ 181 struct agfn_fan_args { 182 struct agfn_args agfn; /* common fields */ 183 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */ 184 u32 speed; /* read: RPM/100 - write: 0-255 */ 185 } __packed; 186 187 /* 188 * <platform>/ - debugfs root directory 189 * dev_id - current dev_id 190 * ctrl_param - current ctrl_param 191 * method_id - current method_id 192 * devs - call DEVS(dev_id, ctrl_param) and print result 193 * dsts - call DSTS(dev_id) and print result 194 * call - call method_id(dev_id, ctrl_param) and print result 195 */ 196 struct asus_wmi_debug { 197 struct dentry *root; 198 u32 method_id; 199 u32 dev_id; 200 u32 ctrl_param; 201 }; 202 203 struct asus_rfkill { 204 struct asus_wmi *asus; 205 struct rfkill *rfkill; 206 u32 dev_id; 207 }; 208 209 enum fan_type { 210 FAN_TYPE_NONE = 0, 211 FAN_TYPE_AGFN, /* deprecated on newer platforms */ 212 FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */ 213 }; 214 215 struct fan_curve_data { 216 bool enabled; 217 u32 device_id; 218 u8 temps[FAN_CURVE_POINTS]; 219 u8 percents[FAN_CURVE_POINTS]; 220 }; 221 222 struct asus_wmi { 223 int dsts_id; 224 int spec; 225 int sfun; 226 227 struct input_dev *inputdev; 228 struct backlight_device *backlight_device; 229 struct backlight_device *screenpad_backlight_device; 230 struct platform_device *platform_device; 231 232 struct led_classdev wlan_led; 233 int wlan_led_wk; 234 struct led_classdev tpd_led; 235 int tpd_led_wk; 236 struct led_classdev kbd_led; 237 int kbd_led_wk; 238 struct led_classdev lightbar_led; 239 int lightbar_led_wk; 240 struct led_classdev micmute_led; 241 struct led_classdev camera_led; 242 struct workqueue_struct *led_workqueue; 243 struct work_struct tpd_led_work; 244 struct work_struct wlan_led_work; 245 struct work_struct lightbar_led_work; 246 247 struct asus_rfkill wlan; 248 struct asus_rfkill bluetooth; 249 struct asus_rfkill wimax; 250 struct asus_rfkill wwan3g; 251 struct asus_rfkill gps; 252 struct asus_rfkill uwb; 253 254 int tablet_switch_event_code; 255 u32 tablet_switch_dev_id; 256 bool tablet_switch_inverted; 257 258 /* The ROG Ally device requires the MCU USB device be disconnected before suspend */ 259 bool ally_mcu_usb_switch; 260 261 enum fan_type fan_type; 262 enum fan_type gpu_fan_type; 263 enum fan_type mid_fan_type; 264 int fan_pwm_mode; 265 int gpu_fan_pwm_mode; 266 int mid_fan_pwm_mode; 267 int agfn_pwm; 268 269 bool fan_boost_mode_available; 270 u8 fan_boost_mode_mask; 271 u8 fan_boost_mode; 272 273 bool egpu_enable_available; 274 bool dgpu_disable_available; 275 u32 gpu_mux_dev; 276 277 /* Tunables provided by ASUS for gaming laptops */ 278 u32 ppt_pl2_sppt; 279 u32 ppt_pl1_spl; 280 u32 ppt_apu_sppt; 281 u32 ppt_platform_sppt; 282 u32 ppt_fppt; 283 u32 nv_dynamic_boost; 284 u32 nv_temp_target; 285 286 u32 kbd_rgb_dev; 287 bool kbd_rgb_state_available; 288 289 bool throttle_thermal_policy_available; 290 u8 throttle_thermal_policy_mode; 291 292 bool cpu_fan_curve_available; 293 bool gpu_fan_curve_available; 294 bool mid_fan_curve_available; 295 struct fan_curve_data custom_fan_curves[3]; 296 297 struct platform_profile_handler platform_profile_handler; 298 bool platform_profile_support; 299 300 // The RSOC controls the maximum charging percentage. 301 bool battery_rsoc_available; 302 303 bool panel_overdrive_available; 304 u32 mini_led_dev_id; 305 306 struct hotplug_slot hotplug_slot; 307 struct mutex hotplug_lock; 308 struct mutex wmi_lock; 309 struct workqueue_struct *hotplug_workqueue; 310 struct work_struct hotplug_work; 311 312 bool fnlock_locked; 313 314 struct asus_wmi_debug debug; 315 316 struct asus_wmi_driver *driver; 317 }; 318 319 /* WMI ************************************************************************/ 320 321 static int asus_wmi_evaluate_method3(u32 method_id, 322 u32 arg0, u32 arg1, u32 arg2, u32 *retval) 323 { 324 struct bios_args args = { 325 .arg0 = arg0, 326 .arg1 = arg1, 327 .arg2 = arg2, 328 }; 329 struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; 330 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; 331 acpi_status status; 332 union acpi_object *obj; 333 u32 tmp = 0; 334 335 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id, 336 &input, &output); 337 338 if (ACPI_FAILURE(status)) 339 return -EIO; 340 341 obj = (union acpi_object *)output.pointer; 342 if (obj && obj->type == ACPI_TYPE_INTEGER) 343 tmp = (u32) obj->integer.value; 344 345 if (retval) 346 *retval = tmp; 347 348 kfree(obj); 349 350 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD) 351 return -ENODEV; 352 353 return 0; 354 } 355 356 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval) 357 { 358 return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval); 359 } 360 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method); 361 362 static int asus_wmi_evaluate_method5(u32 method_id, 363 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4, u32 *retval) 364 { 365 struct bios_args args = { 366 .arg0 = arg0, 367 .arg1 = arg1, 368 .arg2 = arg2, 369 .arg3 = arg3, 370 .arg4 = arg4, 371 }; 372 struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; 373 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; 374 acpi_status status; 375 union acpi_object *obj; 376 u32 tmp = 0; 377 378 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id, 379 &input, &output); 380 381 if (ACPI_FAILURE(status)) 382 return -EIO; 383 384 obj = (union acpi_object *)output.pointer; 385 if (obj && obj->type == ACPI_TYPE_INTEGER) 386 tmp = (u32) obj->integer.value; 387 388 if (retval) 389 *retval = tmp; 390 391 kfree(obj); 392 393 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD) 394 return -ENODEV; 395 396 return 0; 397 } 398 399 /* 400 * Returns as an error if the method output is not a buffer. Typically this 401 * means that the method called is unsupported. 402 */ 403 static int asus_wmi_evaluate_method_buf(u32 method_id, 404 u32 arg0, u32 arg1, u8 *ret_buffer, size_t size) 405 { 406 struct bios_args args = { 407 .arg0 = arg0, 408 .arg1 = arg1, 409 .arg2 = 0, 410 }; 411 struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; 412 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; 413 acpi_status status; 414 union acpi_object *obj; 415 int err = 0; 416 417 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id, 418 &input, &output); 419 420 if (ACPI_FAILURE(status)) 421 return -EIO; 422 423 obj = (union acpi_object *)output.pointer; 424 425 switch (obj->type) { 426 case ACPI_TYPE_BUFFER: 427 if (obj->buffer.length > size) { 428 err = -ENOSPC; 429 break; 430 } 431 if (obj->buffer.length == 0) { 432 err = -ENODATA; 433 break; 434 } 435 436 memcpy(ret_buffer, obj->buffer.pointer, obj->buffer.length); 437 break; 438 case ACPI_TYPE_INTEGER: 439 err = (u32)obj->integer.value; 440 441 if (err == ASUS_WMI_UNSUPPORTED_METHOD) 442 err = -ENODEV; 443 /* 444 * At least one method returns a 0 with no buffer if no arg 445 * is provided, such as ASUS_WMI_DEVID_CPU_FAN_CURVE 446 */ 447 if (err == 0) 448 err = -ENODATA; 449 break; 450 default: 451 err = -ENODATA; 452 break; 453 } 454 455 kfree(obj); 456 457 if (err) 458 return err; 459 460 return 0; 461 } 462 463 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args) 464 { 465 struct acpi_buffer input; 466 u64 phys_addr; 467 u32 retval; 468 u32 status; 469 470 /* 471 * Copy to dma capable address otherwise memory corruption occurs as 472 * bios has to be able to access it. 473 */ 474 input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL); 475 input.length = args.length; 476 if (!input.pointer) 477 return -ENOMEM; 478 phys_addr = virt_to_phys(input.pointer); 479 480 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN, 481 phys_addr, 0, &retval); 482 if (!status) 483 memcpy(args.pointer, input.pointer, args.length); 484 485 kfree(input.pointer); 486 if (status) 487 return -ENXIO; 488 489 return retval; 490 } 491 492 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval) 493 { 494 int err; 495 496 err = asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval); 497 498 if (err) 499 return err; 500 501 if (*retval == ~0) 502 return -ENODEV; 503 504 return 0; 505 } 506 507 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param, 508 u32 *retval) 509 { 510 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id, 511 ctrl_param, retval); 512 } 513 514 /* Helper for special devices with magic return codes */ 515 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus, 516 u32 dev_id, u32 mask) 517 { 518 u32 retval = 0; 519 int err; 520 521 err = asus_wmi_get_devstate(asus, dev_id, &retval); 522 if (err < 0) 523 return err; 524 525 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT)) 526 return -ENODEV; 527 528 if (mask == ASUS_WMI_DSTS_STATUS_BIT) { 529 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT) 530 return -ENODEV; 531 } 532 533 return retval & mask; 534 } 535 536 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id) 537 { 538 return asus_wmi_get_devstate_bits(asus, dev_id, 539 ASUS_WMI_DSTS_STATUS_BIT); 540 } 541 542 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id) 543 { 544 u32 retval; 545 int status = asus_wmi_get_devstate(asus, dev_id, &retval); 546 547 return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT); 548 } 549 550 /* Input **********************************************************************/ 551 static void asus_wmi_tablet_sw_report(struct asus_wmi *asus, bool value) 552 { 553 input_report_switch(asus->inputdev, SW_TABLET_MODE, 554 asus->tablet_switch_inverted ? !value : value); 555 input_sync(asus->inputdev); 556 } 557 558 static void asus_wmi_tablet_sw_init(struct asus_wmi *asus, u32 dev_id, int event_code) 559 { 560 struct device *dev = &asus->platform_device->dev; 561 int result; 562 563 result = asus_wmi_get_devstate_simple(asus, dev_id); 564 if (result >= 0) { 565 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE); 566 asus_wmi_tablet_sw_report(asus, result); 567 asus->tablet_switch_dev_id = dev_id; 568 asus->tablet_switch_event_code = event_code; 569 } else if (result == -ENODEV) { 570 dev_err(dev, "This device has tablet-mode-switch quirk but got ENODEV checking it. This is a bug."); 571 } else { 572 dev_err(dev, "Error checking for tablet-mode-switch: %d\n", result); 573 } 574 } 575 576 static int asus_wmi_input_init(struct asus_wmi *asus) 577 { 578 struct device *dev = &asus->platform_device->dev; 579 int err; 580 581 asus->inputdev = input_allocate_device(); 582 if (!asus->inputdev) 583 return -ENOMEM; 584 585 asus->inputdev->name = asus->driver->input_name; 586 asus->inputdev->phys = asus->driver->input_phys; 587 asus->inputdev->id.bustype = BUS_HOST; 588 asus->inputdev->dev.parent = dev; 589 set_bit(EV_REP, asus->inputdev->evbit); 590 591 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL); 592 if (err) 593 goto err_free_dev; 594 595 switch (asus->driver->quirks->tablet_switch_mode) { 596 case asus_wmi_no_tablet_switch: 597 break; 598 case asus_wmi_kbd_dock_devid: 599 asus->tablet_switch_inverted = true; 600 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_KBD_DOCK, NOTIFY_KBD_DOCK_CHANGE); 601 break; 602 case asus_wmi_lid_flip_devid: 603 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP, NOTIFY_LID_FLIP); 604 break; 605 case asus_wmi_lid_flip_rog_devid: 606 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP_ROG, NOTIFY_LID_FLIP_ROG); 607 break; 608 } 609 610 err = input_register_device(asus->inputdev); 611 if (err) 612 goto err_free_dev; 613 614 return 0; 615 616 err_free_dev: 617 input_free_device(asus->inputdev); 618 return err; 619 } 620 621 static void asus_wmi_input_exit(struct asus_wmi *asus) 622 { 623 if (asus->inputdev) 624 input_unregister_device(asus->inputdev); 625 626 asus->inputdev = NULL; 627 } 628 629 /* Tablet mode ****************************************************************/ 630 631 static void asus_wmi_tablet_mode_get_state(struct asus_wmi *asus) 632 { 633 int result; 634 635 if (!asus->tablet_switch_dev_id) 636 return; 637 638 result = asus_wmi_get_devstate_simple(asus, asus->tablet_switch_dev_id); 639 if (result >= 0) 640 asus_wmi_tablet_sw_report(asus, result); 641 } 642 643 /* Charging mode, 1=Barrel, 2=USB ******************************************/ 644 static ssize_t charge_mode_show(struct device *dev, 645 struct device_attribute *attr, char *buf) 646 { 647 struct asus_wmi *asus = dev_get_drvdata(dev); 648 int result, value; 649 650 result = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CHARGE_MODE, &value); 651 if (result < 0) 652 return result; 653 654 return sysfs_emit(buf, "%d\n", value & 0xff); 655 } 656 657 static DEVICE_ATTR_RO(charge_mode); 658 659 /* dGPU ********************************************************************/ 660 static ssize_t dgpu_disable_show(struct device *dev, 661 struct device_attribute *attr, char *buf) 662 { 663 struct asus_wmi *asus = dev_get_drvdata(dev); 664 int result; 665 666 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU); 667 if (result < 0) 668 return result; 669 670 return sysfs_emit(buf, "%d\n", result); 671 } 672 673 /* 674 * A user may be required to store the value twice, typcial store first, then 675 * rescan PCI bus to activate power, then store a second time to save correctly. 676 * The reason for this is that an extra code path in the ACPI is enabled when 677 * the device and bus are powered. 678 */ 679 static ssize_t dgpu_disable_store(struct device *dev, 680 struct device_attribute *attr, 681 const char *buf, size_t count) 682 { 683 int result, err; 684 u32 disable; 685 686 struct asus_wmi *asus = dev_get_drvdata(dev); 687 688 result = kstrtou32(buf, 10, &disable); 689 if (result) 690 return result; 691 692 if (disable > 1) 693 return -EINVAL; 694 695 if (asus->gpu_mux_dev) { 696 result = asus_wmi_get_devstate_simple(asus, asus->gpu_mux_dev); 697 if (result < 0) 698 /* An error here may signal greater failure of GPU handling */ 699 return result; 700 if (!result && disable) { 701 err = -ENODEV; 702 pr_warn("Can not disable dGPU when the MUX is in dGPU mode: %d\n", err); 703 return err; 704 } 705 } 706 707 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, disable, &result); 708 if (err) { 709 pr_warn("Failed to set dgpu disable: %d\n", err); 710 return err; 711 } 712 713 if (result > 1) { 714 pr_warn("Failed to set dgpu disable (result): 0x%x\n", result); 715 return -EIO; 716 } 717 718 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable"); 719 720 return count; 721 } 722 static DEVICE_ATTR_RW(dgpu_disable); 723 724 /* eGPU ********************************************************************/ 725 static ssize_t egpu_enable_show(struct device *dev, 726 struct device_attribute *attr, char *buf) 727 { 728 struct asus_wmi *asus = dev_get_drvdata(dev); 729 int result; 730 731 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU); 732 if (result < 0) 733 return result; 734 735 return sysfs_emit(buf, "%d\n", result); 736 } 737 738 /* The ACPI call to enable the eGPU also disables the internal dGPU */ 739 static ssize_t egpu_enable_store(struct device *dev, 740 struct device_attribute *attr, 741 const char *buf, size_t count) 742 { 743 int result, err; 744 u32 enable; 745 746 struct asus_wmi *asus = dev_get_drvdata(dev); 747 748 err = kstrtou32(buf, 10, &enable); 749 if (err) 750 return err; 751 752 if (enable > 1) 753 return -EINVAL; 754 755 err = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED); 756 if (err < 0) { 757 pr_warn("Failed to get egpu connection status: %d\n", err); 758 return err; 759 } 760 761 if (asus->gpu_mux_dev) { 762 result = asus_wmi_get_devstate_simple(asus, asus->gpu_mux_dev); 763 if (result < 0) { 764 /* An error here may signal greater failure of GPU handling */ 765 pr_warn("Failed to get gpu mux status: %d\n", result); 766 return result; 767 } 768 if (!result && enable) { 769 err = -ENODEV; 770 pr_warn("Can not enable eGPU when the MUX is in dGPU mode: %d\n", err); 771 return err; 772 } 773 } 774 775 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, enable, &result); 776 if (err) { 777 pr_warn("Failed to set egpu state: %d\n", err); 778 return err; 779 } 780 781 if (result > 1) { 782 pr_warn("Failed to set egpu state (retval): 0x%x\n", result); 783 return -EIO; 784 } 785 786 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable"); 787 788 return count; 789 } 790 static DEVICE_ATTR_RW(egpu_enable); 791 792 /* Is eGPU connected? *********************************************************/ 793 static ssize_t egpu_connected_show(struct device *dev, 794 struct device_attribute *attr, char *buf) 795 { 796 struct asus_wmi *asus = dev_get_drvdata(dev); 797 int result; 798 799 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED); 800 if (result < 0) 801 return result; 802 803 return sysfs_emit(buf, "%d\n", result); 804 } 805 806 static DEVICE_ATTR_RO(egpu_connected); 807 808 /* gpu mux switch *************************************************************/ 809 static ssize_t gpu_mux_mode_show(struct device *dev, 810 struct device_attribute *attr, char *buf) 811 { 812 struct asus_wmi *asus = dev_get_drvdata(dev); 813 int result; 814 815 result = asus_wmi_get_devstate_simple(asus, asus->gpu_mux_dev); 816 if (result < 0) 817 return result; 818 819 return sysfs_emit(buf, "%d\n", result); 820 } 821 822 static ssize_t gpu_mux_mode_store(struct device *dev, 823 struct device_attribute *attr, 824 const char *buf, size_t count) 825 { 826 struct asus_wmi *asus = dev_get_drvdata(dev); 827 int result, err; 828 u32 optimus; 829 830 err = kstrtou32(buf, 10, &optimus); 831 if (err) 832 return err; 833 834 if (optimus > 1) 835 return -EINVAL; 836 837 if (asus->dgpu_disable_available) { 838 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU); 839 if (result < 0) 840 /* An error here may signal greater failure of GPU handling */ 841 return result; 842 if (result && !optimus) { 843 err = -ENODEV; 844 pr_warn("Can not switch MUX to dGPU mode when dGPU is disabled: %d\n", err); 845 return err; 846 } 847 } 848 849 if (asus->egpu_enable_available) { 850 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU); 851 if (result < 0) 852 /* An error here may signal greater failure of GPU handling */ 853 return result; 854 if (result && !optimus) { 855 err = -ENODEV; 856 pr_warn("Can not switch MUX to dGPU mode when eGPU is enabled: %d\n", err); 857 return err; 858 } 859 } 860 861 err = asus_wmi_set_devstate(asus->gpu_mux_dev, optimus, &result); 862 if (err) { 863 dev_err(dev, "Failed to set GPU MUX mode: %d\n", err); 864 return err; 865 } 866 /* !1 is considered a fail by ASUS */ 867 if (result != 1) { 868 dev_warn(dev, "Failed to set GPU MUX mode (result): 0x%x\n", result); 869 return -EIO; 870 } 871 872 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "gpu_mux_mode"); 873 874 return count; 875 } 876 static DEVICE_ATTR_RW(gpu_mux_mode); 877 878 /* TUF Laptop Keyboard RGB Modes **********************************************/ 879 static ssize_t kbd_rgb_mode_store(struct device *dev, 880 struct device_attribute *attr, 881 const char *buf, size_t count) 882 { 883 u32 cmd, mode, r, g, b, speed; 884 struct led_classdev *led; 885 struct asus_wmi *asus; 886 int err; 887 888 led = dev_get_drvdata(dev); 889 asus = container_of(led, struct asus_wmi, kbd_led); 890 891 if (sscanf(buf, "%d %d %d %d %d %d", &cmd, &mode, &r, &g, &b, &speed) != 6) 892 return -EINVAL; 893 894 /* B3 is set and B4 is save to BIOS */ 895 switch (cmd) { 896 case 0: 897 cmd = 0xb3; 898 break; 899 case 1: 900 cmd = 0xb4; 901 break; 902 default: 903 return -EINVAL; 904 } 905 906 /* These are the known usable modes across all TUF/ROG */ 907 if (mode >= 12 || mode == 9) 908 mode = 10; 909 910 switch (speed) { 911 case 0: 912 speed = 0xe1; 913 break; 914 case 1: 915 speed = 0xeb; 916 break; 917 case 2: 918 speed = 0xf5; 919 break; 920 default: 921 speed = 0xeb; 922 } 923 924 err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS, asus->kbd_rgb_dev, 925 cmd | (mode << 8) | (r << 16) | (g << 24), b | (speed << 8), NULL); 926 if (err) 927 return err; 928 929 return count; 930 } 931 static DEVICE_ATTR_WO(kbd_rgb_mode); 932 933 static DEVICE_STRING_ATTR_RO(kbd_rgb_mode_index, 0444, 934 "cmd mode red green blue speed"); 935 936 static struct attribute *kbd_rgb_mode_attrs[] = { 937 &dev_attr_kbd_rgb_mode.attr, 938 &dev_attr_kbd_rgb_mode_index.attr.attr, 939 NULL, 940 }; 941 942 static const struct attribute_group kbd_rgb_mode_group = { 943 .attrs = kbd_rgb_mode_attrs, 944 }; 945 946 /* TUF Laptop Keyboard RGB State **********************************************/ 947 static ssize_t kbd_rgb_state_store(struct device *dev, 948 struct device_attribute *attr, 949 const char *buf, size_t count) 950 { 951 u32 flags, cmd, boot, awake, sleep, keyboard; 952 int err; 953 954 if (sscanf(buf, "%d %d %d %d %d", &cmd, &boot, &awake, &sleep, &keyboard) != 5) 955 return -EINVAL; 956 957 if (cmd) 958 cmd = BIT(2); 959 960 flags = 0; 961 if (boot) 962 flags |= BIT(1); 963 if (awake) 964 flags |= BIT(3); 965 if (sleep) 966 flags |= BIT(5); 967 if (keyboard) 968 flags |= BIT(7); 969 970 /* 0xbd is the required default arg0 for the method. Nothing happens otherwise */ 971 err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS, 972 ASUS_WMI_DEVID_TUF_RGB_STATE, 0xbd | cmd << 8 | (flags << 16), 0, NULL); 973 if (err) 974 return err; 975 976 return count; 977 } 978 static DEVICE_ATTR_WO(kbd_rgb_state); 979 980 static DEVICE_STRING_ATTR_RO(kbd_rgb_state_index, 0444, 981 "cmd boot awake sleep keyboard"); 982 983 static struct attribute *kbd_rgb_state_attrs[] = { 984 &dev_attr_kbd_rgb_state.attr, 985 &dev_attr_kbd_rgb_state_index.attr.attr, 986 NULL, 987 }; 988 989 static const struct attribute_group kbd_rgb_state_group = { 990 .attrs = kbd_rgb_state_attrs, 991 }; 992 993 static const struct attribute_group *kbd_rgb_mode_groups[] = { 994 NULL, 995 NULL, 996 NULL, 997 }; 998 999 /* Tunable: PPT: Intel=PL1, AMD=SPPT *****************************************/ 1000 static ssize_t ppt_pl2_sppt_store(struct device *dev, 1001 struct device_attribute *attr, 1002 const char *buf, size_t count) 1003 { 1004 struct asus_wmi *asus = dev_get_drvdata(dev); 1005 int result, err; 1006 u32 value; 1007 1008 result = kstrtou32(buf, 10, &value); 1009 if (result) 1010 return result; 1011 1012 if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX) 1013 return -EINVAL; 1014 1015 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL2_SPPT, value, &result); 1016 if (err) { 1017 pr_warn("Failed to set ppt_pl2_sppt: %d\n", err); 1018 return err; 1019 } 1020 1021 if (result > 1) { 1022 pr_warn("Failed to set ppt_pl2_sppt (result): 0x%x\n", result); 1023 return -EIO; 1024 } 1025 1026 asus->ppt_pl2_sppt = value; 1027 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl2_sppt"); 1028 1029 return count; 1030 } 1031 1032 static ssize_t ppt_pl2_sppt_show(struct device *dev, 1033 struct device_attribute *attr, 1034 char *buf) 1035 { 1036 struct asus_wmi *asus = dev_get_drvdata(dev); 1037 1038 return sysfs_emit(buf, "%u\n", asus->ppt_pl2_sppt); 1039 } 1040 static DEVICE_ATTR_RW(ppt_pl2_sppt); 1041 1042 /* Tunable: PPT, Intel=PL1, AMD=SPL ******************************************/ 1043 static ssize_t ppt_pl1_spl_store(struct device *dev, 1044 struct device_attribute *attr, 1045 const char *buf, size_t count) 1046 { 1047 struct asus_wmi *asus = dev_get_drvdata(dev); 1048 int result, err; 1049 u32 value; 1050 1051 result = kstrtou32(buf, 10, &value); 1052 if (result) 1053 return result; 1054 1055 if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX) 1056 return -EINVAL; 1057 1058 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL1_SPL, value, &result); 1059 if (err) { 1060 pr_warn("Failed to set ppt_pl1_spl: %d\n", err); 1061 return err; 1062 } 1063 1064 if (result > 1) { 1065 pr_warn("Failed to set ppt_pl1_spl (result): 0x%x\n", result); 1066 return -EIO; 1067 } 1068 1069 asus->ppt_pl1_spl = value; 1070 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl1_spl"); 1071 1072 return count; 1073 } 1074 static ssize_t ppt_pl1_spl_show(struct device *dev, 1075 struct device_attribute *attr, 1076 char *buf) 1077 { 1078 struct asus_wmi *asus = dev_get_drvdata(dev); 1079 1080 return sysfs_emit(buf, "%u\n", asus->ppt_pl1_spl); 1081 } 1082 static DEVICE_ATTR_RW(ppt_pl1_spl); 1083 1084 /* Tunable: PPT APU FPPT ******************************************************/ 1085 static ssize_t ppt_fppt_store(struct device *dev, 1086 struct device_attribute *attr, 1087 const char *buf, size_t count) 1088 { 1089 struct asus_wmi *asus = dev_get_drvdata(dev); 1090 int result, err; 1091 u32 value; 1092 1093 result = kstrtou32(buf, 10, &value); 1094 if (result) 1095 return result; 1096 1097 if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX) 1098 return -EINVAL; 1099 1100 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_FPPT, value, &result); 1101 if (err) { 1102 pr_warn("Failed to set ppt_fppt: %d\n", err); 1103 return err; 1104 } 1105 1106 if (result > 1) { 1107 pr_warn("Failed to set ppt_fppt (result): 0x%x\n", result); 1108 return -EIO; 1109 } 1110 1111 asus->ppt_fppt = value; 1112 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_fpu_sppt"); 1113 1114 return count; 1115 } 1116 1117 static ssize_t ppt_fppt_show(struct device *dev, 1118 struct device_attribute *attr, 1119 char *buf) 1120 { 1121 struct asus_wmi *asus = dev_get_drvdata(dev); 1122 1123 return sysfs_emit(buf, "%u\n", asus->ppt_fppt); 1124 } 1125 static DEVICE_ATTR_RW(ppt_fppt); 1126 1127 /* Tunable: PPT APU SPPT *****************************************************/ 1128 static ssize_t ppt_apu_sppt_store(struct device *dev, 1129 struct device_attribute *attr, 1130 const char *buf, size_t count) 1131 { 1132 struct asus_wmi *asus = dev_get_drvdata(dev); 1133 int result, err; 1134 u32 value; 1135 1136 result = kstrtou32(buf, 10, &value); 1137 if (result) 1138 return result; 1139 1140 if (value < PPT_CPU_MIN || value > PPT_CPU_MAX) 1141 return -EINVAL; 1142 1143 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_APU_SPPT, value, &result); 1144 if (err) { 1145 pr_warn("Failed to set ppt_apu_sppt: %d\n", err); 1146 return err; 1147 } 1148 1149 if (result > 1) { 1150 pr_warn("Failed to set ppt_apu_sppt (result): 0x%x\n", result); 1151 return -EIO; 1152 } 1153 1154 asus->ppt_apu_sppt = value; 1155 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_apu_sppt"); 1156 1157 return count; 1158 } 1159 1160 static ssize_t ppt_apu_sppt_show(struct device *dev, 1161 struct device_attribute *attr, 1162 char *buf) 1163 { 1164 struct asus_wmi *asus = dev_get_drvdata(dev); 1165 1166 return sysfs_emit(buf, "%u\n", asus->ppt_apu_sppt); 1167 } 1168 static DEVICE_ATTR_RW(ppt_apu_sppt); 1169 1170 /* Tunable: PPT platform SPPT ************************************************/ 1171 static ssize_t ppt_platform_sppt_store(struct device *dev, 1172 struct device_attribute *attr, 1173 const char *buf, size_t count) 1174 { 1175 struct asus_wmi *asus = dev_get_drvdata(dev); 1176 int result, err; 1177 u32 value; 1178 1179 result = kstrtou32(buf, 10, &value); 1180 if (result) 1181 return result; 1182 1183 if (value < PPT_CPU_MIN || value > PPT_CPU_MAX) 1184 return -EINVAL; 1185 1186 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PLAT_SPPT, value, &result); 1187 if (err) { 1188 pr_warn("Failed to set ppt_platform_sppt: %d\n", err); 1189 return err; 1190 } 1191 1192 if (result > 1) { 1193 pr_warn("Failed to set ppt_platform_sppt (result): 0x%x\n", result); 1194 return -EIO; 1195 } 1196 1197 asus->ppt_platform_sppt = value; 1198 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_platform_sppt"); 1199 1200 return count; 1201 } 1202 1203 static ssize_t ppt_platform_sppt_show(struct device *dev, 1204 struct device_attribute *attr, 1205 char *buf) 1206 { 1207 struct asus_wmi *asus = dev_get_drvdata(dev); 1208 1209 return sysfs_emit(buf, "%u\n", asus->ppt_platform_sppt); 1210 } 1211 static DEVICE_ATTR_RW(ppt_platform_sppt); 1212 1213 /* Tunable: NVIDIA dynamic boost *********************************************/ 1214 static ssize_t nv_dynamic_boost_store(struct device *dev, 1215 struct device_attribute *attr, 1216 const char *buf, size_t count) 1217 { 1218 struct asus_wmi *asus = dev_get_drvdata(dev); 1219 int result, err; 1220 u32 value; 1221 1222 result = kstrtou32(buf, 10, &value); 1223 if (result) 1224 return result; 1225 1226 if (value < NVIDIA_BOOST_MIN || value > NVIDIA_BOOST_MAX) 1227 return -EINVAL; 1228 1229 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_DYN_BOOST, value, &result); 1230 if (err) { 1231 pr_warn("Failed to set nv_dynamic_boost: %d\n", err); 1232 return err; 1233 } 1234 1235 if (result > 1) { 1236 pr_warn("Failed to set nv_dynamic_boost (result): 0x%x\n", result); 1237 return -EIO; 1238 } 1239 1240 asus->nv_dynamic_boost = value; 1241 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_dynamic_boost"); 1242 1243 return count; 1244 } 1245 1246 static ssize_t nv_dynamic_boost_show(struct device *dev, 1247 struct device_attribute *attr, 1248 char *buf) 1249 { 1250 struct asus_wmi *asus = dev_get_drvdata(dev); 1251 1252 return sysfs_emit(buf, "%u\n", asus->nv_dynamic_boost); 1253 } 1254 static DEVICE_ATTR_RW(nv_dynamic_boost); 1255 1256 /* Tunable: NVIDIA temperature target ****************************************/ 1257 static ssize_t nv_temp_target_store(struct device *dev, 1258 struct device_attribute *attr, 1259 const char *buf, size_t count) 1260 { 1261 struct asus_wmi *asus = dev_get_drvdata(dev); 1262 int result, err; 1263 u32 value; 1264 1265 result = kstrtou32(buf, 10, &value); 1266 if (result) 1267 return result; 1268 1269 if (value < NVIDIA_TEMP_MIN || value > NVIDIA_TEMP_MAX) 1270 return -EINVAL; 1271 1272 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_THERM_TARGET, value, &result); 1273 if (err) { 1274 pr_warn("Failed to set nv_temp_target: %d\n", err); 1275 return err; 1276 } 1277 1278 if (result > 1) { 1279 pr_warn("Failed to set nv_temp_target (result): 0x%x\n", result); 1280 return -EIO; 1281 } 1282 1283 asus->nv_temp_target = value; 1284 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_temp_target"); 1285 1286 return count; 1287 } 1288 1289 static ssize_t nv_temp_target_show(struct device *dev, 1290 struct device_attribute *attr, 1291 char *buf) 1292 { 1293 struct asus_wmi *asus = dev_get_drvdata(dev); 1294 1295 return sysfs_emit(buf, "%u\n", asus->nv_temp_target); 1296 } 1297 static DEVICE_ATTR_RW(nv_temp_target); 1298 1299 /* Ally MCU Powersave ********************************************************/ 1300 static ssize_t mcu_powersave_show(struct device *dev, 1301 struct device_attribute *attr, char *buf) 1302 { 1303 struct asus_wmi *asus = dev_get_drvdata(dev); 1304 int result; 1305 1306 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_MCU_POWERSAVE); 1307 if (result < 0) 1308 return result; 1309 1310 return sysfs_emit(buf, "%d\n", result); 1311 } 1312 1313 static ssize_t mcu_powersave_store(struct device *dev, 1314 struct device_attribute *attr, 1315 const char *buf, size_t count) 1316 { 1317 int result, err; 1318 u32 enable; 1319 1320 struct asus_wmi *asus = dev_get_drvdata(dev); 1321 1322 result = kstrtou32(buf, 10, &enable); 1323 if (result) 1324 return result; 1325 1326 if (enable > 1) 1327 return -EINVAL; 1328 1329 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MCU_POWERSAVE, enable, &result); 1330 if (err) { 1331 pr_warn("Failed to set MCU powersave: %d\n", err); 1332 return err; 1333 } 1334 1335 if (result > 1) { 1336 pr_warn("Failed to set MCU powersave (result): 0x%x\n", result); 1337 return -EIO; 1338 } 1339 1340 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "mcu_powersave"); 1341 1342 return count; 1343 } 1344 static DEVICE_ATTR_RW(mcu_powersave); 1345 1346 /* Battery ********************************************************************/ 1347 1348 /* The battery maximum charging percentage */ 1349 static int charge_end_threshold; 1350 1351 static ssize_t charge_control_end_threshold_store(struct device *dev, 1352 struct device_attribute *attr, 1353 const char *buf, size_t count) 1354 { 1355 int value, ret, rv; 1356 1357 ret = kstrtouint(buf, 10, &value); 1358 if (ret) 1359 return ret; 1360 1361 if (value < 0 || value > 100) 1362 return -EINVAL; 1363 1364 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv); 1365 if (ret) 1366 return ret; 1367 1368 if (rv != 1) 1369 return -EIO; 1370 1371 /* There isn't any method in the DSDT to read the threshold, so we 1372 * save the threshold. 1373 */ 1374 charge_end_threshold = value; 1375 return count; 1376 } 1377 1378 static ssize_t charge_control_end_threshold_show(struct device *device, 1379 struct device_attribute *attr, 1380 char *buf) 1381 { 1382 return sysfs_emit(buf, "%d\n", charge_end_threshold); 1383 } 1384 1385 static DEVICE_ATTR_RW(charge_control_end_threshold); 1386 1387 static int asus_wmi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook) 1388 { 1389 /* The WMI method does not provide a way to specific a battery, so we 1390 * just assume it is the first battery. 1391 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first 1392 * battery is named BATT. 1393 */ 1394 if (strcmp(battery->desc->name, "BAT0") != 0 && 1395 strcmp(battery->desc->name, "BAT1") != 0 && 1396 strcmp(battery->desc->name, "BATC") != 0 && 1397 strcmp(battery->desc->name, "BATT") != 0) 1398 return -ENODEV; 1399 1400 if (device_create_file(&battery->dev, 1401 &dev_attr_charge_control_end_threshold)) 1402 return -ENODEV; 1403 1404 /* The charge threshold is only reset when the system is power cycled, 1405 * and we can't get the current threshold so let set it to 100% when 1406 * a battery is added. 1407 */ 1408 asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL); 1409 charge_end_threshold = 100; 1410 1411 return 0; 1412 } 1413 1414 static int asus_wmi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook) 1415 { 1416 device_remove_file(&battery->dev, 1417 &dev_attr_charge_control_end_threshold); 1418 return 0; 1419 } 1420 1421 static struct acpi_battery_hook battery_hook = { 1422 .add_battery = asus_wmi_battery_add, 1423 .remove_battery = asus_wmi_battery_remove, 1424 .name = "ASUS Battery Extension", 1425 }; 1426 1427 static void asus_wmi_battery_init(struct asus_wmi *asus) 1428 { 1429 asus->battery_rsoc_available = false; 1430 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) { 1431 asus->battery_rsoc_available = true; 1432 battery_hook_register(&battery_hook); 1433 } 1434 } 1435 1436 static void asus_wmi_battery_exit(struct asus_wmi *asus) 1437 { 1438 if (asus->battery_rsoc_available) 1439 battery_hook_unregister(&battery_hook); 1440 } 1441 1442 /* LEDs ***********************************************************************/ 1443 1444 /* 1445 * These functions actually update the LED's, and are called from a 1446 * workqueue. By doing this as separate work rather than when the LED 1447 * subsystem asks, we avoid messing with the Asus ACPI stuff during a 1448 * potentially bad time, such as a timer interrupt. 1449 */ 1450 static void tpd_led_update(struct work_struct *work) 1451 { 1452 int ctrl_param; 1453 struct asus_wmi *asus; 1454 1455 asus = container_of(work, struct asus_wmi, tpd_led_work); 1456 1457 ctrl_param = asus->tpd_led_wk; 1458 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL); 1459 } 1460 1461 static void tpd_led_set(struct led_classdev *led_cdev, 1462 enum led_brightness value) 1463 { 1464 struct asus_wmi *asus; 1465 1466 asus = container_of(led_cdev, struct asus_wmi, tpd_led); 1467 1468 asus->tpd_led_wk = !!value; 1469 queue_work(asus->led_workqueue, &asus->tpd_led_work); 1470 } 1471 1472 static int read_tpd_led_state(struct asus_wmi *asus) 1473 { 1474 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED); 1475 } 1476 1477 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev) 1478 { 1479 struct asus_wmi *asus; 1480 1481 asus = container_of(led_cdev, struct asus_wmi, tpd_led); 1482 1483 return read_tpd_led_state(asus); 1484 } 1485 1486 static void kbd_led_update(struct asus_wmi *asus) 1487 { 1488 int ctrl_param = 0; 1489 1490 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F); 1491 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL); 1492 } 1493 1494 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env) 1495 { 1496 int retval; 1497 1498 /* 1499 * bits 0-2: level 1500 * bit 7: light on/off 1501 * bit 8-10: environment (0: dark, 1: normal, 2: light) 1502 * bit 17: status unknown 1503 */ 1504 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT, 1505 0xFFFF); 1506 1507 /* Unknown status is considered as off */ 1508 if (retval == 0x8000) 1509 retval = 0; 1510 1511 if (retval < 0) 1512 return retval; 1513 1514 if (level) 1515 *level = retval & 0x7F; 1516 if (env) 1517 *env = (retval >> 8) & 0x7F; 1518 return 0; 1519 } 1520 1521 static void do_kbd_led_set(struct led_classdev *led_cdev, int value) 1522 { 1523 struct asus_wmi *asus; 1524 int max_level; 1525 1526 asus = container_of(led_cdev, struct asus_wmi, kbd_led); 1527 max_level = asus->kbd_led.max_brightness; 1528 1529 asus->kbd_led_wk = clamp_val(value, 0, max_level); 1530 kbd_led_update(asus); 1531 } 1532 1533 static void kbd_led_set(struct led_classdev *led_cdev, 1534 enum led_brightness value) 1535 { 1536 /* Prevent disabling keyboard backlight on module unregister */ 1537 if (led_cdev->flags & LED_UNREGISTERING) 1538 return; 1539 1540 do_kbd_led_set(led_cdev, value); 1541 } 1542 1543 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value) 1544 { 1545 struct led_classdev *led_cdev = &asus->kbd_led; 1546 1547 do_kbd_led_set(led_cdev, value); 1548 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk); 1549 } 1550 1551 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev) 1552 { 1553 struct asus_wmi *asus; 1554 int retval, value; 1555 1556 asus = container_of(led_cdev, struct asus_wmi, kbd_led); 1557 1558 retval = kbd_led_read(asus, &value, NULL); 1559 if (retval < 0) 1560 return retval; 1561 1562 return value; 1563 } 1564 1565 static int wlan_led_unknown_state(struct asus_wmi *asus) 1566 { 1567 u32 result; 1568 1569 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result); 1570 1571 return result & ASUS_WMI_DSTS_UNKNOWN_BIT; 1572 } 1573 1574 static void wlan_led_update(struct work_struct *work) 1575 { 1576 int ctrl_param; 1577 struct asus_wmi *asus; 1578 1579 asus = container_of(work, struct asus_wmi, wlan_led_work); 1580 1581 ctrl_param = asus->wlan_led_wk; 1582 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL); 1583 } 1584 1585 static void wlan_led_set(struct led_classdev *led_cdev, 1586 enum led_brightness value) 1587 { 1588 struct asus_wmi *asus; 1589 1590 asus = container_of(led_cdev, struct asus_wmi, wlan_led); 1591 1592 asus->wlan_led_wk = !!value; 1593 queue_work(asus->led_workqueue, &asus->wlan_led_work); 1594 } 1595 1596 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev) 1597 { 1598 struct asus_wmi *asus; 1599 u32 result; 1600 1601 asus = container_of(led_cdev, struct asus_wmi, wlan_led); 1602 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result); 1603 1604 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK; 1605 } 1606 1607 static void lightbar_led_update(struct work_struct *work) 1608 { 1609 struct asus_wmi *asus; 1610 int ctrl_param; 1611 1612 asus = container_of(work, struct asus_wmi, lightbar_led_work); 1613 1614 ctrl_param = asus->lightbar_led_wk; 1615 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL); 1616 } 1617 1618 static void lightbar_led_set(struct led_classdev *led_cdev, 1619 enum led_brightness value) 1620 { 1621 struct asus_wmi *asus; 1622 1623 asus = container_of(led_cdev, struct asus_wmi, lightbar_led); 1624 1625 asus->lightbar_led_wk = !!value; 1626 queue_work(asus->led_workqueue, &asus->lightbar_led_work); 1627 } 1628 1629 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev) 1630 { 1631 struct asus_wmi *asus; 1632 u32 result; 1633 1634 asus = container_of(led_cdev, struct asus_wmi, lightbar_led); 1635 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result); 1636 1637 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK; 1638 } 1639 1640 static int micmute_led_set(struct led_classdev *led_cdev, 1641 enum led_brightness brightness) 1642 { 1643 int state = brightness != LED_OFF; 1644 int err; 1645 1646 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MICMUTE_LED, state, NULL); 1647 return err < 0 ? err : 0; 1648 } 1649 1650 static enum led_brightness camera_led_get(struct led_classdev *led_cdev) 1651 { 1652 struct asus_wmi *asus; 1653 u32 result; 1654 1655 asus = container_of(led_cdev, struct asus_wmi, camera_led); 1656 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CAMERA_LED, &result); 1657 1658 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK; 1659 } 1660 1661 static int camera_led_set(struct led_classdev *led_cdev, 1662 enum led_brightness brightness) 1663 { 1664 int state = brightness != LED_OFF; 1665 int err; 1666 1667 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_CAMERA_LED, state, NULL); 1668 return err < 0 ? err : 0; 1669 } 1670 1671 static void asus_wmi_led_exit(struct asus_wmi *asus) 1672 { 1673 led_classdev_unregister(&asus->kbd_led); 1674 led_classdev_unregister(&asus->tpd_led); 1675 led_classdev_unregister(&asus->wlan_led); 1676 led_classdev_unregister(&asus->lightbar_led); 1677 led_classdev_unregister(&asus->micmute_led); 1678 led_classdev_unregister(&asus->camera_led); 1679 1680 if (asus->led_workqueue) 1681 destroy_workqueue(asus->led_workqueue); 1682 } 1683 1684 static int asus_wmi_led_init(struct asus_wmi *asus) 1685 { 1686 int rv = 0, num_rgb_groups = 0, led_val; 1687 1688 if (asus->kbd_rgb_dev) 1689 kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_mode_group; 1690 if (asus->kbd_rgb_state_available) 1691 kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_state_group; 1692 1693 asus->led_workqueue = create_singlethread_workqueue("led_workqueue"); 1694 if (!asus->led_workqueue) 1695 return -ENOMEM; 1696 1697 if (read_tpd_led_state(asus) >= 0) { 1698 INIT_WORK(&asus->tpd_led_work, tpd_led_update); 1699 1700 asus->tpd_led.name = "asus::touchpad"; 1701 asus->tpd_led.brightness_set = tpd_led_set; 1702 asus->tpd_led.brightness_get = tpd_led_get; 1703 asus->tpd_led.max_brightness = 1; 1704 1705 rv = led_classdev_register(&asus->platform_device->dev, 1706 &asus->tpd_led); 1707 if (rv) 1708 goto error; 1709 } 1710 1711 if (!kbd_led_read(asus, &led_val, NULL)) { 1712 asus->kbd_led_wk = led_val; 1713 asus->kbd_led.name = "asus::kbd_backlight"; 1714 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED; 1715 asus->kbd_led.brightness_set = kbd_led_set; 1716 asus->kbd_led.brightness_get = kbd_led_get; 1717 asus->kbd_led.max_brightness = 3; 1718 1719 if (num_rgb_groups != 0) 1720 asus->kbd_led.groups = kbd_rgb_mode_groups; 1721 1722 rv = led_classdev_register(&asus->platform_device->dev, 1723 &asus->kbd_led); 1724 if (rv) 1725 goto error; 1726 } 1727 1728 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED) 1729 && (asus->driver->quirks->wapf > 0)) { 1730 INIT_WORK(&asus->wlan_led_work, wlan_led_update); 1731 1732 asus->wlan_led.name = "asus::wlan"; 1733 asus->wlan_led.brightness_set = wlan_led_set; 1734 if (!wlan_led_unknown_state(asus)) 1735 asus->wlan_led.brightness_get = wlan_led_get; 1736 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME; 1737 asus->wlan_led.max_brightness = 1; 1738 asus->wlan_led.default_trigger = "asus-wlan"; 1739 1740 rv = led_classdev_register(&asus->platform_device->dev, 1741 &asus->wlan_led); 1742 if (rv) 1743 goto error; 1744 } 1745 1746 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) { 1747 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update); 1748 1749 asus->lightbar_led.name = "asus::lightbar"; 1750 asus->lightbar_led.brightness_set = lightbar_led_set; 1751 asus->lightbar_led.brightness_get = lightbar_led_get; 1752 asus->lightbar_led.max_brightness = 1; 1753 1754 rv = led_classdev_register(&asus->platform_device->dev, 1755 &asus->lightbar_led); 1756 } 1757 1758 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MICMUTE_LED)) { 1759 asus->micmute_led.name = "platform::micmute"; 1760 asus->micmute_led.max_brightness = 1; 1761 asus->micmute_led.brightness_set_blocking = micmute_led_set; 1762 asus->micmute_led.default_trigger = "audio-micmute"; 1763 1764 rv = led_classdev_register(&asus->platform_device->dev, 1765 &asus->micmute_led); 1766 if (rv) 1767 goto error; 1768 } 1769 1770 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CAMERA_LED)) { 1771 asus->camera_led.name = "asus::camera"; 1772 asus->camera_led.max_brightness = 1; 1773 asus->camera_led.brightness_get = camera_led_get; 1774 asus->camera_led.brightness_set_blocking = camera_led_set; 1775 1776 rv = led_classdev_register(&asus->platform_device->dev, 1777 &asus->camera_led); 1778 if (rv) 1779 goto error; 1780 } 1781 1782 error: 1783 if (rv) 1784 asus_wmi_led_exit(asus); 1785 1786 return rv; 1787 } 1788 1789 /* RF *************************************************************************/ 1790 1791 /* 1792 * PCI hotplug (for wlan rfkill) 1793 */ 1794 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus) 1795 { 1796 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN); 1797 1798 if (result < 0) 1799 return false; 1800 return !result; 1801 } 1802 1803 static void asus_rfkill_hotplug(struct asus_wmi *asus) 1804 { 1805 struct pci_dev *dev; 1806 struct pci_bus *bus; 1807 bool blocked; 1808 bool absent; 1809 u32 l; 1810 1811 mutex_lock(&asus->wmi_lock); 1812 blocked = asus_wlan_rfkill_blocked(asus); 1813 mutex_unlock(&asus->wmi_lock); 1814 1815 mutex_lock(&asus->hotplug_lock); 1816 pci_lock_rescan_remove(); 1817 1818 if (asus->wlan.rfkill) 1819 rfkill_set_sw_state(asus->wlan.rfkill, blocked); 1820 1821 if (asus->hotplug_slot.ops) { 1822 bus = pci_find_bus(0, 1); 1823 if (!bus) { 1824 pr_warn("Unable to find PCI bus 1?\n"); 1825 goto out_unlock; 1826 } 1827 1828 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) { 1829 pr_err("Unable to read PCI config space?\n"); 1830 goto out_unlock; 1831 } 1832 absent = (l == 0xffffffff); 1833 1834 if (blocked != absent) { 1835 pr_warn("BIOS says wireless lan is %s, but the pci device is %s\n", 1836 blocked ? "blocked" : "unblocked", 1837 absent ? "absent" : "present"); 1838 pr_warn("skipped wireless hotplug as probably inappropriate for this model\n"); 1839 goto out_unlock; 1840 } 1841 1842 if (!blocked) { 1843 dev = pci_get_slot(bus, 0); 1844 if (dev) { 1845 /* Device already present */ 1846 pci_dev_put(dev); 1847 goto out_unlock; 1848 } 1849 dev = pci_scan_single_device(bus, 0); 1850 if (dev) { 1851 pci_bus_assign_resources(bus); 1852 pci_bus_add_device(dev); 1853 } 1854 } else { 1855 dev = pci_get_slot(bus, 0); 1856 if (dev) { 1857 pci_stop_and_remove_bus_device(dev); 1858 pci_dev_put(dev); 1859 } 1860 } 1861 } 1862 1863 out_unlock: 1864 pci_unlock_rescan_remove(); 1865 mutex_unlock(&asus->hotplug_lock); 1866 } 1867 1868 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data) 1869 { 1870 struct asus_wmi *asus = data; 1871 1872 if (event != ACPI_NOTIFY_BUS_CHECK) 1873 return; 1874 1875 /* 1876 * We can't call directly asus_rfkill_hotplug because most 1877 * of the time WMBC is still being executed and not reetrant. 1878 * There is currently no way to tell ACPICA that we want this 1879 * method to be serialized, we schedule a asus_rfkill_hotplug 1880 * call later, in a safer context. 1881 */ 1882 queue_work(asus->hotplug_workqueue, &asus->hotplug_work); 1883 } 1884 1885 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node) 1886 { 1887 acpi_status status; 1888 acpi_handle handle; 1889 1890 status = acpi_get_handle(NULL, node, &handle); 1891 if (ACPI_FAILURE(status)) 1892 return -ENODEV; 1893 1894 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, 1895 asus_rfkill_notify, asus); 1896 if (ACPI_FAILURE(status)) 1897 pr_warn("Failed to register notify on %s\n", node); 1898 1899 return 0; 1900 } 1901 1902 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node) 1903 { 1904 acpi_status status = AE_OK; 1905 acpi_handle handle; 1906 1907 status = acpi_get_handle(NULL, node, &handle); 1908 if (ACPI_FAILURE(status)) 1909 return; 1910 1911 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, 1912 asus_rfkill_notify); 1913 if (ACPI_FAILURE(status)) 1914 pr_err("Error removing rfkill notify handler %s\n", node); 1915 } 1916 1917 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot, 1918 u8 *value) 1919 { 1920 struct asus_wmi *asus = container_of(hotplug_slot, 1921 struct asus_wmi, hotplug_slot); 1922 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN); 1923 1924 if (result < 0) 1925 return result; 1926 1927 *value = !!result; 1928 return 0; 1929 } 1930 1931 static const struct hotplug_slot_ops asus_hotplug_slot_ops = { 1932 .get_adapter_status = asus_get_adapter_status, 1933 .get_power_status = asus_get_adapter_status, 1934 }; 1935 1936 static void asus_hotplug_work(struct work_struct *work) 1937 { 1938 struct asus_wmi *asus; 1939 1940 asus = container_of(work, struct asus_wmi, hotplug_work); 1941 asus_rfkill_hotplug(asus); 1942 } 1943 1944 static int asus_setup_pci_hotplug(struct asus_wmi *asus) 1945 { 1946 int ret = -ENOMEM; 1947 struct pci_bus *bus = pci_find_bus(0, 1); 1948 1949 if (!bus) { 1950 pr_err("Unable to find wifi PCI bus\n"); 1951 return -ENODEV; 1952 } 1953 1954 asus->hotplug_workqueue = 1955 create_singlethread_workqueue("hotplug_workqueue"); 1956 if (!asus->hotplug_workqueue) 1957 goto error_workqueue; 1958 1959 INIT_WORK(&asus->hotplug_work, asus_hotplug_work); 1960 1961 asus->hotplug_slot.ops = &asus_hotplug_slot_ops; 1962 1963 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi"); 1964 if (ret) { 1965 pr_err("Unable to register hotplug slot - %d\n", ret); 1966 goto error_register; 1967 } 1968 1969 return 0; 1970 1971 error_register: 1972 asus->hotplug_slot.ops = NULL; 1973 destroy_workqueue(asus->hotplug_workqueue); 1974 error_workqueue: 1975 return ret; 1976 } 1977 1978 /* 1979 * Rfkill devices 1980 */ 1981 static int asus_rfkill_set(void *data, bool blocked) 1982 { 1983 struct asus_rfkill *priv = data; 1984 u32 ctrl_param = !blocked; 1985 u32 dev_id = priv->dev_id; 1986 1987 /* 1988 * If the user bit is set, BIOS can't set and record the wlan status, 1989 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED 1990 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN). 1991 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED 1992 * while setting the wlan status through WMI. 1993 * This is also the behavior that windows app will do. 1994 */ 1995 if ((dev_id == ASUS_WMI_DEVID_WLAN) && 1996 priv->asus->driver->wlan_ctrl_by_user) 1997 dev_id = ASUS_WMI_DEVID_WLAN_LED; 1998 1999 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL); 2000 } 2001 2002 static void asus_rfkill_query(struct rfkill *rfkill, void *data) 2003 { 2004 struct asus_rfkill *priv = data; 2005 int result; 2006 2007 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id); 2008 2009 if (result < 0) 2010 return; 2011 2012 rfkill_set_sw_state(priv->rfkill, !result); 2013 } 2014 2015 static int asus_rfkill_wlan_set(void *data, bool blocked) 2016 { 2017 struct asus_rfkill *priv = data; 2018 struct asus_wmi *asus = priv->asus; 2019 int ret; 2020 2021 /* 2022 * This handler is enabled only if hotplug is enabled. 2023 * In this case, the asus_wmi_set_devstate() will 2024 * trigger a wmi notification and we need to wait 2025 * this call to finish before being able to call 2026 * any wmi method 2027 */ 2028 mutex_lock(&asus->wmi_lock); 2029 ret = asus_rfkill_set(data, blocked); 2030 mutex_unlock(&asus->wmi_lock); 2031 return ret; 2032 } 2033 2034 static const struct rfkill_ops asus_rfkill_wlan_ops = { 2035 .set_block = asus_rfkill_wlan_set, 2036 .query = asus_rfkill_query, 2037 }; 2038 2039 static const struct rfkill_ops asus_rfkill_ops = { 2040 .set_block = asus_rfkill_set, 2041 .query = asus_rfkill_query, 2042 }; 2043 2044 static int asus_new_rfkill(struct asus_wmi *asus, 2045 struct asus_rfkill *arfkill, 2046 const char *name, enum rfkill_type type, int dev_id) 2047 { 2048 int result = asus_wmi_get_devstate_simple(asus, dev_id); 2049 struct rfkill **rfkill = &arfkill->rfkill; 2050 2051 if (result < 0) 2052 return result; 2053 2054 arfkill->dev_id = dev_id; 2055 arfkill->asus = asus; 2056 2057 if (dev_id == ASUS_WMI_DEVID_WLAN && 2058 asus->driver->quirks->hotplug_wireless) 2059 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type, 2060 &asus_rfkill_wlan_ops, arfkill); 2061 else 2062 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type, 2063 &asus_rfkill_ops, arfkill); 2064 2065 if (!*rfkill) 2066 return -EINVAL; 2067 2068 if ((dev_id == ASUS_WMI_DEVID_WLAN) && 2069 (asus->driver->quirks->wapf > 0)) 2070 rfkill_set_led_trigger_name(*rfkill, "asus-wlan"); 2071 2072 rfkill_init_sw_state(*rfkill, !result); 2073 result = rfkill_register(*rfkill); 2074 if (result) { 2075 rfkill_destroy(*rfkill); 2076 *rfkill = NULL; 2077 return result; 2078 } 2079 return 0; 2080 } 2081 2082 static void asus_wmi_rfkill_exit(struct asus_wmi *asus) 2083 { 2084 if (asus->driver->wlan_ctrl_by_user && ashs_present()) 2085 return; 2086 2087 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5"); 2088 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6"); 2089 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7"); 2090 if (asus->wlan.rfkill) { 2091 rfkill_unregister(asus->wlan.rfkill); 2092 rfkill_destroy(asus->wlan.rfkill); 2093 asus->wlan.rfkill = NULL; 2094 } 2095 /* 2096 * Refresh pci hotplug in case the rfkill state was changed after 2097 * asus_unregister_rfkill_notifier() 2098 */ 2099 asus_rfkill_hotplug(asus); 2100 if (asus->hotplug_slot.ops) 2101 pci_hp_deregister(&asus->hotplug_slot); 2102 if (asus->hotplug_workqueue) 2103 destroy_workqueue(asus->hotplug_workqueue); 2104 2105 if (asus->bluetooth.rfkill) { 2106 rfkill_unregister(asus->bluetooth.rfkill); 2107 rfkill_destroy(asus->bluetooth.rfkill); 2108 asus->bluetooth.rfkill = NULL; 2109 } 2110 if (asus->wimax.rfkill) { 2111 rfkill_unregister(asus->wimax.rfkill); 2112 rfkill_destroy(asus->wimax.rfkill); 2113 asus->wimax.rfkill = NULL; 2114 } 2115 if (asus->wwan3g.rfkill) { 2116 rfkill_unregister(asus->wwan3g.rfkill); 2117 rfkill_destroy(asus->wwan3g.rfkill); 2118 asus->wwan3g.rfkill = NULL; 2119 } 2120 if (asus->gps.rfkill) { 2121 rfkill_unregister(asus->gps.rfkill); 2122 rfkill_destroy(asus->gps.rfkill); 2123 asus->gps.rfkill = NULL; 2124 } 2125 if (asus->uwb.rfkill) { 2126 rfkill_unregister(asus->uwb.rfkill); 2127 rfkill_destroy(asus->uwb.rfkill); 2128 asus->uwb.rfkill = NULL; 2129 } 2130 } 2131 2132 static int asus_wmi_rfkill_init(struct asus_wmi *asus) 2133 { 2134 int result = 0; 2135 2136 mutex_init(&asus->hotplug_lock); 2137 mutex_init(&asus->wmi_lock); 2138 2139 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan", 2140 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN); 2141 2142 if (result && result != -ENODEV) 2143 goto exit; 2144 2145 result = asus_new_rfkill(asus, &asus->bluetooth, 2146 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH, 2147 ASUS_WMI_DEVID_BLUETOOTH); 2148 2149 if (result && result != -ENODEV) 2150 goto exit; 2151 2152 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax", 2153 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX); 2154 2155 if (result && result != -ENODEV) 2156 goto exit; 2157 2158 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g", 2159 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G); 2160 2161 if (result && result != -ENODEV) 2162 goto exit; 2163 2164 result = asus_new_rfkill(asus, &asus->gps, "asus-gps", 2165 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS); 2166 2167 if (result && result != -ENODEV) 2168 goto exit; 2169 2170 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb", 2171 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB); 2172 2173 if (result && result != -ENODEV) 2174 goto exit; 2175 2176 if (!asus->driver->quirks->hotplug_wireless) 2177 goto exit; 2178 2179 result = asus_setup_pci_hotplug(asus); 2180 /* 2181 * If we get -EBUSY then something else is handling the PCI hotplug - 2182 * don't fail in this case 2183 */ 2184 if (result == -EBUSY) 2185 result = 0; 2186 2187 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5"); 2188 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6"); 2189 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7"); 2190 /* 2191 * Refresh pci hotplug in case the rfkill state was changed during 2192 * setup. 2193 */ 2194 asus_rfkill_hotplug(asus); 2195 2196 exit: 2197 if (result && result != -ENODEV) 2198 asus_wmi_rfkill_exit(asus); 2199 2200 if (result == -ENODEV) 2201 result = 0; 2202 2203 return result; 2204 } 2205 2206 /* Panel Overdrive ************************************************************/ 2207 static ssize_t panel_od_show(struct device *dev, 2208 struct device_attribute *attr, char *buf) 2209 { 2210 struct asus_wmi *asus = dev_get_drvdata(dev); 2211 int result; 2212 2213 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_PANEL_OD); 2214 if (result < 0) 2215 return result; 2216 2217 return sysfs_emit(buf, "%d\n", result); 2218 } 2219 2220 static ssize_t panel_od_store(struct device *dev, 2221 struct device_attribute *attr, 2222 const char *buf, size_t count) 2223 { 2224 int result, err; 2225 u32 overdrive; 2226 2227 struct asus_wmi *asus = dev_get_drvdata(dev); 2228 2229 result = kstrtou32(buf, 10, &overdrive); 2230 if (result) 2231 return result; 2232 2233 if (overdrive > 1) 2234 return -EINVAL; 2235 2236 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, overdrive, &result); 2237 2238 if (err) { 2239 pr_warn("Failed to set panel overdrive: %d\n", err); 2240 return err; 2241 } 2242 2243 if (result > 1) { 2244 pr_warn("Failed to set panel overdrive (result): 0x%x\n", result); 2245 return -EIO; 2246 } 2247 2248 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od"); 2249 2250 return count; 2251 } 2252 static DEVICE_ATTR_RW(panel_od); 2253 2254 /* Bootup sound ***************************************************************/ 2255 2256 static ssize_t boot_sound_show(struct device *dev, 2257 struct device_attribute *attr, char *buf) 2258 { 2259 struct asus_wmi *asus = dev_get_drvdata(dev); 2260 int result; 2261 2262 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_BOOT_SOUND); 2263 if (result < 0) 2264 return result; 2265 2266 return sysfs_emit(buf, "%d\n", result); 2267 } 2268 2269 static ssize_t boot_sound_store(struct device *dev, 2270 struct device_attribute *attr, 2271 const char *buf, size_t count) 2272 { 2273 int result, err; 2274 u32 snd; 2275 2276 struct asus_wmi *asus = dev_get_drvdata(dev); 2277 2278 result = kstrtou32(buf, 10, &snd); 2279 if (result) 2280 return result; 2281 2282 if (snd > 1) 2283 return -EINVAL; 2284 2285 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BOOT_SOUND, snd, &result); 2286 if (err) { 2287 pr_warn("Failed to set boot sound: %d\n", err); 2288 return err; 2289 } 2290 2291 if (result > 1) { 2292 pr_warn("Failed to set panel boot sound (result): 0x%x\n", result); 2293 return -EIO; 2294 } 2295 2296 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "boot_sound"); 2297 2298 return count; 2299 } 2300 static DEVICE_ATTR_RW(boot_sound); 2301 2302 /* Mini-LED mode **************************************************************/ 2303 static ssize_t mini_led_mode_show(struct device *dev, 2304 struct device_attribute *attr, char *buf) 2305 { 2306 struct asus_wmi *asus = dev_get_drvdata(dev); 2307 u32 value; 2308 int err; 2309 2310 err = asus_wmi_get_devstate(asus, asus->mini_led_dev_id, &value); 2311 if (err < 0) 2312 return err; 2313 value = value & ASUS_MINI_LED_MODE_MASK; 2314 2315 /* 2316 * Remap the mode values to match previous generation mini-led. The last gen 2317 * WMI 0 == off, while on this version WMI 2 ==off (flipped). 2318 */ 2319 if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE2) { 2320 switch (value) { 2321 case ASUS_MINI_LED_2024_WEAK: 2322 value = ASUS_MINI_LED_ON; 2323 break; 2324 case ASUS_MINI_LED_2024_STRONG: 2325 value = ASUS_MINI_LED_STRONG_MODE; 2326 break; 2327 case ASUS_MINI_LED_2024_OFF: 2328 value = ASUS_MINI_LED_OFF; 2329 break; 2330 } 2331 } 2332 2333 return sysfs_emit(buf, "%d\n", value); 2334 } 2335 2336 static ssize_t mini_led_mode_store(struct device *dev, 2337 struct device_attribute *attr, 2338 const char *buf, size_t count) 2339 { 2340 int result, err; 2341 u32 mode; 2342 2343 struct asus_wmi *asus = dev_get_drvdata(dev); 2344 2345 result = kstrtou32(buf, 10, &mode); 2346 if (result) 2347 return result; 2348 2349 if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE && 2350 mode > ASUS_MINI_LED_ON) 2351 return -EINVAL; 2352 if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE2 && 2353 mode > ASUS_MINI_LED_STRONG_MODE) 2354 return -EINVAL; 2355 2356 /* 2357 * Remap the mode values so expected behaviour is the same as the last 2358 * generation of mini-LED with 0 == off, 1 == on. 2359 */ 2360 if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE2) { 2361 switch (mode) { 2362 case ASUS_MINI_LED_OFF: 2363 mode = ASUS_MINI_LED_2024_OFF; 2364 break; 2365 case ASUS_MINI_LED_ON: 2366 mode = ASUS_MINI_LED_2024_WEAK; 2367 break; 2368 case ASUS_MINI_LED_STRONG_MODE: 2369 mode = ASUS_MINI_LED_2024_STRONG; 2370 break; 2371 } 2372 } 2373 2374 err = asus_wmi_set_devstate(asus->mini_led_dev_id, mode, &result); 2375 if (err) { 2376 pr_warn("Failed to set mini-LED: %d\n", err); 2377 return err; 2378 } 2379 2380 if (result > 1) { 2381 pr_warn("Failed to set mini-LED mode (result): 0x%x\n", result); 2382 return -EIO; 2383 } 2384 2385 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "mini_led_mode"); 2386 2387 return count; 2388 } 2389 static DEVICE_ATTR_RW(mini_led_mode); 2390 2391 static ssize_t available_mini_led_mode_show(struct device *dev, 2392 struct device_attribute *attr, char *buf) 2393 { 2394 struct asus_wmi *asus = dev_get_drvdata(dev); 2395 2396 switch (asus->mini_led_dev_id) { 2397 case ASUS_WMI_DEVID_MINI_LED_MODE: 2398 return sysfs_emit(buf, "0 1\n"); 2399 case ASUS_WMI_DEVID_MINI_LED_MODE2: 2400 return sysfs_emit(buf, "0 1 2\n"); 2401 } 2402 2403 return sysfs_emit(buf, "0\n"); 2404 } 2405 2406 static DEVICE_ATTR_RO(available_mini_led_mode); 2407 2408 /* Quirks *********************************************************************/ 2409 2410 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus) 2411 { 2412 struct pci_dev *xhci_pdev; 2413 u32 orig_ports_available; 2414 u32 ports_available = asus->driver->quirks->xusb2pr; 2415 2416 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 2417 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI, 2418 NULL); 2419 2420 if (!xhci_pdev) 2421 return; 2422 2423 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR, 2424 &orig_ports_available); 2425 2426 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR, 2427 cpu_to_le32(ports_available)); 2428 2429 pci_dev_put(xhci_pdev); 2430 2431 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n", 2432 orig_ports_available, ports_available); 2433 } 2434 2435 /* 2436 * Some devices dont support or have borcken get_als method 2437 * but still support set method. 2438 */ 2439 static void asus_wmi_set_als(void) 2440 { 2441 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL); 2442 } 2443 2444 /* Hwmon device ***************************************************************/ 2445 2446 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan, 2447 int *speed) 2448 { 2449 struct agfn_fan_args args = { 2450 .agfn.len = sizeof(args), 2451 .agfn.mfun = ASUS_FAN_MFUN, 2452 .agfn.sfun = ASUS_FAN_SFUN_READ, 2453 .fan = fan, 2454 .speed = 0, 2455 }; 2456 struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; 2457 int status; 2458 2459 if (fan != 1) 2460 return -EINVAL; 2461 2462 status = asus_wmi_evaluate_method_agfn(input); 2463 2464 if (status || args.agfn.err) 2465 return -ENXIO; 2466 2467 if (speed) 2468 *speed = args.speed; 2469 2470 return 0; 2471 } 2472 2473 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan, 2474 int *speed) 2475 { 2476 struct agfn_fan_args args = { 2477 .agfn.len = sizeof(args), 2478 .agfn.mfun = ASUS_FAN_MFUN, 2479 .agfn.sfun = ASUS_FAN_SFUN_WRITE, 2480 .fan = fan, 2481 .speed = speed ? *speed : 0, 2482 }; 2483 struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; 2484 int status; 2485 2486 /* 1: for setting 1st fan's speed 0: setting auto mode */ 2487 if (fan != 1 && fan != 0) 2488 return -EINVAL; 2489 2490 status = asus_wmi_evaluate_method_agfn(input); 2491 2492 if (status || args.agfn.err) 2493 return -ENXIO; 2494 2495 if (speed && fan == 1) 2496 asus->agfn_pwm = *speed; 2497 2498 return 0; 2499 } 2500 2501 /* 2502 * Check if we can read the speed of one fan. If true we assume we can also 2503 * control it. 2504 */ 2505 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus) 2506 { 2507 int status; 2508 int speed; 2509 u32 value; 2510 2511 status = asus_agfn_fan_speed_read(asus, 1, &speed); 2512 if (status != 0) 2513 return false; 2514 2515 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value); 2516 if (status != 0) 2517 return false; 2518 2519 /* 2520 * We need to find a better way, probably using sfun, 2521 * bits or spec ... 2522 * Currently we disable it if: 2523 * - ASUS_WMI_UNSUPPORTED_METHOD is returned 2524 * - reverved bits are non-zero 2525 * - sfun and presence bit are not set 2526 */ 2527 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000 2528 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT))); 2529 } 2530 2531 static int asus_fan_set_auto(struct asus_wmi *asus) 2532 { 2533 int status; 2534 u32 retval; 2535 2536 switch (asus->fan_type) { 2537 case FAN_TYPE_SPEC83: 2538 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL, 2539 0, &retval); 2540 if (status) 2541 return status; 2542 2543 if (retval != 1) 2544 return -EIO; 2545 break; 2546 2547 case FAN_TYPE_AGFN: 2548 status = asus_agfn_fan_speed_write(asus, 0, NULL); 2549 if (status) 2550 return -ENXIO; 2551 break; 2552 2553 default: 2554 return -ENXIO; 2555 } 2556 2557 /* 2558 * Modern models like the G713 also have GPU fan control (this is not AGFN) 2559 */ 2560 if (asus->gpu_fan_type == FAN_TYPE_SPEC83) { 2561 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL, 2562 0, &retval); 2563 if (status) 2564 return status; 2565 2566 if (retval != 1) 2567 return -EIO; 2568 } 2569 2570 return 0; 2571 } 2572 2573 static ssize_t pwm1_show(struct device *dev, 2574 struct device_attribute *attr, 2575 char *buf) 2576 { 2577 struct asus_wmi *asus = dev_get_drvdata(dev); 2578 int err; 2579 int value; 2580 2581 /* If we already set a value then just return it */ 2582 if (asus->agfn_pwm >= 0) 2583 return sysfs_emit(buf, "%d\n", asus->agfn_pwm); 2584 2585 /* 2586 * If we haven't set already set a value through the AGFN interface, 2587 * we read a current value through the (now-deprecated) FAN_CTRL device. 2588 */ 2589 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value); 2590 if (err < 0) 2591 return err; 2592 2593 value &= 0xFF; 2594 2595 if (value == 1) /* Low Speed */ 2596 value = 85; 2597 else if (value == 2) 2598 value = 170; 2599 else if (value == 3) 2600 value = 255; 2601 else if (value) { 2602 pr_err("Unknown fan speed %#x\n", value); 2603 value = -1; 2604 } 2605 2606 return sysfs_emit(buf, "%d\n", value); 2607 } 2608 2609 static ssize_t pwm1_store(struct device *dev, 2610 struct device_attribute *attr, 2611 const char *buf, size_t count) { 2612 struct asus_wmi *asus = dev_get_drvdata(dev); 2613 int value; 2614 int state; 2615 int ret; 2616 2617 ret = kstrtouint(buf, 10, &value); 2618 if (ret) 2619 return ret; 2620 2621 value = clamp(value, 0, 255); 2622 2623 state = asus_agfn_fan_speed_write(asus, 1, &value); 2624 if (state) 2625 pr_warn("Setting fan speed failed: %d\n", state); 2626 else 2627 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL; 2628 2629 return count; 2630 } 2631 2632 static ssize_t fan1_input_show(struct device *dev, 2633 struct device_attribute *attr, 2634 char *buf) 2635 { 2636 struct asus_wmi *asus = dev_get_drvdata(dev); 2637 int value; 2638 int ret; 2639 2640 switch (asus->fan_type) { 2641 case FAN_TYPE_SPEC83: 2642 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL, 2643 &value); 2644 if (ret < 0) 2645 return ret; 2646 2647 value &= 0xffff; 2648 break; 2649 2650 case FAN_TYPE_AGFN: 2651 /* no speed readable on manual mode */ 2652 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL) 2653 return -ENXIO; 2654 2655 ret = asus_agfn_fan_speed_read(asus, 1, &value); 2656 if (ret) { 2657 pr_warn("reading fan speed failed: %d\n", ret); 2658 return -ENXIO; 2659 } 2660 break; 2661 2662 default: 2663 return -ENXIO; 2664 } 2665 2666 return sysfs_emit(buf, "%d\n", value < 0 ? -1 : value * 100); 2667 } 2668 2669 static ssize_t pwm1_enable_show(struct device *dev, 2670 struct device_attribute *attr, 2671 char *buf) 2672 { 2673 struct asus_wmi *asus = dev_get_drvdata(dev); 2674 2675 /* 2676 * Just read back the cached pwm mode. 2677 * 2678 * For the CPU_FAN device, the spec indicates that we should be 2679 * able to read the device status and consult bit 19 to see if we 2680 * are in Full On or Automatic mode. However, this does not work 2681 * in practice on X532FL at least (the bit is always 0) and there's 2682 * also nothing in the DSDT to indicate that this behaviour exists. 2683 */ 2684 return sysfs_emit(buf, "%d\n", asus->fan_pwm_mode); 2685 } 2686 2687 static ssize_t pwm1_enable_store(struct device *dev, 2688 struct device_attribute *attr, 2689 const char *buf, size_t count) 2690 { 2691 struct asus_wmi *asus = dev_get_drvdata(dev); 2692 int status = 0; 2693 int state; 2694 int value; 2695 int ret; 2696 u32 retval; 2697 2698 ret = kstrtouint(buf, 10, &state); 2699 if (ret) 2700 return ret; 2701 2702 if (asus->fan_type == FAN_TYPE_SPEC83) { 2703 switch (state) { /* standard documented hwmon values */ 2704 case ASUS_FAN_CTRL_FULLSPEED: 2705 value = 1; 2706 break; 2707 case ASUS_FAN_CTRL_AUTO: 2708 value = 0; 2709 break; 2710 default: 2711 return -EINVAL; 2712 } 2713 2714 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL, 2715 value, &retval); 2716 if (ret) 2717 return ret; 2718 2719 if (retval != 1) 2720 return -EIO; 2721 } else if (asus->fan_type == FAN_TYPE_AGFN) { 2722 switch (state) { 2723 case ASUS_FAN_CTRL_MANUAL: 2724 break; 2725 2726 case ASUS_FAN_CTRL_AUTO: 2727 status = asus_fan_set_auto(asus); 2728 if (status) 2729 return status; 2730 break; 2731 2732 default: 2733 return -EINVAL; 2734 } 2735 } 2736 2737 asus->fan_pwm_mode = state; 2738 2739 /* Must set to disabled if mode is toggled */ 2740 if (asus->cpu_fan_curve_available) 2741 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false; 2742 if (asus->gpu_fan_curve_available) 2743 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false; 2744 if (asus->mid_fan_curve_available) 2745 asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false; 2746 2747 return count; 2748 } 2749 2750 static ssize_t asus_hwmon_temp1(struct device *dev, 2751 struct device_attribute *attr, 2752 char *buf) 2753 { 2754 struct asus_wmi *asus = dev_get_drvdata(dev); 2755 u32 value; 2756 int err; 2757 2758 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value); 2759 if (err < 0) 2760 return err; 2761 2762 return sysfs_emit(buf, "%ld\n", 2763 deci_kelvin_to_millicelsius(value & 0xFFFF)); 2764 } 2765 2766 /* GPU fan on modern ROG laptops */ 2767 static ssize_t fan2_input_show(struct device *dev, 2768 struct device_attribute *attr, 2769 char *buf) 2770 { 2771 struct asus_wmi *asus = dev_get_drvdata(dev); 2772 int value; 2773 int ret; 2774 2775 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL, &value); 2776 if (ret < 0) 2777 return ret; 2778 2779 value &= 0xffff; 2780 2781 return sysfs_emit(buf, "%d\n", value * 100); 2782 } 2783 2784 /* Middle/Center fan on modern ROG laptops */ 2785 static ssize_t fan3_input_show(struct device *dev, 2786 struct device_attribute *attr, 2787 char *buf) 2788 { 2789 struct asus_wmi *asus = dev_get_drvdata(dev); 2790 int value; 2791 int ret; 2792 2793 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_MID_FAN_CTRL, &value); 2794 if (ret < 0) 2795 return ret; 2796 2797 value &= 0xffff; 2798 2799 return sysfs_emit(buf, "%d\n", value * 100); 2800 } 2801 2802 static ssize_t pwm2_enable_show(struct device *dev, 2803 struct device_attribute *attr, 2804 char *buf) 2805 { 2806 struct asus_wmi *asus = dev_get_drvdata(dev); 2807 2808 return sysfs_emit(buf, "%d\n", asus->gpu_fan_pwm_mode); 2809 } 2810 2811 static ssize_t pwm2_enable_store(struct device *dev, 2812 struct device_attribute *attr, 2813 const char *buf, size_t count) 2814 { 2815 struct asus_wmi *asus = dev_get_drvdata(dev); 2816 int state; 2817 int value; 2818 int ret; 2819 u32 retval; 2820 2821 ret = kstrtouint(buf, 10, &state); 2822 if (ret) 2823 return ret; 2824 2825 switch (state) { /* standard documented hwmon values */ 2826 case ASUS_FAN_CTRL_FULLSPEED: 2827 value = 1; 2828 break; 2829 case ASUS_FAN_CTRL_AUTO: 2830 value = 0; 2831 break; 2832 default: 2833 return -EINVAL; 2834 } 2835 2836 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL, 2837 value, &retval); 2838 if (ret) 2839 return ret; 2840 2841 if (retval != 1) 2842 return -EIO; 2843 2844 asus->gpu_fan_pwm_mode = state; 2845 return count; 2846 } 2847 2848 static ssize_t pwm3_enable_show(struct device *dev, 2849 struct device_attribute *attr, 2850 char *buf) 2851 { 2852 struct asus_wmi *asus = dev_get_drvdata(dev); 2853 2854 return sysfs_emit(buf, "%d\n", asus->mid_fan_pwm_mode); 2855 } 2856 2857 static ssize_t pwm3_enable_store(struct device *dev, 2858 struct device_attribute *attr, 2859 const char *buf, size_t count) 2860 { 2861 struct asus_wmi *asus = dev_get_drvdata(dev); 2862 int state; 2863 int value; 2864 int ret; 2865 u32 retval; 2866 2867 ret = kstrtouint(buf, 10, &state); 2868 if (ret) 2869 return ret; 2870 2871 switch (state) { /* standard documented hwmon values */ 2872 case ASUS_FAN_CTRL_FULLSPEED: 2873 value = 1; 2874 break; 2875 case ASUS_FAN_CTRL_AUTO: 2876 value = 0; 2877 break; 2878 default: 2879 return -EINVAL; 2880 } 2881 2882 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_MID_FAN_CTRL, 2883 value, &retval); 2884 if (ret) 2885 return ret; 2886 2887 if (retval != 1) 2888 return -EIO; 2889 2890 asus->mid_fan_pwm_mode = state; 2891 return count; 2892 } 2893 2894 /* Fan1 */ 2895 static DEVICE_ATTR_RW(pwm1); 2896 static DEVICE_ATTR_RW(pwm1_enable); 2897 static DEVICE_ATTR_RO(fan1_input); 2898 static DEVICE_STRING_ATTR_RO(fan1_label, 0444, ASUS_FAN_DESC); 2899 2900 /* Fan2 - GPU fan */ 2901 static DEVICE_ATTR_RW(pwm2_enable); 2902 static DEVICE_ATTR_RO(fan2_input); 2903 static DEVICE_STRING_ATTR_RO(fan2_label, 0444, ASUS_GPU_FAN_DESC); 2904 /* Fan3 - Middle/center fan */ 2905 static DEVICE_ATTR_RW(pwm3_enable); 2906 static DEVICE_ATTR_RO(fan3_input); 2907 static DEVICE_STRING_ATTR_RO(fan3_label, 0444, ASUS_MID_FAN_DESC); 2908 2909 /* Temperature */ 2910 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL); 2911 2912 static struct attribute *hwmon_attributes[] = { 2913 &dev_attr_pwm1.attr, 2914 &dev_attr_pwm1_enable.attr, 2915 &dev_attr_pwm2_enable.attr, 2916 &dev_attr_pwm3_enable.attr, 2917 &dev_attr_fan1_input.attr, 2918 &dev_attr_fan1_label.attr.attr, 2919 &dev_attr_fan2_input.attr, 2920 &dev_attr_fan2_label.attr.attr, 2921 &dev_attr_fan3_input.attr, 2922 &dev_attr_fan3_label.attr.attr, 2923 2924 &dev_attr_temp1_input.attr, 2925 NULL 2926 }; 2927 2928 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj, 2929 struct attribute *attr, int idx) 2930 { 2931 struct device *dev = kobj_to_dev(kobj); 2932 struct asus_wmi *asus = dev_get_drvdata(dev->parent); 2933 u32 value = ASUS_WMI_UNSUPPORTED_METHOD; 2934 2935 if (attr == &dev_attr_pwm1.attr) { 2936 if (asus->fan_type != FAN_TYPE_AGFN) 2937 return 0; 2938 } else if (attr == &dev_attr_fan1_input.attr 2939 || attr == &dev_attr_fan1_label.attr.attr 2940 || attr == &dev_attr_pwm1_enable.attr) { 2941 if (asus->fan_type == FAN_TYPE_NONE) 2942 return 0; 2943 } else if (attr == &dev_attr_fan2_input.attr 2944 || attr == &dev_attr_fan2_label.attr.attr 2945 || attr == &dev_attr_pwm2_enable.attr) { 2946 if (asus->gpu_fan_type == FAN_TYPE_NONE) 2947 return 0; 2948 } else if (attr == &dev_attr_fan3_input.attr 2949 || attr == &dev_attr_fan3_label.attr.attr 2950 || attr == &dev_attr_pwm3_enable.attr) { 2951 if (asus->mid_fan_type == FAN_TYPE_NONE) 2952 return 0; 2953 } else if (attr == &dev_attr_temp1_input.attr) { 2954 int err = asus_wmi_get_devstate(asus, 2955 ASUS_WMI_DEVID_THERMAL_CTRL, 2956 &value); 2957 2958 if (err < 0) 2959 return 0; /* can't return negative here */ 2960 2961 /* 2962 * If the temperature value in deci-Kelvin is near the absolute 2963 * zero temperature, something is clearly wrong 2964 */ 2965 if (value == 0 || value == 1) 2966 return 0; 2967 } 2968 2969 return attr->mode; 2970 } 2971 2972 static const struct attribute_group hwmon_attribute_group = { 2973 .is_visible = asus_hwmon_sysfs_is_visible, 2974 .attrs = hwmon_attributes 2975 }; 2976 __ATTRIBUTE_GROUPS(hwmon_attribute); 2977 2978 static int asus_wmi_hwmon_init(struct asus_wmi *asus) 2979 { 2980 struct device *dev = &asus->platform_device->dev; 2981 struct device *hwmon; 2982 2983 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus, 2984 hwmon_attribute_groups); 2985 2986 if (IS_ERR(hwmon)) { 2987 pr_err("Could not register asus hwmon device\n"); 2988 return PTR_ERR(hwmon); 2989 } 2990 return 0; 2991 } 2992 2993 static int asus_wmi_fan_init(struct asus_wmi *asus) 2994 { 2995 asus->gpu_fan_type = FAN_TYPE_NONE; 2996 asus->mid_fan_type = FAN_TYPE_NONE; 2997 asus->fan_type = FAN_TYPE_NONE; 2998 asus->agfn_pwm = -1; 2999 3000 if (asus->driver->quirks->wmi_ignore_fan) 3001 asus->fan_type = FAN_TYPE_NONE; 3002 else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL)) 3003 asus->fan_type = FAN_TYPE_SPEC83; 3004 else if (asus_wmi_has_agfn_fan(asus)) 3005 asus->fan_type = FAN_TYPE_AGFN; 3006 3007 /* Modern models like G713 also have GPU fan control */ 3008 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL)) 3009 asus->gpu_fan_type = FAN_TYPE_SPEC83; 3010 3011 /* Some models also have a center/middle fan */ 3012 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MID_FAN_CTRL)) 3013 asus->mid_fan_type = FAN_TYPE_SPEC83; 3014 3015 if (asus->fan_type == FAN_TYPE_NONE) 3016 return -ENODEV; 3017 3018 asus_fan_set_auto(asus); 3019 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO; 3020 return 0; 3021 } 3022 3023 /* Fan mode *******************************************************************/ 3024 3025 static int fan_boost_mode_check_present(struct asus_wmi *asus) 3026 { 3027 u32 result; 3028 int err; 3029 3030 asus->fan_boost_mode_available = false; 3031 3032 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE, 3033 &result); 3034 if (err) { 3035 if (err == -ENODEV) 3036 return 0; 3037 else 3038 return err; 3039 } 3040 3041 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) && 3042 (result & ASUS_FAN_BOOST_MODES_MASK)) { 3043 asus->fan_boost_mode_available = true; 3044 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK; 3045 } 3046 3047 return 0; 3048 } 3049 3050 static int fan_boost_mode_write(struct asus_wmi *asus) 3051 { 3052 u32 retval; 3053 u8 value; 3054 int err; 3055 3056 value = asus->fan_boost_mode; 3057 3058 pr_info("Set fan boost mode: %u\n", value); 3059 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value, 3060 &retval); 3061 3062 sysfs_notify(&asus->platform_device->dev.kobj, NULL, 3063 "fan_boost_mode"); 3064 3065 if (err) { 3066 pr_warn("Failed to set fan boost mode: %d\n", err); 3067 return err; 3068 } 3069 3070 if (retval != 1) { 3071 pr_warn("Failed to set fan boost mode (retval): 0x%x\n", 3072 retval); 3073 return -EIO; 3074 } 3075 3076 return 0; 3077 } 3078 3079 static int fan_boost_mode_switch_next(struct asus_wmi *asus) 3080 { 3081 u8 mask = asus->fan_boost_mode_mask; 3082 3083 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) { 3084 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK) 3085 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST; 3086 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK) 3087 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT; 3088 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) { 3089 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK) 3090 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT; 3091 else 3092 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL; 3093 } else { 3094 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL; 3095 } 3096 3097 return fan_boost_mode_write(asus); 3098 } 3099 3100 static ssize_t fan_boost_mode_show(struct device *dev, 3101 struct device_attribute *attr, char *buf) 3102 { 3103 struct asus_wmi *asus = dev_get_drvdata(dev); 3104 3105 return sysfs_emit(buf, "%d\n", asus->fan_boost_mode); 3106 } 3107 3108 static ssize_t fan_boost_mode_store(struct device *dev, 3109 struct device_attribute *attr, 3110 const char *buf, size_t count) 3111 { 3112 struct asus_wmi *asus = dev_get_drvdata(dev); 3113 u8 mask = asus->fan_boost_mode_mask; 3114 u8 new_mode; 3115 int result; 3116 3117 result = kstrtou8(buf, 10, &new_mode); 3118 if (result < 0) { 3119 pr_warn("Trying to store invalid value\n"); 3120 return result; 3121 } 3122 3123 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) { 3124 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)) 3125 return -EINVAL; 3126 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) { 3127 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)) 3128 return -EINVAL; 3129 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) { 3130 return -EINVAL; 3131 } 3132 3133 asus->fan_boost_mode = new_mode; 3134 fan_boost_mode_write(asus); 3135 3136 return count; 3137 } 3138 3139 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent 3140 static DEVICE_ATTR_RW(fan_boost_mode); 3141 3142 /* Custom fan curves **********************************************************/ 3143 3144 static void fan_curve_copy_from_buf(struct fan_curve_data *data, u8 *buf) 3145 { 3146 int i; 3147 3148 for (i = 0; i < FAN_CURVE_POINTS; i++) { 3149 data->temps[i] = buf[i]; 3150 } 3151 3152 for (i = 0; i < FAN_CURVE_POINTS; i++) { 3153 data->percents[i] = 3154 255 * buf[i + FAN_CURVE_POINTS] / 100; 3155 } 3156 } 3157 3158 static int fan_curve_get_factory_default(struct asus_wmi *asus, u32 fan_dev) 3159 { 3160 struct fan_curve_data *curves; 3161 u8 buf[FAN_CURVE_BUF_LEN]; 3162 int err, fan_idx; 3163 u8 mode = 0; 3164 3165 if (asus->throttle_thermal_policy_available) 3166 mode = asus->throttle_thermal_policy_mode; 3167 /* DEVID_<C/G>PU_FAN_CURVE is switched for OVERBOOST vs SILENT */ 3168 if (mode == 2) 3169 mode = 1; 3170 else if (mode == 1) 3171 mode = 2; 3172 3173 err = asus_wmi_evaluate_method_buf(asus->dsts_id, fan_dev, mode, buf, 3174 FAN_CURVE_BUF_LEN); 3175 if (err) { 3176 pr_warn("%s (0x%08x) failed: %d\n", __func__, fan_dev, err); 3177 return err; 3178 } 3179 3180 fan_idx = FAN_CURVE_DEV_CPU; 3181 if (fan_dev == ASUS_WMI_DEVID_GPU_FAN_CURVE) 3182 fan_idx = FAN_CURVE_DEV_GPU; 3183 3184 if (fan_dev == ASUS_WMI_DEVID_MID_FAN_CURVE) 3185 fan_idx = FAN_CURVE_DEV_MID; 3186 3187 curves = &asus->custom_fan_curves[fan_idx]; 3188 curves->device_id = fan_dev; 3189 3190 fan_curve_copy_from_buf(curves, buf); 3191 return 0; 3192 } 3193 3194 /* Check if capability exists, and populate defaults */ 3195 static int fan_curve_check_present(struct asus_wmi *asus, bool *available, 3196 u32 fan_dev) 3197 { 3198 int err; 3199 3200 *available = false; 3201 3202 if (asus->fan_type == FAN_TYPE_NONE) 3203 return 0; 3204 3205 err = fan_curve_get_factory_default(asus, fan_dev); 3206 if (err) { 3207 return 0; 3208 } 3209 3210 *available = true; 3211 return 0; 3212 } 3213 3214 /* Determine which fan the attribute is for if SENSOR_ATTR */ 3215 static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus, 3216 struct device_attribute *attr) 3217 { 3218 int index = to_sensor_dev_attr(attr)->index; 3219 3220 return &asus->custom_fan_curves[index]; 3221 } 3222 3223 /* Determine which fan the attribute is for if SENSOR_ATTR_2 */ 3224 static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus, 3225 struct device_attribute *attr) 3226 { 3227 int nr = to_sensor_dev_attr_2(attr)->nr; 3228 3229 return &asus->custom_fan_curves[nr & ~FAN_CURVE_PWM_MASK]; 3230 } 3231 3232 static ssize_t fan_curve_show(struct device *dev, 3233 struct device_attribute *attr, char *buf) 3234 { 3235 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr); 3236 struct asus_wmi *asus = dev_get_drvdata(dev); 3237 struct fan_curve_data *data; 3238 int value, pwm, index; 3239 3240 data = fan_curve_attr_2_select(asus, attr); 3241 pwm = dev_attr->nr & FAN_CURVE_PWM_MASK; 3242 index = dev_attr->index; 3243 3244 if (pwm) 3245 value = data->percents[index]; 3246 else 3247 value = data->temps[index]; 3248 3249 return sysfs_emit(buf, "%d\n", value); 3250 } 3251 3252 /* 3253 * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE. 3254 */ 3255 static int fan_curve_write(struct asus_wmi *asus, 3256 struct fan_curve_data *data) 3257 { 3258 u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0; 3259 u8 *percents = data->percents; 3260 u8 *temps = data->temps; 3261 int ret, i, shift = 0; 3262 3263 if (!data->enabled) 3264 return 0; 3265 3266 for (i = 0; i < FAN_CURVE_POINTS / 2; i++) { 3267 arg1 += (temps[i]) << shift; 3268 arg2 += (temps[i + 4]) << shift; 3269 /* Scale to percentage for device */ 3270 arg3 += (100 * percents[i] / 255) << shift; 3271 arg4 += (100 * percents[i + 4] / 255) << shift; 3272 shift += 8; 3273 } 3274 3275 return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS, 3276 data->device_id, 3277 arg1, arg2, arg3, arg4, &ret); 3278 } 3279 3280 static ssize_t fan_curve_store(struct device *dev, 3281 struct device_attribute *attr, const char *buf, 3282 size_t count) 3283 { 3284 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr); 3285 struct asus_wmi *asus = dev_get_drvdata(dev); 3286 struct fan_curve_data *data; 3287 int err, pwm, index; 3288 u8 value; 3289 3290 data = fan_curve_attr_2_select(asus, attr); 3291 pwm = dev_attr->nr & FAN_CURVE_PWM_MASK; 3292 index = dev_attr->index; 3293 3294 err = kstrtou8(buf, 10, &value); 3295 if (err < 0) 3296 return err; 3297 3298 if (pwm) 3299 data->percents[index] = value; 3300 else 3301 data->temps[index] = value; 3302 3303 /* 3304 * Mark as disabled so the user has to explicitly enable to apply a 3305 * changed fan curve. This prevents potential lockups from writing out 3306 * many changes as one-write-per-change. 3307 */ 3308 data->enabled = false; 3309 3310 return count; 3311 } 3312 3313 static ssize_t fan_curve_enable_show(struct device *dev, 3314 struct device_attribute *attr, char *buf) 3315 { 3316 struct asus_wmi *asus = dev_get_drvdata(dev); 3317 struct fan_curve_data *data; 3318 int out = 2; 3319 3320 data = fan_curve_attr_select(asus, attr); 3321 3322 if (data->enabled) 3323 out = 1; 3324 3325 return sysfs_emit(buf, "%d\n", out); 3326 } 3327 3328 static ssize_t fan_curve_enable_store(struct device *dev, 3329 struct device_attribute *attr, 3330 const char *buf, size_t count) 3331 { 3332 struct asus_wmi *asus = dev_get_drvdata(dev); 3333 struct fan_curve_data *data; 3334 int value, err; 3335 3336 data = fan_curve_attr_select(asus, attr); 3337 3338 err = kstrtoint(buf, 10, &value); 3339 if (err < 0) 3340 return err; 3341 3342 switch (value) { 3343 case 1: 3344 data->enabled = true; 3345 break; 3346 case 2: 3347 data->enabled = false; 3348 break; 3349 /* 3350 * Auto + reset the fan curve data to defaults. Make it an explicit 3351 * option so that users don't accidentally overwrite a set fan curve. 3352 */ 3353 case 3: 3354 err = fan_curve_get_factory_default(asus, data->device_id); 3355 if (err) 3356 return err; 3357 data->enabled = false; 3358 break; 3359 default: 3360 return -EINVAL; 3361 } 3362 3363 if (data->enabled) { 3364 err = fan_curve_write(asus, data); 3365 if (err) 3366 return err; 3367 } else { 3368 /* 3369 * For machines with throttle this is the only way to reset fans 3370 * to default mode of operation (does not erase curve data). 3371 */ 3372 if (asus->throttle_thermal_policy_available) { 3373 err = throttle_thermal_policy_write(asus); 3374 if (err) 3375 return err; 3376 /* Similar is true for laptops with this fan */ 3377 } else if (asus->fan_type == FAN_TYPE_SPEC83) { 3378 err = asus_fan_set_auto(asus); 3379 if (err) 3380 return err; 3381 } else { 3382 /* Safeguard against fautly ACPI tables */ 3383 err = fan_curve_get_factory_default(asus, data->device_id); 3384 if (err) 3385 return err; 3386 err = fan_curve_write(asus, data); 3387 if (err) 3388 return err; 3389 } 3390 } 3391 return count; 3392 } 3393 3394 /* CPU */ 3395 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU); 3396 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve, 3397 FAN_CURVE_DEV_CPU, 0); 3398 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve, 3399 FAN_CURVE_DEV_CPU, 1); 3400 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve, 3401 FAN_CURVE_DEV_CPU, 2); 3402 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve, 3403 FAN_CURVE_DEV_CPU, 3); 3404 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve, 3405 FAN_CURVE_DEV_CPU, 4); 3406 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve, 3407 FAN_CURVE_DEV_CPU, 5); 3408 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve, 3409 FAN_CURVE_DEV_CPU, 6); 3410 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve, 3411 FAN_CURVE_DEV_CPU, 7); 3412 3413 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve, 3414 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0); 3415 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve, 3416 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1); 3417 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve, 3418 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2); 3419 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve, 3420 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3); 3421 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve, 3422 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4); 3423 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve, 3424 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5); 3425 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve, 3426 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6); 3427 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve, 3428 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7); 3429 3430 /* GPU */ 3431 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU); 3432 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve, 3433 FAN_CURVE_DEV_GPU, 0); 3434 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve, 3435 FAN_CURVE_DEV_GPU, 1); 3436 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve, 3437 FAN_CURVE_DEV_GPU, 2); 3438 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve, 3439 FAN_CURVE_DEV_GPU, 3); 3440 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve, 3441 FAN_CURVE_DEV_GPU, 4); 3442 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve, 3443 FAN_CURVE_DEV_GPU, 5); 3444 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve, 3445 FAN_CURVE_DEV_GPU, 6); 3446 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve, 3447 FAN_CURVE_DEV_GPU, 7); 3448 3449 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve, 3450 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0); 3451 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve, 3452 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1); 3453 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve, 3454 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2); 3455 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve, 3456 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3); 3457 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve, 3458 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4); 3459 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve, 3460 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5); 3461 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve, 3462 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6); 3463 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve, 3464 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7); 3465 3466 /* MID */ 3467 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, fan_curve_enable, FAN_CURVE_DEV_MID); 3468 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, fan_curve, 3469 FAN_CURVE_DEV_MID, 0); 3470 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, fan_curve, 3471 FAN_CURVE_DEV_MID, 1); 3472 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, fan_curve, 3473 FAN_CURVE_DEV_MID, 2); 3474 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, fan_curve, 3475 FAN_CURVE_DEV_MID, 3); 3476 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, fan_curve, 3477 FAN_CURVE_DEV_MID, 4); 3478 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_temp, fan_curve, 3479 FAN_CURVE_DEV_MID, 5); 3480 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_temp, fan_curve, 3481 FAN_CURVE_DEV_MID, 6); 3482 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_temp, fan_curve, 3483 FAN_CURVE_DEV_MID, 7); 3484 3485 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, fan_curve, 3486 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 0); 3487 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, fan_curve, 3488 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 1); 3489 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_pwm, fan_curve, 3490 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 2); 3491 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_pwm, fan_curve, 3492 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 3); 3493 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_pwm, fan_curve, 3494 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 4); 3495 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_pwm, fan_curve, 3496 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 5); 3497 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_pwm, fan_curve, 3498 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 6); 3499 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_pwm, fan_curve, 3500 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 7); 3501 3502 static struct attribute *asus_fan_curve_attr[] = { 3503 /* CPU */ 3504 &sensor_dev_attr_pwm1_enable.dev_attr.attr, 3505 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr, 3506 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr, 3507 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr, 3508 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr, 3509 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr, 3510 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr, 3511 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr, 3512 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr, 3513 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, 3514 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, 3515 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr, 3516 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr, 3517 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr, 3518 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr, 3519 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr, 3520 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr, 3521 /* GPU */ 3522 &sensor_dev_attr_pwm2_enable.dev_attr.attr, 3523 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr, 3524 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr, 3525 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr, 3526 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr, 3527 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr, 3528 &sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr, 3529 &sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr, 3530 &sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr, 3531 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, 3532 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, 3533 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr, 3534 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr, 3535 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr, 3536 &sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr, 3537 &sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr, 3538 &sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr, 3539 /* MID */ 3540 &sensor_dev_attr_pwm3_enable.dev_attr.attr, 3541 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr, 3542 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr, 3543 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr, 3544 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr, 3545 &sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr, 3546 &sensor_dev_attr_pwm3_auto_point6_temp.dev_attr.attr, 3547 &sensor_dev_attr_pwm3_auto_point7_temp.dev_attr.attr, 3548 &sensor_dev_attr_pwm3_auto_point8_temp.dev_attr.attr, 3549 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, 3550 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, 3551 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr, 3552 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr, 3553 &sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr, 3554 &sensor_dev_attr_pwm3_auto_point6_pwm.dev_attr.attr, 3555 &sensor_dev_attr_pwm3_auto_point7_pwm.dev_attr.attr, 3556 &sensor_dev_attr_pwm3_auto_point8_pwm.dev_attr.attr, 3557 NULL 3558 }; 3559 3560 static umode_t asus_fan_curve_is_visible(struct kobject *kobj, 3561 struct attribute *attr, int idx) 3562 { 3563 struct device *dev = kobj_to_dev(kobj); 3564 struct asus_wmi *asus = dev_get_drvdata(dev->parent); 3565 3566 /* 3567 * Check the char instead of casting attr as there are two attr types 3568 * involved here (attr1 and attr2) 3569 */ 3570 if (asus->cpu_fan_curve_available && attr->name[3] == '1') 3571 return 0644; 3572 3573 if (asus->gpu_fan_curve_available && attr->name[3] == '2') 3574 return 0644; 3575 3576 if (asus->mid_fan_curve_available && attr->name[3] == '3') 3577 return 0644; 3578 3579 return 0; 3580 } 3581 3582 static const struct attribute_group asus_fan_curve_attr_group = { 3583 .is_visible = asus_fan_curve_is_visible, 3584 .attrs = asus_fan_curve_attr, 3585 }; 3586 __ATTRIBUTE_GROUPS(asus_fan_curve_attr); 3587 3588 /* 3589 * Must be initialised after throttle_thermal_policy_check_present() as 3590 * we check the status of throttle_thermal_policy_available during init. 3591 */ 3592 static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus) 3593 { 3594 struct device *dev = &asus->platform_device->dev; 3595 struct device *hwmon; 3596 int err; 3597 3598 err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available, 3599 ASUS_WMI_DEVID_CPU_FAN_CURVE); 3600 if (err) 3601 return err; 3602 3603 err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available, 3604 ASUS_WMI_DEVID_GPU_FAN_CURVE); 3605 if (err) 3606 return err; 3607 3608 err = fan_curve_check_present(asus, &asus->mid_fan_curve_available, 3609 ASUS_WMI_DEVID_MID_FAN_CURVE); 3610 if (err) 3611 return err; 3612 3613 if (!asus->cpu_fan_curve_available 3614 && !asus->gpu_fan_curve_available 3615 && !asus->mid_fan_curve_available) 3616 return 0; 3617 3618 hwmon = devm_hwmon_device_register_with_groups( 3619 dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups); 3620 3621 if (IS_ERR(hwmon)) { 3622 dev_err(dev, 3623 "Could not register asus_custom_fan_curve device\n"); 3624 return PTR_ERR(hwmon); 3625 } 3626 3627 return 0; 3628 } 3629 3630 /* Throttle thermal policy ****************************************************/ 3631 3632 static int throttle_thermal_policy_check_present(struct asus_wmi *asus) 3633 { 3634 u32 result; 3635 int err; 3636 3637 asus->throttle_thermal_policy_available = false; 3638 3639 err = asus_wmi_get_devstate(asus, 3640 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY, 3641 &result); 3642 if (err) { 3643 if (err == -ENODEV) 3644 return 0; 3645 return err; 3646 } 3647 3648 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) 3649 asus->throttle_thermal_policy_available = true; 3650 3651 return 0; 3652 } 3653 3654 static int throttle_thermal_policy_write(struct asus_wmi *asus) 3655 { 3656 int err; 3657 u8 value; 3658 u32 retval; 3659 3660 value = asus->throttle_thermal_policy_mode; 3661 3662 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY, 3663 value, &retval); 3664 3665 sysfs_notify(&asus->platform_device->dev.kobj, NULL, 3666 "throttle_thermal_policy"); 3667 3668 if (err) { 3669 pr_warn("Failed to set throttle thermal policy: %d\n", err); 3670 return err; 3671 } 3672 3673 if (retval != 1) { 3674 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n", 3675 retval); 3676 return -EIO; 3677 } 3678 3679 /* Must set to disabled if mode is toggled */ 3680 if (asus->cpu_fan_curve_available) 3681 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false; 3682 if (asus->gpu_fan_curve_available) 3683 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false; 3684 if (asus->mid_fan_curve_available) 3685 asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false; 3686 3687 return 0; 3688 } 3689 3690 static int throttle_thermal_policy_set_default(struct asus_wmi *asus) 3691 { 3692 if (!asus->throttle_thermal_policy_available) 3693 return 0; 3694 3695 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT; 3696 return throttle_thermal_policy_write(asus); 3697 } 3698 3699 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus) 3700 { 3701 u8 new_mode = asus->throttle_thermal_policy_mode + 1; 3702 int err; 3703 3704 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT) 3705 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT; 3706 3707 asus->throttle_thermal_policy_mode = new_mode; 3708 err = throttle_thermal_policy_write(asus); 3709 if (err) 3710 return err; 3711 3712 /* 3713 * Ensure that platform_profile updates userspace with the change to ensure 3714 * that platform_profile and throttle_thermal_policy_mode are in sync. 3715 */ 3716 platform_profile_notify(); 3717 3718 return 0; 3719 } 3720 3721 static ssize_t throttle_thermal_policy_show(struct device *dev, 3722 struct device_attribute *attr, char *buf) 3723 { 3724 struct asus_wmi *asus = dev_get_drvdata(dev); 3725 u8 mode = asus->throttle_thermal_policy_mode; 3726 3727 return sysfs_emit(buf, "%d\n", mode); 3728 } 3729 3730 static ssize_t throttle_thermal_policy_store(struct device *dev, 3731 struct device_attribute *attr, 3732 const char *buf, size_t count) 3733 { 3734 struct asus_wmi *asus = dev_get_drvdata(dev); 3735 u8 new_mode; 3736 int result; 3737 int err; 3738 3739 result = kstrtou8(buf, 10, &new_mode); 3740 if (result < 0) 3741 return result; 3742 3743 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT) 3744 return -EINVAL; 3745 3746 asus->throttle_thermal_policy_mode = new_mode; 3747 err = throttle_thermal_policy_write(asus); 3748 if (err) 3749 return err; 3750 3751 /* 3752 * Ensure that platform_profile updates userspace with the change to ensure 3753 * that platform_profile and throttle_thermal_policy_mode are in sync. 3754 */ 3755 platform_profile_notify(); 3756 3757 return count; 3758 } 3759 3760 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent 3761 static DEVICE_ATTR_RW(throttle_thermal_policy); 3762 3763 /* Platform profile ***********************************************************/ 3764 static int asus_wmi_platform_profile_get(struct platform_profile_handler *pprof, 3765 enum platform_profile_option *profile) 3766 { 3767 struct asus_wmi *asus; 3768 int tp; 3769 3770 asus = container_of(pprof, struct asus_wmi, platform_profile_handler); 3771 3772 tp = asus->throttle_thermal_policy_mode; 3773 3774 switch (tp) { 3775 case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT: 3776 *profile = PLATFORM_PROFILE_BALANCED; 3777 break; 3778 case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST: 3779 *profile = PLATFORM_PROFILE_PERFORMANCE; 3780 break; 3781 case ASUS_THROTTLE_THERMAL_POLICY_SILENT: 3782 *profile = PLATFORM_PROFILE_QUIET; 3783 break; 3784 default: 3785 return -EINVAL; 3786 } 3787 3788 return 0; 3789 } 3790 3791 static int asus_wmi_platform_profile_set(struct platform_profile_handler *pprof, 3792 enum platform_profile_option profile) 3793 { 3794 struct asus_wmi *asus; 3795 int tp; 3796 3797 asus = container_of(pprof, struct asus_wmi, platform_profile_handler); 3798 3799 switch (profile) { 3800 case PLATFORM_PROFILE_PERFORMANCE: 3801 tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST; 3802 break; 3803 case PLATFORM_PROFILE_BALANCED: 3804 tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT; 3805 break; 3806 case PLATFORM_PROFILE_QUIET: 3807 tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT; 3808 break; 3809 default: 3810 return -EOPNOTSUPP; 3811 } 3812 3813 asus->throttle_thermal_policy_mode = tp; 3814 return throttle_thermal_policy_write(asus); 3815 } 3816 3817 static int platform_profile_setup(struct asus_wmi *asus) 3818 { 3819 struct device *dev = &asus->platform_device->dev; 3820 int err; 3821 3822 /* 3823 * Not an error if a component platform_profile relies on is unavailable 3824 * so early return, skipping the setup of platform_profile. 3825 */ 3826 if (!asus->throttle_thermal_policy_available) 3827 return 0; 3828 3829 dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n"); 3830 3831 asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get; 3832 asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set; 3833 3834 set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices); 3835 set_bit(PLATFORM_PROFILE_BALANCED, 3836 asus->platform_profile_handler.choices); 3837 set_bit(PLATFORM_PROFILE_PERFORMANCE, 3838 asus->platform_profile_handler.choices); 3839 3840 err = platform_profile_register(&asus->platform_profile_handler); 3841 if (err) 3842 return err; 3843 3844 asus->platform_profile_support = true; 3845 return 0; 3846 } 3847 3848 /* Backlight ******************************************************************/ 3849 3850 static int read_backlight_power(struct asus_wmi *asus) 3851 { 3852 int ret; 3853 3854 if (asus->driver->quirks->store_backlight_power) 3855 ret = !asus->driver->panel_power; 3856 else 3857 ret = asus_wmi_get_devstate_simple(asus, 3858 ASUS_WMI_DEVID_BACKLIGHT); 3859 3860 if (ret < 0) 3861 return ret; 3862 3863 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN; 3864 } 3865 3866 static int read_brightness_max(struct asus_wmi *asus) 3867 { 3868 u32 retval; 3869 int err; 3870 3871 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval); 3872 if (err < 0) 3873 return err; 3874 3875 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK; 3876 retval >>= 8; 3877 3878 if (!retval) 3879 return -ENODEV; 3880 3881 return retval; 3882 } 3883 3884 static int read_brightness(struct backlight_device *bd) 3885 { 3886 struct asus_wmi *asus = bl_get_data(bd); 3887 u32 retval; 3888 int err; 3889 3890 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval); 3891 if (err < 0) 3892 return err; 3893 3894 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK; 3895 } 3896 3897 static u32 get_scalar_command(struct backlight_device *bd) 3898 { 3899 struct asus_wmi *asus = bl_get_data(bd); 3900 u32 ctrl_param = 0; 3901 3902 if ((asus->driver->brightness < bd->props.brightness) || 3903 bd->props.brightness == bd->props.max_brightness) 3904 ctrl_param = 0x00008001; 3905 else if ((asus->driver->brightness > bd->props.brightness) || 3906 bd->props.brightness == 0) 3907 ctrl_param = 0x00008000; 3908 3909 asus->driver->brightness = bd->props.brightness; 3910 3911 return ctrl_param; 3912 } 3913 3914 static int update_bl_status(struct backlight_device *bd) 3915 { 3916 struct asus_wmi *asus = bl_get_data(bd); 3917 u32 ctrl_param; 3918 int power, err = 0; 3919 3920 power = read_backlight_power(asus); 3921 if (power != -ENODEV && bd->props.power != power) { 3922 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK); 3923 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 3924 ctrl_param, NULL); 3925 if (asus->driver->quirks->store_backlight_power) 3926 asus->driver->panel_power = bd->props.power; 3927 3928 /* When using scalar brightness, updating the brightness 3929 * will mess with the backlight power */ 3930 if (asus->driver->quirks->scalar_panel_brightness) 3931 return err; 3932 } 3933 3934 if (asus->driver->quirks->scalar_panel_brightness) 3935 ctrl_param = get_scalar_command(bd); 3936 else 3937 ctrl_param = bd->props.brightness; 3938 3939 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS, 3940 ctrl_param, NULL); 3941 3942 return err; 3943 } 3944 3945 static const struct backlight_ops asus_wmi_bl_ops = { 3946 .get_brightness = read_brightness, 3947 .update_status = update_bl_status, 3948 }; 3949 3950 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code) 3951 { 3952 struct backlight_device *bd = asus->backlight_device; 3953 int old = bd->props.brightness; 3954 int new = old; 3955 3956 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX) 3957 new = code - NOTIFY_BRNUP_MIN + 1; 3958 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX) 3959 new = code - NOTIFY_BRNDOWN_MIN; 3960 3961 bd->props.brightness = new; 3962 backlight_update_status(bd); 3963 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY); 3964 3965 return old; 3966 } 3967 3968 static int asus_wmi_backlight_init(struct asus_wmi *asus) 3969 { 3970 struct backlight_device *bd; 3971 struct backlight_properties props; 3972 int max; 3973 int power; 3974 3975 max = read_brightness_max(asus); 3976 if (max < 0) 3977 return max; 3978 3979 power = read_backlight_power(asus); 3980 if (power == -ENODEV) 3981 power = FB_BLANK_UNBLANK; 3982 else if (power < 0) 3983 return power; 3984 3985 memset(&props, 0, sizeof(struct backlight_properties)); 3986 props.type = BACKLIGHT_PLATFORM; 3987 props.max_brightness = max; 3988 bd = backlight_device_register(asus->driver->name, 3989 &asus->platform_device->dev, asus, 3990 &asus_wmi_bl_ops, &props); 3991 if (IS_ERR(bd)) { 3992 pr_err("Could not register backlight device\n"); 3993 return PTR_ERR(bd); 3994 } 3995 3996 asus->backlight_device = bd; 3997 3998 if (asus->driver->quirks->store_backlight_power) 3999 asus->driver->panel_power = power; 4000 4001 bd->props.brightness = read_brightness(bd); 4002 bd->props.power = power; 4003 backlight_update_status(bd); 4004 4005 asus->driver->brightness = bd->props.brightness; 4006 4007 return 0; 4008 } 4009 4010 static void asus_wmi_backlight_exit(struct asus_wmi *asus) 4011 { 4012 backlight_device_unregister(asus->backlight_device); 4013 4014 asus->backlight_device = NULL; 4015 } 4016 4017 static int is_display_toggle(int code) 4018 { 4019 /* display toggle keys */ 4020 if ((code >= 0x61 && code <= 0x67) || 4021 (code >= 0x8c && code <= 0x93) || 4022 (code >= 0xa0 && code <= 0xa7) || 4023 (code >= 0xd0 && code <= 0xd5)) 4024 return 1; 4025 4026 return 0; 4027 } 4028 4029 /* Screenpad backlight *******************************************************/ 4030 4031 static int read_screenpad_backlight_power(struct asus_wmi *asus) 4032 { 4033 int ret; 4034 4035 ret = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_SCREENPAD_POWER); 4036 if (ret < 0) 4037 return ret; 4038 /* 1 == powered */ 4039 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN; 4040 } 4041 4042 static int read_screenpad_brightness(struct backlight_device *bd) 4043 { 4044 struct asus_wmi *asus = bl_get_data(bd); 4045 u32 retval; 4046 int err; 4047 4048 err = read_screenpad_backlight_power(asus); 4049 if (err < 0) 4050 return err; 4051 /* The device brightness can only be read if powered, so return stored */ 4052 if (err == FB_BLANK_POWERDOWN) 4053 return asus->driver->screenpad_brightness - ASUS_SCREENPAD_BRIGHT_MIN; 4054 4055 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT, &retval); 4056 if (err < 0) 4057 return err; 4058 4059 return (retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK) - ASUS_SCREENPAD_BRIGHT_MIN; 4060 } 4061 4062 static int update_screenpad_bl_status(struct backlight_device *bd) 4063 { 4064 struct asus_wmi *asus = bl_get_data(bd); 4065 int power, err = 0; 4066 u32 ctrl_param; 4067 4068 power = read_screenpad_backlight_power(asus); 4069 if (power < 0) 4070 return power; 4071 4072 if (bd->props.power != power) { 4073 if (power != FB_BLANK_UNBLANK) { 4074 /* Only brightness > 0 can power it back on */ 4075 ctrl_param = asus->driver->screenpad_brightness - ASUS_SCREENPAD_BRIGHT_MIN; 4076 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_LIGHT, 4077 ctrl_param, NULL); 4078 } else { 4079 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_POWER, 0, NULL); 4080 } 4081 } else if (power == FB_BLANK_UNBLANK) { 4082 /* Only set brightness if powered on or we get invalid/unsync state */ 4083 ctrl_param = bd->props.brightness + ASUS_SCREENPAD_BRIGHT_MIN; 4084 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_LIGHT, ctrl_param, NULL); 4085 } 4086 4087 /* Ensure brightness is stored to turn back on with */ 4088 if (err == 0) 4089 asus->driver->screenpad_brightness = bd->props.brightness + ASUS_SCREENPAD_BRIGHT_MIN; 4090 4091 return err; 4092 } 4093 4094 static const struct backlight_ops asus_screenpad_bl_ops = { 4095 .get_brightness = read_screenpad_brightness, 4096 .update_status = update_screenpad_bl_status, 4097 .options = BL_CORE_SUSPENDRESUME, 4098 }; 4099 4100 static int asus_screenpad_init(struct asus_wmi *asus) 4101 { 4102 struct backlight_device *bd; 4103 struct backlight_properties props; 4104 int err, power; 4105 int brightness = 0; 4106 4107 power = read_screenpad_backlight_power(asus); 4108 if (power < 0) 4109 return power; 4110 4111 if (power != FB_BLANK_POWERDOWN) { 4112 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT, &brightness); 4113 if (err < 0) 4114 return err; 4115 } 4116 /* default to an acceptable min brightness on boot if too low */ 4117 if (brightness < ASUS_SCREENPAD_BRIGHT_MIN) 4118 brightness = ASUS_SCREENPAD_BRIGHT_DEFAULT; 4119 4120 memset(&props, 0, sizeof(struct backlight_properties)); 4121 props.type = BACKLIGHT_RAW; /* ensure this bd is last to be picked */ 4122 props.max_brightness = ASUS_SCREENPAD_BRIGHT_MAX - ASUS_SCREENPAD_BRIGHT_MIN; 4123 bd = backlight_device_register("asus_screenpad", 4124 &asus->platform_device->dev, asus, 4125 &asus_screenpad_bl_ops, &props); 4126 if (IS_ERR(bd)) { 4127 pr_err("Could not register backlight device\n"); 4128 return PTR_ERR(bd); 4129 } 4130 4131 asus->screenpad_backlight_device = bd; 4132 asus->driver->screenpad_brightness = brightness; 4133 bd->props.brightness = brightness - ASUS_SCREENPAD_BRIGHT_MIN; 4134 bd->props.power = power; 4135 backlight_update_status(bd); 4136 4137 return 0; 4138 } 4139 4140 static void asus_screenpad_exit(struct asus_wmi *asus) 4141 { 4142 backlight_device_unregister(asus->screenpad_backlight_device); 4143 4144 asus->screenpad_backlight_device = NULL; 4145 } 4146 4147 /* Fn-lock ********************************************************************/ 4148 4149 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus) 4150 { 4151 u32 result; 4152 4153 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result); 4154 4155 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) && 4156 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED); 4157 } 4158 4159 static void asus_wmi_fnlock_update(struct asus_wmi *asus) 4160 { 4161 int mode = asus->fnlock_locked; 4162 4163 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL); 4164 } 4165 4166 /* WMI events *****************************************************************/ 4167 4168 static int asus_wmi_get_event_code(u32 value) 4169 { 4170 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL }; 4171 union acpi_object *obj; 4172 acpi_status status; 4173 int code; 4174 4175 status = wmi_get_event_data(value, &response); 4176 if (ACPI_FAILURE(status)) { 4177 pr_warn("Failed to get WMI notify code: %s\n", 4178 acpi_format_exception(status)); 4179 return -EIO; 4180 } 4181 4182 obj = (union acpi_object *)response.pointer; 4183 4184 if (obj && obj->type == ACPI_TYPE_INTEGER) 4185 code = (int)(obj->integer.value & WMI_EVENT_MASK); 4186 else 4187 code = -EIO; 4188 4189 kfree(obj); 4190 return code; 4191 } 4192 4193 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus) 4194 { 4195 unsigned int key_value = 1; 4196 bool autorelease = 1; 4197 4198 if (asus->driver->key_filter) { 4199 asus->driver->key_filter(asus->driver, &code, &key_value, 4200 &autorelease); 4201 if (code == ASUS_WMI_KEY_IGNORE) 4202 return; 4203 } 4204 4205 if (acpi_video_get_backlight_type() == acpi_backlight_vendor && 4206 code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNDOWN_MAX) { 4207 asus_wmi_backlight_notify(asus, code); 4208 return; 4209 } 4210 4211 if (code == NOTIFY_KBD_BRTUP) { 4212 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1); 4213 return; 4214 } 4215 if (code == NOTIFY_KBD_BRTDWN) { 4216 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1); 4217 return; 4218 } 4219 if (code == NOTIFY_KBD_BRTTOGGLE) { 4220 if (asus->kbd_led_wk == asus->kbd_led.max_brightness) 4221 kbd_led_set_by_kbd(asus, 0); 4222 else 4223 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1); 4224 return; 4225 } 4226 4227 if (code == NOTIFY_FNLOCK_TOGGLE) { 4228 asus->fnlock_locked = !asus->fnlock_locked; 4229 asus_wmi_fnlock_update(asus); 4230 return; 4231 } 4232 4233 if (code == asus->tablet_switch_event_code) { 4234 asus_wmi_tablet_mode_get_state(asus); 4235 return; 4236 } 4237 4238 if (code == NOTIFY_KBD_FBM || code == NOTIFY_KBD_TTP) { 4239 if (asus->fan_boost_mode_available) 4240 fan_boost_mode_switch_next(asus); 4241 if (asus->throttle_thermal_policy_available) 4242 throttle_thermal_policy_switch_next(asus); 4243 return; 4244 4245 } 4246 4247 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle) 4248 return; 4249 4250 if (!sparse_keymap_report_event(asus->inputdev, code, 4251 key_value, autorelease)) 4252 pr_info("Unknown key code 0x%x\n", code); 4253 } 4254 4255 static void asus_wmi_notify(u32 value, void *context) 4256 { 4257 struct asus_wmi *asus = context; 4258 int code = asus_wmi_get_event_code(value); 4259 4260 if (code < 0) { 4261 pr_warn("Failed to get notify code: %d\n", code); 4262 return; 4263 } 4264 4265 asus_wmi_handle_event_code(code, asus); 4266 } 4267 4268 /* Sysfs **********************************************************************/ 4269 4270 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid, 4271 const char *buf, size_t count) 4272 { 4273 u32 retval; 4274 int err, value; 4275 4276 value = asus_wmi_get_devstate_simple(asus, devid); 4277 if (value < 0) 4278 return value; 4279 4280 err = kstrtoint(buf, 0, &value); 4281 if (err) 4282 return err; 4283 4284 err = asus_wmi_set_devstate(devid, value, &retval); 4285 if (err < 0) 4286 return err; 4287 4288 return count; 4289 } 4290 4291 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf) 4292 { 4293 int value = asus_wmi_get_devstate_simple(asus, devid); 4294 4295 if (value < 0) 4296 return value; 4297 4298 return sysfs_emit(buf, "%d\n", value); 4299 } 4300 4301 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \ 4302 static ssize_t show_##_name(struct device *dev, \ 4303 struct device_attribute *attr, \ 4304 char *buf) \ 4305 { \ 4306 struct asus_wmi *asus = dev_get_drvdata(dev); \ 4307 \ 4308 return show_sys_wmi(asus, _cm, buf); \ 4309 } \ 4310 static ssize_t store_##_name(struct device *dev, \ 4311 struct device_attribute *attr, \ 4312 const char *buf, size_t count) \ 4313 { \ 4314 struct asus_wmi *asus = dev_get_drvdata(dev); \ 4315 \ 4316 return store_sys_wmi(asus, _cm, buf, count); \ 4317 } \ 4318 static struct device_attribute dev_attr_##_name = { \ 4319 .attr = { \ 4320 .name = __stringify(_name), \ 4321 .mode = _mode }, \ 4322 .show = show_##_name, \ 4323 .store = store_##_name, \ 4324 } 4325 4326 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD); 4327 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA); 4328 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER); 4329 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME); 4330 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE); 4331 4332 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr, 4333 const char *buf, size_t count) 4334 { 4335 int value, rv; 4336 4337 rv = kstrtoint(buf, 0, &value); 4338 if (rv) 4339 return rv; 4340 4341 if (value < 0 || value > 2) 4342 return -EINVAL; 4343 4344 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL); 4345 if (rv < 0) 4346 return rv; 4347 4348 return count; 4349 } 4350 4351 static DEVICE_ATTR_WO(cpufv); 4352 4353 static struct attribute *platform_attributes[] = { 4354 &dev_attr_cpufv.attr, 4355 &dev_attr_camera.attr, 4356 &dev_attr_cardr.attr, 4357 &dev_attr_touchpad.attr, 4358 &dev_attr_charge_mode.attr, 4359 &dev_attr_egpu_enable.attr, 4360 &dev_attr_egpu_connected.attr, 4361 &dev_attr_dgpu_disable.attr, 4362 &dev_attr_gpu_mux_mode.attr, 4363 &dev_attr_lid_resume.attr, 4364 &dev_attr_als_enable.attr, 4365 &dev_attr_fan_boost_mode.attr, 4366 &dev_attr_throttle_thermal_policy.attr, 4367 &dev_attr_ppt_pl2_sppt.attr, 4368 &dev_attr_ppt_pl1_spl.attr, 4369 &dev_attr_ppt_fppt.attr, 4370 &dev_attr_ppt_apu_sppt.attr, 4371 &dev_attr_ppt_platform_sppt.attr, 4372 &dev_attr_nv_dynamic_boost.attr, 4373 &dev_attr_nv_temp_target.attr, 4374 &dev_attr_mcu_powersave.attr, 4375 &dev_attr_boot_sound.attr, 4376 &dev_attr_panel_od.attr, 4377 &dev_attr_mini_led_mode.attr, 4378 &dev_attr_available_mini_led_mode.attr, 4379 NULL 4380 }; 4381 4382 static umode_t asus_sysfs_is_visible(struct kobject *kobj, 4383 struct attribute *attr, int idx) 4384 { 4385 struct device *dev = kobj_to_dev(kobj); 4386 struct asus_wmi *asus = dev_get_drvdata(dev); 4387 bool ok = true; 4388 int devid = -1; 4389 4390 if (attr == &dev_attr_camera.attr) 4391 devid = ASUS_WMI_DEVID_CAMERA; 4392 else if (attr == &dev_attr_cardr.attr) 4393 devid = ASUS_WMI_DEVID_CARDREADER; 4394 else if (attr == &dev_attr_touchpad.attr) 4395 devid = ASUS_WMI_DEVID_TOUCHPAD; 4396 else if (attr == &dev_attr_lid_resume.attr) 4397 devid = ASUS_WMI_DEVID_LID_RESUME; 4398 else if (attr == &dev_attr_als_enable.attr) 4399 devid = ASUS_WMI_DEVID_ALS_ENABLE; 4400 else if (attr == &dev_attr_charge_mode.attr) 4401 devid = ASUS_WMI_DEVID_CHARGE_MODE; 4402 else if (attr == &dev_attr_egpu_enable.attr) 4403 ok = asus->egpu_enable_available; 4404 else if (attr == &dev_attr_egpu_connected.attr) 4405 devid = ASUS_WMI_DEVID_EGPU_CONNECTED; 4406 else if (attr == &dev_attr_dgpu_disable.attr) 4407 ok = asus->dgpu_disable_available; 4408 else if (attr == &dev_attr_gpu_mux_mode.attr) 4409 ok = asus->gpu_mux_dev != 0; 4410 else if (attr == &dev_attr_fan_boost_mode.attr) 4411 ok = asus->fan_boost_mode_available; 4412 else if (attr == &dev_attr_throttle_thermal_policy.attr) 4413 ok = asus->throttle_thermal_policy_available; 4414 else if (attr == &dev_attr_ppt_pl2_sppt.attr) 4415 devid = ASUS_WMI_DEVID_PPT_PL2_SPPT; 4416 else if (attr == &dev_attr_ppt_pl1_spl.attr) 4417 devid = ASUS_WMI_DEVID_PPT_PL1_SPL; 4418 else if (attr == &dev_attr_ppt_fppt.attr) 4419 devid = ASUS_WMI_DEVID_PPT_FPPT; 4420 else if (attr == &dev_attr_ppt_apu_sppt.attr) 4421 devid = ASUS_WMI_DEVID_PPT_APU_SPPT; 4422 else if (attr == &dev_attr_ppt_platform_sppt.attr) 4423 devid = ASUS_WMI_DEVID_PPT_PLAT_SPPT; 4424 else if (attr == &dev_attr_nv_dynamic_boost.attr) 4425 devid = ASUS_WMI_DEVID_NV_DYN_BOOST; 4426 else if (attr == &dev_attr_nv_temp_target.attr) 4427 devid = ASUS_WMI_DEVID_NV_THERM_TARGET; 4428 else if (attr == &dev_attr_mcu_powersave.attr) 4429 devid = ASUS_WMI_DEVID_MCU_POWERSAVE; 4430 else if (attr == &dev_attr_boot_sound.attr) 4431 devid = ASUS_WMI_DEVID_BOOT_SOUND; 4432 else if (attr == &dev_attr_panel_od.attr) 4433 devid = ASUS_WMI_DEVID_PANEL_OD; 4434 else if (attr == &dev_attr_mini_led_mode.attr) 4435 ok = asus->mini_led_dev_id != 0; 4436 else if (attr == &dev_attr_available_mini_led_mode.attr) 4437 ok = asus->mini_led_dev_id != 0; 4438 4439 if (devid != -1) 4440 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0); 4441 4442 return ok ? attr->mode : 0; 4443 } 4444 4445 static const struct attribute_group platform_attribute_group = { 4446 .is_visible = asus_sysfs_is_visible, 4447 .attrs = platform_attributes 4448 }; 4449 4450 static void asus_wmi_sysfs_exit(struct platform_device *device) 4451 { 4452 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group); 4453 } 4454 4455 static int asus_wmi_sysfs_init(struct platform_device *device) 4456 { 4457 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group); 4458 } 4459 4460 /* Platform device ************************************************************/ 4461 4462 static int asus_wmi_platform_init(struct asus_wmi *asus) 4463 { 4464 struct device *dev = &asus->platform_device->dev; 4465 char *wmi_uid; 4466 int rv; 4467 4468 /* INIT enable hotkeys on some models */ 4469 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv)) 4470 pr_info("Initialization: %#x\n", rv); 4471 4472 /* We don't know yet what to do with this version... */ 4473 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) { 4474 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF); 4475 asus->spec = rv; 4476 } 4477 4478 /* 4479 * The SFUN method probably allows the original driver to get the list 4480 * of features supported by a given model. For now, 0x0100 or 0x0800 4481 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card. 4482 * The significance of others is yet to be found. 4483 */ 4484 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) { 4485 pr_info("SFUN value: %#x\n", rv); 4486 asus->sfun = rv; 4487 } 4488 4489 /* 4490 * Eee PC and Notebooks seems to have different method_id for DSTS, 4491 * but it may also be related to the BIOS's SPEC. 4492 * Note, on most Eeepc, there is no way to check if a method exist 4493 * or note, while on notebooks, they returns 0xFFFFFFFE on failure, 4494 * but once again, SPEC may probably be used for that kind of things. 4495 * 4496 * Additionally at least TUF Gaming series laptops return nothing for 4497 * unknown methods, so the detection in this way is not possible. 4498 * 4499 * There is strong indication that only ACPI WMI devices that have _UID 4500 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS. 4501 */ 4502 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID); 4503 if (!wmi_uid) 4504 return -ENODEV; 4505 4506 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) { 4507 dev_info(dev, "Detected ASUSWMI, use DCTS\n"); 4508 asus->dsts_id = ASUS_WMI_METHODID_DCTS; 4509 } else { 4510 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid); 4511 asus->dsts_id = ASUS_WMI_METHODID_DSTS; 4512 } 4513 4514 /* CWAP allow to define the behavior of the Fn+F2 key, 4515 * this method doesn't seems to be present on Eee PCs */ 4516 if (asus->driver->quirks->wapf >= 0) 4517 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP, 4518 asus->driver->quirks->wapf, NULL); 4519 4520 return 0; 4521 } 4522 4523 /* debugfs ********************************************************************/ 4524 4525 struct asus_wmi_debugfs_node { 4526 struct asus_wmi *asus; 4527 char *name; 4528 int (*show) (struct seq_file *m, void *data); 4529 }; 4530 4531 static int show_dsts(struct seq_file *m, void *data) 4532 { 4533 struct asus_wmi *asus = m->private; 4534 int err; 4535 u32 retval = -1; 4536 4537 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval); 4538 if (err < 0) 4539 return err; 4540 4541 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval); 4542 4543 return 0; 4544 } 4545 4546 static int show_devs(struct seq_file *m, void *data) 4547 { 4548 struct asus_wmi *asus = m->private; 4549 int err; 4550 u32 retval = -1; 4551 4552 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param, 4553 &retval); 4554 if (err < 0) 4555 return err; 4556 4557 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id, 4558 asus->debug.ctrl_param, retval); 4559 4560 return 0; 4561 } 4562 4563 static int show_call(struct seq_file *m, void *data) 4564 { 4565 struct asus_wmi *asus = m->private; 4566 struct bios_args args = { 4567 .arg0 = asus->debug.dev_id, 4568 .arg1 = asus->debug.ctrl_param, 4569 }; 4570 struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; 4571 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; 4572 union acpi_object *obj; 4573 acpi_status status; 4574 4575 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 4576 0, asus->debug.method_id, 4577 &input, &output); 4578 4579 if (ACPI_FAILURE(status)) 4580 return -EIO; 4581 4582 obj = (union acpi_object *)output.pointer; 4583 if (obj && obj->type == ACPI_TYPE_INTEGER) 4584 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id, 4585 asus->debug.dev_id, asus->debug.ctrl_param, 4586 (u32) obj->integer.value); 4587 else 4588 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id, 4589 asus->debug.dev_id, asus->debug.ctrl_param, 4590 obj ? obj->type : -1); 4591 4592 kfree(obj); 4593 4594 return 0; 4595 } 4596 4597 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = { 4598 {NULL, "devs", show_devs}, 4599 {NULL, "dsts", show_dsts}, 4600 {NULL, "call", show_call}, 4601 }; 4602 4603 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file) 4604 { 4605 struct asus_wmi_debugfs_node *node = inode->i_private; 4606 4607 return single_open(file, node->show, node->asus); 4608 } 4609 4610 static const struct file_operations asus_wmi_debugfs_io_ops = { 4611 .owner = THIS_MODULE, 4612 .open = asus_wmi_debugfs_open, 4613 .read = seq_read, 4614 .llseek = seq_lseek, 4615 .release = single_release, 4616 }; 4617 4618 static void asus_wmi_debugfs_exit(struct asus_wmi *asus) 4619 { 4620 debugfs_remove_recursive(asus->debug.root); 4621 } 4622 4623 static void asus_wmi_debugfs_init(struct asus_wmi *asus) 4624 { 4625 int i; 4626 4627 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL); 4628 4629 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root, 4630 &asus->debug.method_id); 4631 4632 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root, 4633 &asus->debug.dev_id); 4634 4635 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root, 4636 &asus->debug.ctrl_param); 4637 4638 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) { 4639 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i]; 4640 4641 node->asus = asus; 4642 debugfs_create_file(node->name, S_IFREG | S_IRUGO, 4643 asus->debug.root, node, 4644 &asus_wmi_debugfs_io_ops); 4645 } 4646 } 4647 4648 /* Init / exit ****************************************************************/ 4649 4650 static int asus_wmi_add(struct platform_device *pdev) 4651 { 4652 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver); 4653 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv); 4654 struct asus_wmi *asus; 4655 acpi_status status; 4656 int err; 4657 u32 result; 4658 4659 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL); 4660 if (!asus) 4661 return -ENOMEM; 4662 4663 asus->driver = wdrv; 4664 asus->platform_device = pdev; 4665 wdrv->platform_device = pdev; 4666 platform_set_drvdata(asus->platform_device, asus); 4667 4668 if (wdrv->detect_quirks) 4669 wdrv->detect_quirks(asus->driver); 4670 4671 err = asus_wmi_platform_init(asus); 4672 if (err) 4673 goto fail_platform; 4674 4675 /* ensure defaults for tunables */ 4676 asus->ppt_pl2_sppt = 5; 4677 asus->ppt_pl1_spl = 5; 4678 asus->ppt_apu_sppt = 5; 4679 asus->ppt_platform_sppt = 5; 4680 asus->ppt_fppt = 5; 4681 asus->nv_dynamic_boost = 5; 4682 asus->nv_temp_target = 75; 4683 4684 asus->egpu_enable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_EGPU); 4685 asus->dgpu_disable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_DGPU); 4686 asus->kbd_rgb_state_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_STATE); 4687 asus->ally_mcu_usb_switch = acpi_has_method(NULL, ASUS_USB0_PWR_EC0_CSEE) 4688 && dmi_match(DMI_BOARD_NAME, "RC71L"); 4689 4690 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MINI_LED_MODE)) 4691 asus->mini_led_dev_id = ASUS_WMI_DEVID_MINI_LED_MODE; 4692 else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MINI_LED_MODE2)) 4693 asus->mini_led_dev_id = ASUS_WMI_DEVID_MINI_LED_MODE2; 4694 4695 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_MUX)) 4696 asus->gpu_mux_dev = ASUS_WMI_DEVID_GPU_MUX; 4697 else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_MUX_VIVO)) 4698 asus->gpu_mux_dev = ASUS_WMI_DEVID_GPU_MUX_VIVO; 4699 4700 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_MODE)) 4701 asus->kbd_rgb_dev = ASUS_WMI_DEVID_TUF_RGB_MODE; 4702 else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_MODE2)) 4703 asus->kbd_rgb_dev = ASUS_WMI_DEVID_TUF_RGB_MODE2; 4704 4705 err = fan_boost_mode_check_present(asus); 4706 if (err) 4707 goto fail_fan_boost_mode; 4708 4709 err = throttle_thermal_policy_check_present(asus); 4710 if (err) 4711 goto fail_throttle_thermal_policy; 4712 else 4713 throttle_thermal_policy_set_default(asus); 4714 4715 err = platform_profile_setup(asus); 4716 if (err) 4717 goto fail_platform_profile_setup; 4718 4719 err = asus_wmi_sysfs_init(asus->platform_device); 4720 if (err) 4721 goto fail_sysfs; 4722 4723 err = asus_wmi_input_init(asus); 4724 if (err) 4725 goto fail_input; 4726 4727 err = asus_wmi_fan_init(asus); /* probably no problems on error */ 4728 4729 err = asus_wmi_hwmon_init(asus); 4730 if (err) 4731 goto fail_hwmon; 4732 4733 err = asus_wmi_custom_fan_curve_init(asus); 4734 if (err) 4735 goto fail_custom_fan_curve; 4736 4737 err = asus_wmi_led_init(asus); 4738 if (err) 4739 goto fail_leds; 4740 4741 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result); 4742 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT)) 4743 asus->driver->wlan_ctrl_by_user = 1; 4744 4745 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) { 4746 err = asus_wmi_rfkill_init(asus); 4747 if (err) 4748 goto fail_rfkill; 4749 } 4750 4751 if (asus->driver->quirks->wmi_force_als_set) 4752 asus_wmi_set_als(); 4753 4754 if (asus->driver->quirks->xusb2pr) 4755 asus_wmi_set_xusb2pr(asus); 4756 4757 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) { 4758 err = asus_wmi_backlight_init(asus); 4759 if (err && err != -ENODEV) 4760 goto fail_backlight; 4761 } else if (asus->driver->quirks->wmi_backlight_set_devstate) 4762 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL); 4763 4764 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT)) { 4765 err = asus_screenpad_init(asus); 4766 if (err && err != -ENODEV) 4767 goto fail_screenpad; 4768 } 4769 4770 if (asus_wmi_has_fnlock_key(asus)) { 4771 asus->fnlock_locked = fnlock_default; 4772 asus_wmi_fnlock_update(asus); 4773 } 4774 4775 status = wmi_install_notify_handler(asus->driver->event_guid, 4776 asus_wmi_notify, asus); 4777 if (ACPI_FAILURE(status)) { 4778 pr_err("Unable to register notify handler - %d\n", status); 4779 err = -ENODEV; 4780 goto fail_wmi_handler; 4781 } 4782 4783 if (asus->driver->i8042_filter) { 4784 err = i8042_install_filter(asus->driver->i8042_filter); 4785 if (err) 4786 pr_warn("Unable to install key filter - %d\n", err); 4787 } 4788 4789 asus_wmi_battery_init(asus); 4790 4791 asus_wmi_debugfs_init(asus); 4792 4793 return 0; 4794 4795 fail_wmi_handler: 4796 asus_wmi_backlight_exit(asus); 4797 fail_backlight: 4798 asus_wmi_rfkill_exit(asus); 4799 fail_screenpad: 4800 asus_screenpad_exit(asus); 4801 fail_rfkill: 4802 asus_wmi_led_exit(asus); 4803 fail_leds: 4804 fail_hwmon: 4805 asus_wmi_input_exit(asus); 4806 fail_input: 4807 asus_wmi_sysfs_exit(asus->platform_device); 4808 fail_sysfs: 4809 fail_throttle_thermal_policy: 4810 fail_custom_fan_curve: 4811 fail_platform_profile_setup: 4812 if (asus->platform_profile_support) 4813 platform_profile_remove(); 4814 fail_fan_boost_mode: 4815 fail_platform: 4816 kfree(asus); 4817 return err; 4818 } 4819 4820 static void asus_wmi_remove(struct platform_device *device) 4821 { 4822 struct asus_wmi *asus; 4823 4824 asus = platform_get_drvdata(device); 4825 if (asus->driver->i8042_filter) 4826 i8042_remove_filter(asus->driver->i8042_filter); 4827 wmi_remove_notify_handler(asus->driver->event_guid); 4828 asus_wmi_backlight_exit(asus); 4829 asus_screenpad_exit(asus); 4830 asus_wmi_input_exit(asus); 4831 asus_wmi_led_exit(asus); 4832 asus_wmi_rfkill_exit(asus); 4833 asus_wmi_debugfs_exit(asus); 4834 asus_wmi_sysfs_exit(asus->platform_device); 4835 asus_fan_set_auto(asus); 4836 throttle_thermal_policy_set_default(asus); 4837 asus_wmi_battery_exit(asus); 4838 4839 if (asus->platform_profile_support) 4840 platform_profile_remove(); 4841 4842 kfree(asus); 4843 } 4844 4845 /* Platform driver - hibernate/resume callbacks *******************************/ 4846 4847 static int asus_hotk_thaw(struct device *device) 4848 { 4849 struct asus_wmi *asus = dev_get_drvdata(device); 4850 4851 if (asus->wlan.rfkill) { 4852 bool wlan; 4853 4854 /* 4855 * Work around bios bug - acpi _PTS turns off the wireless led 4856 * during suspend. Normally it restores it on resume, but 4857 * we should kick it ourselves in case hibernation is aborted. 4858 */ 4859 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN); 4860 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL); 4861 } 4862 4863 return 0; 4864 } 4865 4866 static int asus_hotk_resume(struct device *device) 4867 { 4868 struct asus_wmi *asus = dev_get_drvdata(device); 4869 4870 if (!IS_ERR_OR_NULL(asus->kbd_led.dev)) 4871 kbd_led_update(asus); 4872 4873 if (asus_wmi_has_fnlock_key(asus)) 4874 asus_wmi_fnlock_update(asus); 4875 4876 asus_wmi_tablet_mode_get_state(asus); 4877 4878 return 0; 4879 } 4880 4881 static int asus_hotk_resume_early(struct device *device) 4882 { 4883 struct asus_wmi *asus = dev_get_drvdata(device); 4884 4885 if (asus->ally_mcu_usb_switch) { 4886 /* sleep required to prevent USB0 being yanked then reappearing rapidly */ 4887 if (ACPI_FAILURE(acpi_execute_simple_method(NULL, ASUS_USB0_PWR_EC0_CSEE, 0xB8))) 4888 dev_err(device, "ROG Ally MCU failed to connect USB dev\n"); 4889 else 4890 msleep(ASUS_USB0_PWR_EC0_CSEE_WAIT); 4891 } 4892 return 0; 4893 } 4894 4895 static int asus_hotk_prepare(struct device *device) 4896 { 4897 struct asus_wmi *asus = dev_get_drvdata(device); 4898 4899 if (asus->ally_mcu_usb_switch) { 4900 /* sleep required to ensure USB0 is disabled before sleep continues */ 4901 if (ACPI_FAILURE(acpi_execute_simple_method(NULL, ASUS_USB0_PWR_EC0_CSEE, 0xB7))) 4902 dev_err(device, "ROG Ally MCU failed to disconnect USB dev\n"); 4903 else 4904 msleep(ASUS_USB0_PWR_EC0_CSEE_WAIT); 4905 } 4906 return 0; 4907 } 4908 4909 static int asus_hotk_restore(struct device *device) 4910 { 4911 struct asus_wmi *asus = dev_get_drvdata(device); 4912 int bl; 4913 4914 /* Refresh both wlan rfkill state and pci hotplug */ 4915 if (asus->wlan.rfkill) 4916 asus_rfkill_hotplug(asus); 4917 4918 if (asus->bluetooth.rfkill) { 4919 bl = !asus_wmi_get_devstate_simple(asus, 4920 ASUS_WMI_DEVID_BLUETOOTH); 4921 rfkill_set_sw_state(asus->bluetooth.rfkill, bl); 4922 } 4923 if (asus->wimax.rfkill) { 4924 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX); 4925 rfkill_set_sw_state(asus->wimax.rfkill, bl); 4926 } 4927 if (asus->wwan3g.rfkill) { 4928 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G); 4929 rfkill_set_sw_state(asus->wwan3g.rfkill, bl); 4930 } 4931 if (asus->gps.rfkill) { 4932 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS); 4933 rfkill_set_sw_state(asus->gps.rfkill, bl); 4934 } 4935 if (asus->uwb.rfkill) { 4936 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB); 4937 rfkill_set_sw_state(asus->uwb.rfkill, bl); 4938 } 4939 if (!IS_ERR_OR_NULL(asus->kbd_led.dev)) 4940 kbd_led_update(asus); 4941 4942 if (asus_wmi_has_fnlock_key(asus)) 4943 asus_wmi_fnlock_update(asus); 4944 4945 asus_wmi_tablet_mode_get_state(asus); 4946 return 0; 4947 } 4948 4949 static const struct dev_pm_ops asus_pm_ops = { 4950 .thaw = asus_hotk_thaw, 4951 .restore = asus_hotk_restore, 4952 .resume = asus_hotk_resume, 4953 .resume_early = asus_hotk_resume_early, 4954 .prepare = asus_hotk_prepare, 4955 }; 4956 4957 /* Registration ***************************************************************/ 4958 4959 static int asus_wmi_probe(struct platform_device *pdev) 4960 { 4961 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver); 4962 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv); 4963 int ret; 4964 4965 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) { 4966 pr_warn("ASUS Management GUID not found\n"); 4967 return -ENODEV; 4968 } 4969 4970 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) { 4971 pr_warn("ASUS Event GUID not found\n"); 4972 return -ENODEV; 4973 } 4974 4975 if (wdrv->probe) { 4976 ret = wdrv->probe(pdev); 4977 if (ret) 4978 return ret; 4979 } 4980 4981 return asus_wmi_add(pdev); 4982 } 4983 4984 static bool used; 4985 4986 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver) 4987 { 4988 struct platform_driver *platform_driver; 4989 struct platform_device *platform_device; 4990 4991 if (used) 4992 return -EBUSY; 4993 4994 platform_driver = &driver->platform_driver; 4995 platform_driver->remove_new = asus_wmi_remove; 4996 platform_driver->driver.owner = driver->owner; 4997 platform_driver->driver.name = driver->name; 4998 platform_driver->driver.pm = &asus_pm_ops; 4999 5000 platform_device = platform_create_bundle(platform_driver, 5001 asus_wmi_probe, 5002 NULL, 0, NULL, 0); 5003 if (IS_ERR(platform_device)) 5004 return PTR_ERR(platform_device); 5005 5006 used = true; 5007 return 0; 5008 } 5009 EXPORT_SYMBOL_GPL(asus_wmi_register_driver); 5010 5011 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver) 5012 { 5013 platform_device_unregister(driver->platform_device); 5014 platform_driver_unregister(&driver->platform_driver); 5015 used = false; 5016 } 5017 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver); 5018 5019 static int __init asus_wmi_init(void) 5020 { 5021 pr_info("ASUS WMI generic driver loaded\n"); 5022 return 0; 5023 } 5024 5025 static void __exit asus_wmi_exit(void) 5026 { 5027 pr_info("ASUS WMI generic driver unloaded\n"); 5028 } 5029 5030 module_init(asus_wmi_init); 5031 module_exit(asus_wmi_exit); 5032