GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / watchdog / da9062_wdt.c
1 /*
2  * Watchdog device driver for DA9062 and DA9061 PMICs
3  * Copyright (C) 2015  Dialog Semiconductor Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/watchdog.h>
19 #include <linux/platform_device.h>
20 #include <linux/uaccess.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/jiffies.h>
24 #include <linux/mfd/da9062/registers.h>
25 #include <linux/mfd/da9062/core.h>
26 #include <linux/regmap.h>
27 #include <linux/of.h>
28
29 static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
30 #define DA9062_TWDSCALE_DISABLE         0
31 #define DA9062_TWDSCALE_MIN             1
32 #define DA9062_TWDSCALE_MAX             (ARRAY_SIZE(wdt_timeout) - 1)
33 #define DA9062_WDT_MIN_TIMEOUT          wdt_timeout[DA9062_TWDSCALE_MIN]
34 #define DA9062_WDT_MAX_TIMEOUT          wdt_timeout[DA9062_TWDSCALE_MAX]
35 #define DA9062_WDG_DEFAULT_TIMEOUT      wdt_timeout[DA9062_TWDSCALE_MAX-1]
36 #define DA9062_RESET_PROTECTION_MS      300
37
38 struct da9062_watchdog {
39         struct da9062 *hw;
40         struct watchdog_device wdtdev;
41         unsigned long j_time_stamp;
42 };
43
44 static void da9062_set_window_start(struct da9062_watchdog *wdt)
45 {
46         wdt->j_time_stamp = jiffies;
47 }
48
49 static void da9062_apply_window_protection(struct da9062_watchdog *wdt)
50 {
51         unsigned long delay = msecs_to_jiffies(DA9062_RESET_PROTECTION_MS);
52         unsigned long timeout = wdt->j_time_stamp + delay;
53         unsigned long now = jiffies;
54         unsigned int diff_ms;
55
56         /* if time-limit has not elapsed then wait for remainder */
57         if (time_before(now, timeout)) {
58                 diff_ms = jiffies_to_msecs(timeout-now);
59                 dev_dbg(wdt->hw->dev,
60                         "Kicked too quickly. Delaying %u msecs\n", diff_ms);
61                 msleep(diff_ms);
62         }
63 }
64
65 static unsigned int da9062_wdt_timeout_to_sel(unsigned int secs)
66 {
67         unsigned int i;
68
69         for (i = DA9062_TWDSCALE_MIN; i <= DA9062_TWDSCALE_MAX; i++) {
70                 if (wdt_timeout[i] >= secs)
71                         return i;
72         }
73
74         return DA9062_TWDSCALE_MAX;
75 }
76
77 static int da9062_reset_watchdog_timer(struct da9062_watchdog *wdt)
78 {
79         int ret;
80
81         da9062_apply_window_protection(wdt);
82
83         ret = regmap_update_bits(wdt->hw->regmap,
84                            DA9062AA_CONTROL_F,
85                            DA9062AA_WATCHDOG_MASK,
86                            DA9062AA_WATCHDOG_MASK);
87
88         da9062_set_window_start(wdt);
89
90         return ret;
91 }
92
93 static int da9062_wdt_update_timeout_register(struct da9062_watchdog *wdt,
94                                               unsigned int regval)
95 {
96         struct da9062 *chip = wdt->hw;
97
98         return regmap_update_bits(chip->regmap,
99                                   DA9062AA_CONTROL_D,
100                                   DA9062AA_TWDSCALE_MASK,
101                                   regval);
102 }
103
104 static int da9062_wdt_start(struct watchdog_device *wdd)
105 {
106         struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
107         unsigned int selector;
108         int ret;
109
110         selector = da9062_wdt_timeout_to_sel(wdt->wdtdev.timeout);
111         ret = da9062_wdt_update_timeout_register(wdt, selector);
112         if (ret)
113                 dev_err(wdt->hw->dev, "Watchdog failed to start (err = %d)\n",
114                         ret);
115
116         return ret;
117 }
118
119 static int da9062_wdt_stop(struct watchdog_device *wdd)
120 {
121         struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
122         int ret;
123
124         ret = regmap_update_bits(wdt->hw->regmap,
125                                  DA9062AA_CONTROL_D,
126                                  DA9062AA_TWDSCALE_MASK,
127                                  DA9062_TWDSCALE_DISABLE);
128         if (ret)
129                 dev_err(wdt->hw->dev, "Watchdog failed to stop (err = %d)\n",
130                         ret);
131
132         return ret;
133 }
134
135 static int da9062_wdt_ping(struct watchdog_device *wdd)
136 {
137         struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
138         int ret;
139
140         ret = da9062_reset_watchdog_timer(wdt);
141         if (ret)
142                 dev_err(wdt->hw->dev, "Failed to ping the watchdog (err = %d)\n",
143                         ret);
144
145         return ret;
146 }
147
148 static int da9062_wdt_set_timeout(struct watchdog_device *wdd,
149                                   unsigned int timeout)
150 {
151         struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
152         unsigned int selector;
153         int ret;
154
155         selector = da9062_wdt_timeout_to_sel(timeout);
156         ret = da9062_wdt_update_timeout_register(wdt, selector);
157         if (ret)
158                 dev_err(wdt->hw->dev, "Failed to set watchdog timeout (err = %d)\n",
159                         ret);
160         else
161                 wdd->timeout = wdt_timeout[selector];
162
163         return ret;
164 }
165
166 static const struct watchdog_info da9062_watchdog_info = {
167         .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
168         .identity = "DA9062 WDT",
169 };
170
171 static const struct watchdog_ops da9062_watchdog_ops = {
172         .owner = THIS_MODULE,
173         .start = da9062_wdt_start,
174         .stop = da9062_wdt_stop,
175         .ping = da9062_wdt_ping,
176         .set_timeout = da9062_wdt_set_timeout,
177 };
178
179 static const struct of_device_id da9062_compatible_id_table[] = {
180         { .compatible = "dlg,da9062-watchdog", },
181         { },
182 };
183
184 MODULE_DEVICE_TABLE(of, da9062_compatible_id_table);
185
186 static int da9062_wdt_probe(struct platform_device *pdev)
187 {
188         int ret;
189         struct da9062 *chip;
190         struct da9062_watchdog *wdt;
191
192         chip = dev_get_drvdata(pdev->dev.parent);
193         if (!chip)
194                 return -EINVAL;
195
196         wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
197         if (!wdt)
198                 return -ENOMEM;
199
200         wdt->hw = chip;
201
202         wdt->wdtdev.info = &da9062_watchdog_info;
203         wdt->wdtdev.ops = &da9062_watchdog_ops;
204         wdt->wdtdev.min_timeout = DA9062_WDT_MIN_TIMEOUT;
205         wdt->wdtdev.max_timeout = DA9062_WDT_MAX_TIMEOUT;
206         wdt->wdtdev.timeout = DA9062_WDG_DEFAULT_TIMEOUT;
207         wdt->wdtdev.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
208         wdt->wdtdev.parent = &pdev->dev;
209
210         watchdog_set_drvdata(&wdt->wdtdev, wdt);
211
212         ret = devm_watchdog_register_device(&pdev->dev, &wdt->wdtdev);
213         if (ret < 0) {
214                 dev_err(wdt->hw->dev,
215                         "watchdog registration failed (%d)\n", ret);
216                 return ret;
217         }
218
219         da9062_set_window_start(wdt);
220
221         return da9062_wdt_ping(&wdt->wdtdev);
222 }
223
224 static struct platform_driver da9062_wdt_driver = {
225         .probe = da9062_wdt_probe,
226         .driver = {
227                 .name = "da9062-watchdog",
228                 .of_match_table = da9062_compatible_id_table,
229         },
230 };
231 module_platform_driver(da9062_wdt_driver);
232
233 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
234 MODULE_DESCRIPTION("WDT device driver for Dialog DA9062 and DA9061");
235 MODULE_LICENSE("GPL");
236 MODULE_ALIAS("platform:da9062-watchdog");