Marcus Folkesson | 2e62c49 | 2018-03-16 16:14:11 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Watchdog driver for the A21 VME CPU Boards |
| 4 | * |
| 5 | * Copyright (C) 2013 MEN Mikro Elektronik Nuernberg GmbH |
| 6 | * |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 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> |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 16 | #include <linux/gpio/consumer.h> |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 17 | #include <linux/delay.h> |
| 18 | #include <linux/bitops.h> |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 19 | #include <linux/of.h> |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 20 | |
| 21 | #define NUM_GPIOS 6 |
| 22 | |
| 23 | enum 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 | |
| 32 | struct a21_wdt_drv { |
| 33 | struct watchdog_device wdt; |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 34 | struct gpio_desc *gpios[NUM_GPIOS]; |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 35 | }; |
| 36 | |
| 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) ")"); |
| 41 | |
| 42 | static unsigned int a21_wdt_get_bootstatus(struct a21_wdt_drv *drv) |
| 43 | { |
| 44 | int reset = 0; |
| 45 | |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 46 | reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST0]) ? (1 << 0) : 0; |
| 47 | reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST1]) ? (1 << 1) : 0; |
| 48 | reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST2]) ? (1 << 2) : 0; |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 49 | |
| 50 | return reset; |
| 51 | } |
| 52 | |
| 53 | static int a21_wdt_start(struct watchdog_device *wdt) |
| 54 | { |
| 55 | struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt); |
| 56 | |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 57 | gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 1); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 58 | |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int a21_wdt_stop(struct watchdog_device *wdt) |
| 63 | { |
| 64 | struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt); |
| 65 | |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 66 | gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 67 | |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int a21_wdt_ping(struct watchdog_device *wdt) |
| 72 | { |
| 73 | struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt); |
| 74 | |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 75 | gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 0); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 76 | ndelay(10); |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 77 | gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 1); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 78 | |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static 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) { |
Guenter Roeck | 0735236 | 2015-12-24 14:22:03 -0800 | [diff] [blame] | 88 | dev_err(wdt->parent, "Only 1 and 30 allowed as timeout\n"); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 89 | return -EINVAL; |
| 90 | } |
| 91 | |
| 92 | if (timeout == 30 && wdt->timeout == 1) { |
Guenter Roeck | 0735236 | 2015-12-24 14:22:03 -0800 | [diff] [blame] | 93 | dev_err(wdt->parent, |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 94 | "Transition from fast to slow mode not allowed\n"); |
| 95 | return -EINVAL; |
| 96 | } |
| 97 | |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 98 | if (timeout == 1) |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 99 | gpiod_set_value(drv->gpios[GPIO_WD_FAST], 1); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 100 | else |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 101 | gpiod_set_value(drv->gpios[GPIO_WD_FAST], 0); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 102 | |
| 103 | wdt->timeout = timeout; |
| 104 | |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static const struct watchdog_info a21_wdt_info = { |
| 109 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, |
| 110 | .identity = "MEN A21 Watchdog", |
| 111 | }; |
| 112 | |
| 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, |
| 119 | }; |
| 120 | |
| 121 | static 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 | |
| 128 | static int a21_wdt_probe(struct platform_device *pdev) |
| 129 | { |
Guenter Roeck | 94ac20d | 2019-04-09 10:23:40 -0700 | [diff] [blame] | 130 | struct device *dev = &pdev->dev; |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 131 | struct a21_wdt_drv *drv; |
| 132 | unsigned int reset = 0; |
| 133 | int num_gpios; |
| 134 | int ret; |
| 135 | int i; |
| 136 | |
Guenter Roeck | 94ac20d | 2019-04-09 10:23:40 -0700 | [diff] [blame] | 137 | drv = devm_kzalloc(dev, sizeof(struct a21_wdt_drv), GFP_KERNEL); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 138 | if (!drv) |
| 139 | return -ENOMEM; |
| 140 | |
Guenter Roeck | 94ac20d | 2019-04-09 10:23:40 -0700 | [diff] [blame] | 141 | num_gpios = gpiod_count(dev, NULL); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 142 | if (num_gpios != NUM_GPIOS) { |
Guenter Roeck | 94ac20d | 2019-04-09 10:23:40 -0700 | [diff] [blame] | 143 | dev_err(dev, "gpios DT property wrong, got %d want %d", |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 144 | num_gpios, NUM_GPIOS); |
| 145 | return -ENODEV; |
| 146 | } |
| 147 | |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 148 | /* Request the used GPIOs */ |
| 149 | for (i = 0; i < num_gpios; i++) { |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 150 | enum gpiod_flags gflags; |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 151 | |
| 152 | if (i < GPIO_WD_RST0) |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 153 | gflags = GPIOD_ASIS; |
| 154 | else |
| 155 | gflags = GPIOD_IN; |
Guenter Roeck | 94ac20d | 2019-04-09 10:23:40 -0700 | [diff] [blame] | 156 | drv->gpios[i] = devm_gpiod_get_index(dev, NULL, i, gflags); |
| 157 | if (IS_ERR(drv->gpios[i])) |
| 158 | return PTR_ERR(drv->gpios[i]); |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 159 | |
| 160 | gpiod_set_consumer_name(drv->gpios[i], "MEN A21 Watchdog"); |
| 161 | |
| 162 | /* |
| 163 | * Retrieve the initial value from the GPIOs that should be |
| 164 | * output, then set up the line as output with that value. |
| 165 | */ |
| 166 | if (i < GPIO_WD_RST0) { |
| 167 | int val; |
| 168 | |
| 169 | val = gpiod_get_value(drv->gpios[i]); |
| 170 | gpiod_direction_output(drv->gpios[i], val); |
| 171 | } |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Guenter Roeck | 94ac20d | 2019-04-09 10:23:40 -0700 | [diff] [blame] | 174 | watchdog_init_timeout(&a21_wdt, 30, dev); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 175 | watchdog_set_nowayout(&a21_wdt, nowayout); |
| 176 | watchdog_set_drvdata(&a21_wdt, drv); |
Guenter Roeck | 94ac20d | 2019-04-09 10:23:40 -0700 | [diff] [blame] | 177 | a21_wdt.parent = dev; |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 178 | |
| 179 | reset = a21_wdt_get_bootstatus(drv); |
| 180 | if (reset == 2) |
| 181 | a21_wdt.bootstatus |= WDIOF_EXTERN1; |
| 182 | else if (reset == 4) |
| 183 | a21_wdt.bootstatus |= WDIOF_CARDRESET; |
| 184 | else if (reset == 5) |
| 185 | a21_wdt.bootstatus |= WDIOF_POWERUNDER; |
| 186 | else if (reset == 7) |
| 187 | a21_wdt.bootstatus |= WDIOF_EXTERN2; |
| 188 | |
Johannes Thumshirn | 57337db | 2015-06-02 12:25:26 +0200 | [diff] [blame] | 189 | drv->wdt = a21_wdt; |
Guenter Roeck | 94ac20d | 2019-04-09 10:23:40 -0700 | [diff] [blame] | 190 | dev_set_drvdata(dev, drv); |
Johannes Thumshirn | 57337db | 2015-06-02 12:25:26 +0200 | [diff] [blame] | 191 | |
Guenter Roeck | 94ac20d | 2019-04-09 10:23:40 -0700 | [diff] [blame] | 192 | ret = devm_watchdog_register_device(dev, &a21_wdt); |
Wolfram Sang | eddeb07 | 2019-05-18 23:27:38 +0200 | [diff] [blame] | 193 | if (ret) |
Guenter Roeck | cb5c14e | 2017-01-10 15:21:52 -0800 | [diff] [blame] | 194 | return ret; |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 195 | |
Guenter Roeck | 94ac20d | 2019-04-09 10:23:40 -0700 | [diff] [blame] | 196 | dev_info(dev, "MEN A21 watchdog timer driver enabled\n"); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 197 | |
| 198 | return 0; |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static void a21_wdt_shutdown(struct platform_device *pdev) |
| 202 | { |
| 203 | struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev); |
| 204 | |
Linus Walleij | 22ec9bb | 2018-12-02 12:04:10 +0100 | [diff] [blame] | 205 | gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static const struct of_device_id a21_wdt_ids[] = { |
| 209 | { .compatible = "men,a021-wdt" }, |
| 210 | { }, |
| 211 | }; |
Luis de Bethencourt | c73318f | 2015-09-03 13:06:09 +0200 | [diff] [blame] | 212 | MODULE_DEVICE_TABLE(of, a21_wdt_ids); |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 213 | |
| 214 | static struct platform_driver a21_wdt_driver = { |
| 215 | .probe = a21_wdt_probe, |
Johannes Thumshirn | 26c57ef | 2013-06-18 17:19:45 +0200 | [diff] [blame] | 216 | .shutdown = a21_wdt_shutdown, |
| 217 | .driver = { |
| 218 | .name = "a21-watchdog", |
| 219 | .of_match_table = a21_wdt_ids, |
| 220 | }, |
| 221 | }; |
| 222 | |
| 223 | module_platform_driver(a21_wdt_driver); |
| 224 | |
| 225 | MODULE_AUTHOR("MEN Mikro Elektronik"); |
| 226 | MODULE_DESCRIPTION("MEN A21 Watchdog"); |
| 227 | MODULE_LICENSE("GPL"); |
| 228 | MODULE_ALIAS("platform:a21-watchdog"); |