31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/module.h>
35 #include <linux/slab.h>
47 static unsigned short force_id;
53 #define DRVNAME "f71805f"
60 #define F71805F_LD_HWM 0x04
62 #define SIO_REG_LDSEL 0x07
63 #define SIO_REG_DEVID 0x20
64 #define SIO_REG_DEVREV 0x22
65 #define SIO_REG_MANID 0x23
66 #define SIO_REG_FNSEL1 0x29
67 #define SIO_REG_ENABLE 0x30
68 #define SIO_REG_ADDR 0x60
70 #define SIO_FINTEK_ID 0x1934
71 #define SIO_F71805F_ID 0x0406
72 #define SIO_F71872F_ID 0x0341
75 superio_inb(
int base,
int reg)
82 superio_inw(
int base,
int reg)
86 val =
inb(base + 1) << 8;
100 superio_enter(
int base)
107 superio_exit(
int base)
116 #define REGION_LENGTH 8
117 #define ADDR_REG_OFFSET 5
118 #define DATA_REG_OFFSET 6
125 #define F71805F_REG_IN(nr) (0x10 + (nr))
126 #define F71805F_REG_IN_HIGH(nr) ((nr) < 10 ? 0x40 + 2 * (nr) : 0x2E)
127 #define F71805F_REG_IN_LOW(nr) ((nr) < 10 ? 0x41 + 2 * (nr) : 0x2F)
129 #define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
130 #define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
131 #define F71805F_REG_FAN_TARGET(nr) (0x69 + 16 * (nr))
132 #define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
133 #define F71805F_REG_PWM_FREQ(nr) (0x63 + 16 * (nr))
134 #define F71805F_REG_PWM_DUTY(nr) (0x6B + 16 * (nr))
136 #define F71805F_REG_TEMP(nr) (0x1B + (nr))
137 #define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
138 #define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
139 #define F71805F_REG_TEMP_MODE 0x01
142 #define F71805F_REG_PWM_AUTO_POINT_TEMP(pwmnr, apnr) \
143 (0xA0 + 0x10 * (pwmnr) + (2 - (apnr)))
144 #define F71805F_REG_PWM_AUTO_POINT_FAN(pwmnr, apnr) \
145 (0xA4 + 0x10 * (pwmnr) + \
148 #define F71805F_REG_START 0x00
150 #define F71805F_REG_STATUS(nr) (0x36 + (nr))
153 #define FAN_CTRL_DC_MODE 0x10
154 #define FAN_CTRL_LATCH_FULL 0x08
155 #define FAN_CTRL_MODE_MASK 0x03
156 #define FAN_CTRL_MODE_SPEED 0x00
157 #define FAN_CTRL_MODE_TEMPERATURE 0x01
158 #define FAN_CTRL_MODE_MANUAL 0x02
203 static inline long in_from_reg(
u8 reg)
209 static inline u8 in_to_reg(
long val)
215 return ((val + 16) / 32) << 2;
219 static inline long in0_from_reg(
u8 reg)
224 static inline u8 in0_to_reg(
long val)
230 return ((val + 32) / 64) << 2;
234 static inline long fan_from_reg(
u16 reg)
237 if (!reg || reg == 0xfff)
239 return 1500000 /
reg;
242 static inline u16 fan_to_reg(
long rpm)
251 return 1500000 / rpm;
254 static inline unsigned long pwm_freq_from_reg(
u8 reg)
256 unsigned long clock = (reg & 0x80) ? 48000000
UL : 1000000
UL;
261 return clock / (reg << 8);
264 static inline u8 pwm_freq_to_reg(
unsigned long val)
269 return 0x80 | (48000000
UL / (val << 8));
273 return 1000000
UL / (val << 8);
276 static inline int pwm_mode_from_reg(
u8 reg)
281 static inline long temp_from_reg(
u8 reg)
286 static inline u8 temp_to_reg(
long val)
290 if (val >= 1000 * 0xff)
292 return (val + 500) / 1000;
349 for (nr = 0; nr < 11; nr++) {
350 if (!(data->
has_in & (1 << nr)))
354 data->
in_low[
nr] = f71805f_read8(data,
357 for (nr = 0; nr < 3; nr++) {
365 for (nr = 0; nr < 3; nr++) {
372 for (nr = 0; nr < 3; nr++) {
373 for (apnr = 0; apnr < 3; apnr++) {
391 for (nr = 0; nr < 11; nr++) {
392 if (!(data->
has_in & (1 << nr)))
394 data->
in[
nr] = f71805f_read8(data,
397 for (nr = 0; nr < 3; nr++) {
398 data->
fan[
nr] = f71805f_read16(data,
402 data->
pwm[
nr] = f71805f_read8(data,
405 for (nr = 0; nr < 3; nr++) {
406 data->
temp[
nr] = f71805f_read8(data,
431 int nr = attr->
index;
433 return sprintf(buf,
"%ld\n", in0_from_reg(data->
in[nr]));
441 int nr = attr->
index;
451 int nr = attr->
index;
453 return sprintf(buf,
"%ld\n", in0_from_reg(data->
in_low[nr]));
457 *devattr,
const char *buf,
size_t count)
461 int nr = attr->
index;
465 err = kstrtol(buf, 10, &val);
478 *devattr,
const char *buf,
size_t count)
482 int nr = attr->
index;
486 err = kstrtol(buf, 10, &val);
503 int nr = attr->
index;
505 return sprintf(buf,
"%ld\n", in_from_reg(data->
in[nr]));
513 int nr = attr->
index;
523 int nr = attr->
index;
529 *devattr,
const char *buf,
size_t count)
533 int nr = attr->
index;
537 err = kstrtol(buf, 10, &val);
550 *devattr,
const char *buf,
size_t count)
554 int nr = attr->
index;
558 err = kstrtol(buf, 10, &val);
575 int nr = attr->
index;
577 return sprintf(buf,
"%ld\n", fan_from_reg(data->
fan[nr]));
585 int nr = attr->
index;
595 int nr = attr->
index;
601 *devattr,
const char *buf,
size_t count)
605 int nr = attr->
index;
609 err = kstrtol(buf, 10, &val);
622 *devattr,
const char *buf,
size_t count)
626 int nr = attr->
index;
630 err = kstrtol(buf, 10, &val);
648 int nr = attr->
index;
650 return sprintf(buf,
"%d\n", (
int)data->
pwm[nr]);
658 int nr = attr->
index;
672 return sprintf(buf,
"%d\n", mode);
680 int nr = attr->
index;
690 int nr = attr->
index;
696 const char *buf,
size_t count)
700 int nr = attr->
index;
704 err = kstrtoul(buf, 10, &val);
719 static struct attribute *f71805f_attr_pwm[];
722 *devattr,
const char *buf,
size_t count)
726 int nr = attr->
index;
731 err = kstrtoul(buf, 10, &val);
735 if (val < 1 || val > 3)
741 dev_dbg(dev,
"chmod -w pwm%d failed\n", nr + 1);
765 dev_dbg(dev,
"chmod +w pwm%d failed\n", nr + 1);
772 *devattr,
const char *buf,
size_t count)
776 int nr = attr->
index;
780 err = kstrtoul(buf, 10, &val);
798 int pwmnr = attr->
nr;
799 int apnr = attr->
index;
802 temp_from_reg(data->
auto_points[pwmnr].temp[apnr]));
807 const char *buf,
size_t count)
811 int pwmnr = attr->
nr;
812 int apnr = attr->
index;
816 err = kstrtoul(buf, 10, &val);
821 data->
auto_points[pwmnr].temp[apnr] = temp_to_reg(val);
835 int pwmnr = attr->
nr;
836 int apnr = attr->
index;
844 const char *buf,
size_t count)
848 int pwmnr = attr->
nr;
849 int apnr = attr->
index;
853 err = kstrtoul(buf, 10, &val);
858 data->
auto_points[pwmnr].fan[apnr] = fan_to_reg(val);
871 int nr = attr->
index;
873 return sprintf(buf,
"%ld\n", temp_from_reg(data->
temp[nr]));
881 int nr = attr->
index;
891 int nr = attr->
index;
901 int nr = attr->
index;
908 *devattr,
const char *buf,
size_t count)
912 int nr = attr->
index;
916 err = kstrtol(buf, 10, &val);
929 *devattr,
const char *buf,
size_t count)
933 int nr = attr->
index;
937 err = kstrtol(buf, 10, &val);
978 int bitnr = attr->
index;
993 show_in0_max, set_in0_max, 0);
995 show_in0_min, set_in0_min, 0);
998 show_in_max, set_in_max, 1);
1000 show_in_min, set_in_min, 1);
1003 show_in_max, set_in_max, 2);
1005 show_in_min, set_in_min, 2);
1008 show_in_max, set_in_max, 3);
1010 show_in_min, set_in_min, 3);
1013 show_in_max, set_in_max, 4);
1015 show_in_min, set_in_min, 4);
1018 show_in_max, set_in_max, 5);
1020 show_in_min, set_in_min, 5);
1023 show_in_max, set_in_max, 6);
1025 show_in_min, set_in_min, 6);
1028 show_in_max, set_in_max, 7);
1030 show_in_min, set_in_min, 7);
1033 show_in_max, set_in_max, 8);
1035 show_in_min, set_in_min, 8);
1038 show_in0_max, set_in0_max, 9);
1040 show_in0_min, set_in0_min, 9);
1043 show_in0_max, set_in0_max, 10);
1045 show_in0_min, set_in0_min, 10);
1049 show_fan_min, set_fan_min, 0);
1051 show_fan_target, set_fan_target, 0);
1054 show_fan_min, set_fan_min, 1);
1056 show_fan_target, set_fan_target, 1);
1059 show_fan_min, set_fan_min, 2);
1061 show_fan_target, set_fan_target, 2);
1065 show_temp_max, set_temp_max, 0);
1067 show_temp_hyst, set_temp_hyst, 0);
1071 show_temp_max, set_temp_max, 1);
1073 show_temp_hyst, set_temp_hyst, 1);
1077 show_temp_max, set_temp_max, 2);
1079 show_temp_hyst, set_temp_hyst, 2);
1088 show_pwm_enable, set_pwm_enable, 0);
1090 show_pwm_freq, set_pwm_freq, 0);
1094 show_pwm_enable, set_pwm_enable, 1);
1096 show_pwm_freq, set_pwm_freq, 1);
1100 show_pwm_enable, set_pwm_enable, 2);
1102 show_pwm_freq, set_pwm_freq, 2);
1106 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1109 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1112 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1115 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1118 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1121 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1125 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1128 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1131 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1134 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1137 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1140 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1144 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1147 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1150 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1153 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1156 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1159 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1185 static struct attribute *f71805f_attributes[] = {
1186 &sensor_dev_attr_in0_input.dev_attr.attr,
1187 &sensor_dev_attr_in0_max.dev_attr.attr,
1188 &sensor_dev_attr_in0_min.dev_attr.attr,
1189 &sensor_dev_attr_in1_input.dev_attr.attr,
1190 &sensor_dev_attr_in1_max.dev_attr.attr,
1191 &sensor_dev_attr_in1_min.dev_attr.attr,
1192 &sensor_dev_attr_in2_input.dev_attr.attr,
1193 &sensor_dev_attr_in2_max.dev_attr.attr,
1194 &sensor_dev_attr_in2_min.dev_attr.attr,
1195 &sensor_dev_attr_in3_input.dev_attr.attr,
1196 &sensor_dev_attr_in3_max.dev_attr.attr,
1197 &sensor_dev_attr_in3_min.dev_attr.attr,
1198 &sensor_dev_attr_in5_input.dev_attr.attr,
1199 &sensor_dev_attr_in5_max.dev_attr.attr,
1200 &sensor_dev_attr_in5_min.dev_attr.attr,
1201 &sensor_dev_attr_in6_input.dev_attr.attr,
1202 &sensor_dev_attr_in6_max.dev_attr.attr,
1203 &sensor_dev_attr_in6_min.dev_attr.attr,
1204 &sensor_dev_attr_in7_input.dev_attr.attr,
1205 &sensor_dev_attr_in7_max.dev_attr.attr,
1206 &sensor_dev_attr_in7_min.dev_attr.attr,
1208 &sensor_dev_attr_fan1_input.dev_attr.attr,
1209 &sensor_dev_attr_fan1_min.dev_attr.attr,
1210 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1211 &sensor_dev_attr_fan1_target.dev_attr.attr,
1212 &sensor_dev_attr_fan2_input.dev_attr.attr,
1213 &sensor_dev_attr_fan2_min.dev_attr.attr,
1214 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1215 &sensor_dev_attr_fan2_target.dev_attr.attr,
1216 &sensor_dev_attr_fan3_input.dev_attr.attr,
1217 &sensor_dev_attr_fan3_min.dev_attr.attr,
1218 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1219 &sensor_dev_attr_fan3_target.dev_attr.attr,
1221 &sensor_dev_attr_pwm1.dev_attr.attr,
1222 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1223 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
1224 &sensor_dev_attr_pwm2.dev_attr.attr,
1225 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1226 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
1227 &sensor_dev_attr_pwm3.dev_attr.attr,
1228 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1229 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
1231 &sensor_dev_attr_temp1_input.dev_attr.attr,
1232 &sensor_dev_attr_temp1_max.dev_attr.attr,
1233 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
1234 &sensor_dev_attr_temp1_type.dev_attr.attr,
1235 &sensor_dev_attr_temp2_input.dev_attr.attr,
1236 &sensor_dev_attr_temp2_max.dev_attr.attr,
1237 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
1238 &sensor_dev_attr_temp2_type.dev_attr.attr,
1239 &sensor_dev_attr_temp3_input.dev_attr.attr,
1240 &sensor_dev_attr_temp3_max.dev_attr.attr,
1241 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
1242 &sensor_dev_attr_temp3_type.dev_attr.attr,
1244 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1245 &sensor_dev_attr_pwm1_auto_point1_fan.dev_attr.attr,
1246 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1247 &sensor_dev_attr_pwm1_auto_point2_fan.dev_attr.attr,
1248 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1249 &sensor_dev_attr_pwm1_auto_point3_fan.dev_attr.attr,
1250 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1251 &sensor_dev_attr_pwm2_auto_point1_fan.dev_attr.attr,
1252 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1253 &sensor_dev_attr_pwm2_auto_point2_fan.dev_attr.attr,
1254 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1255 &sensor_dev_attr_pwm2_auto_point3_fan.dev_attr.attr,
1256 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1257 &sensor_dev_attr_pwm3_auto_point1_fan.dev_attr.attr,
1258 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1259 &sensor_dev_attr_pwm3_auto_point2_fan.dev_attr.attr,
1260 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1261 &sensor_dev_attr_pwm3_auto_point3_fan.dev_attr.attr,
1263 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1264 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1265 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1266 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1267 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1268 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1269 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1270 &dev_attr_alarms_in.attr,
1271 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1272 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1273 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1274 &dev_attr_alarms_temp.attr,
1275 &dev_attr_alarms_fan.attr,
1277 &dev_attr_name.attr,
1282 .attrs = f71805f_attributes,
1285 static struct attribute *f71805f_attributes_optin[4][5] = {
1287 &sensor_dev_attr_in4_input.dev_attr.attr,
1288 &sensor_dev_attr_in4_max.dev_attr.attr,
1289 &sensor_dev_attr_in4_min.dev_attr.attr,
1290 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1293 &sensor_dev_attr_in8_input.dev_attr.attr,
1294 &sensor_dev_attr_in8_max.dev_attr.attr,
1295 &sensor_dev_attr_in8_min.dev_attr.attr,
1296 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1299 &sensor_dev_attr_in9_input.dev_attr.attr,
1300 &sensor_dev_attr_in9_max.dev_attr.attr,
1301 &sensor_dev_attr_in9_min.dev_attr.attr,
1302 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1305 &sensor_dev_attr_in10_input.dev_attr.attr,
1306 &sensor_dev_attr_in10_max.dev_attr.attr,
1307 &sensor_dev_attr_in10_min.dev_attr.attr,
1308 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1314 { .attrs = f71805f_attributes_optin[0] },
1315 { .attrs = f71805f_attributes_optin[1] },
1316 { .attrs = f71805f_attributes_optin[2] },
1317 { .attrs = f71805f_attributes_optin[3] },
1324 static struct attribute *f71805f_attributes_pwm_freq[] = {
1325 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1326 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1327 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1332 .attrs = f71805f_attributes_pwm_freq,
1336 static struct attribute *f71805f_attr_pwm[] = {
1337 &sensor_dev_attr_pwm1.dev_attr.attr,
1338 &sensor_dev_attr_pwm2.dev_attr.attr,
1339 &sensor_dev_attr_pwm3.dev_attr.attr,
1352 if ((reg & 0x41) != 0x01) {
1362 for (i = 0; i < 3; i++) {
1370 data->
fan_ctrl[
i] &= ~FAN_CTRL_LATCH_FULL;
1384 static const char *
const names[] = {
1392 pr_err(
"Out of memory\n");
1399 dev_err(&pdev->
dev,
"Failed to request region 0x%lx-0x%lx\n",
1405 data->
name = names[sio_data->
kind];
1408 platform_set_drvdata(pdev, data);
1411 switch (sio_data->
kind) {
1417 if (sio_data->
fnsel1 & 0x01)
1418 data->
has_in |= (1 << 4);
1419 if (sio_data->
fnsel1 & 0x02)
1420 data->
has_in |= (1 << 8);
1425 f71805f_init_device(data);
1431 if (data->
has_in & (1 << 4)) {
1433 &f71805f_group_optin[0]);
1435 goto exit_remove_files;
1437 if (data->
has_in & (1 << 8)) {
1439 &f71805f_group_optin[1]);
1441 goto exit_remove_files;
1443 if (data->
has_in & (1 << 9)) {
1445 &f71805f_group_optin[2]);
1447 goto exit_remove_files;
1449 if (data->
has_in & (1 << 10)) {
1451 &f71805f_group_optin[3]);
1453 goto exit_remove_files;
1455 for (i = 0; i < 3; i++) {
1459 f71805f_attributes_pwm_freq[i]);
1461 goto exit_remove_files;
1466 f71805f_attr_pwm[i],
1469 dev_err(&pdev->
dev,
"chmod +w pwm%d failed\n",
1471 goto exit_remove_files;
1479 dev_err(&pdev->
dev,
"Class registration failed (%d)\n", err);
1480 goto exit_remove_files;
1487 for (i = 0; i < 4; i++)
1495 struct f71805f_data *data = platform_get_drvdata(pdev);
1500 for (i = 0; i < 4; i++)
1512 .probe = f71805f_probe,
1516 static int __init f71805f_device_add(
unsigned short address,
1529 pr_err(
"Device allocation failed\n");
1536 goto exit_device_put;
1540 pr_err(
"Device resource addition failed (%d)\n", err);
1541 goto exit_device_put;
1547 pr_err(
"Platform data allocation failed\n");
1548 goto exit_device_put;
1553 pr_err(
"Device addition failed (%d)\n", err);
1554 goto exit_device_put;
1565 static int __init f71805f_find(
int sioaddr,
unsigned short *address,
1571 static const char *
const names[] = {
1573 "F71872F/FG or F71806F/FG",
1576 superio_enter(sioaddr);
1582 devid = force_id ? force_id : superio_inw(sioaddr,
SIO_REG_DEVID);
1592 pr_info(
"Unsupported Fintek device, skipping\n");
1598 pr_warn(
"Device not activated, skipping\n");
1603 if (*address == 0) {
1604 pr_warn(
"Base address not set, skipping\n");
1610 pr_info(
"Found %s chip at %#x, revision %u\n",
1611 names[sio_data->
kind], *address,
1615 superio_exit(sioaddr);
1619 static int __init f71805f_init(
void)
1625 if (f71805f_find(0x2e, &address, &sio_data)
1626 && f71805f_find(0x4e, &address, &sio_data))
1634 err = f71805f_device_add(address, &sio_data);
1646 static void __exit f71805f_exit(
void)