xref: /linux/drivers/platform/x86/hp/hp-wmi.c (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * HP WMI hotkeys
4  *
5  * Copyright (C) 2008 Red Hat <mjg@redhat.com>
6  * Copyright (C) 2010, 2011 Anssi Hannula <anssi.hannula@iki.fi>
7  *
8  * Portions based on wistron_btns.c:
9  * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
10  * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
11  * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/platform_device.h>
24 #include <linux/platform_profile.h>
25 #include <linux/hwmon.h>
26 #include <linux/acpi.h>
27 #include <linux/mutex.h>
28 #include <linux/cleanup.h>
29 #include <linux/power_supply.h>
30 #include <linux/rfkill.h>
31 #include <linux/string.h>
32 #include <linux/dmi.h>
33 
34 MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
35 MODULE_DESCRIPTION("HP laptop WMI driver");
36 MODULE_LICENSE("GPL");
37 
38 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
39 MODULE_ALIAS("wmi:5FB7F034-2C63-45E9-BE91-3D44E2C707E4");
40 
41 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
42 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45E9-BE91-3D44E2C707E4"
43 
44 #define HP_OMEN_EC_THERMAL_PROFILE_FLAGS_OFFSET 0x62
45 #define HP_OMEN_EC_THERMAL_PROFILE_TIMER_OFFSET 0x63
46 #define HP_OMEN_EC_THERMAL_PROFILE_OFFSET 0x95
47 
48 #define HP_FAN_SPEED_AUTOMATIC	 0x00
49 #define HP_POWER_LIMIT_DEFAULT	 0x00
50 #define HP_POWER_LIMIT_NO_CHANGE 0xFF
51 
52 #define ACPI_AC_CLASS "ac_adapter"
53 
54 #define zero_if_sup(tmp) (zero_insize_support?0:sizeof(tmp)) // use when zero insize is required
55 
56 /* DMI board names of devices that should use the omen specific path for
57  * thermal profiles.
58  * This was obtained by taking a look in the windows omen command center
59  * app and parsing a json file that they use to figure out what capabilities
60  * the device should have.
61  * A device is considered an omen if the DisplayName in that list contains
62  * "OMEN", and it can use the thermal profile stuff if the "Feature" array
63  * contains "PerformanceControl".
64  */
65 static const char * const omen_thermal_profile_boards[] = {
66 	"84DA", "84DB", "84DC", "8574", "8575", "860A", "87B5", "8572", "8573",
67 	"8600", "8601", "8602", "8605", "8606", "8607", "8746", "8747", "8749",
68 	"874A", "8603", "8604", "8748", "886B", "886C", "878A", "878B", "878C",
69 	"88C8", "88CB", "8786", "8787", "8788", "88D1", "88D2", "88F4", "88FD",
70 	"88F5", "88F6", "88F7", "88FE", "88FF", "8900", "8901", "8902", "8912",
71 	"8917", "8918", "8949", "894A", "89EB", "8BAD", "8A42", "8A15"
72 };
73 
74 /* DMI Board names of Omen laptops that are specifically set to be thermal
75  * profile version 0 by the Omen Command Center app, regardless of what
76  * the get system design information WMI call returns
77  */
78 static const char * const omen_thermal_profile_force_v0_boards[] = {
79 	"8607", "8746", "8747", "8749", "874A", "8748"
80 };
81 
82 /* DMI board names of Omen laptops that have a thermal profile timer which will
83  * cause the embedded controller to set the thermal profile back to
84  * "balanced" when reaching zero.
85  */
86 static const char * const omen_timed_thermal_profile_boards[] = {
87 	"8BAD", "8A42", "8A15"
88 };
89 
90 /* DMI Board names of Victus 16-d1xxx laptops */
91 static const char * const victus_thermal_profile_boards[] = {
92 	"8A25"
93 };
94 
95 /* DMI Board names of Victus 16-s1000 laptops */
96 static const char * const victus_s_thermal_profile_boards[] = {
97 	"8C9C"
98 };
99 
100 enum hp_wmi_radio {
101 	HPWMI_WIFI	= 0x0,
102 	HPWMI_BLUETOOTH	= 0x1,
103 	HPWMI_WWAN	= 0x2,
104 	HPWMI_GPS	= 0x3,
105 };
106 
107 enum hp_wmi_event_ids {
108 	HPWMI_DOCK_EVENT		= 0x01,
109 	HPWMI_PARK_HDD			= 0x02,
110 	HPWMI_SMART_ADAPTER		= 0x03,
111 	HPWMI_BEZEL_BUTTON		= 0x04,
112 	HPWMI_WIRELESS			= 0x05,
113 	HPWMI_CPU_BATTERY_THROTTLE	= 0x06,
114 	HPWMI_LOCK_SWITCH		= 0x07,
115 	HPWMI_LID_SWITCH		= 0x08,
116 	HPWMI_SCREEN_ROTATION		= 0x09,
117 	HPWMI_COOLSENSE_SYSTEM_MOBILE	= 0x0A,
118 	HPWMI_COOLSENSE_SYSTEM_HOT	= 0x0B,
119 	HPWMI_PROXIMITY_SENSOR		= 0x0C,
120 	HPWMI_BACKLIT_KB_BRIGHTNESS	= 0x0D,
121 	HPWMI_PEAKSHIFT_PERIOD		= 0x0F,
122 	HPWMI_BATTERY_CHARGE_PERIOD	= 0x10,
123 	HPWMI_SANITIZATION_MODE		= 0x17,
124 	HPWMI_CAMERA_TOGGLE		= 0x1A,
125 	HPWMI_OMEN_KEY			= 0x1D,
126 	HPWMI_SMART_EXPERIENCE_APP	= 0x21,
127 };
128 
129 /*
130  * struct bios_args buffer is dynamically allocated.  New WMI command types
131  * were introduced that exceeds 128-byte data size.  Changes to handle
132  * the data size allocation scheme were kept in hp_wmi_perform_qurey function.
133  */
134 struct bios_args {
135 	u32 signature;
136 	u32 command;
137 	u32 commandtype;
138 	u32 datasize;
139 	u8 data[];
140 };
141 
142 enum hp_wmi_commandtype {
143 	HPWMI_DISPLAY_QUERY		= 0x01,
144 	HPWMI_HDDTEMP_QUERY		= 0x02,
145 	HPWMI_ALS_QUERY			= 0x03,
146 	HPWMI_HARDWARE_QUERY		= 0x04,
147 	HPWMI_WIRELESS_QUERY		= 0x05,
148 	HPWMI_BATTERY_QUERY		= 0x07,
149 	HPWMI_BIOS_QUERY		= 0x09,
150 	HPWMI_FEATURE_QUERY		= 0x0b,
151 	HPWMI_HOTKEY_QUERY		= 0x0c,
152 	HPWMI_FEATURE2_QUERY		= 0x0d,
153 	HPWMI_WIRELESS2_QUERY		= 0x1b,
154 	HPWMI_POSTCODEERROR_QUERY	= 0x2a,
155 	HPWMI_SYSTEM_DEVICE_MODE	= 0x40,
156 	HPWMI_THERMAL_PROFILE_QUERY	= 0x4c,
157 };
158 
159 struct victus_power_limits {
160 	u8 pl1;
161 	u8 pl2;
162 	u8 pl4;
163 	u8 cpu_gpu_concurrent_limit;
164 };
165 
166 struct victus_gpu_power_modes {
167 	u8 ctgp_enable;
168 	u8 ppab_enable;
169 	u8 dstate;
170 	u8 gpu_slowdown_temp;
171 };
172 
173 enum hp_wmi_gm_commandtype {
174 	HPWMI_FAN_SPEED_GET_QUERY		= 0x11,
175 	HPWMI_SET_PERFORMANCE_MODE		= 0x1A,
176 	HPWMI_FAN_SPEED_MAX_GET_QUERY		= 0x26,
177 	HPWMI_FAN_SPEED_MAX_SET_QUERY		= 0x27,
178 	HPWMI_GET_SYSTEM_DESIGN_DATA		= 0x28,
179 	HPWMI_FAN_COUNT_GET_QUERY		= 0x10,
180 	HPWMI_GET_GPU_THERMAL_MODES_QUERY	= 0x21,
181 	HPWMI_SET_GPU_THERMAL_MODES_QUERY	= 0x22,
182 	HPWMI_SET_POWER_LIMITS_QUERY		= 0x29,
183 	HPWMI_VICTUS_S_FAN_SPEED_GET_QUERY	= 0x2D,
184 	HPWMI_FAN_SPEED_SET_QUERY		= 0x2E,
185 };
186 
187 enum hp_wmi_command {
188 	HPWMI_READ	= 0x01,
189 	HPWMI_WRITE	= 0x02,
190 	HPWMI_ODM	= 0x03,
191 	HPWMI_GM	= 0x20008,
192 };
193 
194 enum hp_wmi_hardware_mask {
195 	HPWMI_DOCK_MASK		= 0x01,
196 	HPWMI_TABLET_MASK	= 0x04,
197 };
198 
199 struct bios_return {
200 	u32 sigpass;
201 	u32 return_code;
202 };
203 
204 enum hp_return_value {
205 	HPWMI_RET_WRONG_SIGNATURE	= 0x02,
206 	HPWMI_RET_UNKNOWN_COMMAND	= 0x03,
207 	HPWMI_RET_UNKNOWN_CMDTYPE	= 0x04,
208 	HPWMI_RET_INVALID_PARAMETERS	= 0x05,
209 };
210 
211 enum hp_wireless2_bits {
212 	HPWMI_POWER_STATE	= 0x01,
213 	HPWMI_POWER_SOFT	= 0x02,
214 	HPWMI_POWER_BIOS	= 0x04,
215 	HPWMI_POWER_HARD	= 0x08,
216 	HPWMI_POWER_FW_OR_HW	= HPWMI_POWER_BIOS | HPWMI_POWER_HARD,
217 };
218 
219 enum hp_thermal_profile_omen_v0 {
220 	HP_OMEN_V0_THERMAL_PROFILE_DEFAULT     = 0x00,
221 	HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE = 0x01,
222 	HP_OMEN_V0_THERMAL_PROFILE_COOL        = 0x02,
223 };
224 
225 enum hp_thermal_profile_omen_v1 {
226 	HP_OMEN_V1_THERMAL_PROFILE_DEFAULT	= 0x30,
227 	HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE	= 0x31,
228 	HP_OMEN_V1_THERMAL_PROFILE_COOL		= 0x50,
229 };
230 
231 enum hp_thermal_profile_omen_flags {
232 	HP_OMEN_EC_FLAGS_TURBO		= 0x04,
233 	HP_OMEN_EC_FLAGS_NOTIMER	= 0x02,
234 	HP_OMEN_EC_FLAGS_JUSTSET	= 0x01,
235 };
236 
237 enum hp_thermal_profile_victus {
238 	HP_VICTUS_THERMAL_PROFILE_DEFAULT		= 0x00,
239 	HP_VICTUS_THERMAL_PROFILE_PERFORMANCE		= 0x01,
240 	HP_VICTUS_THERMAL_PROFILE_QUIET			= 0x03,
241 };
242 
243 enum hp_thermal_profile_victus_s {
244 	HP_VICTUS_S_THERMAL_PROFILE_DEFAULT		= 0x00,
245 	HP_VICTUS_S_THERMAL_PROFILE_PERFORMANCE		= 0x01,
246 };
247 
248 enum hp_thermal_profile {
249 	HP_THERMAL_PROFILE_PERFORMANCE	= 0x00,
250 	HP_THERMAL_PROFILE_DEFAULT		= 0x01,
251 	HP_THERMAL_PROFILE_COOL			= 0x02,
252 	HP_THERMAL_PROFILE_QUIET		= 0x03,
253 };
254 
255 #define IS_HWBLOCKED(x) ((x & HPWMI_POWER_FW_OR_HW) != HPWMI_POWER_FW_OR_HW)
256 #define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
257 
258 struct bios_rfkill2_device_state {
259 	u8 radio_type;
260 	u8 bus_type;
261 	u16 vendor_id;
262 	u16 product_id;
263 	u16 subsys_vendor_id;
264 	u16 subsys_product_id;
265 	u8 rfkill_id;
266 	u8 power;
267 	u8 unknown[4];
268 };
269 
270 /* 7 devices fit into the 128 byte buffer */
271 #define HPWMI_MAX_RFKILL2_DEVICES	7
272 
273 struct bios_rfkill2_state {
274 	u8 unknown[7];
275 	u8 count;
276 	u8 pad[8];
277 	struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
278 };
279 
280 static const struct key_entry hp_wmi_keymap[] = {
281 	{ KE_KEY, 0x02,    { KEY_BRIGHTNESSUP } },
282 	{ KE_KEY, 0x03,    { KEY_BRIGHTNESSDOWN } },
283 	{ KE_KEY, 0x270,   { KEY_MICMUTE } },
284 	{ KE_KEY, 0x20e6,  { KEY_PROG1 } },
285 	{ KE_KEY, 0x20e8,  { KEY_MEDIA } },
286 	{ KE_KEY, 0x2142,  { KEY_MEDIA } },
287 	{ KE_KEY, 0x213b,  { KEY_INFO } },
288 	{ KE_KEY, 0x2169,  { KEY_ROTATE_DISPLAY } },
289 	{ KE_KEY, 0x216a,  { KEY_SETUP } },
290 	{ KE_IGNORE, 0x21a4,  }, /* Win Lock On */
291 	{ KE_IGNORE, 0x121a4, }, /* Win Lock Off */
292 	{ KE_KEY, 0x21a5,  { KEY_PROG2 } }, /* HP Omen Key */
293 	{ KE_KEY, 0x21a7,  { KEY_FN_ESC } },
294 	{ KE_KEY, 0x21a8,  { KEY_PROG2 } }, /* HP Envy x360 programmable key */
295 	{ KE_KEY, 0x21a9,  { KEY_TOUCHPAD_OFF } },
296 	{ KE_KEY, 0x121a9, { KEY_TOUCHPAD_ON } },
297 	{ KE_KEY, 0x231b,  { KEY_HELP } },
298 	{ KE_END, 0 }
299 };
300 
301 /*
302  * Mutex for the active_platform_profile variable,
303  * see omen_powersource_event.
304  */
305 static DEFINE_MUTEX(active_platform_profile_lock);
306 
307 static struct input_dev *hp_wmi_input_dev;
308 static struct input_dev *camera_shutter_input_dev;
309 static struct platform_device *hp_wmi_platform_dev;
310 static struct device *platform_profile_device;
311 static struct notifier_block platform_power_source_nb;
312 static enum platform_profile_option active_platform_profile;
313 static bool platform_profile_support;
314 static bool zero_insize_support;
315 
316 static struct rfkill *wifi_rfkill;
317 static struct rfkill *bluetooth_rfkill;
318 static struct rfkill *wwan_rfkill;
319 
320 struct rfkill2_device {
321 	u8 id;
322 	int num;
323 	struct rfkill *rfkill;
324 };
325 
326 static int rfkill2_count;
327 static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
328 
329 /*
330  * Chassis Types values were obtained from SMBIOS reference
331  * specification version 3.00. A complete list of system enclosures
332  * and chassis types is available on Table 17.
333  */
334 static const char * const tablet_chassis_types[] = {
335 	"30", /* Tablet*/
336 	"31", /* Convertible */
337 	"32"  /* Detachable */
338 };
339 
340 #define DEVICE_MODE_TABLET	0x06
341 
342 /* map output size to the corresponding WMI method id */
encode_outsize_for_pvsz(int outsize)343 static inline int encode_outsize_for_pvsz(int outsize)
344 {
345 	if (outsize > 4096)
346 		return -EINVAL;
347 	if (outsize > 1024)
348 		return 5;
349 	if (outsize > 128)
350 		return 4;
351 	if (outsize > 4)
352 		return 3;
353 	if (outsize > 0)
354 		return 2;
355 	return 1;
356 }
357 
358 /*
359  * hp_wmi_perform_query
360  *
361  * query:	The commandtype (enum hp_wmi_commandtype)
362  * write:	The command (enum hp_wmi_command)
363  * buffer:	Buffer used as input and/or output
364  * insize:	Size of input buffer
365  * outsize:	Size of output buffer
366  *
367  * returns zero on success
368  *         an HP WMI query specific error code (which is positive)
369  *         -EINVAL if the query was not successful at all
370  *         -EINVAL if the output buffer size exceeds buffersize
371  *
372  * Note: The buffersize must at least be the maximum of the input and output
373  *       size. E.g. Battery info query is defined to have 1 byte input
374  *       and 128 byte output. The caller would do:
375  *       buffer = kzalloc(128, GFP_KERNEL);
376  *       ret = hp_wmi_perform_query(HPWMI_BATTERY_QUERY, HPWMI_READ, buffer, 1, 128)
377  */
hp_wmi_perform_query(int query,enum hp_wmi_command command,void * buffer,int insize,int outsize)378 static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
379 				void *buffer, int insize, int outsize)
380 {
381 	struct acpi_buffer input, output = { ACPI_ALLOCATE_BUFFER, NULL };
382 	struct bios_return *bios_return;
383 	union acpi_object *obj = NULL;
384 	struct bios_args *args = NULL;
385 	int mid, actual_insize, actual_outsize;
386 	size_t bios_args_size;
387 	int ret;
388 
389 	mid = encode_outsize_for_pvsz(outsize);
390 	if (WARN_ON(mid < 0))
391 		return mid;
392 
393 	actual_insize = max(insize, 128);
394 	bios_args_size = struct_size(args, data, actual_insize);
395 	args = kmalloc(bios_args_size, GFP_KERNEL);
396 	if (!args)
397 		return -ENOMEM;
398 
399 	input.length = bios_args_size;
400 	input.pointer = args;
401 
402 	args->signature = 0x55434553;
403 	args->command = command;
404 	args->commandtype = query;
405 	args->datasize = insize;
406 	memcpy(args->data, buffer, flex_array_size(args, data, insize));
407 
408 	ret = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, mid, &input, &output);
409 	if (ret)
410 		goto out_free;
411 
412 	obj = output.pointer;
413 	if (!obj) {
414 		ret = -EINVAL;
415 		goto out_free;
416 	}
417 
418 	if (obj->type != ACPI_TYPE_BUFFER) {
419 		pr_warn("query 0x%x returned an invalid object 0x%x\n", query, ret);
420 		ret = -EINVAL;
421 		goto out_free;
422 	}
423 
424 	bios_return = (struct bios_return *)obj->buffer.pointer;
425 	ret = bios_return->return_code;
426 
427 	if (ret) {
428 		if (ret != HPWMI_RET_UNKNOWN_COMMAND &&
429 		    ret != HPWMI_RET_UNKNOWN_CMDTYPE)
430 			pr_warn("query 0x%x returned error 0x%x\n", query, ret);
431 		goto out_free;
432 	}
433 
434 	/* Ignore output data of zero size */
435 	if (!outsize)
436 		goto out_free;
437 
438 	actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
439 	memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
440 	memset(buffer + actual_outsize, 0, outsize - actual_outsize);
441 
442 out_free:
443 	kfree(obj);
444 	kfree(args);
445 	return ret;
446 }
447 
448 /*
449  * Calling this hp_wmi_get_fan_count_userdefine_trigger function also enables
450  * and/or maintains the laptop in user defined thermal and fan states, instead
451  * of using a fallback state. After a 120 seconds timeout however, the laptop
452  * goes back to its fallback state.
453  */
hp_wmi_get_fan_count_userdefine_trigger(void)454 static int hp_wmi_get_fan_count_userdefine_trigger(void)
455 {
456 	u8 fan_data[4] = {};
457 	int ret;
458 
459 	ret = hp_wmi_perform_query(HPWMI_FAN_COUNT_GET_QUERY, HPWMI_GM,
460 				   &fan_data, sizeof(u8),
461 				   sizeof(fan_data));
462 	if (ret != 0)
463 		return -EINVAL;
464 
465 	return fan_data[0]; /* Others bytes aren't providing fan count */
466 }
467 
hp_wmi_get_fan_speed(int fan)468 static int hp_wmi_get_fan_speed(int fan)
469 {
470 	u8 fsh, fsl;
471 	char fan_data[4] = { fan, 0, 0, 0 };
472 
473 	int ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_GET_QUERY, HPWMI_GM,
474 				       &fan_data, sizeof(char),
475 				       sizeof(fan_data));
476 
477 	if (ret != 0)
478 		return -EINVAL;
479 
480 	fsh = fan_data[2];
481 	fsl = fan_data[3];
482 
483 	return (fsh << 8) | fsl;
484 }
485 
hp_wmi_get_fan_speed_victus_s(int fan)486 static int hp_wmi_get_fan_speed_victus_s(int fan)
487 {
488 	u8 fan_data[128] = {};
489 	int ret;
490 
491 	if (fan < 0 || fan >= sizeof(fan_data))
492 		return -EINVAL;
493 
494 	ret = hp_wmi_perform_query(HPWMI_VICTUS_S_FAN_SPEED_GET_QUERY,
495 				   HPWMI_GM, &fan_data, sizeof(u8),
496 				   sizeof(fan_data));
497 	if (ret != 0)
498 		return -EINVAL;
499 
500 	return fan_data[fan] * 100;
501 }
502 
hp_wmi_read_int(int query)503 static int hp_wmi_read_int(int query)
504 {
505 	int val = 0, ret;
506 
507 	ret = hp_wmi_perform_query(query, HPWMI_READ, &val,
508 				   zero_if_sup(val), sizeof(val));
509 
510 	if (ret)
511 		return ret < 0 ? ret : -EINVAL;
512 
513 	return val;
514 }
515 
hp_wmi_get_dock_state(void)516 static int hp_wmi_get_dock_state(void)
517 {
518 	int state = hp_wmi_read_int(HPWMI_HARDWARE_QUERY);
519 
520 	if (state < 0)
521 		return state;
522 
523 	return !!(state & HPWMI_DOCK_MASK);
524 }
525 
hp_wmi_get_tablet_mode(void)526 static int hp_wmi_get_tablet_mode(void)
527 {
528 	char system_device_mode[4] = { 0 };
529 	const char *chassis_type;
530 	bool tablet_found;
531 	int ret;
532 
533 	chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
534 	if (!chassis_type)
535 		return -ENODEV;
536 
537 	tablet_found = match_string(tablet_chassis_types,
538 				    ARRAY_SIZE(tablet_chassis_types),
539 				    chassis_type) >= 0;
540 	if (!tablet_found)
541 		return -ENODEV;
542 
543 	ret = hp_wmi_perform_query(HPWMI_SYSTEM_DEVICE_MODE, HPWMI_READ,
544 				   system_device_mode, zero_if_sup(system_device_mode),
545 				   sizeof(system_device_mode));
546 	if (ret < 0)
547 		return ret;
548 
549 	return system_device_mode[0] == DEVICE_MODE_TABLET;
550 }
551 
omen_thermal_profile_set(int mode)552 static int omen_thermal_profile_set(int mode)
553 {
554 	/* The Omen Control Center actively sets the first byte of the buffer to
555 	 * 255, so let's mimic this behaviour to be as close as possible to
556 	 * the original software.
557 	 */
558 	char buffer[2] = {-1, mode};
559 	int ret;
560 
561 	ret = hp_wmi_perform_query(HPWMI_SET_PERFORMANCE_MODE, HPWMI_GM,
562 				   &buffer, sizeof(buffer), 0);
563 
564 	if (ret)
565 		return ret < 0 ? ret : -EINVAL;
566 
567 	return mode;
568 }
569 
is_omen_thermal_profile(void)570 static bool is_omen_thermal_profile(void)
571 {
572 	const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
573 
574 	if (!board_name)
575 		return false;
576 
577 	return match_string(omen_thermal_profile_boards,
578 			    ARRAY_SIZE(omen_thermal_profile_boards),
579 			    board_name) >= 0;
580 }
581 
omen_get_thermal_policy_version(void)582 static int omen_get_thermal_policy_version(void)
583 {
584 	unsigned char buffer[8] = { 0 };
585 	int ret;
586 
587 	const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
588 
589 	if (board_name) {
590 		int matches = match_string(omen_thermal_profile_force_v0_boards,
591 			ARRAY_SIZE(omen_thermal_profile_force_v0_boards),
592 			board_name);
593 		if (matches >= 0)
594 			return 0;
595 	}
596 
597 	ret = hp_wmi_perform_query(HPWMI_GET_SYSTEM_DESIGN_DATA, HPWMI_GM,
598 				   &buffer, sizeof(buffer), sizeof(buffer));
599 
600 	if (ret)
601 		return ret < 0 ? ret : -EINVAL;
602 
603 	return buffer[3];
604 }
605 
omen_thermal_profile_get(void)606 static int omen_thermal_profile_get(void)
607 {
608 	u8 data;
609 
610 	int ret = ec_read(HP_OMEN_EC_THERMAL_PROFILE_OFFSET, &data);
611 
612 	if (ret)
613 		return ret;
614 
615 	return data;
616 }
617 
hp_wmi_fan_speed_max_set(int enabled)618 static int hp_wmi_fan_speed_max_set(int enabled)
619 {
620 	int ret;
621 
622 	ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_MAX_SET_QUERY, HPWMI_GM,
623 				   &enabled, sizeof(enabled), 0);
624 
625 	if (ret)
626 		return ret < 0 ? ret : -EINVAL;
627 
628 	return enabled;
629 }
630 
hp_wmi_fan_speed_reset(void)631 static int hp_wmi_fan_speed_reset(void)
632 {
633 	u8 fan_speed[2] = { HP_FAN_SPEED_AUTOMATIC, HP_FAN_SPEED_AUTOMATIC };
634 	int ret;
635 
636 	ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_SET_QUERY, HPWMI_GM,
637 				   &fan_speed, sizeof(fan_speed), 0);
638 
639 	return ret;
640 }
641 
hp_wmi_fan_speed_max_reset(void)642 static int hp_wmi_fan_speed_max_reset(void)
643 {
644 	int ret;
645 
646 	ret = hp_wmi_fan_speed_max_set(0);
647 	if (ret)
648 		return ret;
649 
650 	/* Disabling max fan speed on Victus s1xxx laptops needs a 2nd step: */
651 	ret = hp_wmi_fan_speed_reset();
652 	return ret;
653 }
654 
hp_wmi_fan_speed_max_get(void)655 static int hp_wmi_fan_speed_max_get(void)
656 {
657 	int val = 0, ret;
658 
659 	ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_MAX_GET_QUERY, HPWMI_GM,
660 				   &val, zero_if_sup(val), sizeof(val));
661 
662 	if (ret)
663 		return ret < 0 ? ret : -EINVAL;
664 
665 	return val;
666 }
667 
hp_wmi_bios_2008_later(void)668 static int __init hp_wmi_bios_2008_later(void)
669 {
670 	int state = 0;
671 	int ret = hp_wmi_perform_query(HPWMI_FEATURE_QUERY, HPWMI_READ, &state,
672 				       zero_if_sup(state), sizeof(state));
673 	if (!ret)
674 		return 1;
675 
676 	return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
677 }
678 
hp_wmi_bios_2009_later(void)679 static int __init hp_wmi_bios_2009_later(void)
680 {
681 	u8 state[128];
682 	int ret = hp_wmi_perform_query(HPWMI_FEATURE2_QUERY, HPWMI_READ, &state,
683 				       zero_if_sup(state), sizeof(state));
684 	if (!ret)
685 		return 1;
686 
687 	return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
688 }
689 
hp_wmi_enable_hotkeys(void)690 static int __init hp_wmi_enable_hotkeys(void)
691 {
692 	int value = 0x6e;
693 	int ret = hp_wmi_perform_query(HPWMI_BIOS_QUERY, HPWMI_WRITE, &value,
694 				       sizeof(value), 0);
695 
696 	return ret <= 0 ? ret : -EINVAL;
697 }
698 
hp_wmi_set_block(void * data,bool blocked)699 static int hp_wmi_set_block(void *data, bool blocked)
700 {
701 	enum hp_wmi_radio r = (long)data;
702 	int query = BIT(r + 8) | ((!blocked) << r);
703 	int ret;
704 
705 	ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE,
706 				   &query, sizeof(query), 0);
707 
708 	return ret <= 0 ? ret : -EINVAL;
709 }
710 
711 static const struct rfkill_ops hp_wmi_rfkill_ops = {
712 	.set_block = hp_wmi_set_block,
713 };
714 
hp_wmi_get_sw_state(enum hp_wmi_radio r)715 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
716 {
717 	int mask = 0x200 << (r * 8);
718 
719 	int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
720 
721 	/* TBD: Pass error */
722 	WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
723 
724 	return !(wireless & mask);
725 }
726 
hp_wmi_get_hw_state(enum hp_wmi_radio r)727 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
728 {
729 	int mask = 0x800 << (r * 8);
730 
731 	int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
732 
733 	/* TBD: Pass error */
734 	WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
735 
736 	return !(wireless & mask);
737 }
738 
hp_wmi_rfkill2_set_block(void * data,bool blocked)739 static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
740 {
741 	int rfkill_id = (int)(long)data;
742 	char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
743 	int ret;
744 
745 	ret = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_WRITE,
746 				   buffer, sizeof(buffer), 0);
747 
748 	return ret <= 0 ? ret : -EINVAL;
749 }
750 
751 static const struct rfkill_ops hp_wmi_rfkill2_ops = {
752 	.set_block = hp_wmi_rfkill2_set_block,
753 };
754 
hp_wmi_rfkill2_refresh(void)755 static int hp_wmi_rfkill2_refresh(void)
756 {
757 	struct bios_rfkill2_state state;
758 	int err, i;
759 
760 	err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
761 				   zero_if_sup(state), sizeof(state));
762 	if (err)
763 		return err;
764 
765 	for (i = 0; i < rfkill2_count; i++) {
766 		int num = rfkill2[i].num;
767 		struct bios_rfkill2_device_state *devstate;
768 
769 		devstate = &state.device[num];
770 
771 		if (num >= state.count ||
772 		    devstate->rfkill_id != rfkill2[i].id) {
773 			pr_warn("power configuration of the wireless devices unexpectedly changed\n");
774 			continue;
775 		}
776 
777 		rfkill_set_states(rfkill2[i].rfkill,
778 				  IS_SWBLOCKED(devstate->power),
779 				  IS_HWBLOCKED(devstate->power));
780 	}
781 
782 	return 0;
783 }
784 
display_show(struct device * dev,struct device_attribute * attr,char * buf)785 static ssize_t display_show(struct device *dev, struct device_attribute *attr,
786 			    char *buf)
787 {
788 	int value = hp_wmi_read_int(HPWMI_DISPLAY_QUERY);
789 
790 	if (value < 0)
791 		return value;
792 	return sysfs_emit(buf, "%d\n", value);
793 }
794 
hddtemp_show(struct device * dev,struct device_attribute * attr,char * buf)795 static ssize_t hddtemp_show(struct device *dev, struct device_attribute *attr,
796 			    char *buf)
797 {
798 	int value = hp_wmi_read_int(HPWMI_HDDTEMP_QUERY);
799 
800 	if (value < 0)
801 		return value;
802 	return sysfs_emit(buf, "%d\n", value);
803 }
804 
als_show(struct device * dev,struct device_attribute * attr,char * buf)805 static ssize_t als_show(struct device *dev, struct device_attribute *attr,
806 			char *buf)
807 {
808 	int value = hp_wmi_read_int(HPWMI_ALS_QUERY);
809 
810 	if (value < 0)
811 		return value;
812 	return sysfs_emit(buf, "%d\n", value);
813 }
814 
dock_show(struct device * dev,struct device_attribute * attr,char * buf)815 static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
816 			 char *buf)
817 {
818 	int value = hp_wmi_get_dock_state();
819 
820 	if (value < 0)
821 		return value;
822 	return sysfs_emit(buf, "%d\n", value);
823 }
824 
tablet_show(struct device * dev,struct device_attribute * attr,char * buf)825 static ssize_t tablet_show(struct device *dev, struct device_attribute *attr,
826 			   char *buf)
827 {
828 	int value = hp_wmi_get_tablet_mode();
829 
830 	if (value < 0)
831 		return value;
832 	return sysfs_emit(buf, "%d\n", value);
833 }
834 
postcode_show(struct device * dev,struct device_attribute * attr,char * buf)835 static ssize_t postcode_show(struct device *dev, struct device_attribute *attr,
836 			     char *buf)
837 {
838 	/* Get the POST error code of previous boot failure. */
839 	int value = hp_wmi_read_int(HPWMI_POSTCODEERROR_QUERY);
840 
841 	if (value < 0)
842 		return value;
843 	return sysfs_emit(buf, "0x%x\n", value);
844 }
845 
als_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)846 static ssize_t als_store(struct device *dev, struct device_attribute *attr,
847 			 const char *buf, size_t count)
848 {
849 	u32 tmp;
850 	int ret;
851 
852 	ret = kstrtou32(buf, 10, &tmp);
853 	if (ret)
854 		return ret;
855 
856 	ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_WRITE, &tmp,
857 				       sizeof(tmp), 0);
858 	if (ret)
859 		return ret < 0 ? ret : -EINVAL;
860 
861 	return count;
862 }
863 
postcode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)864 static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,
865 			      const char *buf, size_t count)
866 {
867 	u32 tmp = 1;
868 	bool clear;
869 	int ret;
870 
871 	ret = kstrtobool(buf, &clear);
872 	if (ret)
873 		return ret;
874 
875 	if (clear == false)
876 		return -EINVAL;
877 
878 	/* Clear the POST error code. It is kept until cleared. */
879 	ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_WRITE, &tmp,
880 				       sizeof(tmp), 0);
881 	if (ret)
882 		return ret < 0 ? ret : -EINVAL;
883 
884 	return count;
885 }
886 
camera_shutter_input_setup(void)887 static int camera_shutter_input_setup(void)
888 {
889 	int err;
890 
891 	camera_shutter_input_dev = input_allocate_device();
892 	if (!camera_shutter_input_dev)
893 		return -ENOMEM;
894 
895 	camera_shutter_input_dev->name = "HP WMI camera shutter";
896 	camera_shutter_input_dev->phys = "wmi/input1";
897 	camera_shutter_input_dev->id.bustype = BUS_HOST;
898 
899 	__set_bit(EV_SW, camera_shutter_input_dev->evbit);
900 	__set_bit(SW_CAMERA_LENS_COVER, camera_shutter_input_dev->swbit);
901 
902 	err = input_register_device(camera_shutter_input_dev);
903 	if (err)
904 		goto err_free_dev;
905 
906 	return 0;
907 
908  err_free_dev:
909 	input_free_device(camera_shutter_input_dev);
910 	camera_shutter_input_dev = NULL;
911 	return err;
912 }
913 
914 static DEVICE_ATTR_RO(display);
915 static DEVICE_ATTR_RO(hddtemp);
916 static DEVICE_ATTR_RW(als);
917 static DEVICE_ATTR_RO(dock);
918 static DEVICE_ATTR_RO(tablet);
919 static DEVICE_ATTR_RW(postcode);
920 
921 static struct attribute *hp_wmi_attrs[] = {
922 	&dev_attr_display.attr,
923 	&dev_attr_hddtemp.attr,
924 	&dev_attr_als.attr,
925 	&dev_attr_dock.attr,
926 	&dev_attr_tablet.attr,
927 	&dev_attr_postcode.attr,
928 	NULL,
929 };
930 ATTRIBUTE_GROUPS(hp_wmi);
931 
hp_wmi_notify(union acpi_object * obj,void * context)932 static void hp_wmi_notify(union acpi_object *obj, void *context)
933 {
934 	u32 event_id, event_data;
935 	u32 *location;
936 	int key_code;
937 
938 	if (!obj)
939 		return;
940 	if (obj->type != ACPI_TYPE_BUFFER) {
941 		pr_info("Unknown response received %d\n", obj->type);
942 		return;
943 	}
944 
945 	/*
946 	 * Depending on ACPI version the concatenation of id and event data
947 	 * inside _WED function will result in a 8 or 16 byte buffer.
948 	 */
949 	location = (u32 *)obj->buffer.pointer;
950 	if (obj->buffer.length == 8) {
951 		event_id = *location;
952 		event_data = *(location + 1);
953 	} else if (obj->buffer.length == 16) {
954 		event_id = *location;
955 		event_data = *(location + 2);
956 	} else {
957 		pr_info("Unknown buffer length %d\n", obj->buffer.length);
958 		return;
959 	}
960 
961 	switch (event_id) {
962 	case HPWMI_DOCK_EVENT:
963 		if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
964 			input_report_switch(hp_wmi_input_dev, SW_DOCK,
965 					    hp_wmi_get_dock_state());
966 		if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
967 			input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
968 					    hp_wmi_get_tablet_mode());
969 		input_sync(hp_wmi_input_dev);
970 		break;
971 	case HPWMI_PARK_HDD:
972 		break;
973 	case HPWMI_SMART_ADAPTER:
974 		break;
975 	case HPWMI_BEZEL_BUTTON:
976 		key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
977 		if (key_code < 0)
978 			break;
979 
980 		if (!sparse_keymap_report_event(hp_wmi_input_dev,
981 						key_code, 1, true))
982 			pr_info("Unknown key code - 0x%x\n", key_code);
983 		break;
984 	case HPWMI_OMEN_KEY:
985 		if (event_data) /* Only should be true for HP Omen */
986 			key_code = event_data;
987 		else
988 			key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
989 
990 		if (!sparse_keymap_report_event(hp_wmi_input_dev,
991 						key_code, 1, true))
992 			pr_info("Unknown key code - 0x%x\n", key_code);
993 		break;
994 	case HPWMI_WIRELESS:
995 		if (rfkill2_count) {
996 			hp_wmi_rfkill2_refresh();
997 			break;
998 		}
999 
1000 		if (wifi_rfkill)
1001 			rfkill_set_states(wifi_rfkill,
1002 					  hp_wmi_get_sw_state(HPWMI_WIFI),
1003 					  hp_wmi_get_hw_state(HPWMI_WIFI));
1004 		if (bluetooth_rfkill)
1005 			rfkill_set_states(bluetooth_rfkill,
1006 					  hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
1007 					  hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
1008 		if (wwan_rfkill)
1009 			rfkill_set_states(wwan_rfkill,
1010 					  hp_wmi_get_sw_state(HPWMI_WWAN),
1011 					  hp_wmi_get_hw_state(HPWMI_WWAN));
1012 		break;
1013 	case HPWMI_CPU_BATTERY_THROTTLE:
1014 		pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
1015 		break;
1016 	case HPWMI_LOCK_SWITCH:
1017 		break;
1018 	case HPWMI_LID_SWITCH:
1019 		break;
1020 	case HPWMI_SCREEN_ROTATION:
1021 		break;
1022 	case HPWMI_COOLSENSE_SYSTEM_MOBILE:
1023 		break;
1024 	case HPWMI_COOLSENSE_SYSTEM_HOT:
1025 		break;
1026 	case HPWMI_PROXIMITY_SENSOR:
1027 		break;
1028 	case HPWMI_BACKLIT_KB_BRIGHTNESS:
1029 		break;
1030 	case HPWMI_PEAKSHIFT_PERIOD:
1031 		break;
1032 	case HPWMI_BATTERY_CHARGE_PERIOD:
1033 		break;
1034 	case HPWMI_SANITIZATION_MODE:
1035 		break;
1036 	case HPWMI_CAMERA_TOGGLE:
1037 		if (!camera_shutter_input_dev)
1038 			if (camera_shutter_input_setup()) {
1039 				pr_err("Failed to setup camera shutter input device\n");
1040 				break;
1041 			}
1042 		if (event_data == 0xff)
1043 			input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 1);
1044 		else if (event_data == 0xfe)
1045 			input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 0);
1046 		else
1047 			pr_warn("Unknown camera shutter state - 0x%x\n", event_data);
1048 		input_sync(camera_shutter_input_dev);
1049 		break;
1050 	case HPWMI_SMART_EXPERIENCE_APP:
1051 		break;
1052 	default:
1053 		pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
1054 		break;
1055 	}
1056 }
1057 
hp_wmi_input_setup(void)1058 static int __init hp_wmi_input_setup(void)
1059 {
1060 	acpi_status status;
1061 	int err, val;
1062 
1063 	hp_wmi_input_dev = input_allocate_device();
1064 	if (!hp_wmi_input_dev)
1065 		return -ENOMEM;
1066 
1067 	hp_wmi_input_dev->name = "HP WMI hotkeys";
1068 	hp_wmi_input_dev->phys = "wmi/input0";
1069 	hp_wmi_input_dev->id.bustype = BUS_HOST;
1070 
1071 	__set_bit(EV_SW, hp_wmi_input_dev->evbit);
1072 
1073 	/* Dock */
1074 	val = hp_wmi_get_dock_state();
1075 	if (!(val < 0)) {
1076 		__set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
1077 		input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
1078 	}
1079 
1080 	/* Tablet mode */
1081 	val = hp_wmi_get_tablet_mode();
1082 	if (!(val < 0)) {
1083 		__set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
1084 		input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
1085 	}
1086 
1087 	err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
1088 	if (err)
1089 		goto err_free_dev;
1090 
1091 	/* Set initial hardware state */
1092 	input_sync(hp_wmi_input_dev);
1093 
1094 	if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
1095 		hp_wmi_enable_hotkeys();
1096 
1097 	status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
1098 	if (ACPI_FAILURE(status)) {
1099 		err = -EIO;
1100 		goto err_free_dev;
1101 	}
1102 
1103 	err = input_register_device(hp_wmi_input_dev);
1104 	if (err)
1105 		goto err_uninstall_notifier;
1106 
1107 	return 0;
1108 
1109  err_uninstall_notifier:
1110 	wmi_remove_notify_handler(HPWMI_EVENT_GUID);
1111  err_free_dev:
1112 	input_free_device(hp_wmi_input_dev);
1113 	return err;
1114 }
1115 
hp_wmi_input_destroy(void)1116 static void hp_wmi_input_destroy(void)
1117 {
1118 	wmi_remove_notify_handler(HPWMI_EVENT_GUID);
1119 	input_unregister_device(hp_wmi_input_dev);
1120 }
1121 
hp_wmi_rfkill_setup(struct platform_device * device)1122 static int __init hp_wmi_rfkill_setup(struct platform_device *device)
1123 {
1124 	int err, wireless;
1125 
1126 	wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
1127 	if (wireless < 0)
1128 		return wireless;
1129 
1130 	err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE, &wireless,
1131 				   sizeof(wireless), 0);
1132 	if (err)
1133 		return err;
1134 
1135 	if (wireless & 0x1) {
1136 		wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
1137 					   RFKILL_TYPE_WLAN,
1138 					   &hp_wmi_rfkill_ops,
1139 					   (void *) HPWMI_WIFI);
1140 		if (!wifi_rfkill)
1141 			return -ENOMEM;
1142 		rfkill_init_sw_state(wifi_rfkill,
1143 				     hp_wmi_get_sw_state(HPWMI_WIFI));
1144 		rfkill_set_hw_state(wifi_rfkill,
1145 				    hp_wmi_get_hw_state(HPWMI_WIFI));
1146 		err = rfkill_register(wifi_rfkill);
1147 		if (err)
1148 			goto register_wifi_error;
1149 	}
1150 
1151 	if (wireless & 0x2) {
1152 		bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
1153 						RFKILL_TYPE_BLUETOOTH,
1154 						&hp_wmi_rfkill_ops,
1155 						(void *) HPWMI_BLUETOOTH);
1156 		if (!bluetooth_rfkill) {
1157 			err = -ENOMEM;
1158 			goto register_bluetooth_error;
1159 		}
1160 		rfkill_init_sw_state(bluetooth_rfkill,
1161 				     hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
1162 		rfkill_set_hw_state(bluetooth_rfkill,
1163 				    hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
1164 		err = rfkill_register(bluetooth_rfkill);
1165 		if (err)
1166 			goto register_bluetooth_error;
1167 	}
1168 
1169 	if (wireless & 0x4) {
1170 		wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
1171 					   RFKILL_TYPE_WWAN,
1172 					   &hp_wmi_rfkill_ops,
1173 					   (void *) HPWMI_WWAN);
1174 		if (!wwan_rfkill) {
1175 			err = -ENOMEM;
1176 			goto register_wwan_error;
1177 		}
1178 		rfkill_init_sw_state(wwan_rfkill,
1179 				     hp_wmi_get_sw_state(HPWMI_WWAN));
1180 		rfkill_set_hw_state(wwan_rfkill,
1181 				    hp_wmi_get_hw_state(HPWMI_WWAN));
1182 		err = rfkill_register(wwan_rfkill);
1183 		if (err)
1184 			goto register_wwan_error;
1185 	}
1186 
1187 	return 0;
1188 
1189 register_wwan_error:
1190 	rfkill_destroy(wwan_rfkill);
1191 	wwan_rfkill = NULL;
1192 	if (bluetooth_rfkill)
1193 		rfkill_unregister(bluetooth_rfkill);
1194 register_bluetooth_error:
1195 	rfkill_destroy(bluetooth_rfkill);
1196 	bluetooth_rfkill = NULL;
1197 	if (wifi_rfkill)
1198 		rfkill_unregister(wifi_rfkill);
1199 register_wifi_error:
1200 	rfkill_destroy(wifi_rfkill);
1201 	wifi_rfkill = NULL;
1202 	return err;
1203 }
1204 
hp_wmi_rfkill2_setup(struct platform_device * device)1205 static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
1206 {
1207 	struct bios_rfkill2_state state;
1208 	int err, i;
1209 
1210 	err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
1211 				   zero_if_sup(state), sizeof(state));
1212 	if (err)
1213 		return err < 0 ? err : -EINVAL;
1214 
1215 	if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
1216 		pr_warn("unable to parse 0x1b query output\n");
1217 		return -EINVAL;
1218 	}
1219 
1220 	for (i = 0; i < state.count; i++) {
1221 		struct rfkill *rfkill;
1222 		enum rfkill_type type;
1223 		char *name;
1224 
1225 		switch (state.device[i].radio_type) {
1226 		case HPWMI_WIFI:
1227 			type = RFKILL_TYPE_WLAN;
1228 			name = "hp-wifi";
1229 			break;
1230 		case HPWMI_BLUETOOTH:
1231 			type = RFKILL_TYPE_BLUETOOTH;
1232 			name = "hp-bluetooth";
1233 			break;
1234 		case HPWMI_WWAN:
1235 			type = RFKILL_TYPE_WWAN;
1236 			name = "hp-wwan";
1237 			break;
1238 		case HPWMI_GPS:
1239 			type = RFKILL_TYPE_GPS;
1240 			name = "hp-gps";
1241 			break;
1242 		default:
1243 			pr_warn("unknown device type 0x%x\n",
1244 				state.device[i].radio_type);
1245 			continue;
1246 		}
1247 
1248 		if (!state.device[i].vendor_id) {
1249 			pr_warn("zero device %d while %d reported\n",
1250 				i, state.count);
1251 			continue;
1252 		}
1253 
1254 		rfkill = rfkill_alloc(name, &device->dev, type,
1255 				      &hp_wmi_rfkill2_ops, (void *)(long)i);
1256 		if (!rfkill) {
1257 			err = -ENOMEM;
1258 			goto fail;
1259 		}
1260 
1261 		rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
1262 		rfkill2[rfkill2_count].num = i;
1263 		rfkill2[rfkill2_count].rfkill = rfkill;
1264 
1265 		rfkill_init_sw_state(rfkill,
1266 				     IS_SWBLOCKED(state.device[i].power));
1267 		rfkill_set_hw_state(rfkill,
1268 				    IS_HWBLOCKED(state.device[i].power));
1269 
1270 		if (!(state.device[i].power & HPWMI_POWER_BIOS))
1271 			pr_info("device %s blocked by BIOS\n", name);
1272 
1273 		err = rfkill_register(rfkill);
1274 		if (err) {
1275 			rfkill_destroy(rfkill);
1276 			goto fail;
1277 		}
1278 
1279 		rfkill2_count++;
1280 	}
1281 
1282 	return 0;
1283 fail:
1284 	for (; rfkill2_count > 0; rfkill2_count--) {
1285 		rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
1286 		rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
1287 	}
1288 	return err;
1289 }
1290 
platform_profile_omen_get_ec(enum platform_profile_option * profile)1291 static int platform_profile_omen_get_ec(enum platform_profile_option *profile)
1292 {
1293 	int tp;
1294 
1295 	tp = omen_thermal_profile_get();
1296 	if (tp < 0)
1297 		return tp;
1298 
1299 	switch (tp) {
1300 	case HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE:
1301 	case HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE:
1302 		*profile = PLATFORM_PROFILE_PERFORMANCE;
1303 		break;
1304 	case HP_OMEN_V0_THERMAL_PROFILE_DEFAULT:
1305 	case HP_OMEN_V1_THERMAL_PROFILE_DEFAULT:
1306 		*profile = PLATFORM_PROFILE_BALANCED;
1307 		break;
1308 	case HP_OMEN_V0_THERMAL_PROFILE_COOL:
1309 	case HP_OMEN_V1_THERMAL_PROFILE_COOL:
1310 		*profile = PLATFORM_PROFILE_COOL;
1311 		break;
1312 	default:
1313 		return -EINVAL;
1314 	}
1315 
1316 	return 0;
1317 }
1318 
platform_profile_omen_get(struct device * dev,enum platform_profile_option * profile)1319 static int platform_profile_omen_get(struct device *dev,
1320 				     enum platform_profile_option *profile)
1321 {
1322 	/*
1323 	 * We directly return the stored platform profile, as the embedded
1324 	 * controller will not accept switching to the performance option when
1325 	 * the conditions are not met (e.g. the laptop is not plugged in).
1326 	 *
1327 	 * If we directly return what the EC reports, the platform profile will
1328 	 * immediately "switch back" to normal mode, which is against the
1329 	 * expected behaviour from a userspace point of view, as described in
1330 	 * the Platform Profile Section page of the kernel documentation.
1331 	 *
1332 	 * See also omen_powersource_event.
1333 	 */
1334 	guard(mutex)(&active_platform_profile_lock);
1335 	*profile = active_platform_profile;
1336 
1337 	return 0;
1338 }
1339 
has_omen_thermal_profile_ec_timer(void)1340 static bool has_omen_thermal_profile_ec_timer(void)
1341 {
1342 	const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
1343 
1344 	if (!board_name)
1345 		return false;
1346 
1347 	return match_string(omen_timed_thermal_profile_boards,
1348 			    ARRAY_SIZE(omen_timed_thermal_profile_boards),
1349 			    board_name) >= 0;
1350 }
1351 
omen_thermal_profile_ec_flags_set(enum hp_thermal_profile_omen_flags flags)1352 inline int omen_thermal_profile_ec_flags_set(enum hp_thermal_profile_omen_flags flags)
1353 {
1354 	return ec_write(HP_OMEN_EC_THERMAL_PROFILE_FLAGS_OFFSET, flags);
1355 }
1356 
omen_thermal_profile_ec_timer_set(u8 value)1357 inline int omen_thermal_profile_ec_timer_set(u8 value)
1358 {
1359 	return ec_write(HP_OMEN_EC_THERMAL_PROFILE_TIMER_OFFSET, value);
1360 }
1361 
platform_profile_omen_set_ec(enum platform_profile_option profile)1362 static int platform_profile_omen_set_ec(enum platform_profile_option profile)
1363 {
1364 	int err, tp, tp_version;
1365 	enum hp_thermal_profile_omen_flags flags = 0;
1366 
1367 	tp_version = omen_get_thermal_policy_version();
1368 
1369 	if (tp_version < 0 || tp_version > 1)
1370 		return -EOPNOTSUPP;
1371 
1372 	switch (profile) {
1373 	case PLATFORM_PROFILE_PERFORMANCE:
1374 		if (tp_version == 0)
1375 			tp = HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE;
1376 		else
1377 			tp = HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE;
1378 		break;
1379 	case PLATFORM_PROFILE_BALANCED:
1380 		if (tp_version == 0)
1381 			tp = HP_OMEN_V0_THERMAL_PROFILE_DEFAULT;
1382 		else
1383 			tp = HP_OMEN_V1_THERMAL_PROFILE_DEFAULT;
1384 		break;
1385 	case PLATFORM_PROFILE_COOL:
1386 		if (tp_version == 0)
1387 			tp = HP_OMEN_V0_THERMAL_PROFILE_COOL;
1388 		else
1389 			tp = HP_OMEN_V1_THERMAL_PROFILE_COOL;
1390 		break;
1391 	default:
1392 		return -EOPNOTSUPP;
1393 	}
1394 
1395 	err = omen_thermal_profile_set(tp);
1396 	if (err < 0)
1397 		return err;
1398 
1399 	if (has_omen_thermal_profile_ec_timer()) {
1400 		err = omen_thermal_profile_ec_timer_set(0);
1401 		if (err < 0)
1402 			return err;
1403 
1404 		if (profile == PLATFORM_PROFILE_PERFORMANCE)
1405 			flags = HP_OMEN_EC_FLAGS_NOTIMER |
1406 				HP_OMEN_EC_FLAGS_TURBO;
1407 
1408 		err = omen_thermal_profile_ec_flags_set(flags);
1409 		if (err < 0)
1410 			return err;
1411 	}
1412 
1413 	return 0;
1414 }
1415 
platform_profile_omen_set(struct device * dev,enum platform_profile_option profile)1416 static int platform_profile_omen_set(struct device *dev,
1417 				     enum platform_profile_option profile)
1418 {
1419 	int err;
1420 
1421 	guard(mutex)(&active_platform_profile_lock);
1422 
1423 	err = platform_profile_omen_set_ec(profile);
1424 	if (err < 0)
1425 		return err;
1426 
1427 	active_platform_profile = profile;
1428 
1429 	return 0;
1430 }
1431 
thermal_profile_get(void)1432 static int thermal_profile_get(void)
1433 {
1434 	return hp_wmi_read_int(HPWMI_THERMAL_PROFILE_QUERY);
1435 }
1436 
thermal_profile_set(int thermal_profile)1437 static int thermal_profile_set(int thermal_profile)
1438 {
1439 	return hp_wmi_perform_query(HPWMI_THERMAL_PROFILE_QUERY, HPWMI_WRITE, &thermal_profile,
1440 							   sizeof(thermal_profile), 0);
1441 }
1442 
hp_wmi_platform_profile_get(struct device * dev,enum platform_profile_option * profile)1443 static int hp_wmi_platform_profile_get(struct device *dev,
1444 					enum platform_profile_option *profile)
1445 {
1446 	int tp;
1447 
1448 	tp = thermal_profile_get();
1449 	if (tp < 0)
1450 		return tp;
1451 
1452 	switch (tp) {
1453 	case HP_THERMAL_PROFILE_PERFORMANCE:
1454 		*profile =  PLATFORM_PROFILE_PERFORMANCE;
1455 		break;
1456 	case HP_THERMAL_PROFILE_DEFAULT:
1457 		*profile =  PLATFORM_PROFILE_BALANCED;
1458 		break;
1459 	case HP_THERMAL_PROFILE_COOL:
1460 		*profile =  PLATFORM_PROFILE_COOL;
1461 		break;
1462 	case HP_THERMAL_PROFILE_QUIET:
1463 		*profile = PLATFORM_PROFILE_QUIET;
1464 		break;
1465 	default:
1466 		return -EINVAL;
1467 	}
1468 
1469 	return 0;
1470 }
1471 
hp_wmi_platform_profile_set(struct device * dev,enum platform_profile_option profile)1472 static int hp_wmi_platform_profile_set(struct device *dev,
1473 					enum platform_profile_option profile)
1474 {
1475 	int err, tp;
1476 
1477 	switch (profile) {
1478 	case PLATFORM_PROFILE_PERFORMANCE:
1479 		tp =  HP_THERMAL_PROFILE_PERFORMANCE;
1480 		break;
1481 	case PLATFORM_PROFILE_BALANCED:
1482 		tp =  HP_THERMAL_PROFILE_DEFAULT;
1483 		break;
1484 	case PLATFORM_PROFILE_COOL:
1485 		tp =  HP_THERMAL_PROFILE_COOL;
1486 		break;
1487 	case PLATFORM_PROFILE_QUIET:
1488 		tp = HP_THERMAL_PROFILE_QUIET;
1489 		break;
1490 	default:
1491 		return -EOPNOTSUPP;
1492 	}
1493 
1494 	err = thermal_profile_set(tp);
1495 	if (err)
1496 		return err;
1497 
1498 	return 0;
1499 }
1500 
is_victus_thermal_profile(void)1501 static bool is_victus_thermal_profile(void)
1502 {
1503 	const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
1504 
1505 	if (!board_name)
1506 		return false;
1507 
1508 	return match_string(victus_thermal_profile_boards,
1509 			    ARRAY_SIZE(victus_thermal_profile_boards),
1510 			    board_name) >= 0;
1511 }
1512 
platform_profile_victus_get_ec(enum platform_profile_option * profile)1513 static int platform_profile_victus_get_ec(enum platform_profile_option *profile)
1514 {
1515 	int tp;
1516 
1517 	tp = omen_thermal_profile_get();
1518 	if (tp < 0)
1519 		return tp;
1520 
1521 	switch (tp) {
1522 	case HP_VICTUS_THERMAL_PROFILE_PERFORMANCE:
1523 		*profile = PLATFORM_PROFILE_PERFORMANCE;
1524 		break;
1525 	case HP_VICTUS_THERMAL_PROFILE_DEFAULT:
1526 		*profile = PLATFORM_PROFILE_BALANCED;
1527 		break;
1528 	case HP_VICTUS_THERMAL_PROFILE_QUIET:
1529 		*profile = PLATFORM_PROFILE_QUIET;
1530 		break;
1531 	default:
1532 		return -EOPNOTSUPP;
1533 	}
1534 
1535 	return 0;
1536 }
1537 
platform_profile_victus_get(struct device * dev,enum platform_profile_option * profile)1538 static int platform_profile_victus_get(struct device *dev,
1539 				       enum platform_profile_option *profile)
1540 {
1541 	/* Same behaviour as platform_profile_omen_get */
1542 	return platform_profile_omen_get(dev, profile);
1543 }
1544 
platform_profile_victus_set_ec(enum platform_profile_option profile)1545 static int platform_profile_victus_set_ec(enum platform_profile_option profile)
1546 {
1547 	int err, tp;
1548 
1549 	switch (profile) {
1550 	case PLATFORM_PROFILE_PERFORMANCE:
1551 		tp = HP_VICTUS_THERMAL_PROFILE_PERFORMANCE;
1552 		break;
1553 	case PLATFORM_PROFILE_BALANCED:
1554 		tp = HP_VICTUS_THERMAL_PROFILE_DEFAULT;
1555 		break;
1556 	case PLATFORM_PROFILE_QUIET:
1557 		tp = HP_VICTUS_THERMAL_PROFILE_QUIET;
1558 		break;
1559 	default:
1560 		return -EOPNOTSUPP;
1561 	}
1562 
1563 	err = omen_thermal_profile_set(tp);
1564 	if (err < 0)
1565 		return err;
1566 
1567 	return 0;
1568 }
1569 
is_victus_s_thermal_profile(void)1570 static bool is_victus_s_thermal_profile(void)
1571 {
1572 	const char *board_name;
1573 
1574 	board_name = dmi_get_system_info(DMI_BOARD_NAME);
1575 	if (!board_name)
1576 		return false;
1577 
1578 	return match_string(victus_s_thermal_profile_boards,
1579 			    ARRAY_SIZE(victus_s_thermal_profile_boards),
1580 			    board_name) >= 0;
1581 }
1582 
victus_s_gpu_thermal_profile_get(bool * ctgp_enable,bool * ppab_enable,u8 * dstate,u8 * gpu_slowdown_temp)1583 static int victus_s_gpu_thermal_profile_get(bool *ctgp_enable,
1584 					    bool *ppab_enable,
1585 					    u8 *dstate,
1586 					    u8 *gpu_slowdown_temp)
1587 {
1588 	struct victus_gpu_power_modes gpu_power_modes;
1589 	int ret;
1590 
1591 	ret = hp_wmi_perform_query(HPWMI_GET_GPU_THERMAL_MODES_QUERY, HPWMI_GM,
1592 				   &gpu_power_modes, sizeof(gpu_power_modes),
1593 				   sizeof(gpu_power_modes));
1594 	if (ret == 0) {
1595 		*ctgp_enable = gpu_power_modes.ctgp_enable ? true : false;
1596 		*ppab_enable = gpu_power_modes.ppab_enable ? true : false;
1597 		*dstate = gpu_power_modes.dstate;
1598 		*gpu_slowdown_temp = gpu_power_modes.gpu_slowdown_temp;
1599 	}
1600 
1601 	return ret;
1602 }
1603 
victus_s_gpu_thermal_profile_set(bool ctgp_enable,bool ppab_enable,u8 dstate)1604 static int victus_s_gpu_thermal_profile_set(bool ctgp_enable,
1605 					    bool ppab_enable,
1606 					    u8 dstate)
1607 {
1608 	struct victus_gpu_power_modes gpu_power_modes;
1609 	int ret;
1610 
1611 	bool current_ctgp_state, current_ppab_state;
1612 	u8 current_dstate, current_gpu_slowdown_temp;
1613 
1614 	/* Retrieving GPU slowdown temperature, in order to keep it unchanged */
1615 	ret = victus_s_gpu_thermal_profile_get(&current_ctgp_state,
1616 					       &current_ppab_state,
1617 					       &current_dstate,
1618 					       &current_gpu_slowdown_temp);
1619 	if (ret < 0) {
1620 		pr_warn("GPU modes not updated, unable to get slowdown temp\n");
1621 		return ret;
1622 	}
1623 
1624 	gpu_power_modes.ctgp_enable = ctgp_enable ? 0x01 : 0x00;
1625 	gpu_power_modes.ppab_enable = ppab_enable ? 0x01 : 0x00;
1626 	gpu_power_modes.dstate = dstate;
1627 	gpu_power_modes.gpu_slowdown_temp = current_gpu_slowdown_temp;
1628 
1629 
1630 	ret = hp_wmi_perform_query(HPWMI_SET_GPU_THERMAL_MODES_QUERY, HPWMI_GM,
1631 				   &gpu_power_modes, sizeof(gpu_power_modes), 0);
1632 
1633 	return ret;
1634 }
1635 
1636 /* Note: HP_POWER_LIMIT_DEFAULT can be used to restore default PL1 and PL2 */
victus_s_set_cpu_pl1_pl2(u8 pl1,u8 pl2)1637 static int victus_s_set_cpu_pl1_pl2(u8 pl1, u8 pl2)
1638 {
1639 	struct victus_power_limits power_limits;
1640 	int ret;
1641 
1642 	/* We need to know both PL1 and PL2 values in order to check them */
1643 	if (pl1 == HP_POWER_LIMIT_NO_CHANGE || pl2 == HP_POWER_LIMIT_NO_CHANGE)
1644 		return -EINVAL;
1645 
1646 	/* PL2 is not supposed to be lower than PL1 */
1647 	if (pl2 < pl1)
1648 		return -EINVAL;
1649 
1650 	power_limits.pl1 = pl1;
1651 	power_limits.pl2 = pl2;
1652 	power_limits.pl4 = HP_POWER_LIMIT_NO_CHANGE;
1653 	power_limits.cpu_gpu_concurrent_limit = HP_POWER_LIMIT_NO_CHANGE;
1654 
1655 	ret = hp_wmi_perform_query(HPWMI_SET_POWER_LIMITS_QUERY, HPWMI_GM,
1656 				   &power_limits, sizeof(power_limits), 0);
1657 
1658 	return ret;
1659 }
1660 
platform_profile_victus_s_set_ec(enum platform_profile_option profile)1661 static int platform_profile_victus_s_set_ec(enum platform_profile_option profile)
1662 {
1663 	bool gpu_ctgp_enable, gpu_ppab_enable;
1664 	u8 gpu_dstate; /* Test shows 1 = 100%, 2 = 50%, 3 = 25%, 4 = 12.5% */
1665 	int err, tp;
1666 
1667 	switch (profile) {
1668 	case PLATFORM_PROFILE_PERFORMANCE:
1669 		tp = HP_VICTUS_S_THERMAL_PROFILE_PERFORMANCE;
1670 		gpu_ctgp_enable = true;
1671 		gpu_ppab_enable = true;
1672 		gpu_dstate = 1;
1673 		break;
1674 	case PLATFORM_PROFILE_BALANCED:
1675 		tp = HP_VICTUS_S_THERMAL_PROFILE_DEFAULT;
1676 		gpu_ctgp_enable = false;
1677 		gpu_ppab_enable = true;
1678 		gpu_dstate = 1;
1679 		break;
1680 	case PLATFORM_PROFILE_LOW_POWER:
1681 		tp = HP_VICTUS_S_THERMAL_PROFILE_DEFAULT;
1682 		gpu_ctgp_enable = false;
1683 		gpu_ppab_enable = false;
1684 		gpu_dstate = 1;
1685 		break;
1686 	default:
1687 		return -EOPNOTSUPP;
1688 	}
1689 
1690 	hp_wmi_get_fan_count_userdefine_trigger();
1691 
1692 	err = omen_thermal_profile_set(tp);
1693 	if (err < 0) {
1694 		pr_err("Failed to set platform profile %d: %d\n", profile, err);
1695 		return err;
1696 	}
1697 
1698 	err = victus_s_gpu_thermal_profile_set(gpu_ctgp_enable,
1699 					       gpu_ppab_enable,
1700 					       gpu_dstate);
1701 	if (err < 0) {
1702 		pr_err("Failed to set GPU profile %d: %d\n", profile, err);
1703 		return err;
1704 	}
1705 
1706 	return 0;
1707 }
1708 
platform_profile_victus_s_set(struct device * dev,enum platform_profile_option profile)1709 static int platform_profile_victus_s_set(struct device *dev,
1710 					 enum platform_profile_option profile)
1711 {
1712 	int err;
1713 
1714 	guard(mutex)(&active_platform_profile_lock);
1715 
1716 	err = platform_profile_victus_s_set_ec(profile);
1717 	if (err < 0)
1718 		return err;
1719 
1720 	active_platform_profile = profile;
1721 
1722 	return 0;
1723 }
1724 
platform_profile_victus_set(struct device * dev,enum platform_profile_option profile)1725 static int platform_profile_victus_set(struct device *dev,
1726 				       enum platform_profile_option profile)
1727 {
1728 	int err;
1729 
1730 	guard(mutex)(&active_platform_profile_lock);
1731 
1732 	err = platform_profile_victus_set_ec(profile);
1733 	if (err < 0)
1734 		return err;
1735 
1736 	active_platform_profile = profile;
1737 
1738 	return 0;
1739 }
1740 
hp_wmi_platform_profile_probe(void * drvdata,unsigned long * choices)1741 static int hp_wmi_platform_profile_probe(void *drvdata, unsigned long *choices)
1742 {
1743 	if (is_omen_thermal_profile()) {
1744 		set_bit(PLATFORM_PROFILE_COOL, choices);
1745 	} else if (is_victus_thermal_profile()) {
1746 		set_bit(PLATFORM_PROFILE_QUIET, choices);
1747 	} else if (is_victus_s_thermal_profile()) {
1748 		/* Adding an equivalent to HP Omen software ECO mode: */
1749 		set_bit(PLATFORM_PROFILE_LOW_POWER, choices);
1750 	} else {
1751 		set_bit(PLATFORM_PROFILE_QUIET, choices);
1752 		set_bit(PLATFORM_PROFILE_COOL, choices);
1753 	}
1754 
1755 	set_bit(PLATFORM_PROFILE_BALANCED, choices);
1756 	set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
1757 
1758 	return 0;
1759 }
1760 
omen_powersource_event(struct notifier_block * nb,unsigned long value,void * data)1761 static int omen_powersource_event(struct notifier_block *nb,
1762 				  unsigned long value,
1763 				  void *data)
1764 {
1765 	struct acpi_bus_event *event_entry = data;
1766 	enum platform_profile_option actual_profile;
1767 	int err;
1768 
1769 	if (strcmp(event_entry->device_class, ACPI_AC_CLASS) != 0)
1770 		return NOTIFY_DONE;
1771 
1772 	pr_debug("Received power source device event\n");
1773 
1774 	guard(mutex)(&active_platform_profile_lock);
1775 
1776 	/*
1777 	 * This handler can only be called on Omen and Victus models, so
1778 	 * there's no need to call is_victus_thermal_profile() here.
1779 	 */
1780 	if (is_omen_thermal_profile())
1781 		err = platform_profile_omen_get_ec(&actual_profile);
1782 	else
1783 		err = platform_profile_victus_get_ec(&actual_profile);
1784 
1785 	if (err < 0) {
1786 		/*
1787 		 * Although we failed to get the current platform profile, we
1788 		 * still want the other event consumers to process it.
1789 		 */
1790 		pr_warn("Failed to read current platform profile (%d)\n", err);
1791 		return NOTIFY_DONE;
1792 	}
1793 
1794 	/*
1795 	 * If we're back on AC and that the user-chosen power profile is
1796 	 * different from what the EC reports, we restore the user-chosen
1797 	 * one.
1798 	 */
1799 	if (power_supply_is_system_supplied() <= 0 ||
1800 	    active_platform_profile == actual_profile) {
1801 		pr_debug("Platform profile update skipped, conditions unmet\n");
1802 		return NOTIFY_DONE;
1803 	}
1804 
1805 	if (is_omen_thermal_profile())
1806 		err = platform_profile_omen_set_ec(active_platform_profile);
1807 	else
1808 		err = platform_profile_victus_set_ec(active_platform_profile);
1809 
1810 	if (err < 0) {
1811 		pr_warn("Failed to restore platform profile (%d)\n", err);
1812 		return NOTIFY_DONE;
1813 	}
1814 
1815 	return NOTIFY_OK;
1816 }
1817 
victus_s_powersource_event(struct notifier_block * nb,unsigned long value,void * data)1818 static int victus_s_powersource_event(struct notifier_block *nb,
1819 				      unsigned long value,
1820 				      void *data)
1821 {
1822 	struct acpi_bus_event *event_entry = data;
1823 	int err;
1824 
1825 	if (strcmp(event_entry->device_class, ACPI_AC_CLASS) != 0)
1826 		return NOTIFY_DONE;
1827 
1828 	pr_debug("Received power source device event\n");
1829 
1830 	/*
1831 	 * Switching to battery power source while Performance mode is active
1832 	 * needs manual triggering of CPU power limits. Same goes when switching
1833 	 * to AC power source while Performance mode is active. Other modes
1834 	 * however are automatically behaving without any manual action.
1835 	 * Seen on HP 16-s1034nf (board 8C9C) with F.11 and F.13 BIOS versions.
1836 	 */
1837 
1838 	if (active_platform_profile == PLATFORM_PROFILE_PERFORMANCE) {
1839 		pr_debug("Triggering CPU PL1/PL2 actualization\n");
1840 		err = victus_s_set_cpu_pl1_pl2(HP_POWER_LIMIT_DEFAULT,
1841 					       HP_POWER_LIMIT_DEFAULT);
1842 		if (err)
1843 			pr_warn("Failed to actualize power limits: %d\n", err);
1844 
1845 		return NOTIFY_DONE;
1846 	}
1847 
1848 	return NOTIFY_OK;
1849 }
1850 
omen_register_powersource_event_handler(void)1851 static int omen_register_powersource_event_handler(void)
1852 {
1853 	int err;
1854 
1855 	platform_power_source_nb.notifier_call = omen_powersource_event;
1856 	err = register_acpi_notifier(&platform_power_source_nb);
1857 
1858 	if (err < 0) {
1859 		pr_warn("Failed to install ACPI power source notify handler\n");
1860 		return err;
1861 	}
1862 
1863 	return 0;
1864 }
1865 
victus_s_register_powersource_event_handler(void)1866 static int victus_s_register_powersource_event_handler(void)
1867 {
1868 	int err;
1869 
1870 	platform_power_source_nb.notifier_call = victus_s_powersource_event;
1871 	err = register_acpi_notifier(&platform_power_source_nb);
1872 	if (err < 0) {
1873 		pr_warn("Failed to install ACPI power source notify handler\n");
1874 		return err;
1875 	}
1876 
1877 	return 0;
1878 }
1879 
omen_unregister_powersource_event_handler(void)1880 static inline void omen_unregister_powersource_event_handler(void)
1881 {
1882 	unregister_acpi_notifier(&platform_power_source_nb);
1883 }
1884 
victus_s_unregister_powersource_event_handler(void)1885 static inline void victus_s_unregister_powersource_event_handler(void)
1886 {
1887 	unregister_acpi_notifier(&platform_power_source_nb);
1888 }
1889 
1890 static const struct platform_profile_ops platform_profile_omen_ops = {
1891 	.probe = hp_wmi_platform_profile_probe,
1892 	.profile_get = platform_profile_omen_get,
1893 	.profile_set = platform_profile_omen_set,
1894 };
1895 
1896 static const struct platform_profile_ops platform_profile_victus_ops = {
1897 	.probe = hp_wmi_platform_profile_probe,
1898 	.profile_get = platform_profile_victus_get,
1899 	.profile_set = platform_profile_victus_set,
1900 };
1901 
1902 static const struct platform_profile_ops platform_profile_victus_s_ops = {
1903 	.probe = hp_wmi_platform_profile_probe,
1904 	.profile_get = platform_profile_omen_get,
1905 	.profile_set = platform_profile_victus_s_set,
1906 };
1907 
1908 static const struct platform_profile_ops hp_wmi_platform_profile_ops = {
1909 	.probe = hp_wmi_platform_profile_probe,
1910 	.profile_get = hp_wmi_platform_profile_get,
1911 	.profile_set = hp_wmi_platform_profile_set,
1912 };
1913 
thermal_profile_setup(struct platform_device * device)1914 static int thermal_profile_setup(struct platform_device *device)
1915 {
1916 	const struct platform_profile_ops *ops;
1917 	int err, tp;
1918 
1919 	if (is_omen_thermal_profile()) {
1920 		err = platform_profile_omen_get_ec(&active_platform_profile);
1921 		if (err < 0)
1922 			return err;
1923 
1924 		/*
1925 		 * call thermal profile write command to ensure that the
1926 		 * firmware correctly sets the OEM variables
1927 		 */
1928 		err = platform_profile_omen_set_ec(active_platform_profile);
1929 		if (err < 0)
1930 			return err;
1931 
1932 		ops = &platform_profile_omen_ops;
1933 	} else if (is_victus_thermal_profile()) {
1934 		err = platform_profile_victus_get_ec(&active_platform_profile);
1935 		if (err < 0)
1936 			return err;
1937 
1938 		/*
1939 		 * call thermal profile write command to ensure that the
1940 		 * firmware correctly sets the OEM variables
1941 		 */
1942 		err = platform_profile_victus_set_ec(active_platform_profile);
1943 		if (err < 0)
1944 			return err;
1945 
1946 		ops = &platform_profile_victus_ops;
1947 	} else if (is_victus_s_thermal_profile()) {
1948 		/*
1949 		 * Being unable to retrieve laptop's current thermal profile,
1950 		 * during this setup, we set it to Balanced by default.
1951 		 */
1952 		active_platform_profile = PLATFORM_PROFILE_BALANCED;
1953 
1954 		err = platform_profile_victus_s_set_ec(active_platform_profile);
1955 		if (err < 0)
1956 			return err;
1957 
1958 		ops = &platform_profile_victus_s_ops;
1959 	} else {
1960 		tp = thermal_profile_get();
1961 
1962 		if (tp < 0)
1963 			return tp;
1964 
1965 		/*
1966 		 * call thermal profile write command to ensure that the
1967 		 * firmware correctly sets the OEM variables for the DPTF
1968 		 */
1969 		err = thermal_profile_set(tp);
1970 		if (err)
1971 			return err;
1972 
1973 		ops = &hp_wmi_platform_profile_ops;
1974 	}
1975 
1976 	platform_profile_device = devm_platform_profile_register(&device->dev, "hp-wmi",
1977 								 NULL, ops);
1978 	if (IS_ERR(platform_profile_device))
1979 		return PTR_ERR(platform_profile_device);
1980 
1981 	pr_info("Registered as platform profile handler\n");
1982 	platform_profile_support = true;
1983 
1984 	return 0;
1985 }
1986 
1987 static int hp_wmi_hwmon_init(void);
1988 
hp_wmi_bios_setup(struct platform_device * device)1989 static int __init hp_wmi_bios_setup(struct platform_device *device)
1990 {
1991 	int err;
1992 	/* clear detected rfkill devices */
1993 	wifi_rfkill = NULL;
1994 	bluetooth_rfkill = NULL;
1995 	wwan_rfkill = NULL;
1996 	rfkill2_count = 0;
1997 
1998 	/*
1999 	 * In pre-2009 BIOS, command 1Bh return 0x4 to indicate that
2000 	 * BIOS no longer controls the power for the wireless
2001 	 * devices. All features supported by this command will no
2002 	 * longer be supported.
2003 	 */
2004 	if (!hp_wmi_bios_2009_later()) {
2005 		if (hp_wmi_rfkill_setup(device))
2006 			hp_wmi_rfkill2_setup(device);
2007 	}
2008 
2009 	err = hp_wmi_hwmon_init();
2010 
2011 	if (err < 0)
2012 		return err;
2013 
2014 	thermal_profile_setup(device);
2015 
2016 	return 0;
2017 }
2018 
hp_wmi_bios_remove(struct platform_device * device)2019 static void __exit hp_wmi_bios_remove(struct platform_device *device)
2020 {
2021 	int i;
2022 
2023 	for (i = 0; i < rfkill2_count; i++) {
2024 		rfkill_unregister(rfkill2[i].rfkill);
2025 		rfkill_destroy(rfkill2[i].rfkill);
2026 	}
2027 
2028 	if (wifi_rfkill) {
2029 		rfkill_unregister(wifi_rfkill);
2030 		rfkill_destroy(wifi_rfkill);
2031 	}
2032 	if (bluetooth_rfkill) {
2033 		rfkill_unregister(bluetooth_rfkill);
2034 		rfkill_destroy(bluetooth_rfkill);
2035 	}
2036 	if (wwan_rfkill) {
2037 		rfkill_unregister(wwan_rfkill);
2038 		rfkill_destroy(wwan_rfkill);
2039 	}
2040 }
2041 
hp_wmi_resume_handler(struct device * device)2042 static int hp_wmi_resume_handler(struct device *device)
2043 {
2044 	/*
2045 	 * Hardware state may have changed while suspended, so trigger
2046 	 * input events for the current state. As this is a switch,
2047 	 * the input layer will only actually pass it on if the state
2048 	 * changed.
2049 	 */
2050 	if (hp_wmi_input_dev) {
2051 		if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
2052 			input_report_switch(hp_wmi_input_dev, SW_DOCK,
2053 					    hp_wmi_get_dock_state());
2054 		if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
2055 			input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
2056 					    hp_wmi_get_tablet_mode());
2057 		input_sync(hp_wmi_input_dev);
2058 	}
2059 
2060 	if (rfkill2_count)
2061 		hp_wmi_rfkill2_refresh();
2062 
2063 	if (wifi_rfkill)
2064 		rfkill_set_states(wifi_rfkill,
2065 				  hp_wmi_get_sw_state(HPWMI_WIFI),
2066 				  hp_wmi_get_hw_state(HPWMI_WIFI));
2067 	if (bluetooth_rfkill)
2068 		rfkill_set_states(bluetooth_rfkill,
2069 				  hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
2070 				  hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
2071 	if (wwan_rfkill)
2072 		rfkill_set_states(wwan_rfkill,
2073 				  hp_wmi_get_sw_state(HPWMI_WWAN),
2074 				  hp_wmi_get_hw_state(HPWMI_WWAN));
2075 
2076 	return 0;
2077 }
2078 
2079 static const struct dev_pm_ops hp_wmi_pm_ops = {
2080 	.resume  = hp_wmi_resume_handler,
2081 	.restore  = hp_wmi_resume_handler,
2082 };
2083 
2084 /*
2085  * hp_wmi_bios_remove() lives in .exit.text. For drivers registered via
2086  * module_platform_driver_probe() this is ok because they cannot get unbound at
2087  * runtime. So mark the driver struct with __refdata to prevent modpost
2088  * triggering a section mismatch warning.
2089  */
2090 static struct platform_driver hp_wmi_driver __refdata = {
2091 	.driver = {
2092 		.name = "hp-wmi",
2093 		.pm = &hp_wmi_pm_ops,
2094 		.dev_groups = hp_wmi_groups,
2095 	},
2096 	.remove = __exit_p(hp_wmi_bios_remove),
2097 };
2098 
hp_wmi_hwmon_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)2099 static umode_t hp_wmi_hwmon_is_visible(const void *data,
2100 				       enum hwmon_sensor_types type,
2101 				       u32 attr, int channel)
2102 {
2103 	switch (type) {
2104 	case hwmon_pwm:
2105 		return 0644;
2106 	case hwmon_fan:
2107 		if (is_victus_s_thermal_profile()) {
2108 			if (hp_wmi_get_fan_speed_victus_s(channel) >= 0)
2109 				return 0444;
2110 		} else {
2111 			if (hp_wmi_get_fan_speed(channel) >= 0)
2112 				return 0444;
2113 		}
2114 		break;
2115 	default:
2116 		return 0;
2117 	}
2118 
2119 	return 0;
2120 }
2121 
hp_wmi_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)2122 static int hp_wmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
2123 			     u32 attr, int channel, long *val)
2124 {
2125 	int ret;
2126 
2127 	switch (type) {
2128 	case hwmon_fan:
2129 		if (is_victus_s_thermal_profile())
2130 			ret = hp_wmi_get_fan_speed_victus_s(channel);
2131 		else
2132 			ret = hp_wmi_get_fan_speed(channel);
2133 		if (ret < 0)
2134 			return ret;
2135 		*val = ret;
2136 		return 0;
2137 	case hwmon_pwm:
2138 		switch (hp_wmi_fan_speed_max_get()) {
2139 		case 0:
2140 			/* 0 is automatic fan, which is 2 for hwmon */
2141 			*val = 2;
2142 			return 0;
2143 		case 1:
2144 			/* 1 is max fan, which is 0
2145 			 * (no fan speed control) for hwmon
2146 			 */
2147 			*val = 0;
2148 			return 0;
2149 		default:
2150 			/* shouldn't happen */
2151 			return -ENODATA;
2152 		}
2153 	default:
2154 		return -EINVAL;
2155 	}
2156 }
2157 
hp_wmi_hwmon_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)2158 static int hp_wmi_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
2159 			      u32 attr, int channel, long val)
2160 {
2161 	switch (type) {
2162 	case hwmon_pwm:
2163 		switch (val) {
2164 		case 0:
2165 			if (is_victus_s_thermal_profile())
2166 				hp_wmi_get_fan_count_userdefine_trigger();
2167 			/* 0 is no fan speed control (max), which is 1 for us */
2168 			return hp_wmi_fan_speed_max_set(1);
2169 		case 2:
2170 			/* 2 is automatic speed control, which is 0 for us */
2171 			if (is_victus_s_thermal_profile()) {
2172 				hp_wmi_get_fan_count_userdefine_trigger();
2173 				return hp_wmi_fan_speed_max_reset();
2174 			} else
2175 				return hp_wmi_fan_speed_max_set(0);
2176 		default:
2177 			/* we don't support manual fan speed control */
2178 			return -EINVAL;
2179 		}
2180 	default:
2181 		return -EOPNOTSUPP;
2182 	}
2183 }
2184 
2185 static const struct hwmon_channel_info * const info[] = {
2186 	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT, HWMON_F_INPUT),
2187 	HWMON_CHANNEL_INFO(pwm, HWMON_PWM_ENABLE),
2188 	NULL
2189 };
2190 
2191 static const struct hwmon_ops ops = {
2192 	.is_visible = hp_wmi_hwmon_is_visible,
2193 	.read = hp_wmi_hwmon_read,
2194 	.write = hp_wmi_hwmon_write,
2195 };
2196 
2197 static const struct hwmon_chip_info chip_info = {
2198 	.ops = &ops,
2199 	.info = info,
2200 };
2201 
hp_wmi_hwmon_init(void)2202 static int hp_wmi_hwmon_init(void)
2203 {
2204 	struct device *dev = &hp_wmi_platform_dev->dev;
2205 	struct device *hwmon;
2206 
2207 	hwmon = devm_hwmon_device_register_with_info(dev, "hp", &hp_wmi_driver,
2208 						     &chip_info, NULL);
2209 
2210 	if (IS_ERR(hwmon)) {
2211 		dev_err(dev, "Could not register hp hwmon device\n");
2212 		return PTR_ERR(hwmon);
2213 	}
2214 
2215 	return 0;
2216 }
2217 
hp_wmi_init(void)2218 static int __init hp_wmi_init(void)
2219 {
2220 	int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
2221 	int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
2222 	int err, tmp = 0;
2223 
2224 	if (!bios_capable && !event_capable)
2225 		return -ENODEV;
2226 
2227 	if (hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, HPWMI_READ, &tmp,
2228 				 sizeof(tmp), sizeof(tmp)) == HPWMI_RET_INVALID_PARAMETERS)
2229 		zero_insize_support = true;
2230 
2231 	if (event_capable) {
2232 		err = hp_wmi_input_setup();
2233 		if (err)
2234 			return err;
2235 	}
2236 
2237 	if (bios_capable) {
2238 		hp_wmi_platform_dev =
2239 			platform_device_register_simple("hp-wmi", PLATFORM_DEVID_NONE, NULL, 0);
2240 		if (IS_ERR(hp_wmi_platform_dev)) {
2241 			err = PTR_ERR(hp_wmi_platform_dev);
2242 			goto err_destroy_input;
2243 		}
2244 
2245 		err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
2246 		if (err)
2247 			goto err_unregister_device;
2248 	}
2249 
2250 	if (is_omen_thermal_profile() || is_victus_thermal_profile()) {
2251 		err = omen_register_powersource_event_handler();
2252 		if (err)
2253 			goto err_unregister_device;
2254 	} else if (is_victus_s_thermal_profile()) {
2255 		err = victus_s_register_powersource_event_handler();
2256 		if (err)
2257 			goto err_unregister_device;
2258 	}
2259 
2260 	return 0;
2261 
2262 err_unregister_device:
2263 	platform_device_unregister(hp_wmi_platform_dev);
2264 err_destroy_input:
2265 	if (event_capable)
2266 		hp_wmi_input_destroy();
2267 
2268 	return err;
2269 }
2270 module_init(hp_wmi_init);
2271 
hp_wmi_exit(void)2272 static void __exit hp_wmi_exit(void)
2273 {
2274 	if (is_omen_thermal_profile() || is_victus_thermal_profile())
2275 		omen_unregister_powersource_event_handler();
2276 
2277 	if (is_victus_s_thermal_profile())
2278 		victus_s_unregister_powersource_event_handler();
2279 
2280 	if (wmi_has_guid(HPWMI_EVENT_GUID))
2281 		hp_wmi_input_destroy();
2282 
2283 	if (camera_shutter_input_dev)
2284 		input_unregister_device(camera_shutter_input_dev);
2285 
2286 	if (hp_wmi_platform_dev) {
2287 		platform_device_unregister(hp_wmi_platform_dev);
2288 		platform_driver_unregister(&hp_wmi_driver);
2289 	}
2290 }
2291 module_exit(hp_wmi_exit);
2292