rtc: pcf8523: don't return invalid date when battery is low
[linux-2.6-block.git] / drivers / watchdog / mena21_wdt.c
CommitLineData
2e62c498 1// SPDX-License-Identifier: GPL-2.0+
26c57ef1
JT
2/*
3 * Watchdog driver for the A21 VME CPU Boards
4 *
5 * Copyright (C) 2013 MEN Mikro Elektronik Nuernberg GmbH
6 *
26c57ef1
JT
7 */
8#include <linux/module.h>
9#include <linux/moduleparam.h>
10#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/platform_device.h>
14#include <linux/watchdog.h>
15#include <linux/uaccess.h>
16#include <linux/gpio.h>
17#include <linux/of_gpio.h>
18#include <linux/delay.h>
19#include <linux/bitops.h>
20
21#define NUM_GPIOS 6
22
23enum a21_wdt_gpios {
24 GPIO_WD_ENAB,
25 GPIO_WD_FAST,
26 GPIO_WD_TRIG,
27 GPIO_WD_RST0,
28 GPIO_WD_RST1,
29 GPIO_WD_RST2,
30};
31
32struct a21_wdt_drv {
33 struct watchdog_device wdt;
26c57ef1
JT
34 unsigned gpios[NUM_GPIOS];
35};
36
37static bool nowayout = WATCHDOG_NOWAYOUT;
38module_param(nowayout, bool, 0);
39MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
40 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
41
42static unsigned int a21_wdt_get_bootstatus(struct a21_wdt_drv *drv)
43{
44 int reset = 0;
45
46 reset |= gpio_get_value(drv->gpios[GPIO_WD_RST0]) ? (1 << 0) : 0;
47 reset |= gpio_get_value(drv->gpios[GPIO_WD_RST1]) ? (1 << 1) : 0;
48 reset |= gpio_get_value(drv->gpios[GPIO_WD_RST2]) ? (1 << 2) : 0;
49
50 return reset;
51}
52
53static int a21_wdt_start(struct watchdog_device *wdt)
54{
55 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
56
26c57ef1
JT
57 gpio_set_value(drv->gpios[GPIO_WD_ENAB], 1);
58
26c57ef1
JT
59 return 0;
60}
61
62static int a21_wdt_stop(struct watchdog_device *wdt)
63{
64 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
65
26c57ef1
JT
66 gpio_set_value(drv->gpios[GPIO_WD_ENAB], 0);
67
26c57ef1
JT
68 return 0;
69}
70
71static int a21_wdt_ping(struct watchdog_device *wdt)
72{
73 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
74
26c57ef1
JT
75 gpio_set_value(drv->gpios[GPIO_WD_TRIG], 0);
76 ndelay(10);
77 gpio_set_value(drv->gpios[GPIO_WD_TRIG], 1);
78
26c57ef1
JT
79 return 0;
80}
81
82static int a21_wdt_set_timeout(struct watchdog_device *wdt,
83 unsigned int timeout)
84{
85 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
86
87 if (timeout != 1 && timeout != 30) {
07352366 88 dev_err(wdt->parent, "Only 1 and 30 allowed as timeout\n");
26c57ef1
JT
89 return -EINVAL;
90 }
91
92 if (timeout == 30 && wdt->timeout == 1) {
07352366 93 dev_err(wdt->parent,
26c57ef1
JT
94 "Transition from fast to slow mode not allowed\n");
95 return -EINVAL;
96 }
97
26c57ef1
JT
98 if (timeout == 1)
99 gpio_set_value(drv->gpios[GPIO_WD_FAST], 1);
100 else
101 gpio_set_value(drv->gpios[GPIO_WD_FAST], 0);
102
103 wdt->timeout = timeout;
104
26c57ef1
JT
105 return 0;
106}
107
108static const struct watchdog_info a21_wdt_info = {
109 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
110 .identity = "MEN A21 Watchdog",
111};
112
113static const struct watchdog_ops a21_wdt_ops = {
114 .owner = THIS_MODULE,
115 .start = a21_wdt_start,
116 .stop = a21_wdt_stop,
117 .ping = a21_wdt_ping,
118 .set_timeout = a21_wdt_set_timeout,
119};
120
121static struct watchdog_device a21_wdt = {
122 .info = &a21_wdt_info,
123 .ops = &a21_wdt_ops,
124 .min_timeout = 1,
125 .max_timeout = 30,
126};
127
128static int a21_wdt_probe(struct platform_device *pdev)
129{
130 struct device_node *node;
131 struct a21_wdt_drv *drv;
132 unsigned int reset = 0;
133 int num_gpios;
134 int ret;
135 int i;
136
137 drv = devm_kzalloc(&pdev->dev, sizeof(struct a21_wdt_drv), GFP_KERNEL);
138 if (!drv)
139 return -ENOMEM;
140
141 /* Fill GPIO pin array */
142 node = pdev->dev.of_node;
143
144 num_gpios = of_gpio_count(node);
145 if (num_gpios != NUM_GPIOS) {
146 dev_err(&pdev->dev, "gpios DT property wrong, got %d want %d",
147 num_gpios, NUM_GPIOS);
148 return -ENODEV;
149 }
150
151 for (i = 0; i < num_gpios; i++) {
152 int val;
153
154 val = of_get_gpio(node, i);
155 if (val < 0)
156 return val;
157
158 drv->gpios[i] = val;
159 }
160
161 /* Request the used GPIOs */
162 for (i = 0; i < num_gpios; i++) {
163 ret = devm_gpio_request(&pdev->dev, drv->gpios[i],
164 "MEN A21 Watchdog");
165 if (ret)
166 return ret;
167
168 if (i < GPIO_WD_RST0)
169 ret = gpio_direction_output(drv->gpios[i],
170 gpio_get_value(drv->gpios[i]));
171 else /* GPIO_WD_RST[0..2] are inputs */
172 ret = gpio_direction_input(drv->gpios[i]);
173 if (ret)
174 return ret;
175 }
176
26c57ef1
JT
177 watchdog_init_timeout(&a21_wdt, 30, &pdev->dev);
178 watchdog_set_nowayout(&a21_wdt, nowayout);
179 watchdog_set_drvdata(&a21_wdt, drv);
6551881c 180 a21_wdt.parent = &pdev->dev;
26c57ef1
JT
181
182 reset = a21_wdt_get_bootstatus(drv);
183 if (reset == 2)
184 a21_wdt.bootstatus |= WDIOF_EXTERN1;
185 else if (reset == 4)
186 a21_wdt.bootstatus |= WDIOF_CARDRESET;
187 else if (reset == 5)
188 a21_wdt.bootstatus |= WDIOF_POWERUNDER;
189 else if (reset == 7)
190 a21_wdt.bootstatus |= WDIOF_EXTERN2;
191
57337db1
JT
192 drv->wdt = a21_wdt;
193 dev_set_drvdata(&pdev->dev, drv);
194
cb5c14ea 195 ret = devm_watchdog_register_device(&pdev->dev, &a21_wdt);
26c57ef1
JT
196 if (ret) {
197 dev_err(&pdev->dev, "Cannot register watchdog device\n");
cb5c14ea 198 return ret;
26c57ef1
JT
199 }
200
26c57ef1
JT
201 dev_info(&pdev->dev, "MEN A21 watchdog timer driver enabled\n");
202
26c57ef1
JT
203 return 0;
204}
205
206static void a21_wdt_shutdown(struct platform_device *pdev)
207{
208 struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev);
209
210 gpio_set_value(drv->gpios[GPIO_WD_ENAB], 0);
211}
212
213static const struct of_device_id a21_wdt_ids[] = {
214 { .compatible = "men,a021-wdt" },
215 { },
216};
c73318f4 217MODULE_DEVICE_TABLE(of, a21_wdt_ids);
26c57ef1
JT
218
219static struct platform_driver a21_wdt_driver = {
220 .probe = a21_wdt_probe,
26c57ef1
JT
221 .shutdown = a21_wdt_shutdown,
222 .driver = {
223 .name = "a21-watchdog",
224 .of_match_table = a21_wdt_ids,
225 },
226};
227
228module_platform_driver(a21_wdt_driver);
229
230MODULE_AUTHOR("MEN Mikro Elektronik");
231MODULE_DESCRIPTION("MEN A21 Watchdog");
232MODULE_LICENSE("GPL");
233MODULE_ALIAS("platform:a21-watchdog");