2 * tc654.c - Linux kernel modules for fan speed controller
4 * Copyright (C) 2016 Allied Telesis Labs NZ
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/bitops.h>
18 #include <linux/err.h>
19 #include <linux/hwmon.h>
20 #include <linux/hwmon-sysfs.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/jiffies.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/slab.h>
27 #include <linux/util_macros.h>
30 TC654_REG_RPM1
= 0x00, /* RPM Output 1 */
31 TC654_REG_RPM2
= 0x01, /* RPM Output 2 */
32 TC654_REG_FAN_FAULT1
= 0x02, /* Fan Fault 1 Threshold */
33 TC654_REG_FAN_FAULT2
= 0x03, /* Fan Fault 2 Threshold */
34 TC654_REG_CONFIG
= 0x04, /* Configuration */
35 TC654_REG_STATUS
= 0x05, /* Status */
36 TC654_REG_DUTY_CYCLE
= 0x06, /* Fan Speed Duty Cycle */
37 TC654_REG_MFR_ID
= 0x07, /* Manufacturer Identification */
38 TC654_REG_VER_ID
= 0x08, /* Version Identification */
41 /* Macros to easily index the registers */
42 #define TC654_REG_RPM(idx) (TC654_REG_RPM1 + (idx))
43 #define TC654_REG_FAN_FAULT(idx) (TC654_REG_FAN_FAULT1 + (idx))
45 /* Config register bits */
46 #define TC654_REG_CONFIG_RES BIT(6) /* Resolution Selection */
47 #define TC654_REG_CONFIG_DUTYC BIT(5) /* Duty Cycle Control */
48 #define TC654_REG_CONFIG_SDM BIT(0) /* Shutdown Mode */
50 /* Status register bits */
51 #define TC654_REG_STATUS_F2F BIT(1) /* Fan 2 Fault */
52 #define TC654_REG_STATUS_F1F BIT(0) /* Fan 1 Fault */
54 /* RPM resolution for RPM Output registers */
55 #define TC654_HIGH_RPM_RESOLUTION 25 /* 25 RPM resolution */
56 #define TC654_LOW_RPM_RESOLUTION 50 /* 50 RPM resolution */
58 /* Convert to the fan fault RPM threshold from register value */
59 #define TC654_FAN_FAULT_FROM_REG(val) ((val) * 50) /* 50 RPM resolution */
61 /* Convert to register value from the fan fault RPM threshold */
62 #define TC654_FAN_FAULT_TO_REG(val) (((val) / 50) & 0xff)
64 /* Register data is read (and cached) at most once per second. */
65 #define TC654_UPDATE_INTERVAL HZ
68 struct i2c_client
*client
;
71 struct mutex update_lock
;
73 /* tc654 register cache */
75 unsigned long last_updated
; /* in jiffies */
77 u8 rpm_output
[2]; /* The fan RPM data for fans 1 and 2 is then
78 * written to registers RPM1 and RPM2
80 u8 fan_fault
[2]; /* The Fan Fault Threshold Registers are used to
81 * set the fan fault threshold levels for fan 1
84 u8 config
; /* The Configuration Register is an 8-bit read/
85 * writable multi-function control register
88 * 0 = Normal Operation (default)
89 * 6: Resolution Selection for RPM Output Registers
90 * RPM Output Registers (RPM1 and RPM2) will be
92 * 1 = 25 RPM (9-bit) resolution
93 * 0 = 50 RPM (8-bit) resolution (default)
94 * 5: Duty Cycle Control Method
95 * The V OUT duty cycle will be controlled via
96 * 1 = the SMBus interface.
97 * 0 = via the V IN analog input pin. (default)
98 * 4,3: Fan 2 Pulses Per Rotation
103 * 2,1: Fan 1 Pulses Per Rotation
110 * 0 = Normal operation. (default)
112 u8 status
; /* The Status register provides all the information
113 * about what is going on within the TC654/TC655
115 * 7,6: Unimplemented, Read as '0'
116 * 5: Over-Temperature Fault Condition
117 * 1 = Over-Temperature condition has occurred
118 * 0 = Normal operation. V IN is less than 2.6V
119 * 4: RPM2 Counter Overflow
120 * 1 = Fault condition
121 * 0 = Normal operation
122 * 3: RPM1 Counter Overflow
123 * 1 = Fault condition
124 * 0 = Normal operation
125 * 2: V IN Input Status
127 * 0 = Normal operation. voltage present at V IN
129 * 1 = Fault condition
130 * 0 = Normal operation
132 * 1 = Fault condition
133 * 0 = Normal operation
135 u8 duty_cycle
; /* The DUTY_CYCLE register is a 4-bit read/
136 * writable register used to control the duty
137 * cycle of the V OUT output.
141 /* helper to grab and cache data, at most one time per second */
142 static struct tc654_data
*tc654_update_client(struct device
*dev
)
144 struct tc654_data
*data
= dev_get_drvdata(dev
);
145 struct i2c_client
*client
= data
->client
;
148 mutex_lock(&data
->update_lock
);
149 if (time_before(jiffies
, data
->last_updated
+ TC654_UPDATE_INTERVAL
) &&
153 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_RPM(0));
156 data
->rpm_output
[0] = ret
;
158 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_RPM(1));
161 data
->rpm_output
[1] = ret
;
163 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_FAN_FAULT(0));
166 data
->fan_fault
[0] = ret
;
168 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_FAN_FAULT(1));
171 data
->fan_fault
[1] = ret
;
173 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_CONFIG
);
178 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_STATUS
);
183 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_DUTY_CYCLE
);
186 data
->duty_cycle
= ret
& 0x0f;
188 data
->last_updated
= jiffies
;
191 mutex_unlock(&data
->update_lock
);
193 if (ret
< 0) /* upon error, encode it in return value */
203 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*da
,
206 int nr
= to_sensor_dev_attr(da
)->index
;
207 struct tc654_data
*data
= tc654_update_client(dev
);
211 return PTR_ERR(data
);
213 if (data
->config
& TC654_REG_CONFIG_RES
)
214 val
= data
->rpm_output
[nr
] * TC654_HIGH_RPM_RESOLUTION
;
216 val
= data
->rpm_output
[nr
] * TC654_LOW_RPM_RESOLUTION
;
218 return sprintf(buf
, "%d\n", val
);
221 static ssize_t
show_fan_min(struct device
*dev
, struct device_attribute
*da
,
224 int nr
= to_sensor_dev_attr(da
)->index
;
225 struct tc654_data
*data
= tc654_update_client(dev
);
228 return PTR_ERR(data
);
230 return sprintf(buf
, "%d\n",
231 TC654_FAN_FAULT_FROM_REG(data
->fan_fault
[nr
]));
234 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*da
,
235 const char *buf
, size_t count
)
237 int nr
= to_sensor_dev_attr(da
)->index
;
238 struct tc654_data
*data
= dev_get_drvdata(dev
);
239 struct i2c_client
*client
= data
->client
;
243 if (kstrtoul(buf
, 10, &val
))
246 val
= clamp_val(val
, 0, 12750);
248 mutex_lock(&data
->update_lock
);
250 data
->fan_fault
[nr
] = TC654_FAN_FAULT_TO_REG(val
);
251 ret
= i2c_smbus_write_byte_data(client
, TC654_REG_FAN_FAULT(nr
),
252 data
->fan_fault
[nr
]);
254 mutex_unlock(&data
->update_lock
);
255 return ret
< 0 ? ret
: count
;
258 static ssize_t
show_fan_alarm(struct device
*dev
, struct device_attribute
*da
,
261 int nr
= to_sensor_dev_attr(da
)->index
;
262 struct tc654_data
*data
= tc654_update_client(dev
);
266 return PTR_ERR(data
);
269 val
= !!(data
->status
& TC654_REG_STATUS_F1F
);
271 val
= !!(data
->status
& TC654_REG_STATUS_F2F
);
273 return sprintf(buf
, "%d\n", val
);
276 static const u8 TC654_FAN_PULSE_SHIFT
[] = { 1, 3 };
278 static ssize_t
show_fan_pulses(struct device
*dev
, struct device_attribute
*da
,
281 int nr
= to_sensor_dev_attr(da
)->index
;
282 struct tc654_data
*data
= tc654_update_client(dev
);
286 return PTR_ERR(data
);
288 val
= BIT((data
->config
>> TC654_FAN_PULSE_SHIFT
[nr
]) & 0x03);
289 return sprintf(buf
, "%d\n", val
);
292 static ssize_t
set_fan_pulses(struct device
*dev
, struct device_attribute
*da
,
293 const char *buf
, size_t count
)
295 int nr
= to_sensor_dev_attr(da
)->index
;
296 struct tc654_data
*data
= dev_get_drvdata(dev
);
297 struct i2c_client
*client
= data
->client
;
302 if (kstrtoul(buf
, 10, &val
))
322 mutex_lock(&data
->update_lock
);
324 data
->config
&= ~(0x03 << TC654_FAN_PULSE_SHIFT
[nr
]);
325 data
->config
|= (config
<< TC654_FAN_PULSE_SHIFT
[nr
]);
326 ret
= i2c_smbus_write_byte_data(client
, TC654_REG_CONFIG
, data
->config
);
328 mutex_unlock(&data
->update_lock
);
329 return ret
< 0 ? ret
: count
;
332 static ssize_t
show_pwm_mode(struct device
*dev
,
333 struct device_attribute
*da
, char *buf
)
335 struct tc654_data
*data
= tc654_update_client(dev
);
338 return PTR_ERR(data
);
340 return sprintf(buf
, "%d\n", !!(data
->config
& TC654_REG_CONFIG_DUTYC
));
343 static ssize_t
set_pwm_mode(struct device
*dev
,
344 struct device_attribute
*da
,
345 const char *buf
, size_t count
)
347 struct tc654_data
*data
= dev_get_drvdata(dev
);
348 struct i2c_client
*client
= data
->client
;
352 if (kstrtoul(buf
, 10, &val
))
355 if (val
!= 0 && val
!= 1)
358 mutex_lock(&data
->update_lock
);
361 data
->config
|= TC654_REG_CONFIG_DUTYC
;
363 data
->config
&= ~TC654_REG_CONFIG_DUTYC
;
365 ret
= i2c_smbus_write_byte_data(client
, TC654_REG_CONFIG
, data
->config
);
367 mutex_unlock(&data
->update_lock
);
368 return ret
< 0 ? ret
: count
;
371 static const int tc654_pwm_map
[16] = { 77, 88, 102, 112, 124, 136, 148, 160,
372 172, 184, 196, 207, 219, 231, 243, 255};
374 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*da
,
377 struct tc654_data
*data
= tc654_update_client(dev
);
381 return PTR_ERR(data
);
383 if (data
->config
& TC654_REG_CONFIG_SDM
)
386 pwm
= tc654_pwm_map
[data
->duty_cycle
];
388 return sprintf(buf
, "%d\n", pwm
);
391 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*da
,
392 const char *buf
, size_t count
)
394 struct tc654_data
*data
= dev_get_drvdata(dev
);
395 struct i2c_client
*client
= data
->client
;
399 if (kstrtoul(buf
, 10, &val
))
404 mutex_lock(&data
->update_lock
);
407 data
->config
|= TC654_REG_CONFIG_SDM
;
409 data
->config
&= ~TC654_REG_CONFIG_SDM
;
411 data
->duty_cycle
= find_closest(val
, tc654_pwm_map
,
412 ARRAY_SIZE(tc654_pwm_map
));
414 ret
= i2c_smbus_write_byte_data(client
, TC654_REG_CONFIG
, data
->config
);
418 ret
= i2c_smbus_write_byte_data(client
, TC654_REG_DUTY_CYCLE
,
422 mutex_unlock(&data
->update_lock
);
423 return ret
< 0 ? ret
: count
;
426 static SENSOR_DEVICE_ATTR(fan1_input
, S_IRUGO
, show_fan
, NULL
, 0);
427 static SENSOR_DEVICE_ATTR(fan2_input
, S_IRUGO
, show_fan
, NULL
, 1);
428 static SENSOR_DEVICE_ATTR(fan1_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
430 static SENSOR_DEVICE_ATTR(fan2_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
432 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_fan_alarm
, NULL
, 0);
433 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_fan_alarm
, NULL
, 1);
434 static SENSOR_DEVICE_ATTR(fan1_pulses
, S_IWUSR
| S_IRUGO
, show_fan_pulses
,
436 static SENSOR_DEVICE_ATTR(fan2_pulses
, S_IWUSR
| S_IRUGO
, show_fan_pulses
,
438 static SENSOR_DEVICE_ATTR(pwm1_mode
, S_IWUSR
| S_IRUGO
,
439 show_pwm_mode
, set_pwm_mode
, 0);
440 static SENSOR_DEVICE_ATTR(pwm1
, S_IWUSR
| S_IRUGO
, show_pwm
,
444 static struct attribute
*tc654_attrs
[] = {
445 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
446 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
447 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
448 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
449 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
450 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
451 &sensor_dev_attr_fan1_pulses
.dev_attr
.attr
,
452 &sensor_dev_attr_fan2_pulses
.dev_attr
.attr
,
453 &sensor_dev_attr_pwm1_mode
.dev_attr
.attr
,
454 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
458 ATTRIBUTE_GROUPS(tc654
);
461 * device probe and removal
464 static int tc654_probe(struct i2c_client
*client
,
465 const struct i2c_device_id
*id
)
467 struct device
*dev
= &client
->dev
;
468 struct tc654_data
*data
;
469 struct device
*hwmon_dev
;
472 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
475 data
= devm_kzalloc(dev
, sizeof(struct tc654_data
), GFP_KERNEL
);
479 data
->client
= client
;
480 mutex_init(&data
->update_lock
);
482 ret
= i2c_smbus_read_byte_data(client
, TC654_REG_CONFIG
);
489 devm_hwmon_device_register_with_groups(dev
, client
->name
, data
,
491 return PTR_ERR_OR_ZERO(hwmon_dev
);
494 static const struct i2c_device_id tc654_id
[] = {
500 MODULE_DEVICE_TABLE(i2c
, tc654_id
);
502 static struct i2c_driver tc654_driver
= {
506 .probe
= tc654_probe
,
507 .id_table
= tc654_id
,
510 module_i2c_driver(tc654_driver
);
512 MODULE_AUTHOR("Allied Telesis Labs");
513 MODULE_DESCRIPTION("Microchip TC654/TC655 driver");
514 MODULE_LICENSE("GPL");