On 06/20/2018 12:53 AM, Ludovic Barre wrote:
From: Ludovic Barre <ludovic.barre@xxxxxx>
This patch adds config data to manage specific properties by
compatible. Adds stm32mp1 config which requires pclk clock.
Signed-off-by: Ludovic Barre <ludovic.barre@xxxxxx>
---
 .../devicetree/bindings/watchdog/st,stm32-iwdg.txt | 21 +++-
 drivers/watchdog/stm32_iwdg.c | 132 ++++++++++++++-------
 2 files changed, 104 insertions(+), 49 deletions(-)
diff --git a/Documentation/devicetree/bindings/watchdog/st,stm32-iwdg.txt b/Documentation/devicetree/bindings/watchdog/st,stm32-iwdg.txt
index cc13b10a..f07f6d89 100644
--- a/Documentation/devicetree/bindings/watchdog/st,stm32-iwdg.txt
+++ b/Documentation/devicetree/bindings/watchdog/st,stm32-iwdg.txt
@@ -2,18 +2,31 @@ STM32 Independent WatchDoG (IWDG)
 ---------------------------------
 Required properties:
-- compatible: "st,stm32-iwdg"
-- reg: physical base address and length of the registers set for the device
-- clocks: must contain a single entry describing the clock input
+- compatible: Should be either "st,stm32-iwdg" or "st,stm32mp1-iwdg"
+- reg: Physical base address and length of the registers set for the device
+- clocks: Reference to the clock entry lsi. Additional pclk clock entry
+Â is required only for st,stm32mp1-iwdg.
+- clock-names: Name of the clocks used.
+Â "lsi" for st,stm32-iwdg
+Â "pclk", "lsi" for st,stm32mp1-iwdg
 Optional Properties:
 - timeout-sec: Watchdog timeout value in seconds.
-Example:
+Examples:
 iwdg: watchdog@40003000 {
ÂÂÂÂÂ compatible = "st,stm32-iwdg";
ÂÂÂÂÂ reg = <0x40003000 0x400>;
ÂÂÂÂÂ clocks = <&clk_lsi>;
+ÂÂÂ clock-names = "lsi";
+ÂÂÂ timeout-sec = <32>;
+};
+
+iwdg: iwdg@5a002000 {
+ÂÂÂ compatible = "st,stm32mp1-iwdg";
+ÂÂÂ reg = <0x5a002000 0x400>;
+ÂÂÂ clocks = <&rcc IWDG2>, <&clk_lsi>;
+ÂÂÂ clock-names = "pclk", "lsi";
ÂÂÂÂÂ timeout-sec = <32>;
 };
diff --git a/drivers/watchdog/stm32_iwdg.c b/drivers/watchdog/stm32_iwdg.c
index c97ad56..fc96670 100644
--- a/drivers/watchdog/stm32_iwdg.c
+++ b/drivers/watchdog/stm32_iwdg.c
@@ -11,12 +11,13 @@
 #include <linux/clk.h>
 #include <linux/delay.h>
-#include <linux/kernel.h>
-#include <linux/module.h>
 #include <linux/interrupt.h>
 #include <linux/io.h>
 #include <linux/iopoll.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
 #include <linux/of.h>
+#include <linux/of_device.h>
 #include <linux/platform_device.h>
 #include <linux/watchdog.h>
@@ -54,11 +55,17 @@
 #define TIMEOUT_US 100000
 #define SLEEP_US 1000
+struct stm32_iwdg_config {
+ÂÂÂ bool has_pclk;
+};
+
This data structure is unnecessary. Just assign the boolean directly to .data
and ...
 struct stm32_iwdg {
-ÂÂÂ struct watchdog_deviceÂÂÂ wdd;
-ÂÂÂ void __iomemÂÂÂÂÂÂÂ *regs;
-ÂÂÂ struct clkÂÂÂÂÂÂÂ *clk;
-ÂÂÂ unsigned intÂÂÂÂÂÂÂ rate;
+ÂÂÂ struct watchdog_deviceÂÂÂÂÂÂÂ wdd;
+ÂÂÂ void __iomemÂÂÂÂÂÂÂÂÂÂÂ *regs;
+ÂÂÂ struct stm32_iwdg_configÂÂÂ *config;
declare bool has_pclk here.
+ÂÂÂ struct clkÂÂÂÂÂÂÂÂÂÂÂ *clk_lsi;
+ÂÂÂ struct clkÂÂÂÂÂÂÂÂÂÂÂ *clk_pclk;
+ÂÂÂ unsigned intÂÂÂÂÂÂÂÂÂÂÂ rate;
 };
 static inline u32 reg_read(void __iomem *base, u32 reg)
@@ -133,6 +140,44 @@ static int stm32_iwdg_set_timeout(struct watchdog_device *wdd,
ÂÂÂÂÂ return 0;
 }
+static int stm32_iwdg_clk_init(struct platform_device *pdev,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct stm32_iwdg *wdt)
+{
+ÂÂÂ u32 ret;
+
+ÂÂÂ wdt->clk_lsi = devm_clk_get(&pdev->dev, "lsi");
+ÂÂÂ if (IS_ERR(wdt->clk_lsi)) {
+ÂÂÂÂÂÂÂ dev_err(&pdev->dev, "Unable to get lsi clock\n");
+ÂÂÂÂÂÂÂ return PTR_ERR(wdt->clk_lsi);
+ÂÂÂ }
+
+ÂÂÂ /* optional peripheral clock */
+ÂÂÂ if (wdt->config->has_pclk) {
+ÂÂÂÂÂÂÂ wdt->clk_pclk = devm_clk_get(&pdev->dev, "pclk");
+ÂÂÂÂÂÂÂ if (IS_ERR(wdt->clk_pclk)) {
+ÂÂÂÂÂÂÂÂÂÂÂ dev_err(&pdev->dev, "Unable to get pclk clock\n");
+ÂÂÂÂÂÂÂÂÂÂÂ return PTR_ERR(wdt->clk_pclk);
+ÂÂÂÂÂÂÂ }
+
+ÂÂÂÂÂÂÂ ret = clk_prepare_enable(wdt->clk_pclk);
+ÂÂÂÂÂÂÂ if (ret) {
+ÂÂÂÂÂÂÂÂÂÂÂ dev_err(&pdev->dev, "Unable to prepare pclk clock\n");
+ÂÂÂÂÂÂÂÂÂÂÂ return ret;
+ÂÂÂÂÂÂÂ }
+ÂÂÂ }
+
+ÂÂÂ ret = clk_prepare_enable(wdt->clk_lsi);
+ÂÂÂ if (ret) {
+ÂÂÂÂÂÂÂ dev_err(&pdev->dev, "Unable to prepare lsi clock\n");
+ÂÂÂÂÂÂÂ clk_disable_unprepare(wdt->clk_pclk);
+ÂÂÂÂÂÂÂ return ret;
+ÂÂÂ }
+
+ÂÂÂ wdt->rate = clk_get_rate(wdt->clk_lsi);
+
+ÂÂÂ return 0;
+}
+
 static const struct watchdog_info stm32_iwdg_info = {
ÂÂÂÂÂ .optionsÂÂÂ = WDIOF_SETTIMEOUT |
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ WDIOF_MAGICCLOSE |
@@ -147,49 +192,50 @@ static const struct watchdog_ops stm32_iwdg_ops = {
ÂÂÂÂÂ .set_timeoutÂÂÂ = stm32_iwdg_set_timeout,
 };
+static const struct stm32_iwdg_config stm32_iwdg_cfg = {
+ÂÂÂ .has_pclk = false,
+};
+
+static const struct stm32_iwdg_config stm32mp1_iwdg_cfg = {
+ÂÂÂ .has_pclk = true,
+};
+
+static const struct of_device_id stm32_iwdg_of_match[] = {
+ÂÂÂ { .compatible = "st,stm32-iwdg", .data = &stm32_iwdg_cfg },
+ÂÂÂ { .compatible = "st,stm32mp1-iwdg", .data = &stm32mp1_iwdg_cfg },
+ÂÂÂ { /* end node */ }
+};
+MODULE_DEVICE_TABLE(of, stm32_iwdg_of_match);
+
 static int stm32_iwdg_probe(struct platform_device *pdev)
 {
ÂÂÂÂÂ struct watchdog_device *wdd;
+ÂÂÂ const struct of_device_id *match;
ÂÂÂÂÂ struct stm32_iwdg *wdt;
ÂÂÂÂÂ struct resource *res;
-ÂÂÂ void __iomem *regs;
-ÂÂÂ struct clk *clk;
ÂÂÂÂÂ int ret;
+ÂÂÂ match = of_match_device(stm32_iwdg_of_match, &pdev->dev);
+ÂÂÂ if (!match || !match->data)
+ÂÂÂÂÂÂÂ return -ENODEV;
+
+ÂÂÂ wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
+ÂÂÂ if (!wdt)
+ÂÂÂÂÂÂÂ return -ENOMEM;
+
+ÂÂÂ wdt->config = (struct stm32_iwdg_config *)match->data;
+
ÂÂÂÂÂ /* This is the timer base. */
ÂÂÂÂÂ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-ÂÂÂ regs = devm_ioremap_resource(&pdev->dev, res);
-ÂÂÂ if (IS_ERR(regs)) {
+ÂÂÂ wdt->regs = devm_ioremap_resource(&pdev->dev, res);
+ÂÂÂ if (IS_ERR(wdt->regs)) {
ÂÂÂÂÂÂÂÂÂ dev_err(&pdev->dev, "Could not get resource\n");
-ÂÂÂÂÂÂÂ return PTR_ERR(regs);
+ÂÂÂÂÂÂÂ return PTR_ERR(wdt->regs);
ÂÂÂÂÂ }
-ÂÂÂ clk = devm_clk_get(&pdev->dev, NULL);
-ÂÂÂ if (IS_ERR(clk)) {
-ÂÂÂÂÂÂÂ dev_err(&pdev->dev, "Unable to get clock\n");
-ÂÂÂÂÂÂÂ return PTR_ERR(clk);
-ÂÂÂ }
-
-ÂÂÂ ret = clk_prepare_enable(clk);
-ÂÂÂ if (ret) {
-ÂÂÂÂÂÂÂ dev_err(&pdev->dev, "Unable to prepare clock %p\n", clk);
+ÂÂÂ ret = stm32_iwdg_clk_init(pdev, wdt);
+ÂÂÂ if (ret)
ÂÂÂÂÂÂÂÂÂ return ret;
-ÂÂÂ }
-
-ÂÂÂ /*
-ÂÂÂÂ * Allocate our watchdog driver data, which has the
-ÂÂÂÂ * struct watchdog_device nested within it.
-ÂÂÂÂ */
-ÂÂÂ wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
-ÂÂÂ if (!wdt) {
-ÂÂÂÂÂÂÂ ret = -ENOMEM;
-ÂÂÂÂÂÂÂ goto err;
-ÂÂÂ }
-
-ÂÂÂ /* Initialize struct stm32_iwdg. */
-ÂÂÂ wdt->regs = regs;
-ÂÂÂ wdt->clk = clk;
-ÂÂÂ wdt->rate = clk_get_rate(clk);
ÂÂÂÂÂ /* Initialize struct watchdog_device. */
ÂÂÂÂÂ wdd = &wdt->wdd;
@@ -217,7 +263,8 @@ static int stm32_iwdg_probe(struct platform_device *pdev)
ÂÂÂÂÂ return 0;
 err:
-ÂÂÂ clk_disable_unprepare(clk);
+ÂÂÂ clk_disable_unprepare(wdt->clk_lsi);
+ÂÂÂ clk_disable_unprepare(wdt->clk_pclk);
ÂÂÂÂÂ return ret;
 }
@@ -227,23 +274,18 @@ static int stm32_iwdg_remove(struct platform_device *pdev)
ÂÂÂÂÂ struct stm32_iwdg *wdt = platform_get_drvdata(pdev);
ÂÂÂÂÂ watchdog_unregister_device(&wdt->wdd);
-ÂÂÂ clk_disable_unprepare(wdt->clk);
+ÂÂÂ clk_disable_unprepare(wdt->clk_lsi);
+ÂÂÂ clk_disable_unprepare(wdt->clk_pclk);
ÂÂÂÂÂ return 0;
 }
-static const struct of_device_id stm32_iwdg_of_match[] = {
-ÂÂÂ { .compatible = "st,stm32-iwdg" },
-ÂÂÂ { /* end node */ }
-};
-MODULE_DEVICE_TABLE(of, stm32_iwdg_of_match);
-
 static struct platform_driver stm32_iwdg_driver = {
ÂÂÂÂÂ .probeÂÂÂÂÂÂÂ = stm32_iwdg_probe,
ÂÂÂÂÂ .removeÂÂÂÂÂÂÂ = stm32_iwdg_remove,
ÂÂÂÂÂ .driver = {
ÂÂÂÂÂÂÂÂÂ .nameÂÂÂ = "iwdg",
-ÂÂÂÂÂÂÂ .of_match_table = stm32_iwdg_of_match,
+ÂÂÂÂÂÂÂ .of_match_table = of_match_ptr(stm32_iwdg_of_match),
ÂÂÂÂÂ },
 };
 module_platform_driver(stm32_iwdg_driver);