1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/module.h>
16#include <linux/delay.h>
17#include <linux/uaccess.h>
18#include <linux/platform_device.h>
19#include <linux/time.h>
20#include <linux/watchdog.h>
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/jiffies.h>
24
25#include <linux/mfd/da9052/reg.h>
26#include <linux/mfd/da9052/da9052.h>
27
28#define DA9052_DEF_TIMEOUT 4
29#define DA9052_TWDMIN 256
30
31struct da9052_wdt_data {
32 struct watchdog_device wdt;
33 struct da9052 *da9052;
34 struct kref kref;
35 unsigned long jpast;
36};
37
38static const struct {
39 u8 reg_val;
40 int time;
41} da9052_wdt_maps[] = {
42 { 1, 2 },
43 { 2, 4 },
44 { 3, 8 },
45 { 4, 16 },
46 { 5, 32 },
47 { 5, 33 },
48 { 6, 65 },
49 { 6, 66 },
50 { 7, 131 },
51};
52
53
54static void da9052_wdt_release_resources(struct kref *r)
55{
56}
57
58static int da9052_wdt_set_timeout(struct watchdog_device *wdt_dev,
59 unsigned int timeout)
60{
61 struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
62 struct da9052 *da9052 = driver_data->da9052;
63 int ret, i;
64
65
66
67
68
69 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
70 DA9052_CONTROLD_TWDSCALE, 0);
71 if (ret < 0) {
72 dev_err(da9052->dev, "Failed to disable watchdog bit, %d\n",
73 ret);
74 return ret;
75 }
76 if (timeout) {
77
78
79
80
81 udelay(150);
82
83
84 for (i = 0; i < ARRAY_SIZE(da9052_wdt_maps); i++)
85 if (da9052_wdt_maps[i].time == timeout)
86 break;
87
88 if (i == ARRAY_SIZE(da9052_wdt_maps))
89 ret = -EINVAL;
90 else
91 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
92 DA9052_CONTROLD_TWDSCALE,
93 da9052_wdt_maps[i].reg_val);
94 if (ret < 0) {
95 dev_err(da9052->dev,
96 "Failed to update timescale bit, %d\n", ret);
97 return ret;
98 }
99
100 wdt_dev->timeout = timeout;
101 driver_data->jpast = jiffies;
102 }
103
104 return 0;
105}
106
107static void da9052_wdt_ref(struct watchdog_device *wdt_dev)
108{
109 struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
110
111 kref_get(&driver_data->kref);
112}
113
114static void da9052_wdt_unref(struct watchdog_device *wdt_dev)
115{
116 struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
117
118 kref_put(&driver_data->kref, da9052_wdt_release_resources);
119}
120
121static int da9052_wdt_start(struct watchdog_device *wdt_dev)
122{
123 return da9052_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
124}
125
126static int da9052_wdt_stop(struct watchdog_device *wdt_dev)
127{
128 return da9052_wdt_set_timeout(wdt_dev, 0);
129}
130
131static int da9052_wdt_ping(struct watchdog_device *wdt_dev)
132{
133 struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
134 struct da9052 *da9052 = driver_data->da9052;
135 unsigned long msec, jnow = jiffies;
136 int ret;
137
138
139
140
141
142 msec = (jnow - driver_data->jpast) * 1000/HZ;
143 if (msec < DA9052_TWDMIN)
144 mdelay(msec);
145
146
147 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
148 DA9052_CONTROLD_WATCHDOG, 1 << 7);
149 if (ret < 0)
150 goto err_strobe;
151
152
153
154
155
156 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
157 DA9052_CONTROLD_WATCHDOG, 0 << 7);
158err_strobe:
159 return ret;
160}
161
162static struct watchdog_info da9052_wdt_info = {
163 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
164 .identity = "DA9052 Watchdog",
165};
166
167static const struct watchdog_ops da9052_wdt_ops = {
168 .owner = THIS_MODULE,
169 .start = da9052_wdt_start,
170 .stop = da9052_wdt_stop,
171 .ping = da9052_wdt_ping,
172 .set_timeout = da9052_wdt_set_timeout,
173 .ref = da9052_wdt_ref,
174 .unref = da9052_wdt_unref,
175};
176
177
178static int da9052_wdt_probe(struct platform_device *pdev)
179{
180 struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent);
181 struct da9052_wdt_data *driver_data;
182 struct watchdog_device *da9052_wdt;
183 int ret;
184
185 driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data),
186 GFP_KERNEL);
187 if (!driver_data) {
188 dev_err(da9052->dev, "Unable to alloacate watchdog device\n");
189 ret = -ENOMEM;
190 goto err;
191 }
192 driver_data->da9052 = da9052;
193
194 da9052_wdt = &driver_data->wdt;
195
196 da9052_wdt->timeout = DA9052_DEF_TIMEOUT;
197 da9052_wdt->info = &da9052_wdt_info;
198 da9052_wdt->ops = &da9052_wdt_ops;
199 watchdog_set_drvdata(da9052_wdt, driver_data);
200
201 kref_init(&driver_data->kref);
202
203 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
204 DA9052_CONTROLD_TWDSCALE, 0);
205 if (ret < 0) {
206 dev_err(&pdev->dev, "Failed to disable watchdog bits, %d\n",
207 ret);
208 goto err;
209 }
210
211 ret = watchdog_register_device(&driver_data->wdt);
212 if (ret != 0) {
213 dev_err(da9052->dev, "watchdog_register_device() failed: %d\n",
214 ret);
215 goto err;
216 }
217
218 platform_set_drvdata(pdev, driver_data);
219err:
220 return ret;
221}
222
223static int da9052_wdt_remove(struct platform_device *pdev)
224{
225 struct da9052_wdt_data *driver_data = platform_get_drvdata(pdev);
226
227 watchdog_unregister_device(&driver_data->wdt);
228 kref_put(&driver_data->kref, da9052_wdt_release_resources);
229
230 return 0;
231}
232
233static struct platform_driver da9052_wdt_driver = {
234 .probe = da9052_wdt_probe,
235 .remove = da9052_wdt_remove,
236 .driver = {
237 .name = "da9052-watchdog",
238 },
239};
240
241module_platform_driver(da9052_wdt_driver);
242
243MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
244MODULE_DESCRIPTION("DA9052 SM Device Driver");
245MODULE_LICENSE("GPL");
246MODULE_ALIAS("platform:da9052-watchdog");
247