Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) Nokia Corporation |
| 4 | * |
| 5 | * Written by Timo Kokkonen <timo.t.kokkonen at nokia.com> |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 12 | #include <linux/watchdog.h> |
| 13 | #include <linux/platform_device.h> |
Wolfram Sang | a205425 | 2017-08-14 18:34:24 +0200 | [diff] [blame] | 14 | #include <linux/mfd/twl.h> |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 15 | |
| 16 | #define TWL4030_WATCHDOG_CFG_REG_OFFS 0x3 |
| 17 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 18 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 19 | module_param(nowayout, bool, 0); |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 20 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started " |
| 21 | "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 22 | |
| 23 | static int twl4030_wdt_write(unsigned char val) |
| 24 | { |
Peter Ujfalusi | 2bc3f62 | 2012-11-13 10:40:22 +0100 | [diff] [blame] | 25 | return twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER, val, |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 26 | TWL4030_WATCHDOG_CFG_REG_OFFS); |
| 27 | } |
| 28 | |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 29 | static int twl4030_wdt_start(struct watchdog_device *wdt) |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 30 | { |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 31 | return twl4030_wdt_write(wdt->timeout + 1); |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 34 | static int twl4030_wdt_stop(struct watchdog_device *wdt) |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 35 | { |
| 36 | return twl4030_wdt_write(0); |
| 37 | } |
| 38 | |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 39 | static int twl4030_wdt_set_timeout(struct watchdog_device *wdt, |
| 40 | unsigned int timeout) |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 41 | { |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 42 | wdt->timeout = timeout; |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 46 | static const struct watchdog_info twl4030_wdt_info = { |
Tony Lindgren | fb1cbea | 2014-10-14 12:25:19 -0700 | [diff] [blame] | 47 | .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING, |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 48 | .identity = "TWL4030 Watchdog", |
| 49 | }; |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 50 | |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 51 | static const struct watchdog_ops twl4030_wdt_ops = { |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 52 | .owner = THIS_MODULE, |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 53 | .start = twl4030_wdt_start, |
| 54 | .stop = twl4030_wdt_stop, |
| 55 | .set_timeout = twl4030_wdt_set_timeout, |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 58 | static int twl4030_wdt_probe(struct platform_device *pdev) |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 59 | { |
Guenter Roeck | b42488b | 2019-04-10 09:27:46 -0700 | [diff] [blame] | 60 | struct device *dev = &pdev->dev; |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 61 | struct watchdog_device *wdt; |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 62 | |
Guenter Roeck | b42488b | 2019-04-10 09:27:46 -0700 | [diff] [blame] | 63 | wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 64 | if (!wdt) |
| 65 | return -ENOMEM; |
| 66 | |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 67 | wdt->info = &twl4030_wdt_info; |
| 68 | wdt->ops = &twl4030_wdt_ops; |
| 69 | wdt->status = 0; |
| 70 | wdt->timeout = 30; |
| 71 | wdt->min_timeout = 1; |
| 72 | wdt->max_timeout = 30; |
Guenter Roeck | b42488b | 2019-04-10 09:27:46 -0700 | [diff] [blame] | 73 | wdt->parent = dev; |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 74 | |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 75 | watchdog_set_nowayout(wdt, nowayout); |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 76 | platform_set_drvdata(pdev, wdt); |
| 77 | |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 78 | twl4030_wdt_stop(wdt); |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 79 | |
Guenter Roeck | b42488b | 2019-04-10 09:27:46 -0700 | [diff] [blame] | 80 | return devm_watchdog_register_device(dev, wdt); |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | #ifdef CONFIG_PM |
| 84 | static int twl4030_wdt_suspend(struct platform_device *pdev, pm_message_t state) |
| 85 | { |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 86 | struct watchdog_device *wdt = platform_get_drvdata(pdev); |
| 87 | if (watchdog_active(wdt)) |
| 88 | return twl4030_wdt_stop(wdt); |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int twl4030_wdt_resume(struct platform_device *pdev) |
| 94 | { |
Jarkko Nikula | b2c4e4b | 2012-09-11 09:01:10 +0300 | [diff] [blame] | 95 | struct watchdog_device *wdt = platform_get_drvdata(pdev); |
| 96 | if (watchdog_active(wdt)) |
| 97 | return twl4030_wdt_start(wdt); |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | #else |
| 102 | #define twl4030_wdt_suspend NULL |
| 103 | #define twl4030_wdt_resume NULL |
| 104 | #endif |
| 105 | |
Aaro Koskinen | 8899b8d | 2012-12-23 22:03:37 +0200 | [diff] [blame] | 106 | static const struct of_device_id twl_wdt_of_match[] = { |
| 107 | { .compatible = "ti,twl4030-wdt", }, |
| 108 | { }, |
| 109 | }; |
| 110 | MODULE_DEVICE_TABLE(of, twl_wdt_of_match); |
| 111 | |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 112 | static struct platform_driver twl4030_wdt_driver = { |
| 113 | .probe = twl4030_wdt_probe, |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 114 | .suspend = twl4030_wdt_suspend, |
| 115 | .resume = twl4030_wdt_resume, |
| 116 | .driver = { |
Aaro Koskinen | 8899b8d | 2012-12-23 22:03:37 +0200 | [diff] [blame] | 117 | .name = "twl4030_wdt", |
| 118 | .of_match_table = twl_wdt_of_match, |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 119 | }, |
| 120 | }; |
| 121 | |
Axel Lin | b8ec611 | 2011-11-29 13:56:27 +0800 | [diff] [blame] | 122 | module_platform_driver(twl4030_wdt_driver); |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 123 | |
| 124 | MODULE_AUTHOR("Nokia Corporation"); |
| 125 | MODULE_LICENSE("GPL"); |
Timo Kokkonen | 80e45b1 | 2009-03-27 16:42:17 +0200 | [diff] [blame] | 126 | MODULE_ALIAS("platform:twl4030_wdt"); |
| 127 | |