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 --- a/drivers/hwmon/Kconfig
28 +++ b/drivers/hwmon/Kconfig
29 @@ -1484,6 +1484,16 @@ config SENSORS_INA2XX
30 This driver can also be built as a module. If so, the module
31 will be called ina2xx.
34 + tristate "Microchip TC654 and TC655"
37 + If you say yes here you get support for Microchip TC655 and TC654
38 + Dual PWM Fan Speed Controllers and sensor chips.
40 + This driver can also be built as a module. If so, the module
41 + will be called tc654.
44 tristate "Microchip TC74"
46 --- a/drivers/hwmon/Makefile
47 +++ b/drivers/hwmon/Makefile
48 @@ -143,6 +143,7 @@ obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc4
49 obj-$(CONFIG_SENSORS_SMSC47M1) += smsc47m1.o
50 obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
51 obj-$(CONFIG_SENSORS_AMC6821) += amc6821.o
52 +obj-$(CONFIG_SENSORS_TC654) += tc654.o
53 obj-$(CONFIG_SENSORS_TC74) += tc74.o
54 obj-$(CONFIG_SENSORS_THMC50) += thmc50.o
55 obj-$(CONFIG_SENSORS_TMP102) += tmp102.o
57 +++ b/drivers/hwmon/tc654.c
60 + * tc654.c - Support for Microchip TC654/TC655
61 + * "A Dual SMBus PWM FAN Speed Controllers with Fan Fault Detection"
63 + * Copyright (c) 2016 Christian Lamparter <chunkeey@gmail.com>
65 + * This program is free software; you can redistribute it and/or modify
66 + * it under the terms of the GNU General Public License as published by
67 + * the Free Software Foundation version 2 of the License.
69 + * This program is distributed in the hope that it will be useful,
70 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
71 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72 + * GNU General Public License for more details.
74 + * The chip is described in the DS2001734C Spec Document from Microchip.
77 +#include <linux/module.h>
78 +#include <linux/init.h>
79 +#include <linux/slab.h>
80 +#include <linux/jiffies.h>
81 +#include <linux/i2c.h>
82 +#include <linux/hwmon.h>
83 +#include <linux/hwmon-sysfs.h>
84 +#include <linux/err.h>
85 +#include <linux/mutex.h>
86 +#include <linux/thermal.h>
88 +/* Hardware definitions */
89 +/* 5.1.4 Address Byte stats that TC654/TC655 are fixed at 0x1b */
90 +static const unsigned short normal_i2c[] = { 0x1b, I2C_CLIENT_END };
93 + TC654_REG_RPM1 = 0x00,
95 + TC654_REG_FAN1_FAULT_THRESH,
96 + TC654_REG_FAN2_FAULT_THRESH,
99 + TC654_REG_DUTY_CYCLE,
107 +#define TC654_MFR_ID_MICROCHIP 0x84
108 +#define TC654_VER_ID 0x00
109 +#define TC655_VER_ID 0x01
111 +enum TC654_CONTROL_BITS {
112 + TC654_CTRL_SDM = BIT(0),
113 + TC654_CTRL_F1PPR_S = 1,
114 + TC654_CTRL_F1PPR_M = (BIT(1) | BIT(2)),
115 + TC654_CTRL_F2PPR_S = 3,
116 + TC654_CTRL_F2PPR_M = (BIT(3) | BIT(4)),
117 + TC654_CTRL_DUTYC = BIT(5),
118 + TC654_CTRL_RES = BIT(6),
119 + TC654_CTRL_FFCLR = BIT(7),
122 +enum TC654_STATUS_BITS {
123 + TC654_STATUS_F1F = BIT(0),
124 + TC654_STATUS_F2F = BIT(1),
125 + TC654_STATUS_VSTAT = BIT(2),
126 + TC654_STATUS_R1CO = BIT(3),
127 + TC654_STATUS_R2CO = BIT(4),
128 + TC654_STATUS_OTF = BIT(5),
139 +enum TC654_FAN_MODE {
140 + TC654_PWM_OFF, /* Shutdown Mode - switch of both fans */
141 + TC654_PWM_VIN, /* Fans will be controlled via V_in analog input pin */
142 + TC654_PWM_3000, /* sets fans to 30% duty cycle */
144 + TC654_PWM_3933, /* default case - if V_in pin is open */
157 + TC654_PWM_10000, /* sets fans to 100% duty cycle */
161 + TC654_ALARM_FAN1_FAULT,
162 + TC654_ALARM_FAN2_FAULT,
163 + TC654_ALARM_FAN1_COUNTER_OVERFLOW,
164 + TC654_ALARM_FAN2_COUNTER_OVERFLOW,
165 + TC654_ALARM_OVER_TEMPERATURE,
168 + __NUM_TC654_ALARMS,
171 +static const struct pwm_table_entry {
173 + enum TC654_FAN_MODE mode;
175 + { 0, TC654_PWM_OFF },
176 + { 1, TC654_PWM_3000 },
177 + { 88, TC654_PWM_3467 },
178 + {101, TC654_PWM_3933 },
179 + {113, TC654_PWM_4400 },
180 + {125, TC654_PWM_4867 },
181 + {137, TC654_PWM_5333 },
182 + {148, TC654_PWM_5800 },
183 + {160, TC654_PWM_6267 },
184 + {172, TC654_PWM_6733 },
185 + {184, TC654_PWM_7200 },
186 + {196, TC654_PWM_7667 },
187 + {208, TC654_PWM_8133 },
188 + {220, TC654_PWM_8600 },
189 + {232, TC654_PWM_9067 },
190 + {244, TC654_PWM_9533 },
191 + {255, TC654_PWM_10000 },
194 +/* driver context */
196 + struct i2c_client *client;
198 + struct mutex update_lock;
200 + unsigned long last_updated; /* in jiffies */
201 + u8 cached_regs[__TC654_REG_NUM];
203 + bool valid; /* monitored registers are valid */
204 + u16 fan_input[__NUM_TC654_FAN];
205 + bool alarms[__NUM_TC654_ALARMS];
209 + /* optional cooling device */
210 + struct thermal_cooling_device *cdev;
213 +/* hardware accessors and functions */
214 +static int read_tc(struct tc654 *tc, u8 reg)
218 + if (reg <= TC654_REG_VER_ID) {
219 + /* Table 6.1 states that all registers are readable */
220 + status = i2c_smbus_read_byte_data(tc->client, reg);
225 + dev_warn(&tc->client->dev, "can't read register 0x%02x due to error (%d)",
228 + tc->cached_regs[reg] = status;
234 +static int write_tc(struct tc654 *tc, u8 i2c_reg, u8 val)
239 + * Table 6.1 states that both fan threshold registers,
240 + * the Config and Duty Cycle are writeable.
243 + case TC654_REG_FAN1_FAULT_THRESH:
244 + case TC654_REG_FAN2_FAULT_THRESH:
245 + case TC654_REG_DUTY_CYCLE:
246 + case TC654_REG_CONFIG:
247 + status = i2c_smbus_write_byte_data(tc->client, i2c_reg, val);
255 + dev_warn(&tc->client->dev, "can't write register 0x%02x with value 0x%02x due to error (%d)",
256 + i2c_reg, val, status);
258 + tc->cached_regs[i2c_reg] = val;
264 +static int mod_config(struct tc654 *tc, u8 set, u8 clear)
268 + /* a bit can't be set and cleared on the same time. */
272 + /* invalidate data to force re-read from hardware */
274 + val = (tc->cached_regs[TC654_REG_CONFIG] | set) & (~clear);
275 + return write_tc(tc, TC654_REG_CONFIG, val);
278 +static int read_fan_rpm(struct tc654 *tc, enum TC654_FAN fan)
282 + /* 6.1 RPM-OUTPUT1 and RPM-OUTPUT2 registers */
283 + ret = read_tc(tc, fan == TC654_FAN1 ? TC654_REG_RPM1 : TC654_REG_RPM2);
288 + * The Resolution Selection Bit in 6.3 CONFIGURATION REGISTER
289 + * is needed to convert the raw value to the RPM.
290 + * 0 = RPM1 and RPM2 use (8-Bit) resolution => * 50 RPM
291 + * 1 = RPM1 and RPM2 use (9-Bit) resolution => * 25 RPM
293 + return ret * (25 <<
294 + !(tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_RES));
297 +static int write_fan_fault_thresh(struct tc654 *tc, enum TC654_FAN fan,
306 + * 6.2 FAN_FAULT1 and FAN_FAULT2 Threshold registers
308 + * Both registers operate in 50 RPM mode exclusively.
310 + converted_rpm = rpm / 50;
312 + /* invalidate data to force re-read from hardware */
314 + return write_tc(tc, fan == TC654_FAN1 ? TC654_REG_FAN1_FAULT_THRESH :
315 + TC654_REG_FAN2_FAULT_THRESH, converted_rpm);
319 +static int read_fan_fault_thresh(struct tc654 *tc, enum TC654_FAN fan)
322 + * 6.2 FAN_FAULT1 and FAN_FAULT2 Threshold registers
324 + * Both registers operate in 50 RPM mode exclusively.
326 + return read_tc(tc, fan == TC654_FAN1 ? TC654_REG_FAN1_FAULT_THRESH :
327 + TC654_REG_FAN2_FAULT_THRESH) * 50;
330 +static enum TC654_FAN_MODE get_fan_mode(struct tc654 *tc)
332 + if (tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_SDM) {
333 + return TC654_PWM_OFF;
334 + } else if (tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_DUTYC) {
335 + return TC654_PWM_3000 + tc->cached_regs[TC654_REG_DUTY_CYCLE];
336 + } else if (tc->vin_status == 0)
337 + return TC654_PWM_VIN;
342 +static int write_fan_mode(struct tc654 *tc, enum TC654_FAN_MODE mode)
348 + in_sdm = !!(tc->cached_regs[TC654_REG_CONFIG] &
352 + case TC654_PWM_OFF:
356 + /* Enter Shutdown Mode - Switches off all fans */
357 + err = mod_config(tc, TC654_CTRL_SDM, TC654_CTRL_DUTYC);
363 + case TC654_PWM_VIN:
364 + if (tc->vin_status) {
365 + dev_err(&tc->client->dev,
366 + "V_in pin is open, can't enable automatic mode.");
370 + err = mod_config(tc, 0, TC654_CTRL_SDM | TC654_CTRL_DUTYC);
374 + tc->pwm_manual = false;
377 + case TC654_PWM_3000:
378 + case TC654_PWM_3467:
379 + case TC654_PWM_3933:
380 + case TC654_PWM_4400:
381 + case TC654_PWM_4867:
382 + case TC654_PWM_5333:
383 + case TC654_PWM_5800:
384 + case TC654_PWM_6267:
385 + case TC654_PWM_6733:
386 + case TC654_PWM_7200:
387 + case TC654_PWM_7667:
388 + case TC654_PWM_8133:
389 + case TC654_PWM_8600:
390 + case TC654_PWM_9067:
391 + case TC654_PWM_9533:
392 + case TC654_PWM_10000:
393 + pwm_mode = mode - TC654_PWM_3000;
395 + err = write_tc(tc, TC654_REG_DUTY_CYCLE, pwm_mode);
400 + err = mod_config(tc, TC654_CTRL_DUTYC, TC654_CTRL_SDM);
404 + tc->pwm_manual = true;
408 + * In case the TC654/TC655 was in SDM mode, the write
409 + * above into the TC654_REG_DUTY_CYCLE register will
410 + * have no effect because the chip was switched off.
412 + * Note: The TC654/TC655 have a special "power-on"
413 + * feature where the PWM will be forced to 100% for
414 + * one full second in order to spin-up a resting fan.
416 + err = write_tc(tc, TC654_REG_DUTY_CYCLE, pwm_mode);
428 +static struct tc654 *tc654_update_device(struct device *dev)
430 + struct tc654 *tc = dev_get_drvdata(dev);
432 + mutex_lock(&tc->update_lock);
435 + * In Chapter "1.0 Electrical Characteristics",
436 + * the "Fault Output Response Time" is specified as 2.4 seconds.
438 + if (time_after(jiffies, tc->last_updated + 2 * HZ + (HZ * 2) / 5)
442 + bool alarm_triggered;
446 + for (i = 0; i < __NUM_TC654_FAN; i++) {
447 + ret = read_fan_rpm(tc, i);
451 + tc->fan_input[i] = ret;
454 + ret = read_tc(tc, TC654_REG_STATUS);
458 + alarm_triggered = !!(ret & (TC654_STATUS_F1F |
459 + TC654_STATUS_F2F | TC654_STATUS_R1CO |
460 + TC654_STATUS_R2CO | TC654_STATUS_OTF));
462 + tc->alarms[TC654_ALARM_FAN1_FAULT] = !!(ret & TC654_STATUS_F1F);
463 + tc->alarms[TC654_ALARM_FAN2_FAULT] = !!(ret & TC654_STATUS_F2F);
464 + tc->alarms[TC654_ALARM_FAN1_COUNTER_OVERFLOW] =
465 + !!(ret & TC654_STATUS_R1CO);
466 + tc->alarms[TC654_ALARM_FAN2_COUNTER_OVERFLOW] =
467 + !!(ret & TC654_STATUS_R2CO);
468 + tc->alarms[TC654_ALARM_OVER_TEMPERATURE] =
469 + !!(ret & TC654_STATUS_OTF);
470 + tc->vin_status = !!(ret & TC654_STATUS_VSTAT);
475 + * ... "If the V_in pin is open when TC654_CTRL_DUTYC is not
476 + * selected, then V_out duty cycle will default to 39.33%.".
478 + * and most importantly 6.5:
479 + * ... "V_in pin is open, the duty cycle will go to the default
480 + * setting of this register, which is 0010 (39.33%)."
482 + tc->pwm_manual |= tc->vin_status &&
483 + (tc->cached_regs[TC654_REG_CONFIG] &
486 + if (alarm_triggered) {
488 + * as the name implies, this FLAG needs to be
489 + * set in order to clear the FAN Fault error.
491 + ret = mod_config(tc, TC654_CTRL_FFCLR, 0);
496 + tc->last_updated = jiffies;
501 + mutex_unlock(&tc->update_lock);
505 +static u8 get_fan_pulse(struct tc654 *tc, enum TC654_FAN fan)
507 + u8 fan_pulse_mask = fan == TC654_FAN1 ?
508 + TC654_CTRL_F1PPR_M : TC654_CTRL_F2PPR_M;
509 + u8 fan_pulse_shift = fan == TC654_FAN1 ?
510 + TC654_CTRL_F1PPR_S : TC654_CTRL_F2PPR_S;
512 + return 1 << ((tc->cached_regs[TC654_REG_CONFIG] & fan_pulse_mask) >>
517 +set_fan_pulse(struct tc654 *tc, enum TC654_FAN fan, int pulses)
521 + u8 new_pulse_per_rotation;
522 + u8 fan_pulse_mask = fan == TC654_FAN1 ?
523 + TC654_CTRL_F1PPR_M : TC654_CTRL_F2PPR_M;
524 + u8 fan_pulse_shift = fan == TC654_FAN1 ?
525 + TC654_CTRL_F1PPR_S : TC654_CTRL_F2PPR_S;
529 + new_pulse_per_rotation = 0;
532 + new_pulse_per_rotation = 1;
535 + new_pulse_per_rotation = 2;
538 + new_pulse_per_rotation = 3;
544 + new_pulse_per_rotation <<= fan_pulse_shift;
545 + new_pulse_per_rotation &= fan_pulse_mask;
547 + old_pulses = tc->cached_regs[TC654_REG_CONFIG];
548 + old_pulses &= fan_pulse_mask;
550 + if (new_pulse_per_rotation == old_pulses)
553 + mutex_lock(&tc->update_lock);
554 + err = mod_config(tc, new_pulse_per_rotation,
555 + old_pulses & (~new_pulse_per_rotation));
556 + mutex_unlock(&tc->update_lock);
558 + /* invalidate RPM data to force re-read from hardware */
564 +static int get_fan_speed(struct tc654 *tc)
566 + enum TC654_FAN_MODE mode;
569 + mode = get_fan_mode(tc);
570 + for (i = 0; i < ARRAY_SIZE(pwm_table); i++) {
571 + if (mode == pwm_table[i].mode)
572 + return pwm_table[i].min;
578 +static int set_fan_speed(struct tc654 *tc, int new_value)
583 + if (new_value > pwm_table[ARRAY_SIZE(pwm_table) - 1].min ||
584 + new_value < pwm_table[0].min)
587 + for (i = 0; i < ARRAY_SIZE(pwm_table); i++) {
589 + if (pwm_table[i].min == new_value)
592 + /* a little bit too big - go with the previous entry */
593 + if (pwm_table[i].min > new_value) {
599 + mutex_lock(&tc->update_lock);
600 + result = write_fan_mode(tc, pwm_table[i].mode);
601 + mutex_unlock(&tc->update_lock);
611 +show_fan_input(struct device *dev, struct device_attribute *da, char *buf)
613 + struct tc654 *tc = tc654_update_device(dev);
614 + int nr = to_sensor_dev_attr(da)->index;
616 + return sprintf(buf, "%d\n", tc->fan_input[nr]);
620 +show_fan_min(struct device *dev, struct device_attribute *da, char *buf)
622 + struct tc654 *tc = dev_get_drvdata(dev);
623 + int nr = to_sensor_dev_attr(da)->index;
625 + return sprintf(buf, "%d\n", read_fan_fault_thresh(tc, nr));
629 +show_fan_min_alarm(struct device *dev, struct device_attribute *da, char *buf)
631 + struct tc654 *tc = tc654_update_device(dev);
632 + int nr = to_sensor_dev_attr(da)->index;
634 + return sprintf(buf, "%d\n", nr == TC654_FAN1 ?
635 + tc->alarms[TC654_ALARM_FAN1_FAULT] :
636 + tc->alarms[TC654_ALARM_FAN2_FAULT]);
640 +show_fan_max_alarm(struct device *dev, struct device_attribute *da, char *buf)
642 + struct tc654 *tc = tc654_update_device(dev);
643 + int nr = to_sensor_dev_attr(da)->index;
645 + return sprintf(buf, "%d\n", nr == TC654_FAN1 ?
646 + tc->alarms[TC654_ALARM_FAN1_COUNTER_OVERFLOW] :
647 + tc->alarms[TC654_ALARM_FAN2_COUNTER_OVERFLOW]);
651 +set_fan_min(struct device *dev, struct device_attribute *da,
652 + const char *buf, size_t count)
654 + struct tc654 *tc = dev_get_drvdata(dev);
656 + int nr = to_sensor_dev_attr(da)->index;
657 + int old_min = read_fan_fault_thresh(tc, nr);
658 + int status = kstrtol(buf, 10, &new_min);
663 + new_min = (new_min / 50) * 50;
664 + if (new_min == old_min) /* No change */
667 + if (new_min < 0 || new_min > 12750)
670 + mutex_lock(&tc->update_lock);
671 + status = write_fan_fault_thresh(tc, nr, new_min);
672 + mutex_unlock(&tc->update_lock);
677 +show_fan_max(struct device *dev, struct device_attribute *da, char *buf)
679 + struct tc654 *tc = dev_get_drvdata(dev);
680 + int max_rpm = tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_RES ?
681 + (((1 << 9) - 1) * 25) /* ((2**9) - 1) * 25 RPM */:
682 + (((1 << 8) - 1) * 50) /* ((2**8) - 1) * 50 RPM */;
684 + return sprintf(buf, "%d\n", max_rpm);
688 +show_fan_fault(struct device *dev, struct device_attribute *da, char *buf)
690 + struct tc654 *tc = tc654_update_device(dev);
691 + int nr = to_sensor_dev_attr(da)->index;
692 + u8 fan_fault_mask = nr == TC654_FAN1 ?
693 + TC654_STATUS_F1F : TC654_STATUS_F2F;
695 + return sprintf(buf, "%d\n",
696 + !!(tc->cached_regs[TC654_REG_STATUS] & fan_fault_mask));
700 +show_fan_pulses(struct device *dev, struct device_attribute *da, char *buf)
702 + struct tc654 *tc = dev_get_drvdata(dev);
703 + int nr = to_sensor_dev_attr(da)->index;
705 + return sprintf(buf, "%d\n", get_fan_pulse(tc, nr));
709 +set_fan_pulses(struct device *dev, struct device_attribute *da,
710 + const char *buf, size_t count)
712 + struct tc654 *tc = dev_get_drvdata(dev);
714 + int nr = to_sensor_dev_attr(da)->index;
715 + int status = kstrtol(buf, 10, &new_pulse);
720 + status = set_fan_pulse(tc, nr, new_pulse);
728 +show_pwm_enable(struct device *dev, struct device_attribute *da, char *buf)
730 + struct tc654 *tc = tc654_update_device(dev);
733 + if ((tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_SDM) &&
735 + pwm_enabled = 0; /* full off */
737 + if (tc->valid && tc->vin_status == 0)
738 + pwm_enabled = 2; /* automatic fan speed control */
740 + pwm_enabled = 1; /* PWM Mode */
743 + return sprintf(buf, "%d\n", pwm_enabled);
747 +set_pwm_enable(struct device *dev, struct device_attribute *da,
748 + const char *buf, size_t count)
750 + struct tc654 *tc = dev_get_drvdata(dev);
753 + int result = kstrtol(buf, 10, &new_value);
758 + mutex_lock(&tc->update_lock);
759 + switch (new_value) {
760 + case 0: /* no fan control (i.e. is OFF) */
761 + result = write_fan_mode(tc, TC654_PWM_OFF);
762 + tc->pwm_manual = false;
765 + case 1: /* manual fan control enabled (using pwm) */
766 + result = write_fan_mode(tc, TC654_PWM_10000);
769 + case 2: /* automatic fan speed control enabled */
770 + result = write_fan_mode(tc, TC654_PWM_VIN);
777 + mutex_unlock(&tc->update_lock);
778 + return result < 0 ? result : count;
782 +show_pwm(struct device *dev, struct device_attribute *da, char *buf)
784 + struct tc654 *tc = tc654_update_device(dev);
787 + ret = get_fan_speed(tc);
791 + return sprintf(buf, "%d\n", ret);
795 +set_pwm(struct device *dev, struct device_attribute *da,
796 + const char *buf, size_t count)
798 + struct tc654 *tc = dev_get_drvdata(dev);
799 + long new_value = -1;
800 + int result = kstrtol(buf, 10, &new_value);
805 + if (new_value < 0 || new_value > INT_MAX)
808 + if (!tc->pwm_manual)
811 + result = set_fan_speed(tc, new_value);
819 +show_temp_alarm_otf(struct device *dev, struct device_attribute *da, char *buf)
821 + struct tc654 *tc = tc654_update_device(dev);
823 + return sprintf(buf, "%d\n", tc->alarms[TC654_ALARM_OVER_TEMPERATURE]);
826 +static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input,
828 +static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR, show_fan_min,
829 + set_fan_min, TC654_FAN1);
830 +static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_fan_min_alarm,
832 +static SENSOR_DEVICE_ATTR(fan1_max_alarm, S_IRUGO, show_fan_max_alarm,
834 +static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, TC654_FAN1);
835 +static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault,
837 +static SENSOR_DEVICE_ATTR(fan1_pulses, S_IRUGO | S_IWUSR, show_fan_pulses,
838 + set_fan_pulses, TC654_FAN1);
839 +static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input,
841 +static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR, show_fan_min,
842 + set_fan_min, TC654_FAN2);
843 +static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max,
845 +static SENSOR_DEVICE_ATTR(fan2_min_alarm, S_IRUGO, show_fan_min_alarm,
847 +static SENSOR_DEVICE_ATTR(fan2_max_alarm, S_IRUGO, show_fan_max_alarm,
849 +static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_fan_fault,
851 +static SENSOR_DEVICE_ATTR(fan2_pulses, S_IRUGO | S_IWUSR, show_fan_pulses,
852 + set_fan_pulses, TC654_FAN2);
854 +static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
856 +static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm);
858 +static DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_temp_alarm_otf, NULL);
860 +/* sensors present on all models */
861 +static struct attribute *tc654_attrs[] = {
862 + &sensor_dev_attr_fan1_input.dev_attr.attr,
863 + &sensor_dev_attr_fan1_min.dev_attr.attr,
864 + &sensor_dev_attr_fan1_max.dev_attr.attr,
865 + &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
866 + &sensor_dev_attr_fan1_max_alarm.dev_attr.attr,
867 + &sensor_dev_attr_fan1_fault.dev_attr.attr,
868 + &sensor_dev_attr_fan1_pulses.dev_attr.attr,
869 + &sensor_dev_attr_fan2_input.dev_attr.attr,
870 + &sensor_dev_attr_fan2_min.dev_attr.attr,
871 + &sensor_dev_attr_fan2_max.dev_attr.attr,
872 + &sensor_dev_attr_fan2_min_alarm.dev_attr.attr,
873 + &sensor_dev_attr_fan2_max_alarm.dev_attr.attr,
874 + &sensor_dev_attr_fan2_fault.dev_attr.attr,
875 + &sensor_dev_attr_fan2_pulses.dev_attr.attr,
877 + &dev_attr_pwm1_enable.attr,
878 + &dev_attr_pwm1.attr,
880 + &dev_attr_temp1_emergency_alarm.attr,
884 +ATTRIBUTE_GROUPS(tc654);
886 +/* cooling device */
888 +static int tc654_get_max_state(struct thermal_cooling_device *cdev,
889 + unsigned long *state)
895 +static int tc654_get_cur_state(struct thermal_cooling_device *cdev,
896 + unsigned long *state)
898 + struct tc654 *tc = cdev->devdata;
904 + ret = get_fan_speed(tc);
912 +static int tc654_set_cur_state(struct thermal_cooling_device *cdev,
913 + unsigned long state)
915 + struct tc654 *tc = cdev->devdata;
920 + if (state > INT_MAX)
923 + return set_fan_speed(tc, state);
926 +static const struct thermal_cooling_device_ops tc654_fan_cool_ops = {
927 + .get_max_state = tc654_get_max_state,
928 + .get_cur_state = tc654_get_cur_state,
929 + .set_cur_state = tc654_set_cur_state,
933 +/* hardware probe and detection */
936 +tc654_probe(struct i2c_client *client, const struct i2c_device_id *id)
939 + struct device *hwmon_dev;
942 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
945 + tc = devm_kzalloc(&client->dev, sizeof(*tc), GFP_KERNEL);
949 + i2c_set_clientdata(client, tc);
950 + tc->client = client;
951 + mutex_init(&tc->update_lock);
953 + /* cache all 8 registers */
954 + for (i = 0; i < __TC654_REG_NUM; i++) {
955 + ret = read_tc(tc, i);
961 + hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
964 + if (IS_ERR(hwmon_dev))
965 + return PTR_ERR(hwmon_dev);
967 +#if IS_ENABLED(CONFIG_OF)
968 + /* Optional cooling device register for Device tree platforms */
969 + tc->cdev = thermal_of_cooling_device_register(client->dev.of_node,
971 + &tc654_fan_cool_ops);
972 +#else /* CONFIG_OF */
973 + /* Optional cooling device register for non Device tree platforms */
974 + tc->cdev = thermal_cooling_device_register("tc654", tc,
975 + &tc654_fan_cool_ops);
976 +#endif /* CONFIG_OF */
978 + dev_info(&client->dev, "%s: sensor '%s'\n",
979 + dev_name(hwmon_dev), client->name);
984 +static const struct i2c_device_id tc654_ids[] = {
988 +MODULE_DEVICE_TABLE(i2c, tc654_ids);
990 +/* Return 0 if detection is successful, -ENODEV otherwise */
992 +tc654_detect(struct i2c_client *new_client, struct i2c_board_info *info)
994 + struct i2c_adapter *adapter = new_client->adapter;
995 + int manufacturer, product;
997 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1000 + manufacturer = i2c_smbus_read_byte_data(new_client, TC654_REG_MFR_ID);
1001 + if (manufacturer != TC654_MFR_ID_MICROCHIP)
1004 + product = i2c_smbus_read_byte_data(new_client, TC654_REG_VER_ID);
1005 + if (!((product == TC654_VER_ID) || (product == TC655_VER_ID)))
1008 + strlcpy(info->type, "tc654", I2C_NAME_SIZE);
1012 +static struct i2c_driver tc654_driver = {
1013 + .class = I2C_CLASS_HWMON,
1017 + .probe = tc654_probe,
1018 + .id_table = tc654_ids,
1019 + .detect = tc654_detect,
1020 + .address_list = normal_i2c,
1023 +module_i2c_driver(tc654_driver);
1025 +MODULE_AUTHOR("Christian Lamparter <chunkeey@gmail.com>");
1026 +MODULE_DESCRIPTION("Microchip TC654/TC655 hwmon driver");
1027 +MODULE_LICENSE("GPL");