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
71 #define NOTIFY_LID_FLIP_ROG 0xbd
73 #define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
75 #define ASUS_MID_FAN_DESC "mid_fan"
76 #define ASUS_GPU_FAN_DESC "gpu_fan"
77 #define ASUS_FAN_DESC "cpu_fan"
78 #define ASUS_FAN_MFUN 0x13
79 #define ASUS_FAN_SFUN_READ 0x06
80 #define ASUS_FAN_SFUN_WRITE 0x07
82 /* Based on standard hwmon pwmX_enable values */
83 #define ASUS_FAN_CTRL_FULLSPEED 0
84 #define ASUS_FAN_CTRL_MANUAL 1
85 #define ASUS_FAN_CTRL_AUTO 2
87 #define ASUS_FAN_BOOST_MODE_NORMAL 0
88 #define ASUS_FAN_BOOST_MODE_OVERBOOST 1
89 #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01
90 #define ASUS_FAN_BOOST_MODE_SILENT 2
91 #define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02
92 #define ASUS_FAN_BOOST_MODES_MASK 0x03
94 #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT 0
95 #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST 1
96 #define ASUS_THROTTLE_THERMAL_POLICY_SILENT 2
98 #define USB_INTEL_XUSB2PR 0xD0
99 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
101 #define ASUS_ACPI_UID_ASUSWMI "ASUSWMI"
102 #define ASUS_ACPI_UID_ATK "ATK"
104 #define WMI_EVENT_QUEUE_SIZE 0x10
105 #define WMI_EVENT_QUEUE_END 0x1
106 #define WMI_EVENT_MASK 0xFFFF
107 /* The WMI hotkey event value is always the same. */
108 #define WMI_EVENT_VALUE_ATK 0xFF
110 #define WMI_EVENT_MASK 0xFFFF
112 #define FAN_CURVE_POINTS 8
113 #define FAN_CURVE_BUF_LEN 32
114 #define FAN_CURVE_DEV_CPU 0x00
115 #define FAN_CURVE_DEV_GPU 0x01
116 #define FAN_CURVE_DEV_MID 0x02
117 /* Mask to determine if setting temperature or percentage */
118 #define FAN_CURVE_PWM_MASK 0x04
120 /* Limits for tunables available on ASUS ROG laptops */
121 #define PPT_TOTAL_MIN 5
122 #define PPT_TOTAL_MAX 250
123 #define PPT_CPU_MIN 5
124 #define PPT_CPU_MAX 130
125 #define NVIDIA_BOOST_MIN 5
126 #define NVIDIA_BOOST_MAX 25
127 #define NVIDIA_TEMP_MIN 75
128 #define NVIDIA_TEMP_MAX 87
130 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
132 static int throttle_thermal_policy_write(struct asus_wmi *);
134 static bool ashs_present(void)
137 while (ashs_ids[i]) {
138 if (acpi_dev_found(ashs_ids[i++]))
147 u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
149 u32 arg4; /* Some ROG laptops require a full 5 input args */
154 * Struct that's used for all methods called via AGFN. Naming is
155 * identically to the AML code.
158 u16 mfun; /* probably "Multi-function" to be called */
159 u16 sfun; /* probably "Sub-function" to be called */
160 u16 len; /* size of the hole struct, including subfunction fields */
161 u8 stas; /* not used by now */
162 u8 err; /* zero on success */
165 /* struct used for calling fan read and write methods */
166 struct agfn_fan_args {
167 struct agfn_args agfn; /* common fields */
168 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
169 u32 speed; /* read: RPM/100 - write: 0-255 */
173 * <platform>/ - debugfs root directory
174 * dev_id - current dev_id
175 * ctrl_param - current ctrl_param
176 * method_id - current method_id
177 * devs - call DEVS(dev_id, ctrl_param) and print result
178 * dsts - call DSTS(dev_id) and print result
179 * call - call method_id(dev_id, ctrl_param) and print result
181 struct asus_wmi_debug {
189 struct asus_wmi *asus;
190 struct rfkill *rfkill;
196 FAN_TYPE_AGFN, /* deprecated on newer platforms */
197 FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */
200 struct fan_curve_data {
203 u8 temps[FAN_CURVE_POINTS];
204 u8 percents[FAN_CURVE_POINTS];
211 bool wmi_event_queue;
213 struct input_dev *inputdev;
214 struct backlight_device *backlight_device;
215 struct platform_device *platform_device;
217 struct led_classdev wlan_led;
219 struct led_classdev tpd_led;
221 struct led_classdev kbd_led;
223 struct led_classdev lightbar_led;
225 struct led_classdev micmute_led;
226 struct workqueue_struct *led_workqueue;
227 struct work_struct tpd_led_work;
228 struct work_struct wlan_led_work;
229 struct work_struct lightbar_led_work;
231 struct asus_rfkill wlan;
232 struct asus_rfkill bluetooth;
233 struct asus_rfkill wimax;
234 struct asus_rfkill wwan3g;
235 struct asus_rfkill gps;
236 struct asus_rfkill uwb;
238 int tablet_switch_event_code;
239 u32 tablet_switch_dev_id;
240 bool tablet_switch_inverted;
242 enum fan_type fan_type;
243 enum fan_type gpu_fan_type;
244 enum fan_type mid_fan_type;
246 int gpu_fan_pwm_mode;
247 int mid_fan_pwm_mode;
250 bool fan_boost_mode_available;
251 u8 fan_boost_mode_mask;
254 bool charge_mode_available;
255 bool egpu_enable_available;
256 bool egpu_connect_available;
257 bool dgpu_disable_available;
258 bool gpu_mux_mode_available;
260 /* Tunables provided by ASUS for gaming laptops */
261 bool ppt_pl2_sppt_available;
262 bool ppt_pl1_spl_available;
263 bool ppt_apu_sppt_available;
264 bool ppt_plat_sppt_available;
265 bool ppt_fppt_available;
266 bool nv_dyn_boost_available;
267 bool nv_temp_tgt_available;
269 bool kbd_rgb_mode_available;
270 bool kbd_rgb_state_available;
272 bool throttle_thermal_policy_available;
273 u8 throttle_thermal_policy_mode;
275 bool cpu_fan_curve_available;
276 bool gpu_fan_curve_available;
277 bool mid_fan_curve_available;
278 struct fan_curve_data custom_fan_curves[3];
280 struct platform_profile_handler platform_profile_handler;
281 bool platform_profile_support;
283 // The RSOC controls the maximum charging percentage.
284 bool battery_rsoc_available;
286 bool panel_overdrive_available;
287 bool mini_led_mode_available;
289 struct hotplug_slot hotplug_slot;
290 struct mutex hotplug_lock;
291 struct mutex wmi_lock;
292 struct workqueue_struct *hotplug_workqueue;
293 struct work_struct hotplug_work;
297 struct asus_wmi_debug debug;
299 struct asus_wmi_driver *driver;
302 /* WMI ************************************************************************/
304 static int asus_wmi_evaluate_method3(u32 method_id,
305 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
307 struct bios_args args = {
312 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
313 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
315 union acpi_object *obj;
318 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
321 if (ACPI_FAILURE(status))
324 obj = (union acpi_object *)output.pointer;
325 if (obj && obj->type == ACPI_TYPE_INTEGER)
326 tmp = (u32) obj->integer.value;
333 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
339 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
341 return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
343 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
345 static int asus_wmi_evaluate_method5(u32 method_id,
346 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4, u32 *retval)
348 struct bios_args args = {
355 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
356 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
358 union acpi_object *obj;
361 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
364 if (ACPI_FAILURE(status))
367 obj = (union acpi_object *)output.pointer;
368 if (obj && obj->type == ACPI_TYPE_INTEGER)
369 tmp = (u32) obj->integer.value;
376 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
383 * Returns as an error if the method output is not a buffer. Typically this
384 * means that the method called is unsupported.
386 static int asus_wmi_evaluate_method_buf(u32 method_id,
387 u32 arg0, u32 arg1, u8 *ret_buffer, size_t size)
389 struct bios_args args = {
394 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
395 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
397 union acpi_object *obj;
400 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
403 if (ACPI_FAILURE(status))
406 obj = (union acpi_object *)output.pointer;
409 case ACPI_TYPE_BUFFER:
410 if (obj->buffer.length > size) {
414 if (obj->buffer.length == 0) {
419 memcpy(ret_buffer, obj->buffer.pointer, obj->buffer.length);
421 case ACPI_TYPE_INTEGER:
422 err = (u32)obj->integer.value;
424 if (err == ASUS_WMI_UNSUPPORTED_METHOD)
427 * At least one method returns a 0 with no buffer if no arg
428 * is provided, such as ASUS_WMI_DEVID_CPU_FAN_CURVE
446 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
448 struct acpi_buffer input;
454 * Copy to dma capable address otherwise memory corruption occurs as
455 * bios has to be able to access it.
457 input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
458 input.length = args.length;
461 phys_addr = virt_to_phys(input.pointer);
463 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
464 phys_addr, 0, &retval);
466 memcpy(args.pointer, input.pointer, args.length);
468 kfree(input.pointer);
475 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
477 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
480 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
483 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
487 /* Helper for special devices with magic return codes */
488 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
489 u32 dev_id, u32 mask)
494 err = asus_wmi_get_devstate(asus, dev_id, &retval);
498 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
501 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
502 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
506 return retval & mask;
509 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
511 return asus_wmi_get_devstate_bits(asus, dev_id,
512 ASUS_WMI_DSTS_STATUS_BIT);
515 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
518 int status = asus_wmi_get_devstate(asus, dev_id, &retval);
520 return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
523 /* Input **********************************************************************/
524 static void asus_wmi_tablet_sw_report(struct asus_wmi *asus, bool value)
526 input_report_switch(asus->inputdev, SW_TABLET_MODE,
527 asus->tablet_switch_inverted ? !value : value);
528 input_sync(asus->inputdev);
531 static void asus_wmi_tablet_sw_init(struct asus_wmi *asus, u32 dev_id, int event_code)
533 struct device *dev = &asus->platform_device->dev;
536 result = asus_wmi_get_devstate_simple(asus, dev_id);
538 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
539 asus_wmi_tablet_sw_report(asus, result);
540 asus->tablet_switch_dev_id = dev_id;
541 asus->tablet_switch_event_code = event_code;
542 } else if (result == -ENODEV) {
543 dev_err(dev, "This device has tablet-mode-switch quirk but got ENODEV checking it. This is a bug.");
545 dev_err(dev, "Error checking for tablet-mode-switch: %d\n", result);
549 static int asus_wmi_input_init(struct asus_wmi *asus)
551 struct device *dev = &asus->platform_device->dev;
554 asus->inputdev = input_allocate_device();
558 asus->inputdev->name = asus->driver->input_name;
559 asus->inputdev->phys = asus->driver->input_phys;
560 asus->inputdev->id.bustype = BUS_HOST;
561 asus->inputdev->dev.parent = dev;
562 set_bit(EV_REP, asus->inputdev->evbit);
564 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
568 switch (asus->driver->quirks->tablet_switch_mode) {
569 case asus_wmi_no_tablet_switch:
571 case asus_wmi_kbd_dock_devid:
572 asus->tablet_switch_inverted = true;
573 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_KBD_DOCK, NOTIFY_KBD_DOCK_CHANGE);
575 case asus_wmi_lid_flip_devid:
576 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP, NOTIFY_LID_FLIP);
578 case asus_wmi_lid_flip_rog_devid:
579 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP_ROG, NOTIFY_LID_FLIP_ROG);
583 err = input_register_device(asus->inputdev);
590 input_free_device(asus->inputdev);
594 static void asus_wmi_input_exit(struct asus_wmi *asus)
597 input_unregister_device(asus->inputdev);
599 asus->inputdev = NULL;
602 /* Tablet mode ****************************************************************/
604 static void asus_wmi_tablet_mode_get_state(struct asus_wmi *asus)
608 if (!asus->tablet_switch_dev_id)
611 result = asus_wmi_get_devstate_simple(asus, asus->tablet_switch_dev_id);
613 asus_wmi_tablet_sw_report(asus, result);
616 /* Charging mode, 1=Barrel, 2=USB ******************************************/
617 static ssize_t charge_mode_show(struct device *dev,
618 struct device_attribute *attr, char *buf)
620 struct asus_wmi *asus = dev_get_drvdata(dev);
623 result = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CHARGE_MODE, &value);
627 return sysfs_emit(buf, "%d\n", value & 0xff);
630 static DEVICE_ATTR_RO(charge_mode);
632 /* dGPU ********************************************************************/
633 static ssize_t dgpu_disable_show(struct device *dev,
634 struct device_attribute *attr, char *buf)
636 struct asus_wmi *asus = dev_get_drvdata(dev);
639 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU);
643 return sysfs_emit(buf, "%d\n", result);
647 * A user may be required to store the value twice, typcial store first, then
648 * rescan PCI bus to activate power, then store a second time to save correctly.
649 * The reason for this is that an extra code path in the ACPI is enabled when
650 * the device and bus are powered.
652 static ssize_t dgpu_disable_store(struct device *dev,
653 struct device_attribute *attr,
654 const char *buf, size_t count)
659 struct asus_wmi *asus = dev_get_drvdata(dev);
661 result = kstrtou32(buf, 10, &disable);
668 if (asus->gpu_mux_mode_available) {
669 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPU_MUX);
671 /* An error here may signal greater failure of GPU handling */
673 if (!result && disable) {
675 pr_warn("Can not disable dGPU when the MUX is in dGPU mode: %d\n", err);
680 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, disable, &result);
682 pr_warn("Failed to set dgpu disable: %d\n", err);
687 pr_warn("Failed to set dgpu disable (result): 0x%x\n", result);
691 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
695 static DEVICE_ATTR_RW(dgpu_disable);
697 /* eGPU ********************************************************************/
698 static ssize_t egpu_enable_show(struct device *dev,
699 struct device_attribute *attr, char *buf)
701 struct asus_wmi *asus = dev_get_drvdata(dev);
704 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU);
708 return sysfs_emit(buf, "%d\n", result);
711 /* The ACPI call to enable the eGPU also disables the internal dGPU */
712 static ssize_t egpu_enable_store(struct device *dev,
713 struct device_attribute *attr,
714 const char *buf, size_t count)
719 struct asus_wmi *asus = dev_get_drvdata(dev);
721 err = kstrtou32(buf, 10, &enable);
728 err = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
730 pr_warn("Failed to get egpu connection status: %d\n", err);
734 if (asus->gpu_mux_mode_available) {
735 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPU_MUX);
737 /* An error here may signal greater failure of GPU handling */
738 pr_warn("Failed to get gpu mux status: %d\n", result);
741 if (!result && enable) {
743 pr_warn("Can not enable eGPU when the MUX is in dGPU mode: %d\n", err);
748 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, enable, &result);
750 pr_warn("Failed to set egpu state: %d\n", err);
755 pr_warn("Failed to set egpu state (retval): 0x%x\n", result);
759 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable");
763 static DEVICE_ATTR_RW(egpu_enable);
765 /* Is eGPU connected? *********************************************************/
766 static ssize_t egpu_connected_show(struct device *dev,
767 struct device_attribute *attr, char *buf)
769 struct asus_wmi *asus = dev_get_drvdata(dev);
772 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
776 return sysfs_emit(buf, "%d\n", result);
779 static DEVICE_ATTR_RO(egpu_connected);
781 /* gpu mux switch *************************************************************/
782 static ssize_t gpu_mux_mode_show(struct device *dev,
783 struct device_attribute *attr, char *buf)
785 struct asus_wmi *asus = dev_get_drvdata(dev);
788 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPU_MUX);
792 return sysfs_emit(buf, "%d\n", result);
795 static ssize_t gpu_mux_mode_store(struct device *dev,
796 struct device_attribute *attr,
797 const char *buf, size_t count)
799 struct asus_wmi *asus = dev_get_drvdata(dev);
803 err = kstrtou32(buf, 10, &optimus);
810 if (asus->dgpu_disable_available) {
811 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU);
813 /* An error here may signal greater failure of GPU handling */
815 if (result && !optimus) {
817 pr_warn("Can not switch MUX to dGPU mode when dGPU is disabled: %d\n", err);
822 if (asus->egpu_enable_available) {
823 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU);
825 /* An error here may signal greater failure of GPU handling */
827 if (result && !optimus) {
829 pr_warn("Can not switch MUX to dGPU mode when eGPU is enabled: %d\n", err);
834 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_MUX, optimus, &result);
836 dev_err(dev, "Failed to set GPU MUX mode: %d\n", err);
839 /* !1 is considered a fail by ASUS */
841 dev_warn(dev, "Failed to set GPU MUX mode (result): 0x%x\n", result);
845 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "gpu_mux_mode");
849 static DEVICE_ATTR_RW(gpu_mux_mode);
851 /* TUF Laptop Keyboard RGB Modes **********************************************/
852 static ssize_t kbd_rgb_mode_store(struct device *dev,
853 struct device_attribute *attr,
854 const char *buf, size_t count)
856 u32 cmd, mode, r, g, b, speed;
859 if (sscanf(buf, "%d %d %d %d %d %d", &cmd, &mode, &r, &g, &b, &speed) != 6)
862 /* B3 is set and B4 is save to BIOS */
874 /* These are the known usable modes across all TUF/ROG */
875 if (mode >= 12 || mode == 9)
892 err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS, ASUS_WMI_DEVID_TUF_RGB_MODE,
893 cmd | (mode << 8) | (r << 16) | (g << 24), b | (speed << 8), NULL);
899 static DEVICE_ATTR_WO(kbd_rgb_mode);
901 static ssize_t kbd_rgb_mode_index_show(struct device *device,
902 struct device_attribute *attr,
905 return sysfs_emit(buf, "%s\n", "cmd mode red green blue speed");
907 static DEVICE_ATTR_RO(kbd_rgb_mode_index);
909 static struct attribute *kbd_rgb_mode_attrs[] = {
910 &dev_attr_kbd_rgb_mode.attr,
911 &dev_attr_kbd_rgb_mode_index.attr,
915 static const struct attribute_group kbd_rgb_mode_group = {
916 .attrs = kbd_rgb_mode_attrs,
919 /* TUF Laptop Keyboard RGB State **********************************************/
920 static ssize_t kbd_rgb_state_store(struct device *dev,
921 struct device_attribute *attr,
922 const char *buf, size_t count)
924 u32 flags, cmd, boot, awake, sleep, keyboard;
927 if (sscanf(buf, "%d %d %d %d %d", &cmd, &boot, &awake, &sleep, &keyboard) != 5)
943 /* 0xbd is the required default arg0 for the method. Nothing happens otherwise */
944 err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS,
945 ASUS_WMI_DEVID_TUF_RGB_STATE, 0xbd | cmd << 8 | (flags << 16), 0, NULL);
951 static DEVICE_ATTR_WO(kbd_rgb_state);
953 static ssize_t kbd_rgb_state_index_show(struct device *device,
954 struct device_attribute *attr,
957 return sysfs_emit(buf, "%s\n", "cmd boot awake sleep keyboard");
959 static DEVICE_ATTR_RO(kbd_rgb_state_index);
961 static struct attribute *kbd_rgb_state_attrs[] = {
962 &dev_attr_kbd_rgb_state.attr,
963 &dev_attr_kbd_rgb_state_index.attr,
967 static const struct attribute_group kbd_rgb_state_group = {
968 .attrs = kbd_rgb_state_attrs,
971 static const struct attribute_group *kbd_rgb_mode_groups[] = {
977 /* Tunable: PPT: Intel=PL1, AMD=SPPT *****************************************/
978 static ssize_t ppt_pl2_sppt_store(struct device *dev,
979 struct device_attribute *attr,
980 const char *buf, size_t count)
985 struct asus_wmi *asus = dev_get_drvdata(dev);
987 result = kstrtou32(buf, 10, &value);
991 if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
994 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL2_SPPT, value, &result);
996 pr_warn("Failed to set ppt_pl2_sppt: %d\n", err);
1001 pr_warn("Failed to set ppt_pl2_sppt (result): 0x%x\n", result);
1005 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl2_sppt");
1009 static DEVICE_ATTR_WO(ppt_pl2_sppt);
1011 /* Tunable: PPT, Intel=PL1, AMD=SPL ******************************************/
1012 static ssize_t ppt_pl1_spl_store(struct device *dev,
1013 struct device_attribute *attr,
1014 const char *buf, size_t count)
1019 struct asus_wmi *asus = dev_get_drvdata(dev);
1021 result = kstrtou32(buf, 10, &value);
1025 if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
1028 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL1_SPL, value, &result);
1030 pr_warn("Failed to set ppt_pl1_spl: %d\n", err);
1035 pr_warn("Failed to set ppt_pl1_spl (result): 0x%x\n", result);
1039 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl1_spl");
1043 static DEVICE_ATTR_WO(ppt_pl1_spl);
1045 /* Tunable: PPT APU FPPT ******************************************************/
1046 static ssize_t ppt_fppt_store(struct device *dev,
1047 struct device_attribute *attr,
1048 const char *buf, size_t count)
1053 struct asus_wmi *asus = dev_get_drvdata(dev);
1055 result = kstrtou32(buf, 10, &value);
1059 if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
1062 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_FPPT, value, &result);
1064 pr_warn("Failed to set ppt_fppt: %d\n", err);
1069 pr_warn("Failed to set ppt_fppt (result): 0x%x\n", result);
1073 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_fpu_sppt");
1077 static DEVICE_ATTR_WO(ppt_fppt);
1079 /* Tunable: PPT APU SPPT *****************************************************/
1080 static ssize_t ppt_apu_sppt_store(struct device *dev,
1081 struct device_attribute *attr,
1082 const char *buf, size_t count)
1087 struct asus_wmi *asus = dev_get_drvdata(dev);
1089 result = kstrtou32(buf, 10, &value);
1093 if (value < PPT_CPU_MIN || value > PPT_CPU_MAX)
1096 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_APU_SPPT, value, &result);
1098 pr_warn("Failed to set ppt_apu_sppt: %d\n", err);
1103 pr_warn("Failed to set ppt_apu_sppt (result): 0x%x\n", result);
1107 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_apu_sppt");
1111 static DEVICE_ATTR_WO(ppt_apu_sppt);
1113 /* Tunable: PPT platform SPPT ************************************************/
1114 static ssize_t ppt_platform_sppt_store(struct device *dev,
1115 struct device_attribute *attr,
1116 const char *buf, size_t count)
1121 struct asus_wmi *asus = dev_get_drvdata(dev);
1123 result = kstrtou32(buf, 10, &value);
1127 if (value < PPT_CPU_MIN || value > PPT_CPU_MAX)
1130 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PLAT_SPPT, value, &result);
1132 pr_warn("Failed to set ppt_platform_sppt: %d\n", err);
1137 pr_warn("Failed to set ppt_platform_sppt (result): 0x%x\n", result);
1141 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_platform_sppt");
1145 static DEVICE_ATTR_WO(ppt_platform_sppt);
1147 /* Tunable: NVIDIA dynamic boost *********************************************/
1148 static ssize_t nv_dynamic_boost_store(struct device *dev,
1149 struct device_attribute *attr,
1150 const char *buf, size_t count)
1155 struct asus_wmi *asus = dev_get_drvdata(dev);
1157 result = kstrtou32(buf, 10, &value);
1161 if (value < NVIDIA_BOOST_MIN || value > NVIDIA_BOOST_MAX)
1164 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_DYN_BOOST, value, &result);
1166 pr_warn("Failed to set nv_dynamic_boost: %d\n", err);
1171 pr_warn("Failed to set nv_dynamic_boost (result): 0x%x\n", result);
1175 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_dynamic_boost");
1179 static DEVICE_ATTR_WO(nv_dynamic_boost);
1181 /* Tunable: NVIDIA temperature target ****************************************/
1182 static ssize_t nv_temp_target_store(struct device *dev,
1183 struct device_attribute *attr,
1184 const char *buf, size_t count)
1189 struct asus_wmi *asus = dev_get_drvdata(dev);
1191 result = kstrtou32(buf, 10, &value);
1195 if (value < NVIDIA_TEMP_MIN || value > NVIDIA_TEMP_MAX)
1198 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_THERM_TARGET, value, &result);
1200 pr_warn("Failed to set nv_temp_target: %d\n", err);
1205 pr_warn("Failed to set nv_temp_target (result): 0x%x\n", result);
1209 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_temp_target");
1213 static DEVICE_ATTR_WO(nv_temp_target);
1215 /* Battery ********************************************************************/
1217 /* The battery maximum charging percentage */
1218 static int charge_end_threshold;
1220 static ssize_t charge_control_end_threshold_store(struct device *dev,
1221 struct device_attribute *attr,
1222 const char *buf, size_t count)
1226 ret = kstrtouint(buf, 10, &value);
1230 if (value < 0 || value > 100)
1233 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
1240 /* There isn't any method in the DSDT to read the threshold, so we
1241 * save the threshold.
1243 charge_end_threshold = value;
1247 static ssize_t charge_control_end_threshold_show(struct device *device,
1248 struct device_attribute *attr,
1251 return sysfs_emit(buf, "%d\n", charge_end_threshold);
1254 static DEVICE_ATTR_RW(charge_control_end_threshold);
1256 static int asus_wmi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
1258 /* The WMI method does not provide a way to specific a battery, so we
1259 * just assume it is the first battery.
1260 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
1261 * battery is named BATT.
1263 if (strcmp(battery->desc->name, "BAT0") != 0 &&
1264 strcmp(battery->desc->name, "BAT1") != 0 &&
1265 strcmp(battery->desc->name, "BATC") != 0 &&
1266 strcmp(battery->desc->name, "BATT") != 0)
1269 if (device_create_file(&battery->dev,
1270 &dev_attr_charge_control_end_threshold))
1273 /* The charge threshold is only reset when the system is power cycled,
1274 * and we can't get the current threshold so let set it to 100% when
1275 * a battery is added.
1277 asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
1278 charge_end_threshold = 100;
1283 static int asus_wmi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
1285 device_remove_file(&battery->dev,
1286 &dev_attr_charge_control_end_threshold);
1290 static struct acpi_battery_hook battery_hook = {
1291 .add_battery = asus_wmi_battery_add,
1292 .remove_battery = asus_wmi_battery_remove,
1293 .name = "ASUS Battery Extension",
1296 static void asus_wmi_battery_init(struct asus_wmi *asus)
1298 asus->battery_rsoc_available = false;
1299 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
1300 asus->battery_rsoc_available = true;
1301 battery_hook_register(&battery_hook);
1305 static void asus_wmi_battery_exit(struct asus_wmi *asus)
1307 if (asus->battery_rsoc_available)
1308 battery_hook_unregister(&battery_hook);
1311 /* LEDs ***********************************************************************/
1314 * These functions actually update the LED's, and are called from a
1315 * workqueue. By doing this as separate work rather than when the LED
1316 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
1317 * potentially bad time, such as a timer interrupt.
1319 static void tpd_led_update(struct work_struct *work)
1322 struct asus_wmi *asus;
1324 asus = container_of(work, struct asus_wmi, tpd_led_work);
1326 ctrl_param = asus->tpd_led_wk;
1327 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
1330 static void tpd_led_set(struct led_classdev *led_cdev,
1331 enum led_brightness value)
1333 struct asus_wmi *asus;
1335 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
1337 asus->tpd_led_wk = !!value;
1338 queue_work(asus->led_workqueue, &asus->tpd_led_work);
1341 static int read_tpd_led_state(struct asus_wmi *asus)
1343 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
1346 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
1348 struct asus_wmi *asus;
1350 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
1352 return read_tpd_led_state(asus);
1355 static void kbd_led_update(struct asus_wmi *asus)
1359 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
1360 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
1363 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
1369 * bit 7: light on/off
1370 * bit 8-10: environment (0: dark, 1: normal, 2: light)
1371 * bit 17: status unknown
1373 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
1376 /* Unknown status is considered as off */
1377 if (retval == 0x8000)
1384 *level = retval & 0x7F;
1386 *env = (retval >> 8) & 0x7F;
1390 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
1392 struct asus_wmi *asus;
1395 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
1396 max_level = asus->kbd_led.max_brightness;
1398 asus->kbd_led_wk = clamp_val(value, 0, max_level);
1399 kbd_led_update(asus);
1402 static void kbd_led_set(struct led_classdev *led_cdev,
1403 enum led_brightness value)
1405 /* Prevent disabling keyboard backlight on module unregister */
1406 if (led_cdev->flags & LED_UNREGISTERING)
1409 do_kbd_led_set(led_cdev, value);
1412 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
1414 struct led_classdev *led_cdev = &asus->kbd_led;
1416 do_kbd_led_set(led_cdev, value);
1417 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
1420 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
1422 struct asus_wmi *asus;
1425 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
1427 retval = kbd_led_read(asus, &value, NULL);
1434 static int wlan_led_unknown_state(struct asus_wmi *asus)
1438 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
1440 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
1443 static void wlan_led_update(struct work_struct *work)
1446 struct asus_wmi *asus;
1448 asus = container_of(work, struct asus_wmi, wlan_led_work);
1450 ctrl_param = asus->wlan_led_wk;
1451 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
1454 static void wlan_led_set(struct led_classdev *led_cdev,
1455 enum led_brightness value)
1457 struct asus_wmi *asus;
1459 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
1461 asus->wlan_led_wk = !!value;
1462 queue_work(asus->led_workqueue, &asus->wlan_led_work);
1465 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
1467 struct asus_wmi *asus;
1470 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
1471 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
1473 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1476 static void lightbar_led_update(struct work_struct *work)
1478 struct asus_wmi *asus;
1481 asus = container_of(work, struct asus_wmi, lightbar_led_work);
1483 ctrl_param = asus->lightbar_led_wk;
1484 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
1487 static void lightbar_led_set(struct led_classdev *led_cdev,
1488 enum led_brightness value)
1490 struct asus_wmi *asus;
1492 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1494 asus->lightbar_led_wk = !!value;
1495 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
1498 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
1500 struct asus_wmi *asus;
1503 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1504 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
1506 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
1509 static int micmute_led_set(struct led_classdev *led_cdev,
1510 enum led_brightness brightness)
1512 int state = brightness != LED_OFF;
1515 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MICMUTE_LED, state, NULL);
1516 return err < 0 ? err : 0;
1519 static void asus_wmi_led_exit(struct asus_wmi *asus)
1521 led_classdev_unregister(&asus->kbd_led);
1522 led_classdev_unregister(&asus->tpd_led);
1523 led_classdev_unregister(&asus->wlan_led);
1524 led_classdev_unregister(&asus->lightbar_led);
1525 led_classdev_unregister(&asus->micmute_led);
1527 if (asus->led_workqueue)
1528 destroy_workqueue(asus->led_workqueue);
1531 static int asus_wmi_led_init(struct asus_wmi *asus)
1533 int rv = 0, num_rgb_groups = 0, led_val;
1535 if (asus->kbd_rgb_mode_available)
1536 kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_mode_group;
1537 if (asus->kbd_rgb_state_available)
1538 kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_state_group;
1540 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
1541 if (!asus->led_workqueue)
1544 if (read_tpd_led_state(asus) >= 0) {
1545 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
1547 asus->tpd_led.name = "asus::touchpad";
1548 asus->tpd_led.brightness_set = tpd_led_set;
1549 asus->tpd_led.brightness_get = tpd_led_get;
1550 asus->tpd_led.max_brightness = 1;
1552 rv = led_classdev_register(&asus->platform_device->dev,
1558 if (!kbd_led_read(asus, &led_val, NULL)) {
1559 asus->kbd_led_wk = led_val;
1560 asus->kbd_led.name = "asus::kbd_backlight";
1561 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
1562 asus->kbd_led.brightness_set = kbd_led_set;
1563 asus->kbd_led.brightness_get = kbd_led_get;
1564 asus->kbd_led.max_brightness = 3;
1566 if (num_rgb_groups != 0)
1567 asus->kbd_led.groups = kbd_rgb_mode_groups;
1569 rv = led_classdev_register(&asus->platform_device->dev,
1575 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
1576 && (asus->driver->quirks->wapf > 0)) {
1577 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
1579 asus->wlan_led.name = "asus::wlan";
1580 asus->wlan_led.brightness_set = wlan_led_set;
1581 if (!wlan_led_unknown_state(asus))
1582 asus->wlan_led.brightness_get = wlan_led_get;
1583 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
1584 asus->wlan_led.max_brightness = 1;
1585 asus->wlan_led.default_trigger = "asus-wlan";
1587 rv = led_classdev_register(&asus->platform_device->dev,
1593 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
1594 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
1596 asus->lightbar_led.name = "asus::lightbar";
1597 asus->lightbar_led.brightness_set = lightbar_led_set;
1598 asus->lightbar_led.brightness_get = lightbar_led_get;
1599 asus->lightbar_led.max_brightness = 1;
1601 rv = led_classdev_register(&asus->platform_device->dev,
1602 &asus->lightbar_led);
1605 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MICMUTE_LED)) {
1606 asus->micmute_led.name = "platform::micmute";
1607 asus->micmute_led.max_brightness = 1;
1608 asus->micmute_led.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
1609 asus->micmute_led.brightness_set_blocking = micmute_led_set;
1610 asus->micmute_led.default_trigger = "audio-micmute";
1612 rv = led_classdev_register(&asus->platform_device->dev,
1613 &asus->micmute_led);
1620 asus_wmi_led_exit(asus);
1625 /* RF *************************************************************************/
1628 * PCI hotplug (for wlan rfkill)
1630 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
1632 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1639 static void asus_rfkill_hotplug(struct asus_wmi *asus)
1641 struct pci_dev *dev;
1642 struct pci_bus *bus;
1647 mutex_lock(&asus->wmi_lock);
1648 blocked = asus_wlan_rfkill_blocked(asus);
1649 mutex_unlock(&asus->wmi_lock);
1651 mutex_lock(&asus->hotplug_lock);
1652 pci_lock_rescan_remove();
1654 if (asus->wlan.rfkill)
1655 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
1657 if (asus->hotplug_slot.ops) {
1658 bus = pci_find_bus(0, 1);
1660 pr_warn("Unable to find PCI bus 1?\n");
1664 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
1665 pr_err("Unable to read PCI config space?\n");
1668 absent = (l == 0xffffffff);
1670 if (blocked != absent) {
1671 pr_warn("BIOS says wireless lan is %s, but the pci device is %s\n",
1672 blocked ? "blocked" : "unblocked",
1673 absent ? "absent" : "present");
1674 pr_warn("skipped wireless hotplug as probably inappropriate for this model\n");
1679 dev = pci_get_slot(bus, 0);
1681 /* Device already present */
1685 dev = pci_scan_single_device(bus, 0);
1687 pci_bus_assign_resources(bus);
1688 pci_bus_add_device(dev);
1691 dev = pci_get_slot(bus, 0);
1693 pci_stop_and_remove_bus_device(dev);
1700 pci_unlock_rescan_remove();
1701 mutex_unlock(&asus->hotplug_lock);
1704 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
1706 struct asus_wmi *asus = data;
1708 if (event != ACPI_NOTIFY_BUS_CHECK)
1712 * We can't call directly asus_rfkill_hotplug because most
1713 * of the time WMBC is still being executed and not reetrant.
1714 * There is currently no way to tell ACPICA that we want this
1715 * method to be serialized, we schedule a asus_rfkill_hotplug
1716 * call later, in a safer context.
1718 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
1721 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
1726 status = acpi_get_handle(NULL, node, &handle);
1727 if (ACPI_FAILURE(status))
1730 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1731 asus_rfkill_notify, asus);
1732 if (ACPI_FAILURE(status))
1733 pr_warn("Failed to register notify on %s\n", node);
1738 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
1740 acpi_status status = AE_OK;
1743 status = acpi_get_handle(NULL, node, &handle);
1744 if (ACPI_FAILURE(status))
1747 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1748 asus_rfkill_notify);
1749 if (ACPI_FAILURE(status))
1750 pr_err("Error removing rfkill notify handler %s\n", node);
1753 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
1756 struct asus_wmi *asus = container_of(hotplug_slot,
1757 struct asus_wmi, hotplug_slot);
1758 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1767 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
1768 .get_adapter_status = asus_get_adapter_status,
1769 .get_power_status = asus_get_adapter_status,
1772 static void asus_hotplug_work(struct work_struct *work)
1774 struct asus_wmi *asus;
1776 asus = container_of(work, struct asus_wmi, hotplug_work);
1777 asus_rfkill_hotplug(asus);
1780 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
1783 struct pci_bus *bus = pci_find_bus(0, 1);
1786 pr_err("Unable to find wifi PCI bus\n");
1790 asus->hotplug_workqueue =
1791 create_singlethread_workqueue("hotplug_workqueue");
1792 if (!asus->hotplug_workqueue)
1793 goto error_workqueue;
1795 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
1797 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
1799 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
1801 pr_err("Unable to register hotplug slot - %d\n", ret);
1802 goto error_register;
1808 asus->hotplug_slot.ops = NULL;
1809 destroy_workqueue(asus->hotplug_workqueue);
1817 static int asus_rfkill_set(void *data, bool blocked)
1819 struct asus_rfkill *priv = data;
1820 u32 ctrl_param = !blocked;
1821 u32 dev_id = priv->dev_id;
1824 * If the user bit is set, BIOS can't set and record the wlan status,
1825 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
1826 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
1827 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
1828 * while setting the wlan status through WMI.
1829 * This is also the behavior that windows app will do.
1831 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1832 priv->asus->driver->wlan_ctrl_by_user)
1833 dev_id = ASUS_WMI_DEVID_WLAN_LED;
1835 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
1838 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
1840 struct asus_rfkill *priv = data;
1843 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
1848 rfkill_set_sw_state(priv->rfkill, !result);
1851 static int asus_rfkill_wlan_set(void *data, bool blocked)
1853 struct asus_rfkill *priv = data;
1854 struct asus_wmi *asus = priv->asus;
1858 * This handler is enabled only if hotplug is enabled.
1859 * In this case, the asus_wmi_set_devstate() will
1860 * trigger a wmi notification and we need to wait
1861 * this call to finish before being able to call
1864 mutex_lock(&asus->wmi_lock);
1865 ret = asus_rfkill_set(data, blocked);
1866 mutex_unlock(&asus->wmi_lock);
1870 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1871 .set_block = asus_rfkill_wlan_set,
1872 .query = asus_rfkill_query,
1875 static const struct rfkill_ops asus_rfkill_ops = {
1876 .set_block = asus_rfkill_set,
1877 .query = asus_rfkill_query,
1880 static int asus_new_rfkill(struct asus_wmi *asus,
1881 struct asus_rfkill *arfkill,
1882 const char *name, enum rfkill_type type, int dev_id)
1884 int result = asus_wmi_get_devstate_simple(asus, dev_id);
1885 struct rfkill **rfkill = &arfkill->rfkill;
1890 arfkill->dev_id = dev_id;
1891 arfkill->asus = asus;
1893 if (dev_id == ASUS_WMI_DEVID_WLAN &&
1894 asus->driver->quirks->hotplug_wireless)
1895 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1896 &asus_rfkill_wlan_ops, arfkill);
1898 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1899 &asus_rfkill_ops, arfkill);
1904 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1905 (asus->driver->quirks->wapf > 0))
1906 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1908 rfkill_init_sw_state(*rfkill, !result);
1909 result = rfkill_register(*rfkill);
1911 rfkill_destroy(*rfkill);
1918 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1920 if (asus->driver->wlan_ctrl_by_user && ashs_present())
1923 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1924 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1925 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1926 if (asus->wlan.rfkill) {
1927 rfkill_unregister(asus->wlan.rfkill);
1928 rfkill_destroy(asus->wlan.rfkill);
1929 asus->wlan.rfkill = NULL;
1932 * Refresh pci hotplug in case the rfkill state was changed after
1933 * asus_unregister_rfkill_notifier()
1935 asus_rfkill_hotplug(asus);
1936 if (asus->hotplug_slot.ops)
1937 pci_hp_deregister(&asus->hotplug_slot);
1938 if (asus->hotplug_workqueue)
1939 destroy_workqueue(asus->hotplug_workqueue);
1941 if (asus->bluetooth.rfkill) {
1942 rfkill_unregister(asus->bluetooth.rfkill);
1943 rfkill_destroy(asus->bluetooth.rfkill);
1944 asus->bluetooth.rfkill = NULL;
1946 if (asus->wimax.rfkill) {
1947 rfkill_unregister(asus->wimax.rfkill);
1948 rfkill_destroy(asus->wimax.rfkill);
1949 asus->wimax.rfkill = NULL;
1951 if (asus->wwan3g.rfkill) {
1952 rfkill_unregister(asus->wwan3g.rfkill);
1953 rfkill_destroy(asus->wwan3g.rfkill);
1954 asus->wwan3g.rfkill = NULL;
1956 if (asus->gps.rfkill) {
1957 rfkill_unregister(asus->gps.rfkill);
1958 rfkill_destroy(asus->gps.rfkill);
1959 asus->gps.rfkill = NULL;
1961 if (asus->uwb.rfkill) {
1962 rfkill_unregister(asus->uwb.rfkill);
1963 rfkill_destroy(asus->uwb.rfkill);
1964 asus->uwb.rfkill = NULL;
1968 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1972 mutex_init(&asus->hotplug_lock);
1973 mutex_init(&asus->wmi_lock);
1975 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1976 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1978 if (result && result != -ENODEV)
1981 result = asus_new_rfkill(asus, &asus->bluetooth,
1982 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1983 ASUS_WMI_DEVID_BLUETOOTH);
1985 if (result && result != -ENODEV)
1988 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1989 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1991 if (result && result != -ENODEV)
1994 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1995 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1997 if (result && result != -ENODEV)
2000 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
2001 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
2003 if (result && result != -ENODEV)
2006 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
2007 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
2009 if (result && result != -ENODEV)
2012 if (!asus->driver->quirks->hotplug_wireless)
2015 result = asus_setup_pci_hotplug(asus);
2017 * If we get -EBUSY then something else is handling the PCI hotplug -
2018 * don't fail in this case
2020 if (result == -EBUSY)
2023 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
2024 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
2025 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
2027 * Refresh pci hotplug in case the rfkill state was changed during
2030 asus_rfkill_hotplug(asus);
2033 if (result && result != -ENODEV)
2034 asus_wmi_rfkill_exit(asus);
2036 if (result == -ENODEV)
2042 /* Panel Overdrive ************************************************************/
2043 static ssize_t panel_od_show(struct device *dev,
2044 struct device_attribute *attr, char *buf)
2046 struct asus_wmi *asus = dev_get_drvdata(dev);
2049 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_PANEL_OD);
2053 return sysfs_emit(buf, "%d\n", result);
2056 static ssize_t panel_od_store(struct device *dev,
2057 struct device_attribute *attr,
2058 const char *buf, size_t count)
2063 struct asus_wmi *asus = dev_get_drvdata(dev);
2065 result = kstrtou32(buf, 10, &overdrive);
2072 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, overdrive, &result);
2075 pr_warn("Failed to set panel overdrive: %d\n", err);
2080 pr_warn("Failed to set panel overdrive (result): 0x%x\n", result);
2084 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
2088 static DEVICE_ATTR_RW(panel_od);
2090 /* Mini-LED mode **************************************************************/
2091 static ssize_t mini_led_mode_show(struct device *dev,
2092 struct device_attribute *attr, char *buf)
2094 struct asus_wmi *asus = dev_get_drvdata(dev);
2097 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_MINI_LED_MODE);
2101 return sysfs_emit(buf, "%d\n", result);
2104 static ssize_t mini_led_mode_store(struct device *dev,
2105 struct device_attribute *attr,
2106 const char *buf, size_t count)
2111 struct asus_wmi *asus = dev_get_drvdata(dev);
2113 result = kstrtou32(buf, 10, &mode);
2120 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MINI_LED_MODE, mode, &result);
2123 pr_warn("Failed to set mini-LED: %d\n", err);
2128 pr_warn("Failed to set mini-LED mode (result): 0x%x\n", result);
2132 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "mini_led_mode");
2136 static DEVICE_ATTR_RW(mini_led_mode);
2138 /* Quirks *********************************************************************/
2140 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
2142 struct pci_dev *xhci_pdev;
2143 u32 orig_ports_available;
2144 u32 ports_available = asus->driver->quirks->xusb2pr;
2146 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
2147 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
2153 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
2154 &orig_ports_available);
2156 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
2157 cpu_to_le32(ports_available));
2159 pci_dev_put(xhci_pdev);
2161 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
2162 orig_ports_available, ports_available);
2166 * Some devices dont support or have borcken get_als method
2167 * but still support set method.
2169 static void asus_wmi_set_als(void)
2171 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
2174 /* Hwmon device ***************************************************************/
2176 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
2179 struct agfn_fan_args args = {
2180 .agfn.len = sizeof(args),
2181 .agfn.mfun = ASUS_FAN_MFUN,
2182 .agfn.sfun = ASUS_FAN_SFUN_READ,
2186 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2192 status = asus_wmi_evaluate_method_agfn(input);
2194 if (status || args.agfn.err)
2198 *speed = args.speed;
2203 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
2206 struct agfn_fan_args args = {
2207 .agfn.len = sizeof(args),
2208 .agfn.mfun = ASUS_FAN_MFUN,
2209 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
2211 .speed = speed ? *speed : 0,
2213 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2216 /* 1: for setting 1st fan's speed 0: setting auto mode */
2217 if (fan != 1 && fan != 0)
2220 status = asus_wmi_evaluate_method_agfn(input);
2222 if (status || args.agfn.err)
2225 if (speed && fan == 1)
2226 asus->agfn_pwm = *speed;
2232 * Check if we can read the speed of one fan. If true we assume we can also
2235 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
2241 status = asus_agfn_fan_speed_read(asus, 1, &speed);
2245 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
2250 * We need to find a better way, probably using sfun,
2252 * Currently we disable it if:
2253 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
2254 * - reverved bits are non-zero
2255 * - sfun and presence bit are not set
2257 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
2258 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
2261 static int asus_fan_set_auto(struct asus_wmi *asus)
2266 switch (asus->fan_type) {
2267 case FAN_TYPE_SPEC83:
2268 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
2278 status = asus_agfn_fan_speed_write(asus, 0, NULL);
2288 * Modern models like the G713 also have GPU fan control (this is not AGFN)
2290 if (asus->gpu_fan_type == FAN_TYPE_SPEC83) {
2291 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
2303 static ssize_t pwm1_show(struct device *dev,
2304 struct device_attribute *attr,
2307 struct asus_wmi *asus = dev_get_drvdata(dev);
2311 /* If we already set a value then just return it */
2312 if (asus->agfn_pwm >= 0)
2313 return sprintf(buf, "%d\n", asus->agfn_pwm);
2316 * If we haven't set already set a value through the AGFN interface,
2317 * we read a current value through the (now-deprecated) FAN_CTRL device.
2319 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
2325 if (value == 1) /* Low Speed */
2327 else if (value == 2)
2329 else if (value == 3)
2332 pr_err("Unknown fan speed %#x\n", value);
2336 return sysfs_emit(buf, "%d\n", value);
2339 static ssize_t pwm1_store(struct device *dev,
2340 struct device_attribute *attr,
2341 const char *buf, size_t count) {
2342 struct asus_wmi *asus = dev_get_drvdata(dev);
2347 ret = kstrtouint(buf, 10, &value);
2351 value = clamp(value, 0, 255);
2353 state = asus_agfn_fan_speed_write(asus, 1, &value);
2355 pr_warn("Setting fan speed failed: %d\n", state);
2357 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
2362 static ssize_t fan1_input_show(struct device *dev,
2363 struct device_attribute *attr,
2366 struct asus_wmi *asus = dev_get_drvdata(dev);
2370 switch (asus->fan_type) {
2371 case FAN_TYPE_SPEC83:
2372 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
2381 /* no speed readable on manual mode */
2382 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
2385 ret = asus_agfn_fan_speed_read(asus, 1, &value);
2387 pr_warn("reading fan speed failed: %d\n", ret);
2396 return sysfs_emit(buf, "%d\n", value < 0 ? -1 : value * 100);
2399 static ssize_t pwm1_enable_show(struct device *dev,
2400 struct device_attribute *attr,
2403 struct asus_wmi *asus = dev_get_drvdata(dev);
2406 * Just read back the cached pwm mode.
2408 * For the CPU_FAN device, the spec indicates that we should be
2409 * able to read the device status and consult bit 19 to see if we
2410 * are in Full On or Automatic mode. However, this does not work
2411 * in practice on X532FL at least (the bit is always 0) and there's
2412 * also nothing in the DSDT to indicate that this behaviour exists.
2414 return sysfs_emit(buf, "%d\n", asus->fan_pwm_mode);
2417 static ssize_t pwm1_enable_store(struct device *dev,
2418 struct device_attribute *attr,
2419 const char *buf, size_t count)
2421 struct asus_wmi *asus = dev_get_drvdata(dev);
2428 ret = kstrtouint(buf, 10, &state);
2432 if (asus->fan_type == FAN_TYPE_SPEC83) {
2433 switch (state) { /* standard documented hwmon values */
2434 case ASUS_FAN_CTRL_FULLSPEED:
2437 case ASUS_FAN_CTRL_AUTO:
2444 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
2451 } else if (asus->fan_type == FAN_TYPE_AGFN) {
2453 case ASUS_FAN_CTRL_MANUAL:
2456 case ASUS_FAN_CTRL_AUTO:
2457 status = asus_fan_set_auto(asus);
2467 asus->fan_pwm_mode = state;
2469 /* Must set to disabled if mode is toggled */
2470 if (asus->cpu_fan_curve_available)
2471 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
2472 if (asus->gpu_fan_curve_available)
2473 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
2474 if (asus->mid_fan_curve_available)
2475 asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false;
2480 static ssize_t fan1_label_show(struct device *dev,
2481 struct device_attribute *attr,
2484 return sysfs_emit(buf, "%s\n", ASUS_FAN_DESC);
2487 static ssize_t asus_hwmon_temp1(struct device *dev,
2488 struct device_attribute *attr,
2491 struct asus_wmi *asus = dev_get_drvdata(dev);
2495 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
2499 return sprintf(buf, "%ld\n",
2500 deci_kelvin_to_millicelsius(value & 0xFFFF));
2503 /* GPU fan on modern ROG laptops */
2504 static ssize_t fan2_input_show(struct device *dev,
2505 struct device_attribute *attr,
2508 struct asus_wmi *asus = dev_get_drvdata(dev);
2512 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL, &value);
2518 return sysfs_emit(buf, "%d\n", value * 100);
2521 static ssize_t fan2_label_show(struct device *dev,
2522 struct device_attribute *attr,
2525 return sysfs_emit(buf, "%s\n", ASUS_GPU_FAN_DESC);
2528 /* Middle/Center fan on modern ROG laptops */
2529 static ssize_t fan3_input_show(struct device *dev,
2530 struct device_attribute *attr,
2533 struct asus_wmi *asus = dev_get_drvdata(dev);
2537 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_MID_FAN_CTRL, &value);
2543 return sysfs_emit(buf, "%d\n", value * 100);
2546 static ssize_t fan3_label_show(struct device *dev,
2547 struct device_attribute *attr,
2550 return sysfs_emit(buf, "%s\n", ASUS_MID_FAN_DESC);
2553 static ssize_t pwm2_enable_show(struct device *dev,
2554 struct device_attribute *attr,
2557 struct asus_wmi *asus = dev_get_drvdata(dev);
2559 return sysfs_emit(buf, "%d\n", asus->gpu_fan_pwm_mode);
2562 static ssize_t pwm2_enable_store(struct device *dev,
2563 struct device_attribute *attr,
2564 const char *buf, size_t count)
2566 struct asus_wmi *asus = dev_get_drvdata(dev);
2572 ret = kstrtouint(buf, 10, &state);
2576 switch (state) { /* standard documented hwmon values */
2577 case ASUS_FAN_CTRL_FULLSPEED:
2580 case ASUS_FAN_CTRL_AUTO:
2587 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
2595 asus->gpu_fan_pwm_mode = state;
2599 static ssize_t pwm3_enable_show(struct device *dev,
2600 struct device_attribute *attr,
2603 struct asus_wmi *asus = dev_get_drvdata(dev);
2605 return sysfs_emit(buf, "%d\n", asus->mid_fan_pwm_mode);
2608 static ssize_t pwm3_enable_store(struct device *dev,
2609 struct device_attribute *attr,
2610 const char *buf, size_t count)
2612 struct asus_wmi *asus = dev_get_drvdata(dev);
2618 ret = kstrtouint(buf, 10, &state);
2622 switch (state) { /* standard documented hwmon values */
2623 case ASUS_FAN_CTRL_FULLSPEED:
2626 case ASUS_FAN_CTRL_AUTO:
2633 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_MID_FAN_CTRL,
2641 asus->mid_fan_pwm_mode = state;
2646 static DEVICE_ATTR_RW(pwm1);
2647 static DEVICE_ATTR_RW(pwm1_enable);
2648 static DEVICE_ATTR_RO(fan1_input);
2649 static DEVICE_ATTR_RO(fan1_label);
2650 /* Fan2 - GPU fan */
2651 static DEVICE_ATTR_RW(pwm2_enable);
2652 static DEVICE_ATTR_RO(fan2_input);
2653 static DEVICE_ATTR_RO(fan2_label);
2654 /* Fan3 - Middle/center fan */
2655 static DEVICE_ATTR_RW(pwm3_enable);
2656 static DEVICE_ATTR_RO(fan3_input);
2657 static DEVICE_ATTR_RO(fan3_label);
2660 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
2662 static struct attribute *hwmon_attributes[] = {
2663 &dev_attr_pwm1.attr,
2664 &dev_attr_pwm1_enable.attr,
2665 &dev_attr_pwm2_enable.attr,
2666 &dev_attr_pwm3_enable.attr,
2667 &dev_attr_fan1_input.attr,
2668 &dev_attr_fan1_label.attr,
2669 &dev_attr_fan2_input.attr,
2670 &dev_attr_fan2_label.attr,
2671 &dev_attr_fan3_input.attr,
2672 &dev_attr_fan3_label.attr,
2674 &dev_attr_temp1_input.attr,
2678 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
2679 struct attribute *attr, int idx)
2681 struct device *dev = kobj_to_dev(kobj);
2682 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2683 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
2685 if (attr == &dev_attr_pwm1.attr) {
2686 if (asus->fan_type != FAN_TYPE_AGFN)
2688 } else if (attr == &dev_attr_fan1_input.attr
2689 || attr == &dev_attr_fan1_label.attr
2690 || attr == &dev_attr_pwm1_enable.attr) {
2691 if (asus->fan_type == FAN_TYPE_NONE)
2693 } else if (attr == &dev_attr_fan2_input.attr
2694 || attr == &dev_attr_fan2_label.attr
2695 || attr == &dev_attr_pwm2_enable.attr) {
2696 if (asus->gpu_fan_type == FAN_TYPE_NONE)
2698 } else if (attr == &dev_attr_fan3_input.attr
2699 || attr == &dev_attr_fan3_label.attr
2700 || attr == &dev_attr_pwm3_enable.attr) {
2701 if (asus->mid_fan_type == FAN_TYPE_NONE)
2703 } else if (attr == &dev_attr_temp1_input.attr) {
2704 int err = asus_wmi_get_devstate(asus,
2705 ASUS_WMI_DEVID_THERMAL_CTRL,
2709 return 0; /* can't return negative here */
2712 * If the temperature value in deci-Kelvin is near the absolute
2713 * zero temperature, something is clearly wrong
2715 if (value == 0 || value == 1)
2722 static const struct attribute_group hwmon_attribute_group = {
2723 .is_visible = asus_hwmon_sysfs_is_visible,
2724 .attrs = hwmon_attributes
2726 __ATTRIBUTE_GROUPS(hwmon_attribute);
2728 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
2730 struct device *dev = &asus->platform_device->dev;
2731 struct device *hwmon;
2733 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
2734 hwmon_attribute_groups);
2736 if (IS_ERR(hwmon)) {
2737 pr_err("Could not register asus hwmon device\n");
2738 return PTR_ERR(hwmon);
2743 static int asus_wmi_fan_init(struct asus_wmi *asus)
2745 asus->gpu_fan_type = FAN_TYPE_NONE;
2746 asus->mid_fan_type = FAN_TYPE_NONE;
2747 asus->fan_type = FAN_TYPE_NONE;
2748 asus->agfn_pwm = -1;
2750 if (asus->driver->quirks->wmi_ignore_fan)
2751 asus->fan_type = FAN_TYPE_NONE;
2752 else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
2753 asus->fan_type = FAN_TYPE_SPEC83;
2754 else if (asus_wmi_has_agfn_fan(asus))
2755 asus->fan_type = FAN_TYPE_AGFN;
2757 /* Modern models like G713 also have GPU fan control */
2758 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL))
2759 asus->gpu_fan_type = FAN_TYPE_SPEC83;
2761 /* Some models also have a center/middle fan */
2762 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MID_FAN_CTRL))
2763 asus->mid_fan_type = FAN_TYPE_SPEC83;
2765 if (asus->fan_type == FAN_TYPE_NONE)
2768 asus_fan_set_auto(asus);
2769 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
2773 /* Fan mode *******************************************************************/
2775 static int fan_boost_mode_check_present(struct asus_wmi *asus)
2780 asus->fan_boost_mode_available = false;
2782 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
2791 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2792 (result & ASUS_FAN_BOOST_MODES_MASK)) {
2793 asus->fan_boost_mode_available = true;
2794 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
2800 static int fan_boost_mode_write(struct asus_wmi *asus)
2806 value = asus->fan_boost_mode;
2808 pr_info("Set fan boost mode: %u\n", value);
2809 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
2812 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2816 pr_warn("Failed to set fan boost mode: %d\n", err);
2821 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
2829 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
2831 u8 mask = asus->fan_boost_mode_mask;
2833 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
2834 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
2835 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
2836 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2837 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2838 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2839 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2840 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2842 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2844 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2847 return fan_boost_mode_write(asus);
2850 static ssize_t fan_boost_mode_show(struct device *dev,
2851 struct device_attribute *attr, char *buf)
2853 struct asus_wmi *asus = dev_get_drvdata(dev);
2855 return sysfs_emit(buf, "%d\n", asus->fan_boost_mode);
2858 static ssize_t fan_boost_mode_store(struct device *dev,
2859 struct device_attribute *attr,
2860 const char *buf, size_t count)
2862 struct asus_wmi *asus = dev_get_drvdata(dev);
2863 u8 mask = asus->fan_boost_mode_mask;
2867 result = kstrtou8(buf, 10, &new_mode);
2869 pr_warn("Trying to store invalid value\n");
2873 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2874 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
2876 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
2877 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
2879 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
2883 asus->fan_boost_mode = new_mode;
2884 fan_boost_mode_write(asus);
2889 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
2890 static DEVICE_ATTR_RW(fan_boost_mode);
2892 /* Custom fan curves **********************************************************/
2894 static void fan_curve_copy_from_buf(struct fan_curve_data *data, u8 *buf)
2898 for (i = 0; i < FAN_CURVE_POINTS; i++) {
2899 data->temps[i] = buf[i];
2902 for (i = 0; i < FAN_CURVE_POINTS; i++) {
2904 255 * buf[i + FAN_CURVE_POINTS] / 100;
2908 static int fan_curve_get_factory_default(struct asus_wmi *asus, u32 fan_dev)
2910 struct fan_curve_data *curves;
2911 u8 buf[FAN_CURVE_BUF_LEN];
2915 if (asus->throttle_thermal_policy_available)
2916 mode = asus->throttle_thermal_policy_mode;
2917 /* DEVID_<C/G>PU_FAN_CURVE is switched for OVERBOOST vs SILENT */
2923 err = asus_wmi_evaluate_method_buf(asus->dsts_id, fan_dev, mode, buf,
2926 pr_warn("%s (0x%08x) failed: %d\n", __func__, fan_dev, err);
2930 fan_idx = FAN_CURVE_DEV_CPU;
2931 if (fan_dev == ASUS_WMI_DEVID_GPU_FAN_CURVE)
2932 fan_idx = FAN_CURVE_DEV_GPU;
2934 if (fan_dev == ASUS_WMI_DEVID_MID_FAN_CURVE)
2935 fan_idx = FAN_CURVE_DEV_MID;
2937 curves = &asus->custom_fan_curves[fan_idx];
2938 curves->device_id = fan_dev;
2940 fan_curve_copy_from_buf(curves, buf);
2944 /* Check if capability exists, and populate defaults */
2945 static int fan_curve_check_present(struct asus_wmi *asus, bool *available,
2952 if (asus->fan_type == FAN_TYPE_NONE)
2955 err = fan_curve_get_factory_default(asus, fan_dev);
2964 /* Determine which fan the attribute is for if SENSOR_ATTR */
2965 static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus,
2966 struct device_attribute *attr)
2968 int index = to_sensor_dev_attr(attr)->index;
2970 return &asus->custom_fan_curves[index];
2973 /* Determine which fan the attribute is for if SENSOR_ATTR_2 */
2974 static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus,
2975 struct device_attribute *attr)
2977 int nr = to_sensor_dev_attr_2(attr)->nr;
2979 return &asus->custom_fan_curves[nr & ~FAN_CURVE_PWM_MASK];
2982 static ssize_t fan_curve_show(struct device *dev,
2983 struct device_attribute *attr, char *buf)
2985 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2986 struct asus_wmi *asus = dev_get_drvdata(dev);
2987 struct fan_curve_data *data;
2988 int value, pwm, index;
2990 data = fan_curve_attr_2_select(asus, attr);
2991 pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
2992 index = dev_attr->index;
2995 value = data->percents[index];
2997 value = data->temps[index];
2999 return sysfs_emit(buf, "%d\n", value);
3003 * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE.
3005 static int fan_curve_write(struct asus_wmi *asus,
3006 struct fan_curve_data *data)
3008 u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0;
3009 u8 *percents = data->percents;
3010 u8 *temps = data->temps;
3011 int ret, i, shift = 0;
3016 for (i = 0; i < FAN_CURVE_POINTS / 2; i++) {
3017 arg1 += (temps[i]) << shift;
3018 arg2 += (temps[i + 4]) << shift;
3019 /* Scale to percentage for device */
3020 arg3 += (100 * percents[i] / 255) << shift;
3021 arg4 += (100 * percents[i + 4] / 255) << shift;
3025 return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS,
3027 arg1, arg2, arg3, arg4, &ret);
3030 static ssize_t fan_curve_store(struct device *dev,
3031 struct device_attribute *attr, const char *buf,
3034 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
3035 struct asus_wmi *asus = dev_get_drvdata(dev);
3036 struct fan_curve_data *data;
3037 int err, pwm, index;
3040 data = fan_curve_attr_2_select(asus, attr);
3041 pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
3042 index = dev_attr->index;
3044 err = kstrtou8(buf, 10, &value);
3049 data->percents[index] = value;
3051 data->temps[index] = value;
3054 * Mark as disabled so the user has to explicitly enable to apply a
3055 * changed fan curve. This prevents potential lockups from writing out
3056 * many changes as one-write-per-change.
3058 data->enabled = false;
3063 static ssize_t fan_curve_enable_show(struct device *dev,
3064 struct device_attribute *attr, char *buf)
3066 struct asus_wmi *asus = dev_get_drvdata(dev);
3067 struct fan_curve_data *data;
3070 data = fan_curve_attr_select(asus, attr);
3075 return sysfs_emit(buf, "%d\n", out);
3078 static ssize_t fan_curve_enable_store(struct device *dev,
3079 struct device_attribute *attr,
3080 const char *buf, size_t count)
3082 struct asus_wmi *asus = dev_get_drvdata(dev);
3083 struct fan_curve_data *data;
3086 data = fan_curve_attr_select(asus, attr);
3088 err = kstrtoint(buf, 10, &value);
3094 data->enabled = true;
3097 data->enabled = false;
3100 * Auto + reset the fan curve data to defaults. Make it an explicit
3101 * option so that users don't accidentally overwrite a set fan curve.
3104 err = fan_curve_get_factory_default(asus, data->device_id);
3107 data->enabled = false;
3113 if (data->enabled) {
3114 err = fan_curve_write(asus, data);
3119 * For machines with throttle this is the only way to reset fans
3120 * to default mode of operation (does not erase curve data).
3122 if (asus->throttle_thermal_policy_available) {
3123 err = throttle_thermal_policy_write(asus);
3126 /* Similar is true for laptops with this fan */
3127 } else if (asus->fan_type == FAN_TYPE_SPEC83) {
3128 err = asus_fan_set_auto(asus);
3132 /* Safeguard against fautly ACPI tables */
3133 err = fan_curve_get_factory_default(asus, data->device_id);
3136 err = fan_curve_write(asus, data);
3145 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU);
3146 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve,
3147 FAN_CURVE_DEV_CPU, 0);
3148 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve,
3149 FAN_CURVE_DEV_CPU, 1);
3150 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve,
3151 FAN_CURVE_DEV_CPU, 2);
3152 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve,
3153 FAN_CURVE_DEV_CPU, 3);
3154 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve,
3155 FAN_CURVE_DEV_CPU, 4);
3156 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve,
3157 FAN_CURVE_DEV_CPU, 5);
3158 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve,
3159 FAN_CURVE_DEV_CPU, 6);
3160 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve,
3161 FAN_CURVE_DEV_CPU, 7);
3163 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve,
3164 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0);
3165 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve,
3166 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1);
3167 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve,
3168 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2);
3169 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve,
3170 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3);
3171 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve,
3172 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4);
3173 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve,
3174 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5);
3175 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve,
3176 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6);
3177 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve,
3178 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7);
3181 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU);
3182 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve,
3183 FAN_CURVE_DEV_GPU, 0);
3184 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve,
3185 FAN_CURVE_DEV_GPU, 1);
3186 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve,
3187 FAN_CURVE_DEV_GPU, 2);
3188 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve,
3189 FAN_CURVE_DEV_GPU, 3);
3190 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve,
3191 FAN_CURVE_DEV_GPU, 4);
3192 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve,
3193 FAN_CURVE_DEV_GPU, 5);
3194 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve,
3195 FAN_CURVE_DEV_GPU, 6);
3196 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve,
3197 FAN_CURVE_DEV_GPU, 7);
3199 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve,
3200 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0);
3201 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve,
3202 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1);
3203 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve,
3204 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2);
3205 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve,
3206 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3);
3207 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve,
3208 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4);
3209 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve,
3210 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5);
3211 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve,
3212 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6);
3213 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve,
3214 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7);
3217 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, fan_curve_enable, FAN_CURVE_DEV_MID);
3218 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, fan_curve,
3219 FAN_CURVE_DEV_MID, 0);
3220 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, fan_curve,
3221 FAN_CURVE_DEV_MID, 1);
3222 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, fan_curve,
3223 FAN_CURVE_DEV_MID, 2);
3224 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, fan_curve,
3225 FAN_CURVE_DEV_MID, 3);
3226 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, fan_curve,
3227 FAN_CURVE_DEV_MID, 4);
3228 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_temp, fan_curve,
3229 FAN_CURVE_DEV_MID, 5);
3230 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_temp, fan_curve,
3231 FAN_CURVE_DEV_MID, 6);
3232 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_temp, fan_curve,
3233 FAN_CURVE_DEV_MID, 7);
3235 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, fan_curve,
3236 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 0);
3237 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, fan_curve,
3238 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 1);
3239 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_pwm, fan_curve,
3240 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 2);
3241 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_pwm, fan_curve,
3242 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 3);
3243 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_pwm, fan_curve,
3244 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 4);
3245 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_pwm, fan_curve,
3246 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 5);
3247 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_pwm, fan_curve,
3248 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 6);
3249 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_pwm, fan_curve,
3250 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 7);
3252 static struct attribute *asus_fan_curve_attr[] = {
3254 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
3255 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
3256 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
3257 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
3258 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
3259 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
3260 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
3261 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
3262 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
3263 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
3264 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
3265 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
3266 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
3267 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
3268 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
3269 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
3270 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
3272 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
3273 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
3274 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
3275 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
3276 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
3277 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
3278 &sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr,
3279 &sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr,
3280 &sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr,
3281 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
3282 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
3283 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
3284 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
3285 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
3286 &sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr,
3287 &sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr,
3288 &sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr,
3290 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
3291 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
3292 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
3293 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
3294 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
3295 &sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
3296 &sensor_dev_attr_pwm3_auto_point6_temp.dev_attr.attr,
3297 &sensor_dev_attr_pwm3_auto_point7_temp.dev_attr.attr,
3298 &sensor_dev_attr_pwm3_auto_point8_temp.dev_attr.attr,
3299 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
3300 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
3301 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
3302 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
3303 &sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
3304 &sensor_dev_attr_pwm3_auto_point6_pwm.dev_attr.attr,
3305 &sensor_dev_attr_pwm3_auto_point7_pwm.dev_attr.attr,
3306 &sensor_dev_attr_pwm3_auto_point8_pwm.dev_attr.attr,
3310 static umode_t asus_fan_curve_is_visible(struct kobject *kobj,
3311 struct attribute *attr, int idx)
3313 struct device *dev = kobj_to_dev(kobj);
3314 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
3317 * Check the char instead of casting attr as there are two attr types
3318 * involved here (attr1 and attr2)
3320 if (asus->cpu_fan_curve_available && attr->name[3] == '1')
3323 if (asus->gpu_fan_curve_available && attr->name[3] == '2')
3326 if (asus->mid_fan_curve_available && attr->name[3] == '3')
3332 static const struct attribute_group asus_fan_curve_attr_group = {
3333 .is_visible = asus_fan_curve_is_visible,
3334 .attrs = asus_fan_curve_attr,
3336 __ATTRIBUTE_GROUPS(asus_fan_curve_attr);
3339 * Must be initialised after throttle_thermal_policy_check_present() as
3340 * we check the status of throttle_thermal_policy_available during init.
3342 static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus)
3344 struct device *dev = &asus->platform_device->dev;
3345 struct device *hwmon;
3348 err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available,
3349 ASUS_WMI_DEVID_CPU_FAN_CURVE);
3353 err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available,
3354 ASUS_WMI_DEVID_GPU_FAN_CURVE);
3358 err = fan_curve_check_present(asus, &asus->mid_fan_curve_available,
3359 ASUS_WMI_DEVID_MID_FAN_CURVE);
3363 if (!asus->cpu_fan_curve_available
3364 && !asus->gpu_fan_curve_available
3365 && !asus->mid_fan_curve_available)
3368 hwmon = devm_hwmon_device_register_with_groups(
3369 dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups);
3371 if (IS_ERR(hwmon)) {
3373 "Could not register asus_custom_fan_curve device\n");
3374 return PTR_ERR(hwmon);
3380 /* Throttle thermal policy ****************************************************/
3382 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
3387 asus->throttle_thermal_policy_available = false;
3389 err = asus_wmi_get_devstate(asus,
3390 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
3398 if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
3399 asus->throttle_thermal_policy_available = true;
3404 static int throttle_thermal_policy_write(struct asus_wmi *asus)
3410 value = asus->throttle_thermal_policy_mode;
3412 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
3415 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
3416 "throttle_thermal_policy");
3419 pr_warn("Failed to set throttle thermal policy: %d\n", err);
3424 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
3429 /* Must set to disabled if mode is toggled */
3430 if (asus->cpu_fan_curve_available)
3431 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
3432 if (asus->gpu_fan_curve_available)
3433 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
3434 if (asus->mid_fan_curve_available)
3435 asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false;
3440 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
3442 if (!asus->throttle_thermal_policy_available)
3445 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3446 return throttle_thermal_policy_write(asus);
3449 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
3451 u8 new_mode = asus->throttle_thermal_policy_mode + 1;
3454 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
3455 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3457 asus->throttle_thermal_policy_mode = new_mode;
3458 err = throttle_thermal_policy_write(asus);
3463 * Ensure that platform_profile updates userspace with the change to ensure
3464 * that platform_profile and throttle_thermal_policy_mode are in sync.
3466 platform_profile_notify();
3471 static ssize_t throttle_thermal_policy_show(struct device *dev,
3472 struct device_attribute *attr, char *buf)
3474 struct asus_wmi *asus = dev_get_drvdata(dev);
3475 u8 mode = asus->throttle_thermal_policy_mode;
3477 return sysfs_emit(buf, "%d\n", mode);
3480 static ssize_t throttle_thermal_policy_store(struct device *dev,
3481 struct device_attribute *attr,
3482 const char *buf, size_t count)
3484 struct asus_wmi *asus = dev_get_drvdata(dev);
3489 result = kstrtou8(buf, 10, &new_mode);
3493 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
3496 asus->throttle_thermal_policy_mode = new_mode;
3497 err = throttle_thermal_policy_write(asus);
3502 * Ensure that platform_profile updates userspace with the change to ensure
3503 * that platform_profile and throttle_thermal_policy_mode are in sync.
3505 platform_profile_notify();
3510 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
3511 static DEVICE_ATTR_RW(throttle_thermal_policy);
3513 /* Platform profile ***********************************************************/
3514 static int asus_wmi_platform_profile_get(struct platform_profile_handler *pprof,
3515 enum platform_profile_option *profile)
3517 struct asus_wmi *asus;
3520 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
3522 tp = asus->throttle_thermal_policy_mode;
3525 case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
3526 *profile = PLATFORM_PROFILE_BALANCED;
3528 case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
3529 *profile = PLATFORM_PROFILE_PERFORMANCE;
3531 case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
3532 *profile = PLATFORM_PROFILE_QUIET;
3541 static int asus_wmi_platform_profile_set(struct platform_profile_handler *pprof,
3542 enum platform_profile_option profile)
3544 struct asus_wmi *asus;
3547 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
3550 case PLATFORM_PROFILE_PERFORMANCE:
3551 tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
3553 case PLATFORM_PROFILE_BALANCED:
3554 tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3556 case PLATFORM_PROFILE_QUIET:
3557 tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
3563 asus->throttle_thermal_policy_mode = tp;
3564 return throttle_thermal_policy_write(asus);
3567 static int platform_profile_setup(struct asus_wmi *asus)
3569 struct device *dev = &asus->platform_device->dev;
3573 * Not an error if a component platform_profile relies on is unavailable
3574 * so early return, skipping the setup of platform_profile.
3576 if (!asus->throttle_thermal_policy_available)
3579 dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
3581 asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
3582 asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
3584 set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
3585 set_bit(PLATFORM_PROFILE_BALANCED,
3586 asus->platform_profile_handler.choices);
3587 set_bit(PLATFORM_PROFILE_PERFORMANCE,
3588 asus->platform_profile_handler.choices);
3590 err = platform_profile_register(&asus->platform_profile_handler);
3594 asus->platform_profile_support = true;
3598 /* Backlight ******************************************************************/
3600 static int read_backlight_power(struct asus_wmi *asus)
3604 if (asus->driver->quirks->store_backlight_power)
3605 ret = !asus->driver->panel_power;
3607 ret = asus_wmi_get_devstate_simple(asus,
3608 ASUS_WMI_DEVID_BACKLIGHT);
3613 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
3616 static int read_brightness_max(struct asus_wmi *asus)
3621 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
3625 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
3634 static int read_brightness(struct backlight_device *bd)
3636 struct asus_wmi *asus = bl_get_data(bd);
3640 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
3644 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
3647 static u32 get_scalar_command(struct backlight_device *bd)
3649 struct asus_wmi *asus = bl_get_data(bd);
3652 if ((asus->driver->brightness < bd->props.brightness) ||
3653 bd->props.brightness == bd->props.max_brightness)
3654 ctrl_param = 0x00008001;
3655 else if ((asus->driver->brightness > bd->props.brightness) ||
3656 bd->props.brightness == 0)
3657 ctrl_param = 0x00008000;
3659 asus->driver->brightness = bd->props.brightness;
3664 static int update_bl_status(struct backlight_device *bd)
3666 struct asus_wmi *asus = bl_get_data(bd);
3670 power = read_backlight_power(asus);
3671 if (power != -ENODEV && bd->props.power != power) {
3672 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
3673 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
3675 if (asus->driver->quirks->store_backlight_power)
3676 asus->driver->panel_power = bd->props.power;
3678 /* When using scalar brightness, updating the brightness
3679 * will mess with the backlight power */
3680 if (asus->driver->quirks->scalar_panel_brightness)
3684 if (asus->driver->quirks->scalar_panel_brightness)
3685 ctrl_param = get_scalar_command(bd);
3687 ctrl_param = bd->props.brightness;
3689 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
3695 static const struct backlight_ops asus_wmi_bl_ops = {
3696 .get_brightness = read_brightness,
3697 .update_status = update_bl_status,
3700 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
3702 struct backlight_device *bd = asus->backlight_device;
3703 int old = bd->props.brightness;
3706 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3707 new = code - NOTIFY_BRNUP_MIN + 1;
3708 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3709 new = code - NOTIFY_BRNDOWN_MIN;
3711 bd->props.brightness = new;
3712 backlight_update_status(bd);
3713 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
3718 static int asus_wmi_backlight_init(struct asus_wmi *asus)
3720 struct backlight_device *bd;
3721 struct backlight_properties props;
3725 max = read_brightness_max(asus);
3729 power = read_backlight_power(asus);
3730 if (power == -ENODEV)
3731 power = FB_BLANK_UNBLANK;
3735 memset(&props, 0, sizeof(struct backlight_properties));
3736 props.type = BACKLIGHT_PLATFORM;
3737 props.max_brightness = max;
3738 bd = backlight_device_register(asus->driver->name,
3739 &asus->platform_device->dev, asus,
3740 &asus_wmi_bl_ops, &props);
3742 pr_err("Could not register backlight device\n");
3746 asus->backlight_device = bd;
3748 if (asus->driver->quirks->store_backlight_power)
3749 asus->driver->panel_power = power;
3751 bd->props.brightness = read_brightness(bd);
3752 bd->props.power = power;
3753 backlight_update_status(bd);
3755 asus->driver->brightness = bd->props.brightness;
3760 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
3762 backlight_device_unregister(asus->backlight_device);
3764 asus->backlight_device = NULL;
3767 static int is_display_toggle(int code)
3769 /* display toggle keys */
3770 if ((code >= 0x61 && code <= 0x67) ||
3771 (code >= 0x8c && code <= 0x93) ||
3772 (code >= 0xa0 && code <= 0xa7) ||
3773 (code >= 0xd0 && code <= 0xd5))
3779 /* Fn-lock ********************************************************************/
3781 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
3785 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
3787 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
3788 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
3791 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
3793 int mode = asus->fnlock_locked;
3795 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
3798 /* WMI events *****************************************************************/
3800 static int asus_wmi_get_event_code(u32 value)
3802 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
3803 union acpi_object *obj;
3807 status = wmi_get_event_data(value, &response);
3808 if (ACPI_FAILURE(status)) {
3809 pr_warn("Failed to get WMI notify code: %s\n",
3810 acpi_format_exception(status));
3814 obj = (union acpi_object *)response.pointer;
3816 if (obj && obj->type == ACPI_TYPE_INTEGER)
3817 code = (int)(obj->integer.value & WMI_EVENT_MASK);
3825 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
3827 unsigned int key_value = 1;
3828 bool autorelease = 1;
3829 int orig_code = code;
3831 if (asus->driver->key_filter) {
3832 asus->driver->key_filter(asus->driver, &code, &key_value,
3834 if (code == ASUS_WMI_KEY_IGNORE)
3838 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3839 code = ASUS_WMI_BRN_UP;
3840 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3841 code = ASUS_WMI_BRN_DOWN;
3843 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
3844 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3845 asus_wmi_backlight_notify(asus, orig_code);
3850 if (code == NOTIFY_KBD_BRTUP) {
3851 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3854 if (code == NOTIFY_KBD_BRTDWN) {
3855 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
3858 if (code == NOTIFY_KBD_BRTTOGGLE) {
3859 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
3860 kbd_led_set_by_kbd(asus, 0);
3862 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3866 if (code == NOTIFY_FNLOCK_TOGGLE) {
3867 asus->fnlock_locked = !asus->fnlock_locked;
3868 asus_wmi_fnlock_update(asus);
3872 if (code == asus->tablet_switch_event_code) {
3873 asus_wmi_tablet_mode_get_state(asus);
3877 if (code == NOTIFY_KBD_FBM || code == NOTIFY_KBD_TTP) {
3878 if (asus->fan_boost_mode_available)
3879 fan_boost_mode_switch_next(asus);
3880 if (asus->throttle_thermal_policy_available)
3881 throttle_thermal_policy_switch_next(asus);
3886 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
3889 if (!sparse_keymap_report_event(asus->inputdev, code,
3890 key_value, autorelease))
3891 pr_info("Unknown key code 0x%x\n", code);
3894 static void asus_wmi_notify(u32 value, void *context)
3896 struct asus_wmi *asus = context;
3900 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3901 code = asus_wmi_get_event_code(value);
3903 pr_warn("Failed to get notify code: %d\n", code);
3907 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3910 asus_wmi_handle_event_code(code, asus);
3913 * Double check that queue is present:
3914 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
3916 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
3920 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
3923 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
3928 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3929 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
3931 pr_warn("Failed to get event during flush: %d\n", code);
3935 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3939 pr_warn("Failed to flush event queue\n");
3943 /* Sysfs **********************************************************************/
3945 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
3946 const char *buf, size_t count)
3951 value = asus_wmi_get_devstate_simple(asus, devid);
3955 err = kstrtoint(buf, 0, &value);
3959 err = asus_wmi_set_devstate(devid, value, &retval);
3966 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
3968 int value = asus_wmi_get_devstate_simple(asus, devid);
3973 return sprintf(buf, "%d\n", value);
3976 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
3977 static ssize_t show_##_name(struct device *dev, \
3978 struct device_attribute *attr, \
3981 struct asus_wmi *asus = dev_get_drvdata(dev); \
3983 return show_sys_wmi(asus, _cm, buf); \
3985 static ssize_t store_##_name(struct device *dev, \
3986 struct device_attribute *attr, \
3987 const char *buf, size_t count) \
3989 struct asus_wmi *asus = dev_get_drvdata(dev); \
3991 return store_sys_wmi(asus, _cm, buf, count); \
3993 static struct device_attribute dev_attr_##_name = { \
3995 .name = __stringify(_name), \
3997 .show = show_##_name, \
3998 .store = store_##_name, \
4001 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
4002 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
4003 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
4004 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
4005 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
4007 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
4008 const char *buf, size_t count)
4012 rv = kstrtoint(buf, 0, &value);
4016 if (value < 0 || value > 2)
4019 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
4026 static DEVICE_ATTR_WO(cpufv);
4028 static struct attribute *platform_attributes[] = {
4029 &dev_attr_cpufv.attr,
4030 &dev_attr_camera.attr,
4031 &dev_attr_cardr.attr,
4032 &dev_attr_touchpad.attr,
4033 &dev_attr_charge_mode.attr,
4034 &dev_attr_egpu_enable.attr,
4035 &dev_attr_egpu_connected.attr,
4036 &dev_attr_dgpu_disable.attr,
4037 &dev_attr_gpu_mux_mode.attr,
4038 &dev_attr_lid_resume.attr,
4039 &dev_attr_als_enable.attr,
4040 &dev_attr_fan_boost_mode.attr,
4041 &dev_attr_throttle_thermal_policy.attr,
4042 &dev_attr_ppt_pl2_sppt.attr,
4043 &dev_attr_ppt_pl1_spl.attr,
4044 &dev_attr_ppt_fppt.attr,
4045 &dev_attr_ppt_apu_sppt.attr,
4046 &dev_attr_ppt_platform_sppt.attr,
4047 &dev_attr_nv_dynamic_boost.attr,
4048 &dev_attr_nv_temp_target.attr,
4049 &dev_attr_panel_od.attr,
4050 &dev_attr_mini_led_mode.attr,
4054 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
4055 struct attribute *attr, int idx)
4057 struct device *dev = kobj_to_dev(kobj);
4058 struct asus_wmi *asus = dev_get_drvdata(dev);
4062 if (attr == &dev_attr_camera.attr)
4063 devid = ASUS_WMI_DEVID_CAMERA;
4064 else if (attr == &dev_attr_cardr.attr)
4065 devid = ASUS_WMI_DEVID_CARDREADER;
4066 else if (attr == &dev_attr_touchpad.attr)
4067 devid = ASUS_WMI_DEVID_TOUCHPAD;
4068 else if (attr == &dev_attr_lid_resume.attr)
4069 devid = ASUS_WMI_DEVID_LID_RESUME;
4070 else if (attr == &dev_attr_als_enable.attr)
4071 devid = ASUS_WMI_DEVID_ALS_ENABLE;
4072 else if (attr == &dev_attr_charge_mode.attr)
4073 ok = asus->charge_mode_available;
4074 else if (attr == &dev_attr_egpu_enable.attr)
4075 ok = asus->egpu_enable_available;
4076 else if (attr == &dev_attr_egpu_connected.attr)
4077 ok = asus->egpu_connect_available;
4078 else if (attr == &dev_attr_dgpu_disable.attr)
4079 ok = asus->dgpu_disable_available;
4080 else if (attr == &dev_attr_gpu_mux_mode.attr)
4081 ok = asus->gpu_mux_mode_available;
4082 else if (attr == &dev_attr_fan_boost_mode.attr)
4083 ok = asus->fan_boost_mode_available;
4084 else if (attr == &dev_attr_throttle_thermal_policy.attr)
4085 ok = asus->throttle_thermal_policy_available;
4086 else if (attr == &dev_attr_ppt_pl2_sppt.attr)
4087 ok = asus->ppt_pl2_sppt_available;
4088 else if (attr == &dev_attr_ppt_pl1_spl.attr)
4089 ok = asus->ppt_pl1_spl_available;
4090 else if (attr == &dev_attr_ppt_fppt.attr)
4091 ok = asus->ppt_fppt_available;
4092 else if (attr == &dev_attr_ppt_apu_sppt.attr)
4093 ok = asus->ppt_apu_sppt_available;
4094 else if (attr == &dev_attr_ppt_platform_sppt.attr)
4095 ok = asus->ppt_plat_sppt_available;
4096 else if (attr == &dev_attr_nv_dynamic_boost.attr)
4097 ok = asus->nv_dyn_boost_available;
4098 else if (attr == &dev_attr_nv_temp_target.attr)
4099 ok = asus->nv_temp_tgt_available;
4100 else if (attr == &dev_attr_panel_od.attr)
4101 ok = asus->panel_overdrive_available;
4102 else if (attr == &dev_attr_mini_led_mode.attr)
4103 ok = asus->mini_led_mode_available;
4106 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
4108 return ok ? attr->mode : 0;
4111 static const struct attribute_group platform_attribute_group = {
4112 .is_visible = asus_sysfs_is_visible,
4113 .attrs = platform_attributes
4116 static void asus_wmi_sysfs_exit(struct platform_device *device)
4118 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
4121 static int asus_wmi_sysfs_init(struct platform_device *device)
4123 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
4126 /* Platform device ************************************************************/
4128 static int asus_wmi_platform_init(struct asus_wmi *asus)
4130 struct device *dev = &asus->platform_device->dev;
4134 /* INIT enable hotkeys on some models */
4135 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
4136 pr_info("Initialization: %#x\n", rv);
4138 /* We don't know yet what to do with this version... */
4139 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
4140 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
4145 * The SFUN method probably allows the original driver to get the list
4146 * of features supported by a given model. For now, 0x0100 or 0x0800
4147 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
4148 * The significance of others is yet to be found.
4150 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
4151 pr_info("SFUN value: %#x\n", rv);
4156 * Eee PC and Notebooks seems to have different method_id for DSTS,
4157 * but it may also be related to the BIOS's SPEC.
4158 * Note, on most Eeepc, there is no way to check if a method exist
4159 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
4160 * but once again, SPEC may probably be used for that kind of things.
4162 * Additionally at least TUF Gaming series laptops return nothing for
4163 * unknown methods, so the detection in this way is not possible.
4165 * There is strong indication that only ACPI WMI devices that have _UID
4166 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
4168 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
4172 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
4173 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
4174 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
4176 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
4177 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
4181 * Some devices can have multiple event codes stored in a queue before
4182 * the module load if it was unloaded intermittently after calling
4183 * the INIT method (enables event handling). The WMI notify handler is
4184 * expected to retrieve all event codes until a retrieved code equals
4185 * queue end marker (One or Ones). Old codes are flushed from the queue
4186 * upon module load. Not enabling this when it should be has minimal
4187 * visible impact so fall back if anything goes wrong.
4189 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
4190 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
4191 dev_info(dev, "Detected ATK, enable event queue\n");
4193 if (!asus_wmi_notify_queue_flush(asus))
4194 asus->wmi_event_queue = true;
4197 /* CWAP allow to define the behavior of the Fn+F2 key,
4198 * this method doesn't seems to be present on Eee PCs */
4199 if (asus->driver->quirks->wapf >= 0)
4200 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
4201 asus->driver->quirks->wapf, NULL);
4206 /* debugfs ********************************************************************/
4208 struct asus_wmi_debugfs_node {
4209 struct asus_wmi *asus;
4211 int (*show) (struct seq_file *m, void *data);
4214 static int show_dsts(struct seq_file *m, void *data)
4216 struct asus_wmi *asus = m->private;
4220 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
4224 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
4229 static int show_devs(struct seq_file *m, void *data)
4231 struct asus_wmi *asus = m->private;
4235 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
4240 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
4241 asus->debug.ctrl_param, retval);
4246 static int show_call(struct seq_file *m, void *data)
4248 struct asus_wmi *asus = m->private;
4249 struct bios_args args = {
4250 .arg0 = asus->debug.dev_id,
4251 .arg1 = asus->debug.ctrl_param,
4253 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
4254 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
4255 union acpi_object *obj;
4258 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
4259 0, asus->debug.method_id,
4262 if (ACPI_FAILURE(status))
4265 obj = (union acpi_object *)output.pointer;
4266 if (obj && obj->type == ACPI_TYPE_INTEGER)
4267 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
4268 asus->debug.dev_id, asus->debug.ctrl_param,
4269 (u32) obj->integer.value);
4271 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
4272 asus->debug.dev_id, asus->debug.ctrl_param,
4273 obj ? obj->type : -1);
4280 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
4281 {NULL, "devs", show_devs},
4282 {NULL, "dsts", show_dsts},
4283 {NULL, "call", show_call},
4286 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
4288 struct asus_wmi_debugfs_node *node = inode->i_private;
4290 return single_open(file, node->show, node->asus);
4293 static const struct file_operations asus_wmi_debugfs_io_ops = {
4294 .owner = THIS_MODULE,
4295 .open = asus_wmi_debugfs_open,
4297 .llseek = seq_lseek,
4298 .release = single_release,
4301 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
4303 debugfs_remove_recursive(asus->debug.root);
4306 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
4310 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
4312 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
4313 &asus->debug.method_id);
4315 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
4316 &asus->debug.dev_id);
4318 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
4319 &asus->debug.ctrl_param);
4321 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
4322 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
4325 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
4326 asus->debug.root, node,
4327 &asus_wmi_debugfs_io_ops);
4331 /* Init / exit ****************************************************************/
4333 static int asus_wmi_add(struct platform_device *pdev)
4335 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
4336 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
4337 struct asus_wmi *asus;
4342 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
4346 asus->driver = wdrv;
4347 asus->platform_device = pdev;
4348 wdrv->platform_device = pdev;
4349 platform_set_drvdata(asus->platform_device, asus);
4351 if (wdrv->detect_quirks)
4352 wdrv->detect_quirks(asus->driver);
4354 err = asus_wmi_platform_init(asus);
4358 asus->charge_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CHARGE_MODE);
4359 asus->egpu_enable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_EGPU);
4360 asus->egpu_connect_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
4361 asus->dgpu_disable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_DGPU);
4362 asus->gpu_mux_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_MUX);
4363 asus->kbd_rgb_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_MODE);
4364 asus->kbd_rgb_state_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_STATE);
4365 asus->ppt_pl2_sppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_PL2_SPPT);
4366 asus->ppt_pl1_spl_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_PL1_SPL);
4367 asus->ppt_fppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_FPPT);
4368 asus->ppt_apu_sppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_APU_SPPT);
4369 asus->ppt_plat_sppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_PLAT_SPPT);
4370 asus->nv_dyn_boost_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_NV_DYN_BOOST);
4371 asus->nv_temp_tgt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_NV_THERM_TARGET);
4372 asus->panel_overdrive_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PANEL_OD);
4373 asus->mini_led_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MINI_LED_MODE);
4375 err = fan_boost_mode_check_present(asus);
4377 goto fail_fan_boost_mode;
4379 err = throttle_thermal_policy_check_present(asus);
4381 goto fail_throttle_thermal_policy;
4383 throttle_thermal_policy_set_default(asus);
4385 err = platform_profile_setup(asus);
4387 goto fail_platform_profile_setup;
4389 err = asus_wmi_sysfs_init(asus->platform_device);
4393 err = asus_wmi_input_init(asus);
4397 err = asus_wmi_fan_init(asus); /* probably no problems on error */
4399 err = asus_wmi_hwmon_init(asus);
4403 err = asus_wmi_custom_fan_curve_init(asus);
4405 goto fail_custom_fan_curve;
4407 err = asus_wmi_led_init(asus);
4411 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
4412 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
4413 asus->driver->wlan_ctrl_by_user = 1;
4415 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
4416 err = asus_wmi_rfkill_init(asus);
4421 if (asus->driver->quirks->wmi_force_als_set)
4424 if (asus->driver->quirks->xusb2pr)
4425 asus_wmi_set_xusb2pr(asus);
4427 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
4428 err = asus_wmi_backlight_init(asus);
4429 if (err && err != -ENODEV)
4430 goto fail_backlight;
4431 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
4432 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
4434 if (asus_wmi_has_fnlock_key(asus)) {
4435 asus->fnlock_locked = fnlock_default;
4436 asus_wmi_fnlock_update(asus);
4439 status = wmi_install_notify_handler(asus->driver->event_guid,
4440 asus_wmi_notify, asus);
4441 if (ACPI_FAILURE(status)) {
4442 pr_err("Unable to register notify handler - %d\n", status);
4444 goto fail_wmi_handler;
4447 asus_wmi_battery_init(asus);
4449 asus_wmi_debugfs_init(asus);
4454 asus_wmi_backlight_exit(asus);
4456 asus_wmi_rfkill_exit(asus);
4458 asus_wmi_led_exit(asus);
4461 asus_wmi_input_exit(asus);
4463 asus_wmi_sysfs_exit(asus->platform_device);
4465 fail_throttle_thermal_policy:
4466 fail_custom_fan_curve:
4467 fail_platform_profile_setup:
4468 if (asus->platform_profile_support)
4469 platform_profile_remove();
4470 fail_fan_boost_mode:
4476 static int asus_wmi_remove(struct platform_device *device)
4478 struct asus_wmi *asus;
4480 asus = platform_get_drvdata(device);
4481 wmi_remove_notify_handler(asus->driver->event_guid);
4482 asus_wmi_backlight_exit(asus);
4483 asus_wmi_input_exit(asus);
4484 asus_wmi_led_exit(asus);
4485 asus_wmi_rfkill_exit(asus);
4486 asus_wmi_debugfs_exit(asus);
4487 asus_wmi_sysfs_exit(asus->platform_device);
4488 asus_fan_set_auto(asus);
4489 throttle_thermal_policy_set_default(asus);
4490 asus_wmi_battery_exit(asus);
4492 if (asus->platform_profile_support)
4493 platform_profile_remove();
4499 /* Platform driver - hibernate/resume callbacks *******************************/
4501 static int asus_hotk_thaw(struct device *device)
4503 struct asus_wmi *asus = dev_get_drvdata(device);
4505 if (asus->wlan.rfkill) {
4509 * Work around bios bug - acpi _PTS turns off the wireless led
4510 * during suspend. Normally it restores it on resume, but
4511 * we should kick it ourselves in case hibernation is aborted.
4513 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
4514 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
4520 static int asus_hotk_resume(struct device *device)
4522 struct asus_wmi *asus = dev_get_drvdata(device);
4524 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
4525 kbd_led_update(asus);
4527 if (asus_wmi_has_fnlock_key(asus))
4528 asus_wmi_fnlock_update(asus);
4530 asus_wmi_tablet_mode_get_state(asus);
4534 static int asus_hotk_restore(struct device *device)
4536 struct asus_wmi *asus = dev_get_drvdata(device);
4539 /* Refresh both wlan rfkill state and pci hotplug */
4540 if (asus->wlan.rfkill)
4541 asus_rfkill_hotplug(asus);
4543 if (asus->bluetooth.rfkill) {
4544 bl = !asus_wmi_get_devstate_simple(asus,
4545 ASUS_WMI_DEVID_BLUETOOTH);
4546 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
4548 if (asus->wimax.rfkill) {
4549 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
4550 rfkill_set_sw_state(asus->wimax.rfkill, bl);
4552 if (asus->wwan3g.rfkill) {
4553 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
4554 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
4556 if (asus->gps.rfkill) {
4557 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
4558 rfkill_set_sw_state(asus->gps.rfkill, bl);
4560 if (asus->uwb.rfkill) {
4561 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
4562 rfkill_set_sw_state(asus->uwb.rfkill, bl);
4564 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
4565 kbd_led_update(asus);
4567 if (asus_wmi_has_fnlock_key(asus))
4568 asus_wmi_fnlock_update(asus);
4570 asus_wmi_tablet_mode_get_state(asus);
4574 static const struct dev_pm_ops asus_pm_ops = {
4575 .thaw = asus_hotk_thaw,
4576 .restore = asus_hotk_restore,
4577 .resume = asus_hotk_resume,
4580 /* Registration ***************************************************************/
4582 static int asus_wmi_probe(struct platform_device *pdev)
4584 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
4585 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
4588 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
4589 pr_warn("ASUS Management GUID not found\n");
4593 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
4594 pr_warn("ASUS Event GUID not found\n");
4599 ret = wdrv->probe(pdev);
4604 return asus_wmi_add(pdev);
4609 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
4611 struct platform_driver *platform_driver;
4612 struct platform_device *platform_device;
4617 platform_driver = &driver->platform_driver;
4618 platform_driver->remove = asus_wmi_remove;
4619 platform_driver->driver.owner = driver->owner;
4620 platform_driver->driver.name = driver->name;
4621 platform_driver->driver.pm = &asus_pm_ops;
4623 platform_device = platform_create_bundle(platform_driver,
4626 if (IS_ERR(platform_device))
4627 return PTR_ERR(platform_device);
4632 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
4634 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
4636 platform_device_unregister(driver->platform_device);
4637 platform_driver_unregister(&driver->platform_driver);
4640 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
4642 static int __init asus_wmi_init(void)
4644 pr_info("ASUS WMI generic driver loaded\n");
4648 static void __exit asus_wmi_exit(void)
4650 pr_info("ASUS WMI generic driver unloaded\n");
4653 module_init(asus_wmi_init);
4654 module_exit(asus_wmi_exit);