nct6775.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * nct6775 - Driver for the hardware monitoring functionality of
  3. * Nuvoton NCT677x Super-I/O chips
  4. *
  5. * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
  6. *
  7. * Derived from w83627ehf driver
  8. * Copyright (C) 2005-2012 Jean Delvare <khali@linux-fr.org>
  9. * Copyright (C) 2006 Yuan Mu (Winbond),
  10. * Rudolf Marek <r.marek@assembler.cz>
  11. * David Hubbard <david.c.hubbard@gmail.com>
  12. * Daniel J Blueman <daniel.blueman@gmail.com>
  13. * Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00)
  14. *
  15. * Shamelessly ripped from the w83627hf driver
  16. * Copyright (C) 2003 Mark Studebaker
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. *
  32. *
  33. * Supports the following chips:
  34. *
  35. * Chip #vin #fan #pwm #temp chip IDs man ID
  36. * nct6775f 9 4 3 6+3 0xb470 0xc1 0x5ca3
  37. * nct6776f 9 5 3 6+3 0xc330 0xc1 0x5ca3
  38. * nct6779d 15 5 5 2+6 0xc560 0xc1 0x5ca3
  39. *
  40. * #temp lists the number of monitored temperature sources (first value) plus
  41. * the number of directly connectable temperature sensors (second value).
  42. */
  43. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  44. #include <linux/module.h>
  45. #include <linux/init.h>
  46. #include <linux/slab.h>
  47. #include <linux/jiffies.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/hwmon.h>
  50. #include <linux/hwmon-sysfs.h>
  51. #include <linux/hwmon-vid.h>
  52. #include <linux/err.h>
  53. #include <linux/mutex.h>
  54. #include <linux/acpi.h>
  55. #include <linux/io.h>
  56. #include "lm75.h"
  57. enum kinds { nct6775, nct6776, nct6779 };
  58. /* used to set data->name = nct6775_device_names[data->sio_kind] */
  59. static const char * const nct6775_device_names[] = {
  60. "nct6775",
  61. "nct6776",
  62. "nct6779",
  63. };
  64. static unsigned short force_id;
  65. module_param(force_id, ushort, 0);
  66. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  67. #define DRVNAME "nct6775"
  68. /*
  69. * Super-I/O constants and functions
  70. */
  71. #define NCT6775_LD_HWM 0x0b
  72. #define NCT6775_LD_VID 0x0d
  73. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  74. #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
  75. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  76. #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
  77. #define SIO_NCT6775_ID 0xb470
  78. #define SIO_NCT6776_ID 0xc330
  79. #define SIO_NCT6779_ID 0xc560
  80. #define SIO_ID_MASK 0xFFF0
  81. static inline void
  82. superio_outb(int ioreg, int reg, int val)
  83. {
  84. outb(reg, ioreg);
  85. outb(val, ioreg + 1);
  86. }
  87. static inline int
  88. superio_inb(int ioreg, int reg)
  89. {
  90. outb(reg, ioreg);
  91. return inb(ioreg + 1);
  92. }
  93. static inline void
  94. superio_select(int ioreg, int ld)
  95. {
  96. outb(SIO_REG_LDSEL, ioreg);
  97. outb(ld, ioreg + 1);
  98. }
  99. static inline int
  100. superio_enter(int ioreg)
  101. {
  102. /*
  103. * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
  104. */
  105. if (!request_muxed_region(ioreg, 2, DRVNAME))
  106. return -EBUSY;
  107. outb(0x87, ioreg);
  108. outb(0x87, ioreg);
  109. return 0;
  110. }
  111. static inline void
  112. superio_exit(int ioreg)
  113. {
  114. outb(0xaa, ioreg);
  115. outb(0x02, ioreg);
  116. outb(0x02, ioreg + 1);
  117. release_region(ioreg, 2);
  118. }
  119. /*
  120. * ISA constants
  121. */
  122. #define IOREGION_ALIGNMENT (~7)
  123. #define IOREGION_OFFSET 5
  124. #define IOREGION_LENGTH 2
  125. #define ADDR_REG_OFFSET 0
  126. #define DATA_REG_OFFSET 1
  127. #define NCT6775_REG_BANK 0x4E
  128. #define NCT6775_REG_CONFIG 0x40
  129. /*
  130. * Not currently used:
  131. * REG_MAN_ID has the value 0x5ca3 for all supported chips.
  132. * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
  133. * REG_MAN_ID is at port 0x4f
  134. * REG_CHIP_ID is at port 0x58
  135. */
  136. #define NUM_REG_ALARM 4 /* Max number of alarm registers */
  137. /* Common and NCT6775 specific data */
  138. /* Voltage min/max registers for nr=7..14 are in bank 5 */
  139. static const u16 NCT6775_REG_IN_MAX[] = {
  140. 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
  141. 0x55c, 0x55e, 0x560, 0x562 };
  142. static const u16 NCT6775_REG_IN_MIN[] = {
  143. 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
  144. 0x55d, 0x55f, 0x561, 0x563 };
  145. static const u16 NCT6775_REG_IN[] = {
  146. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
  147. };
  148. #define NCT6775_REG_VBAT 0x5D
  149. static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
  150. /* 0..15 voltages, 16..23 fans, 24..31 temperatures */
  151. static const s8 NCT6775_ALARM_BITS[] = {
  152. 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
  153. 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
  154. -1, /* unused */
  155. 6, 7, 11, 10, 23, /* fan1..fan5 */
  156. -1, -1, -1, /* unused */
  157. 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
  158. 12, -1 }; /* intrusion0, intrusion1 */
  159. /* NCT6776 specific data */
  160. static const s8 NCT6776_ALARM_BITS[] = {
  161. 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
  162. 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
  163. -1, /* unused */
  164. 6, 7, 11, 10, 23, /* fan1..fan5 */
  165. -1, -1, -1, /* unused */
  166. 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
  167. 12, 9 }; /* intrusion0, intrusion1 */
  168. /* NCT6779 specific data */
  169. static const u16 NCT6779_REG_IN[] = {
  170. 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
  171. 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
  172. static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
  173. 0x459, 0x45A, 0x45B, 0x568 };
  174. static const s8 NCT6779_ALARM_BITS[] = {
  175. 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
  176. 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
  177. -1, /* unused */
  178. 6, 7, 11, 10, 23, /* fan1..fan5 */
  179. -1, -1, -1, /* unused */
  180. 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
  181. 12, 9 }; /* intrusion0, intrusion1 */
  182. /*
  183. * Conversions
  184. */
  185. /*
  186. * Some of the voltage inputs have internal scaling, the tables below
  187. * contain 8 (the ADC LSB in mV) * scaling factor * 100
  188. */
  189. static const u16 scale_in[15] = {
  190. 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
  191. 800, 800
  192. };
  193. static inline long in_from_reg(u8 reg, u8 nr)
  194. {
  195. return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
  196. }
  197. static inline u8 in_to_reg(u32 val, u8 nr)
  198. {
  199. return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
  200. }
  201. /*
  202. * Data structures and manipulation thereof
  203. */
  204. struct nct6775_data {
  205. int addr; /* IO base of hw monitor block */
  206. enum kinds kind;
  207. const char *name;
  208. struct device *hwmon_dev;
  209. struct mutex lock;
  210. u16 REG_CONFIG;
  211. u16 REG_VBAT;
  212. const s8 *ALARM_BITS;
  213. const u16 *REG_VIN;
  214. const u16 *REG_IN_MINMAX[2];
  215. const u16 *REG_ALARM;
  216. struct mutex update_lock;
  217. bool valid; /* true if following fields are valid */
  218. unsigned long last_updated; /* In jiffies */
  219. /* Register values */
  220. u8 bank; /* current register bank */
  221. u8 in_num; /* number of in inputs we have */
  222. u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
  223. u64 alarms;
  224. u8 vid;
  225. u8 vrm;
  226. u16 have_in;
  227. };
  228. struct nct6775_sio_data {
  229. int sioreg;
  230. enum kinds kind;
  231. };
  232. static bool is_word_sized(struct nct6775_data *data, u16 reg)
  233. {
  234. switch (data->kind) {
  235. case nct6775:
  236. return (((reg & 0xff00) == 0x100 ||
  237. (reg & 0xff00) == 0x200) &&
  238. ((reg & 0x00ff) == 0x50 ||
  239. (reg & 0x00ff) == 0x53 ||
  240. (reg & 0x00ff) == 0x55)) ||
  241. (reg & 0xfff0) == 0x630 ||
  242. reg == 0x640 || reg == 0x642 ||
  243. reg == 0x662 ||
  244. ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
  245. reg == 0x73 || reg == 0x75 || reg == 0x77;
  246. case nct6776:
  247. return (((reg & 0xff00) == 0x100 ||
  248. (reg & 0xff00) == 0x200) &&
  249. ((reg & 0x00ff) == 0x50 ||
  250. (reg & 0x00ff) == 0x53 ||
  251. (reg & 0x00ff) == 0x55)) ||
  252. (reg & 0xfff0) == 0x630 ||
  253. reg == 0x402 ||
  254. reg == 0x640 || reg == 0x642 ||
  255. ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
  256. reg == 0x73 || reg == 0x75 || reg == 0x77;
  257. case nct6779:
  258. return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
  259. ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
  260. reg == 0x402 ||
  261. reg == 0x63a || reg == 0x63c || reg == 0x63e ||
  262. reg == 0x640 || reg == 0x642 ||
  263. reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
  264. reg == 0x7b;
  265. }
  266. return false;
  267. }
  268. /*
  269. * On older chips, only registers 0x50-0x5f are banked.
  270. * On more recent chips, all registers are banked.
  271. * Assume that is the case and set the bank number for each access.
  272. * Cache the bank number so it only needs to be set if it changes.
  273. */
  274. static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
  275. {
  276. u8 bank = reg >> 8;
  277. if (data->bank != bank) {
  278. outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
  279. outb_p(bank, data->addr + DATA_REG_OFFSET);
  280. data->bank = bank;
  281. }
  282. }
  283. static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
  284. {
  285. int res, word_sized = is_word_sized(data, reg);
  286. mutex_lock(&data->lock);
  287. nct6775_set_bank(data, reg);
  288. outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
  289. res = inb_p(data->addr + DATA_REG_OFFSET);
  290. if (word_sized) {
  291. outb_p((reg & 0xff) + 1,
  292. data->addr + ADDR_REG_OFFSET);
  293. res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
  294. }
  295. mutex_unlock(&data->lock);
  296. return res;
  297. }
  298. static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
  299. {
  300. int word_sized = is_word_sized(data, reg);
  301. mutex_lock(&data->lock);
  302. nct6775_set_bank(data, reg);
  303. outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
  304. if (word_sized) {
  305. outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
  306. outb_p((reg & 0xff) + 1,
  307. data->addr + ADDR_REG_OFFSET);
  308. }
  309. outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
  310. mutex_unlock(&data->lock);
  311. return 0;
  312. }
  313. static struct nct6775_data *nct6775_update_device(struct device *dev)
  314. {
  315. struct nct6775_data *data = dev_get_drvdata(dev);
  316. int i;
  317. mutex_lock(&data->update_lock);
  318. if (time_after(jiffies, data->last_updated + HZ + HZ/2)
  319. || !data->valid) {
  320. /* Measured voltages and limits */
  321. for (i = 0; i < data->in_num; i++) {
  322. if (!(data->have_in & (1 << i)))
  323. continue;
  324. data->in[i][0] = nct6775_read_value(data,
  325. data->REG_VIN[i]);
  326. data->in[i][1] = nct6775_read_value(data,
  327. data->REG_IN_MINMAX[0][i]);
  328. data->in[i][2] = nct6775_read_value(data,
  329. data->REG_IN_MINMAX[1][i]);
  330. }
  331. data->alarms = 0;
  332. for (i = 0; i < NUM_REG_ALARM; i++) {
  333. u8 alarm;
  334. if (!data->REG_ALARM[i])
  335. continue;
  336. alarm = nct6775_read_value(data, data->REG_ALARM[i]);
  337. data->alarms |= ((u64)alarm) << (i << 3);
  338. }
  339. data->last_updated = jiffies;
  340. data->valid = true;
  341. }
  342. mutex_unlock(&data->update_lock);
  343. return data;
  344. }
  345. /*
  346. * Sysfs callback functions
  347. */
  348. static ssize_t
  349. show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
  350. {
  351. struct nct6775_data *data = nct6775_update_device(dev);
  352. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  353. int nr = sattr->nr;
  354. int index = sattr->index;
  355. return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
  356. }
  357. static ssize_t
  358. store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
  359. size_t count)
  360. {
  361. struct nct6775_data *data = dev_get_drvdata(dev);
  362. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  363. int nr = sattr->nr;
  364. int index = sattr->index;
  365. unsigned long val;
  366. int err = kstrtoul(buf, 10, &val);
  367. if (err < 0)
  368. return err;
  369. mutex_lock(&data->update_lock);
  370. data->in[nr][index] = in_to_reg(val, nr);
  371. nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
  372. data->in[nr][index]);
  373. mutex_unlock(&data->update_lock);
  374. return count;
  375. }
  376. static ssize_t
  377. show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
  378. {
  379. struct nct6775_data *data = nct6775_update_device(dev);
  380. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  381. int nr = data->ALARM_BITS[sattr->index];
  382. return sprintf(buf, "%u\n",
  383. (unsigned int)((data->alarms >> nr) & 0x01));
  384. }
  385. static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
  386. static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
  387. static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
  388. static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
  389. static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
  390. static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
  391. static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
  392. static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
  393. static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
  394. static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
  395. static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
  396. static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
  397. static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
  398. static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
  399. static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
  400. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  401. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  402. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  403. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  404. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
  405. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
  406. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
  407. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
  408. static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
  409. static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
  410. static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
  411. static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
  412. static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
  413. static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
  414. static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
  415. static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
  416. store_in_reg, 0, 1);
  417. static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
  418. store_in_reg, 1, 1);
  419. static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
  420. store_in_reg, 2, 1);
  421. static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
  422. store_in_reg, 3, 1);
  423. static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
  424. store_in_reg, 4, 1);
  425. static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
  426. store_in_reg, 5, 1);
  427. static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
  428. store_in_reg, 6, 1);
  429. static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
  430. store_in_reg, 7, 1);
  431. static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
  432. store_in_reg, 8, 1);
  433. static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
  434. store_in_reg, 9, 1);
  435. static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
  436. store_in_reg, 10, 1);
  437. static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
  438. store_in_reg, 11, 1);
  439. static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
  440. store_in_reg, 12, 1);
  441. static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
  442. store_in_reg, 13, 1);
  443. static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
  444. store_in_reg, 14, 1);
  445. static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
  446. store_in_reg, 0, 2);
  447. static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
  448. store_in_reg, 1, 2);
  449. static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
  450. store_in_reg, 2, 2);
  451. static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
  452. store_in_reg, 3, 2);
  453. static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
  454. store_in_reg, 4, 2);
  455. static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
  456. store_in_reg, 5, 2);
  457. static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
  458. store_in_reg, 6, 2);
  459. static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
  460. store_in_reg, 7, 2);
  461. static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
  462. store_in_reg, 8, 2);
  463. static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
  464. store_in_reg, 9, 2);
  465. static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
  466. store_in_reg, 10, 2);
  467. static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
  468. store_in_reg, 11, 2);
  469. static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
  470. store_in_reg, 12, 2);
  471. static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
  472. store_in_reg, 13, 2);
  473. static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
  474. store_in_reg, 14, 2);
  475. static struct attribute *nct6775_attributes_in[15][5] = {
  476. {
  477. &sensor_dev_attr_in0_input.dev_attr.attr,
  478. &sensor_dev_attr_in0_min.dev_attr.attr,
  479. &sensor_dev_attr_in0_max.dev_attr.attr,
  480. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  481. NULL
  482. },
  483. {
  484. &sensor_dev_attr_in1_input.dev_attr.attr,
  485. &sensor_dev_attr_in1_min.dev_attr.attr,
  486. &sensor_dev_attr_in1_max.dev_attr.attr,
  487. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  488. NULL
  489. },
  490. {
  491. &sensor_dev_attr_in2_input.dev_attr.attr,
  492. &sensor_dev_attr_in2_min.dev_attr.attr,
  493. &sensor_dev_attr_in2_max.dev_attr.attr,
  494. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  495. NULL
  496. },
  497. {
  498. &sensor_dev_attr_in3_input.dev_attr.attr,
  499. &sensor_dev_attr_in3_min.dev_attr.attr,
  500. &sensor_dev_attr_in3_max.dev_attr.attr,
  501. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  502. NULL
  503. },
  504. {
  505. &sensor_dev_attr_in4_input.dev_attr.attr,
  506. &sensor_dev_attr_in4_min.dev_attr.attr,
  507. &sensor_dev_attr_in4_max.dev_attr.attr,
  508. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  509. NULL
  510. },
  511. {
  512. &sensor_dev_attr_in5_input.dev_attr.attr,
  513. &sensor_dev_attr_in5_min.dev_attr.attr,
  514. &sensor_dev_attr_in5_max.dev_attr.attr,
  515. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  516. NULL
  517. },
  518. {
  519. &sensor_dev_attr_in6_input.dev_attr.attr,
  520. &sensor_dev_attr_in6_min.dev_attr.attr,
  521. &sensor_dev_attr_in6_max.dev_attr.attr,
  522. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  523. NULL
  524. },
  525. {
  526. &sensor_dev_attr_in7_input.dev_attr.attr,
  527. &sensor_dev_attr_in7_min.dev_attr.attr,
  528. &sensor_dev_attr_in7_max.dev_attr.attr,
  529. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  530. NULL
  531. },
  532. {
  533. &sensor_dev_attr_in8_input.dev_attr.attr,
  534. &sensor_dev_attr_in8_min.dev_attr.attr,
  535. &sensor_dev_attr_in8_max.dev_attr.attr,
  536. &sensor_dev_attr_in8_alarm.dev_attr.attr,
  537. NULL
  538. },
  539. {
  540. &sensor_dev_attr_in9_input.dev_attr.attr,
  541. &sensor_dev_attr_in9_min.dev_attr.attr,
  542. &sensor_dev_attr_in9_max.dev_attr.attr,
  543. &sensor_dev_attr_in9_alarm.dev_attr.attr,
  544. NULL
  545. },
  546. {
  547. &sensor_dev_attr_in10_input.dev_attr.attr,
  548. &sensor_dev_attr_in10_min.dev_attr.attr,
  549. &sensor_dev_attr_in10_max.dev_attr.attr,
  550. &sensor_dev_attr_in10_alarm.dev_attr.attr,
  551. NULL
  552. },
  553. {
  554. &sensor_dev_attr_in11_input.dev_attr.attr,
  555. &sensor_dev_attr_in11_min.dev_attr.attr,
  556. &sensor_dev_attr_in11_max.dev_attr.attr,
  557. &sensor_dev_attr_in11_alarm.dev_attr.attr,
  558. NULL
  559. },
  560. {
  561. &sensor_dev_attr_in12_input.dev_attr.attr,
  562. &sensor_dev_attr_in12_min.dev_attr.attr,
  563. &sensor_dev_attr_in12_max.dev_attr.attr,
  564. &sensor_dev_attr_in12_alarm.dev_attr.attr,
  565. NULL
  566. },
  567. {
  568. &sensor_dev_attr_in13_input.dev_attr.attr,
  569. &sensor_dev_attr_in13_min.dev_attr.attr,
  570. &sensor_dev_attr_in13_max.dev_attr.attr,
  571. &sensor_dev_attr_in13_alarm.dev_attr.attr,
  572. NULL
  573. },
  574. {
  575. &sensor_dev_attr_in14_input.dev_attr.attr,
  576. &sensor_dev_attr_in14_min.dev_attr.attr,
  577. &sensor_dev_attr_in14_max.dev_attr.attr,
  578. &sensor_dev_attr_in14_alarm.dev_attr.attr,
  579. NULL
  580. },
  581. };
  582. static const struct attribute_group nct6775_group_in[15] = {
  583. { .attrs = nct6775_attributes_in[0] },
  584. { .attrs = nct6775_attributes_in[1] },
  585. { .attrs = nct6775_attributes_in[2] },
  586. { .attrs = nct6775_attributes_in[3] },
  587. { .attrs = nct6775_attributes_in[4] },
  588. { .attrs = nct6775_attributes_in[5] },
  589. { .attrs = nct6775_attributes_in[6] },
  590. { .attrs = nct6775_attributes_in[7] },
  591. { .attrs = nct6775_attributes_in[8] },
  592. { .attrs = nct6775_attributes_in[9] },
  593. { .attrs = nct6775_attributes_in[10] },
  594. { .attrs = nct6775_attributes_in[11] },
  595. { .attrs = nct6775_attributes_in[12] },
  596. { .attrs = nct6775_attributes_in[13] },
  597. { .attrs = nct6775_attributes_in[14] },
  598. };
  599. static ssize_t
  600. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  601. {
  602. struct nct6775_data *data = dev_get_drvdata(dev);
  603. return sprintf(buf, "%s\n", data->name);
  604. }
  605. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  606. static ssize_t
  607. show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  608. {
  609. struct nct6775_data *data = dev_get_drvdata(dev);
  610. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  611. }
  612. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  613. /*
  614. * Driver and device management
  615. */
  616. static void nct6775_device_remove_files(struct device *dev)
  617. {
  618. /*
  619. * some entries in the following arrays may not have been used in
  620. * device_create_file(), but device_remove_file() will ignore them
  621. */
  622. int i;
  623. struct nct6775_data *data = dev_get_drvdata(dev);
  624. for (i = 0; i < data->in_num; i++)
  625. sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
  626. device_remove_file(dev, &dev_attr_name);
  627. device_remove_file(dev, &dev_attr_cpu0_vid);
  628. }
  629. /* Get the monitoring functions started */
  630. static inline void nct6775_init_device(struct nct6775_data *data)
  631. {
  632. u8 tmp;
  633. /* Start monitoring if needed */
  634. if (data->REG_CONFIG) {
  635. tmp = nct6775_read_value(data, data->REG_CONFIG);
  636. if (!(tmp & 0x01))
  637. nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
  638. }
  639. /* Enable VBAT monitoring if needed */
  640. tmp = nct6775_read_value(data, data->REG_VBAT);
  641. if (!(tmp & 0x01))
  642. nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
  643. }
  644. static int nct6775_probe(struct platform_device *pdev)
  645. {
  646. struct device *dev = &pdev->dev;
  647. struct nct6775_sio_data *sio_data = dev->platform_data;
  648. struct nct6775_data *data;
  649. struct resource *res;
  650. int i, err = 0;
  651. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  652. if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
  653. DRVNAME))
  654. return -EBUSY;
  655. data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
  656. GFP_KERNEL);
  657. if (!data)
  658. return -ENOMEM;
  659. data->kind = sio_data->kind;
  660. data->addr = res->start;
  661. mutex_init(&data->lock);
  662. mutex_init(&data->update_lock);
  663. data->name = nct6775_device_names[data->kind];
  664. data->bank = 0xff; /* Force initial bank selection */
  665. platform_set_drvdata(pdev, data);
  666. switch (data->kind) {
  667. case nct6775:
  668. data->in_num = 9;
  669. data->ALARM_BITS = NCT6775_ALARM_BITS;
  670. data->REG_CONFIG = NCT6775_REG_CONFIG;
  671. data->REG_VBAT = NCT6775_REG_VBAT;
  672. data->REG_VIN = NCT6775_REG_IN;
  673. data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
  674. data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
  675. data->REG_ALARM = NCT6775_REG_ALARM;
  676. break;
  677. case nct6776:
  678. data->in_num = 9;
  679. data->ALARM_BITS = NCT6776_ALARM_BITS;
  680. data->REG_CONFIG = NCT6775_REG_CONFIG;
  681. data->REG_VBAT = NCT6775_REG_VBAT;
  682. data->REG_VIN = NCT6775_REG_IN;
  683. data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
  684. data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
  685. data->REG_ALARM = NCT6775_REG_ALARM;
  686. break;
  687. case nct6779:
  688. data->in_num = 15;
  689. data->ALARM_BITS = NCT6779_ALARM_BITS;
  690. data->REG_CONFIG = NCT6775_REG_CONFIG;
  691. data->REG_VBAT = NCT6775_REG_VBAT;
  692. data->REG_VIN = NCT6779_REG_IN;
  693. data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
  694. data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
  695. data->REG_ALARM = NCT6779_REG_ALARM;
  696. break;
  697. default:
  698. return -ENODEV;
  699. }
  700. data->have_in = (1 << data->in_num) - 1;
  701. /* Initialize the chip */
  702. nct6775_init_device(data);
  703. data->vrm = vid_which_vrm();
  704. err = superio_enter(sio_data->sioreg);
  705. if (err)
  706. return err;
  707. /*
  708. * Read VID value
  709. * We can get the VID input values directly at logical device D 0xe3.
  710. */
  711. superio_select(sio_data->sioreg, NCT6775_LD_VID);
  712. data->vid = superio_inb(sio_data->sioreg, 0xe3);
  713. superio_exit(sio_data->sioreg);
  714. err = device_create_file(dev, &dev_attr_cpu0_vid);
  715. if (err)
  716. return err;
  717. for (i = 0; i < data->in_num; i++) {
  718. if (!(data->have_in & (1 << i)))
  719. continue;
  720. err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
  721. if (err)
  722. goto exit_remove;
  723. }
  724. err = device_create_file(dev, &dev_attr_name);
  725. if (err)
  726. goto exit_remove;
  727. data->hwmon_dev = hwmon_device_register(dev);
  728. if (IS_ERR(data->hwmon_dev)) {
  729. err = PTR_ERR(data->hwmon_dev);
  730. goto exit_remove;
  731. }
  732. return 0;
  733. exit_remove:
  734. nct6775_device_remove_files(dev);
  735. return err;
  736. }
  737. static int nct6775_remove(struct platform_device *pdev)
  738. {
  739. struct nct6775_data *data = platform_get_drvdata(pdev);
  740. hwmon_device_unregister(data->hwmon_dev);
  741. nct6775_device_remove_files(&pdev->dev);
  742. return 0;
  743. }
  744. static struct platform_driver nct6775_driver = {
  745. .driver = {
  746. .owner = THIS_MODULE,
  747. .name = DRVNAME,
  748. },
  749. .probe = nct6775_probe,
  750. .remove = nct6775_remove,
  751. };
  752. /* nct6775_find() looks for a '627 in the Super-I/O config space */
  753. static int __init nct6775_find(int sioaddr, unsigned short *addr,
  754. struct nct6775_sio_data *sio_data)
  755. {
  756. static const char sio_name_NCT6775[] __initconst = "NCT6775F";
  757. static const char sio_name_NCT6776[] __initconst = "NCT6776F";
  758. static const char sio_name_NCT6779[] __initconst = "NCT6779D";
  759. u16 val;
  760. const char *sio_name;
  761. int err;
  762. err = superio_enter(sioaddr);
  763. if (err)
  764. return err;
  765. if (force_id)
  766. val = force_id;
  767. else
  768. val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
  769. | superio_inb(sioaddr, SIO_REG_DEVID + 1);
  770. switch (val & SIO_ID_MASK) {
  771. case SIO_NCT6775_ID:
  772. sio_data->kind = nct6775;
  773. sio_name = sio_name_NCT6775;
  774. break;
  775. case SIO_NCT6776_ID:
  776. sio_data->kind = nct6776;
  777. sio_name = sio_name_NCT6776;
  778. break;
  779. case SIO_NCT6779_ID:
  780. sio_data->kind = nct6779;
  781. sio_name = sio_name_NCT6779;
  782. break;
  783. default:
  784. if (val != 0xffff)
  785. pr_debug("unsupported chip ID: 0x%04x\n", val);
  786. superio_exit(sioaddr);
  787. return -ENODEV;
  788. }
  789. /* We have a known chip, find the HWM I/O address */
  790. superio_select(sioaddr, NCT6775_LD_HWM);
  791. val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
  792. | superio_inb(sioaddr, SIO_REG_ADDR + 1);
  793. *addr = val & IOREGION_ALIGNMENT;
  794. if (*addr == 0) {
  795. pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
  796. superio_exit(sioaddr);
  797. return -ENODEV;
  798. }
  799. /* Activate logical device if needed */
  800. val = superio_inb(sioaddr, SIO_REG_ENABLE);
  801. if (!(val & 0x01)) {
  802. pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
  803. superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
  804. }
  805. superio_exit(sioaddr);
  806. pr_info("Found %s chip at %#x\n", sio_name, *addr);
  807. sio_data->sioreg = sioaddr;
  808. return 0;
  809. }
  810. /*
  811. * when Super-I/O functions move to a separate file, the Super-I/O
  812. * bus will manage the lifetime of the device and this module will only keep
  813. * track of the nct6775 driver. But since we platform_device_alloc(), we
  814. * must keep track of the device
  815. */
  816. static struct platform_device *pdev;
  817. static int __init sensors_nct6775_init(void)
  818. {
  819. int err;
  820. unsigned short address;
  821. struct resource res;
  822. struct nct6775_sio_data sio_data;
  823. /*
  824. * initialize sio_data->kind and sio_data->sioreg.
  825. *
  826. * when Super-I/O functions move to a separate file, the Super-I/O
  827. * driver will probe 0x2e and 0x4e and auto-detect the presence of a
  828. * nct6775 hardware monitor, and call probe()
  829. */
  830. if (nct6775_find(0x2e, &address, &sio_data) &&
  831. nct6775_find(0x4e, &address, &sio_data))
  832. return -ENODEV;
  833. err = platform_driver_register(&nct6775_driver);
  834. if (err)
  835. goto exit;
  836. pdev = platform_device_alloc(DRVNAME, address);
  837. if (!pdev) {
  838. err = -ENOMEM;
  839. pr_err("Device allocation failed\n");
  840. goto exit_unregister;
  841. }
  842. err = platform_device_add_data(pdev, &sio_data,
  843. sizeof(struct nct6775_sio_data));
  844. if (err) {
  845. pr_err("Platform data allocation failed\n");
  846. goto exit_device_put;
  847. }
  848. memset(&res, 0, sizeof(res));
  849. res.name = DRVNAME;
  850. res.start = address + IOREGION_OFFSET;
  851. res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
  852. res.flags = IORESOURCE_IO;
  853. err = acpi_check_resource_conflict(&res);
  854. if (err)
  855. goto exit_device_put;
  856. err = platform_device_add_resources(pdev, &res, 1);
  857. if (err) {
  858. pr_err("Device resource addition failed (%d)\n", err);
  859. goto exit_device_put;
  860. }
  861. /* platform_device_add calls probe() */
  862. err = platform_device_add(pdev);
  863. if (err) {
  864. pr_err("Device addition failed (%d)\n", err);
  865. goto exit_device_put;
  866. }
  867. return 0;
  868. exit_device_put:
  869. platform_device_put(pdev);
  870. exit_unregister:
  871. platform_driver_unregister(&nct6775_driver);
  872. exit:
  873. return err;
  874. }
  875. static void __exit sensors_nct6775_exit(void)
  876. {
  877. platform_device_unregister(pdev);
  878. platform_driver_unregister(&nct6775_driver);
  879. }
  880. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  881. MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
  882. MODULE_LICENSE("GPL");
  883. module_init(sensors_nct6775_init);
  884. module_exit(sensors_nct6775_exit);