1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops. 4 * 5 * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org> 6 * 7 * Hwmon integration: 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> 11 */ 12 13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 15 #include <linux/capability.h> 16 #include <linux/cpu.h> 17 #include <linux/ctype.h> 18 #include <linux/delay.h> 19 #include <linux/dmi.h> 20 #include <linux/err.h> 21 #include <linux/errno.h> 22 #include <linux/hwmon.h> 23 #include <linux/init.h> 24 #include <linux/kernel.h> 25 #include <linux/module.h> 26 #include <linux/mutex.h> 27 #include <linux/platform_device.h> 28 #include <linux/proc_fs.h> 29 #include <linux/seq_file.h> 30 #include <linux/string.h> 31 #include <linux/smp.h> 32 #include <linux/types.h> 33 #include <linux/uaccess.h> 34 35 #include <linux/i8k.h> 36 37 #define I8K_SMM_FN_STATUS 0x0025 38 #define I8K_SMM_POWER_STATUS 0x0069 39 #define I8K_SMM_SET_FAN 0x01a3 40 #define I8K_SMM_GET_FAN 0x00a3 41 #define I8K_SMM_GET_SPEED 0x02a3 42 #define I8K_SMM_GET_FAN_TYPE 0x03a3 43 #define I8K_SMM_GET_NOM_SPEED 0x04a3 44 #define I8K_SMM_GET_TEMP 0x10a3 45 #define I8K_SMM_GET_TEMP_TYPE 0x11a3 46 #define I8K_SMM_GET_DELL_SIG1 0xfea3 47 #define I8K_SMM_GET_DELL_SIG2 0xffa3 48 49 #define I8K_FAN_MULT 30 50 #define I8K_FAN_MAX_RPM 30000 51 #define I8K_MAX_TEMP 127 52 53 #define I8K_FN_NONE 0x00 54 #define I8K_FN_UP 0x01 55 #define I8K_FN_DOWN 0x02 56 #define I8K_FN_MUTE 0x04 57 #define I8K_FN_MASK 0x07 58 #define I8K_FN_SHIFT 8 59 60 #define I8K_POWER_AC 0x05 61 #define I8K_POWER_BATTERY 0x01 62 63 #define DELL_SMM_NO_TEMP 10 64 #define DELL_SMM_NO_FANS 3 65 66 struct dell_smm_data { 67 struct mutex i8k_mutex; /* lock for sensors writes */ 68 char bios_version[4]; 69 char bios_machineid[16]; 70 uint i8k_fan_mult; 71 uint i8k_pwm_mult; 72 uint i8k_fan_max; 73 bool disallow_fan_type_call; 74 bool disallow_fan_support; 75 unsigned int manual_fan; 76 unsigned int auto_fan; 77 int temp_type[DELL_SMM_NO_TEMP]; 78 bool fan[DELL_SMM_NO_FANS]; 79 int fan_type[DELL_SMM_NO_FANS]; 80 int *fan_nominal_speed[DELL_SMM_NO_FANS]; 81 }; 82 83 MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)"); 84 MODULE_AUTHOR("Pali Rohár <pali@kernel.org>"); 85 MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver"); 86 MODULE_LICENSE("GPL"); 87 MODULE_ALIAS("i8k"); 88 89 static bool force; 90 module_param_unsafe(force, bool, 0); 91 MODULE_PARM_DESC(force, "Force loading without checking for supported models and features"); 92 93 static bool ignore_dmi; 94 module_param(ignore_dmi, bool, 0); 95 MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match"); 96 97 #if IS_ENABLED(CONFIG_I8K) 98 static bool restricted = true; 99 module_param(restricted, bool, 0); 100 MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)"); 101 102 static bool power_status; 103 module_param(power_status, bool, 0600); 104 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)"); 105 #endif 106 107 static uint fan_mult; 108 module_param(fan_mult, uint, 0); 109 MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)"); 110 111 static uint fan_max; 112 module_param(fan_max, uint, 0); 113 MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)"); 114 115 struct smm_regs { 116 unsigned int eax; 117 unsigned int ebx; 118 unsigned int ecx; 119 unsigned int edx; 120 unsigned int esi; 121 unsigned int edi; 122 } __packed; 123 124 static const char * const temp_labels[] = { 125 "CPU", 126 "GPU", 127 "SODIMM", 128 "Other", 129 "Ambient", 130 "Other", 131 }; 132 133 static const char * const fan_labels[] = { 134 "Processor Fan", 135 "Motherboard Fan", 136 "Video Fan", 137 "Power Supply Fan", 138 "Chipset Fan", 139 "Other Fan", 140 }; 141 142 static const char * const docking_labels[] = { 143 "Docking Processor Fan", 144 "Docking Motherboard Fan", 145 "Docking Video Fan", 146 "Docking Power Supply Fan", 147 "Docking Chipset Fan", 148 "Docking Other Fan", 149 }; 150 151 static inline const char __init *i8k_get_dmi_data(int field) 152 { 153 const char *dmi_data = dmi_get_system_info(field); 154 155 return dmi_data && *dmi_data ? dmi_data : "?"; 156 } 157 158 /* 159 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard. 160 */ 161 static int i8k_smm_func(void *par) 162 { 163 ktime_t calltime = ktime_get(); 164 struct smm_regs *regs = par; 165 int eax = regs->eax; 166 int ebx = regs->ebx; 167 long long duration; 168 int rc; 169 170 /* SMM requires CPU 0 */ 171 if (smp_processor_id() != 0) 172 return -EBUSY; 173 174 #if defined(CONFIG_X86_64) 175 asm volatile("pushq %%rax\n\t" 176 "movl 0(%%rax),%%edx\n\t" 177 "pushq %%rdx\n\t" 178 "movl 4(%%rax),%%ebx\n\t" 179 "movl 8(%%rax),%%ecx\n\t" 180 "movl 12(%%rax),%%edx\n\t" 181 "movl 16(%%rax),%%esi\n\t" 182 "movl 20(%%rax),%%edi\n\t" 183 "popq %%rax\n\t" 184 "out %%al,$0xb2\n\t" 185 "out %%al,$0x84\n\t" 186 "xchgq %%rax,(%%rsp)\n\t" 187 "movl %%ebx,4(%%rax)\n\t" 188 "movl %%ecx,8(%%rax)\n\t" 189 "movl %%edx,12(%%rax)\n\t" 190 "movl %%esi,16(%%rax)\n\t" 191 "movl %%edi,20(%%rax)\n\t" 192 "popq %%rdx\n\t" 193 "movl %%edx,0(%%rax)\n\t" 194 "pushfq\n\t" 195 "popq %%rax\n\t" 196 "andl $1,%%eax\n" 197 : "=a"(rc) 198 : "a"(regs) 199 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); 200 #else 201 asm volatile("pushl %%eax\n\t" 202 "movl 0(%%eax),%%edx\n\t" 203 "push %%edx\n\t" 204 "movl 4(%%eax),%%ebx\n\t" 205 "movl 8(%%eax),%%ecx\n\t" 206 "movl 12(%%eax),%%edx\n\t" 207 "movl 16(%%eax),%%esi\n\t" 208 "movl 20(%%eax),%%edi\n\t" 209 "popl %%eax\n\t" 210 "out %%al,$0xb2\n\t" 211 "out %%al,$0x84\n\t" 212 "xchgl %%eax,(%%esp)\n\t" 213 "movl %%ebx,4(%%eax)\n\t" 214 "movl %%ecx,8(%%eax)\n\t" 215 "movl %%edx,12(%%eax)\n\t" 216 "movl %%esi,16(%%eax)\n\t" 217 "movl %%edi,20(%%eax)\n\t" 218 "popl %%edx\n\t" 219 "movl %%edx,0(%%eax)\n\t" 220 "lahf\n\t" 221 "shrl $8,%%eax\n\t" 222 "andl $1,%%eax\n" 223 : "=a"(rc) 224 : "a"(regs) 225 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); 226 #endif 227 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax) 228 rc = -EINVAL; 229 230 duration = ktime_us_delta(ktime_get(), calltime); 231 pr_debug("smm(0x%.4x 0x%.4x) = 0x%.4x (took %7lld usecs)\n", eax, ebx, 232 (rc ? 0xffff : regs->eax & 0xffff), duration); 233 234 return rc; 235 } 236 237 /* 238 * Call the System Management Mode BIOS. 239 */ 240 static int i8k_smm(struct smm_regs *regs) 241 { 242 int ret; 243 244 cpus_read_lock(); 245 ret = smp_call_on_cpu(0, i8k_smm_func, regs, true); 246 cpus_read_unlock(); 247 248 return ret; 249 } 250 251 /* 252 * Read the fan status. 253 */ 254 static int i8k_get_fan_status(const struct dell_smm_data *data, u8 fan) 255 { 256 struct smm_regs regs = { 257 .eax = I8K_SMM_GET_FAN, 258 .ebx = fan, 259 }; 260 261 if (data->disallow_fan_support) 262 return -EINVAL; 263 264 return i8k_smm(®s) ? : regs.eax & 0xff; 265 } 266 267 /* 268 * Read the fan speed in RPM. 269 */ 270 static int i8k_get_fan_speed(const struct dell_smm_data *data, u8 fan) 271 { 272 struct smm_regs regs = { 273 .eax = I8K_SMM_GET_SPEED, 274 .ebx = fan, 275 }; 276 277 if (data->disallow_fan_support) 278 return -EINVAL; 279 280 return i8k_smm(®s) ? : (regs.eax & 0xffff) * data->i8k_fan_mult; 281 } 282 283 /* 284 * Read the fan type. 285 */ 286 static int _i8k_get_fan_type(const struct dell_smm_data *data, u8 fan) 287 { 288 struct smm_regs regs = { 289 .eax = I8K_SMM_GET_FAN_TYPE, 290 .ebx = fan, 291 }; 292 293 if (data->disallow_fan_support || data->disallow_fan_type_call) 294 return -EINVAL; 295 296 return i8k_smm(®s) ? : regs.eax & 0xff; 297 } 298 299 static int i8k_get_fan_type(struct dell_smm_data *data, u8 fan) 300 { 301 /* I8K_SMM_GET_FAN_TYPE SMM call is expensive, so cache values */ 302 if (data->fan_type[fan] == INT_MIN) 303 data->fan_type[fan] = _i8k_get_fan_type(data, fan); 304 305 return data->fan_type[fan]; 306 } 307 308 /* 309 * Read the fan nominal rpm for specific fan speed. 310 */ 311 static int __init i8k_get_fan_nominal_speed(const struct dell_smm_data *data, u8 fan, int speed) 312 { 313 struct smm_regs regs = { 314 .eax = I8K_SMM_GET_NOM_SPEED, 315 .ebx = fan | (speed << 8), 316 }; 317 318 if (data->disallow_fan_support) 319 return -EINVAL; 320 321 return i8k_smm(®s) ? : (regs.eax & 0xffff) * data->i8k_fan_mult; 322 } 323 324 /* 325 * Enable or disable automatic BIOS fan control support 326 */ 327 static int i8k_enable_fan_auto_mode(const struct dell_smm_data *data, bool enable) 328 { 329 struct smm_regs regs = { }; 330 331 if (data->disallow_fan_support) 332 return -EINVAL; 333 334 regs.eax = enable ? data->auto_fan : data->manual_fan; 335 return i8k_smm(®s); 336 } 337 338 /* 339 * Set the fan speed (off, low, high, ...). 340 */ 341 static int i8k_set_fan(const struct dell_smm_data *data, u8 fan, int speed) 342 { 343 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, }; 344 345 if (data->disallow_fan_support) 346 return -EINVAL; 347 348 speed = (speed < 0) ? 0 : ((speed > data->i8k_fan_max) ? data->i8k_fan_max : speed); 349 regs.ebx = fan | (speed << 8); 350 351 return i8k_smm(®s); 352 } 353 354 static int __init i8k_get_temp_type(u8 sensor) 355 { 356 struct smm_regs regs = { 357 .eax = I8K_SMM_GET_TEMP_TYPE, 358 .ebx = sensor, 359 }; 360 361 return i8k_smm(®s) ? : regs.eax & 0xff; 362 } 363 364 /* 365 * Read the cpu temperature. 366 */ 367 static int _i8k_get_temp(u8 sensor) 368 { 369 struct smm_regs regs = { 370 .eax = I8K_SMM_GET_TEMP, 371 .ebx = sensor, 372 }; 373 374 return i8k_smm(®s) ? : regs.eax & 0xff; 375 } 376 377 static int i8k_get_temp(u8 sensor) 378 { 379 int temp = _i8k_get_temp(sensor); 380 381 /* 382 * Sometimes the temperature sensor returns 0x99, which is out of range. 383 * In this case we retry (once) before returning an error. 384 # 1003655137 00000058 00005a4b 385 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees 386 # 1003655139 00000054 00005c52 387 */ 388 if (temp == 0x99) { 389 msleep(100); 390 temp = _i8k_get_temp(sensor); 391 } 392 /* 393 * Return -ENODATA for all invalid temperatures. 394 * 395 * Known instances are the 0x99 value as seen above as well as 396 * 0xc1 (193), which may be returned when trying to read the GPU 397 * temperature if the system supports a GPU and it is currently 398 * turned off. 399 */ 400 if (temp > I8K_MAX_TEMP) 401 return -ENODATA; 402 403 return temp; 404 } 405 406 static int __init i8k_get_dell_signature(int req_fn) 407 { 408 struct smm_regs regs = { .eax = req_fn, }; 409 int rc; 410 411 rc = i8k_smm(®s); 412 if (rc < 0) 413 return rc; 414 415 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1; 416 } 417 418 #if IS_ENABLED(CONFIG_I8K) 419 420 /* 421 * Read the Fn key status. 422 */ 423 static int i8k_get_fn_status(void) 424 { 425 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, }; 426 int rc; 427 428 rc = i8k_smm(®s); 429 if (rc < 0) 430 return rc; 431 432 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) { 433 case I8K_FN_UP: 434 return I8K_VOL_UP; 435 case I8K_FN_DOWN: 436 return I8K_VOL_DOWN; 437 case I8K_FN_MUTE: 438 return I8K_VOL_MUTE; 439 default: 440 return 0; 441 } 442 } 443 444 /* 445 * Read the power status. 446 */ 447 static int i8k_get_power_status(void) 448 { 449 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, }; 450 int rc; 451 452 rc = i8k_smm(®s); 453 if (rc < 0) 454 return rc; 455 456 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY; 457 } 458 459 /* 460 * Procfs interface 461 */ 462 463 static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) 464 { 465 struct dell_smm_data *data = pde_data(file_inode(fp)); 466 int __user *argp = (int __user *)arg; 467 int speed, err; 468 int val = 0; 469 470 if (!argp) 471 return -EINVAL; 472 473 switch (cmd) { 474 case I8K_BIOS_VERSION: 475 if (!isdigit(data->bios_version[0]) || !isdigit(data->bios_version[1]) || 476 !isdigit(data->bios_version[2])) 477 return -EINVAL; 478 479 val = (data->bios_version[0] << 16) | 480 (data->bios_version[1] << 8) | data->bios_version[2]; 481 482 if (copy_to_user(argp, &val, sizeof(val))) 483 return -EFAULT; 484 485 return 0; 486 case I8K_MACHINE_ID: 487 if (restricted && !capable(CAP_SYS_ADMIN)) 488 return -EPERM; 489 490 if (copy_to_user(argp, data->bios_machineid, sizeof(data->bios_machineid))) 491 return -EFAULT; 492 493 return 0; 494 case I8K_FN_STATUS: 495 val = i8k_get_fn_status(); 496 break; 497 498 case I8K_POWER_STATUS: 499 val = i8k_get_power_status(); 500 break; 501 502 case I8K_GET_TEMP: 503 val = i8k_get_temp(0); 504 break; 505 506 case I8K_GET_SPEED: 507 if (copy_from_user(&val, argp, sizeof(int))) 508 return -EFAULT; 509 510 if (val > U8_MAX || val < 0) 511 return -EINVAL; 512 513 val = i8k_get_fan_speed(data, val); 514 break; 515 516 case I8K_GET_FAN: 517 if (copy_from_user(&val, argp, sizeof(int))) 518 return -EFAULT; 519 520 if (val > U8_MAX || val < 0) 521 return -EINVAL; 522 523 val = i8k_get_fan_status(data, val); 524 break; 525 526 case I8K_SET_FAN: 527 if (restricted && !capable(CAP_SYS_ADMIN)) 528 return -EPERM; 529 530 if (copy_from_user(&val, argp, sizeof(int))) 531 return -EFAULT; 532 533 if (val > U8_MAX || val < 0) 534 return -EINVAL; 535 536 if (copy_from_user(&speed, argp + 1, sizeof(int))) 537 return -EFAULT; 538 539 mutex_lock(&data->i8k_mutex); 540 err = i8k_set_fan(data, val, speed); 541 if (err < 0) 542 val = err; 543 else 544 val = i8k_get_fan_status(data, val); 545 mutex_unlock(&data->i8k_mutex); 546 break; 547 548 default: 549 return -ENOIOCTLCMD; 550 } 551 552 if (val < 0) 553 return val; 554 555 if (copy_to_user(argp, &val, sizeof(int))) 556 return -EFAULT; 557 558 return 0; 559 } 560 561 /* 562 * Print the information for /proc/i8k. 563 */ 564 static int i8k_proc_show(struct seq_file *seq, void *offset) 565 { 566 struct dell_smm_data *data = seq->private; 567 int fn_key, cpu_temp, ac_power; 568 int left_fan, right_fan, left_speed, right_speed; 569 570 cpu_temp = i8k_get_temp(0); /* 11100 µs */ 571 left_fan = i8k_get_fan_status(data, I8K_FAN_LEFT); /* 580 µs */ 572 right_fan = i8k_get_fan_status(data, I8K_FAN_RIGHT); /* 580 µs */ 573 left_speed = i8k_get_fan_speed(data, I8K_FAN_LEFT); /* 580 µs */ 574 right_speed = i8k_get_fan_speed(data, I8K_FAN_RIGHT); /* 580 µs */ 575 fn_key = i8k_get_fn_status(); /* 750 µs */ 576 if (power_status) 577 ac_power = i8k_get_power_status(); /* 14700 µs */ 578 else 579 ac_power = -1; 580 581 /* 582 * Info: 583 * 584 * 1) Format version (this will change if format changes) 585 * 2) BIOS version 586 * 3) BIOS machine ID 587 * 4) Cpu temperature 588 * 5) Left fan status 589 * 6) Right fan status 590 * 7) Left fan speed 591 * 8) Right fan speed 592 * 9) AC power 593 * 10) Fn Key status 594 */ 595 seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n", 596 I8K_PROC_FMT, 597 data->bios_version, 598 (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : data->bios_machineid, 599 cpu_temp, 600 left_fan, right_fan, left_speed, right_speed, 601 ac_power, fn_key); 602 603 return 0; 604 } 605 606 static int i8k_open_fs(struct inode *inode, struct file *file) 607 { 608 return single_open(file, i8k_proc_show, pde_data(inode)); 609 } 610 611 static const struct proc_ops i8k_proc_ops = { 612 .proc_open = i8k_open_fs, 613 .proc_read = seq_read, 614 .proc_lseek = seq_lseek, 615 .proc_release = single_release, 616 .proc_ioctl = i8k_ioctl, 617 }; 618 619 static void i8k_exit_procfs(void *param) 620 { 621 remove_proc_entry("i8k", NULL); 622 } 623 624 static void __init i8k_init_procfs(struct device *dev) 625 { 626 struct dell_smm_data *data = dev_get_drvdata(dev); 627 628 /* Only register exit function if creation was successful */ 629 if (proc_create_data("i8k", 0, NULL, &i8k_proc_ops, data)) 630 devm_add_action_or_reset(dev, i8k_exit_procfs, NULL); 631 } 632 633 #else 634 635 static void __init i8k_init_procfs(struct device *dev) 636 { 637 } 638 639 #endif 640 641 /* 642 * Hwmon interface 643 */ 644 645 static umode_t dell_smm_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr, 646 int channel) 647 { 648 const struct dell_smm_data *data = drvdata; 649 650 switch (type) { 651 case hwmon_temp: 652 switch (attr) { 653 case hwmon_temp_input: 654 /* _i8k_get_temp() is fine since we do not care about the actual value */ 655 if (data->temp_type[channel] >= 0 || _i8k_get_temp(channel) >= 0) 656 return 0444; 657 658 break; 659 case hwmon_temp_label: 660 if (data->temp_type[channel] >= 0) 661 return 0444; 662 663 break; 664 default: 665 break; 666 } 667 break; 668 case hwmon_fan: 669 if (data->disallow_fan_support) 670 break; 671 672 switch (attr) { 673 case hwmon_fan_input: 674 if (data->fan[channel]) 675 return 0444; 676 677 break; 678 case hwmon_fan_label: 679 if (data->fan[channel] && !data->disallow_fan_type_call) 680 return 0444; 681 682 break; 683 case hwmon_fan_min: 684 case hwmon_fan_max: 685 case hwmon_fan_target: 686 if (data->fan_nominal_speed[channel]) 687 return 0444; 688 689 break; 690 default: 691 break; 692 } 693 break; 694 case hwmon_pwm: 695 if (data->disallow_fan_support) 696 break; 697 698 switch (attr) { 699 case hwmon_pwm_input: 700 if (data->fan[channel]) 701 return 0644; 702 703 break; 704 case hwmon_pwm_enable: 705 if (data->auto_fan) 706 /* 707 * There is no command for retrieve the current status 708 * from BIOS, and userspace/firmware itself can change 709 * it. 710 * Thus we can only provide write-only access for now. 711 */ 712 return 0200; 713 714 break; 715 default: 716 break; 717 } 718 break; 719 default: 720 break; 721 } 722 723 return 0; 724 } 725 726 static int dell_smm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, 727 long *val) 728 { 729 struct dell_smm_data *data = dev_get_drvdata(dev); 730 int ret; 731 732 switch (type) { 733 case hwmon_temp: 734 switch (attr) { 735 case hwmon_temp_input: 736 ret = i8k_get_temp(channel); 737 if (ret < 0) 738 return ret; 739 740 *val = ret * 1000; 741 742 return 0; 743 default: 744 break; 745 } 746 break; 747 case hwmon_fan: 748 switch (attr) { 749 case hwmon_fan_input: 750 ret = i8k_get_fan_speed(data, channel); 751 if (ret < 0) 752 return ret; 753 754 *val = ret; 755 756 return 0; 757 case hwmon_fan_min: 758 *val = data->fan_nominal_speed[channel][0]; 759 760 return 0; 761 case hwmon_fan_max: 762 *val = data->fan_nominal_speed[channel][data->i8k_fan_max]; 763 764 return 0; 765 case hwmon_fan_target: 766 ret = i8k_get_fan_status(data, channel); 767 if (ret < 0) 768 return ret; 769 770 if (ret > data->i8k_fan_max) 771 ret = data->i8k_fan_max; 772 773 *val = data->fan_nominal_speed[channel][ret]; 774 775 return 0; 776 default: 777 break; 778 } 779 break; 780 case hwmon_pwm: 781 switch (attr) { 782 case hwmon_pwm_input: 783 ret = i8k_get_fan_status(data, channel); 784 if (ret < 0) 785 return ret; 786 787 *val = clamp_val(ret * data->i8k_pwm_mult, 0, 255); 788 789 return 0; 790 default: 791 break; 792 } 793 break; 794 default: 795 break; 796 } 797 798 return -EOPNOTSUPP; 799 } 800 801 static const char *dell_smm_fan_label(struct dell_smm_data *data, int channel) 802 { 803 bool dock = false; 804 int type = i8k_get_fan_type(data, channel); 805 806 if (type < 0) 807 return ERR_PTR(type); 808 809 if (type & 0x10) { 810 dock = true; 811 type &= 0x0F; 812 } 813 814 if (type >= ARRAY_SIZE(fan_labels)) 815 type = ARRAY_SIZE(fan_labels) - 1; 816 817 return dock ? docking_labels[type] : fan_labels[type]; 818 } 819 820 static int dell_smm_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr, 821 int channel, const char **str) 822 { 823 struct dell_smm_data *data = dev_get_drvdata(dev); 824 825 switch (type) { 826 case hwmon_temp: 827 switch (attr) { 828 case hwmon_temp_label: 829 *str = temp_labels[data->temp_type[channel]]; 830 return 0; 831 default: 832 break; 833 } 834 break; 835 case hwmon_fan: 836 switch (attr) { 837 case hwmon_fan_label: 838 *str = dell_smm_fan_label(data, channel); 839 return PTR_ERR_OR_ZERO(*str); 840 default: 841 break; 842 } 843 break; 844 default: 845 break; 846 } 847 848 return -EOPNOTSUPP; 849 } 850 851 static int dell_smm_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, 852 long val) 853 { 854 struct dell_smm_data *data = dev_get_drvdata(dev); 855 unsigned long pwm; 856 bool enable; 857 int err; 858 859 switch (type) { 860 case hwmon_pwm: 861 switch (attr) { 862 case hwmon_pwm_input: 863 pwm = clamp_val(DIV_ROUND_CLOSEST(val, data->i8k_pwm_mult), 0, 864 data->i8k_fan_max); 865 866 mutex_lock(&data->i8k_mutex); 867 err = i8k_set_fan(data, channel, pwm); 868 mutex_unlock(&data->i8k_mutex); 869 870 if (err < 0) 871 return err; 872 873 return 0; 874 case hwmon_pwm_enable: 875 if (!val) 876 return -EINVAL; 877 878 if (val == 1) 879 enable = false; 880 else 881 enable = true; 882 883 mutex_lock(&data->i8k_mutex); 884 err = i8k_enable_fan_auto_mode(data, enable); 885 mutex_unlock(&data->i8k_mutex); 886 887 if (err < 0) 888 return err; 889 890 return 0; 891 default: 892 break; 893 } 894 break; 895 default: 896 break; 897 } 898 899 return -EOPNOTSUPP; 900 } 901 902 static const struct hwmon_ops dell_smm_ops = { 903 .is_visible = dell_smm_is_visible, 904 .read = dell_smm_read, 905 .read_string = dell_smm_read_string, 906 .write = dell_smm_write, 907 }; 908 909 static const struct hwmon_channel_info *dell_smm_info[] = { 910 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ), 911 HWMON_CHANNEL_INFO(temp, 912 HWMON_T_INPUT | HWMON_T_LABEL, 913 HWMON_T_INPUT | HWMON_T_LABEL, 914 HWMON_T_INPUT | HWMON_T_LABEL, 915 HWMON_T_INPUT | HWMON_T_LABEL, 916 HWMON_T_INPUT | HWMON_T_LABEL, 917 HWMON_T_INPUT | HWMON_T_LABEL, 918 HWMON_T_INPUT | HWMON_T_LABEL, 919 HWMON_T_INPUT | HWMON_T_LABEL, 920 HWMON_T_INPUT | HWMON_T_LABEL, 921 HWMON_T_INPUT | HWMON_T_LABEL 922 ), 923 HWMON_CHANNEL_INFO(fan, 924 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX | 925 HWMON_F_TARGET, 926 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX | 927 HWMON_F_TARGET, 928 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX | 929 HWMON_F_TARGET 930 ), 931 HWMON_CHANNEL_INFO(pwm, 932 HWMON_PWM_INPUT | HWMON_PWM_ENABLE, 933 HWMON_PWM_INPUT, 934 HWMON_PWM_INPUT 935 ), 936 NULL 937 }; 938 939 static const struct hwmon_chip_info dell_smm_chip_info = { 940 .ops = &dell_smm_ops, 941 .info = dell_smm_info, 942 }; 943 944 static int __init dell_smm_init_hwmon(struct device *dev) 945 { 946 struct dell_smm_data *data = dev_get_drvdata(dev); 947 struct device *dell_smm_hwmon_dev; 948 int state, err; 949 u8 i; 950 951 for (i = 0; i < DELL_SMM_NO_TEMP; i++) { 952 data->temp_type[i] = i8k_get_temp_type(i); 953 if (data->temp_type[i] < 0) 954 continue; 955 956 if (data->temp_type[i] >= ARRAY_SIZE(temp_labels)) 957 data->temp_type[i] = ARRAY_SIZE(temp_labels) - 1; 958 } 959 960 for (i = 0; i < DELL_SMM_NO_FANS; i++) { 961 data->fan_type[i] = INT_MIN; 962 err = i8k_get_fan_status(data, i); 963 if (err < 0) 964 err = i8k_get_fan_type(data, i); 965 966 if (err < 0) 967 continue; 968 969 data->fan[i] = true; 970 data->fan_nominal_speed[i] = devm_kmalloc_array(dev, data->i8k_fan_max + 1, 971 sizeof(*data->fan_nominal_speed[i]), 972 GFP_KERNEL); 973 if (!data->fan_nominal_speed[i]) 974 continue; 975 976 for (state = 0; state <= data->i8k_fan_max; state++) { 977 err = i8k_get_fan_nominal_speed(data, i, state); 978 if (err < 0) { 979 /* Mark nominal speed table as invalid in case of error */ 980 devm_kfree(dev, data->fan_nominal_speed[i]); 981 data->fan_nominal_speed[i] = NULL; 982 break; 983 } 984 data->fan_nominal_speed[i][state] = err; 985 } 986 } 987 988 dell_smm_hwmon_dev = devm_hwmon_device_register_with_info(dev, "dell_smm", data, 989 &dell_smm_chip_info, NULL); 990 991 return PTR_ERR_OR_ZERO(dell_smm_hwmon_dev); 992 } 993 994 struct i8k_config_data { 995 uint fan_mult; 996 uint fan_max; 997 }; 998 999 enum i8k_configs { 1000 DELL_LATITUDE_D520, 1001 DELL_PRECISION_490, 1002 DELL_STUDIO, 1003 DELL_XPS, 1004 }; 1005 1006 /* 1007 * Only use for machines which need some special configuration 1008 * in order to work correctly (e.g. if autoconfig fails on this machines). 1009 */ 1010 1011 static const struct i8k_config_data i8k_config_data[] __initconst = { 1012 [DELL_LATITUDE_D520] = { 1013 .fan_mult = 1, 1014 .fan_max = I8K_FAN_TURBO, 1015 }, 1016 [DELL_PRECISION_490] = { 1017 .fan_mult = 1, 1018 .fan_max = I8K_FAN_TURBO, 1019 }, 1020 [DELL_STUDIO] = { 1021 .fan_mult = 1, 1022 .fan_max = I8K_FAN_HIGH, 1023 }, 1024 [DELL_XPS] = { 1025 .fan_mult = 1, 1026 .fan_max = I8K_FAN_HIGH, 1027 }, 1028 }; 1029 1030 static const struct dmi_system_id i8k_dmi_table[] __initconst = { 1031 { 1032 .ident = "Dell Inspiron", 1033 .matches = { 1034 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"), 1035 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"), 1036 }, 1037 }, 1038 { 1039 .ident = "Dell Latitude", 1040 .matches = { 1041 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"), 1042 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"), 1043 }, 1044 }, 1045 { 1046 .ident = "Dell Inspiron 2", 1047 .matches = { 1048 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1049 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"), 1050 }, 1051 }, 1052 { 1053 .ident = "Dell Latitude D520", 1054 .matches = { 1055 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1056 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"), 1057 }, 1058 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520], 1059 }, 1060 { 1061 .ident = "Dell Latitude 2", 1062 .matches = { 1063 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1064 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"), 1065 }, 1066 }, 1067 { /* UK Inspiron 6400 */ 1068 .ident = "Dell Inspiron 3", 1069 .matches = { 1070 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1071 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"), 1072 }, 1073 }, 1074 { 1075 .ident = "Dell Inspiron 3", 1076 .matches = { 1077 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1078 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"), 1079 }, 1080 }, 1081 { 1082 .ident = "Dell Precision 490", 1083 .matches = { 1084 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1085 DMI_MATCH(DMI_PRODUCT_NAME, 1086 "Precision WorkStation 490"), 1087 }, 1088 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490], 1089 }, 1090 { 1091 .ident = "Dell Precision", 1092 .matches = { 1093 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1094 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"), 1095 }, 1096 }, 1097 { 1098 .ident = "Dell Vostro", 1099 .matches = { 1100 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1101 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"), 1102 }, 1103 }, 1104 { 1105 .ident = "Dell Studio", 1106 .matches = { 1107 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1108 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"), 1109 }, 1110 .driver_data = (void *)&i8k_config_data[DELL_STUDIO], 1111 }, 1112 { 1113 .ident = "Dell XPS M140", 1114 .matches = { 1115 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1116 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"), 1117 }, 1118 .driver_data = (void *)&i8k_config_data[DELL_XPS], 1119 }, 1120 { 1121 .ident = "Dell XPS", 1122 .matches = { 1123 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1124 DMI_MATCH(DMI_PRODUCT_NAME, "XPS"), 1125 }, 1126 }, 1127 { } 1128 }; 1129 1130 MODULE_DEVICE_TABLE(dmi, i8k_dmi_table); 1131 1132 /* 1133 * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed 1134 * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist 1135 * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call. 1136 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121 1137 */ 1138 static const struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initconst = { 1139 { 1140 .ident = "Dell Studio XPS 8000", 1141 .matches = { 1142 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1143 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"), 1144 }, 1145 }, 1146 { 1147 .ident = "Dell Studio XPS 8100", 1148 .matches = { 1149 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1150 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"), 1151 }, 1152 }, 1153 { 1154 .ident = "Dell Inspiron 580", 1155 .matches = { 1156 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1157 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "), 1158 }, 1159 }, 1160 { 1161 .ident = "Dell Inspiron 3505", 1162 .matches = { 1163 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1164 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 3505"), 1165 }, 1166 }, 1167 { } 1168 }; 1169 1170 /* 1171 * On some machines all fan related SMM functions implemented by Dell BIOS 1172 * firmware freeze kernel for about 500ms. Until Dell fixes these problems fan 1173 * support for affected blacklisted Dell machines stay disabled. 1174 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=195751 1175 */ 1176 static const struct dmi_system_id i8k_blacklist_fan_support_dmi_table[] __initconst = { 1177 { 1178 .ident = "Dell Inspiron 7720", 1179 .matches = { 1180 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1181 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"), 1182 }, 1183 }, 1184 { 1185 .ident = "Dell Vostro 3360", 1186 .matches = { 1187 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1188 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"), 1189 }, 1190 }, 1191 { 1192 .ident = "Dell XPS13 9333", 1193 .matches = { 1194 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1195 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"), 1196 }, 1197 }, 1198 { 1199 .ident = "Dell XPS 15 L502X", 1200 .matches = { 1201 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1202 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Dell System XPS L502X"), 1203 }, 1204 }, 1205 { } 1206 }; 1207 1208 struct i8k_fan_control_data { 1209 unsigned int manual_fan; 1210 unsigned int auto_fan; 1211 }; 1212 1213 enum i8k_fan_controls { 1214 I8K_FAN_34A3_35A3, 1215 }; 1216 1217 static const struct i8k_fan_control_data i8k_fan_control_data[] __initconst = { 1218 [I8K_FAN_34A3_35A3] = { 1219 .manual_fan = 0x34a3, 1220 .auto_fan = 0x35a3, 1221 }, 1222 }; 1223 1224 static const struct dmi_system_id i8k_whitelist_fan_control[] __initconst = { 1225 { 1226 .ident = "Dell Latitude 5480", 1227 .matches = { 1228 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1229 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude 5480"), 1230 }, 1231 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3], 1232 }, 1233 { 1234 .ident = "Dell Latitude E6440", 1235 .matches = { 1236 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1237 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E6440"), 1238 }, 1239 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3], 1240 }, 1241 { 1242 .ident = "Dell Latitude E7440", 1243 .matches = { 1244 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1245 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E7440"), 1246 }, 1247 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3], 1248 }, 1249 { 1250 .ident = "Dell Precision 5530", 1251 .matches = { 1252 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1253 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 5530"), 1254 }, 1255 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3], 1256 }, 1257 { 1258 .ident = "Dell Precision 7510", 1259 .matches = { 1260 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1261 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 7510"), 1262 }, 1263 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3], 1264 }, 1265 { } 1266 }; 1267 1268 static int __init dell_smm_probe(struct platform_device *pdev) 1269 { 1270 struct dell_smm_data *data; 1271 const struct dmi_system_id *id, *fan_control; 1272 int ret; 1273 u8 fan; 1274 1275 data = devm_kzalloc(&pdev->dev, sizeof(struct dell_smm_data), GFP_KERNEL); 1276 if (!data) 1277 return -ENOMEM; 1278 1279 mutex_init(&data->i8k_mutex); 1280 data->i8k_fan_mult = I8K_FAN_MULT; 1281 data->i8k_fan_max = I8K_FAN_HIGH; 1282 platform_set_drvdata(pdev, data); 1283 1284 if (dmi_check_system(i8k_blacklist_fan_support_dmi_table)) { 1285 dev_warn(&pdev->dev, "broken Dell BIOS detected, disallow fan support\n"); 1286 if (!force) 1287 data->disallow_fan_support = true; 1288 } 1289 1290 if (dmi_check_system(i8k_blacklist_fan_type_dmi_table)) { 1291 dev_warn(&pdev->dev, "broken Dell BIOS detected, disallow fan type call\n"); 1292 if (!force) 1293 data->disallow_fan_type_call = true; 1294 } 1295 1296 strscpy(data->bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION), 1297 sizeof(data->bios_version)); 1298 strscpy(data->bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL), 1299 sizeof(data->bios_machineid)); 1300 1301 /* 1302 * Set fan multiplier and maximal fan speed from dmi config 1303 * Values specified in module parameters override values from dmi 1304 */ 1305 id = dmi_first_match(i8k_dmi_table); 1306 if (id && id->driver_data) { 1307 const struct i8k_config_data *conf = id->driver_data; 1308 1309 if (!fan_mult && conf->fan_mult) 1310 fan_mult = conf->fan_mult; 1311 1312 if (!fan_max && conf->fan_max) 1313 fan_max = conf->fan_max; 1314 } 1315 1316 data->i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */ 1317 data->i8k_pwm_mult = DIV_ROUND_UP(255, data->i8k_fan_max); 1318 1319 fan_control = dmi_first_match(i8k_whitelist_fan_control); 1320 if (fan_control && fan_control->driver_data) { 1321 const struct i8k_fan_control_data *control = fan_control->driver_data; 1322 1323 data->manual_fan = control->manual_fan; 1324 data->auto_fan = control->auto_fan; 1325 dev_info(&pdev->dev, "enabling support for setting automatic/manual fan control\n"); 1326 } 1327 1328 if (!fan_mult) { 1329 /* 1330 * Autodetect fan multiplier based on nominal rpm 1331 * If fan reports rpm value too high then set multiplier to 1 1332 */ 1333 for (fan = 0; fan < DELL_SMM_NO_FANS; ++fan) { 1334 ret = i8k_get_fan_nominal_speed(data, fan, data->i8k_fan_max); 1335 if (ret < 0) 1336 continue; 1337 1338 if (ret > I8K_FAN_MAX_RPM) 1339 data->i8k_fan_mult = 1; 1340 break; 1341 } 1342 } else { 1343 /* Fan multiplier was specified in module param or in dmi */ 1344 data->i8k_fan_mult = fan_mult; 1345 } 1346 1347 ret = dell_smm_init_hwmon(&pdev->dev); 1348 if (ret) 1349 return ret; 1350 1351 i8k_init_procfs(&pdev->dev); 1352 1353 return 0; 1354 } 1355 1356 static struct platform_driver dell_smm_driver = { 1357 .driver = { 1358 .name = KBUILD_MODNAME, 1359 }, 1360 }; 1361 1362 static struct platform_device *dell_smm_device; 1363 1364 /* 1365 * Probe for the presence of a supported laptop. 1366 */ 1367 static int __init i8k_init(void) 1368 { 1369 /* 1370 * Get DMI information 1371 */ 1372 if (!dmi_check_system(i8k_dmi_table)) { 1373 if (!ignore_dmi && !force) 1374 return -ENODEV; 1375 1376 pr_info("not running on a supported Dell system.\n"); 1377 pr_info("vendor=%s, model=%s, version=%s\n", 1378 i8k_get_dmi_data(DMI_SYS_VENDOR), 1379 i8k_get_dmi_data(DMI_PRODUCT_NAME), 1380 i8k_get_dmi_data(DMI_BIOS_VERSION)); 1381 } 1382 1383 /* 1384 * Get SMM Dell signature 1385 */ 1386 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) && 1387 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) { 1388 pr_err("unable to get SMM Dell signature\n"); 1389 if (!force) 1390 return -ENODEV; 1391 } 1392 1393 dell_smm_device = platform_create_bundle(&dell_smm_driver, dell_smm_probe, NULL, 0, NULL, 1394 0); 1395 1396 return PTR_ERR_OR_ZERO(dell_smm_device); 1397 } 1398 1399 static void __exit i8k_exit(void) 1400 { 1401 platform_device_unregister(dell_smm_device); 1402 platform_driver_unregister(&dell_smm_driver); 1403 } 1404 1405 module_init(i8k_init); 1406 module_exit(i8k_exit); 1407