1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * tc654.c - Linux kernel modules for fan speed controller
5 * Copyright (C) 2016 Allied Telesis Labs NZ
8 #include <linux/bitops.h>
10 #include <linux/hwmon.h>
11 #include <linux/hwmon-sysfs.h>
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/jiffies.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/util_macros.h>
21 TC654_REG_RPM1
= 0x00, /* RPM Output 1 */
22 TC654_REG_RPM2
= 0x01, /* RPM Output 2 */
23 TC654_REG_FAN_FAULT1
= 0x02, /* Fan Fault 1 Threshold */
24 TC654_REG_FAN_FAULT2
= 0x03, /* Fan Fault 2 Threshold */
25 TC654_REG_CONFIG
= 0x04, /* Configuration */
26 TC654_REG_STATUS
= 0x05, /* Status */
27 TC654_REG_DUTY_CYCLE
= 0x06, /* Fan Speed Duty Cycle */
28 TC654_REG_MFR_ID
= 0x07, /* Manufacturer Identification */
29 TC654_REG_VER_ID
= 0x08, /* Version Identification */
32 /* Macros to easily index the registers */
33 #define TC654_REG_RPM(idx) (TC654_REG_RPM1 + (idx))
34 #define TC654_REG_FAN_FAULT(idx) (TC654_REG_FAN_FAULT1 + (idx))
36 /* Config register bits */
37 #define TC654_REG_CONFIG_RES BIT(6) /* Resolution Selection */
38 #define TC654_REG_CONFIG_DUTYC BIT(5) /* Duty Cycle Control */
39 #define TC654_REG_CONFIG_SDM BIT(0) /* Shutdown Mode */
41 /* Status register bits */
42 #define TC654_REG_STATUS_F2F BIT(1) /* Fan 2 Fault */
43 #define TC654_REG_STATUS_F1F BIT(0) /* Fan 1 Fault */
45 /* RPM resolution for RPM Output registers */
46 #define TC654_HIGH_RPM_RESOLUTION 25 /* 25 RPM resolution */
47 #define TC654_LOW_RPM_RESOLUTION 50 /* 50 RPM resolution */
49 /* Convert to the fan fault RPM threshold from register value */
50 #define TC654_FAN_FAULT_FROM_REG(val) ((val) * 50) /* 50 RPM resolution */
52 /* Convert to register value from the fan fault RPM threshold */
53 #define TC654_FAN_FAULT_TO_REG(val) (((val) / 50) & 0xff)
55 /* Register data is read (and cached) at most once per second. */
56 #define TC654_UPDATE_INTERVAL HZ
59 struct i2c_client
*client
;
62 struct mutex update_lock
;
64 /* tc654 register cache */
66 unsigned long last_updated
; /* in jiffies */
68 u8 rpm_output
[2]; /* The fan RPM data for fans 1 and 2 is then
69 * written to registers RPM1 and RPM2
71 u8 fan_fault
[2]; /* The Fan Fault Threshold Registers are used to
72 * set the fan fault threshold levels for fan 1
75 u8 config
; /* The Configuration Register is an 8-bit read/
76 * writable multi-function control register
79 * 0 = Normal Operation (default)
80 * 6: Resolution Selection for RPM Output Registers
81 * RPM Output Registers (RPM1 and RPM2) will be
83 * 1 = 25 RPM (9-bit) resolution
84 * 0 = 50 RPM (8-bit) resolution (default)
85 * 5: Duty Cycle Control Method
86 * The V OUT duty cycle will be controlled via
87 * 1 = the SMBus interface.
88 * 0 = via the V IN analog input pin. (default)
89 * 4,3: Fan 2 Pulses Per Rotation
94 * 2,1: Fan 1 Pulses Per Rotation
101 * 0 = Normal operation. (default)
103 u8 status
; /* The Status register provides all the information
104 * about what is going on within the TC654/TC655
106 * 7,6: Unimplemented, Read as '0'
107 * 5: Over-Temperature Fault Condition
108 * 1 = Over-Temperature condition has occurred
109 * 0 = Normal operation. V IN is less than 2.6V
110 * 4: RPM2 Counter Overflow
111 * 1 = Fault condition
112 * 0 = Normal operation
113 * 3: RPM1 Counter Overflow
114 * 1 = Fault condition
115 * 0 = Normal operation
116 * 2: V IN Input Status
118 * 0 = Normal operation. voltage present at V IN
120 * 1 = Fault condition
121 * 0 = Normal operation
123 * 1 = Fault condition
124 * 0 = Normal operation
126 u8 duty_cycle
; /* The DUTY_CYCLE register is a 4-bit read/
127 * writable register used to control the duty
128 * cycle of the V OUT output.
132 /* helper to grab and cache data, at most one time per second */
133 static struct tc654_data
*tc654_update_client(struct device
*dev
)
135 struct tc654_data
*data
= dev_get_drvdata(dev
);
136 struct i2c_client
*client
= data
->client
;
139 mutex_lock(&data
->update_lock
);
140 if (time_before(jiffies
, data
->last_updated
+ TC654_UPDATE_INTERVAL
) &&
144 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_RPM(0));
147 data
->rpm_output
[0] = ret
;
149 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_RPM(1));
152 data
->rpm_output
[1] = ret
;
154 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_FAN_FAULT(0));
157 data
->fan_fault
[0] = ret
;
159 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_FAN_FAULT(1));
162 data
->fan_fault
[1] = ret
;
164 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_CONFIG
);
169 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_STATUS
);
174 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_DUTY_CYCLE
);
177 data
->duty_cycle
= ret
& 0x0f;
179 data
->last_updated
= jiffies
;
182 mutex_unlock(&data
->update_lock
);
184 if (ret
< 0) /* upon error, encode it in return value */
194 static ssize_t
fan_show(struct device
*dev
, struct device_attribute
*da
,
197 int nr
= to_sensor_dev_attr(da
)->index
;
198 struct tc654_data
*data
= tc654_update_client(dev
);
202 return PTR_ERR(data
);
204 if (data
->config
& TC654_REG_CONFIG_RES
)
205 val
= data
->rpm_output
[nr
] * TC654_HIGH_RPM_RESOLUTION
;
207 val
= data
->rpm_output
[nr
] * TC654_LOW_RPM_RESOLUTION
;
209 return sprintf(buf
, "%d\n", val
);
212 static ssize_t
fan_min_show(struct device
*dev
, struct device_attribute
*da
,
215 int nr
= to_sensor_dev_attr(da
)->index
;
216 struct tc654_data
*data
= tc654_update_client(dev
);
219 return PTR_ERR(data
);
221 return sprintf(buf
, "%d\n",
222 TC654_FAN_FAULT_FROM_REG(data
->fan_fault
[nr
]));
225 static ssize_t
fan_min_store(struct device
*dev
, struct device_attribute
*da
,
226 const char *buf
, size_t count
)
228 int nr
= to_sensor_dev_attr(da
)->index
;
229 struct tc654_data
*data
= dev_get_drvdata(dev
);
230 struct i2c_client
*client
= data
->client
;
234 if (kstrtoul(buf
, 10, &val
))
237 val
= clamp_val(val
, 0, 12750);
239 mutex_lock(&data
->update_lock
);
241 data
->fan_fault
[nr
] = TC654_FAN_FAULT_TO_REG(val
);
242 ret
= i2c_smbus_write_byte_data(client
, TC654_REG_FAN_FAULT(nr
),
243 data
->fan_fault
[nr
]);
245 mutex_unlock(&data
->update_lock
);
246 return ret
< 0 ? ret
: count
;
249 static ssize_t
fan_alarm_show(struct device
*dev
, struct device_attribute
*da
,
252 int nr
= to_sensor_dev_attr(da
)->index
;
253 struct tc654_data
*data
= tc654_update_client(dev
);
257 return PTR_ERR(data
);
260 val
= !!(data
->status
& TC654_REG_STATUS_F1F
);
262 val
= !!(data
->status
& TC654_REG_STATUS_F2F
);
264 return sprintf(buf
, "%d\n", val
);
267 static const u8 TC654_FAN_PULSE_SHIFT
[] = { 1, 3 };
269 static ssize_t
fan_pulses_show(struct device
*dev
,
270 struct device_attribute
*da
, char *buf
)
272 int nr
= to_sensor_dev_attr(da
)->index
;
273 struct tc654_data
*data
= tc654_update_client(dev
);
277 return PTR_ERR(data
);
279 val
= BIT((data
->config
>> TC654_FAN_PULSE_SHIFT
[nr
]) & 0x03);
280 return sprintf(buf
, "%d\n", val
);
283 static ssize_t
fan_pulses_store(struct device
*dev
,
284 struct device_attribute
*da
, const char *buf
,
287 int nr
= to_sensor_dev_attr(da
)->index
;
288 struct tc654_data
*data
= dev_get_drvdata(dev
);
289 struct i2c_client
*client
= data
->client
;
294 if (kstrtoul(buf
, 10, &val
))
314 mutex_lock(&data
->update_lock
);
316 data
->config
&= ~(0x03 << TC654_FAN_PULSE_SHIFT
[nr
]);
317 data
->config
|= (config
<< TC654_FAN_PULSE_SHIFT
[nr
]);
318 ret
= i2c_smbus_write_byte_data(client
, TC654_REG_CONFIG
, data
->config
);
320 mutex_unlock(&data
->update_lock
);
321 return ret
< 0 ? ret
: count
;
324 static ssize_t
pwm_mode_show(struct device
*dev
, struct device_attribute
*da
,
327 struct tc654_data
*data
= tc654_update_client(dev
);
330 return PTR_ERR(data
);
332 return sprintf(buf
, "%d\n", !!(data
->config
& TC654_REG_CONFIG_DUTYC
));
335 static ssize_t
pwm_mode_store(struct device
*dev
, struct device_attribute
*da
,
336 const char *buf
, size_t count
)
338 struct tc654_data
*data
= dev_get_drvdata(dev
);
339 struct i2c_client
*client
= data
->client
;
343 if (kstrtoul(buf
, 10, &val
))
346 if (val
!= 0 && val
!= 1)
349 mutex_lock(&data
->update_lock
);
352 data
->config
|= TC654_REG_CONFIG_DUTYC
;
354 data
->config
&= ~TC654_REG_CONFIG_DUTYC
;
356 ret
= i2c_smbus_write_byte_data(client
, TC654_REG_CONFIG
, data
->config
);
358 mutex_unlock(&data
->update_lock
);
359 return ret
< 0 ? ret
: count
;
362 static const int tc654_pwm_map
[16] = { 77, 88, 102, 112, 124, 136, 148, 160,
363 172, 184, 196, 207, 219, 231, 243, 255};
365 static ssize_t
pwm_show(struct device
*dev
, struct device_attribute
*da
,
368 struct tc654_data
*data
= tc654_update_client(dev
);
372 return PTR_ERR(data
);
374 if (data
->config
& TC654_REG_CONFIG_SDM
)
377 pwm
= tc654_pwm_map
[data
->duty_cycle
];
379 return sprintf(buf
, "%d\n", pwm
);
382 static ssize_t
pwm_store(struct device
*dev
, struct device_attribute
*da
,
383 const char *buf
, size_t count
)
385 struct tc654_data
*data
= dev_get_drvdata(dev
);
386 struct i2c_client
*client
= data
->client
;
390 if (kstrtoul(buf
, 10, &val
))
395 mutex_lock(&data
->update_lock
);
398 data
->config
|= TC654_REG_CONFIG_SDM
;
400 data
->config
&= ~TC654_REG_CONFIG_SDM
;
402 data
->duty_cycle
= find_closest(val
, tc654_pwm_map
,
403 ARRAY_SIZE(tc654_pwm_map
));
405 ret
= i2c_smbus_write_byte_data(client
, TC654_REG_CONFIG
, data
->config
);
409 ret
= i2c_smbus_write_byte_data(client
, TC654_REG_DUTY_CYCLE
,
413 mutex_unlock(&data
->update_lock
);
414 return ret
< 0 ? ret
: count
;
417 static SENSOR_DEVICE_ATTR_RO(fan1_input
, fan
, 0);
418 static SENSOR_DEVICE_ATTR_RO(fan2_input
, fan
, 1);
419 static SENSOR_DEVICE_ATTR_RW(fan1_min
, fan_min
, 0);
420 static SENSOR_DEVICE_ATTR_RW(fan2_min
, fan_min
, 1);
421 static SENSOR_DEVICE_ATTR_RO(fan1_alarm
, fan_alarm
, 0);
422 static SENSOR_DEVICE_ATTR_RO(fan2_alarm
, fan_alarm
, 1);
423 static SENSOR_DEVICE_ATTR_RW(fan1_pulses
, fan_pulses
, 0);
424 static SENSOR_DEVICE_ATTR_RW(fan2_pulses
, fan_pulses
, 1);
425 static SENSOR_DEVICE_ATTR_RW(pwm1_mode
, pwm_mode
, 0);
426 static SENSOR_DEVICE_ATTR_RW(pwm1
, pwm
, 0);
429 static struct attribute
*tc654_attrs
[] = {
430 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
431 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
432 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
433 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
434 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
435 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
436 &sensor_dev_attr_fan1_pulses
.dev_attr
.attr
,
437 &sensor_dev_attr_fan2_pulses
.dev_attr
.attr
,
438 &sensor_dev_attr_pwm1_mode
.dev_attr
.attr
,
439 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
443 ATTRIBUTE_GROUPS(tc654
);
446 * device probe and removal
449 static int tc654_probe(struct i2c_client
*client
,
450 const struct i2c_device_id
*id
)
452 struct device
*dev
= &client
->dev
;
453 struct tc654_data
*data
;
454 struct device
*hwmon_dev
;
457 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
460 data
= devm_kzalloc(dev
, sizeof(struct tc654_data
), GFP_KERNEL
);
464 data
->client
= client
;
465 mutex_init(&data
->update_lock
);
467 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_CONFIG
);
474 devm_hwmon_device_register_with_groups(dev
, client
->name
, data
,
476 return PTR_ERR_OR_ZERO(hwmon_dev
);
479 static const struct i2c_device_id tc654_id
[] = {
485 MODULE_DEVICE_TABLE(i2c
, tc654_id
);
487 static struct i2c_driver tc654_driver
= {
491 .probe
= tc654_probe
,
492 .id_table
= tc654_id
,
495 module_i2c_driver(tc654_driver
);
497 MODULE_AUTHOR("Allied Telesis Labs");
498 MODULE_DESCRIPTION("Microchip TC654/TC655 driver");
499 MODULE_LICENSE("GPL");