59 #include <linux/module.h>
61 #include <linux/slab.h>
63 #include <linux/i2c.h>
75 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e,
I2C_CLIENT_END };
84 #define LM87_REG_IN(nr) (0x20 + (nr))
85 #define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
86 #define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
88 #define LM87_REG_AIN(nr) (0x28 + (nr))
89 #define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
90 #define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
92 static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
93 static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
94 static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
96 #define LM87_REG_TEMP_HW_INT_LOCK 0x13
97 #define LM87_REG_TEMP_HW_EXT_LOCK 0x14
98 #define LM87_REG_TEMP_HW_INT 0x17
99 #define LM87_REG_TEMP_HW_EXT 0x18
102 #define LM87_REG_FAN(nr) (0x28 + (nr))
103 #define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
104 #define LM87_REG_AOUT 0x19
106 #define LM87_REG_CONFIG 0x40
107 #define LM87_REG_CHANNEL_MODE 0x16
108 #define LM87_REG_VID_FAN_DIV 0x47
109 #define LM87_REG_VID4 0x49
111 #define LM87_REG_ALARMS1 0x41
112 #define LM87_REG_ALARMS2 0x42
114 #define LM87_REG_COMPANY_ID 0x3E
115 #define LM87_REG_REVISION 0x3F
122 #define IN_FROM_REG(reg, scale) (((reg) * (scale) + 96) / 192)
123 #define IN_TO_REG(val, scale) ((val) <= 0 ? 0 : \
124 (val) * 192 >= (scale) * 255 ? 255 : \
125 ((val) * 192 + (scale) / 2) / (scale))
127 #define TEMP_FROM_REG(reg) ((reg) * 1000)
128 #define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
129 (val) >= 126500 ? 127 : \
130 (((val) < 0 ? (val) - 500 : \
131 (val) + 500) / 1000))
133 #define FAN_FROM_REG(reg, div) ((reg) == 255 || (reg) == 0 ? 0 : \
134 (1350000 + (reg)*(div) / 2) / ((reg) * (div)))
135 #define FAN_TO_REG(val, div) ((val) * (div) * 255 <= 1350000 ? 255 : \
136 (1350000 + (val)*(div) / 2) / ((val) * (div)))
138 #define FAN_DIV_FROM_REG(reg) (1 << (reg))
141 #define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
142 #define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
143 (val) >= 2500 ? 255 : \
144 ((val) * 10 + 49) / 98)
147 #define CHAN_NO_FAN(nr) (1 << (nr))
148 #define CHAN_TEMP3 (1 << 2)
149 #define CHAN_VCC_5V (1 << 3)
150 #define CHAN_NO_VID (1 << 7)
211 data->
in[
i] = lm87_read_value(client,
213 data->
in_min[
i] = lm87_read_value(client,
215 data->
in_max[
i] = lm87_read_value(client,
219 for (i = 0; i < 2; i++) {
221 data->
in[6+
i] = lm87_read_value(client,
223 data->
in_max[6+
i] = lm87_read_value(client,
225 data->
in_min[6+
i] = lm87_read_value(client,
229 data->
fan[
i] = lm87_read_value(client,
231 data->
fan_min[
i] = lm87_read_value(client,
237 for (i = 0 ; i <
j; i++) {
238 data->
temp[
i] = lm87_read_value(client,
241 LM87_REG_TEMP_HIGH[i]);
242 data->
temp_low[
i] = lm87_read_value(client,
243 LM87_REG_TEMP_LOW[i]);
255 data->
fan_div[0] = (i >> 4) & 0x03;
256 data->
fan_div[1] = (i >> 6) & 0x03;
257 data->
vid = (i & 0x0F)
282 struct lm87_data *data = lm87_update_device(dev);
292 struct lm87_data *data = lm87_update_device(dev);
302 struct lm87_data *data = lm87_update_device(dev);
310 const char *buf,
size_t count)
313 struct lm87_data *data = i2c_get_clientdata(client);
318 err = kstrtol(buf, 10, &val);
331 const char *buf,
size_t count)
334 struct lm87_data *data = i2c_get_clientdata(client);
339 err = kstrtol(buf, 10, &val);
351 #define set_in(offset) \
352 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
353 show_in_input, NULL, offset); \
354 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
355 show_in_min, set_in_min, offset); \
356 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
357 show_in_max, set_in_max, offset)
370 struct lm87_data *data = lm87_update_device(dev);
379 struct lm87_data *data = lm87_update_device(dev);
389 struct lm87_data *data = lm87_update_device(dev);
397 const char *buf,
size_t count)
400 struct lm87_data *data = i2c_get_clientdata(client);
405 err = kstrtol(buf, 10, &val);
411 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->
temp_low[nr]);
417 const char *buf,
size_t count)
420 struct lm87_data *data = i2c_get_clientdata(client);
425 err = kstrtol(buf, 10, &val);
431 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->
temp_high[nr]);
436 #define set_temp(offset) \
437 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
438 show_temp_input, NULL, offset - 1); \
439 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
440 show_temp_high, set_temp_high, offset - 1); \
441 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
442 show_temp_low, set_temp_low, offset - 1)
450 struct lm87_data *data = lm87_update_device(dev);
457 struct lm87_data *data = lm87_update_device(dev);
468 struct lm87_data *data = lm87_update_device(dev);
478 struct lm87_data *data = lm87_update_device(dev);
488 struct lm87_data *data = lm87_update_device(dev);
496 const char *buf,
size_t count)
499 struct lm87_data *data = i2c_get_clientdata(client);
504 err = kstrtol(buf, 10, &val);
523 const char *buf,
size_t count)
526 struct lm87_data *data = i2c_get_clientdata(client);
533 err = kstrtol(buf, 10, &val);
562 reg = (reg & 0xCF) | (data->
fan_div[0] << 4);
565 reg = (reg & 0x3F) | (data->
fan_div[1] << 6);
578 #define set_fan(offset) \
579 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
580 show_fan_input, NULL, offset - 1); \
581 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
582 show_fan_min, set_fan_min, offset - 1); \
583 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
584 show_fan_div, set_fan_div, offset - 1)
591 struct lm87_data *data = lm87_update_device(dev);
599 struct lm87_data *data = lm87_update_device(dev);
611 const char *buf,
size_t count)
617 err = kstrtoul(buf, 10, &val);
628 struct lm87_data *data = lm87_update_device(dev);
632 const char *buf,
size_t count)
635 struct lm87_data *data = i2c_get_clientdata(client);
639 err = kstrtol(buf, 10, &val);
654 struct lm87_data *data = lm87_update_device(dev);
678 static struct attribute *lm87_attributes[] = {
679 &sensor_dev_attr_in1_input.dev_attr.attr,
680 &sensor_dev_attr_in1_min.dev_attr.attr,
681 &sensor_dev_attr_in1_max.dev_attr.attr,
682 &sensor_dev_attr_in1_alarm.dev_attr.attr,
683 &sensor_dev_attr_in2_input.dev_attr.attr,
684 &sensor_dev_attr_in2_min.dev_attr.attr,
685 &sensor_dev_attr_in2_max.dev_attr.attr,
686 &sensor_dev_attr_in2_alarm.dev_attr.attr,
687 &sensor_dev_attr_in3_input.dev_attr.attr,
688 &sensor_dev_attr_in3_min.dev_attr.attr,
689 &sensor_dev_attr_in3_max.dev_attr.attr,
690 &sensor_dev_attr_in3_alarm.dev_attr.attr,
691 &sensor_dev_attr_in4_input.dev_attr.attr,
692 &sensor_dev_attr_in4_min.dev_attr.attr,
693 &sensor_dev_attr_in4_max.dev_attr.attr,
694 &sensor_dev_attr_in4_alarm.dev_attr.attr,
696 &sensor_dev_attr_temp1_input.dev_attr.attr,
697 &sensor_dev_attr_temp1_max.dev_attr.attr,
698 &sensor_dev_attr_temp1_min.dev_attr.attr,
699 &dev_attr_temp1_crit.attr,
700 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
701 &sensor_dev_attr_temp2_input.dev_attr.attr,
702 &sensor_dev_attr_temp2_max.dev_attr.attr,
703 &sensor_dev_attr_temp2_min.dev_attr.attr,
704 &dev_attr_temp2_crit.attr,
705 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
706 &sensor_dev_attr_temp2_fault.dev_attr.attr,
708 &dev_attr_alarms.attr,
709 &dev_attr_aout_output.attr,
715 .attrs = lm87_attributes,
718 static struct attribute *lm87_attributes_in6[] = {
719 &sensor_dev_attr_in6_input.dev_attr.attr,
720 &sensor_dev_attr_in6_min.dev_attr.attr,
721 &sensor_dev_attr_in6_max.dev_attr.attr,
722 &sensor_dev_attr_in6_alarm.dev_attr.attr,
727 .attrs = lm87_attributes_in6,
730 static struct attribute *lm87_attributes_fan1[] = {
731 &sensor_dev_attr_fan1_input.dev_attr.attr,
732 &sensor_dev_attr_fan1_min.dev_attr.attr,
733 &sensor_dev_attr_fan1_div.dev_attr.attr,
734 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
739 .attrs = lm87_attributes_fan1,
742 static struct attribute *lm87_attributes_in7[] = {
743 &sensor_dev_attr_in7_input.dev_attr.attr,
744 &sensor_dev_attr_in7_min.dev_attr.attr,
745 &sensor_dev_attr_in7_max.dev_attr.attr,
746 &sensor_dev_attr_in7_alarm.dev_attr.attr,
751 .attrs = lm87_attributes_in7,
754 static struct attribute *lm87_attributes_fan2[] = {
755 &sensor_dev_attr_fan2_input.dev_attr.attr,
756 &sensor_dev_attr_fan2_min.dev_attr.attr,
757 &sensor_dev_attr_fan2_div.dev_attr.attr,
758 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
763 .attrs = lm87_attributes_fan2,
766 static struct attribute *lm87_attributes_temp3[] = {
767 &sensor_dev_attr_temp3_input.dev_attr.attr,
768 &sensor_dev_attr_temp3_max.dev_attr.attr,
769 &sensor_dev_attr_temp3_min.dev_attr.attr,
770 &dev_attr_temp3_crit.attr,
771 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
772 &sensor_dev_attr_temp3_fault.dev_attr.attr,
777 .attrs = lm87_attributes_temp3,
780 static struct attribute *lm87_attributes_in0_5[] = {
781 &sensor_dev_attr_in0_input.dev_attr.attr,
782 &sensor_dev_attr_in0_min.dev_attr.attr,
783 &sensor_dev_attr_in0_max.dev_attr.attr,
784 &sensor_dev_attr_in0_alarm.dev_attr.attr,
785 &sensor_dev_attr_in5_input.dev_attr.attr,
786 &sensor_dev_attr_in5_min.dev_attr.attr,
787 &sensor_dev_attr_in5_max.dev_attr.attr,
788 &sensor_dev_attr_in5_alarm.dev_attr.attr,
793 .attrs = lm87_attributes_in0_5,
796 static struct attribute *lm87_attributes_vid[] = {
797 &dev_attr_cpu0_vid.attr,
803 .attrs = lm87_attributes_vid,
824 && (rev >= 0x01 && rev <= 0x08))
827 && (rev & 0xf0) == 0x10)
830 dev_dbg(&adapter->
dev,
"LM87 detection failed at 0x%02x\n",
840 static void lm87_remove_files(
struct i2c_client *client)
854 static void lm87_init_client(
struct i2c_client *client)
856 struct lm87_data *data = i2c_get_clientdata(client);
858 if (client->
dev.platform_data) {
860 lm87_write_value(client,
867 if (!(data->
config & 0x01)) {
871 for (i = 1; i < 6; i++) {
875 for (i = 0; i < 2; i++) {
876 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
877 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
882 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
883 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
891 if ((data->
config & 0x09) != 0x01)
893 (data->
config & 0x77) | 0x01);
905 i2c_set_clientdata(client, data);
910 lm87_init_client(client);
972 lm87_remove_files(client);
978 static int lm87_remove(
struct i2c_client *client)
980 struct lm87_data *data = i2c_get_clientdata(client);
983 lm87_remove_files(client);
1005 .probe = lm87_probe,
1006 .remove = lm87_remove,
1007 .id_table = lm87_id,
1008 .detect = lm87_detect,
1009 .address_list = normal_i2c,