linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Jonathan Neuschäfer" <j.neuschaefer@gmx.net>
To: linux-kernel@vger.kernel.org
Cc: "Lee Jones" <lee.jones@linaro.org>,
	"Rob Herring" <robh+dt@kernel.org>,
	"Jonathan Neuschäfer" <j.neuschaefer@gmx.net>,
	"Thierry Reding" <thierry.reding@gmail.com>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"Alessandro Zummo" <a.zummo@towertech.it>,
	"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	"Shawn Guo" <shawnguo@kernel.org>,
	"Sascha Hauer" <s.hauer@pengutronix.de>,
	"Pengutronix Kernel Team" <kernel@pengutronix.de>,
	"Fabio Estevam" <festevam@gmail.com>,
	"NXP Linux Team" <linux-imx@nxp.com>,
	"Sam Ravnborg" <sam@ravnborg.org>,
	"Linus Walleij" <linus.walleij@linaro.org>,
	"Heiko Stuebner" <heiko.stuebner@theobroma-systems.com>,
	"Stephan Gerhold" <stephan@gerhold.net>,
	"Lubomir Rintel" <lkundrak@v3.sk>,
	"Mark Brown" <broonie@kernel.org>, allen <allen.chen@ite.com.tw>,
	"Mauro Carvalho Chehab" <mchehab+huawei@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	devicetree@vger.kernel.org, linux-pwm@vger.kernel.org,
	linux-rtc@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	"Heiko Stuebner" <heiko@sntech.de>,
	"Josua Mayer" <josua.mayer@jm0.eu>,
	"Andreas Kemnade" <andreas@kemnade.info>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Daniel Palmer" <daniel@0x0f.com>
Subject: [PATCH v2 08/10] rtc: New driver for RTC in Netronix embedded controller
Date: Sat,  5 Sep 2020 16:45:00 +0200	[thread overview]
Message-ID: <20200905144503.1067124-1-j.neuschaefer@gmx.net> (raw)
In-Reply-To: <20200905133230.1014581-1-j.neuschaefer@gmx.net>

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 <j.neuschaefer@gmx.net>
---

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

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 <linux/mfd/ntxec.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/rtc.h>
+#include <linux/types.h>
+
+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 <j.neuschaefer@gmx.net>");
+MODULE_DESCRIPTION("RTC driver for Netronix EC");
+MODULE_LICENSE("GPL");
--
2.28.0


  parent reply	other threads:[~2020-09-05 14:47 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-05 13:32 [PATCH v2 00/10] Netronix embedded controller driver for Kobo and Tolino ebook readers Jonathan Neuschäfer
2020-09-05 13:32 ` [PATCH v2 01/10] dt-bindings: Add vendor prefix for Netronix, Inc Jonathan Neuschäfer
2020-09-15  0:43   ` Rob Herring
2020-09-05 13:32 ` [PATCH v2 02/10] dt-bindings: mfd: Add binding for Netronix's embedded controller Jonathan Neuschäfer
2020-09-15  0:50   ` Rob Herring
2020-09-17 11:12     ` Jonathan Neuschäfer
2020-09-05 13:32 ` [PATCH v2 03/10] mfd: Add base driver for Netronix " Jonathan Neuschäfer
2020-09-08 13:29   ` Lee Jones
2020-09-10 12:04     ` Jonathan Neuschäfer
2020-09-05 13:32 ` [PATCH v2 04/10] dt-bindings: pwm: Add bindings for PWM function in Netronix EC Jonathan Neuschäfer
2020-09-15  0:54   ` Rob Herring
2020-09-15  6:23     ` Andreas Kemnade
2020-09-15 14:31       ` Rob Herring
2020-09-17 11:58         ` Jonathan Neuschäfer
2020-09-05 13:32 ` [PATCH v2 05/10] pwm: ntxec: Add driver " Jonathan Neuschäfer
     [not found]   ` <CAHp75VdUHoOyM3bObzhdfiqpne0AmSK_UakteTZxnjqJVrNV9A@mail.gmail.com>
2020-09-08  8:14     ` Lee Jones
2020-09-08  8:47       ` Andy Shevchenko
2020-09-08  9:35         ` Lee Jones
2020-09-10 19:13           ` Jonathan Neuschäfer
2020-09-10 19:09     ` Jonathan Neuschäfer
2020-09-05 13:32 ` [PATCH v2 06/10] dt-bindings: rtc: Add bindings for Netronix embedded controller RTC Jonathan Neuschäfer
2020-09-05 13:32 ` [PATCH v2 07/10] rtc: Introduce RTC_TIMESTAMP_END_2255 Jonathan Neuschäfer
2020-09-08  9:39   ` Alexandre Belloni
2020-09-10 19:21     ` Jonathan Neuschäfer
2020-09-05 14:45 ` Jonathan Neuschäfer [this message]
     [not found]   ` <CAHp75Vca+Tp0v_Q4RBuUNSo_5aq_u6mBGeXecHULC1Avgm5=Xg@mail.gmail.com>
2020-09-10 19:42     ` [PATCH v2 08/10] rtc: New driver for RTC in Netronix embedded controller Jonathan Neuschäfer
2020-09-05 14:45 ` [PATCH v2 09/10] MAINTAINERS: Add entry for " Jonathan Neuschäfer
2020-09-05 14:45 ` [PATCH v2 10/10] ARM: dts: imx50-kobo-aura: Add " Jonathan Neuschäfer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200905144503.1067124-1-j.neuschaefer@gmx.net \
    --to=j.neuschaefer@gmx.net \
    --cc=a.zummo@towertech.it \
    --cc=alexandre.belloni@bootlin.com \
    --cc=allen.chen@ite.com.tw \
    --cc=andreas@kemnade.info \
    --cc=arnd@arndb.de \
    --cc=broonie@kernel.org \
    --cc=daniel@0x0f.com \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=festevam@gmail.com \
    --cc=heiko.stuebner@theobroma-systems.com \
    --cc=heiko@sntech.de \
    --cc=josua.mayer@jm0.eu \
    --cc=kernel@pengutronix.de \
    --cc=lee.jones@linaro.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-imx@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=lkundrak@v3.sk \
    --cc=mchehab+huawei@kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=s.hauer@pengutronix.de \
    --cc=sam@ravnborg.org \
    --cc=shawnguo@kernel.org \
    --cc=stephan@gerhold.net \
    --cc=thierry.reding@gmail.com \
    --cc=u.kleine-koenig@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).