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/hwmon-sysfs.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/jiffies.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
27 /* MAX31790 registers */
28 #define MAX31790_REG_GLOBAL_CONFIG 0x00
29 #define MAX31790_REG_FAN_CONFIG(ch) (0x02 + (ch))
30 #define MAX31790_REG_FAN_DYNAMICS(ch) (0x08 + (ch))
31 #define MAX31790_REG_FAN_FAULT_STATUS2 0x10
32 #define MAX31790_REG_FAN_FAULT_STATUS1 0x11
33 #define MAX31790_REG_TACH_COUNT(ch) (0x18 + (ch) * 2)
34 #define MAX31790_REG_PWM_DUTY_CYCLE(ch) (0x30 + (ch) * 2)
35 #define MAX31790_REG_PWMOUT(ch) (0x40 + (ch) * 2)
36 #define MAX31790_REG_TARGET_COUNT(ch) (0x50 + (ch) * 2)
38 /* Fan Config register bits */
39 #define MAX31790_FAN_CFG_RPM_MODE 0x80
40 #define MAX31790_FAN_CFG_TACH_INPUT_EN 0x08
41 #define MAX31790_FAN_CFG_TACH_INPUT 0x01
43 /* Fan Dynamics register bits */
44 #define MAX31790_FAN_DYN_SR_SHIFT 5
45 #define MAX31790_FAN_DYN_SR_MASK 0xE0
46 #define SR_FROM_REG(reg) (((reg) & MAX31790_FAN_DYN_SR_MASK) \
47 >> MAX31790_FAN_DYN_SR_SHIFT)
49 #define FAN_RPM_MIN 120
50 #define FAN_RPM_MAX 7864320
52 #define RPM_FROM_REG(reg, sr) (((reg) >> 4) ? \
53 ((60 * (sr) * 8192) / ((reg) >> 4)) : \
55 #define RPM_TO_REG(rpm, sr) ((60 * (sr) * 8192) / ((rpm) * 2))
60 * Client data (each client gets its own)
62 struct max31790_data
{
63 struct i2c_client
*client
;
64 struct mutex update_lock
;
65 bool valid
; /* zero until following fields are valid */
66 unsigned long last_updated
; /* in jiffies */
69 u8 fan_config
[NR_CHANNEL
];
70 u8 fan_dynamics
[NR_CHANNEL
];
72 u16 tach
[NR_CHANNEL
* 2];
74 u16 target_count
[NR_CHANNEL
];
77 static struct max31790_data
*max31790_update_device(struct device
*dev
)
79 struct max31790_data
*data
= dev_get_drvdata(dev
);
80 struct i2c_client
*client
= data
->client
;
81 struct max31790_data
*ret
= data
;
85 mutex_lock(&data
->update_lock
);
87 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
88 rv
= i2c_smbus_read_byte_data(client
,
89 MAX31790_REG_FAN_FAULT_STATUS1
);
92 data
->fault_status
= rv
& 0x3F;
94 rv
= i2c_smbus_read_byte_data(client
,
95 MAX31790_REG_FAN_FAULT_STATUS2
);
98 data
->fault_status
|= (rv
& 0x3F) << 6;
100 for (i
= 0; i
< NR_CHANNEL
; i
++) {
101 rv
= i2c_smbus_read_word_swapped(client
,
102 MAX31790_REG_TACH_COUNT(i
));
107 if (data
->fan_config
[i
]
108 & MAX31790_FAN_CFG_TACH_INPUT
) {
109 rv
= i2c_smbus_read_word_swapped(client
,
110 MAX31790_REG_TACH_COUNT(NR_CHANNEL
114 data
->tach
[NR_CHANNEL
+ i
] = rv
;
116 rv
= i2c_smbus_read_word_swapped(client
,
117 MAX31790_REG_PWMOUT(i
));
122 rv
= i2c_smbus_read_word_swapped(client
,
123 MAX31790_REG_TARGET_COUNT(i
));
126 data
->target_count
[i
] = rv
;
130 data
->last_updated
= jiffies
;
140 mutex_unlock(&data
->update_lock
);
145 static const u8 tach_period
[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };
147 static u8
get_tach_period(u8 fan_dynamics
)
149 return tach_period
[SR_FROM_REG(fan_dynamics
)];
152 static u8
bits_for_tach_period(int rpm
)
172 static ssize_t
get_fan(struct device
*dev
,
173 struct device_attribute
*devattr
, char *buf
)
175 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
176 struct max31790_data
*data
= max31790_update_device(dev
);
180 return PTR_ERR(data
);
182 sr
= get_tach_period(data
->fan_dynamics
[attr
->index
]);
183 rpm
= RPM_FROM_REG(data
->tach
[attr
->index
], sr
);
185 return sprintf(buf
, "%d\n", rpm
);
188 static ssize_t
get_fan_target(struct device
*dev
,
189 struct device_attribute
*devattr
, char *buf
)
191 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
192 struct max31790_data
*data
= max31790_update_device(dev
);
196 return PTR_ERR(data
);
198 sr
= get_tach_period(data
->fan_dynamics
[attr
->index
]);
199 rpm
= RPM_FROM_REG(data
->target_count
[attr
->index
], sr
);
201 return sprintf(buf
, "%d\n", rpm
);
204 static ssize_t
set_fan_target(struct device
*dev
,
205 struct device_attribute
*devattr
,
206 const char *buf
, size_t count
)
208 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
209 struct max31790_data
*data
= dev_get_drvdata(dev
);
210 struct i2c_client
*client
= data
->client
;
217 err
= kstrtoul(buf
, 10, &rpm
);
221 mutex_lock(&data
->update_lock
);
223 rpm
= clamp_val(rpm
, FAN_RPM_MIN
, FAN_RPM_MAX
);
224 bits
= bits_for_tach_period(rpm
);
225 data
->fan_dynamics
[attr
->index
] =
226 ((data
->fan_dynamics
[attr
->index
]
227 & ~MAX31790_FAN_DYN_SR_MASK
)
228 | (bits
<< MAX31790_FAN_DYN_SR_SHIFT
));
229 err
= i2c_smbus_write_byte_data(client
,
230 MAX31790_REG_FAN_DYNAMICS(attr
->index
),
231 data
->fan_dynamics
[attr
->index
]);
234 mutex_unlock(&data
->update_lock
);
238 sr
= get_tach_period(data
->fan_dynamics
[attr
->index
]);
239 target_count
= RPM_TO_REG(rpm
, sr
);
240 target_count
= clamp_val(target_count
, 0x1, 0x7FF);
242 data
->target_count
[attr
->index
] = target_count
<< 5;
244 err
= i2c_smbus_write_word_swapped(client
,
245 MAX31790_REG_TARGET_COUNT(attr
->index
),
246 data
->target_count
[attr
->index
]);
248 mutex_unlock(&data
->update_lock
);
256 static ssize_t
get_pwm(struct device
*dev
,
257 struct device_attribute
*devattr
, char *buf
)
259 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
260 struct max31790_data
*data
= max31790_update_device(dev
);
264 return PTR_ERR(data
);
266 pwm
= data
->pwm
[attr
->index
] >> 8;
268 return sprintf(buf
, "%d\n", pwm
);
271 static ssize_t
set_pwm(struct device
*dev
,
272 struct device_attribute
*devattr
,
273 const char *buf
, size_t count
)
275 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
276 struct max31790_data
*data
= dev_get_drvdata(dev
);
277 struct i2c_client
*client
= data
->client
;
281 err
= kstrtoul(buf
, 10, &pwm
);
288 mutex_lock(&data
->update_lock
);
290 data
->pwm
[attr
->index
] = pwm
<< 8;
291 err
= i2c_smbus_write_word_swapped(client
,
292 MAX31790_REG_PWMOUT(attr
->index
),
293 data
->pwm
[attr
->index
]);
295 mutex_unlock(&data
->update_lock
);
303 static ssize_t
get_pwm_enable(struct device
*dev
,
304 struct device_attribute
*devattr
, char *buf
)
306 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
307 struct max31790_data
*data
= max31790_update_device(dev
);
311 return PTR_ERR(data
);
313 if (data
->fan_config
[attr
->index
] & MAX31790_FAN_CFG_RPM_MODE
)
315 else if (data
->fan_config
[attr
->index
] & MAX31790_FAN_CFG_TACH_INPUT_EN
)
320 return sprintf(buf
, "%d\n", mode
);
323 static ssize_t
set_pwm_enable(struct device
*dev
,
324 struct device_attribute
*devattr
,
325 const char *buf
, size_t count
)
327 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
328 struct max31790_data
*data
= dev_get_drvdata(dev
);
329 struct i2c_client
*client
= data
->client
;
333 err
= kstrtoul(buf
, 10, &mode
);
339 data
->fan_config
[attr
->index
] =
340 data
->fan_config
[attr
->index
]
341 & ~(MAX31790_FAN_CFG_TACH_INPUT_EN
342 | MAX31790_FAN_CFG_RPM_MODE
);
345 data
->fan_config
[attr
->index
] =
346 (data
->fan_config
[attr
->index
]
347 | MAX31790_FAN_CFG_TACH_INPUT_EN
)
348 & ~MAX31790_FAN_CFG_RPM_MODE
;
351 data
->fan_config
[attr
->index
] =
352 data
->fan_config
[attr
->index
]
353 | MAX31790_FAN_CFG_TACH_INPUT_EN
354 | MAX31790_FAN_CFG_RPM_MODE
;
360 mutex_lock(&data
->update_lock
);
362 err
= i2c_smbus_write_byte_data(client
,
363 MAX31790_REG_FAN_CONFIG(attr
->index
),
364 data
->fan_config
[attr
->index
]);
366 mutex_unlock(&data
->update_lock
);
374 static ssize_t
get_fan_fault(struct device
*dev
,
375 struct device_attribute
*devattr
, char *buf
)
377 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
378 struct max31790_data
*data
= max31790_update_device(dev
);
382 return PTR_ERR(data
);
384 fault
= !!(data
->fault_status
& (1 << attr
->index
));
386 return sprintf(buf
, "%d\n", fault
);
389 static SENSOR_DEVICE_ATTR(fan1_input
, S_IRUGO
, get_fan
, NULL
, 0);
390 static SENSOR_DEVICE_ATTR(fan2_input
, S_IRUGO
, get_fan
, NULL
, 1);
391 static SENSOR_DEVICE_ATTR(fan3_input
, S_IRUGO
, get_fan
, NULL
, 2);
392 static SENSOR_DEVICE_ATTR(fan4_input
, S_IRUGO
, get_fan
, NULL
, 3);
393 static SENSOR_DEVICE_ATTR(fan5_input
, S_IRUGO
, get_fan
, NULL
, 4);
394 static SENSOR_DEVICE_ATTR(fan6_input
, S_IRUGO
, get_fan
, NULL
, 5);
396 static SENSOR_DEVICE_ATTR(fan1_fault
, S_IRUGO
, get_fan_fault
, NULL
, 0);
397 static SENSOR_DEVICE_ATTR(fan2_fault
, S_IRUGO
, get_fan_fault
, NULL
, 1);
398 static SENSOR_DEVICE_ATTR(fan3_fault
, S_IRUGO
, get_fan_fault
, NULL
, 2);
399 static SENSOR_DEVICE_ATTR(fan4_fault
, S_IRUGO
, get_fan_fault
, NULL
, 3);
400 static SENSOR_DEVICE_ATTR(fan5_fault
, S_IRUGO
, get_fan_fault
, NULL
, 4);
401 static SENSOR_DEVICE_ATTR(fan6_fault
, S_IRUGO
, get_fan_fault
, NULL
, 5);
403 static SENSOR_DEVICE_ATTR(fan7_input
, S_IRUGO
, get_fan
, NULL
, 6);
404 static SENSOR_DEVICE_ATTR(fan8_input
, S_IRUGO
, get_fan
, NULL
, 7);
405 static SENSOR_DEVICE_ATTR(fan9_input
, S_IRUGO
, get_fan
, NULL
, 8);
406 static SENSOR_DEVICE_ATTR(fan10_input
, S_IRUGO
, get_fan
, NULL
, 9);
407 static SENSOR_DEVICE_ATTR(fan11_input
, S_IRUGO
, get_fan
, NULL
, 10);
408 static SENSOR_DEVICE_ATTR(fan12_input
, S_IRUGO
, get_fan
, NULL
, 11);
410 static SENSOR_DEVICE_ATTR(fan7_fault
, S_IRUGO
, get_fan_fault
, NULL
, 6);
411 static SENSOR_DEVICE_ATTR(fan8_fault
, S_IRUGO
, get_fan_fault
, NULL
, 7);
412 static SENSOR_DEVICE_ATTR(fan9_fault
, S_IRUGO
, get_fan_fault
, NULL
, 8);
413 static SENSOR_DEVICE_ATTR(fan10_fault
, S_IRUGO
, get_fan_fault
, NULL
, 9);
414 static SENSOR_DEVICE_ATTR(fan11_fault
, S_IRUGO
, get_fan_fault
, NULL
, 10);
415 static SENSOR_DEVICE_ATTR(fan12_fault
, S_IRUGO
, get_fan_fault
, NULL
, 11);
417 static SENSOR_DEVICE_ATTR(fan1_target
, S_IWUSR
| S_IRUGO
,
418 get_fan_target
, set_fan_target
, 0);
419 static SENSOR_DEVICE_ATTR(fan2_target
, S_IWUSR
| S_IRUGO
,
420 get_fan_target
, set_fan_target
, 1);
421 static SENSOR_DEVICE_ATTR(fan3_target
, S_IWUSR
| S_IRUGO
,
422 get_fan_target
, set_fan_target
, 2);
423 static SENSOR_DEVICE_ATTR(fan4_target
, S_IWUSR
| S_IRUGO
,
424 get_fan_target
, set_fan_target
, 3);
425 static SENSOR_DEVICE_ATTR(fan5_target
, S_IWUSR
| S_IRUGO
,
426 get_fan_target
, set_fan_target
, 4);
427 static SENSOR_DEVICE_ATTR(fan6_target
, S_IWUSR
| S_IRUGO
,
428 get_fan_target
, set_fan_target
, 5);
430 static SENSOR_DEVICE_ATTR(pwm1
, S_IWUSR
| S_IRUGO
, get_pwm
, set_pwm
, 0);
431 static SENSOR_DEVICE_ATTR(pwm2
, S_IWUSR
| S_IRUGO
, get_pwm
, set_pwm
, 1);
432 static SENSOR_DEVICE_ATTR(pwm3
, S_IWUSR
| S_IRUGO
, get_pwm
, set_pwm
, 2);
433 static SENSOR_DEVICE_ATTR(pwm4
, S_IWUSR
| S_IRUGO
, get_pwm
, set_pwm
, 3);
434 static SENSOR_DEVICE_ATTR(pwm5
, S_IWUSR
| S_IRUGO
, get_pwm
, set_pwm
, 4);
435 static SENSOR_DEVICE_ATTR(pwm6
, S_IWUSR
| S_IRUGO
, get_pwm
, set_pwm
, 5);
437 static SENSOR_DEVICE_ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
438 get_pwm_enable
, set_pwm_enable
, 0);
439 static SENSOR_DEVICE_ATTR(pwm2_enable
, S_IWUSR
| S_IRUGO
,
440 get_pwm_enable
, set_pwm_enable
, 1);
441 static SENSOR_DEVICE_ATTR(pwm3_enable
, S_IWUSR
| S_IRUGO
,
442 get_pwm_enable
, set_pwm_enable
, 2);
443 static SENSOR_DEVICE_ATTR(pwm4_enable
, S_IWUSR
| S_IRUGO
,
444 get_pwm_enable
, set_pwm_enable
, 3);
445 static SENSOR_DEVICE_ATTR(pwm5_enable
, S_IWUSR
| S_IRUGO
,
446 get_pwm_enable
, set_pwm_enable
, 4);
447 static SENSOR_DEVICE_ATTR(pwm6_enable
, S_IWUSR
| S_IRUGO
,
448 get_pwm_enable
, set_pwm_enable
, 5);
450 static struct attribute
*max31790_attrs
[] = {
451 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
452 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
453 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
454 &sensor_dev_attr_fan4_input
.dev_attr
.attr
,
455 &sensor_dev_attr_fan5_input
.dev_attr
.attr
,
456 &sensor_dev_attr_fan6_input
.dev_attr
.attr
,
458 &sensor_dev_attr_fan1_fault
.dev_attr
.attr
,
459 &sensor_dev_attr_fan2_fault
.dev_attr
.attr
,
460 &sensor_dev_attr_fan3_fault
.dev_attr
.attr
,
461 &sensor_dev_attr_fan4_fault
.dev_attr
.attr
,
462 &sensor_dev_attr_fan5_fault
.dev_attr
.attr
,
463 &sensor_dev_attr_fan6_fault
.dev_attr
.attr
,
465 &sensor_dev_attr_fan7_input
.dev_attr
.attr
,
466 &sensor_dev_attr_fan8_input
.dev_attr
.attr
,
467 &sensor_dev_attr_fan9_input
.dev_attr
.attr
,
468 &sensor_dev_attr_fan10_input
.dev_attr
.attr
,
469 &sensor_dev_attr_fan11_input
.dev_attr
.attr
,
470 &sensor_dev_attr_fan12_input
.dev_attr
.attr
,
472 &sensor_dev_attr_fan7_fault
.dev_attr
.attr
,
473 &sensor_dev_attr_fan8_fault
.dev_attr
.attr
,
474 &sensor_dev_attr_fan9_fault
.dev_attr
.attr
,
475 &sensor_dev_attr_fan10_fault
.dev_attr
.attr
,
476 &sensor_dev_attr_fan11_fault
.dev_attr
.attr
,
477 &sensor_dev_attr_fan12_fault
.dev_attr
.attr
,
479 &sensor_dev_attr_fan1_target
.dev_attr
.attr
,
480 &sensor_dev_attr_fan2_target
.dev_attr
.attr
,
481 &sensor_dev_attr_fan3_target
.dev_attr
.attr
,
482 &sensor_dev_attr_fan4_target
.dev_attr
.attr
,
483 &sensor_dev_attr_fan5_target
.dev_attr
.attr
,
484 &sensor_dev_attr_fan6_target
.dev_attr
.attr
,
486 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
487 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
488 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
489 &sensor_dev_attr_pwm4
.dev_attr
.attr
,
490 &sensor_dev_attr_pwm5
.dev_attr
.attr
,
491 &sensor_dev_attr_pwm6
.dev_attr
.attr
,
493 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
494 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
495 &sensor_dev_attr_pwm3_enable
.dev_attr
.attr
,
496 &sensor_dev_attr_pwm4_enable
.dev_attr
.attr
,
497 &sensor_dev_attr_pwm5_enable
.dev_attr
.attr
,
498 &sensor_dev_attr_pwm6_enable
.dev_attr
.attr
,
502 static umode_t
max31790_attrs_visible(struct kobject
*kobj
,
503 struct attribute
*a
, int n
)
505 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
506 struct max31790_data
*data
= dev_get_drvdata(dev
);
507 struct device_attribute
*devattr
=
508 container_of(a
, struct device_attribute
, attr
);
509 int index
= to_sensor_dev_attr(devattr
)->index
% NR_CHANNEL
;
512 fan_config
= data
->fan_config
[index
];
514 if (n
>= NR_CHANNEL
* 2 && n
< NR_CHANNEL
* 4 &&
515 !(fan_config
& MAX31790_FAN_CFG_TACH_INPUT
))
517 if (n
>= NR_CHANNEL
* 4 && (fan_config
& MAX31790_FAN_CFG_TACH_INPUT
))
523 static const struct attribute_group max31790_group
= {
524 .attrs
= max31790_attrs
,
525 .is_visible
= max31790_attrs_visible
,
527 __ATTRIBUTE_GROUPS(max31790
);
529 static int max31790_init_client(struct i2c_client
*client
,
530 struct max31790_data
*data
)
534 for (i
= 0; i
< NR_CHANNEL
; i
++) {
535 rv
= i2c_smbus_read_byte_data(client
,
536 MAX31790_REG_FAN_CONFIG(i
));
539 data
->fan_config
[i
] = rv
;
541 rv
= i2c_smbus_read_byte_data(client
,
542 MAX31790_REG_FAN_DYNAMICS(i
));
545 data
->fan_dynamics
[i
] = rv
;
551 static int max31790_probe(struct i2c_client
*client
,
552 const struct i2c_device_id
*id
)
554 struct i2c_adapter
*adapter
= client
->adapter
;
555 struct device
*dev
= &client
->dev
;
556 struct max31790_data
*data
;
557 struct device
*hwmon_dev
;
560 if (!i2c_check_functionality(adapter
,
561 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
))
564 data
= devm_kzalloc(dev
, sizeof(struct max31790_data
), GFP_KERNEL
);
568 data
->client
= client
;
569 mutex_init(&data
->update_lock
);
572 * Initialize the max31790 chip
574 err
= max31790_init_client(client
, data
);
578 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
,
579 client
->name
, data
, max31790_groups
);
581 return PTR_ERR_OR_ZERO(hwmon_dev
);
584 static const struct i2c_device_id max31790_id
[] = {
588 MODULE_DEVICE_TABLE(i2c
, max31790_id
);
590 static struct i2c_driver max31790_driver
= {
591 .class = I2C_CLASS_HWMON
,
592 .probe
= max31790_probe
,
596 .id_table
= max31790_id
,
599 module_i2c_driver(max31790_driver
);
601 MODULE_AUTHOR("Il Han <corone.il.han@gmail.com>");
602 MODULE_DESCRIPTION("MAX31790 sensor driver");
603 MODULE_LICENSE("GPL");