25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/module.h>
29 #include <linux/slab.h>
41 static unsigned short force_id;
47 #define DRVNAME "pc87427"
90 #define SIOREG_LDSEL 0x07
91 #define SIOREG_DEVID 0x20
92 #define SIOREG_CF2 0x22
93 #define SIOREG_CF3 0x23
94 #define SIOREG_CF4 0x24
95 #define SIOREG_CF5 0x25
96 #define SIOREG_CFB 0x2B
97 #define SIOREG_CFC 0x2C
98 #define SIOREG_CFD 0x2D
99 #define SIOREG_ACT 0x30
100 #define SIOREG_MAP 0x50
101 #define SIOREG_IOBASE 0x60
103 static const u8 logdev[2] = { 0x09, 0x14 };
104 static const char *logdev_str[2] = {
DRVNAME " FMC",
DRVNAME " HMC" };
109 static inline void superio_outb(
int sioaddr,
int reg,
int val)
112 outb(val, sioaddr + 1);
115 static inline int superio_inb(
int sioaddr,
int reg)
118 return inb(sioaddr + 1);
121 static inline void superio_exit(
int sioaddr)
124 outb(0x02, sioaddr + 1);
131 #define REGION_LENGTH 32
132 #define PC87427_REG_BANK 0x0f
133 #define BANK_FM(nr) (nr)
134 #define BANK_FT(nr) (0x08 + (nr))
135 #define BANK_FC(nr) (0x10 + (nr) * 2)
136 #define BANK_TM(nr) (nr)
137 #define BANK_VM(nr) (0x08 + (nr))
170 #define PC87427_REG_FAN 0x12
171 #define PC87427_REG_FAN_MIN 0x14
172 #define PC87427_REG_FAN_STATUS 0x10
174 #define FAN_STATUS_STALL (1 << 3)
175 #define FAN_STATUS_LOSPD (1 << 1)
176 #define FAN_STATUS_MONEN (1 << 0)
200 static inline unsigned long fan_from_reg(
u16 reg)
203 if (reg == 0x0000 || reg == 0xfffc)
205 return 5400000
UL /
reg;
209 static inline u16 fan_to_reg(
unsigned long val)
213 if (val >= 1350000
UL)
215 return ((1350000
UL + val / 2) / val) << 2;
222 #define PC87427_REG_PWM_ENABLE 0x10
223 #define PC87427_REG_PWM_DUTY 0x12
225 #define PWM_ENABLE_MODE_MASK (7 << 4)
226 #define PWM_ENABLE_CTLEN (1 << 0)
228 #define PWM_MODE_MANUAL (0 << 4)
229 #define PWM_MODE_AUTO (1 << 4)
230 #define PWM_MODE_OFF (2 << 4)
231 #define PWM_MODE_ON (7 << 4)
248 static inline int pwm_enable_from_reg(
u8 reg)
263 static inline u8 pwm_enable_to_reg(
unsigned long val,
u8 pwmval)
279 #define PC87427_REG_TEMP_STATUS 0x10
280 #define PC87427_REG_TEMP 0x14
281 #define PC87427_REG_TEMP_MAX 0x18
282 #define PC87427_REG_TEMP_MIN 0x19
283 #define PC87427_REG_TEMP_CRIT 0x1a
284 #define PC87427_REG_TEMP_TYPE 0x1d
286 #define TEMP_STATUS_CHANEN (1 << 0)
287 #define TEMP_STATUS_LOWFLG (1 << 1)
288 #define TEMP_STATUS_HIGHFLG (1 << 2)
289 #define TEMP_STATUS_CRITFLG (1 << 3)
290 #define TEMP_STATUS_SENSERR (1 << 5)
291 #define TEMP_TYPE_MASK (3 << 5)
293 #define TEMP_TYPE_THERMISTOR (1 << 5)
294 #define TEMP_TYPE_REMOTE_DIODE (2 << 5)
295 #define TEMP_TYPE_LOCAL_DIODE (3 << 5)
318 static inline unsigned int temp_type_from_reg(
u8 reg)
335 static inline long temp_from_reg(
s16 reg)
337 return reg * 1000 / 256;
340 static inline long temp_from_reg8(
s8 reg)
360 for (i = 0; i < 8; i++) {
363 pc87427_readall_fan(data, i);
367 for (i = 0; i < 4; i++) {
370 pc87427_readall_pwm(data, i);
374 for (i = 0; i < 6; i++) {
377 pc87427_readall_temp(data, i);
393 return sprintf(buf,
"%lu\n", fan_from_reg(data->
fan[nr]));
426 *devattr,
const char *buf,
size_t count)
433 if (kstrtoul(buf, 10, &val) < 0)
462 show_fan_min, set_fan_min, 0);
464 show_fan_min, set_fan_min, 1);
466 show_fan_min, set_fan_min, 2);
468 show_fan_min, set_fan_min, 3);
470 show_fan_min, set_fan_min, 4);
472 show_fan_min, set_fan_min, 5);
474 show_fan_min, set_fan_min, 6);
476 show_fan_min, set_fan_min, 7);
496 static struct attribute *pc87427_attributes_fan[8][5] = {
498 &sensor_dev_attr_fan1_input.dev_attr.attr,
499 &sensor_dev_attr_fan1_min.dev_attr.attr,
500 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
501 &sensor_dev_attr_fan1_fault.dev_attr.attr,
504 &sensor_dev_attr_fan2_input.dev_attr.attr,
505 &sensor_dev_attr_fan2_min.dev_attr.attr,
506 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
507 &sensor_dev_attr_fan2_fault.dev_attr.attr,
510 &sensor_dev_attr_fan3_input.dev_attr.attr,
511 &sensor_dev_attr_fan3_min.dev_attr.attr,
512 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
513 &sensor_dev_attr_fan3_fault.dev_attr.attr,
516 &sensor_dev_attr_fan4_input.dev_attr.attr,
517 &sensor_dev_attr_fan4_min.dev_attr.attr,
518 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
519 &sensor_dev_attr_fan4_fault.dev_attr.attr,
522 &sensor_dev_attr_fan5_input.dev_attr.attr,
523 &sensor_dev_attr_fan5_min.dev_attr.attr,
524 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
525 &sensor_dev_attr_fan5_fault.dev_attr.attr,
528 &sensor_dev_attr_fan6_input.dev_attr.attr,
529 &sensor_dev_attr_fan6_min.dev_attr.attr,
530 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
531 &sensor_dev_attr_fan6_fault.dev_attr.attr,
534 &sensor_dev_attr_fan7_input.dev_attr.attr,
535 &sensor_dev_attr_fan7_min.dev_attr.attr,
536 &sensor_dev_attr_fan7_alarm.dev_attr.attr,
537 &sensor_dev_attr_fan7_fault.dev_attr.attr,
540 &sensor_dev_attr_fan8_input.dev_attr.attr,
541 &sensor_dev_attr_fan8_min.dev_attr.attr,
542 &sensor_dev_attr_fan8_alarm.dev_attr.attr,
543 &sensor_dev_attr_fan8_fault.dev_attr.attr,
549 { .attrs = pc87427_attributes_fan[0] },
550 { .attrs = pc87427_attributes_fan[1] },
551 { .attrs = pc87427_attributes_fan[2] },
552 { .attrs = pc87427_attributes_fan[3] },
553 { .attrs = pc87427_attributes_fan[4] },
554 { .attrs = pc87427_attributes_fan[5] },
555 { .attrs = pc87427_attributes_fan[6] },
556 { .attrs = pc87427_attributes_fan[7] },
578 pwm_enable = pwm_enable_from_reg(data->
pwm_enable[nr]);
581 return sprintf(buf,
"%d\n", pwm_enable);
585 *devattr,
const char *buf,
size_t count)
591 if (kstrtoul(buf, 10, &val) < 0 || val > 2)
598 pc87427_readall_pwm(data, nr);
599 update_pwm_enable(data, nr, pwm_enable_to_reg(val, data->
pwm[nr]));
611 return sprintf(buf,
"%d\n", (
int)data->
pwm[nr]);
615 *devattr,
const char *buf,
size_t count)
623 if (kstrtoul(buf, 10, &val) < 0 || val > 0xff)
627 pc87427_readall_pwm(data, nr);
630 dev_notice(dev,
"Can't set PWM%d duty cycle while not in "
631 "manual mode\n", nr + 1);
641 dev_dbg(dev,
"Switching PWM%d from %s to %s\n", nr + 1,
647 dev_dbg(dev,
"Switching PWM%d from %s to %s\n", nr + 1,
660 show_pwm_enable, set_pwm_enable, 0);
662 show_pwm_enable, set_pwm_enable, 1);
664 show_pwm_enable, set_pwm_enable, 2);
666 show_pwm_enable, set_pwm_enable, 3);
673 static struct attribute *pc87427_attributes_pwm[4][3] = {
675 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
676 &sensor_dev_attr_pwm1.dev_attr.attr,
679 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
680 &sensor_dev_attr_pwm2.dev_attr.attr,
683 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
684 &sensor_dev_attr_pwm3.dev_attr.attr,
687 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
688 &sensor_dev_attr_pwm4.dev_attr.attr,
694 { .attrs = pc87427_attributes_pwm[0] },
695 { .attrs = pc87427_attributes_pwm[1] },
696 { .attrs = pc87427_attributes_pwm[2] },
697 { .attrs = pc87427_attributes_pwm[3] },
706 return sprintf(buf,
"%ld\n", temp_from_reg(data->
temp[nr]));
821 show_temp_min_alarm,
NULL, 0);
823 show_temp_min_alarm,
NULL, 1);
825 show_temp_min_alarm,
NULL, 2);
827 show_temp_min_alarm,
NULL, 3);
829 show_temp_min_alarm,
NULL, 4);
831 show_temp_min_alarm,
NULL, 5);
834 show_temp_max_alarm,
NULL, 0);
836 show_temp_max_alarm,
NULL, 1);
838 show_temp_max_alarm,
NULL, 2);
840 show_temp_max_alarm,
NULL, 3);
842 show_temp_max_alarm,
NULL, 4);
844 show_temp_max_alarm,
NULL, 5);
847 show_temp_crit_alarm,
NULL, 0);
849 show_temp_crit_alarm,
NULL, 1);
851 show_temp_crit_alarm,
NULL, 2);
853 show_temp_crit_alarm,
NULL, 3);
855 show_temp_crit_alarm,
NULL, 4);
857 show_temp_crit_alarm,
NULL, 5);
866 static struct attribute *pc87427_attributes_temp[6][10] = {
868 &sensor_dev_attr_temp1_input.dev_attr.attr,
869 &sensor_dev_attr_temp1_min.dev_attr.attr,
870 &sensor_dev_attr_temp1_max.dev_attr.attr,
871 &sensor_dev_attr_temp1_crit.dev_attr.attr,
872 &sensor_dev_attr_temp1_type.dev_attr.attr,
873 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
874 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
875 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
876 &sensor_dev_attr_temp1_fault.dev_attr.attr,
879 &sensor_dev_attr_temp2_input.dev_attr.attr,
880 &sensor_dev_attr_temp2_min.dev_attr.attr,
881 &sensor_dev_attr_temp2_max.dev_attr.attr,
882 &sensor_dev_attr_temp2_crit.dev_attr.attr,
883 &sensor_dev_attr_temp2_type.dev_attr.attr,
884 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
885 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
886 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
887 &sensor_dev_attr_temp2_fault.dev_attr.attr,
890 &sensor_dev_attr_temp3_input.dev_attr.attr,
891 &sensor_dev_attr_temp3_min.dev_attr.attr,
892 &sensor_dev_attr_temp3_max.dev_attr.attr,
893 &sensor_dev_attr_temp3_crit.dev_attr.attr,
894 &sensor_dev_attr_temp3_type.dev_attr.attr,
895 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
896 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
897 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
898 &sensor_dev_attr_temp3_fault.dev_attr.attr,
901 &sensor_dev_attr_temp4_input.dev_attr.attr,
902 &sensor_dev_attr_temp4_min.dev_attr.attr,
903 &sensor_dev_attr_temp4_max.dev_attr.attr,
904 &sensor_dev_attr_temp4_crit.dev_attr.attr,
905 &sensor_dev_attr_temp4_type.dev_attr.attr,
906 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
907 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
908 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
909 &sensor_dev_attr_temp4_fault.dev_attr.attr,
912 &sensor_dev_attr_temp5_input.dev_attr.attr,
913 &sensor_dev_attr_temp5_min.dev_attr.attr,
914 &sensor_dev_attr_temp5_max.dev_attr.attr,
915 &sensor_dev_attr_temp5_crit.dev_attr.attr,
916 &sensor_dev_attr_temp5_type.dev_attr.attr,
917 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
918 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
919 &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
920 &sensor_dev_attr_temp5_fault.dev_attr.attr,
923 &sensor_dev_attr_temp6_input.dev_attr.attr,
924 &sensor_dev_attr_temp6_min.dev_attr.attr,
925 &sensor_dev_attr_temp6_max.dev_attr.attr,
926 &sensor_dev_attr_temp6_crit.dev_attr.attr,
927 &sensor_dev_attr_temp6_type.dev_attr.attr,
928 &sensor_dev_attr_temp6_min_alarm.dev_attr.attr,
929 &sensor_dev_attr_temp6_max_alarm.dev_attr.attr,
930 &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr,
931 &sensor_dev_attr_temp6_fault.dev_attr.attr,
937 { .attrs = pc87427_attributes_temp[0] },
938 { .attrs = pc87427_attributes_temp[1] },
939 { .attrs = pc87427_attributes_temp[2] },
940 { .attrs = pc87427_attributes_temp[3] },
941 { .attrs = pc87427_attributes_temp[4] },
942 { .attrs = pc87427_attributes_temp[5] },
965 for (i = 0; i <
count; i++) {
968 dev_err(&pdev->
dev,
"Missing resource #%d\n", i);
972 resource_size(res),
DRVNAME)) {
974 "Failed to request region 0x%lx-0x%lx\n",
975 (
unsigned long)res->
start,
976 (
unsigned long)res->
end);
993 dev_warn(dev,
"%s module not ready!\n",
"FMC");
996 for (i = 0; i < 8; i++) {
1006 dev_dbg(dev,
"Enabling monitoring of all fans\n");
1007 for (i = 0; i < 8; i++) {
1018 for (i = 0; i < 4; i++) {
1032 dev_dbg(dev,
"PWM%d is in automatic control mode\n",
1041 dev_warn(dev,
"%s module not ready!\n",
"HMC");
1044 for (i = 0; i < 6; i++) {
1052 static void pc87427_remove_files(
struct device *dev)
1058 for (i = 0; i < 8; i++) {
1063 for (i = 0; i < 4; i++) {
1068 for (i = 0; i < 6; i++) {
1084 pr_err(
"Out of memory\n");
1092 err = pc87427_request_regions(pdev, res_count);
1097 data->
name =
"pc87427";
1098 platform_set_drvdata(pdev, data);
1099 pc87427_init_device(&pdev->
dev);
1105 for (i = 0; i < 8; i++) {
1109 &pc87427_group_fan[i]);
1111 goto exit_remove_files;
1113 for (i = 0; i < 4; i++) {
1117 &pc87427_group_pwm[i]);
1119 goto exit_remove_files;
1121 for (i = 0; i < 6; i++) {
1125 &pc87427_group_temp[i]);
1127 goto exit_remove_files;
1133 dev_err(&pdev->
dev,
"Class registration failed (%d)\n", err);
1134 goto exit_remove_files;
1140 pc87427_remove_files(&pdev->
dev);
1146 struct pc87427_data *data = platform_get_drvdata(pdev);
1149 pc87427_remove_files(&pdev->
dev);
1160 .probe = pc87427_probe,
1173 for (i = 0; i < 2; i++) {
1190 pr_err(
"Device allocation failed\n");
1196 pr_err(
"Device resource addition failed (%d)\n", err);
1197 goto exit_device_put;
1203 pr_err(
"Platform data allocation failed\n");
1204 goto exit_device_put;
1209 pr_err(
"Device addition failed (%d)\n", err);
1210 goto exit_device_put;
1228 val = force_id ? force_id : superio_inb(sioaddr,
SIOREG_DEVID);
1234 for (i = 0; i < 2; i++) {
1240 if (!(val & 0x01)) {
1241 pr_info(
"Logical device 0x%02x not activated\n",
1248 pr_warn(
"Logical device 0x%02x is memory-mapped, "
1249 "can't use\n", logdev[i]);
1256 pr_info(
"I/O base address not set for logical device "
1257 "0x%02x\n", logdev[i]);
1270 sio_data->
has_fanin = (1 << 2) | (1 << 3);
1273 if (!(cfg & (1 << 3)))
1275 if (!(cfg & (1 << 2)))
1279 if (!(cfg & (1 << 0)))
1283 if (!(cfg & (1 << 0)))
1286 if (!(cfg & (1 << 1)) && (cfg_b & (1 << 3)))
1289 if ((cfg & (1 << 3)) && !(cfg_b & (1 << 5)))
1294 if (cfg_b & (1 << 0))
1298 if (!(cfg & (1 << 4))) {
1299 if (cfg_b & (1 << 1))
1301 if (cfg_b & (1 << 2))
1313 superio_exit(sioaddr);
1317 static int __init pc87427_init(
void)
1322 if (pc87427_find(0x2e, &sio_data)
1323 && pc87427_find(0x4e, &sio_data))
1331 err = pc87427_device_add(&sio_data);
1343 static void __exit pc87427_exit(
void)