xref: /linux/drivers/watchdog/stmp3xxx_rtc_wdt.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * Watchdog driver for the RTC based watchdog in STMP3xxx and i.MX23/28
3  *
4  * Author: Wolfram Sang <kernel@pengutronix.de>
5  *
6  * Copyright (C) 2011-12 Wolfram Sang, Pengutronix
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  */
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/watchdog.h>
15 #include <linux/platform_device.h>
16 #include <linux/stmp3xxx_rtc_wdt.h>
17 
18 #define WDOG_TICK_RATE 1000 /* 1 kHz clock */
19 #define STMP3XXX_DEFAULT_TIMEOUT 19
20 #define STMP3XXX_MAX_TIMEOUT (UINT_MAX / WDOG_TICK_RATE)
21 
22 static int heartbeat = STMP3XXX_DEFAULT_TIMEOUT;
23 module_param(heartbeat, uint, 0);
24 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat period in seconds from 1 to "
25 		 __MODULE_STRING(STMP3XXX_MAX_TIMEOUT) ", default "
26 		 __MODULE_STRING(STMP3XXX_DEFAULT_TIMEOUT));
27 
28 static int wdt_start(struct watchdog_device *wdd)
29 {
30 	struct device *dev = watchdog_get_drvdata(wdd);
31 	struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);
32 
33 	pdata->wdt_set_timeout(dev->parent, wdd->timeout * WDOG_TICK_RATE);
34 	return 0;
35 }
36 
37 static int wdt_stop(struct watchdog_device *wdd)
38 {
39 	struct device *dev = watchdog_get_drvdata(wdd);
40 	struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);
41 
42 	pdata->wdt_set_timeout(dev->parent, 0);
43 	return 0;
44 }
45 
46 static int wdt_set_timeout(struct watchdog_device *wdd, unsigned new_timeout)
47 {
48 	wdd->timeout = new_timeout;
49 	return wdt_start(wdd);
50 }
51 
52 static const struct watchdog_info stmp3xxx_wdt_ident = {
53 	.options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
54 	.identity = "STMP3XXX RTC Watchdog",
55 };
56 
57 static const struct watchdog_ops stmp3xxx_wdt_ops = {
58 	.owner = THIS_MODULE,
59 	.start = wdt_start,
60 	.stop = wdt_stop,
61 	.set_timeout = wdt_set_timeout,
62 };
63 
64 static struct watchdog_device stmp3xxx_wdd = {
65 	.info = &stmp3xxx_wdt_ident,
66 	.ops = &stmp3xxx_wdt_ops,
67 	.min_timeout = 1,
68 	.max_timeout = STMP3XXX_MAX_TIMEOUT,
69 	.status = WATCHDOG_NOWAYOUT_INIT_STATUS,
70 };
71 
72 static int stmp3xxx_wdt_probe(struct platform_device *pdev)
73 {
74 	int ret;
75 
76 	watchdog_set_drvdata(&stmp3xxx_wdd, &pdev->dev);
77 
78 	stmp3xxx_wdd.timeout = clamp_t(unsigned, heartbeat, 1, STMP3XXX_MAX_TIMEOUT);
79 	stmp3xxx_wdd.parent = &pdev->dev;
80 
81 	ret = watchdog_register_device(&stmp3xxx_wdd);
82 	if (ret < 0) {
83 		dev_err(&pdev->dev, "cannot register watchdog device\n");
84 		return ret;
85 	}
86 
87 	dev_info(&pdev->dev, "initialized watchdog with heartbeat %ds\n",
88 			stmp3xxx_wdd.timeout);
89 	return 0;
90 }
91 
92 static int stmp3xxx_wdt_remove(struct platform_device *pdev)
93 {
94 	watchdog_unregister_device(&stmp3xxx_wdd);
95 	return 0;
96 }
97 
98 static int __maybe_unused stmp3xxx_wdt_suspend(struct device *dev)
99 {
100 	struct watchdog_device *wdd = &stmp3xxx_wdd;
101 
102 	if (watchdog_active(wdd))
103 		return wdt_stop(wdd);
104 
105 	return 0;
106 }
107 
108 static int __maybe_unused stmp3xxx_wdt_resume(struct device *dev)
109 {
110 	struct watchdog_device *wdd = &stmp3xxx_wdd;
111 
112 	if (watchdog_active(wdd))
113 		return wdt_start(wdd);
114 
115 	return 0;
116 }
117 
118 static SIMPLE_DEV_PM_OPS(stmp3xxx_wdt_pm_ops,
119 			 stmp3xxx_wdt_suspend, stmp3xxx_wdt_resume);
120 
121 static struct platform_driver stmp3xxx_wdt_driver = {
122 	.driver = {
123 		.name = "stmp3xxx_rtc_wdt",
124 		.pm = &stmp3xxx_wdt_pm_ops,
125 	},
126 	.probe = stmp3xxx_wdt_probe,
127 	.remove = stmp3xxx_wdt_remove,
128 };
129 module_platform_driver(stmp3xxx_wdt_driver);
130 
131 MODULE_DESCRIPTION("STMP3XXX RTC Watchdog Driver");
132 MODULE_LICENSE("GPL v2");
133 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
134