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/hwmon-vid.h>
26 #include <linux/err.h>
27 #include <linux/mutex.h>
28 #include <linux/acpi.h>
31 static int force_addr;
32 module_param(force_addr, int, 0);
33 MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
35 static struct platform_device *pdev;
37 #define VT8231_EXTENT 0x80
38 #define VT8231_BASE_REG 0x70
39 #define VT8231_ENABLE_REG 0x74
41 #define DRIVER_NAME "vt8231"
44 * The VT8231 registers
46 * The reset value for the input channel configuration is used (Reg 0x4A=0x07)
47 * which sets the selected inputs marked with '*' below if multiple options are
50 * Voltage Mode Temperature Mode
51 * Sensor Linux Id Linux Id VIA Id
52 * -------- -------- -------- ------
53 * CPU Diode N/A temp1 0
61 * Note that the BIOS may set the configuration register to a different value
62 * to match the motherboard configuration.
65 /* fans numbered 0-1 */
66 #define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
67 #define VT8231_REG_FAN(nr) (0x29 + (nr))
69 /* Voltage inputs numbered 0-5 */
71 static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
72 static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
73 static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
76 * Temperatures are numbered 1-6 according to the Linux kernel specification.
78 * In the VIA datasheet, however, the temperatures are numbered from zero.
79 * Since it is important that this driver can easily be compared to the VIA
80 * datasheet, we will use the VIA numbering within this driver and map the
81 * kernel sysfs device name to the VIA number in the sysfs callback.
84 #define VT8231_REG_TEMP_LOW01 0x49
85 #define VT8231_REG_TEMP_LOW25 0x4d
87 static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
88 static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
89 static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
91 #define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
92 #define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
93 #define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
95 #define VT8231_REG_CONFIG 0x40
96 #define VT8231_REG_ALARM1 0x41
97 #define VT8231_REG_ALARM2 0x42
98 #define VT8231_REG_FANDIV 0x47
99 #define VT8231_REG_UCH_CONFIG 0x4a
100 #define VT8231_REG_TEMP1_CONFIG 0x4b
101 #define VT8231_REG_TEMP2_CONFIG 0x4c
104 * temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
107 #define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
108 ((ch_config) >> ((i)+1)) & 0x01)
110 #define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
111 !(((ch_config) >> ((i)+2)) & 0x01))
113 #define DIV_FROM_REG(val) (1 << (val))
116 * NB The values returned here are NOT temperatures. The calibration curves
117 * for the thermistor curves are board-specific and must go in the
118 * sensors.conf file. Temperature sensors are actually ten bits, but the
119 * VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
120 * register. The temperature value returned should have a magnitude of 3,
121 * so we use the VIA scaling as the "true" scaling and use the remaining 2
122 * LSBs as fractional precision.
124 * All the on-chip hardware temperature comparisons for the alarms are only
125 * 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
126 * in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
131 ****** FAN RPM CONVERSIONS ********
132 * This chip saturates back at 0, not at 255 like many the other chips.
135 static inline u8 FAN_TO_REG(long rpm, int div)
137 if (rpm <= 0 || rpm > 1310720)
139 return clamp_val(1310720 / (rpm * div), 1, 255);
142 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
148 struct mutex update_lock;
149 struct device *hwmon_dev;
150 bool valid; /* true if following fields are valid */
151 unsigned long last_updated; /* In jiffies */
153 u8 in[6]; /* Register value */
154 u8 in_max[6]; /* Register value */
155 u8 in_min[6]; /* Register value */
156 u16 temp[6]; /* Register value 10 bit, right aligned */
157 u8 temp_max[6]; /* Register value */
158 u8 temp_min[6]; /* Register value */
159 u8 fan[2]; /* Register value */
160 u8 fan_min[2]; /* Register value */
161 u8 fan_div[2]; /* Register encoding, shifted right */
162 u16 alarms; /* Register encoding */
166 static struct pci_dev *s_bridge;
168 static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
170 return inb_p(data->addr + reg);
173 static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
176 outb_p(value, data->addr + reg);
179 static struct vt8231_data *vt8231_update_device(struct device *dev)
181 struct vt8231_data *data = dev_get_drvdata(dev);
185 mutex_lock(&data->update_lock);
187 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
189 for (i = 0; i < 6; i++) {
190 if (ISVOLT(i, data->uch_config)) {
191 data->in[i] = vt8231_read_value(data,
193 data->in_min[i] = vt8231_read_value(data,
195 data->in_max[i] = vt8231_read_value(data,
199 for (i = 0; i < 2; i++) {
200 data->fan[i] = vt8231_read_value(data,
202 data->fan_min[i] = vt8231_read_value(data,
203 VT8231_REG_FAN_MIN(i));
206 low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
207 low = (low >> 6) | ((low & 0x30) >> 2)
208 | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
209 for (i = 0; i < 6; i++) {
210 if (ISTEMP(i, data->uch_config)) {
211 data->temp[i] = (vt8231_read_value(data,
213 | ((low >> (2 * i)) & 0x03);
214 data->temp_max[i] = vt8231_read_value(data,
216 data->temp_min[i] = vt8231_read_value(data,
221 i = vt8231_read_value(data, VT8231_REG_FANDIV);
222 data->fan_div[0] = (i >> 4) & 0x03;
223 data->fan_div[1] = i >> 6;
224 data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
225 (vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
227 /* Set alarm flags correctly */
228 if (!data->fan[0] && data->fan_min[0])
229 data->alarms |= 0x40;
230 else if (data->fan[0] && !data->fan_min[0])
231 data->alarms &= ~0x40;
233 if (!data->fan[1] && data->fan_min[1])
234 data->alarms |= 0x80;
235 else if (data->fan[1] && !data->fan_min[1])
236 data->alarms &= ~0x80;
238 data->last_updated = jiffies;
242 mutex_unlock(&data->update_lock);
247 /* following are the sysfs callback functions */
248 static ssize_t in_show(struct device *dev, struct device_attribute *attr,
251 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
252 int nr = sensor_attr->index;
253 struct vt8231_data *data = vt8231_update_device(dev);
255 return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
258 static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
261 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
262 int nr = sensor_attr->index;
263 struct vt8231_data *data = vt8231_update_device(dev);
265 return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
268 static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
271 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
272 int nr = sensor_attr->index;
273 struct vt8231_data *data = vt8231_update_device(dev);
275 return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
278 static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
279 const char *buf, size_t count)
281 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
282 int nr = sensor_attr->index;
283 struct vt8231_data *data = dev_get_drvdata(dev);
287 err = kstrtoul(buf, 10, &val);
291 mutex_lock(&data->update_lock);
292 data->in_min[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
293 vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
294 mutex_unlock(&data->update_lock);
298 static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
299 const char *buf, size_t count)
301 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
302 int nr = sensor_attr->index;
303 struct vt8231_data *data = dev_get_drvdata(dev);
307 err = kstrtoul(buf, 10, &val);
311 mutex_lock(&data->update_lock);
312 data->in_max[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
313 vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
314 mutex_unlock(&data->update_lock);
318 /* Special case for input 5 as this has 3.3V scaling built into the chip */
319 static ssize_t in5_input_show(struct device *dev,
320 struct device_attribute *attr, char *buf)
322 struct vt8231_data *data = vt8231_update_device(dev);
324 return sprintf(buf, "%d\n",
325 (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
328 static ssize_t in5_min_show(struct device *dev, struct device_attribute *attr,
331 struct vt8231_data *data = vt8231_update_device(dev);
333 return sprintf(buf, "%d\n",
334 (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
337 static ssize_t in5_max_show(struct device *dev, struct device_attribute *attr,
340 struct vt8231_data *data = vt8231_update_device(dev);
342 return sprintf(buf, "%d\n",
343 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
346 static ssize_t in5_min_store(struct device *dev,
347 struct device_attribute *attr, const char *buf,
350 struct vt8231_data *data = dev_get_drvdata(dev);
354 err = kstrtoul(buf, 10, &val);
358 mutex_lock(&data->update_lock);
359 data->in_min[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
361 vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
362 mutex_unlock(&data->update_lock);
366 static ssize_t in5_max_store(struct device *dev,
367 struct device_attribute *attr, const char *buf,
370 struct vt8231_data *data = dev_get_drvdata(dev);
374 err = kstrtoul(buf, 10, &val);
378 mutex_lock(&data->update_lock);
379 data->in_max[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
381 vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
382 mutex_unlock(&data->update_lock);
386 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
387 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
388 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
389 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
390 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
391 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
392 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
393 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
394 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
395 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
396 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
397 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
398 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
399 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
400 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
402 static DEVICE_ATTR_RO(in5_input);
403 static DEVICE_ATTR_RW(in5_min);
404 static DEVICE_ATTR_RW(in5_max);
407 static ssize_t temp1_input_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
410 struct vt8231_data *data = vt8231_update_device(dev);
411 return sprintf(buf, "%d\n", data->temp[0] * 250);
414 static ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
417 struct vt8231_data *data = vt8231_update_device(dev);
418 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
421 static ssize_t temp1_max_hyst_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
424 struct vt8231_data *data = vt8231_update_device(dev);
425 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
428 static ssize_t temp1_max_store(struct device *dev,
429 struct device_attribute *attr, const char *buf,
432 struct vt8231_data *data = dev_get_drvdata(dev);
436 err = kstrtol(buf, 10, &val);
440 mutex_lock(&data->update_lock);
441 data->temp_max[0] = clamp_val((val + 500) / 1000, 0, 255);
442 vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
443 mutex_unlock(&data->update_lock);
446 static ssize_t temp1_max_hyst_store(struct device *dev,
447 struct device_attribute *attr,
448 const char *buf, size_t count)
450 struct vt8231_data *data = dev_get_drvdata(dev);
454 err = kstrtol(buf, 10, &val);
458 mutex_lock(&data->update_lock);
459 data->temp_min[0] = clamp_val((val + 500) / 1000, 0, 255);
460 vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
461 mutex_unlock(&data->update_lock);
465 static ssize_t temp_show(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", TEMP_FROM_REG(data->temp[nr]));
474 static ssize_t temp_max_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
477 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
478 int nr = sensor_attr->index;
479 struct vt8231_data *data = vt8231_update_device(dev);
480 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
483 static ssize_t temp_min_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
486 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
487 int nr = sensor_attr->index;
488 struct vt8231_data *data = vt8231_update_device(dev);
489 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
492 static ssize_t temp_max_store(struct device *dev,
493 struct device_attribute *attr, const char *buf,
496 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
497 int nr = sensor_attr->index;
498 struct vt8231_data *data = dev_get_drvdata(dev);
502 err = kstrtol(buf, 10, &val);
506 mutex_lock(&data->update_lock);
507 data->temp_max[nr] = clamp_val(TEMP_MAXMIN_TO_REG(val), 0, 255);
508 vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
509 mutex_unlock(&data->update_lock);
512 static ssize_t temp_min_store(struct device *dev,
513 struct device_attribute *attr, const char *buf,
516 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
517 int nr = sensor_attr->index;
518 struct vt8231_data *data = dev_get_drvdata(dev);
522 err = kstrtol(buf, 10, &val);
526 mutex_lock(&data->update_lock);
527 data->temp_min[nr] = clamp_val(TEMP_MAXMIN_TO_REG(val), 0, 255);
528 vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
529 mutex_unlock(&data->update_lock);
534 * Note that these map the Linux temperature sensor numbering (1-6) to the VIA
535 * temperature sensor numbering (0-5)
538 static DEVICE_ATTR_RO(temp1_input);
539 static DEVICE_ATTR_RW(temp1_max);
540 static DEVICE_ATTR_RW(temp1_max_hyst);
542 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
543 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
544 static SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_min, 1);
545 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
546 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
547 static SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, temp_min, 2);
548 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
549 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
550 static SENSOR_DEVICE_ATTR_RW(temp4_max_hyst, temp_min, 3);
551 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
552 static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
553 static SENSOR_DEVICE_ATTR_RW(temp5_max_hyst, temp_min, 4);
554 static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
555 static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
556 static SENSOR_DEVICE_ATTR_RW(temp6_max_hyst, temp_min, 5);
559 static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
562 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
563 int nr = sensor_attr->index;
564 struct vt8231_data *data = vt8231_update_device(dev);
565 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
566 DIV_FROM_REG(data->fan_div[nr])));
569 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
572 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
573 int nr = sensor_attr->index;
574 struct vt8231_data *data = vt8231_update_device(dev);
575 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
576 DIV_FROM_REG(data->fan_div[nr])));
579 static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
582 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
583 int nr = sensor_attr->index;
584 struct vt8231_data *data = vt8231_update_device(dev);
585 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
588 static ssize_t fan_min_store(struct device *dev,
589 struct device_attribute *attr, const char *buf,
592 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
593 int nr = sensor_attr->index;
594 struct vt8231_data *data = dev_get_drvdata(dev);
598 err = kstrtoul(buf, 10, &val);
602 mutex_lock(&data->update_lock);
603 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
604 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
605 mutex_unlock(&data->update_lock);
609 static ssize_t fan_div_store(struct device *dev,
610 struct device_attribute *attr, const char *buf,
613 struct vt8231_data *data = dev_get_drvdata(dev);
614 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
616 int nr = sensor_attr->index;
617 int old = vt8231_read_value(data, VT8231_REG_FANDIV);
618 long min = FAN_FROM_REG(data->fan_min[nr],
619 DIV_FROM_REG(data->fan_div[nr]));
622 err = kstrtoul(buf, 10, &val);
626 mutex_lock(&data->update_lock);
629 data->fan_div[nr] = 0;
632 data->fan_div[nr] = 1;
635 data->fan_div[nr] = 2;
638 data->fan_div[nr] = 3;
642 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
644 mutex_unlock(&data->update_lock);
648 /* Correct the fan minimum speed */
649 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
650 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
652 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
653 vt8231_write_value(data, VT8231_REG_FANDIV, old);
654 mutex_unlock(&data->update_lock);
658 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
659 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
660 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
661 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
662 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
663 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
666 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
669 struct vt8231_data *data = vt8231_update_device(dev);
670 return sprintf(buf, "%d\n", data->alarms);
672 static DEVICE_ATTR_RO(alarms);
674 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
677 int bitnr = to_sensor_dev_attr(attr)->index;
678 struct vt8231_data *data = vt8231_update_device(dev);
679 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
681 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
682 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 11);
683 static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 0);
684 static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, 1);
685 static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, 3);
686 static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, 8);
687 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 11);
688 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 0);
689 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 1);
690 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
691 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
692 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 2);
693 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
694 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
696 static ssize_t name_show(struct device *dev, struct device_attribute
699 struct vt8231_data *data = dev_get_drvdata(dev);
700 return sprintf(buf, "%s\n", data->name);
702 static DEVICE_ATTR_RO(name);
704 static struct attribute *vt8231_attributes_temps[6][5] = {
706 &dev_attr_temp1_input.attr,
707 &dev_attr_temp1_max_hyst.attr,
708 &dev_attr_temp1_max.attr,
709 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
712 &sensor_dev_attr_temp2_input.dev_attr.attr,
713 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
714 &sensor_dev_attr_temp2_max.dev_attr.attr,
715 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
718 &sensor_dev_attr_temp3_input.dev_attr.attr,
719 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
720 &sensor_dev_attr_temp3_max.dev_attr.attr,
721 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
724 &sensor_dev_attr_temp4_input.dev_attr.attr,
725 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
726 &sensor_dev_attr_temp4_max.dev_attr.attr,
727 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
730 &sensor_dev_attr_temp5_input.dev_attr.attr,
731 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
732 &sensor_dev_attr_temp5_max.dev_attr.attr,
733 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
736 &sensor_dev_attr_temp6_input.dev_attr.attr,
737 &sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
738 &sensor_dev_attr_temp6_max.dev_attr.attr,
739 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
744 static const struct attribute_group vt8231_group_temps[6] = {
745 { .attrs = vt8231_attributes_temps[0] },
746 { .attrs = vt8231_attributes_temps[1] },
747 { .attrs = vt8231_attributes_temps[2] },
748 { .attrs = vt8231_attributes_temps[3] },
749 { .attrs = vt8231_attributes_temps[4] },
750 { .attrs = vt8231_attributes_temps[5] },
753 static struct attribute *vt8231_attributes_volts[6][5] = {
755 &sensor_dev_attr_in0_input.dev_attr.attr,
756 &sensor_dev_attr_in0_min.dev_attr.attr,
757 &sensor_dev_attr_in0_max.dev_attr.attr,
758 &sensor_dev_attr_in0_alarm.dev_attr.attr,
761 &sensor_dev_attr_in1_input.dev_attr.attr,
762 &sensor_dev_attr_in1_min.dev_attr.attr,
763 &sensor_dev_attr_in1_max.dev_attr.attr,
764 &sensor_dev_attr_in1_alarm.dev_attr.attr,
767 &sensor_dev_attr_in2_input.dev_attr.attr,
768 &sensor_dev_attr_in2_min.dev_attr.attr,
769 &sensor_dev_attr_in2_max.dev_attr.attr,
770 &sensor_dev_attr_in2_alarm.dev_attr.attr,
773 &sensor_dev_attr_in3_input.dev_attr.attr,
774 &sensor_dev_attr_in3_min.dev_attr.attr,
775 &sensor_dev_attr_in3_max.dev_attr.attr,
776 &sensor_dev_attr_in3_alarm.dev_attr.attr,
779 &sensor_dev_attr_in4_input.dev_attr.attr,
780 &sensor_dev_attr_in4_min.dev_attr.attr,
781 &sensor_dev_attr_in4_max.dev_attr.attr,
782 &sensor_dev_attr_in4_alarm.dev_attr.attr,
785 &dev_attr_in5_input.attr,
786 &dev_attr_in5_min.attr,
787 &dev_attr_in5_max.attr,
788 &sensor_dev_attr_in5_alarm.dev_attr.attr,
793 static const struct attribute_group vt8231_group_volts[6] = {
794 { .attrs = vt8231_attributes_volts[0] },
795 { .attrs = vt8231_attributes_volts[1] },
796 { .attrs = vt8231_attributes_volts[2] },
797 { .attrs = vt8231_attributes_volts[3] },
798 { .attrs = vt8231_attributes_volts[4] },
799 { .attrs = vt8231_attributes_volts[5] },
802 static struct attribute *vt8231_attributes[] = {
803 &sensor_dev_attr_fan1_input.dev_attr.attr,
804 &sensor_dev_attr_fan2_input.dev_attr.attr,
805 &sensor_dev_attr_fan1_min.dev_attr.attr,
806 &sensor_dev_attr_fan2_min.dev_attr.attr,
807 &sensor_dev_attr_fan1_div.dev_attr.attr,
808 &sensor_dev_attr_fan2_div.dev_attr.attr,
809 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
810 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
811 &dev_attr_alarms.attr,
816 static const struct attribute_group vt8231_group = {
817 .attrs = vt8231_attributes,
820 static void vt8231_init_device(struct vt8231_data *data)
822 vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
823 vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
826 static int vt8231_probe(struct platform_device *pdev)
828 struct resource *res;
829 struct vt8231_data *data;
832 /* Reserve the ISA region */
833 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
834 if (!devm_request_region(&pdev->dev, res->start, VT8231_EXTENT,
836 dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
837 (unsigned long)res->start, (unsigned long)res->end);
841 data = devm_kzalloc(&pdev->dev, sizeof(struct vt8231_data), GFP_KERNEL);
845 platform_set_drvdata(pdev, data);
846 data->addr = res->start;
847 data->name = DRIVER_NAME;
849 mutex_init(&data->update_lock);
850 vt8231_init_device(data);
852 /* Register sysfs hooks */
853 err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group);
857 /* Must update device information to find out the config field */
858 data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
860 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
861 if (ISTEMP(i, data->uch_config)) {
862 err = sysfs_create_group(&pdev->dev.kobj,
863 &vt8231_group_temps[i]);
865 goto exit_remove_files;
869 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
870 if (ISVOLT(i, data->uch_config)) {
871 err = sysfs_create_group(&pdev->dev.kobj,
872 &vt8231_group_volts[i]);
874 goto exit_remove_files;
878 data->hwmon_dev = hwmon_device_register(&pdev->dev);
879 if (IS_ERR(data->hwmon_dev)) {
880 err = PTR_ERR(data->hwmon_dev);
881 goto exit_remove_files;
886 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
887 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
889 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
890 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
892 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
896 static int vt8231_remove(struct platform_device *pdev)
898 struct vt8231_data *data = platform_get_drvdata(pdev);
901 hwmon_device_unregister(data->hwmon_dev);
903 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
904 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
906 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
907 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
909 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
915 static struct platform_driver vt8231_driver = {
919 .probe = vt8231_probe,
920 .remove = vt8231_remove,
923 static const struct pci_device_id vt8231_pci_ids[] = {
924 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
928 MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
930 static int vt8231_device_add(unsigned short address)
932 struct resource res = {
934 .end = address + VT8231_EXTENT - 1,
936 .flags = IORESOURCE_IO,
940 err = acpi_check_resource_conflict(&res);
944 pdev = platform_device_alloc(DRIVER_NAME, address);
947 pr_err("Device allocation failed\n");
951 err = platform_device_add_resources(pdev, &res, 1);
953 pr_err("Device resource addition failed (%d)\n", err);
954 goto exit_device_put;
957 err = platform_device_add(pdev);
959 pr_err("Device addition failed (%d)\n", err);
960 goto exit_device_put;
966 platform_device_put(pdev);
971 static int vt8231_pci_probe(struct pci_dev *dev,
972 const struct pci_device_id *id)
976 address = force_addr & 0xff00;
977 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
980 if (PCIBIOS_SUCCESSFUL !=
981 pci_write_config_word(dev, VT8231_BASE_REG, address | 1))
985 pci_read_config_word(dev, VT8231_BASE_REG, &val);
989 address = val & ~(VT8231_EXTENT - 1);
991 dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n");
995 pci_read_config_word(dev, VT8231_ENABLE_REG, &val);
999 if (!(val & 0x0001)) {
1000 dev_warn(&dev->dev, "enabling sensors\n");
1001 if (PCIBIOS_SUCCESSFUL !=
1002 pci_write_config_word(dev, VT8231_ENABLE_REG,
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);