blob: c43318d3416e20a4d027a32402b1cf751718b8c6 [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>");
Pali Rohár039ae582015-05-14 13:16:37 +020084MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085MODULE_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
Pali Rohár039ae582015-05-14 13:16:37 +020096#if IS_ENABLED(CONFIG_I8K)
Rusty Russell90ab5ee2012-01-13 09:32:20 +103097static bool restricted;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098module_param(restricted, bool, 0);
99MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
100
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030101static bool power_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102module_param(power_status, bool, 0600);
103MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
Pali Rohár039ae582015-05-14 13:16:37 +0200104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100106static uint fan_mult;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100107module_param(fan_mult, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100108MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
Jochen Eisinger4ed99a22008-05-01 04:34:58 -0700109
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100110static uint fan_max;
Pali Rohár7f69fb02015-01-12 14:32:02 +0100111module_param(fan_max, uint, 0);
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100112MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
Guenter Roeck81474fc2014-06-21 08:08:10 -0700113
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700114struct smm_regs {
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700115 unsigned int eax;
Guenter Roeck12186f52013-12-14 09:30:09 -0800116 unsigned int ebx __packed;
117 unsigned int ecx __packed;
118 unsigned int edx __packed;
119 unsigned int esi __packed;
120 unsigned int edi __packed;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700121};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Jeff Garzik18552562007-10-03 15:15:40 -0400123static inline const char *i8k_get_dmi_data(int field)
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700124{
Jeff Garzik18552562007-10-03 15:15:40 -0400125 const char *dmi_data = dmi_get_system_info(field);
Dmitry Torokhov4f005552005-11-12 00:55:15 -0500126
127 return dmi_data && *dmi_data ? dmi_data : "?";
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/*
131 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
132 */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700133static int i8k_smm(struct smm_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700135 int rc;
136 int eax = regs->eax;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800137 cpumask_var_t old_mask;
138
139 /* SMM requires CPU 0 */
140 if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
141 return -ENOMEM;
142 cpumask_copy(old_mask, &current->cpus_allowed);
Guenter Roeck6d827fb2014-06-21 08:08:08 -0700143 rc = set_cpus_allowed_ptr(current, cpumask_of(0));
144 if (rc)
145 goto out;
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800146 if (smp_processor_id() != 0) {
147 rc = -EBUSY;
148 goto out;
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Bradley Smithfe04f222008-02-07 00:16:27 -0800151#if defined(CONFIG_X86_64)
Jim Bos22d32432010-11-15 21:22:37 +0100152 asm volatile("pushq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800153 "movl 0(%%rax),%%edx\n\t"
154 "pushq %%rdx\n\t"
155 "movl 4(%%rax),%%ebx\n\t"
156 "movl 8(%%rax),%%ecx\n\t"
157 "movl 12(%%rax),%%edx\n\t"
158 "movl 16(%%rax),%%esi\n\t"
159 "movl 20(%%rax),%%edi\n\t"
160 "popq %%rax\n\t"
161 "out %%al,$0xb2\n\t"
162 "out %%al,$0x84\n\t"
163 "xchgq %%rax,(%%rsp)\n\t"
164 "movl %%ebx,4(%%rax)\n\t"
165 "movl %%ecx,8(%%rax)\n\t"
166 "movl %%edx,12(%%rax)\n\t"
167 "movl %%esi,16(%%rax)\n\t"
168 "movl %%edi,20(%%rax)\n\t"
169 "popq %%rdx\n\t"
170 "movl %%edx,0(%%rax)\n\t"
Luca Tettamantibc1f4192011-05-25 20:43:31 +0200171 "pushfq\n\t"
172 "popq %%rax\n\t"
Bradley Smithfe04f222008-02-07 00:16:27 -0800173 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800174 : "=a"(rc)
Bradley Smithfe04f222008-02-07 00:16:27 -0800175 : "a"(regs)
176 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
177#else
Jim Bos22d32432010-11-15 21:22:37 +0100178 asm volatile("pushl %%eax\n\t"
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700179 "movl 0(%%eax),%%edx\n\t"
180 "push %%edx\n\t"
181 "movl 4(%%eax),%%ebx\n\t"
182 "movl 8(%%eax),%%ecx\n\t"
183 "movl 12(%%eax),%%edx\n\t"
184 "movl 16(%%eax),%%esi\n\t"
185 "movl 20(%%eax),%%edi\n\t"
186 "popl %%eax\n\t"
187 "out %%al,$0xb2\n\t"
188 "out %%al,$0x84\n\t"
189 "xchgl %%eax,(%%esp)\n\t"
190 "movl %%ebx,4(%%eax)\n\t"
191 "movl %%ecx,8(%%eax)\n\t"
192 "movl %%edx,12(%%eax)\n\t"
193 "movl %%esi,16(%%eax)\n\t"
194 "movl %%edi,20(%%eax)\n\t"
195 "popl %%edx\n\t"
196 "movl %%edx,0(%%eax)\n\t"
197 "lahf\n\t"
198 "shrl $8,%%eax\n\t"
Jim Bos6b4e81d2010-11-13 12:13:53 +0100199 "andl $1,%%eax\n"
Guenter Roeck12186f52013-12-14 09:30:09 -0800200 : "=a"(rc)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700201 : "a"(regs)
202 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
Bradley Smithfe04f222008-02-07 00:16:27 -0800203#endif
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700204 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800205 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Guenter Roeckf36fdb92013-12-14 09:30:15 -0800207out:
208 set_cpus_allowed_ptr(current, old_mask);
209 free_cpumask_var(old_mask);
210 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * Read the fan status.
215 */
216static int i8k_get_fan_status(int fan)
217{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700218 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700220 regs.ebx = fan & 0xff;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700221 return i8k_smm(&regs) ? : regs.eax & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*
225 * Read the fan speed in RPM.
226 */
227static int i8k_get_fan_speed(int fan)
228{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700229 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700231 regs.ebx = fan & 0xff;
Guenter Roeck26d09382013-12-14 09:30:19 -0800232 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235/*
Pali Rohárf989e552015-01-12 14:32:05 +0100236 * Read the fan type.
237 */
238static int i8k_get_fan_type(int fan)
239{
240 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, };
241
242 regs.ebx = fan & 0xff;
243 return i8k_smm(&regs) ? : regs.eax & 0xff;
244}
245
246/*
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100247 * Read the fan nominal rpm for specific fan speed.
248 */
249static int i8k_get_fan_nominal_speed(int fan, int speed)
250{
251 struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
252
253 regs.ebx = (fan & 0xff) | (speed << 8);
254 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
255}
256
257/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * Set the fan speed (off, low, high). Returns the new fan status.
259 */
260static int i8k_set_fan(int fan, int speed)
261{
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700262 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Guenter Roeck81474fc2014-06-21 08:08:10 -0700264 speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700265 regs.ebx = (fan & 0xff) | (speed << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700267 return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Pali Rohár5114b472015-01-12 14:31:57 +0100270static int i8k_get_temp_type(int sensor)
271{
272 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
273
274 regs.ebx = sensor & 0xff;
275 return i8k_smm(&regs) ? : regs.eax & 0xff;
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/*
279 * Read the cpu temperature.
280 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100281static int _i8k_get_temp(int sensor)
282{
283 struct smm_regs regs = {
284 .eax = I8K_SMM_GET_TEMP,
285 .ebx = sensor & 0xff,
286 };
287
288 return i8k_smm(&regs) ? : regs.eax & 0xff;
289}
290
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700291static int i8k_get_temp(int sensor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Guenter Roeck564132d2015-01-12 14:32:00 +0100293 int temp = _i8k_get_temp(sensor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700295 /*
296 * Sometimes the temperature sensor returns 0x99, which is out of range.
Guenter Roeck564132d2015-01-12 14:32:00 +0100297 * In this case we retry (once) before returning an error.
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700298 # 1003655137 00000058 00005a4b
299 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
300 # 1003655139 00000054 00005c52
301 */
Guenter Roeck564132d2015-01-12 14:32:00 +0100302 if (temp == 0x99) {
303 msleep(100);
304 temp = _i8k_get_temp(sensor);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700305 }
Guenter Roeck564132d2015-01-12 14:32:00 +0100306 /*
307 * Return -ENODATA for all invalid temperatures.
308 *
309 * Known instances are the 0x99 value as seen above as well as
310 * 0xc1 (193), which may be returned when trying to read the GPU
311 * temperature if the system supports a GPU and it is currently
312 * turned off.
313 */
Pali Rohár723493c2014-11-18 15:56:54 +0100314 if (temp > I8K_MAX_TEMP)
Pali Rohár83d514d2015-01-12 14:31:59 +0100315 return -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700317 return temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700320static int i8k_get_dell_signature(int req_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700322 struct smm_regs regs = { .eax = req_fn, };
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700323 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Guenter Roeck12186f52013-12-14 09:30:09 -0800325 rc = i8k_smm(&regs);
326 if (rc < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700327 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700329 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Pali Rohár039ae582015-05-14 13:16:37 +0200332#if IS_ENABLED(CONFIG_I8K)
333
334/*
335 * Read the Fn key status.
336 */
337static int i8k_get_fn_status(void)
338{
339 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
340 int rc;
341
342 rc = i8k_smm(&regs);
343 if (rc < 0)
344 return rc;
345
346 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
347 case I8K_FN_UP:
348 return I8K_VOL_UP;
349 case I8K_FN_DOWN:
350 return I8K_VOL_DOWN;
351 case I8K_FN_MUTE:
352 return I8K_VOL_MUTE;
353 default:
354 return 0;
355 }
356}
357
358/*
359 * Read the power status.
360 */
361static int i8k_get_power_status(void)
362{
363 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
364 int rc;
365
366 rc = i8k_smm(&regs);
367 if (rc < 0)
368 return rc;
369
370 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
371}
372
373/*
374 * Procfs interface
375 */
376
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200377static int
378i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700380 int val = 0;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700381 int speed;
382 unsigned char buff[16];
383 int __user *argp = (int __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700385 if (!argp)
386 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700388 switch (cmd) {
389 case I8K_BIOS_VERSION:
Guenter Roecke551b152013-12-14 09:30:20 -0800390 val = (bios_version[0] << 16) |
391 (bios_version[1] << 8) | bios_version[2];
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700394 case I8K_MACHINE_ID:
395 memset(buff, 0, 16);
Guenter Roeck12186f52013-12-14 09:30:09 -0800396 strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
397 sizeof(buff));
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700400 case I8K_FN_STATUS:
401 val = i8k_get_fn_status();
402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700404 case I8K_POWER_STATUS:
405 val = i8k_get_power_status();
406 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700408 case I8K_GET_TEMP:
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700409 val = i8k_get_temp(0);
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700412 case I8K_GET_SPEED:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700413 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700414 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700415
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700416 val = i8k_get_fan_speed(val);
417 break;
418
419 case I8K_GET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700420 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
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700423 val = i8k_get_fan_status(val);
424 break;
425
426 case I8K_SET_FAN:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700427 if (restricted && !capable(CAP_SYS_ADMIN))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700428 return -EPERM;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700429
430 if (copy_from_user(&val, argp, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700431 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700432
433 if (copy_from_user(&speed, argp + 1, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700434 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700435
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700436 val = i8k_set_fan(val, speed);
437 break;
438
439 default:
440 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700443 if (val < 0)
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700444 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700446 switch (cmd) {
447 case I8K_BIOS_VERSION:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700448 if (copy_to_user(argp, &val, 4))
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;
452 case I8K_MACHINE_ID:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700453 if (copy_to_user(argp, buff, 16))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700454 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700455
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700456 break;
457 default:
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700458 if (copy_to_user(argp, &val, sizeof(int)))
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700459 return -EFAULT;
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700460
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200467static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
468{
469 long ret;
470
Arnd Bergmann613655f2010-06-02 14:28:52 +0200471 mutex_lock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200472 ret = i8k_ioctl_unlocked(fp, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200473 mutex_unlock(&i8k_mutex);
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +0200474
475 return ret;
476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/*
479 * Print the information for /proc/i8k.
480 */
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700481static int i8k_proc_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Dmitry Torokhov352f8f82005-06-25 14:54:26 -0700483 int fn_key, cpu_temp, ac_power;
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700484 int left_fan, right_fan, left_speed, right_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200486 cpu_temp = i8k_get_temp(0); /* 11100 µs */
487 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
488 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
489 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
490 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
491 fn_key = i8k_get_fn_status(); /* 750 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700492 if (power_status)
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200493 ac_power = i8k_get_power_status(); /* 14700 µs */
Dmitry Torokhov8378b9242005-06-25 14:54:27 -0700494 else
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700495 ac_power = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700497 /*
498 * Info:
499 *
500 * 1) Format version (this will change if format changes)
501 * 2) BIOS version
502 * 3) BIOS machine ID
503 * 4) Cpu temperature
504 * 5) Left fan status
505 * 6) Right fan status
506 * 7) Left fan speed
507 * 8) Right fan speed
508 * 9) AC power
509 * 10) Fn Key status
510 */
Joe Perches3a267d32015-02-21 18:53:47 -0800511 seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
512 I8K_PROC_FMT,
513 bios_version,
514 i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
515 cpu_temp,
516 left_fan, right_fan, left_speed, right_speed,
517 ac_power, fn_key);
518
519 return 0;
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
Pali Rohár039ae582015-05-14 13:16:37 +0200527static const struct file_operations i8k_fops = {
528 .owner = THIS_MODULE,
529 .open = i8k_open_fs,
530 .read = seq_read,
531 .llseek = seq_lseek,
532 .release = single_release,
533 .unlocked_ioctl = i8k_ioctl,
534};
535
536static void __init i8k_init_procfs(void)
537{
538 /* Register the proc entry */
539 proc_create("i8k", 0, NULL, &i8k_fops);
540}
541
542static void __exit i8k_exit_procfs(void)
543{
544 remove_proc_entry("i8k", NULL);
545}
546
547#else
548
549static inline void __init i8k_init_procfs(void)
550{
551}
552
553static inline void __exit i8k_exit_procfs(void)
554{
555}
556
557#endif
Jean Delvare949a9d72011-05-25 20:43:33 +0200558
559/*
560 * Hwmon interface
561 */
562
Pali Rohár5114b472015-01-12 14:31:57 +0100563static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
564 struct device_attribute *devattr,
565 char *buf)
566{
567 static const char * const labels[] = {
568 "CPU",
569 "GPU",
570 "SODIMM",
571 "Other",
572 "Ambient",
573 "Other",
574 };
575 int index = to_sensor_dev_attr(devattr)->index;
576 int type;
577
578 type = i8k_get_temp_type(index);
579 if (type < 0)
580 return type;
581 if (type >= ARRAY_SIZE(labels))
582 type = ARRAY_SIZE(labels) - 1;
583 return sprintf(buf, "%s\n", labels[type]);
584}
585
Jean Delvare949a9d72011-05-25 20:43:33 +0200586static ssize_t i8k_hwmon_show_temp(struct device *dev,
587 struct device_attribute *devattr,
588 char *buf)
589{
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800590 int index = to_sensor_dev_attr(devattr)->index;
591 int temp;
Jean Delvare949a9d72011-05-25 20:43:33 +0200592
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800593 temp = i8k_get_temp(index);
594 if (temp < 0)
595 return temp;
596 return sprintf(buf, "%d\n", temp * 1000);
Jean Delvare949a9d72011-05-25 20:43:33 +0200597}
598
Pali Rohárf989e552015-01-12 14:32:05 +0100599static ssize_t i8k_hwmon_show_fan_label(struct device *dev,
600 struct device_attribute *devattr,
601 char *buf)
602{
603 static const char * const labels[] = {
604 "Processor Fan",
605 "Motherboard Fan",
606 "Video Fan",
607 "Power Supply Fan",
608 "Chipset Fan",
609 "Other Fan",
610 };
611 int index = to_sensor_dev_attr(devattr)->index;
612 bool dock = false;
613 int type;
614
615 type = i8k_get_fan_type(index);
616 if (type < 0)
617 return type;
618
619 if (type & 0x10) {
620 dock = true;
621 type &= 0x0F;
622 }
623
624 if (type >= ARRAY_SIZE(labels))
625 type = (ARRAY_SIZE(labels) - 1);
626
627 return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
628}
629
Jean Delvare949a9d72011-05-25 20:43:33 +0200630static ssize_t i8k_hwmon_show_fan(struct device *dev,
631 struct device_attribute *devattr,
632 char *buf)
633{
634 int index = to_sensor_dev_attr(devattr)->index;
635 int fan_speed;
636
637 fan_speed = i8k_get_fan_speed(index);
638 if (fan_speed < 0)
639 return fan_speed;
640 return sprintf(buf, "%d\n", fan_speed);
641}
642
Guenter Roecke7f5f272013-12-14 09:30:21 -0800643static ssize_t i8k_hwmon_show_pwm(struct device *dev,
644 struct device_attribute *devattr,
645 char *buf)
646{
647 int index = to_sensor_dev_attr(devattr)->index;
648 int status;
649
650 status = i8k_get_fan_status(index);
651 if (status < 0)
652 return -EIO;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700653 return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
Guenter Roecke7f5f272013-12-14 09:30:21 -0800654}
655
656static ssize_t i8k_hwmon_set_pwm(struct device *dev,
657 struct device_attribute *attr,
658 const char *buf, size_t count)
659{
660 int index = to_sensor_dev_attr(attr)->index;
661 unsigned long val;
662 int err;
663
664 err = kstrtoul(buf, 10, &val);
665 if (err)
666 return err;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700667 val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800668
669 mutex_lock(&i8k_mutex);
670 err = i8k_set_fan(index, val);
671 mutex_unlock(&i8k_mutex);
672
673 return err < 0 ? -EIO : count;
674}
675
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800676static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
Pali Rohár5114b472015-01-12 14:31:57 +0100677static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
678 0);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800679static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
Pali Rohár5114b472015-01-12 14:31:57 +0100680static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
681 1);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800682static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
Pali Rohár5114b472015-01-12 14:31:57 +0100683static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
684 2);
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800685static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
Pali Rohár5114b472015-01-12 14:31:57 +0100686static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
687 3);
Pali Rohárf989e552015-01-12 14:32:05 +0100688static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, 0);
689static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
690 0);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800691static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100692 i8k_hwmon_set_pwm, 0);
Jean Delvare949a9d72011-05-25 20:43:33 +0200693static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
Pali Rohárf989e552015-01-12 14:32:05 +0100694 1);
695static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
696 1);
Guenter Roecke7f5f272013-12-14 09:30:21 -0800697static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
Pali Rohárf989e552015-01-12 14:32:05 +0100698 i8k_hwmon_set_pwm, 1);
Jean Delvare949a9d72011-05-25 20:43:33 +0200699
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800700static struct attribute *i8k_attrs[] = {
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800701 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
Pali Rohár5114b472015-01-12 14:31:57 +0100702 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
703 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
704 &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */
705 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */
706 &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */
707 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */
708 &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */
709 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 8 */
Pali Rohárf989e552015-01-12 14:32:05 +0100710 &sensor_dev_attr_fan1_label.dev_attr.attr, /* 9 */
711 &sensor_dev_attr_pwm1.dev_attr.attr, /* 10 */
712 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 11 */
713 &sensor_dev_attr_fan2_label.dev_attr.attr, /* 12 */
714 &sensor_dev_attr_pwm2.dev_attr.attr, /* 13 */
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800715 NULL
716};
717
718static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
719 int index)
Jean Delvare949a9d72011-05-25 20:43:33 +0200720{
Pali Rohár5114b472015-01-12 14:31:57 +0100721 if (index >= 0 && index <= 1 &&
722 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800723 return 0;
Pali Rohár5114b472015-01-12 14:31:57 +0100724 if (index >= 2 && index <= 3 &&
725 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800726 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700727 if (index >= 4 && index <= 5 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100728 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800729 return 0;
Guenter Roeckb12ce5f2014-06-21 08:08:09 -0700730 if (index >= 6 && index <= 7 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100731 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
732 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100733 if (index >= 8 && index <= 10 &&
Pali Rohár5114b472015-01-12 14:31:57 +0100734 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
735 return 0;
Pali Rohárf989e552015-01-12 14:32:05 +0100736 if (index >= 11 && index <= 13 &&
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800737 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
738 return 0;
739
740 return attr->mode;
Jean Delvare949a9d72011-05-25 20:43:33 +0200741}
742
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800743static const struct attribute_group i8k_group = {
744 .attrs = i8k_attrs,
745 .is_visible = i8k_is_visible,
746};
747__ATTRIBUTE_GROUPS(i8k);
748
Jean Delvare949a9d72011-05-25 20:43:33 +0200749static int __init i8k_init_hwmon(void)
750{
751 int err;
752
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800753 i8k_hwmon_flags = 0;
754
Pali Rohár672af222015-01-12 14:31:58 +0100755 /* CPU temperature attributes, if temperature type is OK */
756 err = i8k_get_temp_type(0);
757 if (err >= 0)
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800758 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800759 /* check for additional temperature sensors */
Pali Rohár672af222015-01-12 14:31:58 +0100760 err = i8k_get_temp_type(1);
761 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800762 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
Pali Rohár672af222015-01-12 14:31:58 +0100763 err = i8k_get_temp_type(2);
764 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800765 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
Pali Rohár672af222015-01-12 14:31:58 +0100766 err = i8k_get_temp_type(3);
767 if (err >= 0)
Guenter Roeckd83c39d2013-12-14 09:30:11 -0800768 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800769
Pali Rohárf989e552015-01-12 14:32:05 +0100770 /* First fan attributes, if fan type is OK */
771 err = i8k_get_fan_type(0);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800772 if (err >= 0)
773 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
774
Pali Rohárf989e552015-01-12 14:32:05 +0100775 /* Second fan attributes, if fan type is OK */
776 err = i8k_get_fan_type(1);
Guenter Roeck82ba1d32013-12-14 09:30:10 -0800777 if (err >= 0)
778 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
779
Gabriele Mazzotta9026cae2015-06-27 15:22:05 +0200780 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm",
Pali Rohár039ae582015-05-14 13:16:37 +0200781 NULL, i8k_groups);
Jean Delvare949a9d72011-05-25 20:43:33 +0200782 if (IS_ERR(i8k_hwmon_dev)) {
783 err = PTR_ERR(i8k_hwmon_dev);
784 i8k_hwmon_dev = NULL;
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800785 pr_err("hwmon registration failed (%d)\n", err);
Jean Delvare949a9d72011-05-25 20:43:33 +0200786 return err;
787 }
Jean Delvare949a9d72011-05-25 20:43:33 +0200788 return 0;
Jean Delvare949a9d72011-05-25 20:43:33 +0200789}
790
Guenter Roeck81474fc2014-06-21 08:08:10 -0700791struct i8k_config_data {
Pali Rohár7f69fb02015-01-12 14:32:02 +0100792 uint fan_mult;
793 uint fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -0700794};
795
796enum i8k_configs {
Guenter Roeck7b883442014-06-21 08:08:12 -0700797 DELL_LATITUDE_D520,
798 DELL_PRECISION_490,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700799 DELL_STUDIO,
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100800 DELL_XPS,
Guenter Roeck81474fc2014-06-21 08:08:10 -0700801};
802
803static const struct i8k_config_data i8k_config_data[] = {
Guenter Roeck7b883442014-06-21 08:08:12 -0700804 [DELL_LATITUDE_D520] = {
805 .fan_mult = 1,
806 .fan_max = I8K_FAN_TURBO,
807 },
808 [DELL_PRECISION_490] = {
809 .fan_mult = 1,
810 .fan_max = I8K_FAN_TURBO,
811 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700812 [DELL_STUDIO] = {
813 .fan_mult = 1,
814 .fan_max = I8K_FAN_HIGH,
815 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100816 [DELL_XPS] = {
Guenter Roeck81474fc2014-06-21 08:08:10 -0700817 .fan_mult = 1,
818 .fan_max = I8K_FAN_HIGH,
819 },
820};
821
Guenter Roeck12186f52013-12-14 09:30:09 -0800822static struct dmi_system_id i8k_dmi_table[] __initdata = {
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700823 {
824 .ident = "Dell Inspiron",
825 .matches = {
826 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
827 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
828 },
829 },
830 {
831 .ident = "Dell Latitude",
832 .matches = {
833 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
834 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
835 },
836 },
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700837 {
838 .ident = "Dell Inspiron 2",
839 .matches = {
840 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
841 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
842 },
843 },
844 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700845 .ident = "Dell Latitude D520",
846 .matches = {
847 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
848 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
849 },
850 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
851 },
852 {
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700853 .ident = "Dell Latitude 2",
854 .matches = {
855 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
856 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
857 },
858 },
Nick Warnea9000d02008-02-06 01:37:47 -0800859 { /* UK Inspiron 6400 */
860 .ident = "Dell Inspiron 3",
861 .matches = {
862 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
863 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
864 },
865 },
Frank Sorenson48103c52008-02-07 00:16:31 -0800866 {
867 .ident = "Dell Inspiron 3",
868 .matches = {
869 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
870 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
871 },
872 },
Andy Spencer7ab21a82009-01-02 16:19:13 +0000873 {
Guenter Roeck7b883442014-06-21 08:08:12 -0700874 .ident = "Dell Precision 490",
875 .matches = {
876 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
877 DMI_MATCH(DMI_PRODUCT_NAME,
878 "Precision WorkStation 490"),
879 },
880 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
881 },
882 {
Andy Spencer7ab21a82009-01-02 16:19:13 +0000883 .ident = "Dell Precision",
884 .matches = {
885 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
886 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
887 },
888 },
Federico Heinzbef2a502009-01-02 16:19:23 +0000889 {
890 .ident = "Dell Vostro",
891 .matches = {
892 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
893 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
894 },
895 },
Alan Cox9aa5b012013-12-03 13:56:56 -0800896 {
897 .ident = "Dell XPS421",
898 .matches = {
899 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
900 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
901 },
902 },
Guenter Roeckff457bd2013-12-14 09:30:17 -0800903 {
904 .ident = "Dell Studio",
905 .matches = {
906 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
907 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
908 },
Guenter Roeck81474fc2014-06-21 08:08:10 -0700909 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
Guenter Roeckff457bd2013-12-14 09:30:17 -0800910 },
Guenter Roeck919a0302013-12-14 09:30:18 -0800911 {
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100912 .ident = "Dell XPS 13",
913 .matches = {
914 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
915 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
916 },
917 .driver_data = (void *)&i8k_config_data[DELL_XPS],
918 },
919 {
Guenter Roeck919a0302013-12-14 09:30:18 -0800920 .ident = "Dell XPS M140",
921 .matches = {
922 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
923 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
924 },
Guenter Roeck34ae40f2015-01-12 14:32:01 +0100925 .driver_data = (void *)&i8k_config_data[DELL_XPS],
Guenter Roeck919a0302013-12-14 09:30:18 -0800926 },
Guenter Roeck12186f52013-12-14 09:30:09 -0800927 { }
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700928};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Pali Rohár148b1fd2014-10-18 00:17:49 +0200930MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
931
Pali Rohára4b45b22015-07-30 20:41:57 +0200932static struct dmi_system_id i8k_blacklist_dmi_table[] __initdata = {
933 {
934 /*
Thorsten Leemhuis6220f4e2016-01-17 16:03:04 +0100935 * CPU fan speed going up and down on Dell Studio XPS 8000
936 * for unknown reasons.
937 */
938 .ident = "Dell Studio XPS 8000",
939 .matches = {
940 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
941 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
942 },
943 },
944 {
945 /*
Pali Rohára4b45b22015-07-30 20:41:57 +0200946 * CPU fan speed going up and down on Dell Studio XPS 8100
947 * for unknown reasons.
948 */
949 .ident = "Dell Studio XPS 8100",
950 .matches = {
951 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
952 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
953 },
954 },
955 { }
956};
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/*
959 * Probe for the presence of a supported laptop.
960 */
961static int __init i8k_probe(void)
962{
Guenter Roeck26d09382013-12-14 09:30:19 -0800963 const struct dmi_system_id *id;
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100964 int fan, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700967 * Get DMI information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Pali Rohára4b45b22015-07-30 20:41:57 +0200969 if (!dmi_check_system(i8k_dmi_table) ||
970 dmi_check_system(i8k_blacklist_dmi_table)) {
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700971 if (!ignore_dmi && !force)
972 return -ENODEV;
973
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800974 pr_info("not running on a supported Dell system.\n");
975 pr_info("vendor=%s, model=%s, version=%s\n",
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700976 i8k_get_dmi_data(DMI_SYS_VENDOR),
977 i8k_get_dmi_data(DMI_PRODUCT_NAME),
978 i8k_get_dmi_data(DMI_BIOS_VERSION));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700980
Guenter Roeck12186f52013-12-14 09:30:09 -0800981 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
982 sizeof(bios_version));
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -0700985 * Get SMM Dell signature
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 */
Dmitry Torokhov7e0fa312005-06-25 14:54:28 -0700987 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
988 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
Guenter Roeck60e71aa2013-12-14 09:30:08 -0800989 pr_err("unable to get SMM Dell signature\n");
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700990 if (!force)
991 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Pali Rohár8f21d8e2015-01-12 14:32:03 +0100994 /*
995 * Set fan multiplier and maximal fan speed from dmi config
996 * Values specified in module parameters override values from dmi
997 */
Guenter Roeck26d09382013-12-14 09:30:19 -0800998 id = dmi_first_match(i8k_dmi_table);
Guenter Roeck81474fc2014-06-21 08:08:10 -0700999 if (id && id->driver_data) {
1000 const struct i8k_config_data *conf = id->driver_data;
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001001 if (!fan_mult && conf->fan_mult)
1002 fan_mult = conf->fan_mult;
1003 if (!fan_max && conf->fan_max)
1004 fan_max = conf->fan_max;
Guenter Roeck81474fc2014-06-21 08:08:10 -07001005 }
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001006
1007 i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
Guenter Roeck81474fc2014-06-21 08:08:10 -07001008 i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
Guenter Roeck26d09382013-12-14 09:30:19 -08001009
Pali Rohár8f21d8e2015-01-12 14:32:03 +01001010 if (!fan_mult) {
1011 /*
1012 * Autodetect fan multiplier based on nominal rpm
1013 * If fan reports rpm value too high then set multiplier to 1
1014 */
1015 for (fan = 0; fan < 2; ++fan) {
1016 ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
1017 if (ret < 0)
1018 continue;
1019 if (ret > I8K_FAN_MAX_RPM)
1020 i8k_fan_mult = 1;
1021 break;
1022 }
1023 } else {
1024 /* Fan multiplier was specified in module param or in dmi */
1025 i8k_fan_mult = fan_mult;
1026 }
1027
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001031static int __init i8k_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Jean Delvare949a9d72011-05-25 20:43:33 +02001033 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001035 /* Are we running on an supported laptop? */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -07001036 if (i8k_probe())
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001037 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Jean Delvare949a9d72011-05-25 20:43:33 +02001039 err = i8k_init_hwmon();
1040 if (err)
Pali Rohár039ae582015-05-14 13:16:37 +02001041 return err;
Jean Delvare949a9d72011-05-25 20:43:33 +02001042
Pali Rohár039ae582015-05-14 13:16:37 +02001043 i8k_init_procfs();
Dmitry Torokhovdec63ec2005-06-25 14:54:23 -07001044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001047static void __exit i8k_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Guenter Roeck82ba1d32013-12-14 09:30:10 -08001049 hwmon_device_unregister(i8k_hwmon_dev);
Pali Rohár039ae582015-05-14 13:16:37 +02001050 i8k_exit_procfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Dmitry Torokhov8378b9242005-06-25 14:54:27 -07001053module_init(i8k_init);
1054module_exit(i8k_exit);