via686a.c 31 KB

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