blob: 65d314b0f197add98cdf212e73be7ff190c33d56 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <linux/i8k.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define I8K_SMM_FN_STATUS 0x0025
42#define I8K_SMM_POWER_STATUS 0x0069
43#define I8K_SMM_SET_FAN 0x01a3
44#define I8K_SMM_GET_FAN 0x00a3
45#define I8K_SMM_GET_SPEED 0x02a3
Pali Rohárf989e552015-01-12 14:32:05 +010046#define I8K_SMM_GET_FAN_TYPE 0x03a3
Pali Rohár8f21d8e2015-01-12 14:32:03 +010047#define I8K_SMM_GET_NOM_SPEED 0x04a3
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define I8K_SMM_GET_TEMP 0x10a3
Pali Rohár5114b472015-01-12 14:31:57 +010049#define I8K_SMM_GET_TEMP_TYPE 0x11a3
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -070050#define I8K_SMM_GET_DELL_SIG1 0xfea3
51#define I8K_SMM_GET_DELL_SIG2 0xffa3
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define I8K_FAN_MULT 30
Pali Rohár8f21d8e2015-01-12 14:32:03 +010054#define I8K_FAN_MAX_RPM 30000
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define I8K_MAX_TEMP 127
56
57#define I8K_FN_NONE 0x00
58#define I8K_FN_UP 0x01
59#define I8K_FN_DOWN 0x02
60#define I8K_FN_MUTE 0x04
61#define I8K_FN_MASK 0x07
62#define I8K_FN_SHIFT 8
63
64#define I8K_POWER_AC 0x05
65#define I8K_POWER_BATTERY 0x01
66
Arnd Bergmann613655f2010-06-02 14:28:52 +020067static DEFINE_MUTEX(i8k_mutex);
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070068static char bios_version[4];
Jean Delvare949a9d72011-05-25 20:43:33 +020069static struct device *i8k_hwmon_dev;
Guenter Roeck82ba1d32013-12-14 09:30:10 -080070static u32 i8k_hwmon_flags;
Pali Rohár8f21d8e2015-01-12 14:32:03 +010071static uint i8k_fan_mult = I8K_FAN_MULT;
Pali Rohár7f69fb02015-01-12 14:32:02 +010072static uint i8k_pwm_mult;
73static uint i8k_fan_max = I8K_FAN_HIGH;
Guenter Roeck82ba1d32013-12-14 09:30:10 -080074
75#define I8K_HWMON_HAVE_TEMP1 (1 << 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -080076#define I8K_HWMON_HAVE_TEMP2 (1 << 1)
77#define I8K_HWMON_HAVE_TEMP3 (1 << 2)
78#define I8K_HWMON_HAVE_TEMP4 (1 << 3)
79#define I8K_HWMON_HAVE_FAN1 (1 << 4)
80#define I8K_HWMON_HAVE_FAN2 (1 << 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
Pali Rohára5afba12015-05-14 13:16:36 +020083MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
85MODULE_LICENSE("GPL");
Pali Rohára5afba12015-05-14 13:16:36 +020086MODULE_ALIAS("i8k");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Rusty Russell90ab5ee2012-01-13 09:32:20 +103088static bool force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089module_param(force, bool, 0);
90MODULE_PARM_DESC(force, "Force loading without checking for supported models");
91
Rusty Russell90ab5ee2012-01-13 09:32:20 +103092static bool ignore_dmi;
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070093module_param(ignore_dmi, bool, 0);
94MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
95
Rusty Russell90ab5ee2012-01-13 09:32:20 +103096static bool restricted;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097module_param(restricted, bool, 0);
98MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
99
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030100static bool power_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101module_param(power_status, bool, 0600);
102MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
103
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100104static uint fan_mult;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100105module_param(fan_mult, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100106MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
Jochen Eisinger4ed99a22008-05-01 04:34:58 -0700107
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100108static uint fan_max;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100109module_param(fan_max, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100110MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
Guenter Roeck81474fc2014-06-21 08:08:10 -0700111
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700112static int i8k_open_fs(struct inode *inode, struct file *file);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200113static long i8k_ioctl(struct file *, unsigned int, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Arjan van de Ven62322d22006-07-03 00:24:21 -0700115static const struct file_operations i8k_fops = {
Denis V. Lunev1b502212008-04-29 01:02:34 -0700116 .owner = THIS_MODULE,
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700117 .open = i8k_open_fs,
118 .read = seq_read,
119 .llseek = seq_lseek,
120 .release = single_release,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200121 .unlocked_ioctl = i8k_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700124struct smm_regs {
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700125 unsigned int eax;
Guenter Roeck12186f52013-12-14 09:30:09 -0800126 unsigned int ebx __packed;
127 unsigned int ecx __packed;
128 unsigned int edx __packed;
129 unsigned int esi __packed;
130 unsigned int edi __packed;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700131};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jeff Garzik18552562007-10-03 15:15:40 -0400133static inline const char *i8k_get_dmi_data(int field)
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700134{
Jeff Garzik18552562007-10-03 15:15:40 -0400135 const char *dmi_data = dmi_get_system_info(field);
Dmitry Torokhov4f005552005-11-12 00:55:15 -0500136
137 return dmi_data && *dmi_data ? dmi_data : "?";
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/*
141 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
142 */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700143static int i8k_smm(struct smm_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700145 int rc;
146 int eax = regs->eax;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800147 cpumask_var_t old_mask;
148
149 /* SMM requires CPU 0 */
150 if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
151 return -ENOMEM;
152 cpumask_copy(old_mask, &current->cpus_allowed);
Guenter Roeck6d827fb2014-06-21 08:08:08 -0700153 rc = set_cpus_allowed_ptr(current, cpumask_of(0));
154 if (rc)
155 goto out;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800156 if (smp_processor_id() != 0) {
157 rc = -EBUSY;
158 goto out;
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Bradley Smithfe04f222008-02-07 00:16:27 -0800161#if defined(CONFIG_X86_64)
Jim Bos22d32432010-11-15 21:22:37 +0100162 asm volatile("pushq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800163 "movl 0(%%rax),%%edx\n\t"
164 "pushq %%rdx\n\t"
165 "movl 4(%%rax),%%ebx\n\t"
166 "movl 8(%%rax),%%ecx\n\t"
167 "movl 12(%%rax),%%edx\n\t"
168 "movl 16(%%rax),%%esi\n\t"
169 "movl 20(%%rax),%%edi\n\t"
170 "popq %%rax\n\t"
171 "out %%al,$0xb2\n\t"
172 "out %%al,$0x84\n\t"
173 "xchgq %%rax,(%%rsp)\n\t"
174 "movl %%ebx,4(%%rax)\n\t"
175 "movl %%ecx,8(%%rax)\n\t"
176 "movl %%edx,12(%%rax)\n\t"
177 "movl %%esi,16(%%rax)\n\t"
178 "movl %%edi,20(%%rax)\n\t"
179 "popq %%rdx\n\t"
180 "movl %%edx,0(%%rax)\n\t"
Luca Tettamantibc1f4192011-05-25 20:43:31 +0200181 "pushfq\n\t"
182 "popq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800183 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800184 : "=a"(rc)
Bradley Smithfe04f222008-02-07 00:16:27 -0800185 : "a"(regs)
186 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
187#else
Jim Bos22d32432010-11-15 21:22:37 +0100188 asm volatile("pushl %%eax\n\t"
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700189 "movl 0(%%eax),%%edx\n\t"
190 "push %%edx\n\t"
191 "movl 4(%%eax),%%ebx\n\t"
192 "movl 8(%%eax),%%ecx\n\t"
193 "movl 12(%%eax),%%edx\n\t"
194 "movl 16(%%eax),%%esi\n\t"
195 "movl 20(%%eax),%%edi\n\t"
196 "popl %%eax\n\t"
197 "out %%al,$0xb2\n\t"
198 "out %%al,$0x84\n\t"
199 "xchgl %%eax,(%%esp)\n\t"
200 "movl %%ebx,4(%%eax)\n\t"
201 "movl %%ecx,8(%%eax)\n\t"
202 "movl %%edx,12(%%eax)\n\t"
203 "movl %%esi,16(%%eax)\n\t"
204 "movl %%edi,20(%%eax)\n\t"
205 "popl %%edx\n\t"
206 "movl %%edx,0(%%eax)\n\t"
207 "lahf\n\t"
208 "shrl $8,%%eax\n\t"
Jim Bos6b4e81d2010-11-13 12:13:53 +0100209 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800210 : "=a"(rc)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700211 : "a"(regs)
212 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
Bradley Smithfe04f222008-02-07 00:16:27 -0800213#endif
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700214 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800215 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800217out:
218 set_cpus_allowed_ptr(current, old_mask);
219 free_cpumask_var(old_mask);
220 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * Read the Fn key status.
225 */
226static int i8k_get_fn_status(void)
227{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700228 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700229 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Guenter Roeck12186f52013-12-14 09:30:09 -0800231 rc = i8k_smm(&regs);
232 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700233 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700235 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
236 case I8K_FN_UP:
237 return I8K_VOL_UP;
238 case I8K_FN_DOWN:
239 return I8K_VOL_DOWN;
240 case I8K_FN_MUTE:
241 return I8K_VOL_MUTE;
242 default:
243 return 0;
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247/*
248 * Read the power status.
249 */
250static int i8k_get_power_status(void)
251{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700252 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700253 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Guenter Roeck12186f52013-12-14 09:30:09 -0800255 rc = i8k_smm(&regs);
256 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700257 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700259 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/*
263 * Read the fan status.
264 */
265static int i8k_get_fan_status(int fan)
266{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700267 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700269 regs.ebx = fan & 0xff;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700270 return i8k_smm(&regs) ? : regs.eax & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/*
274 * Read the fan speed in RPM.
275 */
276static int i8k_get_fan_speed(int fan)
277{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700278 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700280 regs.ebx = fan & 0xff;
Guenter Roeck26d09382013-12-14 09:30:19 -0800281 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284/*
Pali Rohárf989e552015-01-12 14:32:05 +0100285 * Read the fan type.
286 */
287static int i8k_get_fan_type(int fan)
288{
289 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, };
290
291 regs.ebx = fan & 0xff;
292 return i8k_smm(&regs) ? : regs.eax & 0xff;
293}
294
295/*
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100296 * Read the fan nominal rpm for specific fan speed.
297 */
298static int i8k_get_fan_nominal_speed(int fan, int speed)
299{
300 struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
301
302 regs.ebx = (fan & 0xff) | (speed << 8);
303 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
304}
305
306/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * Set the fan speed (off, low, high). Returns the new fan status.
308 */
309static int i8k_set_fan(int fan, int speed)
310{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700311 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Guenter Roeck81474fc2014-06-21 08:08:10 -0700313 speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700314 regs.ebx = (fan & 0xff) | (speed << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700316 return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Pali Rohár5114b472015-01-12 14:31:57 +0100319static int i8k_get_temp_type(int sensor)
320{
321 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
322
323 regs.ebx = sensor & 0xff;
324 return i8k_smm(&regs) ? : regs.eax & 0xff;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*
328 * Read the cpu temperature.
329 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100330static int _i8k_get_temp(int sensor)
331{
332 struct smm_regs regs = {
333 .eax = I8K_SMM_GET_TEMP,
334 .ebx = sensor & 0xff,
335 };
336
337 return i8k_smm(&regs) ? : regs.eax & 0xff;
338}
339
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700340static int i8k_get_temp(int sensor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Guenter Roeck564132d2015-01-12 14:32:00 +0100342 int temp = _i8k_get_temp(sensor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700344 /*
345 * Sometimes the temperature sensor returns 0x99, which is out of range.
Guenter Roeck564132d2015-01-12 14:32:00 +0100346 * In this case we retry (once) before returning an error.
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700347 # 1003655137 00000058 00005a4b
348 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
349 # 1003655139 00000054 00005c52
350 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100351 if (temp == 0x99) {
352 msleep(100);
353 temp = _i8k_get_temp(sensor);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700354 }
Guenter Roeck564132d2015-01-12 14:32:00 +0100355 /*
356 * Return -ENODATA for all invalid temperatures.
357 *
358 * Known instances are the 0x99 value as seen above as well as
359 * 0xc1 (193), which may be returned when trying to read the GPU
360 * temperature if the system supports a GPU and it is currently
361 * turned off.
362 */
Pali Rohár723493c2014-11-18 15:56:54 +0100363 if (temp > I8K_MAX_TEMP)
Pali Rohár83d514d2015-01-12 14:31:59 +0100364 return -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700366 return temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700369static int i8k_get_dell_signature(int req_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700371 struct smm_regs regs = { .eax = req_fn, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700372 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Guenter Roeck12186f52013-12-14 09:30:09 -0800374 rc = i8k_smm(&regs);
375 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700376 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700378 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200381static int
382i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700384 int val = 0;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700385 int speed;
386 unsigned char buff[16];
387 int __user *argp = (int __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700389 if (!argp)
390 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700392 switch (cmd) {
393 case I8K_BIOS_VERSION:
Guenter Roecke551b152013-12-14 09:30:20 -0800394 val = (bios_version[0] << 16) |
395 (bios_version[1] << 8) | bios_version[2];
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700398 case I8K_MACHINE_ID:
399 memset(buff, 0, 16);
Guenter Roeck12186f52013-12-14 09:30:09 -0800400 strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
401 sizeof(buff));
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700404 case I8K_FN_STATUS:
405 val = i8k_get_fn_status();
406 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700408 case I8K_POWER_STATUS:
409 val = i8k_get_power_status();
410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700412 case I8K_GET_TEMP:
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700413 val = i8k_get_temp(0);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700414 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700416 case I8K_GET_SPEED:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700417 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700418 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700419
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700420 val = i8k_get_fan_speed(val);
421 break;
422
423 case I8K_GET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700424 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700425 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700426
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700427 val = i8k_get_fan_status(val);
428 break;
429
430 case I8K_SET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700431 if (restricted && !capable(CAP_SYS_ADMIN))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700432 return -EPERM;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700433
434 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700435 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700436
437 if (copy_from_user(&speed, argp + 1, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700438 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700439
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700440 val = i8k_set_fan(val, speed);
441 break;
442
443 default:
444 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700447 if (val < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700448 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700450 switch (cmd) {
451 case I8K_BIOS_VERSION:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700452 if (copy_to_user(argp, &val, 4))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700453 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700454
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700455 break;
456 case I8K_MACHINE_ID:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700457 if (copy_to_user(argp, buff, 16))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700458 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700459
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700460 break;
461 default:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700462 if (copy_to_user(argp, &val, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700463 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700464
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700465 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200471static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
472{
473 long ret;
474
Arnd Bergmann613655f2010-06-02 14:28:52 +0200475 mutex_lock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200476 ret = i8k_ioctl_unlocked(fp, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200477 mutex_unlock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200478
479 return ret;
480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/*
483 * Print the information for /proc/i8k.
484 */
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700485static int i8k_proc_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700487 int fn_key, cpu_temp, ac_power;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700488 int left_fan, right_fan, left_speed, right_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200490 cpu_temp = i8k_get_temp(0); /* 11100 µs */
491 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
492 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
493 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
494 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
495 fn_key = i8k_get_fn_status(); /* 750 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700496 if (power_status)
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200497 ac_power = i8k_get_power_status(); /* 14700 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700498 else
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700499 ac_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700501 /*
502 * Info:
503 *
504 * 1) Format version (this will change if format changes)
505 * 2) BIOS version
506 * 3) BIOS machine ID
507 * 4) Cpu temperature
508 * 5) Left fan status
509 * 6) Right fan status
510 * 7) Left fan speed
511 * 8) Right fan speed
512 * 9) AC power
513 * 10) Fn Key status
514 */
Joe Perches3a267d32015-02-21 18:53:47 -0800515 seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
516 I8K_PROC_FMT,
517 bios_version,
518 i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
519 cpu_temp,
520 left_fan, right_fan, left_speed, right_speed,
521 ac_power, fn_key);
522
523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700526static int i8k_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700528 return single_open(file, i8k_proc_show, NULL);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700529}
530
Jean Delvare949a9d72011-05-25 20:43:33 +0200531
532/*
533 * Hwmon interface
534 */
535
Pali Rohár5114b472015-01-12 14:31:57 +0100536static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
537 struct device_attribute *devattr,
538 char *buf)
539{
540 static const char * const labels[] = {
541 "CPU",
542 "GPU",
543 "SODIMM",
544 "Other",
545 "Ambient",
546 "Other",
547 };
548 int index = to_sensor_dev_attr(devattr)->index;
549 int type;
550
551 type = i8k_get_temp_type(index);
552 if (type < 0)
553 return type;
554 if (type >= ARRAY_SIZE(labels))
555 type = ARRAY_SIZE(labels) - 1;
556 return sprintf(buf, "%s\n", labels[type]);
557}
558
Jean Delvare949a9d72011-05-25 20:43:33 +0200559static ssize_t i8k_hwmon_show_temp(struct device *dev,
560 struct device_attribute *devattr,
561 char *buf)
562{
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800563 int index = to_sensor_dev_attr(devattr)->index;
564 int temp;
Jean Delvare949a9d72011-05-25 20:43:33 +0200565
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800566 temp = i8k_get_temp(index);
567 if (temp < 0)
568 return temp;
569 return sprintf(buf, "%d\n", temp * 1000);
Jean Delvare949a9d72011-05-25 20:43:33 +0200570}
571
Pali Rohárf989e552015-01-12 14:32:05 +0100572static ssize_t i8k_hwmon_show_fan_label(struct device *dev,
573 struct device_attribute *devattr,
574 char *buf)
575{
576 static const char * const labels[] = {
577 "Processor Fan",
578 "Motherboard Fan",
579 "Video Fan",
580 "Power Supply Fan",
581 "Chipset Fan",
582 "Other Fan",
583 };
584 int index = to_sensor_dev_attr(devattr)->index;
585 bool dock = false;
586 int type;
587
588 type = i8k_get_fan_type(index);
589 if (type < 0)
590 return type;
591
592 if (type & 0x10) {
593 dock = true;
594 type &= 0x0F;
595 }
596
597 if (type >= ARRAY_SIZE(labels))
598 type = (ARRAY_SIZE(labels) - 1);
599
600 return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
601}
602
Jean Delvare949a9d72011-05-25 20:43:33 +0200603static ssize_t i8k_hwmon_show_fan(struct device *dev,
604 struct device_attribute *devattr,
605 char *buf)
606{
607 int index = to_sensor_dev_attr(devattr)->index;
608 int fan_speed;
609
610 fan_speed = i8k_get_fan_speed(index);
611 if (fan_speed < 0)
612 return fan_speed;
613 return sprintf(buf, "%d\n", fan_speed);
614}
615
Guenter Roecke7f5f272013-12-14 09:30:21 -0800616static ssize_t i8k_hwmon_show_pwm(struct device *dev,
617 struct device_attribute *devattr,
618 char *buf)
619{
620 int index = to_sensor_dev_attr(devattr)->index;
621 int status;
622
623 status = i8k_get_fan_status(index);
624 if (status < 0)
625 return -EIO;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700626 return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
Guenter Roecke7f5f272013-12-14 09:30:21 -0800627}
628
629static ssize_t i8k_hwmon_set_pwm(struct device *dev,
630 struct device_attribute *attr,
631 const char *buf, size_t count)
632{
633 int index = to_sensor_dev_attr(attr)->index;
634 unsigned long val;
635 int err;
636
637 err = kstrtoul(buf, 10, &val);
638 if (err)
639 return err;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700640 val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800641
642 mutex_lock(&i8k_mutex);
643 err = i8k_set_fan(index, val);
644 mutex_unlock(&i8k_mutex);
645
646 return err < 0 ? -EIO : count;
647}
648
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800649static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
Pali Rohár5114b472015-01-12 14:31:57 +0100650static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
651 0);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800652static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
Pali Rohár5114b472015-01-12 14:31:57 +0100653static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
654 1);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800655static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
Pali Rohár5114b472015-01-12 14:31:57 +0100656static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
657 2);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800658static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
Pali Rohár5114b472015-01-12 14:31:57 +0100659static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
660 3);
Pali Rohárf989e552015-01-12 14:32:05 +0100661static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, 0);
662static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
663 0);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800664static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100665 i8k_hwmon_set_pwm, 0);
Jean Delvare949a9d72011-05-25 20:43:33 +0200666static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
Pali Rohárf989e552015-01-12 14:32:05 +0100667 1);
668static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
669 1);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800670static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100671 i8k_hwmon_set_pwm, 1);
Jean Delvare949a9d72011-05-25 20:43:33 +0200672
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800673static struct attribute *i8k_attrs[] = {
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800674 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
Pali Rohár5114b472015-01-12 14:31:57 +0100675 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
676 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
677 &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */
678 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */
679 &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */
680 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */
681 &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */
682 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 8 */
Pali Rohárf989e552015-01-12 14:32:05 +0100683 &sensor_dev_attr_fan1_label.dev_attr.attr, /* 9 */
684 &sensor_dev_attr_pwm1.dev_attr.attr, /* 10 */
685 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 11 */
686 &sensor_dev_attr_fan2_label.dev_attr.attr, /* 12 */
687 &sensor_dev_attr_pwm2.dev_attr.attr, /* 13 */
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800688 NULL
689};
690
691static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
692 int index)
Jean Delvare949a9d72011-05-25 20:43:33 +0200693{
Pali Rohár5114b472015-01-12 14:31:57 +0100694 if (index >= 0 && index <= 1 &&
695 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800696 return 0;
Pali Rohár5114b472015-01-12 14:31:57 +0100697 if (index >= 2 && index <= 3 &&
698 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800699 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700700 if (index >= 4 && index <= 5 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100701 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800702 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700703 if (index >= 6 && index <= 7 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100704 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
705 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100706 if (index >= 8 && index <= 10 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100707 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
708 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100709 if (index >= 11 && index <= 13 &&
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800710 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
711 return 0;
712
713 return attr->mode;
Jean Delvare949a9d72011-05-25 20:43:33 +0200714}
715
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800716static const struct attribute_group i8k_group = {
717 .attrs = i8k_attrs,
718 .is_visible = i8k_is_visible,
719};
720__ATTRIBUTE_GROUPS(i8k);
721
Jean Delvare949a9d72011-05-25 20:43:33 +0200722static int __init i8k_init_hwmon(void)
723{
724 int err;
725
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800726 i8k_hwmon_flags = 0;
727
Pali Rohár672af222015-01-12 14:31:58 +0100728 /* CPU temperature attributes, if temperature type is OK */
729 err = i8k_get_temp_type(0);
730 if (err >= 0)
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800731 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800732 /* check for additional temperature sensors */
Pali Rohár672af222015-01-12 14:31:58 +0100733 err = i8k_get_temp_type(1);
734 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800735 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
Pali Rohár672af222015-01-12 14:31:58 +0100736 err = i8k_get_temp_type(2);
737 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800738 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
Pali Rohár672af222015-01-12 14:31:58 +0100739 err = i8k_get_temp_type(3);
740 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800741 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800742
Pali Rohárf989e552015-01-12 14:32:05 +0100743 /* First fan attributes, if fan type is OK */
744 err = i8k_get_fan_type(0);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800745 if (err >= 0)
746 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
747
Pali Rohárf989e552015-01-12 14:32:05 +0100748 /* Second fan attributes, if fan type is OK */
749 err = i8k_get_fan_type(1);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800750 if (err >= 0)
751 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
752
753 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "i8k", NULL,
754 i8k_groups);
Jean Delvare949a9d72011-05-25 20:43:33 +0200755 if (IS_ERR(i8k_hwmon_dev)) {
756 err = PTR_ERR(i8k_hwmon_dev);
757 i8k_hwmon_dev = NULL;
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800758 pr_err("hwmon registration failed (%d)\n", err);
Jean Delvare949a9d72011-05-25 20:43:33 +0200759 return err;
760 }
Jean Delvare949a9d72011-05-25 20:43:33 +0200761 return 0;
Jean Delvare949a9d72011-05-25 20:43:33 +0200762}
763
Guenter Roeck81474fc2014-06-21 08:08:10 -0700764struct i8k_config_data {
Pali Rohár7f69fb02015-01-12 14:32:02 +0100765 uint fan_mult;
766 uint fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700767};
768
769enum i8k_configs {
Guenter Roeck7b883442014-06-21 08:08:12 -0700770 DELL_LATITUDE_D520,
771 DELL_PRECISION_490,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700772 DELL_STUDIO,
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100773 DELL_XPS,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700774};
775
776static const struct i8k_config_data i8k_config_data[] = {
Guenter Roeck7b883442014-06-21 08:08:12 -0700777 [DELL_LATITUDE_D520] = {
778 .fan_mult = 1,
779 .fan_max = I8K_FAN_TURBO,
780 },
781 [DELL_PRECISION_490] = {
782 .fan_mult = 1,
783 .fan_max = I8K_FAN_TURBO,
784 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700785 [DELL_STUDIO] = {
786 .fan_mult = 1,
787 .fan_max = I8K_FAN_HIGH,
788 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100789 [DELL_XPS] = {
Guenter Roeck81474fc2014-06-21 08:08:10 -0700790 .fan_mult = 1,
791 .fan_max = I8K_FAN_HIGH,
792 },
793};
794
Guenter Roeck12186f52013-12-14 09:30:09 -0800795static struct dmi_system_id i8k_dmi_table[] __initdata = {
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700796 {
797 .ident = "Dell Inspiron",
798 .matches = {
799 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
800 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
801 },
802 },
803 {
804 .ident = "Dell Latitude",
805 .matches = {
806 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
807 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
808 },
809 },
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700810 {
811 .ident = "Dell Inspiron 2",
812 .matches = {
813 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
814 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
815 },
816 },
817 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700818 .ident = "Dell Latitude D520",
819 .matches = {
820 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
821 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
822 },
823 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
824 },
825 {
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700826 .ident = "Dell Latitude 2",
827 .matches = {
828 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
829 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
830 },
831 },
Nick Warnea9000d02008-02-06 01:37:47 -0800832 { /* UK Inspiron 6400 */
833 .ident = "Dell Inspiron 3",
834 .matches = {
835 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
836 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
837 },
838 },
Frank Sorenson48103c52008-02-07 00:16:31 -0800839 {
840 .ident = "Dell Inspiron 3",
841 .matches = {
842 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
843 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
844 },
845 },
Andy Spencer7ab21a82009-01-02 16:19:13 +0000846 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700847 .ident = "Dell Precision 490",
848 .matches = {
849 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
850 DMI_MATCH(DMI_PRODUCT_NAME,
851 "Precision WorkStation 490"),
852 },
853 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
854 },
855 {
Andy Spencer7ab21a82009-01-02 16:19:13 +0000856 .ident = "Dell Precision",
857 .matches = {
858 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
859 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
860 },
861 },
Federico Heinzbef2a502009-01-02 16:19:23 +0000862 {
863 .ident = "Dell Vostro",
864 .matches = {
865 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
866 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
867 },
868 },
Alan Cox9aa5b012013-12-03 13:56:56 -0800869 {
870 .ident = "Dell XPS421",
871 .matches = {
872 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
873 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
874 },
875 },
Guenter Roeckff457bd2013-12-14 09:30:17 -0800876 {
877 .ident = "Dell Studio",
878 .matches = {
879 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
880 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
881 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700882 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
Guenter Roeckff457bd2013-12-14 09:30:17 -0800883 },
Guenter Roeck919a0302013-12-14 09:30:18 -0800884 {
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100885 .ident = "Dell XPS 13",
886 .matches = {
887 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
888 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
889 },
890 .driver_data = (void *)&i8k_config_data[DELL_XPS],
891 },
892 {
Guenter Roeck919a0302013-12-14 09:30:18 -0800893 .ident = "Dell XPS M140",
894 .matches = {
895 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
896 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
897 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100898 .driver_data = (void *)&i8k_config_data[DELL_XPS],
Guenter Roeck919a0302013-12-14 09:30:18 -0800899 },
Guenter Roeck12186f52013-12-14 09:30:09 -0800900 { }
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700901};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Pali Rohár148b1fd2014-10-18 00:17:49 +0200903MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/*
906 * Probe for the presence of a supported laptop.
907 */
908static int __init i8k_probe(void)
909{
Guenter Roeck26d09382013-12-14 09:30:19 -0800910 const struct dmi_system_id *id;
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100911 int fan, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700914 * Get DMI information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700916 if (!dmi_check_system(i8k_dmi_table)) {
917 if (!ignore_dmi && !force)
918 return -ENODEV;
919
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800920 pr_info("not running on a supported Dell system.\n");
921 pr_info("vendor=%s, model=%s, version=%s\n",
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700922 i8k_get_dmi_data(DMI_SYS_VENDOR),
923 i8k_get_dmi_data(DMI_PRODUCT_NAME),
924 i8k_get_dmi_data(DMI_BIOS_VERSION));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700926
Guenter Roeck12186f52013-12-14 09:30:09 -0800927 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
928 sizeof(bios_version));
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700931 * Get SMM Dell signature
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700933 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
934 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800935 pr_err("unable to get SMM Dell signature\n");
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700936 if (!force)
937 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100940 /*
941 * Set fan multiplier and maximal fan speed from dmi config
942 * Values specified in module parameters override values from dmi
943 */
Guenter Roeck26d09382013-12-14 09:30:19 -0800944 id = dmi_first_match(i8k_dmi_table);
Guenter Roeck81474fc2014-06-21 08:08:10 -0700945 if (id && id->driver_data) {
946 const struct i8k_config_data *conf = id->driver_data;
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100947 if (!fan_mult && conf->fan_mult)
948 fan_mult = conf->fan_mult;
949 if (!fan_max && conf->fan_max)
950 fan_max = conf->fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700951 }
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100952
953 i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
Guenter Roeck81474fc2014-06-21 08:08:10 -0700954 i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
Guenter Roeck26d09382013-12-14 09:30:19 -0800955
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100956 if (!fan_mult) {
957 /*
958 * Autodetect fan multiplier based on nominal rpm
959 * If fan reports rpm value too high then set multiplier to 1
960 */
961 for (fan = 0; fan < 2; ++fan) {
962 ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
963 if (ret < 0)
964 continue;
965 if (ret > I8K_FAN_MAX_RPM)
966 i8k_fan_mult = 1;
967 break;
968 }
969 } else {
970 /* Fan multiplier was specified in module param or in dmi */
971 i8k_fan_mult = fan_mult;
972 }
973
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700977static int __init i8k_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700979 struct proc_dir_entry *proc_i8k;
Jean Delvare949a9d72011-05-25 20:43:33 +0200980 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700982 /* Are we running on an supported laptop? */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700983 if (i8k_probe())
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700984 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700986 /* Register the proc entry */
Denis V. Lunev1b502212008-04-29 01:02:34 -0700987 proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops);
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700988 if (!proc_i8k)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700989 return -ENOENT;
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700990
Jean Delvare949a9d72011-05-25 20:43:33 +0200991 err = i8k_init_hwmon();
992 if (err)
993 goto exit_remove_proc;
994
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700995 return 0;
Jean Delvare949a9d72011-05-25 20:43:33 +0200996
997 exit_remove_proc:
998 remove_proc_entry("i8k", NULL);
999 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001002static void __exit i8k_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Guenter Roeck82ba1d32013-12-14 09:30:10 -08001004 hwmon_device_unregister(i8k_hwmon_dev);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001005 remove_proc_entry("i8k", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001008module_init(i8k_init);
1009module_exit(i8k_exit);