2 * MOXA ART SoCs watchdog driver.
4 * Copyright (C) 2013 Jonas Jensen
6 * Jonas Jensen <jonas.jensen@gmail.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
13 #include <linux/clk.h>
15 #include <linux/module.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
19 #include <linux/watchdog.h>
20 #include <linux/moduleparam.h>
24 #define REG_ENABLE 0xC
26 struct moxart_wdt_dev {
27 struct watchdog_device dev;
29 unsigned int clock_frequency;
34 static int moxart_wdt_stop(struct watchdog_device *wdt_dev)
36 struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
38 writel(0, moxart_wdt->base + REG_ENABLE);
43 static int moxart_wdt_start(struct watchdog_device *wdt_dev)
45 struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
47 writel(moxart_wdt->clock_frequency * wdt_dev->timeout,
48 moxart_wdt->base + REG_COUNT);
49 writel(0x5ab9, moxart_wdt->base + REG_MODE);
50 writel(0x03, moxart_wdt->base + REG_ENABLE);
55 static int moxart_wdt_set_timeout(struct watchdog_device *wdt_dev,
58 wdt_dev->timeout = timeout;
63 static const struct watchdog_info moxart_wdt_info = {
64 .identity = "moxart-wdt",
65 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
69 static const struct watchdog_ops moxart_wdt_ops = {
71 .start = moxart_wdt_start,
72 .stop = moxart_wdt_stop,
73 .set_timeout = moxart_wdt_set_timeout,
76 static int moxart_wdt_probe(struct platform_device *pdev)
78 struct moxart_wdt_dev *moxart_wdt;
79 struct device *dev = &pdev->dev;
80 struct device_node *node = dev->of_node;
84 unsigned int max_timeout;
85 bool nowayout = WATCHDOG_NOWAYOUT;
87 moxart_wdt = devm_kzalloc(dev, sizeof(*moxart_wdt), GFP_KERNEL);
91 platform_set_drvdata(pdev, moxart_wdt);
93 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
94 moxart_wdt->base = devm_ioremap_resource(dev, res);
95 if (IS_ERR(moxart_wdt->base))
96 return PTR_ERR(moxart_wdt->base);
98 clk = of_clk_get(node, 0);
100 pr_err("%s: of_clk_get failed\n", __func__);
104 moxart_wdt->clock_frequency = clk_get_rate(clk);
105 if (moxart_wdt->clock_frequency == 0) {
106 pr_err("%s: incorrect clock frequency\n", __func__);
110 max_timeout = UINT_MAX / moxart_wdt->clock_frequency;
112 moxart_wdt->dev.info = &moxart_wdt_info;
113 moxart_wdt->dev.ops = &moxart_wdt_ops;
114 moxart_wdt->dev.timeout = max_timeout;
115 moxart_wdt->dev.min_timeout = 1;
116 moxart_wdt->dev.max_timeout = max_timeout;
117 moxart_wdt->dev.parent = dev;
119 watchdog_init_timeout(&moxart_wdt->dev, heartbeat, dev);
120 watchdog_set_nowayout(&moxart_wdt->dev, nowayout);
122 watchdog_set_drvdata(&moxart_wdt->dev, moxart_wdt);
124 err = watchdog_register_device(&moxart_wdt->dev);
128 dev_dbg(dev, "Watchdog enabled (heartbeat=%d sec, nowayout=%d)\n",
129 moxart_wdt->dev.timeout, nowayout);
134 static int moxart_wdt_remove(struct platform_device *pdev)
136 struct moxart_wdt_dev *moxart_wdt = platform_get_drvdata(pdev);
138 moxart_wdt_stop(&moxart_wdt->dev);
139 watchdog_unregister_device(&moxart_wdt->dev);
144 static const struct of_device_id moxart_watchdog_match[] = {
145 { .compatible = "moxa,moxart-watchdog" },
149 static struct platform_driver moxart_wdt_driver = {
150 .probe = moxart_wdt_probe,
151 .remove = moxart_wdt_remove,
153 .name = "moxart-watchdog",
154 .owner = THIS_MODULE,
155 .of_match_table = moxart_watchdog_match,
158 module_platform_driver(moxart_wdt_driver);
160 module_param(heartbeat, int, 0);
161 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds");
163 MODULE_DESCRIPTION("MOXART watchdog driver");
164 MODULE_LICENSE("GPL");
165 MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");