blob: 3f75129eb0a9f06cd0a7d194a3c390f5ee9bfd3f [file] [log] [blame]
Ashish Jangam664a0d72012-05-24 18:31:14 +05301/*
2 * System monitoring driver for DA9052 PMICs.
3 *
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
5 *
6 * Author: Anthony Olech <Anthony.Olech@diasemi.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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#include <linux/delay.h>
25
26#include <linux/mfd/da9052/reg.h>
27#include <linux/mfd/da9052/da9052.h>
28
29#define DA9052_DEF_TIMEOUT 4
30#define DA9052_TWDMIN 256
31
32struct da9052_wdt_data {
33 struct watchdog_device wdt;
34 struct da9052 *da9052;
35 struct kref kref;
36 unsigned long jpast;
37};
38
39static const struct {
40 u8 reg_val;
41 int time; /* Seconds */
42} da9052_wdt_maps[] = {
43 { 1, 2 },
44 { 2, 4 },
45 { 3, 8 },
46 { 4, 16 },
47 { 5, 32 },
48 { 5, 33 }, /* Actual time 32.768s so included both 32s and 33s */
49 { 6, 65 },
50 { 6, 66 }, /* Actual time 65.536s so include both, 65s and 66s */
51 { 7, 131 },
52};
53
54
55static void da9052_wdt_release_resources(struct kref *r)
56{
57 struct da9052_wdt_data *driver_data =
58 container_of(r, struct da9052_wdt_data, kref);
59
60 kfree(driver_data);
61}
62
63static int da9052_wdt_set_timeout(struct watchdog_device *wdt_dev,
64 unsigned int timeout)
65{
66 struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
67 struct da9052 *da9052 = driver_data->da9052;
68 int ret, i;
69
70 /*
71 * Disable the Watchdog timer before setting
72 * new time out.
73 */
74 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
75 DA9052_CONTROLD_TWDSCALE, 0);
76 if (ret < 0) {
77 dev_err(da9052->dev, "Failed to disable watchdog bit, %d\n",
78 ret);
79 return ret;
80 }
81 if (timeout) {
82 /*
83 * To change the timeout, da9052 needs to
84 * be disabled for at least 150 us.
85 */
86 udelay(150);
87
88 /* Set the desired timeout */
89 for (i = 0; i < ARRAY_SIZE(da9052_wdt_maps); i++)
90 if (da9052_wdt_maps[i].time == timeout)
91 break;
92
93 if (i == ARRAY_SIZE(da9052_wdt_maps))
94 ret = -EINVAL;
95 else
96 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
97 DA9052_CONTROLD_TWDSCALE,
98 da9052_wdt_maps[i].reg_val);
99 if (ret < 0) {
100 dev_err(da9052->dev,
101 "Failed to update timescale bit, %d\n", ret);
102 return ret;
103 }
104
105 wdt_dev->timeout = timeout;
106 driver_data->jpast = jiffies;
107 }
108
109 return 0;
110}
111
112static void da9052_wdt_ref(struct watchdog_device *wdt_dev)
113{
114 struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
115
116 kref_get(&driver_data->kref);
117}
118
119static void da9052_wdt_unref(struct watchdog_device *wdt_dev)
120{
121 struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
122
123 kref_put(&driver_data->kref, da9052_wdt_release_resources);
124}
125
126static int da9052_wdt_start(struct watchdog_device *wdt_dev)
127{
128 return da9052_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
129}
130
131static int da9052_wdt_stop(struct watchdog_device *wdt_dev)
132{
133 return da9052_wdt_set_timeout(wdt_dev, 0);
134}
135
136static int da9052_wdt_ping(struct watchdog_device *wdt_dev)
137{
138 struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
139 struct da9052 *da9052 = driver_data->da9052;
140 unsigned long msec, jnow = jiffies;
141 int ret;
142
143 /*
144 * We have a minimum time for watchdog window called TWDMIN. A write
145 * to the watchdog before this elapsed time should cause an error.
146 */
147 msec = (jnow - driver_data->jpast) * 1000/HZ;
148 if (msec < DA9052_TWDMIN)
149 mdelay(msec);
150
151 /* Reset the watchdog timer */
152 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
153 DA9052_CONTROLD_WATCHDOG, 1 << 7);
154 if (ret < 0)
155 goto err_strobe;
156
157 /*
158 * FIXME: Reset the watchdog core, in general PMIC
159 * is supposed to do this
160 */
161 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
162 DA9052_CONTROLD_WATCHDOG, 0 << 7);
163err_strobe:
164 return ret;
165}
166
167static struct watchdog_info da9052_wdt_info = {
168 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
169 .identity = "DA9052 Watchdog",
170};
171
172static const struct watchdog_ops da9052_wdt_ops = {
173 .owner = THIS_MODULE,
174 .start = da9052_wdt_start,
175 .stop = da9052_wdt_stop,
176 .ping = da9052_wdt_ping,
177 .set_timeout = da9052_wdt_set_timeout,
178 .ref = da9052_wdt_ref,
179 .unref = da9052_wdt_unref,
180};
181
182
183static int __devinit da9052_wdt_probe(struct platform_device *pdev)
184{
185 struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent);
186 struct da9052_wdt_data *driver_data;
187 struct watchdog_device *da9052_wdt;
188 int ret;
189
190 driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data),
191 GFP_KERNEL);
192 if (!driver_data) {
193 dev_err(da9052->dev, "Unable to alloacate watchdog device\n");
194 ret = -ENOMEM;
195 goto err;
196 }
197 driver_data->da9052 = da9052;
198
199 da9052_wdt = &driver_data->wdt;
200
201 da9052_wdt->timeout = DA9052_DEF_TIMEOUT;
202 da9052_wdt->info = &da9052_wdt_info;
203 da9052_wdt->ops = &da9052_wdt_ops;
204 watchdog_set_drvdata(da9052_wdt, driver_data);
205
206 kref_init(&driver_data->kref);
207
208 ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
209 DA9052_CONTROLD_TWDSCALE, 0);
210 if (ret < 0) {
211 dev_err(&pdev->dev, "Failed to disable watchdog bits, %d\n",
212 ret);
213 goto err;
214 }
215
216 ret = watchdog_register_device(&driver_data->wdt);
217 if (ret != 0) {
218 dev_err(da9052->dev, "watchdog_register_device() failed: %d\n",
219 ret);
220 goto err;
221 }
222
223 dev_set_drvdata(&pdev->dev, driver_data);
224err:
225 return ret;
226}
227
228static int __devexit da9052_wdt_remove(struct platform_device *pdev)
229{
230 struct da9052_wdt_data *driver_data = dev_get_drvdata(&pdev->dev);
231
232 watchdog_unregister_device(&driver_data->wdt);
233 kref_put(&driver_data->kref, da9052_wdt_release_resources);
234
235 return 0;
236}
237
238static struct platform_driver da9052_wdt_driver = {
239 .probe = da9052_wdt_probe,
240 .remove = __devexit_p(da9052_wdt_remove),
241 .driver = {
242 .name = "da9052-watchdog",
243 },
244};
245
246module_platform_driver(da9052_wdt_driver);
247
248MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
249MODULE_DESCRIPTION("DA9052 SM Device Driver");
250MODULE_LICENSE("GPL");
251MODULE_ALIAS("platform:da9052-watchdog");