22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/module.h>
26 #include <linux/i2c.h>
34 #include <linux/slab.h>
37 static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F,
I2C_CLIENT_END };
40 #define ADT7470_REG_BASE_ADDR 0x20
41 #define ADT7470_REG_TEMP_BASE_ADDR 0x20
42 #define ADT7470_REG_TEMP_MAX_ADDR 0x29
43 #define ADT7470_REG_FAN_BASE_ADDR 0x2A
44 #define ADT7470_REG_FAN_MAX_ADDR 0x31
45 #define ADT7470_REG_PWM_BASE_ADDR 0x32
46 #define ADT7470_REG_PWM_MAX_ADDR 0x35
47 #define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38
48 #define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B
49 #define ADT7470_REG_CFG 0x40
50 #define ADT7470_FSPD_MASK 0x04
51 #define ADT7470_REG_ALARM1 0x41
52 #define ADT7470_R1T_ALARM 0x01
53 #define ADT7470_R2T_ALARM 0x02
54 #define ADT7470_R3T_ALARM 0x04
55 #define ADT7470_R4T_ALARM 0x08
56 #define ADT7470_R5T_ALARM 0x10
57 #define ADT7470_R6T_ALARM 0x20
58 #define ADT7470_R7T_ALARM 0x40
59 #define ADT7470_OOL_ALARM 0x80
60 #define ADT7470_REG_ALARM2 0x42
61 #define ADT7470_R8T_ALARM 0x01
62 #define ADT7470_R9T_ALARM 0x02
63 #define ADT7470_R10T_ALARM 0x04
64 #define ADT7470_FAN1_ALARM 0x10
65 #define ADT7470_FAN2_ALARM 0x20
66 #define ADT7470_FAN3_ALARM 0x40
67 #define ADT7470_FAN4_ALARM 0x80
68 #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44
69 #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57
70 #define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58
71 #define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F
72 #define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60
73 #define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67
74 #define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68
75 #define ADT7470_REG_PWM12_CFG 0x68
76 #define ADT7470_PWM2_AUTO_MASK 0x40
77 #define ADT7470_PWM1_AUTO_MASK 0x80
78 #define ADT7470_PWM_AUTO_MASK 0xC0
79 #define ADT7470_REG_PWM34_CFG 0x69
80 #define ADT7470_PWM3_AUTO_MASK 0x40
81 #define ADT7470_PWM4_AUTO_MASK 0x80
82 #define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
83 #define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
84 #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
85 #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
86 #define ADT7470_REG_ACOUSTICS12 0x75
87 #define ADT7470_REG_ACOUSTICS34 0x76
88 #define ADT7470_REG_DEVICE 0x3D
89 #define ADT7470_REG_VENDOR 0x3E
90 #define ADT7470_REG_REVISION 0x3F
91 #define ADT7470_REG_ALARM1_MASK 0x72
92 #define ADT7470_REG_ALARM2_MASK 0x73
93 #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
94 #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
95 #define ADT7470_REG_MAX_ADDR 0x81
97 #define ADT7470_TEMP_COUNT 10
98 #define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
99 #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
100 #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
103 #define ADT7470_FAN_COUNT 4
104 #define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
105 #define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
106 #define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
108 #define ADT7470_PWM_COUNT 4
109 #define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
110 #define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
111 #define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
112 #define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
113 #define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
114 #define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
117 #define ALARM2(x) ((x) << 8)
119 #define ADT7470_VENDOR 0x41
120 #define ADT7470_DEVICE 0x70
122 #define ADT7470_REVISION 0x02
125 #define ADT7470_PWM_ALL_TEMPS 0x3FF
128 #define SENSOR_REFRESH_INTERVAL (5 * HZ)
131 #define LIMIT_REFRESH_INTERVAL (60 * HZ)
134 #define TEMP_COLLECTION_TIME 2000
137 #define AUTO_UPDATE_INTERVAL 2000
140 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
141 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
142 #define FAN_PERIOD_INVALID 65535
143 #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
195 .probe = adt7470_probe,
196 .remove = adt7470_remove,
197 .id_table = adt7470_id,
198 .detect = adt7470_detect,
199 .address_list = normal_i2c,
214 static inline int adt7470_write_word_data(
struct i2c_client *client,
u8 reg,
221 static void adt7470_init_client(
struct i2c_client *client)
226 dev_err(&client->
dev,
"cannot read configuration register\n");
234 static int adt7470_read_temperatures(
struct i2c_client *client,
279 pr_err(
"ha ha, interrupted\n");
297 static int adt7470_update_thread(
void *
p)
304 adt7470_read_temperatures(client, data);
319 unsigned long local_jiffies =
jiffies;
322 int need_sensors = 1;
340 if (!need_sensors && !need_limits)
345 goto no_sensor_update;
348 adt7470_read_temperatures(client, data);
355 data->
fan[i] = adt7470_read_word_data(client,
412 data->
fan_min[
i] = adt7470_read_word_data(client,
414 data->
fan_max[
i] = adt7470_read_word_data(client,
452 if (kstrtol(buf, 10, &temp))
455 temp = SENSORS_LIMIT(temp, 0, 60000);
481 if (kstrtol(buf, 10, &temp))
484 temp = SENSORS_LIMIT(temp, -1, 10);
514 if (kstrtol(buf, 10, &temp))
518 temp = SENSORS_LIMIT(temp, 0, 255);
548 if (kstrtol(buf, 10, &temp))
552 temp = SENSORS_LIMIT(temp, 0, 255);
596 const char *buf,
size_t count)
603 if (kstrtol(buf, 10, &temp) || !temp)
607 temp = SENSORS_LIMIT(temp, 1, 65534);
633 const char *buf,
size_t count)
640 if (kstrtol(buf, 10, &temp) || !temp)
644 temp = SENSORS_LIMIT(temp, 1, 65534);
685 if (kstrtol(buf, 10, &temp))
694 reg &= ~ADT7470_FSPD_MASK;
710 const char *buf,
size_t count)
717 if (kstrtol(buf, 10, &temp))
720 temp = SENSORS_LIMIT(temp, 0, 255);
749 if (kstrtol(buf, 10, &temp))
752 temp = SENSORS_LIMIT(temp, 0, 255);
782 if (kstrtol(buf, 10, &temp))
785 temp = SENSORS_LIMIT(temp, 0, 255);
825 if (kstrtol(buf, 10, &temp))
829 temp = SENSORS_LIMIT(temp, 0, 255);
858 int pwm_auto_reg_mask;
862 if (kstrtol(buf, 10, &temp))
870 if (temp != 2 && temp != 1)
878 reg |= pwm_auto_reg_mask;
880 reg &= ~pwm_auto_reg_mask;
896 return sprintf(buf,
"%d\n", 1 << (ctrl - 1));
901 static int cvt_auto_temp(
int input)
907 return ilog2(input) + 1;
922 if (kstrtol(buf, 10, &temp))
925 temp = cvt_auto_temp(temp);
933 if (!(attr->
index % 2)) {
935 reg |= (temp << 4) & 0xF0;
962 set_num_temp_sensors);
964 show_auto_update_interval, set_auto_update_interval);
1073 show_force_pwm_max, set_force_pwm_max, 0);
1081 show_pwm_min, set_pwm_min, 0);
1083 show_pwm_min, set_pwm_min, 1);
1085 show_pwm_min, set_pwm_min, 2);
1087 show_pwm_min, set_pwm_min, 3);
1090 show_pwm_max, set_pwm_max, 0);
1092 show_pwm_max, set_pwm_max, 1);
1094 show_pwm_max, set_pwm_max, 2);
1096 show_pwm_max, set_pwm_max, 3);
1099 show_pwm_tmin, set_pwm_tmin, 0);
1101 show_pwm_tmin, set_pwm_tmin, 1);
1103 show_pwm_tmin, set_pwm_tmin, 2);
1105 show_pwm_tmin, set_pwm_tmin, 3);
1126 show_pwm_auto_temp, set_pwm_auto_temp, 0);
1128 show_pwm_auto_temp, set_pwm_auto_temp, 1);
1130 show_pwm_auto_temp, set_pwm_auto_temp, 2);
1132 show_pwm_auto_temp, set_pwm_auto_temp, 3);
1134 static struct attribute *adt7470_attr[] = {
1135 &dev_attr_alarm_mask.attr,
1136 &dev_attr_num_temp_sensors.attr,
1137 &dev_attr_auto_update_interval.attr,
1138 &sensor_dev_attr_temp1_max.dev_attr.attr,
1139 &sensor_dev_attr_temp2_max.dev_attr.attr,
1140 &sensor_dev_attr_temp3_max.dev_attr.attr,
1141 &sensor_dev_attr_temp4_max.dev_attr.attr,
1142 &sensor_dev_attr_temp5_max.dev_attr.attr,
1143 &sensor_dev_attr_temp6_max.dev_attr.attr,
1144 &sensor_dev_attr_temp7_max.dev_attr.attr,
1145 &sensor_dev_attr_temp8_max.dev_attr.attr,
1146 &sensor_dev_attr_temp9_max.dev_attr.attr,
1147 &sensor_dev_attr_temp10_max.dev_attr.attr,
1148 &sensor_dev_attr_temp1_min.dev_attr.attr,
1149 &sensor_dev_attr_temp2_min.dev_attr.attr,
1150 &sensor_dev_attr_temp3_min.dev_attr.attr,
1151 &sensor_dev_attr_temp4_min.dev_attr.attr,
1152 &sensor_dev_attr_temp5_min.dev_attr.attr,
1153 &sensor_dev_attr_temp6_min.dev_attr.attr,
1154 &sensor_dev_attr_temp7_min.dev_attr.attr,
1155 &sensor_dev_attr_temp8_min.dev_attr.attr,
1156 &sensor_dev_attr_temp9_min.dev_attr.attr,
1157 &sensor_dev_attr_temp10_min.dev_attr.attr,
1158 &sensor_dev_attr_temp1_input.dev_attr.attr,
1159 &sensor_dev_attr_temp2_input.dev_attr.attr,
1160 &sensor_dev_attr_temp3_input.dev_attr.attr,
1161 &sensor_dev_attr_temp4_input.dev_attr.attr,
1162 &sensor_dev_attr_temp5_input.dev_attr.attr,
1163 &sensor_dev_attr_temp6_input.dev_attr.attr,
1164 &sensor_dev_attr_temp7_input.dev_attr.attr,
1165 &sensor_dev_attr_temp8_input.dev_attr.attr,
1166 &sensor_dev_attr_temp9_input.dev_attr.attr,
1167 &sensor_dev_attr_temp10_input.dev_attr.attr,
1168 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1169 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1170 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1171 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
1172 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
1173 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
1174 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
1175 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
1176 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
1177 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
1178 &sensor_dev_attr_fan1_max.dev_attr.attr,
1179 &sensor_dev_attr_fan2_max.dev_attr.attr,
1180 &sensor_dev_attr_fan3_max.dev_attr.attr,
1181 &sensor_dev_attr_fan4_max.dev_attr.attr,
1182 &sensor_dev_attr_fan1_min.dev_attr.attr,
1183 &sensor_dev_attr_fan2_min.dev_attr.attr,
1184 &sensor_dev_attr_fan3_min.dev_attr.attr,
1185 &sensor_dev_attr_fan4_min.dev_attr.attr,
1186 &sensor_dev_attr_fan1_input.dev_attr.attr,
1187 &sensor_dev_attr_fan2_input.dev_attr.attr,
1188 &sensor_dev_attr_fan3_input.dev_attr.attr,
1189 &sensor_dev_attr_fan4_input.dev_attr.attr,
1190 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1191 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1192 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1193 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1194 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
1195 &sensor_dev_attr_pwm1.dev_attr.attr,
1196 &sensor_dev_attr_pwm2.dev_attr.attr,
1197 &sensor_dev_attr_pwm3.dev_attr.attr,
1198 &sensor_dev_attr_pwm4.dev_attr.attr,
1199 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1200 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1201 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1202 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1203 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1204 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1205 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1206 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1207 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1208 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1209 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1210 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
1211 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1212 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1213 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1214 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1215 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1216 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1217 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1218 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1219 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1220 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1221 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1222 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1227 static int adt7470_detect(
struct i2c_client *client,
1253 static int adt7470_probe(
struct i2c_client *client,
1267 i2c_set_clientdata(client, data);
1273 adt7470_init_client(client);
1276 data->
attrs.attrs = adt7470_attr;
1292 goto exit_unregister;
1304 static int adt7470_remove(
struct i2c_client *client)
1306 struct adt7470_data *data = i2c_get_clientdata(client);