Skip to content

Commit 77849a5

Browse files
Yuechao Zhaogroeck
authored andcommitted
hwmon: (nct7904) Add watchdog function
Implement watchdog functionality for NCT7904. Signed-off-by: Yuechao Zhao <[email protected]> Link: https://lore.kernel.org/r/[email protected] [groeck: Squashed fixup patch] Signed-off-by: Guenter Roeck <[email protected]>
1 parent b9bbe6e commit 77849a5

File tree

2 files changed

+141
-3
lines changed

2 files changed

+141
-3
lines changed

drivers/hwmon/Kconfig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,10 +1340,12 @@ config SENSORS_NCT7802
13401340

13411341
config SENSORS_NCT7904
13421342
tristate "Nuvoton NCT7904"
1343-
depends on I2C
1343+
depends on I2C && WATCHDOG
1344+
select WATCHDOG_CORE
13441345
help
13451346
If you say yes here you get support for the Nuvoton NCT7904
1346-
hardware monitoring chip, including manual fan speed control.
1347+
hardware monitoring chip, including manual fan speed control
1348+
and support for the integrated watchdog.
13471349

13481350
This driver can also be built as a module. If so, the module
13491351
will be called nct7904.

drivers/hwmon/nct7904.c

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
* Copyright (c) 2019 Advantech
99
* Author: Amy.Shih <[email protected]>
1010
*
11+
* Copyright (c) 2020 Advantech
12+
* Author: Yuechao Zhao <[email protected]>
13+
*
1114
* Supports the following chips:
1215
*
1316
* Chip #vin #fan #pwm #temp #dts chip ID
@@ -20,6 +23,7 @@
2023
#include <linux/i2c.h>
2124
#include <linux/mutex.h>
2225
#include <linux/hwmon.h>
26+
#include <linux/watchdog.h>
2327

2428
#define VENDOR_ID_REG 0x7A /* Any bank */
2529
#define NUVOTON_ID 0x50
@@ -88,18 +92,42 @@
8892
#define FANCTL1_FMR_REG 0x00 /* Bank 3; 1 reg per channel */
8993
#define FANCTL1_OUT_REG 0x10 /* Bank 3; 1 reg per channel */
9094

95+
#define WDT_LOCK_REG 0xE0 /* W/O Lock Watchdog Register */
96+
#define WDT_EN_REG 0xE1 /* R/O Watchdog Enable Register */
97+
#define WDT_STS_REG 0xE2 /* R/O Watchdog Status Register */
98+
#define WDT_TIMER_REG 0xE3 /* R/W Watchdog Timer Register */
99+
#define WDT_SOFT_EN 0x55 /* Enable soft watchdog timer */
100+
#define WDT_SOFT_DIS 0xAA /* Disable soft watchdog timer */
101+
91102
#define VOLT_MONITOR_MODE 0x0
92103
#define THERMAL_DIODE_MODE 0x1
93104
#define THERMISTOR_MODE 0x3
94105

95106
#define ENABLE_TSI BIT(1)
96107

108+
#define WATCHDOG_TIMEOUT 1 /* 1 minute default timeout */
109+
110+
/*The timeout range is 1-255 minutes*/
111+
#define MIN_TIMEOUT (1 * 60)
112+
#define MAX_TIMEOUT (255 * 60)
113+
114+
static int timeout = WATCHDOG_TIMEOUT;
115+
module_param(timeout, int, 0);
116+
MODULE_PARM_DESC(timeout, "Watchdog timeout in minutes. 1 <= timeout <= 255, default="
117+
__MODULE_STRING(WATCHODOG_TIMEOUT) ".");
118+
119+
static bool nowayout = WATCHDOG_NOWAYOUT;
120+
module_param(nowayout, bool, 0);
121+
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started once started (default="
122+
__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
123+
97124
static const unsigned short normal_i2c[] = {
98125
0x2d, 0x2e, I2C_CLIENT_END
99126
};
100127

101128
struct nct7904_data {
102129
struct i2c_client *client;
130+
struct watchdog_device wdt;
103131
struct mutex bank_lock;
104132
int bank_sel;
105133
u32 fanin_mask;
@@ -892,6 +920,95 @@ static const struct hwmon_chip_info nct7904_chip_info = {
892920
.info = nct7904_info,
893921
};
894922

923+
/*
924+
* Watchdog Function
925+
*/
926+
static int nct7904_wdt_start(struct watchdog_device *wdt)
927+
{
928+
struct nct7904_data *data = watchdog_get_drvdata(wdt);
929+
930+
/* Enable soft watchdog timer */
931+
return nct7904_write_reg(data, BANK_0, WDT_LOCK_REG, WDT_SOFT_EN);
932+
}
933+
934+
static int nct7904_wdt_stop(struct watchdog_device *wdt)
935+
{
936+
struct nct7904_data *data = watchdog_get_drvdata(wdt);
937+
938+
return nct7904_write_reg(data, BANK_0, WDT_LOCK_REG, WDT_SOFT_DIS);
939+
}
940+
941+
static int nct7904_wdt_set_timeout(struct watchdog_device *wdt,
942+
unsigned int timeout)
943+
{
944+
struct nct7904_data *data = watchdog_get_drvdata(wdt);
945+
/*
946+
* The NCT7904 is very special in watchdog function.
947+
* Its minimum unit is minutes. And wdt->timeout needs
948+
* to match the actual timeout selected. So, this needs
949+
* to be: wdt->timeout = timeout / 60 * 60.
950+
* For example, if the user configures a timeout of
951+
* 119 seconds, the actual timeout will be 60 seconds.
952+
* So, wdt->timeout must then be set to 60 seconds.
953+
*/
954+
wdt->timeout = timeout / 60 * 60;
955+
956+
return nct7904_write_reg(data, BANK_0, WDT_TIMER_REG,
957+
wdt->timeout / 60);
958+
}
959+
960+
static int nct7904_wdt_ping(struct watchdog_device *wdt)
961+
{
962+
/*
963+
* Note:
964+
* NCT7904 does not support refreshing WDT_TIMER_REG register when
965+
* the watchdog is active. Please disable watchdog before feeding
966+
* the watchdog and enable it again.
967+
*/
968+
struct nct7904_data *data = watchdog_get_drvdata(wdt);
969+
int ret;
970+
971+
/* Disable soft watchdog timer */
972+
ret = nct7904_write_reg(data, BANK_0, WDT_LOCK_REG, WDT_SOFT_DIS);
973+
if (ret < 0)
974+
return ret;
975+
976+
/* feed watchdog */
977+
ret = nct7904_write_reg(data, BANK_0, WDT_TIMER_REG, wdt->timeout / 60);
978+
if (ret < 0)
979+
return ret;
980+
981+
/* Enable soft watchdog timer */
982+
return nct7904_write_reg(data, BANK_0, WDT_LOCK_REG, WDT_SOFT_EN);
983+
}
984+
985+
static unsigned int nct7904_wdt_get_timeleft(struct watchdog_device *wdt)
986+
{
987+
struct nct7904_data *data = watchdog_get_drvdata(wdt);
988+
int ret;
989+
990+
ret = nct7904_read_reg(data, BANK_0, WDT_TIMER_REG);
991+
if (ret < 0)
992+
return 0;
993+
994+
return ret * 60;
995+
}
996+
997+
static const struct watchdog_info nct7904_wdt_info = {
998+
.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
999+
WDIOF_MAGICCLOSE,
1000+
.identity = "nct7904 watchdog",
1001+
};
1002+
1003+
static const struct watchdog_ops nct7904_wdt_ops = {
1004+
.owner = THIS_MODULE,
1005+
.start = nct7904_wdt_start,
1006+
.stop = nct7904_wdt_stop,
1007+
.ping = nct7904_wdt_ping,
1008+
.set_timeout = nct7904_wdt_set_timeout,
1009+
.get_timeleft = nct7904_wdt_get_timeleft,
1010+
};
1011+
8951012
static int nct7904_probe(struct i2c_client *client,
8961013
const struct i2c_device_id *id)
8971014
{
@@ -1022,7 +1139,26 @@ static int nct7904_probe(struct i2c_client *client,
10221139
hwmon_dev =
10231140
devm_hwmon_device_register_with_info(dev, client->name, data,
10241141
&nct7904_chip_info, NULL);
1025-
return PTR_ERR_OR_ZERO(hwmon_dev);
1142+
ret = PTR_ERR_OR_ZERO(hwmon_dev);
1143+
if (ret)
1144+
return ret;
1145+
1146+
/* Watchdog initialization */
1147+
data->wdt.ops = &nct7904_wdt_ops;
1148+
data->wdt.info = &nct7904_wdt_info;
1149+
1150+
data->wdt.timeout = timeout * 60; /* in seconds */
1151+
data->wdt.min_timeout = MIN_TIMEOUT;
1152+
data->wdt.max_timeout = MAX_TIMEOUT;
1153+
data->wdt.parent = &client->dev;
1154+
1155+
watchdog_init_timeout(&data->wdt, timeout * 60, &client->dev);
1156+
watchdog_set_nowayout(&data->wdt, nowayout);
1157+
watchdog_set_drvdata(&data->wdt, data);
1158+
1159+
watchdog_stop_on_unregister(&data->wdt);
1160+
1161+
return devm_watchdog_register_device(dev, &data->wdt);
10261162
}
10271163

10281164
static const struct i2c_device_id nct7904_id[] = {

0 commit comments

Comments
 (0)