Linux 4.19.133
[linux/fpc-iii.git] / drivers / watchdog / digicolor_wdt.c
bloba9e11df155b8e44e5aac99fd02db4fb5e7d21038
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Watchdog driver for Conexant Digicolor
5 * Copyright (C) 2015 Paradox Innovation Ltd.
7 */
9 #include <linux/types.h>
10 #include <linux/module.h>
11 #include <linux/io.h>
12 #include <linux/delay.h>
13 #include <linux/clk.h>
14 #include <linux/watchdog.h>
15 #include <linux/platform_device.h>
16 #include <linux/of_address.h>
18 #define TIMER_A_CONTROL 0
19 #define TIMER_A_COUNT 4
21 #define TIMER_A_ENABLE_COUNT BIT(0)
22 #define TIMER_A_ENABLE_WATCHDOG BIT(1)
24 struct dc_wdt {
25 void __iomem *base;
26 struct clk *clk;
27 spinlock_t lock;
30 static unsigned timeout;
31 module_param(timeout, uint, 0);
32 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
34 static void dc_wdt_set(struct dc_wdt *wdt, u32 ticks)
36 unsigned long flags;
38 spin_lock_irqsave(&wdt->lock, flags);
40 writel_relaxed(0, wdt->base + TIMER_A_CONTROL);
41 writel_relaxed(ticks, wdt->base + TIMER_A_COUNT);
42 writel_relaxed(TIMER_A_ENABLE_COUNT | TIMER_A_ENABLE_WATCHDOG,
43 wdt->base + TIMER_A_CONTROL);
45 spin_unlock_irqrestore(&wdt->lock, flags);
48 static int dc_wdt_restart(struct watchdog_device *wdog, unsigned long action,
49 void *data)
51 struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
53 dc_wdt_set(wdt, 1);
54 /* wait for reset to assert... */
55 mdelay(500);
57 return 0;
60 static int dc_wdt_start(struct watchdog_device *wdog)
62 struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
64 dc_wdt_set(wdt, wdog->timeout * clk_get_rate(wdt->clk));
66 return 0;
69 static int dc_wdt_stop(struct watchdog_device *wdog)
71 struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
73 writel_relaxed(0, wdt->base + TIMER_A_CONTROL);
75 return 0;
78 static int dc_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t)
80 struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
82 dc_wdt_set(wdt, t * clk_get_rate(wdt->clk));
83 wdog->timeout = t;
85 return 0;
88 static unsigned int dc_wdt_get_timeleft(struct watchdog_device *wdog)
90 struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
91 uint32_t count = readl_relaxed(wdt->base + TIMER_A_COUNT);
93 return count / clk_get_rate(wdt->clk);
96 static const struct watchdog_ops dc_wdt_ops = {
97 .owner = THIS_MODULE,
98 .start = dc_wdt_start,
99 .stop = dc_wdt_stop,
100 .set_timeout = dc_wdt_set_timeout,
101 .get_timeleft = dc_wdt_get_timeleft,
102 .restart = dc_wdt_restart,
105 static const struct watchdog_info dc_wdt_info = {
106 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE
107 | WDIOF_KEEPALIVEPING,
108 .identity = "Conexant Digicolor Watchdog",
111 static struct watchdog_device dc_wdt_wdd = {
112 .info = &dc_wdt_info,
113 .ops = &dc_wdt_ops,
114 .min_timeout = 1,
117 static int dc_wdt_probe(struct platform_device *pdev)
119 struct resource *res;
120 struct device *dev = &pdev->dev;
121 struct dc_wdt *wdt;
122 int ret;
124 wdt = devm_kzalloc(dev, sizeof(struct dc_wdt), GFP_KERNEL);
125 if (!wdt)
126 return -ENOMEM;
128 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
129 wdt->base = devm_ioremap_resource(dev, res);
130 if (IS_ERR(wdt->base))
131 return PTR_ERR(wdt->base);
133 wdt->clk = devm_clk_get(dev, NULL);
134 if (IS_ERR(wdt->clk))
135 return PTR_ERR(wdt->clk);
136 dc_wdt_wdd.max_timeout = U32_MAX / clk_get_rate(wdt->clk);
137 dc_wdt_wdd.timeout = dc_wdt_wdd.max_timeout;
138 dc_wdt_wdd.parent = dev;
140 spin_lock_init(&wdt->lock);
142 watchdog_set_drvdata(&dc_wdt_wdd, wdt);
143 watchdog_set_restart_priority(&dc_wdt_wdd, 128);
144 watchdog_init_timeout(&dc_wdt_wdd, timeout, dev);
145 watchdog_stop_on_reboot(&dc_wdt_wdd);
146 ret = devm_watchdog_register_device(dev, &dc_wdt_wdd);
147 if (ret) {
148 dev_err(dev, "Failed to register watchdog device");
149 return ret;
152 return 0;
155 static const struct of_device_id dc_wdt_of_match[] = {
156 { .compatible = "cnxt,cx92755-wdt", },
159 MODULE_DEVICE_TABLE(of, dc_wdt_of_match);
161 static struct platform_driver dc_wdt_driver = {
162 .probe = dc_wdt_probe,
163 .driver = {
164 .name = "digicolor-wdt",
165 .of_match_table = dc_wdt_of_match,
168 module_platform_driver(dc_wdt_driver);
170 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
171 MODULE_DESCRIPTION("Driver for Conexant Digicolor watchdog timer");
172 MODULE_LICENSE("GPL");