headers_check fix: x86, swab.h
[linux-2.6/mini2440.git] / drivers / hwmon / vt8231.c
blob9982b45fbb1488aa1160683ada5f92cac4c88f24
1 /*
2 vt8231.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
5 Copyright (c) 2005 Roger Lucas <vt8231@hiddenengine.co.uk>
6 Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
7 Aaron M. Marsh <amarsh@sdf.lonestar.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (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.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* Supports VIA VT8231 South Bridge embedded sensors
27 #include <linux/module.h>
28 #include <linux/init.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/hwmon-vid.h>
36 #include <linux/err.h>
37 #include <linux/mutex.h>
38 #include <linux/acpi.h>
39 #include <asm/io.h>
41 static int force_addr;
42 module_param(force_addr, int, 0);
43 MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
45 static struct platform_device *pdev;
47 #define VT8231_EXTENT 0x80
48 #define VT8231_BASE_REG 0x70
49 #define VT8231_ENABLE_REG 0x74
51 /* The VT8231 registers
53 The reset value for the input channel configuration is used (Reg 0x4A=0x07)
54 which sets the selected inputs marked with '*' below if multiple options are
55 possible:
57 Voltage Mode Temperature Mode
58 Sensor Linux Id Linux Id VIA Id
59 -------- -------- -------- ------
60 CPU Diode N/A temp1 0
61 UIC1 in0 temp2 * 1
62 UIC2 in1 * temp3 2
63 UIC3 in2 * temp4 3
64 UIC4 in3 * temp5 4
65 UIC5 in4 * temp6 5
66 3.3V in5 N/A
68 Note that the BIOS may set the configuration register to a different value
69 to match the motherboard configuration.
72 /* fans numbered 0-1 */
73 #define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
74 #define VT8231_REG_FAN(nr) (0x29 + (nr))
76 /* Voltage inputs numbered 0-5 */
78 static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
79 static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
80 static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
82 /* Temperatures are numbered 1-6 according to the Linux kernel specification.
84 ** In the VIA datasheet, however, the temperatures are numbered from zero.
85 ** Since it is important that this driver can easily be compared to the VIA
86 ** datasheet, we will use the VIA numbering within this driver and map the
87 ** kernel sysfs device name to the VIA number in the sysfs callback.
90 #define VT8231_REG_TEMP_LOW01 0x49
91 #define VT8231_REG_TEMP_LOW25 0x4d
93 static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
94 static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
95 static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
97 #define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
98 #define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
99 #define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
101 #define VT8231_REG_CONFIG 0x40
102 #define VT8231_REG_ALARM1 0x41
103 #define VT8231_REG_ALARM2 0x42
104 #define VT8231_REG_FANDIV 0x47
105 #define VT8231_REG_UCH_CONFIG 0x4a
106 #define VT8231_REG_TEMP1_CONFIG 0x4b
107 #define VT8231_REG_TEMP2_CONFIG 0x4c
109 /* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
110 ** numbering
112 #define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
113 ((ch_config) >> ((i)+1)) & 0x01)
114 /* voltages 0-5 */
115 #define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
116 !(((ch_config) >> ((i)+2)) & 0x01))
118 #define DIV_FROM_REG(val) (1 << (val))
120 /* NB The values returned here are NOT temperatures. The calibration curves
121 ** for the thermistor curves are board-specific and must go in the
122 ** sensors.conf file. Temperature sensors are actually ten bits, but the
123 ** VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
124 ** register. The temperature value returned should have a magnitude of 3,
125 ** so we use the VIA scaling as the "true" scaling and use the remaining 2
126 ** LSBs as fractional precision.
128 ** All the on-chip hardware temperature comparisons for the alarms are only
129 ** 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
130 ** in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
131 ** ignored.
134 /******** FAN RPM CONVERSIONS ********
135 ** This chip saturates back at 0, not at 255 like many the other chips.
136 ** So, 0 means 0 RPM
138 static inline u8 FAN_TO_REG(long rpm, int div)
140 if (rpm == 0)
141 return 0;
142 return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
145 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
147 struct vt8231_data {
148 unsigned short addr;
149 const char *name;
151 struct mutex update_lock;
152 struct device *hwmon_dev;
153 char valid; /* !=0 if following fields are valid */
154 unsigned long last_updated; /* In jiffies */
156 u8 in[6]; /* Register value */
157 u8 in_max[6]; /* Register value */
158 u8 in_min[6]; /* Register value */
159 u16 temp[6]; /* Register value 10 bit, right aligned */
160 u8 temp_max[6]; /* Register value */
161 u8 temp_min[6]; /* Register value */
162 u8 fan[2]; /* Register value */
163 u8 fan_min[2]; /* Register value */
164 u8 fan_div[2]; /* Register encoding, shifted right */
165 u16 alarms; /* Register encoding */
166 u8 uch_config;
169 static struct pci_dev *s_bridge;
170 static int vt8231_probe(struct platform_device *pdev);
171 static int __devexit vt8231_remove(struct platform_device *pdev);
172 static struct vt8231_data *vt8231_update_device(struct device *dev);
173 static void vt8231_init_device(struct vt8231_data *data);
175 static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
177 return inb_p(data->addr + reg);
180 static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
181 u8 value)
183 outb_p(value, data->addr + reg);
186 /* following are the sysfs callback functions */
187 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
188 char *buf)
190 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
191 int nr = sensor_attr->index;
192 struct vt8231_data *data = vt8231_update_device(dev);
194 return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
197 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
198 char *buf)
200 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
201 int nr = sensor_attr->index;
202 struct vt8231_data *data = vt8231_update_device(dev);
204 return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
207 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
208 char *buf)
210 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
211 int nr = sensor_attr->index;
212 struct vt8231_data *data = vt8231_update_device(dev);
214 return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
217 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
218 const char *buf, size_t count)
220 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
221 int nr = sensor_attr->index;
222 struct vt8231_data *data = dev_get_drvdata(dev);
223 unsigned long val = simple_strtoul(buf, NULL, 10);
225 mutex_lock(&data->update_lock);
226 data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
227 vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
228 mutex_unlock(&data->update_lock);
229 return count;
232 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
233 const char *buf, size_t count)
235 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
236 int nr = sensor_attr->index;
237 struct vt8231_data *data = dev_get_drvdata(dev);
238 unsigned long val = simple_strtoul(buf, NULL, 10);
240 mutex_lock(&data->update_lock);
241 data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
242 vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
243 mutex_unlock(&data->update_lock);
244 return count;
247 /* Special case for input 5 as this has 3.3V scaling built into the chip */
248 static ssize_t show_in5(struct device *dev, struct device_attribute *attr,
249 char *buf)
251 struct vt8231_data *data = vt8231_update_device(dev);
253 return sprintf(buf, "%d\n",
254 (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
257 static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr,
258 char *buf)
260 struct vt8231_data *data = vt8231_update_device(dev);
262 return sprintf(buf, "%d\n",
263 (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
266 static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr,
267 char *buf)
269 struct vt8231_data *data = vt8231_update_device(dev);
271 return sprintf(buf, "%d\n",
272 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
275 static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t count)
278 struct vt8231_data *data = dev_get_drvdata(dev);
279 unsigned long val = simple_strtoul(buf, NULL, 10);
281 mutex_lock(&data->update_lock);
282 data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
283 0, 255);
284 vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
285 mutex_unlock(&data->update_lock);
286 return count;
289 static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr,
290 const char *buf, size_t count)
292 struct vt8231_data *data = dev_get_drvdata(dev);
293 unsigned long val = simple_strtoul(buf, NULL, 10);
295 mutex_lock(&data->update_lock);
296 data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
297 0, 255);
298 vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
299 mutex_unlock(&data->update_lock);
300 return count;
303 #define define_voltage_sysfs(offset) \
304 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
305 show_in, NULL, offset); \
306 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
307 show_in_min, set_in_min, offset); \
308 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
309 show_in_max, set_in_max, offset)
311 define_voltage_sysfs(0);
312 define_voltage_sysfs(1);
313 define_voltage_sysfs(2);
314 define_voltage_sysfs(3);
315 define_voltage_sysfs(4);
317 static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL);
318 static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min);
319 static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max);
321 /* Temperatures */
322 static ssize_t show_temp0(struct device *dev, struct device_attribute *attr,
323 char *buf)
325 struct vt8231_data *data = vt8231_update_device(dev);
326 return sprintf(buf, "%d\n", data->temp[0] * 250);
329 static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr,
330 char *buf)
332 struct vt8231_data *data = vt8231_update_device(dev);
333 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
336 static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr,
337 char *buf)
339 struct vt8231_data *data = vt8231_update_device(dev);
340 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
343 static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr,
344 const char *buf, size_t count)
346 struct vt8231_data *data = dev_get_drvdata(dev);
347 int val = simple_strtol(buf, NULL, 10);
349 mutex_lock(&data->update_lock);
350 data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
351 vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
352 mutex_unlock(&data->update_lock);
353 return count;
355 static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
356 const char *buf, size_t count)
358 struct vt8231_data *data = dev_get_drvdata(dev);
359 int val = simple_strtol(buf, NULL, 10);
361 mutex_lock(&data->update_lock);
362 data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
363 vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
364 mutex_unlock(&data->update_lock);
365 return count;
368 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
369 char *buf)
371 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
372 int nr = sensor_attr->index;
373 struct vt8231_data *data = vt8231_update_device(dev);
374 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
377 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
378 char *buf)
380 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
381 int nr = sensor_attr->index;
382 struct vt8231_data *data = vt8231_update_device(dev);
383 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
386 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
387 char *buf)
389 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
390 int nr = sensor_attr->index;
391 struct vt8231_data *data = vt8231_update_device(dev);
392 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
395 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
396 const char *buf, size_t count)
398 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
399 int nr = sensor_attr->index;
400 struct vt8231_data *data = dev_get_drvdata(dev);
401 int val = simple_strtol(buf, NULL, 10);
403 mutex_lock(&data->update_lock);
404 data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
405 vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
406 mutex_unlock(&data->update_lock);
407 return count;
409 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
410 const char *buf, size_t count)
412 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
413 int nr = sensor_attr->index;
414 struct vt8231_data *data = dev_get_drvdata(dev);
415 int val = simple_strtol(buf, NULL, 10);
417 mutex_lock(&data->update_lock);
418 data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
419 vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
420 mutex_unlock(&data->update_lock);
421 return count;
424 /* Note that these map the Linux temperature sensor numbering (1-6) to the VIA
425 ** temperature sensor numbering (0-5)
427 #define define_temperature_sysfs(offset) \
428 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
429 show_temp, NULL, offset - 1); \
430 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
431 show_temp_max, set_temp_max, offset - 1); \
432 static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
433 show_temp_min, set_temp_min, offset - 1)
435 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL);
436 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max);
437 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp0_min, set_temp0_min);
439 define_temperature_sysfs(2);
440 define_temperature_sysfs(3);
441 define_temperature_sysfs(4);
442 define_temperature_sysfs(5);
443 define_temperature_sysfs(6);
445 /* Fans */
446 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
447 char *buf)
449 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
450 int nr = sensor_attr->index;
451 struct vt8231_data *data = vt8231_update_device(dev);
452 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
453 DIV_FROM_REG(data->fan_div[nr])));
456 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
457 char *buf)
459 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
460 int nr = sensor_attr->index;
461 struct vt8231_data *data = vt8231_update_device(dev);
462 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
463 DIV_FROM_REG(data->fan_div[nr])));
466 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
467 char *buf)
469 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
470 int nr = sensor_attr->index;
471 struct vt8231_data *data = vt8231_update_device(dev);
472 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
475 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
476 const char *buf, size_t count)
478 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
479 int nr = sensor_attr->index;
480 struct vt8231_data *data = dev_get_drvdata(dev);
481 int val = simple_strtoul(buf, NULL, 10);
483 mutex_lock(&data->update_lock);
484 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
485 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
486 mutex_unlock(&data->update_lock);
487 return count;
490 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
491 const char *buf, size_t count)
493 struct vt8231_data *data = dev_get_drvdata(dev);
494 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
495 unsigned long val = simple_strtoul(buf, NULL, 10);
496 int nr = sensor_attr->index;
497 int old = vt8231_read_value(data, VT8231_REG_FANDIV);
498 long min = FAN_FROM_REG(data->fan_min[nr],
499 DIV_FROM_REG(data->fan_div[nr]));
501 mutex_lock(&data->update_lock);
502 switch (val) {
503 case 1: data->fan_div[nr] = 0; break;
504 case 2: data->fan_div[nr] = 1; break;
505 case 4: data->fan_div[nr] = 2; break;
506 case 8: data->fan_div[nr] = 3; break;
507 default:
508 dev_err(dev, "fan_div value %ld not supported. "
509 "Choose one of 1, 2, 4 or 8!\n", val);
510 mutex_unlock(&data->update_lock);
511 return -EINVAL;
514 /* Correct the fan minimum speed */
515 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
516 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
518 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
519 vt8231_write_value(data, VT8231_REG_FANDIV, old);
520 mutex_unlock(&data->update_lock);
521 return count;
525 #define define_fan_sysfs(offset) \
526 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
527 show_fan, NULL, offset - 1); \
528 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
529 show_fan_div, set_fan_div, offset - 1); \
530 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
531 show_fan_min, set_fan_min, offset - 1)
533 define_fan_sysfs(1);
534 define_fan_sysfs(2);
536 /* Alarms */
537 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
538 char *buf)
540 struct vt8231_data *data = vt8231_update_device(dev);
541 return sprintf(buf, "%d\n", data->alarms);
543 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
545 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
546 char *buf)
548 int bitnr = to_sensor_dev_attr(attr)->index;
549 struct vt8231_data *data = vt8231_update_device(dev);
550 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
552 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
553 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 11);
554 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0);
555 static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL, 1);
556 static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL, 3);
557 static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL, 8);
558 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 11);
559 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0);
560 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 1);
561 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
562 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
563 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 2);
564 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
565 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
567 static ssize_t show_name(struct device *dev, struct device_attribute
568 *devattr, char *buf)
570 struct vt8231_data *data = dev_get_drvdata(dev);
571 return sprintf(buf, "%s\n", data->name);
573 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
575 static struct attribute *vt8231_attributes_temps[6][5] = {
577 &dev_attr_temp1_input.attr,
578 &dev_attr_temp1_max_hyst.attr,
579 &dev_attr_temp1_max.attr,
580 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
581 NULL
582 }, {
583 &sensor_dev_attr_temp2_input.dev_attr.attr,
584 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
585 &sensor_dev_attr_temp2_max.dev_attr.attr,
586 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
587 NULL
588 }, {
589 &sensor_dev_attr_temp3_input.dev_attr.attr,
590 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
591 &sensor_dev_attr_temp3_max.dev_attr.attr,
592 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
593 NULL
594 }, {
595 &sensor_dev_attr_temp4_input.dev_attr.attr,
596 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
597 &sensor_dev_attr_temp4_max.dev_attr.attr,
598 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
599 NULL
600 }, {
601 &sensor_dev_attr_temp5_input.dev_attr.attr,
602 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
603 &sensor_dev_attr_temp5_max.dev_attr.attr,
604 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
605 NULL
606 }, {
607 &sensor_dev_attr_temp6_input.dev_attr.attr,
608 &sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
609 &sensor_dev_attr_temp6_max.dev_attr.attr,
610 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
611 NULL
615 static const struct attribute_group vt8231_group_temps[6] = {
616 { .attrs = vt8231_attributes_temps[0] },
617 { .attrs = vt8231_attributes_temps[1] },
618 { .attrs = vt8231_attributes_temps[2] },
619 { .attrs = vt8231_attributes_temps[3] },
620 { .attrs = vt8231_attributes_temps[4] },
621 { .attrs = vt8231_attributes_temps[5] },
624 static struct attribute *vt8231_attributes_volts[6][5] = {
626 &sensor_dev_attr_in0_input.dev_attr.attr,
627 &sensor_dev_attr_in0_min.dev_attr.attr,
628 &sensor_dev_attr_in0_max.dev_attr.attr,
629 &sensor_dev_attr_in0_alarm.dev_attr.attr,
630 NULL
631 }, {
632 &sensor_dev_attr_in1_input.dev_attr.attr,
633 &sensor_dev_attr_in1_min.dev_attr.attr,
634 &sensor_dev_attr_in1_max.dev_attr.attr,
635 &sensor_dev_attr_in1_alarm.dev_attr.attr,
636 NULL
637 }, {
638 &sensor_dev_attr_in2_input.dev_attr.attr,
639 &sensor_dev_attr_in2_min.dev_attr.attr,
640 &sensor_dev_attr_in2_max.dev_attr.attr,
641 &sensor_dev_attr_in2_alarm.dev_attr.attr,
642 NULL
643 }, {
644 &sensor_dev_attr_in3_input.dev_attr.attr,
645 &sensor_dev_attr_in3_min.dev_attr.attr,
646 &sensor_dev_attr_in3_max.dev_attr.attr,
647 &sensor_dev_attr_in3_alarm.dev_attr.attr,
648 NULL
649 }, {
650 &sensor_dev_attr_in4_input.dev_attr.attr,
651 &sensor_dev_attr_in4_min.dev_attr.attr,
652 &sensor_dev_attr_in4_max.dev_attr.attr,
653 &sensor_dev_attr_in4_alarm.dev_attr.attr,
654 NULL
655 }, {
656 &dev_attr_in5_input.attr,
657 &dev_attr_in5_min.attr,
658 &dev_attr_in5_max.attr,
659 &sensor_dev_attr_in5_alarm.dev_attr.attr,
660 NULL
664 static const struct attribute_group vt8231_group_volts[6] = {
665 { .attrs = vt8231_attributes_volts[0] },
666 { .attrs = vt8231_attributes_volts[1] },
667 { .attrs = vt8231_attributes_volts[2] },
668 { .attrs = vt8231_attributes_volts[3] },
669 { .attrs = vt8231_attributes_volts[4] },
670 { .attrs = vt8231_attributes_volts[5] },
673 static struct attribute *vt8231_attributes[] = {
674 &sensor_dev_attr_fan1_input.dev_attr.attr,
675 &sensor_dev_attr_fan2_input.dev_attr.attr,
676 &sensor_dev_attr_fan1_min.dev_attr.attr,
677 &sensor_dev_attr_fan2_min.dev_attr.attr,
678 &sensor_dev_attr_fan1_div.dev_attr.attr,
679 &sensor_dev_attr_fan2_div.dev_attr.attr,
680 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
681 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
682 &dev_attr_alarms.attr,
683 &dev_attr_name.attr,
684 NULL
687 static const struct attribute_group vt8231_group = {
688 .attrs = vt8231_attributes,
691 static struct platform_driver vt8231_driver = {
692 .driver = {
693 .owner = THIS_MODULE,
694 .name = "vt8231",
696 .probe = vt8231_probe,
697 .remove = __devexit_p(vt8231_remove),
700 static struct pci_device_id vt8231_pci_ids[] = {
701 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
702 { 0, }
705 MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
707 static int __devinit vt8231_pci_probe(struct pci_dev *dev,
708 const struct pci_device_id *id);
710 static struct pci_driver vt8231_pci_driver = {
711 .name = "vt8231",
712 .id_table = vt8231_pci_ids,
713 .probe = vt8231_pci_probe,
716 static int vt8231_probe(struct platform_device *pdev)
718 struct resource *res;
719 struct vt8231_data *data;
720 int err = 0, i;
722 /* Reserve the ISA region */
723 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
724 if (!request_region(res->start, VT8231_EXTENT,
725 vt8231_driver.driver.name)) {
726 dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
727 (unsigned long)res->start, (unsigned long)res->end);
728 return -ENODEV;
731 if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
732 err = -ENOMEM;
733 goto exit_release;
736 platform_set_drvdata(pdev, data);
737 data->addr = res->start;
738 data->name = "vt8231";
740 mutex_init(&data->update_lock);
741 vt8231_init_device(data);
743 /* Register sysfs hooks */
744 if ((err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group)))
745 goto exit_free;
747 /* Must update device information to find out the config field */
748 data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
750 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
751 if (ISTEMP(i, data->uch_config)) {
752 if ((err = sysfs_create_group(&pdev->dev.kobj,
753 &vt8231_group_temps[i])))
754 goto exit_remove_files;
758 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
759 if (ISVOLT(i, data->uch_config)) {
760 if ((err = sysfs_create_group(&pdev->dev.kobj,
761 &vt8231_group_volts[i])))
762 goto exit_remove_files;
766 data->hwmon_dev = hwmon_device_register(&pdev->dev);
767 if (IS_ERR(data->hwmon_dev)) {
768 err = PTR_ERR(data->hwmon_dev);
769 goto exit_remove_files;
771 return 0;
773 exit_remove_files:
774 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
775 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
777 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
778 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
780 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
782 exit_free:
783 platform_set_drvdata(pdev, NULL);
784 kfree(data);
786 exit_release:
787 release_region(res->start, VT8231_EXTENT);
788 return err;
791 static int __devexit vt8231_remove(struct platform_device *pdev)
793 struct vt8231_data *data = platform_get_drvdata(pdev);
794 int i;
796 hwmon_device_unregister(data->hwmon_dev);
798 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
799 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
801 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
802 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
804 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
806 release_region(data->addr, VT8231_EXTENT);
807 platform_set_drvdata(pdev, NULL);
808 kfree(data);
809 return 0;
812 static void vt8231_init_device(struct vt8231_data *data)
814 vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
815 vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
818 static struct vt8231_data *vt8231_update_device(struct device *dev)
820 struct vt8231_data *data = dev_get_drvdata(dev);
821 int i;
822 u16 low;
824 mutex_lock(&data->update_lock);
826 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
827 || !data->valid) {
828 for (i = 0; i < 6; i++) {
829 if (ISVOLT(i, data->uch_config)) {
830 data->in[i] = vt8231_read_value(data,
831 regvolt[i]);
832 data->in_min[i] = vt8231_read_value(data,
833 regvoltmin[i]);
834 data->in_max[i] = vt8231_read_value(data,
835 regvoltmax[i]);
838 for (i = 0; i < 2; i++) {
839 data->fan[i] = vt8231_read_value(data,
840 VT8231_REG_FAN(i));
841 data->fan_min[i] = vt8231_read_value(data,
842 VT8231_REG_FAN_MIN(i));
845 low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
846 low = (low >> 6) | ((low & 0x30) >> 2)
847 | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
848 for (i = 0; i < 6; i++) {
849 if (ISTEMP(i, data->uch_config)) {
850 data->temp[i] = (vt8231_read_value(data,
851 regtemp[i]) << 2)
852 | ((low >> (2 * i)) & 0x03);
853 data->temp_max[i] = vt8231_read_value(data,
854 regtempmax[i]);
855 data->temp_min[i] = vt8231_read_value(data,
856 regtempmin[i]);
860 i = vt8231_read_value(data, VT8231_REG_FANDIV);
861 data->fan_div[0] = (i >> 4) & 0x03;
862 data->fan_div[1] = i >> 6;
863 data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
864 (vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
866 /* Set alarm flags correctly */
867 if (!data->fan[0] && data->fan_min[0]) {
868 data->alarms |= 0x40;
869 } else if (data->fan[0] && !data->fan_min[0]) {
870 data->alarms &= ~0x40;
873 if (!data->fan[1] && data->fan_min[1]) {
874 data->alarms |= 0x80;
875 } else if (data->fan[1] && !data->fan_min[1]) {
876 data->alarms &= ~0x80;
879 data->last_updated = jiffies;
880 data->valid = 1;
883 mutex_unlock(&data->update_lock);
885 return data;
888 static int __devinit vt8231_device_add(unsigned short address)
890 struct resource res = {
891 .start = address,
892 .end = address + VT8231_EXTENT - 1,
893 .name = "vt8231",
894 .flags = IORESOURCE_IO,
896 int err;
898 err = acpi_check_resource_conflict(&res);
899 if (err)
900 goto exit;
902 pdev = platform_device_alloc("vt8231", address);
903 if (!pdev) {
904 err = -ENOMEM;
905 printk(KERN_ERR "vt8231: Device allocation failed\n");
906 goto exit;
909 err = platform_device_add_resources(pdev, &res, 1);
910 if (err) {
911 printk(KERN_ERR "vt8231: Device resource addition failed "
912 "(%d)\n", err);
913 goto exit_device_put;
916 err = platform_device_add(pdev);
917 if (err) {
918 printk(KERN_ERR "vt8231: Device addition failed (%d)\n",
919 err);
920 goto exit_device_put;
923 return 0;
925 exit_device_put:
926 platform_device_put(pdev);
927 exit:
928 return err;
931 static int __devinit vt8231_pci_probe(struct pci_dev *dev,
932 const struct pci_device_id *id)
934 u16 address, val;
935 if (force_addr) {
936 address = force_addr & 0xff00;
937 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
938 address);
940 if (PCIBIOS_SUCCESSFUL !=
941 pci_write_config_word(dev, VT8231_BASE_REG, address | 1))
942 return -ENODEV;
945 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
946 &val))
947 return -ENODEV;
949 address = val & ~(VT8231_EXTENT - 1);
950 if (address == 0) {
951 dev_err(&dev->dev, "base address not set -\
952 upgrade BIOS or use force_addr=0xaddr\n");
953 return -ENODEV;
956 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG,
957 &val))
958 return -ENODEV;
960 if (!(val & 0x0001)) {
961 dev_warn(&dev->dev, "enabling sensors\n");
962 if (PCIBIOS_SUCCESSFUL !=
963 pci_write_config_word(dev, VT8231_ENABLE_REG,
964 val | 0x0001))
965 return -ENODEV;
968 if (platform_driver_register(&vt8231_driver))
969 goto exit;
971 /* Sets global pdev as a side effect */
972 if (vt8231_device_add(address))
973 goto exit_unregister;
975 /* Always return failure here. This is to allow other drivers to bind
976 * to this pci device. We don't really want to have control over the
977 * pci device, we only wanted to read as few register values from it.
980 /* We do, however, mark ourselves as using the PCI device to stop it
981 getting unloaded. */
982 s_bridge = pci_dev_get(dev);
983 return -ENODEV;
985 exit_unregister:
986 platform_driver_unregister(&vt8231_driver);
987 exit:
988 return -ENODEV;
991 static int __init sm_vt8231_init(void)
993 return pci_register_driver(&vt8231_pci_driver);
996 static void __exit sm_vt8231_exit(void)
998 pci_unregister_driver(&vt8231_pci_driver);
999 if (s_bridge != NULL) {
1000 platform_device_unregister(pdev);
1001 platform_driver_unregister(&vt8231_driver);
1002 pci_dev_put(s_bridge);
1003 s_bridge = NULL;
1007 MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>");
1008 MODULE_DESCRIPTION("VT8231 sensors");
1009 MODULE_LICENSE("GPL");
1011 module_init(sm_vt8231_init);
1012 module_exit(sm_vt8231_exit);