libceph: move r_reply_op_{len,result} into struct ceph_osd_req_op
[linux-2.6-block.git] / drivers / hwmon / dell-smm-hwmon.c
CommitLineData
1da177e4 1/*
a5afba16 2 * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
1da177e4
LT
3 *
4 * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org>
5 *
949a9d70 6 * Hwmon integration:
7c81c60f 7 * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de>
564132d9 8 * Copyright (C) 2013, 2014 Guenter Roeck <linux@roeck-us.net>
a5afba16 9 * Copyright (C) 2014, 2015 Pali Rohár <pali.rohar@gmail.com>
949a9d70 10 *
1da177e4
LT
11 * 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
60e71aaf
GR
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
564132d9 24#include <linux/delay.h>
1da177e4
LT
25#include <linux/module.h>
26#include <linux/types.h>
27#include <linux/init.h>
28#include <linux/proc_fs.h>
352f8f8b 29#include <linux/seq_file.h>
e70c9d5e 30#include <linux/dmi.h>
7e80d0d0 31#include <linux/capability.h>
613655fa 32#include <linux/mutex.h>
949a9d70
JD
33#include <linux/hwmon.h>
34#include <linux/hwmon-sysfs.h>
12186f51
GR
35#include <linux/uaccess.h>
36#include <linux/io.h>
f36fdb9f 37#include <linux/sched.h>
1da177e4
LT
38
39#include <linux/i8k.h>
40
1da177e4
LT
41#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
f989e554 46#define I8K_SMM_GET_FAN_TYPE 0x03a3
8f21d8e9 47#define I8K_SMM_GET_NOM_SPEED 0x04a3
1da177e4 48#define I8K_SMM_GET_TEMP 0x10a3
5114b474 49#define I8K_SMM_GET_TEMP_TYPE 0x11a3
7e0fa31d
DT
50#define I8K_SMM_GET_DELL_SIG1 0xfea3
51#define I8K_SMM_GET_DELL_SIG2 0xffa3
1da177e4
LT
52
53#define I8K_FAN_MULT 30
8f21d8e9 54#define I8K_FAN_MAX_RPM 30000
1da177e4
LT
55#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
613655fa 67static DEFINE_MUTEX(i8k_mutex);
e70c9d5e 68static char bios_version[4];
949a9d70 69static struct device *i8k_hwmon_dev;
82ba1d3f 70static u32 i8k_hwmon_flags;
8f21d8e9 71static uint i8k_fan_mult = I8K_FAN_MULT;
7f69fb03
PR
72static uint i8k_pwm_mult;
73static uint i8k_fan_max = I8K_FAN_HIGH;
82ba1d3f
GR
74
75#define I8K_HWMON_HAVE_TEMP1 (1 << 0)
d83c39de
GR
76#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)
1da177e4
LT
81
82MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
a5afba16 83MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
039ae585 84MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
1da177e4 85MODULE_LICENSE("GPL");
a5afba16 86MODULE_ALIAS("i8k");
1da177e4 87
90ab5ee9 88static bool force;
1da177e4
LT
89module_param(force, bool, 0);
90MODULE_PARM_DESC(force, "Force loading without checking for supported models");
91
90ab5ee9 92static bool ignore_dmi;
e70c9d5e
DT
93module_param(ignore_dmi, bool, 0);
94MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
95
039ae585 96#if IS_ENABLED(CONFIG_I8K)
90ab5ee9 97static bool restricted;
1da177e4
LT
98module_param(restricted, bool, 0);
99MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
100
90ab5ee9 101static bool power_status;
1da177e4
LT
102module_param(power_status, bool, 0600);
103MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
039ae585 104#endif
1da177e4 105
8f21d8e9 106static uint fan_mult;
7f69fb03 107module_param(fan_mult, uint, 0);
8f21d8e9 108MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
4ed99a27 109
8f21d8e9 110static uint fan_max;
7f69fb03 111module_param(fan_max, uint, 0);
8f21d8e9 112MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
81474fc2 113
8378b924 114struct smm_regs {
dec63ec3 115 unsigned int eax;
12186f51
GR
116 unsigned int ebx __packed;
117 unsigned int ecx __packed;
118 unsigned int edx __packed;
119 unsigned int esi __packed;
120 unsigned int edi __packed;
8378b924 121};
1da177e4 122
1855256c 123static inline const char *i8k_get_dmi_data(int field)
e70c9d5e 124{
1855256c 125 const char *dmi_data = dmi_get_system_info(field);
4f005551
DT
126
127 return dmi_data && *dmi_data ? dmi_data : "?";
e70c9d5e 128}
1da177e4
LT
129
130/*
131 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
132 */
8378b924 133static int i8k_smm(struct smm_regs *regs)
1da177e4 134{
dec63ec3
DT
135 int rc;
136 int eax = regs->eax;
f36fdb9f
GR
137 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);
6d827fbc
GR
143 rc = set_cpus_allowed_ptr(current, cpumask_of(0));
144 if (rc)
145 goto out;
f36fdb9f
GR
146 if (smp_processor_id() != 0) {
147 rc = -EBUSY;
148 goto out;
149 }
dec63ec3 150
fe04f22f 151#if defined(CONFIG_X86_64)
22d3243d 152 asm volatile("pushq %%rax\n\t"
fe04f22f
BS
153 "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"
bc1f419c
LT
171 "pushfq\n\t"
172 "popq %%rax\n\t"
fe04f22f 173 "andl $1,%%eax\n"
12186f51 174 : "=a"(rc)
fe04f22f
BS
175 : "a"(regs)
176 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
177#else
22d3243d 178 asm volatile("pushl %%eax\n\t"
dec63ec3
DT
179 "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"
6b4e81db 199 "andl $1,%%eax\n"
12186f51 200 : "=a"(rc)
dec63ec3
DT
201 : "a"(regs)
202 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
fe04f22f 203#endif
8378b924 204 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
f36fdb9f 205 rc = -EINVAL;
dec63ec3 206
f36fdb9f
GR
207out:
208 set_cpus_allowed_ptr(current, old_mask);
209 free_cpumask_var(old_mask);
210 return rc;
1da177e4
LT
211}
212
1da177e4
LT
213/*
214 * Read the fan status.
215 */
216static int i8k_get_fan_status(int fan)
217{
8378b924 218 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
1da177e4 219
dec63ec3 220 regs.ebx = fan & 0xff;
8378b924 221 return i8k_smm(&regs) ? : regs.eax & 0xff;
1da177e4
LT
222}
223
224/*
225 * Read the fan speed in RPM.
226 */
227static int i8k_get_fan_speed(int fan)
228{
8378b924 229 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
1da177e4 230
dec63ec3 231 regs.ebx = fan & 0xff;
26d09382 232 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
1da177e4
LT
233}
234
f989e554
PR
235/*
236 * 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
8f21d8e9
PR
246/*
247 * 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
1da177e4
LT
257/*
258 * Set the fan speed (off, low, high). Returns the new fan status.
259 */
260static int i8k_set_fan(int fan, int speed)
261{
8378b924 262 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
1da177e4 263
81474fc2 264 speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
dec63ec3 265 regs.ebx = (fan & 0xff) | (speed << 8);
1da177e4 266
8378b924 267 return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
1da177e4
LT
268}
269
5114b474
PR
270static 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
1da177e4
LT
278/*
279 * Read the cpu temperature.
280 */
564132d9 281static int _i8k_get_temp(int sensor)
1da177e4 282{
564132d9
GR
283 struct smm_regs regs = {
284 .eax = I8K_SMM_GET_TEMP,
285 .ebx = sensor & 0xff,
286 };
1da177e4 287
564132d9
GR
288 return i8k_smm(&regs) ? : regs.eax & 0xff;
289}
8378b924 290
564132d9
GR
291static int i8k_get_temp(int sensor)
292{
293 int temp = _i8k_get_temp(sensor);
1da177e4 294
dec63ec3
DT
295 /*
296 * Sometimes the temperature sensor returns 0x99, which is out of range.
564132d9 297 * In this case we retry (once) before returning an error.
dec63ec3
DT
298 # 1003655137 00000058 00005a4b
299 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
300 # 1003655139 00000054 00005c52
301 */
564132d9
GR
302 if (temp == 0x99) {
303 msleep(100);
304 temp = _i8k_get_temp(sensor);
dec63ec3 305 }
564132d9
GR
306 /*
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 */
723493ca 314 if (temp > I8K_MAX_TEMP)
83d514d7 315 return -ENODATA;
1da177e4 316
dec63ec3 317 return temp;
1da177e4
LT
318}
319
7e0fa31d 320static int i8k_get_dell_signature(int req_fn)
1da177e4 321{
7e0fa31d 322 struct smm_regs regs = { .eax = req_fn, };
dec63ec3 323 int rc;
1da177e4 324
12186f51
GR
325 rc = i8k_smm(&regs);
326 if (rc < 0)
dec63ec3 327 return rc;
1da177e4 328
8378b924 329 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
1da177e4
LT
330}
331
039ae585
PR
332#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
d79b6f4d
FW
377static int
378i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
1da177e4 379{
e70c9d5e 380 int val = 0;
dec63ec3
DT
381 int speed;
382 unsigned char buff[16];
383 int __user *argp = (int __user *)arg;
384
385 if (!argp)
386 return -EINVAL;
387
388 switch (cmd) {
389 case I8K_BIOS_VERSION:
e551b152
GR
390 val = (bios_version[0] << 16) |
391 (bios_version[1] << 8) | bios_version[2];
dec63ec3
DT
392 break;
393
394 case I8K_MACHINE_ID:
395 memset(buff, 0, 16);
12186f51
GR
396 strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
397 sizeof(buff));
dec63ec3
DT
398 break;
399
400 case I8K_FN_STATUS:
401 val = i8k_get_fn_status();
402 break;
403
404 case I8K_POWER_STATUS:
405 val = i8k_get_power_status();
406 break;
407
408 case I8K_GET_TEMP:
7e0fa31d 409 val = i8k_get_temp(0);
dec63ec3
DT
410 break;
411
412 case I8K_GET_SPEED:
8378b924 413 if (copy_from_user(&val, argp, sizeof(int)))
dec63ec3 414 return -EFAULT;
8378b924 415
dec63ec3
DT
416 val = i8k_get_fan_speed(val);
417 break;
1da177e4 418
dec63ec3 419 case I8K_GET_FAN:
8378b924 420 if (copy_from_user(&val, argp, sizeof(int)))
dec63ec3 421 return -EFAULT;
8378b924 422
dec63ec3
DT
423 val = i8k_get_fan_status(val);
424 break;
1da177e4 425
dec63ec3 426 case I8K_SET_FAN:
8378b924 427 if (restricted && !capable(CAP_SYS_ADMIN))
dec63ec3 428 return -EPERM;
8378b924
DT
429
430 if (copy_from_user(&val, argp, sizeof(int)))
dec63ec3 431 return -EFAULT;
8378b924
DT
432
433 if (copy_from_user(&speed, argp + 1, sizeof(int)))
dec63ec3 434 return -EFAULT;
8378b924 435
dec63ec3
DT
436 val = i8k_set_fan(val, speed);
437 break;
1da177e4 438
dec63ec3
DT
439 default:
440 return -EINVAL;
1da177e4 441 }
1da177e4 442
8378b924 443 if (val < 0)
dec63ec3 444 return val;
1da177e4 445
dec63ec3
DT
446 switch (cmd) {
447 case I8K_BIOS_VERSION:
8378b924 448 if (copy_to_user(argp, &val, 4))
dec63ec3 449 return -EFAULT;
8378b924 450
dec63ec3
DT
451 break;
452 case I8K_MACHINE_ID:
8378b924 453 if (copy_to_user(argp, buff, 16))
dec63ec3 454 return -EFAULT;
8378b924 455
dec63ec3
DT
456 break;
457 default:
8378b924 458 if (copy_to_user(argp, &val, sizeof(int)))
dec63ec3 459 return -EFAULT;
8378b924 460
dec63ec3 461 break;
1da177e4 462 }
1da177e4 463
dec63ec3 464 return 0;
1da177e4
LT
465}
466
d79b6f4d
FW
467static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
468{
469 long ret;
470
613655fa 471 mutex_lock(&i8k_mutex);
d79b6f4d 472 ret = i8k_ioctl_unlocked(fp, cmd, arg);
613655fa 473 mutex_unlock(&i8k_mutex);
d79b6f4d
FW
474
475 return ret;
476}
477
1da177e4
LT
478/*
479 * Print the information for /proc/i8k.
480 */
352f8f8b 481static int i8k_proc_show(struct seq_file *seq, void *offset)
1da177e4 482{
352f8f8b 483 int fn_key, cpu_temp, ac_power;
dec63ec3
DT
484 int left_fan, right_fan, left_speed, right_speed;
485
96de0e25
JE
486 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 */
8378b924 492 if (power_status)
96de0e25 493 ac_power = i8k_get_power_status(); /* 14700 µs */
8378b924 494 else
dec63ec3 495 ac_power = -1;
dec63ec3
DT
496
497 /*
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 */
3a267d3b
JP
511 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;
1da177e4
LT
520}
521
352f8f8b 522static int i8k_open_fs(struct inode *inode, struct file *file)
1da177e4 523{
352f8f8b 524 return single_open(file, i8k_proc_show, NULL);
1da177e4
LT
525}
526
039ae585
PR
527static 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
949a9d70
JD
558
559/*
560 * Hwmon interface
561 */
562
5114b474
PR
563static 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
949a9d70
JD
586static ssize_t i8k_hwmon_show_temp(struct device *dev,
587 struct device_attribute *devattr,
588 char *buf)
589{
d83c39de
GR
590 int index = to_sensor_dev_attr(devattr)->index;
591 int temp;
949a9d70 592
d83c39de
GR
593 temp = i8k_get_temp(index);
594 if (temp < 0)
595 return temp;
596 return sprintf(buf, "%d\n", temp * 1000);
949a9d70
JD
597}
598
f989e554
PR
599static 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
949a9d70
JD
630static 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
e7f5f275
GR
643static 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;
81474fc2 653 return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
e7f5f275
GR
654}
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;
81474fc2 667 val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
e7f5f275
GR
668
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
d83c39de 676static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
5114b474
PR
677static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
678 0);
d83c39de 679static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
5114b474
PR
680static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
681 1);
d83c39de 682static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
5114b474
PR
683static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
684 2);
d83c39de 685static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
5114b474
PR
686static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
687 3);
f989e554
PR
688static 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);
e7f5f275 691static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
f989e554 692 i8k_hwmon_set_pwm, 0);
949a9d70 693static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
f989e554
PR
694 1);
695static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
696 1);
e7f5f275 697static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
f989e554 698 i8k_hwmon_set_pwm, 1);
82ba1d3f
GR
699
700static struct attribute *i8k_attrs[] = {
d83c39de 701 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
5114b474
PR
702 &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 */
f989e554
PR
710 &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 */
82ba1d3f
GR
715 NULL
716};
949a9d70 717
82ba1d3f
GR
718static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
719 int index)
949a9d70 720{
5114b474
PR
721 if (index >= 0 && index <= 1 &&
722 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
82ba1d3f 723 return 0;
5114b474
PR
724 if (index >= 2 && index <= 3 &&
725 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
d83c39de 726 return 0;
5114b474
PR
727 if (index >= 4 && index <= 5 &&
728 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
d83c39de 729 return 0;
5114b474
PR
730 if (index >= 6 && index <= 7 &&
731 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
d83c39de 732 return 0;
f989e554 733 if (index >= 8 && index <= 10 &&
82ba1d3f
GR
734 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
735 return 0;
f989e554 736 if (index >= 11 && index <= 13 &&
82ba1d3f
GR
737 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
738 return 0;
739
740 return attr->mode;
949a9d70
JD
741}
742
82ba1d3f
GR
743static const struct attribute_group i8k_group = {
744 .attrs = i8k_attrs,
745 .is_visible = i8k_is_visible,
746};
747__ATTRIBUTE_GROUPS(i8k);
748
949a9d70
JD
749static int __init i8k_init_hwmon(void)
750{
751 int err;
752
82ba1d3f 753 i8k_hwmon_flags = 0;
949a9d70 754
672af223
PR
755 /* CPU temperature attributes, if temperature type is OK */
756 err = i8k_get_temp_type(0);
757 if (err >= 0)
82ba1d3f 758 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
d83c39de 759 /* check for additional temperature sensors */
672af223
PR
760 err = i8k_get_temp_type(1);
761 if (err >= 0)
d83c39de 762 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
672af223
PR
763 err = i8k_get_temp_type(2);
764 if (err >= 0)
d83c39de 765 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
672af223
PR
766 err = i8k_get_temp_type(3);
767 if (err >= 0)
d83c39de 768 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
949a9d70 769
f989e554
PR
770 /* First fan attributes, if fan type is OK */
771 err = i8k_get_fan_type(0);
82ba1d3f
GR
772 if (err >= 0)
773 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
949a9d70 774
f989e554
PR
775 /* Second fan attributes, if fan type is OK */
776 err = i8k_get_fan_type(1);
82ba1d3f
GR
777 if (err >= 0)
778 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
949a9d70 779
9026cae1 780 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm",
039ae585 781 NULL, i8k_groups);
82ba1d3f
GR
782 if (IS_ERR(i8k_hwmon_dev)) {
783 err = PTR_ERR(i8k_hwmon_dev);
784 i8k_hwmon_dev = NULL;
785 pr_err("hwmon registration failed (%d)\n", err);
786 return err;
787 }
949a9d70 788 return 0;
949a9d70
JD
789}
790
81474fc2 791struct i8k_config_data {
7f69fb03
PR
792 uint fan_mult;
793 uint fan_max;
81474fc2
GR
794};
795
796enum i8k_configs {
7b883446
GR
797 DELL_LATITUDE_D520,
798 DELL_PRECISION_490,
81474fc2 799 DELL_STUDIO,
34ae40f6 800 DELL_XPS,
81474fc2
GR
801};
802
803static const struct i8k_config_data i8k_config_data[] = {
7b883446
GR
804 [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 },
81474fc2
GR
812 [DELL_STUDIO] = {
813 .fan_mult = 1,
814 .fan_max = I8K_FAN_HIGH,
815 },
34ae40f6 816 [DELL_XPS] = {
81474fc2
GR
817 .fan_mult = 1,
818 .fan_max = I8K_FAN_HIGH,
819 },
820};
821
12186f51 822static struct dmi_system_id i8k_dmi_table[] __initdata = {
e70c9d5e
DT
823 {
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 },
7e0fa31d
DT
837 {
838 .ident = "Dell Inspiron 2",
839 .matches = {
840 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
841 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
842 },
843 },
7b883446
GR
844 {
845 .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 },
7e0fa31d
DT
852 {
853 .ident = "Dell Latitude 2",
854 .matches = {
855 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
856 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
857 },
858 },
a9000d03
NW
859 { /* 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 },
48103c52
FS
866 {
867 .ident = "Dell Inspiron 3",
868 .matches = {
869 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
870 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
871 },
872 },
7b883446
GR
873 {
874 .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 },
7ab21a86
AS
882 {
883 .ident = "Dell Precision",
884 .matches = {
885 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
886 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
887 },
888 },
bef2a508
FH
889 {
890 .ident = "Dell Vostro",
891 .matches = {
892 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
893 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
894 },
895 },
9aa5b018
AC
896 {
897 .ident = "Dell XPS421",
898 .matches = {
899 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
900 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
901 },
902 },
ff457bde
GR
903 {
904 .ident = "Dell Studio",
905 .matches = {
906 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
907 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
908 },
81474fc2 909 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
ff457bde 910 },
34ae40f6
GR
911 {
912 .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 },
919a0304
GR
919 {
920 .ident = "Dell XPS M140",
921 .matches = {
922 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
923 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
924 },
34ae40f6 925 .driver_data = (void *)&i8k_config_data[DELL_XPS],
919a0304 926 },
12186f51 927 { }
e70c9d5e 928};
1da177e4 929
148b1fda
PR
930MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
931
a4b45b25 932static struct dmi_system_id i8k_blacklist_dmi_table[] __initdata = {
6220f4eb
TL
933 {
934 /*
935 * 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 },
a4b45b25
PR
944 {
945 /*
946 * 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
1da177e4
LT
958/*
959 * Probe for the presence of a supported laptop.
960 */
961static int __init i8k_probe(void)
962{
26d09382 963 const struct dmi_system_id *id;
8f21d8e9 964 int fan, ret;
dec63ec3 965
1da177e4 966 /*
dec63ec3 967 * Get DMI information
1da177e4 968 */
a4b45b25
PR
969 if (!dmi_check_system(i8k_dmi_table) ||
970 dmi_check_system(i8k_blacklist_dmi_table)) {
e70c9d5e
DT
971 if (!ignore_dmi && !force)
972 return -ENODEV;
973
60e71aaf
GR
974 pr_info("not running on a supported Dell system.\n");
975 pr_info("vendor=%s, model=%s, version=%s\n",
e70c9d5e
DT
976 i8k_get_dmi_data(DMI_SYS_VENDOR),
977 i8k_get_dmi_data(DMI_PRODUCT_NAME),
978 i8k_get_dmi_data(DMI_BIOS_VERSION));
1da177e4 979 }
dec63ec3 980
12186f51
GR
981 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
982 sizeof(bios_version));
e70c9d5e 983
1da177e4 984 /*
dec63ec3 985 * Get SMM Dell signature
1da177e4 986 */
7e0fa31d
DT
987 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
988 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
60e71aaf 989 pr_err("unable to get SMM Dell signature\n");
e70c9d5e
DT
990 if (!force)
991 return -ENODEV;
1da177e4 992 }
1da177e4 993
8f21d8e9
PR
994 /*
995 * Set fan multiplier and maximal fan speed from dmi config
996 * Values specified in module parameters override values from dmi
997 */
26d09382 998 id = dmi_first_match(i8k_dmi_table);
81474fc2
GR
999 if (id && id->driver_data) {
1000 const struct i8k_config_data *conf = id->driver_data;
8f21d8e9
PR
1001 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;
81474fc2 1005 }
8f21d8e9
PR
1006
1007 i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
81474fc2 1008 i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
26d09382 1009
8f21d8e9
PR
1010 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
dec63ec3 1028 return 0;
1da177e4
LT
1029}
1030
8378b924 1031static int __init i8k_init(void)
1da177e4 1032{
949a9d70 1033 int err;
dec63ec3
DT
1034
1035 /* Are we running on an supported laptop? */
e70c9d5e 1036 if (i8k_probe())
dec63ec3 1037 return -ENODEV;
dec63ec3 1038
949a9d70
JD
1039 err = i8k_init_hwmon();
1040 if (err)
039ae585 1041 return err;
949a9d70 1042
039ae585 1043 i8k_init_procfs();
dec63ec3 1044 return 0;
1da177e4
LT
1045}
1046
8378b924 1047static void __exit i8k_exit(void)
1da177e4 1048{
82ba1d3f 1049 hwmon_device_unregister(i8k_hwmon_dev);
039ae585 1050 i8k_exit_procfs();
1da177e4 1051}
1da177e4 1052
8378b924
DT
1053module_init(i8k_init);
1054module_exit(i8k_exit);