]>
Commit | Line | Data |
---|---|---|
2e62c498 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
5e9c16e3 KG |
2 | /* |
3 | * Watchdog driver for DA9063 PMICs. | |
4 | * | |
5 | * Copyright(c) 2012 Dialog Semiconductor Ltd. | |
6 | * | |
7 | * Author: Mariusz Wojtasik <mariusz.wojtasik@diasemi.com> | |
8 | * | |
5e9c16e3 KG |
9 | */ |
10 | ||
11 | #include <linux/kernel.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/watchdog.h> | |
14 | #include <linux/platform_device.h> | |
15 | #include <linux/uaccess.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/delay.h> | |
18 | #include <linux/mfd/da9063/registers.h> | |
19 | #include <linux/mfd/da9063/core.h> | |
20 | #include <linux/regmap.h> | |
21 | ||
22 | /* | |
23 | * Watchdog selector to timeout in seconds. | |
24 | * 0: WDT disabled; | |
25 | * others: timeout = 2048 ms * 2^(TWDSCALE-1). | |
26 | */ | |
27 | static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 }; | |
28 | #define DA9063_TWDSCALE_DISABLE 0 | |
29 | #define DA9063_TWDSCALE_MIN 1 | |
30 | #define DA9063_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1) | |
31 | #define DA9063_WDT_MIN_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MIN] | |
32 | #define DA9063_WDT_MAX_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MAX] | |
33 | #define DA9063_WDG_TIMEOUT wdt_timeout[3] | |
a74cab40 | 34 | #define DA9063_RESET_PROTECTION_MS 256 |
5e9c16e3 | 35 | |
5e9c16e3 KG |
36 | static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs) |
37 | { | |
38 | unsigned int i; | |
39 | ||
40 | for (i = DA9063_TWDSCALE_MIN; i <= DA9063_TWDSCALE_MAX; i++) { | |
41 | if (wdt_timeout[i] >= secs) | |
42 | return i; | |
43 | } | |
44 | ||
45 | return DA9063_TWDSCALE_MAX; | |
46 | } | |
47 | ||
48 | static int _da9063_wdt_set_timeout(struct da9063 *da9063, unsigned int regval) | |
49 | { | |
50 | return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D, | |
51 | DA9063_TWDSCALE_MASK, regval); | |
52 | } | |
53 | ||
54 | static int da9063_wdt_start(struct watchdog_device *wdd) | |
55 | { | |
a1f2a820 | 56 | struct da9063 *da9063 = watchdog_get_drvdata(wdd); |
5e9c16e3 KG |
57 | unsigned int selector; |
58 | int ret; | |
59 | ||
a1f2a820 | 60 | selector = da9063_wdt_timeout_to_sel(wdd->timeout); |
61 | ret = _da9063_wdt_set_timeout(da9063, selector); | |
5e9c16e3 | 62 | if (ret) |
a1f2a820 | 63 | dev_err(da9063->dev, "Watchdog failed to start (err = %d)\n", |
5e9c16e3 KG |
64 | ret); |
65 | ||
66 | return ret; | |
67 | } | |
68 | ||
69 | static int da9063_wdt_stop(struct watchdog_device *wdd) | |
70 | { | |
a1f2a820 | 71 | struct da9063 *da9063 = watchdog_get_drvdata(wdd); |
5e9c16e3 KG |
72 | int ret; |
73 | ||
a1f2a820 | 74 | ret = regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D, |
5e9c16e3 KG |
75 | DA9063_TWDSCALE_MASK, DA9063_TWDSCALE_DISABLE); |
76 | if (ret) | |
a1f2a820 | 77 | dev_alert(da9063->dev, "Watchdog failed to stop (err = %d)\n", |
5e9c16e3 KG |
78 | ret); |
79 | ||
80 | return ret; | |
81 | } | |
82 | ||
83 | static int da9063_wdt_ping(struct watchdog_device *wdd) | |
84 | { | |
a1f2a820 | 85 | struct da9063 *da9063 = watchdog_get_drvdata(wdd); |
5e9c16e3 KG |
86 | int ret; |
87 | ||
a1f2a820 | 88 | ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F, |
5e9c16e3 KG |
89 | DA9063_WATCHDOG); |
90 | if (ret) | |
a1f2a820 | 91 | dev_alert(da9063->dev, "Failed to ping the watchdog (err = %d)\n", |
5e9c16e3 KG |
92 | ret); |
93 | ||
94 | return ret; | |
95 | } | |
96 | ||
97 | static int da9063_wdt_set_timeout(struct watchdog_device *wdd, | |
98 | unsigned int timeout) | |
99 | { | |
a1f2a820 | 100 | struct da9063 *da9063 = watchdog_get_drvdata(wdd); |
5e9c16e3 KG |
101 | unsigned int selector; |
102 | int ret; | |
103 | ||
104 | selector = da9063_wdt_timeout_to_sel(timeout); | |
a1f2a820 | 105 | ret = _da9063_wdt_set_timeout(da9063, selector); |
5e9c16e3 | 106 | if (ret) |
a1f2a820 | 107 | dev_err(da9063->dev, "Failed to set watchdog timeout (err = %d)\n", |
5e9c16e3 KG |
108 | ret); |
109 | else | |
110 | wdd->timeout = wdt_timeout[selector]; | |
111 | ||
112 | return ret; | |
113 | } | |
114 | ||
4d8b229d GR |
115 | static int da9063_wdt_restart(struct watchdog_device *wdd, unsigned long action, |
116 | void *data) | |
396f163c | 117 | { |
a1f2a820 | 118 | struct da9063 *da9063 = watchdog_get_drvdata(wdd); |
396f163c GU |
119 | int ret; |
120 | ||
a1f2a820 | 121 | ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F, |
396f163c GU |
122 | DA9063_SHUTDOWN); |
123 | if (ret) | |
a1f2a820 | 124 | dev_alert(da9063->dev, "Failed to shutdown (err = %d)\n", |
396f163c GU |
125 | ret); |
126 | ||
f79781ce | 127 | return ret; |
396f163c GU |
128 | } |
129 | ||
5e9c16e3 KG |
130 | static const struct watchdog_info da9063_watchdog_info = { |
131 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, | |
132 | .identity = "DA9063 Watchdog", | |
133 | }; | |
134 | ||
135 | static const struct watchdog_ops da9063_watchdog_ops = { | |
136 | .owner = THIS_MODULE, | |
137 | .start = da9063_wdt_start, | |
138 | .stop = da9063_wdt_stop, | |
139 | .ping = da9063_wdt_ping, | |
140 | .set_timeout = da9063_wdt_set_timeout, | |
f79781ce | 141 | .restart = da9063_wdt_restart, |
5e9c16e3 KG |
142 | }; |
143 | ||
144 | static int da9063_wdt_probe(struct platform_device *pdev) | |
145 | { | |
5e9c16e3 | 146 | struct da9063 *da9063; |
a1f2a820 | 147 | struct watchdog_device *wdd; |
5e9c16e3 KG |
148 | |
149 | if (!pdev->dev.parent) | |
150 | return -EINVAL; | |
151 | ||
152 | da9063 = dev_get_drvdata(pdev->dev.parent); | |
153 | if (!da9063) | |
154 | return -EINVAL; | |
155 | ||
a1f2a820 | 156 | wdd = devm_kzalloc(&pdev->dev, sizeof(*wdd), GFP_KERNEL); |
157 | if (!wdd) | |
5e9c16e3 KG |
158 | return -ENOMEM; |
159 | ||
a1f2a820 | 160 | wdd->info = &da9063_watchdog_info; |
161 | wdd->ops = &da9063_watchdog_ops; | |
162 | wdd->min_timeout = DA9063_WDT_MIN_TIMEOUT; | |
163 | wdd->max_timeout = DA9063_WDT_MAX_TIMEOUT; | |
164 | wdd->min_hw_heartbeat_ms = DA9063_RESET_PROTECTION_MS; | |
165 | wdd->timeout = DA9063_WDG_TIMEOUT; | |
166 | wdd->parent = &pdev->dev; | |
5e9c16e3 | 167 | |
a1f2a820 | 168 | wdd->status = WATCHDOG_NOWAYOUT_INIT_STATUS; |
5e9c16e3 | 169 | |
a1f2a820 | 170 | watchdog_set_restart_priority(wdd, 128); |
f79781ce | 171 | |
a1f2a820 | 172 | watchdog_set_drvdata(wdd, da9063); |
5e9c16e3 | 173 | |
a1f2a820 | 174 | return devm_watchdog_register_device(&pdev->dev, wdd); |
5e9c16e3 KG |
175 | } |
176 | ||
177 | static struct platform_driver da9063_wdt_driver = { | |
178 | .probe = da9063_wdt_probe, | |
5e9c16e3 KG |
179 | .driver = { |
180 | .name = DA9063_DRVNAME_WATCHDOG, | |
181 | }, | |
182 | }; | |
183 | module_platform_driver(da9063_wdt_driver); | |
184 | ||
185 | MODULE_AUTHOR("Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>"); | |
186 | MODULE_DESCRIPTION("Watchdog driver for Dialog DA9063"); | |
187 | MODULE_LICENSE("GPL"); | |
188 | MODULE_ALIAS("platform:" DA9063_DRVNAME_WATCHDOG); |