1 // SPDX-License-Identifier: GPL-2.0-or-later
8 * Portions based on wistron_btns.c:
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/platform_device.h>
24 #include <linux/platform_profile.h>
25 #include <linux/hwmon.h>
26 #include <linux/acpi.h>
27 #include <linux/mutex.h>
28 #include <linux/cleanup.h>
29 #include <linux/power_supply.h>
30 #include <linux/rfkill.h>
31 #include <linux/string.h>
32 #include <linux/dmi.h>
35 MODULE_DESCRIPTION("HP laptop WMI driver");
36 MODULE_LICENSE("GPL");
38 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
39 MODULE_ALIAS("wmi:5FB7F034-2C63-45E9-BE91-3D44E2C707E4");
41 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
42 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45E9-BE91-3D44E2C707E4"
44 #define HP_OMEN_EC_THERMAL_PROFILE_FLAGS_OFFSET 0x62
45 #define HP_OMEN_EC_THERMAL_PROFILE_TIMER_OFFSET 0x63
46 #define HP_OMEN_EC_THERMAL_PROFILE_OFFSET 0x95
48 #define HP_FAN_SPEED_AUTOMATIC 0x00
49 #define HP_POWER_LIMIT_DEFAULT 0x00
50 #define HP_POWER_LIMIT_NO_CHANGE 0xFF
52 #define ACPI_AC_CLASS "ac_adapter"
54 #define zero_if_sup(tmp) (zero_insize_support?0:sizeof(tmp)) // use when zero insize is required
56 /* DMI board names of devices that should use the omen specific path for
58 * This was obtained by taking a look in the windows omen command center
59 * app and parsing a json file that they use to figure out what capabilities
60 * the device should have.
61 * A device is considered an omen if the DisplayName in that list contains
62 * "OMEN", and it can use the thermal profile stuff if the "Feature" array
63 * contains "PerformanceControl".
65 static const char * const omen_thermal_profile_boards[] = {
66 "84DA", "84DB", "84DC", "8574", "8575", "860A", "87B5", "8572", "8573",
67 "8600", "8601", "8602", "8605", "8606", "8607", "8746", "8747", "8749",
68 "874A", "8603", "8604", "8748", "886B", "886C", "878A", "878B", "878C",
69 "88C8", "88CB", "8786", "8787", "8788", "88D1", "88D2", "88F4", "88FD",
70 "88F5", "88F6", "88F7", "88FE", "88FF", "8900", "8901", "8902", "8912",
71 "8917", "8918", "8949", "894A", "89EB", "8BAD", "8A42", "8A15"
74 /* DMI Board names of Omen laptops that are specifically set to be thermal
75 * profile version 0 by the Omen Command Center app, regardless of what
76 * the get system design information WMI call returns
78 static const char * const omen_thermal_profile_force_v0_boards[] = {
79 "8607", "8746", "8747", "8749", "874A", "8748"
82 /* DMI board names of Omen laptops that have a thermal profile timer which will
83 * cause the embedded controller to set the thermal profile back to
84 * "balanced" when reaching zero.
86 static const char * const omen_timed_thermal_profile_boards[] = {
87 "8BAD", "8A42", "8A15"
90 /* DMI Board names of Victus 16-d1xxx laptops */
91 static const char * const victus_thermal_profile_boards[] = {
95 /* DMI Board names of Victus 16-s1000 laptops */
96 static const char * const victus_s_thermal_profile_boards[] = {
102 HPWMI_BLUETOOTH = 0x1,
107 enum hp_wmi_event_ids {
108 HPWMI_DOCK_EVENT = 0x01,
109 HPWMI_PARK_HDD = 0x02,
110 HPWMI_SMART_ADAPTER = 0x03,
111 HPWMI_BEZEL_BUTTON = 0x04,
112 HPWMI_WIRELESS = 0x05,
113 HPWMI_CPU_BATTERY_THROTTLE = 0x06,
114 HPWMI_LOCK_SWITCH = 0x07,
115 HPWMI_LID_SWITCH = 0x08,
116 HPWMI_SCREEN_ROTATION = 0x09,
117 HPWMI_COOLSENSE_SYSTEM_MOBILE = 0x0A,
118 HPWMI_COOLSENSE_SYSTEM_HOT = 0x0B,
119 HPWMI_PROXIMITY_SENSOR = 0x0C,
120 HPWMI_BACKLIT_KB_BRIGHTNESS = 0x0D,
121 HPWMI_PEAKSHIFT_PERIOD = 0x0F,
122 HPWMI_BATTERY_CHARGE_PERIOD = 0x10,
123 HPWMI_SANITIZATION_MODE = 0x17,
124 HPWMI_CAMERA_TOGGLE = 0x1A,
125 HPWMI_OMEN_KEY = 0x1D,
126 HPWMI_SMART_EXPERIENCE_APP = 0x21,
130 * struct bios_args buffer is dynamically allocated. New WMI command types
131 * were introduced that exceeds 128-byte data size. Changes to handle
132 * the data size allocation scheme were kept in hp_wmi_perform_qurey function.
142 enum hp_wmi_commandtype {
143 HPWMI_DISPLAY_QUERY = 0x01,
144 HPWMI_HDDTEMP_QUERY = 0x02,
145 HPWMI_ALS_QUERY = 0x03,
146 HPWMI_HARDWARE_QUERY = 0x04,
147 HPWMI_WIRELESS_QUERY = 0x05,
148 HPWMI_BATTERY_QUERY = 0x07,
149 HPWMI_BIOS_QUERY = 0x09,
150 HPWMI_FEATURE_QUERY = 0x0b,
151 HPWMI_HOTKEY_QUERY = 0x0c,
152 HPWMI_FEATURE2_QUERY = 0x0d,
153 HPWMI_WIRELESS2_QUERY = 0x1b,
154 HPWMI_POSTCODEERROR_QUERY = 0x2a,
155 HPWMI_SYSTEM_DEVICE_MODE = 0x40,
156 HPWMI_THERMAL_PROFILE_QUERY = 0x4c,
159 struct victus_power_limits {
163 u8 cpu_gpu_concurrent_limit;
166 struct victus_gpu_power_modes {
170 u8 gpu_slowdown_temp;
173 enum hp_wmi_gm_commandtype {
174 HPWMI_FAN_SPEED_GET_QUERY = 0x11,
175 HPWMI_SET_PERFORMANCE_MODE = 0x1A,
176 HPWMI_FAN_SPEED_MAX_GET_QUERY = 0x26,
177 HPWMI_FAN_SPEED_MAX_SET_QUERY = 0x27,
178 HPWMI_GET_SYSTEM_DESIGN_DATA = 0x28,
179 HPWMI_FAN_COUNT_GET_QUERY = 0x10,
180 HPWMI_GET_GPU_THERMAL_MODES_QUERY = 0x21,
181 HPWMI_SET_GPU_THERMAL_MODES_QUERY = 0x22,
182 HPWMI_SET_POWER_LIMITS_QUERY = 0x29,
183 HPWMI_VICTUS_S_FAN_SPEED_GET_QUERY = 0x2D,
184 HPWMI_FAN_SPEED_SET_QUERY = 0x2E,
187 enum hp_wmi_command {
194 enum hp_wmi_hardware_mask {
195 HPWMI_DOCK_MASK = 0x01,
196 HPWMI_TABLET_MASK = 0x04,
204 enum hp_return_value {
205 HPWMI_RET_WRONG_SIGNATURE = 0x02,
206 HPWMI_RET_UNKNOWN_COMMAND = 0x03,
207 HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
208 HPWMI_RET_INVALID_PARAMETERS = 0x05,
211 enum hp_wireless2_bits {
212 HPWMI_POWER_STATE = 0x01,
213 HPWMI_POWER_SOFT = 0x02,
214 HPWMI_POWER_BIOS = 0x04,
215 HPWMI_POWER_HARD = 0x08,
216 HPWMI_POWER_FW_OR_HW = HPWMI_POWER_BIOS | HPWMI_POWER_HARD,
219 enum hp_thermal_profile_omen_v0 {
220 HP_OMEN_V0_THERMAL_PROFILE_DEFAULT = 0x00,
221 HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE = 0x01,
222 HP_OMEN_V0_THERMAL_PROFILE_COOL = 0x02,
225 enum hp_thermal_profile_omen_v1 {
226 HP_OMEN_V1_THERMAL_PROFILE_DEFAULT = 0x30,
227 HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE = 0x31,
228 HP_OMEN_V1_THERMAL_PROFILE_COOL = 0x50,
231 enum hp_thermal_profile_omen_flags {
232 HP_OMEN_EC_FLAGS_TURBO = 0x04,
233 HP_OMEN_EC_FLAGS_NOTIMER = 0x02,
234 HP_OMEN_EC_FLAGS_JUSTSET = 0x01,
237 enum hp_thermal_profile_victus {
238 HP_VICTUS_THERMAL_PROFILE_DEFAULT = 0x00,
239 HP_VICTUS_THERMAL_PROFILE_PERFORMANCE = 0x01,
240 HP_VICTUS_THERMAL_PROFILE_QUIET = 0x03,
243 enum hp_thermal_profile_victus_s {
244 HP_VICTUS_S_THERMAL_PROFILE_DEFAULT = 0x00,
245 HP_VICTUS_S_THERMAL_PROFILE_PERFORMANCE = 0x01,
248 enum hp_thermal_profile {
249 HP_THERMAL_PROFILE_PERFORMANCE = 0x00,
250 HP_THERMAL_PROFILE_DEFAULT = 0x01,
251 HP_THERMAL_PROFILE_COOL = 0x02,
252 HP_THERMAL_PROFILE_QUIET = 0x03,
255 #define IS_HWBLOCKED(x) ((x & HPWMI_POWER_FW_OR_HW) != HPWMI_POWER_FW_OR_HW)
256 #define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
258 struct bios_rfkill2_device_state {
263 u16 subsys_vendor_id;
264 u16 subsys_product_id;
270 /* 7 devices fit into the 128 byte buffer */
271 #define HPWMI_MAX_RFKILL2_DEVICES 7
273 struct bios_rfkill2_state {
277 struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
280 static const struct key_entry hp_wmi_keymap[] = {
281 { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
282 { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
283 { KE_KEY, 0x270, { KEY_MICMUTE } },
284 { KE_KEY, 0x20e6, { KEY_PROG1 } },
285 { KE_KEY, 0x20e8, { KEY_MEDIA } },
286 { KE_KEY, 0x2142, { KEY_MEDIA } },
287 { KE_KEY, 0x213b, { KEY_INFO } },
288 { KE_KEY, 0x2169, { KEY_ROTATE_DISPLAY } },
289 { KE_KEY, 0x216a, { KEY_SETUP } },
290 { KE_IGNORE, 0x21a4, }, /* Win Lock On */
291 { KE_IGNORE, 0x121a4, }, /* Win Lock Off */
292 { KE_KEY, 0x21a5, { KEY_PROG2 } }, /* HP Omen Key */
293 { KE_KEY, 0x21a7, { KEY_FN_ESC } },
294 { KE_KEY, 0x21a8, { KEY_PROG2 } }, /* HP Envy x360 programmable key */
295 { KE_KEY, 0x21a9, { KEY_TOUCHPAD_OFF } },
296 { KE_KEY, 0x121a9, { KEY_TOUCHPAD_ON } },
297 { KE_KEY, 0x231b, { KEY_HELP } },
302 * Mutex for the active_platform_profile variable,
303 * see omen_powersource_event.
305 static DEFINE_MUTEX(active_platform_profile_lock);
307 static struct input_dev *hp_wmi_input_dev;
308 static struct input_dev *camera_shutter_input_dev;
309 static struct platform_device *hp_wmi_platform_dev;
310 static struct device *platform_profile_device;
311 static struct notifier_block platform_power_source_nb;
312 static enum platform_profile_option active_platform_profile;
313 static bool platform_profile_support;
314 static bool zero_insize_support;
316 static struct rfkill *wifi_rfkill;
317 static struct rfkill *bluetooth_rfkill;
318 static struct rfkill *wwan_rfkill;
320 struct rfkill2_device {
323 struct rfkill *rfkill;
326 static int rfkill2_count;
327 static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
330 * Chassis Types values were obtained from SMBIOS reference
331 * specification version 3.00. A complete list of system enclosures
332 * and chassis types is available on Table 17.
334 static const char * const tablet_chassis_types[] = {
336 "31", /* Convertible */
337 "32" /* Detachable */
340 #define DEVICE_MODE_TABLET 0x06
342 /* map output size to the corresponding WMI method id */
343 static inline int encode_outsize_for_pvsz(int outsize)
359 * hp_wmi_perform_query
361 * query: The commandtype (enum hp_wmi_commandtype)
362 * write: The command (enum hp_wmi_command)
363 * buffer: Buffer used as input and/or output
364 * insize: Size of input buffer
365 * outsize: Size of output buffer
367 * returns zero on success
368 * an HP WMI query specific error code (which is positive)
369 * -EINVAL if the query was not successful at all
370 * -EINVAL if the output buffer size exceeds buffersize
372 * Note: The buffersize must at least be the maximum of the input and output
373 * size. E.g. Battery info query is defined to have 1 byte input
374 * and 128 byte output. The caller would do:
375 * buffer = kzalloc(128, GFP_KERNEL);
376 * ret = hp_wmi_perform_query(HPWMI_BATTERY_QUERY, HPWMI_READ, buffer, 1, 128)
378 static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
379 void *buffer, int insize, int outsize)
381 struct acpi_buffer input, output = { ACPI_ALLOCATE_BUFFER, NULL };
382 struct bios_return *bios_return;
383 union acpi_object *obj = NULL;
384 struct bios_args *args = NULL;
385 int mid, actual_insize, actual_outsize;
386 size_t bios_args_size;
389 mid = encode_outsize_for_pvsz(outsize);
390 if (WARN_ON(mid < 0))
393 actual_insize = max(insize, 128);
394 bios_args_size = struct_size(args, data, actual_insize);
395 args = kmalloc(bios_args_size, GFP_KERNEL);
399 input.length = bios_args_size;
400 input.pointer = args;
402 args->signature = 0x55434553;
403 args->command = command;
404 args->commandtype = query;
405 args->datasize = insize;
406 memcpy(args->data, buffer, flex_array_size(args, data, insize));
408 ret = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, mid, &input, &output);
412 obj = output.pointer;
418 if (obj->type != ACPI_TYPE_BUFFER) {
419 pr_warn("query 0x%x returned an invalid object 0x%x\n", query, ret);
424 bios_return = (struct bios_return *)obj->buffer.pointer;
425 ret = bios_return->return_code;
428 if (ret != HPWMI_RET_UNKNOWN_COMMAND &&
429 ret != HPWMI_RET_UNKNOWN_CMDTYPE)
430 pr_warn("query 0x%x returned error 0x%x\n", query, ret);
434 /* Ignore output data of zero size */
438 actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
439 memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
440 memset(buffer + actual_outsize, 0, outsize - actual_outsize);
449 * Calling this hp_wmi_get_fan_count_userdefine_trigger function also enables
450 * and/or maintains the laptop in user defined thermal and fan states, instead
451 * of using a fallback state. After a 120 seconds timeout however, the laptop
452 * goes back to its fallback state.
454 static int hp_wmi_get_fan_count_userdefine_trigger(void)
459 ret = hp_wmi_perform_query(HPWMI_FAN_COUNT_GET_QUERY, HPWMI_GM,
460 &fan_data, sizeof(u8),
465 return fan_data[0]; /* Others bytes aren't providing fan count */
468 static int hp_wmi_get_fan_speed(int fan)
471 char fan_data[4] = { fan, 0, 0, 0 };
473 int ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_GET_QUERY, HPWMI_GM,
474 &fan_data, sizeof(char),
483 return (fsh << 8) | fsl;
486 static int hp_wmi_get_fan_speed_victus_s(int fan)
488 u8 fan_data[128] = {};
491 if (fan < 0 || fan >= sizeof(fan_data))
494 ret = hp_wmi_perform_query(HPWMI_VICTUS_S_FAN_SPEED_GET_QUERY,
495 HPWMI_GM, &fan_data, sizeof(u8),
500 return fan_data[fan] * 100;
503 static int hp_wmi_read_int(int query)
507 ret = hp_wmi_perform_query(query, HPWMI_READ, &val,
508 zero_if_sup(val), sizeof(val));
511 return ret < 0 ? ret : -EINVAL;
516 static int hp_wmi_get_dock_state(void)
518 int state = hp_wmi_read_int(HPWMI_HARDWARE_QUERY);
523 return !!(state & HPWMI_DOCK_MASK);
526 static int hp_wmi_get_tablet_mode(void)
528 char system_device_mode[4] = { 0 };
529 const char *chassis_type;
533 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
537 tablet_found = match_string(tablet_chassis_types,
538 ARRAY_SIZE(tablet_chassis_types),
543 ret = hp_wmi_perform_query(HPWMI_SYSTEM_DEVICE_MODE, HPWMI_READ,
544 system_device_mode, zero_if_sup(system_device_mode),
545 sizeof(system_device_mode));
549 return system_device_mode[0] == DEVICE_MODE_TABLET;
552 static int omen_thermal_profile_set(int mode)
554 /* The Omen Control Center actively sets the first byte of the buffer to
555 * 255, so let's mimic this behaviour to be as close as possible to
556 * the original software.
558 char buffer[2] = {-1, mode};
561 ret = hp_wmi_perform_query(HPWMI_SET_PERFORMANCE_MODE, HPWMI_GM,
562 &buffer, sizeof(buffer), 0);
565 return ret < 0 ? ret : -EINVAL;
570 static bool is_omen_thermal_profile(void)
572 const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
577 return match_string(omen_thermal_profile_boards,
578 ARRAY_SIZE(omen_thermal_profile_boards),
582 static int omen_get_thermal_policy_version(void)
584 unsigned char buffer[8] = { 0 };
587 const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
590 int matches = match_string(omen_thermal_profile_force_v0_boards,
591 ARRAY_SIZE(omen_thermal_profile_force_v0_boards),
597 ret = hp_wmi_perform_query(HPWMI_GET_SYSTEM_DESIGN_DATA, HPWMI_GM,
598 &buffer, sizeof(buffer), sizeof(buffer));
601 return ret < 0 ? ret : -EINVAL;
606 static int omen_thermal_profile_get(void)
610 int ret = ec_read(HP_OMEN_EC_THERMAL_PROFILE_OFFSET, &data);
618 static int hp_wmi_fan_speed_max_set(int enabled)
622 ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_MAX_SET_QUERY, HPWMI_GM,
623 &enabled, sizeof(enabled), 0);
626 return ret < 0 ? ret : -EINVAL;
631 static int hp_wmi_fan_speed_reset(void)
633 u8 fan_speed[2] = { HP_FAN_SPEED_AUTOMATIC, HP_FAN_SPEED_AUTOMATIC };
636 ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_SET_QUERY, HPWMI_GM,
637 &fan_speed, sizeof(fan_speed), 0);
642 static int hp_wmi_fan_speed_max_reset(void)
646 ret = hp_wmi_fan_speed_max_set(0);
650 /* Disabling max fan speed on Victus s1xxx laptops needs a 2nd step: */
651 ret = hp_wmi_fan_speed_reset();
655 static int hp_wmi_fan_speed_max_get(void)
659 ret = hp_wmi_perform_query(HPWMI_FAN_SPEED_MAX_GET_QUERY, HPWMI_GM,
660 &val, zero_if_sup(val), sizeof(val));
663 return ret < 0 ? ret : -EINVAL;
668 static int __init hp_wmi_bios_2008_later(void)
671 int ret = hp_wmi_perform_query(HPWMI_FEATURE_QUERY, HPWMI_READ, &state,
672 zero_if_sup(state), sizeof(state));
676 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
679 static int __init hp_wmi_bios_2009_later(void)
682 int ret = hp_wmi_perform_query(HPWMI_FEATURE2_QUERY, HPWMI_READ, &state,
683 zero_if_sup(state), sizeof(state));
687 return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
690 static int __init hp_wmi_enable_hotkeys(void)
693 int ret = hp_wmi_perform_query(HPWMI_BIOS_QUERY, HPWMI_WRITE, &value,
696 return ret <= 0 ? ret : -EINVAL;
699 static int hp_wmi_set_block(void *data, bool blocked)
701 enum hp_wmi_radio r = (long)data;
702 int query = BIT(r + 8) | ((!blocked) << r);
705 ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE,
706 &query, sizeof(query), 0);
708 return ret <= 0 ? ret : -EINVAL;
711 static const struct rfkill_ops hp_wmi_rfkill_ops = {
712 .set_block = hp_wmi_set_block,
715 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
717 int mask = 0x200 << (r * 8);
719 int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
721 /* TBD: Pass error */
722 WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
724 return !(wireless & mask);
727 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
729 int mask = 0x800 << (r * 8);
731 int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
733 /* TBD: Pass error */
734 WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
736 return !(wireless & mask);
739 static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
741 int rfkill_id = (int)(long)data;
742 char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
745 ret = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_WRITE,
746 buffer, sizeof(buffer), 0);
748 return ret <= 0 ? ret : -EINVAL;
751 static const struct rfkill_ops hp_wmi_rfkill2_ops = {
752 .set_block = hp_wmi_rfkill2_set_block,
755 static int hp_wmi_rfkill2_refresh(void)
757 struct bios_rfkill2_state state;
760 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
761 zero_if_sup(state), sizeof(state));
765 for (i = 0; i < rfkill2_count; i++) {
766 int num = rfkill2[i].num;
767 struct bios_rfkill2_device_state *devstate;
769 devstate = &state.device[num];
771 if (num >= state.count ||
772 devstate->rfkill_id != rfkill2[i].id) {
773 pr_warn("power configuration of the wireless devices unexpectedly changed\n");
777 rfkill_set_states(rfkill2[i].rfkill,
778 IS_SWBLOCKED(devstate->power),
779 IS_HWBLOCKED(devstate->power));
785 static ssize_t display_show(struct device *dev, struct device_attribute *attr,
788 int value = hp_wmi_read_int(HPWMI_DISPLAY_QUERY);
792 return sysfs_emit(buf, "%d\n", value);
795 static ssize_t hddtemp_show(struct device *dev, struct device_attribute *attr,
798 int value = hp_wmi_read_int(HPWMI_HDDTEMP_QUERY);
802 return sysfs_emit(buf, "%d\n", value);
805 static ssize_t als_show(struct device *dev, struct device_attribute *attr,
808 int value = hp_wmi_read_int(HPWMI_ALS_QUERY);
812 return sysfs_emit(buf, "%d\n", value);
815 static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
818 int value = hp_wmi_get_dock_state();
822 return sysfs_emit(buf, "%d\n", value);
825 static ssize_t tablet_show(struct device *dev, struct device_attribute *attr,
828 int value = hp_wmi_get_tablet_mode();
832 return sysfs_emit(buf, "%d\n", value);
835 static ssize_t postcode_show(struct device *dev, struct device_attribute *attr,
838 /* Get the POST error code of previous boot failure. */
839 int value = hp_wmi_read_int(HPWMI_POSTCODEERROR_QUERY);
843 return sysfs_emit(buf, "0x%x\n", value);
846 static ssize_t als_store(struct device *dev, struct device_attribute *attr,
847 const char *buf, size_t count)
852 ret = kstrtou32(buf, 10, &tmp);
856 ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_WRITE, &tmp,
859 return ret < 0 ? ret : -EINVAL;
864 static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,
865 const char *buf, size_t count)
871 ret = kstrtobool(buf, &clear);
878 /* Clear the POST error code. It is kept until cleared. */
879 ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_WRITE, &tmp,
882 return ret < 0 ? ret : -EINVAL;
887 static int camera_shutter_input_setup(void)
891 camera_shutter_input_dev = input_allocate_device();
892 if (!camera_shutter_input_dev)
895 camera_shutter_input_dev->name = "HP WMI camera shutter";
896 camera_shutter_input_dev->phys = "wmi/input1";
897 camera_shutter_input_dev->id.bustype = BUS_HOST;
899 __set_bit(EV_SW, camera_shutter_input_dev->evbit);
900 __set_bit(SW_CAMERA_LENS_COVER, camera_shutter_input_dev->swbit);
902 err = input_register_device(camera_shutter_input_dev);
909 input_free_device(camera_shutter_input_dev);
910 camera_shutter_input_dev = NULL;
914 static DEVICE_ATTR_RO(display);
915 static DEVICE_ATTR_RO(hddtemp);
916 static DEVICE_ATTR_RW(als);
917 static DEVICE_ATTR_RO(dock);
918 static DEVICE_ATTR_RO(tablet);
919 static DEVICE_ATTR_RW(postcode);
921 static struct attribute *hp_wmi_attrs[] = {
922 &dev_attr_display.attr,
923 &dev_attr_hddtemp.attr,
926 &dev_attr_tablet.attr,
927 &dev_attr_postcode.attr,
930 ATTRIBUTE_GROUPS(hp_wmi);
932 static void hp_wmi_notify(union acpi_object *obj, void *context)
934 u32 event_id, event_data;
940 if (obj->type != ACPI_TYPE_BUFFER) {
941 pr_info("Unknown response received %d\n", obj->type);
946 * Depending on ACPI version the concatenation of id and event data
947 * inside _WED function will result in a 8 or 16 byte buffer.
949 location = (u32 *)obj->buffer.pointer;
950 if (obj->buffer.length == 8) {
951 event_id = *location;
952 event_data = *(location + 1);
953 } else if (obj->buffer.length == 16) {
954 event_id = *location;
955 event_data = *(location + 2);
957 pr_info("Unknown buffer length %d\n", obj->buffer.length);
962 case HPWMI_DOCK_EVENT:
963 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
964 input_report_switch(hp_wmi_input_dev, SW_DOCK,
965 hp_wmi_get_dock_state());
966 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
967 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
968 hp_wmi_get_tablet_mode());
969 input_sync(hp_wmi_input_dev);
973 case HPWMI_SMART_ADAPTER:
975 case HPWMI_BEZEL_BUTTON:
976 key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
980 if (!sparse_keymap_report_event(hp_wmi_input_dev,
982 pr_info("Unknown key code - 0x%x\n", key_code);
985 if (event_data) /* Only should be true for HP Omen */
986 key_code = event_data;
988 key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
990 if (!sparse_keymap_report_event(hp_wmi_input_dev,
992 pr_info("Unknown key code - 0x%x\n", key_code);
996 hp_wmi_rfkill2_refresh();
1001 rfkill_set_states(wifi_rfkill,
1002 hp_wmi_get_sw_state(HPWMI_WIFI),
1003 hp_wmi_get_hw_state(HPWMI_WIFI));
1004 if (bluetooth_rfkill)
1005 rfkill_set_states(bluetooth_rfkill,
1006 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
1007 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
1009 rfkill_set_states(wwan_rfkill,
1010 hp_wmi_get_sw_state(HPWMI_WWAN),
1011 hp_wmi_get_hw_state(HPWMI_WWAN));
1013 case HPWMI_CPU_BATTERY_THROTTLE:
1014 pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
1016 case HPWMI_LOCK_SWITCH:
1018 case HPWMI_LID_SWITCH:
1020 case HPWMI_SCREEN_ROTATION:
1022 case HPWMI_COOLSENSE_SYSTEM_MOBILE:
1024 case HPWMI_COOLSENSE_SYSTEM_HOT:
1026 case HPWMI_PROXIMITY_SENSOR:
1028 case HPWMI_BACKLIT_KB_BRIGHTNESS:
1030 case HPWMI_PEAKSHIFT_PERIOD:
1032 case HPWMI_BATTERY_CHARGE_PERIOD:
1034 case HPWMI_SANITIZATION_MODE:
1036 case HPWMI_CAMERA_TOGGLE:
1037 if (!camera_shutter_input_dev)
1038 if (camera_shutter_input_setup()) {
1039 pr_err("Failed to setup camera shutter input device\n");
1042 if (event_data == 0xff)
1043 input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 1);
1044 else if (event_data == 0xfe)
1045 input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 0);
1047 pr_warn("Unknown camera shutter state - 0x%x\n", event_data);
1048 input_sync(camera_shutter_input_dev);
1050 case HPWMI_SMART_EXPERIENCE_APP:
1053 pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
1058 static int __init hp_wmi_input_setup(void)
1063 hp_wmi_input_dev = input_allocate_device();
1064 if (!hp_wmi_input_dev)
1067 hp_wmi_input_dev->name = "HP WMI hotkeys";
1068 hp_wmi_input_dev->phys = "wmi/input0";
1069 hp_wmi_input_dev->id.bustype = BUS_HOST;
1071 __set_bit(EV_SW, hp_wmi_input_dev->evbit);
1074 val = hp_wmi_get_dock_state();
1076 __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
1077 input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
1081 val = hp_wmi_get_tablet_mode();
1083 __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
1084 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
1087 err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
1091 /* Set initial hardware state */
1092 input_sync(hp_wmi_input_dev);
1094 if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
1095 hp_wmi_enable_hotkeys();
1097 status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
1098 if (ACPI_FAILURE(status)) {
1103 err = input_register_device(hp_wmi_input_dev);
1105 goto err_uninstall_notifier;
1109 err_uninstall_notifier:
1110 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
1112 input_free_device(hp_wmi_input_dev);
1116 static void hp_wmi_input_destroy(void)
1118 wmi_remove_notify_handler(HPWMI_EVENT_GUID);
1119 input_unregister_device(hp_wmi_input_dev);
1122 static int __init hp_wmi_rfkill_setup(struct platform_device *device)
1126 wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
1130 err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE, &wireless,
1131 sizeof(wireless), 0);
1135 if (wireless & 0x1) {
1136 wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
1139 (void *) HPWMI_WIFI);
1142 rfkill_init_sw_state(wifi_rfkill,
1143 hp_wmi_get_sw_state(HPWMI_WIFI));
1144 rfkill_set_hw_state(wifi_rfkill,
1145 hp_wmi_get_hw_state(HPWMI_WIFI));
1146 err = rfkill_register(wifi_rfkill);
1148 goto register_wifi_error;
1151 if (wireless & 0x2) {
1152 bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
1153 RFKILL_TYPE_BLUETOOTH,
1155 (void *) HPWMI_BLUETOOTH);
1156 if (!bluetooth_rfkill) {
1158 goto register_bluetooth_error;
1160 rfkill_init_sw_state(bluetooth_rfkill,
1161 hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
1162 rfkill_set_hw_state(bluetooth_rfkill,
1163 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
1164 err = rfkill_register(bluetooth_rfkill);
1166 goto register_bluetooth_error;
1169 if (wireless & 0x4) {
1170 wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
1173 (void *) HPWMI_WWAN);
1176 goto register_wwan_error;
1178 rfkill_init_sw_state(wwan_rfkill,
1179 hp_wmi_get_sw_state(HPWMI_WWAN));
1180 rfkill_set_hw_state(wwan_rfkill,
1181 hp_wmi_get_hw_state(HPWMI_WWAN));
1182 err = rfkill_register(wwan_rfkill);
1184 goto register_wwan_error;
1189 register_wwan_error:
1190 rfkill_destroy(wwan_rfkill);
1192 if (bluetooth_rfkill)
1193 rfkill_unregister(bluetooth_rfkill);
1194 register_bluetooth_error:
1195 rfkill_destroy(bluetooth_rfkill);
1196 bluetooth_rfkill = NULL;
1198 rfkill_unregister(wifi_rfkill);
1199 register_wifi_error:
1200 rfkill_destroy(wifi_rfkill);
1205 static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
1207 struct bios_rfkill2_state state;
1210 err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
1211 zero_if_sup(state), sizeof(state));
1213 return err < 0 ? err : -EINVAL;
1215 if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
1216 pr_warn("unable to parse 0x1b query output\n");
1220 for (i = 0; i < state.count; i++) {
1221 struct rfkill *rfkill;
1222 enum rfkill_type type;
1225 switch (state.device[i].radio_type) {
1227 type = RFKILL_TYPE_WLAN;
1230 case HPWMI_BLUETOOTH:
1231 type = RFKILL_TYPE_BLUETOOTH;
1232 name = "hp-bluetooth";
1235 type = RFKILL_TYPE_WWAN;
1239 type = RFKILL_TYPE_GPS;
1243 pr_warn("unknown device type 0x%x\n",
1244 state.device[i].radio_type);
1248 if (!state.device[i].vendor_id) {
1249 pr_warn("zero device %d while %d reported\n",
1254 rfkill = rfkill_alloc(name, &device->dev, type,
1255 &hp_wmi_rfkill2_ops, (void *)(long)i);
1261 rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
1262 rfkill2[rfkill2_count].num = i;
1263 rfkill2[rfkill2_count].rfkill = rfkill;
1265 rfkill_init_sw_state(rfkill,
1266 IS_SWBLOCKED(state.device[i].power));
1267 rfkill_set_hw_state(rfkill,
1268 IS_HWBLOCKED(state.device[i].power));
1270 if (!(state.device[i].power & HPWMI_POWER_BIOS))
1271 pr_info("device %s blocked by BIOS\n", name);
1273 err = rfkill_register(rfkill);
1275 rfkill_destroy(rfkill);
1284 for (; rfkill2_count > 0; rfkill2_count--) {
1285 rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
1286 rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
1291 static int platform_profile_omen_get_ec(enum platform_profile_option *profile)
1295 tp = omen_thermal_profile_get();
1300 case HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE:
1301 case HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE:
1302 *profile = PLATFORM_PROFILE_PERFORMANCE;
1304 case HP_OMEN_V0_THERMAL_PROFILE_DEFAULT:
1305 case HP_OMEN_V1_THERMAL_PROFILE_DEFAULT:
1306 *profile = PLATFORM_PROFILE_BALANCED;
1308 case HP_OMEN_V0_THERMAL_PROFILE_COOL:
1309 case HP_OMEN_V1_THERMAL_PROFILE_COOL:
1310 *profile = PLATFORM_PROFILE_COOL;
1319 static int platform_profile_omen_get(struct device *dev,
1320 enum platform_profile_option *profile)
1323 * We directly return the stored platform profile, as the embedded
1324 * controller will not accept switching to the performance option when
1325 * the conditions are not met (e.g. the laptop is not plugged in).
1327 * If we directly return what the EC reports, the platform profile will
1328 * immediately "switch back" to normal mode, which is against the
1329 * expected behaviour from a userspace point of view, as described in
1330 * the Platform Profile Section page of the kernel documentation.
1332 * See also omen_powersource_event.
1334 guard(mutex)(&active_platform_profile_lock);
1335 *profile = active_platform_profile;
1340 static bool has_omen_thermal_profile_ec_timer(void)
1342 const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
1347 return match_string(omen_timed_thermal_profile_boards,
1348 ARRAY_SIZE(omen_timed_thermal_profile_boards),
1352 inline int omen_thermal_profile_ec_flags_set(enum hp_thermal_profile_omen_flags flags)
1354 return ec_write(HP_OMEN_EC_THERMAL_PROFILE_FLAGS_OFFSET, flags);
1357 inline int omen_thermal_profile_ec_timer_set(u8 value)
1359 return ec_write(HP_OMEN_EC_THERMAL_PROFILE_TIMER_OFFSET, value);
1362 static int platform_profile_omen_set_ec(enum platform_profile_option profile)
1364 int err, tp, tp_version;
1365 enum hp_thermal_profile_omen_flags flags = 0;
1367 tp_version = omen_get_thermal_policy_version();
1369 if (tp_version < 0 || tp_version > 1)
1373 case PLATFORM_PROFILE_PERFORMANCE:
1374 if (tp_version == 0)
1375 tp = HP_OMEN_V0_THERMAL_PROFILE_PERFORMANCE;
1377 tp = HP_OMEN_V1_THERMAL_PROFILE_PERFORMANCE;
1379 case PLATFORM_PROFILE_BALANCED:
1380 if (tp_version == 0)
1381 tp = HP_OMEN_V0_THERMAL_PROFILE_DEFAULT;
1383 tp = HP_OMEN_V1_THERMAL_PROFILE_DEFAULT;
1385 case PLATFORM_PROFILE_COOL:
1386 if (tp_version == 0)
1387 tp = HP_OMEN_V0_THERMAL_PROFILE_COOL;
1389 tp = HP_OMEN_V1_THERMAL_PROFILE_COOL;
1395 err = omen_thermal_profile_set(tp);
1399 if (has_omen_thermal_profile_ec_timer()) {
1400 err = omen_thermal_profile_ec_timer_set(0);
1404 if (profile == PLATFORM_PROFILE_PERFORMANCE)
1405 flags = HP_OMEN_EC_FLAGS_NOTIMER |
1406 HP_OMEN_EC_FLAGS_TURBO;
1408 err = omen_thermal_profile_ec_flags_set(flags);
1416 static int platform_profile_omen_set(struct device *dev,
1417 enum platform_profile_option profile)
1421 guard(mutex)(&active_platform_profile_lock);
1423 err = platform_profile_omen_set_ec(profile);
1427 active_platform_profile = profile;
1432 static int thermal_profile_get(void)
1434 return hp_wmi_read_int(HPWMI_THERMAL_PROFILE_QUERY);
1437 static int thermal_profile_set(int thermal_profile)
1439 return hp_wmi_perform_query(HPWMI_THERMAL_PROFILE_QUERY, HPWMI_WRITE, &thermal_profile,
1440 sizeof(thermal_profile), 0);
1443 static int hp_wmi_platform_profile_get(struct device *dev,
1444 enum platform_profile_option *profile)
1448 tp = thermal_profile_get();
1453 case HP_THERMAL_PROFILE_PERFORMANCE:
1454 *profile = PLATFORM_PROFILE_PERFORMANCE;
1456 case HP_THERMAL_PROFILE_DEFAULT:
1457 *profile = PLATFORM_PROFILE_BALANCED;
1459 case HP_THERMAL_PROFILE_COOL:
1460 *profile = PLATFORM_PROFILE_COOL;
1462 case HP_THERMAL_PROFILE_QUIET:
1463 *profile = PLATFORM_PROFILE_QUIET;
1472 static int hp_wmi_platform_profile_set(struct device *dev,
1473 enum platform_profile_option profile)
1478 case PLATFORM_PROFILE_PERFORMANCE:
1479 tp = HP_THERMAL_PROFILE_PERFORMANCE;
1481 case PLATFORM_PROFILE_BALANCED:
1482 tp = HP_THERMAL_PROFILE_DEFAULT;
1484 case PLATFORM_PROFILE_COOL:
1485 tp = HP_THERMAL_PROFILE_COOL;
1487 case PLATFORM_PROFILE_QUIET:
1488 tp = HP_THERMAL_PROFILE_QUIET;
1494 err = thermal_profile_set(tp);
1501 static bool is_victus_thermal_profile(void)
1503 const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
1508 return match_string(victus_thermal_profile_boards,
1509 ARRAY_SIZE(victus_thermal_profile_boards),
1513 static int platform_profile_victus_get_ec(enum platform_profile_option *profile)
1517 tp = omen_thermal_profile_get();
1522 case HP_VICTUS_THERMAL_PROFILE_PERFORMANCE:
1523 *profile = PLATFORM_PROFILE_PERFORMANCE;
1525 case HP_VICTUS_THERMAL_PROFILE_DEFAULT:
1526 *profile = PLATFORM_PROFILE_BALANCED;
1528 case HP_VICTUS_THERMAL_PROFILE_QUIET:
1529 *profile = PLATFORM_PROFILE_QUIET;
1538 static int platform_profile_victus_get(struct device *dev,
1539 enum platform_profile_option *profile)
1541 /* Same behaviour as platform_profile_omen_get */
1542 return platform_profile_omen_get(dev, profile);
1545 static int platform_profile_victus_set_ec(enum platform_profile_option profile)
1550 case PLATFORM_PROFILE_PERFORMANCE:
1551 tp = HP_VICTUS_THERMAL_PROFILE_PERFORMANCE;
1553 case PLATFORM_PROFILE_BALANCED:
1554 tp = HP_VICTUS_THERMAL_PROFILE_DEFAULT;
1556 case PLATFORM_PROFILE_QUIET:
1557 tp = HP_VICTUS_THERMAL_PROFILE_QUIET;
1563 err = omen_thermal_profile_set(tp);
1570 static bool is_victus_s_thermal_profile(void)
1572 const char *board_name;
1574 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1578 return match_string(victus_s_thermal_profile_boards,
1579 ARRAY_SIZE(victus_s_thermal_profile_boards),
1583 static int victus_s_gpu_thermal_profile_get(bool *ctgp_enable,
1586 u8 *gpu_slowdown_temp)
1588 struct victus_gpu_power_modes gpu_power_modes;
1591 ret = hp_wmi_perform_query(HPWMI_GET_GPU_THERMAL_MODES_QUERY, HPWMI_GM,
1592 &gpu_power_modes, sizeof(gpu_power_modes),
1593 sizeof(gpu_power_modes));
1595 *ctgp_enable = gpu_power_modes.ctgp_enable ? true : false;
1596 *ppab_enable = gpu_power_modes.ppab_enable ? true : false;
1597 *dstate = gpu_power_modes.dstate;
1598 *gpu_slowdown_temp = gpu_power_modes.gpu_slowdown_temp;
1604 static int victus_s_gpu_thermal_profile_set(bool ctgp_enable,
1608 struct victus_gpu_power_modes gpu_power_modes;
1611 bool current_ctgp_state, current_ppab_state;
1612 u8 current_dstate, current_gpu_slowdown_temp;
1614 /* Retrieving GPU slowdown temperature, in order to keep it unchanged */
1615 ret = victus_s_gpu_thermal_profile_get(¤t_ctgp_state,
1616 ¤t_ppab_state,
1618 ¤t_gpu_slowdown_temp);
1620 pr_warn("GPU modes not updated, unable to get slowdown temp\n");
1624 gpu_power_modes.ctgp_enable = ctgp_enable ? 0x01 : 0x00;
1625 gpu_power_modes.ppab_enable = ppab_enable ? 0x01 : 0x00;
1626 gpu_power_modes.dstate = dstate;
1627 gpu_power_modes.gpu_slowdown_temp = current_gpu_slowdown_temp;
1630 ret = hp_wmi_perform_query(HPWMI_SET_GPU_THERMAL_MODES_QUERY, HPWMI_GM,
1631 &gpu_power_modes, sizeof(gpu_power_modes), 0);
1636 /* Note: HP_POWER_LIMIT_DEFAULT can be used to restore default PL1 and PL2 */
1637 static int victus_s_set_cpu_pl1_pl2(u8 pl1, u8 pl2)
1639 struct victus_power_limits power_limits;
1642 /* We need to know both PL1 and PL2 values in order to check them */
1643 if (pl1 == HP_POWER_LIMIT_NO_CHANGE || pl2 == HP_POWER_LIMIT_NO_CHANGE)
1646 /* PL2 is not supposed to be lower than PL1 */
1650 power_limits.pl1 = pl1;
1651 power_limits.pl2 = pl2;
1652 power_limits.pl4 = HP_POWER_LIMIT_NO_CHANGE;
1653 power_limits.cpu_gpu_concurrent_limit = HP_POWER_LIMIT_NO_CHANGE;
1655 ret = hp_wmi_perform_query(HPWMI_SET_POWER_LIMITS_QUERY, HPWMI_GM,
1656 &power_limits, sizeof(power_limits), 0);
1661 static int platform_profile_victus_s_set_ec(enum platform_profile_option profile)
1663 bool gpu_ctgp_enable, gpu_ppab_enable;
1664 u8 gpu_dstate; /* Test shows 1 = 100%, 2 = 50%, 3 = 25%, 4 = 12.5% */
1668 case PLATFORM_PROFILE_PERFORMANCE:
1669 tp = HP_VICTUS_S_THERMAL_PROFILE_PERFORMANCE;
1670 gpu_ctgp_enable = true;
1671 gpu_ppab_enable = true;
1674 case PLATFORM_PROFILE_BALANCED:
1675 tp = HP_VICTUS_S_THERMAL_PROFILE_DEFAULT;
1676 gpu_ctgp_enable = false;
1677 gpu_ppab_enable = true;
1680 case PLATFORM_PROFILE_LOW_POWER:
1681 tp = HP_VICTUS_S_THERMAL_PROFILE_DEFAULT;
1682 gpu_ctgp_enable = false;
1683 gpu_ppab_enable = false;
1690 hp_wmi_get_fan_count_userdefine_trigger();
1692 err = omen_thermal_profile_set(tp);
1694 pr_err("Failed to set platform profile %d: %d\n", profile, err);
1698 err = victus_s_gpu_thermal_profile_set(gpu_ctgp_enable,
1702 pr_err("Failed to set GPU profile %d: %d\n", profile, err);
1709 static int platform_profile_victus_s_set(struct device *dev,
1710 enum platform_profile_option profile)
1714 guard(mutex)(&active_platform_profile_lock);
1716 err = platform_profile_victus_s_set_ec(profile);
1720 active_platform_profile = profile;
1725 static int platform_profile_victus_set(struct device *dev,
1726 enum platform_profile_option profile)
1730 guard(mutex)(&active_platform_profile_lock);
1732 err = platform_profile_victus_set_ec(profile);
1736 active_platform_profile = profile;
1741 static int hp_wmi_platform_profile_probe(void *drvdata, unsigned long *choices)
1743 if (is_omen_thermal_profile()) {
1744 set_bit(PLATFORM_PROFILE_COOL, choices);
1745 } else if (is_victus_thermal_profile()) {
1746 set_bit(PLATFORM_PROFILE_QUIET, choices);
1747 } else if (is_victus_s_thermal_profile()) {
1748 /* Adding an equivalent to HP Omen software ECO mode: */
1749 set_bit(PLATFORM_PROFILE_LOW_POWER, choices);
1751 set_bit(PLATFORM_PROFILE_QUIET, choices);
1752 set_bit(PLATFORM_PROFILE_COOL, choices);
1755 set_bit(PLATFORM_PROFILE_BALANCED, choices);
1756 set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
1761 static int omen_powersource_event(struct notifier_block *nb,
1762 unsigned long value,
1765 struct acpi_bus_event *event_entry = data;
1766 enum platform_profile_option actual_profile;
1769 if (strcmp(event_entry->device_class, ACPI_AC_CLASS) != 0)
1772 pr_debug("Received power source device event\n");
1774 guard(mutex)(&active_platform_profile_lock);
1777 * This handler can only be called on Omen and Victus models, so
1778 * there's no need to call is_victus_thermal_profile() here.
1780 if (is_omen_thermal_profile())
1781 err = platform_profile_omen_get_ec(&actual_profile);
1783 err = platform_profile_victus_get_ec(&actual_profile);
1787 * Although we failed to get the current platform profile, we
1788 * still want the other event consumers to process it.
1790 pr_warn("Failed to read current platform profile (%d)\n", err);
1795 * If we're back on AC and that the user-chosen power profile is
1796 * different from what the EC reports, we restore the user-chosen
1799 if (power_supply_is_system_supplied() <= 0 ||
1800 active_platform_profile == actual_profile) {
1801 pr_debug("Platform profile update skipped, conditions unmet\n");
1805 if (is_omen_thermal_profile())
1806 err = platform_profile_omen_set_ec(active_platform_profile);
1808 err = platform_profile_victus_set_ec(active_platform_profile);
1811 pr_warn("Failed to restore platform profile (%d)\n", err);
1818 static int victus_s_powersource_event(struct notifier_block *nb,
1819 unsigned long value,
1822 struct acpi_bus_event *event_entry = data;
1825 if (strcmp(event_entry->device_class, ACPI_AC_CLASS) != 0)
1828 pr_debug("Received power source device event\n");
1831 * Switching to battery power source while Performance mode is active
1832 * needs manual triggering of CPU power limits. Same goes when switching
1833 * to AC power source while Performance mode is active. Other modes
1834 * however are automatically behaving without any manual action.
1835 * Seen on HP 16-s1034nf (board 8C9C) with F.11 and F.13 BIOS versions.
1838 if (active_platform_profile == PLATFORM_PROFILE_PERFORMANCE) {
1839 pr_debug("Triggering CPU PL1/PL2 actualization\n");
1840 err = victus_s_set_cpu_pl1_pl2(HP_POWER_LIMIT_DEFAULT,
1841 HP_POWER_LIMIT_DEFAULT);
1843 pr_warn("Failed to actualize power limits: %d\n", err);
1851 static int omen_register_powersource_event_handler(void)
1855 platform_power_source_nb.notifier_call = omen_powersource_event;
1856 err = register_acpi_notifier(&platform_power_source_nb);
1859 pr_warn("Failed to install ACPI power source notify handler\n");
1866 static int victus_s_register_powersource_event_handler(void)
1870 platform_power_source_nb.notifier_call = victus_s_powersource_event;
1871 err = register_acpi_notifier(&platform_power_source_nb);
1873 pr_warn("Failed to install ACPI power source notify handler\n");
1880 static inline void omen_unregister_powersource_event_handler(void)
1882 unregister_acpi_notifier(&platform_power_source_nb);
1885 static inline void victus_s_unregister_powersource_event_handler(void)
1887 unregister_acpi_notifier(&platform_power_source_nb);
1890 static const struct platform_profile_ops platform_profile_omen_ops = {
1891 .probe = hp_wmi_platform_profile_probe,
1892 .profile_get = platform_profile_omen_get,
1893 .profile_set = platform_profile_omen_set,
1896 static const struct platform_profile_ops platform_profile_victus_ops = {
1897 .probe = hp_wmi_platform_profile_probe,
1898 .profile_get = platform_profile_victus_get,
1899 .profile_set = platform_profile_victus_set,
1902 static const struct platform_profile_ops platform_profile_victus_s_ops = {
1903 .probe = hp_wmi_platform_profile_probe,
1904 .profile_get = platform_profile_omen_get,
1905 .profile_set = platform_profile_victus_s_set,
1908 static const struct platform_profile_ops hp_wmi_platform_profile_ops = {
1909 .probe = hp_wmi_platform_profile_probe,
1910 .profile_get = hp_wmi_platform_profile_get,
1911 .profile_set = hp_wmi_platform_profile_set,
1914 static int thermal_profile_setup(struct platform_device *device)
1916 const struct platform_profile_ops *ops;
1919 if (is_omen_thermal_profile()) {
1920 err = platform_profile_omen_get_ec(&active_platform_profile);
1925 * call thermal profile write command to ensure that the
1926 * firmware correctly sets the OEM variables
1928 err = platform_profile_omen_set_ec(active_platform_profile);
1932 ops = &platform_profile_omen_ops;
1933 } else if (is_victus_thermal_profile()) {
1934 err = platform_profile_victus_get_ec(&active_platform_profile);
1939 * call thermal profile write command to ensure that the
1940 * firmware correctly sets the OEM variables
1942 err = platform_profile_victus_set_ec(active_platform_profile);
1946 ops = &platform_profile_victus_ops;
1947 } else if (is_victus_s_thermal_profile()) {
1949 * Being unable to retrieve laptop's current thermal profile,
1950 * during this setup, we set it to Balanced by default.
1952 active_platform_profile = PLATFORM_PROFILE_BALANCED;
1954 err = platform_profile_victus_s_set_ec(active_platform_profile);
1958 ops = &platform_profile_victus_s_ops;
1960 tp = thermal_profile_get();
1966 * call thermal profile write command to ensure that the
1967 * firmware correctly sets the OEM variables for the DPTF
1969 err = thermal_profile_set(tp);
1973 ops = &hp_wmi_platform_profile_ops;
1976 platform_profile_device = devm_platform_profile_register(&device->dev, "hp-wmi",
1978 if (IS_ERR(platform_profile_device))
1979 return PTR_ERR(platform_profile_device);
1981 pr_info("Registered as platform profile handler\n");
1982 platform_profile_support = true;
1987 static int hp_wmi_hwmon_init(void);
1989 static int __init hp_wmi_bios_setup(struct platform_device *device)
1992 /* clear detected rfkill devices */
1994 bluetooth_rfkill = NULL;
1999 * In pre-2009 BIOS, command 1Bh return 0x4 to indicate that
2000 * BIOS no longer controls the power for the wireless
2001 * devices. All features supported by this command will no
2002 * longer be supported.
2004 if (!hp_wmi_bios_2009_later()) {
2005 if (hp_wmi_rfkill_setup(device))
2006 hp_wmi_rfkill2_setup(device);
2009 err = hp_wmi_hwmon_init();
2014 thermal_profile_setup(device);
2019 static void __exit hp_wmi_bios_remove(struct platform_device *device)
2023 for (i = 0; i < rfkill2_count; i++) {
2024 rfkill_unregister(rfkill2[i].rfkill);
2025 rfkill_destroy(rfkill2[i].rfkill);
2029 rfkill_unregister(wifi_rfkill);
2030 rfkill_destroy(wifi_rfkill);
2032 if (bluetooth_rfkill) {
2033 rfkill_unregister(bluetooth_rfkill);
2034 rfkill_destroy(bluetooth_rfkill);
2037 rfkill_unregister(wwan_rfkill);
2038 rfkill_destroy(wwan_rfkill);
2042 static int hp_wmi_resume_handler(struct device *device)
2045 * Hardware state may have changed while suspended, so trigger
2046 * input events for the current state. As this is a switch,
2047 * the input layer will only actually pass it on if the state
2050 if (hp_wmi_input_dev) {
2051 if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
2052 input_report_switch(hp_wmi_input_dev, SW_DOCK,
2053 hp_wmi_get_dock_state());
2054 if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
2055 input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
2056 hp_wmi_get_tablet_mode());
2057 input_sync(hp_wmi_input_dev);
2061 hp_wmi_rfkill2_refresh();
2064 rfkill_set_states(wifi_rfkill,
2065 hp_wmi_get_sw_state(HPWMI_WIFI),
2066 hp_wmi_get_hw_state(HPWMI_WIFI));
2067 if (bluetooth_rfkill)
2068 rfkill_set_states(bluetooth_rfkill,
2069 hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
2070 hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
2072 rfkill_set_states(wwan_rfkill,
2073 hp_wmi_get_sw_state(HPWMI_WWAN),
2074 hp_wmi_get_hw_state(HPWMI_WWAN));
2079 static const struct dev_pm_ops hp_wmi_pm_ops = {
2080 .resume = hp_wmi_resume_handler,
2081 .restore = hp_wmi_resume_handler,
2085 * hp_wmi_bios_remove() lives in .exit.text. For drivers registered via
2086 * module_platform_driver_probe() this is ok because they cannot get unbound at
2087 * runtime. So mark the driver struct with __refdata to prevent modpost
2088 * triggering a section mismatch warning.
2090 static struct platform_driver hp_wmi_driver __refdata = {
2093 .pm = &hp_wmi_pm_ops,
2094 .dev_groups = hp_wmi_groups,
2096 .remove = __exit_p(hp_wmi_bios_remove),
2099 static umode_t hp_wmi_hwmon_is_visible(const void *data,
2100 enum hwmon_sensor_types type,
2101 u32 attr, int channel)
2107 if (is_victus_s_thermal_profile()) {
2108 if (hp_wmi_get_fan_speed_victus_s(channel) >= 0)
2111 if (hp_wmi_get_fan_speed(channel) >= 0)
2122 static int hp_wmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
2123 u32 attr, int channel, long *val)
2129 if (is_victus_s_thermal_profile())
2130 ret = hp_wmi_get_fan_speed_victus_s(channel);
2132 ret = hp_wmi_get_fan_speed(channel);
2138 switch (hp_wmi_fan_speed_max_get()) {
2140 /* 0 is automatic fan, which is 2 for hwmon */
2144 /* 1 is max fan, which is 0
2145 * (no fan speed control) for hwmon
2150 /* shouldn't happen */
2158 static int hp_wmi_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
2159 u32 attr, int channel, long val)
2165 if (is_victus_s_thermal_profile())
2166 hp_wmi_get_fan_count_userdefine_trigger();
2167 /* 0 is no fan speed control (max), which is 1 for us */
2168 return hp_wmi_fan_speed_max_set(1);
2170 /* 2 is automatic speed control, which is 0 for us */
2171 if (is_victus_s_thermal_profile()) {
2172 hp_wmi_get_fan_count_userdefine_trigger();
2173 return hp_wmi_fan_speed_max_reset();
2175 return hp_wmi_fan_speed_max_set(0);
2177 /* we don't support manual fan speed control */
2185 static const struct hwmon_channel_info * const info[] = {
2186 HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT, HWMON_F_INPUT),
2187 HWMON_CHANNEL_INFO(pwm, HWMON_PWM_ENABLE),
2191 static const struct hwmon_ops ops = {
2192 .is_visible = hp_wmi_hwmon_is_visible,
2193 .read = hp_wmi_hwmon_read,
2194 .write = hp_wmi_hwmon_write,
2197 static const struct hwmon_chip_info chip_info = {
2202 static int hp_wmi_hwmon_init(void)
2204 struct device *dev = &hp_wmi_platform_dev->dev;
2205 struct device *hwmon;
2207 hwmon = devm_hwmon_device_register_with_info(dev, "hp", &hp_wmi_driver,
2210 if (IS_ERR(hwmon)) {
2211 dev_err(dev, "Could not register hp hwmon device\n");
2212 return PTR_ERR(hwmon);
2218 static int __init hp_wmi_init(void)
2220 int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
2221 int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
2224 if (!bios_capable && !event_capable)
2227 if (hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, HPWMI_READ, &tmp,
2228 sizeof(tmp), sizeof(tmp)) == HPWMI_RET_INVALID_PARAMETERS)
2229 zero_insize_support = true;
2231 if (event_capable) {
2232 err = hp_wmi_input_setup();
2238 hp_wmi_platform_dev =
2239 platform_device_register_simple("hp-wmi", PLATFORM_DEVID_NONE, NULL, 0);
2240 if (IS_ERR(hp_wmi_platform_dev)) {
2241 err = PTR_ERR(hp_wmi_platform_dev);
2242 goto err_destroy_input;
2245 err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
2247 goto err_unregister_device;
2250 if (is_omen_thermal_profile() || is_victus_thermal_profile()) {
2251 err = omen_register_powersource_event_handler();
2253 goto err_unregister_device;
2254 } else if (is_victus_s_thermal_profile()) {
2255 err = victus_s_register_powersource_event_handler();
2257 goto err_unregister_device;
2262 err_unregister_device:
2263 platform_device_unregister(hp_wmi_platform_dev);
2266 hp_wmi_input_destroy();
2270 module_init(hp_wmi_init);
2272 static void __exit hp_wmi_exit(void)
2274 if (is_omen_thermal_profile() || is_victus_thermal_profile())
2275 omen_unregister_powersource_event_handler();
2277 if (is_victus_s_thermal_profile())
2278 victus_s_unregister_powersource_event_handler();
2280 if (wmi_has_guid(HPWMI_EVENT_GUID))
2281 hp_wmi_input_destroy();
2283 if (camera_shutter_input_dev)
2284 input_unregister_device(camera_shutter_input_dev);
2286 if (hp_wmi_platform_dev) {
2287 platform_device_unregister(hp_wmi_platform_dev);
2288 platform_driver_unregister(&hp_wmi_driver);
2291 module_exit(hp_wmi_exit);