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