0001
0002
0003
0004
0005 #include <linux/kernel.h>
0006 #include <linux/mfd/stpmic1.h>
0007 #include <linux/module.h>
0008 #include <linux/platform_device.h>
0009 #include <linux/of.h>
0010 #include <linux/regmap.h>
0011 #include <linux/slab.h>
0012 #include <linux/watchdog.h>
0013
0014
0015 #define WDT_START BIT(0)
0016 #define WDT_PING BIT(1)
0017 #define WDT_START_MASK BIT(0)
0018 #define WDT_PING_MASK BIT(1)
0019 #define WDT_STOP 0
0020
0021 #define PMIC_WDT_MIN_TIMEOUT 1
0022 #define PMIC_WDT_MAX_TIMEOUT 256
0023 #define PMIC_WDT_DEFAULT_TIMEOUT 30
0024
0025 static bool nowayout = WATCHDOG_NOWAYOUT;
0026 module_param(nowayout, bool, 0);
0027 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
0028 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0029
0030 struct stpmic1_wdt {
0031 struct stpmic1 *pmic;
0032 struct watchdog_device wdtdev;
0033 };
0034
0035 static int pmic_wdt_start(struct watchdog_device *wdd)
0036 {
0037 struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
0038
0039 return regmap_update_bits(wdt->pmic->regmap,
0040 WCHDG_CR, WDT_START_MASK, WDT_START);
0041 }
0042
0043 static int pmic_wdt_stop(struct watchdog_device *wdd)
0044 {
0045 struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
0046
0047 return regmap_update_bits(wdt->pmic->regmap,
0048 WCHDG_CR, WDT_START_MASK, WDT_STOP);
0049 }
0050
0051 static int pmic_wdt_ping(struct watchdog_device *wdd)
0052 {
0053 struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
0054
0055 return regmap_update_bits(wdt->pmic->regmap,
0056 WCHDG_CR, WDT_PING_MASK, WDT_PING);
0057 }
0058
0059 static int pmic_wdt_set_timeout(struct watchdog_device *wdd,
0060 unsigned int timeout)
0061 {
0062 struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
0063
0064 wdd->timeout = timeout;
0065
0066 return regmap_write(wdt->pmic->regmap, WCHDG_TIMER_CR, timeout - 1);
0067 }
0068
0069 static const struct watchdog_info pmic_watchdog_info = {
0070 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
0071 .identity = "STPMIC1 PMIC Watchdog",
0072 };
0073
0074 static const struct watchdog_ops pmic_watchdog_ops = {
0075 .owner = THIS_MODULE,
0076 .start = pmic_wdt_start,
0077 .stop = pmic_wdt_stop,
0078 .ping = pmic_wdt_ping,
0079 .set_timeout = pmic_wdt_set_timeout,
0080 };
0081
0082 static int pmic_wdt_probe(struct platform_device *pdev)
0083 {
0084 struct device *dev = &pdev->dev;
0085 int ret;
0086 struct stpmic1 *pmic;
0087 struct stpmic1_wdt *wdt;
0088
0089 if (!dev->parent)
0090 return -EINVAL;
0091
0092 pmic = dev_get_drvdata(dev->parent);
0093 if (!pmic)
0094 return -EINVAL;
0095
0096 wdt = devm_kzalloc(dev, sizeof(struct stpmic1_wdt), GFP_KERNEL);
0097 if (!wdt)
0098 return -ENOMEM;
0099
0100 wdt->pmic = pmic;
0101
0102 wdt->wdtdev.info = &pmic_watchdog_info;
0103 wdt->wdtdev.ops = &pmic_watchdog_ops;
0104 wdt->wdtdev.min_timeout = PMIC_WDT_MIN_TIMEOUT;
0105 wdt->wdtdev.max_timeout = PMIC_WDT_MAX_TIMEOUT;
0106 wdt->wdtdev.parent = dev;
0107
0108 wdt->wdtdev.timeout = PMIC_WDT_DEFAULT_TIMEOUT;
0109 watchdog_init_timeout(&wdt->wdtdev, 0, dev);
0110
0111 watchdog_set_nowayout(&wdt->wdtdev, nowayout);
0112 watchdog_set_drvdata(&wdt->wdtdev, wdt);
0113
0114 ret = devm_watchdog_register_device(dev, &wdt->wdtdev);
0115 if (ret)
0116 return ret;
0117
0118 dev_dbg(wdt->pmic->dev, "PMIC Watchdog driver probed\n");
0119 return 0;
0120 }
0121
0122 static const struct of_device_id of_pmic_wdt_match[] = {
0123 { .compatible = "st,stpmic1-wdt" },
0124 { },
0125 };
0126
0127 MODULE_DEVICE_TABLE(of, of_pmic_wdt_match);
0128
0129 static struct platform_driver stpmic1_wdt_driver = {
0130 .probe = pmic_wdt_probe,
0131 .driver = {
0132 .name = "stpmic1-wdt",
0133 .of_match_table = of_pmic_wdt_match,
0134 },
0135 };
0136 module_platform_driver(stpmic1_wdt_driver);
0137
0138 MODULE_DESCRIPTION("Watchdog driver for STPMIC1 device");
0139 MODULE_AUTHOR("Pascal Paillet <p.paillet@st.com>");
0140 MODULE_LICENSE("GPL v2");