linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [v1,1/1] hwmon: (nct7904) Add watchdog function
@ 2020-03-24  1:46 yuechao.zhao
  2020-03-24 16:49 ` kbuild test robot
  0 siblings, 1 reply; 2+ messages in thread
From: yuechao.zhao @ 2020-03-24  1:46 UTC (permalink / raw)
  To: 345351830
  Cc: Jean Delvare, Guenter Roeck, linux-hwmon, linux-kernel, amy.shih,
	oakley.ding, jia.sui, shengkui.leng, Yuechao Zhao

From: Yuechao Zhao <yuechao.zhao@advantech.com.cn>

implement watchdong functionality into the "hwmon/nct7904.c"

Signed-off-by: Yuechao Zhao <yuechao.zhao@advantech.com.cn>
---
 drivers/hwmon/nct7904.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 156 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/nct7904.c b/drivers/hwmon/nct7904.c
index 1f5743d..137199c 100644
--- a/drivers/hwmon/nct7904.c
+++ b/drivers/hwmon/nct7904.c
@@ -8,6 +8,9 @@
  * Copyright (c) 2019 Advantech
  * Author: Amy.Shih <amy.shih@advantech.com.tw>
  *
+ * Copyright (c) 2020 Advantech
+ * Author: Yuechao Zhao <yuechao.zhao@advantech.com.cn>
+ *
  * Supports the following chips:
  *
  * Chip        #vin  #fan  #pwm  #temp  #dts  chip ID
@@ -20,6 +23,7 @@
 #include <linux/i2c.h>
 #include <linux/mutex.h>
 #include <linux/hwmon.h>
+#include <linux/watchdog.h>
 
 #define VENDOR_ID_REG		0x7A	/* Any bank */
 #define NUVOTON_ID		0x50
@@ -87,18 +91,39 @@
 #define FANCTL1_FMR_REG		0x00	/* Bank 3; 1 reg per channel */
 #define FANCTL1_OUT_REG		0x10	/* Bank 3; 1 reg per channel */
 
+#define WDT_LOCK_REG		0xE0	/* W/O Lock Watchdog Register */
+#define WDT_EN_REG		0xE1	/* R/O Watchdog Enable Register */
+#define WDT_STS_REG		0xE2	/* R/O Watchdog Status Register */
+#define WDT_TIMER_REG		0xE3	/* R/W Watchdog Timer Register */
+#define WDT_SOFT_EN		0x55	/* Enable soft watchdog timer */
+#define WDT_SOFT_DIS		0xAA	/* Disable soft watchdog timer */
+
 #define VOLT_MONITOR_MODE	0x0
 #define THERMAL_DIODE_MODE	0x1
 #define THERMISTOR_MODE		0x3
 
 #define ENABLE_TSI	BIT(1)
 
+#define WATCHDOG_TIMEOUT	1	/* 1 minute default timeout */
+static int ping_timeout = WATCHDOG_TIMEOUT; /* default feeding timeout */
+
+static int timeout = WATCHDOG_TIMEOUT;
+module_param(timeout, int, 0);
+MODULE_PARM_DESC(timeout, "Watchdog timeout in minutes. 1 <= timeout <= 255, default="
+			__MODULE_STRING(WATCHODOG_TIMEOUT) ".");
+
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started once started (default="
+			__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
 static const unsigned short normal_i2c[] = {
 	0x2d, 0x2e, I2C_CLIENT_END
 };
 
 struct nct7904_data {
 	struct i2c_client *client;
+	struct watchdog_device wdt;
 	struct mutex bank_lock;
 	int bank_sel;
 	u32 fanin_mask;
@@ -889,6 +914,91 @@ static int nct7904_detect(struct i2c_client *client,
 	.info = nct7904_info,
 };
 
+/*
+ * Wathcdog Function
+ */
+static int nct7904_wdt_start(struct watchdog_device *wdt)
+{
+	int ret;
+	struct nct7904_data *data = watchdog_get_drvdata(wdt);
+
+	/* Disable soft watchdog timer first */
+	ret = nct7904_write_reg(data, BANK_0, WDT_LOCK_REG, WDT_SOFT_DIS);
+	if (ret < 0)
+		return ret;
+
+	/* Enable soft watchdog timer */
+	ret = nct7904_write_reg(data, BANK_0, WDT_LOCK_REG, WDT_SOFT_EN);
+	return ret;
+}
+
+static int nct7904_wdt_stop(struct watchdog_device *wdt)
+{
+	struct nct7904_data *data = watchdog_get_drvdata(wdt);
+	int ret;
+
+	ret = nct7904_write_reg(data, BANK_0, WDT_LOCK_REG, WDT_SOFT_DIS);
+
+	return ret;
+}
+
+static int nct7904_wdt_set_timeout(struct watchdog_device *wdt,
+				   unsigned int timeout)
+{
+	struct nct7904_data *data = watchdog_get_drvdata(wdt);
+
+	ping_timeout = timeout / 60;
+
+	return nct7904_write_reg(data, BANK_0, WDT_TIMER_REG, ping_timeout);
+}
+
+static int nct7904_wdt_ping(struct watchdog_device *wdt)
+{
+	/*
+	 * Note:
+	 * NCT7904 is not supported refresh WDT_TIMER_REG register when the
+	 * watchdog is actiove. Please disable watchdog before fedding the
+	 * watchdog and enable it again.
+	 */
+	struct nct7904_data *data = watchdog_get_drvdata(wdt);
+	int ret;
+
+	/* Disable soft watchdog timer */
+	ret = nct7904_write_reg(data, BANK_0, WDT_LOCK_REG, WDT_SOFT_DIS);
+	if (ret < 0)
+		goto ping_err;
+
+	/* feed watchdog */
+	ret = nct7904_write_reg(data, BANK_0, WDT_TIMER_REG, ping_timeout);
+	if (ret < 0)
+		goto ping_err;
+
+	/* Enable soft watchdog timer */
+	ret = nct7904_write_reg(data, BANK_0, WDT_TIMER_REG, WDT_SOFT_EN);
+	if (ret < 0)
+		goto ping_err;
+
+	return 0;
+
+ping_err:
+	pr_err("nct7904 ping error\n");
+	return ret;
+}
+
+static const struct watchdog_info nct7904_wdt_info = {
+	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
+				WDIOF_MAGICCLOSE,
+	.identity	= "nct7904 watchdog",
+};
+
+static const struct watchdog_ops nct7904_wdt_ops = {
+	.owner		= THIS_MODULE,
+	.start		= nct7904_wdt_start,
+	.stop		= nct7904_wdt_stop,
+	.ping		= nct7904_wdt_ping,
+	.set_timeout	= nct7904_wdt_set_timeout,
+};
+
 static int nct7904_probe(struct i2c_client *client,
 			 const struct i2c_device_id *id)
 {
@@ -1012,7 +1122,51 @@ static int nct7904_probe(struct i2c_client *client,
 	hwmon_dev =
 		devm_hwmon_device_register_with_info(dev, client->name, data,
 						     &nct7904_chip_info, NULL);
-	return PTR_ERR_OR_ZERO(hwmon_dev);
+	ret = PTR_ERR_OR_ZERO(hwmon_dev);
+	if (ret)
+		return ret;
+
+	/* Watchdog initialization */
+	data->wdt.ops = &nct7904_wdt_ops;
+	data->wdt.info = &nct7904_wdt_info;
+
+	data->wdt.timeout = timeout * 60; /* in seconds */
+	data->wdt.min_timeout = 1;
+	data->wdt.max_timeout = 15300;
+	data->wdt.parent = &client->dev;
+
+	watchdog_init_timeout(&data->wdt, timeout, &client->dev);
+	watchdog_set_nowayout(&data->wdt, nowayout);
+	watchdog_set_drvdata(&data->wdt, data);
+
+	i2c_set_clientdata(client, data);
+
+	ret = watchdog_register_device(&data->wdt);
+	if (ret)
+		return ret;
+
+	dev_info(&client->dev, "NCT7904 HWMON and Watchdog device probed\n");
+
+	return ret;
+}
+
+static int nct7904_remove(struct i2c_client *client)
+{
+	/*
+	 * HWMON use devm_hwmon_device_register_with_info() register. So, do
+	 * not need unregister it manually.
+	 */
+	struct nct7904_data *data = i2c_get_clientdata(client);
+
+	/* disable watchdog */
+	if (!nowayout)
+		nct7904_write_reg(data, BANK_0, WDT_LOCK_REG, WDT_SOFT_DIS);
+
+	watchdog_unregister_device(&data->wdt);
+
+	dev_info(&client->dev, "NCT7904 driver removed\n");
+
+	return 0;
 }
 
 static const struct i2c_device_id nct7904_id[] = {
@@ -1030,6 +1184,7 @@ static int nct7904_probe(struct i2c_client *client,
 	.id_table = nct7904_id,
 	.detect = nct7904_detect,
 	.address_list = normal_i2c,
+	.remove = nct7904_remove,
 };
 
 module_i2c_driver(nct7904_driver);
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [v1,1/1] hwmon: (nct7904) Add watchdog function
  2020-03-24  1:46 [v1,1/1] hwmon: (nct7904) Add watchdog function yuechao.zhao
@ 2020-03-24 16:49 ` kbuild test robot
  0 siblings, 0 replies; 2+ messages in thread
From: kbuild test robot @ 2020-03-24 16:49 UTC (permalink / raw)
  To: yuechao.zhao
  Cc: kbuild-all, 345351830@qq.com, Jean Delvare, Guenter Roeck,
	linux-hwmon, linux-kernel, amy.shih, oakley.ding, jia.sui,
	shengkui.leng, Yuechao Zhao

[-- Attachment #1: Type: text/plain, Size: 1646 bytes --]

Hi,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on hwmon/hwmon-next]
[also build test ERROR on next-20200324]
[cannot apply to v5.6-rc7]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/yuechao-zhao-advantech-com-cn/hwmon-nct7904-Add-watchdog-function/20200324-095738
base:   https://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging.git hwmon-next
config: h8300-randconfig-a001-20200324 (attached as .config)
compiler: h8300-linux-gcc (GCC) 9.2.0
reproduce:
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        GCC_VERSION=9.2.0 make.cross ARCH=h8300 

If you fix the issue, kindly add following tag
Reported-by: kbuild test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   h8300-linux-ld: drivers/hwmon/nct7904.o: in function `nct7904_remove':
   drivers/hwmon/nct7904.c:1165: undefined reference to `watchdog_unregister_device'
   h8300-linux-ld: drivers/hwmon/nct7904.o: in function `nct7904_probe':
   drivers/hwmon/nct7904.c:1138: undefined reference to `watchdog_init_timeout'
>> h8300-linux-ld: drivers/hwmon/nct7904.c:1144: undefined reference to `watchdog_register_device'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 26148 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-03-24 16:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-24  1:46 [v1,1/1] hwmon: (nct7904) Add watchdog function yuechao.zhao
2020-03-24 16:49 ` kbuild test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).