[PATCH v7 6/7] watchdog: stpmic1: add stpmic1 watchdog driver

2018-11-30 Thread Pascal PAILLET-LME
The stpmic1 PMIC embeds a watchdog which is disabled by default. As soon
as the watchdog is started, it must be refreshed periodically otherwise
the PMIC goes off.

Signed-off-by: Pascal Paillet 
Reviewed-by: Guenter Roeck 
---
changes in v7: nothing

 drivers/watchdog/Kconfig   |  12 
 drivers/watchdog/Makefile  |   1 +
 drivers/watchdog/stpmic1_wdt.c | 139 +
 3 files changed, 152 insertions(+)
 create mode 100644 drivers/watchdog/stpmic1_wdt.c

diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig
index 2d64333..cc5155b 100644
--- a/drivers/watchdog/Kconfig
+++ b/drivers/watchdog/Kconfig
@@ -817,6 +817,18 @@ config STM32_WATCHDOG
  To compile this driver as a module, choose M here: the
  module will be called stm32_iwdg.
 
+config STPMIC1_WATCHDOG
+   tristate "STPMIC1 PMIC watchdog support"
+   depends on MFD_STPMIC1
+   select WATCHDOG_CORE
+   help
+ Say Y here to include watchdog support embedded into STPMIC1 PMIC.
+ If the watchdog timer expires, stpmic1 will shut down all its power
+ supplies.
+
+ To compile this driver as a module, choose M here: the
+ module will be called spmic1_wdt.
+
 config UNIPHIER_WATCHDOG
tristate "UniPhier watchdog support"
depends on ARCH_UNIPHIER || COMPILE_TEST
diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile
index f69cdff..bdc072c 100644
--- a/drivers/watchdog/Makefile
+++ b/drivers/watchdog/Makefile
@@ -218,3 +218,4 @@ obj-$(CONFIG_SOFT_WATCHDOG) += softdog.o
 obj-$(CONFIG_MENF21BMC_WATCHDOG) += menf21bmc_wdt.o
 obj-$(CONFIG_MENZ069_WATCHDOG) += menz69_wdt.o
 obj-$(CONFIG_RAVE_SP_WATCHDOG) += rave-sp-wdt.o
+obj-$(CONFIG_STPMIC1_WATCHDOG) += stpmic1_wdt.o
diff --git a/drivers/watchdog/stpmic1_wdt.c b/drivers/watchdog/stpmic1_wdt.c
new file mode 100644
index 000..ad431d8
--- /dev/null
+++ b/drivers/watchdog/stpmic1_wdt.c
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) STMicroelectronics 2018
+// Author: Pascal Paillet  for STMicroelectronics.
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* WATCHDOG CONTROL REGISTER bit */
+#define WDT_START  BIT(0)
+#define WDT_PING   BIT(1)
+#define WDT_START_MASK BIT(0)
+#define WDT_PING_MASK  BIT(1)
+#define WDT_STOP   0
+
+#define PMIC_WDT_MIN_TIMEOUT 1
+#define PMIC_WDT_MAX_TIMEOUT 256
+#define PMIC_WDT_DEFAULT_TIMEOUT 30
+
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
+__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
+struct stpmic1_wdt {
+   struct stpmic1 *pmic;
+   struct watchdog_device wdtdev;
+};
+
+static int pmic_wdt_start(struct watchdog_device *wdd)
+{
+   struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
+
+   return regmap_update_bits(wdt->pmic->regmap,
+ WCHDG_CR, WDT_START_MASK, WDT_START);
+}
+
+static int pmic_wdt_stop(struct watchdog_device *wdd)
+{
+   struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
+
+   return regmap_update_bits(wdt->pmic->regmap,
+ WCHDG_CR, WDT_START_MASK, WDT_STOP);
+}
+
+static int pmic_wdt_ping(struct watchdog_device *wdd)
+{
+   struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
+
+   return regmap_update_bits(wdt->pmic->regmap,
+ WCHDG_CR, WDT_PING_MASK, WDT_PING);
+}
+
+static int pmic_wdt_set_timeout(struct watchdog_device *wdd,
+   unsigned int timeout)
+{
+   struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
+
+   wdd->timeout = timeout;
+   /* timeout is equal to register value + 1 */
+   return regmap_write(wdt->pmic->regmap, WCHDG_TIMER_CR, timeout - 1);
+}
+
+static const struct watchdog_info pmic_watchdog_info = {
+   .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
+   .identity = "STPMIC1 PMIC Watchdog",
+};
+
+static const struct watchdog_ops pmic_watchdog_ops = {
+   .owner = THIS_MODULE,
+   .start = pmic_wdt_start,
+   .stop = pmic_wdt_stop,
+   .ping = pmic_wdt_ping,
+   .set_timeout = pmic_wdt_set_timeout,
+};
+
+static int pmic_wdt_probe(struct platform_device *pdev)
+{
+   int ret;
+   struct stpmic1 *pmic;
+   struct stpmic1_wdt *wdt;
+
+   if (!pdev->dev.parent)
+   return -EINVAL;
+
+   pmic = dev_get_drvdata(pdev->dev.parent);
+   if (!pmic)
+   return -EINVAL;
+
+   wdt = devm_kzalloc(>dev, sizeof(struct stpmic1_wdt), GFP_KERNEL);
+   if (!wdt)
+   return -ENOMEM;
+
+   wdt->pmic = pmic;
+
+   wdt->wdtdev.info = _watchdog_info;
+   wdt->wdtdev.ops = _watchdog_ops;
+   wdt->wdtdev.min_timeout = PMIC_WDT_MIN_TIMEOUT;
+   

[PATCH v7 6/7] watchdog: stpmic1: add stpmic1 watchdog driver

2018-11-30 Thread Pascal PAILLET-LME
The stpmic1 PMIC embeds a watchdog which is disabled by default. As soon
as the watchdog is started, it must be refreshed periodically otherwise
the PMIC goes off.

Signed-off-by: Pascal Paillet 
Reviewed-by: Guenter Roeck 
---
changes in v7: nothing

 drivers/watchdog/Kconfig   |  12 
 drivers/watchdog/Makefile  |   1 +
 drivers/watchdog/stpmic1_wdt.c | 139 +
 3 files changed, 152 insertions(+)
 create mode 100644 drivers/watchdog/stpmic1_wdt.c

diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig
index 2d64333..cc5155b 100644
--- a/drivers/watchdog/Kconfig
+++ b/drivers/watchdog/Kconfig
@@ -817,6 +817,18 @@ config STM32_WATCHDOG
  To compile this driver as a module, choose M here: the
  module will be called stm32_iwdg.
 
+config STPMIC1_WATCHDOG
+   tristate "STPMIC1 PMIC watchdog support"
+   depends on MFD_STPMIC1
+   select WATCHDOG_CORE
+   help
+ Say Y here to include watchdog support embedded into STPMIC1 PMIC.
+ If the watchdog timer expires, stpmic1 will shut down all its power
+ supplies.
+
+ To compile this driver as a module, choose M here: the
+ module will be called spmic1_wdt.
+
 config UNIPHIER_WATCHDOG
tristate "UniPhier watchdog support"
depends on ARCH_UNIPHIER || COMPILE_TEST
diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile
index f69cdff..bdc072c 100644
--- a/drivers/watchdog/Makefile
+++ b/drivers/watchdog/Makefile
@@ -218,3 +218,4 @@ obj-$(CONFIG_SOFT_WATCHDOG) += softdog.o
 obj-$(CONFIG_MENF21BMC_WATCHDOG) += menf21bmc_wdt.o
 obj-$(CONFIG_MENZ069_WATCHDOG) += menz69_wdt.o
 obj-$(CONFIG_RAVE_SP_WATCHDOG) += rave-sp-wdt.o
+obj-$(CONFIG_STPMIC1_WATCHDOG) += stpmic1_wdt.o
diff --git a/drivers/watchdog/stpmic1_wdt.c b/drivers/watchdog/stpmic1_wdt.c
new file mode 100644
index 000..ad431d8
--- /dev/null
+++ b/drivers/watchdog/stpmic1_wdt.c
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) STMicroelectronics 2018
+// Author: Pascal Paillet  for STMicroelectronics.
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* WATCHDOG CONTROL REGISTER bit */
+#define WDT_START  BIT(0)
+#define WDT_PING   BIT(1)
+#define WDT_START_MASK BIT(0)
+#define WDT_PING_MASK  BIT(1)
+#define WDT_STOP   0
+
+#define PMIC_WDT_MIN_TIMEOUT 1
+#define PMIC_WDT_MAX_TIMEOUT 256
+#define PMIC_WDT_DEFAULT_TIMEOUT 30
+
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
+__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
+struct stpmic1_wdt {
+   struct stpmic1 *pmic;
+   struct watchdog_device wdtdev;
+};
+
+static int pmic_wdt_start(struct watchdog_device *wdd)
+{
+   struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
+
+   return regmap_update_bits(wdt->pmic->regmap,
+ WCHDG_CR, WDT_START_MASK, WDT_START);
+}
+
+static int pmic_wdt_stop(struct watchdog_device *wdd)
+{
+   struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
+
+   return regmap_update_bits(wdt->pmic->regmap,
+ WCHDG_CR, WDT_START_MASK, WDT_STOP);
+}
+
+static int pmic_wdt_ping(struct watchdog_device *wdd)
+{
+   struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
+
+   return regmap_update_bits(wdt->pmic->regmap,
+ WCHDG_CR, WDT_PING_MASK, WDT_PING);
+}
+
+static int pmic_wdt_set_timeout(struct watchdog_device *wdd,
+   unsigned int timeout)
+{
+   struct stpmic1_wdt *wdt = watchdog_get_drvdata(wdd);
+
+   wdd->timeout = timeout;
+   /* timeout is equal to register value + 1 */
+   return regmap_write(wdt->pmic->regmap, WCHDG_TIMER_CR, timeout - 1);
+}
+
+static const struct watchdog_info pmic_watchdog_info = {
+   .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
+   .identity = "STPMIC1 PMIC Watchdog",
+};
+
+static const struct watchdog_ops pmic_watchdog_ops = {
+   .owner = THIS_MODULE,
+   .start = pmic_wdt_start,
+   .stop = pmic_wdt_stop,
+   .ping = pmic_wdt_ping,
+   .set_timeout = pmic_wdt_set_timeout,
+};
+
+static int pmic_wdt_probe(struct platform_device *pdev)
+{
+   int ret;
+   struct stpmic1 *pmic;
+   struct stpmic1_wdt *wdt;
+
+   if (!pdev->dev.parent)
+   return -EINVAL;
+
+   pmic = dev_get_drvdata(pdev->dev.parent);
+   if (!pmic)
+   return -EINVAL;
+
+   wdt = devm_kzalloc(>dev, sizeof(struct stpmic1_wdt), GFP_KERNEL);
+   if (!wdt)
+   return -ENOMEM;
+
+   wdt->pmic = pmic;
+
+   wdt->wdtdev.info = _watchdog_info;
+   wdt->wdtdev.ops = _watchdog_ops;
+   wdt->wdtdev.min_timeout = PMIC_WDT_MIN_TIMEOUT;
+