2 * max31790.c - Part of lm_sensors, Linux kernel modules for hardware
5 * (C) 2015 by Il Han <corone.il.han@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/err.h>
19 #include <linux/hwmon.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/jiffies.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
26 /* MAX31790 registers */
27 #define MAX31790_REG_GLOBAL_CONFIG 0x00
28 #define MAX31790_REG_FAN_CONFIG(ch) (0x02 + (ch))
29 #define MAX31790_REG_FAN_DYNAMICS(ch) (0x08 + (ch))
30 #define MAX31790_REG_FAN_FAULT_STATUS2 0x10
31 #define MAX31790_REG_FAN_FAULT_STATUS1 0x11
32 #define MAX31790_REG_TACH_COUNT(ch) (0x18 + (ch) * 2)
33 #define MAX31790_REG_PWM_DUTY_CYCLE(ch) (0x30 + (ch) * 2)
34 #define MAX31790_REG_PWMOUT(ch) (0x40 + (ch) * 2)
35 #define MAX31790_REG_TARGET_COUNT(ch) (0x50 + (ch) * 2)
37 /* Fan Config register bits */
38 #define MAX31790_FAN_CFG_RPM_MODE 0x80
39 #define MAX31790_FAN_CFG_TACH_INPUT_EN 0x08
40 #define MAX31790_FAN_CFG_TACH_INPUT 0x01
42 /* Fan Dynamics register bits */
43 #define MAX31790_FAN_DYN_SR_SHIFT 5
44 #define MAX31790_FAN_DYN_SR_MASK 0xE0
45 #define SR_FROM_REG(reg) (((reg) & MAX31790_FAN_DYN_SR_MASK) \
46 >> MAX31790_FAN_DYN_SR_SHIFT)
48 #define FAN_RPM_MIN 120
49 #define FAN_RPM_MAX 7864320
51 #define RPM_FROM_REG(reg, sr) (((reg) >> 4) ? \
52 ((60 * (sr) * 8192) / ((reg) >> 4)) : \
54 #define RPM_TO_REG(rpm, sr) ((60 * (sr) * 8192) / ((rpm) * 2))
59 * Client data (each client gets its own)
61 struct max31790_data
{
62 struct i2c_client
*client
;
63 struct mutex update_lock
;
64 bool valid
; /* zero until following fields are valid */
65 unsigned long last_updated
; /* in jiffies */
68 u8 fan_config
[NR_CHANNEL
];
69 u8 fan_dynamics
[NR_CHANNEL
];
71 u16 tach
[NR_CHANNEL
* 2];
73 u16 target_count
[NR_CHANNEL
];
76 static struct max31790_data
*max31790_update_device(struct device
*dev
)
78 struct max31790_data
*data
= dev_get_drvdata(dev
);
79 struct i2c_client
*client
= data
->client
;
80 struct max31790_data
*ret
= data
;
84 mutex_lock(&data
->update_lock
);
86 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
87 rv
= i2c_smbus_read_byte_data(client
,
88 MAX31790_REG_FAN_FAULT_STATUS1
);
91 data
->fault_status
= rv
& 0x3F;
93 rv
= i2c_smbus_read_byte_data(client
,
94 MAX31790_REG_FAN_FAULT_STATUS2
);
97 data
->fault_status
|= (rv
& 0x3F) << 6;
99 for (i
= 0; i
< NR_CHANNEL
; i
++) {
100 rv
= i2c_smbus_read_word_swapped(client
,
101 MAX31790_REG_TACH_COUNT(i
));
106 if (data
->fan_config
[i
]
107 & MAX31790_FAN_CFG_TACH_INPUT
) {
108 rv
= i2c_smbus_read_word_swapped(client
,
109 MAX31790_REG_TACH_COUNT(NR_CHANNEL
113 data
->tach
[NR_CHANNEL
+ i
] = rv
;
115 rv
= i2c_smbus_read_word_swapped(client
,
116 MAX31790_REG_PWMOUT(i
));
121 rv
= i2c_smbus_read_word_swapped(client
,
122 MAX31790_REG_TARGET_COUNT(i
));
125 data
->target_count
[i
] = rv
;
129 data
->last_updated
= jiffies
;
139 mutex_unlock(&data
->update_lock
);
144 static const u8 tach_period
[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };
146 static u8
get_tach_period(u8 fan_dynamics
)
148 return tach_period
[SR_FROM_REG(fan_dynamics
)];
151 static u8
bits_for_tach_period(int rpm
)
171 static int max31790_read_fan(struct device
*dev
, u32 attr
, int channel
,
174 struct max31790_data
*data
= max31790_update_device(dev
);
178 return PTR_ERR(data
);
181 case hwmon_fan_input
:
182 sr
= get_tach_period(data
->fan_dynamics
[channel
]);
183 rpm
= RPM_FROM_REG(data
->tach
[channel
], sr
);
186 case hwmon_fan_target
:
187 sr
= get_tach_period(data
->fan_dynamics
[channel
]);
188 rpm
= RPM_FROM_REG(data
->target_count
[channel
], sr
);
191 case hwmon_fan_fault
:
192 *val
= !!(data
->fault_status
& (1 << channel
));
199 static int max31790_write_fan(struct device
*dev
, u32 attr
, int channel
,
202 struct max31790_data
*data
= dev_get_drvdata(dev
);
203 struct i2c_client
*client
= data
->client
;
209 mutex_lock(&data
->update_lock
);
212 case hwmon_fan_target
:
213 val
= clamp_val(val
, FAN_RPM_MIN
, FAN_RPM_MAX
);
214 bits
= bits_for_tach_period(val
);
215 data
->fan_dynamics
[channel
] =
216 ((data
->fan_dynamics
[channel
] &
217 ~MAX31790_FAN_DYN_SR_MASK
) |
218 (bits
<< MAX31790_FAN_DYN_SR_SHIFT
));
219 err
= i2c_smbus_write_byte_data(client
,
220 MAX31790_REG_FAN_DYNAMICS(channel
),
221 data
->fan_dynamics
[channel
]);
225 sr
= get_tach_period(data
->fan_dynamics
[channel
]);
226 target_count
= RPM_TO_REG(val
, sr
);
227 target_count
= clamp_val(target_count
, 0x1, 0x7FF);
229 data
->target_count
[channel
] = target_count
<< 5;
231 err
= i2c_smbus_write_word_swapped(client
,
232 MAX31790_REG_TARGET_COUNT(channel
),
233 data
->target_count
[channel
]);
240 mutex_unlock(&data
->update_lock
);
245 static umode_t
max31790_fan_is_visible(const void *_data
, u32 attr
, int channel
)
247 const struct max31790_data
*data
= _data
;
248 u8 fan_config
= data
->fan_config
[channel
% NR_CHANNEL
];
251 case hwmon_fan_input
:
252 case hwmon_fan_fault
:
253 if (channel
< NR_CHANNEL
||
254 (fan_config
& MAX31790_FAN_CFG_TACH_INPUT
))
257 case hwmon_fan_target
:
258 if (channel
< NR_CHANNEL
&&
259 !(fan_config
& MAX31790_FAN_CFG_TACH_INPUT
))
260 return S_IRUGO
| S_IWUSR
;
267 static int max31790_read_pwm(struct device
*dev
, u32 attr
, int channel
,
270 struct max31790_data
*data
= max31790_update_device(dev
);
274 return PTR_ERR(data
);
276 fan_config
= data
->fan_config
[channel
];
279 case hwmon_pwm_input
:
280 *val
= data
->pwm
[channel
] >> 8;
282 case hwmon_pwm_enable
:
283 if (fan_config
& MAX31790_FAN_CFG_RPM_MODE
)
285 else if (fan_config
& MAX31790_FAN_CFG_TACH_INPUT_EN
)
295 static int max31790_write_pwm(struct device
*dev
, u32 attr
, int channel
,
298 struct max31790_data
*data
= dev_get_drvdata(dev
);
299 struct i2c_client
*client
= data
->client
;
303 mutex_lock(&data
->update_lock
);
306 case hwmon_pwm_input
:
307 if (val
< 0 || val
> 255) {
311 data
->pwm
[channel
] = val
<< 8;
312 err
= i2c_smbus_write_word_swapped(client
,
313 MAX31790_REG_PWMOUT(channel
),
316 case hwmon_pwm_enable
:
317 fan_config
= data
->fan_config
[channel
];
319 fan_config
&= ~(MAX31790_FAN_CFG_TACH_INPUT_EN
|
320 MAX31790_FAN_CFG_RPM_MODE
);
321 } else if (val
== 1) {
322 fan_config
= (fan_config
|
323 MAX31790_FAN_CFG_TACH_INPUT_EN
) &
324 ~MAX31790_FAN_CFG_RPM_MODE
;
325 } else if (val
== 2) {
326 fan_config
|= MAX31790_FAN_CFG_TACH_INPUT_EN
|
327 MAX31790_FAN_CFG_RPM_MODE
;
332 data
->fan_config
[channel
] = fan_config
;
333 err
= i2c_smbus_write_byte_data(client
,
334 MAX31790_REG_FAN_CONFIG(channel
),
342 mutex_unlock(&data
->update_lock
);
347 static umode_t
max31790_pwm_is_visible(const void *_data
, u32 attr
, int channel
)
349 const struct max31790_data
*data
= _data
;
350 u8 fan_config
= data
->fan_config
[channel
];
353 case hwmon_pwm_input
:
354 case hwmon_pwm_enable
:
355 if (!(fan_config
& MAX31790_FAN_CFG_TACH_INPUT
))
356 return S_IRUGO
| S_IWUSR
;
363 static int max31790_read(struct device
*dev
, enum hwmon_sensor_types type
,
364 u32 attr
, int channel
, long *val
)
368 return max31790_read_fan(dev
, attr
, channel
, val
);
370 return max31790_read_pwm(dev
, attr
, channel
, val
);
376 static int max31790_write(struct device
*dev
, enum hwmon_sensor_types type
,
377 u32 attr
, int channel
, long val
)
381 return max31790_write_fan(dev
, attr
, channel
, val
);
383 return max31790_write_pwm(dev
, attr
, channel
, val
);
389 static umode_t
max31790_is_visible(const void *data
,
390 enum hwmon_sensor_types type
,
391 u32 attr
, int channel
)
395 return max31790_fan_is_visible(data
, attr
, channel
);
397 return max31790_pwm_is_visible(data
, attr
, channel
);
403 static const u32 max31790_fan_config
[] = {
404 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
405 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
406 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
407 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
408 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
409 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
410 HWMON_F_INPUT
| HWMON_F_FAULT
,
411 HWMON_F_INPUT
| HWMON_F_FAULT
,
412 HWMON_F_INPUT
| HWMON_F_FAULT
,
413 HWMON_F_INPUT
| HWMON_F_FAULT
,
414 HWMON_F_INPUT
| HWMON_F_FAULT
,
415 HWMON_F_INPUT
| HWMON_F_FAULT
,
419 static const struct hwmon_channel_info max31790_fan
= {
421 .config
= max31790_fan_config
,
424 static const u32 max31790_pwm_config
[] = {
425 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
426 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
427 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
428 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
429 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
430 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
434 static const struct hwmon_channel_info max31790_pwm
= {
436 .config
= max31790_pwm_config
,
439 static const struct hwmon_channel_info
*max31790_info
[] = {
445 static const struct hwmon_ops max31790_hwmon_ops
= {
446 .is_visible
= max31790_is_visible
,
447 .read
= max31790_read
,
448 .write
= max31790_write
,
451 static const struct hwmon_chip_info max31790_chip_info
= {
452 .ops
= &max31790_hwmon_ops
,
453 .info
= max31790_info
,
456 static int max31790_init_client(struct i2c_client
*client
,
457 struct max31790_data
*data
)
461 for (i
= 0; i
< NR_CHANNEL
; i
++) {
462 rv
= i2c_smbus_read_byte_data(client
,
463 MAX31790_REG_FAN_CONFIG(i
));
466 data
->fan_config
[i
] = rv
;
468 rv
= i2c_smbus_read_byte_data(client
,
469 MAX31790_REG_FAN_DYNAMICS(i
));
472 data
->fan_dynamics
[i
] = rv
;
478 static int max31790_probe(struct i2c_client
*client
,
479 const struct i2c_device_id
*id
)
481 struct i2c_adapter
*adapter
= client
->adapter
;
482 struct device
*dev
= &client
->dev
;
483 struct max31790_data
*data
;
484 struct device
*hwmon_dev
;
487 if (!i2c_check_functionality(adapter
,
488 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
))
491 data
= devm_kzalloc(dev
, sizeof(struct max31790_data
), GFP_KERNEL
);
495 data
->client
= client
;
496 mutex_init(&data
->update_lock
);
499 * Initialize the max31790 chip
501 err
= max31790_init_client(client
, data
);
505 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
510 return PTR_ERR_OR_ZERO(hwmon_dev
);
513 static const struct i2c_device_id max31790_id
[] = {
517 MODULE_DEVICE_TABLE(i2c
, max31790_id
);
519 static struct i2c_driver max31790_driver
= {
520 .class = I2C_CLASS_HWMON
,
521 .probe
= max31790_probe
,
525 .id_table
= max31790_id
,
528 module_i2c_driver(max31790_driver
);
530 MODULE_AUTHOR("Il Han <corone.il.han@gmail.com>");
531 MODULE_DESCRIPTION("MAX31790 sensor driver");
532 MODULE_LICENSE("GPL");