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