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