1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hardware monitoring driver for Maxim MAX6620
5 * Originally from L. Grunenberg.
6 * (C) 2012 by L. Grunenberg <contact@lgrunenberg.de>
8 * Copyright (c) 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
10 * based on code written by :
11 * 2007 by Hans J. Koch <hjk@hansjkoch.de>
12 * John Morris <john.morris@spirentcom.com>
13 * Copyright (c) 2003 Spirent Communications
14 * and Claus Gindhart <claus.gindhart@kontron.com>
16 * This module has only been tested with the MAX6620 chip.
18 * The datasheet was last seen at:
20 * http://pdfserv.maxim-ic.com/en/ds/MAX6620.pdf
24 #include <linux/bits.h>
25 #include <linux/err.h>
26 #include <linux/hwmon.h>
27 #include <linux/i2c.h>
28 #include <linux/init.h>
29 #include <linux/jiffies.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
37 #define MAX6620_REG_CONFIG 0x00
38 #define MAX6620_REG_FAULT 0x01
39 #define MAX6620_REG_CONF_FAN0 0x02
40 #define MAX6620_REG_CONF_FAN1 0x03
41 #define MAX6620_REG_CONF_FAN2 0x04
42 #define MAX6620_REG_CONF_FAN3 0x05
43 #define MAX6620_REG_DYN_FAN0 0x06
44 #define MAX6620_REG_DYN_FAN1 0x07
45 #define MAX6620_REG_DYN_FAN2 0x08
46 #define MAX6620_REG_DYN_FAN3 0x09
47 #define MAX6620_REG_TACH0 0x10
48 #define MAX6620_REG_TACH1 0x12
49 #define MAX6620_REG_TACH2 0x14
50 #define MAX6620_REG_TACH3 0x16
51 #define MAX6620_REG_VOLT0 0x18
52 #define MAX6620_REG_VOLT1 0x1A
53 #define MAX6620_REG_VOLT2 0x1C
54 #define MAX6620_REG_VOLT3 0x1E
55 #define MAX6620_REG_TAR0 0x20
56 #define MAX6620_REG_TAR1 0x22
57 #define MAX6620_REG_TAR2 0x24
58 #define MAX6620_REG_TAR3 0x26
59 #define MAX6620_REG_DAC0 0x28
60 #define MAX6620_REG_DAC1 0x2A
61 #define MAX6620_REG_DAC2 0x2C
62 #define MAX6620_REG_DAC3 0x2E
65 * Config register bits
68 #define MAX6620_CFG_RUN BIT(7)
69 #define MAX6620_CFG_POR BIT(6)
70 #define MAX6620_CFG_TIMEOUT BIT(5)
71 #define MAX6620_CFG_FULLFAN BIT(4)
72 #define MAX6620_CFG_OSC BIT(3)
73 #define MAX6620_CFG_WD_MASK (BIT(2) | BIT(1))
74 #define MAX6620_CFG_WD_2 BIT(1)
75 #define MAX6620_CFG_WD_6 BIT(2)
76 #define MAX6620_CFG_WD10 (BIT(2) | BIT(1))
77 #define MAX6620_CFG_WD BIT(0)
80 * Failure status register bits
83 #define MAX6620_FAIL_TACH0 BIT(4)
84 #define MAX6620_FAIL_TACH1 BIT(5)
85 #define MAX6620_FAIL_TACH2 BIT(6)
86 #define MAX6620_FAIL_TACH3 BIT(7)
87 #define MAX6620_FAIL_MASK0 BIT(0)
88 #define MAX6620_FAIL_MASK1 BIT(1)
89 #define MAX6620_FAIL_MASK2 BIT(2)
90 #define MAX6620_FAIL_MASK3 BIT(3)
92 #define MAX6620_CLOCK_FREQ 8192 /* Clock frequency in Hz */
93 #define MAX6620_PULSE_PER_REV 2 /* Tachometer pulses per revolution */
95 /* Minimum and maximum values of the FAN-RPM */
96 #define FAN_RPM_MIN 240
97 #define FAN_RPM_MAX 30000
99 static const u8 config_reg
[] = {
100 MAX6620_REG_CONF_FAN0
,
101 MAX6620_REG_CONF_FAN1
,
102 MAX6620_REG_CONF_FAN2
,
103 MAX6620_REG_CONF_FAN3
,
106 static const u8 dyn_reg
[] = {
107 MAX6620_REG_DYN_FAN0
,
108 MAX6620_REG_DYN_FAN1
,
109 MAX6620_REG_DYN_FAN2
,
110 MAX6620_REG_DYN_FAN3
,
113 static const u8 tach_reg
[] = {
120 static const u8 target_reg
[] = {
128 * Client data (each client gets its own)
131 struct max6620_data
{
132 struct i2c_client
*client
;
133 struct mutex update_lock
;
134 bool valid
; /* false until following fields are valid */
135 unsigned long last_updated
; /* in jiffies */
137 /* register values */
145 static u8
max6620_fan_div_from_reg(u8 val
)
147 return BIT((val
& 0xE0) >> 5);
150 static u16
max6620_fan_rpm_to_tach(u8 div
, int rpm
)
152 return (60 * div
* MAX6620_CLOCK_FREQ
) / (rpm
* MAX6620_PULSE_PER_REV
);
155 static int max6620_fan_tach_to_rpm(u8 div
, u16 tach
)
157 return (60 * div
* MAX6620_CLOCK_FREQ
) / (tach
* MAX6620_PULSE_PER_REV
);
160 static int max6620_update_device(struct device
*dev
)
162 struct max6620_data
*data
= dev_get_drvdata(dev
);
163 struct i2c_client
*client
= data
->client
;
167 mutex_lock(&data
->update_lock
);
169 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
170 for (i
= 0; i
< 4; i
++) {
171 ret
= i2c_smbus_read_byte_data(client
, config_reg
[i
]);
174 data
->fancfg
[i
] = ret
;
176 ret
= i2c_smbus_read_byte_data(client
, dyn_reg
[i
]);
179 data
->fandyn
[i
] = ret
;
181 ret
= i2c_smbus_read_byte_data(client
, tach_reg
[i
]);
184 data
->tach
[i
] = (ret
<< 3) & 0x7f8;
185 ret
= i2c_smbus_read_byte_data(client
, tach_reg
[i
] + 1);
188 data
->tach
[i
] |= (ret
>> 5) & 0x7;
190 ret
= i2c_smbus_read_byte_data(client
, target_reg
[i
]);
193 data
->target
[i
] = (ret
<< 3) & 0x7f8;
194 ret
= i2c_smbus_read_byte_data(client
, target_reg
[i
] + 1);
197 data
->target
[i
] |= (ret
>> 5) & 0x7;
201 * Alarms are cleared on read in case the condition that
202 * caused the alarm is removed. Keep the value latched here
203 * for providing the register through different alarm files.
205 ret
= i2c_smbus_read_byte_data(client
, MAX6620_REG_FAULT
);
208 data
->fault
|= (ret
>> 4) & (ret
& 0x0F);
210 data
->last_updated
= jiffies
;
215 mutex_unlock(&data
->update_lock
);
220 max6620_is_visible(const void *data
, enum hwmon_sensor_types type
, u32 attr
,
226 case hwmon_fan_alarm
:
227 case hwmon_fan_input
:
230 case hwmon_fan_target
:
244 max6620_read(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
245 int channel
, long *val
)
247 struct max6620_data
*data
;
248 struct i2c_client
*client
;
254 ret
= max6620_update_device(dev
);
257 data
= dev_get_drvdata(dev
);
258 client
= data
->client
;
263 case hwmon_fan_alarm
:
264 mutex_lock(&data
->update_lock
);
265 *val
= !!(data
->fault
& BIT(channel
));
267 /* Setting TACH count to re-enable fan fault detection */
269 val1
= (data
->target
[channel
] >> 3) & 0xff;
270 val2
= (data
->target
[channel
] << 5) & 0xe0;
271 ret
= i2c_smbus_write_byte_data(client
,
272 target_reg
[channel
], val1
);
274 mutex_unlock(&data
->update_lock
);
277 ret
= i2c_smbus_write_byte_data(client
,
278 target_reg
[channel
] + 1, val2
);
280 mutex_unlock(&data
->update_lock
);
284 data
->fault
&= ~BIT(channel
);
286 mutex_unlock(&data
->update_lock
);
290 *val
= max6620_fan_div_from_reg(data
->fandyn
[channel
]);
292 case hwmon_fan_input
:
293 if (data
->tach
[channel
] == 0) {
296 div
= max6620_fan_div_from_reg(data
->fandyn
[channel
]);
297 *val
= max6620_fan_tach_to_rpm(div
, data
->tach
[channel
]);
300 case hwmon_fan_target
:
301 if (data
->target
[channel
] == 0) {
304 div
= max6620_fan_div_from_reg(data
->fandyn
[channel
]);
305 *val
= max6620_fan_tach_to_rpm(div
, data
->target
[channel
]);
321 max6620_write(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
322 int channel
, long val
)
324 struct max6620_data
*data
;
325 struct i2c_client
*client
;
332 ret
= max6620_update_device(dev
);
335 data
= dev_get_drvdata(dev
);
336 client
= data
->client
;
337 mutex_lock(&data
->update_lock
);
366 data
->fandyn
[channel
] &= 0x1F;
367 data
->fandyn
[channel
] |= div
<< 5;
368 ret
= i2c_smbus_write_byte_data(client
, dyn_reg
[channel
],
369 data
->fandyn
[channel
]);
371 case hwmon_fan_target
:
372 val
= clamp_val(val
, FAN_RPM_MIN
, FAN_RPM_MAX
);
373 div
= max6620_fan_div_from_reg(data
->fandyn
[channel
]);
374 tach
= max6620_fan_rpm_to_tach(div
, val
);
375 val1
= (tach
>> 3) & 0xff;
376 val2
= (tach
<< 5) & 0xe0;
377 ret
= i2c_smbus_write_byte_data(client
, target_reg
[channel
], val1
);
380 ret
= i2c_smbus_write_byte_data(client
, target_reg
[channel
] + 1, val2
);
384 /* Setting TACH count re-enables fan fault detection */
385 data
->fault
&= ~BIT(channel
);
400 mutex_unlock(&data
->update_lock
);
404 static const struct hwmon_channel_info
* const max6620_info
[] = {
405 HWMON_CHANNEL_INFO(fan
,
406 HWMON_F_INPUT
| HWMON_F_DIV
| HWMON_F_TARGET
| HWMON_F_ALARM
,
407 HWMON_F_INPUT
| HWMON_F_DIV
| HWMON_F_TARGET
| HWMON_F_ALARM
,
408 HWMON_F_INPUT
| HWMON_F_DIV
| HWMON_F_TARGET
| HWMON_F_ALARM
,
409 HWMON_F_INPUT
| HWMON_F_DIV
| HWMON_F_TARGET
| HWMON_F_ALARM
),
413 static const struct hwmon_ops max6620_hwmon_ops
= {
414 .read
= max6620_read
,
415 .write
= max6620_write
,
416 .is_visible
= max6620_is_visible
,
419 static const struct hwmon_chip_info max6620_chip_info
= {
420 .ops
= &max6620_hwmon_ops
,
421 .info
= max6620_info
,
424 static int max6620_init_client(struct max6620_data
*data
)
426 struct i2c_client
*client
= data
->client
;
432 config
= i2c_smbus_read_byte_data(client
, MAX6620_REG_CONFIG
);
434 dev_err(&client
->dev
, "Error reading config, aborting.\n");
439 * Set bit 4, disable other fans from going full speed on a fail
442 err
= i2c_smbus_write_byte_data(client
, MAX6620_REG_CONFIG
, config
| 0x10);
444 dev_err(&client
->dev
, "Config write error, aborting.\n");
448 for (i
= 0; i
< 4; i
++) {
449 reg
= i2c_smbus_read_byte_data(client
, config_reg
[i
]);
452 data
->fancfg
[i
] = reg
;
454 /* Enable RPM mode */
455 data
->fancfg
[i
] |= 0xa8;
456 err
= i2c_smbus_write_byte_data(client
, config_reg
[i
], data
->fancfg
[i
]);
460 /* 2 counts (001) and Rate change 100 (0.125 secs) */
461 data
->fandyn
[i
] = 0x30;
462 err
= i2c_smbus_write_byte_data(client
, dyn_reg
[i
], data
->fandyn
[i
]);
469 static int max6620_probe(struct i2c_client
*client
)
471 struct device
*dev
= &client
->dev
;
472 struct max6620_data
*data
;
473 struct device
*hwmon_dev
;
476 data
= devm_kzalloc(dev
, sizeof(struct max6620_data
), GFP_KERNEL
);
480 data
->client
= client
;
481 mutex_init(&data
->update_lock
);
483 err
= max6620_init_client(data
);
487 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
492 return PTR_ERR_OR_ZERO(hwmon_dev
);
495 static const struct i2c_device_id max6620_id
[] = {
499 MODULE_DEVICE_TABLE(i2c
, max6620_id
);
501 static struct i2c_driver max6620_driver
= {
502 .class = I2C_CLASS_HWMON
,
506 .probe
= max6620_probe
,
507 .id_table
= max6620_id
,
510 module_i2c_driver(max6620_driver
);
512 MODULE_AUTHOR("Lucas Grunenberg");
513 MODULE_DESCRIPTION("MAX6620 sensor driver");
514 MODULE_LICENSE("GPL");