staging: lustre: acl: Remove lustre_posix_acl_xattr_free wrapper
[linux-2.6/btrfs-unstable.git] / drivers / watchdog / digicolor_wdt.c
blob50abe1bf62a50a9a914d53e2c5cd0469fafb84fd
1 /*
2 * Watchdog driver for Conexant Digicolor
4 * Copyright (C) 2015 Paradox Innovation Ltd.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/io.h>
15 #include <linux/delay.h>
16 #include <linux/clk.h>
17 #include <linux/watchdog.h>
18 #include <linux/reboot.h>
19 #include <linux/platform_device.h>
20 #include <linux/of_address.h>
22 #define TIMER_A_CONTROL 0
23 #define TIMER_A_COUNT 4
25 #define TIMER_A_ENABLE_COUNT BIT(0)
26 #define TIMER_A_ENABLE_WATCHDOG BIT(1)
28 struct dc_wdt {
29 void __iomem *base;
30 struct clk *clk;
31 struct notifier_block restart_handler;
32 spinlock_t lock;
35 static unsigned timeout;
36 module_param(timeout, uint, 0);
37 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
39 static void dc_wdt_set(struct dc_wdt *wdt, u32 ticks)
41 unsigned long flags;
43 spin_lock_irqsave(&wdt->lock, flags);
45 writel_relaxed(0, wdt->base + TIMER_A_CONTROL);
46 writel_relaxed(ticks, wdt->base + TIMER_A_COUNT);
47 writel_relaxed(TIMER_A_ENABLE_COUNT | TIMER_A_ENABLE_WATCHDOG,
48 wdt->base + TIMER_A_CONTROL);
50 spin_unlock_irqrestore(&wdt->lock, flags);
53 static int dc_restart_handler(struct notifier_block *this, unsigned long mode,
54 void *cmd)
56 struct dc_wdt *wdt = container_of(this, struct dc_wdt, restart_handler);
58 dc_wdt_set(wdt, 1);
59 /* wait for reset to assert... */
60 mdelay(500);
62 return NOTIFY_DONE;
65 static int dc_wdt_start(struct watchdog_device *wdog)
67 struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
69 dc_wdt_set(wdt, wdog->timeout * clk_get_rate(wdt->clk));
71 return 0;
74 static int dc_wdt_stop(struct watchdog_device *wdog)
76 struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
78 writel_relaxed(0, wdt->base + TIMER_A_CONTROL);
80 return 0;
83 static int dc_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t)
85 struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
87 dc_wdt_set(wdt, t * clk_get_rate(wdt->clk));
88 wdog->timeout = t;
90 return 0;
93 static unsigned int dc_wdt_get_timeleft(struct watchdog_device *wdog)
95 struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
96 uint32_t count = readl_relaxed(wdt->base + TIMER_A_COUNT);
98 return count / clk_get_rate(wdt->clk);
101 static struct watchdog_ops dc_wdt_ops = {
102 .owner = THIS_MODULE,
103 .start = dc_wdt_start,
104 .stop = dc_wdt_stop,
105 .set_timeout = dc_wdt_set_timeout,
106 .get_timeleft = dc_wdt_get_timeleft,
109 static struct watchdog_info dc_wdt_info = {
110 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE
111 | WDIOF_KEEPALIVEPING,
112 .identity = "Conexant Digicolor Watchdog",
115 static struct watchdog_device dc_wdt_wdd = {
116 .info = &dc_wdt_info,
117 .ops = &dc_wdt_ops,
118 .min_timeout = 1,
121 static int dc_wdt_probe(struct platform_device *pdev)
123 struct device *dev = &pdev->dev;
124 struct device_node *np = dev->of_node;
125 struct dc_wdt *wdt;
126 int ret;
128 wdt = devm_kzalloc(dev, sizeof(struct dc_wdt), GFP_KERNEL);
129 if (!wdt)
130 return -ENOMEM;
131 platform_set_drvdata(pdev, wdt);
133 wdt->base = of_iomap(np, 0);
134 if (!wdt->base) {
135 dev_err(dev, "Failed to remap watchdog regs");
136 return -ENODEV;
139 wdt->clk = devm_clk_get(&pdev->dev, NULL);
140 if (IS_ERR(wdt->clk)) {
141 ret = PTR_ERR(wdt->clk);
142 goto err_iounmap;
144 dc_wdt_wdd.max_timeout = U32_MAX / clk_get_rate(wdt->clk);
145 dc_wdt_wdd.timeout = dc_wdt_wdd.max_timeout;
146 dc_wdt_wdd.parent = &pdev->dev;
148 spin_lock_init(&wdt->lock);
150 watchdog_set_drvdata(&dc_wdt_wdd, wdt);
151 watchdog_init_timeout(&dc_wdt_wdd, timeout, dev);
152 ret = watchdog_register_device(&dc_wdt_wdd);
153 if (ret) {
154 dev_err(dev, "Failed to register watchdog device");
155 goto err_iounmap;
158 wdt->restart_handler.notifier_call = dc_restart_handler;
159 wdt->restart_handler.priority = 128;
160 ret = register_restart_handler(&wdt->restart_handler);
161 if (ret)
162 dev_warn(&pdev->dev, "cannot register restart handler\n");
164 return 0;
166 err_iounmap:
167 iounmap(wdt->base);
168 return ret;
171 static int dc_wdt_remove(struct platform_device *pdev)
173 struct dc_wdt *wdt = platform_get_drvdata(pdev);
175 unregister_restart_handler(&wdt->restart_handler);
176 watchdog_unregister_device(&dc_wdt_wdd);
177 iounmap(wdt->base);
179 return 0;
182 static void dc_wdt_shutdown(struct platform_device *pdev)
184 dc_wdt_stop(&dc_wdt_wdd);
187 static const struct of_device_id dc_wdt_of_match[] = {
188 { .compatible = "cnxt,cx92755-wdt", },
191 MODULE_DEVICE_TABLE(of, dc_wdt_of_match);
193 static struct platform_driver dc_wdt_driver = {
194 .probe = dc_wdt_probe,
195 .remove = dc_wdt_remove,
196 .shutdown = dc_wdt_shutdown,
197 .driver = {
198 .name = "digicolor-wdt",
199 .of_match_table = dc_wdt_of_match,
202 module_platform_driver(dc_wdt_driver);
204 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
205 MODULE_DESCRIPTION("Driver for Conexant Digicolor watchdog timer");
206 MODULE_LICENSE("GPL");