From patchwork Sat Sep 5 14:45:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?J=2E_Neusch=C3=A4fer?= X-Patchwork-Id: 11759145 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3C69D112E for ; Sat, 5 Sep 2020 14:54:30 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id EF41D208B3 for ; Sat, 5 Sep 2020 14:54:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="G/MuiK3S"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=gmx.net header.i=@gmx.net header.b="fGPuaB7R" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EF41D208B3 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=gmx.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=seCV5YD3mLhFIp5bYcHt0h+VsKqEBGVcIPs0L8WYxAw=; b=G/MuiK3STgp/qtn2DjelFOGhs tbEtXn18t4xO7NbvWxAMbK7lB2J49F1ldUlpSM6acWGTrOFl/MpEysinAozeDF4uILx+BJlxCPd65 IWkG/vjkrxLXe/zL4ELzleZloBhvn95v42kYpnebyJnRbM1XRuOxYbXQaP7Kz17GwUhjOs9yLGEcb cnp1j9PXrCZ0MAbzJZxiXjY+0VpmnUuXhusX6Mgnp+V+vo+u/HAwDmScVpZ3zLgroPCOySchsgEhr osbQv5BQthN7kBfIVKESh7JCe5MP40Q7YMPQIvZYRifV2dHMW9Hl7Nd9ss6Hm7MGhQsqsPblQIAMg rDpZ3SxUQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kEZYJ-0002wV-1q; Sat, 05 Sep 2020 14:52:27 +0000 Received: from mout.gmx.net ([212.227.15.18]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kEZYF-0002vz-QT for linux-arm-kernel@lists.infradead.org; Sat, 05 Sep 2020 14:52:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1599317541; bh=AmcVqt376Z16SI/dPXXTlsZxd3ygG5eId5RZWTXwmYU=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=fGPuaB7RffFFwirpNW7YWlIhxcYSBDK9NwYcLhOZsn+FKfnORixNe0U8cYAXJB7Jr clkgFHJ9pHaDPKQA3DeHzBnEw4AIqTW2dgvJ77I/1Rc2WVgf2gFgBG+JWQyj4DWxct kSRFpo5yYy//AufxS0aSImyCfFD5qs29mAReVoEI= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from longitude ([5.146.195.151]) by mail.gmx.com (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MTiPv-1k8pu63Yvl-00TzFu; Sat, 05 Sep 2020 16:45:36 +0200 From: =?utf-8?q?Jonathan_Neusch=C3=A4fer?= To: linux-kernel@vger.kernel.org Subject: [PATCH v2 08/10] rtc: New driver for RTC in Netronix embedded controller Date: Sat, 5 Sep 2020 16:45:00 +0200 Message-Id: <20200905144503.1067124-1-j.neuschaefer@gmx.net> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200905133230.1014581-1-j.neuschaefer@gmx.net> References: <20200905133230.1014581-1-j.neuschaefer@gmx.net> MIME-Version: 1.0 X-Provags-ID: V03:K1:s1Iuy0DqF3nvuCeJDEoK6kI2mBJxXiedCE/mjpNaXYYDwjmv37D RxR6zMa9OiHI6c3hAO40fXIgiLrCbUToOLmrQxoNxuNVYu5CsOSFv5sh2yR5c8DpscEysJC /nYdzmBrKo0DxrcxUpjxHo8yn5AvKv2KxrZCg4floK3I5QMI8c2XBgs4igC0T5OCv0/Kof+ mUzjOZUPeEP+5QnC6jNGA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:5SWfn4hfEiY=:iKTvceN57gmD2GEmGlnaRC RG6omwJ2nmiByVrfSj2qTcvsGSRq4dCShYR0ztwHY/+pvWJnhHx57f3WkdXcHrTHlNJI+ZckN 93UC+Nljb191MhTugvULy4kGhd8gF7O4oQchDGJhcOnUAJTfbFUyLAq+pkCVh5owGHpsxBJMu QruJZ/8T8b9EtACcBt6rCfuzdXzoEcU6cgZ+9TYOQ9j1cfrbfrCWBOz9mM4MnlIUK/u/gFzdN b4TcY3Hby6Zex736wXlWtJ0bKa8THzZu/TBLDw8e778CemgighiAPzrARw6Q3rKZFY5tSosT4 8pr8JrGbj6k1IjQOOApZdVHoKO6fV03CrhMNC+5Q4I6r/c7oh0eGalmxBp+SFuuxXj4nlBtq5 ApREmRkB/SjeCP7dX/KY23Sl2f0R4177u4XF8cEiAN8yPU4Eydb1KCF1PNpclY6IpvY4euI00 xPnTTp+xFnKZpiGtmdubG7aTUWAM/YpGOUaFr9AdQhLIWshR4WgaNPVzfDwC895JcA2z1Fz/m +fEB2KFqoTir0p6g2FxlH9+6vwJ0M9zMSEDfTlnsWMATOiDs7G1h5YyRNpwMAkIeUkyqOSb0C 9/OvhNOjdddYKzKR/Vgqu4Hm1z9WHmI3Qs/HszsfwlcoNQ8Uf65BpWL+JCI+jGutp4/9oyXHX opVEk903ZCQR6Sk0SG5e+dVcWsls7fYTgACOwwkW6LcCafm7+BtzOZ6BVLtX8d6AuhH3zAmZz lpM9w6XG3NQMPABIAk4RZDJq3b/4cBraU3lgIuHajVomU/Tg8C8NStwOwgXoTrD8G0+jT6ksT yRASnL/TFOf26ugj481Nq8OVsiQu1B3rA+952GmDw/KJ5IG4YFzIepFABPmCBPjqoEJ2NlO03 VltVTv161JnAffhedcsGTxScRmW5bcaWlXvqAYcachxz7M6201hLrqYcdqcfor9LuB7unPwjm aF+0GpI/1QdWz0ffFNYBrBrQRr1/dILhyWvl9s0r/CrXuNxQa5LV05J4F7xKtXTNFNwuq6rg9 W/1SLmLQ9+Rug9XWSfU5eG/Dd7yBTMAtouSapfomMADP+7i2fGLR6zuXnkRxt0lXwxtUfvW6T lCwiewDafZCRn1Wjefu/l+0G4o5Lpz6oPs1b12OgKpgRwvrZ6VdfIK3DYa6j6cxBbzEQ9Sy3l pkj6ZYbgXI2vYWUXjp6dcdxrA2ATZ77r9Fo53MplDFgWjgePbohv7cyRCzz6n9rK9XUqRRL09 PIcVlucfhK5j6fELUF5GXvB+8HngmScGagPULcQ== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200905_105224_078666_C2BA2262 X-CRM114-Status: GOOD ( 27.79 ) X-Spam-Score: -0.9 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.9 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [212.227.15.18 listed in list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [j.neuschaefer[at]gmx.net] -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [212.227.15.18 listed in wl.mailspike.net] X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alexandre Belloni , Heiko Stuebner , linux-pwm@vger.kernel.org, Linus Walleij , Thierry Reding , Fabio Estevam , linux-rtc@vger.kernel.org, Arnd Bergmann , Mauro Carvalho Chehab , Sam Ravnborg , Daniel Palmer , Andreas Kemnade , NXP Linux Team , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , devicetree@vger.kernel.org, Stephan Gerhold , allen , Sascha Hauer , =?utf-8?q?Jonathan_Neusch=C3=A4fer?= , Lubomir Rintel , Rob Herring , Lee Jones , linux-arm-kernel@lists.infradead.org, Alessandro Zummo , Mark Brown , Pengutronix Kernel Team , Heiko Stuebner , Josua Mayer , Shawn Guo , "David S. Miller" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org With this driver, mainline Linux can keep its time and date in sync with the vendor kernel. Advanced functionality like alarm and automatic power-on is not yet supported. Signed-off-by: Jonathan Neuschäfer --- v2: - Rework top-of-file comment [Lee Jones] - Sort the #include lines [Alexandre Belloni] - don't align = signs in struct initializers [Uwe Kleine-König] - Switch to regmap - Fix register number used to read minutes and seconds - Prefix registers with NTXEC_REG_ - Add help text to the Kconfig option - Use devm_rtc_allocate_device and rtc_register_device, set ->range_min and ->range_max --- drivers/rtc/Kconfig | 8 +++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-ntxec.c | 130 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 drivers/rtc/rtc-ntxec.c -- 2.28.0 diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 48c536acd777f..ae8f3dc36c9a3 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -1301,6 +1301,14 @@ config RTC_DRV_CROS_EC This driver can also be built as a module. If so, the module will be called rtc-cros-ec. +config RTC_DRV_NTXEC + tristate "Netronix embedded controller RTC driver" + depends on MFD_NTXEC + help + Say yes here if you want to support the RTC functionality of the + embedded controller found in certain e-book readers designed by the + ODM Netronix. + comment "on-CPU RTC drivers" config RTC_DRV_ASM9260 diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 880e08a409c3d..733479db18896 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -111,6 +111,7 @@ obj-$(CONFIG_RTC_DRV_MT7622) += rtc-mt7622.o obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o obj-$(CONFIG_RTC_DRV_MXC) += rtc-mxc.o obj-$(CONFIG_RTC_DRV_MXC_V2) += rtc-mxc_v2.o +obj-$(CONFIG_RTC_DRV_NTXEC) += rtc-ntxec.o obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o obj-$(CONFIG_RTC_DRV_OPAL) += rtc-opal.o obj-$(CONFIG_RTC_DRV_PALMAS) += rtc-palmas.o diff --git a/drivers/rtc/rtc-ntxec.c b/drivers/rtc/rtc-ntxec.c new file mode 100644 index 0000000000000..82eb6352353cd --- /dev/null +++ b/drivers/rtc/rtc-ntxec.c @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * The Netronix embedded controller is a microcontroller found in some + * e-book readers designed by the ODM Netronix, Inc. It contains RTC, + * battery monitoring, system power management, and PWM functionality. + * + * This driver implements access to the RTC time and date. + * + * Copyright 2020 Jonathan Neuschäfer + */ + +#include +#include +#include +#include +#include +#include +#include + +struct ntxec_rtc { + struct device *dev; + struct ntxec *ec; +}; + +#define NTXEC_REG_WRITE_YEAR 0x10 +#define NTXEC_REG_WRITE_MONTH 0x11 +#define NTXEC_REG_WRITE_DAY 0x12 +#define NTXEC_REG_WRITE_HOUR 0x13 +#define NTXEC_REG_WRITE_MINUTE 0x14 +#define NTXEC_REG_WRITE_SECOND 0x15 + +/* Convert an 8-bit value into the correct format for writing into a register */ +#define u8_to_reg(x) (((x) & 0xff) << 8) + +#define NTXEC_REG_READ_YM 0x20 +#define NTXEC_REG_READ_DH 0x21 +#define NTXEC_REG_READ_MS 0x23 + +static int ntxec_read_time(struct device *dev, struct rtc_time *tm) +{ + struct ntxec_rtc *rtc = dev_get_drvdata(dev); + unsigned int value; + int res; + + res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_YM, &value); + if (res < 0) + return res; + + tm->tm_year = (value >> 8) + 100; + tm->tm_mon = (value & 0xff) - 1; + + res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_DH, &value); + if (res < 0) + return res; + + tm->tm_mday = value >> 8; + tm->tm_hour = value & 0xff; + + res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MS, &value); + if (res < 0) + return res; + + tm->tm_min = value >> 8; + tm->tm_sec = value & 0xff; + + return 0; +} + +static int ntxec_set_time(struct device *dev, struct rtc_time *tm) +{ + struct ntxec_rtc *rtc = dev_get_drvdata(dev); + int res = 0; + + res |= regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_YEAR, u8_to_reg(tm->tm_year - 100)); + res |= regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_MONTH, u8_to_reg(tm->tm_mon + 1)); + res |= regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_DAY, u8_to_reg(tm->tm_mday)); + res |= regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_HOUR, u8_to_reg(tm->tm_hour)); + res |= regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_MINUTE, u8_to_reg(tm->tm_min)); + res |= regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_SECOND, u8_to_reg(tm->tm_sec)); + + return (res < 0) ? -EIO : 0; +} + +static const struct rtc_class_ops ntxec_rtc_ops = { + .read_time = ntxec_read_time, + .set_time = ntxec_set_time, +}; + +static int ntxec_rtc_probe(struct platform_device *pdev) +{ + struct rtc_device *dev; + struct ntxec_rtc *rtc; + + rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); + if (!rtc) + return -ENOMEM; + + rtc->dev = &pdev->dev; + rtc->ec = dev_get_drvdata(pdev->dev.parent); + platform_set_drvdata(pdev, rtc); + + dev = devm_rtc_allocate_device(&pdev->dev); + if (IS_ERR(dev)) + return PTR_ERR(dev); + + dev->ops = &ntxec_rtc_ops; + dev->range_min = RTC_TIMESTAMP_BEGIN_2000; + dev->range_max = RTC_TIMESTAMP_END_2255; + + return rtc_register_device(dev); +} + +static const struct of_device_id ntxec_rtc_of_match[] = { + { .compatible = "netronix,ntxec-rtc" }, + { }, +}; +MODULE_DEVICE_TABLE(of, ntxec_rtc_of_match); + +static struct platform_driver ntxec_rtc_driver = { + .driver = { + .name = "ntxec-rtc", + .of_match_table = ntxec_rtc_of_match, + }, + .probe = ntxec_rtc_probe, +}; +module_platform_driver(ntxec_rtc_driver); + +MODULE_AUTHOR("Jonathan Neuschäfer "); +MODULE_DESCRIPTION("RTC driver for Netronix EC"); +MODULE_LICENSE("GPL");