40 #include <linux/module.h>
42 #include <linux/slab.h>
44 #include <linux/i2c.h>
50 #include <linux/types.h>
60 static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e,
I2C_CLIENT_END };
66 #define LM63_REG_CONFIG1 0x03
67 #define LM63_REG_CONVRATE 0x04
68 #define LM63_REG_CONFIG2 0xBF
69 #define LM63_REG_CONFIG_FAN 0x4A
71 #define LM63_REG_TACH_COUNT_MSB 0x47
72 #define LM63_REG_TACH_COUNT_LSB 0x46
73 #define LM63_REG_TACH_LIMIT_MSB 0x49
74 #define LM63_REG_TACH_LIMIT_LSB 0x48
76 #define LM63_REG_PWM_VALUE 0x4C
77 #define LM63_REG_PWM_FREQ 0x4D
78 #define LM63_REG_LUT_TEMP_HYST 0x4F
79 #define LM63_REG_LUT_TEMP(nr) (0x50 + 2 * (nr))
80 #define LM63_REG_LUT_PWM(nr) (0x51 + 2 * (nr))
82 #define LM63_REG_LOCAL_TEMP 0x00
83 #define LM63_REG_LOCAL_HIGH 0x05
85 #define LM63_REG_REMOTE_TEMP_MSB 0x01
86 #define LM63_REG_REMOTE_TEMP_LSB 0x10
87 #define LM63_REG_REMOTE_OFFSET_MSB 0x11
88 #define LM63_REG_REMOTE_OFFSET_LSB 0x12
89 #define LM63_REG_REMOTE_HIGH_MSB 0x07
90 #define LM63_REG_REMOTE_HIGH_LSB 0x13
91 #define LM63_REG_REMOTE_LOW_MSB 0x08
92 #define LM63_REG_REMOTE_LOW_LSB 0x14
93 #define LM63_REG_REMOTE_TCRIT 0x19
94 #define LM63_REG_REMOTE_TCRIT_HYST 0x21
96 #define LM63_REG_ALERT_STATUS 0x02
97 #define LM63_REG_ALERT_MASK 0x16
99 #define LM63_REG_MAN_ID 0xFE
100 #define LM63_REG_CHIP_ID 0xFF
102 #define LM96163_REG_TRUTHERM 0x30
103 #define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
104 #define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
105 #define LM96163_REG_CONFIG_ENHANCED 0x45
107 #define LM63_MAX_CONVRATE 9
109 #define LM63_MAX_CONVRATE_HZ 32
110 #define LM96163_MAX_CONVRATE_HZ 26
124 #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
126 #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
127 (5400000 / (val)) & 0xFFFC)
128 #define TEMP8_FROM_REG(reg) ((reg) * 1000)
129 #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
130 (val) >= 127000 ? 127 : \
131 (val) < 0 ? ((val) - 500) / 1000 : \
132 ((val) + 500) / 1000)
133 #define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \
134 (val) >= 255000 ? 255 : \
135 ((val) + 500) / 1000)
136 #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
137 #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
138 (val) >= 127875 ? 0x7FE0 : \
139 (val) < 0 ? ((val) - 62) / 125 * 32 : \
140 ((val) + 62) / 125 * 32)
141 #define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \
142 (val) >= 255875 ? 0xFFE0 : \
143 ((val) + 62) / 125 * 32)
144 #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
145 (val) >= 127000 ? 127 : \
146 ((val) + 500) / 1000)
148 #define UPDATE_INTERVAL(max, rate) \
149 ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
223 struct lm63_data *data = i2c_get_clientdata(client);
228 for (i = 0; i < data->
lut_size; i++) {
245 struct lm63_data *data = i2c_get_clientdata(client);
246 unsigned long next_update;
254 if (data->
config & 0x04) {
314 lm63_update_lut(client);
325 static int lm63_lut_looks_bad(
struct i2c_client *client)
327 struct lm63_data *data = i2c_get_clientdata(client);
331 lm63_update_lut(client);
333 for (i = 1; i < data->
lut_size; i++) {
334 if (data->
pwm1[1 + i - 1] > data->
pwm1[1 + i]
335 || data->
temp8[3 + i - 1] > data->
temp8[3 + i]) {
337 "Lookup table doesn't look sane (check entries %d and %d)\n",
355 struct lm63_data *data = lm63_update_device(dev);
360 const char *buf,
size_t count)
363 struct lm63_data *data = i2c_get_clientdata(client);
367 err = kstrtoul(buf, 10, &val);
374 data->
fan[1] & 0xFF);
385 struct lm63_data *data = lm63_update_device(dev);
396 return sprintf(buf,
"%d\n", pwm);
400 const char *buf,
size_t count)
404 struct lm63_data *data = i2c_get_clientdata(client);
405 int nr = attr->
index;
413 err = kstrtoul(buf, 10, &val);
418 val = SENSORS_LIMIT(val, 0, 255);
431 struct lm63_data *data = lm63_update_device(dev);
437 const char *buf,
size_t count)
440 struct lm63_data *data = i2c_get_clientdata(client);
444 err = kstrtoul(buf, 10, &val);
447 if (val < 1 || val > 2)
454 if (val == 2 && lm63_lut_looks_bad(client))
481 struct lm63_data *data = lm63_update_device(dev);
490 struct lm63_data *data = lm63_update_device(dev);
491 return sprintf(buf,
"%d\n", temp8_from_reg(data, attr->
index)
500 struct lm63_data *data = lm63_update_device(dev);
501 return sprintf(buf,
"%d\n", lut_temp_from_reg(data, attr->
index)
506 const char *buf,
size_t count)
510 struct lm63_data *data = i2c_get_clientdata(client);
511 int nr = attr->
index;
517 err = kstrtol(buf, 10, &val);
536 temp = lut_temp_to_reg(data, val);
548 struct lm63_data *data = lm63_update_device(dev);
549 int nr = attr->
index;
571 const char *buf,
size_t count)
573 static const u8 reg[6] = {
584 struct lm63_data *data = i2c_get_clientdata(client);
587 int nr = attr->
index;
589 err = kstrtol(buf, 10, &val);
614 struct lm63_data *data = lm63_update_device(dev);
615 return sprintf(buf,
"%d\n", temp8_from_reg(data, 2)
624 struct lm63_data *data = lm63_update_device(dev);
626 return sprintf(buf,
"%d\n", lut_temp_from_reg(data, attr->
index)
637 const char *buf,
size_t count)
640 struct lm63_data *data = i2c_get_clientdata(client);
645 err = kstrtol(buf, 10, &val);
673 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
674 if (interval >= update_interval * 3 / 4)
691 const char *buf,
size_t count)
694 struct lm63_data *data = i2c_get_clientdata(client);
698 err = kstrtoul(buf, 10, &val);
703 lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000));
713 struct lm63_data *data = i2c_get_clientdata(client);
719 const char *buf,
size_t count)
722 struct lm63_data *data = i2c_get_clientdata(client);
727 ret = kstrtoul(buf, 10, &val);
730 if (val != 1 && val != 2)
737 reg | (data->
trutherm ? 0x02 : 0x00));
747 struct lm63_data *data = lm63_update_device(dev);
755 struct lm63_data *data = lm63_update_device(dev);
756 int bitnr = attr->
index;
767 show_pwm1_enable, set_pwm1_enable);
769 show_pwm1, set_pwm1, 1);
771 show_lut_temp, set_temp8, 3);
773 show_lut_temp_hyst,
NULL, 3);
775 show_pwm1, set_pwm1, 2);
777 show_lut_temp, set_temp8, 4);
779 show_lut_temp_hyst,
NULL, 4);
781 show_pwm1, set_pwm1, 3);
783 show_lut_temp, set_temp8, 5);
785 show_lut_temp_hyst,
NULL, 5);
787 show_pwm1, set_pwm1, 4);
789 show_lut_temp, set_temp8, 6);
791 show_lut_temp_hyst,
NULL, 6);
793 show_pwm1, set_pwm1, 5);
795 show_lut_temp, set_temp8, 7);
797 show_lut_temp_hyst,
NULL, 7);
799 show_pwm1, set_pwm1, 6);
801 show_lut_temp, set_temp8, 8);
803 show_lut_temp_hyst,
NULL, 8);
805 show_pwm1, set_pwm1, 7);
807 show_lut_temp, set_temp8, 9);
809 show_lut_temp_hyst,
NULL, 9);
811 show_pwm1, set_pwm1, 8);
813 show_lut_temp, set_temp8, 10);
815 show_lut_temp_hyst,
NULL, 10);
817 show_pwm1, set_pwm1, 9);
819 show_lut_temp, set_temp8, 11);
821 show_lut_temp_hyst,
NULL, 11);
823 show_pwm1, set_pwm1, 10);
825 show_lut_temp, set_temp8, 12);
827 show_lut_temp_hyst,
NULL, 12);
829 show_pwm1, set_pwm1, 11);
831 show_lut_temp, set_temp8, 13);
833 show_lut_temp_hyst,
NULL, 13);
835 show_pwm1, set_pwm1, 12);
837 show_lut_temp, set_temp8, 14);
839 show_lut_temp_hyst,
NULL, 14);
855 set_temp2_crit_hyst);
870 set_update_interval);
872 static struct attribute *lm63_attributes[] = {
873 &sensor_dev_attr_pwm1.dev_attr.attr,
874 &dev_attr_pwm1_enable.attr,
875 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
876 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
877 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
878 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
879 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
880 &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
881 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
882 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
883 &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
884 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
885 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
886 &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
887 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
888 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
889 &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
890 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
891 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
892 &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
893 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
894 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
895 &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
896 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
897 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
898 &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
900 &sensor_dev_attr_temp1_input.dev_attr.attr,
901 &sensor_dev_attr_temp2_input.dev_attr.attr,
902 &sensor_dev_attr_temp2_min.dev_attr.attr,
903 &sensor_dev_attr_temp1_max.dev_attr.attr,
904 &sensor_dev_attr_temp2_max.dev_attr.attr,
905 &sensor_dev_attr_temp2_offset.dev_attr.attr,
906 &sensor_dev_attr_temp2_crit.dev_attr.attr,
907 &dev_attr_temp2_crit_hyst.attr,
909 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
910 &sensor_dev_attr_temp2_fault.dev_attr.attr,
911 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
912 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
913 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
914 &dev_attr_alarms.attr,
915 &dev_attr_update_interval.attr,
919 static struct attribute *lm63_attributes_extra_lut[] = {
920 &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr,
921 &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr,
922 &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr,
923 &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr,
924 &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr,
925 &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr,
926 &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr,
927 &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr,
928 &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr,
929 &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr,
930 &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr,
931 &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr,
936 .attrs = lm63_attributes_extra_lut,
951 struct lm63_data *data = i2c_get_clientdata(client);
953 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
962 .is_visible = lm63_attribute_mode,
963 .attrs = lm63_attributes,
966 static struct attribute *lm63_attributes_fan1[] = {
967 &sensor_dev_attr_fan1_input.dev_attr.attr,
968 &sensor_dev_attr_fan1_min.dev_attr.attr,
970 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
975 .attrs = lm63_attributes_fan1,
983 static int lm63_detect(
struct i2c_client *client,
987 u8 man_id,
chip_id, reg_config1, reg_config2;
988 u8 reg_alert_status, reg_alert_mask;
1004 || (reg_config1 & 0x18) != 0x00
1005 || (reg_config2 & 0xF8) != 0x00
1006 || (reg_alert_status & 0x20) != 0x00
1007 || (reg_alert_mask & 0xA4) != 0xA4) {
1009 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
1014 if (chip_id == 0x41 && address == 0x4c)
1016 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
1018 else if (chip_id == 0x49 && address == 0x4c)
1030 static void lm63_init_client(
struct i2c_client *client)
1032 struct lm63_data *data = i2c_get_clientdata(client);
1040 if (data->
config & 0x40) {
1041 dev_dbg(&client->
dev,
"Switching to operational mode\n");
1055 switch (data->
kind) {
1083 if (config_enhanced & 0x20)
1085 if ((config_enhanced & 0x10)
1088 if (config_enhanced & 0x08)
1094 dev_dbg(&client->
dev,
"Alert/tach pin configured for %s\n",
1095 (data->
config & 0x04) ?
"tachometer input" :
1097 dev_dbg(&client->
dev,
"PWM clock %s kHz, output frequency %u Hz\n",
1100 dev_dbg(&client->
dev,
"PWM output active %s, %s mode\n",
1102 (data->
config_fan & 0x20) ?
"manual" :
"auto");
1105 static int lm63_probe(
struct i2c_client *client,
1115 i2c_set_clientdata(client, data);
1120 data->
kind =
id->driver_data;
1125 lm63_init_client(client);
1131 if (data->
config & 0x04) {
1134 goto exit_remove_files;
1139 goto exit_remove_files;
1142 &lm63_group_extra_lut);
1144 goto exit_remove_files;
1150 goto exit_remove_files;
1165 static int lm63_remove(
struct i2c_client *client)
1167 struct lm63_data *data = i2c_get_clientdata(client);
1197 .probe = lm63_probe,
1198 .remove = lm63_remove,
1199 .id_table = lm63_id,
1200 .detect = lm63_detect,
1201 .address_list = normal_i2c,