15 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/i2c.h>
46 #define REG_DEVREV2 0x12
49 #define REG_EXTEND3 0x1F
51 #define REG_VOLTAGE_BASE 0x20
52 #define REG_TEMP_BASE 0x25
53 #define REG_TACH_BASE 0x28
54 #define REG_PWM_BASE 0x30
55 #define REG_PWM_MAX_BASE 0x38
57 #define REG_DEVID 0x3D
58 #define REG_VENDID 0x3E
59 #define REG_DEVID2 0x3F
61 #define REG_STATUS1 0x41
62 #define REG_STATUS2 0x42
66 #define REG_VOLTAGE_MIN_BASE 0x44
67 #define REG_VOLTAGE_MAX_BASE 0x45
69 #define REG_TEMP_MIN_BASE 0x4E
70 #define REG_TEMP_MAX_BASE 0x4F
72 #define REG_TACH_MIN_BASE 0x54
74 #define REG_PWM_CONFIG_BASE 0x5C
76 #define REG_TEMP_TRANGE_BASE 0x5F
78 #define REG_PWM_MIN_BASE 0x64
80 #define REG_TEMP_TMIN_BASE 0x67
81 #define REG_TEMP_THERM_BASE 0x6A
83 #define REG_REMOTE1_HYSTERSIS 0x6D
84 #define REG_REMOTE2_HYSTERSIS 0x6E
86 #define REG_TEMP_OFFSET_BASE 0x70
88 #define REG_CONFIG2 0x73
90 #define REG_EXTEND1 0x76
91 #define REG_EXTEND2 0x77
93 #define REG_CONFIG3 0x78
94 #define REG_CONFIG5 0x7C
95 #define REG_CONFIG4 0x7D
97 #define REG_STATUS4 0x81
99 #define REG_VTT_MIN 0x84
100 #define REG_VTT_MAX 0x86
102 #define VID_VIDSEL 0x80
104 #define CONFIG2_ATTN 0x20
106 #define CONFIG3_SMBALERT 0x01
107 #define CONFIG3_THERM 0x02
109 #define CONFIG4_PINFUNC 0x03
110 #define CONFIG4_MAXDUTY 0x08
111 #define CONFIG4_ATTN_IN10 0x30
112 #define CONFIG4_ATTN_IN43 0xC0
114 #define CONFIG5_TWOSCOMP 0x01
115 #define CONFIG5_TEMPOFFSET 0x02
116 #define CONFIG5_VIDGPIO 0x10
120 #define ADT7475_VOLTAGE_COUNT 5
121 #define ADT7475_TEMP_COUNT 3
122 #define ADT7475_TACH_COUNT 4
123 #define ADT7475_PWM_COUNT 3
127 #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
131 #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
132 #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
134 #define PWM_REG(idx) (REG_PWM_BASE + (idx))
135 #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
136 #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
137 #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
139 #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
140 #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
141 #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
143 #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
144 #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
145 #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
146 #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
147 #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
148 #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
149 #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
151 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e,
I2C_CLIENT_END };
204 val = SENSORS_LIMIT(val, -64000, 191000);
205 ret = (val + 64500) / 1000;
207 val = SENSORS_LIMIT(val, -128000, 127000);
209 ret = (256500 +
val) / 1000;
211 ret = (val + 500) / 1000;
223 return (reg - 1024) * 250;
227 return (reg - 256) * 250;
230 static inline int tach2rpm(
u16 tach)
232 if (tach == 0 || tach == 0xFFFF)
235 return (90000 * 60) /
tach;
238 static inline u16 rpm2tach(
unsigned long rpm)
243 return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF);
258 const int *
r = adt7473_in_scaling[
channel];
260 if (bypass_attn & (1 << channel))
265 static inline u16 volt2reg(
int channel,
long volt,
u8 bypass_attn)
267 const int *r = adt7473_in_scaling[
channel];
270 if (bypass_attn & (1 << channel))
271 reg = (volt * 1024) / 2250;
273 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
274 return SENSORS_LIMIT(reg, 0, 1023) & (0xff << 2);
297 static int find_nearest(
long val,
const int *array,
int size)
304 if (val > array[size - 1])
307 for (i = 0; i < size - 1; i++) {
310 if (val > array[i + 1])
314 b = array[i + 1] -
val;
316 return (a <= b) ? i : i + 1;
341 const char *buf,
size_t count)
350 if (kstrtol(buf, 10, &val))
359 if (sattr->
nr ==
MIN)
364 if (sattr->
nr ==
MIN)
388 if (sattr->
index != 1)
389 out = (out >> 4) & 0xF;
421 out = !!(data->
alarms & (sattr->
index ? 0x8000 : 0x4000));
426 out = reg2temp(data, data->
temp[sattr->
nr][sattr->
index]);
429 return sprintf(buf,
"%d\n", out);
433 const char *buf,
size_t count)
438 unsigned char reg = 0;
443 if (kstrtol(buf, 10, &val))
454 val = SENSORS_LIMIT(val, -63000, 127000);
457 val = SENSORS_LIMIT(val, -63000, 64000);
471 adt7475_read_hystersis(client);
474 val = SENSORS_LIMIT(val, temp - 15000, temp);
475 val = (temp -
val) / 1000;
477 if (sattr->
index != 1) {
489 data->
temp[sattr->
nr][sattr->
index] = temp2reg(data, val);
515 if (sattr->
index != 2)
533 static const int autorange_table[] = {
534 2000, 2500, 3330, 4000, 5000, 6670, 8000,
535 10000, 13330, 16000, 20000, 26670, 32000, 40000,
547 out = (data->
range[sattr->
index] >> 4) & 0x0F;
551 return sprintf(buf,
"%d\n", val + autorange_table[out]);
555 const char *buf,
size_t count)
563 if (kstrtol(buf, 10, &val))
580 val = SENSORS_LIMIT(val, temp + autorange_table[0],
581 temp + autorange_table[
ARRAY_SIZE(autorange_table) - 1]);
585 val = find_nearest(val, autorange_table,
ARRAY_SIZE(autorange_table));
607 out = tach2rpm(data->
tach[sattr->
nr][sattr->
index]);
609 return sprintf(buf,
"%d\n", out);
613 const char *buf,
size_t count)
621 if (kstrtoul(buf, 10, &val))
663 const char *buf,
size_t count)
669 unsigned char reg = 0;
672 if (kstrtol(buf, 10, &val))
704 data->
pwm[sattr->
nr][sattr->
index] = SENSORS_LIMIT(val, 0, 0xFF);
771 const char *buf,
size_t count)
779 if (kstrtol(buf, 10, &val))
784 adt7475_read_pwm(client, sattr->
index);
794 const char *buf,
size_t count)
802 if (kstrtol(buf, 10, &val))
807 adt7475_read_pwm(client, sattr->
index);
817 static const int pwmfreq_table[] = {
818 11, 14, 22, 29, 35, 44, 58, 88
828 pwmfreq_table[data->
range[sattr->
index] & 7]);
832 const char *buf,
size_t count)
840 if (kstrtol(buf, 10, &val))
843 out = find_nearest(val, pwmfreq_table,
ARRAY_SIZE(pwmfreq_table));
868 const char *buf,
size_t count)
874 if (kstrtol(buf, 10, &val))
876 if (val != 0 && val != 1)
899 const char *buf,
size_t count)
904 if (kstrtol(buf, 10, &val))
906 if (val < 0 || val > 255)
968 show_point2, set_point2, 0, 0);
984 show_point2, set_point2, 0, 1);
1001 show_point2, set_point2, 0, 2);
1025 set_pwmfreq,
INPUT, 0);
1027 set_pwmctrl,
INPUT, 0);
1029 show_pwmchan, set_pwmchan,
INPUT, 0);
1037 set_pwmfreq,
INPUT, 1);
1039 set_pwmctrl,
INPUT, 1);
1041 show_pwmchan, set_pwmchan,
INPUT, 1);
1049 set_pwmfreq,
INPUT, 2);
1051 set_pwmctrl,
INPUT, 2);
1053 show_pwmchan, set_pwmchan,
INPUT, 2);
1061 show_pwm_at_crit, set_pwm_at_crit);
1066 static struct attribute *adt7475_attrs[] = {
1067 &sensor_dev_attr_in1_input.dev_attr.attr,
1068 &sensor_dev_attr_in1_max.dev_attr.attr,
1069 &sensor_dev_attr_in1_min.dev_attr.attr,
1070 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1071 &sensor_dev_attr_in2_input.dev_attr.attr,
1072 &sensor_dev_attr_in2_max.dev_attr.attr,
1073 &sensor_dev_attr_in2_min.dev_attr.attr,
1074 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1075 &sensor_dev_attr_temp1_input.dev_attr.attr,
1076 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1077 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1078 &sensor_dev_attr_temp1_max.dev_attr.attr,
1079 &sensor_dev_attr_temp1_min.dev_attr.attr,
1080 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1081 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1082 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1083 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1084 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1085 &sensor_dev_attr_temp2_input.dev_attr.attr,
1086 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1087 &sensor_dev_attr_temp2_max.dev_attr.attr,
1088 &sensor_dev_attr_temp2_min.dev_attr.attr,
1089 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1090 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1091 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1092 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1093 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1094 &sensor_dev_attr_temp3_input.dev_attr.attr,
1095 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1096 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1097 &sensor_dev_attr_temp3_max.dev_attr.attr,
1098 &sensor_dev_attr_temp3_min.dev_attr.attr,
1099 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1100 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1101 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1102 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1103 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1104 &sensor_dev_attr_fan1_input.dev_attr.attr,
1105 &sensor_dev_attr_fan1_min.dev_attr.attr,
1106 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1107 &sensor_dev_attr_fan2_input.dev_attr.attr,
1108 &sensor_dev_attr_fan2_min.dev_attr.attr,
1109 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1110 &sensor_dev_attr_fan3_input.dev_attr.attr,
1111 &sensor_dev_attr_fan3_min.dev_attr.attr,
1112 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1113 &sensor_dev_attr_pwm1.dev_attr.attr,
1114 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1115 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1116 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1117 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1118 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1119 &sensor_dev_attr_pwm3.dev_attr.attr,
1120 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1121 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1122 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1123 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1124 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1125 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
1129 static struct attribute *fan4_attrs[] = {
1130 &sensor_dev_attr_fan4_input.dev_attr.attr,
1131 &sensor_dev_attr_fan4_min.dev_attr.attr,
1132 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1136 static struct attribute *pwm2_attrs[] = {
1137 &sensor_dev_attr_pwm2.dev_attr.attr,
1138 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1139 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1140 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1141 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1142 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1146 static struct attribute *in0_attrs[] = {
1147 &sensor_dev_attr_in0_input.dev_attr.attr,
1148 &sensor_dev_attr_in0_max.dev_attr.attr,
1149 &sensor_dev_attr_in0_min.dev_attr.attr,
1150 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1154 static struct attribute *in3_attrs[] = {
1155 &sensor_dev_attr_in3_input.dev_attr.attr,
1156 &sensor_dev_attr_in3_max.dev_attr.attr,
1157 &sensor_dev_attr_in3_min.dev_attr.attr,
1158 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1162 static struct attribute *in4_attrs[] = {
1163 &sensor_dev_attr_in4_input.dev_attr.attr,
1164 &sensor_dev_attr_in4_max.dev_attr.attr,
1165 &sensor_dev_attr_in4_min.dev_attr.attr,
1166 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1170 static struct attribute *in5_attrs[] = {
1171 &sensor_dev_attr_in5_input.dev_attr.attr,
1172 &sensor_dev_attr_in5_max.dev_attr.attr,
1173 &sensor_dev_attr_in5_min.dev_attr.attr,
1174 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1178 static struct attribute *vid_attrs[] = {
1179 &dev_attr_cpu0_vid.attr,
1184 static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1185 static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1186 static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1187 static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1188 static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1189 static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1190 static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1191 static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
1193 static int adt7475_detect(
struct i2c_client *client,
1205 if (vendid != 0x41 ||
1206 (devid2 & 0xf8) != 0x68)
1212 else if (devid == 0x75 && client->
addr == 0x2e)
1214 else if (devid == 0x76)
1216 else if ((devid2 & 0xfc) == 0x6c)
1220 "Couldn't detect an ADT7473/75/76/90 part at "
1221 "0x%02x\n", (
unsigned int)client->
addr);
1230 static void adt7475_remove_files(
struct i2c_client *client,
1250 static int adt7475_probe(
struct i2c_client *client,
1253 static const char *
const names[] = {
1262 u8 config2, config3;
1269 i2c_set_clientdata(client, data);
1272 switch (id->driver_data) {
1306 if (id->driver_data ==
adt7490) {
1321 if (id->driver_data ==
adt7476) {
1344 adt7475_read_pwm(client, i);
1394 dev_info(&client->
dev,
"%s device, revision %d\n",
1397 dev_info(&client->
dev,
"Optional features:%s%s%s%s%s\n",
1404 dev_info(&client->
dev,
"Bypassing attenuators on:%s%s%s%s\n",
1413 adt7475_remove_files(client, data);
1417 static int adt7475_remove(
struct i2c_client *client)
1419 struct adt7475_data *data = i2c_get_clientdata(client);
1422 adt7475_remove_files(client, data);
1432 .probe = adt7475_probe,
1433 .remove = adt7475_remove,
1434 .id_table = adt7475_id,
1435 .detect = adt7475_detect,
1436 .address_list = normal_i2c,
1439 static void adt7475_read_hystersis(
struct i2c_client *client)
1441 struct adt7475_data *data = i2c_get_clientdata(client);
1448 static void adt7475_read_pwm(
struct i2c_client *client,
int index)
1450 struct adt7475_data *data = i2c_get_clientdata(client);
1508 struct adt7475_data *data = i2c_get_clientdata(client);
1527 ((ext >> (i * 2)) & 3);
1533 ((ext >> ((i + 5) * 2)) & 3);
1546 adt7475_read_word(client,
TACH_REG(i));
1596 adt7475_read_hystersis(client);
1611 adt7475_read_pwm(client, i);