1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Asus PC WMI hotkey driver
5 * Copyright(C) 2010 Intel Corporation.
8 * Portions based on wistron_btns.c:
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/acpi.h>
17 #include <linux/backlight.h>
18 #include <linux/debugfs.h>
19 #include <linux/dmi.h>
21 #include <linux/hwmon.h>
22 #include <linux/hwmon-sysfs.h>
23 #include <linux/init.h>
24 #include <linux/input.h>
25 #include <linux/input/sparse-keymap.h>
26 #include <linux/kernel.h>
27 #include <linux/leds.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/pci_hotplug.h>
31 #include <linux/platform_data/x86/asus-wmi.h>
32 #include <linux/platform_device.h>
33 #include <linux/platform_profile.h>
34 #include <linux/power_supply.h>
35 #include <linux/rfkill.h>
36 #include <linux/seq_file.h>
37 #include <linux/slab.h>
38 #include <linux/types.h>
39 #include <linux/units.h>
41 #include <acpi/battery.h>
42 #include <acpi/video.h>
48 MODULE_DESCRIPTION("Asus Generic WMI Driver");
49 MODULE_LICENSE("GPL");
51 static bool fnlock_default = true;
52 module_param(fnlock_default, bool, 0444);
54 #define to_asus_wmi_driver(pdrv) \
55 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
57 #define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
59 #define NOTIFY_BRNUP_MIN 0x11
60 #define NOTIFY_BRNUP_MAX 0x1f
61 #define NOTIFY_BRNDOWN_MIN 0x20
62 #define NOTIFY_BRNDOWN_MAX 0x2e
63 #define NOTIFY_FNLOCK_TOGGLE 0x4e
64 #define NOTIFY_KBD_DOCK_CHANGE 0x75
65 #define NOTIFY_KBD_BRTUP 0xc4
66 #define NOTIFY_KBD_BRTDWN 0xc5
67 #define NOTIFY_KBD_BRTTOGGLE 0xc7
68 #define NOTIFY_KBD_FBM 0x99
69 #define NOTIFY_KBD_TTP 0xae
70 #define NOTIFY_LID_FLIP 0xfa
72 #define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
74 #define ASUS_FAN_DESC "cpu_fan"
75 #define ASUS_FAN_MFUN 0x13
76 #define ASUS_FAN_SFUN_READ 0x06
77 #define ASUS_FAN_SFUN_WRITE 0x07
79 /* Based on standard hwmon pwmX_enable values */
80 #define ASUS_FAN_CTRL_FULLSPEED 0
81 #define ASUS_FAN_CTRL_MANUAL 1
82 #define ASUS_FAN_CTRL_AUTO 2
84 #define ASUS_FAN_BOOST_MODE_NORMAL 0
85 #define ASUS_FAN_BOOST_MODE_OVERBOOST 1
86 #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01
87 #define ASUS_FAN_BOOST_MODE_SILENT 2
88 #define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02
89 #define ASUS_FAN_BOOST_MODES_MASK 0x03
91 #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT 0
92 #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST 1
93 #define ASUS_THROTTLE_THERMAL_POLICY_SILENT 2
95 #define USB_INTEL_XUSB2PR 0xD0
96 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
98 #define ASUS_ACPI_UID_ASUSWMI "ASUSWMI"
99 #define ASUS_ACPI_UID_ATK "ATK"
101 #define WMI_EVENT_QUEUE_SIZE 0x10
102 #define WMI_EVENT_QUEUE_END 0x1
103 #define WMI_EVENT_MASK 0xFFFF
104 /* The WMI hotkey event value is always the same. */
105 #define WMI_EVENT_VALUE_ATK 0xFF
107 #define WMI_EVENT_MASK 0xFFFF
109 #define FAN_CURVE_POINTS 8
110 #define FAN_CURVE_BUF_LEN (FAN_CURVE_POINTS * 2)
111 #define FAN_CURVE_DEV_CPU 0x00
112 #define FAN_CURVE_DEV_GPU 0x01
113 /* Mask to determine if setting temperature or percentage */
114 #define FAN_CURVE_PWM_MASK 0x04
116 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
118 static int throttle_thermal_policy_write(struct asus_wmi *);
120 static bool ashs_present(void)
123 while (ashs_ids[i]) {
124 if (acpi_dev_found(ashs_ids[i++]))
133 u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
135 u32 arg4; /* Some ROG laptops require a full 5 input args */
140 * Struct that's used for all methods called via AGFN. Naming is
141 * identically to the AML code.
144 u16 mfun; /* probably "Multi-function" to be called */
145 u16 sfun; /* probably "Sub-function" to be called */
146 u16 len; /* size of the hole struct, including subfunction fields */
147 u8 stas; /* not used by now */
148 u8 err; /* zero on success */
151 /* struct used for calling fan read and write methods */
152 struct agfn_fan_args {
153 struct agfn_args agfn; /* common fields */
154 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
155 u32 speed; /* read: RPM/100 - write: 0-255 */
159 * <platform>/ - debugfs root directory
160 * dev_id - current dev_id
161 * ctrl_param - current ctrl_param
162 * method_id - current method_id
163 * devs - call DEVS(dev_id, ctrl_param) and print result
164 * dsts - call DSTS(dev_id) and print result
165 * call - call method_id(dev_id, ctrl_param) and print result
167 struct asus_wmi_debug {
175 struct asus_wmi *asus;
176 struct rfkill *rfkill;
182 FAN_TYPE_AGFN, /* deprecated on newer platforms */
183 FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */
186 struct fan_curve_data {
189 u8 temps[FAN_CURVE_POINTS];
190 u8 percents[FAN_CURVE_POINTS];
197 bool wmi_event_queue;
199 struct input_dev *inputdev;
200 struct backlight_device *backlight_device;
201 struct platform_device *platform_device;
203 struct led_classdev wlan_led;
205 struct led_classdev tpd_led;
207 struct led_classdev kbd_led;
209 struct led_classdev lightbar_led;
211 struct led_classdev micmute_led;
212 struct workqueue_struct *led_workqueue;
213 struct work_struct tpd_led_work;
214 struct work_struct wlan_led_work;
215 struct work_struct lightbar_led_work;
217 struct asus_rfkill wlan;
218 struct asus_rfkill bluetooth;
219 struct asus_rfkill wimax;
220 struct asus_rfkill wwan3g;
221 struct asus_rfkill gps;
222 struct asus_rfkill uwb;
224 enum fan_type fan_type;
228 bool fan_boost_mode_available;
229 u8 fan_boost_mode_mask;
232 bool egpu_enable_available; // 0 = enable
235 bool dgpu_disable_available;
238 bool throttle_thermal_policy_available;
239 u8 throttle_thermal_policy_mode;
241 bool cpu_fan_curve_available;
242 bool gpu_fan_curve_available;
243 struct fan_curve_data custom_fan_curves[2];
245 struct platform_profile_handler platform_profile_handler;
246 bool platform_profile_support;
248 // The RSOC controls the maximum charging percentage.
249 bool battery_rsoc_available;
251 bool panel_overdrive_available;
252 bool panel_overdrive;
254 struct hotplug_slot hotplug_slot;
255 struct mutex hotplug_lock;
256 struct mutex wmi_lock;
257 struct workqueue_struct *hotplug_workqueue;
258 struct work_struct hotplug_work;
262 struct asus_wmi_debug debug;
264 struct asus_wmi_driver *driver;
267 /* WMI ************************************************************************/
269 static int asus_wmi_evaluate_method3(u32 method_id,
270 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
272 struct bios_args args = {
277 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
278 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
280 union acpi_object *obj;
283 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
286 if (ACPI_FAILURE(status))
289 obj = (union acpi_object *)output.pointer;
290 if (obj && obj->type == ACPI_TYPE_INTEGER)
291 tmp = (u32) obj->integer.value;
298 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
304 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
306 return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
308 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
310 static int asus_wmi_evaluate_method5(u32 method_id,
311 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4, u32 *retval)
313 struct bios_args args = {
320 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
321 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
323 union acpi_object *obj;
326 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
329 if (ACPI_FAILURE(status))
332 obj = (union acpi_object *)output.pointer;
333 if (obj && obj->type == ACPI_TYPE_INTEGER)
334 tmp = (u32) obj->integer.value;
341 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
348 * Returns as an error if the method output is not a buffer. Typically this
349 * means that the method called is unsupported.
351 static int asus_wmi_evaluate_method_buf(u32 method_id,
352 u32 arg0, u32 arg1, u8 *ret_buffer, size_t size)
354 struct bios_args args = {
359 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
360 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
362 union acpi_object *obj;
365 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
368 if (ACPI_FAILURE(status))
371 obj = (union acpi_object *)output.pointer;
374 case ACPI_TYPE_BUFFER:
375 if (obj->buffer.length > size) {
379 if (obj->buffer.length == 0) {
384 memcpy(ret_buffer, obj->buffer.pointer, obj->buffer.length);
386 case ACPI_TYPE_INTEGER:
387 err = (u32)obj->integer.value;
389 if (err == ASUS_WMI_UNSUPPORTED_METHOD)
392 * At least one method returns a 0 with no buffer if no arg
393 * is provided, such as ASUS_WMI_DEVID_CPU_FAN_CURVE
411 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
413 struct acpi_buffer input;
419 * Copy to dma capable address otherwise memory corruption occurs as
420 * bios has to be able to access it.
422 input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
423 input.length = args.length;
426 phys_addr = virt_to_phys(input.pointer);
428 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
429 phys_addr, 0, &retval);
431 memcpy(args.pointer, input.pointer, args.length);
433 kfree(input.pointer);
440 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
442 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
445 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
448 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
452 /* Helper for special devices with magic return codes */
453 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
454 u32 dev_id, u32 mask)
459 err = asus_wmi_get_devstate(asus, dev_id, &retval);
463 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
466 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
467 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
471 return retval & mask;
474 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
476 return asus_wmi_get_devstate_bits(asus, dev_id,
477 ASUS_WMI_DSTS_STATUS_BIT);
480 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
483 int status = asus_wmi_get_devstate(asus, dev_id, &retval);
485 return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
488 /* Input **********************************************************************/
490 static int asus_wmi_input_init(struct asus_wmi *asus)
494 asus->inputdev = input_allocate_device();
498 asus->inputdev->name = asus->driver->input_name;
499 asus->inputdev->phys = asus->driver->input_phys;
500 asus->inputdev->id.bustype = BUS_HOST;
501 asus->inputdev->dev.parent = &asus->platform_device->dev;
502 set_bit(EV_REP, asus->inputdev->evbit);
504 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
508 if (asus->driver->quirks->use_kbd_dock_devid) {
509 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_KBD_DOCK);
511 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
512 input_report_switch(asus->inputdev, SW_TABLET_MODE, !result);
513 } else if (result != -ENODEV) {
514 pr_err("Error checking for keyboard-dock: %d\n", result);
518 if (asus->driver->quirks->use_lid_flip_devid) {
519 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_LID_FLIP);
521 asus->driver->quirks->use_lid_flip_devid = 0;
523 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
524 input_report_switch(asus->inputdev, SW_TABLET_MODE, result);
525 } else if (result == -ENODEV) {
526 pr_err("This device has lid_flip quirk but got ENODEV checking it. This is a bug.");
528 pr_err("Error checking for lid-flip: %d\n", result);
532 err = input_register_device(asus->inputdev);
539 input_free_device(asus->inputdev);
543 static void asus_wmi_input_exit(struct asus_wmi *asus)
546 input_unregister_device(asus->inputdev);
548 asus->inputdev = NULL;
551 /* Tablet mode ****************************************************************/
553 static void lid_flip_tablet_mode_get_state(struct asus_wmi *asus)
555 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_LID_FLIP);
558 input_report_switch(asus->inputdev, SW_TABLET_MODE, result);
559 input_sync(asus->inputdev);
563 /* dGPU ********************************************************************/
564 static int dgpu_disable_check_present(struct asus_wmi *asus)
569 asus->dgpu_disable_available = false;
571 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_DGPU, &result);
578 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
579 asus->dgpu_disable_available = true;
580 asus->dgpu_disable = result & ASUS_WMI_DSTS_STATUS_BIT;
586 static int dgpu_disable_write(struct asus_wmi *asus)
592 /* Don't rely on type conversion */
593 value = asus->dgpu_disable ? 1 : 0;
595 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, value, &retval);
597 pr_warn("Failed to set dgpu disable: %d\n", err);
602 pr_warn("Failed to set dgpu disable (retval): 0x%x\n", retval);
606 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
611 static ssize_t dgpu_disable_show(struct device *dev,
612 struct device_attribute *attr, char *buf)
614 struct asus_wmi *asus = dev_get_drvdata(dev);
615 u8 mode = asus->dgpu_disable;
617 return sysfs_emit(buf, "%d\n", mode);
621 * A user may be required to store the value twice, typcial store first, then
622 * rescan PCI bus to activate power, then store a second time to save correctly.
623 * The reason for this is that an extra code path in the ACPI is enabled when
624 * the device and bus are powered.
626 static ssize_t dgpu_disable_store(struct device *dev,
627 struct device_attribute *attr,
628 const char *buf, size_t count)
633 struct asus_wmi *asus = dev_get_drvdata(dev);
635 result = kstrtobool(buf, &disable);
639 asus->dgpu_disable = disable;
641 result = dgpu_disable_write(asus);
648 static DEVICE_ATTR_RW(dgpu_disable);
650 /* eGPU ********************************************************************/
651 static int egpu_enable_check_present(struct asus_wmi *asus)
656 asus->egpu_enable_available = false;
658 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_EGPU, &result);
665 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
666 asus->egpu_enable_available = true;
667 asus->egpu_enable = result & ASUS_WMI_DSTS_STATUS_BIT;
673 static int egpu_enable_write(struct asus_wmi *asus)
679 /* Don't rely on type conversion */
680 value = asus->egpu_enable ? 1 : 0;
682 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, value, &retval);
685 pr_warn("Failed to set egpu disable: %d\n", err);
690 pr_warn("Failed to set egpu disable (retval): 0x%x\n", retval);
694 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable");
699 static ssize_t egpu_enable_show(struct device *dev,
700 struct device_attribute *attr, char *buf)
702 struct asus_wmi *asus = dev_get_drvdata(dev);
703 bool mode = asus->egpu_enable;
705 return sysfs_emit(buf, "%d\n", mode);
708 /* The ACPI call to enable the eGPU also disables the internal dGPU */
709 static ssize_t egpu_enable_store(struct device *dev,
710 struct device_attribute *attr,
711 const char *buf, size_t count)
716 struct asus_wmi *asus = dev_get_drvdata(dev);
718 result = kstrtobool(buf, &enable);
722 asus->egpu_enable = enable;
724 result = egpu_enable_write(asus);
728 /* Ensure that the kernel status of dgpu is updated */
729 result = dgpu_disable_check_present(asus);
736 static DEVICE_ATTR_RW(egpu_enable);
738 /* Battery ********************************************************************/
740 /* The battery maximum charging percentage */
741 static int charge_end_threshold;
743 static ssize_t charge_control_end_threshold_store(struct device *dev,
744 struct device_attribute *attr,
745 const char *buf, size_t count)
749 ret = kstrtouint(buf, 10, &value);
753 if (value < 0 || value > 100)
756 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
763 /* There isn't any method in the DSDT to read the threshold, so we
764 * save the threshold.
766 charge_end_threshold = value;
770 static ssize_t charge_control_end_threshold_show(struct device *device,
771 struct device_attribute *attr,
774 return sprintf(buf, "%d\n", charge_end_threshold);
777 static DEVICE_ATTR_RW(charge_control_end_threshold);
779 static int asus_wmi_battery_add(struct power_supply *battery)
781 /* The WMI method does not provide a way to specific a battery, so we
782 * just assume it is the first battery.
783 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
784 * battery is named BATT.
786 if (strcmp(battery->desc->name, "BAT0") != 0 &&
787 strcmp(battery->desc->name, "BAT1") != 0 &&
788 strcmp(battery->desc->name, "BATC") != 0 &&
789 strcmp(battery->desc->name, "BATT") != 0)
792 if (device_create_file(&battery->dev,
793 &dev_attr_charge_control_end_threshold))
796 /* The charge threshold is only reset when the system is power cycled,
797 * and we can't get the current threshold so let set it to 100% when
798 * a battery is added.
800 asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
801 charge_end_threshold = 100;
806 static int asus_wmi_battery_remove(struct power_supply *battery)
808 device_remove_file(&battery->dev,
809 &dev_attr_charge_control_end_threshold);
813 static struct acpi_battery_hook battery_hook = {
814 .add_battery = asus_wmi_battery_add,
815 .remove_battery = asus_wmi_battery_remove,
816 .name = "ASUS Battery Extension",
819 static void asus_wmi_battery_init(struct asus_wmi *asus)
821 asus->battery_rsoc_available = false;
822 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
823 asus->battery_rsoc_available = true;
824 battery_hook_register(&battery_hook);
828 static void asus_wmi_battery_exit(struct asus_wmi *asus)
830 if (asus->battery_rsoc_available)
831 battery_hook_unregister(&battery_hook);
834 /* LEDs ***********************************************************************/
837 * These functions actually update the LED's, and are called from a
838 * workqueue. By doing this as separate work rather than when the LED
839 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
840 * potentially bad time, such as a timer interrupt.
842 static void tpd_led_update(struct work_struct *work)
845 struct asus_wmi *asus;
847 asus = container_of(work, struct asus_wmi, tpd_led_work);
849 ctrl_param = asus->tpd_led_wk;
850 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
853 static void tpd_led_set(struct led_classdev *led_cdev,
854 enum led_brightness value)
856 struct asus_wmi *asus;
858 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
860 asus->tpd_led_wk = !!value;
861 queue_work(asus->led_workqueue, &asus->tpd_led_work);
864 static int read_tpd_led_state(struct asus_wmi *asus)
866 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
869 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
871 struct asus_wmi *asus;
873 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
875 return read_tpd_led_state(asus);
878 static void kbd_led_update(struct asus_wmi *asus)
882 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
883 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
886 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
892 * bit 7: light on/off
893 * bit 8-10: environment (0: dark, 1: normal, 2: light)
894 * bit 17: status unknown
896 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
899 /* Unknown status is considered as off */
900 if (retval == 0x8000)
907 *level = retval & 0x7F;
909 *env = (retval >> 8) & 0x7F;
913 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
915 struct asus_wmi *asus;
918 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
919 max_level = asus->kbd_led.max_brightness;
921 asus->kbd_led_wk = clamp_val(value, 0, max_level);
922 kbd_led_update(asus);
925 static void kbd_led_set(struct led_classdev *led_cdev,
926 enum led_brightness value)
928 /* Prevent disabling keyboard backlight on module unregister */
929 if (led_cdev->flags & LED_UNREGISTERING)
932 do_kbd_led_set(led_cdev, value);
935 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
937 struct led_classdev *led_cdev = &asus->kbd_led;
939 do_kbd_led_set(led_cdev, value);
940 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
943 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
945 struct asus_wmi *asus;
948 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
950 retval = kbd_led_read(asus, &value, NULL);
957 static int wlan_led_unknown_state(struct asus_wmi *asus)
961 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
963 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
966 static void wlan_led_update(struct work_struct *work)
969 struct asus_wmi *asus;
971 asus = container_of(work, struct asus_wmi, wlan_led_work);
973 ctrl_param = asus->wlan_led_wk;
974 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
977 static void wlan_led_set(struct led_classdev *led_cdev,
978 enum led_brightness value)
980 struct asus_wmi *asus;
982 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
984 asus->wlan_led_wk = !!value;
985 queue_work(asus->led_workqueue, &asus->wlan_led_work);
988 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
990 struct asus_wmi *asus;
993 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
994 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
996 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
999 static void lightbar_led_update(struct work_struct *work)
1001 struct asus_wmi *asus;
1004 asus = container_of(work, struct asus_wmi, lightbar_led_work);
1006 ctrl_param = asus->lightbar_led_wk;
1007 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
1010 static void lightbar_led_set(struct led_classdev *led_cdev,
1011 enum led_brightness value)
1013 struct asus_wmi *asus;
1015 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1017 asus->lightbar_led_wk = !!value;
1018 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
1021 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
1023 struct asus_wmi *asus;
1026 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1027 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
1029 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
1032 static int micmute_led_set(struct led_classdev *led_cdev,
1033 enum led_brightness brightness)
1035 int state = brightness != LED_OFF;
1038 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MICMUTE_LED, state, NULL);
1039 return err < 0 ? err : 0;
1042 static void asus_wmi_led_exit(struct asus_wmi *asus)
1044 led_classdev_unregister(&asus->kbd_led);
1045 led_classdev_unregister(&asus->tpd_led);
1046 led_classdev_unregister(&asus->wlan_led);
1047 led_classdev_unregister(&asus->lightbar_led);
1048 led_classdev_unregister(&asus->micmute_led);
1050 if (asus->led_workqueue)
1051 destroy_workqueue(asus->led_workqueue);
1054 static int asus_wmi_led_init(struct asus_wmi *asus)
1056 int rv = 0, led_val;
1058 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
1059 if (!asus->led_workqueue)
1062 if (read_tpd_led_state(asus) >= 0) {
1063 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
1065 asus->tpd_led.name = "asus::touchpad";
1066 asus->tpd_led.brightness_set = tpd_led_set;
1067 asus->tpd_led.brightness_get = tpd_led_get;
1068 asus->tpd_led.max_brightness = 1;
1070 rv = led_classdev_register(&asus->platform_device->dev,
1076 if (!kbd_led_read(asus, &led_val, NULL)) {
1077 asus->kbd_led_wk = led_val;
1078 asus->kbd_led.name = "asus::kbd_backlight";
1079 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
1080 asus->kbd_led.brightness_set = kbd_led_set;
1081 asus->kbd_led.brightness_get = kbd_led_get;
1082 asus->kbd_led.max_brightness = 3;
1084 rv = led_classdev_register(&asus->platform_device->dev,
1090 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
1091 && (asus->driver->quirks->wapf > 0)) {
1092 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
1094 asus->wlan_led.name = "asus::wlan";
1095 asus->wlan_led.brightness_set = wlan_led_set;
1096 if (!wlan_led_unknown_state(asus))
1097 asus->wlan_led.brightness_get = wlan_led_get;
1098 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
1099 asus->wlan_led.max_brightness = 1;
1100 asus->wlan_led.default_trigger = "asus-wlan";
1102 rv = led_classdev_register(&asus->platform_device->dev,
1108 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
1109 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
1111 asus->lightbar_led.name = "asus::lightbar";
1112 asus->lightbar_led.brightness_set = lightbar_led_set;
1113 asus->lightbar_led.brightness_get = lightbar_led_get;
1114 asus->lightbar_led.max_brightness = 1;
1116 rv = led_classdev_register(&asus->platform_device->dev,
1117 &asus->lightbar_led);
1120 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MICMUTE_LED)) {
1121 asus->micmute_led.name = "asus::micmute";
1122 asus->micmute_led.max_brightness = 1;
1123 asus->micmute_led.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
1124 asus->micmute_led.brightness_set_blocking = micmute_led_set;
1125 asus->micmute_led.default_trigger = "audio-micmute";
1127 rv = led_classdev_register(&asus->platform_device->dev,
1128 &asus->micmute_led);
1135 asus_wmi_led_exit(asus);
1140 /* RF *************************************************************************/
1143 * PCI hotplug (for wlan rfkill)
1145 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
1147 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1154 static void asus_rfkill_hotplug(struct asus_wmi *asus)
1156 struct pci_dev *dev;
1157 struct pci_bus *bus;
1162 mutex_lock(&asus->wmi_lock);
1163 blocked = asus_wlan_rfkill_blocked(asus);
1164 mutex_unlock(&asus->wmi_lock);
1166 mutex_lock(&asus->hotplug_lock);
1167 pci_lock_rescan_remove();
1169 if (asus->wlan.rfkill)
1170 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
1172 if (asus->hotplug_slot.ops) {
1173 bus = pci_find_bus(0, 1);
1175 pr_warn("Unable to find PCI bus 1?\n");
1179 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
1180 pr_err("Unable to read PCI config space?\n");
1183 absent = (l == 0xffffffff);
1185 if (blocked != absent) {
1186 pr_warn("BIOS says wireless lan is %s, but the pci device is %s\n",
1187 blocked ? "blocked" : "unblocked",
1188 absent ? "absent" : "present");
1189 pr_warn("skipped wireless hotplug as probably inappropriate for this model\n");
1194 dev = pci_get_slot(bus, 0);
1196 /* Device already present */
1200 dev = pci_scan_single_device(bus, 0);
1202 pci_bus_assign_resources(bus);
1203 pci_bus_add_device(dev);
1206 dev = pci_get_slot(bus, 0);
1208 pci_stop_and_remove_bus_device(dev);
1215 pci_unlock_rescan_remove();
1216 mutex_unlock(&asus->hotplug_lock);
1219 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
1221 struct asus_wmi *asus = data;
1223 if (event != ACPI_NOTIFY_BUS_CHECK)
1227 * We can't call directly asus_rfkill_hotplug because most
1228 * of the time WMBC is still being executed and not reetrant.
1229 * There is currently no way to tell ACPICA that we want this
1230 * method to be serialized, we schedule a asus_rfkill_hotplug
1231 * call later, in a safer context.
1233 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
1236 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
1241 status = acpi_get_handle(NULL, node, &handle);
1242 if (ACPI_FAILURE(status))
1245 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1246 asus_rfkill_notify, asus);
1247 if (ACPI_FAILURE(status))
1248 pr_warn("Failed to register notify on %s\n", node);
1253 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
1255 acpi_status status = AE_OK;
1258 status = acpi_get_handle(NULL, node, &handle);
1259 if (ACPI_FAILURE(status))
1262 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1263 asus_rfkill_notify);
1264 if (ACPI_FAILURE(status))
1265 pr_err("Error removing rfkill notify handler %s\n", node);
1268 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
1271 struct asus_wmi *asus = container_of(hotplug_slot,
1272 struct asus_wmi, hotplug_slot);
1273 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1282 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
1283 .get_adapter_status = asus_get_adapter_status,
1284 .get_power_status = asus_get_adapter_status,
1287 static void asus_hotplug_work(struct work_struct *work)
1289 struct asus_wmi *asus;
1291 asus = container_of(work, struct asus_wmi, hotplug_work);
1292 asus_rfkill_hotplug(asus);
1295 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
1298 struct pci_bus *bus = pci_find_bus(0, 1);
1301 pr_err("Unable to find wifi PCI bus\n");
1305 asus->hotplug_workqueue =
1306 create_singlethread_workqueue("hotplug_workqueue");
1307 if (!asus->hotplug_workqueue)
1308 goto error_workqueue;
1310 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
1312 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
1314 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
1316 pr_err("Unable to register hotplug slot - %d\n", ret);
1317 goto error_register;
1323 asus->hotplug_slot.ops = NULL;
1324 destroy_workqueue(asus->hotplug_workqueue);
1332 static int asus_rfkill_set(void *data, bool blocked)
1334 struct asus_rfkill *priv = data;
1335 u32 ctrl_param = !blocked;
1336 u32 dev_id = priv->dev_id;
1339 * If the user bit is set, BIOS can't set and record the wlan status,
1340 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
1341 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
1342 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
1343 * while setting the wlan status through WMI.
1344 * This is also the behavior that windows app will do.
1346 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1347 priv->asus->driver->wlan_ctrl_by_user)
1348 dev_id = ASUS_WMI_DEVID_WLAN_LED;
1350 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
1353 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
1355 struct asus_rfkill *priv = data;
1358 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
1363 rfkill_set_sw_state(priv->rfkill, !result);
1366 static int asus_rfkill_wlan_set(void *data, bool blocked)
1368 struct asus_rfkill *priv = data;
1369 struct asus_wmi *asus = priv->asus;
1373 * This handler is enabled only if hotplug is enabled.
1374 * In this case, the asus_wmi_set_devstate() will
1375 * trigger a wmi notification and we need to wait
1376 * this call to finish before being able to call
1379 mutex_lock(&asus->wmi_lock);
1380 ret = asus_rfkill_set(data, blocked);
1381 mutex_unlock(&asus->wmi_lock);
1385 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1386 .set_block = asus_rfkill_wlan_set,
1387 .query = asus_rfkill_query,
1390 static const struct rfkill_ops asus_rfkill_ops = {
1391 .set_block = asus_rfkill_set,
1392 .query = asus_rfkill_query,
1395 static int asus_new_rfkill(struct asus_wmi *asus,
1396 struct asus_rfkill *arfkill,
1397 const char *name, enum rfkill_type type, int dev_id)
1399 int result = asus_wmi_get_devstate_simple(asus, dev_id);
1400 struct rfkill **rfkill = &arfkill->rfkill;
1405 arfkill->dev_id = dev_id;
1406 arfkill->asus = asus;
1408 if (dev_id == ASUS_WMI_DEVID_WLAN &&
1409 asus->driver->quirks->hotplug_wireless)
1410 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1411 &asus_rfkill_wlan_ops, arfkill);
1413 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1414 &asus_rfkill_ops, arfkill);
1419 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1420 (asus->driver->quirks->wapf > 0))
1421 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1423 rfkill_init_sw_state(*rfkill, !result);
1424 result = rfkill_register(*rfkill);
1426 rfkill_destroy(*rfkill);
1433 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1435 if (asus->driver->wlan_ctrl_by_user && ashs_present())
1438 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1439 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1440 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1441 if (asus->wlan.rfkill) {
1442 rfkill_unregister(asus->wlan.rfkill);
1443 rfkill_destroy(asus->wlan.rfkill);
1444 asus->wlan.rfkill = NULL;
1447 * Refresh pci hotplug in case the rfkill state was changed after
1448 * asus_unregister_rfkill_notifier()
1450 asus_rfkill_hotplug(asus);
1451 if (asus->hotplug_slot.ops)
1452 pci_hp_deregister(&asus->hotplug_slot);
1453 if (asus->hotplug_workqueue)
1454 destroy_workqueue(asus->hotplug_workqueue);
1456 if (asus->bluetooth.rfkill) {
1457 rfkill_unregister(asus->bluetooth.rfkill);
1458 rfkill_destroy(asus->bluetooth.rfkill);
1459 asus->bluetooth.rfkill = NULL;
1461 if (asus->wimax.rfkill) {
1462 rfkill_unregister(asus->wimax.rfkill);
1463 rfkill_destroy(asus->wimax.rfkill);
1464 asus->wimax.rfkill = NULL;
1466 if (asus->wwan3g.rfkill) {
1467 rfkill_unregister(asus->wwan3g.rfkill);
1468 rfkill_destroy(asus->wwan3g.rfkill);
1469 asus->wwan3g.rfkill = NULL;
1471 if (asus->gps.rfkill) {
1472 rfkill_unregister(asus->gps.rfkill);
1473 rfkill_destroy(asus->gps.rfkill);
1474 asus->gps.rfkill = NULL;
1476 if (asus->uwb.rfkill) {
1477 rfkill_unregister(asus->uwb.rfkill);
1478 rfkill_destroy(asus->uwb.rfkill);
1479 asus->uwb.rfkill = NULL;
1483 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1487 mutex_init(&asus->hotplug_lock);
1488 mutex_init(&asus->wmi_lock);
1490 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1491 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1493 if (result && result != -ENODEV)
1496 result = asus_new_rfkill(asus, &asus->bluetooth,
1497 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1498 ASUS_WMI_DEVID_BLUETOOTH);
1500 if (result && result != -ENODEV)
1503 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1504 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1506 if (result && result != -ENODEV)
1509 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1510 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1512 if (result && result != -ENODEV)
1515 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1516 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1518 if (result && result != -ENODEV)
1521 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1522 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1524 if (result && result != -ENODEV)
1527 if (!asus->driver->quirks->hotplug_wireless)
1530 result = asus_setup_pci_hotplug(asus);
1532 * If we get -EBUSY then something else is handling the PCI hotplug -
1533 * don't fail in this case
1535 if (result == -EBUSY)
1538 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1539 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1540 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1542 * Refresh pci hotplug in case the rfkill state was changed during
1545 asus_rfkill_hotplug(asus);
1548 if (result && result != -ENODEV)
1549 asus_wmi_rfkill_exit(asus);
1551 if (result == -ENODEV)
1557 /* Panel Overdrive ************************************************************/
1558 static int panel_od_check_present(struct asus_wmi *asus)
1563 asus->panel_overdrive_available = false;
1565 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_PANEL_OD, &result);
1572 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
1573 asus->panel_overdrive_available = true;
1574 asus->panel_overdrive = result & ASUS_WMI_DSTS_STATUS_BIT;
1580 static int panel_od_write(struct asus_wmi *asus)
1586 /* Don't rely on type conversion */
1587 value = asus->panel_overdrive ? 1 : 0;
1589 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, value, &retval);
1592 pr_warn("Failed to set panel overdrive: %d\n", err);
1597 pr_warn("Failed to set panel overdrive (retval): 0x%x\n", retval);
1601 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
1606 static ssize_t panel_od_show(struct device *dev,
1607 struct device_attribute *attr, char *buf)
1609 struct asus_wmi *asus = dev_get_drvdata(dev);
1611 return sysfs_emit(buf, "%d\n", asus->panel_overdrive);
1614 static ssize_t panel_od_store(struct device *dev,
1615 struct device_attribute *attr,
1616 const char *buf, size_t count)
1621 struct asus_wmi *asus = dev_get_drvdata(dev);
1623 result = kstrtobool(buf, &overdrive);
1627 asus->panel_overdrive = overdrive;
1628 result = panel_od_write(asus);
1636 static DEVICE_ATTR_RW(panel_od);
1638 /* Quirks *********************************************************************/
1640 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1642 struct pci_dev *xhci_pdev;
1643 u32 orig_ports_available;
1644 u32 ports_available = asus->driver->quirks->xusb2pr;
1646 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1647 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1653 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1654 &orig_ports_available);
1656 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1657 cpu_to_le32(ports_available));
1659 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1660 orig_ports_available, ports_available);
1664 * Some devices dont support or have borcken get_als method
1665 * but still support set method.
1667 static void asus_wmi_set_als(void)
1669 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1672 /* Hwmon device ***************************************************************/
1674 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1677 struct agfn_fan_args args = {
1678 .agfn.len = sizeof(args),
1679 .agfn.mfun = ASUS_FAN_MFUN,
1680 .agfn.sfun = ASUS_FAN_SFUN_READ,
1684 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1690 status = asus_wmi_evaluate_method_agfn(input);
1692 if (status || args.agfn.err)
1696 *speed = args.speed;
1701 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1704 struct agfn_fan_args args = {
1705 .agfn.len = sizeof(args),
1706 .agfn.mfun = ASUS_FAN_MFUN,
1707 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1709 .speed = speed ? *speed : 0,
1711 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1714 /* 1: for setting 1st fan's speed 0: setting auto mode */
1715 if (fan != 1 && fan != 0)
1718 status = asus_wmi_evaluate_method_agfn(input);
1720 if (status || args.agfn.err)
1723 if (speed && fan == 1)
1724 asus->agfn_pwm = *speed;
1730 * Check if we can read the speed of one fan. If true we assume we can also
1733 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
1739 status = asus_agfn_fan_speed_read(asus, 1, &speed);
1743 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1748 * We need to find a better way, probably using sfun,
1750 * Currently we disable it if:
1751 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1752 * - reverved bits are non-zero
1753 * - sfun and presence bit are not set
1755 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1756 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
1759 static int asus_fan_set_auto(struct asus_wmi *asus)
1764 switch (asus->fan_type) {
1765 case FAN_TYPE_SPEC83:
1766 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1776 status = asus_agfn_fan_speed_write(asus, 0, NULL);
1789 static ssize_t pwm1_show(struct device *dev,
1790 struct device_attribute *attr,
1793 struct asus_wmi *asus = dev_get_drvdata(dev);
1797 /* If we already set a value then just return it */
1798 if (asus->agfn_pwm >= 0)
1799 return sprintf(buf, "%d\n", asus->agfn_pwm);
1802 * If we haven't set already set a value through the AGFN interface,
1803 * we read a current value through the (now-deprecated) FAN_CTRL device.
1805 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1811 if (value == 1) /* Low Speed */
1813 else if (value == 2)
1815 else if (value == 3)
1818 pr_err("Unknown fan speed %#x\n", value);
1822 return sprintf(buf, "%d\n", value);
1825 static ssize_t pwm1_store(struct device *dev,
1826 struct device_attribute *attr,
1827 const char *buf, size_t count) {
1828 struct asus_wmi *asus = dev_get_drvdata(dev);
1833 ret = kstrtouint(buf, 10, &value);
1837 value = clamp(value, 0, 255);
1839 state = asus_agfn_fan_speed_write(asus, 1, &value);
1841 pr_warn("Setting fan speed failed: %d\n", state);
1843 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
1848 static ssize_t fan1_input_show(struct device *dev,
1849 struct device_attribute *attr,
1852 struct asus_wmi *asus = dev_get_drvdata(dev);
1856 switch (asus->fan_type) {
1857 case FAN_TYPE_SPEC83:
1858 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1867 /* no speed readable on manual mode */
1868 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1871 ret = asus_agfn_fan_speed_read(asus, 1, &value);
1873 pr_warn("reading fan speed failed: %d\n", ret);
1882 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1885 static ssize_t pwm1_enable_show(struct device *dev,
1886 struct device_attribute *attr,
1889 struct asus_wmi *asus = dev_get_drvdata(dev);
1892 * Just read back the cached pwm mode.
1894 * For the CPU_FAN device, the spec indicates that we should be
1895 * able to read the device status and consult bit 19 to see if we
1896 * are in Full On or Automatic mode. However, this does not work
1897 * in practice on X532FL at least (the bit is always 0) and there's
1898 * also nothing in the DSDT to indicate that this behaviour exists.
1900 return sprintf(buf, "%d\n", asus->fan_pwm_mode);
1903 static ssize_t pwm1_enable_store(struct device *dev,
1904 struct device_attribute *attr,
1905 const char *buf, size_t count)
1907 struct asus_wmi *asus = dev_get_drvdata(dev);
1914 ret = kstrtouint(buf, 10, &state);
1918 if (asus->fan_type == FAN_TYPE_SPEC83) {
1919 switch (state) { /* standard documented hwmon values */
1920 case ASUS_FAN_CTRL_FULLSPEED:
1923 case ASUS_FAN_CTRL_AUTO:
1930 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1937 } else if (asus->fan_type == FAN_TYPE_AGFN) {
1939 case ASUS_FAN_CTRL_MANUAL:
1942 case ASUS_FAN_CTRL_AUTO:
1943 status = asus_fan_set_auto(asus);
1953 asus->fan_pwm_mode = state;
1955 /* Must set to disabled if mode is toggled */
1956 if (asus->cpu_fan_curve_available)
1957 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
1958 if (asus->gpu_fan_curve_available)
1959 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
1964 static ssize_t fan1_label_show(struct device *dev,
1965 struct device_attribute *attr,
1968 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1971 static ssize_t asus_hwmon_temp1(struct device *dev,
1972 struct device_attribute *attr,
1975 struct asus_wmi *asus = dev_get_drvdata(dev);
1979 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1983 return sprintf(buf, "%ld\n",
1984 deci_kelvin_to_millicelsius(value & 0xFFFF));
1988 static DEVICE_ATTR_RW(pwm1);
1989 static DEVICE_ATTR_RW(pwm1_enable);
1990 static DEVICE_ATTR_RO(fan1_input);
1991 static DEVICE_ATTR_RO(fan1_label);
1994 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1996 static struct attribute *hwmon_attributes[] = {
1997 &dev_attr_pwm1.attr,
1998 &dev_attr_pwm1_enable.attr,
1999 &dev_attr_fan1_input.attr,
2000 &dev_attr_fan1_label.attr,
2002 &dev_attr_temp1_input.attr,
2006 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
2007 struct attribute *attr, int idx)
2009 struct device *dev = container_of(kobj, struct device, kobj);
2010 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2011 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
2013 if (attr == &dev_attr_pwm1.attr) {
2014 if (asus->fan_type != FAN_TYPE_AGFN)
2016 } else if (attr == &dev_attr_fan1_input.attr
2017 || attr == &dev_attr_fan1_label.attr
2018 || attr == &dev_attr_pwm1_enable.attr) {
2019 if (asus->fan_type == FAN_TYPE_NONE)
2021 } else if (attr == &dev_attr_temp1_input.attr) {
2022 int err = asus_wmi_get_devstate(asus,
2023 ASUS_WMI_DEVID_THERMAL_CTRL,
2027 return 0; /* can't return negative here */
2030 * If the temperature value in deci-Kelvin is near the absolute
2031 * zero temperature, something is clearly wrong
2033 if (value == 0 || value == 1)
2040 static const struct attribute_group hwmon_attribute_group = {
2041 .is_visible = asus_hwmon_sysfs_is_visible,
2042 .attrs = hwmon_attributes
2044 __ATTRIBUTE_GROUPS(hwmon_attribute);
2046 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
2048 struct device *dev = &asus->platform_device->dev;
2049 struct device *hwmon;
2051 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
2052 hwmon_attribute_groups);
2054 if (IS_ERR(hwmon)) {
2055 pr_err("Could not register asus hwmon device\n");
2056 return PTR_ERR(hwmon);
2061 static int asus_wmi_fan_init(struct asus_wmi *asus)
2063 asus->fan_type = FAN_TYPE_NONE;
2064 asus->agfn_pwm = -1;
2066 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
2067 asus->fan_type = FAN_TYPE_SPEC83;
2068 else if (asus_wmi_has_agfn_fan(asus))
2069 asus->fan_type = FAN_TYPE_AGFN;
2071 if (asus->fan_type == FAN_TYPE_NONE)
2074 asus_fan_set_auto(asus);
2075 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
2079 /* Fan mode *******************************************************************/
2081 static int fan_boost_mode_check_present(struct asus_wmi *asus)
2086 asus->fan_boost_mode_available = false;
2088 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
2097 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2098 (result & ASUS_FAN_BOOST_MODES_MASK)) {
2099 asus->fan_boost_mode_available = true;
2100 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
2106 static int fan_boost_mode_write(struct asus_wmi *asus)
2112 value = asus->fan_boost_mode;
2114 pr_info("Set fan boost mode: %u\n", value);
2115 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
2118 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2122 pr_warn("Failed to set fan boost mode: %d\n", err);
2127 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
2135 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
2137 u8 mask = asus->fan_boost_mode_mask;
2139 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
2140 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
2141 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
2142 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2143 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2144 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2145 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2146 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2148 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2150 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2153 return fan_boost_mode_write(asus);
2156 static ssize_t fan_boost_mode_show(struct device *dev,
2157 struct device_attribute *attr, char *buf)
2159 struct asus_wmi *asus = dev_get_drvdata(dev);
2161 return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
2164 static ssize_t fan_boost_mode_store(struct device *dev,
2165 struct device_attribute *attr,
2166 const char *buf, size_t count)
2168 struct asus_wmi *asus = dev_get_drvdata(dev);
2169 u8 mask = asus->fan_boost_mode_mask;
2173 result = kstrtou8(buf, 10, &new_mode);
2175 pr_warn("Trying to store invalid value\n");
2179 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2180 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
2182 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
2183 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
2185 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
2189 asus->fan_boost_mode = new_mode;
2190 fan_boost_mode_write(asus);
2195 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
2196 static DEVICE_ATTR_RW(fan_boost_mode);
2198 /* Custom fan curves **********************************************************/
2200 static void fan_curve_copy_from_buf(struct fan_curve_data *data, u8 *buf)
2204 for (i = 0; i < FAN_CURVE_POINTS; i++) {
2205 data->temps[i] = buf[i];
2208 for (i = 0; i < FAN_CURVE_POINTS; i++) {
2210 255 * buf[i + FAN_CURVE_POINTS] / 100;
2214 static int fan_curve_get_factory_default(struct asus_wmi *asus, u32 fan_dev)
2216 struct fan_curve_data *curves;
2217 u8 buf[FAN_CURVE_BUF_LEN];
2222 if (asus->throttle_thermal_policy_available)
2223 mode = asus->throttle_thermal_policy_mode;
2224 /* DEVID_<C/G>PU_FAN_CURVE is switched for OVERBOOST vs SILENT */
2230 if (fan_dev == ASUS_WMI_DEVID_GPU_FAN_CURVE)
2231 fan_idx = FAN_CURVE_DEV_GPU;
2233 curves = &asus->custom_fan_curves[fan_idx];
2234 err = asus_wmi_evaluate_method_buf(asus->dsts_id, fan_dev, mode, buf,
2239 fan_curve_copy_from_buf(curves, buf);
2240 curves->device_id = fan_dev;
2245 /* Check if capability exists, and populate defaults */
2246 static int fan_curve_check_present(struct asus_wmi *asus, bool *available,
2253 err = fan_curve_get_factory_default(asus, fan_dev);
2255 pr_debug("fan_curve_get_factory_default(0x%08x) failed: %d\n",
2257 /* Don't cause probe to fail on devices without fan-curves */
2265 /* Determine which fan the attribute is for if SENSOR_ATTR */
2266 static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus,
2267 struct device_attribute *attr)
2269 int index = to_sensor_dev_attr(attr)->index;
2271 return &asus->custom_fan_curves[index & FAN_CURVE_DEV_GPU];
2274 /* Determine which fan the attribute is for if SENSOR_ATTR_2 */
2275 static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus,
2276 struct device_attribute *attr)
2278 int nr = to_sensor_dev_attr_2(attr)->nr;
2280 return &asus->custom_fan_curves[nr & FAN_CURVE_DEV_GPU];
2283 static ssize_t fan_curve_show(struct device *dev,
2284 struct device_attribute *attr, char *buf)
2286 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2287 struct asus_wmi *asus = dev_get_drvdata(dev);
2288 struct fan_curve_data *data;
2289 int value, index, nr;
2291 data = fan_curve_attr_2_select(asus, attr);
2292 index = dev_attr->index;
2295 if (nr & FAN_CURVE_PWM_MASK)
2296 value = data->percents[index];
2298 value = data->temps[index];
2300 return sysfs_emit(buf, "%d\n", value);
2304 * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE.
2306 static int fan_curve_write(struct asus_wmi *asus,
2307 struct fan_curve_data *data)
2309 u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0;
2310 u8 *percents = data->percents;
2311 u8 *temps = data->temps;
2312 int ret, i, shift = 0;
2317 for (i = 0; i < FAN_CURVE_POINTS / 2; i++) {
2318 arg1 += (temps[i]) << shift;
2319 arg2 += (temps[i + 4]) << shift;
2320 /* Scale to percentage for device */
2321 arg3 += (100 * percents[i] / 255) << shift;
2322 arg4 += (100 * percents[i + 4] / 255) << shift;
2326 return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS,
2328 arg1, arg2, arg3, arg4, &ret);
2331 static ssize_t fan_curve_store(struct device *dev,
2332 struct device_attribute *attr, const char *buf,
2335 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2336 struct asus_wmi *asus = dev_get_drvdata(dev);
2337 struct fan_curve_data *data;
2341 int pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
2342 int index = dev_attr->index;
2344 data = fan_curve_attr_2_select(asus, attr);
2346 err = kstrtou8(buf, 10, &value);
2351 data->percents[index] = value;
2353 data->temps[index] = value;
2357 * Mark as disabled so the user has to explicitly enable to apply a
2358 * changed fan curve. This prevents potential lockups from writing out
2359 * many changes as one-write-per-change.
2361 data->enabled = false;
2366 static ssize_t fan_curve_enable_show(struct device *dev,
2367 struct device_attribute *attr, char *buf)
2369 struct asus_wmi *asus = dev_get_drvdata(dev);
2370 struct fan_curve_data *data;
2373 data = fan_curve_attr_select(asus, attr);
2378 return sysfs_emit(buf, "%d\n", out);
2381 static ssize_t fan_curve_enable_store(struct device *dev,
2382 struct device_attribute *attr,
2383 const char *buf, size_t count)
2385 struct asus_wmi *asus = dev_get_drvdata(dev);
2386 struct fan_curve_data *data;
2389 data = fan_curve_attr_select(asus, attr);
2391 err = kstrtoint(buf, 10, &value);
2397 data->enabled = true;
2400 data->enabled = false;
2403 * Auto + reset the fan curve data to defaults. Make it an explicit
2404 * option so that users don't accidentally overwrite a set fan curve.
2407 err = fan_curve_get_factory_default(asus, data->device_id);
2410 data->enabled = false;
2416 if (data->enabled) {
2417 err = fan_curve_write(asus, data);
2422 * For machines with throttle this is the only way to reset fans
2423 * to default mode of operation (does not erase curve data).
2425 if (asus->throttle_thermal_policy_available) {
2426 err = throttle_thermal_policy_write(asus);
2429 /* Similar is true for laptops with this fan */
2430 } else if (asus->fan_type == FAN_TYPE_SPEC83) {
2431 err = asus_fan_set_auto(asus);
2435 /* Safeguard against fautly ACPI tables */
2436 err = fan_curve_get_factory_default(asus, data->device_id);
2439 err = fan_curve_write(asus, data);
2448 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU);
2449 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve,
2450 FAN_CURVE_DEV_CPU, 0);
2451 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve,
2452 FAN_CURVE_DEV_CPU, 1);
2453 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve,
2454 FAN_CURVE_DEV_CPU, 2);
2455 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve,
2456 FAN_CURVE_DEV_CPU, 3);
2457 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve,
2458 FAN_CURVE_DEV_CPU, 4);
2459 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve,
2460 FAN_CURVE_DEV_CPU, 5);
2461 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve,
2462 FAN_CURVE_DEV_CPU, 6);
2463 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve,
2464 FAN_CURVE_DEV_CPU, 7);
2466 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve,
2467 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0);
2468 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve,
2469 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1);
2470 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve,
2471 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2);
2472 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve,
2473 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3);
2474 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve,
2475 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4);
2476 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve,
2477 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5);
2478 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve,
2479 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6);
2480 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve,
2481 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7);
2484 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU);
2485 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve,
2486 FAN_CURVE_DEV_GPU, 0);
2487 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve,
2488 FAN_CURVE_DEV_GPU, 1);
2489 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve,
2490 FAN_CURVE_DEV_GPU, 2);
2491 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve,
2492 FAN_CURVE_DEV_GPU, 3);
2493 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve,
2494 FAN_CURVE_DEV_GPU, 4);
2495 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve,
2496 FAN_CURVE_DEV_GPU, 5);
2497 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve,
2498 FAN_CURVE_DEV_GPU, 6);
2499 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve,
2500 FAN_CURVE_DEV_GPU, 7);
2502 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve,
2503 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0);
2504 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve,
2505 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1);
2506 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve,
2507 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2);
2508 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve,
2509 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3);
2510 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve,
2511 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4);
2512 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve,
2513 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5);
2514 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve,
2515 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6);
2516 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve,
2517 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7);
2519 static struct attribute *asus_fan_curve_attr[] = {
2521 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2522 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2523 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2524 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2525 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2526 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
2527 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
2528 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
2529 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
2530 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2531 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2532 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2533 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2534 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
2535 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
2536 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
2537 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
2539 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2540 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2541 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2542 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2543 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2544 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
2545 &sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr,
2546 &sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr,
2547 &sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr,
2548 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
2549 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2550 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2551 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2552 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
2553 &sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr,
2554 &sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr,
2555 &sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr,
2559 static umode_t asus_fan_curve_is_visible(struct kobject *kobj,
2560 struct attribute *attr, int idx)
2562 struct device *dev = kobj_to_dev(kobj);
2563 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2566 * Check the char instead of casting attr as there are two attr types
2567 * involved here (attr1 and attr2)
2569 if (asus->cpu_fan_curve_available && attr->name[3] == '1')
2572 if (asus->gpu_fan_curve_available && attr->name[3] == '2')
2578 static const struct attribute_group asus_fan_curve_attr_group = {
2579 .is_visible = asus_fan_curve_is_visible,
2580 .attrs = asus_fan_curve_attr,
2582 __ATTRIBUTE_GROUPS(asus_fan_curve_attr);
2585 * Must be initialised after throttle_thermal_policy_check_present() as
2586 * we check the status of throttle_thermal_policy_available during init.
2588 static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus)
2590 struct device *dev = &asus->platform_device->dev;
2591 struct device *hwmon;
2594 err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available,
2595 ASUS_WMI_DEVID_CPU_FAN_CURVE);
2599 err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available,
2600 ASUS_WMI_DEVID_GPU_FAN_CURVE);
2604 if (!asus->cpu_fan_curve_available && !asus->gpu_fan_curve_available)
2607 hwmon = devm_hwmon_device_register_with_groups(
2608 dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups);
2610 if (IS_ERR(hwmon)) {
2612 "Could not register asus_custom_fan_curve device\n");
2613 return PTR_ERR(hwmon);
2619 /* Throttle thermal policy ****************************************************/
2621 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
2626 asus->throttle_thermal_policy_available = false;
2628 err = asus_wmi_get_devstate(asus,
2629 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2637 if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
2638 asus->throttle_thermal_policy_available = true;
2643 static int throttle_thermal_policy_write(struct asus_wmi *asus)
2649 value = asus->throttle_thermal_policy_mode;
2651 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2654 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2655 "throttle_thermal_policy");
2658 pr_warn("Failed to set throttle thermal policy: %d\n", err);
2663 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
2668 /* Must set to disabled if mode is toggled */
2669 if (asus->cpu_fan_curve_available)
2670 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
2671 if (asus->gpu_fan_curve_available)
2672 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
2677 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
2679 if (!asus->throttle_thermal_policy_available)
2682 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2683 return throttle_thermal_policy_write(asus);
2686 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
2688 u8 new_mode = asus->throttle_thermal_policy_mode + 1;
2691 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2692 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2694 asus->throttle_thermal_policy_mode = new_mode;
2695 err = throttle_thermal_policy_write(asus);
2700 * Ensure that platform_profile updates userspace with the change to ensure
2701 * that platform_profile and throttle_thermal_policy_mode are in sync.
2703 platform_profile_notify();
2708 static ssize_t throttle_thermal_policy_show(struct device *dev,
2709 struct device_attribute *attr, char *buf)
2711 struct asus_wmi *asus = dev_get_drvdata(dev);
2712 u8 mode = asus->throttle_thermal_policy_mode;
2714 return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
2717 static ssize_t throttle_thermal_policy_store(struct device *dev,
2718 struct device_attribute *attr,
2719 const char *buf, size_t count)
2721 struct asus_wmi *asus = dev_get_drvdata(dev);
2726 result = kstrtou8(buf, 10, &new_mode);
2730 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2733 asus->throttle_thermal_policy_mode = new_mode;
2734 err = throttle_thermal_policy_write(asus);
2739 * Ensure that platform_profile updates userspace with the change to ensure
2740 * that platform_profile and throttle_thermal_policy_mode are in sync.
2742 platform_profile_notify();
2747 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
2748 static DEVICE_ATTR_RW(throttle_thermal_policy);
2750 /* Platform profile ***********************************************************/
2751 static int asus_wmi_platform_profile_get(struct platform_profile_handler *pprof,
2752 enum platform_profile_option *profile)
2754 struct asus_wmi *asus;
2757 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2759 tp = asus->throttle_thermal_policy_mode;
2762 case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
2763 *profile = PLATFORM_PROFILE_BALANCED;
2765 case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
2766 *profile = PLATFORM_PROFILE_PERFORMANCE;
2768 case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
2769 *profile = PLATFORM_PROFILE_QUIET;
2778 static int asus_wmi_platform_profile_set(struct platform_profile_handler *pprof,
2779 enum platform_profile_option profile)
2781 struct asus_wmi *asus;
2784 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2787 case PLATFORM_PROFILE_PERFORMANCE:
2788 tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
2790 case PLATFORM_PROFILE_BALANCED:
2791 tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2793 case PLATFORM_PROFILE_QUIET:
2794 tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
2800 asus->throttle_thermal_policy_mode = tp;
2801 return throttle_thermal_policy_write(asus);
2804 static int platform_profile_setup(struct asus_wmi *asus)
2806 struct device *dev = &asus->platform_device->dev;
2810 * Not an error if a component platform_profile relies on is unavailable
2811 * so early return, skipping the setup of platform_profile.
2813 if (!asus->throttle_thermal_policy_available)
2816 dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
2818 asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
2819 asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
2821 set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
2822 set_bit(PLATFORM_PROFILE_BALANCED,
2823 asus->platform_profile_handler.choices);
2824 set_bit(PLATFORM_PROFILE_PERFORMANCE,
2825 asus->platform_profile_handler.choices);
2827 err = platform_profile_register(&asus->platform_profile_handler);
2831 asus->platform_profile_support = true;
2835 /* Backlight ******************************************************************/
2837 static int read_backlight_power(struct asus_wmi *asus)
2841 if (asus->driver->quirks->store_backlight_power)
2842 ret = !asus->driver->panel_power;
2844 ret = asus_wmi_get_devstate_simple(asus,
2845 ASUS_WMI_DEVID_BACKLIGHT);
2850 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
2853 static int read_brightness_max(struct asus_wmi *asus)
2858 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2862 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
2871 static int read_brightness(struct backlight_device *bd)
2873 struct asus_wmi *asus = bl_get_data(bd);
2877 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2881 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
2884 static u32 get_scalar_command(struct backlight_device *bd)
2886 struct asus_wmi *asus = bl_get_data(bd);
2889 if ((asus->driver->brightness < bd->props.brightness) ||
2890 bd->props.brightness == bd->props.max_brightness)
2891 ctrl_param = 0x00008001;
2892 else if ((asus->driver->brightness > bd->props.brightness) ||
2893 bd->props.brightness == 0)
2894 ctrl_param = 0x00008000;
2896 asus->driver->brightness = bd->props.brightness;
2901 static int update_bl_status(struct backlight_device *bd)
2903 struct asus_wmi *asus = bl_get_data(bd);
2907 power = read_backlight_power(asus);
2908 if (power != -ENODEV && bd->props.power != power) {
2909 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
2910 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
2912 if (asus->driver->quirks->store_backlight_power)
2913 asus->driver->panel_power = bd->props.power;
2915 /* When using scalar brightness, updating the brightness
2916 * will mess with the backlight power */
2917 if (asus->driver->quirks->scalar_panel_brightness)
2921 if (asus->driver->quirks->scalar_panel_brightness)
2922 ctrl_param = get_scalar_command(bd);
2924 ctrl_param = bd->props.brightness;
2926 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
2932 static const struct backlight_ops asus_wmi_bl_ops = {
2933 .get_brightness = read_brightness,
2934 .update_status = update_bl_status,
2937 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
2939 struct backlight_device *bd = asus->backlight_device;
2940 int old = bd->props.brightness;
2943 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2944 new = code - NOTIFY_BRNUP_MIN + 1;
2945 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2946 new = code - NOTIFY_BRNDOWN_MIN;
2948 bd->props.brightness = new;
2949 backlight_update_status(bd);
2950 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
2955 static int asus_wmi_backlight_init(struct asus_wmi *asus)
2957 struct backlight_device *bd;
2958 struct backlight_properties props;
2962 max = read_brightness_max(asus);
2966 power = read_backlight_power(asus);
2967 if (power == -ENODEV)
2968 power = FB_BLANK_UNBLANK;
2972 memset(&props, 0, sizeof(struct backlight_properties));
2973 props.type = BACKLIGHT_PLATFORM;
2974 props.max_brightness = max;
2975 bd = backlight_device_register(asus->driver->name,
2976 &asus->platform_device->dev, asus,
2977 &asus_wmi_bl_ops, &props);
2979 pr_err("Could not register backlight device\n");
2983 asus->backlight_device = bd;
2985 if (asus->driver->quirks->store_backlight_power)
2986 asus->driver->panel_power = power;
2988 bd->props.brightness = read_brightness(bd);
2989 bd->props.power = power;
2990 backlight_update_status(bd);
2992 asus->driver->brightness = bd->props.brightness;
2997 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
2999 backlight_device_unregister(asus->backlight_device);
3001 asus->backlight_device = NULL;
3004 static int is_display_toggle(int code)
3006 /* display toggle keys */
3007 if ((code >= 0x61 && code <= 0x67) ||
3008 (code >= 0x8c && code <= 0x93) ||
3009 (code >= 0xa0 && code <= 0xa7) ||
3010 (code >= 0xd0 && code <= 0xd5))
3016 /* Fn-lock ********************************************************************/
3018 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
3022 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
3024 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
3025 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
3028 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
3030 int mode = asus->fnlock_locked;
3032 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
3035 /* WMI events *****************************************************************/
3037 static int asus_wmi_get_event_code(u32 value)
3039 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
3040 union acpi_object *obj;
3044 status = wmi_get_event_data(value, &response);
3045 if (ACPI_FAILURE(status)) {
3046 pr_warn("Failed to get WMI notify code: %s\n",
3047 acpi_format_exception(status));
3051 obj = (union acpi_object *)response.pointer;
3053 if (obj && obj->type == ACPI_TYPE_INTEGER)
3054 code = (int)(obj->integer.value & WMI_EVENT_MASK);
3062 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
3064 unsigned int key_value = 1;
3065 bool autorelease = 1;
3066 int result, orig_code;
3070 if (asus->driver->key_filter) {
3071 asus->driver->key_filter(asus->driver, &code, &key_value,
3073 if (code == ASUS_WMI_KEY_IGNORE)
3077 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3078 code = ASUS_WMI_BRN_UP;
3079 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3080 code = ASUS_WMI_BRN_DOWN;
3082 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
3083 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3084 asus_wmi_backlight_notify(asus, orig_code);
3089 if (code == NOTIFY_KBD_BRTUP) {
3090 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3093 if (code == NOTIFY_KBD_BRTDWN) {
3094 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
3097 if (code == NOTIFY_KBD_BRTTOGGLE) {
3098 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
3099 kbd_led_set_by_kbd(asus, 0);
3101 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3105 if (code == NOTIFY_FNLOCK_TOGGLE) {
3106 asus->fnlock_locked = !asus->fnlock_locked;
3107 asus_wmi_fnlock_update(asus);
3111 if (asus->driver->quirks->use_kbd_dock_devid && code == NOTIFY_KBD_DOCK_CHANGE) {
3112 result = asus_wmi_get_devstate_simple(asus,
3113 ASUS_WMI_DEVID_KBD_DOCK);
3115 input_report_switch(asus->inputdev, SW_TABLET_MODE,
3117 input_sync(asus->inputdev);
3122 if (asus->driver->quirks->use_lid_flip_devid && code == NOTIFY_LID_FLIP) {
3123 lid_flip_tablet_mode_get_state(asus);
3127 if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
3128 fan_boost_mode_switch_next(asus);
3132 if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
3133 throttle_thermal_policy_switch_next(asus);
3137 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
3140 if (!sparse_keymap_report_event(asus->inputdev, code,
3141 key_value, autorelease))
3142 pr_info("Unknown key code 0x%x\n", code);
3145 static void asus_wmi_notify(u32 value, void *context)
3147 struct asus_wmi *asus = context;
3151 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3152 code = asus_wmi_get_event_code(value);
3154 pr_warn("Failed to get notify code: %d\n", code);
3158 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3161 asus_wmi_handle_event_code(code, asus);
3164 * Double check that queue is present:
3165 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
3167 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
3171 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
3174 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
3179 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3180 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
3182 pr_warn("Failed to get event during flush: %d\n", code);
3186 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3190 pr_warn("Failed to flush event queue\n");
3194 /* Sysfs **********************************************************************/
3196 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
3197 const char *buf, size_t count)
3202 value = asus_wmi_get_devstate_simple(asus, devid);
3206 err = kstrtoint(buf, 0, &value);
3210 err = asus_wmi_set_devstate(devid, value, &retval);
3217 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
3219 int value = asus_wmi_get_devstate_simple(asus, devid);
3224 return sprintf(buf, "%d\n", value);
3227 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
3228 static ssize_t show_##_name(struct device *dev, \
3229 struct device_attribute *attr, \
3232 struct asus_wmi *asus = dev_get_drvdata(dev); \
3234 return show_sys_wmi(asus, _cm, buf); \
3236 static ssize_t store_##_name(struct device *dev, \
3237 struct device_attribute *attr, \
3238 const char *buf, size_t count) \
3240 struct asus_wmi *asus = dev_get_drvdata(dev); \
3242 return store_sys_wmi(asus, _cm, buf, count); \
3244 static struct device_attribute dev_attr_##_name = { \
3246 .name = __stringify(_name), \
3248 .show = show_##_name, \
3249 .store = store_##_name, \
3252 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
3253 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
3254 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
3255 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
3256 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
3258 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
3259 const char *buf, size_t count)
3263 rv = kstrtoint(buf, 0, &value);
3267 if (value < 0 || value > 2)
3270 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
3277 static DEVICE_ATTR_WO(cpufv);
3279 static struct attribute *platform_attributes[] = {
3280 &dev_attr_cpufv.attr,
3281 &dev_attr_camera.attr,
3282 &dev_attr_cardr.attr,
3283 &dev_attr_touchpad.attr,
3284 &dev_attr_egpu_enable.attr,
3285 &dev_attr_dgpu_disable.attr,
3286 &dev_attr_lid_resume.attr,
3287 &dev_attr_als_enable.attr,
3288 &dev_attr_fan_boost_mode.attr,
3289 &dev_attr_throttle_thermal_policy.attr,
3290 &dev_attr_panel_od.attr,
3294 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
3295 struct attribute *attr, int idx)
3297 struct device *dev = container_of(kobj, struct device, kobj);
3298 struct asus_wmi *asus = dev_get_drvdata(dev);
3302 if (attr == &dev_attr_camera.attr)
3303 devid = ASUS_WMI_DEVID_CAMERA;
3304 else if (attr == &dev_attr_cardr.attr)
3305 devid = ASUS_WMI_DEVID_CARDREADER;
3306 else if (attr == &dev_attr_touchpad.attr)
3307 devid = ASUS_WMI_DEVID_TOUCHPAD;
3308 else if (attr == &dev_attr_lid_resume.attr)
3309 devid = ASUS_WMI_DEVID_LID_RESUME;
3310 else if (attr == &dev_attr_als_enable.attr)
3311 devid = ASUS_WMI_DEVID_ALS_ENABLE;
3312 else if (attr == &dev_attr_egpu_enable.attr)
3313 ok = asus->egpu_enable_available;
3314 else if (attr == &dev_attr_dgpu_disable.attr)
3315 ok = asus->dgpu_disable_available;
3316 else if (attr == &dev_attr_fan_boost_mode.attr)
3317 ok = asus->fan_boost_mode_available;
3318 else if (attr == &dev_attr_throttle_thermal_policy.attr)
3319 ok = asus->throttle_thermal_policy_available;
3320 else if (attr == &dev_attr_panel_od.attr)
3321 ok = asus->panel_overdrive_available;
3324 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
3326 return ok ? attr->mode : 0;
3329 static const struct attribute_group platform_attribute_group = {
3330 .is_visible = asus_sysfs_is_visible,
3331 .attrs = platform_attributes
3334 static void asus_wmi_sysfs_exit(struct platform_device *device)
3336 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
3339 static int asus_wmi_sysfs_init(struct platform_device *device)
3341 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
3344 /* Platform device ************************************************************/
3346 static int asus_wmi_platform_init(struct asus_wmi *asus)
3348 struct device *dev = &asus->platform_device->dev;
3352 /* INIT enable hotkeys on some models */
3353 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
3354 pr_info("Initialization: %#x\n", rv);
3356 /* We don't know yet what to do with this version... */
3357 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
3358 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
3363 * The SFUN method probably allows the original driver to get the list
3364 * of features supported by a given model. For now, 0x0100 or 0x0800
3365 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
3366 * The significance of others is yet to be found.
3368 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
3369 pr_info("SFUN value: %#x\n", rv);
3374 * Eee PC and Notebooks seems to have different method_id for DSTS,
3375 * but it may also be related to the BIOS's SPEC.
3376 * Note, on most Eeepc, there is no way to check if a method exist
3377 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
3378 * but once again, SPEC may probably be used for that kind of things.
3380 * Additionally at least TUF Gaming series laptops return nothing for
3381 * unknown methods, so the detection in this way is not possible.
3383 * There is strong indication that only ACPI WMI devices that have _UID
3384 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
3386 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
3390 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
3391 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
3392 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
3394 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
3395 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
3399 * Some devices can have multiple event codes stored in a queue before
3400 * the module load if it was unloaded intermittently after calling
3401 * the INIT method (enables event handling). The WMI notify handler is
3402 * expected to retrieve all event codes until a retrieved code equals
3403 * queue end marker (One or Ones). Old codes are flushed from the queue
3404 * upon module load. Not enabling this when it should be has minimal
3405 * visible impact so fall back if anything goes wrong.
3407 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
3408 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
3409 dev_info(dev, "Detected ATK, enable event queue\n");
3411 if (!asus_wmi_notify_queue_flush(asus))
3412 asus->wmi_event_queue = true;
3415 /* CWAP allow to define the behavior of the Fn+F2 key,
3416 * this method doesn't seems to be present on Eee PCs */
3417 if (asus->driver->quirks->wapf >= 0)
3418 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
3419 asus->driver->quirks->wapf, NULL);
3424 /* debugfs ********************************************************************/
3426 struct asus_wmi_debugfs_node {
3427 struct asus_wmi *asus;
3429 int (*show) (struct seq_file *m, void *data);
3432 static int show_dsts(struct seq_file *m, void *data)
3434 struct asus_wmi *asus = m->private;
3438 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
3442 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
3447 static int show_devs(struct seq_file *m, void *data)
3449 struct asus_wmi *asus = m->private;
3453 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
3458 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
3459 asus->debug.ctrl_param, retval);
3464 static int show_call(struct seq_file *m, void *data)
3466 struct asus_wmi *asus = m->private;
3467 struct bios_args args = {
3468 .arg0 = asus->debug.dev_id,
3469 .arg1 = asus->debug.ctrl_param,
3471 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
3472 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
3473 union acpi_object *obj;
3476 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
3477 0, asus->debug.method_id,
3480 if (ACPI_FAILURE(status))
3483 obj = (union acpi_object *)output.pointer;
3484 if (obj && obj->type == ACPI_TYPE_INTEGER)
3485 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
3486 asus->debug.dev_id, asus->debug.ctrl_param,
3487 (u32) obj->integer.value);
3489 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
3490 asus->debug.dev_id, asus->debug.ctrl_param,
3491 obj ? obj->type : -1);
3498 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
3499 {NULL, "devs", show_devs},
3500 {NULL, "dsts", show_dsts},
3501 {NULL, "call", show_call},
3504 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
3506 struct asus_wmi_debugfs_node *node = inode->i_private;
3508 return single_open(file, node->show, node->asus);
3511 static const struct file_operations asus_wmi_debugfs_io_ops = {
3512 .owner = THIS_MODULE,
3513 .open = asus_wmi_debugfs_open,
3515 .llseek = seq_lseek,
3516 .release = single_release,
3519 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
3521 debugfs_remove_recursive(asus->debug.root);
3524 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
3528 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
3530 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
3531 &asus->debug.method_id);
3533 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
3534 &asus->debug.dev_id);
3536 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
3537 &asus->debug.ctrl_param);
3539 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
3540 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
3543 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
3544 asus->debug.root, node,
3545 &asus_wmi_debugfs_io_ops);
3549 /* Init / exit ****************************************************************/
3551 static int asus_wmi_add(struct platform_device *pdev)
3553 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3554 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3555 struct asus_wmi *asus;
3556 const char *chassis_type;
3561 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
3565 asus->driver = wdrv;
3566 asus->platform_device = pdev;
3567 wdrv->platform_device = pdev;
3568 platform_set_drvdata(asus->platform_device, asus);
3570 if (wdrv->detect_quirks)
3571 wdrv->detect_quirks(asus->driver);
3573 err = asus_wmi_platform_init(asus);
3577 err = egpu_enable_check_present(asus);
3579 goto fail_egpu_enable;
3581 err = dgpu_disable_check_present(asus);
3583 goto fail_dgpu_disable;
3585 err = fan_boost_mode_check_present(asus);
3587 goto fail_fan_boost_mode;
3589 err = throttle_thermal_policy_check_present(asus);
3591 goto fail_throttle_thermal_policy;
3593 throttle_thermal_policy_set_default(asus);
3595 err = platform_profile_setup(asus);
3597 goto fail_platform_profile_setup;
3599 err = panel_od_check_present(asus);
3603 err = asus_wmi_sysfs_init(asus->platform_device);
3607 err = asus_wmi_input_init(asus);
3611 err = asus_wmi_fan_init(asus); /* probably no problems on error */
3613 err = asus_wmi_hwmon_init(asus);
3617 err = asus_wmi_custom_fan_curve_init(asus);
3619 goto fail_custom_fan_curve;
3621 err = asus_wmi_led_init(asus);
3625 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
3626 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
3627 asus->driver->wlan_ctrl_by_user = 1;
3629 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
3630 err = asus_wmi_rfkill_init(asus);
3635 if (asus->driver->quirks->wmi_force_als_set)
3638 /* Some Asus desktop boards export an acpi-video backlight interface,
3639 stop this from showing up */
3640 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
3641 if (chassis_type && !strcmp(chassis_type, "3"))
3642 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3644 if (asus->driver->quirks->wmi_backlight_power)
3645 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3647 if (asus->driver->quirks->wmi_backlight_native)
3648 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
3650 if (asus->driver->quirks->xusb2pr)
3651 asus_wmi_set_xusb2pr(asus);
3653 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3654 err = asus_wmi_backlight_init(asus);
3655 if (err && err != -ENODEV)
3656 goto fail_backlight;
3657 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
3658 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
3660 if (asus_wmi_has_fnlock_key(asus)) {
3661 asus->fnlock_locked = fnlock_default;
3662 asus_wmi_fnlock_update(asus);
3665 status = wmi_install_notify_handler(asus->driver->event_guid,
3666 asus_wmi_notify, asus);
3667 if (ACPI_FAILURE(status)) {
3668 pr_err("Unable to register notify handler - %d\n", status);
3670 goto fail_wmi_handler;
3673 asus_wmi_battery_init(asus);
3675 asus_wmi_debugfs_init(asus);
3680 asus_wmi_backlight_exit(asus);
3682 asus_wmi_rfkill_exit(asus);
3684 asus_wmi_led_exit(asus);
3687 asus_wmi_input_exit(asus);
3689 asus_wmi_sysfs_exit(asus->platform_device);
3691 fail_throttle_thermal_policy:
3692 fail_custom_fan_curve:
3693 fail_platform_profile_setup:
3694 if (asus->platform_profile_support)
3695 platform_profile_remove();
3696 fail_fan_boost_mode:
3705 static int asus_wmi_remove(struct platform_device *device)
3707 struct asus_wmi *asus;
3709 asus = platform_get_drvdata(device);
3710 wmi_remove_notify_handler(asus->driver->event_guid);
3711 asus_wmi_backlight_exit(asus);
3712 asus_wmi_input_exit(asus);
3713 asus_wmi_led_exit(asus);
3714 asus_wmi_rfkill_exit(asus);
3715 asus_wmi_debugfs_exit(asus);
3716 asus_wmi_sysfs_exit(asus->platform_device);
3717 asus_fan_set_auto(asus);
3718 throttle_thermal_policy_set_default(asus);
3719 asus_wmi_battery_exit(asus);
3721 if (asus->platform_profile_support)
3722 platform_profile_remove();
3728 /* Platform driver - hibernate/resume callbacks *******************************/
3730 static int asus_hotk_thaw(struct device *device)
3732 struct asus_wmi *asus = dev_get_drvdata(device);
3734 if (asus->wlan.rfkill) {
3738 * Work around bios bug - acpi _PTS turns off the wireless led
3739 * during suspend. Normally it restores it on resume, but
3740 * we should kick it ourselves in case hibernation is aborted.
3742 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
3743 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
3749 static int asus_hotk_resume(struct device *device)
3751 struct asus_wmi *asus = dev_get_drvdata(device);
3753 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3754 kbd_led_update(asus);
3756 if (asus_wmi_has_fnlock_key(asus))
3757 asus_wmi_fnlock_update(asus);
3759 if (asus->driver->quirks->use_lid_flip_devid)
3760 lid_flip_tablet_mode_get_state(asus);
3765 static int asus_hotk_restore(struct device *device)
3767 struct asus_wmi *asus = dev_get_drvdata(device);
3770 /* Refresh both wlan rfkill state and pci hotplug */
3771 if (asus->wlan.rfkill)
3772 asus_rfkill_hotplug(asus);
3774 if (asus->bluetooth.rfkill) {
3775 bl = !asus_wmi_get_devstate_simple(asus,
3776 ASUS_WMI_DEVID_BLUETOOTH);
3777 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
3779 if (asus->wimax.rfkill) {
3780 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
3781 rfkill_set_sw_state(asus->wimax.rfkill, bl);
3783 if (asus->wwan3g.rfkill) {
3784 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
3785 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
3787 if (asus->gps.rfkill) {
3788 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
3789 rfkill_set_sw_state(asus->gps.rfkill, bl);
3791 if (asus->uwb.rfkill) {
3792 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
3793 rfkill_set_sw_state(asus->uwb.rfkill, bl);
3795 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3796 kbd_led_update(asus);
3798 if (asus_wmi_has_fnlock_key(asus))
3799 asus_wmi_fnlock_update(asus);
3801 if (asus->driver->quirks->use_lid_flip_devid)
3802 lid_flip_tablet_mode_get_state(asus);
3807 static const struct dev_pm_ops asus_pm_ops = {
3808 .thaw = asus_hotk_thaw,
3809 .restore = asus_hotk_restore,
3810 .resume = asus_hotk_resume,
3813 /* Registration ***************************************************************/
3815 static int asus_wmi_probe(struct platform_device *pdev)
3817 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3818 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3821 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
3822 pr_warn("ASUS Management GUID not found\n");
3826 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
3827 pr_warn("ASUS Event GUID not found\n");
3832 ret = wdrv->probe(pdev);
3837 return asus_wmi_add(pdev);
3842 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
3844 struct platform_driver *platform_driver;
3845 struct platform_device *platform_device;
3850 platform_driver = &driver->platform_driver;
3851 platform_driver->remove = asus_wmi_remove;
3852 platform_driver->driver.owner = driver->owner;
3853 platform_driver->driver.name = driver->name;
3854 platform_driver->driver.pm = &asus_pm_ops;
3856 platform_device = platform_create_bundle(platform_driver,
3859 if (IS_ERR(platform_device))
3860 return PTR_ERR(platform_device);
3865 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
3867 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
3869 platform_device_unregister(driver->platform_device);
3870 platform_driver_unregister(&driver->platform_driver);
3873 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
3875 static int __init asus_wmi_init(void)
3877 pr_info("ASUS WMI generic driver loaded\n");
3881 static void __exit asus_wmi_exit(void)
3883 pr_info("ASUS WMI generic driver unloaded\n");
3886 module_init(asus_wmi_init);
3887 module_exit(asus_wmi_exit);