1 // SPDX-License-Identifier: GPL-2.0+
3 * Watchdog driver for the A21 VME CPU Boards
5 * Copyright (C) 2013 MEN Mikro Elektronik Nuernberg GmbH
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>
33 struct watchdog_device wdt
;
34 unsigned gpios
[NUM_GPIOS
];
37 static bool nowayout
= WATCHDOG_NOWAYOUT
;
38 module_param(nowayout
, bool, 0);
39 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
40 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
42 static unsigned int a21_wdt_get_bootstatus(struct a21_wdt_drv
*drv
)
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;
53 static int a21_wdt_start(struct watchdog_device
*wdt
)
55 struct a21_wdt_drv
*drv
= watchdog_get_drvdata(wdt
);
57 gpio_set_value(drv
->gpios
[GPIO_WD_ENAB
], 1);
62 static int a21_wdt_stop(struct watchdog_device
*wdt
)
64 struct a21_wdt_drv
*drv
= watchdog_get_drvdata(wdt
);
66 gpio_set_value(drv
->gpios
[GPIO_WD_ENAB
], 0);
71 static int a21_wdt_ping(struct watchdog_device
*wdt
)
73 struct a21_wdt_drv
*drv
= watchdog_get_drvdata(wdt
);
75 gpio_set_value(drv
->gpios
[GPIO_WD_TRIG
], 0);
77 gpio_set_value(drv
->gpios
[GPIO_WD_TRIG
], 1);
82 static int a21_wdt_set_timeout(struct watchdog_device
*wdt
,
85 struct a21_wdt_drv
*drv
= watchdog_get_drvdata(wdt
);
87 if (timeout
!= 1 && timeout
!= 30) {
88 dev_err(wdt
->parent
, "Only 1 and 30 allowed as timeout\n");
92 if (timeout
== 30 && wdt
->timeout
== 1) {
94 "Transition from fast to slow mode not allowed\n");
99 gpio_set_value(drv
->gpios
[GPIO_WD_FAST
], 1);
101 gpio_set_value(drv
->gpios
[GPIO_WD_FAST
], 0);
103 wdt
->timeout
= timeout
;
108 static const struct watchdog_info a21_wdt_info
= {
109 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
110 .identity
= "MEN A21 Watchdog",
113 static 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
,
121 static struct watchdog_device a21_wdt
= {
122 .info
= &a21_wdt_info
,
128 static int a21_wdt_probe(struct platform_device
*pdev
)
130 struct device_node
*node
;
131 struct a21_wdt_drv
*drv
;
132 unsigned int reset
= 0;
137 drv
= devm_kzalloc(&pdev
->dev
, sizeof(struct a21_wdt_drv
), GFP_KERNEL
);
141 /* Fill GPIO pin array */
142 node
= pdev
->dev
.of_node
;
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
);
151 for (i
= 0; i
< num_gpios
; i
++) {
154 val
= of_get_gpio(node
, i
);
161 /* Request the used GPIOs */
162 for (i
= 0; i
< num_gpios
; i
++) {
163 ret
= devm_gpio_request(&pdev
->dev
, drv
->gpios
[i
],
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
]);
177 watchdog_init_timeout(&a21_wdt
, 30, &pdev
->dev
);
178 watchdog_set_nowayout(&a21_wdt
, nowayout
);
179 watchdog_set_drvdata(&a21_wdt
, drv
);
180 a21_wdt
.parent
= &pdev
->dev
;
182 reset
= a21_wdt_get_bootstatus(drv
);
184 a21_wdt
.bootstatus
|= WDIOF_EXTERN1
;
186 a21_wdt
.bootstatus
|= WDIOF_CARDRESET
;
188 a21_wdt
.bootstatus
|= WDIOF_POWERUNDER
;
190 a21_wdt
.bootstatus
|= WDIOF_EXTERN2
;
193 dev_set_drvdata(&pdev
->dev
, drv
);
195 ret
= devm_watchdog_register_device(&pdev
->dev
, &a21_wdt
);
197 dev_err(&pdev
->dev
, "Cannot register watchdog device\n");
201 dev_info(&pdev
->dev
, "MEN A21 watchdog timer driver enabled\n");
206 static void a21_wdt_shutdown(struct platform_device
*pdev
)
208 struct a21_wdt_drv
*drv
= dev_get_drvdata(&pdev
->dev
);
210 gpio_set_value(drv
->gpios
[GPIO_WD_ENAB
], 0);
213 static const struct of_device_id a21_wdt_ids
[] = {
214 { .compatible
= "men,a021-wdt" },
217 MODULE_DEVICE_TABLE(of
, a21_wdt_ids
);
219 static struct platform_driver a21_wdt_driver
= {
220 .probe
= a21_wdt_probe
,
221 .shutdown
= a21_wdt_shutdown
,
223 .name
= "a21-watchdog",
224 .of_match_table
= a21_wdt_ids
,
228 module_platform_driver(a21_wdt_driver
);
230 MODULE_AUTHOR("MEN Mikro Elektronik");
231 MODULE_DESCRIPTION("MEN A21 Watchdog");
232 MODULE_LICENSE("GPL");
233 MODULE_ALIAS("platform:a21-watchdog");