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