^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) * System monitoring driver for DA9055 PMICs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright(c) 2012 Dialog Semiconductor Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: David Dajun Chen <dchen@diasemi.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.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/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/mfd/da9055/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mfd/da9055/reg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static bool nowayout = WATCHDOG_NOWAYOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) module_param(nowayout, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) MODULE_PARM_DESC(nowayout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) "Watchdog cannot be stopped once started (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define DA9055_DEF_TIMEOUT 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define DA9055_TWDMIN 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct da9055_wdt_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct watchdog_device wdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct da9055 *da9055;
^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 const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u8 reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int user_time; /* In seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) } da9055_wdt_maps[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) { 0, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) { 1, 2 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) { 2, 4 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) { 3, 8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) { 4, 16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) { 5, 32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) { 5, 33 }, /* Actual time 32.768s so included both 32s and 33s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) { 6, 65 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) { 6, 66 }, /* Actual time 65.536s so include both, 65s and 66s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) { 7, 131 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static int da9055_wdt_set_timeout(struct watchdog_device *wdt_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct da9055 *da9055 = driver_data->da9055;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) for (i = 0; i < ARRAY_SIZE(da9055_wdt_maps); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (da9055_wdt_maps[i].user_time == timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (i == ARRAY_SIZE(da9055_wdt_maps))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) DA9055_TWDSCALE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) da9055_wdt_maps[i].reg_val <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) DA9055_TWDSCALE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) dev_err(da9055->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) "Failed to update timescale bit, %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) wdt_dev->timeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int da9055_wdt_ping(struct watchdog_device *wdt_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct da9055 *da9055 = driver_data->da9055;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * We have a minimum time for watchdog window called TWDMIN. A write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * to the watchdog before this elapsed time will cause an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) mdelay(DA9055_TWDMIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* Reset the watchdog timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return da9055_reg_update(da9055, DA9055_REG_CONTROL_E,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) DA9055_WATCHDOG_MASK, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static int da9055_wdt_start(struct watchdog_device *wdt_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return da9055_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int da9055_wdt_stop(struct watchdog_device *wdt_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return da9055_wdt_set_timeout(wdt_dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static const struct watchdog_info da9055_wdt_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .identity = "DA9055 Watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static const struct watchdog_ops da9055_wdt_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .start = da9055_wdt_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .stop = da9055_wdt_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .ping = da9055_wdt_ping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .set_timeout = da9055_wdt_set_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int da9055_wdt_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct da9055 *da9055 = dev_get_drvdata(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct da9055_wdt_data *driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct watchdog_device *da9055_wdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) driver_data = devm_kzalloc(dev, sizeof(*driver_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!driver_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) driver_data->da9055 = da9055;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) da9055_wdt = &driver_data->wdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) da9055_wdt->timeout = DA9055_DEF_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) da9055_wdt->info = &da9055_wdt_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) da9055_wdt->ops = &da9055_wdt_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) da9055_wdt->parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) watchdog_set_nowayout(da9055_wdt, nowayout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) watchdog_set_drvdata(da9055_wdt, driver_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ret = da9055_wdt_stop(da9055_wdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) dev_err(dev, "Failed to stop watchdog, %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return ret;
^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) ret = devm_watchdog_register_device(dev, &driver_data->wdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dev_err(da9055->dev, "watchdog_register_device() failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static struct platform_driver da9055_wdt_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .probe = da9055_wdt_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .name = "da9055-watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) module_platform_driver(da9055_wdt_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) MODULE_DESCRIPTION("DA9055 watchdog");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) MODULE_ALIAS("platform:da9055-watchdog");