24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/module.h>
28 #include <linux/slab.h>
40 static int uch_config = -1;
42 MODULE_PARM_DESC(uch_config,
"Initialize the universal channel configuration");
44 static int int_mode = -1;
48 static unsigned short force_id;
54 #define DRVNAME "vt1211"
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))
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};
85 #define VT1211_REG_FAN(ix) (0x29 + (ix))
86 #define VT1211_REG_FAN_MIN(ix) (0x3b + (ix))
87 #define VT1211_REG_FAN_DIV 0x47
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))
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
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};
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)
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) (SENSORS_LIMIT((ix) == 5 ? \
162 ((val) * 958 + 7941) / 15882 + 3 : \
163 ((val) * 958 + 5000) / 10000 + 3, 0, 255))
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) SENSORS_LIMIT( \
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 SENSORS_LIMIT((1310720 / (val) / \
187 DIV_FROM_REG(div)), 1, 254))
197 #define SIO_REG_CIP1 0x2e
198 #define SIO_REG_CIP2 0x4e
201 #define SIO_VT1211_LDN 0x07
202 #define SIO_VT1211_DEVID 0x20
203 #define SIO_VT1211_DEVREV 0x21
204 #define SIO_VT1211_ACTIVE 0x30
205 #define SIO_VT1211_BADDR 0x60
206 #define SIO_VT1211_ID 0x3c
209 #define SIO_VT1211_LDN_HWMON 0x0b
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);
226 outb(ldn, sio_cip + 1);
229 static inline void superio_enter(
int sio_cip)
235 static inline void superio_exit(
int sio_cip)
269 data->
in[ix] = vt1211_read8(data,
271 data->
in_min[ix] = vt1211_read8(data,
273 data->
in_max[ix] = vt1211_read8(data,
281 data->
temp[ix] = vt1211_read8(data,
283 data->
temp_max[ix] = vt1211_read8(data,
292 data->
fan[ix] = vt1211_read8(data,
294 data->
fan_min[ix] = vt1211_read8(data,
296 data->
pwm[ix] = vt1211_read8(data,
300 data->
fan_div[0] = (val >> 4) & 3;
301 data->
fan_div[1] = (val >> 6) & 3;
306 data->
pwm_ctl[1] = (val >> 4) & 0xf;
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
350 struct vt1211_data *data = vt1211_update_device(dev);
353 int ix = sensor_attr_2->
index;
354 int fn = sensor_attr_2->
nr;
368 res = (data->
alarms >> bitalarmin[ix]) & 1;
372 dev_dbg(dev,
"Unknown attr fetch (%d)\n", fn);
375 return sprintf(buf,
"%d\n", res);
384 int ix = sensor_attr_2->
index;
385 int fn = sensor_attr_2->
nr;
389 err = kstrtol(buf, 10, &val);
404 dev_dbg(dev,
"Unknown attr fetch (%d)\n", fn);
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
424 struct vt1211_data *data = vt1211_update_device(dev);
427 int ix = sensor_attr_2->
index;
428 int fn = sensor_attr_2->
nr;
442 res = (data->
alarms >> bitalarmtemp[ix]) & 1;
446 dev_dbg(dev,
"Unknown attr fetch (%d)\n", fn);
449 return sprintf(buf,
"%d\n", res);
453 const char *buf,
size_t count)
458 int ix = sensor_attr_2->
index;
459 int fn = sensor_attr_2->
nr;
463 err = kstrtol(buf, 10, &val);
471 vt1211_write8(data, regtempmax[ix],
476 vt1211_write8(data, regtemphyst[ix],
480 dev_dbg(dev,
"Unknown attr fetch (%d)\n", fn);
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
500 struct vt1211_data *data = vt1211_update_device(dev);
503 int ix = sensor_attr_2->
index;
504 int fn = sensor_attr_2->
nr;
518 res = (data->
alarms >> bitalarmfan[ix]) & 1;
522 dev_dbg(dev,
"Unknown attr fetch (%d)\n", fn);
525 return sprintf(buf,
"%d\n", res);
529 const char *buf,
size_t count)
534 int ix = sensor_attr_2->
index;
535 int fn = sensor_attr_2->
nr;
540 err = kstrtoul(buf, 10, &val);
548 data->
fan_div[0] = (reg >> 4) & 3;
549 data->
fan_div[1] = (reg >> 6) & 3;
574 dev_warn(dev,
"fan div value %ld not supported. "
575 "Choose one of 1, 2, 4, or 8.\n", val);
584 dev_dbg(dev,
"Unknown attr fetch (%d)\n", fn);
598 #define SHOW_SET_PWM_ENABLE 1
599 #define SHOW_SET_PWM_FREQ 2
600 #define SHOW_SET_PWM_AUTO_CHANNELS_TEMP 3
605 struct vt1211_data *data = vt1211_update_device(dev);
608 int ix = sensor_attr_2->
index;
609 int fn = sensor_attr_2->
nr;
617 res = ((data->
pwm_ctl[ix] >> 3) & 1) ? 2 : 0;
620 res = 90000 >> (data->
pwm_clk & 7);
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);
634 const char *buf,
size_t count)
639 int ix = sensor_attr_2->
index;
640 int fn = sensor_attr_2->
nr;
645 err = kstrtoul(buf, 10, &val);
655 data->
fan_div[0] = (reg >> 4) & 3;
656 data->
fan_div[1] = (reg >> 6) & 3;
660 data->
pwm_ctl[1] = (reg >> 4) & 0xf;
668 if ((data->
pwm_ctl[ix ^ 1] & 1) == 0)
677 dev_warn(dev,
"pwm mode %ld not supported. "
678 "Choose one of 0 or 2.\n", val);
690 val = 135000 / SENSORS_LIMIT(val, 135000 >> 7, 135000);
693 for (val >>= 1; val > 0; val >>= 1)
697 data->
pwm_clk = (reg & 0xf8) | tmp;
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);
709 dev_warn(dev,
"temp channel %ld is not available.\n",
716 data->
pwm_ctl[1] = (reg >> 4) & 0xf;
722 dev_dbg(dev,
"Unknown attr fetch (%d)\n", fn);
758 struct vt1211_data *data = vt1211_update_device(dev);
761 int ix = sensor_attr_2->
index;
762 int ap = sensor_attr_2->
nr;
770 const char *buf,
size_t count)
775 int ix = sensor_attr_2->
index;
776 int ap = sensor_attr_2->
nr;
781 err = kstrtol(buf, 10, &val);
791 data->
pwm_ctl[1] = (reg >> 4) & 0xf;
822 struct vt1211_data *data = vt1211_update_device(dev);
825 int ix = sensor_attr_2->
index;
826 int ap = sensor_attr_2->
nr;
833 const char *buf,
size_t count)
838 int ix = sensor_attr_2->
index;
839 int ap = sensor_attr_2->
nr;
843 err = kstrtoul(buf, 10, &val);
848 data->
pwm_auto_pwm[ix][ap] = SENSORS_LIMIT(val, 0, 255);
869 const char *buf,
size_t count)
875 err = kstrtoul(buf, 10, &val);
903 struct vt1211_data *data = vt1211_update_device(dev);
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) \
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] = {
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) \
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] = {
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, \
1096 if (uch_config > -1) {
1115 if (int_mode == 0) {
1130 for (i = 0; i <
ARRAY_SIZE(vt1211_in_attr_group); i++)
1133 for (i = 0; i <
ARRAY_SIZE(vt1211_temp_attr_group); i++)
1136 for (i = 0; i <
ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1138 &vt1211_sysfs_fan_pwm[i].
dev_attr);
1140 for (i = 0; i <
ARRAY_SIZE(vt1211_sysfs_misc); i++)
1153 dev_err(dev,
"Out of memory\n");
1160 dev_err(dev,
"Failed to request region 0x%lx-0x%lx\n",
1161 (
unsigned long)res->
start, (
unsigned long)res->
end);
1168 platform_set_drvdata(pdev, data);
1171 vt1211_init_device(data);
1174 for (i = 0; i <
ARRAY_SIZE(vt1211_in_attr_group); i++) {
1177 &vt1211_in_attr_group[i]);
1179 goto EXIT_DEV_REMOVE;
1182 for (i = 0; i <
ARRAY_SIZE(vt1211_temp_attr_group); i++) {
1185 &vt1211_temp_attr_group[i]);
1187 goto EXIT_DEV_REMOVE;
1190 for (i = 0; i <
ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1192 &vt1211_sysfs_fan_pwm[i].
dev_attr);
1194 goto EXIT_DEV_REMOVE;
1196 for (i = 0; i <
ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1198 &vt1211_sysfs_misc[i]);
1200 goto EXIT_DEV_REMOVE;
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);
1222 struct vt1211_data *data = platform_get_drvdata(pdev);
1225 vt1211_remove_sysfs(pdev);
1235 .probe = vt1211_probe,
1243 .end = address + 0x7f,
1251 pr_err(
"Device allocation failed (%d)\n", err);
1262 pr_err(
"Device resource addition failed (%d)\n", err);
1268 pr_err(
"Device addition failed (%d)\n", err);
1280 static int __init vt1211_find(
int sio_cip,
unsigned short *address)
1285 superio_enter(sio_cip);
1294 pr_warn(
"HW monitor is disabled, skipping\n");
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",
1310 superio_exit(sio_cip);
1314 static int __init vt1211_init(
void)
1317 unsigned short address = 0;
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);
1345 err = vt1211_device_add(address);
1347 goto EXIT_DRV_UNREGISTER;
1351 EXIT_DRV_UNREGISTER:
1357 static void __exit vt1211_exit(
void)