0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0014
0015 #include <linux/capability.h>
0016 #include <linux/cpu.h>
0017 #include <linux/ctype.h>
0018 #include <linux/delay.h>
0019 #include <linux/dmi.h>
0020 #include <linux/err.h>
0021 #include <linux/errno.h>
0022 #include <linux/hwmon.h>
0023 #include <linux/init.h>
0024 #include <linux/kconfig.h>
0025 #include <linux/kernel.h>
0026 #include <linux/module.h>
0027 #include <linux/mutex.h>
0028 #include <linux/platform_device.h>
0029 #include <linux/proc_fs.h>
0030 #include <linux/seq_file.h>
0031 #include <linux/slab.h>
0032 #include <linux/smp.h>
0033 #include <linux/string.h>
0034 #include <linux/thermal.h>
0035 #include <linux/types.h>
0036 #include <linux/uaccess.h>
0037
0038 #include <linux/i8k.h>
0039
0040 #define I8K_SMM_FN_STATUS 0x0025
0041 #define I8K_SMM_POWER_STATUS 0x0069
0042 #define I8K_SMM_SET_FAN 0x01a3
0043 #define I8K_SMM_GET_FAN 0x00a3
0044 #define I8K_SMM_GET_SPEED 0x02a3
0045 #define I8K_SMM_GET_FAN_TYPE 0x03a3
0046 #define I8K_SMM_GET_NOM_SPEED 0x04a3
0047 #define I8K_SMM_GET_TEMP 0x10a3
0048 #define I8K_SMM_GET_TEMP_TYPE 0x11a3
0049 #define I8K_SMM_GET_DELL_SIG1 0xfea3
0050 #define I8K_SMM_GET_DELL_SIG2 0xffa3
0051
0052
0053 #define DELL_SMM_MAX_DURATION 250000
0054
0055 #define I8K_FAN_MULT 30
0056 #define I8K_FAN_RPM_THRESHOLD 1000
0057 #define I8K_MAX_TEMP 127
0058
0059 #define I8K_FN_NONE 0x00
0060 #define I8K_FN_UP 0x01
0061 #define I8K_FN_DOWN 0x02
0062 #define I8K_FN_MUTE 0x04
0063 #define I8K_FN_MASK 0x07
0064 #define I8K_FN_SHIFT 8
0065
0066 #define I8K_POWER_AC 0x05
0067 #define I8K_POWER_BATTERY 0x01
0068
0069 #define DELL_SMM_NO_TEMP 10
0070 #define DELL_SMM_NO_FANS 3
0071
0072 struct dell_smm_data {
0073 struct mutex i8k_mutex;
0074 char bios_version[4];
0075 char bios_machineid[16];
0076 uint i8k_fan_mult;
0077 uint i8k_pwm_mult;
0078 uint i8k_fan_max;
0079 bool disallow_fan_type_call;
0080 bool disallow_fan_support;
0081 unsigned int manual_fan;
0082 unsigned int auto_fan;
0083 int temp_type[DELL_SMM_NO_TEMP];
0084 bool fan[DELL_SMM_NO_FANS];
0085 int fan_type[DELL_SMM_NO_FANS];
0086 int *fan_nominal_speed[DELL_SMM_NO_FANS];
0087 };
0088
0089 struct dell_smm_cooling_data {
0090 u8 fan_num;
0091 struct dell_smm_data *data;
0092 };
0093
0094 MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
0095 MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
0096 MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
0097 MODULE_LICENSE("GPL");
0098 MODULE_ALIAS("i8k");
0099
0100 static bool force;
0101 module_param_unsafe(force, bool, 0);
0102 MODULE_PARM_DESC(force, "Force loading without checking for supported models and features");
0103
0104 static bool ignore_dmi;
0105 module_param(ignore_dmi, bool, 0);
0106 MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
0107
0108 #if IS_ENABLED(CONFIG_I8K)
0109 static bool restricted = true;
0110 module_param(restricted, bool, 0);
0111 MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
0112
0113 static bool power_status;
0114 module_param(power_status, bool, 0600);
0115 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
0116 #endif
0117
0118 static uint fan_mult;
0119 module_param(fan_mult, uint, 0);
0120 MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
0121
0122 static uint fan_max;
0123 module_param(fan_max, uint, 0);
0124 MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
0125
0126 struct smm_regs {
0127 unsigned int eax;
0128 unsigned int ebx;
0129 unsigned int ecx;
0130 unsigned int edx;
0131 unsigned int esi;
0132 unsigned int edi;
0133 };
0134
0135 static const char * const temp_labels[] = {
0136 "CPU",
0137 "GPU",
0138 "SODIMM",
0139 "Other",
0140 "Ambient",
0141 "Other",
0142 };
0143
0144 static const char * const fan_labels[] = {
0145 "Processor Fan",
0146 "Motherboard Fan",
0147 "Video Fan",
0148 "Power Supply Fan",
0149 "Chipset Fan",
0150 "Other Fan",
0151 };
0152
0153 static const char * const docking_labels[] = {
0154 "Docking Processor Fan",
0155 "Docking Motherboard Fan",
0156 "Docking Video Fan",
0157 "Docking Power Supply Fan",
0158 "Docking Chipset Fan",
0159 "Docking Other Fan",
0160 };
0161
0162 static inline const char __init *i8k_get_dmi_data(int field)
0163 {
0164 const char *dmi_data = dmi_get_system_info(field);
0165
0166 return dmi_data && *dmi_data ? dmi_data : "?";
0167 }
0168
0169
0170
0171
0172 static int i8k_smm_func(void *par)
0173 {
0174 ktime_t calltime = ktime_get();
0175 struct smm_regs *regs = par;
0176 int eax = regs->eax;
0177 int ebx = regs->ebx;
0178 unsigned char carry;
0179 long long duration;
0180
0181
0182 if (smp_processor_id() != 0)
0183 return -EBUSY;
0184
0185 asm volatile("out %%al,$0xb2\n\t"
0186 "out %%al,$0x84\n\t"
0187 "setc %0\n"
0188 : "=mr" (carry),
0189 "+a" (regs->eax),
0190 "+b" (regs->ebx),
0191 "+c" (regs->ecx),
0192 "+d" (regs->edx),
0193 "+S" (regs->esi),
0194 "+D" (regs->edi));
0195
0196 duration = ktime_us_delta(ktime_get(), calltime);
0197 pr_debug("smm(0x%.4x 0x%.4x) = 0x%.4x carry: %d (took %7lld usecs)\n",
0198 eax, ebx, regs->eax & 0xffff, carry, duration);
0199
0200 if (duration > DELL_SMM_MAX_DURATION)
0201 pr_warn_once("SMM call took %lld usecs!\n", duration);
0202
0203 if (carry || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
0204 return -EINVAL;
0205
0206 return 0;
0207 }
0208
0209
0210
0211
0212 static int i8k_smm(struct smm_regs *regs)
0213 {
0214 int ret;
0215
0216 cpus_read_lock();
0217 ret = smp_call_on_cpu(0, i8k_smm_func, regs, true);
0218 cpus_read_unlock();
0219
0220 return ret;
0221 }
0222
0223
0224
0225
0226 static int i8k_get_fan_status(const struct dell_smm_data *data, u8 fan)
0227 {
0228 struct smm_regs regs = {
0229 .eax = I8K_SMM_GET_FAN,
0230 .ebx = fan,
0231 };
0232
0233 if (data->disallow_fan_support)
0234 return -EINVAL;
0235
0236 return i8k_smm(®s) ? : regs.eax & 0xff;
0237 }
0238
0239
0240
0241
0242 static int i8k_get_fan_speed(const struct dell_smm_data *data, u8 fan)
0243 {
0244 struct smm_regs regs = {
0245 .eax = I8K_SMM_GET_SPEED,
0246 .ebx = fan,
0247 };
0248
0249 if (data->disallow_fan_support)
0250 return -EINVAL;
0251
0252 return i8k_smm(®s) ? : (regs.eax & 0xffff) * data->i8k_fan_mult;
0253 }
0254
0255
0256
0257
0258 static int _i8k_get_fan_type(const struct dell_smm_data *data, u8 fan)
0259 {
0260 struct smm_regs regs = {
0261 .eax = I8K_SMM_GET_FAN_TYPE,
0262 .ebx = fan,
0263 };
0264
0265 if (data->disallow_fan_support || data->disallow_fan_type_call)
0266 return -EINVAL;
0267
0268 return i8k_smm(®s) ? : regs.eax & 0xff;
0269 }
0270
0271 static int i8k_get_fan_type(struct dell_smm_data *data, u8 fan)
0272 {
0273
0274 if (data->fan_type[fan] == INT_MIN)
0275 data->fan_type[fan] = _i8k_get_fan_type(data, fan);
0276
0277 return data->fan_type[fan];
0278 }
0279
0280
0281
0282
0283 static int __init i8k_get_fan_nominal_speed(const struct dell_smm_data *data, u8 fan, int speed)
0284 {
0285 struct smm_regs regs = {
0286 .eax = I8K_SMM_GET_NOM_SPEED,
0287 .ebx = fan | (speed << 8),
0288 };
0289
0290 if (data->disallow_fan_support)
0291 return -EINVAL;
0292
0293 return i8k_smm(®s) ? : (regs.eax & 0xffff);
0294 }
0295
0296
0297
0298
0299 static int i8k_enable_fan_auto_mode(const struct dell_smm_data *data, bool enable)
0300 {
0301 struct smm_regs regs = { };
0302
0303 if (data->disallow_fan_support)
0304 return -EINVAL;
0305
0306 regs.eax = enable ? data->auto_fan : data->manual_fan;
0307 return i8k_smm(®s);
0308 }
0309
0310
0311
0312
0313 static int i8k_set_fan(const struct dell_smm_data *data, u8 fan, int speed)
0314 {
0315 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
0316
0317 if (data->disallow_fan_support)
0318 return -EINVAL;
0319
0320 speed = (speed < 0) ? 0 : ((speed > data->i8k_fan_max) ? data->i8k_fan_max : speed);
0321 regs.ebx = fan | (speed << 8);
0322
0323 return i8k_smm(®s);
0324 }
0325
0326 static int __init i8k_get_temp_type(u8 sensor)
0327 {
0328 struct smm_regs regs = {
0329 .eax = I8K_SMM_GET_TEMP_TYPE,
0330 .ebx = sensor,
0331 };
0332
0333 return i8k_smm(®s) ? : regs.eax & 0xff;
0334 }
0335
0336
0337
0338
0339 static int _i8k_get_temp(u8 sensor)
0340 {
0341 struct smm_regs regs = {
0342 .eax = I8K_SMM_GET_TEMP,
0343 .ebx = sensor,
0344 };
0345
0346 return i8k_smm(®s) ? : regs.eax & 0xff;
0347 }
0348
0349 static int i8k_get_temp(u8 sensor)
0350 {
0351 int temp = _i8k_get_temp(sensor);
0352
0353
0354
0355
0356
0357
0358
0359
0360 if (temp == 0x99) {
0361 msleep(100);
0362 temp = _i8k_get_temp(sensor);
0363 }
0364
0365
0366
0367
0368
0369
0370
0371
0372 if (temp > I8K_MAX_TEMP)
0373 return -ENODATA;
0374
0375 return temp;
0376 }
0377
0378 static int __init i8k_get_dell_signature(int req_fn)
0379 {
0380 struct smm_regs regs = { .eax = req_fn, };
0381 int rc;
0382
0383 rc = i8k_smm(®s);
0384 if (rc < 0)
0385 return rc;
0386
0387 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
0388 }
0389
0390 #if IS_ENABLED(CONFIG_I8K)
0391
0392
0393
0394
0395 static int i8k_get_fn_status(void)
0396 {
0397 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
0398 int rc;
0399
0400 rc = i8k_smm(®s);
0401 if (rc < 0)
0402 return rc;
0403
0404 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
0405 case I8K_FN_UP:
0406 return I8K_VOL_UP;
0407 case I8K_FN_DOWN:
0408 return I8K_VOL_DOWN;
0409 case I8K_FN_MUTE:
0410 return I8K_VOL_MUTE;
0411 default:
0412 return 0;
0413 }
0414 }
0415
0416
0417
0418
0419 static int i8k_get_power_status(void)
0420 {
0421 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
0422 int rc;
0423
0424 rc = i8k_smm(®s);
0425 if (rc < 0)
0426 return rc;
0427
0428 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
0429 }
0430
0431
0432
0433
0434
0435 static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
0436 {
0437 struct dell_smm_data *data = pde_data(file_inode(fp));
0438 int __user *argp = (int __user *)arg;
0439 int speed, err;
0440 int val = 0;
0441
0442 if (!argp)
0443 return -EINVAL;
0444
0445 switch (cmd) {
0446 case I8K_BIOS_VERSION:
0447 if (!isdigit(data->bios_version[0]) || !isdigit(data->bios_version[1]) ||
0448 !isdigit(data->bios_version[2]))
0449 return -EINVAL;
0450
0451 val = (data->bios_version[0] << 16) |
0452 (data->bios_version[1] << 8) | data->bios_version[2];
0453
0454 if (copy_to_user(argp, &val, sizeof(val)))
0455 return -EFAULT;
0456
0457 return 0;
0458 case I8K_MACHINE_ID:
0459 if (restricted && !capable(CAP_SYS_ADMIN))
0460 return -EPERM;
0461
0462 if (copy_to_user(argp, data->bios_machineid, sizeof(data->bios_machineid)))
0463 return -EFAULT;
0464
0465 return 0;
0466 case I8K_FN_STATUS:
0467 val = i8k_get_fn_status();
0468 break;
0469
0470 case I8K_POWER_STATUS:
0471 val = i8k_get_power_status();
0472 break;
0473
0474 case I8K_GET_TEMP:
0475 val = i8k_get_temp(0);
0476 break;
0477
0478 case I8K_GET_SPEED:
0479 if (copy_from_user(&val, argp, sizeof(int)))
0480 return -EFAULT;
0481
0482 if (val > U8_MAX || val < 0)
0483 return -EINVAL;
0484
0485 val = i8k_get_fan_speed(data, val);
0486 break;
0487
0488 case I8K_GET_FAN:
0489 if (copy_from_user(&val, argp, sizeof(int)))
0490 return -EFAULT;
0491
0492 if (val > U8_MAX || val < 0)
0493 return -EINVAL;
0494
0495 val = i8k_get_fan_status(data, val);
0496 break;
0497
0498 case I8K_SET_FAN:
0499 if (restricted && !capable(CAP_SYS_ADMIN))
0500 return -EPERM;
0501
0502 if (copy_from_user(&val, argp, sizeof(int)))
0503 return -EFAULT;
0504
0505 if (val > U8_MAX || val < 0)
0506 return -EINVAL;
0507
0508 if (copy_from_user(&speed, argp + 1, sizeof(int)))
0509 return -EFAULT;
0510
0511 mutex_lock(&data->i8k_mutex);
0512 err = i8k_set_fan(data, val, speed);
0513 if (err < 0)
0514 val = err;
0515 else
0516 val = i8k_get_fan_status(data, val);
0517 mutex_unlock(&data->i8k_mutex);
0518 break;
0519
0520 default:
0521 return -ENOIOCTLCMD;
0522 }
0523
0524 if (val < 0)
0525 return val;
0526
0527 if (copy_to_user(argp, &val, sizeof(int)))
0528 return -EFAULT;
0529
0530 return 0;
0531 }
0532
0533
0534
0535
0536 static int i8k_proc_show(struct seq_file *seq, void *offset)
0537 {
0538 struct dell_smm_data *data = seq->private;
0539 int fn_key, cpu_temp, ac_power;
0540 int left_fan, right_fan, left_speed, right_speed;
0541
0542 cpu_temp = i8k_get_temp(0);
0543 left_fan = i8k_get_fan_status(data, I8K_FAN_LEFT);
0544 right_fan = i8k_get_fan_status(data, I8K_FAN_RIGHT);
0545 left_speed = i8k_get_fan_speed(data, I8K_FAN_LEFT);
0546 right_speed = i8k_get_fan_speed(data, I8K_FAN_RIGHT);
0547 fn_key = i8k_get_fn_status();
0548 if (power_status)
0549 ac_power = i8k_get_power_status();
0550 else
0551 ac_power = -1;
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567 seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
0568 I8K_PROC_FMT,
0569 data->bios_version,
0570 (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : data->bios_machineid,
0571 cpu_temp,
0572 left_fan, right_fan, left_speed, right_speed,
0573 ac_power, fn_key);
0574
0575 return 0;
0576 }
0577
0578 static int i8k_open_fs(struct inode *inode, struct file *file)
0579 {
0580 return single_open(file, i8k_proc_show, pde_data(inode));
0581 }
0582
0583 static const struct proc_ops i8k_proc_ops = {
0584 .proc_open = i8k_open_fs,
0585 .proc_read = seq_read,
0586 .proc_lseek = seq_lseek,
0587 .proc_release = single_release,
0588 .proc_ioctl = i8k_ioctl,
0589 };
0590
0591 static void i8k_exit_procfs(void *param)
0592 {
0593 remove_proc_entry("i8k", NULL);
0594 }
0595
0596 static void __init i8k_init_procfs(struct device *dev)
0597 {
0598 struct dell_smm_data *data = dev_get_drvdata(dev);
0599
0600
0601 if (proc_create_data("i8k", 0, NULL, &i8k_proc_ops, data))
0602 devm_add_action_or_reset(dev, i8k_exit_procfs, NULL);
0603 }
0604
0605 #else
0606
0607 static void __init i8k_init_procfs(struct device *dev)
0608 {
0609 }
0610
0611 #endif
0612
0613 static int dell_smm_get_max_state(struct thermal_cooling_device *dev, unsigned long *state)
0614 {
0615 struct dell_smm_cooling_data *cdata = dev->devdata;
0616
0617 *state = cdata->data->i8k_fan_max;
0618
0619 return 0;
0620 }
0621
0622 static int dell_smm_get_cur_state(struct thermal_cooling_device *dev, unsigned long *state)
0623 {
0624 struct dell_smm_cooling_data *cdata = dev->devdata;
0625 int ret;
0626
0627 ret = i8k_get_fan_status(cdata->data, cdata->fan_num);
0628 if (ret < 0)
0629 return ret;
0630
0631 *state = ret;
0632
0633 return 0;
0634 }
0635
0636 static int dell_smm_set_cur_state(struct thermal_cooling_device *dev, unsigned long state)
0637 {
0638 struct dell_smm_cooling_data *cdata = dev->devdata;
0639 struct dell_smm_data *data = cdata->data;
0640 int ret;
0641
0642 if (state > data->i8k_fan_max)
0643 return -EINVAL;
0644
0645 mutex_lock(&data->i8k_mutex);
0646 ret = i8k_set_fan(data, cdata->fan_num, (int)state);
0647 mutex_unlock(&data->i8k_mutex);
0648
0649 return ret;
0650 }
0651
0652 static const struct thermal_cooling_device_ops dell_smm_cooling_ops = {
0653 .get_max_state = dell_smm_get_max_state,
0654 .get_cur_state = dell_smm_get_cur_state,
0655 .set_cur_state = dell_smm_set_cur_state,
0656 };
0657
0658 static umode_t dell_smm_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr,
0659 int channel)
0660 {
0661 const struct dell_smm_data *data = drvdata;
0662
0663 switch (type) {
0664 case hwmon_temp:
0665 switch (attr) {
0666 case hwmon_temp_input:
0667
0668 if (data->temp_type[channel] >= 0 || _i8k_get_temp(channel) >= 0)
0669 return 0444;
0670
0671 break;
0672 case hwmon_temp_label:
0673 if (data->temp_type[channel] >= 0)
0674 return 0444;
0675
0676 break;
0677 default:
0678 break;
0679 }
0680 break;
0681 case hwmon_fan:
0682 if (data->disallow_fan_support)
0683 break;
0684
0685 switch (attr) {
0686 case hwmon_fan_input:
0687 if (data->fan[channel])
0688 return 0444;
0689
0690 break;
0691 case hwmon_fan_label:
0692 if (data->fan[channel] && !data->disallow_fan_type_call)
0693 return 0444;
0694
0695 break;
0696 case hwmon_fan_min:
0697 case hwmon_fan_max:
0698 case hwmon_fan_target:
0699 if (data->fan_nominal_speed[channel])
0700 return 0444;
0701
0702 break;
0703 default:
0704 break;
0705 }
0706 break;
0707 case hwmon_pwm:
0708 if (data->disallow_fan_support)
0709 break;
0710
0711 switch (attr) {
0712 case hwmon_pwm_input:
0713 if (data->fan[channel])
0714 return 0644;
0715
0716 break;
0717 case hwmon_pwm_enable:
0718 if (data->auto_fan)
0719
0720
0721
0722
0723
0724
0725 return 0200;
0726
0727 break;
0728 default:
0729 break;
0730 }
0731 break;
0732 default:
0733 break;
0734 }
0735
0736 return 0;
0737 }
0738
0739 static int dell_smm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
0740 long *val)
0741 {
0742 struct dell_smm_data *data = dev_get_drvdata(dev);
0743 int mult = data->i8k_fan_mult;
0744 int ret;
0745
0746 switch (type) {
0747 case hwmon_temp:
0748 switch (attr) {
0749 case hwmon_temp_input:
0750 ret = i8k_get_temp(channel);
0751 if (ret < 0)
0752 return ret;
0753
0754 *val = ret * 1000;
0755
0756 return 0;
0757 default:
0758 break;
0759 }
0760 break;
0761 case hwmon_fan:
0762 switch (attr) {
0763 case hwmon_fan_input:
0764 ret = i8k_get_fan_speed(data, channel);
0765 if (ret < 0)
0766 return ret;
0767
0768 *val = ret;
0769
0770 return 0;
0771 case hwmon_fan_min:
0772 *val = data->fan_nominal_speed[channel][0] * mult;
0773
0774 return 0;
0775 case hwmon_fan_max:
0776 *val = data->fan_nominal_speed[channel][data->i8k_fan_max] * mult;
0777
0778 return 0;
0779 case hwmon_fan_target:
0780 ret = i8k_get_fan_status(data, channel);
0781 if (ret < 0)
0782 return ret;
0783
0784 if (ret > data->i8k_fan_max)
0785 ret = data->i8k_fan_max;
0786
0787 *val = data->fan_nominal_speed[channel][ret] * mult;
0788
0789 return 0;
0790 default:
0791 break;
0792 }
0793 break;
0794 case hwmon_pwm:
0795 switch (attr) {
0796 case hwmon_pwm_input:
0797 ret = i8k_get_fan_status(data, channel);
0798 if (ret < 0)
0799 return ret;
0800
0801 *val = clamp_val(ret * data->i8k_pwm_mult, 0, 255);
0802
0803 return 0;
0804 default:
0805 break;
0806 }
0807 break;
0808 default:
0809 break;
0810 }
0811
0812 return -EOPNOTSUPP;
0813 }
0814
0815 static const char *dell_smm_fan_label(struct dell_smm_data *data, int channel)
0816 {
0817 bool dock = false;
0818 int type = i8k_get_fan_type(data, channel);
0819
0820 if (type < 0)
0821 return ERR_PTR(type);
0822
0823 if (type & 0x10) {
0824 dock = true;
0825 type &= 0x0F;
0826 }
0827
0828 if (type >= ARRAY_SIZE(fan_labels))
0829 type = ARRAY_SIZE(fan_labels) - 1;
0830
0831 return dock ? docking_labels[type] : fan_labels[type];
0832 }
0833
0834 static int dell_smm_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
0835 int channel, const char **str)
0836 {
0837 struct dell_smm_data *data = dev_get_drvdata(dev);
0838
0839 switch (type) {
0840 case hwmon_temp:
0841 switch (attr) {
0842 case hwmon_temp_label:
0843 *str = temp_labels[data->temp_type[channel]];
0844 return 0;
0845 default:
0846 break;
0847 }
0848 break;
0849 case hwmon_fan:
0850 switch (attr) {
0851 case hwmon_fan_label:
0852 *str = dell_smm_fan_label(data, channel);
0853 return PTR_ERR_OR_ZERO(*str);
0854 default:
0855 break;
0856 }
0857 break;
0858 default:
0859 break;
0860 }
0861
0862 return -EOPNOTSUPP;
0863 }
0864
0865 static int dell_smm_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
0866 long val)
0867 {
0868 struct dell_smm_data *data = dev_get_drvdata(dev);
0869 unsigned long pwm;
0870 bool enable;
0871 int err;
0872
0873 switch (type) {
0874 case hwmon_pwm:
0875 switch (attr) {
0876 case hwmon_pwm_input:
0877 pwm = clamp_val(DIV_ROUND_CLOSEST(val, data->i8k_pwm_mult), 0,
0878 data->i8k_fan_max);
0879
0880 mutex_lock(&data->i8k_mutex);
0881 err = i8k_set_fan(data, channel, pwm);
0882 mutex_unlock(&data->i8k_mutex);
0883
0884 if (err < 0)
0885 return err;
0886
0887 return 0;
0888 case hwmon_pwm_enable:
0889 if (!val)
0890 return -EINVAL;
0891
0892 if (val == 1)
0893 enable = false;
0894 else
0895 enable = true;
0896
0897 mutex_lock(&data->i8k_mutex);
0898 err = i8k_enable_fan_auto_mode(data, enable);
0899 mutex_unlock(&data->i8k_mutex);
0900
0901 if (err < 0)
0902 return err;
0903
0904 return 0;
0905 default:
0906 break;
0907 }
0908 break;
0909 default:
0910 break;
0911 }
0912
0913 return -EOPNOTSUPP;
0914 }
0915
0916 static const struct hwmon_ops dell_smm_ops = {
0917 .is_visible = dell_smm_is_visible,
0918 .read = dell_smm_read,
0919 .read_string = dell_smm_read_string,
0920 .write = dell_smm_write,
0921 };
0922
0923 static const struct hwmon_channel_info *dell_smm_info[] = {
0924 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
0925 HWMON_CHANNEL_INFO(temp,
0926 HWMON_T_INPUT | HWMON_T_LABEL,
0927 HWMON_T_INPUT | HWMON_T_LABEL,
0928 HWMON_T_INPUT | HWMON_T_LABEL,
0929 HWMON_T_INPUT | HWMON_T_LABEL,
0930 HWMON_T_INPUT | HWMON_T_LABEL,
0931 HWMON_T_INPUT | HWMON_T_LABEL,
0932 HWMON_T_INPUT | HWMON_T_LABEL,
0933 HWMON_T_INPUT | HWMON_T_LABEL,
0934 HWMON_T_INPUT | HWMON_T_LABEL,
0935 HWMON_T_INPUT | HWMON_T_LABEL
0936 ),
0937 HWMON_CHANNEL_INFO(fan,
0938 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
0939 HWMON_F_TARGET,
0940 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
0941 HWMON_F_TARGET,
0942 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
0943 HWMON_F_TARGET
0944 ),
0945 HWMON_CHANNEL_INFO(pwm,
0946 HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
0947 HWMON_PWM_INPUT,
0948 HWMON_PWM_INPUT
0949 ),
0950 NULL
0951 };
0952
0953 static const struct hwmon_chip_info dell_smm_chip_info = {
0954 .ops = &dell_smm_ops,
0955 .info = dell_smm_info,
0956 };
0957
0958 static int __init dell_smm_init_cdev(struct device *dev, u8 fan_num)
0959 {
0960 struct dell_smm_data *data = dev_get_drvdata(dev);
0961 struct thermal_cooling_device *cdev;
0962 struct dell_smm_cooling_data *cdata;
0963 int ret = 0;
0964 char *name;
0965
0966 name = kasprintf(GFP_KERNEL, "dell-smm-fan%u", fan_num + 1);
0967 if (!name)
0968 return -ENOMEM;
0969
0970 cdata = devm_kmalloc(dev, sizeof(*cdata), GFP_KERNEL);
0971 if (cdata) {
0972 cdata->fan_num = fan_num;
0973 cdata->data = data;
0974 cdev = devm_thermal_of_cooling_device_register(dev, NULL, name, cdata,
0975 &dell_smm_cooling_ops);
0976 if (IS_ERR(cdev)) {
0977 devm_kfree(dev, cdata);
0978 ret = PTR_ERR(cdev);
0979 }
0980 } else {
0981 ret = -ENOMEM;
0982 }
0983
0984 kfree(name);
0985
0986 return ret;
0987 }
0988
0989 static int __init dell_smm_init_hwmon(struct device *dev)
0990 {
0991 struct dell_smm_data *data = dev_get_drvdata(dev);
0992 struct device *dell_smm_hwmon_dev;
0993 int state, err;
0994 u8 i;
0995
0996 for (i = 0; i < DELL_SMM_NO_TEMP; i++) {
0997 data->temp_type[i] = i8k_get_temp_type(i);
0998 if (data->temp_type[i] < 0)
0999 continue;
1000
1001 if (data->temp_type[i] >= ARRAY_SIZE(temp_labels))
1002 data->temp_type[i] = ARRAY_SIZE(temp_labels) - 1;
1003 }
1004
1005 for (i = 0; i < DELL_SMM_NO_FANS; i++) {
1006 data->fan_type[i] = INT_MIN;
1007 err = i8k_get_fan_status(data, i);
1008 if (err < 0)
1009 err = i8k_get_fan_type(data, i);
1010
1011 if (err < 0)
1012 continue;
1013
1014 data->fan[i] = true;
1015
1016
1017 if (IS_REACHABLE(CONFIG_THERMAL)) {
1018 err = dell_smm_init_cdev(dev, i);
1019 if (err < 0)
1020 dev_warn(dev, "Failed to register cooling device for fan %u\n",
1021 i + 1);
1022 }
1023
1024 data->fan_nominal_speed[i] = devm_kmalloc_array(dev, data->i8k_fan_max + 1,
1025 sizeof(*data->fan_nominal_speed[i]),
1026 GFP_KERNEL);
1027 if (!data->fan_nominal_speed[i])
1028 continue;
1029
1030 for (state = 0; state <= data->i8k_fan_max; state++) {
1031 err = i8k_get_fan_nominal_speed(data, i, state);
1032 if (err < 0) {
1033
1034 devm_kfree(dev, data->fan_nominal_speed[i]);
1035 data->fan_nominal_speed[i] = NULL;
1036 break;
1037 }
1038 data->fan_nominal_speed[i][state] = err;
1039
1040
1041
1042
1043
1044 if (!fan_mult && err > I8K_FAN_RPM_THRESHOLD)
1045 data->i8k_fan_mult = 1;
1046 }
1047 }
1048
1049 dell_smm_hwmon_dev = devm_hwmon_device_register_with_info(dev, "dell_smm", data,
1050 &dell_smm_chip_info, NULL);
1051
1052 return PTR_ERR_OR_ZERO(dell_smm_hwmon_dev);
1053 }
1054
1055 struct i8k_config_data {
1056 uint fan_mult;
1057 uint fan_max;
1058 };
1059
1060 enum i8k_configs {
1061 DELL_LATITUDE_D520,
1062 DELL_PRECISION_490,
1063 DELL_STUDIO,
1064 DELL_XPS,
1065 };
1066
1067
1068
1069
1070
1071
1072 static const struct i8k_config_data i8k_config_data[] __initconst = {
1073 [DELL_LATITUDE_D520] = {
1074 .fan_mult = 1,
1075 .fan_max = I8K_FAN_TURBO,
1076 },
1077 [DELL_PRECISION_490] = {
1078 .fan_mult = 1,
1079 .fan_max = I8K_FAN_TURBO,
1080 },
1081 [DELL_STUDIO] = {
1082 .fan_mult = 1,
1083 .fan_max = I8K_FAN_HIGH,
1084 },
1085 [DELL_XPS] = {
1086 .fan_mult = 1,
1087 .fan_max = I8K_FAN_HIGH,
1088 },
1089 };
1090
1091 static const struct dmi_system_id i8k_dmi_table[] __initconst = {
1092 {
1093 .ident = "Dell G5 5590",
1094 .matches = {
1095 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1096 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "G5 5590"),
1097 },
1098 },
1099 {
1100 .ident = "Dell Inspiron",
1101 .matches = {
1102 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
1103 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
1104 },
1105 },
1106 {
1107 .ident = "Dell Latitude",
1108 .matches = {
1109 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
1110 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
1111 },
1112 },
1113 {
1114 .ident = "Dell Inspiron 2",
1115 .matches = {
1116 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1117 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
1118 },
1119 },
1120 {
1121 .ident = "Dell Latitude D520",
1122 .matches = {
1123 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1124 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
1125 },
1126 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
1127 },
1128 {
1129 .ident = "Dell Latitude 2",
1130 .matches = {
1131 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1132 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
1133 },
1134 },
1135 {
1136 .ident = "Dell Inspiron 3",
1137 .matches = {
1138 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1139 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
1140 },
1141 },
1142 {
1143 .ident = "Dell Inspiron 3",
1144 .matches = {
1145 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1146 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
1147 },
1148 },
1149 {
1150 .ident = "Dell Precision 490",
1151 .matches = {
1152 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1153 DMI_MATCH(DMI_PRODUCT_NAME,
1154 "Precision WorkStation 490"),
1155 },
1156 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
1157 },
1158 {
1159 .ident = "Dell Precision",
1160 .matches = {
1161 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1162 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
1163 },
1164 },
1165 {
1166 .ident = "Dell Vostro",
1167 .matches = {
1168 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1169 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
1170 },
1171 },
1172 {
1173 .ident = "Dell Studio",
1174 .matches = {
1175 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1176 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
1177 },
1178 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
1179 },
1180 {
1181 .ident = "Dell XPS M140",
1182 .matches = {
1183 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1184 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
1185 },
1186 .driver_data = (void *)&i8k_config_data[DELL_XPS],
1187 },
1188 {
1189 .ident = "Dell XPS",
1190 .matches = {
1191 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1192 DMI_MATCH(DMI_PRODUCT_NAME, "XPS"),
1193 },
1194 },
1195 { }
1196 };
1197
1198 MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
1199
1200
1201
1202
1203
1204
1205
1206 static const struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initconst = {
1207 {
1208 .ident = "Dell Studio XPS 8000",
1209 .matches = {
1210 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1211 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
1212 },
1213 },
1214 {
1215 .ident = "Dell Studio XPS 8100",
1216 .matches = {
1217 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1218 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
1219 },
1220 },
1221 {
1222 .ident = "Dell Inspiron 580",
1223 .matches = {
1224 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1225 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
1226 },
1227 },
1228 {
1229 .ident = "Dell Inspiron 3505",
1230 .matches = {
1231 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1232 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 3505"),
1233 },
1234 },
1235 { }
1236 };
1237
1238
1239
1240
1241
1242
1243
1244 static const struct dmi_system_id i8k_blacklist_fan_support_dmi_table[] __initconst = {
1245 {
1246 .ident = "Dell Inspiron 7720",
1247 .matches = {
1248 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1249 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
1250 },
1251 },
1252 {
1253 .ident = "Dell Vostro 3360",
1254 .matches = {
1255 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1256 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
1257 },
1258 },
1259 {
1260 .ident = "Dell XPS13 9333",
1261 .matches = {
1262 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1263 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
1264 },
1265 },
1266 {
1267 .ident = "Dell XPS 15 L502X",
1268 .matches = {
1269 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1270 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Dell System XPS L502X"),
1271 },
1272 },
1273 { }
1274 };
1275
1276 struct i8k_fan_control_data {
1277 unsigned int manual_fan;
1278 unsigned int auto_fan;
1279 };
1280
1281 enum i8k_fan_controls {
1282 I8K_FAN_34A3_35A3,
1283 };
1284
1285 static const struct i8k_fan_control_data i8k_fan_control_data[] __initconst = {
1286 [I8K_FAN_34A3_35A3] = {
1287 .manual_fan = 0x34a3,
1288 .auto_fan = 0x35a3,
1289 },
1290 };
1291
1292 static const struct dmi_system_id i8k_whitelist_fan_control[] __initconst = {
1293 {
1294 .ident = "Dell Latitude 5480",
1295 .matches = {
1296 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1297 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude 5480"),
1298 },
1299 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1300 },
1301 {
1302 .ident = "Dell Latitude E6440",
1303 .matches = {
1304 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1305 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E6440"),
1306 },
1307 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1308 },
1309 {
1310 .ident = "Dell Latitude E7440",
1311 .matches = {
1312 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1313 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E7440"),
1314 },
1315 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1316 },
1317 {
1318 .ident = "Dell Precision 5530",
1319 .matches = {
1320 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1321 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 5530"),
1322 },
1323 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1324 },
1325 {
1326 .ident = "Dell Precision 7510",
1327 .matches = {
1328 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1329 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 7510"),
1330 },
1331 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1332 },
1333 {
1334 .ident = "Dell XPS 13 7390",
1335 .matches = {
1336 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1337 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 13 7390"),
1338 },
1339 .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1340 },
1341 { }
1342 };
1343
1344 static int __init dell_smm_probe(struct platform_device *pdev)
1345 {
1346 struct dell_smm_data *data;
1347 const struct dmi_system_id *id, *fan_control;
1348 int ret;
1349
1350 data = devm_kzalloc(&pdev->dev, sizeof(struct dell_smm_data), GFP_KERNEL);
1351 if (!data)
1352 return -ENOMEM;
1353
1354 mutex_init(&data->i8k_mutex);
1355 platform_set_drvdata(pdev, data);
1356
1357 if (dmi_check_system(i8k_blacklist_fan_support_dmi_table)) {
1358 dev_warn(&pdev->dev, "broken Dell BIOS detected, disallow fan support\n");
1359 if (!force)
1360 data->disallow_fan_support = true;
1361 }
1362
1363 if (dmi_check_system(i8k_blacklist_fan_type_dmi_table)) {
1364 dev_warn(&pdev->dev, "broken Dell BIOS detected, disallow fan type call\n");
1365 if (!force)
1366 data->disallow_fan_type_call = true;
1367 }
1368
1369 strscpy(data->bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
1370 sizeof(data->bios_version));
1371 strscpy(data->bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
1372 sizeof(data->bios_machineid));
1373
1374
1375
1376
1377
1378 id = dmi_first_match(i8k_dmi_table);
1379 if (id && id->driver_data) {
1380 const struct i8k_config_data *conf = id->driver_data;
1381
1382 if (!fan_mult && conf->fan_mult)
1383 fan_mult = conf->fan_mult;
1384
1385 if (!fan_max && conf->fan_max)
1386 fan_max = conf->fan_max;
1387 }
1388
1389
1390 data->i8k_fan_mult = fan_mult ? : I8K_FAN_MULT;
1391 data->i8k_fan_max = fan_max ? : I8K_FAN_HIGH;
1392 data->i8k_pwm_mult = DIV_ROUND_UP(255, data->i8k_fan_max);
1393
1394 fan_control = dmi_first_match(i8k_whitelist_fan_control);
1395 if (fan_control && fan_control->driver_data) {
1396 const struct i8k_fan_control_data *control = fan_control->driver_data;
1397
1398 data->manual_fan = control->manual_fan;
1399 data->auto_fan = control->auto_fan;
1400 dev_info(&pdev->dev, "enabling support for setting automatic/manual fan control\n");
1401 }
1402
1403 ret = dell_smm_init_hwmon(&pdev->dev);
1404 if (ret)
1405 return ret;
1406
1407 i8k_init_procfs(&pdev->dev);
1408
1409 return 0;
1410 }
1411
1412 static struct platform_driver dell_smm_driver = {
1413 .driver = {
1414 .name = KBUILD_MODNAME,
1415 },
1416 };
1417
1418 static struct platform_device *dell_smm_device;
1419
1420
1421
1422
1423 static int __init i8k_init(void)
1424 {
1425
1426
1427
1428 if (!dmi_check_system(i8k_dmi_table)) {
1429 if (!ignore_dmi && !force)
1430 return -ENODEV;
1431
1432 pr_info("not running on a supported Dell system.\n");
1433 pr_info("vendor=%s, model=%s, version=%s\n",
1434 i8k_get_dmi_data(DMI_SYS_VENDOR),
1435 i8k_get_dmi_data(DMI_PRODUCT_NAME),
1436 i8k_get_dmi_data(DMI_BIOS_VERSION));
1437 }
1438
1439
1440
1441
1442 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
1443 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
1444 pr_err("unable to get SMM Dell signature\n");
1445 if (!force)
1446 return -ENODEV;
1447 }
1448
1449 dell_smm_device = platform_create_bundle(&dell_smm_driver, dell_smm_probe, NULL, 0, NULL,
1450 0);
1451
1452 return PTR_ERR_OR_ZERO(dell_smm_device);
1453 }
1454
1455 static void __exit i8k_exit(void)
1456 {
1457 platform_device_unregister(dell_smm_device);
1458 platform_driver_unregister(&dell_smm_driver);
1459 }
1460
1461 module_init(i8k_init);
1462 module_exit(i8k_exit);