xref: /linux/drivers/platform/x86/asus-wmi.c (revision 001821b0e79716c4e17c71d8e053a23599a7a508)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Asus PC WMI hotkey driver
4  *
5  * Copyright(C) 2010 Intel Corporation.
6  * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
7  *
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>
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
16 #include <linux/acpi.h>
17 #include <linux/backlight.h>
18 #include <linux/debugfs.h>
19 #include <linux/delay.h>
20 #include <linux/dmi.h>
21 #include <linux/fb.h>
22 #include <linux/hwmon.h>
23 #include <linux/hwmon-sysfs.h>
24 #include <linux/init.h>
25 #include <linux/input.h>
26 #include <linux/input/sparse-keymap.h>
27 #include <linux/kernel.h>
28 #include <linux/leds.h>
29 #include <linux/minmax.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/pci_hotplug.h>
33 #include <linux/platform_data/x86/asus-wmi.h>
34 #include <linux/platform_device.h>
35 #include <linux/platform_profile.h>
36 #include <linux/power_supply.h>
37 #include <linux/rfkill.h>
38 #include <linux/seq_file.h>
39 #include <linux/slab.h>
40 #include <linux/types.h>
41 #include <linux/units.h>
42 
43 #include <acpi/battery.h>
44 #include <acpi/video.h>
45 
46 #include "asus-wmi.h"
47 
48 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>");
49 MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
50 MODULE_DESCRIPTION("Asus Generic WMI Driver");
51 MODULE_LICENSE("GPL");
52 
53 static bool fnlock_default = true;
54 module_param(fnlock_default, bool, 0444);
55 
56 #define to_asus_wmi_driver(pdrv)					\
57 	(container_of((pdrv), struct asus_wmi_driver, platform_driver))
58 
59 #define ASUS_WMI_MGMT_GUID	"97845ED0-4E6D-11DE-8A39-0800200C9A66"
60 
61 #define NOTIFY_BRNUP_MIN		0x11
62 #define NOTIFY_BRNUP_MAX		0x1f
63 #define NOTIFY_BRNDOWN_MIN		0x20
64 #define NOTIFY_BRNDOWN_MAX		0x2e
65 #define NOTIFY_FNLOCK_TOGGLE		0x4e
66 #define NOTIFY_KBD_DOCK_CHANGE		0x75
67 #define NOTIFY_KBD_BRTUP		0xc4
68 #define NOTIFY_KBD_BRTDWN		0xc5
69 #define NOTIFY_KBD_BRTTOGGLE		0xc7
70 #define NOTIFY_KBD_FBM			0x99
71 #define NOTIFY_KBD_TTP			0xae
72 #define NOTIFY_LID_FLIP			0xfa
73 #define NOTIFY_LID_FLIP_ROG		0xbd
74 
75 #define ASUS_WMI_FNLOCK_BIOS_DISABLED	BIT(0)
76 
77 #define ASUS_MID_FAN_DESC		"mid_fan"
78 #define ASUS_GPU_FAN_DESC		"gpu_fan"
79 #define ASUS_FAN_DESC			"cpu_fan"
80 #define ASUS_FAN_MFUN			0x13
81 #define ASUS_FAN_SFUN_READ		0x06
82 #define ASUS_FAN_SFUN_WRITE		0x07
83 
84 /* Based on standard hwmon pwmX_enable values */
85 #define ASUS_FAN_CTRL_FULLSPEED		0
86 #define ASUS_FAN_CTRL_MANUAL		1
87 #define ASUS_FAN_CTRL_AUTO		2
88 
89 #define ASUS_FAN_BOOST_MODE_NORMAL		0
90 #define ASUS_FAN_BOOST_MODE_OVERBOOST		1
91 #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK	0x01
92 #define ASUS_FAN_BOOST_MODE_SILENT		2
93 #define ASUS_FAN_BOOST_MODE_SILENT_MASK		0x02
94 #define ASUS_FAN_BOOST_MODES_MASK		0x03
95 
96 #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT	0
97 #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST	1
98 #define ASUS_THROTTLE_THERMAL_POLICY_SILENT	2
99 
100 #define USB_INTEL_XUSB2PR		0xD0
101 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI	0x9c31
102 
103 #define ASUS_ACPI_UID_ASUSWMI		"ASUSWMI"
104 
105 #define WMI_EVENT_MASK			0xFFFF
106 
107 #define FAN_CURVE_POINTS		8
108 #define FAN_CURVE_BUF_LEN		32
109 #define FAN_CURVE_DEV_CPU		0x00
110 #define FAN_CURVE_DEV_GPU		0x01
111 #define FAN_CURVE_DEV_MID		0x02
112 /* Mask to determine if setting temperature or percentage */
113 #define FAN_CURVE_PWM_MASK		0x04
114 
115 /* Limits for tunables available on ASUS ROG laptops */
116 #define PPT_TOTAL_MIN		5
117 #define PPT_TOTAL_MAX		250
118 #define PPT_CPU_MIN			5
119 #define PPT_CPU_MAX			130
120 #define NVIDIA_BOOST_MIN	5
121 #define NVIDIA_BOOST_MAX	25
122 #define NVIDIA_TEMP_MIN		75
123 #define NVIDIA_TEMP_MAX		87
124 
125 #define ASUS_SCREENPAD_BRIGHT_MIN 20
126 #define ASUS_SCREENPAD_BRIGHT_MAX 255
127 #define ASUS_SCREENPAD_BRIGHT_DEFAULT 60
128 
129 #define ASUS_MINI_LED_MODE_MASK		0x03
130 /* Standard modes for devices with only on/off */
131 #define ASUS_MINI_LED_OFF		0x00
132 #define ASUS_MINI_LED_ON		0x01
133 /* New mode on some devices, define here to clarify remapping later */
134 #define ASUS_MINI_LED_STRONG_MODE	0x02
135 /* New modes for devices with 3 mini-led mode types */
136 #define ASUS_MINI_LED_2024_WEAK		0x00
137 #define ASUS_MINI_LED_2024_STRONG	0x01
138 #define ASUS_MINI_LED_2024_OFF		0x02
139 
140 /* Controls the power state of the USB0 hub on ROG Ally which input is on */
141 #define ASUS_USB0_PWR_EC0_CSEE "\\_SB.PCI0.SBRG.EC0.CSEE"
142 /* 300ms so far seems to produce a reliable result on AC and battery */
143 #define ASUS_USB0_PWR_EC0_CSEE_WAIT 1500
144 
145 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
146 
147 static int throttle_thermal_policy_write(struct asus_wmi *);
148 
149 static bool ashs_present(void)
150 {
151 	int i = 0;
152 	while (ashs_ids[i]) {
153 		if (acpi_dev_found(ashs_ids[i++]))
154 			return true;
155 	}
156 	return false;
157 }
158 
159 struct bios_args {
160 	u32 arg0;
161 	u32 arg1;
162 	u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
163 	u32 arg3;
164 	u32 arg4; /* Some ROG laptops require a full 5 input args */
165 	u32 arg5;
166 } __packed;
167 
168 /*
169  * Struct that's used for all methods called via AGFN. Naming is
170  * identically to the AML code.
171  */
172 struct agfn_args {
173 	u16 mfun; /* probably "Multi-function" to be called */
174 	u16 sfun; /* probably "Sub-function" to be called */
175 	u16 len;  /* size of the hole struct, including subfunction fields */
176 	u8 stas;  /* not used by now */
177 	u8 err;   /* zero on success */
178 } __packed;
179 
180 /* struct used for calling fan read and write methods */
181 struct agfn_fan_args {
182 	struct agfn_args agfn;	/* common fields */
183 	u8 fan;			/* fan number: 0: set auto mode 1: 1st fan */
184 	u32 speed;		/* read: RPM/100 - write: 0-255 */
185 } __packed;
186 
187 /*
188  * <platform>/    - debugfs root directory
189  *   dev_id      - current dev_id
190  *   ctrl_param  - current ctrl_param
191  *   method_id   - current method_id
192  *   devs        - call DEVS(dev_id, ctrl_param) and print result
193  *   dsts        - call DSTS(dev_id)  and print result
194  *   call        - call method_id(dev_id, ctrl_param) and print result
195  */
196 struct asus_wmi_debug {
197 	struct dentry *root;
198 	u32 method_id;
199 	u32 dev_id;
200 	u32 ctrl_param;
201 };
202 
203 struct asus_rfkill {
204 	struct asus_wmi *asus;
205 	struct rfkill *rfkill;
206 	u32 dev_id;
207 };
208 
209 enum fan_type {
210 	FAN_TYPE_NONE = 0,
211 	FAN_TYPE_AGFN,		/* deprecated on newer platforms */
212 	FAN_TYPE_SPEC83,	/* starting in Spec 8.3, use CPU_FAN_CTRL */
213 };
214 
215 struct fan_curve_data {
216 	bool enabled;
217 	u32 device_id;
218 	u8 temps[FAN_CURVE_POINTS];
219 	u8 percents[FAN_CURVE_POINTS];
220 };
221 
222 struct asus_wmi {
223 	int dsts_id;
224 	int spec;
225 	int sfun;
226 
227 	struct input_dev *inputdev;
228 	struct backlight_device *backlight_device;
229 	struct backlight_device *screenpad_backlight_device;
230 	struct platform_device *platform_device;
231 
232 	struct led_classdev wlan_led;
233 	int wlan_led_wk;
234 	struct led_classdev tpd_led;
235 	int tpd_led_wk;
236 	struct led_classdev kbd_led;
237 	int kbd_led_wk;
238 	struct led_classdev lightbar_led;
239 	int lightbar_led_wk;
240 	struct led_classdev micmute_led;
241 	struct workqueue_struct *led_workqueue;
242 	struct work_struct tpd_led_work;
243 	struct work_struct wlan_led_work;
244 	struct work_struct lightbar_led_work;
245 
246 	struct asus_rfkill wlan;
247 	struct asus_rfkill bluetooth;
248 	struct asus_rfkill wimax;
249 	struct asus_rfkill wwan3g;
250 	struct asus_rfkill gps;
251 	struct asus_rfkill uwb;
252 
253 	int tablet_switch_event_code;
254 	u32 tablet_switch_dev_id;
255 	bool tablet_switch_inverted;
256 
257 	/* The ROG Ally device requires the MCU USB device be disconnected before suspend */
258 	bool ally_mcu_usb_switch;
259 
260 	enum fan_type fan_type;
261 	enum fan_type gpu_fan_type;
262 	enum fan_type mid_fan_type;
263 	int fan_pwm_mode;
264 	int gpu_fan_pwm_mode;
265 	int mid_fan_pwm_mode;
266 	int agfn_pwm;
267 
268 	bool fan_boost_mode_available;
269 	u8 fan_boost_mode_mask;
270 	u8 fan_boost_mode;
271 
272 	bool egpu_enable_available;
273 	bool dgpu_disable_available;
274 	u32 gpu_mux_dev;
275 
276 	/* Tunables provided by ASUS for gaming laptops */
277 	u32 ppt_pl2_sppt;
278 	u32 ppt_pl1_spl;
279 	u32 ppt_apu_sppt;
280 	u32 ppt_platform_sppt;
281 	u32 ppt_fppt;
282 	u32 nv_dynamic_boost;
283 	u32 nv_temp_target;
284 
285 	u32 kbd_rgb_dev;
286 	bool kbd_rgb_state_available;
287 
288 	bool throttle_thermal_policy_available;
289 	u8 throttle_thermal_policy_mode;
290 
291 	bool cpu_fan_curve_available;
292 	bool gpu_fan_curve_available;
293 	bool mid_fan_curve_available;
294 	struct fan_curve_data custom_fan_curves[3];
295 
296 	struct platform_profile_handler platform_profile_handler;
297 	bool platform_profile_support;
298 
299 	// The RSOC controls the maximum charging percentage.
300 	bool battery_rsoc_available;
301 
302 	bool panel_overdrive_available;
303 	u32 mini_led_dev_id;
304 
305 	struct hotplug_slot hotplug_slot;
306 	struct mutex hotplug_lock;
307 	struct mutex wmi_lock;
308 	struct workqueue_struct *hotplug_workqueue;
309 	struct work_struct hotplug_work;
310 
311 	bool fnlock_locked;
312 
313 	struct asus_wmi_debug debug;
314 
315 	struct asus_wmi_driver *driver;
316 };
317 
318 /* WMI ************************************************************************/
319 
320 static int asus_wmi_evaluate_method3(u32 method_id,
321 		u32 arg0, u32 arg1, u32 arg2, u32 *retval)
322 {
323 	struct bios_args args = {
324 		.arg0 = arg0,
325 		.arg1 = arg1,
326 		.arg2 = arg2,
327 	};
328 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
329 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
330 	acpi_status status;
331 	union acpi_object *obj;
332 	u32 tmp = 0;
333 
334 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
335 				     &input, &output);
336 
337 	if (ACPI_FAILURE(status))
338 		return -EIO;
339 
340 	obj = (union acpi_object *)output.pointer;
341 	if (obj && obj->type == ACPI_TYPE_INTEGER)
342 		tmp = (u32) obj->integer.value;
343 
344 	if (retval)
345 		*retval = tmp;
346 
347 	kfree(obj);
348 
349 	if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
350 		return -ENODEV;
351 
352 	return 0;
353 }
354 
355 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
356 {
357 	return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
358 }
359 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
360 
361 static int asus_wmi_evaluate_method5(u32 method_id,
362 		u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4, u32 *retval)
363 {
364 	struct bios_args args = {
365 		.arg0 = arg0,
366 		.arg1 = arg1,
367 		.arg2 = arg2,
368 		.arg3 = arg3,
369 		.arg4 = arg4,
370 	};
371 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
372 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
373 	acpi_status status;
374 	union acpi_object *obj;
375 	u32 tmp = 0;
376 
377 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
378 				     &input, &output);
379 
380 	if (ACPI_FAILURE(status))
381 		return -EIO;
382 
383 	obj = (union acpi_object *)output.pointer;
384 	if (obj && obj->type == ACPI_TYPE_INTEGER)
385 		tmp = (u32) obj->integer.value;
386 
387 	if (retval)
388 		*retval = tmp;
389 
390 	kfree(obj);
391 
392 	if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
393 		return -ENODEV;
394 
395 	return 0;
396 }
397 
398 /*
399  * Returns as an error if the method output is not a buffer. Typically this
400  * means that the method called is unsupported.
401  */
402 static int asus_wmi_evaluate_method_buf(u32 method_id,
403 		u32 arg0, u32 arg1, u8 *ret_buffer, size_t size)
404 {
405 	struct bios_args args = {
406 		.arg0 = arg0,
407 		.arg1 = arg1,
408 		.arg2 = 0,
409 	};
410 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
411 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
412 	acpi_status status;
413 	union acpi_object *obj;
414 	int err = 0;
415 
416 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
417 				     &input, &output);
418 
419 	if (ACPI_FAILURE(status))
420 		return -EIO;
421 
422 	obj = (union acpi_object *)output.pointer;
423 
424 	switch (obj->type) {
425 	case ACPI_TYPE_BUFFER:
426 		if (obj->buffer.length > size) {
427 			err = -ENOSPC;
428 			break;
429 		}
430 		if (obj->buffer.length == 0) {
431 			err = -ENODATA;
432 			break;
433 		}
434 
435 		memcpy(ret_buffer, obj->buffer.pointer, obj->buffer.length);
436 		break;
437 	case ACPI_TYPE_INTEGER:
438 		err = (u32)obj->integer.value;
439 
440 		if (err == ASUS_WMI_UNSUPPORTED_METHOD)
441 			err = -ENODEV;
442 		/*
443 		 * At least one method returns a 0 with no buffer if no arg
444 		 * is provided, such as ASUS_WMI_DEVID_CPU_FAN_CURVE
445 		 */
446 		if (err == 0)
447 			err = -ENODATA;
448 		break;
449 	default:
450 		err = -ENODATA;
451 		break;
452 	}
453 
454 	kfree(obj);
455 
456 	if (err)
457 		return err;
458 
459 	return 0;
460 }
461 
462 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
463 {
464 	struct acpi_buffer input;
465 	u64 phys_addr;
466 	u32 retval;
467 	u32 status;
468 
469 	/*
470 	 * Copy to dma capable address otherwise memory corruption occurs as
471 	 * bios has to be able to access it.
472 	 */
473 	input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
474 	input.length = args.length;
475 	if (!input.pointer)
476 		return -ENOMEM;
477 	phys_addr = virt_to_phys(input.pointer);
478 
479 	status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
480 					phys_addr, 0, &retval);
481 	if (!status)
482 		memcpy(args.pointer, input.pointer, args.length);
483 
484 	kfree(input.pointer);
485 	if (status)
486 		return -ENXIO;
487 
488 	return retval;
489 }
490 
491 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
492 {
493 	int err;
494 
495 	err = asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
496 
497 	if (err)
498 		return err;
499 
500 	if (*retval == ~0)
501 		return -ENODEV;
502 
503 	return 0;
504 }
505 
506 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
507 				 u32 *retval)
508 {
509 	return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
510 					ctrl_param, retval);
511 }
512 
513 /* Helper for special devices with magic return codes */
514 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
515 				      u32 dev_id, u32 mask)
516 {
517 	u32 retval = 0;
518 	int err;
519 
520 	err = asus_wmi_get_devstate(asus, dev_id, &retval);
521 	if (err < 0)
522 		return err;
523 
524 	if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
525 		return -ENODEV;
526 
527 	if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
528 		if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
529 			return -ENODEV;
530 	}
531 
532 	return retval & mask;
533 }
534 
535 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
536 {
537 	return asus_wmi_get_devstate_bits(asus, dev_id,
538 					  ASUS_WMI_DSTS_STATUS_BIT);
539 }
540 
541 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
542 {
543 	u32 retval;
544 	int status = asus_wmi_get_devstate(asus, dev_id, &retval);
545 
546 	return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
547 }
548 
549 /* Input **********************************************************************/
550 static void asus_wmi_tablet_sw_report(struct asus_wmi *asus, bool value)
551 {
552 	input_report_switch(asus->inputdev, SW_TABLET_MODE,
553 			    asus->tablet_switch_inverted ? !value : value);
554 	input_sync(asus->inputdev);
555 }
556 
557 static void asus_wmi_tablet_sw_init(struct asus_wmi *asus, u32 dev_id, int event_code)
558 {
559 	struct device *dev = &asus->platform_device->dev;
560 	int result;
561 
562 	result = asus_wmi_get_devstate_simple(asus, dev_id);
563 	if (result >= 0) {
564 		input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
565 		asus_wmi_tablet_sw_report(asus, result);
566 		asus->tablet_switch_dev_id = dev_id;
567 		asus->tablet_switch_event_code = event_code;
568 	} else if (result == -ENODEV) {
569 		dev_err(dev, "This device has tablet-mode-switch quirk but got ENODEV checking it. This is a bug.");
570 	} else {
571 		dev_err(dev, "Error checking for tablet-mode-switch: %d\n", result);
572 	}
573 }
574 
575 static int asus_wmi_input_init(struct asus_wmi *asus)
576 {
577 	struct device *dev = &asus->platform_device->dev;
578 	int err;
579 
580 	asus->inputdev = input_allocate_device();
581 	if (!asus->inputdev)
582 		return -ENOMEM;
583 
584 	asus->inputdev->name = asus->driver->input_name;
585 	asus->inputdev->phys = asus->driver->input_phys;
586 	asus->inputdev->id.bustype = BUS_HOST;
587 	asus->inputdev->dev.parent = dev;
588 	set_bit(EV_REP, asus->inputdev->evbit);
589 
590 	err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
591 	if (err)
592 		goto err_free_dev;
593 
594 	switch (asus->driver->quirks->tablet_switch_mode) {
595 	case asus_wmi_no_tablet_switch:
596 		break;
597 	case asus_wmi_kbd_dock_devid:
598 		asus->tablet_switch_inverted = true;
599 		asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_KBD_DOCK, NOTIFY_KBD_DOCK_CHANGE);
600 		break;
601 	case asus_wmi_lid_flip_devid:
602 		asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP, NOTIFY_LID_FLIP);
603 		break;
604 	case asus_wmi_lid_flip_rog_devid:
605 		asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP_ROG, NOTIFY_LID_FLIP_ROG);
606 		break;
607 	}
608 
609 	err = input_register_device(asus->inputdev);
610 	if (err)
611 		goto err_free_dev;
612 
613 	return 0;
614 
615 err_free_dev:
616 	input_free_device(asus->inputdev);
617 	return err;
618 }
619 
620 static void asus_wmi_input_exit(struct asus_wmi *asus)
621 {
622 	if (asus->inputdev)
623 		input_unregister_device(asus->inputdev);
624 
625 	asus->inputdev = NULL;
626 }
627 
628 /* Tablet mode ****************************************************************/
629 
630 static void asus_wmi_tablet_mode_get_state(struct asus_wmi *asus)
631 {
632 	int result;
633 
634 	if (!asus->tablet_switch_dev_id)
635 		return;
636 
637 	result = asus_wmi_get_devstate_simple(asus, asus->tablet_switch_dev_id);
638 	if (result >= 0)
639 		asus_wmi_tablet_sw_report(asus, result);
640 }
641 
642 /* Charging mode, 1=Barrel, 2=USB ******************************************/
643 static ssize_t charge_mode_show(struct device *dev,
644 				   struct device_attribute *attr, char *buf)
645 {
646 	struct asus_wmi *asus = dev_get_drvdata(dev);
647 	int result, value;
648 
649 	result = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CHARGE_MODE, &value);
650 	if (result < 0)
651 		return result;
652 
653 	return sysfs_emit(buf, "%d\n", value & 0xff);
654 }
655 
656 static DEVICE_ATTR_RO(charge_mode);
657 
658 /* dGPU ********************************************************************/
659 static ssize_t dgpu_disable_show(struct device *dev,
660 				   struct device_attribute *attr, char *buf)
661 {
662 	struct asus_wmi *asus = dev_get_drvdata(dev);
663 	int result;
664 
665 	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU);
666 	if (result < 0)
667 		return result;
668 
669 	return sysfs_emit(buf, "%d\n", result);
670 }
671 
672 /*
673  * A user may be required to store the value twice, typcial store first, then
674  * rescan PCI bus to activate power, then store a second time to save correctly.
675  * The reason for this is that an extra code path in the ACPI is enabled when
676  * the device and bus are powered.
677  */
678 static ssize_t dgpu_disable_store(struct device *dev,
679 				    struct device_attribute *attr,
680 				    const char *buf, size_t count)
681 {
682 	int result, err;
683 	u32 disable;
684 
685 	struct asus_wmi *asus = dev_get_drvdata(dev);
686 
687 	result = kstrtou32(buf, 10, &disable);
688 	if (result)
689 		return result;
690 
691 	if (disable > 1)
692 		return -EINVAL;
693 
694 	if (asus->gpu_mux_dev) {
695 		result = asus_wmi_get_devstate_simple(asus, asus->gpu_mux_dev);
696 		if (result < 0)
697 			/* An error here may signal greater failure of GPU handling */
698 			return result;
699 		if (!result && disable) {
700 			err = -ENODEV;
701 			pr_warn("Can not disable dGPU when the MUX is in dGPU mode: %d\n", err);
702 			return err;
703 		}
704 	}
705 
706 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, disable, &result);
707 	if (err) {
708 		pr_warn("Failed to set dgpu disable: %d\n", err);
709 		return err;
710 	}
711 
712 	if (result > 1) {
713 		pr_warn("Failed to set dgpu disable (result): 0x%x\n", result);
714 		return -EIO;
715 	}
716 
717 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
718 
719 	return count;
720 }
721 static DEVICE_ATTR_RW(dgpu_disable);
722 
723 /* eGPU ********************************************************************/
724 static ssize_t egpu_enable_show(struct device *dev,
725 				   struct device_attribute *attr, char *buf)
726 {
727 	struct asus_wmi *asus = dev_get_drvdata(dev);
728 	int result;
729 
730 	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU);
731 	if (result < 0)
732 		return result;
733 
734 	return sysfs_emit(buf, "%d\n", result);
735 }
736 
737 /* The ACPI call to enable the eGPU also disables the internal dGPU */
738 static ssize_t egpu_enable_store(struct device *dev,
739 				    struct device_attribute *attr,
740 				    const char *buf, size_t count)
741 {
742 	int result, err;
743 	u32 enable;
744 
745 	struct asus_wmi *asus = dev_get_drvdata(dev);
746 
747 	err = kstrtou32(buf, 10, &enable);
748 	if (err)
749 		return err;
750 
751 	if (enable > 1)
752 		return -EINVAL;
753 
754 	err = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
755 	if (err < 0) {
756 		pr_warn("Failed to get egpu connection status: %d\n", err);
757 		return err;
758 	}
759 
760 	if (asus->gpu_mux_dev) {
761 		result = asus_wmi_get_devstate_simple(asus, asus->gpu_mux_dev);
762 		if (result < 0) {
763 			/* An error here may signal greater failure of GPU handling */
764 			pr_warn("Failed to get gpu mux status: %d\n", result);
765 			return result;
766 		}
767 		if (!result && enable) {
768 			err = -ENODEV;
769 			pr_warn("Can not enable eGPU when the MUX is in dGPU mode: %d\n", err);
770 			return err;
771 		}
772 	}
773 
774 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, enable, &result);
775 	if (err) {
776 		pr_warn("Failed to set egpu state: %d\n", err);
777 		return err;
778 	}
779 
780 	if (result > 1) {
781 		pr_warn("Failed to set egpu state (retval): 0x%x\n", result);
782 		return -EIO;
783 	}
784 
785 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable");
786 
787 	return count;
788 }
789 static DEVICE_ATTR_RW(egpu_enable);
790 
791 /* Is eGPU connected? *********************************************************/
792 static ssize_t egpu_connected_show(struct device *dev,
793 				   struct device_attribute *attr, char *buf)
794 {
795 	struct asus_wmi *asus = dev_get_drvdata(dev);
796 	int result;
797 
798 	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
799 	if (result < 0)
800 		return result;
801 
802 	return sysfs_emit(buf, "%d\n", result);
803 }
804 
805 static DEVICE_ATTR_RO(egpu_connected);
806 
807 /* gpu mux switch *************************************************************/
808 static ssize_t gpu_mux_mode_show(struct device *dev,
809 				 struct device_attribute *attr, char *buf)
810 {
811 	struct asus_wmi *asus = dev_get_drvdata(dev);
812 	int result;
813 
814 	result = asus_wmi_get_devstate_simple(asus, asus->gpu_mux_dev);
815 	if (result < 0)
816 		return result;
817 
818 	return sysfs_emit(buf, "%d\n", result);
819 }
820 
821 static ssize_t gpu_mux_mode_store(struct device *dev,
822 				  struct device_attribute *attr,
823 				  const char *buf, size_t count)
824 {
825 	struct asus_wmi *asus = dev_get_drvdata(dev);
826 	int result, err;
827 	u32 optimus;
828 
829 	err = kstrtou32(buf, 10, &optimus);
830 	if (err)
831 		return err;
832 
833 	if (optimus > 1)
834 		return -EINVAL;
835 
836 	if (asus->dgpu_disable_available) {
837 		result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU);
838 		if (result < 0)
839 			/* An error here may signal greater failure of GPU handling */
840 			return result;
841 		if (result && !optimus) {
842 			err = -ENODEV;
843 			pr_warn("Can not switch MUX to dGPU mode when dGPU is disabled: %d\n", err);
844 			return err;
845 		}
846 	}
847 
848 	if (asus->egpu_enable_available) {
849 		result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU);
850 		if (result < 0)
851 			/* An error here may signal greater failure of GPU handling */
852 			return result;
853 		if (result && !optimus) {
854 			err = -ENODEV;
855 			pr_warn("Can not switch MUX to dGPU mode when eGPU is enabled: %d\n", err);
856 			return err;
857 		}
858 	}
859 
860 	err = asus_wmi_set_devstate(asus->gpu_mux_dev, optimus, &result);
861 	if (err) {
862 		dev_err(dev, "Failed to set GPU MUX mode: %d\n", err);
863 		return err;
864 	}
865 	/* !1 is considered a fail by ASUS */
866 	if (result != 1) {
867 		dev_warn(dev, "Failed to set GPU MUX mode (result): 0x%x\n", result);
868 		return -EIO;
869 	}
870 
871 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "gpu_mux_mode");
872 
873 	return count;
874 }
875 static DEVICE_ATTR_RW(gpu_mux_mode);
876 
877 /* TUF Laptop Keyboard RGB Modes **********************************************/
878 static ssize_t kbd_rgb_mode_store(struct device *dev,
879 				 struct device_attribute *attr,
880 				 const char *buf, size_t count)
881 {
882 	struct asus_wmi *asus = dev_get_drvdata(dev);
883 	u32 cmd, mode, r, g, b, speed;
884 	int err;
885 
886 	if (sscanf(buf, "%d %d %d %d %d %d", &cmd, &mode, &r, &g, &b, &speed) != 6)
887 		return -EINVAL;
888 
889 	/* B3 is set and B4 is save to BIOS */
890 	switch (cmd) {
891 	case 0:
892 		cmd = 0xb3;
893 		break;
894 	case 1:
895 		cmd = 0xb4;
896 		break;
897 	default:
898 		return -EINVAL;
899 	}
900 
901 	/* These are the known usable modes across all TUF/ROG */
902 	if (mode >= 12 || mode == 9)
903 		mode = 10;
904 
905 	switch (speed) {
906 	case 0:
907 		speed = 0xe1;
908 		break;
909 	case 1:
910 		speed = 0xeb;
911 		break;
912 	case 2:
913 		speed = 0xf5;
914 		break;
915 	default:
916 		speed = 0xeb;
917 	}
918 
919 	err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS, asus->kbd_rgb_dev,
920 			cmd | (mode << 8) | (r << 16) | (g << 24), b | (speed << 8), NULL);
921 	if (err)
922 		return err;
923 
924 	return count;
925 }
926 static DEVICE_ATTR_WO(kbd_rgb_mode);
927 
928 static DEVICE_STRING_ATTR_RO(kbd_rgb_mode_index, 0444,
929 			     "cmd mode red green blue speed");
930 
931 static struct attribute *kbd_rgb_mode_attrs[] = {
932 	&dev_attr_kbd_rgb_mode.attr,
933 	&dev_attr_kbd_rgb_mode_index.attr.attr,
934 	NULL,
935 };
936 
937 static const struct attribute_group kbd_rgb_mode_group = {
938 	.attrs = kbd_rgb_mode_attrs,
939 };
940 
941 /* TUF Laptop Keyboard RGB State **********************************************/
942 static ssize_t kbd_rgb_state_store(struct device *dev,
943 				 struct device_attribute *attr,
944 				 const char *buf, size_t count)
945 {
946 	u32 flags, cmd, boot, awake, sleep, keyboard;
947 	int err;
948 
949 	if (sscanf(buf, "%d %d %d %d %d", &cmd, &boot, &awake, &sleep, &keyboard) != 5)
950 		return -EINVAL;
951 
952 	if (cmd)
953 		cmd = BIT(2);
954 
955 	flags = 0;
956 	if (boot)
957 		flags |= BIT(1);
958 	if (awake)
959 		flags |= BIT(3);
960 	if (sleep)
961 		flags |= BIT(5);
962 	if (keyboard)
963 		flags |= BIT(7);
964 
965 	/* 0xbd is the required default arg0 for the method. Nothing happens otherwise */
966 	err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS,
967 			ASUS_WMI_DEVID_TUF_RGB_STATE, 0xbd | cmd << 8 | (flags << 16), 0, NULL);
968 	if (err)
969 		return err;
970 
971 	return count;
972 }
973 static DEVICE_ATTR_WO(kbd_rgb_state);
974 
975 static DEVICE_STRING_ATTR_RO(kbd_rgb_state_index, 0444,
976 			     "cmd boot awake sleep keyboard");
977 
978 static struct attribute *kbd_rgb_state_attrs[] = {
979 	&dev_attr_kbd_rgb_state.attr,
980 	&dev_attr_kbd_rgb_state_index.attr.attr,
981 	NULL,
982 };
983 
984 static const struct attribute_group kbd_rgb_state_group = {
985 	.attrs = kbd_rgb_state_attrs,
986 };
987 
988 static const struct attribute_group *kbd_rgb_mode_groups[] = {
989 	NULL,
990 	NULL,
991 	NULL,
992 };
993 
994 /* Tunable: PPT: Intel=PL1, AMD=SPPT *****************************************/
995 static ssize_t ppt_pl2_sppt_store(struct device *dev,
996 				    struct device_attribute *attr,
997 				    const char *buf, size_t count)
998 {
999 	struct asus_wmi *asus = dev_get_drvdata(dev);
1000 	int result, err;
1001 	u32 value;
1002 
1003 	result = kstrtou32(buf, 10, &value);
1004 	if (result)
1005 		return result;
1006 
1007 	if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
1008 		return -EINVAL;
1009 
1010 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL2_SPPT, value, &result);
1011 	if (err) {
1012 		pr_warn("Failed to set ppt_pl2_sppt: %d\n", err);
1013 		return err;
1014 	}
1015 
1016 	if (result > 1) {
1017 		pr_warn("Failed to set ppt_pl2_sppt (result): 0x%x\n", result);
1018 		return -EIO;
1019 	}
1020 
1021 	asus->ppt_pl2_sppt = value;
1022 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl2_sppt");
1023 
1024 	return count;
1025 }
1026 
1027 static ssize_t ppt_pl2_sppt_show(struct device *dev,
1028 				       struct device_attribute *attr,
1029 				       char *buf)
1030 {
1031 	struct asus_wmi *asus = dev_get_drvdata(dev);
1032 
1033 	return sysfs_emit(buf, "%u\n", asus->ppt_pl2_sppt);
1034 }
1035 static DEVICE_ATTR_RW(ppt_pl2_sppt);
1036 
1037 /* Tunable: PPT, Intel=PL1, AMD=SPL ******************************************/
1038 static ssize_t ppt_pl1_spl_store(struct device *dev,
1039 				    struct device_attribute *attr,
1040 				    const char *buf, size_t count)
1041 {
1042 	struct asus_wmi *asus = dev_get_drvdata(dev);
1043 	int result, err;
1044 	u32 value;
1045 
1046 	result = kstrtou32(buf, 10, &value);
1047 	if (result)
1048 		return result;
1049 
1050 	if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
1051 		return -EINVAL;
1052 
1053 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL1_SPL, value, &result);
1054 	if (err) {
1055 		pr_warn("Failed to set ppt_pl1_spl: %d\n", err);
1056 		return err;
1057 	}
1058 
1059 	if (result > 1) {
1060 		pr_warn("Failed to set ppt_pl1_spl (result): 0x%x\n", result);
1061 		return -EIO;
1062 	}
1063 
1064 	asus->ppt_pl1_spl = value;
1065 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl1_spl");
1066 
1067 	return count;
1068 }
1069 static ssize_t ppt_pl1_spl_show(struct device *dev,
1070 				 struct device_attribute *attr,
1071 				 char *buf)
1072 {
1073 	struct asus_wmi *asus = dev_get_drvdata(dev);
1074 
1075 	return sysfs_emit(buf, "%u\n", asus->ppt_pl1_spl);
1076 }
1077 static DEVICE_ATTR_RW(ppt_pl1_spl);
1078 
1079 /* Tunable: PPT APU FPPT ******************************************************/
1080 static ssize_t ppt_fppt_store(struct device *dev,
1081 				    struct device_attribute *attr,
1082 				    const char *buf, size_t count)
1083 {
1084 	struct asus_wmi *asus = dev_get_drvdata(dev);
1085 	int result, err;
1086 	u32 value;
1087 
1088 	result = kstrtou32(buf, 10, &value);
1089 	if (result)
1090 		return result;
1091 
1092 	if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
1093 		return -EINVAL;
1094 
1095 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_FPPT, value, &result);
1096 	if (err) {
1097 		pr_warn("Failed to set ppt_fppt: %d\n", err);
1098 		return err;
1099 	}
1100 
1101 	if (result > 1) {
1102 		pr_warn("Failed to set ppt_fppt (result): 0x%x\n", result);
1103 		return -EIO;
1104 	}
1105 
1106 	asus->ppt_fppt = value;
1107 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_fpu_sppt");
1108 
1109 	return count;
1110 }
1111 
1112 static ssize_t ppt_fppt_show(struct device *dev,
1113 				struct device_attribute *attr,
1114 				char *buf)
1115 {
1116 	struct asus_wmi *asus = dev_get_drvdata(dev);
1117 
1118 	return sysfs_emit(buf, "%u\n", asus->ppt_fppt);
1119 }
1120 static DEVICE_ATTR_RW(ppt_fppt);
1121 
1122 /* Tunable: PPT APU SPPT *****************************************************/
1123 static ssize_t ppt_apu_sppt_store(struct device *dev,
1124 				    struct device_attribute *attr,
1125 				    const char *buf, size_t count)
1126 {
1127 	struct asus_wmi *asus = dev_get_drvdata(dev);
1128 	int result, err;
1129 	u32 value;
1130 
1131 	result = kstrtou32(buf, 10, &value);
1132 	if (result)
1133 		return result;
1134 
1135 	if (value < PPT_CPU_MIN || value > PPT_CPU_MAX)
1136 		return -EINVAL;
1137 
1138 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_APU_SPPT, value, &result);
1139 	if (err) {
1140 		pr_warn("Failed to set ppt_apu_sppt: %d\n", err);
1141 		return err;
1142 	}
1143 
1144 	if (result > 1) {
1145 		pr_warn("Failed to set ppt_apu_sppt (result): 0x%x\n", result);
1146 		return -EIO;
1147 	}
1148 
1149 	asus->ppt_apu_sppt = value;
1150 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_apu_sppt");
1151 
1152 	return count;
1153 }
1154 
1155 static ssize_t ppt_apu_sppt_show(struct device *dev,
1156 			     struct device_attribute *attr,
1157 			     char *buf)
1158 {
1159 	struct asus_wmi *asus = dev_get_drvdata(dev);
1160 
1161 	return sysfs_emit(buf, "%u\n", asus->ppt_apu_sppt);
1162 }
1163 static DEVICE_ATTR_RW(ppt_apu_sppt);
1164 
1165 /* Tunable: PPT platform SPPT ************************************************/
1166 static ssize_t ppt_platform_sppt_store(struct device *dev,
1167 				    struct device_attribute *attr,
1168 				    const char *buf, size_t count)
1169 {
1170 	struct asus_wmi *asus = dev_get_drvdata(dev);
1171 	int result, err;
1172 	u32 value;
1173 
1174 	result = kstrtou32(buf, 10, &value);
1175 	if (result)
1176 		return result;
1177 
1178 	if (value < PPT_CPU_MIN || value > PPT_CPU_MAX)
1179 		return -EINVAL;
1180 
1181 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PLAT_SPPT, value, &result);
1182 	if (err) {
1183 		pr_warn("Failed to set ppt_platform_sppt: %d\n", err);
1184 		return err;
1185 	}
1186 
1187 	if (result > 1) {
1188 		pr_warn("Failed to set ppt_platform_sppt (result): 0x%x\n", result);
1189 		return -EIO;
1190 	}
1191 
1192 	asus->ppt_platform_sppt = value;
1193 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_platform_sppt");
1194 
1195 	return count;
1196 }
1197 
1198 static ssize_t ppt_platform_sppt_show(struct device *dev,
1199 				 struct device_attribute *attr,
1200 				 char *buf)
1201 {
1202 	struct asus_wmi *asus = dev_get_drvdata(dev);
1203 
1204 	return sysfs_emit(buf, "%u\n", asus->ppt_platform_sppt);
1205 }
1206 static DEVICE_ATTR_RW(ppt_platform_sppt);
1207 
1208 /* Tunable: NVIDIA dynamic boost *********************************************/
1209 static ssize_t nv_dynamic_boost_store(struct device *dev,
1210 				    struct device_attribute *attr,
1211 				    const char *buf, size_t count)
1212 {
1213 	struct asus_wmi *asus = dev_get_drvdata(dev);
1214 	int result, err;
1215 	u32 value;
1216 
1217 	result = kstrtou32(buf, 10, &value);
1218 	if (result)
1219 		return result;
1220 
1221 	if (value < NVIDIA_BOOST_MIN || value > NVIDIA_BOOST_MAX)
1222 		return -EINVAL;
1223 
1224 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_DYN_BOOST, value, &result);
1225 	if (err) {
1226 		pr_warn("Failed to set nv_dynamic_boost: %d\n", err);
1227 		return err;
1228 	}
1229 
1230 	if (result > 1) {
1231 		pr_warn("Failed to set nv_dynamic_boost (result): 0x%x\n", result);
1232 		return -EIO;
1233 	}
1234 
1235 	asus->nv_dynamic_boost = value;
1236 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_dynamic_boost");
1237 
1238 	return count;
1239 }
1240 
1241 static ssize_t nv_dynamic_boost_show(struct device *dev,
1242 				      struct device_attribute *attr,
1243 				      char *buf)
1244 {
1245 	struct asus_wmi *asus = dev_get_drvdata(dev);
1246 
1247 	return sysfs_emit(buf, "%u\n", asus->nv_dynamic_boost);
1248 }
1249 static DEVICE_ATTR_RW(nv_dynamic_boost);
1250 
1251 /* Tunable: NVIDIA temperature target ****************************************/
1252 static ssize_t nv_temp_target_store(struct device *dev,
1253 				    struct device_attribute *attr,
1254 				    const char *buf, size_t count)
1255 {
1256 	struct asus_wmi *asus = dev_get_drvdata(dev);
1257 	int result, err;
1258 	u32 value;
1259 
1260 	result = kstrtou32(buf, 10, &value);
1261 	if (result)
1262 		return result;
1263 
1264 	if (value < NVIDIA_TEMP_MIN || value > NVIDIA_TEMP_MAX)
1265 		return -EINVAL;
1266 
1267 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_THERM_TARGET, value, &result);
1268 	if (err) {
1269 		pr_warn("Failed to set nv_temp_target: %d\n", err);
1270 		return err;
1271 	}
1272 
1273 	if (result > 1) {
1274 		pr_warn("Failed to set nv_temp_target (result): 0x%x\n", result);
1275 		return -EIO;
1276 	}
1277 
1278 	asus->nv_temp_target = value;
1279 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_temp_target");
1280 
1281 	return count;
1282 }
1283 
1284 static ssize_t nv_temp_target_show(struct device *dev,
1285 				     struct device_attribute *attr,
1286 				     char *buf)
1287 {
1288 	struct asus_wmi *asus = dev_get_drvdata(dev);
1289 
1290 	return sysfs_emit(buf, "%u\n", asus->nv_temp_target);
1291 }
1292 static DEVICE_ATTR_RW(nv_temp_target);
1293 
1294 /* Ally MCU Powersave ********************************************************/
1295 static ssize_t mcu_powersave_show(struct device *dev,
1296 				   struct device_attribute *attr, char *buf)
1297 {
1298 	struct asus_wmi *asus = dev_get_drvdata(dev);
1299 	int result;
1300 
1301 	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_MCU_POWERSAVE);
1302 	if (result < 0)
1303 		return result;
1304 
1305 	return sysfs_emit(buf, "%d\n", result);
1306 }
1307 
1308 static ssize_t mcu_powersave_store(struct device *dev,
1309 				    struct device_attribute *attr,
1310 				    const char *buf, size_t count)
1311 {
1312 	int result, err;
1313 	u32 enable;
1314 
1315 	struct asus_wmi *asus = dev_get_drvdata(dev);
1316 
1317 	result = kstrtou32(buf, 10, &enable);
1318 	if (result)
1319 		return result;
1320 
1321 	if (enable > 1)
1322 		return -EINVAL;
1323 
1324 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MCU_POWERSAVE, enable, &result);
1325 	if (err) {
1326 		pr_warn("Failed to set MCU powersave: %d\n", err);
1327 		return err;
1328 	}
1329 
1330 	if (result > 1) {
1331 		pr_warn("Failed to set MCU powersave (result): 0x%x\n", result);
1332 		return -EIO;
1333 	}
1334 
1335 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "mcu_powersave");
1336 
1337 	return count;
1338 }
1339 static DEVICE_ATTR_RW(mcu_powersave);
1340 
1341 /* Battery ********************************************************************/
1342 
1343 /* The battery maximum charging percentage */
1344 static int charge_end_threshold;
1345 
1346 static ssize_t charge_control_end_threshold_store(struct device *dev,
1347 						  struct device_attribute *attr,
1348 						  const char *buf, size_t count)
1349 {
1350 	int value, ret, rv;
1351 
1352 	ret = kstrtouint(buf, 10, &value);
1353 	if (ret)
1354 		return ret;
1355 
1356 	if (value < 0 || value > 100)
1357 		return -EINVAL;
1358 
1359 	ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
1360 	if (ret)
1361 		return ret;
1362 
1363 	if (rv != 1)
1364 		return -EIO;
1365 
1366 	/* There isn't any method in the DSDT to read the threshold, so we
1367 	 * save the threshold.
1368 	 */
1369 	charge_end_threshold = value;
1370 	return count;
1371 }
1372 
1373 static ssize_t charge_control_end_threshold_show(struct device *device,
1374 						 struct device_attribute *attr,
1375 						 char *buf)
1376 {
1377 	return sysfs_emit(buf, "%d\n", charge_end_threshold);
1378 }
1379 
1380 static DEVICE_ATTR_RW(charge_control_end_threshold);
1381 
1382 static int asus_wmi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
1383 {
1384 	/* The WMI method does not provide a way to specific a battery, so we
1385 	 * just assume it is the first battery.
1386 	 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
1387 	 * battery is named BATT.
1388 	 */
1389 	if (strcmp(battery->desc->name, "BAT0") != 0 &&
1390 	    strcmp(battery->desc->name, "BAT1") != 0 &&
1391 	    strcmp(battery->desc->name, "BATC") != 0 &&
1392 	    strcmp(battery->desc->name, "BATT") != 0)
1393 		return -ENODEV;
1394 
1395 	if (device_create_file(&battery->dev,
1396 	    &dev_attr_charge_control_end_threshold))
1397 		return -ENODEV;
1398 
1399 	/* The charge threshold is only reset when the system is power cycled,
1400 	 * and we can't get the current threshold so let set it to 100% when
1401 	 * a battery is added.
1402 	 */
1403 	asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
1404 	charge_end_threshold = 100;
1405 
1406 	return 0;
1407 }
1408 
1409 static int asus_wmi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
1410 {
1411 	device_remove_file(&battery->dev,
1412 			   &dev_attr_charge_control_end_threshold);
1413 	return 0;
1414 }
1415 
1416 static struct acpi_battery_hook battery_hook = {
1417 	.add_battery = asus_wmi_battery_add,
1418 	.remove_battery = asus_wmi_battery_remove,
1419 	.name = "ASUS Battery Extension",
1420 };
1421 
1422 static void asus_wmi_battery_init(struct asus_wmi *asus)
1423 {
1424 	asus->battery_rsoc_available = false;
1425 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
1426 		asus->battery_rsoc_available = true;
1427 		battery_hook_register(&battery_hook);
1428 	}
1429 }
1430 
1431 static void asus_wmi_battery_exit(struct asus_wmi *asus)
1432 {
1433 	if (asus->battery_rsoc_available)
1434 		battery_hook_unregister(&battery_hook);
1435 }
1436 
1437 /* LEDs ***********************************************************************/
1438 
1439 /*
1440  * These functions actually update the LED's, and are called from a
1441  * workqueue. By doing this as separate work rather than when the LED
1442  * subsystem asks, we avoid messing with the Asus ACPI stuff during a
1443  * potentially bad time, such as a timer interrupt.
1444  */
1445 static void tpd_led_update(struct work_struct *work)
1446 {
1447 	int ctrl_param;
1448 	struct asus_wmi *asus;
1449 
1450 	asus = container_of(work, struct asus_wmi, tpd_led_work);
1451 
1452 	ctrl_param = asus->tpd_led_wk;
1453 	asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
1454 }
1455 
1456 static void tpd_led_set(struct led_classdev *led_cdev,
1457 			enum led_brightness value)
1458 {
1459 	struct asus_wmi *asus;
1460 
1461 	asus = container_of(led_cdev, struct asus_wmi, tpd_led);
1462 
1463 	asus->tpd_led_wk = !!value;
1464 	queue_work(asus->led_workqueue, &asus->tpd_led_work);
1465 }
1466 
1467 static int read_tpd_led_state(struct asus_wmi *asus)
1468 {
1469 	return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
1470 }
1471 
1472 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
1473 {
1474 	struct asus_wmi *asus;
1475 
1476 	asus = container_of(led_cdev, struct asus_wmi, tpd_led);
1477 
1478 	return read_tpd_led_state(asus);
1479 }
1480 
1481 static void kbd_led_update(struct asus_wmi *asus)
1482 {
1483 	int ctrl_param = 0;
1484 
1485 	ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
1486 	asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
1487 }
1488 
1489 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
1490 {
1491 	int retval;
1492 
1493 	/*
1494 	 * bits 0-2: level
1495 	 * bit 7: light on/off
1496 	 * bit 8-10: environment (0: dark, 1: normal, 2: light)
1497 	 * bit 17: status unknown
1498 	 */
1499 	retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
1500 					    0xFFFF);
1501 
1502 	/* Unknown status is considered as off */
1503 	if (retval == 0x8000)
1504 		retval = 0;
1505 
1506 	if (retval < 0)
1507 		return retval;
1508 
1509 	if (level)
1510 		*level = retval & 0x7F;
1511 	if (env)
1512 		*env = (retval >> 8) & 0x7F;
1513 	return 0;
1514 }
1515 
1516 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
1517 {
1518 	struct asus_wmi *asus;
1519 	int max_level;
1520 
1521 	asus = container_of(led_cdev, struct asus_wmi, kbd_led);
1522 	max_level = asus->kbd_led.max_brightness;
1523 
1524 	asus->kbd_led_wk = clamp_val(value, 0, max_level);
1525 	kbd_led_update(asus);
1526 }
1527 
1528 static void kbd_led_set(struct led_classdev *led_cdev,
1529 			enum led_brightness value)
1530 {
1531 	/* Prevent disabling keyboard backlight on module unregister */
1532 	if (led_cdev->flags & LED_UNREGISTERING)
1533 		return;
1534 
1535 	do_kbd_led_set(led_cdev, value);
1536 }
1537 
1538 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
1539 {
1540 	struct led_classdev *led_cdev = &asus->kbd_led;
1541 
1542 	do_kbd_led_set(led_cdev, value);
1543 	led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
1544 }
1545 
1546 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
1547 {
1548 	struct asus_wmi *asus;
1549 	int retval, value;
1550 
1551 	asus = container_of(led_cdev, struct asus_wmi, kbd_led);
1552 
1553 	retval = kbd_led_read(asus, &value, NULL);
1554 	if (retval < 0)
1555 		return retval;
1556 
1557 	return value;
1558 }
1559 
1560 static int wlan_led_unknown_state(struct asus_wmi *asus)
1561 {
1562 	u32 result;
1563 
1564 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
1565 
1566 	return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
1567 }
1568 
1569 static void wlan_led_update(struct work_struct *work)
1570 {
1571 	int ctrl_param;
1572 	struct asus_wmi *asus;
1573 
1574 	asus = container_of(work, struct asus_wmi, wlan_led_work);
1575 
1576 	ctrl_param = asus->wlan_led_wk;
1577 	asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
1578 }
1579 
1580 static void wlan_led_set(struct led_classdev *led_cdev,
1581 			 enum led_brightness value)
1582 {
1583 	struct asus_wmi *asus;
1584 
1585 	asus = container_of(led_cdev, struct asus_wmi, wlan_led);
1586 
1587 	asus->wlan_led_wk = !!value;
1588 	queue_work(asus->led_workqueue, &asus->wlan_led_work);
1589 }
1590 
1591 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
1592 {
1593 	struct asus_wmi *asus;
1594 	u32 result;
1595 
1596 	asus = container_of(led_cdev, struct asus_wmi, wlan_led);
1597 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
1598 
1599 	return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1600 }
1601 
1602 static void lightbar_led_update(struct work_struct *work)
1603 {
1604 	struct asus_wmi *asus;
1605 	int ctrl_param;
1606 
1607 	asus = container_of(work, struct asus_wmi, lightbar_led_work);
1608 
1609 	ctrl_param = asus->lightbar_led_wk;
1610 	asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
1611 }
1612 
1613 static void lightbar_led_set(struct led_classdev *led_cdev,
1614 			     enum led_brightness value)
1615 {
1616 	struct asus_wmi *asus;
1617 
1618 	asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1619 
1620 	asus->lightbar_led_wk = !!value;
1621 	queue_work(asus->led_workqueue, &asus->lightbar_led_work);
1622 }
1623 
1624 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
1625 {
1626 	struct asus_wmi *asus;
1627 	u32 result;
1628 
1629 	asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1630 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
1631 
1632 	return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
1633 }
1634 
1635 static int micmute_led_set(struct led_classdev *led_cdev,
1636 			   enum led_brightness brightness)
1637 {
1638 	int state = brightness != LED_OFF;
1639 	int err;
1640 
1641 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MICMUTE_LED, state, NULL);
1642 	return err < 0 ? err : 0;
1643 }
1644 
1645 static void asus_wmi_led_exit(struct asus_wmi *asus)
1646 {
1647 	led_classdev_unregister(&asus->kbd_led);
1648 	led_classdev_unregister(&asus->tpd_led);
1649 	led_classdev_unregister(&asus->wlan_led);
1650 	led_classdev_unregister(&asus->lightbar_led);
1651 	led_classdev_unregister(&asus->micmute_led);
1652 
1653 	if (asus->led_workqueue)
1654 		destroy_workqueue(asus->led_workqueue);
1655 }
1656 
1657 static int asus_wmi_led_init(struct asus_wmi *asus)
1658 {
1659 	int rv = 0, num_rgb_groups = 0, led_val;
1660 
1661 	if (asus->kbd_rgb_dev)
1662 		kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_mode_group;
1663 	if (asus->kbd_rgb_state_available)
1664 		kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_state_group;
1665 
1666 	asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
1667 	if (!asus->led_workqueue)
1668 		return -ENOMEM;
1669 
1670 	if (read_tpd_led_state(asus) >= 0) {
1671 		INIT_WORK(&asus->tpd_led_work, tpd_led_update);
1672 
1673 		asus->tpd_led.name = "asus::touchpad";
1674 		asus->tpd_led.brightness_set = tpd_led_set;
1675 		asus->tpd_led.brightness_get = tpd_led_get;
1676 		asus->tpd_led.max_brightness = 1;
1677 
1678 		rv = led_classdev_register(&asus->platform_device->dev,
1679 					   &asus->tpd_led);
1680 		if (rv)
1681 			goto error;
1682 	}
1683 
1684 	if (!kbd_led_read(asus, &led_val, NULL)) {
1685 		asus->kbd_led_wk = led_val;
1686 		asus->kbd_led.name = "asus::kbd_backlight";
1687 		asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
1688 		asus->kbd_led.brightness_set = kbd_led_set;
1689 		asus->kbd_led.brightness_get = kbd_led_get;
1690 		asus->kbd_led.max_brightness = 3;
1691 
1692 		if (num_rgb_groups != 0)
1693 			asus->kbd_led.groups = kbd_rgb_mode_groups;
1694 
1695 		rv = led_classdev_register(&asus->platform_device->dev,
1696 					   &asus->kbd_led);
1697 		if (rv)
1698 			goto error;
1699 	}
1700 
1701 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
1702 			&& (asus->driver->quirks->wapf > 0)) {
1703 		INIT_WORK(&asus->wlan_led_work, wlan_led_update);
1704 
1705 		asus->wlan_led.name = "asus::wlan";
1706 		asus->wlan_led.brightness_set = wlan_led_set;
1707 		if (!wlan_led_unknown_state(asus))
1708 			asus->wlan_led.brightness_get = wlan_led_get;
1709 		asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
1710 		asus->wlan_led.max_brightness = 1;
1711 		asus->wlan_led.default_trigger = "asus-wlan";
1712 
1713 		rv = led_classdev_register(&asus->platform_device->dev,
1714 					   &asus->wlan_led);
1715 		if (rv)
1716 			goto error;
1717 	}
1718 
1719 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
1720 		INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
1721 
1722 		asus->lightbar_led.name = "asus::lightbar";
1723 		asus->lightbar_led.brightness_set = lightbar_led_set;
1724 		asus->lightbar_led.brightness_get = lightbar_led_get;
1725 		asus->lightbar_led.max_brightness = 1;
1726 
1727 		rv = led_classdev_register(&asus->platform_device->dev,
1728 					   &asus->lightbar_led);
1729 	}
1730 
1731 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MICMUTE_LED)) {
1732 		asus->micmute_led.name = "platform::micmute";
1733 		asus->micmute_led.max_brightness = 1;
1734 		asus->micmute_led.brightness_set_blocking = micmute_led_set;
1735 		asus->micmute_led.default_trigger = "audio-micmute";
1736 
1737 		rv = led_classdev_register(&asus->platform_device->dev,
1738 						&asus->micmute_led);
1739 		if (rv)
1740 			goto error;
1741 	}
1742 
1743 error:
1744 	if (rv)
1745 		asus_wmi_led_exit(asus);
1746 
1747 	return rv;
1748 }
1749 
1750 /* RF *************************************************************************/
1751 
1752 /*
1753  * PCI hotplug (for wlan rfkill)
1754  */
1755 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
1756 {
1757 	int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1758 
1759 	if (result < 0)
1760 		return false;
1761 	return !result;
1762 }
1763 
1764 static void asus_rfkill_hotplug(struct asus_wmi *asus)
1765 {
1766 	struct pci_dev *dev;
1767 	struct pci_bus *bus;
1768 	bool blocked;
1769 	bool absent;
1770 	u32 l;
1771 
1772 	mutex_lock(&asus->wmi_lock);
1773 	blocked = asus_wlan_rfkill_blocked(asus);
1774 	mutex_unlock(&asus->wmi_lock);
1775 
1776 	mutex_lock(&asus->hotplug_lock);
1777 	pci_lock_rescan_remove();
1778 
1779 	if (asus->wlan.rfkill)
1780 		rfkill_set_sw_state(asus->wlan.rfkill, blocked);
1781 
1782 	if (asus->hotplug_slot.ops) {
1783 		bus = pci_find_bus(0, 1);
1784 		if (!bus) {
1785 			pr_warn("Unable to find PCI bus 1?\n");
1786 			goto out_unlock;
1787 		}
1788 
1789 		if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
1790 			pr_err("Unable to read PCI config space?\n");
1791 			goto out_unlock;
1792 		}
1793 		absent = (l == 0xffffffff);
1794 
1795 		if (blocked != absent) {
1796 			pr_warn("BIOS says wireless lan is %s, but the pci device is %s\n",
1797 				blocked ? "blocked" : "unblocked",
1798 				absent ? "absent" : "present");
1799 			pr_warn("skipped wireless hotplug as probably inappropriate for this model\n");
1800 			goto out_unlock;
1801 		}
1802 
1803 		if (!blocked) {
1804 			dev = pci_get_slot(bus, 0);
1805 			if (dev) {
1806 				/* Device already present */
1807 				pci_dev_put(dev);
1808 				goto out_unlock;
1809 			}
1810 			dev = pci_scan_single_device(bus, 0);
1811 			if (dev) {
1812 				pci_bus_assign_resources(bus);
1813 				pci_bus_add_device(dev);
1814 			}
1815 		} else {
1816 			dev = pci_get_slot(bus, 0);
1817 			if (dev) {
1818 				pci_stop_and_remove_bus_device(dev);
1819 				pci_dev_put(dev);
1820 			}
1821 		}
1822 	}
1823 
1824 out_unlock:
1825 	pci_unlock_rescan_remove();
1826 	mutex_unlock(&asus->hotplug_lock);
1827 }
1828 
1829 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
1830 {
1831 	struct asus_wmi *asus = data;
1832 
1833 	if (event != ACPI_NOTIFY_BUS_CHECK)
1834 		return;
1835 
1836 	/*
1837 	 * We can't call directly asus_rfkill_hotplug because most
1838 	 * of the time WMBC is still being executed and not reetrant.
1839 	 * There is currently no way to tell ACPICA that  we want this
1840 	 * method to be serialized, we schedule a asus_rfkill_hotplug
1841 	 * call later, in a safer context.
1842 	 */
1843 	queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
1844 }
1845 
1846 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
1847 {
1848 	acpi_status status;
1849 	acpi_handle handle;
1850 
1851 	status = acpi_get_handle(NULL, node, &handle);
1852 	if (ACPI_FAILURE(status))
1853 		return -ENODEV;
1854 
1855 	status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1856 					     asus_rfkill_notify, asus);
1857 	if (ACPI_FAILURE(status))
1858 		pr_warn("Failed to register notify on %s\n", node);
1859 
1860 	return 0;
1861 }
1862 
1863 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
1864 {
1865 	acpi_status status = AE_OK;
1866 	acpi_handle handle;
1867 
1868 	status = acpi_get_handle(NULL, node, &handle);
1869 	if (ACPI_FAILURE(status))
1870 		return;
1871 
1872 	status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1873 					    asus_rfkill_notify);
1874 	if (ACPI_FAILURE(status))
1875 		pr_err("Error removing rfkill notify handler %s\n", node);
1876 }
1877 
1878 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
1879 				   u8 *value)
1880 {
1881 	struct asus_wmi *asus = container_of(hotplug_slot,
1882 					     struct asus_wmi, hotplug_slot);
1883 	int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1884 
1885 	if (result < 0)
1886 		return result;
1887 
1888 	*value = !!result;
1889 	return 0;
1890 }
1891 
1892 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
1893 	.get_adapter_status = asus_get_adapter_status,
1894 	.get_power_status = asus_get_adapter_status,
1895 };
1896 
1897 static void asus_hotplug_work(struct work_struct *work)
1898 {
1899 	struct asus_wmi *asus;
1900 
1901 	asus = container_of(work, struct asus_wmi, hotplug_work);
1902 	asus_rfkill_hotplug(asus);
1903 }
1904 
1905 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
1906 {
1907 	int ret = -ENOMEM;
1908 	struct pci_bus *bus = pci_find_bus(0, 1);
1909 
1910 	if (!bus) {
1911 		pr_err("Unable to find wifi PCI bus\n");
1912 		return -ENODEV;
1913 	}
1914 
1915 	asus->hotplug_workqueue =
1916 	    create_singlethread_workqueue("hotplug_workqueue");
1917 	if (!asus->hotplug_workqueue)
1918 		goto error_workqueue;
1919 
1920 	INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
1921 
1922 	asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
1923 
1924 	ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
1925 	if (ret) {
1926 		pr_err("Unable to register hotplug slot - %d\n", ret);
1927 		goto error_register;
1928 	}
1929 
1930 	return 0;
1931 
1932 error_register:
1933 	asus->hotplug_slot.ops = NULL;
1934 	destroy_workqueue(asus->hotplug_workqueue);
1935 error_workqueue:
1936 	return ret;
1937 }
1938 
1939 /*
1940  * Rfkill devices
1941  */
1942 static int asus_rfkill_set(void *data, bool blocked)
1943 {
1944 	struct asus_rfkill *priv = data;
1945 	u32 ctrl_param = !blocked;
1946 	u32 dev_id = priv->dev_id;
1947 
1948 	/*
1949 	 * If the user bit is set, BIOS can't set and record the wlan status,
1950 	 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
1951 	 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
1952 	 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
1953 	 * while setting the wlan status through WMI.
1954 	 * This is also the behavior that windows app will do.
1955 	 */
1956 	if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1957 	     priv->asus->driver->wlan_ctrl_by_user)
1958 		dev_id = ASUS_WMI_DEVID_WLAN_LED;
1959 
1960 	return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
1961 }
1962 
1963 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
1964 {
1965 	struct asus_rfkill *priv = data;
1966 	int result;
1967 
1968 	result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
1969 
1970 	if (result < 0)
1971 		return;
1972 
1973 	rfkill_set_sw_state(priv->rfkill, !result);
1974 }
1975 
1976 static int asus_rfkill_wlan_set(void *data, bool blocked)
1977 {
1978 	struct asus_rfkill *priv = data;
1979 	struct asus_wmi *asus = priv->asus;
1980 	int ret;
1981 
1982 	/*
1983 	 * This handler is enabled only if hotplug is enabled.
1984 	 * In this case, the asus_wmi_set_devstate() will
1985 	 * trigger a wmi notification and we need to wait
1986 	 * this call to finish before being able to call
1987 	 * any wmi method
1988 	 */
1989 	mutex_lock(&asus->wmi_lock);
1990 	ret = asus_rfkill_set(data, blocked);
1991 	mutex_unlock(&asus->wmi_lock);
1992 	return ret;
1993 }
1994 
1995 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1996 	.set_block = asus_rfkill_wlan_set,
1997 	.query = asus_rfkill_query,
1998 };
1999 
2000 static const struct rfkill_ops asus_rfkill_ops = {
2001 	.set_block = asus_rfkill_set,
2002 	.query = asus_rfkill_query,
2003 };
2004 
2005 static int asus_new_rfkill(struct asus_wmi *asus,
2006 			   struct asus_rfkill *arfkill,
2007 			   const char *name, enum rfkill_type type, int dev_id)
2008 {
2009 	int result = asus_wmi_get_devstate_simple(asus, dev_id);
2010 	struct rfkill **rfkill = &arfkill->rfkill;
2011 
2012 	if (result < 0)
2013 		return result;
2014 
2015 	arfkill->dev_id = dev_id;
2016 	arfkill->asus = asus;
2017 
2018 	if (dev_id == ASUS_WMI_DEVID_WLAN &&
2019 	    asus->driver->quirks->hotplug_wireless)
2020 		*rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
2021 				       &asus_rfkill_wlan_ops, arfkill);
2022 	else
2023 		*rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
2024 				       &asus_rfkill_ops, arfkill);
2025 
2026 	if (!*rfkill)
2027 		return -EINVAL;
2028 
2029 	if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
2030 			(asus->driver->quirks->wapf > 0))
2031 		rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
2032 
2033 	rfkill_init_sw_state(*rfkill, !result);
2034 	result = rfkill_register(*rfkill);
2035 	if (result) {
2036 		rfkill_destroy(*rfkill);
2037 		*rfkill = NULL;
2038 		return result;
2039 	}
2040 	return 0;
2041 }
2042 
2043 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
2044 {
2045 	if (asus->driver->wlan_ctrl_by_user && ashs_present())
2046 		return;
2047 
2048 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
2049 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
2050 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
2051 	if (asus->wlan.rfkill) {
2052 		rfkill_unregister(asus->wlan.rfkill);
2053 		rfkill_destroy(asus->wlan.rfkill);
2054 		asus->wlan.rfkill = NULL;
2055 	}
2056 	/*
2057 	 * Refresh pci hotplug in case the rfkill state was changed after
2058 	 * asus_unregister_rfkill_notifier()
2059 	 */
2060 	asus_rfkill_hotplug(asus);
2061 	if (asus->hotplug_slot.ops)
2062 		pci_hp_deregister(&asus->hotplug_slot);
2063 	if (asus->hotplug_workqueue)
2064 		destroy_workqueue(asus->hotplug_workqueue);
2065 
2066 	if (asus->bluetooth.rfkill) {
2067 		rfkill_unregister(asus->bluetooth.rfkill);
2068 		rfkill_destroy(asus->bluetooth.rfkill);
2069 		asus->bluetooth.rfkill = NULL;
2070 	}
2071 	if (asus->wimax.rfkill) {
2072 		rfkill_unregister(asus->wimax.rfkill);
2073 		rfkill_destroy(asus->wimax.rfkill);
2074 		asus->wimax.rfkill = NULL;
2075 	}
2076 	if (asus->wwan3g.rfkill) {
2077 		rfkill_unregister(asus->wwan3g.rfkill);
2078 		rfkill_destroy(asus->wwan3g.rfkill);
2079 		asus->wwan3g.rfkill = NULL;
2080 	}
2081 	if (asus->gps.rfkill) {
2082 		rfkill_unregister(asus->gps.rfkill);
2083 		rfkill_destroy(asus->gps.rfkill);
2084 		asus->gps.rfkill = NULL;
2085 	}
2086 	if (asus->uwb.rfkill) {
2087 		rfkill_unregister(asus->uwb.rfkill);
2088 		rfkill_destroy(asus->uwb.rfkill);
2089 		asus->uwb.rfkill = NULL;
2090 	}
2091 }
2092 
2093 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
2094 {
2095 	int result = 0;
2096 
2097 	mutex_init(&asus->hotplug_lock);
2098 	mutex_init(&asus->wmi_lock);
2099 
2100 	result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
2101 				 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
2102 
2103 	if (result && result != -ENODEV)
2104 		goto exit;
2105 
2106 	result = asus_new_rfkill(asus, &asus->bluetooth,
2107 				 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
2108 				 ASUS_WMI_DEVID_BLUETOOTH);
2109 
2110 	if (result && result != -ENODEV)
2111 		goto exit;
2112 
2113 	result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
2114 				 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
2115 
2116 	if (result && result != -ENODEV)
2117 		goto exit;
2118 
2119 	result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
2120 				 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
2121 
2122 	if (result && result != -ENODEV)
2123 		goto exit;
2124 
2125 	result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
2126 				 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
2127 
2128 	if (result && result != -ENODEV)
2129 		goto exit;
2130 
2131 	result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
2132 				 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
2133 
2134 	if (result && result != -ENODEV)
2135 		goto exit;
2136 
2137 	if (!asus->driver->quirks->hotplug_wireless)
2138 		goto exit;
2139 
2140 	result = asus_setup_pci_hotplug(asus);
2141 	/*
2142 	 * If we get -EBUSY then something else is handling the PCI hotplug -
2143 	 * don't fail in this case
2144 	 */
2145 	if (result == -EBUSY)
2146 		result = 0;
2147 
2148 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
2149 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
2150 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
2151 	/*
2152 	 * Refresh pci hotplug in case the rfkill state was changed during
2153 	 * setup.
2154 	 */
2155 	asus_rfkill_hotplug(asus);
2156 
2157 exit:
2158 	if (result && result != -ENODEV)
2159 		asus_wmi_rfkill_exit(asus);
2160 
2161 	if (result == -ENODEV)
2162 		result = 0;
2163 
2164 	return result;
2165 }
2166 
2167 /* Panel Overdrive ************************************************************/
2168 static ssize_t panel_od_show(struct device *dev,
2169 				   struct device_attribute *attr, char *buf)
2170 {
2171 	struct asus_wmi *asus = dev_get_drvdata(dev);
2172 	int result;
2173 
2174 	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_PANEL_OD);
2175 	if (result < 0)
2176 		return result;
2177 
2178 	return sysfs_emit(buf, "%d\n", result);
2179 }
2180 
2181 static ssize_t panel_od_store(struct device *dev,
2182 				    struct device_attribute *attr,
2183 				    const char *buf, size_t count)
2184 {
2185 	int result, err;
2186 	u32 overdrive;
2187 
2188 	struct asus_wmi *asus = dev_get_drvdata(dev);
2189 
2190 	result = kstrtou32(buf, 10, &overdrive);
2191 	if (result)
2192 		return result;
2193 
2194 	if (overdrive > 1)
2195 		return -EINVAL;
2196 
2197 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, overdrive, &result);
2198 
2199 	if (err) {
2200 		pr_warn("Failed to set panel overdrive: %d\n", err);
2201 		return err;
2202 	}
2203 
2204 	if (result > 1) {
2205 		pr_warn("Failed to set panel overdrive (result): 0x%x\n", result);
2206 		return -EIO;
2207 	}
2208 
2209 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
2210 
2211 	return count;
2212 }
2213 static DEVICE_ATTR_RW(panel_od);
2214 
2215 /* Bootup sound ***************************************************************/
2216 
2217 static ssize_t boot_sound_show(struct device *dev,
2218 			     struct device_attribute *attr, char *buf)
2219 {
2220 	struct asus_wmi *asus = dev_get_drvdata(dev);
2221 	int result;
2222 
2223 	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_BOOT_SOUND);
2224 	if (result < 0)
2225 		return result;
2226 
2227 	return sysfs_emit(buf, "%d\n", result);
2228 }
2229 
2230 static ssize_t boot_sound_store(struct device *dev,
2231 			      struct device_attribute *attr,
2232 			      const char *buf, size_t count)
2233 {
2234 	int result, err;
2235 	u32 snd;
2236 
2237 	struct asus_wmi *asus = dev_get_drvdata(dev);
2238 
2239 	result = kstrtou32(buf, 10, &snd);
2240 	if (result)
2241 		return result;
2242 
2243 	if (snd > 1)
2244 		return -EINVAL;
2245 
2246 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BOOT_SOUND, snd, &result);
2247 	if (err) {
2248 		pr_warn("Failed to set boot sound: %d\n", err);
2249 		return err;
2250 	}
2251 
2252 	if (result > 1) {
2253 		pr_warn("Failed to set panel boot sound (result): 0x%x\n", result);
2254 		return -EIO;
2255 	}
2256 
2257 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "boot_sound");
2258 
2259 	return count;
2260 }
2261 static DEVICE_ATTR_RW(boot_sound);
2262 
2263 /* Mini-LED mode **************************************************************/
2264 static ssize_t mini_led_mode_show(struct device *dev,
2265 				   struct device_attribute *attr, char *buf)
2266 {
2267 	struct asus_wmi *asus = dev_get_drvdata(dev);
2268 	u32 value;
2269 	int err;
2270 
2271 	err = asus_wmi_get_devstate(asus, asus->mini_led_dev_id, &value);
2272 	if (err < 0)
2273 		return err;
2274 	value = value & ASUS_MINI_LED_MODE_MASK;
2275 
2276 	/*
2277 	 * Remap the mode values to match previous generation mini-led. The last gen
2278 	 * WMI 0 == off, while on this version WMI 2 ==off (flipped).
2279 	 */
2280 	if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE2) {
2281 		switch (value) {
2282 		case ASUS_MINI_LED_2024_WEAK:
2283 			value = ASUS_MINI_LED_ON;
2284 			break;
2285 		case ASUS_MINI_LED_2024_STRONG:
2286 			value = ASUS_MINI_LED_STRONG_MODE;
2287 			break;
2288 		case ASUS_MINI_LED_2024_OFF:
2289 			value = ASUS_MINI_LED_OFF;
2290 			break;
2291 		}
2292 	}
2293 
2294 	return sysfs_emit(buf, "%d\n", value);
2295 }
2296 
2297 static ssize_t mini_led_mode_store(struct device *dev,
2298 				    struct device_attribute *attr,
2299 				    const char *buf, size_t count)
2300 {
2301 	int result, err;
2302 	u32 mode;
2303 
2304 	struct asus_wmi *asus = dev_get_drvdata(dev);
2305 
2306 	result = kstrtou32(buf, 10, &mode);
2307 	if (result)
2308 		return result;
2309 
2310 	if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE &&
2311 	    mode > ASUS_MINI_LED_ON)
2312 		return -EINVAL;
2313 	if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE2 &&
2314 	    mode > ASUS_MINI_LED_STRONG_MODE)
2315 		return -EINVAL;
2316 
2317 	/*
2318 	 * Remap the mode values so expected behaviour is the same as the last
2319 	 * generation of mini-LED with 0 == off, 1 == on.
2320 	 */
2321 	if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE2) {
2322 		switch (mode) {
2323 		case ASUS_MINI_LED_OFF:
2324 			mode = ASUS_MINI_LED_2024_OFF;
2325 			break;
2326 		case ASUS_MINI_LED_ON:
2327 			mode = ASUS_MINI_LED_2024_WEAK;
2328 			break;
2329 		case ASUS_MINI_LED_STRONG_MODE:
2330 			mode = ASUS_MINI_LED_2024_STRONG;
2331 			break;
2332 		}
2333 	}
2334 
2335 	err = asus_wmi_set_devstate(asus->mini_led_dev_id, mode, &result);
2336 	if (err) {
2337 		pr_warn("Failed to set mini-LED: %d\n", err);
2338 		return err;
2339 	}
2340 
2341 	if (result > 1) {
2342 		pr_warn("Failed to set mini-LED mode (result): 0x%x\n", result);
2343 		return -EIO;
2344 	}
2345 
2346 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "mini_led_mode");
2347 
2348 	return count;
2349 }
2350 static DEVICE_ATTR_RW(mini_led_mode);
2351 
2352 static ssize_t available_mini_led_mode_show(struct device *dev,
2353 				  struct device_attribute *attr, char *buf)
2354 {
2355 	struct asus_wmi *asus = dev_get_drvdata(dev);
2356 
2357 	switch (asus->mini_led_dev_id) {
2358 	case ASUS_WMI_DEVID_MINI_LED_MODE:
2359 		return sysfs_emit(buf, "0 1\n");
2360 	case ASUS_WMI_DEVID_MINI_LED_MODE2:
2361 		return sysfs_emit(buf, "0 1 2\n");
2362 	}
2363 
2364 	return sysfs_emit(buf, "0\n");
2365 }
2366 
2367 static DEVICE_ATTR_RO(available_mini_led_mode);
2368 
2369 /* Quirks *********************************************************************/
2370 
2371 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
2372 {
2373 	struct pci_dev *xhci_pdev;
2374 	u32 orig_ports_available;
2375 	u32 ports_available = asus->driver->quirks->xusb2pr;
2376 
2377 	xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
2378 			PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
2379 			NULL);
2380 
2381 	if (!xhci_pdev)
2382 		return;
2383 
2384 	pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
2385 				&orig_ports_available);
2386 
2387 	pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
2388 				cpu_to_le32(ports_available));
2389 
2390 	pci_dev_put(xhci_pdev);
2391 
2392 	pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
2393 			orig_ports_available, ports_available);
2394 }
2395 
2396 /*
2397  * Some devices dont support or have borcken get_als method
2398  * but still support set method.
2399  */
2400 static void asus_wmi_set_als(void)
2401 {
2402 	asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
2403 }
2404 
2405 /* Hwmon device ***************************************************************/
2406 
2407 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
2408 					  int *speed)
2409 {
2410 	struct agfn_fan_args args = {
2411 		.agfn.len = sizeof(args),
2412 		.agfn.mfun = ASUS_FAN_MFUN,
2413 		.agfn.sfun = ASUS_FAN_SFUN_READ,
2414 		.fan = fan,
2415 		.speed = 0,
2416 	};
2417 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2418 	int status;
2419 
2420 	if (fan != 1)
2421 		return -EINVAL;
2422 
2423 	status = asus_wmi_evaluate_method_agfn(input);
2424 
2425 	if (status || args.agfn.err)
2426 		return -ENXIO;
2427 
2428 	if (speed)
2429 		*speed = args.speed;
2430 
2431 	return 0;
2432 }
2433 
2434 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
2435 				     int *speed)
2436 {
2437 	struct agfn_fan_args args = {
2438 		.agfn.len = sizeof(args),
2439 		.agfn.mfun = ASUS_FAN_MFUN,
2440 		.agfn.sfun = ASUS_FAN_SFUN_WRITE,
2441 		.fan = fan,
2442 		.speed = speed ?  *speed : 0,
2443 	};
2444 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2445 	int status;
2446 
2447 	/* 1: for setting 1st fan's speed 0: setting auto mode */
2448 	if (fan != 1 && fan != 0)
2449 		return -EINVAL;
2450 
2451 	status = asus_wmi_evaluate_method_agfn(input);
2452 
2453 	if (status || args.agfn.err)
2454 		return -ENXIO;
2455 
2456 	if (speed && fan == 1)
2457 		asus->agfn_pwm = *speed;
2458 
2459 	return 0;
2460 }
2461 
2462 /*
2463  * Check if we can read the speed of one fan. If true we assume we can also
2464  * control it.
2465  */
2466 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
2467 {
2468 	int status;
2469 	int speed;
2470 	u32 value;
2471 
2472 	status = asus_agfn_fan_speed_read(asus, 1, &speed);
2473 	if (status != 0)
2474 		return false;
2475 
2476 	status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
2477 	if (status != 0)
2478 		return false;
2479 
2480 	/*
2481 	 * We need to find a better way, probably using sfun,
2482 	 * bits or spec ...
2483 	 * Currently we disable it if:
2484 	 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
2485 	 * - reverved bits are non-zero
2486 	 * - sfun and presence bit are not set
2487 	 */
2488 	return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
2489 		 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
2490 }
2491 
2492 static int asus_fan_set_auto(struct asus_wmi *asus)
2493 {
2494 	int status;
2495 	u32 retval;
2496 
2497 	switch (asus->fan_type) {
2498 	case FAN_TYPE_SPEC83:
2499 		status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
2500 					       0, &retval);
2501 		if (status)
2502 			return status;
2503 
2504 		if (retval != 1)
2505 			return -EIO;
2506 		break;
2507 
2508 	case FAN_TYPE_AGFN:
2509 		status = asus_agfn_fan_speed_write(asus, 0, NULL);
2510 		if (status)
2511 			return -ENXIO;
2512 		break;
2513 
2514 	default:
2515 		return -ENXIO;
2516 	}
2517 
2518 	/*
2519 	 * Modern models like the G713 also have GPU fan control (this is not AGFN)
2520 	 */
2521 	if (asus->gpu_fan_type == FAN_TYPE_SPEC83) {
2522 		status = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
2523 					       0, &retval);
2524 		if (status)
2525 			return status;
2526 
2527 		if (retval != 1)
2528 			return -EIO;
2529 	}
2530 
2531 	return 0;
2532 }
2533 
2534 static ssize_t pwm1_show(struct device *dev,
2535 			       struct device_attribute *attr,
2536 			       char *buf)
2537 {
2538 	struct asus_wmi *asus = dev_get_drvdata(dev);
2539 	int err;
2540 	int value;
2541 
2542 	/* If we already set a value then just return it */
2543 	if (asus->agfn_pwm >= 0)
2544 		return sysfs_emit(buf, "%d\n", asus->agfn_pwm);
2545 
2546 	/*
2547 	 * If we haven't set already set a value through the AGFN interface,
2548 	 * we read a current value through the (now-deprecated) FAN_CTRL device.
2549 	 */
2550 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
2551 	if (err < 0)
2552 		return err;
2553 
2554 	value &= 0xFF;
2555 
2556 	if (value == 1) /* Low Speed */
2557 		value = 85;
2558 	else if (value == 2)
2559 		value = 170;
2560 	else if (value == 3)
2561 		value = 255;
2562 	else if (value) {
2563 		pr_err("Unknown fan speed %#x\n", value);
2564 		value = -1;
2565 	}
2566 
2567 	return sysfs_emit(buf, "%d\n", value);
2568 }
2569 
2570 static ssize_t pwm1_store(struct device *dev,
2571 				     struct device_attribute *attr,
2572 				     const char *buf, size_t count) {
2573 	struct asus_wmi *asus = dev_get_drvdata(dev);
2574 	int value;
2575 	int state;
2576 	int ret;
2577 
2578 	ret = kstrtouint(buf, 10, &value);
2579 	if (ret)
2580 		return ret;
2581 
2582 	value = clamp(value, 0, 255);
2583 
2584 	state = asus_agfn_fan_speed_write(asus, 1, &value);
2585 	if (state)
2586 		pr_warn("Setting fan speed failed: %d\n", state);
2587 	else
2588 		asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
2589 
2590 	return count;
2591 }
2592 
2593 static ssize_t fan1_input_show(struct device *dev,
2594 					struct device_attribute *attr,
2595 					char *buf)
2596 {
2597 	struct asus_wmi *asus = dev_get_drvdata(dev);
2598 	int value;
2599 	int ret;
2600 
2601 	switch (asus->fan_type) {
2602 	case FAN_TYPE_SPEC83:
2603 		ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
2604 					    &value);
2605 		if (ret < 0)
2606 			return ret;
2607 
2608 		value &= 0xffff;
2609 		break;
2610 
2611 	case FAN_TYPE_AGFN:
2612 		/* no speed readable on manual mode */
2613 		if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
2614 			return -ENXIO;
2615 
2616 		ret = asus_agfn_fan_speed_read(asus, 1, &value);
2617 		if (ret) {
2618 			pr_warn("reading fan speed failed: %d\n", ret);
2619 			return -ENXIO;
2620 		}
2621 		break;
2622 
2623 	default:
2624 		return -ENXIO;
2625 	}
2626 
2627 	return sysfs_emit(buf, "%d\n", value < 0 ? -1 : value * 100);
2628 }
2629 
2630 static ssize_t pwm1_enable_show(struct device *dev,
2631 						 struct device_attribute *attr,
2632 						 char *buf)
2633 {
2634 	struct asus_wmi *asus = dev_get_drvdata(dev);
2635 
2636 	/*
2637 	 * Just read back the cached pwm mode.
2638 	 *
2639 	 * For the CPU_FAN device, the spec indicates that we should be
2640 	 * able to read the device status and consult bit 19 to see if we
2641 	 * are in Full On or Automatic mode. However, this does not work
2642 	 * in practice on X532FL at least (the bit is always 0) and there's
2643 	 * also nothing in the DSDT to indicate that this behaviour exists.
2644 	 */
2645 	return sysfs_emit(buf, "%d\n", asus->fan_pwm_mode);
2646 }
2647 
2648 static ssize_t pwm1_enable_store(struct device *dev,
2649 						  struct device_attribute *attr,
2650 						  const char *buf, size_t count)
2651 {
2652 	struct asus_wmi *asus = dev_get_drvdata(dev);
2653 	int status = 0;
2654 	int state;
2655 	int value;
2656 	int ret;
2657 	u32 retval;
2658 
2659 	ret = kstrtouint(buf, 10, &state);
2660 	if (ret)
2661 		return ret;
2662 
2663 	if (asus->fan_type == FAN_TYPE_SPEC83) {
2664 		switch (state) { /* standard documented hwmon values */
2665 		case ASUS_FAN_CTRL_FULLSPEED:
2666 			value = 1;
2667 			break;
2668 		case ASUS_FAN_CTRL_AUTO:
2669 			value = 0;
2670 			break;
2671 		default:
2672 			return -EINVAL;
2673 		}
2674 
2675 		ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
2676 					    value, &retval);
2677 		if (ret)
2678 			return ret;
2679 
2680 		if (retval != 1)
2681 			return -EIO;
2682 	} else if (asus->fan_type == FAN_TYPE_AGFN) {
2683 		switch (state) {
2684 		case ASUS_FAN_CTRL_MANUAL:
2685 			break;
2686 
2687 		case ASUS_FAN_CTRL_AUTO:
2688 			status = asus_fan_set_auto(asus);
2689 			if (status)
2690 				return status;
2691 			break;
2692 
2693 		default:
2694 			return -EINVAL;
2695 		}
2696 	}
2697 
2698 	asus->fan_pwm_mode = state;
2699 
2700 	/* Must set to disabled if mode is toggled */
2701 	if (asus->cpu_fan_curve_available)
2702 		asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
2703 	if (asus->gpu_fan_curve_available)
2704 		asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
2705 	if (asus->mid_fan_curve_available)
2706 		asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false;
2707 
2708 	return count;
2709 }
2710 
2711 static ssize_t asus_hwmon_temp1(struct device *dev,
2712 				struct device_attribute *attr,
2713 				char *buf)
2714 {
2715 	struct asus_wmi *asus = dev_get_drvdata(dev);
2716 	u32 value;
2717 	int err;
2718 
2719 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
2720 	if (err < 0)
2721 		return err;
2722 
2723 	return sysfs_emit(buf, "%ld\n",
2724 			  deci_kelvin_to_millicelsius(value & 0xFFFF));
2725 }
2726 
2727 /* GPU fan on modern ROG laptops */
2728 static ssize_t fan2_input_show(struct device *dev,
2729 					struct device_attribute *attr,
2730 					char *buf)
2731 {
2732 	struct asus_wmi *asus = dev_get_drvdata(dev);
2733 	int value;
2734 	int ret;
2735 
2736 	ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL, &value);
2737 	if (ret < 0)
2738 		return ret;
2739 
2740 	value &= 0xffff;
2741 
2742 	return sysfs_emit(buf, "%d\n", value * 100);
2743 }
2744 
2745 /* Middle/Center fan on modern ROG laptops */
2746 static ssize_t fan3_input_show(struct device *dev,
2747 					struct device_attribute *attr,
2748 					char *buf)
2749 {
2750 	struct asus_wmi *asus = dev_get_drvdata(dev);
2751 	int value;
2752 	int ret;
2753 
2754 	ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_MID_FAN_CTRL, &value);
2755 	if (ret < 0)
2756 		return ret;
2757 
2758 	value &= 0xffff;
2759 
2760 	return sysfs_emit(buf, "%d\n", value * 100);
2761 }
2762 
2763 static ssize_t pwm2_enable_show(struct device *dev,
2764 				struct device_attribute *attr,
2765 				char *buf)
2766 {
2767 	struct asus_wmi *asus = dev_get_drvdata(dev);
2768 
2769 	return sysfs_emit(buf, "%d\n", asus->gpu_fan_pwm_mode);
2770 }
2771 
2772 static ssize_t pwm2_enable_store(struct device *dev,
2773 				 struct device_attribute *attr,
2774 				 const char *buf, size_t count)
2775 {
2776 	struct asus_wmi *asus = dev_get_drvdata(dev);
2777 	int state;
2778 	int value;
2779 	int ret;
2780 	u32 retval;
2781 
2782 	ret = kstrtouint(buf, 10, &state);
2783 	if (ret)
2784 		return ret;
2785 
2786 	switch (state) { /* standard documented hwmon values */
2787 	case ASUS_FAN_CTRL_FULLSPEED:
2788 		value = 1;
2789 		break;
2790 	case ASUS_FAN_CTRL_AUTO:
2791 		value = 0;
2792 		break;
2793 	default:
2794 		return -EINVAL;
2795 	}
2796 
2797 	ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
2798 				    value, &retval);
2799 	if (ret)
2800 		return ret;
2801 
2802 	if (retval != 1)
2803 		return -EIO;
2804 
2805 	asus->gpu_fan_pwm_mode = state;
2806 	return count;
2807 }
2808 
2809 static ssize_t pwm3_enable_show(struct device *dev,
2810 				struct device_attribute *attr,
2811 				char *buf)
2812 {
2813 	struct asus_wmi *asus = dev_get_drvdata(dev);
2814 
2815 	return sysfs_emit(buf, "%d\n", asus->mid_fan_pwm_mode);
2816 }
2817 
2818 static ssize_t pwm3_enable_store(struct device *dev,
2819 				 struct device_attribute *attr,
2820 				 const char *buf, size_t count)
2821 {
2822 	struct asus_wmi *asus = dev_get_drvdata(dev);
2823 	int state;
2824 	int value;
2825 	int ret;
2826 	u32 retval;
2827 
2828 	ret = kstrtouint(buf, 10, &state);
2829 	if (ret)
2830 		return ret;
2831 
2832 	switch (state) { /* standard documented hwmon values */
2833 	case ASUS_FAN_CTRL_FULLSPEED:
2834 		value = 1;
2835 		break;
2836 	case ASUS_FAN_CTRL_AUTO:
2837 		value = 0;
2838 		break;
2839 	default:
2840 		return -EINVAL;
2841 	}
2842 
2843 	ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_MID_FAN_CTRL,
2844 				    value, &retval);
2845 	if (ret)
2846 		return ret;
2847 
2848 	if (retval != 1)
2849 		return -EIO;
2850 
2851 	asus->mid_fan_pwm_mode = state;
2852 	return count;
2853 }
2854 
2855 /* Fan1 */
2856 static DEVICE_ATTR_RW(pwm1);
2857 static DEVICE_ATTR_RW(pwm1_enable);
2858 static DEVICE_ATTR_RO(fan1_input);
2859 static DEVICE_STRING_ATTR_RO(fan1_label, 0444, ASUS_FAN_DESC);
2860 
2861 /* Fan2 - GPU fan */
2862 static DEVICE_ATTR_RW(pwm2_enable);
2863 static DEVICE_ATTR_RO(fan2_input);
2864 static DEVICE_STRING_ATTR_RO(fan2_label, 0444, ASUS_GPU_FAN_DESC);
2865 /* Fan3 - Middle/center fan */
2866 static DEVICE_ATTR_RW(pwm3_enable);
2867 static DEVICE_ATTR_RO(fan3_input);
2868 static DEVICE_STRING_ATTR_RO(fan3_label, 0444, ASUS_MID_FAN_DESC);
2869 
2870 /* Temperature */
2871 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
2872 
2873 static struct attribute *hwmon_attributes[] = {
2874 	&dev_attr_pwm1.attr,
2875 	&dev_attr_pwm1_enable.attr,
2876 	&dev_attr_pwm2_enable.attr,
2877 	&dev_attr_pwm3_enable.attr,
2878 	&dev_attr_fan1_input.attr,
2879 	&dev_attr_fan1_label.attr.attr,
2880 	&dev_attr_fan2_input.attr,
2881 	&dev_attr_fan2_label.attr.attr,
2882 	&dev_attr_fan3_input.attr,
2883 	&dev_attr_fan3_label.attr.attr,
2884 
2885 	&dev_attr_temp1_input.attr,
2886 	NULL
2887 };
2888 
2889 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
2890 					  struct attribute *attr, int idx)
2891 {
2892 	struct device *dev = kobj_to_dev(kobj);
2893 	struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2894 	u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
2895 
2896 	if (attr == &dev_attr_pwm1.attr) {
2897 		if (asus->fan_type != FAN_TYPE_AGFN)
2898 			return 0;
2899 	} else if (attr == &dev_attr_fan1_input.attr
2900 	    || attr == &dev_attr_fan1_label.attr.attr
2901 	    || attr == &dev_attr_pwm1_enable.attr) {
2902 		if (asus->fan_type == FAN_TYPE_NONE)
2903 			return 0;
2904 	} else if (attr == &dev_attr_fan2_input.attr
2905 	    || attr == &dev_attr_fan2_label.attr.attr
2906 	    || attr == &dev_attr_pwm2_enable.attr) {
2907 		if (asus->gpu_fan_type == FAN_TYPE_NONE)
2908 			return 0;
2909 	} else if (attr == &dev_attr_fan3_input.attr
2910 	    || attr == &dev_attr_fan3_label.attr.attr
2911 	    || attr == &dev_attr_pwm3_enable.attr) {
2912 		if (asus->mid_fan_type == FAN_TYPE_NONE)
2913 			return 0;
2914 	} else if (attr == &dev_attr_temp1_input.attr) {
2915 		int err = asus_wmi_get_devstate(asus,
2916 						ASUS_WMI_DEVID_THERMAL_CTRL,
2917 						&value);
2918 
2919 		if (err < 0)
2920 			return 0; /* can't return negative here */
2921 
2922 		/*
2923 		 * If the temperature value in deci-Kelvin is near the absolute
2924 		 * zero temperature, something is clearly wrong
2925 		 */
2926 		if (value == 0 || value == 1)
2927 			return 0;
2928 	}
2929 
2930 	return attr->mode;
2931 }
2932 
2933 static const struct attribute_group hwmon_attribute_group = {
2934 	.is_visible = asus_hwmon_sysfs_is_visible,
2935 	.attrs = hwmon_attributes
2936 };
2937 __ATTRIBUTE_GROUPS(hwmon_attribute);
2938 
2939 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
2940 {
2941 	struct device *dev = &asus->platform_device->dev;
2942 	struct device *hwmon;
2943 
2944 	hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
2945 			hwmon_attribute_groups);
2946 
2947 	if (IS_ERR(hwmon)) {
2948 		pr_err("Could not register asus hwmon device\n");
2949 		return PTR_ERR(hwmon);
2950 	}
2951 	return 0;
2952 }
2953 
2954 static int asus_wmi_fan_init(struct asus_wmi *asus)
2955 {
2956 	asus->gpu_fan_type = FAN_TYPE_NONE;
2957 	asus->mid_fan_type = FAN_TYPE_NONE;
2958 	asus->fan_type = FAN_TYPE_NONE;
2959 	asus->agfn_pwm = -1;
2960 
2961 	if (asus->driver->quirks->wmi_ignore_fan)
2962 		asus->fan_type = FAN_TYPE_NONE;
2963 	else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
2964 		asus->fan_type = FAN_TYPE_SPEC83;
2965 	else if (asus_wmi_has_agfn_fan(asus))
2966 		asus->fan_type = FAN_TYPE_AGFN;
2967 
2968 	/*  Modern models like G713 also have GPU fan control */
2969 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL))
2970 		asus->gpu_fan_type = FAN_TYPE_SPEC83;
2971 
2972 	/* Some models also have a center/middle fan */
2973 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MID_FAN_CTRL))
2974 		asus->mid_fan_type = FAN_TYPE_SPEC83;
2975 
2976 	if (asus->fan_type == FAN_TYPE_NONE)
2977 		return -ENODEV;
2978 
2979 	asus_fan_set_auto(asus);
2980 	asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
2981 	return 0;
2982 }
2983 
2984 /* Fan mode *******************************************************************/
2985 
2986 static int fan_boost_mode_check_present(struct asus_wmi *asus)
2987 {
2988 	u32 result;
2989 	int err;
2990 
2991 	asus->fan_boost_mode_available = false;
2992 
2993 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
2994 				    &result);
2995 	if (err) {
2996 		if (err == -ENODEV)
2997 			return 0;
2998 		else
2999 			return err;
3000 	}
3001 
3002 	if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
3003 			(result & ASUS_FAN_BOOST_MODES_MASK)) {
3004 		asus->fan_boost_mode_available = true;
3005 		asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
3006 	}
3007 
3008 	return 0;
3009 }
3010 
3011 static int fan_boost_mode_write(struct asus_wmi *asus)
3012 {
3013 	u32 retval;
3014 	u8 value;
3015 	int err;
3016 
3017 	value = asus->fan_boost_mode;
3018 
3019 	pr_info("Set fan boost mode: %u\n", value);
3020 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
3021 				    &retval);
3022 
3023 	sysfs_notify(&asus->platform_device->dev.kobj, NULL,
3024 			"fan_boost_mode");
3025 
3026 	if (err) {
3027 		pr_warn("Failed to set fan boost mode: %d\n", err);
3028 		return err;
3029 	}
3030 
3031 	if (retval != 1) {
3032 		pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
3033 			retval);
3034 		return -EIO;
3035 	}
3036 
3037 	return 0;
3038 }
3039 
3040 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
3041 {
3042 	u8 mask = asus->fan_boost_mode_mask;
3043 
3044 	if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
3045 		if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
3046 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
3047 		else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
3048 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
3049 	} else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
3050 		if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
3051 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
3052 		else
3053 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
3054 	} else {
3055 		asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
3056 	}
3057 
3058 	return fan_boost_mode_write(asus);
3059 }
3060 
3061 static ssize_t fan_boost_mode_show(struct device *dev,
3062 				   struct device_attribute *attr, char *buf)
3063 {
3064 	struct asus_wmi *asus = dev_get_drvdata(dev);
3065 
3066 	return sysfs_emit(buf, "%d\n", asus->fan_boost_mode);
3067 }
3068 
3069 static ssize_t fan_boost_mode_store(struct device *dev,
3070 				    struct device_attribute *attr,
3071 				    const char *buf, size_t count)
3072 {
3073 	struct asus_wmi *asus = dev_get_drvdata(dev);
3074 	u8 mask = asus->fan_boost_mode_mask;
3075 	u8 new_mode;
3076 	int result;
3077 
3078 	result = kstrtou8(buf, 10, &new_mode);
3079 	if (result < 0) {
3080 		pr_warn("Trying to store invalid value\n");
3081 		return result;
3082 	}
3083 
3084 	if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
3085 		if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
3086 			return -EINVAL;
3087 	} else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
3088 		if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
3089 			return -EINVAL;
3090 	} else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
3091 		return -EINVAL;
3092 	}
3093 
3094 	asus->fan_boost_mode = new_mode;
3095 	fan_boost_mode_write(asus);
3096 
3097 	return count;
3098 }
3099 
3100 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
3101 static DEVICE_ATTR_RW(fan_boost_mode);
3102 
3103 /* Custom fan curves **********************************************************/
3104 
3105 static void fan_curve_copy_from_buf(struct fan_curve_data *data, u8 *buf)
3106 {
3107 	int i;
3108 
3109 	for (i = 0; i < FAN_CURVE_POINTS; i++) {
3110 		data->temps[i] = buf[i];
3111 	}
3112 
3113 	for (i = 0; i < FAN_CURVE_POINTS; i++) {
3114 		data->percents[i] =
3115 			255 * buf[i + FAN_CURVE_POINTS] / 100;
3116 	}
3117 }
3118 
3119 static int fan_curve_get_factory_default(struct asus_wmi *asus, u32 fan_dev)
3120 {
3121 	struct fan_curve_data *curves;
3122 	u8 buf[FAN_CURVE_BUF_LEN];
3123 	int err, fan_idx;
3124 	u8 mode = 0;
3125 
3126 	if (asus->throttle_thermal_policy_available)
3127 		mode = asus->throttle_thermal_policy_mode;
3128 	/* DEVID_<C/G>PU_FAN_CURVE is switched for OVERBOOST vs SILENT */
3129 	if (mode == 2)
3130 		mode = 1;
3131 	else if (mode == 1)
3132 		mode = 2;
3133 
3134 	err = asus_wmi_evaluate_method_buf(asus->dsts_id, fan_dev, mode, buf,
3135 					   FAN_CURVE_BUF_LEN);
3136 	if (err) {
3137 		pr_warn("%s (0x%08x) failed: %d\n", __func__, fan_dev, err);
3138 		return err;
3139 	}
3140 
3141 	fan_idx = FAN_CURVE_DEV_CPU;
3142 	if (fan_dev == ASUS_WMI_DEVID_GPU_FAN_CURVE)
3143 		fan_idx = FAN_CURVE_DEV_GPU;
3144 
3145 	if (fan_dev == ASUS_WMI_DEVID_MID_FAN_CURVE)
3146 		fan_idx = FAN_CURVE_DEV_MID;
3147 
3148 	curves = &asus->custom_fan_curves[fan_idx];
3149 	curves->device_id = fan_dev;
3150 
3151 	fan_curve_copy_from_buf(curves, buf);
3152 	return 0;
3153 }
3154 
3155 /* Check if capability exists, and populate defaults */
3156 static int fan_curve_check_present(struct asus_wmi *asus, bool *available,
3157 				   u32 fan_dev)
3158 {
3159 	int err;
3160 
3161 	*available = false;
3162 
3163 	if (asus->fan_type == FAN_TYPE_NONE)
3164 		return 0;
3165 
3166 	err = fan_curve_get_factory_default(asus, fan_dev);
3167 	if (err) {
3168 		return 0;
3169 	}
3170 
3171 	*available = true;
3172 	return 0;
3173 }
3174 
3175 /* Determine which fan the attribute is for if SENSOR_ATTR */
3176 static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus,
3177 					      struct device_attribute *attr)
3178 {
3179 	int index = to_sensor_dev_attr(attr)->index;
3180 
3181 	return &asus->custom_fan_curves[index];
3182 }
3183 
3184 /* Determine which fan the attribute is for if SENSOR_ATTR_2 */
3185 static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus,
3186 					    struct device_attribute *attr)
3187 {
3188 	int nr = to_sensor_dev_attr_2(attr)->nr;
3189 
3190 	return &asus->custom_fan_curves[nr & ~FAN_CURVE_PWM_MASK];
3191 }
3192 
3193 static ssize_t fan_curve_show(struct device *dev,
3194 			      struct device_attribute *attr, char *buf)
3195 {
3196 	struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
3197 	struct asus_wmi *asus = dev_get_drvdata(dev);
3198 	struct fan_curve_data *data;
3199 	int value, pwm, index;
3200 
3201 	data = fan_curve_attr_2_select(asus, attr);
3202 	pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
3203 	index = dev_attr->index;
3204 
3205 	if (pwm)
3206 		value = data->percents[index];
3207 	else
3208 		value = data->temps[index];
3209 
3210 	return sysfs_emit(buf, "%d\n", value);
3211 }
3212 
3213 /*
3214  * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE.
3215  */
3216 static int fan_curve_write(struct asus_wmi *asus,
3217 			   struct fan_curve_data *data)
3218 {
3219 	u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0;
3220 	u8 *percents = data->percents;
3221 	u8 *temps = data->temps;
3222 	int ret, i, shift = 0;
3223 
3224 	if (!data->enabled)
3225 		return 0;
3226 
3227 	for (i = 0; i < FAN_CURVE_POINTS / 2; i++) {
3228 		arg1 += (temps[i]) << shift;
3229 		arg2 += (temps[i + 4]) << shift;
3230 		/* Scale to percentage for device */
3231 		arg3 += (100 * percents[i] / 255) << shift;
3232 		arg4 += (100 * percents[i + 4] / 255) << shift;
3233 		shift += 8;
3234 	}
3235 
3236 	return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS,
3237 					 data->device_id,
3238 					 arg1, arg2, arg3, arg4, &ret);
3239 }
3240 
3241 static ssize_t fan_curve_store(struct device *dev,
3242 			       struct device_attribute *attr, const char *buf,
3243 			       size_t count)
3244 {
3245 	struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
3246 	struct asus_wmi *asus = dev_get_drvdata(dev);
3247 	struct fan_curve_data *data;
3248 	int err, pwm, index;
3249 	u8 value;
3250 
3251 	data = fan_curve_attr_2_select(asus, attr);
3252 	pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
3253 	index = dev_attr->index;
3254 
3255 	err = kstrtou8(buf, 10, &value);
3256 	if (err < 0)
3257 		return err;
3258 
3259 	if (pwm)
3260 		data->percents[index] = value;
3261 	else
3262 		data->temps[index] = value;
3263 
3264 	/*
3265 	 * Mark as disabled so the user has to explicitly enable to apply a
3266 	 * changed fan curve. This prevents potential lockups from writing out
3267 	 * many changes as one-write-per-change.
3268 	 */
3269 	data->enabled = false;
3270 
3271 	return count;
3272 }
3273 
3274 static ssize_t fan_curve_enable_show(struct device *dev,
3275 				     struct device_attribute *attr, char *buf)
3276 {
3277 	struct asus_wmi *asus = dev_get_drvdata(dev);
3278 	struct fan_curve_data *data;
3279 	int out = 2;
3280 
3281 	data = fan_curve_attr_select(asus, attr);
3282 
3283 	if (data->enabled)
3284 		out = 1;
3285 
3286 	return sysfs_emit(buf, "%d\n", out);
3287 }
3288 
3289 static ssize_t fan_curve_enable_store(struct device *dev,
3290 				      struct device_attribute *attr,
3291 				      const char *buf, size_t count)
3292 {
3293 	struct asus_wmi *asus = dev_get_drvdata(dev);
3294 	struct fan_curve_data *data;
3295 	int value, err;
3296 
3297 	data = fan_curve_attr_select(asus, attr);
3298 
3299 	err = kstrtoint(buf, 10, &value);
3300 	if (err < 0)
3301 		return err;
3302 
3303 	switch (value) {
3304 	case 1:
3305 		data->enabled = true;
3306 		break;
3307 	case 2:
3308 		data->enabled = false;
3309 		break;
3310 	/*
3311 	 * Auto + reset the fan curve data to defaults. Make it an explicit
3312 	 * option so that users don't accidentally overwrite a set fan curve.
3313 	 */
3314 	case 3:
3315 		err = fan_curve_get_factory_default(asus, data->device_id);
3316 		if (err)
3317 			return err;
3318 		data->enabled = false;
3319 		break;
3320 	default:
3321 		return -EINVAL;
3322 	}
3323 
3324 	if (data->enabled) {
3325 		err = fan_curve_write(asus, data);
3326 		if (err)
3327 			return err;
3328 	} else {
3329 		/*
3330 		 * For machines with throttle this is the only way to reset fans
3331 		 * to default mode of operation (does not erase curve data).
3332 		 */
3333 		if (asus->throttle_thermal_policy_available) {
3334 			err = throttle_thermal_policy_write(asus);
3335 			if (err)
3336 				return err;
3337 		/* Similar is true for laptops with this fan */
3338 		} else if (asus->fan_type == FAN_TYPE_SPEC83) {
3339 			err = asus_fan_set_auto(asus);
3340 			if (err)
3341 				return err;
3342 		} else {
3343 			/* Safeguard against fautly ACPI tables */
3344 			err = fan_curve_get_factory_default(asus, data->device_id);
3345 			if (err)
3346 				return err;
3347 			err = fan_curve_write(asus, data);
3348 			if (err)
3349 				return err;
3350 		}
3351 	}
3352 	return count;
3353 }
3354 
3355 /* CPU */
3356 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU);
3357 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve,
3358 			       FAN_CURVE_DEV_CPU, 0);
3359 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve,
3360 			       FAN_CURVE_DEV_CPU, 1);
3361 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve,
3362 			       FAN_CURVE_DEV_CPU, 2);
3363 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve,
3364 			       FAN_CURVE_DEV_CPU, 3);
3365 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve,
3366 			       FAN_CURVE_DEV_CPU, 4);
3367 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve,
3368 			       FAN_CURVE_DEV_CPU, 5);
3369 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve,
3370 			       FAN_CURVE_DEV_CPU, 6);
3371 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve,
3372 			       FAN_CURVE_DEV_CPU, 7);
3373 
3374 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve,
3375 				FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0);
3376 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve,
3377 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1);
3378 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve,
3379 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2);
3380 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve,
3381 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3);
3382 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve,
3383 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4);
3384 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve,
3385 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5);
3386 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve,
3387 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6);
3388 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve,
3389 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7);
3390 
3391 /* GPU */
3392 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU);
3393 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve,
3394 			       FAN_CURVE_DEV_GPU, 0);
3395 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve,
3396 			       FAN_CURVE_DEV_GPU, 1);
3397 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve,
3398 			       FAN_CURVE_DEV_GPU, 2);
3399 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve,
3400 			       FAN_CURVE_DEV_GPU, 3);
3401 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve,
3402 			       FAN_CURVE_DEV_GPU, 4);
3403 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve,
3404 			       FAN_CURVE_DEV_GPU, 5);
3405 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve,
3406 			       FAN_CURVE_DEV_GPU, 6);
3407 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve,
3408 			       FAN_CURVE_DEV_GPU, 7);
3409 
3410 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve,
3411 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0);
3412 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve,
3413 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1);
3414 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve,
3415 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2);
3416 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve,
3417 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3);
3418 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve,
3419 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4);
3420 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve,
3421 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5);
3422 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve,
3423 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6);
3424 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve,
3425 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7);
3426 
3427 /* MID */
3428 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, fan_curve_enable, FAN_CURVE_DEV_MID);
3429 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, fan_curve,
3430 			       FAN_CURVE_DEV_MID, 0);
3431 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, fan_curve,
3432 			       FAN_CURVE_DEV_MID, 1);
3433 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, fan_curve,
3434 			       FAN_CURVE_DEV_MID, 2);
3435 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, fan_curve,
3436 			       FAN_CURVE_DEV_MID, 3);
3437 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, fan_curve,
3438 			       FAN_CURVE_DEV_MID, 4);
3439 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_temp, fan_curve,
3440 			       FAN_CURVE_DEV_MID, 5);
3441 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_temp, fan_curve,
3442 			       FAN_CURVE_DEV_MID, 6);
3443 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_temp, fan_curve,
3444 			       FAN_CURVE_DEV_MID, 7);
3445 
3446 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, fan_curve,
3447 			       FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 0);
3448 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, fan_curve,
3449 			       FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 1);
3450 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_pwm, fan_curve,
3451 			       FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 2);
3452 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_pwm, fan_curve,
3453 			       FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 3);
3454 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_pwm, fan_curve,
3455 			       FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 4);
3456 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_pwm, fan_curve,
3457 			       FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 5);
3458 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_pwm, fan_curve,
3459 			       FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 6);
3460 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_pwm, fan_curve,
3461 			       FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 7);
3462 
3463 static struct attribute *asus_fan_curve_attr[] = {
3464 	/* CPU */
3465 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3466 	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
3467 	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
3468 	&sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
3469 	&sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
3470 	&sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
3471 	&sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
3472 	&sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
3473 	&sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
3474 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
3475 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
3476 	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
3477 	&sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
3478 	&sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
3479 	&sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
3480 	&sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
3481 	&sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
3482 	/* GPU */
3483 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
3484 	&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
3485 	&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
3486 	&sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
3487 	&sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
3488 	&sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
3489 	&sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr,
3490 	&sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr,
3491 	&sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr,
3492 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
3493 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
3494 	&sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
3495 	&sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
3496 	&sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
3497 	&sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr,
3498 	&sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr,
3499 	&sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr,
3500 	/* MID */
3501 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
3502 	&sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
3503 	&sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
3504 	&sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
3505 	&sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
3506 	&sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
3507 	&sensor_dev_attr_pwm3_auto_point6_temp.dev_attr.attr,
3508 	&sensor_dev_attr_pwm3_auto_point7_temp.dev_attr.attr,
3509 	&sensor_dev_attr_pwm3_auto_point8_temp.dev_attr.attr,
3510 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
3511 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
3512 	&sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
3513 	&sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
3514 	&sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
3515 	&sensor_dev_attr_pwm3_auto_point6_pwm.dev_attr.attr,
3516 	&sensor_dev_attr_pwm3_auto_point7_pwm.dev_attr.attr,
3517 	&sensor_dev_attr_pwm3_auto_point8_pwm.dev_attr.attr,
3518 	NULL
3519 };
3520 
3521 static umode_t asus_fan_curve_is_visible(struct kobject *kobj,
3522 					 struct attribute *attr, int idx)
3523 {
3524 	struct device *dev = kobj_to_dev(kobj);
3525 	struct asus_wmi *asus = dev_get_drvdata(dev->parent);
3526 
3527 	/*
3528 	 * Check the char instead of casting attr as there are two attr types
3529 	 * involved here (attr1 and attr2)
3530 	 */
3531 	if (asus->cpu_fan_curve_available && attr->name[3] == '1')
3532 		return 0644;
3533 
3534 	if (asus->gpu_fan_curve_available && attr->name[3] == '2')
3535 		return 0644;
3536 
3537 	if (asus->mid_fan_curve_available && attr->name[3] == '3')
3538 		return 0644;
3539 
3540 	return 0;
3541 }
3542 
3543 static const struct attribute_group asus_fan_curve_attr_group = {
3544 	.is_visible = asus_fan_curve_is_visible,
3545 	.attrs = asus_fan_curve_attr,
3546 };
3547 __ATTRIBUTE_GROUPS(asus_fan_curve_attr);
3548 
3549 /*
3550  * Must be initialised after throttle_thermal_policy_check_present() as
3551  * we check the status of throttle_thermal_policy_available during init.
3552  */
3553 static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus)
3554 {
3555 	struct device *dev = &asus->platform_device->dev;
3556 	struct device *hwmon;
3557 	int err;
3558 
3559 	err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available,
3560 				      ASUS_WMI_DEVID_CPU_FAN_CURVE);
3561 	if (err)
3562 		return err;
3563 
3564 	err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available,
3565 				      ASUS_WMI_DEVID_GPU_FAN_CURVE);
3566 	if (err)
3567 		return err;
3568 
3569 	err = fan_curve_check_present(asus, &asus->mid_fan_curve_available,
3570 				      ASUS_WMI_DEVID_MID_FAN_CURVE);
3571 	if (err)
3572 		return err;
3573 
3574 	if (!asus->cpu_fan_curve_available
3575 		&& !asus->gpu_fan_curve_available
3576 		&& !asus->mid_fan_curve_available)
3577 		return 0;
3578 
3579 	hwmon = devm_hwmon_device_register_with_groups(
3580 		dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups);
3581 
3582 	if (IS_ERR(hwmon)) {
3583 		dev_err(dev,
3584 			"Could not register asus_custom_fan_curve device\n");
3585 		return PTR_ERR(hwmon);
3586 	}
3587 
3588 	return 0;
3589 }
3590 
3591 /* Throttle thermal policy ****************************************************/
3592 
3593 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
3594 {
3595 	u32 result;
3596 	int err;
3597 
3598 	asus->throttle_thermal_policy_available = false;
3599 
3600 	err = asus_wmi_get_devstate(asus,
3601 				    ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
3602 				    &result);
3603 	if (err) {
3604 		if (err == -ENODEV)
3605 			return 0;
3606 		return err;
3607 	}
3608 
3609 	if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
3610 		asus->throttle_thermal_policy_available = true;
3611 
3612 	return 0;
3613 }
3614 
3615 static int throttle_thermal_policy_write(struct asus_wmi *asus)
3616 {
3617 	int err;
3618 	u8 value;
3619 	u32 retval;
3620 
3621 	value = asus->throttle_thermal_policy_mode;
3622 
3623 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
3624 				    value, &retval);
3625 
3626 	sysfs_notify(&asus->platform_device->dev.kobj, NULL,
3627 			"throttle_thermal_policy");
3628 
3629 	if (err) {
3630 		pr_warn("Failed to set throttle thermal policy: %d\n", err);
3631 		return err;
3632 	}
3633 
3634 	if (retval != 1) {
3635 		pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
3636 			retval);
3637 		return -EIO;
3638 	}
3639 
3640 	/* Must set to disabled if mode is toggled */
3641 	if (asus->cpu_fan_curve_available)
3642 		asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
3643 	if (asus->gpu_fan_curve_available)
3644 		asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
3645 	if (asus->mid_fan_curve_available)
3646 		asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false;
3647 
3648 	return 0;
3649 }
3650 
3651 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
3652 {
3653 	if (!asus->throttle_thermal_policy_available)
3654 		return 0;
3655 
3656 	asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3657 	return throttle_thermal_policy_write(asus);
3658 }
3659 
3660 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
3661 {
3662 	u8 new_mode = asus->throttle_thermal_policy_mode + 1;
3663 	int err;
3664 
3665 	if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
3666 		new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3667 
3668 	asus->throttle_thermal_policy_mode = new_mode;
3669 	err = throttle_thermal_policy_write(asus);
3670 	if (err)
3671 		return err;
3672 
3673 	/*
3674 	 * Ensure that platform_profile updates userspace with the change to ensure
3675 	 * that platform_profile and throttle_thermal_policy_mode are in sync.
3676 	 */
3677 	platform_profile_notify();
3678 
3679 	return 0;
3680 }
3681 
3682 static ssize_t throttle_thermal_policy_show(struct device *dev,
3683 				   struct device_attribute *attr, char *buf)
3684 {
3685 	struct asus_wmi *asus = dev_get_drvdata(dev);
3686 	u8 mode = asus->throttle_thermal_policy_mode;
3687 
3688 	return sysfs_emit(buf, "%d\n", mode);
3689 }
3690 
3691 static ssize_t throttle_thermal_policy_store(struct device *dev,
3692 				    struct device_attribute *attr,
3693 				    const char *buf, size_t count)
3694 {
3695 	struct asus_wmi *asus = dev_get_drvdata(dev);
3696 	u8 new_mode;
3697 	int result;
3698 	int err;
3699 
3700 	result = kstrtou8(buf, 10, &new_mode);
3701 	if (result < 0)
3702 		return result;
3703 
3704 	if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
3705 		return -EINVAL;
3706 
3707 	asus->throttle_thermal_policy_mode = new_mode;
3708 	err = throttle_thermal_policy_write(asus);
3709 	if (err)
3710 		return err;
3711 
3712 	/*
3713 	 * Ensure that platform_profile updates userspace with the change to ensure
3714 	 * that platform_profile and throttle_thermal_policy_mode are in sync.
3715 	 */
3716 	platform_profile_notify();
3717 
3718 	return count;
3719 }
3720 
3721 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
3722 static DEVICE_ATTR_RW(throttle_thermal_policy);
3723 
3724 /* Platform profile ***********************************************************/
3725 static int asus_wmi_platform_profile_get(struct platform_profile_handler *pprof,
3726 					enum platform_profile_option *profile)
3727 {
3728 	struct asus_wmi *asus;
3729 	int tp;
3730 
3731 	asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
3732 
3733 	tp = asus->throttle_thermal_policy_mode;
3734 
3735 	switch (tp) {
3736 	case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
3737 		*profile = PLATFORM_PROFILE_BALANCED;
3738 		break;
3739 	case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
3740 		*profile = PLATFORM_PROFILE_PERFORMANCE;
3741 		break;
3742 	case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
3743 		*profile = PLATFORM_PROFILE_QUIET;
3744 		break;
3745 	default:
3746 		return -EINVAL;
3747 	}
3748 
3749 	return 0;
3750 }
3751 
3752 static int asus_wmi_platform_profile_set(struct platform_profile_handler *pprof,
3753 					enum platform_profile_option profile)
3754 {
3755 	struct asus_wmi *asus;
3756 	int tp;
3757 
3758 	asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
3759 
3760 	switch (profile) {
3761 	case PLATFORM_PROFILE_PERFORMANCE:
3762 		tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
3763 		break;
3764 	case PLATFORM_PROFILE_BALANCED:
3765 		tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3766 		break;
3767 	case PLATFORM_PROFILE_QUIET:
3768 		tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
3769 		break;
3770 	default:
3771 		return -EOPNOTSUPP;
3772 	}
3773 
3774 	asus->throttle_thermal_policy_mode = tp;
3775 	return throttle_thermal_policy_write(asus);
3776 }
3777 
3778 static int platform_profile_setup(struct asus_wmi *asus)
3779 {
3780 	struct device *dev = &asus->platform_device->dev;
3781 	int err;
3782 
3783 	/*
3784 	 * Not an error if a component platform_profile relies on is unavailable
3785 	 * so early return, skipping the setup of platform_profile.
3786 	 */
3787 	if (!asus->throttle_thermal_policy_available)
3788 		return 0;
3789 
3790 	dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
3791 
3792 	asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
3793 	asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
3794 
3795 	set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
3796 	set_bit(PLATFORM_PROFILE_BALANCED,
3797 		asus->platform_profile_handler.choices);
3798 	set_bit(PLATFORM_PROFILE_PERFORMANCE,
3799 		asus->platform_profile_handler.choices);
3800 
3801 	err = platform_profile_register(&asus->platform_profile_handler);
3802 	if (err)
3803 		return err;
3804 
3805 	asus->platform_profile_support = true;
3806 	return 0;
3807 }
3808 
3809 /* Backlight ******************************************************************/
3810 
3811 static int read_backlight_power(struct asus_wmi *asus)
3812 {
3813 	int ret;
3814 
3815 	if (asus->driver->quirks->store_backlight_power)
3816 		ret = !asus->driver->panel_power;
3817 	else
3818 		ret = asus_wmi_get_devstate_simple(asus,
3819 						   ASUS_WMI_DEVID_BACKLIGHT);
3820 
3821 	if (ret < 0)
3822 		return ret;
3823 
3824 	return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
3825 }
3826 
3827 static int read_brightness_max(struct asus_wmi *asus)
3828 {
3829 	u32 retval;
3830 	int err;
3831 
3832 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
3833 	if (err < 0)
3834 		return err;
3835 
3836 	retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
3837 	retval >>= 8;
3838 
3839 	if (!retval)
3840 		return -ENODEV;
3841 
3842 	return retval;
3843 }
3844 
3845 static int read_brightness(struct backlight_device *bd)
3846 {
3847 	struct asus_wmi *asus = bl_get_data(bd);
3848 	u32 retval;
3849 	int err;
3850 
3851 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
3852 	if (err < 0)
3853 		return err;
3854 
3855 	return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
3856 }
3857 
3858 static u32 get_scalar_command(struct backlight_device *bd)
3859 {
3860 	struct asus_wmi *asus = bl_get_data(bd);
3861 	u32 ctrl_param = 0;
3862 
3863 	if ((asus->driver->brightness < bd->props.brightness) ||
3864 	    bd->props.brightness == bd->props.max_brightness)
3865 		ctrl_param = 0x00008001;
3866 	else if ((asus->driver->brightness > bd->props.brightness) ||
3867 		 bd->props.brightness == 0)
3868 		ctrl_param = 0x00008000;
3869 
3870 	asus->driver->brightness = bd->props.brightness;
3871 
3872 	return ctrl_param;
3873 }
3874 
3875 static int update_bl_status(struct backlight_device *bd)
3876 {
3877 	struct asus_wmi *asus = bl_get_data(bd);
3878 	u32 ctrl_param;
3879 	int power, err = 0;
3880 
3881 	power = read_backlight_power(asus);
3882 	if (power != -ENODEV && bd->props.power != power) {
3883 		ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
3884 		err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
3885 					    ctrl_param, NULL);
3886 		if (asus->driver->quirks->store_backlight_power)
3887 			asus->driver->panel_power = bd->props.power;
3888 
3889 		/* When using scalar brightness, updating the brightness
3890 		 * will mess with the backlight power */
3891 		if (asus->driver->quirks->scalar_panel_brightness)
3892 			return err;
3893 	}
3894 
3895 	if (asus->driver->quirks->scalar_panel_brightness)
3896 		ctrl_param = get_scalar_command(bd);
3897 	else
3898 		ctrl_param = bd->props.brightness;
3899 
3900 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
3901 				    ctrl_param, NULL);
3902 
3903 	return err;
3904 }
3905 
3906 static const struct backlight_ops asus_wmi_bl_ops = {
3907 	.get_brightness = read_brightness,
3908 	.update_status = update_bl_status,
3909 };
3910 
3911 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
3912 {
3913 	struct backlight_device *bd = asus->backlight_device;
3914 	int old = bd->props.brightness;
3915 	int new = old;
3916 
3917 	if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3918 		new = code - NOTIFY_BRNUP_MIN + 1;
3919 	else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3920 		new = code - NOTIFY_BRNDOWN_MIN;
3921 
3922 	bd->props.brightness = new;
3923 	backlight_update_status(bd);
3924 	backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
3925 
3926 	return old;
3927 }
3928 
3929 static int asus_wmi_backlight_init(struct asus_wmi *asus)
3930 {
3931 	struct backlight_device *bd;
3932 	struct backlight_properties props;
3933 	int max;
3934 	int power;
3935 
3936 	max = read_brightness_max(asus);
3937 	if (max < 0)
3938 		return max;
3939 
3940 	power = read_backlight_power(asus);
3941 	if (power == -ENODEV)
3942 		power = FB_BLANK_UNBLANK;
3943 	else if (power < 0)
3944 		return power;
3945 
3946 	memset(&props, 0, sizeof(struct backlight_properties));
3947 	props.type = BACKLIGHT_PLATFORM;
3948 	props.max_brightness = max;
3949 	bd = backlight_device_register(asus->driver->name,
3950 				       &asus->platform_device->dev, asus,
3951 				       &asus_wmi_bl_ops, &props);
3952 	if (IS_ERR(bd)) {
3953 		pr_err("Could not register backlight device\n");
3954 		return PTR_ERR(bd);
3955 	}
3956 
3957 	asus->backlight_device = bd;
3958 
3959 	if (asus->driver->quirks->store_backlight_power)
3960 		asus->driver->panel_power = power;
3961 
3962 	bd->props.brightness = read_brightness(bd);
3963 	bd->props.power = power;
3964 	backlight_update_status(bd);
3965 
3966 	asus->driver->brightness = bd->props.brightness;
3967 
3968 	return 0;
3969 }
3970 
3971 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
3972 {
3973 	backlight_device_unregister(asus->backlight_device);
3974 
3975 	asus->backlight_device = NULL;
3976 }
3977 
3978 static int is_display_toggle(int code)
3979 {
3980 	/* display toggle keys */
3981 	if ((code >= 0x61 && code <= 0x67) ||
3982 	    (code >= 0x8c && code <= 0x93) ||
3983 	    (code >= 0xa0 && code <= 0xa7) ||
3984 	    (code >= 0xd0 && code <= 0xd5))
3985 		return 1;
3986 
3987 	return 0;
3988 }
3989 
3990 /* Screenpad backlight *******************************************************/
3991 
3992 static int read_screenpad_backlight_power(struct asus_wmi *asus)
3993 {
3994 	int ret;
3995 
3996 	ret = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_SCREENPAD_POWER);
3997 	if (ret < 0)
3998 		return ret;
3999 	/* 1 == powered */
4000 	return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
4001 }
4002 
4003 static int read_screenpad_brightness(struct backlight_device *bd)
4004 {
4005 	struct asus_wmi *asus = bl_get_data(bd);
4006 	u32 retval;
4007 	int err;
4008 
4009 	err = read_screenpad_backlight_power(asus);
4010 	if (err < 0)
4011 		return err;
4012 	/* The device brightness can only be read if powered, so return stored */
4013 	if (err == FB_BLANK_POWERDOWN)
4014 		return asus->driver->screenpad_brightness - ASUS_SCREENPAD_BRIGHT_MIN;
4015 
4016 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT, &retval);
4017 	if (err < 0)
4018 		return err;
4019 
4020 	return (retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK) - ASUS_SCREENPAD_BRIGHT_MIN;
4021 }
4022 
4023 static int update_screenpad_bl_status(struct backlight_device *bd)
4024 {
4025 	struct asus_wmi *asus = bl_get_data(bd);
4026 	int power, err = 0;
4027 	u32 ctrl_param;
4028 
4029 	power = read_screenpad_backlight_power(asus);
4030 	if (power < 0)
4031 		return power;
4032 
4033 	if (bd->props.power != power) {
4034 		if (power != FB_BLANK_UNBLANK) {
4035 			/* Only brightness > 0 can power it back on */
4036 			ctrl_param = asus->driver->screenpad_brightness - ASUS_SCREENPAD_BRIGHT_MIN;
4037 			err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_LIGHT,
4038 						    ctrl_param, NULL);
4039 		} else {
4040 			err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_POWER, 0, NULL);
4041 		}
4042 	} else if (power == FB_BLANK_UNBLANK) {
4043 		/* Only set brightness if powered on or we get invalid/unsync state */
4044 		ctrl_param = bd->props.brightness + ASUS_SCREENPAD_BRIGHT_MIN;
4045 		err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_LIGHT, ctrl_param, NULL);
4046 	}
4047 
4048 	/* Ensure brightness is stored to turn back on with */
4049 	if (err == 0)
4050 		asus->driver->screenpad_brightness = bd->props.brightness + ASUS_SCREENPAD_BRIGHT_MIN;
4051 
4052 	return err;
4053 }
4054 
4055 static const struct backlight_ops asus_screenpad_bl_ops = {
4056 	.get_brightness = read_screenpad_brightness,
4057 	.update_status = update_screenpad_bl_status,
4058 	.options = BL_CORE_SUSPENDRESUME,
4059 };
4060 
4061 static int asus_screenpad_init(struct asus_wmi *asus)
4062 {
4063 	struct backlight_device *bd;
4064 	struct backlight_properties props;
4065 	int err, power;
4066 	int brightness = 0;
4067 
4068 	power = read_screenpad_backlight_power(asus);
4069 	if (power < 0)
4070 		return power;
4071 
4072 	if (power != FB_BLANK_POWERDOWN) {
4073 		err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT, &brightness);
4074 		if (err < 0)
4075 			return err;
4076 	}
4077 	/* default to an acceptable min brightness on boot if too low */
4078 	if (brightness < ASUS_SCREENPAD_BRIGHT_MIN)
4079 		brightness = ASUS_SCREENPAD_BRIGHT_DEFAULT;
4080 
4081 	memset(&props, 0, sizeof(struct backlight_properties));
4082 	props.type = BACKLIGHT_RAW; /* ensure this bd is last to be picked */
4083 	props.max_brightness = ASUS_SCREENPAD_BRIGHT_MAX - ASUS_SCREENPAD_BRIGHT_MIN;
4084 	bd = backlight_device_register("asus_screenpad",
4085 				       &asus->platform_device->dev, asus,
4086 				       &asus_screenpad_bl_ops, &props);
4087 	if (IS_ERR(bd)) {
4088 		pr_err("Could not register backlight device\n");
4089 		return PTR_ERR(bd);
4090 	}
4091 
4092 	asus->screenpad_backlight_device = bd;
4093 	asus->driver->screenpad_brightness = brightness;
4094 	bd->props.brightness = brightness - ASUS_SCREENPAD_BRIGHT_MIN;
4095 	bd->props.power = power;
4096 	backlight_update_status(bd);
4097 
4098 	return 0;
4099 }
4100 
4101 static void asus_screenpad_exit(struct asus_wmi *asus)
4102 {
4103 	backlight_device_unregister(asus->screenpad_backlight_device);
4104 
4105 	asus->screenpad_backlight_device = NULL;
4106 }
4107 
4108 /* Fn-lock ********************************************************************/
4109 
4110 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
4111 {
4112 	u32 result;
4113 
4114 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
4115 
4116 	return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
4117 		!(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
4118 }
4119 
4120 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
4121 {
4122 	int mode = asus->fnlock_locked;
4123 
4124 	asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
4125 }
4126 
4127 /* WMI events *****************************************************************/
4128 
4129 static int asus_wmi_get_event_code(u32 value)
4130 {
4131 	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
4132 	union acpi_object *obj;
4133 	acpi_status status;
4134 	int code;
4135 
4136 	status = wmi_get_event_data(value, &response);
4137 	if (ACPI_FAILURE(status)) {
4138 		pr_warn("Failed to get WMI notify code: %s\n",
4139 				acpi_format_exception(status));
4140 		return -EIO;
4141 	}
4142 
4143 	obj = (union acpi_object *)response.pointer;
4144 
4145 	if (obj && obj->type == ACPI_TYPE_INTEGER)
4146 		code = (int)(obj->integer.value & WMI_EVENT_MASK);
4147 	else
4148 		code = -EIO;
4149 
4150 	kfree(obj);
4151 	return code;
4152 }
4153 
4154 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
4155 {
4156 	unsigned int key_value = 1;
4157 	bool autorelease = 1;
4158 
4159 	if (asus->driver->key_filter) {
4160 		asus->driver->key_filter(asus->driver, &code, &key_value,
4161 					 &autorelease);
4162 		if (code == ASUS_WMI_KEY_IGNORE)
4163 			return;
4164 	}
4165 
4166 	if (acpi_video_get_backlight_type() == acpi_backlight_vendor &&
4167 	    code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNDOWN_MAX) {
4168 		asus_wmi_backlight_notify(asus, code);
4169 		return;
4170 	}
4171 
4172 	if (code == NOTIFY_KBD_BRTUP) {
4173 		kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
4174 		return;
4175 	}
4176 	if (code == NOTIFY_KBD_BRTDWN) {
4177 		kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
4178 		return;
4179 	}
4180 	if (code == NOTIFY_KBD_BRTTOGGLE) {
4181 		if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
4182 			kbd_led_set_by_kbd(asus, 0);
4183 		else
4184 			kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
4185 		return;
4186 	}
4187 
4188 	if (code == NOTIFY_FNLOCK_TOGGLE) {
4189 		asus->fnlock_locked = !asus->fnlock_locked;
4190 		asus_wmi_fnlock_update(asus);
4191 		return;
4192 	}
4193 
4194 	if (code == asus->tablet_switch_event_code) {
4195 		asus_wmi_tablet_mode_get_state(asus);
4196 		return;
4197 	}
4198 
4199 	if (code == NOTIFY_KBD_FBM || code == NOTIFY_KBD_TTP) {
4200 		if (asus->fan_boost_mode_available)
4201 			fan_boost_mode_switch_next(asus);
4202 		if (asus->throttle_thermal_policy_available)
4203 			throttle_thermal_policy_switch_next(asus);
4204 		return;
4205 
4206 	}
4207 
4208 	if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
4209 		return;
4210 
4211 	if (!sparse_keymap_report_event(asus->inputdev, code,
4212 					key_value, autorelease))
4213 		pr_info("Unknown key code 0x%x\n", code);
4214 }
4215 
4216 static void asus_wmi_notify(u32 value, void *context)
4217 {
4218 	struct asus_wmi *asus = context;
4219 	int code = asus_wmi_get_event_code(value);
4220 
4221 	if (code < 0) {
4222 		pr_warn("Failed to get notify code: %d\n", code);
4223 		return;
4224 	}
4225 
4226 	asus_wmi_handle_event_code(code, asus);
4227 }
4228 
4229 /* Sysfs **********************************************************************/
4230 
4231 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
4232 			     const char *buf, size_t count)
4233 {
4234 	u32 retval;
4235 	int err, value;
4236 
4237 	value = asus_wmi_get_devstate_simple(asus, devid);
4238 	if (value < 0)
4239 		return value;
4240 
4241 	err = kstrtoint(buf, 0, &value);
4242 	if (err)
4243 		return err;
4244 
4245 	err = asus_wmi_set_devstate(devid, value, &retval);
4246 	if (err < 0)
4247 		return err;
4248 
4249 	return count;
4250 }
4251 
4252 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
4253 {
4254 	int value = asus_wmi_get_devstate_simple(asus, devid);
4255 
4256 	if (value < 0)
4257 		return value;
4258 
4259 	return sysfs_emit(buf, "%d\n", value);
4260 }
4261 
4262 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm)			\
4263 	static ssize_t show_##_name(struct device *dev,			\
4264 				    struct device_attribute *attr,	\
4265 				    char *buf)				\
4266 	{								\
4267 		struct asus_wmi *asus = dev_get_drvdata(dev);		\
4268 									\
4269 		return show_sys_wmi(asus, _cm, buf);			\
4270 	}								\
4271 	static ssize_t store_##_name(struct device *dev,		\
4272 				     struct device_attribute *attr,	\
4273 				     const char *buf, size_t count)	\
4274 	{								\
4275 		struct asus_wmi *asus = dev_get_drvdata(dev);		\
4276 									\
4277 		return store_sys_wmi(asus, _cm, buf, count);		\
4278 	}								\
4279 	static struct device_attribute dev_attr_##_name = {		\
4280 		.attr = {						\
4281 			.name = __stringify(_name),			\
4282 			.mode = _mode },				\
4283 		.show   = show_##_name,					\
4284 		.store  = store_##_name,				\
4285 	}
4286 
4287 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
4288 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
4289 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
4290 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
4291 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
4292 
4293 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
4294 			   const char *buf, size_t count)
4295 {
4296 	int value, rv;
4297 
4298 	rv = kstrtoint(buf, 0, &value);
4299 	if (rv)
4300 		return rv;
4301 
4302 	if (value < 0 || value > 2)
4303 		return -EINVAL;
4304 
4305 	rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
4306 	if (rv < 0)
4307 		return rv;
4308 
4309 	return count;
4310 }
4311 
4312 static DEVICE_ATTR_WO(cpufv);
4313 
4314 static struct attribute *platform_attributes[] = {
4315 	&dev_attr_cpufv.attr,
4316 	&dev_attr_camera.attr,
4317 	&dev_attr_cardr.attr,
4318 	&dev_attr_touchpad.attr,
4319 	&dev_attr_charge_mode.attr,
4320 	&dev_attr_egpu_enable.attr,
4321 	&dev_attr_egpu_connected.attr,
4322 	&dev_attr_dgpu_disable.attr,
4323 	&dev_attr_gpu_mux_mode.attr,
4324 	&dev_attr_lid_resume.attr,
4325 	&dev_attr_als_enable.attr,
4326 	&dev_attr_fan_boost_mode.attr,
4327 	&dev_attr_throttle_thermal_policy.attr,
4328 	&dev_attr_ppt_pl2_sppt.attr,
4329 	&dev_attr_ppt_pl1_spl.attr,
4330 	&dev_attr_ppt_fppt.attr,
4331 	&dev_attr_ppt_apu_sppt.attr,
4332 	&dev_attr_ppt_platform_sppt.attr,
4333 	&dev_attr_nv_dynamic_boost.attr,
4334 	&dev_attr_nv_temp_target.attr,
4335 	&dev_attr_mcu_powersave.attr,
4336 	&dev_attr_boot_sound.attr,
4337 	&dev_attr_panel_od.attr,
4338 	&dev_attr_mini_led_mode.attr,
4339 	&dev_attr_available_mini_led_mode.attr,
4340 	NULL
4341 };
4342 
4343 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
4344 				    struct attribute *attr, int idx)
4345 {
4346 	struct device *dev = kobj_to_dev(kobj);
4347 	struct asus_wmi *asus = dev_get_drvdata(dev);
4348 	bool ok = true;
4349 	int devid = -1;
4350 
4351 	if (attr == &dev_attr_camera.attr)
4352 		devid = ASUS_WMI_DEVID_CAMERA;
4353 	else if (attr == &dev_attr_cardr.attr)
4354 		devid = ASUS_WMI_DEVID_CARDREADER;
4355 	else if (attr == &dev_attr_touchpad.attr)
4356 		devid = ASUS_WMI_DEVID_TOUCHPAD;
4357 	else if (attr == &dev_attr_lid_resume.attr)
4358 		devid = ASUS_WMI_DEVID_LID_RESUME;
4359 	else if (attr == &dev_attr_als_enable.attr)
4360 		devid = ASUS_WMI_DEVID_ALS_ENABLE;
4361 	else if (attr == &dev_attr_charge_mode.attr)
4362 		devid = ASUS_WMI_DEVID_CHARGE_MODE;
4363 	else if (attr == &dev_attr_egpu_enable.attr)
4364 		ok = asus->egpu_enable_available;
4365 	else if (attr == &dev_attr_egpu_connected.attr)
4366 		devid = ASUS_WMI_DEVID_EGPU_CONNECTED;
4367 	else if (attr == &dev_attr_dgpu_disable.attr)
4368 		ok = asus->dgpu_disable_available;
4369 	else if (attr == &dev_attr_gpu_mux_mode.attr)
4370 		ok = asus->gpu_mux_dev != 0;
4371 	else if (attr == &dev_attr_fan_boost_mode.attr)
4372 		ok = asus->fan_boost_mode_available;
4373 	else if (attr == &dev_attr_throttle_thermal_policy.attr)
4374 		ok = asus->throttle_thermal_policy_available;
4375 	else if (attr == &dev_attr_ppt_pl2_sppt.attr)
4376 		devid = ASUS_WMI_DEVID_PPT_PL2_SPPT;
4377 	else if (attr == &dev_attr_ppt_pl1_spl.attr)
4378 		devid = ASUS_WMI_DEVID_PPT_PL1_SPL;
4379 	else if (attr == &dev_attr_ppt_fppt.attr)
4380 		devid = ASUS_WMI_DEVID_PPT_FPPT;
4381 	else if (attr == &dev_attr_ppt_apu_sppt.attr)
4382 		devid = ASUS_WMI_DEVID_PPT_APU_SPPT;
4383 	else if (attr == &dev_attr_ppt_platform_sppt.attr)
4384 		devid = ASUS_WMI_DEVID_PPT_PLAT_SPPT;
4385 	else if (attr == &dev_attr_nv_dynamic_boost.attr)
4386 		devid = ASUS_WMI_DEVID_NV_DYN_BOOST;
4387 	else if (attr == &dev_attr_nv_temp_target.attr)
4388 		devid = ASUS_WMI_DEVID_NV_THERM_TARGET;
4389 	else if (attr == &dev_attr_mcu_powersave.attr)
4390 		devid = ASUS_WMI_DEVID_MCU_POWERSAVE;
4391 	else if (attr == &dev_attr_boot_sound.attr)
4392 		devid = ASUS_WMI_DEVID_BOOT_SOUND;
4393 	else if (attr == &dev_attr_panel_od.attr)
4394 		devid = ASUS_WMI_DEVID_PANEL_OD;
4395 	else if (attr == &dev_attr_mini_led_mode.attr)
4396 		ok = asus->mini_led_dev_id != 0;
4397 	else if (attr == &dev_attr_available_mini_led_mode.attr)
4398 		ok = asus->mini_led_dev_id != 0;
4399 
4400 	if (devid != -1)
4401 		ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
4402 
4403 	return ok ? attr->mode : 0;
4404 }
4405 
4406 static const struct attribute_group platform_attribute_group = {
4407 	.is_visible = asus_sysfs_is_visible,
4408 	.attrs = platform_attributes
4409 };
4410 
4411 static void asus_wmi_sysfs_exit(struct platform_device *device)
4412 {
4413 	sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
4414 }
4415 
4416 static int asus_wmi_sysfs_init(struct platform_device *device)
4417 {
4418 	return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
4419 }
4420 
4421 /* Platform device ************************************************************/
4422 
4423 static int asus_wmi_platform_init(struct asus_wmi *asus)
4424 {
4425 	struct device *dev = &asus->platform_device->dev;
4426 	char *wmi_uid;
4427 	int rv;
4428 
4429 	/* INIT enable hotkeys on some models */
4430 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
4431 		pr_info("Initialization: %#x\n", rv);
4432 
4433 	/* We don't know yet what to do with this version... */
4434 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
4435 		pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
4436 		asus->spec = rv;
4437 	}
4438 
4439 	/*
4440 	 * The SFUN method probably allows the original driver to get the list
4441 	 * of features supported by a given model. For now, 0x0100 or 0x0800
4442 	 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
4443 	 * The significance of others is yet to be found.
4444 	 */
4445 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
4446 		pr_info("SFUN value: %#x\n", rv);
4447 		asus->sfun = rv;
4448 	}
4449 
4450 	/*
4451 	 * Eee PC and Notebooks seems to have different method_id for DSTS,
4452 	 * but it may also be related to the BIOS's SPEC.
4453 	 * Note, on most Eeepc, there is no way to check if a method exist
4454 	 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
4455 	 * but once again, SPEC may probably be used for that kind of things.
4456 	 *
4457 	 * Additionally at least TUF Gaming series laptops return nothing for
4458 	 * unknown methods, so the detection in this way is not possible.
4459 	 *
4460 	 * There is strong indication that only ACPI WMI devices that have _UID
4461 	 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
4462 	 */
4463 	wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
4464 	if (!wmi_uid)
4465 		return -ENODEV;
4466 
4467 	if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
4468 		dev_info(dev, "Detected ASUSWMI, use DCTS\n");
4469 		asus->dsts_id = ASUS_WMI_METHODID_DCTS;
4470 	} else {
4471 		dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
4472 		asus->dsts_id = ASUS_WMI_METHODID_DSTS;
4473 	}
4474 
4475 	/* CWAP allow to define the behavior of the Fn+F2 key,
4476 	 * this method doesn't seems to be present on Eee PCs */
4477 	if (asus->driver->quirks->wapf >= 0)
4478 		asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
4479 				      asus->driver->quirks->wapf, NULL);
4480 
4481 	return 0;
4482 }
4483 
4484 /* debugfs ********************************************************************/
4485 
4486 struct asus_wmi_debugfs_node {
4487 	struct asus_wmi *asus;
4488 	char *name;
4489 	int (*show) (struct seq_file *m, void *data);
4490 };
4491 
4492 static int show_dsts(struct seq_file *m, void *data)
4493 {
4494 	struct asus_wmi *asus = m->private;
4495 	int err;
4496 	u32 retval = -1;
4497 
4498 	err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
4499 	if (err < 0)
4500 		return err;
4501 
4502 	seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
4503 
4504 	return 0;
4505 }
4506 
4507 static int show_devs(struct seq_file *m, void *data)
4508 {
4509 	struct asus_wmi *asus = m->private;
4510 	int err;
4511 	u32 retval = -1;
4512 
4513 	err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
4514 				    &retval);
4515 	if (err < 0)
4516 		return err;
4517 
4518 	seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
4519 		   asus->debug.ctrl_param, retval);
4520 
4521 	return 0;
4522 }
4523 
4524 static int show_call(struct seq_file *m, void *data)
4525 {
4526 	struct asus_wmi *asus = m->private;
4527 	struct bios_args args = {
4528 		.arg0 = asus->debug.dev_id,
4529 		.arg1 = asus->debug.ctrl_param,
4530 	};
4531 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
4532 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
4533 	union acpi_object *obj;
4534 	acpi_status status;
4535 
4536 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
4537 				     0, asus->debug.method_id,
4538 				     &input, &output);
4539 
4540 	if (ACPI_FAILURE(status))
4541 		return -EIO;
4542 
4543 	obj = (union acpi_object *)output.pointer;
4544 	if (obj && obj->type == ACPI_TYPE_INTEGER)
4545 		seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
4546 			   asus->debug.dev_id, asus->debug.ctrl_param,
4547 			   (u32) obj->integer.value);
4548 	else
4549 		seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
4550 			   asus->debug.dev_id, asus->debug.ctrl_param,
4551 			   obj ? obj->type : -1);
4552 
4553 	kfree(obj);
4554 
4555 	return 0;
4556 }
4557 
4558 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
4559 	{NULL, "devs", show_devs},
4560 	{NULL, "dsts", show_dsts},
4561 	{NULL, "call", show_call},
4562 };
4563 
4564 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
4565 {
4566 	struct asus_wmi_debugfs_node *node = inode->i_private;
4567 
4568 	return single_open(file, node->show, node->asus);
4569 }
4570 
4571 static const struct file_operations asus_wmi_debugfs_io_ops = {
4572 	.owner = THIS_MODULE,
4573 	.open = asus_wmi_debugfs_open,
4574 	.read = seq_read,
4575 	.llseek = seq_lseek,
4576 	.release = single_release,
4577 };
4578 
4579 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
4580 {
4581 	debugfs_remove_recursive(asus->debug.root);
4582 }
4583 
4584 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
4585 {
4586 	int i;
4587 
4588 	asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
4589 
4590 	debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
4591 			   &asus->debug.method_id);
4592 
4593 	debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
4594 			   &asus->debug.dev_id);
4595 
4596 	debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
4597 			   &asus->debug.ctrl_param);
4598 
4599 	for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
4600 		struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
4601 
4602 		node->asus = asus;
4603 		debugfs_create_file(node->name, S_IFREG | S_IRUGO,
4604 				    asus->debug.root, node,
4605 				    &asus_wmi_debugfs_io_ops);
4606 	}
4607 }
4608 
4609 /* Init / exit ****************************************************************/
4610 
4611 static int asus_wmi_add(struct platform_device *pdev)
4612 {
4613 	struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
4614 	struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
4615 	struct asus_wmi *asus;
4616 	acpi_status status;
4617 	int err;
4618 	u32 result;
4619 
4620 	asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
4621 	if (!asus)
4622 		return -ENOMEM;
4623 
4624 	asus->driver = wdrv;
4625 	asus->platform_device = pdev;
4626 	wdrv->platform_device = pdev;
4627 	platform_set_drvdata(asus->platform_device, asus);
4628 
4629 	if (wdrv->detect_quirks)
4630 		wdrv->detect_quirks(asus->driver);
4631 
4632 	err = asus_wmi_platform_init(asus);
4633 	if (err)
4634 		goto fail_platform;
4635 
4636 	/* ensure defaults for tunables */
4637 	asus->ppt_pl2_sppt = 5;
4638 	asus->ppt_pl1_spl = 5;
4639 	asus->ppt_apu_sppt = 5;
4640 	asus->ppt_platform_sppt = 5;
4641 	asus->ppt_fppt = 5;
4642 	asus->nv_dynamic_boost = 5;
4643 	asus->nv_temp_target = 75;
4644 
4645 	asus->egpu_enable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_EGPU);
4646 	asus->dgpu_disable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_DGPU);
4647 	asus->kbd_rgb_state_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_STATE);
4648 	asus->ally_mcu_usb_switch = acpi_has_method(NULL, ASUS_USB0_PWR_EC0_CSEE)
4649 						&& dmi_match(DMI_BOARD_NAME, "RC71L");
4650 
4651 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MINI_LED_MODE))
4652 		asus->mini_led_dev_id = ASUS_WMI_DEVID_MINI_LED_MODE;
4653 	else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MINI_LED_MODE2))
4654 		asus->mini_led_dev_id = ASUS_WMI_DEVID_MINI_LED_MODE2;
4655 
4656 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_MUX))
4657 		asus->gpu_mux_dev = ASUS_WMI_DEVID_GPU_MUX;
4658 	else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_MUX_VIVO))
4659 		asus->gpu_mux_dev = ASUS_WMI_DEVID_GPU_MUX_VIVO;
4660 
4661 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_MODE))
4662 		asus->kbd_rgb_dev = ASUS_WMI_DEVID_TUF_RGB_MODE;
4663 	else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_MODE2))
4664 		asus->kbd_rgb_dev = ASUS_WMI_DEVID_TUF_RGB_MODE2;
4665 
4666 	err = fan_boost_mode_check_present(asus);
4667 	if (err)
4668 		goto fail_fan_boost_mode;
4669 
4670 	err = throttle_thermal_policy_check_present(asus);
4671 	if (err)
4672 		goto fail_throttle_thermal_policy;
4673 	else
4674 		throttle_thermal_policy_set_default(asus);
4675 
4676 	err = platform_profile_setup(asus);
4677 	if (err)
4678 		goto fail_platform_profile_setup;
4679 
4680 	err = asus_wmi_sysfs_init(asus->platform_device);
4681 	if (err)
4682 		goto fail_sysfs;
4683 
4684 	err = asus_wmi_input_init(asus);
4685 	if (err)
4686 		goto fail_input;
4687 
4688 	err = asus_wmi_fan_init(asus); /* probably no problems on error */
4689 
4690 	err = asus_wmi_hwmon_init(asus);
4691 	if (err)
4692 		goto fail_hwmon;
4693 
4694 	err = asus_wmi_custom_fan_curve_init(asus);
4695 	if (err)
4696 		goto fail_custom_fan_curve;
4697 
4698 	err = asus_wmi_led_init(asus);
4699 	if (err)
4700 		goto fail_leds;
4701 
4702 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
4703 	if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
4704 		asus->driver->wlan_ctrl_by_user = 1;
4705 
4706 	if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
4707 		err = asus_wmi_rfkill_init(asus);
4708 		if (err)
4709 			goto fail_rfkill;
4710 	}
4711 
4712 	if (asus->driver->quirks->wmi_force_als_set)
4713 		asus_wmi_set_als();
4714 
4715 	if (asus->driver->quirks->xusb2pr)
4716 		asus_wmi_set_xusb2pr(asus);
4717 
4718 	if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
4719 		err = asus_wmi_backlight_init(asus);
4720 		if (err && err != -ENODEV)
4721 			goto fail_backlight;
4722 	} else if (asus->driver->quirks->wmi_backlight_set_devstate)
4723 		err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
4724 
4725 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT)) {
4726 		err = asus_screenpad_init(asus);
4727 		if (err && err != -ENODEV)
4728 			goto fail_screenpad;
4729 	}
4730 
4731 	if (asus_wmi_has_fnlock_key(asus)) {
4732 		asus->fnlock_locked = fnlock_default;
4733 		asus_wmi_fnlock_update(asus);
4734 	}
4735 
4736 	status = wmi_install_notify_handler(asus->driver->event_guid,
4737 					    asus_wmi_notify, asus);
4738 	if (ACPI_FAILURE(status)) {
4739 		pr_err("Unable to register notify handler - %d\n", status);
4740 		err = -ENODEV;
4741 		goto fail_wmi_handler;
4742 	}
4743 
4744 	if (asus->driver->i8042_filter) {
4745 		err = i8042_install_filter(asus->driver->i8042_filter);
4746 		if (err)
4747 			pr_warn("Unable to install key filter - %d\n", err);
4748 	}
4749 
4750 	asus_wmi_battery_init(asus);
4751 
4752 	asus_wmi_debugfs_init(asus);
4753 
4754 	return 0;
4755 
4756 fail_wmi_handler:
4757 	asus_wmi_backlight_exit(asus);
4758 fail_backlight:
4759 	asus_wmi_rfkill_exit(asus);
4760 fail_screenpad:
4761 	asus_screenpad_exit(asus);
4762 fail_rfkill:
4763 	asus_wmi_led_exit(asus);
4764 fail_leds:
4765 fail_hwmon:
4766 	asus_wmi_input_exit(asus);
4767 fail_input:
4768 	asus_wmi_sysfs_exit(asus->platform_device);
4769 fail_sysfs:
4770 fail_throttle_thermal_policy:
4771 fail_custom_fan_curve:
4772 fail_platform_profile_setup:
4773 	if (asus->platform_profile_support)
4774 		platform_profile_remove();
4775 fail_fan_boost_mode:
4776 fail_platform:
4777 	kfree(asus);
4778 	return err;
4779 }
4780 
4781 static void asus_wmi_remove(struct platform_device *device)
4782 {
4783 	struct asus_wmi *asus;
4784 
4785 	asus = platform_get_drvdata(device);
4786 	if (asus->driver->i8042_filter)
4787 		i8042_remove_filter(asus->driver->i8042_filter);
4788 	wmi_remove_notify_handler(asus->driver->event_guid);
4789 	asus_wmi_backlight_exit(asus);
4790 	asus_screenpad_exit(asus);
4791 	asus_wmi_input_exit(asus);
4792 	asus_wmi_led_exit(asus);
4793 	asus_wmi_rfkill_exit(asus);
4794 	asus_wmi_debugfs_exit(asus);
4795 	asus_wmi_sysfs_exit(asus->platform_device);
4796 	asus_fan_set_auto(asus);
4797 	throttle_thermal_policy_set_default(asus);
4798 	asus_wmi_battery_exit(asus);
4799 
4800 	if (asus->platform_profile_support)
4801 		platform_profile_remove();
4802 
4803 	kfree(asus);
4804 }
4805 
4806 /* Platform driver - hibernate/resume callbacks *******************************/
4807 
4808 static int asus_hotk_thaw(struct device *device)
4809 {
4810 	struct asus_wmi *asus = dev_get_drvdata(device);
4811 
4812 	if (asus->wlan.rfkill) {
4813 		bool wlan;
4814 
4815 		/*
4816 		 * Work around bios bug - acpi _PTS turns off the wireless led
4817 		 * during suspend.  Normally it restores it on resume, but
4818 		 * we should kick it ourselves in case hibernation is aborted.
4819 		 */
4820 		wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
4821 		asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
4822 	}
4823 
4824 	return 0;
4825 }
4826 
4827 static int asus_hotk_resume(struct device *device)
4828 {
4829 	struct asus_wmi *asus = dev_get_drvdata(device);
4830 
4831 	if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
4832 		kbd_led_update(asus);
4833 
4834 	if (asus_wmi_has_fnlock_key(asus))
4835 		asus_wmi_fnlock_update(asus);
4836 
4837 	asus_wmi_tablet_mode_get_state(asus);
4838 
4839 	return 0;
4840 }
4841 
4842 static int asus_hotk_resume_early(struct device *device)
4843 {
4844 	struct asus_wmi *asus = dev_get_drvdata(device);
4845 
4846 	if (asus->ally_mcu_usb_switch) {
4847 		/* sleep required to prevent USB0 being yanked then reappearing rapidly */
4848 		if (ACPI_FAILURE(acpi_execute_simple_method(NULL, ASUS_USB0_PWR_EC0_CSEE, 0xB8)))
4849 			dev_err(device, "ROG Ally MCU failed to connect USB dev\n");
4850 		else
4851 			msleep(ASUS_USB0_PWR_EC0_CSEE_WAIT);
4852 	}
4853 	return 0;
4854 }
4855 
4856 static int asus_hotk_prepare(struct device *device)
4857 {
4858 	struct asus_wmi *asus = dev_get_drvdata(device);
4859 
4860 	if (asus->ally_mcu_usb_switch) {
4861 		/* sleep required to ensure USB0 is disabled before sleep continues */
4862 		if (ACPI_FAILURE(acpi_execute_simple_method(NULL, ASUS_USB0_PWR_EC0_CSEE, 0xB7)))
4863 			dev_err(device, "ROG Ally MCU failed to disconnect USB dev\n");
4864 		else
4865 			msleep(ASUS_USB0_PWR_EC0_CSEE_WAIT);
4866 	}
4867 	return 0;
4868 }
4869 
4870 static int asus_hotk_restore(struct device *device)
4871 {
4872 	struct asus_wmi *asus = dev_get_drvdata(device);
4873 	int bl;
4874 
4875 	/* Refresh both wlan rfkill state and pci hotplug */
4876 	if (asus->wlan.rfkill)
4877 		asus_rfkill_hotplug(asus);
4878 
4879 	if (asus->bluetooth.rfkill) {
4880 		bl = !asus_wmi_get_devstate_simple(asus,
4881 						   ASUS_WMI_DEVID_BLUETOOTH);
4882 		rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
4883 	}
4884 	if (asus->wimax.rfkill) {
4885 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
4886 		rfkill_set_sw_state(asus->wimax.rfkill, bl);
4887 	}
4888 	if (asus->wwan3g.rfkill) {
4889 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
4890 		rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
4891 	}
4892 	if (asus->gps.rfkill) {
4893 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
4894 		rfkill_set_sw_state(asus->gps.rfkill, bl);
4895 	}
4896 	if (asus->uwb.rfkill) {
4897 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
4898 		rfkill_set_sw_state(asus->uwb.rfkill, bl);
4899 	}
4900 	if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
4901 		kbd_led_update(asus);
4902 
4903 	if (asus_wmi_has_fnlock_key(asus))
4904 		asus_wmi_fnlock_update(asus);
4905 
4906 	asus_wmi_tablet_mode_get_state(asus);
4907 	return 0;
4908 }
4909 
4910 static const struct dev_pm_ops asus_pm_ops = {
4911 	.thaw = asus_hotk_thaw,
4912 	.restore = asus_hotk_restore,
4913 	.resume = asus_hotk_resume,
4914 	.resume_early = asus_hotk_resume_early,
4915 	.prepare = asus_hotk_prepare,
4916 };
4917 
4918 /* Registration ***************************************************************/
4919 
4920 static int asus_wmi_probe(struct platform_device *pdev)
4921 {
4922 	struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
4923 	struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
4924 	int ret;
4925 
4926 	if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
4927 		pr_warn("ASUS Management GUID not found\n");
4928 		return -ENODEV;
4929 	}
4930 
4931 	if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
4932 		pr_warn("ASUS Event GUID not found\n");
4933 		return -ENODEV;
4934 	}
4935 
4936 	if (wdrv->probe) {
4937 		ret = wdrv->probe(pdev);
4938 		if (ret)
4939 			return ret;
4940 	}
4941 
4942 	return asus_wmi_add(pdev);
4943 }
4944 
4945 static bool used;
4946 
4947 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
4948 {
4949 	struct platform_driver *platform_driver;
4950 	struct platform_device *platform_device;
4951 
4952 	if (used)
4953 		return -EBUSY;
4954 
4955 	platform_driver = &driver->platform_driver;
4956 	platform_driver->remove_new = asus_wmi_remove;
4957 	platform_driver->driver.owner = driver->owner;
4958 	platform_driver->driver.name = driver->name;
4959 	platform_driver->driver.pm = &asus_pm_ops;
4960 
4961 	platform_device = platform_create_bundle(platform_driver,
4962 						 asus_wmi_probe,
4963 						 NULL, 0, NULL, 0);
4964 	if (IS_ERR(platform_device))
4965 		return PTR_ERR(platform_device);
4966 
4967 	used = true;
4968 	return 0;
4969 }
4970 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
4971 
4972 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
4973 {
4974 	platform_device_unregister(driver->platform_device);
4975 	platform_driver_unregister(&driver->platform_driver);
4976 	used = false;
4977 }
4978 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
4979 
4980 static int __init asus_wmi_init(void)
4981 {
4982 	pr_info("ASUS WMI generic driver loaded\n");
4983 	return 0;
4984 }
4985 
4986 static void __exit asus_wmi_exit(void)
4987 {
4988 	pr_info("ASUS WMI generic driver unloaded\n");
4989 }
4990 
4991 module_init(asus_wmi_init);
4992 module_exit(asus_wmi_exit);
4993