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