via686a.c 31 KB

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