Lines Matching +full:temp +full:- +full:alarm

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * nct7802 - Driver for Nuvoton NCT7802Y
5 * Copyright (C) 2014 Guenter Roeck <linux@roeck-us.net>
14 #include <linux/hwmon-sysfs.h>
77 struct mutex access_lock; /* for multi-byte read and write operations */
90 ret = regmap_read(data->regmap, REG_MODE, &mode); in temp_type_show()
94 return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2); in temp_type_show()
109 if (sattr->index == 2 && type != 4) /* RD3 */ in temp_type_store()
110 return -EINVAL; in temp_type_store()
112 return -EINVAL; in temp_type_store()
113 err = regmap_update_bits(data->regmap, REG_MODE, in temp_type_store()
114 3 << 2 * sattr->index, (type - 2) << 2 * sattr->index); in temp_type_store()
126 if (sattr->index > 1) in pwm_mode_show()
129 ret = regmap_read(data->regmap, 0x5E, &regval); in pwm_mode_show()
133 return sprintf(buf, "%u\n", !(regval & (1 << sattr->index))); in pwm_mode_show()
144 if (!attr->index) in pwm_show()
147 ret = regmap_read(data->regmap, attr->index, &val); in pwm_show()
166 err = regmap_write(data->regmap, attr->index, val); in pwm_store()
178 ret = regmap_read(data->regmap, REG_SMARTFAN_EN(sattr->index), &reg); in pwm_enable_show()
181 enabled = reg >> SMARTFAN_EN_SHIFT(sattr->index) & 1; in pwm_enable_show()
198 return -EINVAL; in pwm_enable_store()
199 ret = regmap_update_bits(data->regmap, REG_SMARTFAN_EN(sattr->index), in pwm_enable_store()
200 1 << SMARTFAN_EN_SHIFT(sattr->index), in pwm_enable_store()
201 (val - 1) << SMARTFAN_EN_SHIFT(sattr->index)); in pwm_enable_store()
206 u8 reg_temp, u8 reg_temp_low, int *temp) in nct7802_read_temp() argument
211 *temp = 0; in nct7802_read_temp()
213 mutex_lock(&data->access_lock); in nct7802_read_temp()
214 err = regmap_read(data->regmap, reg_temp, &t1); in nct7802_read_temp()
219 err = regmap_read(data->regmap, reg_temp_low, &t2); in nct7802_read_temp()
224 *temp = (s16)t1 / 32 * 125; in nct7802_read_temp()
226 mutex_unlock(&data->access_lock); in nct7802_read_temp()
235 mutex_lock(&data->access_lock); in nct7802_read_fan()
236 ret = regmap_read(data->regmap, reg_fan, &f1); in nct7802_read_fan()
239 ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2); in nct7802_read_fan()
249 mutex_unlock(&data->access_lock); in nct7802_read_fan()
259 mutex_lock(&data->access_lock); in nct7802_read_fan_min()
260 ret = regmap_read(data->regmap, reg_fan_low, &f1); in nct7802_read_fan_min()
263 ret = regmap_read(data->regmap, reg_fan_high, &f2); in nct7802_read_fan_min()
275 mutex_unlock(&data->access_lock); in nct7802_read_fan_min()
290 mutex_lock(&data->access_lock); in nct7802_write_fan_min()
291 err = regmap_write(data->regmap, reg_fan_low, limit & 0xff); in nct7802_write_fan_min()
295 err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5); in nct7802_write_fan_min()
297 mutex_unlock(&data->access_lock); in nct7802_write_fan_min()
308 mutex_lock(&data->access_lock); in nct7802_read_voltage()
310 ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1); in nct7802_read_voltage()
313 ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2); in nct7802_read_voltage()
318 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr]; in nct7802_read_voltage()
320 ret = regmap_read(data->regmap, in nct7802_read_voltage()
321 REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1); in nct7802_read_voltage()
324 ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr], in nct7802_read_voltage()
331 mutex_unlock(&data->access_lock); in nct7802_read_voltage()
338 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr]; in nct7802_write_voltage()
344 mutex_lock(&data->access_lock); in nct7802_write_voltage()
345 err = regmap_write(data->regmap, in nct7802_write_voltage()
346 REG_VOLTAGE_LIMIT_LSB[index - 1][nr], in nct7802_write_voltage()
351 err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr], in nct7802_write_voltage()
354 mutex_unlock(&data->access_lock); in nct7802_write_voltage()
365 voltage = nct7802_read_voltage(data, sattr->nr, sattr->index); in in_show()
377 int index = sattr->index; in in_store()
378 int nr = sattr->nr; in in_store()
398 mutex_lock(&data->in_alarm_lock); in in_alarm_show()
412 ret = regmap_read(data->regmap, 0x1e, &val); /* SMI Voltage status */ in in_alarm_show()
417 data->in_status &= ~((val & 0x0f) << 4); in in_alarm_show()
420 if (!(data->in_status & (0x10 << sattr->index))) { in in_alarm_show()
421 ret = nct7802_read_voltage(data, sattr->nr, 0); in in_alarm_show()
426 ret = nct7802_read_voltage(data, sattr->nr, 1); in in_alarm_show()
431 ret = nct7802_read_voltage(data, sattr->nr, 2); in in_alarm_show()
437 data->in_status |= (1 << sattr->index); in in_alarm_show()
439 data->in_status &= ~(1 << sattr->index); in in_alarm_show()
441 data->in_status |= 0x10 << sattr->index; in in_alarm_show()
444 ret = sprintf(buf, "%u\n", !!(data->in_status & (1 << sattr->index))); in in_alarm_show()
446 mutex_unlock(&data->in_alarm_lock); in in_alarm_show()
455 int err, temp; in temp_show() local
457 err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp); in temp_show()
461 return sprintf(buf, "%d\n", temp); in temp_show()
469 int nr = sattr->nr; in temp_store()
477 val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000); in temp_store()
479 err = regmap_write(data->regmap, nr, val & 0xff); in temp_store()
490 speed = nct7802_read_fan(data, sattr->index); in fan_show()
504 speed = nct7802_read_fan_min(data, sattr->nr, sattr->index); in fan_min_show()
524 err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val); in fan_min_store()
533 int bit = sattr->index; in alarm_show()
537 ret = regmap_read(data->regmap, sattr->nr, &val); in alarm_show()
552 err = regmap_read(data->regmap, sattr->nr, &regval); in beep_show()
556 return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index))); in beep_show()
572 return -EINVAL; in beep_store()
574 err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index, in beep_store()
575 val ? 1 << sattr->index : 0); in beep_store()
580 static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0x01, REG_TEMP_LSB);
581 static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0x31, 0);
582 static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0x30, 0);
583 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 0x3a, 0);
586 static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0x02, REG_TEMP_LSB);
587 static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 0x33, 0);
588 static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 0x32, 0);
589 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 0x3b, 0);
592 static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0x03, REG_TEMP_LSB);
593 static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 0x35, 0);
594 static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 0x34, 0);
595 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 0x3c, 0);
597 static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 0x04, 0);
598 static SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 0x37, 0);
599 static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 0x36, 0);
600 static SENSOR_DEVICE_ATTR_2_RW(temp4_crit, temp, 0x3d, 0);
602 static SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 0x06, REG_TEMP_PECI_LSB);
603 static SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 0x39, 0);
604 static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 0x38, 0);
605 static SENSOR_DEVICE_ATTR_2_RW(temp5_crit, temp, 0x3e, 0);
607 static SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 0x07, REG_TEMP_PECI_LSB);
609 static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, alarm, 0x18, 0);
610 static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, alarm, 0x18, 1);
611 static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, alarm, 0x18, 2);
612 static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, alarm, 0x18, 3);
613 static SENSOR_DEVICE_ATTR_2_RO(temp5_min_alarm, alarm, 0x18, 4);
615 static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, alarm, 0x19, 0);
616 static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, alarm, 0x19, 1);
617 static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, alarm, 0x19, 2);
618 static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, alarm, 0x19, 3);
619 static SENSOR_DEVICE_ATTR_2_RO(temp5_max_alarm, alarm, 0x19, 4);
621 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, alarm, 0x1b, 0);
622 static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, alarm, 0x1b, 1);
623 static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, alarm, 0x1b, 2);
624 static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, alarm, 0x1b, 3);
625 static SENSOR_DEVICE_ATTR_2_RO(temp5_crit_alarm, alarm, 0x1b, 4);
627 static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, alarm, 0x17, 0);
628 static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, alarm, 0x17, 1);
629 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, alarm, 0x17, 2);
704 err = regmap_read(data->regmap, REG_MODE, &reg); in nct7802_temp_is_visible()
719 return attr->mode; in nct7802_temp_is_visible()
721 err = regmap_read(data->regmap, REG_PECI_ENABLE, &reg); in nct7802_temp_is_visible()
731 return attr->mode; in nct7802_temp_is_visible()
804 return attr->mode; in nct7802_in_is_visible()
806 err = regmap_read(data->regmap, REG_MODE, &reg); in nct7802_in_is_visible()
817 return attr->mode; in nct7802_in_is_visible()
827 static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, alarm, 0x1a, 0);
831 static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, alarm, 0x1a, 1);
835 static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, alarm, 0x1a, 2);
879 err = regmap_read(data->regmap, REG_FAN_ENABLE, &reg); in nct7802_fan_is_visible()
883 return attr->mode; in nct7802_fan_is_visible()
908 /* 7.2.115... 0x80-0x83, 0x84 Temperature (X-axis) transition */
909 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, temp, 0x80, 0);
910 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, temp, 0x81, 0);
911 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, temp, 0x82, 0);
912 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, temp, 0x83, 0);
913 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, temp, 0x84, 0);
915 /* 7.2.120... 0x85-0x88 PWM (Y-axis) transition */
922 /* 7.2.124 Table 2 X-axis Transition Point 1 Register */
923 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, temp, 0x90, 0);
924 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, temp, 0x91, 0);
925 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, temp, 0x92, 0);
926 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, temp, 0x93, 0);
927 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, temp, 0x94, 0);
929 /* 7.2.129 Table 2 Y-axis Transition Point 1 Register */
936 /* 7.2.133 Table 3 X-axis Transition Point 1 Register */
937 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, temp, 0xA0, 0);
938 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, temp, 0xA1, 0);
939 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, temp, 0xA2, 0);
940 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, temp, 0xA3, 0);
941 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, temp, 0xA4, 0);
943 /* 7.2.138 Table 3 Y-axis Transition Point 1 Register */
1014 return -ENODEV; in nct7802_detect()
1018 return -ENODEV; in nct7802_detect()
1022 return -ENODEV; in nct7802_detect()
1026 return -ENODEV; in nct7802_detect()
1031 return -ENODEV; in nct7802_detect()
1035 return -ENODEV; in nct7802_detect()
1039 return -ENODEV; in nct7802_detect()
1041 strscpy(info->type, "nct7802", I2C_NAME_SIZE); in nct7802_detect()
1066 if (!node->name || of_node_cmp(node->name, "channel")) in nct7802_get_channel_config()
1071 node->full_name); in nct7802_get_channel_config()
1072 return -EINVAL; in nct7802_get_channel_config()
1077 node->full_name); in nct7802_get_channel_config()
1078 return -EINVAL; in nct7802_get_channel_config()
1093 *mode_val &= ~(MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1)); in nct7802_get_channel_config()
1094 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1); in nct7802_get_channel_config()
1098 if (of_property_read_string(node, "sensor-type", &type_str)) { in nct7802_get_channel_config()
1099 dev_err(dev, "No type for '%s'\n", node->full_name); in nct7802_get_channel_config()
1100 return -EINVAL; in nct7802_get_channel_config()
1105 << MODE_BIT_OFFSET_RTD(reg - 1); in nct7802_get_channel_config()
1106 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1); in nct7802_get_channel_config()
1112 node->full_name); in nct7802_get_channel_config()
1113 return -EINVAL; in nct7802_get_channel_config()
1120 if (of_property_read_string(node, "temperature-mode", in nct7802_get_channel_config()
1122 dev_err(dev, "No mode for '%s'\n", node->full_name); in nct7802_get_channel_config()
1123 return -EINVAL; in nct7802_get_channel_config()
1126 if (!strcmp(md_str, "thermal-diode")) in nct7802_get_channel_config()
1132 node->full_name); in nct7802_get_channel_config()
1133 return -EINVAL; in nct7802_get_channel_config()
1137 *mode_val |= (md & MODE_RTD_MASK) << MODE_BIT_OFFSET_RTD(reg - 1); in nct7802_get_channel_config()
1138 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1); in nct7802_get_channel_config()
1151 if (dev->of_node) { in nct7802_configure_channels()
1152 for_each_child_of_node(dev->of_node, node) { in nct7802_configure_channels()
1162 return regmap_update_bits(data->regmap, REG_MODE, mode_mask, mode_val); in nct7802_configure_channels()
1170 err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01); in nct7802_init_chip()
1179 return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03); in nct7802_init_chip()
1184 struct device *dev = &client->dev; in nct7802_probe()
1191 return -ENOMEM; in nct7802_probe()
1193 data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config); in nct7802_probe()
1194 if (IS_ERR(data->regmap)) in nct7802_probe()
1195 return PTR_ERR(data->regmap); in nct7802_probe()
1197 mutex_init(&data->access_lock); in nct7802_probe()
1198 mutex_init(&data->in_alarm_lock); in nct7802_probe()
1204 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, in nct7802_probe()
1233 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");