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)
376 if (obj->buffer.length == 0)
379 memcpy(ret_buffer, obj->buffer.pointer, obj->buffer.length);
381 case ACPI_TYPE_INTEGER:
382 err = (u32)obj->integer.value;
384 if (err == ASUS_WMI_UNSUPPORTED_METHOD)
387 * At least one method returns a 0 with no buffer if no arg
388 * is provided, such as ASUS_WMI_DEVID_CPU_FAN_CURVE
406 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
408 struct acpi_buffer input;
414 * Copy to dma capable address otherwise memory corruption occurs as
415 * bios has to be able to access it.
417 input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
418 input.length = args.length;
421 phys_addr = virt_to_phys(input.pointer);
423 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
424 phys_addr, 0, &retval);
426 memcpy(args.pointer, input.pointer, args.length);
428 kfree(input.pointer);
435 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
437 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
440 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
443 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
447 /* Helper for special devices with magic return codes */
448 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
449 u32 dev_id, u32 mask)
454 err = asus_wmi_get_devstate(asus, dev_id, &retval);
458 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
461 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
462 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
466 return retval & mask;
469 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
471 return asus_wmi_get_devstate_bits(asus, dev_id,
472 ASUS_WMI_DSTS_STATUS_BIT);
475 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
478 int status = asus_wmi_get_devstate(asus, dev_id, &retval);
480 return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
483 /* Input **********************************************************************/
485 static int asus_wmi_input_init(struct asus_wmi *asus)
489 asus->inputdev = input_allocate_device();
493 asus->inputdev->name = asus->driver->input_name;
494 asus->inputdev->phys = asus->driver->input_phys;
495 asus->inputdev->id.bustype = BUS_HOST;
496 asus->inputdev->dev.parent = &asus->platform_device->dev;
497 set_bit(EV_REP, asus->inputdev->evbit);
499 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
503 if (asus->driver->quirks->use_kbd_dock_devid) {
504 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_KBD_DOCK);
506 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
507 input_report_switch(asus->inputdev, SW_TABLET_MODE, !result);
508 } else if (result != -ENODEV) {
509 pr_err("Error checking for keyboard-dock: %d\n", result);
513 if (asus->driver->quirks->use_lid_flip_devid) {
514 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_LID_FLIP);
516 asus->driver->quirks->use_lid_flip_devid = 0;
518 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
519 input_report_switch(asus->inputdev, SW_TABLET_MODE, result);
520 } else if (result == -ENODEV) {
521 pr_err("This device has lid_flip quirk but got ENODEV checking it. This is a bug.");
523 pr_err("Error checking for lid-flip: %d\n", result);
527 err = input_register_device(asus->inputdev);
534 input_free_device(asus->inputdev);
538 static void asus_wmi_input_exit(struct asus_wmi *asus)
541 input_unregister_device(asus->inputdev);
543 asus->inputdev = NULL;
546 /* Tablet mode ****************************************************************/
548 static void lid_flip_tablet_mode_get_state(struct asus_wmi *asus)
550 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_LID_FLIP);
553 input_report_switch(asus->inputdev, SW_TABLET_MODE, result);
554 input_sync(asus->inputdev);
558 /* dGPU ********************************************************************/
559 static int dgpu_disable_check_present(struct asus_wmi *asus)
564 asus->dgpu_disable_available = false;
566 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_DGPU, &result);
573 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
574 asus->dgpu_disable_available = true;
575 asus->dgpu_disable = result & ASUS_WMI_DSTS_STATUS_BIT;
581 static int dgpu_disable_write(struct asus_wmi *asus)
587 /* Don't rely on type conversion */
588 value = asus->dgpu_disable ? 1 : 0;
590 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, value, &retval);
592 pr_warn("Failed to set dgpu disable: %d\n", err);
597 pr_warn("Failed to set dgpu disable (retval): 0x%x\n", retval);
601 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
606 static ssize_t dgpu_disable_show(struct device *dev,
607 struct device_attribute *attr, char *buf)
609 struct asus_wmi *asus = dev_get_drvdata(dev);
610 u8 mode = asus->dgpu_disable;
612 return sysfs_emit(buf, "%d\n", mode);
616 * A user may be required to store the value twice, typcial store first, then
617 * rescan PCI bus to activate power, then store a second time to save correctly.
618 * The reason for this is that an extra code path in the ACPI is enabled when
619 * the device and bus are powered.
621 static ssize_t dgpu_disable_store(struct device *dev,
622 struct device_attribute *attr,
623 const char *buf, size_t count)
628 struct asus_wmi *asus = dev_get_drvdata(dev);
630 result = kstrtobool(buf, &disable);
634 asus->dgpu_disable = disable;
636 result = dgpu_disable_write(asus);
643 static DEVICE_ATTR_RW(dgpu_disable);
645 /* eGPU ********************************************************************/
646 static int egpu_enable_check_present(struct asus_wmi *asus)
651 asus->egpu_enable_available = false;
653 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_EGPU, &result);
660 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
661 asus->egpu_enable_available = true;
662 asus->egpu_enable = result & ASUS_WMI_DSTS_STATUS_BIT;
668 static int egpu_enable_write(struct asus_wmi *asus)
674 /* Don't rely on type conversion */
675 value = asus->egpu_enable ? 1 : 0;
677 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, value, &retval);
680 pr_warn("Failed to set egpu disable: %d\n", err);
685 pr_warn("Failed to set egpu disable (retval): 0x%x\n", retval);
689 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable");
694 static ssize_t egpu_enable_show(struct device *dev,
695 struct device_attribute *attr, char *buf)
697 struct asus_wmi *asus = dev_get_drvdata(dev);
698 bool mode = asus->egpu_enable;
700 return sysfs_emit(buf, "%d\n", mode);
703 /* The ACPI call to enable the eGPU also disables the internal dGPU */
704 static ssize_t egpu_enable_store(struct device *dev,
705 struct device_attribute *attr,
706 const char *buf, size_t count)
711 struct asus_wmi *asus = dev_get_drvdata(dev);
713 result = kstrtobool(buf, &enable);
717 asus->egpu_enable = enable;
719 result = egpu_enable_write(asus);
723 /* Ensure that the kernel status of dgpu is updated */
724 result = dgpu_disable_check_present(asus);
731 static DEVICE_ATTR_RW(egpu_enable);
733 /* Battery ********************************************************************/
735 /* The battery maximum charging percentage */
736 static int charge_end_threshold;
738 static ssize_t charge_control_end_threshold_store(struct device *dev,
739 struct device_attribute *attr,
740 const char *buf, size_t count)
744 ret = kstrtouint(buf, 10, &value);
748 if (value < 0 || value > 100)
751 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
758 /* There isn't any method in the DSDT to read the threshold, so we
759 * save the threshold.
761 charge_end_threshold = value;
765 static ssize_t charge_control_end_threshold_show(struct device *device,
766 struct device_attribute *attr,
769 return sprintf(buf, "%d\n", charge_end_threshold);
772 static DEVICE_ATTR_RW(charge_control_end_threshold);
774 static int asus_wmi_battery_add(struct power_supply *battery)
776 /* The WMI method does not provide a way to specific a battery, so we
777 * just assume it is the first battery.
778 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
779 * battery is named BATT.
781 if (strcmp(battery->desc->name, "BAT0") != 0 &&
782 strcmp(battery->desc->name, "BAT1") != 0 &&
783 strcmp(battery->desc->name, "BATC") != 0 &&
784 strcmp(battery->desc->name, "BATT") != 0)
787 if (device_create_file(&battery->dev,
788 &dev_attr_charge_control_end_threshold))
791 /* The charge threshold is only reset when the system is power cycled,
792 * and we can't get the current threshold so let set it to 100% when
793 * a battery is added.
795 asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
796 charge_end_threshold = 100;
801 static int asus_wmi_battery_remove(struct power_supply *battery)
803 device_remove_file(&battery->dev,
804 &dev_attr_charge_control_end_threshold);
808 static struct acpi_battery_hook battery_hook = {
809 .add_battery = asus_wmi_battery_add,
810 .remove_battery = asus_wmi_battery_remove,
811 .name = "ASUS Battery Extension",
814 static void asus_wmi_battery_init(struct asus_wmi *asus)
816 asus->battery_rsoc_available = false;
817 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
818 asus->battery_rsoc_available = true;
819 battery_hook_register(&battery_hook);
823 static void asus_wmi_battery_exit(struct asus_wmi *asus)
825 if (asus->battery_rsoc_available)
826 battery_hook_unregister(&battery_hook);
829 /* LEDs ***********************************************************************/
832 * These functions actually update the LED's, and are called from a
833 * workqueue. By doing this as separate work rather than when the LED
834 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
835 * potentially bad time, such as a timer interrupt.
837 static void tpd_led_update(struct work_struct *work)
840 struct asus_wmi *asus;
842 asus = container_of(work, struct asus_wmi, tpd_led_work);
844 ctrl_param = asus->tpd_led_wk;
845 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
848 static void tpd_led_set(struct led_classdev *led_cdev,
849 enum led_brightness value)
851 struct asus_wmi *asus;
853 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
855 asus->tpd_led_wk = !!value;
856 queue_work(asus->led_workqueue, &asus->tpd_led_work);
859 static int read_tpd_led_state(struct asus_wmi *asus)
861 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
864 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
866 struct asus_wmi *asus;
868 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
870 return read_tpd_led_state(asus);
873 static void kbd_led_update(struct asus_wmi *asus)
877 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
878 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
881 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
887 * bit 7: light on/off
888 * bit 8-10: environment (0: dark, 1: normal, 2: light)
889 * bit 17: status unknown
891 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
894 /* Unknown status is considered as off */
895 if (retval == 0x8000)
902 *level = retval & 0x7F;
904 *env = (retval >> 8) & 0x7F;
908 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
910 struct asus_wmi *asus;
913 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
914 max_level = asus->kbd_led.max_brightness;
916 asus->kbd_led_wk = clamp_val(value, 0, max_level);
917 kbd_led_update(asus);
920 static void kbd_led_set(struct led_classdev *led_cdev,
921 enum led_brightness value)
923 /* Prevent disabling keyboard backlight on module unregister */
924 if (led_cdev->flags & LED_UNREGISTERING)
927 do_kbd_led_set(led_cdev, value);
930 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
932 struct led_classdev *led_cdev = &asus->kbd_led;
934 do_kbd_led_set(led_cdev, value);
935 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
938 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
940 struct asus_wmi *asus;
943 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
945 retval = kbd_led_read(asus, &value, NULL);
952 static int wlan_led_unknown_state(struct asus_wmi *asus)
956 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
958 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
961 static void wlan_led_update(struct work_struct *work)
964 struct asus_wmi *asus;
966 asus = container_of(work, struct asus_wmi, wlan_led_work);
968 ctrl_param = asus->wlan_led_wk;
969 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
972 static void wlan_led_set(struct led_classdev *led_cdev,
973 enum led_brightness value)
975 struct asus_wmi *asus;
977 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
979 asus->wlan_led_wk = !!value;
980 queue_work(asus->led_workqueue, &asus->wlan_led_work);
983 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
985 struct asus_wmi *asus;
988 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
989 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
991 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
994 static void lightbar_led_update(struct work_struct *work)
996 struct asus_wmi *asus;
999 asus = container_of(work, struct asus_wmi, lightbar_led_work);
1001 ctrl_param = asus->lightbar_led_wk;
1002 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
1005 static void lightbar_led_set(struct led_classdev *led_cdev,
1006 enum led_brightness value)
1008 struct asus_wmi *asus;
1010 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1012 asus->lightbar_led_wk = !!value;
1013 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
1016 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
1018 struct asus_wmi *asus;
1021 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1022 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
1024 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
1027 static void asus_wmi_led_exit(struct asus_wmi *asus)
1029 led_classdev_unregister(&asus->kbd_led);
1030 led_classdev_unregister(&asus->tpd_led);
1031 led_classdev_unregister(&asus->wlan_led);
1032 led_classdev_unregister(&asus->lightbar_led);
1034 if (asus->led_workqueue)
1035 destroy_workqueue(asus->led_workqueue);
1038 static int asus_wmi_led_init(struct asus_wmi *asus)
1040 int rv = 0, led_val;
1042 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
1043 if (!asus->led_workqueue)
1046 if (read_tpd_led_state(asus) >= 0) {
1047 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
1049 asus->tpd_led.name = "asus::touchpad";
1050 asus->tpd_led.brightness_set = tpd_led_set;
1051 asus->tpd_led.brightness_get = tpd_led_get;
1052 asus->tpd_led.max_brightness = 1;
1054 rv = led_classdev_register(&asus->platform_device->dev,
1060 if (!kbd_led_read(asus, &led_val, NULL)) {
1061 asus->kbd_led_wk = led_val;
1062 asus->kbd_led.name = "asus::kbd_backlight";
1063 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
1064 asus->kbd_led.brightness_set = kbd_led_set;
1065 asus->kbd_led.brightness_get = kbd_led_get;
1066 asus->kbd_led.max_brightness = 3;
1068 rv = led_classdev_register(&asus->platform_device->dev,
1074 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
1075 && (asus->driver->quirks->wapf > 0)) {
1076 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
1078 asus->wlan_led.name = "asus::wlan";
1079 asus->wlan_led.brightness_set = wlan_led_set;
1080 if (!wlan_led_unknown_state(asus))
1081 asus->wlan_led.brightness_get = wlan_led_get;
1082 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
1083 asus->wlan_led.max_brightness = 1;
1084 asus->wlan_led.default_trigger = "asus-wlan";
1086 rv = led_classdev_register(&asus->platform_device->dev,
1092 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
1093 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
1095 asus->lightbar_led.name = "asus::lightbar";
1096 asus->lightbar_led.brightness_set = lightbar_led_set;
1097 asus->lightbar_led.brightness_get = lightbar_led_get;
1098 asus->lightbar_led.max_brightness = 1;
1100 rv = led_classdev_register(&asus->platform_device->dev,
1101 &asus->lightbar_led);
1106 asus_wmi_led_exit(asus);
1111 /* RF *************************************************************************/
1114 * PCI hotplug (for wlan rfkill)
1116 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
1118 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1125 static void asus_rfkill_hotplug(struct asus_wmi *asus)
1127 struct pci_dev *dev;
1128 struct pci_bus *bus;
1133 mutex_lock(&asus->wmi_lock);
1134 blocked = asus_wlan_rfkill_blocked(asus);
1135 mutex_unlock(&asus->wmi_lock);
1137 mutex_lock(&asus->hotplug_lock);
1138 pci_lock_rescan_remove();
1140 if (asus->wlan.rfkill)
1141 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
1143 if (asus->hotplug_slot.ops) {
1144 bus = pci_find_bus(0, 1);
1146 pr_warn("Unable to find PCI bus 1?\n");
1150 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
1151 pr_err("Unable to read PCI config space?\n");
1154 absent = (l == 0xffffffff);
1156 if (blocked != absent) {
1157 pr_warn("BIOS says wireless lan is %s, but the pci device is %s\n",
1158 blocked ? "blocked" : "unblocked",
1159 absent ? "absent" : "present");
1160 pr_warn("skipped wireless hotplug as probably inappropriate for this model\n");
1165 dev = pci_get_slot(bus, 0);
1167 /* Device already present */
1171 dev = pci_scan_single_device(bus, 0);
1173 pci_bus_assign_resources(bus);
1174 pci_bus_add_device(dev);
1177 dev = pci_get_slot(bus, 0);
1179 pci_stop_and_remove_bus_device(dev);
1186 pci_unlock_rescan_remove();
1187 mutex_unlock(&asus->hotplug_lock);
1190 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
1192 struct asus_wmi *asus = data;
1194 if (event != ACPI_NOTIFY_BUS_CHECK)
1198 * We can't call directly asus_rfkill_hotplug because most
1199 * of the time WMBC is still being executed and not reetrant.
1200 * There is currently no way to tell ACPICA that we want this
1201 * method to be serialized, we schedule a asus_rfkill_hotplug
1202 * call later, in a safer context.
1204 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
1207 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
1212 status = acpi_get_handle(NULL, node, &handle);
1213 if (ACPI_FAILURE(status))
1216 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1217 asus_rfkill_notify, asus);
1218 if (ACPI_FAILURE(status))
1219 pr_warn("Failed to register notify on %s\n", node);
1224 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
1226 acpi_status status = AE_OK;
1229 status = acpi_get_handle(NULL, node, &handle);
1230 if (ACPI_FAILURE(status))
1233 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1234 asus_rfkill_notify);
1235 if (ACPI_FAILURE(status))
1236 pr_err("Error removing rfkill notify handler %s\n", node);
1239 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
1242 struct asus_wmi *asus = container_of(hotplug_slot,
1243 struct asus_wmi, hotplug_slot);
1244 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1253 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
1254 .get_adapter_status = asus_get_adapter_status,
1255 .get_power_status = asus_get_adapter_status,
1258 static void asus_hotplug_work(struct work_struct *work)
1260 struct asus_wmi *asus;
1262 asus = container_of(work, struct asus_wmi, hotplug_work);
1263 asus_rfkill_hotplug(asus);
1266 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
1269 struct pci_bus *bus = pci_find_bus(0, 1);
1272 pr_err("Unable to find wifi PCI bus\n");
1276 asus->hotplug_workqueue =
1277 create_singlethread_workqueue("hotplug_workqueue");
1278 if (!asus->hotplug_workqueue)
1279 goto error_workqueue;
1281 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
1283 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
1285 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
1287 pr_err("Unable to register hotplug slot - %d\n", ret);
1288 goto error_register;
1294 asus->hotplug_slot.ops = NULL;
1295 destroy_workqueue(asus->hotplug_workqueue);
1303 static int asus_rfkill_set(void *data, bool blocked)
1305 struct asus_rfkill *priv = data;
1306 u32 ctrl_param = !blocked;
1307 u32 dev_id = priv->dev_id;
1310 * If the user bit is set, BIOS can't set and record the wlan status,
1311 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
1312 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
1313 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
1314 * while setting the wlan status through WMI.
1315 * This is also the behavior that windows app will do.
1317 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1318 priv->asus->driver->wlan_ctrl_by_user)
1319 dev_id = ASUS_WMI_DEVID_WLAN_LED;
1321 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
1324 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
1326 struct asus_rfkill *priv = data;
1329 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
1334 rfkill_set_sw_state(priv->rfkill, !result);
1337 static int asus_rfkill_wlan_set(void *data, bool blocked)
1339 struct asus_rfkill *priv = data;
1340 struct asus_wmi *asus = priv->asus;
1344 * This handler is enabled only if hotplug is enabled.
1345 * In this case, the asus_wmi_set_devstate() will
1346 * trigger a wmi notification and we need to wait
1347 * this call to finish before being able to call
1350 mutex_lock(&asus->wmi_lock);
1351 ret = asus_rfkill_set(data, blocked);
1352 mutex_unlock(&asus->wmi_lock);
1356 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1357 .set_block = asus_rfkill_wlan_set,
1358 .query = asus_rfkill_query,
1361 static const struct rfkill_ops asus_rfkill_ops = {
1362 .set_block = asus_rfkill_set,
1363 .query = asus_rfkill_query,
1366 static int asus_new_rfkill(struct asus_wmi *asus,
1367 struct asus_rfkill *arfkill,
1368 const char *name, enum rfkill_type type, int dev_id)
1370 int result = asus_wmi_get_devstate_simple(asus, dev_id);
1371 struct rfkill **rfkill = &arfkill->rfkill;
1376 arfkill->dev_id = dev_id;
1377 arfkill->asus = asus;
1379 if (dev_id == ASUS_WMI_DEVID_WLAN &&
1380 asus->driver->quirks->hotplug_wireless)
1381 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1382 &asus_rfkill_wlan_ops, arfkill);
1384 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1385 &asus_rfkill_ops, arfkill);
1390 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1391 (asus->driver->quirks->wapf > 0))
1392 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1394 rfkill_init_sw_state(*rfkill, !result);
1395 result = rfkill_register(*rfkill);
1397 rfkill_destroy(*rfkill);
1404 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1406 if (asus->driver->wlan_ctrl_by_user && ashs_present())
1409 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1410 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1411 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1412 if (asus->wlan.rfkill) {
1413 rfkill_unregister(asus->wlan.rfkill);
1414 rfkill_destroy(asus->wlan.rfkill);
1415 asus->wlan.rfkill = NULL;
1418 * Refresh pci hotplug in case the rfkill state was changed after
1419 * asus_unregister_rfkill_notifier()
1421 asus_rfkill_hotplug(asus);
1422 if (asus->hotplug_slot.ops)
1423 pci_hp_deregister(&asus->hotplug_slot);
1424 if (asus->hotplug_workqueue)
1425 destroy_workqueue(asus->hotplug_workqueue);
1427 if (asus->bluetooth.rfkill) {
1428 rfkill_unregister(asus->bluetooth.rfkill);
1429 rfkill_destroy(asus->bluetooth.rfkill);
1430 asus->bluetooth.rfkill = NULL;
1432 if (asus->wimax.rfkill) {
1433 rfkill_unregister(asus->wimax.rfkill);
1434 rfkill_destroy(asus->wimax.rfkill);
1435 asus->wimax.rfkill = NULL;
1437 if (asus->wwan3g.rfkill) {
1438 rfkill_unregister(asus->wwan3g.rfkill);
1439 rfkill_destroy(asus->wwan3g.rfkill);
1440 asus->wwan3g.rfkill = NULL;
1442 if (asus->gps.rfkill) {
1443 rfkill_unregister(asus->gps.rfkill);
1444 rfkill_destroy(asus->gps.rfkill);
1445 asus->gps.rfkill = NULL;
1447 if (asus->uwb.rfkill) {
1448 rfkill_unregister(asus->uwb.rfkill);
1449 rfkill_destroy(asus->uwb.rfkill);
1450 asus->uwb.rfkill = NULL;
1454 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1458 mutex_init(&asus->hotplug_lock);
1459 mutex_init(&asus->wmi_lock);
1461 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1462 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1464 if (result && result != -ENODEV)
1467 result = asus_new_rfkill(asus, &asus->bluetooth,
1468 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1469 ASUS_WMI_DEVID_BLUETOOTH);
1471 if (result && result != -ENODEV)
1474 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1475 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1477 if (result && result != -ENODEV)
1480 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1481 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1483 if (result && result != -ENODEV)
1486 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1487 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1489 if (result && result != -ENODEV)
1492 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1493 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1495 if (result && result != -ENODEV)
1498 if (!asus->driver->quirks->hotplug_wireless)
1501 result = asus_setup_pci_hotplug(asus);
1503 * If we get -EBUSY then something else is handling the PCI hotplug -
1504 * don't fail in this case
1506 if (result == -EBUSY)
1509 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1510 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1511 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1513 * Refresh pci hotplug in case the rfkill state was changed during
1516 asus_rfkill_hotplug(asus);
1519 if (result && result != -ENODEV)
1520 asus_wmi_rfkill_exit(asus);
1522 if (result == -ENODEV)
1528 /* Panel Overdrive ************************************************************/
1529 static int panel_od_check_present(struct asus_wmi *asus)
1534 asus->panel_overdrive_available = false;
1536 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_PANEL_OD, &result);
1543 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
1544 asus->panel_overdrive_available = true;
1545 asus->panel_overdrive = result & ASUS_WMI_DSTS_STATUS_BIT;
1551 static int panel_od_write(struct asus_wmi *asus)
1557 /* Don't rely on type conversion */
1558 value = asus->panel_overdrive ? 1 : 0;
1560 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, value, &retval);
1563 pr_warn("Failed to set panel overdrive: %d\n", err);
1568 pr_warn("Failed to set panel overdrive (retval): 0x%x\n", retval);
1572 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
1577 static ssize_t panel_od_show(struct device *dev,
1578 struct device_attribute *attr, char *buf)
1580 struct asus_wmi *asus = dev_get_drvdata(dev);
1582 return sysfs_emit(buf, "%d\n", asus->panel_overdrive);
1585 static ssize_t panel_od_store(struct device *dev,
1586 struct device_attribute *attr,
1587 const char *buf, size_t count)
1592 struct asus_wmi *asus = dev_get_drvdata(dev);
1594 result = kstrtobool(buf, &overdrive);
1598 asus->panel_overdrive = overdrive;
1599 result = panel_od_write(asus);
1607 static DEVICE_ATTR_RW(panel_od);
1609 /* Quirks *********************************************************************/
1611 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1613 struct pci_dev *xhci_pdev;
1614 u32 orig_ports_available;
1615 u32 ports_available = asus->driver->quirks->xusb2pr;
1617 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1618 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1624 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1625 &orig_ports_available);
1627 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1628 cpu_to_le32(ports_available));
1630 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1631 orig_ports_available, ports_available);
1635 * Some devices dont support or have borcken get_als method
1636 * but still support set method.
1638 static void asus_wmi_set_als(void)
1640 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1643 /* Hwmon device ***************************************************************/
1645 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1648 struct agfn_fan_args args = {
1649 .agfn.len = sizeof(args),
1650 .agfn.mfun = ASUS_FAN_MFUN,
1651 .agfn.sfun = ASUS_FAN_SFUN_READ,
1655 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1661 status = asus_wmi_evaluate_method_agfn(input);
1663 if (status || args.agfn.err)
1667 *speed = args.speed;
1672 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1675 struct agfn_fan_args args = {
1676 .agfn.len = sizeof(args),
1677 .agfn.mfun = ASUS_FAN_MFUN,
1678 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1680 .speed = speed ? *speed : 0,
1682 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1685 /* 1: for setting 1st fan's speed 0: setting auto mode */
1686 if (fan != 1 && fan != 0)
1689 status = asus_wmi_evaluate_method_agfn(input);
1691 if (status || args.agfn.err)
1694 if (speed && fan == 1)
1695 asus->agfn_pwm = *speed;
1701 * Check if we can read the speed of one fan. If true we assume we can also
1704 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
1710 status = asus_agfn_fan_speed_read(asus, 1, &speed);
1714 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1719 * We need to find a better way, probably using sfun,
1721 * Currently we disable it if:
1722 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1723 * - reverved bits are non-zero
1724 * - sfun and presence bit are not set
1726 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1727 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
1730 static int asus_fan_set_auto(struct asus_wmi *asus)
1735 switch (asus->fan_type) {
1736 case FAN_TYPE_SPEC83:
1737 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1747 status = asus_agfn_fan_speed_write(asus, 0, NULL);
1760 static ssize_t pwm1_show(struct device *dev,
1761 struct device_attribute *attr,
1764 struct asus_wmi *asus = dev_get_drvdata(dev);
1768 /* If we already set a value then just return it */
1769 if (asus->agfn_pwm >= 0)
1770 return sprintf(buf, "%d\n", asus->agfn_pwm);
1773 * If we haven't set already set a value through the AGFN interface,
1774 * we read a current value through the (now-deprecated) FAN_CTRL device.
1776 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1782 if (value == 1) /* Low Speed */
1784 else if (value == 2)
1786 else if (value == 3)
1789 pr_err("Unknown fan speed %#x\n", value);
1793 return sprintf(buf, "%d\n", value);
1796 static ssize_t pwm1_store(struct device *dev,
1797 struct device_attribute *attr,
1798 const char *buf, size_t count) {
1799 struct asus_wmi *asus = dev_get_drvdata(dev);
1804 ret = kstrtouint(buf, 10, &value);
1808 value = clamp(value, 0, 255);
1810 state = asus_agfn_fan_speed_write(asus, 1, &value);
1812 pr_warn("Setting fan speed failed: %d\n", state);
1814 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
1819 static ssize_t fan1_input_show(struct device *dev,
1820 struct device_attribute *attr,
1823 struct asus_wmi *asus = dev_get_drvdata(dev);
1827 switch (asus->fan_type) {
1828 case FAN_TYPE_SPEC83:
1829 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1838 /* no speed readable on manual mode */
1839 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1842 ret = asus_agfn_fan_speed_read(asus, 1, &value);
1844 pr_warn("reading fan speed failed: %d\n", ret);
1853 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1856 static ssize_t pwm1_enable_show(struct device *dev,
1857 struct device_attribute *attr,
1860 struct asus_wmi *asus = dev_get_drvdata(dev);
1863 * Just read back the cached pwm mode.
1865 * For the CPU_FAN device, the spec indicates that we should be
1866 * able to read the device status and consult bit 19 to see if we
1867 * are in Full On or Automatic mode. However, this does not work
1868 * in practice on X532FL at least (the bit is always 0) and there's
1869 * also nothing in the DSDT to indicate that this behaviour exists.
1871 return sprintf(buf, "%d\n", asus->fan_pwm_mode);
1874 static ssize_t pwm1_enable_store(struct device *dev,
1875 struct device_attribute *attr,
1876 const char *buf, size_t count)
1878 struct asus_wmi *asus = dev_get_drvdata(dev);
1885 ret = kstrtouint(buf, 10, &state);
1889 if (asus->fan_type == FAN_TYPE_SPEC83) {
1890 switch (state) { /* standard documented hwmon values */
1891 case ASUS_FAN_CTRL_FULLSPEED:
1894 case ASUS_FAN_CTRL_AUTO:
1901 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1908 } else if (asus->fan_type == FAN_TYPE_AGFN) {
1910 case ASUS_FAN_CTRL_MANUAL:
1913 case ASUS_FAN_CTRL_AUTO:
1914 status = asus_fan_set_auto(asus);
1924 asus->fan_pwm_mode = state;
1926 /* Must set to disabled if mode is toggled */
1927 if (asus->cpu_fan_curve_available)
1928 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
1929 if (asus->gpu_fan_curve_available)
1930 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
1935 static ssize_t fan1_label_show(struct device *dev,
1936 struct device_attribute *attr,
1939 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1942 static ssize_t asus_hwmon_temp1(struct device *dev,
1943 struct device_attribute *attr,
1946 struct asus_wmi *asus = dev_get_drvdata(dev);
1950 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1954 return sprintf(buf, "%ld\n",
1955 deci_kelvin_to_millicelsius(value & 0xFFFF));
1959 static DEVICE_ATTR_RW(pwm1);
1960 static DEVICE_ATTR_RW(pwm1_enable);
1961 static DEVICE_ATTR_RO(fan1_input);
1962 static DEVICE_ATTR_RO(fan1_label);
1965 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1967 static struct attribute *hwmon_attributes[] = {
1968 &dev_attr_pwm1.attr,
1969 &dev_attr_pwm1_enable.attr,
1970 &dev_attr_fan1_input.attr,
1971 &dev_attr_fan1_label.attr,
1973 &dev_attr_temp1_input.attr,
1977 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1978 struct attribute *attr, int idx)
1980 struct device *dev = container_of(kobj, struct device, kobj);
1981 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
1982 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1984 if (attr == &dev_attr_pwm1.attr) {
1985 if (asus->fan_type != FAN_TYPE_AGFN)
1987 } else if (attr == &dev_attr_fan1_input.attr
1988 || attr == &dev_attr_fan1_label.attr
1989 || attr == &dev_attr_pwm1_enable.attr) {
1990 if (asus->fan_type == FAN_TYPE_NONE)
1992 } else if (attr == &dev_attr_temp1_input.attr) {
1993 int err = asus_wmi_get_devstate(asus,
1994 ASUS_WMI_DEVID_THERMAL_CTRL,
1998 return 0; /* can't return negative here */
2001 * If the temperature value in deci-Kelvin is near the absolute
2002 * zero temperature, something is clearly wrong
2004 if (value == 0 || value == 1)
2011 static const struct attribute_group hwmon_attribute_group = {
2012 .is_visible = asus_hwmon_sysfs_is_visible,
2013 .attrs = hwmon_attributes
2015 __ATTRIBUTE_GROUPS(hwmon_attribute);
2017 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
2019 struct device *dev = &asus->platform_device->dev;
2020 struct device *hwmon;
2022 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
2023 hwmon_attribute_groups);
2025 if (IS_ERR(hwmon)) {
2026 pr_err("Could not register asus hwmon device\n");
2027 return PTR_ERR(hwmon);
2032 static int asus_wmi_fan_init(struct asus_wmi *asus)
2034 asus->fan_type = FAN_TYPE_NONE;
2035 asus->agfn_pwm = -1;
2037 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
2038 asus->fan_type = FAN_TYPE_SPEC83;
2039 else if (asus_wmi_has_agfn_fan(asus))
2040 asus->fan_type = FAN_TYPE_AGFN;
2042 if (asus->fan_type == FAN_TYPE_NONE)
2045 asus_fan_set_auto(asus);
2046 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
2050 /* Fan mode *******************************************************************/
2052 static int fan_boost_mode_check_present(struct asus_wmi *asus)
2057 asus->fan_boost_mode_available = false;
2059 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
2068 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2069 (result & ASUS_FAN_BOOST_MODES_MASK)) {
2070 asus->fan_boost_mode_available = true;
2071 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
2077 static int fan_boost_mode_write(struct asus_wmi *asus)
2083 value = asus->fan_boost_mode;
2085 pr_info("Set fan boost mode: %u\n", value);
2086 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
2089 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2093 pr_warn("Failed to set fan boost mode: %d\n", err);
2098 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
2106 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
2108 u8 mask = asus->fan_boost_mode_mask;
2110 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
2111 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
2112 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
2113 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2114 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2115 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2116 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2117 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2119 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2121 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2124 return fan_boost_mode_write(asus);
2127 static ssize_t fan_boost_mode_show(struct device *dev,
2128 struct device_attribute *attr, char *buf)
2130 struct asus_wmi *asus = dev_get_drvdata(dev);
2132 return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
2135 static ssize_t fan_boost_mode_store(struct device *dev,
2136 struct device_attribute *attr,
2137 const char *buf, size_t count)
2139 struct asus_wmi *asus = dev_get_drvdata(dev);
2140 u8 mask = asus->fan_boost_mode_mask;
2144 result = kstrtou8(buf, 10, &new_mode);
2146 pr_warn("Trying to store invalid value\n");
2150 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2151 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
2153 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
2154 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
2156 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
2160 asus->fan_boost_mode = new_mode;
2161 fan_boost_mode_write(asus);
2166 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
2167 static DEVICE_ATTR_RW(fan_boost_mode);
2169 /* Custom fan curves **********************************************************/
2171 static void fan_curve_copy_from_buf(struct fan_curve_data *data, u8 *buf)
2175 for (i = 0; i < FAN_CURVE_POINTS; i++) {
2176 data->temps[i] = buf[i];
2179 for (i = 0; i < FAN_CURVE_POINTS; i++) {
2181 255 * buf[i + FAN_CURVE_POINTS] / 100;
2185 static int fan_curve_get_factory_default(struct asus_wmi *asus, u32 fan_dev)
2187 struct fan_curve_data *curves;
2188 u8 buf[FAN_CURVE_BUF_LEN];
2193 if (asus->throttle_thermal_policy_available)
2194 mode = asus->throttle_thermal_policy_mode;
2195 /* DEVID_<C/G>PU_FAN_CURVE is switched for OVERBOOST vs SILENT */
2201 if (fan_dev == ASUS_WMI_DEVID_GPU_FAN_CURVE)
2202 fan_idx = FAN_CURVE_DEV_GPU;
2204 curves = &asus->custom_fan_curves[fan_idx];
2205 err = asus_wmi_evaluate_method_buf(asus->dsts_id, fan_dev, mode, buf,
2210 fan_curve_copy_from_buf(curves, buf);
2211 curves->device_id = fan_dev;
2216 /* Check if capability exists, and populate defaults */
2217 static int fan_curve_check_present(struct asus_wmi *asus, bool *available,
2224 err = fan_curve_get_factory_default(asus, fan_dev);
2226 if (err == -ENODEV || err == -ENODATA)
2235 /* Determine which fan the attribute is for if SENSOR_ATTR */
2236 static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus,
2237 struct device_attribute *attr)
2239 int index = to_sensor_dev_attr(attr)->index;
2241 return &asus->custom_fan_curves[index & FAN_CURVE_DEV_GPU];
2244 /* Determine which fan the attribute is for if SENSOR_ATTR_2 */
2245 static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus,
2246 struct device_attribute *attr)
2248 int nr = to_sensor_dev_attr_2(attr)->nr;
2250 return &asus->custom_fan_curves[nr & FAN_CURVE_DEV_GPU];
2253 static ssize_t fan_curve_show(struct device *dev,
2254 struct device_attribute *attr, char *buf)
2256 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2257 struct asus_wmi *asus = dev_get_drvdata(dev);
2258 struct fan_curve_data *data;
2259 int value, index, nr;
2261 data = fan_curve_attr_2_select(asus, attr);
2262 index = dev_attr->index;
2265 if (nr & FAN_CURVE_PWM_MASK)
2266 value = data->percents[index];
2268 value = data->temps[index];
2270 return sysfs_emit(buf, "%d\n", value);
2274 * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE.
2276 static int fan_curve_write(struct asus_wmi *asus,
2277 struct fan_curve_data *data)
2279 u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0;
2280 u8 *percents = data->percents;
2281 u8 *temps = data->temps;
2282 int ret, i, shift = 0;
2287 for (i = 0; i < FAN_CURVE_POINTS / 2; i++) {
2288 arg1 += (temps[i]) << shift;
2289 arg2 += (temps[i + 4]) << shift;
2290 /* Scale to percentage for device */
2291 arg3 += (100 * percents[i] / 255) << shift;
2292 arg4 += (100 * percents[i + 4] / 255) << shift;
2296 return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS,
2298 arg1, arg2, arg3, arg4, &ret);
2301 static ssize_t fan_curve_store(struct device *dev,
2302 struct device_attribute *attr, const char *buf,
2305 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2306 struct asus_wmi *asus = dev_get_drvdata(dev);
2307 struct fan_curve_data *data;
2311 int pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
2312 int index = dev_attr->index;
2314 data = fan_curve_attr_2_select(asus, attr);
2316 err = kstrtou8(buf, 10, &value);
2321 data->percents[index] = value;
2323 data->temps[index] = value;
2327 * Mark as disabled so the user has to explicitly enable to apply a
2328 * changed fan curve. This prevents potential lockups from writing out
2329 * many changes as one-write-per-change.
2331 data->enabled = false;
2336 static ssize_t fan_curve_enable_show(struct device *dev,
2337 struct device_attribute *attr, char *buf)
2339 struct asus_wmi *asus = dev_get_drvdata(dev);
2340 struct fan_curve_data *data;
2343 data = fan_curve_attr_select(asus, attr);
2348 return sysfs_emit(buf, "%d\n", out);
2351 static ssize_t fan_curve_enable_store(struct device *dev,
2352 struct device_attribute *attr,
2353 const char *buf, size_t count)
2355 struct asus_wmi *asus = dev_get_drvdata(dev);
2356 struct fan_curve_data *data;
2359 data = fan_curve_attr_select(asus, attr);
2361 err = kstrtoint(buf, 10, &value);
2367 data->enabled = true;
2370 data->enabled = false;
2373 * Auto + reset the fan curve data to defaults. Make it an explicit
2374 * option so that users don't accidentally overwrite a set fan curve.
2377 err = fan_curve_get_factory_default(asus, data->device_id);
2380 data->enabled = false;
2386 if (data->enabled) {
2387 err = fan_curve_write(asus, data);
2392 * For machines with throttle this is the only way to reset fans
2393 * to default mode of operation (does not erase curve data).
2395 if (asus->throttle_thermal_policy_available) {
2396 err = throttle_thermal_policy_write(asus);
2399 /* Similar is true for laptops with this fan */
2400 } else if (asus->fan_type == FAN_TYPE_SPEC83) {
2401 err = asus_fan_set_auto(asus);
2405 /* Safeguard against fautly ACPI tables */
2406 err = fan_curve_get_factory_default(asus, data->device_id);
2409 err = fan_curve_write(asus, data);
2418 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU);
2419 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve,
2420 FAN_CURVE_DEV_CPU, 0);
2421 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve,
2422 FAN_CURVE_DEV_CPU, 1);
2423 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve,
2424 FAN_CURVE_DEV_CPU, 2);
2425 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve,
2426 FAN_CURVE_DEV_CPU, 3);
2427 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve,
2428 FAN_CURVE_DEV_CPU, 4);
2429 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve,
2430 FAN_CURVE_DEV_CPU, 5);
2431 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve,
2432 FAN_CURVE_DEV_CPU, 6);
2433 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve,
2434 FAN_CURVE_DEV_CPU, 7);
2436 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve,
2437 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0);
2438 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve,
2439 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1);
2440 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve,
2441 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2);
2442 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve,
2443 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3);
2444 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve,
2445 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4);
2446 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve,
2447 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5);
2448 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve,
2449 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6);
2450 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve,
2451 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7);
2454 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU);
2455 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve,
2456 FAN_CURVE_DEV_GPU, 0);
2457 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve,
2458 FAN_CURVE_DEV_GPU, 1);
2459 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve,
2460 FAN_CURVE_DEV_GPU, 2);
2461 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve,
2462 FAN_CURVE_DEV_GPU, 3);
2463 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve,
2464 FAN_CURVE_DEV_GPU, 4);
2465 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve,
2466 FAN_CURVE_DEV_GPU, 5);
2467 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve,
2468 FAN_CURVE_DEV_GPU, 6);
2469 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve,
2470 FAN_CURVE_DEV_GPU, 7);
2472 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve,
2473 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0);
2474 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve,
2475 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1);
2476 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve,
2477 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2);
2478 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve,
2479 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3);
2480 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve,
2481 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4);
2482 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve,
2483 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5);
2484 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve,
2485 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6);
2486 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve,
2487 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7);
2489 static struct attribute *asus_fan_curve_attr[] = {
2491 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2492 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2493 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2494 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2495 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2496 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
2497 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
2498 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
2499 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
2500 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2501 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2502 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2503 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2504 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
2505 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
2506 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
2507 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
2509 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2510 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2511 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2512 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2513 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2514 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
2515 &sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr,
2516 &sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr,
2517 &sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr,
2518 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
2519 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2520 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2521 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2522 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
2523 &sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr,
2524 &sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr,
2525 &sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr,
2529 static umode_t asus_fan_curve_is_visible(struct kobject *kobj,
2530 struct attribute *attr, int idx)
2532 struct device *dev = container_of(kobj, struct device, kobj);
2533 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2536 * Check the char instead of casting attr as there are two attr types
2537 * involved here (attr1 and attr2)
2539 if (asus->cpu_fan_curve_available && attr->name[3] == '1')
2542 if (asus->gpu_fan_curve_available && attr->name[3] == '2')
2548 static const struct attribute_group asus_fan_curve_attr_group = {
2549 .is_visible = asus_fan_curve_is_visible,
2550 .attrs = asus_fan_curve_attr,
2552 __ATTRIBUTE_GROUPS(asus_fan_curve_attr);
2555 * Must be initialised after throttle_thermal_policy_check_present() as
2556 * we check the status of throttle_thermal_policy_available during init.
2558 static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus)
2560 struct device *dev = &asus->platform_device->dev;
2561 struct device *hwmon;
2564 err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available,
2565 ASUS_WMI_DEVID_CPU_FAN_CURVE);
2569 err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available,
2570 ASUS_WMI_DEVID_GPU_FAN_CURVE);
2574 if (!asus->cpu_fan_curve_available && !asus->gpu_fan_curve_available)
2577 hwmon = devm_hwmon_device_register_with_groups(
2578 dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups);
2580 if (IS_ERR(hwmon)) {
2582 "Could not register asus_custom_fan_curve device\n");
2583 return PTR_ERR(hwmon);
2589 /* Throttle thermal policy ****************************************************/
2591 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
2596 asus->throttle_thermal_policy_available = false;
2598 err = asus_wmi_get_devstate(asus,
2599 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2607 if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
2608 asus->throttle_thermal_policy_available = true;
2613 static int throttle_thermal_policy_write(struct asus_wmi *asus)
2619 value = asus->throttle_thermal_policy_mode;
2621 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2624 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2625 "throttle_thermal_policy");
2628 pr_warn("Failed to set throttle thermal policy: %d\n", err);
2633 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
2638 /* Must set to disabled if mode is toggled */
2639 if (asus->cpu_fan_curve_available)
2640 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
2641 if (asus->gpu_fan_curve_available)
2642 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
2647 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
2649 if (!asus->throttle_thermal_policy_available)
2652 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2653 return throttle_thermal_policy_write(asus);
2656 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
2658 u8 new_mode = asus->throttle_thermal_policy_mode + 1;
2661 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2662 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2664 asus->throttle_thermal_policy_mode = new_mode;
2665 err = throttle_thermal_policy_write(asus);
2670 * Ensure that platform_profile updates userspace with the change to ensure
2671 * that platform_profile and throttle_thermal_policy_mode are in sync.
2673 platform_profile_notify();
2678 static ssize_t throttle_thermal_policy_show(struct device *dev,
2679 struct device_attribute *attr, char *buf)
2681 struct asus_wmi *asus = dev_get_drvdata(dev);
2682 u8 mode = asus->throttle_thermal_policy_mode;
2684 return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
2687 static ssize_t throttle_thermal_policy_store(struct device *dev,
2688 struct device_attribute *attr,
2689 const char *buf, size_t count)
2691 struct asus_wmi *asus = dev_get_drvdata(dev);
2696 result = kstrtou8(buf, 10, &new_mode);
2700 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2703 asus->throttle_thermal_policy_mode = new_mode;
2704 err = throttle_thermal_policy_write(asus);
2709 * Ensure that platform_profile updates userspace with the change to ensure
2710 * that platform_profile and throttle_thermal_policy_mode are in sync.
2712 platform_profile_notify();
2717 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
2718 static DEVICE_ATTR_RW(throttle_thermal_policy);
2720 /* Platform profile ***********************************************************/
2721 static int asus_wmi_platform_profile_get(struct platform_profile_handler *pprof,
2722 enum platform_profile_option *profile)
2724 struct asus_wmi *asus;
2727 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2729 tp = asus->throttle_thermal_policy_mode;
2732 case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
2733 *profile = PLATFORM_PROFILE_BALANCED;
2735 case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
2736 *profile = PLATFORM_PROFILE_PERFORMANCE;
2738 case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
2739 *profile = PLATFORM_PROFILE_QUIET;
2748 static int asus_wmi_platform_profile_set(struct platform_profile_handler *pprof,
2749 enum platform_profile_option profile)
2751 struct asus_wmi *asus;
2754 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2757 case PLATFORM_PROFILE_PERFORMANCE:
2758 tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
2760 case PLATFORM_PROFILE_BALANCED:
2761 tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2763 case PLATFORM_PROFILE_QUIET:
2764 tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
2770 asus->throttle_thermal_policy_mode = tp;
2771 return throttle_thermal_policy_write(asus);
2774 static int platform_profile_setup(struct asus_wmi *asus)
2776 struct device *dev = &asus->platform_device->dev;
2780 * Not an error if a component platform_profile relies on is unavailable
2781 * so early return, skipping the setup of platform_profile.
2783 if (!asus->throttle_thermal_policy_available)
2786 dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
2788 asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
2789 asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
2791 set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
2792 set_bit(PLATFORM_PROFILE_BALANCED,
2793 asus->platform_profile_handler.choices);
2794 set_bit(PLATFORM_PROFILE_PERFORMANCE,
2795 asus->platform_profile_handler.choices);
2797 err = platform_profile_register(&asus->platform_profile_handler);
2801 asus->platform_profile_support = true;
2805 /* Backlight ******************************************************************/
2807 static int read_backlight_power(struct asus_wmi *asus)
2811 if (asus->driver->quirks->store_backlight_power)
2812 ret = !asus->driver->panel_power;
2814 ret = asus_wmi_get_devstate_simple(asus,
2815 ASUS_WMI_DEVID_BACKLIGHT);
2820 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
2823 static int read_brightness_max(struct asus_wmi *asus)
2828 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2832 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
2841 static int read_brightness(struct backlight_device *bd)
2843 struct asus_wmi *asus = bl_get_data(bd);
2847 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2851 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
2854 static u32 get_scalar_command(struct backlight_device *bd)
2856 struct asus_wmi *asus = bl_get_data(bd);
2859 if ((asus->driver->brightness < bd->props.brightness) ||
2860 bd->props.brightness == bd->props.max_brightness)
2861 ctrl_param = 0x00008001;
2862 else if ((asus->driver->brightness > bd->props.brightness) ||
2863 bd->props.brightness == 0)
2864 ctrl_param = 0x00008000;
2866 asus->driver->brightness = bd->props.brightness;
2871 static int update_bl_status(struct backlight_device *bd)
2873 struct asus_wmi *asus = bl_get_data(bd);
2877 power = read_backlight_power(asus);
2878 if (power != -ENODEV && bd->props.power != power) {
2879 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
2880 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
2882 if (asus->driver->quirks->store_backlight_power)
2883 asus->driver->panel_power = bd->props.power;
2885 /* When using scalar brightness, updating the brightness
2886 * will mess with the backlight power */
2887 if (asus->driver->quirks->scalar_panel_brightness)
2891 if (asus->driver->quirks->scalar_panel_brightness)
2892 ctrl_param = get_scalar_command(bd);
2894 ctrl_param = bd->props.brightness;
2896 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
2902 static const struct backlight_ops asus_wmi_bl_ops = {
2903 .get_brightness = read_brightness,
2904 .update_status = update_bl_status,
2907 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
2909 struct backlight_device *bd = asus->backlight_device;
2910 int old = bd->props.brightness;
2913 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2914 new = code - NOTIFY_BRNUP_MIN + 1;
2915 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2916 new = code - NOTIFY_BRNDOWN_MIN;
2918 bd->props.brightness = new;
2919 backlight_update_status(bd);
2920 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
2925 static int asus_wmi_backlight_init(struct asus_wmi *asus)
2927 struct backlight_device *bd;
2928 struct backlight_properties props;
2932 max = read_brightness_max(asus);
2936 power = read_backlight_power(asus);
2937 if (power == -ENODEV)
2938 power = FB_BLANK_UNBLANK;
2942 memset(&props, 0, sizeof(struct backlight_properties));
2943 props.type = BACKLIGHT_PLATFORM;
2944 props.max_brightness = max;
2945 bd = backlight_device_register(asus->driver->name,
2946 &asus->platform_device->dev, asus,
2947 &asus_wmi_bl_ops, &props);
2949 pr_err("Could not register backlight device\n");
2953 asus->backlight_device = bd;
2955 if (asus->driver->quirks->store_backlight_power)
2956 asus->driver->panel_power = power;
2958 bd->props.brightness = read_brightness(bd);
2959 bd->props.power = power;
2960 backlight_update_status(bd);
2962 asus->driver->brightness = bd->props.brightness;
2967 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
2969 backlight_device_unregister(asus->backlight_device);
2971 asus->backlight_device = NULL;
2974 static int is_display_toggle(int code)
2976 /* display toggle keys */
2977 if ((code >= 0x61 && code <= 0x67) ||
2978 (code >= 0x8c && code <= 0x93) ||
2979 (code >= 0xa0 && code <= 0xa7) ||
2980 (code >= 0xd0 && code <= 0xd5))
2986 /* Fn-lock ********************************************************************/
2988 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
2992 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
2994 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2995 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
2998 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
3000 int mode = asus->fnlock_locked;
3002 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
3005 /* WMI events *****************************************************************/
3007 static int asus_wmi_get_event_code(u32 value)
3009 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
3010 union acpi_object *obj;
3014 status = wmi_get_event_data(value, &response);
3015 if (ACPI_FAILURE(status)) {
3016 pr_warn("Failed to get WMI notify code: %s\n",
3017 acpi_format_exception(status));
3021 obj = (union acpi_object *)response.pointer;
3023 if (obj && obj->type == ACPI_TYPE_INTEGER)
3024 code = (int)(obj->integer.value & WMI_EVENT_MASK);
3032 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
3034 unsigned int key_value = 1;
3035 bool autorelease = 1;
3036 int result, orig_code;
3040 if (asus->driver->key_filter) {
3041 asus->driver->key_filter(asus->driver, &code, &key_value,
3043 if (code == ASUS_WMI_KEY_IGNORE)
3047 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3048 code = ASUS_WMI_BRN_UP;
3049 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3050 code = ASUS_WMI_BRN_DOWN;
3052 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
3053 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3054 asus_wmi_backlight_notify(asus, orig_code);
3059 if (code == NOTIFY_KBD_BRTUP) {
3060 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3063 if (code == NOTIFY_KBD_BRTDWN) {
3064 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
3067 if (code == NOTIFY_KBD_BRTTOGGLE) {
3068 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
3069 kbd_led_set_by_kbd(asus, 0);
3071 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3075 if (code == NOTIFY_FNLOCK_TOGGLE) {
3076 asus->fnlock_locked = !asus->fnlock_locked;
3077 asus_wmi_fnlock_update(asus);
3081 if (asus->driver->quirks->use_kbd_dock_devid && code == NOTIFY_KBD_DOCK_CHANGE) {
3082 result = asus_wmi_get_devstate_simple(asus,
3083 ASUS_WMI_DEVID_KBD_DOCK);
3085 input_report_switch(asus->inputdev, SW_TABLET_MODE,
3087 input_sync(asus->inputdev);
3092 if (asus->driver->quirks->use_lid_flip_devid && code == NOTIFY_LID_FLIP) {
3093 lid_flip_tablet_mode_get_state(asus);
3097 if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
3098 fan_boost_mode_switch_next(asus);
3102 if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
3103 throttle_thermal_policy_switch_next(asus);
3107 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
3110 if (!sparse_keymap_report_event(asus->inputdev, code,
3111 key_value, autorelease))
3112 pr_info("Unknown key %x pressed\n", code);
3115 static void asus_wmi_notify(u32 value, void *context)
3117 struct asus_wmi *asus = context;
3121 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3122 code = asus_wmi_get_event_code(value);
3124 pr_warn("Failed to get notify code: %d\n", code);
3128 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3131 asus_wmi_handle_event_code(code, asus);
3134 * Double check that queue is present:
3135 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
3137 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
3141 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
3144 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
3149 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3150 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
3152 pr_warn("Failed to get event during flush: %d\n", code);
3156 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3160 pr_warn("Failed to flush event queue\n");
3164 /* Sysfs **********************************************************************/
3166 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
3167 const char *buf, size_t count)
3172 value = asus_wmi_get_devstate_simple(asus, devid);
3176 err = kstrtoint(buf, 0, &value);
3180 err = asus_wmi_set_devstate(devid, value, &retval);
3187 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
3189 int value = asus_wmi_get_devstate_simple(asus, devid);
3194 return sprintf(buf, "%d\n", value);
3197 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
3198 static ssize_t show_##_name(struct device *dev, \
3199 struct device_attribute *attr, \
3202 struct asus_wmi *asus = dev_get_drvdata(dev); \
3204 return show_sys_wmi(asus, _cm, buf); \
3206 static ssize_t store_##_name(struct device *dev, \
3207 struct device_attribute *attr, \
3208 const char *buf, size_t count) \
3210 struct asus_wmi *asus = dev_get_drvdata(dev); \
3212 return store_sys_wmi(asus, _cm, buf, count); \
3214 static struct device_attribute dev_attr_##_name = { \
3216 .name = __stringify(_name), \
3218 .show = show_##_name, \
3219 .store = store_##_name, \
3222 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
3223 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
3224 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
3225 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
3226 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
3228 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
3229 const char *buf, size_t count)
3233 rv = kstrtoint(buf, 0, &value);
3237 if (value < 0 || value > 2)
3240 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
3247 static DEVICE_ATTR_WO(cpufv);
3249 static struct attribute *platform_attributes[] = {
3250 &dev_attr_cpufv.attr,
3251 &dev_attr_camera.attr,
3252 &dev_attr_cardr.attr,
3253 &dev_attr_touchpad.attr,
3254 &dev_attr_egpu_enable.attr,
3255 &dev_attr_dgpu_disable.attr,
3256 &dev_attr_lid_resume.attr,
3257 &dev_attr_als_enable.attr,
3258 &dev_attr_fan_boost_mode.attr,
3259 &dev_attr_throttle_thermal_policy.attr,
3260 &dev_attr_panel_od.attr,
3264 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
3265 struct attribute *attr, int idx)
3267 struct device *dev = container_of(kobj, struct device, kobj);
3268 struct asus_wmi *asus = dev_get_drvdata(dev);
3272 if (attr == &dev_attr_camera.attr)
3273 devid = ASUS_WMI_DEVID_CAMERA;
3274 else if (attr == &dev_attr_cardr.attr)
3275 devid = ASUS_WMI_DEVID_CARDREADER;
3276 else if (attr == &dev_attr_touchpad.attr)
3277 devid = ASUS_WMI_DEVID_TOUCHPAD;
3278 else if (attr == &dev_attr_lid_resume.attr)
3279 devid = ASUS_WMI_DEVID_LID_RESUME;
3280 else if (attr == &dev_attr_als_enable.attr)
3281 devid = ASUS_WMI_DEVID_ALS_ENABLE;
3282 else if (attr == &dev_attr_egpu_enable.attr)
3283 ok = asus->egpu_enable_available;
3284 else if (attr == &dev_attr_dgpu_disable.attr)
3285 ok = asus->dgpu_disable_available;
3286 else if (attr == &dev_attr_fan_boost_mode.attr)
3287 ok = asus->fan_boost_mode_available;
3288 else if (attr == &dev_attr_throttle_thermal_policy.attr)
3289 ok = asus->throttle_thermal_policy_available;
3290 else if (attr == &dev_attr_panel_od.attr)
3291 ok = asus->panel_overdrive_available;
3294 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
3296 return ok ? attr->mode : 0;
3299 static const struct attribute_group platform_attribute_group = {
3300 .is_visible = asus_sysfs_is_visible,
3301 .attrs = platform_attributes
3304 static void asus_wmi_sysfs_exit(struct platform_device *device)
3306 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
3309 static int asus_wmi_sysfs_init(struct platform_device *device)
3311 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
3314 /* Platform device ************************************************************/
3316 static int asus_wmi_platform_init(struct asus_wmi *asus)
3318 struct device *dev = &asus->platform_device->dev;
3322 /* INIT enable hotkeys on some models */
3323 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
3324 pr_info("Initialization: %#x\n", rv);
3326 /* We don't know yet what to do with this version... */
3327 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
3328 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
3333 * The SFUN method probably allows the original driver to get the list
3334 * of features supported by a given model. For now, 0x0100 or 0x0800
3335 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
3336 * The significance of others is yet to be found.
3338 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
3339 pr_info("SFUN value: %#x\n", rv);
3344 * Eee PC and Notebooks seems to have different method_id for DSTS,
3345 * but it may also be related to the BIOS's SPEC.
3346 * Note, on most Eeepc, there is no way to check if a method exist
3347 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
3348 * but once again, SPEC may probably be used for that kind of things.
3350 * Additionally at least TUF Gaming series laptops return nothing for
3351 * unknown methods, so the detection in this way is not possible.
3353 * There is strong indication that only ACPI WMI devices that have _UID
3354 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
3356 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
3360 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
3361 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
3362 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
3364 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
3365 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
3369 * Some devices can have multiple event codes stored in a queue before
3370 * the module load if it was unloaded intermittently after calling
3371 * the INIT method (enables event handling). The WMI notify handler is
3372 * expected to retrieve all event codes until a retrieved code equals
3373 * queue end marker (One or Ones). Old codes are flushed from the queue
3374 * upon module load. Not enabling this when it should be has minimal
3375 * visible impact so fall back if anything goes wrong.
3377 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
3378 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
3379 dev_info(dev, "Detected ATK, enable event queue\n");
3381 if (!asus_wmi_notify_queue_flush(asus))
3382 asus->wmi_event_queue = true;
3385 /* CWAP allow to define the behavior of the Fn+F2 key,
3386 * this method doesn't seems to be present on Eee PCs */
3387 if (asus->driver->quirks->wapf >= 0)
3388 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
3389 asus->driver->quirks->wapf, NULL);
3394 /* debugfs ********************************************************************/
3396 struct asus_wmi_debugfs_node {
3397 struct asus_wmi *asus;
3399 int (*show) (struct seq_file *m, void *data);
3402 static int show_dsts(struct seq_file *m, void *data)
3404 struct asus_wmi *asus = m->private;
3408 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
3412 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
3417 static int show_devs(struct seq_file *m, void *data)
3419 struct asus_wmi *asus = m->private;
3423 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
3428 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
3429 asus->debug.ctrl_param, retval);
3434 static int show_call(struct seq_file *m, void *data)
3436 struct asus_wmi *asus = m->private;
3437 struct bios_args args = {
3438 .arg0 = asus->debug.dev_id,
3439 .arg1 = asus->debug.ctrl_param,
3441 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
3442 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
3443 union acpi_object *obj;
3446 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
3447 0, asus->debug.method_id,
3450 if (ACPI_FAILURE(status))
3453 obj = (union acpi_object *)output.pointer;
3454 if (obj && obj->type == ACPI_TYPE_INTEGER)
3455 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
3456 asus->debug.dev_id, asus->debug.ctrl_param,
3457 (u32) obj->integer.value);
3459 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
3460 asus->debug.dev_id, asus->debug.ctrl_param,
3461 obj ? obj->type : -1);
3468 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
3469 {NULL, "devs", show_devs},
3470 {NULL, "dsts", show_dsts},
3471 {NULL, "call", show_call},
3474 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
3476 struct asus_wmi_debugfs_node *node = inode->i_private;
3478 return single_open(file, node->show, node->asus);
3481 static const struct file_operations asus_wmi_debugfs_io_ops = {
3482 .owner = THIS_MODULE,
3483 .open = asus_wmi_debugfs_open,
3485 .llseek = seq_lseek,
3486 .release = single_release,
3489 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
3491 debugfs_remove_recursive(asus->debug.root);
3494 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
3498 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
3500 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
3501 &asus->debug.method_id);
3503 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
3504 &asus->debug.dev_id);
3506 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
3507 &asus->debug.ctrl_param);
3509 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
3510 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
3513 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
3514 asus->debug.root, node,
3515 &asus_wmi_debugfs_io_ops);
3519 /* Init / exit ****************************************************************/
3521 static int asus_wmi_add(struct platform_device *pdev)
3523 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3524 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3525 struct asus_wmi *asus;
3526 const char *chassis_type;
3531 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
3535 asus->driver = wdrv;
3536 asus->platform_device = pdev;
3537 wdrv->platform_device = pdev;
3538 platform_set_drvdata(asus->platform_device, asus);
3540 if (wdrv->detect_quirks)
3541 wdrv->detect_quirks(asus->driver);
3543 err = asus_wmi_platform_init(asus);
3547 err = egpu_enable_check_present(asus);
3549 goto fail_egpu_enable;
3551 err = dgpu_disable_check_present(asus);
3553 goto fail_dgpu_disable;
3555 err = fan_boost_mode_check_present(asus);
3557 goto fail_fan_boost_mode;
3559 err = throttle_thermal_policy_check_present(asus);
3561 goto fail_throttle_thermal_policy;
3563 throttle_thermal_policy_set_default(asus);
3565 err = platform_profile_setup(asus);
3567 goto fail_platform_profile_setup;
3569 err = panel_od_check_present(asus);
3573 err = asus_wmi_sysfs_init(asus->platform_device);
3577 err = asus_wmi_input_init(asus);
3581 err = asus_wmi_fan_init(asus); /* probably no problems on error */
3583 err = asus_wmi_hwmon_init(asus);
3587 err = asus_wmi_custom_fan_curve_init(asus);
3589 goto fail_custom_fan_curve;
3591 err = asus_wmi_led_init(asus);
3595 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
3596 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
3597 asus->driver->wlan_ctrl_by_user = 1;
3599 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
3600 err = asus_wmi_rfkill_init(asus);
3605 if (asus->driver->quirks->wmi_force_als_set)
3608 /* Some Asus desktop boards export an acpi-video backlight interface,
3609 stop this from showing up */
3610 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
3611 if (chassis_type && !strcmp(chassis_type, "3"))
3612 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3614 if (asus->driver->quirks->wmi_backlight_power)
3615 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3617 if (asus->driver->quirks->wmi_backlight_native)
3618 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
3620 if (asus->driver->quirks->xusb2pr)
3621 asus_wmi_set_xusb2pr(asus);
3623 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3624 err = asus_wmi_backlight_init(asus);
3625 if (err && err != -ENODEV)
3626 goto fail_backlight;
3627 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
3628 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
3630 if (asus_wmi_has_fnlock_key(asus)) {
3631 asus->fnlock_locked = fnlock_default;
3632 asus_wmi_fnlock_update(asus);
3635 status = wmi_install_notify_handler(asus->driver->event_guid,
3636 asus_wmi_notify, asus);
3637 if (ACPI_FAILURE(status)) {
3638 pr_err("Unable to register notify handler - %d\n", status);
3640 goto fail_wmi_handler;
3643 asus_wmi_battery_init(asus);
3645 asus_wmi_debugfs_init(asus);
3650 asus_wmi_backlight_exit(asus);
3652 asus_wmi_rfkill_exit(asus);
3654 asus_wmi_led_exit(asus);
3657 asus_wmi_input_exit(asus);
3659 asus_wmi_sysfs_exit(asus->platform_device);
3661 fail_throttle_thermal_policy:
3662 fail_custom_fan_curve:
3663 fail_platform_profile_setup:
3664 if (asus->platform_profile_support)
3665 platform_profile_remove();
3666 fail_fan_boost_mode:
3675 static int asus_wmi_remove(struct platform_device *device)
3677 struct asus_wmi *asus;
3679 asus = platform_get_drvdata(device);
3680 wmi_remove_notify_handler(asus->driver->event_guid);
3681 asus_wmi_backlight_exit(asus);
3682 asus_wmi_input_exit(asus);
3683 asus_wmi_led_exit(asus);
3684 asus_wmi_rfkill_exit(asus);
3685 asus_wmi_debugfs_exit(asus);
3686 asus_wmi_sysfs_exit(asus->platform_device);
3687 asus_fan_set_auto(asus);
3688 throttle_thermal_policy_set_default(asus);
3689 asus_wmi_battery_exit(asus);
3691 if (asus->platform_profile_support)
3692 platform_profile_remove();
3698 /* Platform driver - hibernate/resume callbacks *******************************/
3700 static int asus_hotk_thaw(struct device *device)
3702 struct asus_wmi *asus = dev_get_drvdata(device);
3704 if (asus->wlan.rfkill) {
3708 * Work around bios bug - acpi _PTS turns off the wireless led
3709 * during suspend. Normally it restores it on resume, but
3710 * we should kick it ourselves in case hibernation is aborted.
3712 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
3713 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
3719 static int asus_hotk_resume(struct device *device)
3721 struct asus_wmi *asus = dev_get_drvdata(device);
3723 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3724 kbd_led_update(asus);
3726 if (asus_wmi_has_fnlock_key(asus))
3727 asus_wmi_fnlock_update(asus);
3729 if (asus->driver->quirks->use_lid_flip_devid)
3730 lid_flip_tablet_mode_get_state(asus);
3735 static int asus_hotk_restore(struct device *device)
3737 struct asus_wmi *asus = dev_get_drvdata(device);
3740 /* Refresh both wlan rfkill state and pci hotplug */
3741 if (asus->wlan.rfkill)
3742 asus_rfkill_hotplug(asus);
3744 if (asus->bluetooth.rfkill) {
3745 bl = !asus_wmi_get_devstate_simple(asus,
3746 ASUS_WMI_DEVID_BLUETOOTH);
3747 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
3749 if (asus->wimax.rfkill) {
3750 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
3751 rfkill_set_sw_state(asus->wimax.rfkill, bl);
3753 if (asus->wwan3g.rfkill) {
3754 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
3755 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
3757 if (asus->gps.rfkill) {
3758 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
3759 rfkill_set_sw_state(asus->gps.rfkill, bl);
3761 if (asus->uwb.rfkill) {
3762 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
3763 rfkill_set_sw_state(asus->uwb.rfkill, bl);
3765 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3766 kbd_led_update(asus);
3768 if (asus_wmi_has_fnlock_key(asus))
3769 asus_wmi_fnlock_update(asus);
3771 if (asus->driver->quirks->use_lid_flip_devid)
3772 lid_flip_tablet_mode_get_state(asus);
3777 static const struct dev_pm_ops asus_pm_ops = {
3778 .thaw = asus_hotk_thaw,
3779 .restore = asus_hotk_restore,
3780 .resume = asus_hotk_resume,
3783 /* Registration ***************************************************************/
3785 static int asus_wmi_probe(struct platform_device *pdev)
3787 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3788 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3791 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
3792 pr_warn("ASUS Management GUID not found\n");
3796 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
3797 pr_warn("ASUS Event GUID not found\n");
3802 ret = wdrv->probe(pdev);
3807 return asus_wmi_add(pdev);
3812 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
3814 struct platform_driver *platform_driver;
3815 struct platform_device *platform_device;
3820 platform_driver = &driver->platform_driver;
3821 platform_driver->remove = asus_wmi_remove;
3822 platform_driver->driver.owner = driver->owner;
3823 platform_driver->driver.name = driver->name;
3824 platform_driver->driver.pm = &asus_pm_ops;
3826 platform_device = platform_create_bundle(platform_driver,
3829 if (IS_ERR(platform_device))
3830 return PTR_ERR(platform_device);
3835 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
3837 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
3839 platform_device_unregister(driver->platform_device);
3840 platform_driver_unregister(&driver->platform_driver);
3843 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
3845 static int __init asus_wmi_init(void)
3847 pr_info("ASUS WMI generic driver loaded\n");
3851 static void __exit asus_wmi_exit(void)
3853 pr_info("ASUS WMI generic driver unloaded\n");
3856 module_init(asus_wmi_init);
3857 module_exit(asus_wmi_exit);