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