blob: 994c54cc68e9c930f628fc6ef0f4345181347245 [file] [log] [blame]
Fabio Estevam2671a3a2018-07-10 11:21:23 -03001// SPDX-License-Identifier: GPL-2.0
Wolfram Sangde6303a2011-09-27 22:35:40 +02002/*
3 * Watchdog driver for the RTC based watchdog in STMP3xxx and i.MX23/28
4 *
Wolfram Sangcf82f522015-04-20 15:51:44 +02005 * Author: Wolfram Sang <kernel@pengutronix.de>
Wolfram Sangde6303a2011-09-27 22:35:40 +02006 *
7 * Copyright (C) 2011-12 Wolfram Sang, Pengutronix
Wolfram Sangde6303a2011-09-27 22:35:40 +02008 */
Wolfram Sangde6303a2011-09-27 22:35:40 +02009#include <linux/kernel.h>
10#include <linux/module.h>
Wolfram Sangde6303a2011-09-27 22:35:40 +020011#include <linux/watchdog.h>
12#include <linux/platform_device.h>
13#include <linux/stmp3xxx_rtc_wdt.h>
Harald Geyer8d2fa172015-12-12 14:32:15 +000014#include <linux/notifier.h>
15#include <linux/reboot.h>
Wolfram Sangde6303a2011-09-27 22:35:40 +020016
17#define WDOG_TICK_RATE 1000 /* 1 kHz clock */
18#define STMP3XXX_DEFAULT_TIMEOUT 19
19#define STMP3XXX_MAX_TIMEOUT (UINT_MAX / WDOG_TICK_RATE)
20
21static int heartbeat = STMP3XXX_DEFAULT_TIMEOUT;
22module_param(heartbeat, uint, 0);
23MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat period in seconds from 1 to "
24 __MODULE_STRING(STMP3XXX_MAX_TIMEOUT) ", default "
25 __MODULE_STRING(STMP3XXX_DEFAULT_TIMEOUT));
26
27static int wdt_start(struct watchdog_device *wdd)
28{
29 struct device *dev = watchdog_get_drvdata(wdd);
Jingoo Hanbc8fdfb2013-07-30 19:58:51 +090030 struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);
Wolfram Sangde6303a2011-09-27 22:35:40 +020031
32 pdata->wdt_set_timeout(dev->parent, wdd->timeout * WDOG_TICK_RATE);
33 return 0;
34}
35
36static int wdt_stop(struct watchdog_device *wdd)
37{
38 struct device *dev = watchdog_get_drvdata(wdd);
Jingoo Hanbc8fdfb2013-07-30 19:58:51 +090039 struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);
Wolfram Sangde6303a2011-09-27 22:35:40 +020040
41 pdata->wdt_set_timeout(dev->parent, 0);
42 return 0;
43}
44
45static int wdt_set_timeout(struct watchdog_device *wdd, unsigned new_timeout)
46{
47 wdd->timeout = new_timeout;
48 return wdt_start(wdd);
49}
50
51static const struct watchdog_info stmp3xxx_wdt_ident = {
52 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
53 .identity = "STMP3XXX RTC Watchdog",
54};
55
56static const struct watchdog_ops stmp3xxx_wdt_ops = {
57 .owner = THIS_MODULE,
58 .start = wdt_start,
59 .stop = wdt_stop,
60 .set_timeout = wdt_set_timeout,
61};
62
63static struct watchdog_device stmp3xxx_wdd = {
64 .info = &stmp3xxx_wdt_ident,
65 .ops = &stmp3xxx_wdt_ops,
66 .min_timeout = 1,
67 .max_timeout = STMP3XXX_MAX_TIMEOUT,
68 .status = WATCHDOG_NOWAYOUT_INIT_STATUS,
69};
70
Harald Geyer8d2fa172015-12-12 14:32:15 +000071static int wdt_notify_sys(struct notifier_block *nb, unsigned long code,
72 void *unused)
73{
Harald Geyer8d2fa172015-12-12 14:32:15 +000074 switch (code) {
75 case SYS_DOWN: /* keep enabled, system might crash while going down */
76 break;
77 case SYS_HALT: /* allow the system to actually halt */
78 case SYS_POWER_OFF:
79 wdt_stop(&stmp3xxx_wdd);
80 break;
81 }
82
83 return NOTIFY_DONE;
84}
85
86static struct notifier_block wdt_notifier = {
87 .notifier_call = wdt_notify_sys,
88};
89
Wolfram Sangde6303a2011-09-27 22:35:40 +020090static int stmp3xxx_wdt_probe(struct platform_device *pdev)
91{
92 int ret;
93
94 watchdog_set_drvdata(&stmp3xxx_wdd, &pdev->dev);
95
96 stmp3xxx_wdd.timeout = clamp_t(unsigned, heartbeat, 1, STMP3XXX_MAX_TIMEOUT);
Pratyush Anand65518812015-08-20 14:05:01 +053097 stmp3xxx_wdd.parent = &pdev->dev;
Wolfram Sangde6303a2011-09-27 22:35:40 +020098
99 ret = watchdog_register_device(&stmp3xxx_wdd);
100 if (ret < 0) {
101 dev_err(&pdev->dev, "cannot register watchdog device\n");
102 return ret;
103 }
104
Harald Geyer8d2fa172015-12-12 14:32:15 +0000105 if (register_reboot_notifier(&wdt_notifier))
106 dev_warn(&pdev->dev, "cannot register reboot notifier\n");
107
Wolfram Sangde6303a2011-09-27 22:35:40 +0200108 dev_info(&pdev->dev, "initialized watchdog with heartbeat %ds\n",
109 stmp3xxx_wdd.timeout);
110 return 0;
111}
112
113static int stmp3xxx_wdt_remove(struct platform_device *pdev)
114{
Harald Geyer8d2fa172015-12-12 14:32:15 +0000115 unregister_reboot_notifier(&wdt_notifier);
Wolfram Sangde6303a2011-09-27 22:35:40 +0200116 watchdog_unregister_device(&stmp3xxx_wdd);
117 return 0;
118}
119
Janusz Uzycki3281b852014-09-22 22:55:47 +0200120static int __maybe_unused stmp3xxx_wdt_suspend(struct device *dev)
121{
122 struct watchdog_device *wdd = &stmp3xxx_wdd;
123
124 if (watchdog_active(wdd))
125 return wdt_stop(wdd);
126
127 return 0;
128}
129
130static int __maybe_unused stmp3xxx_wdt_resume(struct device *dev)
131{
132 struct watchdog_device *wdd = &stmp3xxx_wdd;
133
134 if (watchdog_active(wdd))
135 return wdt_start(wdd);
136
137 return 0;
138}
139
140static SIMPLE_DEV_PM_OPS(stmp3xxx_wdt_pm_ops,
141 stmp3xxx_wdt_suspend, stmp3xxx_wdt_resume);
142
Wolfram Sangde6303a2011-09-27 22:35:40 +0200143static struct platform_driver stmp3xxx_wdt_driver = {
144 .driver = {
145 .name = "stmp3xxx_rtc_wdt",
Janusz Uzycki3281b852014-09-22 22:55:47 +0200146 .pm = &stmp3xxx_wdt_pm_ops,
Wolfram Sangde6303a2011-09-27 22:35:40 +0200147 },
148 .probe = stmp3xxx_wdt_probe,
149 .remove = stmp3xxx_wdt_remove,
150};
151module_platform_driver(stmp3xxx_wdt_driver);
152
153MODULE_DESCRIPTION("STMP3XXX RTC Watchdog Driver");
154MODULE_LICENSE("GPL v2");
Wolfram Sangcf82f522015-04-20 15:51:44 +0200155MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");