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
);
271 u8 fan_config
= data
->fan_config
[channel
];
274 return PTR_ERR(data
);
277 case hwmon_pwm_input
:
278 *val
= data
->pwm
[channel
] >> 8;
280 case hwmon_pwm_enable
:
281 if (fan_config
& MAX31790_FAN_CFG_RPM_MODE
)
283 else if (fan_config
& MAX31790_FAN_CFG_TACH_INPUT_EN
)
293 static int max31790_write_pwm(struct device
*dev
, u32 attr
, int channel
,
296 struct max31790_data
*data
= dev_get_drvdata(dev
);
297 struct i2c_client
*client
= data
->client
;
301 mutex_lock(&data
->update_lock
);
304 case hwmon_pwm_input
:
305 if (val
< 0 || val
> 255) {
309 data
->pwm
[channel
] = val
<< 8;
310 err
= i2c_smbus_write_word_swapped(client
,
311 MAX31790_REG_PWMOUT(channel
),
314 case hwmon_pwm_enable
:
315 fan_config
= data
->fan_config
[channel
];
317 fan_config
&= ~(MAX31790_FAN_CFG_TACH_INPUT_EN
|
318 MAX31790_FAN_CFG_RPM_MODE
);
319 } else if (val
== 1) {
320 fan_config
= (fan_config
|
321 MAX31790_FAN_CFG_TACH_INPUT_EN
) &
322 ~MAX31790_FAN_CFG_RPM_MODE
;
323 } else if (val
== 2) {
324 fan_config
|= MAX31790_FAN_CFG_TACH_INPUT_EN
|
325 MAX31790_FAN_CFG_RPM_MODE
;
330 data
->fan_config
[channel
] = fan_config
;
331 err
= i2c_smbus_write_byte_data(client
,
332 MAX31790_REG_FAN_CONFIG(channel
),
340 mutex_unlock(&data
->update_lock
);
345 static umode_t
max31790_pwm_is_visible(const void *_data
, u32 attr
, int channel
)
347 const struct max31790_data
*data
= _data
;
348 u8 fan_config
= data
->fan_config
[channel
];
351 case hwmon_pwm_input
:
352 case hwmon_pwm_enable
:
353 if (!(fan_config
& MAX31790_FAN_CFG_TACH_INPUT
))
354 return S_IRUGO
| S_IWUSR
;
361 static int max31790_read(struct device
*dev
, enum hwmon_sensor_types type
,
362 u32 attr
, int channel
, long *val
)
366 return max31790_read_fan(dev
, attr
, channel
, val
);
368 return max31790_read_pwm(dev
, attr
, channel
, val
);
374 static int max31790_write(struct device
*dev
, enum hwmon_sensor_types type
,
375 u32 attr
, int channel
, long val
)
379 return max31790_write_fan(dev
, attr
, channel
, val
);
381 return max31790_write_pwm(dev
, attr
, channel
, val
);
387 static umode_t
max31790_is_visible(const void *data
,
388 enum hwmon_sensor_types type
,
389 u32 attr
, int channel
)
393 return max31790_fan_is_visible(data
, attr
, channel
);
395 return max31790_pwm_is_visible(data
, attr
, channel
);
401 static const u32 max31790_fan_config
[] = {
402 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
403 HWMON_F_INPUT
| HWMON_F_TARGET
| HWMON_F_FAULT
,
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_FAULT
,
409 HWMON_F_INPUT
| 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
,
417 static const struct hwmon_channel_info max31790_fan
= {
419 .config
= max31790_fan_config
,
422 static const u32 max31790_pwm_config
[] = {
423 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
424 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
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
,
432 static const struct hwmon_channel_info max31790_pwm
= {
434 .config
= max31790_pwm_config
,
437 static const struct hwmon_channel_info
*max31790_info
[] = {
443 static const struct hwmon_ops max31790_hwmon_ops
= {
444 .is_visible
= max31790_is_visible
,
445 .read
= max31790_read
,
446 .write
= max31790_write
,
449 static const struct hwmon_chip_info max31790_chip_info
= {
450 .ops
= &max31790_hwmon_ops
,
451 .info
= max31790_info
,
454 static int max31790_init_client(struct i2c_client
*client
,
455 struct max31790_data
*data
)
459 for (i
= 0; i
< NR_CHANNEL
; i
++) {
460 rv
= i2c_smbus_read_byte_data(client
,
461 MAX31790_REG_FAN_CONFIG(i
));
464 data
->fan_config
[i
] = rv
;
466 rv
= i2c_smbus_read_byte_data(client
,
467 MAX31790_REG_FAN_DYNAMICS(i
));
470 data
->fan_dynamics
[i
] = rv
;
476 static int max31790_probe(struct i2c_client
*client
,
477 const struct i2c_device_id
*id
)
479 struct i2c_adapter
*adapter
= client
->adapter
;
480 struct device
*dev
= &client
->dev
;
481 struct max31790_data
*data
;
482 struct device
*hwmon_dev
;
485 if (!i2c_check_functionality(adapter
,
486 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
))
489 data
= devm_kzalloc(dev
, sizeof(struct max31790_data
), GFP_KERNEL
);
493 data
->client
= client
;
494 mutex_init(&data
->update_lock
);
497 * Initialize the max31790 chip
499 err
= max31790_init_client(client
, data
);
503 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
508 return PTR_ERR_OR_ZERO(hwmon_dev
);
511 static const struct i2c_device_id max31790_id
[] = {
515 MODULE_DEVICE_TABLE(i2c
, max31790_id
);
517 static struct i2c_driver max31790_driver
= {
518 .class = I2C_CLASS_HWMON
,
519 .probe
= max31790_probe
,
523 .id_table
= max31790_id
,
526 module_i2c_driver(max31790_driver
);
528 MODULE_AUTHOR("Il Han <corone.il.han@gmail.com>");
529 MODULE_DESCRIPTION("MAX31790 sensor driver");
530 MODULE_LICENSE("GPL");