blob: 355e428c0b99f1b82b15d05a5c13a1f85c02c5bd [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Timo Kokkonen80e45b12009-03-27 16:42:17 +02002/*
3 * Copyright (C) Nokia Corporation
4 *
5 * Written by Timo Kokkonen <timo.t.kokkonen at nokia.com>
Timo Kokkonen80e45b12009-03-27 16:42:17 +02006 */
7
8#include <linux/module.h>
9#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Timo Kokkonen80e45b12009-03-27 16:42:17 +020011#include <linux/kernel.h>
Timo Kokkonen80e45b12009-03-27 16:42:17 +020012#include <linux/watchdog.h>
13#include <linux/platform_device.h>
Wolfram Sanga2054252017-08-14 18:34:24 +020014#include <linux/mfd/twl.h>
Timo Kokkonen80e45b12009-03-27 16:42:17 +020015
16#define TWL4030_WATCHDOG_CFG_REG_OFFS 0x3
17
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010018static bool nowayout = WATCHDOG_NOWAYOUT;
19module_param(nowayout, bool, 0);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020020MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
21 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
22
23static int twl4030_wdt_write(unsigned char val)
24{
Peter Ujfalusi2bc3f622012-11-13 10:40:22 +010025 return twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER, val,
Timo Kokkonen80e45b12009-03-27 16:42:17 +020026 TWL4030_WATCHDOG_CFG_REG_OFFS);
27}
28
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030029static int twl4030_wdt_start(struct watchdog_device *wdt)
Timo Kokkonen80e45b12009-03-27 16:42:17 +020030{
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030031 return twl4030_wdt_write(wdt->timeout + 1);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020032}
33
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030034static int twl4030_wdt_stop(struct watchdog_device *wdt)
Timo Kokkonen80e45b12009-03-27 16:42:17 +020035{
36 return twl4030_wdt_write(0);
37}
38
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030039static int twl4030_wdt_set_timeout(struct watchdog_device *wdt,
40 unsigned int timeout)
Timo Kokkonen80e45b12009-03-27 16:42:17 +020041{
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030042 wdt->timeout = timeout;
Timo Kokkonen80e45b12009-03-27 16:42:17 +020043 return 0;
44}
45
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030046static const struct watchdog_info twl4030_wdt_info = {
Tony Lindgrenfb1cbea2014-10-14 12:25:19 -070047 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030048 .identity = "TWL4030 Watchdog",
49};
Timo Kokkonen80e45b12009-03-27 16:42:17 +020050
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030051static const struct watchdog_ops twl4030_wdt_ops = {
Timo Kokkonen80e45b12009-03-27 16:42:17 +020052 .owner = THIS_MODULE,
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030053 .start = twl4030_wdt_start,
54 .stop = twl4030_wdt_stop,
55 .set_timeout = twl4030_wdt_set_timeout,
Timo Kokkonen80e45b12009-03-27 16:42:17 +020056};
57
Bill Pemberton2d991a12012-11-19 13:21:41 -050058static int twl4030_wdt_probe(struct platform_device *pdev)
Timo Kokkonen80e45b12009-03-27 16:42:17 +020059{
Guenter Roeckb42488b2019-04-10 09:27:46 -070060 struct device *dev = &pdev->dev;
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030061 struct watchdog_device *wdt;
Timo Kokkonen80e45b12009-03-27 16:42:17 +020062
Guenter Roeckb42488b2019-04-10 09:27:46 -070063 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020064 if (!wdt)
65 return -ENOMEM;
66
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030067 wdt->info = &twl4030_wdt_info;
68 wdt->ops = &twl4030_wdt_ops;
69 wdt->status = 0;
70 wdt->timeout = 30;
71 wdt->min_timeout = 1;
72 wdt->max_timeout = 30;
Guenter Roeckb42488b2019-04-10 09:27:46 -070073 wdt->parent = dev;
Timo Kokkonen80e45b12009-03-27 16:42:17 +020074
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030075 watchdog_set_nowayout(wdt, nowayout);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020076 platform_set_drvdata(pdev, wdt);
77
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030078 twl4030_wdt_stop(wdt);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020079
Guenter Roeckb42488b2019-04-10 09:27:46 -070080 return devm_watchdog_register_device(dev, wdt);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020081}
82
83#ifdef CONFIG_PM
84static int twl4030_wdt_suspend(struct platform_device *pdev, pm_message_t state)
85{
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030086 struct watchdog_device *wdt = platform_get_drvdata(pdev);
87 if (watchdog_active(wdt))
88 return twl4030_wdt_stop(wdt);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020089
90 return 0;
91}
92
93static int twl4030_wdt_resume(struct platform_device *pdev)
94{
Jarkko Nikulab2c4e4b2012-09-11 09:01:10 +030095 struct watchdog_device *wdt = platform_get_drvdata(pdev);
96 if (watchdog_active(wdt))
97 return twl4030_wdt_start(wdt);
Timo Kokkonen80e45b12009-03-27 16:42:17 +020098
99 return 0;
100}
101#else
102#define twl4030_wdt_suspend NULL
103#define twl4030_wdt_resume NULL
104#endif
105
Aaro Koskinen8899b8d2012-12-23 22:03:37 +0200106static const struct of_device_id twl_wdt_of_match[] = {
107 { .compatible = "ti,twl4030-wdt", },
108 { },
109};
110MODULE_DEVICE_TABLE(of, twl_wdt_of_match);
111
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200112static struct platform_driver twl4030_wdt_driver = {
113 .probe = twl4030_wdt_probe,
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200114 .suspend = twl4030_wdt_suspend,
115 .resume = twl4030_wdt_resume,
116 .driver = {
Aaro Koskinen8899b8d2012-12-23 22:03:37 +0200117 .name = "twl4030_wdt",
118 .of_match_table = twl_wdt_of_match,
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200119 },
120};
121
Axel Linb8ec6112011-11-29 13:56:27 +0800122module_platform_driver(twl4030_wdt_driver);
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200123
124MODULE_AUTHOR("Nokia Corporation");
125MODULE_LICENSE("GPL");
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200126MODULE_ALIAS("platform:twl4030_wdt");
127