53b9bdbc179690231b5ea74eeefa198aa924fbc8
[openwrt/openwrt.git] / target / linux / apm821xx / patches-4.4 / 901-hwmon-add-driver-for-Microchip-TC654-TC655-PWM-fan-c.patch
1 From 5ea2e152d846bf60901107fefd81a58f792f3bc2 Mon Sep 17 00:00:00 2001
2 From: Christian Lamparter <chunkeey@gmail.com>
3 Date: Fri, 10 Jun 2016 03:00:46 +0200
4 Subject: [PATCH] hwmon: add driver for Microchip TC654/TC655 PWM fan
5 controllers
6
7 This patch adds a hwmon driver for the Microchip TC654 and TC655
8 Dual SMBus PWM Fan Speed Controllers with Fan Fault detection.
9
10 The chip is described in the DS2001734C Spec Document from Microchip.
11 It supports:
12 - Shared PWM Fan Drive for two fans
13 - Provides RPM
14 - automatic PWM controller (needs additional
15 NTC/PTC Thermistors.)
16 - Overtemperature alarm (when using NTC/PTC
17 Thermistors)
18
19 Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
20 ---
21 drivers/hwmon/Kconfig | 10 +
22 drivers/hwmon/Makefile | 1 +
23 drivers/hwmon/tc654.c | 969 +++++++++++++++++++++++++++++++++++++++++++++++++
24 3 files changed, 980 insertions(+)
25 create mode 100644 drivers/hwmon/tc654.c
26
27 diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
28 index ff94007..941fe4951 100644
29 --- a/drivers/hwmon/Kconfig
30 +++ b/drivers/hwmon/Kconfig
31 @@ -1514,6 +1514,16 @@ config SENSORS_INA2XX
32 This driver can also be built as a module. If so, the module
33 will be called ina2xx.
34
35 +config SENSORS_TC654
36 + tristate "Microchip TC654 and TC655"
37 + depends on I2C
38 + help
39 + If you say yes here you get support for Microchip TC655 and TC654
40 + Dual PWM Fan Speed Controllers and sensor chips.
41 +
42 + This driver can also be built as a module. If so, the module
43 + will be called tc654.
44 +
45 config SENSORS_TC74
46 tristate "Microchip TC74"
47 depends on I2C
48 diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
49 index 2ef5b7c..04270c7 100644
50 --- a/drivers/hwmon/Makefile
51 +++ b/drivers/hwmon/Makefile
52 @@ -145,6 +145,7 @@ obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.o
53 obj-$(CONFIG_SENSORS_SMSC47M1) += smsc47m1.o
54 obj-$(CONFIG_SENSORS_SMSC47M192)+= smsc47m192.o
55 obj-$(CONFIG_SENSORS_AMC6821) += amc6821.o
56 +obj-$(CONFIG_SENSORS_TC654) += tc654.o
57 obj-$(CONFIG_SENSORS_TC74) += tc74.o
58 obj-$(CONFIG_SENSORS_THMC50) += thmc50.o
59 obj-$(CONFIG_SENSORS_TMP102) += tmp102.o
60 diff --git a/drivers/hwmon/tc654.c b/drivers/hwmon/tc654.c
61 new file mode 100644
62 index 0000000..d584baf
63 --- /dev/null
64 +++ b/drivers/hwmon/tc654.c
65 @@ -0,0 +1,969 @@
66 +/*
67 + * tc654.c - Support for Microchip TC654/TC655
68 + * "A Dual SMBus PWM FAN Speed Controllers with Fan Fault Detection"
69 + *
70 + * Copyright (c) 2016 Christian Lamparter <chunkeey@gmail.com>
71 + *
72 + * This program is free software; you can redistribute it and/or modify
73 + * it under the terms of the GNU General Public License as published by
74 + * the Free Software Foundation version 2 of the License.
75 + *
76 + * This program is distributed in the hope that it will be useful,
77 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
78 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
79 + * GNU General Public License for more details.
80 + *
81 + * The chip is described in the DS2001734C Spec Document from Microchip.
82 + */
83 +
84 +#include <linux/module.h>
85 +#include <linux/init.h>
86 +#include <linux/slab.h>
87 +#include <linux/jiffies.h>
88 +#include <linux/i2c.h>
89 +#include <linux/hwmon.h>
90 +#include <linux/hwmon-sysfs.h>
91 +#include <linux/err.h>
92 +#include <linux/mutex.h>
93 +#include <linux/thermal.h>
94 +
95 +/* Hardware definitions */
96 +/* 5.1.4 Address Byte stats that TC654/TC655 are fixed at 0x1b */
97 +static const unsigned short normal_i2c[] = { 0x1b, I2C_CLIENT_END };
98 +
99 +enum TC654_REGS {
100 + TC654_REG_RPM1 = 0x00,
101 + TC654_REG_RPM2,
102 + TC654_REG_FAN1_FAULT_THRESH,
103 + TC654_REG_FAN2_FAULT_THRESH,
104 + TC654_REG_CONFIG,
105 + TC654_REG_STATUS,
106 + TC654_REG_DUTY_CYCLE,
107 + TC654_REG_MFR_ID,
108 + TC654_REG_VER_ID,
109 +
110 + /* keep last */
111 + __TC654_REG_NUM,
112 +};
113 +
114 +#define TC654_MFR_ID_MICROCHIP 0x84
115 +#define TC654_VER_ID 0x00
116 +#define TC655_VER_ID 0x01
117 +
118 +enum TC654_CONTROL_BITS {
119 + TC654_CTRL_SDM = BIT(0),
120 + TC654_CTRL_F1PPR_S = 1,
121 + TC654_CTRL_F1PPR_M = (BIT(1) | BIT(2)),
122 + TC654_CTRL_F2PPR_S = 3,
123 + TC654_CTRL_F2PPR_M = (BIT(3) | BIT(4)),
124 + TC654_CTRL_DUTYC = BIT(5),
125 + TC654_CTRL_RES = BIT(6),
126 + TC654_CTRL_FFCLR = BIT(7),
127 +};
128 +
129 +enum TC654_STATUS_BITS {
130 + TC654_STATUS_F1F = BIT(0),
131 + TC654_STATUS_F2F = BIT(1),
132 + TC654_STATUS_VSTAT = BIT(2),
133 + TC654_STATUS_R1CO = BIT(3),
134 + TC654_STATUS_R2CO = BIT(4),
135 + TC654_STATUS_OTF = BIT(5),
136 +};
137 +
138 +enum TC654_FAN {
139 + TC654_FAN1 = 0,
140 + TC654_FAN2,
141 +
142 + /* keep last */
143 + __NUM_TC654_FAN,
144 +};
145 +
146 +enum TC654_FAN_MODE {
147 + TC654_PWM_OFF, /* Shutdown Mode - switch of both fans */
148 + TC654_PWM_VIN, /* Fans will be controlled via V_in analog input pin */
149 + TC654_PWM_3000, /* sets fans to 30% duty cycle */
150 + TC654_PWM_3467,
151 + TC654_PWM_3933, /* default case - if V_in pin is open */
152 + TC654_PWM_4400,
153 + TC654_PWM_4867,
154 + TC654_PWM_5333,
155 + TC654_PWM_5800,
156 + TC654_PWM_6267,
157 + TC654_PWM_6733,
158 + TC654_PWM_7200,
159 + TC654_PWM_7667,
160 + TC654_PWM_8133,
161 + TC654_PWM_8600,
162 + TC654_PWM_9067,
163 + TC654_PWM_9533,
164 + TC654_PWM_10000, /* sets fans to 100% duty cycle */
165 +};
166 +
167 +enum TC654_ALARMS {
168 + TC654_ALARM_FAN1_FAULT,
169 + TC654_ALARM_FAN2_FAULT,
170 + TC654_ALARM_FAN1_COUNTER_OVERFLOW,
171 + TC654_ALARM_FAN2_COUNTER_OVERFLOW,
172 + TC654_ALARM_OVER_TEMPERATURE,
173 +
174 + /* KEEP LAST */
175 + __NUM_TC654_ALARMS,
176 +};
177 +
178 +static const struct pwm_table_entry {
179 + u8 min;
180 + enum TC654_FAN_MODE mode;
181 +} pwm_table[] = {
182 + { 0, TC654_PWM_OFF },
183 + { 1, TC654_PWM_3000 },
184 + { 88, TC654_PWM_3467 },
185 + {101, TC654_PWM_3933 },
186 + {113, TC654_PWM_4400 },
187 + {125, TC654_PWM_4867 },
188 + {137, TC654_PWM_5333 },
189 + {148, TC654_PWM_5800 },
190 + {160, TC654_PWM_6267 },
191 + {172, TC654_PWM_6733 },
192 + {184, TC654_PWM_7200 },
193 + {196, TC654_PWM_7667 },
194 + {208, TC654_PWM_8133 },
195 + {220, TC654_PWM_8600 },
196 + {232, TC654_PWM_9067 },
197 + {244, TC654_PWM_9533 },
198 + {255, TC654_PWM_10000 },
199 +};
200 +
201 +/* driver context */
202 +struct tc654 {
203 + struct i2c_client *client;
204 +
205 + struct mutex update_lock;
206 +
207 + unsigned long last_updated; /* in jiffies */
208 + u8 cached_regs[__TC654_REG_NUM];
209 +
210 + bool valid; /* monitored registers are valid */
211 + u16 fan_input[__NUM_TC654_FAN];
212 + bool alarms[__NUM_TC654_ALARMS];
213 + bool vin_status;
214 + bool pwm_manual;
215 +
216 + /* optional cooling device */
217 + struct thermal_cooling_device *cdev;
218 +};
219 +
220 +/* hardware accessors and functions */
221 +static int read_tc(struct tc654 *tc, u8 reg)
222 +{
223 + s32 status;
224 +
225 + if (reg <= TC654_REG_VER_ID) {
226 + /* Table 6.1 states that all registers are readable */
227 + status = i2c_smbus_read_byte_data(tc->client, reg);
228 + } else
229 + status = -EINVAL;
230 +
231 + if (status < 0) {
232 + dev_warn(&tc->client->dev, "can't read register 0x%02x due to error (%d)",
233 + reg, status);
234 + } else {
235 + tc->cached_regs[reg] = status;
236 + }
237 +
238 + return status;
239 +}
240 +
241 +static int write_tc(struct tc654 *tc, u8 i2c_reg, u8 val)
242 +{
243 + s32 status;
244 +
245 + /*
246 + * Table 6.1 states that both fan threshold registers,
247 + * the Config and Duty Cycle are writeable.
248 + */
249 + switch (i2c_reg) {
250 + case TC654_REG_FAN1_FAULT_THRESH:
251 + case TC654_REG_FAN2_FAULT_THRESH:
252 + case TC654_REG_DUTY_CYCLE:
253 + case TC654_REG_CONFIG:
254 + status = i2c_smbus_write_byte_data(tc->client, i2c_reg, val);
255 + break;
256 +
257 + default:
258 + return -EINVAL;
259 + }
260 +
261 + if (status < 0) {
262 + dev_warn(&tc->client->dev, "can't write register 0x%02x with value 0x%02x due to error (%d)",
263 + i2c_reg, val, status);
264 + } else {
265 + tc->cached_regs[i2c_reg] = val;
266 + }
267 +
268 + return status;
269 +}
270 +
271 +static int mod_config(struct tc654 *tc, u8 set, u8 clear)
272 +{
273 + u8 val = 0;
274 +
275 + /* a bit can't be set and cleared on the same time. */
276 + if (set & clear)
277 + return -EINVAL;
278 +
279 + /* invalidate data to force re-read from hardware */
280 + tc->valid = false;
281 + val = (tc->cached_regs[TC654_REG_CONFIG] | set) & (~clear);
282 + return write_tc(tc, TC654_REG_CONFIG, val);
283 +}
284 +
285 +static int read_fan_rpm(struct tc654 *tc, enum TC654_FAN fan)
286 +{
287 + int ret;
288 +
289 + /* 6.1 RPM-OUTPUT1 and RPM-OUTPUT2 registers */
290 + ret = read_tc(tc, fan == TC654_FAN1 ? TC654_REG_RPM1 : TC654_REG_RPM2);
291 + if (ret < 0)
292 + return ret;
293 +
294 + /*
295 + * The Resolution Selection Bit in 6.3 CONFIGURATION REGISTER
296 + * is needed to convert the raw value to the RPM.
297 + * 0 = RPM1 and RPM2 use (8-Bit) resolution => * 50 RPM
298 + * 1 = RPM1 and RPM2 use (9-Bit) resolution => * 25 RPM
299 + */
300 + return ret * (25 <<
301 + !(tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_RES));
302 +}
303 +
304 +static int write_fan_fault_thresh(struct tc654 *tc, enum TC654_FAN fan,
305 + u16 rpm)
306 +{
307 + u8 converted_rpm;
308 +
309 + if (rpm > 12750)
310 + return -EINVAL;
311 +
312 + /*
313 + * 6.2 FAN_FAULT1 and FAN_FAULT2 Threshold registers
314 + *
315 + * Both registers operate in 50 RPM mode exclusively.
316 + */
317 + converted_rpm = rpm / 50;
318 +
319 + /* invalidate data to force re-read from hardware */
320 + tc->valid = false;
321 + return write_tc(tc, fan == TC654_FAN1 ? TC654_REG_FAN1_FAULT_THRESH :
322 + TC654_REG_FAN2_FAULT_THRESH, converted_rpm);
323 +}
324 +
325 +
326 +static int read_fan_fault_thresh(struct tc654 *tc, enum TC654_FAN fan)
327 +{
328 + /*
329 + * 6.2 FAN_FAULT1 and FAN_FAULT2 Threshold registers
330 + *
331 + * Both registers operate in 50 RPM mode exclusively.
332 + */
333 + return read_tc(tc, fan == TC654_FAN1 ? TC654_REG_FAN1_FAULT_THRESH :
334 + TC654_REG_FAN2_FAULT_THRESH) * 50;
335 +}
336 +
337 +static enum TC654_FAN_MODE get_fan_mode(struct tc654 *tc)
338 +{
339 + if (tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_SDM) {
340 + return TC654_PWM_OFF;
341 + } else if (tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_DUTYC) {
342 + return TC654_PWM_3000 + tc->cached_regs[TC654_REG_DUTY_CYCLE];
343 + } else if (tc->vin_status == 0)
344 + return TC654_PWM_VIN;
345 +
346 + return -EINVAL;
347 +}
348 +
349 +static int write_fan_mode(struct tc654 *tc, enum TC654_FAN_MODE mode)
350 +{
351 + int err;
352 + u8 pwm_mode;
353 + bool in_sdm;
354 +
355 + in_sdm = !!(tc->cached_regs[TC654_REG_CONFIG] &
356 + TC654_CTRL_SDM);
357 +
358 + switch (mode) {
359 + case TC654_PWM_OFF:
360 + if (in_sdm)
361 + return 0;
362 +
363 + /* Enter Shutdown Mode - Switches off all fans */
364 + err = mod_config(tc, TC654_CTRL_SDM, TC654_CTRL_DUTYC);
365 + if (err)
366 + return err;
367 +
368 + return 0;
369 +
370 + case TC654_PWM_VIN:
371 + if (tc->vin_status) {
372 + dev_err(&tc->client->dev,
373 + "V_in pin is open, can't enable automatic mode.");
374 + return -EINVAL;
375 + }
376 +
377 + err = mod_config(tc, 0, TC654_CTRL_SDM | TC654_CTRL_DUTYC);
378 + if (err)
379 + return err;
380 +
381 + tc->pwm_manual = false;
382 + return 0;
383 +
384 + case TC654_PWM_3000:
385 + case TC654_PWM_3467:
386 + case TC654_PWM_3933:
387 + case TC654_PWM_4400:
388 + case TC654_PWM_4867:
389 + case TC654_PWM_5333:
390 + case TC654_PWM_5800:
391 + case TC654_PWM_6267:
392 + case TC654_PWM_6733:
393 + case TC654_PWM_7200:
394 + case TC654_PWM_7667:
395 + case TC654_PWM_8133:
396 + case TC654_PWM_8600:
397 + case TC654_PWM_9067:
398 + case TC654_PWM_9533:
399 + case TC654_PWM_10000:
400 + pwm_mode = mode - TC654_PWM_3000;
401 + if (!in_sdm) {
402 + err = write_tc(tc, TC654_REG_DUTY_CYCLE, pwm_mode);
403 + if (err)
404 + return err;
405 + }
406 +
407 + err = mod_config(tc, TC654_CTRL_DUTYC, TC654_CTRL_SDM);
408 + if (err)
409 + return err;
410 +
411 + tc->pwm_manual = true;
412 +
413 + if (in_sdm) {
414 + /*
415 + * In case the TC654/TC655 was in SDM mode, the write
416 + * above into the TC654_REG_DUTY_CYCLE register will
417 + * have no effect because the chip was switched off.
418 + *
419 + * Note: The TC654/TC655 have a special "power-on"
420 + * feature where the PWM will be forced to 100% for
421 + * one full second in order to spin-up a resting fan.
422 + */
423 + err = write_tc(tc, TC654_REG_DUTY_CYCLE, pwm_mode);
424 + if (err)
425 + return err;
426 + }
427 +
428 + return 0;
429 +
430 + default:
431 + return -EINVAL;
432 + }
433 +}
434 +
435 +static struct tc654 *tc654_update_device(struct device *dev)
436 +{
437 + struct tc654 *tc = dev_get_drvdata(dev);
438 +
439 + mutex_lock(&tc->update_lock);
440 +
441 + /*
442 + * In Chapter "1.0 Electrical Characteristics",
443 + * the "Fault Output Response Time" is specified as 2.4 seconds.
444 + */
445 + if (time_after(jiffies, tc->last_updated + 2 * HZ + (HZ * 2) / 5)
446 + || !tc->valid) {
447 + size_t i;
448 + int ret;
449 + bool alarm_triggered;
450 +
451 + tc->valid = false;
452 +
453 + for (i = 0; i < __NUM_TC654_FAN; i++) {
454 + ret = read_fan_rpm(tc, i);
455 + if (ret < 0)
456 + goto out;
457 +
458 + tc->fan_input[i] = ret;
459 + }
460 +
461 + ret = read_tc(tc, TC654_REG_STATUS);
462 + if (ret < 0)
463 + goto out;
464 +
465 + alarm_triggered = !!(ret & (TC654_STATUS_F1F |
466 + TC654_STATUS_F2F | TC654_STATUS_R1CO |
467 + TC654_STATUS_R2CO | TC654_STATUS_OTF));
468 +
469 + tc->alarms[TC654_ALARM_FAN1_FAULT] = !!(ret & TC654_STATUS_F1F);
470 + tc->alarms[TC654_ALARM_FAN2_FAULT] = !!(ret & TC654_STATUS_F2F);
471 + tc->alarms[TC654_ALARM_FAN1_COUNTER_OVERFLOW] =
472 + !!(ret & TC654_STATUS_R1CO);
473 + tc->alarms[TC654_ALARM_FAN2_COUNTER_OVERFLOW] =
474 + !!(ret & TC654_STATUS_R2CO);
475 + tc->alarms[TC654_ALARM_OVER_TEMPERATURE] =
476 + !!(ret & TC654_STATUS_OTF);
477 + tc->vin_status = !!(ret & TC654_STATUS_VSTAT);
478 +
479 + /*
480 + * From 4.5 and 6.3
481 + *
482 + * ... "If the V_in pin is open when TC654_CTRL_DUTYC is not
483 + * selected, then V_out duty cycle will default to 39.33%.".
484 + *
485 + * and most importantly 6.5:
486 + * ... "V_in pin is open, the duty cycle will go to the default
487 + * setting of this register, which is 0010 (39.33%)."
488 + */
489 + tc->pwm_manual |= tc->vin_status &&
490 + (tc->cached_regs[TC654_REG_CONFIG] &
491 + TC654_CTRL_DUTYC);
492 +
493 + if (alarm_triggered) {
494 + /*
495 + * as the name implies, this FLAG needs to be
496 + * set in order to clear the FAN Fault error.
497 + */
498 + ret = mod_config(tc, TC654_CTRL_FFCLR, 0);
499 + if (ret < 0)
500 + goto out;
501 + }
502 +
503 + tc->last_updated = jiffies;
504 + tc->valid = true;
505 + }
506 +
507 +out:
508 + mutex_unlock(&tc->update_lock);
509 + return tc;
510 +}
511 +
512 +static u8 get_fan_pulse(struct tc654 *tc, enum TC654_FAN fan)
513 +{
514 + u8 fan_pulse_mask = fan == TC654_FAN1 ?
515 + TC654_CTRL_F1PPR_M : TC654_CTRL_F2PPR_M;
516 + u8 fan_pulse_shift = fan == TC654_FAN1 ?
517 + TC654_CTRL_F1PPR_S : TC654_CTRL_F2PPR_S;
518 +
519 + return 1 << ((tc->cached_regs[TC654_REG_CONFIG] & fan_pulse_mask) >>
520 + fan_pulse_shift);
521 +}
522 +
523 +static int
524 +set_fan_pulse(struct tc654 *tc, enum TC654_FAN fan, int pulses)
525 +{
526 + int old_pulses;
527 + int err;
528 + u8 new_pulse_per_rotation;
529 + u8 fan_pulse_mask = fan == TC654_FAN1 ?
530 + TC654_CTRL_F1PPR_M : TC654_CTRL_F2PPR_M;
531 + u8 fan_pulse_shift = fan == TC654_FAN1 ?
532 + TC654_CTRL_F1PPR_S : TC654_CTRL_F2PPR_S;
533 +
534 + switch (pulses) {
535 + case 1:
536 + new_pulse_per_rotation = 0;
537 + break;
538 + case 2:
539 + new_pulse_per_rotation = 1;
540 + break;
541 + case 4:
542 + new_pulse_per_rotation = 2;
543 + break;
544 + case 8:
545 + new_pulse_per_rotation = 3;
546 + break;
547 + default:
548 + return -EINVAL;
549 + }
550 +
551 + new_pulse_per_rotation <<= fan_pulse_shift;
552 + new_pulse_per_rotation &= fan_pulse_mask;
553 +
554 + old_pulses = tc->cached_regs[TC654_REG_CONFIG];
555 + old_pulses &= fan_pulse_mask;
556 +
557 + if (new_pulse_per_rotation == old_pulses)
558 + return 0;
559 +
560 + mutex_lock(&tc->update_lock);
561 + err = mod_config(tc, new_pulse_per_rotation,
562 + old_pulses & (~new_pulse_per_rotation));
563 + mutex_unlock(&tc->update_lock);
564 +
565 + /* invalidate RPM data to force re-read from hardware */
566 + tc->valid = false;
567 +
568 + return err;
569 +}
570 +
571 +static int get_fan_speed(struct tc654 *tc)
572 +{
573 + enum TC654_FAN_MODE mode;
574 + size_t i;
575 +
576 + mode = get_fan_mode(tc);
577 + for (i = 0; i < ARRAY_SIZE(pwm_table); i++) {
578 + if (mode == pwm_table[i].mode)
579 + return pwm_table[i].min;
580 + }
581 +
582 + return -EINVAL;
583 +}
584 +
585 +static int set_fan_speed(struct tc654 *tc, int new_value)
586 +{
587 + int result;
588 + size_t i;
589 +
590 + if (new_value > pwm_table[ARRAY_SIZE(pwm_table) - 1].min ||
591 + new_value < pwm_table[0].min)
592 + return -EINVAL;
593 +
594 + for (i = 0; i < ARRAY_SIZE(pwm_table); i++) {
595 + /* exact match */
596 + if (pwm_table[i].min == new_value)
597 + break;
598 +
599 + /* a little bit too big - go with the previous entry */
600 + if (pwm_table[i].min > new_value) {
601 + --i;
602 + break;
603 + }
604 + }
605 +
606 + mutex_lock(&tc->update_lock);
607 + result = write_fan_mode(tc, pwm_table[i].mode);
608 + mutex_unlock(&tc->update_lock);
609 + if (result < 0)
610 + return result;
611 +
612 + return 0;
613 +}
614 +
615 +/* sysfs */
616 +
617 +static ssize_t
618 +show_fan_input(struct device *dev, struct device_attribute *da, char *buf)
619 +{
620 + struct tc654 *tc = tc654_update_device(dev);
621 + int nr = to_sensor_dev_attr(da)->index;
622 +
623 + return sprintf(buf, "%d\n", tc->fan_input[nr]);
624 +}
625 +
626 +static ssize_t
627 +show_fan_min(struct device *dev, struct device_attribute *da, char *buf)
628 +{
629 + struct tc654 *tc = dev_get_drvdata(dev);
630 + int nr = to_sensor_dev_attr(da)->index;
631 +
632 + return sprintf(buf, "%d\n", read_fan_fault_thresh(tc, nr));
633 +}
634 +
635 +static ssize_t
636 +show_fan_min_alarm(struct device *dev, struct device_attribute *da, char *buf)
637 +{
638 + struct tc654 *tc = tc654_update_device(dev);
639 + int nr = to_sensor_dev_attr(da)->index;
640 +
641 + return sprintf(buf, "%d\n", nr == TC654_FAN1 ?
642 + tc->alarms[TC654_ALARM_FAN1_FAULT] :
643 + tc->alarms[TC654_ALARM_FAN2_FAULT]);
644 +}
645 +
646 +static ssize_t
647 +show_fan_max_alarm(struct device *dev, struct device_attribute *da, char *buf)
648 +{
649 + struct tc654 *tc = tc654_update_device(dev);
650 + int nr = to_sensor_dev_attr(da)->index;
651 +
652 + return sprintf(buf, "%d\n", nr == TC654_FAN1 ?
653 + tc->alarms[TC654_ALARM_FAN1_COUNTER_OVERFLOW] :
654 + tc->alarms[TC654_ALARM_FAN2_COUNTER_OVERFLOW]);
655 +}
656 +
657 +static ssize_t
658 +set_fan_min(struct device *dev, struct device_attribute *da,
659 + const char *buf, size_t count)
660 +{
661 + struct tc654 *tc = dev_get_drvdata(dev);
662 + long new_min;
663 + int nr = to_sensor_dev_attr(da)->index;
664 + int old_min = read_fan_fault_thresh(tc, nr);
665 + int status = kstrtol(buf, 10, &new_min);
666 +
667 + if (status < 0)
668 + return status;
669 +
670 + new_min = (new_min / 50) * 50;
671 + if (new_min == old_min) /* No change */
672 + return count;
673 +
674 + if (new_min < 0 || new_min > 12750)
675 + return -EINVAL;
676 +
677 + mutex_lock(&tc->update_lock);
678 + status = write_fan_fault_thresh(tc, nr, new_min);
679 + mutex_unlock(&tc->update_lock);
680 + return count;
681 +}
682 +
683 +static ssize_t
684 +show_fan_max(struct device *dev, struct device_attribute *da, char *buf)
685 +{
686 + struct tc654 *tc = dev_get_drvdata(dev);
687 + int max_rpm = tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_RES ?
688 + (((1 << 9) - 1) * 25) /* ((2**9) - 1) * 25 RPM */:
689 + (((1 << 8) - 1) * 50) /* ((2**8) - 1) * 50 RPM */;
690 +
691 + return sprintf(buf, "%d\n", max_rpm);
692 +}
693 +
694 +static ssize_t
695 +show_fan_fault(struct device *dev, struct device_attribute *da, char *buf)
696 +{
697 + struct tc654 *tc = tc654_update_device(dev);
698 + int nr = to_sensor_dev_attr(da)->index;
699 + u8 fan_fault_mask = nr == TC654_FAN1 ?
700 + TC654_STATUS_F1F : TC654_STATUS_F2F;
701 +
702 + return sprintf(buf, "%d\n",
703 + !!(tc->cached_regs[TC654_REG_STATUS] & fan_fault_mask));
704 +}
705 +
706 +static ssize_t
707 +show_fan_pulses(struct device *dev, struct device_attribute *da, char *buf)
708 +{
709 + struct tc654 *tc = dev_get_drvdata(dev);
710 + int nr = to_sensor_dev_attr(da)->index;
711 +
712 + return sprintf(buf, "%d\n", get_fan_pulse(tc, nr));
713 +}
714 +
715 +static ssize_t
716 +set_fan_pulses(struct device *dev, struct device_attribute *da,
717 + const char *buf, size_t count)
718 +{
719 + struct tc654 *tc = dev_get_drvdata(dev);
720 + long new_pulse;
721 + int nr = to_sensor_dev_attr(da)->index;
722 + int status = kstrtol(buf, 10, &new_pulse);
723 +
724 + if (status < 0)
725 + return status;
726 +
727 + status = set_fan_pulse(tc, nr, new_pulse);
728 + if (status < 0)
729 + return status;
730 +
731 + return count;
732 +}
733 +
734 +static ssize_t
735 +show_pwm_enable(struct device *dev, struct device_attribute *da, char *buf)
736 +{
737 + struct tc654 *tc = tc654_update_device(dev);
738 + int pwm_enabled;
739 +
740 + if ((tc->cached_regs[TC654_REG_CONFIG] & TC654_CTRL_SDM) &&
741 + !tc->pwm_manual) {
742 + pwm_enabled = 0; /* full off */
743 + } else {
744 + if (tc->valid && tc->vin_status == 0)
745 + pwm_enabled = 2; /* automatic fan speed control */
746 +
747 + pwm_enabled = 1; /* PWM Mode */
748 + }
749 +
750 + return sprintf(buf, "%d\n", pwm_enabled);
751 +}
752 +
753 +static ssize_t
754 +set_pwm_enable(struct device *dev, struct device_attribute *da,
755 + const char *buf, size_t count)
756 +{
757 + struct tc654 *tc = dev_get_drvdata(dev);
758 + long new_value;
759 +
760 + int result = kstrtol(buf, 10, &new_value);
761 +
762 + if (result < 0)
763 + return result;
764 +
765 + mutex_lock(&tc->update_lock);
766 + switch (new_value) {
767 + case 0: /* no fan control (i.e. is OFF) */
768 + result = write_fan_mode(tc, TC654_PWM_OFF);
769 + tc->pwm_manual = false;
770 + break;
771 +
772 + case 1: /* manual fan control enabled (using pwm) */
773 + result = write_fan_mode(tc, TC654_PWM_10000);
774 + break;
775 +
776 + case 2: /* automatic fan speed control enabled */
777 + result = write_fan_mode(tc, TC654_PWM_VIN);
778 + break;
779 +
780 + default:
781 + result = -EINVAL;
782 + }
783 +
784 + mutex_unlock(&tc->update_lock);
785 + return result < 0 ? result : count;
786 +}
787 +
788 +static ssize_t
789 +show_pwm(struct device *dev, struct device_attribute *da, char *buf)
790 +{
791 + struct tc654 *tc = tc654_update_device(dev);
792 + int ret;
793 +
794 + ret = get_fan_speed(tc);
795 + if (ret < 0)
796 + return ret;
797 +
798 + return sprintf(buf, "%d\n", ret);
799 +}
800 +
801 +static ssize_t
802 +set_pwm(struct device *dev, struct device_attribute *da,
803 + const char *buf, size_t count)
804 +{
805 + struct tc654 *tc = dev_get_drvdata(dev);
806 + long new_value = -1;
807 + int result = kstrtol(buf, 10, &new_value);
808 +
809 + if (result < 0)
810 + return result;
811 +
812 + if (new_value < 0 || new_value > INT_MAX)
813 + return -EINVAL;
814 +
815 + if (!tc->pwm_manual)
816 + return -EINVAL;
817 +
818 + result = set_fan_speed(tc, new_value);
819 + if (result < 0)
820 + return result;
821 +
822 + return count;
823 +}
824 +
825 +static ssize_t
826 +show_temp_alarm_otf(struct device *dev, struct device_attribute *da, char *buf)
827 +{
828 + struct tc654 *tc = tc654_update_device(dev);
829 +
830 + return sprintf(buf, "%d\n", tc->alarms[TC654_ALARM_OVER_TEMPERATURE]);
831 +}
832 +
833 +static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input,
834 + NULL, TC654_FAN1);
835 +static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR, show_fan_min,
836 + set_fan_min, TC654_FAN1);
837 +static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_fan_min_alarm,
838 + NULL, TC654_FAN1);
839 +static SENSOR_DEVICE_ATTR(fan1_max_alarm, S_IRUGO, show_fan_max_alarm,
840 + NULL, TC654_FAN1);
841 +static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, TC654_FAN1);
842 +static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault,
843 + NULL, TC654_FAN1);
844 +static SENSOR_DEVICE_ATTR(fan1_pulses, S_IRUGO | S_IWUSR, show_fan_pulses,
845 + set_fan_pulses, TC654_FAN1);
846 +static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input,
847 + NULL, TC654_FAN2);
848 +static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR, show_fan_min,
849 + set_fan_min, TC654_FAN2);
850 +static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max,
851 + NULL, TC654_FAN2);
852 +static SENSOR_DEVICE_ATTR(fan2_min_alarm, S_IRUGO, show_fan_min_alarm,
853 + NULL, TC654_FAN2);
854 +static SENSOR_DEVICE_ATTR(fan2_max_alarm, S_IRUGO, show_fan_max_alarm,
855 + NULL, TC654_FAN2);
856 +static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_fan_fault,
857 + NULL, TC654_FAN2);
858 +static SENSOR_DEVICE_ATTR(fan2_pulses, S_IRUGO | S_IWUSR, show_fan_pulses,
859 + set_fan_pulses, TC654_FAN2);
860 +
861 +static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
862 + set_pwm_enable);
863 +static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm);
864 +
865 +static DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_temp_alarm_otf, NULL);
866 +
867 +/* sensors present on all models */
868 +static struct attribute *tc654_attrs[] = {
869 + &sensor_dev_attr_fan1_input.dev_attr.attr,
870 + &sensor_dev_attr_fan1_min.dev_attr.attr,
871 + &sensor_dev_attr_fan1_max.dev_attr.attr,
872 + &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
873 + &sensor_dev_attr_fan1_max_alarm.dev_attr.attr,
874 + &sensor_dev_attr_fan1_fault.dev_attr.attr,
875 + &sensor_dev_attr_fan1_pulses.dev_attr.attr,
876 + &sensor_dev_attr_fan2_input.dev_attr.attr,
877 + &sensor_dev_attr_fan2_min.dev_attr.attr,
878 + &sensor_dev_attr_fan2_max.dev_attr.attr,
879 + &sensor_dev_attr_fan2_min_alarm.dev_attr.attr,
880 + &sensor_dev_attr_fan2_max_alarm.dev_attr.attr,
881 + &sensor_dev_attr_fan2_fault.dev_attr.attr,
882 + &sensor_dev_attr_fan2_pulses.dev_attr.attr,
883 +
884 + &dev_attr_pwm1_enable.attr,
885 + &dev_attr_pwm1.attr,
886 +
887 + &dev_attr_temp1_emergency_alarm.attr,
888 + NULL
889 +};
890 +
891 +ATTRIBUTE_GROUPS(tc654);
892 +
893 +/* cooling device */
894 +
895 +static int tc654_get_max_state(struct thermal_cooling_device *cdev,
896 + unsigned long *state)
897 +{
898 + *state = 255;
899 + return 0;
900 +}
901 +
902 +static int tc654_get_cur_state(struct thermal_cooling_device *cdev,
903 + unsigned long *state)
904 +{
905 + struct tc654 *tc = cdev->devdata;
906 + int ret;
907 +
908 + if (!tc)
909 + return -EINVAL;
910 +
911 + ret = get_fan_speed(tc);
912 + if (ret < 0)
913 + return ret;
914 +
915 + *state = ret;
916 + return 0;
917 +}
918 +
919 +static int tc654_set_cur_state(struct thermal_cooling_device *cdev,
920 + unsigned long state)
921 +{
922 + struct tc654 *tc = cdev->devdata;
923 +
924 + if (!tc)
925 + return -EINVAL;
926 +
927 + if (state > INT_MAX)
928 + return -EINVAL;
929 +
930 + return set_fan_speed(tc, state);
931 +}
932 +
933 +static const struct thermal_cooling_device_ops tc654_fan_cool_ops = {
934 + .get_max_state = tc654_get_max_state,
935 + .get_cur_state = tc654_get_cur_state,
936 + .set_cur_state = tc654_set_cur_state,
937 +};
938 +
939 +
940 +/* hardware probe and detection */
941 +
942 +static int
943 +tc654_probe(struct i2c_client *client, const struct i2c_device_id *id)
944 +{
945 + struct tc654 *tc;
946 + struct device *hwmon_dev;
947 + int ret, i;
948 +
949 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
950 + return -EIO;
951 +
952 + tc = devm_kzalloc(&client->dev, sizeof(*tc), GFP_KERNEL);
953 + if (!tc)
954 + return -ENOMEM;
955 +
956 + i2c_set_clientdata(client, tc);
957 + tc->client = client;
958 + mutex_init(&tc->update_lock);
959 +
960 + /* cache all 8 registers */
961 + for (i = 0; i < __TC654_REG_NUM; i++) {
962 + ret = read_tc(tc, i);
963 + if (ret < 0)
964 + return ret;
965 + }
966 +
967 + /* sysfs hooks */
968 + hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
969 + client->name, tc,
970 + tc654_groups);
971 + if (IS_ERR(hwmon_dev))
972 + return PTR_ERR(hwmon_dev);
973 +
974 +#if IS_ENABLED(CONFIG_OF)
975 + /* Optional cooling device register for Device tree platforms */
976 + tc->cdev = thermal_of_cooling_device_register(client->dev.of_node,
977 + "tc654", tc,
978 + &tc654_fan_cool_ops);
979 +#else /* CONFIG_OF */
980 + /* Optional cooling device register for non Device tree platforms */
981 + tc->cdev = thermal_cooling_device_register("tc654", tc,
982 + &tc654_fan_cool_ops);
983 +#endif /* CONFIG_OF */
984 +
985 + dev_info(&client->dev, "%s: sensor '%s'\n",
986 + dev_name(hwmon_dev), client->name);
987 +
988 + return 0;
989 +}
990 +
991 +static const struct i2c_device_id tc654_ids[] = {
992 + { "tc654", 0, },
993 + { }
994 +};
995 +MODULE_DEVICE_TABLE(i2c, tc654_ids);
996 +
997 +/* Return 0 if detection is successful, -ENODEV otherwise */
998 +static int
999 +tc654_detect(struct i2c_client *new_client, struct i2c_board_info *info)
1000 +{
1001 + struct i2c_adapter *adapter = new_client->adapter;
1002 + int manufacturer, product;
1003 +
1004 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1005 + return -ENODEV;
1006 +
1007 + manufacturer = i2c_smbus_read_byte_data(new_client, TC654_REG_MFR_ID);
1008 + if (manufacturer != TC654_MFR_ID_MICROCHIP)
1009 + return -ENODEV;
1010 +
1011 + product = i2c_smbus_read_byte_data(new_client, TC654_REG_VER_ID);
1012 + if (!((product == TC654_VER_ID) || (product == TC655_VER_ID)))
1013 + return -ENODEV;
1014 +
1015 + strlcpy(info->type, "tc654", I2C_NAME_SIZE);
1016 + return 0;
1017 +}
1018 +
1019 +static struct i2c_driver tc654_driver = {
1020 + .class = I2C_CLASS_HWMON,
1021 + .driver = {
1022 + .name = "tc654",
1023 + },
1024 + .probe = tc654_probe,
1025 + .id_table = tc654_ids,
1026 + .detect = tc654_detect,
1027 + .address_list = normal_i2c,
1028 +};
1029 +
1030 +module_i2c_driver(tc654_driver);
1031 +
1032 +MODULE_AUTHOR("Christian Lamparter <chunkeey@gmail.com>");
1033 +MODULE_DESCRIPTION("Microchip TC654/TC655 hwmon driver");
1034 +MODULE_LICENSE("GPL");
1035 --
1036 2.8.1
1037