1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * max6639.c - Support for Maxim MAX6639
5 * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller
7 * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
9 * based on the initial MAX6639 support from semptian.net
10 * by He Changqing <hechangqing@semptian.com>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/jiffies.h>
17 #include <linux/i2c.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/err.h>
21 #include <linux/mutex.h>
22 #include <linux/regmap.h>
23 #include <linux/util_macros.h>
25 /* Addresses to scan */
26 static const unsigned short normal_i2c
[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END
};
28 /* The MAX6639 registers, valid channel numbers: 0, 1 */
29 #define MAX6639_REG_TEMP(ch) (0x00 + (ch))
30 #define MAX6639_REG_STATUS 0x02
31 #define MAX6639_REG_OUTPUT_MASK 0x03
32 #define MAX6639_REG_GCONFIG 0x04
33 #define MAX6639_REG_TEMP_EXT(ch) (0x05 + (ch))
34 #define MAX6639_REG_ALERT_LIMIT(ch) (0x08 + (ch))
35 #define MAX6639_REG_OT_LIMIT(ch) (0x0A + (ch))
36 #define MAX6639_REG_THERM_LIMIT(ch) (0x0C + (ch))
37 #define MAX6639_REG_FAN_CONFIG1(ch) (0x10 + (ch) * 4)
38 #define MAX6639_REG_FAN_CONFIG2a(ch) (0x11 + (ch) * 4)
39 #define MAX6639_REG_FAN_CONFIG2b(ch) (0x12 + (ch) * 4)
40 #define MAX6639_REG_FAN_CONFIG3(ch) (0x13 + (ch) * 4)
41 #define MAX6639_REG_FAN_CNT(ch) (0x20 + (ch))
42 #define MAX6639_REG_TARGET_CNT(ch) (0x22 + (ch))
43 #define MAX6639_REG_FAN_PPR(ch) (0x24 + (ch))
44 #define MAX6639_REG_TARGTDUTY(ch) (0x26 + (ch))
45 #define MAX6639_REG_FAN_START_TEMP(ch) (0x28 + (ch))
46 #define MAX6639_REG_DEVID 0x3D
47 #define MAX6639_REG_MANUID 0x3E
48 #define MAX6639_REG_DEVREV 0x3F
51 #define MAX6639_GCONFIG_STANDBY 0x80
52 #define MAX6639_GCONFIG_POR 0x40
53 #define MAX6639_GCONFIG_DISABLE_TIMEOUT 0x20
54 #define MAX6639_GCONFIG_CH2_LOCAL 0x10
55 #define MAX6639_GCONFIG_PWM_FREQ_HI 0x08
57 #define MAX6639_FAN_CONFIG1_PWM 0x80
58 #define MAX6639_FAN_CONFIG3_FREQ_MASK 0x03
59 #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED 0x40
61 #define MAX6639_NUM_CHANNELS 2
63 static const int rpm_ranges
[] = { 2000, 4000, 8000, 16000 };
65 /* Supported PWM frequency */
66 static const unsigned int freq_table
[] = { 20, 33, 50, 100, 5000, 8333, 12500,
69 #define FAN_FROM_REG(val, rpm_range) ((val) == 0 || (val) == 255 ? \
70 0 : (rpm_ranges[rpm_range] * 30) / (val))
71 #define TEMP_LIMIT_TO_REG(val) clamp_val((val) / 1000, 0, 255)
74 * Client data (each client gets its own)
77 struct regmap
*regmap
;
78 struct mutex update_lock
;
80 /* Register values initialized only once */
81 u8 ppr
[MAX6639_NUM_CHANNELS
]; /* Pulses per rotation 0..3 for 1..4 ppr */
82 u8 rpm_range
[MAX6639_NUM_CHANNELS
]; /* Index in above rpm_ranges table */
84 /* Optional regulator for FAN supply */
85 struct regulator
*reg
;
88 static int max6639_temp_read_input(struct device
*dev
, int channel
, long *temp
)
90 u32 regs
[2] = { MAX6639_REG_TEMP_EXT(channel
), MAX6639_REG_TEMP(channel
) };
91 struct max6639_data
*data
= dev_get_drvdata(dev
);
95 res
= regmap_multi_reg_read(data
->regmap
, regs
, regvals
, 2);
99 *temp
= ((regvals
[0] >> 5) | (regvals
[1] << 3)) * 125;
104 static int max6639_temp_read_fault(struct device
*dev
, int channel
, long *fault
)
106 struct max6639_data
*data
= dev_get_drvdata(dev
);
110 res
= regmap_read(data
->regmap
, MAX6639_REG_TEMP_EXT(channel
), &val
);
119 static int max6639_temp_read_max(struct device
*dev
, int channel
, long *max
)
121 struct max6639_data
*data
= dev_get_drvdata(dev
);
125 res
= regmap_read(data
->regmap
, MAX6639_REG_THERM_LIMIT(channel
), &val
);
129 *max
= (long)val
* 1000;
134 static int max6639_temp_read_crit(struct device
*dev
, int channel
, long *crit
)
136 struct max6639_data
*data
= dev_get_drvdata(dev
);
140 res
= regmap_read(data
->regmap
, MAX6639_REG_ALERT_LIMIT(channel
), &val
);
144 *crit
= (long)val
* 1000;
149 static int max6639_temp_read_emergency(struct device
*dev
, int channel
, long *emerg
)
151 struct max6639_data
*data
= dev_get_drvdata(dev
);
155 res
= regmap_read(data
->regmap
, MAX6639_REG_OT_LIMIT(channel
), &val
);
159 *emerg
= (long)val
* 1000;
164 static int max6639_get_status(struct device
*dev
, unsigned int *status
)
166 struct max6639_data
*data
= dev_get_drvdata(dev
);
170 res
= regmap_read(data
->regmap
, MAX6639_REG_STATUS
, &val
);
179 static int max6639_temp_set_max(struct max6639_data
*data
, int channel
, long val
)
183 res
= regmap_write(data
->regmap
, MAX6639_REG_THERM_LIMIT(channel
),
184 TEMP_LIMIT_TO_REG(val
));
188 static int max6639_temp_set_crit(struct max6639_data
*data
, int channel
, long val
)
192 res
= regmap_write(data
->regmap
, MAX6639_REG_ALERT_LIMIT(channel
), TEMP_LIMIT_TO_REG(val
));
197 static int max6639_temp_set_emergency(struct max6639_data
*data
, int channel
, long val
)
201 res
= regmap_write(data
->regmap
, MAX6639_REG_OT_LIMIT(channel
), TEMP_LIMIT_TO_REG(val
));
206 static int max6639_read_fan(struct device
*dev
, u32 attr
, int channel
,
209 struct max6639_data
*data
= dev_get_drvdata(dev
);
214 case hwmon_fan_input
:
215 res
= regmap_read(data
->regmap
, MAX6639_REG_FAN_CNT(channel
), &val
);
218 *fan_val
= FAN_FROM_REG(val
, data
->rpm_range
[channel
]);
220 case hwmon_fan_fault
:
221 res
= max6639_get_status(dev
, &val
);
224 *fan_val
= !!(val
& BIT(1 - channel
));
226 case hwmon_fan_pulses
:
227 *fan_val
= data
->ppr
[channel
];
234 static int max6639_set_ppr(struct max6639_data
*data
, int channel
, u8 ppr
)
236 /* Decrement the PPR value and shift left by 6 to match the register format */
237 return regmap_write(data
->regmap
, MAX6639_REG_FAN_PPR(channel
), ppr
-- << 6);
240 static int max6639_write_fan(struct device
*dev
, u32 attr
, int channel
,
243 struct max6639_data
*data
= dev_get_drvdata(dev
);
247 case hwmon_fan_pulses
:
248 if (val
<= 0 || val
> 4)
251 mutex_lock(&data
->update_lock
);
252 /* Set Fan pulse per revolution */
253 err
= max6639_set_ppr(data
, channel
, val
);
255 mutex_unlock(&data
->update_lock
);
258 data
->ppr
[channel
] = val
;
260 mutex_unlock(&data
->update_lock
);
267 static umode_t
max6639_fan_is_visible(const void *_data
, u32 attr
, int channel
)
270 case hwmon_fan_input
:
271 case hwmon_fan_fault
:
273 case hwmon_fan_pulses
:
280 static int max6639_read_pwm(struct device
*dev
, u32 attr
, int channel
,
283 u32 regs
[2] = { MAX6639_REG_FAN_CONFIG3(channel
), MAX6639_REG_GCONFIG
};
284 struct max6639_data
*data
= dev_get_drvdata(dev
);
291 case hwmon_pwm_input
:
292 res
= regmap_read(data
->regmap
, MAX6639_REG_TARGTDUTY(channel
), &val
);
295 *pwm_val
= val
* 255 / 120;
298 res
= regmap_multi_reg_read(data
->regmap
, regs
, regvals
, 2);
301 i
= regvals
[0] & MAX6639_FAN_CONFIG3_FREQ_MASK
;
302 if (regvals
[1] & MAX6639_GCONFIG_PWM_FREQ_HI
)
304 *pwm_val
= freq_table
[i
];
311 static int max6639_write_pwm(struct device
*dev
, u32 attr
, int channel
,
314 struct max6639_data
*data
= dev_get_drvdata(dev
);
319 case hwmon_pwm_input
:
320 if (val
< 0 || val
> 255)
322 err
= regmap_write(data
->regmap
, MAX6639_REG_TARGTDUTY(channel
),
326 val
= clamp_val(val
, 0, 25000);
328 i
= find_closest(val
, freq_table
, ARRAY_SIZE(freq_table
));
330 mutex_lock(&data
->update_lock
);
331 err
= regmap_update_bits(data
->regmap
, MAX6639_REG_FAN_CONFIG3(channel
),
332 MAX6639_FAN_CONFIG3_FREQ_MASK
, i
);
334 mutex_unlock(&data
->update_lock
);
339 err
= regmap_set_bits(data
->regmap
, MAX6639_REG_GCONFIG
,
340 MAX6639_GCONFIG_PWM_FREQ_HI
);
342 err
= regmap_clear_bits(data
->regmap
, MAX6639_REG_GCONFIG
,
343 MAX6639_GCONFIG_PWM_FREQ_HI
);
345 mutex_unlock(&data
->update_lock
);
352 static umode_t
max6639_pwm_is_visible(const void *_data
, u32 attr
, int channel
)
355 case hwmon_pwm_input
:
363 static int max6639_read_temp(struct device
*dev
, u32 attr
, int channel
,
370 case hwmon_temp_input
:
371 res
= max6639_temp_read_input(dev
, channel
, val
);
373 case hwmon_temp_fault
:
374 res
= max6639_temp_read_fault(dev
, channel
, val
);
377 res
= max6639_temp_read_max(dev
, channel
, val
);
379 case hwmon_temp_crit
:
380 res
= max6639_temp_read_crit(dev
, channel
, val
);
382 case hwmon_temp_emergency
:
383 res
= max6639_temp_read_emergency(dev
, channel
, val
);
385 case hwmon_temp_max_alarm
:
386 res
= max6639_get_status(dev
, &status
);
389 *val
= !!(status
& BIT(3 - channel
));
391 case hwmon_temp_crit_alarm
:
392 res
= max6639_get_status(dev
, &status
);
395 *val
= !!(status
& BIT(7 - channel
));
397 case hwmon_temp_emergency_alarm
:
398 res
= max6639_get_status(dev
, &status
);
401 *val
= !!(status
& BIT(5 - channel
));
408 static int max6639_write_temp(struct device
*dev
, u32 attr
, int channel
,
411 struct max6639_data
*data
= dev_get_drvdata(dev
);
415 return max6639_temp_set_max(data
, channel
, val
);
416 case hwmon_temp_crit
:
417 return max6639_temp_set_crit(data
, channel
, val
);
418 case hwmon_temp_emergency
:
419 return max6639_temp_set_emergency(data
, channel
, val
);
425 static umode_t
max6639_temp_is_visible(const void *_data
, u32 attr
, int channel
)
428 case hwmon_temp_input
:
429 case hwmon_temp_fault
:
430 case hwmon_temp_max_alarm
:
431 case hwmon_temp_crit_alarm
:
432 case hwmon_temp_emergency_alarm
:
435 case hwmon_temp_crit
:
436 case hwmon_temp_emergency
:
443 static int max6639_read(struct device
*dev
, enum hwmon_sensor_types type
,
444 u32 attr
, int channel
, long *val
)
448 return max6639_read_fan(dev
, attr
, channel
, val
);
450 return max6639_read_pwm(dev
, attr
, channel
, val
);
452 return max6639_read_temp(dev
, attr
, channel
, val
);
458 static int max6639_write(struct device
*dev
, enum hwmon_sensor_types type
,
459 u32 attr
, int channel
, long val
)
463 return max6639_write_fan(dev
, attr
, channel
, val
);
465 return max6639_write_pwm(dev
, attr
, channel
, val
);
467 return max6639_write_temp(dev
, attr
, channel
, val
);
473 static umode_t
max6639_is_visible(const void *data
,
474 enum hwmon_sensor_types type
,
475 u32 attr
, int channel
)
479 return max6639_fan_is_visible(data
, attr
, channel
);
481 return max6639_pwm_is_visible(data
, attr
, channel
);
483 return max6639_temp_is_visible(data
, attr
, channel
);
489 static const struct hwmon_channel_info
* const max6639_info
[] = {
490 HWMON_CHANNEL_INFO(fan
,
491 HWMON_F_INPUT
| HWMON_F_FAULT
| HWMON_F_PULSES
,
492 HWMON_F_INPUT
| HWMON_F_FAULT
| HWMON_F_PULSES
),
493 HWMON_CHANNEL_INFO(pwm
,
494 HWMON_PWM_INPUT
| HWMON_PWM_FREQ
,
495 HWMON_PWM_INPUT
| HWMON_PWM_FREQ
),
496 HWMON_CHANNEL_INFO(temp
,
497 HWMON_T_INPUT
| HWMON_T_FAULT
| HWMON_T_MAX
| HWMON_T_MAX_ALARM
|
498 HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
| HWMON_T_EMERGENCY
|
499 HWMON_T_EMERGENCY_ALARM
,
500 HWMON_T_INPUT
| HWMON_T_FAULT
| HWMON_T_MAX
| HWMON_T_MAX_ALARM
|
501 HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
| HWMON_T_EMERGENCY
|
502 HWMON_T_EMERGENCY_ALARM
),
506 static const struct hwmon_ops max6639_hwmon_ops
= {
507 .is_visible
= max6639_is_visible
,
508 .read
= max6639_read
,
509 .write
= max6639_write
,
512 static const struct hwmon_chip_info max6639_chip_info
= {
513 .ops
= &max6639_hwmon_ops
,
514 .info
= max6639_info
,
518 * returns respective index in rpm_ranges table
519 * 1 by default on invalid range
521 static int rpm_range_to_reg(int range
)
525 for (i
= 0; i
< ARRAY_SIZE(rpm_ranges
); i
++) {
526 if (rpm_ranges
[i
] == range
)
530 return 1; /* default: 4000 RPM */
533 static int max6639_probe_child_from_dt(struct i2c_client
*client
,
534 struct device_node
*child
,
535 struct max6639_data
*data
)
538 struct device
*dev
= &client
->dev
;
542 err
= of_property_read_u32(child
, "reg", &i
);
544 dev_err(dev
, "missing reg property of %pOFn\n", child
);
549 dev_err(dev
, "Invalid fan index reg %d\n", i
);
553 err
= of_property_read_u32(child
, "pulses-per-revolution", &val
);
555 if (val
< 1 || val
> 5) {
556 dev_err(dev
, "invalid pulses-per-revolution %d of %pOFn\n", val
, child
);
562 err
= of_property_read_u32(child
, "max-rpm", &val
);
564 data
->rpm_range
[i
] = rpm_range_to_reg(val
);
569 static int max6639_init_client(struct i2c_client
*client
,
570 struct max6639_data
*data
)
572 struct device
*dev
= &client
->dev
;
573 const struct device_node
*np
= dev
->of_node
;
574 struct device_node
*child
;
577 /* Reset chip to default values, see below for GCONFIG setup */
578 err
= regmap_write(data
->regmap
, MAX6639_REG_GCONFIG
, MAX6639_GCONFIG_POR
);
582 /* Fans pulse per revolution is 2 by default */
586 /* default: 4000 RPM */
587 data
->rpm_range
[0] = 1;
588 data
->rpm_range
[1] = 1;
590 for_each_child_of_node(np
, child
) {
591 if (strcmp(child
->name
, "fan"))
594 err
= max6639_probe_child_from_dt(client
, child
, data
);
601 for (i
= 0; i
< MAX6639_NUM_CHANNELS
; i
++) {
602 err
= regmap_set_bits(data
->regmap
, MAX6639_REG_OUTPUT_MASK
, BIT(1 - i
));
606 /* Set Fan pulse per revolution */
607 err
= max6639_set_ppr(data
, i
, data
->ppr
[i
]);
611 /* Fans config PWM, RPM */
612 err
= regmap_write(data
->regmap
, MAX6639_REG_FAN_CONFIG1(i
),
613 MAX6639_FAN_CONFIG1_PWM
| data
->rpm_range
[i
]);
617 /* Fans PWM polarity high by default */
618 err
= regmap_write(data
->regmap
, MAX6639_REG_FAN_CONFIG2a(i
), 0x00);
623 * /THERM full speed enable,
624 * PWM frequency 25kHz, see also GCONFIG below
626 err
= regmap_write(data
->regmap
, MAX6639_REG_FAN_CONFIG3(i
),
627 MAX6639_FAN_CONFIG3_THERM_FULL_SPEED
| 0x03);
631 /* Max. temp. 80C/90C/100C */
632 err
= regmap_write(data
->regmap
, MAX6639_REG_THERM_LIMIT(i
), 80);
635 err
= regmap_write(data
->regmap
, MAX6639_REG_ALERT_LIMIT(i
), 90);
638 err
= regmap_write(data
->regmap
, MAX6639_REG_OT_LIMIT(i
), 100);
642 /* PWM 120/120 (i.e. 100%) */
643 err
= regmap_write(data
->regmap
, MAX6639_REG_TARGTDUTY(i
), 120);
647 /* Start monitoring */
648 return regmap_write(data
->regmap
, MAX6639_REG_GCONFIG
,
649 MAX6639_GCONFIG_DISABLE_TIMEOUT
| MAX6639_GCONFIG_CH2_LOCAL
|
650 MAX6639_GCONFIG_PWM_FREQ_HI
);
654 /* Return 0 if detection is successful, -ENODEV otherwise */
655 static int max6639_detect(struct i2c_client
*client
,
656 struct i2c_board_info
*info
)
658 struct i2c_adapter
*adapter
= client
->adapter
;
661 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
664 /* Actual detection via device and manufacturer ID */
665 dev_id
= i2c_smbus_read_byte_data(client
, MAX6639_REG_DEVID
);
666 manu_id
= i2c_smbus_read_byte_data(client
, MAX6639_REG_MANUID
);
667 if (dev_id
!= 0x58 || manu_id
!= 0x4D)
670 strscpy(info
->type
, "max6639", I2C_NAME_SIZE
);
675 static void max6639_regulator_disable(void *data
)
677 regulator_disable(data
);
680 static bool max6639_regmap_is_volatile(struct device
*dev
, unsigned int reg
)
683 case MAX6639_REG_TEMP(0):
684 case MAX6639_REG_TEMP_EXT(0):
685 case MAX6639_REG_TEMP(1):
686 case MAX6639_REG_TEMP_EXT(1):
687 case MAX6639_REG_STATUS
:
688 case MAX6639_REG_FAN_CNT(0):
689 case MAX6639_REG_FAN_CNT(1):
690 case MAX6639_REG_TARGTDUTY(0):
691 case MAX6639_REG_TARGTDUTY(1):
698 static const struct regmap_config max6639_regmap_config
= {
701 .max_register
= MAX6639_REG_DEVREV
,
702 .cache_type
= REGCACHE_MAPLE
,
703 .volatile_reg
= max6639_regmap_is_volatile
,
706 static int max6639_probe(struct i2c_client
*client
)
708 struct device
*dev
= &client
->dev
;
709 struct max6639_data
*data
;
710 struct device
*hwmon_dev
;
713 data
= devm_kzalloc(dev
, sizeof(struct max6639_data
), GFP_KERNEL
);
717 data
->regmap
= devm_regmap_init_i2c(client
, &max6639_regmap_config
);
718 if (IS_ERR(data
->regmap
))
719 return dev_err_probe(dev
,
720 PTR_ERR(data
->regmap
),
721 "regmap initialization failed\n");
723 data
->reg
= devm_regulator_get_optional(dev
, "fan");
724 if (IS_ERR(data
->reg
)) {
725 if (PTR_ERR(data
->reg
) != -ENODEV
)
726 return PTR_ERR(data
->reg
);
731 err
= regulator_enable(data
->reg
);
733 dev_err(dev
, "Failed to enable fan supply: %d\n", err
);
736 err
= devm_add_action_or_reset(dev
, max6639_regulator_disable
,
739 dev_err(dev
, "Failed to register action: %d\n", err
);
744 mutex_init(&data
->update_lock
);
746 /* Initialize the max6639 chip */
747 err
= max6639_init_client(client
, data
);
751 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
752 data
, &max6639_chip_info
,
755 return PTR_ERR_OR_ZERO(hwmon_dev
);
758 static int max6639_suspend(struct device
*dev
)
760 struct max6639_data
*data
= dev_get_drvdata(dev
);
763 regulator_disable(data
->reg
);
765 return regmap_write_bits(data
->regmap
, MAX6639_REG_GCONFIG
, MAX6639_GCONFIG_STANDBY
,
766 MAX6639_GCONFIG_STANDBY
);
769 static int max6639_resume(struct device
*dev
)
771 struct max6639_data
*data
= dev_get_drvdata(dev
);
775 ret
= regulator_enable(data
->reg
);
777 dev_err(dev
, "Failed to enable fan supply: %d\n", ret
);
782 return regmap_write_bits(data
->regmap
, MAX6639_REG_GCONFIG
, MAX6639_GCONFIG_STANDBY
,
783 ~MAX6639_GCONFIG_STANDBY
);
786 static const struct i2c_device_id max6639_id
[] = {
791 MODULE_DEVICE_TABLE(i2c
, max6639_id
);
793 static DEFINE_SIMPLE_DEV_PM_OPS(max6639_pm_ops
, max6639_suspend
, max6639_resume
);
795 static const struct of_device_id max6639_of_match
[] = {
796 { .compatible
= "maxim,max6639", },
800 static struct i2c_driver max6639_driver
= {
801 .class = I2C_CLASS_HWMON
,
804 .pm
= pm_sleep_ptr(&max6639_pm_ops
),
805 .of_match_table
= max6639_of_match
,
807 .probe
= max6639_probe
,
808 .id_table
= max6639_id
,
809 .detect
= max6639_detect
,
810 .address_list
= normal_i2c
,
813 module_i2c_driver(max6639_driver
);
815 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
816 MODULE_DESCRIPTION("max6639 driver");
817 MODULE_LICENSE("GPL");