^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Watchdog driver for the wm8350
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2007, 2008 Wolfson Microelectronics <linux@wolfsonmicro.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mfd/wm8350/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static bool nowayout = WATCHDOG_NOWAYOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) module_param(nowayout, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) MODULE_PARM_DESC(nowayout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) "Watchdog cannot be stopped once started (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static DEFINE_MUTEX(wdt_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned int time; /* Seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u16 val; /* To be set in WM8350_SYSTEM_CONTROL_2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) } wm8350_wdt_cfgs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) { 1, 0x02 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) { 2, 0x04 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) { 4, 0x05 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int wm8350_wdt_set_timeout(struct watchdog_device *wdt_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) u16 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) for (i = 0; i < ARRAY_SIZE(wm8350_wdt_cfgs); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (wm8350_wdt_cfgs[i].time == timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (i == ARRAY_SIZE(wm8350_wdt_cfgs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) mutex_lock(&wdt_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) wm8350_reg_unlock(wm8350);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) reg &= ~WM8350_WDOG_TO_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) reg |= wm8350_wdt_cfgs[i].val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) wm8350_reg_lock(wm8350);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) mutex_unlock(&wdt_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) wdt_dev->timeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int wm8350_wdt_start(struct watchdog_device *wdt_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u16 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) mutex_lock(&wdt_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) wm8350_reg_unlock(wm8350);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) reg &= ~WM8350_WDOG_MODE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) reg |= 0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) wm8350_reg_lock(wm8350);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) mutex_unlock(&wdt_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int wm8350_wdt_stop(struct watchdog_device *wdt_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) u16 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) mutex_lock(&wdt_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) wm8350_reg_unlock(wm8350);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) reg &= ~WM8350_WDOG_MODE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) wm8350_reg_lock(wm8350);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) mutex_unlock(&wdt_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int wm8350_wdt_ping(struct watchdog_device *wdt_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) u16 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) mutex_lock(&wdt_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) mutex_unlock(&wdt_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static const struct watchdog_info wm8350_wdt_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .identity = "WM8350 Watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static const struct watchdog_ops wm8350_wdt_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) .start = wm8350_wdt_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) .stop = wm8350_wdt_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .ping = wm8350_wdt_ping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .set_timeout = wm8350_wdt_set_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static struct watchdog_device wm8350_wdt = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .info = &wm8350_wdt_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .ops = &wm8350_wdt_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .timeout = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .min_timeout = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) .max_timeout = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int wm8350_wdt_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct wm8350 *wm8350 = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (!wm8350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) pr_err("No driver data supplied\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) watchdog_set_nowayout(&wm8350_wdt, nowayout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) watchdog_set_drvdata(&wm8350_wdt, wm8350);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) wm8350_wdt.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Default to 4s timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) wm8350_wdt_set_timeout(&wm8350_wdt, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return watchdog_register_device(&wm8350_wdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int wm8350_wdt_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) watchdog_unregister_device(&wm8350_wdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static struct platform_driver wm8350_wdt_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .probe = wm8350_wdt_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .remove = wm8350_wdt_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .name = "wm8350-wdt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) module_platform_driver(wm8350_wdt_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) MODULE_AUTHOR("Mark Brown");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) MODULE_DESCRIPTION("WM8350 Watchdog");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) MODULE_ALIAS("platform:wm8350-wdt");