2 * Watchdog driver for the A21 VME CPU Boards
4 * Copyright (C) 2013 MEN Mikro Elektronik Nuernberg GmbH
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation
10 #include <linux/module.h>
11 #include <linux/moduleparam.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/uaccess.h>
18 #include <linux/gpio.h>
19 #include <linux/of_gpio.h>
20 #include <linux/delay.h>
21 #include <linux/bitops.h>
35 struct watchdog_device wdt
;
37 unsigned gpios
[NUM_GPIOS
];
40 static bool nowayout
= WATCHDOG_NOWAYOUT
;
41 module_param(nowayout
, bool, 0);
42 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
43 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
45 static unsigned int a21_wdt_get_bootstatus(struct a21_wdt_drv
*drv
)
49 reset
|= gpio_get_value(drv
->gpios
[GPIO_WD_RST0
]) ? (1 << 0) : 0;
50 reset
|= gpio_get_value(drv
->gpios
[GPIO_WD_RST1
]) ? (1 << 1) : 0;
51 reset
|= gpio_get_value(drv
->gpios
[GPIO_WD_RST2
]) ? (1 << 2) : 0;
56 static int a21_wdt_start(struct watchdog_device
*wdt
)
58 struct a21_wdt_drv
*drv
= watchdog_get_drvdata(wdt
);
60 mutex_lock(&drv
->lock
);
62 gpio_set_value(drv
->gpios
[GPIO_WD_ENAB
], 1);
64 mutex_unlock(&drv
->lock
);
69 static int a21_wdt_stop(struct watchdog_device
*wdt
)
71 struct a21_wdt_drv
*drv
= watchdog_get_drvdata(wdt
);
73 mutex_lock(&drv
->lock
);
75 gpio_set_value(drv
->gpios
[GPIO_WD_ENAB
], 0);
77 mutex_unlock(&drv
->lock
);
82 static int a21_wdt_ping(struct watchdog_device
*wdt
)
84 struct a21_wdt_drv
*drv
= watchdog_get_drvdata(wdt
);
86 mutex_lock(&drv
->lock
);
88 gpio_set_value(drv
->gpios
[GPIO_WD_TRIG
], 0);
90 gpio_set_value(drv
->gpios
[GPIO_WD_TRIG
], 1);
92 mutex_unlock(&drv
->lock
);
97 static int a21_wdt_set_timeout(struct watchdog_device
*wdt
,
100 struct a21_wdt_drv
*drv
= watchdog_get_drvdata(wdt
);
102 if (timeout
!= 1 && timeout
!= 30) {
103 dev_err(wdt
->parent
, "Only 1 and 30 allowed as timeout\n");
107 if (timeout
== 30 && wdt
->timeout
== 1) {
109 "Transition from fast to slow mode not allowed\n");
113 mutex_lock(&drv
->lock
);
116 gpio_set_value(drv
->gpios
[GPIO_WD_FAST
], 1);
118 gpio_set_value(drv
->gpios
[GPIO_WD_FAST
], 0);
120 wdt
->timeout
= timeout
;
122 mutex_unlock(&drv
->lock
);
127 static const struct watchdog_info a21_wdt_info
= {
128 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
129 .identity
= "MEN A21 Watchdog",
132 static const struct watchdog_ops a21_wdt_ops
= {
133 .owner
= THIS_MODULE
,
134 .start
= a21_wdt_start
,
135 .stop
= a21_wdt_stop
,
136 .ping
= a21_wdt_ping
,
137 .set_timeout
= a21_wdt_set_timeout
,
140 static struct watchdog_device a21_wdt
= {
141 .info
= &a21_wdt_info
,
147 static int a21_wdt_probe(struct platform_device
*pdev
)
149 struct device_node
*node
;
150 struct a21_wdt_drv
*drv
;
151 unsigned int reset
= 0;
156 drv
= devm_kzalloc(&pdev
->dev
, sizeof(struct a21_wdt_drv
), GFP_KERNEL
);
160 /* Fill GPIO pin array */
161 node
= pdev
->dev
.of_node
;
163 num_gpios
= of_gpio_count(node
);
164 if (num_gpios
!= NUM_GPIOS
) {
165 dev_err(&pdev
->dev
, "gpios DT property wrong, got %d want %d",
166 num_gpios
, NUM_GPIOS
);
170 for (i
= 0; i
< num_gpios
; i
++) {
173 val
= of_get_gpio(node
, i
);
180 /* Request the used GPIOs */
181 for (i
= 0; i
< num_gpios
; i
++) {
182 ret
= devm_gpio_request(&pdev
->dev
, drv
->gpios
[i
],
187 if (i
< GPIO_WD_RST0
)
188 ret
= gpio_direction_output(drv
->gpios
[i
],
189 gpio_get_value(drv
->gpios
[i
]));
190 else /* GPIO_WD_RST[0..2] are inputs */
191 ret
= gpio_direction_input(drv
->gpios
[i
]);
196 mutex_init(&drv
->lock
);
197 watchdog_init_timeout(&a21_wdt
, 30, &pdev
->dev
);
198 watchdog_set_nowayout(&a21_wdt
, nowayout
);
199 watchdog_set_drvdata(&a21_wdt
, drv
);
200 a21_wdt
.parent
= &pdev
->dev
;
202 reset
= a21_wdt_get_bootstatus(drv
);
204 a21_wdt
.bootstatus
|= WDIOF_EXTERN1
;
206 a21_wdt
.bootstatus
|= WDIOF_CARDRESET
;
208 a21_wdt
.bootstatus
|= WDIOF_POWERUNDER
;
210 a21_wdt
.bootstatus
|= WDIOF_EXTERN2
;
213 dev_set_drvdata(&pdev
->dev
, drv
);
215 ret
= watchdog_register_device(&a21_wdt
);
217 dev_err(&pdev
->dev
, "Cannot register watchdog device\n");
218 goto err_register_wd
;
221 dev_info(&pdev
->dev
, "MEN A21 watchdog timer driver enabled\n");
226 mutex_destroy(&drv
->lock
);
231 static int a21_wdt_remove(struct platform_device
*pdev
)
233 struct a21_wdt_drv
*drv
= dev_get_drvdata(&pdev
->dev
);
236 "Unregistering A21 watchdog driver, board may reboot\n");
238 watchdog_unregister_device(&drv
->wdt
);
240 mutex_destroy(&drv
->lock
);
245 static void a21_wdt_shutdown(struct platform_device
*pdev
)
247 struct a21_wdt_drv
*drv
= dev_get_drvdata(&pdev
->dev
);
249 gpio_set_value(drv
->gpios
[GPIO_WD_ENAB
], 0);
252 static const struct of_device_id a21_wdt_ids
[] = {
253 { .compatible
= "men,a021-wdt" },
256 MODULE_DEVICE_TABLE(of
, a21_wdt_ids
);
258 static struct platform_driver a21_wdt_driver
= {
259 .probe
= a21_wdt_probe
,
260 .remove
= a21_wdt_remove
,
261 .shutdown
= a21_wdt_shutdown
,
263 .name
= "a21-watchdog",
264 .of_match_table
= a21_wdt_ids
,
268 module_platform_driver(a21_wdt_driver
);
270 MODULE_AUTHOR("MEN Mikro Elektronik");
271 MODULE_DESCRIPTION("MEN A21 Watchdog");
272 MODULE_LICENSE("GPL");
273 MODULE_ALIAS("platform:a21-watchdog");