2 * vt1211.c - driver for the VIA VT1211 Super-I/O chip integrated hardware
4 * Copyright (C) 2006 Juerg Haefliger <juergh@gmail.com>
6 * This driver is based on the driver for kernel 2.4 by Mark D. Studebaker
7 * and its port to kernel 2.6 by Lars Ekman.
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/jiffies.h>
30 #include <linux/platform_device.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/hwmon-vid.h>
34 #include <linux/err.h>
35 #include <linux/mutex.h>
36 #include <linux/ioport.h>
37 #include <linux/acpi.h>
40 static int uch_config = -1;
41 module_param(uch_config, int, 0);
42 MODULE_PARM_DESC(uch_config, "Initialize the universal channel configuration");
44 static int int_mode = -1;
45 module_param(int_mode, int, 0);
46 MODULE_PARM_DESC(int_mode, "Force the temperature interrupt mode");
48 static unsigned short force_id;
49 module_param(force_id, ushort, 0);
50 MODULE_PARM_DESC(force_id, "Override the detected device ID");
52 static struct platform_device *pdev;
54 #define DRVNAME "vt1211"
56 /* ---------------------------------------------------------------------
59 * The sensors are defined as follows.
61 * Sensor Voltage Mode Temp Mode Notes (from the datasheet)
62 * -------- ------------ --------- --------------------------
63 * Reading 1 temp1 Intel thermal diode
64 * Reading 3 temp2 Internal thermal diode
65 * UCH1/Reading2 in0 temp3 NTC type thermistor
66 * UCH2 in1 temp4 +2.5V
70 * 3.3V in5 Internal VDD (+3.3V)
72 * --------------------------------------------------------------------- */
74 /* Voltages (in) numbered 0-5 (ix) */
75 #define VT1211_REG_IN(ix) (0x21 + (ix))
76 #define VT1211_REG_IN_MIN(ix) ((ix) == 0 ? 0x3e : 0x2a + 2 * (ix))
77 #define VT1211_REG_IN_MAX(ix) ((ix) == 0 ? 0x3d : 0x29 + 2 * (ix))
79 /* Temperatures (temp) numbered 0-6 (ix) */
80 static u8 regtemp[] = {0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25};
81 static u8 regtempmax[] = {0x39, 0x1d, 0x3d, 0x2b, 0x2d, 0x2f, 0x31};
82 static u8 regtemphyst[] = {0x3a, 0x1e, 0x3e, 0x2c, 0x2e, 0x30, 0x32};
84 /* Fans numbered 0-1 (ix) */
85 #define VT1211_REG_FAN(ix) (0x29 + (ix))
86 #define VT1211_REG_FAN_MIN(ix) (0x3b + (ix))
87 #define VT1211_REG_FAN_DIV 0x47
89 /* PWMs numbered 0-1 (ix) */
90 /* Auto points numbered 0-3 (ap) */
91 #define VT1211_REG_PWM(ix) (0x60 + (ix))
92 #define VT1211_REG_PWM_CLK 0x50
93 #define VT1211_REG_PWM_CTL 0x51
94 #define VT1211_REG_PWM_AUTO_TEMP(ap) (0x55 - (ap))
95 #define VT1211_REG_PWM_AUTO_PWM(ix, ap) (0x58 + 2 * (ix) - (ap))
97 /* Miscellaneous registers */
98 #define VT1211_REG_CONFIG 0x40
99 #define VT1211_REG_ALARM1 0x41
100 #define VT1211_REG_ALARM2 0x42
101 #define VT1211_REG_VID 0x45
102 #define VT1211_REG_UCH_CONFIG 0x4a
103 #define VT1211_REG_TEMP1_CONFIG 0x4b
104 #define VT1211_REG_TEMP2_CONFIG 0x4c
106 /* In, temp & fan alarm bits */
107 static const u8 bitalarmin[] = {11, 0, 1, 3, 8, 2, 9};
108 static const u8 bitalarmtemp[] = {4, 15, 11, 0, 1, 3, 8};
109 static const u8 bitalarmfan[] = {6, 7};
111 /* ---------------------------------------------------------------------
112 * Data structures and manipulation thereof
113 * --------------------------------------------------------------------- */
118 struct device *hwmon_dev;
120 struct mutex update_lock;
121 char valid; /* !=0 if following fields are valid */
122 unsigned long last_updated; /* In jiffies */
124 /* Register values */
139 u8 pwm_auto_pwm[2][4];
140 u8 vid; /* Read once at init time */
142 u8 uch_config; /* Read once at init time */
147 #define ISVOLT(ix, uch_config) ((ix) > 4 ? 1 : \
148 !(((uch_config) >> ((ix) + 2)) & 1))
151 #define ISTEMP(ix, uch_config) ((ix) < 2 ? 1 : \
152 ((uch_config) >> (ix)) & 1)
155 * in5 (ix = 5) is special. It's the internal 3.3V so it's scaled in the
156 * driver according to the VT1211 BIOS porting guide
158 #define IN_FROM_REG(ix, reg) ((reg) < 3 ? 0 : (ix) == 5 ? \
159 (((reg) - 3) * 15882 + 479) / 958 : \
160 (((reg) - 3) * 10000 + 479) / 958)
161 #define IN_TO_REG(ix, val) (clamp_val((ix) == 5 ? \
162 ((val) * 958 + 7941) / 15882 + 3 : \
163 ((val) * 958 + 5000) / 10000 + 3, 0, 255))
166 * temp1 (ix = 0) is an intel thermal diode which is scaled in user space.
167 * temp2 (ix = 1) is the internal temp diode so it's scaled in the driver
168 * according to some measurements that I took on an EPIA M10000.
169 * temp3-7 are thermistor based so the driver returns the voltage measured at
170 * the pin (range 0V - 2.2V).
172 #define TEMP_FROM_REG(ix, reg) ((ix) == 0 ? (reg) * 1000 : \
173 (ix) == 1 ? (reg) < 51 ? 0 : \
174 ((reg) - 51) * 1000 : \
175 ((253 - (reg)) * 2200 + 105) / 210)
176 #define TEMP_TO_REG(ix, val) clamp_val( \
177 ((ix) == 0 ? ((val) + 500) / 1000 : \
178 (ix) == 1 ? ((val) + 500) / 1000 + 51 : \
179 253 - ((val) * 210 + 1100) / 2200), 0, 255)
181 #define DIV_FROM_REG(reg) (1 << (reg))
183 #define RPM_FROM_REG(reg, div) (((reg) == 0) || ((reg) == 255) ? 0 : \
184 1310720 / (reg) / DIV_FROM_REG(div))
185 #define RPM_TO_REG(val, div) ((val) == 0 ? 255 : \
186 clamp_val((1310720 / (val) / \
187 DIV_FROM_REG(div)), 1, 254))
189 /* ---------------------------------------------------------------------
190 * Super-I/O constants and functions
191 * --------------------------------------------------------------------- */
194 * Configuration index port registers
195 * The vt1211 can live at 2 different addresses so we need to probe both
197 #define SIO_REG_CIP1 0x2e
198 #define SIO_REG_CIP2 0x4e
200 /* Configuration registers */
201 #define SIO_VT1211_LDN 0x07 /* logical device number */
202 #define SIO_VT1211_DEVID 0x20 /* device ID */
203 #define SIO_VT1211_DEVREV 0x21 /* device revision */
204 #define SIO_VT1211_ACTIVE 0x30 /* HW monitor active */
205 #define SIO_VT1211_BADDR 0x60 /* base I/O address */
206 #define SIO_VT1211_ID 0x3c /* VT1211 device ID */
208 /* VT1211 logical device numbers */
209 #define SIO_VT1211_LDN_HWMON 0x0b /* HW monitor */
211 static inline void superio_outb(int sio_cip, int reg, int val)
214 outb(val, sio_cip + 1);
217 static inline int superio_inb(int sio_cip, int reg)
220 return inb(sio_cip + 1);
223 static inline void superio_select(int sio_cip, int ldn)
225 outb(SIO_VT1211_LDN, sio_cip);
226 outb(ldn, sio_cip + 1);
229 static inline void superio_enter(int sio_cip)
235 static inline void superio_exit(int sio_cip)
240 /* ---------------------------------------------------------------------
242 * --------------------------------------------------------------------- */
244 static inline u8 vt1211_read8(struct vt1211_data *data, u8 reg)
246 return inb(data->addr + reg);
249 static inline void vt1211_write8(struct vt1211_data *data, u8 reg, u8 val)
251 outb(val, data->addr + reg);
254 static struct vt1211_data *vt1211_update_device(struct device *dev)
256 struct vt1211_data *data = dev_get_drvdata(dev);
259 mutex_lock(&data->update_lock);
261 /* registers cache is refreshed after 1 second */
262 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
264 data->vid = vt1211_read8(data, VT1211_REG_VID) & 0x1f;
266 /* voltage (in) registers */
267 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
268 if (ISVOLT(ix, data->uch_config)) {
269 data->in[ix] = vt1211_read8(data,
271 data->in_min[ix] = vt1211_read8(data,
272 VT1211_REG_IN_MIN(ix));
273 data->in_max[ix] = vt1211_read8(data,
274 VT1211_REG_IN_MAX(ix));
279 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
280 if (ISTEMP(ix, data->uch_config)) {
281 data->temp[ix] = vt1211_read8(data,
283 data->temp_max[ix] = vt1211_read8(data,
285 data->temp_hyst[ix] = vt1211_read8(data,
290 /* fan & pwm registers */
291 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
292 data->fan[ix] = vt1211_read8(data,
294 data->fan_min[ix] = vt1211_read8(data,
295 VT1211_REG_FAN_MIN(ix));
296 data->pwm[ix] = vt1211_read8(data,
299 val = vt1211_read8(data, VT1211_REG_FAN_DIV);
300 data->fan_div[0] = (val >> 4) & 3;
301 data->fan_div[1] = (val >> 6) & 3;
302 data->fan_ctl = val & 0xf;
304 val = vt1211_read8(data, VT1211_REG_PWM_CTL);
305 data->pwm_ctl[0] = val & 0xf;
306 data->pwm_ctl[1] = (val >> 4) & 0xf;
308 data->pwm_clk = vt1211_read8(data, VT1211_REG_PWM_CLK);
310 /* pwm & temp auto point registers */
311 data->pwm_auto_pwm[0][1] = vt1211_read8(data,
312 VT1211_REG_PWM_AUTO_PWM(0, 1));
313 data->pwm_auto_pwm[0][2] = vt1211_read8(data,
314 VT1211_REG_PWM_AUTO_PWM(0, 2));
315 data->pwm_auto_pwm[1][1] = vt1211_read8(data,
316 VT1211_REG_PWM_AUTO_PWM(1, 1));
317 data->pwm_auto_pwm[1][2] = vt1211_read8(data,
318 VT1211_REG_PWM_AUTO_PWM(1, 2));
319 for (ix = 0; ix < ARRAY_SIZE(data->pwm_auto_temp); ix++) {
320 data->pwm_auto_temp[ix] = vt1211_read8(data,
321 VT1211_REG_PWM_AUTO_TEMP(ix));
324 /* alarm registers */
325 data->alarms = (vt1211_read8(data, VT1211_REG_ALARM2) << 8) |
326 vt1211_read8(data, VT1211_REG_ALARM1);
328 data->last_updated = jiffies;
332 mutex_unlock(&data->update_lock);
337 /* ---------------------------------------------------------------------
338 * Voltage sysfs interfaces
340 * --------------------------------------------------------------------- */
342 #define SHOW_IN_INPUT 0
343 #define SHOW_SET_IN_MIN 1
344 #define SHOW_SET_IN_MAX 2
345 #define SHOW_IN_ALARM 3
347 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
350 struct vt1211_data *data = vt1211_update_device(dev);
351 struct sensor_device_attribute_2 *sensor_attr_2 =
352 to_sensor_dev_attr_2(attr);
353 int ix = sensor_attr_2->index;
354 int fn = sensor_attr_2->nr;
359 res = IN_FROM_REG(ix, data->in[ix]);
361 case SHOW_SET_IN_MIN:
362 res = IN_FROM_REG(ix, data->in_min[ix]);
364 case SHOW_SET_IN_MAX:
365 res = IN_FROM_REG(ix, data->in_max[ix]);
368 res = (data->alarms >> bitalarmin[ix]) & 1;
372 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
375 return sprintf(buf, "%d\n", res);
378 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
379 const char *buf, size_t count)
381 struct vt1211_data *data = dev_get_drvdata(dev);
382 struct sensor_device_attribute_2 *sensor_attr_2 =
383 to_sensor_dev_attr_2(attr);
384 int ix = sensor_attr_2->index;
385 int fn = sensor_attr_2->nr;
389 err = kstrtol(buf, 10, &val);
393 mutex_lock(&data->update_lock);
395 case SHOW_SET_IN_MIN:
396 data->in_min[ix] = IN_TO_REG(ix, val);
397 vt1211_write8(data, VT1211_REG_IN_MIN(ix), data->in_min[ix]);
399 case SHOW_SET_IN_MAX:
400 data->in_max[ix] = IN_TO_REG(ix, val);
401 vt1211_write8(data, VT1211_REG_IN_MAX(ix), data->in_max[ix]);
404 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
406 mutex_unlock(&data->update_lock);
411 /* ---------------------------------------------------------------------
412 * Temperature sysfs interfaces
414 * --------------------------------------------------------------------- */
416 #define SHOW_TEMP_INPUT 0
417 #define SHOW_SET_TEMP_MAX 1
418 #define SHOW_SET_TEMP_MAX_HYST 2
419 #define SHOW_TEMP_ALARM 3
421 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
424 struct vt1211_data *data = vt1211_update_device(dev);
425 struct sensor_device_attribute_2 *sensor_attr_2 =
426 to_sensor_dev_attr_2(attr);
427 int ix = sensor_attr_2->index;
428 int fn = sensor_attr_2->nr;
432 case SHOW_TEMP_INPUT:
433 res = TEMP_FROM_REG(ix, data->temp[ix]);
435 case SHOW_SET_TEMP_MAX:
436 res = TEMP_FROM_REG(ix, data->temp_max[ix]);
438 case SHOW_SET_TEMP_MAX_HYST:
439 res = TEMP_FROM_REG(ix, data->temp_hyst[ix]);
441 case SHOW_TEMP_ALARM:
442 res = (data->alarms >> bitalarmtemp[ix]) & 1;
446 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
449 return sprintf(buf, "%d\n", res);
452 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
453 const char *buf, size_t count)
455 struct vt1211_data *data = dev_get_drvdata(dev);
456 struct sensor_device_attribute_2 *sensor_attr_2 =
457 to_sensor_dev_attr_2(attr);
458 int ix = sensor_attr_2->index;
459 int fn = sensor_attr_2->nr;
463 err = kstrtol(buf, 10, &val);
467 mutex_lock(&data->update_lock);
469 case SHOW_SET_TEMP_MAX:
470 data->temp_max[ix] = TEMP_TO_REG(ix, val);
471 vt1211_write8(data, regtempmax[ix],
474 case SHOW_SET_TEMP_MAX_HYST:
475 data->temp_hyst[ix] = TEMP_TO_REG(ix, val);
476 vt1211_write8(data, regtemphyst[ix],
477 data->temp_hyst[ix]);
480 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
482 mutex_unlock(&data->update_lock);
487 /* ---------------------------------------------------------------------
488 * Fan sysfs interfaces
490 * --------------------------------------------------------------------- */
492 #define SHOW_FAN_INPUT 0
493 #define SHOW_SET_FAN_MIN 1
494 #define SHOW_SET_FAN_DIV 2
495 #define SHOW_FAN_ALARM 3
497 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
500 struct vt1211_data *data = vt1211_update_device(dev);
501 struct sensor_device_attribute_2 *sensor_attr_2 =
502 to_sensor_dev_attr_2(attr);
503 int ix = sensor_attr_2->index;
504 int fn = sensor_attr_2->nr;
509 res = RPM_FROM_REG(data->fan[ix], data->fan_div[ix]);
511 case SHOW_SET_FAN_MIN:
512 res = RPM_FROM_REG(data->fan_min[ix], data->fan_div[ix]);
514 case SHOW_SET_FAN_DIV:
515 res = DIV_FROM_REG(data->fan_div[ix]);
518 res = (data->alarms >> bitalarmfan[ix]) & 1;
522 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
525 return sprintf(buf, "%d\n", res);
528 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
529 const char *buf, size_t count)
531 struct vt1211_data *data = dev_get_drvdata(dev);
532 struct sensor_device_attribute_2 *sensor_attr_2 =
533 to_sensor_dev_attr_2(attr);
534 int ix = sensor_attr_2->index;
535 int fn = sensor_attr_2->nr;
540 err = kstrtoul(buf, 10, &val);
544 mutex_lock(&data->update_lock);
546 /* sync the data cache */
547 reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
548 data->fan_div[0] = (reg >> 4) & 3;
549 data->fan_div[1] = (reg >> 6) & 3;
550 data->fan_ctl = reg & 0xf;
553 case SHOW_SET_FAN_MIN:
554 data->fan_min[ix] = RPM_TO_REG(val, data->fan_div[ix]);
555 vt1211_write8(data, VT1211_REG_FAN_MIN(ix),
558 case SHOW_SET_FAN_DIV:
561 data->fan_div[ix] = 0;
564 data->fan_div[ix] = 1;
567 data->fan_div[ix] = 2;
570 data->fan_div[ix] = 3;
574 dev_warn(dev, "fan div value %ld not supported. "
575 "Choose one of 1, 2, 4, or 8.\n", val);
578 vt1211_write8(data, VT1211_REG_FAN_DIV,
579 ((data->fan_div[1] << 6) |
580 (data->fan_div[0] << 4) |
584 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
588 mutex_unlock(&data->update_lock);
592 /* ---------------------------------------------------------------------
593 * PWM sysfs interfaces
595 * --------------------------------------------------------------------- */
598 #define SHOW_SET_PWM_ENABLE 1
599 #define SHOW_SET_PWM_FREQ 2
600 #define SHOW_SET_PWM_AUTO_CHANNELS_TEMP 3
602 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
605 struct vt1211_data *data = vt1211_update_device(dev);
606 struct sensor_device_attribute_2 *sensor_attr_2 =
607 to_sensor_dev_attr_2(attr);
608 int ix = sensor_attr_2->index;
609 int fn = sensor_attr_2->nr;
616 case SHOW_SET_PWM_ENABLE:
617 res = ((data->pwm_ctl[ix] >> 3) & 1) ? 2 : 0;
619 case SHOW_SET_PWM_FREQ:
620 res = 90000 >> (data->pwm_clk & 7);
622 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
623 res = (data->pwm_ctl[ix] & 7) + 1;
627 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
630 return sprintf(buf, "%d\n", res);
633 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
634 const char *buf, size_t count)
636 struct vt1211_data *data = dev_get_drvdata(dev);
637 struct sensor_device_attribute_2 *sensor_attr_2 =
638 to_sensor_dev_attr_2(attr);
639 int ix = sensor_attr_2->index;
640 int fn = sensor_attr_2->nr;
645 err = kstrtoul(buf, 10, &val);
649 mutex_lock(&data->update_lock);
652 case SHOW_SET_PWM_ENABLE:
653 /* sync the data cache */
654 reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
655 data->fan_div[0] = (reg >> 4) & 3;
656 data->fan_div[1] = (reg >> 6) & 3;
657 data->fan_ctl = reg & 0xf;
658 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
659 data->pwm_ctl[0] = reg & 0xf;
660 data->pwm_ctl[1] = (reg >> 4) & 0xf;
663 data->pwm_ctl[ix] &= 7;
665 * disable SmartGuardian if both PWM outputs are
668 if ((data->pwm_ctl[ix ^ 1] & 1) == 0)
669 data->fan_ctl &= 0xe;
672 data->pwm_ctl[ix] |= 8;
677 dev_warn(dev, "pwm mode %ld not supported. "
678 "Choose one of 0 or 2.\n", val);
681 vt1211_write8(data, VT1211_REG_PWM_CTL,
682 ((data->pwm_ctl[1] << 4) |
684 vt1211_write8(data, VT1211_REG_FAN_DIV,
685 ((data->fan_div[1] << 6) |
686 (data->fan_div[0] << 4) |
689 case SHOW_SET_PWM_FREQ:
690 val = 135000 / clamp_val(val, 135000 >> 7, 135000);
691 /* calculate tmp = log2(val) */
693 for (val >>= 1; val > 0; val >>= 1)
695 /* sync the data cache */
696 reg = vt1211_read8(data, VT1211_REG_PWM_CLK);
697 data->pwm_clk = (reg & 0xf8) | tmp;
698 vt1211_write8(data, VT1211_REG_PWM_CLK, data->pwm_clk);
700 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
701 if (val < 1 || val > 7) {
703 dev_warn(dev, "temp channel %ld not supported. "
704 "Choose a value between 1 and 7.\n", val);
707 if (!ISTEMP(val - 1, data->uch_config)) {
709 dev_warn(dev, "temp channel %ld is not available.\n",
713 /* sync the data cache */
714 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
715 data->pwm_ctl[0] = reg & 0xf;
716 data->pwm_ctl[1] = (reg >> 4) & 0xf;
717 data->pwm_ctl[ix] = (data->pwm_ctl[ix] & 8) | (val - 1);
718 vt1211_write8(data, VT1211_REG_PWM_CTL,
719 ((data->pwm_ctl[1] << 4) | data->pwm_ctl[0]));
722 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
726 mutex_unlock(&data->update_lock);
730 /* ---------------------------------------------------------------------
731 * PWM auto point definitions
734 * --------------------------------------------------------------------- */
737 * pwm[ix+1]_auto_point[ap+1]_temp mapping table:
738 * Note that there is only a single set of temp auto points that controls both
739 * PWM controllers. We still create 2 sets of sysfs files to make it look
740 * more consistent even though they map to the same registers.
742 * ix ap : description
743 * -------------------
744 * 0 0 : pwm1/2 off temperature (pwm_auto_temp[0])
745 * 0 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
746 * 0 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
747 * 0 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
748 * 1 0 : pwm1/2 off temperature (pwm_auto_temp[0])
749 * 1 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
750 * 1 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
751 * 1 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
754 static ssize_t show_pwm_auto_point_temp(struct device *dev,
755 struct device_attribute *attr,
758 struct vt1211_data *data = vt1211_update_device(dev);
759 struct sensor_device_attribute_2 *sensor_attr_2 =
760 to_sensor_dev_attr_2(attr);
761 int ix = sensor_attr_2->index;
762 int ap = sensor_attr_2->nr;
764 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->pwm_ctl[ix] & 7,
765 data->pwm_auto_temp[ap]));
768 static ssize_t set_pwm_auto_point_temp(struct device *dev,
769 struct device_attribute *attr,
770 const char *buf, size_t count)
772 struct vt1211_data *data = dev_get_drvdata(dev);
773 struct sensor_device_attribute_2 *sensor_attr_2 =
774 to_sensor_dev_attr_2(attr);
775 int ix = sensor_attr_2->index;
776 int ap = sensor_attr_2->nr;
781 err = kstrtol(buf, 10, &val);
786 mutex_lock(&data->update_lock);
788 /* sync the data cache */
789 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
790 data->pwm_ctl[0] = reg & 0xf;
791 data->pwm_ctl[1] = (reg >> 4) & 0xf;
793 data->pwm_auto_temp[ap] = TEMP_TO_REG(data->pwm_ctl[ix] & 7, val);
794 vt1211_write8(data, VT1211_REG_PWM_AUTO_TEMP(ap),
795 data->pwm_auto_temp[ap]);
796 mutex_unlock(&data->update_lock);
802 * pwm[ix+1]_auto_point[ap+1]_pwm mapping table:
803 * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't
806 * ix ap : description
807 * -------------------
808 * 0 0 : pwm1 off (pwm_auto_pwm[0][0], hard-wired to 0)
809 * 0 1 : pwm1 low speed duty cycle (pwm_auto_pwm[0][1])
810 * 0 2 : pwm1 high speed duty cycle (pwm_auto_pwm[0][2])
811 * 0 3 : pwm1 full speed (pwm_auto_pwm[0][3], hard-wired to 255)
812 * 1 0 : pwm2 off (pwm_auto_pwm[1][0], hard-wired to 0)
813 * 1 1 : pwm2 low speed duty cycle (pwm_auto_pwm[1][1])
814 * 1 2 : pwm2 high speed duty cycle (pwm_auto_pwm[1][2])
815 * 1 3 : pwm2 full speed (pwm_auto_pwm[1][3], hard-wired to 255)
818 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
819 struct device_attribute *attr,
822 struct vt1211_data *data = vt1211_update_device(dev);
823 struct sensor_device_attribute_2 *sensor_attr_2 =
824 to_sensor_dev_attr_2(attr);
825 int ix = sensor_attr_2->index;
826 int ap = sensor_attr_2->nr;
828 return sprintf(buf, "%d\n", data->pwm_auto_pwm[ix][ap]);
831 static ssize_t set_pwm_auto_point_pwm(struct device *dev,
832 struct device_attribute *attr,
833 const char *buf, size_t count)
835 struct vt1211_data *data = dev_get_drvdata(dev);
836 struct sensor_device_attribute_2 *sensor_attr_2 =
837 to_sensor_dev_attr_2(attr);
838 int ix = sensor_attr_2->index;
839 int ap = sensor_attr_2->nr;
843 err = kstrtoul(buf, 10, &val);
847 mutex_lock(&data->update_lock);
848 data->pwm_auto_pwm[ix][ap] = clamp_val(val, 0, 255);
849 vt1211_write8(data, VT1211_REG_PWM_AUTO_PWM(ix, ap),
850 data->pwm_auto_pwm[ix][ap]);
851 mutex_unlock(&data->update_lock);
856 /* ---------------------------------------------------------------------
857 * Miscellaneous sysfs interfaces (VRM, VID, name, and (legacy) alarms)
858 * --------------------------------------------------------------------- */
860 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
863 struct vt1211_data *data = dev_get_drvdata(dev);
865 return sprintf(buf, "%d\n", data->vrm);
868 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
869 const char *buf, size_t count)
871 struct vt1211_data *data = dev_get_drvdata(dev);
875 err = kstrtoul(buf, 10, &val);
884 static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
887 struct vt1211_data *data = dev_get_drvdata(dev);
889 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
892 static ssize_t show_name(struct device *dev,
893 struct device_attribute *attr, char *buf)
895 struct vt1211_data *data = dev_get_drvdata(dev);
897 return sprintf(buf, "%s\n", data->name);
900 static ssize_t show_alarms(struct device *dev,
901 struct device_attribute *attr, char *buf)
903 struct vt1211_data *data = vt1211_update_device(dev);
905 return sprintf(buf, "%d\n", data->alarms);
908 /* ---------------------------------------------------------------------
909 * Device attribute structs
910 * --------------------------------------------------------------------- */
912 #define SENSOR_ATTR_IN(ix) \
913 { SENSOR_ATTR_2(in##ix##_input, S_IRUGO, \
914 show_in, NULL, SHOW_IN_INPUT, ix), \
915 SENSOR_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
916 show_in, set_in, SHOW_SET_IN_MIN, ix), \
917 SENSOR_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
918 show_in, set_in, SHOW_SET_IN_MAX, ix), \
919 SENSOR_ATTR_2(in##ix##_alarm, S_IRUGO, \
920 show_in, NULL, SHOW_IN_ALARM, ix) \
923 static struct sensor_device_attribute_2 vt1211_sysfs_in[][4] = {
932 #define IN_UNIT_ATTRS(X) \
933 { &vt1211_sysfs_in[X][0].dev_attr.attr, \
934 &vt1211_sysfs_in[X][1].dev_attr.attr, \
935 &vt1211_sysfs_in[X][2].dev_attr.attr, \
936 &vt1211_sysfs_in[X][3].dev_attr.attr, \
940 static struct attribute *vt1211_in_attr[][5] = {
949 static const struct attribute_group vt1211_in_attr_group[] = {
950 { .attrs = vt1211_in_attr[0] },
951 { .attrs = vt1211_in_attr[1] },
952 { .attrs = vt1211_in_attr[2] },
953 { .attrs = vt1211_in_attr[3] },
954 { .attrs = vt1211_in_attr[4] },
955 { .attrs = vt1211_in_attr[5] }
958 #define SENSOR_ATTR_TEMP(ix) \
959 { SENSOR_ATTR_2(temp##ix##_input, S_IRUGO, \
960 show_temp, NULL, SHOW_TEMP_INPUT, ix-1), \
961 SENSOR_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
962 show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1), \
963 SENSOR_ATTR_2(temp##ix##_max_hyst, S_IRUGO | S_IWUSR, \
964 show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1), \
965 SENSOR_ATTR_2(temp##ix##_alarm, S_IRUGO, \
966 show_temp, NULL, SHOW_TEMP_ALARM, ix-1) \
969 static struct sensor_device_attribute_2 vt1211_sysfs_temp[][4] = {
979 #define TEMP_UNIT_ATTRS(X) \
980 { &vt1211_sysfs_temp[X][0].dev_attr.attr, \
981 &vt1211_sysfs_temp[X][1].dev_attr.attr, \
982 &vt1211_sysfs_temp[X][2].dev_attr.attr, \
983 &vt1211_sysfs_temp[X][3].dev_attr.attr, \
987 static struct attribute *vt1211_temp_attr[][5] = {
997 static const struct attribute_group vt1211_temp_attr_group[] = {
998 { .attrs = vt1211_temp_attr[0] },
999 { .attrs = vt1211_temp_attr[1] },
1000 { .attrs = vt1211_temp_attr[2] },
1001 { .attrs = vt1211_temp_attr[3] },
1002 { .attrs = vt1211_temp_attr[4] },
1003 { .attrs = vt1211_temp_attr[5] },
1004 { .attrs = vt1211_temp_attr[6] }
1007 #define SENSOR_ATTR_FAN(ix) \
1008 SENSOR_ATTR_2(fan##ix##_input, S_IRUGO, \
1009 show_fan, NULL, SHOW_FAN_INPUT, ix-1), \
1010 SENSOR_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1011 show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \
1012 SENSOR_ATTR_2(fan##ix##_div, S_IRUGO | S_IWUSR, \
1013 show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \
1014 SENSOR_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1015 show_fan, NULL, SHOW_FAN_ALARM, ix-1)
1017 #define SENSOR_ATTR_PWM(ix) \
1018 SENSOR_ATTR_2(pwm##ix, S_IRUGO, \
1019 show_pwm, NULL, SHOW_PWM, ix-1), \
1020 SENSOR_ATTR_2(pwm##ix##_enable, S_IRUGO | S_IWUSR, \
1021 show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \
1022 SENSOR_ATTR_2(pwm##ix##_auto_channels_temp, S_IRUGO | S_IWUSR, \
1023 show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1)
1025 #define SENSOR_ATTR_PWM_FREQ(ix) \
1026 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
1027 show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1)
1029 #define SENSOR_ATTR_PWM_FREQ_RO(ix) \
1030 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1031 show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1)
1033 #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP(ix, ap) \
1034 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO | S_IWUSR, \
1035 show_pwm_auto_point_temp, set_pwm_auto_point_temp, \
1038 #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(ix, ap) \
1039 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO, \
1040 show_pwm_auto_point_temp, NULL, \
1043 #define SENSOR_ATTR_PWM_AUTO_POINT_PWM(ix, ap) \
1044 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO | S_IWUSR, \
1045 show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, \
1048 #define SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(ix, ap) \
1049 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO, \
1050 show_pwm_auto_point_pwm, NULL, \
1053 static struct sensor_device_attribute_2 vt1211_sysfs_fan_pwm[] = {
1058 SENSOR_ATTR_PWM_FREQ(1),
1059 SENSOR_ATTR_PWM_FREQ_RO(2),
1060 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 1),
1061 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 2),
1062 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 3),
1063 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 4),
1064 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 1),
1065 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 2),
1066 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 3),
1067 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 4),
1068 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 1),
1069 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 2),
1070 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 3),
1071 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 4),
1072 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 1),
1073 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 2),
1074 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 3),
1075 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 4),
1078 static struct device_attribute vt1211_sysfs_misc[] = {
1079 __ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm),
1080 __ATTR(cpu0_vid, S_IRUGO, show_vid, NULL),
1081 __ATTR(name, S_IRUGO, show_name, NULL),
1082 __ATTR(alarms, S_IRUGO, show_alarms, NULL),
1085 /* ---------------------------------------------------------------------
1086 * Device registration and initialization
1087 * --------------------------------------------------------------------- */
1089 static void vt1211_init_device(struct vt1211_data *data)
1092 data->vrm = vid_which_vrm();
1094 /* Read (and initialize) UCH config */
1095 data->uch_config = vt1211_read8(data, VT1211_REG_UCH_CONFIG);
1096 if (uch_config > -1) {
1097 data->uch_config = (data->uch_config & 0x83) |
1099 vt1211_write8(data, VT1211_REG_UCH_CONFIG, data->uch_config);
1103 * Initialize the interrupt mode (if request at module load time).
1104 * The VT1211 implements 3 different modes for clearing interrupts:
1105 * 0: Clear INT when status register is read. Regenerate INT as long
1106 * as temp stays above hysteresis limit.
1107 * 1: Clear INT when status register is read. DON'T regenerate INT
1108 * until temp falls below hysteresis limit and exceeds hot limit
1110 * 2: Clear INT when temp falls below max limit.
1112 * The driver only allows to force mode 0 since that's the only one
1113 * that makes sense for 'sensors'
1115 if (int_mode == 0) {
1116 vt1211_write8(data, VT1211_REG_TEMP1_CONFIG, 0);
1117 vt1211_write8(data, VT1211_REG_TEMP2_CONFIG, 0);
1120 /* Fill in some hard wired values into our data struct */
1121 data->pwm_auto_pwm[0][3] = 255;
1122 data->pwm_auto_pwm[1][3] = 255;
1125 static void vt1211_remove_sysfs(struct platform_device *pdev)
1127 struct device *dev = &pdev->dev;
1130 for (i = 0; i < ARRAY_SIZE(vt1211_in_attr_group); i++)
1131 sysfs_remove_group(&dev->kobj, &vt1211_in_attr_group[i]);
1133 for (i = 0; i < ARRAY_SIZE(vt1211_temp_attr_group); i++)
1134 sysfs_remove_group(&dev->kobj, &vt1211_temp_attr_group[i]);
1136 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1137 device_remove_file(dev,
1138 &vt1211_sysfs_fan_pwm[i].dev_attr);
1140 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++)
1141 device_remove_file(dev, &vt1211_sysfs_misc[i]);
1144 static int vt1211_probe(struct platform_device *pdev)
1146 struct device *dev = &pdev->dev;
1147 struct vt1211_data *data;
1148 struct resource *res;
1151 data = devm_kzalloc(dev, sizeof(struct vt1211_data), GFP_KERNEL);
1153 dev_err(dev, "Out of memory\n");
1157 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1158 if (!devm_request_region(dev, res->start, resource_size(res),
1160 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1161 (unsigned long)res->start, (unsigned long)res->end);
1164 data->addr = res->start;
1165 data->name = DRVNAME;
1166 mutex_init(&data->update_lock);
1168 platform_set_drvdata(pdev, data);
1170 /* Initialize the VT1211 chip */
1171 vt1211_init_device(data);
1173 /* Create sysfs interface files */
1174 for (i = 0; i < ARRAY_SIZE(vt1211_in_attr_group); i++) {
1175 if (ISVOLT(i, data->uch_config)) {
1176 err = sysfs_create_group(&dev->kobj,
1177 &vt1211_in_attr_group[i]);
1179 goto EXIT_DEV_REMOVE;
1182 for (i = 0; i < ARRAY_SIZE(vt1211_temp_attr_group); i++) {
1183 if (ISTEMP(i, data->uch_config)) {
1184 err = sysfs_create_group(&dev->kobj,
1185 &vt1211_temp_attr_group[i]);
1187 goto EXIT_DEV_REMOVE;
1190 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1191 err = device_create_file(dev,
1192 &vt1211_sysfs_fan_pwm[i].dev_attr);
1194 goto EXIT_DEV_REMOVE;
1196 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1197 err = device_create_file(dev,
1198 &vt1211_sysfs_misc[i]);
1200 goto EXIT_DEV_REMOVE;
1203 /* Register device */
1204 data->hwmon_dev = hwmon_device_register(dev);
1205 if (IS_ERR(data->hwmon_dev)) {
1206 err = PTR_ERR(data->hwmon_dev);
1207 dev_err(dev, "Class registration failed (%d)\n", err);
1208 goto EXIT_DEV_REMOVE_SILENT;
1214 dev_err(dev, "Sysfs interface creation failed (%d)\n", err);
1215 EXIT_DEV_REMOVE_SILENT:
1216 vt1211_remove_sysfs(pdev);
1220 static int vt1211_remove(struct platform_device *pdev)
1222 struct vt1211_data *data = platform_get_drvdata(pdev);
1224 hwmon_device_unregister(data->hwmon_dev);
1225 vt1211_remove_sysfs(pdev);
1230 static struct platform_driver vt1211_driver = {
1232 .owner = THIS_MODULE,
1235 .probe = vt1211_probe,
1236 .remove = vt1211_remove,
1239 static int __init vt1211_device_add(unsigned short address)
1241 struct resource res = {
1243 .end = address + 0x7f,
1244 .flags = IORESOURCE_IO,
1248 pdev = platform_device_alloc(DRVNAME, address);
1251 pr_err("Device allocation failed (%d)\n", err);
1255 res.name = pdev->name;
1256 err = acpi_check_resource_conflict(&res);
1260 err = platform_device_add_resources(pdev, &res, 1);
1262 pr_err("Device resource addition failed (%d)\n", err);
1266 err = platform_device_add(pdev);
1268 pr_err("Device addition failed (%d)\n", err);
1275 platform_device_put(pdev);
1280 static int __init vt1211_find(int sio_cip, unsigned short *address)
1285 superio_enter(sio_cip);
1287 devid = force_id ? force_id : superio_inb(sio_cip, SIO_VT1211_DEVID);
1288 if (devid != SIO_VT1211_ID)
1291 superio_select(sio_cip, SIO_VT1211_LDN_HWMON);
1293 if ((superio_inb(sio_cip, SIO_VT1211_ACTIVE) & 1) == 0) {
1294 pr_warn("HW monitor is disabled, skipping\n");
1298 *address = ((superio_inb(sio_cip, SIO_VT1211_BADDR) << 8) |
1299 (superio_inb(sio_cip, SIO_VT1211_BADDR + 1))) & 0xff00;
1300 if (*address == 0) {
1301 pr_warn("Base address is not set, skipping\n");
1306 pr_info("Found VT1211 chip at 0x%04x, revision %u\n",
1307 *address, superio_inb(sio_cip, SIO_VT1211_DEVREV));
1310 superio_exit(sio_cip);
1314 static int __init vt1211_init(void)
1317 unsigned short address = 0;
1319 err = vt1211_find(SIO_REG_CIP1, &address);
1321 err = vt1211_find(SIO_REG_CIP2, &address);
1326 if ((uch_config < -1) || (uch_config > 31)) {
1328 pr_warn("Invalid UCH configuration %d. "
1329 "Choose a value between 0 and 31.\n", uch_config);
1333 if ((int_mode < -1) || (int_mode > 0)) {
1335 pr_warn("Invalid interrupt mode %d. "
1336 "Only mode 0 is supported.\n", int_mode);
1340 err = platform_driver_register(&vt1211_driver);
1344 /* Sets global pdev as a side effect */
1345 err = vt1211_device_add(address);
1347 goto EXIT_DRV_UNREGISTER;
1351 EXIT_DRV_UNREGISTER:
1352 platform_driver_unregister(&vt1211_driver);
1357 static void __exit vt1211_exit(void)
1359 platform_device_unregister(pdev);
1360 platform_driver_unregister(&vt1211_driver);
1363 MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
1364 MODULE_DESCRIPTION("VT1211 sensors");
1365 MODULE_LICENSE("GPL");
1367 module_init(vt1211_init);
1368 module_exit(vt1211_exit);