22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/module.h>
26 #include <linux/slab.h>
28 #include <linux/i2c.h>
42 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
52 #define LM78_ADDR_REG_OFFSET 5
53 #define LM78_DATA_REG_OFFSET 6
56 #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
57 #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
58 #define LM78_REG_IN(nr) (0x20 + (nr))
60 #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
61 #define LM78_REG_FAN(nr) (0x28 + (nr))
63 #define LM78_REG_TEMP 0x27
64 #define LM78_REG_TEMP_OVER 0x39
65 #define LM78_REG_TEMP_HYST 0x3a
67 #define LM78_REG_ALARM1 0x41
68 #define LM78_REG_ALARM2 0x42
70 #define LM78_REG_VID_FANDIV 0x47
72 #define LM78_REG_CONFIG 0x40
73 #define LM78_REG_CHIPID 0x49
74 #define LM78_REG_I2C_ADDR 0x48
88 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
89 return (nval + 8) / 16;
91 #define IN_FROM_REG(val) ((val) * 16)
97 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
102 return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val *
div);
111 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
112 return nval < 0 ? (nval - 500) / 1000 : (nval + 500) / 1000;
120 #define DIV_FROM_REG(val) (1 << (val))
169 struct lm78_data *data = lm78_update_device(dev);
177 struct lm78_data *data = lm78_update_device(dev);
182 const char *buf,
size_t count)
190 err = kstrtoul(buf, 10, &val);
202 const char *buf,
size_t count)
206 int nr = attr->
index;
210 err = kstrtoul(buf, 10, &val);
221 #define show_in_offset(offset) \
222 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
223 show_in, NULL, offset); \
224 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
225 show_in_min, set_in_min, offset); \
226 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
227 show_in_max, set_in_max, offset);
241 struct lm78_data *data = lm78_update_device(dev);
248 struct lm78_data *data = lm78_update_device(dev);
253 const char *buf,
size_t count)
259 err = kstrtol(buf, 10, &val);
273 struct lm78_data *data = lm78_update_device(dev);
278 const char *buf,
size_t count)
284 err = kstrtol(buf, 10, &val);
297 show_temp_over, set_temp_over);
299 show_temp_hyst, set_temp_hyst);
306 struct lm78_data *data = lm78_update_device(dev);
307 int nr = attr->
index;
316 struct lm78_data *data = lm78_update_device(dev);
317 int nr = attr->
index;
323 const char *buf,
size_t count)
327 int nr = attr->
index;
331 err = kstrtoul(buf, 10, &val);
346 struct lm78_data *data = lm78_update_device(dev);
357 const char *buf,
size_t count)
361 int nr = attr->
index;
367 err = kstrtoul(buf, 10, &val);
389 dev_err(dev,
"fan_div value %ld not "
390 "supported. Choose one of 1, 2, 4 or 8!\n", val);
398 reg = (reg & 0xcf) | (data->
fan_div[nr] << 4);
401 reg = (reg & 0x3f) | (data->
fan_div[nr] << 6);
414 #define show_fan_offset(offset) \
415 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
416 show_fan, NULL, offset - 1); \
417 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
418 show_fan_min, set_fan_min, offset - 1);
426 show_fan_div, set_fan_div, 0);
428 show_fan_div, set_fan_div, 1);
435 struct lm78_data *data = lm78_update_device(dev);
444 struct lm78_data *data = lm78_update_device(dev);
452 struct lm78_data *data = lm78_update_device(dev);
468 static struct attribute *lm78_attributes[] = {
469 &sensor_dev_attr_in0_input.dev_attr.attr,
470 &sensor_dev_attr_in0_min.dev_attr.attr,
471 &sensor_dev_attr_in0_max.dev_attr.attr,
472 &sensor_dev_attr_in0_alarm.dev_attr.attr,
473 &sensor_dev_attr_in1_input.dev_attr.attr,
474 &sensor_dev_attr_in1_min.dev_attr.attr,
475 &sensor_dev_attr_in1_max.dev_attr.attr,
476 &sensor_dev_attr_in1_alarm.dev_attr.attr,
477 &sensor_dev_attr_in2_input.dev_attr.attr,
478 &sensor_dev_attr_in2_min.dev_attr.attr,
479 &sensor_dev_attr_in2_max.dev_attr.attr,
480 &sensor_dev_attr_in2_alarm.dev_attr.attr,
481 &sensor_dev_attr_in3_input.dev_attr.attr,
482 &sensor_dev_attr_in3_min.dev_attr.attr,
483 &sensor_dev_attr_in3_max.dev_attr.attr,
484 &sensor_dev_attr_in3_alarm.dev_attr.attr,
485 &sensor_dev_attr_in4_input.dev_attr.attr,
486 &sensor_dev_attr_in4_min.dev_attr.attr,
487 &sensor_dev_attr_in4_max.dev_attr.attr,
488 &sensor_dev_attr_in4_alarm.dev_attr.attr,
489 &sensor_dev_attr_in5_input.dev_attr.attr,
490 &sensor_dev_attr_in5_min.dev_attr.attr,
491 &sensor_dev_attr_in5_max.dev_attr.attr,
492 &sensor_dev_attr_in5_alarm.dev_attr.attr,
493 &sensor_dev_attr_in6_input.dev_attr.attr,
494 &sensor_dev_attr_in6_min.dev_attr.attr,
495 &sensor_dev_attr_in6_max.dev_attr.attr,
496 &sensor_dev_attr_in6_alarm.dev_attr.attr,
497 &dev_attr_temp1_input.attr,
498 &dev_attr_temp1_max.attr,
499 &dev_attr_temp1_max_hyst.attr,
500 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
501 &sensor_dev_attr_fan1_input.dev_attr.attr,
502 &sensor_dev_attr_fan1_min.dev_attr.attr,
503 &sensor_dev_attr_fan1_div.dev_attr.attr,
504 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
505 &sensor_dev_attr_fan2_input.dev_attr.attr,
506 &sensor_dev_attr_fan2_min.dev_attr.attr,
507 &sensor_dev_attr_fan2_div.dev_attr.attr,
508 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
509 &sensor_dev_attr_fan3_input.dev_attr.attr,
510 &sensor_dev_attr_fan3_min.dev_attr.attr,
511 &sensor_dev_attr_fan3_div.dev_attr.attr,
512 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
513 &dev_attr_alarms.attr,
514 &dev_attr_cpu0_vid.attr,
520 .attrs = lm78_attributes,
531 static unsigned short isa_address = 0x290;
546 static struct lm78_data *lm78_data_if_isa(
void)
548 return pdev ? platform_get_drvdata(pdev) :
NULL;
559 isa = platform_get_drvdata(pdev);
570 for (i = 0x2b; i <= 0x3d; i++) {
571 if (lm78_read_value(isa, i) !=
578 for (i = 0x43; i <= 0x46; i++) {
579 if (lm78_read_value(isa, i) !=
588 static int lm78_alias_detect(
struct i2c_client *client,
u8 chipid)
593 static struct lm78_data *lm78_data_if_isa(
void)
599 static int lm78_i2c_detect(
struct i2c_client *client,
603 struct lm78_data *isa = lm78_data_if_isa();
604 const char *client_name;
625 if (i == 0xa3 || i == 0x5c)
630 if (i == 0x00 || i == 0x20
632 client_name =
"lm78";
633 else if ((i & 0xfe) == 0xc0)
634 client_name =
"lm79";
638 if (lm78_alias_detect(client, i)) {
639 dev_dbg(&adapter->
dev,
"Device at 0x%02x appears to "
640 "be the same as ISA device\n", address);
657 static int lm78_i2c_probe(
struct i2c_client *client,
667 i2c_set_clientdata(client, data);
669 data->
type =
id->driver_data;
672 lm78_init_device(data);
692 static int lm78_i2c_remove(
struct i2c_client *client)
694 struct lm78_data *data = i2c_get_clientdata(client);
714 .probe = lm78_i2c_probe,
715 .remove = lm78_i2c_remove,
716 .id_table = lm78_i2c_id,
717 .detect = lm78_i2c_detect,
718 .address_list = normal_i2c,
728 static int lm78_read_value(
struct lm78_data *data,
u8 reg)
761 static void lm78_init_device(
struct lm78_data *data)
768 if ((config & 0x09) != 0x01)
770 (config & 0xf7) | 0x01);
773 for (i = 0; i < 3; i++) {
774 data->
fan_min[
i] = lm78_read_value(data,
791 dev_dbg(dev,
"Starting lm78 update\n");
793 for (i = 0; i <= 6; i++) {
801 for (i = 0; i < 3; i++) {
813 data->
vid = i & 0x0f;
820 data->
fan_div[0] = (i >> 4) & 0x03;
854 platform_set_drvdata(pdev, data);
865 lm78_init_device(data);
870 goto exit_remove_files;
873 goto exit_remove_files;
878 goto exit_remove_files;
891 struct lm78_data *data = platform_get_drvdata(pdev);
905 .probe = lm78_isa_probe,
910 static int __init lm78_isa_found(
unsigned short address)
912 int val, save, found = 0;
920 for (port = address; port < address +
LM78_EXTENT; port++) {
922 pr_debug(
"Failed to request port 0x%x\n", port);
927 #define REALLY_SLOW_IO
932 val =
inb_p(address + 1);
933 if (
inb_p(address + 2) != val
934 ||
inb_p(address + 3) != val
935 ||
inb_p(address + 7) != val)
937 #undef REALLY_SLOW_IO
960 if (val < 0x03 || val > 0x77)
970 if (val == 0xa3 || val == 0x5c)
982 if (val == 0x00 || val == 0x20
984 || (val & 0xfe) == 0xc0)
988 pr_info(
"Found an %s chip at %#x\n",
989 val & 0x80 ?
"LM79" :
"LM78", (
int)address);
992 for (port--; port >=
address; port--)
997 static int __init lm78_isa_device_add(
unsigned short address)
1001 .end = address + LM78_EXTENT - 1,
1010 pr_err(
"Device allocation failed\n");
1016 pr_err(
"Device resource addition failed (%d)\n", err);
1017 goto exit_device_put;
1022 pr_err(
"Device addition failed (%d)\n", err);
1023 goto exit_device_put;
1035 static int __init lm78_isa_register(
void)
1039 if (lm78_isa_found(isa_address)) {
1045 res = lm78_isa_device_add(isa_address);
1047 goto exit_unreg_isa_driver;
1052 exit_unreg_isa_driver:
1058 static void lm78_isa_unregister(
void)
1067 static int __init lm78_isa_register(
void)
1072 static void lm78_isa_unregister(
void)
1077 static int __init sm_lm78_init(
void)
1085 res = lm78_isa_register();
1089 res = i2c_add_driver(&lm78_driver);
1091 goto exit_unreg_isa_device;
1095 exit_unreg_isa_device:
1096 lm78_isa_unregister();
1101 static void __exit sm_lm78_exit(
void)
1103 lm78_isa_unregister();