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