1
2
3
4
5
6
7
8
9
10
11#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/platform_device.h>
16#include <linux/watchdog.h>
17#include <linux/delay.h>
18
19#include <linux/mfd/da9055/core.h>
20#include <linux/mfd/da9055/reg.h>
21
22static bool nowayout = WATCHDOG_NOWAYOUT;
23module_param(nowayout, bool, 0);
24MODULE_PARM_DESC(nowayout,
25 "Watchdog cannot be stopped once started (default="
26 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
27
28#define DA9055_DEF_TIMEOUT 4
29#define DA9055_TWDMIN 256
30
31struct da9055_wdt_data {
32 struct watchdog_device wdt;
33 struct da9055 *da9055;
34};
35
36static const struct {
37 u8 reg_val;
38 int user_time;
39} da9055_wdt_maps[] = {
40 { 0, 0 },
41 { 1, 2 },
42 { 2, 4 },
43 { 3, 8 },
44 { 4, 16 },
45 { 5, 32 },
46 { 5, 33 },
47 { 6, 65 },
48 { 6, 66 },
49 { 7, 131 },
50};
51
52static int da9055_wdt_set_timeout(struct watchdog_device *wdt_dev,
53 unsigned int timeout)
54{
55 struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
56 struct da9055 *da9055 = driver_data->da9055;
57 int ret, i;
58
59 for (i = 0; i < ARRAY_SIZE(da9055_wdt_maps); i++)
60 if (da9055_wdt_maps[i].user_time == timeout)
61 break;
62
63 if (i == ARRAY_SIZE(da9055_wdt_maps))
64 ret = -EINVAL;
65 else
66 ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B,
67 DA9055_TWDSCALE_MASK,
68 da9055_wdt_maps[i].reg_val <<
69 DA9055_TWDSCALE_SHIFT);
70 if (ret < 0) {
71 dev_err(da9055->dev,
72 "Failed to update timescale bit, %d\n", ret);
73 return ret;
74 }
75
76 wdt_dev->timeout = timeout;
77
78 return 0;
79}
80
81static int da9055_wdt_ping(struct watchdog_device *wdt_dev)
82{
83 struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
84 struct da9055 *da9055 = driver_data->da9055;
85
86
87
88
89
90 mdelay(DA9055_TWDMIN);
91
92
93 return da9055_reg_update(da9055, DA9055_REG_CONTROL_E,
94 DA9055_WATCHDOG_MASK, 1);
95}
96
97static int da9055_wdt_start(struct watchdog_device *wdt_dev)
98{
99 return da9055_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
100}
101
102static int da9055_wdt_stop(struct watchdog_device *wdt_dev)
103{
104 return da9055_wdt_set_timeout(wdt_dev, 0);
105}
106
107static const struct watchdog_info da9055_wdt_info = {
108 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
109 .identity = "DA9055 Watchdog",
110};
111
112static const struct watchdog_ops da9055_wdt_ops = {
113 .owner = THIS_MODULE,
114 .start = da9055_wdt_start,
115 .stop = da9055_wdt_stop,
116 .ping = da9055_wdt_ping,
117 .set_timeout = da9055_wdt_set_timeout,
118};
119
120static int da9055_wdt_probe(struct platform_device *pdev)
121{
122 struct device *dev = &pdev->dev;
123 struct da9055 *da9055 = dev_get_drvdata(dev->parent);
124 struct da9055_wdt_data *driver_data;
125 struct watchdog_device *da9055_wdt;
126 int ret;
127
128 driver_data = devm_kzalloc(dev, sizeof(*driver_data), GFP_KERNEL);
129 if (!driver_data)
130 return -ENOMEM;
131
132 driver_data->da9055 = da9055;
133
134 da9055_wdt = &driver_data->wdt;
135
136 da9055_wdt->timeout = DA9055_DEF_TIMEOUT;
137 da9055_wdt->info = &da9055_wdt_info;
138 da9055_wdt->ops = &da9055_wdt_ops;
139 da9055_wdt->parent = dev;
140 watchdog_set_nowayout(da9055_wdt, nowayout);
141 watchdog_set_drvdata(da9055_wdt, driver_data);
142
143 ret = da9055_wdt_stop(da9055_wdt);
144 if (ret < 0) {
145 dev_err(dev, "Failed to stop watchdog, %d\n", ret);
146 return ret;
147 }
148
149 ret = devm_watchdog_register_device(dev, &driver_data->wdt);
150 if (ret != 0)
151 dev_err(da9055->dev, "watchdog_register_device() failed: %d\n",
152 ret);
153
154 return ret;
155}
156
157static struct platform_driver da9055_wdt_driver = {
158 .probe = da9055_wdt_probe,
159 .driver = {
160 .name = "da9055-watchdog",
161 },
162};
163
164module_platform_driver(da9055_wdt_driver);
165
166MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
167MODULE_DESCRIPTION("DA9055 watchdog");
168MODULE_LICENSE("GPL");
169MODULE_ALIAS("platform:da9055-watchdog");
170