blob: 2ac87d553e22f67ebaca32d62fb76375e3d22bdc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pali Rohára5afba12015-05-14 13:16:36 +02002 * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org>
5 *
Jean Delvare949a9d72011-05-25 20:43:33 +02006 * Hwmon integration:
Jean Delvare7c81c602014-01-29 20:40:08 +01007 * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de>
Guenter Roeck564132d2015-01-12 14:32:00 +01008 * Copyright (C) 2013, 2014 Guenter Roeck <linux@roeck-us.net>
Pali Rohára5afba12015-05-14 13:16:36 +02009 * Copyright (C) 2014, 2015 Pali Rohár <pali.rohar@gmail.com>
Jean Delvare949a9d72011-05-25 20:43:33 +020010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any
14 * later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 */
21
Guenter Roeck60e71aa2013-12-14 09:30:08 -080022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Guenter Roeck564132d2015-01-12 14:32:00 +010024#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/types.h>
27#include <linux/init.h>
28#include <linux/proc_fs.h>
Dmitry Torokhov352f8f82005-06-25 14:54:26 -070029#include <linux/seq_file.h>
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070030#include <linux/dmi.h>
Alexey Dobriyan7e80d0d2007-05-08 00:28:59 -070031#include <linux/capability.h>
Arnd Bergmann613655f2010-06-02 14:28:52 +020032#include <linux/mutex.h>
Jean Delvare949a9d72011-05-25 20:43:33 +020033#include <linux/hwmon.h>
34#include <linux/hwmon-sysfs.h>
Guenter Roeck12186f52013-12-14 09:30:09 -080035#include <linux/uaccess.h>
36#include <linux/io.h>
Guenter Roeckf36fdb92013-12-14 09:30:15 -080037#include <linux/sched.h>
Pali Rohár053ea642016-06-18 00:54:44 +020038#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <linux/i8k.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define I8K_SMM_FN_STATUS 0x0025
43#define I8K_SMM_POWER_STATUS 0x0069
44#define I8K_SMM_SET_FAN 0x01a3
45#define I8K_SMM_GET_FAN 0x00a3
46#define I8K_SMM_GET_SPEED 0x02a3
Pali Rohárf989e552015-01-12 14:32:05 +010047#define I8K_SMM_GET_FAN_TYPE 0x03a3
Pali Rohár8f21d8e2015-01-12 14:32:03 +010048#define I8K_SMM_GET_NOM_SPEED 0x04a3
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define I8K_SMM_GET_TEMP 0x10a3
Pali Rohár5114b472015-01-12 14:31:57 +010050#define I8K_SMM_GET_TEMP_TYPE 0x11a3
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -070051#define I8K_SMM_GET_DELL_SIG1 0xfea3
52#define I8K_SMM_GET_DELL_SIG2 0xffa3
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define I8K_FAN_MULT 30
Pali Rohár8f21d8e2015-01-12 14:32:03 +010055#define I8K_FAN_MAX_RPM 30000
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define I8K_MAX_TEMP 127
57
58#define I8K_FN_NONE 0x00
59#define I8K_FN_UP 0x01
60#define I8K_FN_DOWN 0x02
61#define I8K_FN_MUTE 0x04
62#define I8K_FN_MASK 0x07
63#define I8K_FN_SHIFT 8
64
65#define I8K_POWER_AC 0x05
66#define I8K_POWER_BATTERY 0x01
67
Arnd Bergmann613655f2010-06-02 14:28:52 +020068static DEFINE_MUTEX(i8k_mutex);
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070069static char bios_version[4];
Pali Rohár76136632016-06-18 00:54:45 +020070static char bios_machineid[16];
Jean Delvare949a9d72011-05-25 20:43:33 +020071static struct device *i8k_hwmon_dev;
Guenter Roeck82ba1d32013-12-14 09:30:10 -080072static u32 i8k_hwmon_flags;
Pali Rohár8f21d8e2015-01-12 14:32:03 +010073static uint i8k_fan_mult = I8K_FAN_MULT;
Pali Rohár7f69fb02015-01-12 14:32:02 +010074static uint i8k_pwm_mult;
75static uint i8k_fan_max = I8K_FAN_HIGH;
Pali Rohár2744d2f2016-06-18 00:54:46 +020076static bool disallow_fan_type_call;
Guenter Roeck82ba1d32013-12-14 09:30:10 -080077
78#define I8K_HWMON_HAVE_TEMP1 (1 << 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -080079#define I8K_HWMON_HAVE_TEMP2 (1 << 1)
80#define I8K_HWMON_HAVE_TEMP3 (1 << 2)
81#define I8K_HWMON_HAVE_TEMP4 (1 << 3)
82#define I8K_HWMON_HAVE_FAN1 (1 << 4)
83#define I8K_HWMON_HAVE_FAN2 (1 << 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
Pali Rohára5afba12015-05-14 13:16:36 +020086MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
Pali Rohár039ae582015-05-14 13:16:37 +020087MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070088MODULE_LICENSE("GPL");
Pali Rohára5afba12015-05-14 13:16:36 +020089MODULE_ALIAS("i8k");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Rusty Russell90ab5ee2012-01-13 09:32:20 +103091static bool force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092module_param(force, bool, 0);
93MODULE_PARM_DESC(force, "Force loading without checking for supported models");
94
Rusty Russell90ab5ee2012-01-13 09:32:20 +103095static bool ignore_dmi;
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070096module_param(ignore_dmi, bool, 0);
97MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
98
Pali Rohár039ae582015-05-14 13:16:37 +020099#if IS_ENABLED(CONFIG_I8K)
Pali Rohár76136632016-06-18 00:54:45 +0200100static bool restricted = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101module_param(restricted, bool, 0);
Pali Rohár76136632016-06-18 00:54:45 +0200102MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030104static bool power_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105module_param(power_status, bool, 0600);
Pali Rohár76136632016-06-18 00:54:45 +0200106MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
Pali Rohár039ae582015-05-14 13:16:37 +0200107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100109static uint fan_mult;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100110module_param(fan_mult, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100111MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
Jochen Eisinger4ed99a22008-05-01 04:34:58 -0700112
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100113static uint fan_max;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100114module_param(fan_max, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100115MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
Guenter Roeck81474fc2014-06-21 08:08:10 -0700116
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700117struct smm_regs {
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700118 unsigned int eax;
Guenter Roeck12186f52013-12-14 09:30:09 -0800119 unsigned int ebx __packed;
120 unsigned int ecx __packed;
121 unsigned int edx __packed;
122 unsigned int esi __packed;
123 unsigned int edi __packed;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700124};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Jeff Garzik18552562007-10-03 15:15:40 -0400126static inline const char *i8k_get_dmi_data(int field)
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700127{
Jeff Garzik18552562007-10-03 15:15:40 -0400128 const char *dmi_data = dmi_get_system_info(field);
Dmitry Torokhov4f005552005-11-12 00:55:15 -0500129
130 return dmi_data && *dmi_data ? dmi_data : "?";
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/*
134 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
135 */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700136static int i8k_smm(struct smm_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700138 int rc;
139 int eax = regs->eax;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800140 cpumask_var_t old_mask;
141
142 /* SMM requires CPU 0 */
143 if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
144 return -ENOMEM;
145 cpumask_copy(old_mask, &current->cpus_allowed);
Guenter Roeck6d827fb2014-06-21 08:08:08 -0700146 rc = set_cpus_allowed_ptr(current, cpumask_of(0));
147 if (rc)
148 goto out;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800149 if (smp_processor_id() != 0) {
150 rc = -EBUSY;
151 goto out;
152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Bradley Smithfe04f222008-02-07 00:16:27 -0800154#if defined(CONFIG_X86_64)
Jim Bos22d32432010-11-15 21:22:37 +0100155 asm volatile("pushq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800156 "movl 0(%%rax),%%edx\n\t"
157 "pushq %%rdx\n\t"
158 "movl 4(%%rax),%%ebx\n\t"
159 "movl 8(%%rax),%%ecx\n\t"
160 "movl 12(%%rax),%%edx\n\t"
161 "movl 16(%%rax),%%esi\n\t"
162 "movl 20(%%rax),%%edi\n\t"
163 "popq %%rax\n\t"
164 "out %%al,$0xb2\n\t"
165 "out %%al,$0x84\n\t"
166 "xchgq %%rax,(%%rsp)\n\t"
167 "movl %%ebx,4(%%rax)\n\t"
168 "movl %%ecx,8(%%rax)\n\t"
169 "movl %%edx,12(%%rax)\n\t"
170 "movl %%esi,16(%%rax)\n\t"
171 "movl %%edi,20(%%rax)\n\t"
172 "popq %%rdx\n\t"
173 "movl %%edx,0(%%rax)\n\t"
Luca Tettamantibc1f4192011-05-25 20:43:31 +0200174 "pushfq\n\t"
175 "popq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800176 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800177 : "=a"(rc)
Bradley Smithfe04f222008-02-07 00:16:27 -0800178 : "a"(regs)
179 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
180#else
Jim Bos22d32432010-11-15 21:22:37 +0100181 asm volatile("pushl %%eax\n\t"
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700182 "movl 0(%%eax),%%edx\n\t"
183 "push %%edx\n\t"
184 "movl 4(%%eax),%%ebx\n\t"
185 "movl 8(%%eax),%%ecx\n\t"
186 "movl 12(%%eax),%%edx\n\t"
187 "movl 16(%%eax),%%esi\n\t"
188 "movl 20(%%eax),%%edi\n\t"
189 "popl %%eax\n\t"
190 "out %%al,$0xb2\n\t"
191 "out %%al,$0x84\n\t"
192 "xchgl %%eax,(%%esp)\n\t"
193 "movl %%ebx,4(%%eax)\n\t"
194 "movl %%ecx,8(%%eax)\n\t"
195 "movl %%edx,12(%%eax)\n\t"
196 "movl %%esi,16(%%eax)\n\t"
197 "movl %%edi,20(%%eax)\n\t"
198 "popl %%edx\n\t"
199 "movl %%edx,0(%%eax)\n\t"
200 "lahf\n\t"
201 "shrl $8,%%eax\n\t"
Jim Bos6b4e81d2010-11-13 12:13:53 +0100202 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800203 : "=a"(rc)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700204 : "a"(regs)
205 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
Bradley Smithfe04f222008-02-07 00:16:27 -0800206#endif
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700207 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800208 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800210out:
211 set_cpus_allowed_ptr(current, old_mask);
212 free_cpumask_var(old_mask);
213 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * Read the fan status.
218 */
219static int i8k_get_fan_status(int fan)
220{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700221 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700223 regs.ebx = fan & 0xff;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700224 return i8k_smm(&regs) ? : regs.eax & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227/*
228 * Read the fan speed in RPM.
229 */
230static int i8k_get_fan_speed(int fan)
231{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700232 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700234 regs.ebx = fan & 0xff;
Guenter Roeck26d09382013-12-14 09:30:19 -0800235 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/*
Pali Rohárf989e552015-01-12 14:32:05 +0100239 * Read the fan type.
240 */
Pali Rohár5ce91712016-06-18 00:54:47 +0200241static int _i8k_get_fan_type(int fan)
Pali Rohárf989e552015-01-12 14:32:05 +0100242{
243 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, };
244
Pali Rohár2744d2f2016-06-18 00:54:46 +0200245 if (disallow_fan_type_call)
246 return -EINVAL;
247
Pali Rohárf989e552015-01-12 14:32:05 +0100248 regs.ebx = fan & 0xff;
249 return i8k_smm(&regs) ? : regs.eax & 0xff;
250}
251
Pali Rohár5ce91712016-06-18 00:54:47 +0200252static int i8k_get_fan_type(int fan)
253{
254 /* I8K_SMM_GET_FAN_TYPE SMM call is expensive, so cache values */
255 static int types[2] = { INT_MIN, INT_MIN };
256
257 if (types[fan] == INT_MIN)
258 types[fan] = _i8k_get_fan_type(fan);
259
260 return types[fan];
261}
262
Pali Rohárf989e552015-01-12 14:32:05 +0100263/*
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100264 * Read the fan nominal rpm for specific fan speed.
265 */
266static int i8k_get_fan_nominal_speed(int fan, int speed)
267{
268 struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
269
270 regs.ebx = (fan & 0xff) | (speed << 8);
271 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
272}
273
274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * Set the fan speed (off, low, high). Returns the new fan status.
276 */
277static int i8k_set_fan(int fan, int speed)
278{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700279 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Guenter Roeck81474fc2014-06-21 08:08:10 -0700281 speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700282 regs.ebx = (fan & 0xff) | (speed << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700284 return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Pali Rohár5114b472015-01-12 14:31:57 +0100287static int i8k_get_temp_type(int sensor)
288{
289 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
290
291 regs.ebx = sensor & 0xff;
292 return i8k_smm(&regs) ? : regs.eax & 0xff;
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*
296 * Read the cpu temperature.
297 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100298static int _i8k_get_temp(int sensor)
299{
300 struct smm_regs regs = {
301 .eax = I8K_SMM_GET_TEMP,
302 .ebx = sensor & 0xff,
303 };
304
305 return i8k_smm(&regs) ? : regs.eax & 0xff;
306}
307
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700308static int i8k_get_temp(int sensor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Guenter Roeck564132d2015-01-12 14:32:00 +0100310 int temp = _i8k_get_temp(sensor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700312 /*
313 * Sometimes the temperature sensor returns 0x99, which is out of range.
Guenter Roeck564132d2015-01-12 14:32:00 +0100314 * In this case we retry (once) before returning an error.
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700315 # 1003655137 00000058 00005a4b
316 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
317 # 1003655139 00000054 00005c52
318 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100319 if (temp == 0x99) {
320 msleep(100);
321 temp = _i8k_get_temp(sensor);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700322 }
Guenter Roeck564132d2015-01-12 14:32:00 +0100323 /*
324 * Return -ENODATA for all invalid temperatures.
325 *
326 * Known instances are the 0x99 value as seen above as well as
327 * 0xc1 (193), which may be returned when trying to read the GPU
328 * temperature if the system supports a GPU and it is currently
329 * turned off.
330 */
Pali Rohár723493c2014-11-18 15:56:54 +0100331 if (temp > I8K_MAX_TEMP)
Pali Rohár83d514d2015-01-12 14:31:59 +0100332 return -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700334 return temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700337static int i8k_get_dell_signature(int req_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700339 struct smm_regs regs = { .eax = req_fn, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700340 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Guenter Roeck12186f52013-12-14 09:30:09 -0800342 rc = i8k_smm(&regs);
343 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700344 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700346 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Pali Rohár039ae582015-05-14 13:16:37 +0200349#if IS_ENABLED(CONFIG_I8K)
350
351/*
352 * Read the Fn key status.
353 */
354static int i8k_get_fn_status(void)
355{
356 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
357 int rc;
358
359 rc = i8k_smm(&regs);
360 if (rc < 0)
361 return rc;
362
363 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
364 case I8K_FN_UP:
365 return I8K_VOL_UP;
366 case I8K_FN_DOWN:
367 return I8K_VOL_DOWN;
368 case I8K_FN_MUTE:
369 return I8K_VOL_MUTE;
370 default:
371 return 0;
372 }
373}
374
375/*
376 * Read the power status.
377 */
378static int i8k_get_power_status(void)
379{
380 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
381 int rc;
382
383 rc = i8k_smm(&regs);
384 if (rc < 0)
385 return rc;
386
387 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
388}
389
390/*
391 * Procfs interface
392 */
393
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200394static int
395i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700397 int val = 0;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700398 int speed;
399 unsigned char buff[16];
400 int __user *argp = (int __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700402 if (!argp)
403 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700405 switch (cmd) {
406 case I8K_BIOS_VERSION:
Pali Rohár053ea642016-06-18 00:54:44 +0200407 if (!isdigit(bios_version[0]) || !isdigit(bios_version[1]) ||
408 !isdigit(bios_version[2]))
409 return -EINVAL;
410
Guenter Roecke551b152013-12-14 09:30:20 -0800411 val = (bios_version[0] << 16) |
412 (bios_version[1] << 8) | bios_version[2];
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700415 case I8K_MACHINE_ID:
Pali Rohár76136632016-06-18 00:54:45 +0200416 if (restricted && !capable(CAP_SYS_ADMIN))
417 return -EPERM;
418
419 memset(buff, 0, sizeof(buff));
420 strlcpy(buff, bios_machineid, sizeof(buff));
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700423 case I8K_FN_STATUS:
424 val = i8k_get_fn_status();
425 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700427 case I8K_POWER_STATUS:
428 val = i8k_get_power_status();
429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700431 case I8K_GET_TEMP:
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700432 val = i8k_get_temp(0);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700435 case I8K_GET_SPEED:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700436 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700437 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700438
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700439 val = i8k_get_fan_speed(val);
440 break;
441
442 case I8K_GET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700443 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700444 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700445
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700446 val = i8k_get_fan_status(val);
447 break;
448
449 case I8K_SET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700450 if (restricted && !capable(CAP_SYS_ADMIN))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700451 return -EPERM;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700452
453 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700454 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700455
456 if (copy_from_user(&speed, argp + 1, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700457 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700458
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700459 val = i8k_set_fan(val, speed);
460 break;
461
462 default:
463 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700466 if (val < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700467 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700469 switch (cmd) {
470 case I8K_BIOS_VERSION:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700471 if (copy_to_user(argp, &val, 4))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700472 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700473
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700474 break;
475 case I8K_MACHINE_ID:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700476 if (copy_to_user(argp, buff, 16))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700477 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700478
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700479 break;
480 default:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700481 if (copy_to_user(argp, &val, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700482 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700483
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200490static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
491{
492 long ret;
493
Arnd Bergmann613655f2010-06-02 14:28:52 +0200494 mutex_lock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200495 ret = i8k_ioctl_unlocked(fp, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200496 mutex_unlock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200497
498 return ret;
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
502 * Print the information for /proc/i8k.
503 */
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700504static int i8k_proc_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700506 int fn_key, cpu_temp, ac_power;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700507 int left_fan, right_fan, left_speed, right_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200509 cpu_temp = i8k_get_temp(0); /* 11100 µs */
510 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
511 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
512 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
513 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
514 fn_key = i8k_get_fn_status(); /* 750 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700515 if (power_status)
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200516 ac_power = i8k_get_power_status(); /* 14700 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700517 else
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700518 ac_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700520 /*
521 * Info:
522 *
523 * 1) Format version (this will change if format changes)
524 * 2) BIOS version
525 * 3) BIOS machine ID
526 * 4) Cpu temperature
527 * 5) Left fan status
528 * 6) Right fan status
529 * 7) Left fan speed
530 * 8) Right fan speed
531 * 9) AC power
532 * 10) Fn Key status
533 */
Joe Perches3a267d32015-02-21 18:53:47 -0800534 seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
535 I8K_PROC_FMT,
536 bios_version,
Pali Rohár76136632016-06-18 00:54:45 +0200537 (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : bios_machineid,
Joe Perches3a267d32015-02-21 18:53:47 -0800538 cpu_temp,
539 left_fan, right_fan, left_speed, right_speed,
540 ac_power, fn_key);
541
542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700545static int i8k_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700547 return single_open(file, i8k_proc_show, NULL);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700548}
549
Pali Rohár039ae582015-05-14 13:16:37 +0200550static const struct file_operations i8k_fops = {
551 .owner = THIS_MODULE,
552 .open = i8k_open_fs,
553 .read = seq_read,
554 .llseek = seq_lseek,
555 .release = single_release,
556 .unlocked_ioctl = i8k_ioctl,
557};
558
559static void __init i8k_init_procfs(void)
560{
561 /* Register the proc entry */
562 proc_create("i8k", 0, NULL, &i8k_fops);
563}
564
565static void __exit i8k_exit_procfs(void)
566{
567 remove_proc_entry("i8k", NULL);
568}
569
570#else
571
572static inline void __init i8k_init_procfs(void)
573{
574}
575
576static inline void __exit i8k_exit_procfs(void)
577{
578}
579
580#endif
Jean Delvare949a9d72011-05-25 20:43:33 +0200581
582/*
583 * Hwmon interface
584 */
585
Pali Rohár5114b472015-01-12 14:31:57 +0100586static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
587 struct device_attribute *devattr,
588 char *buf)
589{
590 static const char * const labels[] = {
591 "CPU",
592 "GPU",
593 "SODIMM",
594 "Other",
595 "Ambient",
596 "Other",
597 };
598 int index = to_sensor_dev_attr(devattr)->index;
599 int type;
600
601 type = i8k_get_temp_type(index);
602 if (type < 0)
603 return type;
604 if (type >= ARRAY_SIZE(labels))
605 type = ARRAY_SIZE(labels) - 1;
606 return sprintf(buf, "%s\n", labels[type]);
607}
608
Jean Delvare949a9d72011-05-25 20:43:33 +0200609static ssize_t i8k_hwmon_show_temp(struct device *dev,
610 struct device_attribute *devattr,
611 char *buf)
612{
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800613 int index = to_sensor_dev_attr(devattr)->index;
614 int temp;
Jean Delvare949a9d72011-05-25 20:43:33 +0200615
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800616 temp = i8k_get_temp(index);
617 if (temp < 0)
618 return temp;
619 return sprintf(buf, "%d\n", temp * 1000);
Jean Delvare949a9d72011-05-25 20:43:33 +0200620}
621
Pali Rohárf989e552015-01-12 14:32:05 +0100622static ssize_t i8k_hwmon_show_fan_label(struct device *dev,
623 struct device_attribute *devattr,
624 char *buf)
625{
626 static const char * const labels[] = {
627 "Processor Fan",
628 "Motherboard Fan",
629 "Video Fan",
630 "Power Supply Fan",
631 "Chipset Fan",
632 "Other Fan",
633 };
634 int index = to_sensor_dev_attr(devattr)->index;
635 bool dock = false;
636 int type;
637
638 type = i8k_get_fan_type(index);
639 if (type < 0)
640 return type;
641
642 if (type & 0x10) {
643 dock = true;
644 type &= 0x0F;
645 }
646
647 if (type >= ARRAY_SIZE(labels))
648 type = (ARRAY_SIZE(labels) - 1);
649
650 return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
651}
652
Jean Delvare949a9d72011-05-25 20:43:33 +0200653static ssize_t i8k_hwmon_show_fan(struct device *dev,
654 struct device_attribute *devattr,
655 char *buf)
656{
657 int index = to_sensor_dev_attr(devattr)->index;
658 int fan_speed;
659
660 fan_speed = i8k_get_fan_speed(index);
661 if (fan_speed < 0)
662 return fan_speed;
663 return sprintf(buf, "%d\n", fan_speed);
664}
665
Guenter Roecke7f5f272013-12-14 09:30:21 -0800666static ssize_t i8k_hwmon_show_pwm(struct device *dev,
667 struct device_attribute *devattr,
668 char *buf)
669{
670 int index = to_sensor_dev_attr(devattr)->index;
671 int status;
672
673 status = i8k_get_fan_status(index);
674 if (status < 0)
675 return -EIO;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700676 return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
Guenter Roecke7f5f272013-12-14 09:30:21 -0800677}
678
679static ssize_t i8k_hwmon_set_pwm(struct device *dev,
680 struct device_attribute *attr,
681 const char *buf, size_t count)
682{
683 int index = to_sensor_dev_attr(attr)->index;
684 unsigned long val;
685 int err;
686
687 err = kstrtoul(buf, 10, &val);
688 if (err)
689 return err;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700690 val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800691
692 mutex_lock(&i8k_mutex);
693 err = i8k_set_fan(index, val);
694 mutex_unlock(&i8k_mutex);
695
696 return err < 0 ? -EIO : count;
697}
698
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800699static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
Pali Rohár5114b472015-01-12 14:31:57 +0100700static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
701 0);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800702static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
Pali Rohár5114b472015-01-12 14:31:57 +0100703static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
704 1);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800705static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
Pali Rohár5114b472015-01-12 14:31:57 +0100706static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
707 2);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800708static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
Pali Rohár5114b472015-01-12 14:31:57 +0100709static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
710 3);
Pali Rohárf989e552015-01-12 14:32:05 +0100711static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, 0);
712static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
713 0);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800714static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100715 i8k_hwmon_set_pwm, 0);
Jean Delvare949a9d72011-05-25 20:43:33 +0200716static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
Pali Rohárf989e552015-01-12 14:32:05 +0100717 1);
718static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
719 1);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800720static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100721 i8k_hwmon_set_pwm, 1);
Jean Delvare949a9d72011-05-25 20:43:33 +0200722
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800723static struct attribute *i8k_attrs[] = {
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800724 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
Pali Rohár5114b472015-01-12 14:31:57 +0100725 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
726 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
727 &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */
728 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */
729 &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */
730 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */
731 &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */
732 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 8 */
Pali Rohárf989e552015-01-12 14:32:05 +0100733 &sensor_dev_attr_fan1_label.dev_attr.attr, /* 9 */
734 &sensor_dev_attr_pwm1.dev_attr.attr, /* 10 */
735 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 11 */
736 &sensor_dev_attr_fan2_label.dev_attr.attr, /* 12 */
737 &sensor_dev_attr_pwm2.dev_attr.attr, /* 13 */
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800738 NULL
739};
740
741static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
742 int index)
Jean Delvare949a9d72011-05-25 20:43:33 +0200743{
Pali Rohár2744d2f2016-06-18 00:54:46 +0200744 if (disallow_fan_type_call &&
745 (index == 9 || index == 12))
746 return 0;
Pali Rohár5114b472015-01-12 14:31:57 +0100747 if (index >= 0 && index <= 1 &&
748 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800749 return 0;
Pali Rohár5114b472015-01-12 14:31:57 +0100750 if (index >= 2 && index <= 3 &&
751 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800752 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700753 if (index >= 4 && index <= 5 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100754 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800755 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700756 if (index >= 6 && index <= 7 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100757 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
758 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100759 if (index >= 8 && index <= 10 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100760 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
761 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100762 if (index >= 11 && index <= 13 &&
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800763 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
764 return 0;
765
766 return attr->mode;
Jean Delvare949a9d72011-05-25 20:43:33 +0200767}
768
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800769static const struct attribute_group i8k_group = {
770 .attrs = i8k_attrs,
771 .is_visible = i8k_is_visible,
772};
773__ATTRIBUTE_GROUPS(i8k);
774
Jean Delvare949a9d72011-05-25 20:43:33 +0200775static int __init i8k_init_hwmon(void)
776{
777 int err;
778
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800779 i8k_hwmon_flags = 0;
780
Pali Rohár672af222015-01-12 14:31:58 +0100781 /* CPU temperature attributes, if temperature type is OK */
782 err = i8k_get_temp_type(0);
783 if (err >= 0)
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800784 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800785 /* check for additional temperature sensors */
Pali Rohár672af222015-01-12 14:31:58 +0100786 err = i8k_get_temp_type(1);
787 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800788 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
Pali Rohár672af222015-01-12 14:31:58 +0100789 err = i8k_get_temp_type(2);
790 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800791 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
Pali Rohár672af222015-01-12 14:31:58 +0100792 err = i8k_get_temp_type(3);
793 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800794 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800795
Pali Rohár5ce91712016-06-18 00:54:47 +0200796 /* First fan attributes, if fan status or type is OK */
797 err = i8k_get_fan_status(0);
798 if (err < 0)
799 err = i8k_get_fan_type(0);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800800 if (err >= 0)
801 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
802
Pali Rohár5ce91712016-06-18 00:54:47 +0200803 /* Second fan attributes, if fan status or type is OK */
804 err = i8k_get_fan_status(1);
805 if (err < 0)
806 err = i8k_get_fan_type(1);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800807 if (err >= 0)
808 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
809
Gabriele Mazzotta9026cae2015-06-27 15:22:05 +0200810 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm",
Pali Rohár039ae582015-05-14 13:16:37 +0200811 NULL, i8k_groups);
Jean Delvare949a9d72011-05-25 20:43:33 +0200812 if (IS_ERR(i8k_hwmon_dev)) {
813 err = PTR_ERR(i8k_hwmon_dev);
814 i8k_hwmon_dev = NULL;
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800815 pr_err("hwmon registration failed (%d)\n", err);
Jean Delvare949a9d72011-05-25 20:43:33 +0200816 return err;
817 }
Jean Delvare949a9d72011-05-25 20:43:33 +0200818 return 0;
Jean Delvare949a9d72011-05-25 20:43:33 +0200819}
820
Guenter Roeck81474fc2014-06-21 08:08:10 -0700821struct i8k_config_data {
Pali Rohár7f69fb02015-01-12 14:32:02 +0100822 uint fan_mult;
823 uint fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700824};
825
826enum i8k_configs {
Guenter Roeck7b883442014-06-21 08:08:12 -0700827 DELL_LATITUDE_D520,
828 DELL_PRECISION_490,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700829 DELL_STUDIO,
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100830 DELL_XPS,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700831};
832
833static const struct i8k_config_data i8k_config_data[] = {
Guenter Roeck7b883442014-06-21 08:08:12 -0700834 [DELL_LATITUDE_D520] = {
835 .fan_mult = 1,
836 .fan_max = I8K_FAN_TURBO,
837 },
838 [DELL_PRECISION_490] = {
839 .fan_mult = 1,
840 .fan_max = I8K_FAN_TURBO,
841 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700842 [DELL_STUDIO] = {
843 .fan_mult = 1,
844 .fan_max = I8K_FAN_HIGH,
845 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100846 [DELL_XPS] = {
Guenter Roeck81474fc2014-06-21 08:08:10 -0700847 .fan_mult = 1,
848 .fan_max = I8K_FAN_HIGH,
849 },
850};
851
Guenter Roeck12186f52013-12-14 09:30:09 -0800852static struct dmi_system_id i8k_dmi_table[] __initdata = {
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700853 {
854 .ident = "Dell Inspiron",
855 .matches = {
856 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
857 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
858 },
859 },
860 {
861 .ident = "Dell Latitude",
862 .matches = {
863 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
864 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
865 },
866 },
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700867 {
868 .ident = "Dell Inspiron 2",
869 .matches = {
870 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
871 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
872 },
873 },
874 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700875 .ident = "Dell Latitude D520",
876 .matches = {
877 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
878 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
879 },
880 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
881 },
882 {
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700883 .ident = "Dell Latitude 2",
884 .matches = {
885 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
886 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
887 },
888 },
Nick Warnea9000d02008-02-06 01:37:47 -0800889 { /* UK Inspiron 6400 */
890 .ident = "Dell Inspiron 3",
891 .matches = {
892 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
893 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
894 },
895 },
Frank Sorenson48103c52008-02-07 00:16:31 -0800896 {
897 .ident = "Dell Inspiron 3",
898 .matches = {
899 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
900 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
901 },
902 },
Andy Spencer7ab21a82009-01-02 16:19:13 +0000903 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700904 .ident = "Dell Precision 490",
905 .matches = {
906 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
907 DMI_MATCH(DMI_PRODUCT_NAME,
908 "Precision WorkStation 490"),
909 },
910 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
911 },
912 {
Andy Spencer7ab21a82009-01-02 16:19:13 +0000913 .ident = "Dell Precision",
914 .matches = {
915 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
916 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
917 },
918 },
Federico Heinzbef2a502009-01-02 16:19:23 +0000919 {
920 .ident = "Dell Vostro",
921 .matches = {
922 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
923 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
924 },
925 },
Alan Cox9aa5b012013-12-03 13:56:56 -0800926 {
927 .ident = "Dell XPS421",
928 .matches = {
929 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
930 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
931 },
932 },
Guenter Roeckff457bd2013-12-14 09:30:17 -0800933 {
934 .ident = "Dell Studio",
935 .matches = {
936 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
937 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
938 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700939 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
Guenter Roeckff457bd2013-12-14 09:30:17 -0800940 },
Guenter Roeck919a0302013-12-14 09:30:18 -0800941 {
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100942 .ident = "Dell XPS 13",
943 .matches = {
944 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
945 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
946 },
947 .driver_data = (void *)&i8k_config_data[DELL_XPS],
948 },
949 {
Guenter Roeck919a0302013-12-14 09:30:18 -0800950 .ident = "Dell XPS M140",
951 .matches = {
952 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
953 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
954 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100955 .driver_data = (void *)&i8k_config_data[DELL_XPS],
Guenter Roeck919a0302013-12-14 09:30:18 -0800956 },
Guenter Roeck12186f52013-12-14 09:30:09 -0800957 { }
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700958};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Pali Rohár148b1fd2014-10-18 00:17:49 +0200960MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
961
Pali Rohár2744d2f2016-06-18 00:54:46 +0200962/*
963 * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed
964 * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist
965 * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call.
966 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121
967 */
968static struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initdata = {
Pali Rohára4b45b22015-07-30 20:41:57 +0200969 {
Thorsten Leemhuis6220f4e2016-01-17 16:03:04 +0100970 .ident = "Dell Studio XPS 8000",
971 .matches = {
972 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
973 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
974 },
975 },
976 {
Pali Rohára4b45b22015-07-30 20:41:57 +0200977 .ident = "Dell Studio XPS 8100",
978 .matches = {
979 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
980 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
981 },
982 },
Pali Rohár2744d2f2016-06-18 00:54:46 +0200983 {
984 .ident = "Dell Inspiron 580",
985 .matches = {
986 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
987 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
988 },
989 },
Pali Rohára4b45b22015-07-30 20:41:57 +0200990 { }
991};
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993/*
994 * Probe for the presence of a supported laptop.
995 */
996static int __init i8k_probe(void)
997{
Guenter Roeck26d09382013-12-14 09:30:19 -0800998 const struct dmi_system_id *id;
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100999 int fan, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001002 * Get DMI information
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 */
Pali Rohár2744d2f2016-06-18 00:54:46 +02001004 if (!dmi_check_system(i8k_dmi_table)) {
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001005 if (!ignore_dmi && !force)
1006 return -ENODEV;
1007
Guenter Roeck60e71aa2013-12-14 09:30:08 -08001008 pr_info("not running on a supported Dell system.\n");
1009 pr_info("vendor=%s, model=%s, version=%s\n",
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001010 i8k_get_dmi_data(DMI_SYS_VENDOR),
1011 i8k_get_dmi_data(DMI_PRODUCT_NAME),
1012 i8k_get_dmi_data(DMI_BIOS_VERSION));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001014
Pali Rohár2744d2f2016-06-18 00:54:46 +02001015 if (dmi_check_system(i8k_blacklist_fan_type_dmi_table))
1016 disallow_fan_type_call = true;
1017
Guenter Roeck12186f52013-12-14 09:30:09 -08001018 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
1019 sizeof(bios_version));
Pali Rohár76136632016-06-18 00:54:45 +02001020 strlcpy(bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
1021 sizeof(bios_machineid));
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001024 * Get SMM Dell signature
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -07001026 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
1027 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
Guenter Roeck60e71aa2013-12-14 09:30:08 -08001028 pr_err("unable to get SMM Dell signature\n");
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001029 if (!force)
1030 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001033 /*
1034 * Set fan multiplier and maximal fan speed from dmi config
1035 * Values specified in module parameters override values from dmi
1036 */
Guenter Roeck26d09382013-12-14 09:30:19 -08001037 id = dmi_first_match(i8k_dmi_table);
Guenter Roeck81474fc2014-06-21 08:08:10 -07001038 if (id && id->driver_data) {
1039 const struct i8k_config_data *conf = id->driver_data;
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001040 if (!fan_mult && conf->fan_mult)
1041 fan_mult = conf->fan_mult;
1042 if (!fan_max && conf->fan_max)
1043 fan_max = conf->fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -07001044 }
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001045
1046 i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
Guenter Roeck81474fc2014-06-21 08:08:10 -07001047 i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
Guenter Roeck26d09382013-12-14 09:30:19 -08001048
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001049 if (!fan_mult) {
1050 /*
1051 * Autodetect fan multiplier based on nominal rpm
1052 * If fan reports rpm value too high then set multiplier to 1
1053 */
1054 for (fan = 0; fan < 2; ++fan) {
1055 ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
1056 if (ret < 0)
1057 continue;
1058 if (ret > I8K_FAN_MAX_RPM)
1059 i8k_fan_mult = 1;
1060 break;
1061 }
1062 } else {
1063 /* Fan multiplier was specified in module param or in dmi */
1064 i8k_fan_mult = fan_mult;
1065 }
1066
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001070static int __init i8k_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Jean Delvare949a9d72011-05-25 20:43:33 +02001072 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001074 /* Are we running on an supported laptop? */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001075 if (i8k_probe())
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001076 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Jean Delvare949a9d72011-05-25 20:43:33 +02001078 err = i8k_init_hwmon();
1079 if (err)
Pali Rohár039ae582015-05-14 13:16:37 +02001080 return err;
Jean Delvare949a9d72011-05-25 20:43:33 +02001081
Pali Rohár039ae582015-05-14 13:16:37 +02001082 i8k_init_procfs();
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001086static void __exit i8k_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Guenter Roeck82ba1d32013-12-14 09:30:10 -08001088 hwmon_device_unregister(i8k_hwmon_dev);
Pali Rohár039ae582015-05-14 13:16:37 +02001089 i8k_exit_procfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001092module_init(i8k_init);
1093module_exit(i8k_exit);