1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Asus PC WMI hotkey driver
5 * Copyright(C) 2010 Intel Corporation.
6 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
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>
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>
46 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>");
47 MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
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 workqueue_struct *led_workqueue;
212 struct work_struct tpd_led_work;
213 struct work_struct wlan_led_work;
214 struct work_struct lightbar_led_work;
216 struct asus_rfkill wlan;
217 struct asus_rfkill bluetooth;
218 struct asus_rfkill wimax;
219 struct asus_rfkill wwan3g;
220 struct asus_rfkill gps;
221 struct asus_rfkill uwb;
223 enum fan_type fan_type;
227 bool fan_boost_mode_available;
228 u8 fan_boost_mode_mask;
231 bool egpu_enable_available; // 0 = enable
234 bool dgpu_disable_available;
237 bool throttle_thermal_policy_available;
238 u8 throttle_thermal_policy_mode;
240 bool cpu_fan_curve_available;
241 bool gpu_fan_curve_available;
242 struct fan_curve_data custom_fan_curves[2];
244 struct platform_profile_handler platform_profile_handler;
245 bool platform_profile_support;
247 // The RSOC controls the maximum charging percentage.
248 bool battery_rsoc_available;
250 bool panel_overdrive_available;
251 bool panel_overdrive;
253 struct hotplug_slot hotplug_slot;
254 struct mutex hotplug_lock;
255 struct mutex wmi_lock;
256 struct workqueue_struct *hotplug_workqueue;
257 struct work_struct hotplug_work;
261 struct asus_wmi_debug debug;
263 struct asus_wmi_driver *driver;
266 /* WMI ************************************************************************/
268 static int asus_wmi_evaluate_method3(u32 method_id,
269 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
271 struct bios_args args = {
276 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
277 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
279 union acpi_object *obj;
282 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
285 if (ACPI_FAILURE(status))
288 obj = (union acpi_object *)output.pointer;
289 if (obj && obj->type == ACPI_TYPE_INTEGER)
290 tmp = (u32) obj->integer.value;
297 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
303 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
305 return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
307 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
309 static int asus_wmi_evaluate_method5(u32 method_id,
310 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4, u32 *retval)
312 struct bios_args args = {
319 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
320 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
322 union acpi_object *obj;
325 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
328 if (ACPI_FAILURE(status))
331 obj = (union acpi_object *)output.pointer;
332 if (obj && obj->type == ACPI_TYPE_INTEGER)
333 tmp = (u32) obj->integer.value;
340 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
347 * Returns as an error if the method output is not a buffer. Typically this
348 * means that the method called is unsupported.
350 static int asus_wmi_evaluate_method_buf(u32 method_id,
351 u32 arg0, u32 arg1, u8 *ret_buffer, size_t size)
353 struct bios_args args = {
358 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
359 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
361 union acpi_object *obj;
364 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
367 if (ACPI_FAILURE(status))
370 obj = (union acpi_object *)output.pointer;
373 case ACPI_TYPE_BUFFER:
374 if (obj->buffer.length > size) {
378 if (obj->buffer.length == 0) {
383 memcpy(ret_buffer, obj->buffer.pointer, obj->buffer.length);
385 case ACPI_TYPE_INTEGER:
386 err = (u32)obj->integer.value;
388 if (err == ASUS_WMI_UNSUPPORTED_METHOD)
391 * At least one method returns a 0 with no buffer if no arg
392 * is provided, such as ASUS_WMI_DEVID_CPU_FAN_CURVE
410 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
412 struct acpi_buffer input;
418 * Copy to dma capable address otherwise memory corruption occurs as
419 * bios has to be able to access it.
421 input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
422 input.length = args.length;
425 phys_addr = virt_to_phys(input.pointer);
427 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
428 phys_addr, 0, &retval);
430 memcpy(args.pointer, input.pointer, args.length);
432 kfree(input.pointer);
439 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
441 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
444 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
447 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
451 /* Helper for special devices with magic return codes */
452 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
453 u32 dev_id, u32 mask)
458 err = asus_wmi_get_devstate(asus, dev_id, &retval);
462 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
465 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
466 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
470 return retval & mask;
473 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
475 return asus_wmi_get_devstate_bits(asus, dev_id,
476 ASUS_WMI_DSTS_STATUS_BIT);
479 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
482 int status = asus_wmi_get_devstate(asus, dev_id, &retval);
484 return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
487 /* Input **********************************************************************/
489 static int asus_wmi_input_init(struct asus_wmi *asus)
493 asus->inputdev = input_allocate_device();
497 asus->inputdev->name = asus->driver->input_name;
498 asus->inputdev->phys = asus->driver->input_phys;
499 asus->inputdev->id.bustype = BUS_HOST;
500 asus->inputdev->dev.parent = &asus->platform_device->dev;
501 set_bit(EV_REP, asus->inputdev->evbit);
503 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
507 if (asus->driver->quirks->use_kbd_dock_devid) {
508 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_KBD_DOCK);
510 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
511 input_report_switch(asus->inputdev, SW_TABLET_MODE, !result);
512 } else if (result != -ENODEV) {
513 pr_err("Error checking for keyboard-dock: %d\n", result);
517 if (asus->driver->quirks->use_lid_flip_devid) {
518 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_LID_FLIP);
520 asus->driver->quirks->use_lid_flip_devid = 0;
522 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
523 input_report_switch(asus->inputdev, SW_TABLET_MODE, result);
524 } else if (result == -ENODEV) {
525 pr_err("This device has lid_flip quirk but got ENODEV checking it. This is a bug.");
527 pr_err("Error checking for lid-flip: %d\n", result);
531 err = input_register_device(asus->inputdev);
538 input_free_device(asus->inputdev);
542 static void asus_wmi_input_exit(struct asus_wmi *asus)
545 input_unregister_device(asus->inputdev);
547 asus->inputdev = NULL;
550 /* Tablet mode ****************************************************************/
552 static void lid_flip_tablet_mode_get_state(struct asus_wmi *asus)
554 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_LID_FLIP);
557 input_report_switch(asus->inputdev, SW_TABLET_MODE, result);
558 input_sync(asus->inputdev);
562 /* dGPU ********************************************************************/
563 static int dgpu_disable_check_present(struct asus_wmi *asus)
568 asus->dgpu_disable_available = false;
570 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_DGPU, &result);
577 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
578 asus->dgpu_disable_available = true;
579 asus->dgpu_disable = result & ASUS_WMI_DSTS_STATUS_BIT;
585 static int dgpu_disable_write(struct asus_wmi *asus)
591 /* Don't rely on type conversion */
592 value = asus->dgpu_disable ? 1 : 0;
594 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, value, &retval);
596 pr_warn("Failed to set dgpu disable: %d\n", err);
601 pr_warn("Failed to set dgpu disable (retval): 0x%x\n", retval);
605 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
610 static ssize_t dgpu_disable_show(struct device *dev,
611 struct device_attribute *attr, char *buf)
613 struct asus_wmi *asus = dev_get_drvdata(dev);
614 u8 mode = asus->dgpu_disable;
616 return sysfs_emit(buf, "%d\n", mode);
620 * A user may be required to store the value twice, typcial store first, then
621 * rescan PCI bus to activate power, then store a second time to save correctly.
622 * The reason for this is that an extra code path in the ACPI is enabled when
623 * the device and bus are powered.
625 static ssize_t dgpu_disable_store(struct device *dev,
626 struct device_attribute *attr,
627 const char *buf, size_t count)
632 struct asus_wmi *asus = dev_get_drvdata(dev);
634 result = kstrtobool(buf, &disable);
638 asus->dgpu_disable = disable;
640 result = dgpu_disable_write(asus);
647 static DEVICE_ATTR_RW(dgpu_disable);
649 /* eGPU ********************************************************************/
650 static int egpu_enable_check_present(struct asus_wmi *asus)
655 asus->egpu_enable_available = false;
657 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_EGPU, &result);
664 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
665 asus->egpu_enable_available = true;
666 asus->egpu_enable = result & ASUS_WMI_DSTS_STATUS_BIT;
672 static int egpu_enable_write(struct asus_wmi *asus)
678 /* Don't rely on type conversion */
679 value = asus->egpu_enable ? 1 : 0;
681 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, value, &retval);
684 pr_warn("Failed to set egpu disable: %d\n", err);
689 pr_warn("Failed to set egpu disable (retval): 0x%x\n", retval);
693 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable");
698 static ssize_t egpu_enable_show(struct device *dev,
699 struct device_attribute *attr, char *buf)
701 struct asus_wmi *asus = dev_get_drvdata(dev);
702 bool mode = asus->egpu_enable;
704 return sysfs_emit(buf, "%d\n", mode);
707 /* The ACPI call to enable the eGPU also disables the internal dGPU */
708 static ssize_t egpu_enable_store(struct device *dev,
709 struct device_attribute *attr,
710 const char *buf, size_t count)
715 struct asus_wmi *asus = dev_get_drvdata(dev);
717 result = kstrtobool(buf, &enable);
721 asus->egpu_enable = enable;
723 result = egpu_enable_write(asus);
727 /* Ensure that the kernel status of dgpu is updated */
728 result = dgpu_disable_check_present(asus);
735 static DEVICE_ATTR_RW(egpu_enable);
737 /* Battery ********************************************************************/
739 /* The battery maximum charging percentage */
740 static int charge_end_threshold;
742 static ssize_t charge_control_end_threshold_store(struct device *dev,
743 struct device_attribute *attr,
744 const char *buf, size_t count)
748 ret = kstrtouint(buf, 10, &value);
752 if (value < 0 || value > 100)
755 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
762 /* There isn't any method in the DSDT to read the threshold, so we
763 * save the threshold.
765 charge_end_threshold = value;
769 static ssize_t charge_control_end_threshold_show(struct device *device,
770 struct device_attribute *attr,
773 return sprintf(buf, "%d\n", charge_end_threshold);
776 static DEVICE_ATTR_RW(charge_control_end_threshold);
778 static int asus_wmi_battery_add(struct power_supply *battery)
780 /* The WMI method does not provide a way to specific a battery, so we
781 * just assume it is the first battery.
782 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
783 * battery is named BATT.
785 if (strcmp(battery->desc->name, "BAT0") != 0 &&
786 strcmp(battery->desc->name, "BAT1") != 0 &&
787 strcmp(battery->desc->name, "BATC") != 0 &&
788 strcmp(battery->desc->name, "BATT") != 0)
791 if (device_create_file(&battery->dev,
792 &dev_attr_charge_control_end_threshold))
795 /* The charge threshold is only reset when the system is power cycled,
796 * and we can't get the current threshold so let set it to 100% when
797 * a battery is added.
799 asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
800 charge_end_threshold = 100;
805 static int asus_wmi_battery_remove(struct power_supply *battery)
807 device_remove_file(&battery->dev,
808 &dev_attr_charge_control_end_threshold);
812 static struct acpi_battery_hook battery_hook = {
813 .add_battery = asus_wmi_battery_add,
814 .remove_battery = asus_wmi_battery_remove,
815 .name = "ASUS Battery Extension",
818 static void asus_wmi_battery_init(struct asus_wmi *asus)
820 asus->battery_rsoc_available = false;
821 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
822 asus->battery_rsoc_available = true;
823 battery_hook_register(&battery_hook);
827 static void asus_wmi_battery_exit(struct asus_wmi *asus)
829 if (asus->battery_rsoc_available)
830 battery_hook_unregister(&battery_hook);
833 /* LEDs ***********************************************************************/
836 * These functions actually update the LED's, and are called from a
837 * workqueue. By doing this as separate work rather than when the LED
838 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
839 * potentially bad time, such as a timer interrupt.
841 static void tpd_led_update(struct work_struct *work)
844 struct asus_wmi *asus;
846 asus = container_of(work, struct asus_wmi, tpd_led_work);
848 ctrl_param = asus->tpd_led_wk;
849 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
852 static void tpd_led_set(struct led_classdev *led_cdev,
853 enum led_brightness value)
855 struct asus_wmi *asus;
857 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
859 asus->tpd_led_wk = !!value;
860 queue_work(asus->led_workqueue, &asus->tpd_led_work);
863 static int read_tpd_led_state(struct asus_wmi *asus)
865 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
868 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
870 struct asus_wmi *asus;
872 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
874 return read_tpd_led_state(asus);
877 static void kbd_led_update(struct asus_wmi *asus)
881 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
882 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
885 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
891 * bit 7: light on/off
892 * bit 8-10: environment (0: dark, 1: normal, 2: light)
893 * bit 17: status unknown
895 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
898 /* Unknown status is considered as off */
899 if (retval == 0x8000)
906 *level = retval & 0x7F;
908 *env = (retval >> 8) & 0x7F;
912 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
914 struct asus_wmi *asus;
917 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
918 max_level = asus->kbd_led.max_brightness;
920 asus->kbd_led_wk = clamp_val(value, 0, max_level);
921 kbd_led_update(asus);
924 static void kbd_led_set(struct led_classdev *led_cdev,
925 enum led_brightness value)
927 /* Prevent disabling keyboard backlight on module unregister */
928 if (led_cdev->flags & LED_UNREGISTERING)
931 do_kbd_led_set(led_cdev, value);
934 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
936 struct led_classdev *led_cdev = &asus->kbd_led;
938 do_kbd_led_set(led_cdev, value);
939 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
942 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
944 struct asus_wmi *asus;
947 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
949 retval = kbd_led_read(asus, &value, NULL);
956 static int wlan_led_unknown_state(struct asus_wmi *asus)
960 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
962 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
965 static void wlan_led_update(struct work_struct *work)
968 struct asus_wmi *asus;
970 asus = container_of(work, struct asus_wmi, wlan_led_work);
972 ctrl_param = asus->wlan_led_wk;
973 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
976 static void wlan_led_set(struct led_classdev *led_cdev,
977 enum led_brightness value)
979 struct asus_wmi *asus;
981 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
983 asus->wlan_led_wk = !!value;
984 queue_work(asus->led_workqueue, &asus->wlan_led_work);
987 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
989 struct asus_wmi *asus;
992 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
993 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
995 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
998 static void lightbar_led_update(struct work_struct *work)
1000 struct asus_wmi *asus;
1003 asus = container_of(work, struct asus_wmi, lightbar_led_work);
1005 ctrl_param = asus->lightbar_led_wk;
1006 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
1009 static void lightbar_led_set(struct led_classdev *led_cdev,
1010 enum led_brightness value)
1012 struct asus_wmi *asus;
1014 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1016 asus->lightbar_led_wk = !!value;
1017 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
1020 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
1022 struct asus_wmi *asus;
1025 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1026 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
1028 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
1031 static void asus_wmi_led_exit(struct asus_wmi *asus)
1033 led_classdev_unregister(&asus->kbd_led);
1034 led_classdev_unregister(&asus->tpd_led);
1035 led_classdev_unregister(&asus->wlan_led);
1036 led_classdev_unregister(&asus->lightbar_led);
1038 if (asus->led_workqueue)
1039 destroy_workqueue(asus->led_workqueue);
1042 static int asus_wmi_led_init(struct asus_wmi *asus)
1044 int rv = 0, led_val;
1046 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
1047 if (!asus->led_workqueue)
1050 if (read_tpd_led_state(asus) >= 0) {
1051 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
1053 asus->tpd_led.name = "asus::touchpad";
1054 asus->tpd_led.brightness_set = tpd_led_set;
1055 asus->tpd_led.brightness_get = tpd_led_get;
1056 asus->tpd_led.max_brightness = 1;
1058 rv = led_classdev_register(&asus->platform_device->dev,
1064 if (!kbd_led_read(asus, &led_val, NULL)) {
1065 asus->kbd_led_wk = led_val;
1066 asus->kbd_led.name = "asus::kbd_backlight";
1067 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
1068 asus->kbd_led.brightness_set = kbd_led_set;
1069 asus->kbd_led.brightness_get = kbd_led_get;
1070 asus->kbd_led.max_brightness = 3;
1072 rv = led_classdev_register(&asus->platform_device->dev,
1078 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
1079 && (asus->driver->quirks->wapf > 0)) {
1080 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
1082 asus->wlan_led.name = "asus::wlan";
1083 asus->wlan_led.brightness_set = wlan_led_set;
1084 if (!wlan_led_unknown_state(asus))
1085 asus->wlan_led.brightness_get = wlan_led_get;
1086 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
1087 asus->wlan_led.max_brightness = 1;
1088 asus->wlan_led.default_trigger = "asus-wlan";
1090 rv = led_classdev_register(&asus->platform_device->dev,
1096 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
1097 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
1099 asus->lightbar_led.name = "asus::lightbar";
1100 asus->lightbar_led.brightness_set = lightbar_led_set;
1101 asus->lightbar_led.brightness_get = lightbar_led_get;
1102 asus->lightbar_led.max_brightness = 1;
1104 rv = led_classdev_register(&asus->platform_device->dev,
1105 &asus->lightbar_led);
1110 asus_wmi_led_exit(asus);
1115 /* RF *************************************************************************/
1118 * PCI hotplug (for wlan rfkill)
1120 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
1122 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1129 static void asus_rfkill_hotplug(struct asus_wmi *asus)
1131 struct pci_dev *dev;
1132 struct pci_bus *bus;
1137 mutex_lock(&asus->wmi_lock);
1138 blocked = asus_wlan_rfkill_blocked(asus);
1139 mutex_unlock(&asus->wmi_lock);
1141 mutex_lock(&asus->hotplug_lock);
1142 pci_lock_rescan_remove();
1144 if (asus->wlan.rfkill)
1145 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
1147 if (asus->hotplug_slot.ops) {
1148 bus = pci_find_bus(0, 1);
1150 pr_warn("Unable to find PCI bus 1?\n");
1154 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
1155 pr_err("Unable to read PCI config space?\n");
1158 absent = (l == 0xffffffff);
1160 if (blocked != absent) {
1161 pr_warn("BIOS says wireless lan is %s, but the pci device is %s\n",
1162 blocked ? "blocked" : "unblocked",
1163 absent ? "absent" : "present");
1164 pr_warn("skipped wireless hotplug as probably inappropriate for this model\n");
1169 dev = pci_get_slot(bus, 0);
1171 /* Device already present */
1175 dev = pci_scan_single_device(bus, 0);
1177 pci_bus_assign_resources(bus);
1178 pci_bus_add_device(dev);
1181 dev = pci_get_slot(bus, 0);
1183 pci_stop_and_remove_bus_device(dev);
1190 pci_unlock_rescan_remove();
1191 mutex_unlock(&asus->hotplug_lock);
1194 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
1196 struct asus_wmi *asus = data;
1198 if (event != ACPI_NOTIFY_BUS_CHECK)
1202 * We can't call directly asus_rfkill_hotplug because most
1203 * of the time WMBC is still being executed and not reetrant.
1204 * There is currently no way to tell ACPICA that we want this
1205 * method to be serialized, we schedule a asus_rfkill_hotplug
1206 * call later, in a safer context.
1208 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
1211 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
1216 status = acpi_get_handle(NULL, node, &handle);
1217 if (ACPI_FAILURE(status))
1220 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1221 asus_rfkill_notify, asus);
1222 if (ACPI_FAILURE(status))
1223 pr_warn("Failed to register notify on %s\n", node);
1228 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
1230 acpi_status status = AE_OK;
1233 status = acpi_get_handle(NULL, node, &handle);
1234 if (ACPI_FAILURE(status))
1237 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1238 asus_rfkill_notify);
1239 if (ACPI_FAILURE(status))
1240 pr_err("Error removing rfkill notify handler %s\n", node);
1243 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
1246 struct asus_wmi *asus = container_of(hotplug_slot,
1247 struct asus_wmi, hotplug_slot);
1248 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1257 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
1258 .get_adapter_status = asus_get_adapter_status,
1259 .get_power_status = asus_get_adapter_status,
1262 static void asus_hotplug_work(struct work_struct *work)
1264 struct asus_wmi *asus;
1266 asus = container_of(work, struct asus_wmi, hotplug_work);
1267 asus_rfkill_hotplug(asus);
1270 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
1273 struct pci_bus *bus = pci_find_bus(0, 1);
1276 pr_err("Unable to find wifi PCI bus\n");
1280 asus->hotplug_workqueue =
1281 create_singlethread_workqueue("hotplug_workqueue");
1282 if (!asus->hotplug_workqueue)
1283 goto error_workqueue;
1285 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
1287 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
1289 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
1291 pr_err("Unable to register hotplug slot - %d\n", ret);
1292 goto error_register;
1298 asus->hotplug_slot.ops = NULL;
1299 destroy_workqueue(asus->hotplug_workqueue);
1307 static int asus_rfkill_set(void *data, bool blocked)
1309 struct asus_rfkill *priv = data;
1310 u32 ctrl_param = !blocked;
1311 u32 dev_id = priv->dev_id;
1314 * If the user bit is set, BIOS can't set and record the wlan status,
1315 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
1316 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
1317 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
1318 * while setting the wlan status through WMI.
1319 * This is also the behavior that windows app will do.
1321 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1322 priv->asus->driver->wlan_ctrl_by_user)
1323 dev_id = ASUS_WMI_DEVID_WLAN_LED;
1325 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
1328 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
1330 struct asus_rfkill *priv = data;
1333 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
1338 rfkill_set_sw_state(priv->rfkill, !result);
1341 static int asus_rfkill_wlan_set(void *data, bool blocked)
1343 struct asus_rfkill *priv = data;
1344 struct asus_wmi *asus = priv->asus;
1348 * This handler is enabled only if hotplug is enabled.
1349 * In this case, the asus_wmi_set_devstate() will
1350 * trigger a wmi notification and we need to wait
1351 * this call to finish before being able to call
1354 mutex_lock(&asus->wmi_lock);
1355 ret = asus_rfkill_set(data, blocked);
1356 mutex_unlock(&asus->wmi_lock);
1360 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1361 .set_block = asus_rfkill_wlan_set,
1362 .query = asus_rfkill_query,
1365 static const struct rfkill_ops asus_rfkill_ops = {
1366 .set_block = asus_rfkill_set,
1367 .query = asus_rfkill_query,
1370 static int asus_new_rfkill(struct asus_wmi *asus,
1371 struct asus_rfkill *arfkill,
1372 const char *name, enum rfkill_type type, int dev_id)
1374 int result = asus_wmi_get_devstate_simple(asus, dev_id);
1375 struct rfkill **rfkill = &arfkill->rfkill;
1380 arfkill->dev_id = dev_id;
1381 arfkill->asus = asus;
1383 if (dev_id == ASUS_WMI_DEVID_WLAN &&
1384 asus->driver->quirks->hotplug_wireless)
1385 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1386 &asus_rfkill_wlan_ops, arfkill);
1388 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1389 &asus_rfkill_ops, arfkill);
1394 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1395 (asus->driver->quirks->wapf > 0))
1396 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1398 rfkill_init_sw_state(*rfkill, !result);
1399 result = rfkill_register(*rfkill);
1401 rfkill_destroy(*rfkill);
1408 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1410 if (asus->driver->wlan_ctrl_by_user && ashs_present())
1413 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1414 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1415 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1416 if (asus->wlan.rfkill) {
1417 rfkill_unregister(asus->wlan.rfkill);
1418 rfkill_destroy(asus->wlan.rfkill);
1419 asus->wlan.rfkill = NULL;
1422 * Refresh pci hotplug in case the rfkill state was changed after
1423 * asus_unregister_rfkill_notifier()
1425 asus_rfkill_hotplug(asus);
1426 if (asus->hotplug_slot.ops)
1427 pci_hp_deregister(&asus->hotplug_slot);
1428 if (asus->hotplug_workqueue)
1429 destroy_workqueue(asus->hotplug_workqueue);
1431 if (asus->bluetooth.rfkill) {
1432 rfkill_unregister(asus->bluetooth.rfkill);
1433 rfkill_destroy(asus->bluetooth.rfkill);
1434 asus->bluetooth.rfkill = NULL;
1436 if (asus->wimax.rfkill) {
1437 rfkill_unregister(asus->wimax.rfkill);
1438 rfkill_destroy(asus->wimax.rfkill);
1439 asus->wimax.rfkill = NULL;
1441 if (asus->wwan3g.rfkill) {
1442 rfkill_unregister(asus->wwan3g.rfkill);
1443 rfkill_destroy(asus->wwan3g.rfkill);
1444 asus->wwan3g.rfkill = NULL;
1446 if (asus->gps.rfkill) {
1447 rfkill_unregister(asus->gps.rfkill);
1448 rfkill_destroy(asus->gps.rfkill);
1449 asus->gps.rfkill = NULL;
1451 if (asus->uwb.rfkill) {
1452 rfkill_unregister(asus->uwb.rfkill);
1453 rfkill_destroy(asus->uwb.rfkill);
1454 asus->uwb.rfkill = NULL;
1458 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1462 mutex_init(&asus->hotplug_lock);
1463 mutex_init(&asus->wmi_lock);
1465 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1466 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1468 if (result && result != -ENODEV)
1471 result = asus_new_rfkill(asus, &asus->bluetooth,
1472 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1473 ASUS_WMI_DEVID_BLUETOOTH);
1475 if (result && result != -ENODEV)
1478 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1479 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1481 if (result && result != -ENODEV)
1484 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1485 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1487 if (result && result != -ENODEV)
1490 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1491 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1493 if (result && result != -ENODEV)
1496 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1497 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1499 if (result && result != -ENODEV)
1502 if (!asus->driver->quirks->hotplug_wireless)
1505 result = asus_setup_pci_hotplug(asus);
1507 * If we get -EBUSY then something else is handling the PCI hotplug -
1508 * don't fail in this case
1510 if (result == -EBUSY)
1513 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1514 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1515 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1517 * Refresh pci hotplug in case the rfkill state was changed during
1520 asus_rfkill_hotplug(asus);
1523 if (result && result != -ENODEV)
1524 asus_wmi_rfkill_exit(asus);
1526 if (result == -ENODEV)
1532 /* Panel Overdrive ************************************************************/
1533 static int panel_od_check_present(struct asus_wmi *asus)
1538 asus->panel_overdrive_available = false;
1540 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_PANEL_OD, &result);
1547 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
1548 asus->panel_overdrive_available = true;
1549 asus->panel_overdrive = result & ASUS_WMI_DSTS_STATUS_BIT;
1555 static int panel_od_write(struct asus_wmi *asus)
1561 /* Don't rely on type conversion */
1562 value = asus->panel_overdrive ? 1 : 0;
1564 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, value, &retval);
1567 pr_warn("Failed to set panel overdrive: %d\n", err);
1572 pr_warn("Failed to set panel overdrive (retval): 0x%x\n", retval);
1576 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
1581 static ssize_t panel_od_show(struct device *dev,
1582 struct device_attribute *attr, char *buf)
1584 struct asus_wmi *asus = dev_get_drvdata(dev);
1586 return sysfs_emit(buf, "%d\n", asus->panel_overdrive);
1589 static ssize_t panel_od_store(struct device *dev,
1590 struct device_attribute *attr,
1591 const char *buf, size_t count)
1596 struct asus_wmi *asus = dev_get_drvdata(dev);
1598 result = kstrtobool(buf, &overdrive);
1602 asus->panel_overdrive = overdrive;
1603 result = panel_od_write(asus);
1611 static DEVICE_ATTR_RW(panel_od);
1613 /* Quirks *********************************************************************/
1615 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1617 struct pci_dev *xhci_pdev;
1618 u32 orig_ports_available;
1619 u32 ports_available = asus->driver->quirks->xusb2pr;
1621 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1622 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1628 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1629 &orig_ports_available);
1631 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1632 cpu_to_le32(ports_available));
1634 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1635 orig_ports_available, ports_available);
1639 * Some devices dont support or have borcken get_als method
1640 * but still support set method.
1642 static void asus_wmi_set_als(void)
1644 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1647 /* Hwmon device ***************************************************************/
1649 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1652 struct agfn_fan_args args = {
1653 .agfn.len = sizeof(args),
1654 .agfn.mfun = ASUS_FAN_MFUN,
1655 .agfn.sfun = ASUS_FAN_SFUN_READ,
1659 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1665 status = asus_wmi_evaluate_method_agfn(input);
1667 if (status || args.agfn.err)
1671 *speed = args.speed;
1676 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1679 struct agfn_fan_args args = {
1680 .agfn.len = sizeof(args),
1681 .agfn.mfun = ASUS_FAN_MFUN,
1682 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1684 .speed = speed ? *speed : 0,
1686 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1689 /* 1: for setting 1st fan's speed 0: setting auto mode */
1690 if (fan != 1 && fan != 0)
1693 status = asus_wmi_evaluate_method_agfn(input);
1695 if (status || args.agfn.err)
1698 if (speed && fan == 1)
1699 asus->agfn_pwm = *speed;
1705 * Check if we can read the speed of one fan. If true we assume we can also
1708 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
1714 status = asus_agfn_fan_speed_read(asus, 1, &speed);
1718 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1723 * We need to find a better way, probably using sfun,
1725 * Currently we disable it if:
1726 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1727 * - reverved bits are non-zero
1728 * - sfun and presence bit are not set
1730 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1731 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
1734 static int asus_fan_set_auto(struct asus_wmi *asus)
1739 switch (asus->fan_type) {
1740 case FAN_TYPE_SPEC83:
1741 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1751 status = asus_agfn_fan_speed_write(asus, 0, NULL);
1764 static ssize_t pwm1_show(struct device *dev,
1765 struct device_attribute *attr,
1768 struct asus_wmi *asus = dev_get_drvdata(dev);
1772 /* If we already set a value then just return it */
1773 if (asus->agfn_pwm >= 0)
1774 return sprintf(buf, "%d\n", asus->agfn_pwm);
1777 * If we haven't set already set a value through the AGFN interface,
1778 * we read a current value through the (now-deprecated) FAN_CTRL device.
1780 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1786 if (value == 1) /* Low Speed */
1788 else if (value == 2)
1790 else if (value == 3)
1793 pr_err("Unknown fan speed %#x\n", value);
1797 return sprintf(buf, "%d\n", value);
1800 static ssize_t pwm1_store(struct device *dev,
1801 struct device_attribute *attr,
1802 const char *buf, size_t count) {
1803 struct asus_wmi *asus = dev_get_drvdata(dev);
1808 ret = kstrtouint(buf, 10, &value);
1812 value = clamp(value, 0, 255);
1814 state = asus_agfn_fan_speed_write(asus, 1, &value);
1816 pr_warn("Setting fan speed failed: %d\n", state);
1818 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
1823 static ssize_t fan1_input_show(struct device *dev,
1824 struct device_attribute *attr,
1827 struct asus_wmi *asus = dev_get_drvdata(dev);
1831 switch (asus->fan_type) {
1832 case FAN_TYPE_SPEC83:
1833 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1842 /* no speed readable on manual mode */
1843 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1846 ret = asus_agfn_fan_speed_read(asus, 1, &value);
1848 pr_warn("reading fan speed failed: %d\n", ret);
1857 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1860 static ssize_t pwm1_enable_show(struct device *dev,
1861 struct device_attribute *attr,
1864 struct asus_wmi *asus = dev_get_drvdata(dev);
1867 * Just read back the cached pwm mode.
1869 * For the CPU_FAN device, the spec indicates that we should be
1870 * able to read the device status and consult bit 19 to see if we
1871 * are in Full On or Automatic mode. However, this does not work
1872 * in practice on X532FL at least (the bit is always 0) and there's
1873 * also nothing in the DSDT to indicate that this behaviour exists.
1875 return sprintf(buf, "%d\n", asus->fan_pwm_mode);
1878 static ssize_t pwm1_enable_store(struct device *dev,
1879 struct device_attribute *attr,
1880 const char *buf, size_t count)
1882 struct asus_wmi *asus = dev_get_drvdata(dev);
1889 ret = kstrtouint(buf, 10, &state);
1893 if (asus->fan_type == FAN_TYPE_SPEC83) {
1894 switch (state) { /* standard documented hwmon values */
1895 case ASUS_FAN_CTRL_FULLSPEED:
1898 case ASUS_FAN_CTRL_AUTO:
1905 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1912 } else if (asus->fan_type == FAN_TYPE_AGFN) {
1914 case ASUS_FAN_CTRL_MANUAL:
1917 case ASUS_FAN_CTRL_AUTO:
1918 status = asus_fan_set_auto(asus);
1928 asus->fan_pwm_mode = state;
1930 /* Must set to disabled if mode is toggled */
1931 if (asus->cpu_fan_curve_available)
1932 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
1933 if (asus->gpu_fan_curve_available)
1934 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
1939 static ssize_t fan1_label_show(struct device *dev,
1940 struct device_attribute *attr,
1943 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1946 static ssize_t asus_hwmon_temp1(struct device *dev,
1947 struct device_attribute *attr,
1950 struct asus_wmi *asus = dev_get_drvdata(dev);
1954 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1958 return sprintf(buf, "%ld\n",
1959 deci_kelvin_to_millicelsius(value & 0xFFFF));
1963 static DEVICE_ATTR_RW(pwm1);
1964 static DEVICE_ATTR_RW(pwm1_enable);
1965 static DEVICE_ATTR_RO(fan1_input);
1966 static DEVICE_ATTR_RO(fan1_label);
1969 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1971 static struct attribute *hwmon_attributes[] = {
1972 &dev_attr_pwm1.attr,
1973 &dev_attr_pwm1_enable.attr,
1974 &dev_attr_fan1_input.attr,
1975 &dev_attr_fan1_label.attr,
1977 &dev_attr_temp1_input.attr,
1981 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1982 struct attribute *attr, int idx)
1984 struct device *dev = container_of(kobj, struct device, kobj);
1985 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
1986 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1988 if (attr == &dev_attr_pwm1.attr) {
1989 if (asus->fan_type != FAN_TYPE_AGFN)
1991 } else if (attr == &dev_attr_fan1_input.attr
1992 || attr == &dev_attr_fan1_label.attr
1993 || attr == &dev_attr_pwm1_enable.attr) {
1994 if (asus->fan_type == FAN_TYPE_NONE)
1996 } else if (attr == &dev_attr_temp1_input.attr) {
1997 int err = asus_wmi_get_devstate(asus,
1998 ASUS_WMI_DEVID_THERMAL_CTRL,
2002 return 0; /* can't return negative here */
2005 * If the temperature value in deci-Kelvin is near the absolute
2006 * zero temperature, something is clearly wrong
2008 if (value == 0 || value == 1)
2015 static const struct attribute_group hwmon_attribute_group = {
2016 .is_visible = asus_hwmon_sysfs_is_visible,
2017 .attrs = hwmon_attributes
2019 __ATTRIBUTE_GROUPS(hwmon_attribute);
2021 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
2023 struct device *dev = &asus->platform_device->dev;
2024 struct device *hwmon;
2026 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
2027 hwmon_attribute_groups);
2029 if (IS_ERR(hwmon)) {
2030 pr_err("Could not register asus hwmon device\n");
2031 return PTR_ERR(hwmon);
2036 static int asus_wmi_fan_init(struct asus_wmi *asus)
2038 asus->fan_type = FAN_TYPE_NONE;
2039 asus->agfn_pwm = -1;
2041 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
2042 asus->fan_type = FAN_TYPE_SPEC83;
2043 else if (asus_wmi_has_agfn_fan(asus))
2044 asus->fan_type = FAN_TYPE_AGFN;
2046 if (asus->fan_type == FAN_TYPE_NONE)
2049 asus_fan_set_auto(asus);
2050 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
2054 /* Fan mode *******************************************************************/
2056 static int fan_boost_mode_check_present(struct asus_wmi *asus)
2061 asus->fan_boost_mode_available = false;
2063 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
2072 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2073 (result & ASUS_FAN_BOOST_MODES_MASK)) {
2074 asus->fan_boost_mode_available = true;
2075 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
2081 static int fan_boost_mode_write(struct asus_wmi *asus)
2087 value = asus->fan_boost_mode;
2089 pr_info("Set fan boost mode: %u\n", value);
2090 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
2093 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2097 pr_warn("Failed to set fan boost mode: %d\n", err);
2102 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
2110 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
2112 u8 mask = asus->fan_boost_mode_mask;
2114 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
2115 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
2116 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
2117 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2118 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2119 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2120 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2121 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2123 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2125 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2128 return fan_boost_mode_write(asus);
2131 static ssize_t fan_boost_mode_show(struct device *dev,
2132 struct device_attribute *attr, char *buf)
2134 struct asus_wmi *asus = dev_get_drvdata(dev);
2136 return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
2139 static ssize_t fan_boost_mode_store(struct device *dev,
2140 struct device_attribute *attr,
2141 const char *buf, size_t count)
2143 struct asus_wmi *asus = dev_get_drvdata(dev);
2144 u8 mask = asus->fan_boost_mode_mask;
2148 result = kstrtou8(buf, 10, &new_mode);
2150 pr_warn("Trying to store invalid value\n");
2154 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2155 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
2157 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
2158 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
2160 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
2164 asus->fan_boost_mode = new_mode;
2165 fan_boost_mode_write(asus);
2170 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
2171 static DEVICE_ATTR_RW(fan_boost_mode);
2173 /* Custom fan curves **********************************************************/
2175 static void fan_curve_copy_from_buf(struct fan_curve_data *data, u8 *buf)
2179 for (i = 0; i < FAN_CURVE_POINTS; i++) {
2180 data->temps[i] = buf[i];
2183 for (i = 0; i < FAN_CURVE_POINTS; i++) {
2185 255 * buf[i + FAN_CURVE_POINTS] / 100;
2189 static int fan_curve_get_factory_default(struct asus_wmi *asus, u32 fan_dev)
2191 struct fan_curve_data *curves;
2192 u8 buf[FAN_CURVE_BUF_LEN];
2197 if (asus->throttle_thermal_policy_available)
2198 mode = asus->throttle_thermal_policy_mode;
2199 /* DEVID_<C/G>PU_FAN_CURVE is switched for OVERBOOST vs SILENT */
2205 if (fan_dev == ASUS_WMI_DEVID_GPU_FAN_CURVE)
2206 fan_idx = FAN_CURVE_DEV_GPU;
2208 curves = &asus->custom_fan_curves[fan_idx];
2209 err = asus_wmi_evaluate_method_buf(asus->dsts_id, fan_dev, mode, buf,
2214 fan_curve_copy_from_buf(curves, buf);
2215 curves->device_id = fan_dev;
2220 /* Check if capability exists, and populate defaults */
2221 static int fan_curve_check_present(struct asus_wmi *asus, bool *available,
2228 err = fan_curve_get_factory_default(asus, fan_dev);
2230 pr_debug("fan_curve_get_factory_default(0x%08x) failed: %d\n",
2232 /* Don't cause probe to fail on devices without fan-curves */
2240 /* Determine which fan the attribute is for if SENSOR_ATTR */
2241 static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus,
2242 struct device_attribute *attr)
2244 int index = to_sensor_dev_attr(attr)->index;
2246 return &asus->custom_fan_curves[index & FAN_CURVE_DEV_GPU];
2249 /* Determine which fan the attribute is for if SENSOR_ATTR_2 */
2250 static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus,
2251 struct device_attribute *attr)
2253 int nr = to_sensor_dev_attr_2(attr)->nr;
2255 return &asus->custom_fan_curves[nr & FAN_CURVE_DEV_GPU];
2258 static ssize_t fan_curve_show(struct device *dev,
2259 struct device_attribute *attr, char *buf)
2261 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2262 struct asus_wmi *asus = dev_get_drvdata(dev);
2263 struct fan_curve_data *data;
2264 int value, index, nr;
2266 data = fan_curve_attr_2_select(asus, attr);
2267 index = dev_attr->index;
2270 if (nr & FAN_CURVE_PWM_MASK)
2271 value = data->percents[index];
2273 value = data->temps[index];
2275 return sysfs_emit(buf, "%d\n", value);
2279 * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE.
2281 static int fan_curve_write(struct asus_wmi *asus,
2282 struct fan_curve_data *data)
2284 u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0;
2285 u8 *percents = data->percents;
2286 u8 *temps = data->temps;
2287 int ret, i, shift = 0;
2292 for (i = 0; i < FAN_CURVE_POINTS / 2; i++) {
2293 arg1 += (temps[i]) << shift;
2294 arg2 += (temps[i + 4]) << shift;
2295 /* Scale to percentage for device */
2296 arg3 += (100 * percents[i] / 255) << shift;
2297 arg4 += (100 * percents[i + 4] / 255) << shift;
2301 return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS,
2303 arg1, arg2, arg3, arg4, &ret);
2306 static ssize_t fan_curve_store(struct device *dev,
2307 struct device_attribute *attr, const char *buf,
2310 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2311 struct asus_wmi *asus = dev_get_drvdata(dev);
2312 struct fan_curve_data *data;
2316 int pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
2317 int index = dev_attr->index;
2319 data = fan_curve_attr_2_select(asus, attr);
2321 err = kstrtou8(buf, 10, &value);
2326 data->percents[index] = value;
2328 data->temps[index] = value;
2332 * Mark as disabled so the user has to explicitly enable to apply a
2333 * changed fan curve. This prevents potential lockups from writing out
2334 * many changes as one-write-per-change.
2336 data->enabled = false;
2341 static ssize_t fan_curve_enable_show(struct device *dev,
2342 struct device_attribute *attr, char *buf)
2344 struct asus_wmi *asus = dev_get_drvdata(dev);
2345 struct fan_curve_data *data;
2348 data = fan_curve_attr_select(asus, attr);
2353 return sysfs_emit(buf, "%d\n", out);
2356 static ssize_t fan_curve_enable_store(struct device *dev,
2357 struct device_attribute *attr,
2358 const char *buf, size_t count)
2360 struct asus_wmi *asus = dev_get_drvdata(dev);
2361 struct fan_curve_data *data;
2364 data = fan_curve_attr_select(asus, attr);
2366 err = kstrtoint(buf, 10, &value);
2372 data->enabled = true;
2375 data->enabled = false;
2378 * Auto + reset the fan curve data to defaults. Make it an explicit
2379 * option so that users don't accidentally overwrite a set fan curve.
2382 err = fan_curve_get_factory_default(asus, data->device_id);
2385 data->enabled = false;
2391 if (data->enabled) {
2392 err = fan_curve_write(asus, data);
2397 * For machines with throttle this is the only way to reset fans
2398 * to default mode of operation (does not erase curve data).
2400 if (asus->throttle_thermal_policy_available) {
2401 err = throttle_thermal_policy_write(asus);
2404 /* Similar is true for laptops with this fan */
2405 } else if (asus->fan_type == FAN_TYPE_SPEC83) {
2406 err = asus_fan_set_auto(asus);
2410 /* Safeguard against fautly ACPI tables */
2411 err = fan_curve_get_factory_default(asus, data->device_id);
2414 err = fan_curve_write(asus, data);
2423 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU);
2424 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve,
2425 FAN_CURVE_DEV_CPU, 0);
2426 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve,
2427 FAN_CURVE_DEV_CPU, 1);
2428 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve,
2429 FAN_CURVE_DEV_CPU, 2);
2430 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve,
2431 FAN_CURVE_DEV_CPU, 3);
2432 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve,
2433 FAN_CURVE_DEV_CPU, 4);
2434 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve,
2435 FAN_CURVE_DEV_CPU, 5);
2436 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve,
2437 FAN_CURVE_DEV_CPU, 6);
2438 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve,
2439 FAN_CURVE_DEV_CPU, 7);
2441 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve,
2442 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0);
2443 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve,
2444 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1);
2445 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve,
2446 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2);
2447 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve,
2448 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3);
2449 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve,
2450 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4);
2451 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve,
2452 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5);
2453 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve,
2454 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6);
2455 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve,
2456 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7);
2459 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU);
2460 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve,
2461 FAN_CURVE_DEV_GPU, 0);
2462 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve,
2463 FAN_CURVE_DEV_GPU, 1);
2464 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve,
2465 FAN_CURVE_DEV_GPU, 2);
2466 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve,
2467 FAN_CURVE_DEV_GPU, 3);
2468 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve,
2469 FAN_CURVE_DEV_GPU, 4);
2470 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve,
2471 FAN_CURVE_DEV_GPU, 5);
2472 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve,
2473 FAN_CURVE_DEV_GPU, 6);
2474 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve,
2475 FAN_CURVE_DEV_GPU, 7);
2477 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve,
2478 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0);
2479 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve,
2480 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1);
2481 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve,
2482 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2);
2483 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve,
2484 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3);
2485 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve,
2486 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4);
2487 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve,
2488 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5);
2489 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve,
2490 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6);
2491 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve,
2492 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7);
2494 static struct attribute *asus_fan_curve_attr[] = {
2496 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2497 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2498 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2499 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2500 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2501 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
2502 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
2503 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
2504 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
2505 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2506 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2507 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2508 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2509 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
2510 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
2511 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
2512 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
2514 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2515 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2516 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2517 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2518 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2519 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
2520 &sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr,
2521 &sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr,
2522 &sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr,
2523 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
2524 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2525 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2526 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2527 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
2528 &sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr,
2529 &sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr,
2530 &sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr,
2534 static umode_t asus_fan_curve_is_visible(struct kobject *kobj,
2535 struct attribute *attr, int idx)
2537 struct device *dev = kobj_to_dev(kobj);
2538 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2541 * Check the char instead of casting attr as there are two attr types
2542 * involved here (attr1 and attr2)
2544 if (asus->cpu_fan_curve_available && attr->name[3] == '1')
2547 if (asus->gpu_fan_curve_available && attr->name[3] == '2')
2553 static const struct attribute_group asus_fan_curve_attr_group = {
2554 .is_visible = asus_fan_curve_is_visible,
2555 .attrs = asus_fan_curve_attr,
2557 __ATTRIBUTE_GROUPS(asus_fan_curve_attr);
2560 * Must be initialised after throttle_thermal_policy_check_present() as
2561 * we check the status of throttle_thermal_policy_available during init.
2563 static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus)
2565 struct device *dev = &asus->platform_device->dev;
2566 struct device *hwmon;
2569 err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available,
2570 ASUS_WMI_DEVID_CPU_FAN_CURVE);
2574 err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available,
2575 ASUS_WMI_DEVID_GPU_FAN_CURVE);
2579 if (!asus->cpu_fan_curve_available && !asus->gpu_fan_curve_available)
2582 hwmon = devm_hwmon_device_register_with_groups(
2583 dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups);
2585 if (IS_ERR(hwmon)) {
2587 "Could not register asus_custom_fan_curve device\n");
2588 return PTR_ERR(hwmon);
2594 /* Throttle thermal policy ****************************************************/
2596 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
2601 asus->throttle_thermal_policy_available = false;
2603 err = asus_wmi_get_devstate(asus,
2604 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2612 if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
2613 asus->throttle_thermal_policy_available = true;
2618 static int throttle_thermal_policy_write(struct asus_wmi *asus)
2624 value = asus->throttle_thermal_policy_mode;
2626 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2629 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2630 "throttle_thermal_policy");
2633 pr_warn("Failed to set throttle thermal policy: %d\n", err);
2638 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
2643 /* Must set to disabled if mode is toggled */
2644 if (asus->cpu_fan_curve_available)
2645 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
2646 if (asus->gpu_fan_curve_available)
2647 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
2652 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
2654 if (!asus->throttle_thermal_policy_available)
2657 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2658 return throttle_thermal_policy_write(asus);
2661 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
2663 u8 new_mode = asus->throttle_thermal_policy_mode + 1;
2666 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2667 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2669 asus->throttle_thermal_policy_mode = new_mode;
2670 err = throttle_thermal_policy_write(asus);
2675 * Ensure that platform_profile updates userspace with the change to ensure
2676 * that platform_profile and throttle_thermal_policy_mode are in sync.
2678 platform_profile_notify();
2683 static ssize_t throttle_thermal_policy_show(struct device *dev,
2684 struct device_attribute *attr, char *buf)
2686 struct asus_wmi *asus = dev_get_drvdata(dev);
2687 u8 mode = asus->throttle_thermal_policy_mode;
2689 return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
2692 static ssize_t throttle_thermal_policy_store(struct device *dev,
2693 struct device_attribute *attr,
2694 const char *buf, size_t count)
2696 struct asus_wmi *asus = dev_get_drvdata(dev);
2701 result = kstrtou8(buf, 10, &new_mode);
2705 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2708 asus->throttle_thermal_policy_mode = new_mode;
2709 err = throttle_thermal_policy_write(asus);
2714 * Ensure that platform_profile updates userspace with the change to ensure
2715 * that platform_profile and throttle_thermal_policy_mode are in sync.
2717 platform_profile_notify();
2722 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
2723 static DEVICE_ATTR_RW(throttle_thermal_policy);
2725 /* Platform profile ***********************************************************/
2726 static int asus_wmi_platform_profile_get(struct platform_profile_handler *pprof,
2727 enum platform_profile_option *profile)
2729 struct asus_wmi *asus;
2732 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2734 tp = asus->throttle_thermal_policy_mode;
2737 case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
2738 *profile = PLATFORM_PROFILE_BALANCED;
2740 case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
2741 *profile = PLATFORM_PROFILE_PERFORMANCE;
2743 case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
2744 *profile = PLATFORM_PROFILE_QUIET;
2753 static int asus_wmi_platform_profile_set(struct platform_profile_handler *pprof,
2754 enum platform_profile_option profile)
2756 struct asus_wmi *asus;
2759 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2762 case PLATFORM_PROFILE_PERFORMANCE:
2763 tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
2765 case PLATFORM_PROFILE_BALANCED:
2766 tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2768 case PLATFORM_PROFILE_QUIET:
2769 tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
2775 asus->throttle_thermal_policy_mode = tp;
2776 return throttle_thermal_policy_write(asus);
2779 static int platform_profile_setup(struct asus_wmi *asus)
2781 struct device *dev = &asus->platform_device->dev;
2785 * Not an error if a component platform_profile relies on is unavailable
2786 * so early return, skipping the setup of platform_profile.
2788 if (!asus->throttle_thermal_policy_available)
2791 dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
2793 asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
2794 asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
2796 set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
2797 set_bit(PLATFORM_PROFILE_BALANCED,
2798 asus->platform_profile_handler.choices);
2799 set_bit(PLATFORM_PROFILE_PERFORMANCE,
2800 asus->platform_profile_handler.choices);
2802 err = platform_profile_register(&asus->platform_profile_handler);
2806 asus->platform_profile_support = true;
2810 /* Backlight ******************************************************************/
2812 static int read_backlight_power(struct asus_wmi *asus)
2816 if (asus->driver->quirks->store_backlight_power)
2817 ret = !asus->driver->panel_power;
2819 ret = asus_wmi_get_devstate_simple(asus,
2820 ASUS_WMI_DEVID_BACKLIGHT);
2825 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
2828 static int read_brightness_max(struct asus_wmi *asus)
2833 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2837 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
2846 static int read_brightness(struct backlight_device *bd)
2848 struct asus_wmi *asus = bl_get_data(bd);
2852 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2856 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
2859 static u32 get_scalar_command(struct backlight_device *bd)
2861 struct asus_wmi *asus = bl_get_data(bd);
2864 if ((asus->driver->brightness < bd->props.brightness) ||
2865 bd->props.brightness == bd->props.max_brightness)
2866 ctrl_param = 0x00008001;
2867 else if ((asus->driver->brightness > bd->props.brightness) ||
2868 bd->props.brightness == 0)
2869 ctrl_param = 0x00008000;
2871 asus->driver->brightness = bd->props.brightness;
2876 static int update_bl_status(struct backlight_device *bd)
2878 struct asus_wmi *asus = bl_get_data(bd);
2882 power = read_backlight_power(asus);
2883 if (power != -ENODEV && bd->props.power != power) {
2884 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
2885 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
2887 if (asus->driver->quirks->store_backlight_power)
2888 asus->driver->panel_power = bd->props.power;
2890 /* When using scalar brightness, updating the brightness
2891 * will mess with the backlight power */
2892 if (asus->driver->quirks->scalar_panel_brightness)
2896 if (asus->driver->quirks->scalar_panel_brightness)
2897 ctrl_param = get_scalar_command(bd);
2899 ctrl_param = bd->props.brightness;
2901 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
2907 static const struct backlight_ops asus_wmi_bl_ops = {
2908 .get_brightness = read_brightness,
2909 .update_status = update_bl_status,
2912 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
2914 struct backlight_device *bd = asus->backlight_device;
2915 int old = bd->props.brightness;
2918 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2919 new = code - NOTIFY_BRNUP_MIN + 1;
2920 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2921 new = code - NOTIFY_BRNDOWN_MIN;
2923 bd->props.brightness = new;
2924 backlight_update_status(bd);
2925 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
2930 static int asus_wmi_backlight_init(struct asus_wmi *asus)
2932 struct backlight_device *bd;
2933 struct backlight_properties props;
2937 max = read_brightness_max(asus);
2941 power = read_backlight_power(asus);
2942 if (power == -ENODEV)
2943 power = FB_BLANK_UNBLANK;
2947 memset(&props, 0, sizeof(struct backlight_properties));
2948 props.type = BACKLIGHT_PLATFORM;
2949 props.max_brightness = max;
2950 bd = backlight_device_register(asus->driver->name,
2951 &asus->platform_device->dev, asus,
2952 &asus_wmi_bl_ops, &props);
2954 pr_err("Could not register backlight device\n");
2958 asus->backlight_device = bd;
2960 if (asus->driver->quirks->store_backlight_power)
2961 asus->driver->panel_power = power;
2963 bd->props.brightness = read_brightness(bd);
2964 bd->props.power = power;
2965 backlight_update_status(bd);
2967 asus->driver->brightness = bd->props.brightness;
2972 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
2974 backlight_device_unregister(asus->backlight_device);
2976 asus->backlight_device = NULL;
2979 static int is_display_toggle(int code)
2981 /* display toggle keys */
2982 if ((code >= 0x61 && code <= 0x67) ||
2983 (code >= 0x8c && code <= 0x93) ||
2984 (code >= 0xa0 && code <= 0xa7) ||
2985 (code >= 0xd0 && code <= 0xd5))
2991 /* Fn-lock ********************************************************************/
2993 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
2997 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
2999 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
3000 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
3003 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
3005 int mode = asus->fnlock_locked;
3007 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
3010 /* WMI events *****************************************************************/
3012 static int asus_wmi_get_event_code(u32 value)
3014 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
3015 union acpi_object *obj;
3019 status = wmi_get_event_data(value, &response);
3020 if (ACPI_FAILURE(status)) {
3021 pr_warn("Failed to get WMI notify code: %s\n",
3022 acpi_format_exception(status));
3026 obj = (union acpi_object *)response.pointer;
3028 if (obj && obj->type == ACPI_TYPE_INTEGER)
3029 code = (int)(obj->integer.value & WMI_EVENT_MASK);
3037 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
3039 unsigned int key_value = 1;
3040 bool autorelease = 1;
3041 int result, orig_code;
3045 if (asus->driver->key_filter) {
3046 asus->driver->key_filter(asus->driver, &code, &key_value,
3048 if (code == ASUS_WMI_KEY_IGNORE)
3052 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3053 code = ASUS_WMI_BRN_UP;
3054 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3055 code = ASUS_WMI_BRN_DOWN;
3057 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
3058 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3059 asus_wmi_backlight_notify(asus, orig_code);
3064 if (code == NOTIFY_KBD_BRTUP) {
3065 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3068 if (code == NOTIFY_KBD_BRTDWN) {
3069 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
3072 if (code == NOTIFY_KBD_BRTTOGGLE) {
3073 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
3074 kbd_led_set_by_kbd(asus, 0);
3076 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3080 if (code == NOTIFY_FNLOCK_TOGGLE) {
3081 asus->fnlock_locked = !asus->fnlock_locked;
3082 asus_wmi_fnlock_update(asus);
3086 if (asus->driver->quirks->use_kbd_dock_devid && code == NOTIFY_KBD_DOCK_CHANGE) {
3087 result = asus_wmi_get_devstate_simple(asus,
3088 ASUS_WMI_DEVID_KBD_DOCK);
3090 input_report_switch(asus->inputdev, SW_TABLET_MODE,
3092 input_sync(asus->inputdev);
3097 if (asus->driver->quirks->use_lid_flip_devid && code == NOTIFY_LID_FLIP) {
3098 lid_flip_tablet_mode_get_state(asus);
3102 if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
3103 fan_boost_mode_switch_next(asus);
3107 if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
3108 throttle_thermal_policy_switch_next(asus);
3112 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
3115 if (!sparse_keymap_report_event(asus->inputdev, code,
3116 key_value, autorelease))
3117 pr_info("Unknown key code 0x%x\n", code);
3120 static void asus_wmi_notify(u32 value, void *context)
3122 struct asus_wmi *asus = context;
3126 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3127 code = asus_wmi_get_event_code(value);
3129 pr_warn("Failed to get notify code: %d\n", code);
3133 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3136 asus_wmi_handle_event_code(code, asus);
3139 * Double check that queue is present:
3140 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
3142 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
3146 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
3149 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
3154 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3155 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
3157 pr_warn("Failed to get event during flush: %d\n", code);
3161 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3165 pr_warn("Failed to flush event queue\n");
3169 /* Sysfs **********************************************************************/
3171 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
3172 const char *buf, size_t count)
3177 value = asus_wmi_get_devstate_simple(asus, devid);
3181 err = kstrtoint(buf, 0, &value);
3185 err = asus_wmi_set_devstate(devid, value, &retval);
3192 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
3194 int value = asus_wmi_get_devstate_simple(asus, devid);
3199 return sprintf(buf, "%d\n", value);
3202 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
3203 static ssize_t show_##_name(struct device *dev, \
3204 struct device_attribute *attr, \
3207 struct asus_wmi *asus = dev_get_drvdata(dev); \
3209 return show_sys_wmi(asus, _cm, buf); \
3211 static ssize_t store_##_name(struct device *dev, \
3212 struct device_attribute *attr, \
3213 const char *buf, size_t count) \
3215 struct asus_wmi *asus = dev_get_drvdata(dev); \
3217 return store_sys_wmi(asus, _cm, buf, count); \
3219 static struct device_attribute dev_attr_##_name = { \
3221 .name = __stringify(_name), \
3223 .show = show_##_name, \
3224 .store = store_##_name, \
3227 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
3228 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
3229 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
3230 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
3231 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
3233 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
3234 const char *buf, size_t count)
3238 rv = kstrtoint(buf, 0, &value);
3242 if (value < 0 || value > 2)
3245 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
3252 static DEVICE_ATTR_WO(cpufv);
3254 static struct attribute *platform_attributes[] = {
3255 &dev_attr_cpufv.attr,
3256 &dev_attr_camera.attr,
3257 &dev_attr_cardr.attr,
3258 &dev_attr_touchpad.attr,
3259 &dev_attr_egpu_enable.attr,
3260 &dev_attr_dgpu_disable.attr,
3261 &dev_attr_lid_resume.attr,
3262 &dev_attr_als_enable.attr,
3263 &dev_attr_fan_boost_mode.attr,
3264 &dev_attr_throttle_thermal_policy.attr,
3265 &dev_attr_panel_od.attr,
3269 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
3270 struct attribute *attr, int idx)
3272 struct device *dev = container_of(kobj, struct device, kobj);
3273 struct asus_wmi *asus = dev_get_drvdata(dev);
3277 if (attr == &dev_attr_camera.attr)
3278 devid = ASUS_WMI_DEVID_CAMERA;
3279 else if (attr == &dev_attr_cardr.attr)
3280 devid = ASUS_WMI_DEVID_CARDREADER;
3281 else if (attr == &dev_attr_touchpad.attr)
3282 devid = ASUS_WMI_DEVID_TOUCHPAD;
3283 else if (attr == &dev_attr_lid_resume.attr)
3284 devid = ASUS_WMI_DEVID_LID_RESUME;
3285 else if (attr == &dev_attr_als_enable.attr)
3286 devid = ASUS_WMI_DEVID_ALS_ENABLE;
3287 else if (attr == &dev_attr_egpu_enable.attr)
3288 ok = asus->egpu_enable_available;
3289 else if (attr == &dev_attr_dgpu_disable.attr)
3290 ok = asus->dgpu_disable_available;
3291 else if (attr == &dev_attr_fan_boost_mode.attr)
3292 ok = asus->fan_boost_mode_available;
3293 else if (attr == &dev_attr_throttle_thermal_policy.attr)
3294 ok = asus->throttle_thermal_policy_available;
3295 else if (attr == &dev_attr_panel_od.attr)
3296 ok = asus->panel_overdrive_available;
3299 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
3301 return ok ? attr->mode : 0;
3304 static const struct attribute_group platform_attribute_group = {
3305 .is_visible = asus_sysfs_is_visible,
3306 .attrs = platform_attributes
3309 static void asus_wmi_sysfs_exit(struct platform_device *device)
3311 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
3314 static int asus_wmi_sysfs_init(struct platform_device *device)
3316 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
3319 /* Platform device ************************************************************/
3321 static int asus_wmi_platform_init(struct asus_wmi *asus)
3323 struct device *dev = &asus->platform_device->dev;
3327 /* INIT enable hotkeys on some models */
3328 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
3329 pr_info("Initialization: %#x\n", rv);
3331 /* We don't know yet what to do with this version... */
3332 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
3333 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
3338 * The SFUN method probably allows the original driver to get the list
3339 * of features supported by a given model. For now, 0x0100 or 0x0800
3340 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
3341 * The significance of others is yet to be found.
3343 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
3344 pr_info("SFUN value: %#x\n", rv);
3349 * Eee PC and Notebooks seems to have different method_id for DSTS,
3350 * but it may also be related to the BIOS's SPEC.
3351 * Note, on most Eeepc, there is no way to check if a method exist
3352 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
3353 * but once again, SPEC may probably be used for that kind of things.
3355 * Additionally at least TUF Gaming series laptops return nothing for
3356 * unknown methods, so the detection in this way is not possible.
3358 * There is strong indication that only ACPI WMI devices that have _UID
3359 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
3361 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
3365 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
3366 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
3367 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
3369 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
3370 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
3374 * Some devices can have multiple event codes stored in a queue before
3375 * the module load if it was unloaded intermittently after calling
3376 * the INIT method (enables event handling). The WMI notify handler is
3377 * expected to retrieve all event codes until a retrieved code equals
3378 * queue end marker (One or Ones). Old codes are flushed from the queue
3379 * upon module load. Not enabling this when it should be has minimal
3380 * visible impact so fall back if anything goes wrong.
3382 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
3383 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
3384 dev_info(dev, "Detected ATK, enable event queue\n");
3386 if (!asus_wmi_notify_queue_flush(asus))
3387 asus->wmi_event_queue = true;
3390 /* CWAP allow to define the behavior of the Fn+F2 key,
3391 * this method doesn't seems to be present on Eee PCs */
3392 if (asus->driver->quirks->wapf >= 0)
3393 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
3394 asus->driver->quirks->wapf, NULL);
3399 /* debugfs ********************************************************************/
3401 struct asus_wmi_debugfs_node {
3402 struct asus_wmi *asus;
3404 int (*show) (struct seq_file *m, void *data);
3407 static int show_dsts(struct seq_file *m, void *data)
3409 struct asus_wmi *asus = m->private;
3413 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
3417 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
3422 static int show_devs(struct seq_file *m, void *data)
3424 struct asus_wmi *asus = m->private;
3428 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
3433 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
3434 asus->debug.ctrl_param, retval);
3439 static int show_call(struct seq_file *m, void *data)
3441 struct asus_wmi *asus = m->private;
3442 struct bios_args args = {
3443 .arg0 = asus->debug.dev_id,
3444 .arg1 = asus->debug.ctrl_param,
3446 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
3447 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
3448 union acpi_object *obj;
3451 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
3452 0, asus->debug.method_id,
3455 if (ACPI_FAILURE(status))
3458 obj = (union acpi_object *)output.pointer;
3459 if (obj && obj->type == ACPI_TYPE_INTEGER)
3460 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
3461 asus->debug.dev_id, asus->debug.ctrl_param,
3462 (u32) obj->integer.value);
3464 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
3465 asus->debug.dev_id, asus->debug.ctrl_param,
3466 obj ? obj->type : -1);
3473 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
3474 {NULL, "devs", show_devs},
3475 {NULL, "dsts", show_dsts},
3476 {NULL, "call", show_call},
3479 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
3481 struct asus_wmi_debugfs_node *node = inode->i_private;
3483 return single_open(file, node->show, node->asus);
3486 static const struct file_operations asus_wmi_debugfs_io_ops = {
3487 .owner = THIS_MODULE,
3488 .open = asus_wmi_debugfs_open,
3490 .llseek = seq_lseek,
3491 .release = single_release,
3494 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
3496 debugfs_remove_recursive(asus->debug.root);
3499 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
3503 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
3505 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
3506 &asus->debug.method_id);
3508 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
3509 &asus->debug.dev_id);
3511 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
3512 &asus->debug.ctrl_param);
3514 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
3515 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
3518 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
3519 asus->debug.root, node,
3520 &asus_wmi_debugfs_io_ops);
3524 /* Init / exit ****************************************************************/
3526 static int asus_wmi_add(struct platform_device *pdev)
3528 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3529 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3530 struct asus_wmi *asus;
3531 const char *chassis_type;
3536 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
3540 asus->driver = wdrv;
3541 asus->platform_device = pdev;
3542 wdrv->platform_device = pdev;
3543 platform_set_drvdata(asus->platform_device, asus);
3545 if (wdrv->detect_quirks)
3546 wdrv->detect_quirks(asus->driver);
3548 err = asus_wmi_platform_init(asus);
3552 err = egpu_enable_check_present(asus);
3554 goto fail_egpu_enable;
3556 err = dgpu_disable_check_present(asus);
3558 goto fail_dgpu_disable;
3560 err = fan_boost_mode_check_present(asus);
3562 goto fail_fan_boost_mode;
3564 err = throttle_thermal_policy_check_present(asus);
3566 goto fail_throttle_thermal_policy;
3568 throttle_thermal_policy_set_default(asus);
3570 err = platform_profile_setup(asus);
3572 goto fail_platform_profile_setup;
3574 err = panel_od_check_present(asus);
3578 err = asus_wmi_sysfs_init(asus->platform_device);
3582 err = asus_wmi_input_init(asus);
3586 err = asus_wmi_fan_init(asus); /* probably no problems on error */
3588 err = asus_wmi_hwmon_init(asus);
3592 err = asus_wmi_custom_fan_curve_init(asus);
3594 goto fail_custom_fan_curve;
3596 err = asus_wmi_led_init(asus);
3600 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
3601 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
3602 asus->driver->wlan_ctrl_by_user = 1;
3604 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
3605 err = asus_wmi_rfkill_init(asus);
3610 if (asus->driver->quirks->wmi_force_als_set)
3613 /* Some Asus desktop boards export an acpi-video backlight interface,
3614 stop this from showing up */
3615 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
3616 if (chassis_type && !strcmp(chassis_type, "3"))
3617 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3619 if (asus->driver->quirks->wmi_backlight_power)
3620 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3622 if (asus->driver->quirks->wmi_backlight_native)
3623 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
3625 if (asus->driver->quirks->xusb2pr)
3626 asus_wmi_set_xusb2pr(asus);
3628 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3629 err = asus_wmi_backlight_init(asus);
3630 if (err && err != -ENODEV)
3631 goto fail_backlight;
3632 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
3633 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
3635 if (asus_wmi_has_fnlock_key(asus)) {
3636 asus->fnlock_locked = fnlock_default;
3637 asus_wmi_fnlock_update(asus);
3640 status = wmi_install_notify_handler(asus->driver->event_guid,
3641 asus_wmi_notify, asus);
3642 if (ACPI_FAILURE(status)) {
3643 pr_err("Unable to register notify handler - %d\n", status);
3645 goto fail_wmi_handler;
3648 asus_wmi_battery_init(asus);
3650 asus_wmi_debugfs_init(asus);
3655 asus_wmi_backlight_exit(asus);
3657 asus_wmi_rfkill_exit(asus);
3659 asus_wmi_led_exit(asus);
3662 asus_wmi_input_exit(asus);
3664 asus_wmi_sysfs_exit(asus->platform_device);
3666 fail_throttle_thermal_policy:
3667 fail_custom_fan_curve:
3668 fail_platform_profile_setup:
3669 if (asus->platform_profile_support)
3670 platform_profile_remove();
3671 fail_fan_boost_mode:
3680 static int asus_wmi_remove(struct platform_device *device)
3682 struct asus_wmi *asus;
3684 asus = platform_get_drvdata(device);
3685 wmi_remove_notify_handler(asus->driver->event_guid);
3686 asus_wmi_backlight_exit(asus);
3687 asus_wmi_input_exit(asus);
3688 asus_wmi_led_exit(asus);
3689 asus_wmi_rfkill_exit(asus);
3690 asus_wmi_debugfs_exit(asus);
3691 asus_wmi_sysfs_exit(asus->platform_device);
3692 asus_fan_set_auto(asus);
3693 throttle_thermal_policy_set_default(asus);
3694 asus_wmi_battery_exit(asus);
3696 if (asus->platform_profile_support)
3697 platform_profile_remove();
3703 /* Platform driver - hibernate/resume callbacks *******************************/
3705 static int asus_hotk_thaw(struct device *device)
3707 struct asus_wmi *asus = dev_get_drvdata(device);
3709 if (asus->wlan.rfkill) {
3713 * Work around bios bug - acpi _PTS turns off the wireless led
3714 * during suspend. Normally it restores it on resume, but
3715 * we should kick it ourselves in case hibernation is aborted.
3717 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
3718 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
3724 static int asus_hotk_resume(struct device *device)
3726 struct asus_wmi *asus = dev_get_drvdata(device);
3728 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3729 kbd_led_update(asus);
3731 if (asus_wmi_has_fnlock_key(asus))
3732 asus_wmi_fnlock_update(asus);
3734 if (asus->driver->quirks->use_lid_flip_devid)
3735 lid_flip_tablet_mode_get_state(asus);
3740 static int asus_hotk_restore(struct device *device)
3742 struct asus_wmi *asus = dev_get_drvdata(device);
3745 /* Refresh both wlan rfkill state and pci hotplug */
3746 if (asus->wlan.rfkill)
3747 asus_rfkill_hotplug(asus);
3749 if (asus->bluetooth.rfkill) {
3750 bl = !asus_wmi_get_devstate_simple(asus,
3751 ASUS_WMI_DEVID_BLUETOOTH);
3752 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
3754 if (asus->wimax.rfkill) {
3755 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
3756 rfkill_set_sw_state(asus->wimax.rfkill, bl);
3758 if (asus->wwan3g.rfkill) {
3759 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
3760 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
3762 if (asus->gps.rfkill) {
3763 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
3764 rfkill_set_sw_state(asus->gps.rfkill, bl);
3766 if (asus->uwb.rfkill) {
3767 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
3768 rfkill_set_sw_state(asus->uwb.rfkill, bl);
3770 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3771 kbd_led_update(asus);
3773 if (asus_wmi_has_fnlock_key(asus))
3774 asus_wmi_fnlock_update(asus);
3776 if (asus->driver->quirks->use_lid_flip_devid)
3777 lid_flip_tablet_mode_get_state(asus);
3782 static const struct dev_pm_ops asus_pm_ops = {
3783 .thaw = asus_hotk_thaw,
3784 .restore = asus_hotk_restore,
3785 .resume = asus_hotk_resume,
3788 /* Registration ***************************************************************/
3790 static int asus_wmi_probe(struct platform_device *pdev)
3792 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3793 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3796 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
3797 pr_warn("ASUS Management GUID not found\n");
3801 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
3802 pr_warn("ASUS Event GUID not found\n");
3807 ret = wdrv->probe(pdev);
3812 return asus_wmi_add(pdev);
3817 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
3819 struct platform_driver *platform_driver;
3820 struct platform_device *platform_device;
3825 platform_driver = &driver->platform_driver;
3826 platform_driver->remove = asus_wmi_remove;
3827 platform_driver->driver.owner = driver->owner;
3828 platform_driver->driver.name = driver->name;
3829 platform_driver->driver.pm = &asus_pm_ops;
3831 platform_device = platform_create_bundle(platform_driver,
3834 if (IS_ERR(platform_device))
3835 return PTR_ERR(platform_device);
3840 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
3842 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
3844 platform_device_unregister(driver->platform_device);
3845 platform_driver_unregister(&driver->platform_driver);
3848 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
3850 static int __init asus_wmi_init(void)
3852 pr_info("ASUS WMI generic driver loaded\n");
3856 static void __exit asus_wmi_exit(void)
3858 pr_info("ASUS WMI generic driver unloaded\n");
3861 module_init(asus_wmi_init);
3862 module_exit(asus_wmi_exit);