blob: 24cc4ed9a78014509371815cbe0dfda3ef5ca679 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * i8k.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ár8f21d8e2015-01-12 14:32:03 +01009 * Copyright (C) 2014 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)");
83MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
84MODULE_LICENSE("GPL");
85
Rusty Russell90ab5ee2012-01-13 09:32:20 +103086static bool force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087module_param(force, bool, 0);
88MODULE_PARM_DESC(force, "Force loading without checking for supported models");
89
Rusty Russell90ab5ee2012-01-13 09:32:20 +103090static bool ignore_dmi;
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070091module_param(ignore_dmi, bool, 0);
92MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
93
Rusty Russell90ab5ee2012-01-13 09:32:20 +103094static bool restricted;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095module_param(restricted, bool, 0);
96MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
97
Rusty Russell90ab5ee2012-01-13 09:32:20 +103098static bool power_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099module_param(power_status, bool, 0600);
100MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
101
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100102static uint fan_mult;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100103module_param(fan_mult, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100104MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
Jochen Eisinger4ed99a22008-05-01 04:34:58 -0700105
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100106static uint fan_max;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100107module_param(fan_max, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100108MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
Guenter Roeck81474fc2014-06-21 08:08:10 -0700109
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700110static int i8k_open_fs(struct inode *inode, struct file *file);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200111static long i8k_ioctl(struct file *, unsigned int, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Arjan van de Ven62322d22006-07-03 00:24:21 -0700113static const struct file_operations i8k_fops = {
Denis V. Lunev1b502212008-04-29 01:02:34 -0700114 .owner = THIS_MODULE,
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700115 .open = i8k_open_fs,
116 .read = seq_read,
117 .llseek = seq_lseek,
118 .release = single_release,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200119 .unlocked_ioctl = i8k_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700122struct smm_regs {
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700123 unsigned int eax;
Guenter Roeck12186f52013-12-14 09:30:09 -0800124 unsigned int ebx __packed;
125 unsigned int ecx __packed;
126 unsigned int edx __packed;
127 unsigned int esi __packed;
128 unsigned int edi __packed;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700129};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Jeff Garzik18552562007-10-03 15:15:40 -0400131static inline const char *i8k_get_dmi_data(int field)
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700132{
Jeff Garzik18552562007-10-03 15:15:40 -0400133 const char *dmi_data = dmi_get_system_info(field);
Dmitry Torokhov4f005552005-11-12 00:55:15 -0500134
135 return dmi_data && *dmi_data ? dmi_data : "?";
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/*
139 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
140 */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700141static int i8k_smm(struct smm_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700143 int rc;
144 int eax = regs->eax;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800145 cpumask_var_t old_mask;
146
147 /* SMM requires CPU 0 */
148 if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
149 return -ENOMEM;
150 cpumask_copy(old_mask, &current->cpus_allowed);
Guenter Roeck6d827fb2014-06-21 08:08:08 -0700151 rc = set_cpus_allowed_ptr(current, cpumask_of(0));
152 if (rc)
153 goto out;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800154 if (smp_processor_id() != 0) {
155 rc = -EBUSY;
156 goto out;
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Bradley Smithfe04f222008-02-07 00:16:27 -0800159#if defined(CONFIG_X86_64)
Jim Bos22d32432010-11-15 21:22:37 +0100160 asm volatile("pushq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800161 "movl 0(%%rax),%%edx\n\t"
162 "pushq %%rdx\n\t"
163 "movl 4(%%rax),%%ebx\n\t"
164 "movl 8(%%rax),%%ecx\n\t"
165 "movl 12(%%rax),%%edx\n\t"
166 "movl 16(%%rax),%%esi\n\t"
167 "movl 20(%%rax),%%edi\n\t"
168 "popq %%rax\n\t"
169 "out %%al,$0xb2\n\t"
170 "out %%al,$0x84\n\t"
171 "xchgq %%rax,(%%rsp)\n\t"
172 "movl %%ebx,4(%%rax)\n\t"
173 "movl %%ecx,8(%%rax)\n\t"
174 "movl %%edx,12(%%rax)\n\t"
175 "movl %%esi,16(%%rax)\n\t"
176 "movl %%edi,20(%%rax)\n\t"
177 "popq %%rdx\n\t"
178 "movl %%edx,0(%%rax)\n\t"
Luca Tettamantibc1f4192011-05-25 20:43:31 +0200179 "pushfq\n\t"
180 "popq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800181 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800182 : "=a"(rc)
Bradley Smithfe04f222008-02-07 00:16:27 -0800183 : "a"(regs)
184 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
185#else
Jim Bos22d32432010-11-15 21:22:37 +0100186 asm volatile("pushl %%eax\n\t"
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700187 "movl 0(%%eax),%%edx\n\t"
188 "push %%edx\n\t"
189 "movl 4(%%eax),%%ebx\n\t"
190 "movl 8(%%eax),%%ecx\n\t"
191 "movl 12(%%eax),%%edx\n\t"
192 "movl 16(%%eax),%%esi\n\t"
193 "movl 20(%%eax),%%edi\n\t"
194 "popl %%eax\n\t"
195 "out %%al,$0xb2\n\t"
196 "out %%al,$0x84\n\t"
197 "xchgl %%eax,(%%esp)\n\t"
198 "movl %%ebx,4(%%eax)\n\t"
199 "movl %%ecx,8(%%eax)\n\t"
200 "movl %%edx,12(%%eax)\n\t"
201 "movl %%esi,16(%%eax)\n\t"
202 "movl %%edi,20(%%eax)\n\t"
203 "popl %%edx\n\t"
204 "movl %%edx,0(%%eax)\n\t"
205 "lahf\n\t"
206 "shrl $8,%%eax\n\t"
Jim Bos6b4e81d2010-11-13 12:13:53 +0100207 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800208 : "=a"(rc)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700209 : "a"(regs)
210 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
Bradley Smithfe04f222008-02-07 00:16:27 -0800211#endif
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700212 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800213 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800215out:
216 set_cpus_allowed_ptr(current, old_mask);
217 free_cpumask_var(old_mask);
218 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * Read the Fn key status.
223 */
224static int i8k_get_fn_status(void)
225{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700226 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700227 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Guenter Roeck12186f52013-12-14 09:30:09 -0800229 rc = i8k_smm(&regs);
230 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700231 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700233 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
234 case I8K_FN_UP:
235 return I8K_VOL_UP;
236 case I8K_FN_DOWN:
237 return I8K_VOL_DOWN;
238 case I8K_FN_MUTE:
239 return I8K_VOL_MUTE;
240 default:
241 return 0;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245/*
246 * Read the power status.
247 */
248static int i8k_get_power_status(void)
249{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700250 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700251 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Guenter Roeck12186f52013-12-14 09:30:09 -0800253 rc = i8k_smm(&regs);
254 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700255 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700257 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/*
261 * Read the fan status.
262 */
263static int i8k_get_fan_status(int fan)
264{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700265 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700267 regs.ebx = fan & 0xff;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700268 return i8k_smm(&regs) ? : regs.eax & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/*
272 * Read the fan speed in RPM.
273 */
274static int i8k_get_fan_speed(int fan)
275{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700276 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700278 regs.ebx = fan & 0xff;
Guenter Roeck26d09382013-12-14 09:30:19 -0800279 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/*
Pali Rohárf989e552015-01-12 14:32:05 +0100283 * Read the fan type.
284 */
285static int i8k_get_fan_type(int fan)
286{
287 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, };
288
289 regs.ebx = fan & 0xff;
290 return i8k_smm(&regs) ? : regs.eax & 0xff;
291}
292
293/*
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100294 * Read the fan nominal rpm for specific fan speed.
295 */
296static int i8k_get_fan_nominal_speed(int fan, int speed)
297{
298 struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
299
300 regs.ebx = (fan & 0xff) | (speed << 8);
301 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
302}
303
304/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * Set the fan speed (off, low, high). Returns the new fan status.
306 */
307static int i8k_set_fan(int fan, int speed)
308{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700309 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Guenter Roeck81474fc2014-06-21 08:08:10 -0700311 speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700312 regs.ebx = (fan & 0xff) | (speed << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700314 return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Pali Rohár5114b472015-01-12 14:31:57 +0100317static int i8k_get_temp_type(int sensor)
318{
319 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
320
321 regs.ebx = sensor & 0xff;
322 return i8k_smm(&regs) ? : regs.eax & 0xff;
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/*
326 * Read the cpu temperature.
327 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100328static int _i8k_get_temp(int sensor)
329{
330 struct smm_regs regs = {
331 .eax = I8K_SMM_GET_TEMP,
332 .ebx = sensor & 0xff,
333 };
334
335 return i8k_smm(&regs) ? : regs.eax & 0xff;
336}
337
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700338static int i8k_get_temp(int sensor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Guenter Roeck564132d2015-01-12 14:32:00 +0100340 int temp = _i8k_get_temp(sensor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700342 /*
343 * Sometimes the temperature sensor returns 0x99, which is out of range.
Guenter Roeck564132d2015-01-12 14:32:00 +0100344 * In this case we retry (once) before returning an error.
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700345 # 1003655137 00000058 00005a4b
346 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
347 # 1003655139 00000054 00005c52
348 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100349 if (temp == 0x99) {
350 msleep(100);
351 temp = _i8k_get_temp(sensor);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700352 }
Guenter Roeck564132d2015-01-12 14:32:00 +0100353 /*
354 * Return -ENODATA for all invalid temperatures.
355 *
356 * Known instances are the 0x99 value as seen above as well as
357 * 0xc1 (193), which may be returned when trying to read the GPU
358 * temperature if the system supports a GPU and it is currently
359 * turned off.
360 */
Pali Rohár723493c2014-11-18 15:56:54 +0100361 if (temp > I8K_MAX_TEMP)
Pali Rohár83d514d2015-01-12 14:31:59 +0100362 return -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700364 return temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700367static int i8k_get_dell_signature(int req_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700369 struct smm_regs regs = { .eax = req_fn, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700370 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Guenter Roeck12186f52013-12-14 09:30:09 -0800372 rc = i8k_smm(&regs);
373 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700374 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700376 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200379static int
380i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700382 int val = 0;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700383 int speed;
384 unsigned char buff[16];
385 int __user *argp = (int __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700387 if (!argp)
388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700390 switch (cmd) {
391 case I8K_BIOS_VERSION:
Guenter Roecke551b152013-12-14 09:30:20 -0800392 val = (bios_version[0] << 16) |
393 (bios_version[1] << 8) | bios_version[2];
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700396 case I8K_MACHINE_ID:
397 memset(buff, 0, 16);
Guenter Roeck12186f52013-12-14 09:30:09 -0800398 strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
399 sizeof(buff));
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700400 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700402 case I8K_FN_STATUS:
403 val = i8k_get_fn_status();
404 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700406 case I8K_POWER_STATUS:
407 val = i8k_get_power_status();
408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700410 case I8K_GET_TEMP:
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700411 val = i8k_get_temp(0);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700412 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700414 case I8K_GET_SPEED:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700415 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700416 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700417
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700418 val = i8k_get_fan_speed(val);
419 break;
420
421 case I8K_GET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700422 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700423 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700424
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700425 val = i8k_get_fan_status(val);
426 break;
427
428 case I8K_SET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700429 if (restricted && !capable(CAP_SYS_ADMIN))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700430 return -EPERM;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700431
432 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700433 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700434
435 if (copy_from_user(&speed, argp + 1, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700436 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700437
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700438 val = i8k_set_fan(val, speed);
439 break;
440
441 default:
442 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700445 if (val < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700446 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700448 switch (cmd) {
449 case I8K_BIOS_VERSION:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700450 if (copy_to_user(argp, &val, 4))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700451 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700452
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700453 break;
454 case I8K_MACHINE_ID:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700455 if (copy_to_user(argp, buff, 16))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700456 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700457
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700458 break;
459 default:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700460 if (copy_to_user(argp, &val, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700461 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700462
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700463 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200469static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
470{
471 long ret;
472
Arnd Bergmann613655f2010-06-02 14:28:52 +0200473 mutex_lock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200474 ret = i8k_ioctl_unlocked(fp, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200475 mutex_unlock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200476
477 return ret;
478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
481 * Print the information for /proc/i8k.
482 */
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700483static int i8k_proc_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700485 int fn_key, cpu_temp, ac_power;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700486 int left_fan, right_fan, left_speed, right_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200488 cpu_temp = i8k_get_temp(0); /* 11100 µs */
489 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
490 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
491 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
492 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
493 fn_key = i8k_get_fn_status(); /* 750 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700494 if (power_status)
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200495 ac_power = i8k_get_power_status(); /* 14700 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700496 else
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700497 ac_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700499 /*
500 * Info:
501 *
502 * 1) Format version (this will change if format changes)
503 * 2) BIOS version
504 * 3) BIOS machine ID
505 * 4) Cpu temperature
506 * 5) Left fan status
507 * 6) Right fan status
508 * 7) Left fan speed
509 * 8) Right fan speed
510 * 9) AC power
511 * 10) Fn Key status
512 */
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700513 return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
514 I8K_PROC_FMT,
515 bios_version,
Dmitry Torokhov4f005552005-11-12 00:55:15 -0500516 i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700517 cpu_temp,
518 left_fan, right_fan, left_speed, right_speed,
519 ac_power, fn_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700522static int i8k_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700524 return single_open(file, i8k_proc_show, NULL);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700525}
526
Jean Delvare949a9d72011-05-25 20:43:33 +0200527
528/*
529 * Hwmon interface
530 */
531
Pali Rohár5114b472015-01-12 14:31:57 +0100532static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
533 struct device_attribute *devattr,
534 char *buf)
535{
536 static const char * const labels[] = {
537 "CPU",
538 "GPU",
539 "SODIMM",
540 "Other",
541 "Ambient",
542 "Other",
543 };
544 int index = to_sensor_dev_attr(devattr)->index;
545 int type;
546
547 type = i8k_get_temp_type(index);
548 if (type < 0)
549 return type;
550 if (type >= ARRAY_SIZE(labels))
551 type = ARRAY_SIZE(labels) - 1;
552 return sprintf(buf, "%s\n", labels[type]);
553}
554
Jean Delvare949a9d72011-05-25 20:43:33 +0200555static ssize_t i8k_hwmon_show_temp(struct device *dev,
556 struct device_attribute *devattr,
557 char *buf)
558{
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800559 int index = to_sensor_dev_attr(devattr)->index;
560 int temp;
Jean Delvare949a9d72011-05-25 20:43:33 +0200561
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800562 temp = i8k_get_temp(index);
563 if (temp < 0)
564 return temp;
565 return sprintf(buf, "%d\n", temp * 1000);
Jean Delvare949a9d72011-05-25 20:43:33 +0200566}
567
Pali Rohárf989e552015-01-12 14:32:05 +0100568static ssize_t i8k_hwmon_show_fan_label(struct device *dev,
569 struct device_attribute *devattr,
570 char *buf)
571{
572 static const char * const labels[] = {
573 "Processor Fan",
574 "Motherboard Fan",
575 "Video Fan",
576 "Power Supply Fan",
577 "Chipset Fan",
578 "Other Fan",
579 };
580 int index = to_sensor_dev_attr(devattr)->index;
581 bool dock = false;
582 int type;
583
584 type = i8k_get_fan_type(index);
585 if (type < 0)
586 return type;
587
588 if (type & 0x10) {
589 dock = true;
590 type &= 0x0F;
591 }
592
593 if (type >= ARRAY_SIZE(labels))
594 type = (ARRAY_SIZE(labels) - 1);
595
596 return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
597}
598
Jean Delvare949a9d72011-05-25 20:43:33 +0200599static ssize_t i8k_hwmon_show_fan(struct device *dev,
600 struct device_attribute *devattr,
601 char *buf)
602{
603 int index = to_sensor_dev_attr(devattr)->index;
604 int fan_speed;
605
606 fan_speed = i8k_get_fan_speed(index);
607 if (fan_speed < 0)
608 return fan_speed;
609 return sprintf(buf, "%d\n", fan_speed);
610}
611
Guenter Roecke7f5f272013-12-14 09:30:21 -0800612static ssize_t i8k_hwmon_show_pwm(struct device *dev,
613 struct device_attribute *devattr,
614 char *buf)
615{
616 int index = to_sensor_dev_attr(devattr)->index;
617 int status;
618
619 status = i8k_get_fan_status(index);
620 if (status < 0)
621 return -EIO;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700622 return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
Guenter Roecke7f5f272013-12-14 09:30:21 -0800623}
624
625static ssize_t i8k_hwmon_set_pwm(struct device *dev,
626 struct device_attribute *attr,
627 const char *buf, size_t count)
628{
629 int index = to_sensor_dev_attr(attr)->index;
630 unsigned long val;
631 int err;
632
633 err = kstrtoul(buf, 10, &val);
634 if (err)
635 return err;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700636 val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800637
638 mutex_lock(&i8k_mutex);
639 err = i8k_set_fan(index, val);
640 mutex_unlock(&i8k_mutex);
641
642 return err < 0 ? -EIO : count;
643}
644
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800645static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
Pali Rohár5114b472015-01-12 14:31:57 +0100646static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
647 0);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800648static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
Pali Rohár5114b472015-01-12 14:31:57 +0100649static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
650 1);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800651static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
Pali Rohár5114b472015-01-12 14:31:57 +0100652static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
653 2);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800654static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
Pali Rohár5114b472015-01-12 14:31:57 +0100655static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
656 3);
Pali Rohárf989e552015-01-12 14:32:05 +0100657static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, 0);
658static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
659 0);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800660static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100661 i8k_hwmon_set_pwm, 0);
Jean Delvare949a9d72011-05-25 20:43:33 +0200662static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
Pali Rohárf989e552015-01-12 14:32:05 +0100663 1);
664static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
665 1);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800666static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100667 i8k_hwmon_set_pwm, 1);
Jean Delvare949a9d72011-05-25 20:43:33 +0200668
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800669static struct attribute *i8k_attrs[] = {
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800670 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
Pali Rohár5114b472015-01-12 14:31:57 +0100671 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
672 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
673 &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */
674 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */
675 &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */
676 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */
677 &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */
678 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 8 */
Pali Rohárf989e552015-01-12 14:32:05 +0100679 &sensor_dev_attr_fan1_label.dev_attr.attr, /* 9 */
680 &sensor_dev_attr_pwm1.dev_attr.attr, /* 10 */
681 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 11 */
682 &sensor_dev_attr_fan2_label.dev_attr.attr, /* 12 */
683 &sensor_dev_attr_pwm2.dev_attr.attr, /* 13 */
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800684 NULL
685};
686
687static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
688 int index)
Jean Delvare949a9d72011-05-25 20:43:33 +0200689{
Pali Rohár5114b472015-01-12 14:31:57 +0100690 if (index >= 0 && index <= 1 &&
691 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800692 return 0;
Pali Rohár5114b472015-01-12 14:31:57 +0100693 if (index >= 2 && index <= 3 &&
694 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800695 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700696 if (index >= 4 && index <= 5 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100697 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800698 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700699 if (index >= 6 && index <= 7 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100700 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
701 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100702 if (index >= 8 && index <= 10 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100703 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
704 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100705 if (index >= 11 && index <= 13 &&
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800706 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
707 return 0;
708
709 return attr->mode;
Jean Delvare949a9d72011-05-25 20:43:33 +0200710}
711
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800712static const struct attribute_group i8k_group = {
713 .attrs = i8k_attrs,
714 .is_visible = i8k_is_visible,
715};
716__ATTRIBUTE_GROUPS(i8k);
717
Jean Delvare949a9d72011-05-25 20:43:33 +0200718static int __init i8k_init_hwmon(void)
719{
720 int err;
721
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800722 i8k_hwmon_flags = 0;
723
Pali Rohár672af222015-01-12 14:31:58 +0100724 /* CPU temperature attributes, if temperature type is OK */
725 err = i8k_get_temp_type(0);
726 if (err >= 0)
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800727 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800728 /* check for additional temperature sensors */
Pali Rohár672af222015-01-12 14:31:58 +0100729 err = i8k_get_temp_type(1);
730 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800731 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
Pali Rohár672af222015-01-12 14:31:58 +0100732 err = i8k_get_temp_type(2);
733 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800734 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
Pali Rohár672af222015-01-12 14:31:58 +0100735 err = i8k_get_temp_type(3);
736 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800737 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800738
Pali Rohárf989e552015-01-12 14:32:05 +0100739 /* First fan attributes, if fan type is OK */
740 err = i8k_get_fan_type(0);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800741 if (err >= 0)
742 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
743
Pali Rohárf989e552015-01-12 14:32:05 +0100744 /* Second fan attributes, if fan type is OK */
745 err = i8k_get_fan_type(1);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800746 if (err >= 0)
747 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
748
749 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "i8k", NULL,
750 i8k_groups);
Jean Delvare949a9d72011-05-25 20:43:33 +0200751 if (IS_ERR(i8k_hwmon_dev)) {
752 err = PTR_ERR(i8k_hwmon_dev);
753 i8k_hwmon_dev = NULL;
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800754 pr_err("hwmon registration failed (%d)\n", err);
Jean Delvare949a9d72011-05-25 20:43:33 +0200755 return err;
756 }
Jean Delvare949a9d72011-05-25 20:43:33 +0200757 return 0;
Jean Delvare949a9d72011-05-25 20:43:33 +0200758}
759
Guenter Roeck81474fc2014-06-21 08:08:10 -0700760struct i8k_config_data {
Pali Rohár7f69fb02015-01-12 14:32:02 +0100761 uint fan_mult;
762 uint fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700763};
764
765enum i8k_configs {
Guenter Roeck7b883442014-06-21 08:08:12 -0700766 DELL_LATITUDE_D520,
767 DELL_PRECISION_490,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700768 DELL_STUDIO,
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100769 DELL_XPS,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700770};
771
772static const struct i8k_config_data i8k_config_data[] = {
Guenter Roeck7b883442014-06-21 08:08:12 -0700773 [DELL_LATITUDE_D520] = {
774 .fan_mult = 1,
775 .fan_max = I8K_FAN_TURBO,
776 },
777 [DELL_PRECISION_490] = {
778 .fan_mult = 1,
779 .fan_max = I8K_FAN_TURBO,
780 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700781 [DELL_STUDIO] = {
782 .fan_mult = 1,
783 .fan_max = I8K_FAN_HIGH,
784 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100785 [DELL_XPS] = {
Guenter Roeck81474fc2014-06-21 08:08:10 -0700786 .fan_mult = 1,
787 .fan_max = I8K_FAN_HIGH,
788 },
789};
790
Guenter Roeck12186f52013-12-14 09:30:09 -0800791static struct dmi_system_id i8k_dmi_table[] __initdata = {
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700792 {
793 .ident = "Dell Inspiron",
794 .matches = {
795 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
796 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
797 },
798 },
799 {
800 .ident = "Dell Latitude",
801 .matches = {
802 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
803 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
804 },
805 },
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700806 {
807 .ident = "Dell Inspiron 2",
808 .matches = {
809 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
810 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
811 },
812 },
813 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700814 .ident = "Dell Latitude D520",
815 .matches = {
816 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
817 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
818 },
819 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
820 },
821 {
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700822 .ident = "Dell Latitude 2",
823 .matches = {
824 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
825 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
826 },
827 },
Nick Warnea9000d02008-02-06 01:37:47 -0800828 { /* UK Inspiron 6400 */
829 .ident = "Dell Inspiron 3",
830 .matches = {
831 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
832 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
833 },
834 },
Frank Sorenson48103c52008-02-07 00:16:31 -0800835 {
836 .ident = "Dell Inspiron 3",
837 .matches = {
838 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
839 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
840 },
841 },
Andy Spencer7ab21a82009-01-02 16:19:13 +0000842 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700843 .ident = "Dell Precision 490",
844 .matches = {
845 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
846 DMI_MATCH(DMI_PRODUCT_NAME,
847 "Precision WorkStation 490"),
848 },
849 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
850 },
851 {
Andy Spencer7ab21a82009-01-02 16:19:13 +0000852 .ident = "Dell Precision",
853 .matches = {
854 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
855 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
856 },
857 },
Federico Heinzbef2a502009-01-02 16:19:23 +0000858 {
859 .ident = "Dell Vostro",
860 .matches = {
861 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
862 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
863 },
864 },
Alan Cox9aa5b012013-12-03 13:56:56 -0800865 {
866 .ident = "Dell XPS421",
867 .matches = {
868 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
869 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
870 },
871 },
Guenter Roeckff457bd2013-12-14 09:30:17 -0800872 {
873 .ident = "Dell Studio",
874 .matches = {
875 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
876 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
877 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700878 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
Guenter Roeckff457bd2013-12-14 09:30:17 -0800879 },
Guenter Roeck919a0302013-12-14 09:30:18 -0800880 {
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100881 .ident = "Dell XPS 13",
882 .matches = {
883 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
884 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
885 },
886 .driver_data = (void *)&i8k_config_data[DELL_XPS],
887 },
888 {
Guenter Roeck919a0302013-12-14 09:30:18 -0800889 .ident = "Dell XPS M140",
890 .matches = {
891 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
892 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
893 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100894 .driver_data = (void *)&i8k_config_data[DELL_XPS],
Guenter Roeck919a0302013-12-14 09:30:18 -0800895 },
Guenter Roeck12186f52013-12-14 09:30:09 -0800896 { }
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700897};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Pali Rohár148b1fd2014-10-18 00:17:49 +0200899MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/*
902 * Probe for the presence of a supported laptop.
903 */
904static int __init i8k_probe(void)
905{
Guenter Roeck26d09382013-12-14 09:30:19 -0800906 const struct dmi_system_id *id;
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100907 int fan, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700910 * Get DMI information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700912 if (!dmi_check_system(i8k_dmi_table)) {
913 if (!ignore_dmi && !force)
914 return -ENODEV;
915
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800916 pr_info("not running on a supported Dell system.\n");
917 pr_info("vendor=%s, model=%s, version=%s\n",
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700918 i8k_get_dmi_data(DMI_SYS_VENDOR),
919 i8k_get_dmi_data(DMI_PRODUCT_NAME),
920 i8k_get_dmi_data(DMI_BIOS_VERSION));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700922
Guenter Roeck12186f52013-12-14 09:30:09 -0800923 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
924 sizeof(bios_version));
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700927 * Get SMM Dell signature
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 */
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700929 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
930 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800931 pr_err("unable to get SMM Dell signature\n");
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700932 if (!force)
933 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100936 /*
937 * Set fan multiplier and maximal fan speed from dmi config
938 * Values specified in module parameters override values from dmi
939 */
Guenter Roeck26d09382013-12-14 09:30:19 -0800940 id = dmi_first_match(i8k_dmi_table);
Guenter Roeck81474fc2014-06-21 08:08:10 -0700941 if (id && id->driver_data) {
942 const struct i8k_config_data *conf = id->driver_data;
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100943 if (!fan_mult && conf->fan_mult)
944 fan_mult = conf->fan_mult;
945 if (!fan_max && conf->fan_max)
946 fan_max = conf->fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700947 }
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100948
949 i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
Guenter Roeck81474fc2014-06-21 08:08:10 -0700950 i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
Guenter Roeck26d09382013-12-14 09:30:19 -0800951
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100952 if (!fan_mult) {
953 /*
954 * Autodetect fan multiplier based on nominal rpm
955 * If fan reports rpm value too high then set multiplier to 1
956 */
957 for (fan = 0; fan < 2; ++fan) {
958 ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
959 if (ret < 0)
960 continue;
961 if (ret > I8K_FAN_MAX_RPM)
962 i8k_fan_mult = 1;
963 break;
964 }
965 } else {
966 /* Fan multiplier was specified in module param or in dmi */
967 i8k_fan_mult = fan_mult;
968 }
969
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700973static int __init i8k_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700975 struct proc_dir_entry *proc_i8k;
Jean Delvare949a9d72011-05-25 20:43:33 +0200976 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700978 /* Are we running on an supported laptop? */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700979 if (i8k_probe())
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700980 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700982 /* Register the proc entry */
Denis V. Lunev1b502212008-04-29 01:02:34 -0700983 proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops);
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700984 if (!proc_i8k)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700985 return -ENOENT;
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700986
Jean Delvare949a9d72011-05-25 20:43:33 +0200987 err = i8k_init_hwmon();
988 if (err)
989 goto exit_remove_proc;
990
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700991 return 0;
Jean Delvare949a9d72011-05-25 20:43:33 +0200992
993 exit_remove_proc:
994 remove_proc_entry("i8k", NULL);
995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700998static void __exit i8k_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Guenter Roeck82ba1d32013-12-14 09:30:10 -08001000 hwmon_device_unregister(i8k_hwmon_dev);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001001 remove_proc_entry("i8k", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001004module_init(i8k_init);
1005module_exit(i8k_exit);