revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / drivers / hwmon / vt8231.c
blob2cd005a5e3f1ed9d87c71dde75f18a667ac34110
1 /*
2 vt8231.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
5 Copyright (c) 2005 Roger Lucas <roger@planbit.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_name(struct device *dev, struct device_attribute
546 *devattr, char *buf)
548 struct vt8231_data *data = dev_get_drvdata(dev);
549 return sprintf(buf, "%s\n", data->name);
551 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
553 static struct attribute *vt8231_attributes_temps[6][4] = {
555 &dev_attr_temp1_input.attr,
556 &dev_attr_temp1_max_hyst.attr,
557 &dev_attr_temp1_max.attr,
558 NULL
559 }, {
560 &sensor_dev_attr_temp2_input.dev_attr.attr,
561 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
562 &sensor_dev_attr_temp2_max.dev_attr.attr,
563 NULL
564 }, {
565 &sensor_dev_attr_temp3_input.dev_attr.attr,
566 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
567 &sensor_dev_attr_temp3_max.dev_attr.attr,
568 NULL
569 }, {
570 &sensor_dev_attr_temp4_input.dev_attr.attr,
571 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
572 &sensor_dev_attr_temp4_max.dev_attr.attr,
573 NULL
574 }, {
575 &sensor_dev_attr_temp5_input.dev_attr.attr,
576 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
577 &sensor_dev_attr_temp5_max.dev_attr.attr,
578 NULL
579 }, {
580 &sensor_dev_attr_temp6_input.dev_attr.attr,
581 &sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
582 &sensor_dev_attr_temp6_max.dev_attr.attr,
583 NULL
587 static const struct attribute_group vt8231_group_temps[6] = {
588 { .attrs = vt8231_attributes_temps[0] },
589 { .attrs = vt8231_attributes_temps[1] },
590 { .attrs = vt8231_attributes_temps[2] },
591 { .attrs = vt8231_attributes_temps[3] },
592 { .attrs = vt8231_attributes_temps[4] },
593 { .attrs = vt8231_attributes_temps[5] },
596 static struct attribute *vt8231_attributes_volts[6][4] = {
598 &sensor_dev_attr_in0_input.dev_attr.attr,
599 &sensor_dev_attr_in0_min.dev_attr.attr,
600 &sensor_dev_attr_in0_max.dev_attr.attr,
601 NULL
602 }, {
603 &sensor_dev_attr_in1_input.dev_attr.attr,
604 &sensor_dev_attr_in1_min.dev_attr.attr,
605 &sensor_dev_attr_in1_max.dev_attr.attr,
606 NULL
607 }, {
608 &sensor_dev_attr_in2_input.dev_attr.attr,
609 &sensor_dev_attr_in2_min.dev_attr.attr,
610 &sensor_dev_attr_in2_max.dev_attr.attr,
611 NULL
612 }, {
613 &sensor_dev_attr_in3_input.dev_attr.attr,
614 &sensor_dev_attr_in3_min.dev_attr.attr,
615 &sensor_dev_attr_in3_max.dev_attr.attr,
616 NULL
617 }, {
618 &sensor_dev_attr_in4_input.dev_attr.attr,
619 &sensor_dev_attr_in4_min.dev_attr.attr,
620 &sensor_dev_attr_in4_max.dev_attr.attr,
621 NULL
622 }, {
623 &dev_attr_in5_input.attr,
624 &dev_attr_in5_min.attr,
625 &dev_attr_in5_max.attr,
626 NULL
630 static const struct attribute_group vt8231_group_volts[6] = {
631 { .attrs = vt8231_attributes_volts[0] },
632 { .attrs = vt8231_attributes_volts[1] },
633 { .attrs = vt8231_attributes_volts[2] },
634 { .attrs = vt8231_attributes_volts[3] },
635 { .attrs = vt8231_attributes_volts[4] },
636 { .attrs = vt8231_attributes_volts[5] },
639 static struct attribute *vt8231_attributes[] = {
640 &sensor_dev_attr_fan1_input.dev_attr.attr,
641 &sensor_dev_attr_fan2_input.dev_attr.attr,
642 &sensor_dev_attr_fan1_min.dev_attr.attr,
643 &sensor_dev_attr_fan2_min.dev_attr.attr,
644 &sensor_dev_attr_fan1_div.dev_attr.attr,
645 &sensor_dev_attr_fan2_div.dev_attr.attr,
646 &dev_attr_alarms.attr,
647 &dev_attr_name.attr,
648 NULL
651 static const struct attribute_group vt8231_group = {
652 .attrs = vt8231_attributes,
655 static struct platform_driver vt8231_driver = {
656 .driver = {
657 .owner = THIS_MODULE,
658 .name = "vt8231",
660 .probe = vt8231_probe,
661 .remove = __devexit_p(vt8231_remove),
664 static struct pci_device_id vt8231_pci_ids[] = {
665 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
666 { 0, }
669 MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
671 static int __devinit vt8231_pci_probe(struct pci_dev *dev,
672 const struct pci_device_id *id);
674 static struct pci_driver vt8231_pci_driver = {
675 .name = "vt8231",
676 .id_table = vt8231_pci_ids,
677 .probe = vt8231_pci_probe,
680 static int vt8231_probe(struct platform_device *pdev)
682 struct resource *res;
683 struct vt8231_data *data;
684 int err = 0, i;
686 /* Reserve the ISA region */
687 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
688 if (!request_region(res->start, VT8231_EXTENT,
689 vt8231_driver.driver.name)) {
690 dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
691 (unsigned long)res->start, (unsigned long)res->end);
692 return -ENODEV;
695 if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
696 err = -ENOMEM;
697 goto exit_release;
700 platform_set_drvdata(pdev, data);
701 data->addr = res->start;
702 data->name = "vt8231";
704 mutex_init(&data->update_lock);
705 vt8231_init_device(data);
707 /* Register sysfs hooks */
708 if ((err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group)))
709 goto exit_free;
711 /* Must update device information to find out the config field */
712 data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
714 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
715 if (ISTEMP(i, data->uch_config)) {
716 if ((err = sysfs_create_group(&pdev->dev.kobj,
717 &vt8231_group_temps[i])))
718 goto exit_remove_files;
722 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
723 if (ISVOLT(i, data->uch_config)) {
724 if ((err = sysfs_create_group(&pdev->dev.kobj,
725 &vt8231_group_volts[i])))
726 goto exit_remove_files;
730 data->hwmon_dev = hwmon_device_register(&pdev->dev);
731 if (IS_ERR(data->hwmon_dev)) {
732 err = PTR_ERR(data->hwmon_dev);
733 goto exit_remove_files;
735 return 0;
737 exit_remove_files:
738 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
739 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
741 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
742 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
744 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
746 exit_free:
747 platform_set_drvdata(pdev, NULL);
748 kfree(data);
750 exit_release:
751 release_region(res->start, VT8231_EXTENT);
752 return err;
755 static int __devexit vt8231_remove(struct platform_device *pdev)
757 struct vt8231_data *data = platform_get_drvdata(pdev);
758 int i;
760 hwmon_device_unregister(data->hwmon_dev);
762 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
763 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
765 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
766 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
768 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
770 release_region(data->addr, VT8231_EXTENT);
771 platform_set_drvdata(pdev, NULL);
772 kfree(data);
773 return 0;
776 static void vt8231_init_device(struct vt8231_data *data)
778 vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
779 vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
782 static struct vt8231_data *vt8231_update_device(struct device *dev)
784 struct vt8231_data *data = dev_get_drvdata(dev);
785 int i;
786 u16 low;
788 mutex_lock(&data->update_lock);
790 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
791 || !data->valid) {
792 for (i = 0; i < 6; i++) {
793 if (ISVOLT(i, data->uch_config)) {
794 data->in[i] = vt8231_read_value(data,
795 regvolt[i]);
796 data->in_min[i] = vt8231_read_value(data,
797 regvoltmin[i]);
798 data->in_max[i] = vt8231_read_value(data,
799 regvoltmax[i]);
802 for (i = 0; i < 2; i++) {
803 data->fan[i] = vt8231_read_value(data,
804 VT8231_REG_FAN(i));
805 data->fan_min[i] = vt8231_read_value(data,
806 VT8231_REG_FAN_MIN(i));
809 low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
810 low = (low >> 6) | ((low & 0x30) >> 2)
811 | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
812 for (i = 0; i < 6; i++) {
813 if (ISTEMP(i, data->uch_config)) {
814 data->temp[i] = (vt8231_read_value(data,
815 regtemp[i]) << 2)
816 | ((low >> (2 * i)) & 0x03);
817 data->temp_max[i] = vt8231_read_value(data,
818 regtempmax[i]);
819 data->temp_min[i] = vt8231_read_value(data,
820 regtempmin[i]);
824 i = vt8231_read_value(data, VT8231_REG_FANDIV);
825 data->fan_div[0] = (i >> 4) & 0x03;
826 data->fan_div[1] = i >> 6;
827 data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
828 (vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
830 /* Set alarm flags correctly */
831 if (!data->fan[0] && data->fan_min[0]) {
832 data->alarms |= 0x40;
833 } else if (data->fan[0] && !data->fan_min[0]) {
834 data->alarms &= ~0x40;
837 if (!data->fan[1] && data->fan_min[1]) {
838 data->alarms |= 0x80;
839 } else if (data->fan[1] && !data->fan_min[1]) {
840 data->alarms &= ~0x80;
843 data->last_updated = jiffies;
844 data->valid = 1;
847 mutex_unlock(&data->update_lock);
849 return data;
852 static int __devinit vt8231_device_add(unsigned short address)
854 struct resource res = {
855 .start = address,
856 .end = address + VT8231_EXTENT - 1,
857 .name = "vt8231",
858 .flags = IORESOURCE_IO,
860 int err;
862 err = acpi_check_resource_conflict(&res);
863 if (err)
864 goto exit;
866 pdev = platform_device_alloc("vt8231", address);
867 if (!pdev) {
868 err = -ENOMEM;
869 printk(KERN_ERR "vt8231: Device allocation failed\n");
870 goto exit;
873 err = platform_device_add_resources(pdev, &res, 1);
874 if (err) {
875 printk(KERN_ERR "vt8231: Device resource addition failed "
876 "(%d)\n", err);
877 goto exit_device_put;
880 err = platform_device_add(pdev);
881 if (err) {
882 printk(KERN_ERR "vt8231: Device addition failed (%d)\n",
883 err);
884 goto exit_device_put;
887 return 0;
889 exit_device_put:
890 platform_device_put(pdev);
891 exit:
892 return err;
895 static int __devinit vt8231_pci_probe(struct pci_dev *dev,
896 const struct pci_device_id *id)
898 u16 address, val;
899 if (force_addr) {
900 address = force_addr & 0xff00;
901 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
902 address);
904 if (PCIBIOS_SUCCESSFUL !=
905 pci_write_config_word(dev, VT8231_BASE_REG, address | 1))
906 return -ENODEV;
909 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
910 &val))
911 return -ENODEV;
913 address = val & ~(VT8231_EXTENT - 1);
914 if (address == 0) {
915 dev_err(&dev->dev, "base address not set -\
916 upgrade BIOS or use force_addr=0xaddr\n");
917 return -ENODEV;
920 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG,
921 &val))
922 return -ENODEV;
924 if (!(val & 0x0001)) {
925 dev_warn(&dev->dev, "enabling sensors\n");
926 if (PCIBIOS_SUCCESSFUL !=
927 pci_write_config_word(dev, VT8231_ENABLE_REG,
928 val | 0x0001))
929 return -ENODEV;
932 if (platform_driver_register(&vt8231_driver))
933 goto exit;
935 /* Sets global pdev as a side effect */
936 if (vt8231_device_add(address))
937 goto exit_unregister;
939 /* Always return failure here. This is to allow other drivers to bind
940 * to this pci device. We don't really want to have control over the
941 * pci device, we only wanted to read as few register values from it.
944 /* We do, however, mark ourselves as using the PCI device to stop it
945 getting unloaded. */
946 s_bridge = pci_dev_get(dev);
947 return -ENODEV;
949 exit_unregister:
950 platform_driver_unregister(&vt8231_driver);
951 exit:
952 return -ENODEV;
955 static int __init sm_vt8231_init(void)
957 return pci_register_driver(&vt8231_pci_driver);
960 static void __exit sm_vt8231_exit(void)
962 pci_unregister_driver(&vt8231_pci_driver);
963 if (s_bridge != NULL) {
964 platform_device_unregister(pdev);
965 platform_driver_unregister(&vt8231_driver);
966 pci_dev_put(s_bridge);
967 s_bridge = NULL;
971 MODULE_AUTHOR("Roger Lucas <roger@planbit.co.uk>");
972 MODULE_DESCRIPTION("VT8231 sensors");
973 MODULE_LICENSE("GPL");
975 module_init(sm_vt8231_init);
976 module_exit(sm_vt8231_exit);