GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / hwmon / dell-smm-hwmon.c
1 /*
2  * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
3  *
4  * Copyright (C) 2001  Massimo Dal Zotto <dz@debian.org>
5  *
6  * Hwmon integration:
7  * Copyright (C) 2011  Jean Delvare <jdelvare@suse.de>
8  * Copyright (C) 2013, 2014  Guenter Roeck <linux@roeck-us.net>
9  * Copyright (C) 2014, 2015  Pali Rohár <pali.rohar@gmail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License as published by the
13  * Free Software Foundation; either version 2, or (at your option) any
14  * later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  */
21
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24 #include <linux/cpu.h>
25 #include <linux/delay.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/init.h>
29 #include <linux/proc_fs.h>
30 #include <linux/seq_file.h>
31 #include <linux/dmi.h>
32 #include <linux/capability.h>
33 #include <linux/mutex.h>
34 #include <linux/hwmon.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/uaccess.h>
37 #include <linux/io.h>
38 #include <linux/sched.h>
39 #include <linux/ctype.h>
40 #include <linux/smp.h>
41
42 #include <linux/i8k.h>
43
44 #define I8K_SMM_FN_STATUS       0x0025
45 #define I8K_SMM_POWER_STATUS    0x0069
46 #define I8K_SMM_SET_FAN         0x01a3
47 #define I8K_SMM_GET_FAN         0x00a3
48 #define I8K_SMM_GET_SPEED       0x02a3
49 #define I8K_SMM_GET_FAN_TYPE    0x03a3
50 #define I8K_SMM_GET_NOM_SPEED   0x04a3
51 #define I8K_SMM_GET_TEMP        0x10a3
52 #define I8K_SMM_GET_TEMP_TYPE   0x11a3
53 #define I8K_SMM_GET_DELL_SIG1   0xfea3
54 #define I8K_SMM_GET_DELL_SIG2   0xffa3
55
56 #define I8K_FAN_MULT            30
57 #define I8K_FAN_MAX_RPM         30000
58 #define I8K_MAX_TEMP            127
59
60 #define I8K_FN_NONE             0x00
61 #define I8K_FN_UP               0x01
62 #define I8K_FN_DOWN             0x02
63 #define I8K_FN_MUTE             0x04
64 #define I8K_FN_MASK             0x07
65 #define I8K_FN_SHIFT            8
66
67 #define I8K_POWER_AC            0x05
68 #define I8K_POWER_BATTERY       0x01
69
70 static DEFINE_MUTEX(i8k_mutex);
71 static char bios_version[4];
72 static char bios_machineid[16];
73 static struct device *i8k_hwmon_dev;
74 static u32 i8k_hwmon_flags;
75 static uint i8k_fan_mult = I8K_FAN_MULT;
76 static uint i8k_pwm_mult;
77 static uint i8k_fan_max = I8K_FAN_HIGH;
78 static bool disallow_fan_type_call;
79 static bool disallow_fan_support;
80
81 #define I8K_HWMON_HAVE_TEMP1    (1 << 0)
82 #define I8K_HWMON_HAVE_TEMP2    (1 << 1)
83 #define I8K_HWMON_HAVE_TEMP3    (1 << 2)
84 #define I8K_HWMON_HAVE_TEMP4    (1 << 3)
85 #define I8K_HWMON_HAVE_FAN1     (1 << 4)
86 #define I8K_HWMON_HAVE_FAN2     (1 << 5)
87 #define I8K_HWMON_HAVE_FAN3     (1 << 6)
88
89 MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
90 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
91 MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
92 MODULE_LICENSE("GPL");
93 MODULE_ALIAS("i8k");
94
95 static bool force;
96 module_param(force, bool, 0);
97 MODULE_PARM_DESC(force, "Force loading without checking for supported models");
98
99 static bool ignore_dmi;
100 module_param(ignore_dmi, bool, 0);
101 MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
102
103 #if IS_ENABLED(CONFIG_I8K)
104 static bool restricted = true;
105 module_param(restricted, bool, 0);
106 MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
107
108 static bool power_status;
109 module_param(power_status, bool, 0600);
110 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
111 #endif
112
113 static uint fan_mult;
114 module_param(fan_mult, uint, 0);
115 MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
116
117 static uint fan_max;
118 module_param(fan_max, uint, 0);
119 MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
120
121 struct smm_regs {
122         unsigned int eax;
123         unsigned int ebx __packed;
124         unsigned int ecx __packed;
125         unsigned int edx __packed;
126         unsigned int esi __packed;
127         unsigned int edi __packed;
128 };
129
130 static inline const char *i8k_get_dmi_data(int field)
131 {
132         const char *dmi_data = dmi_get_system_info(field);
133
134         return dmi_data && *dmi_data ? dmi_data : "?";
135 }
136
137 /*
138  * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
139  */
140 static int i8k_smm_func(void *par)
141 {
142         int rc;
143         struct smm_regs *regs = par;
144         int eax = regs->eax;
145
146 #ifdef DEBUG
147         int ebx = regs->ebx;
148         unsigned long duration;
149         ktime_t calltime, delta, rettime;
150
151         calltime = ktime_get();
152 #endif
153
154         /* SMM requires CPU 0 */
155         if (smp_processor_id() != 0)
156                 return -EBUSY;
157
158 #if defined(CONFIG_X86_64)
159         asm volatile("pushq %%rax\n\t"
160                 "movl 0(%%rax),%%edx\n\t"
161                 "pushq %%rdx\n\t"
162                 "movl 4(%%rax),%%ebx\n\t"
163                 "movl 8(%%rax),%%ecx\n\t"
164                 "movl 12(%%rax),%%edx\n\t"
165                 "movl 16(%%rax),%%esi\n\t"
166                 "movl 20(%%rax),%%edi\n\t"
167                 "popq %%rax\n\t"
168                 "out %%al,$0xb2\n\t"
169                 "out %%al,$0x84\n\t"
170                 "xchgq %%rax,(%%rsp)\n\t"
171                 "movl %%ebx,4(%%rax)\n\t"
172                 "movl %%ecx,8(%%rax)\n\t"
173                 "movl %%edx,12(%%rax)\n\t"
174                 "movl %%esi,16(%%rax)\n\t"
175                 "movl %%edi,20(%%rax)\n\t"
176                 "popq %%rdx\n\t"
177                 "movl %%edx,0(%%rax)\n\t"
178                 "pushfq\n\t"
179                 "popq %%rax\n\t"
180                 "andl $1,%%eax\n"
181                 : "=a"(rc)
182                 :    "a"(regs)
183                 :    "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
184 #else
185         asm volatile("pushl %%eax\n\t"
186             "movl 0(%%eax),%%edx\n\t"
187             "push %%edx\n\t"
188             "movl 4(%%eax),%%ebx\n\t"
189             "movl 8(%%eax),%%ecx\n\t"
190             "movl 12(%%eax),%%edx\n\t"
191             "movl 16(%%eax),%%esi\n\t"
192             "movl 20(%%eax),%%edi\n\t"
193             "popl %%eax\n\t"
194             "out %%al,$0xb2\n\t"
195             "out %%al,$0x84\n\t"
196             "xchgl %%eax,(%%esp)\n\t"
197             "movl %%ebx,4(%%eax)\n\t"
198             "movl %%ecx,8(%%eax)\n\t"
199             "movl %%edx,12(%%eax)\n\t"
200             "movl %%esi,16(%%eax)\n\t"
201             "movl %%edi,20(%%eax)\n\t"
202             "popl %%edx\n\t"
203             "movl %%edx,0(%%eax)\n\t"
204             "lahf\n\t"
205             "shrl $8,%%eax\n\t"
206             "andl $1,%%eax\n"
207             : "=a"(rc)
208             :    "a"(regs)
209             :    "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
210 #endif
211         if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
212                 rc = -EINVAL;
213
214 #ifdef DEBUG
215         rettime = ktime_get();
216         delta = ktime_sub(rettime, calltime);
217         duration = ktime_to_ns(delta) >> 10;
218         pr_debug("smm(0x%.4x 0x%.4x) = 0x%.4x  (took %7lu usecs)\n", eax, ebx,
219                 (rc ? 0xffff : regs->eax & 0xffff), duration);
220 #endif
221
222         return rc;
223 }
224
225 /*
226  * Call the System Management Mode BIOS.
227  */
228 static int i8k_smm(struct smm_regs *regs)
229 {
230         int ret;
231
232         get_online_cpus();
233         ret = smp_call_on_cpu(0, i8k_smm_func, regs, true);
234         put_online_cpus();
235
236         return ret;
237 }
238
239 /*
240  * Read the fan status.
241  */
242 static int i8k_get_fan_status(int fan)
243 {
244         struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
245
246         if (disallow_fan_support)
247                 return -EINVAL;
248
249         regs.ebx = fan & 0xff;
250         return i8k_smm(&regs) ? : regs.eax & 0xff;
251 }
252
253 /*
254  * Read the fan speed in RPM.
255  */
256 static int i8k_get_fan_speed(int fan)
257 {
258         struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
259
260         if (disallow_fan_support)
261                 return -EINVAL;
262
263         regs.ebx = fan & 0xff;
264         return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
265 }
266
267 /*
268  * Read the fan type.
269  */
270 static int _i8k_get_fan_type(int fan)
271 {
272         struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, };
273
274         if (disallow_fan_support || disallow_fan_type_call)
275                 return -EINVAL;
276
277         regs.ebx = fan & 0xff;
278         return i8k_smm(&regs) ? : regs.eax & 0xff;
279 }
280
281 static int i8k_get_fan_type(int fan)
282 {
283         /* I8K_SMM_GET_FAN_TYPE SMM call is expensive, so cache values */
284         static int types[3] = { INT_MIN, INT_MIN, INT_MIN };
285
286         if (types[fan] == INT_MIN)
287                 types[fan] = _i8k_get_fan_type(fan);
288
289         return types[fan];
290 }
291
292 /*
293  * Read the fan nominal rpm for specific fan speed.
294  */
295 static int i8k_get_fan_nominal_speed(int fan, int speed)
296 {
297         struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
298
299         if (disallow_fan_support)
300                 return -EINVAL;
301
302         regs.ebx = (fan & 0xff) | (speed << 8);
303         return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
304 }
305
306 /*
307  * Set the fan speed (off, low, high, ...).
308  */
309 static int i8k_set_fan(int fan, int speed)
310 {
311         struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
312
313         if (disallow_fan_support)
314                 return -EINVAL;
315
316         speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
317         regs.ebx = (fan & 0xff) | (speed << 8);
318
319         return i8k_smm(&regs);
320 }
321
322 static int i8k_get_temp_type(int sensor)
323 {
324         struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
325
326         regs.ebx = sensor & 0xff;
327         return i8k_smm(&regs) ? : regs.eax & 0xff;
328 }
329
330 /*
331  * Read the cpu temperature.
332  */
333 static int _i8k_get_temp(int sensor)
334 {
335         struct smm_regs regs = {
336                 .eax = I8K_SMM_GET_TEMP,
337                 .ebx = sensor & 0xff,
338         };
339
340         return i8k_smm(&regs) ? : regs.eax & 0xff;
341 }
342
343 static int i8k_get_temp(int sensor)
344 {
345         int temp = _i8k_get_temp(sensor);
346
347         /*
348          * Sometimes the temperature sensor returns 0x99, which is out of range.
349          * In this case we retry (once) before returning an error.
350          # 1003655137 00000058 00005a4b
351          # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
352          # 1003655139 00000054 00005c52
353          */
354         if (temp == 0x99) {
355                 msleep(100);
356                 temp = _i8k_get_temp(sensor);
357         }
358         /*
359          * Return -ENODATA for all invalid temperatures.
360          *
361          * Known instances are the 0x99 value as seen above as well as
362          * 0xc1 (193), which may be returned when trying to read the GPU
363          * temperature if the system supports a GPU and it is currently
364          * turned off.
365          */
366         if (temp > I8K_MAX_TEMP)
367                 return -ENODATA;
368
369         return temp;
370 }
371
372 static int i8k_get_dell_signature(int req_fn)
373 {
374         struct smm_regs regs = { .eax = req_fn, };
375         int rc;
376
377         rc = i8k_smm(&regs);
378         if (rc < 0)
379                 return rc;
380
381         return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
382 }
383
384 #if IS_ENABLED(CONFIG_I8K)
385
386 /*
387  * Read the Fn key status.
388  */
389 static int i8k_get_fn_status(void)
390 {
391         struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
392         int rc;
393
394         rc = i8k_smm(&regs);
395         if (rc < 0)
396                 return rc;
397
398         switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
399         case I8K_FN_UP:
400                 return I8K_VOL_UP;
401         case I8K_FN_DOWN:
402                 return I8K_VOL_DOWN;
403         case I8K_FN_MUTE:
404                 return I8K_VOL_MUTE;
405         default:
406                 return 0;
407         }
408 }
409
410 /*
411  * Read the power status.
412  */
413 static int i8k_get_power_status(void)
414 {
415         struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
416         int rc;
417
418         rc = i8k_smm(&regs);
419         if (rc < 0)
420                 return rc;
421
422         return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
423 }
424
425 /*
426  * Procfs interface
427  */
428
429 static int
430 i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
431 {
432         int val = 0;
433         int speed, err;
434         unsigned char buff[16];
435         int __user *argp = (int __user *)arg;
436
437         if (!argp)
438                 return -EINVAL;
439
440         switch (cmd) {
441         case I8K_BIOS_VERSION:
442                 if (!isdigit(bios_version[0]) || !isdigit(bios_version[1]) ||
443                     !isdigit(bios_version[2]))
444                         return -EINVAL;
445
446                 val = (bios_version[0] << 16) |
447                                 (bios_version[1] << 8) | bios_version[2];
448                 break;
449
450         case I8K_MACHINE_ID:
451                 if (restricted && !capable(CAP_SYS_ADMIN))
452                         return -EPERM;
453
454                 memset(buff, 0, sizeof(buff));
455                 strlcpy(buff, bios_machineid, sizeof(buff));
456                 break;
457
458         case I8K_FN_STATUS:
459                 val = i8k_get_fn_status();
460                 break;
461
462         case I8K_POWER_STATUS:
463                 val = i8k_get_power_status();
464                 break;
465
466         case I8K_GET_TEMP:
467                 val = i8k_get_temp(0);
468                 break;
469
470         case I8K_GET_SPEED:
471                 if (copy_from_user(&val, argp, sizeof(int)))
472                         return -EFAULT;
473
474                 val = i8k_get_fan_speed(val);
475                 break;
476
477         case I8K_GET_FAN:
478                 if (copy_from_user(&val, argp, sizeof(int)))
479                         return -EFAULT;
480
481                 val = i8k_get_fan_status(val);
482                 break;
483
484         case I8K_SET_FAN:
485                 if (restricted && !capable(CAP_SYS_ADMIN))
486                         return -EPERM;
487
488                 if (copy_from_user(&val, argp, sizeof(int)))
489                         return -EFAULT;
490
491                 if (copy_from_user(&speed, argp + 1, sizeof(int)))
492                         return -EFAULT;
493
494                 err = i8k_set_fan(val, speed);
495                 if (err < 0)
496                         return err;
497
498                 val = i8k_get_fan_status(val);
499                 break;
500
501         default:
502                 return -EINVAL;
503         }
504
505         if (val < 0)
506                 return val;
507
508         switch (cmd) {
509         case I8K_BIOS_VERSION:
510                 if (copy_to_user(argp, &val, 4))
511                         return -EFAULT;
512
513                 break;
514         case I8K_MACHINE_ID:
515                 if (copy_to_user(argp, buff, 16))
516                         return -EFAULT;
517
518                 break;
519         default:
520                 if (copy_to_user(argp, &val, sizeof(int)))
521                         return -EFAULT;
522
523                 break;
524         }
525
526         return 0;
527 }
528
529 static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
530 {
531         long ret;
532
533         mutex_lock(&i8k_mutex);
534         ret = i8k_ioctl_unlocked(fp, cmd, arg);
535         mutex_unlock(&i8k_mutex);
536
537         return ret;
538 }
539
540 /*
541  * Print the information for /proc/i8k.
542  */
543 static int i8k_proc_show(struct seq_file *seq, void *offset)
544 {
545         int fn_key, cpu_temp, ac_power;
546         int left_fan, right_fan, left_speed, right_speed;
547
548         cpu_temp        = i8k_get_temp(0);                      /* 11100 µs */
549         left_fan        = i8k_get_fan_status(I8K_FAN_LEFT);     /*   580 µs */
550         right_fan       = i8k_get_fan_status(I8K_FAN_RIGHT);    /*   580 µs */
551         left_speed      = i8k_get_fan_speed(I8K_FAN_LEFT);      /*   580 µs */
552         right_speed     = i8k_get_fan_speed(I8K_FAN_RIGHT);     /*   580 µs */
553         fn_key          = i8k_get_fn_status();                  /*   750 µs */
554         if (power_status)
555                 ac_power = i8k_get_power_status();              /* 14700 µs */
556         else
557                 ac_power = -1;
558
559         /*
560          * Info:
561          *
562          * 1)  Format version (this will change if format changes)
563          * 2)  BIOS version
564          * 3)  BIOS machine ID
565          * 4)  Cpu temperature
566          * 5)  Left fan status
567          * 6)  Right fan status
568          * 7)  Left fan speed
569          * 8)  Right fan speed
570          * 9)  AC power
571          * 10) Fn Key status
572          */
573         seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
574                    I8K_PROC_FMT,
575                    bios_version,
576                    (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : bios_machineid,
577                    cpu_temp,
578                    left_fan, right_fan, left_speed, right_speed,
579                    ac_power, fn_key);
580
581         return 0;
582 }
583
584 static int i8k_open_fs(struct inode *inode, struct file *file)
585 {
586         return single_open(file, i8k_proc_show, NULL);
587 }
588
589 static const struct file_operations i8k_fops = {
590         .owner          = THIS_MODULE,
591         .open           = i8k_open_fs,
592         .read           = seq_read,
593         .llseek         = seq_lseek,
594         .release        = single_release,
595         .unlocked_ioctl = i8k_ioctl,
596 };
597
598 static struct proc_dir_entry *entry;
599
600 static void __init i8k_init_procfs(void)
601 {
602         /* Register the proc entry */
603         entry = proc_create("i8k", 0, NULL, &i8k_fops);
604 }
605
606 static void __exit i8k_exit_procfs(void)
607 {
608         if (entry)
609                 remove_proc_entry("i8k", NULL);
610 }
611
612 #else
613
614 static inline void __init i8k_init_procfs(void)
615 {
616 }
617
618 static inline void __exit i8k_exit_procfs(void)
619 {
620 }
621
622 #endif
623
624 /*
625  * Hwmon interface
626  */
627
628 static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
629                                          struct device_attribute *devattr,
630                                          char *buf)
631 {
632         static const char * const labels[] = {
633                 "CPU",
634                 "GPU",
635                 "SODIMM",
636                 "Other",
637                 "Ambient",
638                 "Other",
639         };
640         int index = to_sensor_dev_attr(devattr)->index;
641         int type;
642
643         type = i8k_get_temp_type(index);
644         if (type < 0)
645                 return type;
646         if (type >= ARRAY_SIZE(labels))
647                 type = ARRAY_SIZE(labels) - 1;
648         return sprintf(buf, "%s\n", labels[type]);
649 }
650
651 static ssize_t i8k_hwmon_show_temp(struct device *dev,
652                                    struct device_attribute *devattr,
653                                    char *buf)
654 {
655         int index = to_sensor_dev_attr(devattr)->index;
656         int temp;
657
658         temp = i8k_get_temp(index);
659         if (temp < 0)
660                 return temp;
661         return sprintf(buf, "%d\n", temp * 1000);
662 }
663
664 static ssize_t i8k_hwmon_show_fan_label(struct device *dev,
665                                         struct device_attribute *devattr,
666                                         char *buf)
667 {
668         static const char * const labels[] = {
669                 "Processor Fan",
670                 "Motherboard Fan",
671                 "Video Fan",
672                 "Power Supply Fan",
673                 "Chipset Fan",
674                 "Other Fan",
675         };
676         int index = to_sensor_dev_attr(devattr)->index;
677         bool dock = false;
678         int type;
679
680         type = i8k_get_fan_type(index);
681         if (type < 0)
682                 return type;
683
684         if (type & 0x10) {
685                 dock = true;
686                 type &= 0x0F;
687         }
688
689         if (type >= ARRAY_SIZE(labels))
690                 type = (ARRAY_SIZE(labels) - 1);
691
692         return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
693 }
694
695 static ssize_t i8k_hwmon_show_fan(struct device *dev,
696                                   struct device_attribute *devattr,
697                                   char *buf)
698 {
699         int index = to_sensor_dev_attr(devattr)->index;
700         int fan_speed;
701
702         fan_speed = i8k_get_fan_speed(index);
703         if (fan_speed < 0)
704                 return fan_speed;
705         return sprintf(buf, "%d\n", fan_speed);
706 }
707
708 static ssize_t i8k_hwmon_show_pwm(struct device *dev,
709                                   struct device_attribute *devattr,
710                                   char *buf)
711 {
712         int index = to_sensor_dev_attr(devattr)->index;
713         int status;
714
715         status = i8k_get_fan_status(index);
716         if (status < 0)
717                 return -EIO;
718         return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
719 }
720
721 static ssize_t i8k_hwmon_set_pwm(struct device *dev,
722                                  struct device_attribute *attr,
723                                  const char *buf, size_t count)
724 {
725         int index = to_sensor_dev_attr(attr)->index;
726         unsigned long val;
727         int err;
728
729         err = kstrtoul(buf, 10, &val);
730         if (err)
731                 return err;
732         val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
733
734         mutex_lock(&i8k_mutex);
735         err = i8k_set_fan(index, val);
736         mutex_unlock(&i8k_mutex);
737
738         return err < 0 ? -EIO : count;
739 }
740
741 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
742 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
743                           0);
744 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
745 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
746                           1);
747 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
748 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
749                           2);
750 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
751 static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
752                           3);
753 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, 0);
754 static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
755                           0);
756 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
757                           i8k_hwmon_set_pwm, 0);
758 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
759                           1);
760 static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
761                           1);
762 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
763                           i8k_hwmon_set_pwm, 1);
764 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
765                           2);
766 static SENSOR_DEVICE_ATTR(fan3_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
767                           2);
768 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
769                           i8k_hwmon_set_pwm, 2);
770
771 static struct attribute *i8k_attrs[] = {
772         &sensor_dev_attr_temp1_input.dev_attr.attr,     /* 0 */
773         &sensor_dev_attr_temp1_label.dev_attr.attr,     /* 1 */
774         &sensor_dev_attr_temp2_input.dev_attr.attr,     /* 2 */
775         &sensor_dev_attr_temp2_label.dev_attr.attr,     /* 3 */
776         &sensor_dev_attr_temp3_input.dev_attr.attr,     /* 4 */
777         &sensor_dev_attr_temp3_label.dev_attr.attr,     /* 5 */
778         &sensor_dev_attr_temp4_input.dev_attr.attr,     /* 6 */
779         &sensor_dev_attr_temp4_label.dev_attr.attr,     /* 7 */
780         &sensor_dev_attr_fan1_input.dev_attr.attr,      /* 8 */
781         &sensor_dev_attr_fan1_label.dev_attr.attr,      /* 9 */
782         &sensor_dev_attr_pwm1.dev_attr.attr,            /* 10 */
783         &sensor_dev_attr_fan2_input.dev_attr.attr,      /* 11 */
784         &sensor_dev_attr_fan2_label.dev_attr.attr,      /* 12 */
785         &sensor_dev_attr_pwm2.dev_attr.attr,            /* 13 */
786         &sensor_dev_attr_fan3_input.dev_attr.attr,      /* 14 */
787         &sensor_dev_attr_fan3_label.dev_attr.attr,      /* 15 */
788         &sensor_dev_attr_pwm3.dev_attr.attr,            /* 16 */
789         NULL
790 };
791
792 static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
793                               int index)
794 {
795         if (disallow_fan_support && index >= 8)
796                 return 0;
797         if (disallow_fan_type_call &&
798             (index == 9 || index == 12 || index == 15))
799                 return 0;
800         if (index >= 0 && index <= 1 &&
801             !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
802                 return 0;
803         if (index >= 2 && index <= 3 &&
804             !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
805                 return 0;
806         if (index >= 4 && index <= 5 &&
807             !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
808                 return 0;
809         if (index >= 6 && index <= 7 &&
810             !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
811                 return 0;
812         if (index >= 8 && index <= 10 &&
813             !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
814                 return 0;
815         if (index >= 11 && index <= 13 &&
816             !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
817                 return 0;
818         if (index >= 14 && index <= 16 &&
819             !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN3))
820                 return 0;
821
822         return attr->mode;
823 }
824
825 static const struct attribute_group i8k_group = {
826         .attrs = i8k_attrs,
827         .is_visible = i8k_is_visible,
828 };
829 __ATTRIBUTE_GROUPS(i8k);
830
831 static int __init i8k_init_hwmon(void)
832 {
833         int err;
834
835         i8k_hwmon_flags = 0;
836
837         /* CPU temperature attributes, if temperature type is OK */
838         err = i8k_get_temp_type(0);
839         if (err >= 0)
840                 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
841         /* check for additional temperature sensors */
842         err = i8k_get_temp_type(1);
843         if (err >= 0)
844                 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
845         err = i8k_get_temp_type(2);
846         if (err >= 0)
847                 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
848         err = i8k_get_temp_type(3);
849         if (err >= 0)
850                 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
851
852         /* First fan attributes, if fan status or type is OK */
853         err = i8k_get_fan_status(0);
854         if (err < 0)
855                 err = i8k_get_fan_type(0);
856         if (err >= 0)
857                 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
858
859         /* Second fan attributes, if fan status or type is OK */
860         err = i8k_get_fan_status(1);
861         if (err < 0)
862                 err = i8k_get_fan_type(1);
863         if (err >= 0)
864                 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
865
866         /* Third fan attributes, if fan status or type is OK */
867         err = i8k_get_fan_status(2);
868         if (err < 0)
869                 err = i8k_get_fan_type(2);
870         if (err >= 0)
871                 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN3;
872
873         i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm",
874                                                           NULL, i8k_groups);
875         if (IS_ERR(i8k_hwmon_dev)) {
876                 err = PTR_ERR(i8k_hwmon_dev);
877                 i8k_hwmon_dev = NULL;
878                 pr_err("hwmon registration failed (%d)\n", err);
879                 return err;
880         }
881         return 0;
882 }
883
884 struct i8k_config_data {
885         uint fan_mult;
886         uint fan_max;
887 };
888
889 enum i8k_configs {
890         DELL_LATITUDE_D520,
891         DELL_PRECISION_490,
892         DELL_STUDIO,
893         DELL_XPS,
894 };
895
896 static const struct i8k_config_data i8k_config_data[] = {
897         [DELL_LATITUDE_D520] = {
898                 .fan_mult = 1,
899                 .fan_max = I8K_FAN_TURBO,
900         },
901         [DELL_PRECISION_490] = {
902                 .fan_mult = 1,
903                 .fan_max = I8K_FAN_TURBO,
904         },
905         [DELL_STUDIO] = {
906                 .fan_mult = 1,
907                 .fan_max = I8K_FAN_HIGH,
908         },
909         [DELL_XPS] = {
910                 .fan_mult = 1,
911                 .fan_max = I8K_FAN_HIGH,
912         },
913 };
914
915 static const struct dmi_system_id i8k_dmi_table[] __initconst = {
916         {
917                 .ident = "Dell Inspiron",
918                 .matches = {
919                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
920                         DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
921                 },
922         },
923         {
924                 .ident = "Dell Latitude",
925                 .matches = {
926                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
927                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
928                 },
929         },
930         {
931                 .ident = "Dell Inspiron 2",
932                 .matches = {
933                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
934                         DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
935                 },
936         },
937         {
938                 .ident = "Dell Latitude D520",
939                 .matches = {
940                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
941                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
942                 },
943                 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
944         },
945         {
946                 .ident = "Dell Latitude 2",
947                 .matches = {
948                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
949                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
950                 },
951         },
952         {       /* UK Inspiron 6400  */
953                 .ident = "Dell Inspiron 3",
954                 .matches = {
955                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
956                         DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
957                 },
958         },
959         {
960                 .ident = "Dell Inspiron 3",
961                 .matches = {
962                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
963                         DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
964                 },
965         },
966         {
967                 .ident = "Dell Precision 490",
968                 .matches = {
969                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
970                         DMI_MATCH(DMI_PRODUCT_NAME,
971                                   "Precision WorkStation 490"),
972                 },
973                 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
974         },
975         {
976                 .ident = "Dell Precision",
977                 .matches = {
978                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
979                         DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
980                 },
981         },
982         {
983                 .ident = "Dell Vostro",
984                 .matches = {
985                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
986                         DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
987                 },
988         },
989         {
990                 .ident = "Dell XPS421",
991                 .matches = {
992                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
993                         DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
994                 },
995         },
996         {
997                 .ident = "Dell Studio",
998                 .matches = {
999                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1000                         DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
1001                 },
1002                 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
1003         },
1004         {
1005                 .ident = "Dell XPS 13",
1006                 .matches = {
1007                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1008                         DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
1009                 },
1010                 .driver_data = (void *)&i8k_config_data[DELL_XPS],
1011         },
1012         {
1013                 .ident = "Dell XPS M140",
1014                 .matches = {
1015                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1016                         DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
1017                 },
1018                 .driver_data = (void *)&i8k_config_data[DELL_XPS],
1019         },
1020         {
1021                 .ident = "Dell XPS 15 9560",
1022                 .matches = {
1023                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1024                         DMI_MATCH(DMI_PRODUCT_NAME, "XPS 15 9560"),
1025                 },
1026         },
1027         { }
1028 };
1029
1030 MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
1031
1032 /*
1033  * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed
1034  * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist
1035  * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call.
1036  * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121
1037  */
1038 static const struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initconst = {
1039         {
1040                 .ident = "Dell Studio XPS 8000",
1041                 .matches = {
1042                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1043                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
1044                 },
1045         },
1046         {
1047                 .ident = "Dell Studio XPS 8100",
1048                 .matches = {
1049                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1050                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
1051                 },
1052         },
1053         {
1054                 .ident = "Dell Inspiron 580",
1055                 .matches = {
1056                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1057                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
1058                 },
1059         },
1060         { }
1061 };
1062
1063 /*
1064  * On some machines all fan related SMM functions implemented by Dell BIOS
1065  * firmware freeze kernel for about 500ms. Until Dell fixes these problems fan
1066  * support for affected blacklisted Dell machines stay disabled.
1067  * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=195751
1068  */
1069 static struct dmi_system_id i8k_blacklist_fan_support_dmi_table[] __initdata = {
1070         {
1071                 .ident = "Dell Inspiron 7720",
1072                 .matches = {
1073                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1074                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
1075                 },
1076         },
1077         {
1078                 .ident = "Dell Vostro 3360",
1079                 .matches = {
1080                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1081                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
1082                 },
1083         },
1084         {
1085                 .ident = "Dell XPS13 9333",
1086                 .matches = {
1087                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1088                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
1089                 },
1090         },
1091         { }
1092 };
1093
1094 /*
1095  * Probe for the presence of a supported laptop.
1096  */
1097 static int __init i8k_probe(void)
1098 {
1099         const struct dmi_system_id *id;
1100         int fan, ret;
1101
1102         /*
1103          * Get DMI information
1104          */
1105         if (!dmi_check_system(i8k_dmi_table)) {
1106                 if (!ignore_dmi && !force)
1107                         return -ENODEV;
1108
1109                 pr_info("not running on a supported Dell system.\n");
1110                 pr_info("vendor=%s, model=%s, version=%s\n",
1111                         i8k_get_dmi_data(DMI_SYS_VENDOR),
1112                         i8k_get_dmi_data(DMI_PRODUCT_NAME),
1113                         i8k_get_dmi_data(DMI_BIOS_VERSION));
1114         }
1115
1116         if (dmi_check_system(i8k_blacklist_fan_support_dmi_table)) {
1117                 pr_warn("broken Dell BIOS detected, disallow fan support\n");
1118                 if (!force)
1119                         disallow_fan_support = true;
1120         }
1121
1122         if (dmi_check_system(i8k_blacklist_fan_type_dmi_table)) {
1123                 pr_warn("broken Dell BIOS detected, disallow fan type call\n");
1124                 if (!force)
1125                         disallow_fan_type_call = true;
1126         }
1127
1128         strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
1129                 sizeof(bios_version));
1130         strlcpy(bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
1131                 sizeof(bios_machineid));
1132
1133         /*
1134          * Get SMM Dell signature
1135          */
1136         if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
1137             i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
1138                 pr_err("unable to get SMM Dell signature\n");
1139                 if (!force)
1140                         return -ENODEV;
1141         }
1142
1143         /*
1144          * Set fan multiplier and maximal fan speed from dmi config
1145          * Values specified in module parameters override values from dmi
1146          */
1147         id = dmi_first_match(i8k_dmi_table);
1148         if (id && id->driver_data) {
1149                 const struct i8k_config_data *conf = id->driver_data;
1150                 if (!fan_mult && conf->fan_mult)
1151                         fan_mult = conf->fan_mult;
1152                 if (!fan_max && conf->fan_max)
1153                         fan_max = conf->fan_max;
1154         }
1155
1156         i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
1157         i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
1158
1159         if (!fan_mult) {
1160                 /*
1161                  * Autodetect fan multiplier based on nominal rpm
1162                  * If fan reports rpm value too high then set multiplier to 1
1163                  */
1164                 for (fan = 0; fan < 2; ++fan) {
1165                         ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
1166                         if (ret < 0)
1167                                 continue;
1168                         if (ret > I8K_FAN_MAX_RPM)
1169                                 i8k_fan_mult = 1;
1170                         break;
1171                 }
1172         } else {
1173                 /* Fan multiplier was specified in module param or in dmi */
1174                 i8k_fan_mult = fan_mult;
1175         }
1176
1177         return 0;
1178 }
1179
1180 static int __init i8k_init(void)
1181 {
1182         int err;
1183
1184         /* Are we running on an supported laptop? */
1185         if (i8k_probe())
1186                 return -ENODEV;
1187
1188         err = i8k_init_hwmon();
1189         if (err)
1190                 return err;
1191
1192         i8k_init_procfs();
1193         return 0;
1194 }
1195
1196 static void __exit i8k_exit(void)
1197 {
1198         hwmon_device_unregister(i8k_hwmon_dev);
1199         i8k_exit_procfs();
1200 }
1201
1202 module_init(i8k_init);
1203 module_exit(i8k_exit);