linux/drivers/watchdog/mena21_wdt.c
<<
>>
Prefs
   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.h>
  17#include <linux/of_gpio.h>
  18#include <linux/delay.h>
  19#include <linux/bitops.h>
  20
  21#define NUM_GPIOS 6
  22
  23enum 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
  32struct a21_wdt_drv {
  33        struct watchdog_device wdt;
  34        struct mutex lock;
  35        unsigned gpios[NUM_GPIOS];
  36};
  37
  38static bool nowayout = WATCHDOG_NOWAYOUT;
  39module_param(nowayout, bool, 0);
  40MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  41                            __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  42
  43static unsigned int a21_wdt_get_bootstatus(struct a21_wdt_drv *drv)
  44{
  45        int reset = 0;
  46
  47        reset |= gpio_get_value(drv->gpios[GPIO_WD_RST0]) ? (1 << 0) : 0;
  48        reset |= gpio_get_value(drv->gpios[GPIO_WD_RST1]) ? (1 << 1) : 0;
  49        reset |= gpio_get_value(drv->gpios[GPIO_WD_RST2]) ? (1 << 2) : 0;
  50
  51        return reset;
  52}
  53
  54static int a21_wdt_start(struct watchdog_device *wdt)
  55{
  56        struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  57
  58        mutex_lock(&drv->lock);
  59
  60        gpio_set_value(drv->gpios[GPIO_WD_ENAB], 1);
  61
  62        mutex_unlock(&drv->lock);
  63
  64        return 0;
  65}
  66
  67static int a21_wdt_stop(struct watchdog_device *wdt)
  68{
  69        struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  70
  71        mutex_lock(&drv->lock);
  72
  73        gpio_set_value(drv->gpios[GPIO_WD_ENAB], 0);
  74
  75        mutex_unlock(&drv->lock);
  76
  77        return 0;
  78}
  79
  80static int a21_wdt_ping(struct watchdog_device *wdt)
  81{
  82        struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  83
  84        mutex_lock(&drv->lock);
  85
  86        gpio_set_value(drv->gpios[GPIO_WD_TRIG], 0);
  87        ndelay(10);
  88        gpio_set_value(drv->gpios[GPIO_WD_TRIG], 1);
  89
  90        mutex_unlock(&drv->lock);
  91
  92        return 0;
  93}
  94
  95static int a21_wdt_set_timeout(struct watchdog_device *wdt,
  96                               unsigned int timeout)
  97{
  98        struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  99
 100        if (timeout != 1 && timeout != 30) {
 101                dev_err(wdt->parent, "Only 1 and 30 allowed as timeout\n");
 102                return -EINVAL;
 103        }
 104
 105        if (timeout == 30 && wdt->timeout == 1) {
 106                dev_err(wdt->parent,
 107                        "Transition from fast to slow mode not allowed\n");
 108                return -EINVAL;
 109        }
 110
 111        mutex_lock(&drv->lock);
 112
 113        if (timeout == 1)
 114                gpio_set_value(drv->gpios[GPIO_WD_FAST], 1);
 115        else
 116                gpio_set_value(drv->gpios[GPIO_WD_FAST], 0);
 117
 118        wdt->timeout = timeout;
 119
 120        mutex_unlock(&drv->lock);
 121
 122        return 0;
 123}
 124
 125static const struct watchdog_info a21_wdt_info = {
 126        .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
 127        .identity = "MEN A21 Watchdog",
 128};
 129
 130static const struct watchdog_ops a21_wdt_ops = {
 131        .owner = THIS_MODULE,
 132        .start = a21_wdt_start,
 133        .stop = a21_wdt_stop,
 134        .ping = a21_wdt_ping,
 135        .set_timeout = a21_wdt_set_timeout,
 136};
 137
 138static struct watchdog_device a21_wdt = {
 139        .info = &a21_wdt_info,
 140        .ops = &a21_wdt_ops,
 141        .min_timeout = 1,
 142        .max_timeout = 30,
 143};
 144
 145static int a21_wdt_probe(struct platform_device *pdev)
 146{
 147        struct device_node *node;
 148        struct a21_wdt_drv *drv;
 149        unsigned int reset = 0;
 150        int num_gpios;
 151        int ret;
 152        int i;
 153
 154        drv = devm_kzalloc(&pdev->dev, sizeof(struct a21_wdt_drv), GFP_KERNEL);
 155        if (!drv)
 156                return -ENOMEM;
 157
 158        /* Fill GPIO pin array */
 159        node = pdev->dev.of_node;
 160
 161        num_gpios = of_gpio_count(node);
 162        if (num_gpios != NUM_GPIOS) {
 163                dev_err(&pdev->dev, "gpios DT property wrong, got %d want %d",
 164                        num_gpios, NUM_GPIOS);
 165                return -ENODEV;
 166        }
 167
 168        for (i = 0; i < num_gpios; i++) {
 169                int val;
 170
 171                val = of_get_gpio(node, i);
 172                if (val < 0)
 173                        return val;
 174
 175                drv->gpios[i] = val;
 176        }
 177
 178        /* Request the used GPIOs */
 179        for (i = 0; i < num_gpios; i++) {
 180                ret = devm_gpio_request(&pdev->dev, drv->gpios[i],
 181                                        "MEN A21 Watchdog");
 182                if (ret)
 183                        return ret;
 184
 185                if (i < GPIO_WD_RST0)
 186                        ret = gpio_direction_output(drv->gpios[i],
 187                                                gpio_get_value(drv->gpios[i]));
 188                else            /* GPIO_WD_RST[0..2] are inputs */
 189                        ret = gpio_direction_input(drv->gpios[i]);
 190                if (ret)
 191                        return ret;
 192        }
 193
 194        mutex_init(&drv->lock);
 195        watchdog_init_timeout(&a21_wdt, 30, &pdev->dev);
 196        watchdog_set_nowayout(&a21_wdt, nowayout);
 197        watchdog_set_drvdata(&a21_wdt, drv);
 198        a21_wdt.parent = &pdev->dev;
 199
 200        reset = a21_wdt_get_bootstatus(drv);
 201        if (reset == 2)
 202                a21_wdt.bootstatus |= WDIOF_EXTERN1;
 203        else if (reset == 4)
 204                a21_wdt.bootstatus |= WDIOF_CARDRESET;
 205        else if (reset == 5)
 206                a21_wdt.bootstatus |= WDIOF_POWERUNDER;
 207        else if (reset == 7)
 208                a21_wdt.bootstatus |= WDIOF_EXTERN2;
 209
 210        drv->wdt = a21_wdt;
 211        dev_set_drvdata(&pdev->dev, drv);
 212
 213        ret = devm_watchdog_register_device(&pdev->dev, &a21_wdt);
 214        if (ret) {
 215                dev_err(&pdev->dev, "Cannot register watchdog device\n");
 216                return ret;
 217        }
 218
 219        dev_info(&pdev->dev, "MEN A21 watchdog timer driver enabled\n");
 220
 221        return 0;
 222}
 223
 224static void a21_wdt_shutdown(struct platform_device *pdev)
 225{
 226        struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev);
 227
 228        gpio_set_value(drv->gpios[GPIO_WD_ENAB], 0);
 229}
 230
 231static const struct of_device_id a21_wdt_ids[] = {
 232        { .compatible = "men,a021-wdt" },
 233        { },
 234};
 235MODULE_DEVICE_TABLE(of, a21_wdt_ids);
 236
 237static struct platform_driver a21_wdt_driver = {
 238        .probe = a21_wdt_probe,
 239        .shutdown = a21_wdt_shutdown,
 240        .driver = {
 241                .name = "a21-watchdog",
 242                .of_match_table = a21_wdt_ids,
 243        },
 244};
 245
 246module_platform_driver(a21_wdt_driver);
 247
 248MODULE_AUTHOR("MEN Mikro Elektronik");
 249MODULE_DESCRIPTION("MEN A21 Watchdog");
 250MODULE_LICENSE("GPL");
 251MODULE_ALIAS("platform:a21-watchdog");
 252