Fabio Estevam | 2671a3a | 2018-07-10 11:21:23 -0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Watchdog driver for the RTC based watchdog in STMP3xxx and i.MX23/28 |
| 4 | * |
Wolfram Sang | cf82f52 | 2015-04-20 15:51:44 +0200 | [diff] [blame] | 5 | * Author: Wolfram Sang <kernel@pengutronix.de> |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) 2011-12 Wolfram Sang, Pengutronix |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 8 | */ |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 11 | #include <linux/watchdog.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/stmp3xxx_rtc_wdt.h> |
Harald Geyer | 8d2fa17 | 2015-12-12 14:32:15 +0000 | [diff] [blame] | 14 | #include <linux/notifier.h> |
| 15 | #include <linux/reboot.h> |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 16 | |
| 17 | #define WDOG_TICK_RATE 1000 /* 1 kHz clock */ |
| 18 | #define STMP3XXX_DEFAULT_TIMEOUT 19 |
| 19 | #define STMP3XXX_MAX_TIMEOUT (UINT_MAX / WDOG_TICK_RATE) |
| 20 | |
| 21 | static int heartbeat = STMP3XXX_DEFAULT_TIMEOUT; |
| 22 | module_param(heartbeat, uint, 0); |
| 23 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat period in seconds from 1 to " |
| 24 | __MODULE_STRING(STMP3XXX_MAX_TIMEOUT) ", default " |
| 25 | __MODULE_STRING(STMP3XXX_DEFAULT_TIMEOUT)); |
| 26 | |
| 27 | static int wdt_start(struct watchdog_device *wdd) |
| 28 | { |
| 29 | struct device *dev = watchdog_get_drvdata(wdd); |
Jingoo Han | bc8fdfb | 2013-07-30 19:58:51 +0900 | [diff] [blame] | 30 | struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev); |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 31 | |
| 32 | pdata->wdt_set_timeout(dev->parent, wdd->timeout * WDOG_TICK_RATE); |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static int wdt_stop(struct watchdog_device *wdd) |
| 37 | { |
| 38 | struct device *dev = watchdog_get_drvdata(wdd); |
Jingoo Han | bc8fdfb | 2013-07-30 19:58:51 +0900 | [diff] [blame] | 39 | struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev); |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 40 | |
| 41 | pdata->wdt_set_timeout(dev->parent, 0); |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static int wdt_set_timeout(struct watchdog_device *wdd, unsigned new_timeout) |
| 46 | { |
| 47 | wdd->timeout = new_timeout; |
| 48 | return wdt_start(wdd); |
| 49 | } |
| 50 | |
| 51 | static const struct watchdog_info stmp3xxx_wdt_ident = { |
| 52 | .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, |
| 53 | .identity = "STMP3XXX RTC Watchdog", |
| 54 | }; |
| 55 | |
| 56 | static const struct watchdog_ops stmp3xxx_wdt_ops = { |
| 57 | .owner = THIS_MODULE, |
| 58 | .start = wdt_start, |
| 59 | .stop = wdt_stop, |
| 60 | .set_timeout = wdt_set_timeout, |
| 61 | }; |
| 62 | |
| 63 | static struct watchdog_device stmp3xxx_wdd = { |
| 64 | .info = &stmp3xxx_wdt_ident, |
| 65 | .ops = &stmp3xxx_wdt_ops, |
| 66 | .min_timeout = 1, |
| 67 | .max_timeout = STMP3XXX_MAX_TIMEOUT, |
| 68 | .status = WATCHDOG_NOWAYOUT_INIT_STATUS, |
| 69 | }; |
| 70 | |
Harald Geyer | 8d2fa17 | 2015-12-12 14:32:15 +0000 | [diff] [blame] | 71 | static int wdt_notify_sys(struct notifier_block *nb, unsigned long code, |
| 72 | void *unused) |
| 73 | { |
Harald Geyer | 8d2fa17 | 2015-12-12 14:32:15 +0000 | [diff] [blame] | 74 | switch (code) { |
| 75 | case SYS_DOWN: /* keep enabled, system might crash while going down */ |
| 76 | break; |
| 77 | case SYS_HALT: /* allow the system to actually halt */ |
| 78 | case SYS_POWER_OFF: |
| 79 | wdt_stop(&stmp3xxx_wdd); |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | return NOTIFY_DONE; |
| 84 | } |
| 85 | |
| 86 | static struct notifier_block wdt_notifier = { |
| 87 | .notifier_call = wdt_notify_sys, |
| 88 | }; |
| 89 | |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 90 | static int stmp3xxx_wdt_probe(struct platform_device *pdev) |
| 91 | { |
Guenter Roeck | 55082c0 | 2019-04-09 10:23:58 -0700 | [diff] [blame] | 92 | struct device *dev = &pdev->dev; |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 93 | int ret; |
| 94 | |
Guenter Roeck | 55082c0 | 2019-04-09 10:23:58 -0700 | [diff] [blame] | 95 | watchdog_set_drvdata(&stmp3xxx_wdd, dev); |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 96 | |
| 97 | stmp3xxx_wdd.timeout = clamp_t(unsigned, heartbeat, 1, STMP3XXX_MAX_TIMEOUT); |
Guenter Roeck | 55082c0 | 2019-04-09 10:23:58 -0700 | [diff] [blame] | 98 | stmp3xxx_wdd.parent = dev; |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 99 | |
Guenter Roeck | 55082c0 | 2019-04-09 10:23:58 -0700 | [diff] [blame] | 100 | ret = devm_watchdog_register_device(dev, &stmp3xxx_wdd); |
Wolfram Sang | 913b187 | 2019-05-18 23:27:57 +0200 | [diff] [blame^] | 101 | if (ret < 0) |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 102 | return ret; |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 103 | |
Harald Geyer | 8d2fa17 | 2015-12-12 14:32:15 +0000 | [diff] [blame] | 104 | if (register_reboot_notifier(&wdt_notifier)) |
Guenter Roeck | 55082c0 | 2019-04-09 10:23:58 -0700 | [diff] [blame] | 105 | dev_warn(dev, "cannot register reboot notifier\n"); |
Harald Geyer | 8d2fa17 | 2015-12-12 14:32:15 +0000 | [diff] [blame] | 106 | |
Guenter Roeck | 55082c0 | 2019-04-09 10:23:58 -0700 | [diff] [blame] | 107 | dev_info(dev, "initialized watchdog with heartbeat %ds\n", |
| 108 | stmp3xxx_wdd.timeout); |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int stmp3xxx_wdt_remove(struct platform_device *pdev) |
| 113 | { |
Harald Geyer | 8d2fa17 | 2015-12-12 14:32:15 +0000 | [diff] [blame] | 114 | unregister_reboot_notifier(&wdt_notifier); |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
Janusz Uzycki | 3281b85 | 2014-09-22 22:55:47 +0200 | [diff] [blame] | 118 | static int __maybe_unused stmp3xxx_wdt_suspend(struct device *dev) |
| 119 | { |
| 120 | struct watchdog_device *wdd = &stmp3xxx_wdd; |
| 121 | |
| 122 | if (watchdog_active(wdd)) |
| 123 | return wdt_stop(wdd); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int __maybe_unused stmp3xxx_wdt_resume(struct device *dev) |
| 129 | { |
| 130 | struct watchdog_device *wdd = &stmp3xxx_wdd; |
| 131 | |
| 132 | if (watchdog_active(wdd)) |
| 133 | return wdt_start(wdd); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static SIMPLE_DEV_PM_OPS(stmp3xxx_wdt_pm_ops, |
| 139 | stmp3xxx_wdt_suspend, stmp3xxx_wdt_resume); |
| 140 | |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 141 | static struct platform_driver stmp3xxx_wdt_driver = { |
| 142 | .driver = { |
| 143 | .name = "stmp3xxx_rtc_wdt", |
Janusz Uzycki | 3281b85 | 2014-09-22 22:55:47 +0200 | [diff] [blame] | 144 | .pm = &stmp3xxx_wdt_pm_ops, |
Wolfram Sang | de6303a | 2011-09-27 22:35:40 +0200 | [diff] [blame] | 145 | }, |
| 146 | .probe = stmp3xxx_wdt_probe, |
| 147 | .remove = stmp3xxx_wdt_remove, |
| 148 | }; |
| 149 | module_platform_driver(stmp3xxx_wdt_driver); |
| 150 | |
| 151 | MODULE_DESCRIPTION("STMP3XXX RTC Watchdog Driver"); |
| 152 | MODULE_LICENSE("GPL v2"); |
Wolfram Sang | cf82f52 | 2015-04-20 15:51:44 +0200 | [diff] [blame] | 153 | MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>"); |