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