1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) Nokia Corporation
5 * Written by Timo Kokkonen <timo.t.kokkonen at nokia.com>
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/watchdog.h>
14 #include <linux/platform_device.h>
15 #include <linux/mfd/twl.h>
17 #define TWL4030_WATCHDOG_CFG_REG_OFFS 0x3
19 static bool nowayout
= WATCHDOG_NOWAYOUT
;
20 module_param(nowayout
, bool, 0);
21 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started "
22 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
24 static int twl4030_wdt_write(unsigned char val
)
26 return twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER
, val
,
27 TWL4030_WATCHDOG_CFG_REG_OFFS
);
30 static int twl4030_wdt_start(struct watchdog_device
*wdt
)
32 return twl4030_wdt_write(wdt
->timeout
+ 1);
35 static int twl4030_wdt_stop(struct watchdog_device
*wdt
)
37 return twl4030_wdt_write(0);
40 static int twl4030_wdt_set_timeout(struct watchdog_device
*wdt
,
43 wdt
->timeout
= timeout
;
47 static const struct watchdog_info twl4030_wdt_info
= {
48 .options
= WDIOF_SETTIMEOUT
| WDIOF_MAGICCLOSE
| WDIOF_KEEPALIVEPING
,
49 .identity
= "TWL4030 Watchdog",
52 static const struct watchdog_ops twl4030_wdt_ops
= {
54 .start
= twl4030_wdt_start
,
55 .stop
= twl4030_wdt_stop
,
56 .set_timeout
= twl4030_wdt_set_timeout
,
59 static int twl4030_wdt_probe(struct platform_device
*pdev
)
61 struct device
*dev
= &pdev
->dev
;
62 struct watchdog_device
*wdt
;
64 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
68 wdt
->info
= &twl4030_wdt_info
;
69 wdt
->ops
= &twl4030_wdt_ops
;
73 wdt
->max_timeout
= 30;
76 watchdog_set_nowayout(wdt
, nowayout
);
77 platform_set_drvdata(pdev
, wdt
);
79 twl4030_wdt_stop(wdt
);
81 return devm_watchdog_register_device(dev
, wdt
);
84 static int twl4030_wdt_suspend(struct platform_device
*pdev
, pm_message_t state
)
86 struct watchdog_device
*wdt
= platform_get_drvdata(pdev
);
87 if (watchdog_active(wdt
))
88 return twl4030_wdt_stop(wdt
);
93 static int twl4030_wdt_resume(struct platform_device
*pdev
)
95 struct watchdog_device
*wdt
= platform_get_drvdata(pdev
);
96 if (watchdog_active(wdt
))
97 return twl4030_wdt_start(wdt
);
102 static const struct of_device_id twl_wdt_of_match
[] = {
103 { .compatible
= "ti,twl4030-wdt", },
106 MODULE_DEVICE_TABLE(of
, twl_wdt_of_match
);
108 static struct platform_driver twl4030_wdt_driver
= {
109 .probe
= twl4030_wdt_probe
,
110 .suspend
= pm_ptr(twl4030_wdt_suspend
),
111 .resume
= pm_ptr(twl4030_wdt_resume
),
113 .name
= "twl4030_wdt",
114 .of_match_table
= twl_wdt_of_match
,
118 module_platform_driver(twl4030_wdt_driver
);
120 MODULE_AUTHOR("Nokia Corporation");
121 MODULE_DESCRIPTION("TWL4030 Watchdog");
122 MODULE_LICENSE("GPL");
123 MODULE_ALIAS("platform:twl4030_wdt");