1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Watchdog driver for the A21 VME CPU Boards 4 * 5 * Copyright (C) 2013 MEN Mikro Elektronik Nuernberg GmbH 6 * 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/consumer.h> 17 #include <linux/delay.h> 18 #include <linux/bitops.h> 19 #include <linux/of.h> 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; 34 struct gpio_desc *gpios[NUM_GPIOS]; 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 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; 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 57 gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 1); 58 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 66 gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0); 67 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 75 gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 0); 76 ndelay(10); 77 gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 1); 78 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) { 88 dev_err(wdt->parent, "Only 1 and 30 allowed as timeout\n"); 89 return -EINVAL; 90 } 91 92 if (timeout == 30 && wdt->timeout == 1) { 93 dev_err(wdt->parent, 94 "Transition from fast to slow mode not allowed\n"); 95 return -EINVAL; 96 } 97 98 if (timeout == 1) 99 gpiod_set_value(drv->gpios[GPIO_WD_FAST], 1); 100 else 101 gpiod_set_value(drv->gpios[GPIO_WD_FAST], 0); 102 103 wdt->timeout = timeout; 104 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 { 130 struct a21_wdt_drv *drv; 131 unsigned int reset = 0; 132 int num_gpios; 133 int ret; 134 int i; 135 136 drv = devm_kzalloc(&pdev->dev, sizeof(struct a21_wdt_drv), GFP_KERNEL); 137 if (!drv) 138 return -ENOMEM; 139 140 num_gpios = gpiod_count(&pdev->dev, NULL); 141 if (num_gpios != NUM_GPIOS) { 142 dev_err(&pdev->dev, "gpios DT property wrong, got %d want %d", 143 num_gpios, NUM_GPIOS); 144 return -ENODEV; 145 } 146 147 /* Request the used GPIOs */ 148 for (i = 0; i < num_gpios; i++) { 149 enum gpiod_flags gflags; 150 151 if (i < GPIO_WD_RST0) 152 gflags = GPIOD_ASIS; 153 else 154 gflags = GPIOD_IN; 155 drv->gpios[i] = devm_gpiod_get_index(&pdev->dev, NULL, i, 156 gflags); 157 if (IS_ERR(drv->gpios[i])) { 158 ret = PTR_ERR(drv->gpios[i]); 159 return ret; 160 } 161 162 gpiod_set_consumer_name(drv->gpios[i], "MEN A21 Watchdog"); 163 164 /* 165 * Retrieve the initial value from the GPIOs that should be 166 * output, then set up the line as output with that value. 167 */ 168 if (i < GPIO_WD_RST0) { 169 int val; 170 171 val = gpiod_get_value(drv->gpios[i]); 172 gpiod_direction_output(drv->gpios[i], val); 173 } 174 } 175 176 watchdog_init_timeout(&a21_wdt, 30, &pdev->dev); 177 watchdog_set_nowayout(&a21_wdt, nowayout); 178 watchdog_set_drvdata(&a21_wdt, drv); 179 a21_wdt.parent = &pdev->dev; 180 181 reset = a21_wdt_get_bootstatus(drv); 182 if (reset == 2) 183 a21_wdt.bootstatus |= WDIOF_EXTERN1; 184 else if (reset == 4) 185 a21_wdt.bootstatus |= WDIOF_CARDRESET; 186 else if (reset == 5) 187 a21_wdt.bootstatus |= WDIOF_POWERUNDER; 188 else if (reset == 7) 189 a21_wdt.bootstatus |= WDIOF_EXTERN2; 190 191 drv->wdt = a21_wdt; 192 dev_set_drvdata(&pdev->dev, drv); 193 194 ret = devm_watchdog_register_device(&pdev->dev, &a21_wdt); 195 if (ret) { 196 dev_err(&pdev->dev, "Cannot register watchdog device\n"); 197 return ret; 198 } 199 200 dev_info(&pdev->dev, "MEN A21 watchdog timer driver enabled\n"); 201 202 return 0; 203 } 204 205 static void a21_wdt_shutdown(struct platform_device *pdev) 206 { 207 struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev); 208 209 gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0); 210 } 211 212 static const struct of_device_id a21_wdt_ids[] = { 213 { .compatible = "men,a021-wdt" }, 214 { }, 215 }; 216 MODULE_DEVICE_TABLE(of, a21_wdt_ids); 217 218 static struct platform_driver a21_wdt_driver = { 219 .probe = a21_wdt_probe, 220 .shutdown = a21_wdt_shutdown, 221 .driver = { 222 .name = "a21-watchdog", 223 .of_match_table = a21_wdt_ids, 224 }, 225 }; 226 227 module_platform_driver(a21_wdt_driver); 228 229 MODULE_AUTHOR("MEN Mikro Elektronik"); 230 MODULE_DESCRIPTION("MEN A21 Watchdog"); 231 MODULE_LICENSE("GPL"); 232 MODULE_ALIAS("platform:a21-watchdog"); 233