1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * max6650.c - Part of lm_sensors, Linux kernel modules for hardware
6 * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de>
8 * based on code written by John Morris <john.morris@spirentcom.com>
9 * Copyright (c) 2003 Spirent Communications
10 * and Claus Gindhart <claus.gindhart@kontron.com>
12 * This module has only been tested with the MAX6650 chip. It should
13 * also work with the MAX6651. It does not distinguish max6650 and max6651
16 * The datasheet was last seen at:
18 * http://pdfserv.maxim-ic.com/en/ds/MAX6650-MAX6651.pdf
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/of_device.h>
30 #include <linux/thermal.h>
36 /* fan_voltage: 5=5V fan, 12=12V fan, 0=don't change */
37 static int fan_voltage;
38 /* prescaler: Possible values are 1, 2, 4, 8, 16 or 0 for don't change */
40 /* clock: The clock frequency of the chip (max6651 can be clocked externally) */
41 static int clock = 254000;
43 module_param(fan_voltage, int, 0444);
44 module_param(prescaler, int, 0444);
45 module_param(clock, int, 0444);
48 * MAX 6650/6651 registers
51 #define MAX6650_REG_SPEED 0x00
52 #define MAX6650_REG_CONFIG 0x02
53 #define MAX6650_REG_GPIO_DEF 0x04
54 #define MAX6650_REG_DAC 0x06
55 #define MAX6650_REG_ALARM_EN 0x08
56 #define MAX6650_REG_ALARM 0x0A
57 #define MAX6650_REG_TACH0 0x0C
58 #define MAX6650_REG_TACH1 0x0E
59 #define MAX6650_REG_TACH2 0x10
60 #define MAX6650_REG_TACH3 0x12
61 #define MAX6650_REG_GPIO_STAT 0x14
62 #define MAX6650_REG_COUNT 0x16
65 * Config register bits
68 #define MAX6650_CFG_V12 0x08
69 #define MAX6650_CFG_PRESCALER_MASK 0x07
70 #define MAX6650_CFG_PRESCALER_2 0x01
71 #define MAX6650_CFG_PRESCALER_4 0x02
72 #define MAX6650_CFG_PRESCALER_8 0x03
73 #define MAX6650_CFG_PRESCALER_16 0x04
74 #define MAX6650_CFG_MODE_MASK 0x30
75 #define MAX6650_CFG_MODE_ON 0x00
76 #define MAX6650_CFG_MODE_OFF 0x10
77 #define MAX6650_CFG_MODE_CLOSED_LOOP 0x20
78 #define MAX6650_CFG_MODE_OPEN_LOOP 0x30
79 #define MAX6650_COUNT_MASK 0x03
82 * Alarm status register bits
85 #define MAX6650_ALRM_MAX 0x01
86 #define MAX6650_ALRM_MIN 0x02
87 #define MAX6650_ALRM_TACH 0x04
88 #define MAX6650_ALRM_GPIO1 0x08
89 #define MAX6650_ALRM_GPIO2 0x10
91 /* Minimum and maximum values of the FAN-RPM */
92 #define FAN_RPM_MIN 240
93 #define FAN_RPM_MAX 30000
95 #define DIV_FROM_REG(reg) (1 << ((reg) & 7))
96 #define DAC_LIMIT(v12) ((v12) ? 180 : 76)
99 * Client data (each client gets its own)
102 struct max6650_data {
103 struct i2c_client *client;
104 const struct attribute_group *groups[3];
105 struct mutex update_lock;
107 bool valid; /* false until following fields are valid */
108 unsigned long last_updated; /* in jiffies */
110 /* register values */
117 unsigned long cooling_dev_state;
120 static const u8 tach_reg[] = {
127 static const struct of_device_id __maybe_unused max6650_dt_match[] = {
129 .compatible = "maxim,max6650",
133 .compatible = "maxim,max6651",
138 MODULE_DEVICE_TABLE(of, max6650_dt_match);
140 static int dac_to_pwm(int dac, bool v12)
143 * Useful range for dac is 0-180 for 12V fans and 0-76 for 5V fans.
144 * Lower DAC values mean higher speeds.
146 return clamp_val(255 - (255 * dac) / DAC_LIMIT(v12), 0, 255);
149 static u8 pwm_to_dac(unsigned int pwm, bool v12)
151 int limit = DAC_LIMIT(v12);
153 return limit - (limit * pwm) / 255;
156 static struct max6650_data *max6650_update_device(struct device *dev)
158 struct max6650_data *data = dev_get_drvdata(dev);
159 struct i2c_client *client = data->client;
162 mutex_lock(&data->update_lock);
164 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
165 data->speed = i2c_smbus_read_byte_data(client,
167 data->config = i2c_smbus_read_byte_data(client,
169 for (i = 0; i < data->nr_fans; i++) {
170 data->tach[i] = i2c_smbus_read_byte_data(client,
173 data->count = i2c_smbus_read_byte_data(client,
175 data->dac = i2c_smbus_read_byte_data(client, MAX6650_REG_DAC);
178 * Alarms are cleared on read in case the condition that
179 * caused the alarm is removed. Keep the value latched here
180 * for providing the register through different alarm files.
182 data->alarm |= i2c_smbus_read_byte_data(client,
185 data->last_updated = jiffies;
189 mutex_unlock(&data->update_lock);
195 * Change the operating mode of the chip (if needed).
196 * mode is one of the MAX6650_CFG_MODE_* values.
198 static int max6650_set_operating_mode(struct max6650_data *data, u8 mode)
201 u8 config = data->config;
203 if (mode == (config & MAX6650_CFG_MODE_MASK))
206 config = (config & ~MAX6650_CFG_MODE_MASK) | mode;
208 result = i2c_smbus_write_byte_data(data->client, MAX6650_REG_CONFIG,
213 data->config = config;
218 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
221 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
222 struct max6650_data *data = max6650_update_device(dev);
226 * Calculation details:
228 * Each tachometer counts over an interval given by the "count"
229 * register (0.25, 0.5, 1 or 2 seconds). This module assumes
230 * that the fans produce two pulses per revolution (this seems
231 * to be the most common).
234 rpm = ((data->tach[attr->index] * 120) / DIV_FROM_REG(data->count));
235 return sprintf(buf, "%d\n", rpm);
239 * Set the fan speed to the specified RPM (or read back the RPM setting).
240 * This works in closed loop mode only. Use pwm1 for open loop speed setting.
242 * The MAX6650/1 will automatically control fan speed when in closed loop
247 * 1) The MAX6650/1 internal 254kHz clock frequency is set correctly. Use
248 * the clock module parameter if you need to fine tune this.
250 * 2) The prescaler (low three bits of the config register) has already
251 * been set to an appropriate value. Use the prescaler module parameter
252 * if your BIOS doesn't initialize the chip properly.
254 * The relevant equations are given on pages 21 and 22 of the datasheet.
256 * From the datasheet, the relevant equation when in regulation is:
258 * [fCLK / (128 x (KTACH + 1))] = 2 x FanSpeed / KSCALE
262 * fCLK is the oscillator frequency (either the 254kHz internal
263 * oscillator or the externally applied clock)
265 * KTACH is the value in the speed register
267 * FanSpeed is the speed of the fan in rps
269 * KSCALE is the prescaler value (1, 2, 4, 8, or 16)
271 * When reading, we need to solve for FanSpeed. When writing, we need to
274 * Note: this tachometer is completely separate from the tachometers
275 * used to measure the fan speeds. Only one fan's speed (fan1) is
279 static ssize_t fan1_target_show(struct device *dev,
280 struct device_attribute *devattr, char *buf)
282 struct max6650_data *data = max6650_update_device(dev);
283 int kscale, ktach, rpm;
286 * Use the datasheet equation:
288 * FanSpeed = KSCALE x fCLK / [256 x (KTACH + 1)]
290 * then multiply by 60 to give rpm.
293 kscale = DIV_FROM_REG(data->config);
295 rpm = 60 * kscale * clock / (256 * (ktach + 1));
296 return sprintf(buf, "%d\n", rpm);
299 static int max6650_set_target(struct max6650_data *data, unsigned long rpm)
304 return max6650_set_operating_mode(data, MAX6650_CFG_MODE_OFF);
306 rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
309 * Divide the required speed by 60 to get from rpm to rps, then
310 * use the datasheet equation:
312 * KTACH = [(fCLK x KSCALE) / (256 x FanSpeed)] - 1
315 kscale = DIV_FROM_REG(data->config);
316 ktach = ((clock * kscale) / (256 * rpm / 60)) - 1;
323 return i2c_smbus_write_byte_data(data->client, MAX6650_REG_SPEED,
327 static ssize_t fan1_target_store(struct device *dev,
328 struct device_attribute *devattr,
329 const char *buf, size_t count)
331 struct max6650_data *data = dev_get_drvdata(dev);
335 err = kstrtoul(buf, 10, &rpm);
339 mutex_lock(&data->update_lock);
341 err = max6650_set_target(data, rpm);
343 mutex_unlock(&data->update_lock);
352 * Get/set the fan speed in open loop mode using pwm1 sysfs file.
353 * Speed is given as a relative value from 0 to 255, where 255 is maximum
354 * speed. Note that this is done by writing directly to the chip's DAC,
355 * it won't change the closed loop speed set by fan1_target.
356 * Also note that due to rounding errors it is possible that you don't read
357 * back exactly the value you have set.
360 static ssize_t pwm1_show(struct device *dev, struct device_attribute *devattr,
363 struct max6650_data *data = max6650_update_device(dev);
365 return sprintf(buf, "%d\n", dac_to_pwm(data->dac,
366 data->config & MAX6650_CFG_V12));
369 static ssize_t pwm1_store(struct device *dev,
370 struct device_attribute *devattr, const char *buf,
373 struct max6650_data *data = dev_get_drvdata(dev);
374 struct i2c_client *client = data->client;
379 err = kstrtoul(buf, 10, &pwm);
383 pwm = clamp_val(pwm, 0, 255);
385 mutex_lock(&data->update_lock);
386 dac = pwm_to_dac(pwm, data->config & MAX6650_CFG_V12);
387 err = i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, dac);
390 mutex_unlock(&data->update_lock);
392 return err < 0 ? err : count;
396 * Get/Set controller mode:
399 * 1 = Open loop, Voltage is set according to speed, not regulated.
400 * 2 = Closed loop, RPM for all fans regulated by fan1 tachometer
403 static ssize_t pwm1_enable_show(struct device *dev,
404 struct device_attribute *devattr, char *buf)
406 struct max6650_data *data = max6650_update_device(dev);
407 int mode = (data->config & MAX6650_CFG_MODE_MASK) >> 4;
408 int sysfs_modes[4] = {0, 3, 2, 1};
410 return sprintf(buf, "%d\n", sysfs_modes[mode]);
413 static ssize_t pwm1_enable_store(struct device *dev,
414 struct device_attribute *devattr,
415 const char *buf, size_t count)
417 struct max6650_data *data = dev_get_drvdata(dev);
420 const u8 max6650_modes[] = {
422 MAX6650_CFG_MODE_OPEN_LOOP,
423 MAX6650_CFG_MODE_CLOSED_LOOP,
424 MAX6650_CFG_MODE_OFF,
427 err = kstrtoul(buf, 10, &mode);
431 if (mode >= ARRAY_SIZE(max6650_modes))
434 mutex_lock(&data->update_lock);
436 max6650_set_operating_mode(data, max6650_modes[mode]);
438 mutex_unlock(&data->update_lock);
444 * Read/write functions for fan1_div sysfs file. The MAX6650 has no such
445 * divider. We handle this by converting between divider and counttime:
447 * (counttime == k) <==> (divider == 2^k), k = 0, 1, 2, or 3
449 * Lower values of k allow to connect a faster fan without the risk of
450 * counter overflow. The price is lower resolution. You can also set counttime
451 * using the module parameter. Note that the module parameter "prescaler" also
452 * influences the behaviour. Unfortunately, there's no sysfs attribute
453 * defined for that. See the data sheet for details.
456 static ssize_t fan1_div_show(struct device *dev,
457 struct device_attribute *devattr, char *buf)
459 struct max6650_data *data = max6650_update_device(dev);
461 return sprintf(buf, "%d\n", DIV_FROM_REG(data->count));
464 static ssize_t fan1_div_store(struct device *dev,
465 struct device_attribute *devattr,
466 const char *buf, size_t count)
468 struct max6650_data *data = dev_get_drvdata(dev);
469 struct i2c_client *client = data->client;
473 err = kstrtoul(buf, 10, &div);
477 mutex_lock(&data->update_lock);
492 mutex_unlock(&data->update_lock);
496 i2c_smbus_write_byte_data(client, MAX6650_REG_COUNT, data->count);
497 mutex_unlock(&data->update_lock);
509 static ssize_t alarm_show(struct device *dev,
510 struct device_attribute *devattr, char *buf)
512 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
513 struct max6650_data *data = max6650_update_device(dev);
514 struct i2c_client *client = data->client;
517 if (data->alarm & attr->index) {
518 mutex_lock(&data->update_lock);
520 data->alarm &= ~attr->index;
521 data->alarm |= i2c_smbus_read_byte_data(client,
523 mutex_unlock(&data->update_lock);
526 return sprintf(buf, "%d\n", alarm);
529 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
530 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
531 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
532 static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
533 static DEVICE_ATTR_RW(fan1_target);
534 static DEVICE_ATTR_RW(fan1_div);
535 static DEVICE_ATTR_RW(pwm1_enable);
536 static DEVICE_ATTR_RW(pwm1);
537 static SENSOR_DEVICE_ATTR_RO(fan1_max_alarm, alarm, MAX6650_ALRM_MAX);
538 static SENSOR_DEVICE_ATTR_RO(fan1_min_alarm, alarm, MAX6650_ALRM_MIN);
539 static SENSOR_DEVICE_ATTR_RO(fan1_fault, alarm, MAX6650_ALRM_TACH);
540 static SENSOR_DEVICE_ATTR_RO(gpio1_alarm, alarm, MAX6650_ALRM_GPIO1);
541 static SENSOR_DEVICE_ATTR_RO(gpio2_alarm, alarm, MAX6650_ALRM_GPIO2);
543 static umode_t max6650_attrs_visible(struct kobject *kobj, struct attribute *a,
546 struct device *dev = container_of(kobj, struct device, kobj);
547 struct max6650_data *data = dev_get_drvdata(dev);
548 struct i2c_client *client = data->client;
549 u8 alarm_en = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM_EN);
550 struct device_attribute *devattr;
553 * Hide the alarms that have not been enabled by the firmware
556 devattr = container_of(a, struct device_attribute, attr);
557 if (devattr == &sensor_dev_attr_fan1_max_alarm.dev_attr
558 || devattr == &sensor_dev_attr_fan1_min_alarm.dev_attr
559 || devattr == &sensor_dev_attr_fan1_fault.dev_attr
560 || devattr == &sensor_dev_attr_gpio1_alarm.dev_attr
561 || devattr == &sensor_dev_attr_gpio2_alarm.dev_attr) {
562 if (!(alarm_en & to_sensor_dev_attr(devattr)->index))
569 static struct attribute *max6650_attrs[] = {
570 &sensor_dev_attr_fan1_input.dev_attr.attr,
571 &dev_attr_fan1_target.attr,
572 &dev_attr_fan1_div.attr,
573 &dev_attr_pwm1_enable.attr,
575 &sensor_dev_attr_fan1_max_alarm.dev_attr.attr,
576 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
577 &sensor_dev_attr_fan1_fault.dev_attr.attr,
578 &sensor_dev_attr_gpio1_alarm.dev_attr.attr,
579 &sensor_dev_attr_gpio2_alarm.dev_attr.attr,
583 static const struct attribute_group max6650_group = {
584 .attrs = max6650_attrs,
585 .is_visible = max6650_attrs_visible,
588 static struct attribute *max6651_attrs[] = {
589 &sensor_dev_attr_fan2_input.dev_attr.attr,
590 &sensor_dev_attr_fan3_input.dev_attr.attr,
591 &sensor_dev_attr_fan4_input.dev_attr.attr,
595 static const struct attribute_group max6651_group = {
596 .attrs = max6651_attrs,
603 static int max6650_init_client(struct max6650_data *data,
604 struct i2c_client *client)
606 struct device *dev = &client->dev;
613 if (of_property_read_u32(dev->of_node, "maxim,fan-microvolt",
615 voltage = fan_voltage;
617 voltage /= 1000000; /* Microvolts to volts */
618 if (of_property_read_u32(dev->of_node, "maxim,fan-prescale",
620 prescale = prescaler;
622 reg = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG);
624 dev_err(dev, "Error reading config register, aborting.\n");
632 reg &= ~MAX6650_CFG_V12;
635 reg |= MAX6650_CFG_V12;
638 dev_err(dev, "illegal value for fan_voltage (%d)\n", voltage);
645 reg &= ~MAX6650_CFG_PRESCALER_MASK;
648 reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
649 | MAX6650_CFG_PRESCALER_2;
652 reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
653 | MAX6650_CFG_PRESCALER_4;
656 reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
657 | MAX6650_CFG_PRESCALER_8;
660 reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
661 | MAX6650_CFG_PRESCALER_16;
664 dev_err(dev, "illegal value for prescaler (%d)\n", prescale);
667 dev_info(dev, "Fan voltage: %dV, prescaler: %d.\n",
668 (reg & MAX6650_CFG_V12) ? 12 : 5,
669 1 << (reg & MAX6650_CFG_PRESCALER_MASK));
671 err = i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, reg);
673 dev_err(dev, "Config write error, aborting.\n");
678 reg = i2c_smbus_read_byte_data(client, MAX6650_REG_COUNT);
680 dev_err(dev, "Failed to read count register, aborting.\n");
685 if (!of_property_read_u32(client->dev.of_node, "maxim,fan-target-rpm",
687 max6650_set_target(data, target_rpm);
688 max6650_set_operating_mode(data, MAX6650_CFG_MODE_CLOSED_LOOP);
694 #if IS_ENABLED(CONFIG_THERMAL)
696 static int max6650_get_max_state(struct thermal_cooling_device *cdev,
697 unsigned long *state)
704 static int max6650_get_cur_state(struct thermal_cooling_device *cdev,
705 unsigned long *state)
707 struct max6650_data *data = cdev->devdata;
709 *state = data->cooling_dev_state;
714 static int max6650_set_cur_state(struct thermal_cooling_device *cdev,
717 struct max6650_data *data = cdev->devdata;
718 struct i2c_client *client = data->client;
721 state = clamp_val(state, 0, 255);
723 mutex_lock(&data->update_lock);
725 data->dac = pwm_to_dac(state, data->config & MAX6650_CFG_V12);
726 err = i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, data->dac);
729 max6650_set_operating_mode(data, state ?
730 MAX6650_CFG_MODE_OPEN_LOOP :
731 MAX6650_CFG_MODE_OFF);
732 data->cooling_dev_state = state;
735 mutex_unlock(&data->update_lock);
740 static const struct thermal_cooling_device_ops max6650_cooling_ops = {
741 .get_max_state = max6650_get_max_state,
742 .get_cur_state = max6650_get_cur_state,
743 .set_cur_state = max6650_set_cur_state,
747 static int max6650_probe(struct i2c_client *client,
748 const struct i2c_device_id *id)
750 struct thermal_cooling_device *cooling_dev;
751 struct device *dev = &client->dev;
752 const struct of_device_id *of_id =
753 of_match_device(of_match_ptr(max6650_dt_match), dev);
754 struct max6650_data *data;
755 struct device *hwmon_dev;
758 data = devm_kzalloc(dev, sizeof(struct max6650_data), GFP_KERNEL);
762 data->client = client;
763 i2c_set_clientdata(client, data);
764 mutex_init(&data->update_lock);
765 data->nr_fans = of_id ? (int)(uintptr_t)of_id->data : id->driver_data;
768 * Initialize the max6650 chip
770 err = max6650_init_client(data, client);
774 data->groups[0] = &max6650_group;
775 /* 3 additional fan inputs for the MAX6651 */
776 if (data->nr_fans == 4)
777 data->groups[1] = &max6651_group;
779 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
782 err = PTR_ERR_OR_ZERO(hwmon_dev);
786 #if IS_ENABLED(CONFIG_THERMAL)
787 cooling_dev = devm_thermal_of_cooling_device_register(dev, dev->of_node,
788 client->name, data, &max6650_cooling_ops);
789 if (IS_ERR(cooling_dev)) {
790 dev_warn(dev, "thermal cooling device register failed: %ld\n",
791 PTR_ERR(cooling_dev));
797 static const struct i2c_device_id max6650_id[] = {
802 MODULE_DEVICE_TABLE(i2c, max6650_id);
804 static struct i2c_driver max6650_driver = {
807 .of_match_table = of_match_ptr(max6650_dt_match),
809 .probe = max6650_probe,
810 .id_table = max6650_id,
813 module_i2c_driver(max6650_driver);
815 MODULE_AUTHOR("Hans J. Koch");
816 MODULE_DESCRIPTION("MAX6650 sensor driver");
817 MODULE_LICENSE("GPL");