Received: by 2002:a05:6520:1682:b0:147:d1a0:b502 with SMTP id ck2csp5625030lkb; Mon, 11 Oct 2021 10:11:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzi0WjwCrD5Dsc+PIKkrfksMGn62HoxN0J6tYBDFAnsT0HIpDBYz9AoatiSpm7DNwQFk3/o X-Received: by 2002:a17:906:7ac9:: with SMTP id k9mr5605045ejo.411.1633972264975; Mon, 11 Oct 2021 10:11:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633972264; cv=none; d=google.com; s=arc-20160816; b=MYBf2A8/QkAzdRwwhbEs1+rlHMJkonYa9Qyfy9uw5+/jOof59gACyU6dce7SASSQlV EkD3fJKq0WDQ/BQFDHoK73JvYw/IlRRt0YLLjO92qchqrS2rw8xu3qfavn/yx3L6GftA ohHv97EeJQyfczo82phvQRDbQPHAlitTSQ3uCI9adXM5tiHYGF8A7wsuwhVnAnMFm1gW wuzUJtgSVcg2jL3Gx7mEPpGeq9TUQSJzua35vjggAAaQyjc+tOGb2afLgFpIoh4rjep7 oiR+aPMtw3TrOtQpVf7GgHW8xruk+q7ciFkZb+4yNsjTMmV/Em8uOgENesHPmg2N1gpz IBmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=C1+DYzkI3//Zq83r0uR5Qhr/5ezfL4chw3o+B3eGrOw=; b=cDrN/pLAtAluU3coSSBByyrmquFEyLeYLYJHoGKHA1lXc0LrTmWxbfzFJ8LEjgm8L2 Qw9Jujuzm99wDrQlD7xStdzGGJicsI8gSDwKrtvDwlR+WyBR5IcSa0KEKMAQ8ie8WcAn IG3+BUWeXLYz3ogxsnYRE2mHZnAOBus2EPamwe42vRJw2paLPa2wMILt1nMWogKvawSE IK2h8HvRDhVKKeHnq3B7ES5b+qk2fJmRDDItVWadIpAcOrSx9jpSwIxfixc+beoAr9kB KFNBG4T1DECGEmBH63oFJiiTinZ4F42GV4GBtTxjCOO9y4TYFqhMOYLWH1X5sAtG0Map DVDg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k9si13864273edv.565.2021.10.11.10.10.42; Mon, 11 Oct 2021 10:11:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233421AbhJKRIr (ORCPT + 96 others); Mon, 11 Oct 2021 13:08:47 -0400 Received: from hostingweb31-40.netsons.net ([89.40.174.40]:40018 "EHLO hostingweb31-40.netsons.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232477AbhJKRIq (ORCPT ); Mon, 11 Oct 2021 13:08:46 -0400 Received: from [77.244.183.192] (port=63592 helo=melee.dev.aim) by hostingweb31.netsons.net with esmtpa (Exim 4.94.2) (envelope-from ) id 1mZxfQ-00DXft-LW; Mon, 11 Oct 2021 17:56:44 +0200 From: Luca Ceresoli To: linux-kernel@vger.kernel.org Cc: Luca Ceresoli , Lee Jones , Rob Herring , Alessandro Zummo , Alexandre Belloni , Chanwoo Choi , Krzysztof Kozlowski , Bartlomiej Zolnierkiewicz , Wim Van Sebroeck , Guenter Roeck , devicetree@vger.kernel.org, linux-rtc@vger.kernel.org, linux-watchdog@vger.kernel.org, Chiwoong Byun , Laxman Dewangan Subject: [PATCH 7/8] watchdog: max77714: add driver for the watchdog in the MAX77714 PMIC Date: Mon, 11 Oct 2021 17:56:14 +0200 Message-Id: <20211011155615.257529-8-luca@lucaceresoli.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211011155615.257529-1-luca@lucaceresoli.net> References: <20211011155615.257529-1-luca@lucaceresoli.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - hostingweb31.netsons.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - lucaceresoli.net X-Get-Message-Sender-Via: hostingweb31.netsons.net: authenticated_id: luca+lucaceresoli.net/only user confirmed/virtual account not confirmed X-Authenticated-Sender: hostingweb31.netsons.net: luca@lucaceresoli.net X-Source: X-Source-Args: X-Source-Dir: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a simple driver to suppor the watchdog embedded in the Maxim MAX77714 PMIC. Signed-off-by: Luca Ceresoli --- MAINTAINERS | 1 + drivers/watchdog/Kconfig | 9 ++ drivers/watchdog/Makefile | 1 + drivers/watchdog/max77714_wdt.c | 171 ++++++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 drivers/watchdog/max77714_wdt.c diff --git a/MAINTAINERS b/MAINTAINERS index df394192f14e..08900b5729a5 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11390,6 +11390,7 @@ M: Luca Ceresoli S: Maintained F: Documentation/devicetree/bindings/mfd/maxim,max77714.yaml F: drivers/mfd/max77714.c +F: drivers/watchdog/max77714_wdt.c F: include/linux/mfd/max77714.h MAXIM MAX77802 PMIC REGULATOR DEVICE DRIVER diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig index bf59faeb3de1..00bc3f932a6c 100644 --- a/drivers/watchdog/Kconfig +++ b/drivers/watchdog/Kconfig @@ -699,6 +699,15 @@ config MAX77620_WATCHDOG MAX77620 chips. To compile this driver as a module, choose M here: the module will be called max77620_wdt. +config MAX77714_WATCHDOG + tristate "Maxim MAX77714 Watchdog Timer" + depends on MFD_MAX77714 || COMPILE_TEST + help + This is the driver for watchdog timer in the MAX77714 PMIC. + Say 'Y' here to enable the watchdog timer support for + MAX77714 chips. To compile this driver as a module, + choose M here: the module will be called max77714_wdt. + config IMX2_WDT tristate "IMX2+ Watchdog" depends on ARCH_MXC || ARCH_LAYERSCAPE || COMPILE_TEST diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile index 1bd2d6f37c53..268a942311a0 100644 --- a/drivers/watchdog/Makefile +++ b/drivers/watchdog/Makefile @@ -215,6 +215,7 @@ obj-$(CONFIG_WM831X_WATCHDOG) += wm831x_wdt.o obj-$(CONFIG_WM8350_WATCHDOG) += wm8350_wdt.o obj-$(CONFIG_MAX63XX_WATCHDOG) += max63xx_wdt.o obj-$(CONFIG_MAX77620_WATCHDOG) += max77620_wdt.o +obj-$(CONFIG_MAX77714_WATCHDOG) += max77714_wdt.o obj-$(CONFIG_ZIIRAVE_WATCHDOG) += ziirave_wdt.o obj-$(CONFIG_SOFT_WATCHDOG) += softdog.o obj-$(CONFIG_MENF21BMC_WATCHDOG) += menf21bmc_wdt.o diff --git a/drivers/watchdog/max77714_wdt.c b/drivers/watchdog/max77714_wdt.c new file mode 100644 index 000000000000..2d468db849f9 --- /dev/null +++ b/drivers/watchdog/max77714_wdt.c @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Maxim MAX77714 Watchdog Driver + * + * Copyright (C) 2021 Luca Ceresoli + * Author: Luca Ceresoli + */ + +#include +#include +#include +#include +#include +#include +#include + +struct max77714_wdt { + struct device *dev; + struct regmap *rmap; + struct watchdog_device wd_dev; +}; + +/* Timeout in seconds, indexed by TWD bits of CNFG_GLBL2 register */ +unsigned int max77714_margin_value[] = { 2, 16, 64, 128 }; + +static int max77714_wdt_start(struct watchdog_device *wd_dev) +{ + struct max77714_wdt *wdt = watchdog_get_drvdata(wd_dev); + + return regmap_update_bits(wdt->rmap, MAX77714_CNFG_GLBL2, + MAX77714_WDTEN, MAX77714_WDTEN); +} + +static int max77714_wdt_stop(struct watchdog_device *wd_dev) +{ + struct max77714_wdt *wdt = watchdog_get_drvdata(wd_dev); + + return regmap_update_bits(wdt->rmap, MAX77714_CNFG_GLBL2, + MAX77714_WDTEN, 0); +} + +static int max77714_wdt_ping(struct watchdog_device *wd_dev) +{ + struct max77714_wdt *wdt = watchdog_get_drvdata(wd_dev); + + return regmap_update_bits(wdt->rmap, MAX77714_CNFG_GLBL3, + MAX77714_WDTC, 1); +} + +static int max77714_wdt_set_timeout(struct watchdog_device *wd_dev, + unsigned int timeout) +{ + struct max77714_wdt *wdt = watchdog_get_drvdata(wd_dev); + unsigned int new_timeout, new_twd; + int err; + + for (new_twd = 0; new_twd < ARRAY_SIZE(max77714_margin_value) - 1; new_twd++) + if (timeout <= max77714_margin_value[new_twd]) + break; + + /* new_wdt is not out of bounds here due to the "- 1" in the for loop */ + new_timeout = max77714_margin_value[new_twd]; + + /* + * "If the value of TWD needs to be changed, clear the system + * watchdog timer first [...], then change the value of TWD." + * (MAX77714 datasheet) + */ + err = regmap_update_bits(wdt->rmap, MAX77714_CNFG_GLBL3, + MAX77714_WDTC, 1); + if (err) + return err; + + err = regmap_update_bits(wdt->rmap, MAX77714_CNFG_GLBL2, + MAX77714_TWD_MASK, new_twd); + if (err) + return err; + + wd_dev->timeout = new_timeout; + + dev_dbg(wdt->dev, "New timeout = %u s (WDT = 0x%x)", new_timeout, new_twd); + + return 0; +} + +static const struct watchdog_info max77714_wdt_info = { + .identity = "max77714-watchdog", + .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, +}; + +static const struct watchdog_ops max77714_wdt_ops = { + .start = max77714_wdt_start, + .stop = max77714_wdt_stop, + .ping = max77714_wdt_ping, + .set_timeout = max77714_wdt_set_timeout, +}; + +static int max77714_wdt_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct max77714_wdt *wdt; + struct watchdog_device *wd_dev; + unsigned int regval; + int err; + + wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); + if (!wdt) + return -ENOMEM; + + wdt->dev = dev; + + wd_dev = &wdt->wd_dev; + wd_dev->info = &max77714_wdt_info; + wd_dev->ops = &max77714_wdt_ops; + wd_dev->min_timeout = 2; + wd_dev->max_timeout = 128; + + platform_set_drvdata(pdev, wdt); + watchdog_set_drvdata(wd_dev, wdt); + + wdt->rmap = dev_get_regmap(dev->parent, NULL); + if (!wdt->rmap) + return dev_err_probe(wdt->dev, -ENODEV, "Failed to get parent regmap\n"); + + /* WD_RST_WK: if 1 wdog restarts; if 0 wdog shuts down */ + err = regmap_update_bits(wdt->rmap, MAX77714_CNFG2_ONOFF, + MAX77714_WD_RST_WK, MAX77714_WD_RST_WK); + if (err) + return dev_err_probe(wdt->dev, err, "Error updating CNFG2_ONOFF\n"); + + err = regmap_read(wdt->rmap, MAX77714_CNFG_GLBL2, ®val); + if (err) + return dev_err_probe(wdt->dev, err, "Error reading CNFG_GLBL2\n"); + + /* enable watchdog | enable auto-clear in sleep state */ + regval |= (MAX77714_WDTEN | MAX77714_WDTSLPC); + + err = regmap_write(wdt->rmap, MAX77714_CNFG_GLBL2, regval); + if (err) + return dev_err_probe(wdt->dev, err, "Error writing CNFG_GLBL2\n"); + + wd_dev->timeout = max77714_margin_value[regval & MAX77714_TWD_MASK]; + + dev_dbg(wdt->dev, "Timeout = %u s (WDT = 0x%x)", + wd_dev->timeout, regval & MAX77714_TWD_MASK); + + set_bit(WDOG_HW_RUNNING, &wd_dev->status); + + watchdog_stop_on_unregister(wd_dev); + + err = devm_watchdog_register_device(dev, wd_dev); + if (err) + return dev_err_probe(dev, err, "Cannot register watchdog device\n"); + + dev_info(dev, "registered as /dev/watchdog%d\n", wd_dev->id); + + return 0; +} + +static struct platform_driver max77714_wdt_driver = { + .driver = { + .name = "max77714-watchdog", + }, + .probe = max77714_wdt_probe, +}; + +module_platform_driver(max77714_wdt_driver); + +MODULE_DESCRIPTION("MAX77714 watchdog timer driver"); +MODULE_AUTHOR("Luca Ceresoli "); +MODULE_LICENSE("GPL v2"); -- 2.25.1