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