blob: 34704b0451b49700b5bf5ce7eb47dea905680a17 [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
Juergen Gross27046a32016-08-29 08:48:47 +020024#include <linux/cpu.h>
Guenter Roeck564132d2015-01-12 14:32:00 +010025#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/init.h>
29#include <linux/proc_fs.h>
Dmitry Torokhov352f8f82005-06-25 14:54:26 -070030#include <linux/seq_file.h>
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070031#include <linux/dmi.h>
Alexey Dobriyan7e80d0d2007-05-08 00:28:59 -070032#include <linux/capability.h>
Arnd Bergmann613655f2010-06-02 14:28:52 +020033#include <linux/mutex.h>
Jean Delvare949a9d72011-05-25 20:43:33 +020034#include <linux/hwmon.h>
35#include <linux/hwmon-sysfs.h>
Guenter Roeck12186f52013-12-14 09:30:09 -080036#include <linux/uaccess.h>
37#include <linux/io.h>
Guenter Roeckf36fdb92013-12-14 09:30:15 -080038#include <linux/sched.h>
Pali Rohár053ea642016-06-18 00:54:44 +020039#include <linux/ctype.h>
Juergen Gross27046a32016-08-29 08:48:47 +020040#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <linux/i8k.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define I8K_SMM_FN_STATUS 0x0025
45#define I8K_SMM_POWER_STATUS 0x0069
46#define I8K_SMM_SET_FAN 0x01a3
47#define I8K_SMM_GET_FAN 0x00a3
48#define I8K_SMM_GET_SPEED 0x02a3
Pali Rohárf989e552015-01-12 14:32:05 +010049#define I8K_SMM_GET_FAN_TYPE 0x03a3
Pali Rohár8f21d8e2015-01-12 14:32:03 +010050#define I8K_SMM_GET_NOM_SPEED 0x04a3
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define I8K_SMM_GET_TEMP 0x10a3
Pali Rohár5114b472015-01-12 14:31:57 +010052#define I8K_SMM_GET_TEMP_TYPE 0x11a3
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -070053#define I8K_SMM_GET_DELL_SIG1 0xfea3
54#define I8K_SMM_GET_DELL_SIG2 0xffa3
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define I8K_FAN_MULT 30
Pali Rohár8f21d8e2015-01-12 14:32:03 +010057#define I8K_FAN_MAX_RPM 30000
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define I8K_MAX_TEMP 127
59
60#define I8K_FN_NONE 0x00
61#define I8K_FN_UP 0x01
62#define I8K_FN_DOWN 0x02
63#define I8K_FN_MUTE 0x04
64#define I8K_FN_MASK 0x07
65#define I8K_FN_SHIFT 8
66
67#define I8K_POWER_AC 0x05
68#define I8K_POWER_BATTERY 0x01
69
Arnd Bergmann613655f2010-06-02 14:28:52 +020070static DEFINE_MUTEX(i8k_mutex);
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070071static char bios_version[4];
Pali Rohár76136632016-06-18 00:54:45 +020072static char bios_machineid[16];
Jean Delvare949a9d72011-05-25 20:43:33 +020073static struct device *i8k_hwmon_dev;
Guenter Roeck82ba1d32013-12-14 09:30:10 -080074static u32 i8k_hwmon_flags;
Pali Rohár8f21d8e2015-01-12 14:32:03 +010075static uint i8k_fan_mult = I8K_FAN_MULT;
Pali Rohár7f69fb02015-01-12 14:32:02 +010076static uint i8k_pwm_mult;
77static uint i8k_fan_max = I8K_FAN_HIGH;
Pali Rohár2744d2f2016-06-18 00:54:46 +020078static bool disallow_fan_type_call;
Guenter Roeck82ba1d32013-12-14 09:30:10 -080079
80#define I8K_HWMON_HAVE_TEMP1 (1 << 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -080081#define I8K_HWMON_HAVE_TEMP2 (1 << 1)
82#define I8K_HWMON_HAVE_TEMP3 (1 << 2)
83#define I8K_HWMON_HAVE_TEMP4 (1 << 3)
84#define I8K_HWMON_HAVE_FAN1 (1 << 4)
85#define I8K_HWMON_HAVE_FAN2 (1 << 5)
Pali Rohár747bc8b2016-06-18 00:54:48 +020086#define I8K_HWMON_HAVE_FAN3 (1 << 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
Pali Rohára5afba12015-05-14 13:16:36 +020089MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
Pali Rohár039ae582015-05-14 13:16:37 +020090MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091MODULE_LICENSE("GPL");
Pali Rohára5afba12015-05-14 13:16:36 +020092MODULE_ALIAS("i8k");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Rusty Russell90ab5ee2012-01-13 09:32:20 +103094static bool force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095module_param(force, bool, 0);
96MODULE_PARM_DESC(force, "Force loading without checking for supported models");
97
Rusty Russell90ab5ee2012-01-13 09:32:20 +103098static bool ignore_dmi;
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070099module_param(ignore_dmi, bool, 0);
100MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
101
Pali Rohár039ae582015-05-14 13:16:37 +0200102#if IS_ENABLED(CONFIG_I8K)
Pali Rohár76136632016-06-18 00:54:45 +0200103static bool restricted = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104module_param(restricted, bool, 0);
Pali Rohár76136632016-06-18 00:54:45 +0200105MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030107static bool power_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108module_param(power_status, bool, 0600);
Pali Rohár76136632016-06-18 00:54:45 +0200109MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
Pali Rohár039ae582015-05-14 13:16:37 +0200110#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100112static uint fan_mult;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100113module_param(fan_mult, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100114MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
Jochen Eisinger4ed99a22008-05-01 04:34:58 -0700115
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100116static uint fan_max;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100117module_param(fan_max, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100118MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
Guenter Roeck81474fc2014-06-21 08:08:10 -0700119
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700120struct smm_regs {
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700121 unsigned int eax;
Guenter Roeck12186f52013-12-14 09:30:09 -0800122 unsigned int ebx __packed;
123 unsigned int ecx __packed;
124 unsigned int edx __packed;
125 unsigned int esi __packed;
126 unsigned int edi __packed;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700127};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jeff Garzik18552562007-10-03 15:15:40 -0400129static inline const char *i8k_get_dmi_data(int field)
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700130{
Jeff Garzik18552562007-10-03 15:15:40 -0400131 const char *dmi_data = dmi_get_system_info(field);
Dmitry Torokhov4f005552005-11-12 00:55:15 -0500132
133 return dmi_data && *dmi_data ? dmi_data : "?";
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/*
137 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
138 */
Juergen Gross27046a32016-08-29 08:48:47 +0200139static int i8k_smm_func(void *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700141 int rc;
Juergen Gross27046a32016-08-29 08:48:47 +0200142 struct smm_regs *regs = par;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700143 int eax = regs->eax;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800144
Pali Rohár9d58bec2016-06-18 00:54:49 +0200145#ifdef DEBUG
146 int ebx = regs->ebx;
147 unsigned long duration;
148 ktime_t calltime, delta, rettime;
149
150 calltime = ktime_get();
151#endif
152
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800153 /* SMM requires CPU 0 */
Juergen Gross27046a32016-08-29 08:48:47 +0200154 if (smp_processor_id() != 0)
155 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Bradley Smithfe04f222008-02-07 00:16:27 -0800157#if defined(CONFIG_X86_64)
Jim Bos22d32432010-11-15 21:22:37 +0100158 asm volatile("pushq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800159 "movl 0(%%rax),%%edx\n\t"
160 "pushq %%rdx\n\t"
161 "movl 4(%%rax),%%ebx\n\t"
162 "movl 8(%%rax),%%ecx\n\t"
163 "movl 12(%%rax),%%edx\n\t"
164 "movl 16(%%rax),%%esi\n\t"
165 "movl 20(%%rax),%%edi\n\t"
166 "popq %%rax\n\t"
167 "out %%al,$0xb2\n\t"
168 "out %%al,$0x84\n\t"
169 "xchgq %%rax,(%%rsp)\n\t"
170 "movl %%ebx,4(%%rax)\n\t"
171 "movl %%ecx,8(%%rax)\n\t"
172 "movl %%edx,12(%%rax)\n\t"
173 "movl %%esi,16(%%rax)\n\t"
174 "movl %%edi,20(%%rax)\n\t"
175 "popq %%rdx\n\t"
176 "movl %%edx,0(%%rax)\n\t"
Luca Tettamantibc1f4192011-05-25 20:43:31 +0200177 "pushfq\n\t"
178 "popq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800179 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800180 : "=a"(rc)
Bradley Smithfe04f222008-02-07 00:16:27 -0800181 : "a"(regs)
182 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
183#else
Jim Bos22d32432010-11-15 21:22:37 +0100184 asm volatile("pushl %%eax\n\t"
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700185 "movl 0(%%eax),%%edx\n\t"
186 "push %%edx\n\t"
187 "movl 4(%%eax),%%ebx\n\t"
188 "movl 8(%%eax),%%ecx\n\t"
189 "movl 12(%%eax),%%edx\n\t"
190 "movl 16(%%eax),%%esi\n\t"
191 "movl 20(%%eax),%%edi\n\t"
192 "popl %%eax\n\t"
193 "out %%al,$0xb2\n\t"
194 "out %%al,$0x84\n\t"
195 "xchgl %%eax,(%%esp)\n\t"
196 "movl %%ebx,4(%%eax)\n\t"
197 "movl %%ecx,8(%%eax)\n\t"
198 "movl %%edx,12(%%eax)\n\t"
199 "movl %%esi,16(%%eax)\n\t"
200 "movl %%edi,20(%%eax)\n\t"
201 "popl %%edx\n\t"
202 "movl %%edx,0(%%eax)\n\t"
203 "lahf\n\t"
204 "shrl $8,%%eax\n\t"
Jim Bos6b4e81d2010-11-13 12:13:53 +0100205 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800206 : "=a"(rc)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700207 : "a"(regs)
208 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
Bradley Smithfe04f222008-02-07 00:16:27 -0800209#endif
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700210 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800211 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Pali Rohár9d58bec2016-06-18 00:54:49 +0200213#ifdef DEBUG
214 rettime = ktime_get();
215 delta = ktime_sub(rettime, calltime);
216 duration = ktime_to_ns(delta) >> 10;
217 pr_debug("smm(0x%.4x 0x%.4x) = 0x%.4x (took %7lu usecs)\n", eax, ebx,
218 (rc ? 0xffff : regs->eax & 0xffff), duration);
219#endif
220
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800221 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*
Juergen Gross27046a32016-08-29 08:48:47 +0200225 * Call the System Management Mode BIOS.
226 */
227static int i8k_smm(struct smm_regs *regs)
228{
229 int ret;
230
231 get_online_cpus();
232 ret = smp_call_on_cpu(0, i8k_smm_func, regs, true);
233 put_online_cpus();
234
235 return ret;
236}
237
238/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * Read the fan status.
240 */
241static int i8k_get_fan_status(int fan)
242{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700243 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700245 regs.ebx = fan & 0xff;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700246 return i8k_smm(&regs) ? : regs.eax & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249/*
250 * Read the fan speed in RPM.
251 */
252static int i8k_get_fan_speed(int fan)
253{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700254 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700256 regs.ebx = fan & 0xff;
Guenter Roeck26d09382013-12-14 09:30:19 -0800257 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/*
Pali Rohárf989e552015-01-12 14:32:05 +0100261 * Read the fan type.
262 */
Pali Rohár5ce91712016-06-18 00:54:47 +0200263static int _i8k_get_fan_type(int fan)
Pali Rohárf989e552015-01-12 14:32:05 +0100264{
265 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, };
266
Pali Rohár2744d2f2016-06-18 00:54:46 +0200267 if (disallow_fan_type_call)
268 return -EINVAL;
269
Pali Rohárf989e552015-01-12 14:32:05 +0100270 regs.ebx = fan & 0xff;
271 return i8k_smm(&regs) ? : regs.eax & 0xff;
272}
273
Pali Rohár5ce91712016-06-18 00:54:47 +0200274static int i8k_get_fan_type(int fan)
275{
276 /* I8K_SMM_GET_FAN_TYPE SMM call is expensive, so cache values */
Pali Rohár747bc8b2016-06-18 00:54:48 +0200277 static int types[3] = { INT_MIN, INT_MIN, INT_MIN };
Pali Rohár5ce91712016-06-18 00:54:47 +0200278
279 if (types[fan] == INT_MIN)
280 types[fan] = _i8k_get_fan_type(fan);
281
282 return types[fan];
283}
284
Pali Rohárf989e552015-01-12 14:32:05 +0100285/*
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100286 * Read the fan nominal rpm for specific fan speed.
287 */
288static int i8k_get_fan_nominal_speed(int fan, int speed)
289{
290 struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
291
292 regs.ebx = (fan & 0xff) | (speed << 8);
293 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
294}
295
296/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Set the fan speed (off, low, high). Returns the new fan status.
298 */
299static int i8k_set_fan(int fan, int speed)
300{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700301 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Guenter Roeck81474fc2014-06-21 08:08:10 -0700303 speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700304 regs.ebx = (fan & 0xff) | (speed << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700306 return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Pali Rohár5114b472015-01-12 14:31:57 +0100309static int i8k_get_temp_type(int sensor)
310{
311 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
312
313 regs.ebx = sensor & 0xff;
314 return i8k_smm(&regs) ? : regs.eax & 0xff;
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
318 * Read the cpu temperature.
319 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100320static int _i8k_get_temp(int sensor)
321{
322 struct smm_regs regs = {
323 .eax = I8K_SMM_GET_TEMP,
324 .ebx = sensor & 0xff,
325 };
326
327 return i8k_smm(&regs) ? : regs.eax & 0xff;
328}
329
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700330static int i8k_get_temp(int sensor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Guenter Roeck564132d2015-01-12 14:32:00 +0100332 int temp = _i8k_get_temp(sensor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700334 /*
335 * Sometimes the temperature sensor returns 0x99, which is out of range.
Guenter Roeck564132d2015-01-12 14:32:00 +0100336 * In this case we retry (once) before returning an error.
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700337 # 1003655137 00000058 00005a4b
338 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
339 # 1003655139 00000054 00005c52
340 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100341 if (temp == 0x99) {
342 msleep(100);
343 temp = _i8k_get_temp(sensor);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700344 }
Guenter Roeck564132d2015-01-12 14:32:00 +0100345 /*
346 * Return -ENODATA for all invalid temperatures.
347 *
348 * Known instances are the 0x99 value as seen above as well as
349 * 0xc1 (193), which may be returned when trying to read the GPU
350 * temperature if the system supports a GPU and it is currently
351 * turned off.
352 */
Pali Rohár723493c2014-11-18 15:56:54 +0100353 if (temp > I8K_MAX_TEMP)
Pali Rohár83d514d2015-01-12 14:31:59 +0100354 return -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700356 return temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700359static int i8k_get_dell_signature(int req_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700361 struct smm_regs regs = { .eax = req_fn, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700362 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Guenter Roeck12186f52013-12-14 09:30:09 -0800364 rc = i8k_smm(&regs);
365 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700366 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700368 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Pali Rohár039ae582015-05-14 13:16:37 +0200371#if IS_ENABLED(CONFIG_I8K)
372
373/*
374 * Read the Fn key status.
375 */
376static int i8k_get_fn_status(void)
377{
378 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
379 int rc;
380
381 rc = i8k_smm(&regs);
382 if (rc < 0)
383 return rc;
384
385 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
386 case I8K_FN_UP:
387 return I8K_VOL_UP;
388 case I8K_FN_DOWN:
389 return I8K_VOL_DOWN;
390 case I8K_FN_MUTE:
391 return I8K_VOL_MUTE;
392 default:
393 return 0;
394 }
395}
396
397/*
398 * Read the power status.
399 */
400static int i8k_get_power_status(void)
401{
402 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
403 int rc;
404
405 rc = i8k_smm(&regs);
406 if (rc < 0)
407 return rc;
408
409 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
410}
411
412/*
413 * Procfs interface
414 */
415
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200416static int
417i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700419 int val = 0;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700420 int speed;
421 unsigned char buff[16];
422 int __user *argp = (int __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700424 if (!argp)
425 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700427 switch (cmd) {
428 case I8K_BIOS_VERSION:
Pali Rohár053ea642016-06-18 00:54:44 +0200429 if (!isdigit(bios_version[0]) || !isdigit(bios_version[1]) ||
430 !isdigit(bios_version[2]))
431 return -EINVAL;
432
Guenter Roecke551b152013-12-14 09:30:20 -0800433 val = (bios_version[0] << 16) |
434 (bios_version[1] << 8) | bios_version[2];
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700437 case I8K_MACHINE_ID:
Pali Rohár76136632016-06-18 00:54:45 +0200438 if (restricted && !capable(CAP_SYS_ADMIN))
439 return -EPERM;
440
441 memset(buff, 0, sizeof(buff));
442 strlcpy(buff, bios_machineid, sizeof(buff));
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700443 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700445 case I8K_FN_STATUS:
446 val = i8k_get_fn_status();
447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700449 case I8K_POWER_STATUS:
450 val = i8k_get_power_status();
451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700453 case I8K_GET_TEMP:
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700454 val = i8k_get_temp(0);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700457 case I8K_GET_SPEED:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700458 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700459 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700460
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700461 val = i8k_get_fan_speed(val);
462 break;
463
464 case I8K_GET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700465 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700466 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700467
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700468 val = i8k_get_fan_status(val);
469 break;
470
471 case I8K_SET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700472 if (restricted && !capable(CAP_SYS_ADMIN))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700473 return -EPERM;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700474
475 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700476 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700477
478 if (copy_from_user(&speed, argp + 1, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700479 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700480
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700481 val = i8k_set_fan(val, speed);
482 break;
483
484 default:
485 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700488 if (val < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700489 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700491 switch (cmd) {
492 case I8K_BIOS_VERSION:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700493 if (copy_to_user(argp, &val, 4))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700494 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700495
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700496 break;
497 case I8K_MACHINE_ID:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700498 if (copy_to_user(argp, buff, 16))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700499 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700500
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700501 break;
502 default:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700503 if (copy_to_user(argp, &val, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700504 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700505
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200512static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
513{
514 long ret;
515
Arnd Bergmann613655f2010-06-02 14:28:52 +0200516 mutex_lock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200517 ret = i8k_ioctl_unlocked(fp, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200518 mutex_unlock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200519
520 return ret;
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/*
524 * Print the information for /proc/i8k.
525 */
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700526static int i8k_proc_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700528 int fn_key, cpu_temp, ac_power;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700529 int left_fan, right_fan, left_speed, right_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200531 cpu_temp = i8k_get_temp(0); /* 11100 µs */
532 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
533 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
534 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
535 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
536 fn_key = i8k_get_fn_status(); /* 750 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700537 if (power_status)
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200538 ac_power = i8k_get_power_status(); /* 14700 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700539 else
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700540 ac_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700542 /*
543 * Info:
544 *
545 * 1) Format version (this will change if format changes)
546 * 2) BIOS version
547 * 3) BIOS machine ID
548 * 4) Cpu temperature
549 * 5) Left fan status
550 * 6) Right fan status
551 * 7) Left fan speed
552 * 8) Right fan speed
553 * 9) AC power
554 * 10) Fn Key status
555 */
Joe Perches3a267d32015-02-21 18:53:47 -0800556 seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
557 I8K_PROC_FMT,
558 bios_version,
Pali Rohár76136632016-06-18 00:54:45 +0200559 (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : bios_machineid,
Joe Perches3a267d32015-02-21 18:53:47 -0800560 cpu_temp,
561 left_fan, right_fan, left_speed, right_speed,
562 ac_power, fn_key);
563
564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700567static int i8k_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700569 return single_open(file, i8k_proc_show, NULL);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700570}
571
Pali Rohár039ae582015-05-14 13:16:37 +0200572static const struct file_operations i8k_fops = {
573 .owner = THIS_MODULE,
574 .open = i8k_open_fs,
575 .read = seq_read,
576 .llseek = seq_lseek,
577 .release = single_release,
578 .unlocked_ioctl = i8k_ioctl,
579};
580
581static void __init i8k_init_procfs(void)
582{
583 /* Register the proc entry */
584 proc_create("i8k", 0, NULL, &i8k_fops);
585}
586
587static void __exit i8k_exit_procfs(void)
588{
589 remove_proc_entry("i8k", NULL);
590}
591
592#else
593
594static inline void __init i8k_init_procfs(void)
595{
596}
597
598static inline void __exit i8k_exit_procfs(void)
599{
600}
601
602#endif
Jean Delvare949a9d72011-05-25 20:43:33 +0200603
604/*
605 * Hwmon interface
606 */
607
Pali Rohár5114b472015-01-12 14:31:57 +0100608static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
609 struct device_attribute *devattr,
610 char *buf)
611{
612 static const char * const labels[] = {
613 "CPU",
614 "GPU",
615 "SODIMM",
616 "Other",
617 "Ambient",
618 "Other",
619 };
620 int index = to_sensor_dev_attr(devattr)->index;
621 int type;
622
623 type = i8k_get_temp_type(index);
624 if (type < 0)
625 return type;
626 if (type >= ARRAY_SIZE(labels))
627 type = ARRAY_SIZE(labels) - 1;
628 return sprintf(buf, "%s\n", labels[type]);
629}
630
Jean Delvare949a9d72011-05-25 20:43:33 +0200631static ssize_t i8k_hwmon_show_temp(struct device *dev,
632 struct device_attribute *devattr,
633 char *buf)
634{
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800635 int index = to_sensor_dev_attr(devattr)->index;
636 int temp;
Jean Delvare949a9d72011-05-25 20:43:33 +0200637
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800638 temp = i8k_get_temp(index);
639 if (temp < 0)
640 return temp;
641 return sprintf(buf, "%d\n", temp * 1000);
Jean Delvare949a9d72011-05-25 20:43:33 +0200642}
643
Pali Rohárf989e552015-01-12 14:32:05 +0100644static ssize_t i8k_hwmon_show_fan_label(struct device *dev,
645 struct device_attribute *devattr,
646 char *buf)
647{
648 static const char * const labels[] = {
649 "Processor Fan",
650 "Motherboard Fan",
651 "Video Fan",
652 "Power Supply Fan",
653 "Chipset Fan",
654 "Other Fan",
655 };
656 int index = to_sensor_dev_attr(devattr)->index;
657 bool dock = false;
658 int type;
659
660 type = i8k_get_fan_type(index);
661 if (type < 0)
662 return type;
663
664 if (type & 0x10) {
665 dock = true;
666 type &= 0x0F;
667 }
668
669 if (type >= ARRAY_SIZE(labels))
670 type = (ARRAY_SIZE(labels) - 1);
671
672 return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
673}
674
Jean Delvare949a9d72011-05-25 20:43:33 +0200675static ssize_t i8k_hwmon_show_fan(struct device *dev,
676 struct device_attribute *devattr,
677 char *buf)
678{
679 int index = to_sensor_dev_attr(devattr)->index;
680 int fan_speed;
681
682 fan_speed = i8k_get_fan_speed(index);
683 if (fan_speed < 0)
684 return fan_speed;
685 return sprintf(buf, "%d\n", fan_speed);
686}
687
Guenter Roecke7f5f272013-12-14 09:30:21 -0800688static ssize_t i8k_hwmon_show_pwm(struct device *dev,
689 struct device_attribute *devattr,
690 char *buf)
691{
692 int index = to_sensor_dev_attr(devattr)->index;
693 int status;
694
695 status = i8k_get_fan_status(index);
696 if (status < 0)
697 return -EIO;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700698 return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
Guenter Roecke7f5f272013-12-14 09:30:21 -0800699}
700
701static ssize_t i8k_hwmon_set_pwm(struct device *dev,
702 struct device_attribute *attr,
703 const char *buf, size_t count)
704{
705 int index = to_sensor_dev_attr(attr)->index;
706 unsigned long val;
707 int err;
708
709 err = kstrtoul(buf, 10, &val);
710 if (err)
711 return err;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700712 val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800713
714 mutex_lock(&i8k_mutex);
715 err = i8k_set_fan(index, val);
716 mutex_unlock(&i8k_mutex);
717
718 return err < 0 ? -EIO : count;
719}
720
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800721static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
Pali Rohár5114b472015-01-12 14:31:57 +0100722static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
723 0);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800724static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
Pali Rohár5114b472015-01-12 14:31:57 +0100725static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
726 1);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800727static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
Pali Rohár5114b472015-01-12 14:31:57 +0100728static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
729 2);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800730static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
Pali Rohár5114b472015-01-12 14:31:57 +0100731static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
732 3);
Pali Rohárf989e552015-01-12 14:32:05 +0100733static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, 0);
734static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
735 0);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800736static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100737 i8k_hwmon_set_pwm, 0);
Jean Delvare949a9d72011-05-25 20:43:33 +0200738static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
Pali Rohárf989e552015-01-12 14:32:05 +0100739 1);
740static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
741 1);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800742static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100743 i8k_hwmon_set_pwm, 1);
Pali Rohár747bc8b2016-06-18 00:54:48 +0200744static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
745 2);
746static SENSOR_DEVICE_ATTR(fan3_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
747 2);
748static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
749 i8k_hwmon_set_pwm, 2);
Jean Delvare949a9d72011-05-25 20:43:33 +0200750
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800751static struct attribute *i8k_attrs[] = {
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800752 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
Pali Rohár5114b472015-01-12 14:31:57 +0100753 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
754 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
755 &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */
756 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */
757 &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */
758 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */
759 &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */
760 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 8 */
Pali Rohárf989e552015-01-12 14:32:05 +0100761 &sensor_dev_attr_fan1_label.dev_attr.attr, /* 9 */
762 &sensor_dev_attr_pwm1.dev_attr.attr, /* 10 */
763 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 11 */
764 &sensor_dev_attr_fan2_label.dev_attr.attr, /* 12 */
765 &sensor_dev_attr_pwm2.dev_attr.attr, /* 13 */
Pali Rohár747bc8b2016-06-18 00:54:48 +0200766 &sensor_dev_attr_fan3_input.dev_attr.attr, /* 14 */
767 &sensor_dev_attr_fan3_label.dev_attr.attr, /* 15 */
768 &sensor_dev_attr_pwm3.dev_attr.attr, /* 16 */
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800769 NULL
770};
771
772static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
773 int index)
Jean Delvare949a9d72011-05-25 20:43:33 +0200774{
Pali Rohár2744d2f2016-06-18 00:54:46 +0200775 if (disallow_fan_type_call &&
Pali Rohár747bc8b2016-06-18 00:54:48 +0200776 (index == 9 || index == 12 || index == 15))
Pali Rohár2744d2f2016-06-18 00:54:46 +0200777 return 0;
Pali Rohár5114b472015-01-12 14:31:57 +0100778 if (index >= 0 && index <= 1 &&
779 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800780 return 0;
Pali Rohár5114b472015-01-12 14:31:57 +0100781 if (index >= 2 && index <= 3 &&
782 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800783 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700784 if (index >= 4 && index <= 5 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100785 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800786 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700787 if (index >= 6 && index <= 7 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100788 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
789 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100790 if (index >= 8 && index <= 10 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100791 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
792 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100793 if (index >= 11 && index <= 13 &&
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800794 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
795 return 0;
Pali Rohár747bc8b2016-06-18 00:54:48 +0200796 if (index >= 14 && index <= 16 &&
797 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN3))
798 return 0;
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800799
800 return attr->mode;
Jean Delvare949a9d72011-05-25 20:43:33 +0200801}
802
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800803static const struct attribute_group i8k_group = {
804 .attrs = i8k_attrs,
805 .is_visible = i8k_is_visible,
806};
807__ATTRIBUTE_GROUPS(i8k);
808
Jean Delvare949a9d72011-05-25 20:43:33 +0200809static int __init i8k_init_hwmon(void)
810{
811 int err;
812
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800813 i8k_hwmon_flags = 0;
814
Pali Rohár672af222015-01-12 14:31:58 +0100815 /* CPU temperature attributes, if temperature type is OK */
816 err = i8k_get_temp_type(0);
817 if (err >= 0)
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800818 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800819 /* check for additional temperature sensors */
Pali Rohár672af222015-01-12 14:31:58 +0100820 err = i8k_get_temp_type(1);
821 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800822 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
Pali Rohár672af222015-01-12 14:31:58 +0100823 err = i8k_get_temp_type(2);
824 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800825 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
Pali Rohár672af222015-01-12 14:31:58 +0100826 err = i8k_get_temp_type(3);
827 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800828 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800829
Pali Rohár5ce91712016-06-18 00:54:47 +0200830 /* First fan attributes, if fan status or type is OK */
831 err = i8k_get_fan_status(0);
832 if (err < 0)
833 err = i8k_get_fan_type(0);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800834 if (err >= 0)
835 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
836
Pali Rohár5ce91712016-06-18 00:54:47 +0200837 /* Second fan attributes, if fan status or type is OK */
838 err = i8k_get_fan_status(1);
839 if (err < 0)
840 err = i8k_get_fan_type(1);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800841 if (err >= 0)
842 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
843
Pali Rohár747bc8b2016-06-18 00:54:48 +0200844 /* Third fan attributes, if fan status or type is OK */
845 err = i8k_get_fan_status(2);
846 if (err < 0)
847 err = i8k_get_fan_type(2);
848 if (err >= 0)
849 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN3;
850
Gabriele Mazzotta9026cae2015-06-27 15:22:05 +0200851 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm",
Pali Rohár039ae582015-05-14 13:16:37 +0200852 NULL, i8k_groups);
Jean Delvare949a9d72011-05-25 20:43:33 +0200853 if (IS_ERR(i8k_hwmon_dev)) {
854 err = PTR_ERR(i8k_hwmon_dev);
855 i8k_hwmon_dev = NULL;
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800856 pr_err("hwmon registration failed (%d)\n", err);
Jean Delvare949a9d72011-05-25 20:43:33 +0200857 return err;
858 }
Jean Delvare949a9d72011-05-25 20:43:33 +0200859 return 0;
Jean Delvare949a9d72011-05-25 20:43:33 +0200860}
861
Guenter Roeck81474fc2014-06-21 08:08:10 -0700862struct i8k_config_data {
Pali Rohár7f69fb02015-01-12 14:32:02 +0100863 uint fan_mult;
864 uint fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700865};
866
867enum i8k_configs {
Guenter Roeck7b883442014-06-21 08:08:12 -0700868 DELL_LATITUDE_D520,
869 DELL_PRECISION_490,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700870 DELL_STUDIO,
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100871 DELL_XPS,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700872};
873
874static const struct i8k_config_data i8k_config_data[] = {
Guenter Roeck7b883442014-06-21 08:08:12 -0700875 [DELL_LATITUDE_D520] = {
876 .fan_mult = 1,
877 .fan_max = I8K_FAN_TURBO,
878 },
879 [DELL_PRECISION_490] = {
880 .fan_mult = 1,
881 .fan_max = I8K_FAN_TURBO,
882 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700883 [DELL_STUDIO] = {
884 .fan_mult = 1,
885 .fan_max = I8K_FAN_HIGH,
886 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100887 [DELL_XPS] = {
Guenter Roeck81474fc2014-06-21 08:08:10 -0700888 .fan_mult = 1,
889 .fan_max = I8K_FAN_HIGH,
890 },
891};
892
Guenter Roeck12186f52013-12-14 09:30:09 -0800893static struct dmi_system_id i8k_dmi_table[] __initdata = {
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700894 {
895 .ident = "Dell Inspiron",
896 .matches = {
897 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
898 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
899 },
900 },
901 {
902 .ident = "Dell Latitude",
903 .matches = {
904 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
905 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
906 },
907 },
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700908 {
909 .ident = "Dell Inspiron 2",
910 .matches = {
911 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
912 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
913 },
914 },
915 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700916 .ident = "Dell Latitude D520",
917 .matches = {
918 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
919 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
920 },
921 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
922 },
923 {
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700924 .ident = "Dell Latitude 2",
925 .matches = {
926 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
927 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
928 },
929 },
Nick Warnea9000d02008-02-06 01:37:47 -0800930 { /* UK Inspiron 6400 */
931 .ident = "Dell Inspiron 3",
932 .matches = {
933 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
934 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
935 },
936 },
Frank Sorenson48103c52008-02-07 00:16:31 -0800937 {
938 .ident = "Dell Inspiron 3",
939 .matches = {
940 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
941 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
942 },
943 },
Andy Spencer7ab21a82009-01-02 16:19:13 +0000944 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700945 .ident = "Dell Precision 490",
946 .matches = {
947 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
948 DMI_MATCH(DMI_PRODUCT_NAME,
949 "Precision WorkStation 490"),
950 },
951 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
952 },
953 {
Andy Spencer7ab21a82009-01-02 16:19:13 +0000954 .ident = "Dell Precision",
955 .matches = {
956 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
957 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
958 },
959 },
Federico Heinzbef2a502009-01-02 16:19:23 +0000960 {
961 .ident = "Dell Vostro",
962 .matches = {
963 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
964 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
965 },
966 },
Alan Cox9aa5b012013-12-03 13:56:56 -0800967 {
968 .ident = "Dell XPS421",
969 .matches = {
970 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
971 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
972 },
973 },
Guenter Roeckff457bd2013-12-14 09:30:17 -0800974 {
975 .ident = "Dell Studio",
976 .matches = {
977 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
978 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
979 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700980 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
Guenter Roeckff457bd2013-12-14 09:30:17 -0800981 },
Guenter Roeck919a0302013-12-14 09:30:18 -0800982 {
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100983 .ident = "Dell XPS 13",
984 .matches = {
985 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
986 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
987 },
988 .driver_data = (void *)&i8k_config_data[DELL_XPS],
989 },
990 {
Guenter Roeck919a0302013-12-14 09:30:18 -0800991 .ident = "Dell XPS M140",
992 .matches = {
993 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
994 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
995 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100996 .driver_data = (void *)&i8k_config_data[DELL_XPS],
Guenter Roeck919a0302013-12-14 09:30:18 -0800997 },
Guenter Roeck12186f52013-12-14 09:30:09 -0800998 { }
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700999};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Pali Rohár148b1fd2014-10-18 00:17:49 +02001001MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
1002
Pali Rohár2744d2f2016-06-18 00:54:46 +02001003/*
1004 * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed
1005 * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist
1006 * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call.
1007 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121
1008 */
1009static struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initdata = {
Pali Rohára4b45b22015-07-30 20:41:57 +02001010 {
Thorsten Leemhuis6220f4e2016-01-17 16:03:04 +01001011 .ident = "Dell Studio XPS 8000",
1012 .matches = {
1013 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1014 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
1015 },
1016 },
1017 {
Pali Rohára4b45b22015-07-30 20:41:57 +02001018 .ident = "Dell Studio XPS 8100",
1019 .matches = {
1020 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1021 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
1022 },
1023 },
Pali Rohár2744d2f2016-06-18 00:54:46 +02001024 {
1025 .ident = "Dell Inspiron 580",
1026 .matches = {
1027 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1028 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
1029 },
1030 },
Pali Rohára4b45b22015-07-30 20:41:57 +02001031 { }
1032};
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
1035 * Probe for the presence of a supported laptop.
1036 */
1037static int __init i8k_probe(void)
1038{
Guenter Roeck26d09382013-12-14 09:30:19 -08001039 const struct dmi_system_id *id;
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001040 int fan, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001043 * Get DMI information
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 */
Pali Rohár2744d2f2016-06-18 00:54:46 +02001045 if (!dmi_check_system(i8k_dmi_table)) {
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001046 if (!ignore_dmi && !force)
1047 return -ENODEV;
1048
Guenter Roeck60e71aa2013-12-14 09:30:08 -08001049 pr_info("not running on a supported Dell system.\n");
1050 pr_info("vendor=%s, model=%s, version=%s\n",
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001051 i8k_get_dmi_data(DMI_SYS_VENDOR),
1052 i8k_get_dmi_data(DMI_PRODUCT_NAME),
1053 i8k_get_dmi_data(DMI_BIOS_VERSION));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001055
Pali Rohár2744d2f2016-06-18 00:54:46 +02001056 if (dmi_check_system(i8k_blacklist_fan_type_dmi_table))
1057 disallow_fan_type_call = true;
1058
Guenter Roeck12186f52013-12-14 09:30:09 -08001059 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
1060 sizeof(bios_version));
Pali Rohár76136632016-06-18 00:54:45 +02001061 strlcpy(bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
1062 sizeof(bios_machineid));
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001065 * Get SMM Dell signature
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -07001067 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
1068 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
Guenter Roeck60e71aa2013-12-14 09:30:08 -08001069 pr_err("unable to get SMM Dell signature\n");
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001070 if (!force)
1071 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001074 /*
1075 * Set fan multiplier and maximal fan speed from dmi config
1076 * Values specified in module parameters override values from dmi
1077 */
Guenter Roeck26d09382013-12-14 09:30:19 -08001078 id = dmi_first_match(i8k_dmi_table);
Guenter Roeck81474fc2014-06-21 08:08:10 -07001079 if (id && id->driver_data) {
1080 const struct i8k_config_data *conf = id->driver_data;
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001081 if (!fan_mult && conf->fan_mult)
1082 fan_mult = conf->fan_mult;
1083 if (!fan_max && conf->fan_max)
1084 fan_max = conf->fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -07001085 }
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001086
1087 i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
Guenter Roeck81474fc2014-06-21 08:08:10 -07001088 i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
Guenter Roeck26d09382013-12-14 09:30:19 -08001089
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001090 if (!fan_mult) {
1091 /*
1092 * Autodetect fan multiplier based on nominal rpm
1093 * If fan reports rpm value too high then set multiplier to 1
1094 */
1095 for (fan = 0; fan < 2; ++fan) {
1096 ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
1097 if (ret < 0)
1098 continue;
1099 if (ret > I8K_FAN_MAX_RPM)
1100 i8k_fan_mult = 1;
1101 break;
1102 }
1103 } else {
1104 /* Fan multiplier was specified in module param or in dmi */
1105 i8k_fan_mult = fan_mult;
1106 }
1107
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001111static int __init i8k_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Jean Delvare949a9d72011-05-25 20:43:33 +02001113 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001115 /* Are we running on an supported laptop? */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001116 if (i8k_probe())
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001117 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Jean Delvare949a9d72011-05-25 20:43:33 +02001119 err = i8k_init_hwmon();
1120 if (err)
Pali Rohár039ae582015-05-14 13:16:37 +02001121 return err;
Jean Delvare949a9d72011-05-25 20:43:33 +02001122
Pali Rohár039ae582015-05-14 13:16:37 +02001123 i8k_init_procfs();
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001127static void __exit i8k_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Guenter Roeck82ba1d32013-12-14 09:30:10 -08001129 hwmon_device_unregister(i8k_hwmon_dev);
Pali Rohár039ae582015-05-14 13:16:37 +02001130 i8k_exit_procfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001133module_init(i8k_init);
1134module_exit(i8k_exit);