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