1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
5 * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org>
8 * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de>
9 * Copyright (C) 2013, 2014 Guenter Roeck <linux@roeck-us.net>
10 * Copyright (C) 2014, 2015 Pali Rohár <pali@kernel.org>
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/acpi.h>
16 #include <linux/capability.h>
17 #include <linux/cpu.h>
18 #include <linux/ctype.h>
19 #include <linux/delay.h>
20 #include <linux/dmi.h>
21 #include <linux/err.h>
22 #include <linux/errno.h>
23 #include <linux/hwmon.h>
24 #include <linux/init.h>
25 #include <linux/kconfig.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/mutex.h>
29 #include <linux/platform_device.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/slab.h>
33 #include <linux/smp.h>
34 #include <linux/string.h>
35 #include <linux/thermal.h>
36 #include <linux/types.h>
37 #include <linux/uaccess.h>
38 #include <linux/wmi.h>
40 #include <linux/i8k.h>
41 #include <asm/unaligned.h>
43 #define I8K_SMM_FN_STATUS 0x0025
44 #define I8K_SMM_POWER_STATUS 0x0069
45 #define I8K_SMM_SET_FAN 0x01a3
46 #define I8K_SMM_GET_FAN 0x00a3
47 #define I8K_SMM_GET_SPEED 0x02a3
48 #define I8K_SMM_GET_FAN_TYPE 0x03a3
49 #define I8K_SMM_GET_NOM_SPEED 0x04a3
50 #define I8K_SMM_GET_TEMP 0x10a3
51 #define I8K_SMM_GET_TEMP_TYPE 0x11a3
52 #define I8K_SMM_GET_DELL_SIG1 0xfea3
53 #define I8K_SMM_GET_DELL_SIG2 0xffa3
56 #define DELL_SMM_MAX_DURATION 250000
58 #define I8K_FAN_MULT 30
59 #define I8K_FAN_RPM_THRESHOLD 1000
60 #define I8K_MAX_TEMP 127
62 #define I8K_FN_NONE 0x00
63 #define I8K_FN_UP 0x01
64 #define I8K_FN_DOWN 0x02
65 #define I8K_FN_MUTE 0x04
66 #define I8K_FN_MASK 0x07
67 #define I8K_FN_SHIFT 8
69 #define I8K_POWER_AC 0x05
70 #define I8K_POWER_BATTERY 0x01
72 #define DELL_SMM_WMI_GUID "F1DDEE52-063C-4784-A11E-8A06684B9B01"
73 #define DELL_SMM_LEGACY_EXECUTE 0x1
75 #define DELL_SMM_NO_TEMP 10
76 #define DELL_SMM_NO_FANS 3
88 struct device *smm_dev;
89 int (*smm_call)(struct device *smm_dev, struct smm_regs *regs);
92 struct dell_smm_data {
93 struct mutex i8k_mutex; /* lock for sensors writes */
95 char bios_machineid[16];
99 int temp_type[DELL_SMM_NO_TEMP];
100 bool fan[DELL_SMM_NO_FANS];
101 int fan_type[DELL_SMM_NO_FANS];
102 int *fan_nominal_speed[DELL_SMM_NO_FANS];
103 const struct dell_smm_ops *ops;
106 struct dell_smm_cooling_data {
108 struct dell_smm_data *data;
111 MODULE_AUTHOR("Massimo Dal Zotto <dz@debian.org>");
112 MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
113 MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
114 MODULE_LICENSE("GPL");
118 module_param_unsafe(force, bool, 0);
119 MODULE_PARM_DESC(force, "Force loading without checking for supported models and features");
121 static bool ignore_dmi;
122 module_param(ignore_dmi, bool, 0);
123 MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
125 #if IS_ENABLED(CONFIG_I8K)
126 static bool restricted = true;
127 module_param(restricted, bool, 0);
128 MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
130 static bool power_status;
131 module_param(power_status, bool, 0600);
132 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
135 static uint fan_mult;
136 module_param(fan_mult, uint, 0);
137 MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
140 module_param(fan_max, uint, 0);
141 MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
143 static bool disallow_fan_type_call, disallow_fan_support;
145 static unsigned int manual_fan, auto_fan;
147 static const char * const temp_labels[] = {
156 static const char * const fan_labels[] = {
165 static const char * const docking_labels[] = {
166 "Docking Processor Fan",
167 "Docking Motherboard Fan",
169 "Docking Power Supply Fan",
170 "Docking Chipset Fan",
174 static inline const char __init *i8k_get_dmi_data(int field)
176 const char *dmi_data = dmi_get_system_info(field);
178 return dmi_data && *dmi_data ? dmi_data : "?";
182 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
184 static int i8k_smm_func(void *par)
186 struct smm_regs *regs = par;
189 /* SMM requires CPU 0 */
190 if (smp_processor_id() != 0)
193 asm volatile("out %%al,$0xb2\n\t"
211 * Call the System Management Mode BIOS.
213 static int i8k_smm_call(struct device *dummy, struct smm_regs *regs)
218 ret = smp_call_on_cpu(0, i8k_smm_func, regs, true);
224 static const struct dell_smm_ops i8k_smm_ops = {
225 .smm_call = i8k_smm_call,
229 * Call the System Management Mode BIOS over WMI.
231 static ssize_t wmi_parse_register(u8 *buffer, u32 length, unsigned int *reg)
236 if (length <= sizeof(reg_size))
239 reg_size = get_unaligned_le32(buffer);
240 if (!reg_size || reg_size > sizeof(value))
243 if (length < sizeof(reg_size) + reg_size)
246 memcpy_and_pad(&value, sizeof(value), buffer + sizeof(reg_size), reg_size, 0);
247 *reg = le32_to_cpu(value);
249 return reg_size + sizeof(reg_size);
252 static int wmi_parse_response(u8 *buffer, u32 length, struct smm_regs *regs)
254 unsigned int *registers[] = {
264 for (i = 0; i < ARRAY_SIZE(registers); i++) {
265 if (offset >= length)
268 ret = wmi_parse_register(buffer + offset, length - offset, registers[i]);
275 if (offset != length)
281 static int wmi_smm_call(struct device *dev, struct smm_regs *regs)
283 struct wmi_device *wdev = container_of(dev, struct wmi_device, dev);
284 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
285 u32 wmi_payload[] = {
295 const struct acpi_buffer in = {
296 .length = sizeof(wmi_payload),
297 .pointer = &wmi_payload,
299 union acpi_object *obj;
303 status = wmidev_evaluate_method(wdev, 0x0, DELL_SMM_LEGACY_EXECUTE, &in, &out);
304 if (ACPI_FAILURE(status))
311 if (obj->type != ACPI_TYPE_BUFFER) {
317 ret = wmi_parse_response(obj->buffer.pointer, obj->buffer.length, regs);
325 static int dell_smm_call(const struct dell_smm_ops *ops, struct smm_regs *regs)
327 unsigned int eax = regs->eax;
328 unsigned int ebx = regs->ebx;
333 calltime = ktime_get();
334 ret = ops->smm_call(ops->smm_dev, regs);
335 duration = ktime_us_delta(ktime_get(), calltime);
337 pr_debug("SMM(0x%.4x 0x%.4x) = 0x%.4x status: %d (took %7lld usecs)\n",
338 eax, ebx, regs->eax & 0xffff, ret, duration);
340 if (duration > DELL_SMM_MAX_DURATION)
341 pr_warn_once("SMM call took %lld usecs!\n", duration);
346 if ((regs->eax & 0xffff) == 0xffff || regs->eax == eax)
353 * Read the fan status.
355 static int i8k_get_fan_status(const struct dell_smm_data *data, u8 fan)
357 struct smm_regs regs = {
358 .eax = I8K_SMM_GET_FAN,
362 if (disallow_fan_support)
365 return dell_smm_call(data->ops, ®s) ? : regs.eax & 0xff;
369 * Read the fan speed in RPM.
371 static int i8k_get_fan_speed(const struct dell_smm_data *data, u8 fan)
373 struct smm_regs regs = {
374 .eax = I8K_SMM_GET_SPEED,
378 if (disallow_fan_support)
381 return dell_smm_call(data->ops, ®s) ? : (regs.eax & 0xffff) * data->i8k_fan_mult;
387 static int _i8k_get_fan_type(const struct dell_smm_data *data, u8 fan)
389 struct smm_regs regs = {
390 .eax = I8K_SMM_GET_FAN_TYPE,
394 if (disallow_fan_support || disallow_fan_type_call)
397 return dell_smm_call(data->ops, ®s) ? : regs.eax & 0xff;
400 static int i8k_get_fan_type(struct dell_smm_data *data, u8 fan)
402 /* I8K_SMM_GET_FAN_TYPE SMM call is expensive, so cache values */
403 if (data->fan_type[fan] == INT_MIN)
404 data->fan_type[fan] = _i8k_get_fan_type(data, fan);
406 return data->fan_type[fan];
410 * Read the fan nominal rpm for specific fan speed.
412 static int i8k_get_fan_nominal_speed(const struct dell_smm_data *data, u8 fan, int speed)
414 struct smm_regs regs = {
415 .eax = I8K_SMM_GET_NOM_SPEED,
416 .ebx = fan | (speed << 8),
419 if (disallow_fan_support)
422 return dell_smm_call(data->ops, ®s) ? : (regs.eax & 0xffff);
426 * Enable or disable automatic BIOS fan control support
428 static int i8k_enable_fan_auto_mode(const struct dell_smm_data *data, bool enable)
430 struct smm_regs regs = { };
432 if (disallow_fan_support)
435 regs.eax = enable ? auto_fan : manual_fan;
436 return dell_smm_call(data->ops, ®s);
440 * Set the fan speed (off, low, high, ...).
442 static int i8k_set_fan(const struct dell_smm_data *data, u8 fan, int speed)
444 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
446 if (disallow_fan_support)
449 speed = (speed < 0) ? 0 : ((speed > data->i8k_fan_max) ? data->i8k_fan_max : speed);
450 regs.ebx = fan | (speed << 8);
452 return dell_smm_call(data->ops, ®s);
455 static int i8k_get_temp_type(const struct dell_smm_data *data, u8 sensor)
457 struct smm_regs regs = {
458 .eax = I8K_SMM_GET_TEMP_TYPE,
462 return dell_smm_call(data->ops, ®s) ? : regs.eax & 0xff;
466 * Read the cpu temperature.
468 static int _i8k_get_temp(const struct dell_smm_data *data, u8 sensor)
470 struct smm_regs regs = {
471 .eax = I8K_SMM_GET_TEMP,
475 return dell_smm_call(data->ops, ®s) ? : regs.eax & 0xff;
478 static int i8k_get_temp(const struct dell_smm_data *data, u8 sensor)
480 int temp = _i8k_get_temp(data, sensor);
483 * Sometimes the temperature sensor returns 0x99, which is out of range.
484 * In this case we retry (once) before returning an error.
485 # 1003655137 00000058 00005a4b
486 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
487 # 1003655139 00000054 00005c52
491 temp = _i8k_get_temp(data, sensor);
494 * Return -ENODATA for all invalid temperatures.
496 * Known instances are the 0x99 value as seen above as well as
497 * 0xc1 (193), which may be returned when trying to read the GPU
498 * temperature if the system supports a GPU and it is currently
501 if (temp > I8K_MAX_TEMP)
507 static int dell_smm_get_signature(const struct dell_smm_ops *ops, int req_fn)
509 struct smm_regs regs = { .eax = req_fn, };
512 rc = dell_smm_call(ops, ®s);
516 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
519 #if IS_ENABLED(CONFIG_I8K)
522 * Read the Fn key status.
524 static int i8k_get_fn_status(const struct dell_smm_data *data)
526 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
529 rc = dell_smm_call(data->ops, ®s);
533 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
546 * Read the power status.
548 static int i8k_get_power_status(const struct dell_smm_data *data)
550 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
553 rc = dell_smm_call(data->ops, ®s);
557 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
564 static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
566 struct dell_smm_data *data = pde_data(file_inode(fp));
567 int __user *argp = (int __user *)arg;
575 case I8K_BIOS_VERSION:
576 if (!isdigit(data->bios_version[0]) || !isdigit(data->bios_version[1]) ||
577 !isdigit(data->bios_version[2]))
580 val = (data->bios_version[0] << 16) |
581 (data->bios_version[1] << 8) | data->bios_version[2];
583 if (copy_to_user(argp, &val, sizeof(val)))
588 if (restricted && !capable(CAP_SYS_ADMIN))
591 if (copy_to_user(argp, data->bios_machineid, sizeof(data->bios_machineid)))
596 val = i8k_get_fn_status(data);
599 case I8K_POWER_STATUS:
600 val = i8k_get_power_status(data);
604 val = i8k_get_temp(data, 0);
608 if (copy_from_user(&val, argp, sizeof(int)))
611 if (val > U8_MAX || val < 0)
614 val = i8k_get_fan_speed(data, val);
618 if (copy_from_user(&val, argp, sizeof(int)))
621 if (val > U8_MAX || val < 0)
624 val = i8k_get_fan_status(data, val);
628 if (restricted && !capable(CAP_SYS_ADMIN))
631 if (copy_from_user(&val, argp, sizeof(int)))
634 if (val > U8_MAX || val < 0)
637 if (copy_from_user(&speed, argp + 1, sizeof(int)))
640 mutex_lock(&data->i8k_mutex);
641 err = i8k_set_fan(data, val, speed);
645 val = i8k_get_fan_status(data, val);
646 mutex_unlock(&data->i8k_mutex);
656 if (copy_to_user(argp, &val, sizeof(int)))
663 * Print the information for /proc/i8k.
665 static int i8k_proc_show(struct seq_file *seq, void *offset)
667 struct dell_smm_data *data = seq->private;
668 int fn_key, cpu_temp, ac_power;
669 int left_fan, right_fan, left_speed, right_speed;
671 cpu_temp = i8k_get_temp(data, 0); /* 11100 µs */
672 left_fan = i8k_get_fan_status(data, I8K_FAN_LEFT); /* 580 µs */
673 right_fan = i8k_get_fan_status(data, I8K_FAN_RIGHT); /* 580 µs */
674 left_speed = i8k_get_fan_speed(data, I8K_FAN_LEFT); /* 580 µs */
675 right_speed = i8k_get_fan_speed(data, I8K_FAN_RIGHT); /* 580 µs */
676 fn_key = i8k_get_fn_status(data); /* 750 µs */
678 ac_power = i8k_get_power_status(data); /* 14700 µs */
685 * 1) Format version (this will change if format changes)
690 * 6) Right fan status
696 seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
699 (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : data->bios_machineid,
701 left_fan, right_fan, left_speed, right_speed,
707 static int i8k_open_fs(struct inode *inode, struct file *file)
709 return single_open(file, i8k_proc_show, pde_data(inode));
712 static const struct proc_ops i8k_proc_ops = {
713 .proc_open = i8k_open_fs,
714 .proc_read = seq_read,
715 .proc_lseek = seq_lseek,
716 .proc_release = single_release,
717 .proc_ioctl = i8k_ioctl,
720 static void i8k_exit_procfs(void *param)
722 remove_proc_entry("i8k", NULL);
725 static void __init i8k_init_procfs(struct device *dev)
727 struct dell_smm_data *data = dev_get_drvdata(dev);
729 strscpy(data->bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
730 sizeof(data->bios_version));
731 strscpy(data->bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
732 sizeof(data->bios_machineid));
734 /* Only register exit function if creation was successful */
735 if (proc_create_data("i8k", 0, NULL, &i8k_proc_ops, data))
736 devm_add_action_or_reset(dev, i8k_exit_procfs, NULL);
741 static void __init i8k_init_procfs(struct device *dev)
747 static int dell_smm_get_max_state(struct thermal_cooling_device *dev, unsigned long *state)
749 struct dell_smm_cooling_data *cdata = dev->devdata;
751 *state = cdata->data->i8k_fan_max;
756 static int dell_smm_get_cur_state(struct thermal_cooling_device *dev, unsigned long *state)
758 struct dell_smm_cooling_data *cdata = dev->devdata;
761 ret = i8k_get_fan_status(cdata->data, cdata->fan_num);
770 static int dell_smm_set_cur_state(struct thermal_cooling_device *dev, unsigned long state)
772 struct dell_smm_cooling_data *cdata = dev->devdata;
773 struct dell_smm_data *data = cdata->data;
776 if (state > data->i8k_fan_max)
779 mutex_lock(&data->i8k_mutex);
780 ret = i8k_set_fan(data, cdata->fan_num, (int)state);
781 mutex_unlock(&data->i8k_mutex);
786 static const struct thermal_cooling_device_ops dell_smm_cooling_ops = {
787 .get_max_state = dell_smm_get_max_state,
788 .get_cur_state = dell_smm_get_cur_state,
789 .set_cur_state = dell_smm_set_cur_state,
792 static umode_t dell_smm_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr,
795 const struct dell_smm_data *data = drvdata;
800 case hwmon_temp_input:
801 /* _i8k_get_temp() is fine since we do not care about the actual value */
802 if (data->temp_type[channel] >= 0 || _i8k_get_temp(data, channel) >= 0)
806 case hwmon_temp_label:
807 if (data->temp_type[channel] >= 0)
816 if (disallow_fan_support)
820 case hwmon_fan_input:
821 if (data->fan[channel])
825 case hwmon_fan_label:
826 if (data->fan[channel] && !disallow_fan_type_call)
832 case hwmon_fan_target:
833 if (data->fan_nominal_speed[channel])
842 if (disallow_fan_support)
846 case hwmon_pwm_input:
847 if (data->fan[channel])
851 case hwmon_pwm_enable:
854 * There is no command for retrieve the current status
855 * from BIOS, and userspace/firmware itself can change
857 * Thus we can only provide write-only access for now.
873 static int dell_smm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
876 struct dell_smm_data *data = dev_get_drvdata(dev);
877 int mult = data->i8k_fan_mult;
883 case hwmon_temp_input:
884 ret = i8k_get_temp(data, channel);
897 case hwmon_fan_input:
898 ret = i8k_get_fan_speed(data, channel);
906 *val = data->fan_nominal_speed[channel][0] * mult;
910 *val = data->fan_nominal_speed[channel][data->i8k_fan_max] * mult;
913 case hwmon_fan_target:
914 ret = i8k_get_fan_status(data, channel);
918 if (ret > data->i8k_fan_max)
919 ret = data->i8k_fan_max;
921 *val = data->fan_nominal_speed[channel][ret] * mult;
930 case hwmon_pwm_input:
931 ret = i8k_get_fan_status(data, channel);
935 *val = clamp_val(ret * data->i8k_pwm_mult, 0, 255);
949 static const char *dell_smm_fan_label(struct dell_smm_data *data, int channel)
952 int type = i8k_get_fan_type(data, channel);
955 return ERR_PTR(type);
962 if (type >= ARRAY_SIZE(fan_labels))
963 type = ARRAY_SIZE(fan_labels) - 1;
965 return dock ? docking_labels[type] : fan_labels[type];
968 static int dell_smm_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
969 int channel, const char **str)
971 struct dell_smm_data *data = dev_get_drvdata(dev);
976 case hwmon_temp_label:
977 *str = temp_labels[data->temp_type[channel]];
985 case hwmon_fan_label:
986 *str = dell_smm_fan_label(data, channel);
987 return PTR_ERR_OR_ZERO(*str);
999 static int dell_smm_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
1002 struct dell_smm_data *data = dev_get_drvdata(dev);
1010 case hwmon_pwm_input:
1011 pwm = clamp_val(DIV_ROUND_CLOSEST(val, data->i8k_pwm_mult), 0,
1014 mutex_lock(&data->i8k_mutex);
1015 err = i8k_set_fan(data, channel, pwm);
1016 mutex_unlock(&data->i8k_mutex);
1022 case hwmon_pwm_enable:
1031 mutex_lock(&data->i8k_mutex);
1032 err = i8k_enable_fan_auto_mode(data, enable);
1033 mutex_unlock(&data->i8k_mutex);
1050 static const struct hwmon_ops dell_smm_ops = {
1051 .is_visible = dell_smm_is_visible,
1052 .read = dell_smm_read,
1053 .read_string = dell_smm_read_string,
1054 .write = dell_smm_write,
1057 static const struct hwmon_channel_info * const dell_smm_info[] = {
1058 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
1059 HWMON_CHANNEL_INFO(temp,
1060 HWMON_T_INPUT | HWMON_T_LABEL,
1061 HWMON_T_INPUT | HWMON_T_LABEL,
1062 HWMON_T_INPUT | HWMON_T_LABEL,
1063 HWMON_T_INPUT | HWMON_T_LABEL,
1064 HWMON_T_INPUT | HWMON_T_LABEL,
1065 HWMON_T_INPUT | HWMON_T_LABEL,
1066 HWMON_T_INPUT | HWMON_T_LABEL,
1067 HWMON_T_INPUT | HWMON_T_LABEL,
1068 HWMON_T_INPUT | HWMON_T_LABEL,
1069 HWMON_T_INPUT | HWMON_T_LABEL
1071 HWMON_CHANNEL_INFO(fan,
1072 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
1074 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
1076 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
1079 HWMON_CHANNEL_INFO(pwm,
1080 HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
1087 static const struct hwmon_chip_info dell_smm_chip_info = {
1088 .ops = &dell_smm_ops,
1089 .info = dell_smm_info,
1092 static int dell_smm_init_cdev(struct device *dev, u8 fan_num)
1094 struct dell_smm_data *data = dev_get_drvdata(dev);
1095 struct thermal_cooling_device *cdev;
1096 struct dell_smm_cooling_data *cdata;
1100 name = kasprintf(GFP_KERNEL, "dell-smm-fan%u", fan_num + 1);
1104 cdata = devm_kmalloc(dev, sizeof(*cdata), GFP_KERNEL);
1106 cdata->fan_num = fan_num;
1108 cdev = devm_thermal_of_cooling_device_register(dev, NULL, name, cdata,
1109 &dell_smm_cooling_ops);
1111 devm_kfree(dev, cdata);
1112 ret = PTR_ERR(cdev);
1123 static int dell_smm_init_hwmon(struct device *dev)
1125 struct dell_smm_data *data = dev_get_drvdata(dev);
1126 struct device *dell_smm_hwmon_dev;
1130 for (i = 0; i < DELL_SMM_NO_TEMP; i++) {
1131 data->temp_type[i] = i8k_get_temp_type(data, i);
1132 if (data->temp_type[i] < 0)
1135 if (data->temp_type[i] >= ARRAY_SIZE(temp_labels))
1136 data->temp_type[i] = ARRAY_SIZE(temp_labels) - 1;
1139 for (i = 0; i < DELL_SMM_NO_FANS; i++) {
1140 data->fan_type[i] = INT_MIN;
1141 err = i8k_get_fan_status(data, i);
1143 err = i8k_get_fan_type(data, i);
1148 data->fan[i] = true;
1150 /* the cooling device is not critical, ignore failures */
1151 if (IS_REACHABLE(CONFIG_THERMAL)) {
1152 err = dell_smm_init_cdev(dev, i);
1154 dev_warn(dev, "Failed to register cooling device for fan %u\n",
1158 data->fan_nominal_speed[i] = devm_kmalloc_array(dev, data->i8k_fan_max + 1,
1159 sizeof(*data->fan_nominal_speed[i]),
1161 if (!data->fan_nominal_speed[i])
1164 for (state = 0; state <= data->i8k_fan_max; state++) {
1165 err = i8k_get_fan_nominal_speed(data, i, state);
1167 /* Mark nominal speed table as invalid in case of error */
1168 devm_kfree(dev, data->fan_nominal_speed[i]);
1169 data->fan_nominal_speed[i] = NULL;
1172 data->fan_nominal_speed[i][state] = err;
1174 * Autodetect fan multiplier based on nominal rpm if multiplier
1175 * was not specified as module param or in DMI. If fan reports
1176 * rpm value too high then set multiplier to 1.
1178 if (!fan_mult && err > I8K_FAN_RPM_THRESHOLD)
1179 data->i8k_fan_mult = 1;
1183 dell_smm_hwmon_dev = devm_hwmon_device_register_with_info(dev, "dell_smm", data,
1184 &dell_smm_chip_info, NULL);
1186 return PTR_ERR_OR_ZERO(dell_smm_hwmon_dev);
1189 static int dell_smm_init_data(struct device *dev, const struct dell_smm_ops *ops)
1191 struct dell_smm_data *data;
1193 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1197 mutex_init(&data->i8k_mutex);
1198 dev_set_drvdata(dev, data);
1201 /* All options must not be 0 */
1202 data->i8k_fan_mult = fan_mult ? : I8K_FAN_MULT;
1203 data->i8k_fan_max = fan_max ? : I8K_FAN_HIGH;
1204 data->i8k_pwm_mult = DIV_ROUND_UP(255, data->i8k_fan_max);
1209 static const struct dmi_system_id i8k_dmi_table[] __initconst = {
1211 .ident = "Dell G5 5590",
1213 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1214 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "G5 5590"),
1218 .ident = "Dell G5 5505",
1220 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1221 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "G5 5505"),
1225 .ident = "Dell Inspiron",
1227 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
1228 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
1232 .ident = "Dell Latitude",
1234 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
1235 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
1239 .ident = "Dell Inspiron 2",
1241 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1242 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
1246 .ident = "Dell Latitude 2",
1248 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1249 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
1252 { /* UK Inspiron 6400 */
1253 .ident = "Dell Inspiron 3",
1255 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1256 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
1260 .ident = "Dell Inspiron 3",
1262 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1263 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
1267 .ident = "Dell Precision",
1269 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1270 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
1274 .ident = "Dell Vostro",
1276 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1277 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
1281 .ident = "Dell Studio",
1283 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1284 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
1288 .ident = "Dell XPS M140",
1290 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1291 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
1295 .ident = "Dell XPS",
1297 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1298 DMI_MATCH(DMI_PRODUCT_NAME, "XPS"),
1304 MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
1307 * Only use for machines which need some special configuration
1308 * in order to work correctly (e.g. if autoconfig fails on this machines).
1310 struct i8k_config_data {
1322 static const struct i8k_config_data i8k_config_data[] __initconst = {
1323 [DELL_LATITUDE_D520] = {
1325 .fan_max = I8K_FAN_TURBO,
1327 [DELL_PRECISION_490] = {
1329 .fan_max = I8K_FAN_TURBO,
1333 .fan_max = I8K_FAN_HIGH,
1337 .fan_max = I8K_FAN_HIGH,
1341 static const struct dmi_system_id i8k_config_dmi_table[] __initconst = {
1343 .ident = "Dell Latitude D520",
1345 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1346 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
1348 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
1351 .ident = "Dell Precision 490",
1353 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1354 DMI_MATCH(DMI_PRODUCT_NAME,
1355 "Precision WorkStation 490"),
1357 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
1360 .ident = "Dell Studio",
1362 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1363 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
1365 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
1368 .ident = "Dell XPS M140",
1370 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1371 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
1373 .driver_data = (void *)&i8k_config_data[DELL_XPS],
1379 * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed
1380 * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist
1381 * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call.
1382 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121
1384 static const struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initconst = {
1386 .ident = "Dell Studio XPS 8000",
1388 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1389 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
1393 .ident = "Dell Studio XPS 8100",
1395 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1396 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
1400 .ident = "Dell Inspiron 580",
1402 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1403 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
1407 .ident = "Dell Inspiron 3505",
1409 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1410 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 3505"),
1417 * On some machines all fan related SMM functions implemented by Dell BIOS
1418 * firmware freeze kernel for about 500ms. Until Dell fixes these problems fan
1419 * support for affected blacklisted Dell machines stay disabled.
1420 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=195751
1422 static const struct dmi_system_id i8k_blacklist_fan_support_dmi_table[] __initconst = {
1424 .ident = "Dell Inspiron 7720",
1426 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1427 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
1431 .ident = "Dell Vostro 3360",
1433 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1434 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
1438 .ident = "Dell XPS13 9333",
1440 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1441 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
1445 .ident = "Dell XPS 15 L502X",
1447 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1448 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Dell System XPS L502X"),
1454 struct i8k_fan_control_data {
1455 unsigned int manual_fan;
1456 unsigned int auto_fan;
1459 enum i8k_fan_controls {
1464 static const struct i8k_fan_control_data i8k_fan_control_data[] __initconst = {
1465 [I8K_FAN_30A3_31A3] = {
1466 .manual_fan = 0x30a3,
1469 [I8K_FAN_34A3_35A3] = {
1470 .manual_fan = 0x34a3,
1475 static const struct dmi_system_id i8k_whitelist_fan_control[] __initconst = {
1477 .ident = "Dell Latitude 5480",
1479 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1480 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude 5480"),
1482 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1485 .ident = "Dell Latitude E6440",
1487 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1488 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E6440"),
1490 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1493 .ident = "Dell Latitude E7440",
1495 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1496 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E7440"),
1498 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1501 .ident = "Dell Precision 5530",
1503 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1504 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 5530"),
1506 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1509 .ident = "Dell Precision 7510",
1511 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1512 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 7510"),
1514 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1517 .ident = "Dell Precision 7540",
1519 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1520 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 7540"),
1522 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1525 .ident = "Dell XPS 13 7390",
1527 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1528 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 13 7390"),
1530 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1533 .ident = "Dell Optiplex 7000",
1535 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1536 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7000"),
1538 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1541 .ident = "Dell XPS 9315",
1543 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1544 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 9315"),
1546 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_30A3_31A3],
1549 .ident = "Dell G15 5511",
1551 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1552 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Dell G15 5511"),
1554 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_30A3_31A3],
1560 * Legacy SMM backend driver.
1562 static int __init dell_smm_probe(struct platform_device *pdev)
1566 ret = dell_smm_init_data(&pdev->dev, &i8k_smm_ops);
1570 ret = dell_smm_init_hwmon(&pdev->dev);
1574 i8k_init_procfs(&pdev->dev);
1579 static struct platform_driver dell_smm_driver = {
1581 .name = KBUILD_MODNAME,
1585 static struct platform_device *dell_smm_device;
1588 * WMI SMM backend driver.
1590 static int dell_smm_wmi_probe(struct wmi_device *wdev, const void *context)
1592 struct dell_smm_ops *ops;
1595 ops = devm_kzalloc(&wdev->dev, sizeof(*ops), GFP_KERNEL);
1599 ops->smm_call = wmi_smm_call;
1600 ops->smm_dev = &wdev->dev;
1602 if (dell_smm_get_signature(ops, I8K_SMM_GET_DELL_SIG1) &&
1603 dell_smm_get_signature(ops, I8K_SMM_GET_DELL_SIG2))
1606 ret = dell_smm_init_data(&wdev->dev, ops);
1610 return dell_smm_init_hwmon(&wdev->dev);
1613 static const struct wmi_device_id dell_smm_wmi_id_table[] = {
1614 { DELL_SMM_WMI_GUID, NULL },
1617 MODULE_DEVICE_TABLE(wmi, dell_smm_wmi_id_table);
1619 static struct wmi_driver dell_smm_wmi_driver = {
1621 .name = KBUILD_MODNAME,
1622 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1624 .id_table = dell_smm_wmi_id_table,
1625 .probe = dell_smm_wmi_probe,
1626 .no_singleton = true,
1630 * Probe for the presence of a supported laptop.
1632 static void __init dell_smm_init_dmi(void)
1634 struct i8k_fan_control_data *control;
1635 struct i8k_config_data *config;
1636 const struct dmi_system_id *id;
1638 if (dmi_check_system(i8k_blacklist_fan_support_dmi_table)) {
1640 pr_notice("Disabling fan support due to BIOS bugs\n");
1641 disallow_fan_support = true;
1643 pr_warn("Enabling fan support despite BIOS bugs\n");
1647 if (dmi_check_system(i8k_blacklist_fan_type_dmi_table)) {
1649 pr_notice("Disabling fan type call due to BIOS bugs\n");
1650 disallow_fan_type_call = true;
1652 pr_warn("Enabling fan type call despite BIOS bugs\n");
1657 * Set fan multiplier and maximal fan speed from DMI config.
1658 * Values specified in module parameters override values from DMI.
1660 id = dmi_first_match(i8k_config_dmi_table);
1661 if (id && id->driver_data) {
1662 config = id->driver_data;
1663 if (!fan_mult && config->fan_mult)
1664 fan_mult = config->fan_mult;
1666 if (!fan_max && config->fan_max)
1667 fan_max = config->fan_max;
1670 id = dmi_first_match(i8k_whitelist_fan_control);
1671 if (id && id->driver_data) {
1672 control = id->driver_data;
1673 manual_fan = control->manual_fan;
1674 auto_fan = control->auto_fan;
1676 pr_info("Enabling support for setting automatic/manual fan control\n");
1680 static int __init dell_smm_legacy_check(void)
1682 if (!dmi_check_system(i8k_dmi_table)) {
1683 if (!ignore_dmi && !force)
1686 pr_info("Probing for legacy SMM handler on unsupported machine\n");
1687 pr_info("vendor=%s, model=%s, version=%s\n",
1688 i8k_get_dmi_data(DMI_SYS_VENDOR),
1689 i8k_get_dmi_data(DMI_PRODUCT_NAME),
1690 i8k_get_dmi_data(DMI_BIOS_VERSION));
1693 if (dell_smm_get_signature(&i8k_smm_ops, I8K_SMM_GET_DELL_SIG1) &&
1694 dell_smm_get_signature(&i8k_smm_ops, I8K_SMM_GET_DELL_SIG2)) {
1698 pr_warn("Forcing legacy SMM calls on a possibly incompatible machine\n");
1704 static int __init i8k_init(void)
1708 dell_smm_init_dmi();
1710 ret = dell_smm_legacy_check();
1713 * On modern machines, SMM communication happens over WMI, meaning
1714 * the SMM handler might not react to legacy SMM calls.
1716 return wmi_driver_register(&dell_smm_wmi_driver);
1719 dell_smm_device = platform_create_bundle(&dell_smm_driver, dell_smm_probe, NULL, 0, NULL,
1722 return PTR_ERR_OR_ZERO(dell_smm_device);
1725 static void __exit i8k_exit(void)
1727 if (dell_smm_device) {
1728 platform_device_unregister(dell_smm_device);
1729 platform_driver_unregister(&dell_smm_driver);
1731 wmi_driver_unregister(&dell_smm_wmi_driver);
1735 module_init(i8k_init);
1736 module_exit(i8k_exit);