mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-16 00:34:20 +08:00
0f0a6a285e
Use devm_platform_ioremap_resource to reduce source code size, improve readability, and reduce the likelyhood of bugs. The conversion was done automatically with coccinelle using the following semantic patch. @r@ identifier res, pdev; expression a; expression index; expression e; @@ <+... - res = platform_get_resource(pdev, IORESOURCE_MEM, index); - a = devm_ioremap_resource(e, res); + a = devm_platform_ioremap_resource(pdev, index); ...+> @depends on r@ identifier r.res; @@ - struct resource *res; ... when != res @@ identifier res, pdev; expression index; expression a; @@ - struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, index); - a = devm_ioremap_resource(&pdev->dev, res); + a = devm_platform_ioremap_resource(pdev, index); Cc: Joel Stanley <joel@jms.id.au> Cc: Nicolas Ferre <nicolas.ferre@microchip.com> Cc: Alexandre Belloni <alexandre.belloni@bootlin.com> Cc: Florian Fainelli <f.fainelli@gmail.com> Cc: Linus Walleij <linus.walleij@linaro.org> Cc: Baruch Siach <baruch@tkos.co.il> Cc: Keguang Zhang <keguang.zhang@gmail.com> Cc: Vladimir Zapolskiy <vz@mleia.com> Cc: Kevin Hilman <khilman@baylibre.com> Cc: Matthias Brugger <matthias.bgg@gmail.com> Cc: Avi Fishman <avifishman70@gmail.com> Cc: Nancy Yuen <yuenn@google.com> Cc: Brendan Higgins <brendanhiggins@google.com> Cc: Wan ZongShun <mcuos.com@gmail.com> Cc: Michal Simek <michal.simek@xilinx.com> Cc: Sylvain Lemieux <slemieux.tyco@gmail.com> Cc: Kukjin Kim <kgene@kernel.org> Cc: Barry Song <baohua@kernel.org> Cc: Orson Zhai <orsonzhai@gmail.com> Cc: Patrice Chotard <patrice.chotard@st.com> Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com> Cc: Maxime Ripard <maxime.ripard@bootlin.com> Cc: Chen-Yu Tsai <wens@csie.org> Cc: Marc Gonzalez <marc.w.gonzalez@free.fr> Cc: Thierry Reding <thierry.reding@gmail.com> Cc: Shawn Guo <shawnguo@kernel.org> Signed-off-by: Guenter Roeck <linux@roeck-us.net> Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com> Tested-by: Alexandre Belloni <alexandre.belloni@bootlin.com> Acked-by: Joel Stanley <joel@jms.id.au> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Acked-by: Maxime Ripard <maxime.ripard@bootlin.com> Acked-by: Michal Simek <michal.simek@xilinx.com> (cadence/xilinx wdts) Acked-by: Thierry Reding <treding@nvidia.com> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com> Acked-by: Patrice Chotard <patrice.chotard@st.com> Acked-by: Vladimir Zapolskiy <vz@mleia.com> Signed-off-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Wim Van Sebroeck <wim@linux-watchdog.org>
169 lines
3.9 KiB
C
169 lines
3.9 KiB
C
/*
|
|
* Copyright (c) 2016 Yang Ling <gnaygnil@gmail.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
* option) any later version.
|
|
*/
|
|
|
|
#include <linux/clk.h>
|
|
#include <linux/module.h>
|
|
#include <linux/platform_device.h>
|
|
#include <linux/watchdog.h>
|
|
#include <loongson1.h>
|
|
|
|
#define DEFAULT_HEARTBEAT 30
|
|
|
|
static bool nowayout = WATCHDOG_NOWAYOUT;
|
|
module_param(nowayout, bool, 0444);
|
|
|
|
static unsigned int heartbeat;
|
|
module_param(heartbeat, uint, 0444);
|
|
|
|
struct ls1x_wdt_drvdata {
|
|
void __iomem *base;
|
|
struct clk *clk;
|
|
unsigned long clk_rate;
|
|
struct watchdog_device wdt;
|
|
};
|
|
|
|
static int ls1x_wdt_ping(struct watchdog_device *wdt_dev)
|
|
{
|
|
struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
|
|
|
|
writel(0x1, drvdata->base + WDT_SET);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int ls1x_wdt_set_timeout(struct watchdog_device *wdt_dev,
|
|
unsigned int timeout)
|
|
{
|
|
struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
|
|
unsigned int max_hw_heartbeat = wdt_dev->max_hw_heartbeat_ms / 1000;
|
|
unsigned int counts;
|
|
|
|
wdt_dev->timeout = timeout;
|
|
|
|
counts = drvdata->clk_rate * min(timeout, max_hw_heartbeat);
|
|
writel(counts, drvdata->base + WDT_TIMER);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int ls1x_wdt_start(struct watchdog_device *wdt_dev)
|
|
{
|
|
struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
|
|
|
|
writel(0x1, drvdata->base + WDT_EN);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int ls1x_wdt_stop(struct watchdog_device *wdt_dev)
|
|
{
|
|
struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
|
|
|
|
writel(0x0, drvdata->base + WDT_EN);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct watchdog_info ls1x_wdt_info = {
|
|
.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
|
|
.identity = "Loongson1 Watchdog",
|
|
};
|
|
|
|
static const struct watchdog_ops ls1x_wdt_ops = {
|
|
.owner = THIS_MODULE,
|
|
.start = ls1x_wdt_start,
|
|
.stop = ls1x_wdt_stop,
|
|
.ping = ls1x_wdt_ping,
|
|
.set_timeout = ls1x_wdt_set_timeout,
|
|
};
|
|
|
|
static int ls1x_wdt_probe(struct platform_device *pdev)
|
|
{
|
|
struct ls1x_wdt_drvdata *drvdata;
|
|
struct watchdog_device *ls1x_wdt;
|
|
unsigned long clk_rate;
|
|
int err;
|
|
|
|
drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
|
|
if (!drvdata)
|
|
return -ENOMEM;
|
|
|
|
drvdata->base = devm_platform_ioremap_resource(pdev, 0);
|
|
if (IS_ERR(drvdata->base))
|
|
return PTR_ERR(drvdata->base);
|
|
|
|
drvdata->clk = devm_clk_get(&pdev->dev, pdev->name);
|
|
if (IS_ERR(drvdata->clk))
|
|
return PTR_ERR(drvdata->clk);
|
|
|
|
err = clk_prepare_enable(drvdata->clk);
|
|
if (err) {
|
|
dev_err(&pdev->dev, "clk enable failed\n");
|
|
return err;
|
|
}
|
|
|
|
clk_rate = clk_get_rate(drvdata->clk);
|
|
if (!clk_rate) {
|
|
err = -EINVAL;
|
|
goto err0;
|
|
}
|
|
drvdata->clk_rate = clk_rate;
|
|
|
|
ls1x_wdt = &drvdata->wdt;
|
|
ls1x_wdt->info = &ls1x_wdt_info;
|
|
ls1x_wdt->ops = &ls1x_wdt_ops;
|
|
ls1x_wdt->timeout = DEFAULT_HEARTBEAT;
|
|
ls1x_wdt->min_timeout = 1;
|
|
ls1x_wdt->max_hw_heartbeat_ms = U32_MAX / clk_rate * 1000;
|
|
ls1x_wdt->parent = &pdev->dev;
|
|
|
|
watchdog_init_timeout(ls1x_wdt, heartbeat, &pdev->dev);
|
|
watchdog_set_nowayout(ls1x_wdt, nowayout);
|
|
watchdog_set_drvdata(ls1x_wdt, drvdata);
|
|
|
|
err = watchdog_register_device(&drvdata->wdt);
|
|
if (err) {
|
|
dev_err(&pdev->dev, "failed to register watchdog device\n");
|
|
goto err0;
|
|
}
|
|
|
|
platform_set_drvdata(pdev, drvdata);
|
|
|
|
dev_info(&pdev->dev, "Loongson1 Watchdog driver registered\n");
|
|
|
|
return 0;
|
|
err0:
|
|
clk_disable_unprepare(drvdata->clk);
|
|
return err;
|
|
}
|
|
|
|
static int ls1x_wdt_remove(struct platform_device *pdev)
|
|
{
|
|
struct ls1x_wdt_drvdata *drvdata = platform_get_drvdata(pdev);
|
|
|
|
watchdog_unregister_device(&drvdata->wdt);
|
|
clk_disable_unprepare(drvdata->clk);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct platform_driver ls1x_wdt_driver = {
|
|
.probe = ls1x_wdt_probe,
|
|
.remove = ls1x_wdt_remove,
|
|
.driver = {
|
|
.name = "ls1x-wdt",
|
|
},
|
|
};
|
|
|
|
module_platform_driver(ls1x_wdt_driver);
|
|
|
|
MODULE_AUTHOR("Yang Ling <gnaygnil@gmail.com>");
|
|
MODULE_DESCRIPTION("Loongson1 Watchdog Driver");
|
|
MODULE_LICENSE("GPL");
|