2019-05-27 09:55:00 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
2010-12-05 23:08:22 +03:00
/*
* Copyright ( C ) 2010 , Paul Cercueil < paul @ crapouillou . net >
* JZ4740 Watchdog driver
*/
2019-06-07 19:24:26 +03:00
# include <linux/mfd/ingenic-tcu.h>
2019-10-23 20:47:13 +03:00
# include <linux/mfd/syscon.h>
2010-12-05 23:08:22 +03:00
# include <linux/module.h>
# include <linux/moduleparam.h>
# include <linux/types.h>
# include <linux/kernel.h>
# include <linux/watchdog.h>
# include <linux/platform_device.h>
# include <linux/io.h>
# include <linux/device.h>
# include <linux/clk.h>
# include <linux/slab.h>
2012-01-26 14:10:45 +04:00
# include <linux/err.h>
2015-02-03 13:25:48 +03:00
# include <linux/of.h>
2019-10-23 20:47:13 +03:00
# include <linux/regmap.h>
2010-12-05 23:08:22 +03:00
# define DEFAULT_HEARTBEAT 5
# define MAX_HEARTBEAT 2048
2012-01-26 14:10:45 +04:00
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 ) " ) " ) ;
2010-12-05 23:08:22 +03:00
2012-01-26 14:10:45 +04:00
static unsigned int heartbeat = DEFAULT_HEARTBEAT ;
module_param ( heartbeat , uint , 0 ) ;
MODULE_PARM_DESC ( heartbeat ,
" Watchdog heartbeat period in seconds from 1 to "
__MODULE_STRING ( MAX_HEARTBEAT ) " , default "
__MODULE_STRING ( DEFAULT_HEARTBEAT ) ) ;
2010-12-05 23:08:22 +03:00
2012-01-26 14:10:45 +04:00
struct jz4740_wdt_drvdata {
struct watchdog_device wdt ;
2019-10-23 20:47:13 +03:00
struct regmap * map ;
2019-10-23 20:47:12 +03:00
struct clk * clk ;
unsigned long clk_rate ;
2012-01-26 14:10:45 +04:00
} ;
2010-12-05 23:08:22 +03:00
2012-01-26 14:10:45 +04:00
static int jz4740_wdt_ping ( struct watchdog_device * wdt_dev )
2010-12-05 23:08:22 +03:00
{
2012-01-26 14:10:45 +04:00
struct jz4740_wdt_drvdata * drvdata = watchdog_get_drvdata ( wdt_dev ) ;
2019-10-23 20:47:13 +03:00
regmap_write ( drvdata - > map , TCU_REG_WDT_TCNT , 0 ) ;
2012-01-26 14:10:45 +04:00
return 0 ;
2010-12-05 23:08:22 +03:00
}
2012-01-26 14:10:45 +04:00
static int jz4740_wdt_set_timeout ( struct watchdog_device * wdt_dev ,
unsigned int new_timeout )
2010-12-05 23:08:22 +03:00
{
2012-01-26 14:10:45 +04:00
struct jz4740_wdt_drvdata * drvdata = watchdog_get_drvdata ( wdt_dev ) ;
2019-10-23 20:47:12 +03:00
u16 timeout_value = ( u16 ) ( drvdata - > clk_rate * new_timeout ) ;
2019-10-23 20:47:13 +03:00
unsigned int tcer ;
2010-12-05 23:08:22 +03:00
2019-10-23 20:47:13 +03:00
regmap_read ( drvdata - > map , TCU_REG_WDT_TCER , & tcer ) ;
regmap_write ( drvdata - > map , TCU_REG_WDT_TCER , 0 ) ;
2010-12-05 23:08:22 +03:00
2019-10-23 20:47:13 +03:00
regmap_write ( drvdata - > map , TCU_REG_WDT_TDR , timeout_value ) ;
regmap_write ( drvdata - > map , TCU_REG_WDT_TCNT , 0 ) ;
2010-12-05 23:08:22 +03:00
2019-06-07 19:24:27 +03:00
if ( tcer & TCU_WDT_TCER_TCEN )
2019-10-23 20:47:13 +03:00
regmap_write ( drvdata - > map , TCU_REG_WDT_TCER , TCU_WDT_TCER_TCEN ) ;
2010-12-05 23:08:22 +03:00
2012-02-29 23:20:58 +04:00
wdt_dev - > timeout = new_timeout ;
2012-01-26 14:10:45 +04:00
return 0 ;
2010-12-05 23:08:22 +03:00
}
2012-01-26 14:10:45 +04:00
static int jz4740_wdt_start ( struct watchdog_device * wdt_dev )
2010-12-05 23:08:22 +03:00
{
2019-06-07 19:24:27 +03:00
struct jz4740_wdt_drvdata * drvdata = watchdog_get_drvdata ( wdt_dev ) ;
2019-10-23 20:47:13 +03:00
unsigned int tcer ;
2019-10-23 20:47:12 +03:00
int ret ;
2019-06-07 19:24:27 +03:00
2019-10-23 20:47:12 +03:00
ret = clk_prepare_enable ( drvdata - > clk ) ;
if ( ret )
return ret ;
2019-10-23 20:47:13 +03:00
regmap_read ( drvdata - > map , TCU_REG_WDT_TCER , & tcer ) ;
2019-06-07 19:24:27 +03:00
2012-01-26 14:10:45 +04:00
jz4740_wdt_set_timeout ( wdt_dev , wdt_dev - > timeout ) ;
2010-12-05 23:08:22 +03:00
2019-06-07 19:24:27 +03:00
/* Start watchdog if it wasn't started already */
if ( ! ( tcer & TCU_WDT_TCER_TCEN ) )
2019-10-23 20:47:13 +03:00
regmap_write ( drvdata - > map , TCU_REG_WDT_TCER , TCU_WDT_TCER_TCEN ) ;
2019-06-07 19:24:27 +03:00
2012-01-26 14:10:45 +04:00
return 0 ;
2010-12-05 23:08:22 +03:00
}
2012-01-26 14:10:45 +04:00
static int jz4740_wdt_stop ( struct watchdog_device * wdt_dev )
2010-12-05 23:08:22 +03:00
{
2012-01-26 14:10:45 +04:00
struct jz4740_wdt_drvdata * drvdata = watchdog_get_drvdata ( wdt_dev ) ;
2011-01-16 22:05:39 +03:00
2019-10-23 20:47:13 +03:00
regmap_write ( drvdata - > map , TCU_REG_WDT_TCER , 0 ) ;
2019-10-23 20:47:12 +03:00
clk_disable_unprepare ( drvdata - > clk ) ;
2010-12-05 23:08:22 +03:00
2012-01-26 14:10:45 +04:00
return 0 ;
2010-12-05 23:08:22 +03:00
}
2018-05-10 21:47:46 +03:00
static int jz4740_wdt_restart ( struct watchdog_device * wdt_dev ,
unsigned long action , void * data )
{
wdt_dev - > timeout = 0 ;
jz4740_wdt_start ( wdt_dev ) ;
return 0 ;
}
2012-01-26 14:10:45 +04:00
static const struct watchdog_info jz4740_wdt_info = {
. options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE ,
2010-12-05 23:08:22 +03:00
. identity = " jz4740 Watchdog " ,
} ;
2012-01-26 14:10:45 +04:00
static const struct watchdog_ops jz4740_wdt_ops = {
2010-12-05 23:08:22 +03:00
. owner = THIS_MODULE ,
2012-01-26 14:10:45 +04:00
. start = jz4740_wdt_start ,
. stop = jz4740_wdt_stop ,
. ping = jz4740_wdt_ping ,
. set_timeout = jz4740_wdt_set_timeout ,
2018-05-10 21:47:46 +03:00
. restart = jz4740_wdt_restart ,
2010-12-05 23:08:22 +03:00
} ;
2015-02-03 13:25:48 +03:00
# ifdef CONFIG_OF
static const struct of_device_id jz4740_wdt_of_matches [ ] = {
{ . compatible = " ingenic,jz4740-watchdog " , } ,
2017-09-15 22:20:19 +03:00
{ . compatible = " ingenic,jz4780-watchdog " , } ,
2015-02-03 13:25:48 +03:00
{ /* sentinel */ }
} ;
2016-11-11 03:02:20 +03:00
MODULE_DEVICE_TABLE ( of , jz4740_wdt_of_matches ) ;
2015-02-03 13:25:48 +03:00
# endif
2012-11-19 22:21:41 +04:00
static int jz4740_wdt_probe ( struct platform_device * pdev )
2010-12-05 23:08:22 +03:00
{
2019-04-10 19:28:01 +03:00
struct device * dev = & pdev - > dev ;
2012-01-26 14:10:45 +04:00
struct jz4740_wdt_drvdata * drvdata ;
struct watchdog_device * jz4740_wdt ;
2019-10-23 20:47:12 +03:00
long rate ;
int ret ;
2012-01-26 14:10:45 +04:00
2019-04-10 19:28:01 +03:00
drvdata = devm_kzalloc ( dev , sizeof ( struct jz4740_wdt_drvdata ) ,
2012-01-26 14:10:45 +04:00
GFP_KERNEL ) ;
2016-04-26 20:18:49 +03:00
if ( ! drvdata )
2012-01-26 14:10:45 +04:00
return - ENOMEM ;
2010-12-05 23:08:22 +03:00
2019-10-23 20:47:12 +03:00
drvdata - > clk = devm_clk_get ( & pdev - > dev , " wdt " ) ;
if ( IS_ERR ( drvdata - > clk ) ) {
dev_err ( & pdev - > dev , " cannot find WDT clock \n " ) ;
return PTR_ERR ( drvdata - > clk ) ;
}
/* Set smallest clock possible */
rate = clk_round_rate ( drvdata - > clk , 1 ) ;
if ( rate < 0 )
return rate ;
ret = clk_set_rate ( drvdata - > clk , rate ) ;
if ( ret )
return ret ;
2010-12-05 23:08:22 +03:00
2019-10-23 20:47:12 +03:00
drvdata - > clk_rate = rate ;
2012-01-26 14:10:45 +04:00
jz4740_wdt = & drvdata - > wdt ;
jz4740_wdt - > info = & jz4740_wdt_info ;
jz4740_wdt - > ops = & jz4740_wdt_ops ;
jz4740_wdt - > min_timeout = 1 ;
2019-10-23 20:47:12 +03:00
jz4740_wdt - > max_timeout = 0xffff / rate ;
jz4740_wdt - > timeout = clamp ( heartbeat ,
jz4740_wdt - > min_timeout ,
jz4740_wdt - > max_timeout ) ;
2019-04-10 19:28:01 +03:00
jz4740_wdt - > parent = dev ;
2012-01-26 14:10:45 +04:00
watchdog_set_nowayout ( jz4740_wdt , nowayout ) ;
watchdog_set_drvdata ( jz4740_wdt , drvdata ) ;
2019-10-23 20:47:13 +03:00
drvdata - > map = device_node_to_regmap ( dev - > parent - > of_node ) ;
2021-03-04 07:59:09 +03:00
if ( IS_ERR ( drvdata - > map ) ) {
2019-10-23 20:47:13 +03:00
dev_err ( dev , " regmap not found \n " ) ;
2021-03-04 07:59:09 +03:00
return PTR_ERR ( drvdata - > map ) ;
2019-10-23 20:47:13 +03:00
}
2010-12-05 23:08:22 +03:00
2019-05-19 00:27:34 +03:00
return devm_watchdog_register_device ( dev , & drvdata - > wdt ) ;
2010-12-05 23:08:22 +03:00
}
static struct platform_driver jz4740_wdt_driver = {
. probe = jz4740_wdt_probe ,
. driver = {
. name = " jz4740-wdt " ,
2015-02-03 13:25:48 +03:00
. of_match_table = of_match_ptr ( jz4740_wdt_of_matches ) ,
2010-12-05 23:08:22 +03:00
} ,
} ;
2011-11-29 09:56:27 +04:00
module_platform_driver ( jz4740_wdt_driver ) ;
2010-12-05 23:08:22 +03:00
MODULE_AUTHOR ( " Paul Cercueil <paul@crapouillou.net> " ) ;
MODULE_DESCRIPTION ( " jz4740 Watchdog Driver " ) ;
MODULE_LICENSE ( " GPL " ) ;
MODULE_ALIAS ( " platform:jz4740-wdt " ) ;