0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/moduleparam.h>
0010 #include <linux/types.h>
0011 #include <linux/kernel.h>
0012 #include <linux/slab.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/watchdog.h>
0015 #include <linux/uaccess.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/delay.h>
0018 #include <linux/bitops.h>
0019 #include <linux/of.h>
0020
0021 #define NUM_GPIOS 6
0022
0023 enum a21_wdt_gpios {
0024 GPIO_WD_ENAB,
0025 GPIO_WD_FAST,
0026 GPIO_WD_TRIG,
0027 GPIO_WD_RST0,
0028 GPIO_WD_RST1,
0029 GPIO_WD_RST2,
0030 };
0031
0032 struct a21_wdt_drv {
0033 struct watchdog_device wdt;
0034 struct gpio_desc *gpios[NUM_GPIOS];
0035 };
0036
0037 static bool nowayout = WATCHDOG_NOWAYOUT;
0038 module_param(nowayout, bool, 0);
0039 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
0040 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0041
0042 static unsigned int a21_wdt_get_bootstatus(struct a21_wdt_drv *drv)
0043 {
0044 int reset = 0;
0045
0046 reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST0]) ? (1 << 0) : 0;
0047 reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST1]) ? (1 << 1) : 0;
0048 reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST2]) ? (1 << 2) : 0;
0049
0050 return reset;
0051 }
0052
0053 static int a21_wdt_start(struct watchdog_device *wdt)
0054 {
0055 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
0056
0057 gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 1);
0058
0059 return 0;
0060 }
0061
0062 static int a21_wdt_stop(struct watchdog_device *wdt)
0063 {
0064 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
0065
0066 gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0);
0067
0068 return 0;
0069 }
0070
0071 static int a21_wdt_ping(struct watchdog_device *wdt)
0072 {
0073 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
0074
0075 gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 0);
0076 ndelay(10);
0077 gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 1);
0078
0079 return 0;
0080 }
0081
0082 static int a21_wdt_set_timeout(struct watchdog_device *wdt,
0083 unsigned int timeout)
0084 {
0085 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
0086
0087 if (timeout != 1 && timeout != 30) {
0088 dev_err(wdt->parent, "Only 1 and 30 allowed as timeout\n");
0089 return -EINVAL;
0090 }
0091
0092 if (timeout == 30 && wdt->timeout == 1) {
0093 dev_err(wdt->parent,
0094 "Transition from fast to slow mode not allowed\n");
0095 return -EINVAL;
0096 }
0097
0098 if (timeout == 1)
0099 gpiod_set_value(drv->gpios[GPIO_WD_FAST], 1);
0100 else
0101 gpiod_set_value(drv->gpios[GPIO_WD_FAST], 0);
0102
0103 wdt->timeout = timeout;
0104
0105 return 0;
0106 }
0107
0108 static const struct watchdog_info a21_wdt_info = {
0109 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
0110 .identity = "MEN A21 Watchdog",
0111 };
0112
0113 static const struct watchdog_ops a21_wdt_ops = {
0114 .owner = THIS_MODULE,
0115 .start = a21_wdt_start,
0116 .stop = a21_wdt_stop,
0117 .ping = a21_wdt_ping,
0118 .set_timeout = a21_wdt_set_timeout,
0119 };
0120
0121 static struct watchdog_device a21_wdt = {
0122 .info = &a21_wdt_info,
0123 .ops = &a21_wdt_ops,
0124 .min_timeout = 1,
0125 .max_timeout = 30,
0126 };
0127
0128 static int a21_wdt_probe(struct platform_device *pdev)
0129 {
0130 struct device *dev = &pdev->dev;
0131 struct a21_wdt_drv *drv;
0132 unsigned int reset = 0;
0133 int num_gpios;
0134 int ret;
0135 int i;
0136
0137 drv = devm_kzalloc(dev, sizeof(struct a21_wdt_drv), GFP_KERNEL);
0138 if (!drv)
0139 return -ENOMEM;
0140
0141 num_gpios = gpiod_count(dev, NULL);
0142 if (num_gpios != NUM_GPIOS) {
0143 dev_err(dev, "gpios DT property wrong, got %d want %d",
0144 num_gpios, NUM_GPIOS);
0145 return -ENODEV;
0146 }
0147
0148
0149 for (i = 0; i < num_gpios; i++) {
0150 enum gpiod_flags gflags;
0151
0152 if (i < GPIO_WD_RST0)
0153 gflags = GPIOD_ASIS;
0154 else
0155 gflags = GPIOD_IN;
0156 drv->gpios[i] = devm_gpiod_get_index(dev, NULL, i, gflags);
0157 if (IS_ERR(drv->gpios[i]))
0158 return PTR_ERR(drv->gpios[i]);
0159
0160 gpiod_set_consumer_name(drv->gpios[i], "MEN A21 Watchdog");
0161
0162
0163
0164
0165
0166 if (i < GPIO_WD_RST0) {
0167 int val;
0168
0169 val = gpiod_get_value(drv->gpios[i]);
0170 gpiod_direction_output(drv->gpios[i], val);
0171 }
0172 }
0173
0174 watchdog_init_timeout(&a21_wdt, 30, dev);
0175 watchdog_set_nowayout(&a21_wdt, nowayout);
0176 watchdog_set_drvdata(&a21_wdt, drv);
0177 a21_wdt.parent = dev;
0178
0179 reset = a21_wdt_get_bootstatus(drv);
0180 if (reset == 2)
0181 a21_wdt.bootstatus |= WDIOF_EXTERN1;
0182 else if (reset == 4)
0183 a21_wdt.bootstatus |= WDIOF_CARDRESET;
0184 else if (reset == 5)
0185 a21_wdt.bootstatus |= WDIOF_POWERUNDER;
0186 else if (reset == 7)
0187 a21_wdt.bootstatus |= WDIOF_EXTERN2;
0188
0189 drv->wdt = a21_wdt;
0190 dev_set_drvdata(dev, drv);
0191
0192 ret = devm_watchdog_register_device(dev, &a21_wdt);
0193 if (ret)
0194 return ret;
0195
0196 dev_info(dev, "MEN A21 watchdog timer driver enabled\n");
0197
0198 return 0;
0199 }
0200
0201 static void a21_wdt_shutdown(struct platform_device *pdev)
0202 {
0203 struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev);
0204
0205 gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0);
0206 }
0207
0208 static const struct of_device_id a21_wdt_ids[] = {
0209 { .compatible = "men,a021-wdt" },
0210 { },
0211 };
0212 MODULE_DEVICE_TABLE(of, a21_wdt_ids);
0213
0214 static struct platform_driver a21_wdt_driver = {
0215 .probe = a21_wdt_probe,
0216 .shutdown = a21_wdt_shutdown,
0217 .driver = {
0218 .name = "a21-watchdog",
0219 .of_match_table = a21_wdt_ids,
0220 },
0221 };
0222
0223 module_platform_driver(a21_wdt_driver);
0224
0225 MODULE_AUTHOR("MEN Mikro Elektronik");
0226 MODULE_DESCRIPTION("MEN A21 Watchdog");
0227 MODULE_LICENSE("GPL");
0228 MODULE_ALIAS("platform:a21-watchdog");