From patchwork Wed Jul 1 10:09:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?Sm9obnNvbiBDSCBDaGVuICjpmbPmmK3li7Mp?= X-Patchwork-Id: 11635997 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B14A260D for ; Wed, 1 Jul 2020 10:09:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7C2EF2073E for ; Wed, 1 Jul 2020 10:09:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=moxa.com header.i=@moxa.com header.b="ZI/9WaKt" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729339AbgGAKJ7 (ORCPT ); Wed, 1 Jul 2020 06:09:59 -0400 Received: from mail-eopbgr1300071.outbound.protection.outlook.com ([40.107.130.71]:22944 "EHLO APC01-HK2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728941AbgGAKJ6 (ORCPT ); Wed, 1 Jul 2020 06:09:58 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KcKthpmryrw9h2QNaAx9AqiFz7V5arnjNBDO/w0jTOElZyS740VkSydlgDSSkh1fvWdafrp/d4SQ5asqy0OGtEC4YvljteDatp4Ofj5T0mH2c61eg0tp/rCXpPuI/mv9c9A6TSqN8Z+xg0ndf5I8UHQ7G77eFlDUyIMX2gNQLqWDlk8sJ66JekXbpFdxbnT16FBVmCWQp9qesAiaoFJSoJk/0y/QY17KJsWP5WqqHDSIYefzWWQQNcfdpuNx/thSwLRzqFVMcZe/sKFCBQTFS6A/rtP7BoSl94UBWqLnLU1HauqCRpbidNT+egiw0+AurKa/XD8ox0/4PqduCfRvcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Bi6fLrFcA+Z2f96scn53qPMJ0qBFonzpmBXsA9QXAaU=; b=iaIXGCuvRzE30AhceEYoSFCMKiM4EfrVSDLESiwSGn/VudiTBL1iOMo+yl0wwVoBOy6hOawTWykdeQ3Pp9G31MhE/ejhL4v8Mo4h67xLfqZdAD6GTkovqRu1d8IokUi8a0DVXKsIV/9w4e2/WCM45+Z6Lg2Bag3yO61lsMVvIvTKcxFP3XbcYEZQfHovF8ZDqfdSPz7xXWDG2RJiMTuUV+/+WD8dMpSe8ylse3pMUu7Pdhbf50FDsgR0JVrGjkpHxkr5j+2gRl9Vfz10hOsJs8yynjJqUHVvQ+XfA2BNUv5vMk3vdYszlVvF/T6biL+oI4U1vO+MdSKUdlvbq/nsQQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=moxa.com; dmarc=pass action=none header.from=moxa.com; dkim=pass header.d=moxa.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=moxa.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Bi6fLrFcA+Z2f96scn53qPMJ0qBFonzpmBXsA9QXAaU=; b=ZI/9WaKtMe+7krd66cwXGzyjTV7Jc/FHIUIuwEXne8A8gDZ3XhE76/37wlz6iAOqE5bbycKaGs4GD0kLSvHG6xBQR2IPRUBI+BM0p2yvYDh3p4lcZrJ1w7UO3UnMqup4E9SpPzhkij0lfrltLBaFwxpmyKpItCEjJFMDEurrS5E= Received: from HK2PR01MB3281.apcprd01.prod.exchangelabs.com (2603:1096:202:22::12) by HK0PR01MB1955.apcprd01.prod.exchangelabs.com (2603:1096:203:1c::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3153.21; Wed, 1 Jul 2020 10:09:49 +0000 Received: from HK2PR01MB3281.apcprd01.prod.exchangelabs.com ([fe80::712b:170d:f873:68a3]) by HK2PR01MB3281.apcprd01.prod.exchangelabs.com ([fe80::712b:170d:f873:68a3%6]) with mapi id 15.20.3131.028; Wed, 1 Jul 2020 10:09:48 +0000 From: =?big5_tw?b?Sm9obnNvbiBDSCBDaGVuICizr6xMvrEp?= To: "linux-kernel@vger.kernel.org" CC: "linux-rtc@vger.kernel.org" , LINUX-WATCHDOG , Wim Van Sebroeck , Alessandro Zummo , Alexandre Belloni , Guenter Roeck Subject: [PATCH] rtc: ds1374: wdt: Use watchdog core for watchdog part Thread-Topic: [PATCH] rtc: ds1374: wdt: Use watchdog core for watchdog part Thread-Index: AQHWT46vOpykt28FdUy3wlV4O8FI6g== Date: Wed, 1 Jul 2020 10:09:48 +0000 Message-ID: Accept-Language: zh-TW, en-US Content-Language: zh-TW X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=none action=none header.from=moxa.com; x-originating-ip: [123.51.145.16] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: f484dc15-684d-401a-98c0-08d81da6e360 x-ms-traffictypediagnostic: HK0PR01MB1955: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:6108; x-forefront-prvs: 04519BA941 x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: s6aMr271Mni9BfoUcnyq09ti/pI11ZDX5zIr73iTgmZibUG5f4yzX34LA4bOzBMTDIYPfvoiQ8+l4HDJzOcXVfVbLpqaV4i7RZ1iE5UuGfM3IOlQucY/1jD7A1nmx4l+C+9AoJc31VtkG4SvbTutoRYBOjz6/TPXviHE8N9/isjlFHgA9YhY/wNWprw1PxEut7C/EjQY0XyTTW4u0qsbaz85HBTNZ8eKo9kR2x3U1o4WxV0B2wm9p8bJL4RSGXdYiftQEaV4NjXB80Q08gQCXUQs7CoU7iCvWULS3rUfbFhnhB5S5FmkamFVK2HKZ59veJ8CRRGpV6qF6k46cW2r9yvrcjs5yKiCVEyOSknmds8dKXnkEH6yEN7dHPh3owMVvPaAKz+RHFrJ8KqSuXSncw== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:HK2PR01MB3281.apcprd01.prod.exchangelabs.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(396003)(136003)(39850400004)(376002)(366004)(346002)(6916009)(55016002)(8676002)(4326008)(186003)(8936002)(9686003)(85182001)(83380400001)(316002)(52536014)(71200400001)(66946007)(66556008)(64756008)(66446008)(66476007)(76116006)(33656002)(6506007)(7696005)(5660300002)(478600001)(54906003)(30864003)(2906002)(86362001)(26005)(171213001)(6606295002);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata: WbTQxwk80XZGvPEkPoQNj/ZFWN1R/jg/yGxqIZt6FY2LYdQNXC5/JEmVRCtdBSCuQUh/I/0hsv5mC+CSGS7A0zjAuT1SJBRq+tOiizjXETq5+6A55G1Z1P8qwKZFblMMAAKd6HrDCNsI/XgJcfOPB+faf8SYRv19lClkftvdelMyEzdnSB5ZKyyOnsNLq56y5Lkam1JMf/+mJCl5gYYqZa6MVdz3G9BC9TEVYUflFMYqbYi3tlVfyAB2KyfRPnQ3YPBOVqT9Bwz56Bewjn/gtI/xmyK2mTlNbkkzWhYWw0Fn8mAzWjKgJQiPp60iHMF7debwlVJBSN2Mj+gx//LJAFQTV5D8zi7GmizxhmRlEzrO0gzLOWHk1vr+0zm6g0JQV9wWosyMnZrXZfWXjYymqxua/UQ9GjeHPnqLqy7u/x8ReFtd2T90whQkHBVKtSxlt9o185tUDxBw+dkk4NA2jx6kbiKWGHV/ohFM2FoUaqY= x-ms-exchange-transport-forked: True MIME-Version: 1.0 X-OriginatorOrg: moxa.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: HK2PR01MB3281.apcprd01.prod.exchangelabs.com X-MS-Exchange-CrossTenant-Network-Message-Id: f484dc15-684d-401a-98c0-08d81da6e360 X-MS-Exchange-CrossTenant-originalarrivaltime: 01 Jul 2020 10:09:48.7691 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 5571c7d4-286b-47f6-9dd5-0aa688773c8e X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: SnZ35o4dQ5gn6+m7gmQ58emO+sxn9nkQky4F75BR3iFmZQ9IMm1vg/HJs9AHkT7ygEpY0sdhh44Jqzlx7S1GVA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: HK0PR01MB1955 Sender: linux-watchdog-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org Let ds1374 watchdog use watchdog core. It also includes improving watchdog timer setting and nowayout, and just uses ioctl() of watchdog core. Signed-off-by: Johnson Chen Reported-by: kernel test robot Reported-by: kernel test robot --- drivers/rtc/rtc-ds1374.c | 257 ++++++++++----------------------------- 1 file changed, 67 insertions(+), 190 deletions(-) diff --git a/drivers/rtc/rtc-ds1374.c b/drivers/rtc/rtc-ds1374.c index 9c51a12cf70f..25b28f7546ff 100644 --- a/drivers/rtc/rtc-ds1374.c +++ b/drivers/rtc/rtc-ds1374.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 /* * RTC client/driver for the Maxim/Dallas DS1374 Real-Time Clock over I2C * @@ -6,11 +7,7 @@ * * Copyright (C) 2014 Rose Technology * Copyright (C) 2006-2007 Freescale Semiconductor - * - * 2005 (c) MontaVista Software, Inc. This file is licensed under - * the terms of the GNU General Public License version 2. This program - * is licensed "as is" without any warranty of any kind, whether express - * or implied. + * Copyright (C) 2005 (c) MontaVista Software, Inc. */ /* * It would be more efficient to use i2c msgs/i2c_transfer directly but, as @@ -46,6 +43,7 @@ #define DS1374_REG_WDALM2 0x06 #define DS1374_REG_CR 0x07 /* Control */ #define DS1374_REG_CR_AIE 0x01 /* Alarm Int. Enable */ +#define DS1374_REG_CR_WDSTR 0x08 /* 1=INT, 0=RST */ #define DS1374_REG_CR_WDALM 0x20 /* 1=Watchdog, 0=Alarm */ #define DS1374_REG_CR_WACE 0x40 /* WD/Alarm counter enable */ #define DS1374_REG_SR 0x08 /* Status */ @@ -71,7 +69,9 @@ struct ds1374 { struct i2c_client *client; struct rtc_device *rtc; struct work_struct work; - +#ifdef CONFIG_RTC_DRV_DS1374_WDT + struct watchdog_device wdt; +#endif /* The mutex protects alarm operations, and prevents a race * between the enable_irq() in the workqueue and the free_irq() * in the remove function. @@ -257,7 +257,8 @@ static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) goto out; /* Disable any existing alarm before setting the new one - * (or lack thereof). */ + * (or lack thereof). + */ cr &= ~DS1374_REG_CR_WACE; ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr); @@ -371,14 +372,21 @@ static const struct rtc_class_ops ds1374_rtc_ops = { */ static struct i2c_client *save_client; /* Default margin */ -#define WD_TIMO 131762 +#define TIMER_MARGIN_DEFAULT 32 +#define TIMER_MARGIN_MIN 1 +#define TIMER_MARGIN_MAX (60*60*24) /* one day */ #define DRV_NAME "DS1374 Watchdog" -static int wdt_margin = WD_TIMO; -static unsigned long wdt_is_open; -module_param(wdt_margin, int, 0); -MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)"); +static int timeout = TIMER_MARGIN_DEFAULT; +module_param(timeout, int, 0); +MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default 32s)"); + +static bool nowayout = WATCHDOG_NOWAYOUT; +module_param(nowayout, bool, 0); +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default =" + __MODULE_STRING(WATCHDOG_NOWAYOUT)")"); + static const struct watchdog_info ds1374_wdt_info = { .identity = "DS1374 WTD", @@ -386,57 +394,61 @@ static const struct watchdog_info ds1374_wdt_info = { WDIOF_MAGICCLOSE, }; -static int ds1374_wdt_settimeout(unsigned int timeout) +static int ds1374_wdt_settimeout(struct watchdog_device *wdt, + unsigned int timeout) { - int ret = -ENOIOCTLCMD; - int cr; + int ret, cr; + + wdt->timeout = timeout; - ret = cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR); + cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR); + ret = cr; if (ret < 0) - goto out; + return ret; /* Disable any existing watchdog/alarm before setting the new one */ cr &= ~DS1374_REG_CR_WACE; ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr); if (ret < 0) - goto out; + return ret; /* Set new watchdog time */ + timeout = timeout * 4096; ret = ds1374_write_rtc(save_client, timeout, DS1374_REG_WDALM0, 3); if (ret) { pr_info("couldn't set new watchdog time\n"); - goto out; + return ret; } /* Enable watchdog timer */ cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM; + cr &= ~DS1374_REG_CR_WDSTR;/* for RST PIN */ cr &= ~DS1374_REG_CR_AIE; ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr); if (ret < 0) - goto out; + return ret; return 0; -out: - return ret; } - /* * Reload the watchdog timer. (ie, pat the watchdog) */ -static void ds1374_wdt_ping(void) +static int ds1374_wdt_ping(struct watchdog_device *wdt) { u32 val; - int ret = 0; - ret = ds1374_read_rtc(save_client, &val, DS1374_REG_WDALM0, 3); - if (ret) - pr_info("WD TICK FAIL!!!!!!!!!! %i\n", ret); + return ds1374_read_rtc(save_client, &val, DS1374_REG_WDALM0, 3); +} + +static int ds1374_wdt_start(struct watchdog_device *wdt) +{ + return ds1374_wdt_ping(wdt); } -static void ds1374_wdt_disable(void) +static int ds1374_wdt_stop(struct watchdog_device *wdt) { int cr; @@ -444,162 +456,17 @@ static void ds1374_wdt_disable(void) /* Disable watchdog timer */ cr &= ~DS1374_REG_CR_WACE; - i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr); + return i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr); } -/* - * Watchdog device is opened, and watchdog starts running. - */ -static int ds1374_wdt_open(struct inode *inode, struct file *file) -{ - struct ds1374 *ds1374 = i2c_get_clientdata(save_client); - - if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) { - mutex_lock(&ds1374->mutex); - if (test_and_set_bit(0, &wdt_is_open)) { - mutex_unlock(&ds1374->mutex); - return -EBUSY; - } - /* - * Activate - */ - wdt_is_open = 1; - mutex_unlock(&ds1374->mutex); - return stream_open(inode, file); - } - return -ENODEV; -} - -/* - * Close the watchdog device. - */ -static int ds1374_wdt_release(struct inode *inode, struct file *file) -{ - if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) - clear_bit(0, &wdt_is_open); - - return 0; -} - -/* - * Pat the watchdog whenever device is written to. - */ -static ssize_t ds1374_wdt_write(struct file *file, const char __user *data, - size_t len, loff_t *ppos) -{ - if (len) { - ds1374_wdt_ping(); - return 1; - } - return 0; -} - -static ssize_t ds1374_wdt_read(struct file *file, char __user *data, - size_t len, loff_t *ppos) -{ - return 0; -} - -/* - * Handle commands from user-space. - */ -static long ds1374_wdt_ioctl(struct file *file, unsigned int cmd, - unsigned long arg) -{ - int new_margin, options; - - switch (cmd) { - case WDIOC_GETSUPPORT: - return copy_to_user((struct watchdog_info __user *)arg, - &ds1374_wdt_info, sizeof(ds1374_wdt_info)) ? -EFAULT : 0; - - case WDIOC_GETSTATUS: - case WDIOC_GETBOOTSTATUS: - return put_user(0, (int __user *)arg); - case WDIOC_KEEPALIVE: - ds1374_wdt_ping(); - return 0; - case WDIOC_SETTIMEOUT: - if (get_user(new_margin, (int __user *)arg)) - return -EFAULT; - - /* the hardware's tick rate is 4096 Hz, so - * the counter value needs to be scaled accordingly - */ - new_margin <<= 12; - if (new_margin < 1 || new_margin > 16777216) - return -EINVAL; - - wdt_margin = new_margin; - ds1374_wdt_settimeout(new_margin); - ds1374_wdt_ping(); - /* fallthrough */ - case WDIOC_GETTIMEOUT: - /* when returning ... inverse is true */ - return put_user((wdt_margin >> 12), (int __user *)arg); - case WDIOC_SETOPTIONS: - if (copy_from_user(&options, (int __user *)arg, sizeof(int))) - return -EFAULT; - - if (options & WDIOS_DISABLECARD) { - pr_info("disable watchdog\n"); - ds1374_wdt_disable(); - return 0; - } - - if (options & WDIOS_ENABLECARD) { - pr_info("enable watchdog\n"); - ds1374_wdt_settimeout(wdt_margin); - ds1374_wdt_ping(); - return 0; - } - return -EINVAL; - } - return -ENOTTY; -} - -static long ds1374_wdt_unlocked_ioctl(struct file *file, unsigned int cmd, - unsigned long arg) -{ - int ret; - struct ds1374 *ds1374 = i2c_get_clientdata(save_client); - - mutex_lock(&ds1374->mutex); - ret = ds1374_wdt_ioctl(file, cmd, arg); - mutex_unlock(&ds1374->mutex); - - return ret; -} - -static int ds1374_wdt_notify_sys(struct notifier_block *this, - unsigned long code, void *unused) -{ - if (code == SYS_DOWN || code == SYS_HALT) - /* Disable Watchdog */ - ds1374_wdt_disable(); - return NOTIFY_DONE; -} - -static const struct file_operations ds1374_wdt_fops = { - .owner = THIS_MODULE, - .read = ds1374_wdt_read, - .unlocked_ioctl = ds1374_wdt_unlocked_ioctl, - .compat_ioctl = compat_ptr_ioctl, - .write = ds1374_wdt_write, - .open = ds1374_wdt_open, - .release = ds1374_wdt_release, - .llseek = no_llseek, -}; - -static struct miscdevice ds1374_miscdev = { - .minor = WATCHDOG_MINOR, - .name = "watchdog", - .fops = &ds1374_wdt_fops, +static const struct watchdog_ops ds1374_wdt_fops = { + .owner = THIS_MODULE, + .start = ds1374_wdt_start, + .stop = ds1374_wdt_stop, + .ping = ds1374_wdt_ping, + .set_timeout = ds1374_wdt_settimeout, }; -static struct notifier_block ds1374_wdt_notifier = { - .notifier_call = ds1374_wdt_notify_sys, -}; #endif /*CONFIG_RTC_DRV_DS1374_WDT*/ /* @@ -653,15 +520,25 @@ static int ds1374_probe(struct i2c_client *client, #ifdef CONFIG_RTC_DRV_DS1374_WDT save_client = client; - ret = misc_register(&ds1374_miscdev); - if (ret) - return ret; - ret = register_reboot_notifier(&ds1374_wdt_notifier); + ds1374->wdt.info = &ds1374_wdt_info; + ds1374->wdt.ops = &ds1374_wdt_fops; + ds1374->wdt.timeout = TIMER_MARGIN_DEFAULT; + ds1374->wdt.min_timeout = TIMER_MARGIN_MIN; + ds1374->wdt.max_timeout = TIMER_MARGIN_MAX; + + watchdog_init_timeout(&ds1374->wdt, timeout, &client->dev); + watchdog_set_nowayout(&ds1374->wdt, nowayout); + watchdog_stop_on_reboot(&ds1374->wdt); + watchdog_stop_on_unregister(&ds1374->wdt); + + ret = devm_watchdog_register_device(&client->dev, &ds1374->wdt); if (ret) { - misc_deregister(&ds1374_miscdev); + dev_err(&client->dev, "failed to register DS1374 watchdog device\n"); return ret; } - ds1374_wdt_settimeout(131072); + + ds1374_wdt_settimeout(&ds1374->wdt, timeout); + dev_info(&client->dev, "DS1374 watchdog device enabled\n"); #endif return 0; @@ -670,10 +547,10 @@ static int ds1374_probe(struct i2c_client *client, static int ds1374_remove(struct i2c_client *client) { struct ds1374 *ds1374 = i2c_get_clientdata(client); + #ifdef CONFIG_RTC_DRV_DS1374_WDT - misc_deregister(&ds1374_miscdev); - ds1374_miscdev.parent = NULL; - unregister_reboot_notifier(&ds1374_wdt_notifier); + dev_warn(&client->dev, "Unregister DS1374 watchdog device\n"); + watchdog_unregister_device(&ds1374->wdt); #endif if (client->irq > 0) {