901-hwmon-add-driver-for-Microchip-TC654-TC655-PWM-fan-c.patch 26 KB

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