1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * vt1211.c - driver for the VIA VT1211 Super-I/O chip integrated hardware
5 * Copyright (C) 2006 Juerg Haefliger <juergh@gmail.com>
7 * This driver is based on the driver for kernel 2.4 by Mark D. Studebaker
8 * and its port to kernel 2.6 by Lars Ekman.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/jiffies.h>
17 #include <linux/platform_device.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/hwmon-vid.h>
21 #include <linux/err.h>
22 #include <linux/mutex.h>
23 #include <linux/ioport.h>
24 #include <linux/acpi.h>
27 static int uch_config = -1;
28 module_param(uch_config, int, 0);
29 MODULE_PARM_DESC(uch_config, "Initialize the universal channel configuration");
31 static int int_mode = -1;
32 module_param(int_mode, int, 0);
33 MODULE_PARM_DESC(int_mode, "Force the temperature interrupt mode");
35 static unsigned short force_id;
36 module_param(force_id, ushort, 0);
37 MODULE_PARM_DESC(force_id, "Override the detected device ID");
39 static struct platform_device *pdev;
41 #define DRVNAME "vt1211"
43 /* ---------------------------------------------------------------------
46 * The sensors are defined as follows.
48 * Sensor Voltage Mode Temp Mode Notes (from the datasheet)
49 * -------- ------------ --------- --------------------------
50 * Reading 1 temp1 Intel thermal diode
51 * Reading 3 temp2 Internal thermal diode
52 * UCH1/Reading2 in0 temp3 NTC type thermistor
53 * UCH2 in1 temp4 +2.5V
57 * 3.3V in5 Internal VDD (+3.3V)
59 * --------------------------------------------------------------------- */
61 /* Voltages (in) numbered 0-5 (ix) */
62 #define VT1211_REG_IN(ix) (0x21 + (ix))
63 #define VT1211_REG_IN_MIN(ix) ((ix) == 0 ? 0x3e : 0x2a + 2 * (ix))
64 #define VT1211_REG_IN_MAX(ix) ((ix) == 0 ? 0x3d : 0x29 + 2 * (ix))
66 /* Temperatures (temp) numbered 0-6 (ix) */
67 static u8 regtemp[] = {0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25};
68 static u8 regtempmax[] = {0x39, 0x1d, 0x3d, 0x2b, 0x2d, 0x2f, 0x31};
69 static u8 regtemphyst[] = {0x3a, 0x1e, 0x3e, 0x2c, 0x2e, 0x30, 0x32};
71 /* Fans numbered 0-1 (ix) */
72 #define VT1211_REG_FAN(ix) (0x29 + (ix))
73 #define VT1211_REG_FAN_MIN(ix) (0x3b + (ix))
74 #define VT1211_REG_FAN_DIV 0x47
76 /* PWMs numbered 0-1 (ix) */
77 /* Auto points numbered 0-3 (ap) */
78 #define VT1211_REG_PWM(ix) (0x60 + (ix))
79 #define VT1211_REG_PWM_CLK 0x50
80 #define VT1211_REG_PWM_CTL 0x51
81 #define VT1211_REG_PWM_AUTO_TEMP(ap) (0x55 - (ap))
82 #define VT1211_REG_PWM_AUTO_PWM(ix, ap) (0x58 + 2 * (ix) - (ap))
84 /* Miscellaneous registers */
85 #define VT1211_REG_CONFIG 0x40
86 #define VT1211_REG_ALARM1 0x41
87 #define VT1211_REG_ALARM2 0x42
88 #define VT1211_REG_VID 0x45
89 #define VT1211_REG_UCH_CONFIG 0x4a
90 #define VT1211_REG_TEMP1_CONFIG 0x4b
91 #define VT1211_REG_TEMP2_CONFIG 0x4c
93 /* In, temp & fan alarm bits */
94 static const u8 bitalarmin[] = {11, 0, 1, 3, 8, 2, 9};
95 static const u8 bitalarmtemp[] = {4, 15, 11, 0, 1, 3, 8};
96 static const u8 bitalarmfan[] = {6, 7};
98 /* ---------------------------------------------------------------------
99 * Data structures and manipulation thereof
100 * --------------------------------------------------------------------- */
105 struct device *hwmon_dev;
107 struct mutex update_lock;
108 bool valid; /* true if following fields are valid */
109 unsigned long last_updated; /* In jiffies */
111 /* Register values */
126 u8 pwm_auto_pwm[2][4];
127 u8 vid; /* Read once at init time */
129 u8 uch_config; /* Read once at init time */
134 #define ISVOLT(ix, uch_config) ((ix) > 4 ? 1 : \
135 !(((uch_config) >> ((ix) + 2)) & 1))
138 #define ISTEMP(ix, uch_config) ((ix) < 2 ? 1 : \
139 ((uch_config) >> (ix)) & 1)
142 * in5 (ix = 5) is special. It's the internal 3.3V so it's scaled in the
143 * driver according to the VT1211 BIOS porting guide
145 #define IN_FROM_REG(ix, reg) ((reg) < 3 ? 0 : (ix) == 5 ? \
146 (((reg) - 3) * 15882 + 479) / 958 : \
147 (((reg) - 3) * 10000 + 479) / 958)
148 #define IN_TO_REG(ix, val) (clamp_val((ix) == 5 ? \
149 ((val) * 958 + 7941) / 15882 + 3 : \
150 ((val) * 958 + 5000) / 10000 + 3, 0, 255))
153 * temp1 (ix = 0) is an intel thermal diode which is scaled in user space.
154 * temp2 (ix = 1) is the internal temp diode so it's scaled in the driver
155 * according to some measurements that I took on an EPIA M10000.
156 * temp3-7 are thermistor based so the driver returns the voltage measured at
157 * the pin (range 0V - 2.2V).
159 #define TEMP_FROM_REG(ix, reg) ((ix) == 0 ? (reg) * 1000 : \
160 (ix) == 1 ? (reg) < 51 ? 0 : \
161 ((reg) - 51) * 1000 : \
162 ((253 - (reg)) * 2200 + 105) / 210)
163 #define TEMP_TO_REG(ix, val) clamp_val( \
164 ((ix) == 0 ? ((val) + 500) / 1000 : \
165 (ix) == 1 ? ((val) + 500) / 1000 + 51 : \
166 253 - ((val) * 210 + 1100) / 2200), 0, 255)
168 #define DIV_FROM_REG(reg) (1 << (reg))
170 #define RPM_FROM_REG(reg, div) (((reg) == 0) || ((reg) == 255) ? 0 : \
171 1310720 / (reg) / DIV_FROM_REG(div))
172 #define RPM_TO_REG(val, div) ((val) == 0 ? 255 : \
173 clamp_val((1310720 / (val) / \
174 DIV_FROM_REG(div)), 1, 254))
176 /* ---------------------------------------------------------------------
177 * Super-I/O constants and functions
178 * --------------------------------------------------------------------- */
181 * Configuration index port registers
182 * The vt1211 can live at 2 different addresses so we need to probe both
184 #define SIO_REG_CIP1 0x2e
185 #define SIO_REG_CIP2 0x4e
187 /* Configuration registers */
188 #define SIO_VT1211_LDN 0x07 /* logical device number */
189 #define SIO_VT1211_DEVID 0x20 /* device ID */
190 #define SIO_VT1211_DEVREV 0x21 /* device revision */
191 #define SIO_VT1211_ACTIVE 0x30 /* HW monitor active */
192 #define SIO_VT1211_BADDR 0x60 /* base I/O address */
193 #define SIO_VT1211_ID 0x3c /* VT1211 device ID */
195 /* VT1211 logical device numbers */
196 #define SIO_VT1211_LDN_HWMON 0x0b /* HW monitor */
198 static inline int superio_inb(int sio_cip, int reg)
201 return inb(sio_cip + 1);
204 static inline void superio_select(int sio_cip, int ldn)
206 outb(SIO_VT1211_LDN, sio_cip);
207 outb(ldn, sio_cip + 1);
210 static inline int superio_enter(int sio_cip)
212 if (!request_muxed_region(sio_cip, 2, DRVNAME))
221 static inline void superio_exit(int sio_cip)
224 release_region(sio_cip, 2);
227 /* ---------------------------------------------------------------------
229 * --------------------------------------------------------------------- */
231 static inline u8 vt1211_read8(struct vt1211_data *data, u8 reg)
233 return inb(data->addr + reg);
236 static inline void vt1211_write8(struct vt1211_data *data, u8 reg, u8 val)
238 outb(val, data->addr + reg);
241 static struct vt1211_data *vt1211_update_device(struct device *dev)
243 struct vt1211_data *data = dev_get_drvdata(dev);
246 mutex_lock(&data->update_lock);
248 /* registers cache is refreshed after 1 second */
249 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
251 data->vid = vt1211_read8(data, VT1211_REG_VID) & 0x1f;
253 /* voltage (in) registers */
254 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
255 if (ISVOLT(ix, data->uch_config)) {
256 data->in[ix] = vt1211_read8(data,
258 data->in_min[ix] = vt1211_read8(data,
259 VT1211_REG_IN_MIN(ix));
260 data->in_max[ix] = vt1211_read8(data,
261 VT1211_REG_IN_MAX(ix));
266 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
267 if (ISTEMP(ix, data->uch_config)) {
268 data->temp[ix] = vt1211_read8(data,
270 data->temp_max[ix] = vt1211_read8(data,
272 data->temp_hyst[ix] = vt1211_read8(data,
277 /* fan & pwm registers */
278 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
279 data->fan[ix] = vt1211_read8(data,
281 data->fan_min[ix] = vt1211_read8(data,
282 VT1211_REG_FAN_MIN(ix));
283 data->pwm[ix] = vt1211_read8(data,
286 val = vt1211_read8(data, VT1211_REG_FAN_DIV);
287 data->fan_div[0] = (val >> 4) & 3;
288 data->fan_div[1] = (val >> 6) & 3;
289 data->fan_ctl = val & 0xf;
291 val = vt1211_read8(data, VT1211_REG_PWM_CTL);
292 data->pwm_ctl[0] = val & 0xf;
293 data->pwm_ctl[1] = (val >> 4) & 0xf;
295 data->pwm_clk = vt1211_read8(data, VT1211_REG_PWM_CLK);
297 /* pwm & temp auto point registers */
298 data->pwm_auto_pwm[0][1] = vt1211_read8(data,
299 VT1211_REG_PWM_AUTO_PWM(0, 1));
300 data->pwm_auto_pwm[0][2] = vt1211_read8(data,
301 VT1211_REG_PWM_AUTO_PWM(0, 2));
302 data->pwm_auto_pwm[1][1] = vt1211_read8(data,
303 VT1211_REG_PWM_AUTO_PWM(1, 1));
304 data->pwm_auto_pwm[1][2] = vt1211_read8(data,
305 VT1211_REG_PWM_AUTO_PWM(1, 2));
306 for (ix = 0; ix < ARRAY_SIZE(data->pwm_auto_temp); ix++) {
307 data->pwm_auto_temp[ix] = vt1211_read8(data,
308 VT1211_REG_PWM_AUTO_TEMP(ix));
311 /* alarm registers */
312 data->alarms = (vt1211_read8(data, VT1211_REG_ALARM2) << 8) |
313 vt1211_read8(data, VT1211_REG_ALARM1);
315 data->last_updated = jiffies;
319 mutex_unlock(&data->update_lock);
324 /* ---------------------------------------------------------------------
325 * Voltage sysfs interfaces
327 * --------------------------------------------------------------------- */
329 #define SHOW_IN_INPUT 0
330 #define SHOW_SET_IN_MIN 1
331 #define SHOW_SET_IN_MAX 2
332 #define SHOW_IN_ALARM 3
334 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
337 struct vt1211_data *data = vt1211_update_device(dev);
338 struct sensor_device_attribute_2 *sensor_attr_2 =
339 to_sensor_dev_attr_2(attr);
340 int ix = sensor_attr_2->index;
341 int fn = sensor_attr_2->nr;
346 res = IN_FROM_REG(ix, data->in[ix]);
348 case SHOW_SET_IN_MIN:
349 res = IN_FROM_REG(ix, data->in_min[ix]);
351 case SHOW_SET_IN_MAX:
352 res = IN_FROM_REG(ix, data->in_max[ix]);
355 res = (data->alarms >> bitalarmin[ix]) & 1;
359 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
362 return sprintf(buf, "%d\n", res);
365 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
366 const char *buf, size_t count)
368 struct vt1211_data *data = dev_get_drvdata(dev);
369 struct sensor_device_attribute_2 *sensor_attr_2 =
370 to_sensor_dev_attr_2(attr);
371 int ix = sensor_attr_2->index;
372 int fn = sensor_attr_2->nr;
376 err = kstrtol(buf, 10, &val);
380 mutex_lock(&data->update_lock);
382 case SHOW_SET_IN_MIN:
383 data->in_min[ix] = IN_TO_REG(ix, val);
384 vt1211_write8(data, VT1211_REG_IN_MIN(ix), data->in_min[ix]);
386 case SHOW_SET_IN_MAX:
387 data->in_max[ix] = IN_TO_REG(ix, val);
388 vt1211_write8(data, VT1211_REG_IN_MAX(ix), data->in_max[ix]);
391 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
393 mutex_unlock(&data->update_lock);
398 /* ---------------------------------------------------------------------
399 * Temperature sysfs interfaces
401 * --------------------------------------------------------------------- */
403 #define SHOW_TEMP_INPUT 0
404 #define SHOW_SET_TEMP_MAX 1
405 #define SHOW_SET_TEMP_MAX_HYST 2
406 #define SHOW_TEMP_ALARM 3
408 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
411 struct vt1211_data *data = vt1211_update_device(dev);
412 struct sensor_device_attribute_2 *sensor_attr_2 =
413 to_sensor_dev_attr_2(attr);
414 int ix = sensor_attr_2->index;
415 int fn = sensor_attr_2->nr;
419 case SHOW_TEMP_INPUT:
420 res = TEMP_FROM_REG(ix, data->temp[ix]);
422 case SHOW_SET_TEMP_MAX:
423 res = TEMP_FROM_REG(ix, data->temp_max[ix]);
425 case SHOW_SET_TEMP_MAX_HYST:
426 res = TEMP_FROM_REG(ix, data->temp_hyst[ix]);
428 case SHOW_TEMP_ALARM:
429 res = (data->alarms >> bitalarmtemp[ix]) & 1;
433 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
436 return sprintf(buf, "%d\n", res);
439 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
440 const char *buf, size_t count)
442 struct vt1211_data *data = dev_get_drvdata(dev);
443 struct sensor_device_attribute_2 *sensor_attr_2 =
444 to_sensor_dev_attr_2(attr);
445 int ix = sensor_attr_2->index;
446 int fn = sensor_attr_2->nr;
450 err = kstrtol(buf, 10, &val);
454 mutex_lock(&data->update_lock);
456 case SHOW_SET_TEMP_MAX:
457 data->temp_max[ix] = TEMP_TO_REG(ix, val);
458 vt1211_write8(data, regtempmax[ix],
461 case SHOW_SET_TEMP_MAX_HYST:
462 data->temp_hyst[ix] = TEMP_TO_REG(ix, val);
463 vt1211_write8(data, regtemphyst[ix],
464 data->temp_hyst[ix]);
467 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
469 mutex_unlock(&data->update_lock);
474 /* ---------------------------------------------------------------------
475 * Fan sysfs interfaces
477 * --------------------------------------------------------------------- */
479 #define SHOW_FAN_INPUT 0
480 #define SHOW_SET_FAN_MIN 1
481 #define SHOW_SET_FAN_DIV 2
482 #define SHOW_FAN_ALARM 3
484 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
487 struct vt1211_data *data = vt1211_update_device(dev);
488 struct sensor_device_attribute_2 *sensor_attr_2 =
489 to_sensor_dev_attr_2(attr);
490 int ix = sensor_attr_2->index;
491 int fn = sensor_attr_2->nr;
496 res = RPM_FROM_REG(data->fan[ix], data->fan_div[ix]);
498 case SHOW_SET_FAN_MIN:
499 res = RPM_FROM_REG(data->fan_min[ix], data->fan_div[ix]);
501 case SHOW_SET_FAN_DIV:
502 res = DIV_FROM_REG(data->fan_div[ix]);
505 res = (data->alarms >> bitalarmfan[ix]) & 1;
509 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
512 return sprintf(buf, "%d\n", res);
515 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
516 const char *buf, size_t count)
518 struct vt1211_data *data = dev_get_drvdata(dev);
519 struct sensor_device_attribute_2 *sensor_attr_2 =
520 to_sensor_dev_attr_2(attr);
521 int ix = sensor_attr_2->index;
522 int fn = sensor_attr_2->nr;
527 err = kstrtoul(buf, 10, &val);
531 mutex_lock(&data->update_lock);
533 /* sync the data cache */
534 reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
535 data->fan_div[0] = (reg >> 4) & 3;
536 data->fan_div[1] = (reg >> 6) & 3;
537 data->fan_ctl = reg & 0xf;
540 case SHOW_SET_FAN_MIN:
541 data->fan_min[ix] = RPM_TO_REG(val, data->fan_div[ix]);
542 vt1211_write8(data, VT1211_REG_FAN_MIN(ix),
545 case SHOW_SET_FAN_DIV:
548 data->fan_div[ix] = 0;
551 data->fan_div[ix] = 1;
554 data->fan_div[ix] = 2;
557 data->fan_div[ix] = 3;
562 "fan div value %ld not supported. Choose one of 1, 2, 4, or 8.\n",
566 vt1211_write8(data, VT1211_REG_FAN_DIV,
567 ((data->fan_div[1] << 6) |
568 (data->fan_div[0] << 4) |
572 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
576 mutex_unlock(&data->update_lock);
580 /* ---------------------------------------------------------------------
581 * PWM sysfs interfaces
583 * --------------------------------------------------------------------- */
586 #define SHOW_SET_PWM_ENABLE 1
587 #define SHOW_SET_PWM_FREQ 2
588 #define SHOW_SET_PWM_AUTO_CHANNELS_TEMP 3
590 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
593 struct vt1211_data *data = vt1211_update_device(dev);
594 struct sensor_device_attribute_2 *sensor_attr_2 =
595 to_sensor_dev_attr_2(attr);
596 int ix = sensor_attr_2->index;
597 int fn = sensor_attr_2->nr;
604 case SHOW_SET_PWM_ENABLE:
605 res = ((data->pwm_ctl[ix] >> 3) & 1) ? 2 : 0;
607 case SHOW_SET_PWM_FREQ:
608 res = 90000 >> (data->pwm_clk & 7);
610 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
611 res = (data->pwm_ctl[ix] & 7) + 1;
615 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
618 return sprintf(buf, "%d\n", res);
621 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
622 const char *buf, size_t count)
624 struct vt1211_data *data = dev_get_drvdata(dev);
625 struct sensor_device_attribute_2 *sensor_attr_2 =
626 to_sensor_dev_attr_2(attr);
627 int ix = sensor_attr_2->index;
628 int fn = sensor_attr_2->nr;
633 err = kstrtoul(buf, 10, &val);
637 mutex_lock(&data->update_lock);
640 case SHOW_SET_PWM_ENABLE:
641 /* sync the data cache */
642 reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
643 data->fan_div[0] = (reg >> 4) & 3;
644 data->fan_div[1] = (reg >> 6) & 3;
645 data->fan_ctl = reg & 0xf;
646 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
647 data->pwm_ctl[0] = reg & 0xf;
648 data->pwm_ctl[1] = (reg >> 4) & 0xf;
651 data->pwm_ctl[ix] &= 7;
653 * disable SmartGuardian if both PWM outputs are
656 if ((data->pwm_ctl[ix ^ 1] & 1) == 0)
657 data->fan_ctl &= 0xe;
660 data->pwm_ctl[ix] |= 8;
666 "pwm mode %ld not supported. Choose one of 0 or 2.\n",
670 vt1211_write8(data, VT1211_REG_PWM_CTL,
671 ((data->pwm_ctl[1] << 4) |
673 vt1211_write8(data, VT1211_REG_FAN_DIV,
674 ((data->fan_div[1] << 6) |
675 (data->fan_div[0] << 4) |
678 case SHOW_SET_PWM_FREQ:
679 val = 135000 / clamp_val(val, 135000 >> 7, 135000);
680 /* calculate tmp = log2(val) */
682 for (val >>= 1; val > 0; val >>= 1)
684 /* sync the data cache */
685 reg = vt1211_read8(data, VT1211_REG_PWM_CLK);
686 data->pwm_clk = (reg & 0xf8) | tmp;
687 vt1211_write8(data, VT1211_REG_PWM_CLK, data->pwm_clk);
689 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
690 if (val < 1 || val > 7) {
693 "temp channel %ld not supported. Choose a value between 1 and 7.\n",
697 if (!ISTEMP(val - 1, data->uch_config)) {
699 dev_warn(dev, "temp channel %ld is not available.\n",
703 /* sync the data cache */
704 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
705 data->pwm_ctl[0] = reg & 0xf;
706 data->pwm_ctl[1] = (reg >> 4) & 0xf;
707 data->pwm_ctl[ix] = (data->pwm_ctl[ix] & 8) | (val - 1);
708 vt1211_write8(data, VT1211_REG_PWM_CTL,
709 ((data->pwm_ctl[1] << 4) | data->pwm_ctl[0]));
712 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
716 mutex_unlock(&data->update_lock);
720 /* ---------------------------------------------------------------------
721 * PWM auto point definitions
724 * --------------------------------------------------------------------- */
727 * pwm[ix+1]_auto_point[ap+1]_temp mapping table:
728 * Note that there is only a single set of temp auto points that controls both
729 * PWM controllers. We still create 2 sets of sysfs files to make it look
730 * more consistent even though they map to the same registers.
732 * ix ap : description
733 * -------------------
734 * 0 0 : pwm1/2 off temperature (pwm_auto_temp[0])
735 * 0 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
736 * 0 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
737 * 0 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
738 * 1 0 : pwm1/2 off temperature (pwm_auto_temp[0])
739 * 1 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
740 * 1 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
741 * 1 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
744 static ssize_t show_pwm_auto_point_temp(struct device *dev,
745 struct device_attribute *attr,
748 struct vt1211_data *data = vt1211_update_device(dev);
749 struct sensor_device_attribute_2 *sensor_attr_2 =
750 to_sensor_dev_attr_2(attr);
751 int ix = sensor_attr_2->index;
752 int ap = sensor_attr_2->nr;
754 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->pwm_ctl[ix] & 7,
755 data->pwm_auto_temp[ap]));
758 static ssize_t set_pwm_auto_point_temp(struct device *dev,
759 struct device_attribute *attr,
760 const char *buf, size_t count)
762 struct vt1211_data *data = dev_get_drvdata(dev);
763 struct sensor_device_attribute_2 *sensor_attr_2 =
764 to_sensor_dev_attr_2(attr);
765 int ix = sensor_attr_2->index;
766 int ap = sensor_attr_2->nr;
771 err = kstrtol(buf, 10, &val);
776 mutex_lock(&data->update_lock);
778 /* sync the data cache */
779 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
780 data->pwm_ctl[0] = reg & 0xf;
781 data->pwm_ctl[1] = (reg >> 4) & 0xf;
783 data->pwm_auto_temp[ap] = TEMP_TO_REG(data->pwm_ctl[ix] & 7, val);
784 vt1211_write8(data, VT1211_REG_PWM_AUTO_TEMP(ap),
785 data->pwm_auto_temp[ap]);
786 mutex_unlock(&data->update_lock);
792 * pwm[ix+1]_auto_point[ap+1]_pwm mapping table:
793 * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't
796 * ix ap : description
797 * -------------------
798 * 0 0 : pwm1 off (pwm_auto_pwm[0][0], hard-wired to 0)
799 * 0 1 : pwm1 low speed duty cycle (pwm_auto_pwm[0][1])
800 * 0 2 : pwm1 high speed duty cycle (pwm_auto_pwm[0][2])
801 * 0 3 : pwm1 full speed (pwm_auto_pwm[0][3], hard-wired to 255)
802 * 1 0 : pwm2 off (pwm_auto_pwm[1][0], hard-wired to 0)
803 * 1 1 : pwm2 low speed duty cycle (pwm_auto_pwm[1][1])
804 * 1 2 : pwm2 high speed duty cycle (pwm_auto_pwm[1][2])
805 * 1 3 : pwm2 full speed (pwm_auto_pwm[1][3], hard-wired to 255)
808 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
809 struct device_attribute *attr,
812 struct vt1211_data *data = vt1211_update_device(dev);
813 struct sensor_device_attribute_2 *sensor_attr_2 =
814 to_sensor_dev_attr_2(attr);
815 int ix = sensor_attr_2->index;
816 int ap = sensor_attr_2->nr;
818 return sprintf(buf, "%d\n", data->pwm_auto_pwm[ix][ap]);
821 static ssize_t set_pwm_auto_point_pwm(struct device *dev,
822 struct device_attribute *attr,
823 const char *buf, size_t count)
825 struct vt1211_data *data = dev_get_drvdata(dev);
826 struct sensor_device_attribute_2 *sensor_attr_2 =
827 to_sensor_dev_attr_2(attr);
828 int ix = sensor_attr_2->index;
829 int ap = sensor_attr_2->nr;
833 err = kstrtoul(buf, 10, &val);
837 mutex_lock(&data->update_lock);
838 data->pwm_auto_pwm[ix][ap] = clamp_val(val, 0, 255);
839 vt1211_write8(data, VT1211_REG_PWM_AUTO_PWM(ix, ap),
840 data->pwm_auto_pwm[ix][ap]);
841 mutex_unlock(&data->update_lock);
846 /* ---------------------------------------------------------------------
847 * Miscellaneous sysfs interfaces (VRM, VID, name, and (legacy) alarms)
848 * --------------------------------------------------------------------- */
850 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
853 struct vt1211_data *data = dev_get_drvdata(dev);
855 return sprintf(buf, "%d\n", data->vrm);
858 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
859 const char *buf, size_t count)
861 struct vt1211_data *data = dev_get_drvdata(dev);
865 err = kstrtoul(buf, 10, &val);
877 static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
880 struct vt1211_data *data = dev_get_drvdata(dev);
882 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
885 static ssize_t show_name(struct device *dev,
886 struct device_attribute *attr, char *buf)
888 struct vt1211_data *data = dev_get_drvdata(dev);
890 return sprintf(buf, "%s\n", data->name);
893 static ssize_t show_alarms(struct device *dev,
894 struct device_attribute *attr, char *buf)
896 struct vt1211_data *data = vt1211_update_device(dev);
898 return sprintf(buf, "%d\n", data->alarms);
901 /* ---------------------------------------------------------------------
902 * Device attribute structs
903 * --------------------------------------------------------------------- */
905 #define SENSOR_ATTR_IN(ix) \
906 { SENSOR_ATTR_2(in##ix##_input, S_IRUGO, \
907 show_in, NULL, SHOW_IN_INPUT, ix), \
908 SENSOR_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
909 show_in, set_in, SHOW_SET_IN_MIN, ix), \
910 SENSOR_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
911 show_in, set_in, SHOW_SET_IN_MAX, ix), \
912 SENSOR_ATTR_2(in##ix##_alarm, S_IRUGO, \
913 show_in, NULL, SHOW_IN_ALARM, ix) \
916 static struct sensor_device_attribute_2 vt1211_sysfs_in[][4] = {
925 #define IN_UNIT_ATTRS(X) \
926 { &vt1211_sysfs_in[X][0].dev_attr.attr, \
927 &vt1211_sysfs_in[X][1].dev_attr.attr, \
928 &vt1211_sysfs_in[X][2].dev_attr.attr, \
929 &vt1211_sysfs_in[X][3].dev_attr.attr, \
933 static struct attribute *vt1211_in_attr[][5] = {
942 static const struct attribute_group vt1211_in_attr_group[] = {
943 { .attrs = vt1211_in_attr[0] },
944 { .attrs = vt1211_in_attr[1] },
945 { .attrs = vt1211_in_attr[2] },
946 { .attrs = vt1211_in_attr[3] },
947 { .attrs = vt1211_in_attr[4] },
948 { .attrs = vt1211_in_attr[5] }
951 #define SENSOR_ATTR_TEMP(ix) \
952 { SENSOR_ATTR_2(temp##ix##_input, S_IRUGO, \
953 show_temp, NULL, SHOW_TEMP_INPUT, ix-1), \
954 SENSOR_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
955 show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1), \
956 SENSOR_ATTR_2(temp##ix##_max_hyst, S_IRUGO | S_IWUSR, \
957 show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1), \
958 SENSOR_ATTR_2(temp##ix##_alarm, S_IRUGO, \
959 show_temp, NULL, SHOW_TEMP_ALARM, ix-1) \
962 static struct sensor_device_attribute_2 vt1211_sysfs_temp[][4] = {
972 #define TEMP_UNIT_ATTRS(X) \
973 { &vt1211_sysfs_temp[X][0].dev_attr.attr, \
974 &vt1211_sysfs_temp[X][1].dev_attr.attr, \
975 &vt1211_sysfs_temp[X][2].dev_attr.attr, \
976 &vt1211_sysfs_temp[X][3].dev_attr.attr, \
980 static struct attribute *vt1211_temp_attr[][5] = {
990 static const struct attribute_group vt1211_temp_attr_group[] = {
991 { .attrs = vt1211_temp_attr[0] },
992 { .attrs = vt1211_temp_attr[1] },
993 { .attrs = vt1211_temp_attr[2] },
994 { .attrs = vt1211_temp_attr[3] },
995 { .attrs = vt1211_temp_attr[4] },
996 { .attrs = vt1211_temp_attr[5] },
997 { .attrs = vt1211_temp_attr[6] }
1000 #define SENSOR_ATTR_FAN(ix) \
1001 SENSOR_ATTR_2(fan##ix##_input, S_IRUGO, \
1002 show_fan, NULL, SHOW_FAN_INPUT, ix-1), \
1003 SENSOR_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1004 show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \
1005 SENSOR_ATTR_2(fan##ix##_div, S_IRUGO | S_IWUSR, \
1006 show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \
1007 SENSOR_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1008 show_fan, NULL, SHOW_FAN_ALARM, ix-1)
1010 #define SENSOR_ATTR_PWM(ix) \
1011 SENSOR_ATTR_2(pwm##ix, S_IRUGO, \
1012 show_pwm, NULL, SHOW_PWM, ix-1), \
1013 SENSOR_ATTR_2(pwm##ix##_enable, S_IRUGO | S_IWUSR, \
1014 show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \
1015 SENSOR_ATTR_2(pwm##ix##_auto_channels_temp, S_IRUGO | S_IWUSR, \
1016 show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1)
1018 #define SENSOR_ATTR_PWM_FREQ(ix) \
1019 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
1020 show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1)
1022 #define SENSOR_ATTR_PWM_FREQ_RO(ix) \
1023 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1024 show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1)
1026 #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP(ix, ap) \
1027 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO | S_IWUSR, \
1028 show_pwm_auto_point_temp, set_pwm_auto_point_temp, \
1031 #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(ix, ap) \
1032 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO, \
1033 show_pwm_auto_point_temp, NULL, \
1036 #define SENSOR_ATTR_PWM_AUTO_POINT_PWM(ix, ap) \
1037 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO | S_IWUSR, \
1038 show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, \
1041 #define SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(ix, ap) \
1042 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO, \
1043 show_pwm_auto_point_pwm, NULL, \
1046 static struct sensor_device_attribute_2 vt1211_sysfs_fan_pwm[] = {
1051 SENSOR_ATTR_PWM_FREQ(1),
1052 SENSOR_ATTR_PWM_FREQ_RO(2),
1053 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 1),
1054 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 2),
1055 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 3),
1056 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 4),
1057 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 1),
1058 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 2),
1059 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 3),
1060 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 4),
1061 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 1),
1062 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 2),
1063 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 3),
1064 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 4),
1065 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 1),
1066 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 2),
1067 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 3),
1068 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 4),
1071 static struct device_attribute vt1211_sysfs_misc[] = {
1072 __ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm),
1073 __ATTR(cpu0_vid, S_IRUGO, show_vid, NULL),
1074 __ATTR(name, S_IRUGO, show_name, NULL),
1075 __ATTR(alarms, S_IRUGO, show_alarms, NULL),
1078 /* ---------------------------------------------------------------------
1079 * Device registration and initialization
1080 * --------------------------------------------------------------------- */
1082 static void vt1211_init_device(struct vt1211_data *data)
1085 data->vrm = vid_which_vrm();
1087 /* Read (and initialize) UCH config */
1088 data->uch_config = vt1211_read8(data, VT1211_REG_UCH_CONFIG);
1089 if (uch_config > -1) {
1090 data->uch_config = (data->uch_config & 0x83) |
1092 vt1211_write8(data, VT1211_REG_UCH_CONFIG, data->uch_config);
1096 * Initialize the interrupt mode (if request at module load time).
1097 * The VT1211 implements 3 different modes for clearing interrupts:
1098 * 0: Clear INT when status register is read. Regenerate INT as long
1099 * as temp stays above hysteresis limit.
1100 * 1: Clear INT when status register is read. DON'T regenerate INT
1101 * until temp falls below hysteresis limit and exceeds hot limit
1103 * 2: Clear INT when temp falls below max limit.
1105 * The driver only allows to force mode 0 since that's the only one
1106 * that makes sense for 'sensors'
1108 if (int_mode == 0) {
1109 vt1211_write8(data, VT1211_REG_TEMP1_CONFIG, 0);
1110 vt1211_write8(data, VT1211_REG_TEMP2_CONFIG, 0);
1113 /* Fill in some hard wired values into our data struct */
1114 data->pwm_auto_pwm[0][3] = 255;
1115 data->pwm_auto_pwm[1][3] = 255;
1118 static void vt1211_remove_sysfs(struct platform_device *pdev)
1120 struct device *dev = &pdev->dev;
1123 for (i = 0; i < ARRAY_SIZE(vt1211_in_attr_group); i++)
1124 sysfs_remove_group(&dev->kobj, &vt1211_in_attr_group[i]);
1126 for (i = 0; i < ARRAY_SIZE(vt1211_temp_attr_group); i++)
1127 sysfs_remove_group(&dev->kobj, &vt1211_temp_attr_group[i]);
1129 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1130 device_remove_file(dev,
1131 &vt1211_sysfs_fan_pwm[i].dev_attr);
1133 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++)
1134 device_remove_file(dev, &vt1211_sysfs_misc[i]);
1137 static int vt1211_probe(struct platform_device *pdev)
1139 struct device *dev = &pdev->dev;
1140 struct vt1211_data *data;
1141 struct resource *res;
1144 data = devm_kzalloc(dev, sizeof(struct vt1211_data), GFP_KERNEL);
1148 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1149 if (!devm_request_region(dev, res->start, resource_size(res),
1151 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1152 (unsigned long)res->start, (unsigned long)res->end);
1155 data->addr = res->start;
1156 data->name = DRVNAME;
1157 mutex_init(&data->update_lock);
1159 platform_set_drvdata(pdev, data);
1161 /* Initialize the VT1211 chip */
1162 vt1211_init_device(data);
1164 /* Create sysfs interface files */
1165 for (i = 0; i < ARRAY_SIZE(vt1211_in_attr_group); i++) {
1166 if (ISVOLT(i, data->uch_config)) {
1167 err = sysfs_create_group(&dev->kobj,
1168 &vt1211_in_attr_group[i]);
1170 goto EXIT_DEV_REMOVE;
1173 for (i = 0; i < ARRAY_SIZE(vt1211_temp_attr_group); i++) {
1174 if (ISTEMP(i, data->uch_config)) {
1175 err = sysfs_create_group(&dev->kobj,
1176 &vt1211_temp_attr_group[i]);
1178 goto EXIT_DEV_REMOVE;
1181 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1182 err = device_create_file(dev,
1183 &vt1211_sysfs_fan_pwm[i].dev_attr);
1185 goto EXIT_DEV_REMOVE;
1187 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1188 err = device_create_file(dev,
1189 &vt1211_sysfs_misc[i]);
1191 goto EXIT_DEV_REMOVE;
1194 /* Register device */
1195 data->hwmon_dev = hwmon_device_register(dev);
1196 if (IS_ERR(data->hwmon_dev)) {
1197 err = PTR_ERR(data->hwmon_dev);
1198 dev_err(dev, "Class registration failed (%d)\n", err);
1199 goto EXIT_DEV_REMOVE_SILENT;
1205 dev_err(dev, "Sysfs interface creation failed (%d)\n", err);
1206 EXIT_DEV_REMOVE_SILENT:
1207 vt1211_remove_sysfs(pdev);
1211 static int vt1211_remove(struct platform_device *pdev)
1213 struct vt1211_data *data = platform_get_drvdata(pdev);
1215 hwmon_device_unregister(data->hwmon_dev);
1216 vt1211_remove_sysfs(pdev);
1221 static struct platform_driver vt1211_driver = {
1225 .probe = vt1211_probe,
1226 .remove = vt1211_remove,
1229 static int __init vt1211_device_add(unsigned short address)
1231 struct resource res = {
1233 .end = address + 0x7f,
1234 .flags = IORESOURCE_IO,
1238 pdev = platform_device_alloc(DRVNAME, address);
1241 pr_err("Device allocation failed (%d)\n", err);
1245 res.name = pdev->name;
1246 err = acpi_check_resource_conflict(&res);
1250 err = platform_device_add_resources(pdev, &res, 1);
1252 pr_err("Device resource addition failed (%d)\n", err);
1256 err = platform_device_add(pdev);
1258 pr_err("Device addition failed (%d)\n", err);
1265 platform_device_put(pdev);
1270 static int __init vt1211_find(int sio_cip, unsigned short *address)
1275 err = superio_enter(sio_cip);
1280 devid = force_id ? force_id : superio_inb(sio_cip, SIO_VT1211_DEVID);
1281 if (devid != SIO_VT1211_ID)
1284 superio_select(sio_cip, SIO_VT1211_LDN_HWMON);
1286 if ((superio_inb(sio_cip, SIO_VT1211_ACTIVE) & 1) == 0) {
1287 pr_warn("HW monitor is disabled, skipping\n");
1291 *address = ((superio_inb(sio_cip, SIO_VT1211_BADDR) << 8) |
1292 (superio_inb(sio_cip, SIO_VT1211_BADDR + 1))) & 0xff00;
1293 if (*address == 0) {
1294 pr_warn("Base address is not set, skipping\n");
1299 pr_info("Found VT1211 chip at 0x%04x, revision %u\n",
1300 *address, superio_inb(sio_cip, SIO_VT1211_DEVREV));
1303 superio_exit(sio_cip);
1307 static int __init vt1211_init(void)
1310 unsigned short address = 0;
1312 err = vt1211_find(SIO_REG_CIP1, &address);
1314 err = vt1211_find(SIO_REG_CIP2, &address);
1319 if ((uch_config < -1) || (uch_config > 31)) {
1321 pr_warn("Invalid UCH configuration %d. Choose a value between 0 and 31.\n",
1326 if ((int_mode < -1) || (int_mode > 0)) {
1328 pr_warn("Invalid interrupt mode %d. Only mode 0 is supported.\n",
1333 err = platform_driver_register(&vt1211_driver);
1337 /* Sets global pdev as a side effect */
1338 err = vt1211_device_add(address);
1340 goto EXIT_DRV_UNREGISTER;
1344 EXIT_DRV_UNREGISTER:
1345 platform_driver_unregister(&vt1211_driver);
1350 static void __exit vt1211_exit(void)
1352 platform_device_unregister(pdev);
1353 platform_driver_unregister(&vt1211_driver);
1356 MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
1357 MODULE_DESCRIPTION("VT1211 sensors");
1358 MODULE_LICENSE("GPL");
1360 module_init(vt1211_init);
1361 module_exit(vt1211_exit);