From patchwork Mon Oct 16 20:54:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 115991 Delivered-To: patch@linaro.org Received: by 10.140.22.163 with SMTP id 32csp4110489qgn; Mon, 16 Oct 2017 13:56:56 -0700 (PDT) X-Received: by 10.101.88.142 with SMTP id d14mr8925650pgu.36.1508187416695; Mon, 16 Oct 2017 13:56:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1508187416; cv=none; d=google.com; s=arc-20160816; b=MXEiDjIHuiOg0+hu5OuIGJUkBOrkPVp/WyqcGXCBm67740uJ/WWGk4z955PI0W+Ck5 dVXtK5GErUiGChblGEZ4nwdaqg+bOlWjT88FhSOVCs8cbgIdkgyu6ynKvWLbmToBcPUY Qn/q0FFXJ/KZuY9p7xmuVE0GrLZZW6jGPdcZm0Q17ky2qYMnciXnVMxS9BKDbajIh4yl uouJzZL7AvlMETtFePqxjeHXpH71JrtKBlCGxO17dElhvdbROkrDWyit2ZM0v9tRW+d5 zOKeIl1+swYmdrd9OhnuhySX45z4cR1HfHJdcDh2wBlDvyie94eh1qie6YG+76nbnB2C wHjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Th6gTiO8k2w3M7WWHYcJmkTEg2+iKKTtNBVi9vVAmc8=; b=IaIUlCzrERqqAI64uh0ngGWDeLYdgM71NLMzmUqBRkLfrIpKHldArl9YLweWV8I50/ Q/dDSvQEGbKBoIKJK/6RcLUUmD6Xy8jnCH7xxT0NDoWiK7Pco6DqKMCCGkmBoAdV2Mnn 1XIUzTbOA0EBnXRPGq4o/mDI4pv03tcuJaBQZ0S5GinTONI+vrBULotpbosy9FftAzaj DxXcUqYvfuCW03aMfoCkp4oVFWINmXUTzIP3KkWzUz8l4ugDiQweBoNNK2BT5baG0RA/ 7P8SqhVLvzZnzU8HQsKBQyhktywn4vsLhHlWETnub/xg8U7uCW5CSskbL3e7q6AsYbGL 25SA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=WmLchNpJ; spf=pass (google.com: best guess record for domain of linux-watchdog-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-watchdog-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m6si533400pff.595.2017.10.16.13.56.56 for ; Mon, 16 Oct 2017 13:56:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-watchdog-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=WmLchNpJ; spf=pass (google.com: best guess record for domain of linux-watchdog-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-watchdog-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756681AbdJPU44 (ORCPT ); Mon, 16 Oct 2017 16:56:56 -0400 Received: from mail-lf0-f48.google.com ([209.85.215.48]:57005 "EHLO mail-lf0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754256AbdJPU4z (ORCPT ); Mon, 16 Oct 2017 16:56:55 -0400 Received: by mail-lf0-f48.google.com with SMTP id 90so18477098lfs.13 for ; Mon, 16 Oct 2017 13:56:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=G7Sw8uKwb6WrA4VDLGB9FInQ9PQivMaZriugftJaVOs=; b=WmLchNpJ5hPbARZthJSEuqp66hTe0XOwlcf1paLCMjDenZL4+Au1ozczE0IR6N1Mfd ZwTs/uBFIVRbTX8nf8TwHJcya88wtgORav6kYoekiXD+ThtNlnQ+lJbO0tdSmC+Sn0Z9 4Z8onsmtfEvKBEKAsKj4jWk0K2KbP+TQCxLFA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=G7Sw8uKwb6WrA4VDLGB9FInQ9PQivMaZriugftJaVOs=; b=QeVrTJ6Yy/4TZJBwzFkR444qw8ShfDa0UTXp7mVi5LfKW5LcT+yCRAgrKDySPyzOt6 d4s3m7Sb5yvLEnNgrB92ZAzaOEeysNdLID/V3KyIJJsJKqJLdL7j01QFVdWKsmxncT4H YmpuafLzUhEPLKupRGoG/TM3umS0HO+waQpbdLH31OnvmLld2D2goLla/UNuNM2+Lb51 RvY/uwkJBnYC5js4hqzuyh4rTbyLhqpNiXN00ZeEmO8Gud7Yre5XVhV/QydAVQOz02IP bg8WF43IeSt89ufrLypfxDqeuuTj8MXwA3zy51bvrwf1VuKCYuTdeW98hmpfKKSP3TCU RWOw== X-Gm-Message-State: AMCzsaXTNXVHBNn/gIsUm/1DpkUUVHzCYMRRiv62IQKe23FAGdhlqorr RTe5/Y8fouwdJ52kLZreVfGzWA== X-Google-Smtp-Source: ABhQp+SkW6vT1hHPno0+b1gQPI5uRgzhyb5eIQESoBWPeHV2lSneJrzulBIbiSuJ88RMJyI+GaHHmw== X-Received: by 10.25.67.4 with SMTP id q4mr3296293lfa.69.1508187414068; Mon, 16 Oct 2017 13:56:54 -0700 (PDT) Received: from localhost.localdomain (c-5f7c71d5.014-348-6c756e10.cust.bredbandsbolaget.se. [213.113.124.95]) by smtp.gmail.com with ESMTPSA id s88sm2131227lje.40.2017.10.16.13.56.52 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 16 Oct 2017 13:56:53 -0700 (PDT) From: Linus Walleij To: Wim Van Sebroeck , Guenter Roeck , Jonas Jensen , Andrew Jeffery , Joel Stanley Cc: linux-arm-kernel@lists.infradead.org, linux-watchdog@vger.kernel.org, Linus Walleij Subject: [PATCH 4/5 v2] watchdog: ftwdt010: Add clock support Date: Mon, 16 Oct 2017 22:54:26 +0200 Message-Id: <20171016205427.4297-4-linus.walleij@linaro.org> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171016205427.4297-1-linus.walleij@linaro.org> References: <20171016205427.4297-1-linus.walleij@linaro.org> Sender: linux-watchdog-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org The Gemini platform now provides a proper clock look-up for this and other IPs, so add clock support to the driver. This also aids in using the same driver with other platforms such as MOXA ART. The IP has two clock inputs: PCLK (the IP peripheral clock) and EXTCLK (an external clock). We are a bit elaborate around this: on Gemini the EXTCLK is used by default today and it's 5MHz, and on MOXA ART the PCLK is used. On Aspeed the EXTCLK is used and it's 1MHz. So add some clever code to fall back to platform defaults if PCLK or EXTCLK is not provided by the device tree. Take this opportunity to implement .remove() for the driver that stops the watchdog and disables the clocks. Add credits that this code is inspired by MOXA ART. Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - Strictly require the clock framework - Sort header files in alphabetic order - Fix the error path to properly disable the clocks - Fix spelling in commit message - Be minimalist with informational messages and cut down on error messages --- drivers/watchdog/Kconfig | 1 + drivers/watchdog/ftwdt010_wdt.c | 79 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 71 insertions(+), 9 deletions(-) -- 2.13.6 -- To unsubscribe from this list: send the line "unsubscribe linux-watchdog" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig index fd44a542036a..245cff03c992 100644 --- a/drivers/watchdog/Kconfig +++ b/drivers/watchdog/Kconfig @@ -324,6 +324,7 @@ config 977_WATCHDOG config FTWDT010_WATCHDOG tristate "Faraday Technology FTWDT010 watchdog" depends on ARM || COMPILE_TEST + depends on COMMON_CLK select WATCHDOG_CORE default ARCH_GEMINI help diff --git a/drivers/watchdog/ftwdt010_wdt.c b/drivers/watchdog/ftwdt010_wdt.c index a9c2912ee280..21c3ac7f557a 100644 --- a/drivers/watchdog/ftwdt010_wdt.c +++ b/drivers/watchdog/ftwdt010_wdt.c @@ -5,6 +5,8 @@ * * Inspired by the out-of-tree drivers from OpenWRT: * Copyright (C) 2009 Paulius Zaleckas + * Inspired by the MOXA ART driver from Jonas Jensen: + * Copyright (C) 2013 Jonas Jensen * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -12,12 +14,14 @@ */ #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -29,19 +33,21 @@ #define WDRESTART_MAGIC 0x5AB9 -#define WDCR_CLOCK_5MHZ BIT(4) +#define WDCR_EXTCLK BIT(4) #define WDCR_WDEXT BIT(3) #define WDCR_WDINTR BIT(2) #define WDCR_SYS_RST BIT(1) #define WDCR_ENABLE BIT(0) -#define WDT_CLOCK 5000000 /* 5 MHz */ - struct ftwdt010_wdt { struct watchdog_device wdd; struct device *dev; void __iomem *base; bool has_irq; + struct clk *pclk; + struct clk *extclk; + unsigned int clk_freq; + bool use_extclk; }; static inline @@ -55,10 +61,12 @@ static int ftwdt010_wdt_start(struct watchdog_device *wdd) struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd); u32 enable; - writel(wdd->timeout * WDT_CLOCK, gwdt->base + FTWDT010_WDLOAD); + writel(wdd->timeout * gwdt->clk_freq, gwdt->base + FTWDT010_WDLOAD); writel(WDRESTART_MAGIC, gwdt->base + FTWDT010_WDRESTART); /* set clock before enabling */ - enable = WDCR_CLOCK_5MHZ | WDCR_SYS_RST; + enable = WDCR_SYS_RST; + if (gwdt->use_extclk) + enable |= WDCR_EXTCLK; writel(enable, gwdt->base + FTWDT010_WDCR); if (gwdt->has_irq) enable |= WDCR_WDINTR; @@ -124,6 +132,7 @@ static const struct watchdog_info ftwdt010_wdt_info = { static int ftwdt010_wdt_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; + struct device_node *np = dev->of_node; struct resource *res; struct ftwdt010_wdt *gwdt; unsigned int reg; @@ -139,11 +148,40 @@ static int ftwdt010_wdt_probe(struct platform_device *pdev) if (IS_ERR(gwdt->base)) return PTR_ERR(gwdt->base); + gwdt->use_extclk = of_property_read_bool(np, "faraday,use-extclk"); + + gwdt->pclk = devm_clk_get(dev, "PCLK"); + if (IS_ERR(gwdt->pclk)) + return PTR_ERR(gwdt->pclk); + ret = clk_prepare_enable(gwdt->pclk); + if (ret) + return ret; + if (!gwdt->use_extclk) + gwdt->clk_freq = clk_get_rate(gwdt->pclk); + + /* Only enable and get frequency from EXTCLK if it's in use */ + if (gwdt->use_extclk) { + gwdt->extclk = devm_clk_get(dev, "EXTCLK"); + if (IS_ERR(gwdt->extclk)) { + ret = PTR_ERR(gwdt->extclk); + goto out_disable_pclk; + } + ret = clk_prepare_enable(gwdt->extclk); + if (ret) + goto out_disable_pclk; + gwdt->clk_freq = clk_get_rate(gwdt->extclk); + } + + if (gwdt->clk_freq == 0) { + ret = -EINVAL; + goto out_disable_extclk; + } + gwdt->dev = dev; gwdt->wdd.info = &ftwdt010_wdt_info; gwdt->wdd.ops = &ftwdt010_wdt_ops; gwdt->wdd.min_timeout = 1; - gwdt->wdd.max_timeout = 0xFFFFFFFF / WDT_CLOCK; + gwdt->wdd.max_timeout = UINT_MAX / gwdt->clk_freq; gwdt->wdd.parent = dev; /* @@ -165,19 +203,41 @@ static int ftwdt010_wdt_probe(struct platform_device *pdev) ret = devm_request_irq(dev, irq, ftwdt010_wdt_interrupt, 0, "watchdog bark", gwdt); if (ret) - return ret; + goto out_disable_extclk; gwdt->has_irq = true; } ret = devm_watchdog_register_device(dev, &gwdt->wdd); if (ret) { dev_err(&pdev->dev, "failed to register watchdog\n"); - return ret; + goto out_disable_extclk; } /* Set up platform driver data */ platform_set_drvdata(pdev, gwdt); - dev_info(dev, "FTWDT010 watchdog driver enabled\n"); + dev_info(dev, "FTWDT010 watchdog driver @%uHz\n", + gwdt->clk_freq); + + return 0; + +out_disable_extclk: + if (gwdt->use_extclk) + clk_disable_unprepare(gwdt->extclk); +out_disable_pclk: + if (!IS_ERR(gwdt->pclk)) + clk_disable_unprepare(gwdt->pclk); + + return ret; +} + +static int ftwdt010_wdt_remove(struct platform_device *pdev) +{ + struct ftwdt010_wdt *gwdt = platform_get_drvdata(pdev); + + writel(0, gwdt->base + FTWDT010_WDCR); + clk_disable_unprepare(gwdt->pclk); + if (gwdt->use_extclk) + clk_disable_unprepare(gwdt->extclk); return 0; } @@ -224,6 +284,7 @@ MODULE_DEVICE_TABLE(of, ftwdt010_wdt_match); static struct platform_driver ftwdt010_wdt_driver = { .probe = ftwdt010_wdt_probe, + .remove = ftwdt010_wdt_remove, .driver = { .name = "ftwdt010-wdt", .of_match_table = of_match_ptr(ftwdt010_wdt_match),