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