cregit-Linux how code gets into the kernel

Release 4.11 drivers/watchdog/gpio_wdt.c

Directory: drivers/watchdog
/*
 * Driver for watchdog device controlled through GPIO-line
 *
 * Author: 2013, Alexander Shiyan <shc_work@mail.ru>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/err.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <linux/watchdog.h>


#define SOFT_TIMEOUT_MIN	1

#define SOFT_TIMEOUT_DEF	60

#define SOFT_TIMEOUT_MAX	0xffff

enum {
	
HW_ALGO_TOGGLE,
	
HW_ALGO_LEVEL,
};


struct gpio_wdt_priv {
	
int			gpio;
	
bool			active_low;
	
bool			state;
	
bool			always_running;
	
bool			armed;
	
unsigned int		hw_algo;
	
unsigned int		hw_margin;
	
unsigned long		last_jiffies;
	
struct timer_list	timer;
	
struct watchdog_device	wdd;
};


static void gpio_wdt_disable(struct gpio_wdt_priv *priv) { gpio_set_value_cansleep(priv->gpio, !priv->active_low); /* Put GPIO back to tristate */ if (priv->hw_algo == HW_ALGO_TOGGLE) gpio_direction_input(priv->gpio); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Shiyan39100.00%1100.00%
Total39100.00%1100.00%


static void gpio_wdt_hwping(unsigned long data) { struct watchdog_device *wdd = (struct watchdog_device *)data; struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd); if (priv->armed && time_after(jiffies, priv->last_jiffies + msecs_to_jiffies(wdd->timeout * 1000))) { dev_crit(wdd->parent, "Timer expired. System will reboot soon!\n"); return; } /* Restart timer */ mod_timer(&priv->timer, jiffies + priv->hw_margin); switch (priv->hw_algo) { case HW_ALGO_TOGGLE: /* Toggle output pin */ priv->state = !priv->state; gpio_set_value_cansleep(priv->gpio, priv->state); break; case HW_ALGO_LEVEL: /* Pulse */ gpio_set_value_cansleep(priv->gpio, !priv->active_low); udelay(1); gpio_set_value_cansleep(priv->gpio, priv->active_low); break; } }

Contributors

PersonTokensPropCommitsCommitProp
Uwe Kleine-König14899.33%150.00%
Guenter Roeck10.67%150.00%
Total149100.00%2100.00%


static void gpio_wdt_start_impl(struct gpio_wdt_priv *priv) { priv->state = priv->active_low; gpio_direction_output(priv->gpio, priv->state); priv->last_jiffies = jiffies; gpio_wdt_hwping((unsigned long)&priv->wdd); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Shiyan3777.08%133.33%
Uwe Kleine-König612.50%133.33%
Mike Looijmans510.42%133.33%
Total48100.00%3100.00%


static int gpio_wdt_start(struct watchdog_device *wdd) { struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd); gpio_wdt_start_impl(priv); priv->armed = true; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Mike Looijmans3188.57%150.00%
Alexander Shiyan411.43%150.00%
Total35100.00%2100.00%


static int gpio_wdt_stop(struct watchdog_device *wdd) { struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd); priv->armed = false; if (!priv->always_running) { mod_timer(&priv->timer, 0); gpio_wdt_disable(priv); } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Shiyan3972.22%150.00%
Mike Looijmans1527.78%150.00%
Total54100.00%2100.00%


static int gpio_wdt_ping(struct watchdog_device *wdd) { struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd); priv->last_jiffies = jiffies; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Shiyan30100.00%1100.00%
Total30100.00%1100.00%


static int gpio_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t) { wdd->timeout = t; return gpio_wdt_ping(wdd); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Shiyan27100.00%1100.00%
Total27100.00%1100.00%

static const struct watchdog_info gpio_wdt_ident = { .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT, .identity = "GPIO Watchdog", }; static const struct watchdog_ops gpio_wdt_ops = { .owner = THIS_MODULE, .start = gpio_wdt_start, .stop = gpio_wdt_stop, .ping = gpio_wdt_ping, .set_timeout = gpio_wdt_set_timeout, };
static int gpio_wdt_probe(struct platform_device *pdev) { struct gpio_wdt_priv *priv; enum of_gpio_flags flags; unsigned int hw_margin; unsigned long f = 0; const char *algo; int ret; priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); if (!priv) return -ENOMEM; platform_set_drvdata(pdev, priv); priv->gpio = of_get_gpio_flags(pdev->dev.of_node, 0, &flags); if (!gpio_is_valid(priv->gpio)) return priv->gpio; priv->active_low = flags & OF_GPIO_ACTIVE_LOW; ret = of_property_read_string(pdev->dev.of_node, "hw_algo", &algo); if (ret) return ret; if (!strcmp(algo, "toggle")) { priv->hw_algo = HW_ALGO_TOGGLE; f = GPIOF_IN; } else if (!strcmp(algo, "level")) { priv->hw_algo = HW_ALGO_LEVEL; f = priv->active_low ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW; } else { return -EINVAL; } ret = devm_gpio_request_one(&pdev->dev, priv->gpio, f, dev_name(&pdev->dev)); if (ret) return ret; ret = of_property_read_u32(pdev->dev.of_node, "hw_margin_ms", &hw_margin); if (ret) return ret; /* Disallow values lower than 2 and higher than 65535 ms */ if (hw_margin < 2 || hw_margin > 65535) return -EINVAL; /* Use safe value (1/2 of real timeout) */ priv->hw_margin = msecs_to_jiffies(hw_margin / 2); priv->always_running = of_property_read_bool(pdev->dev.of_node, "always-running"); watchdog_set_drvdata(&priv->wdd, priv); priv->wdd.info = &gpio_wdt_ident; priv->wdd.ops = &gpio_wdt_ops; priv->wdd.min_timeout = SOFT_TIMEOUT_MIN; priv->wdd.max_timeout = SOFT_TIMEOUT_MAX; priv->wdd.parent = &pdev->dev; if (watchdog_init_timeout(&priv->wdd, 0, &pdev->dev) < 0) priv->wdd.timeout = SOFT_TIMEOUT_DEF; setup_timer(&priv->timer, gpio_wdt_hwping, (unsigned long)&priv->wdd); watchdog_stop_on_reboot(&priv->wdd); ret = watchdog_register_device(&priv->wdd); if (ret) return ret; if (priv->always_running) gpio_wdt_start_impl(priv); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Shiyan37587.21%116.67%
Mike Looijmans296.74%116.67%
Pratyush Anand112.56%116.67%
Wei Yongjun71.63%116.67%
Damien Riegel61.40%116.67%
Uwe Kleine-König20.47%116.67%
Total430100.00%6100.00%


static int gpio_wdt_remove(struct platform_device *pdev) { struct gpio_wdt_priv *priv = platform_get_drvdata(pdev); del_timer_sync(&priv->timer); watchdog_unregister_device(&priv->wdd); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Shiyan40100.00%1100.00%
Total40100.00%1100.00%

static const struct of_device_id gpio_wdt_dt_ids[] = { { .compatible = "linux,wdt-gpio", }, { } }; MODULE_DEVICE_TABLE(of, gpio_wdt_dt_ids); static struct platform_driver gpio_wdt_driver = { .driver = { .name = "gpio-wdt", .of_match_table = gpio_wdt_dt_ids, }, .probe = gpio_wdt_probe, .remove = gpio_wdt_remove, }; #ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
static int __init gpio_wdt_init(void) { return platform_driver_register(&gpio_wdt_driver); }

Contributors

PersonTokensPropCommitsCommitProp
Jean-Baptiste Theou16100.00%1100.00%
Total16100.00%1100.00%

arch_initcall(gpio_wdt_init); #else module_platform_driver(gpio_wdt_driver); #endif MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>"); MODULE_DESCRIPTION("GPIO Watchdog"); MODULE_LICENSE("GPL");

Overall Contributors

PersonTokensPropCommitsCommitProp
Alexander Shiyan79572.94%111.11%
Uwe Kleine-König15614.31%222.22%
Mike Looijmans867.89%111.11%
Jean-Baptiste Theou282.57%111.11%
Pratyush Anand111.01%111.11%
Wei Yongjun70.64%111.11%
Damien Riegel60.55%111.11%
Guenter Roeck10.09%111.11%
Total1090100.00%9100.00%
Directory: drivers/watchdog
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.