1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * adm9240.c Part of lm_sensors, Linux kernel modules for hardware
6 * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl>
7 * Philip Edelbrock <phil@netroedge.com>
8 * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl>
9 * Copyright (C) 2005 Grant Coady <gcoady.lk@gmail.com> with valuable
10 * guidance from Jean Delvare
12 * Driver supports Analog Devices ADM9240
13 * Dallas Semiconductor DS1780
14 * National Semiconductor LM81
16 * ADM9240 is the reference, DS1780 and LM81 are register compatibles
18 * Voltage Six inputs are scaled by chip, VID also reported
19 * Temperature Chip temperature to 0.5'C, maximum and max_hysteris
20 * Fans 2 fans, low speed alarm, automatic fan clock divider
21 * Alarms 16-bit map of active alarms
22 * Analog Out 0..1250 mV output
24 * Chassis Intrusion: clear CI latch with 'echo 0 > intrusion0_alarm'
26 * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
28 * LM81 extended temp reading not implemented
31 #include <linux/bits.h>
32 #include <linux/init.h>
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/i2c.h>
36 #include <linux/hwmon-sysfs.h>
37 #include <linux/hwmon.h>
38 #include <linux/hwmon-vid.h>
39 #include <linux/err.h>
40 #include <linux/mutex.h>
41 #include <linux/regmap.h>
43 /* Addresses to scan */
44 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
47 enum chips { adm9240, ds1780, lm81 };
49 /* ADM9240 registers */
50 #define ADM9240_REG_MAN_ID 0x3e
51 #define ADM9240_REG_DIE_REV 0x3f
52 #define ADM9240_REG_CONFIG 0x40
54 #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
55 #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
56 #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
57 #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
58 #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
59 #define ADM9240_REG_INT(nr) (0x41 + (nr))
60 #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
61 #define ADM9240_REG_TEMP 0x27
62 #define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */
63 #define ADM9240_REG_ANALOG_OUT 0x19
64 #define ADM9240_REG_CHASSIS_CLEAR 0x46
65 #define ADM9240_REG_VID_FAN_DIV 0x47
66 #define ADM9240_REG_I2C_ADDR 0x48
67 #define ADM9240_REG_VID4 0x49
68 #define ADM9240_REG_TEMP_CONF 0x4b
70 /* generalised scaling with integer rounding */
71 static inline int SCALE(long val, int mul, int div)
74 return (val * mul - div / 2) / div;
76 return (val * mul + div / 2) / div;
79 /* adm9240 internally scales voltage measurements */
80 static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
82 static inline unsigned int IN_FROM_REG(u8 reg, int n)
84 return SCALE(reg, nom_mv[n], 192);
87 static inline u8 IN_TO_REG(unsigned long val, int n)
89 val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
90 return SCALE(val, 192, nom_mv[n]);
93 /* temperature range: -40..125, 127 disables temperature alarm */
94 static inline s8 TEMP_TO_REG(long val)
96 val = clamp_val(val, -40000, 127000);
97 return SCALE(val, 1, 1000);
100 /* two fans, each with low fan speed limit */
101 static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
103 if (!reg) /* error */
109 return SCALE(1350000, 1, reg * div);
112 /* analog out 0..1250mV */
113 static inline u8 AOUT_TO_REG(unsigned long val)
115 val = clamp_val(val, 0, 1250);
116 return SCALE(val, 255, 1250);
119 static inline unsigned int AOUT_FROM_REG(u8 reg)
121 return SCALE(reg, 1250, 255);
124 /* per client data */
125 struct adm9240_data {
127 struct regmap *regmap;
128 struct mutex update_lock;
130 u8 fan_div[2]; /* rw fan1_div, read-only accessor */
131 u8 vrm; /* -- vrm set on startup, no accessor */
134 /* write new fan div, callers must hold data->update_lock */
135 static int adm9240_write_fan_div(struct adm9240_data *data, int channel, u8 fan_div)
137 unsigned int reg, old, shift = (channel + 2) * 2;
140 err = regmap_read(data->regmap, ADM9240_REG_VID_FAN_DIV, ®);
143 old = (reg >> shift) & 3;
144 reg &= ~(3 << shift);
145 reg |= (fan_div << shift);
146 err = regmap_write(data->regmap, ADM9240_REG_VID_FAN_DIV, reg);
150 "fan%d clock divider changed from %lu to %lu\n",
151 channel + 1, BIT(old), BIT(fan_div));
157 * set fan speed low limit:
159 * - value is zero: disable fan speed low limit alarm
161 * - value is below fan speed measurement range: enable fan speed low
162 * limit alarm to be asserted while fan speed too slow to measure
164 * - otherwise: select fan clock divider to suit fan speed low limit,
165 * measurement code may adjust registers to ensure fan speed reading
167 static int adm9240_fan_min_write(struct adm9240_data *data, int channel, long val)
173 mutex_lock(&data->update_lock);
177 new_div = data->fan_div[channel];
179 dev_dbg(data->dev, "fan%u low limit set disabled\n", channel + 1);
180 } else if (val < 1350000 / (8 * 254)) {
184 dev_dbg(data->dev, "fan%u low limit set minimum %u\n",
185 channel + 1, FAN_FROM_REG(254, BIT(new_div)));
187 unsigned int new_min = 1350000 / val;
190 while (new_min > 192 && new_div < 3) {
194 if (!new_min) /* keep > 0 */
199 dev_dbg(data->dev, "fan%u low limit set fan speed %u\n",
200 channel + 1, FAN_FROM_REG(new_min, BIT(new_div)));
203 if (new_div != data->fan_div[channel]) {
204 data->fan_div[channel] = new_div;
205 adm9240_write_fan_div(data, channel, new_div);
207 err = regmap_write(data->regmap, ADM9240_REG_FAN_MIN(channel), fan_min);
209 mutex_unlock(&data->update_lock);
214 static ssize_t cpu0_vid_show(struct device *dev,
215 struct device_attribute *attr, char *buf)
217 struct adm9240_data *data = dev_get_drvdata(dev);
222 err = regmap_read(data->regmap, ADM9240_REG_VID_FAN_DIV, ®val);
226 err = regmap_read(data->regmap, ADM9240_REG_VID4, ®val);
229 vid |= (regval & 1) << 4;
230 return sprintf(buf, "%d\n", vid_from_reg(vid, data->vrm));
232 static DEVICE_ATTR_RO(cpu0_vid);
234 static ssize_t aout_output_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct adm9240_data *data = dev_get_drvdata(dev);
241 err = regmap_read(data->regmap, ADM9240_REG_ANALOG_OUT, ®val);
245 return sprintf(buf, "%d\n", AOUT_FROM_REG(regval));
248 static ssize_t aout_output_store(struct device *dev,
249 struct device_attribute *attr,
250 const char *buf, size_t count)
252 struct adm9240_data *data = dev_get_drvdata(dev);
256 err = kstrtol(buf, 10, &val);
260 err = regmap_write(data->regmap, ADM9240_REG_ANALOG_OUT, AOUT_TO_REG(val));
261 return err < 0 ? err : count;
263 static DEVICE_ATTR_RW(aout_output);
265 static struct attribute *adm9240_attrs[] = {
266 &dev_attr_aout_output.attr,
267 &dev_attr_cpu0_vid.attr,
271 ATTRIBUTE_GROUPS(adm9240);
273 /*** sensor chip detect and driver install ***/
275 /* Return 0 if detection is successful, -ENODEV otherwise */
276 static int adm9240_detect(struct i2c_client *new_client,
277 struct i2c_board_info *info)
279 struct i2c_adapter *adapter = new_client->adapter;
280 const char *name = "";
281 int address = new_client->addr;
284 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
287 /* verify chip: reg address should match i2c address */
288 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR) != address)
291 /* check known chip manufacturer */
292 man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID);
295 else if (man_id == 0xda)
297 else if (man_id == 0x01)
302 /* successful detect, print chip info */
303 die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV);
304 dev_info(&adapter->dev, "found %s revision %u\n",
305 man_id == 0x23 ? "ADM9240" :
306 man_id == 0xda ? "DS1780" : "LM81", die_rev);
308 strscpy(info->type, name, I2C_NAME_SIZE);
313 static int adm9240_init_client(struct adm9240_data *data)
319 err = regmap_raw_read(data->regmap, ADM9240_REG_CONFIG, &conf, 1);
322 err = regmap_raw_read(data->regmap, ADM9240_REG_TEMP_CONF, &mode, 1);
327 data->vrm = vid_which_vrm(); /* need this to report vid as mV */
329 dev_info(data->dev, "Using VRM: %d.%d\n", data->vrm / 10,
332 if (conf & 1) { /* measurement cycle running: report state */
334 dev_info(data->dev, "status: config 0x%02x mode %u\n",
337 } else { /* cold start: open limits before starting chip */
340 for (i = 0; i < 6; i++) {
341 err = regmap_write(data->regmap,
342 ADM9240_REG_IN_MIN(i), 0);
345 err = regmap_write(data->regmap,
346 ADM9240_REG_IN_MAX(i), 255);
350 for (i = 0; i < 2; i++) {
351 err = regmap_write(data->regmap,
352 ADM9240_REG_FAN_MIN(i), 255);
356 for (i = 0; i < 2; i++) {
357 err = regmap_write(data->regmap,
358 ADM9240_REG_TEMP_MAX(i), 127);
363 /* start measurement cycle */
364 err = regmap_write(data->regmap, ADM9240_REG_CONFIG, 1);
369 "cold start: config was 0x%02x mode %u\n", conf, mode);
373 err = regmap_read(data->regmap, ADM9240_REG_VID_FAN_DIV, ®val);
376 data->fan_div[0] = (regval >> 4) & 3;
377 data->fan_div[1] = (regval >> 6) & 3;
381 static int adm9240_chip_read(struct device *dev, u32 attr, long *val)
383 struct adm9240_data *data = dev_get_drvdata(dev);
388 case hwmon_chip_alarms:
389 err = regmap_bulk_read(data->regmap, ADM9240_REG_INT(0), ®s, 2);
392 *val = regs[0] | regs[1] << 8;
400 static int adm9240_intrusion_read(struct device *dev, u32 attr, long *val)
402 struct adm9240_data *data = dev_get_drvdata(dev);
407 case hwmon_intrusion_alarm:
408 err = regmap_read(data->regmap, ADM9240_REG_INT(1), ®val);
411 *val = !!(regval & BIT(4));
419 static int adm9240_intrusion_write(struct device *dev, u32 attr, long val)
421 struct adm9240_data *data = dev_get_drvdata(dev);
425 case hwmon_intrusion_alarm:
428 err = regmap_write(data->regmap, ADM9240_REG_CHASSIS_CLEAR, 0x80);
431 dev_dbg(data->dev, "chassis intrusion latch cleared\n");
439 static int adm9240_in_read(struct device *dev, u32 attr, int channel, long *val)
441 struct adm9240_data *data = dev_get_drvdata(dev);
448 reg = ADM9240_REG_IN(channel);
451 reg = ADM9240_REG_IN_MIN(channel);
454 reg = ADM9240_REG_IN_MAX(channel);
458 reg = ADM9240_REG_INT(0);
460 reg = ADM9240_REG_INT(1);
463 err = regmap_read(data->regmap, reg, ®val);
466 *val = !!(regval & BIT(channel));
471 err = regmap_read(data->regmap, reg, ®val);
474 *val = IN_FROM_REG(regval, channel);
478 static int adm9240_in_write(struct device *dev, u32 attr, int channel, long val)
480 struct adm9240_data *data = dev_get_drvdata(dev);
485 reg = ADM9240_REG_IN_MIN(channel);
488 reg = ADM9240_REG_IN_MAX(channel);
493 return regmap_write(data->regmap, reg, IN_TO_REG(val, channel));
496 static int adm9240_fan_read(struct device *dev, u32 attr, int channel, long *val)
498 struct adm9240_data *data = dev_get_drvdata(dev);
503 case hwmon_fan_input:
504 mutex_lock(&data->update_lock);
505 err = regmap_read(data->regmap, ADM9240_REG_FAN(channel), ®val);
507 mutex_unlock(&data->update_lock);
510 if (regval == 255 && data->fan_div[channel] < 3) {
511 /* adjust fan clock divider on overflow */
512 err = adm9240_write_fan_div(data, channel,
513 ++data->fan_div[channel]);
515 mutex_unlock(&data->update_lock);
519 *val = FAN_FROM_REG(regval, BIT(data->fan_div[channel]));
520 mutex_unlock(&data->update_lock);
523 *val = BIT(data->fan_div[channel]);
526 err = regmap_read(data->regmap, ADM9240_REG_FAN_MIN(channel), ®val);
529 *val = FAN_FROM_REG(regval, BIT(data->fan_div[channel]));
531 case hwmon_fan_alarm:
532 err = regmap_read(data->regmap, ADM9240_REG_INT(0), ®val);
535 *val = !!(regval & BIT(channel + 6));
543 static int adm9240_fan_write(struct device *dev, u32 attr, int channel, long val)
545 struct adm9240_data *data = dev_get_drvdata(dev);
550 err = adm9240_fan_min_write(data, channel, val);
560 static int adm9240_temp_read(struct device *dev, u32 attr, int channel, long *val)
562 struct adm9240_data *data = dev_get_drvdata(dev);
567 case hwmon_temp_input:
568 err = regmap_read(data->regmap, ADM9240_REG_TEMP, ®val);
572 err = regmap_read(data->regmap, ADM9240_REG_TEMP_CONF, ®val);
576 *val = sign_extend32(temp, 8) * 500;
579 err = regmap_read(data->regmap, ADM9240_REG_TEMP_MAX(0), ®val);
582 *val = (s8)regval * 1000;
584 case hwmon_temp_max_hyst:
585 err = regmap_read(data->regmap, ADM9240_REG_TEMP_MAX(1), ®val);
588 *val = (s8)regval * 1000;
590 case hwmon_temp_alarm:
591 err = regmap_read(data->regmap, ADM9240_REG_INT(0), ®val);
594 *val = !!(regval & BIT(4));
602 static int adm9240_temp_write(struct device *dev, u32 attr, int channel, long val)
604 struct adm9240_data *data = dev_get_drvdata(dev);
609 reg = ADM9240_REG_TEMP_MAX(0);
611 case hwmon_temp_max_hyst:
612 reg = ADM9240_REG_TEMP_MAX(1);
617 return regmap_write(data->regmap, reg, TEMP_TO_REG(val));
620 static int adm9240_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
621 int channel, long *val)
625 return adm9240_chip_read(dev, attr, val);
626 case hwmon_intrusion:
627 return adm9240_intrusion_read(dev, attr, val);
629 return adm9240_in_read(dev, attr, channel, val);
631 return adm9240_fan_read(dev, attr, channel, val);
633 return adm9240_temp_read(dev, attr, channel, val);
639 static int adm9240_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
640 int channel, long val)
643 case hwmon_intrusion:
644 return adm9240_intrusion_write(dev, attr, val);
646 return adm9240_in_write(dev, attr, channel, val);
648 return adm9240_fan_write(dev, attr, channel, val);
650 return adm9240_temp_write(dev, attr, channel, val);
656 static umode_t adm9240_is_visible(const void *_data, enum hwmon_sensor_types type,
657 u32 attr, int channel)
664 case hwmon_chip_alarms:
671 case hwmon_intrusion:
673 case hwmon_intrusion_alarm:
683 case hwmon_temp_alarm:
687 case hwmon_temp_max_hyst:
696 case hwmon_fan_input:
698 case hwmon_fan_alarm:
728 static const struct hwmon_ops adm9240_hwmon_ops = {
729 .is_visible = adm9240_is_visible,
730 .read = adm9240_read,
731 .write = adm9240_write,
734 static const struct hwmon_channel_info *adm9240_info[] = {
735 HWMON_CHANNEL_INFO(chip, HWMON_C_ALARMS),
736 HWMON_CHANNEL_INFO(intrusion, HWMON_INTRUSION_ALARM),
737 HWMON_CHANNEL_INFO(temp,
738 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST | HWMON_T_ALARM),
739 HWMON_CHANNEL_INFO(in,
740 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
741 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
742 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
743 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
744 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
745 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM),
746 HWMON_CHANNEL_INFO(fan,
747 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_DIV | HWMON_F_ALARM,
748 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_DIV | HWMON_F_ALARM),
752 static const struct hwmon_chip_info adm9240_chip_info = {
753 .ops = &adm9240_hwmon_ops,
754 .info = adm9240_info,
757 static bool adm9240_volatile_reg(struct device *dev, unsigned int reg)
760 case ADM9240_REG_IN(0) ... ADM9240_REG_IN(5):
761 case ADM9240_REG_FAN(0) ... ADM9240_REG_FAN(1):
762 case ADM9240_REG_INT(0) ... ADM9240_REG_INT(1):
763 case ADM9240_REG_TEMP:
764 case ADM9240_REG_TEMP_CONF:
765 case ADM9240_REG_VID_FAN_DIV:
766 case ADM9240_REG_VID4:
767 case ADM9240_REG_ANALOG_OUT:
774 static const struct regmap_config adm9240_regmap_config = {
777 .use_single_read = true,
778 .use_single_write = true,
779 .volatile_reg = adm9240_volatile_reg,
782 static int adm9240_probe(struct i2c_client *client)
784 struct device *dev = &client->dev;
785 struct device *hwmon_dev;
786 struct adm9240_data *data;
789 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
794 mutex_init(&data->update_lock);
795 data->regmap = devm_regmap_init_i2c(client, &adm9240_regmap_config);
796 if (IS_ERR(data->regmap))
797 return PTR_ERR(data->regmap);
799 err = adm9240_init_client(data);
803 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
806 return PTR_ERR_OR_ZERO(hwmon_dev);
809 static const struct i2c_device_id adm9240_id[] = {
810 { "adm9240", adm9240 },
811 { "ds1780", ds1780 },
815 MODULE_DEVICE_TABLE(i2c, adm9240_id);
817 static struct i2c_driver adm9240_driver = {
818 .class = I2C_CLASS_HWMON,
822 .probe_new = adm9240_probe,
823 .id_table = adm9240_id,
824 .detect = adm9240_detect,
825 .address_list = normal_i2c,
828 module_i2c_driver(adm9240_driver);
830 MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
831 "Grant Coady <gcoady.lk@gmail.com> and others");
832 MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
833 MODULE_LICENSE("GPL");