1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * vt8231.c - Part of lm_sensors, Linux kernel modules
4 * for hardware monitoring
6 * Copyright (c) 2005 Roger Lucas <vt8231@hiddenengine.co.uk>
7 * Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
8 * Aaron M. Marsh <amarsh@sdf.lonestar.org>
12 * Supports VIA VT8231 South Bridge embedded sensors
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/pci.h>
21 #include <linux/jiffies.h>
22 #include <linux/platform_device.h>
23 #include <linux/hwmon.h>
24 #include <linux/hwmon-sysfs.h>
25 #include <linux/err.h>
26 #include <linux/mutex.h>
27 #include <linux/acpi.h>
30 static int force_addr;
31 module_param(force_addr, int, 0);
32 MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
34 static struct platform_device *pdev;
36 #define VT8231_EXTENT 0x80
37 #define VT8231_BASE_REG 0x70
38 #define VT8231_ENABLE_REG 0x74
40 #define DRIVER_NAME "vt8231"
43 * The VT8231 registers
45 * The reset value for the input channel configuration is used (Reg 0x4A=0x07)
46 * which sets the selected inputs marked with '*' below if multiple options are
49 * Voltage Mode Temperature Mode
50 * Sensor Linux Id Linux Id VIA Id
51 * -------- -------- -------- ------
52 * CPU Diode N/A temp1 0
60 * Note that the BIOS may set the configuration register to a different value
61 * to match the motherboard configuration.
64 /* fans numbered 0-1 */
65 #define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
66 #define VT8231_REG_FAN(nr) (0x29 + (nr))
68 /* Voltage inputs numbered 0-5 */
70 static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
71 static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
72 static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
75 * Temperatures are numbered 1-6 according to the Linux kernel specification.
77 * In the VIA datasheet, however, the temperatures are numbered from zero.
78 * Since it is important that this driver can easily be compared to the VIA
79 * datasheet, we will use the VIA numbering within this driver and map the
80 * kernel sysfs device name to the VIA number in the sysfs callback.
83 #define VT8231_REG_TEMP_LOW01 0x49
84 #define VT8231_REG_TEMP_LOW25 0x4d
86 static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
87 static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
88 static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
90 #define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
91 #define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
92 #define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
94 #define VT8231_REG_CONFIG 0x40
95 #define VT8231_REG_ALARM1 0x41
96 #define VT8231_REG_ALARM2 0x42
97 #define VT8231_REG_FANDIV 0x47
98 #define VT8231_REG_UCH_CONFIG 0x4a
99 #define VT8231_REG_TEMP1_CONFIG 0x4b
100 #define VT8231_REG_TEMP2_CONFIG 0x4c
103 * temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
106 #define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
107 ((ch_config) >> ((i)+1)) & 0x01)
109 #define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
110 !(((ch_config) >> ((i)+2)) & 0x01))
112 #define DIV_FROM_REG(val) (1 << (val))
115 * NB The values returned here are NOT temperatures. The calibration curves
116 * for the thermistor curves are board-specific and must go in the
117 * sensors.conf file. Temperature sensors are actually ten bits, but the
118 * VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
119 * register. The temperature value returned should have a magnitude of 3,
120 * so we use the VIA scaling as the "true" scaling and use the remaining 2
121 * LSBs as fractional precision.
123 * All the on-chip hardware temperature comparisons for the alarms are only
124 * 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
125 * in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
130 ****** FAN RPM CONVERSIONS ********
131 * This chip saturates back at 0, not at 255 like many the other chips.
134 static inline u8 FAN_TO_REG(long rpm, int div)
136 if (rpm <= 0 || rpm > 1310720)
138 return clamp_val(1310720 / (rpm * div), 1, 255);
141 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
147 struct mutex update_lock;
148 struct device *hwmon_dev;
149 bool valid; /* true if following fields are valid */
150 unsigned long last_updated; /* In jiffies */
152 u8 in[6]; /* Register value */
153 u8 in_max[6]; /* Register value */
154 u8 in_min[6]; /* Register value */
155 u16 temp[6]; /* Register value 10 bit, right aligned */
156 u8 temp_max[6]; /* Register value */
157 u8 temp_min[6]; /* Register value */
158 u8 fan[2]; /* Register value */
159 u8 fan_min[2]; /* Register value */
160 u8 fan_div[2]; /* Register encoding, shifted right */
161 u16 alarms; /* Register encoding */
165 static struct pci_dev *s_bridge;
167 static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
169 return inb_p(data->addr + reg);
172 static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
175 outb_p(value, data->addr + reg);
178 static struct vt8231_data *vt8231_update_device(struct device *dev)
180 struct vt8231_data *data = dev_get_drvdata(dev);
184 mutex_lock(&data->update_lock);
186 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
188 for (i = 0; i < 6; i++) {
189 if (ISVOLT(i, data->uch_config)) {
190 data->in[i] = vt8231_read_value(data,
192 data->in_min[i] = vt8231_read_value(data,
194 data->in_max[i] = vt8231_read_value(data,
198 for (i = 0; i < 2; i++) {
199 data->fan[i] = vt8231_read_value(data,
201 data->fan_min[i] = vt8231_read_value(data,
202 VT8231_REG_FAN_MIN(i));
205 low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
206 low = (low >> 6) | ((low & 0x30) >> 2)
207 | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
208 for (i = 0; i < 6; i++) {
209 if (ISTEMP(i, data->uch_config)) {
210 data->temp[i] = (vt8231_read_value(data,
212 | ((low >> (2 * i)) & 0x03);
213 data->temp_max[i] = vt8231_read_value(data,
215 data->temp_min[i] = vt8231_read_value(data,
220 i = vt8231_read_value(data, VT8231_REG_FANDIV);
221 data->fan_div[0] = (i >> 4) & 0x03;
222 data->fan_div[1] = i >> 6;
223 data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
224 (vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
226 /* Set alarm flags correctly */
227 if (!data->fan[0] && data->fan_min[0])
228 data->alarms |= 0x40;
229 else if (data->fan[0] && !data->fan_min[0])
230 data->alarms &= ~0x40;
232 if (!data->fan[1] && data->fan_min[1])
233 data->alarms |= 0x80;
234 else if (data->fan[1] && !data->fan_min[1])
235 data->alarms &= ~0x80;
237 data->last_updated = jiffies;
241 mutex_unlock(&data->update_lock);
246 /* following are the sysfs callback functions */
247 static ssize_t in_show(struct device *dev, struct device_attribute *attr,
250 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
251 int nr = sensor_attr->index;
252 struct vt8231_data *data = vt8231_update_device(dev);
254 return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
257 static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
260 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
261 int nr = sensor_attr->index;
262 struct vt8231_data *data = vt8231_update_device(dev);
264 return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
267 static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
270 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
271 int nr = sensor_attr->index;
272 struct vt8231_data *data = vt8231_update_device(dev);
274 return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
277 static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
278 const char *buf, size_t count)
280 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
281 int nr = sensor_attr->index;
282 struct vt8231_data *data = dev_get_drvdata(dev);
286 err = kstrtoul(buf, 10, &val);
290 mutex_lock(&data->update_lock);
291 data->in_min[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
292 vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
293 mutex_unlock(&data->update_lock);
297 static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
298 const char *buf, size_t count)
300 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
301 int nr = sensor_attr->index;
302 struct vt8231_data *data = dev_get_drvdata(dev);
306 err = kstrtoul(buf, 10, &val);
310 mutex_lock(&data->update_lock);
311 data->in_max[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
312 vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
313 mutex_unlock(&data->update_lock);
317 /* Special case for input 5 as this has 3.3V scaling built into the chip */
318 static ssize_t in5_input_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
321 struct vt8231_data *data = vt8231_update_device(dev);
323 return sprintf(buf, "%d\n",
324 (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
327 static ssize_t in5_min_show(struct device *dev, struct device_attribute *attr,
330 struct vt8231_data *data = vt8231_update_device(dev);
332 return sprintf(buf, "%d\n",
333 (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
336 static ssize_t in5_max_show(struct device *dev, struct device_attribute *attr,
339 struct vt8231_data *data = vt8231_update_device(dev);
341 return sprintf(buf, "%d\n",
342 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
345 static ssize_t in5_min_store(struct device *dev,
346 struct device_attribute *attr, const char *buf,
349 struct vt8231_data *data = dev_get_drvdata(dev);
353 err = kstrtoul(buf, 10, &val);
357 mutex_lock(&data->update_lock);
358 data->in_min[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
360 vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
361 mutex_unlock(&data->update_lock);
365 static ssize_t in5_max_store(struct device *dev,
366 struct device_attribute *attr, const char *buf,
369 struct vt8231_data *data = dev_get_drvdata(dev);
373 err = kstrtoul(buf, 10, &val);
377 mutex_lock(&data->update_lock);
378 data->in_max[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
380 vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
381 mutex_unlock(&data->update_lock);
385 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
386 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
387 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
388 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
389 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
390 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
391 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
392 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
393 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
394 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
395 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
396 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
397 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
398 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
399 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
401 static DEVICE_ATTR_RO(in5_input);
402 static DEVICE_ATTR_RW(in5_min);
403 static DEVICE_ATTR_RW(in5_max);
406 static ssize_t temp1_input_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
409 struct vt8231_data *data = vt8231_update_device(dev);
410 return sprintf(buf, "%d\n", data->temp[0] * 250);
413 static ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
416 struct vt8231_data *data = vt8231_update_device(dev);
417 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
420 static ssize_t temp1_max_hyst_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
423 struct vt8231_data *data = vt8231_update_device(dev);
424 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
427 static ssize_t temp1_max_store(struct device *dev,
428 struct device_attribute *attr, const char *buf,
431 struct vt8231_data *data = dev_get_drvdata(dev);
435 err = kstrtol(buf, 10, &val);
439 mutex_lock(&data->update_lock);
440 data->temp_max[0] = clamp_val((val + 500) / 1000, 0, 255);
441 vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
442 mutex_unlock(&data->update_lock);
445 static ssize_t temp1_max_hyst_store(struct device *dev,
446 struct device_attribute *attr,
447 const char *buf, size_t count)
449 struct vt8231_data *data = dev_get_drvdata(dev);
453 err = kstrtol(buf, 10, &val);
457 mutex_lock(&data->update_lock);
458 data->temp_min[0] = clamp_val((val + 500) / 1000, 0, 255);
459 vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
460 mutex_unlock(&data->update_lock);
464 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
467 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
468 int nr = sensor_attr->index;
469 struct vt8231_data *data = vt8231_update_device(dev);
470 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
473 static ssize_t temp_max_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
476 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
477 int nr = sensor_attr->index;
478 struct vt8231_data *data = vt8231_update_device(dev);
479 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
482 static ssize_t temp_min_show(struct device *dev,
483 struct device_attribute *attr, char *buf)
485 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
486 int nr = sensor_attr->index;
487 struct vt8231_data *data = vt8231_update_device(dev);
488 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
491 static ssize_t temp_max_store(struct device *dev,
492 struct device_attribute *attr, const char *buf,
495 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
496 int nr = sensor_attr->index;
497 struct vt8231_data *data = dev_get_drvdata(dev);
501 err = kstrtol(buf, 10, &val);
505 mutex_lock(&data->update_lock);
506 data->temp_max[nr] = clamp_val(TEMP_MAXMIN_TO_REG(val), 0, 255);
507 vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
508 mutex_unlock(&data->update_lock);
511 static ssize_t temp_min_store(struct device *dev,
512 struct device_attribute *attr, const char *buf,
515 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
516 int nr = sensor_attr->index;
517 struct vt8231_data *data = dev_get_drvdata(dev);
521 err = kstrtol(buf, 10, &val);
525 mutex_lock(&data->update_lock);
526 data->temp_min[nr] = clamp_val(TEMP_MAXMIN_TO_REG(val), 0, 255);
527 vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
528 mutex_unlock(&data->update_lock);
533 * Note that these map the Linux temperature sensor numbering (1-6) to the VIA
534 * temperature sensor numbering (0-5)
537 static DEVICE_ATTR_RO(temp1_input);
538 static DEVICE_ATTR_RW(temp1_max);
539 static DEVICE_ATTR_RW(temp1_max_hyst);
541 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
542 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
543 static SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_min, 1);
544 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
545 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
546 static SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, temp_min, 2);
547 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
548 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
549 static SENSOR_DEVICE_ATTR_RW(temp4_max_hyst, temp_min, 3);
550 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
551 static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
552 static SENSOR_DEVICE_ATTR_RW(temp5_max_hyst, temp_min, 4);
553 static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
554 static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
555 static SENSOR_DEVICE_ATTR_RW(temp6_max_hyst, temp_min, 5);
558 static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
561 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
562 int nr = sensor_attr->index;
563 struct vt8231_data *data = vt8231_update_device(dev);
564 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
565 DIV_FROM_REG(data->fan_div[nr])));
568 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
571 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
572 int nr = sensor_attr->index;
573 struct vt8231_data *data = vt8231_update_device(dev);
574 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
575 DIV_FROM_REG(data->fan_div[nr])));
578 static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
581 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
582 int nr = sensor_attr->index;
583 struct vt8231_data *data = vt8231_update_device(dev);
584 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
587 static ssize_t fan_min_store(struct device *dev,
588 struct device_attribute *attr, const char *buf,
591 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
592 int nr = sensor_attr->index;
593 struct vt8231_data *data = dev_get_drvdata(dev);
597 err = kstrtoul(buf, 10, &val);
601 mutex_lock(&data->update_lock);
602 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
603 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
604 mutex_unlock(&data->update_lock);
608 static ssize_t fan_div_store(struct device *dev,
609 struct device_attribute *attr, const char *buf,
612 struct vt8231_data *data = dev_get_drvdata(dev);
613 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
615 int nr = sensor_attr->index;
616 int old = vt8231_read_value(data, VT8231_REG_FANDIV);
617 long min = FAN_FROM_REG(data->fan_min[nr],
618 DIV_FROM_REG(data->fan_div[nr]));
621 err = kstrtoul(buf, 10, &val);
625 mutex_lock(&data->update_lock);
628 data->fan_div[nr] = 0;
631 data->fan_div[nr] = 1;
634 data->fan_div[nr] = 2;
637 data->fan_div[nr] = 3;
641 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
643 mutex_unlock(&data->update_lock);
647 /* Correct the fan minimum speed */
648 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
649 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
651 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
652 vt8231_write_value(data, VT8231_REG_FANDIV, old);
653 mutex_unlock(&data->update_lock);
657 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
658 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
659 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
660 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
661 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
662 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
665 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
668 struct vt8231_data *data = vt8231_update_device(dev);
669 return sprintf(buf, "%d\n", data->alarms);
671 static DEVICE_ATTR_RO(alarms);
673 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
676 int bitnr = to_sensor_dev_attr(attr)->index;
677 struct vt8231_data *data = vt8231_update_device(dev);
678 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
680 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
681 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 11);
682 static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 0);
683 static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, 1);
684 static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, 3);
685 static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, 8);
686 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 11);
687 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 0);
688 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 1);
689 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
690 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
691 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 2);
692 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
693 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
695 static ssize_t name_show(struct device *dev, struct device_attribute
698 struct vt8231_data *data = dev_get_drvdata(dev);
699 return sprintf(buf, "%s\n", data->name);
701 static DEVICE_ATTR_RO(name);
703 static struct attribute *vt8231_attributes_temps[6][5] = {
705 &dev_attr_temp1_input.attr,
706 &dev_attr_temp1_max_hyst.attr,
707 &dev_attr_temp1_max.attr,
708 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
711 &sensor_dev_attr_temp2_input.dev_attr.attr,
712 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
713 &sensor_dev_attr_temp2_max.dev_attr.attr,
714 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
717 &sensor_dev_attr_temp3_input.dev_attr.attr,
718 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
719 &sensor_dev_attr_temp3_max.dev_attr.attr,
720 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
723 &sensor_dev_attr_temp4_input.dev_attr.attr,
724 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
725 &sensor_dev_attr_temp4_max.dev_attr.attr,
726 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
729 &sensor_dev_attr_temp5_input.dev_attr.attr,
730 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
731 &sensor_dev_attr_temp5_max.dev_attr.attr,
732 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
735 &sensor_dev_attr_temp6_input.dev_attr.attr,
736 &sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
737 &sensor_dev_attr_temp6_max.dev_attr.attr,
738 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
743 static const struct attribute_group vt8231_group_temps[6] = {
744 { .attrs = vt8231_attributes_temps[0] },
745 { .attrs = vt8231_attributes_temps[1] },
746 { .attrs = vt8231_attributes_temps[2] },
747 { .attrs = vt8231_attributes_temps[3] },
748 { .attrs = vt8231_attributes_temps[4] },
749 { .attrs = vt8231_attributes_temps[5] },
752 static struct attribute *vt8231_attributes_volts[6][5] = {
754 &sensor_dev_attr_in0_input.dev_attr.attr,
755 &sensor_dev_attr_in0_min.dev_attr.attr,
756 &sensor_dev_attr_in0_max.dev_attr.attr,
757 &sensor_dev_attr_in0_alarm.dev_attr.attr,
760 &sensor_dev_attr_in1_input.dev_attr.attr,
761 &sensor_dev_attr_in1_min.dev_attr.attr,
762 &sensor_dev_attr_in1_max.dev_attr.attr,
763 &sensor_dev_attr_in1_alarm.dev_attr.attr,
766 &sensor_dev_attr_in2_input.dev_attr.attr,
767 &sensor_dev_attr_in2_min.dev_attr.attr,
768 &sensor_dev_attr_in2_max.dev_attr.attr,
769 &sensor_dev_attr_in2_alarm.dev_attr.attr,
772 &sensor_dev_attr_in3_input.dev_attr.attr,
773 &sensor_dev_attr_in3_min.dev_attr.attr,
774 &sensor_dev_attr_in3_max.dev_attr.attr,
775 &sensor_dev_attr_in3_alarm.dev_attr.attr,
778 &sensor_dev_attr_in4_input.dev_attr.attr,
779 &sensor_dev_attr_in4_min.dev_attr.attr,
780 &sensor_dev_attr_in4_max.dev_attr.attr,
781 &sensor_dev_attr_in4_alarm.dev_attr.attr,
784 &dev_attr_in5_input.attr,
785 &dev_attr_in5_min.attr,
786 &dev_attr_in5_max.attr,
787 &sensor_dev_attr_in5_alarm.dev_attr.attr,
792 static const struct attribute_group vt8231_group_volts[6] = {
793 { .attrs = vt8231_attributes_volts[0] },
794 { .attrs = vt8231_attributes_volts[1] },
795 { .attrs = vt8231_attributes_volts[2] },
796 { .attrs = vt8231_attributes_volts[3] },
797 { .attrs = vt8231_attributes_volts[4] },
798 { .attrs = vt8231_attributes_volts[5] },
801 static struct attribute *vt8231_attributes[] = {
802 &sensor_dev_attr_fan1_input.dev_attr.attr,
803 &sensor_dev_attr_fan2_input.dev_attr.attr,
804 &sensor_dev_attr_fan1_min.dev_attr.attr,
805 &sensor_dev_attr_fan2_min.dev_attr.attr,
806 &sensor_dev_attr_fan1_div.dev_attr.attr,
807 &sensor_dev_attr_fan2_div.dev_attr.attr,
808 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
809 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
810 &dev_attr_alarms.attr,
815 static const struct attribute_group vt8231_group = {
816 .attrs = vt8231_attributes,
819 static void vt8231_init_device(struct vt8231_data *data)
821 vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
822 vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
825 static int vt8231_probe(struct platform_device *pdev)
827 struct resource *res;
828 struct vt8231_data *data;
831 /* Reserve the ISA region */
832 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
833 if (!devm_request_region(&pdev->dev, res->start, VT8231_EXTENT,
835 dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
836 (unsigned long)res->start, (unsigned long)res->end);
840 data = devm_kzalloc(&pdev->dev, sizeof(struct vt8231_data), GFP_KERNEL);
844 platform_set_drvdata(pdev, data);
845 data->addr = res->start;
846 data->name = DRIVER_NAME;
848 mutex_init(&data->update_lock);
849 vt8231_init_device(data);
851 /* Register sysfs hooks */
852 err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group);
856 /* Must update device information to find out the config field */
857 data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
859 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
860 if (ISTEMP(i, data->uch_config)) {
861 err = sysfs_create_group(&pdev->dev.kobj,
862 &vt8231_group_temps[i]);
864 goto exit_remove_files;
868 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
869 if (ISVOLT(i, data->uch_config)) {
870 err = sysfs_create_group(&pdev->dev.kobj,
871 &vt8231_group_volts[i]);
873 goto exit_remove_files;
877 data->hwmon_dev = hwmon_device_register(&pdev->dev);
878 if (IS_ERR(data->hwmon_dev)) {
879 err = PTR_ERR(data->hwmon_dev);
880 goto exit_remove_files;
885 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
886 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
888 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
889 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
891 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
895 static int vt8231_remove(struct platform_device *pdev)
897 struct vt8231_data *data = platform_get_drvdata(pdev);
900 hwmon_device_unregister(data->hwmon_dev);
902 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
903 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
905 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
906 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
908 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
914 static struct platform_driver vt8231_driver = {
918 .probe = vt8231_probe,
919 .remove = vt8231_remove,
922 static const struct pci_device_id vt8231_pci_ids[] = {
923 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
927 MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
929 static int vt8231_device_add(unsigned short address)
931 struct resource res = {
933 .end = address + VT8231_EXTENT - 1,
935 .flags = IORESOURCE_IO,
939 err = acpi_check_resource_conflict(&res);
943 pdev = platform_device_alloc(DRIVER_NAME, address);
946 pr_err("Device allocation failed\n");
950 err = platform_device_add_resources(pdev, &res, 1);
952 pr_err("Device resource addition failed (%d)\n", err);
953 goto exit_device_put;
956 err = platform_device_add(pdev);
958 pr_err("Device addition failed (%d)\n", err);
959 goto exit_device_put;
965 platform_device_put(pdev);
970 static int vt8231_pci_probe(struct pci_dev *dev,
971 const struct pci_device_id *id)
977 address = force_addr & 0xff00;
978 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
981 ret = pci_write_config_word(dev, VT8231_BASE_REG, address | 1);
982 if (ret != PCIBIOS_SUCCESSFUL)
986 pci_read_config_word(dev, VT8231_BASE_REG, &val);
990 address = val & ~(VT8231_EXTENT - 1);
992 dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n");
996 pci_read_config_word(dev, VT8231_ENABLE_REG, &val);
1000 if (!(val & 0x0001)) {
1001 dev_warn(&dev->dev, "enabling sensors\n");
1002 ret = pci_write_config_word(dev, VT8231_ENABLE_REG, val | 0x1);
1003 if (ret != PCIBIOS_SUCCESSFUL)
1007 if (platform_driver_register(&vt8231_driver))
1010 /* Sets global pdev as a side effect */
1011 if (vt8231_device_add(address))
1012 goto exit_unregister;
1015 * Always return failure here. This is to allow other drivers to bind
1016 * to this pci device. We don't really want to have control over the
1017 * pci device, we only wanted to read as few register values from it.
1021 * We do, however, mark ourselves as using the PCI device to stop it
1024 s_bridge = pci_dev_get(dev);
1028 platform_driver_unregister(&vt8231_driver);
1033 static struct pci_driver vt8231_pci_driver = {
1034 .name = DRIVER_NAME,
1035 .id_table = vt8231_pci_ids,
1036 .probe = vt8231_pci_probe,
1039 static int __init sm_vt8231_init(void)
1041 return pci_register_driver(&vt8231_pci_driver);
1044 static void __exit sm_vt8231_exit(void)
1046 pci_unregister_driver(&vt8231_pci_driver);
1047 if (s_bridge != NULL) {
1048 platform_device_unregister(pdev);
1049 platform_driver_unregister(&vt8231_driver);
1050 pci_dev_put(s_bridge);
1055 MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>");
1056 MODULE_DESCRIPTION("VT8231 sensors");
1057 MODULE_LICENSE("GPL");
1059 module_init(sm_vt8231_init);
1060 module_exit(sm_vt8231_exit);