Debian Codegraph @

drivers/platform/x86/hp/hp-wmi.c

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