Skip to contentSkip to conversation

rtc: Add a driver for Cadence RTC

PATCH Superseded Jan Kotas 2 participants 5 messages Jan 8, 2019 – Jan 11, 2019

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jan Kotas

author
#0 lore raw

cover rtc: Add a driver for Cadence RTC

This patchset adds a driver support for Cadence RTC IP. It supports time, date and wakeups from sleep.

Jan Kotas (2):
dt-bindings: rtc: Add bindings for Cadence RTC
rtc: Add Cadence RTC driver

 Documentation/devicetree/bindings/rtc/cdns,rtc.txt |  25 ++
 drivers/rtc/Kconfig                                |  10 +
 drivers/rtc/Makefile                               |   1 +
 drivers/rtc/rtc-cadence.c                          | 404 +++++++++++++++++++++
 4 files changed, 440 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/cdns,rtc.txt
 create mode 100644 drivers/rtc/rtc-cadence.c
Signature · 1 line
2.15.0
author
#1 lore raw

PATCH 1/2 dt-bindings: rtc: Add bindings for Cadence RTC

This patch adds a DT binding documentation for
Cadence RTC controller.

---
 Documentation/devicetree/bindings/rtc/cdns,rtc.txt | 25 ++++++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/cdns,rtc.txt
Documentation/devicetree/bindings/rtc/cdns,rtc.txt new file +25 -0
new file mode 100644
index 000000000..40713bbbe
@@ -0,0 +1,25 @@
+Cadence Real Time Clock
+
+The Cadence RTC controller with date, time and alarm capabilities.
+The alarm may wake the system from low-power state.
+
+Required properties:
+- compatible: Should be "cdns,rtc-r109v3"
+- reg: Specifies base physical address and size of the register area.
+- interrupts: A single interrupt specifier.
+- clocks: Must contain two entries:
+	- pclk: APB registers clock
+	- ref_clk: reference 1Hz of 100Hz clock, depending on IP configuration
+	See ../clocks/clock-bindings.txt for details.
+
+Example:
+        rtc0: rtc@fd080000 {
+        	compatible = "cdns,rtc-r109v3";
+        	reg = <0xfd080000 0x1000>;
+
+        	clock-names = "pclk", "ref_clk";
+        	clocks = <&sysclock>, <&refclock>;
+
+        	interrupt-parent = <&gic>;
+        	interrupts = <0 6 IRQ_TYPE_LEVEL_HIGH>;
+        };
Signature · 1 line
2.15.0
author
#2 lore raw

PATCH 2/2 rtc: Add Cadence RTC driver

In reply to Jan Kotas

This patch adds a driver for Cadence RTC controller.

It can be enabled with RTC_DRV_CADENCE Kconfig option. It supports waking system from sleep modes.

---
 drivers/rtc/Kconfig       |  10 ++
 drivers/rtc/Makefile      |   1 +
 drivers/rtc/rtc-cadence.c | 404 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 415 insertions(+)
 create mode 100644 drivers/rtc/rtc-cadence.c
drivers/rtc/Kconfig +10 -0
index 225b0b851..0aba341ad 100644
@@ -1508,6 +1508,16 @@ config RTC_DRV_ARMADA38X
 	  This driver can also be built as a module. If so, the module
 	  will be called armada38x-rtc.
 
+config RTC_DRV_CADENCE
+	tristate "Cadence RTC driver"
+	depends on OF && HAS_IOMEM
+	help
+	  If you say Y here you will get access to Cadence RTC IP
+	  found on certain SOCs.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called rtc-cadence.
+
 config RTC_DRV_FTRTC010
 	tristate "Faraday Technology FTRTC010 RTC"
 	depends on HAS_IOMEM
drivers/rtc/Makefile +1 -0
index df022d820..5adb41b45 100644
@@ -39,6 +39,7 @@ obj-$(CONFIG_RTC_DRV_AU1XXX)	+= rtc-au1xxx.o
 obj-$(CONFIG_RTC_DRV_BQ32K)	+= rtc-bq32k.o
 obj-$(CONFIG_RTC_DRV_BQ4802)	+= rtc-bq4802.o
 obj-$(CONFIG_RTC_DRV_BRCMSTB)	+= rtc-brcmstb-waketimer.o
+obj-$(CONFIG_RTC_DRV_CADENCE)	+= rtc-cadence.o
 obj-$(CONFIG_RTC_DRV_CMOS)	+= rtc-cmos.o
 obj-$(CONFIG_RTC_DRV_COH901331)	+= rtc-coh901331.o
 obj-$(CONFIG_RTC_DRV_CPCAP)	+= rtc-cpcap.o
drivers/rtc/rtc-cadence.c new file +404 -0

404 lines not on this page · raw message

Signature · 1 line
2.15.0
#3 lore raw

Re: [PATCH 2/2] rtc: Add Cadence RTC driver

Hello,

Quoted 3 lines · Jan Kotas
 drivers/rtc/Kconfig       |  10 ++
 drivers/rtc/Makefile      |   1 +
 drivers/rtc/rtc-cadence.c | 404 ++++++++++++++++++++++++++++++++++++++++++++++

I would prefer a name that is a bit less generic, unless you can guarantee this driver will be able to handle every RTCs from Cadence.

drivers/rtc/rtc-cadence.c line 182

Quoted 7 lines
drivers/rtc/rtc-cadence.c new file +7 -0
+static int cdns_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
+{
+	struct cdns_rtc *crtc = dev_get_drvdata(dev);
+
+	if (enabled) {
+		writel((CDNS_RTC_AEI_SEC | CDNS_RTC_AEI_MIN | CDNS_RTC_AEI_HOUR
+			| CDNS_RTC_AEI_DATE | CDNS_RTC_AEI_SEC),

CDNS_RTC_AEI_SEC is used twice here.

Quoted 71 lines
drivers/rtc/rtc-cadence.c new file +69 -0
+		       crtc->regs + CDNS_RTC_AENR);
+		writel(CDNS_RTC_AEI_ALRM, crtc->regs + CDNS_RTC_IENR);
+	} else {
+		writel(0, crtc->regs + CDNS_RTC_AENR);
+		writel(CDNS_RTC_AEI_ALRM, crtc->regs + CDNS_RTC_IDISR);
+	}
+
+	return 0;
+}
+
 
 
+static int cdns_rtc_probe(struct platform_device *pdev)
+{
+	struct cdns_rtc *crtc;
+	struct resource *res;
+	int ret;
+	unsigned long ref_clk_freq;
+
+	crtc = devm_kzalloc(&pdev->dev, sizeof(*crtc), GFP_KERNEL);
+	if (!crtc)
+		return -ENOMEM;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	crtc->regs = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(crtc->regs))
+		return PTR_ERR(crtc->regs);
+
+	crtc->irq = platform_get_irq(pdev, 0);
+	if (crtc->irq < 0)
+		return -EINVAL;
+
+	crtc->pclk = devm_clk_get(&pdev->dev, "pclk");
+	if (IS_ERR(crtc->pclk)) {
+		ret = PTR_ERR(crtc->pclk);
+		dev_err(&pdev->dev,
+			"Failed to retrieve the peripheral clock, %d\n", ret);
+		return ret;
+	}
+
+	crtc->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
+	if (IS_ERR(crtc->ref_clk)) {
+		ret = PTR_ERR(crtc->ref_clk);
+		dev_err(&pdev->dev,
+			"Failed to retrieve the reference clock, %d\n", ret);
+		return ret;
+	}
+
+	ret = clk_prepare_enable(crtc->pclk);
+	if (ret) {
+		dev_err(&pdev->dev,
+			"Failed to enable the peripheral clock, %d\n", ret);
+		return ret;
+	}
+
+	ret = clk_prepare_enable(crtc->ref_clk);
+	if (ret) {
+		dev_err(&pdev->dev,
+			"Failed to enable the reference clock, %d\n", ret);
+		goto err_disable_pclk;
+	}
+
+	ref_clk_freq = clk_get_rate(crtc->ref_clk);
+	if ((ref_clk_freq != 1) && (ref_clk_freq != 100)) {
+		dev_err(&pdev->dev,
+			"Invalid reference clock frequency %lu Hz.\n",
+			ref_clk_freq);
+		ret = -EINVAL;
+		goto err_disable_ref_clk;
+	}
+

drivers/rtc/rtc-cadence.c line 314

Quoted 10 lines
drivers/rtc/rtc-cadence.c new file +10 -0
+	ret = devm_request_irq(&pdev->dev, crtc->irq,
+			       cdns_rtc_irq_handler, 0,
+			       dev_name(&pdev->dev), &pdev->dev);
+	if (ret) {
+		dev_err(&pdev->dev,
+			"Unable to request interrupt for the device, %d\n",
+			ret);
+		goto err_disable_ref_clk;
+	}
+

You should use devm_rtc_allocate_device to allocate crtc->rtc_dev before requesting the IRQ. Else, this leaves a race condition open.

Also, please set crtc->rtc_dev->range_min and crtc->rtc_dev->range_max according to the fully contiguous range of time that is supported by the RTC. You can use
https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/rtc-tools.git/tree/rtc-range.c
to assist you.

drivers/rtc/rtc-cadence.c line 320

Quoted 6 lines
drivers/rtc/rtc-cadence.c new file +6 -0
+	/* Always use 24-hour mode */
+	writel(0, crtc->regs + CDNS_RTC_HMR);
+
+	device_init_wakeup(&pdev->dev, 1);
+	platform_set_drvdata(pdev, crtc);
+	cdns_rtc_set_enabled(crtc, true);

Is that really necessary? I guess you could check whether it has already been enabled to know whether the currently set time is valid so cdns_rtc_read_time could return -EINVAL when it knows it isn't valid. cdns_rtc_set_time will enable the RTC once the time has been set anyway.

Quoted 55 lines
drivers/rtc/rtc-cadence.c new file +55 -0
+
+	crtc->rtc_dev = devm_rtc_device_register(&pdev->dev,
+						 dev_name(&pdev->dev),
+						 &cdns_rtc_ops,
+						 THIS_MODULE);
+	if (IS_ERR(crtc->rtc_dev)) {
+		ret = PTR_ERR(crtc->rtc_dev);
+		dev_err(&pdev->dev, "Unable to register the rtc device, %d\n",
+			ret);
+		goto err_stop_rtc;
+	}
+
+	return 0;
+
+err_stop_rtc:
+	cdns_rtc_set_enabled(crtc, false);
+
+err_disable_ref_clk:
+	clk_disable_unprepare(crtc->ref_clk);
+
+err_disable_pclk:
+	clk_disable_unprepare(crtc->pclk);
+
+	return ret;
+}
+
+static int cdns_rtc_remove(struct platform_device *pdev)
+{
+	struct cdns_rtc *crtc = platform_get_drvdata(pdev);
+
+	cdns_rtc_alarm_irq_enable(&pdev->dev, 0);
+	device_init_wakeup(&pdev->dev, 0);
+
+	clk_disable_unprepare(crtc->pclk);
+	clk_disable_unprepare(crtc->ref_clk);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int cdns_rtc_suspend(struct device *dev)
+{
+	struct cdns_rtc *crtc = dev_get_drvdata(dev);
+
+	if (device_may_wakeup(dev))
+		enable_irq_wake(crtc->irq);
+
+	return 0;
+}
+
+static int cdns_rtc_resume(struct device *dev)
+{
+	struct cdns_rtc *crtc = dev_get_drvdata(dev);
+
+	if (device_may_wakeup(dev))

drivers/rtc/rtc-cadence.c line 385

Quoted 10 lines
drivers/rtc/rtc-cadence.c new file +10 -0
+		disable_irq_wake(crtc->irq);
+
+	return 0;
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(cdns_rtc_pm_ops, cdns_rtc_suspend, cdns_rtc_resume);
+
+static const struct of_device_id cdns_rtc_of_match[] = {
+	{ .compatible = "cdns,rtc-r109v3" },

Is r109v3 an IP name or a revision?

Quoted 22 lines
drivers/rtc/rtc-cadence.c new file +19 -0
+	{ },
+};
+MODULE_DEVICE_TABLE(of, cdns_rtc_of_match);
+
+static struct platform_driver cdns_rtc_driver = {
+	.driver = {
+		.name = "cdns-rtc",
+		.of_match_table = cdns_rtc_of_match,
+		.pm = &cdns_rtc_pm_ops,
+	},
+	.probe = cdns_rtc_probe,
+	.remove = cdns_rtc_remove,
+};
+module_platform_driver(cdns_rtc_driver);
+
+MODULE_AUTHOR("Jan Kotas <[email protected]>");
+MODULE_DESCRIPTION("Cadence RTC driver");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:cdns-rtc");
Signature · 1 line
2.15.0
Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw

Re: [PATCH 2/2] rtc: Add Cadence RTC driver

Hi Alexandre,

Thank you for your reply.

Quoted 11 lines · Alexandre Belloni

Hello,

Quoted 3 lines · Jan Kotas
drivers/rtc/Kconfig       |  10 ++
drivers/rtc/Makefile      |   1 +
drivers/rtc/rtc-cadence.c | 404 ++++++++++++++++++++++++++++++++++++++++++++++

I would prefer a name that is a bit less generic, unless you can guarantee this driver will be able to handle every RTCs from Cadence.

Yes, that’s the only IP that is being sold.
It was designed a long time ago, it’s not being actively developed in terms of new features.

Quoted 9 lines

drivers/rtc/rtc-cadence.c line 182

Quoted 7 lines
drivers/rtc/rtc-cadence.c new file +7 -0
+static int cdns_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
+{
+	struct cdns_rtc *crtc = dev_get_drvdata(dev);
+
+	if (enabled) {
+		writel((CDNS_RTC_AEI_SEC | CDNS_RTC_AEI_MIN | CDNS_RTC_AEI_HOUR
+			| CDNS_RTC_AEI_DATE | CDNS_RTC_AEI_SEC),

CDNS_RTC_AEI_SEC is used twice here.

Thanks for spotting that, I’ll fix it.

Quoted 14 lines

drivers/rtc/rtc-cadence.c line 314

Quoted 11 lines
drivers/rtc/rtc-cadence.c new file +10 -0
+	ret = devm_request_irq(&pdev->dev, crtc->irq,
+			       cdns_rtc_irq_handler, 0,
+			       dev_name(&pdev->dev), &pdev->dev);
+	if (ret) {
+		dev_err(&pdev->dev,
+			"Unable to request interrupt for the device, %d\n",
+			ret);
+		goto err_disable_ref_clk;
+	}
+

You should use devm_rtc_allocate_device to allocate crtc->rtc_dev before requesting the IRQ. Else, this leaves a race condition open.

Ok, I’ll fix it.

Quoted 3 lines

Also, please set crtc->rtc_dev->range_min and crtc->rtc_dev->range_max according to the fully contiguous range of time that is supported by the RTC.

I’ll add it, thanks for pointing it out.

Quoted 11 lines

drivers/rtc/rtc-cadence.c line 320

Quoted 6 lines
drivers/rtc/rtc-cadence.c new file +6 -0
+	/* Always use 24-hour mode */
+	writel(0, crtc->regs + CDNS_RTC_HMR);
+
+	device_init_wakeup(&pdev->dev, 1);
+	platform_set_drvdata(pdev, crtc);
+	cdns_rtc_set_enabled(crtc, true);

Is that really necessary? I guess you could check whether it has already been enabled to know whether the currently set time is valid so cdns_rtc_read_time could return -EINVAL when it knows it isn't valid. cdns_rtc_set_time will enable the RTC once the time has been set anyway.

That’s a good idea, I’ll add it.

Quoted 4 lines

drivers/rtc/rtc-cadence.c line 385

Quoted 2 lines
drivers/rtc/rtc-cadence.c new file +2 -0
+static const struct of_device_id cdns_rtc_of_match[] = {
+	{ .compatible = "cdns,rtc-r109v3" },

Is r109v3 an IP name or a revision?

It’s a revision.

Quoted 3 lines
Signature · 2 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering

Regards,
Jan