dell-smm-hwmon.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
  3. *
  4. * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org>
  5. *
  6. * Hwmon integration:
  7. * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de>
  8. * Copyright (C) 2013, 2014 Guenter Roeck <linux@roeck-us.net>
  9. * Copyright (C) 2014, 2015 Pali Rohár <pali.rohar@gmail.com>
  10. *
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/delay.h>
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/init.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/dmi.h>
  29. #include <linux/capability.h>
  30. #include <linux/mutex.h>
  31. #include <linux/hwmon.h>
  32. #include <linux/hwmon-sysfs.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/io.h>
  35. #include <linux/sched.h>
  36. #include <linux/i8k.h>
  37. #define I8K_SMM_FN_STATUS 0x0025
  38. #define I8K_SMM_POWER_STATUS 0x0069
  39. #define I8K_SMM_SET_FAN 0x01a3
  40. #define I8K_SMM_GET_FAN 0x00a3
  41. #define I8K_SMM_GET_SPEED 0x02a3
  42. #define I8K_SMM_GET_FAN_TYPE 0x03a3
  43. #define I8K_SMM_GET_NOM_SPEED 0x04a3
  44. #define I8K_SMM_GET_TEMP 0x10a3
  45. #define I8K_SMM_GET_TEMP_TYPE 0x11a3
  46. #define I8K_SMM_GET_DELL_SIG1 0xfea3
  47. #define I8K_SMM_GET_DELL_SIG2 0xffa3
  48. #define I8K_FAN_MULT 30
  49. #define I8K_FAN_MAX_RPM 30000
  50. #define I8K_MAX_TEMP 127
  51. #define I8K_FN_NONE 0x00
  52. #define I8K_FN_UP 0x01
  53. #define I8K_FN_DOWN 0x02
  54. #define I8K_FN_MUTE 0x04
  55. #define I8K_FN_MASK 0x07
  56. #define I8K_FN_SHIFT 8
  57. #define I8K_POWER_AC 0x05
  58. #define I8K_POWER_BATTERY 0x01
  59. static DEFINE_MUTEX(i8k_mutex);
  60. static char bios_version[4];
  61. static struct device *i8k_hwmon_dev;
  62. static u32 i8k_hwmon_flags;
  63. static uint i8k_fan_mult = I8K_FAN_MULT;
  64. static uint i8k_pwm_mult;
  65. static uint i8k_fan_max = I8K_FAN_HIGH;
  66. #define I8K_HWMON_HAVE_TEMP1 (1 << 0)
  67. #define I8K_HWMON_HAVE_TEMP2 (1 << 1)
  68. #define I8K_HWMON_HAVE_TEMP3 (1 << 2)
  69. #define I8K_HWMON_HAVE_TEMP4 (1 << 3)
  70. #define I8K_HWMON_HAVE_FAN1 (1 << 4)
  71. #define I8K_HWMON_HAVE_FAN2 (1 << 5)
  72. MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
  73. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  74. MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
  75. MODULE_LICENSE("GPL");
  76. MODULE_ALIAS("i8k");
  77. static bool force;
  78. module_param(force, bool, 0);
  79. MODULE_PARM_DESC(force, "Force loading without checking for supported models");
  80. static bool ignore_dmi;
  81. module_param(ignore_dmi, bool, 0);
  82. MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
  83. #if IS_ENABLED(CONFIG_I8K)
  84. static bool restricted;
  85. module_param(restricted, bool, 0);
  86. MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
  87. static bool power_status;
  88. module_param(power_status, bool, 0600);
  89. MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
  90. #endif
  91. static uint fan_mult;
  92. module_param(fan_mult, uint, 0);
  93. MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
  94. static uint fan_max;
  95. module_param(fan_max, uint, 0);
  96. MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
  97. struct smm_regs {
  98. unsigned int eax;
  99. unsigned int ebx __packed;
  100. unsigned int ecx __packed;
  101. unsigned int edx __packed;
  102. unsigned int esi __packed;
  103. unsigned int edi __packed;
  104. };
  105. static inline const char *i8k_get_dmi_data(int field)
  106. {
  107. const char *dmi_data = dmi_get_system_info(field);
  108. return dmi_data && *dmi_data ? dmi_data : "?";
  109. }
  110. /*
  111. * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
  112. */
  113. static int i8k_smm(struct smm_regs *regs)
  114. {
  115. int rc;
  116. int eax = regs->eax;
  117. cpumask_var_t old_mask;
  118. /* SMM requires CPU 0 */
  119. if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
  120. return -ENOMEM;
  121. cpumask_copy(old_mask, &current->cpus_allowed);
  122. rc = set_cpus_allowed_ptr(current, cpumask_of(0));
  123. if (rc)
  124. goto out;
  125. if (smp_processor_id() != 0) {
  126. rc = -EBUSY;
  127. goto out;
  128. }
  129. #if defined(CONFIG_X86_64)
  130. asm volatile("pushq %%rax\n\t"
  131. "movl 0(%%rax),%%edx\n\t"
  132. "pushq %%rdx\n\t"
  133. "movl 4(%%rax),%%ebx\n\t"
  134. "movl 8(%%rax),%%ecx\n\t"
  135. "movl 12(%%rax),%%edx\n\t"
  136. "movl 16(%%rax),%%esi\n\t"
  137. "movl 20(%%rax),%%edi\n\t"
  138. "popq %%rax\n\t"
  139. "out %%al,$0xb2\n\t"
  140. "out %%al,$0x84\n\t"
  141. "xchgq %%rax,(%%rsp)\n\t"
  142. "movl %%ebx,4(%%rax)\n\t"
  143. "movl %%ecx,8(%%rax)\n\t"
  144. "movl %%edx,12(%%rax)\n\t"
  145. "movl %%esi,16(%%rax)\n\t"
  146. "movl %%edi,20(%%rax)\n\t"
  147. "popq %%rdx\n\t"
  148. "movl %%edx,0(%%rax)\n\t"
  149. "pushfq\n\t"
  150. "popq %%rax\n\t"
  151. "andl $1,%%eax\n"
  152. : "=a"(rc)
  153. : "a"(regs)
  154. : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
  155. #else
  156. asm volatile("pushl %%eax\n\t"
  157. "movl 0(%%eax),%%edx\n\t"
  158. "push %%edx\n\t"
  159. "movl 4(%%eax),%%ebx\n\t"
  160. "movl 8(%%eax),%%ecx\n\t"
  161. "movl 12(%%eax),%%edx\n\t"
  162. "movl 16(%%eax),%%esi\n\t"
  163. "movl 20(%%eax),%%edi\n\t"
  164. "popl %%eax\n\t"
  165. "out %%al,$0xb2\n\t"
  166. "out %%al,$0x84\n\t"
  167. "xchgl %%eax,(%%esp)\n\t"
  168. "movl %%ebx,4(%%eax)\n\t"
  169. "movl %%ecx,8(%%eax)\n\t"
  170. "movl %%edx,12(%%eax)\n\t"
  171. "movl %%esi,16(%%eax)\n\t"
  172. "movl %%edi,20(%%eax)\n\t"
  173. "popl %%edx\n\t"
  174. "movl %%edx,0(%%eax)\n\t"
  175. "lahf\n\t"
  176. "shrl $8,%%eax\n\t"
  177. "andl $1,%%eax\n"
  178. : "=a"(rc)
  179. : "a"(regs)
  180. : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
  181. #endif
  182. if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
  183. rc = -EINVAL;
  184. out:
  185. set_cpus_allowed_ptr(current, old_mask);
  186. free_cpumask_var(old_mask);
  187. return rc;
  188. }
  189. /*
  190. * Read the fan status.
  191. */
  192. static int i8k_get_fan_status(int fan)
  193. {
  194. struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
  195. regs.ebx = fan & 0xff;
  196. return i8k_smm(&regs) ? : regs.eax & 0xff;
  197. }
  198. /*
  199. * Read the fan speed in RPM.
  200. */
  201. static int i8k_get_fan_speed(int fan)
  202. {
  203. struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
  204. regs.ebx = fan & 0xff;
  205. return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
  206. }
  207. /*
  208. * Read the fan type.
  209. */
  210. static int i8k_get_fan_type(int fan)
  211. {
  212. struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, };
  213. regs.ebx = fan & 0xff;
  214. return i8k_smm(&regs) ? : regs.eax & 0xff;
  215. }
  216. /*
  217. * Read the fan nominal rpm for specific fan speed.
  218. */
  219. static int i8k_get_fan_nominal_speed(int fan, int speed)
  220. {
  221. struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
  222. regs.ebx = (fan & 0xff) | (speed << 8);
  223. return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
  224. }
  225. /*
  226. * Set the fan speed (off, low, high). Returns the new fan status.
  227. */
  228. static int i8k_set_fan(int fan, int speed)
  229. {
  230. struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
  231. speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
  232. regs.ebx = (fan & 0xff) | (speed << 8);
  233. return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
  234. }
  235. static int i8k_get_temp_type(int sensor)
  236. {
  237. struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
  238. regs.ebx = sensor & 0xff;
  239. return i8k_smm(&regs) ? : regs.eax & 0xff;
  240. }
  241. /*
  242. * Read the cpu temperature.
  243. */
  244. static int _i8k_get_temp(int sensor)
  245. {
  246. struct smm_regs regs = {
  247. .eax = I8K_SMM_GET_TEMP,
  248. .ebx = sensor & 0xff,
  249. };
  250. return i8k_smm(&regs) ? : regs.eax & 0xff;
  251. }
  252. static int i8k_get_temp(int sensor)
  253. {
  254. int temp = _i8k_get_temp(sensor);
  255. /*
  256. * Sometimes the temperature sensor returns 0x99, which is out of range.
  257. * In this case we retry (once) before returning an error.
  258. # 1003655137 00000058 00005a4b
  259. # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
  260. # 1003655139 00000054 00005c52
  261. */
  262. if (temp == 0x99) {
  263. msleep(100);
  264. temp = _i8k_get_temp(sensor);
  265. }
  266. /*
  267. * Return -ENODATA for all invalid temperatures.
  268. *
  269. * Known instances are the 0x99 value as seen above as well as
  270. * 0xc1 (193), which may be returned when trying to read the GPU
  271. * temperature if the system supports a GPU and it is currently
  272. * turned off.
  273. */
  274. if (temp > I8K_MAX_TEMP)
  275. return -ENODATA;
  276. return temp;
  277. }
  278. static int i8k_get_dell_signature(int req_fn)
  279. {
  280. struct smm_regs regs = { .eax = req_fn, };
  281. int rc;
  282. rc = i8k_smm(&regs);
  283. if (rc < 0)
  284. return rc;
  285. return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
  286. }
  287. #if IS_ENABLED(CONFIG_I8K)
  288. /*
  289. * Read the Fn key status.
  290. */
  291. static int i8k_get_fn_status(void)
  292. {
  293. struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
  294. int rc;
  295. rc = i8k_smm(&regs);
  296. if (rc < 0)
  297. return rc;
  298. switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
  299. case I8K_FN_UP:
  300. return I8K_VOL_UP;
  301. case I8K_FN_DOWN:
  302. return I8K_VOL_DOWN;
  303. case I8K_FN_MUTE:
  304. return I8K_VOL_MUTE;
  305. default:
  306. return 0;
  307. }
  308. }
  309. /*
  310. * Read the power status.
  311. */
  312. static int i8k_get_power_status(void)
  313. {
  314. struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
  315. int rc;
  316. rc = i8k_smm(&regs);
  317. if (rc < 0)
  318. return rc;
  319. return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
  320. }
  321. /*
  322. * Procfs interface
  323. */
  324. static int
  325. i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
  326. {
  327. int val = 0;
  328. int speed;
  329. unsigned char buff[16];
  330. int __user *argp = (int __user *)arg;
  331. if (!argp)
  332. return -EINVAL;
  333. switch (cmd) {
  334. case I8K_BIOS_VERSION:
  335. val = (bios_version[0] << 16) |
  336. (bios_version[1] << 8) | bios_version[2];
  337. break;
  338. case I8K_MACHINE_ID:
  339. memset(buff, 0, 16);
  340. strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
  341. sizeof(buff));
  342. break;
  343. case I8K_FN_STATUS:
  344. val = i8k_get_fn_status();
  345. break;
  346. case I8K_POWER_STATUS:
  347. val = i8k_get_power_status();
  348. break;
  349. case I8K_GET_TEMP:
  350. val = i8k_get_temp(0);
  351. break;
  352. case I8K_GET_SPEED:
  353. if (copy_from_user(&val, argp, sizeof(int)))
  354. return -EFAULT;
  355. val = i8k_get_fan_speed(val);
  356. break;
  357. case I8K_GET_FAN:
  358. if (copy_from_user(&val, argp, sizeof(int)))
  359. return -EFAULT;
  360. val = i8k_get_fan_status(val);
  361. break;
  362. case I8K_SET_FAN:
  363. if (restricted && !capable(CAP_SYS_ADMIN))
  364. return -EPERM;
  365. if (copy_from_user(&val, argp, sizeof(int)))
  366. return -EFAULT;
  367. if (copy_from_user(&speed, argp + 1, sizeof(int)))
  368. return -EFAULT;
  369. val = i8k_set_fan(val, speed);
  370. break;
  371. default:
  372. return -EINVAL;
  373. }
  374. if (val < 0)
  375. return val;
  376. switch (cmd) {
  377. case I8K_BIOS_VERSION:
  378. if (copy_to_user(argp, &val, 4))
  379. return -EFAULT;
  380. break;
  381. case I8K_MACHINE_ID:
  382. if (copy_to_user(argp, buff, 16))
  383. return -EFAULT;
  384. break;
  385. default:
  386. if (copy_to_user(argp, &val, sizeof(int)))
  387. return -EFAULT;
  388. break;
  389. }
  390. return 0;
  391. }
  392. static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  393. {
  394. long ret;
  395. mutex_lock(&i8k_mutex);
  396. ret = i8k_ioctl_unlocked(fp, cmd, arg);
  397. mutex_unlock(&i8k_mutex);
  398. return ret;
  399. }
  400. /*
  401. * Print the information for /proc/i8k.
  402. */
  403. static int i8k_proc_show(struct seq_file *seq, void *offset)
  404. {
  405. int fn_key, cpu_temp, ac_power;
  406. int left_fan, right_fan, left_speed, right_speed;
  407. cpu_temp = i8k_get_temp(0); /* 11100 µs */
  408. left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
  409. right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
  410. left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
  411. right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
  412. fn_key = i8k_get_fn_status(); /* 750 µs */
  413. if (power_status)
  414. ac_power = i8k_get_power_status(); /* 14700 µs */
  415. else
  416. ac_power = -1;
  417. /*
  418. * Info:
  419. *
  420. * 1) Format version (this will change if format changes)
  421. * 2) BIOS version
  422. * 3) BIOS machine ID
  423. * 4) Cpu temperature
  424. * 5) Left fan status
  425. * 6) Right fan status
  426. * 7) Left fan speed
  427. * 8) Right fan speed
  428. * 9) AC power
  429. * 10) Fn Key status
  430. */
  431. seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
  432. I8K_PROC_FMT,
  433. bios_version,
  434. i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
  435. cpu_temp,
  436. left_fan, right_fan, left_speed, right_speed,
  437. ac_power, fn_key);
  438. return 0;
  439. }
  440. static int i8k_open_fs(struct inode *inode, struct file *file)
  441. {
  442. return single_open(file, i8k_proc_show, NULL);
  443. }
  444. static const struct file_operations i8k_fops = {
  445. .owner = THIS_MODULE,
  446. .open = i8k_open_fs,
  447. .read = seq_read,
  448. .llseek = seq_lseek,
  449. .release = single_release,
  450. .unlocked_ioctl = i8k_ioctl,
  451. };
  452. static void __init i8k_init_procfs(void)
  453. {
  454. /* Register the proc entry */
  455. proc_create("i8k", 0, NULL, &i8k_fops);
  456. }
  457. static void __exit i8k_exit_procfs(void)
  458. {
  459. remove_proc_entry("i8k", NULL);
  460. }
  461. #else
  462. static inline void __init i8k_init_procfs(void)
  463. {
  464. }
  465. static inline void __exit i8k_exit_procfs(void)
  466. {
  467. }
  468. #endif
  469. /*
  470. * Hwmon interface
  471. */
  472. static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
  473. struct device_attribute *devattr,
  474. char *buf)
  475. {
  476. static const char * const labels[] = {
  477. "CPU",
  478. "GPU",
  479. "SODIMM",
  480. "Other",
  481. "Ambient",
  482. "Other",
  483. };
  484. int index = to_sensor_dev_attr(devattr)->index;
  485. int type;
  486. type = i8k_get_temp_type(index);
  487. if (type < 0)
  488. return type;
  489. if (type >= ARRAY_SIZE(labels))
  490. type = ARRAY_SIZE(labels) - 1;
  491. return sprintf(buf, "%s\n", labels[type]);
  492. }
  493. static ssize_t i8k_hwmon_show_temp(struct device *dev,
  494. struct device_attribute *devattr,
  495. char *buf)
  496. {
  497. int index = to_sensor_dev_attr(devattr)->index;
  498. int temp;
  499. temp = i8k_get_temp(index);
  500. if (temp < 0)
  501. return temp;
  502. return sprintf(buf, "%d\n", temp * 1000);
  503. }
  504. static ssize_t i8k_hwmon_show_fan_label(struct device *dev,
  505. struct device_attribute *devattr,
  506. char *buf)
  507. {
  508. static const char * const labels[] = {
  509. "Processor Fan",
  510. "Motherboard Fan",
  511. "Video Fan",
  512. "Power Supply Fan",
  513. "Chipset Fan",
  514. "Other Fan",
  515. };
  516. int index = to_sensor_dev_attr(devattr)->index;
  517. bool dock = false;
  518. int type;
  519. type = i8k_get_fan_type(index);
  520. if (type < 0)
  521. return type;
  522. if (type & 0x10) {
  523. dock = true;
  524. type &= 0x0F;
  525. }
  526. if (type >= ARRAY_SIZE(labels))
  527. type = (ARRAY_SIZE(labels) - 1);
  528. return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
  529. }
  530. static ssize_t i8k_hwmon_show_fan(struct device *dev,
  531. struct device_attribute *devattr,
  532. char *buf)
  533. {
  534. int index = to_sensor_dev_attr(devattr)->index;
  535. int fan_speed;
  536. fan_speed = i8k_get_fan_speed(index);
  537. if (fan_speed < 0)
  538. return fan_speed;
  539. return sprintf(buf, "%d\n", fan_speed);
  540. }
  541. static ssize_t i8k_hwmon_show_pwm(struct device *dev,
  542. struct device_attribute *devattr,
  543. char *buf)
  544. {
  545. int index = to_sensor_dev_attr(devattr)->index;
  546. int status;
  547. status = i8k_get_fan_status(index);
  548. if (status < 0)
  549. return -EIO;
  550. return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
  551. }
  552. static ssize_t i8k_hwmon_set_pwm(struct device *dev,
  553. struct device_attribute *attr,
  554. const char *buf, size_t count)
  555. {
  556. int index = to_sensor_dev_attr(attr)->index;
  557. unsigned long val;
  558. int err;
  559. err = kstrtoul(buf, 10, &val);
  560. if (err)
  561. return err;
  562. val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
  563. mutex_lock(&i8k_mutex);
  564. err = i8k_set_fan(index, val);
  565. mutex_unlock(&i8k_mutex);
  566. return err < 0 ? -EIO : count;
  567. }
  568. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
  569. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
  570. 0);
  571. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
  572. static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
  573. 1);
  574. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
  575. static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
  576. 2);
  577. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
  578. static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
  579. 3);
  580. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, 0);
  581. static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
  582. 0);
  583. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
  584. i8k_hwmon_set_pwm, 0);
  585. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
  586. 1);
  587. static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
  588. 1);
  589. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
  590. i8k_hwmon_set_pwm, 1);
  591. static struct attribute *i8k_attrs[] = {
  592. &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
  593. &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
  594. &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
  595. &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */
  596. &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */
  597. &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */
  598. &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */
  599. &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */
  600. &sensor_dev_attr_fan1_input.dev_attr.attr, /* 8 */
  601. &sensor_dev_attr_fan1_label.dev_attr.attr, /* 9 */
  602. &sensor_dev_attr_pwm1.dev_attr.attr, /* 10 */
  603. &sensor_dev_attr_fan2_input.dev_attr.attr, /* 11 */
  604. &sensor_dev_attr_fan2_label.dev_attr.attr, /* 12 */
  605. &sensor_dev_attr_pwm2.dev_attr.attr, /* 13 */
  606. NULL
  607. };
  608. static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
  609. int index)
  610. {
  611. if (index >= 0 && index <= 1 &&
  612. !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
  613. return 0;
  614. if (index >= 2 && index <= 3 &&
  615. !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
  616. return 0;
  617. if (index >= 4 && index <= 5 &&
  618. !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
  619. return 0;
  620. if (index >= 6 && index <= 7 &&
  621. !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
  622. return 0;
  623. if (index >= 8 && index <= 10 &&
  624. !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
  625. return 0;
  626. if (index >= 11 && index <= 13 &&
  627. !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
  628. return 0;
  629. return attr->mode;
  630. }
  631. static const struct attribute_group i8k_group = {
  632. .attrs = i8k_attrs,
  633. .is_visible = i8k_is_visible,
  634. };
  635. __ATTRIBUTE_GROUPS(i8k);
  636. static int __init i8k_init_hwmon(void)
  637. {
  638. int err;
  639. i8k_hwmon_flags = 0;
  640. /* CPU temperature attributes, if temperature type is OK */
  641. err = i8k_get_temp_type(0);
  642. if (err >= 0)
  643. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
  644. /* check for additional temperature sensors */
  645. err = i8k_get_temp_type(1);
  646. if (err >= 0)
  647. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
  648. err = i8k_get_temp_type(2);
  649. if (err >= 0)
  650. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
  651. err = i8k_get_temp_type(3);
  652. if (err >= 0)
  653. i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
  654. /* First fan attributes, if fan type is OK */
  655. err = i8k_get_fan_type(0);
  656. if (err >= 0)
  657. i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
  658. /* Second fan attributes, if fan type is OK */
  659. err = i8k_get_fan_type(1);
  660. if (err >= 0)
  661. i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
  662. i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm",
  663. NULL, i8k_groups);
  664. if (IS_ERR(i8k_hwmon_dev)) {
  665. err = PTR_ERR(i8k_hwmon_dev);
  666. i8k_hwmon_dev = NULL;
  667. pr_err("hwmon registration failed (%d)\n", err);
  668. return err;
  669. }
  670. return 0;
  671. }
  672. struct i8k_config_data {
  673. uint fan_mult;
  674. uint fan_max;
  675. };
  676. enum i8k_configs {
  677. DELL_LATITUDE_D520,
  678. DELL_PRECISION_490,
  679. DELL_STUDIO,
  680. DELL_XPS,
  681. };
  682. static const struct i8k_config_data i8k_config_data[] = {
  683. [DELL_LATITUDE_D520] = {
  684. .fan_mult = 1,
  685. .fan_max = I8K_FAN_TURBO,
  686. },
  687. [DELL_PRECISION_490] = {
  688. .fan_mult = 1,
  689. .fan_max = I8K_FAN_TURBO,
  690. },
  691. [DELL_STUDIO] = {
  692. .fan_mult = 1,
  693. .fan_max = I8K_FAN_HIGH,
  694. },
  695. [DELL_XPS] = {
  696. .fan_mult = 1,
  697. .fan_max = I8K_FAN_HIGH,
  698. },
  699. };
  700. static struct dmi_system_id i8k_dmi_table[] __initdata = {
  701. {
  702. .ident = "Dell Inspiron",
  703. .matches = {
  704. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
  705. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
  706. },
  707. },
  708. {
  709. .ident = "Dell Latitude",
  710. .matches = {
  711. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
  712. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
  713. },
  714. },
  715. {
  716. .ident = "Dell Inspiron 2",
  717. .matches = {
  718. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  719. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
  720. },
  721. },
  722. {
  723. .ident = "Dell Latitude D520",
  724. .matches = {
  725. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  726. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
  727. },
  728. .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
  729. },
  730. {
  731. .ident = "Dell Latitude 2",
  732. .matches = {
  733. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  734. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
  735. },
  736. },
  737. { /* UK Inspiron 6400 */
  738. .ident = "Dell Inspiron 3",
  739. .matches = {
  740. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  741. DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
  742. },
  743. },
  744. {
  745. .ident = "Dell Inspiron 3",
  746. .matches = {
  747. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  748. DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
  749. },
  750. },
  751. {
  752. .ident = "Dell Precision 490",
  753. .matches = {
  754. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  755. DMI_MATCH(DMI_PRODUCT_NAME,
  756. "Precision WorkStation 490"),
  757. },
  758. .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
  759. },
  760. {
  761. .ident = "Dell Precision",
  762. .matches = {
  763. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  764. DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
  765. },
  766. },
  767. {
  768. .ident = "Dell Vostro",
  769. .matches = {
  770. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  771. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
  772. },
  773. },
  774. {
  775. .ident = "Dell XPS421",
  776. .matches = {
  777. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  778. DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
  779. },
  780. },
  781. {
  782. .ident = "Dell Studio",
  783. .matches = {
  784. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  785. DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
  786. },
  787. .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
  788. },
  789. {
  790. .ident = "Dell XPS 13",
  791. .matches = {
  792. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  793. DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
  794. },
  795. .driver_data = (void *)&i8k_config_data[DELL_XPS],
  796. },
  797. {
  798. .ident = "Dell XPS M140",
  799. .matches = {
  800. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  801. DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
  802. },
  803. .driver_data = (void *)&i8k_config_data[DELL_XPS],
  804. },
  805. { }
  806. };
  807. MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
  808. /*
  809. * Probe for the presence of a supported laptop.
  810. */
  811. static int __init i8k_probe(void)
  812. {
  813. const struct dmi_system_id *id;
  814. int fan, ret;
  815. /*
  816. * Get DMI information
  817. */
  818. if (!dmi_check_system(i8k_dmi_table)) {
  819. if (!ignore_dmi && !force)
  820. return -ENODEV;
  821. pr_info("not running on a supported Dell system.\n");
  822. pr_info("vendor=%s, model=%s, version=%s\n",
  823. i8k_get_dmi_data(DMI_SYS_VENDOR),
  824. i8k_get_dmi_data(DMI_PRODUCT_NAME),
  825. i8k_get_dmi_data(DMI_BIOS_VERSION));
  826. }
  827. strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
  828. sizeof(bios_version));
  829. /*
  830. * Get SMM Dell signature
  831. */
  832. if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
  833. i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
  834. pr_err("unable to get SMM Dell signature\n");
  835. if (!force)
  836. return -ENODEV;
  837. }
  838. /*
  839. * Set fan multiplier and maximal fan speed from dmi config
  840. * Values specified in module parameters override values from dmi
  841. */
  842. id = dmi_first_match(i8k_dmi_table);
  843. if (id && id->driver_data) {
  844. const struct i8k_config_data *conf = id->driver_data;
  845. if (!fan_mult && conf->fan_mult)
  846. fan_mult = conf->fan_mult;
  847. if (!fan_max && conf->fan_max)
  848. fan_max = conf->fan_max;
  849. }
  850. i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
  851. i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
  852. if (!fan_mult) {
  853. /*
  854. * Autodetect fan multiplier based on nominal rpm
  855. * If fan reports rpm value too high then set multiplier to 1
  856. */
  857. for (fan = 0; fan < 2; ++fan) {
  858. ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
  859. if (ret < 0)
  860. continue;
  861. if (ret > I8K_FAN_MAX_RPM)
  862. i8k_fan_mult = 1;
  863. break;
  864. }
  865. } else {
  866. /* Fan multiplier was specified in module param or in dmi */
  867. i8k_fan_mult = fan_mult;
  868. }
  869. return 0;
  870. }
  871. static int __init i8k_init(void)
  872. {
  873. int err;
  874. /* Are we running on an supported laptop? */
  875. if (i8k_probe())
  876. return -ENODEV;
  877. err = i8k_init_hwmon();
  878. if (err)
  879. return err;
  880. i8k_init_procfs();
  881. return 0;
  882. }
  883. static void __exit i8k_exit(void)
  884. {
  885. hwmon_device_unregister(i8k_hwmon_dev);
  886. i8k_exit_procfs();
  887. }
  888. module_init(i8k_init);
  889. module_exit(i8k_exit);