blob: acf9c0361d9f7e16f639fbbfeac642b1ca5258b5 [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)
Pali Rohár747bc8b2016-06-18 00:54:48 +020084#define I8K_HWMON_HAVE_FAN3 (1 << 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
Pali Rohára5afba12015-05-14 13:16:36 +020087MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
Pali Rohár039ae582015-05-14 13:16:37 +020088MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089MODULE_LICENSE("GPL");
Pali Rohára5afba12015-05-14 13:16:36 +020090MODULE_ALIAS("i8k");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Rusty Russell90ab5ee2012-01-13 09:32:20 +103092static bool force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093module_param(force, bool, 0);
94MODULE_PARM_DESC(force, "Force loading without checking for supported models");
95
Rusty Russell90ab5ee2012-01-13 09:32:20 +103096static bool ignore_dmi;
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070097module_param(ignore_dmi, bool, 0);
98MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
99
Pali Rohár039ae582015-05-14 13:16:37 +0200100#if IS_ENABLED(CONFIG_I8K)
Pali Rohár76136632016-06-18 00:54:45 +0200101static bool restricted = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102module_param(restricted, bool, 0);
Pali Rohár76136632016-06-18 00:54:45 +0200103MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030105static bool power_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106module_param(power_status, bool, 0600);
Pali Rohár76136632016-06-18 00:54:45 +0200107MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
Pali Rohár039ae582015-05-14 13:16:37 +0200108#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100110static uint fan_mult;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100111module_param(fan_mult, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100112MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
Jochen Eisinger4ed99a22008-05-01 04:34:58 -0700113
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100114static uint fan_max;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100115module_param(fan_max, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100116MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
Guenter Roeck81474fc2014-06-21 08:08:10 -0700117
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700118struct smm_regs {
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700119 unsigned int eax;
Guenter Roeck12186f52013-12-14 09:30:09 -0800120 unsigned int ebx __packed;
121 unsigned int ecx __packed;
122 unsigned int edx __packed;
123 unsigned int esi __packed;
124 unsigned int edi __packed;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700125};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Jeff Garzik18552562007-10-03 15:15:40 -0400127static inline const char *i8k_get_dmi_data(int field)
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700128{
Jeff Garzik18552562007-10-03 15:15:40 -0400129 const char *dmi_data = dmi_get_system_info(field);
Dmitry Torokhov4f005552005-11-12 00:55:15 -0500130
131 return dmi_data && *dmi_data ? dmi_data : "?";
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/*
135 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
136 */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700137static int i8k_smm(struct smm_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700139 int rc;
140 int eax = regs->eax;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800141 cpumask_var_t old_mask;
142
Pali Rohár9d58bec2016-06-18 00:54:49 +0200143#ifdef DEBUG
144 int ebx = regs->ebx;
145 unsigned long duration;
146 ktime_t calltime, delta, rettime;
147
148 calltime = ktime_get();
149#endif
150
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800151 /* SMM requires CPU 0 */
152 if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
153 return -ENOMEM;
154 cpumask_copy(old_mask, &current->cpus_allowed);
Guenter Roeck6d827fb2014-06-21 08:08:08 -0700155 rc = set_cpus_allowed_ptr(current, cpumask_of(0));
156 if (rc)
157 goto out;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800158 if (smp_processor_id() != 0) {
159 rc = -EBUSY;
160 goto out;
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Bradley Smithfe04f222008-02-07 00:16:27 -0800163#if defined(CONFIG_X86_64)
Jim Bos22d32432010-11-15 21:22:37 +0100164 asm volatile("pushq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800165 "movl 0(%%rax),%%edx\n\t"
166 "pushq %%rdx\n\t"
167 "movl 4(%%rax),%%ebx\n\t"
168 "movl 8(%%rax),%%ecx\n\t"
169 "movl 12(%%rax),%%edx\n\t"
170 "movl 16(%%rax),%%esi\n\t"
171 "movl 20(%%rax),%%edi\n\t"
172 "popq %%rax\n\t"
173 "out %%al,$0xb2\n\t"
174 "out %%al,$0x84\n\t"
175 "xchgq %%rax,(%%rsp)\n\t"
176 "movl %%ebx,4(%%rax)\n\t"
177 "movl %%ecx,8(%%rax)\n\t"
178 "movl %%edx,12(%%rax)\n\t"
179 "movl %%esi,16(%%rax)\n\t"
180 "movl %%edi,20(%%rax)\n\t"
181 "popq %%rdx\n\t"
182 "movl %%edx,0(%%rax)\n\t"
Luca Tettamantibc1f4192011-05-25 20:43:31 +0200183 "pushfq\n\t"
184 "popq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800185 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800186 : "=a"(rc)
Bradley Smithfe04f222008-02-07 00:16:27 -0800187 : "a"(regs)
188 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
189#else
Jim Bos22d32432010-11-15 21:22:37 +0100190 asm volatile("pushl %%eax\n\t"
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700191 "movl 0(%%eax),%%edx\n\t"
192 "push %%edx\n\t"
193 "movl 4(%%eax),%%ebx\n\t"
194 "movl 8(%%eax),%%ecx\n\t"
195 "movl 12(%%eax),%%edx\n\t"
196 "movl 16(%%eax),%%esi\n\t"
197 "movl 20(%%eax),%%edi\n\t"
198 "popl %%eax\n\t"
199 "out %%al,$0xb2\n\t"
200 "out %%al,$0x84\n\t"
201 "xchgl %%eax,(%%esp)\n\t"
202 "movl %%ebx,4(%%eax)\n\t"
203 "movl %%ecx,8(%%eax)\n\t"
204 "movl %%edx,12(%%eax)\n\t"
205 "movl %%esi,16(%%eax)\n\t"
206 "movl %%edi,20(%%eax)\n\t"
207 "popl %%edx\n\t"
208 "movl %%edx,0(%%eax)\n\t"
209 "lahf\n\t"
210 "shrl $8,%%eax\n\t"
Jim Bos6b4e81d2010-11-13 12:13:53 +0100211 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800212 : "=a"(rc)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700213 : "a"(regs)
214 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
Bradley Smithfe04f222008-02-07 00:16:27 -0800215#endif
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700216 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800217 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800219out:
220 set_cpus_allowed_ptr(current, old_mask);
221 free_cpumask_var(old_mask);
Pali Rohár9d58bec2016-06-18 00:54:49 +0200222
223#ifdef DEBUG
224 rettime = ktime_get();
225 delta = ktime_sub(rettime, calltime);
226 duration = ktime_to_ns(delta) >> 10;
227 pr_debug("smm(0x%.4x 0x%.4x) = 0x%.4x (took %7lu usecs)\n", eax, ebx,
228 (rc ? 0xffff : regs->eax & 0xffff), duration);
229#endif
230
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800231 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * Read the fan status.
236 */
237static int i8k_get_fan_status(int fan)
238{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700239 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700241 regs.ebx = fan & 0xff;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700242 return i8k_smm(&regs) ? : regs.eax & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245/*
246 * Read the fan speed in RPM.
247 */
248static int i8k_get_fan_speed(int fan)
249{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700250 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700252 regs.ebx = fan & 0xff;
Guenter Roeck26d09382013-12-14 09:30:19 -0800253 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256/*
Pali Rohárf989e552015-01-12 14:32:05 +0100257 * Read the fan type.
258 */
Pali Rohár5ce91712016-06-18 00:54:47 +0200259static int _i8k_get_fan_type(int fan)
Pali Rohárf989e552015-01-12 14:32:05 +0100260{
261 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, };
262
Pali Rohár2744d2f2016-06-18 00:54:46 +0200263 if (disallow_fan_type_call)
264 return -EINVAL;
265
Pali Rohárf989e552015-01-12 14:32:05 +0100266 regs.ebx = fan & 0xff;
267 return i8k_smm(&regs) ? : regs.eax & 0xff;
268}
269
Pali Rohár5ce91712016-06-18 00:54:47 +0200270static int i8k_get_fan_type(int fan)
271{
272 /* I8K_SMM_GET_FAN_TYPE SMM call is expensive, so cache values */
Pali Rohár747bc8b2016-06-18 00:54:48 +0200273 static int types[3] = { INT_MIN, INT_MIN, INT_MIN };
Pali Rohár5ce91712016-06-18 00:54:47 +0200274
275 if (types[fan] == INT_MIN)
276 types[fan] = _i8k_get_fan_type(fan);
277
278 return types[fan];
279}
280
Pali Rohárf989e552015-01-12 14:32:05 +0100281/*
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100282 * Read the fan nominal rpm for specific fan speed.
283 */
284static int i8k_get_fan_nominal_speed(int fan, int speed)
285{
286 struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
287
288 regs.ebx = (fan & 0xff) | (speed << 8);
289 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
290}
291
292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * Set the fan speed (off, low, high). Returns the new fan status.
294 */
295static int i8k_set_fan(int fan, int speed)
296{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700297 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Guenter Roeck81474fc2014-06-21 08:08:10 -0700299 speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700300 regs.ebx = (fan & 0xff) | (speed << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700302 return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Pali Rohár5114b472015-01-12 14:31:57 +0100305static int i8k_get_temp_type(int sensor)
306{
307 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
308
309 regs.ebx = sensor & 0xff;
310 return i8k_smm(&regs) ? : regs.eax & 0xff;
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
314 * Read the cpu temperature.
315 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100316static int _i8k_get_temp(int sensor)
317{
318 struct smm_regs regs = {
319 .eax = I8K_SMM_GET_TEMP,
320 .ebx = sensor & 0xff,
321 };
322
323 return i8k_smm(&regs) ? : regs.eax & 0xff;
324}
325
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700326static int i8k_get_temp(int sensor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Guenter Roeck564132d2015-01-12 14:32:00 +0100328 int temp = _i8k_get_temp(sensor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700330 /*
331 * Sometimes the temperature sensor returns 0x99, which is out of range.
Guenter Roeck564132d2015-01-12 14:32:00 +0100332 * In this case we retry (once) before returning an error.
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700333 # 1003655137 00000058 00005a4b
334 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
335 # 1003655139 00000054 00005c52
336 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100337 if (temp == 0x99) {
338 msleep(100);
339 temp = _i8k_get_temp(sensor);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700340 }
Guenter Roeck564132d2015-01-12 14:32:00 +0100341 /*
342 * Return -ENODATA for all invalid temperatures.
343 *
344 * Known instances are the 0x99 value as seen above as well as
345 * 0xc1 (193), which may be returned when trying to read the GPU
346 * temperature if the system supports a GPU and it is currently
347 * turned off.
348 */
Pali Rohár723493c2014-11-18 15:56:54 +0100349 if (temp > I8K_MAX_TEMP)
Pali Rohár83d514d2015-01-12 14:31:59 +0100350 return -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700352 return temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700355static int i8k_get_dell_signature(int req_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700357 struct smm_regs regs = { .eax = req_fn, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700358 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Guenter Roeck12186f52013-12-14 09:30:09 -0800360 rc = i8k_smm(&regs);
361 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700362 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700364 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Pali Rohár039ae582015-05-14 13:16:37 +0200367#if IS_ENABLED(CONFIG_I8K)
368
369/*
370 * Read the Fn key status.
371 */
372static int i8k_get_fn_status(void)
373{
374 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
375 int rc;
376
377 rc = i8k_smm(&regs);
378 if (rc < 0)
379 return rc;
380
381 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
382 case I8K_FN_UP:
383 return I8K_VOL_UP;
384 case I8K_FN_DOWN:
385 return I8K_VOL_DOWN;
386 case I8K_FN_MUTE:
387 return I8K_VOL_MUTE;
388 default:
389 return 0;
390 }
391}
392
393/*
394 * Read the power status.
395 */
396static int i8k_get_power_status(void)
397{
398 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
399 int rc;
400
401 rc = i8k_smm(&regs);
402 if (rc < 0)
403 return rc;
404
405 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
406}
407
408/*
409 * Procfs interface
410 */
411
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200412static int
413i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700415 int val = 0;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700416 int speed;
417 unsigned char buff[16];
418 int __user *argp = (int __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700420 if (!argp)
421 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700423 switch (cmd) {
424 case I8K_BIOS_VERSION:
Pali Rohár053ea642016-06-18 00:54:44 +0200425 if (!isdigit(bios_version[0]) || !isdigit(bios_version[1]) ||
426 !isdigit(bios_version[2]))
427 return -EINVAL;
428
Guenter Roecke551b152013-12-14 09:30:20 -0800429 val = (bios_version[0] << 16) |
430 (bios_version[1] << 8) | bios_version[2];
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700433 case I8K_MACHINE_ID:
Pali Rohár76136632016-06-18 00:54:45 +0200434 if (restricted && !capable(CAP_SYS_ADMIN))
435 return -EPERM;
436
437 memset(buff, 0, sizeof(buff));
438 strlcpy(buff, bios_machineid, sizeof(buff));
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700441 case I8K_FN_STATUS:
442 val = i8k_get_fn_status();
443 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700445 case I8K_POWER_STATUS:
446 val = i8k_get_power_status();
447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700449 case I8K_GET_TEMP:
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700450 val = i8k_get_temp(0);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700453 case I8K_GET_SPEED:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700454 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700455 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700456
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700457 val = i8k_get_fan_speed(val);
458 break;
459
460 case I8K_GET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700461 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700462 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700463
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700464 val = i8k_get_fan_status(val);
465 break;
466
467 case I8K_SET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700468 if (restricted && !capable(CAP_SYS_ADMIN))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700469 return -EPERM;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700470
471 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700472 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700473
474 if (copy_from_user(&speed, argp + 1, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700475 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700476
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700477 val = i8k_set_fan(val, speed);
478 break;
479
480 default:
481 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700484 if (val < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700485 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700487 switch (cmd) {
488 case I8K_BIOS_VERSION:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700489 if (copy_to_user(argp, &val, 4))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700490 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700491
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700492 break;
493 case I8K_MACHINE_ID:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700494 if (copy_to_user(argp, buff, 16))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700495 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700496
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700497 break;
498 default:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700499 if (copy_to_user(argp, &val, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700500 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700501
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700502 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200508static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
509{
510 long ret;
511
Arnd Bergmann613655f2010-06-02 14:28:52 +0200512 mutex_lock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200513 ret = i8k_ioctl_unlocked(fp, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200514 mutex_unlock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200515
516 return ret;
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/*
520 * Print the information for /proc/i8k.
521 */
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700522static int i8k_proc_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700524 int fn_key, cpu_temp, ac_power;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700525 int left_fan, right_fan, left_speed, right_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200527 cpu_temp = i8k_get_temp(0); /* 11100 µs */
528 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
529 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
530 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
531 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
532 fn_key = i8k_get_fn_status(); /* 750 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700533 if (power_status)
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200534 ac_power = i8k_get_power_status(); /* 14700 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700535 else
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700536 ac_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700538 /*
539 * Info:
540 *
541 * 1) Format version (this will change if format changes)
542 * 2) BIOS version
543 * 3) BIOS machine ID
544 * 4) Cpu temperature
545 * 5) Left fan status
546 * 6) Right fan status
547 * 7) Left fan speed
548 * 8) Right fan speed
549 * 9) AC power
550 * 10) Fn Key status
551 */
Joe Perches3a267d32015-02-21 18:53:47 -0800552 seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
553 I8K_PROC_FMT,
554 bios_version,
Pali Rohár76136632016-06-18 00:54:45 +0200555 (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : bios_machineid,
Joe Perches3a267d32015-02-21 18:53:47 -0800556 cpu_temp,
557 left_fan, right_fan, left_speed, right_speed,
558 ac_power, fn_key);
559
560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700563static int i8k_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700565 return single_open(file, i8k_proc_show, NULL);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700566}
567
Pali Rohár039ae582015-05-14 13:16:37 +0200568static const struct file_operations i8k_fops = {
569 .owner = THIS_MODULE,
570 .open = i8k_open_fs,
571 .read = seq_read,
572 .llseek = seq_lseek,
573 .release = single_release,
574 .unlocked_ioctl = i8k_ioctl,
575};
576
577static void __init i8k_init_procfs(void)
578{
579 /* Register the proc entry */
580 proc_create("i8k", 0, NULL, &i8k_fops);
581}
582
583static void __exit i8k_exit_procfs(void)
584{
585 remove_proc_entry("i8k", NULL);
586}
587
588#else
589
590static inline void __init i8k_init_procfs(void)
591{
592}
593
594static inline void __exit i8k_exit_procfs(void)
595{
596}
597
598#endif
Jean Delvare949a9d72011-05-25 20:43:33 +0200599
600/*
601 * Hwmon interface
602 */
603
Pali Rohár5114b472015-01-12 14:31:57 +0100604static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
605 struct device_attribute *devattr,
606 char *buf)
607{
608 static const char * const labels[] = {
609 "CPU",
610 "GPU",
611 "SODIMM",
612 "Other",
613 "Ambient",
614 "Other",
615 };
616 int index = to_sensor_dev_attr(devattr)->index;
617 int type;
618
619 type = i8k_get_temp_type(index);
620 if (type < 0)
621 return type;
622 if (type >= ARRAY_SIZE(labels))
623 type = ARRAY_SIZE(labels) - 1;
624 return sprintf(buf, "%s\n", labels[type]);
625}
626
Jean Delvare949a9d72011-05-25 20:43:33 +0200627static ssize_t i8k_hwmon_show_temp(struct device *dev,
628 struct device_attribute *devattr,
629 char *buf)
630{
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800631 int index = to_sensor_dev_attr(devattr)->index;
632 int temp;
Jean Delvare949a9d72011-05-25 20:43:33 +0200633
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800634 temp = i8k_get_temp(index);
635 if (temp < 0)
636 return temp;
637 return sprintf(buf, "%d\n", temp * 1000);
Jean Delvare949a9d72011-05-25 20:43:33 +0200638}
639
Pali Rohárf989e552015-01-12 14:32:05 +0100640static ssize_t i8k_hwmon_show_fan_label(struct device *dev,
641 struct device_attribute *devattr,
642 char *buf)
643{
644 static const char * const labels[] = {
645 "Processor Fan",
646 "Motherboard Fan",
647 "Video Fan",
648 "Power Supply Fan",
649 "Chipset Fan",
650 "Other Fan",
651 };
652 int index = to_sensor_dev_attr(devattr)->index;
653 bool dock = false;
654 int type;
655
656 type = i8k_get_fan_type(index);
657 if (type < 0)
658 return type;
659
660 if (type & 0x10) {
661 dock = true;
662 type &= 0x0F;
663 }
664
665 if (type >= ARRAY_SIZE(labels))
666 type = (ARRAY_SIZE(labels) - 1);
667
668 return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
669}
670
Jean Delvare949a9d72011-05-25 20:43:33 +0200671static ssize_t i8k_hwmon_show_fan(struct device *dev,
672 struct device_attribute *devattr,
673 char *buf)
674{
675 int index = to_sensor_dev_attr(devattr)->index;
676 int fan_speed;
677
678 fan_speed = i8k_get_fan_speed(index);
679 if (fan_speed < 0)
680 return fan_speed;
681 return sprintf(buf, "%d\n", fan_speed);
682}
683
Guenter Roecke7f5f272013-12-14 09:30:21 -0800684static ssize_t i8k_hwmon_show_pwm(struct device *dev,
685 struct device_attribute *devattr,
686 char *buf)
687{
688 int index = to_sensor_dev_attr(devattr)->index;
689 int status;
690
691 status = i8k_get_fan_status(index);
692 if (status < 0)
693 return -EIO;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700694 return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
Guenter Roecke7f5f272013-12-14 09:30:21 -0800695}
696
697static ssize_t i8k_hwmon_set_pwm(struct device *dev,
698 struct device_attribute *attr,
699 const char *buf, size_t count)
700{
701 int index = to_sensor_dev_attr(attr)->index;
702 unsigned long val;
703 int err;
704
705 err = kstrtoul(buf, 10, &val);
706 if (err)
707 return err;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700708 val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800709
710 mutex_lock(&i8k_mutex);
711 err = i8k_set_fan(index, val);
712 mutex_unlock(&i8k_mutex);
713
714 return err < 0 ? -EIO : count;
715}
716
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800717static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
Pali Rohár5114b472015-01-12 14:31:57 +0100718static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
719 0);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800720static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
Pali Rohár5114b472015-01-12 14:31:57 +0100721static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
722 1);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800723static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
Pali Rohár5114b472015-01-12 14:31:57 +0100724static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
725 2);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800726static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
Pali Rohár5114b472015-01-12 14:31:57 +0100727static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
728 3);
Pali Rohárf989e552015-01-12 14:32:05 +0100729static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, 0);
730static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
731 0);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800732static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100733 i8k_hwmon_set_pwm, 0);
Jean Delvare949a9d72011-05-25 20:43:33 +0200734static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
Pali Rohárf989e552015-01-12 14:32:05 +0100735 1);
736static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
737 1);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800738static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100739 i8k_hwmon_set_pwm, 1);
Pali Rohár747bc8b2016-06-18 00:54:48 +0200740static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
741 2);
742static SENSOR_DEVICE_ATTR(fan3_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
743 2);
744static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
745 i8k_hwmon_set_pwm, 2);
Jean Delvare949a9d72011-05-25 20:43:33 +0200746
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800747static struct attribute *i8k_attrs[] = {
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800748 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
Pali Rohár5114b472015-01-12 14:31:57 +0100749 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
750 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
751 &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */
752 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */
753 &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */
754 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */
755 &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */
756 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 8 */
Pali Rohárf989e552015-01-12 14:32:05 +0100757 &sensor_dev_attr_fan1_label.dev_attr.attr, /* 9 */
758 &sensor_dev_attr_pwm1.dev_attr.attr, /* 10 */
759 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 11 */
760 &sensor_dev_attr_fan2_label.dev_attr.attr, /* 12 */
761 &sensor_dev_attr_pwm2.dev_attr.attr, /* 13 */
Pali Rohár747bc8b2016-06-18 00:54:48 +0200762 &sensor_dev_attr_fan3_input.dev_attr.attr, /* 14 */
763 &sensor_dev_attr_fan3_label.dev_attr.attr, /* 15 */
764 &sensor_dev_attr_pwm3.dev_attr.attr, /* 16 */
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800765 NULL
766};
767
768static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
769 int index)
Jean Delvare949a9d72011-05-25 20:43:33 +0200770{
Pali Rohár2744d2f2016-06-18 00:54:46 +0200771 if (disallow_fan_type_call &&
Pali Rohár747bc8b2016-06-18 00:54:48 +0200772 (index == 9 || index == 12 || index == 15))
Pali Rohár2744d2f2016-06-18 00:54:46 +0200773 return 0;
Pali Rohár5114b472015-01-12 14:31:57 +0100774 if (index >= 0 && index <= 1 &&
775 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800776 return 0;
Pali Rohár5114b472015-01-12 14:31:57 +0100777 if (index >= 2 && index <= 3 &&
778 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800779 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700780 if (index >= 4 && index <= 5 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100781 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800782 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700783 if (index >= 6 && index <= 7 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100784 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
785 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100786 if (index >= 8 && index <= 10 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100787 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
788 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100789 if (index >= 11 && index <= 13 &&
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800790 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
791 return 0;
Pali Rohár747bc8b2016-06-18 00:54:48 +0200792 if (index >= 14 && index <= 16 &&
793 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN3))
794 return 0;
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800795
796 return attr->mode;
Jean Delvare949a9d72011-05-25 20:43:33 +0200797}
798
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800799static const struct attribute_group i8k_group = {
800 .attrs = i8k_attrs,
801 .is_visible = i8k_is_visible,
802};
803__ATTRIBUTE_GROUPS(i8k);
804
Jean Delvare949a9d72011-05-25 20:43:33 +0200805static int __init i8k_init_hwmon(void)
806{
807 int err;
808
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800809 i8k_hwmon_flags = 0;
810
Pali Rohár672af222015-01-12 14:31:58 +0100811 /* CPU temperature attributes, if temperature type is OK */
812 err = i8k_get_temp_type(0);
813 if (err >= 0)
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800814 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800815 /* check for additional temperature sensors */
Pali Rohár672af222015-01-12 14:31:58 +0100816 err = i8k_get_temp_type(1);
817 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800818 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
Pali Rohár672af222015-01-12 14:31:58 +0100819 err = i8k_get_temp_type(2);
820 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800821 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
Pali Rohár672af222015-01-12 14:31:58 +0100822 err = i8k_get_temp_type(3);
823 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800824 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800825
Pali Rohár5ce91712016-06-18 00:54:47 +0200826 /* First fan attributes, if fan status or type is OK */
827 err = i8k_get_fan_status(0);
828 if (err < 0)
829 err = i8k_get_fan_type(0);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800830 if (err >= 0)
831 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
832
Pali Rohár5ce91712016-06-18 00:54:47 +0200833 /* Second fan attributes, if fan status or type is OK */
834 err = i8k_get_fan_status(1);
835 if (err < 0)
836 err = i8k_get_fan_type(1);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800837 if (err >= 0)
838 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
839
Pali Rohár747bc8b2016-06-18 00:54:48 +0200840 /* Third fan attributes, if fan status or type is OK */
841 err = i8k_get_fan_status(2);
842 if (err < 0)
843 err = i8k_get_fan_type(2);
844 if (err >= 0)
845 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN3;
846
Gabriele Mazzotta9026cae2015-06-27 15:22:05 +0200847 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm",
Pali Rohár039ae582015-05-14 13:16:37 +0200848 NULL, i8k_groups);
Jean Delvare949a9d72011-05-25 20:43:33 +0200849 if (IS_ERR(i8k_hwmon_dev)) {
850 err = PTR_ERR(i8k_hwmon_dev);
851 i8k_hwmon_dev = NULL;
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800852 pr_err("hwmon registration failed (%d)\n", err);
Jean Delvare949a9d72011-05-25 20:43:33 +0200853 return err;
854 }
Jean Delvare949a9d72011-05-25 20:43:33 +0200855 return 0;
Jean Delvare949a9d72011-05-25 20:43:33 +0200856}
857
Guenter Roeck81474fc2014-06-21 08:08:10 -0700858struct i8k_config_data {
Pali Rohár7f69fb02015-01-12 14:32:02 +0100859 uint fan_mult;
860 uint fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700861};
862
863enum i8k_configs {
Guenter Roeck7b883442014-06-21 08:08:12 -0700864 DELL_LATITUDE_D520,
865 DELL_PRECISION_490,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700866 DELL_STUDIO,
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100867 DELL_XPS,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700868};
869
870static const struct i8k_config_data i8k_config_data[] = {
Guenter Roeck7b883442014-06-21 08:08:12 -0700871 [DELL_LATITUDE_D520] = {
872 .fan_mult = 1,
873 .fan_max = I8K_FAN_TURBO,
874 },
875 [DELL_PRECISION_490] = {
876 .fan_mult = 1,
877 .fan_max = I8K_FAN_TURBO,
878 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700879 [DELL_STUDIO] = {
880 .fan_mult = 1,
881 .fan_max = I8K_FAN_HIGH,
882 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100883 [DELL_XPS] = {
Guenter Roeck81474fc2014-06-21 08:08:10 -0700884 .fan_mult = 1,
885 .fan_max = I8K_FAN_HIGH,
886 },
887};
888
Guenter Roeck12186f52013-12-14 09:30:09 -0800889static struct dmi_system_id i8k_dmi_table[] __initdata = {
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700890 {
891 .ident = "Dell Inspiron",
892 .matches = {
893 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
894 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
895 },
896 },
897 {
898 .ident = "Dell Latitude",
899 .matches = {
900 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
901 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
902 },
903 },
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700904 {
905 .ident = "Dell Inspiron 2",
906 .matches = {
907 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
908 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
909 },
910 },
911 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700912 .ident = "Dell Latitude D520",
913 .matches = {
914 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
915 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
916 },
917 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
918 },
919 {
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700920 .ident = "Dell Latitude 2",
921 .matches = {
922 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
923 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
924 },
925 },
Nick Warnea9000d02008-02-06 01:37:47 -0800926 { /* UK Inspiron 6400 */
927 .ident = "Dell Inspiron 3",
928 .matches = {
929 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
930 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
931 },
932 },
Frank Sorenson48103c52008-02-07 00:16:31 -0800933 {
934 .ident = "Dell Inspiron 3",
935 .matches = {
936 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
937 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
938 },
939 },
Andy Spencer7ab21a82009-01-02 16:19:13 +0000940 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700941 .ident = "Dell Precision 490",
942 .matches = {
943 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
944 DMI_MATCH(DMI_PRODUCT_NAME,
945 "Precision WorkStation 490"),
946 },
947 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
948 },
949 {
Andy Spencer7ab21a82009-01-02 16:19:13 +0000950 .ident = "Dell Precision",
951 .matches = {
952 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
953 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
954 },
955 },
Federico Heinzbef2a502009-01-02 16:19:23 +0000956 {
957 .ident = "Dell Vostro",
958 .matches = {
959 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
960 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
961 },
962 },
Alan Cox9aa5b012013-12-03 13:56:56 -0800963 {
964 .ident = "Dell XPS421",
965 .matches = {
966 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
967 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
968 },
969 },
Guenter Roeckff457bd2013-12-14 09:30:17 -0800970 {
971 .ident = "Dell Studio",
972 .matches = {
973 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
974 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
975 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700976 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
Guenter Roeckff457bd2013-12-14 09:30:17 -0800977 },
Guenter Roeck919a0302013-12-14 09:30:18 -0800978 {
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100979 .ident = "Dell XPS 13",
980 .matches = {
981 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
982 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
983 },
984 .driver_data = (void *)&i8k_config_data[DELL_XPS],
985 },
986 {
Guenter Roeck919a0302013-12-14 09:30:18 -0800987 .ident = "Dell XPS M140",
988 .matches = {
989 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
990 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
991 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100992 .driver_data = (void *)&i8k_config_data[DELL_XPS],
Guenter Roeck919a0302013-12-14 09:30:18 -0800993 },
Guenter Roeck12186f52013-12-14 09:30:09 -0800994 { }
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700995};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Pali Rohár148b1fd2014-10-18 00:17:49 +0200997MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
998
Pali Rohár2744d2f2016-06-18 00:54:46 +0200999/*
1000 * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed
1001 * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist
1002 * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call.
1003 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121
1004 */
1005static struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initdata = {
Pali Rohára4b45b22015-07-30 20:41:57 +02001006 {
Thorsten Leemhuis6220f4e2016-01-17 16:03:04 +01001007 .ident = "Dell Studio XPS 8000",
1008 .matches = {
1009 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1010 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
1011 },
1012 },
1013 {
Pali Rohára4b45b22015-07-30 20:41:57 +02001014 .ident = "Dell Studio XPS 8100",
1015 .matches = {
1016 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1017 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
1018 },
1019 },
Pali Rohár2744d2f2016-06-18 00:54:46 +02001020 {
1021 .ident = "Dell Inspiron 580",
1022 .matches = {
1023 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1024 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
1025 },
1026 },
Pali Rohára4b45b22015-07-30 20:41:57 +02001027 { }
1028};
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030/*
1031 * Probe for the presence of a supported laptop.
1032 */
1033static int __init i8k_probe(void)
1034{
Guenter Roeck26d09382013-12-14 09:30:19 -08001035 const struct dmi_system_id *id;
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001036 int fan, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001039 * Get DMI information
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 */
Pali Rohár2744d2f2016-06-18 00:54:46 +02001041 if (!dmi_check_system(i8k_dmi_table)) {
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001042 if (!ignore_dmi && !force)
1043 return -ENODEV;
1044
Guenter Roeck60e71aa2013-12-14 09:30:08 -08001045 pr_info("not running on a supported Dell system.\n");
1046 pr_info("vendor=%s, model=%s, version=%s\n",
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001047 i8k_get_dmi_data(DMI_SYS_VENDOR),
1048 i8k_get_dmi_data(DMI_PRODUCT_NAME),
1049 i8k_get_dmi_data(DMI_BIOS_VERSION));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001051
Pali Rohár2744d2f2016-06-18 00:54:46 +02001052 if (dmi_check_system(i8k_blacklist_fan_type_dmi_table))
1053 disallow_fan_type_call = true;
1054
Guenter Roeck12186f52013-12-14 09:30:09 -08001055 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
1056 sizeof(bios_version));
Pali Rohár76136632016-06-18 00:54:45 +02001057 strlcpy(bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
1058 sizeof(bios_machineid));
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001061 * Get SMM Dell signature
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -07001063 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
1064 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
Guenter Roeck60e71aa2013-12-14 09:30:08 -08001065 pr_err("unable to get SMM Dell signature\n");
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001066 if (!force)
1067 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001070 /*
1071 * Set fan multiplier and maximal fan speed from dmi config
1072 * Values specified in module parameters override values from dmi
1073 */
Guenter Roeck26d09382013-12-14 09:30:19 -08001074 id = dmi_first_match(i8k_dmi_table);
Guenter Roeck81474fc2014-06-21 08:08:10 -07001075 if (id && id->driver_data) {
1076 const struct i8k_config_data *conf = id->driver_data;
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001077 if (!fan_mult && conf->fan_mult)
1078 fan_mult = conf->fan_mult;
1079 if (!fan_max && conf->fan_max)
1080 fan_max = conf->fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -07001081 }
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001082
1083 i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
Guenter Roeck81474fc2014-06-21 08:08:10 -07001084 i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
Guenter Roeck26d09382013-12-14 09:30:19 -08001085
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001086 if (!fan_mult) {
1087 /*
1088 * Autodetect fan multiplier based on nominal rpm
1089 * If fan reports rpm value too high then set multiplier to 1
1090 */
1091 for (fan = 0; fan < 2; ++fan) {
1092 ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
1093 if (ret < 0)
1094 continue;
1095 if (ret > I8K_FAN_MAX_RPM)
1096 i8k_fan_mult = 1;
1097 break;
1098 }
1099 } else {
1100 /* Fan multiplier was specified in module param or in dmi */
1101 i8k_fan_mult = fan_mult;
1102 }
1103
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001107static int __init i8k_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Jean Delvare949a9d72011-05-25 20:43:33 +02001109 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001111 /* Are we running on an supported laptop? */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001112 if (i8k_probe())
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001113 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Jean Delvare949a9d72011-05-25 20:43:33 +02001115 err = i8k_init_hwmon();
1116 if (err)
Pali Rohár039ae582015-05-14 13:16:37 +02001117 return err;
Jean Delvare949a9d72011-05-25 20:43:33 +02001118
Pali Rohár039ae582015-05-14 13:16:37 +02001119 i8k_init_procfs();
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001123static void __exit i8k_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Guenter Roeck82ba1d32013-12-14 09:30:10 -08001125 hwmon_device_unregister(i8k_hwmon_dev);
Pali Rohár039ae582015-05-14 13:16:37 +02001126 i8k_exit_procfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001129module_init(i8k_init);
1130module_exit(i8k_exit);