Skip to contentSkip to conversation

rtc: Introduce rtc-ti-k3

PATCH Superseded Nishanth Menon 3 participants 6 messages May 13, 2022 – May 17, 2022

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover rtc: Introduce rtc-ti-k3

Hi Folks,

Hopefully third time is a charm ;).

This series adds support for TI K3 RTC as instantiated on TI's AM625 SoC.

Documentation in the current early release version of Technical Reference Manual is incomplete at the moment, but due to be updated later this year.
https://www.ti.com/lit/pdf/spruiv7

Testing log can be found here (next-20220509 + additional node for dts):
https://gist.github.com/nmenon/2139c57212605bb38a4624ad0523416f

Changes since V2:
* bindings updated for review comments
* Driver updated with fixes for issues caught during system level tests in 5.10 backport testing.
* Picked up Andrew's Acked-by

V2: https://lore.kernel.org/all/[email protected]/
V1: https://lore.kernel.org/all/[email protected]/

Nishanth Menon (2):
dt-bindings: rtc: Add TI K3 RTC description
rtc: Introduce ti-k3-rtc

 .../devicetree/bindings/rtc/ti,k3-rtc.yaml    |  62 ++
 drivers/rtc/Kconfig                           |  11 +
 drivers/rtc/Makefile                          |   1 +
 drivers/rtc/rtc-ti-k3.c                       | 695 ++++++++++++++++++
 4 files changed, 769 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/ti,k3-rtc.yaml
 create mode 100644 drivers/rtc/rtc-ti-k3.c
Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/2 dt-bindings: rtc: Add TI K3 RTC description

This adds the documentation for the devicetree bindings of the Texas Instruments RTC modules on K3 family of SoCs such as AM62x SoCs or newer.

---

Changes since V2:
* Krzysztof's comments addressed (rtc.yaml, dropped wakeup-source, switched over to unevaluatedProperties)

V2: https://lore.kernel.org/all/[email protected]/
V1: https://lore.kernel.org/all/[email protected]/

 .../devicetree/bindings/rtc/ti,k3-rtc.yaml    | 62 +++++++++++++++++++
 1 file changed, 62 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/ti,k3-rtc.yaml
Documentation/devicetree/bindings/rtc/ti,k3-rtc.yaml new file +62 -0
new file mode 100644
index 000000000000..d995ef04a6eb
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rtc/ti,k3-rtc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Texas Instruments K3 Real Time Clock
+
+maintainers:
+  - Nishanth Menon <[email protected]>
+
+description: |
+  This RTC appears in the AM62x family of SoCs.
+
+allOf:
+  - $ref: "rtc.yaml#"
+
+properties:
+  compatible:
+    enum:
+      - ti,am62-rtc
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: VBUS Interface clock
+      - description: 32k Clock source (external or internal).
+
+  clock-names:
+    items:
+      - const: vbus
+      - const: osc32k
+
+  power-domains:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - clock-names
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    rtc@2b1f0000 {
+        compatible = "ti,am62-rtc";
+        reg = <0x2b1f0000 0x100>;
+        interrupts = <GIC_SPI 100 IRQ_TYPE_LEVEL_HIGH>;
+        power-domains = <&bar 0>;
+        clocks = <&foo 0>, <&foo 1>;
+        clock-names = "vbus", "osc32k";
+        wakeup-source;
+    };
Signature · 1 line
2.31.1
#3 lore raw

PATCH v3 2/2 rtc: Introduce ti-k3-rtc

In reply to Nishanth Menon

Introduce support for Texas Instruments Real Time Clock controller on newer K3 family of SoCs such as AM62x.

The hardware module that is being supported is the "digital only" version which does'nt have capability of external wakeup sources and external power backup. However, for many practical applications, this should suffice as RTC is operational across low power sequences.

The hardware block by itself is split into two distinct domains internally to further reduce the power consumption with the actual counter block and comparators clocked off a 32k clock source (which based on SoC integration can be sourced by an external crystal) and an register interface block which is driven by the bus clock. While optimal from power perspective, it does create some complicated synchronizations and sequences that one must be wary of in the driver handling.

---

Changes since v2:
* Picked up Andrew's ack (as it was around reg_fields migration)
* sync_timeout_us bumped up - wider 5.10 backported driver testing and followups indicated that h/w IP spec had factored only the domain to domain time, which was insufficient to account for additional SoC level integration deltas that randomly triggers. So the timeout is safer to be set at 4x32k clk periods to account for additional bus segments in the reg paths.
* REG_K3RTC_IRQSTATUS_SYS had to be switched over to a regmap_write - a field operation in the sequence is wrong as it creates a issue in the statemachine with multiple pends
* Much more rigorous testing across multiple boards and suspend-resume scenarios on backported driver (suspend-resume yet to come together in upstream).

V2: https://lore.kernel.org/all/[email protected]/
V1: https://lore.kernel.org/all/[email protected]/

 drivers/rtc/Kconfig     |  11 +
 drivers/rtc/Makefile    |   1 +
 drivers/rtc/rtc-ti-k3.c | 695 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 707 insertions(+)
 create mode 100644 drivers/rtc/rtc-ti-k3.c
drivers/rtc/Kconfig +11 -0
index 41c65b4d2baf..fc57977b417c 100644
@@ -1922,6 +1922,17 @@ config RTC_DRV_ASPEED
 	  This driver can also be built as a module, if so, the module
 	  will be called "rtc-aspeed".
 
+config RTC_DRV_TI_K3
+	tristate "TI K3 RTC"
+	depends on ARCH_K3 || COMPILE_TEST
+	select REGMAP_MMIO
+	help
+	  If you say yes here you get support for the Texas Instruments's
+	  Real Time Clock for K3 architecture.
+
+	  This driver can also be built as a module, if so, the module
+	  will be called "rtc-ti-k3".
+
 comment "HID Sensor RTC drivers"
 
 config RTC_DRV_HID_SENSOR_TIME
drivers/rtc/Makefile +1 -0
index 2d827d8261d5..d3d5964fe352 100644
@@ -171,6 +171,7 @@ obj-$(CONFIG_RTC_DRV_SUNPLUS)	+= rtc-sunplus.o
 obj-$(CONFIG_RTC_DRV_SUNXI)	+= rtc-sunxi.o
 obj-$(CONFIG_RTC_DRV_TEGRA)	+= rtc-tegra.o
 obj-$(CONFIG_RTC_DRV_TEST)	+= rtc-test.o
+obj-$(CONFIG_RTC_DRV_TI_K3)	+= rtc-ti-k3.o
 obj-$(CONFIG_RTC_DRV_TPS6586X)	+= rtc-tps6586x.o
 obj-$(CONFIG_RTC_DRV_TPS65910)	+= rtc-tps65910.o
 obj-$(CONFIG_RTC_DRV_TWL4030)	+= rtc-twl.o
drivers/rtc/rtc-ti-k3.c new file +695 -0

695 lines not on this page · raw message

Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH V3 2/2] rtc: Introduce ti-k3-rtc

Hello Nishanth,

I have some very minor comments:

Quoted 6 lines · Nishanth Menon
drivers/rtc/rtc-ti-k3.c new file +0 -0
new file mode 100644
index 000000000000..21a64051fd42

drivers/rtc/rtc-ti-k3.c line 8

Quoted 8 lines
drivers/rtc/rtc-ti-k3.c new file +8 -0
@@ -0,0 +1,695 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Texas Instruments K3 RTC driver
+ *
+ * Copyright (C) 2021-2022 Texas Instruments Incorporated - https://www.ti.com/
+ */
+
+#define dev_fmt(fmt) "%s: " fmt, __func__

Are you sure you want to keep this line?

Quoted 5 lines
drivers/rtc/rtc-ti-k3.c new file +5 -0
+static int ti_k3_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
+{
+	struct ti_k3_rtc *priv = dev_get_drvdata(dev);
+	time64_t seconds;
+	int ret;

drivers/rtc/rtc-ti-k3.c line 345

Quoted 10 lines
drivers/rtc/rtc-ti-k3.c new file +10 -0
+
+	seconds = rtc_tm_to_time64(&alarm->time);
+
+	k3rtc_field_write(priv, K3RTC_ALM_S_CNT_LSW, seconds);
+	k3rtc_field_write(priv, K3RTC_ALM_S_CNT_MSW, (seconds >> 32));
+
+	/* Make sure the alarm time is synced in */
+	ret = k3rtc_fence(priv);
+	if (ret) {
+		dev_err(dev, "Failed to fence(%d)!\n", ret);

I'm not sure this message is useful because the only thing the user may do would be trying to set the time again.

Quoted 6 lines
drivers/rtc/rtc-ti-k3.c new file +6 -0
+		return ret;
+	}
+
+	/* Alarm irq enable will do a sync */
+	return ti_k3_rtc_alarm_irq_enable(dev, alarm->enabled);
+}

drivers/rtc/rtc-ti-k3.c line 562

Quoted 10 lines
drivers/rtc/rtc-ti-k3.c new file +8 -0
+
 
 
+
+static int k3rtc_get_vbusclk(struct device *dev, struct ti_k3_rtc *priv)
+{
+	int ret;
+	struct clk *clk;
+
+	/* Note: VBUS is'nt a context clock, it is needed for hardware operation */

typo ---------------^

drivers/rtc/rtc-ti-k3.c line 571

Quoted 9 lines
drivers/rtc/rtc-ti-k3.c new file +9 -0
+	clk = devm_clk_get(dev, "vbus");
+	if (IS_ERR(clk)) {
+		dev_err(dev, "No input vbus clock\n");
+		return PTR_ERR(clk);
+	}
+
+	ret = clk_prepare_enable(clk);
+	if (ret) {
+		dev_err(dev, "Failed to enable the vbus clock(%d)\n", ret);

I would also remove those two dev_err

Quoted 69 lines
drivers/rtc/rtc-ti-k3.c new file +69 -0
+		return ret;
+	}
+
+	ret = devm_add_action_or_reset(dev, (void (*)(void *))clk_disable_unprepare, clk);
+	return ret;
+}
+
+static int ti_k3_rtc_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct ti_k3_rtc *priv;
+	void __iomem *rtc_base;
+	int ret;
+
+	priv = devm_kzalloc(dev, sizeof(struct ti_k3_rtc), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	rtc_base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(rtc_base))
+		return PTR_ERR(rtc_base);
+
+	priv->regmap = devm_regmap_init_mmio(dev, rtc_base, &ti_k3_rtc_regmap_config);
+	if (IS_ERR(priv->regmap))
+		return PTR_ERR(priv->regmap);
+
+	ret = devm_regmap_field_bulk_alloc(dev, priv->regmap, priv->r_fields,
+					   ti_rtc_reg_fields, K3_RTC_MAX_FIELDS);
+	if (ret)
+		return ret;
+
+	ret = k3rtc_get_32kclk(dev, priv);
+	if (ret)
+		return ret;
+	ret = k3rtc_get_vbusclk(dev, priv);
+	if (ret)
+		return ret;
+
+	ret = platform_get_irq(pdev, 0);
+	if (ret < 0)
+		return ret;
+	priv->irq = (unsigned int)ret;
+
+	priv->rtc_dev = devm_rtc_allocate_device(dev);
+	if (IS_ERR(priv->rtc_dev))
+		return PTR_ERR(priv->rtc_dev);
+
+	priv->soc = of_device_get_match_data(dev);
+
+	priv->rtc_dev->ops = &ti_k3_rtc_ops;
+	priv->rtc_dev->range_max = (1ULL << 48) - 1;	/* 48Bit seconds */
+	ti_k3_rtc_nvmem_config.priv = priv;
+
+	ret = devm_request_threaded_irq(dev, priv->irq, NULL,
+					ti_k3_rtc_interrupt,
+					IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
+					dev_name(dev), dev);
+	if (ret) {
+		dev_err(dev, "Could not request IRQ: %d\n", ret);
+		return ret;
+	}
+
+	platform_set_drvdata(pdev, priv);
+
+	ret = k3rtc_configure(dev);
+	if (ret)
+		return ret;
+
+	if (device_property_present(dev, "wakeup-source"))

drivers/rtc/rtc-ti-k3.c line 650

Quoted 10 lines
drivers/rtc/rtc-ti-k3.c new file +10 -0
+		device_init_wakeup(dev, true);
+	else
+		device_set_wakeup_capable(dev, true);
+
+	ret = devm_rtc_register_device(priv->rtc_dev);
+	if (ret)
+		return ret;
+
+	ret = devm_rtc_nvmem_register(priv->rtc_dev, &ti_k3_rtc_nvmem_config);
+	return ret;

You don't need ret here and if I take that, I'll soon get an automatically generated patch.

Quoted 48 lines
drivers/rtc/rtc-ti-k3.c new file +45 -0
+}
+
+static const struct ti_k3_rtc_soc_data ti_k3_am62_data = {
+	.unlock_irq_erratum = true,
+};
+
+static const struct of_device_id ti_k3_rtc_of_match_table[] = {
+	{.compatible = "ti,am62-rtc", .data = &ti_k3_am62_data},
+	{}
+};
+MODULE_DEVICE_TABLE(of, ti_k3_rtc_of_match_table);
+
+static int __maybe_unused ti_k3_rtc_suspend(struct device *dev)
+{
+	struct ti_k3_rtc *priv = dev_get_drvdata(dev);
+
+	if (device_may_wakeup(dev))
+		enable_irq_wake(priv->irq);
+	return 0;
+}
+
+static int __maybe_unused ti_k3_rtc_resume(struct device *dev)
+{
+	struct ti_k3_rtc *priv = dev_get_drvdata(dev);
+
+	if (device_may_wakeup(dev))
+		disable_irq_wake(priv->irq);
+	return 0;
+}
+
+static SIMPLE_DEV_PM_OPS(ti_k3_rtc_pm_ops, ti_k3_rtc_suspend, ti_k3_rtc_resume);
+
+static struct platform_driver ti_k3_rtc_driver = {
+	.probe = ti_k3_rtc_probe,
+	.driver = {
+		   .name = "rtc-ti-k3",
+		   .of_match_table = ti_k3_rtc_of_match_table,
+		   .pm = &ti_k3_rtc_pm_ops,
+	},
+};
+module_platform_driver(ti_k3_rtc_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("TI K3 RTC driver");
+MODULE_AUTHOR("Nishanth Menon");
Signature · 1 line
2.31.1
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#5 lore raw

Re: [PATCH V3 2/2] rtc: Introduce ti-k3-rtc

Quoted 3 lines · Alexandre Belloni

Hello Nishanth,

I have some very minor comments:

Thanks for the review.

Quoted 10 lines · Nishanth Menon
Quoted 8 lines
drivers/rtc/rtc-ti-k3.c new file +2 -0
new file mode 100644
index 000000000000..21a64051fd42
@@ -0,0 +1,695 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
Quoted 8 lines

drivers/rtc/rtc-ti-k3.c line 8

Quoted 6 lines
drivers/rtc/rtc-ti-k3.c new file +6 -0
+ * Texas Instruments K3 RTC driver
+ *
+ * Copyright (C) 2021-2022 Texas Instruments Incorporated - https://www.ti.com/
+ */
+
+#define dev_fmt(fmt) "%s: " fmt, __func__

Are you sure you want to keep this line?

Saves me the headache of trying to find which function reported it from the logs, but I can drop it.

Quoted 11 lines
Quoted 11 lines
drivers/rtc/rtc-ti-k3.c new file +10 -0
+static int ti_k3_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
+{
+	struct ti_k3_rtc *priv = dev_get_drvdata(dev);
+	time64_t seconds;
+	int ret;
+
+	seconds = rtc_tm_to_time64(&alarm->time);
+
+	k3rtc_field_write(priv, K3RTC_ALM_S_CNT_LSW, seconds);
+	k3rtc_field_write(priv, K3RTC_ALM_S_CNT_MSW, (seconds >> 32));
Quoted 8 lines

drivers/rtc/rtc-ti-k3.c line 345

Quoted 5 lines
drivers/rtc/rtc-ti-k3.c new file +5 -0
+
+	/* Make sure the alarm time is synced in */
+	ret = k3rtc_fence(priv);
+	if (ret) {
+		dev_err(dev, "Failed to fence(%d)!\n", ret);

I'm not sure this message is useful because the only thing the user may do would be trying to set the time again.

I should probably indicate a potential s/w config problem in driver here - but the fail here is crucial for me to understand if there is some other problem that I have'nt un-covered in the testing (hoping none, but everytime I have had a configuration error, this shows up as a symptom allowing me to drill down to the problem).

Let me know if you feel strongly about this, will drop.

Quoted 10 lines
Quoted 8 lines
drivers/rtc/rtc-ti-k3.c new file +7 -0
+		return ret;
+	}
+
+	/* Alarm irq enable will do a sync */
+	return ti_k3_rtc_alarm_irq_enable(dev, alarm->enabled);
+}
+
Quoted 8 lines

drivers/rtc/rtc-ti-k3.c line 562

Quoted 7 lines
drivers/rtc/rtc-ti-k3.c new file +7 -0
+
+static int k3rtc_get_vbusclk(struct device *dev, struct ti_k3_rtc *priv)
+{
+	int ret;
+	struct clk *clk;
+
+	/* Note: VBUS is'nt a context clock, it is needed for hardware operation */

typo ---------------^

yup, will replace with isn't

Quoted 12 lines

drivers/rtc/rtc-ti-k3.c line 571

Quoted 10 lines
drivers/rtc/rtc-ti-k3.c new file +9 -0
+	clk = devm_clk_get(dev, "vbus");
+	if (IS_ERR(clk)) {
+		dev_err(dev, "No input vbus clock\n");
+		return PTR_ERR(clk);
+	}
+
+	ret = clk_prepare_enable(clk);
+	if (ret) {
+		dev_err(dev, "Failed to enable the vbus clock(%d)\n", ret);

I would also remove those two dev_err

OK.

Quoted 6 lines

drivers/rtc/rtc-ti-k3.c line 538

Quoted 6 lines
drivers/rtc/rtc-ti-k3.c new file +5 -0
+		return ret;
+	}
+
+	ret = devm_add_action_or_reset(dev, (void (*)(void *))clk_disable_unprepare, clk);
+	return ret;

And will drop the ret usage here.. just return the result of
devm_add_action_or_reset

Quoted 9 lines

drivers/rtc/rtc-ti-k3.c line 585

Quoted 9 lines
drivers/rtc/rtc-ti-k3.c new file +9 -0
+}
+
+static int ti_k3_rtc_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct ti_k3_rtc *priv;
+	void __iomem *rtc_base;
+	int ret;
+
	[...]
Quoted 10 lines
Quoted 10 lines
drivers/rtc/rtc-ti-k3.c new file +10 -0
+	ret = k3rtc_configure(dev);
+	if (ret)
+		return ret;
+
+	if (device_property_present(dev, "wakeup-source"))
+		device_init_wakeup(dev, true);
+	else
+		device_set_wakeup_capable(dev, true);
+
+	ret = devm_rtc_register_device(priv->rtc_dev);
Quoted 8 lines

drivers/rtc/rtc-ti-k3.c line 650

Quoted 5 lines
drivers/rtc/rtc-ti-k3.c new file +5 -0
+	if (ret)
+		return ret;
+
+	ret = devm_rtc_nvmem_register(priv->rtc_dev, &ti_k3_rtc_nvmem_config);
+	return ret;

You don't need ret here and if I take that, I'll soon get an automatically generated patch.

Yup. will do.

[...]

Signature · 3 lines
Regards,
Nishanth Menon
Key (0xDDB5849D1736249D) / Fingerprint: F8A2 8693 54EB 8232 17A3  1A34 DDB5 849D 1736 249D