Linux Audio

Check our new training course

Loading...
v6.8
  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/thermal.h>
 19#include <linux/util_macros.h>
 20
 21enum tc654_regs {
 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 */
 31};
 32
 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))
 36
 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 */
 41
 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 */
 45
 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 */
 49
 50/* Convert to the fan fault RPM threshold from register value */
 51#define TC654_FAN_FAULT_FROM_REG(val)	((val) * 50)	/* 50 RPM resolution */
 52
 53/* Convert to register value from the fan fault RPM threshold */
 54#define TC654_FAN_FAULT_TO_REG(val)	(((val) / 50) & 0xff)
 55
 56/* Register data is read (and cached) at most once per second. */
 57#define TC654_UPDATE_INTERVAL		HZ
 58
 59struct tc654_data {
 60	struct i2c_client *client;
 61
 62	/* update mutex */
 63	struct mutex update_lock;
 64
 65	/* tc654 register cache */
 66	bool valid;
 67	unsigned long last_updated;	/* in jiffies */
 68
 69	u8 rpm_output[2];	/* The fan RPM data for fans 1 and 2 is then
 70				 * written to registers RPM1 and RPM2
 71				 */
 72	u8 fan_fault[2];	/* The Fan Fault Threshold Registers are used to
 73				 * set the fan fault threshold levels for fan 1
 74				 * and fan 2
 75				 */
 76	u8 config;	/* The Configuration Register is an 8-bit read/
 77			 * writable multi-function control register
 78			 *   7: Fan Fault Clear
 79			 *      1 = Clear Fan Fault
 80			 *      0 = Normal Operation (default)
 81			 *   6: Resolution Selection for RPM Output Registers
 82			 *      RPM Output Registers (RPM1 and RPM2) will be
 83			 *      set for
 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
 91			 *      00 = 1
 92			 *      01 = 2 (default)
 93			 *      10 = 4
 94			 *      11 = 8
 95			 * 2,1: Fan 1 Pulses Per Rotation
 96			 *      00 = 1
 97			 *      01 = 2 (default)
 98			 *      10 = 4
 99			 *      11 = 8
100			 *   0: Shutdown Mode
101			 *      1 = Shutdown mode.
102			 *      0 = Normal operation. (default)
103			 */
104	u8 status;	/* The Status register provides all the information
105			 * about what is going on within the TC654/TC655
106			 * devices.
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
118			 *      1 = V IN is open
119			 *      0 = Normal operation. voltage present at V IN
120			 *   1: Fan 2 Fault
121			 *      1 = Fault condition
122			 *      0 = Normal operation
123			 *   0: Fan 1 Fault
124			 *      1 = Fault condition
125			 *      0 = Normal operation
126			 */
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.
130			 */
131};
132
133/* helper to grab and cache data, at most one time per second */
134static struct tc654_data *tc654_update_client(struct device *dev)
135{
136	struct tc654_data *data = dev_get_drvdata(dev);
137	struct i2c_client *client = data->client;
138	int ret = 0;
139
140	mutex_lock(&data->update_lock);
141	if (time_before(jiffies, data->last_updated + TC654_UPDATE_INTERVAL) &&
142	    likely(data->valid))
143		goto out;
144
145	ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(0));
146	if (ret < 0)
147		goto out;
148	data->rpm_output[0] = ret;
149
150	ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(1));
151	if (ret < 0)
152		goto out;
153	data->rpm_output[1] = ret;
154
155	ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(0));
156	if (ret < 0)
157		goto out;
158	data->fan_fault[0] = ret;
159
160	ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(1));
161	if (ret < 0)
162		goto out;
163	data->fan_fault[1] = ret;
164
165	ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
166	if (ret < 0)
167		goto out;
168	data->config = ret;
169
170	ret = i2c_smbus_read_byte_data(client, TC654_REG_STATUS);
171	if (ret < 0)
172		goto out;
173	data->status = ret;
174
175	ret = i2c_smbus_read_byte_data(client, TC654_REG_DUTY_CYCLE);
176	if (ret < 0)
177		goto out;
178	data->duty_cycle = ret & 0x0f;
179
180	data->last_updated = jiffies;
181	data->valid = true;
182out:
183	mutex_unlock(&data->update_lock);
184
185	if (ret < 0)		/* upon error, encode it in return value */
186		data = ERR_PTR(ret);
187
188	return data;
189}
190
191/*
192 * sysfs attributes
193 */
194
195static ssize_t fan_show(struct device *dev, struct device_attribute *da,
196			char *buf)
197{
198	int nr = to_sensor_dev_attr(da)->index;
199	struct tc654_data *data = tc654_update_client(dev);
200	int val;
201
202	if (IS_ERR(data))
203		return PTR_ERR(data);
204
205	if (data->config & TC654_REG_CONFIG_RES)
206		val = data->rpm_output[nr] * TC654_HIGH_RPM_RESOLUTION;
207	else
208		val = data->rpm_output[nr] * TC654_LOW_RPM_RESOLUTION;
209
210	return sprintf(buf, "%d\n", val);
211}
212
213static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
214			    char *buf)
215{
216	int nr = to_sensor_dev_attr(da)->index;
217	struct tc654_data *data = tc654_update_client(dev);
218
219	if (IS_ERR(data))
220		return PTR_ERR(data);
221
222	return sprintf(buf, "%d\n",
223		       TC654_FAN_FAULT_FROM_REG(data->fan_fault[nr]));
224}
225
226static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
227			     const char *buf, size_t count)
228{
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;
232	unsigned long val;
233	int ret;
234
235	if (kstrtoul(buf, 10, &val))
236		return -EINVAL;
237
238	val = clamp_val(val, 0, 12750);
239
240	mutex_lock(&data->update_lock);
241
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]);
245
246	mutex_unlock(&data->update_lock);
247	return ret < 0 ? ret : count;
248}
249
250static ssize_t fan_alarm_show(struct device *dev, struct device_attribute *da,
251			      char *buf)
252{
253	int nr = to_sensor_dev_attr(da)->index;
254	struct tc654_data *data = tc654_update_client(dev);
255	int val;
256
257	if (IS_ERR(data))
258		return PTR_ERR(data);
259
260	if (nr == 0)
261		val = !!(data->status & TC654_REG_STATUS_F1F);
262	else
263		val = !!(data->status & TC654_REG_STATUS_F2F);
264
265	return sprintf(buf, "%d\n", val);
266}
267
268static const u8 TC654_FAN_PULSE_SHIFT[] = { 1, 3 };
269
270static ssize_t fan_pulses_show(struct device *dev,
271			       struct device_attribute *da, char *buf)
272{
273	int nr = to_sensor_dev_attr(da)->index;
274	struct tc654_data *data = tc654_update_client(dev);
275	u8 val;
276
277	if (IS_ERR(data))
278		return PTR_ERR(data);
279
280	val = BIT((data->config >> TC654_FAN_PULSE_SHIFT[nr]) & 0x03);
281	return sprintf(buf, "%d\n", val);
282}
283
284static ssize_t fan_pulses_store(struct device *dev,
285				struct device_attribute *da, const char *buf,
286				size_t count)
287{
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;
291	u8 config;
292	unsigned long val;
293	int ret;
294
295	if (kstrtoul(buf, 10, &val))
296		return -EINVAL;
297
298	switch (val) {
299	case 1:
300		config = 0;
301		break;
302	case 2:
303		config = 1;
304		break;
305	case 4:
306		config = 2;
307		break;
308	case 8:
309		config = 3;
310		break;
311	default:
312		return -EINVAL;
313	}
314
315	mutex_lock(&data->update_lock);
316
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);
320
321	mutex_unlock(&data->update_lock);
322	return ret < 0 ? ret : count;
323}
324
325static ssize_t pwm_mode_show(struct device *dev, struct device_attribute *da,
326			     char *buf)
327{
328	struct tc654_data *data = tc654_update_client(dev);
329
330	if (IS_ERR(data))
331		return PTR_ERR(data);
332
333	return sprintf(buf, "%d\n", !!(data->config & TC654_REG_CONFIG_DUTYC));
334}
335
336static ssize_t pwm_mode_store(struct device *dev, struct device_attribute *da,
337			      const char *buf, size_t count)
338{
339	struct tc654_data *data = dev_get_drvdata(dev);
340	struct i2c_client *client = data->client;
341	unsigned long val;
342	int ret;
343
344	if (kstrtoul(buf, 10, &val))
345		return -EINVAL;
346
347	if (val != 0 && val != 1)
348		return -EINVAL;
349
350	mutex_lock(&data->update_lock);
351
352	if (val)
353		data->config |= TC654_REG_CONFIG_DUTYC;
354	else
355		data->config &= ~TC654_REG_CONFIG_DUTYC;
356
357	ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
358
359	mutex_unlock(&data->update_lock);
360	return ret < 0 ? ret : count;
361}
362
363static const int tc654_pwm_map[16] = { 77,  88, 102, 112, 124, 136, 148, 160,
364				      172, 184, 196, 207, 219, 231, 243, 255};
365
366static ssize_t pwm_show(struct device *dev, struct device_attribute *da,
367			char *buf)
368{
369	struct tc654_data *data = tc654_update_client(dev);
370	int pwm;
371
372	if (IS_ERR(data))
373		return PTR_ERR(data);
374
375	if (data->config & TC654_REG_CONFIG_SDM)
376		pwm = 0;
377	else
378		pwm = tc654_pwm_map[data->duty_cycle];
379
380	return sprintf(buf, "%d\n", pwm);
381}
382
383static int _set_pwm(struct tc654_data *data, unsigned long val)
 
384{
 
385	struct i2c_client *client = data->client;
 
386	int ret;
387
 
 
 
 
 
388	mutex_lock(&data->update_lock);
389
390	if (val == 0) {
391		data->config |= TC654_REG_CONFIG_SDM;
392		data->duty_cycle = 0;
393	} else {
394		data->config &= ~TC654_REG_CONFIG_SDM;
395		data->duty_cycle = val - 1;
396	}
 
397
398	ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
399	if (ret < 0)
400		goto out;
401
402	ret = i2c_smbus_write_byte_data(client, TC654_REG_DUTY_CYCLE,
403					data->duty_cycle);
404
405out:
406	mutex_unlock(&data->update_lock);
407	return ret;
408}
409
410static ssize_t pwm_store(struct device *dev, struct device_attribute *da,
411			 const char *buf, size_t count)
412{
413	struct tc654_data *data = dev_get_drvdata(dev);
414	unsigned long val;
415	int ret;
416
417	if (kstrtoul(buf, 10, &val))
418		return -EINVAL;
419	if (val > 255)
420		return -EINVAL;
421	if (val > 0)
422		val = find_closest(val, tc654_pwm_map, ARRAY_SIZE(tc654_pwm_map)) + 1;
423
424	ret = _set_pwm(data, val);
425	return ret < 0 ? ret : count;
426}
427
428static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
429static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
430static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
431static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
432static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0);
433static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1);
434static SENSOR_DEVICE_ATTR_RW(fan1_pulses, fan_pulses, 0);
435static SENSOR_DEVICE_ATTR_RW(fan2_pulses, fan_pulses, 1);
436static SENSOR_DEVICE_ATTR_RW(pwm1_mode, pwm_mode, 0);
437static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
438
439/* Driver data */
440static 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,
451	NULL
452};
453
454ATTRIBUTE_GROUPS(tc654);
455
456/*
457 * thermal cooling device functions
458 *
459 * Account for the "ShutDown Mode (SDM)" state by offsetting
460 * the 16 PWM duty cycle states by 1.
461 *
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
465 * [...]
466 * State 15 = ~95% PWM | duty_cycle = 14
467 * State 16 = 100% PWM | duty_cycle = 15
468 */
469#define TC654_MAX_COOLING_STATE	16
470
471static int tc654_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
472{
473	*state = TC654_MAX_COOLING_STATE;
474	return 0;
475}
476
477static int tc654_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
478{
479	struct tc654_data *data = tc654_update_client(cdev->devdata);
480
481	if (IS_ERR(data))
482		return PTR_ERR(data);
483
484	if (data->config & TC654_REG_CONFIG_SDM)
485		*state = 0;	/* FAN is off */
486	else
487		*state = data->duty_cycle + 1;	/* offset PWM States by 1 */
488
489	return 0;
490}
491
492static int tc654_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
493{
494	struct tc654_data *data = tc654_update_client(cdev->devdata);
495
496	if (IS_ERR(data))
497		return PTR_ERR(data);
498
499	return _set_pwm(data, clamp_val(state, 0, TC654_MAX_COOLING_STATE));
500}
501
502static 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,
506};
507
508/*
509 * device probe and removal
510 */
511
512static int tc654_probe(struct i2c_client *client)
 
513{
514	struct device *dev = &client->dev;
515	struct tc654_data *data;
516	struct device *hwmon_dev;
517	int ret;
518
519	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
520		return -ENODEV;
521
522	data = devm_kzalloc(dev, sizeof(struct tc654_data), GFP_KERNEL);
523	if (!data)
524		return -ENOMEM;
525
526	data->client = client;
527	mutex_init(&data->update_lock);
528
529	ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
530	if (ret < 0)
531		return ret;
532
533	data->config = ret;
534
535	hwmon_dev =
536	    devm_hwmon_device_register_with_groups(dev, client->name, data,
537						   tc654_groups);
538	if (IS_ERR(hwmon_dev))
539		return PTR_ERR(hwmon_dev);
540
541	if (IS_ENABLED(CONFIG_THERMAL)) {
542		struct thermal_cooling_device *cdev;
543
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);
547	}
548
549	return 0;
550}
551
552static const struct i2c_device_id tc654_id[] = {
553	{"tc654", 0},
554	{"tc655", 0},
555	{}
556};
557
558MODULE_DEVICE_TABLE(i2c, tc654_id);
559
560static struct i2c_driver tc654_driver = {
561	.driver = {
562		   .name = "tc654",
563		   },
564	.probe = tc654_probe,
565	.id_table = tc654_id,
566};
567
568module_i2c_driver(tc654_driver);
569
570MODULE_AUTHOR("Allied Telesis Labs");
571MODULE_DESCRIPTION("Microchip TC654/TC655 driver");
572MODULE_LICENSE("GPL");
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");