Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp1207013pxb; Sat, 9 Jan 2021 11:09:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJxqfdr9U2gpREuxL97LGBnO/i2N6hoI96CgijVUHm5UtwCFK92lglOCVv3THiNdeL3drByz X-Received: by 2002:a17:907:2116:: with SMTP id qn22mr6144291ejb.483.1610219395751; Sat, 09 Jan 2021 11:09:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610219395; cv=none; d=google.com; s=arc-20160816; b=XtJR5xrSrBsqEFzkgqlK1daMJsh0NYlk5g4aVkmPoZ+4jphgKNlrASrwliQYiXmHM/ F/JFV8xkWYkzJ0SOGhxzm1Zxn+BDbiLyyo5Kf4jNS42Ugw6YHgOZCkCjlnYMLqhz+qvZ lubQ07LaM8+IdbY807smxmVrCS19+pXalufsI5ryGsDqSUbAsF815npmKG8XBucGhyD7 HLNDRFl382hhocjtsIIa6AP0vwFBW4AB1OW8umLjsWOrFWogdVRQXjgXOqYlES9cqodN kI7KunkJIxtL65OPJi6FkIq3dUQEKwPh9C55FROJnx0B1jKlhRvH9+IErDr6Znrkj6mu lSrw== 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 :dkim-signature; bh=pxDiz5NY8Vdg64fAb0yTAOnen95onGwJ6Ewy9t2/W5w=; b=VOj8HRy39DNjmY5nzTWMTF1YDDgRChmnKzLAu9+6Dk+USX5WBz/hJoQSRFKXK8G61G ZCXXiaQoiocauI1hcEx9w2OYBWy9BWQkx8u6hB4fq+NBns14CpeYlGAoUsi/tBpQGdWX /+TfpHNIEBYwcBKGGwJbTXkSl2AJXvS7KWmxMq+FIJwSsq8cpnLofQVNn8jZS9Z8orVS FmJGTzF01nNhtn090UN46o1GjXgM3jCOI0XVnVyyLzgxjSfMaYF3ry0cJxeHDK91wqHY og8fsB2HY7yUzuxc5/mv6Um8vOAD6JGo4P5PDxtUCogaQMU47Jgq1rg4/xqN0L+ULF2i YnNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b=Tcc5q3w1; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmx.net Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id by23si4929174ejb.165.2021.01.09.11.09.32; Sat, 09 Jan 2021 11:09:55 -0800 (PST) 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; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b=Tcc5q3w1; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmx.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726429AbhAITHn (ORCPT + 99 others); Sat, 9 Jan 2021 14:07:43 -0500 Received: from mout.gmx.net ([212.227.17.22]:47245 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725978AbhAITHm (ORCPT ); Sat, 9 Jan 2021 14:07:42 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1610219120; bh=z4JXsPGpBtiItD5DSjIUAVtxqz+CJxpHzrhtiUm6REI=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=Tcc5q3w17FVSfwbY/QgmptZU7qGAMMBc0265NBKl3by+ROcvv7qyUnkcUldhFEalj 7VJnrbIg0w1dAYSbsBn3ACOFFIJXtRa1tM/ut54E11Sho/v5FUwGyvyHbbHOnDNzWd VN9foPW/Q6G/NtySaf+3QTRtmiuuGFzmStfk5vPw= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from longitude ([37.201.215.57]) by mail.gmx.com (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MIdif-1klswk3WWD-00EeQX; Sat, 09 Jan 2021 20:05:20 +0100 From: =?UTF-8?q?Jonathan=20Neusch=C3=A4fer?= To: linux-kernel@vger.kernel.org Cc: Lee Jones , Rob Herring , =?UTF-8?q?Jonathan=20Neusch=C3=A4fer?= , Thierry Reding , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Alessandro Zummo , Alexandre Belloni , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , Sam Ravnborg , Linus Walleij , Heiko Stuebner , Stephan Gerhold , Lubomir Rintel , Mark Brown , allen , Mauro Carvalho Chehab , "David S. Miller" , devicetree@vger.kernel.org, linux-pwm@vger.kernel.org, linux-rtc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Heiko Stuebner , Josua Mayer , Andreas Kemnade , Arnd Bergmann , Daniel Palmer , Andy Shevchenko Subject: [PATCH v7 5/7] rtc: New driver for RTC in Netronix embedded controller Date: Sat, 9 Jan 2021 19:02:18 +0100 Message-Id: <20210109180220.121511-6-j.neuschaefer@gmx.net> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210109180220.121511-1-j.neuschaefer@gmx.net> References: <20210109180220.121511-1-j.neuschaefer@gmx.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:uZP9hf1aS3usw+/nYC0lrmHsNRp228AfYNgr3WydDpFImCEpBkk MnBVfgO17Z8uyGA8e7LxavLi414bUL0LTLFQa5QyWugX2XE4OgNNMjM5dZXVEZx1I+SIGN8 DApIa2fEx8PeaOZx/mDhmbDsdqN+uXovIanL+ibipU51qXPhii0oN2kUW3hTGtojvCbHD5S Cm5XVdFo0EAYVbAUNS2eA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:EYrpvAOSfd0=:7B24LpOnbiA8yFU1TbabCM nlW02BFpaymxews572XPp689VcVj35qTa/g79V0ItUPWe/Zhz0RkQdTgVniKadL5s1xqU3lWX zFCIK2bjlQYZoqLbg9utONjjNLwEC6biKTa26jwqDsFeNCOqSVTYUbtjqvlkziT7HCOYG2HgW pcVLqNdr6ec67LlRzD9V5dIOcLmYPxVls+HsVUr9MSz9MU2MC7or8WWbw5VbAWMMpGTDon1yj 5Mna3sxWGye7RaqvsKh8fiC996dW+4ULbq8h37Q/5XKnj4cNB3JiX6logEgknBvOe9UtBe7Oj U/C9XTeRoxon+5WvAo2vedhvr5+CqLMCaYuf2OJVD+NZ8uFkYIWlteZD+9m9HDtsay35H2dRD opLBXIRVylZ9iQOgpUFFVGoEgsKEfUV5/ZaQeC4Ig7MhyQO8kYzhWH4K/8tOoS1dR9wL979C7 yzaE06xbYaQCYSbwpCGIPi3XUI3XFUWGVBWnbjzva76jqp79ESxtNkVB8SLRSyuB+5mXqw8SJ U/UU4IIUgV/soEaw+aMiNQaZ+tVuB/vwUEuLbMbHAQcm6aC1F9JI1uW8+g8aVxbG8j0mXfteH Pw6Et5nwhIceVu29DwfVEhv+/aO4JsW7gU1/7RyCBjKjGLhfs9Hy3hr5GjOrHxt4pBbJTkfbN RmsHKQWMQmMICiVhnqTkmauWtttmym9qA3q9K1wM9uKXUYJrgmcNDkuKuUlGbYPxGj1jL4+WT MkyrPaizxcL7gs+upgVnAuQaQGgdC9rqOrO22OzrI6kxa+KRuKPW9TUeP1PyA/x9GIwwkhBSu nUP4lzMkGT8KkUxKisHvYS3NlDy4vEBPjOwJhWekzmeuQkYCwOTlJEJQVzuDZqq5rMEwOxriW SiCjw3SYTSrxRbeqA+xA== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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=C3=A4fer Acked-by: Alexandre Belloni =2D-- v7: - Adjust to recent RTC API change (rtc_register_device -> devm_rtc_registe= r_device) v6: - no changes v5: - https://lore.kernel.org/lkml/20201201011513.1627028-6-j.neuschaefer@gmx.= net/ - Add Alexandre Belloni's A-b - Use regmap_multi_reg_write v4: - https://lore.kernel.org/lkml/20201122222739.1455132-6-j.neuschaefer@gmx.= net/ - Remove "driver" from Kconfig entry for consistency with most other entri= es - 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=C3=B6nig'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 |=3D ... 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 =3D signs in struct initializers [Uwe Kleine-K=C3=B6nig] - 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 an= d ->range_max =2D-- drivers/rtc/Kconfig | 8 +++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-ntxec.c | 143 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 drivers/rtc/rtc-ntxec.c diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 6123f9f4fbc90..d49cf387add14 100644 =2D-- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -1315,6 +1315,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 bb8f319b09fbf..92c26eafe70ce 100644 =2D-- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -110,6 +110,7 @@ obj-$(CONFIG_RTC_DRV_MT7622) +=3D rtc-mt7622.o obj-$(CONFIG_RTC_DRV_MV) +=3D rtc-mv.o obj-$(CONFIG_RTC_DRV_MXC) +=3D rtc-mxc.o obj-$(CONFIG_RTC_DRV_MXC_V2) +=3D rtc-mxc_v2.o +obj-$(CONFIG_RTC_DRV_NTXEC) +=3D rtc-ntxec.o obj-$(CONFIG_RTC_DRV_OMAP) +=3D rtc-omap.o obj-$(CONFIG_RTC_DRV_OPAL) +=3D rtc-opal.o obj-$(CONFIG_RTC_DRV_PALMAS) +=3D rtc-palmas.o diff --git a/drivers/rtc/rtc-ntxec.c b/drivers/rtc/rtc-ntxec.c new file mode 100644 index 0000000000000..09cdbcab8eff9 =2D-- /dev/null +++ b/drivers/rtc/rtc-ntxec.c @@ -0,0 +1,143 @@ +// 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=C3=A4fer + */ + +#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 =3D dev_get_drvdata(dev); + unsigned int value; + int res; + +retry: + res =3D regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MINUTE_SECOND, &valu= e); + if (res < 0) + return res; + + tm->tm_min =3D value >> 8; + tm->tm_sec =3D value & 0xff; + + res =3D regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MDAY_HOUR, &value); + if (res < 0) + return res; + + tm->tm_mday =3D value >> 8; + tm->tm_hour =3D value & 0xff; + + res =3D regmap_read(rtc->ec->regmap, NTXEC_REG_READ_YEAR_MONTH, &value); + if (res < 0) + return res; + + tm->tm_year =3D (value >> 8) + 100; + tm->tm_mon =3D (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 =3D regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MINUTE_SECOND, &valu= e); + if (res < 0) + return res; + + if (tm->tm_min !=3D value >> 8 || tm->tm_sec !=3D (value & 0xff)) + goto retry; + + return 0; +} + +static int ntxec_set_time(struct device *dev, struct rtc_time *tm) +{ + struct ntxec_rtc *rtc =3D dev_get_drvdata(dev); + + /* + * 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. + */ + struct reg_sequence regs[] =3D { + { NTXEC_REG_WRITE_SECOND, ntxec_reg8(0) }, + { NTXEC_REG_WRITE_YEAR, ntxec_reg8(tm->tm_year - 100) }, + { NTXEC_REG_WRITE_MONTH, ntxec_reg8(tm->tm_mon + 1) }, + { NTXEC_REG_WRITE_DAY, ntxec_reg8(tm->tm_mday) }, + { NTXEC_REG_WRITE_HOUR, ntxec_reg8(tm->tm_hour) }, + { NTXEC_REG_WRITE_MINUTE, ntxec_reg8(tm->tm_min) }, + { NTXEC_REG_WRITE_SECOND, ntxec_reg8(tm->tm_sec) }, + }; + + return regmap_multi_reg_write(rtc->ec->regmap, regs, ARRAY_SIZE(regs)); +} + +static const struct rtc_class_ops ntxec_rtc_ops =3D { + .read_time =3D ntxec_read_time, + .set_time =3D ntxec_set_time, +}; + +static int ntxec_rtc_probe(struct platform_device *pdev) +{ + struct rtc_device *dev; + struct ntxec_rtc *rtc; + + rtc =3D devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); + if (!rtc) + return -ENOMEM; + + rtc->dev =3D &pdev->dev; + rtc->ec =3D dev_get_drvdata(pdev->dev.parent); + platform_set_drvdata(pdev, rtc); + + dev =3D devm_rtc_allocate_device(&pdev->dev); + if (IS_ERR(dev)) + return PTR_ERR(dev); + + dev->ops =3D &ntxec_rtc_ops; + dev->range_min =3D RTC_TIMESTAMP_BEGIN_2000; + dev->range_max =3D 9025257599LL; /* 2255-12-31 23:59:59 */ + + return devm_rtc_register_device(dev); +} + +static struct platform_driver ntxec_rtc_driver =3D { + .driver =3D { + .name =3D "ntxec-rtc", + }, + .probe =3D ntxec_rtc_probe, +}; +module_platform_driver(ntxec_rtc_driver); + +MODULE_AUTHOR("Jonathan Neusch=C3=A4fer "); +MODULE_DESCRIPTION("RTC driver for Netronix EC"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:ntxec-rtc"); =2D- 2.29.2