blob: 6ad087240cefabcd7fbe411b52bed00e04b201ae [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ár8f21d8e2015-01-12 14:32:03 +010046#define I8K_SMM_GET_NOM_SPEED 0x04a3
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define I8K_SMM_GET_TEMP 0x10a3
Pali Rohár5114b472015-01-12 14:31:57 +010048#define I8K_SMM_GET_TEMP_TYPE 0x11a3
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -070049#define I8K_SMM_GET_DELL_SIG1 0xfea3
50#define I8K_SMM_GET_DELL_SIG2 0xffa3
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#define I8K_FAN_MULT 30
Pali Rohár8f21d8e2015-01-12 14:32:03 +010053#define I8K_FAN_MAX_RPM 30000
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define I8K_MAX_TEMP 127
55
56#define I8K_FN_NONE 0x00
57#define I8K_FN_UP 0x01
58#define I8K_FN_DOWN 0x02
59#define I8K_FN_MUTE 0x04
60#define I8K_FN_MASK 0x07
61#define I8K_FN_SHIFT 8
62
63#define I8K_POWER_AC 0x05
64#define I8K_POWER_BATTERY 0x01
65
Arnd Bergmann613655f2010-06-02 14:28:52 +020066static DEFINE_MUTEX(i8k_mutex);
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070067static char bios_version[4];
Jean Delvare949a9d72011-05-25 20:43:33 +020068static struct device *i8k_hwmon_dev;
Guenter Roeck82ba1d32013-12-14 09:30:10 -080069static u32 i8k_hwmon_flags;
Pali Rohár8f21d8e2015-01-12 14:32:03 +010070static uint i8k_fan_mult = I8K_FAN_MULT;
Pali Rohár7f69fb02015-01-12 14:32:02 +010071static uint i8k_pwm_mult;
72static uint i8k_fan_max = I8K_FAN_HIGH;
Guenter Roeck82ba1d32013-12-14 09:30:10 -080073
74#define I8K_HWMON_HAVE_TEMP1 (1 << 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -080075#define I8K_HWMON_HAVE_TEMP2 (1 << 1)
76#define I8K_HWMON_HAVE_TEMP3 (1 << 2)
77#define I8K_HWMON_HAVE_TEMP4 (1 << 3)
78#define I8K_HWMON_HAVE_FAN1 (1 << 4)
79#define I8K_HWMON_HAVE_FAN2 (1 << 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
82MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
83MODULE_LICENSE("GPL");
84
Rusty Russell90ab5ee2012-01-13 09:32:20 +103085static bool force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086module_param(force, bool, 0);
87MODULE_PARM_DESC(force, "Force loading without checking for supported models");
88
Rusty Russell90ab5ee2012-01-13 09:32:20 +103089static bool ignore_dmi;
Dmitry Torokhove70c9d52005-06-25 14:54:25 -070090module_param(ignore_dmi, bool, 0);
91MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
92
Rusty Russell90ab5ee2012-01-13 09:32:20 +103093static bool restricted;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094module_param(restricted, bool, 0);
95MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
96
Rusty Russell90ab5ee2012-01-13 09:32:20 +103097static bool power_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098module_param(power_status, bool, 0600);
99MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
100
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100101static uint fan_mult;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100102module_param(fan_mult, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100103MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
Jochen Eisinger4ed99a22008-05-01 04:34:58 -0700104
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100105static uint fan_max;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100106module_param(fan_max, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100107MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
Guenter Roeck81474fc2014-06-21 08:08:10 -0700108
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700109static int i8k_open_fs(struct inode *inode, struct file *file);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200110static long i8k_ioctl(struct file *, unsigned int, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Arjan van de Ven62322d22006-07-03 00:24:21 -0700112static const struct file_operations i8k_fops = {
Denis V. Lunev1b502212008-04-29 01:02:34 -0700113 .owner = THIS_MODULE,
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700114 .open = i8k_open_fs,
115 .read = seq_read,
116 .llseek = seq_lseek,
117 .release = single_release,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200118 .unlocked_ioctl = i8k_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700121struct smm_regs {
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700122 unsigned int eax;
Guenter Roeck12186f52013-12-14 09:30:09 -0800123 unsigned int ebx __packed;
124 unsigned int ecx __packed;
125 unsigned int edx __packed;
126 unsigned int esi __packed;
127 unsigned int edi __packed;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700128};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jeff Garzik18552562007-10-03 15:15:40 -0400130static inline const char *i8k_get_dmi_data(int field)
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700131{
Jeff Garzik18552562007-10-03 15:15:40 -0400132 const char *dmi_data = dmi_get_system_info(field);
Dmitry Torokhov4f005552005-11-12 00:55:15 -0500133
134 return dmi_data && *dmi_data ? dmi_data : "?";
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/*
138 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
139 */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700140static int i8k_smm(struct smm_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700142 int rc;
143 int eax = regs->eax;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800144 cpumask_var_t old_mask;
145
146 /* SMM requires CPU 0 */
147 if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
148 return -ENOMEM;
149 cpumask_copy(old_mask, &current->cpus_allowed);
Guenter Roeck6d827fb2014-06-21 08:08:08 -0700150 rc = set_cpus_allowed_ptr(current, cpumask_of(0));
151 if (rc)
152 goto out;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800153 if (smp_processor_id() != 0) {
154 rc = -EBUSY;
155 goto out;
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Bradley Smithfe04f222008-02-07 00:16:27 -0800158#if defined(CONFIG_X86_64)
Jim Bos22d32432010-11-15 21:22:37 +0100159 asm volatile("pushq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800160 "movl 0(%%rax),%%edx\n\t"
161 "pushq %%rdx\n\t"
162 "movl 4(%%rax),%%ebx\n\t"
163 "movl 8(%%rax),%%ecx\n\t"
164 "movl 12(%%rax),%%edx\n\t"
165 "movl 16(%%rax),%%esi\n\t"
166 "movl 20(%%rax),%%edi\n\t"
167 "popq %%rax\n\t"
168 "out %%al,$0xb2\n\t"
169 "out %%al,$0x84\n\t"
170 "xchgq %%rax,(%%rsp)\n\t"
171 "movl %%ebx,4(%%rax)\n\t"
172 "movl %%ecx,8(%%rax)\n\t"
173 "movl %%edx,12(%%rax)\n\t"
174 "movl %%esi,16(%%rax)\n\t"
175 "movl %%edi,20(%%rax)\n\t"
176 "popq %%rdx\n\t"
177 "movl %%edx,0(%%rax)\n\t"
Luca Tettamantibc1f4192011-05-25 20:43:31 +0200178 "pushfq\n\t"
179 "popq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800180 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800181 : "=a"(rc)
Bradley Smithfe04f222008-02-07 00:16:27 -0800182 : "a"(regs)
183 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
184#else
Jim Bos22d32432010-11-15 21:22:37 +0100185 asm volatile("pushl %%eax\n\t"
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700186 "movl 0(%%eax),%%edx\n\t"
187 "push %%edx\n\t"
188 "movl 4(%%eax),%%ebx\n\t"
189 "movl 8(%%eax),%%ecx\n\t"
190 "movl 12(%%eax),%%edx\n\t"
191 "movl 16(%%eax),%%esi\n\t"
192 "movl 20(%%eax),%%edi\n\t"
193 "popl %%eax\n\t"
194 "out %%al,$0xb2\n\t"
195 "out %%al,$0x84\n\t"
196 "xchgl %%eax,(%%esp)\n\t"
197 "movl %%ebx,4(%%eax)\n\t"
198 "movl %%ecx,8(%%eax)\n\t"
199 "movl %%edx,12(%%eax)\n\t"
200 "movl %%esi,16(%%eax)\n\t"
201 "movl %%edi,20(%%eax)\n\t"
202 "popl %%edx\n\t"
203 "movl %%edx,0(%%eax)\n\t"
204 "lahf\n\t"
205 "shrl $8,%%eax\n\t"
Jim Bos6b4e81d2010-11-13 12:13:53 +0100206 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800207 : "=a"(rc)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700208 : "a"(regs)
209 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
Bradley Smithfe04f222008-02-07 00:16:27 -0800210#endif
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700211 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800212 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800214out:
215 set_cpus_allowed_ptr(current, old_mask);
216 free_cpumask_var(old_mask);
217 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Read the Fn key status.
222 */
223static int i8k_get_fn_status(void)
224{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700225 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700226 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Guenter Roeck12186f52013-12-14 09:30:09 -0800228 rc = i8k_smm(&regs);
229 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700230 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700232 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
233 case I8K_FN_UP:
234 return I8K_VOL_UP;
235 case I8K_FN_DOWN:
236 return I8K_VOL_DOWN;
237 case I8K_FN_MUTE:
238 return I8K_VOL_MUTE;
239 default:
240 return 0;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244/*
245 * Read the power status.
246 */
247static int i8k_get_power_status(void)
248{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700249 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700250 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Guenter Roeck12186f52013-12-14 09:30:09 -0800252 rc = i8k_smm(&regs);
253 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700254 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700256 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259/*
260 * Read the fan status.
261 */
262static int i8k_get_fan_status(int fan)
263{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700264 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700266 regs.ebx = fan & 0xff;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700267 return i8k_smm(&regs) ? : regs.eax & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/*
271 * Read the fan speed in RPM.
272 */
273static int i8k_get_fan_speed(int fan)
274{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700275 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700277 regs.ebx = fan & 0xff;
Guenter Roeck26d09382013-12-14 09:30:19 -0800278 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/*
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100282 * Read the fan nominal rpm for specific fan speed.
283 */
284static int i8k_get_fan_nominal_speed(int fan, int speed)
285{
286 struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
287
288 regs.ebx = (fan & 0xff) | (speed << 8);
289 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
290}
291
292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * Set the fan speed (off, low, high). Returns the new fan status.
294 */
295static int i8k_set_fan(int fan, int speed)
296{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700297 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Guenter Roeck81474fc2014-06-21 08:08:10 -0700299 speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700300 regs.ebx = (fan & 0xff) | (speed << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700302 return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Pali Rohár5114b472015-01-12 14:31:57 +0100305static int i8k_get_temp_type(int sensor)
306{
307 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
308
309 regs.ebx = sensor & 0xff;
310 return i8k_smm(&regs) ? : regs.eax & 0xff;
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
314 * Read the cpu temperature.
315 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100316static int _i8k_get_temp(int sensor)
317{
318 struct smm_regs regs = {
319 .eax = I8K_SMM_GET_TEMP,
320 .ebx = sensor & 0xff,
321 };
322
323 return i8k_smm(&regs) ? : regs.eax & 0xff;
324}
325
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700326static int i8k_get_temp(int sensor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Guenter Roeck564132d2015-01-12 14:32:00 +0100328 int temp = _i8k_get_temp(sensor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700330 /*
331 * Sometimes the temperature sensor returns 0x99, which is out of range.
Guenter Roeck564132d2015-01-12 14:32:00 +0100332 * In this case we retry (once) before returning an error.
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700333 # 1003655137 00000058 00005a4b
334 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
335 # 1003655139 00000054 00005c52
336 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100337 if (temp == 0x99) {
338 msleep(100);
339 temp = _i8k_get_temp(sensor);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700340 }
Guenter Roeck564132d2015-01-12 14:32:00 +0100341 /*
342 * Return -ENODATA for all invalid temperatures.
343 *
344 * Known instances are the 0x99 value as seen above as well as
345 * 0xc1 (193), which may be returned when trying to read the GPU
346 * temperature if the system supports a GPU and it is currently
347 * turned off.
348 */
Pali Rohár723493c2014-11-18 15:56:54 +0100349 if (temp > I8K_MAX_TEMP)
Pali Rohár83d514d2015-01-12 14:31:59 +0100350 return -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700352 return temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700355static int i8k_get_dell_signature(int req_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700357 struct smm_regs regs = { .eax = req_fn, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700358 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Guenter Roeck12186f52013-12-14 09:30:09 -0800360 rc = i8k_smm(&regs);
361 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700362 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700364 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200367static int
368i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700370 int val = 0;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700371 int speed;
372 unsigned char buff[16];
373 int __user *argp = (int __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700375 if (!argp)
376 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700378 switch (cmd) {
379 case I8K_BIOS_VERSION:
Guenter Roecke551b152013-12-14 09:30:20 -0800380 val = (bios_version[0] << 16) |
381 (bios_version[1] << 8) | bios_version[2];
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700382 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700384 case I8K_MACHINE_ID:
385 memset(buff, 0, 16);
Guenter Roeck12186f52013-12-14 09:30:09 -0800386 strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
387 sizeof(buff));
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700388 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700390 case I8K_FN_STATUS:
391 val = i8k_get_fn_status();
392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700394 case I8K_POWER_STATUS:
395 val = i8k_get_power_status();
396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700398 case I8K_GET_TEMP:
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700399 val = i8k_get_temp(0);
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_GET_SPEED:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700403 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700404 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700405
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700406 val = i8k_get_fan_speed(val);
407 break;
408
409 case I8K_GET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700410 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700411 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700412
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700413 val = i8k_get_fan_status(val);
414 break;
415
416 case I8K_SET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700417 if (restricted && !capable(CAP_SYS_ADMIN))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700418 return -EPERM;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700419
420 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700421 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700422
423 if (copy_from_user(&speed, argp + 1, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700424 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700425
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700426 val = i8k_set_fan(val, speed);
427 break;
428
429 default:
430 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700433 if (val < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700434 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700436 switch (cmd) {
437 case I8K_BIOS_VERSION:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700438 if (copy_to_user(argp, &val, 4))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700439 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700440
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700441 break;
442 case I8K_MACHINE_ID:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700443 if (copy_to_user(argp, buff, 16))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700444 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700445
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700446 break;
447 default:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700448 if (copy_to_user(argp, &val, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700449 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700450
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200457static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
458{
459 long ret;
460
Arnd Bergmann613655f2010-06-02 14:28:52 +0200461 mutex_lock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200462 ret = i8k_ioctl_unlocked(fp, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200463 mutex_unlock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200464
465 return ret;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
469 * Print the information for /proc/i8k.
470 */
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700471static int i8k_proc_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700473 int fn_key, cpu_temp, ac_power;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700474 int left_fan, right_fan, left_speed, right_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200476 cpu_temp = i8k_get_temp(0); /* 11100 µs */
477 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
478 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
479 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
480 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
481 fn_key = i8k_get_fn_status(); /* 750 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700482 if (power_status)
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200483 ac_power = i8k_get_power_status(); /* 14700 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700484 else
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700485 ac_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700487 /*
488 * Info:
489 *
490 * 1) Format version (this will change if format changes)
491 * 2) BIOS version
492 * 3) BIOS machine ID
493 * 4) Cpu temperature
494 * 5) Left fan status
495 * 6) Right fan status
496 * 7) Left fan speed
497 * 8) Right fan speed
498 * 9) AC power
499 * 10) Fn Key status
500 */
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700501 return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
502 I8K_PROC_FMT,
503 bios_version,
Dmitry Torokhov4f005552005-11-12 00:55:15 -0500504 i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700505 cpu_temp,
506 left_fan, right_fan, left_speed, right_speed,
507 ac_power, fn_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700510static int i8k_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700512 return single_open(file, i8k_proc_show, NULL);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700513}
514
Jean Delvare949a9d72011-05-25 20:43:33 +0200515
516/*
517 * Hwmon interface
518 */
519
Pali Rohár5114b472015-01-12 14:31:57 +0100520static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
521 struct device_attribute *devattr,
522 char *buf)
523{
524 static const char * const labels[] = {
525 "CPU",
526 "GPU",
527 "SODIMM",
528 "Other",
529 "Ambient",
530 "Other",
531 };
532 int index = to_sensor_dev_attr(devattr)->index;
533 int type;
534
535 type = i8k_get_temp_type(index);
536 if (type < 0)
537 return type;
538 if (type >= ARRAY_SIZE(labels))
539 type = ARRAY_SIZE(labels) - 1;
540 return sprintf(buf, "%s\n", labels[type]);
541}
542
Jean Delvare949a9d72011-05-25 20:43:33 +0200543static ssize_t i8k_hwmon_show_temp(struct device *dev,
544 struct device_attribute *devattr,
545 char *buf)
546{
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800547 int index = to_sensor_dev_attr(devattr)->index;
548 int temp;
Jean Delvare949a9d72011-05-25 20:43:33 +0200549
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800550 temp = i8k_get_temp(index);
551 if (temp < 0)
552 return temp;
553 return sprintf(buf, "%d\n", temp * 1000);
Jean Delvare949a9d72011-05-25 20:43:33 +0200554}
555
556static ssize_t i8k_hwmon_show_fan(struct device *dev,
557 struct device_attribute *devattr,
558 char *buf)
559{
560 int index = to_sensor_dev_attr(devattr)->index;
561 int fan_speed;
562
563 fan_speed = i8k_get_fan_speed(index);
564 if (fan_speed < 0)
565 return fan_speed;
566 return sprintf(buf, "%d\n", fan_speed);
567}
568
Guenter Roecke7f5f272013-12-14 09:30:21 -0800569static ssize_t i8k_hwmon_show_pwm(struct device *dev,
570 struct device_attribute *devattr,
571 char *buf)
572{
573 int index = to_sensor_dev_attr(devattr)->index;
574 int status;
575
576 status = i8k_get_fan_status(index);
577 if (status < 0)
578 return -EIO;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700579 return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
Guenter Roecke7f5f272013-12-14 09:30:21 -0800580}
581
582static ssize_t i8k_hwmon_set_pwm(struct device *dev,
583 struct device_attribute *attr,
584 const char *buf, size_t count)
585{
586 int index = to_sensor_dev_attr(attr)->index;
587 unsigned long val;
588 int err;
589
590 err = kstrtoul(buf, 10, &val);
591 if (err)
592 return err;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700593 val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800594
595 mutex_lock(&i8k_mutex);
596 err = i8k_set_fan(index, val);
597 mutex_unlock(&i8k_mutex);
598
599 return err < 0 ? -EIO : count;
600}
601
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800602static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
Pali Rohár5114b472015-01-12 14:31:57 +0100603static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
604 0);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800605static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
Pali Rohár5114b472015-01-12 14:31:57 +0100606static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
607 1);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800608static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
Pali Rohár5114b472015-01-12 14:31:57 +0100609static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
610 2);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800611static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
Pali Rohár5114b472015-01-12 14:31:57 +0100612static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
613 3);
Jean Delvare949a9d72011-05-25 20:43:33 +0200614static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
615 I8K_FAN_LEFT);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800616static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
617 i8k_hwmon_set_pwm, I8K_FAN_LEFT);
Jean Delvare949a9d72011-05-25 20:43:33 +0200618static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
619 I8K_FAN_RIGHT);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800620static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
621 i8k_hwmon_set_pwm, I8K_FAN_RIGHT);
Jean Delvare949a9d72011-05-25 20:43:33 +0200622
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800623static struct attribute *i8k_attrs[] = {
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800624 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
Pali Rohár5114b472015-01-12 14:31:57 +0100625 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
626 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
627 &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */
628 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */
629 &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */
630 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */
631 &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */
632 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 8 */
633 &sensor_dev_attr_pwm1.dev_attr.attr, /* 9 */
634 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 10 */
635 &sensor_dev_attr_pwm2.dev_attr.attr, /* 11 */
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800636 NULL
637};
638
639static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
640 int index)
Jean Delvare949a9d72011-05-25 20:43:33 +0200641{
Pali Rohár5114b472015-01-12 14:31:57 +0100642 if (index >= 0 && index <= 1 &&
643 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800644 return 0;
Pali Rohár5114b472015-01-12 14:31:57 +0100645 if (index >= 2 && index <= 3 &&
646 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800647 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700648 if (index >= 4 && index <= 5 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100649 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800650 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700651 if (index >= 6 && index <= 7 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100652 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
653 return 0;
654 if (index >= 8 && index <= 9 &&
655 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
656 return 0;
657 if (index >= 10 && index <= 11 &&
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800658 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
659 return 0;
660
661 return attr->mode;
Jean Delvare949a9d72011-05-25 20:43:33 +0200662}
663
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800664static const struct attribute_group i8k_group = {
665 .attrs = i8k_attrs,
666 .is_visible = i8k_is_visible,
667};
668__ATTRIBUTE_GROUPS(i8k);
669
Jean Delvare949a9d72011-05-25 20:43:33 +0200670static int __init i8k_init_hwmon(void)
671{
672 int err;
673
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800674 i8k_hwmon_flags = 0;
675
Pali Rohár672af222015-01-12 14:31:58 +0100676 /* CPU temperature attributes, if temperature type is OK */
677 err = i8k_get_temp_type(0);
678 if (err >= 0)
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800679 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800680 /* check for additional temperature sensors */
Pali Rohár672af222015-01-12 14:31:58 +0100681 err = i8k_get_temp_type(1);
682 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800683 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
Pali Rohár672af222015-01-12 14:31:58 +0100684 err = i8k_get_temp_type(2);
685 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800686 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
Pali Rohár672af222015-01-12 14:31:58 +0100687 err = i8k_get_temp_type(3);
688 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800689 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800690
691 /* Left fan attributes, if left fan is present */
692 err = i8k_get_fan_status(I8K_FAN_LEFT);
693 if (err >= 0)
694 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
695
696 /* Right fan attributes, if right fan is present */
697 err = i8k_get_fan_status(I8K_FAN_RIGHT);
698 if (err >= 0)
699 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
700
701 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "i8k", NULL,
702 i8k_groups);
Jean Delvare949a9d72011-05-25 20:43:33 +0200703 if (IS_ERR(i8k_hwmon_dev)) {
704 err = PTR_ERR(i8k_hwmon_dev);
705 i8k_hwmon_dev = NULL;
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800706 pr_err("hwmon registration failed (%d)\n", err);
Jean Delvare949a9d72011-05-25 20:43:33 +0200707 return err;
708 }
Jean Delvare949a9d72011-05-25 20:43:33 +0200709 return 0;
Jean Delvare949a9d72011-05-25 20:43:33 +0200710}
711
Guenter Roeck81474fc2014-06-21 08:08:10 -0700712struct i8k_config_data {
Pali Rohár7f69fb02015-01-12 14:32:02 +0100713 uint fan_mult;
714 uint fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700715};
716
717enum i8k_configs {
Guenter Roeck7b883442014-06-21 08:08:12 -0700718 DELL_LATITUDE_D520,
Steven Honeyman06c88b02014-09-05 18:03:42 +0100719 DELL_LATITUDE_E6540,
Guenter Roeck7b883442014-06-21 08:08:12 -0700720 DELL_PRECISION_490,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700721 DELL_STUDIO,
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100722 DELL_XPS,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700723};
724
725static const struct i8k_config_data i8k_config_data[] = {
Guenter Roeck7b883442014-06-21 08:08:12 -0700726 [DELL_LATITUDE_D520] = {
727 .fan_mult = 1,
728 .fan_max = I8K_FAN_TURBO,
729 },
Steven Honeyman06c88b02014-09-05 18:03:42 +0100730 [DELL_LATITUDE_E6540] = {
731 .fan_mult = 1,
732 .fan_max = I8K_FAN_HIGH,
733 },
Guenter Roeck7b883442014-06-21 08:08:12 -0700734 [DELL_PRECISION_490] = {
735 .fan_mult = 1,
736 .fan_max = I8K_FAN_TURBO,
737 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700738 [DELL_STUDIO] = {
739 .fan_mult = 1,
740 .fan_max = I8K_FAN_HIGH,
741 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100742 [DELL_XPS] = {
Guenter Roeck81474fc2014-06-21 08:08:10 -0700743 .fan_mult = 1,
744 .fan_max = I8K_FAN_HIGH,
745 },
746};
747
Guenter Roeck12186f52013-12-14 09:30:09 -0800748static struct dmi_system_id i8k_dmi_table[] __initdata = {
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700749 {
750 .ident = "Dell Inspiron",
751 .matches = {
752 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
753 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
754 },
755 },
756 {
757 .ident = "Dell Latitude",
758 .matches = {
759 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
760 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
761 },
762 },
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700763 {
764 .ident = "Dell Inspiron 2",
765 .matches = {
766 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
767 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
768 },
769 },
770 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700771 .ident = "Dell Latitude D520",
772 .matches = {
773 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
774 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
775 },
776 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
777 },
778 {
Pali Rohár0f352232014-10-10 11:12:47 +0200779 .ident = "Dell Latitude E6440",
780 .matches = {
781 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
782 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6440"),
783 },
784 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_E6540],
785 },
786 {
Steven Honeyman06c88b02014-09-05 18:03:42 +0100787 .ident = "Dell Latitude E6540",
788 .matches = {
789 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
790 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6540"),
791 },
792 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_E6540],
793 },
794 {
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700795 .ident = "Dell Latitude 2",
796 .matches = {
797 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
798 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
799 },
800 },
Nick Warnea9000d02008-02-06 01:37:47 -0800801 { /* UK Inspiron 6400 */
802 .ident = "Dell Inspiron 3",
803 .matches = {
804 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
805 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
806 },
807 },
Frank Sorenson48103c52008-02-07 00:16:31 -0800808 {
809 .ident = "Dell Inspiron 3",
810 .matches = {
811 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
812 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
813 },
814 },
Andy Spencer7ab21a82009-01-02 16:19:13 +0000815 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700816 .ident = "Dell Precision 490",
817 .matches = {
818 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
819 DMI_MATCH(DMI_PRODUCT_NAME,
820 "Precision WorkStation 490"),
821 },
822 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
823 },
824 {
Andy Spencer7ab21a82009-01-02 16:19:13 +0000825 .ident = "Dell Precision",
826 .matches = {
827 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
828 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
829 },
830 },
Federico Heinzbef2a502009-01-02 16:19:23 +0000831 {
832 .ident = "Dell Vostro",
833 .matches = {
834 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
835 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
836 },
837 },
Alan Cox9aa5b012013-12-03 13:56:56 -0800838 {
839 .ident = "Dell XPS421",
840 .matches = {
841 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
842 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
843 },
844 },
Guenter Roeckff457bd2013-12-14 09:30:17 -0800845 {
846 .ident = "Dell Studio",
847 .matches = {
848 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
849 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
850 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700851 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
Guenter Roeckff457bd2013-12-14 09:30:17 -0800852 },
Guenter Roeck919a0302013-12-14 09:30:18 -0800853 {
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100854 .ident = "Dell XPS 13",
855 .matches = {
856 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
857 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
858 },
859 .driver_data = (void *)&i8k_config_data[DELL_XPS],
860 },
861 {
Guenter Roeck919a0302013-12-14 09:30:18 -0800862 .ident = "Dell XPS M140",
863 .matches = {
864 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
865 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
866 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100867 .driver_data = (void *)&i8k_config_data[DELL_XPS],
Guenter Roeck919a0302013-12-14 09:30:18 -0800868 },
Guenter Roeck12186f52013-12-14 09:30:09 -0800869 { }
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700870};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Pali Rohár148b1fd2014-10-18 00:17:49 +0200872MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
875 * Probe for the presence of a supported laptop.
876 */
877static int __init i8k_probe(void)
878{
Guenter Roeck26d09382013-12-14 09:30:19 -0800879 const struct dmi_system_id *id;
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100880 int fan, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700883 * Get DMI information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700885 if (!dmi_check_system(i8k_dmi_table)) {
886 if (!ignore_dmi && !force)
887 return -ENODEV;
888
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800889 pr_info("not running on a supported Dell system.\n");
890 pr_info("vendor=%s, model=%s, version=%s\n",
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700891 i8k_get_dmi_data(DMI_SYS_VENDOR),
892 i8k_get_dmi_data(DMI_PRODUCT_NAME),
893 i8k_get_dmi_data(DMI_BIOS_VERSION));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700895
Guenter Roeck12186f52013-12-14 09:30:09 -0800896 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
897 sizeof(bios_version));
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700900 * Get SMM Dell signature
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700902 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
903 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800904 pr_err("unable to get SMM Dell signature\n");
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700905 if (!force)
906 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100909 /*
910 * Set fan multiplier and maximal fan speed from dmi config
911 * Values specified in module parameters override values from dmi
912 */
Guenter Roeck26d09382013-12-14 09:30:19 -0800913 id = dmi_first_match(i8k_dmi_table);
Guenter Roeck81474fc2014-06-21 08:08:10 -0700914 if (id && id->driver_data) {
915 const struct i8k_config_data *conf = id->driver_data;
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100916 if (!fan_mult && conf->fan_mult)
917 fan_mult = conf->fan_mult;
918 if (!fan_max && conf->fan_max)
919 fan_max = conf->fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700920 }
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100921
922 i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
Guenter Roeck81474fc2014-06-21 08:08:10 -0700923 i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
Guenter Roeck26d09382013-12-14 09:30:19 -0800924
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100925 if (!fan_mult) {
926 /*
927 * Autodetect fan multiplier based on nominal rpm
928 * If fan reports rpm value too high then set multiplier to 1
929 */
930 for (fan = 0; fan < 2; ++fan) {
931 ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
932 if (ret < 0)
933 continue;
934 if (ret > I8K_FAN_MAX_RPM)
935 i8k_fan_mult = 1;
936 break;
937 }
938 } else {
939 /* Fan multiplier was specified in module param or in dmi */
940 i8k_fan_mult = fan_mult;
941 }
942
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700946static int __init i8k_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700948 struct proc_dir_entry *proc_i8k;
Jean Delvare949a9d72011-05-25 20:43:33 +0200949 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700951 /* Are we running on an supported laptop? */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700952 if (i8k_probe())
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700953 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700955 /* Register the proc entry */
Denis V. Lunev1b502212008-04-29 01:02:34 -0700956 proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops);
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700957 if (!proc_i8k)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700958 return -ENOENT;
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700959
Jean Delvare949a9d72011-05-25 20:43:33 +0200960 err = i8k_init_hwmon();
961 if (err)
962 goto exit_remove_proc;
963
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700964 return 0;
Jean Delvare949a9d72011-05-25 20:43:33 +0200965
966 exit_remove_proc:
967 remove_proc_entry("i8k", NULL);
968 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700971static void __exit i8k_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800973 hwmon_device_unregister(i8k_hwmon_dev);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700974 remove_proc_entry("i8k", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700977module_init(i8k_init);
978module_exit(i8k_exit);