Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (adm1031) Allow setting update rate

Based on earlier work by Ira W. Snyder.

The adm1031 chip is capable of using a runtime configurable sampling rate,
using the fan filter register. Add support for reading and setting the
update rate via sysfs.

Signed-off-by: Jean Delvare <khali@linux-fr.org>
Acked-by: Ira W. Snyder <iws@ovro.caltech.edu>

+66 -2
+66 -2
drivers/hwmon/adm1031.c
··· 36 36 #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr)) 37 37 #define ADM1031_REG_PWM (0x22) 38 38 #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr)) 39 + #define ADM1031_REG_FAN_FILTER (0x23) 39 40 40 41 #define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr)) 41 42 #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr)) ··· 62 61 #define ADM1031_CONF2_TACH2_ENABLE 0x08 63 62 #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan)) 64 63 64 + #define ADM1031_UPDATE_RATE_MASK 0x1c 65 + #define ADM1031_UPDATE_RATE_SHIFT 2 66 + 65 67 /* Addresses to scan */ 66 68 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; 67 69 ··· 79 75 int chip_type; 80 76 char valid; /* !=0 if following fields are valid */ 81 77 unsigned long last_updated; /* In jiffies */ 78 + unsigned int update_rate; /* In milliseconds */ 82 79 /* The chan_select_table contains the possible configurations for 83 80 * auto fan control. 84 81 */ ··· 743 738 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13); 744 739 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14); 745 740 741 + /* Update Rate */ 742 + static const unsigned int update_rates[] = { 743 + 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 744 + }; 745 + 746 + static ssize_t show_update_rate(struct device *dev, 747 + struct device_attribute *attr, char *buf) 748 + { 749 + struct i2c_client *client = to_i2c_client(dev); 750 + struct adm1031_data *data = i2c_get_clientdata(client); 751 + 752 + return sprintf(buf, "%u\n", data->update_rate); 753 + } 754 + 755 + static ssize_t set_update_rate(struct device *dev, 756 + struct device_attribute *attr, 757 + const char *buf, size_t count) 758 + { 759 + struct i2c_client *client = to_i2c_client(dev); 760 + struct adm1031_data *data = i2c_get_clientdata(client); 761 + unsigned long val; 762 + int i, err; 763 + u8 reg; 764 + 765 + err = strict_strtoul(buf, 10, &val); 766 + if (err) 767 + return err; 768 + 769 + /* find the nearest update rate from the table */ 770 + for (i = 0; i < ARRAY_SIZE(update_rates) - 1; i++) { 771 + if (val >= update_rates[i]) 772 + break; 773 + } 774 + /* if not found, we point to the last entry (lowest update rate) */ 775 + 776 + /* set the new update rate while preserving other settings */ 777 + reg = adm1031_read_value(client, ADM1031_REG_FAN_FILTER); 778 + reg &= ~ADM1031_UPDATE_RATE_MASK; 779 + reg |= i << ADM1031_UPDATE_RATE_SHIFT; 780 + adm1031_write_value(client, ADM1031_REG_FAN_FILTER, reg); 781 + 782 + mutex_lock(&data->update_lock); 783 + data->update_rate = update_rates[i]; 784 + mutex_unlock(&data->update_lock); 785 + 786 + return count; 787 + } 788 + 789 + static DEVICE_ATTR(update_rate, S_IRUGO | S_IWUSR, show_update_rate, 790 + set_update_rate); 791 + 746 792 static struct attribute *adm1031_attributes[] = { 747 793 &sensor_dev_attr_fan1_input.dev_attr.attr, 748 794 &sensor_dev_attr_fan1_div.dev_attr.attr, ··· 830 774 831 775 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr, 832 776 777 + &dev_attr_update_rate.attr, 833 778 &dev_attr_alarms.attr, 834 779 835 780 NULL ··· 957 900 { 958 901 unsigned int read_val; 959 902 unsigned int mask; 903 + int i; 960 904 struct adm1031_data *data = i2c_get_clientdata(client); 961 905 962 906 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE); ··· 977 919 ADM1031_CONF1_MONITOR_ENABLE); 978 920 } 979 921 922 + /* Read the chip's update rate */ 923 + mask = ADM1031_UPDATE_RATE_MASK; 924 + read_val = adm1031_read_value(client, ADM1031_REG_FAN_FILTER); 925 + i = (read_val & mask) >> ADM1031_UPDATE_RATE_SHIFT; 926 + data->update_rate = update_rates[i]; 980 927 } 981 928 982 929 static struct adm1031_data *adm1031_update_device(struct device *dev) 983 930 { 984 931 struct i2c_client *client = to_i2c_client(dev); 985 932 struct adm1031_data *data = i2c_get_clientdata(client); 933 + unsigned long next_update; 986 934 int chan; 987 935 988 936 mutex_lock(&data->update_lock); 989 937 990 - if (time_after(jiffies, data->last_updated + HZ + HZ / 2) 991 - || !data->valid) { 938 + next_update = data->last_updated + msecs_to_jiffies(data->update_rate); 939 + if (time_after(jiffies, next_update) || !data->valid) { 992 940 993 941 dev_dbg(&client->dev, "Starting adm1031 update\n"); 994 942 for (chan = 0;