1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * max31790.c - Part of lm_sensors, Linux kernel modules for hardware
6 * (C) 2015 by Il Han <corone.il.han@gmail.com>
10 #include <linux/hwmon.h>
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/jiffies.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
17 /* MAX31790 registers */
18 #define MAX31790_REG_GLOBAL_CONFIG 0x00
19 #define MAX31790_REG_FAN_CONFIG(ch) (0x02 + (ch))
20 #define MAX31790_REG_FAN_DYNAMICS(ch) (0x08 + (ch))
21 #define MAX31790_REG_FAN_FAULT_STATUS2 0x10
22 #define MAX31790_REG_FAN_FAULT_STATUS1 0x11
23 #define MAX31790_REG_TACH_COUNT(ch) (0x18 + (ch) * 2)
24 #define MAX31790_REG_PWM_DUTY_CYCLE(ch) (0x30 + (ch) * 2)
25 #define MAX31790_REG_PWMOUT(ch) (0x40 + (ch) * 2)
26 #define MAX31790_REG_TARGET_COUNT(ch) (0x50 + (ch) * 2)
28 /* Fan Config register bits */
29 #define MAX31790_FAN_CFG_RPM_MODE 0x80
30 #define MAX31790_FAN_CFG_TACH_INPUT_EN 0x08
31 #define MAX31790_FAN_CFG_TACH_INPUT 0x01
33 /* Fan Dynamics register bits */
34 #define MAX31790_FAN_DYN_SR_SHIFT 5
35 #define MAX31790_FAN_DYN_SR_MASK 0xE0
36 #define SR_FROM_REG(reg) (((reg) & MAX31790_FAN_DYN_SR_MASK) \
37 >> MAX31790_FAN_DYN_SR_SHIFT)
39 #define FAN_RPM_MIN 120
40 #define FAN_RPM_MAX 7864320
42 #define RPM_FROM_REG(reg, sr) (((reg) >> 4) ? \
43 ((60 * (sr) * 8192) / ((reg) >> 4)) : \
45 #define RPM_TO_REG(rpm, sr) ((60 * (sr) * 8192) / ((rpm) * 2))
50 * Client data (each client gets its own)
52 struct max31790_data
{
53 struct i2c_client
*client
;
54 struct mutex update_lock
;
55 bool valid
; /* zero until following fields are valid */
56 unsigned long last_updated
; /* in jiffies */
59 u8 fan_config
[NR_CHANNEL
];
60 u8 fan_dynamics
[NR_CHANNEL
];
62 u16 tach
[NR_CHANNEL
* 2];
64 u16 target_count
[NR_CHANNEL
];
67 static struct max31790_data
*max31790_update_device(struct device
*dev
)
69 struct max31790_data
*data
= dev_get_drvdata(dev
);
70 struct i2c_client
*client
= data
->client
;
71 struct max31790_data
*ret
= data
;
75 mutex_lock(&data
->update_lock
);
77 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
78 rv
= i2c_smbus_read_byte_data(client
,
79 MAX31790_REG_FAN_FAULT_STATUS1
);
82 data
->fault_status
= rv
& 0x3F;
84 rv
= i2c_smbus_read_byte_data(client
,
85 MAX31790_REG_FAN_FAULT_STATUS2
);
88 data
->fault_status
|= (rv
& 0x3F) << 6;
90 for (i
= 0; i
< NR_CHANNEL
; i
++) {
91 rv
= i2c_smbus_read_word_swapped(client
,
92 MAX31790_REG_TACH_COUNT(i
));
97 if (data
->fan_config
[i
]
98 & MAX31790_FAN_CFG_TACH_INPUT
) {
99 rv
= i2c_smbus_read_word_swapped(client
,
100 MAX31790_REG_TACH_COUNT(NR_CHANNEL
104 data
->tach
[NR_CHANNEL
+ i
] = rv
;
106 rv
= i2c_smbus_read_word_swapped(client
,
107 MAX31790_REG_PWMOUT(i
));
112 rv
= i2c_smbus_read_word_swapped(client
,
113 MAX31790_REG_TARGET_COUNT(i
));
116 data
->target_count
[i
] = rv
;
120 data
->last_updated
= jiffies
;
130 mutex_unlock(&data
->update_lock
);
135 static const u8 tach_period
[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };
137 static u8
get_tach_period(u8 fan_dynamics
)
139 return tach_period
[SR_FROM_REG(fan_dynamics
)];
142 static u8
bits_for_tach_period(int rpm
)
162 static int max31790_read_fan(struct device
*dev
, u32 attr
, int channel
,
165 struct max31790_data
*data
= max31790_update_device(dev
);
169 return PTR_ERR(data
);
172 case hwmon_fan_input
:
173 sr
= get_tach_period(data
->fan_dynamics
[channel
]);
174 rpm
= RPM_FROM_REG(data
->tach
[channel
], sr
);
177 case hwmon_fan_target
:
178 sr
= get_tach_period(data
->fan_dynamics
[channel
]);
179 rpm
= RPM_FROM_REG(data
->target_count
[channel
], sr
);
182 case hwmon_fan_fault
:
183 *val
= !!(data
->fault_status
& (1 << channel
));
190 static int max31790_write_fan(struct device
*dev
, u32 attr
, int channel
,
193 struct max31790_data
*data
= dev_get_drvdata(dev
);
194 struct i2c_client
*client
= data
->client
;
200 mutex_lock(&data
->update_lock
);
203 case hwmon_fan_target
:
204 val
= clamp_val(val
, FAN_RPM_MIN
, FAN_RPM_MAX
);
205 bits
= bits_for_tach_period(val
);
206 data
->fan_dynamics
[channel
] =
207 ((data
->fan_dynamics
[channel
] &
208 ~MAX31790_FAN_DYN_SR_MASK
) |
209 (bits
<< MAX31790_FAN_DYN_SR_SHIFT
));
210 err
= i2c_smbus_write_byte_data(client
,
211 MAX31790_REG_FAN_DYNAMICS(channel
),
212 data
->fan_dynamics
[channel
]);
216 sr
= get_tach_period(data
->fan_dynamics
[channel
]);
217 target_count
= RPM_TO_REG(val
, sr
);
218 target_count
= clamp_val(target_count
, 0x1, 0x7FF);
220 data
->target_count
[channel
] = target_count
<< 5;
222 err
= i2c_smbus_write_word_swapped(client
,
223 MAX31790_REG_TARGET_COUNT(channel
),
224 data
->target_count
[channel
]);
231 mutex_unlock(&data
->update_lock
);
236 static umode_t
max31790_fan_is_visible(const void *_data
, u32 attr
, int channel
)
238 const struct max31790_data
*data
= _data
;
239 u8 fan_config
= data
->fan_config
[channel
% NR_CHANNEL
];
242 case hwmon_fan_input
:
243 case hwmon_fan_fault
:
244 if (channel
< NR_CHANNEL
||
245 (fan_config
& MAX31790_FAN_CFG_TACH_INPUT
))
248 case hwmon_fan_target
:
249 if (channel
< NR_CHANNEL
&&
250 !(fan_config
& MAX31790_FAN_CFG_TACH_INPUT
))
258 static int max31790_read_pwm(struct device
*dev
, u32 attr
, int channel
,
261 struct max31790_data
*data
= max31790_update_device(dev
);
265 return PTR_ERR(data
);
267 fan_config
= data
->fan_config
[channel
];
270 case hwmon_pwm_input
:
271 *val
= data
->pwm
[channel
] >> 8;
273 case hwmon_pwm_enable
:
274 if (fan_config
& MAX31790_FAN_CFG_RPM_MODE
)
276 else if (fan_config
& MAX31790_FAN_CFG_TACH_INPUT_EN
)
286 static int max31790_write_pwm(struct device
*dev
, u32 attr
, int channel
,
289 struct max31790_data
*data
= dev_get_drvdata(dev
);
290 struct i2c_client
*client
= data
->client
;
294 mutex_lock(&data
->update_lock
);
297 case hwmon_pwm_input
:
298 if (val
< 0 || val
> 255) {
302 data
->pwm
[channel
] = val
<< 8;
303 err
= i2c_smbus_write_word_swapped(client
,
304 MAX31790_REG_PWMOUT(channel
),
307 case hwmon_pwm_enable
:
308 fan_config
= data
->fan_config
[channel
];
310 fan_config
&= ~(MAX31790_FAN_CFG_TACH_INPUT_EN
|
311 MAX31790_FAN_CFG_RPM_MODE
);
312 } else if (val
== 1) {
313 fan_config
= (fan_config
|
314 MAX31790_FAN_CFG_TACH_INPUT_EN
) &
315 ~MAX31790_FAN_CFG_RPM_MODE
;
316 } else if (val
== 2) {
317 fan_config
|= MAX31790_FAN_CFG_TACH_INPUT_EN
|
318 MAX31790_FAN_CFG_RPM_MODE
;
323 data
->fan_config
[channel
] = fan_config
;
324 err
= i2c_smbus_write_byte_data(client
,
325 MAX31790_REG_FAN_CONFIG(channel
),
333 mutex_unlock(&data
->update_lock
);
338 static umode_t
max31790_pwm_is_visible(const void *_data
, u32 attr
, int channel
)
340 const struct max31790_data
*data
= _data
;
341 u8 fan_config
= data
->fan_config
[channel
];
344 case hwmon_pwm_input
:
345 case hwmon_pwm_enable
:
346 if (!(fan_config
& MAX31790_FAN_CFG_TACH_INPUT
))
354 static int max31790_read(struct device
*dev
, enum hwmon_sensor_types type
,
355 u32 attr
, int channel
, long *val
)
359 return max31790_read_fan(dev
, attr
, channel
, val
);
361 return max31790_read_pwm(dev
, attr
, channel
, val
);
367 static int max31790_write(struct device
*dev
, enum hwmon_sensor_types type
,
368 u32 attr
, int channel
, long val
)
372 return max31790_write_fan(dev
, attr
, channel
, val
);
374 return max31790_write_pwm(dev
, attr
, channel
, val
);
380 static umode_t
max31790_is_visible(const void *data
,
381 enum hwmon_sensor_types type
,
382 u32 attr
, int channel
)
386 return max31790_fan_is_visible(data
, attr
, channel
);
388 return max31790_pwm_is_visible(data
, attr
, channel
);
394 static const struct hwmon_channel_info
*max31790_info
[] = {
395 HWMON_CHANNEL_INFO(fan
,
396 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
397 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
398 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
399 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
400 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
401 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
402 HWMON_F_INPUT
| HWMON_F_FAULT
,
403 HWMON_F_INPUT
| HWMON_F_FAULT
,
404 HWMON_F_INPUT
| HWMON_F_FAULT
,
405 HWMON_F_INPUT
| HWMON_F_FAULT
,
406 HWMON_F_INPUT
| HWMON_F_FAULT
,
407 HWMON_F_INPUT
| HWMON_F_FAULT
),
408 HWMON_CHANNEL_INFO(pwm
,
409 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
410 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
411 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
412 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
413 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
414 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
),
418 static const struct hwmon_ops max31790_hwmon_ops
= {
419 .is_visible
= max31790_is_visible
,
420 .read
= max31790_read
,
421 .write
= max31790_write
,
424 static const struct hwmon_chip_info max31790_chip_info
= {
425 .ops
= &max31790_hwmon_ops
,
426 .info
= max31790_info
,
429 static int max31790_init_client(struct i2c_client
*client
,
430 struct max31790_data
*data
)
434 for (i
= 0; i
< NR_CHANNEL
; i
++) {
435 rv
= i2c_smbus_read_byte_data(client
,
436 MAX31790_REG_FAN_CONFIG(i
));
439 data
->fan_config
[i
] = rv
;
441 rv
= i2c_smbus_read_byte_data(client
,
442 MAX31790_REG_FAN_DYNAMICS(i
));
445 data
->fan_dynamics
[i
] = rv
;
451 static int max31790_probe(struct i2c_client
*client
,
452 const struct i2c_device_id
*id
)
454 struct i2c_adapter
*adapter
= client
->adapter
;
455 struct device
*dev
= &client
->dev
;
456 struct max31790_data
*data
;
457 struct device
*hwmon_dev
;
460 if (!i2c_check_functionality(adapter
,
461 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
))
464 data
= devm_kzalloc(dev
, sizeof(struct max31790_data
), GFP_KERNEL
);
468 data
->client
= client
;
469 mutex_init(&data
->update_lock
);
472 * Initialize the max31790 chip
474 err
= max31790_init_client(client
, data
);
478 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
483 return PTR_ERR_OR_ZERO(hwmon_dev
);
486 static const struct i2c_device_id max31790_id
[] = {
490 MODULE_DEVICE_TABLE(i2c
, max31790_id
);
492 static struct i2c_driver max31790_driver
= {
493 .class = I2C_CLASS_HWMON
,
494 .probe
= max31790_probe
,
498 .id_table
= max31790_id
,
501 module_i2c_driver(max31790_driver
);
503 MODULE_AUTHOR("Il Han <corone.il.han@gmail.com>");
504 MODULE_DESCRIPTION("MAX31790 sensor driver");
505 MODULE_LICENSE("GPL");