2018-07-10 17:21:23 +03:00
// SPDX-License-Identifier: GPL-2.0
2011-09-28 00:35:40 +04:00
/*
* Watchdog driver for the RTC based watchdog in STMP3xxx and i . MX23 / 28
*
2015-04-20 16:51:44 +03:00
* Author : Wolfram Sang < kernel @ pengutronix . de >
2011-09-28 00:35:40 +04:00
*
* Copyright ( C ) 2011 - 12 Wolfram Sang , Pengutronix
*/
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/watchdog.h>
# include <linux/platform_device.h>
# include <linux/stmp3xxx_rtc_wdt.h>
2015-12-12 17:32:15 +03:00
# include <linux/notifier.h>
# include <linux/reboot.h>
2011-09-28 00:35:40 +04:00
# define WDOG_TICK_RATE 1000 /* 1 kHz clock */
# define STMP3XXX_DEFAULT_TIMEOUT 19
# define STMP3XXX_MAX_TIMEOUT (UINT_MAX / WDOG_TICK_RATE)
static int heartbeat = STMP3XXX_DEFAULT_TIMEOUT ;
module_param ( heartbeat , uint , 0 ) ;
MODULE_PARM_DESC ( heartbeat , " Watchdog heartbeat period in seconds from 1 to "
__MODULE_STRING ( STMP3XXX_MAX_TIMEOUT ) " , default "
__MODULE_STRING ( STMP3XXX_DEFAULT_TIMEOUT ) ) ;
static int wdt_start ( struct watchdog_device * wdd )
{
struct device * dev = watchdog_get_drvdata ( wdd ) ;
2013-07-30 14:58:51 +04:00
struct stmp3xxx_wdt_pdata * pdata = dev_get_platdata ( dev ) ;
2011-09-28 00:35:40 +04:00
pdata - > wdt_set_timeout ( dev - > parent , wdd - > timeout * WDOG_TICK_RATE ) ;
return 0 ;
}
static int wdt_stop ( struct watchdog_device * wdd )
{
struct device * dev = watchdog_get_drvdata ( wdd ) ;
2013-07-30 14:58:51 +04:00
struct stmp3xxx_wdt_pdata * pdata = dev_get_platdata ( dev ) ;
2011-09-28 00:35:40 +04:00
pdata - > wdt_set_timeout ( dev - > parent , 0 ) ;
return 0 ;
}
static int wdt_set_timeout ( struct watchdog_device * wdd , unsigned new_timeout )
{
wdd - > timeout = new_timeout ;
return wdt_start ( wdd ) ;
}
static const struct watchdog_info stmp3xxx_wdt_ident = {
. options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING ,
. identity = " STMP3XXX RTC Watchdog " ,
} ;
static const struct watchdog_ops stmp3xxx_wdt_ops = {
. owner = THIS_MODULE ,
. start = wdt_start ,
. stop = wdt_stop ,
. set_timeout = wdt_set_timeout ,
} ;
static struct watchdog_device stmp3xxx_wdd = {
. info = & stmp3xxx_wdt_ident ,
. ops = & stmp3xxx_wdt_ops ,
. min_timeout = 1 ,
. max_timeout = STMP3XXX_MAX_TIMEOUT ,
. status = WATCHDOG_NOWAYOUT_INIT_STATUS ,
} ;
2015-12-12 17:32:15 +03:00
static int wdt_notify_sys ( struct notifier_block * nb , unsigned long code ,
void * unused )
{
switch ( code ) {
case SYS_DOWN : /* keep enabled, system might crash while going down */
break ;
case SYS_HALT : /* allow the system to actually halt */
case SYS_POWER_OFF :
wdt_stop ( & stmp3xxx_wdd ) ;
break ;
}
return NOTIFY_DONE ;
}
static struct notifier_block wdt_notifier = {
. notifier_call = wdt_notify_sys ,
} ;
2011-09-28 00:35:40 +04:00
static int stmp3xxx_wdt_probe ( struct platform_device * pdev )
{
2019-04-09 20:23:58 +03:00
struct device * dev = & pdev - > dev ;
2011-09-28 00:35:40 +04:00
int ret ;
2019-04-09 20:23:58 +03:00
watchdog_set_drvdata ( & stmp3xxx_wdd , dev ) ;
2011-09-28 00:35:40 +04:00
stmp3xxx_wdd . timeout = clamp_t ( unsigned , heartbeat , 1 , STMP3XXX_MAX_TIMEOUT ) ;
2019-04-09 20:23:58 +03:00
stmp3xxx_wdd . parent = dev ;
2011-09-28 00:35:40 +04:00
2019-04-09 20:23:58 +03:00
ret = devm_watchdog_register_device ( dev , & stmp3xxx_wdd ) ;
2019-05-19 00:27:57 +03:00
if ( ret < 0 )
2011-09-28 00:35:40 +04:00
return ret ;
2015-12-12 17:32:15 +03:00
if ( register_reboot_notifier ( & wdt_notifier ) )
2019-04-09 20:23:58 +03:00
dev_warn ( dev , " cannot register reboot notifier \n " ) ;
2015-12-12 17:32:15 +03:00
2019-04-09 20:23:58 +03:00
dev_info ( dev , " initialized watchdog with heartbeat %ds \n " ,
stmp3xxx_wdd . timeout ) ;
2011-09-28 00:35:40 +04:00
return 0 ;
}
2023-03-04 00:37:15 +03:00
static void stmp3xxx_wdt_remove ( struct platform_device * pdev )
2011-09-28 00:35:40 +04:00
{
2015-12-12 17:32:15 +03:00
unregister_reboot_notifier ( & wdt_notifier ) ;
2011-09-28 00:35:40 +04:00
}
2014-09-23 00:55:47 +04:00
static int __maybe_unused stmp3xxx_wdt_suspend ( struct device * dev )
{
struct watchdog_device * wdd = & stmp3xxx_wdd ;
if ( watchdog_active ( wdd ) )
return wdt_stop ( wdd ) ;
return 0 ;
}
static int __maybe_unused stmp3xxx_wdt_resume ( struct device * dev )
{
struct watchdog_device * wdd = & stmp3xxx_wdd ;
if ( watchdog_active ( wdd ) )
return wdt_start ( wdd ) ;
return 0 ;
}
static SIMPLE_DEV_PM_OPS ( stmp3xxx_wdt_pm_ops ,
stmp3xxx_wdt_suspend , stmp3xxx_wdt_resume ) ;
2011-09-28 00:35:40 +04:00
static struct platform_driver stmp3xxx_wdt_driver = {
. driver = {
. name = " stmp3xxx_rtc_wdt " ,
2014-09-23 00:55:47 +04:00
. pm = & stmp3xxx_wdt_pm_ops ,
2011-09-28 00:35:40 +04:00
} ,
. probe = stmp3xxx_wdt_probe ,
2023-03-04 00:37:15 +03:00
. remove_new = stmp3xxx_wdt_remove ,
2011-09-28 00:35:40 +04:00
} ;
module_platform_driver ( stmp3xxx_wdt_driver ) ;
MODULE_DESCRIPTION ( " STMP3XXX RTC Watchdog Driver " ) ;
MODULE_LICENSE ( " GPL v2 " ) ;
2015-04-20 16:51:44 +03:00
MODULE_AUTHOR ( " Wolfram Sang <kernel@pengutronix.de> " ) ;