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/thermal.h>
19 #include <linux/util_macros.h>
22 TC654_REG_RPM1 = 0x00, /* RPM Output 1 */
23 TC654_REG_RPM2 = 0x01, /* RPM Output 2 */
24 TC654_REG_FAN_FAULT1 = 0x02, /* Fan Fault 1 Threshold */
25 TC654_REG_FAN_FAULT2 = 0x03, /* Fan Fault 2 Threshold */
26 TC654_REG_CONFIG = 0x04, /* Configuration */
27 TC654_REG_STATUS = 0x05, /* Status */
28 TC654_REG_DUTY_CYCLE = 0x06, /* Fan Speed Duty Cycle */
29 TC654_REG_MFR_ID = 0x07, /* Manufacturer Identification */
30 TC654_REG_VER_ID = 0x08, /* Version Identification */
33 /* Macros to easily index the registers */
34 #define TC654_REG_RPM(idx) (TC654_REG_RPM1 + (idx))
35 #define TC654_REG_FAN_FAULT(idx) (TC654_REG_FAN_FAULT1 + (idx))
37 /* Config register bits */
38 #define TC654_REG_CONFIG_RES BIT(6) /* Resolution Selection */
39 #define TC654_REG_CONFIG_DUTYC BIT(5) /* Duty Cycle Control */
40 #define TC654_REG_CONFIG_SDM BIT(0) /* Shutdown Mode */
42 /* Status register bits */
43 #define TC654_REG_STATUS_F2F BIT(1) /* Fan 2 Fault */
44 #define TC654_REG_STATUS_F1F BIT(0) /* Fan 1 Fault */
46 /* RPM resolution for RPM Output registers */
47 #define TC654_HIGH_RPM_RESOLUTION 25 /* 25 RPM resolution */
48 #define TC654_LOW_RPM_RESOLUTION 50 /* 50 RPM resolution */
50 /* Convert to the fan fault RPM threshold from register value */
51 #define TC654_FAN_FAULT_FROM_REG(val) ((val) * 50) /* 50 RPM resolution */
53 /* Convert to register value from the fan fault RPM threshold */
54 #define TC654_FAN_FAULT_TO_REG(val) (((val) / 50) & 0xff)
56 /* Register data is read (and cached) at most once per second. */
57 #define TC654_UPDATE_INTERVAL HZ
60 struct i2c_client *client;
63 struct mutex update_lock;
65 /* tc654 register cache */
67 unsigned long last_updated; /* in jiffies */
69 u8 rpm_output[2]; /* The fan RPM data for fans 1 and 2 is then
70 * written to registers RPM1 and RPM2
72 u8 fan_fault[2]; /* The Fan Fault Threshold Registers are used to
73 * set the fan fault threshold levels for fan 1
76 u8 config; /* The Configuration Register is an 8-bit read/
77 * writable multi-function control register
80 * 0 = Normal Operation (default)
81 * 6: Resolution Selection for RPM Output Registers
82 * RPM Output Registers (RPM1 and RPM2) will be
84 * 1 = 25 RPM (9-bit) resolution
85 * 0 = 50 RPM (8-bit) resolution (default)
86 * 5: Duty Cycle Control Method
87 * The V OUT duty cycle will be controlled via
88 * 1 = the SMBus interface.
89 * 0 = via the V IN analog input pin. (default)
90 * 4,3: Fan 2 Pulses Per Rotation
95 * 2,1: Fan 1 Pulses Per Rotation
102 * 0 = Normal operation. (default)
104 u8 status; /* The Status register provides all the information
105 * about what is going on within the TC654/TC655
107 * 7,6: Unimplemented, Read as '0'
108 * 5: Over-Temperature Fault Condition
109 * 1 = Over-Temperature condition has occurred
110 * 0 = Normal operation. V IN is less than 2.6V
111 * 4: RPM2 Counter Overflow
112 * 1 = Fault condition
113 * 0 = Normal operation
114 * 3: RPM1 Counter Overflow
115 * 1 = Fault condition
116 * 0 = Normal operation
117 * 2: V IN Input Status
119 * 0 = Normal operation. voltage present at V IN
121 * 1 = Fault condition
122 * 0 = Normal operation
124 * 1 = Fault condition
125 * 0 = Normal operation
127 u8 duty_cycle; /* The DUTY_CYCLE register is a 4-bit read/
128 * writable register used to control the duty
129 * cycle of the V OUT output.
133 /* helper to grab and cache data, at most one time per second */
134 static struct tc654_data *tc654_update_client(struct device *dev)
136 struct tc654_data *data = dev_get_drvdata(dev);
137 struct i2c_client *client = data->client;
140 mutex_lock(&data->update_lock);
141 if (time_before(jiffies, data->last_updated + TC654_UPDATE_INTERVAL) &&
145 ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(0));
148 data->rpm_output[0] = ret;
150 ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(1));
153 data->rpm_output[1] = ret;
155 ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(0));
158 data->fan_fault[0] = ret;
160 ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(1));
163 data->fan_fault[1] = ret;
165 ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
170 ret = i2c_smbus_read_byte_data(client, TC654_REG_STATUS);
175 ret = i2c_smbus_read_byte_data(client, TC654_REG_DUTY_CYCLE);
178 data->duty_cycle = ret & 0x0f;
180 data->last_updated = jiffies;
183 mutex_unlock(&data->update_lock);
185 if (ret < 0) /* upon error, encode it in return value */
195 static ssize_t fan_show(struct device *dev, struct device_attribute *da,
198 int nr = to_sensor_dev_attr(da)->index;
199 struct tc654_data *data = tc654_update_client(dev);
203 return PTR_ERR(data);
205 if (data->config & TC654_REG_CONFIG_RES)
206 val = data->rpm_output[nr] * TC654_HIGH_RPM_RESOLUTION;
208 val = data->rpm_output[nr] * TC654_LOW_RPM_RESOLUTION;
210 return sprintf(buf, "%d\n", val);
213 static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
216 int nr = to_sensor_dev_attr(da)->index;
217 struct tc654_data *data = tc654_update_client(dev);
220 return PTR_ERR(data);
222 return sprintf(buf, "%d\n",
223 TC654_FAN_FAULT_FROM_REG(data->fan_fault[nr]));
226 static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
227 const char *buf, size_t count)
229 int nr = to_sensor_dev_attr(da)->index;
230 struct tc654_data *data = dev_get_drvdata(dev);
231 struct i2c_client *client = data->client;
235 if (kstrtoul(buf, 10, &val))
238 val = clamp_val(val, 0, 12750);
240 mutex_lock(&data->update_lock);
242 data->fan_fault[nr] = TC654_FAN_FAULT_TO_REG(val);
243 ret = i2c_smbus_write_byte_data(client, TC654_REG_FAN_FAULT(nr),
244 data->fan_fault[nr]);
246 mutex_unlock(&data->update_lock);
247 return ret < 0 ? ret : count;
250 static ssize_t fan_alarm_show(struct device *dev, struct device_attribute *da,
253 int nr = to_sensor_dev_attr(da)->index;
254 struct tc654_data *data = tc654_update_client(dev);
258 return PTR_ERR(data);
261 val = !!(data->status & TC654_REG_STATUS_F1F);
263 val = !!(data->status & TC654_REG_STATUS_F2F);
265 return sprintf(buf, "%d\n", val);
268 static const u8 TC654_FAN_PULSE_SHIFT[] = { 1, 3 };
270 static ssize_t fan_pulses_show(struct device *dev,
271 struct device_attribute *da, char *buf)
273 int nr = to_sensor_dev_attr(da)->index;
274 struct tc654_data *data = tc654_update_client(dev);
278 return PTR_ERR(data);
280 val = BIT((data->config >> TC654_FAN_PULSE_SHIFT[nr]) & 0x03);
281 return sprintf(buf, "%d\n", val);
284 static ssize_t fan_pulses_store(struct device *dev,
285 struct device_attribute *da, const char *buf,
288 int nr = to_sensor_dev_attr(da)->index;
289 struct tc654_data *data = dev_get_drvdata(dev);
290 struct i2c_client *client = data->client;
295 if (kstrtoul(buf, 10, &val))
315 mutex_lock(&data->update_lock);
317 data->config &= ~(0x03 << TC654_FAN_PULSE_SHIFT[nr]);
318 data->config |= (config << TC654_FAN_PULSE_SHIFT[nr]);
319 ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
321 mutex_unlock(&data->update_lock);
322 return ret < 0 ? ret : count;
325 static ssize_t pwm_mode_show(struct device *dev, struct device_attribute *da,
328 struct tc654_data *data = tc654_update_client(dev);
331 return PTR_ERR(data);
333 return sprintf(buf, "%d\n", !!(data->config & TC654_REG_CONFIG_DUTYC));
336 static ssize_t pwm_mode_store(struct device *dev, struct device_attribute *da,
337 const char *buf, size_t count)
339 struct tc654_data *data = dev_get_drvdata(dev);
340 struct i2c_client *client = data->client;
344 if (kstrtoul(buf, 10, &val))
347 if (val != 0 && val != 1)
350 mutex_lock(&data->update_lock);
353 data->config |= TC654_REG_CONFIG_DUTYC;
355 data->config &= ~TC654_REG_CONFIG_DUTYC;
357 ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
359 mutex_unlock(&data->update_lock);
360 return ret < 0 ? ret : count;
363 static const int tc654_pwm_map[16] = { 77, 88, 102, 112, 124, 136, 148, 160,
364 172, 184, 196, 207, 219, 231, 243, 255};
366 static ssize_t pwm_show(struct device *dev, struct device_attribute *da,
369 struct tc654_data *data = tc654_update_client(dev);
373 return PTR_ERR(data);
375 if (data->config & TC654_REG_CONFIG_SDM)
378 pwm = tc654_pwm_map[data->duty_cycle];
380 return sprintf(buf, "%d\n", pwm);
383 static int _set_pwm(struct tc654_data *data, unsigned long val)
385 struct i2c_client *client = data->client;
388 mutex_lock(&data->update_lock);
391 data->config |= TC654_REG_CONFIG_SDM;
392 data->duty_cycle = 0;
394 data->config &= ~TC654_REG_CONFIG_SDM;
395 data->duty_cycle = val - 1;
398 ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
402 ret = i2c_smbus_write_byte_data(client, TC654_REG_DUTY_CYCLE,
406 mutex_unlock(&data->update_lock);
410 static ssize_t pwm_store(struct device *dev, struct device_attribute *da,
411 const char *buf, size_t count)
413 struct tc654_data *data = dev_get_drvdata(dev);
417 if (kstrtoul(buf, 10, &val))
422 val = find_closest(val, tc654_pwm_map, ARRAY_SIZE(tc654_pwm_map)) + 1;
424 ret = _set_pwm(data, val);
425 return ret < 0 ? ret : count;
428 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
429 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
430 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
431 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
432 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0);
433 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1);
434 static SENSOR_DEVICE_ATTR_RW(fan1_pulses, fan_pulses, 0);
435 static SENSOR_DEVICE_ATTR_RW(fan2_pulses, fan_pulses, 1);
436 static SENSOR_DEVICE_ATTR_RW(pwm1_mode, pwm_mode, 0);
437 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
440 static struct attribute *tc654_attrs[] = {
441 &sensor_dev_attr_fan1_input.dev_attr.attr,
442 &sensor_dev_attr_fan2_input.dev_attr.attr,
443 &sensor_dev_attr_fan1_min.dev_attr.attr,
444 &sensor_dev_attr_fan2_min.dev_attr.attr,
445 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
446 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
447 &sensor_dev_attr_fan1_pulses.dev_attr.attr,
448 &sensor_dev_attr_fan2_pulses.dev_attr.attr,
449 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
450 &sensor_dev_attr_pwm1.dev_attr.attr,
454 ATTRIBUTE_GROUPS(tc654);
457 * thermal cooling device functions
459 * Account for the "ShutDown Mode (SDM)" state by offsetting
460 * the 16 PWM duty cycle states by 1.
462 * State 0 = 0% PWM | Shutdown - Fan(s) are off
463 * State 1 = 30% PWM | duty_cycle = 0
464 * State 2 = ~35% PWM | duty_cycle = 1
466 * State 15 = ~95% PWM | duty_cycle = 14
467 * State 16 = 100% PWM | duty_cycle = 15
469 #define TC654_MAX_COOLING_STATE 16
471 static int tc654_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
473 *state = TC654_MAX_COOLING_STATE;
477 static int tc654_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
479 struct tc654_data *data = tc654_update_client(cdev->devdata);
482 return PTR_ERR(data);
484 if (data->config & TC654_REG_CONFIG_SDM)
485 *state = 0; /* FAN is off */
487 *state = data->duty_cycle + 1; /* offset PWM States by 1 */
492 static int tc654_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
494 struct tc654_data *data = tc654_update_client(cdev->devdata);
497 return PTR_ERR(data);
499 return _set_pwm(data, clamp_val(state, 0, TC654_MAX_COOLING_STATE));
502 static const struct thermal_cooling_device_ops tc654_fan_cool_ops = {
503 .get_max_state = tc654_get_max_state,
504 .get_cur_state = tc654_get_cur_state,
505 .set_cur_state = tc654_set_cur_state,
509 * device probe and removal
512 static int tc654_probe(struct i2c_client *client)
514 struct device *dev = &client->dev;
515 struct tc654_data *data;
516 struct device *hwmon_dev;
519 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
522 data = devm_kzalloc(dev, sizeof(struct tc654_data), GFP_KERNEL);
526 data->client = client;
527 mutex_init(&data->update_lock);
529 ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
536 devm_hwmon_device_register_with_groups(dev, client->name, data,
538 if (IS_ERR(hwmon_dev))
539 return PTR_ERR(hwmon_dev);
541 if (IS_ENABLED(CONFIG_THERMAL)) {
542 struct thermal_cooling_device *cdev;
544 cdev = devm_thermal_of_cooling_device_register(dev, dev->of_node, client->name,
545 hwmon_dev, &tc654_fan_cool_ops);
546 return PTR_ERR_OR_ZERO(cdev);
552 static const struct i2c_device_id tc654_id[] = {
558 MODULE_DEVICE_TABLE(i2c, tc654_id);
560 static struct i2c_driver tc654_driver = {
564 .probe = tc654_probe,
565 .id_table = tc654_id,
568 module_i2c_driver(tc654_driver);
570 MODULE_AUTHOR("Allied Telesis Labs");
571 MODULE_DESCRIPTION("Microchip TC654/TC655 driver");
572 MODULE_LICENSE("GPL");