1 From 5ea2e152d846bf60901107fefd81a58f792f3bc2 Mon Sep 17 00:00:00 2001
2 From: Christian Lamparter <chunkeey@gmail.com>
3 Date: Fri, 10 Jun 2016 03:00:46 +0200
4 Subject: [PATCH] hwmon: add driver for Microchip TC654/TC655 PWM fan
7 This patch adds a hwmon driver for the Microchip TC654 and TC655
8 Dual SMBus PWM Fan Speed Controllers with Fan Fault detection.
10 The chip is described in the DS2001734C Spec Document from Microchip.
12 - Shared PWM Fan Drive for two fans
14 - automatic PWM controller (needs additional
16 - Overtemperature alarm (when using NTC/PTC
19 Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
21 drivers/hwmon/Kconfig | 10 +
22 drivers/hwmon/Makefile | 1 +
23 drivers/hwmon/tc654.c | 969 +++++++++++++++++++++++++++++++++++++++++++++++++
24 3 files changed, 980 insertions(+)
25 create mode 100644 drivers/hwmon/tc654.c
27 diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
28 index ff94007..941fe4951 100644
29 --- a/drivers/hwmon/Kconfig
30 +++ b/drivers/hwmon/Kconfig
31 @@ -1514,6 +1514,16 @@ config SENSORS_INA2XX
32 This driver can also be built as a module. If so, the module
33 will be called ina2xx.
36 + tristate "Microchip TC654 and TC655"
39 + If you say yes here you get support for Microchip TC655 and TC654
40 + Dual PWM Fan Speed Controllers and sensor chips.
42 + This driver can also be built as a module. If so, the module
43 + will be called tc654.
46 tristate "Microchip TC74"
48 diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
49 index 2ef5b7c..04270c7 100644
50 --- a/drivers/hwmon/Makefile
51 +++ b/drivers/hwmon/Makefile
52 @@ -145,6 +145,7 @@ obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.o
53 obj-$(CONFIG_SENSORS_SMSC47M1) += smsc47m1.o
54 obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
55 obj-$(CONFIG_SENSORS_AMC6821) += amc6821.o
56 +obj-$(CONFIG_SENSORS_TC654) += tc654.o
57 obj-$(CONFIG_SENSORS_TC74) += tc74.o
58 obj-$(CONFIG_SENSORS_THMC50) += thmc50.o
59 obj-$(CONFIG_SENSORS_TMP102) += tmp102.o
60 diff --git a/drivers/hwmon/tc654.c b/drivers/hwmon/tc654.c
62 index 0000000..d584baf
64 +++ b/drivers/hwmon/tc654.c
67 + * tc654.c - Support for Microchip TC654/TC655
68 + * "A Dual SMBus PWM FAN Speed Controllers with Fan Fault Detection"
70 + * Copyright (c) 2016 Christian Lamparter <chunkeey@gmail.com>
72 + * This program is free software; you can redistribute it and/or modify
73 + * it under the terms of the GNU General Public License as published by
74 + * the Free Software Foundation version 2 of the License.
76 + * This program is distributed in the hope that it will be useful,
77 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
78 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
79 + * GNU General Public License for more details.
81 + * The chip is described in the DS2001734C Spec Document from Microchip.
84 +#include <linux/module.h>
85 +#include <linux/init.h>
86 +#include <linux/slab.h>
87 +#include <linux/jiffies.h>
88 +#include <linux/i2c.h>
89 +#include <linux/hwmon.h>
90 +#include <linux/hwmon-sysfs.h>
91 +#include <linux/err.h>
92 +#include <linux/mutex.h>
93 +#include <linux/thermal.h>
95 +/* Hardware definitions */
96 +/* 5.1.4 Address Byte stats that TC654/TC655 are fixed at 0x1b */
97 +static const unsigned short normal_i2c[] = { 0x1b, I2C_CLIENT_END };
100 + TC654_REG_RPM1 = 0x00,
102 + TC654_REG_FAN1_FAULT_THRESH,
103 + TC654_REG_FAN2_FAULT_THRESH,
106 + TC654_REG_DUTY_CYCLE,
114 +#define TC654_MFR_ID_MICROCHIP 0x84
115 +#define TC654_VER_ID 0x00
116 +#define TC655_VER_ID 0x01
118 +enum TC654_CONTROL_BITS {
119 + TC654_CTRL_SDM = BIT(0),
120 + TC654_CTRL_F1PPR_S = 1,
121 + TC654_CTRL_F1PPR_M = (BIT(1) | BIT(2)),
122 + TC654_CTRL_F2PPR_S = 3,
123 + TC654_CTRL_F2PPR_M = (BIT(3) | BIT(4)),
124 + TC654_CTRL_DUTYC = BIT(5),
125 + TC654_CTRL_RES = BIT(6),
126 + TC654_CTRL_FFCLR = BIT(7),
129 +enum TC654_STATUS_BITS {
130 + TC654_STATUS_F1F = BIT(0),
131 + TC654_STATUS_F2F = BIT(1),
132 + TC654_STATUS_VSTAT = BIT(2),
133 + TC654_STATUS_R1CO = BIT(3),
134 + TC654_STATUS_R2CO = BIT(4),
135 + TC654_STATUS_OTF = BIT(5),
146 +enum TC654_FAN_MODE {
147 + TC654_PWM_OFF, /* Shutdown Mode - switch of both fans */
148 + TC654_PWM_VIN, /* Fans will be controlled via V_in analog input pin */
149 + TC654_PWM_3000, /* sets fans to 30% duty cycle */
151 + TC654_PWM_3933, /* default case - if V_in pin is open */
164 + TC654_PWM_10000, /* sets fans to 100% duty cycle */
168 + TC654_ALARM_FAN1_FAULT,
169 + TC654_ALARM_FAN2_FAULT,
170 + TC654_ALARM_FAN1_COUNTER_OVERFLOW,
171 + TC654_ALARM_FAN2_COUNTER_OVERFLOW,
172 + TC654_ALARM_OVER_TEMPERATURE,
175 + __NUM_TC654_ALARMS,
178 +static const struct pwm_table_entry {
180 + enum TC654_FAN_MODE mode;
182 + { 0, TC654_PWM_OFF },
183 + { 1, TC654_PWM_3000 },
184 + { 88, TC654_PWM_3467 },
185 + {101, TC654_PWM_3933 },
186 + {113, TC654_PWM_4400 },
187 + {125, TC654_PWM_4867 },
188 + {137, TC654_PWM_5333 },
189 + {148, TC654_PWM_5800 },
190 + {160, TC654_PWM_6267 },
191 + {172, TC654_PWM_6733 },
192 + {184, TC654_PWM_7200 },
193 + {196, TC654_PWM_7667 },
194 + {208, TC654_PWM_8133 },
195 + {220, TC654_PWM_8600 },
196 + {232, TC654_PWM_9067 },
197 + {244, TC654_PWM_9533 },
198 + {255, TC654_PWM_10000 },
201 +/* driver context */
203 + struct i2c_client *client;
205 + struct mutex update_lock;
207 + unsigned long last_updated; /* in jiffies */
208 + u8 cached_regs[__TC654_REG_NUM];
210 + bool valid; /* monitored registers are valid */
211 + u16 fan_input[__NUM_TC654_FAN];
212 + bool alarms[__NUM_TC654_ALARMS];
216 + /* optional cooling device */
217 + struct thermal_cooling_device *cdev;
220 +/* hardware accessors and functions */
221 +static int read_tc(struct tc654 *tc, u8 reg)
225 + if (reg <= TC654_REG_VER_ID) {
226 + /* Table 6.1 states that all registers are readable */
227 + status = i2c_smbus_read_byte_data(tc->client, reg);
232 + dev_warn(&tc->client->dev, "can't read register 0x%02x due to error (%d)",
235 + tc->cached_regs[reg] = status;
241 +static int write_tc(struct tc654 *tc, u8 i2c_reg, u8 val)
246 + * Table 6.1 states that both fan threshold registers,
247 + * the Config and Duty Cycle are writeable.
250 + case TC654_REG_FAN1_FAULT_THRESH:
251 + case TC654_REG_FAN2_FAULT_THRESH:
252 + case TC654_REG_DUTY_CYCLE:
253 + case TC654_REG_CONFIG:
254 + status = i2c_smbus_write_byte_data(tc->client, i2c_reg, val);
262 + dev_warn(&tc->client->dev, "can't write register 0x%02x with value 0x%02x due to error (%d)",
263 + i2c_reg, val, status);
265 + tc->cached_regs[i2c_reg] = val;
271 +static int mod_config(struct tc654 *tc, u8 set, u8 clear)
275 + /* a bit can't be set and cleared on the same time. */
279 + /* invalidate data to force re-read from hardware */
281 + val = (tc->cached_regs[TC654_REG_CONFIG] | set) & (~clear);
282 + return write_tc(tc, TC654_REG_CONFIG, val);
285 +static int read_fan_rpm(struct tc654 *tc, enum TC654_FAN fan)
289 + /* 6.1 RPM-OUTPUT1 and RPM-OUTPUT2 registers */
290 + ret = read_tc(tc, fan == TC654_FAN1 ? TC654_REG_RPM1 : TC654_REG_RPM2);
295 + * The Resolution Selection Bit in 6.3 CONFIGURATION REGISTER
296 + * is needed to convert the raw value to the RPM.
297 + * 0 = RPM1 and RPM2 use (8-Bit) resolution => * 50 RPM
298 + * 1 = RPM1 and RPM2 use (9-Bit) resolution => * 25 RPM
300 + return ret * (25 <<
301 + !(tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_RES));
304 +static int write_fan_fault_thresh(struct tc654 *tc, enum TC654_FAN fan,
313 + * 6.2 FAN_FAULT1 and FAN_FAULT2 Threshold registers
315 + * Both registers operate in 50 RPM mode exclusively.
317 + converted_rpm = rpm / 50;
319 + /* invalidate data to force re-read from hardware */
321 + return write_tc(tc, fan == TC654_FAN1 ? TC654_REG_FAN1_FAULT_THRESH :
322 + TC654_REG_FAN2_FAULT_THRESH, converted_rpm);
326 +static int read_fan_fault_thresh(struct tc654 *tc, enum TC654_FAN fan)
329 + * 6.2 FAN_FAULT1 and FAN_FAULT2 Threshold registers
331 + * Both registers operate in 50 RPM mode exclusively.
333 + return read_tc(tc, fan == TC654_FAN1 ? TC654_REG_FAN1_FAULT_THRESH :
334 + TC654_REG_FAN2_FAULT_THRESH) * 50;
337 +static enum TC654_FAN_MODE get_fan_mode(struct tc654 *tc)
339 + if (tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_SDM) {
340 + return TC654_PWM_OFF;
341 + } else if (tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_DUTYC) {
342 + return TC654_PWM_3000 + tc->cached_regs[TC654_REG_DUTY_CYCLE];
343 + } else if (tc->vin_status == 0)
344 + return TC654_PWM_VIN;
349 +static int write_fan_mode(struct tc654 *tc, enum TC654_FAN_MODE mode)
355 + in_sdm = !!(tc->cached_regs[TC654_REG_CONFIG] &
359 + case TC654_PWM_OFF:
363 + /* Enter Shutdown Mode - Switches off all fans */
364 + err = mod_config(tc, TC654_CTRL_SDM, TC654_CTRL_DUTYC);
370 + case TC654_PWM_VIN:
371 + if (tc->vin_status) {
372 + dev_err(&tc->client->dev,
373 + "V_in pin is open, can't enable automatic mode.");
377 + err = mod_config(tc, 0, TC654_CTRL_SDM | TC654_CTRL_DUTYC);
381 + tc->pwm_manual = false;
384 + case TC654_PWM_3000:
385 + case TC654_PWM_3467:
386 + case TC654_PWM_3933:
387 + case TC654_PWM_4400:
388 + case TC654_PWM_4867:
389 + case TC654_PWM_5333:
390 + case TC654_PWM_5800:
391 + case TC654_PWM_6267:
392 + case TC654_PWM_6733:
393 + case TC654_PWM_7200:
394 + case TC654_PWM_7667:
395 + case TC654_PWM_8133:
396 + case TC654_PWM_8600:
397 + case TC654_PWM_9067:
398 + case TC654_PWM_9533:
399 + case TC654_PWM_10000:
400 + pwm_mode = mode - TC654_PWM_3000;
402 + err = write_tc(tc, TC654_REG_DUTY_CYCLE, pwm_mode);
407 + err = mod_config(tc, TC654_CTRL_DUTYC, TC654_CTRL_SDM);
411 + tc->pwm_manual = true;
415 + * In case the TC654/TC655 was in SDM mode, the write
416 + * above into the TC654_REG_DUTY_CYCLE register will
417 + * have no effect because the chip was switched off.
419 + * Note: The TC654/TC655 have a special "power-on"
420 + * feature where the PWM will be forced to 100% for
421 + * one full second in order to spin-up a resting fan.
423 + err = write_tc(tc, TC654_REG_DUTY_CYCLE, pwm_mode);
435 +static struct tc654 *tc654_update_device(struct device *dev)
437 + struct tc654 *tc = dev_get_drvdata(dev);
439 + mutex_lock(&tc->update_lock);
442 + * In Chapter "1.0 Electrical Characteristics",
443 + * the "Fault Output Response Time" is specified as 2.4 seconds.
445 + if (time_after(jiffies, tc->last_updated + 2 * HZ + (HZ * 2) / 5)
449 + bool alarm_triggered;
453 + for (i = 0; i < __NUM_TC654_FAN; i++) {
454 + ret = read_fan_rpm(tc, i);
458 + tc->fan_input[i] = ret;
461 + ret = read_tc(tc, TC654_REG_STATUS);
465 + alarm_triggered = !!(ret & (TC654_STATUS_F1F |
466 + TC654_STATUS_F2F | TC654_STATUS_R1CO |
467 + TC654_STATUS_R2CO | TC654_STATUS_OTF));
469 + tc->alarms[TC654_ALARM_FAN1_FAULT] = !!(ret & TC654_STATUS_F1F);
470 + tc->alarms[TC654_ALARM_FAN2_FAULT] = !!(ret & TC654_STATUS_F2F);
471 + tc->alarms[TC654_ALARM_FAN1_COUNTER_OVERFLOW] =
472 + !!(ret & TC654_STATUS_R1CO);
473 + tc->alarms[TC654_ALARM_FAN2_COUNTER_OVERFLOW] =
474 + !!(ret & TC654_STATUS_R2CO);
475 + tc->alarms[TC654_ALARM_OVER_TEMPERATURE] =
476 + !!(ret & TC654_STATUS_OTF);
477 + tc->vin_status = !!(ret & TC654_STATUS_VSTAT);
482 + * ... "If the V_in pin is open when TC654_CTRL_DUTYC is not
483 + * selected, then V_out duty cycle will default to 39.33%.".
485 + * and most importantly 6.5:
486 + * ... "V_in pin is open, the duty cycle will go to the default
487 + * setting of this register, which is 0010 (39.33%)."
489 + tc->pwm_manual |= tc->vin_status &&
490 + (tc->cached_regs[TC654_REG_CONFIG] &
493 + if (alarm_triggered) {
495 + * as the name implies, this FLAG needs to be
496 + * set in order to clear the FAN Fault error.
498 + ret = mod_config(tc, TC654_CTRL_FFCLR, 0);
503 + tc->last_updated = jiffies;
508 + mutex_unlock(&tc->update_lock);
512 +static u8 get_fan_pulse(struct tc654 *tc, enum TC654_FAN fan)
514 + u8 fan_pulse_mask = fan == TC654_FAN1 ?
515 + TC654_CTRL_F1PPR_M : TC654_CTRL_F2PPR_M;
516 + u8 fan_pulse_shift = fan == TC654_FAN1 ?
517 + TC654_CTRL_F1PPR_S : TC654_CTRL_F2PPR_S;
519 + return 1 << ((tc->cached_regs[TC654_REG_CONFIG] & fan_pulse_mask) >>
524 +set_fan_pulse(struct tc654 *tc, enum TC654_FAN fan, int pulses)
528 + u8 new_pulse_per_rotation;
529 + u8 fan_pulse_mask = fan == TC654_FAN1 ?
530 + TC654_CTRL_F1PPR_M : TC654_CTRL_F2PPR_M;
531 + u8 fan_pulse_shift = fan == TC654_FAN1 ?
532 + TC654_CTRL_F1PPR_S : TC654_CTRL_F2PPR_S;
536 + new_pulse_per_rotation = 0;
539 + new_pulse_per_rotation = 1;
542 + new_pulse_per_rotation = 2;
545 + new_pulse_per_rotation = 3;
551 + new_pulse_per_rotation <<= fan_pulse_shift;
552 + new_pulse_per_rotation &= fan_pulse_mask;
554 + old_pulses = tc->cached_regs[TC654_REG_CONFIG];
555 + old_pulses &= fan_pulse_mask;
557 + if (new_pulse_per_rotation == old_pulses)
560 + mutex_lock(&tc->update_lock);
561 + err = mod_config(tc, new_pulse_per_rotation,
562 + old_pulses & (~new_pulse_per_rotation));
563 + mutex_unlock(&tc->update_lock);
565 + /* invalidate RPM data to force re-read from hardware */
571 +static int get_fan_speed(struct tc654 *tc)
573 + enum TC654_FAN_MODE mode;
576 + mode = get_fan_mode(tc);
577 + for (i = 0; i < ARRAY_SIZE(pwm_table); i++) {
578 + if (mode == pwm_table[i].mode)
579 + return pwm_table[i].min;
585 +static int set_fan_speed(struct tc654 *tc, int new_value)
590 + if (new_value > pwm_table[ARRAY_SIZE(pwm_table) - 1].min ||
591 + new_value < pwm_table[0].min)
594 + for (i = 0; i < ARRAY_SIZE(pwm_table); i++) {
596 + if (pwm_table[i].min == new_value)
599 + /* a little bit too big - go with the previous entry */
600 + if (pwm_table[i].min > new_value) {
606 + mutex_lock(&tc->update_lock);
607 + result = write_fan_mode(tc, pwm_table[i].mode);
608 + mutex_unlock(&tc->update_lock);
618 +show_fan_input(struct device *dev, struct device_attribute *da, char *buf)
620 + struct tc654 *tc = tc654_update_device(dev);
621 + int nr = to_sensor_dev_attr(da)->index;
623 + return sprintf(buf, "%d\n", tc->fan_input[nr]);
627 +show_fan_min(struct device *dev, struct device_attribute *da, char *buf)
629 + struct tc654 *tc = dev_get_drvdata(dev);
630 + int nr = to_sensor_dev_attr(da)->index;
632 + return sprintf(buf, "%d\n", read_fan_fault_thresh(tc, nr));
636 +show_fan_min_alarm(struct device *dev, struct device_attribute *da, char *buf)
638 + struct tc654 *tc = tc654_update_device(dev);
639 + int nr = to_sensor_dev_attr(da)->index;
641 + return sprintf(buf, "%d\n", nr == TC654_FAN1 ?
642 + tc->alarms[TC654_ALARM_FAN1_FAULT] :
643 + tc->alarms[TC654_ALARM_FAN2_FAULT]);
647 +show_fan_max_alarm(struct device *dev, struct device_attribute *da, char *buf)
649 + struct tc654 *tc = tc654_update_device(dev);
650 + int nr = to_sensor_dev_attr(da)->index;
652 + return sprintf(buf, "%d\n", nr == TC654_FAN1 ?
653 + tc->alarms[TC654_ALARM_FAN1_COUNTER_OVERFLOW] :
654 + tc->alarms[TC654_ALARM_FAN2_COUNTER_OVERFLOW]);
658 +set_fan_min(struct device *dev, struct device_attribute *da,
659 + const char *buf, size_t count)
661 + struct tc654 *tc = dev_get_drvdata(dev);
663 + int nr = to_sensor_dev_attr(da)->index;
664 + int old_min = read_fan_fault_thresh(tc, nr);
665 + int status = kstrtol(buf, 10, &new_min);
670 + new_min = (new_min / 50) * 50;
671 + if (new_min == old_min) /* No change */
674 + if (new_min < 0 || new_min > 12750)
677 + mutex_lock(&tc->update_lock);
678 + status = write_fan_fault_thresh(tc, nr, new_min);
679 + mutex_unlock(&tc->update_lock);
684 +show_fan_max(struct device *dev, struct device_attribute *da, char *buf)
686 + struct tc654 *tc = dev_get_drvdata(dev);
687 + int max_rpm = tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_RES ?
688 + (((1 << 9) - 1) * 25) /* ((2**9) - 1) * 25 RPM */:
689 + (((1 << 8) - 1) * 50) /* ((2**8) - 1) * 50 RPM */;
691 + return sprintf(buf, "%d\n", max_rpm);
695 +show_fan_fault(struct device *dev, struct device_attribute *da, char *buf)
697 + struct tc654 *tc = tc654_update_device(dev);
698 + int nr = to_sensor_dev_attr(da)->index;
699 + u8 fan_fault_mask = nr == TC654_FAN1 ?
700 + TC654_STATUS_F1F : TC654_STATUS_F2F;
702 + return sprintf(buf, "%d\n",
703 + !!(tc->cached_regs[TC654_REG_STATUS] & fan_fault_mask));
707 +show_fan_pulses(struct device *dev, struct device_attribute *da, char *buf)
709 + struct tc654 *tc = dev_get_drvdata(dev);
710 + int nr = to_sensor_dev_attr(da)->index;
712 + return sprintf(buf, "%d\n", get_fan_pulse(tc, nr));
716 +set_fan_pulses(struct device *dev, struct device_attribute *da,
717 + const char *buf, size_t count)
719 + struct tc654 *tc = dev_get_drvdata(dev);
721 + int nr = to_sensor_dev_attr(da)->index;
722 + int status = kstrtol(buf, 10, &new_pulse);
727 + status = set_fan_pulse(tc, nr, new_pulse);
735 +show_pwm_enable(struct device *dev, struct device_attribute *da, char *buf)
737 + struct tc654 *tc = tc654_update_device(dev);
740 + if ((tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_SDM) &&
742 + pwm_enabled = 0; /* full off */
744 + if (tc->valid && tc->vin_status == 0)
745 + pwm_enabled = 2; /* automatic fan speed control */
747 + pwm_enabled = 1; /* PWM Mode */
750 + return sprintf(buf, "%d\n", pwm_enabled);
754 +set_pwm_enable(struct device *dev, struct device_attribute *da,
755 + const char *buf, size_t count)
757 + struct tc654 *tc = dev_get_drvdata(dev);
760 + int result = kstrtol(buf, 10, &new_value);
765 + mutex_lock(&tc->update_lock);
766 + switch (new_value) {
767 + case 0: /* no fan control (i.e. is OFF) */
768 + result = write_fan_mode(tc, TC654_PWM_OFF);
769 + tc->pwm_manual = false;
772 + case 1: /* manual fan control enabled (using pwm) */
773 + result = write_fan_mode(tc, TC654_PWM_10000);
776 + case 2: /* automatic fan speed control enabled */
777 + result = write_fan_mode(tc, TC654_PWM_VIN);
784 + mutex_unlock(&tc->update_lock);
785 + return result < 0 ? result : count;
789 +show_pwm(struct device *dev, struct device_attribute *da, char *buf)
791 + struct tc654 *tc = tc654_update_device(dev);
794 + ret = get_fan_speed(tc);
798 + return sprintf(buf, "%d\n", ret);
802 +set_pwm(struct device *dev, struct device_attribute *da,
803 + const char *buf, size_t count)
805 + struct tc654 *tc = dev_get_drvdata(dev);
806 + long new_value = -1;
807 + int result = kstrtol(buf, 10, &new_value);
812 + if (new_value < 0 || new_value > INT_MAX)
815 + if (!tc->pwm_manual)
818 + result = set_fan_speed(tc, new_value);
826 +show_temp_alarm_otf(struct device *dev, struct device_attribute *da, char *buf)
828 + struct tc654 *tc = tc654_update_device(dev);
830 + return sprintf(buf, "%d\n", tc->alarms[TC654_ALARM_OVER_TEMPERATURE]);
833 +static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input,
835 +static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR, show_fan_min,
836 + set_fan_min, TC654_FAN1);
837 +static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_fan_min_alarm,
839 +static SENSOR_DEVICE_ATTR(fan1_max_alarm, S_IRUGO, show_fan_max_alarm,
841 +static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, TC654_FAN1);
842 +static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault,
844 +static SENSOR_DEVICE_ATTR(fan1_pulses, S_IRUGO | S_IWUSR, show_fan_pulses,
845 + set_fan_pulses, TC654_FAN1);
846 +static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input,
848 +static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR, show_fan_min,
849 + set_fan_min, TC654_FAN2);
850 +static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max,
852 +static SENSOR_DEVICE_ATTR(fan2_min_alarm, S_IRUGO, show_fan_min_alarm,
854 +static SENSOR_DEVICE_ATTR(fan2_max_alarm, S_IRUGO, show_fan_max_alarm,
856 +static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_fan_fault,
858 +static SENSOR_DEVICE_ATTR(fan2_pulses, S_IRUGO | S_IWUSR, show_fan_pulses,
859 + set_fan_pulses, TC654_FAN2);
861 +static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
863 +static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm);
865 +static DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_temp_alarm_otf, NULL);
867 +/* sensors present on all models */
868 +static struct attribute *tc654_attrs[] = {
869 + &sensor_dev_attr_fan1_input.dev_attr.attr,
870 + &sensor_dev_attr_fan1_min.dev_attr.attr,
871 + &sensor_dev_attr_fan1_max.dev_attr.attr,
872 + &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
873 + &sensor_dev_attr_fan1_max_alarm.dev_attr.attr,
874 + &sensor_dev_attr_fan1_fault.dev_attr.attr,
875 + &sensor_dev_attr_fan1_pulses.dev_attr.attr,
876 + &sensor_dev_attr_fan2_input.dev_attr.attr,
877 + &sensor_dev_attr_fan2_min.dev_attr.attr,
878 + &sensor_dev_attr_fan2_max.dev_attr.attr,
879 + &sensor_dev_attr_fan2_min_alarm.dev_attr.attr,
880 + &sensor_dev_attr_fan2_max_alarm.dev_attr.attr,
881 + &sensor_dev_attr_fan2_fault.dev_attr.attr,
882 + &sensor_dev_attr_fan2_pulses.dev_attr.attr,
884 + &dev_attr_pwm1_enable.attr,
885 + &dev_attr_pwm1.attr,
887 + &dev_attr_temp1_emergency_alarm.attr,
891 +ATTRIBUTE_GROUPS(tc654);
893 +/* cooling device */
895 +static int tc654_get_max_state(struct thermal_cooling_device *cdev,
896 + unsigned long *state)
902 +static int tc654_get_cur_state(struct thermal_cooling_device *cdev,
903 + unsigned long *state)
905 + struct tc654 *tc = cdev->devdata;
911 + ret = get_fan_speed(tc);
919 +static int tc654_set_cur_state(struct thermal_cooling_device *cdev,
920 + unsigned long state)
922 + struct tc654 *tc = cdev->devdata;
927 + if (state > INT_MAX)
930 + return set_fan_speed(tc, state);
933 +static const struct thermal_cooling_device_ops tc654_fan_cool_ops = {
934 + .get_max_state = tc654_get_max_state,
935 + .get_cur_state = tc654_get_cur_state,
936 + .set_cur_state = tc654_set_cur_state,
940 +/* hardware probe and detection */
943 +tc654_probe(struct i2c_client *client, const struct i2c_device_id *id)
946 + struct device *hwmon_dev;
949 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
952 + tc = devm_kzalloc(&client->dev, sizeof(*tc), GFP_KERNEL);
956 + i2c_set_clientdata(client, tc);
957 + tc->client = client;
958 + mutex_init(&tc->update_lock);
960 + /* cache all 8 registers */
961 + for (i = 0; i < __TC654_REG_NUM; i++) {
962 + ret = read_tc(tc, i);
968 + hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
971 + if (IS_ERR(hwmon_dev))
972 + return PTR_ERR(hwmon_dev);
974 +#if IS_ENABLED(CONFIG_OF)
975 + /* Optional cooling device register for Device tree platforms */
976 + tc->cdev = thermal_of_cooling_device_register(client->dev.of_node,
978 + &tc654_fan_cool_ops);
979 +#else /* CONFIG_OF */
980 + /* Optional cooling device register for non Device tree platforms */
981 + tc->cdev = thermal_cooling_device_register("tc654", tc,
982 + &tc654_fan_cool_ops);
983 +#endif /* CONFIG_OF */
985 + dev_info(&client->dev, "%s: sensor '%s'\n",
986 + dev_name(hwmon_dev), client->name);
991 +static const struct i2c_device_id tc654_ids[] = {
995 +MODULE_DEVICE_TABLE(i2c, tc654_ids);
997 +/* Return 0 if detection is successful, -ENODEV otherwise */
999 +tc654_detect(struct i2c_client *new_client, struct i2c_board_info *info)
1001 + struct i2c_adapter *adapter = new_client->adapter;
1002 + int manufacturer, product;
1004 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1007 + manufacturer = i2c_smbus_read_byte_data(new_client, TC654_REG_MFR_ID);
1008 + if (manufacturer != TC654_MFR_ID_MICROCHIP)
1011 + product = i2c_smbus_read_byte_data(new_client, TC654_REG_VER_ID);
1012 + if (!((product == TC654_VER_ID) || (product == TC655_VER_ID)))
1015 + strlcpy(info->type, "tc654", I2C_NAME_SIZE);
1019 +static struct i2c_driver tc654_driver = {
1020 + .class = I2C_CLASS_HWMON,
1024 + .probe = tc654_probe,
1025 + .id_table = tc654_ids,
1026 + .detect = tc654_detect,
1027 + .address_list = normal_i2c,
1030 +module_i2c_driver(tc654_driver);
1032 +MODULE_AUTHOR("Christian Lamparter <chunkeey@gmail.com>");
1033 +MODULE_DESCRIPTION("Microchip TC654/TC655 hwmon driver");
1034 +MODULE_LICENSE("GPL");