24 #include <linux/module.h>
26 #include <linux/slab.h>
28 #include <linux/i2c.h>
35 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
41 #define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2)
42 #define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2)
43 #define LM80_REG_IN(nr) (0x20 + (nr))
45 #define LM80_REG_FAN1 0x28
46 #define LM80_REG_FAN2 0x29
47 #define LM80_REG_FAN_MIN(nr) (0x3b + (nr))
49 #define LM80_REG_TEMP 0x27
50 #define LM80_REG_TEMP_HOT_MAX 0x38
51 #define LM80_REG_TEMP_HOT_HYST 0x39
52 #define LM80_REG_TEMP_OS_MAX 0x3a
53 #define LM80_REG_TEMP_OS_HYST 0x3b
55 #define LM80_REG_CONFIG 0x00
56 #define LM80_REG_ALARM1 0x01
57 #define LM80_REG_ALARM2 0x02
58 #define LM80_REG_MASK1 0x03
59 #define LM80_REG_MASK2 0x04
60 #define LM80_REG_FANDIV 0x05
61 #define LM80_REG_RES 0x06
63 #define LM96080_REG_CONV_RATE 0x07
64 #define LM96080_REG_MAN_ID 0x3e
65 #define LM96080_REG_DEV_ID 0x3f
75 #define IN_TO_REG(val) (SENSORS_LIMIT(((val) + 5) / 10, 0, 255))
76 #define IN_FROM_REG(val) ((val) * 10)
78 static inline unsigned char FAN_TO_REG(
unsigned rpm,
unsigned div)
82 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
83 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
86 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
87 (val) == 255 ? 0 : 1350000/((div) * (val)))
95 res = 625 * (
long) temp;
97 res = ((
long) temp - 0x01000) * 625;
102 #define TEMP_LIMIT_FROM_REG(val) (((val) > 0x80 ? \
103 (val) - 0x100 : (val)) * 1000)
105 #define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val) < 0 ? \
106 ((val) - 500) / 1000 : ((val) + 500) / 1000, 0, 255)
108 #define DIV_FROM_REG(val) (1 << (val))
165 .remove = lm80_remove,
167 .detect = lm80_detect,
168 .address_list = normal_i2c,
175 #define show_in(suffix, value) \
176 static ssize_t show_in_##suffix(struct device *dev, \
177 struct device_attribute *attr, char *buf) \
179 int nr = to_sensor_dev_attr(attr)->index; \
180 struct lm80_data *data = lm80_update_device(dev); \
182 return PTR_ERR(data); \
183 return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \
189 #define set_in(suffix, value, reg) \
190 static ssize_t set_in_##suffix(struct device *dev, \
191 struct device_attribute *attr, const char *buf, size_t count) \
193 int nr = to_sensor_dev_attr(attr)->index; \
194 struct i2c_client *client = to_i2c_client(dev); \
195 struct lm80_data *data = i2c_get_clientdata(client); \
197 int err = kstrtol(buf, 10, &val); \
201 mutex_lock(&data->update_lock);\
202 data->value[nr] = IN_TO_REG(val); \
203 lm80_write_value(client, reg(nr), data->value[nr]); \
204 mutex_unlock(&data->update_lock);\
210 #define show_fan(suffix, value) \
211 static ssize_t show_fan_##suffix(struct device *dev, \
212 struct device_attribute *attr, char *buf) \
214 int nr = to_sensor_dev_attr(attr)->index; \
215 struct lm80_data *data = lm80_update_device(dev); \
217 return PTR_ERR(data); \
218 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \
219 DIV_FROM_REG(data->fan_div[nr]))); \
230 return PTR_ERR(data);
241 int err = kstrtoul(buf, 10, &val);
259 const char *buf,
size_t count)
263 struct lm80_data *data = i2c_get_clientdata(client);
266 int err = kstrtoul(buf, 10, &val);
289 dev_err(&client->
dev,
"fan_div value %ld not "
290 "supported. Choose one of 1, 2, 4 or 8!\n", val);
295 reg = (lm80_read_value(client,
LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
310 struct lm80_data *data = lm80_update_device(dev);
312 return PTR_ERR(data);
316 #define show_temp(suffix, value) \
317 static ssize_t show_temp_##suffix(struct device *dev, \
318 struct device_attribute *attr, char *buf) \
320 struct lm80_data *data = lm80_update_device(dev); \
322 return PTR_ERR(data); \
323 return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
330 #define set_temp(suffix, value, reg) \
331 static ssize_t set_temp_##suffix(struct device *dev, \
332 struct device_attribute *attr, const char *buf, size_t count) \
334 struct i2c_client *client = to_i2c_client(dev); \
335 struct lm80_data *data = i2c_get_clientdata(client); \
337 int err = kstrtol(buf, 10, &val); \
341 mutex_lock(&data->update_lock); \
342 data->value = TEMP_LIMIT_TO_REG(val); \
343 lm80_write_value(client, reg, data->value); \
344 mutex_unlock(&data->update_lock); \
355 struct lm80_data *data = lm80_update_device(dev);
357 return PTR_ERR(data);
365 struct lm80_data *data = lm80_update_device(dev);
367 return PTR_ERR(data);
372 show_in_min, set_in_min, 0);
374 show_in_min, set_in_min, 1);
376 show_in_min, set_in_min, 2);
378 show_in_min, set_in_min, 3);
380 show_in_min, set_in_min, 4);
382 show_in_min, set_in_min, 5);
384 show_in_min, set_in_min, 6);
386 show_in_max, set_in_max, 0);
388 show_in_max, set_in_max, 1);
390 show_in_max, set_in_max, 2);
392 show_in_max, set_in_max, 3);
394 show_in_max, set_in_max, 4);
396 show_in_max, set_in_max, 5);
398 show_in_max, set_in_max, 6);
407 show_fan_min, set_fan_min, 0);
409 show_fan_min, set_fan_min, 1);
413 show_fan_div, set_fan_div, 0);
415 show_fan_div, set_fan_div, 1);
442 static struct attribute *lm80_attributes[] = {
443 &sensor_dev_attr_in0_min.dev_attr.attr,
444 &sensor_dev_attr_in1_min.dev_attr.attr,
445 &sensor_dev_attr_in2_min.dev_attr.attr,
446 &sensor_dev_attr_in3_min.dev_attr.attr,
447 &sensor_dev_attr_in4_min.dev_attr.attr,
448 &sensor_dev_attr_in5_min.dev_attr.attr,
449 &sensor_dev_attr_in6_min.dev_attr.attr,
450 &sensor_dev_attr_in0_max.dev_attr.attr,
451 &sensor_dev_attr_in1_max.dev_attr.attr,
452 &sensor_dev_attr_in2_max.dev_attr.attr,
453 &sensor_dev_attr_in3_max.dev_attr.attr,
454 &sensor_dev_attr_in4_max.dev_attr.attr,
455 &sensor_dev_attr_in5_max.dev_attr.attr,
456 &sensor_dev_attr_in6_max.dev_attr.attr,
457 &sensor_dev_attr_in0_input.dev_attr.attr,
458 &sensor_dev_attr_in1_input.dev_attr.attr,
459 &sensor_dev_attr_in2_input.dev_attr.attr,
460 &sensor_dev_attr_in3_input.dev_attr.attr,
461 &sensor_dev_attr_in4_input.dev_attr.attr,
462 &sensor_dev_attr_in5_input.dev_attr.attr,
463 &sensor_dev_attr_in6_input.dev_attr.attr,
464 &sensor_dev_attr_fan1_min.dev_attr.attr,
465 &sensor_dev_attr_fan2_min.dev_attr.attr,
466 &sensor_dev_attr_fan1_input.dev_attr.attr,
467 &sensor_dev_attr_fan2_input.dev_attr.attr,
468 &sensor_dev_attr_fan1_div.dev_attr.attr,
469 &sensor_dev_attr_fan2_div.dev_attr.attr,
470 &dev_attr_temp1_input.attr,
471 &dev_attr_temp1_max.attr,
472 &dev_attr_temp1_max_hyst.attr,
473 &dev_attr_temp1_crit.attr,
474 &dev_attr_temp1_crit_hyst.attr,
475 &dev_attr_alarms.attr,
476 &sensor_dev_attr_in0_alarm.dev_attr.attr,
477 &sensor_dev_attr_in1_alarm.dev_attr.attr,
478 &sensor_dev_attr_in2_alarm.dev_attr.attr,
479 &sensor_dev_attr_in3_alarm.dev_attr.attr,
480 &sensor_dev_attr_in4_alarm.dev_attr.attr,
481 &sensor_dev_attr_in5_alarm.dev_attr.attr,
482 &sensor_dev_attr_in6_alarm.dev_attr.attr,
483 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
484 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
485 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
486 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
491 .attrs = lm80_attributes,
516 if (man_id == 0x01 && dev_id == 0x08) {
524 for (i = 0x2a; i <= 0x3d; i++) {
540 static int lm80_probe(
struct i2c_client *client,
550 i2c_set_clientdata(client, data);
554 lm80_init_client(client);
578 static int lm80_remove(
struct i2c_client *client)
580 struct lm80_data *data = i2c_get_clientdata(client);
588 static int lm80_read_value(
struct i2c_client *client,
u8 reg)
599 static void lm80_init_client(
struct i2c_client *client)
617 struct lm80_data *data = i2c_get_clientdata(client);
626 lm80_init_client(client);
629 dev_dbg(&client->
dev,
"Starting lm80 update\n");
630 for (i = 0; i <= 6; i++) {
673 data->
temp = (prev_rv << 8) | (rv & 0xf0);
698 data->
fan_div[0] = (rv >> 2) & 0x03;
699 data->
fan_div[1] = (rv >> 4) & 0x03;
707 data->
alarms = prev_rv + (rv << 8);