ALSA: hda/ca0132 - Call pci_iounmap() instead of iounmap()
[linux/fpc-iii.git] / drivers / watchdog / tangox_wdt.c
blobd0b53f3c0d171ebaf8d796d371c31f36aa04a837
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2015 Mans Rullgard <mans@mansr.com>
4 * SMP86xx/SMP87xx Watchdog driver
5 */
7 #include <linux/bitops.h>
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/io.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/platform_device.h>
16 #include <linux/watchdog.h>
18 #define DEFAULT_TIMEOUT 30
20 static bool nowayout = WATCHDOG_NOWAYOUT;
21 module_param(nowayout, bool, 0);
22 MODULE_PARM_DESC(nowayout,
23 "Watchdog cannot be stopped once started (default="
24 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
26 static unsigned int timeout;
27 module_param(timeout, int, 0);
28 MODULE_PARM_DESC(timeout, "Watchdog timeout");
31 * Counter counts down from programmed value. Reset asserts when
32 * the counter reaches 1.
34 #define WD_COUNTER 0
36 #define WD_CONFIG 4
37 #define WD_CONFIG_XTAL_IN BIT(0)
38 #define WD_CONFIG_DISABLE BIT(31)
40 struct tangox_wdt_device {
41 struct watchdog_device wdt;
42 void __iomem *base;
43 unsigned long clk_rate;
44 struct clk *clk;
47 static int tangox_wdt_set_timeout(struct watchdog_device *wdt,
48 unsigned int new_timeout)
50 wdt->timeout = new_timeout;
52 return 0;
55 static int tangox_wdt_start(struct watchdog_device *wdt)
57 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
58 u32 ticks;
60 ticks = 1 + wdt->timeout * dev->clk_rate;
61 writel(ticks, dev->base + WD_COUNTER);
63 return 0;
66 static int tangox_wdt_stop(struct watchdog_device *wdt)
68 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
70 writel(0, dev->base + WD_COUNTER);
72 return 0;
75 static unsigned int tangox_wdt_get_timeleft(struct watchdog_device *wdt)
77 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
78 u32 count;
80 count = readl(dev->base + WD_COUNTER);
82 if (!count)
83 return 0;
85 return (count - 1) / dev->clk_rate;
88 static const struct watchdog_info tangox_wdt_info = {
89 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
90 .identity = "tangox watchdog",
93 static int tangox_wdt_restart(struct watchdog_device *wdt,
94 unsigned long action, void *data)
96 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
98 writel(1, dev->base + WD_COUNTER);
100 return 0;
103 static const struct watchdog_ops tangox_wdt_ops = {
104 .start = tangox_wdt_start,
105 .stop = tangox_wdt_stop,
106 .set_timeout = tangox_wdt_set_timeout,
107 .get_timeleft = tangox_wdt_get_timeleft,
108 .restart = tangox_wdt_restart,
111 static int tangox_wdt_probe(struct platform_device *pdev)
113 struct tangox_wdt_device *dev;
114 struct resource *res;
115 u32 config;
116 int err;
118 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
119 if (!dev)
120 return -ENOMEM;
122 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
123 dev->base = devm_ioremap_resource(&pdev->dev, res);
124 if (IS_ERR(dev->base))
125 return PTR_ERR(dev->base);
127 dev->clk = devm_clk_get(&pdev->dev, NULL);
128 if (IS_ERR(dev->clk))
129 return PTR_ERR(dev->clk);
131 err = clk_prepare_enable(dev->clk);
132 if (err)
133 return err;
135 dev->clk_rate = clk_get_rate(dev->clk);
136 if (!dev->clk_rate) {
137 err = -EINVAL;
138 goto err;
141 dev->wdt.parent = &pdev->dev;
142 dev->wdt.info = &tangox_wdt_info;
143 dev->wdt.ops = &tangox_wdt_ops;
144 dev->wdt.timeout = DEFAULT_TIMEOUT;
145 dev->wdt.min_timeout = 1;
146 dev->wdt.max_hw_heartbeat_ms = (U32_MAX - 1) / dev->clk_rate;
148 watchdog_init_timeout(&dev->wdt, timeout, &pdev->dev);
149 watchdog_set_nowayout(&dev->wdt, nowayout);
150 watchdog_set_drvdata(&dev->wdt, dev);
153 * Deactivate counter if disable bit is set to avoid
154 * accidental reset.
156 config = readl(dev->base + WD_CONFIG);
157 if (config & WD_CONFIG_DISABLE)
158 writel(0, dev->base + WD_COUNTER);
160 writel(WD_CONFIG_XTAL_IN, dev->base + WD_CONFIG);
163 * Mark as active and restart with configured timeout if
164 * already running.
166 if (readl(dev->base + WD_COUNTER)) {
167 set_bit(WDOG_HW_RUNNING, &dev->wdt.status);
168 tangox_wdt_start(&dev->wdt);
171 watchdog_set_restart_priority(&dev->wdt, 128);
173 err = watchdog_register_device(&dev->wdt);
174 if (err)
175 goto err;
177 platform_set_drvdata(pdev, dev);
179 dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
181 return 0;
183 err:
184 clk_disable_unprepare(dev->clk);
185 return err;
188 static int tangox_wdt_remove(struct platform_device *pdev)
190 struct tangox_wdt_device *dev = platform_get_drvdata(pdev);
192 tangox_wdt_stop(&dev->wdt);
193 clk_disable_unprepare(dev->clk);
195 watchdog_unregister_device(&dev->wdt);
197 return 0;
200 static const struct of_device_id tangox_wdt_dt_ids[] = {
201 { .compatible = "sigma,smp8642-wdt" },
202 { .compatible = "sigma,smp8759-wdt" },
205 MODULE_DEVICE_TABLE(of, tangox_wdt_dt_ids);
207 static struct platform_driver tangox_wdt_driver = {
208 .probe = tangox_wdt_probe,
209 .remove = tangox_wdt_remove,
210 .driver = {
211 .name = "tangox-wdt",
212 .of_match_table = tangox_wdt_dt_ids,
216 module_platform_driver(tangox_wdt_driver);
218 MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
219 MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
220 MODULE_LICENSE("GPL");