summaryrefslogtreecommitdiffstats
path: root/drivers/watchdog/stmp3xxx_rtc_wdt.c
blob: a62b1b6decf472ac2b35cf7e663da7e704f9e005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Watchdog driver for the RTC based watchdog in STMP3xxx and i.MX23/28
 *
 * Author: Wolfram Sang <w.sang@pengutronix.de>
 *
 * Copyright (C) 2011-12 Wolfram Sang, Pengutronix
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/watchdog.h>
#include <linux/platform_device.h>
#include <linux/stmp3xxx_rtc_wdt.h>

#define WDOG_TICK_RATE 1000 /* 1 kHz clock */
#define STMP3XXX_DEFAULT_TIMEOUT 19
#define STMP3XXX_MAX_TIMEOUT (UINT_MAX / WDOG_TICK_RATE)

static int heartbeat = STMP3XXX_DEFAULT_TIMEOUT;
module_param(heartbeat, uint, 0);
MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat period in seconds from 1 to "
		 __MODULE_STRING(STMP3XXX_MAX_TIMEOUT) ", default "
		 __MODULE_STRING(STMP3XXX_DEFAULT_TIMEOUT));

static int wdt_start(struct watchdog_device *wdd)
{
	struct device *dev = watchdog_get_drvdata(wdd);
	struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);

	pdata->wdt_set_timeout(dev->parent, wdd->timeout * WDOG_TICK_RATE);
	return 0;
}

static int wdt_stop(struct watchdog_device *wdd)
{
	struct device *dev = watchdog_get_drvdata(wdd);
	struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);

	pdata->wdt_set_timeout(dev->parent, 0);
	return 0;
}

static int wdt_set_timeout(struct watchdog_device *wdd, unsigned new_timeout)
{
	wdd->timeout = new_timeout;
	return wdt_start(wdd);
}

static const struct watchdog_info stmp3xxx_wdt_ident = {
	.options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
	.identity = "STMP3XXX RTC Watchdog",
};

static const struct watchdog_ops stmp3xxx_wdt_ops = {
	.owner = THIS_MODULE,
	.start = wdt_start,
	.stop = wdt_stop,
	.set_timeout = wdt_set_timeout,
};

static struct watchdog_device stmp3xxx_wdd = {
	.info = &stmp3xxx_wdt_ident,
	.ops = &stmp3xxx_wdt_ops,
	.min_timeout = 1,
	.max_timeout = STMP3XXX_MAX_TIMEOUT,
	.status = WATCHDOG_NOWAYOUT_INIT_STATUS,
};

static int stmp3xxx_wdt_probe(struct platform_device *pdev)
{
	int ret;

	watchdog_set_drvdata(&stmp3xxx_wdd, &pdev->dev);

	stmp3xxx_wdd.timeout = clamp_t(unsigned, heartbeat, 1, STMP3XXX_MAX_TIMEOUT);

	ret = watchdog_register_device(&stmp3xxx_wdd);
	if (ret < 0) {
		dev_err(&pdev->dev, "cannot register watchdog device\n");
		return ret;
	}

	dev_info(&pdev->dev, "initialized watchdog with heartbeat %ds\n",
			stmp3xxx_wdd.timeout);
	return 0;
}

static int stmp3xxx_wdt_remove(struct platform_device *pdev)
{
	watchdog_unregister_device(&stmp3xxx_wdd);
	return 0;
}

static int __maybe_unused stmp3xxx_wdt_suspend(struct device *dev)
{
	struct watchdog_device *wdd = &stmp3xxx_wdd;

	if (watchdog_active(wdd))
		return wdt_stop(wdd);

	return 0;
}

static int __maybe_unused stmp3xxx_wdt_resume(struct device *dev)
{
	struct watchdog_device *wdd = &stmp3xxx_wdd;

	if (watchdog_active(wdd))
		return wdt_start(wdd);

	return 0;
}

static SIMPLE_DEV_PM_OPS(stmp3xxx_wdt_pm_ops,
			 stmp3xxx_wdt_suspend, stmp3xxx_wdt_resume);

static struct platform_driver stmp3xxx_wdt_driver = {
	.driver = {
		.name = "stmp3xxx_rtc_wdt",
		.pm = &stmp3xxx_wdt_pm_ops,
	},
	.probe = stmp3xxx_wdt_probe,
	.remove = stmp3xxx_wdt_remove,
};
module_platform_driver(stmp3xxx_wdt_driver);

MODULE_DESCRIPTION("STMP3XXX RTC Watchdog Driver");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");