2 * System monitoring driver for DA9052 PMICs.
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
6 * Author: Anthony Olech <Anthony.Olech@diasemi.com>
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.
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>
25 #include <linux/mfd/da9052/reg.h>
26 #include <linux/mfd/da9052/da9052.h>
28 #define DA9052_DEF_TIMEOUT 4
29 #define DA9052_TWDMIN 256
31 struct da9052_wdt_data
{
32 struct watchdog_device wdt
;
33 struct da9052
*da9052
;
40 int time
; /* Seconds */
41 } da9052_wdt_maps
[] = {
47 { 5, 33 }, /* Actual time 32.768s so included both 32s and 33s */
49 { 6, 66 }, /* Actual time 65.536s so include both, 65s and 66s */
54 static void da9052_wdt_release_resources(struct kref
*r
)
58 static int da9052_wdt_set_timeout(struct watchdog_device
*wdt_dev
,
61 struct da9052_wdt_data
*driver_data
= watchdog_get_drvdata(wdt_dev
);
62 struct da9052
*da9052
= driver_data
->da9052
;
66 * Disable the Watchdog timer before setting
69 ret
= da9052_reg_update(da9052
, DA9052_CONTROL_D_REG
,
70 DA9052_CONTROLD_TWDSCALE
, 0);
72 dev_err(da9052
->dev
, "Failed to disable watchdog bit, %d\n",
78 * To change the timeout, da9052 needs to
79 * be disabled for at least 150 us.
83 /* Set the desired timeout */
84 for (i
= 0; i
< ARRAY_SIZE(da9052_wdt_maps
); i
++)
85 if (da9052_wdt_maps
[i
].time
== timeout
)
88 if (i
== ARRAY_SIZE(da9052_wdt_maps
))
91 ret
= da9052_reg_update(da9052
, DA9052_CONTROL_D_REG
,
92 DA9052_CONTROLD_TWDSCALE
,
93 da9052_wdt_maps
[i
].reg_val
);
96 "Failed to update timescale bit, %d\n", ret
);
100 wdt_dev
->timeout
= timeout
;
101 driver_data
->jpast
= jiffies
;
107 static void da9052_wdt_ref(struct watchdog_device
*wdt_dev
)
109 struct da9052_wdt_data
*driver_data
= watchdog_get_drvdata(wdt_dev
);
111 kref_get(&driver_data
->kref
);
114 static void da9052_wdt_unref(struct watchdog_device
*wdt_dev
)
116 struct da9052_wdt_data
*driver_data
= watchdog_get_drvdata(wdt_dev
);
118 kref_put(&driver_data
->kref
, da9052_wdt_release_resources
);
121 static int da9052_wdt_start(struct watchdog_device
*wdt_dev
)
123 return da9052_wdt_set_timeout(wdt_dev
, wdt_dev
->timeout
);
126 static int da9052_wdt_stop(struct watchdog_device
*wdt_dev
)
128 return da9052_wdt_set_timeout(wdt_dev
, 0);
131 static int da9052_wdt_ping(struct watchdog_device
*wdt_dev
)
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
;
139 * We have a minimum time for watchdog window called TWDMIN. A write
140 * to the watchdog before this elapsed time should cause an error.
142 msec
= (jnow
- driver_data
->jpast
) * 1000/HZ
;
143 if (msec
< DA9052_TWDMIN
)
146 /* Reset the watchdog timer */
147 ret
= da9052_reg_update(da9052
, DA9052_CONTROL_D_REG
,
148 DA9052_CONTROLD_WATCHDOG
, 1 << 7);
153 * FIXME: Reset the watchdog core, in general PMIC
154 * is supposed to do this
156 ret
= da9052_reg_update(da9052
, DA9052_CONTROL_D_REG
,
157 DA9052_CONTROLD_WATCHDOG
, 0 << 7);
162 static struct watchdog_info da9052_wdt_info
= {
163 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
,
164 .identity
= "DA9052 Watchdog",
167 static 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
,
178 static int da9052_wdt_probe(struct platform_device
*pdev
)
180 struct da9052
*da9052
= dev_get_drvdata(pdev
->dev
.parent
);
181 struct da9052_wdt_data
*driver_data
;
182 struct watchdog_device
*da9052_wdt
;
185 driver_data
= devm_kzalloc(&pdev
->dev
, sizeof(*driver_data
),
188 dev_err(da9052
->dev
, "Unable to alloacate watchdog device\n");
192 driver_data
->da9052
= da9052
;
194 da9052_wdt
= &driver_data
->wdt
;
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
);
201 kref_init(&driver_data
->kref
);
203 ret
= da9052_reg_update(da9052
, DA9052_CONTROL_D_REG
,
204 DA9052_CONTROLD_TWDSCALE
, 0);
206 dev_err(&pdev
->dev
, "Failed to disable watchdog bits, %d\n",
211 ret
= watchdog_register_device(&driver_data
->wdt
);
213 dev_err(da9052
->dev
, "watchdog_register_device() failed: %d\n",
218 platform_set_drvdata(pdev
, driver_data
);
223 static int da9052_wdt_remove(struct platform_device
*pdev
)
225 struct da9052_wdt_data
*driver_data
= platform_get_drvdata(pdev
);
227 watchdog_unregister_device(&driver_data
->wdt
);
228 kref_put(&driver_data
->kref
, da9052_wdt_release_resources
);
233 static struct platform_driver da9052_wdt_driver
= {
234 .probe
= da9052_wdt_probe
,
235 .remove
= da9052_wdt_remove
,
237 .name
= "da9052-watchdog",
241 module_platform_driver(da9052_wdt_driver
);
243 MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
244 MODULE_DESCRIPTION("DA9052 SM Device Driver");
245 MODULE_LICENSE("GPL");
246 MODULE_ALIAS("platform:da9052-watchdog");