Skip to contentSkip to conversation

rtc: mediatek: add support for SoC based RTC on MT7622

PATCH Superseded [email protected] 4 participants 10 messages Sep 22, 2017 – Oct 17, 2017

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover rtc: mediatek: add support for SoC based RTC on MT7622

From: Sean Wang <[email protected]>

This patchset introduces support for MediaTek SoC based real time clock (RTC) Currently, the driver is already tested successfully with hwclock and wakealarm on MT7622 SoC. And it should also be workable on other similar MediaTek SoCs. And patch 3 is a distinct patch used to distinguish between either SoC based or PMIC based RTC to avoid confusion for the RTC selection for the target SoC.

Sean Wang (4):
dt-bindings: rtc: mediatek: add bindings for MediaTek SoC based RTC
rtc: mediatek: add driver for RTC on MT7622 SoC
rtc: mediatek: enhance the description for MediaTek PMIC based RTC
rtc: mediatek: update MAINTAINERS entry with MediaTek RTC driver

 .../devicetree/bindings/rtc/rtc-mediatek.txt       |  21 ++
 MAINTAINERS                                        |   3 +
 drivers/rtc/Kconfig                                |  18 +-
 drivers/rtc/Makefile                               |   1 +
 drivers/rtc/rtc-mediatek.c                         | 398 +++++++++++++++++++++
 5 files changed, 437 insertions(+), 4 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rtc/rtc-mediatek.txt
 create mode 100644 drivers/rtc/rtc-mediatek.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/4 dt-bindings: rtc: mediatek: add bindings for MediaTek SoC based RTC

From: Sean Wang <[email protected]>

Add device-tree binding for MediaTek SoC based RTC

---
 .../devicetree/bindings/rtc/rtc-mediatek.txt        | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/rtc-mediatek.txt
Documentation/devicetree/bindings/rtc/rtc-mediatek.txt new file +21 -0
new file mode 100644
index 0000000..09fe8f5
@@ -0,0 +1,21 @@
+Device-Tree bindings for MediaTek SoC based RTC
+
+Required properties:
+- compatible	    : Should be
+			"mediatek,mt7622-rtc", "mediatek,soc-rtc" : for MT7622 SoC
+- reg 		    : Specifies base physical address and size of the registers;
+- interrupts	    : Should contain the interrupt for RTC alarm;
+- clocks	    : Specifies list of clock specifiers, corresponding to
+		      entries in clock-names property;
+- clock-names	    : Should contain "rtc" entries
+
+Example:
+
+rtc: rtc@10212800 {
+	compatible = "mediatek,mt7622-rtc",
+		     "mediatek,soc-rtc";
+	reg = <0 0x10212800 0 0x200>;
+	interrupts = <GIC_SPI 129 IRQ_TYPE_LEVEL_LOW>;
+	clocks = <&topckgen CLK_TOP_RTC>;
+	clock-names = "rtc";
+};
Signature · 1 line
2.7.4
#3 lore raw

PATCH 2/4 rtc: mediatek: add driver for RTC on MT7622 SoC

In reply to [email protected]

From: Sean Wang <sean.wang-NuS5LvNUpcJWk0Htik3J/[email protected]>

This patch introduces the driver for the RTC on MT7622 SoC.

---
 drivers/rtc/Kconfig        |  10 ++
 drivers/rtc/Makefile       |   1 +
 drivers/rtc/rtc-mediatek.c | 398 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 409 insertions(+)
 create mode 100644 drivers/rtc/rtc-mediatek.c
drivers/rtc/Kconfig +10 -0
index e0e58f3..4226295 100644
@@ -1705,6 +1705,16 @@ config RTC_DRV_MOXART
 	   This driver can also be built as a module. If so, the module
 	   will be called rtc-moxart
 
+config RTC_DRV_MEDIATEK
+	tristate "MediaTek SoC based RTC"
+	depends on ARCH_MEDIATEK || COMPILE_TEST
+	help
+	  This enables support for the real time clock built in the MediaTek
+	  SoCs.
+
+	  This drive can also be built as a module. If so, the module
+	  will be called rtc-mediatek.
+
 config RTC_DRV_MT6397
 	tristate "Mediatek Real Time Clock driver"
 	depends on MFD_MT6397 || (COMPILE_TEST && IRQ_DOMAIN)
drivers/rtc/Makefile +1 -0
index 7230014..ba0206a 100644
@@ -101,6 +101,7 @@ obj-$(CONFIG_RTC_DRV_MOXART)	+= rtc-moxart.o
 obj-$(CONFIG_RTC_DRV_MPC5121)	+= rtc-mpc5121.o
 obj-$(CONFIG_RTC_DRV_VRTC)	+= rtc-mrst.o
 obj-$(CONFIG_RTC_DRV_MSM6242)	+= rtc-msm6242.o
+obj-$(CONFIG_RTC_DRV_MEDIATEK)	+= rtc-mediatek.o
 obj-$(CONFIG_RTC_DRV_MT6397)	+= rtc-mt6397.o
 obj-$(CONFIG_RTC_DRV_MV)	+= rtc-mv.o
 obj-$(CONFIG_RTC_DRV_MXC)	+= rtc-mxc.o
drivers/rtc/rtc-mediatek.c new file +398 -0

398 lines not on this page · raw message

Signature · 6 lines
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#7 lore raw

PATCH 3/4 rtc: mediatek: enhance the description for MediaTek PMIC based RTC

In reply to [email protected]

From: Sean Wang <[email protected]>

Give a better description for original MediaTek RTC driver as PMIC based RTC in order to distinguish SoC based RTC. Also turning all words with Mediatek to MediaTek here.

---
 drivers/rtc/Kconfig | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/rtc/Kconfig +4 -4
index 4226295..4500f77 100644
@@ -1716,14 +1716,14 @@ config RTC_DRV_MEDIATEK
 	  will be called rtc-mediatek.
 
 config RTC_DRV_MT6397
-	tristate "Mediatek Real Time Clock driver"
+	tristate "MediaTek PMIC based RTC"
 	depends on MFD_MT6397 || (COMPILE_TEST && IRQ_DOMAIN)
 	help
-	  This selects the Mediatek(R) RTC driver. RTC is part of Mediatek
+	  This selects the MediaTek(R) RTC driver. RTC is part of MediaTek
 	  MT6397 PMIC. You should enable MT6397 PMIC MFD before select
-	  Mediatek(R) RTC driver.
+	  MediaTek(R) RTC driver.
 
-	  If you want to use Mediatek(R) RTC interface, select Y or M here.
+	  If you want to use MediaTek(R) RTC interface, select Y or M here.
 
 config RTC_DRV_XGENE
 	tristate "APM X-Gene RTC"
Signature · 1 line
2.7.4
#9 lore raw

PATCH 4/4 rtc: mediatek: update MAINTAINERS entry with MediaTek RTC driver

In reply to [email protected]

From: Sean Wang <[email protected]>

I work for MediaTek on maintaining the MediaTek SoC based RTC driver for the existing SoCs and keep adding support for the following SoCs in the future.

---
 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)
MAINTAINERS +3 -0
index 2281af4..5142029 100644
@@ -1579,10 +1579,13 @@ F:	drivers/rtc/rtc-armada38x.c
 
 ARM/Mediatek RTC DRIVER
 M:	Eddie Huang <[email protected]>
+M:	Sean Wang <[email protected]>
 L:	[email protected] (moderated for non-subscribers)
 L:	[email protected] (moderated for non-subscribers)
 S:	Maintained
+F:	Documentation/devicetree/bindings/rtc/rtc-mediatek.txt
 F:	drivers/rtc/rtc-mt6397.c
+F:	drivers/rtc/rtc-mediatek.c
 
 ARM/Mediatek SoC support
 M:	Matthias Brugger <[email protected]>
Signature · 1 line
2.7.4
Acked-by on v1 3/4
#8 lore raw

Re: [PATCH 3/4] rtc: mediatek: enhance the description for MediaTek PMIC based RTC

In reply to [email protected]

Hi Sean,

Quoted 25 lines · On Fri, 2017-09-22 at 11:33 +0800, sean.wang-NuS5LvNUpcJWk0Htik3J/[email protected]…

From: Sean Wang <sean.wang-NuS5LvNUpcJWk0Htik3J/[email protected]>

Give a better description for original MediaTek RTC driver as PMIC based RTC in order to distinguish SoC based RTC. Also turning all words with Mediatek to MediaTek here.

Cc Eddie Huang <eddie.huang-NuS5LvNUpcJWk0Htik3J/[email protected]>
---
 drivers/rtc/Kconfig | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/rtc/Kconfig +1 -2
index 4226295..4500f77 100644
@@ -1716,14 +1716,14 @@ config RTC_DRV_MEDIATEK
 	  will be called rtc-mediatek.
 
 config RTC_DRV_MT6397
-	tristate "Mediatek Real Time Clock driver"
+	tristate "MediaTek PMIC based RTC"
 	depends on MFD_MT6397 || (COMPILE_TEST && IRQ_DOMAIN)
 	help
-	  This selects the Mediatek(R) RTC driver. RTC is part of Mediatek

drivers/rtc/Kconfig config RTC_DRV_MEDIATEK · line 1729

Quoted 10 lines
drivers/rtc/Kconfig +3 -2
+	  This selects the MediaTek(R) RTC driver. RTC is part of MediaTek
 	  MT6397 PMIC. You should enable MT6397 PMIC MFD before select
-	  Mediatek(R) RTC driver.
+	  MediaTek(R) RTC driver.
 
-	  If you want to use Mediatek(R) RTC interface, select Y or M here.
+	  If you want to use MediaTek(R) RTC interface, select Y or M here.
 
 config RTC_DRV_XGENE
 	tristate "APM X-Gene RTC"

Thanks your correction.

Regards,

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer Acked-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] dt-bindings: rtc: mediatek: add bindings for MediaTek SoC based RTC

In reply to [email protected]
Quoted 10 lines · On Fri, Sep 22, 2017 at 11:33:14AM +0800, [email protected] wrote:

From: Sean Wang <[email protected]>

Add device-tree binding for MediaTek SoC based RTC

---
 .../devicetree/bindings/rtc/rtc-mediatek.txt        | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/rtc-mediatek.txt
#4 lore raw

Re: [PATCH 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC

In reply to [email protected]

Hi,

drivers/rtc/rtc-mediatek.c

Quoted 1 line · On 22/09/2017 at 11:33:15 +0800, [email protected] wrote:
drivers/rtc/rtc-mediatek.c +0 -0

I'm pretty sure this should be named rtc-mt7622.c instead of rtc-mediatek.c, exactly for the same reason you have patch 3/4.

drivers/rtc/rtc-mediatek.c line 92

Quoted 3 lines
drivers/rtc/rtc-mediatek.c +3 -0
+static void mtk_w32(struct mtk_rtc *rtc, u32 reg, u32 val)
+{
+	__raw_writel(val, rtc->base + reg);

Do you really need the __raw accessors? What about running your SoC in BE mode? I guess the _relaxed version are fast enough.

Quoted 38 lines
drivers/rtc/rtc-mediatek.c +36 -0
+}
+
+static u32 mtk_r32(struct mtk_rtc *rtc, u32 reg)
+{
+	return __raw_readl(rtc->base + reg);
+}
+
 
 
+static void mtk_rtc_hw_init(struct mtk_rtc *hw)
+{
+	/* The setup of the init sequence is for allowing RTC got to work */
+	mtk_w32(hw, MTK_RTC_PWRCHK1, RTC_PWRCHK1_MAGIC);
+	mtk_w32(hw, MTK_RTC_PWRCHK2, RTC_PWRCHK2_MAGIC);
+	mtk_w32(hw, MTK_RTC_KEY, RTC_KEY_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT1, RTC_PROT1_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT2, RTC_PROT2_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT3, RTC_PROT3_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT4, RTC_PROT4_MAGIC);
+	mtk_rmw(hw, MTK_RTC_DEBNCE, RTC_DEBNCE_MASK, 0);
+	mtk_clr(hw, MTK_RTC_CTL, RTC_RC_STOP);
+}
+
+static void mtk_rtc_get_alarm_or_time(struct mtk_rtc *hw, struct rtc_time *tm,
+				      int time_alarm)
+{
+	u32 year, mon, mday, wday, hour, min, sec;
+
+	/*
+	 * Read again until all fields are not changed for all fields in the
+	 * consistent state.
+	 */
+	do {
+		year = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_YEA));
+		mon = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MON));
+		wday = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOW));
+		mday = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOM));
+		hour = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_HOU));

drivers/rtc/rtc-mediatek.c line 158

Quoted 10 lines
drivers/rtc/rtc-mediatek.c +10 -0
+		min = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MIN));
+		sec = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_SEC));
+	} while (year != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_YEA)) ||
+		 mon != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MON))  ||
+		 mday != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOM))	||
+		 wday != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOW))	||
+		 hour != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_HOU))	||
+		 min != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MIN))	||
+		 sec != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_SEC))
+		);

I'm pretty sure only checking sec is enough because it is highly unlikely that 7 reads take a minute.

drivers/rtc/rtc-mediatek.c line 188

Quoted 7 lines
drivers/rtc/rtc-mediatek.c +7 -0
+static irqreturn_t mtk_rtc_alarmirq(int irq, void *id)
+{
+	struct mtk_rtc *hw = (struct mtk_rtc *)id;
+	u32 irq_sta;
+
+	/* Stop alarm also implicitly disable the alarm interrupt */
+	mtk_w32(hw, MTK_RTC_AL_CTL, 0);

You stop the alarm here, before testing whether the alarm really happened.

Quoted 21 lines
drivers/rtc/rtc-mediatek.c +21 -0
+	irq_sta = mtk_r32(hw, MTK_RTC_INT);
+	if (irq_sta & RTC_INT_AL_STA) {
+		rtc_update_irq(hw->rtc, 1, RTC_IRQF | RTC_AF);
+
+		/* Ack alarm interrupt status */
+		mtk_w32(hw, MTK_RTC_INT, RTC_INT_AL_STA);
+		return IRQ_HANDLED;
+	}
+
+	return IRQ_NONE;
+}
+
+static int mtk_rtc_gettime(struct device *dev, struct rtc_time *tm)
+{
+	struct mtk_rtc *hw = dev_get_drvdata(dev);
+
+	mtk_rtc_get_alarm_or_time(hw, tm, MTK_TC);
+
+	return rtc_valid_tm(tm);
+}
+

drivers/rtc/rtc-mediatek.c line 219

Quoted 10 lines
drivers/rtc/rtc-mediatek.c +10 -0
+static int mtk_rtc_settime(struct device *dev, struct rtc_time *tm)
+{
+	struct mtk_rtc *hw = dev_get_drvdata(dev);
+
+	/* Stop time counter before setting a new one*/
+	mtk_set(hw, MTK_RTC_CTL, RTC_RC_STOP);
+
+	/* Epoch == 1900 */
+	if (tm->tm_year < 100 || tm->tm_year > 199)
+		return -EINVAL;

Year is a 32 bits register, what makes the RTC fail in 2100? Is it because of the leap year handling?

drivers/rtc/rtc-mediatek.c line 250

Quoted 9 lines
drivers/rtc/rtc-mediatek.c +9 -0
+static int mtk_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
+{
+	struct mtk_rtc *hw = dev_get_drvdata(dev);
+	struct rtc_time *alrm_tm = &wkalrm->time;
+
+	/* Epoch == 1900 */
+	if (alrm_tm->tm_year < 100 || alrm_tm->tm_year > 199)
+		return -EINVAL;
+

Ditto.

drivers/rtc/rtc-mediatek.c line 339

Quoted 3 lines
drivers/rtc/rtc-mediatek.c +3 -0
+
+	dev_info(&pdev->dev, "MediaTek SoC based RTC enabled\n");
+

I think the rtc core is verbose enough that this message is not needed.

Signature · 3 lines
Alexandre Belloni, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
author
#5 lore raw

Re: [PATCH 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC

Hi Alexandre,

Thanks for your valuable suggestions on the driver.

I added comments inline and will have following-ups in the next version

Sean

Quoted 8 lines · Alexandre Belloni

Hi,

drivers/rtc/rtc-mediatek.c

Quoted 1 line · On 22/09/2017 at 11:33:15 +0800, [email protected] wrote:
drivers/rtc/rtc-mediatek.c +0 -0

I'm pretty sure this should be named rtc-mt7622.c instead of rtc-mediatek.c, exactly for the same reason you have patch 3/4.

It's okay for naming with rtc-mt7622.c at this moment. But if more SoCs support gets into the driver, I will consider again to give a more generic name for the driver.

Quoted 7 lines

drivers/rtc/rtc-mediatek.c line 92

Quoted 3 lines
drivers/rtc/rtc-mediatek.c +3 -0
+static void mtk_w32(struct mtk_rtc *rtc, u32 reg, u32 val)
+{
+	__raw_writel(val, rtc->base + reg);

Do you really need the __raw accessors? What about running your SoC in BE mode? I guess the _relaxed version are fast enough.

SoC runs on LE mode. I also think it's fine and enough to use _relaxed version instead of __raw version.

Quoted 43 lines
Quoted 43 lines
drivers/rtc/rtc-mediatek.c +41 -0
+}
+
+static u32 mtk_r32(struct mtk_rtc *rtc, u32 reg)
+{
+	return __raw_readl(rtc->base + reg);
+}
+
 
 
+static void mtk_rtc_hw_init(struct mtk_rtc *hw)
+{
+	/* The setup of the init sequence is for allowing RTC got to work */
+	mtk_w32(hw, MTK_RTC_PWRCHK1, RTC_PWRCHK1_MAGIC);
+	mtk_w32(hw, MTK_RTC_PWRCHK2, RTC_PWRCHK2_MAGIC);
+	mtk_w32(hw, MTK_RTC_KEY, RTC_KEY_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT1, RTC_PROT1_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT2, RTC_PROT2_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT3, RTC_PROT3_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT4, RTC_PROT4_MAGIC);
+	mtk_rmw(hw, MTK_RTC_DEBNCE, RTC_DEBNCE_MASK, 0);
+	mtk_clr(hw, MTK_RTC_CTL, RTC_RC_STOP);
+}
+
+static void mtk_rtc_get_alarm_or_time(struct mtk_rtc *hw, struct rtc_time *tm,
+				      int time_alarm)
+{
+	u32 year, mon, mday, wday, hour, min, sec;
+
+	/*
+	 * Read again until all fields are not changed for all fields in the
+	 * consistent state.
+	 */
+	do {
+		year = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_YEA));
+		mon = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MON));
+		wday = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOW));
+		mday = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOM));
+		hour = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_HOU));
+		min = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MIN));
+		sec = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_SEC));
+	} while (year != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_YEA)) ||
+		 mon != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MON))  ||
+		 mday != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOM))	||
Quoted 9 lines

drivers/rtc/rtc-mediatek.c line 158

Quoted 5 lines
drivers/rtc/rtc-mediatek.c +5 -0
+		 wday != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOW))	||
+		 hour != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_HOU))	||
+		 min != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MIN))	||
+		 sec != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_SEC))
+		);

I'm pretty sure only checking sec is enough because it is highly unlikely that 7 reads take a minute.

You're right. I made something stupid here. Only checking on sec is enough and will give simpler and better code.

Quoted 11 lines

drivers/rtc/rtc-mediatek.c line 188

Quoted 7 lines
drivers/rtc/rtc-mediatek.c +7 -0
+static irqreturn_t mtk_rtc_alarmirq(int irq, void *id)
+{
+	struct mtk_rtc *hw = (struct mtk_rtc *)id;
+	u32 irq_sta;
+
+	/* Stop alarm also implicitly disable the alarm interrupt */
+	mtk_w32(hw, MTK_RTC_AL_CTL, 0);

You stop the alarm here, before testing whether the alarm really happened.

Okay. I will exchange the order for alarm stopping and the examination whether the alarm is really expired.

Quoted 26 lines
Quoted 26 lines
drivers/rtc/rtc-mediatek.c +26 -0
+	irq_sta = mtk_r32(hw, MTK_RTC_INT);
+	if (irq_sta & RTC_INT_AL_STA) {
+		rtc_update_irq(hw->rtc, 1, RTC_IRQF | RTC_AF);
+
+		/* Ack alarm interrupt status */
+		mtk_w32(hw, MTK_RTredundantC_INT, RTC_INT_AL_STA);
+		return IRQ_HANDLED;
+	}
+
+	return IRQ_NONE;
+}
+
+static int mtk_rtc_gettime(struct device *dev, struct rtc_time *tm)
+{
+	struct mtk_rtc *hw = dev_get_drvdata(dev);
+
+	mtk_rtc_get_alarm_or_time(hw, tm, MTK_TC);
+
+	return rtc_valid_tm(tm);
+}
+
+static int mtk_rtc_settime(struct device *dev, struct rtc_time *tm)
+{
+	struct mtk_rtc *hw = dev_get_drvdata(dev);
+
+	/* Stop time counter before setting a new one*/
Quoted 9 lines

drivers/rtc/rtc-mediatek.c line 219

Quoted 5 lines
drivers/rtc/rtc-mediatek.c +5 -0
+	mtk_set(hw, MTK_RTC_CTL, RTC_RC_STOP);
+
+	/* Epoch == 1900 */
+	if (tm->tm_year < 100 || tm->tm_year > 199)
+		return -EINVAL;

Year is a 32 bits register, what makes the RTC fail in 2100? Is it because of the leap year handling?

I made something stupid again here: rtc hardware doesn't have such the limitation. I just felt alarm set up prior to 2100 is enough in my initial thought, but it seemed I shouldn't do this. I will remove the sanity condition.

Quoted 12 lines

drivers/rtc/rtc-mediatek.c line 250

Quoted 9 lines
drivers/rtc/rtc-mediatek.c +9 -0
+static int mtk_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
+{
+	struct mtk_rtc *hw = dev_get_drvdata(dev);
+	struct rtc_time *alrm_tm = &wkalrm->time;
+
+	/* Epoch == 1900 */
+	if (alrm_tm->tm_year < 100 || alrm_tm->tm_year > 199)
+		return -EINVAL;
+

Ditto.

Ditto. those condition will be removed.

Quoted 6 lines

drivers/rtc/rtc-mediatek.c line 339

Quoted 3 lines
drivers/rtc/rtc-mediatek.c +3 -0
+
+	dev_info(&pdev->dev, "MediaTek SoC based RTC enabled\n");
+

I think the rtc core is verbose enough that this message is not needed.

Okay. the redundant and specific log prompt would be removed as well.

Quoted 1 line
author
#6 lore raw

Re: [PATCH 2/4] rtc: mediatek: add driver for RTC on MT7622 SoC

Quoted 140 lines · Sean Wang

Hi Alexandre,

Thanks for your valuable suggestions on the driver.

I added comments inline and will have following-ups in the next version

Sean

Quoted 8 lines · Alexandre Belloni

Hi,

drivers/rtc/rtc-mediatek.c

Quoted 1 line · On 22/09/2017 at 11:33:15 +0800, sean.wang-NuS5LvNUpcJWk0Htik3J/[email protected].…
drivers/rtc/rtc-mediatek.c +0 -0

I'm pretty sure this should be named rtc-mt7622.c instead of rtc-mediatek.c, exactly for the same reason you have patch 3/4.

It's okay for naming with rtc-mt7622.c at this moment. But if more SoCs support gets into the driver, I will consider again to give a more generic name for the driver.

Quoted 7 lines

drivers/rtc/rtc-mediatek.c line 92

Quoted 3 lines
drivers/rtc/rtc-mediatek.c +3 -0
+static void mtk_w32(struct mtk_rtc *rtc, u32 reg, u32 val)
+{
+	__raw_writel(val, rtc->base + reg);

Do you really need the __raw accessors? What about running your SoC in BE mode? I guess the _relaxed version are fast enough.

SoC runs on LE mode. I also think it's fine and enough to use _relaxed version instead of __raw version.

Quoted 43 lines
Quoted 43 lines
drivers/rtc/rtc-mediatek.c +41 -0
+}
+
+static u32 mtk_r32(struct mtk_rtc *rtc, u32 reg)
+{
+	return __raw_readl(rtc->base + reg);
+}
+
 
 
+static void mtk_rtc_hw_init(struct mtk_rtc *hw)
+{
+	/* The setup of the init sequence is for allowing RTC got to work */
+	mtk_w32(hw, MTK_RTC_PWRCHK1, RTC_PWRCHK1_MAGIC);
+	mtk_w32(hw, MTK_RTC_PWRCHK2, RTC_PWRCHK2_MAGIC);
+	mtk_w32(hw, MTK_RTC_KEY, RTC_KEY_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT1, RTC_PROT1_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT2, RTC_PROT2_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT3, RTC_PROT3_MAGIC);
+	mtk_w32(hw, MTK_RTC_PROT4, RTC_PROT4_MAGIC);
+	mtk_rmw(hw, MTK_RTC_DEBNCE, RTC_DEBNCE_MASK, 0);
+	mtk_clr(hw, MTK_RTC_CTL, RTC_RC_STOP);
+}
+
+static void mtk_rtc_get_alarm_or_time(struct mtk_rtc *hw, struct rtc_time *tm,
+				      int time_alarm)
+{
+	u32 year, mon, mday, wday, hour, min, sec;
+
+	/*
+	 * Read again until all fields are not changed for all fields in the
+	 * consistent state.
+	 */
+	do {
+		year = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_YEA));
+		mon = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MON));
+		wday = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOW));
+		mday = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOM));
+		hour = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_HOU));
+		min = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MIN));
+		sec = mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_SEC));
+	} while (year != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_YEA)) ||
+		 mon != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MON))  ||
+		 mday != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOM))	||
Quoted 9 lines

drivers/rtc/rtc-mediatek.c line 158

Quoted 5 lines
drivers/rtc/rtc-mediatek.c +5 -0
+		 wday != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_DOW))	||
+		 hour != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_HOU))	||
+		 min != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_MIN))	||
+		 sec != mtk_r32(hw, MTK_RTC_TREG(time_alarm, MTK_SEC))
+		);

I'm pretty sure only checking sec is enough because it is highly unlikely that 7 reads take a minute.

You're right. I made something stupid here. Only checking on sec is enough and will give simpler and better code.

Quoted 11 lines

drivers/rtc/rtc-mediatek.c line 188

Quoted 7 lines
drivers/rtc/rtc-mediatek.c +7 -0
+static irqreturn_t mtk_rtc_alarmirq(int irq, void *id)
+{
+	struct mtk_rtc *hw = (struct mtk_rtc *)id;
+	u32 irq_sta;
+
+	/* Stop alarm also implicitly disable the alarm interrupt */
+	mtk_w32(hw, MTK_RTC_AL_CTL, 0);

You stop the alarm here, before testing whether the alarm really happened.

Okay. I will exchange the order for alarm stopping and the examination whether the alarm is really expired.

Quoted 26 lines
Quoted 26 lines
drivers/rtc/rtc-mediatek.c +26 -0
+	irq_sta = mtk_r32(hw, MTK_RTC_INT);
+	if (irq_sta & RTC_INT_AL_STA) {
+		rtc_update_irq(hw->rtc, 1, RTC_IRQF | RTC_AF);
+
+		/* Ack alarm interrupt status */
+		mtk_w32(hw, MTK_RTredundantC_INT, RTC_INT_AL_STA);
+		return IRQ_HANDLED;
+	}
+
+	return IRQ_NONE;
+}
+
+static int mtk_rtc_gettime(struct device *dev, struct rtc_time *tm)
+{
+	struct mtk_rtc *hw = dev_get_drvdata(dev);
+
+	mtk_rtc_get_alarm_or_time(hw, tm, MTK_TC);
+
+	return rtc_valid_tm(tm);
+}
+
+static int mtk_rtc_settime(struct device *dev, struct rtc_time *tm)
+{
+	struct mtk_rtc *hw = dev_get_drvdata(dev);
+
+	/* Stop time counter before setting a new one*/
Quoted 9 lines

drivers/rtc/rtc-mediatek.c line 219

Quoted 5 lines
drivers/rtc/rtc-mediatek.c +5 -0
+	mtk_set(hw, MTK_RTC_CTL, RTC_RC_STOP);
+
+	/* Epoch == 1900 */
+	if (tm->tm_year < 100 || tm->tm_year > 199)
+		return -EINVAL;

Year is a 32 bits register, what makes the RTC fail in 2100? Is it because of the leap year handling?

Quoted 5 lines

I made something stupid again here: rtc hardware doesn't have such the limitation. I just felt alarm set up prior to 2100 is enough in my initial thought, but it seemed I shouldn't do this. I will remove the sanity condition.

Sorry for that I gave incorrect information for the RTC in the previous
reply: After check again the usage of the register, the maximum number of the year the RTC can hold is 99 and then wraparound to 0 when overflow occurs although the year register is a 32 bits register.

Therefore, the sanity for tm->tm_year is still required for the both setup handler on alarm and rtc to ensure the user input data is valid, where the current driver assume it's valid when tm->tm_year is between 2000 and 2099. I'll add more comments for the hardware limitation.

Sean

Quoted 15 lines
Quoted 13 lines

drivers/rtc/rtc-mediatek.c line 250

Quoted 10 lines
drivers/rtc/rtc-mediatek.c +9 -0
+static int mtk_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
+{
+	struct mtk_rtc *hw = dev_get_drvdata(dev);
+	struct rtc_time *alrm_tm = &wkalrm->time;
+
+	/* Epoch == 1900 */
+	if (alrm_tm->tm_year < 100 || alrm_tm->tm_year > 199)
+		return -EINVAL;
+

Ditto.

Ditto. those condition will be removed.

Quoted 12 lines
Quoted 6 lines

drivers/rtc/rtc-mediatek.c line 339

Quoted 3 lines
drivers/rtc/rtc-mediatek.c +3 -0
+
+	dev_info(&pdev->dev, "MediaTek SoC based RTC enabled\n");
+

I think the rtc core is verbose enough that this message is not needed.

Okay. the redundant and specific log prompt would be removed as well.

Quoted 1 line

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html