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 32
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 = "platform::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,
2237 pr_warn("%s (0x%08x) failed: %d\n", __func__, fan_dev, err);
2241 fan_curve_copy_from_buf(curves, buf);
2242 curves->device_id = fan_dev;
2247 /* Check if capability exists, and populate defaults */
2248 static int fan_curve_check_present(struct asus_wmi *asus, bool *available,
2255 err = fan_curve_get_factory_default(asus, fan_dev);
2264 /* Determine which fan the attribute is for if SENSOR_ATTR */
2265 static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus,
2266 struct device_attribute *attr)
2268 int index = to_sensor_dev_attr(attr)->index;
2270 return &asus->custom_fan_curves[index & FAN_CURVE_DEV_GPU];
2273 /* Determine which fan the attribute is for if SENSOR_ATTR_2 */
2274 static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus,
2275 struct device_attribute *attr)
2277 int nr = to_sensor_dev_attr_2(attr)->nr;
2279 return &asus->custom_fan_curves[nr & FAN_CURVE_DEV_GPU];
2282 static ssize_t fan_curve_show(struct device *dev,
2283 struct device_attribute *attr, char *buf)
2285 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2286 struct asus_wmi *asus = dev_get_drvdata(dev);
2287 struct fan_curve_data *data;
2288 int value, index, nr;
2290 data = fan_curve_attr_2_select(asus, attr);
2291 index = dev_attr->index;
2294 if (nr & FAN_CURVE_PWM_MASK)
2295 value = data->percents[index];
2297 value = data->temps[index];
2299 return sysfs_emit(buf, "%d\n", value);
2303 * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE.
2305 static int fan_curve_write(struct asus_wmi *asus,
2306 struct fan_curve_data *data)
2308 u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0;
2309 u8 *percents = data->percents;
2310 u8 *temps = data->temps;
2311 int ret, i, shift = 0;
2316 for (i = 0; i < FAN_CURVE_POINTS / 2; i++) {
2317 arg1 += (temps[i]) << shift;
2318 arg2 += (temps[i + 4]) << shift;
2319 /* Scale to percentage for device */
2320 arg3 += (100 * percents[i] / 255) << shift;
2321 arg4 += (100 * percents[i + 4] / 255) << shift;
2325 return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS,
2327 arg1, arg2, arg3, arg4, &ret);
2330 static ssize_t fan_curve_store(struct device *dev,
2331 struct device_attribute *attr, const char *buf,
2334 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2335 struct asus_wmi *asus = dev_get_drvdata(dev);
2336 struct fan_curve_data *data;
2340 int pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
2341 int index = dev_attr->index;
2343 data = fan_curve_attr_2_select(asus, attr);
2345 err = kstrtou8(buf, 10, &value);
2350 data->percents[index] = value;
2352 data->temps[index] = value;
2356 * Mark as disabled so the user has to explicitly enable to apply a
2357 * changed fan curve. This prevents potential lockups from writing out
2358 * many changes as one-write-per-change.
2360 data->enabled = false;
2365 static ssize_t fan_curve_enable_show(struct device *dev,
2366 struct device_attribute *attr, char *buf)
2368 struct asus_wmi *asus = dev_get_drvdata(dev);
2369 struct fan_curve_data *data;
2372 data = fan_curve_attr_select(asus, attr);
2377 return sysfs_emit(buf, "%d\n", out);
2380 static ssize_t fan_curve_enable_store(struct device *dev,
2381 struct device_attribute *attr,
2382 const char *buf, size_t count)
2384 struct asus_wmi *asus = dev_get_drvdata(dev);
2385 struct fan_curve_data *data;
2388 data = fan_curve_attr_select(asus, attr);
2390 err = kstrtoint(buf, 10, &value);
2396 data->enabled = true;
2399 data->enabled = false;
2402 * Auto + reset the fan curve data to defaults. Make it an explicit
2403 * option so that users don't accidentally overwrite a set fan curve.
2406 err = fan_curve_get_factory_default(asus, data->device_id);
2409 data->enabled = false;
2415 if (data->enabled) {
2416 err = fan_curve_write(asus, data);
2421 * For machines with throttle this is the only way to reset fans
2422 * to default mode of operation (does not erase curve data).
2424 if (asus->throttle_thermal_policy_available) {
2425 err = throttle_thermal_policy_write(asus);
2428 /* Similar is true for laptops with this fan */
2429 } else if (asus->fan_type == FAN_TYPE_SPEC83) {
2430 err = asus_fan_set_auto(asus);
2434 /* Safeguard against fautly ACPI tables */
2435 err = fan_curve_get_factory_default(asus, data->device_id);
2438 err = fan_curve_write(asus, data);
2447 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU);
2448 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve,
2449 FAN_CURVE_DEV_CPU, 0);
2450 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve,
2451 FAN_CURVE_DEV_CPU, 1);
2452 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve,
2453 FAN_CURVE_DEV_CPU, 2);
2454 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve,
2455 FAN_CURVE_DEV_CPU, 3);
2456 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve,
2457 FAN_CURVE_DEV_CPU, 4);
2458 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve,
2459 FAN_CURVE_DEV_CPU, 5);
2460 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve,
2461 FAN_CURVE_DEV_CPU, 6);
2462 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve,
2463 FAN_CURVE_DEV_CPU, 7);
2465 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve,
2466 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0);
2467 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve,
2468 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1);
2469 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve,
2470 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2);
2471 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve,
2472 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3);
2473 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve,
2474 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4);
2475 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve,
2476 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5);
2477 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve,
2478 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6);
2479 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve,
2480 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7);
2483 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU);
2484 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve,
2485 FAN_CURVE_DEV_GPU, 0);
2486 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve,
2487 FAN_CURVE_DEV_GPU, 1);
2488 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve,
2489 FAN_CURVE_DEV_GPU, 2);
2490 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve,
2491 FAN_CURVE_DEV_GPU, 3);
2492 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve,
2493 FAN_CURVE_DEV_GPU, 4);
2494 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve,
2495 FAN_CURVE_DEV_GPU, 5);
2496 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve,
2497 FAN_CURVE_DEV_GPU, 6);
2498 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve,
2499 FAN_CURVE_DEV_GPU, 7);
2501 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve,
2502 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0);
2503 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve,
2504 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1);
2505 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve,
2506 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2);
2507 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve,
2508 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3);
2509 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve,
2510 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4);
2511 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve,
2512 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5);
2513 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve,
2514 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6);
2515 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve,
2516 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7);
2518 static struct attribute *asus_fan_curve_attr[] = {
2520 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2521 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2522 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2523 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2524 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2525 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
2526 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
2527 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
2528 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
2529 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2530 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2531 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2532 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2533 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
2534 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
2535 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
2536 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
2538 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2539 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2540 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2541 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2542 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2543 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
2544 &sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr,
2545 &sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr,
2546 &sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr,
2547 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
2548 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2549 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2550 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2551 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
2552 &sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr,
2553 &sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr,
2554 &sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr,
2558 static umode_t asus_fan_curve_is_visible(struct kobject *kobj,
2559 struct attribute *attr, int idx)
2561 struct device *dev = kobj_to_dev(kobj);
2562 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2565 * Check the char instead of casting attr as there are two attr types
2566 * involved here (attr1 and attr2)
2568 if (asus->cpu_fan_curve_available && attr->name[3] == '1')
2571 if (asus->gpu_fan_curve_available && attr->name[3] == '2')
2577 static const struct attribute_group asus_fan_curve_attr_group = {
2578 .is_visible = asus_fan_curve_is_visible,
2579 .attrs = asus_fan_curve_attr,
2581 __ATTRIBUTE_GROUPS(asus_fan_curve_attr);
2584 * Must be initialised after throttle_thermal_policy_check_present() as
2585 * we check the status of throttle_thermal_policy_available during init.
2587 static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus)
2589 struct device *dev = &asus->platform_device->dev;
2590 struct device *hwmon;
2593 err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available,
2594 ASUS_WMI_DEVID_CPU_FAN_CURVE);
2598 err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available,
2599 ASUS_WMI_DEVID_GPU_FAN_CURVE);
2603 if (!asus->cpu_fan_curve_available && !asus->gpu_fan_curve_available)
2606 hwmon = devm_hwmon_device_register_with_groups(
2607 dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups);
2609 if (IS_ERR(hwmon)) {
2611 "Could not register asus_custom_fan_curve device\n");
2612 return PTR_ERR(hwmon);
2618 /* Throttle thermal policy ****************************************************/
2620 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
2625 asus->throttle_thermal_policy_available = false;
2627 err = asus_wmi_get_devstate(asus,
2628 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2636 if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
2637 asus->throttle_thermal_policy_available = true;
2642 static int throttle_thermal_policy_write(struct asus_wmi *asus)
2648 value = asus->throttle_thermal_policy_mode;
2650 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2653 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2654 "throttle_thermal_policy");
2657 pr_warn("Failed to set throttle thermal policy: %d\n", err);
2662 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
2667 /* Must set to disabled if mode is toggled */
2668 if (asus->cpu_fan_curve_available)
2669 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
2670 if (asus->gpu_fan_curve_available)
2671 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
2676 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
2678 if (!asus->throttle_thermal_policy_available)
2681 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2682 return throttle_thermal_policy_write(asus);
2685 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
2687 u8 new_mode = asus->throttle_thermal_policy_mode + 1;
2690 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2691 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2693 asus->throttle_thermal_policy_mode = new_mode;
2694 err = throttle_thermal_policy_write(asus);
2699 * Ensure that platform_profile updates userspace with the change to ensure
2700 * that platform_profile and throttle_thermal_policy_mode are in sync.
2702 platform_profile_notify();
2707 static ssize_t throttle_thermal_policy_show(struct device *dev,
2708 struct device_attribute *attr, char *buf)
2710 struct asus_wmi *asus = dev_get_drvdata(dev);
2711 u8 mode = asus->throttle_thermal_policy_mode;
2713 return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
2716 static ssize_t throttle_thermal_policy_store(struct device *dev,
2717 struct device_attribute *attr,
2718 const char *buf, size_t count)
2720 struct asus_wmi *asus = dev_get_drvdata(dev);
2725 result = kstrtou8(buf, 10, &new_mode);
2729 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2732 asus->throttle_thermal_policy_mode = new_mode;
2733 err = throttle_thermal_policy_write(asus);
2738 * Ensure that platform_profile updates userspace with the change to ensure
2739 * that platform_profile and throttle_thermal_policy_mode are in sync.
2741 platform_profile_notify();
2746 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
2747 static DEVICE_ATTR_RW(throttle_thermal_policy);
2749 /* Platform profile ***********************************************************/
2750 static int asus_wmi_platform_profile_get(struct platform_profile_handler *pprof,
2751 enum platform_profile_option *profile)
2753 struct asus_wmi *asus;
2756 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2758 tp = asus->throttle_thermal_policy_mode;
2761 case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
2762 *profile = PLATFORM_PROFILE_BALANCED;
2764 case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
2765 *profile = PLATFORM_PROFILE_PERFORMANCE;
2767 case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
2768 *profile = PLATFORM_PROFILE_QUIET;
2777 static int asus_wmi_platform_profile_set(struct platform_profile_handler *pprof,
2778 enum platform_profile_option profile)
2780 struct asus_wmi *asus;
2783 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2786 case PLATFORM_PROFILE_PERFORMANCE:
2787 tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
2789 case PLATFORM_PROFILE_BALANCED:
2790 tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2792 case PLATFORM_PROFILE_QUIET:
2793 tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
2799 asus->throttle_thermal_policy_mode = tp;
2800 return throttle_thermal_policy_write(asus);
2803 static int platform_profile_setup(struct asus_wmi *asus)
2805 struct device *dev = &asus->platform_device->dev;
2809 * Not an error if a component platform_profile relies on is unavailable
2810 * so early return, skipping the setup of platform_profile.
2812 if (!asus->throttle_thermal_policy_available)
2815 dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
2817 asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
2818 asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
2820 set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
2821 set_bit(PLATFORM_PROFILE_BALANCED,
2822 asus->platform_profile_handler.choices);
2823 set_bit(PLATFORM_PROFILE_PERFORMANCE,
2824 asus->platform_profile_handler.choices);
2826 err = platform_profile_register(&asus->platform_profile_handler);
2830 asus->platform_profile_support = true;
2834 /* Backlight ******************************************************************/
2836 static int read_backlight_power(struct asus_wmi *asus)
2840 if (asus->driver->quirks->store_backlight_power)
2841 ret = !asus->driver->panel_power;
2843 ret = asus_wmi_get_devstate_simple(asus,
2844 ASUS_WMI_DEVID_BACKLIGHT);
2849 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
2852 static int read_brightness_max(struct asus_wmi *asus)
2857 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2861 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
2870 static int read_brightness(struct backlight_device *bd)
2872 struct asus_wmi *asus = bl_get_data(bd);
2876 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2880 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
2883 static u32 get_scalar_command(struct backlight_device *bd)
2885 struct asus_wmi *asus = bl_get_data(bd);
2888 if ((asus->driver->brightness < bd->props.brightness) ||
2889 bd->props.brightness == bd->props.max_brightness)
2890 ctrl_param = 0x00008001;
2891 else if ((asus->driver->brightness > bd->props.brightness) ||
2892 bd->props.brightness == 0)
2893 ctrl_param = 0x00008000;
2895 asus->driver->brightness = bd->props.brightness;
2900 static int update_bl_status(struct backlight_device *bd)
2902 struct asus_wmi *asus = bl_get_data(bd);
2906 power = read_backlight_power(asus);
2907 if (power != -ENODEV && bd->props.power != power) {
2908 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
2909 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
2911 if (asus->driver->quirks->store_backlight_power)
2912 asus->driver->panel_power = bd->props.power;
2914 /* When using scalar brightness, updating the brightness
2915 * will mess with the backlight power */
2916 if (asus->driver->quirks->scalar_panel_brightness)
2920 if (asus->driver->quirks->scalar_panel_brightness)
2921 ctrl_param = get_scalar_command(bd);
2923 ctrl_param = bd->props.brightness;
2925 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
2931 static const struct backlight_ops asus_wmi_bl_ops = {
2932 .get_brightness = read_brightness,
2933 .update_status = update_bl_status,
2936 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
2938 struct backlight_device *bd = asus->backlight_device;
2939 int old = bd->props.brightness;
2942 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2943 new = code - NOTIFY_BRNUP_MIN + 1;
2944 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2945 new = code - NOTIFY_BRNDOWN_MIN;
2947 bd->props.brightness = new;
2948 backlight_update_status(bd);
2949 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
2954 static int asus_wmi_backlight_init(struct asus_wmi *asus)
2956 struct backlight_device *bd;
2957 struct backlight_properties props;
2961 max = read_brightness_max(asus);
2965 power = read_backlight_power(asus);
2966 if (power == -ENODEV)
2967 power = FB_BLANK_UNBLANK;
2971 memset(&props, 0, sizeof(struct backlight_properties));
2972 props.type = BACKLIGHT_PLATFORM;
2973 props.max_brightness = max;
2974 bd = backlight_device_register(asus->driver->name,
2975 &asus->platform_device->dev, asus,
2976 &asus_wmi_bl_ops, &props);
2978 pr_err("Could not register backlight device\n");
2982 asus->backlight_device = bd;
2984 if (asus->driver->quirks->store_backlight_power)
2985 asus->driver->panel_power = power;
2987 bd->props.brightness = read_brightness(bd);
2988 bd->props.power = power;
2989 backlight_update_status(bd);
2991 asus->driver->brightness = bd->props.brightness;
2996 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
2998 backlight_device_unregister(asus->backlight_device);
3000 asus->backlight_device = NULL;
3003 static int is_display_toggle(int code)
3005 /* display toggle keys */
3006 if ((code >= 0x61 && code <= 0x67) ||
3007 (code >= 0x8c && code <= 0x93) ||
3008 (code >= 0xa0 && code <= 0xa7) ||
3009 (code >= 0xd0 && code <= 0xd5))
3015 /* Fn-lock ********************************************************************/
3017 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
3021 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
3023 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
3024 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
3027 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
3029 int mode = asus->fnlock_locked;
3031 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
3034 /* WMI events *****************************************************************/
3036 static int asus_wmi_get_event_code(u32 value)
3038 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
3039 union acpi_object *obj;
3043 status = wmi_get_event_data(value, &response);
3044 if (ACPI_FAILURE(status)) {
3045 pr_warn("Failed to get WMI notify code: %s\n",
3046 acpi_format_exception(status));
3050 obj = (union acpi_object *)response.pointer;
3052 if (obj && obj->type == ACPI_TYPE_INTEGER)
3053 code = (int)(obj->integer.value & WMI_EVENT_MASK);
3061 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
3063 unsigned int key_value = 1;
3064 bool autorelease = 1;
3065 int result, orig_code;
3069 if (asus->driver->key_filter) {
3070 asus->driver->key_filter(asus->driver, &code, &key_value,
3072 if (code == ASUS_WMI_KEY_IGNORE)
3076 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3077 code = ASUS_WMI_BRN_UP;
3078 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3079 code = ASUS_WMI_BRN_DOWN;
3081 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
3082 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3083 asus_wmi_backlight_notify(asus, orig_code);
3088 if (code == NOTIFY_KBD_BRTUP) {
3089 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3092 if (code == NOTIFY_KBD_BRTDWN) {
3093 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
3096 if (code == NOTIFY_KBD_BRTTOGGLE) {
3097 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
3098 kbd_led_set_by_kbd(asus, 0);
3100 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3104 if (code == NOTIFY_FNLOCK_TOGGLE) {
3105 asus->fnlock_locked = !asus->fnlock_locked;
3106 asus_wmi_fnlock_update(asus);
3110 if (asus->driver->quirks->use_kbd_dock_devid && code == NOTIFY_KBD_DOCK_CHANGE) {
3111 result = asus_wmi_get_devstate_simple(asus,
3112 ASUS_WMI_DEVID_KBD_DOCK);
3114 input_report_switch(asus->inputdev, SW_TABLET_MODE,
3116 input_sync(asus->inputdev);
3121 if (asus->driver->quirks->use_lid_flip_devid && code == NOTIFY_LID_FLIP) {
3122 lid_flip_tablet_mode_get_state(asus);
3126 if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
3127 fan_boost_mode_switch_next(asus);
3131 if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
3132 throttle_thermal_policy_switch_next(asus);
3136 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
3139 if (!sparse_keymap_report_event(asus->inputdev, code,
3140 key_value, autorelease))
3141 pr_info("Unknown key code 0x%x\n", code);
3144 static void asus_wmi_notify(u32 value, void *context)
3146 struct asus_wmi *asus = context;
3150 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3151 code = asus_wmi_get_event_code(value);
3153 pr_warn("Failed to get notify code: %d\n", code);
3157 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3160 asus_wmi_handle_event_code(code, asus);
3163 * Double check that queue is present:
3164 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
3166 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
3170 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
3173 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
3178 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3179 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
3181 pr_warn("Failed to get event during flush: %d\n", code);
3185 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3189 pr_warn("Failed to flush event queue\n");
3193 /* Sysfs **********************************************************************/
3195 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
3196 const char *buf, size_t count)
3201 value = asus_wmi_get_devstate_simple(asus, devid);
3205 err = kstrtoint(buf, 0, &value);
3209 err = asus_wmi_set_devstate(devid, value, &retval);
3216 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
3218 int value = asus_wmi_get_devstate_simple(asus, devid);
3223 return sprintf(buf, "%d\n", value);
3226 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
3227 static ssize_t show_##_name(struct device *dev, \
3228 struct device_attribute *attr, \
3231 struct asus_wmi *asus = dev_get_drvdata(dev); \
3233 return show_sys_wmi(asus, _cm, buf); \
3235 static ssize_t store_##_name(struct device *dev, \
3236 struct device_attribute *attr, \
3237 const char *buf, size_t count) \
3239 struct asus_wmi *asus = dev_get_drvdata(dev); \
3241 return store_sys_wmi(asus, _cm, buf, count); \
3243 static struct device_attribute dev_attr_##_name = { \
3245 .name = __stringify(_name), \
3247 .show = show_##_name, \
3248 .store = store_##_name, \
3251 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
3252 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
3253 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
3254 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
3255 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
3257 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
3258 const char *buf, size_t count)
3262 rv = kstrtoint(buf, 0, &value);
3266 if (value < 0 || value > 2)
3269 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
3276 static DEVICE_ATTR_WO(cpufv);
3278 static struct attribute *platform_attributes[] = {
3279 &dev_attr_cpufv.attr,
3280 &dev_attr_camera.attr,
3281 &dev_attr_cardr.attr,
3282 &dev_attr_touchpad.attr,
3283 &dev_attr_egpu_enable.attr,
3284 &dev_attr_dgpu_disable.attr,
3285 &dev_attr_lid_resume.attr,
3286 &dev_attr_als_enable.attr,
3287 &dev_attr_fan_boost_mode.attr,
3288 &dev_attr_throttle_thermal_policy.attr,
3289 &dev_attr_panel_od.attr,
3293 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
3294 struct attribute *attr, int idx)
3296 struct device *dev = container_of(kobj, struct device, kobj);
3297 struct asus_wmi *asus = dev_get_drvdata(dev);
3301 if (attr == &dev_attr_camera.attr)
3302 devid = ASUS_WMI_DEVID_CAMERA;
3303 else if (attr == &dev_attr_cardr.attr)
3304 devid = ASUS_WMI_DEVID_CARDREADER;
3305 else if (attr == &dev_attr_touchpad.attr)
3306 devid = ASUS_WMI_DEVID_TOUCHPAD;
3307 else if (attr == &dev_attr_lid_resume.attr)
3308 devid = ASUS_WMI_DEVID_LID_RESUME;
3309 else if (attr == &dev_attr_als_enable.attr)
3310 devid = ASUS_WMI_DEVID_ALS_ENABLE;
3311 else if (attr == &dev_attr_egpu_enable.attr)
3312 ok = asus->egpu_enable_available;
3313 else if (attr == &dev_attr_dgpu_disable.attr)
3314 ok = asus->dgpu_disable_available;
3315 else if (attr == &dev_attr_fan_boost_mode.attr)
3316 ok = asus->fan_boost_mode_available;
3317 else if (attr == &dev_attr_throttle_thermal_policy.attr)
3318 ok = asus->throttle_thermal_policy_available;
3319 else if (attr == &dev_attr_panel_od.attr)
3320 ok = asus->panel_overdrive_available;
3323 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
3325 return ok ? attr->mode : 0;
3328 static const struct attribute_group platform_attribute_group = {
3329 .is_visible = asus_sysfs_is_visible,
3330 .attrs = platform_attributes
3333 static void asus_wmi_sysfs_exit(struct platform_device *device)
3335 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
3338 static int asus_wmi_sysfs_init(struct platform_device *device)
3340 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
3343 /* Platform device ************************************************************/
3345 static int asus_wmi_platform_init(struct asus_wmi *asus)
3347 struct device *dev = &asus->platform_device->dev;
3351 /* INIT enable hotkeys on some models */
3352 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
3353 pr_info("Initialization: %#x\n", rv);
3355 /* We don't know yet what to do with this version... */
3356 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
3357 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
3362 * The SFUN method probably allows the original driver to get the list
3363 * of features supported by a given model. For now, 0x0100 or 0x0800
3364 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
3365 * The significance of others is yet to be found.
3367 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
3368 pr_info("SFUN value: %#x\n", rv);
3373 * Eee PC and Notebooks seems to have different method_id for DSTS,
3374 * but it may also be related to the BIOS's SPEC.
3375 * Note, on most Eeepc, there is no way to check if a method exist
3376 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
3377 * but once again, SPEC may probably be used for that kind of things.
3379 * Additionally at least TUF Gaming series laptops return nothing for
3380 * unknown methods, so the detection in this way is not possible.
3382 * There is strong indication that only ACPI WMI devices that have _UID
3383 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
3385 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
3389 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
3390 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
3391 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
3393 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
3394 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
3398 * Some devices can have multiple event codes stored in a queue before
3399 * the module load if it was unloaded intermittently after calling
3400 * the INIT method (enables event handling). The WMI notify handler is
3401 * expected to retrieve all event codes until a retrieved code equals
3402 * queue end marker (One or Ones). Old codes are flushed from the queue
3403 * upon module load. Not enabling this when it should be has minimal
3404 * visible impact so fall back if anything goes wrong.
3406 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
3407 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
3408 dev_info(dev, "Detected ATK, enable event queue\n");
3410 if (!asus_wmi_notify_queue_flush(asus))
3411 asus->wmi_event_queue = true;
3414 /* CWAP allow to define the behavior of the Fn+F2 key,
3415 * this method doesn't seems to be present on Eee PCs */
3416 if (asus->driver->quirks->wapf >= 0)
3417 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
3418 asus->driver->quirks->wapf, NULL);
3423 /* debugfs ********************************************************************/
3425 struct asus_wmi_debugfs_node {
3426 struct asus_wmi *asus;
3428 int (*show) (struct seq_file *m, void *data);
3431 static int show_dsts(struct seq_file *m, void *data)
3433 struct asus_wmi *asus = m->private;
3437 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
3441 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
3446 static int show_devs(struct seq_file *m, void *data)
3448 struct asus_wmi *asus = m->private;
3452 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
3457 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
3458 asus->debug.ctrl_param, retval);
3463 static int show_call(struct seq_file *m, void *data)
3465 struct asus_wmi *asus = m->private;
3466 struct bios_args args = {
3467 .arg0 = asus->debug.dev_id,
3468 .arg1 = asus->debug.ctrl_param,
3470 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
3471 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
3472 union acpi_object *obj;
3475 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
3476 0, asus->debug.method_id,
3479 if (ACPI_FAILURE(status))
3482 obj = (union acpi_object *)output.pointer;
3483 if (obj && obj->type == ACPI_TYPE_INTEGER)
3484 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
3485 asus->debug.dev_id, asus->debug.ctrl_param,
3486 (u32) obj->integer.value);
3488 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
3489 asus->debug.dev_id, asus->debug.ctrl_param,
3490 obj ? obj->type : -1);
3497 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
3498 {NULL, "devs", show_devs},
3499 {NULL, "dsts", show_dsts},
3500 {NULL, "call", show_call},
3503 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
3505 struct asus_wmi_debugfs_node *node = inode->i_private;
3507 return single_open(file, node->show, node->asus);
3510 static const struct file_operations asus_wmi_debugfs_io_ops = {
3511 .owner = THIS_MODULE,
3512 .open = asus_wmi_debugfs_open,
3514 .llseek = seq_lseek,
3515 .release = single_release,
3518 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
3520 debugfs_remove_recursive(asus->debug.root);
3523 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
3527 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
3529 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
3530 &asus->debug.method_id);
3532 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
3533 &asus->debug.dev_id);
3535 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
3536 &asus->debug.ctrl_param);
3538 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
3539 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
3542 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
3543 asus->debug.root, node,
3544 &asus_wmi_debugfs_io_ops);
3548 /* Init / exit ****************************************************************/
3550 static int asus_wmi_add(struct platform_device *pdev)
3552 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3553 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3554 struct asus_wmi *asus;
3555 const char *chassis_type;
3560 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
3564 asus->driver = wdrv;
3565 asus->platform_device = pdev;
3566 wdrv->platform_device = pdev;
3567 platform_set_drvdata(asus->platform_device, asus);
3569 if (wdrv->detect_quirks)
3570 wdrv->detect_quirks(asus->driver);
3572 err = asus_wmi_platform_init(asus);
3576 err = egpu_enable_check_present(asus);
3578 goto fail_egpu_enable;
3580 err = dgpu_disable_check_present(asus);
3582 goto fail_dgpu_disable;
3584 err = fan_boost_mode_check_present(asus);
3586 goto fail_fan_boost_mode;
3588 err = throttle_thermal_policy_check_present(asus);
3590 goto fail_throttle_thermal_policy;
3592 throttle_thermal_policy_set_default(asus);
3594 err = platform_profile_setup(asus);
3596 goto fail_platform_profile_setup;
3598 err = panel_od_check_present(asus);
3602 err = asus_wmi_sysfs_init(asus->platform_device);
3606 err = asus_wmi_input_init(asus);
3610 err = asus_wmi_fan_init(asus); /* probably no problems on error */
3612 err = asus_wmi_hwmon_init(asus);
3616 err = asus_wmi_custom_fan_curve_init(asus);
3618 goto fail_custom_fan_curve;
3620 err = asus_wmi_led_init(asus);
3624 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
3625 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
3626 asus->driver->wlan_ctrl_by_user = 1;
3628 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
3629 err = asus_wmi_rfkill_init(asus);
3634 if (asus->driver->quirks->wmi_force_als_set)
3637 /* Some Asus desktop boards export an acpi-video backlight interface,
3638 stop this from showing up */
3639 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
3640 if (chassis_type && !strcmp(chassis_type, "3"))
3641 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3643 if (asus->driver->quirks->wmi_backlight_power)
3644 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3646 if (asus->driver->quirks->wmi_backlight_native)
3647 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
3649 if (asus->driver->quirks->xusb2pr)
3650 asus_wmi_set_xusb2pr(asus);
3652 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3653 err = asus_wmi_backlight_init(asus);
3654 if (err && err != -ENODEV)
3655 goto fail_backlight;
3656 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
3657 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
3659 if (asus_wmi_has_fnlock_key(asus)) {
3660 asus->fnlock_locked = fnlock_default;
3661 asus_wmi_fnlock_update(asus);
3664 status = wmi_install_notify_handler(asus->driver->event_guid,
3665 asus_wmi_notify, asus);
3666 if (ACPI_FAILURE(status)) {
3667 pr_err("Unable to register notify handler - %d\n", status);
3669 goto fail_wmi_handler;
3672 asus_wmi_battery_init(asus);
3674 asus_wmi_debugfs_init(asus);
3679 asus_wmi_backlight_exit(asus);
3681 asus_wmi_rfkill_exit(asus);
3683 asus_wmi_led_exit(asus);
3686 asus_wmi_input_exit(asus);
3688 asus_wmi_sysfs_exit(asus->platform_device);
3690 fail_throttle_thermal_policy:
3691 fail_custom_fan_curve:
3692 fail_platform_profile_setup:
3693 if (asus->platform_profile_support)
3694 platform_profile_remove();
3695 fail_fan_boost_mode:
3704 static int asus_wmi_remove(struct platform_device *device)
3706 struct asus_wmi *asus;
3708 asus = platform_get_drvdata(device);
3709 wmi_remove_notify_handler(asus->driver->event_guid);
3710 asus_wmi_backlight_exit(asus);
3711 asus_wmi_input_exit(asus);
3712 asus_wmi_led_exit(asus);
3713 asus_wmi_rfkill_exit(asus);
3714 asus_wmi_debugfs_exit(asus);
3715 asus_wmi_sysfs_exit(asus->platform_device);
3716 asus_fan_set_auto(asus);
3717 throttle_thermal_policy_set_default(asus);
3718 asus_wmi_battery_exit(asus);
3720 if (asus->platform_profile_support)
3721 platform_profile_remove();
3727 /* Platform driver - hibernate/resume callbacks *******************************/
3729 static int asus_hotk_thaw(struct device *device)
3731 struct asus_wmi *asus = dev_get_drvdata(device);
3733 if (asus->wlan.rfkill) {
3737 * Work around bios bug - acpi _PTS turns off the wireless led
3738 * during suspend. Normally it restores it on resume, but
3739 * we should kick it ourselves in case hibernation is aborted.
3741 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
3742 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
3748 static int asus_hotk_resume(struct device *device)
3750 struct asus_wmi *asus = dev_get_drvdata(device);
3752 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3753 kbd_led_update(asus);
3755 if (asus_wmi_has_fnlock_key(asus))
3756 asus_wmi_fnlock_update(asus);
3758 if (asus->driver->quirks->use_lid_flip_devid)
3759 lid_flip_tablet_mode_get_state(asus);
3764 static int asus_hotk_restore(struct device *device)
3766 struct asus_wmi *asus = dev_get_drvdata(device);
3769 /* Refresh both wlan rfkill state and pci hotplug */
3770 if (asus->wlan.rfkill)
3771 asus_rfkill_hotplug(asus);
3773 if (asus->bluetooth.rfkill) {
3774 bl = !asus_wmi_get_devstate_simple(asus,
3775 ASUS_WMI_DEVID_BLUETOOTH);
3776 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
3778 if (asus->wimax.rfkill) {
3779 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
3780 rfkill_set_sw_state(asus->wimax.rfkill, bl);
3782 if (asus->wwan3g.rfkill) {
3783 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
3784 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
3786 if (asus->gps.rfkill) {
3787 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
3788 rfkill_set_sw_state(asus->gps.rfkill, bl);
3790 if (asus->uwb.rfkill) {
3791 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
3792 rfkill_set_sw_state(asus->uwb.rfkill, bl);
3794 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3795 kbd_led_update(asus);
3797 if (asus_wmi_has_fnlock_key(asus))
3798 asus_wmi_fnlock_update(asus);
3800 if (asus->driver->quirks->use_lid_flip_devid)
3801 lid_flip_tablet_mode_get_state(asus);
3806 static const struct dev_pm_ops asus_pm_ops = {
3807 .thaw = asus_hotk_thaw,
3808 .restore = asus_hotk_restore,
3809 .resume = asus_hotk_resume,
3812 /* Registration ***************************************************************/
3814 static int asus_wmi_probe(struct platform_device *pdev)
3816 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3817 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3820 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
3821 pr_warn("ASUS Management GUID not found\n");
3825 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
3826 pr_warn("ASUS Event GUID not found\n");
3831 ret = wdrv->probe(pdev);
3836 return asus_wmi_add(pdev);
3841 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
3843 struct platform_driver *platform_driver;
3844 struct platform_device *platform_device;
3849 platform_driver = &driver->platform_driver;
3850 platform_driver->remove = asus_wmi_remove;
3851 platform_driver->driver.owner = driver->owner;
3852 platform_driver->driver.name = driver->name;
3853 platform_driver->driver.pm = &asus_pm_ops;
3855 platform_device = platform_create_bundle(platform_driver,
3858 if (IS_ERR(platform_device))
3859 return PTR_ERR(platform_device);
3864 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
3866 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
3868 platform_device_unregister(driver->platform_device);
3869 platform_driver_unregister(&driver->platform_driver);
3872 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
3874 static int __init asus_wmi_init(void)
3876 pr_info("ASUS WMI generic driver loaded\n");
3880 static void __exit asus_wmi_exit(void)
3882 pr_info("ASUS WMI generic driver unloaded\n");
3885 module_init(asus_wmi_init);
3886 module_exit(asus_wmi_exit);