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