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>
40 static int force_addr
;
41 module_param(force_addr
, int, 0);
42 MODULE_PARM_DESC(force_addr
, "Initialize the base address of the sensors");
44 static struct platform_device
*pdev
;
46 #define VT8231_EXTENT 0x80
47 #define VT8231_BASE_REG 0x70
48 #define VT8231_ENABLE_REG 0x74
50 /* The VT8231 registers
52 The reset value for the input channel configuration is used (Reg 0x4A=0x07)
53 which sets the selected inputs marked with '*' below if multiple options are
56 Voltage Mode Temperature Mode
57 Sensor Linux Id Linux Id VIA Id
58 -------- -------- -------- ------
67 Note that the BIOS may set the configuration register to a different value
68 to match the motherboard configuration.
71 /* fans numbered 0-1 */
72 #define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
73 #define VT8231_REG_FAN(nr) (0x29 + (nr))
75 /* Voltage inputs numbered 0-5 */
77 static const u8 regvolt
[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
78 static const u8 regvoltmax
[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
79 static const u8 regvoltmin
[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
81 /* Temperatures are numbered 1-6 according to the Linux kernel specification.
83 ** In the VIA datasheet, however, the temperatures are numbered from zero.
84 ** Since it is important that this driver can easily be compared to the VIA
85 ** datasheet, we will use the VIA numbering within this driver and map the
86 ** kernel sysfs device name to the VIA number in the sysfs callback.
89 #define VT8231_REG_TEMP_LOW01 0x49
90 #define VT8231_REG_TEMP_LOW25 0x4d
92 static const u8 regtemp
[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
93 static const u8 regtempmax
[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
94 static const u8 regtempmin
[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
96 #define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
97 #define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
98 #define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
100 #define VT8231_REG_CONFIG 0x40
101 #define VT8231_REG_ALARM1 0x41
102 #define VT8231_REG_ALARM2 0x42
103 #define VT8231_REG_FANDIV 0x47
104 #define VT8231_REG_UCH_CONFIG 0x4a
105 #define VT8231_REG_TEMP1_CONFIG 0x4b
106 #define VT8231_REG_TEMP2_CONFIG 0x4c
108 /* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
111 #define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
112 ((ch_config) >> ((i)+1)) & 0x01)
114 #define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
115 !(((ch_config) >> ((i)+2)) & 0x01))
117 #define DIV_FROM_REG(val) (1 << (val))
119 /* NB The values returned here are NOT temperatures. The calibration curves
120 ** for the thermistor curves are board-specific and must go in the
121 ** sensors.conf file. Temperature sensors are actually ten bits, but the
122 ** VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
123 ** register. The temperature value returned should have a magnitude of 3,
124 ** so we use the VIA scaling as the "true" scaling and use the remaining 2
125 ** LSBs as fractional precision.
127 ** All the on-chip hardware temperature comparisons for the alarms are only
128 ** 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
129 ** in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
133 /******** FAN RPM CONVERSIONS ********
134 ** This chip saturates back at 0, not at 255 like many the other chips.
137 static inline u8
FAN_TO_REG(long rpm
, int div
)
141 return SENSORS_LIMIT(1310720 / (rpm
* div
), 1, 255);
144 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
150 struct mutex update_lock
;
151 struct device
*hwmon_dev
;
152 char valid
; /* !=0 if following fields are valid */
153 unsigned long last_updated
; /* In jiffies */
155 u8 in
[6]; /* Register value */
156 u8 in_max
[6]; /* Register value */
157 u8 in_min
[6]; /* Register value */
158 u16 temp
[6]; /* Register value 10 bit, right aligned */
159 u8 temp_max
[6]; /* Register value */
160 u8 temp_min
[6]; /* Register value */
161 u8 fan
[2]; /* Register value */
162 u8 fan_min
[2]; /* Register value */
163 u8 fan_div
[2]; /* Register encoding, shifted right */
164 u16 alarms
; /* Register encoding */
168 static struct pci_dev
*s_bridge
;
169 static int vt8231_probe(struct platform_device
*pdev
);
170 static int __devexit
vt8231_remove(struct platform_device
*pdev
);
171 static struct vt8231_data
*vt8231_update_device(struct device
*dev
);
172 static void vt8231_init_device(struct vt8231_data
*data
);
174 static inline int vt8231_read_value(struct vt8231_data
*data
, u8 reg
)
176 return inb_p(data
->addr
+ reg
);
179 static inline void vt8231_write_value(struct vt8231_data
*data
, u8 reg
,
182 outb_p(value
, data
->addr
+ reg
);
185 /* following are the sysfs callback functions */
186 static ssize_t
show_in(struct device
*dev
, struct device_attribute
*attr
,
189 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
190 int nr
= sensor_attr
->index
;
191 struct vt8231_data
*data
= vt8231_update_device(dev
);
193 return sprintf(buf
, "%d\n", ((data
->in
[nr
] - 3) * 10000) / 958);
196 static ssize_t
show_in_min(struct device
*dev
, struct device_attribute
*attr
,
199 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
200 int nr
= sensor_attr
->index
;
201 struct vt8231_data
*data
= vt8231_update_device(dev
);
203 return sprintf(buf
, "%d\n", ((data
->in_min
[nr
] - 3) * 10000) / 958);
206 static ssize_t
show_in_max(struct device
*dev
, struct device_attribute
*attr
,
209 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
210 int nr
= sensor_attr
->index
;
211 struct vt8231_data
*data
= vt8231_update_device(dev
);
213 return sprintf(buf
, "%d\n", (((data
->in_max
[nr
] - 3) * 10000) / 958));
216 static ssize_t
set_in_min(struct device
*dev
, struct device_attribute
*attr
,
217 const char *buf
, size_t count
)
219 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
220 int nr
= sensor_attr
->index
;
221 struct vt8231_data
*data
= dev_get_drvdata(dev
);
222 unsigned long val
= simple_strtoul(buf
, NULL
, 10);
224 mutex_lock(&data
->update_lock
);
225 data
->in_min
[nr
] = SENSORS_LIMIT(((val
* 958) / 10000) + 3, 0, 255);
226 vt8231_write_value(data
, regvoltmin
[nr
], data
->in_min
[nr
]);
227 mutex_unlock(&data
->update_lock
);
231 static ssize_t
set_in_max(struct device
*dev
, struct device_attribute
*attr
,
232 const char *buf
, size_t count
)
234 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
235 int nr
= sensor_attr
->index
;
236 struct vt8231_data
*data
= dev_get_drvdata(dev
);
237 unsigned long val
= simple_strtoul(buf
, NULL
, 10);
239 mutex_lock(&data
->update_lock
);
240 data
->in_max
[nr
] = SENSORS_LIMIT(((val
* 958) / 10000) + 3, 0, 255);
241 vt8231_write_value(data
, regvoltmax
[nr
], data
->in_max
[nr
]);
242 mutex_unlock(&data
->update_lock
);
246 /* Special case for input 5 as this has 3.3V scaling built into the chip */
247 static ssize_t
show_in5(struct device
*dev
, struct device_attribute
*attr
,
250 struct vt8231_data
*data
= vt8231_update_device(dev
);
252 return sprintf(buf
, "%d\n",
253 (((data
->in
[5] - 3) * 10000 * 54) / (958 * 34)));
256 static ssize_t
show_in5_min(struct device
*dev
, struct device_attribute
*attr
,
259 struct vt8231_data
*data
= vt8231_update_device(dev
);
261 return sprintf(buf
, "%d\n",
262 (((data
->in_min
[5] - 3) * 10000 * 54) / (958 * 34)));
265 static ssize_t
show_in5_max(struct device
*dev
, struct device_attribute
*attr
,
268 struct vt8231_data
*data
= vt8231_update_device(dev
);
270 return sprintf(buf
, "%d\n",
271 (((data
->in_max
[5] - 3) * 10000 * 54) / (958 * 34)));
274 static ssize_t
set_in5_min(struct device
*dev
, struct device_attribute
*attr
,
275 const char *buf
, size_t count
)
277 struct vt8231_data
*data
= dev_get_drvdata(dev
);
278 unsigned long val
= simple_strtoul(buf
, NULL
, 10);
280 mutex_lock(&data
->update_lock
);
281 data
->in_min
[5] = SENSORS_LIMIT(((val
* 958 * 34) / (10000 * 54)) + 3,
283 vt8231_write_value(data
, regvoltmin
[5], data
->in_min
[5]);
284 mutex_unlock(&data
->update_lock
);
288 static ssize_t
set_in5_max(struct device
*dev
, struct device_attribute
*attr
,
289 const char *buf
, size_t count
)
291 struct vt8231_data
*data
= dev_get_drvdata(dev
);
292 unsigned long val
= simple_strtoul(buf
, NULL
, 10);
294 mutex_lock(&data
->update_lock
);
295 data
->in_max
[5] = SENSORS_LIMIT(((val
* 958 * 34) / (10000 * 54)) + 3,
297 vt8231_write_value(data
, regvoltmax
[5], data
->in_max
[5]);
298 mutex_unlock(&data
->update_lock
);
302 #define define_voltage_sysfs(offset) \
303 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
304 show_in, NULL, offset); \
305 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
306 show_in_min, set_in_min, offset); \
307 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
308 show_in_max, set_in_max, offset)
310 define_voltage_sysfs(0);
311 define_voltage_sysfs(1);
312 define_voltage_sysfs(2);
313 define_voltage_sysfs(3);
314 define_voltage_sysfs(4);
316 static DEVICE_ATTR(in5_input
, S_IRUGO
, show_in5
, NULL
);
317 static DEVICE_ATTR(in5_min
, S_IRUGO
| S_IWUSR
, show_in5_min
, set_in5_min
);
318 static DEVICE_ATTR(in5_max
, S_IRUGO
| S_IWUSR
, show_in5_max
, set_in5_max
);
321 static ssize_t
show_temp0(struct device
*dev
, struct device_attribute
*attr
,
324 struct vt8231_data
*data
= vt8231_update_device(dev
);
325 return sprintf(buf
, "%d\n", data
->temp
[0] * 250);
328 static ssize_t
show_temp0_max(struct device
*dev
, struct device_attribute
*attr
,
331 struct vt8231_data
*data
= vt8231_update_device(dev
);
332 return sprintf(buf
, "%d\n", data
->temp_max
[0] * 1000);
335 static ssize_t
show_temp0_min(struct device
*dev
, struct device_attribute
*attr
,
338 struct vt8231_data
*data
= vt8231_update_device(dev
);
339 return sprintf(buf
, "%d\n", data
->temp_min
[0] * 1000);
342 static ssize_t
set_temp0_max(struct device
*dev
, struct device_attribute
*attr
,
343 const char *buf
, size_t count
)
345 struct vt8231_data
*data
= dev_get_drvdata(dev
);
346 int val
= simple_strtol(buf
, NULL
, 10);
348 mutex_lock(&data
->update_lock
);
349 data
->temp_max
[0] = SENSORS_LIMIT((val
+ 500) / 1000, 0, 255);
350 vt8231_write_value(data
, regtempmax
[0], data
->temp_max
[0]);
351 mutex_unlock(&data
->update_lock
);
354 static ssize_t
set_temp0_min(struct device
*dev
, struct device_attribute
*attr
,
355 const char *buf
, size_t count
)
357 struct vt8231_data
*data
= dev_get_drvdata(dev
);
358 int val
= simple_strtol(buf
, NULL
, 10);
360 mutex_lock(&data
->update_lock
);
361 data
->temp_min
[0] = SENSORS_LIMIT((val
+ 500) / 1000, 0, 255);
362 vt8231_write_value(data
, regtempmin
[0], data
->temp_min
[0]);
363 mutex_unlock(&data
->update_lock
);
367 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
370 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
371 int nr
= sensor_attr
->index
;
372 struct vt8231_data
*data
= vt8231_update_device(dev
);
373 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp
[nr
]));
376 static ssize_t
show_temp_max(struct device
*dev
, struct device_attribute
*attr
,
379 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
380 int nr
= sensor_attr
->index
;
381 struct vt8231_data
*data
= vt8231_update_device(dev
);
382 return sprintf(buf
, "%d\n", TEMP_MAXMIN_FROM_REG(data
->temp_max
[nr
]));
385 static ssize_t
show_temp_min(struct device
*dev
, struct device_attribute
*attr
,
388 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
389 int nr
= sensor_attr
->index
;
390 struct vt8231_data
*data
= vt8231_update_device(dev
);
391 return sprintf(buf
, "%d\n", TEMP_MAXMIN_FROM_REG(data
->temp_min
[nr
]));
394 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
395 const char *buf
, size_t count
)
397 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
398 int nr
= sensor_attr
->index
;
399 struct vt8231_data
*data
= dev_get_drvdata(dev
);
400 int val
= simple_strtol(buf
, NULL
, 10);
402 mutex_lock(&data
->update_lock
);
403 data
->temp_max
[nr
] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val
), 0, 255);
404 vt8231_write_value(data
, regtempmax
[nr
], data
->temp_max
[nr
]);
405 mutex_unlock(&data
->update_lock
);
408 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
409 const char *buf
, size_t count
)
411 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
412 int nr
= sensor_attr
->index
;
413 struct vt8231_data
*data
= dev_get_drvdata(dev
);
414 int val
= simple_strtol(buf
, NULL
, 10);
416 mutex_lock(&data
->update_lock
);
417 data
->temp_min
[nr
] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val
), 0, 255);
418 vt8231_write_value(data
, regtempmin
[nr
], data
->temp_min
[nr
]);
419 mutex_unlock(&data
->update_lock
);
423 /* Note that these map the Linux temperature sensor numbering (1-6) to the VIA
424 ** temperature sensor numbering (0-5)
426 #define define_temperature_sysfs(offset) \
427 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
428 show_temp, NULL, offset - 1); \
429 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
430 show_temp_max, set_temp_max, offset - 1); \
431 static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
432 show_temp_min, set_temp_min, offset - 1)
434 static DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp0
, NULL
);
435 static DEVICE_ATTR(temp1_max
, S_IRUGO
| S_IWUSR
, show_temp0_max
, set_temp0_max
);
436 static DEVICE_ATTR(temp1_max_hyst
, S_IRUGO
| S_IWUSR
, show_temp0_min
, set_temp0_min
);
438 define_temperature_sysfs(2);
439 define_temperature_sysfs(3);
440 define_temperature_sysfs(4);
441 define_temperature_sysfs(5);
442 define_temperature_sysfs(6);
445 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*attr
,
448 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
449 int nr
= sensor_attr
->index
;
450 struct vt8231_data
*data
= vt8231_update_device(dev
);
451 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan
[nr
],
452 DIV_FROM_REG(data
->fan_div
[nr
])));
455 static ssize_t
show_fan_min(struct device
*dev
, struct device_attribute
*attr
,
458 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
459 int nr
= sensor_attr
->index
;
460 struct vt8231_data
*data
= vt8231_update_device(dev
);
461 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan_min
[nr
],
462 DIV_FROM_REG(data
->fan_div
[nr
])));
465 static ssize_t
show_fan_div(struct device
*dev
, struct device_attribute
*attr
,
468 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
469 int nr
= sensor_attr
->index
;
470 struct vt8231_data
*data
= vt8231_update_device(dev
);
471 return sprintf(buf
, "%d\n", DIV_FROM_REG(data
->fan_div
[nr
]));
474 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
475 const char *buf
, size_t count
)
477 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
478 int nr
= sensor_attr
->index
;
479 struct vt8231_data
*data
= dev_get_drvdata(dev
);
480 int val
= simple_strtoul(buf
, NULL
, 10);
482 mutex_lock(&data
->update_lock
);
483 data
->fan_min
[nr
] = FAN_TO_REG(val
, DIV_FROM_REG(data
->fan_div
[nr
]));
484 vt8231_write_value(data
, VT8231_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
485 mutex_unlock(&data
->update_lock
);
489 static ssize_t
set_fan_div(struct device
*dev
, struct device_attribute
*attr
,
490 const char *buf
, size_t count
)
492 struct vt8231_data
*data
= dev_get_drvdata(dev
);
493 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
494 unsigned long val
= simple_strtoul(buf
, NULL
, 10);
495 int nr
= sensor_attr
->index
;
496 int old
= vt8231_read_value(data
, VT8231_REG_FANDIV
);
497 long min
= FAN_FROM_REG(data
->fan_min
[nr
],
498 DIV_FROM_REG(data
->fan_div
[nr
]));
500 mutex_lock(&data
->update_lock
);
502 case 1: data
->fan_div
[nr
] = 0; break;
503 case 2: data
->fan_div
[nr
] = 1; break;
504 case 4: data
->fan_div
[nr
] = 2; break;
505 case 8: data
->fan_div
[nr
] = 3; break;
507 dev_err(dev
, "fan_div value %ld not supported."
508 "Choose one of 1, 2, 4 or 8!\n", val
);
509 mutex_unlock(&data
->update_lock
);
513 /* Correct the fan minimum speed */
514 data
->fan_min
[nr
] = FAN_TO_REG(min
, DIV_FROM_REG(data
->fan_div
[nr
]));
515 vt8231_write_value(data
, VT8231_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
517 old
= (old
& 0x0f) | (data
->fan_div
[1] << 6) | (data
->fan_div
[0] << 4);
518 vt8231_write_value(data
, VT8231_REG_FANDIV
, old
);
519 mutex_unlock(&data
->update_lock
);
524 #define define_fan_sysfs(offset) \
525 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
526 show_fan, NULL, offset - 1); \
527 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
528 show_fan_div, set_fan_div, offset - 1); \
529 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
530 show_fan_min, set_fan_min, offset - 1)
536 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*attr
,
539 struct vt8231_data
*data
= vt8231_update_device(dev
);
540 return sprintf(buf
, "%d\n", data
->alarms
);
542 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
544 static ssize_t
show_name(struct device
*dev
, struct device_attribute
547 struct vt8231_data
*data
= dev_get_drvdata(dev
);
548 return sprintf(buf
, "%s\n", data
->name
);
550 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
552 static struct attribute
*vt8231_attributes_temps
[6][4] = {
554 &dev_attr_temp1_input
.attr
,
555 &dev_attr_temp1_max_hyst
.attr
,
556 &dev_attr_temp1_max
.attr
,
559 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
560 &sensor_dev_attr_temp2_max_hyst
.dev_attr
.attr
,
561 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
564 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
565 &sensor_dev_attr_temp3_max_hyst
.dev_attr
.attr
,
566 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
569 &sensor_dev_attr_temp4_input
.dev_attr
.attr
,
570 &sensor_dev_attr_temp4_max_hyst
.dev_attr
.attr
,
571 &sensor_dev_attr_temp4_max
.dev_attr
.attr
,
574 &sensor_dev_attr_temp5_input
.dev_attr
.attr
,
575 &sensor_dev_attr_temp5_max_hyst
.dev_attr
.attr
,
576 &sensor_dev_attr_temp5_max
.dev_attr
.attr
,
579 &sensor_dev_attr_temp6_input
.dev_attr
.attr
,
580 &sensor_dev_attr_temp6_max_hyst
.dev_attr
.attr
,
581 &sensor_dev_attr_temp6_max
.dev_attr
.attr
,
586 static const struct attribute_group vt8231_group_temps
[6] = {
587 { .attrs
= vt8231_attributes_temps
[0] },
588 { .attrs
= vt8231_attributes_temps
[1] },
589 { .attrs
= vt8231_attributes_temps
[2] },
590 { .attrs
= vt8231_attributes_temps
[3] },
591 { .attrs
= vt8231_attributes_temps
[4] },
592 { .attrs
= vt8231_attributes_temps
[5] },
595 static struct attribute
*vt8231_attributes_volts
[6][4] = {
597 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
598 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
599 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
602 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
603 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
604 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
607 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
608 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
609 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
612 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
613 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
614 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
617 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
618 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
619 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
622 &dev_attr_in5_input
.attr
,
623 &dev_attr_in5_min
.attr
,
624 &dev_attr_in5_max
.attr
,
629 static const struct attribute_group vt8231_group_volts
[6] = {
630 { .attrs
= vt8231_attributes_volts
[0] },
631 { .attrs
= vt8231_attributes_volts
[1] },
632 { .attrs
= vt8231_attributes_volts
[2] },
633 { .attrs
= vt8231_attributes_volts
[3] },
634 { .attrs
= vt8231_attributes_volts
[4] },
635 { .attrs
= vt8231_attributes_volts
[5] },
638 static struct attribute
*vt8231_attributes
[] = {
639 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
640 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
641 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
642 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
643 &sensor_dev_attr_fan1_div
.dev_attr
.attr
,
644 &sensor_dev_attr_fan2_div
.dev_attr
.attr
,
645 &dev_attr_alarms
.attr
,
650 static const struct attribute_group vt8231_group
= {
651 .attrs
= vt8231_attributes
,
654 static struct platform_driver vt8231_driver
= {
656 .owner
= THIS_MODULE
,
659 .probe
= vt8231_probe
,
660 .remove
= __devexit_p(vt8231_remove
),
663 static struct pci_device_id vt8231_pci_ids
[] = {
664 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, PCI_DEVICE_ID_VIA_8231_4
) },
668 MODULE_DEVICE_TABLE(pci
, vt8231_pci_ids
);
670 static int __devinit
vt8231_pci_probe(struct pci_dev
*dev
,
671 const struct pci_device_id
*id
);
673 static struct pci_driver vt8231_pci_driver
= {
675 .id_table
= vt8231_pci_ids
,
676 .probe
= vt8231_pci_probe
,
679 static int vt8231_probe(struct platform_device
*pdev
)
681 struct resource
*res
;
682 struct vt8231_data
*data
;
685 /* Reserve the ISA region */
686 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
687 if (!request_region(res
->start
, VT8231_EXTENT
,
688 vt8231_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
);
694 if (!(data
= kzalloc(sizeof(struct vt8231_data
), GFP_KERNEL
))) {
699 platform_set_drvdata(pdev
, data
);
700 data
->addr
= res
->start
;
701 data
->name
= "vt8231";
703 mutex_init(&data
->update_lock
);
704 vt8231_init_device(data
);
706 /* Register sysfs hooks */
707 if ((err
= sysfs_create_group(&pdev
->dev
.kobj
, &vt8231_group
)))
710 /* Must update device information to find out the config field */
711 data
->uch_config
= vt8231_read_value(data
, VT8231_REG_UCH_CONFIG
);
713 for (i
= 0; i
< ARRAY_SIZE(vt8231_group_temps
); i
++) {
714 if (ISTEMP(i
, data
->uch_config
)) {
715 if ((err
= sysfs_create_group(&pdev
->dev
.kobj
,
716 &vt8231_group_temps
[i
])))
717 goto exit_remove_files
;
721 for (i
= 0; i
< ARRAY_SIZE(vt8231_group_volts
); i
++) {
722 if (ISVOLT(i
, data
->uch_config
)) {
723 if ((err
= sysfs_create_group(&pdev
->dev
.kobj
,
724 &vt8231_group_volts
[i
])))
725 goto exit_remove_files
;
729 data
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
730 if (IS_ERR(data
->hwmon_dev
)) {
731 err
= PTR_ERR(data
->hwmon_dev
);
732 goto exit_remove_files
;
737 for (i
= 0; i
< ARRAY_SIZE(vt8231_group_volts
); i
++)
738 sysfs_remove_group(&pdev
->dev
.kobj
, &vt8231_group_volts
[i
]);
740 for (i
= 0; i
< ARRAY_SIZE(vt8231_group_temps
); i
++)
741 sysfs_remove_group(&pdev
->dev
.kobj
, &vt8231_group_temps
[i
]);
743 sysfs_remove_group(&pdev
->dev
.kobj
, &vt8231_group
);
746 platform_set_drvdata(pdev
, NULL
);
750 release_region(res
->start
, VT8231_EXTENT
);
754 static int __devexit
vt8231_remove(struct platform_device
*pdev
)
756 struct vt8231_data
*data
= platform_get_drvdata(pdev
);
759 hwmon_device_unregister(data
->hwmon_dev
);
761 for (i
= 0; i
< ARRAY_SIZE(vt8231_group_volts
); i
++)
762 sysfs_remove_group(&pdev
->dev
.kobj
, &vt8231_group_volts
[i
]);
764 for (i
= 0; i
< ARRAY_SIZE(vt8231_group_temps
); i
++)
765 sysfs_remove_group(&pdev
->dev
.kobj
, &vt8231_group_temps
[i
]);
767 sysfs_remove_group(&pdev
->dev
.kobj
, &vt8231_group
);
769 release_region(data
->addr
, VT8231_EXTENT
);
770 platform_set_drvdata(pdev
, NULL
);
775 static void vt8231_init_device(struct vt8231_data
*data
)
777 vt8231_write_value(data
, VT8231_REG_TEMP1_CONFIG
, 0);
778 vt8231_write_value(data
, VT8231_REG_TEMP2_CONFIG
, 0);
781 static struct vt8231_data
*vt8231_update_device(struct device
*dev
)
783 struct vt8231_data
*data
= dev_get_drvdata(dev
);
787 mutex_lock(&data
->update_lock
);
789 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
791 for (i
= 0; i
< 6; i
++) {
792 if (ISVOLT(i
, data
->uch_config
)) {
793 data
->in
[i
] = vt8231_read_value(data
,
795 data
->in_min
[i
] = vt8231_read_value(data
,
797 data
->in_max
[i
] = vt8231_read_value(data
,
801 for (i
= 0; i
< 2; i
++) {
802 data
->fan
[i
] = vt8231_read_value(data
,
804 data
->fan_min
[i
] = vt8231_read_value(data
,
805 VT8231_REG_FAN_MIN(i
));
808 low
= vt8231_read_value(data
, VT8231_REG_TEMP_LOW01
);
809 low
= (low
>> 6) | ((low
& 0x30) >> 2)
810 | (vt8231_read_value(data
, VT8231_REG_TEMP_LOW25
) << 4);
811 for (i
= 0; i
< 6; i
++) {
812 if (ISTEMP(i
, data
->uch_config
)) {
813 data
->temp
[i
] = (vt8231_read_value(data
,
815 | ((low
>> (2 * i
)) & 0x03);
816 data
->temp_max
[i
] = vt8231_read_value(data
,
818 data
->temp_min
[i
] = vt8231_read_value(data
,
823 i
= vt8231_read_value(data
, VT8231_REG_FANDIV
);
824 data
->fan_div
[0] = (i
>> 4) & 0x03;
825 data
->fan_div
[1] = i
>> 6;
826 data
->alarms
= vt8231_read_value(data
, VT8231_REG_ALARM1
) |
827 (vt8231_read_value(data
, VT8231_REG_ALARM2
) << 8);
829 /* Set alarm flags correctly */
830 if (!data
->fan
[0] && data
->fan_min
[0]) {
831 data
->alarms
|= 0x40;
832 } else if (data
->fan
[0] && !data
->fan_min
[0]) {
833 data
->alarms
&= ~0x40;
836 if (!data
->fan
[1] && data
->fan_min
[1]) {
837 data
->alarms
|= 0x80;
838 } else if (data
->fan
[1] && !data
->fan_min
[1]) {
839 data
->alarms
&= ~0x80;
842 data
->last_updated
= jiffies
;
846 mutex_unlock(&data
->update_lock
);
851 static int __devinit
vt8231_device_add(unsigned short address
)
853 struct resource res
= {
855 .end
= address
+ VT8231_EXTENT
- 1,
857 .flags
= IORESOURCE_IO
,
861 pdev
= platform_device_alloc("vt8231", address
);
864 printk(KERN_ERR
"vt8231: Device allocation failed\n");
868 err
= platform_device_add_resources(pdev
, &res
, 1);
870 printk(KERN_ERR
"vt8231: Device resource addition failed "
872 goto exit_device_put
;
875 err
= platform_device_add(pdev
);
877 printk(KERN_ERR
"vt8231: Device addition failed (%d)\n",
879 goto exit_device_put
;
885 platform_device_put(pdev
);
890 static int __devinit
vt8231_pci_probe(struct pci_dev
*dev
,
891 const struct pci_device_id
*id
)
895 address
= force_addr
& 0xff00;
896 dev_warn(&dev
->dev
, "Forcing ISA address 0x%x\n",
899 if (PCIBIOS_SUCCESSFUL
!=
900 pci_write_config_word(dev
, VT8231_BASE_REG
, address
| 1))
904 if (PCIBIOS_SUCCESSFUL
!= pci_read_config_word(dev
, VT8231_BASE_REG
,
908 address
= val
& ~(VT8231_EXTENT
- 1);
910 dev_err(&dev
->dev
, "base address not set -\
911 upgrade BIOS or use force_addr=0xaddr\n");
915 if (PCIBIOS_SUCCESSFUL
!= pci_read_config_word(dev
, VT8231_ENABLE_REG
,
919 if (!(val
& 0x0001)) {
920 dev_warn(&dev
->dev
, "enabling sensors\n");
921 if (PCIBIOS_SUCCESSFUL
!=
922 pci_write_config_word(dev
, VT8231_ENABLE_REG
,
927 if (platform_driver_register(&vt8231_driver
))
930 /* Sets global pdev as a side effect */
931 if (vt8231_device_add(address
))
932 goto exit_unregister
;
934 /* Always return failure here. This is to allow other drivers to bind
935 * to this pci device. We don't really want to have control over the
936 * pci device, we only wanted to read as few register values from it.
939 /* We do, however, mark ourselves as using the PCI device to stop it
941 s_bridge
= pci_dev_get(dev
);
945 platform_driver_unregister(&vt8231_driver
);
950 static int __init
sm_vt8231_init(void)
952 return pci_register_driver(&vt8231_pci_driver
);
955 static void __exit
sm_vt8231_exit(void)
957 pci_unregister_driver(&vt8231_pci_driver
);
958 if (s_bridge
!= NULL
) {
959 platform_device_unregister(pdev
);
960 platform_driver_unregister(&vt8231_driver
);
961 pci_dev_put(s_bridge
);
966 MODULE_AUTHOR("Roger Lucas <roger@planbit.co.uk>");
967 MODULE_DESCRIPTION("VT8231 sensors");
968 MODULE_LICENSE("GPL");
970 module_init(sm_vt8231_init
);
971 module_exit(sm_vt8231_exit
);