24 #include <linux/module.h>
26 #include <linux/slab.h>
28 #include <linux/i2c.h>
35 #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
36 #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
37 #define ADM1031_REG_PWM (0x22)
38 #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
39 #define ADM1031_REG_FAN_FILTER (0x23)
41 #define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
42 #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
43 #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
44 #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
46 #define ADM1031_REG_TEMP(nr) (0x0a + (nr))
47 #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
49 #define ADM1031_REG_STATUS(nr) (0x2 + (nr))
51 #define ADM1031_REG_CONF1 0x00
52 #define ADM1031_REG_CONF2 0x01
53 #define ADM1031_REG_EXT_TEMP 0x06
55 #define ADM1031_CONF1_MONITOR_ENABLE 0x01
56 #define ADM1031_CONF1_PWM_INVERT 0x08
57 #define ADM1031_CONF1_AUTO_MODE 0x80
59 #define ADM1031_CONF2_PWM1_ENABLE 0x01
60 #define ADM1031_CONF2_PWM2_ENABLE 0x02
61 #define ADM1031_CONF2_TACH1_ENABLE 0x04
62 #define ADM1031_CONF2_TACH2_ENABLE 0x08
63 #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
65 #define ADM1031_UPDATE_RATE_MASK 0x1c
66 #define ADM1031_UPDATE_RATE_SHIFT 2
69 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e,
I2C_CLIENT_END };
129 .probe = adm1031_probe,
130 .remove = adm1031_remove,
131 .id_table = adm1031_id,
132 .detect = adm1031_detect,
133 .address_list = normal_i2c,
148 #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
149 ((val + 500) / 1000)))
151 #define TEMP_FROM_REG(val) ((val) * 1000)
153 #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
155 #define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
156 #define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
157 (val) | 0x70 : (val))
159 #define FAN_FROM_REG(reg, div) ((reg) ? \
160 (11250 * 60) / ((reg) * (div)) : 0)
166 return tmp > 255 ? 255 :
tmp;
169 #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
171 #define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
172 #define PWM_FROM_REG(val) ((val) << 4)
174 #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
175 #define FAN_CHAN_TO_REG(val, reg) \
176 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
178 #define AUTO_TEMP_MIN_TO_REG(val, reg) \
179 ((((val) / 500) & 0xf8) | ((reg) & 0x7))
180 #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1 << ((reg) & 0x7)))
181 #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
183 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
185 #define AUTO_TEMP_OFF_FROM_REG(reg) \
186 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
188 #define AUTO_TEMP_MAX_FROM_REG(reg) \
189 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
190 AUTO_TEMP_MIN_FROM_REG(reg))
192 static int AUTO_TEMP_MAX_TO_REG(
int val,
int reg,
int pwm)
198 ret = ((reg & 0xf8) |
201 range < 40000 ? 2 : range < 80000 ? 3 : 4));
206 #define GET_FAN_AUTO_BITFIELD(data, idx) \
207 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2]
216 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
224 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
239 int first_match = -1, exact_match = -1;
246 for (i = 0; i < 8; i++) {
263 if (exact_match >= 0)
265 else if (first_match >= 0)
281 const char *buf,
size_t count)
291 ret = kstrtol(buf, 10, &val);
295 old_fan_mode = data->
conf1;
299 ret = get_fan_auto_nearest(data, nr, val, data->
conf1);
308 if (data->
conf1 & ADM1031_CONF1_AUTO_MODE) {
323 data->
pwm[0] | (data->
pwm[1] << 4));
333 show_fan_auto_channel, set_fan_auto_channel, 0);
335 show_fan_auto_channel, set_fan_auto_channel, 1);
356 const char *buf,
size_t count)
364 ret = kstrtol(buf, 10, &val);
385 const char *buf,
size_t count)
393 ret = kstrtol(buf, 10, &val);
406 #define auto_temp_reg(offset) \
407 static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
408 show_auto_temp_off, NULL, offset - 1); \
409 static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
410 show_auto_temp_min, set_auto_temp_min, offset - 1); \
411 static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
412 show_auto_temp_max, set_auto_temp_max, offset - 1)
427 const char *buf,
size_t count)
435 ret = kstrtol(buf, 10, &val);
440 if ((data->
conf1 & ADM1031_CONF1_AUTO_MODE) &&
441 (((val>>4) & 0xf) != 5)) {
449 nr ? ((data->
pwm[nr] << 4) & 0xf0) | (reg & 0xf)
450 : (data->
pwm[nr] & 0xf) | (reg & 0xf0));
458 show_pwm, set_pwm, 0);
460 show_pwm, set_pwm, 1);
469 static int trust_fan_readings(
struct adm1031_data *data,
int chan)
473 if (data->
conf1 & ADM1031_CONF1_AUTO_MODE) {
474 switch (data->
conf1 & 0x60) {
480 res = data->
temp[chan+1] >=
505 res = data->
pwm[chan] > 0;
520 return sprintf(buf,
"%d\n", value);
540 const char *buf,
size_t count)
548 ret = kstrtol(buf, 10, &val);
564 const char *buf,
size_t count)
575 ret = kstrtol(buf, 10, &val);
579 tmp = val == 8 ? 0xc0 :
589 data->
fan_div[
nr] = adm1031_read_value(client,
591 data->
fan_min[
nr] = adm1031_read_value(client,
598 data->
fan_min[
nr] = new_min > 0xff ? 0xff : new_min;
611 #define fan_offset(offset) \
612 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
613 show_fan, NULL, offset - 1); \
614 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
615 show_fan_min, set_fan_min, offset - 1); \
616 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
617 show_fan_div, set_fan_div, offset - 1)
632 (((data->
ext_temp[
nr] >> ((nr - 1) * 3)) & 7));
674 ret = kstrtol(buf, 10, &val);
678 val = SENSORS_LIMIT(val, -15000, 15000);
687 const char *buf,
size_t count)
695 ret = kstrtol(buf, 10, &val);
699 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
708 const char *buf,
size_t count)
716 ret = kstrtol(buf, 10, &val);
720 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
729 const char *buf,
size_t count)
737 ret = kstrtol(buf, 10, &val);
741 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
750 #define temp_reg(offset) \
751 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
752 show_temp, NULL, offset - 1); \
753 static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
754 show_temp_offset, set_temp_offset, offset - 1); \
755 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
756 show_temp_min, set_temp_min, offset - 1); \
757 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
758 show_temp_max, set_temp_max, offset - 1); \
759 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
760 show_temp_crit, set_temp_crit, offset - 1)
781 return sprintf(buf,
"%d\n", (data->
alarm >> bitnr) & 1);
801 static const unsigned int update_intervals[] = {
802 16000, 8000, 4000, 2000, 1000, 500, 250, 125,
816 const char *buf,
size_t count)
824 err = kstrtoul(buf, 10, &val);
832 for (i = 0; i <
ARRAY_SIZE(update_intervals) - 1; i++) {
833 if (val >= update_intervals[i])
852 set_update_interval);
854 static struct attribute *adm1031_attributes[] = {
855 &sensor_dev_attr_fan1_input.dev_attr.attr,
856 &sensor_dev_attr_fan1_div.dev_attr.attr,
857 &sensor_dev_attr_fan1_min.dev_attr.attr,
858 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
859 &sensor_dev_attr_fan1_fault.dev_attr.attr,
860 &sensor_dev_attr_pwm1.dev_attr.attr,
861 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
862 &sensor_dev_attr_temp1_input.dev_attr.attr,
863 &sensor_dev_attr_temp1_offset.dev_attr.attr,
864 &sensor_dev_attr_temp1_min.dev_attr.attr,
865 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
866 &sensor_dev_attr_temp1_max.dev_attr.attr,
867 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
868 &sensor_dev_attr_temp1_crit.dev_attr.attr,
869 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
870 &sensor_dev_attr_temp2_input.dev_attr.attr,
871 &sensor_dev_attr_temp2_offset.dev_attr.attr,
872 &sensor_dev_attr_temp2_min.dev_attr.attr,
873 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
874 &sensor_dev_attr_temp2_max.dev_attr.attr,
875 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
876 &sensor_dev_attr_temp2_crit.dev_attr.attr,
877 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
878 &sensor_dev_attr_temp2_fault.dev_attr.attr,
880 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
881 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
882 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
884 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
885 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
886 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
888 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
890 &dev_attr_update_interval.attr,
891 &dev_attr_alarms.attr,
897 .attrs = adm1031_attributes,
900 static struct attribute *adm1031_attributes_opt[] = {
901 &sensor_dev_attr_fan2_input.dev_attr.attr,
902 &sensor_dev_attr_fan2_div.dev_attr.attr,
903 &sensor_dev_attr_fan2_min.dev_attr.attr,
904 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
905 &sensor_dev_attr_fan2_fault.dev_attr.attr,
906 &sensor_dev_attr_pwm2.dev_attr.attr,
907 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
908 &sensor_dev_attr_temp3_input.dev_attr.attr,
909 &sensor_dev_attr_temp3_offset.dev_attr.attr,
910 &sensor_dev_attr_temp3_min.dev_attr.attr,
911 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
912 &sensor_dev_attr_temp3_max.dev_attr.attr,
913 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
914 &sensor_dev_attr_temp3_crit.dev_attr.attr,
915 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
916 &sensor_dev_attr_temp3_fault.dev_attr.attr,
917 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
918 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
919 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
920 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
925 .attrs = adm1031_attributes_opt,
929 static int adm1031_detect(
struct i2c_client *client,
942 if (!((
id == 0x31 ||
id == 0x30) && co == 0x41))
944 name = (
id == 0x30) ?
"adm1030" :
"adm1031";
951 static int adm1031_probe(
struct i2c_client *client,
962 i2c_set_clientdata(client, data);
972 adm1031_init_client(client);
999 static int adm1031_remove(
struct i2c_client *client)
1001 struct adm1031_data *data = i2c_get_clientdata(client);
1009 static void adm1031_init_client(
struct i2c_client *client)
1011 unsigned int read_val;
1014 struct adm1031_data *data = i2c_get_clientdata(client);
1023 if ((read_val | mask) != read_val)
1043 struct adm1031_data *data = i2c_get_clientdata(client);
1044 unsigned long next_update;
1053 dev_dbg(&client->
dev,
"Starting adm1031 update\n");
1066 adm1031_read_value(client,
1070 adm1031_read_value(client,
1076 "Remote temperature may be wrong.\n");
1079 data->
temp[chan] = newh;
1082 adm1031_read_value(client,
1085 adm1031_read_value(client,
1088 adm1031_read_value(client,
1091 adm1031_read_value(client,
1094 adm1031_read_value(client,
1105 data->
alarm &= 0xc0ff;
1110 adm1031_read_value(client,
1113 adm1031_read_value(client,
1116 adm1031_read_value(client,
1119 (adm1031_read_value(client,