blob: e14fb5fa73240b0ec3ef7f22d7a677eac8d90066 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Yong Wangee027e42010-03-21 10:26:34 +08002/*
Corentin Charye12e6d92011-02-26 10:20:31 +01003 * Asus PC WMI hotkey driver
Yong Wangee027e42010-03-21 10:26:34 +08004 *
5 * Copyright(C) 2010 Intel Corporation.
Corentin Chary57ab7da2011-02-26 10:20:32 +01006 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
Yong Wangee027e42010-03-21 10:26:34 +08007 *
8 * Portions based on wistron_btns.c:
9 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
10 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
11 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
Yong Wangee027e42010-03-21 10:26:34 +080012 */
13
Yong Wang81248882010-04-11 09:26:33 +080014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Yong Wangee027e42010-03-21 10:26:34 +080016#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/types.h>
Tejun Heoa32f3922010-04-05 11:37:59 +090020#include <linux/slab.h>
Yong Wangee027e42010-03-21 10:26:34 +080021#include <linux/input.h>
22#include <linux/input/sparse-keymap.h>
Yong Wang3d7b1652010-04-11 09:27:54 +080023#include <linux/fb.h>
24#include <linux/backlight.h>
Corentin Chary084fca62010-11-29 08:14:06 +010025#include <linux/leds.h>
Corentin Charyba48fdb2010-11-29 08:14:07 +010026#include <linux/rfkill.h>
Corentin Charyafa7c882011-02-06 13:28:28 +010027#include <linux/pci.h>
28#include <linux/pci_hotplug.h>
Luke D. Jonesc63d44a2021-08-19 07:07:31 +120029#include <linux/platform_profile.h>
Kristian Klausen79733532019-09-09 19:31:28 +020030#include <linux/power_supply.h>
Corentin Charye07babd2011-02-26 10:20:42 +010031#include <linux/hwmon.h>
32#include <linux/hwmon-sysfs.h>
Corentin Chary8c1b2d82010-11-29 08:14:09 +010033#include <linux/debugfs.h>
34#include <linux/seq_file.h>
Daniel Drakeffb6ce72018-10-09 14:40:55 +080035#include <linux/platform_data/x86/asus-wmi.h>
Yong Wang45f2c692010-04-11 09:27:19 +080036#include <linux/platform_device.h>
Lv Zheng8b484632013-12-03 08:49:16 +080037#include <linux/acpi.h>
Hans de Goeded8c66f62014-07-08 10:47:21 +020038#include <linux/dmi.h>
Akinobu Mitaf07b9fd2020-01-30 22:15:37 -080039#include <linux/units.h>
Kristian Klausen79733532019-09-09 19:31:28 +020040
41#include <acpi/battery.h>
AceLan Kao272c77d2012-06-13 09:32:06 +020042#include <acpi/video.h>
Yong Wangee027e42010-03-21 10:26:34 +080043
Corentin Charye12e6d92011-02-26 10:20:31 +010044#include "asus-wmi.h"
Yong Wang45f2c692010-04-11 09:27:19 +080045
Corentin Chary5909c652012-12-17 16:00:05 -080046MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
Corentin Charye12e6d92011-02-26 10:20:31 +010047 "Yong Wang <yong.y.wang@intel.com>");
48MODULE_DESCRIPTION("Asus Generic WMI Driver");
Yong Wangee027e42010-03-21 10:26:34 +080049MODULE_LICENSE("GPL");
50
Luca Stefanice357fd2021-03-23 22:01:26 +010051static bool fnlock_default = true;
52module_param(fnlock_default, bool, 0444);
53
Corentin Charye12e6d92011-02-26 10:20:31 +010054#define to_asus_wmi_driver(pdrv) \
55 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
Yong Wangee027e42010-03-21 10:26:34 +080056
Corentin Charye12e6d92011-02-26 10:20:31 +010057#define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080058
Corentin Chary33e0e6f2011-02-06 13:28:34 +010059#define NOTIFY_BRNUP_MIN 0x11
60#define NOTIFY_BRNUP_MAX 0x1f
61#define NOTIFY_BRNDOWN_MIN 0x20
62#define NOTIFY_BRNDOWN_MAX 0x2e
Chris Chiu487579b2019-04-18 14:46:48 +080063#define NOTIFY_FNLOCK_TOGGLE 0x4e
Hans de Goedeb0dbd972020-05-10 14:24:31 +020064#define NOTIFY_KBD_DOCK_CHANGE 0x75
Corentin Charye9809c02011-07-01 11:34:31 +020065#define NOTIFY_KBD_BRTUP 0xc4
66#define NOTIFY_KBD_BRTDWN 0xc5
Chris Chiued99d292018-06-20 22:46:45 +080067#define NOTIFY_KBD_BRTTOGGLE 0xc7
Yurii Pavlovskyib096f622019-05-14 21:07:05 +020068#define NOTIFY_KBD_FBM 0x99
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +020069#define NOTIFY_KBD_TTP 0xae
Samuel Čavojea856ec2020-10-21 00:09:44 +020070#define NOTIFY_LID_FLIP 0xfa
Yong Wangee027e42010-03-21 10:26:34 +080071
Chris Chiu487579b2019-04-18 14:46:48 +080072#define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
73
Kast Bernd53e755c2015-05-13 16:24:16 +020074#define ASUS_FAN_DESC "cpu_fan"
75#define ASUS_FAN_MFUN 0x13
76#define ASUS_FAN_SFUN_READ 0x06
77#define ASUS_FAN_SFUN_WRITE 0x07
Daniel Drake2889ffc2019-07-29 16:27:37 +080078
79/* Based on standard hwmon pwmX_enable values */
Daniel Drakee3168b82019-07-29 16:27:39 +080080#define ASUS_FAN_CTRL_FULLSPEED 0
Kast Bernd53e755c2015-05-13 16:24:16 +020081#define ASUS_FAN_CTRL_MANUAL 1
82#define ASUS_FAN_CTRL_AUTO 2
83
Daniel Drake9af93db2019-07-17 13:10:58 +080084#define ASUS_FAN_BOOST_MODE_NORMAL 0
85#define ASUS_FAN_BOOST_MODE_OVERBOOST 1
86#define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01
87#define ASUS_FAN_BOOST_MODE_SILENT 2
88#define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02
89#define ASUS_FAN_BOOST_MODES_MASK 0x03
Yurii Pavlovskyib096f622019-05-14 21:07:05 +020090
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +020091#define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT 0
92#define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST 1
93#define ASUS_THROTTLE_THERMAL_POLICY_SILENT 2
94
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +080095#define USB_INTEL_XUSB2PR 0xD0
96#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
97
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +020098#define ASUS_ACPI_UID_ASUSWMI "ASUSWMI"
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +020099#define ASUS_ACPI_UID_ATK "ATK"
100
101#define WMI_EVENT_QUEUE_SIZE 0x10
102#define WMI_EVENT_QUEUE_END 0x1
103#define WMI_EVENT_MASK 0xFFFF
104/* The WMI hotkey event value is always the same. */
105#define WMI_EVENT_VALUE_ATK 0xFF
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +0200106
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +0200107#define WMI_EVENT_MASK 0xFFFF
108
João Paulo Rechi Vita71050ae2017-02-20 14:50:22 -0500109static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
110
João Paulo Rechi Vitacf48bf92018-05-22 14:30:15 -0700111static bool ashs_present(void)
112{
113 int i = 0;
114 while (ashs_ids[i]) {
115 if (acpi_dev_found(ashs_ids[i++]))
116 return true;
117 }
118 return false;
119}
120
Yong Wang3d7b1652010-04-11 09:27:54 +0800121struct bios_args {
Corentin Charyd33da3b2011-02-26 10:20:35 +0100122 u32 arg0;
123 u32 arg1;
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200124 u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
Chris Chiu7b91f152020-05-22 15:44:24 +0800125 u32 arg4;
126 u32 arg5;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100127} __packed;
Yong Wang3d7b1652010-04-11 09:27:54 +0800128
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100129/*
Kast Bernd53e755c2015-05-13 16:24:16 +0200130 * Struct that's used for all methods called via AGFN. Naming is
131 * identically to the AML code.
132 */
133struct agfn_args {
134 u16 mfun; /* probably "Multi-function" to be called */
135 u16 sfun; /* probably "Sub-function" to be called */
136 u16 len; /* size of the hole struct, including subfunction fields */
137 u8 stas; /* not used by now */
138 u8 err; /* zero on success */
139} __packed;
140
141/* struct used for calling fan read and write methods */
Daniel Drake2889ffc2019-07-29 16:27:37 +0800142struct agfn_fan_args {
Kast Bernd53e755c2015-05-13 16:24:16 +0200143 struct agfn_args agfn; /* common fields */
144 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
145 u32 speed; /* read: RPM/100 - write: 0-255 */
146} __packed;
147
148/*
Corentin Charye12e6d92011-02-26 10:20:31 +0100149 * <platform>/ - debugfs root directory
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100150 * dev_id - current dev_id
151 * ctrl_param - current ctrl_param
Corentin Charyef343492011-02-26 10:20:39 +0100152 * method_id - current method_id
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100153 * devs - call DEVS(dev_id, ctrl_param) and print result
154 * dsts - call DSTS(dev_id) and print result
Corentin Charyef343492011-02-26 10:20:39 +0100155 * call - call method_id(dev_id, ctrl_param) and print result
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100156 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100157struct asus_wmi_debug {
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100158 struct dentry *root;
Corentin Charyef343492011-02-26 10:20:39 +0100159 u32 method_id;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100160 u32 dev_id;
161 u32 ctrl_param;
162};
163
Corentin Charya7ce3f02011-02-26 10:20:33 +0100164struct asus_rfkill {
165 struct asus_wmi *asus;
166 struct rfkill *rfkill;
167 u32 dev_id;
168};
169
Daniel Drake2889ffc2019-07-29 16:27:37 +0800170enum fan_type {
171 FAN_TYPE_NONE = 0,
172 FAN_TYPE_AGFN, /* deprecated on newer platforms */
Daniel Drakee3168b82019-07-29 16:27:39 +0800173 FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */
Daniel Drake2889ffc2019-07-29 16:27:37 +0800174};
175
Corentin Charye12e6d92011-02-26 10:20:31 +0100176struct asus_wmi {
Corentin Chary1d070f82011-02-26 10:20:36 +0100177 int dsts_id;
Corentin Chary46dbca82011-02-26 10:20:38 +0100178 int spec;
179 int sfun;
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +0200180 bool wmi_event_queue;
Corentin Chary1d070f82011-02-26 10:20:36 +0100181
Yong Wang81248882010-04-11 09:26:33 +0800182 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800183 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100184 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100185
AceLan Kao6cae06e2012-07-27 16:51:59 +0800186 struct led_classdev wlan_led;
187 int wlan_led_wk;
Corentin Chary084fca62010-11-29 08:14:06 +0100188 struct led_classdev tpd_led;
189 int tpd_led_wk;
Corentin Charye9809c02011-07-01 11:34:31 +0200190 struct led_classdev kbd_led;
191 int kbd_led_wk;
Maxime Bellengé4c059842017-09-23 18:23:37 +0200192 struct led_classdev lightbar_led;
193 int lightbar_led_wk;
Corentin Chary084fca62010-11-29 08:14:06 +0100194 struct workqueue_struct *led_workqueue;
195 struct work_struct tpd_led_work;
AceLan Kao6cae06e2012-07-27 16:51:59 +0800196 struct work_struct wlan_led_work;
Maxime Bellengé4c059842017-09-23 18:23:37 +0200197 struct work_struct lightbar_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100198
Corentin Charya7ce3f02011-02-26 10:20:33 +0100199 struct asus_rfkill wlan;
200 struct asus_rfkill bluetooth;
201 struct asus_rfkill wimax;
202 struct asus_rfkill wwan3g;
Corentin Chary43be8bd2011-07-01 11:34:40 +0200203 struct asus_rfkill gps;
Corentin Charya912d322011-07-01 11:34:41 +0200204 struct asus_rfkill uwb;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100205
Daniel Drake2889ffc2019-07-29 16:27:37 +0800206 enum fan_type fan_type;
207 int fan_pwm_mode;
208 int agfn_pwm;
Kast Bernd53e755c2015-05-13 16:24:16 +0200209
Daniel Drake9af93db2019-07-17 13:10:58 +0800210 bool fan_boost_mode_available;
211 u8 fan_boost_mode_mask;
212 u8 fan_boost_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +0200213
Luke D. Jones382b91d2021-08-07 14:36:56 +1200214 bool egpu_enable_available; // 0 = enable
215 bool egpu_enable;
216
Luke D. Jones98829e82021-08-07 14:36:55 +1200217 bool dgpu_disable_available;
218 bool dgpu_disable;
219
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +0200220 bool throttle_thermal_policy_available;
221 u8 throttle_thermal_policy_mode;
222
Luke D. Jonesc63d44a2021-08-19 07:07:31 +1200223 struct platform_profile_handler platform_profile_handler;
224 bool platform_profile_support;
225
Kristian Klausen79733532019-09-09 19:31:28 +0200226 // The RSOC controls the maximum charging percentage.
227 bool battery_rsoc_available;
Kristian Klausend507a542019-08-05 21:23:05 +0200228
Luke D. Jonesca91ea342021-08-07 14:36:54 +1200229 bool panel_overdrive_available;
230 bool panel_overdrive;
231
Lukas Wunner125450f2018-09-08 09:59:01 +0200232 struct hotplug_slot hotplug_slot;
Corentin Charyafa7c882011-02-06 13:28:28 +0100233 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100234 struct mutex wmi_lock;
235 struct workqueue_struct *hotplug_workqueue;
236 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100237
Chris Chiu487579b2019-04-18 14:46:48 +0800238 bool fnlock_locked;
239
Corentin Charye12e6d92011-02-26 10:20:31 +0100240 struct asus_wmi_debug debug;
241
242 struct asus_wmi_driver *driver;
Yong Wang81248882010-04-11 09:26:33 +0800243};
244
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +0200245/* WMI ************************************************************************/
246
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200247static int asus_wmi_evaluate_method3(u32 method_id,
248 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800249{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100250 struct bios_args args = {
251 .arg0 = arg0,
252 .arg1 = arg1,
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200253 .arg2 = arg2,
Corentin Charyd33da3b2011-02-26 10:20:35 +0100254 };
255 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
Yong Wang3d7b1652010-04-11 09:27:54 +0800256 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Yong Wang3d7b1652010-04-11 09:27:54 +0800257 acpi_status status;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100258 union acpi_object *obj;
Rickard Strandqvist8ad3be12014-06-01 14:58:52 +0200259 u32 tmp = 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800260
Pali Rohár0fe57262017-08-12 09:44:16 +0200261 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
Corentin Charyafa7c882011-02-06 13:28:28 +0100262 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800263
264 if (ACPI_FAILURE(status))
Yurii Pavlovskyi1827f3f2019-05-14 21:03:50 +0200265 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +0800266
267 obj = (union acpi_object *)output.pointer;
268 if (obj && obj->type == ACPI_TYPE_INTEGER)
Corentin Charye12e6d92011-02-26 10:20:31 +0100269 tmp = (u32) obj->integer.value;
Yong Wang3d7b1652010-04-11 09:27:54 +0800270
Corentin Chary2a3f0062010-11-29 08:14:10 +0100271 if (retval)
272 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800273
274 kfree(obj);
275
Corentin Charyd33da3b2011-02-26 10:20:35 +0100276 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
277 return -ENODEV;
278
279 return 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800280}
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200281
282int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
283{
284 return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
285}
Daniel Drakeffb6ce72018-10-09 14:40:55 +0800286EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
Yong Wang3d7b1652010-04-11 09:27:54 +0800287
Kast Bernd53e755c2015-05-13 16:24:16 +0200288static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
289{
290 struct acpi_buffer input;
291 u64 phys_addr;
292 u32 retval;
Colin Ian King6568d0c2020-05-07 23:04:55 +0100293 u32 status;
Kast Bernd53e755c2015-05-13 16:24:16 +0200294
295 /*
296 * Copy to dma capable address otherwise memory corruption occurs as
297 * bios has to be able to access it.
298 */
Fuqian Huang340f25f2019-07-04 00:29:51 +0800299 input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
Kast Bernd53e755c2015-05-13 16:24:16 +0200300 input.length = args.length;
301 if (!input.pointer)
302 return -ENOMEM;
303 phys_addr = virt_to_phys(input.pointer);
Kast Bernd53e755c2015-05-13 16:24:16 +0200304
305 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
306 phys_addr, 0, &retval);
307 if (!status)
308 memcpy(args.pointer, input.pointer, args.length);
309
310 kfree(input.pointer);
311 if (status)
312 return -ENXIO;
313
314 return retval;
315}
316
Corentin Chary1d070f82011-02-26 10:20:36 +0100317static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
Corentin Charyd33da3b2011-02-26 10:20:35 +0100318{
Corentin Chary1d070f82011-02-26 10:20:36 +0100319 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100320}
321
322static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
Corentin Chary1d070f82011-02-26 10:20:36 +0100323 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800324{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100325 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
326 ctrl_param, retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800327}
328
Corentin Chary5c956382011-02-06 13:28:31 +0100329/* Helper for special devices with magic return codes */
Corentin Chary1d070f82011-02-26 10:20:36 +0100330static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
331 u32 dev_id, u32 mask)
Corentin Chary5c956382011-02-06 13:28:31 +0100332{
333 u32 retval = 0;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100334 int err;
Corentin Chary5c956382011-02-06 13:28:31 +0100335
Corentin Chary1d070f82011-02-26 10:20:36 +0100336 err = asus_wmi_get_devstate(asus, dev_id, &retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100337 if (err < 0)
338 return err;
Corentin Chary5c956382011-02-06 13:28:31 +0100339
Corentin Charye12e6d92011-02-26 10:20:31 +0100340 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100341 return -ENODEV;
342
Corentin Charya75fe0d2011-02-26 10:20:34 +0100343 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
344 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
345 return -ENODEV;
346 }
347
Corentin Charyb7187262011-02-06 13:28:39 +0100348 return retval & mask;
349}
350
Corentin Chary1d070f82011-02-26 10:20:36 +0100351static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
Corentin Charyb7187262011-02-06 13:28:39 +0100352{
Corentin Chary1d070f82011-02-26 10:20:36 +0100353 return asus_wmi_get_devstate_bits(asus, dev_id,
354 ASUS_WMI_DSTS_STATUS_BIT);
Corentin Chary5c956382011-02-06 13:28:31 +0100355}
356
Daniel Drakef1fc0322019-07-29 16:27:38 +0800357static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
358{
359 u32 retval;
360 int status = asus_wmi_get_devstate(asus, dev_id, &retval);
361
362 return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
363}
364
Hans de Goedefed50032020-05-10 14:24:30 +0200365/* Input **********************************************************************/
366
367static int asus_wmi_input_init(struct asus_wmi *asus)
368{
Hans de Goedeb0dbd972020-05-10 14:24:31 +0200369 int err, result;
Hans de Goedefed50032020-05-10 14:24:30 +0200370
371 asus->inputdev = input_allocate_device();
372 if (!asus->inputdev)
373 return -ENOMEM;
374
375 asus->inputdev->name = asus->driver->input_name;
376 asus->inputdev->phys = asus->driver->input_phys;
377 asus->inputdev->id.bustype = BUS_HOST;
378 asus->inputdev->dev.parent = &asus->platform_device->dev;
379 set_bit(EV_REP, asus->inputdev->evbit);
380
381 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
382 if (err)
383 goto err_free_dev;
384
Hans de Goede1797d582020-09-16 16:14:39 +0200385 if (asus->driver->quirks->use_kbd_dock_devid) {
386 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_KBD_DOCK);
387 if (result >= 0) {
388 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
389 input_report_switch(asus->inputdev, SW_TABLET_MODE, !result);
390 } else if (result != -ENODEV) {
391 pr_err("Error checking for keyboard-dock: %d\n", result);
392 }
Hans de Goedeb0dbd972020-05-10 14:24:31 +0200393 }
394
Samuel Čavojea856ec2020-10-21 00:09:44 +0200395 if (asus->driver->quirks->use_lid_flip_devid) {
396 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_LID_FLIP);
397 if (result < 0)
398 asus->driver->quirks->use_lid_flip_devid = 0;
399 if (result >= 0) {
400 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
401 input_report_switch(asus->inputdev, SW_TABLET_MODE, result);
402 } else if (result == -ENODEV) {
403 pr_err("This device has lid_flip quirk but got ENODEV checking it. This is a bug.");
404 } else {
405 pr_err("Error checking for lid-flip: %d\n", result);
406 }
407 }
408
Hans de Goedefed50032020-05-10 14:24:30 +0200409 err = input_register_device(asus->inputdev);
410 if (err)
411 goto err_free_dev;
412
413 return 0;
414
415err_free_dev:
416 input_free_device(asus->inputdev);
417 return err;
418}
419
420static void asus_wmi_input_exit(struct asus_wmi *asus)
421{
422 if (asus->inputdev)
423 input_unregister_device(asus->inputdev);
424
425 asus->inputdev = NULL;
426}
427
Samuel Čavojea856ec2020-10-21 00:09:44 +0200428/* Tablet mode ****************************************************************/
429
430static void lid_flip_tablet_mode_get_state(struct asus_wmi *asus)
431{
432 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_LID_FLIP);
433
434 if (result >= 0) {
435 input_report_switch(asus->inputdev, SW_TABLET_MODE, result);
436 input_sync(asus->inputdev);
437 }
438}
439
Luke D. Jones98829e82021-08-07 14:36:55 +1200440/* dGPU ********************************************************************/
441static int dgpu_disable_check_present(struct asus_wmi *asus)
442{
443 u32 result;
444 int err;
445
446 asus->dgpu_disable_available = false;
447
448 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_DGPU, &result);
449 if (err) {
450 if (err == -ENODEV)
451 return 0;
452 return err;
453 }
454
455 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
456 asus->dgpu_disable_available = true;
457 asus->dgpu_disable = result & ASUS_WMI_DSTS_STATUS_BIT;
458 }
459
460 return 0;
461}
462
463static int dgpu_disable_write(struct asus_wmi *asus)
464{
465 u32 retval;
466 u8 value;
467 int err;
468
469 /* Don't rely on type conversion */
470 value = asus->dgpu_disable ? 1 : 0;
471
472 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, value, &retval);
473 if (err) {
474 pr_warn("Failed to set dgpu disable: %d\n", err);
475 return err;
476 }
477
Jiapeng Chong828857f2021-08-25 18:37:02 +0800478 if (retval > 1) {
Luke D. Jones98829e82021-08-07 14:36:55 +1200479 pr_warn("Failed to set dgpu disable (retval): 0x%x\n", retval);
480 return -EIO;
481 }
482
483 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
484
485 return 0;
486}
487
488static ssize_t dgpu_disable_show(struct device *dev,
489 struct device_attribute *attr, char *buf)
490{
491 struct asus_wmi *asus = dev_get_drvdata(dev);
492 u8 mode = asus->dgpu_disable;
493
494 return sysfs_emit(buf, "%d\n", mode);
495}
496
497/*
498 * A user may be required to store the value twice, typcial store first, then
499 * rescan PCI bus to activate power, then store a second time to save correctly.
500 * The reason for this is that an extra code path in the ACPI is enabled when
501 * the device and bus are powered.
502 */
503static ssize_t dgpu_disable_store(struct device *dev,
504 struct device_attribute *attr,
505 const char *buf, size_t count)
506{
507 bool disable;
508 int result;
509
510 struct asus_wmi *asus = dev_get_drvdata(dev);
511
512 result = kstrtobool(buf, &disable);
513 if (result)
514 return result;
515
516 asus->dgpu_disable = disable;
517
518 result = dgpu_disable_write(asus);
519 if (result)
520 return result;
521
522 return count;
523}
524
525static DEVICE_ATTR_RW(dgpu_disable);
526
Luke D. Jones382b91d2021-08-07 14:36:56 +1200527/* eGPU ********************************************************************/
528static int egpu_enable_check_present(struct asus_wmi *asus)
529{
530 u32 result;
531 int err;
532
533 asus->egpu_enable_available = false;
534
535 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_EGPU, &result);
536 if (err) {
537 if (err == -ENODEV)
538 return 0;
539 return err;
540 }
541
542 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
543 asus->egpu_enable_available = true;
544 asus->egpu_enable = result & ASUS_WMI_DSTS_STATUS_BIT;
545 }
546
547 return 0;
548}
549
550static int egpu_enable_write(struct asus_wmi *asus)
551{
552 u32 retval;
553 u8 value;
554 int err;
555
556 /* Don't rely on type conversion */
557 value = asus->egpu_enable ? 1 : 0;
558
559 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, value, &retval);
560
561 if (err) {
562 pr_warn("Failed to set egpu disable: %d\n", err);
563 return err;
564 }
565
Jiapeng Chong828857f2021-08-25 18:37:02 +0800566 if (retval > 1) {
Luke D. Jones382b91d2021-08-07 14:36:56 +1200567 pr_warn("Failed to set egpu disable (retval): 0x%x\n", retval);
568 return -EIO;
569 }
570
571 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable");
572
573 return 0;
574}
575
576static ssize_t egpu_enable_show(struct device *dev,
577 struct device_attribute *attr, char *buf)
578{
579 struct asus_wmi *asus = dev_get_drvdata(dev);
580 bool mode = asus->egpu_enable;
581
582 return sysfs_emit(buf, "%d\n", mode);
583}
584
585/* The ACPI call to enable the eGPU also disables the internal dGPU */
586static ssize_t egpu_enable_store(struct device *dev,
587 struct device_attribute *attr,
588 const char *buf, size_t count)
589{
590 bool enable;
591 int result;
592
593 struct asus_wmi *asus = dev_get_drvdata(dev);
594
595 result = kstrtobool(buf, &enable);
596 if (result)
597 return result;
598
599 asus->egpu_enable = enable;
600
601 result = egpu_enable_write(asus);
602 if (result)
603 return result;
604
605 /* Ensure that the kernel status of dgpu is updated */
606 result = dgpu_disable_check_present(asus);
607 if (result)
608 return result;
609
610 return count;
611}
612
613static DEVICE_ATTR_RW(egpu_enable);
614
Kristian Klausen79733532019-09-09 19:31:28 +0200615/* Battery ********************************************************************/
616
617/* The battery maximum charging percentage */
618static int charge_end_threshold;
619
620static ssize_t charge_control_end_threshold_store(struct device *dev,
621 struct device_attribute *attr,
622 const char *buf, size_t count)
623{
624 int value, ret, rv;
625
626 ret = kstrtouint(buf, 10, &value);
627 if (ret)
628 return ret;
629
630 if (value < 0 || value > 100)
631 return -EINVAL;
632
633 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
634 if (ret)
635 return ret;
636
637 if (rv != 1)
638 return -EIO;
639
640 /* There isn't any method in the DSDT to read the threshold, so we
641 * save the threshold.
642 */
643 charge_end_threshold = value;
644 return count;
645}
646
647static ssize_t charge_control_end_threshold_show(struct device *device,
648 struct device_attribute *attr,
649 char *buf)
650{
651 return sprintf(buf, "%d\n", charge_end_threshold);
652}
653
654static DEVICE_ATTR_RW(charge_control_end_threshold);
655
656static int asus_wmi_battery_add(struct power_supply *battery)
657{
658 /* The WMI method does not provide a way to specific a battery, so we
659 * just assume it is the first battery.
Kristian Klausen6b3586d2020-03-03 19:02:15 +0100660 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
661 * battery is named BATT.
Kristian Klausen79733532019-09-09 19:31:28 +0200662 */
Kristian Klausen6b3586d2020-03-03 19:02:15 +0100663 if (strcmp(battery->desc->name, "BAT0") != 0 &&
Vasiliy Kupriakov9a33e372020-06-30 20:56:01 +0300664 strcmp(battery->desc->name, "BAT1") != 0 &&
Marius Iacob1d2dd372020-08-31 15:58:01 +0300665 strcmp(battery->desc->name, "BATC") != 0 &&
Kristian Klausen6b3586d2020-03-03 19:02:15 +0100666 strcmp(battery->desc->name, "BATT") != 0)
Kristian Klausen79733532019-09-09 19:31:28 +0200667 return -ENODEV;
668
669 if (device_create_file(&battery->dev,
670 &dev_attr_charge_control_end_threshold))
671 return -ENODEV;
672
673 /* The charge threshold is only reset when the system is power cycled,
674 * and we can't get the current threshold so let set it to 100% when
675 * a battery is added.
676 */
677 asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
678 charge_end_threshold = 100;
679
680 return 0;
681}
682
683static int asus_wmi_battery_remove(struct power_supply *battery)
684{
685 device_remove_file(&battery->dev,
686 &dev_attr_charge_control_end_threshold);
687 return 0;
688}
689
690static struct acpi_battery_hook battery_hook = {
691 .add_battery = asus_wmi_battery_add,
692 .remove_battery = asus_wmi_battery_remove,
693 .name = "ASUS Battery Extension",
694};
695
696static void asus_wmi_battery_init(struct asus_wmi *asus)
697{
698 asus->battery_rsoc_available = false;
699 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
700 asus->battery_rsoc_available = true;
701 battery_hook_register(&battery_hook);
702 }
703}
704
705static void asus_wmi_battery_exit(struct asus_wmi *asus)
706{
707 if (asus->battery_rsoc_available)
708 battery_hook_unregister(&battery_hook);
709}
710
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +0200711/* LEDs ***********************************************************************/
712
Corentin Chary084fca62010-11-29 08:14:06 +0100713/*
714 * These functions actually update the LED's, and are called from a
715 * workqueue. By doing this as separate work rather than when the LED
Corentin Charye12e6d92011-02-26 10:20:31 +0100716 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
Corentin Chary084fca62010-11-29 08:14:06 +0100717 * potentially bad time, such as a timer interrupt.
718 */
719static void tpd_led_update(struct work_struct *work)
720{
721 int ctrl_param;
Corentin Charye12e6d92011-02-26 10:20:31 +0100722 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100723
Corentin Charye12e6d92011-02-26 10:20:31 +0100724 asus = container_of(work, struct asus_wmi, tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100725
Corentin Charye12e6d92011-02-26 10:20:31 +0100726 ctrl_param = asus->tpd_led_wk;
727 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100728}
729
730static void tpd_led_set(struct led_classdev *led_cdev,
731 enum led_brightness value)
732{
Corentin Charye12e6d92011-02-26 10:20:31 +0100733 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100734
Corentin Charye12e6d92011-02-26 10:20:31 +0100735 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100736
Corentin Charye12e6d92011-02-26 10:20:31 +0100737 asus->tpd_led_wk = !!value;
738 queue_work(asus->led_workqueue, &asus->tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100739}
740
Corentin Charye12e6d92011-02-26 10:20:31 +0100741static int read_tpd_led_state(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100742{
Corentin Chary1d070f82011-02-26 10:20:36 +0100743 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
Corentin Chary084fca62010-11-29 08:14:06 +0100744}
745
746static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
747{
Corentin Charye12e6d92011-02-26 10:20:31 +0100748 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100749
Corentin Charye12e6d92011-02-26 10:20:31 +0100750 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100751
Corentin Charye12e6d92011-02-26 10:20:31 +0100752 return read_tpd_led_state(asus);
Corentin Chary084fca62010-11-29 08:14:06 +0100753}
754
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +0800755static void kbd_led_update(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100756{
Corentin Charye9809c02011-07-01 11:34:31 +0200757 int ctrl_param = 0;
Corentin Chary084fca62010-11-29 08:14:06 +0100758
Jian-Hong Pan26e66a02019-12-30 16:30:45 +0800759 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
Corentin Charye9809c02011-07-01 11:34:31 +0200760 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
761}
Corentin Chary084fca62010-11-29 08:14:06 +0100762
Corentin Charye9809c02011-07-01 11:34:31 +0200763static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
764{
765 int retval;
766
767 /*
768 * bits 0-2: level
769 * bit 7: light on/off
770 * bit 8-10: environment (0: dark, 1: normal, 2: light)
771 * bit 17: status unknown
772 */
773 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
774 0xFFFF);
775
Corentin Charyaf965e92011-07-01 11:34:34 +0200776 /* Unknown status is considered as off */
Corentin Charye9809c02011-07-01 11:34:31 +0200777 if (retval == 0x8000)
Corentin Charyaf965e92011-07-01 11:34:34 +0200778 retval = 0;
Corentin Charye9809c02011-07-01 11:34:31 +0200779
Andy Shevchenko127e1df2019-08-16 14:07:17 +0300780 if (retval < 0)
781 return retval;
Corentin Chary084fca62010-11-29 08:14:06 +0100782
Andy Shevchenko127e1df2019-08-16 14:07:17 +0300783 if (level)
784 *level = retval & 0x7F;
785 if (env)
786 *env = (retval >> 8) & 0x7F;
787 return 0;
Corentin Charye9809c02011-07-01 11:34:31 +0200788}
789
Chris Chiudbb3d782018-06-20 22:46:44 +0800790static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
Corentin Charye9809c02011-07-01 11:34:31 +0200791{
792 struct asus_wmi *asus;
Chris Chiudbb3d782018-06-20 22:46:44 +0800793 int max_level;
Corentin Charye9809c02011-07-01 11:34:31 +0200794
795 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
Chris Chiudbb3d782018-06-20 22:46:44 +0800796 max_level = asus->kbd_led.max_brightness;
Corentin Charye9809c02011-07-01 11:34:31 +0200797
Andy Shevchenko22757522019-08-16 14:10:03 +0300798 asus->kbd_led_wk = clamp_val(value, 0, max_level);
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +0800799 kbd_led_update(asus);
Corentin Charye9809c02011-07-01 11:34:31 +0200800}
801
Chris Chiudbb3d782018-06-20 22:46:44 +0800802static void kbd_led_set(struct led_classdev *led_cdev,
803 enum led_brightness value)
804{
Yurii Pavlovskyi3e581672019-05-14 21:07:46 +0200805 /* Prevent disabling keyboard backlight on module unregister */
806 if (led_cdev->flags & LED_UNREGISTERING)
807 return;
808
Chris Chiudbb3d782018-06-20 22:46:44 +0800809 do_kbd_led_set(led_cdev, value);
810}
811
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +0800812static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
813{
814 struct led_classdev *led_cdev = &asus->kbd_led;
815
816 do_kbd_led_set(led_cdev, value);
817 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
818}
819
Corentin Charye9809c02011-07-01 11:34:31 +0200820static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
821{
822 struct asus_wmi *asus;
823 int retval, value;
824
825 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
826
827 retval = kbd_led_read(asus, &value, NULL);
Corentin Charye9809c02011-07-01 11:34:31 +0200828 if (retval < 0)
829 return retval;
830
831 return value;
Corentin Chary084fca62010-11-29 08:14:06 +0100832}
833
AceLan Kao6cae06e2012-07-27 16:51:59 +0800834static int wlan_led_unknown_state(struct asus_wmi *asus)
835{
836 u32 result;
837
838 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
839
840 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
841}
842
AceLan Kao6cae06e2012-07-27 16:51:59 +0800843static void wlan_led_update(struct work_struct *work)
844{
845 int ctrl_param;
846 struct asus_wmi *asus;
847
848 asus = container_of(work, struct asus_wmi, wlan_led_work);
849
850 ctrl_param = asus->wlan_led_wk;
851 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
852}
853
854static void wlan_led_set(struct led_classdev *led_cdev,
855 enum led_brightness value)
856{
857 struct asus_wmi *asus;
858
859 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
860
861 asus->wlan_led_wk = !!value;
862 queue_work(asus->led_workqueue, &asus->wlan_led_work);
863}
864
865static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
866{
867 struct asus_wmi *asus;
868 u32 result;
869
870 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
871 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
872
873 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
874}
875
Maxime Bellengé4c059842017-09-23 18:23:37 +0200876static void lightbar_led_update(struct work_struct *work)
877{
878 struct asus_wmi *asus;
879 int ctrl_param;
880
881 asus = container_of(work, struct asus_wmi, lightbar_led_work);
882
883 ctrl_param = asus->lightbar_led_wk;
884 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
885}
886
887static void lightbar_led_set(struct led_classdev *led_cdev,
888 enum led_brightness value)
889{
890 struct asus_wmi *asus;
891
892 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
893
894 asus->lightbar_led_wk = !!value;
895 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
896}
897
898static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
899{
900 struct asus_wmi *asus;
901 u32 result;
902
903 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
904 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
905
906 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
907}
908
Corentin Charye12e6d92011-02-26 10:20:31 +0100909static void asus_wmi_led_exit(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100910{
Andy Shevchenko2225dba2020-05-07 19:37:23 +0300911 led_classdev_unregister(&asus->kbd_led);
912 led_classdev_unregister(&asus->tpd_led);
913 led_classdev_unregister(&asus->wlan_led);
914 led_classdev_unregister(&asus->lightbar_led);
915
Corentin Charye12e6d92011-02-26 10:20:31 +0100916 if (asus->led_workqueue)
917 destroy_workqueue(asus->led_workqueue);
Corentin Chary084fca62010-11-29 08:14:06 +0100918}
919
Corentin Charye9809c02011-07-01 11:34:31 +0200920static int asus_wmi_led_init(struct asus_wmi *asus)
921{
Oleksij Rempel30734042015-09-14 11:16:30 +0200922 int rv = 0, led_val;
Corentin Charye9809c02011-07-01 11:34:31 +0200923
924 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
925 if (!asus->led_workqueue)
926 return -ENOMEM;
927
928 if (read_tpd_led_state(asus) >= 0) {
929 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
930
931 asus->tpd_led.name = "asus::touchpad";
932 asus->tpd_led.brightness_set = tpd_led_set;
933 asus->tpd_led.brightness_get = tpd_led_get;
934 asus->tpd_led.max_brightness = 1;
935
936 rv = led_classdev_register(&asus->platform_device->dev,
937 &asus->tpd_led);
938 if (rv)
939 goto error;
940 }
941
Yurii Pavlovskyi8853a2f2019-05-14 20:51:25 +0200942 if (!kbd_led_read(asus, &led_val, NULL)) {
Oleksij Rempel30734042015-09-14 11:16:30 +0200943 asus->kbd_led_wk = led_val;
Corentin Charye9809c02011-07-01 11:34:31 +0200944 asus->kbd_led.name = "asus::kbd_backlight";
Chris Chiudbb3d782018-06-20 22:46:44 +0800945 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
Corentin Charye9809c02011-07-01 11:34:31 +0200946 asus->kbd_led.brightness_set = kbd_led_set;
947 asus->kbd_led.brightness_get = kbd_led_get;
948 asus->kbd_led.max_brightness = 3;
949
950 rv = led_classdev_register(&asus->platform_device->dev,
951 &asus->kbd_led);
AceLan Kao6cae06e2012-07-27 16:51:59 +0800952 if (rv)
953 goto error;
954 }
955
Daniel Drakef1fc0322019-07-29 16:27:38 +0800956 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
957 && (asus->driver->quirks->wapf > 0)) {
AceLan Kao6cae06e2012-07-27 16:51:59 +0800958 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
959
960 asus->wlan_led.name = "asus::wlan";
961 asus->wlan_led.brightness_set = wlan_led_set;
962 if (!wlan_led_unknown_state(asus))
963 asus->wlan_led.brightness_get = wlan_led_get;
964 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
965 asus->wlan_led.max_brightness = 1;
966 asus->wlan_led.default_trigger = "asus-wlan";
967
968 rv = led_classdev_register(&asus->platform_device->dev,
969 &asus->wlan_led);
Maxime Bellengé4c059842017-09-23 18:23:37 +0200970 if (rv)
971 goto error;
972 }
973
Daniel Drakef1fc0322019-07-29 16:27:38 +0800974 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
Maxime Bellengé4c059842017-09-23 18:23:37 +0200975 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
976
977 asus->lightbar_led.name = "asus::lightbar";
978 asus->lightbar_led.brightness_set = lightbar_led_set;
979 asus->lightbar_led.brightness_get = lightbar_led_get;
980 asus->lightbar_led.max_brightness = 1;
981
982 rv = led_classdev_register(&asus->platform_device->dev,
983 &asus->lightbar_led);
Corentin Charye9809c02011-07-01 11:34:31 +0200984 }
985
986error:
987 if (rv)
988 asus_wmi_led_exit(asus);
989
990 return rv;
991}
992
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +0200993/* RF *************************************************************************/
Corentin Charye9809c02011-07-01 11:34:31 +0200994
Corentin Chary084fca62010-11-29 08:14:06 +0100995/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100996 * PCI hotplug (for wlan rfkill)
997 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100998static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100999{
Corentin Chary1d070f82011-02-26 10:20:36 +01001000 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +01001001
Corentin Chary5c956382011-02-06 13:28:31 +01001002 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +01001003 return false;
Corentin Chary5c956382011-02-06 13:28:31 +01001004 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +01001005}
1006
Corentin Charye12e6d92011-02-26 10:20:31 +01001007static void asus_rfkill_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +01001008{
1009 struct pci_dev *dev;
1010 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +01001011 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +01001012 bool absent;
1013 u32 l;
1014
Corentin Charye12e6d92011-02-26 10:20:31 +01001015 mutex_lock(&asus->wmi_lock);
1016 blocked = asus_wlan_rfkill_blocked(asus);
1017 mutex_unlock(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +01001018
Corentin Charye12e6d92011-02-26 10:20:31 +01001019 mutex_lock(&asus->hotplug_lock);
Rafael J. Wysocki8b9ec1d2014-01-10 15:27:08 +01001020 pci_lock_rescan_remove();
Corentin Charyafa7c882011-02-06 13:28:28 +01001021
Corentin Charya7ce3f02011-02-26 10:20:33 +01001022 if (asus->wlan.rfkill)
1023 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
Corentin Chary279f8f92011-02-06 13:28:29 +01001024
Lukas Wunner125450f2018-09-08 09:59:01 +02001025 if (asus->hotplug_slot.ops) {
Corentin Charyafa7c882011-02-06 13:28:28 +01001026 bus = pci_find_bus(0, 1);
1027 if (!bus) {
Joe Perches5ad77dc2011-03-29 15:21:35 -07001028 pr_warn("Unable to find PCI bus 1?\n");
Corentin Charyafa7c882011-02-06 13:28:28 +01001029 goto out_unlock;
1030 }
1031
1032 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
1033 pr_err("Unable to read PCI config space?\n");
1034 goto out_unlock;
1035 }
1036 absent = (l == 0xffffffff);
1037
1038 if (blocked != absent) {
Joe Perches5ad77dc2011-03-29 15:21:35 -07001039 pr_warn("BIOS says wireless lan is %s, "
1040 "but the pci device is %s\n",
1041 blocked ? "blocked" : "unblocked",
1042 absent ? "absent" : "present");
1043 pr_warn("skipped wireless hotplug as probably "
1044 "inappropriate for this model\n");
Corentin Charyafa7c882011-02-06 13:28:28 +01001045 goto out_unlock;
1046 }
1047
1048 if (!blocked) {
1049 dev = pci_get_slot(bus, 0);
1050 if (dev) {
1051 /* Device already present */
1052 pci_dev_put(dev);
1053 goto out_unlock;
1054 }
1055 dev = pci_scan_single_device(bus, 0);
1056 if (dev) {
1057 pci_bus_assign_resources(bus);
Yijing Wangc893d132014-05-30 11:01:03 +08001058 pci_bus_add_device(dev);
Corentin Charyafa7c882011-02-06 13:28:28 +01001059 }
1060 } else {
1061 dev = pci_get_slot(bus, 0);
1062 if (dev) {
Yinghai Lu210647a2012-02-25 13:54:20 -08001063 pci_stop_and_remove_bus_device(dev);
Corentin Charyafa7c882011-02-06 13:28:28 +01001064 pci_dev_put(dev);
1065 }
1066 }
1067 }
1068
1069out_unlock:
Rafael J. Wysocki8b9ec1d2014-01-10 15:27:08 +01001070 pci_unlock_rescan_remove();
Corentin Charye12e6d92011-02-26 10:20:31 +01001071 mutex_unlock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +01001072}
1073
Corentin Charye12e6d92011-02-26 10:20:31 +01001074static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
Corentin Charyafa7c882011-02-06 13:28:28 +01001075{
Corentin Charye12e6d92011-02-26 10:20:31 +01001076 struct asus_wmi *asus = data;
Corentin Charyafa7c882011-02-06 13:28:28 +01001077
1078 if (event != ACPI_NOTIFY_BUS_CHECK)
1079 return;
1080
Corentin Chary279f8f92011-02-06 13:28:29 +01001081 /*
Corentin Charye12e6d92011-02-26 10:20:31 +01001082 * We can't call directly asus_rfkill_hotplug because most
Corentin Chary279f8f92011-02-06 13:28:29 +01001083 * of the time WMBC is still being executed and not reetrant.
1084 * There is currently no way to tell ACPICA that we want this
Corentin Charye12e6d92011-02-26 10:20:31 +01001085 * method to be serialized, we schedule a asus_rfkill_hotplug
Corentin Chary279f8f92011-02-06 13:28:29 +01001086 * call later, in a safer context.
1087 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001088 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +01001089}
1090
Corentin Charye12e6d92011-02-26 10:20:31 +01001091static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +01001092{
1093 acpi_status status;
1094 acpi_handle handle;
1095
1096 status = acpi_get_handle(NULL, node, &handle);
Andy Shevchenko127e1df2019-08-16 14:07:17 +03001097 if (ACPI_FAILURE(status))
Corentin Charyafa7c882011-02-06 13:28:28 +01001098 return -ENODEV;
1099
Andy Shevchenko127e1df2019-08-16 14:07:17 +03001100 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1101 asus_rfkill_notify, asus);
1102 if (ACPI_FAILURE(status))
1103 pr_warn("Failed to register notify on %s\n", node);
1104
Corentin Charyafa7c882011-02-06 13:28:28 +01001105 return 0;
1106}
1107
Corentin Charye12e6d92011-02-26 10:20:31 +01001108static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +01001109{
1110 acpi_status status = AE_OK;
1111 acpi_handle handle;
1112
1113 status = acpi_get_handle(NULL, node, &handle);
Andy Shevchenko127e1df2019-08-16 14:07:17 +03001114 if (ACPI_FAILURE(status))
1115 return;
Corentin Charyafa7c882011-02-06 13:28:28 +01001116
Andy Shevchenko127e1df2019-08-16 14:07:17 +03001117 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1118 asus_rfkill_notify);
1119 if (ACPI_FAILURE(status))
1120 pr_err("Error removing rfkill notify handler %s\n", node);
Corentin Charyafa7c882011-02-06 13:28:28 +01001121}
1122
Corentin Charye12e6d92011-02-26 10:20:31 +01001123static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
1124 u8 *value)
Corentin Charyafa7c882011-02-06 13:28:28 +01001125{
Lukas Wunner125450f2018-09-08 09:59:01 +02001126 struct asus_wmi *asus = container_of(hotplug_slot,
1127 struct asus_wmi, hotplug_slot);
Corentin Chary1d070f82011-02-26 10:20:36 +01001128 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +01001129
Corentin Chary5c956382011-02-06 13:28:31 +01001130 if (result < 0)
1131 return result;
Corentin Charyafa7c882011-02-06 13:28:28 +01001132
Corentin Chary5c956382011-02-06 13:28:31 +01001133 *value = !!result;
Corentin Charyafa7c882011-02-06 13:28:28 +01001134 return 0;
1135}
1136
Lukas Wunner81c4b5b2018-09-08 09:59:01 +02001137static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
Corentin Charye12e6d92011-02-26 10:20:31 +01001138 .get_adapter_status = asus_get_adapter_status,
1139 .get_power_status = asus_get_adapter_status,
Corentin Charyafa7c882011-02-06 13:28:28 +01001140};
1141
Corentin Charye12e6d92011-02-26 10:20:31 +01001142static void asus_hotplug_work(struct work_struct *work)
Corentin Chary279f8f92011-02-06 13:28:29 +01001143{
Corentin Charye12e6d92011-02-26 10:20:31 +01001144 struct asus_wmi *asus;
Corentin Chary279f8f92011-02-06 13:28:29 +01001145
Corentin Charye12e6d92011-02-26 10:20:31 +01001146 asus = container_of(work, struct asus_wmi, hotplug_work);
1147 asus_rfkill_hotplug(asus);
Corentin Chary279f8f92011-02-06 13:28:29 +01001148}
1149
Corentin Charye12e6d92011-02-26 10:20:31 +01001150static int asus_setup_pci_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +01001151{
1152 int ret = -ENOMEM;
1153 struct pci_bus *bus = pci_find_bus(0, 1);
1154
1155 if (!bus) {
1156 pr_err("Unable to find wifi PCI bus\n");
1157 return -ENODEV;
1158 }
1159
Corentin Charye12e6d92011-02-26 10:20:31 +01001160 asus->hotplug_workqueue =
1161 create_singlethread_workqueue("hotplug_workqueue");
1162 if (!asus->hotplug_workqueue)
Corentin Chary279f8f92011-02-06 13:28:29 +01001163 goto error_workqueue;
1164
Corentin Charye12e6d92011-02-26 10:20:31 +01001165 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
Corentin Chary279f8f92011-02-06 13:28:29 +01001166
Lukas Wunner125450f2018-09-08 09:59:01 +02001167 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
Corentin Charyafa7c882011-02-06 13:28:28 +01001168
Lukas Wunner125450f2018-09-08 09:59:01 +02001169 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
Corentin Charyafa7c882011-02-06 13:28:28 +01001170 if (ret) {
1171 pr_err("Unable to register hotplug slot - %d\n", ret);
1172 goto error_register;
1173 }
1174
1175 return 0;
1176
1177error_register:
Lukas Wunner125450f2018-09-08 09:59:01 +02001178 asus->hotplug_slot.ops = NULL;
Corentin Charye12e6d92011-02-26 10:20:31 +01001179 destroy_workqueue(asus->hotplug_workqueue);
Corentin Chary279f8f92011-02-06 13:28:29 +01001180error_workqueue:
Corentin Charyafa7c882011-02-06 13:28:28 +01001181 return ret;
1182}
1183
1184/*
Corentin Charyba48fdb2010-11-29 08:14:07 +01001185 * Rfkill devices
1186 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001187static int asus_rfkill_set(void *data, bool blocked)
Corentin Charyba48fdb2010-11-29 08:14:07 +01001188{
Corentin Charya7ce3f02011-02-26 10:20:33 +01001189 struct asus_rfkill *priv = data;
Corentin Charyba48fdb2010-11-29 08:14:07 +01001190 u32 ctrl_param = !blocked;
AceLan Kaoa50bd122012-07-26 17:13:31 +08001191 u32 dev_id = priv->dev_id;
Corentin Charyba48fdb2010-11-29 08:14:07 +01001192
AceLan Kaoa50bd122012-07-26 17:13:31 +08001193 /*
1194 * If the user bit is set, BIOS can't set and record the wlan status,
1195 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
1196 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
1197 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
1198 * while setting the wlan status through WMI.
1199 * This is also the behavior that windows app will do.
1200 */
1201 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1202 priv->asus->driver->wlan_ctrl_by_user)
1203 dev_id = ASUS_WMI_DEVID_WLAN_LED;
1204
1205 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001206}
1207
Corentin Charye12e6d92011-02-26 10:20:31 +01001208static void asus_rfkill_query(struct rfkill *rfkill, void *data)
Corentin Charyba48fdb2010-11-29 08:14:07 +01001209{
Corentin Charya7ce3f02011-02-26 10:20:33 +01001210 struct asus_rfkill *priv = data;
Corentin Chary5c956382011-02-06 13:28:31 +01001211 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +01001212
Corentin Chary1d070f82011-02-26 10:20:36 +01001213 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001214
Corentin Chary5c956382011-02-06 13:28:31 +01001215 if (result < 0)
Corentin Charye12e6d92011-02-26 10:20:31 +01001216 return;
Corentin Charyba48fdb2010-11-29 08:14:07 +01001217
Corentin Charya7ce3f02011-02-26 10:20:33 +01001218 rfkill_set_sw_state(priv->rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001219}
1220
Corentin Charye12e6d92011-02-26 10:20:31 +01001221static int asus_rfkill_wlan_set(void *data, bool blocked)
Corentin Chary279f8f92011-02-06 13:28:29 +01001222{
Corentin Charya7ce3f02011-02-26 10:20:33 +01001223 struct asus_rfkill *priv = data;
1224 struct asus_wmi *asus = priv->asus;
Corentin Chary279f8f92011-02-06 13:28:29 +01001225 int ret;
1226
1227 /*
1228 * This handler is enabled only if hotplug is enabled.
Corentin Charye12e6d92011-02-26 10:20:31 +01001229 * In this case, the asus_wmi_set_devstate() will
Corentin Chary279f8f92011-02-06 13:28:29 +01001230 * trigger a wmi notification and we need to wait
1231 * this call to finish before being able to call
1232 * any wmi method
1233 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001234 mutex_lock(&asus->wmi_lock);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001235 ret = asus_rfkill_set(data, blocked);
Corentin Charye12e6d92011-02-26 10:20:31 +01001236 mutex_unlock(&asus->wmi_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +01001237 return ret;
1238}
1239
Corentin Charye12e6d92011-02-26 10:20:31 +01001240static const struct rfkill_ops asus_rfkill_wlan_ops = {
1241 .set_block = asus_rfkill_wlan_set,
Corentin Charya7ce3f02011-02-26 10:20:33 +01001242 .query = asus_rfkill_query,
Corentin Chary279f8f92011-02-06 13:28:29 +01001243};
1244
Corentin Charye12e6d92011-02-26 10:20:31 +01001245static const struct rfkill_ops asus_rfkill_ops = {
1246 .set_block = asus_rfkill_set,
1247 .query = asus_rfkill_query,
Corentin Charyba48fdb2010-11-29 08:14:07 +01001248};
1249
Corentin Charye12e6d92011-02-26 10:20:31 +01001250static int asus_new_rfkill(struct asus_wmi *asus,
Corentin Charya7ce3f02011-02-26 10:20:33 +01001251 struct asus_rfkill *arfkill,
Corentin Charye12e6d92011-02-26 10:20:31 +01001252 const char *name, enum rfkill_type type, int dev_id)
Corentin Charyba48fdb2010-11-29 08:14:07 +01001253{
Corentin Chary1d070f82011-02-26 10:20:36 +01001254 int result = asus_wmi_get_devstate_simple(asus, dev_id);
Corentin Charya7ce3f02011-02-26 10:20:33 +01001255 struct rfkill **rfkill = &arfkill->rfkill;
Corentin Charyba48fdb2010-11-29 08:14:07 +01001256
Corentin Chary5c956382011-02-06 13:28:31 +01001257 if (result < 0)
1258 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +01001259
Corentin Charya7ce3f02011-02-26 10:20:33 +01001260 arfkill->dev_id = dev_id;
1261 arfkill->asus = asus;
1262
AceLan Kaoc87992d2012-03-20 09:53:08 +01001263 if (dev_id == ASUS_WMI_DEVID_WLAN &&
1264 asus->driver->quirks->hotplug_wireless)
Corentin Charye12e6d92011-02-26 10:20:31 +01001265 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +01001266 &asus_rfkill_wlan_ops, arfkill);
Corentin Chary279f8f92011-02-06 13:28:29 +01001267 else
Corentin Charye12e6d92011-02-26 10:20:31 +01001268 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +01001269 &asus_rfkill_ops, arfkill);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001270
1271 if (!*rfkill)
1272 return -EINVAL;
1273
AceLan Kaoe8f56c82013-05-30 10:31:50 +08001274 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
AceLan Kaof5156232014-07-09 16:18:18 +08001275 (asus->driver->quirks->wapf > 0))
AceLan Kao6cae06e2012-07-27 16:51:59 +08001276 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1277
Corentin Chary5c956382011-02-06 13:28:31 +01001278 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001279 result = rfkill_register(*rfkill);
1280 if (result) {
1281 rfkill_destroy(*rfkill);
1282 *rfkill = NULL;
1283 return result;
1284 }
1285 return 0;
1286}
1287
Corentin Charye12e6d92011-02-26 10:20:31 +01001288static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +01001289{
João Paulo Rechi Vitacf48bf92018-05-22 14:30:15 -07001290 if (asus->driver->wlan_ctrl_by_user && ashs_present())
1291 return;
1292
Corentin Charye12e6d92011-02-26 10:20:31 +01001293 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1294 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1295 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charya7ce3f02011-02-26 10:20:33 +01001296 if (asus->wlan.rfkill) {
1297 rfkill_unregister(asus->wlan.rfkill);
1298 rfkill_destroy(asus->wlan.rfkill);
1299 asus->wlan.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +01001300 }
Corentin Charyafa7c882011-02-06 13:28:28 +01001301 /*
1302 * Refresh pci hotplug in case the rfkill state was changed after
Corentin Charye12e6d92011-02-26 10:20:31 +01001303 * asus_unregister_rfkill_notifier()
Corentin Charyafa7c882011-02-06 13:28:28 +01001304 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001305 asus_rfkill_hotplug(asus);
Lukas Wunner125450f2018-09-08 09:59:01 +02001306 if (asus->hotplug_slot.ops)
1307 pci_hp_deregister(&asus->hotplug_slot);
Corentin Charye12e6d92011-02-26 10:20:31 +01001308 if (asus->hotplug_workqueue)
1309 destroy_workqueue(asus->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +01001310
Corentin Charya7ce3f02011-02-26 10:20:33 +01001311 if (asus->bluetooth.rfkill) {
1312 rfkill_unregister(asus->bluetooth.rfkill);
1313 rfkill_destroy(asus->bluetooth.rfkill);
1314 asus->bluetooth.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +01001315 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001316 if (asus->wimax.rfkill) {
1317 rfkill_unregister(asus->wimax.rfkill);
1318 rfkill_destroy(asus->wimax.rfkill);
1319 asus->wimax.rfkill = NULL;
Corentin Chary2e9e1592011-02-06 13:28:37 +01001320 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001321 if (asus->wwan3g.rfkill) {
1322 rfkill_unregister(asus->wwan3g.rfkill);
1323 rfkill_destroy(asus->wwan3g.rfkill);
1324 asus->wwan3g.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +01001325 }
Corentin Chary43be8bd2011-07-01 11:34:40 +02001326 if (asus->gps.rfkill) {
1327 rfkill_unregister(asus->gps.rfkill);
1328 rfkill_destroy(asus->gps.rfkill);
1329 asus->gps.rfkill = NULL;
1330 }
Corentin Charya912d322011-07-01 11:34:41 +02001331 if (asus->uwb.rfkill) {
1332 rfkill_unregister(asus->uwb.rfkill);
1333 rfkill_destroy(asus->uwb.rfkill);
1334 asus->uwb.rfkill = NULL;
1335 }
Corentin Charyba48fdb2010-11-29 08:14:07 +01001336}
1337
Corentin Charye12e6d92011-02-26 10:20:31 +01001338static int asus_wmi_rfkill_init(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +01001339{
1340 int result = 0;
1341
Corentin Charye12e6d92011-02-26 10:20:31 +01001342 mutex_init(&asus->hotplug_lock);
1343 mutex_init(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +01001344
Corentin Charya7ce3f02011-02-26 10:20:33 +01001345 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1346 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001347
1348 if (result && result != -ENODEV)
1349 goto exit;
1350
Corentin Charya7ce3f02011-02-26 10:20:33 +01001351 result = asus_new_rfkill(asus, &asus->bluetooth,
Corentin Charye12e6d92011-02-26 10:20:31 +01001352 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1353 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001354
1355 if (result && result != -ENODEV)
1356 goto exit;
1357
Corentin Charya7ce3f02011-02-26 10:20:33 +01001358 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1359 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001360
1361 if (result && result != -ENODEV)
1362 goto exit;
1363
Corentin Charya7ce3f02011-02-26 10:20:33 +01001364 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1365 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001366
1367 if (result && result != -ENODEV)
1368 goto exit;
1369
Corentin Chary43be8bd2011-07-01 11:34:40 +02001370 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1371 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1372
1373 if (result && result != -ENODEV)
1374 goto exit;
1375
Corentin Charya912d322011-07-01 11:34:41 +02001376 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1377 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1378
1379 if (result && result != -ENODEV)
1380 goto exit;
1381
AceLan Kaoc87992d2012-03-20 09:53:08 +01001382 if (!asus->driver->quirks->hotplug_wireless)
Corentin Charyc14d4b82011-02-06 13:28:40 +01001383 goto exit;
1384
Corentin Charye12e6d92011-02-26 10:20:31 +01001385 result = asus_setup_pci_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +01001386 /*
1387 * If we get -EBUSY then something else is handling the PCI hotplug -
1388 * don't fail in this case
1389 */
1390 if (result == -EBUSY)
1391 result = 0;
1392
Corentin Charye12e6d92011-02-26 10:20:31 +01001393 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1394 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1395 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charyafa7c882011-02-06 13:28:28 +01001396 /*
1397 * Refresh pci hotplug in case the rfkill state was changed during
1398 * setup.
1399 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001400 asus_rfkill_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +01001401
Corentin Charyba48fdb2010-11-29 08:14:07 +01001402exit:
1403 if (result && result != -ENODEV)
Corentin Charye12e6d92011-02-26 10:20:31 +01001404 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001405
1406 if (result == -ENODEV)
1407 result = 0;
1408
1409 return result;
1410}
1411
Luke D. Jonesca91ea342021-08-07 14:36:54 +12001412/* Panel Overdrive ************************************************************/
1413static int panel_od_check_present(struct asus_wmi *asus)
1414{
1415 u32 result;
1416 int err;
1417
1418 asus->panel_overdrive_available = false;
1419
1420 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_PANEL_OD, &result);
1421 if (err) {
1422 if (err == -ENODEV)
1423 return 0;
1424 return err;
1425 }
1426
1427 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
1428 asus->panel_overdrive_available = true;
1429 asus->panel_overdrive = result & ASUS_WMI_DSTS_STATUS_BIT;
1430 }
1431
1432 return 0;
1433}
1434
1435static int panel_od_write(struct asus_wmi *asus)
1436{
1437 u32 retval;
1438 u8 value;
1439 int err;
1440
1441 /* Don't rely on type conversion */
1442 value = asus->panel_overdrive ? 1 : 0;
1443
1444 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, value, &retval);
1445
1446 if (err) {
1447 pr_warn("Failed to set panel overdrive: %d\n", err);
1448 return err;
1449 }
1450
Jiapeng Chong828857f2021-08-25 18:37:02 +08001451 if (retval > 1) {
Luke D. Jonesca91ea342021-08-07 14:36:54 +12001452 pr_warn("Failed to set panel overdrive (retval): 0x%x\n", retval);
1453 return -EIO;
1454 }
1455
1456 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
1457
1458 return 0;
1459}
1460
1461static ssize_t panel_od_show(struct device *dev,
1462 struct device_attribute *attr, char *buf)
1463{
1464 struct asus_wmi *asus = dev_get_drvdata(dev);
1465
1466 return sysfs_emit(buf, "%d\n", asus->panel_overdrive);
1467}
1468
1469static ssize_t panel_od_store(struct device *dev,
1470 struct device_attribute *attr,
1471 const char *buf, size_t count)
1472{
1473 bool overdrive;
1474 int result;
1475
1476 struct asus_wmi *asus = dev_get_drvdata(dev);
1477
1478 result = kstrtobool(buf, &overdrive);
1479 if (result)
1480 return result;
1481
1482 asus->panel_overdrive = overdrive;
1483 result = panel_od_write(asus);
1484
1485 if (result)
1486 return result;
1487
1488 return count;
1489}
1490
1491static DEVICE_ATTR_RW(panel_od);
1492
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001493/* Quirks *********************************************************************/
1494
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +08001495static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1496{
1497 struct pci_dev *xhci_pdev;
1498 u32 orig_ports_available;
1499 u32 ports_available = asus->driver->quirks->xusb2pr;
1500
1501 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1502 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1503 NULL);
1504
1505 if (!xhci_pdev)
1506 return;
1507
1508 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1509 &orig_ports_available);
1510
1511 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1512 cpu_to_le32(ports_available));
1513
1514 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1515 orig_ports_available, ports_available);
1516}
1517
Corentin Charyba48fdb2010-11-29 08:14:07 +01001518/*
Oleksij Rempele9b61512017-04-28 16:19:49 +02001519 * Some devices dont support or have borcken get_als method
1520 * but still support set method.
1521 */
1522static void asus_wmi_set_als(void)
1523{
1524 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1525}
1526
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001527/* Hwmon device ***************************************************************/
1528
Daniel Drake2889ffc2019-07-29 16:27:37 +08001529static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
Kast Bernd53e755c2015-05-13 16:24:16 +02001530 int *speed)
1531{
Daniel Drake2889ffc2019-07-29 16:27:37 +08001532 struct agfn_fan_args args = {
Kast Bernd53e755c2015-05-13 16:24:16 +02001533 .agfn.len = sizeof(args),
1534 .agfn.mfun = ASUS_FAN_MFUN,
1535 .agfn.sfun = ASUS_FAN_SFUN_READ,
1536 .fan = fan,
1537 .speed = 0,
1538 };
1539 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1540 int status;
1541
1542 if (fan != 1)
1543 return -EINVAL;
1544
1545 status = asus_wmi_evaluate_method_agfn(input);
1546
1547 if (status || args.agfn.err)
1548 return -ENXIO;
1549
1550 if (speed)
1551 *speed = args.speed;
1552
1553 return 0;
1554}
1555
Daniel Drake2889ffc2019-07-29 16:27:37 +08001556static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
Kast Bernd53e755c2015-05-13 16:24:16 +02001557 int *speed)
1558{
Daniel Drake2889ffc2019-07-29 16:27:37 +08001559 struct agfn_fan_args args = {
Kast Bernd53e755c2015-05-13 16:24:16 +02001560 .agfn.len = sizeof(args),
1561 .agfn.mfun = ASUS_FAN_MFUN,
1562 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1563 .fan = fan,
1564 .speed = speed ? *speed : 0,
1565 };
1566 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1567 int status;
1568
1569 /* 1: for setting 1st fan's speed 0: setting auto mode */
1570 if (fan != 1 && fan != 0)
1571 return -EINVAL;
1572
1573 status = asus_wmi_evaluate_method_agfn(input);
1574
1575 if (status || args.agfn.err)
1576 return -ENXIO;
1577
1578 if (speed && fan == 1)
Daniel Drake2889ffc2019-07-29 16:27:37 +08001579 asus->agfn_pwm = *speed;
Kast Bernd53e755c2015-05-13 16:24:16 +02001580
1581 return 0;
1582}
1583
1584/*
1585 * Check if we can read the speed of one fan. If true we assume we can also
1586 * control it.
1587 */
Daniel Drake2889ffc2019-07-29 16:27:37 +08001588static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
Kast Bernd53e755c2015-05-13 16:24:16 +02001589{
1590 int status;
Daniel Drake2889ffc2019-07-29 16:27:37 +08001591 int speed;
1592 u32 value;
Kast Bernd53e755c2015-05-13 16:24:16 +02001593
Daniel Drake2889ffc2019-07-29 16:27:37 +08001594 status = asus_agfn_fan_speed_read(asus, 1, &speed);
1595 if (status != 0)
1596 return false;
Kast Bernd53e755c2015-05-13 16:24:16 +02001597
Daniel Drake2889ffc2019-07-29 16:27:37 +08001598 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1599 if (status != 0)
1600 return false;
Kast Bernd53e755c2015-05-13 16:24:16 +02001601
Daniel Drake2889ffc2019-07-29 16:27:37 +08001602 /*
1603 * We need to find a better way, probably using sfun,
1604 * bits or spec ...
1605 * Currently we disable it if:
1606 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1607 * - reverved bits are non-zero
1608 * - sfun and presence bit are not set
1609 */
1610 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1611 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
Kast Bernd53e755c2015-05-13 16:24:16 +02001612}
1613
Daniel Drake2889ffc2019-07-29 16:27:37 +08001614static int asus_fan_set_auto(struct asus_wmi *asus)
Kast Bernd53e755c2015-05-13 16:24:16 +02001615{
1616 int status;
Daniel Drakee3168b82019-07-29 16:27:39 +08001617 u32 retval;
Kast Bernd53e755c2015-05-13 16:24:16 +02001618
Daniel Drakee3168b82019-07-29 16:27:39 +08001619 switch (asus->fan_type) {
1620 case FAN_TYPE_SPEC83:
1621 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1622 0, &retval);
1623 if (status)
1624 return status;
1625
1626 if (retval != 1)
1627 return -EIO;
1628 break;
1629
1630 case FAN_TYPE_AGFN:
1631 status = asus_agfn_fan_speed_write(asus, 0, NULL);
1632 if (status)
1633 return -ENXIO;
1634 break;
1635
1636 default:
Kast Bernd53e755c2015-05-13 16:24:16 +02001637 return -ENXIO;
Daniel Drakee3168b82019-07-29 16:27:39 +08001638 }
1639
Kast Bernd53e755c2015-05-13 16:24:16 +02001640
Kast Bernd53e755c2015-05-13 16:24:16 +02001641 return 0;
1642}
1643
Kast Bernd53e755c2015-05-13 16:24:16 +02001644static ssize_t pwm1_show(struct device *dev,
Corentin Chary49979d02011-07-01 11:34:26 +02001645 struct device_attribute *attr,
1646 char *buf)
Corentin Charye07babd2011-02-26 10:20:42 +01001647{
1648 struct asus_wmi *asus = dev_get_drvdata(dev);
Daniel Drake2889ffc2019-07-29 16:27:37 +08001649 int err;
Kast Bernd53e755c2015-05-13 16:24:16 +02001650 int value;
Corentin Charye07babd2011-02-26 10:20:42 +01001651
Daniel Drake2889ffc2019-07-29 16:27:37 +08001652 /* If we already set a value then just return it */
1653 if (asus->agfn_pwm >= 0)
1654 return sprintf(buf, "%d\n", asus->agfn_pwm);
1655
1656 /*
1657 * If we haven't set already set a value through the AGFN interface,
1658 * we read a current value through the (now-deprecated) FAN_CTRL device.
1659 */
1660 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1661 if (err < 0)
1662 return err;
1663
1664 value &= 0xFF;
1665
1666 if (value == 1) /* Low Speed */
1667 value = 85;
1668 else if (value == 2)
1669 value = 170;
1670 else if (value == 3)
1671 value = 255;
1672 else if (value) {
1673 pr_err("Unknown fan speed %#x\n", value);
1674 value = -1;
1675 }
Corentin Charye07babd2011-02-26 10:20:42 +01001676
1677 return sprintf(buf, "%d\n", value);
1678}
1679
Kast Bernd53e755c2015-05-13 16:24:16 +02001680static ssize_t pwm1_store(struct device *dev,
1681 struct device_attribute *attr,
1682 const char *buf, size_t count) {
1683 struct asus_wmi *asus = dev_get_drvdata(dev);
1684 int value;
1685 int state;
1686 int ret;
1687
1688 ret = kstrtouint(buf, 10, &value);
Kast Bernd53e755c2015-05-13 16:24:16 +02001689 if (ret)
1690 return ret;
1691
1692 value = clamp(value, 0, 255);
1693
Daniel Drake2889ffc2019-07-29 16:27:37 +08001694 state = asus_agfn_fan_speed_write(asus, 1, &value);
Kast Bernd53e755c2015-05-13 16:24:16 +02001695 if (state)
1696 pr_warn("Setting fan speed failed: %d\n", state);
1697 else
Daniel Drake2889ffc2019-07-29 16:27:37 +08001698 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
Kast Bernd53e755c2015-05-13 16:24:16 +02001699
1700 return count;
1701}
1702
1703static ssize_t fan1_input_show(struct device *dev,
1704 struct device_attribute *attr,
1705 char *buf)
1706{
Daniel Drake2889ffc2019-07-29 16:27:37 +08001707 struct asus_wmi *asus = dev_get_drvdata(dev);
1708 int value;
1709 int ret;
1710
Daniel Drakee3168b82019-07-29 16:27:39 +08001711 switch (asus->fan_type) {
1712 case FAN_TYPE_SPEC83:
1713 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1714 &value);
1715 if (ret < 0)
1716 return ret;
Daniel Drake2889ffc2019-07-29 16:27:37 +08001717
Daniel Drakee3168b82019-07-29 16:27:39 +08001718 value &= 0xffff;
1719 break;
1720
1721 case FAN_TYPE_AGFN:
1722 /* no speed readable on manual mode */
1723 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1724 return -ENXIO;
1725
1726 ret = asus_agfn_fan_speed_read(asus, 1, &value);
1727 if (ret) {
1728 pr_warn("reading fan speed failed: %d\n", ret);
1729 return -ENXIO;
1730 }
1731 break;
1732
1733 default:
Daniel Drake2889ffc2019-07-29 16:27:37 +08001734 return -ENXIO;
1735 }
Kast Bernd53e755c2015-05-13 16:24:16 +02001736
1737 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
Kast Bernd53e755c2015-05-13 16:24:16 +02001738}
1739
1740static ssize_t pwm1_enable_show(struct device *dev,
1741 struct device_attribute *attr,
1742 char *buf)
1743{
1744 struct asus_wmi *asus = dev_get_drvdata(dev);
1745
Daniel Drakee3168b82019-07-29 16:27:39 +08001746 /*
1747 * Just read back the cached pwm mode.
1748 *
1749 * For the CPU_FAN device, the spec indicates that we should be
1750 * able to read the device status and consult bit 19 to see if we
1751 * are in Full On or Automatic mode. However, this does not work
1752 * in practice on X532FL at least (the bit is always 0) and there's
1753 * also nothing in the DSDT to indicate that this behaviour exists.
1754 */
Daniel Drake2889ffc2019-07-29 16:27:37 +08001755 return sprintf(buf, "%d\n", asus->fan_pwm_mode);
Kast Bernd53e755c2015-05-13 16:24:16 +02001756}
1757
1758static ssize_t pwm1_enable_store(struct device *dev,
1759 struct device_attribute *attr,
1760 const char *buf, size_t count)
1761{
1762 struct asus_wmi *asus = dev_get_drvdata(dev);
1763 int status = 0;
1764 int state;
Daniel Drakee3168b82019-07-29 16:27:39 +08001765 int value;
Kast Bernd53e755c2015-05-13 16:24:16 +02001766 int ret;
Daniel Drakee3168b82019-07-29 16:27:39 +08001767 u32 retval;
Kast Bernd53e755c2015-05-13 16:24:16 +02001768
1769 ret = kstrtouint(buf, 10, &state);
Kast Bernd53e755c2015-05-13 16:24:16 +02001770 if (ret)
1771 return ret;
1772
Daniel Drakee3168b82019-07-29 16:27:39 +08001773 if (asus->fan_type == FAN_TYPE_SPEC83) {
1774 switch (state) { /* standard documented hwmon values */
1775 case ASUS_FAN_CTRL_FULLSPEED:
1776 value = 1;
1777 break;
1778 case ASUS_FAN_CTRL_AUTO:
1779 value = 0;
1780 break;
1781 default:
1782 return -EINVAL;
1783 }
Kast Bernd53e755c2015-05-13 16:24:16 +02001784
Daniel Drakee3168b82019-07-29 16:27:39 +08001785 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1786 value, &retval);
1787 if (ret)
1788 return ret;
Kast Bernd53e755c2015-05-13 16:24:16 +02001789
Daniel Drakee3168b82019-07-29 16:27:39 +08001790 if (retval != 1)
1791 return -EIO;
1792 } else if (asus->fan_type == FAN_TYPE_AGFN) {
1793 switch (state) {
1794 case ASUS_FAN_CTRL_MANUAL:
1795 break;
1796
1797 case ASUS_FAN_CTRL_AUTO:
1798 status = asus_fan_set_auto(asus);
1799 if (status)
1800 return status;
1801 break;
1802
1803 default:
1804 return -EINVAL;
1805 }
Daniel Drake2889ffc2019-07-29 16:27:37 +08001806 }
1807
1808 asus->fan_pwm_mode = state;
Kast Bernd53e755c2015-05-13 16:24:16 +02001809 return count;
1810}
1811
1812static ssize_t fan1_label_show(struct device *dev,
1813 struct device_attribute *attr,
1814 char *buf)
1815{
1816 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1817}
1818
Corentin Chary6118b8a2011-07-01 11:34:36 +02001819static ssize_t asus_hwmon_temp1(struct device *dev,
1820 struct device_attribute *attr,
1821 char *buf)
1822{
1823 struct asus_wmi *asus = dev_get_drvdata(dev);
1824 u32 value;
1825 int err;
1826
1827 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
Corentin Chary6118b8a2011-07-01 11:34:36 +02001828 if (err < 0)
1829 return err;
1830
Akinobu Mitaf07b9fd2020-01-30 22:15:37 -08001831 return sprintf(buf, "%ld\n",
1832 deci_kelvin_to_millicelsius(value & 0xFFFF));
Corentin Chary6118b8a2011-07-01 11:34:36 +02001833}
1834
Kast Bernd53e755c2015-05-13 16:24:16 +02001835/* Fan1 */
1836static DEVICE_ATTR_RW(pwm1);
1837static DEVICE_ATTR_RW(pwm1_enable);
1838static DEVICE_ATTR_RO(fan1_input);
1839static DEVICE_ATTR_RO(fan1_label);
1840
1841/* Temperature */
Guenter Roeck50a639f2013-11-23 11:03:17 -08001842static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
Corentin Charye07babd2011-02-26 10:20:42 +01001843
1844static struct attribute *hwmon_attributes[] = {
Guenter Roeck50a639f2013-11-23 11:03:17 -08001845 &dev_attr_pwm1.attr,
Kast Bernd53e755c2015-05-13 16:24:16 +02001846 &dev_attr_pwm1_enable.attr,
1847 &dev_attr_fan1_input.attr,
1848 &dev_attr_fan1_label.attr,
1849
Guenter Roeck50a639f2013-11-23 11:03:17 -08001850 &dev_attr_temp1_input.attr,
Corentin Charye07babd2011-02-26 10:20:42 +01001851 NULL
1852};
1853
Al Viro587a1f12011-07-23 23:11:19 -04001854static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
Corentin Charye02431d2011-07-01 11:34:37 +02001855 struct attribute *attr, int idx)
Corentin Charye07babd2011-02-26 10:20:42 +01001856{
1857 struct device *dev = container_of(kobj, struct device, kobj);
Fuqian Huang8e8fe442019-07-01 11:24:26 +08001858 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
Corentin Charye07babd2011-02-26 10:20:42 +01001859 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
Kast Bernd53e755c2015-05-13 16:24:16 +02001860
Daniel Drakee3168b82019-07-29 16:27:39 +08001861 if (attr == &dev_attr_pwm1.attr) {
1862 if (asus->fan_type != FAN_TYPE_AGFN)
1863 return 0;
1864 } else if (attr == &dev_attr_fan1_input.attr
Kast Bernd53e755c2015-05-13 16:24:16 +02001865 || attr == &dev_attr_fan1_label.attr
Kast Bernd53e755c2015-05-13 16:24:16 +02001866 || attr == &dev_attr_pwm1_enable.attr) {
Daniel Drake2889ffc2019-07-29 16:27:37 +08001867 if (asus->fan_type == FAN_TYPE_NONE)
1868 return 0;
1869 } else if (attr == &dev_attr_temp1_input.attr) {
1870 int err = asus_wmi_get_devstate(asus,
1871 ASUS_WMI_DEVID_THERMAL_CTRL,
1872 &value);
Kast Bernd53e755c2015-05-13 16:24:16 +02001873
Daniel Drake2889ffc2019-07-29 16:27:37 +08001874 if (err < 0)
Al Viroe772aed2011-07-23 20:59:40 -04001875 return 0; /* can't return negative here */
Corentin Charye07babd2011-02-26 10:20:42 +01001876
Yurii Pavlovskyi4fd19822019-05-14 21:05:46 +02001877 /*
1878 * If the temperature value in deci-Kelvin is near the absolute
1879 * zero temperature, something is clearly wrong
1880 */
1881 if (value == 0 || value == 1)
Daniel Drake2889ffc2019-07-29 16:27:37 +08001882 return 0;
Corentin Charye07babd2011-02-26 10:20:42 +01001883 }
1884
Daniel Drake2889ffc2019-07-29 16:27:37 +08001885 return attr->mode;
Corentin Charye07babd2011-02-26 10:20:42 +01001886}
1887
Arvind Yadave90d9ba2017-07-11 16:18:19 +05301888static const struct attribute_group hwmon_attribute_group = {
Corentin Charye07babd2011-02-26 10:20:42 +01001889 .is_visible = asus_hwmon_sysfs_is_visible,
1890 .attrs = hwmon_attributes
1891};
Guenter Roeck50a639f2013-11-23 11:03:17 -08001892__ATTRIBUTE_GROUPS(hwmon_attribute);
Corentin Charye07babd2011-02-26 10:20:42 +01001893
1894static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1895{
Yurii Pavlovskyicd10ee02019-05-14 20:50:30 +02001896 struct device *dev = &asus->platform_device->dev;
Corentin Charye07babd2011-02-26 10:20:42 +01001897 struct device *hwmon;
Corentin Charye07babd2011-02-26 10:20:42 +01001898
Yurii Pavlovskyicd10ee02019-05-14 20:50:30 +02001899 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
1900 hwmon_attribute_groups);
1901
Corentin Charye07babd2011-02-26 10:20:42 +01001902 if (IS_ERR(hwmon)) {
1903 pr_err("Could not register asus hwmon device\n");
1904 return PTR_ERR(hwmon);
1905 }
Guenter Roeck50a639f2013-11-23 11:03:17 -08001906 return 0;
Corentin Charye07babd2011-02-26 10:20:42 +01001907}
1908
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001909static int asus_wmi_fan_init(struct asus_wmi *asus)
1910{
Daniel Drake2889ffc2019-07-29 16:27:37 +08001911 asus->fan_type = FAN_TYPE_NONE;
1912 asus->agfn_pwm = -1;
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001913
Daniel Drakee3168b82019-07-29 16:27:39 +08001914 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
1915 asus->fan_type = FAN_TYPE_SPEC83;
1916 else if (asus_wmi_has_agfn_fan(asus))
Daniel Drake2889ffc2019-07-29 16:27:37 +08001917 asus->fan_type = FAN_TYPE_AGFN;
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001918
Daniel Drakee3168b82019-07-29 16:27:39 +08001919 if (asus->fan_type == FAN_TYPE_NONE)
1920 return -ENODEV;
1921
1922 asus_fan_set_auto(asus);
1923 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
1924 return 0;
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001925}
1926
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001927/* Fan mode *******************************************************************/
1928
Daniel Drake9af93db2019-07-17 13:10:58 +08001929static int fan_boost_mode_check_present(struct asus_wmi *asus)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001930{
1931 u32 result;
1932 int err;
1933
Daniel Drake9af93db2019-07-17 13:10:58 +08001934 asus->fan_boost_mode_available = false;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001935
Daniel Drake9af93db2019-07-17 13:10:58 +08001936 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
1937 &result);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001938 if (err) {
1939 if (err == -ENODEV)
1940 return 0;
1941 else
1942 return err;
1943 }
1944
1945 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
Daniel Drake9af93db2019-07-17 13:10:58 +08001946 (result & ASUS_FAN_BOOST_MODES_MASK)) {
1947 asus->fan_boost_mode_available = true;
1948 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001949 }
1950
1951 return 0;
1952}
1953
Daniel Drake9af93db2019-07-17 13:10:58 +08001954static int fan_boost_mode_write(struct asus_wmi *asus)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001955{
1956 int err;
1957 u8 value;
1958 u32 retval;
1959
Daniel Drake9af93db2019-07-17 13:10:58 +08001960 value = asus->fan_boost_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001961
Daniel Drake9af93db2019-07-17 13:10:58 +08001962 pr_info("Set fan boost mode: %u\n", value);
1963 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
1964 &retval);
Vasiliy Kupriakov72ceec52020-08-29 00:49:30 +03001965
1966 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
1967 "fan_boost_mode");
1968
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001969 if (err) {
Daniel Drake9af93db2019-07-17 13:10:58 +08001970 pr_warn("Failed to set fan boost mode: %d\n", err);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001971 return err;
1972 }
1973
1974 if (retval != 1) {
Daniel Drake9af93db2019-07-17 13:10:58 +08001975 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
1976 retval);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001977 return -EIO;
1978 }
1979
1980 return 0;
1981}
1982
Daniel Drake9af93db2019-07-17 13:10:58 +08001983static int fan_boost_mode_switch_next(struct asus_wmi *asus)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001984{
Daniel Drake9af93db2019-07-17 13:10:58 +08001985 u8 mask = asus->fan_boost_mode_mask;
1986
1987 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
1988 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
1989 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
1990 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1991 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1992 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1993 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1994 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001995 else
Daniel Drake9af93db2019-07-17 13:10:58 +08001996 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001997 } else {
Daniel Drake9af93db2019-07-17 13:10:58 +08001998 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001999 }
2000
Daniel Drake9af93db2019-07-17 13:10:58 +08002001 return fan_boost_mode_write(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002002}
2003
Daniel Drake9af93db2019-07-17 13:10:58 +08002004static ssize_t fan_boost_mode_show(struct device *dev,
2005 struct device_attribute *attr, char *buf)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002006{
2007 struct asus_wmi *asus = dev_get_drvdata(dev);
2008
Daniel Drake9af93db2019-07-17 13:10:58 +08002009 return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002010}
2011
Daniel Drake9af93db2019-07-17 13:10:58 +08002012static ssize_t fan_boost_mode_store(struct device *dev,
2013 struct device_attribute *attr,
2014 const char *buf, size_t count)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002015{
2016 int result;
2017 u8 new_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002018 struct asus_wmi *asus = dev_get_drvdata(dev);
Daniel Drake9af93db2019-07-17 13:10:58 +08002019 u8 mask = asus->fan_boost_mode_mask;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002020
2021 result = kstrtou8(buf, 10, &new_mode);
2022 if (result < 0) {
2023 pr_warn("Trying to store invalid value\n");
2024 return result;
2025 }
2026
Daniel Drake9af93db2019-07-17 13:10:58 +08002027 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2028 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002029 return -EINVAL;
Daniel Drake9af93db2019-07-17 13:10:58 +08002030 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
2031 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002032 return -EINVAL;
Daniel Drake9af93db2019-07-17 13:10:58 +08002033 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002034 return -EINVAL;
2035 }
2036
Daniel Drake9af93db2019-07-17 13:10:58 +08002037 asus->fan_boost_mode = new_mode;
2038 fan_boost_mode_write(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002039
Leonid Maksymchukedeee342020-03-04 13:38:11 +02002040 return count;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002041}
2042
Daniel Drake9af93db2019-07-17 13:10:58 +08002043// Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
2044static DEVICE_ATTR_RW(fan_boost_mode);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002045
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02002046/* Throttle thermal policy ****************************************************/
2047
2048static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
2049{
2050 u32 result;
2051 int err;
2052
2053 asus->throttle_thermal_policy_available = false;
2054
2055 err = asus_wmi_get_devstate(asus,
2056 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2057 &result);
2058 if (err) {
2059 if (err == -ENODEV)
2060 return 0;
2061 return err;
2062 }
2063
2064 if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
2065 asus->throttle_thermal_policy_available = true;
2066
2067 return 0;
2068}
2069
2070static int throttle_thermal_policy_write(struct asus_wmi *asus)
2071{
2072 int err;
2073 u8 value;
2074 u32 retval;
2075
2076 value = asus->throttle_thermal_policy_mode;
2077
2078 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2079 value, &retval);
Vasiliy Kupriakov72ceec52020-08-29 00:49:30 +03002080
2081 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2082 "throttle_thermal_policy");
2083
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02002084 if (err) {
2085 pr_warn("Failed to set throttle thermal policy: %d\n", err);
2086 return err;
2087 }
2088
2089 if (retval != 1) {
2090 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
2091 retval);
2092 return -EIO;
2093 }
2094
2095 return 0;
2096}
2097
Leonid Maksymchuka2821582019-12-15 16:27:24 +02002098static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
2099{
2100 if (!asus->throttle_thermal_policy_available)
2101 return 0;
2102
2103 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2104 return throttle_thermal_policy_write(asus);
2105}
2106
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02002107static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
2108{
2109 u8 new_mode = asus->throttle_thermal_policy_mode + 1;
Luke D. Jonesc63d44a2021-08-19 07:07:31 +12002110 int err;
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02002111
2112 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2113 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2114
2115 asus->throttle_thermal_policy_mode = new_mode;
Luke D. Jonesc63d44a2021-08-19 07:07:31 +12002116 err = throttle_thermal_policy_write(asus);
2117 if (err)
2118 return err;
2119
2120 /*
2121 * Ensure that platform_profile updates userspace with the change to ensure
2122 * that platform_profile and throttle_thermal_policy_mode are in sync.
2123 */
2124 platform_profile_notify();
2125
2126 return 0;
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02002127}
2128
2129static ssize_t throttle_thermal_policy_show(struct device *dev,
2130 struct device_attribute *attr, char *buf)
2131{
2132 struct asus_wmi *asus = dev_get_drvdata(dev);
2133 u8 mode = asus->throttle_thermal_policy_mode;
2134
2135 return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
2136}
2137
2138static ssize_t throttle_thermal_policy_store(struct device *dev,
2139 struct device_attribute *attr,
2140 const char *buf, size_t count)
2141{
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02002142 struct asus_wmi *asus = dev_get_drvdata(dev);
Luke D. Jonesc63d44a2021-08-19 07:07:31 +12002143 u8 new_mode;
2144 int result;
2145 int err;
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02002146
2147 result = kstrtou8(buf, 10, &new_mode);
2148 if (result < 0)
2149 return result;
2150
2151 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2152 return -EINVAL;
2153
2154 asus->throttle_thermal_policy_mode = new_mode;
Luke D. Jonesc63d44a2021-08-19 07:07:31 +12002155 err = throttle_thermal_policy_write(asus);
2156 if (err)
2157 return err;
2158
2159 /*
2160 * Ensure that platform_profile updates userspace with the change to ensure
2161 * that platform_profile and throttle_thermal_policy_mode are in sync.
2162 */
2163 platform_profile_notify();
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02002164
2165 return count;
2166}
2167
2168// Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
2169static DEVICE_ATTR_RW(throttle_thermal_policy);
2170
Luke D. Jonesc63d44a2021-08-19 07:07:31 +12002171/* Platform profile ***********************************************************/
2172static int platform_profile_get(struct platform_profile_handler *pprof,
2173 enum platform_profile_option *profile)
2174{
2175 struct asus_wmi *asus;
2176 int tp;
2177
2178 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2179
2180 tp = asus->throttle_thermal_policy_mode;
2181
Luke D. Jonesc63d44a2021-08-19 07:07:31 +12002182 switch (tp) {
2183 case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
2184 *profile = PLATFORM_PROFILE_BALANCED;
2185 break;
2186 case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
2187 *profile = PLATFORM_PROFILE_PERFORMANCE;
2188 break;
2189 case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
2190 *profile = PLATFORM_PROFILE_QUIET;
2191 break;
2192 default:
2193 return -EINVAL;
2194 }
2195
2196 return 0;
2197}
2198
2199static int platform_profile_set(struct platform_profile_handler *pprof,
2200 enum platform_profile_option profile)
2201{
2202 struct asus_wmi *asus;
2203 int tp;
2204
2205 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2206
2207 switch (profile) {
2208 case PLATFORM_PROFILE_PERFORMANCE:
2209 tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
2210 break;
2211 case PLATFORM_PROFILE_BALANCED:
2212 tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2213 break;
2214 case PLATFORM_PROFILE_QUIET:
2215 tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
2216 break;
2217 default:
2218 return -EOPNOTSUPP;
2219 }
2220
2221 asus->throttle_thermal_policy_mode = tp;
2222 return throttle_thermal_policy_write(asus);
2223}
2224
2225static int platform_profile_setup(struct asus_wmi *asus)
2226{
2227 struct device *dev = &asus->platform_device->dev;
2228 int err;
2229
2230 /*
2231 * Not an error if a component platform_profile relies on is unavailable
2232 * so early return, skipping the setup of platform_profile.
2233 */
2234 if (!asus->throttle_thermal_policy_available)
2235 return 0;
2236
2237 dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
2238
2239 asus->platform_profile_handler.profile_get = platform_profile_get;
2240 asus->platform_profile_handler.profile_set = platform_profile_set;
2241
2242 set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
2243 set_bit(PLATFORM_PROFILE_BALANCED,
2244 asus->platform_profile_handler.choices);
2245 set_bit(PLATFORM_PROFILE_PERFORMANCE,
2246 asus->platform_profile_handler.choices);
2247
2248 err = platform_profile_register(&asus->platform_profile_handler);
2249 if (err)
2250 return err;
2251
2252 asus->platform_profile_support = true;
2253 return 0;
2254}
2255
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002256/* Backlight ******************************************************************/
2257
Corentin Chary1d070f82011-02-26 10:20:36 +01002258static int read_backlight_power(struct asus_wmi *asus)
Corentin Charyb7187262011-02-06 13:28:39 +01002259{
AceLan Kao6e0044b2012-03-20 09:53:09 +01002260 int ret;
Andy Shevchenko109e8ad2019-08-16 14:17:34 +03002261
AceLan Kao6e0044b2012-03-20 09:53:09 +01002262 if (asus->driver->quirks->store_backlight_power)
2263 ret = !asus->driver->panel_power;
2264 else
2265 ret = asus_wmi_get_devstate_simple(asus,
2266 ASUS_WMI_DEVID_BACKLIGHT);
Corentin Charyb7187262011-02-06 13:28:39 +01002267
2268 if (ret < 0)
2269 return ret;
2270
2271 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
2272}
2273
Corentin Chary8fbea012011-02-26 10:20:37 +01002274static int read_brightness_max(struct asus_wmi *asus)
2275{
2276 u32 retval;
2277 int err;
2278
2279 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
Corentin Chary8fbea012011-02-26 10:20:37 +01002280 if (err < 0)
2281 return err;
2282
2283 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
2284 retval >>= 8;
2285
2286 if (!retval)
2287 return -ENODEV;
2288
2289 return retval;
2290}
2291
Yong Wang3d7b1652010-04-11 09:27:54 +08002292static int read_brightness(struct backlight_device *bd)
2293{
Corentin Chary1d070f82011-02-26 10:20:36 +01002294 struct asus_wmi *asus = bl_get_data(bd);
Dan Carpenter0986f252011-03-15 10:06:23 +03002295 u32 retval;
2296 int err;
Yong Wang3d7b1652010-04-11 09:27:54 +08002297
Corentin Chary1d070f82011-02-26 10:20:36 +01002298 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +01002299 if (err < 0)
2300 return err;
2301
2302 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
Yong Wang3d7b1652010-04-11 09:27:54 +08002303}
2304
AceLan Kaoc87992d2012-03-20 09:53:08 +01002305static u32 get_scalar_command(struct backlight_device *bd)
2306{
2307 struct asus_wmi *asus = bl_get_data(bd);
2308 u32 ctrl_param = 0;
2309
2310 if ((asus->driver->brightness < bd->props.brightness) ||
2311 bd->props.brightness == bd->props.max_brightness)
2312 ctrl_param = 0x00008001;
2313 else if ((asus->driver->brightness > bd->props.brightness) ||
2314 bd->props.brightness == 0)
2315 ctrl_param = 0x00008000;
2316
2317 asus->driver->brightness = bd->props.brightness;
2318
2319 return ctrl_param;
2320}
2321
Yong Wang3d7b1652010-04-11 09:27:54 +08002322static int update_bl_status(struct backlight_device *bd)
2323{
Corentin Chary1d070f82011-02-26 10:20:36 +01002324 struct asus_wmi *asus = bl_get_data(bd);
Corentin Charydfed65d2010-11-29 08:14:12 +01002325 u32 ctrl_param;
AceLan Kao6e0044b2012-03-20 09:53:09 +01002326 int power, err = 0;
Corentin Charyb7187262011-02-06 13:28:39 +01002327
Corentin Chary1d070f82011-02-26 10:20:36 +01002328 power = read_backlight_power(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01002329 if (power != -ENODEV && bd->props.power != power) {
2330 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
Corentin Charyd33da3b2011-02-26 10:20:35 +01002331 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
2332 ctrl_param, NULL);
AceLan Kao6e0044b2012-03-20 09:53:09 +01002333 if (asus->driver->quirks->store_backlight_power)
2334 asus->driver->panel_power = bd->props.power;
AceLan Kao6e0044b2012-03-20 09:53:09 +01002335
Corentin Charyade28ab2012-03-20 09:53:14 +01002336 /* When using scalar brightness, updating the brightness
2337 * will mess with the backlight power */
2338 if (asus->driver->quirks->scalar_panel_brightness)
2339 return err;
Corentin Charyb7187262011-02-06 13:28:39 +01002340 }
Corentin Charyade28ab2012-03-20 09:53:14 +01002341
2342 if (asus->driver->quirks->scalar_panel_brightness)
2343 ctrl_param = get_scalar_command(bd);
2344 else
2345 ctrl_param = bd->props.brightness;
2346
2347 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
2348 ctrl_param, NULL);
2349
Corentin Chary8fbea012011-02-26 10:20:37 +01002350 return err;
Yong Wang3d7b1652010-04-11 09:27:54 +08002351}
2352
Corentin Charye12e6d92011-02-26 10:20:31 +01002353static const struct backlight_ops asus_wmi_bl_ops = {
Yong Wang3d7b1652010-04-11 09:27:54 +08002354 .get_brightness = read_brightness,
2355 .update_status = update_bl_status,
2356};
2357
Corentin Charye12e6d92011-02-26 10:20:31 +01002358static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
Yong Wang3d7b1652010-04-11 09:27:54 +08002359{
Corentin Charye12e6d92011-02-26 10:20:31 +01002360 struct backlight_device *bd = asus->backlight_device;
Yong Wang3d7b1652010-04-11 09:27:54 +08002361 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +02002362 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +08002363
2364 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2365 new = code - NOTIFY_BRNUP_MIN + 1;
2366 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2367 new = code - NOTIFY_BRNDOWN_MIN;
2368
2369 bd->props.brightness = new;
2370 backlight_update_status(bd);
2371 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
2372
2373 return old;
2374}
2375
Corentin Charye12e6d92011-02-26 10:20:31 +01002376static int asus_wmi_backlight_init(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08002377{
2378 struct backlight_device *bd;
2379 struct backlight_properties props;
Corentin Charyb7187262011-02-06 13:28:39 +01002380 int max;
2381 int power;
2382
Corentin Chary8fbea012011-02-26 10:20:37 +01002383 max = read_brightness_max(asus);
Hans de Goede86ac27352014-07-08 10:47:22 +02002384 if (max < 0)
Corentin Chary8fbea012011-02-26 10:20:37 +01002385 return max;
2386
2387 power = read_backlight_power(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01002388 if (power == -ENODEV)
2389 power = FB_BLANK_UNBLANK;
Corentin Chary8fbea012011-02-26 10:20:37 +01002390 else if (power < 0)
2391 return power;
Yong Wang3d7b1652010-04-11 09:27:54 +08002392
2393 memset(&props, 0, sizeof(struct backlight_properties));
Axel Lin60cfa092011-06-29 11:43:30 +08002394 props.type = BACKLIGHT_PLATFORM;
Corentin Charyb7187262011-02-06 13:28:39 +01002395 props.max_brightness = max;
Corentin Charye12e6d92011-02-26 10:20:31 +01002396 bd = backlight_device_register(asus->driver->name,
2397 &asus->platform_device->dev, asus,
2398 &asus_wmi_bl_ops, &props);
Yong Wang3d7b1652010-04-11 09:27:54 +08002399 if (IS_ERR(bd)) {
2400 pr_err("Could not register backlight device\n");
2401 return PTR_ERR(bd);
2402 }
2403
Corentin Charye12e6d92011-02-26 10:20:31 +01002404 asus->backlight_device = bd;
Yong Wang3d7b1652010-04-11 09:27:54 +08002405
AceLan Kao6e0044b2012-03-20 09:53:09 +01002406 if (asus->driver->quirks->store_backlight_power)
2407 asus->driver->panel_power = power;
2408
Yong Wang3d7b1652010-04-11 09:27:54 +08002409 bd->props.brightness = read_brightness(bd);
Corentin Charyb7187262011-02-06 13:28:39 +01002410 bd->props.power = power;
Yong Wang3d7b1652010-04-11 09:27:54 +08002411 backlight_update_status(bd);
2412
AceLan Kaoc87992d2012-03-20 09:53:08 +01002413 asus->driver->brightness = bd->props.brightness;
2414
Yong Wang3d7b1652010-04-11 09:27:54 +08002415 return 0;
2416}
2417
Corentin Charye12e6d92011-02-26 10:20:31 +01002418static void asus_wmi_backlight_exit(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08002419{
Markus Elfring00981812014-11-24 20:30:29 +01002420 backlight_device_unregister(asus->backlight_device);
Yong Wang3d7b1652010-04-11 09:27:54 +08002421
Corentin Charye12e6d92011-02-26 10:20:31 +01002422 asus->backlight_device = NULL;
Yong Wang3d7b1652010-04-11 09:27:54 +08002423}
2424
AceLan Kaoa2a96f02012-10-03 11:26:31 +02002425static int is_display_toggle(int code)
2426{
2427 /* display toggle keys */
2428 if ((code >= 0x61 && code <= 0x67) ||
2429 (code >= 0x8c && code <= 0x93) ||
2430 (code >= 0xa0 && code <= 0xa7) ||
2431 (code >= 0xd0 && code <= 0xd5))
2432 return 1;
2433
2434 return 0;
2435}
2436
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002437/* Fn-lock ********************************************************************/
2438
Chris Chiu487579b2019-04-18 14:46:48 +08002439static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
2440{
2441 u32 result;
2442
2443 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
2444
2445 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2446 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
2447}
2448
2449static void asus_wmi_fnlock_update(struct asus_wmi *asus)
2450{
2451 int mode = asus->fnlock_locked;
2452
2453 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
2454}
2455
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002456/* WMI events *****************************************************************/
2457
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002458static int asus_wmi_get_event_code(u32 value)
Yong Wang3d7b1652010-04-11 09:27:54 +08002459{
Yong Wang3d7b1652010-04-11 09:27:54 +08002460 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
2461 union acpi_object *obj;
2462 acpi_status status;
2463 int code;
Yong Wang3d7b1652010-04-11 09:27:54 +08002464
2465 status = wmi_get_event_data(value, &response);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002466 if (ACPI_FAILURE(status)) {
2467 pr_warn("Failed to get WMI notify code: %s\n",
2468 acpi_format_exception(status));
2469 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +08002470 }
2471
2472 obj = (union acpi_object *)response.pointer;
2473
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002474 if (obj && obj->type == ACPI_TYPE_INTEGER)
2475 code = (int)(obj->integer.value & WMI_EVENT_MASK);
2476 else
2477 code = -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +08002478
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002479 kfree(obj);
2480 return code;
2481}
2482
2483static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
2484{
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002485 unsigned int key_value = 1;
2486 bool autorelease = 1;
Hans de Goedeb0dbd972020-05-10 14:24:31 +02002487 int result, orig_code;
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002488
Corentin Chary57ab7da2011-02-26 10:20:32 +01002489 orig_code = code;
Yong Wang3d7b1652010-04-11 09:27:54 +08002490
Seth Forsheec4453f62011-07-01 11:34:27 +02002491 if (asus->driver->key_filter) {
2492 asus->driver->key_filter(asus->driver, &code, &key_value,
2493 &autorelease);
2494 if (code == ASUS_WMI_KEY_IGNORE)
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002495 return;
Seth Forsheec4453f62011-07-01 11:34:27 +02002496 }
2497
Corentin Chary57ab7da2011-02-26 10:20:32 +01002498 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
Corentin Chary3ba03022012-11-29 09:12:38 +01002499 code = ASUS_WMI_BRN_UP;
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002500 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
Corentin Chary3ba03022012-11-29 09:12:38 +01002501 code = ASUS_WMI_BRN_DOWN;
Yong Wang3d7b1652010-04-11 09:27:54 +08002502
Corentin Chary3ba03022012-11-29 09:12:38 +01002503 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
Hans de Goede62c4aa12015-06-16 16:27:58 +02002504 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Corentin Chary57ab7da2011-02-26 10:20:32 +01002505 asus_wmi_backlight_notify(asus, orig_code);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002506 return;
AceLan Kaoa2a96f02012-10-03 11:26:31 +02002507 }
AceLan Kaoa2a96f02012-10-03 11:26:31 +02002508 }
2509
Chris Chiudbb3d782018-06-20 22:46:44 +08002510 if (code == NOTIFY_KBD_BRTUP) {
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08002511 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002512 return;
Chris Chiudbb3d782018-06-20 22:46:44 +08002513 }
2514 if (code == NOTIFY_KBD_BRTDWN) {
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08002515 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002516 return;
Chris Chiudbb3d782018-06-20 22:46:44 +08002517 }
Chris Chiued99d292018-06-20 22:46:45 +08002518 if (code == NOTIFY_KBD_BRTTOGGLE) {
2519 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08002520 kbd_led_set_by_kbd(asus, 0);
Chris Chiued99d292018-06-20 22:46:45 +08002521 else
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08002522 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002523 return;
Chris Chiued99d292018-06-20 22:46:45 +08002524 }
Chris Chiudbb3d782018-06-20 22:46:44 +08002525
Chris Chiu487579b2019-04-18 14:46:48 +08002526 if (code == NOTIFY_FNLOCK_TOGGLE) {
2527 asus->fnlock_locked = !asus->fnlock_locked;
2528 asus_wmi_fnlock_update(asus);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002529 return;
Chris Chiu487579b2019-04-18 14:46:48 +08002530 }
2531
Hans de Goede1797d582020-09-16 16:14:39 +02002532 if (asus->driver->quirks->use_kbd_dock_devid && code == NOTIFY_KBD_DOCK_CHANGE) {
Hans de Goedeb0dbd972020-05-10 14:24:31 +02002533 result = asus_wmi_get_devstate_simple(asus,
2534 ASUS_WMI_DEVID_KBD_DOCK);
2535 if (result >= 0) {
2536 input_report_switch(asus->inputdev, SW_TABLET_MODE,
2537 !result);
2538 input_sync(asus->inputdev);
2539 }
2540 return;
2541 }
2542
Samuel Čavojea856ec2020-10-21 00:09:44 +02002543 if (asus->driver->quirks->use_lid_flip_devid && code == NOTIFY_LID_FLIP) {
2544 lid_flip_tablet_mode_get_state(asus);
2545 return;
2546 }
2547
Daniel Drake9af93db2019-07-17 13:10:58 +08002548 if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
2549 fan_boost_mode_switch_next(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002550 return;
2551 }
2552
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02002553 if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
2554 throttle_thermal_policy_switch_next(asus);
2555 return;
2556 }
2557
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002558 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
2559 return;
AceLan Kaoa2a96f02012-10-03 11:26:31 +02002560
2561 if (!sparse_keymap_report_event(asus->inputdev, code,
2562 key_value, autorelease))
Corentin Chary57ab7da2011-02-26 10:20:32 +01002563 pr_info("Unknown key %x pressed\n", code);
Yong Wang3d7b1652010-04-11 09:27:54 +08002564}
2565
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002566static void asus_wmi_notify(u32 value, void *context)
2567{
2568 struct asus_wmi *asus = context;
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02002569 int code;
2570 int i;
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002571
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02002572 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2573 code = asus_wmi_get_event_code(value);
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02002574 if (code < 0) {
2575 pr_warn("Failed to get notify code: %d\n", code);
2576 return;
2577 }
2578
2579 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2580 return;
2581
2582 asus_wmi_handle_event_code(code, asus);
2583
2584 /*
2585 * Double check that queue is present:
2586 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
2587 */
2588 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
2589 return;
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02002590 }
2591
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02002592 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
2593}
2594
2595static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
2596{
2597 int code;
2598 int i;
2599
2600 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2601 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02002602 if (code < 0) {
2603 pr_warn("Failed to get event during flush: %d\n", code);
2604 return code;
2605 }
2606
2607 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2608 return 0;
2609 }
2610
2611 pr_warn("Failed to flush event queue\n");
2612 return -EIO;
Yong Wang81248882010-04-11 09:26:33 +08002613}
2614
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002615/* Sysfs **********************************************************************/
2616
Corentin Chary1d070f82011-02-26 10:20:36 +01002617static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
2618 const char *buf, size_t count)
Corentin Chary9e1565b2011-02-06 13:28:36 +01002619{
Corentin Chary9e1565b2011-02-06 13:28:36 +01002620 u32 retval;
Andy Shevchenkoa5556fa2019-08-16 14:15:55 +03002621 int err, value;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002622
Corentin Chary1d070f82011-02-26 10:20:36 +01002623 value = asus_wmi_get_devstate_simple(asus, devid);
Dan Carpenterb8298342015-11-11 01:18:16 +03002624 if (value < 0)
Corentin Chary9e1565b2011-02-06 13:28:36 +01002625 return value;
2626
Andy Shevchenkoa5556fa2019-08-16 14:15:55 +03002627 err = kstrtoint(buf, 0, &value);
2628 if (err)
2629 return err;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002630
Andy Shevchenkoa5556fa2019-08-16 14:15:55 +03002631 err = asus_wmi_set_devstate(devid, value, &retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +01002632 if (err < 0)
2633 return err;
2634
Andy Shevchenkoa5556fa2019-08-16 14:15:55 +03002635 return count;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002636}
2637
Corentin Chary1d070f82011-02-26 10:20:36 +01002638static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
Corentin Chary9e1565b2011-02-06 13:28:36 +01002639{
Corentin Chary1d070f82011-02-26 10:20:36 +01002640 int value = asus_wmi_get_devstate_simple(asus, devid);
Corentin Chary9e1565b2011-02-06 13:28:36 +01002641
2642 if (value < 0)
2643 return value;
2644
2645 return sprintf(buf, "%d\n", value);
2646}
2647
Corentin Charye12e6d92011-02-26 10:20:31 +01002648#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
Corentin Chary9e1565b2011-02-06 13:28:36 +01002649 static ssize_t show_##_name(struct device *dev, \
2650 struct device_attribute *attr, \
2651 char *buf) \
2652 { \
Corentin Chary1d070f82011-02-26 10:20:36 +01002653 struct asus_wmi *asus = dev_get_drvdata(dev); \
2654 \
2655 return show_sys_wmi(asus, _cm, buf); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01002656 } \
2657 static ssize_t store_##_name(struct device *dev, \
2658 struct device_attribute *attr, \
2659 const char *buf, size_t count) \
2660 { \
Corentin Chary1d070f82011-02-26 10:20:36 +01002661 struct asus_wmi *asus = dev_get_drvdata(dev); \
2662 \
2663 return store_sys_wmi(asus, _cm, buf, count); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01002664 } \
2665 static struct device_attribute dev_attr_##_name = { \
2666 .attr = { \
2667 .name = __stringify(_name), \
2668 .mode = _mode }, \
2669 .show = show_##_name, \
2670 .store = store_##_name, \
2671 }
2672
Corentin Charye12e6d92011-02-26 10:20:31 +01002673ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
2674ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
2675ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
AceLan Kaoc0b91b62012-06-13 09:32:07 +02002676ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
Oleksij Rempelaca234f2016-04-01 13:35:21 +02002677ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
Corentin Chary9e1565b2011-02-06 13:28:36 +01002678
Jérémy Lefaurea8fe3422017-04-21 22:19:45 -04002679static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -07002680 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -05002681{
Corentin Chary3df5fda2011-07-01 11:34:38 +02002682 int value, rv;
Chris Bagwell7f80d732010-10-11 18:47:18 -05002683
Andy Shevchenkoa5556fa2019-08-16 14:15:55 +03002684 rv = kstrtoint(buf, 0, &value);
2685 if (rv)
2686 return rv;
2687
Chris Bagwell7f80d732010-10-11 18:47:18 -05002688 if (value < 0 || value > 2)
2689 return -EINVAL;
2690
Corentin Chary3df5fda2011-07-01 11:34:38 +02002691 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
2692 if (rv < 0)
2693 return rv;
2694
2695 return count;
Chris Bagwell7f80d732010-10-11 18:47:18 -05002696}
2697
Jérémy Lefaurea8fe3422017-04-21 22:19:45 -04002698static DEVICE_ATTR_WO(cpufv);
Chris Bagwell7f80d732010-10-11 18:47:18 -05002699
Corentin Chary4e37b422010-11-29 08:14:08 +01002700static struct attribute *platform_attributes[] = {
2701 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +01002702 &dev_attr_camera.attr,
2703 &dev_attr_cardr.attr,
Corentin Chary4615bb62011-02-06 13:28:42 +01002704 &dev_attr_touchpad.attr,
Luke D. Jones382b91d2021-08-07 14:36:56 +12002705 &dev_attr_egpu_enable.attr,
Luke D. Jones98829e82021-08-07 14:36:55 +12002706 &dev_attr_dgpu_disable.attr,
AceLan Kaoc0b91b62012-06-13 09:32:07 +02002707 &dev_attr_lid_resume.attr,
Oleksij Rempelaca234f2016-04-01 13:35:21 +02002708 &dev_attr_als_enable.attr,
Daniel Drake9af93db2019-07-17 13:10:58 +08002709 &dev_attr_fan_boost_mode.attr,
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02002710 &dev_attr_throttle_thermal_policy.attr,
Luke D. Jonesca91ea342021-08-07 14:36:54 +12002711 &dev_attr_panel_od.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +01002712 NULL
2713};
2714
Al Viro587a1f12011-07-23 23:11:19 -04002715static umode_t asus_sysfs_is_visible(struct kobject *kobj,
Corentin Charye12e6d92011-02-26 10:20:31 +01002716 struct attribute *attr, int idx)
Corentin Chary9e1565b2011-02-06 13:28:36 +01002717{
Corentin Chary1d070f82011-02-26 10:20:36 +01002718 struct device *dev = container_of(kobj, struct device, kobj);
Wolfram Sangd605ca22018-04-19 16:06:10 +02002719 struct asus_wmi *asus = dev_get_drvdata(dev);
Corentin Chary1d070f82011-02-26 10:20:36 +01002720 bool ok = true;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002721 int devid = -1;
2722
2723 if (attr == &dev_attr_camera.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01002724 devid = ASUS_WMI_DEVID_CAMERA;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002725 else if (attr == &dev_attr_cardr.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01002726 devid = ASUS_WMI_DEVID_CARDREADER;
Corentin Chary4615bb62011-02-06 13:28:42 +01002727 else if (attr == &dev_attr_touchpad.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01002728 devid = ASUS_WMI_DEVID_TOUCHPAD;
AceLan Kaoc0b91b62012-06-13 09:32:07 +02002729 else if (attr == &dev_attr_lid_resume.attr)
2730 devid = ASUS_WMI_DEVID_LID_RESUME;
Oleksij Rempelaca234f2016-04-01 13:35:21 +02002731 else if (attr == &dev_attr_als_enable.attr)
2732 devid = ASUS_WMI_DEVID_ALS_ENABLE;
Luke D. Jones382b91d2021-08-07 14:36:56 +12002733 else if (attr == &dev_attr_egpu_enable.attr)
2734 ok = asus->egpu_enable_available;
Luke D. Jones98829e82021-08-07 14:36:55 +12002735 else if (attr == &dev_attr_dgpu_disable.attr)
2736 ok = asus->dgpu_disable_available;
Daniel Drake9af93db2019-07-17 13:10:58 +08002737 else if (attr == &dev_attr_fan_boost_mode.attr)
2738 ok = asus->fan_boost_mode_available;
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02002739 else if (attr == &dev_attr_throttle_thermal_policy.attr)
2740 ok = asus->throttle_thermal_policy_available;
Luke D. Jonesca91ea342021-08-07 14:36:54 +12002741 else if (attr == &dev_attr_panel_od.attr)
2742 ok = asus->panel_overdrive_available;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002743
2744 if (devid != -1)
Corentin Chary1d070f82011-02-26 10:20:36 +01002745 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
Corentin Chary9e1565b2011-02-06 13:28:36 +01002746
Corentin Chary1d070f82011-02-26 10:20:36 +01002747 return ok ? attr->mode : 0;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002748}
2749
Arvind Yadave90d9ba2017-07-11 16:18:19 +05302750static const struct attribute_group platform_attribute_group = {
Corentin Charye12e6d92011-02-26 10:20:31 +01002751 .is_visible = asus_sysfs_is_visible,
2752 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +01002753};
2754
Corentin Charye12e6d92011-02-26 10:20:31 +01002755static void asus_wmi_sysfs_exit(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05002756{
Corentin Chary4e37b422010-11-29 08:14:08 +01002757 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05002758}
2759
Corentin Charye12e6d92011-02-26 10:20:31 +01002760static int asus_wmi_sysfs_init(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05002761{
Corentin Chary4e37b422010-11-29 08:14:08 +01002762 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05002763}
2764
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002765/* Platform device ************************************************************/
2766
Joe Perches39ddf3b2011-03-29 15:21:32 -07002767static int asus_wmi_platform_init(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01002768{
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002769 struct device *dev = &asus->platform_device->dev;
2770 char *wmi_uid;
Corentin Chary46dbca82011-02-26 10:20:38 +01002771 int rv;
2772
2773 /* INIT enable hotkeys on some models */
2774 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
vic0ed60652013-05-22 21:32:10 +03002775 pr_info("Initialization: %#x\n", rv);
Corentin Chary46dbca82011-02-26 10:20:38 +01002776
2777 /* We don't know yet what to do with this version... */
2778 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
vic0ed60652013-05-22 21:32:10 +03002779 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
Corentin Chary46dbca82011-02-26 10:20:38 +01002780 asus->spec = rv;
2781 }
2782
2783 /*
2784 * The SFUN method probably allows the original driver to get the list
2785 * of features supported by a given model. For now, 0x0100 or 0x0800
2786 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
2787 * The significance of others is yet to be found.
2788 */
2789 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
vic0ed60652013-05-22 21:32:10 +03002790 pr_info("SFUN value: %#x\n", rv);
Corentin Chary46dbca82011-02-26 10:20:38 +01002791 asus->sfun = rv;
2792 }
2793
Corentin Chary1d070f82011-02-26 10:20:36 +01002794 /*
2795 * Eee PC and Notebooks seems to have different method_id for DSTS,
2796 * but it may also be related to the BIOS's SPEC.
2797 * Note, on most Eeepc, there is no way to check if a method exist
2798 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
2799 * but once again, SPEC may probably be used for that kind of things.
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002800 *
2801 * Additionally at least TUF Gaming series laptops return nothing for
2802 * unknown methods, so the detection in this way is not possible.
2803 *
2804 * There is strong indication that only ACPI WMI devices that have _UID
2805 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
Corentin Chary1d070f82011-02-26 10:20:36 +01002806 */
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002807 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
2808 if (!wmi_uid)
2809 return -ENODEV;
2810
2811 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
2812 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
2813 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
2814 } else {
2815 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
Corentin Chary1d070f82011-02-26 10:20:36 +01002816 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002817 }
Corentin Chary1d070f82011-02-26 10:20:36 +01002818
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02002819 /*
2820 * Some devices can have multiple event codes stored in a queue before
2821 * the module load if it was unloaded intermittently after calling
2822 * the INIT method (enables event handling). The WMI notify handler is
2823 * expected to retrieve all event codes until a retrieved code equals
2824 * queue end marker (One or Ones). Old codes are flushed from the queue
2825 * upon module load. Not enabling this when it should be has minimal
2826 * visible impact so fall back if anything goes wrong.
2827 */
2828 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
2829 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
2830 dev_info(dev, "Detected ATK, enable event queue\n");
2831
2832 if (!asus_wmi_notify_queue_flush(asus))
2833 asus->wmi_event_queue = true;
2834 }
Corentin Chary1d070f82011-02-26 10:20:36 +01002835
Corentin Charyfddbfed2011-07-01 11:34:39 +02002836 /* CWAP allow to define the behavior of the Fn+F2 key,
2837 * this method doesn't seems to be present on Eee PCs */
Corentin Chary6a2bccc2012-03-20 09:53:10 +01002838 if (asus->driver->quirks->wapf >= 0)
Corentin Charyfddbfed2011-07-01 11:34:39 +02002839 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
Corentin Chary6a2bccc2012-03-20 09:53:10 +01002840 asus->driver->quirks->wapf, NULL);
Corentin Charyfddbfed2011-07-01 11:34:39 +02002841
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002842 return 0;
Corentin Chary27c136c2010-11-29 08:14:05 +01002843}
2844
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002845/* debugfs ********************************************************************/
Corentin Chary27c136c2010-11-29 08:14:05 +01002846
Corentin Charye12e6d92011-02-26 10:20:31 +01002847struct asus_wmi_debugfs_node {
2848 struct asus_wmi *asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002849 char *name;
Corentin Charye12e6d92011-02-26 10:20:31 +01002850 int (*show) (struct seq_file *m, void *data);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002851};
2852
2853static int show_dsts(struct seq_file *m, void *data)
2854{
Corentin Charye12e6d92011-02-26 10:20:31 +01002855 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01002856 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002857 u32 retval = -1;
2858
Corentin Chary1d070f82011-02-26 10:20:36 +01002859 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +01002860 if (err < 0)
2861 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002862
Corentin Charyef343492011-02-26 10:20:39 +01002863 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002864
2865 return 0;
2866}
2867
2868static int show_devs(struct seq_file *m, void *data)
2869{
Corentin Charye12e6d92011-02-26 10:20:31 +01002870 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01002871 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002872 u32 retval = -1;
2873
Corentin Charyd33da3b2011-02-26 10:20:35 +01002874 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
2875 &retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +01002876 if (err < 0)
2877 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002878
Corentin Charyef343492011-02-26 10:20:39 +01002879 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
Corentin Charye12e6d92011-02-26 10:20:31 +01002880 asus->debug.ctrl_param, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002881
2882 return 0;
2883}
2884
Corentin Charyef343492011-02-26 10:20:39 +01002885static int show_call(struct seq_file *m, void *data)
2886{
2887 struct asus_wmi *asus = m->private;
2888 struct bios_args args = {
2889 .arg0 = asus->debug.dev_id,
2890 .arg1 = asus->debug.ctrl_param,
2891 };
2892 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2893 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
2894 union acpi_object *obj;
2895 acpi_status status;
2896
2897 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
Pali Rohár0fe57262017-08-12 09:44:16 +02002898 0, asus->debug.method_id,
Corentin Charyef343492011-02-26 10:20:39 +01002899 &input, &output);
2900
2901 if (ACPI_FAILURE(status))
2902 return -EIO;
2903
2904 obj = (union acpi_object *)output.pointer;
2905 if (obj && obj->type == ACPI_TYPE_INTEGER)
2906 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
2907 asus->debug.dev_id, asus->debug.ctrl_param,
2908 (u32) obj->integer.value);
2909 else
2910 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
2911 asus->debug.dev_id, asus->debug.ctrl_param,
Dan Carpentera1d60862011-03-15 10:07:37 +03002912 obj ? obj->type : -1);
Corentin Charyef343492011-02-26 10:20:39 +01002913
2914 kfree(obj);
2915
2916 return 0;
2917}
2918
Corentin Charye12e6d92011-02-26 10:20:31 +01002919static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
2920 {NULL, "devs", show_devs},
2921 {NULL, "dsts", show_dsts},
Corentin Charyef343492011-02-26 10:20:39 +01002922 {NULL, "call", show_call},
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002923};
2924
Corentin Charye12e6d92011-02-26 10:20:31 +01002925static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002926{
Corentin Charye12e6d92011-02-26 10:20:31 +01002927 struct asus_wmi_debugfs_node *node = inode->i_private;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002928
Corentin Charye12e6d92011-02-26 10:20:31 +01002929 return single_open(file, node->show, node->asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002930}
2931
Corentin Charye12e6d92011-02-26 10:20:31 +01002932static const struct file_operations asus_wmi_debugfs_io_ops = {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002933 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +01002934 .open = asus_wmi_debugfs_open,
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002935 .read = seq_read,
2936 .llseek = seq_lseek,
2937 .release = single_release,
2938};
2939
Corentin Charye12e6d92011-02-26 10:20:31 +01002940static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002941{
Corentin Charye12e6d92011-02-26 10:20:31 +01002942 debugfs_remove_recursive(asus->debug.root);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002943}
2944
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002945static void asus_wmi_debugfs_init(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002946{
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002947 int i;
2948
Corentin Charye12e6d92011-02-26 10:20:31 +01002949 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002950
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002951 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
2952 &asus->debug.method_id);
Corentin Charyef343492011-02-26 10:20:39 +01002953
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002954 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
2955 &asus->debug.dev_id);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002956
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002957 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
2958 &asus->debug.ctrl_param);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002959
Corentin Charye12e6d92011-02-26 10:20:31 +01002960 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2961 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002962
Corentin Charye12e6d92011-02-26 10:20:31 +01002963 node->asus = asus;
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002964 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2965 asus->debug.root, node,
2966 &asus_wmi_debugfs_io_ops);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002967 }
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002968}
2969
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002970/* Init / exit ****************************************************************/
Kast Bernd53e755c2015-05-13 16:24:16 +02002971
Corentin Charye12e6d92011-02-26 10:20:31 +01002972static int asus_wmi_add(struct platform_device *pdev)
Corentin Charyafa7c882011-02-06 13:28:28 +01002973{
Corentin Charye12e6d92011-02-26 10:20:31 +01002974 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2975 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2976 struct asus_wmi *asus;
Hans de Goeded8c66f62014-07-08 10:47:21 +02002977 const char *chassis_type;
Yong Wangee027e42010-03-21 10:26:34 +08002978 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01002979 int err;
AceLan Kaoa50bd122012-07-26 17:13:31 +08002980 u32 result;
Yong Wangee027e42010-03-21 10:26:34 +08002981
Corentin Charye12e6d92011-02-26 10:20:31 +01002982 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2983 if (!asus)
Corentin Charya04ce292011-02-06 13:28:33 +01002984 return -ENOMEM;
2985
Corentin Charye12e6d92011-02-26 10:20:31 +01002986 asus->driver = wdrv;
2987 asus->platform_device = pdev;
2988 wdrv->platform_device = pdev;
2989 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002990
AceLan Kaoc87992d2012-03-20 09:53:08 +01002991 if (wdrv->detect_quirks)
2992 wdrv->detect_quirks(asus->driver);
Corentin Charyafa7c882011-02-06 13:28:28 +01002993
Corentin Charye12e6d92011-02-26 10:20:31 +01002994 err = asus_wmi_platform_init(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002995 if (err)
2996 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08002997
Luke D. Jones382b91d2021-08-07 14:36:56 +12002998 err = egpu_enable_check_present(asus);
2999 if (err)
3000 goto fail_egpu_enable;
3001
Luke D. Jones98829e82021-08-07 14:36:55 +12003002 err = dgpu_disable_check_present(asus);
3003 if (err)
3004 goto fail_dgpu_disable;
3005
Daniel Drake9af93db2019-07-17 13:10:58 +08003006 err = fan_boost_mode_check_present(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02003007 if (err)
Daniel Drake9af93db2019-07-17 13:10:58 +08003008 goto fail_fan_boost_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02003009
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02003010 err = throttle_thermal_policy_check_present(asus);
3011 if (err)
3012 goto fail_throttle_thermal_policy;
Leonid Maksymchuka2821582019-12-15 16:27:24 +02003013 else
3014 throttle_thermal_policy_set_default(asus);
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02003015
Luke D. Jonesc63d44a2021-08-19 07:07:31 +12003016 err = platform_profile_setup(asus);
3017 if (err)
3018 goto fail_platform_profile_setup;
3019
Luke D. Jonesca91ea342021-08-07 14:36:54 +12003020 err = panel_od_check_present(asus);
3021 if (err)
3022 goto fail_panel_od;
3023
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02003024 err = asus_wmi_sysfs_init(asus->platform_device);
3025 if (err)
3026 goto fail_sysfs;
3027
Corentin Charye12e6d92011-02-26 10:20:31 +01003028 err = asus_wmi_input_init(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08003029 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01003030 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08003031
Kast Bernd53e755c2015-05-13 16:24:16 +02003032 err = asus_wmi_fan_init(asus); /* probably no problems on error */
Kast Bernd53e755c2015-05-13 16:24:16 +02003033
Corentin Charye07babd2011-02-26 10:20:42 +01003034 err = asus_wmi_hwmon_init(asus);
3035 if (err)
3036 goto fail_hwmon;
3037
Corentin Charye12e6d92011-02-26 10:20:31 +01003038 err = asus_wmi_led_init(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01003039 if (err)
3040 goto fail_leds;
3041
João Paulo Rechi Vita71050ae2017-02-20 14:50:22 -05003042 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
3043 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
3044 asus->driver->wlan_ctrl_by_user = 1;
3045
João Paulo Rechi Vitad1c4e9b2017-02-20 14:50:23 -05003046 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
João Paulo Rechi Vitaa977e592016-06-13 16:57:31 -04003047 err = asus_wmi_rfkill_init(asus);
3048 if (err)
3049 goto fail_rfkill;
3050 }
Corentin Charyba48fdb2010-11-29 08:14:07 +01003051
Oleksij Rempele9b61512017-04-28 16:19:49 +02003052 if (asus->driver->quirks->wmi_force_als_set)
3053 asus_wmi_set_als();
3054
Hans de Goeded8c66f62014-07-08 10:47:21 +02003055 /* Some Asus desktop boards export an acpi-video backlight interface,
3056 stop this from showing up */
3057 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
3058 if (chassis_type && !strcmp(chassis_type, "3"))
Hans de Goede62c4aa12015-06-16 16:27:58 +02003059 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3060
AceLan Kao272c77d2012-06-13 09:32:06 +02003061 if (asus->driver->quirks->wmi_backlight_power)
Hans de Goede62c4aa12015-06-16 16:27:58 +02003062 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3063
zino lin999d4372016-08-28 16:12:06 +08003064 if (asus->driver->quirks->wmi_backlight_native)
3065 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
3066
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +08003067 if (asus->driver->quirks->xusb2pr)
3068 asus_wmi_set_xusb2pr(asus);
3069
Hans de Goede62c4aa12015-06-16 16:27:58 +02003070 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Corentin Charye12e6d92011-02-26 10:20:31 +01003071 err = asus_wmi_backlight_init(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01003072 if (err && err != -ENODEV)
Corentin Chary27c136c2010-11-29 08:14:05 +01003073 goto fail_backlight;
Hans de Goede401fee82019-06-12 09:02:02 +02003074 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
João Paulo Rechi Vita78f3ac72018-10-31 17:21:26 -07003075 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
Yong Wang45f2c692010-04-11 09:27:19 +08003076
Chris Chiu487579b2019-04-18 14:46:48 +08003077 if (asus_wmi_has_fnlock_key(asus)) {
Luca Stefanice357fd2021-03-23 22:01:26 +01003078 asus->fnlock_locked = fnlock_default;
Chris Chiu487579b2019-04-18 14:46:48 +08003079 asus_wmi_fnlock_update(asus);
3080 }
3081
Corentin Charye12e6d92011-02-26 10:20:31 +01003082 status = wmi_install_notify_handler(asus->driver->event_guid,
3083 asus_wmi_notify, asus);
Yong Wang45f2c692010-04-11 09:27:19 +08003084 if (ACPI_FAILURE(status)) {
Corentin Charye12e6d92011-02-26 10:20:31 +01003085 pr_err("Unable to register notify handler - %d\n", status);
Yong Wang45f2c692010-04-11 09:27:19 +08003086 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01003087 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08003088 }
3089
Kristian Klausen79733532019-09-09 19:31:28 +02003090 asus_wmi_battery_init(asus);
3091
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02003092 asus_wmi_debugfs_init(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01003093
Corentin Charya04ce292011-02-06 13:28:33 +01003094 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08003095
Corentin Chary27c136c2010-11-29 08:14:05 +01003096fail_wmi_handler:
Corentin Charye12e6d92011-02-26 10:20:31 +01003097 asus_wmi_backlight_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01003098fail_backlight:
Corentin Charye12e6d92011-02-26 10:20:31 +01003099 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01003100fail_rfkill:
Corentin Charye12e6d92011-02-26 10:20:31 +01003101 asus_wmi_led_exit(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01003102fail_leds:
Corentin Charye07babd2011-02-26 10:20:42 +01003103fail_hwmon:
Corentin Charye12e6d92011-02-26 10:20:31 +01003104 asus_wmi_input_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01003105fail_input:
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02003106 asus_wmi_sysfs_exit(asus->platform_device);
3107fail_sysfs:
Leonid Maksymchuk2daa86e2019-12-15 16:26:34 +02003108fail_throttle_thermal_policy:
Luke D. Jonesc63d44a2021-08-19 07:07:31 +12003109fail_platform_profile_setup:
3110 if (asus->platform_profile_support)
3111 platform_profile_remove();
Daniel Drake9af93db2019-07-17 13:10:58 +08003112fail_fan_boost_mode:
Luke D. Jones382b91d2021-08-07 14:36:56 +12003113fail_egpu_enable:
Luke D. Jones98829e82021-08-07 14:36:55 +12003114fail_dgpu_disable:
Corentin Chary27c136c2010-11-29 08:14:05 +01003115fail_platform:
Luke D. Jonesca91ea342021-08-07 14:36:54 +12003116fail_panel_od:
Corentin Charye12e6d92011-02-26 10:20:31 +01003117 kfree(asus);
Corentin Charya04ce292011-02-06 13:28:33 +01003118 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08003119}
3120
Corentin Charye12e6d92011-02-26 10:20:31 +01003121static int asus_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08003122{
Corentin Charye12e6d92011-02-26 10:20:31 +01003123 struct asus_wmi *asus;
Yong Wang45f2c692010-04-11 09:27:19 +08003124
Corentin Charye12e6d92011-02-26 10:20:31 +01003125 asus = platform_get_drvdata(device);
3126 wmi_remove_notify_handler(asus->driver->event_guid);
3127 asus_wmi_backlight_exit(asus);
3128 asus_wmi_input_exit(asus);
3129 asus_wmi_led_exit(asus);
3130 asus_wmi_rfkill_exit(asus);
3131 asus_wmi_debugfs_exit(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02003132 asus_wmi_sysfs_exit(asus->platform_device);
Daniel Drake2889ffc2019-07-29 16:27:37 +08003133 asus_fan_set_auto(asus);
Kristian Klausen79733532019-09-09 19:31:28 +02003134 asus_wmi_battery_exit(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08003135
Luke D. Jonesc63d44a2021-08-19 07:07:31 +12003136 if (asus->platform_profile_support)
3137 platform_profile_remove();
3138
Corentin Charye12e6d92011-02-26 10:20:31 +01003139 kfree(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08003140 return 0;
3141}
3142
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02003143/* Platform driver - hibernate/resume callbacks *******************************/
3144
Corentin Charye12e6d92011-02-26 10:20:31 +01003145static int asus_hotk_thaw(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01003146{
Corentin Charye12e6d92011-02-26 10:20:31 +01003147 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01003148
Corentin Charya7ce3f02011-02-26 10:20:33 +01003149 if (asus->wlan.rfkill) {
Corentin Chary0773d7f2011-02-06 13:28:32 +01003150 bool wlan;
3151
3152 /*
3153 * Work around bios bug - acpi _PTS turns off the wireless led
3154 * during suspend. Normally it restores it on resume, but
3155 * we should kick it ourselves in case hibernation is aborted.
3156 */
Corentin Chary1d070f82011-02-26 10:20:36 +01003157 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charye12e6d92011-02-26 10:20:31 +01003158 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
Corentin Chary0773d7f2011-02-06 13:28:32 +01003159 }
3160
3161 return 0;
3162}
3163
Oleksij Rempel30734042015-09-14 11:16:30 +02003164static int asus_hotk_resume(struct device *device)
3165{
3166 struct asus_wmi *asus = dev_get_drvdata(device);
3167
3168 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +08003169 kbd_led_update(asus);
Oleksij Rempel30734042015-09-14 11:16:30 +02003170
Chris Chiu487579b2019-04-18 14:46:48 +08003171 if (asus_wmi_has_fnlock_key(asus))
3172 asus_wmi_fnlock_update(asus);
Samuel Čavojea856ec2020-10-21 00:09:44 +02003173
3174 if (asus->driver->quirks->use_lid_flip_devid)
3175 lid_flip_tablet_mode_get_state(asus);
3176
Oleksij Rempel30734042015-09-14 11:16:30 +02003177 return 0;
3178}
3179
Corentin Charye12e6d92011-02-26 10:20:31 +01003180static int asus_hotk_restore(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01003181{
Corentin Charye12e6d92011-02-26 10:20:31 +01003182 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01003183 int bl;
3184
3185 /* Refresh both wlan rfkill state and pci hotplug */
Corentin Charya7ce3f02011-02-26 10:20:33 +01003186 if (asus->wlan.rfkill)
Corentin Charye12e6d92011-02-26 10:20:31 +01003187 asus_rfkill_hotplug(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01003188
Corentin Charya7ce3f02011-02-26 10:20:33 +01003189 if (asus->bluetooth.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01003190 bl = !asus_wmi_get_devstate_simple(asus,
3191 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charya7ce3f02011-02-26 10:20:33 +01003192 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01003193 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01003194 if (asus->wimax.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01003195 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
Corentin Charya7ce3f02011-02-26 10:20:33 +01003196 rfkill_set_sw_state(asus->wimax.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01003197 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01003198 if (asus->wwan3g.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01003199 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
Corentin Charya7ce3f02011-02-26 10:20:33 +01003200 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
Corentin Chary0773d7f2011-02-06 13:28:32 +01003201 }
Corentin Chary43be8bd2011-07-01 11:34:40 +02003202 if (asus->gps.rfkill) {
3203 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
3204 rfkill_set_sw_state(asus->gps.rfkill, bl);
3205 }
Corentin Charya912d322011-07-01 11:34:41 +02003206 if (asus->uwb.rfkill) {
3207 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
3208 rfkill_set_sw_state(asus->uwb.rfkill, bl);
3209 }
Oleksij Rempel30734042015-09-14 11:16:30 +02003210 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +08003211 kbd_led_update(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01003212
Chris Chiu487579b2019-04-18 14:46:48 +08003213 if (asus_wmi_has_fnlock_key(asus))
3214 asus_wmi_fnlock_update(asus);
Samuel Čavojea856ec2020-10-21 00:09:44 +02003215
3216 if (asus->driver->quirks->use_lid_flip_devid)
3217 lid_flip_tablet_mode_get_state(asus);
3218
Corentin Chary0773d7f2011-02-06 13:28:32 +01003219 return 0;
3220}
3221
Corentin Charye12e6d92011-02-26 10:20:31 +01003222static const struct dev_pm_ops asus_pm_ops = {
3223 .thaw = asus_hotk_thaw,
3224 .restore = asus_hotk_restore,
Oleksij Rempel30734042015-09-14 11:16:30 +02003225 .resume = asus_hotk_resume,
Corentin Chary0773d7f2011-02-06 13:28:32 +01003226};
3227
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02003228/* Registration ***************************************************************/
3229
Corentin Charye12e6d92011-02-26 10:20:31 +01003230static int asus_wmi_probe(struct platform_device *pdev)
Corentin Charyd358cb52010-11-29 08:14:14 +01003231{
Corentin Charye12e6d92011-02-26 10:20:31 +01003232 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3233 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3234 int ret;
Corentin Charyd358cb52010-11-29 08:14:14 +01003235
Corentin Charye12e6d92011-02-26 10:20:31 +01003236 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
Hans de Goedec9946112019-01-21 14:24:36 +01003237 pr_warn("ASUS Management GUID not found\n");
Yong Wangee027e42010-03-21 10:26:34 +08003238 return -ENODEV;
3239 }
3240
Corentin Charye12e6d92011-02-26 10:20:31 +01003241 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
Hans de Goedec9946112019-01-21 14:24:36 +01003242 pr_warn("ASUS Event GUID not found\n");
Corentin Charyd358cb52010-11-29 08:14:14 +01003243 return -ENODEV;
3244 }
3245
Corentin Charye12e6d92011-02-26 10:20:31 +01003246 if (wdrv->probe) {
3247 ret = wdrv->probe(pdev);
3248 if (ret)
3249 return ret;
3250 }
3251
3252 return asus_wmi_add(pdev);
Corentin Charya04ce292011-02-06 13:28:33 +01003253}
Yong Wangee027e42010-03-21 10:26:34 +08003254
Corentin Charye12e6d92011-02-26 10:20:31 +01003255static bool used;
Yong Wangee027e42010-03-21 10:26:34 +08003256
Corentin Chary8fe8c252011-07-01 11:34:32 +02003257int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
Corentin Charya04ce292011-02-06 13:28:33 +01003258{
Corentin Charye12e6d92011-02-26 10:20:31 +01003259 struct platform_driver *platform_driver;
3260 struct platform_device *platform_device;
3261
3262 if (used)
3263 return -EBUSY;
3264
3265 platform_driver = &driver->platform_driver;
3266 platform_driver->remove = asus_wmi_remove;
3267 platform_driver->driver.owner = driver->owner;
3268 platform_driver->driver.name = driver->name;
3269 platform_driver->driver.pm = &asus_pm_ops;
3270
3271 platform_device = platform_create_bundle(platform_driver,
3272 asus_wmi_probe,
Corentin Charya04ce292011-02-06 13:28:33 +01003273 NULL, 0, NULL, 0);
3274 if (IS_ERR(platform_device))
3275 return PTR_ERR(platform_device);
Corentin Charye12e6d92011-02-26 10:20:31 +01003276
3277 used = true;
3278 return 0;
3279}
3280EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
3281
3282void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
3283{
3284 platform_device_unregister(driver->platform_device);
3285 platform_driver_unregister(&driver->platform_driver);
3286 used = false;
3287}
3288EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
3289
3290static int __init asus_wmi_init(void)
3291{
vic0ed60652013-05-22 21:32:10 +03003292 pr_info("ASUS WMI generic driver loaded\n");
Yong Wangee027e42010-03-21 10:26:34 +08003293 return 0;
3294}
3295
Corentin Charye12e6d92011-02-26 10:20:31 +01003296static void __exit asus_wmi_exit(void)
Yong Wangee027e42010-03-21 10:26:34 +08003297{
vic0ed60652013-05-22 21:32:10 +03003298 pr_info("ASUS WMI generic driver unloaded\n");
Yong Wangee027e42010-03-21 10:26:34 +08003299}
3300
Corentin Charye12e6d92011-02-26 10:20:31 +01003301module_init(asus_wmi_init);
3302module_exit(asus_wmi_exit);