Lines Matching +full:scaled +full:- +full:sync
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * vt1211.c - driver for the VIA VT1211 Super-I/O chip integrated hardware
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/hwmon-vid.h>
27 static int uch_config = -1;
31 static int int_mode = -1;
43 /* ---------------------------------------------------------------------
49 * -------- ------------ --------- --------------------------
59 * --------------------------------------------------------------------- */
61 /* Voltages (in) numbered 0-5 (ix) */
66 /* Temperatures (temp) numbered 0-6 (ix) */
71 /* Fans numbered 0-1 (ix) */
76 /* PWMs numbered 0-1 (ix) */
77 /* Auto points numbered 0-3 (ap) */
81 #define VT1211_REG_PWM_AUTO_TEMP(ap) (0x55 - (ap))
82 #define VT1211_REG_PWM_AUTO_PWM(ix, ap) (0x58 + 2 * (ix) - (ap))
98 /* ---------------------------------------------------------------------
100 * --------------------------------------------------------------------- */
133 /* ix = [0-5] */
137 /* ix = [0-6] */
142 * in5 (ix = 5) is special. It's the internal 3.3V so it's scaled in the
146 (((reg) - 3) * 15882 + 479) / 958 : \
147 (((reg) - 3) * 10000 + 479) / 958)
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
156 * temp3-7 are thermistor based so the driver returns the voltage measured at
157 * the pin (range 0V - 2.2V).
161 ((reg) - 51) * 1000 : \
162 ((253 - (reg)) * 2200 + 105) / 210)
166 253 - ((val) * 210 + 1100) / 2200), 0, 255)
176 /* ---------------------------------------------------------------------
177 * Super-I/O constants and functions
178 * --------------------------------------------------------------------- */
219 return -EBUSY; in superio_enter()
233 /* ---------------------------------------------------------------------
235 * --------------------------------------------------------------------- */
239 return inb(data->addr + reg); in vt1211_read8()
244 outb(val, data->addr + reg); in vt1211_write8()
252 mutex_lock(&data->update_lock); in vt1211_update_device()
255 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { in vt1211_update_device()
257 data->vid = vt1211_read8(data, VT1211_REG_VID) & 0x1f; in vt1211_update_device()
260 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) { in vt1211_update_device()
261 if (ISVOLT(ix, data->uch_config)) { in vt1211_update_device()
262 data->in[ix] = vt1211_read8(data, in vt1211_update_device()
264 data->in_min[ix] = vt1211_read8(data, in vt1211_update_device()
266 data->in_max[ix] = vt1211_read8(data, in vt1211_update_device()
272 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) { in vt1211_update_device()
273 if (ISTEMP(ix, data->uch_config)) { in vt1211_update_device()
274 data->temp[ix] = vt1211_read8(data, in vt1211_update_device()
276 data->temp_max[ix] = vt1211_read8(data, in vt1211_update_device()
278 data->temp_hyst[ix] = vt1211_read8(data, in vt1211_update_device()
284 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) { in vt1211_update_device()
285 data->fan[ix] = vt1211_read8(data, in vt1211_update_device()
287 data->fan_min[ix] = vt1211_read8(data, in vt1211_update_device()
289 data->pwm[ix] = vt1211_read8(data, in vt1211_update_device()
293 data->fan_div[0] = (val >> 4) & 3; in vt1211_update_device()
294 data->fan_div[1] = (val >> 6) & 3; in vt1211_update_device()
295 data->fan_ctl = val & 0xf; in vt1211_update_device()
298 data->pwm_ctl[0] = val & 0xf; in vt1211_update_device()
299 data->pwm_ctl[1] = (val >> 4) & 0xf; in vt1211_update_device()
301 data->pwm_clk = vt1211_read8(data, VT1211_REG_PWM_CLK); in vt1211_update_device()
304 data->pwm_auto_pwm[0][1] = vt1211_read8(data, in vt1211_update_device()
306 data->pwm_auto_pwm[0][2] = vt1211_read8(data, in vt1211_update_device()
308 data->pwm_auto_pwm[1][1] = vt1211_read8(data, in vt1211_update_device()
310 data->pwm_auto_pwm[1][2] = vt1211_read8(data, in vt1211_update_device()
312 for (ix = 0; ix < ARRAY_SIZE(data->pwm_auto_temp); ix++) { in vt1211_update_device()
313 data->pwm_auto_temp[ix] = vt1211_read8(data, in vt1211_update_device()
318 data->alarms = (vt1211_read8(data, VT1211_REG_ALARM2) << 8) | in vt1211_update_device()
321 data->last_updated = jiffies; in vt1211_update_device()
322 data->valid = 1; in vt1211_update_device()
325 mutex_unlock(&data->update_lock); in vt1211_update_device()
330 /* ---------------------------------------------------------------------
332 * ix = [0-5]
333 * --------------------------------------------------------------------- */
346 int ix = sensor_attr_2->index; in show_in()
347 int fn = sensor_attr_2->nr; in show_in()
352 res = IN_FROM_REG(ix, data->in[ix]); in show_in()
355 res = IN_FROM_REG(ix, data->in_min[ix]); in show_in()
358 res = IN_FROM_REG(ix, data->in_max[ix]); in show_in()
361 res = (data->alarms >> bitalarmin[ix]) & 1; in show_in()
377 int ix = sensor_attr_2->index; in set_in()
378 int fn = sensor_attr_2->nr; in set_in()
386 mutex_lock(&data->update_lock); in set_in()
389 data->in_min[ix] = IN_TO_REG(ix, val); in set_in()
390 vt1211_write8(data, VT1211_REG_IN_MIN(ix), data->in_min[ix]); in set_in()
393 data->in_max[ix] = IN_TO_REG(ix, val); in set_in()
394 vt1211_write8(data, VT1211_REG_IN_MAX(ix), data->in_max[ix]); in set_in()
399 mutex_unlock(&data->update_lock); in set_in()
404 /* ---------------------------------------------------------------------
406 * ix = [0-6]
407 * --------------------------------------------------------------------- */
420 int ix = sensor_attr_2->index; in show_temp()
421 int fn = sensor_attr_2->nr; in show_temp()
426 res = TEMP_FROM_REG(ix, data->temp[ix]); in show_temp()
429 res = TEMP_FROM_REG(ix, data->temp_max[ix]); in show_temp()
432 res = TEMP_FROM_REG(ix, data->temp_hyst[ix]); in show_temp()
435 res = (data->alarms >> bitalarmtemp[ix]) & 1; in show_temp()
451 int ix = sensor_attr_2->index; in set_temp()
452 int fn = sensor_attr_2->nr; in set_temp()
460 mutex_lock(&data->update_lock); in set_temp()
463 data->temp_max[ix] = TEMP_TO_REG(ix, val); in set_temp()
465 data->temp_max[ix]); in set_temp()
468 data->temp_hyst[ix] = TEMP_TO_REG(ix, val); in set_temp()
470 data->temp_hyst[ix]); in set_temp()
475 mutex_unlock(&data->update_lock); in set_temp()
480 /* ---------------------------------------------------------------------
482 * ix = [0-1]
483 * --------------------------------------------------------------------- */
496 int ix = sensor_attr_2->index; in show_fan()
497 int fn = sensor_attr_2->nr; in show_fan()
502 res = RPM_FROM_REG(data->fan[ix], data->fan_div[ix]); in show_fan()
505 res = RPM_FROM_REG(data->fan_min[ix], data->fan_div[ix]); in show_fan()
508 res = DIV_FROM_REG(data->fan_div[ix]); in show_fan()
511 res = (data->alarms >> bitalarmfan[ix]) & 1; in show_fan()
527 int ix = sensor_attr_2->index; in set_fan()
528 int fn = sensor_attr_2->nr; in set_fan()
537 mutex_lock(&data->update_lock); in set_fan()
539 /* sync the data cache */ in set_fan()
541 data->fan_div[0] = (reg >> 4) & 3; in set_fan()
542 data->fan_div[1] = (reg >> 6) & 3; in set_fan()
543 data->fan_ctl = reg & 0xf; in set_fan()
547 data->fan_min[ix] = RPM_TO_REG(val, data->fan_div[ix]); in set_fan()
549 data->fan_min[ix]); in set_fan()
554 data->fan_div[ix] = 0; in set_fan()
557 data->fan_div[ix] = 1; in set_fan()
560 data->fan_div[ix] = 2; in set_fan()
563 data->fan_div[ix] = 3; in set_fan()
566 count = -EINVAL; in set_fan()
573 ((data->fan_div[1] << 6) | in set_fan()
574 (data->fan_div[0] << 4) | in set_fan()
575 data->fan_ctl)); in set_fan()
582 mutex_unlock(&data->update_lock); in set_fan()
586 /* ---------------------------------------------------------------------
588 * ix = [0-1]
589 * --------------------------------------------------------------------- */
602 int ix = sensor_attr_2->index; in show_pwm()
603 int fn = sensor_attr_2->nr; in show_pwm()
608 res = data->pwm[ix]; in show_pwm()
611 res = ((data->pwm_ctl[ix] >> 3) & 1) ? 2 : 0; in show_pwm()
614 res = 90000 >> (data->pwm_clk & 7); in show_pwm()
617 res = (data->pwm_ctl[ix] & 7) + 1; in show_pwm()
633 int ix = sensor_attr_2->index; in set_pwm()
634 int fn = sensor_attr_2->nr; in set_pwm()
643 mutex_lock(&data->update_lock); in set_pwm()
647 /* sync the data cache */ in set_pwm()
649 data->fan_div[0] = (reg >> 4) & 3; in set_pwm()
650 data->fan_div[1] = (reg >> 6) & 3; in set_pwm()
651 data->fan_ctl = reg & 0xf; in set_pwm()
653 data->pwm_ctl[0] = reg & 0xf; in set_pwm()
654 data->pwm_ctl[1] = (reg >> 4) & 0xf; in set_pwm()
657 data->pwm_ctl[ix] &= 7; in set_pwm()
662 if ((data->pwm_ctl[ix ^ 1] & 1) == 0) in set_pwm()
663 data->fan_ctl &= 0xe; in set_pwm()
666 data->pwm_ctl[ix] |= 8; in set_pwm()
667 data->fan_ctl |= 1; in set_pwm()
670 count = -EINVAL; in set_pwm()
677 ((data->pwm_ctl[1] << 4) | in set_pwm()
678 data->pwm_ctl[0])); in set_pwm()
680 ((data->fan_div[1] << 6) | in set_pwm()
681 (data->fan_div[0] << 4) | in set_pwm()
682 data->fan_ctl)); in set_pwm()
690 /* sync the data cache */ in set_pwm()
692 data->pwm_clk = (reg & 0xf8) | tmp; in set_pwm()
693 vt1211_write8(data, VT1211_REG_PWM_CLK, data->pwm_clk); in set_pwm()
697 count = -EINVAL; in set_pwm()
703 if (!ISTEMP(val - 1, data->uch_config)) { in set_pwm()
704 count = -EINVAL; in set_pwm()
709 /* sync the data cache */ in set_pwm()
711 data->pwm_ctl[0] = reg & 0xf; in set_pwm()
712 data->pwm_ctl[1] = (reg >> 4) & 0xf; in set_pwm()
713 data->pwm_ctl[ix] = (data->pwm_ctl[ix] & 8) | (val - 1); in set_pwm()
715 ((data->pwm_ctl[1] << 4) | data->pwm_ctl[0])); in set_pwm()
722 mutex_unlock(&data->update_lock); in set_pwm()
726 /* ---------------------------------------------------------------------
728 * ix = [0-1]
729 * ap = [0-3]
730 * --------------------------------------------------------------------- */
739 * -------------------
757 int ix = sensor_attr_2->index; in show_pwm_auto_point_temp()
758 int ap = sensor_attr_2->nr; in show_pwm_auto_point_temp()
760 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->pwm_ctl[ix] & 7, in show_pwm_auto_point_temp()
761 data->pwm_auto_temp[ap])); in show_pwm_auto_point_temp()
771 int ix = sensor_attr_2->index; in set_pwm_auto_point_temp()
772 int ap = sensor_attr_2->nr; in set_pwm_auto_point_temp()
782 mutex_lock(&data->update_lock); in set_pwm_auto_point_temp()
784 /* sync the data cache */ in set_pwm_auto_point_temp()
786 data->pwm_ctl[0] = reg & 0xf; in set_pwm_auto_point_temp()
787 data->pwm_ctl[1] = (reg >> 4) & 0xf; in set_pwm_auto_point_temp()
789 data->pwm_auto_temp[ap] = TEMP_TO_REG(data->pwm_ctl[ix] & 7, val); in set_pwm_auto_point_temp()
791 data->pwm_auto_temp[ap]); in set_pwm_auto_point_temp()
792 mutex_unlock(&data->update_lock); in set_pwm_auto_point_temp()
799 * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't
803 * -------------------
804 * 0 0 : pwm1 off (pwm_auto_pwm[0][0], hard-wired to 0)
807 * 0 3 : pwm1 full speed (pwm_auto_pwm[0][3], hard-wired to 255)
808 * 1 0 : pwm2 off (pwm_auto_pwm[1][0], hard-wired to 0)
811 * 1 3 : pwm2 full speed (pwm_auto_pwm[1][3], hard-wired to 255)
821 int ix = sensor_attr_2->index; in show_pwm_auto_point_pwm()
822 int ap = sensor_attr_2->nr; in show_pwm_auto_point_pwm()
824 return sprintf(buf, "%d\n", data->pwm_auto_pwm[ix][ap]); in show_pwm_auto_point_pwm()
834 int ix = sensor_attr_2->index; in set_pwm_auto_point_pwm()
835 int ap = sensor_attr_2->nr; in set_pwm_auto_point_pwm()
843 mutex_lock(&data->update_lock); in set_pwm_auto_point_pwm()
844 data->pwm_auto_pwm[ix][ap] = clamp_val(val, 0, 255); in set_pwm_auto_point_pwm()
846 data->pwm_auto_pwm[ix][ap]); in set_pwm_auto_point_pwm()
847 mutex_unlock(&data->update_lock); in set_pwm_auto_point_pwm()
852 /* ---------------------------------------------------------------------
854 * --------------------------------------------------------------------- */
861 return sprintf(buf, "%d\n", data->vrm); in show_vrm()
876 return -EINVAL; in set_vrm()
878 data->vrm = val; in set_vrm()
888 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); in show_vid()
896 return sprintf(buf, "%s\n", data->name); in show_name()
904 return sprintf(buf, "%d\n", data->alarms); in show_alarms()
907 /* ---------------------------------------------------------------------
909 * --------------------------------------------------------------------- */
959 show_temp, NULL, SHOW_TEMP_INPUT, ix-1), \
961 show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1), \
963 show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1), \
965 show_temp, NULL, SHOW_TEMP_ALARM, ix-1) \
1008 show_fan, NULL, SHOW_FAN_INPUT, ix-1), \
1010 show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \
1012 show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \
1014 show_fan, NULL, SHOW_FAN_ALARM, ix-1)
1018 show_pwm, NULL, SHOW_PWM, ix-1), \
1020 show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \
1022 show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1)
1026 show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1)
1030 show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1)
1035 ap-1, ix-1)
1040 ap-1, ix-1)
1045 ap-1, ix-1)
1050 ap-1, ix-1)
1084 /* ---------------------------------------------------------------------
1086 * --------------------------------------------------------------------- */
1091 data->vrm = vid_which_vrm(); in vt1211_init_device()
1094 data->uch_config = vt1211_read8(data, VT1211_REG_UCH_CONFIG); in vt1211_init_device()
1095 if (uch_config > -1) { in vt1211_init_device()
1096 data->uch_config = (data->uch_config & 0x83) | in vt1211_init_device()
1098 vt1211_write8(data, VT1211_REG_UCH_CONFIG, data->uch_config); in vt1211_init_device()
1120 data->pwm_auto_pwm[0][3] = 255; in vt1211_init_device()
1121 data->pwm_auto_pwm[1][3] = 255; in vt1211_init_device()
1126 struct device *dev = &pdev->dev; in vt1211_remove_sysfs()
1130 sysfs_remove_group(&dev->kobj, &vt1211_in_attr_group[i]); in vt1211_remove_sysfs()
1133 sysfs_remove_group(&dev->kobj, &vt1211_temp_attr_group[i]); in vt1211_remove_sysfs()
1145 struct device *dev = &pdev->dev; in vt1211_probe()
1152 return -ENOMEM; in vt1211_probe()
1155 if (!devm_request_region(dev, res->start, resource_size(res), in vt1211_probe()
1157 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n", in vt1211_probe()
1158 (unsigned long)res->start, (unsigned long)res->end); in vt1211_probe()
1159 return -EBUSY; in vt1211_probe()
1161 data->addr = res->start; in vt1211_probe()
1162 data->name = DRVNAME; in vt1211_probe()
1163 mutex_init(&data->update_lock); in vt1211_probe()
1172 if (ISVOLT(i, data->uch_config)) { in vt1211_probe()
1173 err = sysfs_create_group(&dev->kobj, in vt1211_probe()
1180 if (ISTEMP(i, data->uch_config)) { in vt1211_probe()
1181 err = sysfs_create_group(&dev->kobj, in vt1211_probe()
1201 data->hwmon_dev = hwmon_device_register(dev); in vt1211_probe()
1202 if (IS_ERR(data->hwmon_dev)) { in vt1211_probe()
1203 err = PTR_ERR(data->hwmon_dev); in vt1211_probe()
1221 hwmon_device_unregister(data->hwmon_dev); in vt1211_remove()
1246 err = -ENOMEM; in vt1211_device_add()
1251 res.name = pdev->name; in vt1211_device_add()
1285 err = -ENODEV; in vt1211_find()
1325 if ((uch_config < -1) || (uch_config > 31)) { in vt1211_init()
1326 err = -EINVAL; in vt1211_init()
1332 if ((int_mode < -1) || (int_mode > 0)) { in vt1211_init()
1333 err = -EINVAL; in vt1211_init()