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