From patchwork Sun Nov 22 22:27:37 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: 11924035 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4A72C5519F for ; Sun, 22 Nov 2020 22:41:03 +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 6419020776 for ; Sun, 22 Nov 2020 22:41:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="DFpVr2im"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=gmx.net header.i=@gmx.net header.b="WwrbWWw2" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6419020776 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmx.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.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=s+jz3NAQKqgFCw1tDGwBsjJ1C2FRqtQ1bBL+RZapt7o=; b=DFpVr2imFlEXxqhAnWgOaM5Vy +BJkbLFfvlfNE2i33V3XdHlcWcPHlU/WwT6uvTyC2YgV6LmmKLeJvxRAOFvhfsLg8IjP7XVFW8s8x ukp6FQ22kJ78JEZxsScGBg0bEJIptTuqxAqeycKFZQnfHEmOSEB4+ywx1nRtf49hrcHKkVxZviBco th7KibRC9O0ulYiaotV94SgfeRi0VK4Oy+tqdwBUoMAbVnybt3rk442/WyNtFPJIHtvXrRjnn6LiN 2tF1XJfANRLbwaf36lKD1bax0KOXqGPNAqCExbZCAJ5qOYzcuV7m0b3cX246hTdRSgIrF1SCJu5ay 6UHWorR8A==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kgy1R-0003U5-6y; Sun, 22 Nov 2020 22:39:53 +0000 Received: from mout.gmx.net ([212.227.17.21]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kgy1L-0003SL-58 for linux-arm-kernel@lists.infradead.org; Sun, 22 Nov 2020 22:39:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1606084736; bh=jXZYKt1n1DuufO0ZGjj1ddg8RrIBzQlHWAXXJJOO2C4=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=WwrbWWw2xo8f38fkc8ed6Wb6KdUbpX/izzoGcvBzBWG9Zi0sIwrEpRfH9REIejDHL FUgZhJP7BWbWzlZuhqpfJUShYH6WE3ocOouKXxWj1NmU64o0oXwEPOLU1beMqfjL6b l5QO5e+x3qiXs1vlgoHG1Cxdt4uVHbak6O4onYJk= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from longitude ([37.201.214.162]) by mail.gmx.com (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MzQkK-1kKmMS1ojg-00vPHa; Sun, 22 Nov 2020 23:38:56 +0100 From: =?utf-8?q?Jonathan_Neusch=C3=A4fer?= To: linux-kernel@vger.kernel.org Subject: [PATCH v4 5/7] rtc: New driver for RTC in Netronix embedded controller Date: Sun, 22 Nov 2020 23:27:37 +0100 Message-Id: <20201122222739.1455132-6-j.neuschaefer@gmx.net> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201122222739.1455132-1-j.neuschaefer@gmx.net> References: <20201122222739.1455132-1-j.neuschaefer@gmx.net> MIME-Version: 1.0 X-Provags-ID: V03:K1:JltiIA9UG40wJKVmGENJpHVueJ0cA0GAUhJzig+CGAhKfmZ5IQQ s9ZvOHiCpfO/OrkveJV3Y2em2dEOBi1nE4sEUMa9q8nN8JsPYCMPRhz0lKt0BvxSoySxeu3 wbWbbUJ+m7NcadF+dFHwLO0xaFwsnryxDtsNFocxpZv8BO5TOEv7LErcSAfEa3o2BzXYlBZ gFn1wYPs1tJv6d1fdwMjw== X-UI-Out-Filterresults: notjunk:1;V03:K0:71rcezld2ys=:T2Xofc9LQyO6Cvtzg/2F1v 6dB23r4qU0Z10lf/0CB8HIJX9f49j34zINJcDJ6Fv+QueVVyk2iBwvVMYa19+xL3SyJ/ljo80 8141IdnvEZ1mQ1uIzYPRMIcWiUKw91BWlen5KTh6fP3N47cttlhUN1/nea5zWKR9wEfq5hCrJ rfprsRq7dV02k+bnsqj1OWeG6Ju2CW508BEqQn05gJhSPbs66NlE0fgCWHsmoYqTLpEeC6h2p szJkIjhaya/UfwvQbPHlMTBf5m+0WtazryByrQdanAIRo3hEqBUwV9aQSoVzok080H2yDlwBg K00a5dSr8wy/AM2gRyJUC56C6nAypUqXRdIQ1T4dzJenuAdaFedClKymCg816ge8CkVIGNJAo fLp2x0RKG8Fk4lb32tTCvi8jtp/kWqwSyDmx+9Vo10BxcJfi2Sc9l3bwwH6e9vacV0OwgpL1b HSqwUiU7ZSstfneCbycTj6Xe/gO+zWdlCSOLF5DxuDVa6AWogKGiC5Yu12S/R0gjyk21A1xol t3A7m2WyUcAzWfRfQQJzFrWQ9wcGlKDjh49jdTExC+mp2yvinuhZ2KpJhg/k+EFblxuVKeUV8 GHVEdYckcL9FIRMusOr/AdPxMuu6mV1tej62zqXxwiKXmpD+IV2w9ZjBkADpC/QIpY8l82Dgg olSzjm1JFs5gic5pQl4VaxXk21YLzblJ/YhoKUTwTnVY5AV60NpraUuiHgWz58P8IiWIK1U3X LYWHiE7dik17fR2eEHmve1/SnYhl1bWSMvBj2819BejnVEKECNX+TyUVpPbThO2GeSkztROWZ cwzU3tMQCODSyU14WeF3t5j0p2iI5/QUAy+j3lOz95fQTUMJ0mX8u5G87lnp1dyawTZMQk8+n IZDK2npODIi9K/2YMCGw== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201122_173947_494742_52577005 X-CRM114-Status: GOOD ( 30.88 ) 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 , Andy Shevchenko , 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+linux-arm-kernel=archiver.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 Acked-by: Alexandre Belloni --- v4: - Remove "driver" from Kconfig entry for consistency with most other entries - Add missing MODULE_ALIAS line - Give NTXEC_REG_READ_ macros longer names - Solve the read tearing issue using Alexandre Belloni's algorithm - Solve the write tearing issue using Uwe Kleine-König's algorithm - Spell out ODM v3: - https://lore.kernel.org/lkml/20200924192455.2484005-6-j.neuschaefer@gmx.net/ - Add email address to copyright line - Remove OF compatible string and don't include linux/of_device.h - Don't use a comma after sentinels - Avoid ret |= ... pattern - Move 8-bit register conversion to ntxec.h - Relicense as GPLv2 or later v2: - https://lore.kernel.org/lkml/20200905133230.1014581-7-j.neuschaefer@gmx.net/ - 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 | 158 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 drivers/rtc/rtc-ntxec.c -- 2.29.2 diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 65ad9d0b47ab1..fe009949728b3 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -1311,6 +1311,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" + 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 + original design manufacturer Netronix. + comment "on-CPU RTC drivers" config RTC_DRV_ASM9260 diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index bfb57464118d0..5f2a7582b2780 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..dad5aba2852f1 --- /dev/null +++ b/drivers/rtc/rtc-ntxec.c @@ -0,0 +1,158 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * The Netronix embedded controller is a microcontroller found in some + * e-book readers designed by the original design manufacturer 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 + +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 + +#define NTXEC_REG_READ_YEAR_MONTH 0x20 +#define NTXEC_REG_READ_MDAY_HOUR 0x21 +#define NTXEC_REG_READ_MINUTE_SECOND 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; + +retry: + res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MINUTE_SECOND, &value); + if (res < 0) + return res; + + tm->tm_min = value >> 8; + tm->tm_sec = value & 0xff; + + res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MDAY_HOUR, &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_YEAR_MONTH, &value); + if (res < 0) + return res; + + tm->tm_year = (value >> 8) + 100; + tm->tm_mon = (value & 0xff) - 1; + + /* + * Read the minutes/seconds field again. If it changed since the first + * read, we can't assume that the values read so far are consistent, + * and should start from the beginning. + */ + res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MINUTE_SECOND, &value); + if (res < 0) + return res; + + if (tm->tm_min != value >> 8 || tm->tm_sec != (value & 0xff)) + goto retry; + + 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; + + /* + * To avoid time overflows while we're writing the full date/time, + * set the seconds field to zero before doing anything else. For the + * next 59 seconds (plus however long it takes until the RTC's next + * update of the second field), the seconds field will not overflow + * into the other fields. + */ + res = regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_SECOND, ntxec_reg8(0)); + if (res) + return res; + + res = regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_YEAR, ntxec_reg8(tm->tm_year - 100)); + if (res) + return res; + + res = regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_MONTH, ntxec_reg8(tm->tm_mon + 1)); + if (res) + return res; + + res = regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_DAY, ntxec_reg8(tm->tm_mday)); + if (res) + return res; + + res = regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_HOUR, ntxec_reg8(tm->tm_hour)); + if (res) + return res; + + res = regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_MINUTE, ntxec_reg8(tm->tm_min)); + if (res) + return res; + + return regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_SECOND, ntxec_reg8(tm->tm_sec)); +} + +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 = 9025257599LL; /* 2255-12-31 23:59:59 */ + + return rtc_register_device(dev); +} + +static struct platform_driver ntxec_rtc_driver = { + .driver = { + .name = "ntxec-rtc", + }, + .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"); +MODULE_ALIAS("platform:ntxec-rtc");