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