via686a.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. * via686a.c - Part of lm_sensors, Linux kernel modules
  3. * for hardware monitoring
  4. *
  5. * Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
  6. * Kyösti Mälkki <kmalkki@cc.hut.fi>,
  7. * Mark Studebaker <mdsxyz123@yahoo.com>,
  8. * and Bob Dougherty <bobd@stanford.edu>
  9. *
  10. * (Some conversion-factor data were contributed by Jonathan Teh Soon Yew
  11. * <j.teh@iname.com> and Alex van Kaam <darkside@chello.nl>.)
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. /*
  28. * Supports the Via VT82C686A, VT82C686B south bridges.
  29. * Reports all as a 686A.
  30. * Warning - only supports a single device.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include <linux/pci.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/hwmon.h>
  39. #include <linux/hwmon-sysfs.h>
  40. #include <linux/err.h>
  41. #include <linux/init.h>
  42. #include <linux/mutex.h>
  43. #include <linux/sysfs.h>
  44. #include <linux/acpi.h>
  45. #include <linux/io.h>
  46. /*
  47. * If force_addr is set to anything different from 0, we forcibly enable
  48. * the device at the given address.
  49. */
  50. static unsigned short force_addr;
  51. module_param(force_addr, ushort, 0);
  52. MODULE_PARM_DESC(force_addr,
  53. "Initialize the base address of the sensors");
  54. static struct platform_device *pdev;
  55. /*
  56. * The Via 686a southbridge has a LM78-like chip integrated on the same IC.
  57. * This driver is a customized copy of lm78.c
  58. */
  59. /* Many VIA686A constants specified below */
  60. /* Length of ISA address segment */
  61. #define VIA686A_EXTENT 0x80
  62. #define VIA686A_BASE_REG 0x70
  63. #define VIA686A_ENABLE_REG 0x74
  64. /* The VIA686A registers */
  65. /* ins numbered 0-4 */
  66. #define VIA686A_REG_IN_MAX(nr) (0x2b + ((nr) * 2))
  67. #define VIA686A_REG_IN_MIN(nr) (0x2c + ((nr) * 2))
  68. #define VIA686A_REG_IN(nr) (0x22 + (nr))
  69. /* fans numbered 1-2 */
  70. #define VIA686A_REG_FAN_MIN(nr) (0x3a + (nr))
  71. #define VIA686A_REG_FAN(nr) (0x28 + (nr))
  72. /* temps numbered 1-3 */
  73. static const u8 VIA686A_REG_TEMP[] = { 0x20, 0x21, 0x1f };
  74. static const u8 VIA686A_REG_TEMP_OVER[] = { 0x39, 0x3d, 0x1d };
  75. static const u8 VIA686A_REG_TEMP_HYST[] = { 0x3a, 0x3e, 0x1e };
  76. /* bits 7-6 */
  77. #define VIA686A_REG_TEMP_LOW1 0x4b
  78. /* 2 = bits 5-4, 3 = bits 7-6 */
  79. #define VIA686A_REG_TEMP_LOW23 0x49
  80. #define VIA686A_REG_ALARM1 0x41
  81. #define VIA686A_REG_ALARM2 0x42
  82. #define VIA686A_REG_FANDIV 0x47
  83. #define VIA686A_REG_CONFIG 0x40
  84. /*
  85. * The following register sets temp interrupt mode (bits 1-0 for temp1,
  86. * 3-2 for temp2, 5-4 for temp3). Modes are:
  87. * 00 interrupt stays as long as value is out-of-range
  88. * 01 interrupt is cleared once register is read (default)
  89. * 10 comparator mode- like 00, but ignores hysteresis
  90. * 11 same as 00
  91. */
  92. #define VIA686A_REG_TEMP_MODE 0x4b
  93. /* We'll just assume that you want to set all 3 simultaneously: */
  94. #define VIA686A_TEMP_MODE_MASK 0x3F
  95. #define VIA686A_TEMP_MODE_CONTINUOUS 0x00
  96. /*
  97. * Conversions. Limit checking is only done on the TO_REG
  98. * variants.
  99. *
  100. ******** VOLTAGE CONVERSIONS (Bob Dougherty) ********
  101. * From HWMon.cpp (Copyright 1998-2000 Jonathan Teh Soon Yew):
  102. * voltagefactor[0]=1.25/2628; (2628/1.25=2102.4) // Vccp
  103. * voltagefactor[1]=1.25/2628; (2628/1.25=2102.4) // +2.5V
  104. * voltagefactor[2]=1.67/2628; (2628/1.67=1573.7) // +3.3V
  105. * voltagefactor[3]=2.6/2628; (2628/2.60=1010.8) // +5V
  106. * voltagefactor[4]=6.3/2628; (2628/6.30=417.14) // +12V
  107. * in[i]=(data[i+2]*25.0+133)*voltagefactor[i];
  108. * That is:
  109. * volts = (25*regVal+133)*factor
  110. * regVal = (volts/factor-133)/25
  111. * (These conversions were contributed by Jonathan Teh Soon Yew
  112. * <j.teh@iname.com>)
  113. */
  114. static inline u8 IN_TO_REG(long val, int inNum)
  115. {
  116. /*
  117. * To avoid floating point, we multiply constants by 10 (100 for +12V).
  118. * Rounding is done (120500 is actually 133000 - 12500).
  119. * Remember that val is expressed in 0.001V/bit, which is why we divide
  120. * by an additional 10000 (100000 for +12V): 1000 for val and 10 (100)
  121. * for the constants.
  122. */
  123. if (inNum <= 1)
  124. return (u8)
  125. SENSORS_LIMIT((val * 21024 - 1205000) / 250000, 0, 255);
  126. else if (inNum == 2)
  127. return (u8)
  128. SENSORS_LIMIT((val * 15737 - 1205000) / 250000, 0, 255);
  129. else if (inNum == 3)
  130. return (u8)
  131. SENSORS_LIMIT((val * 10108 - 1205000) / 250000, 0, 255);
  132. else
  133. return (u8)
  134. SENSORS_LIMIT((val * 41714 - 12050000) / 2500000, 0, 255);
  135. }
  136. static inline long IN_FROM_REG(u8 val, int inNum)
  137. {
  138. /*
  139. * To avoid floating point, we multiply constants by 10 (100 for +12V).
  140. * We also multiply them by 1000 because we want 0.001V/bit for the
  141. * output value. Rounding is done.
  142. */
  143. if (inNum <= 1)
  144. return (long) ((250000 * val + 1330000 + 21024 / 2) / 21024);
  145. else if (inNum == 2)
  146. return (long) ((250000 * val + 1330000 + 15737 / 2) / 15737);
  147. else if (inNum == 3)
  148. return (long) ((250000 * val + 1330000 + 10108 / 2) / 10108);
  149. else
  150. return (long) ((2500000 * val + 13300000 + 41714 / 2) / 41714);
  151. }
  152. /********* FAN RPM CONVERSIONS ********/
  153. /*
  154. * Higher register values = slower fans (the fan's strobe gates a counter).
  155. * But this chip saturates back at 0, not at 255 like all the other chips.
  156. * So, 0 means 0 RPM
  157. */
  158. static inline u8 FAN_TO_REG(long rpm, int div)
  159. {
  160. if (rpm == 0)
  161. return 0;
  162. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  163. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 255);
  164. }
  165. #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (val) == 255 ? 0 : 1350000 / \
  166. ((val) * (div)))
  167. /******** TEMP CONVERSIONS (Bob Dougherty) *********/
  168. /*
  169. * linear fits from HWMon.cpp (Copyright 1998-2000 Jonathan Teh Soon Yew)
  170. * if(temp<169)
  171. * return double(temp)*0.427-32.08;
  172. * else if(temp>=169 && temp<=202)
  173. * return double(temp)*0.582-58.16;
  174. * else
  175. * return double(temp)*0.924-127.33;
  176. *
  177. * A fifth-order polynomial fits the unofficial data (provided by Alex van
  178. * Kaam <darkside@chello.nl>) a bit better. It also give more reasonable
  179. * numbers on my machine (ie. they agree with what my BIOS tells me).
  180. * Here's the fifth-order fit to the 8-bit data:
  181. * temp = 1.625093e-10*val^5 - 1.001632e-07*val^4 + 2.457653e-05*val^3 -
  182. * 2.967619e-03*val^2 + 2.175144e-01*val - 7.090067e+0.
  183. *
  184. * (2000-10-25- RFD: thanks to Uwe Andersen <uandersen@mayah.com> for
  185. * finding my typos in this formula!)
  186. *
  187. * Alas, none of the elegant function-fit solutions will work because we
  188. * aren't allowed to use floating point in the kernel and doing it with
  189. * integers doesn't provide enough precision. So we'll do boring old
  190. * look-up table stuff. The unofficial data (see below) have effectively
  191. * 7-bit resolution (they are rounded to the nearest degree). I'm assuming
  192. * that the transfer function of the device is monotonic and smooth, so a
  193. * smooth function fit to the data will allow us to get better precision.
  194. * I used the 5th-order poly fit described above and solved for
  195. * VIA register values 0-255. I *10 before rounding, so we get tenth-degree
  196. * precision. (I could have done all 1024 values for our 10-bit readings,
  197. * but the function is very linear in the useful range (0-80 deg C), so
  198. * we'll just use linear interpolation for 10-bit readings.) So, tempLUT
  199. * is the temp at via register values 0-255:
  200. */
  201. static const s16 tempLUT[] = {
  202. -709, -688, -667, -646, -627, -607, -589, -570, -553, -536, -519,
  203. -503, -487, -471, -456, -442, -428, -414, -400, -387, -375,
  204. -362, -350, -339, -327, -316, -305, -295, -285, -275, -265,
  205. -255, -246, -237, -229, -220, -212, -204, -196, -188, -180,
  206. -173, -166, -159, -152, -145, -139, -132, -126, -120, -114,
  207. -108, -102, -96, -91, -85, -80, -74, -69, -64, -59, -54, -49,
  208. -44, -39, -34, -29, -25, -20, -15, -11, -6, -2, 3, 7, 12, 16,
  209. 20, 25, 29, 33, 37, 42, 46, 50, 54, 59, 63, 67, 71, 75, 79, 84,
  210. 88, 92, 96, 100, 104, 109, 113, 117, 121, 125, 130, 134, 138,
  211. 142, 146, 151, 155, 159, 163, 168, 172, 176, 181, 185, 189,
  212. 193, 198, 202, 206, 211, 215, 219, 224, 228, 232, 237, 241,
  213. 245, 250, 254, 259, 263, 267, 272, 276, 281, 285, 290, 294,
  214. 299, 303, 307, 312, 316, 321, 325, 330, 334, 339, 344, 348,
  215. 353, 357, 362, 366, 371, 376, 380, 385, 390, 395, 399, 404,
  216. 409, 414, 419, 423, 428, 433, 438, 443, 449, 454, 459, 464,
  217. 469, 475, 480, 486, 491, 497, 502, 508, 514, 520, 526, 532,
  218. 538, 544, 551, 557, 564, 571, 578, 584, 592, 599, 606, 614,
  219. 621, 629, 637, 645, 654, 662, 671, 680, 689, 698, 708, 718,
  220. 728, 738, 749, 759, 770, 782, 793, 805, 818, 830, 843, 856,
  221. 870, 883, 898, 912, 927, 943, 958, 975, 991, 1008, 1026, 1044,
  222. 1062, 1081, 1101, 1121, 1141, 1162, 1184, 1206, 1229, 1252,
  223. 1276, 1301, 1326, 1352, 1378, 1406, 1434, 1462
  224. };
  225. /*
  226. * the original LUT values from Alex van Kaam <darkside@chello.nl>
  227. * (for via register values 12-240):
  228. * {-50,-49,-47,-45,-43,-41,-39,-38,-37,-35,-34,-33,-32,-31,
  229. * -30,-29,-28,-27,-26,-25,-24,-24,-23,-22,-21,-20,-20,-19,-18,-17,-17,-16,-15,
  230. * -15,-14,-14,-13,-12,-12,-11,-11,-10,-9,-9,-8,-8,-7,-7,-6,-6,-5,-5,-4,-4,-3,
  231. * -3,-2,-2,-1,-1,0,0,1,1,1,3,3,3,4,4,4,5,5,5,6,6,7,7,8,8,9,9,9,10,10,11,11,12,
  232. * 12,12,13,13,13,14,14,15,15,16,16,16,17,17,18,18,19,19,20,20,21,21,21,22,22,
  233. * 22,23,23,24,24,25,25,26,26,26,27,27,27,28,28,29,29,30,30,30,31,31,32,32,33,
  234. * 33,34,34,35,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,
  235. * 45,46,46,47,48,48,49,49,50,51,51,52,52,53,53,54,55,55,56,57,57,58,59,59,60,
  236. * 61,62,62,63,64,65,66,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,83,84,
  237. * 85,86,88,89,91,92,94,96,97,99,101,103,105,107,109,110};
  238. *
  239. *
  240. * Here's the reverse LUT. I got it by doing a 6-th order poly fit (needed
  241. * an extra term for a good fit to these inverse data!) and then
  242. * solving for each temp value from -50 to 110 (the useable range for
  243. * this chip). Here's the fit:
  244. * viaRegVal = -1.160370e-10*val^6 +3.193693e-08*val^5 - 1.464447e-06*val^4
  245. * - 2.525453e-04*val^3 + 1.424593e-02*val^2 + 2.148941e+00*val +7.275808e+01)
  246. * Note that n=161:
  247. */
  248. static const u8 viaLUT[] = {
  249. 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18, 19, 20, 20, 21, 22, 23,
  250. 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 39, 40,
  251. 41, 43, 45, 46, 48, 49, 51, 53, 55, 57, 59, 60, 62, 64, 66,
  252. 69, 71, 73, 75, 77, 79, 82, 84, 86, 88, 91, 93, 95, 98, 100,
  253. 103, 105, 107, 110, 112, 115, 117, 119, 122, 124, 126, 129,
  254. 131, 134, 136, 138, 140, 143, 145, 147, 150, 152, 154, 156,
  255. 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180,
  256. 182, 183, 185, 187, 188, 190, 192, 193, 195, 196, 198, 199,
  257. 200, 202, 203, 205, 206, 207, 208, 209, 210, 211, 212, 213,
  258. 214, 215, 216, 217, 218, 219, 220, 221, 222, 222, 223, 224,
  259. 225, 226, 226, 227, 228, 228, 229, 230, 230, 231, 232, 232,
  260. 233, 233, 234, 235, 235, 236, 236, 237, 237, 238, 238, 239,
  261. 239, 240
  262. };
  263. /*
  264. * Converting temps to (8-bit) hyst and over registers
  265. * No interpolation here.
  266. * The +50 is because the temps start at -50
  267. */
  268. static inline u8 TEMP_TO_REG(long val)
  269. {
  270. return viaLUT[val <= -50000 ? 0 : val >= 110000 ? 160 :
  271. (val < 0 ? val - 500 : val + 500) / 1000 + 50];
  272. }
  273. /* for 8-bit temperature hyst and over registers */
  274. #define TEMP_FROM_REG(val) ((long)tempLUT[val] * 100)
  275. /* for 10-bit temperature readings */
  276. static inline long TEMP_FROM_REG10(u16 val)
  277. {
  278. u16 eightBits = val >> 2;
  279. u16 twoBits = val & 3;
  280. /* no interpolation for these */
  281. if (twoBits == 0 || eightBits == 255)
  282. return TEMP_FROM_REG(eightBits);
  283. /* do some linear interpolation */
  284. return (tempLUT[eightBits] * (4 - twoBits) +
  285. tempLUT[eightBits + 1] * twoBits) * 25;
  286. }
  287. #define DIV_FROM_REG(val) (1 << (val))
  288. #define DIV_TO_REG(val) ((val) == 8 ? 3 : (val) == 4 ? 2 : (val) == 1 ? 0 : 1)
  289. /*
  290. * For each registered chip, we need to keep some data in memory.
  291. * The structure is dynamically allocated.
  292. */
  293. struct via686a_data {
  294. unsigned short addr;
  295. const char *name;
  296. struct device *hwmon_dev;
  297. struct mutex update_lock;
  298. char valid; /* !=0 if following fields are valid */
  299. unsigned long last_updated; /* In jiffies */
  300. u8 in[5]; /* Register value */
  301. u8 in_max[5]; /* Register value */
  302. u8 in_min[5]; /* Register value */
  303. u8 fan[2]; /* Register value */
  304. u8 fan_min[2]; /* Register value */
  305. u16 temp[3]; /* Register value 10 bit */
  306. u8 temp_over[3]; /* Register value */
  307. u8 temp_hyst[3]; /* Register value */
  308. u8 fan_div[2]; /* Register encoding, shifted right */
  309. u16 alarms; /* Register encoding, combined */
  310. };
  311. static struct pci_dev *s_bridge; /* pointer to the (only) via686a */
  312. static int via686a_probe(struct platform_device *pdev);
  313. static int __devexit via686a_remove(struct platform_device *pdev);
  314. static inline int via686a_read_value(struct via686a_data *data, u8 reg)
  315. {
  316. return inb_p(data->addr + reg);
  317. }
  318. static inline void via686a_write_value(struct via686a_data *data, u8 reg,
  319. u8 value)
  320. {
  321. outb_p(value, data->addr + reg);
  322. }
  323. static struct via686a_data *via686a_update_device(struct device *dev);
  324. static void via686a_init_device(struct via686a_data *data);
  325. /* following are the sysfs callback functions */
  326. /* 7 voltage sensors */
  327. static ssize_t show_in(struct device *dev, struct device_attribute *da,
  328. char *buf) {
  329. struct via686a_data *data = via686a_update_device(dev);
  330. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  331. int nr = attr->index;
  332. return sprintf(buf, "%ld\n", IN_FROM_REG(data->in[nr], nr));
  333. }
  334. static ssize_t show_in_min(struct device *dev, struct device_attribute *da,
  335. char *buf) {
  336. struct via686a_data *data = via686a_update_device(dev);
  337. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  338. int nr = attr->index;
  339. return sprintf(buf, "%ld\n", IN_FROM_REG(data->in_min[nr], nr));
  340. }
  341. static ssize_t show_in_max(struct device *dev, struct device_attribute *da,
  342. char *buf) {
  343. struct via686a_data *data = via686a_update_device(dev);
  344. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  345. int nr = attr->index;
  346. return sprintf(buf, "%ld\n", IN_FROM_REG(data->in_max[nr], nr));
  347. }
  348. static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
  349. const char *buf, size_t count) {
  350. struct via686a_data *data = dev_get_drvdata(dev);
  351. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  352. int nr = attr->index;
  353. unsigned long val;
  354. int err;
  355. err = kstrtoul(buf, 10, &val);
  356. if (err)
  357. return err;
  358. mutex_lock(&data->update_lock);
  359. data->in_min[nr] = IN_TO_REG(val, nr);
  360. via686a_write_value(data, VIA686A_REG_IN_MIN(nr),
  361. data->in_min[nr]);
  362. mutex_unlock(&data->update_lock);
  363. return count;
  364. }
  365. static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
  366. const char *buf, size_t count) {
  367. struct via686a_data *data = dev_get_drvdata(dev);
  368. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  369. int nr = attr->index;
  370. unsigned long val;
  371. int err;
  372. err = kstrtoul(buf, 10, &val);
  373. if (err)
  374. return err;
  375. mutex_lock(&data->update_lock);
  376. data->in_max[nr] = IN_TO_REG(val, nr);
  377. via686a_write_value(data, VIA686A_REG_IN_MAX(nr),
  378. data->in_max[nr]);
  379. mutex_unlock(&data->update_lock);
  380. return count;
  381. }
  382. #define show_in_offset(offset) \
  383. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  384. show_in, NULL, offset); \
  385. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  386. show_in_min, set_in_min, offset); \
  387. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  388. show_in_max, set_in_max, offset);
  389. show_in_offset(0);
  390. show_in_offset(1);
  391. show_in_offset(2);
  392. show_in_offset(3);
  393. show_in_offset(4);
  394. /* 3 temperatures */
  395. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  396. char *buf) {
  397. struct via686a_data *data = via686a_update_device(dev);
  398. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  399. int nr = attr->index;
  400. return sprintf(buf, "%ld\n", TEMP_FROM_REG10(data->temp[nr]));
  401. }
  402. static ssize_t show_temp_over(struct device *dev, struct device_attribute *da,
  403. char *buf) {
  404. struct via686a_data *data = via686a_update_device(dev);
  405. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  406. int nr = attr->index;
  407. return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp_over[nr]));
  408. }
  409. static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *da,
  410. char *buf) {
  411. struct via686a_data *data = via686a_update_device(dev);
  412. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  413. int nr = attr->index;
  414. return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp_hyst[nr]));
  415. }
  416. static ssize_t set_temp_over(struct device *dev, struct device_attribute *da,
  417. const char *buf, size_t count) {
  418. struct via686a_data *data = dev_get_drvdata(dev);
  419. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  420. int nr = attr->index;
  421. long val;
  422. int err;
  423. err = kstrtol(buf, 10, &val);
  424. if (err)
  425. return err;
  426. mutex_lock(&data->update_lock);
  427. data->temp_over[nr] = TEMP_TO_REG(val);
  428. via686a_write_value(data, VIA686A_REG_TEMP_OVER[nr],
  429. data->temp_over[nr]);
  430. mutex_unlock(&data->update_lock);
  431. return count;
  432. }
  433. static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *da,
  434. const char *buf, size_t count) {
  435. struct via686a_data *data = dev_get_drvdata(dev);
  436. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  437. int nr = attr->index;
  438. long val;
  439. int err;
  440. err = kstrtol(buf, 10, &val);
  441. if (err)
  442. return err;
  443. mutex_lock(&data->update_lock);
  444. data->temp_hyst[nr] = TEMP_TO_REG(val);
  445. via686a_write_value(data, VIA686A_REG_TEMP_HYST[nr],
  446. data->temp_hyst[nr]);
  447. mutex_unlock(&data->update_lock);
  448. return count;
  449. }
  450. #define show_temp_offset(offset) \
  451. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  452. show_temp, NULL, offset - 1); \
  453. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  454. show_temp_over, set_temp_over, offset - 1); \
  455. static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
  456. show_temp_hyst, set_temp_hyst, offset - 1);
  457. show_temp_offset(1);
  458. show_temp_offset(2);
  459. show_temp_offset(3);
  460. /* 2 Fans */
  461. static ssize_t show_fan(struct device *dev, struct device_attribute *da,
  462. char *buf) {
  463. struct via686a_data *data = via686a_update_device(dev);
  464. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  465. int nr = attr->index;
  466. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  467. DIV_FROM_REG(data->fan_div[nr])));
  468. }
  469. static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
  470. char *buf) {
  471. struct via686a_data *data = via686a_update_device(dev);
  472. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  473. int nr = attr->index;
  474. return sprintf(buf, "%d\n",
  475. FAN_FROM_REG(data->fan_min[nr],
  476. DIV_FROM_REG(data->fan_div[nr])));
  477. }
  478. static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
  479. char *buf) {
  480. struct via686a_data *data = via686a_update_device(dev);
  481. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  482. int nr = attr->index;
  483. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  484. }
  485. static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
  486. const char *buf, size_t count) {
  487. struct via686a_data *data = dev_get_drvdata(dev);
  488. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  489. int nr = attr->index;
  490. unsigned long val;
  491. int err;
  492. err = kstrtoul(buf, 10, &val);
  493. if (err)
  494. return err;
  495. mutex_lock(&data->update_lock);
  496. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  497. via686a_write_value(data, VIA686A_REG_FAN_MIN(nr+1), data->fan_min[nr]);
  498. mutex_unlock(&data->update_lock);
  499. return count;
  500. }
  501. static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
  502. const char *buf, size_t count) {
  503. struct via686a_data *data = dev_get_drvdata(dev);
  504. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  505. int nr = attr->index;
  506. int old;
  507. unsigned long val;
  508. int err;
  509. err = kstrtoul(buf, 10, &val);
  510. if (err)
  511. return err;
  512. mutex_lock(&data->update_lock);
  513. old = via686a_read_value(data, VIA686A_REG_FANDIV);
  514. data->fan_div[nr] = DIV_TO_REG(val);
  515. old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
  516. via686a_write_value(data, VIA686A_REG_FANDIV, old);
  517. mutex_unlock(&data->update_lock);
  518. return count;
  519. }
  520. #define show_fan_offset(offset) \
  521. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  522. show_fan, NULL, offset - 1); \
  523. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  524. show_fan_min, set_fan_min, offset - 1); \
  525. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  526. show_fan_div, set_fan_div, offset - 1);
  527. show_fan_offset(1);
  528. show_fan_offset(2);
  529. /* Alarms */
  530. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  531. char *buf)
  532. {
  533. struct via686a_data *data = via686a_update_device(dev);
  534. return sprintf(buf, "%u\n", data->alarms);
  535. }
  536. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  537. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  538. char *buf)
  539. {
  540. int bitnr = to_sensor_dev_attr(attr)->index;
  541. struct via686a_data *data = via686a_update_device(dev);
  542. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  543. }
  544. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  545. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  546. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  547. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  548. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
  549. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
  550. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 11);
  551. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 15);
  552. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  553. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  554. static ssize_t show_name(struct device *dev, struct device_attribute
  555. *devattr, char *buf)
  556. {
  557. struct via686a_data *data = dev_get_drvdata(dev);
  558. return sprintf(buf, "%s\n", data->name);
  559. }
  560. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  561. static struct attribute *via686a_attributes[] = {
  562. &sensor_dev_attr_in0_input.dev_attr.attr,
  563. &sensor_dev_attr_in1_input.dev_attr.attr,
  564. &sensor_dev_attr_in2_input.dev_attr.attr,
  565. &sensor_dev_attr_in3_input.dev_attr.attr,
  566. &sensor_dev_attr_in4_input.dev_attr.attr,
  567. &sensor_dev_attr_in0_min.dev_attr.attr,
  568. &sensor_dev_attr_in1_min.dev_attr.attr,
  569. &sensor_dev_attr_in2_min.dev_attr.attr,
  570. &sensor_dev_attr_in3_min.dev_attr.attr,
  571. &sensor_dev_attr_in4_min.dev_attr.attr,
  572. &sensor_dev_attr_in0_max.dev_attr.attr,
  573. &sensor_dev_attr_in1_max.dev_attr.attr,
  574. &sensor_dev_attr_in2_max.dev_attr.attr,
  575. &sensor_dev_attr_in3_max.dev_attr.attr,
  576. &sensor_dev_attr_in4_max.dev_attr.attr,
  577. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  578. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  579. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  580. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  581. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  582. &sensor_dev_attr_temp1_input.dev_attr.attr,
  583. &sensor_dev_attr_temp2_input.dev_attr.attr,
  584. &sensor_dev_attr_temp3_input.dev_attr.attr,
  585. &sensor_dev_attr_temp1_max.dev_attr.attr,
  586. &sensor_dev_attr_temp2_max.dev_attr.attr,
  587. &sensor_dev_attr_temp3_max.dev_attr.attr,
  588. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  589. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  590. &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
  591. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  592. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  593. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  594. &sensor_dev_attr_fan1_input.dev_attr.attr,
  595. &sensor_dev_attr_fan2_input.dev_attr.attr,
  596. &sensor_dev_attr_fan1_min.dev_attr.attr,
  597. &sensor_dev_attr_fan2_min.dev_attr.attr,
  598. &sensor_dev_attr_fan1_div.dev_attr.attr,
  599. &sensor_dev_attr_fan2_div.dev_attr.attr,
  600. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  601. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  602. &dev_attr_alarms.attr,
  603. &dev_attr_name.attr,
  604. NULL
  605. };
  606. static const struct attribute_group via686a_group = {
  607. .attrs = via686a_attributes,
  608. };
  609. static struct platform_driver via686a_driver = {
  610. .driver = {
  611. .owner = THIS_MODULE,
  612. .name = "via686a",
  613. },
  614. .probe = via686a_probe,
  615. .remove = __devexit_p(via686a_remove),
  616. };
  617. /* This is called when the module is loaded */
  618. static int __devinit via686a_probe(struct platform_device *pdev)
  619. {
  620. struct via686a_data *data;
  621. struct resource *res;
  622. int err;
  623. /* Reserve the ISA region */
  624. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  625. if (!request_region(res->start, VIA686A_EXTENT,
  626. via686a_driver.driver.name)) {
  627. dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
  628. (unsigned long)res->start, (unsigned long)res->end);
  629. return -ENODEV;
  630. }
  631. data = kzalloc(sizeof(struct via686a_data), GFP_KERNEL);
  632. if (!data) {
  633. err = -ENOMEM;
  634. goto exit_release;
  635. }
  636. platform_set_drvdata(pdev, data);
  637. data->addr = res->start;
  638. data->name = "via686a";
  639. mutex_init(&data->update_lock);
  640. /* Initialize the VIA686A chip */
  641. via686a_init_device(data);
  642. /* Register sysfs hooks */
  643. err = sysfs_create_group(&pdev->dev.kobj, &via686a_group);
  644. if (err)
  645. goto exit_free;
  646. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  647. if (IS_ERR(data->hwmon_dev)) {
  648. err = PTR_ERR(data->hwmon_dev);
  649. goto exit_remove_files;
  650. }
  651. return 0;
  652. exit_remove_files:
  653. sysfs_remove_group(&pdev->dev.kobj, &via686a_group);
  654. exit_free:
  655. kfree(data);
  656. exit_release:
  657. release_region(res->start, VIA686A_EXTENT);
  658. return err;
  659. }
  660. static int __devexit via686a_remove(struct platform_device *pdev)
  661. {
  662. struct via686a_data *data = platform_get_drvdata(pdev);
  663. hwmon_device_unregister(data->hwmon_dev);
  664. sysfs_remove_group(&pdev->dev.kobj, &via686a_group);
  665. release_region(data->addr, VIA686A_EXTENT);
  666. platform_set_drvdata(pdev, NULL);
  667. kfree(data);
  668. return 0;
  669. }
  670. static void via686a_update_fan_div(struct via686a_data *data)
  671. {
  672. int reg = via686a_read_value(data, VIA686A_REG_FANDIV);
  673. data->fan_div[0] = (reg >> 4) & 0x03;
  674. data->fan_div[1] = reg >> 6;
  675. }
  676. static void __devinit via686a_init_device(struct via686a_data *data)
  677. {
  678. u8 reg;
  679. /* Start monitoring */
  680. reg = via686a_read_value(data, VIA686A_REG_CONFIG);
  681. via686a_write_value(data, VIA686A_REG_CONFIG, (reg | 0x01) & 0x7F);
  682. /* Configure temp interrupt mode for continuous-interrupt operation */
  683. reg = via686a_read_value(data, VIA686A_REG_TEMP_MODE);
  684. via686a_write_value(data, VIA686A_REG_TEMP_MODE,
  685. (reg & ~VIA686A_TEMP_MODE_MASK)
  686. | VIA686A_TEMP_MODE_CONTINUOUS);
  687. /* Pre-read fan clock divisor values */
  688. via686a_update_fan_div(data);
  689. }
  690. static struct via686a_data *via686a_update_device(struct device *dev)
  691. {
  692. struct via686a_data *data = dev_get_drvdata(dev);
  693. int i;
  694. mutex_lock(&data->update_lock);
  695. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  696. || !data->valid) {
  697. for (i = 0; i <= 4; i++) {
  698. data->in[i] =
  699. via686a_read_value(data, VIA686A_REG_IN(i));
  700. data->in_min[i] = via686a_read_value(data,
  701. VIA686A_REG_IN_MIN
  702. (i));
  703. data->in_max[i] =
  704. via686a_read_value(data, VIA686A_REG_IN_MAX(i));
  705. }
  706. for (i = 1; i <= 2; i++) {
  707. data->fan[i - 1] =
  708. via686a_read_value(data, VIA686A_REG_FAN(i));
  709. data->fan_min[i - 1] = via686a_read_value(data,
  710. VIA686A_REG_FAN_MIN(i));
  711. }
  712. for (i = 0; i <= 2; i++) {
  713. data->temp[i] = via686a_read_value(data,
  714. VIA686A_REG_TEMP[i]) << 2;
  715. data->temp_over[i] =
  716. via686a_read_value(data,
  717. VIA686A_REG_TEMP_OVER[i]);
  718. data->temp_hyst[i] =
  719. via686a_read_value(data,
  720. VIA686A_REG_TEMP_HYST[i]);
  721. }
  722. /*
  723. * add in lower 2 bits
  724. * temp1 uses bits 7-6 of VIA686A_REG_TEMP_LOW1
  725. * temp2 uses bits 5-4 of VIA686A_REG_TEMP_LOW23
  726. * temp3 uses bits 7-6 of VIA686A_REG_TEMP_LOW23
  727. */
  728. data->temp[0] |= (via686a_read_value(data,
  729. VIA686A_REG_TEMP_LOW1)
  730. & 0xc0) >> 6;
  731. data->temp[1] |=
  732. (via686a_read_value(data, VIA686A_REG_TEMP_LOW23) &
  733. 0x30) >> 4;
  734. data->temp[2] |=
  735. (via686a_read_value(data, VIA686A_REG_TEMP_LOW23) &
  736. 0xc0) >> 6;
  737. via686a_update_fan_div(data);
  738. data->alarms =
  739. via686a_read_value(data,
  740. VIA686A_REG_ALARM1) |
  741. (via686a_read_value(data, VIA686A_REG_ALARM2) << 8);
  742. data->last_updated = jiffies;
  743. data->valid = 1;
  744. }
  745. mutex_unlock(&data->update_lock);
  746. return data;
  747. }
  748. static DEFINE_PCI_DEVICE_TABLE(via686a_pci_ids) = {
  749. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_4) },
  750. { }
  751. };
  752. MODULE_DEVICE_TABLE(pci, via686a_pci_ids);
  753. static int __devinit via686a_device_add(unsigned short address)
  754. {
  755. struct resource res = {
  756. .start = address,
  757. .end = address + VIA686A_EXTENT - 1,
  758. .name = "via686a",
  759. .flags = IORESOURCE_IO,
  760. };
  761. int err;
  762. err = acpi_check_resource_conflict(&res);
  763. if (err)
  764. goto exit;
  765. pdev = platform_device_alloc("via686a", address);
  766. if (!pdev) {
  767. err = -ENOMEM;
  768. pr_err("Device allocation failed\n");
  769. goto exit;
  770. }
  771. err = platform_device_add_resources(pdev, &res, 1);
  772. if (err) {
  773. pr_err("Device resource addition failed (%d)\n", err);
  774. goto exit_device_put;
  775. }
  776. err = platform_device_add(pdev);
  777. if (err) {
  778. pr_err("Device addition failed (%d)\n", err);
  779. goto exit_device_put;
  780. }
  781. return 0;
  782. exit_device_put:
  783. platform_device_put(pdev);
  784. exit:
  785. return err;
  786. }
  787. static int __devinit via686a_pci_probe(struct pci_dev *dev,
  788. const struct pci_device_id *id)
  789. {
  790. u16 address, val;
  791. if (force_addr) {
  792. address = force_addr & ~(VIA686A_EXTENT - 1);
  793. dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", address);
  794. if (PCIBIOS_SUCCESSFUL !=
  795. pci_write_config_word(dev, VIA686A_BASE_REG, address | 1))
  796. return -ENODEV;
  797. }
  798. if (PCIBIOS_SUCCESSFUL !=
  799. pci_read_config_word(dev, VIA686A_BASE_REG, &val))
  800. return -ENODEV;
  801. address = val & ~(VIA686A_EXTENT - 1);
  802. if (address == 0) {
  803. dev_err(&dev->dev, "base address not set - upgrade BIOS "
  804. "or use force_addr=0xaddr\n");
  805. return -ENODEV;
  806. }
  807. if (PCIBIOS_SUCCESSFUL !=
  808. pci_read_config_word(dev, VIA686A_ENABLE_REG, &val))
  809. return -ENODEV;
  810. if (!(val & 0x0001)) {
  811. if (!force_addr) {
  812. dev_warn(&dev->dev, "Sensors disabled, enable "
  813. "with force_addr=0x%x\n", address);
  814. return -ENODEV;
  815. }
  816. dev_warn(&dev->dev, "Enabling sensors\n");
  817. if (PCIBIOS_SUCCESSFUL !=
  818. pci_write_config_word(dev, VIA686A_ENABLE_REG,
  819. val | 0x0001))
  820. return -ENODEV;
  821. }
  822. if (platform_driver_register(&via686a_driver))
  823. goto exit;
  824. /* Sets global pdev as a side effect */
  825. if (via686a_device_add(address))
  826. goto exit_unregister;
  827. /*
  828. * Always return failure here. This is to allow other drivers to bind
  829. * to this pci device. We don't really want to have control over the
  830. * pci device, we only wanted to read as few register values from it.
  831. */
  832. s_bridge = pci_dev_get(dev);
  833. return -ENODEV;
  834. exit_unregister:
  835. platform_driver_unregister(&via686a_driver);
  836. exit:
  837. return -ENODEV;
  838. }
  839. static struct pci_driver via686a_pci_driver = {
  840. .name = "via686a",
  841. .id_table = via686a_pci_ids,
  842. .probe = via686a_pci_probe,
  843. };
  844. static int __init sm_via686a_init(void)
  845. {
  846. return pci_register_driver(&via686a_pci_driver);
  847. }
  848. static void __exit sm_via686a_exit(void)
  849. {
  850. pci_unregister_driver(&via686a_pci_driver);
  851. if (s_bridge != NULL) {
  852. platform_device_unregister(pdev);
  853. platform_driver_unregister(&via686a_driver);
  854. pci_dev_put(s_bridge);
  855. s_bridge = NULL;
  856. }
  857. }
  858. MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>, "
  859. "Mark Studebaker <mdsxyz123@yahoo.com> "
  860. "and Bob Dougherty <bobd@stanford.edu>");
  861. MODULE_DESCRIPTION("VIA 686A Sensor device");
  862. MODULE_LICENSE("GPL");
  863. module_init(sm_via686a_init);
  864. module_exit(sm_via686a_exit);