Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * tc654.c - Linux kernel modules for fan speed controller
  4 *
  5 * Copyright (C) 2016 Allied Telesis Labs NZ
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/bitops.h>
  9#include <linux/err.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>
 19
 20enum tc654_regs {
 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 */
 30};
 31
 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))
 35
 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 */
 40
 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 */
 44
 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 */
 48
 49/* Convert to the fan fault RPM threshold from register value */
 50#define TC654_FAN_FAULT_FROM_REG(val)	((val) * 50)	/* 50 RPM resolution */
 51
 52/* Convert to register value from the fan fault RPM threshold */
 53#define TC654_FAN_FAULT_TO_REG(val)	(((val) / 50) & 0xff)
 54
 55/* Register data is read (and cached) at most once per second. */
 56#define TC654_UPDATE_INTERVAL		HZ
 57
 58struct tc654_data {
 59	struct i2c_client *client;
 60
 61	/* update mutex */
 62	struct mutex update_lock;
 63
 64	/* tc654 register cache */
 65	bool valid;
 66	unsigned long last_updated;	/* in jiffies */
 67
 68	u8 rpm_output[2];	/* The fan RPM data for fans 1 and 2 is then
 69				 * written to registers RPM1 and RPM2
 70				 */
 71	u8 fan_fault[2];	/* The Fan Fault Threshold Registers are used to
 72				 * set the fan fault threshold levels for fan 1
 73				 * and fan 2
 74				 */
 75	u8 config;	/* The Configuration Register is an 8-bit read/
 76			 * writable multi-function control register
 77			 *   7: Fan Fault Clear
 78			 *      1 = Clear Fan Fault
 79			 *      0 = Normal Operation (default)
 80			 *   6: Resolution Selection for RPM Output Registers
 81			 *      RPM Output Registers (RPM1 and RPM2) will be
 82			 *      set for
 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
 90			 *      00 = 1
 91			 *      01 = 2 (default)
 92			 *      10 = 4
 93			 *      11 = 8
 94			 * 2,1: Fan 1 Pulses Per Rotation
 95			 *      00 = 1
 96			 *      01 = 2 (default)
 97			 *      10 = 4
 98			 *      11 = 8
 99			 *   0: Shutdown Mode
100			 *      1 = Shutdown mode.
101			 *      0 = Normal operation. (default)
102			 */
103	u8 status;	/* The Status register provides all the information
104			 * about what is going on within the TC654/TC655
105			 * devices.
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
117			 *      1 = V IN is open
118			 *      0 = Normal operation. voltage present at V IN
119			 *   1: Fan 2 Fault
120			 *      1 = Fault condition
121			 *      0 = Normal operation
122			 *   0: Fan 1 Fault
123			 *      1 = Fault condition
124			 *      0 = Normal operation
125			 */
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.
129			 */
130};
131
132/* helper to grab and cache data, at most one time per second */
133static struct tc654_data *tc654_update_client(struct device *dev)
134{
135	struct tc654_data *data = dev_get_drvdata(dev);
136	struct i2c_client *client = data->client;
137	int ret = 0;
138
139	mutex_lock(&data->update_lock);
140	if (time_before(jiffies, data->last_updated + TC654_UPDATE_INTERVAL) &&
141	    likely(data->valid))
142		goto out;
143
144	ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(0));
145	if (ret < 0)
146		goto out;
147	data->rpm_output[0] = ret;
148
149	ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(1));
150	if (ret < 0)
151		goto out;
152	data->rpm_output[1] = ret;
153
154	ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(0));
155	if (ret < 0)
156		goto out;
157	data->fan_fault[0] = ret;
158
159	ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(1));
160	if (ret < 0)
161		goto out;
162	data->fan_fault[1] = ret;
163
164	ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
165	if (ret < 0)
166		goto out;
167	data->config = ret;
168
169	ret = i2c_smbus_read_byte_data(client, TC654_REG_STATUS);
170	if (ret < 0)
171		goto out;
172	data->status = ret;
173
174	ret = i2c_smbus_read_byte_data(client, TC654_REG_DUTY_CYCLE);
175	if (ret < 0)
176		goto out;
177	data->duty_cycle = ret & 0x0f;
178
179	data->last_updated = jiffies;
180	data->valid = true;
181out:
182	mutex_unlock(&data->update_lock);
183
184	if (ret < 0)		/* upon error, encode it in return value */
185		data = ERR_PTR(ret);
186
187	return data;
188}
189
190/*
191 * sysfs attributes
192 */
193
194static ssize_t fan_show(struct device *dev, struct device_attribute *da,
195			char *buf)
196{
197	int nr = to_sensor_dev_attr(da)->index;
198	struct tc654_data *data = tc654_update_client(dev);
199	int val;
200
201	if (IS_ERR(data))
202		return PTR_ERR(data);
203
204	if (data->config & TC654_REG_CONFIG_RES)
205		val = data->rpm_output[nr] * TC654_HIGH_RPM_RESOLUTION;
206	else
207		val = data->rpm_output[nr] * TC654_LOW_RPM_RESOLUTION;
208
209	return sprintf(buf, "%d\n", val);
210}
211
212static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
213			    char *buf)
214{
215	int nr = to_sensor_dev_attr(da)->index;
216	struct tc654_data *data = tc654_update_client(dev);
217
218	if (IS_ERR(data))
219		return PTR_ERR(data);
220
221	return sprintf(buf, "%d\n",
222		       TC654_FAN_FAULT_FROM_REG(data->fan_fault[nr]));
223}
224
225static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
226			     const char *buf, size_t count)
227{
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;
231	unsigned long val;
232	int ret;
233
234	if (kstrtoul(buf, 10, &val))
235		return -EINVAL;
236
237	val = clamp_val(val, 0, 12750);
238
239	mutex_lock(&data->update_lock);
240
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]);
244
245	mutex_unlock(&data->update_lock);
246	return ret < 0 ? ret : count;
247}
248
249static ssize_t fan_alarm_show(struct device *dev, struct device_attribute *da,
250			      char *buf)
251{
252	int nr = to_sensor_dev_attr(da)->index;
253	struct tc654_data *data = tc654_update_client(dev);
254	int val;
255
256	if (IS_ERR(data))
257		return PTR_ERR(data);
258
259	if (nr == 0)
260		val = !!(data->status & TC654_REG_STATUS_F1F);
261	else
262		val = !!(data->status & TC654_REG_STATUS_F2F);
263
264	return sprintf(buf, "%d\n", val);
265}
266
267static const u8 TC654_FAN_PULSE_SHIFT[] = { 1, 3 };
268
269static ssize_t fan_pulses_show(struct device *dev,
270			       struct device_attribute *da, char *buf)
271{
272	int nr = to_sensor_dev_attr(da)->index;
273	struct tc654_data *data = tc654_update_client(dev);
274	u8 val;
275
276	if (IS_ERR(data))
277		return PTR_ERR(data);
278
279	val = BIT((data->config >> TC654_FAN_PULSE_SHIFT[nr]) & 0x03);
280	return sprintf(buf, "%d\n", val);
281}
282
283static ssize_t fan_pulses_store(struct device *dev,
284				struct device_attribute *da, const char *buf,
285				size_t count)
286{
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;
290	u8 config;
291	unsigned long val;
292	int ret;
293
294	if (kstrtoul(buf, 10, &val))
295		return -EINVAL;
296
297	switch (val) {
298	case 1:
299		config = 0;
300		break;
301	case 2:
302		config = 1;
303		break;
304	case 4:
305		config = 2;
306		break;
307	case 8:
308		config = 3;
309		break;
310	default:
311		return -EINVAL;
312	}
313
314	mutex_lock(&data->update_lock);
315
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);
319
320	mutex_unlock(&data->update_lock);
321	return ret < 0 ? ret : count;
322}
323
324static ssize_t pwm_mode_show(struct device *dev, struct device_attribute *da,
325			     char *buf)
326{
327	struct tc654_data *data = tc654_update_client(dev);
328
329	if (IS_ERR(data))
330		return PTR_ERR(data);
331
332	return sprintf(buf, "%d\n", !!(data->config & TC654_REG_CONFIG_DUTYC));
333}
334
335static ssize_t pwm_mode_store(struct device *dev, struct device_attribute *da,
336			      const char *buf, size_t count)
 
337{
338	struct tc654_data *data = dev_get_drvdata(dev);
339	struct i2c_client *client = data->client;
340	unsigned long val;
341	int ret;
342
343	if (kstrtoul(buf, 10, &val))
344		return -EINVAL;
345
346	if (val != 0 && val != 1)
347		return -EINVAL;
348
349	mutex_lock(&data->update_lock);
350
351	if (val)
352		data->config |= TC654_REG_CONFIG_DUTYC;
353	else
354		data->config &= ~TC654_REG_CONFIG_DUTYC;
355
356	ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
357
358	mutex_unlock(&data->update_lock);
359	return ret < 0 ? ret : count;
360}
361
362static const int tc654_pwm_map[16] = { 77,  88, 102, 112, 124, 136, 148, 160,
363				      172, 184, 196, 207, 219, 231, 243, 255};
364
365static ssize_t pwm_show(struct device *dev, struct device_attribute *da,
366			char *buf)
367{
368	struct tc654_data *data = tc654_update_client(dev);
369	int pwm;
370
371	if (IS_ERR(data))
372		return PTR_ERR(data);
373
374	if (data->config & TC654_REG_CONFIG_SDM)
375		pwm = 0;
376	else
377		pwm = tc654_pwm_map[data->duty_cycle];
378
379	return sprintf(buf, "%d\n", pwm);
380}
381
382static ssize_t pwm_store(struct device *dev, struct device_attribute *da,
383			 const char *buf, size_t count)
384{
385	struct tc654_data *data = dev_get_drvdata(dev);
386	struct i2c_client *client = data->client;
387	unsigned long val;
388	int ret;
389
390	if (kstrtoul(buf, 10, &val))
391		return -EINVAL;
392	if (val > 255)
393		return -EINVAL;
394
395	mutex_lock(&data->update_lock);
396
397	if (val == 0)
398		data->config |= TC654_REG_CONFIG_SDM;
399	else
400		data->config &= ~TC654_REG_CONFIG_SDM;
401
402	data->duty_cycle = find_closest(val, tc654_pwm_map,
403					ARRAY_SIZE(tc654_pwm_map));
404
405	ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
406	if (ret < 0)
407		goto out;
408
409	ret = i2c_smbus_write_byte_data(client, TC654_REG_DUTY_CYCLE,
410					data->duty_cycle);
411
412out:
413	mutex_unlock(&data->update_lock);
414	return ret < 0 ? ret : count;
415}
416
417static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
418static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
419static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
420static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
421static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0);
422static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1);
423static SENSOR_DEVICE_ATTR_RW(fan1_pulses, fan_pulses, 0);
424static SENSOR_DEVICE_ATTR_RW(fan2_pulses, fan_pulses, 1);
425static SENSOR_DEVICE_ATTR_RW(pwm1_mode, pwm_mode, 0);
426static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
 
 
 
 
 
 
427
428/* Driver data */
429static 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,
440	NULL
441};
442
443ATTRIBUTE_GROUPS(tc654);
444
445/*
446 * device probe and removal
447 */
448
449static int tc654_probe(struct i2c_client *client,
450		       const struct i2c_device_id *id)
451{
452	struct device *dev = &client->dev;
453	struct tc654_data *data;
454	struct device *hwmon_dev;
455	int ret;
456
457	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
458		return -ENODEV;
459
460	data = devm_kzalloc(dev, sizeof(struct tc654_data), GFP_KERNEL);
461	if (!data)
462		return -ENOMEM;
463
464	data->client = client;
465	mutex_init(&data->update_lock);
466
467	ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
468	if (ret < 0)
469		return ret;
470
471	data->config = ret;
472
473	hwmon_dev =
474	    devm_hwmon_device_register_with_groups(dev, client->name, data,
475						   tc654_groups);
476	return PTR_ERR_OR_ZERO(hwmon_dev);
477}
478
479static const struct i2c_device_id tc654_id[] = {
480	{"tc654", 0},
481	{"tc655", 0},
482	{}
483};
484
485MODULE_DEVICE_TABLE(i2c, tc654_id);
486
487static struct i2c_driver tc654_driver = {
488	.driver = {
489		   .name = "tc654",
490		   },
491	.probe = tc654_probe,
492	.id_table = tc654_id,
493};
494
495module_i2c_driver(tc654_driver);
496
497MODULE_AUTHOR("Allied Telesis Labs");
498MODULE_DESCRIPTION("Microchip TC654/TC655 driver");
499MODULE_LICENSE("GPL");
v4.17
 
  1/*
  2 * tc654.c - Linux kernel modules for fan speed controller
  3 *
  4 * Copyright (C) 2016 Allied Telesis Labs NZ
  5 *
  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.
 10 *
 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.
 15 */
 16
 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>
 28
 29enum tc654_regs {
 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 */
 39};
 40
 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))
 44
 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 */
 49
 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 */
 53
 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 */
 57
 58/* Convert to the fan fault RPM threshold from register value */
 59#define TC654_FAN_FAULT_FROM_REG(val)	((val) * 50)	/* 50 RPM resolution */
 60
 61/* Convert to register value from the fan fault RPM threshold */
 62#define TC654_FAN_FAULT_TO_REG(val)	(((val) / 50) & 0xff)
 63
 64/* Register data is read (and cached) at most once per second. */
 65#define TC654_UPDATE_INTERVAL		HZ
 66
 67struct tc654_data {
 68	struct i2c_client *client;
 69
 70	/* update mutex */
 71	struct mutex update_lock;
 72
 73	/* tc654 register cache */
 74	bool valid;
 75	unsigned long last_updated;	/* in jiffies */
 76
 77	u8 rpm_output[2];	/* The fan RPM data for fans 1 and 2 is then
 78				 * written to registers RPM1 and RPM2
 79				 */
 80	u8 fan_fault[2];	/* The Fan Fault Threshold Registers are used to
 81				 * set the fan fault threshold levels for fan 1
 82				 * and fan 2
 83				 */
 84	u8 config;	/* The Configuration Register is an 8-bit read/
 85			 * writable multi-function control register
 86			 *   7: Fan Fault Clear
 87			 *      1 = Clear Fan Fault
 88			 *      0 = Normal Operation (default)
 89			 *   6: Resolution Selection for RPM Output Registers
 90			 *      RPM Output Registers (RPM1 and RPM2) will be
 91			 *      set for
 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
 99			 *      00 = 1
100			 *      01 = 2 (default)
101			 *      10 = 4
102			 *      11 = 8
103			 * 2,1: Fan 1 Pulses Per Rotation
104			 *      00 = 1
105			 *      01 = 2 (default)
106			 *      10 = 4
107			 *      11 = 8
108			 *   0: Shutdown Mode
109			 *      1 = Shutdown mode.
110			 *      0 = Normal operation. (default)
111			 */
112	u8 status;	/* The Status register provides all the information
113			 * about what is going on within the TC654/TC655
114			 * devices.
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
126			 *      1 = V IN is open
127			 *      0 = Normal operation. voltage present at V IN
128			 *   1: Fan 2 Fault
129			 *      1 = Fault condition
130			 *      0 = Normal operation
131			 *   0: Fan 1 Fault
132			 *      1 = Fault condition
133			 *      0 = Normal operation
134			 */
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.
138			 */
139};
140
141/* helper to grab and cache data, at most one time per second */
142static struct tc654_data *tc654_update_client(struct device *dev)
143{
144	struct tc654_data *data = dev_get_drvdata(dev);
145	struct i2c_client *client = data->client;
146	int ret = 0;
147
148	mutex_lock(&data->update_lock);
149	if (time_before(jiffies, data->last_updated + TC654_UPDATE_INTERVAL) &&
150	    likely(data->valid))
151		goto out;
152
153	ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(0));
154	if (ret < 0)
155		goto out;
156	data->rpm_output[0] = ret;
157
158	ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(1));
159	if (ret < 0)
160		goto out;
161	data->rpm_output[1] = ret;
162
163	ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(0));
164	if (ret < 0)
165		goto out;
166	data->fan_fault[0] = ret;
167
168	ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(1));
169	if (ret < 0)
170		goto out;
171	data->fan_fault[1] = ret;
172
173	ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
174	if (ret < 0)
175		goto out;
176	data->config = ret;
177
178	ret = i2c_smbus_read_byte_data(client, TC654_REG_STATUS);
179	if (ret < 0)
180		goto out;
181	data->status = ret;
182
183	ret = i2c_smbus_read_byte_data(client, TC654_REG_DUTY_CYCLE);
184	if (ret < 0)
185		goto out;
186	data->duty_cycle = ret & 0x0f;
187
188	data->last_updated = jiffies;
189	data->valid = true;
190out:
191	mutex_unlock(&data->update_lock);
192
193	if (ret < 0)		/* upon error, encode it in return value */
194		data = ERR_PTR(ret);
195
196	return data;
197}
198
199/*
200 * sysfs attributes
201 */
202
203static ssize_t show_fan(struct device *dev, struct device_attribute *da,
204			char *buf)
205{
206	int nr = to_sensor_dev_attr(da)->index;
207	struct tc654_data *data = tc654_update_client(dev);
208	int val;
209
210	if (IS_ERR(data))
211		return PTR_ERR(data);
212
213	if (data->config & TC654_REG_CONFIG_RES)
214		val = data->rpm_output[nr] * TC654_HIGH_RPM_RESOLUTION;
215	else
216		val = data->rpm_output[nr] * TC654_LOW_RPM_RESOLUTION;
217
218	return sprintf(buf, "%d\n", val);
219}
220
221static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
222			    char *buf)
223{
224	int nr = to_sensor_dev_attr(da)->index;
225	struct tc654_data *data = tc654_update_client(dev);
226
227	if (IS_ERR(data))
228		return PTR_ERR(data);
229
230	return sprintf(buf, "%d\n",
231		       TC654_FAN_FAULT_FROM_REG(data->fan_fault[nr]));
232}
233
234static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
235			   const char *buf, size_t count)
236{
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;
240	unsigned long val;
241	int ret;
242
243	if (kstrtoul(buf, 10, &val))
244		return -EINVAL;
245
246	val = clamp_val(val, 0, 12750);
247
248	mutex_lock(&data->update_lock);
249
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]);
253
254	mutex_unlock(&data->update_lock);
255	return ret < 0 ? ret : count;
256}
257
258static ssize_t show_fan_alarm(struct device *dev, struct device_attribute *da,
259			      char *buf)
260{
261	int nr = to_sensor_dev_attr(da)->index;
262	struct tc654_data *data = tc654_update_client(dev);
263	int val;
264
265	if (IS_ERR(data))
266		return PTR_ERR(data);
267
268	if (nr == 0)
269		val = !!(data->status & TC654_REG_STATUS_F1F);
270	else
271		val = !!(data->status & TC654_REG_STATUS_F2F);
272
273	return sprintf(buf, "%d\n", val);
274}
275
276static const u8 TC654_FAN_PULSE_SHIFT[] = { 1, 3 };
277
278static ssize_t show_fan_pulses(struct device *dev, struct device_attribute *da,
279			       char *buf)
280{
281	int nr = to_sensor_dev_attr(da)->index;
282	struct tc654_data *data = tc654_update_client(dev);
283	u8 val;
284
285	if (IS_ERR(data))
286		return PTR_ERR(data);
287
288	val = BIT((data->config >> TC654_FAN_PULSE_SHIFT[nr]) & 0x03);
289	return sprintf(buf, "%d\n", val);
290}
291
292static ssize_t set_fan_pulses(struct device *dev, struct device_attribute *da,
293			      const char *buf, size_t count)
 
294{
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;
298	u8 config;
299	unsigned long val;
300	int ret;
301
302	if (kstrtoul(buf, 10, &val))
303		return -EINVAL;
304
305	switch (val) {
306	case 1:
307		config = 0;
308		break;
309	case 2:
310		config = 1;
311		break;
312	case 4:
313		config = 2;
314		break;
315	case 8:
316		config = 3;
317		break;
318	default:
319		return -EINVAL;
320	}
321
322	mutex_lock(&data->update_lock);
323
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);
327
328	mutex_unlock(&data->update_lock);
329	return ret < 0 ? ret : count;
330}
331
332static ssize_t show_pwm_mode(struct device *dev,
333			     struct device_attribute *da, char *buf)
334{
335	struct tc654_data *data = tc654_update_client(dev);
336
337	if (IS_ERR(data))
338		return PTR_ERR(data);
339
340	return sprintf(buf, "%d\n", !!(data->config & TC654_REG_CONFIG_DUTYC));
341}
342
343static ssize_t set_pwm_mode(struct device *dev,
344			    struct device_attribute *da,
345			    const char *buf, size_t count)
346{
347	struct tc654_data *data = dev_get_drvdata(dev);
348	struct i2c_client *client = data->client;
349	unsigned long val;
350	int ret;
351
352	if (kstrtoul(buf, 10, &val))
353		return -EINVAL;
354
355	if (val != 0 && val != 1)
356		return -EINVAL;
357
358	mutex_lock(&data->update_lock);
359
360	if (val)
361		data->config |= TC654_REG_CONFIG_DUTYC;
362	else
363		data->config &= ~TC654_REG_CONFIG_DUTYC;
364
365	ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
366
367	mutex_unlock(&data->update_lock);
368	return ret < 0 ? ret : count;
369}
370
371static const int tc654_pwm_map[16] = { 77,  88, 102, 112, 124, 136, 148, 160,
372				      172, 184, 196, 207, 219, 231, 243, 255};
373
374static ssize_t show_pwm(struct device *dev, struct device_attribute *da,
375			char *buf)
376{
377	struct tc654_data *data = tc654_update_client(dev);
378	int pwm;
379
380	if (IS_ERR(data))
381		return PTR_ERR(data);
382
383	if (data->config & TC654_REG_CONFIG_SDM)
384		pwm = 0;
385	else
386		pwm = tc654_pwm_map[data->duty_cycle];
387
388	return sprintf(buf, "%d\n", pwm);
389}
390
391static ssize_t set_pwm(struct device *dev, struct device_attribute *da,
392		       const char *buf, size_t count)
393{
394	struct tc654_data *data = dev_get_drvdata(dev);
395	struct i2c_client *client = data->client;
396	unsigned long val;
397	int ret;
398
399	if (kstrtoul(buf, 10, &val))
400		return -EINVAL;
401	if (val > 255)
402		return -EINVAL;
403
404	mutex_lock(&data->update_lock);
405
406	if (val == 0)
407		data->config |= TC654_REG_CONFIG_SDM;
408	else
409		data->config &= ~TC654_REG_CONFIG_SDM;
410
411	data->duty_cycle = find_closest(val, tc654_pwm_map,
412					ARRAY_SIZE(tc654_pwm_map));
413
414	ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
415	if (ret < 0)
416		goto out;
417
418	ret = i2c_smbus_write_byte_data(client, TC654_REG_DUTY_CYCLE,
419					data->duty_cycle);
420
421out:
422	mutex_unlock(&data->update_lock);
423	return ret < 0 ? ret : count;
424}
425
426static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
427static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
428static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
429			  set_fan_min, 0);
430static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
431			  set_fan_min, 1);
432static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0);
433static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 1);
434static SENSOR_DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
435			  set_fan_pulses, 0);
436static SENSOR_DEVICE_ATTR(fan2_pulses, S_IWUSR | S_IRUGO, show_fan_pulses,
437			  set_fan_pulses, 1);
438static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO,
439			  show_pwm_mode, set_pwm_mode, 0);
440static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm,
441			  set_pwm, 0);
442
443/* Driver data */
444static 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,
455	NULL
456};
457
458ATTRIBUTE_GROUPS(tc654);
459
460/*
461 * device probe and removal
462 */
463
464static int tc654_probe(struct i2c_client *client,
465		       const struct i2c_device_id *id)
466{
467	struct device *dev = &client->dev;
468	struct tc654_data *data;
469	struct device *hwmon_dev;
470	int ret;
471
472	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
473		return -ENODEV;
474
475	data = devm_kzalloc(dev, sizeof(struct tc654_data), GFP_KERNEL);
476	if (!data)
477		return -ENOMEM;
478
479	data->client = client;
480	mutex_init(&data->update_lock);
481
482	ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
483	if (ret < 0)
484		return ret;
485
486	data->config = ret;
487
488	hwmon_dev =
489	    devm_hwmon_device_register_with_groups(dev, client->name, data,
490						   tc654_groups);
491	return PTR_ERR_OR_ZERO(hwmon_dev);
492}
493
494static const struct i2c_device_id tc654_id[] = {
495	{"tc654", 0},
496	{"tc655", 0},
497	{}
498};
499
500MODULE_DEVICE_TABLE(i2c, tc654_id);
501
502static struct i2c_driver tc654_driver = {
503	.driver = {
504		   .name = "tc654",
505		   },
506	.probe = tc654_probe,
507	.id_table = tc654_id,
508};
509
510module_i2c_driver(tc654_driver);
511
512MODULE_AUTHOR("Allied Telesis Labs");
513MODULE_DESCRIPTION("Microchip TC654/TC655 driver");
514MODULE_LICENSE("GPL");