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