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