Skip to contentSkip to conversation

Add Mediatek SoC RTC driver

PATCH Superseded Eddie Huang 5 participants 12 messages Apr 28, 2015 – May 6, 2015

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Eddie Huang

author maintainer
#0 lore raw

v3 cover Add Mediatek SoC RTC driver

RTC is one submodule of Mediatek MT6397 PMIC chip[1]. This series support RTC driver that work with Mediatek SoC like MT8135, MT8173. It implements second counter and also provide alarm function.

This series base on 4.1-rc1, Test ok on MT8173 platform.

[1] https://lkml.org/lkml/2015/1/23/325

Change in v3:
1. Replace magic number in mt6397-core.c
2. Add comment for some equation and write trigger.
3. Use regmap_bulk_read and regmap_bulk_write to avoid muliple regmap_read and regmap_write
4. Replace devm_request_threaded_irq with request_threaded_irq and add irq_dispose_mapping
5. Fix Tomasz Figa review comment.

Change in v2:
1. Move RTC address and interrupt to mt6397-core.c, and register these resource in mfd_cell.
http://lists.infradead.org/pipermail/linux-arm-kernel/2015-February/323239.html
2. Remove dt-binding document due to register resouce in mfd_cell, not from device tree.
3. Update MAINTAINER file to add Mediatek RTC mainainter.
4. Add prefix mtk_ to some internal functions.
5. Fix racy condition
6. Check return value of regmap_read and regmap_write
7. Remove some unnecessary register readback, clear, then write.
8. Add disable alarm in mtk_rtc_set_alarm function
9. Fix Uwe Kleine-K?nig review comment

Eddie Huang (2):
mfd: provide RTC resource in MT6397 MFD
MAINTAINERS: add Mediatek RTC driver

Tianping Fang (1):
rtc: mediatek: Add MT6397 RTC driver

 MAINTAINERS               |   7 +
 drivers/mfd/mt6397-core.c |  18 +++
 drivers/rtc/Kconfig       |  10 ++
 drivers/rtc/Makefile      |   1 +
 drivers/rtc/rtc-mt6397.c  | 388 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 424 insertions(+)
 create mode 100644 drivers/rtc/rtc-mt6397.c

--
1.8.1.1.dirty

author maintainer
#1 lore raw

PATCH v3 1/3 mfd: provide RTC resource in MT6397 MFD

Provide MT6397 RTC interrupt, base address, and register in MT6397 MFD.

---
 drivers/mfd/mt6397-core.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/mfd/mt6397-core.c +18 -0
index 09bc780..08cfbd1 100644
@@ -21,9 +21,27 @@
 #include <linux/mfd/mt6397/core.h>
 #include <linux/mfd/mt6397/registers.h>
 
+#define MT6397_RTC_BASE		0xe000
+#define MT6397_RTC_SIZE		0x3e
+
+static const struct resource mt6397_rtc_resources[] = {
+	{
+		.start = MT6397_RTC_BASE,
+		.end   = MT6397_RTC_BASE + MT6397_RTC_SIZE,
+		.flags  = IORESOURCE_MEM,
+	},
+	{
+		.start = MT6397_IRQ_RTC,
+		.end   = MT6397_IRQ_RTC,
+		.flags  = IORESOURCE_IRQ,
+	},
+};
+
 static const struct mfd_cell mt6397_devs[] = {
 	{
 		.name = "mt6397-rtc",
+		.num_resources = ARRAY_SIZE(mt6397_rtc_resources),
+		.resources = mt6397_rtc_resources,
 		.of_compatible = "mediatek,mt6397-rtc",
 	}, {
 		.name = "mt6397-regulator",
Signature · 1 line
1.8.1.1.dirty
#2 lore raw

Re: [PATCH v3 1/3] mfd: provide RTC resource in MT6397 MFD

Hello,

Quoted 14 lines · Eddie Huang

Provide MT6397 RTC interrupt, base address, and register in MT6397 MFD.

---
 drivers/mfd/mt6397-core.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/mfd/mt6397-core.c +0 -0
index 09bc780..08cfbd1 100644
@@ -21,9 +21,27 @@
 #include <linux/mfd/mt6397/core.h>

drivers/mfd/mt6397-core.c line 31

Quoted 10 lines
drivers/mfd/mt6397-core.c +8 -0
 #include <linux/mfd/mt6397/registers.h>
 
+#define MT6397_RTC_BASE		0xe000
+#define MT6397_RTC_SIZE		0x3e
+
+static const struct resource mt6397_rtc_resources[] = {
+	{
+		.start = MT6397_RTC_BASE,
+		.end   = MT6397_RTC_BASE + MT6397_RTC_SIZE,
+		.flags  = IORESOURCE_MEM,

It's definitly a matter of taste if you align the rhs here, but if you do, do it consitently. That is, either make sure that all equal signs are in a single column (at least per struct resource), or use a single space between variable name and =.

If you want to hear my personal preference: I always use a single space. As if you have to add
.somelongvariablename = somevalue

you have to touch all other lines, too, which is ugly.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-K?nig            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Acked-by on v3 1/3
#3 lore raw

Re: [PATCH v3 1/3] mfd: provide RTC resource in MT6397 MFD

In reply to Eddie Huang
Quoted 7 lines · Eddie Huang

Provide MT6397 RTC interrupt, base address, and register in MT6397 MFD.

---
 drivers/mfd/mt6397-core.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

*after* Uwe's concerns have been attended to, you may add my:

FWIW, I quite like straight lines. :)

Quoted 32 lines
drivers/mfd/mt6397-core.c +18 -0
index 09bc780..08cfbd1 100644
@@ -21,9 +21,27 @@
 #include <linux/mfd/mt6397/core.h>
 #include <linux/mfd/mt6397/registers.h>
 
+#define MT6397_RTC_BASE		0xe000
+#define MT6397_RTC_SIZE		0x3e
+
+static const struct resource mt6397_rtc_resources[] = {
+	{
+		.start = MT6397_RTC_BASE,
+		.end   = MT6397_RTC_BASE + MT6397_RTC_SIZE,
+		.flags  = IORESOURCE_MEM,
+	},
+	{
+		.start = MT6397_IRQ_RTC,
+		.end   = MT6397_IRQ_RTC,
+		.flags  = IORESOURCE_IRQ,
+	},
+};
+
 static const struct mfd_cell mt6397_devs[] = {
 	{
 		.name = "mt6397-rtc",
+		.num_resources = ARRAY_SIZE(mt6397_rtc_resources),
+		.resources = mt6397_rtc_resources,
 		.of_compatible = "mediatek,mt6397-rtc",
 	}, {
 		.name = "mt6397-regulator",
Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org ? Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
author maintainer
#4 lore raw

PATCH v3 2/3 rtc: mediatek: Add MT6397 RTC driver

In reply to Eddie Huang

From: Tianping Fang <[email protected]>

Add Mediatek MT6397 RTC driver

---
 drivers/rtc/Kconfig      |  10 ++
 drivers/rtc/Makefile     |   1 +
 drivers/rtc/rtc-mt6397.c | 388 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 399 insertions(+)
 create mode 100644 drivers/rtc/rtc-mt6397.c
drivers/rtc/Kconfig +10 -0
index 6149ae0..9608fcb 100644
@@ -1520,6 +1520,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_MT6397
+	tristate "Mediatek Real Time Clock driver"
+	depends on MFD_MT6397 || COMPILE_TEST
+	help
+	  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.
+
+	  If you want to use Mediatek(R) RTC interface, select Y or M here.
+
 config RTC_DRV_XGENE
 	tristate "APM X-Gene RTC"
 	depends on HAS_IOMEM
drivers/rtc/Makefile +1 -0
index c31731c..6ba0ce2 100644
@@ -154,3 +154,4 @@ obj-$(CONFIG_RTC_DRV_X1205)	+= rtc-x1205.o
 obj-$(CONFIG_RTC_DRV_XGENE)	+= rtc-xgene.o
 obj-$(CONFIG_RTC_DRV_SIRFSOC)	+= rtc-sirfsoc.o
 obj-$(CONFIG_RTC_DRV_MOXART)	+= rtc-moxart.o
+obj-$(CONFIG_RTC_DRV_MT6397)	+= rtc-mt6397.o
drivers/rtc/rtc-mt6397.c new file +388 -0

388 lines not on this page · raw message

Signature · 1 line
1.8.1.1.dirty
#5 lore raw

Re: [rtc-linux] [PATCH v3 2/3] rtc: mediatek: Add MT6397 RTC driver

Hi,

This looks mostly good. Could you align the wrapped function parameters to the open parenthesis (use checkpatch --strict)?

Quoted 8 lines · Eddie Huang
drivers/rtc/rtc-mt6397.c new file +8 -0
+static int mtk_rtc_write_trigger(struct mt6397_rtc *rtc)
+{
+	unsigned long timeout = jiffies + HZ;
+	int ret;
+	u32 data;
+
+	ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_WRTGR, 1);
+	if (ret < 0)

drivers/rtc/rtc-mt6397.c line 92

Quoted 10 lines
drivers/rtc/rtc-mt6397.c new file +10 -0
+		return ret;
+
+	do {
+		cpu_relax();
+		ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_BBPU,
+				&data);
+		if (ret < 0)
+			goto exit;
+	} while ((data & RTC_BBPU_CBUSY) && time_after(timeout, jiffies));
+

Shouldn't you return -ETIMEDOUT if the loop breaks because of time_after?

Thanks,

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

Re: [rtc-linux] [PATCH v3 2/3] rtc: mediatek: Add MT6397 RTC driver

Quoted 18 lines · Alexandre Belloni

Hi,

This looks mostly good. Could you align the wrapped function parameters to the open parenthesis (use checkpatch --strict)?

Quoted 12 lines · Eddie Huang
drivers/rtc/rtc-mt6397.c new file +12 -0
+static int mtk_rtc_write_trigger(struct mt6397_rtc *rtc)
+{
+	unsigned long timeout = jiffies + HZ;
+	int ret;
+	u32 data;
+
+	ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_WRTGR, 1);
+	if (ret < 0)
+		return ret;
+
+	do {
+		cpu_relax();
Quoted 8 lines

drivers/rtc/rtc-mt6397.c line 92

Quoted 6 lines
drivers/rtc/rtc-mt6397.c new file +6 -0
+		ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_BBPU,
+				&data);
+		if (ret < 0)
+			goto exit;
+	} while ((data & RTC_BBPU_CBUSY) && time_after(timeout, jiffies));
+

Shouldn't you return -ETIMEDOUT if the loop breaks because of time_after?

Probably yes.

I believe as written the time_after test is too much for my little brain. I would have used time_before and reversed the args.

I suggest moving the time_after() test into the loop, use break; and remove the exit label too.

Maybe something like:

	while (1) {
		ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_BBPU,
				  &data);
		if (ret < 0)
			break;
		if (!(data & RTC_BBPU_CBUSY))
			break;
		if (time_after(jiffies, timeout)) {
			ret = -ETIMEDOUT;
			break;
		}
		cpu_relax();
	}

	return ret;
}
#7 lore raw

Re: [rtc-linux] [PATCH v3 2/3] rtc: mediatek: Add MT6397 RTC driver

Quoted 5 lines · Joe Perches

I suggest moving the time_after() test into the loop, use break; and remove the exit label too.

Maybe something like:

Quoted 15 lines
	while (1) {
		ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_BBPU,
				  &data);
		if (ret < 0)
			break;
		if (!(data & RTC_BBPU_CBUSY))
			break;
		if (time_after(jiffies, timeout)) {
			ret = -ETIMEDOUT;
			break;
		}
		cpu_relax();
	}

	return ret;

That certainly looks more readable.

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

Re: [rtc-linux] [PATCH v3 2/3] rtc: mediatek: Add MT6397 RTC driver

Hi Joe and Alexandre,

Quoted 20 lines · Alexandre Belloni
Quoted 19 lines · Joe Perches

I suggest moving the time_after() test into the loop, use break; and remove the exit label too.

Maybe something like:

	while (1) {
		ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_BBPU,
				  &data);
		if (ret < 0)
			break;
		if (!(data & RTC_BBPU_CBUSY))
			break;
		if (time_after(jiffies, timeout)) {
			ret = -ETIMEDOUT;
			break;
		}
		cpu_relax();
	}
Quoted 4 lines
Quoted 1 line
	return ret;

That certainly looks more readable.

Thanks correct me that I put wrong paramters in time_after, and give me good example. I will adopt your suggestion in next round.

Eddie

author maintainer
#9 lore raw

PATCH v3 3/3 MAINTAINERS: add Mediatek RTC driver

In reply to Eddie Huang

Add Mediatek RTC driver to maintainer entry.

---
 MAINTAINERS | 7 +++++++
 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
index 2e5bbc0..eb80610 100644
@@ -1223,6 +1223,13 @@ W:	http://www.digriz.org.uk/ts78xx/kernel
 S:	Maintained
 F:	arch/arm/mach-orion5x/ts78xx-*
 
+ARM/Mediatek RTC DRIVER
+M:	Eddie Huang <[email protected]>
+L:	linux-arm-kernel at lists.infradead.org (moderated for non-subscribers)
+L:	linux-mediatek at lists.infradead.org (moderated for non-subscribers)
+S:	Maintained
+F:	drivers/rtc/rtc-mt*
+
 ARM/Mediatek SoC support
 M:	Matthias Brugger <[email protected]>
 L:	linux-arm-kernel at lists.infradead.org (moderated for non-subscribers)
Signature · 1 line
1.8.1.1.dirty
#10 lore raw

Re: [rtc-linux] [PATCH v3 3/3] MAINTAINERS: add Mediatek RTC driver

Hi,

Quoted 5 lines · Eddie Huang
MAINTAINERS +0 -0
index 2e5bbc0..eb80610 100644

MAINTAINERS W: http://www.digriz.org.uk/ts78xx/kernel · line 1231

Quoted 9 lines
MAINTAINERS +6 -0
@@ -1223,6 +1223,13 @@ W:	http://www.digriz.org.uk/ts78xx/kernel
 S:	Maintained
 F:	arch/arm/mach-orion5x/ts78xx-*
 
+ARM/Mediatek RTC DRIVER
+M:	Eddie Huang <[email protected]>
+L:	linux-arm-kernel at lists.infradead.org (moderated for non-subscribers)
+L:	linux-mediatek at lists.infradead.org (moderated for non-subscribers)
+S:	Maintained
+F:	drivers/rtc/rtc-mt*

I feel like this is a bit broad. Unless you want to really maintain rtc-mt* which I guess may not be only Mediatek in the future, I would use the full filename here. You will probably remember to update it if you add more, that may not be the case for other developers.

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

Re: [rtc-linux] [PATCH v3 3/3] MAINTAINERS: add Mediatek RTC driver

Hi Alexandre,

Quoted 11 lines · Alexandre Belloni

Hi,

Quoted 8 lines · Eddie Huang
MAINTAINERS +0 -0
index 2e5bbc0..eb80610 100644
@@ -1223,6 +1223,13 @@ W:	http://www.digriz.org.uk/ts78xx/kernel
 S:	Maintained
 F:	arch/arm/mach-orion5x/ts78xx-*
 
Quoted 12 lines

MAINTAINERS W: http://www.digriz.org.uk/ts78xx/kernel · line 1231

Quoted 6 lines
MAINTAINERS +6 -0
+ARM/Mediatek RTC DRIVER
+M:	Eddie Huang <[email protected]>
+L:	linux-arm-kernel at lists.infradead.org (moderated for non-subscribers)
+L:	linux-mediatek at lists.infradead.org (moderated for non-subscribers)
+S:	Maintained
+F:	drivers/rtc/rtc-mt*

I feel like this is a bit broad. Unless you want to really maintain rtc-mt* which I guess may not be only Mediatek in the future, I would use the full filename here. You will probably remember to update it if you add more, that may not be the case for other developers.

Yes, you are right, the rtc-mt* is too broad, use full filename is a better choice right now.

Eddie